xref: /DragonOS/kernel/src/lib.rs (revision 3bc96fa4a9c01d91cddeb152fe78d6408351c29f)
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_transmute_copy)]
10 #![feature(const_refs_to_cell)]
11 #![feature(core_intrinsics)]
12 #![feature(c_void_variant)]
13 #![feature(extract_if)]
14 #![feature(inline_const)]
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 
26 #[cfg(test)]
27 #[macro_use]
28 extern crate std;
29 
30 #[allow(non_upper_case_globals)]
31 #[allow(non_camel_case_types)]
32 #[allow(non_snake_case)]
33 use core::panic::PanicInfo;
34 
35 /// 导出x86_64架构相关的代码,命名为arch模块
36 #[macro_use]
37 mod arch;
38 #[macro_use]
39 mod libs;
40 #[macro_use]
41 mod include;
42 mod debug;
43 mod driver; // 如果driver依赖了libs,应该在libs后面导出
44 mod exception;
45 mod filesystem;
46 mod init;
47 mod ipc;
48 mod mm;
49 mod net;
50 mod process;
51 mod sched;
52 mod smp;
53 mod syscall;
54 mod time;
55 
56 #[cfg(target_arch = "x86_64")]
57 mod virt;
58 
59 #[macro_use]
60 extern crate alloc;
61 #[macro_use]
62 extern crate atomic_enum;
63 #[macro_use]
64 extern crate bitflags;
65 extern crate elf;
66 #[macro_use]
67 extern crate lazy_static;
68 extern crate num;
69 #[macro_use]
70 extern crate num_derive;
71 extern crate smoltcp;
72 #[macro_use]
73 extern crate intertrait;
74 #[cfg(target_arch = "x86_64")]
75 extern crate x86;
76 
77 extern crate klog_types;
78 extern crate uefi;
79 extern crate uefi_raw;
80 
81 use crate::mm::allocator::kernel_allocator::KernelAllocator;
82 
83 use crate::process::ProcessManager;
84 
85 #[cfg(all(feature = "backtrace", target_arch = "x86_64"))]
86 extern crate mini_backtrace;
87 
88 extern "C" {
89     fn lookup_kallsyms(addr: u64, level: i32) -> i32;
90 }
91 
92 // 声明全局的分配器
93 #[cfg_attr(not(test), global_allocator)]
94 pub static KERNEL_ALLOCATOR: KernelAllocator = KernelAllocator;
95 
96 /// 全局的panic处理函数
97 #[cfg(target_os = "none")]
98 #[panic_handler]
99 #[no_mangle]
100 pub fn panic(info: &PanicInfo) -> ! {
101     kerror!("Kernel Panic Occurred.");
102 
103     match info.location() {
104         Some(loc) => {
105             println!(
106                 "Location:\n\tFile: {}\n\tLine: {}, Column: {}",
107                 loc.file(),
108                 loc.line(),
109                 loc.column()
110             );
111         }
112         None => {
113             println!("No location info");
114         }
115     }
116 
117     match info.message() {
118         Some(msg) => {
119             println!("Message:\n\t{}", msg);
120         }
121         None => {
122             println!("No panic message.");
123         }
124     }
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     ProcessManager::exit(usize::MAX);
141 }
142