What changes during an x86 context switch

This figure documents the historical i386 source baseline. Current native x86-64/AArch64 qualification is recorded separately; this is not an ELF64 or native SMP implementation diagram.

What changes during an x86 context switch The scheduler selects next, updates its state, quantum and the ring-0 TSS stack, then changes page directories if needed. Assembly saves flags and callee-saved registers on the outgoing stack, stores its ESP, loads the incoming ESP, restores registers and returns on that stack. The incoming path releases the scheduler guard. cluster_selection 1 · Select task cluster_address 2 · Address space cluster_stack 3 · Stack exchange and return choose->state state->tss tss->next_address cr3->switchdir yes cr3->next_stack no switchdir->next_stack save->exchange exchange->restore restore->unlock choose schedule(): choose next hold scheduler guard state next → RUNNING set current + quantum tss Update TSS ring-0 stack next_address Continue at 2 cr3 Page directories differ? switchdir Load next CR3 next_stack Continue at 3 save context_switch: push flags, EBP, EBX, ESI, EDI exchange Save ESP → prev->esp load ESP ← next->esp restore Pop EDI, ESI, EBX, EBP, flags; ret on incoming stack unlock Incoming task releases guard restores own interrupt state
What changes during an x86 context switch The scheduler selects next, updates its state, quantum and the ring-0 TSS stack, then changes page directories if needed. Assembly saves flags and callee-saved registers on the outgoing stack, stores its ESP, loads the incoming ESP, restores registers and returns on that stack. The incoming path releases the scheduler guard. choose->state state->tss tss->cr3 cr3->switchdir yes cr3->save no switchdir->save save->exchange exchange->restore restore->unlock choose schedule(): choose next hold scheduler guard state next → RUNNING set current + quantum tss Update TSS ring-0 stack cr3 Page directories differ? switchdir Load next CR3 save Push flags; EBP, EBX, ESI, EDI onto old stack exchange Save old ESP → prev->esp load ESP ← next->esp restore Pop EDI, ESI, EBX, EBP, flags; ret on new stack unlock Incoming task releases guard restores own interrupt state

Task metadata, TSS and CR3 change before the assembly stack exchange.

Text equivalent and source references
  • The scheduler selects next, updates its state, quantum and the ring-0 TSS stack, then changes page directories if needed.
  • Assembly saves flags and callee-saved registers on the outgoing stack, stores its ESP, loads the incoming ESP, restores registers and returns on that stack. The incoming path releases the scheduler guard.

Low-level control flow. Source paths refer to the atlas baseline; the kernel checkout is private.

  • kernel/core/task.c · schedule
  • arch/x86/context.asm · context_switch