12813126eSlogin #![no_main] // <1> 2bb24249fSLoGin #![feature(alloc_error_handler)] 340fe15e0SLoGin #![feature(allocator_api)] 440fe15e0SLoGin #![feature(arbitrary_self_types)] 51496ba7bSLoGin #![feature(asm_const)] 6935f40ecSlogin #![feature(const_mut_refs)] 706d5e247SLoGin #![feature(const_trait_impl)] 8a03c4f9dSLoGin #![feature(const_refs_to_cell)] 940fe15e0SLoGin #![feature(core_intrinsics)] 10bf4a4899SLoGin #![feature(cstr_from_bytes_until_nul)] 1113776c11Slogin #![feature(c_void_variant)] 1240fe15e0SLoGin #![feature(drain_filter)] 1370a4e555SLoGin #![feature(inline_const)] 14729a96efSXshine #![feature(is_some_and)] 151496ba7bSLoGin #![feature(naked_functions)] 16bb24249fSLoGin #![feature(panic_info_message)] 17bb24249fSLoGin #![feature(ptr_internals)] 18cde5492fSlogin #![feature(trait_upcasting)] 1940fe15e0SLoGin #![feature(slice_ptr_get)] 2040fe15e0SLoGin #![feature(vec_into_raw_parts)] 2140314b30SXiaoye Zheng #![feature(new_uninit)] 2240314b30SXiaoye Zheng #![feature(ptr_to_from_bits)] 2340314b30SXiaoye Zheng #![feature(concat_idents)] 24fba56231SLoGin #![cfg_attr(target_os = "none", no_std)] 25971462beSGnoCiYeH #![feature(atomic_mut_ptr)] 26fba56231SLoGin 27fba56231SLoGin #[cfg(test)] 28fba56231SLoGin #[macro_use] 29fba56231SLoGin extern crate std; 3040fe15e0SLoGin 312813126eSlogin #[allow(non_upper_case_globals)] 322813126eSlogin #[allow(non_camel_case_types)] 332813126eSlogin #[allow(non_snake_case)] 34fb6c29d0Slogin use core::panic::PanicInfo; 35fb6c29d0Slogin 36adc1846bSlogin /// 导出x86_64架构相关的代码,命名为arch模块 3766f67c6aSlogin #[macro_use] 3866f67c6aSlogin mod arch; 3982d2e446Slogin #[macro_use] 40fb6c29d0Slogin mod libs; 41004e86ffSlogin #[macro_use] 42004e86ffSlogin mod include; 437b32f508SLoGin mod debug; 44004e86ffSlogin mod driver; // 如果driver依赖了libs,应该在libs后面导出 45ec53d23eSlogin mod exception; 46004e86ffSlogin mod filesystem; 47fba56231SLoGin mod init; 48004e86ffSlogin mod ipc; 4966f67c6aSlogin mod mm; 504454d1a2Slogin mod net; 5166f67c6aSlogin mod process; 5266f67c6aSlogin mod sched; 5366f67c6aSlogin mod smp; 544454d1a2Slogin mod syscall; 5501876902SGou Ngai mod time; 56*4fda81ceSLoGin 57*4fda81ceSLoGin #[cfg(target_arch = "x86_64")] 5840314b30SXiaoye Zheng mod virt; 5982d2e446Slogin 60004e86ffSlogin #[macro_use] 61fb6c29d0Slogin extern crate alloc; 62c2481452Shoumkh #[macro_use] 63393f6915SLoGin extern crate atomic_enum; 64393f6915SLoGin #[macro_use] 654454d1a2Slogin extern crate bitflags; 6640fe15e0SLoGin extern crate elf; 674454d1a2Slogin #[macro_use] 68c2481452Shoumkh extern crate lazy_static; 694454d1a2Slogin extern crate num; 70004e86ffSlogin #[macro_use] 714454d1a2Slogin extern crate num_derive; 724454d1a2Slogin extern crate smoltcp; 73fba56231SLoGin #[macro_use] 74fba56231SLoGin extern crate intertrait; 75bacd691cSlogin #[cfg(target_arch = "x86_64")] 76bacd691cSlogin extern crate x86; 77bacd691cSlogin 787b32f508SLoGin extern crate klog_types; 797b32f508SLoGin 8040fe15e0SLoGin use crate::mm::allocator::kernel_allocator::KernelAllocator; 812813126eSlogin 821496ba7bSLoGin use crate::process::ProcessManager; 83fb6c29d0Slogin 84*4fda81ceSLoGin #[cfg(all(feature = "backtrace", target_arch = "x86_64"))] 85d470019bSLoGin extern crate mini_backtrace; 86d470019bSLoGin 87d470019bSLoGin extern "C" { 88d470019bSLoGin fn lookup_kallsyms(addr: u64, level: i32) -> i32; 89d470019bSLoGin } 90d470019bSLoGin 91abe3a6eaShanjiezhou // 声明全局的分配器 92fb6c29d0Slogin #[cfg_attr(not(test), global_allocator)] 9340fe15e0SLoGin pub static KERNEL_ALLOCATOR: KernelAllocator = KernelAllocator; 94fb6c29d0Slogin 95fb6c29d0Slogin /// 全局的panic处理函数 96fba56231SLoGin #[cfg(target_os = "none")] 972813126eSlogin #[panic_handler] 982813126eSlogin #[no_mangle] 9966f67c6aSlogin pub fn panic(info: &PanicInfo) -> ! { 10066f67c6aSlogin kerror!("Kernel Panic Occurred."); 10166f67c6aSlogin 10266f67c6aSlogin match info.location() { 10366f67c6aSlogin Some(loc) => { 10466f67c6aSlogin println!( 10566f67c6aSlogin "Location:\n\tFile: {}\n\tLine: {}, Column: {}", 10666f67c6aSlogin loc.file(), 10766f67c6aSlogin loc.line(), 10866f67c6aSlogin loc.column() 10966f67c6aSlogin ); 11066f67c6aSlogin } 11166f67c6aSlogin None => { 11266f67c6aSlogin println!("No location info"); 11366f67c6aSlogin } 11466f67c6aSlogin } 11566f67c6aSlogin 11666f67c6aSlogin match info.message() { 11766f67c6aSlogin Some(msg) => { 11866f67c6aSlogin println!("Message:\n\t{}", msg); 11966f67c6aSlogin } 12066f67c6aSlogin None => { 12166f67c6aSlogin println!("No panic message."); 12266f67c6aSlogin } 12366f67c6aSlogin } 12466f67c6aSlogin 125*4fda81ceSLoGin #[cfg(all(feature = "backtrace", target_arch = "x86_64"))] 126d470019bSLoGin { 127d470019bSLoGin unsafe { 128d470019bSLoGin let bt = mini_backtrace::Backtrace::<16>::capture(); 129d470019bSLoGin println!("Rust Panic Backtrace:"); 130d470019bSLoGin let mut level = 0; 131d470019bSLoGin for frame in bt.frames { 132d470019bSLoGin lookup_kallsyms(frame as u64, level); 133d470019bSLoGin level += 1; 134d470019bSLoGin } 135d470019bSLoGin }; 136d470019bSLoGin } 137d470019bSLoGin 1381496ba7bSLoGin println!("Current PCB:\n\t{:?}", *(ProcessManager::current_pcb())); 1391496ba7bSLoGin ProcessManager::exit(usize::MAX); 1402813126eSlogin } 141