bpf_jit_comp.c 29 KB

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