xref: /DragonOS/kernel/src/arch/x86_64/mm/bump.rs (revision 83ed0ebc293d5a10245089f627f52770fd5b9dd4)
1 use crate::{
2     kdebug,
3     libs::align::{page_align_down, page_align_up},
4     mm::{
5         allocator::bump::BumpAllocator, MemoryManagementArch, PhysAddr, PhysMemoryArea, VirtAddr,
6     },
7 };
8 
9 use super::{X86_64MMBootstrapInfo, BOOTSTRAP_MM_INFO, PHYS_MEMORY_AREAS};
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 &PHYS_MEMORY_AREAS {
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             kdebug!("new arch remain area: {:?}", ret_areas[res_count]);
48             res_count += 1;
49         }
50 
51         return res_count;
52     }
53 }
54