Lesson 9 · Operating Systems

Microkernels

A kernel that keeps only address spaces, thread scheduling and message passing in kernel mode. Drivers, file systems and networking run as user processes, so a crash stays in one process, and every service call becomes a message.

Microkernels concept diagramA visual explanation of the layout and operations shown in this lesson.only three things need kernel privilege — everything else is a processfile systemdisk drivernetwork stackdisplay serverabove the line: ordinary user processesbelow: the only privileged codekerneladdress spaces · threads · IPCevery service call is a message through the kernel, not a function calla driver fault kills one process — Mach IPC ≈ 115 µs, L4 ≈ 5 µs, and that 20× is why this became practical
1

What a Microkernel Is

A microkernel in OS design is a kernel that does as little as possible in kernel mode. Jochen Liedtke's test is the usual rule: a feature goes in the kernel only if leaving it out would stop the system from working at all. What passes is roughly three things: address spaces, threads with a scheduler, and inter-process communication (IPC).

Everything else becomes an ordinary user process: file systems, device drivers, the network stack, even the pager that handles page faults. They talk by sending messages through the kernel. The kernel itself shrinks to around 10,000 lines of code, against tens of millions in a monolithic kernel such as Linux.

  • Kernel mode: address spaces, threads, IPC
  • User processes: drivers, file systems, networking
2

Microkernel Architecture: One Read, Step by Step

  1. App → file server: request (2 mode switches)
  2. File server → disk driver: request (2)
  3. Disk interrupt → kernel → driver, as a message
  4. Driver → file server: data (2)
  5. File server → app: data (2)

In a microkernel architecture, a read() is a chain of messages. The app cannot call file-system code, because there is none in the kernel, so it sends a message to the file server. The file server has no right to the disk, so it sends a message to the disk driver. The driver starts the disk; when the disk finishes, the kernel turns its interrupt into a message for the driver. Then the replies travel back.

  • 4 messages, 8 mode switches
  • Monolithic kernel: 1 system call, 2 mode switches
3

Why IPC Cost Decides Everything

Eight mode switches is a fixed cost of the structure, so the only lever is how much each message costs. Mach, the best-known first-generation microkernel, took about 115 µs per message on early-1990s hardware. Four messages put about 460 µs of overhead on one read, which is why microkernels were called slow during the 1992 Tanenbaum-Torvalds debate.

Liedtke's L4 (1993) got a message down to about 5 µs on the same class of machine, about 20 times faster. It passed short messages in CPU registers instead of memory buffers, cut checks from the fast path, and tuned the code to the processor's cache and TLB. After L4, the argument was about isolation against speed, not about a design that could not keep up.

  • Mach: about 115 µs per message
  • L4: about 5 µs per message
Implementation

One read() on a microkernel

class Kernel:
    def __init__(self):
        self.servers = {}
        self.messages = 0

    def call(self, to, request):
        self.messages += 2  # the request and its reply both pass through the kernel
        return self.servers[to](request)


kernel = Kernel()
state = {"crash_next": False}


def disk_driver(request):
    if state["crash_next"]:
        state["crash_next"] = False
        return None
    return request["sectors"] * 512


def file_server(request):
    command = {"lba": request["offset"] // 512, "sectors": request["length"] // 512}
    data = kernel.call("driver", command)
    if data is None:
        print("supervisor: driver restarted, request sent again")
        data = kernel.call("driver", command)
    return data


kernel.servers = {"fs": file_server, "driver": disk_driver}

data = kernel.call("fs", {"offset": 8192, "length": 4096})
print(f"read: {data} bytes, {kernel.messages} messages, {2 * kernel.messages} mode switches")
print("monolithic kernel: 2 mode switches")
for name, us in (("Mach", 115), ("L4", 5)):
    print(f"{name}: {kernel.messages} x {us} us = {kernel.messages * us} us of IPC")

state["crash_next"] = True
kernel.messages = 0
data = kernel.call("fs", {"offset": 8192, "length": 4096})
print(f"read after a driver crash: {data} bytes, {kernel.messages} messages, app still running")
#include <cstdio>
#include <functional>
#include <map>
#include <optional>
#include <string>
using namespace std;

struct Request { int offset, length, lba, sectors; };

struct Kernel {
    map<string, function<optional<int>(Request)>> servers;
    int messages = 0;
    optional<int> call(const string& to, Request r) {
        messages += 2;  // the request and its reply both pass through the kernel
        return servers[to](r);
    }
};

Kernel kernel;
bool crashNext = false;

optional<int> diskDriver(Request r) {
    if (crashNext) { crashNext = false; return nullopt; }
    return r.sectors * 512;
}

optional<int> fileServer(Request r) {
    Request command{0, 0, r.offset / 512, r.length / 512};
    optional<int> data = kernel.call("driver", command);
    if (!data) {
        printf("supervisor: driver restarted, request sent again\n");
        data = kernel.call("driver", command);
    }
    return data;
}

int main() {
    kernel.servers["fs"] = fileServer;
    kernel.servers["driver"] = diskDriver;
    int data = *kernel.call("fs", {8192, 4096, 0, 0});
    printf("read: %d bytes, %d messages, %d mode switches\n", data, kernel.messages, 2 * kernel.messages);
    printf("monolithic kernel: 2 mode switches\n");
    printf("Mach: %d x 115 us = %d us of IPC\n", kernel.messages, kernel.messages * 115);
    printf("L4: %d x 5 us = %d us of IPC\n", kernel.messages, kernel.messages * 5);
    crashNext = true;
    kernel.messages = 0;
    data = *kernel.call("fs", {8192, 4096, 0, 0});
    printf("read after a driver crash: %d bytes, %d messages, app still running\n", data, kernel.messages);
}
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

public class Main {
    record Request(int offset, int length, int lba, int sectors) {}

    static Map<String, Function<Request, Integer>> servers = new HashMap<>();
    static int messages = 0;
    static boolean crashNext = false;

    static Integer call(String to, Request r) {
        messages += 2;  // the request and its reply both pass through the kernel
        return servers.get(to).apply(r);
    }

    static Integer diskDriver(Request r) {
        if (crashNext) { crashNext = false; return null; }
        return r.sectors() * 512;
    }

    static Integer fileServer(Request r) {
        Request command = new Request(0, 0, r.offset() / 512, r.length() / 512);
        Integer data = call("driver", command);
        if (data == null) {
            System.out.println("supervisor: driver restarted, request sent again");
            data = call("driver", command);
        }
        return data;
    }

    public static void main(String[] args) {
        servers.put("fs", Main::fileServer);
        servers.put("driver", Main::diskDriver);
        int data = call("fs", new Request(8192, 4096, 0, 0));
        System.out.println("read: " + data + " bytes, " + messages + " messages, " + 2 * messages + " mode switches");
        System.out.println("monolithic kernel: 2 mode switches");
        System.out.println("Mach: " + messages + " x 115 us = " + messages * 115 + " us of IPC");
        System.out.println("L4: " + messages + " x 5 us = " + messages * 5 + " us of IPC");
        crashNext = true;
        messages = 0;
        data = call("fs", new Request(8192, 4096, 0, 0));
        System.out.println("read after a driver crash: " + data + " bytes, " + messages + " messages, app still running");
    }
}
Watch it run

Step through it

Running on read(fd, buf, 4096) · app, file server and disk driver as user processes

a microkernel system before the calluser spacekernel modeappuser processfile serveruser processdisk driveruser processmicrokernel: address spaces · threads · IPCdiskhardwaremessages 0 · mode switches 0the app wants 4 KB from a file: read(fd, buf, 4096)every hop between processes must go through the kernelservices run outside the kernel
Step 1

Three user processes, one tiny kernel

The kernel keeps only what needs kernel mode: address spaces, scheduling threads, and passing messages. The file server and the disk driver are ordinary user processes with their own memory. None of them can touch another's memory, and only the driver is allowed to reach the disk.

Output
1 / 11
4

Microkernel vs Monolithic Kernel

Microkernel vs monolithic kernel is a trade between isolation and speed. A monolithic kernel puts every service in one privileged address space, so a call between subsystems is a function call, and any bug in any subsystem can corrupt all of them. A microkernel pays for messages and gets walls between services.

Microkernel vs monolithic kernel
MicrokernelMonolithic kernel
In kernel modeAddress spaces, threads, IPCEverything, including drivers
Call between servicesMessage through the kernelFunction call
Mode switches for one read()82
Driver bugKills one processCan crash or corrupt the kernel
Code with full privilegeAbout 10,000 linesTens of millions of lines
ExamplesQNX, seL4, MINIX 3, ZirconLinux, FreeBSD
  • Monolithic: faster calls between services
  • Microkernel: faults contained, kernel small enough to verify
5

Microkernel Advantages and Disadvantages

The main advantage is fault containment. A driver that follows a null pointer faults in its own memory, the kernel ends it, and a supervisor can start a fresh copy; MINIX 3 restarts crashed drivers without running programs noticing. The second is a small trusted kernel: seL4, about 8,700 lines of C, has a machine-checked proof that the code matches its specification.

The disadvantages are the message cost on every service call and harder design. Containing a crash is not the same as recovering from it: if the file server dies, a restarted copy has lost its open files and unwritten data unless it saved them elsewhere. Services also need careful protocols between them, work a monolithic kernel avoids by sharing memory.

  • Advantages: fault containment, restartable services, a kernel small enough to prove
  • Disadvantages: IPC on every call, lost state when a stateful service crashes
6

Where Microkernels Run

Microkernels are common where a crash is unacceptable and the workload is known. QNX runs in over 200 million vehicles. seL4 is used in defence and secure-communication systems. OKL4, an L4 variant, ran on the baseband (radio) processors of over a billion phones. Google's Fuchsia is built on the Zircon microkernel, and a version of MINIX 3 runs inside the Intel Management Engine of most modern Intel chipsets.

General-purpose desktops and servers went the other way. Windows NT and macOS began from microkernel ideas but keep drivers and much else in kernel mode for speed, which is why they are called hybrids. How a program enters the kernel in the first place is covered in kernel and system calls.

  • QNX, seL4, OKL4, Zircon, MINIX 3
  • Windows and macOS: hybrids, drivers in kernel mode