bpf_jit_comp.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  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 <asm/nospec-branch.h>
  16. #include <linux/bpf.h>
  17. int bpf_jit_enable __read_mostly;
  18. /*
  19. * assembly code in arch/x86/net/bpf_jit.S
  20. */
  21. extern u8 sk_load_word[], sk_load_half[], sk_load_byte[];
  22. extern u8 sk_load_word_positive_offset[], sk_load_half_positive_offset[];
  23. extern u8 sk_load_byte_positive_offset[];
  24. extern u8 sk_load_word_negative_offset[], sk_load_half_negative_offset[];
  25. extern u8 sk_load_byte_negative_offset[];
  26. static u8 *emit_code(u8 *ptr, u32 bytes, unsigned int len)
  27. {
  28. if (len == 1)
  29. *ptr = bytes;
  30. else if (len == 2)
  31. *(u16 *)ptr = bytes;
  32. else {
  33. *(u32 *)ptr = bytes;
  34. barrier();
  35. }
  36. return ptr + len;
  37. }
  38. #define EMIT(bytes, len) \
  39. do { prog = emit_code(prog, bytes, len); cnt += len; } while (0)
  40. #define EMIT1(b1) EMIT(b1, 1)
  41. #define EMIT2(b1, b2) EMIT((b1) + ((b2) << 8), 2)
  42. #define EMIT3(b1, b2, b3) EMIT((b1) + ((b2) << 8) + ((b3) << 16), 3)
  43. #define EMIT4(b1, b2, b3, b4) EMIT((b1) + ((b2) << 8) + ((b3) << 16) + ((b4) << 24), 4)
  44. #define EMIT1_off32(b1, off) \
  45. do {EMIT1(b1); EMIT(off, 4); } while (0)
  46. #define EMIT2_off32(b1, b2, off) \
  47. do {EMIT2(b1, b2); EMIT(off, 4); } while (0)
  48. #define EMIT3_off32(b1, b2, b3, off) \
  49. do {EMIT3(b1, b2, b3); EMIT(off, 4); } while (0)
  50. #define EMIT4_off32(b1, b2, b3, b4, off) \
  51. do {EMIT4(b1, b2, b3, b4); EMIT(off, 4); } while (0)
  52. static bool is_imm8(int value)
  53. {
  54. return value <= 127 && value >= -128;
  55. }
  56. static bool is_simm32(s64 value)
  57. {
  58. return value == (s64) (s32) value;
  59. }
  60. /* mov dst, src */
  61. #define EMIT_mov(DST, SRC) \
  62. do {if (DST != SRC) \
  63. EMIT3(add_2mod(0x48, DST, SRC), 0x89, add_2reg(0xC0, DST, SRC)); \
  64. } while (0)
  65. static int bpf_size_to_x86_bytes(int bpf_size)
  66. {
  67. if (bpf_size == BPF_W)
  68. return 4;
  69. else if (bpf_size == BPF_H)
  70. return 2;
  71. else if (bpf_size == BPF_B)
  72. return 1;
  73. else if (bpf_size == BPF_DW)
  74. return 4; /* imm32 */
  75. else
  76. return 0;
  77. }
  78. /* list of x86 cond jumps opcodes (. + s8)
  79. * Add 0x10 (and an extra 0x0f) to generate far jumps (. + s32)
  80. */
  81. #define X86_JB 0x72
  82. #define X86_JAE 0x73
  83. #define X86_JE 0x74
  84. #define X86_JNE 0x75
  85. #define X86_JBE 0x76
  86. #define X86_JA 0x77
  87. #define X86_JGE 0x7D
  88. #define X86_JG 0x7F
  89. static void bpf_flush_icache(void *start, void *end)
  90. {
  91. mm_segment_t old_fs = get_fs();
  92. set_fs(KERNEL_DS);
  93. smp_wmb();
  94. flush_icache_range((unsigned long)start, (unsigned long)end);
  95. set_fs(old_fs);
  96. }
  97. #define CHOOSE_LOAD_FUNC(K, func) \
  98. ((int)K < 0 ? ((int)K >= SKF_LL_OFF ? func##_negative_offset : func) : func##_positive_offset)
  99. /* pick a register outside of BPF range for JIT internal work */
  100. #define AUX_REG (MAX_BPF_REG + 1)
  101. /* the following table maps BPF registers to x64 registers.
  102. * x64 register r12 is unused, since if used as base address register
  103. * in load/store instructions, it always needs an extra byte of encoding
  104. */
  105. static const int reg2hex[] = {
  106. [BPF_REG_0] = 0, /* rax */
  107. [BPF_REG_1] = 7, /* rdi */
  108. [BPF_REG_2] = 6, /* rsi */
  109. [BPF_REG_3] = 2, /* rdx */
  110. [BPF_REG_4] = 1, /* rcx */
  111. [BPF_REG_5] = 0, /* r8 */
  112. [BPF_REG_6] = 3, /* rbx callee saved */
  113. [BPF_REG_7] = 5, /* r13 callee saved */
  114. [BPF_REG_8] = 6, /* r14 callee saved */
  115. [BPF_REG_9] = 7, /* r15 callee saved */
  116. [BPF_REG_FP] = 5, /* rbp readonly */
  117. [AUX_REG] = 3, /* r11 temp register */
  118. };
  119. /* is_ereg() == true if BPF register 'reg' maps to x64 r8..r15
  120. * which need extra byte of encoding.
  121. * rax,rcx,...,rbp have simpler encoding
  122. */
  123. static bool is_ereg(u32 reg)
  124. {
  125. return (1 << reg) & (BIT(BPF_REG_5) |
  126. BIT(AUX_REG) |
  127. BIT(BPF_REG_7) |
  128. BIT(BPF_REG_8) |
  129. BIT(BPF_REG_9));
  130. }
  131. /* add modifiers if 'reg' maps to x64 registers r8..r15 */
  132. static u8 add_1mod(u8 byte, u32 reg)
  133. {
  134. if (is_ereg(reg))
  135. byte |= 1;
  136. return byte;
  137. }
  138. static u8 add_2mod(u8 byte, u32 r1, u32 r2)
  139. {
  140. if (is_ereg(r1))
  141. byte |= 1;
  142. if (is_ereg(r2))
  143. byte |= 4;
  144. return byte;
  145. }
  146. /* encode 'dst_reg' register into x64 opcode 'byte' */
  147. static u8 add_1reg(u8 byte, u32 dst_reg)
  148. {
  149. return byte + reg2hex[dst_reg];
  150. }
  151. /* encode 'dst_reg' and 'src_reg' registers into x64 opcode 'byte' */
  152. static u8 add_2reg(u8 byte, u32 dst_reg, u32 src_reg)
  153. {
  154. return byte + reg2hex[dst_reg] + (reg2hex[src_reg] << 3);
  155. }
  156. static void jit_fill_hole(void *area, unsigned int size)
  157. {
  158. /* fill whole space with int3 instructions */
  159. memset(area, 0xcc, size);
  160. }
  161. struct jit_context {
  162. int cleanup_addr; /* epilogue code offset */
  163. bool seen_ld_abs;
  164. };
  165. /* maximum number of bytes emitted while JITing one eBPF insn */
  166. #define BPF_MAX_INSN_SIZE 128
  167. #define BPF_INSN_SAFETY 64
  168. #define STACKSIZE \
  169. (MAX_BPF_STACK + \
  170. 32 /* space for rbx, r13, r14, r15 */ + \
  171. 8 /* space for skb_copy_bits() buffer */)
  172. #define PROLOGUE_SIZE 51
  173. /* emit x64 prologue code for BPF program and check it's size.
  174. * bpf_tail_call helper will skip it while jumping into another program
  175. */
  176. static void emit_prologue(u8 **pprog)
  177. {
  178. u8 *prog = *pprog;
  179. int cnt = 0;
  180. EMIT1(0x55); /* push rbp */
  181. EMIT3(0x48, 0x89, 0xE5); /* mov rbp,rsp */
  182. /* sub rsp, STACKSIZE */
  183. EMIT3_off32(0x48, 0x81, 0xEC, STACKSIZE);
  184. /* all classic BPF filters use R6(rbx) save it */
  185. /* mov qword ptr [rbp-X],rbx */
  186. EMIT3_off32(0x48, 0x89, 0x9D, -STACKSIZE);
  187. /* bpf_convert_filter() maps classic BPF register X to R7 and uses R8
  188. * as temporary, so all tcpdump filters need to spill/fill R7(r13) and
  189. * R8(r14). R9(r15) spill could be made conditional, but there is only
  190. * one 'bpf_error' return path out of helper functions inside bpf_jit.S
  191. * The overhead of extra spill is negligible for any filter other
  192. * than synthetic ones. Therefore not worth adding complexity.
  193. */
  194. /* mov qword ptr [rbp-X],r13 */
  195. EMIT3_off32(0x4C, 0x89, 0xAD, -STACKSIZE + 8);
  196. /* mov qword ptr [rbp-X],r14 */
  197. EMIT3_off32(0x4C, 0x89, 0xB5, -STACKSIZE + 16);
  198. /* mov qword ptr [rbp-X],r15 */
  199. EMIT3_off32(0x4C, 0x89, 0xBD, -STACKSIZE + 24);
  200. /* clear A and X registers */
  201. EMIT2(0x31, 0xc0); /* xor eax, eax */
  202. EMIT3(0x4D, 0x31, 0xED); /* xor r13, r13 */
  203. /* clear tail_cnt: mov qword ptr [rbp-X], rax */
  204. EMIT3_off32(0x48, 0x89, 0x85, -STACKSIZE + 32);
  205. BUILD_BUG_ON(cnt != PROLOGUE_SIZE);
  206. *pprog = prog;
  207. }
  208. /* generate the following code:
  209. * ... bpf_tail_call(void *ctx, struct bpf_array *array, u64 index) ...
  210. * if (index >= array->map.max_entries)
  211. * goto out;
  212. * if (++tail_call_cnt > MAX_TAIL_CALL_CNT)
  213. * goto out;
  214. * prog = array->ptrs[index];
  215. * if (prog == NULL)
  216. * goto out;
  217. * goto *(prog->bpf_func + prologue_size);
  218. * out:
  219. */
  220. static void emit_bpf_tail_call(u8 **pprog)
  221. {
  222. u8 *prog = *pprog;
  223. int label1, label2, label3;
  224. int cnt = 0;
  225. /* rdi - pointer to ctx
  226. * rsi - pointer to bpf_array
  227. * rdx - index in bpf_array
  228. */
  229. /* if (index >= array->map.max_entries)
  230. * goto out;
  231. */
  232. EMIT2(0x89, 0xD2); /* mov edx, edx */
  233. EMIT3(0x39, 0x56, /* cmp dword ptr [rsi + 16], edx */
  234. offsetof(struct bpf_array, map.max_entries));
  235. #define OFFSET1 (41 + RETPOLINE_RAX_BPF_JIT_SIZE) /* number of bytes to jump */
  236. EMIT2(X86_JBE, OFFSET1); /* jbe out */
  237. label1 = cnt;
  238. /* if (tail_call_cnt > MAX_TAIL_CALL_CNT)
  239. * goto out;
  240. */
  241. EMIT2_off32(0x8B, 0x85, -STACKSIZE + 36); /* mov eax, dword ptr [rbp - 516] */
  242. EMIT3(0x83, 0xF8, MAX_TAIL_CALL_CNT); /* cmp eax, MAX_TAIL_CALL_CNT */
  243. #define OFFSET2 (30 + RETPOLINE_RAX_BPF_JIT_SIZE)
  244. EMIT2(X86_JA, OFFSET2); /* ja out */
  245. label2 = cnt;
  246. EMIT3(0x83, 0xC0, 0x01); /* add eax, 1 */
  247. EMIT2_off32(0x89, 0x85, -STACKSIZE + 36); /* mov dword ptr [rbp - 516], eax */
  248. /* prog = array->ptrs[index]; */
  249. EMIT4_off32(0x48, 0x8B, 0x84, 0xD6, /* mov rax, [rsi + rdx * 8 + offsetof(...)] */
  250. offsetof(struct bpf_array, ptrs));
  251. /* if (prog == NULL)
  252. * goto out;
  253. */
  254. EMIT3(0x48, 0x85, 0xC0); /* test rax,rax */
  255. #define OFFSET3 (8 + RETPOLINE_RAX_BPF_JIT_SIZE)
  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. RETPOLINE_RAX_BPF_JIT();
  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 < 20 || 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. cond_resched();
  977. }
  978. if (bpf_jit_enable > 1)
  979. bpf_jit_dump(prog->len, proglen, pass + 1, image);
  980. if (image) {
  981. bpf_flush_icache(header, image + proglen);
  982. set_memory_ro((unsigned long)header, header->pages);
  983. prog->bpf_func = (void *)image;
  984. prog->jited = 1;
  985. }
  986. out:
  987. kfree(addrs);
  988. }
  989. void bpf_jit_free(struct bpf_prog *fp)
  990. {
  991. unsigned long addr = (unsigned long)fp->bpf_func & PAGE_MASK;
  992. struct bpf_binary_header *header = (void *)addr;
  993. if (!fp->jited)
  994. goto free_filter;
  995. set_memory_rw(addr, header->pages);
  996. bpf_jit_binary_free(header);
  997. free_filter:
  998. bpf_prog_unlock_free(fp);
  999. }