199dbf38dSLoGin use crate::{ 299dbf38dSLoGin libs::align::{page_align_down, page_align_up}, 399dbf38dSLoGin mm::{ 492849878SLoGin allocator::bump::BumpAllocator, 592849878SLoGin memblock::{mem_block_manager, MemoryAreaAttr}, 692849878SLoGin MemoryManagementArch, PhysAddr, PhysMemoryArea, VirtAddr, 799dbf38dSLoGin }, 899dbf38dSLoGin }; 999dbf38dSLoGin 1045626c85SLoGin use super::{X86_64MMBootstrapInfo, BOOTSTRAP_MM_INFO}; 1199dbf38dSLoGin 1299dbf38dSLoGin impl<MMA: MemoryManagementArch> BumpAllocator<MMA> { arch_remain_areas( ret_areas: &mut [PhysMemoryArea], mut res_count: usize, ) -> usize1399dbf38dSLoGin pub unsafe fn arch_remain_areas( 1499dbf38dSLoGin ret_areas: &mut [PhysMemoryArea], 1599dbf38dSLoGin mut res_count: usize, 1699dbf38dSLoGin ) -> usize { 17*b5b571e0SLoGin let info: X86_64MMBootstrapInfo = BOOTSTRAP_MM_INFO.unwrap(); 1899dbf38dSLoGin let load_base = info.kernel_load_base_paddr; 1999dbf38dSLoGin let kernel_code_start = MMA::virt_2_phys(VirtAddr::new(info.kernel_code_start)) 2099dbf38dSLoGin .unwrap() 2199dbf38dSLoGin .data(); 2299dbf38dSLoGin 2399dbf38dSLoGin let offset_start = page_align_up(core::cmp::max(load_base + 16384, 0x200000)); 2499dbf38dSLoGin let offset_end = page_align_down(kernel_code_start - 16384); 2599dbf38dSLoGin 2699dbf38dSLoGin // 把内核代码前的空间加入到可用内存区域中 2745626c85SLoGin for area in mem_block_manager().to_iter() { 2899dbf38dSLoGin let area_base = area.area_base_aligned().data(); 2999dbf38dSLoGin let area_end = area.area_end_aligned().data(); 3099dbf38dSLoGin if area_base >= offset_end { 3199dbf38dSLoGin break; 3299dbf38dSLoGin } 3399dbf38dSLoGin 3499dbf38dSLoGin if area_end <= offset_start { 3599dbf38dSLoGin continue; 3699dbf38dSLoGin } 3799dbf38dSLoGin 3899dbf38dSLoGin let new_start = core::cmp::max(offset_start, area_base); 3999dbf38dSLoGin let new_end = core::cmp::min(offset_end, area_end); 4099dbf38dSLoGin 4199dbf38dSLoGin if new_start >= new_end { 4299dbf38dSLoGin continue; 4399dbf38dSLoGin } 4499dbf38dSLoGin 4592849878SLoGin ret_areas[res_count] = PhysMemoryArea::new( 4692849878SLoGin PhysAddr::new(new_start), 4792849878SLoGin new_end - new_start, 4892849878SLoGin MemoryAreaAttr::empty(), 4992849878SLoGin ); 5099dbf38dSLoGin 5199dbf38dSLoGin res_count += 1; 5299dbf38dSLoGin } 5399dbf38dSLoGin 5499dbf38dSLoGin return res_count; 5599dbf38dSLoGin } 5699dbf38dSLoGin } 57