blob: 394252b7b5d7f4ab8ee12d1ef19620b00d1f88cc [file] [log] [blame]
Andre Przywarafa914d82022-11-21 17:04:10 +00001/*
2 * Copyright (c) 2022, ARM Limited. All rights reserved.
Varun Wadekar0a46eb12023-04-13 21:06:18 +01003 * Copyright (c) 2023, NVIDIA Corporation. All rights reserved.
Andre Przywarafa914d82022-11-21 17:04:10 +00004 *
5 * SPDX-License-Identifier: BSD-3-Clause
6 */
7
8#ifndef TRAP_HANDLE_H
9#define TRAP_HANDLE_H
10
11#include <stdbool.h>
12#include <context.h>
13
14#define ISS_SYSREG_OPCODE_MASK 0x3ffc1eUL
15#define ISS_SYSREG_REG_MASK 0x0003e0UL
16#define ISS_SYSREG_REG_SHIFT 5U
17#define ISS_SYSREG_DIRECTION_MASK 0x000001UL
18
Andre Przywarabdc76f12022-11-21 17:07:25 +000019#define ISS_SYSREG_OPCODE_RNDR 0x30c808U
Varun Wadekar0a46eb12023-04-13 21:06:18 +010020#define ISS_SYSREG_OPCODE_IMPDEF 0x303c00U
Andre Przywarabdc76f12022-11-21 17:07:25 +000021#define ISS_SYSREG_OPCODE_RNDRRS 0x32c808U
22
Andre Przywarafa914d82022-11-21 17:04:10 +000023#define TRAP_RET_UNHANDLED -1
24#define TRAP_RET_REPEAT 0
25#define TRAP_RET_CONTINUE 1
26
27#ifndef __ASSEMBLER__
28static inline unsigned int get_sysreg_iss_rt(uint64_t esr)
29{
30 return (esr & ISS_SYSREG_REG_MASK) >> ISS_SYSREG_REG_SHIFT;
31}
32
33static inline bool is_sysreg_iss_write(uint64_t esr)
34{
35 return !(esr & ISS_SYSREG_DIRECTION_MASK);
36}
37
38/**
39 * handle_sysreg_trap() - Handle AArch64 system register traps from lower ELs
40 * @esr_el3: The content of ESR_EL3, containing the trap syndrome information
41 * @ctx: Pointer to the lower EL context, containing saved registers
42 *
43 * Called by the exception handler when a synchronous trap identifies as a
44 * system register trap (EC=0x18). ESR contains the encoding of the op[x] and
45 * CRm/CRn fields, to identify the system register, and the target/source
46 * GPR plus the direction (MRS/MSR). The lower EL's context can be altered
47 * by the function, to inject back the result of the emulation.
48 *
49 * Return: indication how to proceed with the trap:
50 * TRAP_RET_UNHANDLED(-1): trap is unhandled, trigger panic
51 * TRAP_RET_REPEAT(0): trap was handled, return to the trapping instruction
52 * (repeating it)
53 * TRAP_RET_CONTINUE(1): trap was handled, return to the next instruction
54 * (continuing after it)
55 */
56int handle_sysreg_trap(uint64_t esr_el3, cpu_context_t *ctx);
57
Manish Pandey067087f2023-12-08 20:13:29 +000058/* Handler for injecting UNDEF exception to lower EL */
59void inject_undef64(cpu_context_t *ctx);
60
Arvind Ram Prakashdf8200d2024-02-20 11:35:27 -060061u_register_t create_spsr(u_register_t old_spsr, unsigned int target_el);
62
Andre Przywarabdc76f12022-11-21 17:07:25 +000063/* Prototypes for system register emulation handlers provided by platforms. */
Varun Wadekar0a46eb12023-04-13 21:06:18 +010064int plat_handle_impdef_trap(uint64_t esr_el3, cpu_context_t *ctx);
Andre Przywarabdc76f12022-11-21 17:07:25 +000065int plat_handle_rng_trap(uint64_t esr_el3, cpu_context_t *ctx);
66
Andre Przywarafa914d82022-11-21 17:04:10 +000067#endif /* __ASSEMBLER__ */
68
69#endif