bpf_jit_comp.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  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. break;
  486. case 32:
  487. /* emit 'bswap eax' to swap lower 4 bytes */
  488. if (is_ereg(dst_reg))
  489. EMIT2(0x41, 0x0F);
  490. else
  491. EMIT1(0x0F);
  492. EMIT1(add_1reg(0xC8, dst_reg));
  493. break;
  494. case 64:
  495. /* emit 'bswap rax' to swap 8 bytes */
  496. EMIT3(add_1mod(0x48, dst_reg), 0x0F,
  497. add_1reg(0xC8, dst_reg));
  498. break;
  499. }
  500. break;
  501. case BPF_ALU | BPF_END | BPF_FROM_LE:
  502. break;
  503. /* ST: *(u8*)(dst_reg + off) = imm */
  504. case BPF_ST | BPF_MEM | BPF_B:
  505. if (is_ereg(dst_reg))
  506. EMIT2(0x41, 0xC6);
  507. else
  508. EMIT1(0xC6);
  509. goto st;
  510. case BPF_ST | BPF_MEM | BPF_H:
  511. if (is_ereg(dst_reg))
  512. EMIT3(0x66, 0x41, 0xC7);
  513. else
  514. EMIT2(0x66, 0xC7);
  515. goto st;
  516. case BPF_ST | BPF_MEM | BPF_W:
  517. if (is_ereg(dst_reg))
  518. EMIT2(0x41, 0xC7);
  519. else
  520. EMIT1(0xC7);
  521. goto st;
  522. case BPF_ST | BPF_MEM | BPF_DW:
  523. EMIT2(add_1mod(0x48, dst_reg), 0xC7);
  524. st: if (is_imm8(insn->off))
  525. EMIT2(add_1reg(0x40, dst_reg), insn->off);
  526. else
  527. EMIT1_off32(add_1reg(0x80, dst_reg), insn->off);
  528. EMIT(imm32, bpf_size_to_x86_bytes(BPF_SIZE(insn->code)));
  529. break;
  530. /* STX: *(u8*)(dst_reg + off) = src_reg */
  531. case BPF_STX | BPF_MEM | BPF_B:
  532. /* emit 'mov byte ptr [rax + off], al' */
  533. if (is_ereg(dst_reg) || is_ereg(src_reg) ||
  534. /* have to add extra byte for x86 SIL, DIL regs */
  535. src_reg == BPF_REG_1 || src_reg == BPF_REG_2)
  536. EMIT2(add_2mod(0x40, dst_reg, src_reg), 0x88);
  537. else
  538. EMIT1(0x88);
  539. goto stx;
  540. case BPF_STX | BPF_MEM | BPF_H:
  541. if (is_ereg(dst_reg) || is_ereg(src_reg))
  542. EMIT3(0x66, add_2mod(0x40, dst_reg, src_reg), 0x89);
  543. else
  544. EMIT2(0x66, 0x89);
  545. goto stx;
  546. case BPF_STX | BPF_MEM | BPF_W:
  547. if (is_ereg(dst_reg) || is_ereg(src_reg))
  548. EMIT2(add_2mod(0x40, dst_reg, src_reg), 0x89);
  549. else
  550. EMIT1(0x89);
  551. goto stx;
  552. case BPF_STX | BPF_MEM | BPF_DW:
  553. EMIT2(add_2mod(0x48, dst_reg, src_reg), 0x89);
  554. stx: if (is_imm8(insn->off))
  555. EMIT2(add_2reg(0x40, dst_reg, src_reg), insn->off);
  556. else
  557. EMIT1_off32(add_2reg(0x80, dst_reg, src_reg),
  558. insn->off);
  559. break;
  560. /* LDX: dst_reg = *(u8*)(src_reg + off) */
  561. case BPF_LDX | BPF_MEM | BPF_B:
  562. /* emit 'movzx rax, byte ptr [rax + off]' */
  563. EMIT3(add_2mod(0x48, src_reg, dst_reg), 0x0F, 0xB6);
  564. goto ldx;
  565. case BPF_LDX | BPF_MEM | BPF_H:
  566. /* emit 'movzx rax, word ptr [rax + off]' */
  567. EMIT3(add_2mod(0x48, src_reg, dst_reg), 0x0F, 0xB7);
  568. goto ldx;
  569. case BPF_LDX | BPF_MEM | BPF_W:
  570. /* emit 'mov eax, dword ptr [rax+0x14]' */
  571. if (is_ereg(dst_reg) || is_ereg(src_reg))
  572. EMIT2(add_2mod(0x40, src_reg, dst_reg), 0x8B);
  573. else
  574. EMIT1(0x8B);
  575. goto ldx;
  576. case BPF_LDX | BPF_MEM | BPF_DW:
  577. /* emit 'mov rax, qword ptr [rax+0x14]' */
  578. EMIT2(add_2mod(0x48, src_reg, dst_reg), 0x8B);
  579. ldx: /* if insn->off == 0 we can save one extra byte, but
  580. * special case of x86 r13 which always needs an offset
  581. * is not worth the hassle
  582. */
  583. if (is_imm8(insn->off))
  584. EMIT2(add_2reg(0x40, src_reg, dst_reg), insn->off);
  585. else
  586. EMIT1_off32(add_2reg(0x80, src_reg, dst_reg),
  587. insn->off);
  588. break;
  589. /* STX XADD: lock *(u32*)(dst_reg + off) += src_reg */
  590. case BPF_STX | BPF_XADD | BPF_W:
  591. /* emit 'lock add dword ptr [rax + off], eax' */
  592. if (is_ereg(dst_reg) || is_ereg(src_reg))
  593. EMIT3(0xF0, add_2mod(0x40, dst_reg, src_reg), 0x01);
  594. else
  595. EMIT2(0xF0, 0x01);
  596. goto xadd;
  597. case BPF_STX | BPF_XADD | BPF_DW:
  598. EMIT3(0xF0, add_2mod(0x48, dst_reg, src_reg), 0x01);
  599. xadd: if (is_imm8(insn->off))
  600. EMIT2(add_2reg(0x40, dst_reg, src_reg), insn->off);
  601. else
  602. EMIT1_off32(add_2reg(0x80, dst_reg, src_reg),
  603. insn->off);
  604. break;
  605. /* call */
  606. case BPF_JMP | BPF_CALL:
  607. func = (u8 *) __bpf_call_base + imm32;
  608. jmp_offset = func - (image + addrs[i]);
  609. if (seen_ld_abs) {
  610. EMIT2(0x41, 0x52); /* push %r10 */
  611. EMIT2(0x41, 0x51); /* push %r9 */
  612. /* need to adjust jmp offset, since
  613. * pop %r9, pop %r10 take 4 bytes after call insn
  614. */
  615. jmp_offset += 4;
  616. }
  617. if (!imm32 || !is_simm32(jmp_offset)) {
  618. pr_err("unsupported bpf func %d addr %p image %p\n",
  619. imm32, func, image);
  620. return -EINVAL;
  621. }
  622. EMIT1_off32(0xE8, jmp_offset);
  623. if (seen_ld_abs) {
  624. EMIT2(0x41, 0x59); /* pop %r9 */
  625. EMIT2(0x41, 0x5A); /* pop %r10 */
  626. }
  627. break;
  628. /* cond jump */
  629. case BPF_JMP | BPF_JEQ | BPF_X:
  630. case BPF_JMP | BPF_JNE | BPF_X:
  631. case BPF_JMP | BPF_JGT | BPF_X:
  632. case BPF_JMP | BPF_JGE | BPF_X:
  633. case BPF_JMP | BPF_JSGT | BPF_X:
  634. case BPF_JMP | BPF_JSGE | BPF_X:
  635. /* cmp dst_reg, src_reg */
  636. EMIT3(add_2mod(0x48, dst_reg, src_reg), 0x39,
  637. add_2reg(0xC0, dst_reg, src_reg));
  638. goto emit_cond_jmp;
  639. case BPF_JMP | BPF_JSET | BPF_X:
  640. /* test dst_reg, src_reg */
  641. EMIT3(add_2mod(0x48, dst_reg, src_reg), 0x85,
  642. add_2reg(0xC0, dst_reg, src_reg));
  643. goto emit_cond_jmp;
  644. case BPF_JMP | BPF_JSET | BPF_K:
  645. /* test dst_reg, imm32 */
  646. EMIT1(add_1mod(0x48, dst_reg));
  647. EMIT2_off32(0xF7, add_1reg(0xC0, dst_reg), imm32);
  648. goto emit_cond_jmp;
  649. case BPF_JMP | BPF_JEQ | BPF_K:
  650. case BPF_JMP | BPF_JNE | BPF_K:
  651. case BPF_JMP | BPF_JGT | BPF_K:
  652. case BPF_JMP | BPF_JGE | BPF_K:
  653. case BPF_JMP | BPF_JSGT | BPF_K:
  654. case BPF_JMP | BPF_JSGE | BPF_K:
  655. /* cmp dst_reg, imm8/32 */
  656. EMIT1(add_1mod(0x48, dst_reg));
  657. if (is_imm8(imm32))
  658. EMIT3(0x83, add_1reg(0xF8, dst_reg), imm32);
  659. else
  660. EMIT2_off32(0x81, add_1reg(0xF8, dst_reg), imm32);
  661. emit_cond_jmp: /* convert BPF opcode to x86 */
  662. switch (BPF_OP(insn->code)) {
  663. case BPF_JEQ:
  664. jmp_cond = X86_JE;
  665. break;
  666. case BPF_JSET:
  667. case BPF_JNE:
  668. jmp_cond = X86_JNE;
  669. break;
  670. case BPF_JGT:
  671. /* GT is unsigned '>', JA in x86 */
  672. jmp_cond = X86_JA;
  673. break;
  674. case BPF_JGE:
  675. /* GE is unsigned '>=', JAE in x86 */
  676. jmp_cond = X86_JAE;
  677. break;
  678. case BPF_JSGT:
  679. /* signed '>', GT in x86 */
  680. jmp_cond = X86_JG;
  681. break;
  682. case BPF_JSGE:
  683. /* signed '>=', GE in x86 */
  684. jmp_cond = X86_JGE;
  685. break;
  686. default: /* to silence gcc warning */
  687. return -EFAULT;
  688. }
  689. jmp_offset = addrs[i + insn->off] - addrs[i];
  690. if (is_imm8(jmp_offset)) {
  691. EMIT2(jmp_cond, jmp_offset);
  692. } else if (is_simm32(jmp_offset)) {
  693. EMIT2_off32(0x0F, jmp_cond + 0x10, jmp_offset);
  694. } else {
  695. pr_err("cond_jmp gen bug %llx\n", jmp_offset);
  696. return -EFAULT;
  697. }
  698. break;
  699. case BPF_JMP | BPF_JA:
  700. jmp_offset = addrs[i + insn->off] - addrs[i];
  701. if (!jmp_offset)
  702. /* optimize out nop jumps */
  703. break;
  704. emit_jmp:
  705. if (is_imm8(jmp_offset)) {
  706. EMIT2(0xEB, jmp_offset);
  707. } else if (is_simm32(jmp_offset)) {
  708. EMIT1_off32(0xE9, jmp_offset);
  709. } else {
  710. pr_err("jmp gen bug %llx\n", jmp_offset);
  711. return -EFAULT;
  712. }
  713. break;
  714. case BPF_LD | BPF_IND | BPF_W:
  715. func = sk_load_word;
  716. goto common_load;
  717. case BPF_LD | BPF_ABS | BPF_W:
  718. func = CHOOSE_LOAD_FUNC(imm32, sk_load_word);
  719. common_load:
  720. ctx->seen_ld_abs = seen_ld_abs = true;
  721. jmp_offset = func - (image + addrs[i]);
  722. if (!func || !is_simm32(jmp_offset)) {
  723. pr_err("unsupported bpf func %d addr %p image %p\n",
  724. imm32, func, image);
  725. return -EINVAL;
  726. }
  727. if (BPF_MODE(insn->code) == BPF_ABS) {
  728. /* mov %esi, imm32 */
  729. EMIT1_off32(0xBE, imm32);
  730. } else {
  731. /* mov %rsi, src_reg */
  732. EMIT_mov(BPF_REG_2, src_reg);
  733. if (imm32) {
  734. if (is_imm8(imm32))
  735. /* add %esi, imm8 */
  736. EMIT3(0x83, 0xC6, imm32);
  737. else
  738. /* add %esi, imm32 */
  739. EMIT2_off32(0x81, 0xC6, imm32);
  740. }
  741. }
  742. /* skb pointer is in R6 (%rbx), it will be copied into
  743. * %rdi if skb_copy_bits() call is necessary.
  744. * sk_load_* helpers also use %r10 and %r9d.
  745. * See bpf_jit.S
  746. */
  747. EMIT1_off32(0xE8, jmp_offset); /* call */
  748. break;
  749. case BPF_LD | BPF_IND | BPF_H:
  750. func = sk_load_half;
  751. goto common_load;
  752. case BPF_LD | BPF_ABS | BPF_H:
  753. func = CHOOSE_LOAD_FUNC(imm32, sk_load_half);
  754. goto common_load;
  755. case BPF_LD | BPF_IND | BPF_B:
  756. func = sk_load_byte;
  757. goto common_load;
  758. case BPF_LD | BPF_ABS | BPF_B:
  759. func = CHOOSE_LOAD_FUNC(imm32, sk_load_byte);
  760. goto common_load;
  761. case BPF_JMP | BPF_EXIT:
  762. if (seen_exit) {
  763. jmp_offset = ctx->cleanup_addr - addrs[i];
  764. goto emit_jmp;
  765. }
  766. seen_exit = true;
  767. /* update cleanup_addr */
  768. ctx->cleanup_addr = proglen;
  769. /* mov rbx, qword ptr [rbp-X] */
  770. EMIT3_off32(0x48, 0x8B, 0x9D, -stacksize);
  771. /* mov r13, qword ptr [rbp-X] */
  772. EMIT3_off32(0x4C, 0x8B, 0xAD, -stacksize + 8);
  773. /* mov r14, qword ptr [rbp-X] */
  774. EMIT3_off32(0x4C, 0x8B, 0xB5, -stacksize + 16);
  775. /* mov r15, qword ptr [rbp-X] */
  776. EMIT3_off32(0x4C, 0x8B, 0xBD, -stacksize + 24);
  777. EMIT1(0xC9); /* leave */
  778. EMIT1(0xC3); /* ret */
  779. break;
  780. default:
  781. /* By design x64 JIT should support all BPF instructions
  782. * This error will be seen if new instruction was added
  783. * to interpreter, but not to JIT
  784. * or if there is junk in bpf_prog
  785. */
  786. pr_err("bpf_jit: unknown opcode %02x\n", insn->code);
  787. return -EINVAL;
  788. }
  789. ilen = prog - temp;
  790. if (ilen > BPF_MAX_INSN_SIZE) {
  791. pr_err("bpf_jit_compile fatal insn size error\n");
  792. return -EFAULT;
  793. }
  794. if (image) {
  795. if (unlikely(proglen + ilen > oldproglen)) {
  796. pr_err("bpf_jit_compile fatal error\n");
  797. return -EFAULT;
  798. }
  799. memcpy(image + proglen, temp, ilen);
  800. }
  801. proglen += ilen;
  802. addrs[i] = proglen;
  803. prog = temp;
  804. }
  805. return proglen;
  806. }
  807. void bpf_jit_compile(struct bpf_prog *prog)
  808. {
  809. }
  810. void bpf_int_jit_compile(struct bpf_prog *prog)
  811. {
  812. struct bpf_binary_header *header = NULL;
  813. int proglen, oldproglen = 0;
  814. struct jit_context ctx = {};
  815. u8 *image = NULL;
  816. int *addrs;
  817. int pass;
  818. int i;
  819. if (!bpf_jit_enable)
  820. return;
  821. if (!prog || !prog->len)
  822. return;
  823. addrs = kmalloc(prog->len * sizeof(*addrs), GFP_KERNEL);
  824. if (!addrs)
  825. return;
  826. /* Before first pass, make a rough estimation of addrs[]
  827. * each bpf instruction is translated to less than 64 bytes
  828. */
  829. for (proglen = 0, i = 0; i < prog->len; i++) {
  830. proglen += 64;
  831. addrs[i] = proglen;
  832. }
  833. ctx.cleanup_addr = proglen;
  834. for (pass = 0; pass < 10; pass++) {
  835. proglen = do_jit(prog, addrs, image, oldproglen, &ctx);
  836. if (proglen <= 0) {
  837. image = NULL;
  838. if (header)
  839. bpf_jit_binary_free(header);
  840. goto out;
  841. }
  842. if (image) {
  843. if (proglen != oldproglen) {
  844. pr_err("bpf_jit: proglen=%d != oldproglen=%d\n",
  845. proglen, oldproglen);
  846. goto out;
  847. }
  848. break;
  849. }
  850. if (proglen == oldproglen) {
  851. header = bpf_jit_binary_alloc(proglen, &image,
  852. 1, jit_fill_hole);
  853. if (!header)
  854. goto out;
  855. }
  856. oldproglen = proglen;
  857. }
  858. if (bpf_jit_enable > 1)
  859. bpf_jit_dump(prog->len, proglen, 0, image);
  860. if (image) {
  861. bpf_flush_icache(header, image + proglen);
  862. set_memory_ro((unsigned long)header, header->pages);
  863. prog->bpf_func = (void *)image;
  864. prog->jited = true;
  865. }
  866. out:
  867. kfree(addrs);
  868. }
  869. void bpf_jit_free(struct bpf_prog *fp)
  870. {
  871. unsigned long addr = (unsigned long)fp->bpf_func & PAGE_MASK;
  872. struct bpf_binary_header *header = (void *)addr;
  873. if (!fp->jited)
  874. goto free_filter;
  875. set_memory_rw(addr, header->pages);
  876. bpf_jit_binary_free(header);
  877. free_filter:
  878. bpf_prog_unlock_free(fp);
  879. }