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