18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * BPF JIT compiler for RV32G
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2020 Luke Nelson <luke.r.nels@gmail.com>
68c2ecf20Sopenharmony_ci * Copyright (c) 2020 Xi Wang <xi.wang@gmail.com>
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * The code is based on the BPF JIT compiler for RV64G by Björn Töpel and
98c2ecf20Sopenharmony_ci * the BPF JIT compiler for 32-bit ARM by Shubham Bansal and Mircea Gherzan.
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include <linux/bpf.h>
138c2ecf20Sopenharmony_ci#include <linux/filter.h>
148c2ecf20Sopenharmony_ci#include "bpf_jit.h"
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci/*
178c2ecf20Sopenharmony_ci * Stack layout during BPF program execution:
188c2ecf20Sopenharmony_ci *
198c2ecf20Sopenharmony_ci *                     high
208c2ecf20Sopenharmony_ci *     RV32 fp =>  +----------+
218c2ecf20Sopenharmony_ci *                 | saved ra |
228c2ecf20Sopenharmony_ci *                 | saved fp | RV32 callee-saved registers
238c2ecf20Sopenharmony_ci *                 |   ...    |
248c2ecf20Sopenharmony_ci *                 +----------+ <= (fp - 4 * NR_SAVED_REGISTERS)
258c2ecf20Sopenharmony_ci *                 |  hi(R6)  |
268c2ecf20Sopenharmony_ci *                 |  lo(R6)  |
278c2ecf20Sopenharmony_ci *                 |  hi(R7)  | JIT scratch space for BPF registers
288c2ecf20Sopenharmony_ci *                 |  lo(R7)  |
298c2ecf20Sopenharmony_ci *                 |   ...    |
308c2ecf20Sopenharmony_ci *  BPF_REG_FP =>  +----------+ <= (fp - 4 * NR_SAVED_REGISTERS
318c2ecf20Sopenharmony_ci *                 |          |        - 4 * BPF_JIT_SCRATCH_REGS)
328c2ecf20Sopenharmony_ci *                 |          |
338c2ecf20Sopenharmony_ci *                 |   ...    | BPF program stack
348c2ecf20Sopenharmony_ci *                 |          |
358c2ecf20Sopenharmony_ci *     RV32 sp =>  +----------+
368c2ecf20Sopenharmony_ci *                 |          |
378c2ecf20Sopenharmony_ci *                 |   ...    | Function call stack
388c2ecf20Sopenharmony_ci *                 |          |
398c2ecf20Sopenharmony_ci *                 +----------+
408c2ecf20Sopenharmony_ci *                     low
418c2ecf20Sopenharmony_ci */
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_cienum {
448c2ecf20Sopenharmony_ci	/* Stack layout - these are offsets from top of JIT scratch space. */
458c2ecf20Sopenharmony_ci	BPF_R6_HI,
468c2ecf20Sopenharmony_ci	BPF_R6_LO,
478c2ecf20Sopenharmony_ci	BPF_R7_HI,
488c2ecf20Sopenharmony_ci	BPF_R7_LO,
498c2ecf20Sopenharmony_ci	BPF_R8_HI,
508c2ecf20Sopenharmony_ci	BPF_R8_LO,
518c2ecf20Sopenharmony_ci	BPF_R9_HI,
528c2ecf20Sopenharmony_ci	BPF_R9_LO,
538c2ecf20Sopenharmony_ci	BPF_AX_HI,
548c2ecf20Sopenharmony_ci	BPF_AX_LO,
558c2ecf20Sopenharmony_ci	/* Stack space for BPF_REG_6 through BPF_REG_9 and BPF_REG_AX. */
568c2ecf20Sopenharmony_ci	BPF_JIT_SCRATCH_REGS,
578c2ecf20Sopenharmony_ci};
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci/* Number of callee-saved registers stored to stack: ra, fp, s1--s7. */
608c2ecf20Sopenharmony_ci#define NR_SAVED_REGISTERS	9
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci/* Offset from fp for BPF registers stored on stack. */
638c2ecf20Sopenharmony_ci#define STACK_OFFSET(k)	(-4 - (4 * NR_SAVED_REGISTERS) - (4 * (k)))
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci#define TMP_REG_1	(MAX_BPF_JIT_REG + 0)
668c2ecf20Sopenharmony_ci#define TMP_REG_2	(MAX_BPF_JIT_REG + 1)
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci#define RV_REG_TCC		RV_REG_T6
698c2ecf20Sopenharmony_ci#define RV_REG_TCC_SAVED	RV_REG_S7
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_cistatic const s8 bpf2rv32[][2] = {
728c2ecf20Sopenharmony_ci	/* Return value from in-kernel function, and exit value from eBPF. */
738c2ecf20Sopenharmony_ci	[BPF_REG_0] = {RV_REG_S2, RV_REG_S1},
748c2ecf20Sopenharmony_ci	/* Arguments from eBPF program to in-kernel function. */
758c2ecf20Sopenharmony_ci	[BPF_REG_1] = {RV_REG_A1, RV_REG_A0},
768c2ecf20Sopenharmony_ci	[BPF_REG_2] = {RV_REG_A3, RV_REG_A2},
778c2ecf20Sopenharmony_ci	[BPF_REG_3] = {RV_REG_A5, RV_REG_A4},
788c2ecf20Sopenharmony_ci	[BPF_REG_4] = {RV_REG_A7, RV_REG_A6},
798c2ecf20Sopenharmony_ci	[BPF_REG_5] = {RV_REG_S4, RV_REG_S3},
808c2ecf20Sopenharmony_ci	/*
818c2ecf20Sopenharmony_ci	 * Callee-saved registers that in-kernel function will preserve.
828c2ecf20Sopenharmony_ci	 * Stored on the stack.
838c2ecf20Sopenharmony_ci	 */
848c2ecf20Sopenharmony_ci	[BPF_REG_6] = {STACK_OFFSET(BPF_R6_HI), STACK_OFFSET(BPF_R6_LO)},
858c2ecf20Sopenharmony_ci	[BPF_REG_7] = {STACK_OFFSET(BPF_R7_HI), STACK_OFFSET(BPF_R7_LO)},
868c2ecf20Sopenharmony_ci	[BPF_REG_8] = {STACK_OFFSET(BPF_R8_HI), STACK_OFFSET(BPF_R8_LO)},
878c2ecf20Sopenharmony_ci	[BPF_REG_9] = {STACK_OFFSET(BPF_R9_HI), STACK_OFFSET(BPF_R9_LO)},
888c2ecf20Sopenharmony_ci	/* Read-only frame pointer to access BPF stack. */
898c2ecf20Sopenharmony_ci	[BPF_REG_FP] = {RV_REG_S6, RV_REG_S5},
908c2ecf20Sopenharmony_ci	/* Temporary register for blinding constants. Stored on the stack. */
918c2ecf20Sopenharmony_ci	[BPF_REG_AX] = {STACK_OFFSET(BPF_AX_HI), STACK_OFFSET(BPF_AX_LO)},
928c2ecf20Sopenharmony_ci	/*
938c2ecf20Sopenharmony_ci	 * Temporary registers used by the JIT to operate on registers stored
948c2ecf20Sopenharmony_ci	 * on the stack. Save t0 and t1 to be used as temporaries in generated
958c2ecf20Sopenharmony_ci	 * code.
968c2ecf20Sopenharmony_ci	 */
978c2ecf20Sopenharmony_ci	[TMP_REG_1] = {RV_REG_T3, RV_REG_T2},
988c2ecf20Sopenharmony_ci	[TMP_REG_2] = {RV_REG_T5, RV_REG_T4},
998c2ecf20Sopenharmony_ci};
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_cistatic s8 hi(const s8 *r)
1028c2ecf20Sopenharmony_ci{
1038c2ecf20Sopenharmony_ci	return r[0];
1048c2ecf20Sopenharmony_ci}
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_cistatic s8 lo(const s8 *r)
1078c2ecf20Sopenharmony_ci{
1088c2ecf20Sopenharmony_ci	return r[1];
1098c2ecf20Sopenharmony_ci}
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_cistatic void emit_imm(const s8 rd, s32 imm, struct rv_jit_context *ctx)
1128c2ecf20Sopenharmony_ci{
1138c2ecf20Sopenharmony_ci	u32 upper = (imm + (1 << 11)) >> 12;
1148c2ecf20Sopenharmony_ci	u32 lower = imm & 0xfff;
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	if (upper) {
1178c2ecf20Sopenharmony_ci		emit(rv_lui(rd, upper), ctx);
1188c2ecf20Sopenharmony_ci		emit(rv_addi(rd, rd, lower), ctx);
1198c2ecf20Sopenharmony_ci	} else {
1208c2ecf20Sopenharmony_ci		emit(rv_addi(rd, RV_REG_ZERO, lower), ctx);
1218c2ecf20Sopenharmony_ci	}
1228c2ecf20Sopenharmony_ci}
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_cistatic void emit_imm32(const s8 *rd, s32 imm, struct rv_jit_context *ctx)
1258c2ecf20Sopenharmony_ci{
1268c2ecf20Sopenharmony_ci	/* Emit immediate into lower bits. */
1278c2ecf20Sopenharmony_ci	emit_imm(lo(rd), imm, ctx);
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	/* Sign-extend into upper bits. */
1308c2ecf20Sopenharmony_ci	if (imm >= 0)
1318c2ecf20Sopenharmony_ci		emit(rv_addi(hi(rd), RV_REG_ZERO, 0), ctx);
1328c2ecf20Sopenharmony_ci	else
1338c2ecf20Sopenharmony_ci		emit(rv_addi(hi(rd), RV_REG_ZERO, -1), ctx);
1348c2ecf20Sopenharmony_ci}
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_cistatic void emit_imm64(const s8 *rd, s32 imm_hi, s32 imm_lo,
1378c2ecf20Sopenharmony_ci		       struct rv_jit_context *ctx)
1388c2ecf20Sopenharmony_ci{
1398c2ecf20Sopenharmony_ci	emit_imm(lo(rd), imm_lo, ctx);
1408c2ecf20Sopenharmony_ci	emit_imm(hi(rd), imm_hi, ctx);
1418c2ecf20Sopenharmony_ci}
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_cistatic void __build_epilogue(bool is_tail_call, struct rv_jit_context *ctx)
1448c2ecf20Sopenharmony_ci{
1458c2ecf20Sopenharmony_ci	int stack_adjust = ctx->stack_size;
1468c2ecf20Sopenharmony_ci	const s8 *r0 = bpf2rv32[BPF_REG_0];
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	/* Set return value if not tail call. */
1498c2ecf20Sopenharmony_ci	if (!is_tail_call) {
1508c2ecf20Sopenharmony_ci		emit(rv_addi(RV_REG_A0, lo(r0), 0), ctx);
1518c2ecf20Sopenharmony_ci		emit(rv_addi(RV_REG_A1, hi(r0), 0), ctx);
1528c2ecf20Sopenharmony_ci	}
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	/* Restore callee-saved registers. */
1558c2ecf20Sopenharmony_ci	emit(rv_lw(RV_REG_RA, stack_adjust - 4, RV_REG_SP), ctx);
1568c2ecf20Sopenharmony_ci	emit(rv_lw(RV_REG_FP, stack_adjust - 8, RV_REG_SP), ctx);
1578c2ecf20Sopenharmony_ci	emit(rv_lw(RV_REG_S1, stack_adjust - 12, RV_REG_SP), ctx);
1588c2ecf20Sopenharmony_ci	emit(rv_lw(RV_REG_S2, stack_adjust - 16, RV_REG_SP), ctx);
1598c2ecf20Sopenharmony_ci	emit(rv_lw(RV_REG_S3, stack_adjust - 20, RV_REG_SP), ctx);
1608c2ecf20Sopenharmony_ci	emit(rv_lw(RV_REG_S4, stack_adjust - 24, RV_REG_SP), ctx);
1618c2ecf20Sopenharmony_ci	emit(rv_lw(RV_REG_S5, stack_adjust - 28, RV_REG_SP), ctx);
1628c2ecf20Sopenharmony_ci	emit(rv_lw(RV_REG_S6, stack_adjust - 32, RV_REG_SP), ctx);
1638c2ecf20Sopenharmony_ci	emit(rv_lw(RV_REG_S7, stack_adjust - 36, RV_REG_SP), ctx);
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	emit(rv_addi(RV_REG_SP, RV_REG_SP, stack_adjust), ctx);
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	if (is_tail_call) {
1688c2ecf20Sopenharmony_ci		/*
1698c2ecf20Sopenharmony_ci		 * goto *(t0 + 4);
1708c2ecf20Sopenharmony_ci		 * Skips first instruction of prologue which initializes tail
1718c2ecf20Sopenharmony_ci		 * call counter. Assumes t0 contains address of target program,
1728c2ecf20Sopenharmony_ci		 * see emit_bpf_tail_call.
1738c2ecf20Sopenharmony_ci		 */
1748c2ecf20Sopenharmony_ci		emit(rv_jalr(RV_REG_ZERO, RV_REG_T0, 4), ctx);
1758c2ecf20Sopenharmony_ci	} else {
1768c2ecf20Sopenharmony_ci		emit(rv_jalr(RV_REG_ZERO, RV_REG_RA, 0), ctx);
1778c2ecf20Sopenharmony_ci	}
1788c2ecf20Sopenharmony_ci}
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_cistatic bool is_stacked(s8 reg)
1818c2ecf20Sopenharmony_ci{
1828c2ecf20Sopenharmony_ci	return reg < 0;
1838c2ecf20Sopenharmony_ci}
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_cistatic const s8 *bpf_get_reg64(const s8 *reg, const s8 *tmp,
1868c2ecf20Sopenharmony_ci			       struct rv_jit_context *ctx)
1878c2ecf20Sopenharmony_ci{
1888c2ecf20Sopenharmony_ci	if (is_stacked(hi(reg))) {
1898c2ecf20Sopenharmony_ci		emit(rv_lw(hi(tmp), hi(reg), RV_REG_FP), ctx);
1908c2ecf20Sopenharmony_ci		emit(rv_lw(lo(tmp), lo(reg), RV_REG_FP), ctx);
1918c2ecf20Sopenharmony_ci		reg = tmp;
1928c2ecf20Sopenharmony_ci	}
1938c2ecf20Sopenharmony_ci	return reg;
1948c2ecf20Sopenharmony_ci}
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_cistatic void bpf_put_reg64(const s8 *reg, const s8 *src,
1978c2ecf20Sopenharmony_ci			  struct rv_jit_context *ctx)
1988c2ecf20Sopenharmony_ci{
1998c2ecf20Sopenharmony_ci	if (is_stacked(hi(reg))) {
2008c2ecf20Sopenharmony_ci		emit(rv_sw(RV_REG_FP, hi(reg), hi(src)), ctx);
2018c2ecf20Sopenharmony_ci		emit(rv_sw(RV_REG_FP, lo(reg), lo(src)), ctx);
2028c2ecf20Sopenharmony_ci	}
2038c2ecf20Sopenharmony_ci}
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_cistatic const s8 *bpf_get_reg32(const s8 *reg, const s8 *tmp,
2068c2ecf20Sopenharmony_ci			       struct rv_jit_context *ctx)
2078c2ecf20Sopenharmony_ci{
2088c2ecf20Sopenharmony_ci	if (is_stacked(lo(reg))) {
2098c2ecf20Sopenharmony_ci		emit(rv_lw(lo(tmp), lo(reg), RV_REG_FP), ctx);
2108c2ecf20Sopenharmony_ci		reg = tmp;
2118c2ecf20Sopenharmony_ci	}
2128c2ecf20Sopenharmony_ci	return reg;
2138c2ecf20Sopenharmony_ci}
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_cistatic void bpf_put_reg32(const s8 *reg, const s8 *src,
2168c2ecf20Sopenharmony_ci			  struct rv_jit_context *ctx)
2178c2ecf20Sopenharmony_ci{
2188c2ecf20Sopenharmony_ci	if (is_stacked(lo(reg))) {
2198c2ecf20Sopenharmony_ci		emit(rv_sw(RV_REG_FP, lo(reg), lo(src)), ctx);
2208c2ecf20Sopenharmony_ci		if (!ctx->prog->aux->verifier_zext)
2218c2ecf20Sopenharmony_ci			emit(rv_sw(RV_REG_FP, hi(reg), RV_REG_ZERO), ctx);
2228c2ecf20Sopenharmony_ci	} else if (!ctx->prog->aux->verifier_zext) {
2238c2ecf20Sopenharmony_ci		emit(rv_addi(hi(reg), RV_REG_ZERO, 0), ctx);
2248c2ecf20Sopenharmony_ci	}
2258c2ecf20Sopenharmony_ci}
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_cistatic void emit_jump_and_link(u8 rd, s32 rvoff, bool force_jalr,
2288c2ecf20Sopenharmony_ci			       struct rv_jit_context *ctx)
2298c2ecf20Sopenharmony_ci{
2308c2ecf20Sopenharmony_ci	s32 upper, lower;
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci	if (rvoff && is_21b_int(rvoff) && !force_jalr) {
2338c2ecf20Sopenharmony_ci		emit(rv_jal(rd, rvoff >> 1), ctx);
2348c2ecf20Sopenharmony_ci		return;
2358c2ecf20Sopenharmony_ci	}
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci	upper = (rvoff + (1 << 11)) >> 12;
2388c2ecf20Sopenharmony_ci	lower = rvoff & 0xfff;
2398c2ecf20Sopenharmony_ci	emit(rv_auipc(RV_REG_T1, upper), ctx);
2408c2ecf20Sopenharmony_ci	emit(rv_jalr(rd, RV_REG_T1, lower), ctx);
2418c2ecf20Sopenharmony_ci}
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_cistatic void emit_alu_i64(const s8 *dst, s32 imm,
2448c2ecf20Sopenharmony_ci			 struct rv_jit_context *ctx, const u8 op)
2458c2ecf20Sopenharmony_ci{
2468c2ecf20Sopenharmony_ci	const s8 *tmp1 = bpf2rv32[TMP_REG_1];
2478c2ecf20Sopenharmony_ci	const s8 *rd = bpf_get_reg64(dst, tmp1, ctx);
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci	switch (op) {
2508c2ecf20Sopenharmony_ci	case BPF_MOV:
2518c2ecf20Sopenharmony_ci		emit_imm32(rd, imm, ctx);
2528c2ecf20Sopenharmony_ci		break;
2538c2ecf20Sopenharmony_ci	case BPF_AND:
2548c2ecf20Sopenharmony_ci		if (is_12b_int(imm)) {
2558c2ecf20Sopenharmony_ci			emit(rv_andi(lo(rd), lo(rd), imm), ctx);
2568c2ecf20Sopenharmony_ci		} else {
2578c2ecf20Sopenharmony_ci			emit_imm(RV_REG_T0, imm, ctx);
2588c2ecf20Sopenharmony_ci			emit(rv_and(lo(rd), lo(rd), RV_REG_T0), ctx);
2598c2ecf20Sopenharmony_ci		}
2608c2ecf20Sopenharmony_ci		if (imm >= 0)
2618c2ecf20Sopenharmony_ci			emit(rv_addi(hi(rd), RV_REG_ZERO, 0), ctx);
2628c2ecf20Sopenharmony_ci		break;
2638c2ecf20Sopenharmony_ci	case BPF_OR:
2648c2ecf20Sopenharmony_ci		if (is_12b_int(imm)) {
2658c2ecf20Sopenharmony_ci			emit(rv_ori(lo(rd), lo(rd), imm), ctx);
2668c2ecf20Sopenharmony_ci		} else {
2678c2ecf20Sopenharmony_ci			emit_imm(RV_REG_T0, imm, ctx);
2688c2ecf20Sopenharmony_ci			emit(rv_or(lo(rd), lo(rd), RV_REG_T0), ctx);
2698c2ecf20Sopenharmony_ci		}
2708c2ecf20Sopenharmony_ci		if (imm < 0)
2718c2ecf20Sopenharmony_ci			emit(rv_ori(hi(rd), RV_REG_ZERO, -1), ctx);
2728c2ecf20Sopenharmony_ci		break;
2738c2ecf20Sopenharmony_ci	case BPF_XOR:
2748c2ecf20Sopenharmony_ci		if (is_12b_int(imm)) {
2758c2ecf20Sopenharmony_ci			emit(rv_xori(lo(rd), lo(rd), imm), ctx);
2768c2ecf20Sopenharmony_ci		} else {
2778c2ecf20Sopenharmony_ci			emit_imm(RV_REG_T0, imm, ctx);
2788c2ecf20Sopenharmony_ci			emit(rv_xor(lo(rd), lo(rd), RV_REG_T0), ctx);
2798c2ecf20Sopenharmony_ci		}
2808c2ecf20Sopenharmony_ci		if (imm < 0)
2818c2ecf20Sopenharmony_ci			emit(rv_xori(hi(rd), hi(rd), -1), ctx);
2828c2ecf20Sopenharmony_ci		break;
2838c2ecf20Sopenharmony_ci	case BPF_LSH:
2848c2ecf20Sopenharmony_ci		if (imm >= 32) {
2858c2ecf20Sopenharmony_ci			emit(rv_slli(hi(rd), lo(rd), imm - 32), ctx);
2868c2ecf20Sopenharmony_ci			emit(rv_addi(lo(rd), RV_REG_ZERO, 0), ctx);
2878c2ecf20Sopenharmony_ci		} else if (imm == 0) {
2888c2ecf20Sopenharmony_ci			/* Do nothing. */
2898c2ecf20Sopenharmony_ci		} else {
2908c2ecf20Sopenharmony_ci			emit(rv_srli(RV_REG_T0, lo(rd), 32 - imm), ctx);
2918c2ecf20Sopenharmony_ci			emit(rv_slli(hi(rd), hi(rd), imm), ctx);
2928c2ecf20Sopenharmony_ci			emit(rv_or(hi(rd), RV_REG_T0, hi(rd)), ctx);
2938c2ecf20Sopenharmony_ci			emit(rv_slli(lo(rd), lo(rd), imm), ctx);
2948c2ecf20Sopenharmony_ci		}
2958c2ecf20Sopenharmony_ci		break;
2968c2ecf20Sopenharmony_ci	case BPF_RSH:
2978c2ecf20Sopenharmony_ci		if (imm >= 32) {
2988c2ecf20Sopenharmony_ci			emit(rv_srli(lo(rd), hi(rd), imm - 32), ctx);
2998c2ecf20Sopenharmony_ci			emit(rv_addi(hi(rd), RV_REG_ZERO, 0), ctx);
3008c2ecf20Sopenharmony_ci		} else if (imm == 0) {
3018c2ecf20Sopenharmony_ci			/* Do nothing. */
3028c2ecf20Sopenharmony_ci		} else {
3038c2ecf20Sopenharmony_ci			emit(rv_slli(RV_REG_T0, hi(rd), 32 - imm), ctx);
3048c2ecf20Sopenharmony_ci			emit(rv_srli(lo(rd), lo(rd), imm), ctx);
3058c2ecf20Sopenharmony_ci			emit(rv_or(lo(rd), RV_REG_T0, lo(rd)), ctx);
3068c2ecf20Sopenharmony_ci			emit(rv_srli(hi(rd), hi(rd), imm), ctx);
3078c2ecf20Sopenharmony_ci		}
3088c2ecf20Sopenharmony_ci		break;
3098c2ecf20Sopenharmony_ci	case BPF_ARSH:
3108c2ecf20Sopenharmony_ci		if (imm >= 32) {
3118c2ecf20Sopenharmony_ci			emit(rv_srai(lo(rd), hi(rd), imm - 32), ctx);
3128c2ecf20Sopenharmony_ci			emit(rv_srai(hi(rd), hi(rd), 31), ctx);
3138c2ecf20Sopenharmony_ci		} else if (imm == 0) {
3148c2ecf20Sopenharmony_ci			/* Do nothing. */
3158c2ecf20Sopenharmony_ci		} else {
3168c2ecf20Sopenharmony_ci			emit(rv_slli(RV_REG_T0, hi(rd), 32 - imm), ctx);
3178c2ecf20Sopenharmony_ci			emit(rv_srli(lo(rd), lo(rd), imm), ctx);
3188c2ecf20Sopenharmony_ci			emit(rv_or(lo(rd), RV_REG_T0, lo(rd)), ctx);
3198c2ecf20Sopenharmony_ci			emit(rv_srai(hi(rd), hi(rd), imm), ctx);
3208c2ecf20Sopenharmony_ci		}
3218c2ecf20Sopenharmony_ci		break;
3228c2ecf20Sopenharmony_ci	}
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci	bpf_put_reg64(dst, rd, ctx);
3258c2ecf20Sopenharmony_ci}
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_cistatic void emit_alu_i32(const s8 *dst, s32 imm,
3288c2ecf20Sopenharmony_ci			 struct rv_jit_context *ctx, const u8 op)
3298c2ecf20Sopenharmony_ci{
3308c2ecf20Sopenharmony_ci	const s8 *tmp1 = bpf2rv32[TMP_REG_1];
3318c2ecf20Sopenharmony_ci	const s8 *rd = bpf_get_reg32(dst, tmp1, ctx);
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci	switch (op) {
3348c2ecf20Sopenharmony_ci	case BPF_MOV:
3358c2ecf20Sopenharmony_ci		emit_imm(lo(rd), imm, ctx);
3368c2ecf20Sopenharmony_ci		break;
3378c2ecf20Sopenharmony_ci	case BPF_ADD:
3388c2ecf20Sopenharmony_ci		if (is_12b_int(imm)) {
3398c2ecf20Sopenharmony_ci			emit(rv_addi(lo(rd), lo(rd), imm), ctx);
3408c2ecf20Sopenharmony_ci		} else {
3418c2ecf20Sopenharmony_ci			emit_imm(RV_REG_T0, imm, ctx);
3428c2ecf20Sopenharmony_ci			emit(rv_add(lo(rd), lo(rd), RV_REG_T0), ctx);
3438c2ecf20Sopenharmony_ci		}
3448c2ecf20Sopenharmony_ci		break;
3458c2ecf20Sopenharmony_ci	case BPF_SUB:
3468c2ecf20Sopenharmony_ci		if (is_12b_int(-imm)) {
3478c2ecf20Sopenharmony_ci			emit(rv_addi(lo(rd), lo(rd), -imm), ctx);
3488c2ecf20Sopenharmony_ci		} else {
3498c2ecf20Sopenharmony_ci			emit_imm(RV_REG_T0, imm, ctx);
3508c2ecf20Sopenharmony_ci			emit(rv_sub(lo(rd), lo(rd), RV_REG_T0), ctx);
3518c2ecf20Sopenharmony_ci		}
3528c2ecf20Sopenharmony_ci		break;
3538c2ecf20Sopenharmony_ci	case BPF_AND:
3548c2ecf20Sopenharmony_ci		if (is_12b_int(imm)) {
3558c2ecf20Sopenharmony_ci			emit(rv_andi(lo(rd), lo(rd), imm), ctx);
3568c2ecf20Sopenharmony_ci		} else {
3578c2ecf20Sopenharmony_ci			emit_imm(RV_REG_T0, imm, ctx);
3588c2ecf20Sopenharmony_ci			emit(rv_and(lo(rd), lo(rd), RV_REG_T0), ctx);
3598c2ecf20Sopenharmony_ci		}
3608c2ecf20Sopenharmony_ci		break;
3618c2ecf20Sopenharmony_ci	case BPF_OR:
3628c2ecf20Sopenharmony_ci		if (is_12b_int(imm)) {
3638c2ecf20Sopenharmony_ci			emit(rv_ori(lo(rd), lo(rd), imm), ctx);
3648c2ecf20Sopenharmony_ci		} else {
3658c2ecf20Sopenharmony_ci			emit_imm(RV_REG_T0, imm, ctx);
3668c2ecf20Sopenharmony_ci			emit(rv_or(lo(rd), lo(rd), RV_REG_T0), ctx);
3678c2ecf20Sopenharmony_ci		}
3688c2ecf20Sopenharmony_ci		break;
3698c2ecf20Sopenharmony_ci	case BPF_XOR:
3708c2ecf20Sopenharmony_ci		if (is_12b_int(imm)) {
3718c2ecf20Sopenharmony_ci			emit(rv_xori(lo(rd), lo(rd), imm), ctx);
3728c2ecf20Sopenharmony_ci		} else {
3738c2ecf20Sopenharmony_ci			emit_imm(RV_REG_T0, imm, ctx);
3748c2ecf20Sopenharmony_ci			emit(rv_xor(lo(rd), lo(rd), RV_REG_T0), ctx);
3758c2ecf20Sopenharmony_ci		}
3768c2ecf20Sopenharmony_ci		break;
3778c2ecf20Sopenharmony_ci	case BPF_LSH:
3788c2ecf20Sopenharmony_ci		if (is_12b_int(imm)) {
3798c2ecf20Sopenharmony_ci			emit(rv_slli(lo(rd), lo(rd), imm), ctx);
3808c2ecf20Sopenharmony_ci		} else {
3818c2ecf20Sopenharmony_ci			emit_imm(RV_REG_T0, imm, ctx);
3828c2ecf20Sopenharmony_ci			emit(rv_sll(lo(rd), lo(rd), RV_REG_T0), ctx);
3838c2ecf20Sopenharmony_ci		}
3848c2ecf20Sopenharmony_ci		break;
3858c2ecf20Sopenharmony_ci	case BPF_RSH:
3868c2ecf20Sopenharmony_ci		if (is_12b_int(imm)) {
3878c2ecf20Sopenharmony_ci			emit(rv_srli(lo(rd), lo(rd), imm), ctx);
3888c2ecf20Sopenharmony_ci		} else {
3898c2ecf20Sopenharmony_ci			emit_imm(RV_REG_T0, imm, ctx);
3908c2ecf20Sopenharmony_ci			emit(rv_srl(lo(rd), lo(rd), RV_REG_T0), ctx);
3918c2ecf20Sopenharmony_ci		}
3928c2ecf20Sopenharmony_ci		break;
3938c2ecf20Sopenharmony_ci	case BPF_ARSH:
3948c2ecf20Sopenharmony_ci		if (is_12b_int(imm)) {
3958c2ecf20Sopenharmony_ci			emit(rv_srai(lo(rd), lo(rd), imm), ctx);
3968c2ecf20Sopenharmony_ci		} else {
3978c2ecf20Sopenharmony_ci			emit_imm(RV_REG_T0, imm, ctx);
3988c2ecf20Sopenharmony_ci			emit(rv_sra(lo(rd), lo(rd), RV_REG_T0), ctx);
3998c2ecf20Sopenharmony_ci		}
4008c2ecf20Sopenharmony_ci		break;
4018c2ecf20Sopenharmony_ci	}
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_ci	bpf_put_reg32(dst, rd, ctx);
4048c2ecf20Sopenharmony_ci}
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_cistatic void emit_alu_r64(const s8 *dst, const s8 *src,
4078c2ecf20Sopenharmony_ci			 struct rv_jit_context *ctx, const u8 op)
4088c2ecf20Sopenharmony_ci{
4098c2ecf20Sopenharmony_ci	const s8 *tmp1 = bpf2rv32[TMP_REG_1];
4108c2ecf20Sopenharmony_ci	const s8 *tmp2 = bpf2rv32[TMP_REG_2];
4118c2ecf20Sopenharmony_ci	const s8 *rd = bpf_get_reg64(dst, tmp1, ctx);
4128c2ecf20Sopenharmony_ci	const s8 *rs = bpf_get_reg64(src, tmp2, ctx);
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_ci	switch (op) {
4158c2ecf20Sopenharmony_ci	case BPF_MOV:
4168c2ecf20Sopenharmony_ci		emit(rv_addi(lo(rd), lo(rs), 0), ctx);
4178c2ecf20Sopenharmony_ci		emit(rv_addi(hi(rd), hi(rs), 0), ctx);
4188c2ecf20Sopenharmony_ci		break;
4198c2ecf20Sopenharmony_ci	case BPF_ADD:
4208c2ecf20Sopenharmony_ci		if (rd == rs) {
4218c2ecf20Sopenharmony_ci			emit(rv_srli(RV_REG_T0, lo(rd), 31), ctx);
4228c2ecf20Sopenharmony_ci			emit(rv_slli(hi(rd), hi(rd), 1), ctx);
4238c2ecf20Sopenharmony_ci			emit(rv_or(hi(rd), RV_REG_T0, hi(rd)), ctx);
4248c2ecf20Sopenharmony_ci			emit(rv_slli(lo(rd), lo(rd), 1), ctx);
4258c2ecf20Sopenharmony_ci		} else {
4268c2ecf20Sopenharmony_ci			emit(rv_add(lo(rd), lo(rd), lo(rs)), ctx);
4278c2ecf20Sopenharmony_ci			emit(rv_sltu(RV_REG_T0, lo(rd), lo(rs)), ctx);
4288c2ecf20Sopenharmony_ci			emit(rv_add(hi(rd), hi(rd), hi(rs)), ctx);
4298c2ecf20Sopenharmony_ci			emit(rv_add(hi(rd), hi(rd), RV_REG_T0), ctx);
4308c2ecf20Sopenharmony_ci		}
4318c2ecf20Sopenharmony_ci		break;
4328c2ecf20Sopenharmony_ci	case BPF_SUB:
4338c2ecf20Sopenharmony_ci		emit(rv_sub(RV_REG_T1, hi(rd), hi(rs)), ctx);
4348c2ecf20Sopenharmony_ci		emit(rv_sltu(RV_REG_T0, lo(rd), lo(rs)), ctx);
4358c2ecf20Sopenharmony_ci		emit(rv_sub(hi(rd), RV_REG_T1, RV_REG_T0), ctx);
4368c2ecf20Sopenharmony_ci		emit(rv_sub(lo(rd), lo(rd), lo(rs)), ctx);
4378c2ecf20Sopenharmony_ci		break;
4388c2ecf20Sopenharmony_ci	case BPF_AND:
4398c2ecf20Sopenharmony_ci		emit(rv_and(lo(rd), lo(rd), lo(rs)), ctx);
4408c2ecf20Sopenharmony_ci		emit(rv_and(hi(rd), hi(rd), hi(rs)), ctx);
4418c2ecf20Sopenharmony_ci		break;
4428c2ecf20Sopenharmony_ci	case BPF_OR:
4438c2ecf20Sopenharmony_ci		emit(rv_or(lo(rd), lo(rd), lo(rs)), ctx);
4448c2ecf20Sopenharmony_ci		emit(rv_or(hi(rd), hi(rd), hi(rs)), ctx);
4458c2ecf20Sopenharmony_ci		break;
4468c2ecf20Sopenharmony_ci	case BPF_XOR:
4478c2ecf20Sopenharmony_ci		emit(rv_xor(lo(rd), lo(rd), lo(rs)), ctx);
4488c2ecf20Sopenharmony_ci		emit(rv_xor(hi(rd), hi(rd), hi(rs)), ctx);
4498c2ecf20Sopenharmony_ci		break;
4508c2ecf20Sopenharmony_ci	case BPF_MUL:
4518c2ecf20Sopenharmony_ci		emit(rv_mul(RV_REG_T0, hi(rs), lo(rd)), ctx);
4528c2ecf20Sopenharmony_ci		emit(rv_mul(hi(rd), hi(rd), lo(rs)), ctx);
4538c2ecf20Sopenharmony_ci		emit(rv_mulhu(RV_REG_T1, lo(rd), lo(rs)), ctx);
4548c2ecf20Sopenharmony_ci		emit(rv_add(hi(rd), hi(rd), RV_REG_T0), ctx);
4558c2ecf20Sopenharmony_ci		emit(rv_mul(lo(rd), lo(rd), lo(rs)), ctx);
4568c2ecf20Sopenharmony_ci		emit(rv_add(hi(rd), hi(rd), RV_REG_T1), ctx);
4578c2ecf20Sopenharmony_ci		break;
4588c2ecf20Sopenharmony_ci	case BPF_LSH:
4598c2ecf20Sopenharmony_ci		emit(rv_addi(RV_REG_T0, lo(rs), -32), ctx);
4608c2ecf20Sopenharmony_ci		emit(rv_blt(RV_REG_T0, RV_REG_ZERO, 8), ctx);
4618c2ecf20Sopenharmony_ci		emit(rv_sll(hi(rd), lo(rd), RV_REG_T0), ctx);
4628c2ecf20Sopenharmony_ci		emit(rv_addi(lo(rd), RV_REG_ZERO, 0), ctx);
4638c2ecf20Sopenharmony_ci		emit(rv_jal(RV_REG_ZERO, 16), ctx);
4648c2ecf20Sopenharmony_ci		emit(rv_addi(RV_REG_T1, RV_REG_ZERO, 31), ctx);
4658c2ecf20Sopenharmony_ci		emit(rv_srli(RV_REG_T0, lo(rd), 1), ctx);
4668c2ecf20Sopenharmony_ci		emit(rv_sub(RV_REG_T1, RV_REG_T1, lo(rs)), ctx);
4678c2ecf20Sopenharmony_ci		emit(rv_srl(RV_REG_T0, RV_REG_T0, RV_REG_T1), ctx);
4688c2ecf20Sopenharmony_ci		emit(rv_sll(hi(rd), hi(rd), lo(rs)), ctx);
4698c2ecf20Sopenharmony_ci		emit(rv_or(hi(rd), RV_REG_T0, hi(rd)), ctx);
4708c2ecf20Sopenharmony_ci		emit(rv_sll(lo(rd), lo(rd), lo(rs)), ctx);
4718c2ecf20Sopenharmony_ci		break;
4728c2ecf20Sopenharmony_ci	case BPF_RSH:
4738c2ecf20Sopenharmony_ci		emit(rv_addi(RV_REG_T0, lo(rs), -32), ctx);
4748c2ecf20Sopenharmony_ci		emit(rv_blt(RV_REG_T0, RV_REG_ZERO, 8), ctx);
4758c2ecf20Sopenharmony_ci		emit(rv_srl(lo(rd), hi(rd), RV_REG_T0), ctx);
4768c2ecf20Sopenharmony_ci		emit(rv_addi(hi(rd), RV_REG_ZERO, 0), ctx);
4778c2ecf20Sopenharmony_ci		emit(rv_jal(RV_REG_ZERO, 16), ctx);
4788c2ecf20Sopenharmony_ci		emit(rv_addi(RV_REG_T1, RV_REG_ZERO, 31), ctx);
4798c2ecf20Sopenharmony_ci		emit(rv_slli(RV_REG_T0, hi(rd), 1), ctx);
4808c2ecf20Sopenharmony_ci		emit(rv_sub(RV_REG_T1, RV_REG_T1, lo(rs)), ctx);
4818c2ecf20Sopenharmony_ci		emit(rv_sll(RV_REG_T0, RV_REG_T0, RV_REG_T1), ctx);
4828c2ecf20Sopenharmony_ci		emit(rv_srl(lo(rd), lo(rd), lo(rs)), ctx);
4838c2ecf20Sopenharmony_ci		emit(rv_or(lo(rd), RV_REG_T0, lo(rd)), ctx);
4848c2ecf20Sopenharmony_ci		emit(rv_srl(hi(rd), hi(rd), lo(rs)), ctx);
4858c2ecf20Sopenharmony_ci		break;
4868c2ecf20Sopenharmony_ci	case BPF_ARSH:
4878c2ecf20Sopenharmony_ci		emit(rv_addi(RV_REG_T0, lo(rs), -32), ctx);
4888c2ecf20Sopenharmony_ci		emit(rv_blt(RV_REG_T0, RV_REG_ZERO, 8), ctx);
4898c2ecf20Sopenharmony_ci		emit(rv_sra(lo(rd), hi(rd), RV_REG_T0), ctx);
4908c2ecf20Sopenharmony_ci		emit(rv_srai(hi(rd), hi(rd), 31), ctx);
4918c2ecf20Sopenharmony_ci		emit(rv_jal(RV_REG_ZERO, 16), ctx);
4928c2ecf20Sopenharmony_ci		emit(rv_addi(RV_REG_T1, RV_REG_ZERO, 31), ctx);
4938c2ecf20Sopenharmony_ci		emit(rv_slli(RV_REG_T0, hi(rd), 1), ctx);
4948c2ecf20Sopenharmony_ci		emit(rv_sub(RV_REG_T1, RV_REG_T1, lo(rs)), ctx);
4958c2ecf20Sopenharmony_ci		emit(rv_sll(RV_REG_T0, RV_REG_T0, RV_REG_T1), ctx);
4968c2ecf20Sopenharmony_ci		emit(rv_srl(lo(rd), lo(rd), lo(rs)), ctx);
4978c2ecf20Sopenharmony_ci		emit(rv_or(lo(rd), RV_REG_T0, lo(rd)), ctx);
4988c2ecf20Sopenharmony_ci		emit(rv_sra(hi(rd), hi(rd), lo(rs)), ctx);
4998c2ecf20Sopenharmony_ci		break;
5008c2ecf20Sopenharmony_ci	case BPF_NEG:
5018c2ecf20Sopenharmony_ci		emit(rv_sub(lo(rd), RV_REG_ZERO, lo(rd)), ctx);
5028c2ecf20Sopenharmony_ci		emit(rv_sltu(RV_REG_T0, RV_REG_ZERO, lo(rd)), ctx);
5038c2ecf20Sopenharmony_ci		emit(rv_sub(hi(rd), RV_REG_ZERO, hi(rd)), ctx);
5048c2ecf20Sopenharmony_ci		emit(rv_sub(hi(rd), hi(rd), RV_REG_T0), ctx);
5058c2ecf20Sopenharmony_ci		break;
5068c2ecf20Sopenharmony_ci	}
5078c2ecf20Sopenharmony_ci
5088c2ecf20Sopenharmony_ci	bpf_put_reg64(dst, rd, ctx);
5098c2ecf20Sopenharmony_ci}
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_cistatic void emit_alu_r32(const s8 *dst, const s8 *src,
5128c2ecf20Sopenharmony_ci			 struct rv_jit_context *ctx, const u8 op)
5138c2ecf20Sopenharmony_ci{
5148c2ecf20Sopenharmony_ci	const s8 *tmp1 = bpf2rv32[TMP_REG_1];
5158c2ecf20Sopenharmony_ci	const s8 *tmp2 = bpf2rv32[TMP_REG_2];
5168c2ecf20Sopenharmony_ci	const s8 *rd = bpf_get_reg32(dst, tmp1, ctx);
5178c2ecf20Sopenharmony_ci	const s8 *rs = bpf_get_reg32(src, tmp2, ctx);
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_ci	switch (op) {
5208c2ecf20Sopenharmony_ci	case BPF_MOV:
5218c2ecf20Sopenharmony_ci		emit(rv_addi(lo(rd), lo(rs), 0), ctx);
5228c2ecf20Sopenharmony_ci		break;
5238c2ecf20Sopenharmony_ci	case BPF_ADD:
5248c2ecf20Sopenharmony_ci		emit(rv_add(lo(rd), lo(rd), lo(rs)), ctx);
5258c2ecf20Sopenharmony_ci		break;
5268c2ecf20Sopenharmony_ci	case BPF_SUB:
5278c2ecf20Sopenharmony_ci		emit(rv_sub(lo(rd), lo(rd), lo(rs)), ctx);
5288c2ecf20Sopenharmony_ci		break;
5298c2ecf20Sopenharmony_ci	case BPF_AND:
5308c2ecf20Sopenharmony_ci		emit(rv_and(lo(rd), lo(rd), lo(rs)), ctx);
5318c2ecf20Sopenharmony_ci		break;
5328c2ecf20Sopenharmony_ci	case BPF_OR:
5338c2ecf20Sopenharmony_ci		emit(rv_or(lo(rd), lo(rd), lo(rs)), ctx);
5348c2ecf20Sopenharmony_ci		break;
5358c2ecf20Sopenharmony_ci	case BPF_XOR:
5368c2ecf20Sopenharmony_ci		emit(rv_xor(lo(rd), lo(rd), lo(rs)), ctx);
5378c2ecf20Sopenharmony_ci		break;
5388c2ecf20Sopenharmony_ci	case BPF_MUL:
5398c2ecf20Sopenharmony_ci		emit(rv_mul(lo(rd), lo(rd), lo(rs)), ctx);
5408c2ecf20Sopenharmony_ci		break;
5418c2ecf20Sopenharmony_ci	case BPF_DIV:
5428c2ecf20Sopenharmony_ci		emit(rv_divu(lo(rd), lo(rd), lo(rs)), ctx);
5438c2ecf20Sopenharmony_ci		break;
5448c2ecf20Sopenharmony_ci	case BPF_MOD:
5458c2ecf20Sopenharmony_ci		emit(rv_remu(lo(rd), lo(rd), lo(rs)), ctx);
5468c2ecf20Sopenharmony_ci		break;
5478c2ecf20Sopenharmony_ci	case BPF_LSH:
5488c2ecf20Sopenharmony_ci		emit(rv_sll(lo(rd), lo(rd), lo(rs)), ctx);
5498c2ecf20Sopenharmony_ci		break;
5508c2ecf20Sopenharmony_ci	case BPF_RSH:
5518c2ecf20Sopenharmony_ci		emit(rv_srl(lo(rd), lo(rd), lo(rs)), ctx);
5528c2ecf20Sopenharmony_ci		break;
5538c2ecf20Sopenharmony_ci	case BPF_ARSH:
5548c2ecf20Sopenharmony_ci		emit(rv_sra(lo(rd), lo(rd), lo(rs)), ctx);
5558c2ecf20Sopenharmony_ci		break;
5568c2ecf20Sopenharmony_ci	case BPF_NEG:
5578c2ecf20Sopenharmony_ci		emit(rv_sub(lo(rd), RV_REG_ZERO, lo(rd)), ctx);
5588c2ecf20Sopenharmony_ci		break;
5598c2ecf20Sopenharmony_ci	}
5608c2ecf20Sopenharmony_ci
5618c2ecf20Sopenharmony_ci	bpf_put_reg32(dst, rd, ctx);
5628c2ecf20Sopenharmony_ci}
5638c2ecf20Sopenharmony_ci
5648c2ecf20Sopenharmony_cistatic int emit_branch_r64(const s8 *src1, const s8 *src2, s32 rvoff,
5658c2ecf20Sopenharmony_ci			   struct rv_jit_context *ctx, const u8 op)
5668c2ecf20Sopenharmony_ci{
5678c2ecf20Sopenharmony_ci	int e, s = ctx->ninsns;
5688c2ecf20Sopenharmony_ci	const s8 *tmp1 = bpf2rv32[TMP_REG_1];
5698c2ecf20Sopenharmony_ci	const s8 *tmp2 = bpf2rv32[TMP_REG_2];
5708c2ecf20Sopenharmony_ci
5718c2ecf20Sopenharmony_ci	const s8 *rs1 = bpf_get_reg64(src1, tmp1, ctx);
5728c2ecf20Sopenharmony_ci	const s8 *rs2 = bpf_get_reg64(src2, tmp2, ctx);
5738c2ecf20Sopenharmony_ci
5748c2ecf20Sopenharmony_ci	/*
5758c2ecf20Sopenharmony_ci	 * NO_JUMP skips over the rest of the instructions and the
5768c2ecf20Sopenharmony_ci	 * emit_jump_and_link, meaning the BPF branch is not taken.
5778c2ecf20Sopenharmony_ci	 * JUMP skips directly to the emit_jump_and_link, meaning
5788c2ecf20Sopenharmony_ci	 * the BPF branch is taken.
5798c2ecf20Sopenharmony_ci	 *
5808c2ecf20Sopenharmony_ci	 * The fallthrough case results in the BPF branch being taken.
5818c2ecf20Sopenharmony_ci	 */
5828c2ecf20Sopenharmony_ci#define NO_JUMP(idx) (6 + (2 * (idx)))
5838c2ecf20Sopenharmony_ci#define JUMP(idx) (2 + (2 * (idx)))
5848c2ecf20Sopenharmony_ci
5858c2ecf20Sopenharmony_ci	switch (op) {
5868c2ecf20Sopenharmony_ci	case BPF_JEQ:
5878c2ecf20Sopenharmony_ci		emit(rv_bne(hi(rs1), hi(rs2), NO_JUMP(1)), ctx);
5888c2ecf20Sopenharmony_ci		emit(rv_bne(lo(rs1), lo(rs2), NO_JUMP(0)), ctx);
5898c2ecf20Sopenharmony_ci		break;
5908c2ecf20Sopenharmony_ci	case BPF_JGT:
5918c2ecf20Sopenharmony_ci		emit(rv_bgtu(hi(rs1), hi(rs2), JUMP(2)), ctx);
5928c2ecf20Sopenharmony_ci		emit(rv_bltu(hi(rs1), hi(rs2), NO_JUMP(1)), ctx);
5938c2ecf20Sopenharmony_ci		emit(rv_bleu(lo(rs1), lo(rs2), NO_JUMP(0)), ctx);
5948c2ecf20Sopenharmony_ci		break;
5958c2ecf20Sopenharmony_ci	case BPF_JLT:
5968c2ecf20Sopenharmony_ci		emit(rv_bltu(hi(rs1), hi(rs2), JUMP(2)), ctx);
5978c2ecf20Sopenharmony_ci		emit(rv_bgtu(hi(rs1), hi(rs2), NO_JUMP(1)), ctx);
5988c2ecf20Sopenharmony_ci		emit(rv_bgeu(lo(rs1), lo(rs2), NO_JUMP(0)), ctx);
5998c2ecf20Sopenharmony_ci		break;
6008c2ecf20Sopenharmony_ci	case BPF_JGE:
6018c2ecf20Sopenharmony_ci		emit(rv_bgtu(hi(rs1), hi(rs2), JUMP(2)), ctx);
6028c2ecf20Sopenharmony_ci		emit(rv_bltu(hi(rs1), hi(rs2), NO_JUMP(1)), ctx);
6038c2ecf20Sopenharmony_ci		emit(rv_bltu(lo(rs1), lo(rs2), NO_JUMP(0)), ctx);
6048c2ecf20Sopenharmony_ci		break;
6058c2ecf20Sopenharmony_ci	case BPF_JLE:
6068c2ecf20Sopenharmony_ci		emit(rv_bltu(hi(rs1), hi(rs2), JUMP(2)), ctx);
6078c2ecf20Sopenharmony_ci		emit(rv_bgtu(hi(rs1), hi(rs2), NO_JUMP(1)), ctx);
6088c2ecf20Sopenharmony_ci		emit(rv_bgtu(lo(rs1), lo(rs2), NO_JUMP(0)), ctx);
6098c2ecf20Sopenharmony_ci		break;
6108c2ecf20Sopenharmony_ci	case BPF_JNE:
6118c2ecf20Sopenharmony_ci		emit(rv_bne(hi(rs1), hi(rs2), JUMP(1)), ctx);
6128c2ecf20Sopenharmony_ci		emit(rv_beq(lo(rs1), lo(rs2), NO_JUMP(0)), ctx);
6138c2ecf20Sopenharmony_ci		break;
6148c2ecf20Sopenharmony_ci	case BPF_JSGT:
6158c2ecf20Sopenharmony_ci		emit(rv_bgt(hi(rs1), hi(rs2), JUMP(2)), ctx);
6168c2ecf20Sopenharmony_ci		emit(rv_blt(hi(rs1), hi(rs2), NO_JUMP(1)), ctx);
6178c2ecf20Sopenharmony_ci		emit(rv_bleu(lo(rs1), lo(rs2), NO_JUMP(0)), ctx);
6188c2ecf20Sopenharmony_ci		break;
6198c2ecf20Sopenharmony_ci	case BPF_JSLT:
6208c2ecf20Sopenharmony_ci		emit(rv_blt(hi(rs1), hi(rs2), JUMP(2)), ctx);
6218c2ecf20Sopenharmony_ci		emit(rv_bgt(hi(rs1), hi(rs2), NO_JUMP(1)), ctx);
6228c2ecf20Sopenharmony_ci		emit(rv_bgeu(lo(rs1), lo(rs2), NO_JUMP(0)), ctx);
6238c2ecf20Sopenharmony_ci		break;
6248c2ecf20Sopenharmony_ci	case BPF_JSGE:
6258c2ecf20Sopenharmony_ci		emit(rv_bgt(hi(rs1), hi(rs2), JUMP(2)), ctx);
6268c2ecf20Sopenharmony_ci		emit(rv_blt(hi(rs1), hi(rs2), NO_JUMP(1)), ctx);
6278c2ecf20Sopenharmony_ci		emit(rv_bltu(lo(rs1), lo(rs2), NO_JUMP(0)), ctx);
6288c2ecf20Sopenharmony_ci		break;
6298c2ecf20Sopenharmony_ci	case BPF_JSLE:
6308c2ecf20Sopenharmony_ci		emit(rv_blt(hi(rs1), hi(rs2), JUMP(2)), ctx);
6318c2ecf20Sopenharmony_ci		emit(rv_bgt(hi(rs1), hi(rs2), NO_JUMP(1)), ctx);
6328c2ecf20Sopenharmony_ci		emit(rv_bgtu(lo(rs1), lo(rs2), NO_JUMP(0)), ctx);
6338c2ecf20Sopenharmony_ci		break;
6348c2ecf20Sopenharmony_ci	case BPF_JSET:
6358c2ecf20Sopenharmony_ci		emit(rv_and(RV_REG_T0, hi(rs1), hi(rs2)), ctx);
6368c2ecf20Sopenharmony_ci		emit(rv_bne(RV_REG_T0, RV_REG_ZERO, JUMP(2)), ctx);
6378c2ecf20Sopenharmony_ci		emit(rv_and(RV_REG_T0, lo(rs1), lo(rs2)), ctx);
6388c2ecf20Sopenharmony_ci		emit(rv_beq(RV_REG_T0, RV_REG_ZERO, NO_JUMP(0)), ctx);
6398c2ecf20Sopenharmony_ci		break;
6408c2ecf20Sopenharmony_ci	}
6418c2ecf20Sopenharmony_ci
6428c2ecf20Sopenharmony_ci#undef NO_JUMP
6438c2ecf20Sopenharmony_ci#undef JUMP
6448c2ecf20Sopenharmony_ci
6458c2ecf20Sopenharmony_ci	e = ctx->ninsns;
6468c2ecf20Sopenharmony_ci	/* Adjust for extra insns. */
6478c2ecf20Sopenharmony_ci	rvoff -= ninsns_rvoff(e - s);
6488c2ecf20Sopenharmony_ci	emit_jump_and_link(RV_REG_ZERO, rvoff, true, ctx);
6498c2ecf20Sopenharmony_ci	return 0;
6508c2ecf20Sopenharmony_ci}
6518c2ecf20Sopenharmony_ci
6528c2ecf20Sopenharmony_cistatic int emit_bcc(u8 op, u8 rd, u8 rs, int rvoff, struct rv_jit_context *ctx)
6538c2ecf20Sopenharmony_ci{
6548c2ecf20Sopenharmony_ci	int e, s = ctx->ninsns;
6558c2ecf20Sopenharmony_ci	bool far = false;
6568c2ecf20Sopenharmony_ci	int off;
6578c2ecf20Sopenharmony_ci
6588c2ecf20Sopenharmony_ci	if (op == BPF_JSET) {
6598c2ecf20Sopenharmony_ci		/*
6608c2ecf20Sopenharmony_ci		 * BPF_JSET is a special case: it has no inverse so we always
6618c2ecf20Sopenharmony_ci		 * treat it as a far branch.
6628c2ecf20Sopenharmony_ci		 */
6638c2ecf20Sopenharmony_ci		far = true;
6648c2ecf20Sopenharmony_ci	} else if (!is_13b_int(rvoff)) {
6658c2ecf20Sopenharmony_ci		op = invert_bpf_cond(op);
6668c2ecf20Sopenharmony_ci		far = true;
6678c2ecf20Sopenharmony_ci	}
6688c2ecf20Sopenharmony_ci
6698c2ecf20Sopenharmony_ci	/*
6708c2ecf20Sopenharmony_ci	 * For a far branch, the condition is negated and we jump over the
6718c2ecf20Sopenharmony_ci	 * branch itself, and the two instructions from emit_jump_and_link.
6728c2ecf20Sopenharmony_ci	 * For a near branch, just use rvoff.
6738c2ecf20Sopenharmony_ci	 */
6748c2ecf20Sopenharmony_ci	off = far ? 6 : (rvoff >> 1);
6758c2ecf20Sopenharmony_ci
6768c2ecf20Sopenharmony_ci	switch (op) {
6778c2ecf20Sopenharmony_ci	case BPF_JEQ:
6788c2ecf20Sopenharmony_ci		emit(rv_beq(rd, rs, off), ctx);
6798c2ecf20Sopenharmony_ci		break;
6808c2ecf20Sopenharmony_ci	case BPF_JGT:
6818c2ecf20Sopenharmony_ci		emit(rv_bgtu(rd, rs, off), ctx);
6828c2ecf20Sopenharmony_ci		break;
6838c2ecf20Sopenharmony_ci	case BPF_JLT:
6848c2ecf20Sopenharmony_ci		emit(rv_bltu(rd, rs, off), ctx);
6858c2ecf20Sopenharmony_ci		break;
6868c2ecf20Sopenharmony_ci	case BPF_JGE:
6878c2ecf20Sopenharmony_ci		emit(rv_bgeu(rd, rs, off), ctx);
6888c2ecf20Sopenharmony_ci		break;
6898c2ecf20Sopenharmony_ci	case BPF_JLE:
6908c2ecf20Sopenharmony_ci		emit(rv_bleu(rd, rs, off), ctx);
6918c2ecf20Sopenharmony_ci		break;
6928c2ecf20Sopenharmony_ci	case BPF_JNE:
6938c2ecf20Sopenharmony_ci		emit(rv_bne(rd, rs, off), ctx);
6948c2ecf20Sopenharmony_ci		break;
6958c2ecf20Sopenharmony_ci	case BPF_JSGT:
6968c2ecf20Sopenharmony_ci		emit(rv_bgt(rd, rs, off), ctx);
6978c2ecf20Sopenharmony_ci		break;
6988c2ecf20Sopenharmony_ci	case BPF_JSLT:
6998c2ecf20Sopenharmony_ci		emit(rv_blt(rd, rs, off), ctx);
7008c2ecf20Sopenharmony_ci		break;
7018c2ecf20Sopenharmony_ci	case BPF_JSGE:
7028c2ecf20Sopenharmony_ci		emit(rv_bge(rd, rs, off), ctx);
7038c2ecf20Sopenharmony_ci		break;
7048c2ecf20Sopenharmony_ci	case BPF_JSLE:
7058c2ecf20Sopenharmony_ci		emit(rv_ble(rd, rs, off), ctx);
7068c2ecf20Sopenharmony_ci		break;
7078c2ecf20Sopenharmony_ci	case BPF_JSET:
7088c2ecf20Sopenharmony_ci		emit(rv_and(RV_REG_T0, rd, rs), ctx);
7098c2ecf20Sopenharmony_ci		emit(rv_beq(RV_REG_T0, RV_REG_ZERO, off), ctx);
7108c2ecf20Sopenharmony_ci		break;
7118c2ecf20Sopenharmony_ci	}
7128c2ecf20Sopenharmony_ci
7138c2ecf20Sopenharmony_ci	if (far) {
7148c2ecf20Sopenharmony_ci		e = ctx->ninsns;
7158c2ecf20Sopenharmony_ci		/* Adjust for extra insns. */
7168c2ecf20Sopenharmony_ci		rvoff -= ninsns_rvoff(e - s);
7178c2ecf20Sopenharmony_ci		emit_jump_and_link(RV_REG_ZERO, rvoff, true, ctx);
7188c2ecf20Sopenharmony_ci	}
7198c2ecf20Sopenharmony_ci	return 0;
7208c2ecf20Sopenharmony_ci}
7218c2ecf20Sopenharmony_ci
7228c2ecf20Sopenharmony_cistatic int emit_branch_r32(const s8 *src1, const s8 *src2, s32 rvoff,
7238c2ecf20Sopenharmony_ci			   struct rv_jit_context *ctx, const u8 op)
7248c2ecf20Sopenharmony_ci{
7258c2ecf20Sopenharmony_ci	int e, s = ctx->ninsns;
7268c2ecf20Sopenharmony_ci	const s8 *tmp1 = bpf2rv32[TMP_REG_1];
7278c2ecf20Sopenharmony_ci	const s8 *tmp2 = bpf2rv32[TMP_REG_2];
7288c2ecf20Sopenharmony_ci
7298c2ecf20Sopenharmony_ci	const s8 *rs1 = bpf_get_reg32(src1, tmp1, ctx);
7308c2ecf20Sopenharmony_ci	const s8 *rs2 = bpf_get_reg32(src2, tmp2, ctx);
7318c2ecf20Sopenharmony_ci
7328c2ecf20Sopenharmony_ci	e = ctx->ninsns;
7338c2ecf20Sopenharmony_ci	/* Adjust for extra insns. */
7348c2ecf20Sopenharmony_ci	rvoff -= ninsns_rvoff(e - s);
7358c2ecf20Sopenharmony_ci
7368c2ecf20Sopenharmony_ci	if (emit_bcc(op, lo(rs1), lo(rs2), rvoff, ctx))
7378c2ecf20Sopenharmony_ci		return -1;
7388c2ecf20Sopenharmony_ci
7398c2ecf20Sopenharmony_ci	return 0;
7408c2ecf20Sopenharmony_ci}
7418c2ecf20Sopenharmony_ci
7428c2ecf20Sopenharmony_cistatic void emit_call(bool fixed, u64 addr, struct rv_jit_context *ctx)
7438c2ecf20Sopenharmony_ci{
7448c2ecf20Sopenharmony_ci	const s8 *r0 = bpf2rv32[BPF_REG_0];
7458c2ecf20Sopenharmony_ci	const s8 *r5 = bpf2rv32[BPF_REG_5];
7468c2ecf20Sopenharmony_ci	u32 upper = ((u32)addr + (1 << 11)) >> 12;
7478c2ecf20Sopenharmony_ci	u32 lower = addr & 0xfff;
7488c2ecf20Sopenharmony_ci
7498c2ecf20Sopenharmony_ci	/* R1-R4 already in correct registers---need to push R5 to stack. */
7508c2ecf20Sopenharmony_ci	emit(rv_addi(RV_REG_SP, RV_REG_SP, -16), ctx);
7518c2ecf20Sopenharmony_ci	emit(rv_sw(RV_REG_SP, 0, lo(r5)), ctx);
7528c2ecf20Sopenharmony_ci	emit(rv_sw(RV_REG_SP, 4, hi(r5)), ctx);
7538c2ecf20Sopenharmony_ci
7548c2ecf20Sopenharmony_ci	/* Backup TCC. */
7558c2ecf20Sopenharmony_ci	emit(rv_addi(RV_REG_TCC_SAVED, RV_REG_TCC, 0), ctx);
7568c2ecf20Sopenharmony_ci
7578c2ecf20Sopenharmony_ci	/*
7588c2ecf20Sopenharmony_ci	 * Use lui/jalr pair to jump to absolute address. Don't use emit_imm as
7598c2ecf20Sopenharmony_ci	 * the number of emitted instructions should not depend on the value of
7608c2ecf20Sopenharmony_ci	 * addr.
7618c2ecf20Sopenharmony_ci	 */
7628c2ecf20Sopenharmony_ci	emit(rv_lui(RV_REG_T1, upper), ctx);
7638c2ecf20Sopenharmony_ci	emit(rv_jalr(RV_REG_RA, RV_REG_T1, lower), ctx);
7648c2ecf20Sopenharmony_ci
7658c2ecf20Sopenharmony_ci	/* Restore TCC. */
7668c2ecf20Sopenharmony_ci	emit(rv_addi(RV_REG_TCC, RV_REG_TCC_SAVED, 0), ctx);
7678c2ecf20Sopenharmony_ci
7688c2ecf20Sopenharmony_ci	/* Set return value and restore stack. */
7698c2ecf20Sopenharmony_ci	emit(rv_addi(lo(r0), RV_REG_A0, 0), ctx);
7708c2ecf20Sopenharmony_ci	emit(rv_addi(hi(r0), RV_REG_A1, 0), ctx);
7718c2ecf20Sopenharmony_ci	emit(rv_addi(RV_REG_SP, RV_REG_SP, 16), ctx);
7728c2ecf20Sopenharmony_ci}
7738c2ecf20Sopenharmony_ci
7748c2ecf20Sopenharmony_cistatic int emit_bpf_tail_call(int insn, struct rv_jit_context *ctx)
7758c2ecf20Sopenharmony_ci{
7768c2ecf20Sopenharmony_ci	/*
7778c2ecf20Sopenharmony_ci	 * R1 -> &ctx
7788c2ecf20Sopenharmony_ci	 * R2 -> &array
7798c2ecf20Sopenharmony_ci	 * R3 -> index
7808c2ecf20Sopenharmony_ci	 */
7818c2ecf20Sopenharmony_ci	int tc_ninsn, off, start_insn = ctx->ninsns;
7828c2ecf20Sopenharmony_ci	const s8 *arr_reg = bpf2rv32[BPF_REG_2];
7838c2ecf20Sopenharmony_ci	const s8 *idx_reg = bpf2rv32[BPF_REG_3];
7848c2ecf20Sopenharmony_ci
7858c2ecf20Sopenharmony_ci	tc_ninsn = insn ? ctx->offset[insn] - ctx->offset[insn - 1] :
7868c2ecf20Sopenharmony_ci		ctx->offset[0];
7878c2ecf20Sopenharmony_ci
7888c2ecf20Sopenharmony_ci	/* max_entries = array->map.max_entries; */
7898c2ecf20Sopenharmony_ci	off = offsetof(struct bpf_array, map.max_entries);
7908c2ecf20Sopenharmony_ci	if (is_12b_check(off, insn))
7918c2ecf20Sopenharmony_ci		return -1;
7928c2ecf20Sopenharmony_ci	emit(rv_lw(RV_REG_T1, off, lo(arr_reg)), ctx);
7938c2ecf20Sopenharmony_ci
7948c2ecf20Sopenharmony_ci	/*
7958c2ecf20Sopenharmony_ci	 * if (index >= max_entries)
7968c2ecf20Sopenharmony_ci	 *   goto out;
7978c2ecf20Sopenharmony_ci	 */
7988c2ecf20Sopenharmony_ci	off = ninsns_rvoff(tc_ninsn - (ctx->ninsns - start_insn));
7998c2ecf20Sopenharmony_ci	emit_bcc(BPF_JGE, lo(idx_reg), RV_REG_T1, off, ctx);
8008c2ecf20Sopenharmony_ci
8018c2ecf20Sopenharmony_ci	/*
8028c2ecf20Sopenharmony_ci	 * temp_tcc = tcc - 1;
8038c2ecf20Sopenharmony_ci	 * if (tcc < 0)
8048c2ecf20Sopenharmony_ci	 *   goto out;
8058c2ecf20Sopenharmony_ci	 */
8068c2ecf20Sopenharmony_ci	emit(rv_addi(RV_REG_T1, RV_REG_TCC, -1), ctx);
8078c2ecf20Sopenharmony_ci	off = ninsns_rvoff(tc_ninsn - (ctx->ninsns - start_insn));
8088c2ecf20Sopenharmony_ci	emit_bcc(BPF_JSLT, RV_REG_TCC, RV_REG_ZERO, off, ctx);
8098c2ecf20Sopenharmony_ci
8108c2ecf20Sopenharmony_ci	/*
8118c2ecf20Sopenharmony_ci	 * prog = array->ptrs[index];
8128c2ecf20Sopenharmony_ci	 * if (!prog)
8138c2ecf20Sopenharmony_ci	 *   goto out;
8148c2ecf20Sopenharmony_ci	 */
8158c2ecf20Sopenharmony_ci	emit(rv_slli(RV_REG_T0, lo(idx_reg), 2), ctx);
8168c2ecf20Sopenharmony_ci	emit(rv_add(RV_REG_T0, RV_REG_T0, lo(arr_reg)), ctx);
8178c2ecf20Sopenharmony_ci	off = offsetof(struct bpf_array, ptrs);
8188c2ecf20Sopenharmony_ci	if (is_12b_check(off, insn))
8198c2ecf20Sopenharmony_ci		return -1;
8208c2ecf20Sopenharmony_ci	emit(rv_lw(RV_REG_T0, off, RV_REG_T0), ctx);
8218c2ecf20Sopenharmony_ci	off = ninsns_rvoff(tc_ninsn - (ctx->ninsns - start_insn));
8228c2ecf20Sopenharmony_ci	emit_bcc(BPF_JEQ, RV_REG_T0, RV_REG_ZERO, off, ctx);
8238c2ecf20Sopenharmony_ci
8248c2ecf20Sopenharmony_ci	/*
8258c2ecf20Sopenharmony_ci	 * tcc = temp_tcc;
8268c2ecf20Sopenharmony_ci	 * goto *(prog->bpf_func + 4);
8278c2ecf20Sopenharmony_ci	 */
8288c2ecf20Sopenharmony_ci	off = offsetof(struct bpf_prog, bpf_func);
8298c2ecf20Sopenharmony_ci	if (is_12b_check(off, insn))
8308c2ecf20Sopenharmony_ci		return -1;
8318c2ecf20Sopenharmony_ci	emit(rv_lw(RV_REG_T0, off, RV_REG_T0), ctx);
8328c2ecf20Sopenharmony_ci	emit(rv_addi(RV_REG_TCC, RV_REG_T1, 0), ctx);
8338c2ecf20Sopenharmony_ci	/* Epilogue jumps to *(t0 + 4). */
8348c2ecf20Sopenharmony_ci	__build_epilogue(true, ctx);
8358c2ecf20Sopenharmony_ci	return 0;
8368c2ecf20Sopenharmony_ci}
8378c2ecf20Sopenharmony_ci
8388c2ecf20Sopenharmony_cistatic int emit_load_r64(const s8 *dst, const s8 *src, s16 off,
8398c2ecf20Sopenharmony_ci			 struct rv_jit_context *ctx, const u8 size)
8408c2ecf20Sopenharmony_ci{
8418c2ecf20Sopenharmony_ci	const s8 *tmp1 = bpf2rv32[TMP_REG_1];
8428c2ecf20Sopenharmony_ci	const s8 *tmp2 = bpf2rv32[TMP_REG_2];
8438c2ecf20Sopenharmony_ci	const s8 *rd = bpf_get_reg64(dst, tmp1, ctx);
8448c2ecf20Sopenharmony_ci	const s8 *rs = bpf_get_reg64(src, tmp2, ctx);
8458c2ecf20Sopenharmony_ci
8468c2ecf20Sopenharmony_ci	emit_imm(RV_REG_T0, off, ctx);
8478c2ecf20Sopenharmony_ci	emit(rv_add(RV_REG_T0, RV_REG_T0, lo(rs)), ctx);
8488c2ecf20Sopenharmony_ci
8498c2ecf20Sopenharmony_ci	switch (size) {
8508c2ecf20Sopenharmony_ci	case BPF_B:
8518c2ecf20Sopenharmony_ci		emit(rv_lbu(lo(rd), 0, RV_REG_T0), ctx);
8528c2ecf20Sopenharmony_ci		if (!ctx->prog->aux->verifier_zext)
8538c2ecf20Sopenharmony_ci			emit(rv_addi(hi(rd), RV_REG_ZERO, 0), ctx);
8548c2ecf20Sopenharmony_ci		break;
8558c2ecf20Sopenharmony_ci	case BPF_H:
8568c2ecf20Sopenharmony_ci		emit(rv_lhu(lo(rd), 0, RV_REG_T0), ctx);
8578c2ecf20Sopenharmony_ci		if (!ctx->prog->aux->verifier_zext)
8588c2ecf20Sopenharmony_ci			emit(rv_addi(hi(rd), RV_REG_ZERO, 0), ctx);
8598c2ecf20Sopenharmony_ci		break;
8608c2ecf20Sopenharmony_ci	case BPF_W:
8618c2ecf20Sopenharmony_ci		emit(rv_lw(lo(rd), 0, RV_REG_T0), ctx);
8628c2ecf20Sopenharmony_ci		if (!ctx->prog->aux->verifier_zext)
8638c2ecf20Sopenharmony_ci			emit(rv_addi(hi(rd), RV_REG_ZERO, 0), ctx);
8648c2ecf20Sopenharmony_ci		break;
8658c2ecf20Sopenharmony_ci	case BPF_DW:
8668c2ecf20Sopenharmony_ci		emit(rv_lw(lo(rd), 0, RV_REG_T0), ctx);
8678c2ecf20Sopenharmony_ci		emit(rv_lw(hi(rd), 4, RV_REG_T0), ctx);
8688c2ecf20Sopenharmony_ci		break;
8698c2ecf20Sopenharmony_ci	}
8708c2ecf20Sopenharmony_ci
8718c2ecf20Sopenharmony_ci	bpf_put_reg64(dst, rd, ctx);
8728c2ecf20Sopenharmony_ci	return 0;
8738c2ecf20Sopenharmony_ci}
8748c2ecf20Sopenharmony_ci
8758c2ecf20Sopenharmony_cistatic int emit_store_r64(const s8 *dst, const s8 *src, s16 off,
8768c2ecf20Sopenharmony_ci			  struct rv_jit_context *ctx, const u8 size,
8778c2ecf20Sopenharmony_ci			  const u8 mode)
8788c2ecf20Sopenharmony_ci{
8798c2ecf20Sopenharmony_ci	const s8 *tmp1 = bpf2rv32[TMP_REG_1];
8808c2ecf20Sopenharmony_ci	const s8 *tmp2 = bpf2rv32[TMP_REG_2];
8818c2ecf20Sopenharmony_ci	const s8 *rd = bpf_get_reg64(dst, tmp1, ctx);
8828c2ecf20Sopenharmony_ci	const s8 *rs = bpf_get_reg64(src, tmp2, ctx);
8838c2ecf20Sopenharmony_ci
8848c2ecf20Sopenharmony_ci	if (mode == BPF_XADD && size != BPF_W)
8858c2ecf20Sopenharmony_ci		return -1;
8868c2ecf20Sopenharmony_ci
8878c2ecf20Sopenharmony_ci	emit_imm(RV_REG_T0, off, ctx);
8888c2ecf20Sopenharmony_ci	emit(rv_add(RV_REG_T0, RV_REG_T0, lo(rd)), ctx);
8898c2ecf20Sopenharmony_ci
8908c2ecf20Sopenharmony_ci	switch (size) {
8918c2ecf20Sopenharmony_ci	case BPF_B:
8928c2ecf20Sopenharmony_ci		emit(rv_sb(RV_REG_T0, 0, lo(rs)), ctx);
8938c2ecf20Sopenharmony_ci		break;
8948c2ecf20Sopenharmony_ci	case BPF_H:
8958c2ecf20Sopenharmony_ci		emit(rv_sh(RV_REG_T0, 0, lo(rs)), ctx);
8968c2ecf20Sopenharmony_ci		break;
8978c2ecf20Sopenharmony_ci	case BPF_W:
8988c2ecf20Sopenharmony_ci		switch (mode) {
8998c2ecf20Sopenharmony_ci		case BPF_MEM:
9008c2ecf20Sopenharmony_ci			emit(rv_sw(RV_REG_T0, 0, lo(rs)), ctx);
9018c2ecf20Sopenharmony_ci			break;
9028c2ecf20Sopenharmony_ci		case BPF_XADD:
9038c2ecf20Sopenharmony_ci			emit(rv_amoadd_w(RV_REG_ZERO, lo(rs), RV_REG_T0, 0, 0),
9048c2ecf20Sopenharmony_ci			     ctx);
9058c2ecf20Sopenharmony_ci			break;
9068c2ecf20Sopenharmony_ci		}
9078c2ecf20Sopenharmony_ci		break;
9088c2ecf20Sopenharmony_ci	case BPF_DW:
9098c2ecf20Sopenharmony_ci		emit(rv_sw(RV_REG_T0, 0, lo(rs)), ctx);
9108c2ecf20Sopenharmony_ci		emit(rv_sw(RV_REG_T0, 4, hi(rs)), ctx);
9118c2ecf20Sopenharmony_ci		break;
9128c2ecf20Sopenharmony_ci	}
9138c2ecf20Sopenharmony_ci
9148c2ecf20Sopenharmony_ci	return 0;
9158c2ecf20Sopenharmony_ci}
9168c2ecf20Sopenharmony_ci
9178c2ecf20Sopenharmony_cistatic void emit_rev16(const s8 rd, struct rv_jit_context *ctx)
9188c2ecf20Sopenharmony_ci{
9198c2ecf20Sopenharmony_ci	emit(rv_slli(rd, rd, 16), ctx);
9208c2ecf20Sopenharmony_ci	emit(rv_slli(RV_REG_T1, rd, 8), ctx);
9218c2ecf20Sopenharmony_ci	emit(rv_srli(rd, rd, 8), ctx);
9228c2ecf20Sopenharmony_ci	emit(rv_add(RV_REG_T1, rd, RV_REG_T1), ctx);
9238c2ecf20Sopenharmony_ci	emit(rv_srli(rd, RV_REG_T1, 16), ctx);
9248c2ecf20Sopenharmony_ci}
9258c2ecf20Sopenharmony_ci
9268c2ecf20Sopenharmony_cistatic void emit_rev32(const s8 rd, struct rv_jit_context *ctx)
9278c2ecf20Sopenharmony_ci{
9288c2ecf20Sopenharmony_ci	emit(rv_addi(RV_REG_T1, RV_REG_ZERO, 0), ctx);
9298c2ecf20Sopenharmony_ci	emit(rv_andi(RV_REG_T0, rd, 255), ctx);
9308c2ecf20Sopenharmony_ci	emit(rv_add(RV_REG_T1, RV_REG_T1, RV_REG_T0), ctx);
9318c2ecf20Sopenharmony_ci	emit(rv_slli(RV_REG_T1, RV_REG_T1, 8), ctx);
9328c2ecf20Sopenharmony_ci	emit(rv_srli(rd, rd, 8), ctx);
9338c2ecf20Sopenharmony_ci	emit(rv_andi(RV_REG_T0, rd, 255), ctx);
9348c2ecf20Sopenharmony_ci	emit(rv_add(RV_REG_T1, RV_REG_T1, RV_REG_T0), ctx);
9358c2ecf20Sopenharmony_ci	emit(rv_slli(RV_REG_T1, RV_REG_T1, 8), ctx);
9368c2ecf20Sopenharmony_ci	emit(rv_srli(rd, rd, 8), ctx);
9378c2ecf20Sopenharmony_ci	emit(rv_andi(RV_REG_T0, rd, 255), ctx);
9388c2ecf20Sopenharmony_ci	emit(rv_add(RV_REG_T1, RV_REG_T1, RV_REG_T0), ctx);
9398c2ecf20Sopenharmony_ci	emit(rv_slli(RV_REG_T1, RV_REG_T1, 8), ctx);
9408c2ecf20Sopenharmony_ci	emit(rv_srli(rd, rd, 8), ctx);
9418c2ecf20Sopenharmony_ci	emit(rv_andi(RV_REG_T0, rd, 255), ctx);
9428c2ecf20Sopenharmony_ci	emit(rv_add(RV_REG_T1, RV_REG_T1, RV_REG_T0), ctx);
9438c2ecf20Sopenharmony_ci	emit(rv_addi(rd, RV_REG_T1, 0), ctx);
9448c2ecf20Sopenharmony_ci}
9458c2ecf20Sopenharmony_ci
9468c2ecf20Sopenharmony_cistatic void emit_zext64(const s8 *dst, struct rv_jit_context *ctx)
9478c2ecf20Sopenharmony_ci{
9488c2ecf20Sopenharmony_ci	const s8 *rd;
9498c2ecf20Sopenharmony_ci	const s8 *tmp1 = bpf2rv32[TMP_REG_1];
9508c2ecf20Sopenharmony_ci
9518c2ecf20Sopenharmony_ci	rd = bpf_get_reg64(dst, tmp1, ctx);
9528c2ecf20Sopenharmony_ci	emit(rv_addi(hi(rd), RV_REG_ZERO, 0), ctx);
9538c2ecf20Sopenharmony_ci	bpf_put_reg64(dst, rd, ctx);
9548c2ecf20Sopenharmony_ci}
9558c2ecf20Sopenharmony_ci
9568c2ecf20Sopenharmony_ciint bpf_jit_emit_insn(const struct bpf_insn *insn, struct rv_jit_context *ctx,
9578c2ecf20Sopenharmony_ci		      bool extra_pass)
9588c2ecf20Sopenharmony_ci{
9598c2ecf20Sopenharmony_ci	bool is64 = BPF_CLASS(insn->code) == BPF_ALU64 ||
9608c2ecf20Sopenharmony_ci		BPF_CLASS(insn->code) == BPF_JMP;
9618c2ecf20Sopenharmony_ci	int s, e, rvoff, i = insn - ctx->prog->insnsi;
9628c2ecf20Sopenharmony_ci	u8 code = insn->code;
9638c2ecf20Sopenharmony_ci	s16 off = insn->off;
9648c2ecf20Sopenharmony_ci	s32 imm = insn->imm;
9658c2ecf20Sopenharmony_ci
9668c2ecf20Sopenharmony_ci	const s8 *dst = bpf2rv32[insn->dst_reg];
9678c2ecf20Sopenharmony_ci	const s8 *src = bpf2rv32[insn->src_reg];
9688c2ecf20Sopenharmony_ci	const s8 *tmp1 = bpf2rv32[TMP_REG_1];
9698c2ecf20Sopenharmony_ci	const s8 *tmp2 = bpf2rv32[TMP_REG_2];
9708c2ecf20Sopenharmony_ci
9718c2ecf20Sopenharmony_ci	switch (code) {
9728c2ecf20Sopenharmony_ci	case BPF_ALU64 | BPF_MOV | BPF_X:
9738c2ecf20Sopenharmony_ci
9748c2ecf20Sopenharmony_ci	case BPF_ALU64 | BPF_ADD | BPF_X:
9758c2ecf20Sopenharmony_ci	case BPF_ALU64 | BPF_ADD | BPF_K:
9768c2ecf20Sopenharmony_ci
9778c2ecf20Sopenharmony_ci	case BPF_ALU64 | BPF_SUB | BPF_X:
9788c2ecf20Sopenharmony_ci	case BPF_ALU64 | BPF_SUB | BPF_K:
9798c2ecf20Sopenharmony_ci
9808c2ecf20Sopenharmony_ci	case BPF_ALU64 | BPF_AND | BPF_X:
9818c2ecf20Sopenharmony_ci	case BPF_ALU64 | BPF_OR | BPF_X:
9828c2ecf20Sopenharmony_ci	case BPF_ALU64 | BPF_XOR | BPF_X:
9838c2ecf20Sopenharmony_ci
9848c2ecf20Sopenharmony_ci	case BPF_ALU64 | BPF_MUL | BPF_X:
9858c2ecf20Sopenharmony_ci	case BPF_ALU64 | BPF_MUL | BPF_K:
9868c2ecf20Sopenharmony_ci
9878c2ecf20Sopenharmony_ci	case BPF_ALU64 | BPF_LSH | BPF_X:
9888c2ecf20Sopenharmony_ci	case BPF_ALU64 | BPF_RSH | BPF_X:
9898c2ecf20Sopenharmony_ci	case BPF_ALU64 | BPF_ARSH | BPF_X:
9908c2ecf20Sopenharmony_ci		if (BPF_SRC(code) == BPF_K) {
9918c2ecf20Sopenharmony_ci			emit_imm32(tmp2, imm, ctx);
9928c2ecf20Sopenharmony_ci			src = tmp2;
9938c2ecf20Sopenharmony_ci		}
9948c2ecf20Sopenharmony_ci		emit_alu_r64(dst, src, ctx, BPF_OP(code));
9958c2ecf20Sopenharmony_ci		break;
9968c2ecf20Sopenharmony_ci
9978c2ecf20Sopenharmony_ci	case BPF_ALU64 | BPF_NEG:
9988c2ecf20Sopenharmony_ci		emit_alu_r64(dst, tmp2, ctx, BPF_OP(code));
9998c2ecf20Sopenharmony_ci		break;
10008c2ecf20Sopenharmony_ci
10018c2ecf20Sopenharmony_ci	case BPF_ALU64 | BPF_DIV | BPF_X:
10028c2ecf20Sopenharmony_ci	case BPF_ALU64 | BPF_DIV | BPF_K:
10038c2ecf20Sopenharmony_ci	case BPF_ALU64 | BPF_MOD | BPF_X:
10048c2ecf20Sopenharmony_ci	case BPF_ALU64 | BPF_MOD | BPF_K:
10058c2ecf20Sopenharmony_ci		goto notsupported;
10068c2ecf20Sopenharmony_ci
10078c2ecf20Sopenharmony_ci	case BPF_ALU64 | BPF_MOV | BPF_K:
10088c2ecf20Sopenharmony_ci	case BPF_ALU64 | BPF_AND | BPF_K:
10098c2ecf20Sopenharmony_ci	case BPF_ALU64 | BPF_OR | BPF_K:
10108c2ecf20Sopenharmony_ci	case BPF_ALU64 | BPF_XOR | BPF_K:
10118c2ecf20Sopenharmony_ci	case BPF_ALU64 | BPF_LSH | BPF_K:
10128c2ecf20Sopenharmony_ci	case BPF_ALU64 | BPF_RSH | BPF_K:
10138c2ecf20Sopenharmony_ci	case BPF_ALU64 | BPF_ARSH | BPF_K:
10148c2ecf20Sopenharmony_ci		emit_alu_i64(dst, imm, ctx, BPF_OP(code));
10158c2ecf20Sopenharmony_ci		break;
10168c2ecf20Sopenharmony_ci
10178c2ecf20Sopenharmony_ci	case BPF_ALU | BPF_MOV | BPF_X:
10188c2ecf20Sopenharmony_ci		if (imm == 1) {
10198c2ecf20Sopenharmony_ci			/* Special mov32 for zext. */
10208c2ecf20Sopenharmony_ci			emit_zext64(dst, ctx);
10218c2ecf20Sopenharmony_ci			break;
10228c2ecf20Sopenharmony_ci		}
10238c2ecf20Sopenharmony_ci		fallthrough;
10248c2ecf20Sopenharmony_ci
10258c2ecf20Sopenharmony_ci	case BPF_ALU | BPF_ADD | BPF_X:
10268c2ecf20Sopenharmony_ci	case BPF_ALU | BPF_SUB | BPF_X:
10278c2ecf20Sopenharmony_ci	case BPF_ALU | BPF_AND | BPF_X:
10288c2ecf20Sopenharmony_ci	case BPF_ALU | BPF_OR | BPF_X:
10298c2ecf20Sopenharmony_ci	case BPF_ALU | BPF_XOR | BPF_X:
10308c2ecf20Sopenharmony_ci
10318c2ecf20Sopenharmony_ci	case BPF_ALU | BPF_MUL | BPF_X:
10328c2ecf20Sopenharmony_ci	case BPF_ALU | BPF_MUL | BPF_K:
10338c2ecf20Sopenharmony_ci
10348c2ecf20Sopenharmony_ci	case BPF_ALU | BPF_DIV | BPF_X:
10358c2ecf20Sopenharmony_ci	case BPF_ALU | BPF_DIV | BPF_K:
10368c2ecf20Sopenharmony_ci
10378c2ecf20Sopenharmony_ci	case BPF_ALU | BPF_MOD | BPF_X:
10388c2ecf20Sopenharmony_ci	case BPF_ALU | BPF_MOD | BPF_K:
10398c2ecf20Sopenharmony_ci
10408c2ecf20Sopenharmony_ci	case BPF_ALU | BPF_LSH | BPF_X:
10418c2ecf20Sopenharmony_ci	case BPF_ALU | BPF_RSH | BPF_X:
10428c2ecf20Sopenharmony_ci	case BPF_ALU | BPF_ARSH | BPF_X:
10438c2ecf20Sopenharmony_ci		if (BPF_SRC(code) == BPF_K) {
10448c2ecf20Sopenharmony_ci			emit_imm32(tmp2, imm, ctx);
10458c2ecf20Sopenharmony_ci			src = tmp2;
10468c2ecf20Sopenharmony_ci		}
10478c2ecf20Sopenharmony_ci		emit_alu_r32(dst, src, ctx, BPF_OP(code));
10488c2ecf20Sopenharmony_ci		break;
10498c2ecf20Sopenharmony_ci
10508c2ecf20Sopenharmony_ci	case BPF_ALU | BPF_MOV | BPF_K:
10518c2ecf20Sopenharmony_ci	case BPF_ALU | BPF_ADD | BPF_K:
10528c2ecf20Sopenharmony_ci	case BPF_ALU | BPF_SUB | BPF_K:
10538c2ecf20Sopenharmony_ci	case BPF_ALU | BPF_AND | BPF_K:
10548c2ecf20Sopenharmony_ci	case BPF_ALU | BPF_OR | BPF_K:
10558c2ecf20Sopenharmony_ci	case BPF_ALU | BPF_XOR | BPF_K:
10568c2ecf20Sopenharmony_ci	case BPF_ALU | BPF_LSH | BPF_K:
10578c2ecf20Sopenharmony_ci	case BPF_ALU | BPF_RSH | BPF_K:
10588c2ecf20Sopenharmony_ci	case BPF_ALU | BPF_ARSH | BPF_K:
10598c2ecf20Sopenharmony_ci		/*
10608c2ecf20Sopenharmony_ci		 * mul,div,mod are handled in the BPF_X case since there are
10618c2ecf20Sopenharmony_ci		 * no RISC-V I-type equivalents.
10628c2ecf20Sopenharmony_ci		 */
10638c2ecf20Sopenharmony_ci		emit_alu_i32(dst, imm, ctx, BPF_OP(code));
10648c2ecf20Sopenharmony_ci		break;
10658c2ecf20Sopenharmony_ci
10668c2ecf20Sopenharmony_ci	case BPF_ALU | BPF_NEG:
10678c2ecf20Sopenharmony_ci		/*
10688c2ecf20Sopenharmony_ci		 * src is ignored---choose tmp2 as a dummy register since it
10698c2ecf20Sopenharmony_ci		 * is not on the stack.
10708c2ecf20Sopenharmony_ci		 */
10718c2ecf20Sopenharmony_ci		emit_alu_r32(dst, tmp2, ctx, BPF_OP(code));
10728c2ecf20Sopenharmony_ci		break;
10738c2ecf20Sopenharmony_ci
10748c2ecf20Sopenharmony_ci	case BPF_ALU | BPF_END | BPF_FROM_LE:
10758c2ecf20Sopenharmony_ci	{
10768c2ecf20Sopenharmony_ci		const s8 *rd = bpf_get_reg64(dst, tmp1, ctx);
10778c2ecf20Sopenharmony_ci
10788c2ecf20Sopenharmony_ci		switch (imm) {
10798c2ecf20Sopenharmony_ci		case 16:
10808c2ecf20Sopenharmony_ci			emit(rv_slli(lo(rd), lo(rd), 16), ctx);
10818c2ecf20Sopenharmony_ci			emit(rv_srli(lo(rd), lo(rd), 16), ctx);
10828c2ecf20Sopenharmony_ci			fallthrough;
10838c2ecf20Sopenharmony_ci		case 32:
10848c2ecf20Sopenharmony_ci			if (!ctx->prog->aux->verifier_zext)
10858c2ecf20Sopenharmony_ci				emit(rv_addi(hi(rd), RV_REG_ZERO, 0), ctx);
10868c2ecf20Sopenharmony_ci			break;
10878c2ecf20Sopenharmony_ci		case 64:
10888c2ecf20Sopenharmony_ci			/* Do nothing. */
10898c2ecf20Sopenharmony_ci			break;
10908c2ecf20Sopenharmony_ci		default:
10918c2ecf20Sopenharmony_ci			pr_err("bpf-jit: BPF_END imm %d invalid\n", imm);
10928c2ecf20Sopenharmony_ci			return -1;
10938c2ecf20Sopenharmony_ci		}
10948c2ecf20Sopenharmony_ci
10958c2ecf20Sopenharmony_ci		bpf_put_reg64(dst, rd, ctx);
10968c2ecf20Sopenharmony_ci		break;
10978c2ecf20Sopenharmony_ci	}
10988c2ecf20Sopenharmony_ci
10998c2ecf20Sopenharmony_ci	case BPF_ALU | BPF_END | BPF_FROM_BE:
11008c2ecf20Sopenharmony_ci	{
11018c2ecf20Sopenharmony_ci		const s8 *rd = bpf_get_reg64(dst, tmp1, ctx);
11028c2ecf20Sopenharmony_ci
11038c2ecf20Sopenharmony_ci		switch (imm) {
11048c2ecf20Sopenharmony_ci		case 16:
11058c2ecf20Sopenharmony_ci			emit_rev16(lo(rd), ctx);
11068c2ecf20Sopenharmony_ci			if (!ctx->prog->aux->verifier_zext)
11078c2ecf20Sopenharmony_ci				emit(rv_addi(hi(rd), RV_REG_ZERO, 0), ctx);
11088c2ecf20Sopenharmony_ci			break;
11098c2ecf20Sopenharmony_ci		case 32:
11108c2ecf20Sopenharmony_ci			emit_rev32(lo(rd), ctx);
11118c2ecf20Sopenharmony_ci			if (!ctx->prog->aux->verifier_zext)
11128c2ecf20Sopenharmony_ci				emit(rv_addi(hi(rd), RV_REG_ZERO, 0), ctx);
11138c2ecf20Sopenharmony_ci			break;
11148c2ecf20Sopenharmony_ci		case 64:
11158c2ecf20Sopenharmony_ci			/* Swap upper and lower halves. */
11168c2ecf20Sopenharmony_ci			emit(rv_addi(RV_REG_T0, lo(rd), 0), ctx);
11178c2ecf20Sopenharmony_ci			emit(rv_addi(lo(rd), hi(rd), 0), ctx);
11188c2ecf20Sopenharmony_ci			emit(rv_addi(hi(rd), RV_REG_T0, 0), ctx);
11198c2ecf20Sopenharmony_ci
11208c2ecf20Sopenharmony_ci			/* Swap each half. */
11218c2ecf20Sopenharmony_ci			emit_rev32(lo(rd), ctx);
11228c2ecf20Sopenharmony_ci			emit_rev32(hi(rd), ctx);
11238c2ecf20Sopenharmony_ci			break;
11248c2ecf20Sopenharmony_ci		default:
11258c2ecf20Sopenharmony_ci			pr_err("bpf-jit: BPF_END imm %d invalid\n", imm);
11268c2ecf20Sopenharmony_ci			return -1;
11278c2ecf20Sopenharmony_ci		}
11288c2ecf20Sopenharmony_ci
11298c2ecf20Sopenharmony_ci		bpf_put_reg64(dst, rd, ctx);
11308c2ecf20Sopenharmony_ci		break;
11318c2ecf20Sopenharmony_ci	}
11328c2ecf20Sopenharmony_ci
11338c2ecf20Sopenharmony_ci	case BPF_JMP | BPF_JA:
11348c2ecf20Sopenharmony_ci		rvoff = rv_offset(i, off, ctx);
11358c2ecf20Sopenharmony_ci		emit_jump_and_link(RV_REG_ZERO, rvoff, false, ctx);
11368c2ecf20Sopenharmony_ci		break;
11378c2ecf20Sopenharmony_ci
11388c2ecf20Sopenharmony_ci	case BPF_JMP | BPF_CALL:
11398c2ecf20Sopenharmony_ci	{
11408c2ecf20Sopenharmony_ci		bool fixed;
11418c2ecf20Sopenharmony_ci		int ret;
11428c2ecf20Sopenharmony_ci		u64 addr;
11438c2ecf20Sopenharmony_ci
11448c2ecf20Sopenharmony_ci		ret = bpf_jit_get_func_addr(ctx->prog, insn, extra_pass, &addr,
11458c2ecf20Sopenharmony_ci					    &fixed);
11468c2ecf20Sopenharmony_ci		if (ret < 0)
11478c2ecf20Sopenharmony_ci			return ret;
11488c2ecf20Sopenharmony_ci		emit_call(fixed, addr, ctx);
11498c2ecf20Sopenharmony_ci		break;
11508c2ecf20Sopenharmony_ci	}
11518c2ecf20Sopenharmony_ci
11528c2ecf20Sopenharmony_ci	case BPF_JMP | BPF_TAIL_CALL:
11538c2ecf20Sopenharmony_ci		if (emit_bpf_tail_call(i, ctx))
11548c2ecf20Sopenharmony_ci			return -1;
11558c2ecf20Sopenharmony_ci		break;
11568c2ecf20Sopenharmony_ci
11578c2ecf20Sopenharmony_ci	case BPF_JMP | BPF_JEQ | BPF_X:
11588c2ecf20Sopenharmony_ci	case BPF_JMP | BPF_JEQ | BPF_K:
11598c2ecf20Sopenharmony_ci	case BPF_JMP32 | BPF_JEQ | BPF_X:
11608c2ecf20Sopenharmony_ci	case BPF_JMP32 | BPF_JEQ | BPF_K:
11618c2ecf20Sopenharmony_ci
11628c2ecf20Sopenharmony_ci	case BPF_JMP | BPF_JNE | BPF_X:
11638c2ecf20Sopenharmony_ci	case BPF_JMP | BPF_JNE | BPF_K:
11648c2ecf20Sopenharmony_ci	case BPF_JMP32 | BPF_JNE | BPF_X:
11658c2ecf20Sopenharmony_ci	case BPF_JMP32 | BPF_JNE | BPF_K:
11668c2ecf20Sopenharmony_ci
11678c2ecf20Sopenharmony_ci	case BPF_JMP | BPF_JLE | BPF_X:
11688c2ecf20Sopenharmony_ci	case BPF_JMP | BPF_JLE | BPF_K:
11698c2ecf20Sopenharmony_ci	case BPF_JMP32 | BPF_JLE | BPF_X:
11708c2ecf20Sopenharmony_ci	case BPF_JMP32 | BPF_JLE | BPF_K:
11718c2ecf20Sopenharmony_ci
11728c2ecf20Sopenharmony_ci	case BPF_JMP | BPF_JLT | BPF_X:
11738c2ecf20Sopenharmony_ci	case BPF_JMP | BPF_JLT | BPF_K:
11748c2ecf20Sopenharmony_ci	case BPF_JMP32 | BPF_JLT | BPF_X:
11758c2ecf20Sopenharmony_ci	case BPF_JMP32 | BPF_JLT | BPF_K:
11768c2ecf20Sopenharmony_ci
11778c2ecf20Sopenharmony_ci	case BPF_JMP | BPF_JGE | BPF_X:
11788c2ecf20Sopenharmony_ci	case BPF_JMP | BPF_JGE | BPF_K:
11798c2ecf20Sopenharmony_ci	case BPF_JMP32 | BPF_JGE | BPF_X:
11808c2ecf20Sopenharmony_ci	case BPF_JMP32 | BPF_JGE | BPF_K:
11818c2ecf20Sopenharmony_ci
11828c2ecf20Sopenharmony_ci	case BPF_JMP | BPF_JGT | BPF_X:
11838c2ecf20Sopenharmony_ci	case BPF_JMP | BPF_JGT | BPF_K:
11848c2ecf20Sopenharmony_ci	case BPF_JMP32 | BPF_JGT | BPF_X:
11858c2ecf20Sopenharmony_ci	case BPF_JMP32 | BPF_JGT | BPF_K:
11868c2ecf20Sopenharmony_ci
11878c2ecf20Sopenharmony_ci	case BPF_JMP | BPF_JSLE | BPF_X:
11888c2ecf20Sopenharmony_ci	case BPF_JMP | BPF_JSLE | BPF_K:
11898c2ecf20Sopenharmony_ci	case BPF_JMP32 | BPF_JSLE | BPF_X:
11908c2ecf20Sopenharmony_ci	case BPF_JMP32 | BPF_JSLE | BPF_K:
11918c2ecf20Sopenharmony_ci
11928c2ecf20Sopenharmony_ci	case BPF_JMP | BPF_JSLT | BPF_X:
11938c2ecf20Sopenharmony_ci	case BPF_JMP | BPF_JSLT | BPF_K:
11948c2ecf20Sopenharmony_ci	case BPF_JMP32 | BPF_JSLT | BPF_X:
11958c2ecf20Sopenharmony_ci	case BPF_JMP32 | BPF_JSLT | BPF_K:
11968c2ecf20Sopenharmony_ci
11978c2ecf20Sopenharmony_ci	case BPF_JMP | BPF_JSGE | BPF_X:
11988c2ecf20Sopenharmony_ci	case BPF_JMP | BPF_JSGE | BPF_K:
11998c2ecf20Sopenharmony_ci	case BPF_JMP32 | BPF_JSGE | BPF_X:
12008c2ecf20Sopenharmony_ci	case BPF_JMP32 | BPF_JSGE | BPF_K:
12018c2ecf20Sopenharmony_ci
12028c2ecf20Sopenharmony_ci	case BPF_JMP | BPF_JSGT | BPF_X:
12038c2ecf20Sopenharmony_ci	case BPF_JMP | BPF_JSGT | BPF_K:
12048c2ecf20Sopenharmony_ci	case BPF_JMP32 | BPF_JSGT | BPF_X:
12058c2ecf20Sopenharmony_ci	case BPF_JMP32 | BPF_JSGT | BPF_K:
12068c2ecf20Sopenharmony_ci
12078c2ecf20Sopenharmony_ci	case BPF_JMP | BPF_JSET | BPF_X:
12088c2ecf20Sopenharmony_ci	case BPF_JMP | BPF_JSET | BPF_K:
12098c2ecf20Sopenharmony_ci	case BPF_JMP32 | BPF_JSET | BPF_X:
12108c2ecf20Sopenharmony_ci	case BPF_JMP32 | BPF_JSET | BPF_K:
12118c2ecf20Sopenharmony_ci		rvoff = rv_offset(i, off, ctx);
12128c2ecf20Sopenharmony_ci		if (BPF_SRC(code) == BPF_K) {
12138c2ecf20Sopenharmony_ci			s = ctx->ninsns;
12148c2ecf20Sopenharmony_ci			emit_imm32(tmp2, imm, ctx);
12158c2ecf20Sopenharmony_ci			src = tmp2;
12168c2ecf20Sopenharmony_ci			e = ctx->ninsns;
12178c2ecf20Sopenharmony_ci			rvoff -= ninsns_rvoff(e - s);
12188c2ecf20Sopenharmony_ci		}
12198c2ecf20Sopenharmony_ci
12208c2ecf20Sopenharmony_ci		if (is64)
12218c2ecf20Sopenharmony_ci			emit_branch_r64(dst, src, rvoff, ctx, BPF_OP(code));
12228c2ecf20Sopenharmony_ci		else
12238c2ecf20Sopenharmony_ci			emit_branch_r32(dst, src, rvoff, ctx, BPF_OP(code));
12248c2ecf20Sopenharmony_ci		break;
12258c2ecf20Sopenharmony_ci
12268c2ecf20Sopenharmony_ci	case BPF_JMP | BPF_EXIT:
12278c2ecf20Sopenharmony_ci		if (i == ctx->prog->len - 1)
12288c2ecf20Sopenharmony_ci			break;
12298c2ecf20Sopenharmony_ci
12308c2ecf20Sopenharmony_ci		rvoff = epilogue_offset(ctx);
12318c2ecf20Sopenharmony_ci		emit_jump_and_link(RV_REG_ZERO, rvoff, false, ctx);
12328c2ecf20Sopenharmony_ci		break;
12338c2ecf20Sopenharmony_ci
12348c2ecf20Sopenharmony_ci	case BPF_LD | BPF_IMM | BPF_DW:
12358c2ecf20Sopenharmony_ci	{
12368c2ecf20Sopenharmony_ci		struct bpf_insn insn1 = insn[1];
12378c2ecf20Sopenharmony_ci		s32 imm_lo = imm;
12388c2ecf20Sopenharmony_ci		s32 imm_hi = insn1.imm;
12398c2ecf20Sopenharmony_ci		const s8 *rd = bpf_get_reg64(dst, tmp1, ctx);
12408c2ecf20Sopenharmony_ci
12418c2ecf20Sopenharmony_ci		emit_imm64(rd, imm_hi, imm_lo, ctx);
12428c2ecf20Sopenharmony_ci		bpf_put_reg64(dst, rd, ctx);
12438c2ecf20Sopenharmony_ci		return 1;
12448c2ecf20Sopenharmony_ci	}
12458c2ecf20Sopenharmony_ci
12468c2ecf20Sopenharmony_ci	case BPF_LDX | BPF_MEM | BPF_B:
12478c2ecf20Sopenharmony_ci	case BPF_LDX | BPF_MEM | BPF_H:
12488c2ecf20Sopenharmony_ci	case BPF_LDX | BPF_MEM | BPF_W:
12498c2ecf20Sopenharmony_ci	case BPF_LDX | BPF_MEM | BPF_DW:
12508c2ecf20Sopenharmony_ci		if (emit_load_r64(dst, src, off, ctx, BPF_SIZE(code)))
12518c2ecf20Sopenharmony_ci			return -1;
12528c2ecf20Sopenharmony_ci		break;
12538c2ecf20Sopenharmony_ci
12548c2ecf20Sopenharmony_ci	/* speculation barrier */
12558c2ecf20Sopenharmony_ci	case BPF_ST | BPF_NOSPEC:
12568c2ecf20Sopenharmony_ci		break;
12578c2ecf20Sopenharmony_ci
12588c2ecf20Sopenharmony_ci	case BPF_ST | BPF_MEM | BPF_B:
12598c2ecf20Sopenharmony_ci	case BPF_ST | BPF_MEM | BPF_H:
12608c2ecf20Sopenharmony_ci	case BPF_ST | BPF_MEM | BPF_W:
12618c2ecf20Sopenharmony_ci	case BPF_ST | BPF_MEM | BPF_DW:
12628c2ecf20Sopenharmony_ci
12638c2ecf20Sopenharmony_ci	case BPF_STX | BPF_MEM | BPF_B:
12648c2ecf20Sopenharmony_ci	case BPF_STX | BPF_MEM | BPF_H:
12658c2ecf20Sopenharmony_ci	case BPF_STX | BPF_MEM | BPF_W:
12668c2ecf20Sopenharmony_ci	case BPF_STX | BPF_MEM | BPF_DW:
12678c2ecf20Sopenharmony_ci	case BPF_STX | BPF_XADD | BPF_W:
12688c2ecf20Sopenharmony_ci		if (BPF_CLASS(code) == BPF_ST) {
12698c2ecf20Sopenharmony_ci			emit_imm32(tmp2, imm, ctx);
12708c2ecf20Sopenharmony_ci			src = tmp2;
12718c2ecf20Sopenharmony_ci		}
12728c2ecf20Sopenharmony_ci
12738c2ecf20Sopenharmony_ci		if (emit_store_r64(dst, src, off, ctx, BPF_SIZE(code),
12748c2ecf20Sopenharmony_ci				   BPF_MODE(code)))
12758c2ecf20Sopenharmony_ci			return -1;
12768c2ecf20Sopenharmony_ci		break;
12778c2ecf20Sopenharmony_ci
12788c2ecf20Sopenharmony_ci	/* No hardware support for 8-byte atomics in RV32. */
12798c2ecf20Sopenharmony_ci	case BPF_STX | BPF_XADD | BPF_DW:
12808c2ecf20Sopenharmony_ci		/* Fallthrough. */
12818c2ecf20Sopenharmony_ci
12828c2ecf20Sopenharmony_cinotsupported:
12838c2ecf20Sopenharmony_ci		pr_info_once("bpf-jit: not supported: opcode %02x ***\n", code);
12848c2ecf20Sopenharmony_ci		return -EFAULT;
12858c2ecf20Sopenharmony_ci
12868c2ecf20Sopenharmony_ci	default:
12878c2ecf20Sopenharmony_ci		pr_err("bpf-jit: unknown opcode %02x\n", code);
12888c2ecf20Sopenharmony_ci		return -EINVAL;
12898c2ecf20Sopenharmony_ci	}
12908c2ecf20Sopenharmony_ci
12918c2ecf20Sopenharmony_ci	return 0;
12928c2ecf20Sopenharmony_ci}
12938c2ecf20Sopenharmony_ci
12948c2ecf20Sopenharmony_civoid bpf_jit_build_prologue(struct rv_jit_context *ctx)
12958c2ecf20Sopenharmony_ci{
12968c2ecf20Sopenharmony_ci	const s8 *fp = bpf2rv32[BPF_REG_FP];
12978c2ecf20Sopenharmony_ci	const s8 *r1 = bpf2rv32[BPF_REG_1];
12988c2ecf20Sopenharmony_ci	int stack_adjust = 0;
12998c2ecf20Sopenharmony_ci	int bpf_stack_adjust =
13008c2ecf20Sopenharmony_ci		round_up(ctx->prog->aux->stack_depth, STACK_ALIGN);
13018c2ecf20Sopenharmony_ci
13028c2ecf20Sopenharmony_ci	/* Make space for callee-saved registers. */
13038c2ecf20Sopenharmony_ci	stack_adjust += NR_SAVED_REGISTERS * sizeof(u32);
13048c2ecf20Sopenharmony_ci	/* Make space for BPF registers on stack. */
13058c2ecf20Sopenharmony_ci	stack_adjust += BPF_JIT_SCRATCH_REGS * sizeof(u32);
13068c2ecf20Sopenharmony_ci	/* Make space for BPF stack. */
13078c2ecf20Sopenharmony_ci	stack_adjust += bpf_stack_adjust;
13088c2ecf20Sopenharmony_ci	/* Round up for stack alignment. */
13098c2ecf20Sopenharmony_ci	stack_adjust = round_up(stack_adjust, STACK_ALIGN);
13108c2ecf20Sopenharmony_ci
13118c2ecf20Sopenharmony_ci	/*
13128c2ecf20Sopenharmony_ci	 * The first instruction sets the tail-call-counter (TCC) register.
13138c2ecf20Sopenharmony_ci	 * This instruction is skipped by tail calls.
13148c2ecf20Sopenharmony_ci	 */
13158c2ecf20Sopenharmony_ci	emit(rv_addi(RV_REG_TCC, RV_REG_ZERO, MAX_TAIL_CALL_CNT), ctx);
13168c2ecf20Sopenharmony_ci
13178c2ecf20Sopenharmony_ci	emit(rv_addi(RV_REG_SP, RV_REG_SP, -stack_adjust), ctx);
13188c2ecf20Sopenharmony_ci
13198c2ecf20Sopenharmony_ci	/* Save callee-save registers. */
13208c2ecf20Sopenharmony_ci	emit(rv_sw(RV_REG_SP, stack_adjust - 4, RV_REG_RA), ctx);
13218c2ecf20Sopenharmony_ci	emit(rv_sw(RV_REG_SP, stack_adjust - 8, RV_REG_FP), ctx);
13228c2ecf20Sopenharmony_ci	emit(rv_sw(RV_REG_SP, stack_adjust - 12, RV_REG_S1), ctx);
13238c2ecf20Sopenharmony_ci	emit(rv_sw(RV_REG_SP, stack_adjust - 16, RV_REG_S2), ctx);
13248c2ecf20Sopenharmony_ci	emit(rv_sw(RV_REG_SP, stack_adjust - 20, RV_REG_S3), ctx);
13258c2ecf20Sopenharmony_ci	emit(rv_sw(RV_REG_SP, stack_adjust - 24, RV_REG_S4), ctx);
13268c2ecf20Sopenharmony_ci	emit(rv_sw(RV_REG_SP, stack_adjust - 28, RV_REG_S5), ctx);
13278c2ecf20Sopenharmony_ci	emit(rv_sw(RV_REG_SP, stack_adjust - 32, RV_REG_S6), ctx);
13288c2ecf20Sopenharmony_ci	emit(rv_sw(RV_REG_SP, stack_adjust - 36, RV_REG_S7), ctx);
13298c2ecf20Sopenharmony_ci
13308c2ecf20Sopenharmony_ci	/* Set fp: used as the base address for stacked BPF registers. */
13318c2ecf20Sopenharmony_ci	emit(rv_addi(RV_REG_FP, RV_REG_SP, stack_adjust), ctx);
13328c2ecf20Sopenharmony_ci
13338c2ecf20Sopenharmony_ci	/* Set up BPF frame pointer. */
13348c2ecf20Sopenharmony_ci	emit(rv_addi(lo(fp), RV_REG_SP, bpf_stack_adjust), ctx);
13358c2ecf20Sopenharmony_ci	emit(rv_addi(hi(fp), RV_REG_ZERO, 0), ctx);
13368c2ecf20Sopenharmony_ci
13378c2ecf20Sopenharmony_ci	/* Set up BPF context pointer. */
13388c2ecf20Sopenharmony_ci	emit(rv_addi(lo(r1), RV_REG_A0, 0), ctx);
13398c2ecf20Sopenharmony_ci	emit(rv_addi(hi(r1), RV_REG_ZERO, 0), ctx);
13408c2ecf20Sopenharmony_ci
13418c2ecf20Sopenharmony_ci	ctx->stack_size = stack_adjust;
13428c2ecf20Sopenharmony_ci}
13438c2ecf20Sopenharmony_ci
13448c2ecf20Sopenharmony_civoid bpf_jit_build_epilogue(struct rv_jit_context *ctx)
13458c2ecf20Sopenharmony_ci{
13468c2ecf20Sopenharmony_ci	__build_epilogue(false, ctx);
13478c2ecf20Sopenharmony_ci}
1348