[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[RFC v7 17/26] vfio/pci: Implement the DMA fault handler
From: |
Eric Auger |
Subject: |
[RFC v7 17/26] vfio/pci: Implement the DMA fault handler |
Date: |
Mon, 16 Nov 2020 19:13:40 +0100 |
Whenever the eventfd is triggered, we retrieve the DMA fault(s)
from the mmapped fault region and inject them in the iommu
memory region.
Signed-off-by: Eric Auger <eric.auger@redhat.com>
---
hw/vfio/pci.h | 1 +
hw/vfio/pci.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 51 insertions(+)
diff --git a/hw/vfio/pci.h b/hw/vfio/pci.h
index d6cc689f5e..350e9e9005 100644
--- a/hw/vfio/pci.h
+++ b/hw/vfio/pci.h
@@ -146,6 +146,7 @@ struct VFIOPCIDevice {
EventNotifier req_notifier;
VFIOPCIExtIRQ *ext_irqs;
VFIORegion dma_fault_region;
+ uint32_t fault_tail_index;
int (*resetfn)(struct VFIOPCIDevice *);
uint32_t vendor_id;
uint32_t device_id;
diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c
index 35551bfd1d..4e3495bb60 100644
--- a/hw/vfio/pci.c
+++ b/hw/vfio/pci.c
@@ -2891,10 +2891,60 @@ static PCIPASIDOps vfio_pci_pasid_ops = {
static void vfio_dma_fault_notifier_handler(void *opaque)
{
VFIOPCIExtIRQ *ext_irq = opaque;
+ VFIOPCIDevice *vdev = ext_irq->vdev;
+ PCIDevice *pdev = &vdev->pdev;
+ AddressSpace *as = pci_device_iommu_address_space(pdev);
+ IOMMUMemoryRegion *iommu_mr = IOMMU_MEMORY_REGION(as->root);
+ struct vfio_region_dma_fault header;
+ struct iommu_fault *queue;
+ char *queue_buffer = NULL;
+ ssize_t bytes;
if (!event_notifier_test_and_clear(&ext_irq->notifier)) {
return;
}
+
+ bytes = pread(vdev->vbasedev.fd, &header, sizeof(header),
+ vdev->dma_fault_region.fd_offset);
+ if (bytes != sizeof(header)) {
+ error_report("%s unable to read the fault region header (0x%lx)",
+ __func__, bytes);
+ return;
+ }
+
+ /* Normally the fault queue is mmapped */
+ queue = (struct iommu_fault *)vdev->dma_fault_region.mmaps[0].mmap;
+ if (!queue) {
+ size_t queue_size = header.nb_entries * header.entry_size;
+
+ error_report("%s: fault queue not mmapped: slower fault handling",
+ vdev->vbasedev.name);
+
+ queue_buffer = g_malloc(queue_size);
+ bytes = pread(vdev->vbasedev.fd, queue_buffer, queue_size,
+ vdev->dma_fault_region.fd_offset + header.offset);
+ if (bytes != queue_size) {
+ error_report("%s unable to read the fault queue (0x%lx)",
+ __func__, bytes);
+ return;
+ }
+
+ queue = (struct iommu_fault *)queue_buffer;
+ }
+
+ while (vdev->fault_tail_index != header.head) {
+ memory_region_inject_faults(iommu_mr, 1,
+ &queue[vdev->fault_tail_index]);
+ vdev->fault_tail_index =
+ (vdev->fault_tail_index + 1) % header.nb_entries;
+ }
+ bytes = pwrite(vdev->vbasedev.fd, &vdev->fault_tail_index, 4,
+ vdev->dma_fault_region.fd_offset);
+ if (bytes != 4) {
+ error_report("%s unable to write the fault region tail index (0x%lx)",
+ __func__, bytes);
+ }
+ g_free(queue_buffer);
}
static int vfio_register_ext_irq_handler(VFIOPCIDevice *vdev,
--
2.21.3
- [RFC v7 07/26] iommu: Introduce generic header, (continued)
- [RFC v7 07/26] iommu: Introduce generic header, Eric Auger, 2020/11/16
- [RFC v7 08/26] pci: introduce PCIPASIDOps to PCIDevice, Eric Auger, 2020/11/16
- [RFC v7 09/26] vfio: Force nested if iommu requires it, Eric Auger, 2020/11/16
- [RFC v7 10/26] vfio: Introduce hostwin_from_range helper, Eric Auger, 2020/11/16
- [RFC v7 11/26] vfio: Introduce helpers to DMA map/unmap a RAM section, Eric Auger, 2020/11/16
- [RFC v7 12/26] vfio: Set up nested stage mappings, Eric Auger, 2020/11/16
- [RFC v7 13/26] vfio: Pass stage 1 MSI bindings to the host, Eric Auger, 2020/11/16
- [RFC v7 14/26] vfio: Helper to get IRQ info including capabilities, Eric Auger, 2020/11/16
- [RFC v7 15/26] vfio/pci: Register handler for iommu fault, Eric Auger, 2020/11/16
- [RFC v7 16/26] vfio/pci: Set up the DMA FAULT region, Eric Auger, 2020/11/16
- [RFC v7 17/26] vfio/pci: Implement the DMA fault handler,
Eric Auger <=
- [RFC v7 18/26] hw/arm/smmuv3: Advertise MSI_TRANSLATE attribute, Eric Auger, 2020/11/16
- [RFC v7 19/26] hw/arm/smmuv3: Store the PASID table GPA in the translation config, Eric Auger, 2020/11/16
- [RFC v7 20/26] hw/arm/smmuv3: Fill the IOTLBEntry arch_id on NH_VA invalidation, Eric Auger, 2020/11/16
- [RFC v7 21/26] hw/arm/smmuv3: Fill the IOTLBEntry leaf field on NH_VA invalidation, Eric Auger, 2020/11/16
- [RFC v7 23/26] hw/arm/smmuv3: Implement fault injection, Eric Auger, 2020/11/16
- [RFC v7 22/26] hw/arm/smmuv3: Pass stage 1 configurations to the host, Eric Auger, 2020/11/16
- [RFC v7 24/26] hw/arm/smmuv3: Allow MAP notifiers, Eric Auger, 2020/11/16
- [RFC v7 25/26] pci: Add return_page_response pci ops, Eric Auger, 2020/11/16
- [RFC v7 26/26] vfio/pci: Implement return_page_response page response callback, Eric Auger, 2020/11/16