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