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