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.
This commit is contained in:
Matthew Leach
2025-12-17 22:09:46 +00:00
committed by Ashwin Naren
parent 264d603044
commit bebee6d8f5
3 changed files with 18 additions and 16 deletions

View File

@@ -164,10 +164,6 @@ impl<K: MemKind, T> Address<K, T> {
pub fn is_null(self) -> bool {
self.inner == 0
}
pub fn to_pfn(&self) -> PageFrame {
PageFrame::from_pfn(self.inner >> PAGE_SHIFT)
}
}
impl<K: MemKind, T: Sized> Address<K, T> {
@@ -294,6 +290,10 @@ impl PA {
pub fn cast<T>(self) -> TPA<T> {
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.

View File

@@ -79,8 +79,10 @@ impl<A: CpuOps, G: PageAllocGetter<A>, T: AddressTranslator<()>> ClaimedPage<A,
/// Takes ownership of the page at pfn.
///
/// SAFETY: Ensure that the calling context does indeed own this page.
/// Otherwise, the page may be free'd when it's owned by another context.
/// # Safety
///
/// Ensure that the calling context does indeed own this page. Otherwise,
/// the page may be free'd when it's owned by another context.
pub unsafe fn from_pfn(pfn: PageFrame) -> Self {
Self(
unsafe {

View File

@@ -372,6 +372,16 @@ impl<T: MemKind> MemoryRegion<T> {
}
})
}
}
/// A memory region in physical address space.
pub type PhysMemoryRegion = MemoryRegion<Physical>;
impl PhysMemoryRegion {
/// Map the physical region to virtual space using a translator.
pub fn map_via<T: AddressTranslator<()>>(self) -> VirtMemoryRegion {
VirtMemoryRegion::new(self.address.to_va::<T>(), self.size)
}
pub fn iter_pfns(self) -> impl Iterator<Item = PageFrame> {
let mut count = 0;
@@ -391,16 +401,6 @@ impl<T: MemKind> MemoryRegion<T> {
}
}
/// A memory region in physical address space.
pub type PhysMemoryRegion = MemoryRegion<Physical>;
impl PhysMemoryRegion {
/// Map the physical region to virtual space using a translator.
pub fn map_via<T: AddressTranslator<()>>(self) -> VirtMemoryRegion {
VirtMemoryRegion::new(self.address.to_va::<T>(), self.size)
}
}
/// A memory region in virtual address space.
pub type VirtMemoryRegion = MemoryRegion<Virtual>;