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 
initial_kernel_thread() -> i3219 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 
kernel_init() -> Result<(), SystemError>29 fn kernel_init() -> Result<(), SystemError> {
30     KernelThreadMechanism::init_stage2();
31     kenrel_init_freeable()?;
32 
33     // 由于目前加锁,速度过慢,所以先不开启双缓冲
34     // scm_enable_double_buffer().expect("Failed to enable double buffer");
35     stdio_init().expect("Failed to initialize stdio");
36 
37     ahci_init().expect("Failed to initialize AHCI");
38 
39     mount_root_fs().expect("Failed to mount root fs");
40 
41     virtio_probe();
42     e1000e_init();
43     net_init().unwrap_or_else(|err| {
44         kerror!("Failed to initialize network: {:?}", err);
45     });
46 
47     kdebug!("initial kernel thread done.");
48 
49     return Ok(());
50 }
51 
52 #[inline(never)]
kenrel_init_freeable() -> Result<(), SystemError>53 fn kenrel_init_freeable() -> Result<(), SystemError> {
54     do_initcalls().unwrap_or_else(|err| {
55         panic!("Failed to initialize subsystems: {:?}", err);
56     });
57 
58     return Ok(());
59 }
60 
61 /// 切换到用户态
switch_to_user()62 fn switch_to_user() {
63     let path = String::from("/bin/dragonreach");
64     let argv = vec![String::from("/bin/dragonreach")];
65     let envp = vec![String::from("PATH=/")];
66 
67     unsafe { arch_switch_to_user(path, argv, envp) };
68 }
69