Unified exception frame for RISC-V trap handlers and context switching.
More...
Unified exception frame for RISC-V trap handlers and context switching.
This header defines the standard full-context frame saved on trap entry and restored on trap exit. It is the RISC-V equivalent of the ARM ExceptionFrame in cortex.h.
The layout covers all general-purpose registers that must be preserved across a trap (x1/ra, x5-x31), plus the mepc and mstatus CSRs. Registers x0 (zero), x2 (sp), x3 (gp), and x4 (tp) are excluded: x0 is hardwired to zero; sp is saved separately in the thread control block; gp and tp are program-global constants in M-mode.
The frame is 32 words (128 bytes), keeping the stack 16-byte aligned per the RISC-V psABI (riscv-abi documentation, section 2.1).
References:
- RISC-V Privileged Specification (mepc, mstatus, mcause)
- RISC-V psABI (register conventions, stack alignment)
◆ _RISCV_EFS
| #define _RISCV_EFS |
( |
|
x | ) |
#x |
◆ CMRX_RISCV_INITIAL_MSTATUS
| #define CMRX_RISCV_INITIAL_MSTATUS ((1u << 7) | (3u << 11)) |
Initial mstatus value for newly created threads.
MPIE = 1 (bit 7): after mret, MIE is set so interrupts are enabled. MPP = 3 (bits 12:11): stay in M-mode after mret.
These are standard RISC-V privileged specification bit positions.
◆ EF_A0
◆ EF_A1
◆ EF_A2
◆ EF_A3
◆ EF_A4
◆ EF_A5
◆ EF_A6
◆ EF_A7
◆ EF_MEPC
◆ EF_MSTATUS
◆ EF_RA
◆ EF_S0
◆ EF_S1
◆ EF_S10
◆ EF_S11
◆ EF_S2
◆ EF_S3
◆ EF_S4
◆ EF_S5
◆ EF_S6
◆ EF_S7
◆ EF_S8
◆ EF_S9
◆ EF_T0
◆ EF_T1
◆ EF_T2
◆ EF_T3
◆ EF_T4
◆ EF_T5
◆ EF_T6
◆ EXCEPTION_FRAME_SIZE
| #define EXCEPTION_FRAME_SIZE 128 |
Total frame size in bytes.
◆ EXCEPTION_FRAME_WORDS
| #define EXCEPTION_FRAME_WORDS (sizeof(ExceptionFrame) / sizeof(uint32_t)) |
How many uint32_t slots the exception frame occupies.
◆ LOAD_CONTEXT
Load application context.
Restores all GP registers and CSRs (mepc, mstatus) from the ExceptionFrame on the current stack and pops the frame. Does NOT execute mret — the caller handles that.
- Note
- This is defined as a macro so it can live inside naked functions.
◆ RISCV_EFS
◆ SAVE_CONTEXT
Save application context.
Pushes a full ExceptionFrame onto the current stack: all GP registers (ra, t0-t6, a0-a7, s0-s11) plus mepc and mstatus. Uses t0/t1 as scratch for CSR reads (they are saved beforehand).
- Note
- This is defined as a macro so it can live inside naked functions.
◆ riscv_exception_get_arg()
| static uint32_t riscv_exception_get_arg |
( |
const ExceptionFrame * |
frame, |
|
|
unsigned |
argno |
|
) |
| |
|
inlinestatic |
Retrieve a syscall argument from the exception frame.
- Parameters
-
| frame | exception frame pointer |
| argno | argument index (0-3 maps to a0-a3) |
◆ riscv_exception_get_syscall_id()
| static uint8_t riscv_exception_get_syscall_id |
( |
const ExceptionFrame * |
frame | ) |
|
|
inlinestatic |
Retrieve the syscall ID from the exception frame (a7 per RISC-V ecall ABI).
◆ riscv_exception_set_retval()
| static void riscv_exception_set_retval |
( |
ExceptionFrame * |
frame, |
|
|
int32_t |
retval |
|
) |
| |
|
inlinestatic |
Write the syscall return value into the exception frame (a0).