Align the usable regions in the frame allocator

This commit is contained in:
Zhang Junyang
2023-07-24 15:32:53 +08:00
committed by Tate, Hongliang Tian
parent 5012779e38
commit cad808f05c
3 changed files with 11 additions and 26 deletions

View File

@ -1,11 +1,8 @@
//! Information of memory regions in the boot phase. //! Information of memory regions in the boot phase.
//! //!
use align_ext::AlignExt;
use alloc::{vec, vec::Vec}; use alloc::{vec, vec::Vec};
use crate::config::PAGE_SIZE;
/// The type of initial memory regions that are needed for the kernel. /// The type of initial memory regions that are needed for the kernel.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)] #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]
pub enum MemoryRegionType { pub enum MemoryRegionType {
@ -39,26 +36,9 @@ pub struct MemoryRegion {
impl MemoryRegion { impl MemoryRegion {
/// Construct a page aligned memory region. /// Construct a page aligned memory region.
pub fn new(base: usize, len: usize, typ: MemoryRegionType) -> Self { 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 { MemoryRegion {
base: aligned_base, base: base,
len: aligned_end - aligned_base, len: len,
typ: typ, typ: typ,
} }
} }

View File

@ -179,8 +179,11 @@ pub fn init_memory_regions() {
} }
// Initialize with regions_unusable + regions_src // Initialize with regions_unusable + regions_src
regions_unusable.append(regions_src); MEMORY_REGIONS.call_once(move || {
MEMORY_REGIONS.call_once(|| regions_unusable); 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 // The entry point of kernel code, which should be defined by the package that

View File

@ -1,3 +1,4 @@
use align_ext::AlignExt;
use alloc::vec::Vec; use alloc::vec::Vec;
use buddy_system_allocator::FrameAllocator; use buddy_system_allocator::FrameAllocator;
use log::info; use log::info;
@ -60,8 +61,9 @@ pub(crate) fn init(regions: &Vec<MemoryRegion>) {
let mut allocator = FrameAllocator::<32>::new(); let mut allocator = FrameAllocator::<32>::new();
for region in regions.iter() { for region in regions.iter() {
if region.typ() == MemoryRegionType::Usable { if region.typ() == MemoryRegionType::Usable {
let start = region.base() / PAGE_SIZE; // Make the memory region page-aligned
let end = start + region.len() / PAGE_SIZE; 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); allocator.add_frame(start, end);
info!( info!(
"Found usable region, start:{:x}, end:{:x}", "Found usable region, start:{:x}, end:{:x}",