145626c85SLoGin use fdt::node::FdtNode; 25b59005fSLoGin use system_error::SystemError; 345626c85SLoGin 445626c85SLoGin use crate::{ 5cb23beb2SLoGin arch::{driver::sbi::SbiDriver, mm::init::mm_early_init}, 65c4224e5SLoGin driver::{firmware::efi::init::efi_init, open_firmware::fdt::open_firmware_fdt_driver}, 75b59005fSLoGin init::{boot_params, init::start_kernel}, 87a29d4fcSLoGin kdebug, kinfo, 95b59005fSLoGin mm::{memblock::mem_block_manager, PhysAddr, VirtAddr}, 1045626c85SLoGin print, println, 11338f6903SLoGin smp::cpu::ProcessorId, 1245626c85SLoGin }; 1345626c85SLoGin 145c4224e5SLoGin use super::{cpu::init_local_context, interrupt::entry::handle_exception}; 15cb23beb2SLoGin 1645626c85SLoGin #[derive(Debug)] 1745626c85SLoGin pub struct ArchBootParams { 1845626c85SLoGin /// 启动时的fdt物理地址 1945626c85SLoGin pub fdt_paddr: PhysAddr, 207a29d4fcSLoGin pub fdt_vaddr: Option<VirtAddr>, 21*23ef2b33SLoGin pub fdt_size: usize, 22338f6903SLoGin 23338f6903SLoGin pub boot_hartid: ProcessorId, 2445626c85SLoGin } 2545626c85SLoGin 2645626c85SLoGin impl ArchBootParams { 2745626c85SLoGin pub const DEFAULT: Self = ArchBootParams { 2845626c85SLoGin fdt_paddr: PhysAddr::new(0), 297a29d4fcSLoGin fdt_vaddr: None, 30*23ef2b33SLoGin fdt_size: 0, 31338f6903SLoGin boot_hartid: ProcessorId::new(0), 3245626c85SLoGin }; 337a29d4fcSLoGin 347a29d4fcSLoGin pub fn arch_fdt(&self) -> VirtAddr { 357a29d4fcSLoGin // 如果fdt_vaddr为None,则说明还没有进行内核虚拟地址空间的映射,此时返回物理地址 367a29d4fcSLoGin if self.fdt_vaddr.is_none() { 377a29d4fcSLoGin return VirtAddr::new(self.fdt_paddr.data()); 387a29d4fcSLoGin } 397a29d4fcSLoGin self.fdt_vaddr.unwrap() 407a29d4fcSLoGin } 4145626c85SLoGin } 421a72a751SLoGin 43338f6903SLoGin static mut BOOT_HARTID: u32 = 0; 445b59005fSLoGin static mut BOOT_FDT_PADDR: PhysAddr = PhysAddr::new(0); 455b59005fSLoGin 461a72a751SLoGin #[no_mangle] 47666cffedSLoGin unsafe extern "C" fn kernel_main(hartid: usize, fdt_paddr: usize) -> ! { 48666cffedSLoGin let fdt_paddr = PhysAddr::new(fdt_paddr); 497a29d4fcSLoGin 505b59005fSLoGin unsafe { 51338f6903SLoGin BOOT_HARTID = hartid as u32; 525b59005fSLoGin BOOT_FDT_PADDR = fdt_paddr; 53453452ccSLoGin } 545c4224e5SLoGin setup_trap_vector(); 555b59005fSLoGin start_kernel(); 561a72a751SLoGin } 5745626c85SLoGin 585c4224e5SLoGin /// 设置中断、异常处理函数 595c4224e5SLoGin fn setup_trap_vector() { 605c4224e5SLoGin let ptr = handle_exception as *const () as usize; 615c4224e5SLoGin 625c4224e5SLoGin unsafe { 635c4224e5SLoGin riscv::register::stvec::write(ptr, riscv::register::stvec::TrapMode::Direct); 645c4224e5SLoGin // Set sup0 scratch register to 0, indicating to exception vector that 655c4224e5SLoGin // we are presently executing in kernel. 665c4224e5SLoGin riscv::register::sscratch::write(0); 675c4224e5SLoGin } 685c4224e5SLoGin } 695c4224e5SLoGin 707a29d4fcSLoGin #[inline(never)] 7145626c85SLoGin fn print_node(node: FdtNode<'_, '_>, n_spaces: usize) { 7245626c85SLoGin (0..n_spaces).for_each(|_| print!(" ")); 7345626c85SLoGin println!("{}/", node.name); 7445626c85SLoGin node.properties().for_each(|p| { 7545626c85SLoGin (0..n_spaces + 4).for_each(|_| print!(" ")); 7640169973SLoGin 7740169973SLoGin if p.name == "compatible" { 7840169973SLoGin println!("{}: {:?}", p.name, p.as_str()); 7940169973SLoGin } else { 8045626c85SLoGin println!("{}: {:?}", p.name, p.value); 8140169973SLoGin } 8245626c85SLoGin }); 8345626c85SLoGin 8445626c85SLoGin for child in node.children() { 8545626c85SLoGin print_node(child, n_spaces + 4); 8645626c85SLoGin } 8745626c85SLoGin } 8845626c85SLoGin 8945626c85SLoGin /// 解析fdt,获取内核启动参数 907a29d4fcSLoGin #[inline(never)] 9145626c85SLoGin unsafe fn parse_dtb() { 9245626c85SLoGin let fdt_paddr = boot_params().read().arch.fdt_paddr; 9345626c85SLoGin if fdt_paddr.is_null() { 9445626c85SLoGin panic!("Failed to get fdt address!"); 9545626c85SLoGin } 9645626c85SLoGin 9745626c85SLoGin open_firmware_fdt_driver() 9845626c85SLoGin .early_scan_device_tree() 9945626c85SLoGin .expect("Failed to scan device tree at boottime."); 10045626c85SLoGin } 1015b59005fSLoGin 1025b59005fSLoGin #[inline(never)] 1035b59005fSLoGin pub fn early_setup_arch() -> Result<(), SystemError> { 104cb23beb2SLoGin SbiDriver::early_init(); 105338f6903SLoGin let hartid = unsafe { BOOT_HARTID }; 1065b59005fSLoGin let fdt_paddr = unsafe { BOOT_FDT_PADDR }; 107338f6903SLoGin 108*23ef2b33SLoGin let fdt = 109*23ef2b33SLoGin unsafe { fdt::Fdt::from_ptr(fdt_paddr.data() as *const u8).expect("Failed to parse fdt!") }; 110*23ef2b33SLoGin 111338f6903SLoGin let mut arch_boot_params_guard = boot_params().write(); 112338f6903SLoGin arch_boot_params_guard.arch.fdt_paddr = fdt_paddr; 113*23ef2b33SLoGin arch_boot_params_guard.arch.fdt_size = fdt.total_size(); 114338f6903SLoGin arch_boot_params_guard.arch.boot_hartid = ProcessorId::new(hartid); 115338f6903SLoGin 116338f6903SLoGin drop(arch_boot_params_guard); 117cb23beb2SLoGin 1185b59005fSLoGin kinfo!( 1195b59005fSLoGin "DragonOS kernel is running on hart {}, fdt address:{:?}", 1205b59005fSLoGin hartid, 1215b59005fSLoGin fdt_paddr 1225b59005fSLoGin ); 1235b59005fSLoGin mm_early_init(); 1245b59005fSLoGin 1255b59005fSLoGin print_node(fdt.find_node("/").unwrap(), 0); 1265b59005fSLoGin 1275b59005fSLoGin unsafe { parse_dtb() }; 1285b59005fSLoGin 1295b59005fSLoGin for x in mem_block_manager().to_iter() { 1305b59005fSLoGin kdebug!("before efi: {x:?}"); 1315b59005fSLoGin } 1325b59005fSLoGin 1335b59005fSLoGin efi_init(); 1345b59005fSLoGin 1355b59005fSLoGin open_firmware_fdt_driver().early_init_fdt_scan_reserved_mem(); 1365b59005fSLoGin 1375b59005fSLoGin return Ok(()); 1385b59005fSLoGin } 1395b59005fSLoGin 1405b59005fSLoGin #[inline(never)] 1415b59005fSLoGin pub fn setup_arch() -> Result<(), SystemError> { 142338f6903SLoGin init_local_context(); 1435b59005fSLoGin return Ok(()); 1445b59005fSLoGin } 1455b59005fSLoGin 1465b59005fSLoGin #[inline(never)] 1475b59005fSLoGin pub fn setup_arch_post() -> Result<(), SystemError> { 1485b59005fSLoGin // todo 1495b59005fSLoGin return Ok(()); 1505b59005fSLoGin } 151