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