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_mut_refs)] 7 #![feature(const_trait_impl)] 8 #![feature(const_refs_to_cell)] 9 #![feature(core_intrinsics)] 10 #![feature(c_void_variant)] 11 #![feature(drain_filter)] 12 #![feature(inline_const)] 13 #![feature(is_some_and)] 14 #![feature(naked_functions)] 15 #![feature(panic_info_message)] 16 #![feature(ptr_internals)] 17 #![feature(trait_upcasting)] 18 #![feature(slice_ptr_get)] 19 #![feature(vec_into_raw_parts)] 20 #![feature(new_uninit)] 21 #![feature(ptr_to_from_bits)] 22 #![feature(concat_idents)] 23 #![cfg_attr(target_os = "none", no_std)] 24 #![feature(atomic_mut_ptr)] 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 mm; 49 mod net; 50 mod process; 51 mod sched; 52 mod smp; 53 mod syscall; 54 mod time; 55 mod virt; 56 57 #[macro_use] 58 extern crate alloc; 59 #[macro_use] 60 extern crate bitflags; 61 extern crate elf; 62 #[macro_use] 63 extern crate lazy_static; 64 extern crate num; 65 #[macro_use] 66 extern crate num_derive; 67 extern crate smoltcp; 68 #[macro_use] 69 extern crate intertrait; 70 #[cfg(target_arch = "x86_64")] 71 extern crate x86; 72 73 extern crate klog_types; 74 75 use crate::mm::allocator::kernel_allocator::KernelAllocator; 76 77 use crate::process::ProcessManager; 78 79 #[cfg(feature = "backtrace")] 80 extern crate mini_backtrace; 81 82 extern "C" { 83 fn lookup_kallsyms(addr: u64, level: i32) -> i32; 84 } 85 86 // 声明全局的分配器 87 #[cfg_attr(not(test), global_allocator)] 88 pub static KERNEL_ALLOCATOR: KernelAllocator = KernelAllocator; 89 90 /// 全局的panic处理函数 91 #[cfg(target_os = "none")] 92 #[panic_handler] 93 #[no_mangle] 94 pub fn panic(info: &PanicInfo) -> ! { 95 kerror!("Kernel Panic Occurred."); 96 97 match info.location() { 98 Some(loc) => { 99 println!( 100 "Location:\n\tFile: {}\n\tLine: {}, Column: {}", 101 loc.file(), 102 loc.line(), 103 loc.column() 104 ); 105 } 106 None => { 107 println!("No location info"); 108 } 109 } 110 111 match info.message() { 112 Some(msg) => { 113 println!("Message:\n\t{}", msg); 114 } 115 None => { 116 println!("No panic message."); 117 } 118 } 119 120 #[cfg(feature = "backtrace")] 121 { 122 unsafe { 123 let bt = mini_backtrace::Backtrace::<16>::capture(); 124 println!("Rust Panic Backtrace:"); 125 let mut level = 0; 126 for frame in bt.frames { 127 lookup_kallsyms(frame as u64, level); 128 level += 1; 129 } 130 }; 131 } 132 133 println!("Current PCB:\n\t{:?}", *(ProcessManager::current_pcb())); 134 ProcessManager::exit(usize::MAX); 135 } 136