xref: /DragonOS/kernel/src/lib.rs (revision c2481452f81750ec02adec627ab2edbc93d9cd9c)
1 #![no_std] // <1>
2 #![no_main] // <1>
3 #![feature(const_mut_refs)]
4 #![feature(core_intrinsics)] // <2>
5 #![feature(alloc_error_handler)]
6 #![feature(panic_info_message)]
7 #![feature(drain_filter)] // 允许Vec的drain_filter特性
8 #![feature(c_void_variant)] // used in kernel/src/exception/softirq.rs
9 #[allow(non_upper_case_globals)]
10 #[allow(non_camel_case_types)]
11 #[allow(non_snake_case)]
12 use core::panic::PanicInfo;
13 
14 /// 导出x86_64架构相关的代码,命名为arch模块
15 #[cfg(target_arch = "x86_64")]
16 #[path = "arch/x86_64/mod.rs"]
17 #[macro_use]
18 mod arch;
19 
20 mod driver;
21 mod filesystem;
22 #[macro_use]
23 mod include;
24 mod ipc;
25 #[macro_use]
26 mod libs;
27 mod exception;
28 pub mod io;
29 mod mm;
30 mod process;
31 mod sched;
32 mod smp;
33 mod time;
34 
35 extern crate alloc;
36 #[macro_use]
37 extern crate lazy_static;
38 
39 use mm::allocator::KernelAllocator;
40 
41 // <3>
42 use crate::{
43     arch::asm::current::current_pcb,
44     include::bindings::bindings::{process_do_exit, BLACK, GREEN},
45 };
46 
47 // 声明全局的slab分配器
48 #[cfg_attr(not(test), global_allocator)]
49 pub static KERNEL_ALLOCATOR: KernelAllocator = KernelAllocator {};
50 
51 /// 全局的panic处理函数
52 #[panic_handler]
53 #[no_mangle]
54 pub fn panic(info: &PanicInfo) -> ! {
55     kerror!("Kernel Panic Occurred.");
56 
57     match info.location() {
58         Some(loc) => {
59             println!(
60                 "Location:\n\tFile: {}\n\tLine: {}, Column: {}",
61                 loc.file(),
62                 loc.line(),
63                 loc.column()
64             );
65         }
66         None => {
67             println!("No location info");
68         }
69     }
70 
71     match info.message() {
72         Some(msg) => {
73             println!("Message:\n\t{}", msg);
74         }
75         None => {
76             println!("No panic message.");
77         }
78     }
79 
80     println!("Current PCB:\n\t{:?}", current_pcb());
81     unsafe {
82         process_do_exit(u64::MAX);
83     };
84     loop {}
85 }
86 
87 /// 该函数用作测试,在process.c的initial_kernel_thread()中调用了此函数
88 #[no_mangle]
89 pub extern "C" fn __rust_demo_func() -> i32 {
90     printk_color!(GREEN, BLACK, "__rust_demo_func()\n");
91 
92     return 0;
93 }
94