diff mbox

[v3,kvmtool,11/11] vfio: check reserved regions before mapping DMA

Message ID 20171031191449.1950-12-jean-philippe.brucker@arm.com (mailing list archive)
State New, archived
Headers show

Commit Message

Jean-Philippe Brucker Oct. 31, 2017, 7:14 p.m. UTC
Use the new reserved_regions API to ensure that RAM doesn't overlap any
reserved region. This prevents for instance from mapping an MSI doorbell
into the guest IPA space. For the moment we reject any overlapping. In the
future, we might carve reserved regions out of the guest physical
space.

Signed-off-by: Jean-Philippe Brucker <jean-philippe.brucker@arm.com>
---
 vfio/core.c | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)
diff mbox

Patch

diff --git a/vfio/core.c b/vfio/core.c
index 7e1ba7896df2..10b67f6d6343 100644
--- a/vfio/core.c
+++ b/vfio/core.c
@@ -84,6 +84,37 @@  void vfio_unmap_region(struct kvm *kvm, struct vfio_region *region)
 	munmap(region->host_addr, region->info.size);
 }
 
+static int vfio_configure_reserved_regions(struct kvm *kvm,
+					   struct vfio_group *group)
+{
+	FILE *file;
+	int ret = 0;
+	char type[9];
+	char filename[PATH_MAX];
+	unsigned long long start, end;
+
+	snprintf(filename, PATH_MAX, IOMMU_GROUP_DIR "/%lu/reserved_regions",
+		 group->id);
+
+	/* reserved_regions might not be present on older systems */
+	if (access(filename, F_OK))
+		return 0;
+
+	file = fopen(filename, "r");
+	if (!file)
+		return -errno;
+
+	while (fscanf(file, "0x%llx 0x%llx %8s\n", &start, &end, type) == 3) {
+		ret = kvm__reserve_mem(kvm, start, end - start + 1);
+		if (ret)
+			break;
+	}
+
+	fclose(file);
+
+	return ret;
+}
+
 static int vfio_configure_device(struct kvm *kvm, struct vfio_group *group,
 				 const char *dirpath, const char *name)
 {
@@ -196,6 +227,10 @@  static int vfio_configure_iommu_groups(struct kvm *kvm)
 				return ret;
 		}
 
+		ret = vfio_configure_reserved_regions(kvm, group);
+		if (ret)
+			return ret;
+
 		if (closedir(dir))
 			pr_warning("Failed to close IOMMU group %s", dirpath);
 	}