xref: /DragonOS/kernel/src/init/init.rs (revision 9fab312ea9921618629924ab15c28c2d255b21c6)
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::{base::init::driver_init, serial::serial_early_init, video::VideoRefreshManager},
8     exception::{init::irq_init, softirq::softirq_init, InterruptArch},
9     filesystem::vfs::core::vfs_init,
10     include::bindings::bindings::acpi_init,
11     init::init_intertrait,
12     libs::{
13         futex::futex::Futex,
14         lib_ui::{
15             screen_manager::{scm_init, scm_reinit},
16             textui::textui_init,
17         },
18     },
19     mm::init::mm_init,
20     process::{kthread::kthread_init, process_init, ProcessManager},
21     sched::SchedArch,
22     smp::{early_smp_init, SMPArch},
23     syscall::Syscall,
24     time::{
25         clocksource::clocksource_boot_finish, timekeeping::timekeeping_init, timer::timer_init,
26     },
27 };
28 
29 /// The entry point for the kernel
30 ///
31 /// 前面可能会有一个架构相关的函数
32 pub fn start_kernel() -> ! {
33     // 进入内核后,中断应该是关闭的
34     assert!(!CurrentIrqArch::is_irq_enabled());
35 
36     do_start_kernel();
37 
38     CurrentSchedArch::initial_setup_sched_local();
39 
40     CurrentSchedArch::enable_sched_local();
41 
42     ProcessManager::arch_idle_func();
43 }
44 
45 #[inline(never)]
46 fn do_start_kernel() {
47     init_before_mem_init();
48 
49     early_setup_arch().expect("setup_arch failed");
50     unsafe { mm_init() };
51 
52     scm_reinit().unwrap();
53     textui_init().unwrap();
54     init_intertrait();
55 
56     vfs_init().expect("vfs init failed");
57     driver_init().expect("driver init failed");
58 
59     #[cfg(target_arch = "x86_64")]
60     unsafe {
61         acpi_init()
62     };
63     crate::sched::sched_init();
64     process_init();
65     early_smp_init().expect("early smp init failed");
66     irq_init().expect("irq init failed");
67     setup_arch().expect("setup_arch failed");
68     CurrentSMPArch::prepare_cpus().expect("prepare_cpus failed");
69 
70     // sched_init();
71     softirq_init().expect("softirq init failed");
72     Syscall::init().expect("syscall init failed");
73     timekeeping_init();
74     time_init();
75     timer_init();
76     kthread_init();
77     clocksource_boot_finish();
78 
79     Futex::init();
80 
81     setup_arch_post().expect("setup_arch_post failed");
82 
83     #[cfg(all(target_arch = "x86_64", feature = "kvm"))]
84     crate::virt::kvm::kvm_init();
85 }
86 
87 /// 在内存管理初始化之前,执行的初始化
88 #[inline(never)]
89 fn init_before_mem_init() {
90     serial_early_init().expect("serial early init failed");
91     let video_ok = unsafe { VideoRefreshManager::video_init().is_ok() };
92     scm_init(video_ok);
93 }
94