lib.rs 2.5 KB

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