12b7818e8SLoGin use core::{
22b7818e8SLoGin hint::spin_loop,
32b7818e8SLoGin sync::atomic::{compiler_fence, Ordering},
42b7818e8SLoGin };
55b59005fSLoGin
62eab6dd7S曾俊 use log::debug;
75b59005fSLoGin use system_error::SystemError;
85b59005fSLoGin use x86::dtables::DescriptorTablePointer;
95b59005fSLoGin
105b59005fSLoGin use crate::{
11f2022a8aSLoGin arch::{interrupt::trap::arch_trap_init, process::table::TSSManager},
12dd8e74efSMingtao Huang driver::clocksource::acpi_pm::init_acpi_pm_clocksource,
135b59005fSLoGin init::init::start_kernel,
145b59005fSLoGin mm::{MemoryManagementArch, PhysAddr},
155b59005fSLoGin };
165b59005fSLoGin
172b7818e8SLoGin use self::boot::early_boot_init;
182b7818e8SLoGin
195b59005fSLoGin use super::{
205b59005fSLoGin driver::{
215b59005fSLoGin hpet::{hpet_init, hpet_instance},
225b59005fSLoGin tsc::TSCManager,
235b59005fSLoGin },
245b59005fSLoGin MMArch,
255b59005fSLoGin };
265b59005fSLoGin
272b7818e8SLoGin mod boot;
282b7818e8SLoGin mod multiboot2;
29*59a6bcf6SLoGin mod pvh;
302b7818e8SLoGin
3145626c85SLoGin #[derive(Debug)]
3245626c85SLoGin pub struct ArchBootParams {}
3345626c85SLoGin
3445626c85SLoGin impl ArchBootParams {
3545626c85SLoGin pub const DEFAULT: Self = ArchBootParams {};
3645626c85SLoGin }
375b59005fSLoGin
385b59005fSLoGin extern "C" {
395b59005fSLoGin static mut GDT_Table: [usize; 0usize];
405b59005fSLoGin static mut IDT_Table: [usize; 0usize];
head_stack_start()415b59005fSLoGin fn head_stack_start();
425b59005fSLoGin
435b59005fSLoGin }
445b59005fSLoGin
455b59005fSLoGin #[no_mangle]
46bd70d2d1SLoGin #[allow(static_mut_refs)]
kernel_main( mb2_info: u64, mb2_magic: u64, bsp_gdt_size: u64, bsp_idt_size: u64, boot_entry_type: u64, ) -> !475b59005fSLoGin unsafe extern "C" fn kernel_main(
485b59005fSLoGin mb2_info: u64,
495b59005fSLoGin mb2_magic: u64,
505b59005fSLoGin bsp_gdt_size: u64,
515b59005fSLoGin bsp_idt_size: u64,
522b7818e8SLoGin boot_entry_type: u64,
535b59005fSLoGin ) -> ! {
545b59005fSLoGin let mut gdtp = DescriptorTablePointer::<usize>::default();
555b59005fSLoGin let gdt_vaddr =
565b59005fSLoGin MMArch::phys_2_virt(PhysAddr::new(&GDT_Table as *const usize as usize)).unwrap();
575b59005fSLoGin let idt_vaddr =
585b59005fSLoGin MMArch::phys_2_virt(PhysAddr::new(&IDT_Table as *const usize as usize)).unwrap();
595b59005fSLoGin gdtp.base = gdt_vaddr.data() as *const usize;
605b59005fSLoGin gdtp.limit = bsp_gdt_size as u16 - 1;
615b59005fSLoGin
62b5b571e0SLoGin let idtp = DescriptorTablePointer::<usize> {
63b5b571e0SLoGin base: idt_vaddr.data() as *const usize,
64b5b571e0SLoGin limit: bsp_idt_size as u16 - 1,
65b5b571e0SLoGin };
665b59005fSLoGin
675b59005fSLoGin x86::dtables::lgdt(&gdtp);
685b59005fSLoGin x86::dtables::lidt(&idtp);
695b59005fSLoGin
705b59005fSLoGin compiler_fence(Ordering::SeqCst);
712b7818e8SLoGin if early_boot_init(boot_entry_type, mb2_magic, mb2_info).is_err() {
722b7818e8SLoGin loop {
732b7818e8SLoGin spin_loop();
742b7818e8SLoGin }
752b7818e8SLoGin }
765b59005fSLoGin compiler_fence(Ordering::SeqCst);
775b59005fSLoGin
785b59005fSLoGin start_kernel();
795b59005fSLoGin }
805b59005fSLoGin
815b59005fSLoGin /// 在内存管理初始化之前的架构相关的早期初始化
825b59005fSLoGin #[inline(never)]
83bd70d2d1SLoGin #[allow(static_mut_refs)]
early_setup_arch() -> Result<(), SystemError>845b59005fSLoGin pub fn early_setup_arch() -> Result<(), SystemError> {
855b59005fSLoGin let stack_start = unsafe { *(head_stack_start as *const u64) } as usize;
862eab6dd7S曾俊 debug!("head_stack_start={:#x}\n", stack_start);
875b59005fSLoGin unsafe {
885b59005fSLoGin let gdt_vaddr =
895b59005fSLoGin MMArch::phys_2_virt(PhysAddr::new(&GDT_Table as *const usize as usize)).unwrap();
905b59005fSLoGin let idt_vaddr =
915b59005fSLoGin MMArch::phys_2_virt(PhysAddr::new(&IDT_Table as *const usize as usize)).unwrap();
925b59005fSLoGin
932eab6dd7S曾俊 debug!("GDT_Table={:?}, IDT_Table={:?}\n", gdt_vaddr, idt_vaddr);
945b59005fSLoGin }
955b59005fSLoGin
965b59005fSLoGin set_current_core_tss(stack_start, 0);
975b59005fSLoGin unsafe { TSSManager::load_tr() };
98f2022a8aSLoGin arch_trap_init().expect("arch_trap_init failed");
995b59005fSLoGin
1005b59005fSLoGin return Ok(());
1015b59005fSLoGin }
1025b59005fSLoGin
1035b59005fSLoGin /// 架构相关的初始化
1045b59005fSLoGin #[inline(never)]
setup_arch() -> Result<(), SystemError>1055b59005fSLoGin pub fn setup_arch() -> Result<(), SystemError> {
1065b59005fSLoGin return Ok(());
1075b59005fSLoGin }
1085b59005fSLoGin
1095b59005fSLoGin /// 架构相关的初始化(在IDLE的最后一个阶段)
1105b59005fSLoGin #[inline(never)]
setup_arch_post() -> Result<(), SystemError>1115b59005fSLoGin pub fn setup_arch_post() -> Result<(), SystemError> {
112dd8e74efSMingtao Huang let ret = hpet_init();
113dd8e74efSMingtao Huang if ret.is_ok() {
1145b59005fSLoGin hpet_instance().hpet_enable().expect("hpet enable failed");
115dd8e74efSMingtao Huang } else {
116dd8e74efSMingtao Huang init_acpi_pm_clocksource().expect("acpi_pm_timer inits failed");
117dd8e74efSMingtao Huang }
1185b59005fSLoGin TSCManager::init().expect("tsc init failed");
1195b59005fSLoGin
1205b59005fSLoGin return Ok(());
1215b59005fSLoGin }
1225b59005fSLoGin
set_current_core_tss(stack_start: usize, ist0: usize)1235b59005fSLoGin fn set_current_core_tss(stack_start: usize, ist0: usize) {
1245b59005fSLoGin let current_tss = unsafe { TSSManager::current_tss() };
1252eab6dd7S曾俊 debug!(
1265b59005fSLoGin "set_current_core_tss: stack_start={:#x}, ist0={:#x}\n",
1272eab6dd7S曾俊 stack_start, ist0
1285b59005fSLoGin );
1295b59005fSLoGin current_tss.set_rsp(x86::Ring::Ring0, stack_start as u64);
1305b59005fSLoGin current_tss.set_ist(0, ist0 as u64);
1315b59005fSLoGin }
132