diff --git a/framework/jinux-frame/src/arch/x86/mmio.rs b/framework/jinux-frame/src/arch/x86/mmio.rs deleted file mode 100644 index fa6105521..000000000 --- a/framework/jinux-frame/src/arch/x86/mmio.rs +++ /dev/null @@ -1,19 +0,0 @@ -use acpi::PciConfigRegions; - -pub fn start_address() -> usize { - let start = PciConfigRegions::new( - &*crate::arch::x86::kernel::acpi::ACPI_TABLES - .get() - .unwrap() - .lock(), - ) - .unwrap(); - - // all zero to get the start address - start.physical_address(0, 0, 0, 0).unwrap() as usize -} - -pub fn end_address() -> usize { - // 4G-20M - 0xFEC0_0000 -} diff --git a/framework/jinux-frame/src/arch/x86/mod.rs b/framework/jinux-frame/src/arch/x86/mod.rs index 8c38425d6..4ec8d00ad 100644 --- a/framework/jinux-frame/src/arch/x86/mod.rs +++ b/framework/jinux-frame/src/arch/x86/mod.rs @@ -5,7 +5,6 @@ pub mod iommu; pub(crate) mod irq; mod kernel; pub(crate) mod mm; -pub(crate) mod mmio; pub(crate) mod timer; use alloc::fmt; diff --git a/framework/jinux-frame/src/bus/pci/cfg_space.rs b/framework/jinux-frame/src/bus/pci/cfg_space.rs index 10aa9b48a..20b7912b1 100644 --- a/framework/jinux-frame/src/bus/pci/cfg_space.rs +++ b/framework/jinux-frame/src/bus/pci/cfg_space.rs @@ -154,12 +154,13 @@ impl MemoryBar { // length let size = !(len_encoded & !0xF).wrapping_add(1); let prefetchable = if raw & 0b1000 == 0 { false } else { true }; + // The BAR is located in I/O memory region Ok(MemoryBar { base, size, prefetchable, address_length, - io_memory: IoMem::new((base as usize)..((base + size as u64) as usize)).unwrap(), + io_memory: unsafe { IoMem::new((base as usize)..((base + size as u64) as usize)) }, }) } } diff --git a/framework/jinux-frame/src/io_mem.rs b/framework/jinux-frame/src/io_mem.rs index 8565ce59f..b04995cec 100644 --- a/framework/jinux-frame/src/io_mem.rs +++ b/framework/jinux-frame/src/io_mem.rs @@ -1,26 +1,11 @@ use core::{mem::size_of, ops::Range}; use pod::Pod; -use spin::Once; use crate::{ - vm::{HasPaddr, Paddr, Vaddr, VmIo}, - Error, + vm::{paddr_to_vaddr, HasPaddr, Paddr, Vaddr, VmIo}, + Error, Result, }; -static CHECKER: Once = Once::new(); - -pub(crate) fn init() { - CHECKER.call_once(|| MmioChecker { - start: crate::arch::mmio::start_address(), - end: crate::arch::mmio::end_address(), - }); - log::info!( - "MMIO start: 0x{:x}, end: 0x{:x}", - CHECKER.get().unwrap().start, - CHECKER.get().unwrap().end - ); -} - #[derive(Debug, Clone)] pub struct IoMem { virtual_address: Vaddr, @@ -67,18 +52,39 @@ impl HasPaddr for IoMem { } impl IoMem { - pub fn new(range: Range) -> Option { - if CHECKER.get().unwrap().check(&range) { - Some(IoMem { - virtual_address: crate::vm::paddr_to_vaddr(range.start), - limit: range.len(), - }) - } else { - None + /// # Safety + /// + /// User must ensure the range is in the I/O memory region. + pub(crate) unsafe fn new(range: Range) -> IoMem { + IoMem { + virtual_address: paddr_to_vaddr(range.start), + limit: range.len(), } } - fn check_range(&self, offset: usize, len: usize) -> crate::Result<()> { + pub fn paddr(&self) -> Paddr { + crate::vm::vaddr_to_paddr(self.virtual_address).unwrap() + } + + pub fn length(&self) -> usize { + self.limit + } + + pub fn resize(&mut self, range: Range) -> Result<()> { + let start_vaddr = paddr_to_vaddr(range.start); + if start_vaddr < self.virtual_address || start_vaddr >= self.virtual_address + self.limit { + return Err(Error::InvalidArgs); + } + let end_vaddr = start_vaddr + range.len(); + if end_vaddr <= self.virtual_address || end_vaddr > self.virtual_address + self.limit { + return Err(Error::InvalidArgs); + } + self.virtual_address = start_vaddr; + self.limit = range.len(); + Ok(()) + } + + fn check_range(&self, offset: usize, len: usize) -> Result<()> { let sum = offset.checked_add(len).ok_or(Error::InvalidArgs)?; if sum > self.limit { log::error!( @@ -92,15 +98,3 @@ impl IoMem { } } } - -struct MmioChecker { - start: Paddr, - end: Paddr, -} - -impl MmioChecker { - /// Check whether the physical address is in MMIO region. - fn check(&self, range: &Range) -> bool { - range.start >= self.start && range.end < self.end - } -} diff --git a/framework/jinux-frame/src/lib.rs b/framework/jinux-frame/src/lib.rs index 81cc23b55..6c88ed054 100644 --- a/framework/jinux-frame/src/lib.rs +++ b/framework/jinux-frame/src/lib.rs @@ -52,7 +52,6 @@ pub fn init() { vm::init(); trap::init(); arch::after_all_init(); - io_mem::init(); bus::init(); register_irq_common_callback(); invoke_c_init_funcs();