xref: /DragonOS/kernel/src/lib.rs (revision 7a29d4fcbcd89a226289c7bf541c2c78623de3ad)
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_refs_to_cell)]
10 #![feature(core_intrinsics)]
11 #![feature(c_void_variant)]
12 #![feature(extract_if)]
13 #![feature(inline_const)]
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 
25 #[cfg(test)]
26 #[macro_use]
27 extern crate std;
28 
29 #[allow(non_upper_case_globals)]
30 #[allow(non_camel_case_types)]
31 #[allow(non_snake_case)]
32 use core::panic::PanicInfo;
33 
34 /// 导出x86_64架构相关的代码,命名为arch模块
35 #[macro_use]
36 mod arch;
37 #[macro_use]
38 mod libs;
39 #[macro_use]
40 mod include;
41 mod debug;
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 
55 #[cfg(target_arch = "x86_64")]
56 mod virt;
57 
58 #[macro_use]
59 extern crate alloc;
60 #[macro_use]
61 extern crate atomic_enum;
62 #[macro_use]
63 extern crate bitflags;
64 extern crate elf;
65 #[macro_use]
66 extern crate lazy_static;
67 extern crate num;
68 #[macro_use]
69 extern crate num_derive;
70 extern crate smoltcp;
71 #[macro_use]
72 extern crate intertrait;
73 #[cfg(target_arch = "x86_64")]
74 extern crate x86;
75 
76 extern crate klog_types;
77 extern crate uefi;
78 extern crate uefi_raw;
79 
80 use crate::mm::allocator::kernel_allocator::KernelAllocator;
81 
82 use crate::process::ProcessManager;
83 
84 #[cfg(all(feature = "backtrace", target_arch = "x86_64"))]
85 extern crate mini_backtrace;
86 
87 extern "C" {
88     fn lookup_kallsyms(addr: u64, level: i32) -> i32;
89 }
90 
91 // 声明全局的分配器
92 #[cfg_attr(not(test), global_allocator)]
93 pub static KERNEL_ALLOCATOR: KernelAllocator = KernelAllocator;
94 
95 /// 全局的panic处理函数
96 #[cfg(target_os = "none")]
97 #[panic_handler]
98 #[no_mangle]
99 pub fn panic(info: &PanicInfo) -> ! {
100     kerror!("Kernel Panic Occurred.");
101 
102     match info.location() {
103         Some(loc) => {
104             println!(
105                 "Location:\n\tFile: {}\n\tLine: {}, Column: {}",
106                 loc.file(),
107                 loc.line(),
108                 loc.column()
109             );
110         }
111         None => {
112             println!("No location info");
113         }
114     }
115 
116     match info.message() {
117         Some(msg) => {
118             println!("Message:\n\t{}", msg);
119         }
120         None => {
121             println!("No panic message.");
122         }
123     }
124 
125     #[cfg(all(feature = "backtrace", target_arch = "x86_64"))]
126     {
127         unsafe {
128             let bt = mini_backtrace::Backtrace::<16>::capture();
129             println!("Rust Panic Backtrace:");
130             let mut level = 0;
131             for frame in bt.frames {
132                 lookup_kallsyms(frame as u64, level);
133                 level += 1;
134             }
135         };
136     }
137 
138     println!("Current PCB:\n\t{:?}", *(ProcessManager::current_pcb()));
139     ProcessManager::exit(usize::MAX);
140 }
141