The table
Three kinds of event stop a program and run the kernel: a system call (the program asks), an exception (an instruction goes wrong) and an interrupt (a device needs attention). All three are traps: controlled jumps into kernel mode.
At boot the kernel fills the interrupt table: 256 numbered rows on x86-64, each pointing to a kernel handler. The CPU fixes rows 0 to 31 for exceptions and other CPU events; the rest serve devices and the kernel. Only the kernel can set it.
Every trap runs the same four moves, in hardware: find the event's number, save the program's place (its instruction address and flags), switch to kernel mode, and jump to the handler in that row. The program never picks where it lands.
A fault, fixed
Line 1 writes into a big array. To save work, the kernel gives an array page real memory only when it is first used. This page has none yet, so the CPU raises exception 14, a page fault, and switches to kernel mode.
Row 14 sends the CPU to the kernel's page-fault handler. It checks the address: it belongs to the program's array, so the fault is fine. It takes a free page of memory, fills it with zeros and maps it into the program.
Return-from-trap goes back to the same instruction, not the next one. The CPU runs the write again, and now it works: big[500000] becomes 1. The program never knew. A fault the kernel can fix is retried.
An interrupt
Line 2 just counts up. Meanwhile the timer the kernel set at boot fires, every few milliseconds. This is an interrupt: it comes from a device, not from the instruction running now, so it can arrive at any moment.
The CPU pauses the loop, saves its place, switches to kernel mode and looks up row 236, the local timer on Linux. The handler counts the tick and may pick another program. Then it returns and the loop carries on.
A fault, fatal
Line 3 divides 10 by zero. There is no answer, so the CPU's divide instruction raises exception 0, a divide error. As with every trap, the CPU saves the program's place, switches to kernel mode and jumps through row 0.
The handler has nothing to fix: no page to add, no answer to find, and retrying would fail again. So the kernel sends the program the signal SIGFPE, whose default action ends it. The shell prints Floating point exception.
One table, filled by the kernel at boot, decides where every event goes. Exceptions come from the instruction running now; interrupts come from devices at any moment. Fixable faults are retried; fatal ones end only the program that caused them.
What happens
;; step 1/11 · three-ways
event who causes it when
system call the program asks when it chooses
exception the instruction now at that instruction
(divide by zero, bad address, ...)
interrupt a device (timer, disk) any moment
all three: a trap into the kernel, in kernel mode
The figure shows 4 of the table's 256 rows. Rows 0 (divide error) and 14 (page fault) are numbered by the CPU itself; rows 128 (int 0x80) and 236 (local timer) are the numbers Linux chooses on x86-64. On RISC-V, xv6 uses no table of rows: every trap enters at one address (stvec, in direct mode) and the CPU puts the cause number in scause. In C, dividing an int by zero is undefined behaviour. On x86-64 the compiled divide instruction raises the divide error shown here. On 64-bit ARM, integer division by zero gives 0 and nothing traps, so the program would carry on.