bpf_jit_comp.c 29 KB

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