bpf_jit_comp64.c 29 KB

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