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 #[cfg(target_arch = "x86_64")] 16 #[path = "arch/x86_64/mod.rs"] 17 #[macro_use] 18 mod arch; 19 #[macro_use] 20 mod libs; 21 #[macro_use] 22 mod include; 23 mod driver; // 如果driver依赖了libs,应该在libs后面导出 24 mod exception; 25 mod filesystem; 26 mod io; 27 mod ipc; 28 mod mm; 29 mod net; 30 mod process; 31 mod sched; 32 mod smp; 33 mod syscall; 34 mod time; 35 36 #[macro_use] 37 extern crate alloc; 38 #[macro_use] 39 extern crate bitflags; 40 #[macro_use] 41 extern crate lazy_static; 42 43 extern crate num; 44 #[macro_use] 45 extern crate num_derive; 46 extern crate smoltcp; 47 extern crate thingbuf; 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 102 return 0; 103 } 104