xref: /DragonOS/kernel/src/init/init.rs (revision 840045af94ea3391f29e87e968db5d9c48316981)
1 use crate::{
2     arch::{
3         init::{early_setup_arch, setup_arch, setup_arch_post},
4         CurrentIrqArch, CurrentSMPArch, CurrentSchedArch,
5     },
6     driver::{base::init::driver_init, serial::serial_early_init, video::VideoRefreshManager},
7     exception::{init::irq_init, softirq::softirq_init, InterruptArch},
8     filesystem::vfs::core::vfs_init,
9     include::bindings::bindings::acpi_init,
10     init::init_intertrait,
11     libs::{
12         futex::futex::Futex,
13         lib_ui::{
14             screen_manager::{scm_init, scm_reinit},
15             textui::textui_init,
16         },
17     },
18     mm::init::mm_init,
19     process::{kthread::kthread_init, process_init, ProcessManager},
20     sched::{core::sched_init, SchedArch},
21     smp::{early_smp_init, SMPArch},
22     syscall::Syscall,
23     time::{
24         clocksource::clocksource_boot_finish, timekeeping::timekeeping_init, timer::timer_init,
25     },
26 };
27 
28 /// The entry point for the kernel
29 ///
30 /// 前面可能会有一个架构相关的函数
31 pub fn start_kernel() -> ! {
32     // 进入内核后,中断应该是关闭的
33     assert_eq!(CurrentIrqArch::is_irq_enabled(), false);
34 
35     do_start_kernel();
36 
37     CurrentSchedArch::initial_setup_sched_local();
38 
39     CurrentSchedArch::enable_sched_local();
40 
41     ProcessManager::arch_idle_func();
42 }
43 
44 #[inline(never)]
45 fn do_start_kernel() {
46     init_before_mem_init();
47 
48     early_setup_arch().expect("setup_arch failed");
49     unsafe { mm_init() };
50 
51     scm_reinit().unwrap();
52     textui_init().unwrap();
53     init_intertrait();
54 
55     vfs_init().expect("vfs init failed");
56     driver_init().expect("driver init failed");
57 
58     #[cfg(target_arch = "x86_64")]
59     unsafe {
60         acpi_init()
61     };
62 
63     early_smp_init().expect("early smp init failed");
64     irq_init().expect("irq init failed");
65     setup_arch().expect("setup_arch failed");
66     CurrentSMPArch::prepare_cpus().expect("prepare_cpus failed");
67 
68     process_init();
69     sched_init();
70     softirq_init().expect("softirq init failed");
71     Syscall::init().expect("syscall init failed");
72     timekeeping_init();
73     timer_init();
74     kthread_init();
75     clocksource_boot_finish();
76 
77     CurrentSMPArch::init().expect("smp init failed");
78     // SMP初始化有可能会开中断,所以这里再次检查中断是否关闭
79     assert_eq!(CurrentIrqArch::is_irq_enabled(), false);
80     Futex::init();
81 
82     setup_arch_post().expect("setup_arch_post failed");
83 
84     #[cfg(all(target_arch = "x86_64", feature = "kvm"))]
85     crate::virt::kvm::kvm_init();
86 }
87 
88 /// 在内存管理初始化之前,执行的初始化
89 #[inline(never)]
90 fn init_before_mem_init() {
91     serial_early_init().expect("serial early init failed");
92     let video_ok = unsafe { VideoRefreshManager::video_init().is_ok() };
93     scm_init(video_ok);
94 }
95