KL-OS: User Mode
Day 12 was about user mode.
The application that we had made had to be run on the user mode. And since, the execution image is a raw binary, it needs to be prepared with a fixed binary.
#define USER_BASE 0x1000000
After defining the symbols to use in the embedded raw binary, the create process function is updated to start the application from the user_entry.
The create_process is modified to take the pointer to the execution image and image size as arguments. It copies the execution image page by page for the specified size and maps it to the process page table.
Finally, the create_process function is modified to create a user process.
After checking if the execution image is mapped as expected, to run applications we must transition the CPU to the user mode. Or, in RISC-V, the U-Mode.
This switch from S-Mode to U-mode is done with the sret instruction. It does two writes to CSRs while switching:
PCis set for when transitioning in U-Mode in thesepcregister wheresretjumps to.- Then, setting
SPIEbit in thesstatusregister enables hardware interrupts and the handler set in thestvecregister will be called while entering theU-Mode.
Discussion in the ATmosphere