1 #![no_main] // <1> 2 #![feature(alloc_error_handler)] 3 #![feature(allocator_api)] 4 #![feature(arbitrary_self_types)] 5 #![feature(asm_const)] 6 #![feature(const_for)] 7 #![feature(const_mut_refs)] 8 #![feature(const_trait_impl)] 9 #![feature(const_transmute_copy)] 10 #![feature(const_refs_to_cell)] 11 #![feature(core_intrinsics)] 12 #![feature(c_void_variant)] 13 #![feature(extract_if)] 14 #![feature(inline_const)] 15 #![feature(naked_functions)] 16 #![feature(panic_info_message)] 17 #![feature(ptr_internals)] 18 #![feature(trait_upcasting)] 19 #![feature(slice_ptr_get)] 20 #![feature(vec_into_raw_parts)] 21 #![feature(new_uninit)] 22 #![feature(ptr_to_from_bits)] 23 #![feature(concat_idents)] 24 #![cfg_attr(target_os = "none", no_std)] 25 26 #[cfg(test)] 27 #[macro_use] 28 extern crate std; 29 30 #[allow(non_upper_case_globals)] 31 #[allow(non_camel_case_types)] 32 #[allow(non_snake_case)] 33 use core::panic::PanicInfo; 34 35 /// 导出x86_64架构相关的代码,命名为arch模块 36 #[macro_use] 37 mod arch; 38 #[macro_use] 39 mod libs; 40 #[macro_use] 41 mod include; 42 mod debug; 43 mod driver; // 如果driver依赖了libs,应该在libs后面导出 44 mod exception; 45 mod filesystem; 46 mod init; 47 mod ipc; 48 mod misc; 49 mod mm; 50 mod net; 51 mod process; 52 mod sched; 53 mod smp; 54 mod syscall; 55 mod time; 56 57 #[cfg(target_arch = "x86_64")] 58 mod virt; 59 60 #[macro_use] 61 extern crate alloc; 62 #[macro_use] 63 extern crate atomic_enum; 64 #[macro_use] 65 extern crate bitflags; 66 extern crate elf; 67 #[macro_use] 68 extern crate lazy_static; 69 extern crate num; 70 #[macro_use] 71 extern crate num_derive; 72 extern crate smoltcp; 73 #[macro_use] 74 extern crate intertrait; 75 #[cfg(target_arch = "x86_64")] 76 extern crate x86; 77 78 extern crate klog_types; 79 extern crate uefi; 80 extern crate uefi_raw; 81 82 use crate::mm::allocator::kernel_allocator::KernelAllocator; 83 84 use crate::process::ProcessManager; 85 86 #[cfg(all(feature = "backtrace", target_arch = "x86_64"))] 87 extern crate mini_backtrace; 88 89 extern "C" { 90 fn lookup_kallsyms(addr: u64, level: i32) -> i32; 91 } 92 93 // 声明全局的分配器 94 #[cfg_attr(not(test), global_allocator)] 95 pub static KERNEL_ALLOCATOR: KernelAllocator = KernelAllocator; 96 97 /// 全局的panic处理函数 98 #[cfg(target_os = "none")] 99 #[panic_handler] 100 #[no_mangle] 101 pub fn panic(info: &PanicInfo) -> ! { 102 kerror!("Kernel Panic Occurred."); 103 104 match info.location() { 105 Some(loc) => { 106 println!( 107 "Location:\n\tFile: {}\n\tLine: {}, Column: {}", 108 loc.file(), 109 loc.line(), 110 loc.column() 111 ); 112 } 113 None => { 114 println!("No location info"); 115 } 116 } 117 118 match info.message() { 119 Some(msg) => { 120 println!("Message:\n\t{}", msg); 121 } 122 None => { 123 println!("No panic message."); 124 } 125 } 126 127 #[cfg(all(feature = "backtrace", target_arch = "x86_64"))] 128 { 129 unsafe { 130 let bt = mini_backtrace::Backtrace::<16>::capture(); 131 println!("Rust Panic Backtrace:"); 132 let mut level = 0; 133 for frame in bt.frames { 134 lookup_kallsyms(frame as u64, level); 135 level += 1; 136 } 137 }; 138 } 139 140 println!("Current PCB:\n\t{:?}", *(ProcessManager::current_pcb())); 141 ProcessManager::exit(usize::MAX); 142 } 143