xref: /DragonOS/kernel/src/lib.rs (revision 55e6f0b65f91b32638fd56581f711a816eccdcd1)
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 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 net;
60 mod perf;
61 mod process;
62 mod sched;
63 mod smp;
64 mod syscall;
65 mod time;
66 #[cfg(target_arch = "x86_64")]
67 mod virt;
68 
69 #[macro_use]
70 extern crate alloc;
71 #[macro_use]
72 extern crate atomic_enum;
73 #[macro_use]
74 extern crate bitflags;
75 extern crate elf;
76 #[macro_use]
77 extern crate lazy_static;
78 extern crate num;
79 #[macro_use]
80 extern crate num_derive;
81 extern crate smoltcp;
82 #[macro_use]
83 extern crate intertrait;
84 #[cfg(target_arch = "x86_64")]
85 extern crate x86;
86 #[macro_use]
87 extern crate kcmdline_macros;
88 extern crate klog_types;
89 extern crate uefi;
90 extern crate uefi_raw;
91 #[macro_use]
92 extern crate wait_queue_macros;
93 
94 use crate::mm::allocator::kernel_allocator::KernelAllocator;
95 
96 use crate::process::ProcessManager;
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 
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