Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 18 Aug 2023 07:41:52 GMT
From:      Corvin =?utf-8?Q?K=C3=B6hne?= <corvink@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org
Subject:   git: 04e774846c9d - stable/13 - bhyve: copy OpRegion into guest memory
Message-ID:  <202308180741.37I7fqi5066931@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch stable/13 has been updated by corvink:

URL: https://cgit.FreeBSD.org/src/commit/?id=04e774846c9d2e37287a1ddd6e7b0cde5d994ed1

commit 04e774846c9d2e37287a1ddd6e7b0cde5d994ed1
Author:     Corvin Köhne <corvink@FreeBSD.org>
AuthorDate: 2023-05-10 11:39:56 +0000
Commit:     Corvin Köhne <corvink@FreeBSD.org>
CommitDate: 2023-08-18 07:32:08 +0000

    bhyve: copy OpRegion into guest memory
    
    This makes the OpRegion accessible by the guest. However, the guest
    doesn't know the address of the OpRegion. This will be fixed by an
    upcoming commit.
    
    The range of the OpRegion is added to the e820 table. This allows the
    guest firmware to easily pick up this range and to reserve it properly.
    
    Reviewed by:            markj
    MFC after:              1 week
    Sponsored by:           Beckhoff Automation GmbH & Co. KG
    Differential Revision:  https://reviews.freebsd.org/D40041
    
    (cherry picked from commit 1115cdcf7af12fd06ca255b981cb579a7bb7a147)
---
 usr.sbin/bhyve/pci_gvt-d.c | 44 +++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 43 insertions(+), 1 deletion(-)

diff --git a/usr.sbin/bhyve/pci_gvt-d.c b/usr.sbin/bhyve/pci_gvt-d.c
index fd3b48c3e5b7..4616bc84d275 100644
--- a/usr.sbin/bhyve/pci_gvt-d.c
+++ b/usr.sbin/bhyve/pci_gvt-d.c
@@ -222,8 +222,40 @@ gvt_d_setup_opregion(struct pci_devinst *const pi)
 	opregion->len = header->size * KB;
 	munmap(header, sizeof(header));
 
+	opregion->hva = mmap(NULL, opregion->len * KB, PROT_READ, MAP_SHARED,
+	    memfd, opregion->hpa);
+	if (opregion->hva == MAP_FAILED) {
+		warn("%s: Unable to map host OpRegion", __func__);
+		close(memfd);
+		return (-1);
+	}
 	close(memfd);
 
+	opregion->gpa = gvt_d_alloc_mmio_memory(opregion->hpa, opregion->len,
+	    E820_ALIGNMENT_NONE, E820_TYPE_NVS);
+	if (opregion->gpa == 0) {
+		warnx(
+		    "%s: Unable to add OpRegion to E820 table (hpa 0x%lx len 0x%lx)",
+		    __func__, opregion->hpa, opregion->len);
+		e820_dump_table();
+		return (-1);
+	}
+	opregion->gva = vm_map_gpa(pi->pi_vmctx, opregion->gpa, opregion->len);
+	if (opregion->gva == NULL) {
+		warnx("%s: Unable to map guest OpRegion", __func__);
+		return (-1);
+	}
+	if (opregion->gpa != opregion->hpa) {
+		/*
+		 * A 1:1 host to guest mapping is not required but this could
+		 * change in the future.
+		 */
+		warnx(
+		    "Warning: Unable to reuse host address of OpRegion. GPU passthrough might not work properly.");
+	}
+
+	memcpy(opregion->gva, opregion->hva, opregion->len);
+
 	return (0);
 }
 
@@ -247,8 +279,18 @@ done:
 }
 
 static void
-gvt_d_deinit(struct pci_devinst *const pi __unused)
+gvt_d_deinit(struct pci_devinst *const pi)
 {
+	struct passthru_softc *sc;
+	struct passthru_mmio_mapping *opregion;
+
+	sc = pi->pi_arg;
+
+	opregion = passthru_get_mmio(sc, GVT_D_MAP_OPREGION);
+
+	/* HVA is only set, if it's initialized */
+	if (opregion->hva)
+		munmap((void *)opregion->hva, opregion->len);
 }
 
 static struct passthru_dev gvt_d_dev = {



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202308180741.37I7fqi5066931>