bpf_jit_comp.c 37 KB

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