Risc-V Xv6 Syscall Procedure
Risc-V Xv6 Syscall Procedure
-
User syscall function loads corresponding syscall number into register
a7
, then executeecall
. -
What
ecall
does in risc-v are basically 3 things:- Change user mode into super mode.
- Save program counter in
pc
intosepc
. - Load value in
stvec
intopc
. - Resume execution.
Through steps above, the next instruction which will be executed is what the address in
stvec
stvec
points to will be the start of procedure of saving all the 32 user registers(by using a extra registersscrath
). -
Save all the user registers.
-
Set kernel page table, and other relavent info
-
Jump to the function in kernel which handles the trap.
-
Some settings to handle trap from kernel.
-
The function above will call corresponding syscall accroding to the syscall number.
-
Execute syscall, and put return value back to
a0
. -
Restore the user register, and execute
sret
. -
sret
changes mode to user mode, turns on interuption, and puts$sepc
intopc
-
Resume execution.
Actually, xv6 saves all the user registers and a bunch of other infomation in trapframe, which is allocated per process. For other details, refer to xv6book chapter4