Master the fundamental concepts of build a mini kernel through this focused micro-challenge.
Your mini-kernel switches between tasks using the same callee-saved register contract as userspace schedulers, but on kernel stacks allocated per task. Save rsp, switch page tables if processes differ, restore and return into the next task.
Switch steps:
rsp in task structrsp, pop registers, iret or ret depending on modelFor example, task A stack top at 0xC0401000, task B at 0xC0402000; only rsp and CR3 change between them.
The pusha/popa register save-and-restore sequence you're simulating here is exactly what xv6's swtch.S and every real x86 kernel's context switch does to move the CPU from one task's stack to another's. Forgetting to save ESP separately (pusha doesn't cover it) is a classic bug that corrupts a task's entire stack the moment it resumes, wiping out every local variable it had in flight.
Before you call the implementation done, walk failure modes on purpose. Test empty structures, single-element edge cases, maximum concurrency, and errno paths that must not crash the program. OS code usually fails in production when happy-path tests pass but invariants break under contention or memory pressure.
Keep structures small and name fields after kernel counterparts when possible. That lets you read man pages and kernel source side by side while you work. Print observable events during development; remove noisy logs once tests pass reliably.
You will write switch_to in assembly integrated with your scheduler queue. This exercise requires verifying that FPU/SSE state policy is documented (lazy vs eager).
Simulate an x86 context switch in C.
Requirements:
Test:
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
All starter code and reference implementations are available for your local setup.
View on Github