aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNatasha Moongrave <natasha@256phi.eu>2026-03-30 11:07:21 +0200
committerNatasha Moongrave <natasha@256phi.eu>2026-03-30 11:07:21 +0200
commit834c63a77cd17d20bd2d3b994c61290bc14ca6c2 (patch)
tree79497afa63d70ca2c15c423a7072d54fda3ac7b0
parent103d0ab704fe71c7c4c39a17a9e34efd85d24e5d (diff)
created src/memory.rs for helping keep track of physical and virtual memory
-rw-r--r--StrixKernel/src/memory.rs24
1 files changed, 24 insertions, 0 deletions
diff --git a/StrixKernel/src/memory.rs b/StrixKernel/src/memory.rs
new file mode 100644
index 0000000..2493815
--- /dev/null
+++ b/StrixKernel/src/memory.rs
@@ -0,0 +1,24 @@
+use x86_64::{
+ structures::paging::PageTable,
+ VirtAddr,
+};
+
+/// Returns a mutable reference to the active level 4 table.
+///
+/// This function is unsafe because the caller must guarantee that the
+/// complete physical memory is mapped to virtual memory at the passed
+/// `physical_memory_offset`. Also, this function must be only called once
+/// to avoid aliasing `&mut` references (which is undefined behavior).
+pub unsafe fn active_level_4_table(physical_memory_offset: VirtAddr)
+ -> &'static mut PageTable
+{
+ use x86_64::registers::control::Cr3;
+
+ let (level_4_table_frame, _) = Cr3::read();
+
+ let phys = level_4_table_frame.start_address();
+ let virt = physical_memory_offset + phys.as_u64();
+ let page_table_ptr: *mut PageTable = virt.as_mut_ptr();
+
+ unsafe { &mut *page_table_ptr }
+}