lib.rs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #![no_main] // <1>
  2. #![feature(alloc_error_handler)]
  3. #![feature(allocator_api)]
  4. #![feature(arbitrary_self_types)]
  5. #![feature(asm_const)]
  6. #![feature(concat_idents)]
  7. #![feature(const_for)]
  8. #![feature(const_mut_refs)]
  9. #![feature(const_option)]
  10. #![feature(const_trait_impl)]
  11. #![feature(const_refs_to_cell)]
  12. #![feature(core_intrinsics)]
  13. #![feature(c_void_variant)]
  14. #![feature(extract_if)]
  15. #![feature(fn_align)]
  16. #![feature(linked_list_retain)]
  17. #![feature(naked_functions)]
  18. #![feature(new_uninit)]
  19. #![feature(ptr_internals)]
  20. #![feature(trait_upcasting)]
  21. #![feature(slice_ptr_get)]
  22. #![feature(sync_unsafe_cell)]
  23. #![feature(vec_into_raw_parts)]
  24. #![cfg_attr(target_os = "none", no_std)]
  25. #![allow(internal_features)]
  26. // clippy的配置
  27. #![deny(clippy::all)]
  28. #![allow(clippy::bad_bit_mask)]
  29. // DragonOS允许在函数中使用return语句(尤其是长函数时,我们推荐这么做)
  30. #![allow(clippy::let_and_return)]
  31. #![allow(clippy::needless_pass_by_ref_mut)]
  32. #![allow(clippy::needless_return)]
  33. #![allow(clippy::single_char_pattern)]
  34. #![allow(clippy::upper_case_acronyms)]
  35. #[cfg(test)]
  36. #[macro_use]
  37. extern crate std;
  38. use core::panic::PanicInfo;
  39. /// 导出x86_64架构相关的代码,命名为arch模块
  40. #[macro_use]
  41. mod arch;
  42. #[macro_use]
  43. mod libs;
  44. #[macro_use]
  45. mod include;
  46. mod debug;
  47. mod driver; // 如果driver依赖了libs,应该在libs后面导出
  48. mod exception;
  49. mod filesystem;
  50. mod init;
  51. mod ipc;
  52. mod misc;
  53. mod mm;
  54. mod net;
  55. mod process;
  56. mod sched;
  57. mod smp;
  58. mod syscall;
  59. mod time;
  60. #[cfg(target_arch = "x86_64")]
  61. mod virt;
  62. #[macro_use]
  63. extern crate alloc;
  64. #[macro_use]
  65. extern crate atomic_enum;
  66. #[macro_use]
  67. extern crate bitflags;
  68. extern crate elf;
  69. #[macro_use]
  70. extern crate lazy_static;
  71. extern crate num;
  72. #[macro_use]
  73. extern crate num_derive;
  74. extern crate smoltcp;
  75. #[macro_use]
  76. extern crate intertrait;
  77. #[cfg(target_arch = "x86_64")]
  78. extern crate x86;
  79. extern crate klog_types;
  80. extern crate uefi;
  81. extern crate uefi_raw;
  82. #[macro_use]
  83. extern crate wait_queue_macros;
  84. use crate::mm::allocator::kernel_allocator::KernelAllocator;
  85. use crate::process::ProcessManager;
  86. #[cfg(all(feature = "backtrace", target_arch = "x86_64"))]
  87. extern crate mini_backtrace;
  88. extern "C" {
  89. fn lookup_kallsyms(addr: u64, level: i32) -> i32;
  90. }
  91. // 声明全局的分配器
  92. #[cfg_attr(not(test), global_allocator)]
  93. pub static KERNEL_ALLOCATOR: KernelAllocator = KernelAllocator;
  94. /// 全局的panic处理函数
  95. #[cfg(target_os = "none")]
  96. #[panic_handler]
  97. #[no_mangle]
  98. pub fn panic(info: &PanicInfo) -> ! {
  99. use log::error;
  100. error!("Kernel Panic Occurred.");
  101. match info.location() {
  102. Some(loc) => {
  103. println!(
  104. "Location:\n\tFile: {}\n\tLine: {}, Column: {}",
  105. loc.file(),
  106. loc.line(),
  107. loc.column()
  108. );
  109. }
  110. None => {
  111. println!("No location info");
  112. }
  113. }
  114. println!("Message:\n\t{}", info.message());
  115. #[cfg(all(feature = "backtrace", target_arch = "x86_64"))]
  116. {
  117. unsafe {
  118. let bt = mini_backtrace::Backtrace::<16>::capture();
  119. println!("Rust Panic Backtrace:");
  120. let mut level = 0;
  121. for frame in bt.frames {
  122. lookup_kallsyms(frame as u64, level);
  123. level += 1;
  124. }
  125. };
  126. }
  127. println!("Current PCB:\n\t{:?}", (ProcessManager::current_pcb()));
  128. ProcessManager::exit(usize::MAX);
  129. }