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 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::libs::lib_ui::textui::FontColor; 57 use crate::mm::allocator::kernel_allocator::KernelAllocator; 58 59 // <3> 60 use crate::{ 61 arch::asm::current::current_pcb, include::bindings::bindings::process_do_exit, 62 net::net_core::net_init, 63 }; 64 65 // 声明全局的分配器 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!(FontColor::GREEN, FontColor::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