KL-OS: User Mode

2026-01-02POSSE

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:

  • PC is set for when transitioning in U-Mode in the sepc register where sret jumps to.
  • Then, setting SPIE bit in the sstatus register enables hardware interrupts and the handler set in the stvec register will be called while entering the U-Mode.

Webmentions

Have you written a response to this? Let me know the URL, Or, you can send your response via mail:iac@scientiac.space