From nobody Mon Nov 23 04:26:02 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linux.dev Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1793417907908969.2270158681711; Fri, 30 Oct 2026 20:38:27 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xMzub-0003qA-Nt; Fri, 30 Oct 2026 23:37:45 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xMzuY-0003ok-PQ for qemu-devel@nongnu.org; Fri, 30 Oct 2026 23:37:42 -0400 Received: from out-4.mta1.migadu.com ([95.215.58.4] helo=mta1.migadu.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1xMzuW-0003sr-DV for qemu-devel@nongnu.org; Fri, 30 Oct 2026 23:37:42 -0400 Received: by smtp.migadu.com with ESMTPS id c7b862658f3f20ba; Sat, 31 Oct 2026 03:37:37 +0000 X-Envelope-To: qemu-devel@nongnu.org DKIM-Signature: a=rsa-sha256; bh=w7HhoT+HZlE5uIaC1kjOj9y3j81Oh6aWbF0ordaezyc=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1793417857; v=1; x=1794022657; b=fA7P6i3zIZZhz6YmyHIW24+iQNfs+9JnOuN/aJ1kboKB+dGkqJnh/nxcLKa/7oNQ59Osph/r Idjj+aNGbKJ4dcK4GeS30czZg9LLZcAbDSz7gme8mOXBM4pbOpRbsrGLGCZjS94/fwMgWJsHnjf yUQX5ri72lkZhu/58n1GgGGM= X-Envelope-To: qemu-devel@nongnu.org X-Mizu-Trace-ID: c7b862658f3f20ba X-Migadu-Flow: FLOW_OUT From: Junjie Cao To: qemu-devel@nongnu.org Cc: Thomas Huth , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Peter Maydell , Fabiano Rosas , Laurent Vivier , Paolo Bonzini , junjie.cao@linux.dev, qemu-stable@nongnu.org Subject: [PATCH 1/2] hw/usb/hcd-xhci: fail Address Device when the DCBAA entry is unreadable Date: Fri, 30 Oct 2026 22:37:24 -0500 Message-ID: <20261031033725.1275414-2-junjie.cao@linux.dev> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261031033725.1275414-1-junjie.cao@linux.dev> References: <20261031033725.1275414-1-junjie.cao@linux.dev> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=95.215.58.4; envelope-from=junjie.cao@linux.dev; helo=mta1.migadu.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1793417913674154100 Content-Type: text/plain; charset="utf-8" xhci_address_slot() ignores the result of reading the slot's DCBAA entry. When that read fails the output context pointer is whatever the failed read left behind, zero for unassigned memory, so the slot and ep0 contexts land at guest address 0 and the command completes with Success. Log the failed read and raise HCE, as the context DMA helpers do, and return TRB Error, as Address Device already does for an unreadable input control context. xhci_die() only flags HCE and leaves the controller running; the HSE and halt of xHCI 1.2 section 4.10.2.6 are not added here. usb_xhci_post_load() reads the same entry; a failure there now disables the slot, as an unresolvable slot context already does. Fixes: 62c6ae04cf43 ("xhci: Initial xHCI implementation") Fixes: 37352df30fbc ("xhci: add live migration support") Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4706 Cc: qemu-stable@nongnu.org Signed-off-by: Junjie Cao Reviewed-by: Thomas Huth --- hw/usb/hcd-xhci.c | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/hw/usb/hcd-xhci.c b/hw/usb/hcd-xhci.c index 5d7a77b8bc..b5cc1ad2ff 100644 --- a/hw/usb/hcd-xhci.c +++ b/hw/usb/hcd-xhci.c @@ -534,6 +534,23 @@ static inline void xhci_dma_write_u32s(XHCIState *xhci= , dma_addr_t addr, } } =20 +static bool xhci_read_dcbaa(XHCIState *xhci, unsigned int slotid, + dma_addr_t *octx) +{ + dma_addr_t dcbaap =3D xhci_addr64(xhci->dcbaap_low, xhci->dcbaap_high); + uint64_t poctx; + + if (ldq_le_dma(xhci->as, dcbaap + 8 * slotid, &poctx, + MEMTXATTRS_UNSPECIFIED) !=3D MEMTX_OK) { + qemu_log_mask(LOG_GUEST_ERROR, "%s: DMA memory access failed!\n", + __func__); + xhci_die(xhci); + return false; + } + *octx =3D xhci_mask64(poctx); + return true; +} + static XHCIPort *xhci_lookup_port(XHCIState *xhci, struct USBPort *uport) { int index; @@ -2103,8 +2120,7 @@ static TRBCCode xhci_address_slot(XHCIState *xhci, un= signed int slotid, XHCISlot *slot; USBPort *uport; USBDevice *dev; - dma_addr_t ictx, octx, dcbaap; - uint64_t poctx; + dma_addr_t ictx, octx; uint32_t ictl_ctx[2]; uint32_t slot_ctx[4]; uint32_t ep0_ctx[5]; @@ -2113,10 +2129,10 @@ static TRBCCode xhci_address_slot(XHCIState *xhci, = unsigned int slotid, =20 assert(slotid >=3D 1 && slotid <=3D xhci->numslots); =20 - dcbaap =3D xhci_addr64(xhci->dcbaap_low, xhci->dcbaap_high); - ldq_le_dma(xhci->as, dcbaap + 8 * slotid, &poctx, MEMTXATTRS_UNSPECIFI= ED); + if (!xhci_read_dcbaa(xhci, slotid, &octx)) { + return CC_TRB_ERROR; + } ictx =3D xhci_mask64(pictx); - octx =3D xhci_mask64(poctx); =20 DPRINTF("xhci: input context at "DMA_ADDR_FMT"\n", ictx); DPRINTF("xhci: output context at "DMA_ADDR_FMT"\n", octx); @@ -3522,24 +3538,21 @@ static int usb_xhci_post_load(void *opaque, int ver= sion_id) XHCIState *xhci =3D opaque; XHCISlot *slot; XHCIEPContext *epctx; - dma_addr_t dcbaap, pctx; + dma_addr_t pctx; uint32_t slot_ctx[4]; uint32_t ep_ctx[5]; int slotid, epid, state; - uint64_t addr; - - dcbaap =3D xhci_addr64(xhci->dcbaap_low, xhci->dcbaap_high); =20 for (slotid =3D 1; slotid <=3D xhci->numslots; slotid++) { slot =3D &xhci->slots[slotid-1]; if (!slot->addressed) { continue; } - ldq_le_dma(xhci->as, dcbaap + 8 * slotid, &addr, MEMTXATTRS_UNSPEC= IFIED); - slot->ctx =3D xhci_mask64(addr); - - xhci_dma_read_u32s(xhci, slot->ctx, slot_ctx, sizeof(slot_ctx)); - slot->uport =3D xhci_lookup_uport(xhci, slot_ctx); + slot->uport =3D NULL; + if (xhci_read_dcbaa(xhci, slotid, &slot->ctx)) { + xhci_dma_read_u32s(xhci, slot->ctx, slot_ctx, sizeof(slot_ctx)= ); + slot->uport =3D xhci_lookup_uport(xhci, slot_ctx); + } if (!slot->uport) { /* should not happen, but may trigger on guest bugs */ slot->enabled =3D 0; --=20 2.43.0 From nobody Mon Nov 23 04:26:02 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linux.dev Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 179341792494464.96547352363507; Fri, 30 Oct 2026 20:38:44 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xMzue-0003qs-5W; Fri, 30 Oct 2026 23:37:48 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xMzuc-0003qS-FI for qemu-devel@nongnu.org; Fri, 30 Oct 2026 23:37:46 -0400 Received: from out-8.mta1.migadu.com ([95.215.58.8] helo=mta1.migadu.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1xMzua-0003t3-LY for qemu-devel@nongnu.org; Fri, 30 Oct 2026 23:37:46 -0400 Received: by smtp.migadu.com with ESMTPS id 8d6880e513897306; Sat, 31 Oct 2026 03:37:39 +0000 X-Envelope-To: qemu-devel@nongnu.org DKIM-Signature: a=rsa-sha256; bh=WY5GMGb9Ni7Ofb5lnY/xQmfA7Nde8Y/R+i7N1Ts/OAk=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1793417859; v=1; x=1794022659; b=ppbIu7fMMqrFtVhMrJEAZvKN+vfdZuy1F5GeXd4cdYgBvfh1aRn4zGMXlx6oXXVdvgwbLReE G3Ci+5l2DDWw2LFJiJfH7QbppblMPubbcRv/qbtdSHHh5/PuPiwQRXzSiCFj044icGWasQhX8Ws FmjTSh5UFARmURxlOkgKGwK4= X-Envelope-To: qemu-devel@nongnu.org X-Mizu-Trace-ID: 8d6880e513897306 X-Migadu-Flow: FLOW_OUT From: Junjie Cao To: qemu-devel@nongnu.org Cc: Thomas Huth , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Peter Maydell , Fabiano Rosas , Laurent Vivier , Paolo Bonzini , junjie.cao@linux.dev Subject: [PATCH 2/2] tests/qtest/usb-hcd-xhci: test Address Device with an unreadable DCBAA Date: Fri, 30 Oct 2026 22:37:25 -0500 Message-ID: <20261031033725.1275414-3-junjie.cao@linux.dev> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261031033725.1275414-1-junjie.cao@linux.dev> References: <20261031033725.1275414-1-junjie.cao@linux.dev> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=95.215.58.8; envelope-from=junjie.cao@linux.dev; helo=mta1.migadu.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1793417927676154100 Content-Type: text/plain; charset="utf-8" Point DCBAAP at unassigned memory before Address Device. The fixed controller completes it with TRB Error and raises HCE; check that, and that the 64 bytes at guest address 0, where the contexts used to be written, stay intact. With DCBAAP restored the command then succeeds on retry. Signed-off-by: Junjie Cao --- tests/qtest/usb-hcd-xhci-test.c | 87 ++++++++++++++++++++++++++++----- 1 file changed, 75 insertions(+), 12 deletions(-) diff --git a/tests/qtest/usb-hcd-xhci-test.c b/tests/qtest/usb-hcd-xhci-tes= t.c index add2290bff..be09b838c5 100644 --- a/tests/qtest/usb-hcd-xhci-test.c +++ b/tests/qtest/usb-hcd-xhci-test.c @@ -56,6 +56,7 @@ #define ER_TRANSFER 32 #define ER_COMMAND_COMPLETE 33 #define CC_SUCCESS 1 +#define CC_TRB_ERROR 5 =20 #define EP_TYPE_ISOCH_OUT 1 #define EP_TYPE_CONTROL 4 @@ -64,6 +65,8 @@ =20 #define XHCI_RING_TRBS 64 #define XHCI_MICROFRAME_NS 125000 +/* in the PCI hole of the pc machine, nothing is mapped there */ +#define XHCI_UNASSIGNED_ADDR 0xf0000000 =20 typedef struct XHCITest { QTestState *qts; @@ -71,7 +74,7 @@ typedef struct XHCITest { QPCIBus *bus; struct qhc hc; uint32_t oper, runtime, doorbell; - uint64_t cmd_ring, event_ring, input_ctx; + uint64_t dcbaa, cmd_ring, event_ring, input_ctx; unsigned int cmd_idx, event_idx; unsigned int port, slot; } XHCITest; @@ -226,8 +229,9 @@ static bool xhci_next_event(XHCITest *x, uint32_t *stat= us, uint32_t *control) return true; } =20 -static unsigned int xhci_command(XHCITest *x, uint64_t parameter, - uint32_t control) +/* Submit a command and return its completion code. */ +static unsigned int xhci_submit_command(XHCITest *x, uint64_t parameter, + uint32_t control, unsigned int *sl= ot) { uint32_t status; =20 @@ -238,17 +242,30 @@ static unsigned int xhci_command(XHCITest *x, uint64_= t parameter, =20 g_assert_true(xhci_next_event(x, &status, &control)); g_assert_cmpuint(TRB_GET_TYPE(control), =3D=3D, ER_COMMAND_COMPLETE); - g_assert_cmpuint(TRB_GET_CCODE(status), =3D=3D, CC_SUCCESS); - return TRB_GET_SLOT(control); + if (slot) { + *slot =3D TRB_GET_SLOT(control); + } + return TRB_GET_CCODE(status); +} + +/* Submit a command that has to succeed and return the slot ID. */ +static unsigned int xhci_command(XHCITest *x, uint64_t parameter, + uint32_t control) +{ + unsigned int slot; + + g_assert_cmpuint(xhci_submit_command(x, parameter, control, &slot), = =3D=3D, + CC_SUCCESS); + return slot; } =20 /* - * Start qemu-xhci with one USB device, run the controller and bring the - * device to the Addressed state. + * Start qemu-xhci with one USB device, run the controller, enable a slot + * for the device and fill in the input context for Address Device. */ -static void xhci_test_start(XHCITest *x, const char *usb_device) +static void xhci_test_init(XHCITest *x, const char *usb_device) { - uint64_t dcbaa, erst, ep0_ring; + uint64_t erst, ep0_ring; unsigned int maxports; =20 memset(x, 0, sizeof(*x)); @@ -267,14 +284,14 @@ static void xhci_test_start(XHCITest *x, const char *= usb_device) xhci_writel(x, x->oper + XHCI_USBCMD, USBCMD_HCRST); g_assert_false(xhci_readl(x, x->oper + XHCI_USBCMD) & USBCMD_HCRST); =20 - dcbaa =3D xhci_alloc_page(x); + x->dcbaa =3D xhci_alloc_page(x); erst =3D xhci_alloc_page(x); x->cmd_ring =3D xhci_alloc_page(x); x->event_ring =3D xhci_alloc_page(x); x->input_ctx =3D xhci_alloc_page(x); =20 xhci_writel(x, x->oper + XHCI_CONFIG, 1); - xhci_writeq(x, x->oper + XHCI_DCBAAP, dcbaa); + xhci_writeq(x, x->oper + XHCI_DCBAAP, x->dcbaa); qtest_writeq(x->qts, erst, x->event_ring); qtest_writel(x->qts, erst + 8, XHCI_RING_TRBS); xhci_writel(x, x->runtime + XHCI_ERSTSZ, 1); @@ -296,7 +313,7 @@ static void xhci_test_start(XHCITest *x, const char *us= b_device) } =20 x->slot =3D xhci_command(x, 0, TRB_TYPE(CR_ENABLE_SLOT)); - qtest_writeq(x->qts, dcbaa + 8 * x->slot, xhci_alloc_page(x)); + qtest_writeq(x->qts, x->dcbaa + 8 * x->slot, xhci_alloc_page(x)); =20 /* input control context: add slot and ep0 */ qtest_writel(x->qts, x->input_ctx + 0x04, 0x3); @@ -308,6 +325,12 @@ static void xhci_test_start(XHCITest *x, const char *u= sb_device) qtest_writel(x->qts, x->input_ctx + 0x44, (64 << 16) | (EP_TYPE_CONTROL << 3)); qtest_writeq(x->qts, x->input_ctx + 0x48, ep0_ring | 1); +} + +/* xhci_test_init(), then bring the device to the Addressed state. */ +static void xhci_test_start(XHCITest *x, const char *usb_device) +{ + xhci_test_init(x, usb_device); xhci_command(x, x->input_ctx, TRB_TYPE(CR_ADDRESS_DEVICE) | (x->slot << 24)); } @@ -355,6 +378,44 @@ static bool xhci_test_supported(const char *usb_device) return true; } =20 +/* + * Address Device reads the output context pointer from the DCBAA. Point + * DCBAAP at unassigned memory: the command has to fail instead of carrying + * on with the zero the failed read leaves behind, which used to put the + * contexts at guest address 0 and still report Success. + */ +static void test_xhci_address_device_dcbaa_read_error(void) +{ + uint8_t fill[64], low_mem[sizeof(fill)]; + unsigned int ccode; + XHCITest x; + + if (!xhci_test_supported("usb-kbd")) { + return; + } + + xhci_test_init(&x, "-device usb-kbd"); + memset(fill, 0xaa, sizeof(fill)); + qtest_memwrite(x.qts, 0, fill, sizeof(fill)); + xhci_writeq(&x, x.oper + XHCI_DCBAAP, XHCI_UNASSIGNED_ADDR); + + ccode =3D xhci_submit_command(&x, x.input_ctx, + TRB_TYPE(CR_ADDRESS_DEVICE) | (x.slot << 2= 4), + NULL); + g_assert_cmpuint(ccode, =3D=3D, CC_TRB_ERROR); + g_assert_cmphex(xhci_readl(&x, x.oper + XHCI_USBSTS) & USBSTS_HCE, =3D= =3D, + USBSTS_HCE); + qtest_memread(x.qts, 0, low_mem, sizeof(low_mem)); + g_assert_cmpmem(low_mem, sizeof(low_mem), fill, sizeof(fill)); + + /* The failed command left the slot usable. */ + xhci_writeq(&x, x.oper + XHCI_DCBAAP, x.dcbaa); + xhci_command(&x, x.input_ctx, + TRB_TYPE(CR_ADDRESS_DEVICE) | (x.slot << 24)); + + xhci_test_end(&x); +} + /* * An isoch TD with SIA set is run at the next interval boundary. That has= to * hold once the microframe index no longer fits in 32 bits as well. @@ -481,6 +542,8 @@ int main(int argc, char **argv) if (qtest_has_device("usb-ccid")) { qtest_add_func("/xhci/pci/hotplug/usb-ccid", test_usb_ccid_hotplug= ); } + qtest_add_func("/xhci/pci/address-device/dcbaa-read-error", + test_xhci_address_device_dcbaa_read_error); qtest_add_func("/xhci/pci/isoch/mfindex-32bit", test_xhci_isoch_mfindex_32bit); qtest_add_func("/xhci/pci/intr/mfindex-32bit", --=20 2.43.0