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