xref: /DragonOS/kernel/src/lib.rs (revision 004e86ff19727df303c23b42c7a271b9214c6898)
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 
42 use mm::allocator::KernelAllocator;
43 
44 // <3>
45 use crate::{
46     arch::asm::current::current_pcb,
47     include::bindings::bindings::{process_do_exit, BLACK, GREEN},
48 };
49 
50 // 声明全局的slab分配器
51 #[cfg_attr(not(test), global_allocator)]
52 pub static KERNEL_ALLOCATOR: KernelAllocator = KernelAllocator {};
53 
54 /// 全局的panic处理函数
55 #[panic_handler]
56 #[no_mangle]
57 pub fn panic(info: &PanicInfo) -> ! {
58     kerror!("Kernel Panic Occurred.");
59 
60     match info.location() {
61         Some(loc) => {
62             println!(
63                 "Location:\n\tFile: {}\n\tLine: {}, Column: {}",
64                 loc.file(),
65                 loc.line(),
66                 loc.column()
67             );
68         }
69         None => {
70             println!("No location info");
71         }
72     }
73 
74     match info.message() {
75         Some(msg) => {
76             println!("Message:\n\t{}", msg);
77         }
78         None => {
79             println!("No panic message.");
80         }
81     }
82 
83     println!("Current PCB:\n\t{:?}", current_pcb());
84     unsafe {
85         process_do_exit(u64::MAX);
86     };
87     loop {}
88 }
89 
90 /// 该函数用作测试,在process.c的initial_kernel_thread()中调用了此函数
91 #[no_mangle]
92 pub extern "C" fn __rust_demo_func() -> i32 {
93     printk_color!(GREEN, BLACK, "__rust_demo_func()\n");
94 
95     return 0;
96 }
97