1 #![no_std] // <1> 2 #![no_main] // <1> 3 #![feature(core_intrinsics)] // <2> 4 #![feature(alloc_error_handler)] 5 #![feature(panic_info_message)] 6 #![feature(drain_filter)] // 允许Vec的drain_filter特性 7 8 #[allow(non_upper_case_globals)] 9 #[allow(non_camel_case_types)] 10 #[allow(non_snake_case)] 11 use core::panic::PanicInfo; 12 13 /// 导出x86_64架构相关的代码,命名为arch模块 14 #[cfg(target_arch = "x86_64")] 15 #[path = "arch/x86_64/mod.rs"] 16 #[macro_use] 17 mod arch; 18 19 #[macro_use] 20 mod include; 21 mod ipc; 22 23 #[macro_use] 24 mod libs; 25 mod driver; 26 mod mm; 27 mod process; 28 mod sched; 29 mod smp; 30 mod time; 31 32 33 34 extern crate alloc; 35 36 use mm::allocator::KernelAllocator; 37 38 // <3> 39 use crate::{ 40 arch::asm::current::current_pcb, 41 include::bindings::bindings::{process_do_exit, BLACK, GREEN}, 42 }; 43 44 // 声明全局的slab分配器 45 #[cfg_attr(not(test), global_allocator)] 46 pub static KERNEL_ALLOCATOR: KernelAllocator = KernelAllocator {}; 47 48 /// 全局的panic处理函数 49 #[panic_handler] 50 #[no_mangle] 51 pub fn panic(info: &PanicInfo) -> ! { 52 kerror!("Kernel Panic Occurred."); 53 54 match info.location() { 55 Some(loc) => { 56 println!( 57 "Location:\n\tFile: {}\n\tLine: {}, Column: {}", 58 loc.file(), 59 loc.line(), 60 loc.column() 61 ); 62 } 63 None => { 64 println!("No location info"); 65 } 66 } 67 68 match info.message() { 69 Some(msg) => { 70 println!("Message:\n\t{}", msg); 71 } 72 None => { 73 println!("No panic message."); 74 } 75 } 76 77 println!("Current PCB:\n\t{:?}", current_pcb()); 78 unsafe { 79 process_do_exit(u64::MAX); 80 }; 81 loop {} 82 } 83 84 /// 该函数用作测试,在process.c的initial_kernel_thread()中调用了此函数 85 #[no_mangle] 86 pub extern "C" fn __rust_demo_func() -> i32 { 87 printk_color!(GREEN, BLACK, "__rust_demo_func()\n"); 88 return 0; 89 } 90