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