libkernel: pg_offset: decouple from VirtualMemory trait

Since the only requirement for a `PageOffsetTranslator` is a constant
offset, pass that in as a generic parameter, allowing it to be decoupled
from the `VirtualMemory` trait.
This commit is contained in:
Matthew Leach
2026-04-30 20:43:51 +01:00
committed by Ashwin Naren
parent 370aae9697
commit 80af2e8852
6 changed files with 15 additions and 20 deletions

View File

@@ -208,9 +208,6 @@ pub trait VirtualMemory: CpuOps + Sized {
/// The address space type used for the kernel.
type KernelAddressSpace: KernAddressSpace;
/// The starting address for the logical mapping of all physical ram.
const PAGE_OFFSET: usize;
/// Obtain a reference to the kernel's address space.
fn kern_address_space() -> &'static SpinLockIrq<Self::KernelAddressSpace, Self>;
}

View File

@@ -1,22 +1,18 @@
//! Page-offset arithmetic helpers.
use super::address_space::VirtualMemory;
use crate::memory::address::{AddressTranslator, TPA, TVA};
use core::marker::PhantomData;
pub(crate) use crate::memory::address::{AddressTranslator, TPA, TVA};
/// Translates between physical and virtual addresses using a fixed page-offset mapping.
pub struct PageOffsetTranslator<VM: VirtualMemory> {
_phantom: PhantomData<VM>,
}
pub struct PageOffsetTranslator<const OFFSET: usize>;
unsafe impl<VM: VirtualMemory> Send for PageOffsetTranslator<VM> {}
unsafe impl<VM: VirtualMemory> Sync for PageOffsetTranslator<VM> {}
unsafe impl<const OFFSET: usize> Send for PageOffsetTranslator<OFFSET> {}
unsafe impl<const OFFSET: usize> Sync for PageOffsetTranslator<OFFSET> {}
impl<T, VM: VirtualMemory> AddressTranslator<T> for PageOffsetTranslator<VM> {
impl<T, const OFFSET: usize> AddressTranslator<T> for PageOffsetTranslator<OFFSET> {
fn virt_to_phys(va: TVA<T>) -> TPA<T> {
let mut v = va.value();
v -= VM::PAGE_OFFSET;
v -= OFFSET;
TPA::from_value(v)
}
@@ -24,7 +20,7 @@ impl<T, VM: VirtualMemory> AddressTranslator<T> for PageOffsetTranslator<VM> {
fn phys_to_virt(pa: TPA<T>) -> TVA<T> {
let mut v = pa.value();
v += VM::PAGE_OFFSET;
v += OFFSET;
TVA::from_value(v)
}