KERNEL$ ./proghiSegmentation faultKERNEL ONLYKERNEL ONLY1n = 2 + 3;2write(1, "hi\n", 3);3n = *kernel;CPUMODEusern5hi3?
Back: the kernel's side (kernel, kernel memory, the screen)Front: the user's side (your program, its memory, the CPU and its mode bit)Tokens: a system call and a fault crossing the gate; a read refused at the wallThe kernel's side at the back: the kernel, its locked memory and the screen. A wall marked kernel only, with one gate. The user's side in front: the program, its memory, the CPU with its mode bit, and another program. Line 1 adds on the user's side. Line 2's system call lifts the gate and flips the mode to kernel; the kernel prints hi and returns 3, flipping it back. Line 3's read hits the wall; the fault goes through the gate and the kernel ends the program. The wall and the gate are a picture. In the hardware, the wall is the mode check on instructions and the kernel-only mark on memory pages, and the gate is the trap table: the entry addresses the kernel gave the CPU at boot.

Two modes

  1. A computer runs two kinds of software: your programs, and the kernel, the core of the operating system that is trusted with all the hardware and memory. A wall keeps programs out of the kernel's side, with one guarded gate.

Asking the kernel

Breaking the rule

What happens

;; step 1/11 · two-sides
user side     your programs (apps, the shell, ...)
              limited: must ask for anything risky
kernel side   the kernel: core of the operating system
              full control of the CPU, memory and devices
the wall      built by the CPU itself, not by software
the gate      the only way in, chosen by the kernel

The wall and the gate are a picture. In the hardware, the wall is the mode check on instructions and the kernel-only mark on memory pages, and the gate is the trap table: the entry addresses the kernel gave the CPU at boot. Real CPUs often have more than two levels: x86-64 has four rings (0 to 3) and RISC-V has machine, supervisor and user modes. Operating systems such as Linux use just two for programs and the kernel, so the lesson shows two.