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
  1. AIt branches to the numbered address as an ordinary function call, staying in the same mode
  2. BInterrupts are disabled and the CPU halts until the next interrupt
  3. 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.
Practice this topic Back to topic

More OS & Hardware questions

  1. Which is the usual distinction between interrupts and exceptions?
  2. Which of the following is asynchronous, occurring at unpredictable times unrelated to the instruction being executed?
  3. What is the usual sequence when a CPU accepts an interrupt?
  4. What is the role of an interrupt vector table?
  5. What should you keep in mind when temporarily disabling (masking) interrupts during a critical section?