mod.rs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. use fdt::node::FdtNode;
  2. use log::{debug, info};
  3. use system_error::SystemError;
  4. use crate::{
  5. arch::{
  6. driver::sbi::SbiDriver,
  7. init::boot::{early_boot_init, BootProtocol},
  8. mm::init::mm_early_init,
  9. },
  10. driver::{firmware::efi::init::efi_init, open_firmware::fdt::open_firmware_fdt_driver},
  11. init::{boot_params, init::start_kernel},
  12. mm::{memblock::mem_block_manager, PhysAddr, VirtAddr},
  13. print, println,
  14. smp::cpu::ProcessorId,
  15. };
  16. use super::{cpu::init_local_context, interrupt::entry::handle_exception};
  17. mod boot;
  18. mod dragonstub;
  19. #[derive(Debug)]
  20. pub struct ArchBootParams {
  21. /// 启动时的fdt物理地址
  22. pub fdt_paddr: PhysAddr,
  23. pub fdt_vaddr: Option<VirtAddr>,
  24. pub fdt_size: usize,
  25. pub boot_hartid: ProcessorId,
  26. }
  27. impl ArchBootParams {
  28. pub const DEFAULT: Self = ArchBootParams {
  29. fdt_paddr: PhysAddr::new(0),
  30. fdt_vaddr: None,
  31. fdt_size: 0,
  32. boot_hartid: ProcessorId::new(0),
  33. };
  34. pub fn arch_fdt(&self) -> VirtAddr {
  35. // 如果fdt_vaddr为None,则说明还没有进行内核虚拟地址空间的映射,此时返回物理地址
  36. if self.fdt_vaddr.is_none() {
  37. return VirtAddr::new(self.fdt_paddr.data());
  38. }
  39. self.fdt_vaddr.unwrap()
  40. }
  41. pub fn set_alt_mem_k(&mut self, _alt_mem_k: u32) {}
  42. pub fn set_scratch(&mut self, _scratch: u32) {}
  43. pub fn add_e820_entry(&mut self, _addr: u64, _size: u64, _mtype: u32) {}
  44. pub fn init_setupheader(&mut self) {}
  45. pub fn convert_to_buf(&self) -> &[u8] {
  46. unsafe {
  47. core::slice::from_raw_parts(
  48. (self as *const Self) as *const u8,
  49. core::mem::size_of::<Self>(),
  50. )
  51. }
  52. }
  53. }
  54. static mut BOOT_HARTID: u32 = 0;
  55. static mut BOOT_FDT_PADDR: PhysAddr = PhysAddr::new(0);
  56. #[no_mangle]
  57. unsafe extern "C" fn kernel_main(hartid: usize, fdt_paddr: usize) -> ! {
  58. let fdt_paddr = PhysAddr::new(fdt_paddr);
  59. unsafe {
  60. BOOT_HARTID = hartid as u32;
  61. BOOT_FDT_PADDR = fdt_paddr;
  62. }
  63. setup_trap_vector();
  64. start_kernel();
  65. }
  66. /// 设置中断、异常处理函数
  67. fn setup_trap_vector() {
  68. let ptr = handle_exception as *const () as usize;
  69. unsafe {
  70. riscv::register::stvec::write(ptr, riscv::register::stvec::TrapMode::Direct);
  71. // Set sup0 scratch register to 0, indicating to exception vector that
  72. // we are presently executing in kernel.
  73. riscv::register::sscratch::write(0);
  74. }
  75. }
  76. #[inline(never)]
  77. fn print_node(node: FdtNode<'_, '_>, n_spaces: usize) {
  78. (0..n_spaces).for_each(|_| print!(" "));
  79. println!("{}/", node.name);
  80. node.properties().for_each(|p| {
  81. (0..n_spaces + 4).for_each(|_| print!(" "));
  82. if p.name == "compatible" {
  83. println!("{}: {:?}", p.name, p.as_str());
  84. } else {
  85. println!("{}: {:?}", p.name, p.value);
  86. }
  87. });
  88. for child in node.children() {
  89. print_node(child, n_spaces + 4);
  90. }
  91. }
  92. /// 解析fdt,获取内核启动参数
  93. #[inline(never)]
  94. unsafe fn parse_dtb() {
  95. let fdt_paddr = boot_params().read().arch.fdt_paddr;
  96. if fdt_paddr.is_null() {
  97. panic!("Failed to get fdt address!");
  98. }
  99. open_firmware_fdt_driver()
  100. .early_scan_device_tree()
  101. .expect("Failed to scan device tree at boottime.");
  102. }
  103. #[inline(never)]
  104. pub fn early_setup_arch() -> Result<(), SystemError> {
  105. SbiDriver::early_init();
  106. let hartid = unsafe { BOOT_HARTID };
  107. let fdt_paddr = unsafe { BOOT_FDT_PADDR };
  108. let fdt =
  109. unsafe { fdt::Fdt::from_ptr(fdt_paddr.data() as *const u8).expect("Failed to parse fdt!") };
  110. let mut arch_boot_params_guard = boot_params().write();
  111. arch_boot_params_guard.arch.fdt_paddr = fdt_paddr;
  112. arch_boot_params_guard.arch.fdt_size = fdt.total_size();
  113. arch_boot_params_guard.arch.boot_hartid = ProcessorId::new(hartid);
  114. // debug!("fdt_paddr: {:?}, fdt_size: {}", fdt_paddr, fdt.total_size());
  115. drop(arch_boot_params_guard);
  116. info!(
  117. "DragonOS kernel is running on hart {}, fdt address:{:?}",
  118. hartid, fdt_paddr
  119. );
  120. early_boot_init(BootProtocol::DragonStub).expect("Failed to init boot protocol!");
  121. mm_early_init();
  122. print_node(fdt.find_node("/").unwrap(), 0);
  123. unsafe { parse_dtb() };
  124. for x in mem_block_manager().to_iter() {
  125. debug!("before efi: {x:?}");
  126. }
  127. efi_init();
  128. open_firmware_fdt_driver().early_init_fdt_scan_reserved_mem();
  129. return Ok(());
  130. }
  131. #[inline(never)]
  132. pub fn setup_arch() -> Result<(), SystemError> {
  133. init_local_context();
  134. return Ok(());
  135. }
  136. #[inline(never)]
  137. pub fn setup_arch_post() -> Result<(), SystemError> {
  138. // todo
  139. return Ok(());
  140. }