bpf_jit_comp64.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  1. /*
  2. * bpf_jit_comp64.c: eBPF JIT compiler
  3. *
  4. * Copyright 2016 Naveen N. Rao <naveen.n.rao@linux.vnet.ibm.com>
  5. * IBM Corporation
  6. *
  7. * Based on the powerpc classic BPF JIT compiler by Matt Evans
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; version 2
  12. * of the License.
  13. */
  14. #include <linux/moduleloader.h>
  15. #include <asm/cacheflush.h>
  16. #include <linux/netdevice.h>
  17. #include <linux/filter.h>
  18. #include <linux/if_vlan.h>
  19. #include <asm/kprobes.h>
  20. #include <linux/bpf.h>
  21. #include "bpf_jit64.h"
  22. static void bpf_jit_fill_ill_insns(void *area, unsigned int size)
  23. {
  24. memset32(area, BREAKPOINT_INSTRUCTION, size/4);
  25. }
  26. static inline void bpf_flush_icache(void *start, void *end)
  27. {
  28. smp_wmb();
  29. flush_icache_range((unsigned long)start, (unsigned long)end);
  30. }
  31. static inline bool bpf_is_seen_register(struct codegen_context *ctx, int i)
  32. {
  33. return (ctx->seen & (1 << (31 - b2p[i])));
  34. }
  35. static inline void bpf_set_seen_register(struct codegen_context *ctx, int i)
  36. {
  37. ctx->seen |= (1 << (31 - b2p[i]));
  38. }
  39. static inline bool bpf_has_stack_frame(struct codegen_context *ctx)
  40. {
  41. /*
  42. * We only need a stack frame if:
  43. * - we call other functions (kernel helpers), or
  44. * - the bpf program uses its stack area
  45. * The latter condition is deduced from the usage of BPF_REG_FP
  46. */
  47. return ctx->seen & SEEN_FUNC || bpf_is_seen_register(ctx, BPF_REG_FP);
  48. }
  49. /*
  50. * When not setting up our own stackframe, the redzone usage is:
  51. *
  52. * [ prev sp ] <-------------
  53. * [ ... ] |
  54. * sp (r1) ---> [ stack pointer ] --------------
  55. * [ nv gpr save area ] 6*8
  56. * [ tail_call_cnt ] 8
  57. * [ local_tmp_var ] 8
  58. * [ unused red zone ] 208 bytes protected
  59. */
  60. static int bpf_jit_stack_local(struct codegen_context *ctx)
  61. {
  62. if (bpf_has_stack_frame(ctx))
  63. return STACK_FRAME_MIN_SIZE + ctx->stack_size;
  64. else
  65. return -(BPF_PPC_STACK_SAVE + 16);
  66. }
  67. static int bpf_jit_stack_tailcallcnt(struct codegen_context *ctx)
  68. {
  69. return bpf_jit_stack_local(ctx) + 8;
  70. }
  71. static int bpf_jit_stack_offsetof(struct codegen_context *ctx, int reg)
  72. {
  73. if (reg >= BPF_PPC_NVR_MIN && reg < 32)
  74. return (bpf_has_stack_frame(ctx) ?
  75. (BPF_PPC_STACKFRAME + ctx->stack_size) : 0)
  76. - (8 * (32 - reg));
  77. pr_err("BPF JIT is asking about unknown registers");
  78. BUG();
  79. }
  80. static void bpf_jit_build_prologue(u32 *image, struct codegen_context *ctx)
  81. {
  82. int i;
  83. /*
  84. * Initialize tail_call_cnt if we do tail calls.
  85. * Otherwise, put in NOPs so that it can be skipped when we are
  86. * invoked through a tail call.
  87. */
  88. if (ctx->seen & SEEN_TAILCALL) {
  89. PPC_LI(b2p[TMP_REG_1], 0);
  90. /* this goes in the redzone */
  91. PPC_BPF_STL(b2p[TMP_REG_1], 1, -(BPF_PPC_STACK_SAVE + 8));
  92. } else {
  93. PPC_NOP();
  94. PPC_NOP();
  95. }
  96. #define BPF_TAILCALL_PROLOGUE_SIZE 8
  97. if (bpf_has_stack_frame(ctx)) {
  98. /*
  99. * We need a stack frame, but we don't necessarily need to
  100. * save/restore LR unless we call other functions
  101. */
  102. if (ctx->seen & SEEN_FUNC) {
  103. EMIT(PPC_INST_MFLR | __PPC_RT(R0));
  104. PPC_BPF_STL(0, 1, PPC_LR_STKOFF);
  105. }
  106. PPC_BPF_STLU(1, 1, -(BPF_PPC_STACKFRAME + ctx->stack_size));
  107. }
  108. /*
  109. * Back up non-volatile regs -- BPF registers 6-10
  110. * If we haven't created our own stack frame, we save these
  111. * in the protected zone below the previous stack frame
  112. */
  113. for (i = BPF_REG_6; i <= BPF_REG_10; i++)
  114. if (bpf_is_seen_register(ctx, i))
  115. PPC_BPF_STL(b2p[i], 1, bpf_jit_stack_offsetof(ctx, b2p[i]));
  116. /* Setup frame pointer to point to the bpf stack area */
  117. if (bpf_is_seen_register(ctx, BPF_REG_FP))
  118. PPC_ADDI(b2p[BPF_REG_FP], 1,
  119. STACK_FRAME_MIN_SIZE + ctx->stack_size);
  120. }
  121. static void bpf_jit_emit_common_epilogue(u32 *image, struct codegen_context *ctx)
  122. {
  123. int i;
  124. /* Restore NVRs */
  125. for (i = BPF_REG_6; i <= BPF_REG_10; i++)
  126. if (bpf_is_seen_register(ctx, i))
  127. PPC_BPF_LL(b2p[i], 1, bpf_jit_stack_offsetof(ctx, b2p[i]));
  128. /* Tear down our stack frame */
  129. if (bpf_has_stack_frame(ctx)) {
  130. PPC_ADDI(1, 1, BPF_PPC_STACKFRAME + ctx->stack_size);
  131. if (ctx->seen & SEEN_FUNC) {
  132. PPC_BPF_LL(0, 1, PPC_LR_STKOFF);
  133. PPC_MTLR(0);
  134. }
  135. }
  136. }
  137. static void bpf_jit_build_epilogue(u32 *image, struct codegen_context *ctx)
  138. {
  139. bpf_jit_emit_common_epilogue(image, ctx);
  140. /* Move result to r3 */
  141. PPC_MR(3, b2p[BPF_REG_0]);
  142. PPC_BLR();
  143. }
  144. static void bpf_jit_emit_func_call(u32 *image, struct codegen_context *ctx, u64 func)
  145. {
  146. unsigned int i, ctx_idx = ctx->idx;
  147. /* Load function address into r12 */
  148. PPC_LI64(12, func);
  149. /* For bpf-to-bpf function calls, the callee's address is unknown
  150. * until the last extra pass. As seen above, we use PPC_LI64() to
  151. * load the callee's address, but this may optimize the number of
  152. * instructions required based on the nature of the address.
  153. *
  154. * Since we don't want the number of instructions emitted to change,
  155. * we pad the optimized PPC_LI64() call with NOPs to guarantee that
  156. * we always have a five-instruction sequence, which is the maximum
  157. * that PPC_LI64() can emit.
  158. */
  159. for (i = ctx->idx - ctx_idx; i < 5; i++)
  160. PPC_NOP();
  161. #ifdef PPC64_ELF_ABI_v1
  162. /*
  163. * Load TOC from function descriptor at offset 8.
  164. * We can clobber r2 since we get called through a
  165. * function pointer (so caller will save/restore r2)
  166. * and since we don't use a TOC ourself.
  167. */
  168. PPC_BPF_LL(2, 12, 8);
  169. /* Load actual entry point from function descriptor */
  170. PPC_BPF_LL(12, 12, 0);
  171. #endif
  172. PPC_MTLR(12);
  173. PPC_BLRL();
  174. }
  175. static void bpf_jit_emit_tail_call(u32 *image, struct codegen_context *ctx, u32 out)
  176. {
  177. /*
  178. * By now, the eBPF program has already setup parameters in r3, r4 and r5
  179. * r3/BPF_REG_1 - pointer to ctx -- passed as is to the next bpf program
  180. * r4/BPF_REG_2 - pointer to bpf_array
  181. * r5/BPF_REG_3 - index in bpf_array
  182. */
  183. int b2p_bpf_array = b2p[BPF_REG_2];
  184. int b2p_index = b2p[BPF_REG_3];
  185. /*
  186. * if (index >= array->map.max_entries)
  187. * goto out;
  188. */
  189. PPC_LWZ(b2p[TMP_REG_1], b2p_bpf_array, offsetof(struct bpf_array, map.max_entries));
  190. PPC_RLWINM(b2p_index, b2p_index, 0, 0, 31);
  191. PPC_CMPLW(b2p_index, b2p[TMP_REG_1]);
  192. PPC_BCC(COND_GE, out);
  193. /*
  194. * if (tail_call_cnt > MAX_TAIL_CALL_CNT)
  195. * goto out;
  196. */
  197. PPC_LD(b2p[TMP_REG_1], 1, bpf_jit_stack_tailcallcnt(ctx));
  198. PPC_CMPLWI(b2p[TMP_REG_1], MAX_TAIL_CALL_CNT);
  199. PPC_BCC(COND_GT, out);
  200. /*
  201. * tail_call_cnt++;
  202. */
  203. PPC_ADDI(b2p[TMP_REG_1], b2p[TMP_REG_1], 1);
  204. PPC_BPF_STL(b2p[TMP_REG_1], 1, bpf_jit_stack_tailcallcnt(ctx));
  205. /* prog = array->ptrs[index]; */
  206. PPC_MULI(b2p[TMP_REG_1], b2p_index, 8);
  207. PPC_ADD(b2p[TMP_REG_1], b2p[TMP_REG_1], b2p_bpf_array);
  208. PPC_LD(b2p[TMP_REG_1], b2p[TMP_REG_1], offsetof(struct bpf_array, ptrs));
  209. /*
  210. * if (prog == NULL)
  211. * goto out;
  212. */
  213. PPC_CMPLDI(b2p[TMP_REG_1], 0);
  214. PPC_BCC(COND_EQ, out);
  215. /* goto *(prog->bpf_func + prologue_size); */
  216. PPC_LD(b2p[TMP_REG_1], b2p[TMP_REG_1], offsetof(struct bpf_prog, bpf_func));
  217. #ifdef PPC64_ELF_ABI_v1
  218. /* skip past the function descriptor */
  219. PPC_ADDI(b2p[TMP_REG_1], b2p[TMP_REG_1],
  220. FUNCTION_DESCR_SIZE + BPF_TAILCALL_PROLOGUE_SIZE);
  221. #else
  222. PPC_ADDI(b2p[TMP_REG_1], b2p[TMP_REG_1], BPF_TAILCALL_PROLOGUE_SIZE);
  223. #endif
  224. PPC_MTCTR(b2p[TMP_REG_1]);
  225. /* tear down stack, restore NVRs, ... */
  226. bpf_jit_emit_common_epilogue(image, ctx);
  227. PPC_BCTR();
  228. /* out: */
  229. }
  230. /* Assemble the body code between the prologue & epilogue */
  231. static int bpf_jit_build_body(struct bpf_prog *fp, u32 *image,
  232. struct codegen_context *ctx,
  233. u32 *addrs, bool extra_pass)
  234. {
  235. const struct bpf_insn *insn = fp->insnsi;
  236. int flen = fp->len;
  237. int i;
  238. /* Start of epilogue code - will only be valid 2nd pass onwards */
  239. u32 exit_addr = addrs[flen];
  240. for (i = 0; i < flen; i++) {
  241. u32 code = insn[i].code;
  242. u32 dst_reg = b2p[insn[i].dst_reg];
  243. u32 src_reg = b2p[insn[i].src_reg];
  244. s16 off = insn[i].off;
  245. s32 imm = insn[i].imm;
  246. u64 imm64;
  247. u8 *func;
  248. u32 true_cond;
  249. u32 tmp_idx;
  250. /*
  251. * addrs[] maps a BPF bytecode address into a real offset from
  252. * the start of the body code.
  253. */
  254. addrs[i] = ctx->idx * 4;
  255. /*
  256. * As an optimization, we note down which non-volatile registers
  257. * are used so that we can only save/restore those in our
  258. * prologue and epilogue. We do this here regardless of whether
  259. * the actual BPF instruction uses src/dst registers or not
  260. * (for instance, BPF_CALL does not use them). The expectation
  261. * is that those instructions will have src_reg/dst_reg set to
  262. * 0. Even otherwise, we just lose some prologue/epilogue
  263. * optimization but everything else should work without
  264. * any issues.
  265. */
  266. if (dst_reg >= BPF_PPC_NVR_MIN && dst_reg < 32)
  267. bpf_set_seen_register(ctx, insn[i].dst_reg);
  268. if (src_reg >= BPF_PPC_NVR_MIN && src_reg < 32)
  269. bpf_set_seen_register(ctx, insn[i].src_reg);
  270. switch (code) {
  271. /*
  272. * Arithmetic operations: ADD/SUB/MUL/DIV/MOD/NEG
  273. */
  274. case BPF_ALU | BPF_ADD | BPF_X: /* (u32) dst += (u32) src */
  275. case BPF_ALU64 | BPF_ADD | BPF_X: /* dst += src */
  276. PPC_ADD(dst_reg, dst_reg, src_reg);
  277. goto bpf_alu32_trunc;
  278. case BPF_ALU | BPF_SUB | BPF_X: /* (u32) dst -= (u32) src */
  279. case BPF_ALU64 | BPF_SUB | BPF_X: /* dst -= src */
  280. PPC_SUB(dst_reg, dst_reg, src_reg);
  281. goto bpf_alu32_trunc;
  282. case BPF_ALU | BPF_ADD | BPF_K: /* (u32) dst += (u32) imm */
  283. case BPF_ALU | BPF_SUB | BPF_K: /* (u32) dst -= (u32) imm */
  284. case BPF_ALU64 | BPF_ADD | BPF_K: /* dst += imm */
  285. case BPF_ALU64 | BPF_SUB | BPF_K: /* dst -= imm */
  286. if (BPF_OP(code) == BPF_SUB)
  287. imm = -imm;
  288. if (imm) {
  289. if (imm >= -32768 && imm < 32768)
  290. PPC_ADDI(dst_reg, dst_reg, IMM_L(imm));
  291. else {
  292. PPC_LI32(b2p[TMP_REG_1], imm);
  293. PPC_ADD(dst_reg, dst_reg, b2p[TMP_REG_1]);
  294. }
  295. }
  296. goto bpf_alu32_trunc;
  297. case BPF_ALU | BPF_MUL | BPF_X: /* (u32) dst *= (u32) src */
  298. case BPF_ALU64 | BPF_MUL | BPF_X: /* dst *= src */
  299. if (BPF_CLASS(code) == BPF_ALU)
  300. PPC_MULW(dst_reg, dst_reg, src_reg);
  301. else
  302. PPC_MULD(dst_reg, dst_reg, src_reg);
  303. goto bpf_alu32_trunc;
  304. case BPF_ALU | BPF_MUL | BPF_K: /* (u32) dst *= (u32) imm */
  305. case BPF_ALU64 | BPF_MUL | BPF_K: /* dst *= imm */
  306. if (imm >= -32768 && imm < 32768)
  307. PPC_MULI(dst_reg, dst_reg, IMM_L(imm));
  308. else {
  309. PPC_LI32(b2p[TMP_REG_1], imm);
  310. if (BPF_CLASS(code) == BPF_ALU)
  311. PPC_MULW(dst_reg, dst_reg,
  312. b2p[TMP_REG_1]);
  313. else
  314. PPC_MULD(dst_reg, dst_reg,
  315. b2p[TMP_REG_1]);
  316. }
  317. goto bpf_alu32_trunc;
  318. case BPF_ALU | BPF_DIV | BPF_X: /* (u32) dst /= (u32) src */
  319. case BPF_ALU | BPF_MOD | BPF_X: /* (u32) dst %= (u32) src */
  320. if (BPF_OP(code) == BPF_MOD) {
  321. PPC_DIVWU(b2p[TMP_REG_1], dst_reg, src_reg);
  322. PPC_MULW(b2p[TMP_REG_1], src_reg,
  323. b2p[TMP_REG_1]);
  324. PPC_SUB(dst_reg, dst_reg, b2p[TMP_REG_1]);
  325. } else
  326. PPC_DIVWU(dst_reg, dst_reg, src_reg);
  327. goto bpf_alu32_trunc;
  328. case BPF_ALU64 | BPF_DIV | BPF_X: /* dst /= src */
  329. case BPF_ALU64 | BPF_MOD | BPF_X: /* dst %= src */
  330. if (BPF_OP(code) == BPF_MOD) {
  331. PPC_DIVD(b2p[TMP_REG_1], dst_reg, src_reg);
  332. PPC_MULD(b2p[TMP_REG_1], src_reg,
  333. b2p[TMP_REG_1]);
  334. PPC_SUB(dst_reg, dst_reg, b2p[TMP_REG_1]);
  335. } else
  336. PPC_DIVD(dst_reg, dst_reg, src_reg);
  337. break;
  338. case BPF_ALU | BPF_MOD | BPF_K: /* (u32) dst %= (u32) imm */
  339. case BPF_ALU | BPF_DIV | BPF_K: /* (u32) dst /= (u32) imm */
  340. case BPF_ALU64 | BPF_MOD | BPF_K: /* dst %= imm */
  341. case BPF_ALU64 | BPF_DIV | BPF_K: /* dst /= imm */
  342. if (imm == 0)
  343. return -EINVAL;
  344. else if (imm == 1)
  345. goto bpf_alu32_trunc;
  346. PPC_LI32(b2p[TMP_REG_1], imm);
  347. switch (BPF_CLASS(code)) {
  348. case BPF_ALU:
  349. if (BPF_OP(code) == BPF_MOD) {
  350. PPC_DIVWU(b2p[TMP_REG_2], dst_reg,
  351. b2p[TMP_REG_1]);
  352. PPC_MULW(b2p[TMP_REG_1],
  353. b2p[TMP_REG_1],
  354. b2p[TMP_REG_2]);
  355. PPC_SUB(dst_reg, dst_reg,
  356. b2p[TMP_REG_1]);
  357. } else
  358. PPC_DIVWU(dst_reg, dst_reg,
  359. b2p[TMP_REG_1]);
  360. break;
  361. case BPF_ALU64:
  362. if (BPF_OP(code) == BPF_MOD) {
  363. PPC_DIVD(b2p[TMP_REG_2], dst_reg,
  364. b2p[TMP_REG_1]);
  365. PPC_MULD(b2p[TMP_REG_1],
  366. b2p[TMP_REG_1],
  367. b2p[TMP_REG_2]);
  368. PPC_SUB(dst_reg, dst_reg,
  369. b2p[TMP_REG_1]);
  370. } else
  371. PPC_DIVD(dst_reg, dst_reg,
  372. b2p[TMP_REG_1]);
  373. break;
  374. }
  375. goto bpf_alu32_trunc;
  376. case BPF_ALU | BPF_NEG: /* (u32) dst = -dst */
  377. case BPF_ALU64 | BPF_NEG: /* dst = -dst */
  378. PPC_NEG(dst_reg, dst_reg);
  379. goto bpf_alu32_trunc;
  380. /*
  381. * Logical operations: AND/OR/XOR/[A]LSH/[A]RSH
  382. */
  383. case BPF_ALU | BPF_AND | BPF_X: /* (u32) dst = dst & src */
  384. case BPF_ALU64 | BPF_AND | BPF_X: /* dst = dst & src */
  385. PPC_AND(dst_reg, dst_reg, src_reg);
  386. goto bpf_alu32_trunc;
  387. case BPF_ALU | BPF_AND | BPF_K: /* (u32) dst = dst & imm */
  388. case BPF_ALU64 | BPF_AND | BPF_K: /* dst = dst & imm */
  389. if (!IMM_H(imm))
  390. PPC_ANDI(dst_reg, dst_reg, IMM_L(imm));
  391. else {
  392. /* Sign-extended */
  393. PPC_LI32(b2p[TMP_REG_1], imm);
  394. PPC_AND(dst_reg, dst_reg, b2p[TMP_REG_1]);
  395. }
  396. goto bpf_alu32_trunc;
  397. case BPF_ALU | BPF_OR | BPF_X: /* dst = (u32) dst | (u32) src */
  398. case BPF_ALU64 | BPF_OR | BPF_X: /* dst = dst | src */
  399. PPC_OR(dst_reg, dst_reg, src_reg);
  400. goto bpf_alu32_trunc;
  401. case BPF_ALU | BPF_OR | BPF_K:/* dst = (u32) dst | (u32) imm */
  402. case BPF_ALU64 | BPF_OR | BPF_K:/* dst = dst | imm */
  403. if (imm < 0 && BPF_CLASS(code) == BPF_ALU64) {
  404. /* Sign-extended */
  405. PPC_LI32(b2p[TMP_REG_1], imm);
  406. PPC_OR(dst_reg, dst_reg, b2p[TMP_REG_1]);
  407. } else {
  408. if (IMM_L(imm))
  409. PPC_ORI(dst_reg, dst_reg, IMM_L(imm));
  410. if (IMM_H(imm))
  411. PPC_ORIS(dst_reg, dst_reg, IMM_H(imm));
  412. }
  413. goto bpf_alu32_trunc;
  414. case BPF_ALU | BPF_XOR | BPF_X: /* (u32) dst ^= src */
  415. case BPF_ALU64 | BPF_XOR | BPF_X: /* dst ^= src */
  416. PPC_XOR(dst_reg, dst_reg, src_reg);
  417. goto bpf_alu32_trunc;
  418. case BPF_ALU | BPF_XOR | BPF_K: /* (u32) dst ^= (u32) imm */
  419. case BPF_ALU64 | BPF_XOR | BPF_K: /* dst ^= imm */
  420. if (imm < 0 && BPF_CLASS(code) == BPF_ALU64) {
  421. /* Sign-extended */
  422. PPC_LI32(b2p[TMP_REG_1], imm);
  423. PPC_XOR(dst_reg, dst_reg, b2p[TMP_REG_1]);
  424. } else {
  425. if (IMM_L(imm))
  426. PPC_XORI(dst_reg, dst_reg, IMM_L(imm));
  427. if (IMM_H(imm))
  428. PPC_XORIS(dst_reg, dst_reg, IMM_H(imm));
  429. }
  430. goto bpf_alu32_trunc;
  431. case BPF_ALU | BPF_LSH | BPF_X: /* (u32) dst <<= (u32) src */
  432. /* slw clears top 32 bits */
  433. PPC_SLW(dst_reg, dst_reg, src_reg);
  434. break;
  435. case BPF_ALU64 | BPF_LSH | BPF_X: /* dst <<= src; */
  436. PPC_SLD(dst_reg, dst_reg, src_reg);
  437. break;
  438. case BPF_ALU | BPF_LSH | BPF_K: /* (u32) dst <<== (u32) imm */
  439. /* with imm 0, we still need to clear top 32 bits */
  440. PPC_SLWI(dst_reg, dst_reg, imm);
  441. break;
  442. case BPF_ALU64 | BPF_LSH | BPF_K: /* dst <<== imm */
  443. if (imm != 0)
  444. PPC_SLDI(dst_reg, dst_reg, imm);
  445. break;
  446. case BPF_ALU | BPF_RSH | BPF_X: /* (u32) dst >>= (u32) src */
  447. PPC_SRW(dst_reg, dst_reg, src_reg);
  448. break;
  449. case BPF_ALU64 | BPF_RSH | BPF_X: /* dst >>= src */
  450. PPC_SRD(dst_reg, dst_reg, src_reg);
  451. break;
  452. case BPF_ALU | BPF_RSH | BPF_K: /* (u32) dst >>= (u32) imm */
  453. PPC_SRWI(dst_reg, dst_reg, imm);
  454. break;
  455. case BPF_ALU64 | BPF_RSH | BPF_K: /* dst >>= imm */
  456. if (imm != 0)
  457. PPC_SRDI(dst_reg, dst_reg, imm);
  458. break;
  459. case BPF_ALU64 | BPF_ARSH | BPF_X: /* (s64) dst >>= src */
  460. PPC_SRAD(dst_reg, dst_reg, src_reg);
  461. break;
  462. case BPF_ALU64 | BPF_ARSH | BPF_K: /* (s64) dst >>= imm */
  463. if (imm != 0)
  464. PPC_SRADI(dst_reg, dst_reg, imm);
  465. break;
  466. /*
  467. * MOV
  468. */
  469. case BPF_ALU | BPF_MOV | BPF_X: /* (u32) dst = src */
  470. case BPF_ALU64 | BPF_MOV | BPF_X: /* dst = src */
  471. PPC_MR(dst_reg, src_reg);
  472. goto bpf_alu32_trunc;
  473. case BPF_ALU | BPF_MOV | BPF_K: /* (u32) dst = imm */
  474. case BPF_ALU64 | BPF_MOV | BPF_K: /* dst = (s64) imm */
  475. PPC_LI32(dst_reg, imm);
  476. if (imm < 0)
  477. goto bpf_alu32_trunc;
  478. break;
  479. bpf_alu32_trunc:
  480. /* Truncate to 32-bits */
  481. if (BPF_CLASS(code) == BPF_ALU)
  482. PPC_RLWINM(dst_reg, dst_reg, 0, 0, 31);
  483. break;
  484. /*
  485. * BPF_FROM_BE/LE
  486. */
  487. case BPF_ALU | BPF_END | BPF_FROM_LE:
  488. case BPF_ALU | BPF_END | BPF_FROM_BE:
  489. #ifdef __BIG_ENDIAN__
  490. if (BPF_SRC(code) == BPF_FROM_BE)
  491. goto emit_clear;
  492. #else /* !__BIG_ENDIAN__ */
  493. if (BPF_SRC(code) == BPF_FROM_LE)
  494. goto emit_clear;
  495. #endif
  496. switch (imm) {
  497. case 16:
  498. /* Rotate 8 bits left & mask with 0x0000ff00 */
  499. PPC_RLWINM(b2p[TMP_REG_1], dst_reg, 8, 16, 23);
  500. /* Rotate 8 bits right & insert LSB to reg */
  501. PPC_RLWIMI(b2p[TMP_REG_1], dst_reg, 24, 24, 31);
  502. /* Move result back to dst_reg */
  503. PPC_MR(dst_reg, b2p[TMP_REG_1]);
  504. break;
  505. case 32:
  506. /*
  507. * Rotate word left by 8 bits:
  508. * 2 bytes are already in their final position
  509. * -- byte 2 and 4 (of bytes 1, 2, 3 and 4)
  510. */
  511. PPC_RLWINM(b2p[TMP_REG_1], dst_reg, 8, 0, 31);
  512. /* Rotate 24 bits and insert byte 1 */
  513. PPC_RLWIMI(b2p[TMP_REG_1], dst_reg, 24, 0, 7);
  514. /* Rotate 24 bits and insert byte 3 */
  515. PPC_RLWIMI(b2p[TMP_REG_1], dst_reg, 24, 16, 23);
  516. PPC_MR(dst_reg, b2p[TMP_REG_1]);
  517. break;
  518. case 64:
  519. /*
  520. * Way easier and faster(?) to store the value
  521. * into stack and then use ldbrx
  522. *
  523. * ctx->seen will be reliable in pass2, but
  524. * the instructions generated will remain the
  525. * same across all passes
  526. */
  527. PPC_STD(dst_reg, 1, bpf_jit_stack_local(ctx));
  528. PPC_ADDI(b2p[TMP_REG_1], 1, bpf_jit_stack_local(ctx));
  529. PPC_LDBRX(dst_reg, 0, b2p[TMP_REG_1]);
  530. break;
  531. }
  532. break;
  533. emit_clear:
  534. switch (imm) {
  535. case 16:
  536. /* zero-extend 16 bits into 64 bits */
  537. PPC_RLDICL(dst_reg, dst_reg, 0, 48);
  538. break;
  539. case 32:
  540. /* zero-extend 32 bits into 64 bits */
  541. PPC_RLDICL(dst_reg, dst_reg, 0, 32);
  542. break;
  543. case 64:
  544. /* nop */
  545. break;
  546. }
  547. break;
  548. /*
  549. * BPF_ST(X)
  550. */
  551. case BPF_STX | BPF_MEM | BPF_B: /* *(u8 *)(dst + off) = src */
  552. case BPF_ST | BPF_MEM | BPF_B: /* *(u8 *)(dst + off) = imm */
  553. if (BPF_CLASS(code) == BPF_ST) {
  554. PPC_LI(b2p[TMP_REG_1], imm);
  555. src_reg = b2p[TMP_REG_1];
  556. }
  557. PPC_STB(src_reg, dst_reg, off);
  558. break;
  559. case BPF_STX | BPF_MEM | BPF_H: /* (u16 *)(dst + off) = src */
  560. case BPF_ST | BPF_MEM | BPF_H: /* (u16 *)(dst + off) = imm */
  561. if (BPF_CLASS(code) == BPF_ST) {
  562. PPC_LI(b2p[TMP_REG_1], imm);
  563. src_reg = b2p[TMP_REG_1];
  564. }
  565. PPC_STH(src_reg, dst_reg, off);
  566. break;
  567. case BPF_STX | BPF_MEM | BPF_W: /* *(u32 *)(dst + off) = src */
  568. case BPF_ST | BPF_MEM | BPF_W: /* *(u32 *)(dst + off) = imm */
  569. if (BPF_CLASS(code) == BPF_ST) {
  570. PPC_LI32(b2p[TMP_REG_1], imm);
  571. src_reg = b2p[TMP_REG_1];
  572. }
  573. PPC_STW(src_reg, dst_reg, off);
  574. break;
  575. case BPF_STX | BPF_MEM | BPF_DW: /* (u64 *)(dst + off) = src */
  576. case BPF_ST | BPF_MEM | BPF_DW: /* *(u64 *)(dst + off) = imm */
  577. if (BPF_CLASS(code) == BPF_ST) {
  578. PPC_LI32(b2p[TMP_REG_1], imm);
  579. src_reg = b2p[TMP_REG_1];
  580. }
  581. PPC_STD(src_reg, dst_reg, off);
  582. break;
  583. /*
  584. * BPF_STX XADD (atomic_add)
  585. */
  586. /* *(u32 *)(dst + off) += src */
  587. case BPF_STX | BPF_XADD | BPF_W:
  588. /* Get EA into TMP_REG_1 */
  589. PPC_ADDI(b2p[TMP_REG_1], dst_reg, off);
  590. tmp_idx = ctx->idx * 4;
  591. /* load value from memory into TMP_REG_2 */
  592. PPC_BPF_LWARX(b2p[TMP_REG_2], 0, b2p[TMP_REG_1], 0);
  593. /* add value from src_reg into this */
  594. PPC_ADD(b2p[TMP_REG_2], b2p[TMP_REG_2], src_reg);
  595. /* store result back */
  596. PPC_BPF_STWCX(b2p[TMP_REG_2], 0, b2p[TMP_REG_1]);
  597. /* we're done if this succeeded */
  598. PPC_BCC_SHORT(COND_NE, tmp_idx);
  599. break;
  600. /* *(u64 *)(dst + off) += src */
  601. case BPF_STX | BPF_XADD | BPF_DW:
  602. PPC_ADDI(b2p[TMP_REG_1], dst_reg, off);
  603. tmp_idx = ctx->idx * 4;
  604. PPC_BPF_LDARX(b2p[TMP_REG_2], 0, b2p[TMP_REG_1], 0);
  605. PPC_ADD(b2p[TMP_REG_2], b2p[TMP_REG_2], src_reg);
  606. PPC_BPF_STDCX(b2p[TMP_REG_2], 0, b2p[TMP_REG_1]);
  607. PPC_BCC_SHORT(COND_NE, tmp_idx);
  608. break;
  609. /*
  610. * BPF_LDX
  611. */
  612. /* dst = *(u8 *)(ul) (src + off) */
  613. case BPF_LDX | BPF_MEM | BPF_B:
  614. PPC_LBZ(dst_reg, src_reg, off);
  615. break;
  616. /* dst = *(u16 *)(ul) (src + off) */
  617. case BPF_LDX | BPF_MEM | BPF_H:
  618. PPC_LHZ(dst_reg, src_reg, off);
  619. break;
  620. /* dst = *(u32 *)(ul) (src + off) */
  621. case BPF_LDX | BPF_MEM | BPF_W:
  622. PPC_LWZ(dst_reg, src_reg, off);
  623. break;
  624. /* dst = *(u64 *)(ul) (src + off) */
  625. case BPF_LDX | BPF_MEM | BPF_DW:
  626. PPC_LD(dst_reg, src_reg, off);
  627. break;
  628. /*
  629. * Doubleword load
  630. * 16 byte instruction that uses two 'struct bpf_insn'
  631. */
  632. case BPF_LD | BPF_IMM | BPF_DW: /* dst = (u64) imm */
  633. imm64 = ((u64)(u32) insn[i].imm) |
  634. (((u64)(u32) insn[i+1].imm) << 32);
  635. /* Adjust for two bpf instructions */
  636. addrs[++i] = ctx->idx * 4;
  637. PPC_LI64(dst_reg, imm64);
  638. break;
  639. /*
  640. * Return/Exit
  641. */
  642. case BPF_JMP | BPF_EXIT:
  643. /*
  644. * If this isn't the very last instruction, branch to
  645. * the epilogue. If we _are_ the last instruction,
  646. * we'll just fall through to the epilogue.
  647. */
  648. if (i != flen - 1)
  649. PPC_JMP(exit_addr);
  650. /* else fall through to the epilogue */
  651. break;
  652. /*
  653. * Call kernel helper or bpf function
  654. */
  655. case BPF_JMP | BPF_CALL:
  656. ctx->seen |= SEEN_FUNC;
  657. /* bpf function call */
  658. if (insn[i].src_reg == BPF_PSEUDO_CALL)
  659. if (!extra_pass)
  660. func = NULL;
  661. else if (fp->aux->func && off < fp->aux->func_cnt)
  662. /* use the subprog id from the off
  663. * field to lookup the callee address
  664. */
  665. func = (u8 *) fp->aux->func[off]->bpf_func;
  666. else
  667. return -EINVAL;
  668. /* kernel helper call */
  669. else
  670. func = (u8 *) __bpf_call_base + imm;
  671. bpf_jit_emit_func_call(image, ctx, (u64)func);
  672. /* move return value from r3 to BPF_REG_0 */
  673. PPC_MR(b2p[BPF_REG_0], 3);
  674. break;
  675. /*
  676. * Jumps and branches
  677. */
  678. case BPF_JMP | BPF_JA:
  679. PPC_JMP(addrs[i + 1 + off]);
  680. break;
  681. case BPF_JMP | BPF_JGT | BPF_K:
  682. case BPF_JMP | BPF_JGT | BPF_X:
  683. case BPF_JMP | BPF_JSGT | BPF_K:
  684. case BPF_JMP | BPF_JSGT | BPF_X:
  685. true_cond = COND_GT;
  686. goto cond_branch;
  687. case BPF_JMP | BPF_JLT | BPF_K:
  688. case BPF_JMP | BPF_JLT | BPF_X:
  689. case BPF_JMP | BPF_JSLT | BPF_K:
  690. case BPF_JMP | BPF_JSLT | BPF_X:
  691. true_cond = COND_LT;
  692. goto cond_branch;
  693. case BPF_JMP | BPF_JGE | BPF_K:
  694. case BPF_JMP | BPF_JGE | BPF_X:
  695. case BPF_JMP | BPF_JSGE | BPF_K:
  696. case BPF_JMP | BPF_JSGE | BPF_X:
  697. true_cond = COND_GE;
  698. goto cond_branch;
  699. case BPF_JMP | BPF_JLE | BPF_K:
  700. case BPF_JMP | BPF_JLE | BPF_X:
  701. case BPF_JMP | BPF_JSLE | BPF_K:
  702. case BPF_JMP | BPF_JSLE | BPF_X:
  703. true_cond = COND_LE;
  704. goto cond_branch;
  705. case BPF_JMP | BPF_JEQ | BPF_K:
  706. case BPF_JMP | BPF_JEQ | BPF_X:
  707. true_cond = COND_EQ;
  708. goto cond_branch;
  709. case BPF_JMP | BPF_JNE | BPF_K:
  710. case BPF_JMP | BPF_JNE | BPF_X:
  711. true_cond = COND_NE;
  712. goto cond_branch;
  713. case BPF_JMP | BPF_JSET | BPF_K:
  714. case BPF_JMP | BPF_JSET | BPF_X:
  715. true_cond = COND_NE;
  716. /* Fall through */
  717. cond_branch:
  718. switch (code) {
  719. case BPF_JMP | BPF_JGT | BPF_X:
  720. case BPF_JMP | BPF_JLT | BPF_X:
  721. case BPF_JMP | BPF_JGE | BPF_X:
  722. case BPF_JMP | BPF_JLE | BPF_X:
  723. case BPF_JMP | BPF_JEQ | BPF_X:
  724. case BPF_JMP | BPF_JNE | BPF_X:
  725. /* unsigned comparison */
  726. PPC_CMPLD(dst_reg, src_reg);
  727. break;
  728. case BPF_JMP | BPF_JSGT | BPF_X:
  729. case BPF_JMP | BPF_JSLT | BPF_X:
  730. case BPF_JMP | BPF_JSGE | BPF_X:
  731. case BPF_JMP | BPF_JSLE | BPF_X:
  732. /* signed comparison */
  733. PPC_CMPD(dst_reg, src_reg);
  734. break;
  735. case BPF_JMP | BPF_JSET | BPF_X:
  736. PPC_AND_DOT(b2p[TMP_REG_1], dst_reg, src_reg);
  737. break;
  738. case BPF_JMP | BPF_JNE | BPF_K:
  739. case BPF_JMP | BPF_JEQ | BPF_K:
  740. case BPF_JMP | BPF_JGT | BPF_K:
  741. case BPF_JMP | BPF_JLT | BPF_K:
  742. case BPF_JMP | BPF_JGE | BPF_K:
  743. case BPF_JMP | BPF_JLE | BPF_K:
  744. /*
  745. * Need sign-extended load, so only positive
  746. * values can be used as imm in cmpldi
  747. */
  748. if (imm >= 0 && imm < 32768)
  749. PPC_CMPLDI(dst_reg, imm);
  750. else {
  751. /* sign-extending load */
  752. PPC_LI32(b2p[TMP_REG_1], imm);
  753. /* ... but unsigned comparison */
  754. PPC_CMPLD(dst_reg, b2p[TMP_REG_1]);
  755. }
  756. break;
  757. case BPF_JMP | BPF_JSGT | BPF_K:
  758. case BPF_JMP | BPF_JSLT | BPF_K:
  759. case BPF_JMP | BPF_JSGE | BPF_K:
  760. case BPF_JMP | BPF_JSLE | BPF_K:
  761. /*
  762. * signed comparison, so any 16-bit value
  763. * can be used in cmpdi
  764. */
  765. if (imm >= -32768 && imm < 32768)
  766. PPC_CMPDI(dst_reg, imm);
  767. else {
  768. PPC_LI32(b2p[TMP_REG_1], imm);
  769. PPC_CMPD(dst_reg, b2p[TMP_REG_1]);
  770. }
  771. break;
  772. case BPF_JMP | BPF_JSET | BPF_K:
  773. /* andi does not sign-extend the immediate */
  774. if (imm >= 0 && imm < 32768)
  775. /* PPC_ANDI is _only/always_ dot-form */
  776. PPC_ANDI(b2p[TMP_REG_1], dst_reg, imm);
  777. else {
  778. PPC_LI32(b2p[TMP_REG_1], imm);
  779. PPC_AND_DOT(b2p[TMP_REG_1], dst_reg,
  780. b2p[TMP_REG_1]);
  781. }
  782. break;
  783. }
  784. PPC_BCC(true_cond, addrs[i + 1 + off]);
  785. break;
  786. /*
  787. * Tail call
  788. */
  789. case BPF_JMP | BPF_TAIL_CALL:
  790. ctx->seen |= SEEN_TAILCALL;
  791. bpf_jit_emit_tail_call(image, ctx, addrs[i + 1]);
  792. break;
  793. default:
  794. /*
  795. * The filter contains something cruel & unusual.
  796. * We don't handle it, but also there shouldn't be
  797. * anything missing from our list.
  798. */
  799. pr_err_ratelimited("eBPF filter opcode %04x (@%d) unsupported\n",
  800. code, i);
  801. return -ENOTSUPP;
  802. }
  803. }
  804. /* Set end-of-body-code address for exit. */
  805. addrs[i] = ctx->idx * 4;
  806. return 0;
  807. }
  808. struct powerpc64_jit_data {
  809. struct bpf_binary_header *header;
  810. u32 *addrs;
  811. u8 *image;
  812. u32 proglen;
  813. struct codegen_context ctx;
  814. };
  815. struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *fp)
  816. {
  817. u32 proglen;
  818. u32 alloclen;
  819. u8 *image = NULL;
  820. u32 *code_base;
  821. u32 *addrs;
  822. struct powerpc64_jit_data *jit_data;
  823. struct codegen_context cgctx;
  824. int pass;
  825. int flen;
  826. struct bpf_binary_header *bpf_hdr;
  827. struct bpf_prog *org_fp = fp;
  828. struct bpf_prog *tmp_fp;
  829. bool bpf_blinded = false;
  830. bool extra_pass = false;
  831. if (!fp->jit_requested)
  832. return org_fp;
  833. tmp_fp = bpf_jit_blind_constants(org_fp);
  834. if (IS_ERR(tmp_fp))
  835. return org_fp;
  836. if (tmp_fp != org_fp) {
  837. bpf_blinded = true;
  838. fp = tmp_fp;
  839. }
  840. jit_data = fp->aux->jit_data;
  841. if (!jit_data) {
  842. jit_data = kzalloc(sizeof(*jit_data), GFP_KERNEL);
  843. if (!jit_data) {
  844. fp = org_fp;
  845. goto out;
  846. }
  847. fp->aux->jit_data = jit_data;
  848. }
  849. flen = fp->len;
  850. addrs = jit_data->addrs;
  851. if (addrs) {
  852. cgctx = jit_data->ctx;
  853. image = jit_data->image;
  854. bpf_hdr = jit_data->header;
  855. proglen = jit_data->proglen;
  856. alloclen = proglen + FUNCTION_DESCR_SIZE;
  857. extra_pass = true;
  858. goto skip_init_ctx;
  859. }
  860. addrs = kcalloc(flen + 1, sizeof(*addrs), GFP_KERNEL);
  861. if (addrs == NULL) {
  862. fp = org_fp;
  863. goto out_addrs;
  864. }
  865. memset(&cgctx, 0, sizeof(struct codegen_context));
  866. /* Make sure that the stack is quadword aligned. */
  867. cgctx.stack_size = round_up(fp->aux->stack_depth, 16);
  868. /* Scouting faux-generate pass 0 */
  869. if (bpf_jit_build_body(fp, 0, &cgctx, addrs, false)) {
  870. /* We hit something illegal or unsupported. */
  871. fp = org_fp;
  872. goto out_addrs;
  873. }
  874. /*
  875. * Pretend to build prologue, given the features we've seen. This will
  876. * update ctgtx.idx as it pretends to output instructions, then we can
  877. * calculate total size from idx.
  878. */
  879. bpf_jit_build_prologue(0, &cgctx);
  880. bpf_jit_build_epilogue(0, &cgctx);
  881. proglen = cgctx.idx * 4;
  882. alloclen = proglen + FUNCTION_DESCR_SIZE;
  883. bpf_hdr = bpf_jit_binary_alloc(alloclen, &image, 4,
  884. bpf_jit_fill_ill_insns);
  885. if (!bpf_hdr) {
  886. fp = org_fp;
  887. goto out_addrs;
  888. }
  889. skip_init_ctx:
  890. code_base = (u32 *)(image + FUNCTION_DESCR_SIZE);
  891. /* Code generation passes 1-2 */
  892. for (pass = 1; pass < 3; pass++) {
  893. /* Now build the prologue, body code & epilogue for real. */
  894. cgctx.idx = 0;
  895. bpf_jit_build_prologue(code_base, &cgctx);
  896. bpf_jit_build_body(fp, code_base, &cgctx, addrs, extra_pass);
  897. bpf_jit_build_epilogue(code_base, &cgctx);
  898. if (bpf_jit_enable > 1)
  899. pr_info("Pass %d: shrink = %d, seen = 0x%x\n", pass,
  900. proglen - (cgctx.idx * 4), cgctx.seen);
  901. }
  902. if (bpf_jit_enable > 1)
  903. /*
  904. * Note that we output the base address of the code_base
  905. * rather than image, since opcodes are in code_base.
  906. */
  907. bpf_jit_dump(flen, proglen, pass, code_base);
  908. #ifdef PPC64_ELF_ABI_v1
  909. /* Function descriptor nastiness: Address + TOC */
  910. ((u64 *)image)[0] = (u64)code_base;
  911. ((u64 *)image)[1] = local_paca->kernel_toc;
  912. #endif
  913. fp->bpf_func = (void *)image;
  914. fp->jited = 1;
  915. fp->jited_len = alloclen;
  916. bpf_flush_icache(bpf_hdr, (u8 *)bpf_hdr + (bpf_hdr->pages * PAGE_SIZE));
  917. if (!fp->is_func || extra_pass) {
  918. out_addrs:
  919. kfree(addrs);
  920. kfree(jit_data);
  921. fp->aux->jit_data = NULL;
  922. } else {
  923. jit_data->addrs = addrs;
  924. jit_data->ctx = cgctx;
  925. jit_data->proglen = proglen;
  926. jit_data->image = image;
  927. jit_data->header = bpf_hdr;
  928. }
  929. out:
  930. if (bpf_blinded)
  931. bpf_jit_prog_release_other(fp, fp == org_fp ? tmp_fp : org_fp);
  932. return fp;
  933. }
  934. /* Overriding bpf_jit_free() as we don't set images read-only. */
  935. void bpf_jit_free(struct bpf_prog *fp)
  936. {
  937. unsigned long addr = (unsigned long)fp->bpf_func & PAGE_MASK;
  938. struct bpf_binary_header *bpf_hdr = (void *)addr;
  939. if (fp->jited)
  940. bpf_jit_binary_free(bpf_hdr);
  941. bpf_prog_unlock_free(fp);
  942. }