lib.rs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #![no_std] // <1>
  2. #![no_main] // <1>
  3. #![feature(alloc_error_handler)]
  4. #![feature(allocator_api)]
  5. #![feature(arbitrary_self_types)]
  6. #![feature(const_mut_refs)]
  7. #![feature(core_intrinsics)]
  8. #![feature(c_void_variant)]
  9. #![feature(drain_filter)]
  10. #![feature(is_some_and)]
  11. #![feature(panic_info_message)]
  12. #![feature(ptr_internals)]
  13. #![feature(trait_upcasting)]
  14. #![feature(slice_ptr_get)]
  15. #![feature(vec_into_raw_parts)]
  16. #[allow(non_upper_case_globals)]
  17. #[allow(non_camel_case_types)]
  18. #[allow(non_snake_case)]
  19. use core::panic::PanicInfo;
  20. /// 导出x86_64架构相关的代码,命名为arch模块
  21. #[macro_use]
  22. mod arch;
  23. #[macro_use]
  24. mod libs;
  25. #[macro_use]
  26. mod include;
  27. mod driver; // 如果driver依赖了libs,应该在libs后面导出
  28. mod exception;
  29. mod filesystem;
  30. mod io;
  31. mod ipc;
  32. mod mm;
  33. mod net;
  34. mod process;
  35. mod sched;
  36. mod smp;
  37. mod syscall;
  38. mod time;
  39. #[macro_use]
  40. extern crate alloc;
  41. #[macro_use]
  42. extern crate bitflags;
  43. extern crate elf;
  44. #[macro_use]
  45. extern crate lazy_static;
  46. extern crate num;
  47. #[macro_use]
  48. extern crate num_derive;
  49. extern crate smoltcp;
  50. extern crate thingbuf;
  51. #[cfg(target_arch = "x86_64")]
  52. extern crate x86;
  53. use crate::mm::allocator::kernel_allocator::KernelAllocator;
  54. // <3>
  55. use crate::{
  56. arch::asm::current::current_pcb,
  57. include::bindings::bindings::{process_do_exit, BLACK, GREEN},
  58. net::net_core::net_init,
  59. };
  60. // 声明全局的slab分配器
  61. #[cfg_attr(not(test), global_allocator)]
  62. pub static KERNEL_ALLOCATOR: KernelAllocator = KernelAllocator;
  63. /// 全局的panic处理函数
  64. #[panic_handler]
  65. #[no_mangle]
  66. pub fn panic(info: &PanicInfo) -> ! {
  67. kerror!("Kernel Panic Occurred.");
  68. match info.location() {
  69. Some(loc) => {
  70. println!(
  71. "Location:\n\tFile: {}\n\tLine: {}, Column: {}",
  72. loc.file(),
  73. loc.line(),
  74. loc.column()
  75. );
  76. }
  77. None => {
  78. println!("No location info");
  79. }
  80. }
  81. match info.message() {
  82. Some(msg) => {
  83. println!("Message:\n\t{}", msg);
  84. }
  85. None => {
  86. println!("No panic message.");
  87. }
  88. }
  89. println!("Current PCB:\n\t{:?}", current_pcb());
  90. unsafe {
  91. process_do_exit(u64::MAX);
  92. };
  93. loop {}
  94. }
  95. /// 该函数用作测试,在process.c的initial_kernel_thread()中调用了此函数
  96. #[no_mangle]
  97. pub extern "C" fn __rust_demo_func() -> i32 {
  98. printk_color!(GREEN, BLACK, "__rust_demo_func()\n");
  99. let r = net_init();
  100. if r.is_err() {
  101. kwarn!("net_init() failed: {:?}", r.err().unwrap());
  102. }
  103. return 0;
  104. }