xref: /DragonOS/kernel/src/init/initial_kthread.rs (revision 173c4567cf4fb2276ef3f4614b69da7913fc8381)
1 //! 这个文件内放置初始内核线程的代码。
2 
3 use alloc::string::String;
4 use system_error::SystemError;
5 
6 use crate::{
7     arch::process::arch_switch_to_user,
8     driver::{net::e1000e::e1000e::e1000e_init, virtio::virtio::virtio_probe},
9     filesystem::vfs::core::mount_root_fs,
10     kdebug, kerror,
11     net::net_core::net_init,
12     process::{kthread::KernelThreadMechanism, stdio::stdio_init},
13     smp::smp_init,
14 };
15 
16 use super::initcall::do_initcalls;
17 
18 pub fn initial_kernel_thread() -> i32 {
19     kernel_init().unwrap_or_else(|err| {
20         panic!("Failed to initialize kernel: {:?}", err);
21     });
22 
23     switch_to_user();
24 
25     unreachable!();
26 }
27 
28 fn kernel_init() -> Result<(), SystemError> {
29     KernelThreadMechanism::init_stage2();
30     kenrel_init_freeable()?;
31 
32     // 由于目前加锁,速度过慢,所以先不开启双缓冲
33     // scm_enable_double_buffer().expect("Failed to enable double buffer");
34 
35     #[cfg(target_arch = "x86_64")]
36     crate::driver::disk::ahci::ahci_init().expect("Failed to initialize AHCI");
37 
38     virtio_probe();
39     mount_root_fs().expect("Failed to mount root fs");
40 
41     e1000e_init();
42     net_init().unwrap_or_else(|err| {
43         kerror!("Failed to initialize network: {:?}", err);
44     });
45 
46     kdebug!("initial kernel thread done.");
47 
48     return Ok(());
49 }
50 
51 #[inline(never)]
52 fn kenrel_init_freeable() -> Result<(), SystemError> {
53     do_initcalls().unwrap_or_else(|err| {
54         panic!("Failed to initialize subsystems: {:?}", err);
55     });
56     stdio_init().expect("Failed to initialize stdio");
57     smp_init();
58 
59     return Ok(());
60 }
61 
62 /// 切换到用户态
63 fn switch_to_user() {
64     const INIT_PROGRAM: &str = "/bin/dragonreach";
65     let path = String::from(INIT_PROGRAM);
66     let argv = vec![path.clone()];
67     let envp = vec![String::from("PATH=/")];
68 
69     unsafe { arch_switch_to_user(path, argv, envp) };
70 }
71