diff --git a/ahcid/src/main.rs b/ahcid/src/main.rs index fcd3c0920d..166214934c 100644 --- a/ahcid/src/main.rs +++ b/ahcid/src/main.rs @@ -82,22 +82,16 @@ fn daemon(daemon: redox_daemon::Daemon) -> ! { let mut name = pci_config.func.name(); name.push_str("_ahci"); - let (bar, bar_size) = pci_config.func.bars[5].expect_mem(); + let bar = &pci_config.func.bars[5]; + let (bar_ptr, bar_size) = bar.expect_mem(); let irq = pci_config.func.legacy_interrupt_line.expect("ahcid: no legacy interrupts supported"); let _logger_ref = setup_logging(&name); - info!(" + AHCI {} on: {} size: {} IRQ: {}", name, bar, bar_size, irq); + info!(" + AHCI {} on: {} size: {} IRQ: {}", name, bar_ptr, bar_size, irq); - let address = unsafe { - common::physmap( - bar, - bar_size, - common::Prot { read: true, write: true }, - common::MemoryType::Uncacheable, - ).expect("ahcid: failed to map address") - }; + let address = unsafe { bar.physmap_mem("ahcid") }; { let scheme_name = format!("disk.{}", name); let socket_fd = syscall::open( diff --git a/e1000d/src/main.rs b/e1000d/src/main.rs index 094ec47b03..d77c3f82f0 100644 --- a/e1000d/src/main.rs +++ b/e1000d/src/main.rs @@ -68,11 +68,12 @@ fn main() { let mut name = pci_config.func.name(); name.push_str("_e1000"); - let (bar, bar_size) = pci_config.func.bars[0].expect_mem(); + let bar = &pci_config.func.bars[0]; + let (bar_ptr, bar_size) = bar.expect_mem(); let irq = pci_config.func.legacy_interrupt_line.expect("e1000d: no legacy interrupts supported"); - eprintln!(" + E1000 {} on: {:X} size: {} IRQ: {}", name, bar, bar_size, irq); + eprintln!(" + E1000 {} on: {:X} size: {} IRQ: {}", name, bar_ptr, bar_size, irq); redox_daemon::Daemon::new(move |daemon| { let socket_fd = syscall::open( @@ -86,10 +87,7 @@ fn main() { let mut irq_file = irq.irq_handle("e1000d"); - let address = unsafe { - common::physmap(bar, bar_size, common::Prot::RW, common::MemoryType::Uncacheable) - .expect("e1000d: failed to map address") - } as usize; + let address = unsafe { bar.physmap_mem("e1000d") } as usize; { let device = Arc::new(RefCell::new(unsafe { device::Intel8254x::new(address).expect("e1000d: failed to allocate device") diff --git a/ihdad/src/main.rs b/ihdad/src/main.rs index f447df8566..f603197608 100755 --- a/ihdad/src/main.rs +++ b/ihdad/src/main.rs @@ -155,14 +155,12 @@ fn daemon(daemon: redox_daemon::Daemon) -> ! { let mut name = pci_config.func.name(); name.push_str("_ihda"); - let (bar_ptr, bar_size) = pci_config.func.bars[0].expect_mem(); + let bar = &pci_config.func.bars[0]; + let (bar_ptr, bar_size) = bar.expect_mem(); log::info!(" + IHDA {} on: {:#X} size: {}", name, bar_ptr, bar_size); - let address = unsafe { - common::physmap(bar_ptr, bar_size, common::Prot::RW, common::MemoryType::Uncacheable) - .expect("ihdad: failed to map address") as usize - }; + let address = unsafe { bar.physmap_mem("ihdad") } as usize; //TODO: MSI-X let mut irq_file = get_int_method(&mut pcid_handle); diff --git a/nvmed/src/main.rs b/nvmed/src/main.rs index 6aeceeb7f8..8e57eb4db0 100644 --- a/nvmed/src/main.rs +++ b/nvmed/src/main.rs @@ -48,7 +48,12 @@ impl Bar { impl Drop for Bar { fn drop(&mut self) { - let _ = unsafe { syscall::funmap(self.physical, self.bar_size.next_multiple_of(PAGE_SIZE)) }; + let _ = unsafe { + syscall::funmap( + self.ptr.as_ptr() as usize, + self.bar_size.next_multiple_of(PAGE_SIZE), + ) + }; } } @@ -288,24 +293,16 @@ fn daemon(daemon: redox_daemon::Daemon) -> ! { let _logger_ref = setup_logging(&scheme_name); - let (bar, bar_size) = pci_config.func.bars[0].expect_mem(); - let irq = pci_config.func.legacy_interrupt_line; + let bar = &pci_config.func.bars[0]; + let (bar_ptr, bar_size) = bar.expect_mem(); log::debug!("NVME PCI CONFIG: {:?}", pci_config); let allocated_bars = AllocatedBars::default(); - let address = unsafe { - common::physmap( - bar, - bar_size, - common::Prot { read: true, write: true }, - common::MemoryType::Uncacheable, - ) - .expect("nvmed: failed to map address") - } as usize; + let address = unsafe { bar.physmap_mem("nvmed") } as usize; *allocated_bars.0[0].lock().unwrap() = Some(Bar { - physical: bar, + physical: bar_ptr, bar_size, ptr: NonNull::new(address as *mut u8).expect("Physmapping BAR gave nullptr"), }); diff --git a/pcid/src/pci/bar.rs b/pcid/src/pci/bar.rs index 4ee887d91f..fa497d755b 100644 --- a/pcid/src/pci/bar.rs +++ b/pcid/src/pci/bar.rs @@ -41,4 +41,18 @@ impl PciBar { PciBar::None => panic!("expected BAR to exist"), } } + + pub unsafe fn physmap_mem(&self, driver: &str) -> *mut () { + let (bar, bar_size) = self.expect_mem(); + unsafe { + common::physmap( + bar, + bar_size, + common::Prot::RW, + // FIXME once the kernel supports this use write-through for prefetchable BAR + common::MemoryType::Uncacheable, + ) + } + .unwrap_or_else(|err| panic!("{driver}: failed to map BAR at {bar:016X}: {err}")) + } } diff --git a/rtl8139d/src/main.rs b/rtl8139d/src/main.rs index 703fc55f1b..771683010a 100644 --- a/rtl8139d/src/main.rs +++ b/rtl8139d/src/main.rs @@ -169,12 +169,10 @@ fn get_int_method(pcid_handle: &mut PcidServerHandle) -> File { let pba_base = capability.pba_base_pointer(pci_config.func.bars); let bir = capability.table_bir() as usize; - let (bar_ptr, bar_size) = pci_config.func.bars[bir].expect_mem(); + let bar = &pci_config.func.bars[bir]; + let (bar_ptr, bar_size) = bar.expect_mem(); - let address = unsafe { - common::physmap(bar_ptr, bar_size, common::Prot::RW, common::MemoryType::Uncacheable) - .expect("rtl8139d: failed to map address") as usize - }; + let address = unsafe { bar.physmap_mem("rtl8139d") } as usize; if !(bar_ptr as u64..bar_ptr as u64 + bar_size as u64).contains(&(table_base as u64 + table_min_length as u64)) { panic!("Table {:#x}{:#x} outside of BAR {:#x}:{:#x}", table_base, table_base + table_min_length as usize, bar_ptr, bar_ptr + bar_size); diff --git a/rtl8168d/src/main.rs b/rtl8168d/src/main.rs index 19140b6f9a..c38b506d21 100644 --- a/rtl8168d/src/main.rs +++ b/rtl8168d/src/main.rs @@ -167,12 +167,10 @@ fn get_int_method(pcid_handle: &mut PcidServerHandle) -> File { let pba_base = capability.pba_base_pointer(pci_config.func.bars); let bir = capability.table_bir() as usize; - let (bar_ptr, bar_size) = pci_config.func.bars[bir].expect_mem(); + let bar = &pci_config.func.bars[bir]; + let (bar_ptr, bar_size) = bar.expect_mem(); - let address = unsafe { - common::physmap(bar_ptr, bar_size, common::Prot::RW, common::MemoryType::Uncacheable) - .expect("rtl8168d: failed to map address") as usize - }; + let address = unsafe { bar.physmap_mem("rtl8168d") } as usize; if !(bar_ptr as u64..bar_ptr as u64 + bar_size as u64).contains(&(table_base as u64 + table_min_length as u64)) { panic!("Table {:#x}{:#x} outside of BAR {:#x}:{:#x}", table_base, table_base + table_min_length as usize, bar_ptr, bar_ptr + bar_size); diff --git a/vboxd/src/main.rs b/vboxd/src/main.rs index 7905f00e7f..17a8c23a2d 100644 --- a/vboxd/src/main.rs +++ b/vboxd/src/main.rs @@ -196,11 +196,12 @@ fn main() { let bar0 = pci_config.func.bars[0].expect_port(); - let (bar1, _) = pci_config.func.bars[1].expect_mem(); + let bar1 = &pci_config.func.bars[1]; + let (bar1_ptr, _) = bar1.expect_mem(); let irq = pci_config.func.legacy_interrupt_line.expect("vboxd: no legacy interrupts supported"); - print!("{}", format!(" + VirtualBox {} on: {:X}, {:X}, IRQ {}\n", name, bar0, bar1, irq)); + print!("{}", format!(" + VirtualBox {} on: {:X}, {:X}, IRQ {}\n", name, bar0, bar1_ptr, irq)); // Daemonize redox_daemon::Daemon::new(move |daemon| { @@ -224,7 +225,7 @@ fn main() { let mut irq_file = irq.irq_handle("vboxd"); let mut port = Pio::::new(bar0 as u16); - let address = unsafe { common::physmap(bar1, 4096, common::Prot::RW, common::MemoryType::Uncacheable).expect("vboxd: failed to map address") }; + let address = unsafe { bar1.physmap_mem("vboxd") }; { let vmmdev = unsafe { &mut *(address as *mut VboxVmmDev) }; diff --git a/virtio-core/src/arch/x86_64.rs b/virtio-core/src/arch/x86_64.rs index 5bccd00c2a..a160f508cd 100644 --- a/virtio-core/src/arch/x86_64.rs +++ b/virtio-core/src/arch/x86_64.rs @@ -27,16 +27,10 @@ pub fn enable_msix(pcid_handle: &mut PcidServerHandle) -> Result { let pba_base = capability.pba_base_pointer(pci_config.func.bars); let bir = capability.table_bir() as usize; - let (bar_ptr, bar_size) = pci_config.func.bars[bir].expect_mem(); + let bar = &pci_config.func.bars[bir]; + let (bar_ptr, bar_size) = bar.expect_mem(); - let address = unsafe { - common::physmap( - bar_ptr, - bar_size, - common::Prot::RW, - common::MemoryType::Uncacheable, - )? as usize - }; + let address = unsafe { bar.physmap_mem("virtio-core") } as usize; // Ensure that the table and PBA are be within the BAR. { diff --git a/xhcid/src/main.rs b/xhcid/src/main.rs index 21ea4f9d4e..9f95131be7 100644 --- a/xhcid/src/main.rs +++ b/xhcid/src/main.rs @@ -232,12 +232,10 @@ fn daemon(daemon: redox_daemon::Daemon) -> ! { let _logger_ref = setup_logging(&name); log::debug!("XHCI PCI CONFIG: {:?}", pci_config); - let (bar_ptr, bar_size) = pci_config.func.bars[0].expect_mem(); + let bar = &pci_config.func.bars[0]; + let (bar_ptr, _) = bar.expect_mem(); - let address = unsafe { - common::physmap(bar_ptr, bar_size, common::Prot::RW, common::MemoryType::Uncacheable) - .expect("xhcid: failed to map address") as usize - }; + let address = unsafe { bar.physmap_mem("xhcid") } as usize; let (mut irq_file, interrupt_method) = get_int_method(&mut pcid_handle, address);