lib.rs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. // DragonOS允许在函数中使用return语句(尤其是长函数时,我们推荐这么做)
  29. #![allow(clippy::let_and_return)]
  30. #![allow(clippy::needless_pass_by_ref_mut)]
  31. #![allow(clippy::needless_return)]
  32. #![allow(clippy::upper_case_acronyms)]
  33. #[cfg(test)]
  34. #[macro_use]
  35. extern crate std;
  36. #[allow(non_upper_case_globals)]
  37. #[allow(non_camel_case_types)]
  38. #[allow(non_snake_case)]
  39. use core::panic::PanicInfo;
  40. /// 导出x86_64架构相关的代码,命名为arch模块
  41. #[macro_use]
  42. mod arch;
  43. #[macro_use]
  44. mod libs;
  45. #[macro_use]
  46. mod include;
  47. mod debug;
  48. mod driver; // 如果driver依赖了libs,应该在libs后面导出
  49. mod exception;
  50. mod filesystem;
  51. mod init;
  52. mod ipc;
  53. mod misc;
  54. mod mm;
  55. mod net;
  56. mod process;
  57. mod sched;
  58. mod smp;
  59. mod syscall;
  60. mod time;
  61. #[cfg(target_arch = "x86_64")]
  62. mod virt;
  63. #[macro_use]
  64. extern crate alloc;
  65. #[macro_use]
  66. extern crate atomic_enum;
  67. #[macro_use]
  68. extern crate bitflags;
  69. extern crate elf;
  70. #[macro_use]
  71. extern crate lazy_static;
  72. extern crate num;
  73. #[macro_use]
  74. extern crate num_derive;
  75. extern crate smoltcp;
  76. #[macro_use]
  77. extern crate intertrait;
  78. #[cfg(target_arch = "x86_64")]
  79. extern crate x86;
  80. extern crate klog_types;
  81. extern crate uefi;
  82. extern crate uefi_raw;
  83. use crate::mm::allocator::kernel_allocator::KernelAllocator;
  84. use crate::process::ProcessManager;
  85. #[cfg(all(feature = "backtrace", target_arch = "x86_64"))]
  86. extern crate mini_backtrace;
  87. extern "C" {
  88. fn lookup_kallsyms(addr: u64, level: i32) -> i32;
  89. }
  90. // 声明全局的分配器
  91. #[cfg_attr(not(test), global_allocator)]
  92. pub static KERNEL_ALLOCATOR: KernelAllocator = KernelAllocator;
  93. /// 全局的panic处理函数
  94. #[cfg(target_os = "none")]
  95. #[panic_handler]
  96. #[no_mangle]
  97. pub fn panic(info: &PanicInfo) -> ! {
  98. kerror!("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. }