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