xref: /DragonOS/kernel/src/lib.rs (revision 11f78b73e7b18ef04e05e63612f8027eda0740e7)
1 #![no_main] // <1>
2 #![feature(alloc_error_handler)]
3 #![feature(allocator_api)]
4 #![feature(arbitrary_self_types)]
5 #![feature(asm_const)]
6 #![feature(const_mut_refs)]
7 #![feature(const_trait_impl)]
8 #![feature(const_refs_to_cell)]
9 #![feature(core_intrinsics)]
10 #![feature(cstr_from_bytes_until_nul)]
11 #![feature(c_void_variant)]
12 #![feature(drain_filter)]
13 #![feature(inline_const)]
14 #![feature(is_some_and)]
15 #![feature(naked_functions)]
16 #![feature(panic_info_message)]
17 #![feature(ptr_internals)]
18 #![feature(trait_upcasting)]
19 #![feature(slice_ptr_get)]
20 #![feature(vec_into_raw_parts)]
21 #![feature(new_uninit)]
22 #![feature(ptr_to_from_bits)]
23 #![feature(concat_idents)]
24 #![cfg_attr(target_os = "none", no_std)]
25 #![feature(atomic_mut_ptr)]
26 
27 #[cfg(test)]
28 #[macro_use]
29 extern crate std;
30 
31 #[allow(non_upper_case_globals)]
32 #[allow(non_camel_case_types)]
33 #[allow(non_snake_case)]
34 use core::panic::PanicInfo;
35 
36 /// 导出x86_64架构相关的代码,命名为arch模块
37 #[macro_use]
38 mod arch;
39 #[macro_use]
40 mod libs;
41 #[macro_use]
42 mod include;
43 mod debug;
44 mod driver; // 如果driver依赖了libs,应该在libs后面导出
45 mod exception;
46 mod filesystem;
47 mod init;
48 mod ipc;
49 mod mm;
50 mod net;
51 mod process;
52 mod sched;
53 mod smp;
54 mod syscall;
55 mod time;
56 mod virt;
57 
58 #[macro_use]
59 extern crate alloc;
60 #[macro_use]
61 extern crate atomic_enum;
62 #[macro_use]
63 extern crate bitflags;
64 extern crate elf;
65 #[macro_use]
66 extern crate lazy_static;
67 extern crate num;
68 #[macro_use]
69 extern crate num_derive;
70 extern crate smoltcp;
71 #[macro_use]
72 extern crate intertrait;
73 #[cfg(target_arch = "x86_64")]
74 extern crate x86;
75 
76 extern crate klog_types;
77 
78 use crate::mm::allocator::kernel_allocator::KernelAllocator;
79 
80 use crate::process::ProcessManager;
81 
82 #[cfg(feature = "backtrace")]
83 extern crate mini_backtrace;
84 
85 extern "C" {
86     fn lookup_kallsyms(addr: u64, level: i32) -> i32;
87 }
88 
89 // 声明全局的分配器
90 #[cfg_attr(not(test), global_allocator)]
91 pub static KERNEL_ALLOCATOR: KernelAllocator = KernelAllocator;
92 
93 /// 全局的panic处理函数
94 #[cfg(target_os = "none")]
95 #[panic_handler]
96 #[no_mangle]
97 pub fn panic(info: &PanicInfo) -> ! {
98     kerror!("Kernel Panic Occurred.");
99 
100     match info.location() {
101         Some(loc) => {
102             println!(
103                 "Location:\n\tFile: {}\n\tLine: {}, Column: {}",
104                 loc.file(),
105                 loc.line(),
106                 loc.column()
107             );
108         }
109         None => {
110             println!("No location info");
111         }
112     }
113 
114     match info.message() {
115         Some(msg) => {
116             println!("Message:\n\t{}", msg);
117         }
118         None => {
119             println!("No panic message.");
120         }
121     }
122 
123     #[cfg(feature = "backtrace")]
124     {
125         unsafe {
126             let bt = mini_backtrace::Backtrace::<16>::capture();
127             println!("Rust Panic Backtrace:");
128             let mut level = 0;
129             for frame in bt.frames {
130                 lookup_kallsyms(frame as u64, level);
131                 level += 1;
132             }
133         };
134     }
135 
136     println!("Current PCB:\n\t{:?}", *(ProcessManager::current_pcb()));
137     ProcessManager::exit(usize::MAX);
138 }
139