The branch main has been updated by corvink: URL: https://cgit.FreeBSD.org/src/commit/?id=6dda2653bd0ee297b114682972b11d6d3c74a6a6
commit 6dda2653bd0ee297b114682972b11d6d3c74a6a6 Author: Corvin Köhne <corv...@freebsd.org> AuthorDate: 2024-01-08 14:04:04 +0000 Commit: Corvin Köhne <corv...@freebsd.org> CommitDate: 2025-08-05 14:02:09 +0000 bhyve: add interface to protect BAR regions of passthru devices We need an interface for protecting BAR regions to make it easier to use this feature and to make it usable by external emulations like the GVT-d emulation. Reviewed by: jhb MFC after: 1 week Sponsored by: Beckhoff Automation GmbH & Co. KG Differential Revision: https://reviews.freebsd.org/D45341 --- usr.sbin/bhyve/pci_passthru.c | 39 +++++++++++++++++++++++++++++++++++++++ usr.sbin/bhyve/pci_passthru.h | 3 +++ 2 files changed, 42 insertions(+) diff --git a/usr.sbin/bhyve/pci_passthru.c b/usr.sbin/bhyve/pci_passthru.c index 2b84c561927a..8ddcd8bd56e8 100644 --- a/usr.sbin/bhyve/pci_passthru.c +++ b/usr.sbin/bhyve/pci_passthru.c @@ -752,6 +752,45 @@ set_pcir_handler(struct passthru_softc *sc, int reg, int len, return (0); } +int +passthru_set_bar_handler(struct passthru_softc *sc, int baridx, uint64_t off, + uint64_t size, passthru_read_handler rhandler, + passthru_write_handler whandler) +{ + struct passthru_bar_handler *handler_new; + struct passthru_bar_handler *handler; + + assert(sc->psc_bar[baridx].type == PCIBAR_IO || + sc->psc_bar[baridx].type == PCIBAR_MEM32 || + sc->psc_bar[baridx].type == PCIBAR_MEM64); + assert(sc->psc_bar[baridx].size >= off + size); + assert(off < off + size); + + handler_new = malloc(sizeof(struct passthru_bar_handler)); + if (handler_new == NULL) { + return (ENOMEM); + } + + handler_new->off = off; + handler_new->size = size; + handler_new->read = rhandler; + handler_new->write = whandler; + + TAILQ_FOREACH(handler, &sc->psc_bar_handler[baridx], chain) { + if (handler->off < handler_new->off) { + assert(handler->off + handler->size < handler_new->off); + continue; + } + assert(handler->off > handler_new->off + handler_new->size); + TAILQ_INSERT_BEFORE(handler, handler_new, chain); + return (0); + } + + TAILQ_INSERT_TAIL(&sc->psc_bar_handler[baridx], handler_new, chain); + + return (0); +} + static int passthru_legacy_config(nvlist_t *nvl, const char *opts) { diff --git a/usr.sbin/bhyve/pci_passthru.h b/usr.sbin/bhyve/pci_passthru.h index fc3fdbc1719f..9e7b27d95735 100644 --- a/usr.sbin/bhyve/pci_passthru.h +++ b/usr.sbin/bhyve/pci_passthru.h @@ -53,3 +53,6 @@ struct passthru_mmio_mapping *passthru_get_mmio(struct passthru_softc *sc, struct pcisel *passthru_get_sel(struct passthru_softc *sc); int set_pcir_handler(struct passthru_softc *sc, int reg, int len, cfgread_handler rhandler, cfgwrite_handler whandler); +int passthru_set_bar_handler(struct passthru_softc *sc, int baridx, + uint64_t off, uint64_t size, passthru_read_handler rhandler, + passthru_write_handler whandler);