bpf_jit_comp.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183
  1. /*
  2. * bpf_jit_comp.c: BPF JIT compiler
  3. *
  4. * Copyright (C) 2011-2013 Eric Dumazet (eric.dumazet@gmail.com)
  5. * Internal BPF Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; version 2
  10. * of the License.
  11. */
  12. #include <linux/netdevice.h>
  13. #include <linux/filter.h>
  14. #include <linux/if_vlan.h>
  15. #include <linux/bpf.h>
  16. #include <asm/set_memory.h>
  17. #include <asm/nospec-branch.h>
  18. static u8 *emit_code(u8 *ptr, u32 bytes, unsigned int len)
  19. {
  20. if (len == 1)
  21. *ptr = bytes;
  22. else if (len == 2)
  23. *(u16 *)ptr = bytes;
  24. else {
  25. *(u32 *)ptr = bytes;
  26. barrier();
  27. }
  28. return ptr + len;
  29. }
  30. #define EMIT(bytes, len) \
  31. do { prog = emit_code(prog, bytes, len); cnt += len; } while (0)
  32. #define EMIT1(b1) EMIT(b1, 1)
  33. #define EMIT2(b1, b2) EMIT((b1) + ((b2) << 8), 2)
  34. #define EMIT3(b1, b2, b3) EMIT((b1) + ((b2) << 8) + ((b3) << 16), 3)
  35. #define EMIT4(b1, b2, b3, b4) EMIT((b1) + ((b2) << 8) + ((b3) << 16) + ((b4) << 24), 4)
  36. #define EMIT1_off32(b1, off) \
  37. do { EMIT1(b1); EMIT(off, 4); } while (0)
  38. #define EMIT2_off32(b1, b2, off) \
  39. do { EMIT2(b1, b2); EMIT(off, 4); } while (0)
  40. #define EMIT3_off32(b1, b2, b3, off) \
  41. do { EMIT3(b1, b2, b3); EMIT(off, 4); } while (0)
  42. #define EMIT4_off32(b1, b2, b3, b4, off) \
  43. do { EMIT4(b1, b2, b3, b4); EMIT(off, 4); } while (0)
  44. static bool is_imm8(int value)
  45. {
  46. return value <= 127 && value >= -128;
  47. }
  48. static bool is_simm32(s64 value)
  49. {
  50. return value == (s64)(s32)value;
  51. }
  52. static bool is_uimm32(u64 value)
  53. {
  54. return value == (u64)(u32)value;
  55. }
  56. /* mov dst, src */
  57. #define EMIT_mov(DST, SRC) \
  58. do { \
  59. if (DST != SRC) \
  60. EMIT3(add_2mod(0x48, DST, SRC), 0x89, add_2reg(0xC0, DST, SRC)); \
  61. } while (0)
  62. static int bpf_size_to_x86_bytes(int bpf_size)
  63. {
  64. if (bpf_size == BPF_W)
  65. return 4;
  66. else if (bpf_size == BPF_H)
  67. return 2;
  68. else if (bpf_size == BPF_B)
  69. return 1;
  70. else if (bpf_size == BPF_DW)
  71. return 4; /* imm32 */
  72. else
  73. return 0;
  74. }
  75. /*
  76. * List of x86 cond jumps opcodes (. + s8)
  77. * Add 0x10 (and an extra 0x0f) to generate far jumps (. + s32)
  78. */
  79. #define X86_JB 0x72
  80. #define X86_JAE 0x73
  81. #define X86_JE 0x74
  82. #define X86_JNE 0x75
  83. #define X86_JBE 0x76
  84. #define X86_JA 0x77
  85. #define X86_JL 0x7C
  86. #define X86_JGE 0x7D
  87. #define X86_JLE 0x7E
  88. #define X86_JG 0x7F
  89. /* Pick a register outside of BPF range for JIT internal work */
  90. #define AUX_REG (MAX_BPF_JIT_REG + 1)
  91. /*
  92. * The following table maps BPF registers to x86-64 registers.
  93. *
  94. * x86-64 register R12 is unused, since if used as base address
  95. * register in load/store instructions, it always needs an
  96. * extra byte of encoding and is callee saved.
  97. *
  98. * Also x86-64 register R9 is unused. x86-64 register R10 is
  99. * used for blinding (if enabled).
  100. */
  101. static const int reg2hex[] = {
  102. [BPF_REG_0] = 0, /* RAX */
  103. [BPF_REG_1] = 7, /* RDI */
  104. [BPF_REG_2] = 6, /* RSI */
  105. [BPF_REG_3] = 2, /* RDX */
  106. [BPF_REG_4] = 1, /* RCX */
  107. [BPF_REG_5] = 0, /* R8 */
  108. [BPF_REG_6] = 3, /* RBX callee saved */
  109. [BPF_REG_7] = 5, /* R13 callee saved */
  110. [BPF_REG_8] = 6, /* R14 callee saved */
  111. [BPF_REG_9] = 7, /* R15 callee saved */
  112. [BPF_REG_FP] = 5, /* RBP readonly */
  113. [BPF_REG_AX] = 2, /* R10 temp register */
  114. [AUX_REG] = 3, /* R11 temp register */
  115. };
  116. /*
  117. * is_ereg() == true if BPF register 'reg' maps to x86-64 r8..r15
  118. * which need extra byte of encoding.
  119. * rax,rcx,...,rbp have simpler encoding
  120. */
  121. static bool is_ereg(u32 reg)
  122. {
  123. return (1 << reg) & (BIT(BPF_REG_5) |
  124. BIT(AUX_REG) |
  125. BIT(BPF_REG_7) |
  126. BIT(BPF_REG_8) |
  127. BIT(BPF_REG_9) |
  128. BIT(BPF_REG_AX));
  129. }
  130. /*
  131. * is_ereg_8l() == true if BPF register 'reg' is mapped to access x86-64
  132. * lower 8-bit registers dil,sil,bpl,spl,r8b..r15b, which need extra byte
  133. * of encoding. al,cl,dl,bl have simpler encoding.
  134. */
  135. static bool is_ereg_8l(u32 reg)
  136. {
  137. return is_ereg(reg) ||
  138. (1 << reg) & (BIT(BPF_REG_1) |
  139. BIT(BPF_REG_2) |
  140. BIT(BPF_REG_FP));
  141. }
  142. static bool is_axreg(u32 reg)
  143. {
  144. return reg == BPF_REG_0;
  145. }
  146. /* Add modifiers if 'reg' maps to x86-64 registers R8..R15 */
  147. static u8 add_1mod(u8 byte, u32 reg)
  148. {
  149. if (is_ereg(reg))
  150. byte |= 1;
  151. return byte;
  152. }
  153. static u8 add_2mod(u8 byte, u32 r1, u32 r2)
  154. {
  155. if (is_ereg(r1))
  156. byte |= 1;
  157. if (is_ereg(r2))
  158. byte |= 4;
  159. return byte;
  160. }
  161. /* Encode 'dst_reg' register into x86-64 opcode 'byte' */
  162. static u8 add_1reg(u8 byte, u32 dst_reg)
  163. {
  164. return byte + reg2hex[dst_reg];
  165. }
  166. /* Encode 'dst_reg' and 'src_reg' registers into x86-64 opcode 'byte' */
  167. static u8 add_2reg(u8 byte, u32 dst_reg, u32 src_reg)
  168. {
  169. return byte + reg2hex[dst_reg] + (reg2hex[src_reg] << 3);
  170. }
  171. static void jit_fill_hole(void *area, unsigned int size)
  172. {
  173. /* Fill whole space with INT3 instructions */
  174. memset(area, 0xcc, size);
  175. }
  176. struct jit_context {
  177. int cleanup_addr; /* Epilogue code offset */
  178. };
  179. /* Maximum number of bytes emitted while JITing one eBPF insn */
  180. #define BPF_MAX_INSN_SIZE 128
  181. #define BPF_INSN_SAFETY 64
  182. #define PROLOGUE_SIZE 20
  183. /*
  184. * Emit x86-64 prologue code for BPF program and check its size.
  185. * bpf_tail_call helper will skip it while jumping into another program
  186. */
  187. static void emit_prologue(u8 **pprog, u32 stack_depth, bool ebpf_from_cbpf)
  188. {
  189. u8 *prog = *pprog;
  190. int cnt = 0;
  191. EMIT1(0x55); /* push rbp */
  192. EMIT3(0x48, 0x89, 0xE5); /* mov rbp, rsp */
  193. /* sub rsp, rounded_stack_depth */
  194. EMIT3_off32(0x48, 0x81, 0xEC, round_up(stack_depth, 8));
  195. EMIT1(0x53); /* push rbx */
  196. EMIT2(0x41, 0x55); /* push r13 */
  197. EMIT2(0x41, 0x56); /* push r14 */
  198. EMIT2(0x41, 0x57); /* push r15 */
  199. if (!ebpf_from_cbpf) {
  200. /* zero init tail_call_cnt */
  201. EMIT2(0x6a, 0x00);
  202. BUILD_BUG_ON(cnt != PROLOGUE_SIZE);
  203. }
  204. *pprog = prog;
  205. }
  206. /*
  207. * Generate the following code:
  208. *
  209. * ... bpf_tail_call(void *ctx, struct bpf_array *array, u64 index) ...
  210. * if (index >= array->map.max_entries)
  211. * goto out;
  212. * if (++tail_call_cnt > MAX_TAIL_CALL_CNT)
  213. * goto out;
  214. * prog = array->ptrs[index];
  215. * if (prog == NULL)
  216. * goto out;
  217. * goto *(prog->bpf_func + prologue_size);
  218. * out:
  219. */
  220. static void emit_bpf_tail_call(u8 **pprog)
  221. {
  222. u8 *prog = *pprog;
  223. int label1, label2, label3;
  224. int cnt = 0;
  225. /*
  226. * rdi - pointer to ctx
  227. * rsi - pointer to bpf_array
  228. * rdx - index in bpf_array
  229. */
  230. /*
  231. * if (index >= array->map.max_entries)
  232. * goto out;
  233. */
  234. EMIT2(0x89, 0xD2); /* mov edx, edx */
  235. EMIT3(0x39, 0x56, /* cmp dword ptr [rsi + 16], edx */
  236. offsetof(struct bpf_array, map.max_entries));
  237. #define OFFSET1 (41 + RETPOLINE_RAX_BPF_JIT_SIZE) /* Number of bytes to jump */
  238. EMIT2(X86_JBE, OFFSET1); /* jbe out */
  239. label1 = cnt;
  240. /*
  241. * if (tail_call_cnt > MAX_TAIL_CALL_CNT)
  242. * goto out;
  243. */
  244. EMIT2_off32(0x8B, 0x85, -36 - MAX_BPF_STACK); /* mov eax, dword ptr [rbp - 548] */
  245. EMIT3(0x83, 0xF8, MAX_TAIL_CALL_CNT); /* cmp eax, MAX_TAIL_CALL_CNT */
  246. #define OFFSET2 (30 + RETPOLINE_RAX_BPF_JIT_SIZE)
  247. EMIT2(X86_JA, OFFSET2); /* ja out */
  248. label2 = cnt;
  249. EMIT3(0x83, 0xC0, 0x01); /* add eax, 1 */
  250. EMIT2_off32(0x89, 0x85, -36 - MAX_BPF_STACK); /* mov dword ptr [rbp -548], eax */
  251. /* prog = array->ptrs[index]; */
  252. EMIT4_off32(0x48, 0x8B, 0x84, 0xD6, /* mov rax, [rsi + rdx * 8 + offsetof(...)] */
  253. offsetof(struct bpf_array, ptrs));
  254. /*
  255. * if (prog == NULL)
  256. * goto out;
  257. */
  258. EMIT3(0x48, 0x85, 0xC0); /* test rax,rax */
  259. #define OFFSET3 (8 + RETPOLINE_RAX_BPF_JIT_SIZE)
  260. EMIT2(X86_JE, OFFSET3); /* je out */
  261. label3 = cnt;
  262. /* goto *(prog->bpf_func + prologue_size); */
  263. EMIT4(0x48, 0x8B, 0x40, /* mov rax, qword ptr [rax + 32] */
  264. offsetof(struct bpf_prog, bpf_func));
  265. EMIT4(0x48, 0x83, 0xC0, PROLOGUE_SIZE); /* add rax, prologue_size */
  266. /*
  267. * Wow we're ready to jump into next BPF program
  268. * rdi == ctx (1st arg)
  269. * rax == prog->bpf_func + prologue_size
  270. */
  271. RETPOLINE_RAX_BPF_JIT();
  272. /* out: */
  273. BUILD_BUG_ON(cnt - label1 != OFFSET1);
  274. BUILD_BUG_ON(cnt - label2 != OFFSET2);
  275. BUILD_BUG_ON(cnt - label3 != OFFSET3);
  276. *pprog = prog;
  277. }
  278. static void emit_mov_imm32(u8 **pprog, bool sign_propagate,
  279. u32 dst_reg, const u32 imm32)
  280. {
  281. u8 *prog = *pprog;
  282. u8 b1, b2, b3;
  283. int cnt = 0;
  284. /*
  285. * Optimization: if imm32 is positive, use 'mov %eax, imm32'
  286. * (which zero-extends imm32) to save 2 bytes.
  287. */
  288. if (sign_propagate && (s32)imm32 < 0) {
  289. /* 'mov %rax, imm32' sign extends imm32 */
  290. b1 = add_1mod(0x48, dst_reg);
  291. b2 = 0xC7;
  292. b3 = 0xC0;
  293. EMIT3_off32(b1, b2, add_1reg(b3, dst_reg), imm32);
  294. goto done;
  295. }
  296. /*
  297. * Optimization: if imm32 is zero, use 'xor %eax, %eax'
  298. * to save 3 bytes.
  299. */
  300. if (imm32 == 0) {
  301. if (is_ereg(dst_reg))
  302. EMIT1(add_2mod(0x40, dst_reg, dst_reg));
  303. b2 = 0x31; /* xor */
  304. b3 = 0xC0;
  305. EMIT2(b2, add_2reg(b3, dst_reg, dst_reg));
  306. goto done;
  307. }
  308. /* mov %eax, imm32 */
  309. if (is_ereg(dst_reg))
  310. EMIT1(add_1mod(0x40, dst_reg));
  311. EMIT1_off32(add_1reg(0xB8, dst_reg), imm32);
  312. done:
  313. *pprog = prog;
  314. }
  315. static void emit_mov_imm64(u8 **pprog, u32 dst_reg,
  316. const u32 imm32_hi, const u32 imm32_lo)
  317. {
  318. u8 *prog = *pprog;
  319. int cnt = 0;
  320. if (is_uimm32(((u64)imm32_hi << 32) | (u32)imm32_lo)) {
  321. /*
  322. * For emitting plain u32, where sign bit must not be
  323. * propagated LLVM tends to load imm64 over mov32
  324. * directly, so save couple of bytes by just doing
  325. * 'mov %eax, imm32' instead.
  326. */
  327. emit_mov_imm32(&prog, false, dst_reg, imm32_lo);
  328. } else {
  329. /* movabsq %rax, imm64 */
  330. EMIT2(add_1mod(0x48, dst_reg), add_1reg(0xB8, dst_reg));
  331. EMIT(imm32_lo, 4);
  332. EMIT(imm32_hi, 4);
  333. }
  334. *pprog = prog;
  335. }
  336. static void emit_mov_reg(u8 **pprog, bool is64, u32 dst_reg, u32 src_reg)
  337. {
  338. u8 *prog = *pprog;
  339. int cnt = 0;
  340. if (is64) {
  341. /* mov dst, src */
  342. EMIT_mov(dst_reg, src_reg);
  343. } else {
  344. /* mov32 dst, src */
  345. if (is_ereg(dst_reg) || is_ereg(src_reg))
  346. EMIT1(add_2mod(0x40, dst_reg, src_reg));
  347. EMIT2(0x89, add_2reg(0xC0, dst_reg, src_reg));
  348. }
  349. *pprog = prog;
  350. }
  351. static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image,
  352. int oldproglen, struct jit_context *ctx)
  353. {
  354. struct bpf_insn *insn = bpf_prog->insnsi;
  355. int insn_cnt = bpf_prog->len;
  356. bool seen_exit = false;
  357. u8 temp[BPF_MAX_INSN_SIZE + BPF_INSN_SAFETY];
  358. int i, cnt = 0;
  359. int proglen = 0;
  360. u8 *prog = temp;
  361. emit_prologue(&prog, bpf_prog->aux->stack_depth,
  362. bpf_prog_was_classic(bpf_prog));
  363. for (i = 0; i < insn_cnt; i++, insn++) {
  364. const s32 imm32 = insn->imm;
  365. u32 dst_reg = insn->dst_reg;
  366. u32 src_reg = insn->src_reg;
  367. u8 b2 = 0, b3 = 0;
  368. s64 jmp_offset;
  369. u8 jmp_cond;
  370. int ilen;
  371. u8 *func;
  372. switch (insn->code) {
  373. /* ALU */
  374. case BPF_ALU | BPF_ADD | BPF_X:
  375. case BPF_ALU | BPF_SUB | BPF_X:
  376. case BPF_ALU | BPF_AND | BPF_X:
  377. case BPF_ALU | BPF_OR | BPF_X:
  378. case BPF_ALU | BPF_XOR | BPF_X:
  379. case BPF_ALU64 | BPF_ADD | BPF_X:
  380. case BPF_ALU64 | BPF_SUB | BPF_X:
  381. case BPF_ALU64 | BPF_AND | BPF_X:
  382. case BPF_ALU64 | BPF_OR | BPF_X:
  383. case BPF_ALU64 | BPF_XOR | BPF_X:
  384. switch (BPF_OP(insn->code)) {
  385. case BPF_ADD: b2 = 0x01; break;
  386. case BPF_SUB: b2 = 0x29; break;
  387. case BPF_AND: b2 = 0x21; break;
  388. case BPF_OR: b2 = 0x09; break;
  389. case BPF_XOR: b2 = 0x31; break;
  390. }
  391. if (BPF_CLASS(insn->code) == BPF_ALU64)
  392. EMIT1(add_2mod(0x48, dst_reg, src_reg));
  393. else if (is_ereg(dst_reg) || is_ereg(src_reg))
  394. EMIT1(add_2mod(0x40, dst_reg, src_reg));
  395. EMIT2(b2, add_2reg(0xC0, dst_reg, src_reg));
  396. break;
  397. case BPF_ALU64 | BPF_MOV | BPF_X:
  398. case BPF_ALU | BPF_MOV | BPF_X:
  399. emit_mov_reg(&prog,
  400. BPF_CLASS(insn->code) == BPF_ALU64,
  401. dst_reg, src_reg);
  402. break;
  403. /* neg dst */
  404. case BPF_ALU | BPF_NEG:
  405. case BPF_ALU64 | BPF_NEG:
  406. if (BPF_CLASS(insn->code) == BPF_ALU64)
  407. EMIT1(add_1mod(0x48, dst_reg));
  408. else if (is_ereg(dst_reg))
  409. EMIT1(add_1mod(0x40, dst_reg));
  410. EMIT2(0xF7, add_1reg(0xD8, dst_reg));
  411. break;
  412. case BPF_ALU | BPF_ADD | BPF_K:
  413. case BPF_ALU | BPF_SUB | BPF_K:
  414. case BPF_ALU | BPF_AND | BPF_K:
  415. case BPF_ALU | BPF_OR | BPF_K:
  416. case BPF_ALU | BPF_XOR | BPF_K:
  417. case BPF_ALU64 | BPF_ADD | BPF_K:
  418. case BPF_ALU64 | BPF_SUB | BPF_K:
  419. case BPF_ALU64 | BPF_AND | BPF_K:
  420. case BPF_ALU64 | BPF_OR | BPF_K:
  421. case BPF_ALU64 | BPF_XOR | BPF_K:
  422. if (BPF_CLASS(insn->code) == BPF_ALU64)
  423. EMIT1(add_1mod(0x48, dst_reg));
  424. else if (is_ereg(dst_reg))
  425. EMIT1(add_1mod(0x40, dst_reg));
  426. /*
  427. * b3 holds 'normal' opcode, b2 short form only valid
  428. * in case dst is eax/rax.
  429. */
  430. switch (BPF_OP(insn->code)) {
  431. case BPF_ADD:
  432. b3 = 0xC0;
  433. b2 = 0x05;
  434. break;
  435. case BPF_SUB:
  436. b3 = 0xE8;
  437. b2 = 0x2D;
  438. break;
  439. case BPF_AND:
  440. b3 = 0xE0;
  441. b2 = 0x25;
  442. break;
  443. case BPF_OR:
  444. b3 = 0xC8;
  445. b2 = 0x0D;
  446. break;
  447. case BPF_XOR:
  448. b3 = 0xF0;
  449. b2 = 0x35;
  450. break;
  451. }
  452. if (is_imm8(imm32))
  453. EMIT3(0x83, add_1reg(b3, dst_reg), imm32);
  454. else if (is_axreg(dst_reg))
  455. EMIT1_off32(b2, imm32);
  456. else
  457. EMIT2_off32(0x81, add_1reg(b3, dst_reg), imm32);
  458. break;
  459. case BPF_ALU64 | BPF_MOV | BPF_K:
  460. case BPF_ALU | BPF_MOV | BPF_K:
  461. emit_mov_imm32(&prog, BPF_CLASS(insn->code) == BPF_ALU64,
  462. dst_reg, imm32);
  463. break;
  464. case BPF_LD | BPF_IMM | BPF_DW:
  465. emit_mov_imm64(&prog, dst_reg, insn[1].imm, insn[0].imm);
  466. insn++;
  467. i++;
  468. break;
  469. /* dst %= src, dst /= src, dst %= imm32, dst /= imm32 */
  470. case BPF_ALU | BPF_MOD | BPF_X:
  471. case BPF_ALU | BPF_DIV | BPF_X:
  472. case BPF_ALU | BPF_MOD | BPF_K:
  473. case BPF_ALU | BPF_DIV | BPF_K:
  474. case BPF_ALU64 | BPF_MOD | BPF_X:
  475. case BPF_ALU64 | BPF_DIV | BPF_X:
  476. case BPF_ALU64 | BPF_MOD | BPF_K:
  477. case BPF_ALU64 | BPF_DIV | BPF_K:
  478. EMIT1(0x50); /* push rax */
  479. EMIT1(0x52); /* push rdx */
  480. if (BPF_SRC(insn->code) == BPF_X)
  481. /* mov r11, src_reg */
  482. EMIT_mov(AUX_REG, src_reg);
  483. else
  484. /* mov r11, imm32 */
  485. EMIT3_off32(0x49, 0xC7, 0xC3, imm32);
  486. /* mov rax, dst_reg */
  487. EMIT_mov(BPF_REG_0, dst_reg);
  488. /*
  489. * xor edx, edx
  490. * equivalent to 'xor rdx, rdx', but one byte less
  491. */
  492. EMIT2(0x31, 0xd2);
  493. if (BPF_CLASS(insn->code) == BPF_ALU64)
  494. /* div r11 */
  495. EMIT3(0x49, 0xF7, 0xF3);
  496. else
  497. /* div r11d */
  498. EMIT3(0x41, 0xF7, 0xF3);
  499. if (BPF_OP(insn->code) == BPF_MOD)
  500. /* mov r11, rdx */
  501. EMIT3(0x49, 0x89, 0xD3);
  502. else
  503. /* mov r11, rax */
  504. EMIT3(0x49, 0x89, 0xC3);
  505. EMIT1(0x5A); /* pop rdx */
  506. EMIT1(0x58); /* pop rax */
  507. /* mov dst_reg, r11 */
  508. EMIT_mov(dst_reg, AUX_REG);
  509. break;
  510. case BPF_ALU | BPF_MUL | BPF_K:
  511. case BPF_ALU | BPF_MUL | BPF_X:
  512. case BPF_ALU64 | BPF_MUL | BPF_K:
  513. case BPF_ALU64 | BPF_MUL | BPF_X:
  514. {
  515. bool is64 = BPF_CLASS(insn->code) == BPF_ALU64;
  516. if (dst_reg != BPF_REG_0)
  517. EMIT1(0x50); /* push rax */
  518. if (dst_reg != BPF_REG_3)
  519. EMIT1(0x52); /* push rdx */
  520. /* mov r11, dst_reg */
  521. EMIT_mov(AUX_REG, dst_reg);
  522. if (BPF_SRC(insn->code) == BPF_X)
  523. emit_mov_reg(&prog, is64, BPF_REG_0, src_reg);
  524. else
  525. emit_mov_imm32(&prog, is64, BPF_REG_0, imm32);
  526. if (is64)
  527. EMIT1(add_1mod(0x48, AUX_REG));
  528. else if (is_ereg(AUX_REG))
  529. EMIT1(add_1mod(0x40, AUX_REG));
  530. /* mul(q) r11 */
  531. EMIT2(0xF7, add_1reg(0xE0, AUX_REG));
  532. if (dst_reg != BPF_REG_3)
  533. EMIT1(0x5A); /* pop rdx */
  534. if (dst_reg != BPF_REG_0) {
  535. /* mov dst_reg, rax */
  536. EMIT_mov(dst_reg, BPF_REG_0);
  537. EMIT1(0x58); /* pop rax */
  538. }
  539. break;
  540. }
  541. /* Shifts */
  542. case BPF_ALU | BPF_LSH | BPF_K:
  543. case BPF_ALU | BPF_RSH | BPF_K:
  544. case BPF_ALU | BPF_ARSH | BPF_K:
  545. case BPF_ALU64 | BPF_LSH | BPF_K:
  546. case BPF_ALU64 | BPF_RSH | BPF_K:
  547. case BPF_ALU64 | BPF_ARSH | BPF_K:
  548. if (BPF_CLASS(insn->code) == BPF_ALU64)
  549. EMIT1(add_1mod(0x48, dst_reg));
  550. else if (is_ereg(dst_reg))
  551. EMIT1(add_1mod(0x40, dst_reg));
  552. switch (BPF_OP(insn->code)) {
  553. case BPF_LSH: b3 = 0xE0; break;
  554. case BPF_RSH: b3 = 0xE8; break;
  555. case BPF_ARSH: b3 = 0xF8; break;
  556. }
  557. if (imm32 == 1)
  558. EMIT2(0xD1, add_1reg(b3, dst_reg));
  559. else
  560. EMIT3(0xC1, add_1reg(b3, dst_reg), imm32);
  561. break;
  562. case BPF_ALU | BPF_LSH | BPF_X:
  563. case BPF_ALU | BPF_RSH | BPF_X:
  564. case BPF_ALU | BPF_ARSH | BPF_X:
  565. case BPF_ALU64 | BPF_LSH | BPF_X:
  566. case BPF_ALU64 | BPF_RSH | BPF_X:
  567. case BPF_ALU64 | BPF_ARSH | BPF_X:
  568. /* Check for bad case when dst_reg == rcx */
  569. if (dst_reg == BPF_REG_4) {
  570. /* mov r11, dst_reg */
  571. EMIT_mov(AUX_REG, dst_reg);
  572. dst_reg = AUX_REG;
  573. }
  574. if (src_reg != BPF_REG_4) { /* common case */
  575. EMIT1(0x51); /* push rcx */
  576. /* mov rcx, src_reg */
  577. EMIT_mov(BPF_REG_4, src_reg);
  578. }
  579. /* shl %rax, %cl | shr %rax, %cl | sar %rax, %cl */
  580. if (BPF_CLASS(insn->code) == BPF_ALU64)
  581. EMIT1(add_1mod(0x48, dst_reg));
  582. else if (is_ereg(dst_reg))
  583. EMIT1(add_1mod(0x40, dst_reg));
  584. switch (BPF_OP(insn->code)) {
  585. case BPF_LSH: b3 = 0xE0; break;
  586. case BPF_RSH: b3 = 0xE8; break;
  587. case BPF_ARSH: b3 = 0xF8; break;
  588. }
  589. EMIT2(0xD3, add_1reg(b3, dst_reg));
  590. if (src_reg != BPF_REG_4)
  591. EMIT1(0x59); /* pop rcx */
  592. if (insn->dst_reg == BPF_REG_4)
  593. /* mov dst_reg, r11 */
  594. EMIT_mov(insn->dst_reg, AUX_REG);
  595. break;
  596. case BPF_ALU | BPF_END | BPF_FROM_BE:
  597. switch (imm32) {
  598. case 16:
  599. /* Emit 'ror %ax, 8' to swap lower 2 bytes */
  600. EMIT1(0x66);
  601. if (is_ereg(dst_reg))
  602. EMIT1(0x41);
  603. EMIT3(0xC1, add_1reg(0xC8, dst_reg), 8);
  604. /* Emit 'movzwl eax, ax' */
  605. if (is_ereg(dst_reg))
  606. EMIT3(0x45, 0x0F, 0xB7);
  607. else
  608. EMIT2(0x0F, 0xB7);
  609. EMIT1(add_2reg(0xC0, dst_reg, dst_reg));
  610. break;
  611. case 32:
  612. /* Emit 'bswap eax' to swap lower 4 bytes */
  613. if (is_ereg(dst_reg))
  614. EMIT2(0x41, 0x0F);
  615. else
  616. EMIT1(0x0F);
  617. EMIT1(add_1reg(0xC8, dst_reg));
  618. break;
  619. case 64:
  620. /* Emit 'bswap rax' to swap 8 bytes */
  621. EMIT3(add_1mod(0x48, dst_reg), 0x0F,
  622. add_1reg(0xC8, dst_reg));
  623. break;
  624. }
  625. break;
  626. case BPF_ALU | BPF_END | BPF_FROM_LE:
  627. switch (imm32) {
  628. case 16:
  629. /*
  630. * Emit 'movzwl eax, ax' to zero extend 16-bit
  631. * into 64 bit
  632. */
  633. if (is_ereg(dst_reg))
  634. EMIT3(0x45, 0x0F, 0xB7);
  635. else
  636. EMIT2(0x0F, 0xB7);
  637. EMIT1(add_2reg(0xC0, dst_reg, dst_reg));
  638. break;
  639. case 32:
  640. /* Emit 'mov eax, eax' to clear upper 32-bits */
  641. if (is_ereg(dst_reg))
  642. EMIT1(0x45);
  643. EMIT2(0x89, add_2reg(0xC0, dst_reg, dst_reg));
  644. break;
  645. case 64:
  646. /* nop */
  647. break;
  648. }
  649. break;
  650. /* ST: *(u8*)(dst_reg + off) = imm */
  651. case BPF_ST | BPF_MEM | BPF_B:
  652. if (is_ereg(dst_reg))
  653. EMIT2(0x41, 0xC6);
  654. else
  655. EMIT1(0xC6);
  656. goto st;
  657. case BPF_ST | BPF_MEM | BPF_H:
  658. if (is_ereg(dst_reg))
  659. EMIT3(0x66, 0x41, 0xC7);
  660. else
  661. EMIT2(0x66, 0xC7);
  662. goto st;
  663. case BPF_ST | BPF_MEM | BPF_W:
  664. if (is_ereg(dst_reg))
  665. EMIT2(0x41, 0xC7);
  666. else
  667. EMIT1(0xC7);
  668. goto st;
  669. case BPF_ST | BPF_MEM | BPF_DW:
  670. EMIT2(add_1mod(0x48, dst_reg), 0xC7);
  671. st: if (is_imm8(insn->off))
  672. EMIT2(add_1reg(0x40, dst_reg), insn->off);
  673. else
  674. EMIT1_off32(add_1reg(0x80, dst_reg), insn->off);
  675. EMIT(imm32, bpf_size_to_x86_bytes(BPF_SIZE(insn->code)));
  676. break;
  677. /* STX: *(u8*)(dst_reg + off) = src_reg */
  678. case BPF_STX | BPF_MEM | BPF_B:
  679. /* Emit 'mov byte ptr [rax + off], al' */
  680. if (is_ereg(dst_reg) || is_ereg_8l(src_reg))
  681. /* Add extra byte for eregs or SIL,DIL,BPL in src_reg */
  682. EMIT2(add_2mod(0x40, dst_reg, src_reg), 0x88);
  683. else
  684. EMIT1(0x88);
  685. goto stx;
  686. case BPF_STX | BPF_MEM | BPF_H:
  687. if (is_ereg(dst_reg) || is_ereg(src_reg))
  688. EMIT3(0x66, add_2mod(0x40, dst_reg, src_reg), 0x89);
  689. else
  690. EMIT2(0x66, 0x89);
  691. goto stx;
  692. case BPF_STX | BPF_MEM | BPF_W:
  693. if (is_ereg(dst_reg) || is_ereg(src_reg))
  694. EMIT2(add_2mod(0x40, dst_reg, src_reg), 0x89);
  695. else
  696. EMIT1(0x89);
  697. goto stx;
  698. case BPF_STX | BPF_MEM | BPF_DW:
  699. EMIT2(add_2mod(0x48, dst_reg, src_reg), 0x89);
  700. stx: if (is_imm8(insn->off))
  701. EMIT2(add_2reg(0x40, dst_reg, src_reg), insn->off);
  702. else
  703. EMIT1_off32(add_2reg(0x80, dst_reg, src_reg),
  704. insn->off);
  705. break;
  706. /* LDX: dst_reg = *(u8*)(src_reg + off) */
  707. case BPF_LDX | BPF_MEM | BPF_B:
  708. /* Emit 'movzx rax, byte ptr [rax + off]' */
  709. EMIT3(add_2mod(0x48, src_reg, dst_reg), 0x0F, 0xB6);
  710. goto ldx;
  711. case BPF_LDX | BPF_MEM | BPF_H:
  712. /* Emit 'movzx rax, word ptr [rax + off]' */
  713. EMIT3(add_2mod(0x48, src_reg, dst_reg), 0x0F, 0xB7);
  714. goto ldx;
  715. case BPF_LDX | BPF_MEM | BPF_W:
  716. /* Emit 'mov eax, dword ptr [rax+0x14]' */
  717. if (is_ereg(dst_reg) || is_ereg(src_reg))
  718. EMIT2(add_2mod(0x40, src_reg, dst_reg), 0x8B);
  719. else
  720. EMIT1(0x8B);
  721. goto ldx;
  722. case BPF_LDX | BPF_MEM | BPF_DW:
  723. /* Emit 'mov rax, qword ptr [rax+0x14]' */
  724. EMIT2(add_2mod(0x48, src_reg, dst_reg), 0x8B);
  725. ldx: /*
  726. * If insn->off == 0 we can save one extra byte, but
  727. * special case of x86 R13 which always needs an offset
  728. * is not worth the hassle
  729. */
  730. if (is_imm8(insn->off))
  731. EMIT2(add_2reg(0x40, src_reg, dst_reg), insn->off);
  732. else
  733. EMIT1_off32(add_2reg(0x80, src_reg, dst_reg),
  734. insn->off);
  735. break;
  736. /* STX XADD: lock *(u32*)(dst_reg + off) += src_reg */
  737. case BPF_STX | BPF_XADD | BPF_W:
  738. /* Emit 'lock add dword ptr [rax + off], eax' */
  739. if (is_ereg(dst_reg) || is_ereg(src_reg))
  740. EMIT3(0xF0, add_2mod(0x40, dst_reg, src_reg), 0x01);
  741. else
  742. EMIT2(0xF0, 0x01);
  743. goto xadd;
  744. case BPF_STX | BPF_XADD | BPF_DW:
  745. EMIT3(0xF0, add_2mod(0x48, dst_reg, src_reg), 0x01);
  746. xadd: if (is_imm8(insn->off))
  747. EMIT2(add_2reg(0x40, dst_reg, src_reg), insn->off);
  748. else
  749. EMIT1_off32(add_2reg(0x80, dst_reg, src_reg),
  750. insn->off);
  751. break;
  752. /* call */
  753. case BPF_JMP | BPF_CALL:
  754. func = (u8 *) __bpf_call_base + imm32;
  755. jmp_offset = func - (image + addrs[i]);
  756. if (!imm32 || !is_simm32(jmp_offset)) {
  757. pr_err("unsupported BPF func %d addr %p image %p\n",
  758. imm32, func, image);
  759. return -EINVAL;
  760. }
  761. EMIT1_off32(0xE8, jmp_offset);
  762. break;
  763. case BPF_JMP | BPF_TAIL_CALL:
  764. emit_bpf_tail_call(&prog);
  765. break;
  766. /* cond jump */
  767. case BPF_JMP | BPF_JEQ | BPF_X:
  768. case BPF_JMP | BPF_JNE | BPF_X:
  769. case BPF_JMP | BPF_JGT | BPF_X:
  770. case BPF_JMP | BPF_JLT | BPF_X:
  771. case BPF_JMP | BPF_JGE | BPF_X:
  772. case BPF_JMP | BPF_JLE | BPF_X:
  773. case BPF_JMP | BPF_JSGT | BPF_X:
  774. case BPF_JMP | BPF_JSLT | BPF_X:
  775. case BPF_JMP | BPF_JSGE | BPF_X:
  776. case BPF_JMP | BPF_JSLE | BPF_X:
  777. /* cmp dst_reg, src_reg */
  778. EMIT3(add_2mod(0x48, dst_reg, src_reg), 0x39,
  779. add_2reg(0xC0, dst_reg, src_reg));
  780. goto emit_cond_jmp;
  781. case BPF_JMP | BPF_JSET | BPF_X:
  782. /* test dst_reg, src_reg */
  783. EMIT3(add_2mod(0x48, dst_reg, src_reg), 0x85,
  784. add_2reg(0xC0, dst_reg, src_reg));
  785. goto emit_cond_jmp;
  786. case BPF_JMP | BPF_JSET | BPF_K:
  787. /* test dst_reg, imm32 */
  788. EMIT1(add_1mod(0x48, dst_reg));
  789. EMIT2_off32(0xF7, add_1reg(0xC0, dst_reg), imm32);
  790. goto emit_cond_jmp;
  791. case BPF_JMP | BPF_JEQ | BPF_K:
  792. case BPF_JMP | BPF_JNE | BPF_K:
  793. case BPF_JMP | BPF_JGT | BPF_K:
  794. case BPF_JMP | BPF_JLT | BPF_K:
  795. case BPF_JMP | BPF_JGE | BPF_K:
  796. case BPF_JMP | BPF_JLE | BPF_K:
  797. case BPF_JMP | BPF_JSGT | BPF_K:
  798. case BPF_JMP | BPF_JSLT | BPF_K:
  799. case BPF_JMP | BPF_JSGE | BPF_K:
  800. case BPF_JMP | BPF_JSLE | BPF_K:
  801. /* cmp dst_reg, imm8/32 */
  802. EMIT1(add_1mod(0x48, dst_reg));
  803. if (is_imm8(imm32))
  804. EMIT3(0x83, add_1reg(0xF8, dst_reg), imm32);
  805. else
  806. EMIT2_off32(0x81, add_1reg(0xF8, dst_reg), imm32);
  807. emit_cond_jmp: /* Convert BPF opcode to x86 */
  808. switch (BPF_OP(insn->code)) {
  809. case BPF_JEQ:
  810. jmp_cond = X86_JE;
  811. break;
  812. case BPF_JSET:
  813. case BPF_JNE:
  814. jmp_cond = X86_JNE;
  815. break;
  816. case BPF_JGT:
  817. /* GT is unsigned '>', JA in x86 */
  818. jmp_cond = X86_JA;
  819. break;
  820. case BPF_JLT:
  821. /* LT is unsigned '<', JB in x86 */
  822. jmp_cond = X86_JB;
  823. break;
  824. case BPF_JGE:
  825. /* GE is unsigned '>=', JAE in x86 */
  826. jmp_cond = X86_JAE;
  827. break;
  828. case BPF_JLE:
  829. /* LE is unsigned '<=', JBE in x86 */
  830. jmp_cond = X86_JBE;
  831. break;
  832. case BPF_JSGT:
  833. /* Signed '>', GT in x86 */
  834. jmp_cond = X86_JG;
  835. break;
  836. case BPF_JSLT:
  837. /* Signed '<', LT in x86 */
  838. jmp_cond = X86_JL;
  839. break;
  840. case BPF_JSGE:
  841. /* Signed '>=', GE in x86 */
  842. jmp_cond = X86_JGE;
  843. break;
  844. case BPF_JSLE:
  845. /* Signed '<=', LE in x86 */
  846. jmp_cond = X86_JLE;
  847. break;
  848. default: /* to silence GCC warning */
  849. return -EFAULT;
  850. }
  851. jmp_offset = addrs[i + insn->off] - addrs[i];
  852. if (is_imm8(jmp_offset)) {
  853. EMIT2(jmp_cond, jmp_offset);
  854. } else if (is_simm32(jmp_offset)) {
  855. EMIT2_off32(0x0F, jmp_cond + 0x10, jmp_offset);
  856. } else {
  857. pr_err("cond_jmp gen bug %llx\n", jmp_offset);
  858. return -EFAULT;
  859. }
  860. break;
  861. case BPF_JMP | BPF_JA:
  862. if (insn->off == -1)
  863. /* -1 jmp instructions will always jump
  864. * backwards two bytes. Explicitly handling
  865. * this case avoids wasting too many passes
  866. * when there are long sequences of replaced
  867. * dead code.
  868. */
  869. jmp_offset = -2;
  870. else
  871. jmp_offset = addrs[i + insn->off] - addrs[i];
  872. if (!jmp_offset)
  873. /* Optimize out nop jumps */
  874. break;
  875. emit_jmp:
  876. if (is_imm8(jmp_offset)) {
  877. EMIT2(0xEB, jmp_offset);
  878. } else if (is_simm32(jmp_offset)) {
  879. EMIT1_off32(0xE9, jmp_offset);
  880. } else {
  881. pr_err("jmp gen bug %llx\n", jmp_offset);
  882. return -EFAULT;
  883. }
  884. break;
  885. case BPF_JMP | BPF_EXIT:
  886. if (seen_exit) {
  887. jmp_offset = ctx->cleanup_addr - addrs[i];
  888. goto emit_jmp;
  889. }
  890. seen_exit = true;
  891. /* Update cleanup_addr */
  892. ctx->cleanup_addr = proglen;
  893. if (!bpf_prog_was_classic(bpf_prog))
  894. EMIT1(0x5B); /* get rid of tail_call_cnt */
  895. EMIT2(0x41, 0x5F); /* pop r15 */
  896. EMIT2(0x41, 0x5E); /* pop r14 */
  897. EMIT2(0x41, 0x5D); /* pop r13 */
  898. EMIT1(0x5B); /* pop rbx */
  899. EMIT1(0xC9); /* leave */
  900. EMIT1(0xC3); /* ret */
  901. break;
  902. default:
  903. /*
  904. * By design x86-64 JIT should support all BPF instructions.
  905. * This error will be seen if new instruction was added
  906. * to the interpreter, but not to the JIT, or if there is
  907. * junk in bpf_prog.
  908. */
  909. pr_err("bpf_jit: unknown opcode %02x\n", insn->code);
  910. return -EINVAL;
  911. }
  912. ilen = prog - temp;
  913. if (ilen > BPF_MAX_INSN_SIZE) {
  914. pr_err("bpf_jit: fatal insn size error\n");
  915. return -EFAULT;
  916. }
  917. if (image) {
  918. /*
  919. * When populating the image, assert that:
  920. *
  921. * i) We do not write beyond the allocated space, and
  922. * ii) addrs[i] did not change from the prior run, in order
  923. * to validate assumptions made for computing branch
  924. * displacements.
  925. */
  926. if (unlikely(proglen + ilen > oldproglen ||
  927. proglen + ilen != addrs[i])) {
  928. pr_err("bpf_jit: fatal error\n");
  929. return -EFAULT;
  930. }
  931. memcpy(image + proglen, temp, ilen);
  932. }
  933. proglen += ilen;
  934. addrs[i] = proglen;
  935. prog = temp;
  936. }
  937. return proglen;
  938. }
  939. struct x64_jit_data {
  940. struct bpf_binary_header *header;
  941. int *addrs;
  942. u8 *image;
  943. int proglen;
  944. struct jit_context ctx;
  945. };
  946. struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
  947. {
  948. struct bpf_binary_header *header = NULL;
  949. struct bpf_prog *tmp, *orig_prog = prog;
  950. struct x64_jit_data *jit_data;
  951. int proglen, oldproglen = 0;
  952. struct jit_context ctx = {};
  953. bool tmp_blinded = false;
  954. bool extra_pass = false;
  955. u8 *image = NULL;
  956. int *addrs;
  957. int pass;
  958. int i;
  959. if (!prog->jit_requested)
  960. return orig_prog;
  961. tmp = bpf_jit_blind_constants(prog);
  962. /*
  963. * If blinding was requested and we failed during blinding,
  964. * we must fall back to the interpreter.
  965. */
  966. if (IS_ERR(tmp))
  967. return orig_prog;
  968. if (tmp != prog) {
  969. tmp_blinded = true;
  970. prog = tmp;
  971. }
  972. jit_data = prog->aux->jit_data;
  973. if (!jit_data) {
  974. jit_data = kzalloc(sizeof(*jit_data), GFP_KERNEL);
  975. if (!jit_data) {
  976. prog = orig_prog;
  977. goto out;
  978. }
  979. prog->aux->jit_data = jit_data;
  980. }
  981. addrs = jit_data->addrs;
  982. if (addrs) {
  983. ctx = jit_data->ctx;
  984. oldproglen = jit_data->proglen;
  985. image = jit_data->image;
  986. header = jit_data->header;
  987. extra_pass = true;
  988. goto skip_init_addrs;
  989. }
  990. addrs = kmalloc_array(prog->len, sizeof(*addrs), GFP_KERNEL);
  991. if (!addrs) {
  992. prog = orig_prog;
  993. goto out_addrs;
  994. }
  995. /*
  996. * Before first pass, make a rough estimation of addrs[]
  997. * each BPF instruction is translated to less than 64 bytes
  998. */
  999. for (proglen = 0, i = 0; i < prog->len; i++) {
  1000. proglen += 64;
  1001. addrs[i] = proglen;
  1002. }
  1003. ctx.cleanup_addr = proglen;
  1004. skip_init_addrs:
  1005. /*
  1006. * JITed image shrinks with every pass and the loop iterates
  1007. * until the image stops shrinking. Very large BPF programs
  1008. * may converge on the last pass. In such case do one more
  1009. * pass to emit the final image.
  1010. */
  1011. for (pass = 0; pass < 20 || image; pass++) {
  1012. proglen = do_jit(prog, addrs, image, oldproglen, &ctx);
  1013. if (proglen <= 0) {
  1014. out_image:
  1015. image = NULL;
  1016. if (header)
  1017. bpf_jit_binary_free(header);
  1018. prog = orig_prog;
  1019. goto out_addrs;
  1020. }
  1021. if (image) {
  1022. if (proglen != oldproglen) {
  1023. pr_err("bpf_jit: proglen=%d != oldproglen=%d\n",
  1024. proglen, oldproglen);
  1025. goto out_image;
  1026. }
  1027. break;
  1028. }
  1029. if (proglen == oldproglen) {
  1030. header = bpf_jit_binary_alloc(proglen, &image,
  1031. 1, jit_fill_hole);
  1032. if (!header) {
  1033. prog = orig_prog;
  1034. goto out_addrs;
  1035. }
  1036. }
  1037. oldproglen = proglen;
  1038. cond_resched();
  1039. }
  1040. if (bpf_jit_enable > 1)
  1041. bpf_jit_dump(prog->len, proglen, pass + 1, image);
  1042. if (image) {
  1043. if (!prog->is_func || extra_pass) {
  1044. bpf_jit_binary_lock_ro(header);
  1045. } else {
  1046. jit_data->addrs = addrs;
  1047. jit_data->ctx = ctx;
  1048. jit_data->proglen = proglen;
  1049. jit_data->image = image;
  1050. jit_data->header = header;
  1051. }
  1052. prog->bpf_func = (void *)image;
  1053. prog->jited = 1;
  1054. prog->jited_len = proglen;
  1055. } else {
  1056. prog = orig_prog;
  1057. }
  1058. if (!image || !prog->is_func || extra_pass) {
  1059. out_addrs:
  1060. kfree(addrs);
  1061. kfree(jit_data);
  1062. prog->aux->jit_data = NULL;
  1063. }
  1064. out:
  1065. if (tmp_blinded)
  1066. bpf_jit_prog_release_other(prog, prog == orig_prog ?
  1067. tmp : orig_prog);
  1068. return prog;
  1069. }