xref: /DragonOS/kernel/src/lib.rs (revision 83ed0ebc293d5a10245089f627f52770fd5b9dd4)
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(cstr_from_bytes_until_nul)]
11 #![feature(c_void_variant)]
12 #![feature(drain_filter)]
13 #![feature(inline_const)]
14 #![feature(is_some_and)]
15 #![feature(naked_functions)]
16 #![feature(panic_info_message)]
17 #![feature(ptr_internals)]
18 #![feature(trait_upcasting)]
19 #![feature(slice_ptr_get)]
20 #![feature(vec_into_raw_parts)]
21 #![feature(new_uninit)]
22 #![feature(ptr_to_from_bits)]
23 #![feature(concat_idents)]
24 #![cfg_attr(target_os = "none", no_std)]
25 #![feature(atomic_mut_ptr)]
26 
27 #[cfg(test)]
28 #[macro_use]
29 extern crate std;
30 
31 #[allow(non_upper_case_globals)]
32 #[allow(non_camel_case_types)]
33 #[allow(non_snake_case)]
34 use core::panic::PanicInfo;
35 
36 /// 导出x86_64架构相关的代码,命名为arch模块
37 #[macro_use]
38 mod arch;
39 #[macro_use]
40 mod libs;
41 #[macro_use]
42 mod include;
43 mod debug;
44 mod driver; // 如果driver依赖了libs,应该在libs后面导出
45 mod exception;
46 mod filesystem;
47 mod init;
48 mod ipc;
49 mod mm;
50 mod net;
51 mod process;
52 mod sched;
53 mod smp;
54 mod syscall;
55 mod time;
56 
57 #[cfg(target_arch = "x86_64")]
58 mod virt;
59 
60 #[macro_use]
61 extern crate alloc;
62 #[macro_use]
63 extern crate atomic_enum;
64 #[macro_use]
65 extern crate bitflags;
66 extern crate elf;
67 #[macro_use]
68 extern crate lazy_static;
69 extern crate num;
70 #[macro_use]
71 extern crate num_derive;
72 extern crate smoltcp;
73 #[macro_use]
74 extern crate intertrait;
75 #[cfg(target_arch = "x86_64")]
76 extern crate x86;
77 
78 extern crate klog_types;
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