xref: /DragonOS/kernel/src/init/init.rs (revision 9993c0fc61e9603f631bd6748ff0b4fecb7bd483)
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, tty::init::tty_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::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     scm_reinit().unwrap();
51     textui_init().unwrap();
52     init_intertrait();
53     vfs_init().expect("vfs init failed");
54     driver_init().expect("driver init failed");
55     unsafe { acpi_init() };
56     irq_init().expect("irq init failed");
57     CurrentSMPArch::prepare_cpus().expect("prepare_cpus failed");
58 
59     setup_arch().expect("setup_arch failed");
60 
61     process_init();
62     sched_init();
63     softirq_init().expect("softirq init failed");
64     Syscall::init().expect("syscall init failed");
65     timekeeping_init();
66     timer_init();
67     kthread_init();
68     clocksource_boot_finish();
69 
70     CurrentSMPArch::init().expect("smp init failed");
71     // SMP初始化有可能会开中断,所以这里再次检查中断是否关闭
72     assert_eq!(CurrentIrqArch::is_irq_enabled(), false);
73     Futex::init();
74 
75     setup_arch_post().expect("setup_arch_post failed");
76 
77     #[cfg(all(target_arch = "x86_64", feature = "kvm"))]
78     crate::virt::kvm::kvm_init();
79 }
80 
81 /// 在内存管理初始化之前,执行的初始化
82 #[inline(never)]
83 fn init_before_mem_init() {
84     tty_early_init().expect("tty early init failed");
85     let video_ok = unsafe { VideoRefreshManager::video_init().is_ok() };
86     scm_init(video_ok);
87 }
88