Scroll to turn the page
Operating Systems · Operating Systems
one machine, many programs, each sure it is alone
Your laptop has a handful of processor cores, one memory and one disk. Right now it is running hundreds of programs. Each one is written as if it had the whole machine to itself.
The operating system makes that true enough. In the figure (hypothetical) three programs take turns on one CPU, a slice at a time, fast enough that each seems to run all the time. Memory is split into frames: the kernel keeps 2, each program gets 3 of its own, and 1 is free. The program running now is lit red, and so is its memory.
That is the first of the operating system’s three jobs, and the three parts of this book:
The program that does all this is the kernel: the part of the operating system that runs with full control of the hardware. This chapter is about how it keeps that control while getting out of the way.
a bit in the CPU decides what code may do
If any program could do anything, one buggy program could erase the disk, read another program’s memory or simply never give the CPU back. So the hardware itself enforces a line.
The CPU runs in one of two modes. In kernel mode any instruction is allowed. In user mode the privileged instructions are not: they would
x86 processors have 4 privilege levels, rings 0 to 3; Linux, Windows and macOS use only two, ring 0 for the kernel and ring 3 for programs.
A program in user mode that tries a privileged instruction does not get to run it. The CPU traps into the kernel instead, and the kernel usually ends the program (on Linux, with a signal such as SIGSEGV or SIGILL). That is the first half of the figure.
The second half is the legal way down: the program asks the kernel with a system call. It enters the kernel only through an entry point the kernel chose in advance, the kernel checks the request and does the work in kernel mode, then drops back to user mode. A program can request anything; it can do nothing on its own.
read(), step by step
Every time a program opens a file, sends a packet, starts another program or asks the time, it makes a system call. Here is one, read, on Linux on an x86-64 CPU.
n = read(fd, buf, 4096); mov rax, 0 ; read is call 0 mov rdi, fd ; arguments in rdi, rsi, rdx syscall ; trap into the kernel ; ... back in user mode, n in rax
The program never names an address in the kernel. It names a number, and the kernel’s trap table (set up at boot, in kernel memory) says where call number 0 goes. Some of Linux’s x86-64 numbers:
| call number | call |
|---|---|
| 0 | read |
| 1 | write |
| 2 | open |
| 3 | close |
| 39 | getpid |
| 57 | fork |
| 59 | execve |
| 60 | exit |
The trap saves the program’s registers so the return can restore them exactly. The handler checks every argument (is fd an open file of this process? is buf memory the process owns?), because the caller may be buggy or hostile. The result comes back in a register: here n, the number of bytes read, 4096 if the whole request was met.
Library functions such as printf or fopen are not system calls; they are ordinary code that makes system calls (write, open) when they need the kernel.
the timer, and the devices
A system call is the program choosing to enter the kernel. But a program stuck in an infinite loop never chooses. How does the kernel ever get the CPU back?
With a timer interrupt. At boot the kernel starts a hardware timer that interrupts the CPU at a fixed rate. Each interrupt saves what was running and jumps to the kernel’s handler, whatever the program was doing. Linux is built with one of 100, 250, 300 or 1000 ticks a second, so a tick every 10 ms down to every 1 ms.
In the figure the timer runs at 250 a second, a tick every 4 ms. Program A has a time slice of 3 ticks, 12 ms. At each tick the kernel runs for a moment, sees A still has time, and returns to it. At the tick that ends the slice the kernel switches to program B. That is preemption: a program loses the CPU without giving it up.
Devices interrupt too. A disk that finishes a read, a network card that receives a packet, a key pressed: each raises an interrupt, and the kernel’s handler wakes the program that was waiting.
| way in | caused by | example |
|---|---|---|
| system call | the program, on purpose | read, fork |
| exception (fault) | the program, by accident | divide by zero, a bad address |
| interrupt | hardware, any time | the timer, a disk, a keyboard |
These three are the only ways into the kernel. Between them, the kernel always gets control back.
each stage loads the next
When the power comes on, memory is empty and there is no kernel. Something has to load it.
| stage | what it does |
|---|---|
| power on | the CPU starts at a fixed address in firmware |
| firmware (UEFI) | tests the hardware, finds a boot loader |
| boot loader | loads the kernel image into memory |
| kernel | sets up memory, the trap table, the timer, drivers |
| init (PID 1) | the first process: starts every service |
| login / shell | programs run in user mode |
The firmware lives in a chip on the motherboard, so it is there at power-on. On modern PCs it is UEFI (older PCs used BIOS). It checks the hardware and finds a boot loader on the disk (GRUB on many Linux systems), which loads the kernel into memory and jumps to it.
The kernel starts in kernel mode and prepares everything later pages rely on:
The first process, init, gets process ID 1 (on most Linux systems today it is systemd). It is the first code to run in user mode, and every other process descends from it.
The order matters. The trap table and the timer are installed before any program runs, so by the time user code exists the kernel already owns every way back in.
how much belongs in kernel mode
Everything that runs in kernel mode can crash or compromise the whole machine. So how much should be there?
A monolithic kernel puts every service in kernel space: scheduler, memory, file system, drivers, network. A request enters the kernel once and returns once; inside, services call each other as ordinary functions. It is fast, and it is what Linux does (it can load drivers as modules while running, but they still run in kernel mode).
A microkernel keeps only the minimum in kernel mode (IPC, scheduling, memory basics) and runs the rest, such as the file server, disk driver, network server, as ordinary user processes that talk by passing messages. A crashed driver is just a process that can be restarted. MINIX 3 and seL4 are microkernels; seL4’s kernel has a machine-checked proof that its code matches its specification.
The cost is boundary crossings. In the figure (a simplified path) one file read in the monolithic kernel crosses the user–kernel line 2 times. In the microkernel it is 4 messages (app to file server, file server to driver and back, file server to app), each into and out of the kernel: 8 crossings.
Real systems mix the two. macOS’s kernel, XNU, combines the Mach microkernel with BSD code in one kernel address space; Windows runs most services in kernel mode too. Whatever the design, the rest of this book is the same three jobs: virtualize, coordinate, persist.