Skip to main content
Be Bitwise

User vs. Kernel Space

OS & The Kernel · Assembly & OS track

5 min read

In this module

Two Worlds Inside Your Computer

When a computer is running, memory is split into two distinct regions: kernel space and user space. Kernel space is where the kernel and its drivers live. User space is where your applications run. The CPU enforces a hard boundary between them, and crossing it follows a specific protocol.

This separation has one job: protection. If any application could poke directly at hardware or overwrite kernel memory, a single buggy program could take the whole system down.

On 64-bit Linux the split is literal: every process's virtual address space has the kernel mapped into the top half and the program in the bottom half. Hover over each region below. The kernel half is present in every process, but its pages are marked supervisor-only, so a user-mode instruction that dereferences a pointer like 0xFFFF888000000000 gets a page fault and the kernel kills the program with a segmentation fault. The striped gap in the middle can't be used by anyone. (Since the Meltdown attack in 2018, kernels on affected CPUs go further and unmap almost all of the kernel half while user code runs, a fix called kernel page-table isolation.)

64-bit Virtual Address Space Layout

Kernel SpaceDirect mapping, vmalloc, modules, fixmap0xFFFFFFFFFFFFFFFF0xFFFF800000000000Non-canonical HoleAccess causes #GP faultStackGrows downward0x00007FFFFFFFFFFFmmap RegionShared libraries, file mappingsHeapGrows upward (brk/sbrk)BSS / Data / TextProgram image0x0000000000000000

Hover over a region to see details

Protection Rings

Most modern CPUs implement protection rings. Picture concentric circles of trust:

  • Ring 0 (kernel mode) — full access to all hardware and memory. The kernel runs here.
  • Ring 3 (user mode) — restricted access. Your applications run here. They can't execute privileged instructions or touch memory outside their assigned region.

Rings 1 and 2 exist in the hardware spec, but modern operating systems rarely bother with them. In practice, the line that matters is between ring 0 and ring 3.

Why User Programs Cannot Touch Hardware

When the CPU is in user mode (ring 3), it will fault if a program tries to execute a privileged instruction — writing directly to a disk controller, say, or modifying page tables. The CPU flat-out refuses and raises an exception for the kernel to handle, so the rule holds even for a program that's trying to break it.

Mode Switching

So how does a user program do anything useful if it can't touch hardware? It asks the kernel. The request is a system call, and it triggers a mode switch from user mode to kernel mode.

Here's what happens during one:

  1. The user program places the syscall number and arguments in specific registers
  2. It executes a special trap instruction (like syscall on x86-64)
  3. The CPU switches from user mode to kernel mode
  4. The kernel's syscall handler runs, performs the requested operation
  5. The kernel places the result in a register and switches back to user mode
  6. The user program continues with the result

System calls aren't the only way in. Hardware interrupts (a key press, an arriving network packet) and CPU exceptions (a page fault, a divide by zero) also switch the CPU into kernel mode, whether the program asked for it or not.

This round trip isn't free. Mode switching costs more than a regular function call: the CPU has to save and restore state and flip privilege levels. Kernels pay that price on every call because the alternative is letting user code near the hardware.

Worked Example: A User Program Tries hlt

hlt is the x86 instruction that parks the CPU until the next interrupt arrives. The kernel's idle loop uses it when there's nothing to run. Intel's manual lists it as privileged: if the current privilege level isn't 0, the CPU raises a general-protection exception (#GP, vector 13) instead of halting. So what happens if an ordinary C program on x86-64 Linux tries it?

c
int main(void) {
__asm__ volatile ("hlt"); /* privileged: ring 0 only */
return 0;
}
Predict: does the machine freeze, does the instruction get skipped, or something else?

The program dies with Segmentation fault, and the rest of the machine carries on as if nothing happened. Here's the chain:

  1. The CPU decodes hlt, checks the current privilege level, finds 3 and refuses to execute it.
  2. It raises #GP and looks up vector 13 in the interrupt descriptor table, which the kernel filled in at boot. That switches the CPU to ring 0 and runs the kernel's general-protection handler.
  3. The handler sees the fault came from user mode, so it's the program's fault rather than a kernel bug. Linux sends the process SIGSEGV.
  4. The program has no handler for SIGSEGV, so the default action kills it and the shell reports the signal. Linux also logs a line that starts something like traps: a.out[4127] general protection fault ip:401106 sp:7ffd3a2c8e40 error:0 (the numbers are illustrative).

Put the same instruction in a kernel module, running in ring 0, and the check passes: that core halts until the next timer or device interrupt wakes it.

That example also shows the third way into the kernel. Step 2 is an exception, triggered by the instruction itself, which is why a page fault or a divide by zero lands in kernel code without the program asking.

ARM processors draw the same line with different names. Instead of rings they have exception levels: applications run at EL0 and the kernel at EL1, with EL2 for a hypervisor and EL3 for secure firmware. ARM Registers and Modes covers them.

The System Call Interface

The system call interface is the narrow gateway between user space and kernel space. Every interaction your program has with the outside world — reading files, sending network packets, allocating memory — funnels through this interface. We'll dig into system calls properly in the next module.