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, stdio::stdio_init}, 15 smp::smp_init, 16 }; 17 18 use super::initcall::do_initcalls; 19 20 pub fn initial_kernel_thread() -> i32 { 21 kernel_init().unwrap_or_else(|err| { 22 panic!("Failed to initialize kernel: {:?}", err); 23 }); 24 25 switch_to_user(); 26 27 unreachable!(); 28 } 29 30 fn kernel_init() -> Result<(), SystemError> { 31 KernelThreadMechanism::init_stage2(); 32 kenrel_init_freeable()?; 33 34 // 由于目前加锁,速度过慢,所以先不开启双缓冲 35 // scm_enable_double_buffer().expect("Failed to enable double buffer"); 36 37 #[cfg(target_arch = "x86_64")] 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 stdio_init().expect("Failed to initialize stdio"); 59 smp_init(); 60 61 return Ok(()); 62 } 63 64 /// 切换到用户态 65 fn switch_to_user() { 66 let path = String::from("/bin/dragonreach"); 67 let argv = vec![String::from("/bin/dragonreach")]; 68 let envp = vec![String::from("PATH=/")]; 69 70 unsafe { arch_switch_to_user(path, argv, envp) }; 71 } 72