xref: /DragonOS/kernel/src/init/init.rs (revision 55e6f0b65f91b32638fd56581f711a816eccdcd1)
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 use log::warn;
32 
33 use super::{
34     boot::{boot_callback_except_early, boot_callbacks},
35     cmdline::kenrel_cmdline_param_manager,
36 };
37 
38 /// The entry point for the kernel
39 ///
40 /// 前面可能会有一个架构相关的函数
41 pub fn start_kernel() -> ! {
42     // 进入内核后,中断应该是关闭的
43     assert!(!CurrentIrqArch::is_irq_enabled());
44 
45     do_start_kernel();
46 
47     CurrentSchedArch::initial_setup_sched_local();
48 
49     CurrentSchedArch::enable_sched_local();
50 
51     ProcessManager::arch_idle_func();
52 }
53 
54 #[inline(never)]
55 fn do_start_kernel() {
56     init_before_mem_init();
57 
58     unsafe { mm_init() };
59 
60     if scm_reinit().is_ok() {
61         if let Err(e) = textui_init() {
62             warn!("Failed to init textui: {:?}", e);
63         }
64     }
65     // 初始化内核命令行参数
66     kenrel_cmdline_param_manager().init();
67     boot_callback_except_early();
68 
69     init_intertrait();
70 
71     vfs_init().expect("vfs init failed");
72     driver_init().expect("driver init failed");
73 
74     acpi_init().expect("acpi init failed");
75     crate::sched::sched_init();
76     process_init();
77     early_smp_init().expect("early smp init failed");
78     irq_init().expect("irq init failed");
79     setup_arch().expect("setup_arch failed");
80     CurrentSMPArch::prepare_cpus().expect("prepare_cpus failed");
81 
82     // sched_init();
83     softirq_init().expect("softirq init failed");
84     Syscall::init().expect("syscall init failed");
85     timekeeping_init();
86     time_init();
87     timer_init();
88     kthread_init();
89     setup_arch_post().expect("setup_arch_post failed");
90     clocksource_boot_finish();
91     Futex::init();
92     crate::bpf::init_bpf_system();
93     #[cfg(all(target_arch = "x86_64", feature = "kvm"))]
94     crate::virt::kvm::kvm_init();
95 }
96 
97 /// 在内存管理初始化之前,执行的初始化
98 #[inline(never)]
99 fn init_before_mem_init() {
100     serial_early_init().expect("serial early init failed");
101     let video_ok = unsafe { VideoRefreshManager::video_init().is_ok() };
102     scm_init(video_ok);
103 
104     early_init_logging();
105 
106     early_setup_arch().expect("setup_arch failed");
107 
108     boot_callbacks()
109         .init_kernel_cmdline()
110         .inspect_err(|e| {
111             log::error!("Failed to init kernel cmdline: {:?}", e);
112         })
113         .ok();
114     kenrel_cmdline_param_manager().early_init();
115 }
116