377 words
2 minutes
Trap_mechanism
2026-10-05

link#


trap#

  • In CPU and Operating Systems (OS), a trap is an event that causes the CPU to stop its normal instruction execution and transfer control to a special handler, usually provided by the operating system (OS kernel).

  • In simple terms:

  • A trap is a mechanism that transfers CPU execution to the OS kernel to handle a particular event or condition.

1. What does the sentence mean?#

Original:

It is worth noting that computers have traps other than the instruction for executing a system call.

Meaning:

Computers have several mechanisms that transfer control to the OS kernel, not just the special CPU instruction used to request a system call.

For example, a system call is only one reason the CPU might enter the kernel.

EventMeaningExample
System callProgram requests an OS servicewrite(), read()
Divide errorCPU detects division by zero10 / 0
Page faultMemory access requires OS handlingAccessing an unmapped virtual address
BreakpointExecution reaches a debugging breakpointx86 int3
Invalid opcodeCPU encounters an unsupported instructionInvalid machine instruction
General protection faultCPU detects a protection violationInvalid privileged operation

Strictly speaking, CPU architecture distinguishes exceptions, interrupts, and traps. The sentence uses traps in a broader sense.

3. How does a trap work?#

Consider a program accessing invalid memory.

        User Program
             |
             v
    Execute CPU instruction
             |
             v
    Access virtual memory
             |
             v
      Page fault occurs
             |
             v
    CPU transfers control
       to OS kernel
             |
             v
     Page Fault Handler
             |
       +-----+------+
       |            |
       v            v
   Resolve fault   Terminate
       |           process
       v
   Resume program

The CPU uses architecture-specific mechanisms to transfer execution to the appropriate handler.

4. x86-64 assembly examples#

Here are two instructions that can cause transitions into an OS handler:

System call:

mov rax, 1       ; SYS_write
mov rdi, 1       ; stdout
syscall          ; Enter kernel

Breakpoint:

int3             ; Generate breakpoint exception

Both transfer CPU execution away from the normal user-program instruction stream, but they have different purposes.

5. Important terminology#

TermExplanation
TrapOften a synchronous event transferring control to a handler
ExceptionAn event caused by instruction execution
InterruptAn event typically caused by external hardware
System callA controlled request for an OS service
HandlerKernel code responsible for processing an event

Remember this distinction:

  • System call: The program intentionally asks the OS to do something.
  • Exception: The CPU detects a condition during instruction execution.
  • Interrupt: Hardware signals that something needs attention.

For OS development, understanding these mechanisms is essential because they explain how the CPU transitions between user mode and kernel mode.

Trap_mechanism
https://younghakim7.github.io/blog/posts/trap_mechanism/
Author
YoungHa
Published at
2026-10-05