Skip to content

Commit

Permalink
mem: combine PageFrameAllocate and PageFrameFree into Alloc
Browse files Browse the repository at this point in the history
  • Loading branch information
Qix- committed Sep 11, 2024
1 parent 741cc16 commit e210393
Show file tree
Hide file tree
Showing 16 changed files with 62 additions and 104 deletions.
2 changes: 1 addition & 1 deletion oro-arch-aarch64/src/boot/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use oro_boot_protocol::{memory_map::MemoryMapKind, MemoryMapEntry, MemoryMapEntr
use oro_debug::{dbg, dbg_warn};
use oro_macro::assert;
use oro_mem::{
pfa::{alloc::PageFrameFree, filo::FiloPageFrameAllocator},
pfa::{alloc::Alloc, filo::FiloPageFrameAllocator},
translate::{OffsetTranslator, Translator},
};

Expand Down
6 changes: 3 additions & 3 deletions oro-arch-aarch64/src/boot/secondary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use oro_dtb::{FdtHeader, FdtPathFilter, FdtToken};
use oro_macro::{asm_buffer, assert};
use oro_mem::{
mapper::{AddressSegment, AddressSpace, MapError},
pfa::alloc::PageFrameFree,
pfa::alloc::Alloc,
translate::{OffsetTranslator, Translator},
};
use oro_type::Be;
Expand All @@ -26,7 +26,7 @@ use oro_type::Be;
/// This function is inherently unsafe and must only be called
/// once at kernel boot by the bootstrap processor (primary core).
pub unsafe fn boot_secondaries(
pfa: &mut impl PageFrameFree,
pfa: &mut impl Alloc,
pat: &OffsetTranslator,
stack_pages: usize,
) -> usize {
Expand Down Expand Up @@ -317,7 +317,7 @@ const SECONDARY_BOOT_STUB: &[u8] = &asm_buffer! {

/// Attempts to boot a single secondary core.
unsafe fn boot_secondary(
pfa: &mut impl PageFrameFree,
pfa: &mut impl Alloc,
pat: &OffsetTranslator,
psci: PsciMethod,
cpu_id: u64,
Expand Down
10 changes: 5 additions & 5 deletions oro-arch-aarch64/src/mem/address_space.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
},
reg::tcr_el1::TcrEl1,
};
use oro_mem::{mapper::AddressSpace, pfa::alloc::PageFrameAllocate, translate::Translator};
use oro_mem::{mapper::AddressSpace, pfa::alloc::Alloc, translate::Translator};

/// A lightweight handle to an address space.
pub struct AddressSpaceHandle {
Expand Down Expand Up @@ -180,7 +180,7 @@ impl AddressSpaceLayout {
virt_start: usize,
) -> Option<AddressSpaceHandle>
where
A: PageFrameAllocate,
A: Alloc,
P: Translator,
{
let base_phys = alloc.allocate()?;
Expand All @@ -205,7 +205,7 @@ impl AddressSpaceLayout {
translator: &P,
) -> Option<<Self as AddressSpace>::SupervisorHandle>
where
A: PageFrameAllocate,
A: Alloc,
P: Translator,
{
unsafe { Self::new_supervisor_space_with_start(alloc, translator, 0) }
Expand Down Expand Up @@ -265,7 +265,7 @@ unsafe impl AddressSpace for AddressSpaceLayout {

fn new_supervisor_space<A, P>(alloc: &mut A, translator: &P) -> Option<Self::SupervisorHandle>
where
A: PageFrameAllocate,
A: Alloc,
P: Translator,
{
// NOTE(qix-): We currently specify that the kernel uses `TCR_EL1.TnSZ=16`,
Expand All @@ -281,7 +281,7 @@ unsafe impl AddressSpace for AddressSpaceLayout {
translator: &P,
) -> Option<Self::SupervisorHandle>
where
A: PageFrameAllocate,
A: Alloc,
P: Translator,
{
let base_phys = alloc.allocate()?;
Expand Down
16 changes: 8 additions & 8 deletions oro-arch-aarch64/src/mem/segment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use core::panic;
use oro_macro::unlikely;
use oro_mem::{
mapper::{AddressSegment, MapError, UnmapError},
pfa::alloc::{PageFrameAllocate, PageFrameFree},
pfa::alloc::Alloc,
translate::Translator,
};

Expand Down Expand Up @@ -67,7 +67,7 @@ impl Segment {
virt: usize,
) -> Result<&'a mut PageTableEntry, MapError>
where
A: PageFrameAllocate,
A: Alloc,
P: Translator,
{
let virt = virt
Expand Down Expand Up @@ -189,7 +189,7 @@ impl Segment {
virt: usize,
) -> Result<Option<u64>, UnmapError>
where
A: PageFrameAllocate + PageFrameFree,
A: Alloc,
P: Translator,
{
let virt = virt
Expand Down Expand Up @@ -315,7 +315,7 @@ unsafe impl AddressSegment<AddressSpaceHandle> for &'static Segment {
translator: &P,
) -> Result<(), MapError>
where
A: PageFrameAllocate + PageFrameFree,
A: Alloc,
P: Translator,
{
let top_level = unsafe { &mut *translator.translate_mut::<PageTable>(space.base_phys) };
Expand Down Expand Up @@ -346,7 +346,7 @@ unsafe impl AddressSegment<AddressSpaceHandle> for &'static Segment {
phys: u64,
) -> Result<(), MapError>
where
A: PageFrameAllocate + PageFrameFree,
A: Alloc,
P: Translator,
{
// NOTE(qix-): The mapper doesn't actually free anything,
Expand All @@ -363,7 +363,7 @@ unsafe impl AddressSegment<AddressSpaceHandle> for &'static Segment {
phys: u64,
) -> Result<(), MapError>
where
A: PageFrameAllocate,
A: Alloc,
P: Translator,
{
let l3_entry = self.entry(space, alloc, translator, virt)?;
Expand Down Expand Up @@ -397,7 +397,7 @@ unsafe impl AddressSegment<AddressSpaceHandle> for &'static Segment {
virt: usize,
) -> Result<u64, UnmapError>
where
A: PageFrameAllocate + PageFrameFree,
A: Alloc,
P: Translator,
{
let phys = unsafe { self.try_unmap(space, alloc, translator, virt)? };
Expand All @@ -414,7 +414,7 @@ unsafe impl AddressSegment<AddressSpaceHandle> for &'static Segment {
phys: u64,
) -> Result<Option<u64>, MapError>
where
A: PageFrameAllocate + PageFrameFree,
A: Alloc,
P: Translator,
{
let l3_entry = self.entry(space, alloc, translator, virt)?;
Expand Down
2 changes: 1 addition & 1 deletion oro-arch-x86_64/src/boot/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use oro_debug::{dbg, dbg_warn};
use oro_macro::assert;
use oro_mem::{
mapper::AddressSegment as _,
pfa::{alloc::PageFrameFree, filo::FiloPageFrameAllocator},
pfa::{alloc::Alloc, filo::FiloPageFrameAllocator},
translate::{OffsetTranslator, Translator},
};

Expand Down
4 changes: 2 additions & 2 deletions oro-arch-x86_64/src/boot/secondary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use oro_debug::{dbg, dbg_err};
use oro_macro::{asm_buffer, assert};
use oro_mem::{
mapper::{AddressSegment, AddressSpace, MapError, UnmapError},
pfa::alloc::{PageFrameAllocate, PageFrameFree},
pfa::alloc::Alloc,
translate::{OffsetTranslator, Translator},
};

Expand Down Expand Up @@ -46,7 +46,7 @@ pub enum BootError {
///
/// Caller must ensure these pages are mapped (via the PAT) and accessible.
#[expect(clippy::missing_docs_in_private_items)]
pub unsafe fn boot_secondary<A: PageFrameAllocate + PageFrameFree>(
pub unsafe fn boot_secondary<A: Alloc>(
primary_handle: &AddressSpaceHandle,
pfa: &mut A,
pat: &OffsetTranslator,
Expand Down
12 changes: 4 additions & 8 deletions oro-arch-x86_64/src/mem/address_space.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
asm::cr3,
mem::{paging::PageTableEntry, segment::AddressSegment},
};
use oro_mem::{mapper::AddressSpace, pfa::alloc::PageFrameAllocate, translate::Translator};
use oro_mem::{mapper::AddressSpace, pfa::alloc::Alloc, translate::Translator};

/// A handle to an address space for the x86_64 architecture.
///
Expand Down Expand Up @@ -174,7 +174,7 @@ unsafe impl AddressSpace for AddressSpaceLayout {

fn new_supervisor_space<A, P>(alloc: &mut A, translator: &P) -> Option<Self::SupervisorHandle>
where
A: PageFrameAllocate,
A: Alloc,
P: Translator,
{
let base_phys = alloc.allocate()?;
Expand All @@ -189,15 +189,11 @@ unsafe impl AddressSpace for AddressSpaceLayout {
})
}

fn duplicate_supervisor_space_shallow<A, P>(
fn duplicate_supervisor_space_shallow<A: Alloc, P: Translator>(
space: &Self::SupervisorHandle,
alloc: &mut A,
translator: &P,
) -> Option<Self::SupervisorHandle>
where
A: PageFrameAllocate,
P: Translator,
{
) -> Option<Self::SupervisorHandle> {
let base_phys = alloc.allocate()?;

unsafe {
Expand Down
18 changes: 9 additions & 9 deletions oro-arch-x86_64/src/mem/segment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::mem::{paging::PageTableEntry, paging_level::PagingLevel};
use oro_macro::unlikely;
use oro_mem::{
mapper::{AddressSegment as Segment, MapError, UnmapError},
pfa::alloc::{PageFrameAllocate, PageFrameFree},
pfa::alloc::Alloc,
translate::Translator,
};

Expand Down Expand Up @@ -71,7 +71,7 @@ impl AddressSegment {
virt: usize,
) -> Result<&'a mut PageTableEntry, MapError>
where
A: PageFrameAllocate,
A: Alloc,
P: Translator,
{
if unlikely!(virt & 0xFFF != 0) {
Expand Down Expand Up @@ -142,7 +142,7 @@ impl AddressSegment {
virt: usize,
) -> Result<Option<u64>, UnmapError>
where
A: PageFrameAllocate + PageFrameFree,
A: Alloc,
P: Translator,
{
if unlikely!(virt & 0xFFF != 0) {
Expand Down Expand Up @@ -236,7 +236,7 @@ impl AddressSegment {
virt: usize,
) -> Result<Option<u64>, UnmapError>
where
A: PageFrameAllocate + PageFrameFree,
A: Alloc,
P: Translator,
{
if unlikely!(virt & 0xFFF != 0) {
Expand Down Expand Up @@ -381,7 +381,7 @@ unsafe impl Segment<AddressSpaceHandle> for &'static AddressSegment {
translator: &P,
) -> Result<(), MapError>
where
A: PageFrameAllocate + PageFrameFree,
A: Alloc,
P: Translator,
{
let top_level = unsafe { &mut *translator.translate_mut::<PageTable>(space.base_phys()) };
Expand Down Expand Up @@ -412,7 +412,7 @@ unsafe impl Segment<AddressSpaceHandle> for &'static AddressSegment {
phys: u64,
) -> Result<(), MapError>
where
A: PageFrameAllocate + PageFrameFree,
A: Alloc,
P: Translator,
{
// NOTE(qix-): The current implementation of `entry()` doesn't
Expand All @@ -429,7 +429,7 @@ unsafe impl Segment<AddressSpaceHandle> for &'static AddressSegment {
phys: u64,
) -> Result<(), MapError>
where
A: PageFrameAllocate,
A: Alloc,
P: Translator,
{
let entry = unsafe { self.entry(space, alloc, translator, virt)? };
Expand All @@ -451,7 +451,7 @@ unsafe impl Segment<AddressSpaceHandle> for &'static AddressSegment {
virt: usize,
) -> Result<u64, UnmapError>
where
A: PageFrameAllocate + PageFrameFree,
A: Alloc,
P: Translator,
{
let phys = unsafe {
Expand All @@ -472,7 +472,7 @@ unsafe impl Segment<AddressSpaceHandle> for &'static AddressSegment {
phys: u64,
) -> Result<Option<u64>, MapError>
where
A: PageFrameAllocate + PageFrameFree,
A: Alloc,
P: Translator,
{
let entry = unsafe { self.entry(space, alloc, translator, virt)? };
Expand Down
8 changes: 2 additions & 6 deletions oro-boot/src/pfa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,17 +188,13 @@ impl<M: Into<OroMemRe> + Clone, I: Iterator<Item = M> + Clone> PrebootPfa<M, I>
}
}

unsafe impl<M: Into<OroMemRe> + Clone, I: Iterator<Item = M> + Clone>
oro_mem::pfa::alloc::PageFrameAllocate for PrebootPfa<M, I>
unsafe impl<M: Into<OroMemRe> + Clone, I: Iterator<Item = M> + Clone> oro_mem::pfa::alloc::Alloc
for PrebootPfa<M, I>
{
fn allocate(&mut self) -> Option<u64> {
self.allocate_page()
}
}

unsafe impl<M: Into<OroMemRe> + Clone, I: Iterator<Item = M> + Clone>
oro_mem::pfa::alloc::PageFrameFree for PrebootPfa<M, I>
{
unsafe fn free(&mut self, _frame: u64) {
panic!("preboot PFA cannot free frames");
}
Expand Down
4 changes: 2 additions & 2 deletions oro-boot/src/target/aarch64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub use oro_arch_aarch64::{ELF_CLASS, ELF_ENDIANNESS, ELF_MACHINE};
use oro_macro::asm_buffer;
use oro_mem::{
mapper::{AddressSegment, MapError},
pfa::alloc::{PageFrameAllocate, PageFrameFree},
pfa::alloc::Alloc,
translate::Translator,
};

Expand Down Expand Up @@ -71,7 +71,7 @@ const STUBS: &[u8] = &asm_buffer! {

/// Prepares the system for a transfer. Called before the memory map
/// is written, after which `transfer` is called.
pub unsafe fn prepare_transfer<P: Translator, A: PageFrameAllocate + PageFrameFree>(
pub unsafe fn prepare_transfer<P: Translator, A: Alloc>(
mapper: &mut AddressSpaceHandle,
alloc: &mut A,
pat: &P,
Expand Down
4 changes: 2 additions & 2 deletions oro-boot/src/target/x86_64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub use oro_arch_x86_64::{ELF_CLASS, ELF_ENDIANNESS, ELF_MACHINE};
use oro_macro::asm_buffer;
use oro_mem::{
mapper::{AddressSegment as _, AddressSpace as _, MapError, UnmapError},
pfa::alloc::{PageFrameAllocate, PageFrameFree},
pfa::alloc::Alloc,
translate::Translator,
};

Expand Down Expand Up @@ -57,7 +57,7 @@ pub fn target_address() -> usize {
/// Prepares the system for a transfer. Called before the memory map
/// is written, after which `transfer` is called.
#[expect(clippy::unnecessary_wraps)]
pub unsafe fn prepare_transfer<P: Translator, A: PageFrameAllocate + PageFrameFree>(
pub unsafe fn prepare_transfer<P: Translator, A: Alloc>(
mapper: &mut AddressSpaceHandle,
alloc: &mut A,
pat: &P,
Expand Down
10 changes: 5 additions & 5 deletions oro-kernel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

use oro_mem::{
mapper::{AddressSpace, MapError},
pfa::alloc::{PageFrameAllocate, PageFrameFree},
pfa::alloc::Alloc,
translate::Translator,
};
use oro_sync::spinlock::unfair_critical::{InterruptController, UnfairCriticalSpinlock};
Expand All @@ -29,7 +29,7 @@ pub mod ring;
/// and re-created on core powerdown/subsequent bringup).
pub struct Kernel<Pfa, Pat, AddrSpace, IntCtrl>
where
Pfa: PageFrameAllocate + PageFrameFree + 'static,
Pfa: Alloc + 'static,
Pat: Translator,
AddrSpace: AddressSpace + 'static,
IntCtrl: InterruptController + 'static,
Expand All @@ -40,7 +40,7 @@ where

impl<Pfa, Pat, AddrSpace, IntCtrl> Kernel<Pfa, Pat, AddrSpace, IntCtrl>
where
Pfa: PageFrameAllocate + PageFrameFree + 'static,
Pfa: Alloc + 'static,
Pat: Translator,
AddrSpace: AddressSpace,
IntCtrl: InterruptController,
Expand Down Expand Up @@ -71,7 +71,7 @@ where
/// core boot/powerdown/bringup cycles.
pub struct KernelState<Pfa, Pat, AddrSpace, IntCtrl>
where
Pfa: PageFrameAllocate + PageFrameFree,
Pfa: Alloc,
Pat: Translator,
AddrSpace: AddressSpace,
IntCtrl: InterruptController,
Expand All @@ -85,7 +85,7 @@ where

impl<Pfa, Pat, AddrSpace, IntCtrl> KernelState<Pfa, Pat, AddrSpace, IntCtrl>
where
Pfa: PageFrameAllocate + PageFrameFree,
Pfa: Alloc,
Pat: Translator,
AddrSpace: AddressSpace,
IntCtrl: InterruptController,
Expand Down
Loading

0 comments on commit e210393

Please sign in to comment.