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