kernel: fix unsafenode impl

This commit is contained in:
janis 2026-08-03 01:26:21 +02:00
parent b069ee78a8
commit 005a1a1491
Signed by: janis
SSH key fingerprint: SHA256:bB1qbbqmDXZNT0KKD5c2Dfjg53JGhj7B3CFcLIzSqq8
2 changed files with 30 additions and 9 deletions

View file

@ -21,9 +21,18 @@ impl PhyAddr {
virt.into().0.checked_sub(hhdm_base).map(PhyAddr)
}
pub fn into_hhdm_virt(&self) -> VirtAddr {
pub fn from_page_index(page_index: usize) -> Self {
PhyAddr(page_index as u64 * PAGE_SIZE as u64)
}
pub fn as_hhdm_virt(&self) -> VirtAddr {
VirtAddr(self.0 + unsafe { crate::memory::HHDM_BASE.get().unwrap_unchecked() })
}
pub fn into_hhdm_virt(self) -> VirtAddr {
VirtAddr(self.0 + unsafe { crate::memory::HHDM_BASE.get().unwrap_unchecked() })
}
pub fn page_add(&self, page_count: usize) -> PhyAddr {
PhyAddr(self.0 + (page_count as u64 * PAGE_SIZE as u64))
}
@ -88,6 +97,9 @@ impl VirtAddr {
pub fn as_mut<T>(&self) -> *mut T {
core::ptr::with_exposed_provenance_mut(self.0 as usize)
}
pub fn into_nonnull<T>(self) -> Option<NonNull<T>> {
NonZeroUsize::new(self.0 as usize).map(NonNull::with_exposed_provenance)
}
}
pub struct PageChunk {
@ -129,7 +141,7 @@ impl PageHeader {
fn new_from_page_idx_and_count(page_idx: usize, count: usize) -> NonNull<Self> {
let phy = PhyAddr(page_idx as u64 * PAGE_SIZE as u64);
let virt = phy.into_hhdm_virt();
let virt = phy.as_hhdm_virt();
let ptr = virt.as_mut::<Self>();
unsafe {
@ -760,15 +772,24 @@ unsafe impl rbtree::UnsafeNode for PhysicalPageNode {
type Key = PhysicalPageNodeKey;
fn left(&self) -> Option<NonNull<Self>> {
NonZeroUsize::new((self.left_bits() << 12) as usize).map(NonNull::with_exposed_provenance)
NonZeroUsize::new(self.left_bits() as usize)
.map(|n| PhyAddr::from_page_index(n.get()))
.map(PhyAddr::into_hhdm_virt)
.and_then(VirtAddr::into_nonnull)
}
fn right(&self) -> Option<NonNull<Self>> {
NonZeroUsize::new((self.right_bits() << 12) as usize).map(NonNull::with_exposed_provenance)
NonZeroUsize::new(self.right_bits() as usize)
.map(|n| PhyAddr::from_page_index(n.get()))
.map(PhyAddr::into_hhdm_virt)
.and_then(VirtAddr::into_nonnull)
}
fn parent(&self) -> Option<NonNull<Self>> {
NonZeroUsize::new((self.parent_bits() << 12) as usize).map(NonNull::with_exposed_provenance)
NonZeroUsize::new(self.parent_bits() as usize)
.map(|n| PhyAddr::from_page_index(n.get()))
.map(PhyAddr::into_hhdm_virt)
.and_then(VirtAddr::into_nonnull)
}
fn key(&self) -> &Self::Key {

View file

@ -128,7 +128,7 @@ impl PageTableEntry {
unsafe {
Some(
self.phy()
.into_hhdm_virt()
.as_hhdm_virt()
.as_ptr::<PageTable>()
.as_ref()
.unwrap_unchecked(),
@ -143,7 +143,7 @@ impl PageTableEntry {
unsafe {
self.phy()
.into_hhdm_virt()
.as_hhdm_virt()
.as_ptr::<PageTable>()
.as_ref()
.unwrap_unchecked()
@ -192,7 +192,7 @@ pub fn get_physical_addr(virt: VirtAddr) -> Option<PhyAddr> {
let frame = cr3.phy();
let page_table = unsafe {
frame
.into_hhdm_virt()
.as_hhdm_virt()
.as_ptr::<PageTable>()
.as_ref()
.unwrap_unchecked()
@ -235,7 +235,7 @@ pub fn get_physical_addr(virt: VirtAddr) -> Option<PhyAddr> {
return None;
}
let phy_addr = pt_entry.phy().into_hhdm_virt().0 + virt.offset_4k() as u64;
let phy_addr = pt_entry.phy().as_hhdm_virt().0 + virt.offset_4k() as u64;
Some(PhyAddr(phy_addr))
}