Fix CR3 handling in interrupt/syscall paths using per-CPU data#46
Merged
Fix CR3 handling in interrupt/syscall paths using per-CPU data#46
Conversation
The kernel was using a hardcoded CR3 value (0x101000) in assembly code, which became stale after build_master_kernel_pml4() created a new master PML4 at a different address. This caused triple faults during CR3 switches. Changes: - Add kernel_cr3 and saved_process_cr3 fields to PerCpuData struct - Store kernel CR3 at gs:[72] for interrupt/syscall entry to read - Save process CR3 at gs:[80] on entry, restore on exit if no context switch - Update kernel_cr3 in memory::init() after master PML4 is created - Fix SyscallFrame struct field order to match assembly push order - Fix syscall_return_to_userspace to clear next_cr3 BEFORE CR3 switch The fix ensures interrupt handlers and syscall entry can always switch to the correct kernel page table, and restore the process page table on return when no context switch occurred. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
kernel_cr3andsaved_process_cr3fields to PerCpuData structRoot Cause
The kernel was triple faulting because:
per_cpu::init()stored the bootloader's CR3 (0x101000) as kernel_cr3build_master_kernel_pml4()created a new master PML4 at 0x552000Test plan
cargo run -p xtask -- boot-stagespasses 36/40 stages🤖 Generated with Claude Code
Note
Use per-CPU
kernel_cr3/saved_process_cr3for CR3 switching/restoration in interrupts/syscalls, updatekernel_cr3after master PML4, fixSyscallFramelayout, and clearnext_cr3before CR3 switch.gs:[80]; read kernel CR3 fromgs:[72](removes hardcoded0x101000).next_cr3atgs:[64]before switching CR3; if no context switch, restore saved process CR3; ensure properswapgssequencing.kernel/src/per_cpu.rs):PerCpuDatawithkernel_cr3(offset 72) andsaved_process_cr3(offset 80) plus constants; update size/asserts.kernel_cr3from current CR3 ininit(); addget_kernel_cr3/set_kernel_cr3accessors.kernel/src/memory/mod.rs):kernel_cr3.kernel/src/syscall/handler.rs):SyscallFramefields to match push order (r15atRSP+0…raxatRSP+112).Written by Cursor Bugbot for commit 3962add. This will update automatically on new commits. Configure here.