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)
}

View File

@@ -78,8 +78,6 @@ impl VirtualMemory for Aarch64 {
type ProcessAddressSpace = Arm64ProcessAddressSpace;
type KernelAddressSpace = Arm64KernelAddressSpace;
const PAGE_OFFSET: usize = PAGE_OFFSET;
fn kern_address_space() -> &'static SpinLock<Self::KernelAddressSpace> {
KERN_ADDR_SPC.get().unwrap()
}
@@ -89,6 +87,8 @@ impl Arch for Aarch64 {
type UserContext = ExceptionState;
type PTraceGpRegs = Arm64PtraceGPRegs;
const PAGE_OFFSET: usize = PAGE_OFFSET;
fn new_user_context(entry_point: VA, stack_top: VA) -> Self::UserContext {
ExceptionState {
x: [0; 31],

View File

@@ -37,6 +37,9 @@ pub trait Arch: CpuOps + VirtualMemory {
/// The type for GP regs copied via `PTRACE_GETREGSET`.
type PTraceGpRegs: UserCopyable + for<'a> From<&'a Self::UserContext>;
/// The starting address for the logical mapping of all physical ram.
const PAGE_OFFSET: usize;
fn name() -> &'static str;
fn cpu_count() -> usize;

View File

@@ -1,9 +1,8 @@
use crate::arch::ArchImpl;
use crate::arch::{Arch, ArchImpl};
use crate::memory::PageOffsetTranslator;
use core::ptr::NonNull;
use libkernel::memory::PAGE_SIZE;
use libkernel::memory::address::{PA, TPA};
use libkernel::memory::proc_vm::address_space::VirtualMemory;
use libkernel::memory::region::PhysMemoryRegion;
use log::trace;
use virtio_drivers::{BufferDirection, Hal, PhysAddr};

View File

@@ -1,5 +1,5 @@
use crate::{
arch::ArchImpl,
arch::{Arch, ArchImpl},
sync::{OnceLock, SpinLock},
};
use libkernel::memory::{
@@ -19,7 +19,7 @@ pub mod process_vm;
pub mod uaccess;
pub type PageOffsetTranslator =
libkernel::memory::proc_vm::pg_offset::PageOffsetTranslator<ArchImpl>;
libkernel::memory::proc_vm::pg_offset::PageOffsetTranslator<{ ArchImpl::PAGE_OFFSET }>;
// Initial memory allocator. Used for initial memory setup.
const STATIC_REGION_COUNT: usize = 128;