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