162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (C) 2004 Paul Mackerras <paulus@au.ibm.com>, IBM
462306a36Sopenharmony_ci */
562306a36Sopenharmony_ci#include <asm/inst.h>
662306a36Sopenharmony_ci
762306a36Sopenharmony_cistruct pt_regs;
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci/*
1062306a36Sopenharmony_ci * We don't allow single-stepping an mtmsrd that would clear
1162306a36Sopenharmony_ci * MSR_RI, since that would make the exception unrecoverable.
1262306a36Sopenharmony_ci * Since we need to single-step to proceed from a breakpoint,
1362306a36Sopenharmony_ci * we don't allow putting a breakpoint on an mtmsrd instruction.
1462306a36Sopenharmony_ci * Similarly we don't allow breakpoints on rfid instructions.
1562306a36Sopenharmony_ci * These macros tell us if an instruction is a mtmsrd or rfid.
1662306a36Sopenharmony_ci * Note that these return true for both mtmsr/rfi (32-bit)
1762306a36Sopenharmony_ci * and mtmsrd/rfid (64-bit).
1862306a36Sopenharmony_ci */
1962306a36Sopenharmony_ci#define IS_MTMSRD(instr)	((ppc_inst_val(instr) & 0xfc0007be) == 0x7c000124)
2062306a36Sopenharmony_ci#define IS_RFID(instr)		((ppc_inst_val(instr) & 0xfc0007be) == 0x4c000024)
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_cienum instruction_type {
2362306a36Sopenharmony_ci	COMPUTE,		/* arith/logical/CR op, etc. */
2462306a36Sopenharmony_ci	LOAD,			/* load and store types need to be contiguous */
2562306a36Sopenharmony_ci	LOAD_MULTI,
2662306a36Sopenharmony_ci	LOAD_FP,
2762306a36Sopenharmony_ci	LOAD_VMX,
2862306a36Sopenharmony_ci	LOAD_VSX,
2962306a36Sopenharmony_ci	STORE,
3062306a36Sopenharmony_ci	STORE_MULTI,
3162306a36Sopenharmony_ci	STORE_FP,
3262306a36Sopenharmony_ci	STORE_VMX,
3362306a36Sopenharmony_ci	STORE_VSX,
3462306a36Sopenharmony_ci	LARX,
3562306a36Sopenharmony_ci	STCX,
3662306a36Sopenharmony_ci	BRANCH,
3762306a36Sopenharmony_ci	MFSPR,
3862306a36Sopenharmony_ci	MTSPR,
3962306a36Sopenharmony_ci	CACHEOP,
4062306a36Sopenharmony_ci	BARRIER,
4162306a36Sopenharmony_ci	SYSCALL,
4262306a36Sopenharmony_ci	SYSCALL_VECTORED_0,
4362306a36Sopenharmony_ci	MFMSR,
4462306a36Sopenharmony_ci	MTMSR,
4562306a36Sopenharmony_ci	RFI,
4662306a36Sopenharmony_ci	INTERRUPT,
4762306a36Sopenharmony_ci	UNKNOWN
4862306a36Sopenharmony_ci};
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ci#define INSTR_TYPE_MASK	0x1f
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ci#define OP_IS_LOAD(type)	((LOAD <= (type) && (type) <= LOAD_VSX) || (type) == LARX)
5362306a36Sopenharmony_ci#define OP_IS_STORE(type)	((STORE <= (type) && (type) <= STORE_VSX) || (type) == STCX)
5462306a36Sopenharmony_ci#define OP_IS_LOAD_STORE(type)	(LOAD <= (type) && (type) <= STCX)
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci/* Compute flags, ORed in with type */
5762306a36Sopenharmony_ci#define SETREG		0x20
5862306a36Sopenharmony_ci#define SETCC		0x40
5962306a36Sopenharmony_ci#define SETXER		0x80
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_ci/* Branch flags, ORed in with type */
6262306a36Sopenharmony_ci#define SETLK		0x20
6362306a36Sopenharmony_ci#define BRTAKEN		0x40
6462306a36Sopenharmony_ci#define DECCTR		0x80
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci/* Load/store flags, ORed in with type */
6762306a36Sopenharmony_ci#define SIGNEXT		0x20
6862306a36Sopenharmony_ci#define UPDATE		0x40	/* matches bit in opcode 31 instructions */
6962306a36Sopenharmony_ci#define BYTEREV		0x80
7062306a36Sopenharmony_ci#define FPCONV		0x100
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci/* Barrier type field, ORed in with type */
7362306a36Sopenharmony_ci#define BARRIER_MASK	0xe0
7462306a36Sopenharmony_ci#define BARRIER_SYNC	0x00
7562306a36Sopenharmony_ci#define BARRIER_ISYNC	0x20
7662306a36Sopenharmony_ci#define BARRIER_EIEIO	0x40
7762306a36Sopenharmony_ci#define BARRIER_LWSYNC	0x60
7862306a36Sopenharmony_ci#define BARRIER_PTESYNC	0x80
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_ci/* Cacheop values, ORed in with type */
8162306a36Sopenharmony_ci#define CACHEOP_MASK	0x700
8262306a36Sopenharmony_ci#define DCBST		0
8362306a36Sopenharmony_ci#define DCBF		0x100
8462306a36Sopenharmony_ci#define DCBTST		0x200
8562306a36Sopenharmony_ci#define DCBT		0x300
8662306a36Sopenharmony_ci#define ICBI		0x400
8762306a36Sopenharmony_ci#define DCBZ		0x500
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_ci/* VSX flags values */
9062306a36Sopenharmony_ci#define VSX_FPCONV	1	/* do floating point SP/DP conversion */
9162306a36Sopenharmony_ci#define VSX_SPLAT	2	/* store loaded value into all elements */
9262306a36Sopenharmony_ci#define VSX_LDLEFT	4	/* load VSX register from left */
9362306a36Sopenharmony_ci#define VSX_CHECK_VEC	8	/* check MSR_VEC not MSR_VSX for reg >= 32 */
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ci/* Prefixed flag, ORed in with type */
9662306a36Sopenharmony_ci#define PREFIXED       0x800
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ci/* Size field in type word */
9962306a36Sopenharmony_ci#define SIZE(n)		((n) << 12)
10062306a36Sopenharmony_ci#define GETSIZE(w)	((w) >> 12)
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_ci#define GETTYPE(t)	((t) & INSTR_TYPE_MASK)
10362306a36Sopenharmony_ci#define GETLENGTH(t)   (((t) & PREFIXED) ? 8 : 4)
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ci#define MKOP(t, f, s)	((t) | (f) | SIZE(s))
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_ci/* Prefix instruction operands */
10862306a36Sopenharmony_ci#define GET_PREFIX_RA(i)	(((i) >> 16) & 0x1f)
10962306a36Sopenharmony_ci#define GET_PREFIX_R(i)		((i) & (1ul << 20))
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_ciextern s32 patch__exec_instr;
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_cistruct instruction_op {
11462306a36Sopenharmony_ci	int type;
11562306a36Sopenharmony_ci	int reg;
11662306a36Sopenharmony_ci	unsigned long val;
11762306a36Sopenharmony_ci	/* For LOAD/STORE/LARX/STCX */
11862306a36Sopenharmony_ci	unsigned long ea;
11962306a36Sopenharmony_ci	int update_reg;
12062306a36Sopenharmony_ci	/* For MFSPR */
12162306a36Sopenharmony_ci	int spr;
12262306a36Sopenharmony_ci	u32 ccval;
12362306a36Sopenharmony_ci	u32 xerval;
12462306a36Sopenharmony_ci	u8 element_size;	/* for VSX/VMX loads/stores */
12562306a36Sopenharmony_ci	u8 vsx_flags;
12662306a36Sopenharmony_ci};
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_ciunion vsx_reg {
12962306a36Sopenharmony_ci	u8	b[16];
13062306a36Sopenharmony_ci	u16	h[8];
13162306a36Sopenharmony_ci	u32	w[4];
13262306a36Sopenharmony_ci	unsigned long d[2];
13362306a36Sopenharmony_ci	float	fp[4];
13462306a36Sopenharmony_ci	double	dp[2];
13562306a36Sopenharmony_ci	__vector128 v;
13662306a36Sopenharmony_ci};
13762306a36Sopenharmony_ci
13862306a36Sopenharmony_ci/*
13962306a36Sopenharmony_ci * Decode an instruction, and return information about it in *op
14062306a36Sopenharmony_ci * without changing *regs.
14162306a36Sopenharmony_ci *
14262306a36Sopenharmony_ci * Return value is 1 if the instruction can be emulated just by
14362306a36Sopenharmony_ci * updating *regs with the information in *op, -1 if we need the
14462306a36Sopenharmony_ci * GPRs but *regs doesn't contain the full register set, or 0
14562306a36Sopenharmony_ci * otherwise.
14662306a36Sopenharmony_ci */
14762306a36Sopenharmony_ciextern int analyse_instr(struct instruction_op *op, const struct pt_regs *regs,
14862306a36Sopenharmony_ci			 ppc_inst_t instr);
14962306a36Sopenharmony_ci
15062306a36Sopenharmony_ci/*
15162306a36Sopenharmony_ci * Emulate an instruction that can be executed just by updating
15262306a36Sopenharmony_ci * fields in *regs.
15362306a36Sopenharmony_ci */
15462306a36Sopenharmony_civoid emulate_update_regs(struct pt_regs *reg, struct instruction_op *op);
15562306a36Sopenharmony_ci
15662306a36Sopenharmony_ci/*
15762306a36Sopenharmony_ci * Emulate instructions that cause a transfer of control,
15862306a36Sopenharmony_ci * arithmetic/logical instructions, loads and stores,
15962306a36Sopenharmony_ci * cache operations and barriers.
16062306a36Sopenharmony_ci *
16162306a36Sopenharmony_ci * Returns 1 if the instruction was emulated successfully,
16262306a36Sopenharmony_ci * 0 if it could not be emulated, or -1 for an instruction that
16362306a36Sopenharmony_ci * should not be emulated (rfid, mtmsrd clearing MSR_RI, etc.).
16462306a36Sopenharmony_ci */
16562306a36Sopenharmony_ciint emulate_step(struct pt_regs *regs, ppc_inst_t instr);
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_ci/*
16862306a36Sopenharmony_ci * Emulate a load or store instruction by reading/writing the
16962306a36Sopenharmony_ci * memory of the current process.  FP/VMX/VSX registers are assumed
17062306a36Sopenharmony_ci * to hold live values if the appropriate enable bit in regs->msr is
17162306a36Sopenharmony_ci * set; otherwise this will use the saved values in the thread struct
17262306a36Sopenharmony_ci * for user-mode accesses.
17362306a36Sopenharmony_ci */
17462306a36Sopenharmony_ciextern int emulate_loadstore(struct pt_regs *regs, struct instruction_op *op);
17562306a36Sopenharmony_ci
17662306a36Sopenharmony_ciextern void emulate_vsx_load(struct instruction_op *op, union vsx_reg *reg,
17762306a36Sopenharmony_ci			     const void *mem, bool cross_endian);
17862306a36Sopenharmony_ciextern void emulate_vsx_store(struct instruction_op *op,
17962306a36Sopenharmony_ci			      const union vsx_reg *reg, void *mem,
18062306a36Sopenharmony_ci			      bool cross_endian);
18162306a36Sopenharmony_ciextern int emulate_dcbz(unsigned long ea, struct pt_regs *regs);
182