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