Skip to content
CurriculumLesson 02 ยท Foundations
Beginner~14 min

Kernel Mode, User Mode & System Calls

A user program cannot touch the disk controller directly. It asks the kernel, and the kernel decides whether the request is valid.

In plain English

User programs ask the kernel for help the way a student asks the office: through a controlled window called a system call.

By the end you will be able to

  • Explain why CPUs have user and kernel privilege levels.
  • Trace a system call from user code to the kernel and back.
  • Know what the kernel validates before performing a request.
  • Distinguish a library call from a system call.
01

Why privilege levels exist

If every program could execute privileged instructions, one bug could overwrite another process or reprogram a device. The CPU provides at least two modes. User mode restricts privileged instructions and access to kernel memory. Kernel mode allows them. The mode is a hardware state, not a software convention. A user program cannot simply set a flag to enter kernel mode; it must go through a controlled entry point.

02

The system call path, step by step

A system call has a number and arguments. The user program places the number and arguments in agreed registers or on the stack, then executes a trap instruction. The CPU switches to kernel mode and jumps to the trap handler. The kernel saves the user context, validates the request, performs the service, stores a result, restores the context, and returns to user mode. The trap instruction is the doorway; the kernel decides what happens next.

lesson-02.osOS
/* conceptual path */
user:   r0 = syscall_number
        r1 = argument
        trap

kernel: save user context
        check syscall number
        validate pointer and permissions
        perform operation
        set return value
        restore context

user:   continue with result
03

Library calls versus system calls

Not every function call is a system call. A function such as strlen runs entirely in user space. A function such as read ultimately issues a system call, but the C library may buffer data so that one read call does not mean one kernel entry. The library is a user-space wrapper; the kernel performs the privileged work.

Key takeaways

  • User mode restricts privileged instructions; kernel mode allows them.
  • A system call uses a trap instruction to enter the kernel.
  • The kernel validates arguments before acting.
  • Library functions may wrap system calls and add buffering.

Common pitfalls

  • Thinking a normal function call switches to kernel mode.
  • Assuming the user program can pass any pointer safely.
  • Forgetting that a system call has measurable overhead.

Kernel notes

  • Trace the trap, save, validate, service, restore, and return steps.
  • Give one example of a library call and one of a system call.
  • Explain why validation must happen in the kernel.

Quick check

0 / 2 answered

1. What instruction transfers control from user mode to the kernel?

2. Why must the kernel validate a pointer passed by a user program?