12813126eSlogin #![no_main] // <1> 2bb24249fSLoGin #![feature(alloc_error_handler)] 340fe15e0SLoGin #![feature(allocator_api)] 440fe15e0SLoGin #![feature(arbitrary_self_types)] 51496ba7bSLoGin #![feature(asm_const)] 65c4224e5SLoGin #![feature(concat_idents)] 7c566df45SLoGin #![feature(const_for)] 8935f40ecSlogin #![feature(const_mut_refs)] 906d5e247SLoGin #![feature(const_trait_impl)] 1092849878SLoGin #![feature(const_transmute_copy)] 11a03c4f9dSLoGin #![feature(const_refs_to_cell)] 1240fe15e0SLoGin #![feature(core_intrinsics)] 1313776c11Slogin #![feature(c_void_variant)] 141a72a751SLoGin #![feature(extract_if)] 155c4224e5SLoGin #![feature(fn_align)] 1670a4e555SLoGin #![feature(inline_const)] 171496ba7bSLoGin #![feature(naked_functions)] 185c4224e5SLoGin #![feature(new_uninit)] 19bb24249fSLoGin #![feature(panic_info_message)] 20bb24249fSLoGin #![feature(ptr_internals)] 215c4224e5SLoGin #![feature(ptr_to_from_bits)] 22cde5492fSlogin #![feature(trait_upcasting)] 2340fe15e0SLoGin #![feature(slice_ptr_get)] 2440fe15e0SLoGin #![feature(vec_into_raw_parts)] 25fba56231SLoGin #![cfg_attr(target_os = "none", no_std)] 26840045afSLoGin // clippy的配置 27840045afSLoGin #![deny(clippy::all)] 28b5b571e0SLoGin #![allow(clippy::bad_bit_mask)] 29840045afSLoGin // DragonOS允许在函数中使用return语句(尤其是长函数时,我们推荐这么做) 30840045afSLoGin #![allow(clippy::let_and_return)] 31840045afSLoGin #![allow(clippy::needless_pass_by_ref_mut)] 32840045afSLoGin #![allow(clippy::needless_return)] 33b5b571e0SLoGin #![allow(clippy::single_char_pattern)] 34840045afSLoGin #![allow(clippy::upper_case_acronyms)] 35fba56231SLoGin 36fba56231SLoGin #[cfg(test)] 37fba56231SLoGin #[macro_use] 38fba56231SLoGin extern crate std; 3940fe15e0SLoGin 40fb6c29d0Slogin use core::panic::PanicInfo; 41fb6c29d0Slogin 42adc1846bSlogin /// 导出x86_64架构相关的代码,命名为arch模块 4366f67c6aSlogin #[macro_use] 4466f67c6aSlogin mod arch; 4582d2e446Slogin #[macro_use] 46fb6c29d0Slogin mod libs; 47004e86ffSlogin #[macro_use] 48004e86ffSlogin mod include; 497b32f508SLoGin mod debug; 50004e86ffSlogin mod driver; // 如果driver依赖了libs,应该在libs后面导出 51ec53d23eSlogin mod exception; 52004e86ffSlogin mod filesystem; 53fba56231SLoGin mod init; 54004e86ffSlogin mod ipc; 55196b75dcSLoGin mod misc; 5666f67c6aSlogin mod mm; 574454d1a2Slogin mod net; 5866f67c6aSlogin mod process; 5966f67c6aSlogin mod sched; 6066f67c6aSlogin mod smp; 614454d1a2Slogin mod syscall; 6201876902SGou Ngai mod time; 634fda81ceSLoGin 644fda81ceSLoGin #[cfg(target_arch = "x86_64")] 6540314b30SXiaoye Zheng mod virt; 6682d2e446Slogin 67004e86ffSlogin #[macro_use] 68fb6c29d0Slogin extern crate alloc; 69c2481452Shoumkh #[macro_use] 70393f6915SLoGin extern crate atomic_enum; 71393f6915SLoGin #[macro_use] 724454d1a2Slogin extern crate bitflags; 7340fe15e0SLoGin extern crate elf; 744454d1a2Slogin #[macro_use] 75c2481452Shoumkh extern crate lazy_static; 764454d1a2Slogin extern crate num; 77004e86ffSlogin #[macro_use] 784454d1a2Slogin extern crate num_derive; 794454d1a2Slogin extern crate smoltcp; 80fba56231SLoGin #[macro_use] 81fba56231SLoGin extern crate intertrait; 82bacd691cSlogin #[cfg(target_arch = "x86_64")] 83bacd691cSlogin extern crate x86; 84bacd691cSlogin 857b32f508SLoGin extern crate klog_types; 867a29d4fcSLoGin extern crate uefi; 877a29d4fcSLoGin extern crate uefi_raw; 887b32f508SLoGin 8940fe15e0SLoGin use crate::mm::allocator::kernel_allocator::KernelAllocator; 902813126eSlogin 911496ba7bSLoGin use crate::process::ProcessManager; 92fb6c29d0Slogin 934fda81ceSLoGin #[cfg(all(feature = "backtrace", target_arch = "x86_64"))] 94d470019bSLoGin extern crate mini_backtrace; 95d470019bSLoGin 96d470019bSLoGin extern "C" { 97d470019bSLoGin fn lookup_kallsyms(addr: u64, level: i32) -> i32; 98d470019bSLoGin } 99d470019bSLoGin 100abe3a6eaShanjiezhou // 声明全局的分配器 101fb6c29d0Slogin #[cfg_attr(not(test), global_allocator)] 10240fe15e0SLoGin pub static KERNEL_ALLOCATOR: KernelAllocator = KernelAllocator; 103fb6c29d0Slogin 104fb6c29d0Slogin /// 全局的panic处理函数 105fba56231SLoGin #[cfg(target_os = "none")] 1062813126eSlogin #[panic_handler] 1072813126eSlogin #[no_mangle] 10866f67c6aSlogin pub fn panic(info: &PanicInfo) -> ! { 109*2eab6dd7S曾俊 use log::error; 110*2eab6dd7S曾俊 111*2eab6dd7S曾俊 error!("Kernel Panic Occurred."); 11266f67c6aSlogin 11366f67c6aSlogin match info.location() { 11466f67c6aSlogin Some(loc) => { 11566f67c6aSlogin println!( 11666f67c6aSlogin "Location:\n\tFile: {}\n\tLine: {}, Column: {}", 11766f67c6aSlogin loc.file(), 11866f67c6aSlogin loc.line(), 11966f67c6aSlogin loc.column() 12066f67c6aSlogin ); 12166f67c6aSlogin } 12266f67c6aSlogin None => { 12366f67c6aSlogin println!("No location info"); 12466f67c6aSlogin } 12566f67c6aSlogin } 12666f67c6aSlogin 12766f67c6aSlogin match info.message() { 12866f67c6aSlogin Some(msg) => { 12966f67c6aSlogin println!("Message:\n\t{}", msg); 13066f67c6aSlogin } 13166f67c6aSlogin None => { 13266f67c6aSlogin println!("No panic message."); 13366f67c6aSlogin } 13466f67c6aSlogin } 13566f67c6aSlogin 1364fda81ceSLoGin #[cfg(all(feature = "backtrace", target_arch = "x86_64"))] 137d470019bSLoGin { 138d470019bSLoGin unsafe { 139d470019bSLoGin let bt = mini_backtrace::Backtrace::<16>::capture(); 140d470019bSLoGin println!("Rust Panic Backtrace:"); 141d470019bSLoGin let mut level = 0; 142d470019bSLoGin for frame in bt.frames { 143d470019bSLoGin lookup_kallsyms(frame as u64, level); 144d470019bSLoGin level += 1; 145d470019bSLoGin } 146d470019bSLoGin }; 147d470019bSLoGin } 148d470019bSLoGin 1492755467cS曾俊 println!("Current PCB:\n\t{:?}", (ProcessManager::current_pcb())); 150471d65cfSLoGin 1511496ba7bSLoGin ProcessManager::exit(usize::MAX); 1522813126eSlogin } 153