xref: /DragonOS/kernel/src/lib.rs (revision 2291ffdece1dc5a703602f79f74df8a4854d215b)
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 
8 #[allow(non_upper_case_globals)]
9 #[allow(non_camel_case_types)]
10 #[allow(non_snake_case)]
11 use core::panic::PanicInfo;
12 
13 #[macro_use]
14 mod arch;
15 #[macro_use]
16 mod include;
17 mod ipc;
18 
19 #[macro_use]
20 mod libs;
21 mod mm;
22 mod process;
23 mod sched;
24 mod smp;
25 mod driver;
26 
27 extern crate alloc;
28 
29 use mm::allocator::KernelAllocator;
30 
31 // <3>
32 use crate::{include::bindings::bindings::{process_do_exit, BLACK, GREEN}, arch::x86_64::asm::current::current_pcb};
33 
34 // 声明全局的slab分配器
35 #[cfg_attr(not(test), global_allocator)]
36 pub static KERNEL_ALLOCATOR: KernelAllocator = KernelAllocator {};
37 
38 /// 全局的panic处理函数
39 #[panic_handler]
40 #[no_mangle]
41 pub fn panic(info: &PanicInfo) -> ! {
42     kerror!("Kernel Panic Occurred.");
43 
44     match info.location() {
45         Some(loc) => {
46             println!(
47                 "Location:\n\tFile: {}\n\tLine: {}, Column: {}",
48                 loc.file(),
49                 loc.line(),
50                 loc.column()
51             );
52         }
53         None => {
54             println!("No location info");
55         }
56     }
57 
58     match info.message() {
59         Some(msg) => {
60             println!("Message:\n\t{}", msg);
61         }
62         None => {
63             println!("No panic message.");
64         }
65     }
66 
67     println!("Current PCB:\n\t{:?}", current_pcb());
68     unsafe {
69         process_do_exit(u64::MAX);
70     };
71     loop {
72 
73     }
74 }
75 
76 /// 该函数用作测试,在process.c的initial_kernel_thread()中调用了此函数
77 #[no_mangle]
78 pub extern "C" fn __rust_demo_func() -> i32 {
79     printk_color!(GREEN, BLACK, "__rust_demo_func()\n");
80 
81     return 0;
82 }
83