Timedora

Interrupts

How interrupts transfer CPU control, support operating-system work, and provide the trap mechanism behind Linux system calls on RISC-V.

How Interrupts Give the OS Control

Interrupts transfer control

A running process normally executes instructions in sequence. An interrupt gives the operating system and hardware a way to interrupt that flow and transfer control to code that handles an event. The source describes a useful conceptual CPU loop: fetch an instruction, execute it, check for an interrupt, and repeat. In this model, the check happens between adjacent instructions. If an interrupt needs attention, the CPU transfers control to the appropriate handling code.

This loop is a teaching model, not a claim that it captures every detail of how a processor implements interrupts. It helps explain how the kernel can respond while a program is still running. For example, signals use this general mechanism: when a process receives a signal, its normal execution is interrupted and a previously registered signal handler runs.

Why interrupts matter

Interrupts support both unusual conditions and ordinary system activity. An illegal instruction or a reference to an unmapped virtual-memory address is exceptional; handling I/O or scheduling a context switch is routine. In both cases, the system can respond without waiting for the current program to finish.

They also avoid busy-waiting. Instead of repeatedly checking whether a network packet has arrived, a process can do other work; when the packet arrives, the operating system can interrupt it to handle the event. Similarly, interrupts let the system respond to real-world events such as mouse movement, rather than making the cursor wait for a running program to finish. For multitasking, kernels use interrupts for periodic context switches, allowing concurrent processes to share CPU time fairly.

The common idea is that an interrupt creates an opportunity for the system to act at the time an event needs attention, whether that event is an error, an I/O operation, or part of ordinary scheduling.

Traps and System Calls on RISC-V

A system call requests a trap

User-space code uses a system call to request kernel functionality. The request uses a trap: the instruction set provides a special instruction that requests one, and executing it transitions the hardware to kernel mode so the system call can be handled.

The system-call convention is distinct from the ordinary function calling convention. It specifies how to identify the requested kernel operation and where to place its arguments. For Linux on RISC-V, the special instruction is ecall; it has no operands. The convention uses a7 for the system-call number, a0 through a5 for arguments, and a0 for the return value. Linux's unistd.h header lists system-call numbers, while the corresponding manual pages describe the calls' arguments.

RISC-V syscall registers
RegisterSystem-call role
a7System-call number
a0–a5Arguments
a0Return value
Linux system-call register convention on RISC-V.

Worked example: write

The Linux write system call has number 64. In the example, it writes one character to standard output using file descriptor 1. The call needs three arguments: the file descriptor, a pointer to the character buffer, and the number of characters. The wrapper receives the buffer pointer as its own function argument in a0, so it first saves that pointer in t0. It can then reuse a0 for the system call's first argument.

.global printone
printone:
    mv t0, a0       # save the function argument: a character pointer
    # Make a system call: write(1, t0, 1)
    addi a7, x0, 64 # syscall number: write
    addi a0, x0, 1  # first argument: fd
    mv a1, t0       # second argument: buf
    addi a2, x0, 1  # third argument: count
    ecall
    ret

Step by step, a7 selects write; a0 is set to file descriptor 1; a1 receives the saved buffer pointer; and a2 is set to 1, the requested character count. ecall makes the request. The system-call result is returned in a0, and ret returns from the assembly wrapper.

A C file can call the assembly function by declaring it, for example, int printone(char* c);, and then calling printone("h");. The source's example also calls it with "i" and "\n". This demonstrates that a C program can use the assembly wrapper without importing headers or using the C standard library; the system call itself is made by the assembly code.

execve as another system call

The source introduces execve as a system call that transforms the current process so it runs a program. Its parameters are a path, an argument-vector pointer, and an environment-vector pointer. The latter two may be null pointers, as indicated by the nullable parameters in the documented signature. Linux assigns execve system-call number 221 in the RISC-V convention described here.

To construct a call, the same convention applies: put the system-call number in a7, place its arguments in the argument registers, and execute ecall. The source poses a simple_exec implementation task but does not provide its assembly implementation, so the call setup can be described from the convention and signature without claiming a particular wrapper implementation.