xref: /DragonOS/kernel/src/arch/x86_64/mm/mod.rs (revision c75089286e9d49cef8d039446bf570c1bd4d2550)
1 pub mod barrier;
2 pub mod bump;
3 
4 use alloc::vec::Vec;
5 use hashbrown::HashSet;
6 use x86::time::rdtsc;
7 use x86_64::registers::model_specific::EferFlags;
8 
9 use crate::driver::tty::serial::serial8250::send_to_default_serial8250_port;
10 use crate::include::bindings::bindings::{
11     multiboot2_get_load_base, multiboot2_get_memory, multiboot2_iter, multiboot_mmap_entry_t,
12     multiboot_tag_load_base_addr_t,
13 };
14 use crate::libs::align::page_align_up;
15 use crate::libs::lib_ui::screen_manager::scm_disable_put_to_window;
16 use crate::libs::printk::PrintkWriter;
17 use crate::libs::spinlock::SpinLock;
18 
19 use crate::mm::allocator::page_frame::{FrameAllocator, PageFrameCount, PageFrameUsage};
20 use crate::mm::mmio_buddy::mmio_init;
21 use crate::{
22     arch::MMArch,
23     mm::allocator::{buddy::BuddyAllocator, bump::BumpAllocator},
24 };
25 
26 use crate::mm::kernel_mapper::KernelMapper;
27 use crate::mm::page::{PageEntry, PageFlags};
28 use crate::mm::{MemoryManagementArch, PageTableKind, PhysAddr, PhysMemoryArea, VirtAddr};
29 use crate::syscall::SystemError;
30 use crate::{kdebug, kinfo};
31 
32 use core::arch::asm;
33 use core::ffi::c_void;
34 use core::fmt::{Debug, Write};
35 use core::mem::{self};
36 
37 use core::sync::atomic::{compiler_fence, AtomicBool, Ordering};
38 
39 use super::kvm::vmx::vmcs::VmcsFields;
40 use super::kvm::vmx::vmx_asm_wrapper::vmx_vmread;
41 
42 pub type PageMapper =
43     crate::mm::page::PageMapper<crate::arch::x86_64::mm::X86_64MMArch, LockedFrameAllocator>;
44 
45 /// @brief 用于存储物理内存区域的数组
46 static mut PHYS_MEMORY_AREAS: [PhysMemoryArea; 512] = [PhysMemoryArea {
47     base: PhysAddr::new(0),
48     size: 0,
49 }; 512];
50 
51 /// 初始的CR3寄存器的值,用于内存管理初始化时,创建的第一个内核页表的位置
52 static mut INITIAL_CR3_VALUE: PhysAddr = PhysAddr::new(0);
53 
54 /// 内核的第一个页表在pml4中的索引
55 /// 顶级页表的[256, 512)项是内核的页表
56 static KERNEL_PML4E_NO: usize = (X86_64MMArch::PHYS_OFFSET & ((1 << 48) - 1)) >> 39;
57 
58 static INNER_ALLOCATOR: SpinLock<Option<BuddyAllocator<MMArch>>> = SpinLock::new(None);
59 
60 #[derive(Clone, Copy, Debug)]
61 pub struct X86_64MMBootstrapInfo {
62     kernel_load_base_paddr: usize,
63     kernel_code_start: usize,
64     kernel_code_end: usize,
65     kernel_data_end: usize,
66     kernel_rodata_end: usize,
67     start_brk: usize,
68 }
69 
70 pub(super) static mut BOOTSTRAP_MM_INFO: Option<X86_64MMBootstrapInfo> = None;
71 
72 /// @brief X86_64的内存管理架构结构体
73 #[derive(Debug, Clone, Copy, Hash)]
74 pub struct X86_64MMArch;
75 
76 /// XD标志位是否被保留
77 static XD_RESERVED: AtomicBool = AtomicBool::new(false);
78 
79 impl MemoryManagementArch for X86_64MMArch {
80     /// 4K页
81     const PAGE_SHIFT: usize = 12;
82 
83     /// 每个页表项占8字节,总共有512个页表项
84     const PAGE_ENTRY_SHIFT: usize = 9;
85 
86     /// 四级页表(PML4T、PDPT、PDT、PT)
87     const PAGE_LEVELS: usize = 4;
88 
89     /// 页表项的有效位的index。在x86_64中,页表项的第[0, 47]位表示地址和flag,
90     /// 第[48, 51]位表示保留。因此,有效位的index为52。
91     /// 请注意,第63位是XD位,表示是否允许执行。
92     const ENTRY_ADDRESS_SHIFT: usize = 52;
93 
94     const ENTRY_FLAG_DEFAULT_PAGE: usize = Self::ENTRY_FLAG_PRESENT;
95 
96     const ENTRY_FLAG_DEFAULT_TABLE: usize = Self::ENTRY_FLAG_PRESENT;
97 
98     const ENTRY_FLAG_PRESENT: usize = 1 << 0;
99 
100     const ENTRY_FLAG_READONLY: usize = 0;
101 
102     const ENTRY_FLAG_READWRITE: usize = 1 << 1;
103 
104     const ENTRY_FLAG_USER: usize = 1 << 2;
105 
106     const ENTRY_FLAG_WRITE_THROUGH: usize = 1 << 3;
107 
108     const ENTRY_FLAG_CACHE_DISABLE: usize = 1 << 4;
109 
110     const ENTRY_FLAG_NO_EXEC: usize = 1 << 63;
111     /// x86_64不存在EXEC标志位,只有NO_EXEC(XD)标志位
112     const ENTRY_FLAG_EXEC: usize = 0;
113 
114     /// 物理地址与虚拟地址的偏移量
115     /// 0xffff_8000_0000_0000
116     const PHYS_OFFSET: usize = Self::PAGE_NEGATIVE_MASK + (Self::PAGE_ADDRESS_SIZE >> 1);
117 
118     const USER_END_VADDR: VirtAddr = VirtAddr::new(0x0000_7eff_ffff_ffff);
119     const USER_BRK_START: VirtAddr = VirtAddr::new(0x700000000000);
120     const USER_STACK_START: VirtAddr = VirtAddr::new(0x6ffff0a00000);
121 
122     /// @brief 获取物理内存区域
123     unsafe fn init() -> &'static [crate::mm::PhysMemoryArea] {
124         extern "C" {
125             fn _text();
126             fn _etext();
127             fn _edata();
128             fn _erodata();
129             fn _end();
130         }
131 
132         Self::init_xd_rsvd();
133         let load_base_paddr = Self::get_load_base_paddr();
134 
135         let bootstrap_info = X86_64MMBootstrapInfo {
136             kernel_load_base_paddr: load_base_paddr.data(),
137             kernel_code_start: _text as usize,
138             kernel_code_end: _etext as usize,
139             kernel_data_end: _edata as usize,
140             kernel_rodata_end: _erodata as usize,
141             start_brk: _end as usize,
142         };
143 
144         unsafe {
145             BOOTSTRAP_MM_INFO = Some(bootstrap_info);
146         }
147 
148         // 初始化物理内存区域(从multiboot2中获取)
149         let areas_count =
150             Self::init_memory_area_from_multiboot2().expect("init memory area failed");
151 
152         send_to_default_serial8250_port("x86 64 init end\n\0".as_bytes());
153 
154         return &PHYS_MEMORY_AREAS[0..areas_count];
155     }
156 
157     /// @brief 刷新TLB中,关于指定虚拟地址的条目
158     unsafe fn invalidate_page(address: VirtAddr) {
159         compiler_fence(Ordering::SeqCst);
160         asm!("invlpg [{0}]", in(reg) address.data(), options(nostack, preserves_flags));
161         compiler_fence(Ordering::SeqCst);
162     }
163 
164     /// @brief 刷新TLB中,所有的条目
165     unsafe fn invalidate_all() {
166         compiler_fence(Ordering::SeqCst);
167         // 通过设置cr3寄存器,来刷新整个TLB
168         Self::set_table(PageTableKind::User, Self::table(PageTableKind::User));
169         compiler_fence(Ordering::SeqCst);
170     }
171 
172     /// @brief 获取顶级页表的物理地址
173     unsafe fn table(table_kind: PageTableKind) -> PhysAddr {
174         match table_kind {
175             PageTableKind::Kernel | PageTableKind::User => {
176                 let paddr: usize;
177                 compiler_fence(Ordering::SeqCst);
178                 asm!("mov {}, cr3", out(reg) paddr, options(nomem, nostack, preserves_flags));
179                 compiler_fence(Ordering::SeqCst);
180                 return PhysAddr::new(paddr);
181             }
182             PageTableKind::EPT => {
183                 let eptp =
184                     vmx_vmread(VmcsFields::CTRL_EPTP_PTR as u32).expect("Failed to read eptp");
185                 return PhysAddr::new(eptp as usize);
186             }
187         }
188     }
189 
190     /// @brief 设置顶级页表的物理地址到处理器中
191     unsafe fn set_table(_table_kind: PageTableKind, table: PhysAddr) {
192         compiler_fence(Ordering::SeqCst);
193         asm!("mov cr3, {}", in(reg) table.data(), options(nostack, preserves_flags));
194         compiler_fence(Ordering::SeqCst);
195     }
196 
197     /// @brief 判断虚拟地址是否合法
198     fn virt_is_valid(virt: VirtAddr) -> bool {
199         return virt.is_canonical();
200     }
201 
202     /// 获取内存管理初始化时,创建的第一个内核页表的地址
203     fn initial_page_table() -> PhysAddr {
204         unsafe {
205             return INITIAL_CR3_VALUE;
206         }
207     }
208 
209     /// @brief 创建新的顶层页表
210     ///
211     /// 该函数会创建页表并复制内核的映射到新的页表中
212     ///
213     /// @return 新的页表
214     fn setup_new_usermapper() -> Result<crate::mm::ucontext::UserMapper, SystemError> {
215         let new_umapper: crate::mm::page::PageMapper<X86_64MMArch, LockedFrameAllocator> = unsafe {
216             PageMapper::create(PageTableKind::User, LockedFrameAllocator)
217                 .ok_or(SystemError::ENOMEM)?
218         };
219 
220         let current_ktable: KernelMapper = KernelMapper::lock();
221         let copy_mapping = |pml4_entry_no| unsafe {
222             let entry: PageEntry<X86_64MMArch> = current_ktable
223                 .table()
224                 .entry(pml4_entry_no)
225                 .unwrap_or_else(|| panic!("entry {} not found", pml4_entry_no));
226             new_umapper.table().set_entry(pml4_entry_no, entry)
227         };
228 
229         // 复制内核的映射
230         for pml4_entry_no in KERNEL_PML4E_NO..512 {
231             copy_mapping(pml4_entry_no);
232         }
233 
234         return Ok(crate::mm::ucontext::UserMapper::new(new_umapper));
235     }
236 }
237 
238 impl X86_64MMArch {
239     unsafe fn get_load_base_paddr() -> PhysAddr {
240         let mut mb2_lb_info: [multiboot_tag_load_base_addr_t; 512] = mem::zeroed();
241         send_to_default_serial8250_port("get_load_base_paddr begin\n\0".as_bytes());
242 
243         let mut mb2_count: u32 = 0;
244         multiboot2_iter(
245             Some(multiboot2_get_load_base),
246             &mut mb2_lb_info as *mut [multiboot_tag_load_base_addr_t; 512] as usize as *mut c_void,
247             &mut mb2_count,
248         );
249 
250         if mb2_count == 0 {
251             send_to_default_serial8250_port(
252                 "get_load_base_paddr mb2_count == 0, default to 1MB\n\0".as_bytes(),
253             );
254             return PhysAddr::new(0x100000);
255         }
256 
257         let phys = mb2_lb_info[0].load_base_addr as usize;
258 
259         return PhysAddr::new(phys);
260     }
261     unsafe fn init_memory_area_from_multiboot2() -> Result<usize, SystemError> {
262         // 这个数组用来存放内存区域的信息(从C获取)
263         let mut mb2_mem_info: [multiboot_mmap_entry_t; 512] = mem::zeroed();
264         send_to_default_serial8250_port("init_memory_area_from_multiboot2 begin\n\0".as_bytes());
265 
266         let mut mb2_count: u32 = 0;
267         multiboot2_iter(
268             Some(multiboot2_get_memory),
269             &mut mb2_mem_info as *mut [multiboot_mmap_entry_t; 512] as usize as *mut c_void,
270             &mut mb2_count,
271         );
272         send_to_default_serial8250_port("init_memory_area_from_multiboot2 2\n\0".as_bytes());
273 
274         let mb2_count = mb2_count as usize;
275         let mut areas_count = 0usize;
276         let mut total_mem_size = 0usize;
277         for i in 0..mb2_count {
278             // Only use the memory area if its type is 1 (RAM)
279             if mb2_mem_info[i].type_ == 1 {
280                 // Skip the memory area if its len is 0
281                 if mb2_mem_info[i].len == 0 {
282                     continue;
283                 }
284                 total_mem_size += mb2_mem_info[i].len as usize;
285                 PHYS_MEMORY_AREAS[areas_count].base = PhysAddr::new(mb2_mem_info[i].addr as usize);
286                 PHYS_MEMORY_AREAS[areas_count].size = mb2_mem_info[i].len as usize;
287                 areas_count += 1;
288             }
289         }
290         send_to_default_serial8250_port("init_memory_area_from_multiboot2 end\n\0".as_bytes());
291         kinfo!("Total memory size: {} MB, total areas from multiboot2: {mb2_count}, valid areas: {areas_count}", total_mem_size / 1024 / 1024);
292         return Ok(areas_count);
293     }
294 
295     fn init_xd_rsvd() {
296         // 读取ia32-EFER寄存器的值
297         let efer: EferFlags = x86_64::registers::model_specific::Efer::read();
298         if !efer.contains(EferFlags::NO_EXECUTE_ENABLE) {
299             // NO_EXECUTE_ENABLE是false,那么就设置xd_reserved为true
300             kdebug!("NO_EXECUTE_ENABLE is false, set XD_RESERVED to true");
301             XD_RESERVED.store(true, Ordering::Relaxed);
302         }
303         compiler_fence(Ordering::SeqCst);
304     }
305 
306     /// 判断XD标志位是否被保留
307     pub fn is_xd_reserved() -> bool {
308         // return XD_RESERVED.load(Ordering::Relaxed);
309 
310         // 由于暂时不支持execute disable,因此直接返回true
311         // 不支持的原因是,目前好像没有能正确的设置page-level的xd位,会触发page fault
312         return true;
313     }
314 }
315 
316 impl VirtAddr {
317     /// @brief 判断虚拟地址是否合法
318     #[inline(always)]
319     pub fn is_canonical(self) -> bool {
320         let x = self.data() & X86_64MMArch::PHYS_OFFSET;
321         // 如果x为0,说明虚拟地址的高位为0,是合法的用户地址
322         // 如果x为PHYS_OFFSET,说明虚拟地址的高位全为1,是合法的内核地址
323         return x == 0 || x == X86_64MMArch::PHYS_OFFSET;
324     }
325 }
326 
327 /// @brief 初始化内存管理模块
328 pub fn mm_init() {
329     send_to_default_serial8250_port("mm_init\n\0".as_bytes());
330     PrintkWriter
331         .write_fmt(format_args!("mm_init() called\n"))
332         .unwrap();
333     // printk_color!(GREEN, BLACK, "mm_init() called\n");
334     static _CALL_ONCE: AtomicBool = AtomicBool::new(false);
335     if _CALL_ONCE
336         .compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst)
337         .is_err()
338     {
339         send_to_default_serial8250_port("mm_init err\n\0".as_bytes());
340         panic!("mm_init() can only be called once");
341     }
342 
343     unsafe { X86_64MMArch::init() };
344     kdebug!("bootstrap info: {:?}", unsafe { BOOTSTRAP_MM_INFO });
345     kdebug!("phys[0]=virt[0x{:x}]", unsafe {
346         MMArch::phys_2_virt(PhysAddr::new(0)).unwrap().data()
347     });
348 
349     // 初始化内存管理器
350     unsafe { allocator_init() };
351     // enable mmio
352     mmio_init();
353 }
354 
355 unsafe fn allocator_init() {
356     let virt_offset = BOOTSTRAP_MM_INFO.unwrap().start_brk;
357     let phy_offset =
358         unsafe { MMArch::virt_2_phys(VirtAddr::new(page_align_up(virt_offset))) }.unwrap();
359 
360     kdebug!("PhysArea[0..10] = {:?}", &PHYS_MEMORY_AREAS[0..10]);
361     let mut bump_allocator =
362         BumpAllocator::<X86_64MMArch>::new(&PHYS_MEMORY_AREAS, phy_offset.data());
363     kdebug!(
364         "BumpAllocator created, offset={:?}",
365         bump_allocator.offset()
366     );
367 
368     // 暂存初始在head.S中指定的页表的地址,后面再考虑是否需要把它加到buddy的可用空间里面!
369     // 现在不加的原因是,我担心会有安全漏洞问题:这些初始的页表,位于内核的数据段。如果归还到buddy,
370     // 可能会产生一定的安全风险(有的代码可能根据虚拟地址来进行安全校验)
371     let _old_page_table = MMArch::table(PageTableKind::Kernel);
372 
373     let new_page_table: PhysAddr;
374     // 使用bump分配器,把所有的内存页都映射到页表
375     {
376         // 用bump allocator创建新的页表
377         let mut mapper: crate::mm::page::PageMapper<MMArch, &mut BumpAllocator<MMArch>> =
378             crate::mm::page::PageMapper::<MMArch, _>::create(
379                 PageTableKind::Kernel,
380                 &mut bump_allocator,
381             )
382             .expect("Failed to create page mapper");
383         new_page_table = mapper.table().phys();
384         kdebug!("PageMapper created");
385 
386         // 取消最开始时候,在head.S中指定的映射(暂时不刷新TLB)
387         {
388             let table = mapper.table();
389             let empty_entry = PageEntry::<MMArch>::new(0);
390             for i in 0..MMArch::PAGE_ENTRY_NUM {
391                 table
392                     .set_entry(i, empty_entry)
393                     .expect("Failed to empty page table entry");
394             }
395         }
396         kdebug!("Successfully emptied page table");
397 
398         for area in PHYS_MEMORY_AREAS.iter() {
399             // kdebug!("area: base={:?}, size={:#x}, end={:?}", area.base, area.size, area.base + area.size);
400             for i in 0..((area.size + MMArch::PAGE_SIZE - 1) / MMArch::PAGE_SIZE) {
401                 let paddr = area.base.add(i * MMArch::PAGE_SIZE);
402                 let vaddr = unsafe { MMArch::phys_2_virt(paddr) }.unwrap();
403                 let flags = kernel_page_flags::<MMArch>(vaddr);
404 
405                 let flusher = mapper
406                     .map_phys(vaddr, paddr, flags)
407                     .expect("Failed to map frame");
408                 // 暂时不刷新TLB
409                 flusher.ignore();
410             }
411         }
412 
413         // 添加低地址的映射(在smp完成初始化之前,需要使用低地址的映射.初始化之后需要取消这一段映射)
414         LowAddressRemapping::remap_at_low_address(&mut mapper);
415     }
416 
417     unsafe {
418         INITIAL_CR3_VALUE = new_page_table;
419     }
420     kdebug!(
421         "After mapping all physical memory, DragonOS used: {} KB",
422         bump_allocator.offset() / 1024
423     );
424 
425     // 初始化buddy_allocator
426     let buddy_allocator = unsafe { BuddyAllocator::<X86_64MMArch>::new(bump_allocator).unwrap() };
427     // 设置全局的页帧分配器
428     unsafe { set_inner_allocator(buddy_allocator) };
429     kinfo!("Successfully initialized buddy allocator");
430     // 关闭显示输出
431     scm_disable_put_to_window();
432 
433     // make the new page table current
434     {
435         let mut binding = INNER_ALLOCATOR.lock();
436         let mut allocator_guard = binding.as_mut().unwrap();
437         kdebug!("To enable new page table.");
438         compiler_fence(Ordering::SeqCst);
439         let mapper = crate::mm::page::PageMapper::<MMArch, _>::new(
440             PageTableKind::Kernel,
441             new_page_table,
442             &mut allocator_guard,
443         );
444         compiler_fence(Ordering::SeqCst);
445         mapper.make_current();
446         compiler_fence(Ordering::SeqCst);
447         kdebug!("New page table enabled");
448     }
449     kdebug!("Successfully enabled new page table");
450 }
451 
452 #[no_mangle]
453 pub extern "C" fn rs_test_buddy() {
454     test_buddy();
455 }
456 pub fn test_buddy() {
457     // 申请内存然后写入数据然后free掉
458     // 总共申请200MB内存
459     const TOTAL_SIZE: usize = 200 * 1024 * 1024;
460 
461     for i in 0..10 {
462         kdebug!("Test buddy, round: {i}");
463         // 存放申请的内存块
464         let mut v: Vec<(PhysAddr, PageFrameCount)> = Vec::with_capacity(60 * 1024);
465         // 存放已经申请的内存块的地址(用于检查重复)
466         let mut addr_set: HashSet<PhysAddr> = HashSet::new();
467 
468         let mut allocated = 0usize;
469 
470         let mut free_count = 0usize;
471 
472         while allocated < TOTAL_SIZE {
473             let mut random_size = 0u64;
474             unsafe { x86::random::rdrand64(&mut random_size) };
475             // 一次最多申请4M
476             random_size = random_size % (1024 * 4096);
477             if random_size == 0 {
478                 continue;
479             }
480             let random_size =
481                 core::cmp::min(page_align_up(random_size as usize), TOTAL_SIZE - allocated);
482             let random_size = PageFrameCount::from_bytes(random_size.next_power_of_two()).unwrap();
483             // 获取帧
484             let (paddr, allocated_frame_count) =
485                 unsafe { LockedFrameAllocator.allocate(random_size).unwrap() };
486             assert!(allocated_frame_count.data().is_power_of_two());
487             assert!(paddr.data() % MMArch::PAGE_SIZE == 0);
488             unsafe {
489                 assert!(MMArch::phys_2_virt(paddr)
490                     .as_ref()
491                     .unwrap()
492                     .check_aligned(allocated_frame_count.data() * MMArch::PAGE_SIZE));
493             }
494             allocated += allocated_frame_count.data() * MMArch::PAGE_SIZE;
495             v.push((paddr, allocated_frame_count));
496             assert!(addr_set.insert(paddr), "duplicate address: {:?}", paddr);
497 
498             // 写入数据
499             let vaddr = unsafe { MMArch::phys_2_virt(paddr).unwrap() };
500             let slice = unsafe {
501                 core::slice::from_raw_parts_mut(
502                     vaddr.data() as *mut u8,
503                     allocated_frame_count.data() * MMArch::PAGE_SIZE,
504                 )
505             };
506             for i in 0..slice.len() {
507                 slice[i] = ((i + unsafe { rdtsc() } as usize) % 256) as u8;
508             }
509 
510             // 随机释放一个内存块
511             if v.len() > 0 {
512                 let mut random_index = 0u64;
513                 unsafe { x86::random::rdrand64(&mut random_index) };
514                 // 70%概率释放
515                 if random_index % 10 > 7 {
516                     continue;
517                 }
518                 random_index = random_index % v.len() as u64;
519                 let random_index = random_index as usize;
520                 let (paddr, allocated_frame_count) = v.remove(random_index);
521                 assert!(addr_set.remove(&paddr));
522                 unsafe { LockedFrameAllocator.free(paddr, allocated_frame_count) };
523                 free_count += allocated_frame_count.data() * MMArch::PAGE_SIZE;
524             }
525         }
526 
527         kdebug!(
528             "Allocated {} MB memory, release: {} MB, no release: {} bytes",
529             allocated / 1024 / 1024,
530             free_count / 1024 / 1024,
531             (allocated - free_count)
532         );
533 
534         kdebug!("Now, to release buddy memory");
535         // 释放所有的内存
536         for (paddr, allocated_frame_count) in v {
537             unsafe { LockedFrameAllocator.free(paddr, allocated_frame_count) };
538             assert!(addr_set.remove(&paddr));
539             free_count += allocated_frame_count.data() * MMArch::PAGE_SIZE;
540         }
541 
542         kdebug!("release done!, allocated: {allocated}, free_count: {free_count}");
543     }
544 }
545 /// 全局的页帧分配器
546 #[derive(Debug, Clone, Copy, Hash)]
547 pub struct LockedFrameAllocator;
548 
549 impl FrameAllocator for LockedFrameAllocator {
550     unsafe fn allocate(&mut self, count: PageFrameCount) -> Option<(PhysAddr, PageFrameCount)> {
551         if let Some(ref mut allocator) = *INNER_ALLOCATOR.lock_irqsave() {
552             return allocator.allocate(count);
553         } else {
554             return None;
555         }
556     }
557 
558     unsafe fn free(&mut self, address: crate::mm::PhysAddr, count: PageFrameCount) {
559         assert!(count.data().is_power_of_two());
560         if let Some(ref mut allocator) = *INNER_ALLOCATOR.lock_irqsave() {
561             return allocator.free(address, count);
562         }
563     }
564 
565     unsafe fn usage(&self) -> PageFrameUsage {
566         if let Some(ref mut allocator) = *INNER_ALLOCATOR.lock_irqsave() {
567             return allocator.usage();
568         } else {
569             panic!("usage error");
570         }
571     }
572 }
573 
574 impl LockedFrameAllocator {
575     pub fn get_usage(&self) -> PageFrameUsage {
576         unsafe { self.usage() }
577     }
578 }
579 
580 /// 获取内核地址默认的页面标志
581 pub unsafe fn kernel_page_flags<A: MemoryManagementArch>(virt: VirtAddr) -> PageFlags<A> {
582     let info: X86_64MMBootstrapInfo = BOOTSTRAP_MM_INFO.clone().unwrap();
583 
584     if virt.data() >= info.kernel_code_start && virt.data() < info.kernel_code_end {
585         // Remap kernel code  execute
586         return PageFlags::new().set_execute(true).set_write(true);
587     } else if virt.data() >= info.kernel_data_end && virt.data() < info.kernel_rodata_end {
588         // Remap kernel rodata read only
589         return PageFlags::new().set_execute(true);
590     } else {
591         return PageFlags::new().set_write(true).set_execute(true);
592     }
593 }
594 
595 unsafe fn set_inner_allocator(allocator: BuddyAllocator<MMArch>) {
596     static FLAG: AtomicBool = AtomicBool::new(false);
597     if FLAG
598         .compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst)
599         .is_err()
600     {
601         panic!("Cannot set inner allocator twice!");
602     }
603     *INNER_ALLOCATOR.lock() = Some(allocator);
604 }
605 
606 /// 低地址重映射的管理器
607 ///
608 /// 低地址重映射的管理器,在smp初始化完成之前,需要使用低地址的映射,因此需要在smp初始化完成之后,取消这一段映射
609 pub struct LowAddressRemapping;
610 
611 impl LowAddressRemapping {
612     // 映射32M
613     const REMAP_SIZE: usize = 32 * 1024 * 1024;
614 
615     pub unsafe fn remap_at_low_address(
616         mapper: &mut crate::mm::page::PageMapper<MMArch, &mut BumpAllocator<MMArch>>,
617     ) {
618         for i in 0..(Self::REMAP_SIZE / MMArch::PAGE_SIZE) {
619             let paddr = PhysAddr::new(i * MMArch::PAGE_SIZE);
620             let vaddr = VirtAddr::new(i * MMArch::PAGE_SIZE);
621             let flags = kernel_page_flags::<MMArch>(vaddr);
622 
623             let flusher = mapper
624                 .map_phys(vaddr, paddr, flags)
625                 .expect("Failed to map frame");
626             // 暂时不刷新TLB
627             flusher.ignore();
628         }
629     }
630 
631     /// 取消低地址的映射
632     pub unsafe fn unmap_at_low_address(flush: bool) {
633         let mut mapper = KernelMapper::lock();
634         assert!(mapper.as_mut().is_some());
635         for i in 0..(Self::REMAP_SIZE / MMArch::PAGE_SIZE) {
636             let vaddr = VirtAddr::new(i * MMArch::PAGE_SIZE);
637             let (_, _, flusher) = mapper
638                 .as_mut()
639                 .unwrap()
640                 .unmap_phys(vaddr, true)
641                 .expect("Failed to unmap frame");
642             if flush == false {
643                 flusher.ignore();
644             }
645         }
646     }
647 }
648 #[no_mangle]
649 pub extern "C" fn rs_mm_init() {
650     mm_init();
651 }
652