diff mbox

[v6,kvmtool,13/13] vfio: check reserved regions before mapping DMA

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

Commit Message

Jean-Philippe Brucker June 18, 2018, 6:42 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.

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

Patch

diff --git a/vfio/core.c b/vfio/core.c
index a4a257a70..17b5b0cfc 100644
--- a/vfio/core.c
+++ b/vfio/core.c
@@ -379,6 +379,51 @@  static int vfio_unmap_mem_bank(struct kvm *kvm, struct kvm_mem_bank *bank, void
 	return 0;
 }
 
+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_groups(struct kvm *kvm)
+{
+	int ret;
+	struct vfio_group *group;
+
+	list_for_each_entry(group, &vfio_groups, list) {
+		ret = vfio_configure_reserved_regions(kvm, group);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+
 static struct vfio_group *vfio_group_create(struct kvm *kvm, unsigned long id)
 {
 	int ret;
@@ -597,6 +642,10 @@  static int vfio__init(struct kvm *kvm)
 	if (ret)
 		return ret;
 
+	ret = vfio_configure_groups(kvm);
+	if (ret)
+		return ret;
+
 	ret = vfio_configure_devices(kvm);
 	if (ret)
 		return ret;