bpf_jit_comp.c 31 KB

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