xref: /DragonOS/kernel/src/init/init.rs (revision da152319797436368304cbc3f85a3b9ec049134b)
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!(!CurrentIrqArch::is_irq_enabled());
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     process_init();
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     sched_init();
69     softirq_init().expect("softirq init failed");
70     Syscall::init().expect("syscall init failed");
71     timekeeping_init();
72     timer_init();
73     kthread_init();
74     clocksource_boot_finish();
75 
76     Futex::init();
77 
78     setup_arch_post().expect("setup_arch_post failed");
79 
80     #[cfg(all(target_arch = "x86_64", feature = "kvm"))]
81     crate::virt::kvm::kvm_init();
82 }
83 
84 /// 在内存管理初始化之前,执行的初始化
85 #[inline(never)]
86 fn init_before_mem_init() {
87     serial_early_init().expect("serial early init failed");
88     let video_ok = unsafe { VideoRefreshManager::video_init().is_ok() };
89     scm_init(video_ok);
90 }
91