Lec 08b - Exceptions
Introduction
An exception is like an unscheduled function call caused by an event in hardware or software.
Dealing with Exceptions
An exception is treated as a form of Control Hazard. When an exception occurs, the hardware pipeline must immediately change its flow. The sequence is as follows:
Stop Execution: The pipeline stops executing the offending instruction in midstream.
Complete Predecessors: Allow all instructions prior to the exception to complete their execution.
Flush Successors: Flush (discard) all instructions following the offending instruction that are currently in the pipeline.
Record State:
Set a status register to show the cause (e.g.,
mcause).Save the address of the offending/interrupted instruction (e.g.,
mepc).
Transfer Control: Jump to the prearranged exception handler address (e.g., the address found in
mtvec).
Two Types of Exceptions
Based on the source of the cause (either hardware or software), the exceptions can be further divided into two categories:
Interrupts
A hardware exception triggered by an input/output (I/O) device such as a keyboard is often called an interrupt.
Asynchronous: They are asynchronous to program execution, meaning they are caused by external events unrelated to the specific instruction currently running.
Examples & Pipeline Stage: Examples include an I/O service request or a hardware malfunction. Because these are external, they can occur during any stage of the pipeline.
Pipeline Handling: Because the current instruction isn't the cause of the issue, the processor can allow the instructions currently active in the pipeline to complete before passing control to the OS interrupt handler. The program is simply suspended and later resumed.
Register Status: In the
mcauseregister, bit 31 is set to 0 (mcause[31] = 0), resulting in a positive value. (See Table 6.6 in below)
Traps
Alternatively, the program may encounter an error condition caused by the software, such as an undefined instruction. Software exceptions are sometimes called traps or exceptions (in CG3207).
Synchronous: These are synchronous to program execution, caused by internal events directly related to the instruction being executed.
Examples & Pipeline Stage: The stage where these occur depends on the specific error:
Fetch (F) or Memory (M) stages: TLB or page faults.
Decode (D) stage: Undefined instruction.
Execution (E) stage: Arithmetic error.
Pipeline Handling: The condition must be remedied by the handler for that specific instruction. Therefore, the processor must stop the offending instruction midstream in the pipeline and immediately pass control to the OS trap handler.
Outcome: After handling, the offending instruction may be retried (e.g., after a page fault is fixed), simulated (if the hardware doesn't support the instruction), or the program may be aborted.
Register Status: In the
mcauseregister, bit 31 is set to 1 (mcause[31] = 1), resulting in a negative value.
A crucial aspect of pipeline design is recognizing that multiple exceptions can occur simultaneously in a single clock cycle. Because different instructions occupy different stages of the pipeline at the same time, they might all encounter error conditions simultaneously.
The image below illustrates a scenario where instructions 1, 2, 3, and 4 all trigger exceptions in the same clock cycle (the highlighted vertical region).

The processor cannot handle four exceptions at once. The hardware must prioritize based on program order. The rule is: The earliest instruction in the program order is interrupted first.
In the example shown, Inst 1 is the earliest instruction in the sequence. Therefore, its Data Memory page fault will be handled first, and the subsequent instructions (Inst 2, 3, and 4) will be flushed from the pipeline.
Exception Handler
In this section, we will discuss exceptions when running in M-mode. Exceptions that occur at other levels are similar but use registers associated with that mode.
Exception Handling Software
Exception handlers use four special-purpose registers, called control and status registers (CSRs), to handle an exception:
mtvec
mtvecThe machine trap-vector base-address register (mtvec) holds the address of the exception handler code. The last two bits ([1:0]) indicate the exception handling modes: direct or vectored.
In direct mode, all exceptions branch to same address, that is, the base address encoded in bits
[31:2]ofmtvec.In vectored mode, exceptions branch to an offset from the base address, depending on the cause of the exception.
mepc
mepcThe machine exception PC register (mepc) stores the address of the instruction that was executing when the exception occurred. It serves as the return address for the handler; executing the mret instruction copies the value in mepc back to the program counter (PC), allowing execution to resume exactly where it left off (analogous to how ra stores the return address for a jal instruction).
mscratch
mscratchThe machine scratch register (mscratch) holds a pointer to a dedicated memory area (a "scratchpad") used to save the state of the program registers (x1–x31). Since the exception handler needs to use these registers to do its work, it first stores their current values into the memory pointed to by mscratch to preserve the interrupted program's context.
When an exception occurs, the processor records the cause of an exception in mcause (see the following table), stores the PC of the excepting instruction in mepc and jumps to the exception handler at the address of the preconfigured in mtvec.

After jumping to the address in mtvec, the exception handler reads the mcause register to examine what caused the exception and responds appropriately (e.g., by reading the keyboard on a hardware interrupt). It then either aborts the program or returns to the program by executing the mret (machine exception return) instruction, that jumps to the address in mepc. Exception handlers must use program registers (x1−x31) to handle exceptions, so they use the memory pointed to by mscratch to store and restore these registers.
Exception Handling Hardware
While the software handler manages the logic, the hardware must perform specific physical actions to transfer control safely.
PC Loading Mechanism
The hardware needs a mechanism to forcibly load the Program Counter (PC) with the exception handler address.
Implementation: This is typically achieved by expanding the PC Input Multiplexer (Mux).
Control: A control unit signal (e.g.,
PCSrc) selects the "Exception Handler Address" input instead of the standardPC+4or Branch Target Address.

Hardware Trade-offs
We have seen that the last two bits of mtvec will indicate the exception handling mode. The choice between Direct and Vectored modes (set in mtvec) involves a hardware/software trade-off:
Direct Mode:
Hardware: Simpler (Fixed target address).
Software: More complicated/slower. The handler must read
mcauseto determine the source and branch accordingly.RISC-V Note: Synchronous exceptions in RISC-V always use Direct mode (unlike ARM).
Vectored Mode:
Hardware: More complicated. Requires logic to calculate the target address (
Base + Offset) based on the interrupt ID.Software: Simpler and faster latency. The hardware jumps directly to the specific code for that interrupt.
Register Preservation
RISC-V hardware is designed to be simple ("lean"), placing more burden on the software compared to architectures like ARM:
No Automatic Saving: RISC-V hardware does not automatically save caller-saved registers to the stack (ARM often does). This requires explicit compiler support or inline assembly in the handler.
No Banked Registers: RISC-V does not use "banked" registers (separate physical copies of registers like SP for exception modes). The handler shares the same register file as the user program, necessitating the use of
mscratchto safely save context without corruption.
External Interrupt Controllers
Handling interrupts becomes complicated when a processor has many different sources (e.g., timer, UART, GPIO).
To manage this, systems include a separate hardware unit called an Interrupt Controller tightly coupled to the processor.
It handles enabling/disabling specific interrupts and prioritizing them.
Example: The PLIC (Platform-Level Interrupt Controller) in RISC-V or NVIC in ARM.
Execution Modes and Privilege Levels
A RISC-V processor can operate in one of several execution modes with different privilege levels. Privilege levels dictate what instructions can be executed and what memory can be accessed. There are three main RISC-V execution modes and one experimental execution mode. The privilege for each mode is M-mode > H-mode > S-mode > U-mode.
M-mode and S-mode are called privileged mode, while U-mode is not a privileged mode! It's just a normal mode!
Machine Mode (M-mode)
Machine mode (M-mode) is the highest privilege level and the only mandatory mode (always available). It has full access to the system, including all physical memory and all CSRs.
Use Case: Intended for low-level firmware (e.g., bootloaders, security monitors) rather than the main OS. It controls hardware initialization and system-wide configuration.
Registers: Accesses all CSRs including
mstatus,mcause,mepc, andmtvec.Delegation: M-mode can delegate specific exceptions and interrupts to Supervisor mode using the
medelegandmidelegregisters, allowing the OS to handle them directly without M-mode intervention.Return: The
mretinstruction is used to return to a previous privilege level (M, S, or U). The target mode is determined by the MPP (Machine Previous Privilege) field in themstatusregister.
Superviser Mode (S-mode)
The OS runs in Supervisor mode (S-mode), providing a balance of access and restriction.
Use Case: Manages system resources (memory, I/O) for user processes while being restricted from direct machine-level hardware manipulation.
Virtual Memory: Uses the
satpregister to hold the page table address, enabling virtual memory management.Registers: Uses S-mode specific CSRs (
sstatus,scause,sepc) which are restricted equivalents of M-mode registers.Transitions: An
ecallexecuted here causes an M-mode exception (transitioning up to M-mode). Thesretinstruction is used to return to the privilege level (S or U) where the exception occurred.Priority: S-mode interrupts/exceptions generally have lower priority than M-mode.
User Mode (U-mode)
User applications that run on top of an OS typically run in user mode (U-mode), the lowest privilege level. User programs do not have access to privileged registers or memory locations reserved for the OS.
Use Case: Executes unprivileged applications in a sandboxed environment. This isolates programs from critical system components to enhance security and stability.
Restrictions: U-mode has very limited access to CSRs and cannot directly handle hardware interrupts. Attempts to write to non-accessible registers trigger an illegal instruction exception (which escalates to M-mode or S-mode).
System Calls: U-mode uses the
ecallinstruction to request services from the OS (running in S-mode).Memory: Uses page tables managed by S-mode to restrict access to specific memory regions (memory protection).
Hypervisor mode (H-mode)
An intermediate privilege level situated between M-mode and S-mode, intended for virtualization.
Function: It allows a Type-1 Hypervisor to run multiple "Guest Operating Systems." The Guest OS thinks it is running in S-mode, but H-mode translates its requests and isolates different guests from each other.
The different modes keep the key state from being corrupted. So, how do these modes work together to create a secure system?
Privilege Transitions
The processor changes modes strictly through defined events to ensure security:
Escalating Privilege (Moving Up): A program cannot simply "choose" to upgrade its mode. It must trigger an Exception or an Interrupt.
Example: A User program needs to write to a file. It executes
ecall(Environment Call). This triggers an exception, effectively "jumping" the processor into S-mode (OS) to handle the request.
Dropping Privilege (Moving Down): The higher-privileged software returns control using specific return instructions.
Example: The OS finishes writing the file. It executes
sret(Supervisor Return), which restores the PC and drops the privilege level back to U-mode so the app can continue.
Isolation & Traps
The hierarchy prevents crashes in one program from killing the whole system.
If a User program tries to access memory it doesn't own (e.g., the OS kernel memory), the hardware triggers a Page Fault.
This fault is a Trap that pauses the user program and hands control to the OS (S-mode).
The OS sees the illegal attempt and kills only that specific user process, leaving the rest of the system running smoothly.
A switch from U-mode to a privileged mode (M-mode and S-mode) can only be done through an exception or somtimes called interrputs. (In this course, we have distinguished between them at the very start)
Exception-Related Instructions
Exception handlers use special instructions to deal with exceptions. These instructions are called privileged instructions because they access CSRs. They are part of the base RV32I instruction set. The mepc and mcause registers are not part of the RISC-V program registers (x1−x31), so the exception handler must move these special-purpose (CSR) registers into the program registers to read and operate on them. RISC-V uses three instructions to read, write, or both read and write CSRs:
csrr(read CSR),csrw(write CSR), andcsrrw(read/write CSR).
For example,
csrr t1, mcausereads the value inmcauseintot1;csrw mepc, t2writes the value int2intomepc; andcsrrw t1, mscratch, t0simultaneously reads the value inmscratchintot1and writes the value int0intomscratch.
csrrw is an actual RISC-V instruction, but csrr and csrw are pseudoinstructions. csrr is implemented as csrrs rd, csr, x0 and csrw as csrrw x0, csr, rs.
Exception Handling Summary
In summary, when a processor detects an exception, it:
Jumps to the exception handler address held in
mtvec.The exception handler saves registers on a small stack pointed to by
mscratchand then usescsrr(read CSR) to look at the cause of the exception (encoded inmcause) and respond accordingly.When the handler is finished, it optionally increments
mepcby 4, restores registers from memory and either aborts the program or returns to the user code using themretinstruction, which jumps to the address held inmepc.
Last updated