RISC-V (32 bits): PMP support with 4k stack alignment #1
Loading…
Reference in New Issue
No description provided.
Delete Branch "occheung/libfringe:4k-align"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
One-line Summary
This patch inserts an environment call (
ecall
) on context switching for 32 bits RISC-V target, to support 4k aligned stack and potentially physical memorgy protection (PMP) modification in runtime.(Note: PMP manipulation through
ecall
is not within the scope of this library).Relevant issue
artiq: Trivial memory protection unit (stack overflow protection)
Changes
All changes are made towards the
arch/riscv32.rs
file.Stack alignment
Stack of any thread requested by libfringe is now 4k aligned.
Context switching
An environment call will be issued during context switch
arch::swap()
, to prompt adjustment of PMP regions. If switching to a new context, the base address of the stack guard is required.Invoking
ecall
will trigger anEnvCall
related exception.Assumption
All allocated stack must have a stack limit at a non-zero address.
Switching into a new context
It is when
arch::swap()
is called withnew_stack
holding a value (i.e.Some(&Stack)
. The stack limitnew_stack.limit()
is passed to the enviroment call.Switching away from a new context
It is when
arch::swap()
is called withnew_stack
not holding a value (i.e.None
. The value 0 is passed to the enviroment call instead.ecall
ABIThe argument is passed through register
a7
.Caller saved register is expected to be saved by the exception handler that
ecall
will transfer to. See an example implementation in riscv-rt repository.Exception PC register should be incremented by ONE instruction length before returning from the exception handler.
For example,
Increment
mepc
by 4 before callingmret
.Expected privilege
If
ecall
is implemented to support PMP adjustment during runtime, threads created by this library usingriscv32
target expect non-machine privilege (e.g. User).