15b59005fSLoGin use crate::{
25b59005fSLoGin arch::{
35b59005fSLoGin init::{early_setup_arch, setup_arch, setup_arch_post},
423ef2b33SLoGin time::time_init,
55b59005fSLoGin CurrentIrqArch, CurrentSMPArch, CurrentSchedArch,
65b59005fSLoGin },
72b7818e8SLoGin driver::{
82b7818e8SLoGin acpi::acpi_init, base::init::driver_init, serial::serial_early_init,
92b7818e8SLoGin video::VideoRefreshManager,
102b7818e8SLoGin },
11f2022a8aSLoGin exception::{init::irq_init, softirq::softirq_init, InterruptArch},
125b59005fSLoGin filesystem::vfs::core::vfs_init,
135b59005fSLoGin init::init_intertrait,
145b59005fSLoGin libs::{
155b59005fSLoGin futex::futex::Futex,
165b59005fSLoGin lib_ui::{
175b59005fSLoGin screen_manager::{scm_init, scm_reinit},
185b59005fSLoGin textui::textui_init,
195b59005fSLoGin },
20731bc2b3SLoGin printk::early_init_logging,
215b59005fSLoGin },
225b59005fSLoGin mm::init::mm_init,
235b59005fSLoGin process::{kthread::kthread_init, process_init, ProcessManager},
24f0c87a89SGnoCiYeH sched::SchedArch,
25338f6903SLoGin smp::{early_smp_init, SMPArch},
265b59005fSLoGin syscall::Syscall,
275b59005fSLoGin time::{
285b59005fSLoGin clocksource::clocksource_boot_finish, timekeeping::timekeeping_init, timer::timer_init,
295b59005fSLoGin },
305b59005fSLoGin };
31*fae6e9adSlinfeng use log::warn;
325b59005fSLoGin
33f9fe30beSLoGin use super::{
34f9fe30beSLoGin boot::{boot_callback_except_early, boot_callbacks},
35f9fe30beSLoGin cmdline::kenrel_cmdline_param_manager,
36f9fe30beSLoGin };
372b7818e8SLoGin
385b59005fSLoGin /// The entry point for the kernel
395b59005fSLoGin ///
405b59005fSLoGin /// 前面可能会有一个架构相关的函数
start_kernel() -> !415b59005fSLoGin pub fn start_kernel() -> ! {
425b59005fSLoGin // 进入内核后,中断应该是关闭的
43b5b571e0SLoGin assert!(!CurrentIrqArch::is_irq_enabled());
445b59005fSLoGin
455b59005fSLoGin do_start_kernel();
465b59005fSLoGin
475b59005fSLoGin CurrentSchedArch::initial_setup_sched_local();
485b59005fSLoGin
495b59005fSLoGin CurrentSchedArch::enable_sched_local();
505b59005fSLoGin
515b59005fSLoGin ProcessManager::arch_idle_func();
525b59005fSLoGin }
535b59005fSLoGin
545b59005fSLoGin #[inline(never)]
do_start_kernel()555b59005fSLoGin fn do_start_kernel() {
565b59005fSLoGin init_before_mem_init();
57cb23beb2SLoGin
585b59005fSLoGin unsafe { mm_init() };
59338f6903SLoGin
6059a6bcf6SLoGin if scm_reinit().is_ok() {
6159a6bcf6SLoGin if let Err(e) = textui_init() {
6259a6bcf6SLoGin warn!("Failed to init textui: {:?}", e);
6359a6bcf6SLoGin }
6459a6bcf6SLoGin }
65f9fe30beSLoGin // 初始化内核命令行参数
66f9fe30beSLoGin kenrel_cmdline_param_manager().init();
672b7818e8SLoGin boot_callback_except_early();
68f9fe30beSLoGin
695b59005fSLoGin init_intertrait();
70338f6903SLoGin
715b59005fSLoGin vfs_init().expect("vfs init failed");
725b59005fSLoGin driver_init().expect("driver init failed");
73338f6903SLoGin
742b7818e8SLoGin acpi_init().expect("acpi init failed");
75f0c87a89SGnoCiYeH crate::sched::sched_init();
768cb2e9b3SLoGin process_init();
77338f6903SLoGin early_smp_init().expect("early smp init failed");
78f2022a8aSLoGin irq_init().expect("irq init failed");
795b59005fSLoGin setup_arch().expect("setup_arch failed");
805c4224e5SLoGin CurrentSMPArch::prepare_cpus().expect("prepare_cpus failed");
81f2022a8aSLoGin
82f0c87a89SGnoCiYeH // sched_init();
835b59005fSLoGin softirq_init().expect("softirq init failed");
845b59005fSLoGin Syscall::init().expect("syscall init failed");
855b59005fSLoGin timekeeping_init();
8623ef2b33SLoGin time_init();
875b59005fSLoGin timer_init();
885b59005fSLoGin kthread_init();
8992deae63SLoGin setup_arch_post().expect("setup_arch_post failed");
905b59005fSLoGin clocksource_boot_finish();
915b59005fSLoGin Futex::init();
92*fae6e9adSlinfeng crate::bpf::init_bpf_system();
935b59005fSLoGin #[cfg(all(target_arch = "x86_64", feature = "kvm"))]
945b59005fSLoGin crate::virt::kvm::kvm_init();
955b59005fSLoGin }
965b59005fSLoGin
975b59005fSLoGin /// 在内存管理初始化之前,执行的初始化
985b59005fSLoGin #[inline(never)]
init_before_mem_init()995b59005fSLoGin fn init_before_mem_init() {
10052da9a59SGnoCiYeH serial_early_init().expect("serial early init failed");
1015b59005fSLoGin let video_ok = unsafe { VideoRefreshManager::video_init().is_ok() };
1025b59005fSLoGin scm_init(video_ok);
103f9fe30beSLoGin
104f9fe30beSLoGin early_init_logging();
105f9fe30beSLoGin
106f9fe30beSLoGin early_setup_arch().expect("setup_arch failed");
107f9fe30beSLoGin
108f9fe30beSLoGin boot_callbacks()
109f9fe30beSLoGin .init_kernel_cmdline()
110f9fe30beSLoGin .inspect_err(|e| {
111f9fe30beSLoGin log::error!("Failed to init kernel cmdline: {:?}", e);
112f9fe30beSLoGin })
113f9fe30beSLoGin .ok();
114f9fe30beSLoGin kenrel_cmdline_param_manager().early_init();
1155b59005fSLoGin }
116