bpf_jit_comp.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  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 inline 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 inline bool is_imm8(int value)
  50. {
  51. return value <= 127 && value >= -128;
  52. }
  53. static inline 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 inline 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 inline bool is_ereg(u32 reg)
  121. {
  122. if (reg == BPF_REG_5 || reg == AUX_REG ||
  123. (reg >= BPF_REG_7 && reg <= BPF_REG_9))
  124. return true;
  125. else
  126. return false;
  127. }
  128. /* add modifiers if 'reg' maps to x64 registers r8..r15 */
  129. static inline u8 add_1mod(u8 byte, u32 reg)
  130. {
  131. if (is_ereg(reg))
  132. byte |= 1;
  133. return byte;
  134. }
  135. static inline 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 inline 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 inline 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. unsigned 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. u8 temp[BPF_MAX_INSN_SIZE + BPF_INSN_SAFETY];
  172. int i;
  173. int proglen = 0;
  174. u8 *prog = temp;
  175. int stacksize = MAX_BPF_STACK +
  176. 32 /* space for rbx, r13, r14, r15 */ +
  177. 8 /* space for skb_copy_bits() buffer */;
  178. EMIT1(0x55); /* push rbp */
  179. EMIT3(0x48, 0x89, 0xE5); /* mov rbp,rsp */
  180. /* sub rsp, stacksize */
  181. EMIT3_off32(0x48, 0x81, 0xEC, stacksize);
  182. /* all classic BPF filters use R6(rbx) save it */
  183. /* mov qword ptr [rbp-X],rbx */
  184. EMIT3_off32(0x48, 0x89, 0x9D, -stacksize);
  185. /* bpf_convert_filter() maps classic BPF register X to R7 and uses R8
  186. * as temporary, so all tcpdump filters need to spill/fill R7(r13) and
  187. * R8(r14). R9(r15) spill could be made conditional, but there is only
  188. * one 'bpf_error' return path out of helper functions inside bpf_jit.S
  189. * The overhead of extra spill is negligible for any filter other
  190. * than synthetic ones. Therefore not worth adding complexity.
  191. */
  192. /* mov qword ptr [rbp-X],r13 */
  193. EMIT3_off32(0x4C, 0x89, 0xAD, -stacksize + 8);
  194. /* mov qword ptr [rbp-X],r14 */
  195. EMIT3_off32(0x4C, 0x89, 0xB5, -stacksize + 16);
  196. /* mov qword ptr [rbp-X],r15 */
  197. EMIT3_off32(0x4C, 0x89, 0xBD, -stacksize + 24);
  198. /* clear A and X registers */
  199. EMIT2(0x31, 0xc0); /* xor eax, eax */
  200. EMIT3(0x4D, 0x31, 0xED); /* xor r13, r13 */
  201. if (seen_ld_abs) {
  202. /* r9d : skb->len - skb->data_len (headlen)
  203. * r10 : skb->data
  204. */
  205. if (is_imm8(offsetof(struct sk_buff, len)))
  206. /* mov %r9d, off8(%rdi) */
  207. EMIT4(0x44, 0x8b, 0x4f,
  208. offsetof(struct sk_buff, len));
  209. else
  210. /* mov %r9d, off32(%rdi) */
  211. EMIT3_off32(0x44, 0x8b, 0x8f,
  212. offsetof(struct sk_buff, len));
  213. if (is_imm8(offsetof(struct sk_buff, data_len)))
  214. /* sub %r9d, off8(%rdi) */
  215. EMIT4(0x44, 0x2b, 0x4f,
  216. offsetof(struct sk_buff, data_len));
  217. else
  218. EMIT3_off32(0x44, 0x2b, 0x8f,
  219. offsetof(struct sk_buff, data_len));
  220. if (is_imm8(offsetof(struct sk_buff, data)))
  221. /* mov %r10, off8(%rdi) */
  222. EMIT4(0x4c, 0x8b, 0x57,
  223. offsetof(struct sk_buff, data));
  224. else
  225. /* mov %r10, off32(%rdi) */
  226. EMIT3_off32(0x4c, 0x8b, 0x97,
  227. offsetof(struct sk_buff, data));
  228. }
  229. for (i = 0; i < insn_cnt; i++, insn++) {
  230. const s32 imm32 = insn->imm;
  231. u32 dst_reg = insn->dst_reg;
  232. u32 src_reg = insn->src_reg;
  233. u8 b1 = 0, b2 = 0, b3 = 0;
  234. s64 jmp_offset;
  235. u8 jmp_cond;
  236. int ilen;
  237. u8 *func;
  238. switch (insn->code) {
  239. /* ALU */
  240. case BPF_ALU | BPF_ADD | BPF_X:
  241. case BPF_ALU | BPF_SUB | BPF_X:
  242. case BPF_ALU | BPF_AND | BPF_X:
  243. case BPF_ALU | BPF_OR | BPF_X:
  244. case BPF_ALU | BPF_XOR | BPF_X:
  245. case BPF_ALU64 | BPF_ADD | BPF_X:
  246. case BPF_ALU64 | BPF_SUB | BPF_X:
  247. case BPF_ALU64 | BPF_AND | BPF_X:
  248. case BPF_ALU64 | BPF_OR | BPF_X:
  249. case BPF_ALU64 | BPF_XOR | BPF_X:
  250. switch (BPF_OP(insn->code)) {
  251. case BPF_ADD: b2 = 0x01; break;
  252. case BPF_SUB: b2 = 0x29; break;
  253. case BPF_AND: b2 = 0x21; break;
  254. case BPF_OR: b2 = 0x09; break;
  255. case BPF_XOR: b2 = 0x31; break;
  256. }
  257. if (BPF_CLASS(insn->code) == BPF_ALU64)
  258. EMIT1(add_2mod(0x48, dst_reg, src_reg));
  259. else if (is_ereg(dst_reg) || is_ereg(src_reg))
  260. EMIT1(add_2mod(0x40, dst_reg, src_reg));
  261. EMIT2(b2, add_2reg(0xC0, dst_reg, src_reg));
  262. break;
  263. /* mov dst, src */
  264. case BPF_ALU64 | BPF_MOV | BPF_X:
  265. EMIT_mov(dst_reg, src_reg);
  266. break;
  267. /* mov32 dst, src */
  268. case BPF_ALU | BPF_MOV | BPF_X:
  269. if (is_ereg(dst_reg) || is_ereg(src_reg))
  270. EMIT1(add_2mod(0x40, dst_reg, src_reg));
  271. EMIT2(0x89, add_2reg(0xC0, dst_reg, src_reg));
  272. break;
  273. /* neg dst */
  274. case BPF_ALU | BPF_NEG:
  275. case BPF_ALU64 | BPF_NEG:
  276. if (BPF_CLASS(insn->code) == BPF_ALU64)
  277. EMIT1(add_1mod(0x48, dst_reg));
  278. else if (is_ereg(dst_reg))
  279. EMIT1(add_1mod(0x40, dst_reg));
  280. EMIT2(0xF7, add_1reg(0xD8, dst_reg));
  281. break;
  282. case BPF_ALU | BPF_ADD | BPF_K:
  283. case BPF_ALU | BPF_SUB | BPF_K:
  284. case BPF_ALU | BPF_AND | BPF_K:
  285. case BPF_ALU | BPF_OR | BPF_K:
  286. case BPF_ALU | BPF_XOR | BPF_K:
  287. case BPF_ALU64 | BPF_ADD | BPF_K:
  288. case BPF_ALU64 | BPF_SUB | BPF_K:
  289. case BPF_ALU64 | BPF_AND | BPF_K:
  290. case BPF_ALU64 | BPF_OR | BPF_K:
  291. case BPF_ALU64 | BPF_XOR | BPF_K:
  292. if (BPF_CLASS(insn->code) == BPF_ALU64)
  293. EMIT1(add_1mod(0x48, dst_reg));
  294. else if (is_ereg(dst_reg))
  295. EMIT1(add_1mod(0x40, dst_reg));
  296. switch (BPF_OP(insn->code)) {
  297. case BPF_ADD: b3 = 0xC0; break;
  298. case BPF_SUB: b3 = 0xE8; break;
  299. case BPF_AND: b3 = 0xE0; break;
  300. case BPF_OR: b3 = 0xC8; break;
  301. case BPF_XOR: b3 = 0xF0; break;
  302. }
  303. if (is_imm8(imm32))
  304. EMIT3(0x83, add_1reg(b3, dst_reg), imm32);
  305. else
  306. EMIT2_off32(0x81, add_1reg(b3, dst_reg), imm32);
  307. break;
  308. case BPF_ALU64 | BPF_MOV | BPF_K:
  309. /* optimization: if imm32 is positive,
  310. * use 'mov eax, imm32' (which zero-extends imm32)
  311. * to save 2 bytes
  312. */
  313. if (imm32 < 0) {
  314. /* 'mov rax, imm32' sign extends imm32 */
  315. b1 = add_1mod(0x48, dst_reg);
  316. b2 = 0xC7;
  317. b3 = 0xC0;
  318. EMIT3_off32(b1, b2, add_1reg(b3, dst_reg), imm32);
  319. break;
  320. }
  321. case BPF_ALU | BPF_MOV | BPF_K:
  322. /* mov %eax, imm32 */
  323. if (is_ereg(dst_reg))
  324. EMIT1(add_1mod(0x40, dst_reg));
  325. EMIT1_off32(add_1reg(0xB8, dst_reg), imm32);
  326. break;
  327. case BPF_LD | BPF_IMM | BPF_DW:
  328. if (insn[1].code != 0 || insn[1].src_reg != 0 ||
  329. insn[1].dst_reg != 0 || insn[1].off != 0) {
  330. /* verifier must catch invalid insns */
  331. pr_err("invalid BPF_LD_IMM64 insn\n");
  332. return -EINVAL;
  333. }
  334. /* movabsq %rax, imm64 */
  335. EMIT2(add_1mod(0x48, dst_reg), add_1reg(0xB8, dst_reg));
  336. EMIT(insn[0].imm, 4);
  337. EMIT(insn[1].imm, 4);
  338. insn++;
  339. i++;
  340. break;
  341. /* dst %= src, dst /= src, dst %= imm32, dst /= imm32 */
  342. case BPF_ALU | BPF_MOD | BPF_X:
  343. case BPF_ALU | BPF_DIV | BPF_X:
  344. case BPF_ALU | BPF_MOD | BPF_K:
  345. case BPF_ALU | BPF_DIV | BPF_K:
  346. case BPF_ALU64 | BPF_MOD | BPF_X:
  347. case BPF_ALU64 | BPF_DIV | BPF_X:
  348. case BPF_ALU64 | BPF_MOD | BPF_K:
  349. case BPF_ALU64 | BPF_DIV | BPF_K:
  350. EMIT1(0x50); /* push rax */
  351. EMIT1(0x52); /* push rdx */
  352. if (BPF_SRC(insn->code) == BPF_X)
  353. /* mov r11, src_reg */
  354. EMIT_mov(AUX_REG, src_reg);
  355. else
  356. /* mov r11, imm32 */
  357. EMIT3_off32(0x49, 0xC7, 0xC3, imm32);
  358. /* mov rax, dst_reg */
  359. EMIT_mov(BPF_REG_0, dst_reg);
  360. /* xor edx, edx
  361. * equivalent to 'xor rdx, rdx', but one byte less
  362. */
  363. EMIT2(0x31, 0xd2);
  364. if (BPF_SRC(insn->code) == BPF_X) {
  365. /* if (src_reg == 0) return 0 */
  366. /* cmp r11, 0 */
  367. EMIT4(0x49, 0x83, 0xFB, 0x00);
  368. /* jne .+9 (skip over pop, pop, xor and jmp) */
  369. EMIT2(X86_JNE, 1 + 1 + 2 + 5);
  370. EMIT1(0x5A); /* pop rdx */
  371. EMIT1(0x58); /* pop rax */
  372. EMIT2(0x31, 0xc0); /* xor eax, eax */
  373. /* jmp cleanup_addr
  374. * addrs[i] - 11, because there are 11 bytes
  375. * after this insn: div, mov, pop, pop, mov
  376. */
  377. jmp_offset = ctx->cleanup_addr - (addrs[i] - 11);
  378. EMIT1_off32(0xE9, jmp_offset);
  379. }
  380. if (BPF_CLASS(insn->code) == BPF_ALU64)
  381. /* div r11 */
  382. EMIT3(0x49, 0xF7, 0xF3);
  383. else
  384. /* div r11d */
  385. EMIT3(0x41, 0xF7, 0xF3);
  386. if (BPF_OP(insn->code) == BPF_MOD)
  387. /* mov r11, rdx */
  388. EMIT3(0x49, 0x89, 0xD3);
  389. else
  390. /* mov r11, rax */
  391. EMIT3(0x49, 0x89, 0xC3);
  392. EMIT1(0x5A); /* pop rdx */
  393. EMIT1(0x58); /* pop rax */
  394. /* mov dst_reg, r11 */
  395. EMIT_mov(dst_reg, AUX_REG);
  396. break;
  397. case BPF_ALU | BPF_MUL | BPF_K:
  398. case BPF_ALU | BPF_MUL | BPF_X:
  399. case BPF_ALU64 | BPF_MUL | BPF_K:
  400. case BPF_ALU64 | BPF_MUL | BPF_X:
  401. EMIT1(0x50); /* push rax */
  402. EMIT1(0x52); /* push rdx */
  403. /* mov r11, dst_reg */
  404. EMIT_mov(AUX_REG, dst_reg);
  405. if (BPF_SRC(insn->code) == BPF_X)
  406. /* mov rax, src_reg */
  407. EMIT_mov(BPF_REG_0, src_reg);
  408. else
  409. /* mov rax, imm32 */
  410. EMIT3_off32(0x48, 0xC7, 0xC0, imm32);
  411. if (BPF_CLASS(insn->code) == BPF_ALU64)
  412. EMIT1(add_1mod(0x48, AUX_REG));
  413. else if (is_ereg(AUX_REG))
  414. EMIT1(add_1mod(0x40, AUX_REG));
  415. /* mul(q) r11 */
  416. EMIT2(0xF7, add_1reg(0xE0, AUX_REG));
  417. /* mov r11, rax */
  418. EMIT_mov(AUX_REG, BPF_REG_0);
  419. EMIT1(0x5A); /* pop rdx */
  420. EMIT1(0x58); /* pop rax */
  421. /* mov dst_reg, r11 */
  422. EMIT_mov(dst_reg, AUX_REG);
  423. break;
  424. /* shifts */
  425. case BPF_ALU | BPF_LSH | BPF_K:
  426. case BPF_ALU | BPF_RSH | BPF_K:
  427. case BPF_ALU | BPF_ARSH | BPF_K:
  428. case BPF_ALU64 | BPF_LSH | BPF_K:
  429. case BPF_ALU64 | BPF_RSH | BPF_K:
  430. case BPF_ALU64 | BPF_ARSH | BPF_K:
  431. if (BPF_CLASS(insn->code) == BPF_ALU64)
  432. EMIT1(add_1mod(0x48, dst_reg));
  433. else if (is_ereg(dst_reg))
  434. EMIT1(add_1mod(0x40, dst_reg));
  435. switch (BPF_OP(insn->code)) {
  436. case BPF_LSH: b3 = 0xE0; break;
  437. case BPF_RSH: b3 = 0xE8; break;
  438. case BPF_ARSH: b3 = 0xF8; break;
  439. }
  440. EMIT3(0xC1, add_1reg(b3, dst_reg), imm32);
  441. break;
  442. case BPF_ALU | BPF_LSH | BPF_X:
  443. case BPF_ALU | BPF_RSH | BPF_X:
  444. case BPF_ALU | BPF_ARSH | BPF_X:
  445. case BPF_ALU64 | BPF_LSH | BPF_X:
  446. case BPF_ALU64 | BPF_RSH | BPF_X:
  447. case BPF_ALU64 | BPF_ARSH | BPF_X:
  448. /* check for bad case when dst_reg == rcx */
  449. if (dst_reg == BPF_REG_4) {
  450. /* mov r11, dst_reg */
  451. EMIT_mov(AUX_REG, dst_reg);
  452. dst_reg = AUX_REG;
  453. }
  454. if (src_reg != BPF_REG_4) { /* common case */
  455. EMIT1(0x51); /* push rcx */
  456. /* mov rcx, src_reg */
  457. EMIT_mov(BPF_REG_4, src_reg);
  458. }
  459. /* shl %rax, %cl | shr %rax, %cl | sar %rax, %cl */
  460. if (BPF_CLASS(insn->code) == BPF_ALU64)
  461. EMIT1(add_1mod(0x48, dst_reg));
  462. else if (is_ereg(dst_reg))
  463. EMIT1(add_1mod(0x40, dst_reg));
  464. switch (BPF_OP(insn->code)) {
  465. case BPF_LSH: b3 = 0xE0; break;
  466. case BPF_RSH: b3 = 0xE8; break;
  467. case BPF_ARSH: b3 = 0xF8; break;
  468. }
  469. EMIT2(0xD3, add_1reg(b3, dst_reg));
  470. if (src_reg != BPF_REG_4)
  471. EMIT1(0x59); /* pop rcx */
  472. if (insn->dst_reg == BPF_REG_4)
  473. /* mov dst_reg, r11 */
  474. EMIT_mov(insn->dst_reg, AUX_REG);
  475. break;
  476. case BPF_ALU | BPF_END | BPF_FROM_BE:
  477. switch (imm32) {
  478. case 16:
  479. /* emit 'ror %ax, 8' to swap lower 2 bytes */
  480. EMIT1(0x66);
  481. if (is_ereg(dst_reg))
  482. EMIT1(0x41);
  483. EMIT3(0xC1, add_1reg(0xC8, dst_reg), 8);
  484. break;
  485. case 32:
  486. /* emit 'bswap eax' to swap lower 4 bytes */
  487. if (is_ereg(dst_reg))
  488. EMIT2(0x41, 0x0F);
  489. else
  490. EMIT1(0x0F);
  491. EMIT1(add_1reg(0xC8, dst_reg));
  492. break;
  493. case 64:
  494. /* emit 'bswap rax' to swap 8 bytes */
  495. EMIT3(add_1mod(0x48, dst_reg), 0x0F,
  496. add_1reg(0xC8, dst_reg));
  497. break;
  498. }
  499. break;
  500. case BPF_ALU | BPF_END | BPF_FROM_LE:
  501. break;
  502. /* ST: *(u8*)(dst_reg + off) = imm */
  503. case BPF_ST | BPF_MEM | BPF_B:
  504. if (is_ereg(dst_reg))
  505. EMIT2(0x41, 0xC6);
  506. else
  507. EMIT1(0xC6);
  508. goto st;
  509. case BPF_ST | BPF_MEM | BPF_H:
  510. if (is_ereg(dst_reg))
  511. EMIT3(0x66, 0x41, 0xC7);
  512. else
  513. EMIT2(0x66, 0xC7);
  514. goto st;
  515. case BPF_ST | BPF_MEM | BPF_W:
  516. if (is_ereg(dst_reg))
  517. EMIT2(0x41, 0xC7);
  518. else
  519. EMIT1(0xC7);
  520. goto st;
  521. case BPF_ST | BPF_MEM | BPF_DW:
  522. EMIT2(add_1mod(0x48, dst_reg), 0xC7);
  523. st: if (is_imm8(insn->off))
  524. EMIT2(add_1reg(0x40, dst_reg), insn->off);
  525. else
  526. EMIT1_off32(add_1reg(0x80, dst_reg), insn->off);
  527. EMIT(imm32, bpf_size_to_x86_bytes(BPF_SIZE(insn->code)));
  528. break;
  529. /* STX: *(u8*)(dst_reg + off) = src_reg */
  530. case BPF_STX | BPF_MEM | BPF_B:
  531. /* emit 'mov byte ptr [rax + off], al' */
  532. if (is_ereg(dst_reg) || is_ereg(src_reg) ||
  533. /* have to add extra byte for x86 SIL, DIL regs */
  534. src_reg == BPF_REG_1 || src_reg == BPF_REG_2)
  535. EMIT2(add_2mod(0x40, dst_reg, src_reg), 0x88);
  536. else
  537. EMIT1(0x88);
  538. goto stx;
  539. case BPF_STX | BPF_MEM | BPF_H:
  540. if (is_ereg(dst_reg) || is_ereg(src_reg))
  541. EMIT3(0x66, add_2mod(0x40, dst_reg, src_reg), 0x89);
  542. else
  543. EMIT2(0x66, 0x89);
  544. goto stx;
  545. case BPF_STX | BPF_MEM | BPF_W:
  546. if (is_ereg(dst_reg) || is_ereg(src_reg))
  547. EMIT2(add_2mod(0x40, dst_reg, src_reg), 0x89);
  548. else
  549. EMIT1(0x89);
  550. goto stx;
  551. case BPF_STX | BPF_MEM | BPF_DW:
  552. EMIT2(add_2mod(0x48, dst_reg, src_reg), 0x89);
  553. stx: if (is_imm8(insn->off))
  554. EMIT2(add_2reg(0x40, dst_reg, src_reg), insn->off);
  555. else
  556. EMIT1_off32(add_2reg(0x80, dst_reg, src_reg),
  557. insn->off);
  558. break;
  559. /* LDX: dst_reg = *(u8*)(src_reg + off) */
  560. case BPF_LDX | BPF_MEM | BPF_B:
  561. /* emit 'movzx rax, byte ptr [rax + off]' */
  562. EMIT3(add_2mod(0x48, src_reg, dst_reg), 0x0F, 0xB6);
  563. goto ldx;
  564. case BPF_LDX | BPF_MEM | BPF_H:
  565. /* emit 'movzx rax, word ptr [rax + off]' */
  566. EMIT3(add_2mod(0x48, src_reg, dst_reg), 0x0F, 0xB7);
  567. goto ldx;
  568. case BPF_LDX | BPF_MEM | BPF_W:
  569. /* emit 'mov eax, dword ptr [rax+0x14]' */
  570. if (is_ereg(dst_reg) || is_ereg(src_reg))
  571. EMIT2(add_2mod(0x40, src_reg, dst_reg), 0x8B);
  572. else
  573. EMIT1(0x8B);
  574. goto ldx;
  575. case BPF_LDX | BPF_MEM | BPF_DW:
  576. /* emit 'mov rax, qword ptr [rax+0x14]' */
  577. EMIT2(add_2mod(0x48, src_reg, dst_reg), 0x8B);
  578. ldx: /* if insn->off == 0 we can save one extra byte, but
  579. * special case of x86 r13 which always needs an offset
  580. * is not worth the hassle
  581. */
  582. if (is_imm8(insn->off))
  583. EMIT2(add_2reg(0x40, src_reg, dst_reg), insn->off);
  584. else
  585. EMIT1_off32(add_2reg(0x80, src_reg, dst_reg),
  586. insn->off);
  587. break;
  588. /* STX XADD: lock *(u32*)(dst_reg + off) += src_reg */
  589. case BPF_STX | BPF_XADD | BPF_W:
  590. /* emit 'lock add dword ptr [rax + off], eax' */
  591. if (is_ereg(dst_reg) || is_ereg(src_reg))
  592. EMIT3(0xF0, add_2mod(0x40, dst_reg, src_reg), 0x01);
  593. else
  594. EMIT2(0xF0, 0x01);
  595. goto xadd;
  596. case BPF_STX | BPF_XADD | BPF_DW:
  597. EMIT3(0xF0, add_2mod(0x48, dst_reg, src_reg), 0x01);
  598. xadd: if (is_imm8(insn->off))
  599. EMIT2(add_2reg(0x40, dst_reg, src_reg), insn->off);
  600. else
  601. EMIT1_off32(add_2reg(0x80, dst_reg, src_reg),
  602. insn->off);
  603. break;
  604. /* call */
  605. case BPF_JMP | BPF_CALL:
  606. func = (u8 *) __bpf_call_base + imm32;
  607. jmp_offset = func - (image + addrs[i]);
  608. if (seen_ld_abs) {
  609. EMIT2(0x41, 0x52); /* push %r10 */
  610. EMIT2(0x41, 0x51); /* push %r9 */
  611. /* need to adjust jmp offset, since
  612. * pop %r9, pop %r10 take 4 bytes after call insn
  613. */
  614. jmp_offset += 4;
  615. }
  616. if (!imm32 || !is_simm32(jmp_offset)) {
  617. pr_err("unsupported bpf func %d addr %p image %p\n",
  618. imm32, func, image);
  619. return -EINVAL;
  620. }
  621. EMIT1_off32(0xE8, jmp_offset);
  622. if (seen_ld_abs) {
  623. EMIT2(0x41, 0x59); /* pop %r9 */
  624. EMIT2(0x41, 0x5A); /* pop %r10 */
  625. }
  626. break;
  627. /* cond jump */
  628. case BPF_JMP | BPF_JEQ | BPF_X:
  629. case BPF_JMP | BPF_JNE | BPF_X:
  630. case BPF_JMP | BPF_JGT | BPF_X:
  631. case BPF_JMP | BPF_JGE | BPF_X:
  632. case BPF_JMP | BPF_JSGT | BPF_X:
  633. case BPF_JMP | BPF_JSGE | BPF_X:
  634. /* cmp dst_reg, src_reg */
  635. EMIT3(add_2mod(0x48, dst_reg, src_reg), 0x39,
  636. add_2reg(0xC0, dst_reg, src_reg));
  637. goto emit_cond_jmp;
  638. case BPF_JMP | BPF_JSET | BPF_X:
  639. /* test dst_reg, src_reg */
  640. EMIT3(add_2mod(0x48, dst_reg, src_reg), 0x85,
  641. add_2reg(0xC0, dst_reg, src_reg));
  642. goto emit_cond_jmp;
  643. case BPF_JMP | BPF_JSET | BPF_K:
  644. /* test dst_reg, imm32 */
  645. EMIT1(add_1mod(0x48, dst_reg));
  646. EMIT2_off32(0xF7, add_1reg(0xC0, dst_reg), imm32);
  647. goto emit_cond_jmp;
  648. case BPF_JMP | BPF_JEQ | BPF_K:
  649. case BPF_JMP | BPF_JNE | BPF_K:
  650. case BPF_JMP | BPF_JGT | BPF_K:
  651. case BPF_JMP | BPF_JGE | BPF_K:
  652. case BPF_JMP | BPF_JSGT | BPF_K:
  653. case BPF_JMP | BPF_JSGE | BPF_K:
  654. /* cmp dst_reg, imm8/32 */
  655. EMIT1(add_1mod(0x48, dst_reg));
  656. if (is_imm8(imm32))
  657. EMIT3(0x83, add_1reg(0xF8, dst_reg), imm32);
  658. else
  659. EMIT2_off32(0x81, add_1reg(0xF8, dst_reg), imm32);
  660. emit_cond_jmp: /* convert BPF opcode to x86 */
  661. switch (BPF_OP(insn->code)) {
  662. case BPF_JEQ:
  663. jmp_cond = X86_JE;
  664. break;
  665. case BPF_JSET:
  666. case BPF_JNE:
  667. jmp_cond = X86_JNE;
  668. break;
  669. case BPF_JGT:
  670. /* GT is unsigned '>', JA in x86 */
  671. jmp_cond = X86_JA;
  672. break;
  673. case BPF_JGE:
  674. /* GE is unsigned '>=', JAE in x86 */
  675. jmp_cond = X86_JAE;
  676. break;
  677. case BPF_JSGT:
  678. /* signed '>', GT in x86 */
  679. jmp_cond = X86_JG;
  680. break;
  681. case BPF_JSGE:
  682. /* signed '>=', GE in x86 */
  683. jmp_cond = X86_JGE;
  684. break;
  685. default: /* to silence gcc warning */
  686. return -EFAULT;
  687. }
  688. jmp_offset = addrs[i + insn->off] - addrs[i];
  689. if (is_imm8(jmp_offset)) {
  690. EMIT2(jmp_cond, jmp_offset);
  691. } else if (is_simm32(jmp_offset)) {
  692. EMIT2_off32(0x0F, jmp_cond + 0x10, jmp_offset);
  693. } else {
  694. pr_err("cond_jmp gen bug %llx\n", jmp_offset);
  695. return -EFAULT;
  696. }
  697. break;
  698. case BPF_JMP | BPF_JA:
  699. jmp_offset = addrs[i + insn->off] - addrs[i];
  700. if (!jmp_offset)
  701. /* optimize out nop jumps */
  702. break;
  703. emit_jmp:
  704. if (is_imm8(jmp_offset)) {
  705. EMIT2(0xEB, jmp_offset);
  706. } else if (is_simm32(jmp_offset)) {
  707. EMIT1_off32(0xE9, jmp_offset);
  708. } else {
  709. pr_err("jmp gen bug %llx\n", jmp_offset);
  710. return -EFAULT;
  711. }
  712. break;
  713. case BPF_LD | BPF_IND | BPF_W:
  714. func = sk_load_word;
  715. goto common_load;
  716. case BPF_LD | BPF_ABS | BPF_W:
  717. func = CHOOSE_LOAD_FUNC(imm32, sk_load_word);
  718. common_load:
  719. ctx->seen_ld_abs = seen_ld_abs = true;
  720. jmp_offset = func - (image + addrs[i]);
  721. if (!func || !is_simm32(jmp_offset)) {
  722. pr_err("unsupported bpf func %d addr %p image %p\n",
  723. imm32, func, image);
  724. return -EINVAL;
  725. }
  726. if (BPF_MODE(insn->code) == BPF_ABS) {
  727. /* mov %esi, imm32 */
  728. EMIT1_off32(0xBE, imm32);
  729. } else {
  730. /* mov %rsi, src_reg */
  731. EMIT_mov(BPF_REG_2, src_reg);
  732. if (imm32) {
  733. if (is_imm8(imm32))
  734. /* add %esi, imm8 */
  735. EMIT3(0x83, 0xC6, imm32);
  736. else
  737. /* add %esi, imm32 */
  738. EMIT2_off32(0x81, 0xC6, imm32);
  739. }
  740. }
  741. /* skb pointer is in R6 (%rbx), it will be copied into
  742. * %rdi if skb_copy_bits() call is necessary.
  743. * sk_load_* helpers also use %r10 and %r9d.
  744. * See bpf_jit.S
  745. */
  746. EMIT1_off32(0xE8, jmp_offset); /* call */
  747. break;
  748. case BPF_LD | BPF_IND | BPF_H:
  749. func = sk_load_half;
  750. goto common_load;
  751. case BPF_LD | BPF_ABS | BPF_H:
  752. func = CHOOSE_LOAD_FUNC(imm32, sk_load_half);
  753. goto common_load;
  754. case BPF_LD | BPF_IND | BPF_B:
  755. func = sk_load_byte;
  756. goto common_load;
  757. case BPF_LD | BPF_ABS | BPF_B:
  758. func = CHOOSE_LOAD_FUNC(imm32, sk_load_byte);
  759. goto common_load;
  760. case BPF_JMP | BPF_EXIT:
  761. if (i != insn_cnt - 1) {
  762. jmp_offset = ctx->cleanup_addr - addrs[i];
  763. goto emit_jmp;
  764. }
  765. /* update cleanup_addr */
  766. ctx->cleanup_addr = proglen;
  767. /* mov rbx, qword ptr [rbp-X] */
  768. EMIT3_off32(0x48, 0x8B, 0x9D, -stacksize);
  769. /* mov r13, qword ptr [rbp-X] */
  770. EMIT3_off32(0x4C, 0x8B, 0xAD, -stacksize + 8);
  771. /* mov r14, qword ptr [rbp-X] */
  772. EMIT3_off32(0x4C, 0x8B, 0xB5, -stacksize + 16);
  773. /* mov r15, qword ptr [rbp-X] */
  774. EMIT3_off32(0x4C, 0x8B, 0xBD, -stacksize + 24);
  775. EMIT1(0xC9); /* leave */
  776. EMIT1(0xC3); /* ret */
  777. break;
  778. default:
  779. /* By design x64 JIT should support all BPF instructions
  780. * This error will be seen if new instruction was added
  781. * to interpreter, but not to JIT
  782. * or if there is junk in bpf_prog
  783. */
  784. pr_err("bpf_jit: unknown opcode %02x\n", insn->code);
  785. return -EINVAL;
  786. }
  787. ilen = prog - temp;
  788. if (ilen > BPF_MAX_INSN_SIZE) {
  789. pr_err("bpf_jit_compile fatal insn size error\n");
  790. return -EFAULT;
  791. }
  792. if (image) {
  793. if (unlikely(proglen + ilen > oldproglen)) {
  794. pr_err("bpf_jit_compile fatal error\n");
  795. return -EFAULT;
  796. }
  797. memcpy(image + proglen, temp, ilen);
  798. }
  799. proglen += ilen;
  800. addrs[i] = proglen;
  801. prog = temp;
  802. }
  803. return proglen;
  804. }
  805. void bpf_jit_compile(struct bpf_prog *prog)
  806. {
  807. }
  808. void bpf_int_jit_compile(struct bpf_prog *prog)
  809. {
  810. struct bpf_binary_header *header = NULL;
  811. int proglen, oldproglen = 0;
  812. struct jit_context ctx = {};
  813. u8 *image = NULL;
  814. int *addrs;
  815. int pass;
  816. int i;
  817. if (!bpf_jit_enable)
  818. return;
  819. if (!prog || !prog->len)
  820. return;
  821. addrs = kmalloc(prog->len * sizeof(*addrs), GFP_KERNEL);
  822. if (!addrs)
  823. return;
  824. /* Before first pass, make a rough estimation of addrs[]
  825. * each bpf instruction is translated to less than 64 bytes
  826. */
  827. for (proglen = 0, i = 0; i < prog->len; i++) {
  828. proglen += 64;
  829. addrs[i] = proglen;
  830. }
  831. ctx.cleanup_addr = proglen;
  832. for (pass = 0; pass < 10; pass++) {
  833. proglen = do_jit(prog, addrs, image, oldproglen, &ctx);
  834. if (proglen <= 0) {
  835. image = NULL;
  836. if (header)
  837. bpf_jit_binary_free(header);
  838. goto out;
  839. }
  840. if (image) {
  841. if (proglen != oldproglen) {
  842. pr_err("bpf_jit: proglen=%d != oldproglen=%d\n",
  843. proglen, oldproglen);
  844. goto out;
  845. }
  846. break;
  847. }
  848. if (proglen == oldproglen) {
  849. header = bpf_jit_binary_alloc(proglen, &image,
  850. 1, jit_fill_hole);
  851. if (!header)
  852. goto out;
  853. }
  854. oldproglen = proglen;
  855. }
  856. if (bpf_jit_enable > 1)
  857. bpf_jit_dump(prog->len, proglen, 0, image);
  858. if (image) {
  859. bpf_flush_icache(header, image + proglen);
  860. set_memory_ro((unsigned long)header, header->pages);
  861. prog->bpf_func = (void *)image;
  862. prog->jited = true;
  863. }
  864. out:
  865. kfree(addrs);
  866. }
  867. void bpf_jit_free(struct bpf_prog *fp)
  868. {
  869. unsigned long addr = (unsigned long)fp->bpf_func & PAGE_MASK;
  870. struct bpf_binary_header *header = (void *)addr;
  871. if (!fp->jited)
  872. goto free_filter;
  873. set_memory_rw(addr, header->pages);
  874. bpf_jit_binary_free(header);
  875. free_filter:
  876. bpf_prog_unlock_free(fp);
  877. }