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>, 21338f6903SLoGin 22338f6903SLoGin pub boot_hartid: ProcessorId, 2345626c85SLoGin } 2445626c85SLoGin 2545626c85SLoGin impl ArchBootParams { 2645626c85SLoGin pub const DEFAULT: Self = ArchBootParams { 2745626c85SLoGin fdt_paddr: PhysAddr::new(0), 287a29d4fcSLoGin fdt_vaddr: None, 29338f6903SLoGin boot_hartid: ProcessorId::new(0), 3045626c85SLoGin }; 317a29d4fcSLoGin 327a29d4fcSLoGin pub fn arch_fdt(&self) -> VirtAddr { 337a29d4fcSLoGin // 如果fdt_vaddr为None,则说明还没有进行内核虚拟地址空间的映射,此时返回物理地址 347a29d4fcSLoGin if self.fdt_vaddr.is_none() { 357a29d4fcSLoGin return VirtAddr::new(self.fdt_paddr.data()); 367a29d4fcSLoGin } 377a29d4fcSLoGin self.fdt_vaddr.unwrap() 387a29d4fcSLoGin } 3945626c85SLoGin } 401a72a751SLoGin 41338f6903SLoGin static mut BOOT_HARTID: u32 = 0; 425b59005fSLoGin static mut BOOT_FDT_PADDR: PhysAddr = PhysAddr::new(0); 435b59005fSLoGin 441a72a751SLoGin #[no_mangle] 45666cffedSLoGin unsafe extern "C" fn kernel_main(hartid: usize, fdt_paddr: usize) -> ! { 46666cffedSLoGin let fdt_paddr = PhysAddr::new(fdt_paddr); 477a29d4fcSLoGin 485b59005fSLoGin unsafe { 49338f6903SLoGin BOOT_HARTID = hartid as u32; 505b59005fSLoGin BOOT_FDT_PADDR = fdt_paddr; 51453452ccSLoGin } 525c4224e5SLoGin setup_trap_vector(); 535b59005fSLoGin start_kernel(); 541a72a751SLoGin } 5545626c85SLoGin 565c4224e5SLoGin /// 设置中断、异常处理函数 575c4224e5SLoGin fn setup_trap_vector() { 585c4224e5SLoGin let ptr = handle_exception as *const () as usize; 595c4224e5SLoGin 605c4224e5SLoGin unsafe { 615c4224e5SLoGin riscv::register::stvec::write(ptr, riscv::register::stvec::TrapMode::Direct); 625c4224e5SLoGin // Set sup0 scratch register to 0, indicating to exception vector that 635c4224e5SLoGin // we are presently executing in kernel. 645c4224e5SLoGin riscv::register::sscratch::write(0); 655c4224e5SLoGin } 665c4224e5SLoGin } 675c4224e5SLoGin 687a29d4fcSLoGin #[inline(never)] 6945626c85SLoGin fn print_node(node: FdtNode<'_, '_>, n_spaces: usize) { 7045626c85SLoGin (0..n_spaces).for_each(|_| print!(" ")); 7145626c85SLoGin println!("{}/", node.name); 7245626c85SLoGin node.properties().for_each(|p| { 7345626c85SLoGin (0..n_spaces + 4).for_each(|_| print!(" ")); 74*40169973SLoGin 75*40169973SLoGin if p.name == "compatible" { 76*40169973SLoGin println!("{}: {:?}", p.name, p.as_str()); 77*40169973SLoGin } else { 7845626c85SLoGin println!("{}: {:?}", p.name, p.value); 79*40169973SLoGin } 8045626c85SLoGin }); 8145626c85SLoGin 8245626c85SLoGin for child in node.children() { 8345626c85SLoGin print_node(child, n_spaces + 4); 8445626c85SLoGin } 8545626c85SLoGin } 8645626c85SLoGin 8745626c85SLoGin /// 解析fdt,获取内核启动参数 887a29d4fcSLoGin #[inline(never)] 8945626c85SLoGin unsafe fn parse_dtb() { 9045626c85SLoGin let fdt_paddr = boot_params().read().arch.fdt_paddr; 9145626c85SLoGin if fdt_paddr.is_null() { 9245626c85SLoGin panic!("Failed to get fdt address!"); 9345626c85SLoGin } 9445626c85SLoGin 9545626c85SLoGin open_firmware_fdt_driver() 9645626c85SLoGin .early_scan_device_tree() 9745626c85SLoGin .expect("Failed to scan device tree at boottime."); 9845626c85SLoGin } 995b59005fSLoGin 1005b59005fSLoGin #[inline(never)] 1015b59005fSLoGin pub fn early_setup_arch() -> Result<(), SystemError> { 102cb23beb2SLoGin SbiDriver::early_init(); 103338f6903SLoGin let hartid = unsafe { BOOT_HARTID }; 1045b59005fSLoGin let fdt_paddr = unsafe { BOOT_FDT_PADDR }; 105338f6903SLoGin 106338f6903SLoGin let mut arch_boot_params_guard = boot_params().write(); 107338f6903SLoGin arch_boot_params_guard.arch.fdt_paddr = fdt_paddr; 108338f6903SLoGin arch_boot_params_guard.arch.boot_hartid = ProcessorId::new(hartid); 109338f6903SLoGin 110338f6903SLoGin drop(arch_boot_params_guard); 111cb23beb2SLoGin 1125b59005fSLoGin kinfo!( 1135b59005fSLoGin "DragonOS kernel is running on hart {}, fdt address:{:?}", 1145b59005fSLoGin hartid, 1155b59005fSLoGin fdt_paddr 1165b59005fSLoGin ); 1175b59005fSLoGin mm_early_init(); 1185b59005fSLoGin 1195b59005fSLoGin let fdt = 1205b59005fSLoGin unsafe { fdt::Fdt::from_ptr(fdt_paddr.data() as *const u8).expect("Failed to parse fdt!") }; 1215b59005fSLoGin print_node(fdt.find_node("/").unwrap(), 0); 1225b59005fSLoGin 1235b59005fSLoGin unsafe { parse_dtb() }; 1245b59005fSLoGin 1255b59005fSLoGin for x in mem_block_manager().to_iter() { 1265b59005fSLoGin kdebug!("before efi: {x:?}"); 1275b59005fSLoGin } 1285b59005fSLoGin 1295b59005fSLoGin efi_init(); 1305b59005fSLoGin 1315b59005fSLoGin open_firmware_fdt_driver().early_init_fdt_scan_reserved_mem(); 1325b59005fSLoGin 1335b59005fSLoGin return Ok(()); 1345b59005fSLoGin } 1355b59005fSLoGin 1365b59005fSLoGin #[inline(never)] 1375b59005fSLoGin pub fn setup_arch() -> Result<(), SystemError> { 138338f6903SLoGin init_local_context(); 1395b59005fSLoGin return Ok(()); 1405b59005fSLoGin } 1415b59005fSLoGin 1425b59005fSLoGin #[inline(never)] 1435b59005fSLoGin pub fn setup_arch_post() -> Result<(), SystemError> { 1445b59005fSLoGin // todo 1455b59005fSLoGin return Ok(()); 1465b59005fSLoGin } 147