Skip to content
Part IA Lent Term

Traps as Software Interrupts

Traps vs interrupts

A trap (or software interrupt) is a synchronous event caused by the currently executing instruction — a system call (syscall), a debug breakpoint (int3), or a fault (division by zero, page fault). An interrupt is an asynchronous event caused by an external hardware signal.

The handling mechanism is nearly identical: both use the Interrupt Descriptor Table (IDT) to vector to a kernel handler. The trap handler runs with interrupts enabled (typically; faults may disable them briefly), so it can block and take locks — unlike the top half of an interrupt handler.

Why traps are called software interrupts

Historically (x86 real mode and early protected mode), system calls were invoked with the int 0x80 instruction — literally “software interrupt 0x80.” The CPU treated it identically to a hardware interrupt: it pushed the flags, code segment, and instruction pointer onto the stack, indexed the IDT, and jumped to the handler.

Modern CPUs use syscall/sysenter instead, which are faster (no stack push, no IDT lookup — the target address is in a model-specific register). But the conceptual parallel remains: both traps and interrupts are control transfers into the kernel triggered by an event, the only difference being the source (software instruction vs hardware signal).

Handling similarity

Both traps and interrupts follow the same pattern:

  1. Hardware saves minimal processor state (PC, SP, flags).
  2. Hardware switches to kernel mode and jumps to a kernel entry point.
  3. Kernel saves the remaining registers.
  4. Kernel dispatches to the appropriate handler (syscall table for traps, ISR for interrupts).
  5. Handler runs.
  6. Kernel restores registers.
  7. Return-from-interrupt instruction restores PC, SP, flags, and mode.

The key practical difference: interrupt handlers (top halves) run with interrupts disabled and must not block. Trap handlers (system calls, fault handlers) run with interrupts enabled and may block, sleep, take page faults of their own (within strict limits — a double fault is unrecoverable).

Tripos context (2023 Q3)

The question asks directly: “Why are traps sometimes referred to as software interrupts?” The answer: they are handled in essentially the same way — the caller puts values in registers and vectors to the relevant kernel entry point. Both are mechanisms to invoke kernel code in response to an event; the only distinction is the source (hardware vs instruction).

Summary

  • Traps (system calls) and interrupts use the same kernel entry mechanism (IDT or MSR).
  • The distinction is synchronous (trap, caused by the current instruction) vs asynchronous (interrupt, caused by hardware).
  • Trap handlers can block; interrupt top halves cannot.
  • int 0x80 (legacy) and syscall (modern) are both trap mechanisms.

Past Paper Questions

2023 Paper 2 Question 3(a): How are interrupts handled in a modern UNIX-like OS? Why are traps sometimes referred to as software interrupts? [4 marks]