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