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 1793709276773616.0409006341948; Tue, 3 Nov 2026 04:34:36 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xODiO-0000B7-1v; Tue, 03 Nov 2026 07:34:13 -0500 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 1xODiH-00009c-Ld for qemu-devel@nongnu.org; Tue, 03 Nov 2026 07:34:05 -0500 Received: from out-140.mta1.migadu.com ([95.215.58.140] 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 1xODiF-0005ML-Ow for qemu-devel@nongnu.org; Tue, 03 Nov 2026 07:34:05 -0500 Received: by smtp.migadu.com with ESMTPS id 29a5066799d6f858; Tue, 03 Nov 2026 12:34:01 +0000 X-Envelope-To: qemu-devel@nongnu.org DKIM-Signature: a=rsa-sha256; bh=WYdlRMcPEa7W81bJ23TK/XD9IqWX5ZXNvNIkVH759EI=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1793709241; v=1; x=1794314041; b=sfm3i30ogrJm5zgfwF7BhBAFEATlju1NGXiIAsMxPfQ/+LxxH6cCfXOAqPf62he9pX7FUtye 8kn/FGMj1FVvTAZ/8lkYsgJVOZaCh+rexB4/Q6Rt3qd5/L0qzaq20r8dovNnNmM9HQCVyYVlXbj /mInMoG8YTkE5JBQ7b1migVY= X-Envelope-To: qemu-devel@nongnu.org X-Mizu-Trace-ID: 29a5066799d6f858 X-Migadu-Flow: FLOW_OUT From: Junjie Cao To: qemu-devel@nongnu.org Cc: thuth@redhat.com, jamin_lin@aspeedtech.com, philmd@oss.qualcomm.com, clg@redhat.com, peter.maydell@linaro.org, kraxel@redhat.com, marcandre.lureau@redhat.com, farosas@suse.de, lvivier@redhat.com, pbonzini@redhat.com, junjie.cao@linux.dev Subject: [PATCH 1/2] hw/usb/hcd-ehci: idle the schedule after a failed descriptor read Date: Tue, 3 Nov 2026 06:33:49 -0600 Message-ID: <20261103123350.1253202-2-junjie.cao@linux.dev> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261103123350.1253202-1-junjie.cao@linux.dev> References: <20261103123350.1253202-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.140; 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: 1793709279427158500 Content-Type: text/plain; charset="utf-8" Since get_dwords() started checking the DMA result, a failed descriptor read raises HSE, clears Run/Stop and makes the state function return without advancing. That leaves the schedule in WAITLISTHEAD, FETCHQH, FETCHSITD or FETCHQTD, none of which ehci_advance_async_state() or ehci_advance_periodic_state() accept: the next frame hits their g_assert_not_reached(). A guest gets there by pointing ASYNCLISTADDR, a QH's next qTD or a frame list entry at an address outside guest memory; QEMU aborts on the second frame. Return the schedule to EST_ACTIVE at those four sites, so the next pass sees Run/Stop clear and tears it down the way a stopped schedule is. The iTD and fill-queue reads already take the existing controller-reset path on error and are left alone. Fixes: 2b2a0e4f465a ("hw/usb/hcd-ehci: Check for DMA errors in get_dwords()= /put_dwords()") Signed-off-by: Junjie Cao Reviewed-by: Tommaso Califano --- hw/usb/hcd-ehci.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/hw/usb/hcd-ehci.c b/hw/usb/hcd-ehci.c index f371e567f3..938707043f 100644 --- a/hw/usb/hcd-ehci.c +++ b/hw/usb/hcd-ehci.c @@ -330,6 +330,15 @@ static int ehci_get_state(EHCIState *s, int async) return async ? s->astate : s->pstate; } =20 +/* + * get_dwords() has raised HSE and cleared Run/Stop. Park the schedule + * in EST_ACTIVE so the next advance pass idles it instead of asserting. + */ +static void ehci_abandon_pass(EHCIState *ehci, int async) +{ + ehci_set_state(ehci, async, EST_ACTIVE); +} + static void ehci_set_fetch_addr(EHCIState *s, int async, uint64_t addr) { if (async) { @@ -1600,6 +1609,7 @@ static int ehci_state_waitlisthead(EHCIState *ehci, = int async) for (i =3D 0; i < MAX_QH; i++) { if (!get_dwords(ehci, NLPTR_GET(entry), (uint32_t *) &qh, ehci_qh_dwords(ehci))) { + ehci_abandon_pass(ehci, async); return 0; } ehci_trace_qh(NULL, NLPTR_GET(entry), &qh); @@ -1703,6 +1713,7 @@ static EHCIQueue *ehci_state_fetchqh(EHCIState *ehci,= int async) memset(&qh, 0, sizeof(qh)); if (!get_dwords(ehci, NLPTR_GET(q->qhaddr), (uint32_t *) &qh, ehci_qh_dwords(ehci))) { + ehci_abandon_pass(ehci, async); q =3D NULL; goto out; } @@ -1807,6 +1818,7 @@ static int ehci_state_fetchsitd(EHCIState *ehci, int = async) =20 if (!get_dwords(ehci, NLPTR_GET(entry), (uint32_t *)&sitd, sizeof(EHCIsitd) >> 2)) { + ehci_abandon_pass(ehci, async); return 0; } ehci_trace_sitd(ehci, entry, &sitd); @@ -1867,6 +1879,7 @@ static int ehci_state_fetchqtd(EHCIQueue *q) =20 addr =3D NLPTR_GET(q->qtdaddr); if (!get_dwords(q->ehci, addr + 8, &qtd.token, 1)) { + ehci_abandon_pass(q->ehci, q->async); return 0; } barrier(); @@ -1878,6 +1891,7 @@ static int ehci_state_fetchqtd(EHCIQueue *q) (q->ehci->caps_64bit_addr && !get_dwords(q->ehci, addr + offsetof(EHCIqtd, bufptr_hi), qtd.bufptr_hi, ARRAY_SIZE(qtd.bufptr_hi)))) { + ehci_abandon_pass(q->ehci, q->async); return 0; } ehci_trace_qtd(q, NLPTR_GET(q->qtdaddr), &qtd); --=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 1793709290578583.4873150793943; Tue, 3 Nov 2026 04:34:50 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xODiR-0000Cb-0d; Tue, 03 Nov 2026 07:34:15 -0500 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 1xODiK-0000AQ-UA for qemu-devel@nongnu.org; Tue, 03 Nov 2026 07:34:09 -0500 Received: from out-148.mta1.migadu.com ([95.215.58.148] 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 1xODiJ-0005Mo-3q for qemu-devel@nongnu.org; Tue, 03 Nov 2026 07:34:08 -0500 Received: by smtp.migadu.com with ESMTPS id 03580630d6fb2a36; Tue, 03 Nov 2026 12:34:05 +0000 X-Envelope-To: qemu-devel@nongnu.org DKIM-Signature: a=rsa-sha256; bh=K1FxMyuJTEOJcNaBNIhk1yFfIKHt5f4bGQT6c7TTMl8=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1793709245; v=1; x=1794314045; b=iwBBBKkXjB1rmwOwFdFS/AuMjwRkD6mOdr63y5+iU/DgqxWUhWYQb5CM/ItkU5+gvKckpP7R k7nvrPE4QEf+PwNFe9RHrQ1G3hNFcQbu2PhoehPfWDPv0pkGlTgiNphQ2Dkd/pn6R8qheYdfcUA XnG1Mxutegl0FyemLBJJAgNo= X-Envelope-To: qemu-devel@nongnu.org X-Mizu-Trace-ID: 03580630d6fb2a36 X-Migadu-Flow: FLOW_OUT From: Junjie Cao To: qemu-devel@nongnu.org Cc: thuth@redhat.com, jamin_lin@aspeedtech.com, philmd@oss.qualcomm.com, clg@redhat.com, peter.maydell@linaro.org, kraxel@redhat.com, marcandre.lureau@redhat.com, farosas@suse.de, lvivier@redhat.com, pbonzini@redhat.com, junjie.cao@linux.dev Subject: [PATCH 2/2] tests/qtest/usb-hcd-ehci: test descriptor reads that fail Date: Tue, 3 Nov 2026 06:33:50 -0600 Message-ID: <20261103123350.1253202-3-junjie.cao@linux.dev> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261103123350.1253202-1-junjie.cao@linux.dev> References: <20261103123350.1253202-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.148; 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: 1793709291641158500 Content-Type: text/plain; charset="utf-8" Run the async schedule into an unreadable list head, into a head QH whose horizontal link is unreadable and into a QH whose next qTD is unreadable, and the periodic schedule into an unreadable siTD. Each must raise HSE, clear Run/Stop and leave the controller halted with both schedules idle on the following frames; before the previous patch QEMU aborted there. A reset afterwards must bring the controller back. Signed-off-by: Junjie Cao Reviewed-by: Tommaso Califano --- tests/qtest/usb-hcd-ehci-test.c | 119 ++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) diff --git a/tests/qtest/usb-hcd-ehci-test.c b/tests/qtest/usb-hcd-ehci-tes= t.c index 87e37cdd7c..b0a0b6df9b 100644 --- a/tests/qtest/usb-hcd-ehci-test.c +++ b/tests/qtest/usb-hcd-ehci-test.c @@ -12,6 +12,7 @@ #include "libqos/pci-pc.h" #include "hw/usb/uhci-regs.h" #include "hw/usb/ehci-regs.h" +#include "hw/usb/hcd-ehci.h" #include "libqos/usb.h" =20 static QPCIBus *pcibus; @@ -142,6 +143,123 @@ static void pci_ehci_port_hotplug(void) usb_test_hotplug(global_qtest, "ich9-ehci-1", "3", pci_ehci_port_3_hot= plug); } =20 +/* operational registers, relative to the BAR */ +#define EHCI_USBCMD 0x20 +#define EHCI_USBSTS 0x24 +#define EHCI_PERIODICLISTBASE 0x34 +#define EHCI_ASYNCLISTADDR 0x38 + +#define NLPTR_TYPE_QH (1 << 1) +#define NLPTR_TYPE_SITD (2 << 1) +#define NLPTR_T 1 + +#define QH_ADDR 0x100000 /* in the default 128 MiB of R= AM */ +#define FRAME_LIST_ADDR 0x101000 +#define BAD_ADDR 0x40000000 /* above RAM, nothing mapped */ +#define FRAME_NS 1000000 + +static uint32_t ehci_readl(uint32_t off) +{ + return qpci_io_readl(ehci1.dev, ehci1.bar, off); +} + +static void ehci_writel(uint32_t off, uint32_t val) +{ + qpci_io_writel(ehci1.dev, ehci1.bar, off, val); +} + +static void ehci_hcreset(void) +{ + ehci_writel(EHCI_USBCMD, USBCMD_HCRESET); + g_assert_cmphex(ehci_readl(EHCI_USBCMD) & USBCMD_HCRESET, =3D=3D, 0); + g_assert_cmphex(ehci_readl(EHCI_USBSTS) & USBSTS_HALT, =3D=3D, USBSTS_= HALT); +} + +static void ehci_write_qh(uint32_t next, uint32_t next_qtd) +{ + uint32_t qh[16] =3D { + next, + QH_EPCHAR_H | (64 << QH_EPCHAR_MPLEN_SH) | + (EHCI_QH_EPS_HIGH << QH_EPCHAR_EPS_SH), + 1 << QH_EPCAP_MULT_SH, + 0, /* current qTD */ + next_qtd, + NLPTR_T, /* alternate next qTD */ + 0, /* token: inactive */ + }; + + memwrite(QH_ADDR, qh, sizeof(qh)); +} + +/* Run a schedule into a descriptor that cannot be read. */ +static void ehci_run_into_dma_error(uint32_t usbcmd) +{ + uint32_t sts; + int i; + + ehci_writel(EHCI_USBCMD, usbcmd); + for (i =3D 0; i < 16; i++) { + clock_step(FRAME_NS); + if (ehci_readl(EHCI_USBSTS) & USBSTS_HSE) { + break; + } + } + g_assert_cmphex(ehci_readl(EHCI_USBSTS) & USBSTS_HSE, =3D=3D, USBSTS_H= SE); + g_assert_cmphex(ehci_readl(EHCI_USBCMD) & USBCMD_RUNSTOP, =3D=3D, 0); + + for (i =3D 0; i < 4; i++) { + clock_step(FRAME_NS); + } + sts =3D ehci_readl(EHCI_USBSTS); + g_assert_cmphex(sts & USBSTS_HALT, =3D=3D, USBSTS_HALT); + g_assert_cmphex(sts & (USBSTS_ASS | USBSTS_PSS), =3D=3D, 0); + + /* a reset must bring the controller back */ + ehci_hcreset(); + ehci_write_qh(QH_ADDR | NLPTR_TYPE_QH, NLPTR_T); + ehci_writel(EHCI_ASYNCLISTADDR, QH_ADDR); + ehci_writel(EHCI_USBCMD, USBCMD_RUNSTOP | USBCMD_ASE); + for (i =3D 0; i < 4; i++) { + clock_step(FRAME_NS); + } + g_assert_cmphex(ehci_readl(EHCI_USBSTS) & (USBSTS_HSE | USBSTS_HALT), + =3D=3D, 0); + g_assert_cmphex(ehci_readl(EHCI_USBCMD) & USBCMD_RUNSTOP, =3D=3D, + USBCMD_RUNSTOP); + ehci_hcreset(); +} + +static void pci_ehci_dma_error(void) +{ + uint32_t entries[32]; + int i; + + g_assert(pcibus !=3D NULL); + ehci_hcreset(); + + /* async schedule: the list head itself is unreadable */ + ehci_writel(EHCI_ASYNCLISTADDR, BAD_ADDR); + ehci_run_into_dma_error(USBCMD_RUNSTOP | USBCMD_ASE); + + /* async schedule: the head QH links to an unreadable QH */ + ehci_write_qh(BAD_ADDR | NLPTR_TYPE_QH, NLPTR_T); + ehci_writel(EHCI_ASYNCLISTADDR, QH_ADDR); + ehci_run_into_dma_error(USBCMD_RUNSTOP | USBCMD_ASE); + + /* async schedule: a readable QH points at an unreadable qTD */ + ehci_write_qh(QH_ADDR | NLPTR_TYPE_QH, BAD_ADDR); + ehci_writel(EHCI_ASYNCLISTADDR, QH_ADDR); + ehci_run_into_dma_error(USBCMD_RUNSTOP | USBCMD_ASE); + + /* periodic schedule: the frame list points at an unreadable siTD */ + for (i =3D 0; i < ARRAY_SIZE(entries); i++) { + entries[i] =3D BAD_ADDR | NLPTR_TYPE_SITD; + } + memwrite(FRAME_LIST_ADDR, entries, sizeof(entries)); + ehci_writel(EHCI_PERIODICLISTBASE, FRAME_LIST_ADDR); + ehci_run_into_dma_error(USBCMD_RUNSTOP | USBCMD_PSE); +} + =20 int main(int argc, char **argv) { @@ -160,6 +278,7 @@ int main(int argc, char **argv) qtest_add_func("/ehci/pci/uhci-port-2", pci_uhci_port_2); qtest_add_func("/ehci/pci/ehci-port-2", pci_ehci_port_2); qtest_add_func("/ehci/pci/ehci-port-3-hotplug", pci_ehci_port_hotplug); + qtest_add_func("/ehci/pci/ehci-dma-error", pci_ehci_dma_error); =20 qtest_start("-machine q35 -device ich9-usb-ehci1,bus=3Dpcie.0,addr=3D1= d.7," "multifunction=3Don,id=3Dich9-ehci-1 " --=20 2.43.0