lib.rs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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_trait_impl)]
  10. #![feature(const_transmute_copy)]
  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(inline_const)]
  17. #![feature(naked_functions)]
  18. #![feature(new_uninit)]
  19. #![feature(panic_info_message)]
  20. #![feature(ptr_internals)]
  21. #![feature(ptr_to_from_bits)]
  22. #![feature(trait_upcasting)]
  23. #![feature(slice_ptr_get)]
  24. #![feature(vec_into_raw_parts)]
  25. #![cfg_attr(target_os = "none", no_std)]
  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. use crate::mm::allocator::kernel_allocator::KernelAllocator;
  83. use crate::process::ProcessManager;
  84. #[cfg(all(feature = "backtrace", target_arch = "x86_64"))]
  85. extern crate mini_backtrace;
  86. extern "C" {
  87. fn lookup_kallsyms(addr: u64, level: i32) -> i32;
  88. }
  89. // 声明全局的分配器
  90. #[cfg_attr(not(test), global_allocator)]
  91. pub static KERNEL_ALLOCATOR: KernelAllocator = KernelAllocator;
  92. /// 全局的panic处理函数
  93. #[cfg(target_os = "none")]
  94. #[panic_handler]
  95. #[no_mangle]
  96. pub fn panic(info: &PanicInfo) -> ! {
  97. use log::error;
  98. error!("Kernel Panic Occurred.");
  99. match info.location() {
  100. Some(loc) => {
  101. println!(
  102. "Location:\n\tFile: {}\n\tLine: {}, Column: {}",
  103. loc.file(),
  104. loc.line(),
  105. loc.column()
  106. );
  107. }
  108. None => {
  109. println!("No location info");
  110. }
  111. }
  112. match info.message() {
  113. Some(msg) => {
  114. println!("Message:\n\t{}", msg);
  115. }
  116. None => {
  117. println!("No panic message.");
  118. }
  119. }
  120. #[cfg(all(feature = "backtrace", target_arch = "x86_64"))]
  121. {
  122. unsafe {
  123. let bt = mini_backtrace::Backtrace::<16>::capture();
  124. println!("Rust Panic Backtrace:");
  125. let mut level = 0;
  126. for frame in bt.frames {
  127. lookup_kallsyms(frame as u64, level);
  128. level += 1;
  129. }
  130. };
  131. }
  132. println!("Current PCB:\n\t{:?}", (ProcessManager::current_pcb()));
  133. ProcessManager::exit(usize::MAX);
  134. }