User mode and kernel mode
Video 027 of 140 in Virtualization Foundations, in the unit “Privilege, system calls and interrupts”. 4:00, with English and Hindi audio.
A server runs many programs at once: a database, a web server, a backup agent. One bad program should not crash the whole machine, or read another program's data. How is that guaranteed? The processor itself runs code in two modes. This article explains user mode and kernel mode, how a program's request for the hardware passes through the kernel, and where that protection can still fail.
What you will learn
- User mode runs your programs. They can compute and use their own memory, but they cannot reach the hardware directly.
- Kernel mode runs the core of the operating system, with full power over the hardware and memory.
- Programs reach the hardware only by asking the kernel, which checks each request first.
- A bug in kernel code, such as a driver, can take the whole machine down.
Two kinds of code on one processor
Modern processors work in at least two modes. In user mode, your programs run. They may compute and use their own memory, but the processor stops them from touching the hardware directly.
Kernel mode is different. There, the core of the operating system runs. That core is called the kernel. It has full power over the hardware and over the memory of the machine.
Where the kernel sits between your programs and the hardware
- At the base is the hardware: the processor, the memory, the disks and the network cards.
- Above it sits the kernel. It drives that hardware on behalf of ordinary programs.
- At the top sit your programs, in user mode. Each one asks the kernel when it needs the hardware, and the kernel decides whether to do it.
Note: Programs reach the hardware only through the kernel.
How a save to disk passes through kernel mode
Take a warehouse app at a logistics firm that wants to save a new order. It cannot write to the disk itself, so it asks the kernel. The program makes the request through a fixed doorway, and the processor switches into kernel mode at once.
The kernel checks the request first. Is this program allowed to write to that place? If not, the request is refused. If it is allowed, the kernel writes the data to the disk on the program's behalf. The program never touched the disk itself. Finally, the kernel returns the result, and the processor goes back to user mode, so the program carries on.
What each mode may do, and where protection can fail
In user mode, an instruction that reaches for the hardware is stopped by the processor itself. In kernel mode, the same instructions are allowed. That power is why kernel code must be careful, and why a bug there matters so much.
If a user program crashes, only that program stops, because it is limited to its own memory. If the kernel fails, the whole machine can stop with it.
The danger sits in the kernel. A driver is the kernel code that talks to one device, such as a network card. It runs with full power, so a buggy driver can corrupt memory that other programs depend on, and bring the whole machine down.
Note: A server that freezes, or reboots by itself, may have a kernel message in its logs. Look there first.
Terms
- User mode
- The processor mode in which your programs run, with no direct access to the hardware.
- Kernel mode
- The processor mode in which the core of the operating system runs, with full access to the hardware and memory.
- Kernel
- The core of the operating system, which controls the hardware on behalf of programs.
- Driver
- Kernel code that talks to one device, such as a network card or a disk.
Check yourself
What stops a user program from writing to the disk on its own?
The processor stops any direct access to the hardware from user mode, so the program must ask the kernel.
Why does the kernel check a request before it acts?
The kernel is shared by every program, so it must confirm that the program is allowed to do the work, such as writing to that place.
Why can a buggy driver stop a whole machine?
A driver runs in kernel mode with full power, so a bug can corrupt memory that other programs depend on.
Previous: Reading lscpu, line by line. Next: The x86 privilege rings.