1453452ccSLoGin use core::{fmt::Write, sync::atomic::Ordering};
2453452ccSLoGin
32eab6dd7S曾俊 use log::info;
42eab6dd7S曾俊
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,
11*cf7f801eSMemoryShore mm::{
12*cf7f801eSMemoryShore allocator::slab::slab_init,
13*cf7f801eSMemoryShore mmio_buddy::mmio_init,
14*cf7f801eSMemoryShore page::{page_manager_init, page_reclaimer_init},
15*cf7f801eSMemoryShore },
16453452ccSLoGin };
17453452ccSLoGin
18453452ccSLoGin use super::MemoryManagementArch;
19453452ccSLoGin
20453452ccSLoGin #[atomic_enum]
21453452ccSLoGin #[derive(PartialEq, Eq)]
22453452ccSLoGin pub enum MMInitStatus {
23453452ccSLoGin NotInit,
24453452ccSLoGin Initializing,
25453452ccSLoGin Initialized,
26453452ccSLoGin }
27453452ccSLoGin /// 内存管理的初始化状态
28453452ccSLoGin static MM_INIT: AtomicMMInitStatus = AtomicMMInitStatus::new(MMInitStatus::NotInit);
29453452ccSLoGin
305b59005fSLoGin #[inline(never)]
mm_init()31453452ccSLoGin pub unsafe fn mm_init() {
32453452ccSLoGin send_to_default_serial8250_port("mm_init\n\0".as_bytes());
33453452ccSLoGin PrintkWriter
34453452ccSLoGin .write_fmt(format_args!("mm_init() called\n"))
35453452ccSLoGin .unwrap();
36453452ccSLoGin // printk_color!(GREEN, BLACK, "mm_init() called\n");
37453452ccSLoGin
38453452ccSLoGin if MM_INIT
39453452ccSLoGin .compare_exchange(
40453452ccSLoGin MMInitStatus::NotInit,
41453452ccSLoGin MMInitStatus::Initializing,
42453452ccSLoGin Ordering::SeqCst,
43453452ccSLoGin Ordering::SeqCst,
44453452ccSLoGin )
45453452ccSLoGin .is_err()
46453452ccSLoGin {
47453452ccSLoGin send_to_default_serial8250_port("mm_init err\n\0".as_bytes());
48453452ccSLoGin panic!("mm_init() can only be called once");
49453452ccSLoGin }
50453452ccSLoGin
51453452ccSLoGin MMArch::init();
52453452ccSLoGin
53ceeb2e94Slaokengwt // init slab
54ceeb2e94Slaokengwt slab_init();
55ceeb2e94Slaokengwt
56453452ccSLoGin // enable mmio
57453452ccSLoGin mmio_init();
58453452ccSLoGin // enable KMSG
59453452ccSLoGin kmsg_init();
6056cc4dbeSJomo // enable PAGE_MANAGER
6156cc4dbeSJomo page_manager_init();
626fc066acSJomo // enable SHM_MANAGER
636fc066acSJomo shm_manager_init();
64*cf7f801eSMemoryShore // enable PAGE_RECLAIMER
65*cf7f801eSMemoryShore page_reclaimer_init();
66453452ccSLoGin
67453452ccSLoGin MM_INIT
68453452ccSLoGin .compare_exchange(
69453452ccSLoGin MMInitStatus::Initializing,
70453452ccSLoGin MMInitStatus::Initialized,
71453452ccSLoGin Ordering::SeqCst,
72453452ccSLoGin Ordering::SeqCst,
73453452ccSLoGin )
74453452ccSLoGin .unwrap();
7523ef2b33SLoGin MMArch::arch_post_init();
762eab6dd7S曾俊 info!("mm init done.");
77453452ccSLoGin }
78453452ccSLoGin
79453452ccSLoGin /// 获取内存管理的初始化状态
mm_init_status() -> MMInitStatus80453452ccSLoGin pub fn mm_init_status() -> MMInitStatus {
81453452ccSLoGin MM_INIT.load(Ordering::SeqCst)
82453452ccSLoGin }
83