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