xref: /DragonOS/kernel/src/lib.rs (revision 45626c859f95054b76d8b59afcbd24c6b235026f)
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 
78 use crate::mm::allocator::kernel_allocator::KernelAllocator;
79 
80 use crate::process::ProcessManager;
81 
82 #[cfg(all(feature = "backtrace", target_arch = "x86_64"))]
83 extern crate mini_backtrace;
84 
85 extern "C" {
86     fn lookup_kallsyms(addr: u64, level: i32) -> i32;
87 }
88 
89 // 声明全局的分配器
90 #[cfg_attr(not(test), global_allocator)]
91 pub static KERNEL_ALLOCATOR: KernelAllocator = KernelAllocator;
92 
93 /// 全局的panic处理函数
94 #[cfg(target_os = "none")]
95 #[panic_handler]
96 #[no_mangle]
97 pub fn panic(info: &PanicInfo) -> ! {
98     kerror!("Kernel Panic Occurred.");
99 
100     match info.location() {
101         Some(loc) => {
102             println!(
103                 "Location:\n\tFile: {}\n\tLine: {}, Column: {}",
104                 loc.file(),
105                 loc.line(),
106                 loc.column()
107             );
108         }
109         None => {
110             println!("No location info");
111         }
112     }
113 
114     match info.message() {
115         Some(msg) => {
116             println!("Message:\n\t{}", msg);
117         }
118         None => {
119             println!("No panic message.");
120         }
121     }
122 
123     #[cfg(all(feature = "backtrace", target_arch = "x86_64"))]
124     {
125         unsafe {
126             let bt = mini_backtrace::Backtrace::<16>::capture();
127             println!("Rust Panic Backtrace:");
128             let mut level = 0;
129             for frame in bt.frames {
130                 lookup_kallsyms(frame as u64, level);
131                 level += 1;
132             }
133         };
134     }
135 
136     println!("Current PCB:\n\t{:?}", *(ProcessManager::current_pcb()));
137     ProcessManager::exit(usize::MAX);
138 }
139