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