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)] 1013776c11Slogin #![feature(c_void_variant)] 11*1a72a751SLoGin #![feature(extract_if)] 1270a4e555SLoGin #![feature(inline_const)] 131496ba7bSLoGin #![feature(naked_functions)] 14bb24249fSLoGin #![feature(panic_info_message)] 15bb24249fSLoGin #![feature(ptr_internals)] 16cde5492fSlogin #![feature(trait_upcasting)] 1740fe15e0SLoGin #![feature(slice_ptr_get)] 1840fe15e0SLoGin #![feature(vec_into_raw_parts)] 1940314b30SXiaoye Zheng #![feature(new_uninit)] 2040314b30SXiaoye Zheng #![feature(ptr_to_from_bits)] 2140314b30SXiaoye Zheng #![feature(concat_idents)] 22fba56231SLoGin #![cfg_attr(target_os = "none", no_std)] 23fba56231SLoGin 24fba56231SLoGin #[cfg(test)] 25fba56231SLoGin #[macro_use] 26fba56231SLoGin extern crate std; 2740fe15e0SLoGin 282813126eSlogin #[allow(non_upper_case_globals)] 292813126eSlogin #[allow(non_camel_case_types)] 302813126eSlogin #[allow(non_snake_case)] 31fb6c29d0Slogin use core::panic::PanicInfo; 32fb6c29d0Slogin 33adc1846bSlogin /// 导出x86_64架构相关的代码,命名为arch模块 3466f67c6aSlogin #[macro_use] 3566f67c6aSlogin mod arch; 3682d2e446Slogin #[macro_use] 37fb6c29d0Slogin mod libs; 38004e86ffSlogin #[macro_use] 39004e86ffSlogin mod include; 407b32f508SLoGin mod debug; 41004e86ffSlogin mod driver; // 如果driver依赖了libs,应该在libs后面导出 42ec53d23eSlogin mod exception; 43004e86ffSlogin mod filesystem; 44fba56231SLoGin mod init; 45004e86ffSlogin mod ipc; 4666f67c6aSlogin mod mm; 474454d1a2Slogin mod net; 4866f67c6aSlogin mod process; 4966f67c6aSlogin mod sched; 5066f67c6aSlogin mod smp; 514454d1a2Slogin mod syscall; 5201876902SGou Ngai mod time; 534fda81ceSLoGin 544fda81ceSLoGin #[cfg(target_arch = "x86_64")] 5540314b30SXiaoye Zheng mod virt; 5682d2e446Slogin 57004e86ffSlogin #[macro_use] 58fb6c29d0Slogin extern crate alloc; 59c2481452Shoumkh #[macro_use] 60393f6915SLoGin extern crate atomic_enum; 61393f6915SLoGin #[macro_use] 624454d1a2Slogin extern crate bitflags; 6340fe15e0SLoGin extern crate elf; 644454d1a2Slogin #[macro_use] 65c2481452Shoumkh extern crate lazy_static; 664454d1a2Slogin extern crate num; 67004e86ffSlogin #[macro_use] 684454d1a2Slogin extern crate num_derive; 694454d1a2Slogin extern crate smoltcp; 70fba56231SLoGin #[macro_use] 71fba56231SLoGin extern crate intertrait; 72bacd691cSlogin #[cfg(target_arch = "x86_64")] 73bacd691cSlogin extern crate x86; 74bacd691cSlogin 757b32f508SLoGin extern crate klog_types; 767b32f508SLoGin 7740fe15e0SLoGin use crate::mm::allocator::kernel_allocator::KernelAllocator; 782813126eSlogin 791496ba7bSLoGin use crate::process::ProcessManager; 80fb6c29d0Slogin 814fda81ceSLoGin #[cfg(all(feature = "backtrace", target_arch = "x86_64"))] 82d470019bSLoGin extern crate mini_backtrace; 83d470019bSLoGin 84d470019bSLoGin extern "C" { 85d470019bSLoGin fn lookup_kallsyms(addr: u64, level: i32) -> i32; 86d470019bSLoGin } 87d470019bSLoGin 88abe3a6eaShanjiezhou // 声明全局的分配器 89fb6c29d0Slogin #[cfg_attr(not(test), global_allocator)] 9040fe15e0SLoGin pub static KERNEL_ALLOCATOR: KernelAllocator = KernelAllocator; 91fb6c29d0Slogin 92fb6c29d0Slogin /// 全局的panic处理函数 93fba56231SLoGin #[cfg(target_os = "none")] 942813126eSlogin #[panic_handler] 952813126eSlogin #[no_mangle] 9666f67c6aSlogin pub fn panic(info: &PanicInfo) -> ! { 9766f67c6aSlogin kerror!("Kernel Panic Occurred."); 9866f67c6aSlogin 9966f67c6aSlogin match info.location() { 10066f67c6aSlogin Some(loc) => { 10166f67c6aSlogin println!( 10266f67c6aSlogin "Location:\n\tFile: {}\n\tLine: {}, Column: {}", 10366f67c6aSlogin loc.file(), 10466f67c6aSlogin loc.line(), 10566f67c6aSlogin loc.column() 10666f67c6aSlogin ); 10766f67c6aSlogin } 10866f67c6aSlogin None => { 10966f67c6aSlogin println!("No location info"); 11066f67c6aSlogin } 11166f67c6aSlogin } 11266f67c6aSlogin 11366f67c6aSlogin match info.message() { 11466f67c6aSlogin Some(msg) => { 11566f67c6aSlogin println!("Message:\n\t{}", msg); 11666f67c6aSlogin } 11766f67c6aSlogin None => { 11866f67c6aSlogin println!("No panic message."); 11966f67c6aSlogin } 12066f67c6aSlogin } 12166f67c6aSlogin 1224fda81ceSLoGin #[cfg(all(feature = "backtrace", target_arch = "x86_64"))] 123d470019bSLoGin { 124d470019bSLoGin unsafe { 125d470019bSLoGin let bt = mini_backtrace::Backtrace::<16>::capture(); 126d470019bSLoGin println!("Rust Panic Backtrace:"); 127d470019bSLoGin let mut level = 0; 128d470019bSLoGin for frame in bt.frames { 129d470019bSLoGin lookup_kallsyms(frame as u64, level); 130d470019bSLoGin level += 1; 131d470019bSLoGin } 132d470019bSLoGin }; 133d470019bSLoGin } 134d470019bSLoGin 1351496ba7bSLoGin println!("Current PCB:\n\t{:?}", *(ProcessManager::current_pcb())); 1361496ba7bSLoGin ProcessManager::exit(usize::MAX); 1372813126eSlogin } 138