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 driver; // 如果driver依赖了libs,应该在libs后面导出 43 mod exception; 44 mod filesystem; 45 mod init; 46 mod ipc; 47 mod mm; 48 mod net; 49 mod process; 50 mod sched; 51 mod smp; 52 mod syscall; 53 mod time; 54 mod virt; 55 56 #[macro_use] 57 extern crate alloc; 58 #[macro_use] 59 extern crate bitflags; 60 extern crate elf; 61 #[macro_use] 62 extern crate lazy_static; 63 extern crate memoffset; 64 extern crate num; 65 #[macro_use] 66 extern crate num_derive; 67 extern crate smoltcp; 68 extern crate thingbuf; 69 #[macro_use] 70 extern crate intertrait; 71 #[cfg(target_arch = "x86_64")] 72 extern crate x86; 73 74 use crate::mm::allocator::kernel_allocator::KernelAllocator; 75 76 use crate::process::ProcessManager; 77 78 #[cfg(feature = "backtrace")] 79 extern crate mini_backtrace; 80 81 extern "C" { 82 fn lookup_kallsyms(addr: u64, level: i32) -> i32; 83 } 84 85 // 声明全局的分配器 86 #[cfg_attr(not(test), global_allocator)] 87 pub static KERNEL_ALLOCATOR: KernelAllocator = KernelAllocator; 88 89 /// 全局的panic处理函数 90 #[cfg(target_os = "none")] 91 #[panic_handler] 92 #[no_mangle] 93 pub fn panic(info: &PanicInfo) -> ! { 94 kerror!("Kernel Panic Occurred."); 95 96 match info.location() { 97 Some(loc) => { 98 println!( 99 "Location:\n\tFile: {}\n\tLine: {}, Column: {}", 100 loc.file(), 101 loc.line(), 102 loc.column() 103 ); 104 } 105 None => { 106 println!("No location info"); 107 } 108 } 109 110 match info.message() { 111 Some(msg) => { 112 println!("Message:\n\t{}", msg); 113 } 114 None => { 115 println!("No panic message."); 116 } 117 } 118 119 #[cfg(feature = "backtrace")] 120 { 121 unsafe { 122 let bt = mini_backtrace::Backtrace::<16>::capture(); 123 println!("Rust Panic Backtrace:"); 124 let mut level = 0; 125 for frame in bt.frames { 126 lookup_kallsyms(frame as u64, level); 127 level += 1; 128 } 129 }; 130 } 131 132 println!("Current PCB:\n\t{:?}", *(ProcessManager::current_pcb())); 133 ProcessManager::exit(usize::MAX); 134 } 135