bpf_jit_comp64.c 30 KB

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