xref: /openbmc/linux/arch/x86/net/bpf_jit_comp.c (revision 82c93fcc)
10a14842fSEric Dumazet /* bpf_jit_comp.c : BPF JIT compiler
20a14842fSEric Dumazet  *
30a14842fSEric Dumazet  * Copyright (C) 2011 Eric Dumazet (eric.dumazet@gmail.com)
40a14842fSEric Dumazet  *
50a14842fSEric Dumazet  * This program is free software; you can redistribute it and/or
60a14842fSEric Dumazet  * modify it under the terms of the GNU General Public License
70a14842fSEric Dumazet  * as published by the Free Software Foundation; version 2
80a14842fSEric Dumazet  * of the License.
90a14842fSEric Dumazet  */
100a14842fSEric Dumazet #include <linux/moduleloader.h>
110a14842fSEric Dumazet #include <asm/cacheflush.h>
120a14842fSEric Dumazet #include <linux/netdevice.h>
130a14842fSEric Dumazet #include <linux/filter.h>
140a14842fSEric Dumazet 
150a14842fSEric Dumazet /*
160a14842fSEric Dumazet  * Conventions :
170a14842fSEric Dumazet  *  EAX : BPF A accumulator
180a14842fSEric Dumazet  *  EBX : BPF X accumulator
190a14842fSEric Dumazet  *  RDI : pointer to skb   (first argument given to JIT function)
200a14842fSEric Dumazet  *  RBP : frame pointer (even if CONFIG_FRAME_POINTER=n)
210a14842fSEric Dumazet  *  ECX,EDX,ESI : scratch registers
220a14842fSEric Dumazet  *  r9d : skb->len - skb->data_len (headlen)
230a14842fSEric Dumazet  *  r8  : skb->data
240a14842fSEric Dumazet  * -8(RBP) : saved RBX value
250a14842fSEric Dumazet  * -16(RBP)..-80(RBP) : BPF_MEMWORDS values
260a14842fSEric Dumazet  */
270a14842fSEric Dumazet int bpf_jit_enable __read_mostly;
280a14842fSEric Dumazet 
290a14842fSEric Dumazet /*
300a14842fSEric Dumazet  * assembly code in arch/x86/net/bpf_jit.S
310a14842fSEric Dumazet  */
320a14842fSEric Dumazet extern u8 sk_load_word[], sk_load_half[], sk_load_byte[], sk_load_byte_msh[];
33a998d434SJan Seiffert extern u8 sk_load_word_positive_offset[], sk_load_half_positive_offset[];
34a998d434SJan Seiffert extern u8 sk_load_byte_positive_offset[], sk_load_byte_msh_positive_offset[];
35a998d434SJan Seiffert extern u8 sk_load_word_negative_offset[], sk_load_half_negative_offset[];
36a998d434SJan Seiffert extern u8 sk_load_byte_negative_offset[], sk_load_byte_msh_negative_offset[];
370a14842fSEric Dumazet 
380a14842fSEric Dumazet static inline u8 *emit_code(u8 *ptr, u32 bytes, unsigned int len)
390a14842fSEric Dumazet {
400a14842fSEric Dumazet 	if (len == 1)
410a14842fSEric Dumazet 		*ptr = bytes;
420a14842fSEric Dumazet 	else if (len == 2)
430a14842fSEric Dumazet 		*(u16 *)ptr = bytes;
440a14842fSEric Dumazet 	else {
450a14842fSEric Dumazet 		*(u32 *)ptr = bytes;
460a14842fSEric Dumazet 		barrier();
470a14842fSEric Dumazet 	}
480a14842fSEric Dumazet 	return ptr + len;
490a14842fSEric Dumazet }
500a14842fSEric Dumazet 
510a14842fSEric Dumazet #define EMIT(bytes, len)	do { prog = emit_code(prog, bytes, len); } while (0)
520a14842fSEric Dumazet 
530a14842fSEric Dumazet #define EMIT1(b1)		EMIT(b1, 1)
540a14842fSEric Dumazet #define EMIT2(b1, b2)		EMIT((b1) + ((b2) << 8), 2)
550a14842fSEric Dumazet #define EMIT3(b1, b2, b3)	EMIT((b1) + ((b2) << 8) + ((b3) << 16), 3)
560a14842fSEric Dumazet #define EMIT4(b1, b2, b3, b4)   EMIT((b1) + ((b2) << 8) + ((b3) << 16) + ((b4) << 24), 4)
570a14842fSEric Dumazet #define EMIT1_off32(b1, off)	do { EMIT1(b1); EMIT(off, 4);} while (0)
580a14842fSEric Dumazet 
590a14842fSEric Dumazet #define CLEAR_A() EMIT2(0x31, 0xc0) /* xor %eax,%eax */
600a14842fSEric Dumazet #define CLEAR_X() EMIT2(0x31, 0xdb) /* xor %ebx,%ebx */
610a14842fSEric Dumazet 
620a14842fSEric Dumazet static inline bool is_imm8(int value)
630a14842fSEric Dumazet {
640a14842fSEric Dumazet 	return value <= 127 && value >= -128;
650a14842fSEric Dumazet }
660a14842fSEric Dumazet 
670a14842fSEric Dumazet static inline bool is_near(int offset)
680a14842fSEric Dumazet {
690a14842fSEric Dumazet 	return offset <= 127 && offset >= -128;
700a14842fSEric Dumazet }
710a14842fSEric Dumazet 
720a14842fSEric Dumazet #define EMIT_JMP(offset)						\
730a14842fSEric Dumazet do {									\
740a14842fSEric Dumazet 	if (offset) {							\
750a14842fSEric Dumazet 		if (is_near(offset))					\
760a14842fSEric Dumazet 			EMIT2(0xeb, offset); /* jmp .+off8 */		\
770a14842fSEric Dumazet 		else							\
780a14842fSEric Dumazet 			EMIT1_off32(0xe9, offset); /* jmp .+off32 */	\
790a14842fSEric Dumazet 	}								\
800a14842fSEric Dumazet } while (0)
810a14842fSEric Dumazet 
820a14842fSEric Dumazet /* list of x86 cond jumps opcodes (. + s8)
830a14842fSEric Dumazet  * Add 0x10 (and an extra 0x0f) to generate far jumps (. + s32)
840a14842fSEric Dumazet  */
850a14842fSEric Dumazet #define X86_JB  0x72
860a14842fSEric Dumazet #define X86_JAE 0x73
870a14842fSEric Dumazet #define X86_JE  0x74
880a14842fSEric Dumazet #define X86_JNE 0x75
890a14842fSEric Dumazet #define X86_JBE 0x76
900a14842fSEric Dumazet #define X86_JA  0x77
910a14842fSEric Dumazet 
920a14842fSEric Dumazet #define EMIT_COND_JMP(op, offset)				\
930a14842fSEric Dumazet do {								\
940a14842fSEric Dumazet 	if (is_near(offset))					\
950a14842fSEric Dumazet 		EMIT2(op, offset); /* jxx .+off8 */		\
960a14842fSEric Dumazet 	else {							\
970a14842fSEric Dumazet 		EMIT2(0x0f, op + 0x10);				\
980a14842fSEric Dumazet 		EMIT(offset, 4); /* jxx .+off32 */		\
990a14842fSEric Dumazet 	}							\
1000a14842fSEric Dumazet } while (0)
1010a14842fSEric Dumazet 
1020a14842fSEric Dumazet #define COND_SEL(CODE, TOP, FOP)	\
1030a14842fSEric Dumazet 	case CODE:			\
1040a14842fSEric Dumazet 		t_op = TOP;		\
1050a14842fSEric Dumazet 		f_op = FOP;		\
1060a14842fSEric Dumazet 		goto cond_branch
1070a14842fSEric Dumazet 
1080a14842fSEric Dumazet 
1090a14842fSEric Dumazet #define SEEN_DATAREF 1 /* might call external helpers */
1100a14842fSEric Dumazet #define SEEN_XREG    2 /* ebx is used */
1110a14842fSEric Dumazet #define SEEN_MEM     4 /* use mem[] for temporary storage */
1120a14842fSEric Dumazet 
1130a14842fSEric Dumazet static inline void bpf_flush_icache(void *start, void *end)
1140a14842fSEric Dumazet {
1150a14842fSEric Dumazet 	mm_segment_t old_fs = get_fs();
1160a14842fSEric Dumazet 
1170a14842fSEric Dumazet 	set_fs(KERNEL_DS);
1180a14842fSEric Dumazet 	smp_wmb();
1190a14842fSEric Dumazet 	flush_icache_range((unsigned long)start, (unsigned long)end);
1200a14842fSEric Dumazet 	set_fs(old_fs);
1210a14842fSEric Dumazet }
1220a14842fSEric Dumazet 
123a998d434SJan Seiffert #define CHOOSE_LOAD_FUNC(K, func) \
124a998d434SJan Seiffert 	((int)K < 0 ? ((int)K >= SKF_LL_OFF ? func##_negative_offset : func) : func##_positive_offset)
1250a14842fSEric Dumazet 
1260a14842fSEric Dumazet void bpf_jit_compile(struct sk_filter *fp)
1270a14842fSEric Dumazet {
1280a14842fSEric Dumazet 	u8 temp[64];
1290a14842fSEric Dumazet 	u8 *prog;
1300a14842fSEric Dumazet 	unsigned int proglen, oldproglen = 0;
1310a14842fSEric Dumazet 	int ilen, i;
1320a14842fSEric Dumazet 	int t_offset, f_offset;
1330a14842fSEric Dumazet 	u8 t_op, f_op, seen = 0, pass;
1340a14842fSEric Dumazet 	u8 *image = NULL;
1350a14842fSEric Dumazet 	u8 *func;
1360a14842fSEric Dumazet 	int pc_ret0 = -1; /* bpf index of first RET #0 instruction (if any) */
1370a14842fSEric Dumazet 	unsigned int cleanup_addr; /* epilogue code offset */
1380a14842fSEric Dumazet 	unsigned int *addrs;
1390a14842fSEric Dumazet 	const struct sock_filter *filter = fp->insns;
1400a14842fSEric Dumazet 	int flen = fp->len;
1410a14842fSEric Dumazet 
1420a14842fSEric Dumazet 	if (!bpf_jit_enable)
1430a14842fSEric Dumazet 		return;
1440a14842fSEric Dumazet 
1450a14842fSEric Dumazet 	addrs = kmalloc(flen * sizeof(*addrs), GFP_KERNEL);
1460a14842fSEric Dumazet 	if (addrs == NULL)
1470a14842fSEric Dumazet 		return;
1480a14842fSEric Dumazet 
1490a14842fSEric Dumazet 	/* Before first pass, make a rough estimation of addrs[]
1500a14842fSEric Dumazet 	 * each bpf instruction is translated to less than 64 bytes
1510a14842fSEric Dumazet 	 */
1520a14842fSEric Dumazet 	for (proglen = 0, i = 0; i < flen; i++) {
1530a14842fSEric Dumazet 		proglen += 64;
1540a14842fSEric Dumazet 		addrs[i] = proglen;
1550a14842fSEric Dumazet 	}
1560a14842fSEric Dumazet 	cleanup_addr = proglen; /* epilogue address */
1570a14842fSEric Dumazet 
1580a14842fSEric Dumazet 	for (pass = 0; pass < 10; pass++) {
159d00a9dd2SEric Dumazet 		u8 seen_or_pass0 = (pass == 0) ? (SEEN_XREG | SEEN_DATAREF | SEEN_MEM) : seen;
1600a14842fSEric Dumazet 		/* no prologue/epilogue for trivial filters (RET something) */
1610a14842fSEric Dumazet 		proglen = 0;
1620a14842fSEric Dumazet 		prog = temp;
1630a14842fSEric Dumazet 
164d00a9dd2SEric Dumazet 		if (seen_or_pass0) {
1650a14842fSEric Dumazet 			EMIT4(0x55, 0x48, 0x89, 0xe5); /* push %rbp; mov %rsp,%rbp */
1660a14842fSEric Dumazet 			EMIT4(0x48, 0x83, 0xec, 96);	/* subq  $96,%rsp	*/
1670a14842fSEric Dumazet 			/* note : must save %rbx in case bpf_error is hit */
168d00a9dd2SEric Dumazet 			if (seen_or_pass0 & (SEEN_XREG | SEEN_DATAREF))
1690a14842fSEric Dumazet 				EMIT4(0x48, 0x89, 0x5d, 0xf8); /* mov %rbx, -8(%rbp) */
170d00a9dd2SEric Dumazet 			if (seen_or_pass0 & SEEN_XREG)
1710a14842fSEric Dumazet 				CLEAR_X(); /* make sure we dont leek kernel memory */
1720a14842fSEric Dumazet 
1730a14842fSEric Dumazet 			/*
1740a14842fSEric Dumazet 			 * If this filter needs to access skb data,
1750a14842fSEric Dumazet 			 * loads r9 and r8 with :
1760a14842fSEric Dumazet 			 *  r9 = skb->len - skb->data_len
1770a14842fSEric Dumazet 			 *  r8 = skb->data
1780a14842fSEric Dumazet 			 */
179d00a9dd2SEric Dumazet 			if (seen_or_pass0 & SEEN_DATAREF) {
1800a14842fSEric Dumazet 				if (offsetof(struct sk_buff, len) <= 127)
1810a14842fSEric Dumazet 					/* mov    off8(%rdi),%r9d */
1820a14842fSEric Dumazet 					EMIT4(0x44, 0x8b, 0x4f, offsetof(struct sk_buff, len));
1830a14842fSEric Dumazet 				else {
1840a14842fSEric Dumazet 					/* mov    off32(%rdi),%r9d */
1850a14842fSEric Dumazet 					EMIT3(0x44, 0x8b, 0x8f);
1860a14842fSEric Dumazet 					EMIT(offsetof(struct sk_buff, len), 4);
1870a14842fSEric Dumazet 				}
1880a14842fSEric Dumazet 				if (is_imm8(offsetof(struct sk_buff, data_len)))
1890a14842fSEric Dumazet 					/* sub    off8(%rdi),%r9d */
1900a14842fSEric Dumazet 					EMIT4(0x44, 0x2b, 0x4f, offsetof(struct sk_buff, data_len));
1910a14842fSEric Dumazet 				else {
1920a14842fSEric Dumazet 					EMIT3(0x44, 0x2b, 0x8f);
1930a14842fSEric Dumazet 					EMIT(offsetof(struct sk_buff, data_len), 4);
1940a14842fSEric Dumazet 				}
1950a14842fSEric Dumazet 
1960a14842fSEric Dumazet 				if (is_imm8(offsetof(struct sk_buff, data)))
1970a14842fSEric Dumazet 					/* mov off8(%rdi),%r8 */
1980a14842fSEric Dumazet 					EMIT4(0x4c, 0x8b, 0x47, offsetof(struct sk_buff, data));
1990a14842fSEric Dumazet 				else {
2000a14842fSEric Dumazet 					/* mov off32(%rdi),%r8 */
2010a14842fSEric Dumazet 					EMIT3(0x4c, 0x8b, 0x87);
2020a14842fSEric Dumazet 					EMIT(offsetof(struct sk_buff, data), 4);
2030a14842fSEric Dumazet 				}
2040a14842fSEric Dumazet 			}
2050a14842fSEric Dumazet 		}
2060a14842fSEric Dumazet 
2070a14842fSEric Dumazet 		switch (filter[0].code) {
2080a14842fSEric Dumazet 		case BPF_S_RET_K:
2090a14842fSEric Dumazet 		case BPF_S_LD_W_LEN:
2100a14842fSEric Dumazet 		case BPF_S_ANC_PROTOCOL:
2110a14842fSEric Dumazet 		case BPF_S_ANC_IFINDEX:
2120a14842fSEric Dumazet 		case BPF_S_ANC_MARK:
2130a14842fSEric Dumazet 		case BPF_S_ANC_RXHASH:
2140a14842fSEric Dumazet 		case BPF_S_ANC_CPU:
2150a14842fSEric Dumazet 		case BPF_S_ANC_QUEUE:
2160a14842fSEric Dumazet 		case BPF_S_LD_W_ABS:
2170a14842fSEric Dumazet 		case BPF_S_LD_H_ABS:
2180a14842fSEric Dumazet 		case BPF_S_LD_B_ABS:
2190a14842fSEric Dumazet 			/* first instruction sets A register (or is RET 'constant') */
2200a14842fSEric Dumazet 			break;
2210a14842fSEric Dumazet 		default:
2220a14842fSEric Dumazet 			/* make sure we dont leak kernel information to user */
2230a14842fSEric Dumazet 			CLEAR_A(); /* A = 0 */
2240a14842fSEric Dumazet 		}
2250a14842fSEric Dumazet 
2260a14842fSEric Dumazet 		for (i = 0; i < flen; i++) {
2270a14842fSEric Dumazet 			unsigned int K = filter[i].k;
2280a14842fSEric Dumazet 
2290a14842fSEric Dumazet 			switch (filter[i].code) {
2300a14842fSEric Dumazet 			case BPF_S_ALU_ADD_X: /* A += X; */
2310a14842fSEric Dumazet 				seen |= SEEN_XREG;
2320a14842fSEric Dumazet 				EMIT2(0x01, 0xd8);		/* add %ebx,%eax */
2330a14842fSEric Dumazet 				break;
2340a14842fSEric Dumazet 			case BPF_S_ALU_ADD_K: /* A += K; */
2350a14842fSEric Dumazet 				if (!K)
2360a14842fSEric Dumazet 					break;
2370a14842fSEric Dumazet 				if (is_imm8(K))
2380a14842fSEric Dumazet 					EMIT3(0x83, 0xc0, K);	/* add imm8,%eax */
2390a14842fSEric Dumazet 				else
2400a14842fSEric Dumazet 					EMIT1_off32(0x05, K);	/* add imm32,%eax */
2410a14842fSEric Dumazet 				break;
2420a14842fSEric Dumazet 			case BPF_S_ALU_SUB_X: /* A -= X; */
2430a14842fSEric Dumazet 				seen |= SEEN_XREG;
2440a14842fSEric Dumazet 				EMIT2(0x29, 0xd8);		/* sub    %ebx,%eax */
2450a14842fSEric Dumazet 				break;
2460a14842fSEric Dumazet 			case BPF_S_ALU_SUB_K: /* A -= K */
2470a14842fSEric Dumazet 				if (!K)
2480a14842fSEric Dumazet 					break;
2490a14842fSEric Dumazet 				if (is_imm8(K))
2500a14842fSEric Dumazet 					EMIT3(0x83, 0xe8, K); /* sub imm8,%eax */
2510a14842fSEric Dumazet 				else
2520a14842fSEric Dumazet 					EMIT1_off32(0x2d, K); /* sub imm32,%eax */
2530a14842fSEric Dumazet 				break;
2540a14842fSEric Dumazet 			case BPF_S_ALU_MUL_X: /* A *= X; */
2550a14842fSEric Dumazet 				seen |= SEEN_XREG;
2560a14842fSEric Dumazet 				EMIT3(0x0f, 0xaf, 0xc3);	/* imul %ebx,%eax */
2570a14842fSEric Dumazet 				break;
2580a14842fSEric Dumazet 			case BPF_S_ALU_MUL_K: /* A *= K */
2590a14842fSEric Dumazet 				if (is_imm8(K))
2600a14842fSEric Dumazet 					EMIT3(0x6b, 0xc0, K); /* imul imm8,%eax,%eax */
2610a14842fSEric Dumazet 				else {
2620a14842fSEric Dumazet 					EMIT2(0x69, 0xc0);		/* imul imm32,%eax */
2630a14842fSEric Dumazet 					EMIT(K, 4);
2640a14842fSEric Dumazet 				}
2650a14842fSEric Dumazet 				break;
2660a14842fSEric Dumazet 			case BPF_S_ALU_DIV_X: /* A /= X; */
2670a14842fSEric Dumazet 				seen |= SEEN_XREG;
2680a14842fSEric Dumazet 				EMIT2(0x85, 0xdb);	/* test %ebx,%ebx */
269d00a9dd2SEric Dumazet 				if (pc_ret0 > 0) {
270d00a9dd2SEric Dumazet 					/* addrs[pc_ret0 - 1] is start address of target
271d00a9dd2SEric Dumazet 					 * (addrs[i] - 4) is the address following this jmp
272d00a9dd2SEric Dumazet 					 * ("xor %edx,%edx; div %ebx" being 4 bytes long)
273d00a9dd2SEric Dumazet 					 */
274d00a9dd2SEric Dumazet 					EMIT_COND_JMP(X86_JE, addrs[pc_ret0 - 1] -
275d00a9dd2SEric Dumazet 								(addrs[i] - 4));
276d00a9dd2SEric Dumazet 				} else {
2770a14842fSEric Dumazet 					EMIT_COND_JMP(X86_JNE, 2 + 5);
2780a14842fSEric Dumazet 					CLEAR_A();
2790a14842fSEric Dumazet 					EMIT1_off32(0xe9, cleanup_addr - (addrs[i] - 4)); /* jmp .+off32 */
2800a14842fSEric Dumazet 				}
2810a14842fSEric Dumazet 				EMIT4(0x31, 0xd2, 0xf7, 0xf3); /* xor %edx,%edx; div %ebx */
2820a14842fSEric Dumazet 				break;
283280050ccSEric Dumazet 			case BPF_S_ALU_MOD_X: /* A %= X; */
284280050ccSEric Dumazet 				seen |= SEEN_XREG;
285280050ccSEric Dumazet 				EMIT2(0x85, 0xdb);	/* test %ebx,%ebx */
286280050ccSEric Dumazet 				if (pc_ret0 > 0) {
287280050ccSEric Dumazet 					/* addrs[pc_ret0 - 1] is start address of target
288280050ccSEric Dumazet 					 * (addrs[i] - 6) is the address following this jmp
289280050ccSEric Dumazet 					 * ("xor %edx,%edx; div %ebx;mov %edx,%eax" being 6 bytes long)
290280050ccSEric Dumazet 					 */
291280050ccSEric Dumazet 					EMIT_COND_JMP(X86_JE, addrs[pc_ret0 - 1] -
292280050ccSEric Dumazet 								(addrs[i] - 6));
293280050ccSEric Dumazet 				} else {
294280050ccSEric Dumazet 					EMIT_COND_JMP(X86_JNE, 2 + 5);
295280050ccSEric Dumazet 					CLEAR_A();
296280050ccSEric Dumazet 					EMIT1_off32(0xe9, cleanup_addr - (addrs[i] - 6)); /* jmp .+off32 */
297280050ccSEric Dumazet 				}
298280050ccSEric Dumazet 				EMIT2(0x31, 0xd2);	/* xor %edx,%edx */
299280050ccSEric Dumazet 				EMIT2(0xf7, 0xf3);	/* div %ebx */
300280050ccSEric Dumazet 				EMIT2(0x89, 0xd0);	/* mov %edx,%eax */
301280050ccSEric Dumazet 				break;
302280050ccSEric Dumazet 			case BPF_S_ALU_MOD_K: /* A %= K; */
303280050ccSEric Dumazet 				EMIT2(0x31, 0xd2);	/* xor %edx,%edx */
304280050ccSEric Dumazet 				EMIT1(0xb9);EMIT(K, 4);	/* mov imm32,%ecx */
305280050ccSEric Dumazet 				EMIT2(0xf7, 0xf1);	/* div %ecx */
306280050ccSEric Dumazet 				EMIT2(0x89, 0xd0);	/* mov %edx,%eax */
307280050ccSEric Dumazet 				break;
3080a14842fSEric Dumazet 			case BPF_S_ALU_DIV_K: /* A = reciprocal_divide(A, K); */
3090a14842fSEric Dumazet 				EMIT3(0x48, 0x69, 0xc0); /* imul imm32,%rax,%rax */
3100a14842fSEric Dumazet 				EMIT(K, 4);
3110a14842fSEric Dumazet 				EMIT4(0x48, 0xc1, 0xe8, 0x20); /* shr $0x20,%rax */
3120a14842fSEric Dumazet 				break;
3130a14842fSEric Dumazet 			case BPF_S_ALU_AND_X:
3140a14842fSEric Dumazet 				seen |= SEEN_XREG;
3150a14842fSEric Dumazet 				EMIT2(0x21, 0xd8);		/* and %ebx,%eax */
3160a14842fSEric Dumazet 				break;
3170a14842fSEric Dumazet 			case BPF_S_ALU_AND_K:
3180a14842fSEric Dumazet 				if (K >= 0xFFFFFF00) {
3190a14842fSEric Dumazet 					EMIT2(0x24, K & 0xFF); /* and imm8,%al */
3200a14842fSEric Dumazet 				} else if (K >= 0xFFFF0000) {
3210a14842fSEric Dumazet 					EMIT2(0x66, 0x25);	/* and imm16,%ax */
3221d24fb36Szhuangfeiran@ict.ac.cn 					EMIT(K, 2);
3230a14842fSEric Dumazet 				} else {
3240a14842fSEric Dumazet 					EMIT1_off32(0x25, K);	/* and imm32,%eax */
3250a14842fSEric Dumazet 				}
3260a14842fSEric Dumazet 				break;
3270a14842fSEric Dumazet 			case BPF_S_ALU_OR_X:
3280a14842fSEric Dumazet 				seen |= SEEN_XREG;
3290a14842fSEric Dumazet 				EMIT2(0x09, 0xd8);		/* or %ebx,%eax */
3300a14842fSEric Dumazet 				break;
3310a14842fSEric Dumazet 			case BPF_S_ALU_OR_K:
3320a14842fSEric Dumazet 				if (is_imm8(K))
3330a14842fSEric Dumazet 					EMIT3(0x83, 0xc8, K); /* or imm8,%eax */
3340a14842fSEric Dumazet 				else
3350a14842fSEric Dumazet 					EMIT1_off32(0x0d, K);	/* or imm32,%eax */
3360a14842fSEric Dumazet 				break;
3374bfaddf1SEric Dumazet 			case BPF_S_ANC_ALU_XOR_X: /* A ^= X; */
338*82c93fccSDaniel Borkmann 			case BPF_S_ALU_XOR_X:
3394bfaddf1SEric Dumazet 				seen |= SEEN_XREG;
3404bfaddf1SEric Dumazet 				EMIT2(0x31, 0xd8);		/* xor %ebx,%eax */
3414bfaddf1SEric Dumazet 				break;
342*82c93fccSDaniel Borkmann 			case BPF_S_ALU_XOR_K: /* A ^= K; */
343*82c93fccSDaniel Borkmann 				if (K == 0)
344*82c93fccSDaniel Borkmann 					break;
345*82c93fccSDaniel Borkmann 				if (is_imm8(K))
346*82c93fccSDaniel Borkmann 					EMIT3(0x83, 0xf0, K);	/* xor imm8,%eax */
347*82c93fccSDaniel Borkmann 				else
348*82c93fccSDaniel Borkmann 					EMIT1_off32(0x35, K);	/* xor imm32,%eax */
349*82c93fccSDaniel Borkmann 				break;
3500a14842fSEric Dumazet 			case BPF_S_ALU_LSH_X: /* A <<= X; */
3510a14842fSEric Dumazet 				seen |= SEEN_XREG;
3520a14842fSEric Dumazet 				EMIT4(0x89, 0xd9, 0xd3, 0xe0);	/* mov %ebx,%ecx; shl %cl,%eax */
3530a14842fSEric Dumazet 				break;
3540a14842fSEric Dumazet 			case BPF_S_ALU_LSH_K:
3550a14842fSEric Dumazet 				if (K == 0)
3560a14842fSEric Dumazet 					break;
3570a14842fSEric Dumazet 				else if (K == 1)
3580a14842fSEric Dumazet 					EMIT2(0xd1, 0xe0); /* shl %eax */
3590a14842fSEric Dumazet 				else
3600a14842fSEric Dumazet 					EMIT3(0xc1, 0xe0, K);
3610a14842fSEric Dumazet 				break;
3620a14842fSEric Dumazet 			case BPF_S_ALU_RSH_X: /* A >>= X; */
3630a14842fSEric Dumazet 				seen |= SEEN_XREG;
3640a14842fSEric Dumazet 				EMIT4(0x89, 0xd9, 0xd3, 0xe8);	/* mov %ebx,%ecx; shr %cl,%eax */
3650a14842fSEric Dumazet 				break;
3660a14842fSEric Dumazet 			case BPF_S_ALU_RSH_K: /* A >>= K; */
3670a14842fSEric Dumazet 				if (K == 0)
3680a14842fSEric Dumazet 					break;
3690a14842fSEric Dumazet 				else if (K == 1)
3700a14842fSEric Dumazet 					EMIT2(0xd1, 0xe8); /* shr %eax */
3710a14842fSEric Dumazet 				else
3720a14842fSEric Dumazet 					EMIT3(0xc1, 0xe8, K);
3730a14842fSEric Dumazet 				break;
3740a14842fSEric Dumazet 			case BPF_S_ALU_NEG:
3750a14842fSEric Dumazet 				EMIT2(0xf7, 0xd8);		/* neg %eax */
3760a14842fSEric Dumazet 				break;
3770a14842fSEric Dumazet 			case BPF_S_RET_K:
3780a14842fSEric Dumazet 				if (!K) {
3790a14842fSEric Dumazet 					if (pc_ret0 == -1)
3800a14842fSEric Dumazet 						pc_ret0 = i;
3810a14842fSEric Dumazet 					CLEAR_A();
3820a14842fSEric Dumazet 				} else {
3830a14842fSEric Dumazet 					EMIT1_off32(0xb8, K);	/* mov $imm32,%eax */
3840a14842fSEric Dumazet 				}
3850a14842fSEric Dumazet 				/* fallinto */
3860a14842fSEric Dumazet 			case BPF_S_RET_A:
387d00a9dd2SEric Dumazet 				if (seen_or_pass0) {
3880a14842fSEric Dumazet 					if (i != flen - 1) {
3890a14842fSEric Dumazet 						EMIT_JMP(cleanup_addr - addrs[i]);
3900a14842fSEric Dumazet 						break;
3910a14842fSEric Dumazet 					}
392d00a9dd2SEric Dumazet 					if (seen_or_pass0 & SEEN_XREG)
3930a14842fSEric Dumazet 						EMIT4(0x48, 0x8b, 0x5d, 0xf8);  /* mov  -8(%rbp),%rbx */
3940a14842fSEric Dumazet 					EMIT1(0xc9);		/* leaveq */
3950a14842fSEric Dumazet 				}
3960a14842fSEric Dumazet 				EMIT1(0xc3);		/* ret */
3970a14842fSEric Dumazet 				break;
3980a14842fSEric Dumazet 			case BPF_S_MISC_TAX: /* X = A */
3990a14842fSEric Dumazet 				seen |= SEEN_XREG;
4000a14842fSEric Dumazet 				EMIT2(0x89, 0xc3);	/* mov    %eax,%ebx */
4010a14842fSEric Dumazet 				break;
4020a14842fSEric Dumazet 			case BPF_S_MISC_TXA: /* A = X */
4030a14842fSEric Dumazet 				seen |= SEEN_XREG;
4040a14842fSEric Dumazet 				EMIT2(0x89, 0xd8);	/* mov    %ebx,%eax */
4050a14842fSEric Dumazet 				break;
4060a14842fSEric Dumazet 			case BPF_S_LD_IMM: /* A = K */
4070a14842fSEric Dumazet 				if (!K)
4080a14842fSEric Dumazet 					CLEAR_A();
4090a14842fSEric Dumazet 				else
4100a14842fSEric Dumazet 					EMIT1_off32(0xb8, K); /* mov $imm32,%eax */
4110a14842fSEric Dumazet 				break;
4120a14842fSEric Dumazet 			case BPF_S_LDX_IMM: /* X = K */
4130a14842fSEric Dumazet 				seen |= SEEN_XREG;
4140a14842fSEric Dumazet 				if (!K)
4150a14842fSEric Dumazet 					CLEAR_X();
4160a14842fSEric Dumazet 				else
4170a14842fSEric Dumazet 					EMIT1_off32(0xbb, K); /* mov $imm32,%ebx */
4180a14842fSEric Dumazet 				break;
4190a14842fSEric Dumazet 			case BPF_S_LD_MEM: /* A = mem[K] : mov off8(%rbp),%eax */
4200a14842fSEric Dumazet 				seen |= SEEN_MEM;
4210a14842fSEric Dumazet 				EMIT3(0x8b, 0x45, 0xf0 - K*4);
4220a14842fSEric Dumazet 				break;
4230a14842fSEric Dumazet 			case BPF_S_LDX_MEM: /* X = mem[K] : mov off8(%rbp),%ebx */
4240a14842fSEric Dumazet 				seen |= SEEN_XREG | SEEN_MEM;
4250a14842fSEric Dumazet 				EMIT3(0x8b, 0x5d, 0xf0 - K*4);
4260a14842fSEric Dumazet 				break;
4270a14842fSEric Dumazet 			case BPF_S_ST: /* mem[K] = A : mov %eax,off8(%rbp) */
4280a14842fSEric Dumazet 				seen |= SEEN_MEM;
4290a14842fSEric Dumazet 				EMIT3(0x89, 0x45, 0xf0 - K*4);
4300a14842fSEric Dumazet 				break;
4310a14842fSEric Dumazet 			case BPF_S_STX: /* mem[K] = X : mov %ebx,off8(%rbp) */
4320a14842fSEric Dumazet 				seen |= SEEN_XREG | SEEN_MEM;
4330a14842fSEric Dumazet 				EMIT3(0x89, 0x5d, 0xf0 - K*4);
4340a14842fSEric Dumazet 				break;
4350a14842fSEric Dumazet 			case BPF_S_LD_W_LEN: /*	A = skb->len; */
4360a14842fSEric Dumazet 				BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, len) != 4);
4370a14842fSEric Dumazet 				if (is_imm8(offsetof(struct sk_buff, len)))
4380a14842fSEric Dumazet 					/* mov    off8(%rdi),%eax */
4390a14842fSEric Dumazet 					EMIT3(0x8b, 0x47, offsetof(struct sk_buff, len));
4400a14842fSEric Dumazet 				else {
4410a14842fSEric Dumazet 					EMIT2(0x8b, 0x87);
4420a14842fSEric Dumazet 					EMIT(offsetof(struct sk_buff, len), 4);
4430a14842fSEric Dumazet 				}
4440a14842fSEric Dumazet 				break;
4450a14842fSEric Dumazet 			case BPF_S_LDX_W_LEN: /* X = skb->len; */
4460a14842fSEric Dumazet 				seen |= SEEN_XREG;
4470a14842fSEric Dumazet 				if (is_imm8(offsetof(struct sk_buff, len)))
4480a14842fSEric Dumazet 					/* mov off8(%rdi),%ebx */
4490a14842fSEric Dumazet 					EMIT3(0x8b, 0x5f, offsetof(struct sk_buff, len));
4500a14842fSEric Dumazet 				else {
4510a14842fSEric Dumazet 					EMIT2(0x8b, 0x9f);
4520a14842fSEric Dumazet 					EMIT(offsetof(struct sk_buff, len), 4);
4530a14842fSEric Dumazet 				}
4540a14842fSEric Dumazet 				break;
4550a14842fSEric Dumazet 			case BPF_S_ANC_PROTOCOL: /* A = ntohs(skb->protocol); */
4560a14842fSEric Dumazet 				BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
4570a14842fSEric Dumazet 				if (is_imm8(offsetof(struct sk_buff, protocol))) {
4580a14842fSEric Dumazet 					/* movzwl off8(%rdi),%eax */
4590a14842fSEric Dumazet 					EMIT4(0x0f, 0xb7, 0x47, offsetof(struct sk_buff, protocol));
4600a14842fSEric Dumazet 				} else {
4610a14842fSEric Dumazet 					EMIT3(0x0f, 0xb7, 0x87); /* movzwl off32(%rdi),%eax */
4620a14842fSEric Dumazet 					EMIT(offsetof(struct sk_buff, protocol), 4);
4630a14842fSEric Dumazet 				}
4640a14842fSEric Dumazet 				EMIT2(0x86, 0xc4); /* ntohs() : xchg   %al,%ah */
4650a14842fSEric Dumazet 				break;
4660a14842fSEric Dumazet 			case BPF_S_ANC_IFINDEX:
4670a14842fSEric Dumazet 				if (is_imm8(offsetof(struct sk_buff, dev))) {
4680a14842fSEric Dumazet 					/* movq off8(%rdi),%rax */
4690a14842fSEric Dumazet 					EMIT4(0x48, 0x8b, 0x47, offsetof(struct sk_buff, dev));
4700a14842fSEric Dumazet 				} else {
4710a14842fSEric Dumazet 					EMIT3(0x48, 0x8b, 0x87); /* movq off32(%rdi),%rax */
4720a14842fSEric Dumazet 					EMIT(offsetof(struct sk_buff, dev), 4);
4730a14842fSEric Dumazet 				}
4740a14842fSEric Dumazet 				EMIT3(0x48, 0x85, 0xc0);	/* test %rax,%rax */
4750a14842fSEric Dumazet 				EMIT_COND_JMP(X86_JE, cleanup_addr - (addrs[i] - 6));
4760a14842fSEric Dumazet 				BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
4770a14842fSEric Dumazet 				EMIT2(0x8b, 0x80);	/* mov off32(%rax),%eax */
4780a14842fSEric Dumazet 				EMIT(offsetof(struct net_device, ifindex), 4);
4790a14842fSEric Dumazet 				break;
4800a14842fSEric Dumazet 			case BPF_S_ANC_MARK:
4810a14842fSEric Dumazet 				BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
4820a14842fSEric Dumazet 				if (is_imm8(offsetof(struct sk_buff, mark))) {
4830a14842fSEric Dumazet 					/* mov off8(%rdi),%eax */
4840a14842fSEric Dumazet 					EMIT3(0x8b, 0x47, offsetof(struct sk_buff, mark));
4850a14842fSEric Dumazet 				} else {
4860a14842fSEric Dumazet 					EMIT2(0x8b, 0x87);
4870a14842fSEric Dumazet 					EMIT(offsetof(struct sk_buff, mark), 4);
4880a14842fSEric Dumazet 				}
4890a14842fSEric Dumazet 				break;
4900a14842fSEric Dumazet 			case BPF_S_ANC_RXHASH:
4910a14842fSEric Dumazet 				BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, rxhash) != 4);
4920a14842fSEric Dumazet 				if (is_imm8(offsetof(struct sk_buff, rxhash))) {
4930a14842fSEric Dumazet 					/* mov off8(%rdi),%eax */
4940a14842fSEric Dumazet 					EMIT3(0x8b, 0x47, offsetof(struct sk_buff, rxhash));
4950a14842fSEric Dumazet 				} else {
4960a14842fSEric Dumazet 					EMIT2(0x8b, 0x87);
4970a14842fSEric Dumazet 					EMIT(offsetof(struct sk_buff, rxhash), 4);
4980a14842fSEric Dumazet 				}
4990a14842fSEric Dumazet 				break;
5000a14842fSEric Dumazet 			case BPF_S_ANC_QUEUE:
5010a14842fSEric Dumazet 				BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, queue_mapping) != 2);
5020a14842fSEric Dumazet 				if (is_imm8(offsetof(struct sk_buff, queue_mapping))) {
5030a14842fSEric Dumazet 					/* movzwl off8(%rdi),%eax */
5040a14842fSEric Dumazet 					EMIT4(0x0f, 0xb7, 0x47, offsetof(struct sk_buff, queue_mapping));
5050a14842fSEric Dumazet 				} else {
5060a14842fSEric Dumazet 					EMIT3(0x0f, 0xb7, 0x87); /* movzwl off32(%rdi),%eax */
5070a14842fSEric Dumazet 					EMIT(offsetof(struct sk_buff, queue_mapping), 4);
5080a14842fSEric Dumazet 				}
5090a14842fSEric Dumazet 				break;
5100a14842fSEric Dumazet 			case BPF_S_ANC_CPU:
5110a14842fSEric Dumazet #ifdef CONFIG_SMP
5120a14842fSEric Dumazet 				EMIT4(0x65, 0x8b, 0x04, 0x25); /* mov %gs:off32,%eax */
5130a14842fSEric Dumazet 				EMIT((u32)(unsigned long)&cpu_number, 4); /* A = smp_processor_id(); */
5140a14842fSEric Dumazet #else
5150a14842fSEric Dumazet 				CLEAR_A();
5160a14842fSEric Dumazet #endif
5170a14842fSEric Dumazet 				break;
5180a14842fSEric Dumazet 			case BPF_S_LD_W_ABS:
519a998d434SJan Seiffert 				func = CHOOSE_LOAD_FUNC(K, sk_load_word);
5200a14842fSEric Dumazet common_load:			seen |= SEEN_DATAREF;
5210a14842fSEric Dumazet 				t_offset = func - (image + addrs[i]);
5220a14842fSEric Dumazet 				EMIT1_off32(0xbe, K); /* mov imm32,%esi */
5230a14842fSEric Dumazet 				EMIT1_off32(0xe8, t_offset); /* call */
5240a14842fSEric Dumazet 				break;
5250a14842fSEric Dumazet 			case BPF_S_LD_H_ABS:
526a998d434SJan Seiffert 				func = CHOOSE_LOAD_FUNC(K, sk_load_half);
5270a14842fSEric Dumazet 				goto common_load;
5280a14842fSEric Dumazet 			case BPF_S_LD_B_ABS:
529a998d434SJan Seiffert 				func = CHOOSE_LOAD_FUNC(K, sk_load_byte);
5300a14842fSEric Dumazet 				goto common_load;
5310a14842fSEric Dumazet 			case BPF_S_LDX_B_MSH:
532a998d434SJan Seiffert 				func = CHOOSE_LOAD_FUNC(K, sk_load_byte_msh);
5330a14842fSEric Dumazet 				seen |= SEEN_DATAREF | SEEN_XREG;
534a998d434SJan Seiffert 				t_offset = func - (image + addrs[i]);
5350a14842fSEric Dumazet 				EMIT1_off32(0xbe, K);	/* mov imm32,%esi */
5360a14842fSEric Dumazet 				EMIT1_off32(0xe8, t_offset); /* call sk_load_byte_msh */
5370a14842fSEric Dumazet 				break;
5380a14842fSEric Dumazet 			case BPF_S_LD_W_IND:
539a998d434SJan Seiffert 				func = sk_load_word;
5400a14842fSEric Dumazet common_load_ind:		seen |= SEEN_DATAREF | SEEN_XREG;
5410a14842fSEric Dumazet 				t_offset = func - (image + addrs[i]);
542a998d434SJan Seiffert 				if (K) {
543a998d434SJan Seiffert 					if (is_imm8(K)) {
544a998d434SJan Seiffert 						EMIT3(0x8d, 0x73, K); /* lea imm8(%rbx), %esi */
545a998d434SJan Seiffert 					} else {
546a998d434SJan Seiffert 						EMIT2(0x8d, 0xb3); /* lea imm32(%rbx),%esi */
547a998d434SJan Seiffert 						EMIT(K, 4);
548a998d434SJan Seiffert 					}
549a998d434SJan Seiffert 				} else {
550a998d434SJan Seiffert 					EMIT2(0x89,0xde); /* mov %ebx,%esi */
551a998d434SJan Seiffert 				}
5520a14842fSEric Dumazet 				EMIT1_off32(0xe8, t_offset);	/* call sk_load_xxx_ind */
5530a14842fSEric Dumazet 				break;
5540a14842fSEric Dumazet 			case BPF_S_LD_H_IND:
555a998d434SJan Seiffert 				func = sk_load_half;
5560a14842fSEric Dumazet 				goto common_load_ind;
5570a14842fSEric Dumazet 			case BPF_S_LD_B_IND:
558a998d434SJan Seiffert 				func = sk_load_byte;
5590a14842fSEric Dumazet 				goto common_load_ind;
5600a14842fSEric Dumazet 			case BPF_S_JMP_JA:
5610a14842fSEric Dumazet 				t_offset = addrs[i + K] - addrs[i];
5620a14842fSEric Dumazet 				EMIT_JMP(t_offset);
5630a14842fSEric Dumazet 				break;
5640a14842fSEric Dumazet 			COND_SEL(BPF_S_JMP_JGT_K, X86_JA, X86_JBE);
5650a14842fSEric Dumazet 			COND_SEL(BPF_S_JMP_JGE_K, X86_JAE, X86_JB);
5660a14842fSEric Dumazet 			COND_SEL(BPF_S_JMP_JEQ_K, X86_JE, X86_JNE);
5670a14842fSEric Dumazet 			COND_SEL(BPF_S_JMP_JSET_K,X86_JNE, X86_JE);
5680a14842fSEric Dumazet 			COND_SEL(BPF_S_JMP_JGT_X, X86_JA, X86_JBE);
5690a14842fSEric Dumazet 			COND_SEL(BPF_S_JMP_JGE_X, X86_JAE, X86_JB);
5700a14842fSEric Dumazet 			COND_SEL(BPF_S_JMP_JEQ_X, X86_JE, X86_JNE);
5710a14842fSEric Dumazet 			COND_SEL(BPF_S_JMP_JSET_X,X86_JNE, X86_JE);
5720a14842fSEric Dumazet 
5730a14842fSEric Dumazet cond_branch:			f_offset = addrs[i + filter[i].jf] - addrs[i];
5740a14842fSEric Dumazet 				t_offset = addrs[i + filter[i].jt] - addrs[i];
5750a14842fSEric Dumazet 
5760a14842fSEric Dumazet 				/* same targets, can avoid doing the test :) */
5770a14842fSEric Dumazet 				if (filter[i].jt == filter[i].jf) {
5780a14842fSEric Dumazet 					EMIT_JMP(t_offset);
5790a14842fSEric Dumazet 					break;
5800a14842fSEric Dumazet 				}
5810a14842fSEric Dumazet 
5820a14842fSEric Dumazet 				switch (filter[i].code) {
5830a14842fSEric Dumazet 				case BPF_S_JMP_JGT_X:
5840a14842fSEric Dumazet 				case BPF_S_JMP_JGE_X:
5850a14842fSEric Dumazet 				case BPF_S_JMP_JEQ_X:
5860a14842fSEric Dumazet 					seen |= SEEN_XREG;
5870a14842fSEric Dumazet 					EMIT2(0x39, 0xd8); /* cmp %ebx,%eax */
5880a14842fSEric Dumazet 					break;
5890a14842fSEric Dumazet 				case BPF_S_JMP_JSET_X:
5900a14842fSEric Dumazet 					seen |= SEEN_XREG;
5910a14842fSEric Dumazet 					EMIT2(0x85, 0xd8); /* test %ebx,%eax */
5920a14842fSEric Dumazet 					break;
5930a14842fSEric Dumazet 				case BPF_S_JMP_JEQ_K:
5940a14842fSEric Dumazet 					if (K == 0) {
5950a14842fSEric Dumazet 						EMIT2(0x85, 0xc0); /* test   %eax,%eax */
5960a14842fSEric Dumazet 						break;
5970a14842fSEric Dumazet 					}
5980a14842fSEric Dumazet 				case BPF_S_JMP_JGT_K:
5990a14842fSEric Dumazet 				case BPF_S_JMP_JGE_K:
6000a14842fSEric Dumazet 					if (K <= 127)
6010a14842fSEric Dumazet 						EMIT3(0x83, 0xf8, K); /* cmp imm8,%eax */
6020a14842fSEric Dumazet 					else
6030a14842fSEric Dumazet 						EMIT1_off32(0x3d, K); /* cmp imm32,%eax */
6040a14842fSEric Dumazet 					break;
6050a14842fSEric Dumazet 				case BPF_S_JMP_JSET_K:
6060a14842fSEric Dumazet 					if (K <= 0xFF)
6070a14842fSEric Dumazet 						EMIT2(0xa8, K); /* test imm8,%al */
6080a14842fSEric Dumazet 					else if (!(K & 0xFFFF00FF))
6090a14842fSEric Dumazet 						EMIT3(0xf6, 0xc4, K >> 8); /* test imm8,%ah */
6100a14842fSEric Dumazet 					else if (K <= 0xFFFF) {
6110a14842fSEric Dumazet 						EMIT2(0x66, 0xa9); /* test imm16,%ax */
6120a14842fSEric Dumazet 						EMIT(K, 2);
6130a14842fSEric Dumazet 					} else {
6140a14842fSEric Dumazet 						EMIT1_off32(0xa9, K); /* test imm32,%eax */
6150a14842fSEric Dumazet 					}
6160a14842fSEric Dumazet 					break;
6170a14842fSEric Dumazet 				}
6180a14842fSEric Dumazet 				if (filter[i].jt != 0) {
619a03ffcf8SMarkus Kötter 					if (filter[i].jf && f_offset)
620a03ffcf8SMarkus Kötter 						t_offset += is_near(f_offset) ? 2 : 5;
6210a14842fSEric Dumazet 					EMIT_COND_JMP(t_op, t_offset);
6220a14842fSEric Dumazet 					if (filter[i].jf)
6230a14842fSEric Dumazet 						EMIT_JMP(f_offset);
6240a14842fSEric Dumazet 					break;
6250a14842fSEric Dumazet 				}
6260a14842fSEric Dumazet 				EMIT_COND_JMP(f_op, f_offset);
6270a14842fSEric Dumazet 				break;
6280a14842fSEric Dumazet 			default:
6290a14842fSEric Dumazet 				/* hmm, too complex filter, give up with jit compiler */
6300a14842fSEric Dumazet 				goto out;
6310a14842fSEric Dumazet 			}
6320a14842fSEric Dumazet 			ilen = prog - temp;
6330a14842fSEric Dumazet 			if (image) {
6340a14842fSEric Dumazet 				if (unlikely(proglen + ilen > oldproglen)) {
6350a14842fSEric Dumazet 					pr_err("bpb_jit_compile fatal error\n");
6360a14842fSEric Dumazet 					kfree(addrs);
6370a14842fSEric Dumazet 					module_free(NULL, image);
6380a14842fSEric Dumazet 					return;
6390a14842fSEric Dumazet 				}
6400a14842fSEric Dumazet 				memcpy(image + proglen, temp, ilen);
6410a14842fSEric Dumazet 			}
6420a14842fSEric Dumazet 			proglen += ilen;
6430a14842fSEric Dumazet 			addrs[i] = proglen;
6440a14842fSEric Dumazet 			prog = temp;
6450a14842fSEric Dumazet 		}
6460a14842fSEric Dumazet 		/* last bpf instruction is always a RET :
6470a14842fSEric Dumazet 		 * use it to give the cleanup instruction(s) addr
6480a14842fSEric Dumazet 		 */
6490a14842fSEric Dumazet 		cleanup_addr = proglen - 1; /* ret */
650d00a9dd2SEric Dumazet 		if (seen_or_pass0)
6510a14842fSEric Dumazet 			cleanup_addr -= 1; /* leaveq */
652d00a9dd2SEric Dumazet 		if (seen_or_pass0 & SEEN_XREG)
6530a14842fSEric Dumazet 			cleanup_addr -= 4; /* mov  -8(%rbp),%rbx */
6540a14842fSEric Dumazet 
6550a14842fSEric Dumazet 		if (image) {
656d00a9dd2SEric Dumazet 			if (proglen != oldproglen)
657d00a9dd2SEric Dumazet 				pr_err("bpb_jit_compile proglen=%u != oldproglen=%u\n", proglen, oldproglen);
6580a14842fSEric Dumazet 			break;
6590a14842fSEric Dumazet 		}
6600a14842fSEric Dumazet 		if (proglen == oldproglen) {
6610a14842fSEric Dumazet 			image = module_alloc(max_t(unsigned int,
6620a14842fSEric Dumazet 						   proglen,
6630a14842fSEric Dumazet 						   sizeof(struct work_struct)));
6640a14842fSEric Dumazet 			if (!image)
6650a14842fSEric Dumazet 				goto out;
6660a14842fSEric Dumazet 		}
6670a14842fSEric Dumazet 		oldproglen = proglen;
6680a14842fSEric Dumazet 	}
6690a14842fSEric Dumazet 	if (bpf_jit_enable > 1)
6700a14842fSEric Dumazet 		pr_err("flen=%d proglen=%u pass=%d image=%p\n",
6710a14842fSEric Dumazet 		       flen, proglen, pass, image);
6720a14842fSEric Dumazet 
6730a14842fSEric Dumazet 	if (image) {
6740a14842fSEric Dumazet 		if (bpf_jit_enable > 1)
6750a14842fSEric Dumazet 			print_hex_dump(KERN_ERR, "JIT code: ", DUMP_PREFIX_ADDRESS,
6760a14842fSEric Dumazet 				       16, 1, image, proglen, false);
6770a14842fSEric Dumazet 
6780a14842fSEric Dumazet 		bpf_flush_icache(image, image + proglen);
6790a14842fSEric Dumazet 
6800a14842fSEric Dumazet 		fp->bpf_func = (void *)image;
6810a14842fSEric Dumazet 	}
6820a14842fSEric Dumazet out:
6830a14842fSEric Dumazet 	kfree(addrs);
6840a14842fSEric Dumazet 	return;
6850a14842fSEric Dumazet }
6860a14842fSEric Dumazet 
6870a14842fSEric Dumazet static void jit_free_defer(struct work_struct *arg)
6880a14842fSEric Dumazet {
6890a14842fSEric Dumazet 	module_free(NULL, arg);
6900a14842fSEric Dumazet }
6910a14842fSEric Dumazet 
6920a14842fSEric Dumazet /* run from softirq, we must use a work_struct to call
6930a14842fSEric Dumazet  * module_free() from process context
6940a14842fSEric Dumazet  */
6950a14842fSEric Dumazet void bpf_jit_free(struct sk_filter *fp)
6960a14842fSEric Dumazet {
6970a14842fSEric Dumazet 	if (fp->bpf_func != sk_run_filter) {
6980a14842fSEric Dumazet 		struct work_struct *work = (struct work_struct *)fp->bpf_func;
6990a14842fSEric Dumazet 
7000a14842fSEric Dumazet 		INIT_WORK(work, jit_free_defer);
7010a14842fSEric Dumazet 		schedule_work(work);
7020a14842fSEric Dumazet 	}
7030a14842fSEric Dumazet }
704