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