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 extern crate num; 43 #[macro_use] 44 extern crate num_derive; 45 extern crate smoltcp; 46 extern crate thingbuf; 47 48 use mm::allocator::KernelAllocator; 49 50 // <3> 51 use crate::{ 52 arch::asm::current::current_pcb, 53 include::bindings::bindings::{process_do_exit, BLACK, GREEN}, filesystem::vfs::ROOT_INODE, 54 }; 55 56 // 声明全局的slab分配器 57 #[cfg_attr(not(test), global_allocator)] 58 pub static KERNEL_ALLOCATOR: KernelAllocator = KernelAllocator {}; 59 60 /// 全局的panic处理函数 61 #[panic_handler] 62 #[no_mangle] 63 pub fn panic(info: &PanicInfo) -> ! { 64 kerror!("Kernel Panic Occurred."); 65 66 match info.location() { 67 Some(loc) => { 68 println!( 69 "Location:\n\tFile: {}\n\tLine: {}, Column: {}", 70 loc.file(), 71 loc.line(), 72 loc.column() 73 ); 74 } 75 None => { 76 println!("No location info"); 77 } 78 } 79 80 match info.message() { 81 Some(msg) => { 82 println!("Message:\n\t{}", msg); 83 } 84 None => { 85 println!("No panic message."); 86 } 87 } 88 89 println!("Current PCB:\n\t{:?}", current_pcb()); 90 unsafe { 91 process_do_exit(u64::MAX); 92 }; 93 loop {} 94 } 95 96 /// 该函数用作测试,在process.c的initial_kernel_thread()中调用了此函数 97 #[no_mangle] 98 pub extern "C" fn __rust_demo_func() -> i32 { 99 printk_color!(GREEN, BLACK, "__rust_demo_func()\n"); 100 101 return 0; 102 } 103