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