From ee3fa55ce8e28c7314ee66e2ef971f867c8ed5e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kr=C3=B6ning?= Date: Fri, 20 Dec 2024 13:48:31 +0100 Subject: [PATCH] refactor(gdt): migrate CR3 access to `x86_64` crate --- src/arch/x86_64/kernel/gdt.rs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/arch/x86_64/kernel/gdt.rs b/src/arch/x86_64/kernel/gdt.rs index a76d882e7d..9fe3ce841c 100644 --- a/src/arch/x86_64/kernel/gdt.rs +++ b/src/arch/x86_64/kernel/gdt.rs @@ -74,10 +74,21 @@ pub fn add_current_core() { pub extern "C" fn set_current_kernel_stack() { #[cfg(feature = "common-os")] - unsafe { + { + use x86_64::registers::control::Cr3; + use x86_64::structures::paging::PhysFrame; + use x86_64::PhysAddr; + let root = crate::scheduler::get_root_page_table(); - if root != x86::controlregs::cr3().try_into().unwrap() { - x86::controlregs::cr3_write(root.try_into().unwrap()); + let new_frame = + PhysFrame::from_start_address(PhysAddr::new(root.try_into().unwrap())).unwrap(); + + let (current_frame, val) = Cr3::read_raw(); + + if current_frame != new_frame { + unsafe { + Cr3::write_raw(new_frame, val); + } } }