xref: /DragonOS/kernel/src/lib.rs (revision ab5c8ca46db8e7d4793a9791292122b0b9684274)
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)]
9 #![feature(trait_upcasting)]
10 #[allow(non_upper_case_globals)]
11 #[allow(non_camel_case_types)]
12 #[allow(non_snake_case)]
13 use core::panic::PanicInfo;
14 
15 /// 导出x86_64架构相关的代码,命名为arch模块
16 #[macro_use]
17 mod arch;
18 #[macro_use]
19 mod libs;
20 #[macro_use]
21 mod include;
22 mod driver; // 如果driver依赖了libs,应该在libs后面导出
23 mod exception;
24 mod filesystem;
25 mod io;
26 mod ipc;
27 mod mm;
28 mod net;
29 mod process;
30 mod sched;
31 mod smp;
32 mod syscall;
33 mod time;
34 
35 #[macro_use]
36 extern crate alloc;
37 #[macro_use]
38 extern crate bitflags;
39 #[macro_use]
40 extern crate lazy_static;
41 extern crate num;
42 #[macro_use]
43 extern crate num_derive;
44 extern crate smoltcp;
45 extern crate thingbuf;
46 
47 #[cfg(target_arch = "x86_64")]
48 extern crate x86;
49 
50 use mm::allocator::KernelAllocator;
51 
52 // <3>
53 use crate::{
54     arch::asm::current::current_pcb,
55     include::bindings::bindings::{process_do_exit, BLACK, GREEN},
56     net::net_core::net_init,
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     let r = net_init();
104     if r.is_err() {
105         kwarn!("net_init() failed: {:?}", r.err().unwrap());
106     }
107     return 0;
108 }
109