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