xref: /DragonOS/kernel/src/lib.rs (revision 7c28051e8c601312d3d0fd7bcb71bc71450d10c0)
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 #![cfg_attr(target_os = "none", no_std)]
21 #![allow(static_mut_refs, non_local_definitions, internal_features)]
22 // clippy的配置
23 #![deny(clippy::all)]
24 // DragonOS允许在函数中使用return语句(尤其是长函数时,我们推荐这么做)
25 #![allow(
26     clippy::macro_metavars_in_unsafe,
27     clippy::upper_case_acronyms,
28     clippy::single_char_pattern,
29     clippy::needless_return,
30     clippy::needless_pass_by_ref_mut,
31     clippy::let_and_return,
32     clippy::bad_bit_mask
33 )]
34 
35 #[cfg(test)]
36 #[macro_use]
37 extern crate std;
38 
39 use core::panic::PanicInfo;
40 
41 /// 导出x86_64架构相关的代码,命名为arch模块
42 #[macro_use]
43 mod arch;
44 #[macro_use]
45 mod libs;
46 #[macro_use]
47 mod include;
48 mod bpf;
49 mod cgroup;
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 namespaces;
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 #[cfg(all(feature = "backtrace", target_arch = "x86_64"))]
97 extern crate mini_backtrace;
98 
99 extern "C" {
lookup_kallsyms(addr: u64, level: i32) -> i32100     fn lookup_kallsyms(addr: u64, level: i32) -> i32;
101 }
102 
103 // 声明全局的分配器
104 #[cfg_attr(not(test), global_allocator)]
105 pub static KERNEL_ALLOCATOR: KernelAllocator = KernelAllocator;
106 
107 /// 全局的panic处理函数
108 #[cfg(target_os = "none")]
109 #[panic_handler]
110 #[no_mangle]
panic(info: &PanicInfo) -> !111 pub fn panic(info: &PanicInfo) -> ! {
112     use log::error;
113     use process::ProcessManager;
114 
115     error!("Kernel Panic Occurred.");
116 
117     match info.location() {
118         Some(loc) => {
119             println!(
120                 "Location:\n\tFile: {}\n\tLine: {}, Column: {}",
121                 loc.file(),
122                 loc.line(),
123                 loc.column()
124             );
125         }
126         None => {
127             println!("No location info");
128         }
129     }
130     println!("Message:\n\t{}", info.message());
131 
132     #[cfg(all(feature = "backtrace", target_arch = "x86_64"))]
133     {
134         unsafe {
135             let bt = mini_backtrace::Backtrace::<16>::capture();
136             println!("Rust Panic Backtrace:");
137             let mut level = 0;
138             for frame in bt.frames {
139                 lookup_kallsyms(frame as u64, level);
140                 level += 1;
141             }
142         };
143     }
144 
145     println!("Current PCB:\n\t{:?}", (ProcessManager::current_pcb()));
146 
147     ProcessManager::exit(usize::MAX);
148 }
149