bpf_jit_comp.c 18 KB

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