The separation of user mode and kernel mode is the foundation of OS security and stability. It’s enforced by the CPU hardware, not just the OS.
Dual Mode Operation
Modern CPUs support at least two privilege levels:
| Level | Name | What can it do? |
|---|---|---|
| Ring 0 | Kernel mode | Execute any instruction, access any memory, control hardware |
| Ring 3 | User mode | Restricted instruction set, only accesses process’s own memory |
Some systems (x86) have rings 0-3, but most OSes only use 0 and 3.
Privileged Instructions
These can ONLY execute in kernel mode:
- Modifying page table registers
- Disabling/enabling interrupts
- Setting the system timer
- I/O port operations (
in,outon x86) - Switching the mode bit itself
If a user-mode program attempts a privileged instruction, the CPU raises a general protection fault — the OS typically terminates the program.
Mode Switching (System Call Flow)
User mode (application) Kernel mode (OS)
───────────────────────── ──────────────────
read(fd, buf, len) ──[trap]──→ syscall handler
validates arguments
copies data from kernel
return value ←─[return]── switches to user mode
The trap instruction (e.g., syscall or int 0x80 on x86) atomically:
- Saves the user-mode state (return address, stack pointer)
- Switches the mode bit to 0 (kernel)
- Jumps to a predefined handler address in the kernel
Swap Space
When RAM is full, the OS moves pages to a reserved area on disk called swap space. Swapping is slow (disk I/O is orders of magnitude slower than RAM), but it prevents the system from running out of memory entirely.
| Properties | RAM | Swap |
|---|---|---|
| Speed | ~50ns | ~5ms (100,000x slower) |
| Volatility | Volatile | Persistent |
| Cost per GB | ~$10 | ~$0.10 |
| Purpose | Running code | Overflow when RAM is full |
Linux can use a dedicated swap partition or a swap file. The swappiness parameter (0-100) controls how aggressively the kernel swaps.
Q: Why does the OS need dual mode?
A: Without it, any program could read any memory, access any hardware, or corrupt the OS. Dual mode isolates user programs from the kernel and from each other. A crash in user mode doesn’t crash the system.
Q: Give examples of privileged instructions.
A: I/O operations (hardware access), interrupt management (enable/disable), timer configuration, memory management instructions (TLB flush, page table switch), and the mode switch itself.
Q: How does the system transition from user mode to kernel mode?
A: Via a trap (software interrupt). The application executes a syscall instruction. The hardware saves the return address, switches the mode bit to kernel (0), and jumps to the kernel’s interrupt handler.
Q: What is swap space?
A: A reserved area on disk used as an extension of RAM. When physical memory is full, the OS moves inactive pages to swap. It’s slow but prevents out-of-memory crashes. Too much swapping causes thrashing.
Premium Content
Unlock Advanced CPU Modes & Swap Space and all premium lessons with a subscription.
From ₹199.99/year — See plans