bpf_jit_comp.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  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. int bpf_jit_enable __read_mostly;
  16. /*
  17. * assembly code in arch/x86/net/bpf_jit.S
  18. */
  19. extern u8 sk_load_word[], sk_load_half[], sk_load_byte[];
  20. extern u8 sk_load_word_positive_offset[], sk_load_half_positive_offset[];
  21. extern u8 sk_load_byte_positive_offset[];
  22. extern u8 sk_load_word_negative_offset[], sk_load_half_negative_offset[];
  23. extern u8 sk_load_byte_negative_offset[];
  24. static u8 *emit_code(u8 *ptr, u32 bytes, unsigned int len)
  25. {
  26. if (len == 1)
  27. *ptr = bytes;
  28. else if (len == 2)
  29. *(u16 *)ptr = bytes;
  30. else {
  31. *(u32 *)ptr = bytes;
  32. barrier();
  33. }
  34. return ptr + len;
  35. }
  36. #define EMIT(bytes, len) do { prog = emit_code(prog, bytes, len); } while (0)
  37. #define EMIT1(b1) EMIT(b1, 1)
  38. #define EMIT2(b1, b2) EMIT((b1) + ((b2) << 8), 2)
  39. #define EMIT3(b1, b2, b3) EMIT((b1) + ((b2) << 8) + ((b3) << 16), 3)
  40. #define EMIT4(b1, b2, b3, b4) EMIT((b1) + ((b2) << 8) + ((b3) << 16) + ((b4) << 24), 4)
  41. #define EMIT1_off32(b1, off) \
  42. do {EMIT1(b1); EMIT(off, 4); } while (0)
  43. #define EMIT2_off32(b1, b2, off) \
  44. do {EMIT2(b1, b2); EMIT(off, 4); } while (0)
  45. #define EMIT3_off32(b1, b2, b3, off) \
  46. do {EMIT3(b1, b2, b3); EMIT(off, 4); } while (0)
  47. #define EMIT4_off32(b1, b2, b3, b4, off) \
  48. do {EMIT4(b1, b2, b3, b4); EMIT(off, 4); } while (0)
  49. static bool is_imm8(int value)
  50. {
  51. return value <= 127 && value >= -128;
  52. }
  53. static bool is_simm32(s64 value)
  54. {
  55. return value == (s64) (s32) value;
  56. }
  57. /* mov dst, src */
  58. #define EMIT_mov(DST, SRC) \
  59. do {if (DST != SRC) \
  60. EMIT3(add_2mod(0x48, DST, SRC), 0x89, add_2reg(0xC0, DST, SRC)); \
  61. } while (0)
  62. static int bpf_size_to_x86_bytes(int bpf_size)
  63. {
  64. if (bpf_size == BPF_W)
  65. return 4;
  66. else if (bpf_size == BPF_H)
  67. return 2;
  68. else if (bpf_size == BPF_B)
  69. return 1;
  70. else if (bpf_size == BPF_DW)
  71. return 4; /* imm32 */
  72. else
  73. return 0;
  74. }
  75. /* list of x86 cond jumps opcodes (. + s8)
  76. * Add 0x10 (and an extra 0x0f) to generate far jumps (. + s32)
  77. */
  78. #define X86_JB 0x72
  79. #define X86_JAE 0x73
  80. #define X86_JE 0x74
  81. #define X86_JNE 0x75
  82. #define X86_JBE 0x76
  83. #define X86_JA 0x77
  84. #define X86_JGE 0x7D
  85. #define X86_JG 0x7F
  86. static void bpf_flush_icache(void *start, void *end)
  87. {
  88. mm_segment_t old_fs = get_fs();
  89. set_fs(KERNEL_DS);
  90. smp_wmb();
  91. flush_icache_range((unsigned long)start, (unsigned long)end);
  92. set_fs(old_fs);
  93. }
  94. #define CHOOSE_LOAD_FUNC(K, func) \
  95. ((int)K < 0 ? ((int)K >= SKF_LL_OFF ? func##_negative_offset : func) : func##_positive_offset)
  96. /* pick a register outside of BPF range for JIT internal work */
  97. #define AUX_REG (MAX_BPF_REG + 1)
  98. /* the following table maps BPF registers to x64 registers.
  99. * x64 register r12 is unused, since if used as base address register
  100. * in load/store instructions, it always needs an extra byte of encoding
  101. */
  102. static const int reg2hex[] = {
  103. [BPF_REG_0] = 0, /* rax */
  104. [BPF_REG_1] = 7, /* rdi */
  105. [BPF_REG_2] = 6, /* rsi */
  106. [BPF_REG_3] = 2, /* rdx */
  107. [BPF_REG_4] = 1, /* rcx */
  108. [BPF_REG_5] = 0, /* r8 */
  109. [BPF_REG_6] = 3, /* rbx callee saved */
  110. [BPF_REG_7] = 5, /* r13 callee saved */
  111. [BPF_REG_8] = 6, /* r14 callee saved */
  112. [BPF_REG_9] = 7, /* r15 callee saved */
  113. [BPF_REG_FP] = 5, /* rbp readonly */
  114. [AUX_REG] = 3, /* r11 temp register */
  115. };
  116. /* is_ereg() == true if BPF register 'reg' maps to x64 r8..r15
  117. * which need extra byte of encoding.
  118. * rax,rcx,...,rbp have simpler encoding
  119. */
  120. static bool is_ereg(u32 reg)
  121. {
  122. return (1 << reg) & (BIT(BPF_REG_5) |
  123. BIT(AUX_REG) |
  124. BIT(BPF_REG_7) |
  125. BIT(BPF_REG_8) |
  126. BIT(BPF_REG_9));
  127. }
  128. /* add modifiers if 'reg' maps to x64 registers r8..r15 */
  129. static u8 add_1mod(u8 byte, u32 reg)
  130. {
  131. if (is_ereg(reg))
  132. byte |= 1;
  133. return byte;
  134. }
  135. static u8 add_2mod(u8 byte, u32 r1, u32 r2)
  136. {
  137. if (is_ereg(r1))
  138. byte |= 1;
  139. if (is_ereg(r2))
  140. byte |= 4;
  141. return byte;
  142. }
  143. /* encode 'dst_reg' register into x64 opcode 'byte' */
  144. static u8 add_1reg(u8 byte, u32 dst_reg)
  145. {
  146. return byte + reg2hex[dst_reg];
  147. }
  148. /* encode 'dst_reg' and 'src_reg' registers into x64 opcode 'byte' */
  149. static u8 add_2reg(u8 byte, u32 dst_reg, u32 src_reg)
  150. {
  151. return byte + reg2hex[dst_reg] + (reg2hex[src_reg] << 3);
  152. }
  153. static void jit_fill_hole(void *area, unsigned int size)
  154. {
  155. /* fill whole space with int3 instructions */
  156. memset(area, 0xcc, size);
  157. }
  158. struct jit_context {
  159. int cleanup_addr; /* epilogue code offset */
  160. bool seen_ld_abs;
  161. };
  162. /* maximum number of bytes emitted while JITing one eBPF insn */
  163. #define BPF_MAX_INSN_SIZE 128
  164. #define BPF_INSN_SAFETY 64
  165. static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image,
  166. int oldproglen, struct jit_context *ctx)
  167. {
  168. struct bpf_insn *insn = bpf_prog->insnsi;
  169. int insn_cnt = bpf_prog->len;
  170. bool seen_ld_abs = ctx->seen_ld_abs | (oldproglen == 0);
  171. bool seen_exit = false;
  172. u8 temp[BPF_MAX_INSN_SIZE + BPF_INSN_SAFETY];
  173. int i;
  174. int proglen = 0;
  175. u8 *prog = temp;
  176. int stacksize = MAX_BPF_STACK +
  177. 32 /* space for rbx, r13, r14, r15 */ +
  178. 8 /* space for skb_copy_bits() buffer */;
  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. if (seen_ld_abs) {
  203. /* r9d : skb->len - skb->data_len (headlen)
  204. * r10 : skb->data
  205. */
  206. if (is_imm8(offsetof(struct sk_buff, len)))
  207. /* mov %r9d, off8(%rdi) */
  208. EMIT4(0x44, 0x8b, 0x4f,
  209. offsetof(struct sk_buff, len));
  210. else
  211. /* mov %r9d, off32(%rdi) */
  212. EMIT3_off32(0x44, 0x8b, 0x8f,
  213. offsetof(struct sk_buff, len));
  214. if (is_imm8(offsetof(struct sk_buff, data_len)))
  215. /* sub %r9d, off8(%rdi) */
  216. EMIT4(0x44, 0x2b, 0x4f,
  217. offsetof(struct sk_buff, data_len));
  218. else
  219. EMIT3_off32(0x44, 0x2b, 0x8f,
  220. offsetof(struct sk_buff, data_len));
  221. if (is_imm8(offsetof(struct sk_buff, data)))
  222. /* mov %r10, off8(%rdi) */
  223. EMIT4(0x4c, 0x8b, 0x57,
  224. offsetof(struct sk_buff, data));
  225. else
  226. /* mov %r10, off32(%rdi) */
  227. EMIT3_off32(0x4c, 0x8b, 0x97,
  228. offsetof(struct sk_buff, data));
  229. }
  230. for (i = 0; i < insn_cnt; i++, insn++) {
  231. const s32 imm32 = insn->imm;
  232. u32 dst_reg = insn->dst_reg;
  233. u32 src_reg = insn->src_reg;
  234. u8 b1 = 0, b2 = 0, b3 = 0;
  235. s64 jmp_offset;
  236. u8 jmp_cond;
  237. int ilen;
  238. u8 *func;
  239. switch (insn->code) {
  240. /* ALU */
  241. case BPF_ALU | BPF_ADD | BPF_X:
  242. case BPF_ALU | BPF_SUB | BPF_X:
  243. case BPF_ALU | BPF_AND | BPF_X:
  244. case BPF_ALU | BPF_OR | BPF_X:
  245. case BPF_ALU | BPF_XOR | BPF_X:
  246. case BPF_ALU64 | BPF_ADD | BPF_X:
  247. case BPF_ALU64 | BPF_SUB | BPF_X:
  248. case BPF_ALU64 | BPF_AND | BPF_X:
  249. case BPF_ALU64 | BPF_OR | BPF_X:
  250. case BPF_ALU64 | BPF_XOR | BPF_X:
  251. switch (BPF_OP(insn->code)) {
  252. case BPF_ADD: b2 = 0x01; break;
  253. case BPF_SUB: b2 = 0x29; break;
  254. case BPF_AND: b2 = 0x21; break;
  255. case BPF_OR: b2 = 0x09; break;
  256. case BPF_XOR: b2 = 0x31; break;
  257. }
  258. if (BPF_CLASS(insn->code) == BPF_ALU64)
  259. EMIT1(add_2mod(0x48, dst_reg, src_reg));
  260. else if (is_ereg(dst_reg) || is_ereg(src_reg))
  261. EMIT1(add_2mod(0x40, dst_reg, src_reg));
  262. EMIT2(b2, add_2reg(0xC0, dst_reg, src_reg));
  263. break;
  264. /* mov dst, src */
  265. case BPF_ALU64 | BPF_MOV | BPF_X:
  266. EMIT_mov(dst_reg, src_reg);
  267. break;
  268. /* mov32 dst, src */
  269. case BPF_ALU | BPF_MOV | BPF_X:
  270. if (is_ereg(dst_reg) || is_ereg(src_reg))
  271. EMIT1(add_2mod(0x40, dst_reg, src_reg));
  272. EMIT2(0x89, add_2reg(0xC0, dst_reg, src_reg));
  273. break;
  274. /* neg dst */
  275. case BPF_ALU | BPF_NEG:
  276. case BPF_ALU64 | BPF_NEG:
  277. if (BPF_CLASS(insn->code) == BPF_ALU64)
  278. EMIT1(add_1mod(0x48, dst_reg));
  279. else if (is_ereg(dst_reg))
  280. EMIT1(add_1mod(0x40, dst_reg));
  281. EMIT2(0xF7, add_1reg(0xD8, dst_reg));
  282. break;
  283. case BPF_ALU | BPF_ADD | BPF_K:
  284. case BPF_ALU | BPF_SUB | BPF_K:
  285. case BPF_ALU | BPF_AND | BPF_K:
  286. case BPF_ALU | BPF_OR | BPF_K:
  287. case BPF_ALU | BPF_XOR | BPF_K:
  288. case BPF_ALU64 | BPF_ADD | BPF_K:
  289. case BPF_ALU64 | BPF_SUB | BPF_K:
  290. case BPF_ALU64 | BPF_AND | BPF_K:
  291. case BPF_ALU64 | BPF_OR | BPF_K:
  292. case BPF_ALU64 | BPF_XOR | BPF_K:
  293. if (BPF_CLASS(insn->code) == BPF_ALU64)
  294. EMIT1(add_1mod(0x48, dst_reg));
  295. else if (is_ereg(dst_reg))
  296. EMIT1(add_1mod(0x40, dst_reg));
  297. switch (BPF_OP(insn->code)) {
  298. case BPF_ADD: b3 = 0xC0; break;
  299. case BPF_SUB: b3 = 0xE8; break;
  300. case BPF_AND: b3 = 0xE0; break;
  301. case BPF_OR: b3 = 0xC8; break;
  302. case BPF_XOR: b3 = 0xF0; break;
  303. }
  304. if (is_imm8(imm32))
  305. EMIT3(0x83, add_1reg(b3, dst_reg), imm32);
  306. else
  307. EMIT2_off32(0x81, add_1reg(b3, dst_reg), imm32);
  308. break;
  309. case BPF_ALU64 | BPF_MOV | BPF_K:
  310. /* optimization: if imm32 is positive,
  311. * use 'mov eax, imm32' (which zero-extends imm32)
  312. * to save 2 bytes
  313. */
  314. if (imm32 < 0) {
  315. /* 'mov rax, imm32' sign extends imm32 */
  316. b1 = add_1mod(0x48, dst_reg);
  317. b2 = 0xC7;
  318. b3 = 0xC0;
  319. EMIT3_off32(b1, b2, add_1reg(b3, dst_reg), imm32);
  320. break;
  321. }
  322. case BPF_ALU | BPF_MOV | BPF_K:
  323. /* mov %eax, imm32 */
  324. if (is_ereg(dst_reg))
  325. EMIT1(add_1mod(0x40, dst_reg));
  326. EMIT1_off32(add_1reg(0xB8, dst_reg), imm32);
  327. break;
  328. case BPF_LD | BPF_IMM | BPF_DW:
  329. if (insn[1].code != 0 || insn[1].src_reg != 0 ||
  330. insn[1].dst_reg != 0 || insn[1].off != 0) {
  331. /* verifier must catch invalid insns */
  332. pr_err("invalid BPF_LD_IMM64 insn\n");
  333. return -EINVAL;
  334. }
  335. /* movabsq %rax, imm64 */
  336. EMIT2(add_1mod(0x48, dst_reg), add_1reg(0xB8, dst_reg));
  337. EMIT(insn[0].imm, 4);
  338. EMIT(insn[1].imm, 4);
  339. insn++;
  340. i++;
  341. break;
  342. /* dst %= src, dst /= src, dst %= imm32, dst /= imm32 */
  343. case BPF_ALU | BPF_MOD | BPF_X:
  344. case BPF_ALU | BPF_DIV | BPF_X:
  345. case BPF_ALU | BPF_MOD | BPF_K:
  346. case BPF_ALU | BPF_DIV | BPF_K:
  347. case BPF_ALU64 | BPF_MOD | BPF_X:
  348. case BPF_ALU64 | BPF_DIV | BPF_X:
  349. case BPF_ALU64 | BPF_MOD | BPF_K:
  350. case BPF_ALU64 | BPF_DIV | BPF_K:
  351. EMIT1(0x50); /* push rax */
  352. EMIT1(0x52); /* push rdx */
  353. if (BPF_SRC(insn->code) == BPF_X)
  354. /* mov r11, src_reg */
  355. EMIT_mov(AUX_REG, src_reg);
  356. else
  357. /* mov r11, imm32 */
  358. EMIT3_off32(0x49, 0xC7, 0xC3, imm32);
  359. /* mov rax, dst_reg */
  360. EMIT_mov(BPF_REG_0, dst_reg);
  361. /* xor edx, edx
  362. * equivalent to 'xor rdx, rdx', but one byte less
  363. */
  364. EMIT2(0x31, 0xd2);
  365. if (BPF_SRC(insn->code) == BPF_X) {
  366. /* if (src_reg == 0) return 0 */
  367. /* cmp r11, 0 */
  368. EMIT4(0x49, 0x83, 0xFB, 0x00);
  369. /* jne .+9 (skip over pop, pop, xor and jmp) */
  370. EMIT2(X86_JNE, 1 + 1 + 2 + 5);
  371. EMIT1(0x5A); /* pop rdx */
  372. EMIT1(0x58); /* pop rax */
  373. EMIT2(0x31, 0xc0); /* xor eax, eax */
  374. /* jmp cleanup_addr
  375. * addrs[i] - 11, because there are 11 bytes
  376. * after this insn: div, mov, pop, pop, mov
  377. */
  378. jmp_offset = ctx->cleanup_addr - (addrs[i] - 11);
  379. EMIT1_off32(0xE9, jmp_offset);
  380. }
  381. if (BPF_CLASS(insn->code) == BPF_ALU64)
  382. /* div r11 */
  383. EMIT3(0x49, 0xF7, 0xF3);
  384. else
  385. /* div r11d */
  386. EMIT3(0x41, 0xF7, 0xF3);
  387. if (BPF_OP(insn->code) == BPF_MOD)
  388. /* mov r11, rdx */
  389. EMIT3(0x49, 0x89, 0xD3);
  390. else
  391. /* mov r11, rax */
  392. EMIT3(0x49, 0x89, 0xC3);
  393. EMIT1(0x5A); /* pop rdx */
  394. EMIT1(0x58); /* pop rax */
  395. /* mov dst_reg, r11 */
  396. EMIT_mov(dst_reg, AUX_REG);
  397. break;
  398. case BPF_ALU | BPF_MUL | BPF_K:
  399. case BPF_ALU | BPF_MUL | BPF_X:
  400. case BPF_ALU64 | BPF_MUL | BPF_K:
  401. case BPF_ALU64 | BPF_MUL | BPF_X:
  402. EMIT1(0x50); /* push rax */
  403. EMIT1(0x52); /* push rdx */
  404. /* mov r11, dst_reg */
  405. EMIT_mov(AUX_REG, dst_reg);
  406. if (BPF_SRC(insn->code) == BPF_X)
  407. /* mov rax, src_reg */
  408. EMIT_mov(BPF_REG_0, src_reg);
  409. else
  410. /* mov rax, imm32 */
  411. EMIT3_off32(0x48, 0xC7, 0xC0, imm32);
  412. if (BPF_CLASS(insn->code) == BPF_ALU64)
  413. EMIT1(add_1mod(0x48, AUX_REG));
  414. else if (is_ereg(AUX_REG))
  415. EMIT1(add_1mod(0x40, AUX_REG));
  416. /* mul(q) r11 */
  417. EMIT2(0xF7, add_1reg(0xE0, AUX_REG));
  418. /* mov r11, rax */
  419. EMIT_mov(AUX_REG, BPF_REG_0);
  420. EMIT1(0x5A); /* pop rdx */
  421. EMIT1(0x58); /* pop rax */
  422. /* mov dst_reg, r11 */
  423. EMIT_mov(dst_reg, AUX_REG);
  424. break;
  425. /* shifts */
  426. case BPF_ALU | BPF_LSH | BPF_K:
  427. case BPF_ALU | BPF_RSH | BPF_K:
  428. case BPF_ALU | BPF_ARSH | BPF_K:
  429. case BPF_ALU64 | BPF_LSH | BPF_K:
  430. case BPF_ALU64 | BPF_RSH | BPF_K:
  431. case BPF_ALU64 | BPF_ARSH | BPF_K:
  432. if (BPF_CLASS(insn->code) == BPF_ALU64)
  433. EMIT1(add_1mod(0x48, dst_reg));
  434. else if (is_ereg(dst_reg))
  435. EMIT1(add_1mod(0x40, dst_reg));
  436. switch (BPF_OP(insn->code)) {
  437. case BPF_LSH: b3 = 0xE0; break;
  438. case BPF_RSH: b3 = 0xE8; break;
  439. case BPF_ARSH: b3 = 0xF8; break;
  440. }
  441. EMIT3(0xC1, add_1reg(b3, dst_reg), imm32);
  442. break;
  443. case BPF_ALU | BPF_LSH | BPF_X:
  444. case BPF_ALU | BPF_RSH | BPF_X:
  445. case BPF_ALU | BPF_ARSH | BPF_X:
  446. case BPF_ALU64 | BPF_LSH | BPF_X:
  447. case BPF_ALU64 | BPF_RSH | BPF_X:
  448. case BPF_ALU64 | BPF_ARSH | BPF_X:
  449. /* check for bad case when dst_reg == rcx */
  450. if (dst_reg == BPF_REG_4) {
  451. /* mov r11, dst_reg */
  452. EMIT_mov(AUX_REG, dst_reg);
  453. dst_reg = AUX_REG;
  454. }
  455. if (src_reg != BPF_REG_4) { /* common case */
  456. EMIT1(0x51); /* push rcx */
  457. /* mov rcx, src_reg */
  458. EMIT_mov(BPF_REG_4, src_reg);
  459. }
  460. /* shl %rax, %cl | shr %rax, %cl | sar %rax, %cl */
  461. if (BPF_CLASS(insn->code) == BPF_ALU64)
  462. EMIT1(add_1mod(0x48, dst_reg));
  463. else if (is_ereg(dst_reg))
  464. EMIT1(add_1mod(0x40, dst_reg));
  465. switch (BPF_OP(insn->code)) {
  466. case BPF_LSH: b3 = 0xE0; break;
  467. case BPF_RSH: b3 = 0xE8; break;
  468. case BPF_ARSH: b3 = 0xF8; break;
  469. }
  470. EMIT2(0xD3, add_1reg(b3, dst_reg));
  471. if (src_reg != BPF_REG_4)
  472. EMIT1(0x59); /* pop rcx */
  473. if (insn->dst_reg == BPF_REG_4)
  474. /* mov dst_reg, r11 */
  475. EMIT_mov(insn->dst_reg, AUX_REG);
  476. break;
  477. case BPF_ALU | BPF_END | BPF_FROM_BE:
  478. switch (imm32) {
  479. case 16:
  480. /* emit 'ror %ax, 8' to swap lower 2 bytes */
  481. EMIT1(0x66);
  482. if (is_ereg(dst_reg))
  483. EMIT1(0x41);
  484. EMIT3(0xC1, add_1reg(0xC8, dst_reg), 8);
  485. /* emit 'movzwl eax, ax' */
  486. if (is_ereg(dst_reg))
  487. EMIT3(0x45, 0x0F, 0xB7);
  488. else
  489. EMIT2(0x0F, 0xB7);
  490. EMIT1(add_2reg(0xC0, dst_reg, dst_reg));
  491. break;
  492. case 32:
  493. /* emit 'bswap eax' to swap lower 4 bytes */
  494. if (is_ereg(dst_reg))
  495. EMIT2(0x41, 0x0F);
  496. else
  497. EMIT1(0x0F);
  498. EMIT1(add_1reg(0xC8, dst_reg));
  499. break;
  500. case 64:
  501. /* emit 'bswap rax' to swap 8 bytes */
  502. EMIT3(add_1mod(0x48, dst_reg), 0x0F,
  503. add_1reg(0xC8, dst_reg));
  504. break;
  505. }
  506. break;
  507. case BPF_ALU | BPF_END | BPF_FROM_LE:
  508. switch (imm32) {
  509. case 16:
  510. /* emit 'movzwl eax, ax' to zero extend 16-bit
  511. * into 64 bit
  512. */
  513. if (is_ereg(dst_reg))
  514. EMIT3(0x45, 0x0F, 0xB7);
  515. else
  516. EMIT2(0x0F, 0xB7);
  517. EMIT1(add_2reg(0xC0, dst_reg, dst_reg));
  518. break;
  519. case 32:
  520. /* emit 'mov eax, eax' to clear upper 32-bits */
  521. if (is_ereg(dst_reg))
  522. EMIT1(0x45);
  523. EMIT2(0x89, add_2reg(0xC0, dst_reg, dst_reg));
  524. break;
  525. case 64:
  526. /* nop */
  527. break;
  528. }
  529. break;
  530. /* ST: *(u8*)(dst_reg + off) = imm */
  531. case BPF_ST | BPF_MEM | BPF_B:
  532. if (is_ereg(dst_reg))
  533. EMIT2(0x41, 0xC6);
  534. else
  535. EMIT1(0xC6);
  536. goto st;
  537. case BPF_ST | BPF_MEM | BPF_H:
  538. if (is_ereg(dst_reg))
  539. EMIT3(0x66, 0x41, 0xC7);
  540. else
  541. EMIT2(0x66, 0xC7);
  542. goto st;
  543. case BPF_ST | BPF_MEM | BPF_W:
  544. if (is_ereg(dst_reg))
  545. EMIT2(0x41, 0xC7);
  546. else
  547. EMIT1(0xC7);
  548. goto st;
  549. case BPF_ST | BPF_MEM | BPF_DW:
  550. EMIT2(add_1mod(0x48, dst_reg), 0xC7);
  551. st: if (is_imm8(insn->off))
  552. EMIT2(add_1reg(0x40, dst_reg), insn->off);
  553. else
  554. EMIT1_off32(add_1reg(0x80, dst_reg), insn->off);
  555. EMIT(imm32, bpf_size_to_x86_bytes(BPF_SIZE(insn->code)));
  556. break;
  557. /* STX: *(u8*)(dst_reg + off) = src_reg */
  558. case BPF_STX | BPF_MEM | BPF_B:
  559. /* emit 'mov byte ptr [rax + off], al' */
  560. if (is_ereg(dst_reg) || is_ereg(src_reg) ||
  561. /* have to add extra byte for x86 SIL, DIL regs */
  562. src_reg == BPF_REG_1 || src_reg == BPF_REG_2)
  563. EMIT2(add_2mod(0x40, dst_reg, src_reg), 0x88);
  564. else
  565. EMIT1(0x88);
  566. goto stx;
  567. case BPF_STX | BPF_MEM | BPF_H:
  568. if (is_ereg(dst_reg) || is_ereg(src_reg))
  569. EMIT3(0x66, add_2mod(0x40, dst_reg, src_reg), 0x89);
  570. else
  571. EMIT2(0x66, 0x89);
  572. goto stx;
  573. case BPF_STX | BPF_MEM | BPF_W:
  574. if (is_ereg(dst_reg) || is_ereg(src_reg))
  575. EMIT2(add_2mod(0x40, dst_reg, src_reg), 0x89);
  576. else
  577. EMIT1(0x89);
  578. goto stx;
  579. case BPF_STX | BPF_MEM | BPF_DW:
  580. EMIT2(add_2mod(0x48, dst_reg, src_reg), 0x89);
  581. stx: if (is_imm8(insn->off))
  582. EMIT2(add_2reg(0x40, dst_reg, src_reg), insn->off);
  583. else
  584. EMIT1_off32(add_2reg(0x80, dst_reg, src_reg),
  585. insn->off);
  586. break;
  587. /* LDX: dst_reg = *(u8*)(src_reg + off) */
  588. case BPF_LDX | BPF_MEM | BPF_B:
  589. /* emit 'movzx rax, byte ptr [rax + off]' */
  590. EMIT3(add_2mod(0x48, src_reg, dst_reg), 0x0F, 0xB6);
  591. goto ldx;
  592. case BPF_LDX | BPF_MEM | BPF_H:
  593. /* emit 'movzx rax, word ptr [rax + off]' */
  594. EMIT3(add_2mod(0x48, src_reg, dst_reg), 0x0F, 0xB7);
  595. goto ldx;
  596. case BPF_LDX | BPF_MEM | BPF_W:
  597. /* emit 'mov eax, dword ptr [rax+0x14]' */
  598. if (is_ereg(dst_reg) || is_ereg(src_reg))
  599. EMIT2(add_2mod(0x40, src_reg, dst_reg), 0x8B);
  600. else
  601. EMIT1(0x8B);
  602. goto ldx;
  603. case BPF_LDX | BPF_MEM | BPF_DW:
  604. /* emit 'mov rax, qword ptr [rax+0x14]' */
  605. EMIT2(add_2mod(0x48, src_reg, dst_reg), 0x8B);
  606. ldx: /* if insn->off == 0 we can save one extra byte, but
  607. * special case of x86 r13 which always needs an offset
  608. * is not worth the hassle
  609. */
  610. if (is_imm8(insn->off))
  611. EMIT2(add_2reg(0x40, src_reg, dst_reg), insn->off);
  612. else
  613. EMIT1_off32(add_2reg(0x80, src_reg, dst_reg),
  614. insn->off);
  615. break;
  616. /* STX XADD: lock *(u32*)(dst_reg + off) += src_reg */
  617. case BPF_STX | BPF_XADD | BPF_W:
  618. /* emit 'lock add dword ptr [rax + off], eax' */
  619. if (is_ereg(dst_reg) || is_ereg(src_reg))
  620. EMIT3(0xF0, add_2mod(0x40, dst_reg, src_reg), 0x01);
  621. else
  622. EMIT2(0xF0, 0x01);
  623. goto xadd;
  624. case BPF_STX | BPF_XADD | BPF_DW:
  625. EMIT3(0xF0, add_2mod(0x48, dst_reg, src_reg), 0x01);
  626. xadd: if (is_imm8(insn->off))
  627. EMIT2(add_2reg(0x40, dst_reg, src_reg), insn->off);
  628. else
  629. EMIT1_off32(add_2reg(0x80, dst_reg, src_reg),
  630. insn->off);
  631. break;
  632. /* call */
  633. case BPF_JMP | BPF_CALL:
  634. func = (u8 *) __bpf_call_base + imm32;
  635. jmp_offset = func - (image + addrs[i]);
  636. if (seen_ld_abs) {
  637. EMIT2(0x41, 0x52); /* push %r10 */
  638. EMIT2(0x41, 0x51); /* push %r9 */
  639. /* need to adjust jmp offset, since
  640. * pop %r9, pop %r10 take 4 bytes after call insn
  641. */
  642. jmp_offset += 4;
  643. }
  644. if (!imm32 || !is_simm32(jmp_offset)) {
  645. pr_err("unsupported bpf func %d addr %p image %p\n",
  646. imm32, func, image);
  647. return -EINVAL;
  648. }
  649. EMIT1_off32(0xE8, jmp_offset);
  650. if (seen_ld_abs) {
  651. EMIT2(0x41, 0x59); /* pop %r9 */
  652. EMIT2(0x41, 0x5A); /* pop %r10 */
  653. }
  654. break;
  655. /* cond jump */
  656. case BPF_JMP | BPF_JEQ | BPF_X:
  657. case BPF_JMP | BPF_JNE | BPF_X:
  658. case BPF_JMP | BPF_JGT | BPF_X:
  659. case BPF_JMP | BPF_JGE | BPF_X:
  660. case BPF_JMP | BPF_JSGT | BPF_X:
  661. case BPF_JMP | BPF_JSGE | BPF_X:
  662. /* cmp dst_reg, src_reg */
  663. EMIT3(add_2mod(0x48, dst_reg, src_reg), 0x39,
  664. add_2reg(0xC0, dst_reg, src_reg));
  665. goto emit_cond_jmp;
  666. case BPF_JMP | BPF_JSET | BPF_X:
  667. /* test dst_reg, src_reg */
  668. EMIT3(add_2mod(0x48, dst_reg, src_reg), 0x85,
  669. add_2reg(0xC0, dst_reg, src_reg));
  670. goto emit_cond_jmp;
  671. case BPF_JMP | BPF_JSET | BPF_K:
  672. /* test dst_reg, imm32 */
  673. EMIT1(add_1mod(0x48, dst_reg));
  674. EMIT2_off32(0xF7, add_1reg(0xC0, dst_reg), imm32);
  675. goto emit_cond_jmp;
  676. case BPF_JMP | BPF_JEQ | BPF_K:
  677. case BPF_JMP | BPF_JNE | BPF_K:
  678. case BPF_JMP | BPF_JGT | BPF_K:
  679. case BPF_JMP | BPF_JGE | BPF_K:
  680. case BPF_JMP | BPF_JSGT | BPF_K:
  681. case BPF_JMP | BPF_JSGE | BPF_K:
  682. /* cmp dst_reg, imm8/32 */
  683. EMIT1(add_1mod(0x48, dst_reg));
  684. if (is_imm8(imm32))
  685. EMIT3(0x83, add_1reg(0xF8, dst_reg), imm32);
  686. else
  687. EMIT2_off32(0x81, add_1reg(0xF8, dst_reg), imm32);
  688. emit_cond_jmp: /* convert BPF opcode to x86 */
  689. switch (BPF_OP(insn->code)) {
  690. case BPF_JEQ:
  691. jmp_cond = X86_JE;
  692. break;
  693. case BPF_JSET:
  694. case BPF_JNE:
  695. jmp_cond = X86_JNE;
  696. break;
  697. case BPF_JGT:
  698. /* GT is unsigned '>', JA in x86 */
  699. jmp_cond = X86_JA;
  700. break;
  701. case BPF_JGE:
  702. /* GE is unsigned '>=', JAE in x86 */
  703. jmp_cond = X86_JAE;
  704. break;
  705. case BPF_JSGT:
  706. /* signed '>', GT in x86 */
  707. jmp_cond = X86_JG;
  708. break;
  709. case BPF_JSGE:
  710. /* signed '>=', GE in x86 */
  711. jmp_cond = X86_JGE;
  712. break;
  713. default: /* to silence gcc warning */
  714. return -EFAULT;
  715. }
  716. jmp_offset = addrs[i + insn->off] - addrs[i];
  717. if (is_imm8(jmp_offset)) {
  718. EMIT2(jmp_cond, jmp_offset);
  719. } else if (is_simm32(jmp_offset)) {
  720. EMIT2_off32(0x0F, jmp_cond + 0x10, jmp_offset);
  721. } else {
  722. pr_err("cond_jmp gen bug %llx\n", jmp_offset);
  723. return -EFAULT;
  724. }
  725. break;
  726. case BPF_JMP | BPF_JA:
  727. jmp_offset = addrs[i + insn->off] - addrs[i];
  728. if (!jmp_offset)
  729. /* optimize out nop jumps */
  730. break;
  731. emit_jmp:
  732. if (is_imm8(jmp_offset)) {
  733. EMIT2(0xEB, jmp_offset);
  734. } else if (is_simm32(jmp_offset)) {
  735. EMIT1_off32(0xE9, jmp_offset);
  736. } else {
  737. pr_err("jmp gen bug %llx\n", jmp_offset);
  738. return -EFAULT;
  739. }
  740. break;
  741. case BPF_LD | BPF_IND | BPF_W:
  742. func = sk_load_word;
  743. goto common_load;
  744. case BPF_LD | BPF_ABS | BPF_W:
  745. func = CHOOSE_LOAD_FUNC(imm32, sk_load_word);
  746. common_load:
  747. ctx->seen_ld_abs = seen_ld_abs = true;
  748. jmp_offset = func - (image + addrs[i]);
  749. if (!func || !is_simm32(jmp_offset)) {
  750. pr_err("unsupported bpf func %d addr %p image %p\n",
  751. imm32, func, image);
  752. return -EINVAL;
  753. }
  754. if (BPF_MODE(insn->code) == BPF_ABS) {
  755. /* mov %esi, imm32 */
  756. EMIT1_off32(0xBE, imm32);
  757. } else {
  758. /* mov %rsi, src_reg */
  759. EMIT_mov(BPF_REG_2, src_reg);
  760. if (imm32) {
  761. if (is_imm8(imm32))
  762. /* add %esi, imm8 */
  763. EMIT3(0x83, 0xC6, imm32);
  764. else
  765. /* add %esi, imm32 */
  766. EMIT2_off32(0x81, 0xC6, imm32);
  767. }
  768. }
  769. /* skb pointer is in R6 (%rbx), it will be copied into
  770. * %rdi if skb_copy_bits() call is necessary.
  771. * sk_load_* helpers also use %r10 and %r9d.
  772. * See bpf_jit.S
  773. */
  774. EMIT1_off32(0xE8, jmp_offset); /* call */
  775. break;
  776. case BPF_LD | BPF_IND | BPF_H:
  777. func = sk_load_half;
  778. goto common_load;
  779. case BPF_LD | BPF_ABS | BPF_H:
  780. func = CHOOSE_LOAD_FUNC(imm32, sk_load_half);
  781. goto common_load;
  782. case BPF_LD | BPF_IND | BPF_B:
  783. func = sk_load_byte;
  784. goto common_load;
  785. case BPF_LD | BPF_ABS | BPF_B:
  786. func = CHOOSE_LOAD_FUNC(imm32, sk_load_byte);
  787. goto common_load;
  788. case BPF_JMP | BPF_EXIT:
  789. if (seen_exit) {
  790. jmp_offset = ctx->cleanup_addr - addrs[i];
  791. goto emit_jmp;
  792. }
  793. seen_exit = true;
  794. /* update cleanup_addr */
  795. ctx->cleanup_addr = proglen;
  796. /* mov rbx, qword ptr [rbp-X] */
  797. EMIT3_off32(0x48, 0x8B, 0x9D, -stacksize);
  798. /* mov r13, qword ptr [rbp-X] */
  799. EMIT3_off32(0x4C, 0x8B, 0xAD, -stacksize + 8);
  800. /* mov r14, qword ptr [rbp-X] */
  801. EMIT3_off32(0x4C, 0x8B, 0xB5, -stacksize + 16);
  802. /* mov r15, qword ptr [rbp-X] */
  803. EMIT3_off32(0x4C, 0x8B, 0xBD, -stacksize + 24);
  804. EMIT1(0xC9); /* leave */
  805. EMIT1(0xC3); /* ret */
  806. break;
  807. default:
  808. /* By design x64 JIT should support all BPF instructions
  809. * This error will be seen if new instruction was added
  810. * to interpreter, but not to JIT
  811. * or if there is junk in bpf_prog
  812. */
  813. pr_err("bpf_jit: unknown opcode %02x\n", insn->code);
  814. return -EINVAL;
  815. }
  816. ilen = prog - temp;
  817. if (ilen > BPF_MAX_INSN_SIZE) {
  818. pr_err("bpf_jit_compile fatal insn size error\n");
  819. return -EFAULT;
  820. }
  821. if (image) {
  822. if (unlikely(proglen + ilen > oldproglen)) {
  823. pr_err("bpf_jit_compile fatal error\n");
  824. return -EFAULT;
  825. }
  826. memcpy(image + proglen, temp, ilen);
  827. }
  828. proglen += ilen;
  829. addrs[i] = proglen;
  830. prog = temp;
  831. }
  832. return proglen;
  833. }
  834. void bpf_jit_compile(struct bpf_prog *prog)
  835. {
  836. }
  837. void bpf_int_jit_compile(struct bpf_prog *prog)
  838. {
  839. struct bpf_binary_header *header = NULL;
  840. int proglen, oldproglen = 0;
  841. struct jit_context ctx = {};
  842. u8 *image = NULL;
  843. int *addrs;
  844. int pass;
  845. int i;
  846. if (!bpf_jit_enable)
  847. return;
  848. if (!prog || !prog->len)
  849. return;
  850. addrs = kmalloc(prog->len * sizeof(*addrs), GFP_KERNEL);
  851. if (!addrs)
  852. return;
  853. /* Before first pass, make a rough estimation of addrs[]
  854. * each bpf instruction is translated to less than 64 bytes
  855. */
  856. for (proglen = 0, i = 0; i < prog->len; i++) {
  857. proglen += 64;
  858. addrs[i] = proglen;
  859. }
  860. ctx.cleanup_addr = proglen;
  861. /* JITed image shrinks with every pass and the loop iterates
  862. * until the image stops shrinking. Very large bpf programs
  863. * may converge on the last pass. In such case do one more
  864. * pass to emit the final image
  865. */
  866. for (pass = 0; pass < 10 || image; pass++) {
  867. proglen = do_jit(prog, addrs, image, oldproglen, &ctx);
  868. if (proglen <= 0) {
  869. image = NULL;
  870. if (header)
  871. bpf_jit_binary_free(header);
  872. goto out;
  873. }
  874. if (image) {
  875. if (proglen != oldproglen) {
  876. pr_err("bpf_jit: proglen=%d != oldproglen=%d\n",
  877. proglen, oldproglen);
  878. goto out;
  879. }
  880. break;
  881. }
  882. if (proglen == oldproglen) {
  883. header = bpf_jit_binary_alloc(proglen, &image,
  884. 1, jit_fill_hole);
  885. if (!header)
  886. goto out;
  887. }
  888. oldproglen = proglen;
  889. }
  890. if (bpf_jit_enable > 1)
  891. bpf_jit_dump(prog->len, proglen, 0, image);
  892. if (image) {
  893. bpf_flush_icache(header, image + proglen);
  894. set_memory_ro((unsigned long)header, header->pages);
  895. prog->bpf_func = (void *)image;
  896. prog->jited = true;
  897. }
  898. out:
  899. kfree(addrs);
  900. }
  901. void bpf_jit_free(struct bpf_prog *fp)
  902. {
  903. unsigned long addr = (unsigned long)fp->bpf_func & PAGE_MASK;
  904. struct bpf_binary_header *header = (void *)addr;
  905. if (!fp->jited)
  906. goto free_filter;
  907. set_memory_rw(addr, header->pages);
  908. bpf_jit_binary_free(header);
  909. free_filter:
  910. bpf_prog_unlock_free(fp);
  911. }