xref: /DragonOS/kernel/src/lib.rs (revision 4f8f484930ed3c09ecf4b5b05b1dea14f7b05d8b)
1 #![no_main] // <1>
2 #![feature(alloc_error_handler)]
3 #![feature(allocator_api)]
4 #![feature(arbitrary_self_types)]
5 #![feature(asm_const)]
6 #![feature(concat_idents)]
7 #![feature(const_for)]
8 #![feature(const_mut_refs)]
9 #![feature(const_trait_impl)]
10 #![feature(const_transmute_copy)]
11 #![feature(const_refs_to_cell)]
12 #![feature(core_intrinsics)]
13 #![feature(c_void_variant)]
14 #![feature(extract_if)]
15 #![feature(fn_align)]
16 #![feature(inline_const)]
17 #![feature(naked_functions)]
18 #![feature(new_uninit)]
19 #![feature(panic_info_message)]
20 #![feature(ptr_internals)]
21 #![feature(ptr_to_from_bits)]
22 #![feature(trait_upcasting)]
23 #![feature(slice_ptr_get)]
24 #![feature(vec_into_raw_parts)]
25 #![cfg_attr(target_os = "none", no_std)]
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 misc;
50 mod mm;
51 mod net;
52 mod process;
53 mod sched;
54 mod smp;
55 mod syscall;
56 mod time;
57 
58 #[cfg(target_arch = "x86_64")]
59 mod virt;
60 
61 #[macro_use]
62 extern crate alloc;
63 #[macro_use]
64 extern crate atomic_enum;
65 #[macro_use]
66 extern crate bitflags;
67 extern crate elf;
68 #[macro_use]
69 extern crate lazy_static;
70 extern crate num;
71 #[macro_use]
72 extern crate num_derive;
73 extern crate smoltcp;
74 #[macro_use]
75 extern crate intertrait;
76 #[cfg(target_arch = "x86_64")]
77 extern crate x86;
78 
79 extern crate klog_types;
80 extern crate uefi;
81 extern crate uefi_raw;
82 
83 use crate::mm::allocator::kernel_allocator::KernelAllocator;
84 
85 use crate::process::ProcessManager;
86 
87 #[cfg(all(feature = "backtrace", target_arch = "x86_64"))]
88 extern crate mini_backtrace;
89 
90 extern "C" {
91     fn lookup_kallsyms(addr: u64, level: i32) -> i32;
92 }
93 
94 // 声明全局的分配器
95 #[cfg_attr(not(test), global_allocator)]
96 pub static KERNEL_ALLOCATOR: KernelAllocator = KernelAllocator;
97 
98 /// 全局的panic处理函数
99 #[cfg(target_os = "none")]
100 #[panic_handler]
101 #[no_mangle]
102 pub fn panic(info: &PanicInfo) -> ! {
103     kerror!("Kernel Panic Occurred.");
104 
105     match info.location() {
106         Some(loc) => {
107             println!(
108                 "Location:\n\tFile: {}\n\tLine: {}, Column: {}",
109                 loc.file(),
110                 loc.line(),
111                 loc.column()
112             );
113         }
114         None => {
115             println!("No location info");
116         }
117     }
118 
119     match info.message() {
120         Some(msg) => {
121             println!("Message:\n\t{}", msg);
122         }
123         None => {
124             println!("No panic message.");
125         }
126     }
127 
128     #[cfg(all(feature = "backtrace", target_arch = "x86_64"))]
129     {
130         unsafe {
131             let bt = mini_backtrace::Backtrace::<16>::capture();
132             println!("Rust Panic Backtrace:");
133             let mut level = 0;
134             for frame in bt.frames {
135                 lookup_kallsyms(frame as u64, level);
136                 level += 1;
137             }
138         };
139     }
140 
141     println!("Current PCB:\n\t{:?}", *(ProcessManager::current_pcb()));
142     ProcessManager::exit(usize::MAX);
143 }
144