Foundations: The Machine Model

kernel space versus user space (overview)

Imagine a building where the ordinary offices (where everyone works) are kept strictly separate from the locked control room that runs the heating, power, and security for the whole building. Visitors stay in the offices; only authorized staff, through a guarded door, can touch the controls. A computer draws the same line: user space is where your programs run, and kernel space is the protected zone where the operating system's core runs with full power over the hardware.

The CPU itself enforces this split. It can run in a privileged mode (often called kernel or supervisor mode), where code may touch any memory and command the hardware directly, or in a restricted mode (user mode), where dangerous operations are forbidden. Your application runs in user mode. When it needs something only the kernel is allowed to do - read a file, send network data, allocate memory from the OS - it cannot just do it; it must ask, by making a system call, which is the guarded door that switches the CPU into kernel mode, runs trusted kernel code to do the job, and switches back.

Why it matters even at a glance: this boundary is the reason one buggy program usually cannot crash the whole machine or read another program's private memory - the kernel and the hardware refuse the forbidden moves. It is central to security, stability, and the cost model of systems code, because crossing the boundary (a system call) is far more expensive than an ordinary function call. The detailed mechanics - rings, traps, mode switches - come later; the picture to hold now is simply two protected zones with a single guarded doorway between them.

When your program calls printf("hi"), the library eventually makes a write() system call: the CPU traps into kernel mode, the kernel pushes the bytes to the terminal device, then control returns to your user-mode code. Your code never touched the screen hardware directly.

User code asks; the kernel acts; control returns - the guarded door in action.

'Kernel space' and 'user space' are about privilege and protected memory regions, not physical chips. The exact mechanisms (privilege rings, traps) are covered later; this entry is only the orienting overview.

Also called
kernel mode versus user modesupervisor versus user space核心模式 vs 使用者模式