xref: /DragonOS/kernel/src/lib.rs (revision 597ecc08c2444dcc8f527eb021932718b69c9cc5)
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_transmute_copy)]
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(inline_const)]
17 #![feature(naked_functions)]
18 #![feature(new_uninit)]
19 #![feature(panic_info_message)]
20 #![feature(ptr_internals)]
21 #![feature(ptr_to_from_bits)]
22 #![feature(trait_upcasting)]
23 #![feature(slice_ptr_get)]
24 #![feature(vec_into_raw_parts)]
25 #![cfg_attr(target_os = "none", no_std)]
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 
89 use crate::mm::allocator::kernel_allocator::KernelAllocator;
90 
91 use crate::process::ProcessManager;
92 
93 #[cfg(all(feature = "backtrace", target_arch = "x86_64"))]
94 extern crate mini_backtrace;
95 
96 extern "C" {
97     fn lookup_kallsyms(addr: u64, level: i32) -> i32;
98 }
99 
100 // 声明全局的分配器
101 #[cfg_attr(not(test), global_allocator)]
102 pub static KERNEL_ALLOCATOR: KernelAllocator = KernelAllocator;
103 
104 /// 全局的panic处理函数
105 #[cfg(target_os = "none")]
106 #[panic_handler]
107 #[no_mangle]
108 pub fn panic(info: &PanicInfo) -> ! {
109     kerror!("Kernel Panic Occurred.");
110 
111     match info.location() {
112         Some(loc) => {
113             println!(
114                 "Location:\n\tFile: {}\n\tLine: {}, Column: {}",
115                 loc.file(),
116                 loc.line(),
117                 loc.column()
118             );
119         }
120         None => {
121             println!("No location info");
122         }
123     }
124 
125     match info.message() {
126         Some(msg) => {
127             println!("Message:\n\t{}", msg);
128         }
129         None => {
130             println!("No panic message.");
131         }
132     }
133 
134     #[cfg(all(feature = "backtrace", target_arch = "x86_64"))]
135     {
136         unsafe {
137             let bt = mini_backtrace::Backtrace::<16>::capture();
138             println!("Rust Panic Backtrace:");
139             let mut level = 0;
140             for frame in bt.frames {
141                 lookup_kallsyms(frame as u64, level);
142                 level += 1;
143             }
144         };
145     }
146 
147     println!("Current PCB:\n\t{:?}", (ProcessManager::current_pcb()));
148     ProcessManager::exit(usize::MAX);
149 }
150