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