mm: PageBox::into_physical_raw/from_physical_raw

This commit is contained in:
Mark Poliakov 2025-01-21 17:05:38 +02:00
parent cfc11c402a
commit 5d27bd6033
3 changed files with 27 additions and 4 deletions

View File

@ -119,6 +119,10 @@ impl<T> PageBox<T, GlobalPhysicalAllocator> {
result.trace_created();
Ok(result)
}
pub unsafe fn from_physical_raw(address: PhysicalAddress) -> PageBox<T> {
PageBox::from_physical_raw_in(address)
}
}
impl<T, A: PhysicalMemoryAllocator<Address = PhysicalAddress>> PageBox<T, A> {
@ -220,6 +224,16 @@ impl<T, A: PhysicalMemoryAllocator<Address = PhysicalAddress>> PageBox<T, A> {
result.trace_created();
Ok(result)
}
pub unsafe fn from_physical_raw_in(address: PhysicalAddress) -> PageBox<T, A> {
let page_count = size_of::<T>().div_ceil(L3_PAGE_SIZE);
let value = address.virtualize() as *mut T;
PageBox {
value,
page_count,
_pd: PhantomData,
}
}
}
impl<T: ?Sized, A: PhysicalMemoryAllocator<Address = PhysicalAddress>> PageBox<T, A> {
@ -228,6 +242,12 @@ impl<T: ?Sized, A: PhysicalMemoryAllocator<Address = PhysicalAddress>> PageBox<T
self.value as _
}
pub fn into_physical_raw(self) -> PhysicalAddress {
let address = unsafe { self.as_physical_address() };
core::mem::forget(self);
address
}
#[inline]
fn trace_created(&self) {
log::trace!(

View File

@ -15,7 +15,7 @@ use libk::{
};
use libk_mm::{
address::{PhysicalAddress, Virtualize},
pointer::PhysicalRef,
PageBox,
};
use crate::{kernel_main, kernel_secondary_main};
@ -80,10 +80,13 @@ unsafe extern "C" fn __rv64_bsp_entry_upper(bsp_hart_id: u64, dtb_physical: Phys
}
unsafe extern "C" fn __rv64_secondary_entry_upper(context: PhysicalAddress) -> ! {
let context = PhysicalRef::<SecondaryContext>::map(context);
let hart_id = {
let context = PageBox::<SecondaryContext>::from_physical_raw(context);
context.hart_id
};
let queue_index = CPU_COUNT.fetch_add(1, Ordering::Acquire);
if let Err(error) = PLATFORM.init_platform(context.hart_id as u32, queue_index, false) {
if let Err(error) = PLATFORM.init_platform(hart_id as u32, queue_index, false) {
log::error!("Secondary hart init error: {error:?}");
ArchitectureImpl::halt();
}

View File

@ -37,7 +37,7 @@ fn start_secondary_hart(hart_id: u64) -> Result<(), Error> {
stack_top,
hart_id,
})?;
let a1 = unsafe { context.as_physical_address() };
let a1 = context.into_physical_raw();
log::info!(
"Start secondary hart {hart_id}: pc={:#x}, a1={:#x}",