Skip to content
systemcoding.dev
Fundamentals

User Mode vs Kernel Mode and Why System Calls Exist

CPUs split execution into user and kernel mode so the OS can protect itself. See how a Linux x86-64 system call crosses over and what a privileged fault does.

6 min read
Summarize in AI
Flat illustration of two options weighed side by side, with the Linux, C and GNU logos.

Your program can compute anything it wants, but it cannot write a byte to the disk, send a packet or even allocate a page of memory on its own. It has to ask the operating system. That asking is a system call, and the reason it exists is a hardware feature: the CPU runs code in at least two privilege levels, and only one of them is allowed to touch hardware or change the rules of the machine.

This post explains the two modes, shows what a system call is by making one without the C library, and demonstrates what happens when user code tries something only the kernel may do. The examples target Linux 6.8 on x86-64 with GCC 14 and glibc 2.40. The same idea holds on Arm and RISC-V, where the names differ.

Two modes, one reason

A CPU that supports an operating system divides instructions into ordinary ones and privileged ones. Ordinary instructions (arithmetic, loads, stores, jumps within your own memory) can run anywhere. Privileged instructions change the machine itself: they program the interrupt controller, switch page tables, halt the core or talk directly to devices.

The current privilege level is a property of the CPU, and the operating system uses it to separate itself from programs:

  • Kernel mode (supervisor mode) can execute any instruction and touch any memory. On x86 it is ring 0. The kernel runs here.

  • User mode can run ordinary instructions only, and can touch only memory the kernel has mapped for it. On x86 it is ring 3. Your programs run here.

The reason is protection. If any program could write to any address or reprogram the page tables, one buggy or hostile program could corrupt the others or the kernel. With the split, a program that misbehaves is stopped by the hardware and killed by the kernel, and everything else keeps running. It is also the reason a crash in a user program does not take down the machine, while a crash in the kernel usually does.

What a system call is

A program cannot just jump into kernel code, because user mode is not allowed to run it. The processor provides a controlled doorway instead: a special instruction that switches to kernel mode and jumps to one address chosen by the kernel in advance, never by the caller. On x86-64 that instruction is syscall. The kernel sets up the target address in a register when it boots, so a program can enter at exactly one place.

The calling convention on Linux x86-64 is fixed by the kernel ABI. The system call number goes in rax, up to six arguments go in rdi, rsi, rdx, r10, r8 and r9, and the result comes back in rax, with negative values from -4095 to -1 meaning -errno. The syscall instruction clobbers rcx and r11, because the CPU saves the return address and flags there.

You normally call a system call through a C library wrapper such as write. You can also call it directly through the generic syscall function from <unistd.h>, which is the same convention without the wrapper:

raw.cc
#define _GNU_SOURCE
#include <sys/syscall.h>
#include <unistd.h>

int main(void) {
    static const char msg[] = "hello from a raw system call\n";

    long n = syscall(SYS_write, 1, msg, sizeof msg - 1);
    long pid = syscall(SYS_getpid);

    return (n == (long)sizeof msg - 1 && pid > 0) ? 0 : 1;
}
Shell
gcc -std=c17 -Wall -Wextra raw.c -o raw
./raw
strace -e trace=write,getpid ./raw
Text
write(1, "hello from a raw system call\n", 29hello from a raw system call
) = 29
getpid()                                = 31877
+++ exited with 0 +++

The strace lines are the evidence: write and getpid are two crossings from user mode to kernel mode, and a third one, exit_group, ends the process but is filtered out here. The process ID will differ on your machine, and the program's own output lands inside the strace line because both write to the same terminal.

Each crossing does real work. The CPU switches stacks, the kernel saves the user registers, checks the arguments, runs the handler, and returns with sysret. The kernel never trusts a pointer you give it: before reading msg it verifies the address belongs to your process, which is why passing a bad pointer returns EFAULT instead of crashing the machine.

What a system call protects

System calls are the places where the kernel applies its rules. Opening a file checks permissions. Allocating memory with mmap is bounded by limits. Sending a packet goes through the network stack and firewall. Because the user program cannot reach the disk or the network card directly, there is exactly one path to them, and the kernel can enforce policy on it.

The list of what needs a system call is the list of everything a program shares with the rest of the machine:

You want toCall (Linux)Why user mode cannot do it itself
Read or write a fileread, write, openatThe disk driver is in the kernel
Allocate memory pagesmmap, brkPage tables belong to the kernel
Create a processclone, execveThe scheduler owns processes
Send data on a networksendto, write on a socketThe network stack is in the kernel
Get the time from the clockclock_gettimeUsually served by the vDSO, no switch needed

The last row is worth knowing. For a few calls the kernel maps a small piece of code into every process, the vDSO, that reads data the kernel keeps up to date, so clock_gettime can run without entering the kernel at all.

What happens when user code breaks the rule

If a user-mode program executes a privileged instruction, the CPU does not run it. It raises an exception, a general protection fault on x86, and transfers control to the kernel, which sends the offending process a signal. Here is the smallest demonstration, a program that tries to halt the CPU:

halt.cc
#include <stdio.h>

int main(void) {
    puts("about to execute hlt in user mode");
    fflush(stdout);
    __asm__ volatile ("hlt");
    puts("not reached");
    return 0;
}
Shell
gcc -std=c17 -Wall -Wextra halt.c -o halt
./halt
echo "exit status: $?"
Text
about to execute hlt in user mode
Segmentation fault (core dumped)
exit status: 139

hlt is privileged, so the CPU faults, the kernel sees a fault it cannot repair in a user process, and delivers SIGSEGV. The shell reports exit status 139, which is 128 plus signal number 11. The "core dumped" text depends on the system's core file settings and may not appear on yours. The process dies, the machine keeps running, and that is the protection working.

Costs, and the idea of a boundary

Crossing the boundary is not free. A system call involves a mode switch, register saves, argument checks and, since the speculative-execution vulnerabilities disclosed in 2018, extra mitigation work on some processors. The practical advice follows from that. Do I/O in large blocks so one call moves more data, buffer small writes in user space as stdio does, and batch operations where an interface allows it, such as io_uring for submitting many requests with few crossings.

Two caveats keep the picture honest. Not every operation needs the kernel: arithmetic, memory access to pages already mapped, and function calls into libraries all stay in user mode, which is why most of a program's time is user time. And the two-mode model is the simplest case. Hypervisors add another level beneath the kernel, and some systems, such as microcontrollers running an RTOS without memory protection, have no user and kernel split at all, so every task can touch every register.

Also inKernels
Taggedcbeginner
Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.