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