bpf_jit_comp.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. /* bpf_jit_comp.c : BPF JIT compiler
  2. *
  3. * Copyright (C) 2011 Eric Dumazet (eric.dumazet@gmail.com)
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; version 2
  8. * of the License.
  9. */
  10. #include <linux/moduleloader.h>
  11. #include <asm/cacheflush.h>
  12. #include <linux/netdevice.h>
  13. #include <linux/filter.h>
  14. /*
  15. * Conventions :
  16. * EAX : BPF A accumulator
  17. * EBX : BPF X accumulator
  18. * RDI : pointer to skb (first argument given to JIT function)
  19. * RBP : frame pointer (even if CONFIG_FRAME_POINTER=n)
  20. * ECX,EDX,ESI : scratch registers
  21. * r9d : skb->len - skb->data_len (headlen)
  22. * r8 : skb->data
  23. * -8(RBP) : saved RBX value
  24. * -16(RBP)..-80(RBP) : BPF_MEMWORDS values
  25. */
  26. int bpf_jit_enable __read_mostly;
  27. /*
  28. * assembly code in arch/x86/net/bpf_jit.S
  29. */
  30. extern u8 sk_load_word[], sk_load_half[], sk_load_byte[], sk_load_byte_msh[];
  31. extern u8 sk_load_word_positive_offset[], sk_load_half_positive_offset[];
  32. extern u8 sk_load_byte_positive_offset[], sk_load_byte_msh_positive_offset[];
  33. extern u8 sk_load_word_negative_offset[], sk_load_half_negative_offset[];
  34. extern u8 sk_load_byte_negative_offset[], sk_load_byte_msh_negative_offset[];
  35. static inline u8 *emit_code(u8 *ptr, u32 bytes, unsigned int len)
  36. {
  37. if (len == 1)
  38. *ptr = bytes;
  39. else if (len == 2)
  40. *(u16 *)ptr = bytes;
  41. else {
  42. *(u32 *)ptr = bytes;
  43. barrier();
  44. }
  45. return ptr + len;
  46. }
  47. #define EMIT(bytes, len) do { prog = emit_code(prog, bytes, len); } while (0)
  48. #define EMIT1(b1) EMIT(b1, 1)
  49. #define EMIT2(b1, b2) EMIT((b1) + ((b2) << 8), 2)
  50. #define EMIT3(b1, b2, b3) EMIT((b1) + ((b2) << 8) + ((b3) << 16), 3)
  51. #define EMIT4(b1, b2, b3, b4) EMIT((b1) + ((b2) << 8) + ((b3) << 16) + ((b4) << 24), 4)
  52. #define EMIT1_off32(b1, off) do { EMIT1(b1); EMIT(off, 4);} while (0)
  53. #define CLEAR_A() EMIT2(0x31, 0xc0) /* xor %eax,%eax */
  54. #define CLEAR_X() EMIT2(0x31, 0xdb) /* xor %ebx,%ebx */
  55. static inline bool is_imm8(int value)
  56. {
  57. return value <= 127 && value >= -128;
  58. }
  59. static inline bool is_near(int offset)
  60. {
  61. return offset <= 127 && offset >= -128;
  62. }
  63. #define EMIT_JMP(offset) \
  64. do { \
  65. if (offset) { \
  66. if (is_near(offset)) \
  67. EMIT2(0xeb, offset); /* jmp .+off8 */ \
  68. else \
  69. EMIT1_off32(0xe9, offset); /* jmp .+off32 */ \
  70. } \
  71. } while (0)
  72. /* list of x86 cond jumps opcodes (. + s8)
  73. * Add 0x10 (and an extra 0x0f) to generate far jumps (. + s32)
  74. */
  75. #define X86_JB 0x72
  76. #define X86_JAE 0x73
  77. #define X86_JE 0x74
  78. #define X86_JNE 0x75
  79. #define X86_JBE 0x76
  80. #define X86_JA 0x77
  81. #define EMIT_COND_JMP(op, offset) \
  82. do { \
  83. if (is_near(offset)) \
  84. EMIT2(op, offset); /* jxx .+off8 */ \
  85. else { \
  86. EMIT2(0x0f, op + 0x10); \
  87. EMIT(offset, 4); /* jxx .+off32 */ \
  88. } \
  89. } while (0)
  90. #define COND_SEL(CODE, TOP, FOP) \
  91. case CODE: \
  92. t_op = TOP; \
  93. f_op = FOP; \
  94. goto cond_branch
  95. #define SEEN_DATAREF 1 /* might call external helpers */
  96. #define SEEN_XREG 2 /* ebx is used */
  97. #define SEEN_MEM 4 /* use mem[] for temporary storage */
  98. static inline void bpf_flush_icache(void *start, void *end)
  99. {
  100. mm_segment_t old_fs = get_fs();
  101. set_fs(KERNEL_DS);
  102. smp_wmb();
  103. flush_icache_range((unsigned long)start, (unsigned long)end);
  104. set_fs(old_fs);
  105. }
  106. #define CHOOSE_LOAD_FUNC(K, func) \
  107. ((int)K < 0 ? ((int)K >= SKF_LL_OFF ? func##_negative_offset : func) : func##_positive_offset)
  108. void bpf_jit_compile(struct sk_filter *fp)
  109. {
  110. u8 temp[64];
  111. u8 *prog;
  112. unsigned int proglen, oldproglen = 0;
  113. int ilen, i;
  114. int t_offset, f_offset;
  115. u8 t_op, f_op, seen = 0, pass;
  116. u8 *image = NULL;
  117. u8 *func;
  118. int pc_ret0 = -1; /* bpf index of first RET #0 instruction (if any) */
  119. unsigned int cleanup_addr; /* epilogue code offset */
  120. unsigned int *addrs;
  121. const struct sock_filter *filter = fp->insns;
  122. int flen = fp->len;
  123. if (!bpf_jit_enable)
  124. return;
  125. addrs = kmalloc(flen * sizeof(*addrs), GFP_KERNEL);
  126. if (addrs == NULL)
  127. return;
  128. /* Before first pass, make a rough estimation of addrs[]
  129. * each bpf instruction is translated to less than 64 bytes
  130. */
  131. for (proglen = 0, i = 0; i < flen; i++) {
  132. proglen += 64;
  133. addrs[i] = proglen;
  134. }
  135. cleanup_addr = proglen; /* epilogue address */
  136. /* JITed image shrinks with every pass and the loop iterates
  137. * until the image stops shrinking. Very large bpf programs
  138. * may converge on the last pass. In such case do one more
  139. * pass to emit the final image
  140. */
  141. for (pass = 0; pass < 10 || image; pass++) {
  142. u8 seen_or_pass0 = (pass == 0) ? (SEEN_XREG | SEEN_DATAREF | SEEN_MEM) : seen;
  143. /* no prologue/epilogue for trivial filters (RET something) */
  144. proglen = 0;
  145. prog = temp;
  146. if (seen_or_pass0) {
  147. EMIT4(0x55, 0x48, 0x89, 0xe5); /* push %rbp; mov %rsp,%rbp */
  148. EMIT4(0x48, 0x83, 0xec, 96); /* subq $96,%rsp */
  149. /* note : must save %rbx in case bpf_error is hit */
  150. if (seen_or_pass0 & (SEEN_XREG | SEEN_DATAREF))
  151. EMIT4(0x48, 0x89, 0x5d, 0xf8); /* mov %rbx, -8(%rbp) */
  152. if (seen_or_pass0 & SEEN_XREG)
  153. CLEAR_X(); /* make sure we dont leek kernel memory */
  154. /*
  155. * If this filter needs to access skb data,
  156. * loads r9 and r8 with :
  157. * r9 = skb->len - skb->data_len
  158. * r8 = skb->data
  159. */
  160. if (seen_or_pass0 & SEEN_DATAREF) {
  161. if (offsetof(struct sk_buff, len) <= 127)
  162. /* mov off8(%rdi),%r9d */
  163. EMIT4(0x44, 0x8b, 0x4f, offsetof(struct sk_buff, len));
  164. else {
  165. /* mov off32(%rdi),%r9d */
  166. EMIT3(0x44, 0x8b, 0x8f);
  167. EMIT(offsetof(struct sk_buff, len), 4);
  168. }
  169. if (is_imm8(offsetof(struct sk_buff, data_len)))
  170. /* sub off8(%rdi),%r9d */
  171. EMIT4(0x44, 0x2b, 0x4f, offsetof(struct sk_buff, data_len));
  172. else {
  173. EMIT3(0x44, 0x2b, 0x8f);
  174. EMIT(offsetof(struct sk_buff, data_len), 4);
  175. }
  176. if (is_imm8(offsetof(struct sk_buff, data)))
  177. /* mov off8(%rdi),%r8 */
  178. EMIT4(0x4c, 0x8b, 0x47, offsetof(struct sk_buff, data));
  179. else {
  180. /* mov off32(%rdi),%r8 */
  181. EMIT3(0x4c, 0x8b, 0x87);
  182. EMIT(offsetof(struct sk_buff, data), 4);
  183. }
  184. }
  185. }
  186. switch (filter[0].code) {
  187. case BPF_S_RET_K:
  188. case BPF_S_LD_W_LEN:
  189. case BPF_S_ANC_PROTOCOL:
  190. case BPF_S_ANC_IFINDEX:
  191. case BPF_S_ANC_MARK:
  192. case BPF_S_ANC_RXHASH:
  193. case BPF_S_ANC_CPU:
  194. case BPF_S_ANC_QUEUE:
  195. case BPF_S_LD_W_ABS:
  196. case BPF_S_LD_H_ABS:
  197. case BPF_S_LD_B_ABS:
  198. /* first instruction sets A register (or is RET 'constant') */
  199. break;
  200. default:
  201. /* make sure we dont leak kernel information to user */
  202. CLEAR_A(); /* A = 0 */
  203. }
  204. for (i = 0; i < flen; i++) {
  205. unsigned int K = filter[i].k;
  206. switch (filter[i].code) {
  207. case BPF_S_ALU_ADD_X: /* A += X; */
  208. seen |= SEEN_XREG;
  209. EMIT2(0x01, 0xd8); /* add %ebx,%eax */
  210. break;
  211. case BPF_S_ALU_ADD_K: /* A += K; */
  212. if (!K)
  213. break;
  214. if (is_imm8(K))
  215. EMIT3(0x83, 0xc0, K); /* add imm8,%eax */
  216. else
  217. EMIT1_off32(0x05, K); /* add imm32,%eax */
  218. break;
  219. case BPF_S_ALU_SUB_X: /* A -= X; */
  220. seen |= SEEN_XREG;
  221. EMIT2(0x29, 0xd8); /* sub %ebx,%eax */
  222. break;
  223. case BPF_S_ALU_SUB_K: /* A -= K */
  224. if (!K)
  225. break;
  226. if (is_imm8(K))
  227. EMIT3(0x83, 0xe8, K); /* sub imm8,%eax */
  228. else
  229. EMIT1_off32(0x2d, K); /* sub imm32,%eax */
  230. break;
  231. case BPF_S_ALU_MUL_X: /* A *= X; */
  232. seen |= SEEN_XREG;
  233. EMIT3(0x0f, 0xaf, 0xc3); /* imul %ebx,%eax */
  234. break;
  235. case BPF_S_ALU_MUL_K: /* A *= K */
  236. if (is_imm8(K))
  237. EMIT3(0x6b, 0xc0, K); /* imul imm8,%eax,%eax */
  238. else {
  239. EMIT2(0x69, 0xc0); /* imul imm32,%eax */
  240. EMIT(K, 4);
  241. }
  242. break;
  243. case BPF_S_ALU_DIV_X: /* A /= X; */
  244. seen |= SEEN_XREG;
  245. EMIT2(0x85, 0xdb); /* test %ebx,%ebx */
  246. if (pc_ret0 > 0) {
  247. /* addrs[pc_ret0 - 1] is start address of target
  248. * (addrs[i] - 4) is the address following this jmp
  249. * ("xor %edx,%edx; div %ebx" being 4 bytes long)
  250. */
  251. EMIT_COND_JMP(X86_JE, addrs[pc_ret0 - 1] -
  252. (addrs[i] - 4));
  253. } else {
  254. EMIT_COND_JMP(X86_JNE, 2 + 5);
  255. CLEAR_A();
  256. EMIT1_off32(0xe9, cleanup_addr - (addrs[i] - 4)); /* jmp .+off32 */
  257. }
  258. EMIT4(0x31, 0xd2, 0xf7, 0xf3); /* xor %edx,%edx; div %ebx */
  259. break;
  260. case BPF_S_ALU_DIV_K: /* A = reciprocal_divide(A, K); */
  261. EMIT3(0x48, 0x69, 0xc0); /* imul imm32,%rax,%rax */
  262. EMIT(K, 4);
  263. EMIT4(0x48, 0xc1, 0xe8, 0x20); /* shr $0x20,%rax */
  264. break;
  265. case BPF_S_ALU_AND_X:
  266. seen |= SEEN_XREG;
  267. EMIT2(0x21, 0xd8); /* and %ebx,%eax */
  268. break;
  269. case BPF_S_ALU_AND_K:
  270. if (K >= 0xFFFFFF00) {
  271. EMIT2(0x24, K & 0xFF); /* and imm8,%al */
  272. } else if (K >= 0xFFFF0000) {
  273. EMIT2(0x66, 0x25); /* and imm16,%ax */
  274. EMIT(K, 2);
  275. } else {
  276. EMIT1_off32(0x25, K); /* and imm32,%eax */
  277. }
  278. break;
  279. case BPF_S_ALU_OR_X:
  280. seen |= SEEN_XREG;
  281. EMIT2(0x09, 0xd8); /* or %ebx,%eax */
  282. break;
  283. case BPF_S_ALU_OR_K:
  284. if (is_imm8(K))
  285. EMIT3(0x83, 0xc8, K); /* or imm8,%eax */
  286. else
  287. EMIT1_off32(0x0d, K); /* or imm32,%eax */
  288. break;
  289. case BPF_S_ALU_LSH_X: /* A <<= X; */
  290. seen |= SEEN_XREG;
  291. EMIT4(0x89, 0xd9, 0xd3, 0xe0); /* mov %ebx,%ecx; shl %cl,%eax */
  292. break;
  293. case BPF_S_ALU_LSH_K:
  294. if (K == 0)
  295. break;
  296. else if (K == 1)
  297. EMIT2(0xd1, 0xe0); /* shl %eax */
  298. else
  299. EMIT3(0xc1, 0xe0, K);
  300. break;
  301. case BPF_S_ALU_RSH_X: /* A >>= X; */
  302. seen |= SEEN_XREG;
  303. EMIT4(0x89, 0xd9, 0xd3, 0xe8); /* mov %ebx,%ecx; shr %cl,%eax */
  304. break;
  305. case BPF_S_ALU_RSH_K: /* A >>= K; */
  306. if (K == 0)
  307. break;
  308. else if (K == 1)
  309. EMIT2(0xd1, 0xe8); /* shr %eax */
  310. else
  311. EMIT3(0xc1, 0xe8, K);
  312. break;
  313. case BPF_S_ALU_NEG:
  314. EMIT2(0xf7, 0xd8); /* neg %eax */
  315. break;
  316. case BPF_S_RET_K:
  317. if (!K) {
  318. if (pc_ret0 == -1)
  319. pc_ret0 = i;
  320. CLEAR_A();
  321. } else {
  322. EMIT1_off32(0xb8, K); /* mov $imm32,%eax */
  323. }
  324. /* fallinto */
  325. case BPF_S_RET_A:
  326. if (seen_or_pass0) {
  327. if (i != flen - 1) {
  328. EMIT_JMP(cleanup_addr - addrs[i]);
  329. break;
  330. }
  331. if (seen_or_pass0 & SEEN_XREG)
  332. EMIT4(0x48, 0x8b, 0x5d, 0xf8); /* mov -8(%rbp),%rbx */
  333. EMIT1(0xc9); /* leaveq */
  334. }
  335. EMIT1(0xc3); /* ret */
  336. break;
  337. case BPF_S_MISC_TAX: /* X = A */
  338. seen |= SEEN_XREG;
  339. EMIT2(0x89, 0xc3); /* mov %eax,%ebx */
  340. break;
  341. case BPF_S_MISC_TXA: /* A = X */
  342. seen |= SEEN_XREG;
  343. EMIT2(0x89, 0xd8); /* mov %ebx,%eax */
  344. break;
  345. case BPF_S_LD_IMM: /* A = K */
  346. if (!K)
  347. CLEAR_A();
  348. else
  349. EMIT1_off32(0xb8, K); /* mov $imm32,%eax */
  350. break;
  351. case BPF_S_LDX_IMM: /* X = K */
  352. seen |= SEEN_XREG;
  353. if (!K)
  354. CLEAR_X();
  355. else
  356. EMIT1_off32(0xbb, K); /* mov $imm32,%ebx */
  357. break;
  358. case BPF_S_LD_MEM: /* A = mem[K] : mov off8(%rbp),%eax */
  359. seen |= SEEN_MEM;
  360. EMIT3(0x8b, 0x45, 0xf0 - K*4);
  361. break;
  362. case BPF_S_LDX_MEM: /* X = mem[K] : mov off8(%rbp),%ebx */
  363. seen |= SEEN_XREG | SEEN_MEM;
  364. EMIT3(0x8b, 0x5d, 0xf0 - K*4);
  365. break;
  366. case BPF_S_ST: /* mem[K] = A : mov %eax,off8(%rbp) */
  367. seen |= SEEN_MEM;
  368. EMIT3(0x89, 0x45, 0xf0 - K*4);
  369. break;
  370. case BPF_S_STX: /* mem[K] = X : mov %ebx,off8(%rbp) */
  371. seen |= SEEN_XREG | SEEN_MEM;
  372. EMIT3(0x89, 0x5d, 0xf0 - K*4);
  373. break;
  374. case BPF_S_LD_W_LEN: /* A = skb->len; */
  375. BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, len) != 4);
  376. if (is_imm8(offsetof(struct sk_buff, len)))
  377. /* mov off8(%rdi),%eax */
  378. EMIT3(0x8b, 0x47, offsetof(struct sk_buff, len));
  379. else {
  380. EMIT2(0x8b, 0x87);
  381. EMIT(offsetof(struct sk_buff, len), 4);
  382. }
  383. break;
  384. case BPF_S_LDX_W_LEN: /* X = skb->len; */
  385. seen |= SEEN_XREG;
  386. if (is_imm8(offsetof(struct sk_buff, len)))
  387. /* mov off8(%rdi),%ebx */
  388. EMIT3(0x8b, 0x5f, offsetof(struct sk_buff, len));
  389. else {
  390. EMIT2(0x8b, 0x9f);
  391. EMIT(offsetof(struct sk_buff, len), 4);
  392. }
  393. break;
  394. case BPF_S_ANC_PROTOCOL: /* A = ntohs(skb->protocol); */
  395. BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
  396. if (is_imm8(offsetof(struct sk_buff, protocol))) {
  397. /* movzwl off8(%rdi),%eax */
  398. EMIT4(0x0f, 0xb7, 0x47, offsetof(struct sk_buff, protocol));
  399. } else {
  400. EMIT3(0x0f, 0xb7, 0x87); /* movzwl off32(%rdi),%eax */
  401. EMIT(offsetof(struct sk_buff, protocol), 4);
  402. }
  403. EMIT2(0x86, 0xc4); /* ntohs() : xchg %al,%ah */
  404. break;
  405. case BPF_S_ANC_IFINDEX:
  406. if (is_imm8(offsetof(struct sk_buff, dev))) {
  407. /* movq off8(%rdi),%rax */
  408. EMIT4(0x48, 0x8b, 0x47, offsetof(struct sk_buff, dev));
  409. } else {
  410. EMIT3(0x48, 0x8b, 0x87); /* movq off32(%rdi),%rax */
  411. EMIT(offsetof(struct sk_buff, dev), 4);
  412. }
  413. EMIT3(0x48, 0x85, 0xc0); /* test %rax,%rax */
  414. EMIT_COND_JMP(X86_JE, cleanup_addr - (addrs[i] - 6));
  415. BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
  416. EMIT2(0x8b, 0x80); /* mov off32(%rax),%eax */
  417. EMIT(offsetof(struct net_device, ifindex), 4);
  418. break;
  419. case BPF_S_ANC_MARK:
  420. BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
  421. if (is_imm8(offsetof(struct sk_buff, mark))) {
  422. /* mov off8(%rdi),%eax */
  423. EMIT3(0x8b, 0x47, offsetof(struct sk_buff, mark));
  424. } else {
  425. EMIT2(0x8b, 0x87);
  426. EMIT(offsetof(struct sk_buff, mark), 4);
  427. }
  428. break;
  429. case BPF_S_ANC_RXHASH:
  430. BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, rxhash) != 4);
  431. if (is_imm8(offsetof(struct sk_buff, rxhash))) {
  432. /* mov off8(%rdi),%eax */
  433. EMIT3(0x8b, 0x47, offsetof(struct sk_buff, rxhash));
  434. } else {
  435. EMIT2(0x8b, 0x87);
  436. EMIT(offsetof(struct sk_buff, rxhash), 4);
  437. }
  438. break;
  439. case BPF_S_ANC_QUEUE:
  440. BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, queue_mapping) != 2);
  441. if (is_imm8(offsetof(struct sk_buff, queue_mapping))) {
  442. /* movzwl off8(%rdi),%eax */
  443. EMIT4(0x0f, 0xb7, 0x47, offsetof(struct sk_buff, queue_mapping));
  444. } else {
  445. EMIT3(0x0f, 0xb7, 0x87); /* movzwl off32(%rdi),%eax */
  446. EMIT(offsetof(struct sk_buff, queue_mapping), 4);
  447. }
  448. break;
  449. case BPF_S_ANC_CPU:
  450. #ifdef CONFIG_SMP
  451. EMIT4(0x65, 0x8b, 0x04, 0x25); /* mov %gs:off32,%eax */
  452. EMIT((u32)(unsigned long)&cpu_number, 4); /* A = smp_processor_id(); */
  453. #else
  454. CLEAR_A();
  455. #endif
  456. break;
  457. case BPF_S_LD_W_ABS:
  458. func = CHOOSE_LOAD_FUNC(K, sk_load_word);
  459. common_load: seen |= SEEN_DATAREF;
  460. t_offset = func - (image + addrs[i]);
  461. EMIT1_off32(0xbe, K); /* mov imm32,%esi */
  462. EMIT1_off32(0xe8, t_offset); /* call */
  463. break;
  464. case BPF_S_LD_H_ABS:
  465. func = CHOOSE_LOAD_FUNC(K, sk_load_half);
  466. goto common_load;
  467. case BPF_S_LD_B_ABS:
  468. func = CHOOSE_LOAD_FUNC(K, sk_load_byte);
  469. goto common_load;
  470. case BPF_S_LDX_B_MSH:
  471. func = CHOOSE_LOAD_FUNC(K, sk_load_byte_msh);
  472. seen |= SEEN_DATAREF | SEEN_XREG;
  473. t_offset = func - (image + addrs[i]);
  474. EMIT1_off32(0xbe, K); /* mov imm32,%esi */
  475. EMIT1_off32(0xe8, t_offset); /* call sk_load_byte_msh */
  476. break;
  477. case BPF_S_LD_W_IND:
  478. func = sk_load_word;
  479. common_load_ind: seen |= SEEN_DATAREF | SEEN_XREG;
  480. t_offset = func - (image + addrs[i]);
  481. if (K) {
  482. if (is_imm8(K)) {
  483. EMIT3(0x8d, 0x73, K); /* lea imm8(%rbx), %esi */
  484. } else {
  485. EMIT2(0x8d, 0xb3); /* lea imm32(%rbx),%esi */
  486. EMIT(K, 4);
  487. }
  488. } else {
  489. EMIT2(0x89,0xde); /* mov %ebx,%esi */
  490. }
  491. EMIT1_off32(0xe8, t_offset); /* call sk_load_xxx_ind */
  492. break;
  493. case BPF_S_LD_H_IND:
  494. func = sk_load_half;
  495. goto common_load_ind;
  496. case BPF_S_LD_B_IND:
  497. func = sk_load_byte;
  498. goto common_load_ind;
  499. case BPF_S_JMP_JA:
  500. t_offset = addrs[i + K] - addrs[i];
  501. EMIT_JMP(t_offset);
  502. break;
  503. COND_SEL(BPF_S_JMP_JGT_K, X86_JA, X86_JBE);
  504. COND_SEL(BPF_S_JMP_JGE_K, X86_JAE, X86_JB);
  505. COND_SEL(BPF_S_JMP_JEQ_K, X86_JE, X86_JNE);
  506. COND_SEL(BPF_S_JMP_JSET_K,X86_JNE, X86_JE);
  507. COND_SEL(BPF_S_JMP_JGT_X, X86_JA, X86_JBE);
  508. COND_SEL(BPF_S_JMP_JGE_X, X86_JAE, X86_JB);
  509. COND_SEL(BPF_S_JMP_JEQ_X, X86_JE, X86_JNE);
  510. COND_SEL(BPF_S_JMP_JSET_X,X86_JNE, X86_JE);
  511. cond_branch: f_offset = addrs[i + filter[i].jf] - addrs[i];
  512. t_offset = addrs[i + filter[i].jt] - addrs[i];
  513. /* same targets, can avoid doing the test :) */
  514. if (filter[i].jt == filter[i].jf) {
  515. EMIT_JMP(t_offset);
  516. break;
  517. }
  518. switch (filter[i].code) {
  519. case BPF_S_JMP_JGT_X:
  520. case BPF_S_JMP_JGE_X:
  521. case BPF_S_JMP_JEQ_X:
  522. seen |= SEEN_XREG;
  523. EMIT2(0x39, 0xd8); /* cmp %ebx,%eax */
  524. break;
  525. case BPF_S_JMP_JSET_X:
  526. seen |= SEEN_XREG;
  527. EMIT2(0x85, 0xd8); /* test %ebx,%eax */
  528. break;
  529. case BPF_S_JMP_JEQ_K:
  530. if (K == 0) {
  531. EMIT2(0x85, 0xc0); /* test %eax,%eax */
  532. break;
  533. }
  534. case BPF_S_JMP_JGT_K:
  535. case BPF_S_JMP_JGE_K:
  536. if (K <= 127)
  537. EMIT3(0x83, 0xf8, K); /* cmp imm8,%eax */
  538. else
  539. EMIT1_off32(0x3d, K); /* cmp imm32,%eax */
  540. break;
  541. case BPF_S_JMP_JSET_K:
  542. if (K <= 0xFF)
  543. EMIT2(0xa8, K); /* test imm8,%al */
  544. else if (!(K & 0xFFFF00FF))
  545. EMIT3(0xf6, 0xc4, K >> 8); /* test imm8,%ah */
  546. else if (K <= 0xFFFF) {
  547. EMIT2(0x66, 0xa9); /* test imm16,%ax */
  548. EMIT(K, 2);
  549. } else {
  550. EMIT1_off32(0xa9, K); /* test imm32,%eax */
  551. }
  552. break;
  553. }
  554. if (filter[i].jt != 0) {
  555. if (filter[i].jf && f_offset)
  556. t_offset += is_near(f_offset) ? 2 : 5;
  557. EMIT_COND_JMP(t_op, t_offset);
  558. if (filter[i].jf)
  559. EMIT_JMP(f_offset);
  560. break;
  561. }
  562. EMIT_COND_JMP(f_op, f_offset);
  563. break;
  564. default:
  565. /* hmm, too complex filter, give up with jit compiler */
  566. goto out;
  567. }
  568. ilen = prog - temp;
  569. if (image) {
  570. if (unlikely(proglen + ilen > oldproglen)) {
  571. pr_err("bpb_jit_compile fatal error\n");
  572. kfree(addrs);
  573. module_free(NULL, image);
  574. return;
  575. }
  576. memcpy(image + proglen, temp, ilen);
  577. }
  578. proglen += ilen;
  579. addrs[i] = proglen;
  580. prog = temp;
  581. }
  582. /* last bpf instruction is always a RET :
  583. * use it to give the cleanup instruction(s) addr
  584. */
  585. cleanup_addr = proglen - 1; /* ret */
  586. if (seen_or_pass0)
  587. cleanup_addr -= 1; /* leaveq */
  588. if (seen_or_pass0 & SEEN_XREG)
  589. cleanup_addr -= 4; /* mov -8(%rbp),%rbx */
  590. if (image) {
  591. if (proglen != oldproglen)
  592. pr_err("bpb_jit_compile proglen=%u != oldproglen=%u\n", proglen, oldproglen);
  593. break;
  594. }
  595. if (proglen == oldproglen) {
  596. image = module_alloc(max_t(unsigned int,
  597. proglen,
  598. sizeof(struct work_struct)));
  599. if (!image)
  600. goto out;
  601. }
  602. oldproglen = proglen;
  603. }
  604. if (bpf_jit_enable > 1)
  605. pr_err("flen=%d proglen=%u pass=%d image=%p\n",
  606. flen, proglen, pass, image);
  607. if (image) {
  608. if (bpf_jit_enable > 1)
  609. print_hex_dump(KERN_ERR, "JIT code: ", DUMP_PREFIX_ADDRESS,
  610. 16, 1, image, proglen, false);
  611. bpf_flush_icache(image, image + proglen);
  612. fp->bpf_func = (void *)image;
  613. }
  614. out:
  615. kfree(addrs);
  616. return;
  617. }
  618. static void jit_free_defer(struct work_struct *arg)
  619. {
  620. module_free(NULL, arg);
  621. }
  622. /* run from softirq, we must use a work_struct to call
  623. * module_free() from process context
  624. */
  625. void bpf_jit_free(struct sk_filter *fp)
  626. {
  627. if (fp->bpf_func != sk_run_filter) {
  628. struct work_struct *work = (struct work_struct *)fp->bpf_func;
  629. INIT_WORK(work, jit_free_defer);
  630. schedule_work(work);
  631. }
  632. }