Skip to content

feat(cpu): switch between ring-0 contexts, saving their extended state with the strongest XSAVE the processor offers - #806

Merged
MasterLaplace merged 12 commits into
feat/85-known-extended-statefrom
feat/85-context-switch
Nov 9, 2026
Merged

MasterLaplace merged 12 commits into
feat/85-known-extended-statefrom
feat/85-context-switch

Conversation

@MasterLaplace

@MasterLaplace MasterLaplace commented Nov 8, 2026 •

Copy link
Copy Markdown
Owner

What

cpu_context_switch(from, to) switches between two ring-0 contexts on one processor. It saves EBP, EBX, ESI, EDI, the stack pointer and the extended state.

  • The policy for SIMD in ring 0, written on cpu_context_switch: every switch saves eagerly, never lazily through CR0.TS (LazyFP). A context that never touched its vector registers costs only the header.
  • Each context names its save. extended_state_preferred_save takes the SDM's order: XSAVES, then XSAVEOPT, then XSAVE. FXSAVE is offered only while XCR0 enables nothing past SSE.
  • cpu_context_prepare starts a context at its entry, from the initial state, on a 16-byte-aligned stack. An entry that returns is reported on COM1, then its processor halts.
  • cpu_context_adopt makes the running code a context. It ends with an XRSTOR of empty mask, so any area may be adopted, even the one the processor last restored from.
  • tools/instructions.sh --cpu names QEMU's processor model, so a budget can count the XSAVE path.
  • First caller outside the tests: the tasks of Ring-0 tasks: a control block, task states and blocking waits #259.

Specification: said, says, because

#85's criterion said "a test preempts a task". It now says a context finds its registers after another context ran, because no task exists yet; preemption moves to #259.

Verify

./build.sh --server, then qemu-system-i386 -machine q35 -m 1G -smp 2 -kernel kernel/lpl.kernel -append lpl.test=context_switch.*, with -cpu max or -accel kvm -cpu host:

  • Red before: registers lost and inherited, and under KVM the area adopted again kept its initial state.
  • Server: q35 868/0, -cpu max 871/0 with 2 and 4 processors, KVM 871/0, pc 862/0, KVM pc with 4 processors 864/0. Satellite: -cpu max 721/0, KVM pc 714/0.
  • Under KVM, integer contexts write no SSE or AVX byte with XSAVEOPT or XSAVES.
  • 8 mutants, each failing under TCG or KVM: XSAVE for XSAVEOPT, no XRSTOR, FXSAVE only, no XCOMP_BV, no MXCSR, a misaligned frame, FXSAVE offered with AVX, no empty restore.
  • context-switch.debug.budget: 58,852 instructions for 1,000 switches. Server tick unchanged.

Not in it

Stacked on #805. Refs #85

@MasterLaplace MasterLaplace added the type:feature A new capability label Nov 8, 2026
@github-project-automation github-project-automation Bot moved this to To review in Laplace Nov 8, 2026
@MasterLaplace MasterLaplace self-assigned this Nov 8, 2026
@MasterLaplace
MasterLaplace added this pull request to stack #807 November 8, 2026 19:50
@MasterLaplace
MasterLaplace merged commit 6ea8d41 into main Nov 9, 2026
13 checks passed
@MasterLaplace
MasterLaplace deleted the feat/85-context-switch branch November 9, 2026 00:58
@github-project-automation github-project-automation Bot moved this from To review to Done in Laplace Nov 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type:feature A new capability

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant