@@ -48,8 +48,9 @@ typedef uint32_t jmp_buf[17];
4848The ` hal_context_save ` function captures complete task state including both execution context and processor state.
4949The function saves all callee-saved registers as required by the RISC-V ABI,
5050plus essential pointers (gp, tp, sp, ra).
51- For processor state, it performs sophisticated interrupt state reconstruction and ensures that tasks resume with correct interrupt state,
52- maintaining system responsiveness and preventing interrupt state corruption.
51+ For processor state, it saves ` mstatus ` as-is, preserving the exact processor state at the time of the context switch.
52+ The RISC-V hardware automatically manages the ` mstatus.MIE ` and ` mstatus.MPIE ` stack during trap entry and ` MRET ` ,
53+ ensuring correct interrupt state restoration per the RISC-V Privileged Specification.
5354
5455### 2. Select Next Task
5556The scheduler, invoked via ` dispatcher() ` during machine timer interrupts,
@@ -69,19 +70,30 @@ This ordering ensures that interrupt state and privilege mode are correctly esta
6970
7071## Processor State Management
7172
72- ### Interrupt State Reconstruction
73- The HAL context switching routines include sophisticated interrupt state management that handles the complexities of RISC-V interrupt processing:
73+ ### Hardware-Managed Interrupt State
74+ The HAL context switching routines follow the RISC-V Privileged Specification for interrupt state management,
75+ allowing hardware to automatically manage the interrupt enable stack:
7476
75- During Timer Interrupts:
76- - ` mstatus.MIE ` is automatically cleared by hardware when entering the trap
77- - ` mstatus.MPIE ` preserves the previous interrupt enable state
78- - HAL functions reconstruct the original interrupt state from ` MPIE `
79- - This ensures consistent interrupt behavior across context switches
77+ During Trap Entry (Hardware Automatic per RISC-V Spec §3.1.6.1):
78+ - ` mstatus.MPIE ← mstatus.MIE ` (preserve interrupt enable state)
79+ - ` mstatus.MIE ← 0 ` (disable interrupts during trap handling)
80+ - ` mstatus.MPP ← current_privilege ` (preserve privilege mode)
81+
82+ During MRET (Hardware Automatic):
83+ - ` mstatus.MIE ← mstatus.MPIE ` (restore interrupt enable state)
84+ - ` mstatus.MPIE ← 1 ` (reset to default enabled)
85+ - ` privilege ← mstatus.MPP ` (return to saved privilege)
86+
87+ HAL Context Switch Behavior:
88+ - ` hal_context_save ` saves ` mstatus ` exactly as observed (no manual bit manipulation)
89+ - ` hal_context_restore ` restores ` mstatus ` exactly as saved
90+ - Hardware manages the ` MIE ` /` MPIE ` stack automatically during nested traps
91+ - This ensures spec-compliant behavior and correct interrupt state across all scenarios
8092
8193State Preservation:
82- - Each task maintains its own interrupt enable state
94+ - Each task maintains its own complete ` mstatus ` value
8395- Context switches preserve privilege mode (Machine mode for kernel tasks)
84- - Interrupt state is reconstructed accurately for reliable task resumption
96+ - Nested interrupts are handled correctly by hardware's automatic state stacking
8597
8698### Task Initialization
8799New tasks are initialized with proper processor state:
0 commit comments