bpf_jit_comp.c 29 KB

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