1 use core::{fmt::Write, sync::atomic::Ordering}; 2 3 use crate::{ 4 arch::MMArch, 5 driver::serial::serial8250::send_to_default_serial8250_port, 6 filesystem::procfs::kmsg::kmsg_init, 7 ipc::shm::shm_manager_init, 8 libs::printk::PrintkWriter, 9 mm::{allocator::slab::slab_init, mmio_buddy::mmio_init, page::page_manager_init}, 10 }; 11 12 use super::MemoryManagementArch; 13 14 #[atomic_enum] 15 #[derive(PartialEq, Eq)] 16 pub enum MMInitStatus { 17 NotInit, 18 Initializing, 19 Initialized, 20 } 21 /// 内存管理的初始化状态 22 static MM_INIT: AtomicMMInitStatus = AtomicMMInitStatus::new(MMInitStatus::NotInit); 23 24 #[inline(never)] 25 pub unsafe fn mm_init() { 26 send_to_default_serial8250_port("mm_init\n\0".as_bytes()); 27 PrintkWriter 28 .write_fmt(format_args!("mm_init() called\n")) 29 .unwrap(); 30 // printk_color!(GREEN, BLACK, "mm_init() called\n"); 31 32 if MM_INIT 33 .compare_exchange( 34 MMInitStatus::NotInit, 35 MMInitStatus::Initializing, 36 Ordering::SeqCst, 37 Ordering::SeqCst, 38 ) 39 .is_err() 40 { 41 send_to_default_serial8250_port("mm_init err\n\0".as_bytes()); 42 panic!("mm_init() can only be called once"); 43 } 44 45 MMArch::init(); 46 47 // init slab 48 slab_init(); 49 50 // enable mmio 51 mmio_init(); 52 // enable KMSG 53 kmsg_init(); 54 // enable PAGE_MANAGER 55 page_manager_init(); 56 // enable SHM_MANAGER 57 shm_manager_init(); 58 59 MM_INIT 60 .compare_exchange( 61 MMInitStatus::Initializing, 62 MMInitStatus::Initialized, 63 Ordering::SeqCst, 64 Ordering::SeqCst, 65 ) 66 .unwrap(); 67 MMArch::arch_post_init(); 68 kinfo!("mm init done."); 69 } 70 71 /// 获取内存管理的初始化状态 72 pub fn mm_init_status() -> MMInitStatus { 73 MM_INIT.load(Ordering::SeqCst) 74 } 75