From b9ab8da576af85cc67a69fdf0218fa8784e9d2b1 Mon Sep 17 00:00:00 2001 From: Mathieu Strypsteen Date: Fri, 22 Nov 2024 11:21:27 +0100 Subject: [PATCH] Fix booting on real hardware --- kernel/src/sys/acpica_osl.rs | 5 +++-- kernel/src/sys/hpet.rs | 2 +- kernel/src/sys/ioapic.rs | 15 ++++++++------- kernel/src/sys/madt.rs | 2 +- 4 files changed, 13 insertions(+), 11 deletions(-) diff --git a/kernel/src/sys/acpica_osl.rs b/kernel/src/sys/acpica_osl.rs index 2690a38..70dfb03 100644 --- a/kernel/src/sys/acpica_osl.rs +++ b/kernel/src/sys/acpica_osl.rs @@ -24,6 +24,7 @@ static SCI_HANDLER: Mutex U static SCI_CONTEXT: AtomicPtr = AtomicPtr::new(null_mut()); use super::{ + hpet::get_current_time, sync::{create_semaphore, lock_semaphore, unlock_semaphore, Semaphore, Spinlock}, task::{CURRENT_TASK, CURRENT_TASK_LOCK, MULTITASKING_ENABLED}, }; @@ -100,8 +101,8 @@ extern "C" fn AcpiOsGetThreadId() -> UINT64 { task_id as UINT64 } #[no_mangle] -extern "C" fn AcpiOsGetTimer() { - unimplemented!(); +extern "C" fn AcpiOsGetTimer() -> UINT64 { + get_current_time() as UINT64 * 10 } #[no_mangle] extern "C" fn AcpiOsInitialize() -> ACPI_STATUS { diff --git a/kernel/src/sys/hpet.rs b/kernel/src/sys/hpet.rs index e3c0979..474273f 100644 --- a/kernel/src/sys/hpet.rs +++ b/kernel/src/sys/hpet.rs @@ -89,7 +89,7 @@ fn handler() { SLEEP_LOCK.unlock(); } } -fn get_current_time() -> usize { +pub fn get_current_time() -> usize { let address = ADDRESS.load(Ordering::SeqCst); let current_counter = unsafe { address.add(REGISTER_COUNTER).read_volatile() }; ticks_to_us(current_counter as usize) diff --git a/kernel/src/sys/ioapic.rs b/kernel/src/sys/ioapic.rs index 10133ec..4fa680f 100644 --- a/kernel/src/sys/ioapic.rs +++ b/kernel/src/sys/ioapic.rs @@ -1,6 +1,6 @@ use core::{ ptr::null_mut, - sync::atomic::{AtomicPtr, AtomicU32, Ordering}, + sync::atomic::{AtomicPtr, AtomicU32, AtomicUsize, Ordering}, }; use bitfield::bitfield; @@ -36,6 +36,7 @@ const REGISTER_VERSION: u8 = 1; const REGISTER_REDIRECTION: u8 = 0x10; static IOAPICS: [IOAPIC; 32] = [EMPTY_IOAPIC; 32]; +static NEXT_IOAPIC_ID: AtomicUsize = AtomicUsize::new(0); fn read_register(apic_i: usize, reg_i: u8) -> u32 { unsafe { @@ -92,13 +93,13 @@ pub fn register_irq_handler(vector: usize, handler: fn()) { } } } -pub fn setup_ioapic(apic_i: u8, phys: u64, gsi_base: u32) { - let apic_i = apic_i as usize; +pub fn setup_ioapic(phys: u64, gsi_base: u32) { let address = unsafe { map_physical(phys, 0x14, false) as *mut u32 }; - IOAPICS[apic_i].address.store(address, Ordering::SeqCst); - IOAPICS[apic_i].start_gsi.store(gsi_base, Ordering::SeqCst); - let max_ints = (read_register(apic_i, REGISTER_VERSION) >> 16) & 0xff; - IOAPICS[apic_i].end_gsi.store(gsi_base + max_ints, Ordering::SeqCst); + let next_id = NEXT_IOAPIC_ID.fetch_add(1, Ordering::SeqCst); + IOAPICS[next_id].address.store(address, Ordering::SeqCst); + IOAPICS[next_id].start_gsi.store(gsi_base, Ordering::SeqCst); + let max_ints = (read_register(next_id, REGISTER_VERSION) >> 16) & 0xff; + IOAPICS[next_id].end_gsi.store(gsi_base + max_ints, Ordering::SeqCst); assert!(gsi_base + max_ints < 128); for i in gsi_base..gsi_base + max_ints { let mut redirection = RedirectionEntry(0); diff --git a/kernel/src/sys/madt.rs b/kernel/src/sys/madt.rs index 55b44d2..68814ff 100644 --- a/kernel/src/sys/madt.rs +++ b/kernel/src/sys/madt.rs @@ -21,7 +21,7 @@ pub fn parse_madt(tables: &AcpiTables) { setup_lapic(lapic_address); for i in madt.entries() { if let MadtEntry::IoApic(ioapic) = i { - setup_ioapic(ioapic.io_apic_id, ioapic.io_apic_address as u64, ioapic.global_system_interrupt_base); + setup_ioapic(ioapic.io_apic_address as u64, ioapic.global_system_interrupt_base); } } for i in madt.entries() {