From nobody Wed Sep 25 16:51:06 2024 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4XDN622Z3Bz5XRmw; Wed, 25 Sep 2024 16:51:06 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4XDN621tBDz4Yny; Wed, 25 Sep 2024 16:51:06 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1727283066; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=bSBQwOzkPxiGdO0tZwvtxz6H6oowOm3FW+4jFf4hm5k=; b=pq3V5qWE7M6aCVQnscw7hrEI8wrHO/+37J24fyjMJXIuun3C+eooNLcV+DDcR652N7t2oL 1RnUUzo/Yr2bFEj8lQM/LC3Upw61a+KxA89PdoObiJoLqVpTvwO7wH1ZJQkxaI5CWxa/8+ m7qOKYygyigrKPmrOp2YViN01UR15Rt9UPNTWPOL+SDzvSQK1LKcQEjWJN4k+BHCiE37Mb jfvib8z9lle0SP7h3tgTq38t0F2RAwpPvcyydAQa+4UxCc+CHCd7IrtEt1COFzYAWr5M+M SrHXvoyjwHJEZZoa4f8VJWu+EkbUYdHWDq6vI5yJy0AEL04IJY13+fS3DjMvnA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1727283066; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=bSBQwOzkPxiGdO0tZwvtxz6H6oowOm3FW+4jFf4hm5k=; b=RDb6E4jE8dIhVeyrKSKH2cLVOPZQMmmahDLtxwWdfsnDVy40+bH2zP5TyvnClxY0MBejyW vA71px10/w/xxKcf13gcCKDSFj80jJesAA/7oUhSKMEj8rYmSLtgmtHcOsMV81pyQVpTjF szJthFKpr7Aqak3J+wHwpwu/tqeZ1EFNmx+/MVUwIUc63ME6fkPBakCHSd9BB3J4kmAU7P OYEjKh/oWksyRijjKLGnGa+B6DTj1eqp4RwDaCdZgP2uRCr7YnEe9/Tfy4qJ3xwYEnAZIf pdLUqYJH8FDQPrAHW+2Xmri63rYPbyhIym3eDX66f010keC/alpfJ4MwCjuV/A== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1727283066; a=rsa-sha256; cv=none; b=UyPtQ8djC+ubxHsoDwXvw5bnwc83koRa4fssiwT84d9vJJultKKD9u/mCWEC5XwSxhXNMO QmdSOU9gIts7CIXFDqUwl2dx2aFuNni0Jcd6hgH/SO92liyvTNy+N/uU6wglrAqKbOiUVm yL7TquG2VGWmt9ZLPkRE5jWrT1Kv/PMIOpl6O808Jh6pl/yVqbLy8wUbzapcoCSWk69MCA pYXmFRI+g92/9f9R/oHe6tQTYeJQdPx3qfb1rTIbtnFYCDp7tb1HePqXMAkqRhBOalMELn /i/wv9u1kNJdCV2su0Ov1W5YurhTdH7VXPxHxJWfhjOOkgVswYoFr5qFPV2wTA== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4XDN6219KczKsC; Wed, 25 Sep 2024 16:51:06 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 48PGp6rU005035; Wed, 25 Sep 2024 16:51:06 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 48PGp6dV005032; Wed, 25 Sep 2024 16:51:06 GMT (envelope-from git) Date: Wed, 25 Sep 2024 16:51:06 GMT Message-Id: <202409251651.48PGp6dV005032@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Ed Maste Subject: git: 39f39a96569d - stable/14 - bhyve: simplify slot validation in xHCI emulation List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: emaste X-Git-Repository: src X-Git-Refname: refs/heads/stable/14 X-Git-Reftype: branch X-Git-Commit: 39f39a96569dc9f8ca9fc8ee351d3d6edabfb631 Auto-Submitted: auto-generated The branch stable/14 has been updated by emaste: URL: https://cgit.FreeBSD.org/src/commit/?id=39f39a96569dc9f8ca9fc8ee351d3d6edabfb631 commit 39f39a96569dc9f8ca9fc8ee351d3d6edabfb631 Author: Pierre Pronchery AuthorDate: 2024-09-18 18:52:04 +0000 Commit: Ed Maste CommitDate: 2024-09-25 16:50:41 +0000 bhyve: simplify slot validation in xHCI emulation This is a follow-up to commit e72d86ad9c62 ("bhyve: improve input validation in pci_xhci") -- introducing a helper for slot validation. Co-authored-by: John Baldwin Reviewed by: markj, emaste Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D46696 (cherry picked from commit f505f9a842eb3e75e16e6c7c6f67d5b592b1bc65) --- usr.sbin/bhyve/pci_xhci.c | 90 +++++++++++++++++------------------------------ 1 file changed, 32 insertions(+), 58 deletions(-) diff --git a/usr.sbin/bhyve/pci_xhci.c b/usr.sbin/bhyve/pci_xhci.c index 77b17d525bdd..43f30c85d85f 100644 --- a/usr.sbin/bhyve/pci_xhci.c +++ b/usr.sbin/bhyve/pci_xhci.c @@ -348,6 +348,7 @@ static void pci_xhci_update_ep_ring(struct pci_xhci_softc *sc, struct pci_xhci_dev_emu *dev, struct pci_xhci_dev_ep *devep, struct xhci_endp_ctx *ep_ctx, uint32_t streamid, uint64_t ringaddr, int ccs); +static int pci_xhci_validate_slot(uint32_t slot); static void pci_xhci_set_evtrb(struct xhci_trb *evtrb, uint64_t port, uint32_t errcode, @@ -849,17 +850,14 @@ pci_xhci_cmd_disable_slot(struct pci_xhci_softc *sc, uint32_t slot) DPRINTF(("pci_xhci disable slot %u", slot)); - cmderr = XHCI_TRB_ERROR_NO_SLOTS; - if (sc->portregs == NULL) + if (sc->portregs == NULL) { + cmderr = XHCI_TRB_ERROR_NO_SLOTS; goto done; + } - if (slot == 0) { - cmderr = XHCI_TRB_ERROR_TRB; - goto done; - } else if (slot > XHCI_MAX_SLOTS) { - cmderr = XHCI_TRB_ERROR_SLOT_NOT_ON; + cmderr = pci_xhci_validate_slot(slot); + if (cmderr != XHCI_TRB_ERROR_SUCCESS) goto done; - } dev = XHCI_SLOTDEV_PTR(sc, slot); if (dev) { @@ -867,7 +865,6 @@ pci_xhci_cmd_disable_slot(struct pci_xhci_softc *sc, uint32_t slot) cmderr = XHCI_TRB_ERROR_SLOT_NOT_ON; } else { dev->dev_slotstate = XHCI_ST_DISABLED; - cmderr = XHCI_TRB_ERROR_SUCCESS; /* TODO: reset events and endpoints */ } } else @@ -886,19 +883,16 @@ pci_xhci_cmd_reset_device(struct pci_xhci_softc *sc, uint32_t slot) uint32_t cmderr; int i; - cmderr = XHCI_TRB_ERROR_NO_SLOTS; - if (sc->portregs == NULL) + if (sc->portregs == NULL) { + cmderr = XHCI_TRB_ERROR_NO_SLOTS; goto done; + } DPRINTF(("pci_xhci reset device slot %u", slot)); - if (slot == 0) { - cmderr = XHCI_TRB_ERROR_TRB; + cmderr = pci_xhci_validate_slot(slot); + if (cmderr != XHCI_TRB_ERROR_SUCCESS) goto done; - } else if (slot > XHCI_MAX_SLOTS) { - cmderr = XHCI_TRB_ERROR_SLOT_NOT_ON; - goto done; - } dev = XHCI_SLOTDEV_PTR(sc, slot); if (!dev || dev->dev_slotstate == XHCI_ST_DISABLED) @@ -928,8 +922,6 @@ pci_xhci_cmd_reset_device(struct pci_xhci_softc *sc, uint32_t slot) ep_ctx->dwEpCtx0 = FIELD_REPLACE( ep_ctx->dwEpCtx0, XHCI_ST_EPCTX_DISABLED, 0x7, 0); } - - cmderr = XHCI_TRB_ERROR_SUCCESS; } pci_xhci_reset_slot(sc, slot); @@ -953,8 +945,6 @@ pci_xhci_cmd_address_device(struct pci_xhci_softc *sc, uint32_t slot, islot_ctx = &input_ctx->ctx_slot; ep0_ctx = &input_ctx->ctx_ep[1]; - cmderr = XHCI_TRB_ERROR_SUCCESS; - DPRINTF(("pci_xhci: address device, input ctl: D 0x%08x A 0x%08x,", input_ctx->ctx_input.dwInCtx0, input_ctx->ctx_input.dwInCtx1)); DPRINTF((" slot %08x %08x %08x %08x", @@ -972,13 +962,9 @@ pci_xhci_cmd_address_device(struct pci_xhci_softc *sc, uint32_t slot, goto done; } - if (slot == 0) { - cmderr = XHCI_TRB_ERROR_TRB; + cmderr = pci_xhci_validate_slot(slot); + if (cmderr != XHCI_TRB_ERROR_SUCCESS) goto done; - } else if (slot > XHCI_MAX_SLOTS) { - cmderr = XHCI_TRB_ERROR_SLOT_NOT_ON; - goto done; - } /* assign address to slot */ dev_ctx = pci_xhci_get_dev_ctx(sc, slot); @@ -1042,17 +1028,11 @@ pci_xhci_cmd_config_ep(struct pci_xhci_softc *sc, uint32_t slot, uint32_t cmderr; int i; - cmderr = XHCI_TRB_ERROR_SUCCESS; - DPRINTF(("pci_xhci config_ep slot %u", slot)); - if (slot == 0) { - cmderr = XHCI_TRB_ERROR_TRB; + cmderr = pci_xhci_validate_slot(slot); + if (cmderr != XHCI_TRB_ERROR_SUCCESS) goto done; - } else if (slot > XHCI_MAX_SLOTS) { - cmderr = XHCI_TRB_ERROR_SLOT_NOT_ON; - goto done; - } dev = XHCI_SLOTDEV_PTR(sc, slot); assert(dev != NULL); @@ -1175,15 +1155,9 @@ pci_xhci_cmd_reset_ep(struct pci_xhci_softc *sc, uint32_t slot, DPRINTF(("pci_xhci: reset ep %u: slot %u", epid, slot)); - cmderr = XHCI_TRB_ERROR_SUCCESS; - - if (slot == 0) { - cmderr = XHCI_TRB_ERROR_TRB; - goto done; - } else if (slot > XHCI_MAX_SLOTS) { - cmderr = XHCI_TRB_ERROR_SLOT_NOT_ON; + cmderr = pci_xhci_validate_slot(slot); + if (cmderr != XHCI_TRB_ERROR_SUCCESS) goto done; - } dev = XHCI_SLOTDEV_PTR(sc, slot); assert(dev != NULL); @@ -1272,15 +1246,9 @@ pci_xhci_cmd_set_tr(struct pci_xhci_softc *sc, uint32_t slot, uint32_t cmderr, epid; uint32_t streamid; - cmderr = XHCI_TRB_ERROR_SUCCESS; - - if (slot == 0) { - cmderr = XHCI_TRB_ERROR_TRB; - goto done; - } else if (slot > XHCI_MAX_SLOTS) { - cmderr = XHCI_TRB_ERROR_SLOT_NOT_ON; + cmderr = pci_xhci_validate_slot(slot); + if (cmderr != XHCI_TRB_ERROR_SUCCESS) goto done; - } dev = XHCI_SLOTDEV_PTR(sc, slot); assert(dev != NULL); @@ -1362,7 +1330,6 @@ pci_xhci_cmd_eval_ctx(struct pci_xhci_softc *sc, uint32_t slot, islot_ctx = &input_ctx->ctx_slot; ep0_ctx = &input_ctx->ctx_ep[1]; - cmderr = XHCI_TRB_ERROR_SUCCESS; DPRINTF(("pci_xhci: eval ctx, input ctl: D 0x%08x A 0x%08x,", input_ctx->ctx_input.dwInCtx0, input_ctx->ctx_input.dwInCtx1)); DPRINTF((" slot %08x %08x %08x %08x", @@ -1380,13 +1347,9 @@ pci_xhci_cmd_eval_ctx(struct pci_xhci_softc *sc, uint32_t slot, goto done; } - if (slot == 0) { - cmderr = XHCI_TRB_ERROR_TRB; - goto done; - } else if (slot > XHCI_MAX_SLOTS) { - cmderr = XHCI_TRB_ERROR_SLOT_NOT_ON; + cmderr = pci_xhci_validate_slot(slot); + if (cmderr != XHCI_TRB_ERROR_SUCCESS) goto done; - } /* assign address to slot; in this emulation, slot_id = address */ dev_ctx = pci_xhci_get_dev_ctx(sc, slot); @@ -1723,6 +1686,17 @@ pci_xhci_update_ep_ring(struct pci_xhci_softc *sc, } } +static int +pci_xhci_validate_slot(uint32_t slot) +{ + if (slot == 0) + return (XHCI_TRB_ERROR_TRB); + else if (slot > XHCI_MAX_SLOTS) + return (XHCI_TRB_ERROR_SLOT_NOT_ON); + else + return (XHCI_TRB_ERROR_SUCCESS); +} + /* * Outstanding transfer still in progress (device NAK'd earlier) so retry * the transfer again to see if it succeeds.