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