From 7261dccb72faca707514882779bb08e3004cdcc3 Mon Sep 17 00:00:00 2001 From: 4lDO2 <4lDO2@protonmail.com> Date: Sat, 23 Mar 2024 16:54:47 +0100 Subject: [PATCH] Fix i686. --- src/arch/x86/paging/mod.rs | 3 +- src/arch/x86/rmm.rs | 56 ++++++-------------------------------- src/arch/x86/start.rs | 3 -- src/arch/x86_64/rmm.rs | 4 +-- src/context/memory.rs | 2 +- src/memory/mod.rs | 6 ++++ 6 files changed, 19 insertions(+), 55 deletions(-) diff --git a/src/arch/x86/paging/mod.rs b/src/arch/x86/paging/mod.rs index e8bb147055..b316438317 100644 --- a/src/arch/x86/paging/mod.rs +++ b/src/arch/x86/paging/mod.rs @@ -9,7 +9,7 @@ use self::mapper::PageFlushAll; pub use super::CurrentRmmArch as RmmA; pub use rmm::{Arch as RmmArch, Flusher, PageFlags, PhysicalAddress, TableKind, VirtualAddress}; -pub type PageMapper = rmm::PageMapper; +pub type PageMapper = rmm::PageMapper; pub use crate::rmm::KernelMapper; pub mod entry { @@ -29,6 +29,7 @@ pub const ENTRY_COUNT: usize = RmmA::PAGE_ENTRIES; /// Size of pages pub const PAGE_SIZE: usize = RmmA::PAGE_SIZE; +pub const PAGE_MASK: usize = RmmA::PAGE_OFFSET_MASK; /// Setup page attribute table #[cold] diff --git a/src/arch/x86/rmm.rs b/src/arch/x86/rmm.rs index 4e7d55f360..a4adb7ad1a 100644 --- a/src/arch/x86/rmm.rs +++ b/src/arch/x86/rmm.rs @@ -10,7 +10,7 @@ use rmm::{ }; use spin::Mutex; -use crate::cpu_set::LogicalCpuId; +use crate::{cpu_set::LogicalCpuId, memory::TheFrameAllocator}; use super::CurrentRmmArch as RmmA; @@ -49,7 +49,7 @@ unsafe fn page_flags(virt: VirtualAddress) -> PageFlags { } } -unsafe fn inner( +unsafe fn inner( areas: &'static [MemoryArea], kernel_base: usize, kernel_size_aligned: usize, @@ -61,7 +61,9 @@ unsafe fn inner( acpi_size_aligned: usize, initfs_base: usize, initfs_size_aligned: usize, -) -> BuddyAllocator { +) { + type A = RmmA; + // First, calculate how much memory we have let mut size = 0; for area in areas.iter() { @@ -184,46 +186,7 @@ unsafe fn inner( (offset + (KILOBYTE - 1)) / KILOBYTE ); - BuddyAllocator::::new(bump_allocator).expect("failed to create BuddyAllocator") -} - -// There can only be one allocator (at the moment), so making this a ZST is great! -#[derive(Clone, Copy)] -pub struct LockedAllocator; - -static INNER_ALLOCATOR: Mutex>> = Mutex::new(None); - -impl FrameAllocator for LockedAllocator { - unsafe fn allocate(&mut self, count: FrameCount) -> Option { - if let Some(ref mut allocator) = *INNER_ALLOCATOR.lock() { - allocator.allocate(count) - } else { - None - } - } - - unsafe fn free(&mut self, address: PhysicalAddress, count: FrameCount) { - if let Some(ref mut allocator) = *INNER_ALLOCATOR.lock() { - allocator.free(address, count) - } - } - - unsafe fn usage(&self) -> FrameUsage { - if let Some(ref allocator) = *INNER_ALLOCATOR.lock() { - allocator.usage() - } else { - FrameUsage::new(FrameCount::new(0), FrameCount::new(0)) - } - } -} -impl core::fmt::Debug for LockedAllocator { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - match INNER_ALLOCATOR.try_lock().as_deref() { - Some(Some(alloc)) => write!(f, "[locked allocator: {:?}]", unsafe { alloc.usage() }), - Some(None) => write!(f, "[uninitialized lock allocator]"), - None => write!(f, "[failed to lock]"), - } - } + crate::memory::init_mm(bump_allocator); } static AREAS: SyncUnsafeCell<[MemoryArea; 512]> = SyncUnsafeCell::new( @@ -239,8 +202,6 @@ pub fn areas() -> &'static [MemoryArea] { unsafe { &(&*AREAS.get())[..AREA_COUNT.get().read().into()] } } -pub static FRAME_ALLOCATOR: LockedAllocator = LockedAllocator; - const NO_PROCESSOR: usize = !0; static LOCK_OWNER: AtomicUsize = AtomicUsize::new(NO_PROCESSOR); static LOCK_COUNT: AtomicUsize = AtomicUsize::new(0); @@ -291,7 +252,7 @@ impl KernelMapper { unsafe { Self::lock_for_manual_mapper( current_processor, - PageMapper::current(TableKind::Kernel, FRAME_ALLOCATOR), + PageMapper::current(TableKind::Kernel, TheFrameAllocator), ) } } @@ -537,7 +498,7 @@ pub unsafe fn init( } AREA_COUNT.get().write(area_i as u16); - let allocator = inner::( + inner( areas, kernel_base, kernel_size_aligned, @@ -550,5 +511,4 @@ pub unsafe fn init( initfs_base, initfs_size_aligned, ); - *INNER_ALLOCATOR.lock() = Some(allocator); } diff --git a/src/arch/x86/start.rs b/src/arch/x86/start.rs index e0ba567be7..d5fd523a0a 100644 --- a/src/arch/x86/start.rs +++ b/src/arch/x86/start.rs @@ -202,9 +202,6 @@ pub unsafe extern "C" fn kstart(args_ptr: *const KernelArgs) -> ! { // Initialize all of the non-core devices not otherwise needed to complete initialization device::init_noncore(); - // Initialize data structures used to track pages. - memory::init_mm(); - // Stop graphical debug #[cfg(feature = "graphical_debug")] graphical_debug::fini(); diff --git a/src/arch/x86_64/rmm.rs b/src/arch/x86_64/rmm.rs index a5e40eb661..577f02231d 100644 --- a/src/arch/x86_64/rmm.rs +++ b/src/arch/x86_64/rmm.rs @@ -4,8 +4,8 @@ use core::{ sync::atomic::{self, AtomicUsize, Ordering}, }; use rmm::{ - Arch, BuddyAllocator, BumpAllocator, FrameAllocator, FrameCount, FrameUsage, MemoryArea, - PageFlags, PageMapper, PhysicalAddress, TableKind, VirtualAddress, KILOBYTE, MEGABYTE, + Arch, BumpAllocator, FrameAllocator, FrameCount, FrameUsage, MemoryArea, PageFlags, PageMapper, + PhysicalAddress, TableKind, VirtualAddress, KILOBYTE, MEGABYTE, }; use spin::Mutex; diff --git a/src/context/memory.rs b/src/context/memory.rs index e51ecca740..d0ac4b49d0 100644 --- a/src/context/memory.rs +++ b/src/context/memory.rs @@ -2130,7 +2130,7 @@ pub fn setup_new_utable() -> Result { use crate::paging::KernelMapper; let utable = unsafe { - PageMapper::create(TableKind::User, crate::rmm::FRAME_ALLOCATOR) + PageMapper::create(TableKind::User, crate::memory::TheFrameAllocator) .ok_or(Error::new(ENOMEM))? }; diff --git a/src/memory/mod.rs b/src/memory/mod.rs index 811fc88cf3..fb8f6b6f2a 100644 --- a/src/memory/mod.rs +++ b/src/memory/mod.rs @@ -495,6 +495,12 @@ fn init_sections(mut allocator: BumpAllocator) { while let Some(mut memory_map_area) = iter.next() { // TODO: NonZeroUsize + + // TODO: x86_32 fails without this check + if memory_map_area.size == 0 { + continue; + } + assert_ne!( memory_map_area.size, 0, "RMM should enforce areas are not zeroed"