Lesson 10 · Operating Systems

Monolithic Kernels

A kernel that runs the scheduler, memory manager, file systems, network stack and every driver in one privileged address space. Calls between them are plain function calls, which is fast, and any bug in any of them can corrupt the rest.

Monolithic Kernels concept diagramA visual explanation of the layout and operations shown in this lesson.every subsystem inside one address space, all fully privilegedkernel address spacesystem call layerschedulermemory managerVFSnetwork stackblock layerdisk drivernet drivergpu drivera call between any two boxes is a jump — no message, no copy, no context switchthe highlighted drivers are the majority of the source and the majority of the bugsa loadable module is linked INTO this box: dynamically loaded, never isolated
1

What a Monolithic Kernel Is

A monolithic kernel in OS design runs every operating system service in one address space, in kernel mode: the scheduler, memory management, file systems, the network stack and every device driver. When the file system needs a disk block it calls the block layer the way any C function calls another, with arguments in registers and data passed by pointer.

Monolithic does not mean unstructured. Linux has clear internal layers: the virtual file system (VFS), the block layer, the network stack, the driver model. But those layers are kept by code review, not by hardware. Any kernel code can read or write any kernel address.

  • One address space for every service
  • Internal layers are conventions, not walls
2

Monolithic Kernel Architecture: One Read

  1. System call: the CPU switches to kernel mode (switch 1)
  2. VFS finds the file and calls the block layer
  3. The block layer turns the offset into a sector and calls the disk driver
  4. The driver starts the disk and the data comes back up by function returns
  5. The kernel returns to user mode (switch 2)

In a monolithic kernel architecture, the only protection boundary on a read() is between the app and the kernel. The app makes one system call; inside, each layer calls the next directly; then the kernel returns to user mode. On a microkernel the same read is four messages between separate processes.

  • 2 mode switches for the whole read
  • A microkernel needs 8, one pair per message
3

Loadable Kernel Modules

A modern monolithic kernel is not one fixed binary. Loadable kernel modules (Linux .ko files loaded with insmod or modprobe, FreeBSD's KLDs, Windows .sys drivers) are added and removed at run time, so one kernel image supports thousands of devices without keeping all their code in memory.

A module is linked into the kernel's own address space and runs in kernel mode. It is loaded on demand, not isolated: a bad pointer in a module corrupts kernel memory exactly as built-in code would. Modules solve memory use and distribution, not fault containment. Linux can require modules to be signed, which stops unknown code from loading but does nothing about bugs in signed code.

  • Loaded and unloaded at run time
  • Same privilege as the rest of the kernel
Implementation

Inside a monolithic kernel

BUFFER = 1500
kernel_memory = bytearray(BUFFER + 1)  # net driver's buffer, then the scheduler's prio byte
PRIO = BUFFER
kernel_memory[PRIO] = 20
switches = 0


def disk_driver(sector):
    return 4096


def block_layer(offset):
    return disk_driver(offset // 512)


def vfs_read(offset):
    return block_layer(offset)


def sys_read(offset):
    global switches
    switches += 1  # user -> kernel
    n = vfs_read(offset)
    switches += 1  # kernel -> user
    return n


def net_receive(frame):
    for i, b in enumerate(frame):  # no length check: the bug
        kernel_memory[i] = b


def scheduler_tick():
    prio = kernel_memory[PRIO]
    return f"prio = {prio}" if prio < 140 else f"panic: impossible prio {prio}"


print(f"read: {sys_read(8192)} bytes, {switches} mode switches, 2 function calls inside")
net_receive(bytes([0xFF]) * 1500)
print(f"1500-byte frame: {scheduler_tick()}")
net_receive(bytes([0xFF]) * 1501)
print(f"1501-byte frame: {scheduler_tick()} (written by the net driver)")
#include <cstdio>
#include <vector>
using namespace std;

const int BUFFER = 1500, PRIO = BUFFER;
vector<unsigned char> kernelMemory(BUFFER + 1);  // net driver's buffer, then the scheduler's prio byte
int switches = 0;

int diskDriver(int sector) { return 4096; }
int blockLayer(int offset) { return diskDriver(offset / 512); }
int vfsRead(int offset) { return blockLayer(offset); }

int sysRead(int offset) {
    switches++;  // user -> kernel
    int n = vfsRead(offset);
    switches++;  // kernel -> user
    return n;
}

void netReceive(const vector<unsigned char>& frame) {
    for (size_t i = 0; i < frame.size(); i++) kernelMemory[i] = frame[i];  // no length check: the bug
}

void schedulerTick(const char* label, const char* suffix) {
    int prio = kernelMemory[PRIO];
    if (prio < 140) printf("%s: prio = %d%s\n", label, prio, suffix);
    else printf("%s: panic: impossible prio %d%s\n", label, prio, suffix);
}

int main() {
    kernelMemory[PRIO] = 20;
    int n = sysRead(8192);
    printf("read: %d bytes, %d mode switches, 2 function calls inside\n", n, switches);
    netReceive(vector<unsigned char>(1500, 0xFF));
    schedulerTick("1500-byte frame", "");
    netReceive(vector<unsigned char>(1501, 0xFF));
    schedulerTick("1501-byte frame", " (written by the net driver)");
}
import java.util.Arrays;

public class Main {
    static final int BUFFER = 1500, PRIO = BUFFER;
    static byte[] kernelMemory = new byte[BUFFER + 1];  // net driver's buffer, then the scheduler's prio byte
    static int switches = 0;

    static int diskDriver(int sector) { return 4096; }
    static int blockLayer(int offset) { return diskDriver(offset / 512); }
    static int vfsRead(int offset) { return blockLayer(offset); }

    static int sysRead(int offset) {
        switches++;  // user -> kernel
        int n = vfsRead(offset);
        switches++;  // kernel -> user
        return n;
    }

    static void netReceive(byte[] frame) {
        for (int i = 0; i < frame.length; i++) kernelMemory[i] = frame[i];  // no length check: the bug
    }

    static String schedulerTick() {
        int prio = kernelMemory[PRIO] & 0xFF;
        return prio < 140 ? "prio = " + prio : "panic: impossible prio " + prio;
    }

    static byte[] frame(int size) {
        byte[] f = new byte[size];
        Arrays.fill(f, (byte) 0xFF);
        return f;
    }

    public static void main(String[] args) {
        kernelMemory[PRIO] = 20;
        int n = sysRead(8192);
        System.out.println("read: " + n + " bytes, " + switches + " mode switches, 2 function calls inside");
        netReceive(frame(1500));
        System.out.println("1500-byte frame: " + schedulerTick());
        netReceive(frame(1501));
        System.out.println("1501-byte frame: " + schedulerTick() + " (written by the net driver)");
    }
}
Watch it run

Step through it

Running on read(fd, buf, 4096), then insmod netdrv.ko and a 1501-byte frame

Linux-style monolithic kernelappuser modekernel: one address space, full privilegeVFSblock layerdisk driverschedulermode switches 0internal layers are code boundaries, kept by review, not by hardwareone address space for everything
Step 1

Every subsystem in one address space

A monolithic kernel puts the file system layer (VFS), the block layer, the drivers and the scheduler in one address space, all running in kernel mode. The boxes inside are code modules, not walls: any of them can read or write any kernel address, and nothing in the hardware checks which module is doing it.

Output
1 / 9
4

The Cost: Every Bug Has Full Privilege

Everything in the kernel is trusted, and the Linux source is now well over 30 million lines, most of it drivers. Studies of kernel bugs, starting with Chou and colleagues in 2001, found error rates in driver code three to seven times higher than in the rest of the kernel. Drivers are written for hardware with poor documentation, tested on few machines and read by few reviewers.

Because a driver shares memory with everything else, its bugs show up somewhere else. A network driver that copies a 1,501-byte frame into a 1,500-byte buffer writes one byte past the end; if that byte belongs to a scheduler structure, the kernel panics later in the scheduler, and the crash report points at code with no bug in it. KASLR, module signing, lockdown mode, eBPF's verifier and Rust drivers all narrow the risk. None of them puts a wall between the driver and the scheduler.

  • Driver bugs are several times more common than core kernel bugs
  • A fault in one subsystem can surface in another
5

Monolithic and Hybrid Kernels in Practice

Linux, FreeBSD, OpenBSD, NetBSD, Solaris and AIX are monolithic. Windows NT and macOS are usually called hybrids: macOS's XNU contains the Mach microkernel, but the BSD layer and the drivers run beside it in kernel mode, so for fault isolation it behaves like a monolithic kernel.

Windows shows the trade being made on purpose. NT 3.51 ran the window manager and graphics in a user-mode process; NT 4.0 (1996) moved them into the kernel to cut the cost of crossing between processes. General-purpose systems, where workloads vary and speed is what users notice, chose this design; systems where a crash is unacceptable chose microkernels. Kernel and system calls covers how programs enter either kind.

  • Monolithic: Linux, the BSDs, Solaris, AIX
  • Hybrid: Windows NT, macOS (XNU)