bpf_jit_comp.c 32 KB

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