xref: /DragonOS/kernel/src/mm/init.rs (revision 9430523b465b19db4dd476e9fd3038bdc2aa0c8d)
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     libs::printk::PrintkWriter,
8     mm::{mmio_buddy::mmio_init, page::page_manager_init},
9 };
10 
11 use super::MemoryManagementArch;
12 
13 #[atomic_enum]
14 #[derive(PartialEq, Eq)]
15 pub enum MMInitStatus {
16     NotInit,
17     Initializing,
18     Initialized,
19 }
20 /// 内存管理的初始化状态
21 static MM_INIT: AtomicMMInitStatus = AtomicMMInitStatus::new(MMInitStatus::NotInit);
22 
23 #[inline(never)]
24 pub unsafe fn mm_init() {
25     send_to_default_serial8250_port("mm_init\n\0".as_bytes());
26     PrintkWriter
27         .write_fmt(format_args!("mm_init() called\n"))
28         .unwrap();
29     // printk_color!(GREEN, BLACK, "mm_init() called\n");
30 
31     if MM_INIT
32         .compare_exchange(
33             MMInitStatus::NotInit,
34             MMInitStatus::Initializing,
35             Ordering::SeqCst,
36             Ordering::SeqCst,
37         )
38         .is_err()
39     {
40         send_to_default_serial8250_port("mm_init err\n\0".as_bytes());
41         panic!("mm_init() can only be called once");
42     }
43 
44     MMArch::init();
45 
46     // enable mmio
47     mmio_init();
48     // enable KMSG
49     kmsg_init();
50     // enable PAGE_MANAGER
51     page_manager_init();
52 
53     MM_INIT
54         .compare_exchange(
55             MMInitStatus::Initializing,
56             MMInitStatus::Initialized,
57             Ordering::SeqCst,
58             Ordering::SeqCst,
59         )
60         .unwrap();
61     kinfo!("mm init done.");
62 }
63 
64 /// 获取内存管理的初始化状态
65 pub fn mm_init_status() -> MMInitStatus {
66     MM_INIT.load(Ordering::SeqCst)
67 }
68