1ccd81f1eSAndre Przywara /* 2ccd81f1eSAndre Przywara * Copyright (c) 2022, ARM Limited. All rights reserved. 30ed3be6fSVarun Wadekar * Copyright (c) 2023, NVIDIA Corporation. All rights reserved. 4ccd81f1eSAndre Przywara * 5ccd81f1eSAndre Przywara * SPDX-License-Identifier: BSD-3-Clause 6ccd81f1eSAndre Przywara */ 7ccd81f1eSAndre Przywara 8ccd81f1eSAndre Przywara #ifndef TRAP_HANDLE_H 9ccd81f1eSAndre Przywara #define TRAP_HANDLE_H 10ccd81f1eSAndre Przywara 11ccd81f1eSAndre Przywara #include <stdbool.h> 12ccd81f1eSAndre Przywara #include <context.h> 13ccd81f1eSAndre Przywara 14ccd81f1eSAndre Przywara #define ISS_SYSREG_OPCODE_MASK 0x3ffc1eUL 15ccd81f1eSAndre Przywara #define ISS_SYSREG_REG_MASK 0x0003e0UL 16ccd81f1eSAndre Przywara #define ISS_SYSREG_REG_SHIFT 5U 17ccd81f1eSAndre Przywara #define ISS_SYSREG_DIRECTION_MASK 0x000001UL 18ccd81f1eSAndre Przywara 191ae75529SAndre Przywara #define ISS_SYSREG_OPCODE_RNDR 0x30c808U 200ed3be6fSVarun Wadekar #define ISS_SYSREG_OPCODE_IMPDEF 0x303c00U 211ae75529SAndre Przywara #define ISS_SYSREG_OPCODE_RNDRRS 0x32c808U 221ae75529SAndre Przywara 23ccd81f1eSAndre Przywara #define TRAP_RET_UNHANDLED -1 24ccd81f1eSAndre Przywara #define TRAP_RET_REPEAT 0 25ccd81f1eSAndre Przywara #define TRAP_RET_CONTINUE 1 26ccd81f1eSAndre Przywara 27ccd81f1eSAndre Przywara #ifndef __ASSEMBLER__ 28ccd81f1eSAndre Przywara static inline unsigned int get_sysreg_iss_rt(uint64_t esr) 29ccd81f1eSAndre Przywara { 30ccd81f1eSAndre Przywara return (esr & ISS_SYSREG_REG_MASK) >> ISS_SYSREG_REG_SHIFT; 31ccd81f1eSAndre Przywara } 32ccd81f1eSAndre Przywara 33ccd81f1eSAndre Przywara static inline bool is_sysreg_iss_write(uint64_t esr) 34ccd81f1eSAndre Przywara { 35ccd81f1eSAndre Przywara return !(esr & ISS_SYSREG_DIRECTION_MASK); 36ccd81f1eSAndre Przywara } 37ccd81f1eSAndre Przywara 38ccd81f1eSAndre Przywara /** 39ccd81f1eSAndre Przywara * handle_sysreg_trap() - Handle AArch64 system register traps from lower ELs 40ccd81f1eSAndre Przywara * @esr_el3: The content of ESR_EL3, containing the trap syndrome information 41ccd81f1eSAndre Przywara * @ctx: Pointer to the lower EL context, containing saved registers 42ccd81f1eSAndre Przywara * 43ccd81f1eSAndre Przywara * Called by the exception handler when a synchronous trap identifies as a 44ccd81f1eSAndre Przywara * system register trap (EC=0x18). ESR contains the encoding of the op[x] and 45ccd81f1eSAndre Przywara * CRm/CRn fields, to identify the system register, and the target/source 46ccd81f1eSAndre Przywara * GPR plus the direction (MRS/MSR). The lower EL's context can be altered 47ccd81f1eSAndre Przywara * by the function, to inject back the result of the emulation. 48ccd81f1eSAndre Przywara * 49ccd81f1eSAndre Przywara * Return: indication how to proceed with the trap: 50ccd81f1eSAndre Przywara * TRAP_RET_UNHANDLED(-1): trap is unhandled, trigger panic 51ccd81f1eSAndre Przywara * TRAP_RET_REPEAT(0): trap was handled, return to the trapping instruction 52ccd81f1eSAndre Przywara * (repeating it) 53ccd81f1eSAndre Przywara * TRAP_RET_CONTINUE(1): trap was handled, return to the next instruction 54ccd81f1eSAndre Przywara * (continuing after it) 55ccd81f1eSAndre Przywara */ 56ccd81f1eSAndre Przywara int handle_sysreg_trap(uint64_t esr_el3, cpu_context_t *ctx); 57ccd81f1eSAndre Przywara 583c789bfcSManish Pandey /* Handler for injecting UNDEF exception to lower EL */ 593c789bfcSManish Pandey void inject_undef64(cpu_context_t *ctx); 603c789bfcSManish Pandey 61*03fafc0bSArvind Ram Prakash u_register_t create_spsr(u_register_t old_spsr, unsigned int target_el); 62*03fafc0bSArvind Ram Prakash 631ae75529SAndre Przywara /* Prototypes for system register emulation handlers provided by platforms. */ 640ed3be6fSVarun Wadekar int plat_handle_impdef_trap(uint64_t esr_el3, cpu_context_t *ctx); 651ae75529SAndre Przywara int plat_handle_rng_trap(uint64_t esr_el3, cpu_context_t *ctx); 661ae75529SAndre Przywara 67ccd81f1eSAndre Przywara #endif /* __ASSEMBLER__ */ 68ccd81f1eSAndre Przywara 69ccd81f1eSAndre Przywara #endif 70