bpf_jit_comp.c 27 KB

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