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