From 7aa7aa463fedaeec2b784bbae525b8d4d7bc78b0 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Sat, 4 Apr 2026 12:55:20 +0200 Subject: [PATCH] Use map_device_memory where possible --- src/arch/x86_shared/device/ioapic.rs | 26 ++++++------------------ src/arch/x86_shared/device/local_apic.rs | 17 ++++++---------- src/arch/x86_shared/device/mod.rs | 3 +-- src/arch/x86_shared/device/serial.rs | 22 +++++++------------- 4 files changed, 20 insertions(+), 48 deletions(-) diff --git a/src/arch/x86_shared/device/ioapic.rs b/src/arch/x86_shared/device/ioapic.rs index 8d8e4f2b6a..2f8c9bf260 100644 --- a/src/arch/x86_shared/device/ioapic.rs +++ b/src/arch/x86_shared/device/ioapic.rs @@ -6,13 +6,10 @@ use spin::Mutex; #[cfg(feature = "acpi")] use crate::{ acpi::madt::{self, Madt, MadtEntry, MadtIntSrcOverride, MadtIoApic}, - memory::{Frame, PageFlags, PhysicalAddress, RmmA, RmmArch}, + memory::{map_device_memory, PhysicalAddress}, }; -use crate::{ - arch::{cpuid::cpuid, interrupt::irq}, - memory::KernelMapper, -}; +use crate::arch::{cpuid::cpuid, interrupt::irq}; use super::{local_apic::ApicId, pic}; @@ -236,21 +233,10 @@ pub fn src_overrides() -> &'static [Override] { } #[cfg(feature = "acpi")] -pub unsafe fn handle_ioapic(mapper: &mut KernelMapper, madt_ioapic: &'static MadtIoApic) { +pub unsafe fn handle_ioapic(madt_ioapic: &'static MadtIoApic) { unsafe { // map the I/O APIC registers - - let frame = Frame::containing(PhysicalAddress::new(madt_ioapic.address as usize)); - - assert!(mapper.translate(RmmA::phys_to_virt(frame.base())).is_none()); - - let (virt, flush) = mapper - .map_linearly( - frame.base(), - PageFlags::new().write(true).device_memory(true), - ) - .expect("failed to map I/O APIC"); - flush.flush(); + let virt = map_device_memory(PhysicalAddress::new(madt_ioapic.address as usize), 4096); let ioapic_registers = virt.data() as *const u32; let ioapic = IoApic::new(ioapic_registers, madt_ioapic.gsi_base); @@ -302,7 +288,7 @@ pub unsafe fn handle_src_override(src_override: &'static MadtIntSrcOverride) { } #[allow(dead_code)] -pub unsafe fn init(active_table: &mut KernelMapper) { +pub unsafe fn init() { unsafe { let bsp_apic_id = ApicId::new(u32::from( cpuid().get_feature_info().unwrap().initial_local_apic_id(), @@ -324,7 +310,7 @@ pub unsafe fn init(active_table: &mut KernelMapper) { for entry in madt.iter() { match entry { - MadtEntry::IoApic(ioapic) => handle_ioapic(active_table, ioapic), + MadtEntry::IoApic(ioapic) => handle_ioapic(ioapic), MadtEntry::IntSrcOverride(src_override) => handle_src_override(src_override), _ => (), } diff --git a/src/arch/x86_shared/device/local_apic.rs b/src/arch/x86_shared/device/local_apic.rs index 7f4b0af891..e17c4eeb82 100644 --- a/src/arch/x86_shared/device/local_apic.rs +++ b/src/arch/x86_shared/device/local_apic.rs @@ -5,13 +5,12 @@ use core::{ use x86::msr::*; use crate::{ + arch::cpuid::cpuid, ipi::IpiKind, - memory::{PageFlags, PhysicalAddress}, + memory::{map_device_memory, PhysicalAddress}, percpu::PercpuBlock, }; -use crate::{arch::cpuid::cpuid, memory::KernelMapper}; - #[derive(Clone, Copy, Debug)] pub struct ApicId(u32); @@ -33,9 +32,9 @@ pub unsafe fn the_local_apic() -> &'static mut LocalApic { unsafe { &mut *LOCAL_APIC.get() } } -pub unsafe fn init(active_table: &mut KernelMapper) { +pub unsafe fn init() { unsafe { - the_local_apic().init(active_table); + the_local_apic().init(); } } @@ -52,7 +51,7 @@ pub struct LocalApic { } impl LocalApic { - unsafe fn init(&mut self, mapper: &mut KernelMapper) { + unsafe fn init(&mut self) { unsafe { let physaddr = PhysicalAddress::new(rdmsr(IA32_APIC_BASE) as usize & 0xFFFF_0000); @@ -62,11 +61,7 @@ impl LocalApic { if !self.x2 { debug!("Detected xAPIC at {:#x}", physaddr.data()); - let (virt, flush) = mapper - .map_linearly(physaddr, PageFlags::new().write(true)) - .expect("failed to map local APIC memory"); - flush.flush(); - self.address = virt.data(); + self.address = map_device_memory(physaddr, 4096).data(); } else { debug!("Detected x2APIC"); } diff --git a/src/arch/x86_shared/device/mod.rs b/src/arch/x86_shared/device/mod.rs index 0219374e0c..94112e6f50 100644 --- a/src/arch/x86_shared/device/mod.rs +++ b/src/arch/x86_shared/device/mod.rs @@ -1,4 +1,3 @@ -use crate::memory::KernelMapper; use core::cell::Cell; pub mod cpu; @@ -18,7 +17,7 @@ pub mod tsc; pub unsafe fn init() { unsafe { pic::init(); - local_apic::init(&mut KernelMapper::lock_rw()); + local_apic::init(); // Run here for the side effect of printing if KVM was used to avoid interleaved logs. tsc::get_kvm_support(); diff --git a/src/arch/x86_shared/device/serial.rs b/src/arch/x86_shared/device/serial.rs index 9c78591aa3..67aa04c23a 100644 --- a/src/arch/x86_shared/device/serial.rs +++ b/src/arch/x86_shared/device/serial.rs @@ -1,5 +1,6 @@ use crate::{ devices::{serial::SerialKind, uart_16550::SerialPort}, + memory::map_device_memory, syscall::io::{Mmio, Pio}, }; use spin::Mutex; @@ -32,21 +33,12 @@ pub unsafe fn init() { return; } - let virt = { - use crate::memory::{KernelMapper, PageFlags, PhysicalAddress}; - - // TODO: Make this configurable - let phys = PhysicalAddress::new(0xFE032000); - - let mut mapper = KernelMapper::lock_rw(); - let flags = PageFlags::new().write(true).execute(false); - let (virt, flush) = unsafe { - mapper - .map_linearly(phys, flags) - .expect("failed to map frame") - }; - flush.flush(); - virt + let virt = unsafe { + map_device_memory( + // TODO: Make this configurable + crate::memory::PhysicalAddress::new(0xFE032000), + 4, + ) }; let lpss = unsafe { SerialPort::>::new(virt.data()) };