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