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