xref: /DragonOS/kernel/src/init/initial_kthread.rs (revision c75ef4e2126c180bf04c08635ffa5a278619c035)
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::{
9         disk::ahci::ahci_init, net::e1000e::e1000e::e1000e_init, virtio::virtio::virtio_probe,
10     },
11     filesystem::vfs::core::mount_root_fs,
12     kdebug, kerror,
13     net::net_core::net_init,
14     process::{kthread::KernelThreadMechanism, process::stdio_init},
15 };
16 
17 use super::initcall::do_initcalls;
18 
19 pub fn initial_kernel_thread() -> i32 {
20     kernel_init().unwrap_or_else(|err| {
21         panic!("Failed to initialize kernel: {:?}", err);
22     });
23 
24     switch_to_user();
25 
26     loop {}
27 }
28 
29 fn kernel_init() -> Result<(), SystemError> {
30     kenrel_init_freeable()?;
31 
32     KernelThreadMechanism::init_stage2();
33 
34     // 由于目前加锁,速度过慢,所以先不开启双缓冲
35     // scm_enable_double_buffer().expect("Failed to enable double buffer");
36     stdio_init().expect("Failed to initialize stdio");
37 
38     ahci_init().expect("Failed to initialize AHCI");
39 
40     mount_root_fs().expect("Failed to mount root fs");
41 
42     virtio_probe();
43     e1000e_init();
44     net_init().unwrap_or_else(|err| {
45         kerror!("Failed to initialize network: {:?}", err);
46     });
47 
48     kdebug!("initial kernel thread done.");
49 
50     return Ok(());
51 }
52 
53 #[inline(never)]
54 fn kenrel_init_freeable() -> Result<(), SystemError> {
55     do_initcalls().unwrap_or_else(|err| {
56         panic!("Failed to initialize subsystems: {:?}", err);
57     });
58 
59     return Ok(());
60 }
61 
62 /// 切换到用户态
63 fn switch_to_user() {
64     let path = String::from("/bin/DragonReach");
65     let argv = vec![String::from("/bin/DragonReach")];
66     let envp = vec![String::from("PATH=/")];
67 
68     unsafe { arch_switch_to_user(path, argv, envp) };
69 }
70