xref: /DragonOS/kernel/src/lib.rs (revision 36fd013004ee0bd5fc7cfb452ba22531a83a859c)
1 #![no_std] // <1>
2 #![no_main] // <1>
3 #![feature(alloc_error_handler)]
4 #![feature(const_mut_refs)]
5 #![feature(core_intrinsics)] // <2>
6 #![feature(c_void_variant)]
7 #![feature(drain_filter)] // 允许Vec的drain_filter特性
8 #![feature(panic_info_message)]
9 #![feature(ptr_internals)]
10 #![feature(trait_upcasting)]
11 #[allow(non_upper_case_globals)]
12 #[allow(non_camel_case_types)]
13 #[allow(non_snake_case)]
14 use core::panic::PanicInfo;
15 
16 /// 导出x86_64架构相关的代码,命名为arch模块
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     net::net_core::net_init,
58 };
59 
60 // 声明全局的slab分配器
61 #[cfg_attr(not(test), global_allocator)]
62 pub static KERNEL_ALLOCATOR: KernelAllocator = KernelAllocator {};
63 
64 /// 全局的panic处理函数
65 #[panic_handler]
66 #[no_mangle]
67 pub fn panic(info: &PanicInfo) -> ! {
68     kerror!("Kernel Panic Occurred.");
69 
70     match info.location() {
71         Some(loc) => {
72             println!(
73                 "Location:\n\tFile: {}\n\tLine: {}, Column: {}",
74                 loc.file(),
75                 loc.line(),
76                 loc.column()
77             );
78         }
79         None => {
80             println!("No location info");
81         }
82     }
83 
84     match info.message() {
85         Some(msg) => {
86             println!("Message:\n\t{}", msg);
87         }
88         None => {
89             println!("No panic message.");
90         }
91     }
92 
93     println!("Current PCB:\n\t{:?}", current_pcb());
94     unsafe {
95         process_do_exit(u64::MAX);
96     };
97     loop {}
98 }
99 
100 /// 该函数用作测试,在process.c的initial_kernel_thread()中调用了此函数
101 #[no_mangle]
102 pub extern "C" fn __rust_demo_func() -> i32 {
103     printk_color!(GREEN, BLACK, "__rust_demo_func()\n");
104     let r = net_init();
105     if r.is_err() {
106         kwarn!("net_init() failed: {:?}", r.err().unwrap());
107     }
108     return 0;
109 }
110