bpf_jit_comp.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428
  1. /*
  2. * BPF Jit compiler for s390.
  3. *
  4. * Minimum build requirements:
  5. *
  6. * - HAVE_MARCH_Z196_FEATURES: laal, laalg
  7. * - HAVE_MARCH_Z10_FEATURES: msfi, cgrj, clgrj
  8. * - HAVE_MARCH_Z9_109_FEATURES: alfi, llilf, clfi, oilf, nilf
  9. * - PACK_STACK
  10. * - 64BIT
  11. *
  12. * Copyright IBM Corp. 2012,2015
  13. *
  14. * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
  15. * Michael Holzheu <holzheu@linux.vnet.ibm.com>
  16. */
  17. #define KMSG_COMPONENT "bpf_jit"
  18. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  19. #include <linux/netdevice.h>
  20. #include <linux/filter.h>
  21. #include <linux/init.h>
  22. #include <linux/bpf.h>
  23. #include <asm/cacheflush.h>
  24. #include <asm/dis.h>
  25. #include <asm/facility.h>
  26. #include <asm/nospec-branch.h>
  27. #include "bpf_jit.h"
  28. int bpf_jit_enable __read_mostly;
  29. struct bpf_jit {
  30. u32 seen; /* Flags to remember seen eBPF instructions */
  31. u32 seen_reg[16]; /* Array to remember which registers are used */
  32. u32 *addrs; /* Array with relative instruction addresses */
  33. u8 *prg_buf; /* Start of program */
  34. int size; /* Size of program and literal pool */
  35. int size_prg; /* Size of program */
  36. int prg; /* Current position in program */
  37. int lit_start; /* Start of literal pool */
  38. int lit; /* Current position in literal pool */
  39. int base_ip; /* Base address for literal pool */
  40. int ret0_ip; /* Address of return 0 */
  41. int exit_ip; /* Address of exit */
  42. int r1_thunk_ip; /* Address of expoline thunk for 'br %r1' */
  43. int r14_thunk_ip; /* Address of expoline thunk for 'br %r14' */
  44. int tail_call_start; /* Tail call start offset */
  45. int labels[1]; /* Labels for local jumps */
  46. };
  47. #define BPF_SIZE_MAX 0xffff /* Max size for program (16 bit branches) */
  48. #define SEEN_SKB 1 /* skb access */
  49. #define SEEN_MEM 2 /* use mem[] for temporary storage */
  50. #define SEEN_RET0 4 /* ret0_ip points to a valid return 0 */
  51. #define SEEN_LITERAL 8 /* code uses literals */
  52. #define SEEN_FUNC 16 /* calls C functions */
  53. #define SEEN_TAIL_CALL 32 /* code uses tail calls */
  54. #define SEEN_SKB_CHANGE 64 /* code changes skb data */
  55. #define SEEN_REG_AX 128 /* code uses constant blinding */
  56. #define SEEN_STACK (SEEN_FUNC | SEEN_MEM | SEEN_SKB)
  57. /*
  58. * s390 registers
  59. */
  60. #define REG_W0 (MAX_BPF_JIT_REG + 0) /* Work register 1 (even) */
  61. #define REG_W1 (MAX_BPF_JIT_REG + 1) /* Work register 2 (odd) */
  62. #define REG_SKB_DATA (MAX_BPF_JIT_REG + 2) /* SKB data register */
  63. #define REG_L (MAX_BPF_JIT_REG + 3) /* Literal pool register */
  64. #define REG_15 (MAX_BPF_JIT_REG + 4) /* Register 15 */
  65. #define REG_0 REG_W0 /* Register 0 */
  66. #define REG_1 REG_W1 /* Register 1 */
  67. #define REG_2 BPF_REG_1 /* Register 2 */
  68. #define REG_14 BPF_REG_0 /* Register 14 */
  69. /*
  70. * Mapping of BPF registers to s390 registers
  71. */
  72. static const int reg2hex[] = {
  73. /* Return code */
  74. [BPF_REG_0] = 14,
  75. /* Function parameters */
  76. [BPF_REG_1] = 2,
  77. [BPF_REG_2] = 3,
  78. [BPF_REG_3] = 4,
  79. [BPF_REG_4] = 5,
  80. [BPF_REG_5] = 6,
  81. /* Call saved registers */
  82. [BPF_REG_6] = 7,
  83. [BPF_REG_7] = 8,
  84. [BPF_REG_8] = 9,
  85. [BPF_REG_9] = 10,
  86. /* BPF stack pointer */
  87. [BPF_REG_FP] = 13,
  88. /* Register for blinding (shared with REG_SKB_DATA) */
  89. [BPF_REG_AX] = 12,
  90. /* SKB data pointer */
  91. [REG_SKB_DATA] = 12,
  92. /* Work registers for s390x backend */
  93. [REG_W0] = 0,
  94. [REG_W1] = 1,
  95. [REG_L] = 11,
  96. [REG_15] = 15,
  97. };
  98. static inline u32 reg(u32 dst_reg, u32 src_reg)
  99. {
  100. return reg2hex[dst_reg] << 4 | reg2hex[src_reg];
  101. }
  102. static inline u32 reg_high(u32 reg)
  103. {
  104. return reg2hex[reg] << 4;
  105. }
  106. static inline void reg_set_seen(struct bpf_jit *jit, u32 b1)
  107. {
  108. u32 r1 = reg2hex[b1];
  109. if (!jit->seen_reg[r1] && r1 >= 6 && r1 <= 15)
  110. jit->seen_reg[r1] = 1;
  111. }
  112. #define REG_SET_SEEN(b1) \
  113. ({ \
  114. reg_set_seen(jit, b1); \
  115. })
  116. #define REG_SEEN(b1) jit->seen_reg[reg2hex[(b1)]]
  117. /*
  118. * EMIT macros for code generation
  119. */
  120. #define _EMIT2(op) \
  121. ({ \
  122. if (jit->prg_buf) \
  123. *(u16 *) (jit->prg_buf + jit->prg) = op; \
  124. jit->prg += 2; \
  125. })
  126. #define EMIT2(op, b1, b2) \
  127. ({ \
  128. _EMIT2(op | reg(b1, b2)); \
  129. REG_SET_SEEN(b1); \
  130. REG_SET_SEEN(b2); \
  131. })
  132. #define _EMIT4(op) \
  133. ({ \
  134. if (jit->prg_buf) \
  135. *(u32 *) (jit->prg_buf + jit->prg) = op; \
  136. jit->prg += 4; \
  137. })
  138. #define EMIT4(op, b1, b2) \
  139. ({ \
  140. _EMIT4(op | reg(b1, b2)); \
  141. REG_SET_SEEN(b1); \
  142. REG_SET_SEEN(b2); \
  143. })
  144. #define EMIT4_RRF(op, b1, b2, b3) \
  145. ({ \
  146. _EMIT4(op | reg_high(b3) << 8 | reg(b1, b2)); \
  147. REG_SET_SEEN(b1); \
  148. REG_SET_SEEN(b2); \
  149. REG_SET_SEEN(b3); \
  150. })
  151. #define _EMIT4_DISP(op, disp) \
  152. ({ \
  153. unsigned int __disp = (disp) & 0xfff; \
  154. _EMIT4(op | __disp); \
  155. })
  156. #define EMIT4_DISP(op, b1, b2, disp) \
  157. ({ \
  158. _EMIT4_DISP(op | reg_high(b1) << 16 | \
  159. reg_high(b2) << 8, disp); \
  160. REG_SET_SEEN(b1); \
  161. REG_SET_SEEN(b2); \
  162. })
  163. #define EMIT4_IMM(op, b1, imm) \
  164. ({ \
  165. unsigned int __imm = (imm) & 0xffff; \
  166. _EMIT4(op | reg_high(b1) << 16 | __imm); \
  167. REG_SET_SEEN(b1); \
  168. })
  169. #define EMIT4_PCREL(op, pcrel) \
  170. ({ \
  171. long __pcrel = ((pcrel) >> 1) & 0xffff; \
  172. _EMIT4(op | __pcrel); \
  173. })
  174. #define _EMIT6(op1, op2) \
  175. ({ \
  176. if (jit->prg_buf) { \
  177. *(u32 *) (jit->prg_buf + jit->prg) = op1; \
  178. *(u16 *) (jit->prg_buf + jit->prg + 4) = op2; \
  179. } \
  180. jit->prg += 6; \
  181. })
  182. #define _EMIT6_DISP(op1, op2, disp) \
  183. ({ \
  184. unsigned int __disp = (disp) & 0xfff; \
  185. _EMIT6(op1 | __disp, op2); \
  186. })
  187. #define _EMIT6_DISP_LH(op1, op2, disp) \
  188. ({ \
  189. u32 _disp = (u32) disp; \
  190. unsigned int __disp_h = _disp & 0xff000; \
  191. unsigned int __disp_l = _disp & 0x00fff; \
  192. _EMIT6(op1 | __disp_l, op2 | __disp_h >> 4); \
  193. })
  194. #define EMIT6_DISP_LH(op1, op2, b1, b2, b3, disp) \
  195. ({ \
  196. _EMIT6_DISP_LH(op1 | reg(b1, b2) << 16 | \
  197. reg_high(b3) << 8, op2, disp); \
  198. REG_SET_SEEN(b1); \
  199. REG_SET_SEEN(b2); \
  200. REG_SET_SEEN(b3); \
  201. })
  202. #define EMIT6_PCREL_LABEL(op1, op2, b1, b2, label, mask) \
  203. ({ \
  204. int rel = (jit->labels[label] - jit->prg) >> 1; \
  205. _EMIT6(op1 | reg(b1, b2) << 16 | (rel & 0xffff), \
  206. op2 | mask << 12); \
  207. REG_SET_SEEN(b1); \
  208. REG_SET_SEEN(b2); \
  209. })
  210. #define EMIT6_PCREL_IMM_LABEL(op1, op2, b1, imm, label, mask) \
  211. ({ \
  212. int rel = (jit->labels[label] - jit->prg) >> 1; \
  213. _EMIT6(op1 | (reg_high(b1) | mask) << 16 | \
  214. (rel & 0xffff), op2 | (imm & 0xff) << 8); \
  215. REG_SET_SEEN(b1); \
  216. BUILD_BUG_ON(((unsigned long) imm) > 0xff); \
  217. })
  218. #define EMIT6_PCREL(op1, op2, b1, b2, i, off, mask) \
  219. ({ \
  220. /* Branch instruction needs 6 bytes */ \
  221. int rel = (addrs[i + off + 1] - (addrs[i + 1] - 6)) / 2;\
  222. _EMIT6(op1 | reg(b1, b2) << 16 | (rel & 0xffff), op2 | mask); \
  223. REG_SET_SEEN(b1); \
  224. REG_SET_SEEN(b2); \
  225. })
  226. #define EMIT6_PCREL_RILB(op, b, target) \
  227. ({ \
  228. int rel = (target - jit->prg) / 2; \
  229. _EMIT6(op | reg_high(b) << 16 | rel >> 16, rel & 0xffff); \
  230. REG_SET_SEEN(b); \
  231. })
  232. #define EMIT6_PCREL_RIL(op, target) \
  233. ({ \
  234. int rel = (target - jit->prg) / 2; \
  235. _EMIT6(op | rel >> 16, rel & 0xffff); \
  236. })
  237. #define _EMIT6_IMM(op, imm) \
  238. ({ \
  239. unsigned int __imm = (imm); \
  240. _EMIT6(op | (__imm >> 16), __imm & 0xffff); \
  241. })
  242. #define EMIT6_IMM(op, b1, imm) \
  243. ({ \
  244. _EMIT6_IMM(op | reg_high(b1) << 16, imm); \
  245. REG_SET_SEEN(b1); \
  246. })
  247. #define EMIT_CONST_U32(val) \
  248. ({ \
  249. unsigned int ret; \
  250. ret = jit->lit - jit->base_ip; \
  251. jit->seen |= SEEN_LITERAL; \
  252. if (jit->prg_buf) \
  253. *(u32 *) (jit->prg_buf + jit->lit) = (u32) val; \
  254. jit->lit += 4; \
  255. ret; \
  256. })
  257. #define EMIT_CONST_U64(val) \
  258. ({ \
  259. unsigned int ret; \
  260. ret = jit->lit - jit->base_ip; \
  261. jit->seen |= SEEN_LITERAL; \
  262. if (jit->prg_buf) \
  263. *(u64 *) (jit->prg_buf + jit->lit) = (u64) val; \
  264. jit->lit += 8; \
  265. ret; \
  266. })
  267. #define EMIT_ZERO(b1) \
  268. ({ \
  269. /* llgfr %dst,%dst (zero extend to 64 bit) */ \
  270. EMIT4(0xb9160000, b1, b1); \
  271. REG_SET_SEEN(b1); \
  272. })
  273. /*
  274. * Fill whole space with illegal instructions
  275. */
  276. static void jit_fill_hole(void *area, unsigned int size)
  277. {
  278. memset(area, 0, size);
  279. }
  280. /*
  281. * Save registers from "rs" (register start) to "re" (register end) on stack
  282. */
  283. static void save_regs(struct bpf_jit *jit, u32 rs, u32 re)
  284. {
  285. u32 off = STK_OFF_R6 + (rs - 6) * 8;
  286. if (rs == re)
  287. /* stg %rs,off(%r15) */
  288. _EMIT6(0xe300f000 | rs << 20 | off, 0x0024);
  289. else
  290. /* stmg %rs,%re,off(%r15) */
  291. _EMIT6_DISP(0xeb00f000 | rs << 20 | re << 16, 0x0024, off);
  292. }
  293. /*
  294. * Restore registers from "rs" (register start) to "re" (register end) on stack
  295. */
  296. static void restore_regs(struct bpf_jit *jit, u32 rs, u32 re)
  297. {
  298. u32 off = STK_OFF_R6 + (rs - 6) * 8;
  299. if (jit->seen & SEEN_STACK)
  300. off += STK_OFF;
  301. if (rs == re)
  302. /* lg %rs,off(%r15) */
  303. _EMIT6(0xe300f000 | rs << 20 | off, 0x0004);
  304. else
  305. /* lmg %rs,%re,off(%r15) */
  306. _EMIT6_DISP(0xeb00f000 | rs << 20 | re << 16, 0x0004, off);
  307. }
  308. /*
  309. * Return first seen register (from start)
  310. */
  311. static int get_start(struct bpf_jit *jit, int start)
  312. {
  313. int i;
  314. for (i = start; i <= 15; i++) {
  315. if (jit->seen_reg[i])
  316. return i;
  317. }
  318. return 0;
  319. }
  320. /*
  321. * Return last seen register (from start) (gap >= 2)
  322. */
  323. static int get_end(struct bpf_jit *jit, int start)
  324. {
  325. int i;
  326. for (i = start; i < 15; i++) {
  327. if (!jit->seen_reg[i] && !jit->seen_reg[i + 1])
  328. return i - 1;
  329. }
  330. return jit->seen_reg[15] ? 15 : 14;
  331. }
  332. #define REGS_SAVE 1
  333. #define REGS_RESTORE 0
  334. /*
  335. * Save and restore clobbered registers (6-15) on stack.
  336. * We save/restore registers in chunks with gap >= 2 registers.
  337. */
  338. static void save_restore_regs(struct bpf_jit *jit, int op)
  339. {
  340. int re = 6, rs;
  341. do {
  342. rs = get_start(jit, re);
  343. if (!rs)
  344. break;
  345. re = get_end(jit, rs + 1);
  346. if (op == REGS_SAVE)
  347. save_regs(jit, rs, re);
  348. else
  349. restore_regs(jit, rs, re);
  350. re++;
  351. } while (re <= 15);
  352. }
  353. /*
  354. * For SKB access %b1 contains the SKB pointer. For "bpf_jit.S"
  355. * we store the SKB header length on the stack and the SKB data
  356. * pointer in REG_SKB_DATA if BPF_REG_AX is not used.
  357. */
  358. static void emit_load_skb_data_hlen(struct bpf_jit *jit)
  359. {
  360. /* Header length: llgf %w1,<len>(%b1) */
  361. EMIT6_DISP_LH(0xe3000000, 0x0016, REG_W1, REG_0, BPF_REG_1,
  362. offsetof(struct sk_buff, len));
  363. /* s %w1,<data_len>(%b1) */
  364. EMIT4_DISP(0x5b000000, REG_W1, BPF_REG_1,
  365. offsetof(struct sk_buff, data_len));
  366. /* stg %w1,ST_OFF_HLEN(%r0,%r15) */
  367. EMIT6_DISP_LH(0xe3000000, 0x0024, REG_W1, REG_0, REG_15, STK_OFF_HLEN);
  368. if (!(jit->seen & SEEN_REG_AX))
  369. /* lg %skb_data,data_off(%b1) */
  370. EMIT6_DISP_LH(0xe3000000, 0x0004, REG_SKB_DATA, REG_0,
  371. BPF_REG_1, offsetof(struct sk_buff, data));
  372. }
  373. /*
  374. * Emit function prologue
  375. *
  376. * Save registers and create stack frame if necessary.
  377. * See stack frame layout desription in "bpf_jit.h"!
  378. */
  379. static void bpf_jit_prologue(struct bpf_jit *jit)
  380. {
  381. if (jit->seen & SEEN_TAIL_CALL) {
  382. /* xc STK_OFF_TCCNT(4,%r15),STK_OFF_TCCNT(%r15) */
  383. _EMIT6(0xd703f000 | STK_OFF_TCCNT, 0xf000 | STK_OFF_TCCNT);
  384. } else {
  385. /* j tail_call_start: NOP if no tail calls are used */
  386. EMIT4_PCREL(0xa7f40000, 6);
  387. _EMIT2(0);
  388. }
  389. /* Tail calls have to skip above initialization */
  390. jit->tail_call_start = jit->prg;
  391. /* Save registers */
  392. save_restore_regs(jit, REGS_SAVE);
  393. /* Setup literal pool */
  394. if (jit->seen & SEEN_LITERAL) {
  395. /* basr %r13,0 */
  396. EMIT2(0x0d00, REG_L, REG_0);
  397. jit->base_ip = jit->prg;
  398. }
  399. /* Setup stack and backchain */
  400. if (jit->seen & SEEN_STACK) {
  401. if (jit->seen & SEEN_FUNC)
  402. /* lgr %w1,%r15 (backchain) */
  403. EMIT4(0xb9040000, REG_W1, REG_15);
  404. /* la %bfp,STK_160_UNUSED(%r15) (BPF frame pointer) */
  405. EMIT4_DISP(0x41000000, BPF_REG_FP, REG_15, STK_160_UNUSED);
  406. /* aghi %r15,-STK_OFF */
  407. EMIT4_IMM(0xa70b0000, REG_15, -STK_OFF);
  408. if (jit->seen & SEEN_FUNC)
  409. /* stg %w1,152(%r15) (backchain) */
  410. EMIT6_DISP_LH(0xe3000000, 0x0024, REG_W1, REG_0,
  411. REG_15, 152);
  412. }
  413. if (jit->seen & SEEN_SKB)
  414. emit_load_skb_data_hlen(jit);
  415. if (jit->seen & SEEN_SKB_CHANGE)
  416. /* stg %b1,ST_OFF_SKBP(%r0,%r15) */
  417. EMIT6_DISP_LH(0xe3000000, 0x0024, BPF_REG_1, REG_0, REG_15,
  418. STK_OFF_SKBP);
  419. }
  420. /*
  421. * Function epilogue
  422. */
  423. static void bpf_jit_epilogue(struct bpf_jit *jit)
  424. {
  425. /* Return 0 */
  426. if (jit->seen & SEEN_RET0) {
  427. jit->ret0_ip = jit->prg;
  428. /* lghi %b0,0 */
  429. EMIT4_IMM(0xa7090000, BPF_REG_0, 0);
  430. }
  431. jit->exit_ip = jit->prg;
  432. /* Load exit code: lgr %r2,%b0 */
  433. EMIT4(0xb9040000, REG_2, BPF_REG_0);
  434. /* Restore registers */
  435. save_restore_regs(jit, REGS_RESTORE);
  436. if (IS_ENABLED(CC_USING_EXPOLINE) && !nospec_disable) {
  437. jit->r14_thunk_ip = jit->prg;
  438. /* Generate __s390_indirect_jump_r14 thunk */
  439. if (test_facility(35)) {
  440. /* exrl %r0,.+10 */
  441. EMIT6_PCREL_RIL(0xc6000000, jit->prg + 10);
  442. } else {
  443. /* larl %r1,.+14 */
  444. EMIT6_PCREL_RILB(0xc0000000, REG_1, jit->prg + 14);
  445. /* ex 0,0(%r1) */
  446. EMIT4_DISP(0x44000000, REG_0, REG_1, 0);
  447. }
  448. /* j . */
  449. EMIT4_PCREL(0xa7f40000, 0);
  450. }
  451. /* br %r14 */
  452. _EMIT2(0x07fe);
  453. if (IS_ENABLED(CC_USING_EXPOLINE) && !nospec_disable &&
  454. (jit->seen & SEEN_FUNC)) {
  455. jit->r1_thunk_ip = jit->prg;
  456. /* Generate __s390_indirect_jump_r1 thunk */
  457. if (test_facility(35)) {
  458. /* exrl %r0,.+10 */
  459. EMIT6_PCREL_RIL(0xc6000000, jit->prg + 10);
  460. /* j . */
  461. EMIT4_PCREL(0xa7f40000, 0);
  462. /* br %r1 */
  463. _EMIT2(0x07f1);
  464. } else {
  465. /* larl %r1,.+14 */
  466. EMIT6_PCREL_RILB(0xc0000000, REG_1, jit->prg + 14);
  467. /* ex 0,S390_lowcore.br_r1_tampoline */
  468. EMIT4_DISP(0x44000000, REG_0, REG_0,
  469. offsetof(struct lowcore, br_r1_trampoline));
  470. /* j . */
  471. EMIT4_PCREL(0xa7f40000, 0);
  472. }
  473. }
  474. }
  475. /*
  476. * Compile one eBPF instruction into s390x code
  477. *
  478. * NOTE: Use noinline because for gcov (-fprofile-arcs) gcc allocates a lot of
  479. * stack space for the large switch statement.
  480. */
  481. static noinline int bpf_jit_insn(struct bpf_jit *jit, struct bpf_prog *fp, int i)
  482. {
  483. struct bpf_insn *insn = &fp->insnsi[i];
  484. int jmp_off, last, insn_count = 1;
  485. unsigned int func_addr, mask;
  486. u32 dst_reg = insn->dst_reg;
  487. u32 src_reg = insn->src_reg;
  488. u32 *addrs = jit->addrs;
  489. s32 imm = insn->imm;
  490. s16 off = insn->off;
  491. if (dst_reg == BPF_REG_AX || src_reg == BPF_REG_AX)
  492. jit->seen |= SEEN_REG_AX;
  493. switch (insn->code) {
  494. /*
  495. * BPF_MOV
  496. */
  497. case BPF_ALU | BPF_MOV | BPF_X: /* dst = (u32) src */
  498. /* llgfr %dst,%src */
  499. EMIT4(0xb9160000, dst_reg, src_reg);
  500. break;
  501. case BPF_ALU64 | BPF_MOV | BPF_X: /* dst = src */
  502. /* lgr %dst,%src */
  503. EMIT4(0xb9040000, dst_reg, src_reg);
  504. break;
  505. case BPF_ALU | BPF_MOV | BPF_K: /* dst = (u32) imm */
  506. /* llilf %dst,imm */
  507. EMIT6_IMM(0xc00f0000, dst_reg, imm);
  508. break;
  509. case BPF_ALU64 | BPF_MOV | BPF_K: /* dst = imm */
  510. /* lgfi %dst,imm */
  511. EMIT6_IMM(0xc0010000, dst_reg, imm);
  512. break;
  513. /*
  514. * BPF_LD 64
  515. */
  516. case BPF_LD | BPF_IMM | BPF_DW: /* dst = (u64) imm */
  517. {
  518. /* 16 byte instruction that uses two 'struct bpf_insn' */
  519. u64 imm64;
  520. imm64 = (u64)(u32) insn[0].imm | ((u64)(u32) insn[1].imm) << 32;
  521. /* lg %dst,<d(imm)>(%l) */
  522. EMIT6_DISP_LH(0xe3000000, 0x0004, dst_reg, REG_0, REG_L,
  523. EMIT_CONST_U64(imm64));
  524. insn_count = 2;
  525. break;
  526. }
  527. /*
  528. * BPF_ADD
  529. */
  530. case BPF_ALU | BPF_ADD | BPF_X: /* dst = (u32) dst + (u32) src */
  531. /* ar %dst,%src */
  532. EMIT2(0x1a00, dst_reg, src_reg);
  533. EMIT_ZERO(dst_reg);
  534. break;
  535. case BPF_ALU64 | BPF_ADD | BPF_X: /* dst = dst + src */
  536. /* agr %dst,%src */
  537. EMIT4(0xb9080000, dst_reg, src_reg);
  538. break;
  539. case BPF_ALU | BPF_ADD | BPF_K: /* dst = (u32) dst + (u32) imm */
  540. if (!imm)
  541. break;
  542. /* alfi %dst,imm */
  543. EMIT6_IMM(0xc20b0000, dst_reg, imm);
  544. EMIT_ZERO(dst_reg);
  545. break;
  546. case BPF_ALU64 | BPF_ADD | BPF_K: /* dst = dst + imm */
  547. if (!imm)
  548. break;
  549. /* agfi %dst,imm */
  550. EMIT6_IMM(0xc2080000, dst_reg, imm);
  551. break;
  552. /*
  553. * BPF_SUB
  554. */
  555. case BPF_ALU | BPF_SUB | BPF_X: /* dst = (u32) dst - (u32) src */
  556. /* sr %dst,%src */
  557. EMIT2(0x1b00, dst_reg, src_reg);
  558. EMIT_ZERO(dst_reg);
  559. break;
  560. case BPF_ALU64 | BPF_SUB | BPF_X: /* dst = dst - src */
  561. /* sgr %dst,%src */
  562. EMIT4(0xb9090000, dst_reg, src_reg);
  563. break;
  564. case BPF_ALU | BPF_SUB | BPF_K: /* dst = (u32) dst - (u32) imm */
  565. if (!imm)
  566. break;
  567. /* alfi %dst,-imm */
  568. EMIT6_IMM(0xc20b0000, dst_reg, -imm);
  569. EMIT_ZERO(dst_reg);
  570. break;
  571. case BPF_ALU64 | BPF_SUB | BPF_K: /* dst = dst - imm */
  572. if (!imm)
  573. break;
  574. /* agfi %dst,-imm */
  575. EMIT6_IMM(0xc2080000, dst_reg, -imm);
  576. break;
  577. /*
  578. * BPF_MUL
  579. */
  580. case BPF_ALU | BPF_MUL | BPF_X: /* dst = (u32) dst * (u32) src */
  581. /* msr %dst,%src */
  582. EMIT4(0xb2520000, dst_reg, src_reg);
  583. EMIT_ZERO(dst_reg);
  584. break;
  585. case BPF_ALU64 | BPF_MUL | BPF_X: /* dst = dst * src */
  586. /* msgr %dst,%src */
  587. EMIT4(0xb90c0000, dst_reg, src_reg);
  588. break;
  589. case BPF_ALU | BPF_MUL | BPF_K: /* dst = (u32) dst * (u32) imm */
  590. if (imm == 1)
  591. break;
  592. /* msfi %r5,imm */
  593. EMIT6_IMM(0xc2010000, dst_reg, imm);
  594. EMIT_ZERO(dst_reg);
  595. break;
  596. case BPF_ALU64 | BPF_MUL | BPF_K: /* dst = dst * imm */
  597. if (imm == 1)
  598. break;
  599. /* msgfi %dst,imm */
  600. EMIT6_IMM(0xc2000000, dst_reg, imm);
  601. break;
  602. /*
  603. * BPF_DIV / BPF_MOD
  604. */
  605. case BPF_ALU | BPF_DIV | BPF_X: /* dst = (u32) dst / (u32) src */
  606. case BPF_ALU | BPF_MOD | BPF_X: /* dst = (u32) dst % (u32) src */
  607. {
  608. int rc_reg = BPF_OP(insn->code) == BPF_DIV ? REG_W1 : REG_W0;
  609. jit->seen |= SEEN_RET0;
  610. /* ltr %src,%src (if src == 0 goto fail) */
  611. EMIT2(0x1200, src_reg, src_reg);
  612. /* jz <ret0> */
  613. EMIT4_PCREL(0xa7840000, jit->ret0_ip - jit->prg);
  614. /* lhi %w0,0 */
  615. EMIT4_IMM(0xa7080000, REG_W0, 0);
  616. /* lr %w1,%dst */
  617. EMIT2(0x1800, REG_W1, dst_reg);
  618. /* dlr %w0,%src */
  619. EMIT4(0xb9970000, REG_W0, src_reg);
  620. /* llgfr %dst,%rc */
  621. EMIT4(0xb9160000, dst_reg, rc_reg);
  622. break;
  623. }
  624. case BPF_ALU64 | BPF_DIV | BPF_X: /* dst = dst / src */
  625. case BPF_ALU64 | BPF_MOD | BPF_X: /* dst = dst % src */
  626. {
  627. int rc_reg = BPF_OP(insn->code) == BPF_DIV ? REG_W1 : REG_W0;
  628. jit->seen |= SEEN_RET0;
  629. /* ltgr %src,%src (if src == 0 goto fail) */
  630. EMIT4(0xb9020000, src_reg, src_reg);
  631. /* jz <ret0> */
  632. EMIT4_PCREL(0xa7840000, jit->ret0_ip - jit->prg);
  633. /* lghi %w0,0 */
  634. EMIT4_IMM(0xa7090000, REG_W0, 0);
  635. /* lgr %w1,%dst */
  636. EMIT4(0xb9040000, REG_W1, dst_reg);
  637. /* dlgr %w0,%dst */
  638. EMIT4(0xb9870000, REG_W0, src_reg);
  639. /* lgr %dst,%rc */
  640. EMIT4(0xb9040000, dst_reg, rc_reg);
  641. break;
  642. }
  643. case BPF_ALU | BPF_DIV | BPF_K: /* dst = (u32) dst / (u32) imm */
  644. case BPF_ALU | BPF_MOD | BPF_K: /* dst = (u32) dst % (u32) imm */
  645. {
  646. int rc_reg = BPF_OP(insn->code) == BPF_DIV ? REG_W1 : REG_W0;
  647. if (imm == 1) {
  648. if (BPF_OP(insn->code) == BPF_MOD)
  649. /* lhgi %dst,0 */
  650. EMIT4_IMM(0xa7090000, dst_reg, 0);
  651. break;
  652. }
  653. /* lhi %w0,0 */
  654. EMIT4_IMM(0xa7080000, REG_W0, 0);
  655. /* lr %w1,%dst */
  656. EMIT2(0x1800, REG_W1, dst_reg);
  657. /* dl %w0,<d(imm)>(%l) */
  658. EMIT6_DISP_LH(0xe3000000, 0x0097, REG_W0, REG_0, REG_L,
  659. EMIT_CONST_U32(imm));
  660. /* llgfr %dst,%rc */
  661. EMIT4(0xb9160000, dst_reg, rc_reg);
  662. break;
  663. }
  664. case BPF_ALU64 | BPF_DIV | BPF_K: /* dst = dst / imm */
  665. case BPF_ALU64 | BPF_MOD | BPF_K: /* dst = dst % imm */
  666. {
  667. int rc_reg = BPF_OP(insn->code) == BPF_DIV ? REG_W1 : REG_W0;
  668. if (imm == 1) {
  669. if (BPF_OP(insn->code) == BPF_MOD)
  670. /* lhgi %dst,0 */
  671. EMIT4_IMM(0xa7090000, dst_reg, 0);
  672. break;
  673. }
  674. /* lghi %w0,0 */
  675. EMIT4_IMM(0xa7090000, REG_W0, 0);
  676. /* lgr %w1,%dst */
  677. EMIT4(0xb9040000, REG_W1, dst_reg);
  678. /* dlg %w0,<d(imm)>(%l) */
  679. EMIT6_DISP_LH(0xe3000000, 0x0087, REG_W0, REG_0, REG_L,
  680. EMIT_CONST_U64(imm));
  681. /* lgr %dst,%rc */
  682. EMIT4(0xb9040000, dst_reg, rc_reg);
  683. break;
  684. }
  685. /*
  686. * BPF_AND
  687. */
  688. case BPF_ALU | BPF_AND | BPF_X: /* dst = (u32) dst & (u32) src */
  689. /* nr %dst,%src */
  690. EMIT2(0x1400, dst_reg, src_reg);
  691. EMIT_ZERO(dst_reg);
  692. break;
  693. case BPF_ALU64 | BPF_AND | BPF_X: /* dst = dst & src */
  694. /* ngr %dst,%src */
  695. EMIT4(0xb9800000, dst_reg, src_reg);
  696. break;
  697. case BPF_ALU | BPF_AND | BPF_K: /* dst = (u32) dst & (u32) imm */
  698. /* nilf %dst,imm */
  699. EMIT6_IMM(0xc00b0000, dst_reg, imm);
  700. EMIT_ZERO(dst_reg);
  701. break;
  702. case BPF_ALU64 | BPF_AND | BPF_K: /* dst = dst & imm */
  703. /* ng %dst,<d(imm)>(%l) */
  704. EMIT6_DISP_LH(0xe3000000, 0x0080, dst_reg, REG_0, REG_L,
  705. EMIT_CONST_U64(imm));
  706. break;
  707. /*
  708. * BPF_OR
  709. */
  710. case BPF_ALU | BPF_OR | BPF_X: /* dst = (u32) dst | (u32) src */
  711. /* or %dst,%src */
  712. EMIT2(0x1600, dst_reg, src_reg);
  713. EMIT_ZERO(dst_reg);
  714. break;
  715. case BPF_ALU64 | BPF_OR | BPF_X: /* dst = dst | src */
  716. /* ogr %dst,%src */
  717. EMIT4(0xb9810000, dst_reg, src_reg);
  718. break;
  719. case BPF_ALU | BPF_OR | BPF_K: /* dst = (u32) dst | (u32) imm */
  720. /* oilf %dst,imm */
  721. EMIT6_IMM(0xc00d0000, dst_reg, imm);
  722. EMIT_ZERO(dst_reg);
  723. break;
  724. case BPF_ALU64 | BPF_OR | BPF_K: /* dst = dst | imm */
  725. /* og %dst,<d(imm)>(%l) */
  726. EMIT6_DISP_LH(0xe3000000, 0x0081, dst_reg, REG_0, REG_L,
  727. EMIT_CONST_U64(imm));
  728. break;
  729. /*
  730. * BPF_XOR
  731. */
  732. case BPF_ALU | BPF_XOR | BPF_X: /* dst = (u32) dst ^ (u32) src */
  733. /* xr %dst,%src */
  734. EMIT2(0x1700, dst_reg, src_reg);
  735. EMIT_ZERO(dst_reg);
  736. break;
  737. case BPF_ALU64 | BPF_XOR | BPF_X: /* dst = dst ^ src */
  738. /* xgr %dst,%src */
  739. EMIT4(0xb9820000, dst_reg, src_reg);
  740. break;
  741. case BPF_ALU | BPF_XOR | BPF_K: /* dst = (u32) dst ^ (u32) imm */
  742. if (!imm)
  743. break;
  744. /* xilf %dst,imm */
  745. EMIT6_IMM(0xc0070000, dst_reg, imm);
  746. EMIT_ZERO(dst_reg);
  747. break;
  748. case BPF_ALU64 | BPF_XOR | BPF_K: /* dst = dst ^ imm */
  749. /* xg %dst,<d(imm)>(%l) */
  750. EMIT6_DISP_LH(0xe3000000, 0x0082, dst_reg, REG_0, REG_L,
  751. EMIT_CONST_U64(imm));
  752. break;
  753. /*
  754. * BPF_LSH
  755. */
  756. case BPF_ALU | BPF_LSH | BPF_X: /* dst = (u32) dst << (u32) src */
  757. /* sll %dst,0(%src) */
  758. EMIT4_DISP(0x89000000, dst_reg, src_reg, 0);
  759. EMIT_ZERO(dst_reg);
  760. break;
  761. case BPF_ALU64 | BPF_LSH | BPF_X: /* dst = dst << src */
  762. /* sllg %dst,%dst,0(%src) */
  763. EMIT6_DISP_LH(0xeb000000, 0x000d, dst_reg, dst_reg, src_reg, 0);
  764. break;
  765. case BPF_ALU | BPF_LSH | BPF_K: /* dst = (u32) dst << (u32) imm */
  766. if (imm == 0)
  767. break;
  768. /* sll %dst,imm(%r0) */
  769. EMIT4_DISP(0x89000000, dst_reg, REG_0, imm);
  770. EMIT_ZERO(dst_reg);
  771. break;
  772. case BPF_ALU64 | BPF_LSH | BPF_K: /* dst = dst << imm */
  773. if (imm == 0)
  774. break;
  775. /* sllg %dst,%dst,imm(%r0) */
  776. EMIT6_DISP_LH(0xeb000000, 0x000d, dst_reg, dst_reg, REG_0, imm);
  777. break;
  778. /*
  779. * BPF_RSH
  780. */
  781. case BPF_ALU | BPF_RSH | BPF_X: /* dst = (u32) dst >> (u32) src */
  782. /* srl %dst,0(%src) */
  783. EMIT4_DISP(0x88000000, dst_reg, src_reg, 0);
  784. EMIT_ZERO(dst_reg);
  785. break;
  786. case BPF_ALU64 | BPF_RSH | BPF_X: /* dst = dst >> src */
  787. /* srlg %dst,%dst,0(%src) */
  788. EMIT6_DISP_LH(0xeb000000, 0x000c, dst_reg, dst_reg, src_reg, 0);
  789. break;
  790. case BPF_ALU | BPF_RSH | BPF_K: /* dst = (u32) dst >> (u32) imm */
  791. if (imm == 0)
  792. break;
  793. /* srl %dst,imm(%r0) */
  794. EMIT4_DISP(0x88000000, dst_reg, REG_0, imm);
  795. EMIT_ZERO(dst_reg);
  796. break;
  797. case BPF_ALU64 | BPF_RSH | BPF_K: /* dst = dst >> imm */
  798. if (imm == 0)
  799. break;
  800. /* srlg %dst,%dst,imm(%r0) */
  801. EMIT6_DISP_LH(0xeb000000, 0x000c, dst_reg, dst_reg, REG_0, imm);
  802. break;
  803. /*
  804. * BPF_ARSH
  805. */
  806. case BPF_ALU64 | BPF_ARSH | BPF_X: /* ((s64) dst) >>= src */
  807. /* srag %dst,%dst,0(%src) */
  808. EMIT6_DISP_LH(0xeb000000, 0x000a, dst_reg, dst_reg, src_reg, 0);
  809. break;
  810. case BPF_ALU64 | BPF_ARSH | BPF_K: /* ((s64) dst) >>= imm */
  811. if (imm == 0)
  812. break;
  813. /* srag %dst,%dst,imm(%r0) */
  814. EMIT6_DISP_LH(0xeb000000, 0x000a, dst_reg, dst_reg, REG_0, imm);
  815. break;
  816. /*
  817. * BPF_NEG
  818. */
  819. case BPF_ALU | BPF_NEG: /* dst = (u32) -dst */
  820. /* lcr %dst,%dst */
  821. EMIT2(0x1300, dst_reg, dst_reg);
  822. EMIT_ZERO(dst_reg);
  823. break;
  824. case BPF_ALU64 | BPF_NEG: /* dst = -dst */
  825. /* lcgr %dst,%dst */
  826. EMIT4(0xb9130000, dst_reg, dst_reg);
  827. break;
  828. /*
  829. * BPF_FROM_BE/LE
  830. */
  831. case BPF_ALU | BPF_END | BPF_FROM_BE:
  832. /* s390 is big endian, therefore only clear high order bytes */
  833. switch (imm) {
  834. case 16: /* dst = (u16) cpu_to_be16(dst) */
  835. /* llghr %dst,%dst */
  836. EMIT4(0xb9850000, dst_reg, dst_reg);
  837. break;
  838. case 32: /* dst = (u32) cpu_to_be32(dst) */
  839. /* llgfr %dst,%dst */
  840. EMIT4(0xb9160000, dst_reg, dst_reg);
  841. break;
  842. case 64: /* dst = (u64) cpu_to_be64(dst) */
  843. break;
  844. }
  845. break;
  846. case BPF_ALU | BPF_END | BPF_FROM_LE:
  847. switch (imm) {
  848. case 16: /* dst = (u16) cpu_to_le16(dst) */
  849. /* lrvr %dst,%dst */
  850. EMIT4(0xb91f0000, dst_reg, dst_reg);
  851. /* srl %dst,16(%r0) */
  852. EMIT4_DISP(0x88000000, dst_reg, REG_0, 16);
  853. /* llghr %dst,%dst */
  854. EMIT4(0xb9850000, dst_reg, dst_reg);
  855. break;
  856. case 32: /* dst = (u32) cpu_to_le32(dst) */
  857. /* lrvr %dst,%dst */
  858. EMIT4(0xb91f0000, dst_reg, dst_reg);
  859. /* llgfr %dst,%dst */
  860. EMIT4(0xb9160000, dst_reg, dst_reg);
  861. break;
  862. case 64: /* dst = (u64) cpu_to_le64(dst) */
  863. /* lrvgr %dst,%dst */
  864. EMIT4(0xb90f0000, dst_reg, dst_reg);
  865. break;
  866. }
  867. break;
  868. /*
  869. * BPF_ST(X)
  870. */
  871. case BPF_STX | BPF_MEM | BPF_B: /* *(u8 *)(dst + off) = src_reg */
  872. /* stcy %src,off(%dst) */
  873. EMIT6_DISP_LH(0xe3000000, 0x0072, src_reg, dst_reg, REG_0, off);
  874. jit->seen |= SEEN_MEM;
  875. break;
  876. case BPF_STX | BPF_MEM | BPF_H: /* (u16 *)(dst + off) = src */
  877. /* sthy %src,off(%dst) */
  878. EMIT6_DISP_LH(0xe3000000, 0x0070, src_reg, dst_reg, REG_0, off);
  879. jit->seen |= SEEN_MEM;
  880. break;
  881. case BPF_STX | BPF_MEM | BPF_W: /* *(u32 *)(dst + off) = src */
  882. /* sty %src,off(%dst) */
  883. EMIT6_DISP_LH(0xe3000000, 0x0050, src_reg, dst_reg, REG_0, off);
  884. jit->seen |= SEEN_MEM;
  885. break;
  886. case BPF_STX | BPF_MEM | BPF_DW: /* (u64 *)(dst + off) = src */
  887. /* stg %src,off(%dst) */
  888. EMIT6_DISP_LH(0xe3000000, 0x0024, src_reg, dst_reg, REG_0, off);
  889. jit->seen |= SEEN_MEM;
  890. break;
  891. case BPF_ST | BPF_MEM | BPF_B: /* *(u8 *)(dst + off) = imm */
  892. /* lhi %w0,imm */
  893. EMIT4_IMM(0xa7080000, REG_W0, (u8) imm);
  894. /* stcy %w0,off(dst) */
  895. EMIT6_DISP_LH(0xe3000000, 0x0072, REG_W0, dst_reg, REG_0, off);
  896. jit->seen |= SEEN_MEM;
  897. break;
  898. case BPF_ST | BPF_MEM | BPF_H: /* (u16 *)(dst + off) = imm */
  899. /* lhi %w0,imm */
  900. EMIT4_IMM(0xa7080000, REG_W0, (u16) imm);
  901. /* sthy %w0,off(dst) */
  902. EMIT6_DISP_LH(0xe3000000, 0x0070, REG_W0, dst_reg, REG_0, off);
  903. jit->seen |= SEEN_MEM;
  904. break;
  905. case BPF_ST | BPF_MEM | BPF_W: /* *(u32 *)(dst + off) = imm */
  906. /* llilf %w0,imm */
  907. EMIT6_IMM(0xc00f0000, REG_W0, (u32) imm);
  908. /* sty %w0,off(%dst) */
  909. EMIT6_DISP_LH(0xe3000000, 0x0050, REG_W0, dst_reg, REG_0, off);
  910. jit->seen |= SEEN_MEM;
  911. break;
  912. case BPF_ST | BPF_MEM | BPF_DW: /* *(u64 *)(dst + off) = imm */
  913. /* lgfi %w0,imm */
  914. EMIT6_IMM(0xc0010000, REG_W0, imm);
  915. /* stg %w0,off(%dst) */
  916. EMIT6_DISP_LH(0xe3000000, 0x0024, REG_W0, dst_reg, REG_0, off);
  917. jit->seen |= SEEN_MEM;
  918. break;
  919. /*
  920. * BPF_STX XADD (atomic_add)
  921. */
  922. case BPF_STX | BPF_XADD | BPF_W: /* *(u32 *)(dst + off) += src */
  923. /* laal %w0,%src,off(%dst) */
  924. EMIT6_DISP_LH(0xeb000000, 0x00fa, REG_W0, src_reg,
  925. dst_reg, off);
  926. jit->seen |= SEEN_MEM;
  927. break;
  928. case BPF_STX | BPF_XADD | BPF_DW: /* *(u64 *)(dst + off) += src */
  929. /* laalg %w0,%src,off(%dst) */
  930. EMIT6_DISP_LH(0xeb000000, 0x00ea, REG_W0, src_reg,
  931. dst_reg, off);
  932. jit->seen |= SEEN_MEM;
  933. break;
  934. /*
  935. * BPF_LDX
  936. */
  937. case BPF_LDX | BPF_MEM | BPF_B: /* dst = *(u8 *)(ul) (src + off) */
  938. /* llgc %dst,0(off,%src) */
  939. EMIT6_DISP_LH(0xe3000000, 0x0090, dst_reg, src_reg, REG_0, off);
  940. jit->seen |= SEEN_MEM;
  941. break;
  942. case BPF_LDX | BPF_MEM | BPF_H: /* dst = *(u16 *)(ul) (src + off) */
  943. /* llgh %dst,0(off,%src) */
  944. EMIT6_DISP_LH(0xe3000000, 0x0091, dst_reg, src_reg, REG_0, off);
  945. jit->seen |= SEEN_MEM;
  946. break;
  947. case BPF_LDX | BPF_MEM | BPF_W: /* dst = *(u32 *)(ul) (src + off) */
  948. /* llgf %dst,off(%src) */
  949. jit->seen |= SEEN_MEM;
  950. EMIT6_DISP_LH(0xe3000000, 0x0016, dst_reg, src_reg, REG_0, off);
  951. break;
  952. case BPF_LDX | BPF_MEM | BPF_DW: /* dst = *(u64 *)(ul) (src + off) */
  953. /* lg %dst,0(off,%src) */
  954. jit->seen |= SEEN_MEM;
  955. EMIT6_DISP_LH(0xe3000000, 0x0004, dst_reg, src_reg, REG_0, off);
  956. break;
  957. /*
  958. * BPF_JMP / CALL
  959. */
  960. case BPF_JMP | BPF_CALL:
  961. {
  962. /*
  963. * b0 = (__bpf_call_base + imm)(b1, b2, b3, b4, b5)
  964. */
  965. const u64 func = (u64)__bpf_call_base + imm;
  966. REG_SET_SEEN(BPF_REG_5);
  967. jit->seen |= SEEN_FUNC;
  968. /* lg %w1,<d(imm)>(%l) */
  969. EMIT6_DISP_LH(0xe3000000, 0x0004, REG_W1, REG_0, REG_L,
  970. EMIT_CONST_U64(func));
  971. if (IS_ENABLED(CC_USING_EXPOLINE) && !nospec_disable) {
  972. /* brasl %r14,__s390_indirect_jump_r1 */
  973. EMIT6_PCREL_RILB(0xc0050000, REG_14, jit->r1_thunk_ip);
  974. } else {
  975. /* basr %r14,%w1 */
  976. EMIT2(0x0d00, REG_14, REG_W1);
  977. }
  978. /* lgr %b0,%r2: load return value into %b0 */
  979. EMIT4(0xb9040000, BPF_REG_0, REG_2);
  980. if (bpf_helper_changes_skb_data((void *)func)) {
  981. jit->seen |= SEEN_SKB_CHANGE;
  982. /* lg %b1,ST_OFF_SKBP(%r15) */
  983. EMIT6_DISP_LH(0xe3000000, 0x0004, BPF_REG_1, REG_0,
  984. REG_15, STK_OFF_SKBP);
  985. emit_load_skb_data_hlen(jit);
  986. }
  987. break;
  988. }
  989. case BPF_JMP | BPF_CALL | BPF_X:
  990. /*
  991. * Implicit input:
  992. * B1: pointer to ctx
  993. * B2: pointer to bpf_array
  994. * B3: index in bpf_array
  995. */
  996. jit->seen |= SEEN_TAIL_CALL;
  997. /*
  998. * if (index >= array->map.max_entries)
  999. * goto out;
  1000. */
  1001. /* llgf %w1,map.max_entries(%b2) */
  1002. EMIT6_DISP_LH(0xe3000000, 0x0016, REG_W1, REG_0, BPF_REG_2,
  1003. offsetof(struct bpf_array, map.max_entries));
  1004. /* clgrj %b3,%w1,0xa,label0: if %b3 >= %w1 goto out */
  1005. EMIT6_PCREL_LABEL(0xec000000, 0x0065, BPF_REG_3,
  1006. REG_W1, 0, 0xa);
  1007. /*
  1008. * if (tail_call_cnt++ > MAX_TAIL_CALL_CNT)
  1009. * goto out;
  1010. */
  1011. if (jit->seen & SEEN_STACK)
  1012. off = STK_OFF_TCCNT + STK_OFF;
  1013. else
  1014. off = STK_OFF_TCCNT;
  1015. /* lhi %w0,1 */
  1016. EMIT4_IMM(0xa7080000, REG_W0, 1);
  1017. /* laal %w1,%w0,off(%r15) */
  1018. EMIT6_DISP_LH(0xeb000000, 0x00fa, REG_W1, REG_W0, REG_15, off);
  1019. /* clij %w1,MAX_TAIL_CALL_CNT,0x2,label0 */
  1020. EMIT6_PCREL_IMM_LABEL(0xec000000, 0x007f, REG_W1,
  1021. MAX_TAIL_CALL_CNT, 0, 0x2);
  1022. /*
  1023. * prog = array->ptrs[index];
  1024. * if (prog == NULL)
  1025. * goto out;
  1026. */
  1027. /* sllg %r1,%b3,3: %r1 = index * 8 */
  1028. EMIT6_DISP_LH(0xeb000000, 0x000d, REG_1, BPF_REG_3, REG_0, 3);
  1029. /* lg %r1,prog(%b2,%r1) */
  1030. EMIT6_DISP_LH(0xe3000000, 0x0004, REG_1, BPF_REG_2,
  1031. REG_1, offsetof(struct bpf_array, ptrs));
  1032. /* clgij %r1,0,0x8,label0 */
  1033. EMIT6_PCREL_IMM_LABEL(0xec000000, 0x007d, REG_1, 0, 0, 0x8);
  1034. /*
  1035. * Restore registers before calling function
  1036. */
  1037. save_restore_regs(jit, REGS_RESTORE);
  1038. /*
  1039. * goto *(prog->bpf_func + tail_call_start);
  1040. */
  1041. /* lg %r1,bpf_func(%r1) */
  1042. EMIT6_DISP_LH(0xe3000000, 0x0004, REG_1, REG_1, REG_0,
  1043. offsetof(struct bpf_prog, bpf_func));
  1044. /* bc 0xf,tail_call_start(%r1) */
  1045. _EMIT4(0x47f01000 + jit->tail_call_start);
  1046. /* out: */
  1047. jit->labels[0] = jit->prg;
  1048. break;
  1049. case BPF_JMP | BPF_EXIT: /* return b0 */
  1050. last = (i == fp->len - 1) ? 1 : 0;
  1051. if (last && !(jit->seen & SEEN_RET0))
  1052. break;
  1053. /* j <exit> */
  1054. EMIT4_PCREL(0xa7f40000, jit->exit_ip - jit->prg);
  1055. break;
  1056. /*
  1057. * Branch relative (number of skipped instructions) to offset on
  1058. * condition.
  1059. *
  1060. * Condition code to mask mapping:
  1061. *
  1062. * CC | Description | Mask
  1063. * ------------------------------
  1064. * 0 | Operands equal | 8
  1065. * 1 | First operand low | 4
  1066. * 2 | First operand high | 2
  1067. * 3 | Unused | 1
  1068. *
  1069. * For s390x relative branches: ip = ip + off_bytes
  1070. * For BPF relative branches: insn = insn + off_insns + 1
  1071. *
  1072. * For example for s390x with offset 0 we jump to the branch
  1073. * instruction itself (loop) and for BPF with offset 0 we
  1074. * branch to the instruction behind the branch.
  1075. */
  1076. case BPF_JMP | BPF_JA: /* if (true) */
  1077. mask = 0xf000; /* j */
  1078. goto branch_oc;
  1079. case BPF_JMP | BPF_JSGT | BPF_K: /* ((s64) dst > (s64) imm) */
  1080. mask = 0x2000; /* jh */
  1081. goto branch_ks;
  1082. case BPF_JMP | BPF_JSGE | BPF_K: /* ((s64) dst >= (s64) imm) */
  1083. mask = 0xa000; /* jhe */
  1084. goto branch_ks;
  1085. case BPF_JMP | BPF_JGT | BPF_K: /* (dst_reg > imm) */
  1086. mask = 0x2000; /* jh */
  1087. goto branch_ku;
  1088. case BPF_JMP | BPF_JGE | BPF_K: /* (dst_reg >= imm) */
  1089. mask = 0xa000; /* jhe */
  1090. goto branch_ku;
  1091. case BPF_JMP | BPF_JNE | BPF_K: /* (dst_reg != imm) */
  1092. mask = 0x7000; /* jne */
  1093. goto branch_ku;
  1094. case BPF_JMP | BPF_JEQ | BPF_K: /* (dst_reg == imm) */
  1095. mask = 0x8000; /* je */
  1096. goto branch_ku;
  1097. case BPF_JMP | BPF_JSET | BPF_K: /* (dst_reg & imm) */
  1098. mask = 0x7000; /* jnz */
  1099. /* lgfi %w1,imm (load sign extend imm) */
  1100. EMIT6_IMM(0xc0010000, REG_W1, imm);
  1101. /* ngr %w1,%dst */
  1102. EMIT4(0xb9800000, REG_W1, dst_reg);
  1103. goto branch_oc;
  1104. case BPF_JMP | BPF_JSGT | BPF_X: /* ((s64) dst > (s64) src) */
  1105. mask = 0x2000; /* jh */
  1106. goto branch_xs;
  1107. case BPF_JMP | BPF_JSGE | BPF_X: /* ((s64) dst >= (s64) src) */
  1108. mask = 0xa000; /* jhe */
  1109. goto branch_xs;
  1110. case BPF_JMP | BPF_JGT | BPF_X: /* (dst > src) */
  1111. mask = 0x2000; /* jh */
  1112. goto branch_xu;
  1113. case BPF_JMP | BPF_JGE | BPF_X: /* (dst >= src) */
  1114. mask = 0xa000; /* jhe */
  1115. goto branch_xu;
  1116. case BPF_JMP | BPF_JNE | BPF_X: /* (dst != src) */
  1117. mask = 0x7000; /* jne */
  1118. goto branch_xu;
  1119. case BPF_JMP | BPF_JEQ | BPF_X: /* (dst == src) */
  1120. mask = 0x8000; /* je */
  1121. goto branch_xu;
  1122. case BPF_JMP | BPF_JSET | BPF_X: /* (dst & src) */
  1123. mask = 0x7000; /* jnz */
  1124. /* ngrk %w1,%dst,%src */
  1125. EMIT4_RRF(0xb9e40000, REG_W1, dst_reg, src_reg);
  1126. goto branch_oc;
  1127. branch_ks:
  1128. /* lgfi %w1,imm (load sign extend imm) */
  1129. EMIT6_IMM(0xc0010000, REG_W1, imm);
  1130. /* cgrj %dst,%w1,mask,off */
  1131. EMIT6_PCREL(0xec000000, 0x0064, dst_reg, REG_W1, i, off, mask);
  1132. break;
  1133. branch_ku:
  1134. /* lgfi %w1,imm (load sign extend imm) */
  1135. EMIT6_IMM(0xc0010000, REG_W1, imm);
  1136. /* clgrj %dst,%w1,mask,off */
  1137. EMIT6_PCREL(0xec000000, 0x0065, dst_reg, REG_W1, i, off, mask);
  1138. break;
  1139. branch_xs:
  1140. /* cgrj %dst,%src,mask,off */
  1141. EMIT6_PCREL(0xec000000, 0x0064, dst_reg, src_reg, i, off, mask);
  1142. break;
  1143. branch_xu:
  1144. /* clgrj %dst,%src,mask,off */
  1145. EMIT6_PCREL(0xec000000, 0x0065, dst_reg, src_reg, i, off, mask);
  1146. break;
  1147. branch_oc:
  1148. /* brc mask,jmp_off (branch instruction needs 4 bytes) */
  1149. jmp_off = addrs[i + off + 1] - (addrs[i + 1] - 4);
  1150. EMIT4_PCREL(0xa7040000 | mask << 8, jmp_off);
  1151. break;
  1152. /*
  1153. * BPF_LD
  1154. */
  1155. case BPF_LD | BPF_ABS | BPF_B: /* b0 = *(u8 *) (skb->data+imm) */
  1156. case BPF_LD | BPF_IND | BPF_B: /* b0 = *(u8 *) (skb->data+imm+src) */
  1157. if ((BPF_MODE(insn->code) == BPF_ABS) && (imm >= 0))
  1158. func_addr = __pa(sk_load_byte_pos);
  1159. else
  1160. func_addr = __pa(sk_load_byte);
  1161. goto call_fn;
  1162. case BPF_LD | BPF_ABS | BPF_H: /* b0 = *(u16 *) (skb->data+imm) */
  1163. case BPF_LD | BPF_IND | BPF_H: /* b0 = *(u16 *) (skb->data+imm+src) */
  1164. if ((BPF_MODE(insn->code) == BPF_ABS) && (imm >= 0))
  1165. func_addr = __pa(sk_load_half_pos);
  1166. else
  1167. func_addr = __pa(sk_load_half);
  1168. goto call_fn;
  1169. case BPF_LD | BPF_ABS | BPF_W: /* b0 = *(u32 *) (skb->data+imm) */
  1170. case BPF_LD | BPF_IND | BPF_W: /* b0 = *(u32 *) (skb->data+imm+src) */
  1171. if ((BPF_MODE(insn->code) == BPF_ABS) && (imm >= 0))
  1172. func_addr = __pa(sk_load_word_pos);
  1173. else
  1174. func_addr = __pa(sk_load_word);
  1175. goto call_fn;
  1176. call_fn:
  1177. jit->seen |= SEEN_SKB | SEEN_RET0 | SEEN_FUNC;
  1178. REG_SET_SEEN(REG_14); /* Return address of possible func call */
  1179. /*
  1180. * Implicit input:
  1181. * BPF_REG_6 (R7) : skb pointer
  1182. * REG_SKB_DATA (R12): skb data pointer (if no BPF_REG_AX)
  1183. *
  1184. * Calculated input:
  1185. * BPF_REG_2 (R3) : offset of byte(s) to fetch in skb
  1186. * BPF_REG_5 (R6) : return address
  1187. *
  1188. * Output:
  1189. * BPF_REG_0 (R14): data read from skb
  1190. *
  1191. * Scratch registers (BPF_REG_1-5)
  1192. */
  1193. /* Call function: llilf %w1,func_addr */
  1194. EMIT6_IMM(0xc00f0000, REG_W1, func_addr);
  1195. /* Offset: lgfi %b2,imm */
  1196. EMIT6_IMM(0xc0010000, BPF_REG_2, imm);
  1197. if (BPF_MODE(insn->code) == BPF_IND)
  1198. /* agfr %b2,%src (%src is s32 here) */
  1199. EMIT4(0xb9180000, BPF_REG_2, src_reg);
  1200. /* Reload REG_SKB_DATA if BPF_REG_AX is used */
  1201. if (jit->seen & SEEN_REG_AX)
  1202. /* lg %skb_data,data_off(%b6) */
  1203. EMIT6_DISP_LH(0xe3000000, 0x0004, REG_SKB_DATA, REG_0,
  1204. BPF_REG_6, offsetof(struct sk_buff, data));
  1205. /* basr %b5,%w1 (%b5 is call saved) */
  1206. EMIT2(0x0d00, BPF_REG_5, REG_W1);
  1207. /*
  1208. * Note: For fast access we jump directly after the
  1209. * jnz instruction from bpf_jit.S
  1210. */
  1211. /* jnz <ret0> */
  1212. EMIT4_PCREL(0xa7740000, jit->ret0_ip - jit->prg);
  1213. break;
  1214. default: /* too complex, give up */
  1215. pr_err("Unknown opcode %02x\n", insn->code);
  1216. return -1;
  1217. }
  1218. return insn_count;
  1219. }
  1220. /*
  1221. * Compile eBPF program into s390x code
  1222. */
  1223. static int bpf_jit_prog(struct bpf_jit *jit, struct bpf_prog *fp)
  1224. {
  1225. int i, insn_count;
  1226. jit->lit = jit->lit_start;
  1227. jit->prg = 0;
  1228. bpf_jit_prologue(jit);
  1229. for (i = 0; i < fp->len; i += insn_count) {
  1230. insn_count = bpf_jit_insn(jit, fp, i);
  1231. if (insn_count < 0)
  1232. return -1;
  1233. /* Next instruction address */
  1234. jit->addrs[i + insn_count] = jit->prg;
  1235. }
  1236. bpf_jit_epilogue(jit);
  1237. jit->lit_start = jit->prg;
  1238. jit->size = jit->lit;
  1239. jit->size_prg = jit->prg;
  1240. return 0;
  1241. }
  1242. /*
  1243. * Classic BPF function stub. BPF programs will be converted into
  1244. * eBPF and then bpf_int_jit_compile() will be called.
  1245. */
  1246. void bpf_jit_compile(struct bpf_prog *fp)
  1247. {
  1248. }
  1249. /*
  1250. * Compile eBPF program "fp"
  1251. */
  1252. struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *fp)
  1253. {
  1254. struct bpf_prog *tmp, *orig_fp = fp;
  1255. struct bpf_binary_header *header;
  1256. bool tmp_blinded = false;
  1257. struct bpf_jit jit;
  1258. int pass;
  1259. if (!bpf_jit_enable)
  1260. return orig_fp;
  1261. tmp = bpf_jit_blind_constants(fp);
  1262. /*
  1263. * If blinding was requested and we failed during blinding,
  1264. * we must fall back to the interpreter.
  1265. */
  1266. if (IS_ERR(tmp))
  1267. return orig_fp;
  1268. if (tmp != fp) {
  1269. tmp_blinded = true;
  1270. fp = tmp;
  1271. }
  1272. memset(&jit, 0, sizeof(jit));
  1273. jit.addrs = kcalloc(fp->len + 1, sizeof(*jit.addrs), GFP_KERNEL);
  1274. if (jit.addrs == NULL) {
  1275. fp = orig_fp;
  1276. goto out;
  1277. }
  1278. /*
  1279. * Three initial passes:
  1280. * - 1/2: Determine clobbered registers
  1281. * - 3: Calculate program size and addrs arrray
  1282. */
  1283. for (pass = 1; pass <= 3; pass++) {
  1284. if (bpf_jit_prog(&jit, fp)) {
  1285. fp = orig_fp;
  1286. goto free_addrs;
  1287. }
  1288. }
  1289. /*
  1290. * Final pass: Allocate and generate program
  1291. */
  1292. if (jit.size >= BPF_SIZE_MAX) {
  1293. fp = orig_fp;
  1294. goto free_addrs;
  1295. }
  1296. header = bpf_jit_binary_alloc(jit.size, &jit.prg_buf, 2, jit_fill_hole);
  1297. if (!header) {
  1298. fp = orig_fp;
  1299. goto free_addrs;
  1300. }
  1301. if (bpf_jit_prog(&jit, fp)) {
  1302. fp = orig_fp;
  1303. goto free_addrs;
  1304. }
  1305. if (bpf_jit_enable > 1) {
  1306. bpf_jit_dump(fp->len, jit.size, pass, jit.prg_buf);
  1307. if (jit.prg_buf)
  1308. print_fn_code(jit.prg_buf, jit.size_prg);
  1309. }
  1310. if (jit.prg_buf) {
  1311. set_memory_ro((unsigned long)header, header->pages);
  1312. fp->bpf_func = (void *) jit.prg_buf;
  1313. fp->jited = 1;
  1314. }
  1315. free_addrs:
  1316. kfree(jit.addrs);
  1317. out:
  1318. if (tmp_blinded)
  1319. bpf_jit_prog_release_other(fp, fp == orig_fp ?
  1320. tmp : orig_fp);
  1321. return fp;
  1322. }
  1323. /*
  1324. * Free eBPF program
  1325. */
  1326. void bpf_jit_free(struct bpf_prog *fp)
  1327. {
  1328. unsigned long addr = (unsigned long)fp->bpf_func & PAGE_MASK;
  1329. struct bpf_binary_header *header = (void *)addr;
  1330. if (!fp->jited)
  1331. goto free_filter;
  1332. set_memory_rw(addr, header->pages);
  1333. bpf_jit_binary_free(header);
  1334. free_filter:
  1335. bpf_prog_unlock_free(fp);
  1336. }