xref: /DragonOS/kernel/src/arch/x86_64/init/mod.rs (revision bd70d2d1f490aabd570a5301b858bd5eb04149fa)
1 use core::sync::atomic::{compiler_fence, Ordering};
2 
3 use log::debug;
4 use system_error::SystemError;
5 use x86::dtables::DescriptorTablePointer;
6 
7 use crate::{
8     arch::{interrupt::trap::arch_trap_init, process::table::TSSManager},
9     driver::clocksource::acpi_pm::init_acpi_pm_clocksource,
10     init::init::start_kernel,
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 #[allow(static_mut_refs)]
39 unsafe extern "C" fn kernel_main(
40     mb2_info: u64,
41     mb2_magic: u64,
42     bsp_gdt_size: u64,
43     bsp_idt_size: u64,
44 ) -> ! {
45     let mut gdtp = DescriptorTablePointer::<usize>::default();
46     let gdt_vaddr =
47         MMArch::phys_2_virt(PhysAddr::new(&GDT_Table as *const usize as usize)).unwrap();
48     let idt_vaddr =
49         MMArch::phys_2_virt(PhysAddr::new(&IDT_Table as *const usize as usize)).unwrap();
50     gdtp.base = gdt_vaddr.data() as *const usize;
51     gdtp.limit = bsp_gdt_size as u16 - 1;
52 
53     let idtp = DescriptorTablePointer::<usize> {
54         base: idt_vaddr.data() as *const usize,
55         limit: bsp_idt_size as u16 - 1,
56     };
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 #[allow(static_mut_refs)]
71 pub fn early_setup_arch() -> Result<(), SystemError> {
72     let stack_start = unsafe { *(head_stack_start as *const u64) } as usize;
73     debug!("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         debug!("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     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     return Ok(());
94 }
95 
96 /// 架构相关的初始化(在IDLE的最后一个阶段)
97 #[inline(never)]
98 pub fn setup_arch_post() -> Result<(), SystemError> {
99     let ret = hpet_init();
100     if ret.is_ok() {
101         hpet_instance().hpet_enable().expect("hpet enable failed");
102     } else {
103         init_acpi_pm_clocksource().expect("acpi_pm_timer inits failed");
104     }
105     TSCManager::init().expect("tsc init failed");
106 
107     return Ok(());
108 }
109 
110 fn set_current_core_tss(stack_start: usize, ist0: usize) {
111     let current_tss = unsafe { TSSManager::current_tss() };
112     debug!(
113         "set_current_core_tss: stack_start={:#x}, ist0={:#x}\n",
114         stack_start, ist0
115     );
116     current_tss.set_rsp(x86::Ring::Ring0, stack_start as u64);
117     current_tss.set_ist(0, ist0 as u64);
118 }
119