git: 6a284cacb1e5 - main - bhyve: Remove vmctx argument from PCI device model methods.

From: John Baldwin <jhb_at_FreeBSD.org>
Date: Thu, 19 Jan 2023 18:30:40 UTC
The branch main has been updated by jhb:

URL: https://cgit.FreeBSD.org/src/commit/?id=6a284cacb1e56a81dff7f5c34bbbd7ed1a0b6cb1

commit 6a284cacb1e56a81dff7f5c34bbbd7ed1a0b6cb1
Author:     John Baldwin <jhb@FreeBSD.org>
AuthorDate: 2023-01-19 18:30:18 +0000
Commit:     John Baldwin <jhb@FreeBSD.org>
CommitDate: 2023-01-19 18:30:18 +0000

    bhyve: Remove vmctx argument from PCI device model methods.
    
    Most of these arguments were unused.  Device models which do need
    access to the vmctx in one of these methods can obtain it from the
    pi_vmctx member of the pci_devinst argument instead.
    
    Reviewed by:    corvink, markj
    Differential Revision:  https://reviews.freebsd.org/D38096
---
 usr.sbin/bhyve/bhyverun.c           |  4 +-
 usr.sbin/bhyve/pci_ahci.c           | 12 +++---
 usr.sbin/bhyve/pci_e82545.c         | 10 ++---
 usr.sbin/bhyve/pci_emul.c           | 66 ++++++++++++++---------------
 usr.sbin/bhyve/pci_emul.h           | 25 +++++------
 usr.sbin/bhyve/pci_fbuf.c           | 21 +++++-----
 usr.sbin/bhyve/pci_hda.c            | 19 ++++-----
 usr.sbin/bhyve/pci_hostbridge.c     |  3 +-
 usr.sbin/bhyve/pci_lpc.c            | 17 ++++----
 usr.sbin/bhyve/pci_nvme.c           | 27 ++++++------
 usr.sbin/bhyve/pci_passthru.c       | 84 ++++++++++++++++++-------------------
 usr.sbin/bhyve/pci_uart.c           |  8 ++--
 usr.sbin/bhyve/pci_virtio_9p.c      |  2 +-
 usr.sbin/bhyve/pci_virtio_block.c   |  3 +-
 usr.sbin/bhyve/pci_virtio_console.c |  3 +-
 usr.sbin/bhyve/pci_virtio_input.c   |  3 +-
 usr.sbin/bhyve/pci_virtio_net.c     |  3 +-
 usr.sbin/bhyve/pci_virtio_rnd.c     |  3 +-
 usr.sbin/bhyve/pci_virtio_scsi.c    |  5 +--
 usr.sbin/bhyve/pci_xhci.c           | 10 ++---
 usr.sbin/bhyve/snapshot.c           | 12 +++---
 usr.sbin/bhyve/snapshot.h           |  8 ++--
 usr.sbin/bhyve/virtio.c             | 10 ++---
 usr.sbin/bhyve/virtio.h             | 13 +++---
 24 files changed, 167 insertions(+), 204 deletions(-)

diff --git a/usr.sbin/bhyve/bhyverun.c b/usr.sbin/bhyve/bhyverun.c
index aeafed1d0c69..caa284a8dd63 100644
--- a/usr.sbin/bhyve/bhyverun.c
+++ b/usr.sbin/bhyve/bhyverun.c
@@ -1491,7 +1491,7 @@ main(int argc, char *argv[])
 #ifdef BHYVE_SNAPSHOT
 	if (restore_file != NULL) {
 		fprintf(stdout, "Pausing pci devs...\r\n");
-		if (vm_pause_user_devs(ctx) != 0) {
+		if (vm_pause_user_devs() != 0) {
 			fprintf(stderr, "Failed to pause PCI device state.\n");
 			exit(1);
 		}
@@ -1515,7 +1515,7 @@ main(int argc, char *argv[])
 		}
 
 		fprintf(stdout, "Resuming pci devs...\r\n");
-		if (vm_resume_user_devs(ctx) != 0) {
+		if (vm_resume_user_devs() != 0) {
 			fprintf(stderr, "Failed to resume PCI device state.\n");
 			exit(1);
 		}
diff --git a/usr.sbin/bhyve/pci_ahci.c b/usr.sbin/bhyve/pci_ahci.c
index 700170885a13..9c023d93cab9 100644
--- a/usr.sbin/bhyve/pci_ahci.c
+++ b/usr.sbin/bhyve/pci_ahci.c
@@ -2197,8 +2197,7 @@ pci_ahci_host_write(struct pci_ahci_softc *sc, uint64_t offset, uint64_t value)
 }
 
 static void
-pci_ahci_write(struct vmctx *ctx __unused,
-    struct pci_devinst *pi, int baridx, uint64_t offset, int size,
+pci_ahci_write(struct pci_devinst *pi, int baridx, uint64_t offset, int size,
     uint64_t value)
 {
 	struct pci_ahci_softc *sc = pi->pi_arg;
@@ -2292,8 +2291,7 @@ pci_ahci_port_read(struct pci_ahci_softc *sc, uint64_t offset)
 }
 
 static uint64_t
-pci_ahci_read(struct vmctx *ctx __unused,
-    struct pci_devinst *pi, int baridx, uint64_t regoff, int size)
+pci_ahci_read(struct pci_devinst *pi, int baridx, uint64_t regoff, int size)
 {
 	struct pci_ahci_softc *sc = pi->pi_arg;
 	uint64_t offset;
@@ -2420,7 +2418,7 @@ pci_ahci_hd_legacy_config(nvlist_t *nvl, const char *opts)
 }
 
 static int
-pci_ahci_init(struct vmctx *ctx __unused, struct pci_devinst *pi, nvlist_t *nvl)
+pci_ahci_init(struct pci_devinst *pi, nvlist_t *nvl)
 {
 	char bident[sizeof("XXX:XXX:XXX")];
 	char node_name[sizeof("XX")];
@@ -2668,7 +2666,7 @@ done:
 }
 
 static int
-pci_ahci_pause(struct vmctx *ctx __unused, struct pci_devinst *pi)
+pci_ahci_pause(struct pci_devinst *pi)
 {
 	struct pci_ahci_softc *sc;
 	struct blockif_ctxt *bctxt;
@@ -2688,7 +2686,7 @@ pci_ahci_pause(struct vmctx *ctx __unused, struct pci_devinst *pi)
 }
 
 static int
-pci_ahci_resume(struct vmctx *ctx __unused, struct pci_devinst *pi)
+pci_ahci_resume(struct pci_devinst *pi)
 {
 	struct pci_ahci_softc *sc;
 	struct blockif_ctxt *bctxt;
diff --git a/usr.sbin/bhyve/pci_e82545.c b/usr.sbin/bhyve/pci_e82545.c
index 49c5f986a341..bec8edb17caf 100644
--- a/usr.sbin/bhyve/pci_e82545.c
+++ b/usr.sbin/bhyve/pci_e82545.c
@@ -2112,8 +2112,7 @@ e82545_read_register(struct e82545_softc *sc, uint32_t offset)
 }
 
 static void
-e82545_write(struct vmctx *ctx __unused,
-    struct pci_devinst *pi, int baridx, uint64_t offset, int size,
+e82545_write(struct pci_devinst *pi, int baridx, uint64_t offset, int size,
     uint64_t value)
 {
 	struct e82545_softc *sc;
@@ -2163,8 +2162,7 @@ e82545_write(struct vmctx *ctx __unused,
 }
 
 static uint64_t
-e82545_read(struct vmctx *ctx __unused,
-    struct pci_devinst *pi, int baridx, uint64_t offset, int size)
+e82545_read(struct pci_devinst *pi, int baridx, uint64_t offset, int size)
 {
 	struct e82545_softc *sc;
 	uint64_t retval;
@@ -2307,7 +2305,7 @@ e82545_reset(struct e82545_softc *sc, int drvr)
 }
 
 static int
-e82545_init(struct vmctx *ctx, struct pci_devinst *pi, nvlist_t *nvl)
+e82545_init(struct pci_devinst *pi, nvlist_t *nvl)
 {
 	char nstr[80];
 	struct e82545_softc *sc;
@@ -2319,7 +2317,7 @@ e82545_init(struct vmctx *ctx, struct pci_devinst *pi, nvlist_t *nvl)
 
 	pi->pi_arg = sc;
 	sc->esc_pi = pi;
-	sc->esc_ctx = ctx;
+	sc->esc_ctx = pi->pi_vmctx;
 
 	pthread_mutex_init(&sc->esc_mtx, NULL);
 	pthread_cond_init(&sc->esc_rx_cond, NULL);
diff --git a/usr.sbin/bhyve/pci_emul.c b/usr.sbin/bhyve/pci_emul.c
index cca0932180d6..93411751d635 100644
--- a/usr.sbin/bhyve/pci_emul.c
+++ b/usr.sbin/bhyve/pci_emul.c
@@ -140,8 +140,8 @@ SYSRES_MEM(PCI_EMUL_ECFG_BASE, PCI_EMUL_ECFG_SIZE);
 static struct pci_devemu *pci_emul_finddev(const char *name);
 static void pci_lintr_route(struct pci_devinst *pi);
 static void pci_lintr_update(struct pci_devinst *pi);
-static void pci_cfgrw(struct vmctx *ctx, int in, int bus, int slot,
-    int func, int coff, int bytes, uint32_t *val);
+static void pci_cfgrw(int in, int bus, int slot, int func, int coff,
+    int bytes, uint32_t *val);
 
 static __inline void
 CFGWRITE(struct pci_devinst *pi, int coff, uint32_t val, int bytes)
@@ -447,7 +447,7 @@ pci_msix_pba_bar(struct pci_devinst *pi)
 }
 
 static int
-pci_emul_io_handler(struct vmctx *ctx, int in, int port,
+pci_emul_io_handler(struct vmctx *ctx __unused, int in, int port,
     int bytes, uint32_t *eax, void *arg)
 {
 	struct pci_devinst *pdi = arg;
@@ -464,10 +464,10 @@ pci_emul_io_handler(struct vmctx *ctx, int in, int port,
 		    pdi->pi_bar[i].addr + pdi->pi_bar[i].size) {
 			offset = port - pdi->pi_bar[i].addr;
 			if (in)
-				*eax = (*pe->pe_barread)(ctx, pdi, i,
+				*eax = (*pe->pe_barread)(pdi, i,
 							 offset, bytes);
 			else
-				(*pe->pe_barwrite)(ctx, pdi, i, offset,
+				(*pe->pe_barwrite)(pdi, i, offset,
 						   bytes, *eax);
 			return (0);
 		}
@@ -476,7 +476,7 @@ pci_emul_io_handler(struct vmctx *ctx, int in, int port,
 }
 
 static int
-pci_emul_mem_handler(struct vmctx *ctx, int vcpu __unused, int dir,
+pci_emul_mem_handler(struct vmctx *ctx __unused, int vcpu __unused, int dir,
     uint64_t addr, int size, uint64_t *val, void *arg1, long arg2)
 {
 	struct pci_devinst *pdi = arg1;
@@ -494,22 +494,22 @@ pci_emul_mem_handler(struct vmctx *ctx, int vcpu __unused, int dir,
 
 	if (dir == MEM_F_WRITE) {
 		if (size == 8) {
-			(*pe->pe_barwrite)(ctx, pdi, bidx, offset,
+			(*pe->pe_barwrite)(pdi, bidx, offset,
 					   4, *val & 0xffffffff);
-			(*pe->pe_barwrite)(ctx, pdi, bidx, offset + 4,
+			(*pe->pe_barwrite)(pdi, bidx, offset + 4,
 					   4, *val >> 32);
 		} else {
-			(*pe->pe_barwrite)(ctx, pdi, bidx, offset,
+			(*pe->pe_barwrite)(pdi, bidx, offset,
 					   size, *val);
 		}
 	} else {
 		if (size == 8) {
-			*val = (*pe->pe_barread)(ctx, pdi, bidx,
+			*val = (*pe->pe_barread)(pdi, bidx,
 						 offset, 4);
-			*val |= (*pe->pe_barread)(ctx, pdi, bidx,
+			*val |= (*pe->pe_barread)(pdi, bidx,
 						  offset + 4, 4) << 32;
 		} else {
-			*val = (*pe->pe_barread)(ctx, pdi, bidx,
+			*val = (*pe->pe_barread)(pdi, bidx,
 						 offset, size);
 		}
 	}
@@ -563,7 +563,7 @@ modify_bar_registration(struct pci_devinst *pi, int idx, int registration)
 		} else
 			error = unregister_inout(&iop);
 		if (pe->pe_baraddr != NULL)
-			(*pe->pe_baraddr)(pi->pi_vmctx, pi, idx, registration,
+			(*pe->pe_baraddr)(pi, idx, registration,
 					  pi->pi_bar[idx].addr);
 		break;
 	case PCIBAR_MEM32:
@@ -581,13 +581,13 @@ modify_bar_registration(struct pci_devinst *pi, int idx, int registration)
 		} else
 			error = unregister_mem(&mr);
 		if (pe->pe_baraddr != NULL)
-			(*pe->pe_baraddr)(pi->pi_vmctx, pi, idx, registration,
+			(*pe->pe_baraddr)(pi, idx, registration,
 					  pi->pi_bar[idx].addr);
 		break;
 	case PCIBAR_ROM:
 		error = 0;
 		if (pe->pe_baraddr != NULL)
-			(*pe->pe_baraddr)(pi->pi_vmctx, pi, idx, registration,
+			(*pe->pe_baraddr)(pi, idx, registration,
 			    pi->pi_bar[idx].addr);
 		break;
 	default:
@@ -993,7 +993,7 @@ pci_emul_init(struct vmctx *ctx, struct pci_devemu *pde, int bus, int slot,
 
 	pci_set_cfgdata8(pdi, PCIR_COMMAND, PCIM_CMD_BUSMASTEREN);
 
-	err = (*pde->pe_init)(ctx, pdi, fi->fi_config);
+	err = (*pde->pe_init)(pdi, fi->fi_config);
 	if (err == 0)
 		fi->fi_devi = pdi;
 	else
@@ -1294,7 +1294,7 @@ pci_emul_fallback_handler(struct vmctx *ctx __unused, int vcpu __unused,
 }
 
 static int
-pci_emul_ecfg_handler(struct vmctx *ctx, int vcpu __unused, int dir,
+pci_emul_ecfg_handler(struct vmctx *ctx __unused, int vcpu __unused, int dir,
     uint64_t addr, int bytes, uint64_t *val, void *arg1 __unused,
     long arg2 __unused)
 {
@@ -1307,7 +1307,7 @@ pci_emul_ecfg_handler(struct vmctx *ctx, int vcpu __unused, int dir,
 	in = (dir == MEM_F_READ);
 	if (in)
 		*val = ~0UL;
-	pci_cfgrw(ctx, in, bus, slot, func, coff, bytes, (uint32_t *)val);
+	pci_cfgrw(in, bus, slot, func, coff, bytes, (uint32_t *)val);
 	return (0);
 }
 
@@ -2054,8 +2054,8 @@ pci_emul_cmdsts_write(struct pci_devinst *pi, int coff, uint32_t new, int bytes)
 }
 
 static void
-pci_cfgrw(struct vmctx *ctx, int in, int bus, int slot, int func,
-    int coff, int bytes, uint32_t *eax)
+pci_cfgrw(int in, int bus, int slot, int func, int coff, int bytes,
+    uint32_t *eax)
 {
 	struct businfo *bi;
 	struct slotinfo *si;
@@ -2108,7 +2108,7 @@ pci_cfgrw(struct vmctx *ctx, int in, int bus, int slot, int func,
 	if (in) {
 		/* Let the device emulation override the default handler */
 		if (pe->pe_cfgread != NULL) {
-			needcfg = pe->pe_cfgread(ctx, pi, coff, bytes, eax);
+			needcfg = pe->pe_cfgread(pi, coff, bytes, eax);
 		} else {
 			needcfg = 1;
 		}
@@ -2120,7 +2120,7 @@ pci_cfgrw(struct vmctx *ctx, int in, int bus, int slot, int func,
 	} else {
 		/* Let the device emulation override the default handler */
 		if (pe->pe_cfgwrite != NULL &&
-		    (*pe->pe_cfgwrite)(ctx, pi, coff, bytes, *eax) == 0)
+		    (*pe->pe_cfgwrite)(pi, coff, bytes, *eax) == 0)
 			return;
 
 		/*
@@ -2247,7 +2247,7 @@ pci_emul_cfgaddr(struct vmctx *ctx __unused, int in,
 INOUT_PORT(pci_cfgaddr, CONF1_ADDR_PORT, IOPORT_F_INOUT, pci_emul_cfgaddr);
 
 static int
-pci_emul_cfgdata(struct vmctx *ctx, int in, int port,
+pci_emul_cfgdata(struct vmctx *ctx __unused, int in, int port,
     int bytes, uint32_t *eax, void *arg __unused)
 {
 	int coff;
@@ -2256,8 +2256,7 @@ pci_emul_cfgdata(struct vmctx *ctx, int in, int port,
 
 	coff = cfgoff + (port - CONF1_DATA_PORT);
 	if (cfgenable) {
-		pci_cfgrw(ctx, in, cfgbus, cfgslot, cfgfunc, coff, bytes,
-		    eax);
+		pci_cfgrw(in, cfgbus, cfgslot, cfgfunc, coff, bytes, eax);
 	} else {
 		/* Ignore accesses to cfgdata if not enabled by cfgaddr */
 		if (in)
@@ -2395,7 +2394,7 @@ pci_snapshot(struct vm_snapshot_meta *meta)
 }
 
 int
-pci_pause(struct vmctx *ctx, const char *dev_name)
+pci_pause(const char *dev_name)
 {
 	struct pci_devemu *pde;
 	struct pci_devinst *pdi;
@@ -2420,11 +2419,11 @@ pci_pause(struct vmctx *ctx, const char *dev_name)
 		return (0);
 	}
 
-	return (*pde->pe_pause)(ctx, pdi);
+	return (*pde->pe_pause)(pdi);
 }
 
 int
-pci_resume(struct vmctx *ctx, const char *dev_name)
+pci_resume(const char *dev_name)
 {
 	struct pci_devemu *pde;
 	struct pci_devinst *pdi;
@@ -2449,7 +2448,7 @@ pci_resume(struct vmctx *ctx, const char *dev_name)
 		return (0);
 	}
 
-	return (*pde->pe_resume)(ctx, pdi);
+	return (*pde->pe_resume)(pdi);
 }
 #endif
 
@@ -2469,8 +2468,7 @@ struct pci_emul_dsoftc {
 #define	PCI_EMUL_MSIX_MSGS	16
 
 static int
-pci_emul_dinit(struct vmctx *ctx __unused, struct pci_devinst *pi,
-    nvlist_t *nvl __unused)
+pci_emul_dinit(struct pci_devinst *pi, nvlist_t *nvl __unused)
 {
 	int error;
 	struct pci_emul_dsoftc *sc;
@@ -2499,8 +2497,7 @@ pci_emul_dinit(struct vmctx *ctx __unused, struct pci_devinst *pi,
 }
 
 static void
-pci_emul_diow(struct vmctx *ctx __unused,
-    struct pci_devinst *pi, int baridx, uint64_t offset, int size,
+pci_emul_diow(struct pci_devinst *pi, int baridx, uint64_t offset, int size,
     uint64_t value)
 {
 	int i;
@@ -2567,8 +2564,7 @@ pci_emul_diow(struct vmctx *ctx __unused,
 }
 
 static uint64_t
-pci_emul_dior(struct vmctx *ctx __unused,
-    struct pci_devinst *pi, int baridx, uint64_t offset, int size)
+pci_emul_dior(struct pci_devinst *pi, int baridx, uint64_t offset, int size)
 {
 	struct pci_emul_dsoftc *sc = pi->pi_arg;
 	uint32_t value;
diff --git a/usr.sbin/bhyve/pci_emul.h b/usr.sbin/bhyve/pci_emul.h
index 8a7bcb7cc83a..117fc78898a2 100644
--- a/usr.sbin/bhyve/pci_emul.h
+++ b/usr.sbin/bhyve/pci_emul.h
@@ -54,8 +54,7 @@ struct pci_devemu {
 	const char      *pe_emu;	/* Name of device emulation */
 
 	/* instance creation */
-	int       (*pe_init)(struct vmctx *, struct pci_devinst *,
-			     nvlist_t *);
+	int       (*pe_init)(struct pci_devinst *, nvlist_t *);
 	int	(*pe_legacy_config)(nvlist_t *, const char *);
 	const char *pe_alias;
 
@@ -63,28 +62,24 @@ struct pci_devemu {
 	void	(*pe_write_dsdt)(struct pci_devinst *);
 
 	/* config space read/write callbacks */
-	int	(*pe_cfgwrite)(struct vmctx *ctx,
-			       struct pci_devinst *pi, int offset,
+	int	(*pe_cfgwrite)(struct pci_devinst *pi, int offset,
 			       int bytes, uint32_t val);
-	int	(*pe_cfgread)(struct vmctx *ctx,
-			      struct pci_devinst *pi, int offset,
+	int	(*pe_cfgread)(struct pci_devinst *pi, int offset,
 			      int bytes, uint32_t *retval);
 
 	/* BAR read/write callbacks */
-	void      (*pe_barwrite)(struct vmctx *ctx,
-				 struct pci_devinst *pi, int baridx,
+	void      (*pe_barwrite)(struct pci_devinst *pi, int baridx,
 				 uint64_t offset, int size, uint64_t value);
-	uint64_t  (*pe_barread)(struct vmctx *ctx,
-				struct pci_devinst *pi, int baridx,
+	uint64_t  (*pe_barread)(struct pci_devinst *pi, int baridx,
 				uint64_t offset, int size);
 
-	void	(*pe_baraddr)(struct vmctx *ctx, struct pci_devinst *pi,
+	void	(*pe_baraddr)(struct pci_devinst *pi,
 			      int baridx, int enabled, uint64_t address);
 
 	/* Save/restore device state */
 	int	(*pe_snapshot)(struct vm_snapshot_meta *meta);
-	int	(*pe_pause)(struct vmctx *ctx, struct pci_devinst *pi);
-	int	(*pe_resume)(struct vmctx *ctx, struct pci_devinst *pi);
+	int	(*pe_pause)(struct pci_devinst *pi);
+	int	(*pe_resume)(struct pci_devinst *pi);
 
 };
 #define PCI_EMUL_SET(x)   DATA_SET(pci_devemu_set, x);
@@ -266,8 +261,8 @@ uint64_t pci_ecfg_base(void);
 int	pci_bus_configured(int bus);
 #ifdef BHYVE_SNAPSHOT
 int	pci_snapshot(struct vm_snapshot_meta *meta);
-int	pci_pause(struct vmctx *ctx, const char *dev_name);
-int	pci_resume(struct vmctx *ctx, const char *dev_name);
+int	pci_pause(const char *dev_name);
+int	pci_resume(const char *dev_name);
 #endif
 
 static __inline void
diff --git a/usr.sbin/bhyve/pci_fbuf.c b/usr.sbin/bhyve/pci_fbuf.c
index 4cfe872bd934..91215a124c59 100644
--- a/usr.sbin/bhyve/pci_fbuf.c
+++ b/usr.sbin/bhyve/pci_fbuf.c
@@ -117,8 +117,7 @@ static struct pci_fbuf_softc *fbuf_sc;
 #define	PCI_FBUF_MSI_MSGS	 4
 
 static void
-pci_fbuf_write(struct vmctx *ctx __unused,
-    struct pci_devinst *pi, int baridx, uint64_t offset, int size,
+pci_fbuf_write(struct pci_devinst *pi, int baridx, uint64_t offset, int size,
     uint64_t value)
 {
 	struct pci_fbuf_softc *sc;
@@ -172,8 +171,7 @@ pci_fbuf_write(struct vmctx *ctx __unused,
 }
 
 static uint64_t
-pci_fbuf_read(struct vmctx *ctx __unused,
-    struct pci_devinst *pi, int baridx, uint64_t offset, int size)
+pci_fbuf_read(struct pci_devinst *pi, int baridx, uint64_t offset, int size)
 {
 	struct pci_fbuf_softc *sc;
 	uint8_t *p;
@@ -218,8 +216,8 @@ pci_fbuf_read(struct vmctx *ctx __unused,
 }
 
 static void
-pci_fbuf_baraddr(struct vmctx *ctx, struct pci_devinst *pi, int baridx,
-		 int enabled, uint64_t address)
+pci_fbuf_baraddr(struct pci_devinst *pi, int baridx, int enabled,
+    uint64_t address)
 {
 	struct pci_fbuf_softc *sc;
 	int prot;
@@ -229,12 +227,13 @@ pci_fbuf_baraddr(struct vmctx *ctx, struct pci_devinst *pi, int baridx,
 
 	sc = pi->pi_arg;
 	if (!enabled) {
-		if (vm_munmap_memseg(ctx, sc->fbaddr, FB_SIZE) != 0)
+		if (vm_munmap_memseg(pi->pi_vmctx, sc->fbaddr, FB_SIZE) != 0)
 			EPRINTLN("pci_fbuf: munmap_memseg failed");
 		sc->fbaddr = 0;
 	} else {
 		prot = PROT_READ | PROT_WRITE;
-		if (vm_mmap_memseg(ctx, address, VM_FRAMEBUFFER, 0, FB_SIZE, prot) != 0)
+		if (vm_mmap_memseg(pi->pi_vmctx, address, VM_FRAMEBUFFER, 0,
+		    FB_SIZE, prot) != 0)
 			EPRINTLN("pci_fbuf: mmap_memseg failed");
 		sc->fbaddr = address;
 	}
@@ -371,7 +370,7 @@ pci_fbuf_render(struct bhyvegc *gc, void *arg)
 }
 
 static int
-pci_fbuf_init(struct vmctx *ctx, struct pci_devinst *pi, nvlist_t *nvl)
+pci_fbuf_init(struct pci_devinst *pi, nvlist_t *nvl)
 {
 	int error;
 	struct pci_fbuf_softc *sc;
@@ -391,8 +390,8 @@ pci_fbuf_init(struct vmctx *ctx, struct pci_devinst *pi, nvlist_t *nvl)
 	pci_set_cfgdata8(pi, PCIR_CLASS, PCIC_DISPLAY);
 	pci_set_cfgdata8(pi, PCIR_SUBCLASS, PCIS_DISPLAY_VGA);
 
-	sc->fb_base = vm_create_devmem(
-	    ctx, VM_FRAMEBUFFER, "framebuffer", FB_SIZE);
+	sc->fb_base = vm_create_devmem(pi->pi_vmctx, VM_FRAMEBUFFER,
+	    "framebuffer", FB_SIZE);
 	if (sc->fb_base == MAP_FAILED) {
 		error = -1;
 		goto done;
diff --git a/usr.sbin/bhyve/pci_hda.c b/usr.sbin/bhyve/pci_hda.c
index a85935a5cae8..9b0d4bd02ba0 100644
--- a/usr.sbin/bhyve/pci_hda.c
+++ b/usr.sbin/bhyve/pci_hda.c
@@ -209,11 +209,11 @@ static uint64_t hda_get_clock_ns(void);
 /*
  * PCI HDA function declarations
  */
-static int pci_hda_init(struct vmctx *ctx, struct pci_devinst *pi, nvlist_t *nvl);
-static void pci_hda_write(struct vmctx *ctx, struct pci_devinst *pi,
-    int baridx, uint64_t offset, int size, uint64_t value);
-static uint64_t pci_hda_read(struct vmctx *ctx, struct pci_devinst *pi,
-    int baridx, uint64_t offset, int size);
+static int pci_hda_init(struct pci_devinst *pi, nvlist_t *nvl);
+static void pci_hda_write(struct pci_devinst *pi, int baridx, uint64_t offset,
+    int size, uint64_t value);
+static uint64_t pci_hda_read(struct pci_devinst *pi, int baridx,
+    uint64_t offset, int size);
 /*
  * HDA global data
  */
@@ -1223,11 +1223,10 @@ static uint64_t hda_get_clock_ns(void)
  * PCI HDA function definitions
  */
 static int
-pci_hda_init(struct vmctx *ctx, struct pci_devinst *pi, nvlist_t *nvl)
+pci_hda_init(struct pci_devinst *pi, nvlist_t *nvl)
 {
 	struct hda_softc *sc = NULL;
 
-	assert(ctx != NULL);
 	assert(pi != NULL);
 
 	pci_set_cfgdata16(pi, PCIR_VENDOR, INTEL_VENDORID);
@@ -1256,8 +1255,7 @@ pci_hda_init(struct vmctx *ctx, struct pci_devinst *pi, nvlist_t *nvl)
 }
 
 static void
-pci_hda_write(struct vmctx *ctx __unused,
-    struct pci_devinst *pi, int baridx, uint64_t offset, int size,
+pci_hda_write(struct pci_devinst *pi, int baridx, uint64_t offset, int size,
     uint64_t value)
 {
 	struct hda_softc *sc = pi->pi_arg;
@@ -1274,8 +1272,7 @@ pci_hda_write(struct vmctx *ctx __unused,
 }
 
 static uint64_t
-pci_hda_read(struct vmctx *ctx __unused,
-    struct pci_devinst *pi, int baridx, uint64_t offset, int size)
+pci_hda_read(struct pci_devinst *pi, int baridx, uint64_t offset, int size)
 {
 	struct hda_softc *sc = pi->pi_arg;
 	uint64_t value = 0;
diff --git a/usr.sbin/bhyve/pci_hostbridge.c b/usr.sbin/bhyve/pci_hostbridge.c
index 50c334d8e408..f0878d97a77e 100644
--- a/usr.sbin/bhyve/pci_hostbridge.c
+++ b/usr.sbin/bhyve/pci_hostbridge.c
@@ -37,8 +37,7 @@ __FBSDID("$FreeBSD$");
 #include "pci_emul.h"
 
 static int
-pci_hostbridge_init(struct vmctx *ctx __unused, struct pci_devinst *pi,
-    nvlist_t *nvl)
+pci_hostbridge_init(struct pci_devinst *pi, nvlist_t *nvl)
 {
 	const char *value;
 	u_int vendor, device;
diff --git a/usr.sbin/bhyve/pci_lpc.c b/usr.sbin/bhyve/pci_lpc.c
index e275946d9621..548726e27d0d 100644
--- a/usr.sbin/bhyve/pci_lpc.c
+++ b/usr.sbin/bhyve/pci_lpc.c
@@ -398,8 +398,7 @@ pci_lpc_uart_dsdt(void)
 LPC_DSDT(pci_lpc_uart_dsdt);
 
 static int
-pci_lpc_cfgwrite(struct vmctx *ctx, struct pci_devinst *pi,
-    int coff, int bytes, uint32_t val)
+pci_lpc_cfgwrite(struct pci_devinst *pi, int coff, int bytes, uint32_t val)
 {
 	int pirq_pin;
 
@@ -410,7 +409,7 @@ pci_lpc_cfgwrite(struct vmctx *ctx, struct pci_devinst *pi,
 		if (coff >= 0x68 && coff <= 0x6b)
 			pirq_pin = coff - 0x68 + 5;
 		if (pirq_pin != 0) {
-			pirq_write(ctx, pirq_pin, val);
+			pirq_write(pi->pi_vmctx, pirq_pin, val);
 			pci_set_cfgdata8(pi, coff, pirq_read(pirq_pin));
 			return (0);
 		}
@@ -419,16 +418,14 @@ pci_lpc_cfgwrite(struct vmctx *ctx, struct pci_devinst *pi,
 }
 
 static void
-pci_lpc_write(struct vmctx *ctx __unused,
-    struct pci_devinst *pi __unused, int baridx __unused,
+pci_lpc_write(struct pci_devinst *pi __unused, int baridx __unused,
     uint64_t offset __unused, int size __unused, uint64_t value __unused)
 {
 }
 
 static uint64_t
-pci_lpc_read(struct vmctx *ctx __unused,
-    struct pci_devinst *pi __unused, int baridx __unused, uint64_t offset __unused,
-    int size __unused)
+pci_lpc_read(struct pci_devinst *pi __unused, int baridx __unused,
+    uint64_t offset __unused, int size __unused)
 {
 	return (0);
 }
@@ -437,7 +434,7 @@ pci_lpc_read(struct vmctx *ctx __unused,
 #define	LPC_VENDOR	0x8086
 
 static int
-pci_lpc_init(struct vmctx *ctx, struct pci_devinst *pi, nvlist_t *nvl __unused)
+pci_lpc_init(struct pci_devinst *pi, nvlist_t *nvl __unused)
 {
 	/*
 	 * Do not allow more than one LPC bridge to be configured.
@@ -457,7 +454,7 @@ pci_lpc_init(struct vmctx *ctx, struct pci_devinst *pi, nvlist_t *nvl __unused)
 		return (-1);
 	}
 
-	if (lpc_init(ctx) != 0)
+	if (lpc_init(pi->pi_vmctx) != 0)
 		return (-1);
 
 	/* initialize config space */
diff --git a/usr.sbin/bhyve/pci_nvme.c b/usr.sbin/bhyve/pci_nvme.c
index 75e6f247501a..cf1c815096d3 100644
--- a/usr.sbin/bhyve/pci_nvme.c
+++ b/usr.sbin/bhyve/pci_nvme.c
@@ -1100,7 +1100,7 @@ pci_nvme_reset(struct pci_nvme_softc *sc)
 }
 
 static int
-pci_nvme_init_controller(struct vmctx *ctx, struct pci_nvme_softc *sc)
+pci_nvme_init_controller(struct pci_nvme_softc *sc)
 {
 	uint16_t acqs, asqs;
 
@@ -1119,8 +1119,8 @@ pci_nvme_init_controller(struct vmctx *ctx, struct pci_nvme_softc *sc)
 		return (-1);
 	}
 	sc->submit_queues[0].size = asqs;
-	sc->submit_queues[0].qbase = vm_map_gpa(ctx, sc->regs.asq,
-	            sizeof(struct nvme_command) * asqs);
+	sc->submit_queues[0].qbase = vm_map_gpa(sc->nsc_pi->pi_vmctx,
+	    sc->regs.asq, sizeof(struct nvme_command) * asqs);
 	if (sc->submit_queues[0].qbase == NULL) {
 		EPRINTLN("%s: ASQ vm_map_gpa(%lx) failed", __func__,
 		    sc->regs.asq);
@@ -1140,8 +1140,8 @@ pci_nvme_init_controller(struct vmctx *ctx, struct pci_nvme_softc *sc)
 		return (-1);
 	}
 	sc->compl_queues[0].size = acqs;
-	sc->compl_queues[0].qbase = vm_map_gpa(ctx, sc->regs.acq,
-	         sizeof(struct nvme_completion) * acqs);
+	sc->compl_queues[0].qbase = vm_map_gpa(sc->nsc_pi->pi_vmctx,
+	    sc->regs.acq, sizeof(struct nvme_completion) * acqs);
 	if (sc->compl_queues[0].qbase == NULL) {
 		EPRINTLN("%s: ACQ vm_map_gpa(%lx) failed", __func__,
 		    sc->regs.acq);
@@ -2930,8 +2930,8 @@ pci_nvme_bar0_reg_dumps(const char *func, uint64_t offset, int iswrite)
 }
 
 static void
-pci_nvme_write_bar_0(struct vmctx *ctx, struct pci_nvme_softc* sc,
-	uint64_t offset, int size, uint64_t value)
+pci_nvme_write_bar_0(struct pci_nvme_softc *sc, uint64_t offset, int size,
+    uint64_t value)
 {
 	uint32_t ccreg;
 
@@ -3014,7 +3014,7 @@ pci_nvme_write_bar_0(struct vmctx *ctx, struct pci_nvme_softc* sc,
 				/* transition 1-> causes controller reset */
 				pci_nvme_reset_locked(sc);
 			else
-				pci_nvme_init_controller(ctx, sc);
+				pci_nvme_init_controller(sc);
 		}
 
 		/* Insert the iocqes, iosqes and en bits from the write */
@@ -3062,8 +3062,8 @@ pci_nvme_write_bar_0(struct vmctx *ctx, struct pci_nvme_softc* sc,
 }
 
 static void
-pci_nvme_write(struct vmctx *ctx, struct pci_devinst *pi,
-    int baridx, uint64_t offset, int size, uint64_t value)
+pci_nvme_write(struct pci_devinst *pi, int baridx, uint64_t offset, int size,
+    uint64_t value)
 {
 	struct pci_nvme_softc* sc = pi->pi_arg;
 
@@ -3078,7 +3078,7 @@ pci_nvme_write(struct vmctx *ctx, struct pci_devinst *pi,
 
 	switch (baridx) {
 	case 0:
-		pci_nvme_write_bar_0(ctx, sc, offset, size, value);
+		pci_nvme_write_bar_0(sc, offset, size, value);
 		break;
 
 	default:
@@ -3125,8 +3125,7 @@ static uint64_t pci_nvme_read_bar_0(struct pci_nvme_softc* sc,
 
 
 static uint64_t
-pci_nvme_read(struct vmctx *ctx __unused,
-    struct pci_devinst *pi, int baridx, uint64_t offset, int size)
+pci_nvme_read(struct pci_devinst *pi, int baridx, uint64_t offset, int size)
 {
 	struct pci_nvme_softc* sc = pi->pi_arg;
 
@@ -3276,7 +3275,7 @@ pci_nvme_resized(struct blockif_ctxt *bctxt __unused, void *arg,
 }
 
 static int
-pci_nvme_init(struct vmctx *ctx __unused, struct pci_devinst *pi, nvlist_t *nvl)
+pci_nvme_init(struct pci_devinst *pi, nvlist_t *nvl)
 {
 	struct pci_nvme_softc *sc;
 	uint32_t pci_membar_sz;
diff --git a/usr.sbin/bhyve/pci_passthru.c b/usr.sbin/bhyve/pci_passthru.c
index 3dbb80eec9fa..f42bbbda655f 100644
--- a/usr.sbin/bhyve/pci_passthru.c
+++ b/usr.sbin/bhyve/pci_passthru.c
@@ -386,8 +386,8 @@ msix_table_read(struct passthru_softc *sc, uint64_t offset, int size)
 }
 
 static void
-msix_table_write(struct vmctx *ctx, struct passthru_softc *sc,
-		 uint64_t offset, int size, uint64_t data)
+msix_table_write(struct passthru_softc *sc, uint64_t offset, int size,
+    uint64_t data)
 {
 	struct pci_devinst *pi;
 	struct msix_table_entry *entry;
@@ -445,7 +445,7 @@ msix_table_write(struct vmctx *ctx, struct passthru_softc *sc,
 		/* If the entry is masked, don't set it up */
 		if ((entry->vector_control & PCIM_MSIX_VCTRL_MASK) == 0 ||
 		    (vector_control & PCIM_MSIX_VCTRL_MASK) == 0) {
-			(void)vm_setup_pptdev_msix(ctx, 0,
+			(void)vm_setup_pptdev_msix(sc->psc_pi->pi_vmctx, 0,
 			    sc->psc_sel.pc_bus, sc->psc_sel.pc_dev,
 			    sc->psc_sel.pc_func, index, entry->addr,
 			    entry->msg_data, entry->vector_control);
@@ -454,7 +454,7 @@ msix_table_write(struct vmctx *ctx, struct passthru_softc *sc,
 }
 
 static int
-init_msix_table(struct vmctx *ctx __unused, struct passthru_softc *sc)
+init_msix_table(struct passthru_softc *sc)
 {
 	struct pci_devinst *pi = sc->psc_pi;
 	struct pci_bar_mmap pbm;
@@ -515,7 +515,7 @@ init_msix_table(struct vmctx *ctx __unused, struct passthru_softc *sc)
 }
 
 static int
-cfginitbar(struct vmctx *ctx __unused, struct passthru_softc *sc)
+cfginitbar(struct passthru_softc *sc)
 {
 	int i, error;
 	struct pci_devinst *pi;
@@ -596,7 +596,7 @@ cfginitbar(struct vmctx *ctx __unused, struct passthru_softc *sc)
 }
 
 static int
-cfginit(struct vmctx *ctx, struct pci_devinst *pi, int bus, int slot, int func)
+cfginit(struct pci_devinst *pi, int bus, int slot, int func)
 {
 	int error;
 	struct passthru_softc *sc;
@@ -615,7 +615,7 @@ cfginit(struct vmctx *ctx, struct pci_devinst *pi, int bus, int slot, int func)
 		goto done;
 	}
 
-	if (cfginitbar(ctx, sc) != 0) {
+	if (cfginitbar(sc) != 0) {
 		warnx("failed to initialize BARs for PCI %d/%d/%d",
 		    bus, slot, func);
 		goto done;
@@ -629,7 +629,7 @@ cfginit(struct vmctx *ctx, struct pci_devinst *pi, int bus, int slot, int func)
 	 * a BAR was enabled, as otherwise the PCIOCBARMMAP might fail on us.
 	 */
 	if (pci_msix_table_bar(pi) >= 0) {
-		error = init_msix_table(ctx, sc);
+		error = init_msix_table(sc);
 		if (error != 0) {
 			warnx(
 			    "failed to initialize MSI-X table for PCI %d/%d/%d: %d",
@@ -682,8 +682,7 @@ passthru_legacy_config(nvlist_t *nvl, const char *opts)
 }
 
 static int
-passthru_init_rom(struct vmctx *const ctx __unused,
-    struct passthru_softc *const sc, const char *const romfile)
+passthru_init_rom(struct passthru_softc *const sc, const char *const romfile)
 {
 	if (romfile == NULL) {
 		return (0);
@@ -799,7 +798,7 @@ passthru_lookup_pptdev(const char *name, int *bus, int *slot, int *func)
 }
 
 static int
-passthru_init(struct vmctx *ctx, struct pci_devinst *pi, nvlist_t *nvl)
+passthru_init(struct pci_devinst *pi, nvlist_t *nvl)
 {
 	int bus, slot, func, error, memflags;
 	struct passthru_softc *sc;
@@ -808,7 +807,7 @@ passthru_init(struct vmctx *ctx, struct pci_devinst *pi, nvlist_t *nvl)
 	sc = NULL;
 	error = 1;
 
-	memflags = vm_get_memflags(ctx);
+	memflags = vm_get_memflags(pi->pi_vmctx);
 	if (!(memflags & VM_MEM_F_WIRED)) {
 		warnx("passthru requires guest memory to be wired");
 		return (error);
@@ -837,7 +836,7 @@ passthru_init(struct vmctx *ctx, struct pci_devinst *pi, nvlist_t *nvl)
 		GET_INT_CONFIG(func, "func");
 	}
 
-	if (vm_assign_pptdev(ctx, bus, slot, func) != 0) {
+	if (vm_assign_pptdev(pi->pi_vmctx, bus, slot, func) != 0) {
 		warnx("PCI device at %d/%d/%d is not using the ppt(4) driver",
 		    bus, slot, func);
 		goto done;
@@ -849,11 +848,11 @@ passthru_init(struct vmctx *ctx, struct pci_devinst *pi, nvlist_t *nvl)
 	sc->psc_pi = pi;
 
 	/* initialize config space */
-	if ((error = cfginit(ctx, pi, bus, slot, func)) != 0)
+	if ((error = cfginit(pi, bus, slot, func)) != 0)
 		goto done;
 
 	/* initialize ROM */
-	if ((error = passthru_init_rom(ctx, sc,
+	if ((error = passthru_init_rom(sc,
             get_config_value_node(nvl, "rom"))) != 0)
 		goto done;
 
@@ -861,7 +860,7 @@ passthru_init(struct vmctx *ctx, struct pci_devinst *pi, nvlist_t *nvl)
 done:
 	if (error) {
 		free(sc);
-		vm_unassign_pptdev(ctx, bus, slot, func);
+		vm_unassign_pptdev(pi->pi_vmctx, bus, slot, func);
 	}
 	return (error);
 }
@@ -903,8 +902,7 @@ msixcap_access(struct passthru_softc *sc, int coff)
 }
 
 static int
-passthru_cfgread(struct vmctx *ctx __unused, struct pci_devinst *pi, int coff,
-    int bytes, uint32_t *rv)
+passthru_cfgread(struct pci_devinst *pi, int coff, int bytes, uint32_t *rv)
 {
 	struct passthru_softc *sc;
 
@@ -948,8 +946,7 @@ passthru_cfgread(struct vmctx *ctx __unused, struct pci_devinst *pi, int coff,
 }
 
 static int
-passthru_cfgwrite(struct vmctx *ctx, struct pci_devinst *pi, int coff,
-    int bytes, uint32_t val)
+passthru_cfgwrite(struct pci_devinst *pi, int coff, int bytes, uint32_t val)
 {
 	int error, msix_table_entries, i;
 	struct passthru_softc *sc;
@@ -969,7 +966,7 @@ passthru_cfgwrite(struct vmctx *ctx, struct pci_devinst *pi, int coff,
 	if (msicap_access(sc, coff)) {
 		pci_emul_capwrite(pi, coff, bytes, val, sc->psc_msi.capoff,
 		    PCIY_MSI);
-		error = vm_setup_pptdev_msi(ctx, 0, sc->psc_sel.pc_bus,
+		error = vm_setup_pptdev_msi(pi->pi_vmctx, 0, sc->psc_sel.pc_bus,
 			sc->psc_sel.pc_dev, sc->psc_sel.pc_func,
 			pi->pi_msi.addr, pi->pi_msi.msg_data,
 			pi->pi_msi.maxmsgnum);
@@ -984,7 +981,7 @@ passthru_cfgwrite(struct vmctx *ctx, struct pci_devinst *pi, int coff,
 		if (pi->pi_msix.enabled) {
 			msix_table_entries = pi->pi_msix.table_count;
 			for (i = 0; i < msix_table_entries; i++) {
-				error = vm_setup_pptdev_msix(ctx, 0,
+				error = vm_setup_pptdev_msix(pi->pi_vmctx, 0,
 				    sc->psc_sel.pc_bus, sc->psc_sel.pc_dev,
 				    sc->psc_sel.pc_func, i,
 				    pi->pi_msix.table[i].addr,
@@ -995,8 +992,9 @@ passthru_cfgwrite(struct vmctx *ctx, struct pci_devinst *pi, int coff,
 					err(1, "vm_setup_pptdev_msix");
 			}
 		} else {
-			error = vm_disable_pptdev_msix(ctx, sc->psc_sel.pc_bus,
-			    sc->psc_sel.pc_dev, sc->psc_sel.pc_func);
+			error = vm_disable_pptdev_msix(pi->pi_vmctx,
+			    sc->psc_sel.pc_bus, sc->psc_sel.pc_dev,
+			    sc->psc_sel.pc_func);
 			if (error)
 				err(1, "vm_disable_pptdev_msix");
 		}
@@ -1029,8 +1027,8 @@ passthru_cfgwrite(struct vmctx *ctx, struct pci_devinst *pi, int coff,
 }
 
 static void
-passthru_write(struct vmctx *ctx, struct pci_devinst *pi, int baridx,
-    uint64_t offset, int size, uint64_t value)
+passthru_write(struct pci_devinst *pi, int baridx, uint64_t offset, int size,
+    uint64_t value)
 {
 	struct passthru_softc *sc;
 	struct pci_bar_ioreq pio;
@@ -1038,7 +1036,7 @@ passthru_write(struct vmctx *ctx, struct pci_devinst *pi, int baridx,
 	sc = pi->pi_arg;
 
 	if (baridx == pci_msix_table_bar(pi)) {
-		msix_table_write(ctx, sc, offset, size, value);
+		msix_table_write(sc, offset, size, value);
 	} else {
 		assert(pi->pi_bar[baridx].type == PCIBAR_IO);
 		assert(size == 1 || size == 2 || size == 4);
@@ -1057,8 +1055,7 @@ passthru_write(struct vmctx *ctx, struct pci_devinst *pi, int baridx,
 }
 
 static uint64_t
-passthru_read(struct vmctx *ctx __unused, struct pci_devinst *pi, int baridx,
-    uint64_t offset, int size)
+passthru_read(struct pci_devinst *pi, int baridx, uint64_t offset, int size)
 {
 	struct passthru_softc *sc;
 	struct pci_bar_ioreq pio;
@@ -1089,8 +1086,8 @@ passthru_read(struct vmctx *ctx __unused, struct pci_devinst *pi, int baridx,
 }
 
 static void
-passthru_msix_addr(struct vmctx *ctx, struct pci_devinst *pi, int baridx,
-		   int enabled, uint64_t address)
+passthru_msix_addr(struct pci_devinst *pi, int baridx, int enabled,
+    uint64_t address)
 {
 	struct passthru_softc *sc;
 	size_t remaining;
@@ -1100,13 +1097,14 @@ passthru_msix_addr(struct vmctx *ctx, struct pci_devinst *pi, int baridx,
 	table_offset = rounddown2(pi->pi_msix.table_offset, 4096);
 	if (table_offset > 0) {
 		if (!enabled) {
-			if (vm_unmap_pptdev_mmio(ctx, sc->psc_sel.pc_bus,
+			if (vm_unmap_pptdev_mmio(pi->pi_vmctx,
+						 sc->psc_sel.pc_bus,
 						 sc->psc_sel.pc_dev,
 						 sc->psc_sel.pc_func, address,
 						 table_offset) != 0)
 				warnx("pci_passthru: unmap_pptdev_mmio failed");
 		} else {
-			if (vm_map_pptdev_mmio(ctx, sc->psc_sel.pc_bus,
+			if (vm_map_pptdev_mmio(pi->pi_vmctx, sc->psc_sel.pc_bus,
 					       sc->psc_sel.pc_dev,
*** 398 LINES SKIPPED ***