1 #![no_main] // <1>
2 #![feature(alloc_error_handler)]
3 #![feature(allocator_api)]
4 #![feature(arbitrary_self_types)]
5 #![feature(concat_idents)]
6 #![feature(const_for)]
7 #![feature(const_trait_impl)]
8 #![feature(core_intrinsics)]
9 #![feature(c_void_variant)]
10 #![feature(extract_if)]
11 #![feature(fn_align)]
12 #![feature(linked_list_retain)]
13 #![feature(naked_functions)]
14 #![feature(ptr_internals)]
15 #![feature(trait_upcasting)]
16 #![feature(slice_ptr_get)]
17 #![feature(sync_unsafe_cell)]
18 #![feature(vec_into_raw_parts)]
19 #![feature(c_variadic)]
20 #![feature(asm_goto)]
21 #![cfg_attr(target_os = "none", no_std)]
22 #![allow(static_mut_refs, non_local_definitions, internal_features)]
23 // clippy的配置
24 #![deny(clippy::all)]
25 // DragonOS允许在函数中使用return语句(尤其是长函数时,我们推荐这么做)
26 #![allow(
27 clippy::macro_metavars_in_unsafe,
28 clippy::upper_case_acronyms,
29 clippy::single_char_pattern,
30 clippy::needless_return,
31 clippy::needless_pass_by_ref_mut,
32 clippy::let_and_return,
33 clippy::bad_bit_mask
34 )]
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 bpf;
50 mod cgroup;
51 mod debug;
52 mod driver; // 如果driver依赖了libs,应该在libs后面导出
53 mod exception;
54 mod filesystem;
55 mod init;
56 mod ipc;
57 mod misc;
58 mod mm;
59 mod namespaces;
60 mod net;
61 mod perf;
62 mod process;
63 mod sched;
64 mod smp;
65 mod syscall;
66 mod time;
67 #[cfg(target_arch = "x86_64")]
68 mod virt;
69
70 #[macro_use]
71 extern crate alloc;
72 #[macro_use]
73 extern crate atomic_enum;
74 #[macro_use]
75 extern crate bitflags;
76 extern crate elf;
77 #[macro_use]
78 extern crate lazy_static;
79 extern crate num;
80 #[macro_use]
81 extern crate num_derive;
82 extern crate smoltcp;
83 #[macro_use]
84 extern crate intertrait;
85 #[cfg(target_arch = "x86_64")]
86 extern crate x86;
87 #[macro_use]
88 extern crate kcmdline_macros;
89 extern crate klog_types;
90 extern crate uefi;
91 extern crate uefi_raw;
92 #[macro_use]
93 extern crate wait_queue_macros;
94
95 use crate::mm::allocator::kernel_allocator::KernelAllocator;
96
97 #[cfg(feature = "backtrace")]
98 use unwinding::{
99 abi::{UnwindContext, UnwindReasonCode, _Unwind_GetIP},
100 panic::UserUnwindTrace,
101 };
102
103 extern "C" {
lookup_kallsyms(addr: u64, level: i32) -> i32104 fn lookup_kallsyms(addr: u64, level: i32) -> i32;
105 }
106
107 // 声明全局的分配器
108 #[cfg_attr(not(test), global_allocator)]
109 pub static KERNEL_ALLOCATOR: KernelAllocator = KernelAllocator;
110
111 /// 全局的panic处理函数
112 ///
113 /// How to use unwinding lib:
114 ///
115 /// ```
116 /// pub fn test_unwind() {
117 /// struct UnwindTest;
118 /// impl Drop for UnwindTest {
119 /// fn drop(&mut self) {
120 /// println!("Drop UnwindTest");
121 /// }
122 /// }
123 /// let res1 = unwinding::panic::catch_unwind(|| {
124 /// let _unwind_test = UnwindTest;
125 /// println!("Test panic...");
126 /// panic!("Test panic");
127 /// });
128 /// assert_eq!(res1.is_err(), true);
129 /// let res2 = unwinding::panic::catch_unwind(|| {
130 /// let _unwind_test = UnwindTest;
131 /// println!("Test no panic...");
132 /// 0
133 /// });
134 /// assert_eq!(res2.is_ok(), true);
135 /// }
136 /// ```
137 ///
138 #[cfg(target_os = "none")]
139 #[panic_handler]
140 #[no_mangle]
panic(info: &PanicInfo) -> !141 pub fn panic(info: &PanicInfo) -> ! {
142 use log::error;
143
144 error!("Kernel Panic Occurred.");
145
146 match info.location() {
147 Some(loc) => {
148 println!(
149 "Location:\n\tFile: {}\n\tLine: {}, Column: {}",
150 loc.file(),
151 loc.line(),
152 loc.column()
153 );
154 }
155 None => {
156 println!("No location info");
157 }
158 }
159 println!("Message:\n\t{}", info.message());
160 #[cfg(feature = "backtrace")]
161 {
162 let mut data = CallbackData { counter: 0 };
163 println!("Rust Panic Backtrace:");
164 let res = unwinding::panic::begin_panic_with_hook::<Tracer>(
165 alloc::boxed::Box::new(()),
166 &mut data,
167 );
168 log::error!("panic unreachable: {:?}", res.0);
169 }
170 println!(
171 "Current PCB:\n\t{:?}",
172 process::ProcessManager::current_pcb()
173 );
174 process::ProcessManager::exit(usize::MAX);
175 loop {}
176 }
177
178 /// User hook for unwinding
179 ///
180 /// During stack backtrace, the user can print the function location of the current stack frame.
181 struct Tracer;
182 struct CallbackData {
183 counter: usize,
184 }
185 impl UserUnwindTrace for Tracer {
186 type Arg = CallbackData;
187
trace(ctx: &UnwindContext<'_>, arg: *mut Self::Arg) -> UnwindReasonCode188 fn trace(ctx: &UnwindContext<'_>, arg: *mut Self::Arg) -> UnwindReasonCode {
189 let data = unsafe { &mut *(arg) };
190 data.counter += 1;
191 let pc = _Unwind_GetIP(ctx);
192 unsafe {
193 lookup_kallsyms(pc as u64, data.counter as i32);
194 }
195 UnwindReasonCode::NO_REASON
196 }
197 }
198