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 /// 前面可能会有一个架构相关的函数
start_kernel() -> !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)]
do_start_kernel()55 fn do_start_kernel() {
56 init_before_mem_init();
57
58 unsafe { mm_init() };
59
60 // crate::debug::jump_label::static_keys_init();
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 Futex::init();
93 crate::bpf::init_bpf_system();
94 crate::debug::jump_label::static_keys_init();
95 #[cfg(all(target_arch = "x86_64", feature = "kvm"))]
96 crate::virt::kvm::kvm_init();
97 }
98
99 /// 在内存管理初始化之前,执行的初始化
100 #[inline(never)]
init_before_mem_init()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