Menu

Earn Premium with Referrals

Invite your friends and earn Premium rewards through our referral program.

See how it works and start inviting friends.

Advanced CPU Modes & Swap Space
OS

Advanced CPU Modes & Swap Space

Dive deeper into Kernel mode, User mode, and how systems manage extra memory using Swap space.

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:

LevelNameWhat can it do?
Ring 0Kernel modeExecute any instruction, access any memory, control hardware
Ring 3User modeRestricted 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, out on 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:

  1. Saves the user-mode state (return address, stack pointer)
  2. Switches the mode bit to 0 (kernel)
  3. 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.

PropertiesRAMSwap
Speed~50ns~5ms (100,000x slower)
VolatilityVolatilePersistent
Cost per GB~$10~$0.10
PurposeRunning codeOverflow 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.

My Private Notes

Notes are auto-saved locally to this device.