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