From bebee6d8f540f6996d9ac5022b992b61cf01028d Mon Sep 17 00:00:00 2001 From: Matthew Leach Date: Wed, 17 Dec 2025 22:09:46 +0000 Subject: [PATCH] libkernel: address: ensure `to_pfn` can only be called on PA calculating a PFN from a VA makes no sense since it could be mapped to any address. Therefore, ensure that it can *only* be called on PAs. --- libkernel/src/memory/address.rs | 8 ++++---- libkernel/src/memory/page.rs | 6 ++++-- libkernel/src/memory/region.rs | 20 ++++++++++---------- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/libkernel/src/memory/address.rs b/libkernel/src/memory/address.rs index 828ecf1..449ae3e 100644 --- a/libkernel/src/memory/address.rs +++ b/libkernel/src/memory/address.rs @@ -164,10 +164,6 @@ impl Address { pub fn is_null(self) -> bool { self.inner == 0 } - - pub fn to_pfn(&self) -> PageFrame { - PageFrame::from_pfn(self.inner >> PAGE_SHIFT) - } } impl Address { @@ -294,6 +290,10 @@ impl PA { pub fn cast(self) -> TPA { TPA::from_value(self.value()) } + + pub fn to_pfn(&self) -> PageFrame { + PageFrame::from_pfn(self.inner >> PAGE_SHIFT) + } } /// Trait for translating between physical and virtual addresses. diff --git a/libkernel/src/memory/page.rs b/libkernel/src/memory/page.rs index 9e2298f..c320689 100644 --- a/libkernel/src/memory/page.rs +++ b/libkernel/src/memory/page.rs @@ -79,8 +79,10 @@ impl, T: AddressTranslator<()>> ClaimedPage Self { Self( unsafe { diff --git a/libkernel/src/memory/region.rs b/libkernel/src/memory/region.rs index cc009c2..8b83760 100644 --- a/libkernel/src/memory/region.rs +++ b/libkernel/src/memory/region.rs @@ -372,6 +372,16 @@ impl MemoryRegion { } }) } +} + +/// A memory region in physical address space. +pub type PhysMemoryRegion = MemoryRegion; + +impl PhysMemoryRegion { + /// Map the physical region to virtual space using a translator. + pub fn map_via>(self) -> VirtMemoryRegion { + VirtMemoryRegion::new(self.address.to_va::(), self.size) + } pub fn iter_pfns(self) -> impl Iterator { let mut count = 0; @@ -391,16 +401,6 @@ impl MemoryRegion { } } -/// A memory region in physical address space. -pub type PhysMemoryRegion = MemoryRegion; - -impl PhysMemoryRegion { - /// Map the physical region to virtual space using a translator. - pub fn map_via>(self) -> VirtMemoryRegion { - VirtMemoryRegion::new(self.address.to_va::(), self.size) - } -} - /// A memory region in virtual address space. pub type VirtMemoryRegion = MemoryRegion;