xref: /DragonOS/kernel/src/mm/init.rs (revision 4b0170bd6bb374d0e9699a0076cc23b976ad6db7)
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::{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     // enable mmio
48     mmio_init();
49     // enable KMSG
50     kmsg_init();
51     // enable PAGE_MANAGER
52     page_manager_init();
53     // enable SHM_MANAGER
54     shm_manager_init();
55 
56     MM_INIT
57         .compare_exchange(
58             MMInitStatus::Initializing,
59             MMInitStatus::Initialized,
60             Ordering::SeqCst,
61             Ordering::SeqCst,
62         )
63         .unwrap();
64     MMArch::arch_post_init();
65     kinfo!("mm init done.");
66 }
67 
68 /// 获取内存管理的初始化状态
69 pub fn mm_init_status() -> MMInitStatus {
70     MM_INIT.load(Ordering::SeqCst)
71 }
72