18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */ 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (C) 2004 Paul Mackerras <paulus@au.ibm.com>, IBM 48c2ecf20Sopenharmony_ci */ 58c2ecf20Sopenharmony_ci#include <asm/inst.h> 68c2ecf20Sopenharmony_ci 78c2ecf20Sopenharmony_cistruct pt_regs; 88c2ecf20Sopenharmony_ci 98c2ecf20Sopenharmony_ci/* 108c2ecf20Sopenharmony_ci * We don't allow single-stepping an mtmsrd that would clear 118c2ecf20Sopenharmony_ci * MSR_RI, since that would make the exception unrecoverable. 128c2ecf20Sopenharmony_ci * Since we need to single-step to proceed from a breakpoint, 138c2ecf20Sopenharmony_ci * we don't allow putting a breakpoint on an mtmsrd instruction. 148c2ecf20Sopenharmony_ci * Similarly we don't allow breakpoints on rfid instructions. 158c2ecf20Sopenharmony_ci * These macros tell us if an instruction is a mtmsrd or rfid. 168c2ecf20Sopenharmony_ci * Note that IS_MTMSRD returns true for both an mtmsr (32-bit) 178c2ecf20Sopenharmony_ci * and an mtmsrd (64-bit). 188c2ecf20Sopenharmony_ci */ 198c2ecf20Sopenharmony_ci#define IS_MTMSRD(instr) ((ppc_inst_val(instr) & 0xfc0007be) == 0x7c000124) 208c2ecf20Sopenharmony_ci#define IS_RFID(instr) ((ppc_inst_val(instr) & 0xfc0007fe) == 0x4c000024) 218c2ecf20Sopenharmony_ci#define IS_RFI(instr) ((ppc_inst_val(instr) & 0xfc0007fe) == 0x4c000064) 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_cienum instruction_type { 248c2ecf20Sopenharmony_ci COMPUTE, /* arith/logical/CR op, etc. */ 258c2ecf20Sopenharmony_ci LOAD, /* load and store types need to be contiguous */ 268c2ecf20Sopenharmony_ci LOAD_MULTI, 278c2ecf20Sopenharmony_ci LOAD_FP, 288c2ecf20Sopenharmony_ci LOAD_VMX, 298c2ecf20Sopenharmony_ci LOAD_VSX, 308c2ecf20Sopenharmony_ci STORE, 318c2ecf20Sopenharmony_ci STORE_MULTI, 328c2ecf20Sopenharmony_ci STORE_FP, 338c2ecf20Sopenharmony_ci STORE_VMX, 348c2ecf20Sopenharmony_ci STORE_VSX, 358c2ecf20Sopenharmony_ci LARX, 368c2ecf20Sopenharmony_ci STCX, 378c2ecf20Sopenharmony_ci BRANCH, 388c2ecf20Sopenharmony_ci MFSPR, 398c2ecf20Sopenharmony_ci MTSPR, 408c2ecf20Sopenharmony_ci CACHEOP, 418c2ecf20Sopenharmony_ci BARRIER, 428c2ecf20Sopenharmony_ci SYSCALL, 438c2ecf20Sopenharmony_ci SYSCALL_VECTORED_0, 448c2ecf20Sopenharmony_ci MFMSR, 458c2ecf20Sopenharmony_ci MTMSR, 468c2ecf20Sopenharmony_ci RFI, 478c2ecf20Sopenharmony_ci INTERRUPT, 488c2ecf20Sopenharmony_ci UNKNOWN 498c2ecf20Sopenharmony_ci}; 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_ci#define INSTR_TYPE_MASK 0x1f 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_ci#define OP_IS_LOAD(type) ((LOAD <= (type) && (type) <= LOAD_VSX) || (type) == LARX) 548c2ecf20Sopenharmony_ci#define OP_IS_STORE(type) ((STORE <= (type) && (type) <= STORE_VSX) || (type) == STCX) 558c2ecf20Sopenharmony_ci#define OP_IS_LOAD_STORE(type) (LOAD <= (type) && (type) <= STCX) 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_ci/* Compute flags, ORed in with type */ 588c2ecf20Sopenharmony_ci#define SETREG 0x20 598c2ecf20Sopenharmony_ci#define SETCC 0x40 608c2ecf20Sopenharmony_ci#define SETXER 0x80 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ci/* Branch flags, ORed in with type */ 638c2ecf20Sopenharmony_ci#define SETLK 0x20 648c2ecf20Sopenharmony_ci#define BRTAKEN 0x40 658c2ecf20Sopenharmony_ci#define DECCTR 0x80 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_ci/* Load/store flags, ORed in with type */ 688c2ecf20Sopenharmony_ci#define SIGNEXT 0x20 698c2ecf20Sopenharmony_ci#define UPDATE 0x40 /* matches bit in opcode 31 instructions */ 708c2ecf20Sopenharmony_ci#define BYTEREV 0x80 718c2ecf20Sopenharmony_ci#define FPCONV 0x100 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_ci/* Barrier type field, ORed in with type */ 748c2ecf20Sopenharmony_ci#define BARRIER_MASK 0xe0 758c2ecf20Sopenharmony_ci#define BARRIER_SYNC 0x00 768c2ecf20Sopenharmony_ci#define BARRIER_ISYNC 0x20 778c2ecf20Sopenharmony_ci#define BARRIER_EIEIO 0x40 788c2ecf20Sopenharmony_ci#define BARRIER_LWSYNC 0x60 798c2ecf20Sopenharmony_ci#define BARRIER_PTESYNC 0x80 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci/* Cacheop values, ORed in with type */ 828c2ecf20Sopenharmony_ci#define CACHEOP_MASK 0x700 838c2ecf20Sopenharmony_ci#define DCBST 0 848c2ecf20Sopenharmony_ci#define DCBF 0x100 858c2ecf20Sopenharmony_ci#define DCBTST 0x200 868c2ecf20Sopenharmony_ci#define DCBT 0x300 878c2ecf20Sopenharmony_ci#define ICBI 0x400 888c2ecf20Sopenharmony_ci#define DCBZ 0x500 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci/* VSX flags values */ 918c2ecf20Sopenharmony_ci#define VSX_FPCONV 1 /* do floating point SP/DP conversion */ 928c2ecf20Sopenharmony_ci#define VSX_SPLAT 2 /* store loaded value into all elements */ 938c2ecf20Sopenharmony_ci#define VSX_LDLEFT 4 /* load VSX register from left */ 948c2ecf20Sopenharmony_ci#define VSX_CHECK_VEC 8 /* check MSR_VEC not MSR_VSX for reg >= 32 */ 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_ci/* Prefixed flag, ORed in with type */ 978c2ecf20Sopenharmony_ci#define PREFIXED 0x800 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_ci/* Size field in type word */ 1008c2ecf20Sopenharmony_ci#define SIZE(n) ((n) << 12) 1018c2ecf20Sopenharmony_ci#define GETSIZE(w) ((w) >> 12) 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_ci#define GETTYPE(t) ((t) & INSTR_TYPE_MASK) 1048c2ecf20Sopenharmony_ci#define GETLENGTH(t) (((t) & PREFIXED) ? 8 : 4) 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ci#define MKOP(t, f, s) ((t) | (f) | SIZE(s)) 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_ci/* Prefix instruction operands */ 1098c2ecf20Sopenharmony_ci#define GET_PREFIX_RA(i) (((i) >> 16) & 0x1f) 1108c2ecf20Sopenharmony_ci#define GET_PREFIX_R(i) ((i) & (1ul << 20)) 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ciextern s32 patch__exec_instr; 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_cistruct instruction_op { 1158c2ecf20Sopenharmony_ci int type; 1168c2ecf20Sopenharmony_ci int reg; 1178c2ecf20Sopenharmony_ci unsigned long val; 1188c2ecf20Sopenharmony_ci /* For LOAD/STORE/LARX/STCX */ 1198c2ecf20Sopenharmony_ci unsigned long ea; 1208c2ecf20Sopenharmony_ci int update_reg; 1218c2ecf20Sopenharmony_ci /* For MFSPR */ 1228c2ecf20Sopenharmony_ci int spr; 1238c2ecf20Sopenharmony_ci u32 ccval; 1248c2ecf20Sopenharmony_ci u32 xerval; 1258c2ecf20Sopenharmony_ci u8 element_size; /* for VSX/VMX loads/stores */ 1268c2ecf20Sopenharmony_ci u8 vsx_flags; 1278c2ecf20Sopenharmony_ci}; 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ciunion vsx_reg { 1308c2ecf20Sopenharmony_ci u8 b[16]; 1318c2ecf20Sopenharmony_ci u16 h[8]; 1328c2ecf20Sopenharmony_ci u32 w[4]; 1338c2ecf20Sopenharmony_ci unsigned long d[2]; 1348c2ecf20Sopenharmony_ci float fp[4]; 1358c2ecf20Sopenharmony_ci double dp[2]; 1368c2ecf20Sopenharmony_ci __vector128 v; 1378c2ecf20Sopenharmony_ci}; 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci/* 1408c2ecf20Sopenharmony_ci * Decode an instruction, and return information about it in *op 1418c2ecf20Sopenharmony_ci * without changing *regs. 1428c2ecf20Sopenharmony_ci * 1438c2ecf20Sopenharmony_ci * Return value is 1 if the instruction can be emulated just by 1448c2ecf20Sopenharmony_ci * updating *regs with the information in *op, -1 if we need the 1458c2ecf20Sopenharmony_ci * GPRs but *regs doesn't contain the full register set, or 0 1468c2ecf20Sopenharmony_ci * otherwise. 1478c2ecf20Sopenharmony_ci */ 1488c2ecf20Sopenharmony_ciextern int analyse_instr(struct instruction_op *op, const struct pt_regs *regs, 1498c2ecf20Sopenharmony_ci struct ppc_inst instr); 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_ci/* 1528c2ecf20Sopenharmony_ci * Emulate an instruction that can be executed just by updating 1538c2ecf20Sopenharmony_ci * fields in *regs. 1548c2ecf20Sopenharmony_ci */ 1558c2ecf20Sopenharmony_civoid emulate_update_regs(struct pt_regs *reg, struct instruction_op *op); 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_ci/* 1588c2ecf20Sopenharmony_ci * Emulate instructions that cause a transfer of control, 1598c2ecf20Sopenharmony_ci * arithmetic/logical instructions, loads and stores, 1608c2ecf20Sopenharmony_ci * cache operations and barriers. 1618c2ecf20Sopenharmony_ci * 1628c2ecf20Sopenharmony_ci * Returns 1 if the instruction was emulated successfully, 1638c2ecf20Sopenharmony_ci * 0 if it could not be emulated, or -1 for an instruction that 1648c2ecf20Sopenharmony_ci * should not be emulated (rfid, mtmsrd clearing MSR_RI, etc.). 1658c2ecf20Sopenharmony_ci */ 1668c2ecf20Sopenharmony_ciextern int emulate_step(struct pt_regs *regs, struct ppc_inst instr); 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_ci/* 1698c2ecf20Sopenharmony_ci * Emulate a load or store instruction by reading/writing the 1708c2ecf20Sopenharmony_ci * memory of the current process. FP/VMX/VSX registers are assumed 1718c2ecf20Sopenharmony_ci * to hold live values if the appropriate enable bit in regs->msr is 1728c2ecf20Sopenharmony_ci * set; otherwise this will use the saved values in the thread struct 1738c2ecf20Sopenharmony_ci * for user-mode accesses. 1748c2ecf20Sopenharmony_ci */ 1758c2ecf20Sopenharmony_ciextern int emulate_loadstore(struct pt_regs *regs, struct instruction_op *op); 1768c2ecf20Sopenharmony_ci 1778c2ecf20Sopenharmony_ciextern void emulate_vsx_load(struct instruction_op *op, union vsx_reg *reg, 1788c2ecf20Sopenharmony_ci const void *mem, bool cross_endian); 1798c2ecf20Sopenharmony_ciextern void emulate_vsx_store(struct instruction_op *op, 1808c2ecf20Sopenharmony_ci const union vsx_reg *reg, void *mem, 1818c2ecf20Sopenharmony_ci bool cross_endian); 1828c2ecf20Sopenharmony_ciextern int emulate_dcbz(unsigned long ea, struct pt_regs *regs); 183