18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci#include <linux/moduleloader.h>
38c2ecf20Sopenharmony_ci#include <linux/workqueue.h>
48c2ecf20Sopenharmony_ci#include <linux/netdevice.h>
58c2ecf20Sopenharmony_ci#include <linux/filter.h>
68c2ecf20Sopenharmony_ci#include <linux/cache.h>
78c2ecf20Sopenharmony_ci#include <linux/if_vlan.h>
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <asm/cacheflush.h>
108c2ecf20Sopenharmony_ci#include <asm/ptrace.h>
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include "bpf_jit_32.h"
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_cistatic inline bool is_simm13(unsigned int value)
158c2ecf20Sopenharmony_ci{
168c2ecf20Sopenharmony_ci	return value + 0x1000 < 0x2000;
178c2ecf20Sopenharmony_ci}
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#define SEEN_DATAREF 1 /* might call external helpers */
208c2ecf20Sopenharmony_ci#define SEEN_XREG    2 /* ebx is used */
218c2ecf20Sopenharmony_ci#define SEEN_MEM     4 /* use mem[] for temporary storage */
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#define S13(X)		((X) & 0x1fff)
248c2ecf20Sopenharmony_ci#define IMMED		0x00002000
258c2ecf20Sopenharmony_ci#define RD(X)		((X) << 25)
268c2ecf20Sopenharmony_ci#define RS1(X)		((X) << 14)
278c2ecf20Sopenharmony_ci#define RS2(X)		((X))
288c2ecf20Sopenharmony_ci#define OP(X)		((X) << 30)
298c2ecf20Sopenharmony_ci#define OP2(X)		((X) << 22)
308c2ecf20Sopenharmony_ci#define OP3(X)		((X) << 19)
318c2ecf20Sopenharmony_ci#define COND(X)		((X) << 25)
328c2ecf20Sopenharmony_ci#define F1(X)		OP(X)
338c2ecf20Sopenharmony_ci#define F2(X, Y)	(OP(X) | OP2(Y))
348c2ecf20Sopenharmony_ci#define F3(X, Y)	(OP(X) | OP3(Y))
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci#define CONDN		COND(0x0)
378c2ecf20Sopenharmony_ci#define CONDE		COND(0x1)
388c2ecf20Sopenharmony_ci#define CONDLE		COND(0x2)
398c2ecf20Sopenharmony_ci#define CONDL		COND(0x3)
408c2ecf20Sopenharmony_ci#define CONDLEU		COND(0x4)
418c2ecf20Sopenharmony_ci#define CONDCS		COND(0x5)
428c2ecf20Sopenharmony_ci#define CONDNEG		COND(0x6)
438c2ecf20Sopenharmony_ci#define CONDVC		COND(0x7)
448c2ecf20Sopenharmony_ci#define CONDA		COND(0x8)
458c2ecf20Sopenharmony_ci#define CONDNE		COND(0x9)
468c2ecf20Sopenharmony_ci#define CONDG		COND(0xa)
478c2ecf20Sopenharmony_ci#define CONDGE		COND(0xb)
488c2ecf20Sopenharmony_ci#define CONDGU		COND(0xc)
498c2ecf20Sopenharmony_ci#define CONDCC		COND(0xd)
508c2ecf20Sopenharmony_ci#define CONDPOS		COND(0xe)
518c2ecf20Sopenharmony_ci#define CONDVS		COND(0xf)
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci#define CONDGEU		CONDCC
548c2ecf20Sopenharmony_ci#define CONDLU		CONDCS
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci#define WDISP22(X)	(((X) >> 2) & 0x3fffff)
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci#define BA		(F2(0, 2) | CONDA)
598c2ecf20Sopenharmony_ci#define BGU		(F2(0, 2) | CONDGU)
608c2ecf20Sopenharmony_ci#define BLEU		(F2(0, 2) | CONDLEU)
618c2ecf20Sopenharmony_ci#define BGEU		(F2(0, 2) | CONDGEU)
628c2ecf20Sopenharmony_ci#define BLU		(F2(0, 2) | CONDLU)
638c2ecf20Sopenharmony_ci#define BE		(F2(0, 2) | CONDE)
648c2ecf20Sopenharmony_ci#define BNE		(F2(0, 2) | CONDNE)
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci#define BE_PTR		BE
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci#define SETHI(K, REG)	\
698c2ecf20Sopenharmony_ci	(F2(0, 0x4) | RD(REG) | (((K) >> 10) & 0x3fffff))
708c2ecf20Sopenharmony_ci#define OR_LO(K, REG)	\
718c2ecf20Sopenharmony_ci	(F3(2, 0x02) | IMMED | RS1(REG) | ((K) & 0x3ff) | RD(REG))
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci#define ADD		F3(2, 0x00)
748c2ecf20Sopenharmony_ci#define AND		F3(2, 0x01)
758c2ecf20Sopenharmony_ci#define ANDCC		F3(2, 0x11)
768c2ecf20Sopenharmony_ci#define OR		F3(2, 0x02)
778c2ecf20Sopenharmony_ci#define XOR		F3(2, 0x03)
788c2ecf20Sopenharmony_ci#define SUB		F3(2, 0x04)
798c2ecf20Sopenharmony_ci#define SUBCC		F3(2, 0x14)
808c2ecf20Sopenharmony_ci#define MUL		F3(2, 0x0a)	/* umul */
818c2ecf20Sopenharmony_ci#define DIV		F3(2, 0x0e)	/* udiv */
828c2ecf20Sopenharmony_ci#define SLL		F3(2, 0x25)
838c2ecf20Sopenharmony_ci#define SRL		F3(2, 0x26)
848c2ecf20Sopenharmony_ci#define JMPL		F3(2, 0x38)
858c2ecf20Sopenharmony_ci#define CALL		F1(1)
868c2ecf20Sopenharmony_ci#define BR		F2(0, 0x01)
878c2ecf20Sopenharmony_ci#define RD_Y		F3(2, 0x28)
888c2ecf20Sopenharmony_ci#define WR_Y		F3(2, 0x30)
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci#define LD32		F3(3, 0x00)
918c2ecf20Sopenharmony_ci#define LD8		F3(3, 0x01)
928c2ecf20Sopenharmony_ci#define LD16		F3(3, 0x02)
938c2ecf20Sopenharmony_ci#define LD64		F3(3, 0x0b)
948c2ecf20Sopenharmony_ci#define ST32		F3(3, 0x04)
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci#define LDPTR		LD32
978c2ecf20Sopenharmony_ci#define BASE_STACKFRAME	96
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci#define LD32I		(LD32 | IMMED)
1008c2ecf20Sopenharmony_ci#define LD8I		(LD8 | IMMED)
1018c2ecf20Sopenharmony_ci#define LD16I		(LD16 | IMMED)
1028c2ecf20Sopenharmony_ci#define LD64I		(LD64 | IMMED)
1038c2ecf20Sopenharmony_ci#define LDPTRI		(LDPTR | IMMED)
1048c2ecf20Sopenharmony_ci#define ST32I		(ST32 | IMMED)
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci#define emit_nop()		\
1078c2ecf20Sopenharmony_cido {				\
1088c2ecf20Sopenharmony_ci	*prog++ = SETHI(0, G0);	\
1098c2ecf20Sopenharmony_ci} while (0)
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci#define emit_neg()					\
1128c2ecf20Sopenharmony_cido {	/* sub %g0, r_A, r_A */				\
1138c2ecf20Sopenharmony_ci	*prog++ = SUB | RS1(G0) | RS2(r_A) | RD(r_A);	\
1148c2ecf20Sopenharmony_ci} while (0)
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci#define emit_reg_move(FROM, TO)				\
1178c2ecf20Sopenharmony_cido {	/* or %g0, FROM, TO */				\
1188c2ecf20Sopenharmony_ci	*prog++ = OR | RS1(G0) | RS2(FROM) | RD(TO);	\
1198c2ecf20Sopenharmony_ci} while (0)
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci#define emit_clear(REG)					\
1228c2ecf20Sopenharmony_cido {	/* or %g0, %g0, REG */				\
1238c2ecf20Sopenharmony_ci	*prog++ = OR | RS1(G0) | RS2(G0) | RD(REG);	\
1248c2ecf20Sopenharmony_ci} while (0)
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci#define emit_set_const(K, REG)					\
1278c2ecf20Sopenharmony_cido {	/* sethi %hi(K), REG */					\
1288c2ecf20Sopenharmony_ci	*prog++ = SETHI(K, REG);				\
1298c2ecf20Sopenharmony_ci	/* or REG, %lo(K), REG */				\
1308c2ecf20Sopenharmony_ci	*prog++ = OR_LO(K, REG);				\
1318c2ecf20Sopenharmony_ci} while (0)
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	/* Emit
1348c2ecf20Sopenharmony_ci	 *
1358c2ecf20Sopenharmony_ci	 *	OP	r_A, r_X, r_A
1368c2ecf20Sopenharmony_ci	 */
1378c2ecf20Sopenharmony_ci#define emit_alu_X(OPCODE)					\
1388c2ecf20Sopenharmony_cido {								\
1398c2ecf20Sopenharmony_ci	seen |= SEEN_XREG;					\
1408c2ecf20Sopenharmony_ci	*prog++ = OPCODE | RS1(r_A) | RS2(r_X) | RD(r_A);	\
1418c2ecf20Sopenharmony_ci} while (0)
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	/* Emit either:
1448c2ecf20Sopenharmony_ci	 *
1458c2ecf20Sopenharmony_ci	 *	OP	r_A, K, r_A
1468c2ecf20Sopenharmony_ci	 *
1478c2ecf20Sopenharmony_ci	 * or
1488c2ecf20Sopenharmony_ci	 *
1498c2ecf20Sopenharmony_ci	 *	sethi	%hi(K), r_TMP
1508c2ecf20Sopenharmony_ci	 *	or	r_TMP, %lo(K), r_TMP
1518c2ecf20Sopenharmony_ci	 *	OP	r_A, r_TMP, r_A
1528c2ecf20Sopenharmony_ci	 *
1538c2ecf20Sopenharmony_ci	 * depending upon whether K fits in a signed 13-bit
1548c2ecf20Sopenharmony_ci	 * immediate instruction field.  Emit nothing if K
1558c2ecf20Sopenharmony_ci	 * is zero.
1568c2ecf20Sopenharmony_ci	 */
1578c2ecf20Sopenharmony_ci#define emit_alu_K(OPCODE, K)					\
1588c2ecf20Sopenharmony_cido {								\
1598c2ecf20Sopenharmony_ci	if (K || OPCODE == AND || OPCODE == MUL) {		\
1608c2ecf20Sopenharmony_ci		unsigned int _insn = OPCODE;			\
1618c2ecf20Sopenharmony_ci		_insn |= RS1(r_A) | RD(r_A);			\
1628c2ecf20Sopenharmony_ci		if (is_simm13(K)) {				\
1638c2ecf20Sopenharmony_ci			*prog++ = _insn | IMMED | S13(K);	\
1648c2ecf20Sopenharmony_ci		} else {					\
1658c2ecf20Sopenharmony_ci			emit_set_const(K, r_TMP);		\
1668c2ecf20Sopenharmony_ci			*prog++ = _insn | RS2(r_TMP);		\
1678c2ecf20Sopenharmony_ci		}						\
1688c2ecf20Sopenharmony_ci	}							\
1698c2ecf20Sopenharmony_ci} while (0)
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci#define emit_loadimm(K, DEST)						\
1728c2ecf20Sopenharmony_cido {									\
1738c2ecf20Sopenharmony_ci	if (is_simm13(K)) {						\
1748c2ecf20Sopenharmony_ci		/* or %g0, K, DEST */					\
1758c2ecf20Sopenharmony_ci		*prog++ = OR | IMMED | RS1(G0) | S13(K) | RD(DEST);	\
1768c2ecf20Sopenharmony_ci	} else {							\
1778c2ecf20Sopenharmony_ci		emit_set_const(K, DEST);				\
1788c2ecf20Sopenharmony_ci	}								\
1798c2ecf20Sopenharmony_ci} while (0)
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci#define emit_loadptr(BASE, STRUCT, FIELD, DEST)				\
1828c2ecf20Sopenharmony_cido {	unsigned int _off = offsetof(STRUCT, FIELD);			\
1838c2ecf20Sopenharmony_ci	BUILD_BUG_ON(sizeof_field(STRUCT, FIELD) != sizeof(void *));	\
1848c2ecf20Sopenharmony_ci	*prog++ = LDPTRI | RS1(BASE) | S13(_off) | RD(DEST);		\
1858c2ecf20Sopenharmony_ci} while (0)
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci#define emit_load32(BASE, STRUCT, FIELD, DEST)				\
1888c2ecf20Sopenharmony_cido {	unsigned int _off = offsetof(STRUCT, FIELD);			\
1898c2ecf20Sopenharmony_ci	BUILD_BUG_ON(sizeof_field(STRUCT, FIELD) != sizeof(u32));	\
1908c2ecf20Sopenharmony_ci	*prog++ = LD32I | RS1(BASE) | S13(_off) | RD(DEST);		\
1918c2ecf20Sopenharmony_ci} while (0)
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci#define emit_load16(BASE, STRUCT, FIELD, DEST)				\
1948c2ecf20Sopenharmony_cido {	unsigned int _off = offsetof(STRUCT, FIELD);			\
1958c2ecf20Sopenharmony_ci	BUILD_BUG_ON(sizeof_field(STRUCT, FIELD) != sizeof(u16));	\
1968c2ecf20Sopenharmony_ci	*prog++ = LD16I | RS1(BASE) | S13(_off) | RD(DEST);		\
1978c2ecf20Sopenharmony_ci} while (0)
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci#define __emit_load8(BASE, STRUCT, FIELD, DEST)				\
2008c2ecf20Sopenharmony_cido {	unsigned int _off = offsetof(STRUCT, FIELD);			\
2018c2ecf20Sopenharmony_ci	*prog++ = LD8I | RS1(BASE) | S13(_off) | RD(DEST);		\
2028c2ecf20Sopenharmony_ci} while (0)
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci#define emit_load8(BASE, STRUCT, FIELD, DEST)				\
2058c2ecf20Sopenharmony_cido {	BUILD_BUG_ON(sizeof_field(STRUCT, FIELD) != sizeof(u8));	\
2068c2ecf20Sopenharmony_ci	__emit_load8(BASE, STRUCT, FIELD, DEST);			\
2078c2ecf20Sopenharmony_ci} while (0)
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci#define BIAS (-4)
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci#define emit_ldmem(OFF, DEST)						\
2128c2ecf20Sopenharmony_cido {	*prog++ = LD32I | RS1(SP) | S13(BIAS - (OFF)) | RD(DEST);	\
2138c2ecf20Sopenharmony_ci} while (0)
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci#define emit_stmem(OFF, SRC)						\
2168c2ecf20Sopenharmony_cido {	*prog++ = ST32I | RS1(SP) | S13(BIAS - (OFF)) | RD(SRC);	\
2178c2ecf20Sopenharmony_ci} while (0)
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci#ifdef CONFIG_SMP
2208c2ecf20Sopenharmony_ci#define emit_load_cpu(REG)						\
2218c2ecf20Sopenharmony_ci	emit_load32(G6, struct thread_info, cpu, REG)
2228c2ecf20Sopenharmony_ci#else
2238c2ecf20Sopenharmony_ci#define emit_load_cpu(REG)	emit_clear(REG)
2248c2ecf20Sopenharmony_ci#endif
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci#define emit_skb_loadptr(FIELD, DEST) \
2278c2ecf20Sopenharmony_ci	emit_loadptr(r_SKB, struct sk_buff, FIELD, DEST)
2288c2ecf20Sopenharmony_ci#define emit_skb_load32(FIELD, DEST) \
2298c2ecf20Sopenharmony_ci	emit_load32(r_SKB, struct sk_buff, FIELD, DEST)
2308c2ecf20Sopenharmony_ci#define emit_skb_load16(FIELD, DEST) \
2318c2ecf20Sopenharmony_ci	emit_load16(r_SKB, struct sk_buff, FIELD, DEST)
2328c2ecf20Sopenharmony_ci#define __emit_skb_load8(FIELD, DEST) \
2338c2ecf20Sopenharmony_ci	__emit_load8(r_SKB, struct sk_buff, FIELD, DEST)
2348c2ecf20Sopenharmony_ci#define emit_skb_load8(FIELD, DEST) \
2358c2ecf20Sopenharmony_ci	emit_load8(r_SKB, struct sk_buff, FIELD, DEST)
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci#define emit_jmpl(BASE, IMM_OFF, LREG) \
2388c2ecf20Sopenharmony_ci	*prog++ = (JMPL | IMMED | RS1(BASE) | S13(IMM_OFF) | RD(LREG))
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci#define emit_call(FUNC)					\
2418c2ecf20Sopenharmony_cido {	void *_here = image + addrs[i] - 8;		\
2428c2ecf20Sopenharmony_ci	unsigned int _off = (void *)(FUNC) - _here;	\
2438c2ecf20Sopenharmony_ci	*prog++ = CALL | (((_off) >> 2) & 0x3fffffff);	\
2448c2ecf20Sopenharmony_ci	emit_nop();					\
2458c2ecf20Sopenharmony_ci} while (0)
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci#define emit_branch(BR_OPC, DEST)			\
2488c2ecf20Sopenharmony_cido {	unsigned int _here = addrs[i] - 8;		\
2498c2ecf20Sopenharmony_ci	*prog++ = BR_OPC | WDISP22((DEST) - _here);	\
2508c2ecf20Sopenharmony_ci} while (0)
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci#define emit_branch_off(BR_OPC, OFF)			\
2538c2ecf20Sopenharmony_cido {	*prog++ = BR_OPC | WDISP22(OFF);		\
2548c2ecf20Sopenharmony_ci} while (0)
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci#define emit_jump(DEST)		emit_branch(BA, DEST)
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci#define emit_read_y(REG)	*prog++ = RD_Y | RD(REG)
2598c2ecf20Sopenharmony_ci#define emit_write_y(REG)	*prog++ = WR_Y | IMMED | RS1(REG) | S13(0)
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci#define emit_cmp(R1, R2) \
2628c2ecf20Sopenharmony_ci	*prog++ = (SUBCC | RS1(R1) | RS2(R2) | RD(G0))
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci#define emit_cmpi(R1, IMM) \
2658c2ecf20Sopenharmony_ci	*prog++ = (SUBCC | IMMED | RS1(R1) | S13(IMM) | RD(G0));
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci#define emit_btst(R1, R2) \
2688c2ecf20Sopenharmony_ci	*prog++ = (ANDCC | RS1(R1) | RS2(R2) | RD(G0))
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci#define emit_btsti(R1, IMM) \
2718c2ecf20Sopenharmony_ci	*prog++ = (ANDCC | IMMED | RS1(R1) | S13(IMM) | RD(G0));
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci#define emit_sub(R1, R2, R3) \
2748c2ecf20Sopenharmony_ci	*prog++ = (SUB | RS1(R1) | RS2(R2) | RD(R3))
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci#define emit_subi(R1, IMM, R3) \
2778c2ecf20Sopenharmony_ci	*prog++ = (SUB | IMMED | RS1(R1) | S13(IMM) | RD(R3))
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci#define emit_add(R1, R2, R3) \
2808c2ecf20Sopenharmony_ci	*prog++ = (ADD | RS1(R1) | RS2(R2) | RD(R3))
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci#define emit_addi(R1, IMM, R3) \
2838c2ecf20Sopenharmony_ci	*prog++ = (ADD | IMMED | RS1(R1) | S13(IMM) | RD(R3))
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci#define emit_and(R1, R2, R3) \
2868c2ecf20Sopenharmony_ci	*prog++ = (AND | RS1(R1) | RS2(R2) | RD(R3))
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci#define emit_andi(R1, IMM, R3) \
2898c2ecf20Sopenharmony_ci	*prog++ = (AND | IMMED | RS1(R1) | S13(IMM) | RD(R3))
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci#define emit_alloc_stack(SZ) \
2928c2ecf20Sopenharmony_ci	*prog++ = (SUB | IMMED | RS1(SP) | S13(SZ) | RD(SP))
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci#define emit_release_stack(SZ) \
2958c2ecf20Sopenharmony_ci	*prog++ = (ADD | IMMED | RS1(SP) | S13(SZ) | RD(SP))
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci/* A note about branch offset calculations.  The addrs[] array,
2988c2ecf20Sopenharmony_ci * indexed by BPF instruction, records the address after all the
2998c2ecf20Sopenharmony_ci * sparc instructions emitted for that BPF instruction.
3008c2ecf20Sopenharmony_ci *
3018c2ecf20Sopenharmony_ci * The most common case is to emit a branch at the end of such
3028c2ecf20Sopenharmony_ci * a code sequence.  So this would be two instructions, the
3038c2ecf20Sopenharmony_ci * branch and it's delay slot.
3048c2ecf20Sopenharmony_ci *
3058c2ecf20Sopenharmony_ci * Therefore by default the branch emitters calculate the branch
3068c2ecf20Sopenharmony_ci * offset field as:
3078c2ecf20Sopenharmony_ci *
3088c2ecf20Sopenharmony_ci *	destination - (addrs[i] - 8)
3098c2ecf20Sopenharmony_ci *
3108c2ecf20Sopenharmony_ci * This "addrs[i] - 8" is the address of the branch itself or
3118c2ecf20Sopenharmony_ci * what "." would be in assembler notation.  The "8" part is
3128c2ecf20Sopenharmony_ci * how we take into consideration the branch and it's delay
3138c2ecf20Sopenharmony_ci * slot mentioned above.
3148c2ecf20Sopenharmony_ci *
3158c2ecf20Sopenharmony_ci * Sometimes we need to emit a branch earlier in the code
3168c2ecf20Sopenharmony_ci * sequence.  And in these situations we adjust "destination"
3178c2ecf20Sopenharmony_ci * to accommodate this difference.  For example, if we needed
3188c2ecf20Sopenharmony_ci * to emit a branch (and it's delay slot) right before the
3198c2ecf20Sopenharmony_ci * final instruction emitted for a BPF opcode, we'd use
3208c2ecf20Sopenharmony_ci * "destination + 4" instead of just plain "destination" above.
3218c2ecf20Sopenharmony_ci *
3228c2ecf20Sopenharmony_ci * This is why you see all of these funny emit_branch() and
3238c2ecf20Sopenharmony_ci * emit_jump() calls with adjusted offsets.
3248c2ecf20Sopenharmony_ci */
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_civoid bpf_jit_compile(struct bpf_prog *fp)
3278c2ecf20Sopenharmony_ci{
3288c2ecf20Sopenharmony_ci	unsigned int cleanup_addr, proglen, oldproglen = 0;
3298c2ecf20Sopenharmony_ci	u32 temp[8], *prog, *func, seen = 0, pass;
3308c2ecf20Sopenharmony_ci	const struct sock_filter *filter = fp->insns;
3318c2ecf20Sopenharmony_ci	int i, flen = fp->len, pc_ret0 = -1;
3328c2ecf20Sopenharmony_ci	unsigned int *addrs;
3338c2ecf20Sopenharmony_ci	void *image;
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci	if (!bpf_jit_enable)
3368c2ecf20Sopenharmony_ci		return;
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_ci	addrs = kmalloc_array(flen, sizeof(*addrs), GFP_KERNEL);
3398c2ecf20Sopenharmony_ci	if (addrs == NULL)
3408c2ecf20Sopenharmony_ci		return;
3418c2ecf20Sopenharmony_ci
3428c2ecf20Sopenharmony_ci	/* Before first pass, make a rough estimation of addrs[]
3438c2ecf20Sopenharmony_ci	 * each bpf instruction is translated to less than 64 bytes
3448c2ecf20Sopenharmony_ci	 */
3458c2ecf20Sopenharmony_ci	for (proglen = 0, i = 0; i < flen; i++) {
3468c2ecf20Sopenharmony_ci		proglen += 64;
3478c2ecf20Sopenharmony_ci		addrs[i] = proglen;
3488c2ecf20Sopenharmony_ci	}
3498c2ecf20Sopenharmony_ci	cleanup_addr = proglen; /* epilogue address */
3508c2ecf20Sopenharmony_ci	image = NULL;
3518c2ecf20Sopenharmony_ci	for (pass = 0; pass < 10; pass++) {
3528c2ecf20Sopenharmony_ci		u8 seen_or_pass0 = (pass == 0) ? (SEEN_XREG | SEEN_DATAREF | SEEN_MEM) : seen;
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci		/* no prologue/epilogue for trivial filters (RET something) */
3558c2ecf20Sopenharmony_ci		proglen = 0;
3568c2ecf20Sopenharmony_ci		prog = temp;
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci		/* Prologue */
3598c2ecf20Sopenharmony_ci		if (seen_or_pass0) {
3608c2ecf20Sopenharmony_ci			if (seen_or_pass0 & SEEN_MEM) {
3618c2ecf20Sopenharmony_ci				unsigned int sz = BASE_STACKFRAME;
3628c2ecf20Sopenharmony_ci				sz += BPF_MEMWORDS * sizeof(u32);
3638c2ecf20Sopenharmony_ci				emit_alloc_stack(sz);
3648c2ecf20Sopenharmony_ci			}
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci			/* Make sure we dont leek kernel memory. */
3678c2ecf20Sopenharmony_ci			if (seen_or_pass0 & SEEN_XREG)
3688c2ecf20Sopenharmony_ci				emit_clear(r_X);
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci			/* If this filter needs to access skb data,
3718c2ecf20Sopenharmony_ci			 * load %o4 and %o5 with:
3728c2ecf20Sopenharmony_ci			 *  %o4 = skb->len - skb->data_len
3738c2ecf20Sopenharmony_ci			 *  %o5 = skb->data
3748c2ecf20Sopenharmony_ci			 * And also back up %o7 into r_saved_O7 so we can
3758c2ecf20Sopenharmony_ci			 * invoke the stubs using 'call'.
3768c2ecf20Sopenharmony_ci			 */
3778c2ecf20Sopenharmony_ci			if (seen_or_pass0 & SEEN_DATAREF) {
3788c2ecf20Sopenharmony_ci				emit_load32(r_SKB, struct sk_buff, len, r_HEADLEN);
3798c2ecf20Sopenharmony_ci				emit_load32(r_SKB, struct sk_buff, data_len, r_TMP);
3808c2ecf20Sopenharmony_ci				emit_sub(r_HEADLEN, r_TMP, r_HEADLEN);
3818c2ecf20Sopenharmony_ci				emit_loadptr(r_SKB, struct sk_buff, data, r_SKB_DATA);
3828c2ecf20Sopenharmony_ci			}
3838c2ecf20Sopenharmony_ci		}
3848c2ecf20Sopenharmony_ci		emit_reg_move(O7, r_saved_O7);
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci		/* Make sure we dont leak kernel information to the user. */
3878c2ecf20Sopenharmony_ci		if (bpf_needs_clear_a(&filter[0]))
3888c2ecf20Sopenharmony_ci			emit_clear(r_A); /* A = 0 */
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ci		for (i = 0; i < flen; i++) {
3918c2ecf20Sopenharmony_ci			unsigned int K = filter[i].k;
3928c2ecf20Sopenharmony_ci			unsigned int t_offset;
3938c2ecf20Sopenharmony_ci			unsigned int f_offset;
3948c2ecf20Sopenharmony_ci			u32 t_op, f_op;
3958c2ecf20Sopenharmony_ci			u16 code = bpf_anc_helper(&filter[i]);
3968c2ecf20Sopenharmony_ci			int ilen;
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci			switch (code) {
3998c2ecf20Sopenharmony_ci			case BPF_ALU | BPF_ADD | BPF_X:	/* A += X; */
4008c2ecf20Sopenharmony_ci				emit_alu_X(ADD);
4018c2ecf20Sopenharmony_ci				break;
4028c2ecf20Sopenharmony_ci			case BPF_ALU | BPF_ADD | BPF_K:	/* A += K; */
4038c2ecf20Sopenharmony_ci				emit_alu_K(ADD, K);
4048c2ecf20Sopenharmony_ci				break;
4058c2ecf20Sopenharmony_ci			case BPF_ALU | BPF_SUB | BPF_X:	/* A -= X; */
4068c2ecf20Sopenharmony_ci				emit_alu_X(SUB);
4078c2ecf20Sopenharmony_ci				break;
4088c2ecf20Sopenharmony_ci			case BPF_ALU | BPF_SUB | BPF_K:	/* A -= K */
4098c2ecf20Sopenharmony_ci				emit_alu_K(SUB, K);
4108c2ecf20Sopenharmony_ci				break;
4118c2ecf20Sopenharmony_ci			case BPF_ALU | BPF_AND | BPF_X:	/* A &= X */
4128c2ecf20Sopenharmony_ci				emit_alu_X(AND);
4138c2ecf20Sopenharmony_ci				break;
4148c2ecf20Sopenharmony_ci			case BPF_ALU | BPF_AND | BPF_K:	/* A &= K */
4158c2ecf20Sopenharmony_ci				emit_alu_K(AND, K);
4168c2ecf20Sopenharmony_ci				break;
4178c2ecf20Sopenharmony_ci			case BPF_ALU | BPF_OR | BPF_X:	/* A |= X */
4188c2ecf20Sopenharmony_ci				emit_alu_X(OR);
4198c2ecf20Sopenharmony_ci				break;
4208c2ecf20Sopenharmony_ci			case BPF_ALU | BPF_OR | BPF_K:	/* A |= K */
4218c2ecf20Sopenharmony_ci				emit_alu_K(OR, K);
4228c2ecf20Sopenharmony_ci				break;
4238c2ecf20Sopenharmony_ci			case BPF_ANC | SKF_AD_ALU_XOR_X: /* A ^= X; */
4248c2ecf20Sopenharmony_ci			case BPF_ALU | BPF_XOR | BPF_X:
4258c2ecf20Sopenharmony_ci				emit_alu_X(XOR);
4268c2ecf20Sopenharmony_ci				break;
4278c2ecf20Sopenharmony_ci			case BPF_ALU | BPF_XOR | BPF_K:	/* A ^= K */
4288c2ecf20Sopenharmony_ci				emit_alu_K(XOR, K);
4298c2ecf20Sopenharmony_ci				break;
4308c2ecf20Sopenharmony_ci			case BPF_ALU | BPF_LSH | BPF_X:	/* A <<= X */
4318c2ecf20Sopenharmony_ci				emit_alu_X(SLL);
4328c2ecf20Sopenharmony_ci				break;
4338c2ecf20Sopenharmony_ci			case BPF_ALU | BPF_LSH | BPF_K:	/* A <<= K */
4348c2ecf20Sopenharmony_ci				emit_alu_K(SLL, K);
4358c2ecf20Sopenharmony_ci				break;
4368c2ecf20Sopenharmony_ci			case BPF_ALU | BPF_RSH | BPF_X:	/* A >>= X */
4378c2ecf20Sopenharmony_ci				emit_alu_X(SRL);
4388c2ecf20Sopenharmony_ci				break;
4398c2ecf20Sopenharmony_ci			case BPF_ALU | BPF_RSH | BPF_K:	/* A >>= K */
4408c2ecf20Sopenharmony_ci				emit_alu_K(SRL, K);
4418c2ecf20Sopenharmony_ci				break;
4428c2ecf20Sopenharmony_ci			case BPF_ALU | BPF_MUL | BPF_X:	/* A *= X; */
4438c2ecf20Sopenharmony_ci				emit_alu_X(MUL);
4448c2ecf20Sopenharmony_ci				break;
4458c2ecf20Sopenharmony_ci			case BPF_ALU | BPF_MUL | BPF_K:	/* A *= K */
4468c2ecf20Sopenharmony_ci				emit_alu_K(MUL, K);
4478c2ecf20Sopenharmony_ci				break;
4488c2ecf20Sopenharmony_ci			case BPF_ALU | BPF_DIV | BPF_K:	/* A /= K with K != 0*/
4498c2ecf20Sopenharmony_ci				if (K == 1)
4508c2ecf20Sopenharmony_ci					break;
4518c2ecf20Sopenharmony_ci				emit_write_y(G0);
4528c2ecf20Sopenharmony_ci				/* The Sparc v8 architecture requires
4538c2ecf20Sopenharmony_ci				 * three instructions between a %y
4548c2ecf20Sopenharmony_ci				 * register write and the first use.
4558c2ecf20Sopenharmony_ci				 */
4568c2ecf20Sopenharmony_ci				emit_nop();
4578c2ecf20Sopenharmony_ci				emit_nop();
4588c2ecf20Sopenharmony_ci				emit_nop();
4598c2ecf20Sopenharmony_ci				emit_alu_K(DIV, K);
4608c2ecf20Sopenharmony_ci				break;
4618c2ecf20Sopenharmony_ci			case BPF_ALU | BPF_DIV | BPF_X:	/* A /= X; */
4628c2ecf20Sopenharmony_ci				emit_cmpi(r_X, 0);
4638c2ecf20Sopenharmony_ci				if (pc_ret0 > 0) {
4648c2ecf20Sopenharmony_ci					t_offset = addrs[pc_ret0 - 1];
4658c2ecf20Sopenharmony_ci					emit_branch(BE, t_offset + 20);
4668c2ecf20Sopenharmony_ci					emit_nop(); /* delay slot */
4678c2ecf20Sopenharmony_ci				} else {
4688c2ecf20Sopenharmony_ci					emit_branch_off(BNE, 16);
4698c2ecf20Sopenharmony_ci					emit_nop();
4708c2ecf20Sopenharmony_ci					emit_jump(cleanup_addr + 20);
4718c2ecf20Sopenharmony_ci					emit_clear(r_A);
4728c2ecf20Sopenharmony_ci				}
4738c2ecf20Sopenharmony_ci				emit_write_y(G0);
4748c2ecf20Sopenharmony_ci				/* The Sparc v8 architecture requires
4758c2ecf20Sopenharmony_ci				 * three instructions between a %y
4768c2ecf20Sopenharmony_ci				 * register write and the first use.
4778c2ecf20Sopenharmony_ci				 */
4788c2ecf20Sopenharmony_ci				emit_nop();
4798c2ecf20Sopenharmony_ci				emit_nop();
4808c2ecf20Sopenharmony_ci				emit_nop();
4818c2ecf20Sopenharmony_ci				emit_alu_X(DIV);
4828c2ecf20Sopenharmony_ci				break;
4838c2ecf20Sopenharmony_ci			case BPF_ALU | BPF_NEG:
4848c2ecf20Sopenharmony_ci				emit_neg();
4858c2ecf20Sopenharmony_ci				break;
4868c2ecf20Sopenharmony_ci			case BPF_RET | BPF_K:
4878c2ecf20Sopenharmony_ci				if (!K) {
4888c2ecf20Sopenharmony_ci					if (pc_ret0 == -1)
4898c2ecf20Sopenharmony_ci						pc_ret0 = i;
4908c2ecf20Sopenharmony_ci					emit_clear(r_A);
4918c2ecf20Sopenharmony_ci				} else {
4928c2ecf20Sopenharmony_ci					emit_loadimm(K, r_A);
4938c2ecf20Sopenharmony_ci				}
4948c2ecf20Sopenharmony_ci				fallthrough;
4958c2ecf20Sopenharmony_ci			case BPF_RET | BPF_A:
4968c2ecf20Sopenharmony_ci				if (seen_or_pass0) {
4978c2ecf20Sopenharmony_ci					if (i != flen - 1) {
4988c2ecf20Sopenharmony_ci						emit_jump(cleanup_addr);
4998c2ecf20Sopenharmony_ci						emit_nop();
5008c2ecf20Sopenharmony_ci						break;
5018c2ecf20Sopenharmony_ci					}
5028c2ecf20Sopenharmony_ci					if (seen_or_pass0 & SEEN_MEM) {
5038c2ecf20Sopenharmony_ci						unsigned int sz = BASE_STACKFRAME;
5048c2ecf20Sopenharmony_ci						sz += BPF_MEMWORDS * sizeof(u32);
5058c2ecf20Sopenharmony_ci						emit_release_stack(sz);
5068c2ecf20Sopenharmony_ci					}
5078c2ecf20Sopenharmony_ci				}
5088c2ecf20Sopenharmony_ci				/* jmpl %r_saved_O7 + 8, %g0 */
5098c2ecf20Sopenharmony_ci				emit_jmpl(r_saved_O7, 8, G0);
5108c2ecf20Sopenharmony_ci				emit_reg_move(r_A, O0); /* delay slot */
5118c2ecf20Sopenharmony_ci				break;
5128c2ecf20Sopenharmony_ci			case BPF_MISC | BPF_TAX:
5138c2ecf20Sopenharmony_ci				seen |= SEEN_XREG;
5148c2ecf20Sopenharmony_ci				emit_reg_move(r_A, r_X);
5158c2ecf20Sopenharmony_ci				break;
5168c2ecf20Sopenharmony_ci			case BPF_MISC | BPF_TXA:
5178c2ecf20Sopenharmony_ci				seen |= SEEN_XREG;
5188c2ecf20Sopenharmony_ci				emit_reg_move(r_X, r_A);
5198c2ecf20Sopenharmony_ci				break;
5208c2ecf20Sopenharmony_ci			case BPF_ANC | SKF_AD_CPU:
5218c2ecf20Sopenharmony_ci				emit_load_cpu(r_A);
5228c2ecf20Sopenharmony_ci				break;
5238c2ecf20Sopenharmony_ci			case BPF_ANC | SKF_AD_PROTOCOL:
5248c2ecf20Sopenharmony_ci				emit_skb_load16(protocol, r_A);
5258c2ecf20Sopenharmony_ci				break;
5268c2ecf20Sopenharmony_ci			case BPF_ANC | SKF_AD_PKTTYPE:
5278c2ecf20Sopenharmony_ci				__emit_skb_load8(__pkt_type_offset, r_A);
5288c2ecf20Sopenharmony_ci				emit_andi(r_A, PKT_TYPE_MAX, r_A);
5298c2ecf20Sopenharmony_ci				emit_alu_K(SRL, 5);
5308c2ecf20Sopenharmony_ci				break;
5318c2ecf20Sopenharmony_ci			case BPF_ANC | SKF_AD_IFINDEX:
5328c2ecf20Sopenharmony_ci				emit_skb_loadptr(dev, r_A);
5338c2ecf20Sopenharmony_ci				emit_cmpi(r_A, 0);
5348c2ecf20Sopenharmony_ci				emit_branch(BE_PTR, cleanup_addr + 4);
5358c2ecf20Sopenharmony_ci				emit_nop();
5368c2ecf20Sopenharmony_ci				emit_load32(r_A, struct net_device, ifindex, r_A);
5378c2ecf20Sopenharmony_ci				break;
5388c2ecf20Sopenharmony_ci			case BPF_ANC | SKF_AD_MARK:
5398c2ecf20Sopenharmony_ci				emit_skb_load32(mark, r_A);
5408c2ecf20Sopenharmony_ci				break;
5418c2ecf20Sopenharmony_ci			case BPF_ANC | SKF_AD_QUEUE:
5428c2ecf20Sopenharmony_ci				emit_skb_load16(queue_mapping, r_A);
5438c2ecf20Sopenharmony_ci				break;
5448c2ecf20Sopenharmony_ci			case BPF_ANC | SKF_AD_HATYPE:
5458c2ecf20Sopenharmony_ci				emit_skb_loadptr(dev, r_A);
5468c2ecf20Sopenharmony_ci				emit_cmpi(r_A, 0);
5478c2ecf20Sopenharmony_ci				emit_branch(BE_PTR, cleanup_addr + 4);
5488c2ecf20Sopenharmony_ci				emit_nop();
5498c2ecf20Sopenharmony_ci				emit_load16(r_A, struct net_device, type, r_A);
5508c2ecf20Sopenharmony_ci				break;
5518c2ecf20Sopenharmony_ci			case BPF_ANC | SKF_AD_RXHASH:
5528c2ecf20Sopenharmony_ci				emit_skb_load32(hash, r_A);
5538c2ecf20Sopenharmony_ci				break;
5548c2ecf20Sopenharmony_ci			case BPF_ANC | SKF_AD_VLAN_TAG:
5558c2ecf20Sopenharmony_ci				emit_skb_load16(vlan_tci, r_A);
5568c2ecf20Sopenharmony_ci				break;
5578c2ecf20Sopenharmony_ci			case BPF_ANC | SKF_AD_VLAN_TAG_PRESENT:
5588c2ecf20Sopenharmony_ci				__emit_skb_load8(__pkt_vlan_present_offset, r_A);
5598c2ecf20Sopenharmony_ci				if (PKT_VLAN_PRESENT_BIT)
5608c2ecf20Sopenharmony_ci					emit_alu_K(SRL, PKT_VLAN_PRESENT_BIT);
5618c2ecf20Sopenharmony_ci				if (PKT_VLAN_PRESENT_BIT < 7)
5628c2ecf20Sopenharmony_ci					emit_andi(r_A, 1, r_A);
5638c2ecf20Sopenharmony_ci				break;
5648c2ecf20Sopenharmony_ci			case BPF_LD | BPF_W | BPF_LEN:
5658c2ecf20Sopenharmony_ci				emit_skb_load32(len, r_A);
5668c2ecf20Sopenharmony_ci				break;
5678c2ecf20Sopenharmony_ci			case BPF_LDX | BPF_W | BPF_LEN:
5688c2ecf20Sopenharmony_ci				emit_skb_load32(len, r_X);
5698c2ecf20Sopenharmony_ci				break;
5708c2ecf20Sopenharmony_ci			case BPF_LD | BPF_IMM:
5718c2ecf20Sopenharmony_ci				emit_loadimm(K, r_A);
5728c2ecf20Sopenharmony_ci				break;
5738c2ecf20Sopenharmony_ci			case BPF_LDX | BPF_IMM:
5748c2ecf20Sopenharmony_ci				emit_loadimm(K, r_X);
5758c2ecf20Sopenharmony_ci				break;
5768c2ecf20Sopenharmony_ci			case BPF_LD | BPF_MEM:
5778c2ecf20Sopenharmony_ci				seen |= SEEN_MEM;
5788c2ecf20Sopenharmony_ci				emit_ldmem(K * 4, r_A);
5798c2ecf20Sopenharmony_ci				break;
5808c2ecf20Sopenharmony_ci			case BPF_LDX | BPF_MEM:
5818c2ecf20Sopenharmony_ci				seen |= SEEN_MEM | SEEN_XREG;
5828c2ecf20Sopenharmony_ci				emit_ldmem(K * 4, r_X);
5838c2ecf20Sopenharmony_ci				break;
5848c2ecf20Sopenharmony_ci			case BPF_ST:
5858c2ecf20Sopenharmony_ci				seen |= SEEN_MEM;
5868c2ecf20Sopenharmony_ci				emit_stmem(K * 4, r_A);
5878c2ecf20Sopenharmony_ci				break;
5888c2ecf20Sopenharmony_ci			case BPF_STX:
5898c2ecf20Sopenharmony_ci				seen |= SEEN_MEM | SEEN_XREG;
5908c2ecf20Sopenharmony_ci				emit_stmem(K * 4, r_X);
5918c2ecf20Sopenharmony_ci				break;
5928c2ecf20Sopenharmony_ci
5938c2ecf20Sopenharmony_ci#define CHOOSE_LOAD_FUNC(K, func) \
5948c2ecf20Sopenharmony_ci	((int)K < 0 ? ((int)K >= SKF_LL_OFF ? func##_negative_offset : func) : func##_positive_offset)
5958c2ecf20Sopenharmony_ci
5968c2ecf20Sopenharmony_ci			case BPF_LD | BPF_W | BPF_ABS:
5978c2ecf20Sopenharmony_ci				func = CHOOSE_LOAD_FUNC(K, bpf_jit_load_word);
5988c2ecf20Sopenharmony_cicommon_load:			seen |= SEEN_DATAREF;
5998c2ecf20Sopenharmony_ci				emit_loadimm(K, r_OFF);
6008c2ecf20Sopenharmony_ci				emit_call(func);
6018c2ecf20Sopenharmony_ci				break;
6028c2ecf20Sopenharmony_ci			case BPF_LD | BPF_H | BPF_ABS:
6038c2ecf20Sopenharmony_ci				func = CHOOSE_LOAD_FUNC(K, bpf_jit_load_half);
6048c2ecf20Sopenharmony_ci				goto common_load;
6058c2ecf20Sopenharmony_ci			case BPF_LD | BPF_B | BPF_ABS:
6068c2ecf20Sopenharmony_ci				func = CHOOSE_LOAD_FUNC(K, bpf_jit_load_byte);
6078c2ecf20Sopenharmony_ci				goto common_load;
6088c2ecf20Sopenharmony_ci			case BPF_LDX | BPF_B | BPF_MSH:
6098c2ecf20Sopenharmony_ci				func = CHOOSE_LOAD_FUNC(K, bpf_jit_load_byte_msh);
6108c2ecf20Sopenharmony_ci				goto common_load;
6118c2ecf20Sopenharmony_ci			case BPF_LD | BPF_W | BPF_IND:
6128c2ecf20Sopenharmony_ci				func = bpf_jit_load_word;
6138c2ecf20Sopenharmony_cicommon_load_ind:		seen |= SEEN_DATAREF | SEEN_XREG;
6148c2ecf20Sopenharmony_ci				if (K) {
6158c2ecf20Sopenharmony_ci					if (is_simm13(K)) {
6168c2ecf20Sopenharmony_ci						emit_addi(r_X, K, r_OFF);
6178c2ecf20Sopenharmony_ci					} else {
6188c2ecf20Sopenharmony_ci						emit_loadimm(K, r_TMP);
6198c2ecf20Sopenharmony_ci						emit_add(r_X, r_TMP, r_OFF);
6208c2ecf20Sopenharmony_ci					}
6218c2ecf20Sopenharmony_ci				} else {
6228c2ecf20Sopenharmony_ci					emit_reg_move(r_X, r_OFF);
6238c2ecf20Sopenharmony_ci				}
6248c2ecf20Sopenharmony_ci				emit_call(func);
6258c2ecf20Sopenharmony_ci				break;
6268c2ecf20Sopenharmony_ci			case BPF_LD | BPF_H | BPF_IND:
6278c2ecf20Sopenharmony_ci				func = bpf_jit_load_half;
6288c2ecf20Sopenharmony_ci				goto common_load_ind;
6298c2ecf20Sopenharmony_ci			case BPF_LD | BPF_B | BPF_IND:
6308c2ecf20Sopenharmony_ci				func = bpf_jit_load_byte;
6318c2ecf20Sopenharmony_ci				goto common_load_ind;
6328c2ecf20Sopenharmony_ci			case BPF_JMP | BPF_JA:
6338c2ecf20Sopenharmony_ci				emit_jump(addrs[i + K]);
6348c2ecf20Sopenharmony_ci				emit_nop();
6358c2ecf20Sopenharmony_ci				break;
6368c2ecf20Sopenharmony_ci
6378c2ecf20Sopenharmony_ci#define COND_SEL(CODE, TOP, FOP)	\
6388c2ecf20Sopenharmony_ci	case CODE:			\
6398c2ecf20Sopenharmony_ci		t_op = TOP;		\
6408c2ecf20Sopenharmony_ci		f_op = FOP;		\
6418c2ecf20Sopenharmony_ci		goto cond_branch
6428c2ecf20Sopenharmony_ci
6438c2ecf20Sopenharmony_ci			COND_SEL(BPF_JMP | BPF_JGT | BPF_K, BGU, BLEU);
6448c2ecf20Sopenharmony_ci			COND_SEL(BPF_JMP | BPF_JGE | BPF_K, BGEU, BLU);
6458c2ecf20Sopenharmony_ci			COND_SEL(BPF_JMP | BPF_JEQ | BPF_K, BE, BNE);
6468c2ecf20Sopenharmony_ci			COND_SEL(BPF_JMP | BPF_JSET | BPF_K, BNE, BE);
6478c2ecf20Sopenharmony_ci			COND_SEL(BPF_JMP | BPF_JGT | BPF_X, BGU, BLEU);
6488c2ecf20Sopenharmony_ci			COND_SEL(BPF_JMP | BPF_JGE | BPF_X, BGEU, BLU);
6498c2ecf20Sopenharmony_ci			COND_SEL(BPF_JMP | BPF_JEQ | BPF_X, BE, BNE);
6508c2ecf20Sopenharmony_ci			COND_SEL(BPF_JMP | BPF_JSET | BPF_X, BNE, BE);
6518c2ecf20Sopenharmony_ci
6528c2ecf20Sopenharmony_cicond_branch:			f_offset = addrs[i + filter[i].jf];
6538c2ecf20Sopenharmony_ci				t_offset = addrs[i + filter[i].jt];
6548c2ecf20Sopenharmony_ci
6558c2ecf20Sopenharmony_ci				/* same targets, can avoid doing the test :) */
6568c2ecf20Sopenharmony_ci				if (filter[i].jt == filter[i].jf) {
6578c2ecf20Sopenharmony_ci					emit_jump(t_offset);
6588c2ecf20Sopenharmony_ci					emit_nop();
6598c2ecf20Sopenharmony_ci					break;
6608c2ecf20Sopenharmony_ci				}
6618c2ecf20Sopenharmony_ci
6628c2ecf20Sopenharmony_ci				switch (code) {
6638c2ecf20Sopenharmony_ci				case BPF_JMP | BPF_JGT | BPF_X:
6648c2ecf20Sopenharmony_ci				case BPF_JMP | BPF_JGE | BPF_X:
6658c2ecf20Sopenharmony_ci				case BPF_JMP | BPF_JEQ | BPF_X:
6668c2ecf20Sopenharmony_ci					seen |= SEEN_XREG;
6678c2ecf20Sopenharmony_ci					emit_cmp(r_A, r_X);
6688c2ecf20Sopenharmony_ci					break;
6698c2ecf20Sopenharmony_ci				case BPF_JMP | BPF_JSET | BPF_X:
6708c2ecf20Sopenharmony_ci					seen |= SEEN_XREG;
6718c2ecf20Sopenharmony_ci					emit_btst(r_A, r_X);
6728c2ecf20Sopenharmony_ci					break;
6738c2ecf20Sopenharmony_ci				case BPF_JMP | BPF_JEQ | BPF_K:
6748c2ecf20Sopenharmony_ci				case BPF_JMP | BPF_JGT | BPF_K:
6758c2ecf20Sopenharmony_ci				case BPF_JMP | BPF_JGE | BPF_K:
6768c2ecf20Sopenharmony_ci					if (is_simm13(K)) {
6778c2ecf20Sopenharmony_ci						emit_cmpi(r_A, K);
6788c2ecf20Sopenharmony_ci					} else {
6798c2ecf20Sopenharmony_ci						emit_loadimm(K, r_TMP);
6808c2ecf20Sopenharmony_ci						emit_cmp(r_A, r_TMP);
6818c2ecf20Sopenharmony_ci					}
6828c2ecf20Sopenharmony_ci					break;
6838c2ecf20Sopenharmony_ci				case BPF_JMP | BPF_JSET | BPF_K:
6848c2ecf20Sopenharmony_ci					if (is_simm13(K)) {
6858c2ecf20Sopenharmony_ci						emit_btsti(r_A, K);
6868c2ecf20Sopenharmony_ci					} else {
6878c2ecf20Sopenharmony_ci						emit_loadimm(K, r_TMP);
6888c2ecf20Sopenharmony_ci						emit_btst(r_A, r_TMP);
6898c2ecf20Sopenharmony_ci					}
6908c2ecf20Sopenharmony_ci					break;
6918c2ecf20Sopenharmony_ci				}
6928c2ecf20Sopenharmony_ci				if (filter[i].jt != 0) {
6938c2ecf20Sopenharmony_ci					if (filter[i].jf)
6948c2ecf20Sopenharmony_ci						t_offset += 8;
6958c2ecf20Sopenharmony_ci					emit_branch(t_op, t_offset);
6968c2ecf20Sopenharmony_ci					emit_nop(); /* delay slot */
6978c2ecf20Sopenharmony_ci					if (filter[i].jf) {
6988c2ecf20Sopenharmony_ci						emit_jump(f_offset);
6998c2ecf20Sopenharmony_ci						emit_nop();
7008c2ecf20Sopenharmony_ci					}
7018c2ecf20Sopenharmony_ci					break;
7028c2ecf20Sopenharmony_ci				}
7038c2ecf20Sopenharmony_ci				emit_branch(f_op, f_offset);
7048c2ecf20Sopenharmony_ci				emit_nop(); /* delay slot */
7058c2ecf20Sopenharmony_ci				break;
7068c2ecf20Sopenharmony_ci
7078c2ecf20Sopenharmony_ci			default:
7088c2ecf20Sopenharmony_ci				/* hmm, too complex filter, give up with jit compiler */
7098c2ecf20Sopenharmony_ci				goto out;
7108c2ecf20Sopenharmony_ci			}
7118c2ecf20Sopenharmony_ci			ilen = (void *) prog - (void *) temp;
7128c2ecf20Sopenharmony_ci			if (image) {
7138c2ecf20Sopenharmony_ci				if (unlikely(proglen + ilen > oldproglen)) {
7148c2ecf20Sopenharmony_ci					pr_err("bpb_jit_compile fatal error\n");
7158c2ecf20Sopenharmony_ci					kfree(addrs);
7168c2ecf20Sopenharmony_ci					module_memfree(image);
7178c2ecf20Sopenharmony_ci					return;
7188c2ecf20Sopenharmony_ci				}
7198c2ecf20Sopenharmony_ci				memcpy(image + proglen, temp, ilen);
7208c2ecf20Sopenharmony_ci			}
7218c2ecf20Sopenharmony_ci			proglen += ilen;
7228c2ecf20Sopenharmony_ci			addrs[i] = proglen;
7238c2ecf20Sopenharmony_ci			prog = temp;
7248c2ecf20Sopenharmony_ci		}
7258c2ecf20Sopenharmony_ci		/* last bpf instruction is always a RET :
7268c2ecf20Sopenharmony_ci		 * use it to give the cleanup instruction(s) addr
7278c2ecf20Sopenharmony_ci		 */
7288c2ecf20Sopenharmony_ci		cleanup_addr = proglen - 8; /* jmpl; mov r_A,%o0; */
7298c2ecf20Sopenharmony_ci		if (seen_or_pass0 & SEEN_MEM)
7308c2ecf20Sopenharmony_ci			cleanup_addr -= 4; /* add %sp, X, %sp; */
7318c2ecf20Sopenharmony_ci
7328c2ecf20Sopenharmony_ci		if (image) {
7338c2ecf20Sopenharmony_ci			if (proglen != oldproglen)
7348c2ecf20Sopenharmony_ci				pr_err("bpb_jit_compile proglen=%u != oldproglen=%u\n",
7358c2ecf20Sopenharmony_ci				       proglen, oldproglen);
7368c2ecf20Sopenharmony_ci			break;
7378c2ecf20Sopenharmony_ci		}
7388c2ecf20Sopenharmony_ci		if (proglen == oldproglen) {
7398c2ecf20Sopenharmony_ci			image = module_alloc(proglen);
7408c2ecf20Sopenharmony_ci			if (!image)
7418c2ecf20Sopenharmony_ci				goto out;
7428c2ecf20Sopenharmony_ci		}
7438c2ecf20Sopenharmony_ci		oldproglen = proglen;
7448c2ecf20Sopenharmony_ci	}
7458c2ecf20Sopenharmony_ci
7468c2ecf20Sopenharmony_ci	if (bpf_jit_enable > 1)
7478c2ecf20Sopenharmony_ci		bpf_jit_dump(flen, proglen, pass + 1, image);
7488c2ecf20Sopenharmony_ci
7498c2ecf20Sopenharmony_ci	if (image) {
7508c2ecf20Sopenharmony_ci		fp->bpf_func = (void *)image;
7518c2ecf20Sopenharmony_ci		fp->jited = 1;
7528c2ecf20Sopenharmony_ci	}
7538c2ecf20Sopenharmony_ciout:
7548c2ecf20Sopenharmony_ci	kfree(addrs);
7558c2ecf20Sopenharmony_ci	return;
7568c2ecf20Sopenharmony_ci}
7578c2ecf20Sopenharmony_ci
7588c2ecf20Sopenharmony_civoid bpf_jit_free(struct bpf_prog *fp)
7598c2ecf20Sopenharmony_ci{
7608c2ecf20Sopenharmony_ci	if (fp->jited)
7618c2ecf20Sopenharmony_ci		module_memfree(fp->bpf_func);
7628c2ecf20Sopenharmony_ci
7638c2ecf20Sopenharmony_ci	bpf_prog_unlock_free(fp);
7648c2ecf20Sopenharmony_ci}
765