From nobody Sat Sep 21 17:37:38 2024 X-Original-To: dev-commits-src-main@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 4X9xKb3qDhz5XhJ5; Sat, 21 Sep 2024 17:37:39 +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 4X9xKb0y13z3xY2; Sat, 21 Sep 2024 17:37:39 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1726940259; 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=zXroMXPE7qO/pNkxV9oZOU79OBO3S6pOi7DdBd46Vb0=; b=hqbzPZXZj954Y3XUnKK6lCew6JHdO3Fou52IPezXTHUCQ1ump7JLCmAvKAX2h/Eq/ka9i1 JRRVTafGrjPPN2R3tXDs1QEHIcWbXN9F39+GxpW7tzvDZjSCs5Og9Q7xUsZMhSuELQBDCk rpumWewg02Wa4vINnhl59bVcJAjtuTLEgQ7/1xTYlgNtPGyvuuxFyR0acj9GUU7m0TNfir nfHolxXQWeoDo+dIx8zshZ9bbjs89leocA9h2yvOsLucA3mieAW5pBCUYRYaHJ2pSG5tEd R/o3e3w/mGbCdDbpHBB86MEa+xIWRQx74vWx0iQHoIdplKf897RfbWdiUZn1XQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1726940259; a=rsa-sha256; cv=none; b=i9xp0veUQ76dOBIQHGJ+JZ5s02vqYpACnsDtARNXHWE01xYGDhfr2Zw9oV42O7vZao5Aky 9VzCcbYQKP2YD0Esbu5bh2/cmpC/jsmDF0dxEUN41RQEf345/AjmQpg8ReGalTElafhMTv BbXBD/EhG0glaIjEsE998qpXsdrWyqmuvIEHZnp0ZkjDDegC/ruSxb/57B3E9upTGWq0mF EcHx4RVc8dw1pVyaPwUX2UIddbCeFpElGa8LZ16fTysmXnv5HQ+Nywa6uN86hav/oL+3Yf 0BJfqYaGi9ZUlajkw/FUcb3u3pudD6CwoysHf8CD8GXykt7DKVs3jJWQSKfQbg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1726940259; 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=zXroMXPE7qO/pNkxV9oZOU79OBO3S6pOi7DdBd46Vb0=; b=j2e4rvlrBrCQj9o5WTUc1xywI4jTaM3lLozj4MlqFOvk3KVsIYUY6FsvLqmh4NjN0r+I9O lxCqHR+x0k8vk71nUPVMgBONIWZHcG1xVf8QZUJln23xiJ8yZ0a/B0vku2AIysBB4d5Cmh zQRlMaWE4Xr4PrMSffXkZKAUQi0J0aMshg0k92UftGod9FS7+6cds1dLaODX7umast43/c 9LhQv429KKZhYXFF7MU1Hp4CD+1iWD/u5ZZU3nPCpy7dMc58ka3JZnHoZTfoBBQAv4ILbn /D6uhqyq8TGXLlwDsEXLS2wfepqTpCjmfgEFH5U7dxCj3wIYoSv1lqC50PI+mw== 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 4X9xKb0D1Pzj5B; Sat, 21 Sep 2024 17:37:38 +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 48LHbcjI070430; Sat, 21 Sep 2024 17:37:38 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 48LHbcks070427; Sat, 21 Sep 2024 17:37:38 GMT (envelope-from git) Date: Sat, 21 Sep 2024 17:37:38 GMT Message-Id: <202409211737.48LHbcks070427@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Ed Maste Subject: git: f505f9a842eb - main - bhyve: simplify slot validation in xHCI emulation List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@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/main X-Git-Reftype: branch X-Git-Commit: f505f9a842eb3e75e16e6c7c6f67d5b592b1bc65 Auto-Submitted: auto-generated The branch main has been updated by emaste: URL: https://cgit.FreeBSD.org/src/commit/?id=f505f9a842eb3e75e16e6c7c6f67d5b592b1bc65 commit f505f9a842eb3e75e16e6c7c6f67d5b592b1bc65 Author: Pierre Pronchery AuthorDate: 2024-09-18 18:52:04 +0000 Commit: Ed Maste CommitDate: 2024-09-21 17:36:10 +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 --- 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 2399cb4fc9b8..5b21361f2823 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.