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
15*f396aec8SArvind Ram Prakash #define ISS_IDREG_OPCODE_MASK 0x31bc00UL
16ccd81f1eSAndre Przywara #define ISS_SYSREG_REG_MASK 0x0003e0UL
17ccd81f1eSAndre Przywara #define ISS_SYSREG_REG_SHIFT 5U
18ccd81f1eSAndre Przywara #define ISS_SYSREG_DIRECTION_MASK 0x000001UL
19ccd81f1eSAndre Przywara
201ae75529SAndre Przywara #define ISS_SYSREG_OPCODE_RNDR 0x30c808U
210ed3be6fSVarun Wadekar #define ISS_SYSREG_OPCODE_IMPDEF 0x303c00U
221ae75529SAndre Przywara #define ISS_SYSREG_OPCODE_RNDRRS 0x32c808U
23*f396aec8SArvind Ram Prakash #define ISS_SYSREG_OPCODE_IDREG 0x300000U
241ae75529SAndre Przywara
25ccd81f1eSAndre Przywara #define TRAP_RET_UNHANDLED -1
26ccd81f1eSAndre Przywara #define TRAP_RET_REPEAT 0
27ccd81f1eSAndre Przywara #define TRAP_RET_CONTINUE 1
28ccd81f1eSAndre Przywara
29ccd81f1eSAndre Przywara #ifndef __ASSEMBLER__
get_sysreg_iss_rt(uint64_t esr)30ccd81f1eSAndre Przywara static inline unsigned int get_sysreg_iss_rt(uint64_t esr)
31ccd81f1eSAndre Przywara {
32ccd81f1eSAndre Przywara return (esr & ISS_SYSREG_REG_MASK) >> ISS_SYSREG_REG_SHIFT;
33ccd81f1eSAndre Przywara }
34ccd81f1eSAndre Przywara
is_sysreg_iss_write(uint64_t esr)35ccd81f1eSAndre Przywara static inline bool is_sysreg_iss_write(uint64_t esr)
36ccd81f1eSAndre Przywara {
37ccd81f1eSAndre Przywara return !(esr & ISS_SYSREG_DIRECTION_MASK);
38ccd81f1eSAndre Przywara }
39ccd81f1eSAndre Przywara
40ccd81f1eSAndre Przywara /**
41ccd81f1eSAndre Przywara * handle_sysreg_trap() - Handle AArch64 system register traps from lower ELs
42ccd81f1eSAndre Przywara * @esr_el3: The content of ESR_EL3, containing the trap syndrome information
43ccd81f1eSAndre Przywara * @ctx: Pointer to the lower EL context, containing saved registers
44ccd81f1eSAndre Przywara *
45ccd81f1eSAndre Przywara * Called by the exception handler when a synchronous trap identifies as a
46ccd81f1eSAndre Przywara * system register trap (EC=0x18). ESR contains the encoding of the op[x] and
47ccd81f1eSAndre Przywara * CRm/CRn fields, to identify the system register, and the target/source
48ccd81f1eSAndre Przywara * GPR plus the direction (MRS/MSR). The lower EL's context can be altered
49ccd81f1eSAndre Przywara * by the function, to inject back the result of the emulation.
50ccd81f1eSAndre Przywara *
51ccd81f1eSAndre Przywara * Return: indication how to proceed with the trap:
52ccd81f1eSAndre Przywara * TRAP_RET_UNHANDLED(-1): trap is unhandled, trigger panic
53ccd81f1eSAndre Przywara * TRAP_RET_REPEAT(0): trap was handled, return to the trapping instruction
54ccd81f1eSAndre Przywara * (repeating it)
55ccd81f1eSAndre Przywara * TRAP_RET_CONTINUE(1): trap was handled, return to the next instruction
56ccd81f1eSAndre Przywara * (continuing after it)
57ccd81f1eSAndre Przywara */
5888655be9SArvind Ram Prakash int handle_sysreg_trap(uint64_t esr_el3, cpu_context_t *ctx,
5988655be9SArvind Ram Prakash u_register_t flags __unused);
60ccd81f1eSAndre Przywara
613c789bfcSManish Pandey /* Handler for injecting UNDEF exception to lower EL */
623c789bfcSManish Pandey void inject_undef64(cpu_context_t *ctx);
633c789bfcSManish Pandey
6403fafc0bSArvind Ram Prakash u_register_t create_spsr(u_register_t old_spsr, unsigned int target_el);
6503fafc0bSArvind Ram Prakash
661ae75529SAndre Przywara /* Prototypes for system register emulation handlers provided by platforms. */
670ed3be6fSVarun Wadekar int plat_handle_impdef_trap(uint64_t esr_el3, cpu_context_t *ctx);
681ae75529SAndre Przywara int plat_handle_rng_trap(uint64_t esr_el3, cpu_context_t *ctx);
691ae75529SAndre Przywara
70ccd81f1eSAndre Przywara #endif /* __ASSEMBLER__ */
71ccd81f1eSAndre Przywara
72ccd81f1eSAndre Przywara #endif
73