bpf_jit_comp64.c 29 KB

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