bpf_jit_comp.c 35 KB

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