From nobody Tue Feb 10 02:51:05 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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=pass(p=none dis=none) header.from=yandex-team.ru ARC-Seal: i=1; a=rsa-sha256; t=1658218175; cv=none; d=zohomail.com; s=zohoarc; b=VokKJu8h553bSFlPyC3Y7oRpqpxiu1zdFDTajkCaIBesVndUfdDdJV1lCf0g4W63fzAgPuqvLio2hcg1bk5AB1arbmSkFUvnVRIzGhWdfDsX9qy915gqFYkTic6BzxyrMU90EzP8os+MmKXSeaOPgYPfF3AU0p+2om9N/y+XWLk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1658218175; h=Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=NPOrgVUqlkrD0TTtyERGpMyANZPGvgzu/8Ha0spcK+c=; b=iTcgw/2LKOPa6FlHSjLIgUB0XF5N394Ce9oJKmDAzGe6J5Pc4JKVOlTdeUTNkuHpy/1s20tOBPN7riaIYVb/l71yvhhtb0TvZkc5pNZBlOqEzZv6qmOe38UK7Dqb6Lfs4dIOaQ2kIOX89F4xfaQ1H/wF4X7H0muzrLnnJ9cZdCM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1658218174918517.206729702329; Tue, 19 Jul 2022 01:09:34 -0700 (PDT) Received: from localhost ([::1]:45036 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oDiIO-0001oK-1t for importer@patchew.org; Tue, 19 Jul 2022 04:09:32 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:53018) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oDiAr-0002Fy-1Z for qemu-devel@nongnu.org; Tue, 19 Jul 2022 04:01:45 -0400 Received: from forwardcorp1p.mail.yandex.net ([77.88.29.217]:49020) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oDiAm-0003UW-K5 for qemu-devel@nongnu.org; Tue, 19 Jul 2022 04:01:43 -0400 Received: from vla5-d6d5ce7a4718.qloud-c.yandex.net (vla5-d6d5ce7a4718.qloud-c.yandex.net [IPv6:2a02:6b8:c18:341e:0:640:d6d5:ce7a]) by forwardcorp1p.mail.yandex.net (Yandex) with ESMTP id 99FEF2E0D75; Tue, 19 Jul 2022 11:01:25 +0300 (MSK) Received: from rvkaganb.yandex-team.ru (unknown [2a02:6b8:b081:8012::1:1c]) by vla5-d6d5ce7a4718.qloud-c.yandex.net (smtpcorp/Yandex) with ESMTPSA id jTrgVYP7rQ-1OOuh7MV; Tue, 19 Jul 2022 08:01:24 +0000 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (Client certificate not present) Precedence: bulk X-Yandex-Fwd: 1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex-team.ru; s=default; t=1658217684; bh=NPOrgVUqlkrD0TTtyERGpMyANZPGvgzu/8Ha0spcK+c=; h=Message-Id:Date:Cc:Subject:To:From; b=FvjATYlMkdms+THUK4MDwsqRItNpaDMl8ea1xOTqs9eVfhECCUBjzTEZCzBHSOnu5 Yq+GfPyPhl3GZ8gPAChJSSnruQpeHujjREP8Zt0yxvtZdwZIQ3KaUXpSGbi8hrnE4o 00cNM5yRN1DNZVFnEbVtJ7lgW9jn6Jo+4gQPURx8= Authentication-Results: vla5-d6d5ce7a4718.qloud-c.yandex.net; dkim=pass header.i=@yandex-team.ru From: Roman Kagan To: qemu-devel@nongnu.org Cc: Paolo Bonzini , Marcel Apfelbaum , "Michael S. Tsirkin" , Laurent Vivier , yc-core@yandex-team.ru, Thomas Huth Subject: [PATCH v2] hw/pci/pci_bridge: ensure PCIe slots have only one slot Date: Tue, 19 Jul 2022 11:01:23 +0300 Message-Id: <20220719080123.553981-1-rvkagan@yandex-team.ru> X-Mailer: git-send-email 2.36.1 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=lists.gnu.org; Received-SPF: pass client-ip=77.88.29.217; envelope-from=rvkagan@yandex-team.ru; helo=forwardcorp1p.mail.yandex.net X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 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, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @yandex-team.ru) X-ZM-MESSAGEID: 1658218177834100001 Content-Type: text/plain; charset="utf-8" It's possible to create non-working configurations by attaching a device to a derivative of PCIe slot (pcie-root-port, ioh3420, etc) and specifying a slot number other that zero, e.g.: -device pcie-root-port,id=3Ds0,... \ -device virtio-blk-pci,bus=3Ds0,addr=3D4,... Make QEMU reject such configurations and only allow addr=3D0 on the secondary bus of a PCIe slot. To verify this new behavior, add two basic qtests for the PCIe bridges that may be affected by change: pcie-root-port and x3130. For the former, two testcases are included, one positive for slot #0 and one negative for (arbitrary) slot #4; for the latter, only a positive testcase for slot #4 is included. Signed-off-by: Roman Kagan --- v1 -> v2: - use object_dynamic_cast (without assert) [Vladimir] - add explaining comment [Michael] - add tests (I've only had a chance to run tests against x86; hope I didn't mess them up for other arches) hw/pci/pci_bridge.c | 6 +++ tests/qtest/pcie-root-port-test.c | 77 +++++++++++++++++++++++++++++++ tests/qtest/xio3130-test.c | 54 ++++++++++++++++++++++ tests/qtest/meson.build | 2 + 4 files changed, 139 insertions(+) create mode 100644 tests/qtest/pcie-root-port-test.c create mode 100644 tests/qtest/xio3130-test.c diff --git a/hw/pci/pci_bridge.c b/hw/pci/pci_bridge.c index da34c8ebcd..23e1701d06 100644 --- a/hw/pci/pci_bridge.c +++ b/hw/pci/pci_bridge.c @@ -33,6 +33,7 @@ #include "qemu/units.h" #include "hw/pci/pci_bridge.h" #include "hw/pci/pci_bus.h" +#include "hw/pci/pcie_port.h" #include "qemu/module.h" #include "qemu/range.h" #include "qapi/error.h" @@ -386,6 +387,11 @@ void pci_bridge_initfn(PCIDevice *dev, const char *typ= ename) br->windows =3D pci_bridge_region_init(br); QLIST_INIT(&sec_bus->child); QLIST_INSERT_HEAD(&parent->child, sec_bus, sibling); + + /* PCIe slot derivatives are bridges with a single slot; enforce that = */ + if (object_dynamic_cast(OBJECT(dev), TYPE_PCIE_SLOT)) { + sec_bus->slot_reserved_mask =3D ~1u; + } } =20 /* default qdev clean up function for PCI-to-PCI bridge */ diff --git a/tests/qtest/pcie-root-port-test.c b/tests/qtest/pcie-root-port= -test.c new file mode 100644 index 0000000000..a1f3d84d75 --- /dev/null +++ b/tests/qtest/pcie-root-port-test.c @@ -0,0 +1,77 @@ +/* + * QTest testcase for generic PCIe root port + * + * Copyright (c) 2022 Yandex N.V. + * + * This work is licensed under the terms of the GNU GPL, version 2 or late= r. + * See the COPYING file in the top-level directory. + */ + +#include "qemu/osdep.h" +#include "libqtest-single.h" + +/* + * Let QEMU choose the bus and slot for the device under test. It may eve= n be + * a non-PCIe bus but it's ok for the purpose of the test. + */ +static const char *common_args =3D "-device pcie-root-port,id=3Ds0" + ",port=3D1,chassis=3D1,multifunction=3Don= "; + +static void test_slot0(void) +{ + QTestState *qts; + QDict *resp; + + /* attach a PCIe device into slot0 of the root port */ + qts =3D qtest_init(common_args); + /* PCIe root port is known to be supported, use it as a leaf device to= o */ + resp =3D qtest_qmp(qts, "{'execute': 'device_add', 'arguments': {" + "'driver': 'pcie-root-port', " + "'id': 'port1', " + "'bus': 's0', " + "'chassis': 5, " + "'addr': '0'" + "} }"); + g_assert_nonnull(resp); + g_assert(!qdict_haskey(resp, "event")); + g_assert(!qdict_haskey(resp, "error")); + qobject_unref(resp); + + qtest_quit(qts); +} + +static void test_slot4(void) +{ + QTestState *qts; + QDict *resp; + + /* attach a PCIe device into slot4 of the root port should be rejected= */ + qts =3D qtest_init(common_args); + /* PCIe root port is known to be supported, use it as a leaf device to= o */ + resp =3D qtest_qmp(qts, "{'execute': 'device_add', 'arguments': {" + "'driver': 'pcie-root-port', " + "'id': 'port1', " + "'bus': 's0', " + "'chassis': 5, " + "'addr': '4'" + "} }"); + qmp_expect_error_and_unref(resp, "GenericError"); + + qtest_quit(qts); +} + +int main(int argc, char **argv) +{ + int ret; + + g_test_init(&argc, &argv, NULL); + + qtest_add_func("/pcie-root-port/slot0", test_slot0); + qtest_add_func("/pcie-root-port/slot4", test_slot4); + + ret =3D g_test_run(); + + qtest_end(); + + return ret; +} diff --git a/tests/qtest/xio3130-test.c b/tests/qtest/xio3130-test.c new file mode 100644 index 0000000000..3a937889b9 --- /dev/null +++ b/tests/qtest/xio3130-test.c @@ -0,0 +1,54 @@ +/* + * QTest testcase for TI X3130 PCIe switch + * + * Copyright (c) 2022 Yandex N.V. + * + * This work is licensed under the terms of the GNU GPL, version 2 or late= r. + * See the COPYING file in the top-level directory. + */ + +#include "qemu/osdep.h" +#include "libqtest-single.h" + +/* + * Let QEMU choose the bus and slot for the device under test. It may eve= n be + * a non-PCIe bus but it's ok for the purpose of the test. + */ +static const char *common_args =3D "-device x3130-upstream,id=3Ds0"; + +static void test_slot4(void) +{ + QTestState *qts; + QDict *resp; + + /* attach a downstream port into slot4 of the upstream port */ + qts =3D qtest_init(common_args); + resp =3D qtest_qmp(qts, "{'execute': 'device_add', 'arguments': {" + "'driver': 'xio3130-downstream', " + "'id': 'port1', " + "'bus': 's0', " + "'chassis': 5, " + "'addr': '4'" + "} }"); + g_assert_nonnull(resp); + g_assert(!qdict_haskey(resp, "event")); + g_assert(!qdict_haskey(resp, "error")); + qobject_unref(resp); + + qtest_quit(qts); +} + +int main(int argc, char **argv) +{ + int ret; + + g_test_init(&argc, &argv, NULL); + + qtest_add_func("/x3130/slot4", test_slot4); + + ret =3D g_test_run(); + + qtest_end(); + + return ret; +} diff --git a/tests/qtest/meson.build b/tests/qtest/meson.build index 31287a9173..19cab1bc35 100644 --- a/tests/qtest/meson.build +++ b/tests/qtest/meson.build @@ -54,6 +54,7 @@ qtests_i386 =3D \ (config_all_devices.has_key('CONFIG_I82801B11') ? ['i82801b11-test'] : [= ]) + \ (config_all_devices.has_key('CONFIG_IOH3420') ? ['ioh3420-test'] : []) += \ (config_all_devices.has_key('CONFIG_LPC_ICH9') ? ['lpc-ich9-test'] : [])= + \ + (config_all_devices.has_key('CONFIG_PCIE_PORT') ? ['pcie-root-port-test'= ] : []) + \ (config_all_devices.has_key('CONFIG_USB_UHCI') ? ['usb-hcd-uhci-test'] := []) + \ (config_all_devices.has_key('CONFIG_USB_UHCI') and = \ config_all_devices.has_key('CONFIG_USB_EHCI') ? ['usb-hcd-ehci-test'] := []) + \ @@ -63,6 +64,7 @@ qtests_i386 =3D \ (config_all_devices.has_key('CONFIG_TPM_TIS_ISA') ? ['tpm-tis-test'] : [= ]) + \ (config_all_devices.has_key('CONFIG_TPM_TIS_ISA') ? ['tpm-tis-swtpm-test= '] : []) + \ (config_all_devices.has_key('CONFIG_RTL8139_PCI') ? ['rtl8139-test'] : [= ]) + \ + (config_all_devices.has_key('CONFIG_XIO3130') ? ['xio3130-test'] : []) += \ (config_all_devices.has_key('CONFIG_E1000E_PCI_EXPRESS') ? ['fuzz-e1000e= -test'] : []) + \ (config_all_devices.has_key('CONFIG_MEGASAS_SCSI_PCI') ? ['fuzz-megasas-= test'] : []) + \ (config_all_devices.has_key('CONFIG_LSI_SCSI_PCI') ? ['fuzz-lsi53c895a-t= est'] : []) + \ --=20 2.36.1