lib.rs 1.9 KB

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