What is the difference between user mode and kernel mode?
EasyConceptOS Basics & System Calls
Answer
Modern CPUs run in at least two privilege levels. In kernel mode (supervisor mode), code can execute every instruction and access all memory and hardware; only the OS kernel runs this way. In user mode, application code runs with restricted rights and cannot directly touch hardware or other processes' memory.
- A mode bit in the CPU records the current mode. Privileged instructions, such as direct I/O, changing page tables or disabling interrupts, cause a trap (a forced switch into the kernel) if attempted in user mode.
- User code enters kernel mode only through controlled entry points: system calls, interrupts and exceptions. The kernel switches back to user mode before returning.
- Why it matters: a buggy or malicious program cannot crash the machine or read another program's data. The price is a small time cost for every switch.
Example: printf("hi\n") in C runs in user mode inside the C library, which then calls the write() system call. On x86-64 Linux this executes the syscall instruction: the CPU switches to kernel mode, the kernel passes the bytes to the terminal, and the CPU returns to user mode with the number of bytes written.