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/bpf.h>
78c2ecf20Sopenharmony_ci#include <linux/cache.h>
88c2ecf20Sopenharmony_ci#include <linux/if_vlan.h>
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <asm/cacheflush.h>
118c2ecf20Sopenharmony_ci#include <asm/ptrace.h>
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include "bpf_jit_64.h"
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_cistatic inline bool is_simm13(unsigned int value)
168c2ecf20Sopenharmony_ci{
178c2ecf20Sopenharmony_ci	return value + 0x1000 < 0x2000;
188c2ecf20Sopenharmony_ci}
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_cistatic inline bool is_simm10(unsigned int value)
218c2ecf20Sopenharmony_ci{
228c2ecf20Sopenharmony_ci	return value + 0x200 < 0x400;
238c2ecf20Sopenharmony_ci}
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_cistatic inline bool is_simm5(unsigned int value)
268c2ecf20Sopenharmony_ci{
278c2ecf20Sopenharmony_ci	return value + 0x10 < 0x20;
288c2ecf20Sopenharmony_ci}
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_cistatic inline bool is_sethi(unsigned int value)
318c2ecf20Sopenharmony_ci{
328c2ecf20Sopenharmony_ci	return (value & ~0x3fffff) == 0;
338c2ecf20Sopenharmony_ci}
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_cistatic void bpf_flush_icache(void *start_, void *end_)
368c2ecf20Sopenharmony_ci{
378c2ecf20Sopenharmony_ci	/* Cheetah's I-cache is fully coherent.  */
388c2ecf20Sopenharmony_ci	if (tlb_type == spitfire) {
398c2ecf20Sopenharmony_ci		unsigned long start = (unsigned long) start_;
408c2ecf20Sopenharmony_ci		unsigned long end = (unsigned long) end_;
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci		start &= ~7UL;
438c2ecf20Sopenharmony_ci		end = (end + 7UL) & ~7UL;
448c2ecf20Sopenharmony_ci		while (start < end) {
458c2ecf20Sopenharmony_ci			flushi(start);
468c2ecf20Sopenharmony_ci			start += 32;
478c2ecf20Sopenharmony_ci		}
488c2ecf20Sopenharmony_ci	}
498c2ecf20Sopenharmony_ci}
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci#define S13(X)		((X) & 0x1fff)
528c2ecf20Sopenharmony_ci#define S5(X)		((X) & 0x1f)
538c2ecf20Sopenharmony_ci#define IMMED		0x00002000
548c2ecf20Sopenharmony_ci#define RD(X)		((X) << 25)
558c2ecf20Sopenharmony_ci#define RS1(X)		((X) << 14)
568c2ecf20Sopenharmony_ci#define RS2(X)		((X))
578c2ecf20Sopenharmony_ci#define OP(X)		((X) << 30)
588c2ecf20Sopenharmony_ci#define OP2(X)		((X) << 22)
598c2ecf20Sopenharmony_ci#define OP3(X)		((X) << 19)
608c2ecf20Sopenharmony_ci#define COND(X)		(((X) & 0xf) << 25)
618c2ecf20Sopenharmony_ci#define CBCOND(X)	(((X) & 0x1f) << 25)
628c2ecf20Sopenharmony_ci#define F1(X)		OP(X)
638c2ecf20Sopenharmony_ci#define F2(X, Y)	(OP(X) | OP2(Y))
648c2ecf20Sopenharmony_ci#define F3(X, Y)	(OP(X) | OP3(Y))
658c2ecf20Sopenharmony_ci#define ASI(X)		(((X) & 0xff) << 5)
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci#define CONDN		COND(0x0)
688c2ecf20Sopenharmony_ci#define CONDE		COND(0x1)
698c2ecf20Sopenharmony_ci#define CONDLE		COND(0x2)
708c2ecf20Sopenharmony_ci#define CONDL		COND(0x3)
718c2ecf20Sopenharmony_ci#define CONDLEU		COND(0x4)
728c2ecf20Sopenharmony_ci#define CONDCS		COND(0x5)
738c2ecf20Sopenharmony_ci#define CONDNEG		COND(0x6)
748c2ecf20Sopenharmony_ci#define CONDVC		COND(0x7)
758c2ecf20Sopenharmony_ci#define CONDA		COND(0x8)
768c2ecf20Sopenharmony_ci#define CONDNE		COND(0x9)
778c2ecf20Sopenharmony_ci#define CONDG		COND(0xa)
788c2ecf20Sopenharmony_ci#define CONDGE		COND(0xb)
798c2ecf20Sopenharmony_ci#define CONDGU		COND(0xc)
808c2ecf20Sopenharmony_ci#define CONDCC		COND(0xd)
818c2ecf20Sopenharmony_ci#define CONDPOS		COND(0xe)
828c2ecf20Sopenharmony_ci#define CONDVS		COND(0xf)
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci#define CONDGEU		CONDCC
858c2ecf20Sopenharmony_ci#define CONDLU		CONDCS
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci#define WDISP22(X)	(((X) >> 2) & 0x3fffff)
888c2ecf20Sopenharmony_ci#define WDISP19(X)	(((X) >> 2) & 0x7ffff)
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci/* The 10-bit branch displacement for CBCOND is split into two fields */
918c2ecf20Sopenharmony_cistatic u32 WDISP10(u32 off)
928c2ecf20Sopenharmony_ci{
938c2ecf20Sopenharmony_ci	u32 ret = ((off >> 2) & 0xff) << 5;
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	ret |= ((off >> (2 + 8)) & 0x03) << 19;
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	return ret;
988c2ecf20Sopenharmony_ci}
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci#define CBCONDE		CBCOND(0x09)
1018c2ecf20Sopenharmony_ci#define CBCONDLE	CBCOND(0x0a)
1028c2ecf20Sopenharmony_ci#define CBCONDL		CBCOND(0x0b)
1038c2ecf20Sopenharmony_ci#define CBCONDLEU	CBCOND(0x0c)
1048c2ecf20Sopenharmony_ci#define CBCONDCS	CBCOND(0x0d)
1058c2ecf20Sopenharmony_ci#define CBCONDN		CBCOND(0x0e)
1068c2ecf20Sopenharmony_ci#define CBCONDVS	CBCOND(0x0f)
1078c2ecf20Sopenharmony_ci#define CBCONDNE	CBCOND(0x19)
1088c2ecf20Sopenharmony_ci#define CBCONDG		CBCOND(0x1a)
1098c2ecf20Sopenharmony_ci#define CBCONDGE	CBCOND(0x1b)
1108c2ecf20Sopenharmony_ci#define CBCONDGU	CBCOND(0x1c)
1118c2ecf20Sopenharmony_ci#define CBCONDCC	CBCOND(0x1d)
1128c2ecf20Sopenharmony_ci#define CBCONDPOS	CBCOND(0x1e)
1138c2ecf20Sopenharmony_ci#define CBCONDVC	CBCOND(0x1f)
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci#define CBCONDGEU	CBCONDCC
1168c2ecf20Sopenharmony_ci#define CBCONDLU	CBCONDCS
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci#define ANNUL		(1 << 29)
1198c2ecf20Sopenharmony_ci#define XCC		(1 << 21)
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci#define BRANCH		(F2(0, 1) | XCC)
1228c2ecf20Sopenharmony_ci#define CBCOND_OP	(F2(0, 3) | XCC)
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci#define BA		(BRANCH | CONDA)
1258c2ecf20Sopenharmony_ci#define BG		(BRANCH | CONDG)
1268c2ecf20Sopenharmony_ci#define BL		(BRANCH | CONDL)
1278c2ecf20Sopenharmony_ci#define BLE		(BRANCH | CONDLE)
1288c2ecf20Sopenharmony_ci#define BGU		(BRANCH | CONDGU)
1298c2ecf20Sopenharmony_ci#define BLEU		(BRANCH | CONDLEU)
1308c2ecf20Sopenharmony_ci#define BGE		(BRANCH | CONDGE)
1318c2ecf20Sopenharmony_ci#define BGEU		(BRANCH | CONDGEU)
1328c2ecf20Sopenharmony_ci#define BLU		(BRANCH | CONDLU)
1338c2ecf20Sopenharmony_ci#define BE		(BRANCH | CONDE)
1348c2ecf20Sopenharmony_ci#define BNE		(BRANCH | CONDNE)
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci#define SETHI(K, REG)	\
1378c2ecf20Sopenharmony_ci	(F2(0, 0x4) | RD(REG) | (((K) >> 10) & 0x3fffff))
1388c2ecf20Sopenharmony_ci#define OR_LO(K, REG)	\
1398c2ecf20Sopenharmony_ci	(F3(2, 0x02) | IMMED | RS1(REG) | ((K) & 0x3ff) | RD(REG))
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci#define ADD		F3(2, 0x00)
1428c2ecf20Sopenharmony_ci#define AND		F3(2, 0x01)
1438c2ecf20Sopenharmony_ci#define ANDCC		F3(2, 0x11)
1448c2ecf20Sopenharmony_ci#define OR		F3(2, 0x02)
1458c2ecf20Sopenharmony_ci#define XOR		F3(2, 0x03)
1468c2ecf20Sopenharmony_ci#define SUB		F3(2, 0x04)
1478c2ecf20Sopenharmony_ci#define SUBCC		F3(2, 0x14)
1488c2ecf20Sopenharmony_ci#define MUL		F3(2, 0x0a)
1498c2ecf20Sopenharmony_ci#define MULX		F3(2, 0x09)
1508c2ecf20Sopenharmony_ci#define UDIVX		F3(2, 0x0d)
1518c2ecf20Sopenharmony_ci#define DIV		F3(2, 0x0e)
1528c2ecf20Sopenharmony_ci#define SLL		F3(2, 0x25)
1538c2ecf20Sopenharmony_ci#define SLLX		(F3(2, 0x25)|(1<<12))
1548c2ecf20Sopenharmony_ci#define SRA		F3(2, 0x27)
1558c2ecf20Sopenharmony_ci#define SRAX		(F3(2, 0x27)|(1<<12))
1568c2ecf20Sopenharmony_ci#define SRL		F3(2, 0x26)
1578c2ecf20Sopenharmony_ci#define SRLX		(F3(2, 0x26)|(1<<12))
1588c2ecf20Sopenharmony_ci#define JMPL		F3(2, 0x38)
1598c2ecf20Sopenharmony_ci#define SAVE		F3(2, 0x3c)
1608c2ecf20Sopenharmony_ci#define RESTORE		F3(2, 0x3d)
1618c2ecf20Sopenharmony_ci#define CALL		F1(1)
1628c2ecf20Sopenharmony_ci#define BR		F2(0, 0x01)
1638c2ecf20Sopenharmony_ci#define RD_Y		F3(2, 0x28)
1648c2ecf20Sopenharmony_ci#define WR_Y		F3(2, 0x30)
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci#define LD32		F3(3, 0x00)
1678c2ecf20Sopenharmony_ci#define LD8		F3(3, 0x01)
1688c2ecf20Sopenharmony_ci#define LD16		F3(3, 0x02)
1698c2ecf20Sopenharmony_ci#define LD64		F3(3, 0x0b)
1708c2ecf20Sopenharmony_ci#define LD64A		F3(3, 0x1b)
1718c2ecf20Sopenharmony_ci#define ST8		F3(3, 0x05)
1728c2ecf20Sopenharmony_ci#define ST16		F3(3, 0x06)
1738c2ecf20Sopenharmony_ci#define ST32		F3(3, 0x04)
1748c2ecf20Sopenharmony_ci#define ST64		F3(3, 0x0e)
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci#define CAS		F3(3, 0x3c)
1778c2ecf20Sopenharmony_ci#define CASX		F3(3, 0x3e)
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci#define LDPTR		LD64
1808c2ecf20Sopenharmony_ci#define BASE_STACKFRAME	176
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci#define LD32I		(LD32 | IMMED)
1838c2ecf20Sopenharmony_ci#define LD8I		(LD8 | IMMED)
1848c2ecf20Sopenharmony_ci#define LD16I		(LD16 | IMMED)
1858c2ecf20Sopenharmony_ci#define LD64I		(LD64 | IMMED)
1868c2ecf20Sopenharmony_ci#define LDPTRI		(LDPTR | IMMED)
1878c2ecf20Sopenharmony_ci#define ST32I		(ST32 | IMMED)
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_cistruct jit_ctx {
1908c2ecf20Sopenharmony_ci	struct bpf_prog		*prog;
1918c2ecf20Sopenharmony_ci	unsigned int		*offset;
1928c2ecf20Sopenharmony_ci	int			idx;
1938c2ecf20Sopenharmony_ci	int			epilogue_offset;
1948c2ecf20Sopenharmony_ci	bool 			tmp_1_used;
1958c2ecf20Sopenharmony_ci	bool 			tmp_2_used;
1968c2ecf20Sopenharmony_ci	bool 			tmp_3_used;
1978c2ecf20Sopenharmony_ci	bool			saw_frame_pointer;
1988c2ecf20Sopenharmony_ci	bool			saw_call;
1998c2ecf20Sopenharmony_ci	bool			saw_tail_call;
2008c2ecf20Sopenharmony_ci	u32			*image;
2018c2ecf20Sopenharmony_ci};
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci#define TMP_REG_1	(MAX_BPF_JIT_REG + 0)
2048c2ecf20Sopenharmony_ci#define TMP_REG_2	(MAX_BPF_JIT_REG + 1)
2058c2ecf20Sopenharmony_ci#define TMP_REG_3	(MAX_BPF_JIT_REG + 2)
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci/* Map BPF registers to SPARC registers */
2088c2ecf20Sopenharmony_cistatic const int bpf2sparc[] = {
2098c2ecf20Sopenharmony_ci	/* return value from in-kernel function, and exit value from eBPF */
2108c2ecf20Sopenharmony_ci	[BPF_REG_0] = O5,
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci	/* arguments from eBPF program to in-kernel function */
2138c2ecf20Sopenharmony_ci	[BPF_REG_1] = O0,
2148c2ecf20Sopenharmony_ci	[BPF_REG_2] = O1,
2158c2ecf20Sopenharmony_ci	[BPF_REG_3] = O2,
2168c2ecf20Sopenharmony_ci	[BPF_REG_4] = O3,
2178c2ecf20Sopenharmony_ci	[BPF_REG_5] = O4,
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	/* callee saved registers that in-kernel function will preserve */
2208c2ecf20Sopenharmony_ci	[BPF_REG_6] = L0,
2218c2ecf20Sopenharmony_ci	[BPF_REG_7] = L1,
2228c2ecf20Sopenharmony_ci	[BPF_REG_8] = L2,
2238c2ecf20Sopenharmony_ci	[BPF_REG_9] = L3,
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci	/* read-only frame pointer to access stack */
2268c2ecf20Sopenharmony_ci	[BPF_REG_FP] = L6,
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci	[BPF_REG_AX] = G7,
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci	/* temporary register for internal BPF JIT */
2318c2ecf20Sopenharmony_ci	[TMP_REG_1] = G1,
2328c2ecf20Sopenharmony_ci	[TMP_REG_2] = G2,
2338c2ecf20Sopenharmony_ci	[TMP_REG_3] = G3,
2348c2ecf20Sopenharmony_ci};
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_cistatic void emit(const u32 insn, struct jit_ctx *ctx)
2378c2ecf20Sopenharmony_ci{
2388c2ecf20Sopenharmony_ci	if (ctx->image != NULL)
2398c2ecf20Sopenharmony_ci		ctx->image[ctx->idx] = insn;
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci	ctx->idx++;
2428c2ecf20Sopenharmony_ci}
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_cistatic void emit_call(u32 *func, struct jit_ctx *ctx)
2458c2ecf20Sopenharmony_ci{
2468c2ecf20Sopenharmony_ci	if (ctx->image != NULL) {
2478c2ecf20Sopenharmony_ci		void *here = &ctx->image[ctx->idx];
2488c2ecf20Sopenharmony_ci		unsigned int off;
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci		off = (void *)func - here;
2518c2ecf20Sopenharmony_ci		ctx->image[ctx->idx] = CALL | ((off >> 2) & 0x3fffffff);
2528c2ecf20Sopenharmony_ci	}
2538c2ecf20Sopenharmony_ci	ctx->idx++;
2548c2ecf20Sopenharmony_ci}
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_cistatic void emit_nop(struct jit_ctx *ctx)
2578c2ecf20Sopenharmony_ci{
2588c2ecf20Sopenharmony_ci	emit(SETHI(0, G0), ctx);
2598c2ecf20Sopenharmony_ci}
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_cistatic void emit_reg_move(u32 from, u32 to, struct jit_ctx *ctx)
2628c2ecf20Sopenharmony_ci{
2638c2ecf20Sopenharmony_ci	emit(OR | RS1(G0) | RS2(from) | RD(to), ctx);
2648c2ecf20Sopenharmony_ci}
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ci/* Emit 32-bit constant, zero extended. */
2678c2ecf20Sopenharmony_cistatic void emit_set_const(s32 K, u32 reg, struct jit_ctx *ctx)
2688c2ecf20Sopenharmony_ci{
2698c2ecf20Sopenharmony_ci	emit(SETHI(K, reg), ctx);
2708c2ecf20Sopenharmony_ci	emit(OR_LO(K, reg), ctx);
2718c2ecf20Sopenharmony_ci}
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci/* Emit 32-bit constant, sign extended. */
2748c2ecf20Sopenharmony_cistatic void emit_set_const_sext(s32 K, u32 reg, struct jit_ctx *ctx)
2758c2ecf20Sopenharmony_ci{
2768c2ecf20Sopenharmony_ci	if (K >= 0) {
2778c2ecf20Sopenharmony_ci		emit(SETHI(K, reg), ctx);
2788c2ecf20Sopenharmony_ci		emit(OR_LO(K, reg), ctx);
2798c2ecf20Sopenharmony_ci	} else {
2808c2ecf20Sopenharmony_ci		u32 hbits = ~(u32) K;
2818c2ecf20Sopenharmony_ci		u32 lbits = -0x400 | (u32) K;
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ci		emit(SETHI(hbits, reg), ctx);
2848c2ecf20Sopenharmony_ci		emit(XOR | IMMED | RS1(reg) | S13(lbits) | RD(reg), ctx);
2858c2ecf20Sopenharmony_ci	}
2868c2ecf20Sopenharmony_ci}
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_cistatic void emit_alu(u32 opcode, u32 src, u32 dst, struct jit_ctx *ctx)
2898c2ecf20Sopenharmony_ci{
2908c2ecf20Sopenharmony_ci	emit(opcode | RS1(dst) | RS2(src) | RD(dst), ctx);
2918c2ecf20Sopenharmony_ci}
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_cistatic void emit_alu3(u32 opcode, u32 a, u32 b, u32 c, struct jit_ctx *ctx)
2948c2ecf20Sopenharmony_ci{
2958c2ecf20Sopenharmony_ci	emit(opcode | RS1(a) | RS2(b) | RD(c), ctx);
2968c2ecf20Sopenharmony_ci}
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_cistatic void emit_alu_K(unsigned int opcode, unsigned int dst, unsigned int imm,
2998c2ecf20Sopenharmony_ci		       struct jit_ctx *ctx)
3008c2ecf20Sopenharmony_ci{
3018c2ecf20Sopenharmony_ci	bool small_immed = is_simm13(imm);
3028c2ecf20Sopenharmony_ci	unsigned int insn = opcode;
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci	insn |= RS1(dst) | RD(dst);
3058c2ecf20Sopenharmony_ci	if (small_immed) {
3068c2ecf20Sopenharmony_ci		emit(insn | IMMED | S13(imm), ctx);
3078c2ecf20Sopenharmony_ci	} else {
3088c2ecf20Sopenharmony_ci		unsigned int tmp = bpf2sparc[TMP_REG_1];
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci		ctx->tmp_1_used = true;
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci		emit_set_const_sext(imm, tmp, ctx);
3138c2ecf20Sopenharmony_ci		emit(insn | RS2(tmp), ctx);
3148c2ecf20Sopenharmony_ci	}
3158c2ecf20Sopenharmony_ci}
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_cistatic void emit_alu3_K(unsigned int opcode, unsigned int src, unsigned int imm,
3188c2ecf20Sopenharmony_ci			unsigned int dst, struct jit_ctx *ctx)
3198c2ecf20Sopenharmony_ci{
3208c2ecf20Sopenharmony_ci	bool small_immed = is_simm13(imm);
3218c2ecf20Sopenharmony_ci	unsigned int insn = opcode;
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ci	insn |= RS1(src) | RD(dst);
3248c2ecf20Sopenharmony_ci	if (small_immed) {
3258c2ecf20Sopenharmony_ci		emit(insn | IMMED | S13(imm), ctx);
3268c2ecf20Sopenharmony_ci	} else {
3278c2ecf20Sopenharmony_ci		unsigned int tmp = bpf2sparc[TMP_REG_1];
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_ci		ctx->tmp_1_used = true;
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci		emit_set_const_sext(imm, tmp, ctx);
3328c2ecf20Sopenharmony_ci		emit(insn | RS2(tmp), ctx);
3338c2ecf20Sopenharmony_ci	}
3348c2ecf20Sopenharmony_ci}
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_cistatic void emit_loadimm32(s32 K, unsigned int dest, struct jit_ctx *ctx)
3378c2ecf20Sopenharmony_ci{
3388c2ecf20Sopenharmony_ci	if (K >= 0 && is_simm13(K)) {
3398c2ecf20Sopenharmony_ci		/* or %g0, K, DEST */
3408c2ecf20Sopenharmony_ci		emit(OR | IMMED | RS1(G0) | S13(K) | RD(dest), ctx);
3418c2ecf20Sopenharmony_ci	} else {
3428c2ecf20Sopenharmony_ci		emit_set_const(K, dest, ctx);
3438c2ecf20Sopenharmony_ci	}
3448c2ecf20Sopenharmony_ci}
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_cistatic void emit_loadimm(s32 K, unsigned int dest, struct jit_ctx *ctx)
3478c2ecf20Sopenharmony_ci{
3488c2ecf20Sopenharmony_ci	if (is_simm13(K)) {
3498c2ecf20Sopenharmony_ci		/* or %g0, K, DEST */
3508c2ecf20Sopenharmony_ci		emit(OR | IMMED | RS1(G0) | S13(K) | RD(dest), ctx);
3518c2ecf20Sopenharmony_ci	} else {
3528c2ecf20Sopenharmony_ci		emit_set_const(K, dest, ctx);
3538c2ecf20Sopenharmony_ci	}
3548c2ecf20Sopenharmony_ci}
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_cistatic void emit_loadimm_sext(s32 K, unsigned int dest, struct jit_ctx *ctx)
3578c2ecf20Sopenharmony_ci{
3588c2ecf20Sopenharmony_ci	if (is_simm13(K)) {
3598c2ecf20Sopenharmony_ci		/* or %g0, K, DEST */
3608c2ecf20Sopenharmony_ci		emit(OR | IMMED | RS1(G0) | S13(K) | RD(dest), ctx);
3618c2ecf20Sopenharmony_ci	} else {
3628c2ecf20Sopenharmony_ci		emit_set_const_sext(K, dest, ctx);
3638c2ecf20Sopenharmony_ci	}
3648c2ecf20Sopenharmony_ci}
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_cistatic void analyze_64bit_constant(u32 high_bits, u32 low_bits,
3678c2ecf20Sopenharmony_ci				   int *hbsp, int *lbsp, int *abbasp)
3688c2ecf20Sopenharmony_ci{
3698c2ecf20Sopenharmony_ci	int lowest_bit_set, highest_bit_set, all_bits_between_are_set;
3708c2ecf20Sopenharmony_ci	int i;
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci	lowest_bit_set = highest_bit_set = -1;
3738c2ecf20Sopenharmony_ci	i = 0;
3748c2ecf20Sopenharmony_ci	do {
3758c2ecf20Sopenharmony_ci		if ((lowest_bit_set == -1) && ((low_bits >> i) & 1))
3768c2ecf20Sopenharmony_ci			lowest_bit_set = i;
3778c2ecf20Sopenharmony_ci		if ((highest_bit_set == -1) && ((high_bits >> (32 - i - 1)) & 1))
3788c2ecf20Sopenharmony_ci			highest_bit_set = (64 - i - 1);
3798c2ecf20Sopenharmony_ci	}  while (++i < 32 && (highest_bit_set == -1 ||
3808c2ecf20Sopenharmony_ci			       lowest_bit_set == -1));
3818c2ecf20Sopenharmony_ci	if (i == 32) {
3828c2ecf20Sopenharmony_ci		i = 0;
3838c2ecf20Sopenharmony_ci		do {
3848c2ecf20Sopenharmony_ci			if (lowest_bit_set == -1 && ((high_bits >> i) & 1))
3858c2ecf20Sopenharmony_ci				lowest_bit_set = i + 32;
3868c2ecf20Sopenharmony_ci			if (highest_bit_set == -1 &&
3878c2ecf20Sopenharmony_ci			    ((low_bits >> (32 - i - 1)) & 1))
3888c2ecf20Sopenharmony_ci				highest_bit_set = 32 - i - 1;
3898c2ecf20Sopenharmony_ci		} while (++i < 32 && (highest_bit_set == -1 ||
3908c2ecf20Sopenharmony_ci				      lowest_bit_set == -1));
3918c2ecf20Sopenharmony_ci	}
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci	all_bits_between_are_set = 1;
3948c2ecf20Sopenharmony_ci	for (i = lowest_bit_set; i <= highest_bit_set; i++) {
3958c2ecf20Sopenharmony_ci		if (i < 32) {
3968c2ecf20Sopenharmony_ci			if ((low_bits & (1 << i)) != 0)
3978c2ecf20Sopenharmony_ci				continue;
3988c2ecf20Sopenharmony_ci		} else {
3998c2ecf20Sopenharmony_ci			if ((high_bits & (1 << (i - 32))) != 0)
4008c2ecf20Sopenharmony_ci				continue;
4018c2ecf20Sopenharmony_ci		}
4028c2ecf20Sopenharmony_ci		all_bits_between_are_set = 0;
4038c2ecf20Sopenharmony_ci		break;
4048c2ecf20Sopenharmony_ci	}
4058c2ecf20Sopenharmony_ci	*hbsp = highest_bit_set;
4068c2ecf20Sopenharmony_ci	*lbsp = lowest_bit_set;
4078c2ecf20Sopenharmony_ci	*abbasp = all_bits_between_are_set;
4088c2ecf20Sopenharmony_ci}
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_cistatic unsigned long create_simple_focus_bits(unsigned long high_bits,
4118c2ecf20Sopenharmony_ci					      unsigned long low_bits,
4128c2ecf20Sopenharmony_ci					      int lowest_bit_set, int shift)
4138c2ecf20Sopenharmony_ci{
4148c2ecf20Sopenharmony_ci	long hi, lo;
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_ci	if (lowest_bit_set < 32) {
4178c2ecf20Sopenharmony_ci		lo = (low_bits >> lowest_bit_set) << shift;
4188c2ecf20Sopenharmony_ci		hi = ((high_bits << (32 - lowest_bit_set)) << shift);
4198c2ecf20Sopenharmony_ci	} else {
4208c2ecf20Sopenharmony_ci		lo = 0;
4218c2ecf20Sopenharmony_ci		hi = ((high_bits >> (lowest_bit_set - 32)) << shift);
4228c2ecf20Sopenharmony_ci	}
4238c2ecf20Sopenharmony_ci	return hi | lo;
4248c2ecf20Sopenharmony_ci}
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_cistatic bool const64_is_2insns(unsigned long high_bits,
4278c2ecf20Sopenharmony_ci			      unsigned long low_bits)
4288c2ecf20Sopenharmony_ci{
4298c2ecf20Sopenharmony_ci	int highest_bit_set, lowest_bit_set, all_bits_between_are_set;
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_ci	if (high_bits == 0 || high_bits == 0xffffffff)
4328c2ecf20Sopenharmony_ci		return true;
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_ci	analyze_64bit_constant(high_bits, low_bits,
4358c2ecf20Sopenharmony_ci			       &highest_bit_set, &lowest_bit_set,
4368c2ecf20Sopenharmony_ci			       &all_bits_between_are_set);
4378c2ecf20Sopenharmony_ci
4388c2ecf20Sopenharmony_ci	if ((highest_bit_set == 63 || lowest_bit_set == 0) &&
4398c2ecf20Sopenharmony_ci	    all_bits_between_are_set != 0)
4408c2ecf20Sopenharmony_ci		return true;
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_ci	if (highest_bit_set - lowest_bit_set < 21)
4438c2ecf20Sopenharmony_ci		return true;
4448c2ecf20Sopenharmony_ci
4458c2ecf20Sopenharmony_ci	return false;
4468c2ecf20Sopenharmony_ci}
4478c2ecf20Sopenharmony_ci
4488c2ecf20Sopenharmony_cistatic void sparc_emit_set_const64_quick2(unsigned long high_bits,
4498c2ecf20Sopenharmony_ci					  unsigned long low_imm,
4508c2ecf20Sopenharmony_ci					  unsigned int dest,
4518c2ecf20Sopenharmony_ci					  int shift_count, struct jit_ctx *ctx)
4528c2ecf20Sopenharmony_ci{
4538c2ecf20Sopenharmony_ci	emit_loadimm32(high_bits, dest, ctx);
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_ci	/* Now shift it up into place.  */
4568c2ecf20Sopenharmony_ci	emit_alu_K(SLLX, dest, shift_count, ctx);
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_ci	/* If there is a low immediate part piece, finish up by
4598c2ecf20Sopenharmony_ci	 * putting that in as well.
4608c2ecf20Sopenharmony_ci	 */
4618c2ecf20Sopenharmony_ci	if (low_imm != 0)
4628c2ecf20Sopenharmony_ci		emit(OR | IMMED | RS1(dest) | S13(low_imm) | RD(dest), ctx);
4638c2ecf20Sopenharmony_ci}
4648c2ecf20Sopenharmony_ci
4658c2ecf20Sopenharmony_cistatic void emit_loadimm64(u64 K, unsigned int dest, struct jit_ctx *ctx)
4668c2ecf20Sopenharmony_ci{
4678c2ecf20Sopenharmony_ci	int all_bits_between_are_set, lowest_bit_set, highest_bit_set;
4688c2ecf20Sopenharmony_ci	unsigned int tmp = bpf2sparc[TMP_REG_1];
4698c2ecf20Sopenharmony_ci	u32 low_bits = (K & 0xffffffff);
4708c2ecf20Sopenharmony_ci	u32 high_bits = (K >> 32);
4718c2ecf20Sopenharmony_ci
4728c2ecf20Sopenharmony_ci	/* These two tests also take care of all of the one
4738c2ecf20Sopenharmony_ci	 * instruction cases.
4748c2ecf20Sopenharmony_ci	 */
4758c2ecf20Sopenharmony_ci	if (high_bits == 0xffffffff && (low_bits & 0x80000000))
4768c2ecf20Sopenharmony_ci		return emit_loadimm_sext(K, dest, ctx);
4778c2ecf20Sopenharmony_ci	if (high_bits == 0x00000000)
4788c2ecf20Sopenharmony_ci		return emit_loadimm32(K, dest, ctx);
4798c2ecf20Sopenharmony_ci
4808c2ecf20Sopenharmony_ci	analyze_64bit_constant(high_bits, low_bits, &highest_bit_set,
4818c2ecf20Sopenharmony_ci			       &lowest_bit_set, &all_bits_between_are_set);
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_ci	/* 1) mov	-1, %reg
4848c2ecf20Sopenharmony_ci	 *    sllx	%reg, shift, %reg
4858c2ecf20Sopenharmony_ci	 * 2) mov	-1, %reg
4868c2ecf20Sopenharmony_ci	 *    srlx	%reg, shift, %reg
4878c2ecf20Sopenharmony_ci	 * 3) mov	some_small_const, %reg
4888c2ecf20Sopenharmony_ci	 *    sllx	%reg, shift, %reg
4898c2ecf20Sopenharmony_ci	 */
4908c2ecf20Sopenharmony_ci	if (((highest_bit_set == 63 || lowest_bit_set == 0) &&
4918c2ecf20Sopenharmony_ci	     all_bits_between_are_set != 0) ||
4928c2ecf20Sopenharmony_ci	    ((highest_bit_set - lowest_bit_set) < 12)) {
4938c2ecf20Sopenharmony_ci		int shift = lowest_bit_set;
4948c2ecf20Sopenharmony_ci		long the_const = -1;
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_ci		if ((highest_bit_set != 63 && lowest_bit_set != 0) ||
4978c2ecf20Sopenharmony_ci		    all_bits_between_are_set == 0) {
4988c2ecf20Sopenharmony_ci			the_const =
4998c2ecf20Sopenharmony_ci				create_simple_focus_bits(high_bits, low_bits,
5008c2ecf20Sopenharmony_ci							 lowest_bit_set, 0);
5018c2ecf20Sopenharmony_ci		} else if (lowest_bit_set == 0)
5028c2ecf20Sopenharmony_ci			shift = -(63 - highest_bit_set);
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_ci		emit(OR | IMMED | RS1(G0) | S13(the_const) | RD(dest), ctx);
5058c2ecf20Sopenharmony_ci		if (shift > 0)
5068c2ecf20Sopenharmony_ci			emit_alu_K(SLLX, dest, shift, ctx);
5078c2ecf20Sopenharmony_ci		else if (shift < 0)
5088c2ecf20Sopenharmony_ci			emit_alu_K(SRLX, dest, -shift, ctx);
5098c2ecf20Sopenharmony_ci
5108c2ecf20Sopenharmony_ci		return;
5118c2ecf20Sopenharmony_ci	}
5128c2ecf20Sopenharmony_ci
5138c2ecf20Sopenharmony_ci	/* Now a range of 22 or less bits set somewhere.
5148c2ecf20Sopenharmony_ci	 * 1) sethi	%hi(focus_bits), %reg
5158c2ecf20Sopenharmony_ci	 *    sllx	%reg, shift, %reg
5168c2ecf20Sopenharmony_ci	 * 2) sethi	%hi(focus_bits), %reg
5178c2ecf20Sopenharmony_ci	 *    srlx	%reg, shift, %reg
5188c2ecf20Sopenharmony_ci	 */
5198c2ecf20Sopenharmony_ci	if ((highest_bit_set - lowest_bit_set) < 21) {
5208c2ecf20Sopenharmony_ci		unsigned long focus_bits =
5218c2ecf20Sopenharmony_ci			create_simple_focus_bits(high_bits, low_bits,
5228c2ecf20Sopenharmony_ci						 lowest_bit_set, 10);
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_ci		emit(SETHI(focus_bits, dest), ctx);
5258c2ecf20Sopenharmony_ci
5268c2ecf20Sopenharmony_ci		/* If lowest_bit_set == 10 then a sethi alone could
5278c2ecf20Sopenharmony_ci		 * have done it.
5288c2ecf20Sopenharmony_ci		 */
5298c2ecf20Sopenharmony_ci		if (lowest_bit_set < 10)
5308c2ecf20Sopenharmony_ci			emit_alu_K(SRLX, dest, 10 - lowest_bit_set, ctx);
5318c2ecf20Sopenharmony_ci		else if (lowest_bit_set > 10)
5328c2ecf20Sopenharmony_ci			emit_alu_K(SLLX, dest, lowest_bit_set - 10, ctx);
5338c2ecf20Sopenharmony_ci		return;
5348c2ecf20Sopenharmony_ci	}
5358c2ecf20Sopenharmony_ci
5368c2ecf20Sopenharmony_ci	/* Ok, now 3 instruction sequences.  */
5378c2ecf20Sopenharmony_ci	if (low_bits == 0) {
5388c2ecf20Sopenharmony_ci		emit_loadimm32(high_bits, dest, ctx);
5398c2ecf20Sopenharmony_ci		emit_alu_K(SLLX, dest, 32, ctx);
5408c2ecf20Sopenharmony_ci		return;
5418c2ecf20Sopenharmony_ci	}
5428c2ecf20Sopenharmony_ci
5438c2ecf20Sopenharmony_ci	/* We may be able to do something quick
5448c2ecf20Sopenharmony_ci	 * when the constant is negated, so try that.
5458c2ecf20Sopenharmony_ci	 */
5468c2ecf20Sopenharmony_ci	if (const64_is_2insns((~high_bits) & 0xffffffff,
5478c2ecf20Sopenharmony_ci			      (~low_bits) & 0xfffffc00)) {
5488c2ecf20Sopenharmony_ci		/* NOTE: The trailing bits get XOR'd so we need the
5498c2ecf20Sopenharmony_ci		 * non-negated bits, not the negated ones.
5508c2ecf20Sopenharmony_ci		 */
5518c2ecf20Sopenharmony_ci		unsigned long trailing_bits = low_bits & 0x3ff;
5528c2ecf20Sopenharmony_ci
5538c2ecf20Sopenharmony_ci		if ((((~high_bits) & 0xffffffff) == 0 &&
5548c2ecf20Sopenharmony_ci		     ((~low_bits) & 0x80000000) == 0) ||
5558c2ecf20Sopenharmony_ci		    (((~high_bits) & 0xffffffff) == 0xffffffff &&
5568c2ecf20Sopenharmony_ci		     ((~low_bits) & 0x80000000) != 0)) {
5578c2ecf20Sopenharmony_ci			unsigned long fast_int = (~low_bits & 0xffffffff);
5588c2ecf20Sopenharmony_ci
5598c2ecf20Sopenharmony_ci			if ((is_sethi(fast_int) &&
5608c2ecf20Sopenharmony_ci			     (~high_bits & 0xffffffff) == 0)) {
5618c2ecf20Sopenharmony_ci				emit(SETHI(fast_int, dest), ctx);
5628c2ecf20Sopenharmony_ci			} else if (is_simm13(fast_int)) {
5638c2ecf20Sopenharmony_ci				emit(OR | IMMED | RS1(G0) | S13(fast_int) | RD(dest), ctx);
5648c2ecf20Sopenharmony_ci			} else {
5658c2ecf20Sopenharmony_ci				emit_loadimm64(fast_int, dest, ctx);
5668c2ecf20Sopenharmony_ci			}
5678c2ecf20Sopenharmony_ci		} else {
5688c2ecf20Sopenharmony_ci			u64 n = ((~low_bits) & 0xfffffc00) |
5698c2ecf20Sopenharmony_ci				(((unsigned long)((~high_bits) & 0xffffffff))<<32);
5708c2ecf20Sopenharmony_ci			emit_loadimm64(n, dest, ctx);
5718c2ecf20Sopenharmony_ci		}
5728c2ecf20Sopenharmony_ci
5738c2ecf20Sopenharmony_ci		low_bits = -0x400 | trailing_bits;
5748c2ecf20Sopenharmony_ci
5758c2ecf20Sopenharmony_ci		emit(XOR | IMMED | RS1(dest) | S13(low_bits) | RD(dest), ctx);
5768c2ecf20Sopenharmony_ci		return;
5778c2ecf20Sopenharmony_ci	}
5788c2ecf20Sopenharmony_ci
5798c2ecf20Sopenharmony_ci	/* 1) sethi	%hi(xxx), %reg
5808c2ecf20Sopenharmony_ci	 *    or	%reg, %lo(xxx), %reg
5818c2ecf20Sopenharmony_ci	 *    sllx	%reg, yyy, %reg
5828c2ecf20Sopenharmony_ci	 */
5838c2ecf20Sopenharmony_ci	if ((highest_bit_set - lowest_bit_set) < 32) {
5848c2ecf20Sopenharmony_ci		unsigned long focus_bits =
5858c2ecf20Sopenharmony_ci			create_simple_focus_bits(high_bits, low_bits,
5868c2ecf20Sopenharmony_ci						 lowest_bit_set, 0);
5878c2ecf20Sopenharmony_ci
5888c2ecf20Sopenharmony_ci		/* So what we know is that the set bits straddle the
5898c2ecf20Sopenharmony_ci		 * middle of the 64-bit word.
5908c2ecf20Sopenharmony_ci		 */
5918c2ecf20Sopenharmony_ci		sparc_emit_set_const64_quick2(focus_bits, 0, dest,
5928c2ecf20Sopenharmony_ci					      lowest_bit_set, ctx);
5938c2ecf20Sopenharmony_ci		return;
5948c2ecf20Sopenharmony_ci	}
5958c2ecf20Sopenharmony_ci
5968c2ecf20Sopenharmony_ci	/* 1) sethi	%hi(high_bits), %reg
5978c2ecf20Sopenharmony_ci	 *    or	%reg, %lo(high_bits), %reg
5988c2ecf20Sopenharmony_ci	 *    sllx	%reg, 32, %reg
5998c2ecf20Sopenharmony_ci	 *    or	%reg, low_bits, %reg
6008c2ecf20Sopenharmony_ci	 */
6018c2ecf20Sopenharmony_ci	if (is_simm13(low_bits) && ((int)low_bits > 0)) {
6028c2ecf20Sopenharmony_ci		sparc_emit_set_const64_quick2(high_bits, low_bits,
6038c2ecf20Sopenharmony_ci					      dest, 32, ctx);
6048c2ecf20Sopenharmony_ci		return;
6058c2ecf20Sopenharmony_ci	}
6068c2ecf20Sopenharmony_ci
6078c2ecf20Sopenharmony_ci	/* Oh well, we tried... Do a full 64-bit decomposition.  */
6088c2ecf20Sopenharmony_ci	ctx->tmp_1_used = true;
6098c2ecf20Sopenharmony_ci
6108c2ecf20Sopenharmony_ci	emit_loadimm32(high_bits, tmp, ctx);
6118c2ecf20Sopenharmony_ci	emit_loadimm32(low_bits, dest, ctx);
6128c2ecf20Sopenharmony_ci	emit_alu_K(SLLX, tmp, 32, ctx);
6138c2ecf20Sopenharmony_ci	emit(OR | RS1(dest) | RS2(tmp) | RD(dest), ctx);
6148c2ecf20Sopenharmony_ci}
6158c2ecf20Sopenharmony_ci
6168c2ecf20Sopenharmony_cistatic void emit_branch(unsigned int br_opc, unsigned int from_idx, unsigned int to_idx,
6178c2ecf20Sopenharmony_ci			struct jit_ctx *ctx)
6188c2ecf20Sopenharmony_ci{
6198c2ecf20Sopenharmony_ci	unsigned int off = to_idx - from_idx;
6208c2ecf20Sopenharmony_ci
6218c2ecf20Sopenharmony_ci	if (br_opc & XCC)
6228c2ecf20Sopenharmony_ci		emit(br_opc | WDISP19(off << 2), ctx);
6238c2ecf20Sopenharmony_ci	else
6248c2ecf20Sopenharmony_ci		emit(br_opc | WDISP22(off << 2), ctx);
6258c2ecf20Sopenharmony_ci}
6268c2ecf20Sopenharmony_ci
6278c2ecf20Sopenharmony_cistatic void emit_cbcond(unsigned int cb_opc, unsigned int from_idx, unsigned int to_idx,
6288c2ecf20Sopenharmony_ci			const u8 dst, const u8 src, struct jit_ctx *ctx)
6298c2ecf20Sopenharmony_ci{
6308c2ecf20Sopenharmony_ci	unsigned int off = to_idx - from_idx;
6318c2ecf20Sopenharmony_ci
6328c2ecf20Sopenharmony_ci	emit(cb_opc | WDISP10(off << 2) | RS1(dst) | RS2(src), ctx);
6338c2ecf20Sopenharmony_ci}
6348c2ecf20Sopenharmony_ci
6358c2ecf20Sopenharmony_cistatic void emit_cbcondi(unsigned int cb_opc, unsigned int from_idx, unsigned int to_idx,
6368c2ecf20Sopenharmony_ci			 const u8 dst, s32 imm, struct jit_ctx *ctx)
6378c2ecf20Sopenharmony_ci{
6388c2ecf20Sopenharmony_ci	unsigned int off = to_idx - from_idx;
6398c2ecf20Sopenharmony_ci
6408c2ecf20Sopenharmony_ci	emit(cb_opc | IMMED | WDISP10(off << 2) | RS1(dst) | S5(imm), ctx);
6418c2ecf20Sopenharmony_ci}
6428c2ecf20Sopenharmony_ci
6438c2ecf20Sopenharmony_ci#define emit_read_y(REG, CTX)	emit(RD_Y | RD(REG), CTX)
6448c2ecf20Sopenharmony_ci#define emit_write_y(REG, CTX)	emit(WR_Y | IMMED | RS1(REG) | S13(0), CTX)
6458c2ecf20Sopenharmony_ci
6468c2ecf20Sopenharmony_ci#define emit_cmp(R1, R2, CTX)				\
6478c2ecf20Sopenharmony_ci	emit(SUBCC | RS1(R1) | RS2(R2) | RD(G0), CTX)
6488c2ecf20Sopenharmony_ci
6498c2ecf20Sopenharmony_ci#define emit_cmpi(R1, IMM, CTX)				\
6508c2ecf20Sopenharmony_ci	emit(SUBCC | IMMED | RS1(R1) | S13(IMM) | RD(G0), CTX)
6518c2ecf20Sopenharmony_ci
6528c2ecf20Sopenharmony_ci#define emit_btst(R1, R2, CTX)				\
6538c2ecf20Sopenharmony_ci	emit(ANDCC | RS1(R1) | RS2(R2) | RD(G0), CTX)
6548c2ecf20Sopenharmony_ci
6558c2ecf20Sopenharmony_ci#define emit_btsti(R1, IMM, CTX)			\
6568c2ecf20Sopenharmony_ci	emit(ANDCC | IMMED | RS1(R1) | S13(IMM) | RD(G0), CTX)
6578c2ecf20Sopenharmony_ci
6588c2ecf20Sopenharmony_cistatic int emit_compare_and_branch(const u8 code, const u8 dst, u8 src,
6598c2ecf20Sopenharmony_ci				   const s32 imm, bool is_imm, int branch_dst,
6608c2ecf20Sopenharmony_ci				   struct jit_ctx *ctx)
6618c2ecf20Sopenharmony_ci{
6628c2ecf20Sopenharmony_ci	bool use_cbcond = (sparc64_elf_hwcap & AV_SPARC_CBCOND) != 0;
6638c2ecf20Sopenharmony_ci	const u8 tmp = bpf2sparc[TMP_REG_1];
6648c2ecf20Sopenharmony_ci
6658c2ecf20Sopenharmony_ci	branch_dst = ctx->offset[branch_dst];
6668c2ecf20Sopenharmony_ci
6678c2ecf20Sopenharmony_ci	if (!is_simm10(branch_dst - ctx->idx) ||
6688c2ecf20Sopenharmony_ci	    BPF_OP(code) == BPF_JSET)
6698c2ecf20Sopenharmony_ci		use_cbcond = false;
6708c2ecf20Sopenharmony_ci
6718c2ecf20Sopenharmony_ci	if (is_imm) {
6728c2ecf20Sopenharmony_ci		bool fits = true;
6738c2ecf20Sopenharmony_ci
6748c2ecf20Sopenharmony_ci		if (use_cbcond) {
6758c2ecf20Sopenharmony_ci			if (!is_simm5(imm))
6768c2ecf20Sopenharmony_ci				fits = false;
6778c2ecf20Sopenharmony_ci		} else if (!is_simm13(imm)) {
6788c2ecf20Sopenharmony_ci			fits = false;
6798c2ecf20Sopenharmony_ci		}
6808c2ecf20Sopenharmony_ci		if (!fits) {
6818c2ecf20Sopenharmony_ci			ctx->tmp_1_used = true;
6828c2ecf20Sopenharmony_ci			emit_loadimm_sext(imm, tmp, ctx);
6838c2ecf20Sopenharmony_ci			src = tmp;
6848c2ecf20Sopenharmony_ci			is_imm = false;
6858c2ecf20Sopenharmony_ci		}
6868c2ecf20Sopenharmony_ci	}
6878c2ecf20Sopenharmony_ci
6888c2ecf20Sopenharmony_ci	if (!use_cbcond) {
6898c2ecf20Sopenharmony_ci		u32 br_opcode;
6908c2ecf20Sopenharmony_ci
6918c2ecf20Sopenharmony_ci		if (BPF_OP(code) == BPF_JSET) {
6928c2ecf20Sopenharmony_ci			if (is_imm)
6938c2ecf20Sopenharmony_ci				emit_btsti(dst, imm, ctx);
6948c2ecf20Sopenharmony_ci			else
6958c2ecf20Sopenharmony_ci				emit_btst(dst, src, ctx);
6968c2ecf20Sopenharmony_ci		} else {
6978c2ecf20Sopenharmony_ci			if (is_imm)
6988c2ecf20Sopenharmony_ci				emit_cmpi(dst, imm, ctx);
6998c2ecf20Sopenharmony_ci			else
7008c2ecf20Sopenharmony_ci				emit_cmp(dst, src, ctx);
7018c2ecf20Sopenharmony_ci		}
7028c2ecf20Sopenharmony_ci		switch (BPF_OP(code)) {
7038c2ecf20Sopenharmony_ci		case BPF_JEQ:
7048c2ecf20Sopenharmony_ci			br_opcode = BE;
7058c2ecf20Sopenharmony_ci			break;
7068c2ecf20Sopenharmony_ci		case BPF_JGT:
7078c2ecf20Sopenharmony_ci			br_opcode = BGU;
7088c2ecf20Sopenharmony_ci			break;
7098c2ecf20Sopenharmony_ci		case BPF_JLT:
7108c2ecf20Sopenharmony_ci			br_opcode = BLU;
7118c2ecf20Sopenharmony_ci			break;
7128c2ecf20Sopenharmony_ci		case BPF_JGE:
7138c2ecf20Sopenharmony_ci			br_opcode = BGEU;
7148c2ecf20Sopenharmony_ci			break;
7158c2ecf20Sopenharmony_ci		case BPF_JLE:
7168c2ecf20Sopenharmony_ci			br_opcode = BLEU;
7178c2ecf20Sopenharmony_ci			break;
7188c2ecf20Sopenharmony_ci		case BPF_JSET:
7198c2ecf20Sopenharmony_ci		case BPF_JNE:
7208c2ecf20Sopenharmony_ci			br_opcode = BNE;
7218c2ecf20Sopenharmony_ci			break;
7228c2ecf20Sopenharmony_ci		case BPF_JSGT:
7238c2ecf20Sopenharmony_ci			br_opcode = BG;
7248c2ecf20Sopenharmony_ci			break;
7258c2ecf20Sopenharmony_ci		case BPF_JSLT:
7268c2ecf20Sopenharmony_ci			br_opcode = BL;
7278c2ecf20Sopenharmony_ci			break;
7288c2ecf20Sopenharmony_ci		case BPF_JSGE:
7298c2ecf20Sopenharmony_ci			br_opcode = BGE;
7308c2ecf20Sopenharmony_ci			break;
7318c2ecf20Sopenharmony_ci		case BPF_JSLE:
7328c2ecf20Sopenharmony_ci			br_opcode = BLE;
7338c2ecf20Sopenharmony_ci			break;
7348c2ecf20Sopenharmony_ci		default:
7358c2ecf20Sopenharmony_ci			/* Make sure we dont leak kernel information to the
7368c2ecf20Sopenharmony_ci			 * user.
7378c2ecf20Sopenharmony_ci			 */
7388c2ecf20Sopenharmony_ci			return -EFAULT;
7398c2ecf20Sopenharmony_ci		}
7408c2ecf20Sopenharmony_ci		emit_branch(br_opcode, ctx->idx, branch_dst, ctx);
7418c2ecf20Sopenharmony_ci		emit_nop(ctx);
7428c2ecf20Sopenharmony_ci	} else {
7438c2ecf20Sopenharmony_ci		u32 cbcond_opcode;
7448c2ecf20Sopenharmony_ci
7458c2ecf20Sopenharmony_ci		switch (BPF_OP(code)) {
7468c2ecf20Sopenharmony_ci		case BPF_JEQ:
7478c2ecf20Sopenharmony_ci			cbcond_opcode = CBCONDE;
7488c2ecf20Sopenharmony_ci			break;
7498c2ecf20Sopenharmony_ci		case BPF_JGT:
7508c2ecf20Sopenharmony_ci			cbcond_opcode = CBCONDGU;
7518c2ecf20Sopenharmony_ci			break;
7528c2ecf20Sopenharmony_ci		case BPF_JLT:
7538c2ecf20Sopenharmony_ci			cbcond_opcode = CBCONDLU;
7548c2ecf20Sopenharmony_ci			break;
7558c2ecf20Sopenharmony_ci		case BPF_JGE:
7568c2ecf20Sopenharmony_ci			cbcond_opcode = CBCONDGEU;
7578c2ecf20Sopenharmony_ci			break;
7588c2ecf20Sopenharmony_ci		case BPF_JLE:
7598c2ecf20Sopenharmony_ci			cbcond_opcode = CBCONDLEU;
7608c2ecf20Sopenharmony_ci			break;
7618c2ecf20Sopenharmony_ci		case BPF_JNE:
7628c2ecf20Sopenharmony_ci			cbcond_opcode = CBCONDNE;
7638c2ecf20Sopenharmony_ci			break;
7648c2ecf20Sopenharmony_ci		case BPF_JSGT:
7658c2ecf20Sopenharmony_ci			cbcond_opcode = CBCONDG;
7668c2ecf20Sopenharmony_ci			break;
7678c2ecf20Sopenharmony_ci		case BPF_JSLT:
7688c2ecf20Sopenharmony_ci			cbcond_opcode = CBCONDL;
7698c2ecf20Sopenharmony_ci			break;
7708c2ecf20Sopenharmony_ci		case BPF_JSGE:
7718c2ecf20Sopenharmony_ci			cbcond_opcode = CBCONDGE;
7728c2ecf20Sopenharmony_ci			break;
7738c2ecf20Sopenharmony_ci		case BPF_JSLE:
7748c2ecf20Sopenharmony_ci			cbcond_opcode = CBCONDLE;
7758c2ecf20Sopenharmony_ci			break;
7768c2ecf20Sopenharmony_ci		default:
7778c2ecf20Sopenharmony_ci			/* Make sure we dont leak kernel information to the
7788c2ecf20Sopenharmony_ci			 * user.
7798c2ecf20Sopenharmony_ci			 */
7808c2ecf20Sopenharmony_ci			return -EFAULT;
7818c2ecf20Sopenharmony_ci		}
7828c2ecf20Sopenharmony_ci		cbcond_opcode |= CBCOND_OP;
7838c2ecf20Sopenharmony_ci		if (is_imm)
7848c2ecf20Sopenharmony_ci			emit_cbcondi(cbcond_opcode, ctx->idx, branch_dst,
7858c2ecf20Sopenharmony_ci				     dst, imm, ctx);
7868c2ecf20Sopenharmony_ci		else
7878c2ecf20Sopenharmony_ci			emit_cbcond(cbcond_opcode, ctx->idx, branch_dst,
7888c2ecf20Sopenharmony_ci				    dst, src, ctx);
7898c2ecf20Sopenharmony_ci	}
7908c2ecf20Sopenharmony_ci	return 0;
7918c2ecf20Sopenharmony_ci}
7928c2ecf20Sopenharmony_ci
7938c2ecf20Sopenharmony_ci/* Just skip the save instruction and the ctx register move.  */
7948c2ecf20Sopenharmony_ci#define BPF_TAILCALL_PROLOGUE_SKIP	32
7958c2ecf20Sopenharmony_ci#define BPF_TAILCALL_CNT_SP_OFF		(STACK_BIAS + 128)
7968c2ecf20Sopenharmony_ci
7978c2ecf20Sopenharmony_cistatic void build_prologue(struct jit_ctx *ctx)
7988c2ecf20Sopenharmony_ci{
7998c2ecf20Sopenharmony_ci	s32 stack_needed = BASE_STACKFRAME;
8008c2ecf20Sopenharmony_ci
8018c2ecf20Sopenharmony_ci	if (ctx->saw_frame_pointer || ctx->saw_tail_call) {
8028c2ecf20Sopenharmony_ci		struct bpf_prog *prog = ctx->prog;
8038c2ecf20Sopenharmony_ci		u32 stack_depth;
8048c2ecf20Sopenharmony_ci
8058c2ecf20Sopenharmony_ci		stack_depth = prog->aux->stack_depth;
8068c2ecf20Sopenharmony_ci		stack_needed += round_up(stack_depth, 16);
8078c2ecf20Sopenharmony_ci	}
8088c2ecf20Sopenharmony_ci
8098c2ecf20Sopenharmony_ci	if (ctx->saw_tail_call)
8108c2ecf20Sopenharmony_ci		stack_needed += 8;
8118c2ecf20Sopenharmony_ci
8128c2ecf20Sopenharmony_ci	/* save %sp, -176, %sp */
8138c2ecf20Sopenharmony_ci	emit(SAVE | IMMED | RS1(SP) | S13(-stack_needed) | RD(SP), ctx);
8148c2ecf20Sopenharmony_ci
8158c2ecf20Sopenharmony_ci	/* tail_call_cnt = 0 */
8168c2ecf20Sopenharmony_ci	if (ctx->saw_tail_call) {
8178c2ecf20Sopenharmony_ci		u32 off = BPF_TAILCALL_CNT_SP_OFF;
8188c2ecf20Sopenharmony_ci
8198c2ecf20Sopenharmony_ci		emit(ST32 | IMMED | RS1(SP) | S13(off) | RD(G0), ctx);
8208c2ecf20Sopenharmony_ci	} else {
8218c2ecf20Sopenharmony_ci		emit_nop(ctx);
8228c2ecf20Sopenharmony_ci	}
8238c2ecf20Sopenharmony_ci	if (ctx->saw_frame_pointer) {
8248c2ecf20Sopenharmony_ci		const u8 vfp = bpf2sparc[BPF_REG_FP];
8258c2ecf20Sopenharmony_ci
8268c2ecf20Sopenharmony_ci		emit(ADD | IMMED | RS1(FP) | S13(STACK_BIAS) | RD(vfp), ctx);
8278c2ecf20Sopenharmony_ci	} else {
8288c2ecf20Sopenharmony_ci		emit_nop(ctx);
8298c2ecf20Sopenharmony_ci	}
8308c2ecf20Sopenharmony_ci
8318c2ecf20Sopenharmony_ci	emit_reg_move(I0, O0, ctx);
8328c2ecf20Sopenharmony_ci	emit_reg_move(I1, O1, ctx);
8338c2ecf20Sopenharmony_ci	emit_reg_move(I2, O2, ctx);
8348c2ecf20Sopenharmony_ci	emit_reg_move(I3, O3, ctx);
8358c2ecf20Sopenharmony_ci	emit_reg_move(I4, O4, ctx);
8368c2ecf20Sopenharmony_ci	/* If you add anything here, adjust BPF_TAILCALL_PROLOGUE_SKIP above. */
8378c2ecf20Sopenharmony_ci}
8388c2ecf20Sopenharmony_ci
8398c2ecf20Sopenharmony_cistatic void build_epilogue(struct jit_ctx *ctx)
8408c2ecf20Sopenharmony_ci{
8418c2ecf20Sopenharmony_ci	ctx->epilogue_offset = ctx->idx;
8428c2ecf20Sopenharmony_ci
8438c2ecf20Sopenharmony_ci	/* ret (jmpl %i7 + 8, %g0) */
8448c2ecf20Sopenharmony_ci	emit(JMPL | IMMED | RS1(I7) | S13(8) | RD(G0), ctx);
8458c2ecf20Sopenharmony_ci
8468c2ecf20Sopenharmony_ci	/* restore %i5, %g0, %o0 */
8478c2ecf20Sopenharmony_ci	emit(RESTORE | RS1(bpf2sparc[BPF_REG_0]) | RS2(G0) | RD(O0), ctx);
8488c2ecf20Sopenharmony_ci}
8498c2ecf20Sopenharmony_ci
8508c2ecf20Sopenharmony_cistatic void emit_tail_call(struct jit_ctx *ctx)
8518c2ecf20Sopenharmony_ci{
8528c2ecf20Sopenharmony_ci	const u8 bpf_array = bpf2sparc[BPF_REG_2];
8538c2ecf20Sopenharmony_ci	const u8 bpf_index = bpf2sparc[BPF_REG_3];
8548c2ecf20Sopenharmony_ci	const u8 tmp = bpf2sparc[TMP_REG_1];
8558c2ecf20Sopenharmony_ci	u32 off;
8568c2ecf20Sopenharmony_ci
8578c2ecf20Sopenharmony_ci	ctx->saw_tail_call = true;
8588c2ecf20Sopenharmony_ci
8598c2ecf20Sopenharmony_ci	off = offsetof(struct bpf_array, map.max_entries);
8608c2ecf20Sopenharmony_ci	emit(LD32 | IMMED | RS1(bpf_array) | S13(off) | RD(tmp), ctx);
8618c2ecf20Sopenharmony_ci	emit_cmp(bpf_index, tmp, ctx);
8628c2ecf20Sopenharmony_ci#define OFFSET1 17
8638c2ecf20Sopenharmony_ci	emit_branch(BGEU, ctx->idx, ctx->idx + OFFSET1, ctx);
8648c2ecf20Sopenharmony_ci	emit_nop(ctx);
8658c2ecf20Sopenharmony_ci
8668c2ecf20Sopenharmony_ci	off = BPF_TAILCALL_CNT_SP_OFF;
8678c2ecf20Sopenharmony_ci	emit(LD32 | IMMED | RS1(SP) | S13(off) | RD(tmp), ctx);
8688c2ecf20Sopenharmony_ci	emit_cmpi(tmp, MAX_TAIL_CALL_CNT, ctx);
8698c2ecf20Sopenharmony_ci#define OFFSET2 13
8708c2ecf20Sopenharmony_ci	emit_branch(BGU, ctx->idx, ctx->idx + OFFSET2, ctx);
8718c2ecf20Sopenharmony_ci	emit_nop(ctx);
8728c2ecf20Sopenharmony_ci
8738c2ecf20Sopenharmony_ci	emit_alu_K(ADD, tmp, 1, ctx);
8748c2ecf20Sopenharmony_ci	off = BPF_TAILCALL_CNT_SP_OFF;
8758c2ecf20Sopenharmony_ci	emit(ST32 | IMMED | RS1(SP) | S13(off) | RD(tmp), ctx);
8768c2ecf20Sopenharmony_ci
8778c2ecf20Sopenharmony_ci	emit_alu3_K(SLL, bpf_index, 3, tmp, ctx);
8788c2ecf20Sopenharmony_ci	emit_alu(ADD, bpf_array, tmp, ctx);
8798c2ecf20Sopenharmony_ci	off = offsetof(struct bpf_array, ptrs);
8808c2ecf20Sopenharmony_ci	emit(LD64 | IMMED | RS1(tmp) | S13(off) | RD(tmp), ctx);
8818c2ecf20Sopenharmony_ci
8828c2ecf20Sopenharmony_ci	emit_cmpi(tmp, 0, ctx);
8838c2ecf20Sopenharmony_ci#define OFFSET3 5
8848c2ecf20Sopenharmony_ci	emit_branch(BE, ctx->idx, ctx->idx + OFFSET3, ctx);
8858c2ecf20Sopenharmony_ci	emit_nop(ctx);
8868c2ecf20Sopenharmony_ci
8878c2ecf20Sopenharmony_ci	off = offsetof(struct bpf_prog, bpf_func);
8888c2ecf20Sopenharmony_ci	emit(LD64 | IMMED | RS1(tmp) | S13(off) | RD(tmp), ctx);
8898c2ecf20Sopenharmony_ci
8908c2ecf20Sopenharmony_ci	off = BPF_TAILCALL_PROLOGUE_SKIP;
8918c2ecf20Sopenharmony_ci	emit(JMPL | IMMED | RS1(tmp) | S13(off) | RD(G0), ctx);
8928c2ecf20Sopenharmony_ci	emit_nop(ctx);
8938c2ecf20Sopenharmony_ci}
8948c2ecf20Sopenharmony_ci
8958c2ecf20Sopenharmony_cistatic int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx)
8968c2ecf20Sopenharmony_ci{
8978c2ecf20Sopenharmony_ci	const u8 code = insn->code;
8988c2ecf20Sopenharmony_ci	const u8 dst = bpf2sparc[insn->dst_reg];
8998c2ecf20Sopenharmony_ci	const u8 src = bpf2sparc[insn->src_reg];
9008c2ecf20Sopenharmony_ci	const int i = insn - ctx->prog->insnsi;
9018c2ecf20Sopenharmony_ci	const s16 off = insn->off;
9028c2ecf20Sopenharmony_ci	const s32 imm = insn->imm;
9038c2ecf20Sopenharmony_ci
9048c2ecf20Sopenharmony_ci	if (insn->src_reg == BPF_REG_FP)
9058c2ecf20Sopenharmony_ci		ctx->saw_frame_pointer = true;
9068c2ecf20Sopenharmony_ci
9078c2ecf20Sopenharmony_ci	switch (code) {
9088c2ecf20Sopenharmony_ci	/* dst = src */
9098c2ecf20Sopenharmony_ci	case BPF_ALU | BPF_MOV | BPF_X:
9108c2ecf20Sopenharmony_ci		emit_alu3_K(SRL, src, 0, dst, ctx);
9118c2ecf20Sopenharmony_ci		if (insn_is_zext(&insn[1]))
9128c2ecf20Sopenharmony_ci			return 1;
9138c2ecf20Sopenharmony_ci		break;
9148c2ecf20Sopenharmony_ci	case BPF_ALU64 | BPF_MOV | BPF_X:
9158c2ecf20Sopenharmony_ci		emit_reg_move(src, dst, ctx);
9168c2ecf20Sopenharmony_ci		break;
9178c2ecf20Sopenharmony_ci	/* dst = dst OP src */
9188c2ecf20Sopenharmony_ci	case BPF_ALU | BPF_ADD | BPF_X:
9198c2ecf20Sopenharmony_ci	case BPF_ALU64 | BPF_ADD | BPF_X:
9208c2ecf20Sopenharmony_ci		emit_alu(ADD, src, dst, ctx);
9218c2ecf20Sopenharmony_ci		goto do_alu32_trunc;
9228c2ecf20Sopenharmony_ci	case BPF_ALU | BPF_SUB | BPF_X:
9238c2ecf20Sopenharmony_ci	case BPF_ALU64 | BPF_SUB | BPF_X:
9248c2ecf20Sopenharmony_ci		emit_alu(SUB, src, dst, ctx);
9258c2ecf20Sopenharmony_ci		goto do_alu32_trunc;
9268c2ecf20Sopenharmony_ci	case BPF_ALU | BPF_AND | BPF_X:
9278c2ecf20Sopenharmony_ci	case BPF_ALU64 | BPF_AND | BPF_X:
9288c2ecf20Sopenharmony_ci		emit_alu(AND, src, dst, ctx);
9298c2ecf20Sopenharmony_ci		goto do_alu32_trunc;
9308c2ecf20Sopenharmony_ci	case BPF_ALU | BPF_OR | BPF_X:
9318c2ecf20Sopenharmony_ci	case BPF_ALU64 | BPF_OR | BPF_X:
9328c2ecf20Sopenharmony_ci		emit_alu(OR, src, dst, ctx);
9338c2ecf20Sopenharmony_ci		goto do_alu32_trunc;
9348c2ecf20Sopenharmony_ci	case BPF_ALU | BPF_XOR | BPF_X:
9358c2ecf20Sopenharmony_ci	case BPF_ALU64 | BPF_XOR | BPF_X:
9368c2ecf20Sopenharmony_ci		emit_alu(XOR, src, dst, ctx);
9378c2ecf20Sopenharmony_ci		goto do_alu32_trunc;
9388c2ecf20Sopenharmony_ci	case BPF_ALU | BPF_MUL | BPF_X:
9398c2ecf20Sopenharmony_ci		emit_alu(MUL, src, dst, ctx);
9408c2ecf20Sopenharmony_ci		goto do_alu32_trunc;
9418c2ecf20Sopenharmony_ci	case BPF_ALU64 | BPF_MUL | BPF_X:
9428c2ecf20Sopenharmony_ci		emit_alu(MULX, src, dst, ctx);
9438c2ecf20Sopenharmony_ci		break;
9448c2ecf20Sopenharmony_ci	case BPF_ALU | BPF_DIV | BPF_X:
9458c2ecf20Sopenharmony_ci		emit_write_y(G0, ctx);
9468c2ecf20Sopenharmony_ci		emit_alu(DIV, src, dst, ctx);
9478c2ecf20Sopenharmony_ci		if (insn_is_zext(&insn[1]))
9488c2ecf20Sopenharmony_ci			return 1;
9498c2ecf20Sopenharmony_ci		break;
9508c2ecf20Sopenharmony_ci	case BPF_ALU64 | BPF_DIV | BPF_X:
9518c2ecf20Sopenharmony_ci		emit_alu(UDIVX, src, dst, ctx);
9528c2ecf20Sopenharmony_ci		break;
9538c2ecf20Sopenharmony_ci	case BPF_ALU | BPF_MOD | BPF_X: {
9548c2ecf20Sopenharmony_ci		const u8 tmp = bpf2sparc[TMP_REG_1];
9558c2ecf20Sopenharmony_ci
9568c2ecf20Sopenharmony_ci		ctx->tmp_1_used = true;
9578c2ecf20Sopenharmony_ci
9588c2ecf20Sopenharmony_ci		emit_write_y(G0, ctx);
9598c2ecf20Sopenharmony_ci		emit_alu3(DIV, dst, src, tmp, ctx);
9608c2ecf20Sopenharmony_ci		emit_alu3(MULX, tmp, src, tmp, ctx);
9618c2ecf20Sopenharmony_ci		emit_alu3(SUB, dst, tmp, dst, ctx);
9628c2ecf20Sopenharmony_ci		goto do_alu32_trunc;
9638c2ecf20Sopenharmony_ci	}
9648c2ecf20Sopenharmony_ci	case BPF_ALU64 | BPF_MOD | BPF_X: {
9658c2ecf20Sopenharmony_ci		const u8 tmp = bpf2sparc[TMP_REG_1];
9668c2ecf20Sopenharmony_ci
9678c2ecf20Sopenharmony_ci		ctx->tmp_1_used = true;
9688c2ecf20Sopenharmony_ci
9698c2ecf20Sopenharmony_ci		emit_alu3(UDIVX, dst, src, tmp, ctx);
9708c2ecf20Sopenharmony_ci		emit_alu3(MULX, tmp, src, tmp, ctx);
9718c2ecf20Sopenharmony_ci		emit_alu3(SUB, dst, tmp, dst, ctx);
9728c2ecf20Sopenharmony_ci		break;
9738c2ecf20Sopenharmony_ci	}
9748c2ecf20Sopenharmony_ci	case BPF_ALU | BPF_LSH | BPF_X:
9758c2ecf20Sopenharmony_ci		emit_alu(SLL, src, dst, ctx);
9768c2ecf20Sopenharmony_ci		goto do_alu32_trunc;
9778c2ecf20Sopenharmony_ci	case BPF_ALU64 | BPF_LSH | BPF_X:
9788c2ecf20Sopenharmony_ci		emit_alu(SLLX, src, dst, ctx);
9798c2ecf20Sopenharmony_ci		break;
9808c2ecf20Sopenharmony_ci	case BPF_ALU | BPF_RSH | BPF_X:
9818c2ecf20Sopenharmony_ci		emit_alu(SRL, src, dst, ctx);
9828c2ecf20Sopenharmony_ci		if (insn_is_zext(&insn[1]))
9838c2ecf20Sopenharmony_ci			return 1;
9848c2ecf20Sopenharmony_ci		break;
9858c2ecf20Sopenharmony_ci	case BPF_ALU64 | BPF_RSH | BPF_X:
9868c2ecf20Sopenharmony_ci		emit_alu(SRLX, src, dst, ctx);
9878c2ecf20Sopenharmony_ci		break;
9888c2ecf20Sopenharmony_ci	case BPF_ALU | BPF_ARSH | BPF_X:
9898c2ecf20Sopenharmony_ci		emit_alu(SRA, src, dst, ctx);
9908c2ecf20Sopenharmony_ci		goto do_alu32_trunc;
9918c2ecf20Sopenharmony_ci	case BPF_ALU64 | BPF_ARSH | BPF_X:
9928c2ecf20Sopenharmony_ci		emit_alu(SRAX, src, dst, ctx);
9938c2ecf20Sopenharmony_ci		break;
9948c2ecf20Sopenharmony_ci
9958c2ecf20Sopenharmony_ci	/* dst = -dst */
9968c2ecf20Sopenharmony_ci	case BPF_ALU | BPF_NEG:
9978c2ecf20Sopenharmony_ci	case BPF_ALU64 | BPF_NEG:
9988c2ecf20Sopenharmony_ci		emit(SUB | RS1(0) | RS2(dst) | RD(dst), ctx);
9998c2ecf20Sopenharmony_ci		goto do_alu32_trunc;
10008c2ecf20Sopenharmony_ci
10018c2ecf20Sopenharmony_ci	case BPF_ALU | BPF_END | BPF_FROM_BE:
10028c2ecf20Sopenharmony_ci		switch (imm) {
10038c2ecf20Sopenharmony_ci		case 16:
10048c2ecf20Sopenharmony_ci			emit_alu_K(SLL, dst, 16, ctx);
10058c2ecf20Sopenharmony_ci			emit_alu_K(SRL, dst, 16, ctx);
10068c2ecf20Sopenharmony_ci			if (insn_is_zext(&insn[1]))
10078c2ecf20Sopenharmony_ci				return 1;
10088c2ecf20Sopenharmony_ci			break;
10098c2ecf20Sopenharmony_ci		case 32:
10108c2ecf20Sopenharmony_ci			if (!ctx->prog->aux->verifier_zext)
10118c2ecf20Sopenharmony_ci				emit_alu_K(SRL, dst, 0, ctx);
10128c2ecf20Sopenharmony_ci			break;
10138c2ecf20Sopenharmony_ci		case 64:
10148c2ecf20Sopenharmony_ci			/* nop */
10158c2ecf20Sopenharmony_ci			break;
10168c2ecf20Sopenharmony_ci
10178c2ecf20Sopenharmony_ci		}
10188c2ecf20Sopenharmony_ci		break;
10198c2ecf20Sopenharmony_ci
10208c2ecf20Sopenharmony_ci	/* dst = BSWAP##imm(dst) */
10218c2ecf20Sopenharmony_ci	case BPF_ALU | BPF_END | BPF_FROM_LE: {
10228c2ecf20Sopenharmony_ci		const u8 tmp = bpf2sparc[TMP_REG_1];
10238c2ecf20Sopenharmony_ci		const u8 tmp2 = bpf2sparc[TMP_REG_2];
10248c2ecf20Sopenharmony_ci
10258c2ecf20Sopenharmony_ci		ctx->tmp_1_used = true;
10268c2ecf20Sopenharmony_ci		switch (imm) {
10278c2ecf20Sopenharmony_ci		case 16:
10288c2ecf20Sopenharmony_ci			emit_alu3_K(AND, dst, 0xff, tmp, ctx);
10298c2ecf20Sopenharmony_ci			emit_alu3_K(SRL, dst, 8, dst, ctx);
10308c2ecf20Sopenharmony_ci			emit_alu3_K(AND, dst, 0xff, dst, ctx);
10318c2ecf20Sopenharmony_ci			emit_alu3_K(SLL, tmp, 8, tmp, ctx);
10328c2ecf20Sopenharmony_ci			emit_alu(OR, tmp, dst, ctx);
10338c2ecf20Sopenharmony_ci			if (insn_is_zext(&insn[1]))
10348c2ecf20Sopenharmony_ci				return 1;
10358c2ecf20Sopenharmony_ci			break;
10368c2ecf20Sopenharmony_ci
10378c2ecf20Sopenharmony_ci		case 32:
10388c2ecf20Sopenharmony_ci			ctx->tmp_2_used = true;
10398c2ecf20Sopenharmony_ci			emit_alu3_K(SRL, dst, 24, tmp, ctx);	/* tmp  = dst >> 24 */
10408c2ecf20Sopenharmony_ci			emit_alu3_K(SRL, dst, 16, tmp2, ctx);	/* tmp2 = dst >> 16 */
10418c2ecf20Sopenharmony_ci			emit_alu3_K(AND, tmp2, 0xff, tmp2, ctx);/* tmp2 = tmp2 & 0xff */
10428c2ecf20Sopenharmony_ci			emit_alu3_K(SLL, tmp2, 8, tmp2, ctx);	/* tmp2 = tmp2 << 8 */
10438c2ecf20Sopenharmony_ci			emit_alu(OR, tmp2, tmp, ctx);		/* tmp  = tmp | tmp2 */
10448c2ecf20Sopenharmony_ci			emit_alu3_K(SRL, dst, 8, tmp2, ctx);	/* tmp2 = dst >> 8 */
10458c2ecf20Sopenharmony_ci			emit_alu3_K(AND, tmp2, 0xff, tmp2, ctx);/* tmp2 = tmp2 & 0xff */
10468c2ecf20Sopenharmony_ci			emit_alu3_K(SLL, tmp2, 16, tmp2, ctx);	/* tmp2 = tmp2 << 16 */
10478c2ecf20Sopenharmony_ci			emit_alu(OR, tmp2, tmp, ctx);		/* tmp  = tmp | tmp2 */
10488c2ecf20Sopenharmony_ci			emit_alu3_K(AND, dst, 0xff, dst, ctx);	/* dst	= dst & 0xff */
10498c2ecf20Sopenharmony_ci			emit_alu3_K(SLL, dst, 24, dst, ctx);	/* dst  = dst << 24 */
10508c2ecf20Sopenharmony_ci			emit_alu(OR, tmp, dst, ctx);		/* dst  = dst | tmp */
10518c2ecf20Sopenharmony_ci			if (insn_is_zext(&insn[1]))
10528c2ecf20Sopenharmony_ci				return 1;
10538c2ecf20Sopenharmony_ci			break;
10548c2ecf20Sopenharmony_ci
10558c2ecf20Sopenharmony_ci		case 64:
10568c2ecf20Sopenharmony_ci			emit_alu3_K(ADD, SP, STACK_BIAS + 128, tmp, ctx);
10578c2ecf20Sopenharmony_ci			emit(ST64 | RS1(tmp) | RS2(G0) | RD(dst), ctx);
10588c2ecf20Sopenharmony_ci			emit(LD64A | ASI(ASI_PL) | RS1(tmp) | RS2(G0) | RD(dst), ctx);
10598c2ecf20Sopenharmony_ci			break;
10608c2ecf20Sopenharmony_ci		}
10618c2ecf20Sopenharmony_ci		break;
10628c2ecf20Sopenharmony_ci	}
10638c2ecf20Sopenharmony_ci	/* dst = imm */
10648c2ecf20Sopenharmony_ci	case BPF_ALU | BPF_MOV | BPF_K:
10658c2ecf20Sopenharmony_ci		emit_loadimm32(imm, dst, ctx);
10668c2ecf20Sopenharmony_ci		if (insn_is_zext(&insn[1]))
10678c2ecf20Sopenharmony_ci			return 1;
10688c2ecf20Sopenharmony_ci		break;
10698c2ecf20Sopenharmony_ci	case BPF_ALU64 | BPF_MOV | BPF_K:
10708c2ecf20Sopenharmony_ci		emit_loadimm_sext(imm, dst, ctx);
10718c2ecf20Sopenharmony_ci		break;
10728c2ecf20Sopenharmony_ci	/* dst = dst OP imm */
10738c2ecf20Sopenharmony_ci	case BPF_ALU | BPF_ADD | BPF_K:
10748c2ecf20Sopenharmony_ci	case BPF_ALU64 | BPF_ADD | BPF_K:
10758c2ecf20Sopenharmony_ci		emit_alu_K(ADD, dst, imm, ctx);
10768c2ecf20Sopenharmony_ci		goto do_alu32_trunc;
10778c2ecf20Sopenharmony_ci	case BPF_ALU | BPF_SUB | BPF_K:
10788c2ecf20Sopenharmony_ci	case BPF_ALU64 | BPF_SUB | BPF_K:
10798c2ecf20Sopenharmony_ci		emit_alu_K(SUB, dst, imm, ctx);
10808c2ecf20Sopenharmony_ci		goto do_alu32_trunc;
10818c2ecf20Sopenharmony_ci	case BPF_ALU | BPF_AND | BPF_K:
10828c2ecf20Sopenharmony_ci	case BPF_ALU64 | BPF_AND | BPF_K:
10838c2ecf20Sopenharmony_ci		emit_alu_K(AND, dst, imm, ctx);
10848c2ecf20Sopenharmony_ci		goto do_alu32_trunc;
10858c2ecf20Sopenharmony_ci	case BPF_ALU | BPF_OR | BPF_K:
10868c2ecf20Sopenharmony_ci	case BPF_ALU64 | BPF_OR | BPF_K:
10878c2ecf20Sopenharmony_ci		emit_alu_K(OR, dst, imm, ctx);
10888c2ecf20Sopenharmony_ci		goto do_alu32_trunc;
10898c2ecf20Sopenharmony_ci	case BPF_ALU | BPF_XOR | BPF_K:
10908c2ecf20Sopenharmony_ci	case BPF_ALU64 | BPF_XOR | BPF_K:
10918c2ecf20Sopenharmony_ci		emit_alu_K(XOR, dst, imm, ctx);
10928c2ecf20Sopenharmony_ci		goto do_alu32_trunc;
10938c2ecf20Sopenharmony_ci	case BPF_ALU | BPF_MUL | BPF_K:
10948c2ecf20Sopenharmony_ci		emit_alu_K(MUL, dst, imm, ctx);
10958c2ecf20Sopenharmony_ci		goto do_alu32_trunc;
10968c2ecf20Sopenharmony_ci	case BPF_ALU64 | BPF_MUL | BPF_K:
10978c2ecf20Sopenharmony_ci		emit_alu_K(MULX, dst, imm, ctx);
10988c2ecf20Sopenharmony_ci		break;
10998c2ecf20Sopenharmony_ci	case BPF_ALU | BPF_DIV | BPF_K:
11008c2ecf20Sopenharmony_ci		if (imm == 0)
11018c2ecf20Sopenharmony_ci			return -EINVAL;
11028c2ecf20Sopenharmony_ci
11038c2ecf20Sopenharmony_ci		emit_write_y(G0, ctx);
11048c2ecf20Sopenharmony_ci		emit_alu_K(DIV, dst, imm, ctx);
11058c2ecf20Sopenharmony_ci		goto do_alu32_trunc;
11068c2ecf20Sopenharmony_ci	case BPF_ALU64 | BPF_DIV | BPF_K:
11078c2ecf20Sopenharmony_ci		if (imm == 0)
11088c2ecf20Sopenharmony_ci			return -EINVAL;
11098c2ecf20Sopenharmony_ci
11108c2ecf20Sopenharmony_ci		emit_alu_K(UDIVX, dst, imm, ctx);
11118c2ecf20Sopenharmony_ci		break;
11128c2ecf20Sopenharmony_ci	case BPF_ALU64 | BPF_MOD | BPF_K:
11138c2ecf20Sopenharmony_ci	case BPF_ALU | BPF_MOD | BPF_K: {
11148c2ecf20Sopenharmony_ci		const u8 tmp = bpf2sparc[TMP_REG_2];
11158c2ecf20Sopenharmony_ci		unsigned int div;
11168c2ecf20Sopenharmony_ci
11178c2ecf20Sopenharmony_ci		if (imm == 0)
11188c2ecf20Sopenharmony_ci			return -EINVAL;
11198c2ecf20Sopenharmony_ci
11208c2ecf20Sopenharmony_ci		div = (BPF_CLASS(code) == BPF_ALU64) ? UDIVX : DIV;
11218c2ecf20Sopenharmony_ci
11228c2ecf20Sopenharmony_ci		ctx->tmp_2_used = true;
11238c2ecf20Sopenharmony_ci
11248c2ecf20Sopenharmony_ci		if (BPF_CLASS(code) != BPF_ALU64)
11258c2ecf20Sopenharmony_ci			emit_write_y(G0, ctx);
11268c2ecf20Sopenharmony_ci		if (is_simm13(imm)) {
11278c2ecf20Sopenharmony_ci			emit(div | IMMED | RS1(dst) | S13(imm) | RD(tmp), ctx);
11288c2ecf20Sopenharmony_ci			emit(MULX | IMMED | RS1(tmp) | S13(imm) | RD(tmp), ctx);
11298c2ecf20Sopenharmony_ci			emit(SUB | RS1(dst) | RS2(tmp) | RD(dst), ctx);
11308c2ecf20Sopenharmony_ci		} else {
11318c2ecf20Sopenharmony_ci			const u8 tmp1 = bpf2sparc[TMP_REG_1];
11328c2ecf20Sopenharmony_ci
11338c2ecf20Sopenharmony_ci			ctx->tmp_1_used = true;
11348c2ecf20Sopenharmony_ci
11358c2ecf20Sopenharmony_ci			emit_set_const_sext(imm, tmp1, ctx);
11368c2ecf20Sopenharmony_ci			emit(div | RS1(dst) | RS2(tmp1) | RD(tmp), ctx);
11378c2ecf20Sopenharmony_ci			emit(MULX | RS1(tmp) | RS2(tmp1) | RD(tmp), ctx);
11388c2ecf20Sopenharmony_ci			emit(SUB | RS1(dst) | RS2(tmp) | RD(dst), ctx);
11398c2ecf20Sopenharmony_ci		}
11408c2ecf20Sopenharmony_ci		goto do_alu32_trunc;
11418c2ecf20Sopenharmony_ci	}
11428c2ecf20Sopenharmony_ci	case BPF_ALU | BPF_LSH | BPF_K:
11438c2ecf20Sopenharmony_ci		emit_alu_K(SLL, dst, imm, ctx);
11448c2ecf20Sopenharmony_ci		goto do_alu32_trunc;
11458c2ecf20Sopenharmony_ci	case BPF_ALU64 | BPF_LSH | BPF_K:
11468c2ecf20Sopenharmony_ci		emit_alu_K(SLLX, dst, imm, ctx);
11478c2ecf20Sopenharmony_ci		break;
11488c2ecf20Sopenharmony_ci	case BPF_ALU | BPF_RSH | BPF_K:
11498c2ecf20Sopenharmony_ci		emit_alu_K(SRL, dst, imm, ctx);
11508c2ecf20Sopenharmony_ci		if (insn_is_zext(&insn[1]))
11518c2ecf20Sopenharmony_ci			return 1;
11528c2ecf20Sopenharmony_ci		break;
11538c2ecf20Sopenharmony_ci	case BPF_ALU64 | BPF_RSH | BPF_K:
11548c2ecf20Sopenharmony_ci		emit_alu_K(SRLX, dst, imm, ctx);
11558c2ecf20Sopenharmony_ci		break;
11568c2ecf20Sopenharmony_ci	case BPF_ALU | BPF_ARSH | BPF_K:
11578c2ecf20Sopenharmony_ci		emit_alu_K(SRA, dst, imm, ctx);
11588c2ecf20Sopenharmony_ci		goto do_alu32_trunc;
11598c2ecf20Sopenharmony_ci	case BPF_ALU64 | BPF_ARSH | BPF_K:
11608c2ecf20Sopenharmony_ci		emit_alu_K(SRAX, dst, imm, ctx);
11618c2ecf20Sopenharmony_ci		break;
11628c2ecf20Sopenharmony_ci
11638c2ecf20Sopenharmony_ci	do_alu32_trunc:
11648c2ecf20Sopenharmony_ci		if (BPF_CLASS(code) == BPF_ALU &&
11658c2ecf20Sopenharmony_ci		    !ctx->prog->aux->verifier_zext)
11668c2ecf20Sopenharmony_ci			emit_alu_K(SRL, dst, 0, ctx);
11678c2ecf20Sopenharmony_ci		break;
11688c2ecf20Sopenharmony_ci
11698c2ecf20Sopenharmony_ci	/* JUMP off */
11708c2ecf20Sopenharmony_ci	case BPF_JMP | BPF_JA:
11718c2ecf20Sopenharmony_ci		emit_branch(BA, ctx->idx, ctx->offset[i + off], ctx);
11728c2ecf20Sopenharmony_ci		emit_nop(ctx);
11738c2ecf20Sopenharmony_ci		break;
11748c2ecf20Sopenharmony_ci	/* IF (dst COND src) JUMP off */
11758c2ecf20Sopenharmony_ci	case BPF_JMP | BPF_JEQ | BPF_X:
11768c2ecf20Sopenharmony_ci	case BPF_JMP | BPF_JGT | BPF_X:
11778c2ecf20Sopenharmony_ci	case BPF_JMP | BPF_JLT | BPF_X:
11788c2ecf20Sopenharmony_ci	case BPF_JMP | BPF_JGE | BPF_X:
11798c2ecf20Sopenharmony_ci	case BPF_JMP | BPF_JLE | BPF_X:
11808c2ecf20Sopenharmony_ci	case BPF_JMP | BPF_JNE | BPF_X:
11818c2ecf20Sopenharmony_ci	case BPF_JMP | BPF_JSGT | BPF_X:
11828c2ecf20Sopenharmony_ci	case BPF_JMP | BPF_JSLT | BPF_X:
11838c2ecf20Sopenharmony_ci	case BPF_JMP | BPF_JSGE | BPF_X:
11848c2ecf20Sopenharmony_ci	case BPF_JMP | BPF_JSLE | BPF_X:
11858c2ecf20Sopenharmony_ci	case BPF_JMP | BPF_JSET | BPF_X: {
11868c2ecf20Sopenharmony_ci		int err;
11878c2ecf20Sopenharmony_ci
11888c2ecf20Sopenharmony_ci		err = emit_compare_and_branch(code, dst, src, 0, false, i + off, ctx);
11898c2ecf20Sopenharmony_ci		if (err)
11908c2ecf20Sopenharmony_ci			return err;
11918c2ecf20Sopenharmony_ci		break;
11928c2ecf20Sopenharmony_ci	}
11938c2ecf20Sopenharmony_ci	/* IF (dst COND imm) JUMP off */
11948c2ecf20Sopenharmony_ci	case BPF_JMP | BPF_JEQ | BPF_K:
11958c2ecf20Sopenharmony_ci	case BPF_JMP | BPF_JGT | BPF_K:
11968c2ecf20Sopenharmony_ci	case BPF_JMP | BPF_JLT | BPF_K:
11978c2ecf20Sopenharmony_ci	case BPF_JMP | BPF_JGE | BPF_K:
11988c2ecf20Sopenharmony_ci	case BPF_JMP | BPF_JLE | BPF_K:
11998c2ecf20Sopenharmony_ci	case BPF_JMP | BPF_JNE | BPF_K:
12008c2ecf20Sopenharmony_ci	case BPF_JMP | BPF_JSGT | BPF_K:
12018c2ecf20Sopenharmony_ci	case BPF_JMP | BPF_JSLT | BPF_K:
12028c2ecf20Sopenharmony_ci	case BPF_JMP | BPF_JSGE | BPF_K:
12038c2ecf20Sopenharmony_ci	case BPF_JMP | BPF_JSLE | BPF_K:
12048c2ecf20Sopenharmony_ci	case BPF_JMP | BPF_JSET | BPF_K: {
12058c2ecf20Sopenharmony_ci		int err;
12068c2ecf20Sopenharmony_ci
12078c2ecf20Sopenharmony_ci		err = emit_compare_and_branch(code, dst, 0, imm, true, i + off, ctx);
12088c2ecf20Sopenharmony_ci		if (err)
12098c2ecf20Sopenharmony_ci			return err;
12108c2ecf20Sopenharmony_ci		break;
12118c2ecf20Sopenharmony_ci	}
12128c2ecf20Sopenharmony_ci
12138c2ecf20Sopenharmony_ci	/* function call */
12148c2ecf20Sopenharmony_ci	case BPF_JMP | BPF_CALL:
12158c2ecf20Sopenharmony_ci	{
12168c2ecf20Sopenharmony_ci		u8 *func = ((u8 *)__bpf_call_base) + imm;
12178c2ecf20Sopenharmony_ci
12188c2ecf20Sopenharmony_ci		ctx->saw_call = true;
12198c2ecf20Sopenharmony_ci
12208c2ecf20Sopenharmony_ci		emit_call((u32 *)func, ctx);
12218c2ecf20Sopenharmony_ci		emit_nop(ctx);
12228c2ecf20Sopenharmony_ci
12238c2ecf20Sopenharmony_ci		emit_reg_move(O0, bpf2sparc[BPF_REG_0], ctx);
12248c2ecf20Sopenharmony_ci		break;
12258c2ecf20Sopenharmony_ci	}
12268c2ecf20Sopenharmony_ci
12278c2ecf20Sopenharmony_ci	/* tail call */
12288c2ecf20Sopenharmony_ci	case BPF_JMP | BPF_TAIL_CALL:
12298c2ecf20Sopenharmony_ci		emit_tail_call(ctx);
12308c2ecf20Sopenharmony_ci		break;
12318c2ecf20Sopenharmony_ci
12328c2ecf20Sopenharmony_ci	/* function return */
12338c2ecf20Sopenharmony_ci	case BPF_JMP | BPF_EXIT:
12348c2ecf20Sopenharmony_ci		/* Optimization: when last instruction is EXIT,
12358c2ecf20Sopenharmony_ci		   simply fallthrough to epilogue. */
12368c2ecf20Sopenharmony_ci		if (i == ctx->prog->len - 1)
12378c2ecf20Sopenharmony_ci			break;
12388c2ecf20Sopenharmony_ci		emit_branch(BA, ctx->idx, ctx->epilogue_offset, ctx);
12398c2ecf20Sopenharmony_ci		emit_nop(ctx);
12408c2ecf20Sopenharmony_ci		break;
12418c2ecf20Sopenharmony_ci
12428c2ecf20Sopenharmony_ci	/* dst = imm64 */
12438c2ecf20Sopenharmony_ci	case BPF_LD | BPF_IMM | BPF_DW:
12448c2ecf20Sopenharmony_ci	{
12458c2ecf20Sopenharmony_ci		const struct bpf_insn insn1 = insn[1];
12468c2ecf20Sopenharmony_ci		u64 imm64;
12478c2ecf20Sopenharmony_ci
12488c2ecf20Sopenharmony_ci		imm64 = (u64)insn1.imm << 32 | (u32)imm;
12498c2ecf20Sopenharmony_ci		emit_loadimm64(imm64, dst, ctx);
12508c2ecf20Sopenharmony_ci
12518c2ecf20Sopenharmony_ci		return 1;
12528c2ecf20Sopenharmony_ci	}
12538c2ecf20Sopenharmony_ci
12548c2ecf20Sopenharmony_ci	/* LDX: dst = *(size *)(src + off) */
12558c2ecf20Sopenharmony_ci	case BPF_LDX | BPF_MEM | BPF_W:
12568c2ecf20Sopenharmony_ci	case BPF_LDX | BPF_MEM | BPF_H:
12578c2ecf20Sopenharmony_ci	case BPF_LDX | BPF_MEM | BPF_B:
12588c2ecf20Sopenharmony_ci	case BPF_LDX | BPF_MEM | BPF_DW: {
12598c2ecf20Sopenharmony_ci		const u8 tmp = bpf2sparc[TMP_REG_1];
12608c2ecf20Sopenharmony_ci		u32 opcode = 0, rs2;
12618c2ecf20Sopenharmony_ci
12628c2ecf20Sopenharmony_ci		ctx->tmp_1_used = true;
12638c2ecf20Sopenharmony_ci		switch (BPF_SIZE(code)) {
12648c2ecf20Sopenharmony_ci		case BPF_W:
12658c2ecf20Sopenharmony_ci			opcode = LD32;
12668c2ecf20Sopenharmony_ci			break;
12678c2ecf20Sopenharmony_ci		case BPF_H:
12688c2ecf20Sopenharmony_ci			opcode = LD16;
12698c2ecf20Sopenharmony_ci			break;
12708c2ecf20Sopenharmony_ci		case BPF_B:
12718c2ecf20Sopenharmony_ci			opcode = LD8;
12728c2ecf20Sopenharmony_ci			break;
12738c2ecf20Sopenharmony_ci		case BPF_DW:
12748c2ecf20Sopenharmony_ci			opcode = LD64;
12758c2ecf20Sopenharmony_ci			break;
12768c2ecf20Sopenharmony_ci		}
12778c2ecf20Sopenharmony_ci
12788c2ecf20Sopenharmony_ci		if (is_simm13(off)) {
12798c2ecf20Sopenharmony_ci			opcode |= IMMED;
12808c2ecf20Sopenharmony_ci			rs2 = S13(off);
12818c2ecf20Sopenharmony_ci		} else {
12828c2ecf20Sopenharmony_ci			emit_loadimm(off, tmp, ctx);
12838c2ecf20Sopenharmony_ci			rs2 = RS2(tmp);
12848c2ecf20Sopenharmony_ci		}
12858c2ecf20Sopenharmony_ci		emit(opcode | RS1(src) | rs2 | RD(dst), ctx);
12868c2ecf20Sopenharmony_ci		if (opcode != LD64 && insn_is_zext(&insn[1]))
12878c2ecf20Sopenharmony_ci			return 1;
12888c2ecf20Sopenharmony_ci		break;
12898c2ecf20Sopenharmony_ci	}
12908c2ecf20Sopenharmony_ci	/* speculation barrier */
12918c2ecf20Sopenharmony_ci	case BPF_ST | BPF_NOSPEC:
12928c2ecf20Sopenharmony_ci		break;
12938c2ecf20Sopenharmony_ci	/* ST: *(size *)(dst + off) = imm */
12948c2ecf20Sopenharmony_ci	case BPF_ST | BPF_MEM | BPF_W:
12958c2ecf20Sopenharmony_ci	case BPF_ST | BPF_MEM | BPF_H:
12968c2ecf20Sopenharmony_ci	case BPF_ST | BPF_MEM | BPF_B:
12978c2ecf20Sopenharmony_ci	case BPF_ST | BPF_MEM | BPF_DW: {
12988c2ecf20Sopenharmony_ci		const u8 tmp = bpf2sparc[TMP_REG_1];
12998c2ecf20Sopenharmony_ci		const u8 tmp2 = bpf2sparc[TMP_REG_2];
13008c2ecf20Sopenharmony_ci		u32 opcode = 0, rs2;
13018c2ecf20Sopenharmony_ci
13028c2ecf20Sopenharmony_ci		if (insn->dst_reg == BPF_REG_FP)
13038c2ecf20Sopenharmony_ci			ctx->saw_frame_pointer = true;
13048c2ecf20Sopenharmony_ci
13058c2ecf20Sopenharmony_ci		ctx->tmp_2_used = true;
13068c2ecf20Sopenharmony_ci		emit_loadimm(imm, tmp2, ctx);
13078c2ecf20Sopenharmony_ci
13088c2ecf20Sopenharmony_ci		switch (BPF_SIZE(code)) {
13098c2ecf20Sopenharmony_ci		case BPF_W:
13108c2ecf20Sopenharmony_ci			opcode = ST32;
13118c2ecf20Sopenharmony_ci			break;
13128c2ecf20Sopenharmony_ci		case BPF_H:
13138c2ecf20Sopenharmony_ci			opcode = ST16;
13148c2ecf20Sopenharmony_ci			break;
13158c2ecf20Sopenharmony_ci		case BPF_B:
13168c2ecf20Sopenharmony_ci			opcode = ST8;
13178c2ecf20Sopenharmony_ci			break;
13188c2ecf20Sopenharmony_ci		case BPF_DW:
13198c2ecf20Sopenharmony_ci			opcode = ST64;
13208c2ecf20Sopenharmony_ci			break;
13218c2ecf20Sopenharmony_ci		}
13228c2ecf20Sopenharmony_ci
13238c2ecf20Sopenharmony_ci		if (is_simm13(off)) {
13248c2ecf20Sopenharmony_ci			opcode |= IMMED;
13258c2ecf20Sopenharmony_ci			rs2 = S13(off);
13268c2ecf20Sopenharmony_ci		} else {
13278c2ecf20Sopenharmony_ci			ctx->tmp_1_used = true;
13288c2ecf20Sopenharmony_ci			emit_loadimm(off, tmp, ctx);
13298c2ecf20Sopenharmony_ci			rs2 = RS2(tmp);
13308c2ecf20Sopenharmony_ci		}
13318c2ecf20Sopenharmony_ci		emit(opcode | RS1(dst) | rs2 | RD(tmp2), ctx);
13328c2ecf20Sopenharmony_ci		break;
13338c2ecf20Sopenharmony_ci	}
13348c2ecf20Sopenharmony_ci
13358c2ecf20Sopenharmony_ci	/* STX: *(size *)(dst + off) = src */
13368c2ecf20Sopenharmony_ci	case BPF_STX | BPF_MEM | BPF_W:
13378c2ecf20Sopenharmony_ci	case BPF_STX | BPF_MEM | BPF_H:
13388c2ecf20Sopenharmony_ci	case BPF_STX | BPF_MEM | BPF_B:
13398c2ecf20Sopenharmony_ci	case BPF_STX | BPF_MEM | BPF_DW: {
13408c2ecf20Sopenharmony_ci		const u8 tmp = bpf2sparc[TMP_REG_1];
13418c2ecf20Sopenharmony_ci		u32 opcode = 0, rs2;
13428c2ecf20Sopenharmony_ci
13438c2ecf20Sopenharmony_ci		if (insn->dst_reg == BPF_REG_FP)
13448c2ecf20Sopenharmony_ci			ctx->saw_frame_pointer = true;
13458c2ecf20Sopenharmony_ci
13468c2ecf20Sopenharmony_ci		switch (BPF_SIZE(code)) {
13478c2ecf20Sopenharmony_ci		case BPF_W:
13488c2ecf20Sopenharmony_ci			opcode = ST32;
13498c2ecf20Sopenharmony_ci			break;
13508c2ecf20Sopenharmony_ci		case BPF_H:
13518c2ecf20Sopenharmony_ci			opcode = ST16;
13528c2ecf20Sopenharmony_ci			break;
13538c2ecf20Sopenharmony_ci		case BPF_B:
13548c2ecf20Sopenharmony_ci			opcode = ST8;
13558c2ecf20Sopenharmony_ci			break;
13568c2ecf20Sopenharmony_ci		case BPF_DW:
13578c2ecf20Sopenharmony_ci			opcode = ST64;
13588c2ecf20Sopenharmony_ci			break;
13598c2ecf20Sopenharmony_ci		}
13608c2ecf20Sopenharmony_ci		if (is_simm13(off)) {
13618c2ecf20Sopenharmony_ci			opcode |= IMMED;
13628c2ecf20Sopenharmony_ci			rs2 = S13(off);
13638c2ecf20Sopenharmony_ci		} else {
13648c2ecf20Sopenharmony_ci			ctx->tmp_1_used = true;
13658c2ecf20Sopenharmony_ci			emit_loadimm(off, tmp, ctx);
13668c2ecf20Sopenharmony_ci			rs2 = RS2(tmp);
13678c2ecf20Sopenharmony_ci		}
13688c2ecf20Sopenharmony_ci		emit(opcode | RS1(dst) | rs2 | RD(src), ctx);
13698c2ecf20Sopenharmony_ci		break;
13708c2ecf20Sopenharmony_ci	}
13718c2ecf20Sopenharmony_ci
13728c2ecf20Sopenharmony_ci	/* STX XADD: lock *(u32 *)(dst + off) += src */
13738c2ecf20Sopenharmony_ci	case BPF_STX | BPF_XADD | BPF_W: {
13748c2ecf20Sopenharmony_ci		const u8 tmp = bpf2sparc[TMP_REG_1];
13758c2ecf20Sopenharmony_ci		const u8 tmp2 = bpf2sparc[TMP_REG_2];
13768c2ecf20Sopenharmony_ci		const u8 tmp3 = bpf2sparc[TMP_REG_3];
13778c2ecf20Sopenharmony_ci
13788c2ecf20Sopenharmony_ci		if (insn->dst_reg == BPF_REG_FP)
13798c2ecf20Sopenharmony_ci			ctx->saw_frame_pointer = true;
13808c2ecf20Sopenharmony_ci
13818c2ecf20Sopenharmony_ci		ctx->tmp_1_used = true;
13828c2ecf20Sopenharmony_ci		ctx->tmp_2_used = true;
13838c2ecf20Sopenharmony_ci		ctx->tmp_3_used = true;
13848c2ecf20Sopenharmony_ci		emit_loadimm(off, tmp, ctx);
13858c2ecf20Sopenharmony_ci		emit_alu3(ADD, dst, tmp, tmp, ctx);
13868c2ecf20Sopenharmony_ci
13878c2ecf20Sopenharmony_ci		emit(LD32 | RS1(tmp) | RS2(G0) | RD(tmp2), ctx);
13888c2ecf20Sopenharmony_ci		emit_alu3(ADD, tmp2, src, tmp3, ctx);
13898c2ecf20Sopenharmony_ci		emit(CAS | ASI(ASI_P) | RS1(tmp) | RS2(tmp2) | RD(tmp3), ctx);
13908c2ecf20Sopenharmony_ci		emit_cmp(tmp2, tmp3, ctx);
13918c2ecf20Sopenharmony_ci		emit_branch(BNE, 4, 0, ctx);
13928c2ecf20Sopenharmony_ci		emit_nop(ctx);
13938c2ecf20Sopenharmony_ci		break;
13948c2ecf20Sopenharmony_ci	}
13958c2ecf20Sopenharmony_ci	/* STX XADD: lock *(u64 *)(dst + off) += src */
13968c2ecf20Sopenharmony_ci	case BPF_STX | BPF_XADD | BPF_DW: {
13978c2ecf20Sopenharmony_ci		const u8 tmp = bpf2sparc[TMP_REG_1];
13988c2ecf20Sopenharmony_ci		const u8 tmp2 = bpf2sparc[TMP_REG_2];
13998c2ecf20Sopenharmony_ci		const u8 tmp3 = bpf2sparc[TMP_REG_3];
14008c2ecf20Sopenharmony_ci
14018c2ecf20Sopenharmony_ci		if (insn->dst_reg == BPF_REG_FP)
14028c2ecf20Sopenharmony_ci			ctx->saw_frame_pointer = true;
14038c2ecf20Sopenharmony_ci
14048c2ecf20Sopenharmony_ci		ctx->tmp_1_used = true;
14058c2ecf20Sopenharmony_ci		ctx->tmp_2_used = true;
14068c2ecf20Sopenharmony_ci		ctx->tmp_3_used = true;
14078c2ecf20Sopenharmony_ci		emit_loadimm(off, tmp, ctx);
14088c2ecf20Sopenharmony_ci		emit_alu3(ADD, dst, tmp, tmp, ctx);
14098c2ecf20Sopenharmony_ci
14108c2ecf20Sopenharmony_ci		emit(LD64 | RS1(tmp) | RS2(G0) | RD(tmp2), ctx);
14118c2ecf20Sopenharmony_ci		emit_alu3(ADD, tmp2, src, tmp3, ctx);
14128c2ecf20Sopenharmony_ci		emit(CASX | ASI(ASI_P) | RS1(tmp) | RS2(tmp2) | RD(tmp3), ctx);
14138c2ecf20Sopenharmony_ci		emit_cmp(tmp2, tmp3, ctx);
14148c2ecf20Sopenharmony_ci		emit_branch(BNE, 4, 0, ctx);
14158c2ecf20Sopenharmony_ci		emit_nop(ctx);
14168c2ecf20Sopenharmony_ci		break;
14178c2ecf20Sopenharmony_ci	}
14188c2ecf20Sopenharmony_ci
14198c2ecf20Sopenharmony_ci	default:
14208c2ecf20Sopenharmony_ci		pr_err_once("unknown opcode %02x\n", code);
14218c2ecf20Sopenharmony_ci		return -EINVAL;
14228c2ecf20Sopenharmony_ci	}
14238c2ecf20Sopenharmony_ci
14248c2ecf20Sopenharmony_ci	return 0;
14258c2ecf20Sopenharmony_ci}
14268c2ecf20Sopenharmony_ci
14278c2ecf20Sopenharmony_cistatic int build_body(struct jit_ctx *ctx)
14288c2ecf20Sopenharmony_ci{
14298c2ecf20Sopenharmony_ci	const struct bpf_prog *prog = ctx->prog;
14308c2ecf20Sopenharmony_ci	int i;
14318c2ecf20Sopenharmony_ci
14328c2ecf20Sopenharmony_ci	for (i = 0; i < prog->len; i++) {
14338c2ecf20Sopenharmony_ci		const struct bpf_insn *insn = &prog->insnsi[i];
14348c2ecf20Sopenharmony_ci		int ret;
14358c2ecf20Sopenharmony_ci
14368c2ecf20Sopenharmony_ci		ret = build_insn(insn, ctx);
14378c2ecf20Sopenharmony_ci
14388c2ecf20Sopenharmony_ci		if (ret > 0) {
14398c2ecf20Sopenharmony_ci			i++;
14408c2ecf20Sopenharmony_ci			ctx->offset[i] = ctx->idx;
14418c2ecf20Sopenharmony_ci			continue;
14428c2ecf20Sopenharmony_ci		}
14438c2ecf20Sopenharmony_ci		ctx->offset[i] = ctx->idx;
14448c2ecf20Sopenharmony_ci		if (ret)
14458c2ecf20Sopenharmony_ci			return ret;
14468c2ecf20Sopenharmony_ci	}
14478c2ecf20Sopenharmony_ci	return 0;
14488c2ecf20Sopenharmony_ci}
14498c2ecf20Sopenharmony_ci
14508c2ecf20Sopenharmony_cistatic void jit_fill_hole(void *area, unsigned int size)
14518c2ecf20Sopenharmony_ci{
14528c2ecf20Sopenharmony_ci	u32 *ptr;
14538c2ecf20Sopenharmony_ci	/* We are guaranteed to have aligned memory. */
14548c2ecf20Sopenharmony_ci	for (ptr = area; size >= sizeof(u32); size -= sizeof(u32))
14558c2ecf20Sopenharmony_ci		*ptr++ = 0x91d02005; /* ta 5 */
14568c2ecf20Sopenharmony_ci}
14578c2ecf20Sopenharmony_ci
14588c2ecf20Sopenharmony_cibool bpf_jit_needs_zext(void)
14598c2ecf20Sopenharmony_ci{
14608c2ecf20Sopenharmony_ci	return true;
14618c2ecf20Sopenharmony_ci}
14628c2ecf20Sopenharmony_ci
14638c2ecf20Sopenharmony_cistruct sparc64_jit_data {
14648c2ecf20Sopenharmony_ci	struct bpf_binary_header *header;
14658c2ecf20Sopenharmony_ci	u8 *image;
14668c2ecf20Sopenharmony_ci	struct jit_ctx ctx;
14678c2ecf20Sopenharmony_ci};
14688c2ecf20Sopenharmony_ci
14698c2ecf20Sopenharmony_cistruct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
14708c2ecf20Sopenharmony_ci{
14718c2ecf20Sopenharmony_ci	struct bpf_prog *tmp, *orig_prog = prog;
14728c2ecf20Sopenharmony_ci	struct sparc64_jit_data *jit_data;
14738c2ecf20Sopenharmony_ci	struct bpf_binary_header *header;
14748c2ecf20Sopenharmony_ci	u32 prev_image_size, image_size;
14758c2ecf20Sopenharmony_ci	bool tmp_blinded = false;
14768c2ecf20Sopenharmony_ci	bool extra_pass = false;
14778c2ecf20Sopenharmony_ci	struct jit_ctx ctx;
14788c2ecf20Sopenharmony_ci	u8 *image_ptr;
14798c2ecf20Sopenharmony_ci	int pass, i;
14808c2ecf20Sopenharmony_ci
14818c2ecf20Sopenharmony_ci	if (!prog->jit_requested)
14828c2ecf20Sopenharmony_ci		return orig_prog;
14838c2ecf20Sopenharmony_ci
14848c2ecf20Sopenharmony_ci	tmp = bpf_jit_blind_constants(prog);
14858c2ecf20Sopenharmony_ci	/* If blinding was requested and we failed during blinding,
14868c2ecf20Sopenharmony_ci	 * we must fall back to the interpreter.
14878c2ecf20Sopenharmony_ci	 */
14888c2ecf20Sopenharmony_ci	if (IS_ERR(tmp))
14898c2ecf20Sopenharmony_ci		return orig_prog;
14908c2ecf20Sopenharmony_ci	if (tmp != prog) {
14918c2ecf20Sopenharmony_ci		tmp_blinded = true;
14928c2ecf20Sopenharmony_ci		prog = tmp;
14938c2ecf20Sopenharmony_ci	}
14948c2ecf20Sopenharmony_ci
14958c2ecf20Sopenharmony_ci	jit_data = prog->aux->jit_data;
14968c2ecf20Sopenharmony_ci	if (!jit_data) {
14978c2ecf20Sopenharmony_ci		jit_data = kzalloc(sizeof(*jit_data), GFP_KERNEL);
14988c2ecf20Sopenharmony_ci		if (!jit_data) {
14998c2ecf20Sopenharmony_ci			prog = orig_prog;
15008c2ecf20Sopenharmony_ci			goto out;
15018c2ecf20Sopenharmony_ci		}
15028c2ecf20Sopenharmony_ci		prog->aux->jit_data = jit_data;
15038c2ecf20Sopenharmony_ci	}
15048c2ecf20Sopenharmony_ci	if (jit_data->ctx.offset) {
15058c2ecf20Sopenharmony_ci		ctx = jit_data->ctx;
15068c2ecf20Sopenharmony_ci		image_ptr = jit_data->image;
15078c2ecf20Sopenharmony_ci		header = jit_data->header;
15088c2ecf20Sopenharmony_ci		extra_pass = true;
15098c2ecf20Sopenharmony_ci		image_size = sizeof(u32) * ctx.idx;
15108c2ecf20Sopenharmony_ci		prev_image_size = image_size;
15118c2ecf20Sopenharmony_ci		pass = 1;
15128c2ecf20Sopenharmony_ci		goto skip_init_ctx;
15138c2ecf20Sopenharmony_ci	}
15148c2ecf20Sopenharmony_ci
15158c2ecf20Sopenharmony_ci	memset(&ctx, 0, sizeof(ctx));
15168c2ecf20Sopenharmony_ci	ctx.prog = prog;
15178c2ecf20Sopenharmony_ci
15188c2ecf20Sopenharmony_ci	ctx.offset = kmalloc_array(prog->len, sizeof(unsigned int), GFP_KERNEL);
15198c2ecf20Sopenharmony_ci	if (ctx.offset == NULL) {
15208c2ecf20Sopenharmony_ci		prog = orig_prog;
15218c2ecf20Sopenharmony_ci		goto out_off;
15228c2ecf20Sopenharmony_ci	}
15238c2ecf20Sopenharmony_ci
15248c2ecf20Sopenharmony_ci	/* Longest sequence emitted is for bswap32, 12 instructions.  Pre-cook
15258c2ecf20Sopenharmony_ci	 * the offset array so that we converge faster.
15268c2ecf20Sopenharmony_ci	 */
15278c2ecf20Sopenharmony_ci	for (i = 0; i < prog->len; i++)
15288c2ecf20Sopenharmony_ci		ctx.offset[i] = i * (12 * 4);
15298c2ecf20Sopenharmony_ci
15308c2ecf20Sopenharmony_ci	prev_image_size = ~0U;
15318c2ecf20Sopenharmony_ci	for (pass = 1; pass < 40; pass++) {
15328c2ecf20Sopenharmony_ci		ctx.idx = 0;
15338c2ecf20Sopenharmony_ci
15348c2ecf20Sopenharmony_ci		build_prologue(&ctx);
15358c2ecf20Sopenharmony_ci		if (build_body(&ctx)) {
15368c2ecf20Sopenharmony_ci			prog = orig_prog;
15378c2ecf20Sopenharmony_ci			goto out_off;
15388c2ecf20Sopenharmony_ci		}
15398c2ecf20Sopenharmony_ci		build_epilogue(&ctx);
15408c2ecf20Sopenharmony_ci
15418c2ecf20Sopenharmony_ci		if (bpf_jit_enable > 1)
15428c2ecf20Sopenharmony_ci			pr_info("Pass %d: size = %u, seen = [%c%c%c%c%c%c]\n", pass,
15438c2ecf20Sopenharmony_ci				ctx.idx * 4,
15448c2ecf20Sopenharmony_ci				ctx.tmp_1_used ? '1' : ' ',
15458c2ecf20Sopenharmony_ci				ctx.tmp_2_used ? '2' : ' ',
15468c2ecf20Sopenharmony_ci				ctx.tmp_3_used ? '3' : ' ',
15478c2ecf20Sopenharmony_ci				ctx.saw_frame_pointer ? 'F' : ' ',
15488c2ecf20Sopenharmony_ci				ctx.saw_call ? 'C' : ' ',
15498c2ecf20Sopenharmony_ci				ctx.saw_tail_call ? 'T' : ' ');
15508c2ecf20Sopenharmony_ci
15518c2ecf20Sopenharmony_ci		if (ctx.idx * 4 == prev_image_size)
15528c2ecf20Sopenharmony_ci			break;
15538c2ecf20Sopenharmony_ci		prev_image_size = ctx.idx * 4;
15548c2ecf20Sopenharmony_ci		cond_resched();
15558c2ecf20Sopenharmony_ci	}
15568c2ecf20Sopenharmony_ci
15578c2ecf20Sopenharmony_ci	/* Now we know the actual image size. */
15588c2ecf20Sopenharmony_ci	image_size = sizeof(u32) * ctx.idx;
15598c2ecf20Sopenharmony_ci	header = bpf_jit_binary_alloc(image_size, &image_ptr,
15608c2ecf20Sopenharmony_ci				      sizeof(u32), jit_fill_hole);
15618c2ecf20Sopenharmony_ci	if (header == NULL) {
15628c2ecf20Sopenharmony_ci		prog = orig_prog;
15638c2ecf20Sopenharmony_ci		goto out_off;
15648c2ecf20Sopenharmony_ci	}
15658c2ecf20Sopenharmony_ci
15668c2ecf20Sopenharmony_ci	ctx.image = (u32 *)image_ptr;
15678c2ecf20Sopenharmony_ciskip_init_ctx:
15688c2ecf20Sopenharmony_ci	ctx.idx = 0;
15698c2ecf20Sopenharmony_ci
15708c2ecf20Sopenharmony_ci	build_prologue(&ctx);
15718c2ecf20Sopenharmony_ci
15728c2ecf20Sopenharmony_ci	if (build_body(&ctx)) {
15738c2ecf20Sopenharmony_ci		bpf_jit_binary_free(header);
15748c2ecf20Sopenharmony_ci		prog = orig_prog;
15758c2ecf20Sopenharmony_ci		goto out_off;
15768c2ecf20Sopenharmony_ci	}
15778c2ecf20Sopenharmony_ci
15788c2ecf20Sopenharmony_ci	build_epilogue(&ctx);
15798c2ecf20Sopenharmony_ci
15808c2ecf20Sopenharmony_ci	if (ctx.idx * 4 != prev_image_size) {
15818c2ecf20Sopenharmony_ci		pr_err("bpf_jit: Failed to converge, prev_size=%u size=%d\n",
15828c2ecf20Sopenharmony_ci		       prev_image_size, ctx.idx * 4);
15838c2ecf20Sopenharmony_ci		bpf_jit_binary_free(header);
15848c2ecf20Sopenharmony_ci		prog = orig_prog;
15858c2ecf20Sopenharmony_ci		goto out_off;
15868c2ecf20Sopenharmony_ci	}
15878c2ecf20Sopenharmony_ci
15888c2ecf20Sopenharmony_ci	if (bpf_jit_enable > 1)
15898c2ecf20Sopenharmony_ci		bpf_jit_dump(prog->len, image_size, pass, ctx.image);
15908c2ecf20Sopenharmony_ci
15918c2ecf20Sopenharmony_ci	bpf_flush_icache(header, (u8 *)header + (header->pages * PAGE_SIZE));
15928c2ecf20Sopenharmony_ci
15938c2ecf20Sopenharmony_ci	if (!prog->is_func || extra_pass) {
15948c2ecf20Sopenharmony_ci		bpf_jit_binary_lock_ro(header);
15958c2ecf20Sopenharmony_ci	} else {
15968c2ecf20Sopenharmony_ci		jit_data->ctx = ctx;
15978c2ecf20Sopenharmony_ci		jit_data->image = image_ptr;
15988c2ecf20Sopenharmony_ci		jit_data->header = header;
15998c2ecf20Sopenharmony_ci	}
16008c2ecf20Sopenharmony_ci
16018c2ecf20Sopenharmony_ci	prog->bpf_func = (void *)ctx.image;
16028c2ecf20Sopenharmony_ci	prog->jited = 1;
16038c2ecf20Sopenharmony_ci	prog->jited_len = image_size;
16048c2ecf20Sopenharmony_ci
16058c2ecf20Sopenharmony_ci	if (!prog->is_func || extra_pass) {
16068c2ecf20Sopenharmony_ci		bpf_prog_fill_jited_linfo(prog, ctx.offset);
16078c2ecf20Sopenharmony_ciout_off:
16088c2ecf20Sopenharmony_ci		kfree(ctx.offset);
16098c2ecf20Sopenharmony_ci		kfree(jit_data);
16108c2ecf20Sopenharmony_ci		prog->aux->jit_data = NULL;
16118c2ecf20Sopenharmony_ci	}
16128c2ecf20Sopenharmony_ciout:
16138c2ecf20Sopenharmony_ci	if (tmp_blinded)
16148c2ecf20Sopenharmony_ci		bpf_jit_prog_release_other(prog, prog == orig_prog ?
16158c2ecf20Sopenharmony_ci					   tmp : orig_prog);
16168c2ecf20Sopenharmony_ci	return prog;
16178c2ecf20Sopenharmony_ci}
1618