OS & Hardware
What happens when the ecall instruction at the end of the RISC-V code above executes?
li a7, 64 # system call number
li a0, 1 # argument 1
la a1, msg # argument 2
li a2, 13 # argument 3
ecall
- AIt branches to the numbered address as an ordinary function call, staying in the same mode
- BInterrupts are disabled and the CPU halts until the next interrupt
- CAn exception enters privileged mode and the kernel handles the call
Show answer and explanation
Answer: C. An exception enters privileged mode and the kernel handles the call
ecall deliberately raises an exception. The CPU saves the current instruction address, switches to a higher-privilege mode and jumps to the exception entry point set up by the OS. The kernel identifies the system call by the number in a7, uses a0–a2 as arguments, returns the result and goes back to user mode. User code cannot branch directly into kernel functions, and halting to wait for an interrupt is done by a different instruction such as wfi.
- A. It does not branch in user mode; an exception switches modes.
- B. Halting to wait for interrupts is done by other instructions such as wfi.
- C. Correct. An exception enters privileged mode, and the kernel handles the call.