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