xref: /DragonOS/kernel/src/lib.rs (revision 40fe15e0953f989ccfeb74826d61621d43dea6bb)
1 #![no_std] // <1>
2 #![no_main] // <1>
3 #![feature(alloc_error_handler)]
4 #![feature(allocator_api)]
5 #![feature(arbitrary_self_types)]
6 #![feature(const_mut_refs)]
7 #![feature(core_intrinsics)]
8 #![feature(c_void_variant)]
9 #![feature(drain_filter)]
10 #![feature(panic_info_message)]
11 #![feature(ptr_internals)]
12 #![feature(trait_upcasting)]
13 #![feature(slice_ptr_get)]
14 #![feature(vec_into_raw_parts)]
15 
16 #[allow(non_upper_case_globals)]
17 #[allow(non_camel_case_types)]
18 #[allow(non_snake_case)]
19 use core::panic::PanicInfo;
20 
21 /// 导出x86_64架构相关的代码,命名为arch模块
22 #[macro_use]
23 mod arch;
24 #[macro_use]
25 mod libs;
26 #[macro_use]
27 mod include;
28 mod driver; // 如果driver依赖了libs,应该在libs后面导出
29 mod exception;
30 mod filesystem;
31 mod io;
32 mod ipc;
33 mod mm;
34 mod net;
35 mod process;
36 mod sched;
37 mod smp;
38 mod syscall;
39 mod time;
40 
41 #[macro_use]
42 extern crate alloc;
43 #[macro_use]
44 extern crate bitflags;
45 extern crate elf;
46 #[macro_use]
47 extern crate lazy_static;
48 extern crate num;
49 #[macro_use]
50 extern crate num_derive;
51 extern crate smoltcp;
52 extern crate thingbuf;
53 #[cfg(target_arch = "x86_64")]
54 extern crate x86;
55 
56 use crate::mm::allocator::kernel_allocator::KernelAllocator;
57 
58 // <3>
59 use crate::{
60     arch::asm::current::current_pcb,
61     include::bindings::bindings::{process_do_exit, BLACK, GREEN},
62     net::net_core::net_init,
63 };
64 
65 // 声明全局的slab分配器
66 #[cfg_attr(not(test), global_allocator)]
67 pub static KERNEL_ALLOCATOR: KernelAllocator = KernelAllocator;
68 
69 /// 全局的panic处理函数
70 #[panic_handler]
71 #[no_mangle]
72 pub fn panic(info: &PanicInfo) -> ! {
73     kerror!("Kernel Panic Occurred.");
74 
75     match info.location() {
76         Some(loc) => {
77             println!(
78                 "Location:\n\tFile: {}\n\tLine: {}, Column: {}",
79                 loc.file(),
80                 loc.line(),
81                 loc.column()
82             );
83         }
84         None => {
85             println!("No location info");
86         }
87     }
88 
89     match info.message() {
90         Some(msg) => {
91             println!("Message:\n\t{}", msg);
92         }
93         None => {
94             println!("No panic message.");
95         }
96     }
97 
98     println!("Current PCB:\n\t{:?}", current_pcb());
99     unsafe {
100         process_do_exit(u64::MAX);
101     };
102     loop {}
103 }
104 
105 /// 该函数用作测试,在process.c的initial_kernel_thread()中调用了此函数
106 #[no_mangle]
107 pub extern "C" fn __rust_demo_func() -> i32 {
108     printk_color!(GREEN, BLACK, "__rust_demo_func()\n");
109     let r = net_init();
110     if r.is_err() {
111         kwarn!("net_init() failed: {:?}", r.err().unwrap());
112     }
113     return 0;
114 }
115