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