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.
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.
- 1Applications
bash, nginx, your C program.
- 2C library (glibc)
printf, malloc and the system call wrappers.
- 3System call interface
The only door into the kernel.
- 4Kernel subsystems
Scheduler, memory manager, VFS, network stack.
- 5Device drivers
Talk to disks, NICs and other devices.
- 6Hardware
CPU, RAM, storage, network.
- Runs your programs and libraries
- Sees only its own virtual memory
- Cannot run privileged instructions
- A crash kills one process
- Runs the kernel and drivers
- Can access all memory and devices
- Handles syscalls, faults and interrupts
- A crash (panic) stops the machine
- 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
- Program to glibc: strlen(s)1strlen(s)
- glibc to Program: Answer, no kernel entry2Answer, no kernel entry
- Program to glibc: printf("a")3printf("a")
- glibc to Program: Text kept in a buffer4Text kept in a buffer
- Program to glibc: printf("b\n") to a terminal5printf("b\n") to a terminal
- glibc to Kernel: write(1, "ab\n", 3): syscall6write(1, "ab\n", 3): syscall
- Kernel to glibc: 3 bytes written73 bytes written
Learn it in 4 short steps
About 40m each. Track them with a timer in your plan.
- 1Describe user mode and kernel mode~40m
- 2List the kernel's core subsystems~40m
- 3Explain the role of glibc~40m
- 4Read 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
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 libcFind 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.
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 syscallsProving 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.
#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.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Find which section of the manual documents read, printf and proc, and say what each section number means.
- 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.
- 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.