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