From cad808f05cd6a15ac0a7c437ee4aae1bfc8b0f7b Mon Sep 17 00:00:00 2001 From: Zhang Junyang Date: Mon, 24 Jul 2023 15:32:53 +0800 Subject: [PATCH] Align the usable regions in the frame allocator --- .../src/arch/x86/boot/memory_region.rs | 24 ++----------------- .../src/arch/x86/boot/multiboot2/mod.rs | 7 ++++-- .../jinux-frame/src/vm/frame_allocator.rs | 6 +++-- 3 files changed, 11 insertions(+), 26 deletions(-) diff --git a/framework/jinux-frame/src/arch/x86/boot/memory_region.rs b/framework/jinux-frame/src/arch/x86/boot/memory_region.rs index 9282cbd98..20096623c 100644 --- a/framework/jinux-frame/src/arch/x86/boot/memory_region.rs +++ b/framework/jinux-frame/src/arch/x86/boot/memory_region.rs @@ -1,11 +1,8 @@ //! Information of memory regions in the boot phase. //! -use align_ext::AlignExt; use alloc::{vec, vec::Vec}; -use crate::config::PAGE_SIZE; - /// The type of initial memory regions that are needed for the kernel. #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)] pub enum MemoryRegionType { @@ -39,26 +36,9 @@ pub struct MemoryRegion { impl MemoryRegion { /// Construct a page aligned memory region. pub fn new(base: usize, len: usize, typ: MemoryRegionType) -> Self { - let aligned_base; - let aligned_end; - match typ { - MemoryRegionType::Usable | MemoryRegionType::Reclaimable => { - // Align shrinked. These regions may be used by the frame allocator. - aligned_base = base.align_up(PAGE_SIZE); - aligned_end = (base + len).align_down(PAGE_SIZE); - } - _ => { - // We can align other regions in a bloated manner since we do not - // use MemoryRegion as a way to deliver objects. They are just - // markers of untouchable memory areas or areas that need special - // treatments. - aligned_base = base.align_down(PAGE_SIZE); - aligned_end = (base + len).align_up(PAGE_SIZE); - } - } MemoryRegion { - base: aligned_base, - len: aligned_end - aligned_base, + base: base, + len: len, typ: typ, } } diff --git a/framework/jinux-frame/src/arch/x86/boot/multiboot2/mod.rs b/framework/jinux-frame/src/arch/x86/boot/multiboot2/mod.rs index 8506be746..916fa2cfa 100644 --- a/framework/jinux-frame/src/arch/x86/boot/multiboot2/mod.rs +++ b/framework/jinux-frame/src/arch/x86/boot/multiboot2/mod.rs @@ -179,8 +179,11 @@ pub fn init_memory_regions() { } // Initialize with regions_unusable + regions_src - regions_unusable.append(regions_src); - MEMORY_REGIONS.call_once(|| regions_unusable); + MEMORY_REGIONS.call_once(move || { + let mut all_regions = regions_unusable; + all_regions.append(regions_src); + all_regions + }); } // The entry point of kernel code, which should be defined by the package that diff --git a/framework/jinux-frame/src/vm/frame_allocator.rs b/framework/jinux-frame/src/vm/frame_allocator.rs index 83b6ab29b..6c51b20a1 100644 --- a/framework/jinux-frame/src/vm/frame_allocator.rs +++ b/framework/jinux-frame/src/vm/frame_allocator.rs @@ -1,3 +1,4 @@ +use align_ext::AlignExt; use alloc::vec::Vec; use buddy_system_allocator::FrameAllocator; use log::info; @@ -60,8 +61,9 @@ pub(crate) fn init(regions: &Vec) { let mut allocator = FrameAllocator::<32>::new(); for region in regions.iter() { if region.typ() == MemoryRegionType::Usable { - let start = region.base() / PAGE_SIZE; - let end = start + region.len() / PAGE_SIZE; + // Make the memory region page-aligned + let start = region.base().align_up(PAGE_SIZE) / PAGE_SIZE; + let end = (start + region.len()).align_down(PAGE_SIZE) / PAGE_SIZE; allocator.add_frame(start, end); info!( "Found usable region, start:{:x}, end:{:x}",