1453452ccSLoGin use core::{fmt::Write, sync::atomic::Ordering}; 2453452ccSLoGin 3*2eab6dd7S曾俊 use log::info; 4*2eab6dd7S曾俊 5453452ccSLoGin use crate::{ 656cc4dbeSJomo arch::MMArch, 756cc4dbeSJomo driver::serial::serial8250::send_to_default_serial8250_port, 856cc4dbeSJomo filesystem::procfs::kmsg::kmsg_init, 96fc066acSJomo ipc::shm::shm_manager_init, 1056cc4dbeSJomo libs::printk::PrintkWriter, 11ceeb2e94Slaokengwt mm::{allocator::slab::slab_init, mmio_buddy::mmio_init, page::page_manager_init}, 12453452ccSLoGin }; 13453452ccSLoGin 14453452ccSLoGin use super::MemoryManagementArch; 15453452ccSLoGin 16453452ccSLoGin #[atomic_enum] 17453452ccSLoGin #[derive(PartialEq, Eq)] 18453452ccSLoGin pub enum MMInitStatus { 19453452ccSLoGin NotInit, 20453452ccSLoGin Initializing, 21453452ccSLoGin Initialized, 22453452ccSLoGin } 23453452ccSLoGin /// 内存管理的初始化状态 24453452ccSLoGin static MM_INIT: AtomicMMInitStatus = AtomicMMInitStatus::new(MMInitStatus::NotInit); 25453452ccSLoGin 265b59005fSLoGin #[inline(never)] 27453452ccSLoGin pub unsafe fn mm_init() { 28453452ccSLoGin send_to_default_serial8250_port("mm_init\n\0".as_bytes()); 29453452ccSLoGin PrintkWriter 30453452ccSLoGin .write_fmt(format_args!("mm_init() called\n")) 31453452ccSLoGin .unwrap(); 32453452ccSLoGin // printk_color!(GREEN, BLACK, "mm_init() called\n"); 33453452ccSLoGin 34453452ccSLoGin if MM_INIT 35453452ccSLoGin .compare_exchange( 36453452ccSLoGin MMInitStatus::NotInit, 37453452ccSLoGin MMInitStatus::Initializing, 38453452ccSLoGin Ordering::SeqCst, 39453452ccSLoGin Ordering::SeqCst, 40453452ccSLoGin ) 41453452ccSLoGin .is_err() 42453452ccSLoGin { 43453452ccSLoGin send_to_default_serial8250_port("mm_init err\n\0".as_bytes()); 44453452ccSLoGin panic!("mm_init() can only be called once"); 45453452ccSLoGin } 46453452ccSLoGin 47453452ccSLoGin MMArch::init(); 48453452ccSLoGin 49ceeb2e94Slaokengwt // init slab 50ceeb2e94Slaokengwt slab_init(); 51ceeb2e94Slaokengwt 52453452ccSLoGin // enable mmio 53453452ccSLoGin mmio_init(); 54453452ccSLoGin // enable KMSG 55453452ccSLoGin kmsg_init(); 5656cc4dbeSJomo // enable PAGE_MANAGER 5756cc4dbeSJomo page_manager_init(); 586fc066acSJomo // enable SHM_MANAGER 596fc066acSJomo shm_manager_init(); 60453452ccSLoGin 61453452ccSLoGin MM_INIT 62453452ccSLoGin .compare_exchange( 63453452ccSLoGin MMInitStatus::Initializing, 64453452ccSLoGin MMInitStatus::Initialized, 65453452ccSLoGin Ordering::SeqCst, 66453452ccSLoGin Ordering::SeqCst, 67453452ccSLoGin ) 68453452ccSLoGin .unwrap(); 6923ef2b33SLoGin MMArch::arch_post_init(); 70*2eab6dd7S曾俊 info!("mm init done."); 71453452ccSLoGin } 72453452ccSLoGin 73453452ccSLoGin /// 获取内存管理的初始化状态 74453452ccSLoGin pub fn mm_init_status() -> MMInitStatus { 75453452ccSLoGin MM_INIT.load(Ordering::SeqCst) 76453452ccSLoGin } 77