Skip to content
Visual explainer 2h 30m to learn 4 steps

User space, kernel space & hardware

Explain privilege levels and why user programs cannot touch hardware directly.

By Invicly Tech, , in Linux System Programming. About 10 minutes to read.

Topic 5 of 42

In short

The CPU runs user programs in an unprivileged mode and the kernel in a privileged mode. Only the kernel can touch hardware, manage memory mappings or schedule processes, so user programs must ask it through system calls. The C library (glibc) wraps those calls in ordinary functions like open and read.

Think of it like this: The kernel is a bank teller behind glass: you hand over a request slip and it handles the vault.

Explained simply

Your computer runs many programs at once: a browser, a terminal, a music player. If each one could poke the hardware directly, one buggy program could wipe the disk or freeze the machine. So Linux splits the world into two zones.

User space is where normal programs run. They have limited power: they can compute and use their own memory, but nothing more. Kernel space is where the Linux kernel runs, with full power over memory, disks, network cards and the CPU itself.

When a program needs something only the kernel can do, like reading a file, it makes a system call. The CPU switches into a privileged mode, the kernel does the job and checks permissions, and then control returns to the program.

Between your code and the kernel sits the C library, glibc. It turns friendly functions like fopen and printf into the raw system calls the kernel understands.

Press Play on a diagram to walk through it one step at a time.

LayersLinux system layers
  1. 1
    Applications

    bash, nginx, your C program.

  2. 2
    C library (glibc)

    printf, malloc and the system call wrappers.

  3. 3
    System call interface

    The only door into the kernel.

  4. 4
    Kernel subsystems

    Scheduler, memory manager, VFS, network stack.

  5. 5
    Device drivers

    Talk to disks, NICs and other devices.

  6. 6
    Hardware

    CPU, RAM, storage, network.

CompareUser mode vs kernel mode
User mode
  • Runs your programs and libraries
  • Sees only its own virtual memory
  • Cannot run privileged instructions
  • A crash kills one process
Kernel mode
  • Runs the kernel and drivers
  • Can access all memory and devices
  • Handles syscalls, faults and interrupts
  • A crash (panic) stops the machine
MapWays into the kernel
CPU enters kernel mode
  • System call
    • read, write, open
    • fork, execve
    • mmap
  • Exception
    • Page fault
    • Divide by zero
    • Invalid instruction
  • Interrupt
    • Timer tick
    • Network packet
    • Disk I/O done

In depth

How the CPU enforces the two zones

Modern x86-64 CPUs have privilege levels called rings. The kernel runs in ring 0, where every instruction is allowed. User programs run in ring 3, where instructions that talk to devices, change page tables or disable interrupts are forbidden. If a user program tries one, the CPU raises a fault and the kernel usually kills the program.

Memory is protected too. Each page of memory has a flag saying whether user code may touch it. Kernel memory is mapped into every process but marked supervisor-only, so a user program that reads it gets a segmentation fault. ARM CPUs use the same idea with exception levels instead of rings.

  • Ring 0: kernel, full access
  • Ring 3: user programs, restricted
  • Page flags block user access to kernel memory

Three ways to enter the kernel

Control moves from user space into the kernel in only three ways. A system call is a deliberate request, made with the syscall instruction on x86-64. An exception is an accident caused by the current instruction, such as dividing by zero or touching a page that is not in memory yet (a page fault). An interrupt comes from hardware, like a network card saying a packet arrived or the timer saying the current program's time slice is up.

In every case the CPU saves where the program was, jumps to a fixed kernel entry point, and later returns to exactly the same spot. Your program never notices, except that time has passed.

  • System call: program asks on purpose
  • Exception: caused by the current instruction
  • Interrupt: caused by hardware or a timer

What the kernel is made of

Linux is a monolithic kernel: all its main parts run together in kernel space and call each other directly, which is fast. The process scheduler decides which task runs on each CPU. The memory manager handles virtual memory and page tables. The virtual file system (VFS) gives every file system the same interface. The network stack implements TCP/IP. Device drivers talk to hardware.

Drivers can be loaded at run time as modules, which you can list with lsmod. That is flexible, but a buggy module runs with full kernel power, which is why a bad driver can crash the whole machine.

Where glibc fits

glibc is the standard C library on most Linux distros. Some of its functions are thin wrappers: write just puts arguments in registers and runs the syscall instruction. Others do lots of work in user space first: printf formats text into a buffer and only calls write when the buffer is full or a newline is printed to a terminal. Functions like strlen or qsort never enter the kernel at all.

A few frequent calls, like clock_gettime, use the vDSO, a small piece of kernel code mapped into your process so it can run without a real mode switch. Other C libraries, such as musl, make the same system calls with different internals.

  • Thin wrappers: read, write, open
  • Heavy user-space work: printf, malloc
  • No kernel at all: strlen, memcpy, qsort
  • vDSO: fast time calls without a full switch
SequenceWhich calls reach the kernel
Program
glibc
Kernel
  1. 1strlen(s)
    Program to glibc: strlen(s)
  2. 2Answer, no kernel entry
    glibc to Program: Answer, no kernel entry
  3. 3printf("a")
    Program to glibc: printf("a")
  4. 4Text kept in a buffer
    glibc to Program: Text kept in a buffer
  5. 5printf("b\n") to a terminal
    Program to glibc: printf("b\n") to a terminal
  6. 6write(1, "ab\n", 3): syscall
    glibc to Kernel: write(1, "ab\n", 3): syscall
  7. 73 bytes written
    Kernel to glibc: 3 bytes written

Learn it in 4 short steps

About 40m each. Track them with a timer in your plan.

  1. 1
    Describe user mode and kernel mode~40m
  2. 2
    List the kernel's core subsystems~40m
  3. 3
    Explain the role of glibc~40m
  4. 4
    Read the Linux man page sections~40m

Key points

  • User mode cannot execute privileged instructions; trying traps into the kernel.
  • Man section 2 documents system calls, section 3 library functions.
  • A crash in user space kills one process; a kernel bug can crash the whole machine.
  • Some library functions such as printf use system calls internally; others such as strlen never do.

Key terms

Tap a card to flip it. 0/5 known

Examples

bash
man 2 write      # system call
man 3 printf     # C library function
man 7 signal     # overview page
uname -r         # running kernel version
ldd /bin/ls      # shared libraries, including libc

Find documentation for each layer.

Watching a program cross the boundary

The time command splits a program's CPU time into user time and sys time. User time is spent running your own code and libraries. Sys time is spent inside the kernel doing work for you. A program that only does maths has almost no sys time. A program that writes a million tiny lines with raw write calls spends lots of time in the kernel.

bash
time python3 -c "sum(range(10**8))"
# real 1.9s  user 1.9s  sys 0.0s   -> pure computation
time dd if=/dev/zero of=/dev/null bs=1 count=2000000
# real 1.1s  user 0.4s  sys 0.7s   -> millions of 1-byte syscalls
time dd if=/dev/zero of=/dev/null bs=1M count=2
# real 0.0s                         -> same data, two big syscalls

Proving that user code cannot touch kernel memory

On x86-64 Linux, the top half of the address space belongs to the kernel. Here we try to read an address in that range. The page is marked supervisor-only, so the CPU raises a fault, the kernel sees that user code caused it, and sends SIGSEGV. The program dies, but the system is fine: that is the protection working.

c
#include <stdint.h>
#include <stdio.h>

int main(void) {
  volatile uint64_t *p = (uint64_t *)0xffffffff81000000ULL; /* kernel half */
  printf("about to read %p\n", (void *)p);
  fflush(stdout);
  uint64_t v = *p; /* SIGSEGV: Segmentation fault */
  printf("never printed %llu\n", (unsigned long long)v);
  return 0;
}

Quick reference

  • uname -r prints the running kernel version
  • lsmod lists loaded kernel modules
  • dmesg shows kernel log messages (may need sudo)
  • time cmd: user = your code, sys = kernel work for you
  • man 2 = system calls, man 3 = library functions
  • ldd /bin/ls shows libc.so.6 and the dynamic loader
  • /proc/PID/maps shows the [vdso] page mapped into a process

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1A program divides by zero. How does control reach the kernel?

Question 2

2Which of these never enters the kernel?

Question 3

3Why can a buggy device driver crash the whole machine?

Question 4

4What does the vDSO speed up?

0/4 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    Find which section of the manual documents read, printf and proc, and say what each section number means.

  2. Exercise 2medium

    Run time on cat bigfile > /dev/null and on md5sum bigfile. Explain why one has more sys time and the other more user time.

  3. Exercise 3hard

    Look at cat /proc/self/maps and identify the regions belonging to the program, libc, the stack and the vDSO. Explain why the kernel itself is not listed.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

Is the kernel a process?
Not exactly. The kernel is code that runs when a process makes a system call or when an interrupt happens. It also has its own kernel threads, which you see in ps as names in square brackets like [kworker/0:1].
Why is a system call slower than a normal function call?
The CPU has to switch privilege level, save registers and, on many systems, apply security mitigations on entry and exit. That costs around a hundred nanoseconds or more, versus a few nanoseconds for a plain function call.
Can I write my own kernel code?
Yes, as a loadable kernel module. Practise in a virtual machine, because a bug there can freeze or crash the whole system.

Go deeper

ShareXLinkedInWhatsApp