bpf_jit_comp.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161
  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 48
  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 the tail call counter (tail_call_cnt): for eBPF tail calls
  200. * we need to reset the counter to 0. It's done in two instructions,
  201. * resetting rax register to 0 (xor on eax gets 0 extended), and
  202. * moving it to the counter location.
  203. */
  204. /* xor eax, eax */
  205. EMIT2(0x31, 0xc0);
  206. /* mov qword ptr [rbp-X], rax */
  207. EMIT3_off32(0x48, 0x89, 0x85, -STACKSIZE + 32);
  208. BUILD_BUG_ON(cnt != PROLOGUE_SIZE);
  209. *pprog = prog;
  210. }
  211. /* generate the following code:
  212. * ... bpf_tail_call(void *ctx, struct bpf_array *array, u64 index) ...
  213. * if (index >= array->map.max_entries)
  214. * goto out;
  215. * if (++tail_call_cnt > MAX_TAIL_CALL_CNT)
  216. * goto out;
  217. * prog = array->ptrs[index];
  218. * if (prog == NULL)
  219. * goto out;
  220. * goto *(prog->bpf_func + prologue_size);
  221. * out:
  222. */
  223. static void emit_bpf_tail_call(u8 **pprog)
  224. {
  225. u8 *prog = *pprog;
  226. int label1, label2, label3;
  227. int cnt = 0;
  228. /* rdi - pointer to ctx
  229. * rsi - pointer to bpf_array
  230. * rdx - index in bpf_array
  231. */
  232. /* if (index >= array->map.max_entries)
  233. * goto out;
  234. */
  235. EMIT4(0x48, 0x8B, 0x46, /* mov rax, qword ptr [rsi + 16] */
  236. offsetof(struct bpf_array, map.max_entries));
  237. EMIT3(0x48, 0x39, 0xD0); /* cmp rax, rdx */
  238. #define OFFSET1 47 /* number of bytes to jump */
  239. EMIT2(X86_JBE, OFFSET1); /* jbe out */
  240. label1 = cnt;
  241. /* if (tail_call_cnt > MAX_TAIL_CALL_CNT)
  242. * goto out;
  243. */
  244. EMIT2_off32(0x8B, 0x85, -STACKSIZE + 36); /* mov eax, dword ptr [rbp - 516] */
  245. EMIT3(0x83, 0xF8, MAX_TAIL_CALL_CNT); /* cmp eax, MAX_TAIL_CALL_CNT */
  246. #define OFFSET2 36
  247. EMIT2(X86_JA, OFFSET2); /* ja out */
  248. label2 = cnt;
  249. EMIT3(0x83, 0xC0, 0x01); /* add eax, 1 */
  250. EMIT2_off32(0x89, 0x85, -STACKSIZE + 36); /* mov dword ptr [rbp - 516], eax */
  251. /* prog = array->ptrs[index]; */
  252. EMIT4_off32(0x48, 0x8D, 0x84, 0xD6, /* lea rax, [rsi + rdx * 8 + offsetof(...)] */
  253. offsetof(struct bpf_array, ptrs));
  254. EMIT3(0x48, 0x8B, 0x00); /* mov rax, qword ptr [rax] */
  255. /* if (prog == NULL)
  256. * goto out;
  257. */
  258. EMIT4(0x48, 0x83, 0xF8, 0x00); /* cmp rax, 0 */
  259. #define OFFSET3 10
  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. /* now we're ready to jump into next BPF program
  267. * rdi == ctx (1st arg)
  268. * rax == prog->bpf_func + prologue_size
  269. */
  270. EMIT2(0xFF, 0xE0); /* jmp rax */
  271. /* out: */
  272. BUILD_BUG_ON(cnt - label1 != OFFSET1);
  273. BUILD_BUG_ON(cnt - label2 != OFFSET2);
  274. BUILD_BUG_ON(cnt - label3 != OFFSET3);
  275. *pprog = prog;
  276. }
  277. static void emit_load_skb_data_hlen(u8 **pprog)
  278. {
  279. u8 *prog = *pprog;
  280. int cnt = 0;
  281. /* r9d = skb->len - skb->data_len (headlen)
  282. * r10 = skb->data
  283. */
  284. /* mov %r9d, off32(%rdi) */
  285. EMIT3_off32(0x44, 0x8b, 0x8f, offsetof(struct sk_buff, len));
  286. /* sub %r9d, off32(%rdi) */
  287. EMIT3_off32(0x44, 0x2b, 0x8f, offsetof(struct sk_buff, data_len));
  288. /* mov %r10, off32(%rdi) */
  289. EMIT3_off32(0x4c, 0x8b, 0x97, offsetof(struct sk_buff, data));
  290. *pprog = prog;
  291. }
  292. static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image,
  293. int oldproglen, struct jit_context *ctx)
  294. {
  295. struct bpf_insn *insn = bpf_prog->insnsi;
  296. int insn_cnt = bpf_prog->len;
  297. bool seen_ld_abs = ctx->seen_ld_abs | (oldproglen == 0);
  298. bool seen_exit = false;
  299. u8 temp[BPF_MAX_INSN_SIZE + BPF_INSN_SAFETY];
  300. int i, cnt = 0;
  301. int proglen = 0;
  302. u8 *prog = temp;
  303. emit_prologue(&prog);
  304. if (seen_ld_abs)
  305. emit_load_skb_data_hlen(&prog);
  306. for (i = 0; i < insn_cnt; i++, insn++) {
  307. const s32 imm32 = insn->imm;
  308. u32 dst_reg = insn->dst_reg;
  309. u32 src_reg = insn->src_reg;
  310. u8 b1 = 0, b2 = 0, b3 = 0;
  311. s64 jmp_offset;
  312. u8 jmp_cond;
  313. bool reload_skb_data;
  314. int ilen;
  315. u8 *func;
  316. switch (insn->code) {
  317. /* ALU */
  318. case BPF_ALU | BPF_ADD | BPF_X:
  319. case BPF_ALU | BPF_SUB | BPF_X:
  320. case BPF_ALU | BPF_AND | BPF_X:
  321. case BPF_ALU | BPF_OR | BPF_X:
  322. case BPF_ALU | BPF_XOR | BPF_X:
  323. case BPF_ALU64 | BPF_ADD | BPF_X:
  324. case BPF_ALU64 | BPF_SUB | BPF_X:
  325. case BPF_ALU64 | BPF_AND | BPF_X:
  326. case BPF_ALU64 | BPF_OR | BPF_X:
  327. case BPF_ALU64 | BPF_XOR | BPF_X:
  328. switch (BPF_OP(insn->code)) {
  329. case BPF_ADD: b2 = 0x01; break;
  330. case BPF_SUB: b2 = 0x29; break;
  331. case BPF_AND: b2 = 0x21; break;
  332. case BPF_OR: b2 = 0x09; break;
  333. case BPF_XOR: b2 = 0x31; break;
  334. }
  335. if (BPF_CLASS(insn->code) == BPF_ALU64)
  336. EMIT1(add_2mod(0x48, dst_reg, src_reg));
  337. else if (is_ereg(dst_reg) || is_ereg(src_reg))
  338. EMIT1(add_2mod(0x40, dst_reg, src_reg));
  339. EMIT2(b2, add_2reg(0xC0, dst_reg, src_reg));
  340. break;
  341. /* mov dst, src */
  342. case BPF_ALU64 | BPF_MOV | BPF_X:
  343. EMIT_mov(dst_reg, src_reg);
  344. break;
  345. /* mov32 dst, src */
  346. case BPF_ALU | BPF_MOV | BPF_X:
  347. if (is_ereg(dst_reg) || is_ereg(src_reg))
  348. EMIT1(add_2mod(0x40, dst_reg, src_reg));
  349. EMIT2(0x89, add_2reg(0xC0, dst_reg, src_reg));
  350. break;
  351. /* neg dst */
  352. case BPF_ALU | BPF_NEG:
  353. case BPF_ALU64 | BPF_NEG:
  354. if (BPF_CLASS(insn->code) == BPF_ALU64)
  355. EMIT1(add_1mod(0x48, dst_reg));
  356. else if (is_ereg(dst_reg))
  357. EMIT1(add_1mod(0x40, dst_reg));
  358. EMIT2(0xF7, add_1reg(0xD8, dst_reg));
  359. break;
  360. case BPF_ALU | BPF_ADD | BPF_K:
  361. case BPF_ALU | BPF_SUB | BPF_K:
  362. case BPF_ALU | BPF_AND | BPF_K:
  363. case BPF_ALU | BPF_OR | BPF_K:
  364. case BPF_ALU | BPF_XOR | BPF_K:
  365. case BPF_ALU64 | BPF_ADD | BPF_K:
  366. case BPF_ALU64 | BPF_SUB | BPF_K:
  367. case BPF_ALU64 | BPF_AND | BPF_K:
  368. case BPF_ALU64 | BPF_OR | BPF_K:
  369. case BPF_ALU64 | BPF_XOR | BPF_K:
  370. if (BPF_CLASS(insn->code) == BPF_ALU64)
  371. EMIT1(add_1mod(0x48, dst_reg));
  372. else if (is_ereg(dst_reg))
  373. EMIT1(add_1mod(0x40, dst_reg));
  374. switch (BPF_OP(insn->code)) {
  375. case BPF_ADD: b3 = 0xC0; break;
  376. case BPF_SUB: b3 = 0xE8; break;
  377. case BPF_AND: b3 = 0xE0; break;
  378. case BPF_OR: b3 = 0xC8; break;
  379. case BPF_XOR: b3 = 0xF0; break;
  380. }
  381. if (is_imm8(imm32))
  382. EMIT3(0x83, add_1reg(b3, dst_reg), imm32);
  383. else
  384. EMIT2_off32(0x81, add_1reg(b3, dst_reg), imm32);
  385. break;
  386. case BPF_ALU64 | BPF_MOV | BPF_K:
  387. /* optimization: if imm32 is positive,
  388. * use 'mov eax, imm32' (which zero-extends imm32)
  389. * to save 2 bytes
  390. */
  391. if (imm32 < 0) {
  392. /* 'mov rax, imm32' sign extends imm32 */
  393. b1 = add_1mod(0x48, dst_reg);
  394. b2 = 0xC7;
  395. b3 = 0xC0;
  396. EMIT3_off32(b1, b2, add_1reg(b3, dst_reg), imm32);
  397. break;
  398. }
  399. case BPF_ALU | BPF_MOV | BPF_K:
  400. /* optimization: if imm32 is zero, use 'xor <dst>,<dst>'
  401. * to save 3 bytes.
  402. */
  403. if (imm32 == 0) {
  404. if (is_ereg(dst_reg))
  405. EMIT1(add_2mod(0x40, dst_reg, dst_reg));
  406. b2 = 0x31; /* xor */
  407. b3 = 0xC0;
  408. EMIT2(b2, add_2reg(b3, dst_reg, dst_reg));
  409. break;
  410. }
  411. /* mov %eax, imm32 */
  412. if (is_ereg(dst_reg))
  413. EMIT1(add_1mod(0x40, dst_reg));
  414. EMIT1_off32(add_1reg(0xB8, dst_reg), imm32);
  415. break;
  416. case BPF_LD | BPF_IMM | BPF_DW:
  417. if (insn[1].code != 0 || insn[1].src_reg != 0 ||
  418. insn[1].dst_reg != 0 || insn[1].off != 0) {
  419. /* verifier must catch invalid insns */
  420. pr_err("invalid BPF_LD_IMM64 insn\n");
  421. return -EINVAL;
  422. }
  423. /* optimization: if imm64 is zero, use 'xor <dst>,<dst>'
  424. * to save 7 bytes.
  425. */
  426. if (insn[0].imm == 0 && insn[1].imm == 0) {
  427. b1 = add_2mod(0x48, dst_reg, dst_reg);
  428. b2 = 0x31; /* xor */
  429. b3 = 0xC0;
  430. EMIT3(b1, b2, add_2reg(b3, dst_reg, dst_reg));
  431. insn++;
  432. i++;
  433. break;
  434. }
  435. /* movabsq %rax, imm64 */
  436. EMIT2(add_1mod(0x48, dst_reg), add_1reg(0xB8, dst_reg));
  437. EMIT(insn[0].imm, 4);
  438. EMIT(insn[1].imm, 4);
  439. insn++;
  440. i++;
  441. break;
  442. /* dst %= src, dst /= src, dst %= imm32, dst /= imm32 */
  443. case BPF_ALU | BPF_MOD | BPF_X:
  444. case BPF_ALU | BPF_DIV | BPF_X:
  445. case BPF_ALU | BPF_MOD | BPF_K:
  446. case BPF_ALU | BPF_DIV | BPF_K:
  447. case BPF_ALU64 | BPF_MOD | BPF_X:
  448. case BPF_ALU64 | BPF_DIV | BPF_X:
  449. case BPF_ALU64 | BPF_MOD | BPF_K:
  450. case BPF_ALU64 | BPF_DIV | BPF_K:
  451. EMIT1(0x50); /* push rax */
  452. EMIT1(0x52); /* push rdx */
  453. if (BPF_SRC(insn->code) == BPF_X)
  454. /* mov r11, src_reg */
  455. EMIT_mov(AUX_REG, src_reg);
  456. else
  457. /* mov r11, imm32 */
  458. EMIT3_off32(0x49, 0xC7, 0xC3, imm32);
  459. /* mov rax, dst_reg */
  460. EMIT_mov(BPF_REG_0, dst_reg);
  461. /* xor edx, edx
  462. * equivalent to 'xor rdx, rdx', but one byte less
  463. */
  464. EMIT2(0x31, 0xd2);
  465. if (BPF_SRC(insn->code) == BPF_X) {
  466. /* if (src_reg == 0) return 0 */
  467. /* cmp r11, 0 */
  468. EMIT4(0x49, 0x83, 0xFB, 0x00);
  469. /* jne .+9 (skip over pop, pop, xor and jmp) */
  470. EMIT2(X86_JNE, 1 + 1 + 2 + 5);
  471. EMIT1(0x5A); /* pop rdx */
  472. EMIT1(0x58); /* pop rax */
  473. EMIT2(0x31, 0xc0); /* xor eax, eax */
  474. /* jmp cleanup_addr
  475. * addrs[i] - 11, because there are 11 bytes
  476. * after this insn: div, mov, pop, pop, mov
  477. */
  478. jmp_offset = ctx->cleanup_addr - (addrs[i] - 11);
  479. EMIT1_off32(0xE9, jmp_offset);
  480. }
  481. if (BPF_CLASS(insn->code) == BPF_ALU64)
  482. /* div r11 */
  483. EMIT3(0x49, 0xF7, 0xF3);
  484. else
  485. /* div r11d */
  486. EMIT3(0x41, 0xF7, 0xF3);
  487. if (BPF_OP(insn->code) == BPF_MOD)
  488. /* mov r11, rdx */
  489. EMIT3(0x49, 0x89, 0xD3);
  490. else
  491. /* mov r11, rax */
  492. EMIT3(0x49, 0x89, 0xC3);
  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. case BPF_ALU | BPF_MUL | BPF_K:
  499. case BPF_ALU | BPF_MUL | BPF_X:
  500. case BPF_ALU64 | BPF_MUL | BPF_K:
  501. case BPF_ALU64 | BPF_MUL | BPF_X:
  502. EMIT1(0x50); /* push rax */
  503. EMIT1(0x52); /* push rdx */
  504. /* mov r11, dst_reg */
  505. EMIT_mov(AUX_REG, dst_reg);
  506. if (BPF_SRC(insn->code) == BPF_X)
  507. /* mov rax, src_reg */
  508. EMIT_mov(BPF_REG_0, src_reg);
  509. else
  510. /* mov rax, imm32 */
  511. EMIT3_off32(0x48, 0xC7, 0xC0, imm32);
  512. if (BPF_CLASS(insn->code) == BPF_ALU64)
  513. EMIT1(add_1mod(0x48, AUX_REG));
  514. else if (is_ereg(AUX_REG))
  515. EMIT1(add_1mod(0x40, AUX_REG));
  516. /* mul(q) r11 */
  517. EMIT2(0xF7, add_1reg(0xE0, AUX_REG));
  518. /* mov r11, rax */
  519. EMIT_mov(AUX_REG, BPF_REG_0);
  520. EMIT1(0x5A); /* pop rdx */
  521. EMIT1(0x58); /* pop rax */
  522. /* mov dst_reg, r11 */
  523. EMIT_mov(dst_reg, AUX_REG);
  524. break;
  525. /* shifts */
  526. case BPF_ALU | BPF_LSH | BPF_K:
  527. case BPF_ALU | BPF_RSH | BPF_K:
  528. case BPF_ALU | BPF_ARSH | BPF_K:
  529. case BPF_ALU64 | BPF_LSH | BPF_K:
  530. case BPF_ALU64 | BPF_RSH | BPF_K:
  531. case BPF_ALU64 | BPF_ARSH | BPF_K:
  532. if (BPF_CLASS(insn->code) == BPF_ALU64)
  533. EMIT1(add_1mod(0x48, dst_reg));
  534. else if (is_ereg(dst_reg))
  535. EMIT1(add_1mod(0x40, dst_reg));
  536. switch (BPF_OP(insn->code)) {
  537. case BPF_LSH: b3 = 0xE0; break;
  538. case BPF_RSH: b3 = 0xE8; break;
  539. case BPF_ARSH: b3 = 0xF8; break;
  540. }
  541. EMIT3(0xC1, add_1reg(b3, dst_reg), imm32);
  542. break;
  543. case BPF_ALU | BPF_LSH | BPF_X:
  544. case BPF_ALU | BPF_RSH | BPF_X:
  545. case BPF_ALU | BPF_ARSH | BPF_X:
  546. case BPF_ALU64 | BPF_LSH | BPF_X:
  547. case BPF_ALU64 | BPF_RSH | BPF_X:
  548. case BPF_ALU64 | BPF_ARSH | BPF_X:
  549. /* check for bad case when dst_reg == rcx */
  550. if (dst_reg == BPF_REG_4) {
  551. /* mov r11, dst_reg */
  552. EMIT_mov(AUX_REG, dst_reg);
  553. dst_reg = AUX_REG;
  554. }
  555. if (src_reg != BPF_REG_4) { /* common case */
  556. EMIT1(0x51); /* push rcx */
  557. /* mov rcx, src_reg */
  558. EMIT_mov(BPF_REG_4, src_reg);
  559. }
  560. /* shl %rax, %cl | shr %rax, %cl | sar %rax, %cl */
  561. if (BPF_CLASS(insn->code) == BPF_ALU64)
  562. EMIT1(add_1mod(0x48, dst_reg));
  563. else if (is_ereg(dst_reg))
  564. EMIT1(add_1mod(0x40, dst_reg));
  565. switch (BPF_OP(insn->code)) {
  566. case BPF_LSH: b3 = 0xE0; break;
  567. case BPF_RSH: b3 = 0xE8; break;
  568. case BPF_ARSH: b3 = 0xF8; break;
  569. }
  570. EMIT2(0xD3, add_1reg(b3, dst_reg));
  571. if (src_reg != BPF_REG_4)
  572. EMIT1(0x59); /* pop rcx */
  573. if (insn->dst_reg == BPF_REG_4)
  574. /* mov dst_reg, r11 */
  575. EMIT_mov(insn->dst_reg, AUX_REG);
  576. break;
  577. case BPF_ALU | BPF_END | BPF_FROM_BE:
  578. switch (imm32) {
  579. case 16:
  580. /* emit 'ror %ax, 8' to swap lower 2 bytes */
  581. EMIT1(0x66);
  582. if (is_ereg(dst_reg))
  583. EMIT1(0x41);
  584. EMIT3(0xC1, add_1reg(0xC8, dst_reg), 8);
  585. /* emit 'movzwl eax, ax' */
  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 'bswap eax' to swap lower 4 bytes */
  594. if (is_ereg(dst_reg))
  595. EMIT2(0x41, 0x0F);
  596. else
  597. EMIT1(0x0F);
  598. EMIT1(add_1reg(0xC8, dst_reg));
  599. break;
  600. case 64:
  601. /* emit 'bswap rax' to swap 8 bytes */
  602. EMIT3(add_1mod(0x48, dst_reg), 0x0F,
  603. add_1reg(0xC8, dst_reg));
  604. break;
  605. }
  606. break;
  607. case BPF_ALU | BPF_END | BPF_FROM_LE:
  608. switch (imm32) {
  609. case 16:
  610. /* emit 'movzwl eax, ax' to zero extend 16-bit
  611. * into 64 bit
  612. */
  613. if (is_ereg(dst_reg))
  614. EMIT3(0x45, 0x0F, 0xB7);
  615. else
  616. EMIT2(0x0F, 0xB7);
  617. EMIT1(add_2reg(0xC0, dst_reg, dst_reg));
  618. break;
  619. case 32:
  620. /* emit 'mov eax, eax' to clear upper 32-bits */
  621. if (is_ereg(dst_reg))
  622. EMIT1(0x45);
  623. EMIT2(0x89, add_2reg(0xC0, dst_reg, dst_reg));
  624. break;
  625. case 64:
  626. /* nop */
  627. break;
  628. }
  629. break;
  630. /* ST: *(u8*)(dst_reg + off) = imm */
  631. case BPF_ST | BPF_MEM | BPF_B:
  632. if (is_ereg(dst_reg))
  633. EMIT2(0x41, 0xC6);
  634. else
  635. EMIT1(0xC6);
  636. goto st;
  637. case BPF_ST | BPF_MEM | BPF_H:
  638. if (is_ereg(dst_reg))
  639. EMIT3(0x66, 0x41, 0xC7);
  640. else
  641. EMIT2(0x66, 0xC7);
  642. goto st;
  643. case BPF_ST | BPF_MEM | BPF_W:
  644. if (is_ereg(dst_reg))
  645. EMIT2(0x41, 0xC7);
  646. else
  647. EMIT1(0xC7);
  648. goto st;
  649. case BPF_ST | BPF_MEM | BPF_DW:
  650. EMIT2(add_1mod(0x48, dst_reg), 0xC7);
  651. st: if (is_imm8(insn->off))
  652. EMIT2(add_1reg(0x40, dst_reg), insn->off);
  653. else
  654. EMIT1_off32(add_1reg(0x80, dst_reg), insn->off);
  655. EMIT(imm32, bpf_size_to_x86_bytes(BPF_SIZE(insn->code)));
  656. break;
  657. /* STX: *(u8*)(dst_reg + off) = src_reg */
  658. case BPF_STX | BPF_MEM | BPF_B:
  659. /* emit 'mov byte ptr [rax + off], al' */
  660. if (is_ereg(dst_reg) || is_ereg(src_reg) ||
  661. /* have to add extra byte for x86 SIL, DIL regs */
  662. src_reg == BPF_REG_1 || src_reg == BPF_REG_2)
  663. EMIT2(add_2mod(0x40, dst_reg, src_reg), 0x88);
  664. else
  665. EMIT1(0x88);
  666. goto stx;
  667. case BPF_STX | BPF_MEM | BPF_H:
  668. if (is_ereg(dst_reg) || is_ereg(src_reg))
  669. EMIT3(0x66, add_2mod(0x40, dst_reg, src_reg), 0x89);
  670. else
  671. EMIT2(0x66, 0x89);
  672. goto stx;
  673. case BPF_STX | BPF_MEM | BPF_W:
  674. if (is_ereg(dst_reg) || is_ereg(src_reg))
  675. EMIT2(add_2mod(0x40, dst_reg, src_reg), 0x89);
  676. else
  677. EMIT1(0x89);
  678. goto stx;
  679. case BPF_STX | BPF_MEM | BPF_DW:
  680. EMIT2(add_2mod(0x48, dst_reg, src_reg), 0x89);
  681. stx: if (is_imm8(insn->off))
  682. EMIT2(add_2reg(0x40, dst_reg, src_reg), insn->off);
  683. else
  684. EMIT1_off32(add_2reg(0x80, dst_reg, src_reg),
  685. insn->off);
  686. break;
  687. /* LDX: dst_reg = *(u8*)(src_reg + off) */
  688. case BPF_LDX | BPF_MEM | BPF_B:
  689. /* emit 'movzx rax, byte ptr [rax + off]' */
  690. EMIT3(add_2mod(0x48, src_reg, dst_reg), 0x0F, 0xB6);
  691. goto ldx;
  692. case BPF_LDX | BPF_MEM | BPF_H:
  693. /* emit 'movzx rax, word ptr [rax + off]' */
  694. EMIT3(add_2mod(0x48, src_reg, dst_reg), 0x0F, 0xB7);
  695. goto ldx;
  696. case BPF_LDX | BPF_MEM | BPF_W:
  697. /* emit 'mov eax, dword ptr [rax+0x14]' */
  698. if (is_ereg(dst_reg) || is_ereg(src_reg))
  699. EMIT2(add_2mod(0x40, src_reg, dst_reg), 0x8B);
  700. else
  701. EMIT1(0x8B);
  702. goto ldx;
  703. case BPF_LDX | BPF_MEM | BPF_DW:
  704. /* emit 'mov rax, qword ptr [rax+0x14]' */
  705. EMIT2(add_2mod(0x48, src_reg, dst_reg), 0x8B);
  706. ldx: /* if insn->off == 0 we can save one extra byte, but
  707. * special case of x86 r13 which always needs an offset
  708. * is not worth the hassle
  709. */
  710. if (is_imm8(insn->off))
  711. EMIT2(add_2reg(0x40, src_reg, dst_reg), insn->off);
  712. else
  713. EMIT1_off32(add_2reg(0x80, src_reg, dst_reg),
  714. insn->off);
  715. break;
  716. /* STX XADD: lock *(u32*)(dst_reg + off) += src_reg */
  717. case BPF_STX | BPF_XADD | BPF_W:
  718. /* emit 'lock add dword ptr [rax + off], eax' */
  719. if (is_ereg(dst_reg) || is_ereg(src_reg))
  720. EMIT3(0xF0, add_2mod(0x40, dst_reg, src_reg), 0x01);
  721. else
  722. EMIT2(0xF0, 0x01);
  723. goto xadd;
  724. case BPF_STX | BPF_XADD | BPF_DW:
  725. EMIT3(0xF0, add_2mod(0x48, dst_reg, src_reg), 0x01);
  726. xadd: if (is_imm8(insn->off))
  727. EMIT2(add_2reg(0x40, dst_reg, src_reg), insn->off);
  728. else
  729. EMIT1_off32(add_2reg(0x80, dst_reg, src_reg),
  730. insn->off);
  731. break;
  732. /* call */
  733. case BPF_JMP | BPF_CALL:
  734. func = (u8 *) __bpf_call_base + imm32;
  735. jmp_offset = func - (image + addrs[i]);
  736. if (seen_ld_abs) {
  737. reload_skb_data = bpf_helper_changes_skb_data(func);
  738. if (reload_skb_data) {
  739. EMIT1(0x57); /* push %rdi */
  740. jmp_offset += 22; /* pop, mov, sub, mov */
  741. } else {
  742. EMIT2(0x41, 0x52); /* push %r10 */
  743. EMIT2(0x41, 0x51); /* push %r9 */
  744. /* need to adjust jmp offset, since
  745. * pop %r9, pop %r10 take 4 bytes after call insn
  746. */
  747. jmp_offset += 4;
  748. }
  749. }
  750. if (!imm32 || !is_simm32(jmp_offset)) {
  751. pr_err("unsupported bpf func %d addr %p image %p\n",
  752. imm32, func, image);
  753. return -EINVAL;
  754. }
  755. EMIT1_off32(0xE8, jmp_offset);
  756. if (seen_ld_abs) {
  757. if (reload_skb_data) {
  758. EMIT1(0x5F); /* pop %rdi */
  759. emit_load_skb_data_hlen(&prog);
  760. } else {
  761. EMIT2(0x41, 0x59); /* pop %r9 */
  762. EMIT2(0x41, 0x5A); /* pop %r10 */
  763. }
  764. }
  765. break;
  766. case BPF_JMP | BPF_CALL | BPF_X:
  767. emit_bpf_tail_call(&prog);
  768. break;
  769. /* cond jump */
  770. case BPF_JMP | BPF_JEQ | BPF_X:
  771. case BPF_JMP | BPF_JNE | BPF_X:
  772. case BPF_JMP | BPF_JGT | BPF_X:
  773. case BPF_JMP | BPF_JGE | BPF_X:
  774. case BPF_JMP | BPF_JSGT | BPF_X:
  775. case BPF_JMP | BPF_JSGE | BPF_X:
  776. /* cmp dst_reg, src_reg */
  777. EMIT3(add_2mod(0x48, dst_reg, src_reg), 0x39,
  778. add_2reg(0xC0, dst_reg, src_reg));
  779. goto emit_cond_jmp;
  780. case BPF_JMP | BPF_JSET | BPF_X:
  781. /* test dst_reg, src_reg */
  782. EMIT3(add_2mod(0x48, dst_reg, src_reg), 0x85,
  783. add_2reg(0xC0, dst_reg, src_reg));
  784. goto emit_cond_jmp;
  785. case BPF_JMP | BPF_JSET | BPF_K:
  786. /* test dst_reg, imm32 */
  787. EMIT1(add_1mod(0x48, dst_reg));
  788. EMIT2_off32(0xF7, add_1reg(0xC0, dst_reg), imm32);
  789. goto emit_cond_jmp;
  790. case BPF_JMP | BPF_JEQ | BPF_K:
  791. case BPF_JMP | BPF_JNE | BPF_K:
  792. case BPF_JMP | BPF_JGT | BPF_K:
  793. case BPF_JMP | BPF_JGE | BPF_K:
  794. case BPF_JMP | BPF_JSGT | BPF_K:
  795. case BPF_JMP | BPF_JSGE | BPF_K:
  796. /* cmp dst_reg, imm8/32 */
  797. EMIT1(add_1mod(0x48, dst_reg));
  798. if (is_imm8(imm32))
  799. EMIT3(0x83, add_1reg(0xF8, dst_reg), imm32);
  800. else
  801. EMIT2_off32(0x81, add_1reg(0xF8, dst_reg), imm32);
  802. emit_cond_jmp: /* convert BPF opcode to x86 */
  803. switch (BPF_OP(insn->code)) {
  804. case BPF_JEQ:
  805. jmp_cond = X86_JE;
  806. break;
  807. case BPF_JSET:
  808. case BPF_JNE:
  809. jmp_cond = X86_JNE;
  810. break;
  811. case BPF_JGT:
  812. /* GT is unsigned '>', JA in x86 */
  813. jmp_cond = X86_JA;
  814. break;
  815. case BPF_JGE:
  816. /* GE is unsigned '>=', JAE in x86 */
  817. jmp_cond = X86_JAE;
  818. break;
  819. case BPF_JSGT:
  820. /* signed '>', GT in x86 */
  821. jmp_cond = X86_JG;
  822. break;
  823. case BPF_JSGE:
  824. /* signed '>=', GE in x86 */
  825. jmp_cond = X86_JGE;
  826. break;
  827. default: /* to silence gcc warning */
  828. return -EFAULT;
  829. }
  830. jmp_offset = addrs[i + insn->off] - addrs[i];
  831. if (is_imm8(jmp_offset)) {
  832. EMIT2(jmp_cond, jmp_offset);
  833. } else if (is_simm32(jmp_offset)) {
  834. EMIT2_off32(0x0F, jmp_cond + 0x10, jmp_offset);
  835. } else {
  836. pr_err("cond_jmp gen bug %llx\n", jmp_offset);
  837. return -EFAULT;
  838. }
  839. break;
  840. case BPF_JMP | BPF_JA:
  841. jmp_offset = addrs[i + insn->off] - addrs[i];
  842. if (!jmp_offset)
  843. /* optimize out nop jumps */
  844. break;
  845. emit_jmp:
  846. if (is_imm8(jmp_offset)) {
  847. EMIT2(0xEB, jmp_offset);
  848. } else if (is_simm32(jmp_offset)) {
  849. EMIT1_off32(0xE9, jmp_offset);
  850. } else {
  851. pr_err("jmp gen bug %llx\n", jmp_offset);
  852. return -EFAULT;
  853. }
  854. break;
  855. case BPF_LD | BPF_IND | BPF_W:
  856. func = sk_load_word;
  857. goto common_load;
  858. case BPF_LD | BPF_ABS | BPF_W:
  859. func = CHOOSE_LOAD_FUNC(imm32, sk_load_word);
  860. common_load:
  861. ctx->seen_ld_abs = seen_ld_abs = true;
  862. jmp_offset = func - (image + addrs[i]);
  863. if (!func || !is_simm32(jmp_offset)) {
  864. pr_err("unsupported bpf func %d addr %p image %p\n",
  865. imm32, func, image);
  866. return -EINVAL;
  867. }
  868. if (BPF_MODE(insn->code) == BPF_ABS) {
  869. /* mov %esi, imm32 */
  870. EMIT1_off32(0xBE, imm32);
  871. } else {
  872. /* mov %rsi, src_reg */
  873. EMIT_mov(BPF_REG_2, src_reg);
  874. if (imm32) {
  875. if (is_imm8(imm32))
  876. /* add %esi, imm8 */
  877. EMIT3(0x83, 0xC6, imm32);
  878. else
  879. /* add %esi, imm32 */
  880. EMIT2_off32(0x81, 0xC6, imm32);
  881. }
  882. }
  883. /* skb pointer is in R6 (%rbx), it will be copied into
  884. * %rdi if skb_copy_bits() call is necessary.
  885. * sk_load_* helpers also use %r10 and %r9d.
  886. * See bpf_jit.S
  887. */
  888. EMIT1_off32(0xE8, jmp_offset); /* call */
  889. break;
  890. case BPF_LD | BPF_IND | BPF_H:
  891. func = sk_load_half;
  892. goto common_load;
  893. case BPF_LD | BPF_ABS | BPF_H:
  894. func = CHOOSE_LOAD_FUNC(imm32, sk_load_half);
  895. goto common_load;
  896. case BPF_LD | BPF_IND | BPF_B:
  897. func = sk_load_byte;
  898. goto common_load;
  899. case BPF_LD | BPF_ABS | BPF_B:
  900. func = CHOOSE_LOAD_FUNC(imm32, sk_load_byte);
  901. goto common_load;
  902. case BPF_JMP | BPF_EXIT:
  903. if (seen_exit) {
  904. jmp_offset = ctx->cleanup_addr - addrs[i];
  905. goto emit_jmp;
  906. }
  907. seen_exit = true;
  908. /* update cleanup_addr */
  909. ctx->cleanup_addr = proglen;
  910. /* mov rbx, qword ptr [rbp-X] */
  911. EMIT3_off32(0x48, 0x8B, 0x9D, -STACKSIZE);
  912. /* mov r13, qword ptr [rbp-X] */
  913. EMIT3_off32(0x4C, 0x8B, 0xAD, -STACKSIZE + 8);
  914. /* mov r14, qword ptr [rbp-X] */
  915. EMIT3_off32(0x4C, 0x8B, 0xB5, -STACKSIZE + 16);
  916. /* mov r15, qword ptr [rbp-X] */
  917. EMIT3_off32(0x4C, 0x8B, 0xBD, -STACKSIZE + 24);
  918. EMIT1(0xC9); /* leave */
  919. EMIT1(0xC3); /* ret */
  920. break;
  921. default:
  922. /* By design x64 JIT should support all BPF instructions
  923. * This error will be seen if new instruction was added
  924. * to interpreter, but not to JIT
  925. * or if there is junk in bpf_prog
  926. */
  927. pr_err("bpf_jit: unknown opcode %02x\n", insn->code);
  928. return -EINVAL;
  929. }
  930. ilen = prog - temp;
  931. if (ilen > BPF_MAX_INSN_SIZE) {
  932. pr_err("bpf_jit_compile fatal insn size error\n");
  933. return -EFAULT;
  934. }
  935. if (image) {
  936. if (unlikely(proglen + ilen > oldproglen)) {
  937. pr_err("bpf_jit_compile fatal error\n");
  938. return -EFAULT;
  939. }
  940. memcpy(image + proglen, temp, ilen);
  941. }
  942. proglen += ilen;
  943. addrs[i] = proglen;
  944. prog = temp;
  945. }
  946. return proglen;
  947. }
  948. void bpf_jit_compile(struct bpf_prog *prog)
  949. {
  950. }
  951. void bpf_int_jit_compile(struct bpf_prog *prog)
  952. {
  953. struct bpf_binary_header *header = NULL;
  954. int proglen, oldproglen = 0;
  955. struct jit_context ctx = {};
  956. u8 *image = NULL;
  957. int *addrs;
  958. int pass;
  959. int i;
  960. if (!bpf_jit_enable)
  961. return;
  962. if (!prog || !prog->len)
  963. return;
  964. addrs = kmalloc(prog->len * sizeof(*addrs), GFP_KERNEL);
  965. if (!addrs)
  966. return;
  967. /* Before first pass, make a rough estimation of addrs[]
  968. * each bpf instruction is translated to less than 64 bytes
  969. */
  970. for (proglen = 0, i = 0; i < prog->len; i++) {
  971. proglen += 64;
  972. addrs[i] = proglen;
  973. }
  974. ctx.cleanup_addr = proglen;
  975. /* JITed image shrinks with every pass and the loop iterates
  976. * until the image stops shrinking. Very large bpf programs
  977. * may converge on the last pass. In such case do one more
  978. * pass to emit the final image
  979. */
  980. for (pass = 0; pass < 10 || image; pass++) {
  981. proglen = do_jit(prog, addrs, image, oldproglen, &ctx);
  982. if (proglen <= 0) {
  983. image = NULL;
  984. if (header)
  985. bpf_jit_binary_free(header);
  986. goto out;
  987. }
  988. if (image) {
  989. if (proglen != oldproglen) {
  990. pr_err("bpf_jit: proglen=%d != oldproglen=%d\n",
  991. proglen, oldproglen);
  992. goto out;
  993. }
  994. break;
  995. }
  996. if (proglen == oldproglen) {
  997. header = bpf_jit_binary_alloc(proglen, &image,
  998. 1, jit_fill_hole);
  999. if (!header)
  1000. goto out;
  1001. }
  1002. oldproglen = proglen;
  1003. }
  1004. if (bpf_jit_enable > 1)
  1005. bpf_jit_dump(prog->len, proglen, pass + 1, image);
  1006. if (image) {
  1007. bpf_flush_icache(header, image + proglen);
  1008. set_memory_ro((unsigned long)header, header->pages);
  1009. prog->bpf_func = (void *)image;
  1010. prog->jited = 1;
  1011. }
  1012. out:
  1013. kfree(addrs);
  1014. }
  1015. void bpf_jit_free(struct bpf_prog *fp)
  1016. {
  1017. unsigned long addr = (unsigned long)fp->bpf_func & PAGE_MASK;
  1018. struct bpf_binary_header *header = (void *)addr;
  1019. if (!fp->jited)
  1020. goto free_filter;
  1021. set_memory_rw(addr, header->pages);
  1022. bpf_jit_binary_free(header);
  1023. free_filter:
  1024. bpf_prog_unlock_free(fp);
  1025. }