Two modes
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.
Inside the CPU is a mode setting, at least one bit. In user mode some instructions and some memory are off limits. In kernel mode everything is allowed. The CPU itself checks the mode as it runs each instruction.
The machine starts in kernel mode, so the kernel can set up: it tells the CPU where the gate leads (the trap table) and marks its own memory kernel-only. Then it switches to user mode and starts your program.
Asking the kernel
Plain work needs no permission. In user mode the CPU adds 2 and 3 at full speed and stores 5 in the program's own memory. The kernel is not involved at all.
write() wants the screen, but devices are kernel-only. So it runs a special trap instruction (syscall on x86-64). In one move the CPU switches to kernel mode and jumps through the gate to the entry point the kernel chose.
Now in kernel mode, the kernel checks the request: is file descriptor 1 (standard output) open, and is the text in the program's own memory? It is, so the kernel's driver sends the 3 bytes, h, i and a newline, to the screen.
The kernel puts the answer, 3 bytes written, where the program will look, then runs return-from-trap (sysret on x86-64). In one move the CPU drops back to user mode and the program carries on after the call.
Breaking the rule
Line 3 tries to read kernel memory directly. Those pages are marked kernel-only and the CPU is in user mode, so it refuses the read before any data moves. Nothing in the kernel is read or changed.
A refused access raises an exception, a page fault. Like a system call, it enters the kernel through the gate in kernel mode. But this time the program did not ask: the hardware sent it.
The kernel sees that a user program touched memory it may not use, and sends it the signal SIGSEGV, whose default action ends it. The shell reports Segmentation fault. The kernel and every other program keep running.
Programs run at full speed on the real CPU, yet every risky action goes through one gate the kernel chose at boot. A buggy or hostile program can only hurt itself, not the kernel or the other programs.
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.