162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * eBPF JIT compiler for PPC32
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright 2020 Christophe Leroy <christophe.leroy@csgroup.eu>
662306a36Sopenharmony_ci *		  CS GROUP France
762306a36Sopenharmony_ci *
862306a36Sopenharmony_ci * Based on PPC64 eBPF JIT compiler by Naveen N. Rao
962306a36Sopenharmony_ci */
1062306a36Sopenharmony_ci#include <linux/moduleloader.h>
1162306a36Sopenharmony_ci#include <asm/cacheflush.h>
1262306a36Sopenharmony_ci#include <asm/asm-compat.h>
1362306a36Sopenharmony_ci#include <linux/netdevice.h>
1462306a36Sopenharmony_ci#include <linux/filter.h>
1562306a36Sopenharmony_ci#include <linux/if_vlan.h>
1662306a36Sopenharmony_ci#include <asm/kprobes.h>
1762306a36Sopenharmony_ci#include <linux/bpf.h>
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ci#include "bpf_jit.h"
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ci/*
2262306a36Sopenharmony_ci * Stack layout:
2362306a36Sopenharmony_ci *
2462306a36Sopenharmony_ci *		[	prev sp		] <-------------
2562306a36Sopenharmony_ci *		[   nv gpr save area	] 16 * 4	|
2662306a36Sopenharmony_ci * fp (r31) -->	[   ebpf stack space	] upto 512	|
2762306a36Sopenharmony_ci *		[     frame header	] 16		|
2862306a36Sopenharmony_ci * sp (r1) --->	[    stack pointer	] --------------
2962306a36Sopenharmony_ci */
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ci/* for gpr non volatile registers r17 to r31 (14) + tail call */
3262306a36Sopenharmony_ci#define BPF_PPC_STACK_SAVE	(15 * 4 + 4)
3362306a36Sopenharmony_ci/* stack frame, ensure this is quadword aligned */
3462306a36Sopenharmony_ci#define BPF_PPC_STACKFRAME(ctx)	(STACK_FRAME_MIN_SIZE + BPF_PPC_STACK_SAVE + (ctx)->stack_size)
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ci#define PPC_EX32(r, i)		EMIT(PPC_RAW_LI((r), (i) < 0 ? -1 : 0))
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci/* PPC NVR range -- update this if we ever use NVRs below r17 */
3962306a36Sopenharmony_ci#define BPF_PPC_NVR_MIN		_R17
4062306a36Sopenharmony_ci#define BPF_PPC_TC		_R16
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ci/* BPF register usage */
4362306a36Sopenharmony_ci#define TMP_REG			(MAX_BPF_JIT_REG + 0)
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_ci/* BPF to ppc register mappings */
4662306a36Sopenharmony_civoid bpf_jit_init_reg_mapping(struct codegen_context *ctx)
4762306a36Sopenharmony_ci{
4862306a36Sopenharmony_ci	/* function return value */
4962306a36Sopenharmony_ci	ctx->b2p[BPF_REG_0] = _R12;
5062306a36Sopenharmony_ci	/* function arguments */
5162306a36Sopenharmony_ci	ctx->b2p[BPF_REG_1] = _R4;
5262306a36Sopenharmony_ci	ctx->b2p[BPF_REG_2] = _R6;
5362306a36Sopenharmony_ci	ctx->b2p[BPF_REG_3] = _R8;
5462306a36Sopenharmony_ci	ctx->b2p[BPF_REG_4] = _R10;
5562306a36Sopenharmony_ci	ctx->b2p[BPF_REG_5] = _R22;
5662306a36Sopenharmony_ci	/* non volatile registers */
5762306a36Sopenharmony_ci	ctx->b2p[BPF_REG_6] = _R24;
5862306a36Sopenharmony_ci	ctx->b2p[BPF_REG_7] = _R26;
5962306a36Sopenharmony_ci	ctx->b2p[BPF_REG_8] = _R28;
6062306a36Sopenharmony_ci	ctx->b2p[BPF_REG_9] = _R30;
6162306a36Sopenharmony_ci	/* frame pointer aka BPF_REG_10 */
6262306a36Sopenharmony_ci	ctx->b2p[BPF_REG_FP] = _R18;
6362306a36Sopenharmony_ci	/* eBPF jit internal registers */
6462306a36Sopenharmony_ci	ctx->b2p[BPF_REG_AX] = _R20;
6562306a36Sopenharmony_ci	ctx->b2p[TMP_REG] = _R31;		/* 32 bits */
6662306a36Sopenharmony_ci}
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_cistatic int bpf_jit_stack_offsetof(struct codegen_context *ctx, int reg)
6962306a36Sopenharmony_ci{
7062306a36Sopenharmony_ci	if ((reg >= BPF_PPC_NVR_MIN && reg < 32) || reg == BPF_PPC_TC)
7162306a36Sopenharmony_ci		return BPF_PPC_STACKFRAME(ctx) - 4 * (32 - reg);
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_ci	WARN(true, "BPF JIT is asking about unknown registers, will crash the stack");
7462306a36Sopenharmony_ci	/* Use the hole we have left for alignment */
7562306a36Sopenharmony_ci	return BPF_PPC_STACKFRAME(ctx) - 4;
7662306a36Sopenharmony_ci}
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_ci#define SEEN_VREG_MASK		0x1ff80000 /* Volatile registers r3-r12 */
7962306a36Sopenharmony_ci#define SEEN_NVREG_FULL_MASK	0x0003ffff /* Non volatile registers r14-r31 */
8062306a36Sopenharmony_ci#define SEEN_NVREG_TEMP_MASK	0x00001e01 /* BPF_REG_5, BPF_REG_AX, TMP_REG */
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_cistatic inline bool bpf_has_stack_frame(struct codegen_context *ctx)
8362306a36Sopenharmony_ci{
8462306a36Sopenharmony_ci	/*
8562306a36Sopenharmony_ci	 * We only need a stack frame if:
8662306a36Sopenharmony_ci	 * - we call other functions (kernel helpers), or
8762306a36Sopenharmony_ci	 * - we use non volatile registers, or
8862306a36Sopenharmony_ci	 * - we use tail call counter
8962306a36Sopenharmony_ci	 * - the bpf program uses its stack area
9062306a36Sopenharmony_ci	 * The latter condition is deduced from the usage of BPF_REG_FP
9162306a36Sopenharmony_ci	 */
9262306a36Sopenharmony_ci	return ctx->seen & (SEEN_FUNC | SEEN_TAILCALL | SEEN_NVREG_FULL_MASK) ||
9362306a36Sopenharmony_ci	       bpf_is_seen_register(ctx, bpf_to_ppc(BPF_REG_FP));
9462306a36Sopenharmony_ci}
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_civoid bpf_jit_realloc_regs(struct codegen_context *ctx)
9762306a36Sopenharmony_ci{
9862306a36Sopenharmony_ci	unsigned int nvreg_mask;
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_ci	if (ctx->seen & SEEN_FUNC)
10162306a36Sopenharmony_ci		nvreg_mask = SEEN_NVREG_TEMP_MASK;
10262306a36Sopenharmony_ci	else
10362306a36Sopenharmony_ci		nvreg_mask = SEEN_NVREG_FULL_MASK;
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ci	while (ctx->seen & nvreg_mask &&
10662306a36Sopenharmony_ci	      (ctx->seen & SEEN_VREG_MASK) != SEEN_VREG_MASK) {
10762306a36Sopenharmony_ci		int old = 32 - fls(ctx->seen & (nvreg_mask & 0xaaaaaaab));
10862306a36Sopenharmony_ci		int new = 32 - fls(~ctx->seen & (SEEN_VREG_MASK & 0xaaaaaaaa));
10962306a36Sopenharmony_ci		int i;
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_ci		for (i = BPF_REG_0; i <= TMP_REG; i++) {
11262306a36Sopenharmony_ci			if (ctx->b2p[i] != old)
11362306a36Sopenharmony_ci				continue;
11462306a36Sopenharmony_ci			ctx->b2p[i] = new;
11562306a36Sopenharmony_ci			bpf_set_seen_register(ctx, new);
11662306a36Sopenharmony_ci			bpf_clear_seen_register(ctx, old);
11762306a36Sopenharmony_ci			if (i != TMP_REG) {
11862306a36Sopenharmony_ci				bpf_set_seen_register(ctx, new - 1);
11962306a36Sopenharmony_ci				bpf_clear_seen_register(ctx, old - 1);
12062306a36Sopenharmony_ci			}
12162306a36Sopenharmony_ci			break;
12262306a36Sopenharmony_ci		}
12362306a36Sopenharmony_ci	}
12462306a36Sopenharmony_ci}
12562306a36Sopenharmony_ci
12662306a36Sopenharmony_civoid bpf_jit_build_prologue(u32 *image, struct codegen_context *ctx)
12762306a36Sopenharmony_ci{
12862306a36Sopenharmony_ci	int i;
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_ci	/* Initialize tail_call_cnt, to be skipped if we do tail calls. */
13162306a36Sopenharmony_ci	if (ctx->seen & SEEN_TAILCALL)
13262306a36Sopenharmony_ci		EMIT(PPC_RAW_LI(_R4, 0));
13362306a36Sopenharmony_ci	else
13462306a36Sopenharmony_ci		EMIT(PPC_RAW_NOP());
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_ci#define BPF_TAILCALL_PROLOGUE_SIZE	4
13762306a36Sopenharmony_ci
13862306a36Sopenharmony_ci	if (bpf_has_stack_frame(ctx))
13962306a36Sopenharmony_ci		EMIT(PPC_RAW_STWU(_R1, _R1, -BPF_PPC_STACKFRAME(ctx)));
14062306a36Sopenharmony_ci
14162306a36Sopenharmony_ci	if (ctx->seen & SEEN_TAILCALL)
14262306a36Sopenharmony_ci		EMIT(PPC_RAW_STW(_R4, _R1, bpf_jit_stack_offsetof(ctx, BPF_PPC_TC)));
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_ci	/* First arg comes in as a 32 bits pointer. */
14562306a36Sopenharmony_ci	EMIT(PPC_RAW_MR(bpf_to_ppc(BPF_REG_1), _R3));
14662306a36Sopenharmony_ci	EMIT(PPC_RAW_LI(bpf_to_ppc(BPF_REG_1) - 1, 0));
14762306a36Sopenharmony_ci
14862306a36Sopenharmony_ci	/*
14962306a36Sopenharmony_ci	 * We need a stack frame, but we don't necessarily need to
15062306a36Sopenharmony_ci	 * save/restore LR unless we call other functions
15162306a36Sopenharmony_ci	 */
15262306a36Sopenharmony_ci	if (ctx->seen & SEEN_FUNC)
15362306a36Sopenharmony_ci		EMIT(PPC_RAW_MFLR(_R0));
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_ci	/*
15662306a36Sopenharmony_ci	 * Back up non-volatile regs -- registers r18-r31
15762306a36Sopenharmony_ci	 */
15862306a36Sopenharmony_ci	for (i = BPF_PPC_NVR_MIN; i <= 31; i++)
15962306a36Sopenharmony_ci		if (bpf_is_seen_register(ctx, i))
16062306a36Sopenharmony_ci			EMIT(PPC_RAW_STW(i, _R1, bpf_jit_stack_offsetof(ctx, i)));
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_ci	/* Setup frame pointer to point to the bpf stack area */
16362306a36Sopenharmony_ci	if (bpf_is_seen_register(ctx, bpf_to_ppc(BPF_REG_FP))) {
16462306a36Sopenharmony_ci		EMIT(PPC_RAW_LI(bpf_to_ppc(BPF_REG_FP) - 1, 0));
16562306a36Sopenharmony_ci		EMIT(PPC_RAW_ADDI(bpf_to_ppc(BPF_REG_FP), _R1,
16662306a36Sopenharmony_ci				  STACK_FRAME_MIN_SIZE + ctx->stack_size));
16762306a36Sopenharmony_ci	}
16862306a36Sopenharmony_ci
16962306a36Sopenharmony_ci	if (ctx->seen & SEEN_FUNC)
17062306a36Sopenharmony_ci		EMIT(PPC_RAW_STW(_R0, _R1, BPF_PPC_STACKFRAME(ctx) + PPC_LR_STKOFF));
17162306a36Sopenharmony_ci}
17262306a36Sopenharmony_ci
17362306a36Sopenharmony_cistatic void bpf_jit_emit_common_epilogue(u32 *image, struct codegen_context *ctx)
17462306a36Sopenharmony_ci{
17562306a36Sopenharmony_ci	int i;
17662306a36Sopenharmony_ci
17762306a36Sopenharmony_ci	/* Restore NVRs */
17862306a36Sopenharmony_ci	for (i = BPF_PPC_NVR_MIN; i <= 31; i++)
17962306a36Sopenharmony_ci		if (bpf_is_seen_register(ctx, i))
18062306a36Sopenharmony_ci			EMIT(PPC_RAW_LWZ(i, _R1, bpf_jit_stack_offsetof(ctx, i)));
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_ci	if (ctx->seen & SEEN_FUNC)
18362306a36Sopenharmony_ci		EMIT(PPC_RAW_LWZ(_R0, _R1, BPF_PPC_STACKFRAME(ctx) + PPC_LR_STKOFF));
18462306a36Sopenharmony_ci
18562306a36Sopenharmony_ci	/* Tear down our stack frame */
18662306a36Sopenharmony_ci	if (bpf_has_stack_frame(ctx))
18762306a36Sopenharmony_ci		EMIT(PPC_RAW_ADDI(_R1, _R1, BPF_PPC_STACKFRAME(ctx)));
18862306a36Sopenharmony_ci
18962306a36Sopenharmony_ci	if (ctx->seen & SEEN_FUNC)
19062306a36Sopenharmony_ci		EMIT(PPC_RAW_MTLR(_R0));
19162306a36Sopenharmony_ci
19262306a36Sopenharmony_ci}
19362306a36Sopenharmony_ci
19462306a36Sopenharmony_civoid bpf_jit_build_epilogue(u32 *image, struct codegen_context *ctx)
19562306a36Sopenharmony_ci{
19662306a36Sopenharmony_ci	EMIT(PPC_RAW_MR(_R3, bpf_to_ppc(BPF_REG_0)));
19762306a36Sopenharmony_ci
19862306a36Sopenharmony_ci	bpf_jit_emit_common_epilogue(image, ctx);
19962306a36Sopenharmony_ci
20062306a36Sopenharmony_ci	EMIT(PPC_RAW_BLR());
20162306a36Sopenharmony_ci}
20262306a36Sopenharmony_ci
20362306a36Sopenharmony_ciint bpf_jit_emit_func_call_rel(u32 *image, struct codegen_context *ctx, u64 func)
20462306a36Sopenharmony_ci{
20562306a36Sopenharmony_ci	s32 rel = (s32)func - (s32)(image + ctx->idx);
20662306a36Sopenharmony_ci
20762306a36Sopenharmony_ci	if (image && rel < 0x2000000 && rel >= -0x2000000) {
20862306a36Sopenharmony_ci		PPC_BL(func);
20962306a36Sopenharmony_ci	} else {
21062306a36Sopenharmony_ci		/* Load function address into r0 */
21162306a36Sopenharmony_ci		EMIT(PPC_RAW_LIS(_R0, IMM_H(func)));
21262306a36Sopenharmony_ci		EMIT(PPC_RAW_ORI(_R0, _R0, IMM_L(func)));
21362306a36Sopenharmony_ci		EMIT(PPC_RAW_MTCTR(_R0));
21462306a36Sopenharmony_ci		EMIT(PPC_RAW_BCTRL());
21562306a36Sopenharmony_ci	}
21662306a36Sopenharmony_ci
21762306a36Sopenharmony_ci	return 0;
21862306a36Sopenharmony_ci}
21962306a36Sopenharmony_ci
22062306a36Sopenharmony_cistatic int bpf_jit_emit_tail_call(u32 *image, struct codegen_context *ctx, u32 out)
22162306a36Sopenharmony_ci{
22262306a36Sopenharmony_ci	/*
22362306a36Sopenharmony_ci	 * By now, the eBPF program has already setup parameters in r3-r6
22462306a36Sopenharmony_ci	 * r3-r4/BPF_REG_1 - pointer to ctx -- passed as is to the next bpf program
22562306a36Sopenharmony_ci	 * r5-r6/BPF_REG_2 - pointer to bpf_array
22662306a36Sopenharmony_ci	 * r7-r8/BPF_REG_3 - index in bpf_array
22762306a36Sopenharmony_ci	 */
22862306a36Sopenharmony_ci	int b2p_bpf_array = bpf_to_ppc(BPF_REG_2);
22962306a36Sopenharmony_ci	int b2p_index = bpf_to_ppc(BPF_REG_3);
23062306a36Sopenharmony_ci
23162306a36Sopenharmony_ci	/*
23262306a36Sopenharmony_ci	 * if (index >= array->map.max_entries)
23362306a36Sopenharmony_ci	 *   goto out;
23462306a36Sopenharmony_ci	 */
23562306a36Sopenharmony_ci	EMIT(PPC_RAW_LWZ(_R0, b2p_bpf_array, offsetof(struct bpf_array, map.max_entries)));
23662306a36Sopenharmony_ci	EMIT(PPC_RAW_CMPLW(b2p_index, _R0));
23762306a36Sopenharmony_ci	EMIT(PPC_RAW_LWZ(_R0, _R1, bpf_jit_stack_offsetof(ctx, BPF_PPC_TC)));
23862306a36Sopenharmony_ci	PPC_BCC_SHORT(COND_GE, out);
23962306a36Sopenharmony_ci
24062306a36Sopenharmony_ci	/*
24162306a36Sopenharmony_ci	 * if (tail_call_cnt >= MAX_TAIL_CALL_CNT)
24262306a36Sopenharmony_ci	 *   goto out;
24362306a36Sopenharmony_ci	 */
24462306a36Sopenharmony_ci	EMIT(PPC_RAW_CMPLWI(_R0, MAX_TAIL_CALL_CNT));
24562306a36Sopenharmony_ci	/* tail_call_cnt++; */
24662306a36Sopenharmony_ci	EMIT(PPC_RAW_ADDIC(_R0, _R0, 1));
24762306a36Sopenharmony_ci	PPC_BCC_SHORT(COND_GE, out);
24862306a36Sopenharmony_ci
24962306a36Sopenharmony_ci	/* prog = array->ptrs[index]; */
25062306a36Sopenharmony_ci	EMIT(PPC_RAW_RLWINM(_R3, b2p_index, 2, 0, 29));
25162306a36Sopenharmony_ci	EMIT(PPC_RAW_ADD(_R3, _R3, b2p_bpf_array));
25262306a36Sopenharmony_ci	EMIT(PPC_RAW_LWZ(_R3, _R3, offsetof(struct bpf_array, ptrs)));
25362306a36Sopenharmony_ci
25462306a36Sopenharmony_ci	/*
25562306a36Sopenharmony_ci	 * if (prog == NULL)
25662306a36Sopenharmony_ci	 *   goto out;
25762306a36Sopenharmony_ci	 */
25862306a36Sopenharmony_ci	EMIT(PPC_RAW_CMPLWI(_R3, 0));
25962306a36Sopenharmony_ci	PPC_BCC_SHORT(COND_EQ, out);
26062306a36Sopenharmony_ci
26162306a36Sopenharmony_ci	/* goto *(prog->bpf_func + prologue_size); */
26262306a36Sopenharmony_ci	EMIT(PPC_RAW_LWZ(_R3, _R3, offsetof(struct bpf_prog, bpf_func)));
26362306a36Sopenharmony_ci	EMIT(PPC_RAW_ADDIC(_R3, _R3, BPF_TAILCALL_PROLOGUE_SIZE));
26462306a36Sopenharmony_ci	EMIT(PPC_RAW_MTCTR(_R3));
26562306a36Sopenharmony_ci
26662306a36Sopenharmony_ci	EMIT(PPC_RAW_MR(_R3, bpf_to_ppc(BPF_REG_1)));
26762306a36Sopenharmony_ci
26862306a36Sopenharmony_ci	/* Put tail_call_cnt in r4 */
26962306a36Sopenharmony_ci	EMIT(PPC_RAW_MR(_R4, _R0));
27062306a36Sopenharmony_ci
27162306a36Sopenharmony_ci	/* tear restore NVRs, ... */
27262306a36Sopenharmony_ci	bpf_jit_emit_common_epilogue(image, ctx);
27362306a36Sopenharmony_ci
27462306a36Sopenharmony_ci	EMIT(PPC_RAW_BCTR());
27562306a36Sopenharmony_ci
27662306a36Sopenharmony_ci	/* out: */
27762306a36Sopenharmony_ci	return 0;
27862306a36Sopenharmony_ci}
27962306a36Sopenharmony_ci
28062306a36Sopenharmony_ci/* Assemble the body code between the prologue & epilogue */
28162306a36Sopenharmony_ciint bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context *ctx,
28262306a36Sopenharmony_ci		       u32 *addrs, int pass, bool extra_pass)
28362306a36Sopenharmony_ci{
28462306a36Sopenharmony_ci	const struct bpf_insn *insn = fp->insnsi;
28562306a36Sopenharmony_ci	int flen = fp->len;
28662306a36Sopenharmony_ci	int i, ret;
28762306a36Sopenharmony_ci
28862306a36Sopenharmony_ci	/* Start of epilogue code - will only be valid 2nd pass onwards */
28962306a36Sopenharmony_ci	u32 exit_addr = addrs[flen];
29062306a36Sopenharmony_ci
29162306a36Sopenharmony_ci	for (i = 0; i < flen; i++) {
29262306a36Sopenharmony_ci		u32 code = insn[i].code;
29362306a36Sopenharmony_ci		u32 prevcode = i ? insn[i - 1].code : 0;
29462306a36Sopenharmony_ci		u32 dst_reg = bpf_to_ppc(insn[i].dst_reg);
29562306a36Sopenharmony_ci		u32 dst_reg_h = dst_reg - 1;
29662306a36Sopenharmony_ci		u32 src_reg = bpf_to_ppc(insn[i].src_reg);
29762306a36Sopenharmony_ci		u32 src_reg_h = src_reg - 1;
29862306a36Sopenharmony_ci		u32 src2_reg = dst_reg;
29962306a36Sopenharmony_ci		u32 src2_reg_h = dst_reg_h;
30062306a36Sopenharmony_ci		u32 ax_reg = bpf_to_ppc(BPF_REG_AX);
30162306a36Sopenharmony_ci		u32 tmp_reg = bpf_to_ppc(TMP_REG);
30262306a36Sopenharmony_ci		u32 size = BPF_SIZE(code);
30362306a36Sopenharmony_ci		u32 save_reg, ret_reg;
30462306a36Sopenharmony_ci		s16 off = insn[i].off;
30562306a36Sopenharmony_ci		s32 imm = insn[i].imm;
30662306a36Sopenharmony_ci		bool func_addr_fixed;
30762306a36Sopenharmony_ci		u64 func_addr;
30862306a36Sopenharmony_ci		u32 true_cond;
30962306a36Sopenharmony_ci		u32 tmp_idx;
31062306a36Sopenharmony_ci		int j;
31162306a36Sopenharmony_ci
31262306a36Sopenharmony_ci		if (i && (BPF_CLASS(code) == BPF_ALU64 || BPF_CLASS(code) == BPF_ALU) &&
31362306a36Sopenharmony_ci		    (BPF_CLASS(prevcode) == BPF_ALU64 || BPF_CLASS(prevcode) == BPF_ALU) &&
31462306a36Sopenharmony_ci		    BPF_OP(prevcode) == BPF_MOV && BPF_SRC(prevcode) == BPF_X &&
31562306a36Sopenharmony_ci		    insn[i - 1].dst_reg == insn[i].dst_reg && insn[i - 1].imm != 1) {
31662306a36Sopenharmony_ci			src2_reg = bpf_to_ppc(insn[i - 1].src_reg);
31762306a36Sopenharmony_ci			src2_reg_h = src2_reg - 1;
31862306a36Sopenharmony_ci			ctx->idx = addrs[i - 1] / 4;
31962306a36Sopenharmony_ci		}
32062306a36Sopenharmony_ci
32162306a36Sopenharmony_ci		/*
32262306a36Sopenharmony_ci		 * addrs[] maps a BPF bytecode address into a real offset from
32362306a36Sopenharmony_ci		 * the start of the body code.
32462306a36Sopenharmony_ci		 */
32562306a36Sopenharmony_ci		addrs[i] = ctx->idx * 4;
32662306a36Sopenharmony_ci
32762306a36Sopenharmony_ci		/*
32862306a36Sopenharmony_ci		 * As an optimization, we note down which registers
32962306a36Sopenharmony_ci		 * are used so that we can only save/restore those in our
33062306a36Sopenharmony_ci		 * prologue and epilogue. We do this here regardless of whether
33162306a36Sopenharmony_ci		 * the actual BPF instruction uses src/dst registers or not
33262306a36Sopenharmony_ci		 * (for instance, BPF_CALL does not use them). The expectation
33362306a36Sopenharmony_ci		 * is that those instructions will have src_reg/dst_reg set to
33462306a36Sopenharmony_ci		 * 0. Even otherwise, we just lose some prologue/epilogue
33562306a36Sopenharmony_ci		 * optimization but everything else should work without
33662306a36Sopenharmony_ci		 * any issues.
33762306a36Sopenharmony_ci		 */
33862306a36Sopenharmony_ci		if (dst_reg >= 3 && dst_reg < 32) {
33962306a36Sopenharmony_ci			bpf_set_seen_register(ctx, dst_reg);
34062306a36Sopenharmony_ci			bpf_set_seen_register(ctx, dst_reg_h);
34162306a36Sopenharmony_ci		}
34262306a36Sopenharmony_ci
34362306a36Sopenharmony_ci		if (src_reg >= 3 && src_reg < 32) {
34462306a36Sopenharmony_ci			bpf_set_seen_register(ctx, src_reg);
34562306a36Sopenharmony_ci			bpf_set_seen_register(ctx, src_reg_h);
34662306a36Sopenharmony_ci		}
34762306a36Sopenharmony_ci
34862306a36Sopenharmony_ci		switch (code) {
34962306a36Sopenharmony_ci		/*
35062306a36Sopenharmony_ci		 * Arithmetic operations: ADD/SUB/MUL/DIV/MOD/NEG
35162306a36Sopenharmony_ci		 */
35262306a36Sopenharmony_ci		case BPF_ALU | BPF_ADD | BPF_X: /* (u32) dst += (u32) src */
35362306a36Sopenharmony_ci			EMIT(PPC_RAW_ADD(dst_reg, src2_reg, src_reg));
35462306a36Sopenharmony_ci			break;
35562306a36Sopenharmony_ci		case BPF_ALU64 | BPF_ADD | BPF_X: /* dst += src */
35662306a36Sopenharmony_ci			EMIT(PPC_RAW_ADDC(dst_reg, src2_reg, src_reg));
35762306a36Sopenharmony_ci			EMIT(PPC_RAW_ADDE(dst_reg_h, src2_reg_h, src_reg_h));
35862306a36Sopenharmony_ci			break;
35962306a36Sopenharmony_ci		case BPF_ALU | BPF_SUB | BPF_X: /* (u32) dst -= (u32) src */
36062306a36Sopenharmony_ci			EMIT(PPC_RAW_SUB(dst_reg, src2_reg, src_reg));
36162306a36Sopenharmony_ci			break;
36262306a36Sopenharmony_ci		case BPF_ALU64 | BPF_SUB | BPF_X: /* dst -= src */
36362306a36Sopenharmony_ci			EMIT(PPC_RAW_SUBFC(dst_reg, src_reg, src2_reg));
36462306a36Sopenharmony_ci			EMIT(PPC_RAW_SUBFE(dst_reg_h, src_reg_h, src2_reg_h));
36562306a36Sopenharmony_ci			break;
36662306a36Sopenharmony_ci		case BPF_ALU | BPF_SUB | BPF_K: /* (u32) dst -= (u32) imm */
36762306a36Sopenharmony_ci			imm = -imm;
36862306a36Sopenharmony_ci			fallthrough;
36962306a36Sopenharmony_ci		case BPF_ALU | BPF_ADD | BPF_K: /* (u32) dst += (u32) imm */
37062306a36Sopenharmony_ci			if (!imm) {
37162306a36Sopenharmony_ci				EMIT(PPC_RAW_MR(dst_reg, src2_reg));
37262306a36Sopenharmony_ci			} else if (IMM_HA(imm) & 0xffff) {
37362306a36Sopenharmony_ci				EMIT(PPC_RAW_ADDIS(dst_reg, src2_reg, IMM_HA(imm)));
37462306a36Sopenharmony_ci				src2_reg = dst_reg;
37562306a36Sopenharmony_ci			}
37662306a36Sopenharmony_ci			if (IMM_L(imm))
37762306a36Sopenharmony_ci				EMIT(PPC_RAW_ADDI(dst_reg, src2_reg, IMM_L(imm)));
37862306a36Sopenharmony_ci			break;
37962306a36Sopenharmony_ci		case BPF_ALU64 | BPF_SUB | BPF_K: /* dst -= imm */
38062306a36Sopenharmony_ci			imm = -imm;
38162306a36Sopenharmony_ci			fallthrough;
38262306a36Sopenharmony_ci		case BPF_ALU64 | BPF_ADD | BPF_K: /* dst += imm */
38362306a36Sopenharmony_ci			if (!imm) {
38462306a36Sopenharmony_ci				EMIT(PPC_RAW_MR(dst_reg, src2_reg));
38562306a36Sopenharmony_ci				EMIT(PPC_RAW_MR(dst_reg_h, src2_reg_h));
38662306a36Sopenharmony_ci				break;
38762306a36Sopenharmony_ci			}
38862306a36Sopenharmony_ci			if (imm >= -32768 && imm < 32768) {
38962306a36Sopenharmony_ci				EMIT(PPC_RAW_ADDIC(dst_reg, src2_reg, imm));
39062306a36Sopenharmony_ci			} else {
39162306a36Sopenharmony_ci				PPC_LI32(_R0, imm);
39262306a36Sopenharmony_ci				EMIT(PPC_RAW_ADDC(dst_reg, src2_reg, _R0));
39362306a36Sopenharmony_ci			}
39462306a36Sopenharmony_ci			if (imm >= 0 || (BPF_OP(code) == BPF_SUB && imm == 0x80000000))
39562306a36Sopenharmony_ci				EMIT(PPC_RAW_ADDZE(dst_reg_h, src2_reg_h));
39662306a36Sopenharmony_ci			else
39762306a36Sopenharmony_ci				EMIT(PPC_RAW_ADDME(dst_reg_h, src2_reg_h));
39862306a36Sopenharmony_ci			break;
39962306a36Sopenharmony_ci		case BPF_ALU64 | BPF_MUL | BPF_X: /* dst *= src */
40062306a36Sopenharmony_ci			bpf_set_seen_register(ctx, tmp_reg);
40162306a36Sopenharmony_ci			EMIT(PPC_RAW_MULW(_R0, src2_reg, src_reg_h));
40262306a36Sopenharmony_ci			EMIT(PPC_RAW_MULW(dst_reg_h, src2_reg_h, src_reg));
40362306a36Sopenharmony_ci			EMIT(PPC_RAW_MULHWU(tmp_reg, src2_reg, src_reg));
40462306a36Sopenharmony_ci			EMIT(PPC_RAW_MULW(dst_reg, src2_reg, src_reg));
40562306a36Sopenharmony_ci			EMIT(PPC_RAW_ADD(dst_reg_h, dst_reg_h, _R0));
40662306a36Sopenharmony_ci			EMIT(PPC_RAW_ADD(dst_reg_h, dst_reg_h, tmp_reg));
40762306a36Sopenharmony_ci			break;
40862306a36Sopenharmony_ci		case BPF_ALU | BPF_MUL | BPF_X: /* (u32) dst *= (u32) src */
40962306a36Sopenharmony_ci			EMIT(PPC_RAW_MULW(dst_reg, src2_reg, src_reg));
41062306a36Sopenharmony_ci			break;
41162306a36Sopenharmony_ci		case BPF_ALU | BPF_MUL | BPF_K: /* (u32) dst *= (u32) imm */
41262306a36Sopenharmony_ci			if (imm == 1) {
41362306a36Sopenharmony_ci				EMIT(PPC_RAW_MR(dst_reg, src2_reg));
41462306a36Sopenharmony_ci			} else if (imm == -1) {
41562306a36Sopenharmony_ci				EMIT(PPC_RAW_SUBFIC(dst_reg, src2_reg, 0));
41662306a36Sopenharmony_ci			} else if (is_power_of_2((u32)imm)) {
41762306a36Sopenharmony_ci				EMIT(PPC_RAW_SLWI(dst_reg, src2_reg, ilog2(imm)));
41862306a36Sopenharmony_ci			} else if (imm >= -32768 && imm < 32768) {
41962306a36Sopenharmony_ci				EMIT(PPC_RAW_MULI(dst_reg, src2_reg, imm));
42062306a36Sopenharmony_ci			} else {
42162306a36Sopenharmony_ci				PPC_LI32(_R0, imm);
42262306a36Sopenharmony_ci				EMIT(PPC_RAW_MULW(dst_reg, src2_reg, _R0));
42362306a36Sopenharmony_ci			}
42462306a36Sopenharmony_ci			break;
42562306a36Sopenharmony_ci		case BPF_ALU64 | BPF_MUL | BPF_K: /* dst *= imm */
42662306a36Sopenharmony_ci			if (!imm) {
42762306a36Sopenharmony_ci				PPC_LI32(dst_reg, 0);
42862306a36Sopenharmony_ci				PPC_LI32(dst_reg_h, 0);
42962306a36Sopenharmony_ci			} else if (imm == 1) {
43062306a36Sopenharmony_ci				EMIT(PPC_RAW_MR(dst_reg, src2_reg));
43162306a36Sopenharmony_ci				EMIT(PPC_RAW_MR(dst_reg_h, src2_reg_h));
43262306a36Sopenharmony_ci			} else if (imm == -1) {
43362306a36Sopenharmony_ci				EMIT(PPC_RAW_SUBFIC(dst_reg, src2_reg, 0));
43462306a36Sopenharmony_ci				EMIT(PPC_RAW_SUBFZE(dst_reg_h, src2_reg_h));
43562306a36Sopenharmony_ci			} else if (imm > 0 && is_power_of_2(imm)) {
43662306a36Sopenharmony_ci				imm = ilog2(imm);
43762306a36Sopenharmony_ci				EMIT(PPC_RAW_RLWINM(dst_reg_h, src2_reg_h, imm, 0, 31 - imm));
43862306a36Sopenharmony_ci				EMIT(PPC_RAW_RLWIMI(dst_reg_h, dst_reg, imm, 32 - imm, 31));
43962306a36Sopenharmony_ci				EMIT(PPC_RAW_SLWI(dst_reg, src2_reg, imm));
44062306a36Sopenharmony_ci			} else {
44162306a36Sopenharmony_ci				bpf_set_seen_register(ctx, tmp_reg);
44262306a36Sopenharmony_ci				PPC_LI32(tmp_reg, imm);
44362306a36Sopenharmony_ci				EMIT(PPC_RAW_MULW(dst_reg_h, src2_reg_h, tmp_reg));
44462306a36Sopenharmony_ci				if (imm < 0)
44562306a36Sopenharmony_ci					EMIT(PPC_RAW_SUB(dst_reg_h, dst_reg_h, src2_reg));
44662306a36Sopenharmony_ci				EMIT(PPC_RAW_MULHWU(_R0, src2_reg, tmp_reg));
44762306a36Sopenharmony_ci				EMIT(PPC_RAW_MULW(dst_reg, src2_reg, tmp_reg));
44862306a36Sopenharmony_ci				EMIT(PPC_RAW_ADD(dst_reg_h, dst_reg_h, _R0));
44962306a36Sopenharmony_ci			}
45062306a36Sopenharmony_ci			break;
45162306a36Sopenharmony_ci		case BPF_ALU | BPF_DIV | BPF_X: /* (u32) dst /= (u32) src */
45262306a36Sopenharmony_ci			EMIT(PPC_RAW_DIVWU(dst_reg, src2_reg, src_reg));
45362306a36Sopenharmony_ci			break;
45462306a36Sopenharmony_ci		case BPF_ALU | BPF_MOD | BPF_X: /* (u32) dst %= (u32) src */
45562306a36Sopenharmony_ci			EMIT(PPC_RAW_DIVWU(_R0, src2_reg, src_reg));
45662306a36Sopenharmony_ci			EMIT(PPC_RAW_MULW(_R0, src_reg, _R0));
45762306a36Sopenharmony_ci			EMIT(PPC_RAW_SUB(dst_reg, src2_reg, _R0));
45862306a36Sopenharmony_ci			break;
45962306a36Sopenharmony_ci		case BPF_ALU64 | BPF_DIV | BPF_X: /* dst /= src */
46062306a36Sopenharmony_ci			return -EOPNOTSUPP;
46162306a36Sopenharmony_ci		case BPF_ALU64 | BPF_MOD | BPF_X: /* dst %= src */
46262306a36Sopenharmony_ci			return -EOPNOTSUPP;
46362306a36Sopenharmony_ci		case BPF_ALU | BPF_DIV | BPF_K: /* (u32) dst /= (u32) imm */
46462306a36Sopenharmony_ci			if (!imm)
46562306a36Sopenharmony_ci				return -EINVAL;
46662306a36Sopenharmony_ci			if (imm == 1) {
46762306a36Sopenharmony_ci				EMIT(PPC_RAW_MR(dst_reg, src2_reg));
46862306a36Sopenharmony_ci			} else if (is_power_of_2((u32)imm)) {
46962306a36Sopenharmony_ci				EMIT(PPC_RAW_SRWI(dst_reg, src2_reg, ilog2(imm)));
47062306a36Sopenharmony_ci			} else {
47162306a36Sopenharmony_ci				PPC_LI32(_R0, imm);
47262306a36Sopenharmony_ci				EMIT(PPC_RAW_DIVWU(dst_reg, src2_reg, _R0));
47362306a36Sopenharmony_ci			}
47462306a36Sopenharmony_ci			break;
47562306a36Sopenharmony_ci		case BPF_ALU | BPF_MOD | BPF_K: /* (u32) dst %= (u32) imm */
47662306a36Sopenharmony_ci			if (!imm)
47762306a36Sopenharmony_ci				return -EINVAL;
47862306a36Sopenharmony_ci
47962306a36Sopenharmony_ci			if (!is_power_of_2((u32)imm)) {
48062306a36Sopenharmony_ci				bpf_set_seen_register(ctx, tmp_reg);
48162306a36Sopenharmony_ci				PPC_LI32(tmp_reg, imm);
48262306a36Sopenharmony_ci				EMIT(PPC_RAW_DIVWU(_R0, src2_reg, tmp_reg));
48362306a36Sopenharmony_ci				EMIT(PPC_RAW_MULW(_R0, tmp_reg, _R0));
48462306a36Sopenharmony_ci				EMIT(PPC_RAW_SUB(dst_reg, src2_reg, _R0));
48562306a36Sopenharmony_ci			} else if (imm == 1) {
48662306a36Sopenharmony_ci				EMIT(PPC_RAW_LI(dst_reg, 0));
48762306a36Sopenharmony_ci			} else {
48862306a36Sopenharmony_ci				imm = ilog2((u32)imm);
48962306a36Sopenharmony_ci				EMIT(PPC_RAW_RLWINM(dst_reg, src2_reg, 0, 32 - imm, 31));
49062306a36Sopenharmony_ci			}
49162306a36Sopenharmony_ci			break;
49262306a36Sopenharmony_ci		case BPF_ALU64 | BPF_MOD | BPF_K: /* dst %= imm */
49362306a36Sopenharmony_ci			if (!imm)
49462306a36Sopenharmony_ci				return -EINVAL;
49562306a36Sopenharmony_ci			if (imm < 0)
49662306a36Sopenharmony_ci				imm = -imm;
49762306a36Sopenharmony_ci			if (!is_power_of_2(imm))
49862306a36Sopenharmony_ci				return -EOPNOTSUPP;
49962306a36Sopenharmony_ci			if (imm == 1)
50062306a36Sopenharmony_ci				EMIT(PPC_RAW_LI(dst_reg, 0));
50162306a36Sopenharmony_ci			else
50262306a36Sopenharmony_ci				EMIT(PPC_RAW_RLWINM(dst_reg, src2_reg, 0, 32 - ilog2(imm), 31));
50362306a36Sopenharmony_ci			EMIT(PPC_RAW_LI(dst_reg_h, 0));
50462306a36Sopenharmony_ci			break;
50562306a36Sopenharmony_ci		case BPF_ALU64 | BPF_DIV | BPF_K: /* dst /= imm */
50662306a36Sopenharmony_ci			if (!imm)
50762306a36Sopenharmony_ci				return -EINVAL;
50862306a36Sopenharmony_ci			if (!is_power_of_2(abs(imm)))
50962306a36Sopenharmony_ci				return -EOPNOTSUPP;
51062306a36Sopenharmony_ci
51162306a36Sopenharmony_ci			if (imm < 0) {
51262306a36Sopenharmony_ci				EMIT(PPC_RAW_SUBFIC(dst_reg, src2_reg, 0));
51362306a36Sopenharmony_ci				EMIT(PPC_RAW_SUBFZE(dst_reg_h, src2_reg_h));
51462306a36Sopenharmony_ci				imm = -imm;
51562306a36Sopenharmony_ci				src2_reg = dst_reg;
51662306a36Sopenharmony_ci			}
51762306a36Sopenharmony_ci			if (imm == 1) {
51862306a36Sopenharmony_ci				EMIT(PPC_RAW_MR(dst_reg, src2_reg));
51962306a36Sopenharmony_ci				EMIT(PPC_RAW_MR(dst_reg_h, src2_reg_h));
52062306a36Sopenharmony_ci			} else {
52162306a36Sopenharmony_ci				imm = ilog2(imm);
52262306a36Sopenharmony_ci				EMIT(PPC_RAW_RLWINM(dst_reg, src2_reg, 32 - imm, imm, 31));
52362306a36Sopenharmony_ci				EMIT(PPC_RAW_RLWIMI(dst_reg, src2_reg_h, 32 - imm, 0, imm - 1));
52462306a36Sopenharmony_ci				EMIT(PPC_RAW_SRAWI(dst_reg_h, src2_reg_h, imm));
52562306a36Sopenharmony_ci			}
52662306a36Sopenharmony_ci			break;
52762306a36Sopenharmony_ci		case BPF_ALU | BPF_NEG: /* (u32) dst = -dst */
52862306a36Sopenharmony_ci			EMIT(PPC_RAW_NEG(dst_reg, src2_reg));
52962306a36Sopenharmony_ci			break;
53062306a36Sopenharmony_ci		case BPF_ALU64 | BPF_NEG: /* dst = -dst */
53162306a36Sopenharmony_ci			EMIT(PPC_RAW_SUBFIC(dst_reg, src2_reg, 0));
53262306a36Sopenharmony_ci			EMIT(PPC_RAW_SUBFZE(dst_reg_h, src2_reg_h));
53362306a36Sopenharmony_ci			break;
53462306a36Sopenharmony_ci
53562306a36Sopenharmony_ci		/*
53662306a36Sopenharmony_ci		 * Logical operations: AND/OR/XOR/[A]LSH/[A]RSH
53762306a36Sopenharmony_ci		 */
53862306a36Sopenharmony_ci		case BPF_ALU64 | BPF_AND | BPF_X: /* dst = dst & src */
53962306a36Sopenharmony_ci			EMIT(PPC_RAW_AND(dst_reg, src2_reg, src_reg));
54062306a36Sopenharmony_ci			EMIT(PPC_RAW_AND(dst_reg_h, src2_reg_h, src_reg_h));
54162306a36Sopenharmony_ci			break;
54262306a36Sopenharmony_ci		case BPF_ALU | BPF_AND | BPF_X: /* (u32) dst = dst & src */
54362306a36Sopenharmony_ci			EMIT(PPC_RAW_AND(dst_reg, src2_reg, src_reg));
54462306a36Sopenharmony_ci			break;
54562306a36Sopenharmony_ci		case BPF_ALU64 | BPF_AND | BPF_K: /* dst = dst & imm */
54662306a36Sopenharmony_ci			if (imm >= 0)
54762306a36Sopenharmony_ci				EMIT(PPC_RAW_LI(dst_reg_h, 0));
54862306a36Sopenharmony_ci			fallthrough;
54962306a36Sopenharmony_ci		case BPF_ALU | BPF_AND | BPF_K: /* (u32) dst = dst & imm */
55062306a36Sopenharmony_ci			if (!IMM_H(imm)) {
55162306a36Sopenharmony_ci				EMIT(PPC_RAW_ANDI(dst_reg, src2_reg, IMM_L(imm)));
55262306a36Sopenharmony_ci			} else if (!IMM_L(imm)) {
55362306a36Sopenharmony_ci				EMIT(PPC_RAW_ANDIS(dst_reg, src2_reg, IMM_H(imm)));
55462306a36Sopenharmony_ci			} else if (imm == (((1 << fls(imm)) - 1) ^ ((1 << (ffs(i) - 1)) - 1))) {
55562306a36Sopenharmony_ci				EMIT(PPC_RAW_RLWINM(dst_reg, src2_reg, 0,
55662306a36Sopenharmony_ci						    32 - fls(imm), 32 - ffs(imm)));
55762306a36Sopenharmony_ci			} else {
55862306a36Sopenharmony_ci				PPC_LI32(_R0, imm);
55962306a36Sopenharmony_ci				EMIT(PPC_RAW_AND(dst_reg, src2_reg, _R0));
56062306a36Sopenharmony_ci			}
56162306a36Sopenharmony_ci			break;
56262306a36Sopenharmony_ci		case BPF_ALU64 | BPF_OR | BPF_X: /* dst = dst | src */
56362306a36Sopenharmony_ci			EMIT(PPC_RAW_OR(dst_reg, src2_reg, src_reg));
56462306a36Sopenharmony_ci			EMIT(PPC_RAW_OR(dst_reg_h, src2_reg_h, src_reg_h));
56562306a36Sopenharmony_ci			break;
56662306a36Sopenharmony_ci		case BPF_ALU | BPF_OR | BPF_X: /* dst = (u32) dst | (u32) src */
56762306a36Sopenharmony_ci			EMIT(PPC_RAW_OR(dst_reg, src2_reg, src_reg));
56862306a36Sopenharmony_ci			break;
56962306a36Sopenharmony_ci		case BPF_ALU64 | BPF_OR | BPF_K:/* dst = dst | imm */
57062306a36Sopenharmony_ci			/* Sign-extended */
57162306a36Sopenharmony_ci			if (imm < 0)
57262306a36Sopenharmony_ci				EMIT(PPC_RAW_LI(dst_reg_h, -1));
57362306a36Sopenharmony_ci			fallthrough;
57462306a36Sopenharmony_ci		case BPF_ALU | BPF_OR | BPF_K:/* dst = (u32) dst | (u32) imm */
57562306a36Sopenharmony_ci			if (IMM_L(imm)) {
57662306a36Sopenharmony_ci				EMIT(PPC_RAW_ORI(dst_reg, src2_reg, IMM_L(imm)));
57762306a36Sopenharmony_ci				src2_reg = dst_reg;
57862306a36Sopenharmony_ci			}
57962306a36Sopenharmony_ci			if (IMM_H(imm))
58062306a36Sopenharmony_ci				EMIT(PPC_RAW_ORIS(dst_reg, src2_reg, IMM_H(imm)));
58162306a36Sopenharmony_ci			break;
58262306a36Sopenharmony_ci		case BPF_ALU64 | BPF_XOR | BPF_X: /* dst ^= src */
58362306a36Sopenharmony_ci			if (dst_reg == src_reg) {
58462306a36Sopenharmony_ci				EMIT(PPC_RAW_LI(dst_reg, 0));
58562306a36Sopenharmony_ci				EMIT(PPC_RAW_LI(dst_reg_h, 0));
58662306a36Sopenharmony_ci			} else {
58762306a36Sopenharmony_ci				EMIT(PPC_RAW_XOR(dst_reg, src2_reg, src_reg));
58862306a36Sopenharmony_ci				EMIT(PPC_RAW_XOR(dst_reg_h, src2_reg_h, src_reg_h));
58962306a36Sopenharmony_ci			}
59062306a36Sopenharmony_ci			break;
59162306a36Sopenharmony_ci		case BPF_ALU | BPF_XOR | BPF_X: /* (u32) dst ^= src */
59262306a36Sopenharmony_ci			if (dst_reg == src_reg)
59362306a36Sopenharmony_ci				EMIT(PPC_RAW_LI(dst_reg, 0));
59462306a36Sopenharmony_ci			else
59562306a36Sopenharmony_ci				EMIT(PPC_RAW_XOR(dst_reg, src2_reg, src_reg));
59662306a36Sopenharmony_ci			break;
59762306a36Sopenharmony_ci		case BPF_ALU64 | BPF_XOR | BPF_K: /* dst ^= imm */
59862306a36Sopenharmony_ci			if (imm < 0)
59962306a36Sopenharmony_ci				EMIT(PPC_RAW_NOR(dst_reg_h, src2_reg_h, src2_reg_h));
60062306a36Sopenharmony_ci			fallthrough;
60162306a36Sopenharmony_ci		case BPF_ALU | BPF_XOR | BPF_K: /* (u32) dst ^= (u32) imm */
60262306a36Sopenharmony_ci			if (IMM_L(imm)) {
60362306a36Sopenharmony_ci				EMIT(PPC_RAW_XORI(dst_reg, src2_reg, IMM_L(imm)));
60462306a36Sopenharmony_ci				src2_reg = dst_reg;
60562306a36Sopenharmony_ci			}
60662306a36Sopenharmony_ci			if (IMM_H(imm))
60762306a36Sopenharmony_ci				EMIT(PPC_RAW_XORIS(dst_reg, src2_reg, IMM_H(imm)));
60862306a36Sopenharmony_ci			break;
60962306a36Sopenharmony_ci		case BPF_ALU | BPF_LSH | BPF_X: /* (u32) dst <<= (u32) src */
61062306a36Sopenharmony_ci			EMIT(PPC_RAW_SLW(dst_reg, src2_reg, src_reg));
61162306a36Sopenharmony_ci			break;
61262306a36Sopenharmony_ci		case BPF_ALU64 | BPF_LSH | BPF_X: /* dst <<= src; */
61362306a36Sopenharmony_ci			bpf_set_seen_register(ctx, tmp_reg);
61462306a36Sopenharmony_ci			EMIT(PPC_RAW_SUBFIC(_R0, src_reg, 32));
61562306a36Sopenharmony_ci			EMIT(PPC_RAW_SLW(dst_reg_h, src2_reg_h, src_reg));
61662306a36Sopenharmony_ci			EMIT(PPC_RAW_ADDI(tmp_reg, src_reg, 32));
61762306a36Sopenharmony_ci			EMIT(PPC_RAW_SRW(_R0, src2_reg, _R0));
61862306a36Sopenharmony_ci			EMIT(PPC_RAW_SLW(tmp_reg, src2_reg, tmp_reg));
61962306a36Sopenharmony_ci			EMIT(PPC_RAW_OR(dst_reg_h, dst_reg_h, _R0));
62062306a36Sopenharmony_ci			EMIT(PPC_RAW_SLW(dst_reg, src2_reg, src_reg));
62162306a36Sopenharmony_ci			EMIT(PPC_RAW_OR(dst_reg_h, dst_reg_h, tmp_reg));
62262306a36Sopenharmony_ci			break;
62362306a36Sopenharmony_ci		case BPF_ALU | BPF_LSH | BPF_K: /* (u32) dst <<= (u32) imm */
62462306a36Sopenharmony_ci			if (imm)
62562306a36Sopenharmony_ci				EMIT(PPC_RAW_SLWI(dst_reg, src2_reg, imm));
62662306a36Sopenharmony_ci			else
62762306a36Sopenharmony_ci				EMIT(PPC_RAW_MR(dst_reg, src2_reg));
62862306a36Sopenharmony_ci			break;
62962306a36Sopenharmony_ci		case BPF_ALU64 | BPF_LSH | BPF_K: /* dst <<= imm */
63062306a36Sopenharmony_ci			if (imm < 0)
63162306a36Sopenharmony_ci				return -EINVAL;
63262306a36Sopenharmony_ci			if (!imm) {
63362306a36Sopenharmony_ci				EMIT(PPC_RAW_MR(dst_reg, src2_reg));
63462306a36Sopenharmony_ci			} else if (imm < 32) {
63562306a36Sopenharmony_ci				EMIT(PPC_RAW_RLWINM(dst_reg_h, src2_reg_h, imm, 0, 31 - imm));
63662306a36Sopenharmony_ci				EMIT(PPC_RAW_RLWIMI(dst_reg_h, src2_reg, imm, 32 - imm, 31));
63762306a36Sopenharmony_ci				EMIT(PPC_RAW_RLWINM(dst_reg, src2_reg, imm, 0, 31 - imm));
63862306a36Sopenharmony_ci			} else if (imm < 64) {
63962306a36Sopenharmony_ci				EMIT(PPC_RAW_RLWINM(dst_reg_h, src2_reg, imm, 0, 31 - imm));
64062306a36Sopenharmony_ci				EMIT(PPC_RAW_LI(dst_reg, 0));
64162306a36Sopenharmony_ci			} else {
64262306a36Sopenharmony_ci				EMIT(PPC_RAW_LI(dst_reg_h, 0));
64362306a36Sopenharmony_ci				EMIT(PPC_RAW_LI(dst_reg, 0));
64462306a36Sopenharmony_ci			}
64562306a36Sopenharmony_ci			break;
64662306a36Sopenharmony_ci		case BPF_ALU | BPF_RSH | BPF_X: /* (u32) dst >>= (u32) src */
64762306a36Sopenharmony_ci			EMIT(PPC_RAW_SRW(dst_reg, src2_reg, src_reg));
64862306a36Sopenharmony_ci			break;
64962306a36Sopenharmony_ci		case BPF_ALU64 | BPF_RSH | BPF_X: /* dst >>= src */
65062306a36Sopenharmony_ci			bpf_set_seen_register(ctx, tmp_reg);
65162306a36Sopenharmony_ci			EMIT(PPC_RAW_SUBFIC(_R0, src_reg, 32));
65262306a36Sopenharmony_ci			EMIT(PPC_RAW_SRW(dst_reg, src2_reg, src_reg));
65362306a36Sopenharmony_ci			EMIT(PPC_RAW_ADDI(tmp_reg, src_reg, 32));
65462306a36Sopenharmony_ci			EMIT(PPC_RAW_SLW(_R0, src2_reg_h, _R0));
65562306a36Sopenharmony_ci			EMIT(PPC_RAW_SRW(tmp_reg, dst_reg_h, tmp_reg));
65662306a36Sopenharmony_ci			EMIT(PPC_RAW_OR(dst_reg, dst_reg, _R0));
65762306a36Sopenharmony_ci			EMIT(PPC_RAW_SRW(dst_reg_h, src2_reg_h, src_reg));
65862306a36Sopenharmony_ci			EMIT(PPC_RAW_OR(dst_reg, dst_reg, tmp_reg));
65962306a36Sopenharmony_ci			break;
66062306a36Sopenharmony_ci		case BPF_ALU | BPF_RSH | BPF_K: /* (u32) dst >>= (u32) imm */
66162306a36Sopenharmony_ci			if (imm)
66262306a36Sopenharmony_ci				EMIT(PPC_RAW_SRWI(dst_reg, src2_reg, imm));
66362306a36Sopenharmony_ci			else
66462306a36Sopenharmony_ci				EMIT(PPC_RAW_MR(dst_reg, src2_reg));
66562306a36Sopenharmony_ci			break;
66662306a36Sopenharmony_ci		case BPF_ALU64 | BPF_RSH | BPF_K: /* dst >>= imm */
66762306a36Sopenharmony_ci			if (imm < 0)
66862306a36Sopenharmony_ci				return -EINVAL;
66962306a36Sopenharmony_ci			if (!imm) {
67062306a36Sopenharmony_ci				EMIT(PPC_RAW_MR(dst_reg, src2_reg));
67162306a36Sopenharmony_ci				EMIT(PPC_RAW_MR(dst_reg_h, src2_reg_h));
67262306a36Sopenharmony_ci			} else if (imm < 32) {
67362306a36Sopenharmony_ci				EMIT(PPC_RAW_RLWINM(dst_reg, src2_reg, 32 - imm, imm, 31));
67462306a36Sopenharmony_ci				EMIT(PPC_RAW_RLWIMI(dst_reg, src2_reg_h, 32 - imm, 0, imm - 1));
67562306a36Sopenharmony_ci				EMIT(PPC_RAW_RLWINM(dst_reg_h, src2_reg_h, 32 - imm, imm, 31));
67662306a36Sopenharmony_ci			} else if (imm < 64) {
67762306a36Sopenharmony_ci				EMIT(PPC_RAW_RLWINM(dst_reg, src2_reg_h, 64 - imm, imm - 32, 31));
67862306a36Sopenharmony_ci				EMIT(PPC_RAW_LI(dst_reg_h, 0));
67962306a36Sopenharmony_ci			} else {
68062306a36Sopenharmony_ci				EMIT(PPC_RAW_LI(dst_reg, 0));
68162306a36Sopenharmony_ci				EMIT(PPC_RAW_LI(dst_reg_h, 0));
68262306a36Sopenharmony_ci			}
68362306a36Sopenharmony_ci			break;
68462306a36Sopenharmony_ci		case BPF_ALU | BPF_ARSH | BPF_X: /* (s32) dst >>= src */
68562306a36Sopenharmony_ci			EMIT(PPC_RAW_SRAW(dst_reg, src2_reg, src_reg));
68662306a36Sopenharmony_ci			break;
68762306a36Sopenharmony_ci		case BPF_ALU64 | BPF_ARSH | BPF_X: /* (s64) dst >>= src */
68862306a36Sopenharmony_ci			bpf_set_seen_register(ctx, tmp_reg);
68962306a36Sopenharmony_ci			EMIT(PPC_RAW_SUBFIC(_R0, src_reg, 32));
69062306a36Sopenharmony_ci			EMIT(PPC_RAW_SRW(dst_reg, src2_reg, src_reg));
69162306a36Sopenharmony_ci			EMIT(PPC_RAW_SLW(_R0, src2_reg_h, _R0));
69262306a36Sopenharmony_ci			EMIT(PPC_RAW_ADDI(tmp_reg, src_reg, 32));
69362306a36Sopenharmony_ci			EMIT(PPC_RAW_OR(dst_reg, dst_reg, _R0));
69462306a36Sopenharmony_ci			EMIT(PPC_RAW_RLWINM(_R0, tmp_reg, 0, 26, 26));
69562306a36Sopenharmony_ci			EMIT(PPC_RAW_SRAW(tmp_reg, src2_reg_h, tmp_reg));
69662306a36Sopenharmony_ci			EMIT(PPC_RAW_SRAW(dst_reg_h, src2_reg_h, src_reg));
69762306a36Sopenharmony_ci			EMIT(PPC_RAW_SLW(tmp_reg, tmp_reg, _R0));
69862306a36Sopenharmony_ci			EMIT(PPC_RAW_OR(dst_reg, dst_reg, tmp_reg));
69962306a36Sopenharmony_ci			break;
70062306a36Sopenharmony_ci		case BPF_ALU | BPF_ARSH | BPF_K: /* (s32) dst >>= imm */
70162306a36Sopenharmony_ci			if (imm)
70262306a36Sopenharmony_ci				EMIT(PPC_RAW_SRAWI(dst_reg, src2_reg, imm));
70362306a36Sopenharmony_ci			else
70462306a36Sopenharmony_ci				EMIT(PPC_RAW_MR(dst_reg, src2_reg));
70562306a36Sopenharmony_ci			break;
70662306a36Sopenharmony_ci		case BPF_ALU64 | BPF_ARSH | BPF_K: /* (s64) dst >>= imm */
70762306a36Sopenharmony_ci			if (imm < 0)
70862306a36Sopenharmony_ci				return -EINVAL;
70962306a36Sopenharmony_ci			if (!imm) {
71062306a36Sopenharmony_ci				EMIT(PPC_RAW_MR(dst_reg, src2_reg));
71162306a36Sopenharmony_ci				EMIT(PPC_RAW_MR(dst_reg_h, src2_reg_h));
71262306a36Sopenharmony_ci			} else if (imm < 32) {
71362306a36Sopenharmony_ci				EMIT(PPC_RAW_RLWINM(dst_reg, src2_reg, 32 - imm, imm, 31));
71462306a36Sopenharmony_ci				EMIT(PPC_RAW_RLWIMI(dst_reg, src2_reg_h, 32 - imm, 0, imm - 1));
71562306a36Sopenharmony_ci				EMIT(PPC_RAW_SRAWI(dst_reg_h, src2_reg_h, imm));
71662306a36Sopenharmony_ci			} else if (imm < 64) {
71762306a36Sopenharmony_ci				EMIT(PPC_RAW_SRAWI(dst_reg, src2_reg_h, imm - 32));
71862306a36Sopenharmony_ci				EMIT(PPC_RAW_SRAWI(dst_reg_h, src2_reg_h, 31));
71962306a36Sopenharmony_ci			} else {
72062306a36Sopenharmony_ci				EMIT(PPC_RAW_SRAWI(dst_reg, src2_reg_h, 31));
72162306a36Sopenharmony_ci				EMIT(PPC_RAW_SRAWI(dst_reg_h, src2_reg_h, 31));
72262306a36Sopenharmony_ci			}
72362306a36Sopenharmony_ci			break;
72462306a36Sopenharmony_ci
72562306a36Sopenharmony_ci		/*
72662306a36Sopenharmony_ci		 * MOV
72762306a36Sopenharmony_ci		 */
72862306a36Sopenharmony_ci		case BPF_ALU64 | BPF_MOV | BPF_X: /* dst = src */
72962306a36Sopenharmony_ci			if (dst_reg == src_reg)
73062306a36Sopenharmony_ci				break;
73162306a36Sopenharmony_ci			EMIT(PPC_RAW_MR(dst_reg, src_reg));
73262306a36Sopenharmony_ci			EMIT(PPC_RAW_MR(dst_reg_h, src_reg_h));
73362306a36Sopenharmony_ci			break;
73462306a36Sopenharmony_ci		case BPF_ALU | BPF_MOV | BPF_X: /* (u32) dst = src */
73562306a36Sopenharmony_ci			/* special mov32 for zext */
73662306a36Sopenharmony_ci			if (imm == 1)
73762306a36Sopenharmony_ci				EMIT(PPC_RAW_LI(dst_reg_h, 0));
73862306a36Sopenharmony_ci			else if (dst_reg != src_reg)
73962306a36Sopenharmony_ci				EMIT(PPC_RAW_MR(dst_reg, src_reg));
74062306a36Sopenharmony_ci			break;
74162306a36Sopenharmony_ci		case BPF_ALU64 | BPF_MOV | BPF_K: /* dst = (s64) imm */
74262306a36Sopenharmony_ci			PPC_LI32(dst_reg, imm);
74362306a36Sopenharmony_ci			PPC_EX32(dst_reg_h, imm);
74462306a36Sopenharmony_ci			break;
74562306a36Sopenharmony_ci		case BPF_ALU | BPF_MOV | BPF_K: /* (u32) dst = imm */
74662306a36Sopenharmony_ci			PPC_LI32(dst_reg, imm);
74762306a36Sopenharmony_ci			break;
74862306a36Sopenharmony_ci
74962306a36Sopenharmony_ci		/*
75062306a36Sopenharmony_ci		 * BPF_FROM_BE/LE
75162306a36Sopenharmony_ci		 */
75262306a36Sopenharmony_ci		case BPF_ALU | BPF_END | BPF_FROM_LE:
75362306a36Sopenharmony_ci			switch (imm) {
75462306a36Sopenharmony_ci			case 16:
75562306a36Sopenharmony_ci				/* Copy 16 bits to upper part */
75662306a36Sopenharmony_ci				EMIT(PPC_RAW_RLWIMI(dst_reg, src2_reg, 16, 0, 15));
75762306a36Sopenharmony_ci				/* Rotate 8 bits right & mask */
75862306a36Sopenharmony_ci				EMIT(PPC_RAW_RLWINM(dst_reg, dst_reg, 24, 16, 31));
75962306a36Sopenharmony_ci				break;
76062306a36Sopenharmony_ci			case 32:
76162306a36Sopenharmony_ci				/*
76262306a36Sopenharmony_ci				 * Rotate word left by 8 bits:
76362306a36Sopenharmony_ci				 * 2 bytes are already in their final position
76462306a36Sopenharmony_ci				 * -- byte 2 and 4 (of bytes 1, 2, 3 and 4)
76562306a36Sopenharmony_ci				 */
76662306a36Sopenharmony_ci				EMIT(PPC_RAW_RLWINM(_R0, src2_reg, 8, 0, 31));
76762306a36Sopenharmony_ci				/* Rotate 24 bits and insert byte 1 */
76862306a36Sopenharmony_ci				EMIT(PPC_RAW_RLWIMI(_R0, src2_reg, 24, 0, 7));
76962306a36Sopenharmony_ci				/* Rotate 24 bits and insert byte 3 */
77062306a36Sopenharmony_ci				EMIT(PPC_RAW_RLWIMI(_R0, src2_reg, 24, 16, 23));
77162306a36Sopenharmony_ci				EMIT(PPC_RAW_MR(dst_reg, _R0));
77262306a36Sopenharmony_ci				break;
77362306a36Sopenharmony_ci			case 64:
77462306a36Sopenharmony_ci				bpf_set_seen_register(ctx, tmp_reg);
77562306a36Sopenharmony_ci				EMIT(PPC_RAW_RLWINM(tmp_reg, src2_reg, 8, 0, 31));
77662306a36Sopenharmony_ci				EMIT(PPC_RAW_RLWINM(_R0, src2_reg_h, 8, 0, 31));
77762306a36Sopenharmony_ci				/* Rotate 24 bits and insert byte 1 */
77862306a36Sopenharmony_ci				EMIT(PPC_RAW_RLWIMI(tmp_reg, src2_reg, 24, 0, 7));
77962306a36Sopenharmony_ci				EMIT(PPC_RAW_RLWIMI(_R0, src2_reg_h, 24, 0, 7));
78062306a36Sopenharmony_ci				/* Rotate 24 bits and insert byte 3 */
78162306a36Sopenharmony_ci				EMIT(PPC_RAW_RLWIMI(tmp_reg, src2_reg, 24, 16, 23));
78262306a36Sopenharmony_ci				EMIT(PPC_RAW_RLWIMI(_R0, src2_reg_h, 24, 16, 23));
78362306a36Sopenharmony_ci				EMIT(PPC_RAW_MR(dst_reg, _R0));
78462306a36Sopenharmony_ci				EMIT(PPC_RAW_MR(dst_reg_h, tmp_reg));
78562306a36Sopenharmony_ci				break;
78662306a36Sopenharmony_ci			}
78762306a36Sopenharmony_ci			break;
78862306a36Sopenharmony_ci		case BPF_ALU | BPF_END | BPF_FROM_BE:
78962306a36Sopenharmony_ci			switch (imm) {
79062306a36Sopenharmony_ci			case 16:
79162306a36Sopenharmony_ci				/* zero-extend 16 bits into 32 bits */
79262306a36Sopenharmony_ci				EMIT(PPC_RAW_RLWINM(dst_reg, src2_reg, 0, 16, 31));
79362306a36Sopenharmony_ci				break;
79462306a36Sopenharmony_ci			case 32:
79562306a36Sopenharmony_ci			case 64:
79662306a36Sopenharmony_ci				/* nop */
79762306a36Sopenharmony_ci				break;
79862306a36Sopenharmony_ci			}
79962306a36Sopenharmony_ci			break;
80062306a36Sopenharmony_ci
80162306a36Sopenharmony_ci		/*
80262306a36Sopenharmony_ci		 * BPF_ST NOSPEC (speculation barrier)
80362306a36Sopenharmony_ci		 */
80462306a36Sopenharmony_ci		case BPF_ST | BPF_NOSPEC:
80562306a36Sopenharmony_ci			break;
80662306a36Sopenharmony_ci
80762306a36Sopenharmony_ci		/*
80862306a36Sopenharmony_ci		 * BPF_ST(X)
80962306a36Sopenharmony_ci		 */
81062306a36Sopenharmony_ci		case BPF_STX | BPF_MEM | BPF_B: /* *(u8 *)(dst + off) = src */
81162306a36Sopenharmony_ci			EMIT(PPC_RAW_STB(src_reg, dst_reg, off));
81262306a36Sopenharmony_ci			break;
81362306a36Sopenharmony_ci		case BPF_ST | BPF_MEM | BPF_B: /* *(u8 *)(dst + off) = imm */
81462306a36Sopenharmony_ci			PPC_LI32(_R0, imm);
81562306a36Sopenharmony_ci			EMIT(PPC_RAW_STB(_R0, dst_reg, off));
81662306a36Sopenharmony_ci			break;
81762306a36Sopenharmony_ci		case BPF_STX | BPF_MEM | BPF_H: /* (u16 *)(dst + off) = src */
81862306a36Sopenharmony_ci			EMIT(PPC_RAW_STH(src_reg, dst_reg, off));
81962306a36Sopenharmony_ci			break;
82062306a36Sopenharmony_ci		case BPF_ST | BPF_MEM | BPF_H: /* (u16 *)(dst + off) = imm */
82162306a36Sopenharmony_ci			PPC_LI32(_R0, imm);
82262306a36Sopenharmony_ci			EMIT(PPC_RAW_STH(_R0, dst_reg, off));
82362306a36Sopenharmony_ci			break;
82462306a36Sopenharmony_ci		case BPF_STX | BPF_MEM | BPF_W: /* *(u32 *)(dst + off) = src */
82562306a36Sopenharmony_ci			EMIT(PPC_RAW_STW(src_reg, dst_reg, off));
82662306a36Sopenharmony_ci			break;
82762306a36Sopenharmony_ci		case BPF_ST | BPF_MEM | BPF_W: /* *(u32 *)(dst + off) = imm */
82862306a36Sopenharmony_ci			PPC_LI32(_R0, imm);
82962306a36Sopenharmony_ci			EMIT(PPC_RAW_STW(_R0, dst_reg, off));
83062306a36Sopenharmony_ci			break;
83162306a36Sopenharmony_ci		case BPF_STX | BPF_MEM | BPF_DW: /* (u64 *)(dst + off) = src */
83262306a36Sopenharmony_ci			EMIT(PPC_RAW_STW(src_reg_h, dst_reg, off));
83362306a36Sopenharmony_ci			EMIT(PPC_RAW_STW(src_reg, dst_reg, off + 4));
83462306a36Sopenharmony_ci			break;
83562306a36Sopenharmony_ci		case BPF_ST | BPF_MEM | BPF_DW: /* *(u64 *)(dst + off) = imm */
83662306a36Sopenharmony_ci			PPC_LI32(_R0, imm);
83762306a36Sopenharmony_ci			EMIT(PPC_RAW_STW(_R0, dst_reg, off + 4));
83862306a36Sopenharmony_ci			PPC_EX32(_R0, imm);
83962306a36Sopenharmony_ci			EMIT(PPC_RAW_STW(_R0, dst_reg, off));
84062306a36Sopenharmony_ci			break;
84162306a36Sopenharmony_ci
84262306a36Sopenharmony_ci		/*
84362306a36Sopenharmony_ci		 * BPF_STX ATOMIC (atomic ops)
84462306a36Sopenharmony_ci		 */
84562306a36Sopenharmony_ci		case BPF_STX | BPF_ATOMIC | BPF_W:
84662306a36Sopenharmony_ci			save_reg = _R0;
84762306a36Sopenharmony_ci			ret_reg = src_reg;
84862306a36Sopenharmony_ci
84962306a36Sopenharmony_ci			bpf_set_seen_register(ctx, tmp_reg);
85062306a36Sopenharmony_ci			bpf_set_seen_register(ctx, ax_reg);
85162306a36Sopenharmony_ci
85262306a36Sopenharmony_ci			/* Get offset into TMP_REG */
85362306a36Sopenharmony_ci			EMIT(PPC_RAW_LI(tmp_reg, off));
85462306a36Sopenharmony_ci			tmp_idx = ctx->idx * 4;
85562306a36Sopenharmony_ci			/* load value from memory into r0 */
85662306a36Sopenharmony_ci			EMIT(PPC_RAW_LWARX(_R0, tmp_reg, dst_reg, 0));
85762306a36Sopenharmony_ci
85862306a36Sopenharmony_ci			/* Save old value in BPF_REG_AX */
85962306a36Sopenharmony_ci			if (imm & BPF_FETCH)
86062306a36Sopenharmony_ci				EMIT(PPC_RAW_MR(ax_reg, _R0));
86162306a36Sopenharmony_ci
86262306a36Sopenharmony_ci			switch (imm) {
86362306a36Sopenharmony_ci			case BPF_ADD:
86462306a36Sopenharmony_ci			case BPF_ADD | BPF_FETCH:
86562306a36Sopenharmony_ci				EMIT(PPC_RAW_ADD(_R0, _R0, src_reg));
86662306a36Sopenharmony_ci				break;
86762306a36Sopenharmony_ci			case BPF_AND:
86862306a36Sopenharmony_ci			case BPF_AND | BPF_FETCH:
86962306a36Sopenharmony_ci				EMIT(PPC_RAW_AND(_R0, _R0, src_reg));
87062306a36Sopenharmony_ci				break;
87162306a36Sopenharmony_ci			case BPF_OR:
87262306a36Sopenharmony_ci			case BPF_OR | BPF_FETCH:
87362306a36Sopenharmony_ci				EMIT(PPC_RAW_OR(_R0, _R0, src_reg));
87462306a36Sopenharmony_ci				break;
87562306a36Sopenharmony_ci			case BPF_XOR:
87662306a36Sopenharmony_ci			case BPF_XOR | BPF_FETCH:
87762306a36Sopenharmony_ci				EMIT(PPC_RAW_XOR(_R0, _R0, src_reg));
87862306a36Sopenharmony_ci				break;
87962306a36Sopenharmony_ci			case BPF_CMPXCHG:
88062306a36Sopenharmony_ci				/*
88162306a36Sopenharmony_ci				 * Return old value in BPF_REG_0 for BPF_CMPXCHG &
88262306a36Sopenharmony_ci				 * in src_reg for other cases.
88362306a36Sopenharmony_ci				 */
88462306a36Sopenharmony_ci				ret_reg = bpf_to_ppc(BPF_REG_0);
88562306a36Sopenharmony_ci
88662306a36Sopenharmony_ci				/* Compare with old value in BPF_REG_0 */
88762306a36Sopenharmony_ci				EMIT(PPC_RAW_CMPW(bpf_to_ppc(BPF_REG_0), _R0));
88862306a36Sopenharmony_ci				/* Don't set if different from old value */
88962306a36Sopenharmony_ci				PPC_BCC_SHORT(COND_NE, (ctx->idx + 3) * 4);
89062306a36Sopenharmony_ci				fallthrough;
89162306a36Sopenharmony_ci			case BPF_XCHG:
89262306a36Sopenharmony_ci				save_reg = src_reg;
89362306a36Sopenharmony_ci				break;
89462306a36Sopenharmony_ci			default:
89562306a36Sopenharmony_ci				pr_err_ratelimited("eBPF filter atomic op code %02x (@%d) unsupported\n",
89662306a36Sopenharmony_ci						   code, i);
89762306a36Sopenharmony_ci				return -EOPNOTSUPP;
89862306a36Sopenharmony_ci			}
89962306a36Sopenharmony_ci
90062306a36Sopenharmony_ci			/* store new value */
90162306a36Sopenharmony_ci			EMIT(PPC_RAW_STWCX(save_reg, tmp_reg, dst_reg));
90262306a36Sopenharmony_ci			/* we're done if this succeeded */
90362306a36Sopenharmony_ci			PPC_BCC_SHORT(COND_NE, tmp_idx);
90462306a36Sopenharmony_ci
90562306a36Sopenharmony_ci			/* For the BPF_FETCH variant, get old data into src_reg */
90662306a36Sopenharmony_ci			if (imm & BPF_FETCH) {
90762306a36Sopenharmony_ci				EMIT(PPC_RAW_MR(ret_reg, ax_reg));
90862306a36Sopenharmony_ci				if (!fp->aux->verifier_zext)
90962306a36Sopenharmony_ci					EMIT(PPC_RAW_LI(ret_reg - 1, 0)); /* higher 32-bit */
91062306a36Sopenharmony_ci			}
91162306a36Sopenharmony_ci			break;
91262306a36Sopenharmony_ci
91362306a36Sopenharmony_ci		case BPF_STX | BPF_ATOMIC | BPF_DW: /* *(u64 *)(dst + off) += src */
91462306a36Sopenharmony_ci			return -EOPNOTSUPP;
91562306a36Sopenharmony_ci
91662306a36Sopenharmony_ci		/*
91762306a36Sopenharmony_ci		 * BPF_LDX
91862306a36Sopenharmony_ci		 */
91962306a36Sopenharmony_ci		case BPF_LDX | BPF_MEM | BPF_B: /* dst = *(u8 *)(ul) (src + off) */
92062306a36Sopenharmony_ci		case BPF_LDX | BPF_PROBE_MEM | BPF_B:
92162306a36Sopenharmony_ci		case BPF_LDX | BPF_MEM | BPF_H: /* dst = *(u16 *)(ul) (src + off) */
92262306a36Sopenharmony_ci		case BPF_LDX | BPF_PROBE_MEM | BPF_H:
92362306a36Sopenharmony_ci		case BPF_LDX | BPF_MEM | BPF_W: /* dst = *(u32 *)(ul) (src + off) */
92462306a36Sopenharmony_ci		case BPF_LDX | BPF_PROBE_MEM | BPF_W:
92562306a36Sopenharmony_ci		case BPF_LDX | BPF_MEM | BPF_DW: /* dst = *(u64 *)(ul) (src + off) */
92662306a36Sopenharmony_ci		case BPF_LDX | BPF_PROBE_MEM | BPF_DW:
92762306a36Sopenharmony_ci			/*
92862306a36Sopenharmony_ci			 * As PTR_TO_BTF_ID that uses BPF_PROBE_MEM mode could either be a valid
92962306a36Sopenharmony_ci			 * kernel pointer or NULL but not a userspace address, execute BPF_PROBE_MEM
93062306a36Sopenharmony_ci			 * load only if addr is kernel address (see is_kernel_addr()), otherwise
93162306a36Sopenharmony_ci			 * set dst_reg=0 and move on.
93262306a36Sopenharmony_ci			 */
93362306a36Sopenharmony_ci			if (BPF_MODE(code) == BPF_PROBE_MEM) {
93462306a36Sopenharmony_ci				PPC_LI32(_R0, TASK_SIZE - off);
93562306a36Sopenharmony_ci				EMIT(PPC_RAW_CMPLW(src_reg, _R0));
93662306a36Sopenharmony_ci				PPC_BCC_SHORT(COND_GT, (ctx->idx + 4) * 4);
93762306a36Sopenharmony_ci				EMIT(PPC_RAW_LI(dst_reg, 0));
93862306a36Sopenharmony_ci				/*
93962306a36Sopenharmony_ci				 * For BPF_DW case, "li reg_h,0" would be needed when
94062306a36Sopenharmony_ci				 * !fp->aux->verifier_zext. Emit NOP otherwise.
94162306a36Sopenharmony_ci				 *
94262306a36Sopenharmony_ci				 * Note that "li reg_h,0" is emitted for BPF_B/H/W case,
94362306a36Sopenharmony_ci				 * if necessary. So, jump there insted of emitting an
94462306a36Sopenharmony_ci				 * additional "li reg_h,0" instruction.
94562306a36Sopenharmony_ci				 */
94662306a36Sopenharmony_ci				if (size == BPF_DW && !fp->aux->verifier_zext)
94762306a36Sopenharmony_ci					EMIT(PPC_RAW_LI(dst_reg_h, 0));
94862306a36Sopenharmony_ci				else
94962306a36Sopenharmony_ci					EMIT(PPC_RAW_NOP());
95062306a36Sopenharmony_ci				/*
95162306a36Sopenharmony_ci				 * Need to jump two instructions instead of one for BPF_DW case
95262306a36Sopenharmony_ci				 * as there are two load instructions for dst_reg_h & dst_reg
95362306a36Sopenharmony_ci				 * respectively.
95462306a36Sopenharmony_ci				 */
95562306a36Sopenharmony_ci				if (size == BPF_DW)
95662306a36Sopenharmony_ci					PPC_JMP((ctx->idx + 3) * 4);
95762306a36Sopenharmony_ci				else
95862306a36Sopenharmony_ci					PPC_JMP((ctx->idx + 2) * 4);
95962306a36Sopenharmony_ci			}
96062306a36Sopenharmony_ci
96162306a36Sopenharmony_ci			switch (size) {
96262306a36Sopenharmony_ci			case BPF_B:
96362306a36Sopenharmony_ci				EMIT(PPC_RAW_LBZ(dst_reg, src_reg, off));
96462306a36Sopenharmony_ci				break;
96562306a36Sopenharmony_ci			case BPF_H:
96662306a36Sopenharmony_ci				EMIT(PPC_RAW_LHZ(dst_reg, src_reg, off));
96762306a36Sopenharmony_ci				break;
96862306a36Sopenharmony_ci			case BPF_W:
96962306a36Sopenharmony_ci				EMIT(PPC_RAW_LWZ(dst_reg, src_reg, off));
97062306a36Sopenharmony_ci				break;
97162306a36Sopenharmony_ci			case BPF_DW:
97262306a36Sopenharmony_ci				EMIT(PPC_RAW_LWZ(dst_reg_h, src_reg, off));
97362306a36Sopenharmony_ci				EMIT(PPC_RAW_LWZ(dst_reg, src_reg, off + 4));
97462306a36Sopenharmony_ci				break;
97562306a36Sopenharmony_ci			}
97662306a36Sopenharmony_ci
97762306a36Sopenharmony_ci			if (size != BPF_DW && !fp->aux->verifier_zext)
97862306a36Sopenharmony_ci				EMIT(PPC_RAW_LI(dst_reg_h, 0));
97962306a36Sopenharmony_ci
98062306a36Sopenharmony_ci			if (BPF_MODE(code) == BPF_PROBE_MEM) {
98162306a36Sopenharmony_ci				int insn_idx = ctx->idx - 1;
98262306a36Sopenharmony_ci				int jmp_off = 4;
98362306a36Sopenharmony_ci
98462306a36Sopenharmony_ci				/*
98562306a36Sopenharmony_ci				 * In case of BPF_DW, two lwz instructions are emitted, one
98662306a36Sopenharmony_ci				 * for higher 32-bit and another for lower 32-bit. So, set
98762306a36Sopenharmony_ci				 * ex->insn to the first of the two and jump over both
98862306a36Sopenharmony_ci				 * instructions in fixup.
98962306a36Sopenharmony_ci				 *
99062306a36Sopenharmony_ci				 * Similarly, with !verifier_zext, two instructions are
99162306a36Sopenharmony_ci				 * emitted for BPF_B/H/W case. So, set ex->insn to the
99262306a36Sopenharmony_ci				 * instruction that could fault and skip over both
99362306a36Sopenharmony_ci				 * instructions.
99462306a36Sopenharmony_ci				 */
99562306a36Sopenharmony_ci				if (size == BPF_DW || !fp->aux->verifier_zext) {
99662306a36Sopenharmony_ci					insn_idx -= 1;
99762306a36Sopenharmony_ci					jmp_off += 4;
99862306a36Sopenharmony_ci				}
99962306a36Sopenharmony_ci
100062306a36Sopenharmony_ci				ret = bpf_add_extable_entry(fp, image, pass, ctx, insn_idx,
100162306a36Sopenharmony_ci							    jmp_off, dst_reg);
100262306a36Sopenharmony_ci				if (ret)
100362306a36Sopenharmony_ci					return ret;
100462306a36Sopenharmony_ci			}
100562306a36Sopenharmony_ci			break;
100662306a36Sopenharmony_ci
100762306a36Sopenharmony_ci		/*
100862306a36Sopenharmony_ci		 * Doubleword load
100962306a36Sopenharmony_ci		 * 16 byte instruction that uses two 'struct bpf_insn'
101062306a36Sopenharmony_ci		 */
101162306a36Sopenharmony_ci		case BPF_LD | BPF_IMM | BPF_DW: /* dst = (u64) imm */
101262306a36Sopenharmony_ci			tmp_idx = ctx->idx;
101362306a36Sopenharmony_ci			PPC_LI32(dst_reg_h, (u32)insn[i + 1].imm);
101462306a36Sopenharmony_ci			PPC_LI32(dst_reg, (u32)insn[i].imm);
101562306a36Sopenharmony_ci			/* padding to allow full 4 instructions for later patching */
101662306a36Sopenharmony_ci			if (!image)
101762306a36Sopenharmony_ci				for (j = ctx->idx - tmp_idx; j < 4; j++)
101862306a36Sopenharmony_ci					EMIT(PPC_RAW_NOP());
101962306a36Sopenharmony_ci			/* Adjust for two bpf instructions */
102062306a36Sopenharmony_ci			addrs[++i] = ctx->idx * 4;
102162306a36Sopenharmony_ci			break;
102262306a36Sopenharmony_ci
102362306a36Sopenharmony_ci		/*
102462306a36Sopenharmony_ci		 * Return/Exit
102562306a36Sopenharmony_ci		 */
102662306a36Sopenharmony_ci		case BPF_JMP | BPF_EXIT:
102762306a36Sopenharmony_ci			/*
102862306a36Sopenharmony_ci			 * If this isn't the very last instruction, branch to
102962306a36Sopenharmony_ci			 * the epilogue. If we _are_ the last instruction,
103062306a36Sopenharmony_ci			 * we'll just fall through to the epilogue.
103162306a36Sopenharmony_ci			 */
103262306a36Sopenharmony_ci			if (i != flen - 1) {
103362306a36Sopenharmony_ci				ret = bpf_jit_emit_exit_insn(image, ctx, _R0, exit_addr);
103462306a36Sopenharmony_ci				if (ret)
103562306a36Sopenharmony_ci					return ret;
103662306a36Sopenharmony_ci			}
103762306a36Sopenharmony_ci			/* else fall through to the epilogue */
103862306a36Sopenharmony_ci			break;
103962306a36Sopenharmony_ci
104062306a36Sopenharmony_ci		/*
104162306a36Sopenharmony_ci		 * Call kernel helper or bpf function
104262306a36Sopenharmony_ci		 */
104362306a36Sopenharmony_ci		case BPF_JMP | BPF_CALL:
104462306a36Sopenharmony_ci			ctx->seen |= SEEN_FUNC;
104562306a36Sopenharmony_ci
104662306a36Sopenharmony_ci			ret = bpf_jit_get_func_addr(fp, &insn[i], extra_pass,
104762306a36Sopenharmony_ci						    &func_addr, &func_addr_fixed);
104862306a36Sopenharmony_ci			if (ret < 0)
104962306a36Sopenharmony_ci				return ret;
105062306a36Sopenharmony_ci
105162306a36Sopenharmony_ci			if (bpf_is_seen_register(ctx, bpf_to_ppc(BPF_REG_5))) {
105262306a36Sopenharmony_ci				EMIT(PPC_RAW_STW(bpf_to_ppc(BPF_REG_5) - 1, _R1, 8));
105362306a36Sopenharmony_ci				EMIT(PPC_RAW_STW(bpf_to_ppc(BPF_REG_5), _R1, 12));
105462306a36Sopenharmony_ci			}
105562306a36Sopenharmony_ci
105662306a36Sopenharmony_ci			ret = bpf_jit_emit_func_call_rel(image, ctx, func_addr);
105762306a36Sopenharmony_ci			if (ret)
105862306a36Sopenharmony_ci				return ret;
105962306a36Sopenharmony_ci
106062306a36Sopenharmony_ci			EMIT(PPC_RAW_MR(bpf_to_ppc(BPF_REG_0) - 1, _R3));
106162306a36Sopenharmony_ci			EMIT(PPC_RAW_MR(bpf_to_ppc(BPF_REG_0), _R4));
106262306a36Sopenharmony_ci			break;
106362306a36Sopenharmony_ci
106462306a36Sopenharmony_ci		/*
106562306a36Sopenharmony_ci		 * Jumps and branches
106662306a36Sopenharmony_ci		 */
106762306a36Sopenharmony_ci		case BPF_JMP | BPF_JA:
106862306a36Sopenharmony_ci			PPC_JMP(addrs[i + 1 + off]);
106962306a36Sopenharmony_ci			break;
107062306a36Sopenharmony_ci
107162306a36Sopenharmony_ci		case BPF_JMP | BPF_JGT | BPF_K:
107262306a36Sopenharmony_ci		case BPF_JMP | BPF_JGT | BPF_X:
107362306a36Sopenharmony_ci		case BPF_JMP | BPF_JSGT | BPF_K:
107462306a36Sopenharmony_ci		case BPF_JMP | BPF_JSGT | BPF_X:
107562306a36Sopenharmony_ci		case BPF_JMP32 | BPF_JGT | BPF_K:
107662306a36Sopenharmony_ci		case BPF_JMP32 | BPF_JGT | BPF_X:
107762306a36Sopenharmony_ci		case BPF_JMP32 | BPF_JSGT | BPF_K:
107862306a36Sopenharmony_ci		case BPF_JMP32 | BPF_JSGT | BPF_X:
107962306a36Sopenharmony_ci			true_cond = COND_GT;
108062306a36Sopenharmony_ci			goto cond_branch;
108162306a36Sopenharmony_ci		case BPF_JMP | BPF_JLT | BPF_K:
108262306a36Sopenharmony_ci		case BPF_JMP | BPF_JLT | BPF_X:
108362306a36Sopenharmony_ci		case BPF_JMP | BPF_JSLT | BPF_K:
108462306a36Sopenharmony_ci		case BPF_JMP | BPF_JSLT | BPF_X:
108562306a36Sopenharmony_ci		case BPF_JMP32 | BPF_JLT | BPF_K:
108662306a36Sopenharmony_ci		case BPF_JMP32 | BPF_JLT | BPF_X:
108762306a36Sopenharmony_ci		case BPF_JMP32 | BPF_JSLT | BPF_K:
108862306a36Sopenharmony_ci		case BPF_JMP32 | BPF_JSLT | BPF_X:
108962306a36Sopenharmony_ci			true_cond = COND_LT;
109062306a36Sopenharmony_ci			goto cond_branch;
109162306a36Sopenharmony_ci		case BPF_JMP | BPF_JGE | BPF_K:
109262306a36Sopenharmony_ci		case BPF_JMP | BPF_JGE | BPF_X:
109362306a36Sopenharmony_ci		case BPF_JMP | BPF_JSGE | BPF_K:
109462306a36Sopenharmony_ci		case BPF_JMP | BPF_JSGE | BPF_X:
109562306a36Sopenharmony_ci		case BPF_JMP32 | BPF_JGE | BPF_K:
109662306a36Sopenharmony_ci		case BPF_JMP32 | BPF_JGE | BPF_X:
109762306a36Sopenharmony_ci		case BPF_JMP32 | BPF_JSGE | BPF_K:
109862306a36Sopenharmony_ci		case BPF_JMP32 | BPF_JSGE | BPF_X:
109962306a36Sopenharmony_ci			true_cond = COND_GE;
110062306a36Sopenharmony_ci			goto cond_branch;
110162306a36Sopenharmony_ci		case BPF_JMP | BPF_JLE | BPF_K:
110262306a36Sopenharmony_ci		case BPF_JMP | BPF_JLE | BPF_X:
110362306a36Sopenharmony_ci		case BPF_JMP | BPF_JSLE | BPF_K:
110462306a36Sopenharmony_ci		case BPF_JMP | BPF_JSLE | BPF_X:
110562306a36Sopenharmony_ci		case BPF_JMP32 | BPF_JLE | BPF_K:
110662306a36Sopenharmony_ci		case BPF_JMP32 | BPF_JLE | BPF_X:
110762306a36Sopenharmony_ci		case BPF_JMP32 | BPF_JSLE | BPF_K:
110862306a36Sopenharmony_ci		case BPF_JMP32 | BPF_JSLE | BPF_X:
110962306a36Sopenharmony_ci			true_cond = COND_LE;
111062306a36Sopenharmony_ci			goto cond_branch;
111162306a36Sopenharmony_ci		case BPF_JMP | BPF_JEQ | BPF_K:
111262306a36Sopenharmony_ci		case BPF_JMP | BPF_JEQ | BPF_X:
111362306a36Sopenharmony_ci		case BPF_JMP32 | BPF_JEQ | BPF_K:
111462306a36Sopenharmony_ci		case BPF_JMP32 | BPF_JEQ | BPF_X:
111562306a36Sopenharmony_ci			true_cond = COND_EQ;
111662306a36Sopenharmony_ci			goto cond_branch;
111762306a36Sopenharmony_ci		case BPF_JMP | BPF_JNE | BPF_K:
111862306a36Sopenharmony_ci		case BPF_JMP | BPF_JNE | BPF_X:
111962306a36Sopenharmony_ci		case BPF_JMP32 | BPF_JNE | BPF_K:
112062306a36Sopenharmony_ci		case BPF_JMP32 | BPF_JNE | BPF_X:
112162306a36Sopenharmony_ci			true_cond = COND_NE;
112262306a36Sopenharmony_ci			goto cond_branch;
112362306a36Sopenharmony_ci		case BPF_JMP | BPF_JSET | BPF_K:
112462306a36Sopenharmony_ci		case BPF_JMP | BPF_JSET | BPF_X:
112562306a36Sopenharmony_ci		case BPF_JMP32 | BPF_JSET | BPF_K:
112662306a36Sopenharmony_ci		case BPF_JMP32 | BPF_JSET | BPF_X:
112762306a36Sopenharmony_ci			true_cond = COND_NE;
112862306a36Sopenharmony_ci			/* fallthrough; */
112962306a36Sopenharmony_ci
113062306a36Sopenharmony_cicond_branch:
113162306a36Sopenharmony_ci			switch (code) {
113262306a36Sopenharmony_ci			case BPF_JMP | BPF_JGT | BPF_X:
113362306a36Sopenharmony_ci			case BPF_JMP | BPF_JLT | BPF_X:
113462306a36Sopenharmony_ci			case BPF_JMP | BPF_JGE | BPF_X:
113562306a36Sopenharmony_ci			case BPF_JMP | BPF_JLE | BPF_X:
113662306a36Sopenharmony_ci			case BPF_JMP | BPF_JEQ | BPF_X:
113762306a36Sopenharmony_ci			case BPF_JMP | BPF_JNE | BPF_X:
113862306a36Sopenharmony_ci				/* unsigned comparison */
113962306a36Sopenharmony_ci				EMIT(PPC_RAW_CMPLW(dst_reg_h, src_reg_h));
114062306a36Sopenharmony_ci				PPC_BCC_SHORT(COND_NE, (ctx->idx + 2) * 4);
114162306a36Sopenharmony_ci				EMIT(PPC_RAW_CMPLW(dst_reg, src_reg));
114262306a36Sopenharmony_ci				break;
114362306a36Sopenharmony_ci			case BPF_JMP32 | BPF_JGT | BPF_X:
114462306a36Sopenharmony_ci			case BPF_JMP32 | BPF_JLT | BPF_X:
114562306a36Sopenharmony_ci			case BPF_JMP32 | BPF_JGE | BPF_X:
114662306a36Sopenharmony_ci			case BPF_JMP32 | BPF_JLE | BPF_X:
114762306a36Sopenharmony_ci			case BPF_JMP32 | BPF_JEQ | BPF_X:
114862306a36Sopenharmony_ci			case BPF_JMP32 | BPF_JNE | BPF_X:
114962306a36Sopenharmony_ci				/* unsigned comparison */
115062306a36Sopenharmony_ci				EMIT(PPC_RAW_CMPLW(dst_reg, src_reg));
115162306a36Sopenharmony_ci				break;
115262306a36Sopenharmony_ci			case BPF_JMP | BPF_JSGT | BPF_X:
115362306a36Sopenharmony_ci			case BPF_JMP | BPF_JSLT | BPF_X:
115462306a36Sopenharmony_ci			case BPF_JMP | BPF_JSGE | BPF_X:
115562306a36Sopenharmony_ci			case BPF_JMP | BPF_JSLE | BPF_X:
115662306a36Sopenharmony_ci				/* signed comparison */
115762306a36Sopenharmony_ci				EMIT(PPC_RAW_CMPW(dst_reg_h, src_reg_h));
115862306a36Sopenharmony_ci				PPC_BCC_SHORT(COND_NE, (ctx->idx + 2) * 4);
115962306a36Sopenharmony_ci				EMIT(PPC_RAW_CMPLW(dst_reg, src_reg));
116062306a36Sopenharmony_ci				break;
116162306a36Sopenharmony_ci			case BPF_JMP32 | BPF_JSGT | BPF_X:
116262306a36Sopenharmony_ci			case BPF_JMP32 | BPF_JSLT | BPF_X:
116362306a36Sopenharmony_ci			case BPF_JMP32 | BPF_JSGE | BPF_X:
116462306a36Sopenharmony_ci			case BPF_JMP32 | BPF_JSLE | BPF_X:
116562306a36Sopenharmony_ci				/* signed comparison */
116662306a36Sopenharmony_ci				EMIT(PPC_RAW_CMPW(dst_reg, src_reg));
116762306a36Sopenharmony_ci				break;
116862306a36Sopenharmony_ci			case BPF_JMP | BPF_JSET | BPF_X:
116962306a36Sopenharmony_ci				EMIT(PPC_RAW_AND_DOT(_R0, dst_reg_h, src_reg_h));
117062306a36Sopenharmony_ci				PPC_BCC_SHORT(COND_NE, (ctx->idx + 2) * 4);
117162306a36Sopenharmony_ci				EMIT(PPC_RAW_AND_DOT(_R0, dst_reg, src_reg));
117262306a36Sopenharmony_ci				break;
117362306a36Sopenharmony_ci			case BPF_JMP32 | BPF_JSET | BPF_X: {
117462306a36Sopenharmony_ci				EMIT(PPC_RAW_AND_DOT(_R0, dst_reg, src_reg));
117562306a36Sopenharmony_ci				break;
117662306a36Sopenharmony_ci			case BPF_JMP | BPF_JNE | BPF_K:
117762306a36Sopenharmony_ci			case BPF_JMP | BPF_JEQ | BPF_K:
117862306a36Sopenharmony_ci			case BPF_JMP | BPF_JGT | BPF_K:
117962306a36Sopenharmony_ci			case BPF_JMP | BPF_JLT | BPF_K:
118062306a36Sopenharmony_ci			case BPF_JMP | BPF_JGE | BPF_K:
118162306a36Sopenharmony_ci			case BPF_JMP | BPF_JLE | BPF_K:
118262306a36Sopenharmony_ci				/*
118362306a36Sopenharmony_ci				 * Need sign-extended load, so only positive
118462306a36Sopenharmony_ci				 * values can be used as imm in cmplwi
118562306a36Sopenharmony_ci				 */
118662306a36Sopenharmony_ci				if (imm >= 0 && imm < 32768) {
118762306a36Sopenharmony_ci					EMIT(PPC_RAW_CMPLWI(dst_reg_h, 0));
118862306a36Sopenharmony_ci					PPC_BCC_SHORT(COND_NE, (ctx->idx + 2) * 4);
118962306a36Sopenharmony_ci					EMIT(PPC_RAW_CMPLWI(dst_reg, imm));
119062306a36Sopenharmony_ci				} else {
119162306a36Sopenharmony_ci					/* sign-extending load ... but unsigned comparison */
119262306a36Sopenharmony_ci					PPC_EX32(_R0, imm);
119362306a36Sopenharmony_ci					EMIT(PPC_RAW_CMPLW(dst_reg_h, _R0));
119462306a36Sopenharmony_ci					PPC_LI32(_R0, imm);
119562306a36Sopenharmony_ci					PPC_BCC_SHORT(COND_NE, (ctx->idx + 2) * 4);
119662306a36Sopenharmony_ci					EMIT(PPC_RAW_CMPLW(dst_reg, _R0));
119762306a36Sopenharmony_ci				}
119862306a36Sopenharmony_ci				break;
119962306a36Sopenharmony_ci			case BPF_JMP32 | BPF_JNE | BPF_K:
120062306a36Sopenharmony_ci			case BPF_JMP32 | BPF_JEQ | BPF_K:
120162306a36Sopenharmony_ci			case BPF_JMP32 | BPF_JGT | BPF_K:
120262306a36Sopenharmony_ci			case BPF_JMP32 | BPF_JLT | BPF_K:
120362306a36Sopenharmony_ci			case BPF_JMP32 | BPF_JGE | BPF_K:
120462306a36Sopenharmony_ci			case BPF_JMP32 | BPF_JLE | BPF_K:
120562306a36Sopenharmony_ci				if (imm >= 0 && imm < 65536) {
120662306a36Sopenharmony_ci					EMIT(PPC_RAW_CMPLWI(dst_reg, imm));
120762306a36Sopenharmony_ci				} else {
120862306a36Sopenharmony_ci					PPC_LI32(_R0, imm);
120962306a36Sopenharmony_ci					EMIT(PPC_RAW_CMPLW(dst_reg, _R0));
121062306a36Sopenharmony_ci				}
121162306a36Sopenharmony_ci				break;
121262306a36Sopenharmony_ci			}
121362306a36Sopenharmony_ci			case BPF_JMP | BPF_JSGT | BPF_K:
121462306a36Sopenharmony_ci			case BPF_JMP | BPF_JSLT | BPF_K:
121562306a36Sopenharmony_ci			case BPF_JMP | BPF_JSGE | BPF_K:
121662306a36Sopenharmony_ci			case BPF_JMP | BPF_JSLE | BPF_K:
121762306a36Sopenharmony_ci				if (imm >= 0 && imm < 65536) {
121862306a36Sopenharmony_ci					EMIT(PPC_RAW_CMPWI(dst_reg_h, imm < 0 ? -1 : 0));
121962306a36Sopenharmony_ci					PPC_BCC_SHORT(COND_NE, (ctx->idx + 2) * 4);
122062306a36Sopenharmony_ci					EMIT(PPC_RAW_CMPLWI(dst_reg, imm));
122162306a36Sopenharmony_ci				} else {
122262306a36Sopenharmony_ci					/* sign-extending load */
122362306a36Sopenharmony_ci					EMIT(PPC_RAW_CMPWI(dst_reg_h, imm < 0 ? -1 : 0));
122462306a36Sopenharmony_ci					PPC_LI32(_R0, imm);
122562306a36Sopenharmony_ci					PPC_BCC_SHORT(COND_NE, (ctx->idx + 2) * 4);
122662306a36Sopenharmony_ci					EMIT(PPC_RAW_CMPLW(dst_reg, _R0));
122762306a36Sopenharmony_ci				}
122862306a36Sopenharmony_ci				break;
122962306a36Sopenharmony_ci			case BPF_JMP32 | BPF_JSGT | BPF_K:
123062306a36Sopenharmony_ci			case BPF_JMP32 | BPF_JSLT | BPF_K:
123162306a36Sopenharmony_ci			case BPF_JMP32 | BPF_JSGE | BPF_K:
123262306a36Sopenharmony_ci			case BPF_JMP32 | BPF_JSLE | BPF_K:
123362306a36Sopenharmony_ci				/*
123462306a36Sopenharmony_ci				 * signed comparison, so any 16-bit value
123562306a36Sopenharmony_ci				 * can be used in cmpwi
123662306a36Sopenharmony_ci				 */
123762306a36Sopenharmony_ci				if (imm >= -32768 && imm < 32768) {
123862306a36Sopenharmony_ci					EMIT(PPC_RAW_CMPWI(dst_reg, imm));
123962306a36Sopenharmony_ci				} else {
124062306a36Sopenharmony_ci					/* sign-extending load */
124162306a36Sopenharmony_ci					PPC_LI32(_R0, imm);
124262306a36Sopenharmony_ci					EMIT(PPC_RAW_CMPW(dst_reg, _R0));
124362306a36Sopenharmony_ci				}
124462306a36Sopenharmony_ci				break;
124562306a36Sopenharmony_ci			case BPF_JMP | BPF_JSET | BPF_K:
124662306a36Sopenharmony_ci				/* andi does not sign-extend the immediate */
124762306a36Sopenharmony_ci				if (imm >= 0 && imm < 32768) {
124862306a36Sopenharmony_ci					/* PPC_ANDI is _only/always_ dot-form */
124962306a36Sopenharmony_ci					EMIT(PPC_RAW_ANDI(_R0, dst_reg, imm));
125062306a36Sopenharmony_ci				} else {
125162306a36Sopenharmony_ci					PPC_LI32(_R0, imm);
125262306a36Sopenharmony_ci					if (imm < 0) {
125362306a36Sopenharmony_ci						EMIT(PPC_RAW_CMPWI(dst_reg_h, 0));
125462306a36Sopenharmony_ci						PPC_BCC_SHORT(COND_NE, (ctx->idx + 2) * 4);
125562306a36Sopenharmony_ci					}
125662306a36Sopenharmony_ci					EMIT(PPC_RAW_AND_DOT(_R0, dst_reg, _R0));
125762306a36Sopenharmony_ci				}
125862306a36Sopenharmony_ci				break;
125962306a36Sopenharmony_ci			case BPF_JMP32 | BPF_JSET | BPF_K:
126062306a36Sopenharmony_ci				/* andi does not sign-extend the immediate */
126162306a36Sopenharmony_ci				if (imm >= 0 && imm < 32768) {
126262306a36Sopenharmony_ci					/* PPC_ANDI is _only/always_ dot-form */
126362306a36Sopenharmony_ci					EMIT(PPC_RAW_ANDI(_R0, dst_reg, imm));
126462306a36Sopenharmony_ci				} else {
126562306a36Sopenharmony_ci					PPC_LI32(_R0, imm);
126662306a36Sopenharmony_ci					EMIT(PPC_RAW_AND_DOT(_R0, dst_reg, _R0));
126762306a36Sopenharmony_ci				}
126862306a36Sopenharmony_ci				break;
126962306a36Sopenharmony_ci			}
127062306a36Sopenharmony_ci			PPC_BCC(true_cond, addrs[i + 1 + off]);
127162306a36Sopenharmony_ci			break;
127262306a36Sopenharmony_ci
127362306a36Sopenharmony_ci		/*
127462306a36Sopenharmony_ci		 * Tail call
127562306a36Sopenharmony_ci		 */
127662306a36Sopenharmony_ci		case BPF_JMP | BPF_TAIL_CALL:
127762306a36Sopenharmony_ci			ctx->seen |= SEEN_TAILCALL;
127862306a36Sopenharmony_ci			ret = bpf_jit_emit_tail_call(image, ctx, addrs[i + 1]);
127962306a36Sopenharmony_ci			if (ret < 0)
128062306a36Sopenharmony_ci				return ret;
128162306a36Sopenharmony_ci			break;
128262306a36Sopenharmony_ci
128362306a36Sopenharmony_ci		default:
128462306a36Sopenharmony_ci			/*
128562306a36Sopenharmony_ci			 * The filter contains something cruel & unusual.
128662306a36Sopenharmony_ci			 * We don't handle it, but also there shouldn't be
128762306a36Sopenharmony_ci			 * anything missing from our list.
128862306a36Sopenharmony_ci			 */
128962306a36Sopenharmony_ci			pr_err_ratelimited("eBPF filter opcode %04x (@%d) unsupported\n", code, i);
129062306a36Sopenharmony_ci			return -EOPNOTSUPP;
129162306a36Sopenharmony_ci		}
129262306a36Sopenharmony_ci		if (BPF_CLASS(code) == BPF_ALU && !fp->aux->verifier_zext &&
129362306a36Sopenharmony_ci		    !insn_is_zext(&insn[i + 1]) && !(BPF_OP(code) == BPF_END && imm == 64))
129462306a36Sopenharmony_ci			EMIT(PPC_RAW_LI(dst_reg_h, 0));
129562306a36Sopenharmony_ci	}
129662306a36Sopenharmony_ci
129762306a36Sopenharmony_ci	/* Set end-of-body-code address for exit. */
129862306a36Sopenharmony_ci	addrs[i] = ctx->idx * 4;
129962306a36Sopenharmony_ci
130062306a36Sopenharmony_ci	return 0;
130162306a36Sopenharmony_ci}
1302