From e81525d349b84bdf1bc9e8cf939eb5d19dce440e Mon Sep 17 00:00:00 2001 From: Arthur Beck Date: Sun, 16 Feb 2025 21:04:55 -0600 Subject: [PATCH] CONFIG_MEMORY_UNION_ALL --- kernel/build.rs | 2 ++ kernel/config.aphro.example | 1 + kernel/src/kernel/mem.rs | 18 ++++++++++++++++++ 3 files changed, 21 insertions(+) diff --git a/kernel/build.rs b/kernel/build.rs index 0f79722..293a38e 100644 --- a/kernel/build.rs +++ b/kernel/build.rs @@ -39,6 +39,8 @@ fn main() { ); println!(r#"cargo:rustc-check-cfg=cfg(CONFIG_BUILD_GRUB, values("true", "false", none()))"#); + + println!(r#"cargo:rustc-check-cfg=cfg(CONFIG_MEMORY_UNION_ALL, values("true", "false", none()))"#); // End checks // Configuration name used when a config is required but should always evaluate to true diff --git a/kernel/config.aphro.example b/kernel/config.aphro.example index 5023f4c..ecf90c7 100644 --- a/kernel/config.aphro.example +++ b/kernel/config.aphro.example @@ -35,4 +35,5 @@ CONFIG_BUILD_GRUB=true # The precision of the allocator. The size of the allocated region is divided by this to get how much to change it by each loop iteration # when trying to find a allocatable region. CONFIG_ALLOC_PRECISION=4 +CONFIG_MEMORY_UNION_ALL=false # End configs \ No newline at end of file diff --git a/kernel/src/kernel/mem.rs b/kernel/src/kernel/mem.rs index 9dd0dca..4ea881c 100644 --- a/kernel/src/kernel/mem.rs +++ b/kernel/src/kernel/mem.rs @@ -322,6 +322,9 @@ impl<'a> MemoryMapAlloc<'a> { /// Check to see if any allocations contain the given address. Returns true if so. fn check_addr(&self, addr: u64) -> bool { + if cfg!(CONFIG_MEMORY_UNION_ALL = "true") { + return false; + } if addr >= (self.allocationheader as u64) && addr < (self.allocationheader as u64 + unsafe { *self.allocationheader }.len) { @@ -338,6 +341,9 @@ impl<'a> MemoryMapAlloc<'a> { /// Check to see if a range of addresses have any allocations within. Returns true if so. fn check_range(&self, addr: Range) -> bool { + if cfg!(CONFIG_MEMORY_UNION_ALL = "true") { + return false; + } for addr in addr { // REALLY inefficient, but I don't think there's a better way. if self.check_addr(addr) { @@ -519,6 +525,7 @@ unsafe impl<'a> Allocator for MemoryMapAlloc<'a> { continue; } } + if addr == 0 { unsafe { LAST_MEMMAP_ERR = Err(crate::Error::new( @@ -528,6 +535,14 @@ unsafe impl<'a> Allocator for MemoryMapAlloc<'a> { } return Err(core::alloc::AllocError {}); } + + if cfg!(CONFIG_MEMORY_UNION_ALL = "true") { + return Ok(NonNull::from_raw_parts( + NonNull::::without_provenance(NonZero::new(addr as usize).unwrap()), + layout.size(), + )); + } + if let Err(err) = self.add_allocation(Allocation { used: true, addr, @@ -545,6 +560,9 @@ unsafe impl<'a> Allocator for MemoryMapAlloc<'a> { unsafe fn deallocate(&self, ptr: core::ptr::NonNull, _layout: core::alloc::Layout) { unsafe { LAST_MEMMAP_ERR = Ok(()) } + if cfg!(CONFIG_MEMORY_UNION_ALL = "true") { + return; + } let addr = ptr.addr().get() as u64; if self.allocations == core::ptr::null_mut() { unsafe {