1*ccd81f1eSAndre Przywara /* 2*ccd81f1eSAndre Przywara * Copyright (c) 2022, ARM Limited. All rights reserved. 3*ccd81f1eSAndre Przywara * 4*ccd81f1eSAndre Przywara * SPDX-License-Identifier: BSD-3-Clause 5*ccd81f1eSAndre Przywara */ 6*ccd81f1eSAndre Przywara 7*ccd81f1eSAndre Przywara #ifndef TRAP_HANDLE_H 8*ccd81f1eSAndre Przywara #define TRAP_HANDLE_H 9*ccd81f1eSAndre Przywara 10*ccd81f1eSAndre Przywara #include <stdbool.h> 11*ccd81f1eSAndre Przywara #include <context.h> 12*ccd81f1eSAndre Przywara 13*ccd81f1eSAndre Przywara #define ISS_SYSREG_OPCODE_MASK 0x3ffc1eUL 14*ccd81f1eSAndre Przywara #define ISS_SYSREG_REG_MASK 0x0003e0UL 15*ccd81f1eSAndre Przywara #define ISS_SYSREG_REG_SHIFT 5U 16*ccd81f1eSAndre Przywara #define ISS_SYSREG_DIRECTION_MASK 0x000001UL 17*ccd81f1eSAndre Przywara 18*ccd81f1eSAndre Przywara #define TRAP_RET_UNHANDLED -1 19*ccd81f1eSAndre Przywara #define TRAP_RET_REPEAT 0 20*ccd81f1eSAndre Przywara #define TRAP_RET_CONTINUE 1 21*ccd81f1eSAndre Przywara 22*ccd81f1eSAndre Przywara #ifndef __ASSEMBLER__ 23*ccd81f1eSAndre Przywara static inline unsigned int get_sysreg_iss_rt(uint64_t esr) 24*ccd81f1eSAndre Przywara { 25*ccd81f1eSAndre Przywara return (esr & ISS_SYSREG_REG_MASK) >> ISS_SYSREG_REG_SHIFT; 26*ccd81f1eSAndre Przywara } 27*ccd81f1eSAndre Przywara 28*ccd81f1eSAndre Przywara static inline bool is_sysreg_iss_write(uint64_t esr) 29*ccd81f1eSAndre Przywara { 30*ccd81f1eSAndre Przywara return !(esr & ISS_SYSREG_DIRECTION_MASK); 31*ccd81f1eSAndre Przywara } 32*ccd81f1eSAndre Przywara 33*ccd81f1eSAndre Przywara /** 34*ccd81f1eSAndre Przywara * handle_sysreg_trap() - Handle AArch64 system register traps from lower ELs 35*ccd81f1eSAndre Przywara * @esr_el3: The content of ESR_EL3, containing the trap syndrome information 36*ccd81f1eSAndre Przywara * @ctx: Pointer to the lower EL context, containing saved registers 37*ccd81f1eSAndre Przywara * 38*ccd81f1eSAndre Przywara * Called by the exception handler when a synchronous trap identifies as a 39*ccd81f1eSAndre Przywara * system register trap (EC=0x18). ESR contains the encoding of the op[x] and 40*ccd81f1eSAndre Przywara * CRm/CRn fields, to identify the system register, and the target/source 41*ccd81f1eSAndre Przywara * GPR plus the direction (MRS/MSR). The lower EL's context can be altered 42*ccd81f1eSAndre Przywara * by the function, to inject back the result of the emulation. 43*ccd81f1eSAndre Przywara * 44*ccd81f1eSAndre Przywara * Return: indication how to proceed with the trap: 45*ccd81f1eSAndre Przywara * TRAP_RET_UNHANDLED(-1): trap is unhandled, trigger panic 46*ccd81f1eSAndre Przywara * TRAP_RET_REPEAT(0): trap was handled, return to the trapping instruction 47*ccd81f1eSAndre Przywara * (repeating it) 48*ccd81f1eSAndre Przywara * TRAP_RET_CONTINUE(1): trap was handled, return to the next instruction 49*ccd81f1eSAndre Przywara * (continuing after it) 50*ccd81f1eSAndre Przywara */ 51*ccd81f1eSAndre Przywara int handle_sysreg_trap(uint64_t esr_el3, cpu_context_t *ctx); 52*ccd81f1eSAndre Przywara 53*ccd81f1eSAndre Przywara #endif /* __ASSEMBLER__ */ 54*ccd81f1eSAndre Przywara 55*ccd81f1eSAndre Przywara #endif 56