1 use core::sync::atomic::{compiler_fence, Ordering}; 2 3 use system_error::SystemError; 4 use x86::dtables::DescriptorTablePointer; 5 6 use crate::{ 7 arch::{interrupt::trap::arch_trap_init, process::table::TSSManager}, 8 driver::pci::pci::pci_init, 9 init::init::start_kernel, 10 kdebug, 11 mm::{MemoryManagementArch, PhysAddr}, 12 }; 13 14 use super::{ 15 driver::{ 16 hpet::{hpet_init, hpet_instance}, 17 tsc::TSCManager, 18 }, 19 MMArch, 20 }; 21 22 #[derive(Debug)] 23 pub struct ArchBootParams {} 24 25 impl ArchBootParams { 26 pub const DEFAULT: Self = ArchBootParams {}; 27 } 28 29 extern "C" { 30 static mut GDT_Table: [usize; 0usize]; 31 static mut IDT_Table: [usize; 0usize]; 32 fn head_stack_start(); 33 34 fn multiboot2_init(mb2_info: u64, mb2_magic: u32) -> bool; 35 } 36 37 #[no_mangle] 38 unsafe extern "C" fn kernel_main( 39 mb2_info: u64, 40 mb2_magic: u64, 41 bsp_gdt_size: u64, 42 bsp_idt_size: u64, 43 ) -> ! { 44 let mut gdtp = DescriptorTablePointer::<usize>::default(); 45 let gdt_vaddr = 46 MMArch::phys_2_virt(PhysAddr::new(&GDT_Table as *const usize as usize)).unwrap(); 47 let idt_vaddr = 48 MMArch::phys_2_virt(PhysAddr::new(&IDT_Table as *const usize as usize)).unwrap(); 49 gdtp.base = gdt_vaddr.data() as *const usize; 50 gdtp.limit = bsp_gdt_size as u16 - 1; 51 52 let mut idtp = DescriptorTablePointer::<usize>::default(); 53 idtp.base = idt_vaddr.data() as *const usize; 54 idtp.limit = bsp_idt_size as u16 - 1; 55 56 x86::dtables::lgdt(&gdtp); 57 x86::dtables::lidt(&idtp); 58 59 compiler_fence(Ordering::SeqCst); 60 multiboot2_init(mb2_info, (mb2_magic & 0xFFFF_FFFF) as u32); 61 compiler_fence(Ordering::SeqCst); 62 63 start_kernel(); 64 } 65 66 /// 在内存管理初始化之前的架构相关的早期初始化 67 #[inline(never)] 68 pub fn early_setup_arch() -> Result<(), SystemError> { 69 let stack_start = unsafe { *(head_stack_start as *const u64) } as usize; 70 kdebug!("head_stack_start={:#x}\n", stack_start); 71 unsafe { 72 let gdt_vaddr = 73 MMArch::phys_2_virt(PhysAddr::new(&GDT_Table as *const usize as usize)).unwrap(); 74 let idt_vaddr = 75 MMArch::phys_2_virt(PhysAddr::new(&IDT_Table as *const usize as usize)).unwrap(); 76 77 kdebug!("GDT_Table={:?}, IDT_Table={:?}\n", gdt_vaddr, idt_vaddr); 78 } 79 80 set_current_core_tss(stack_start, 0); 81 unsafe { TSSManager::load_tr() }; 82 arch_trap_init().expect("arch_trap_init failed"); 83 84 return Ok(()); 85 } 86 87 /// 架构相关的初始化 88 #[inline(never)] 89 pub fn setup_arch() -> Result<(), SystemError> { 90 // todo: 将来pci接入设备驱动模型之后,删掉这里。 91 pci_init(); 92 return Ok(()); 93 } 94 95 /// 架构相关的初始化(在IDLE的最后一个阶段) 96 #[inline(never)] 97 pub fn setup_arch_post() -> Result<(), SystemError> { 98 hpet_init().expect("hpet init failed"); 99 hpet_instance().hpet_enable().expect("hpet enable failed"); 100 TSCManager::init().expect("tsc init failed"); 101 102 return Ok(()); 103 } 104 105 fn set_current_core_tss(stack_start: usize, ist0: usize) { 106 let current_tss = unsafe { TSSManager::current_tss() }; 107 kdebug!( 108 "set_current_core_tss: stack_start={:#x}, ist0={:#x}\n", 109 stack_start, 110 ist0 111 ); 112 current_tss.set_rsp(x86::Ring::Ring0, stack_start as u64); 113 current_tss.set_ist(0, ist0 as u64); 114 } 115