lib.rs 2.0 KB

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