From a720cf6f4491a7e5d9c8ba73c307450dffdfb121 Mon Sep 17 00:00:00 2001 From: Anhad Singh Date: Thu, 29 Jun 2023 16:36:02 +1000 Subject: [PATCH] virtio-core::probe: make sure the addr is aligned sys_physmap() requires the address to be page aligned. This fixes the panic inside the `virtio-gpu` driver. Signed-off-by: Anhad Singh --- virtio-core/src/probe.rs | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/virtio-core/src/probe.rs b/virtio-core/src/probe.rs index a68ed54541..f627de9e68 100644 --- a/virtio-core/src/probe.rs +++ b/virtio-core/src/probe.rs @@ -12,7 +12,7 @@ use syscall::{Io, PHYSMAP_NO_CACHE, PHYSMAP_WRITE}; use crate::spec::*; use crate::transport::{Error, StandardTransport}; -use crate::utils::VolatileCell; +use crate::utils::{VolatileCell, align_down}; pub struct Device<'a> { pub transport: Arc>, @@ -178,11 +178,21 @@ pub fn probe_device<'a>(pcid_handle: &mut PcidServerHandle) -> Result }; let address = unsafe { - syscall::physmap( - addr + capability.offset as usize, - capability.length as usize, + let addr = addr + capability.offset as usize; + + // XXX: physmap() requires the address to be page aligned. + let aligned_addr = align_down(addr); + let offset = addr - aligned_addr; + + let size = offset + capability.length as usize; + + let addr = syscall::physmap( + aligned_addr, + size, PHYSMAP_WRITE | PHYSMAP_NO_CACHE, - )? + )?; + + addr + offset }; match capability.cfg_type {