xref: /DragonOS/kernel/src/lib.rs (revision 0d48c3c9c21a2dd470d0e1e58b507db60e0887bb)
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 #[macro_use]
20 mod libs;
21 #[macro_use]
22 mod include;
23 mod driver; // 如果driver依赖了libs,应该在libs后面导出
24 mod exception;
25 mod filesystem;
26 mod io;
27 mod ipc;
28 mod mm;
29 mod process;
30 mod sched;
31 mod smp;
32 mod time;
33 
34 #[macro_use]
35 extern crate alloc;
36 #[macro_use]
37 extern crate lazy_static;
38 
39 #[macro_use]
40 extern crate bitflags;
41 extern crate thingbuf;
42 
43 use mm::allocator::KernelAllocator;
44 
45 // <3>
46 use crate::{
47     arch::asm::current::current_pcb,
48     include::bindings::bindings::{process_do_exit, BLACK, GREEN},
49 };
50 
51 // 声明全局的slab分配器
52 #[cfg_attr(not(test), global_allocator)]
53 pub static KERNEL_ALLOCATOR: KernelAllocator = KernelAllocator {};
54 
55 /// 全局的panic处理函数
56 #[panic_handler]
57 #[no_mangle]
58 pub fn panic(info: &PanicInfo) -> ! {
59     kerror!("Kernel Panic Occurred.");
60 
61     match info.location() {
62         Some(loc) => {
63             println!(
64                 "Location:\n\tFile: {}\n\tLine: {}, Column: {}",
65                 loc.file(),
66                 loc.line(),
67                 loc.column()
68             );
69         }
70         None => {
71             println!("No location info");
72         }
73     }
74 
75     match info.message() {
76         Some(msg) => {
77             println!("Message:\n\t{}", msg);
78         }
79         None => {
80             println!("No panic message.");
81         }
82     }
83 
84     println!("Current PCB:\n\t{:?}", current_pcb());
85     unsafe {
86         process_do_exit(u64::MAX);
87     };
88     loop {}
89 }
90 
91 /// 该函数用作测试,在process.c的initial_kernel_thread()中调用了此函数
92 #[no_mangle]
93 pub extern "C" fn __rust_demo_func() -> i32 {
94     printk_color!(GREEN, BLACK, "__rust_demo_func()\n");
95 
96     return 0;
97 }
98