xref: /DragonOS/kernel/src/arch/x86_64/mm/bump.rs (revision fae6e9ade46a52976ad5d099643d51cc20876448)
1 use crate::{
2     libs::align::{page_align_down, page_align_up},
3     mm::{
4         allocator::bump::BumpAllocator,
5         memblock::{mem_block_manager, MemoryAreaAttr},
6         MemoryManagementArch, PhysAddr, PhysMemoryArea, VirtAddr,
7     },
8 };
9 
10 use super::{X86_64MMBootstrapInfo, BOOTSTRAP_MM_INFO};
11 
12 impl<MMA: MemoryManagementArch> BumpAllocator<MMA> {
13     pub unsafe fn arch_remain_areas(
14         ret_areas: &mut [PhysMemoryArea],
15         mut res_count: usize,
16     ) -> usize {
17         let info: X86_64MMBootstrapInfo = BOOTSTRAP_MM_INFO.unwrap();
18         let load_base = info.kernel_load_base_paddr;
19         let kernel_code_start = MMA::virt_2_phys(VirtAddr::new(info.kernel_code_start))
20             .unwrap()
21             .data();
22 
23         let offset_start = page_align_up(core::cmp::max(load_base + 16384, 0x200000));
24         let offset_end = page_align_down(kernel_code_start - 16384);
25 
26         // 把内核代码前的空间加入到可用内存区域中
27         for area in mem_block_manager().to_iter() {
28             let area_base = area.area_base_aligned().data();
29             let area_end = area.area_end_aligned().data();
30             if area_base >= offset_end {
31                 break;
32             }
33 
34             if area_end <= offset_start {
35                 continue;
36             }
37 
38             let new_start = core::cmp::max(offset_start, area_base);
39             let new_end = core::cmp::min(offset_end, area_end);
40 
41             if new_start >= new_end {
42                 continue;
43             }
44 
45             ret_areas[res_count] = PhysMemoryArea::new(
46                 PhysAddr::new(new_start),
47                 new_end - new_start,
48                 MemoryAreaAttr::empty(),
49             );
50 
51             res_count += 1;
52         }
53 
54         return res_count;
55     }
56 }
57