From patchwork Mon Feb 19 18:16:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Marek_Marczykowski-G=C3=B3recki?= X-Patchwork-Id: 13562972 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 201C8C54764 for ; Mon, 19 Feb 2024 18:17:16 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.683045.1062365 (Exim 4.92) (envelope-from ) id 1rc8CJ-0001ry-TP; Mon, 19 Feb 2024 18:16:59 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 683045.1062365; Mon, 19 Feb 2024 18:16:59 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1rc8CJ-0001rr-Qn; Mon, 19 Feb 2024 18:16:59 +0000 Received: by outflank-mailman (input) for mailman id 683045; Mon, 19 Feb 2024 18:16:58 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1rc8CI-0001rg-Gu for xen-devel@lists.xenproject.org; Mon, 19 Feb 2024 18:16:58 +0000 Received: from wout5-smtp.messagingengine.com (wout5-smtp.messagingengine.com [64.147.123.21]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 0f53f2dc-cf53-11ee-8a52-1f161083a0e0; Mon, 19 Feb 2024 19:16:55 +0100 (CET) Received: from compute6.internal (compute6.nyi.internal [10.202.2.47]) by mailout.west.internal (Postfix) with ESMTP id 722FE3200922; Mon, 19 Feb 2024 13:16:52 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Mon, 19 Feb 2024 13:16:53 -0500 Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 19 Feb 2024 13:16:50 -0500 (EST) X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 0f53f2dc-cf53-11ee-8a52-1f161083a0e0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= invisiblethingslab.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:from:from:in-reply-to :message-id:mime-version:reply-to:subject:subject:to:to; s=fm3; t=1708366611; x=1708453011; bh=Uo8NGN15Y+wR+Mg2EFtNRg5h410lOfUh X9FiMUkUpNM=; b=ePuwMlpkFCfcozI+MhZqTBm/Dg8ocqkEHLyQsWztZqNGpe9s nCqPrTjcFqSoUCgm04bnW1Yymew4DGgov8d55FlgtUzGeq5mFqkbCDDdPb74gbRb 8181nh6MIynBD3h22iGeiLurm0BK3Xcb/FMpihuMHPK2n52+7z4J0t7c98jcLFch wqcQDmy/iUeSLMYlPzTc1EtYqrrfflSGCg+UZgLG5GdUPO9badV6jAKGmfTCa7iX KCsn/kiovcGBl2CXn2PHaEDDynOt/ncBEThP+jYpdzujAkLb4AqhG+sVC887CwWl o2XERoGrbgTVPWr6Dte/6tYGLYUCGZFw2fM+9w== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:message-id:mime-version:reply-to:subject :subject:to:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm1; t=1708366611; x=1708453011; bh=Uo8NGN15Y+wR+ Mg2EFtNRg5h410lOfUhX9FiMUkUpNM=; b=NwiBHl2JlPG4RfBLrLc3Di0dZEGVk 1sxrKKmwlUQgPG5xaTShd7X6H56R1Ug/GnCCGIZU1P3qBwgQrb3IP1eEchGYJ5c8 WqeXs9YER/iDyHtzeySPS/sZsXeEwJl1p0sL5z7Qg6tMk9QoWnO1Rovw6reHREUz r5/WwlnAPPVlNWqlVCT05NuUaz1FKPbhaLFaOjhJSOhkqVddHZlrUB1hYRzc/qk2 Oc2EAiiC46G+AQ1WZRQ+dzeJgfAjr1C7gk+8AdJTjg0rwVNqyBqCDoDm7hoDKrw0 yo5oRXjabMXLNexRNy8Lh3dkxT9NadIWrz5jse8jAGD+gmTFCOIdvnf0Q== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrvdekgddutdelucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhephffvvefufffkofggtgfgsehtkeertdertdejnecuhfhrohhmpeforghrvghk ucforghrtgiihihkohifshhkihdqifpkrhgvtghkihcuoehmrghrmhgrrhgvkhesihhnvh hishhisghlvghthhhinhhgshhlrggsrdgtohhmqeenucggtffrrghtthgvrhhnpeelkefh udelteelleelteetveeffeetffekteetjeehlefggeekleeghefhtdehvdenucevlhhush htvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehmrghrmhgrrhgvkhes ihhnvhhishhisghlvghthhhinhhgshhlrggsrdgtohhm X-ME-Proxy: Feedback-ID: i1568416f:Fastmail From: =?utf-8?q?Marek_Marczykowski-G=C3=B3recki?= To: qemu-devel@nongnu.org Cc: =?utf-8?q?Marek_Marczykowski-G=C3=B3recki?= , Stefano Stabellini , Anthony Perard , Paul Durrant , xen-devel@lists.xenproject.org (open list:X86 Xen CPUs) Subject: [PATCH 1/2] hw/xen: detect when running inside stubdomain Date: Mon, 19 Feb 2024 19:16:05 +0100 Message-ID: <20240219181627.282097-1-marmarek@invisiblethingslab.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 Introduce global xen_is_stubdomain variable when qemu is running inside a stubdomain instead of dom0. This will be relevant for subsequent patches, as few things like accessing PCI config space need to be done differently. Signed-off-by: Marek Marczykowski-Górecki Reviewed-by: Jason Andryuk --- hw/xen/xen-legacy-backend.c | 15 +++++++++++++++ include/hw/xen/xen.h | 1 + system/globals.c | 1 + 3 files changed, 17 insertions(+) diff --git a/hw/xen/xen-legacy-backend.c b/hw/xen/xen-legacy-backend.c index 124dd5f3d6..4a09ea2561 100644 --- a/hw/xen/xen-legacy-backend.c +++ b/hw/xen/xen-legacy-backend.c @@ -603,6 +603,19 @@ static void xen_set_dynamic_sysbus(void) machine_class_allow_dynamic_sysbus_dev(mc, TYPE_XENSYSDEV); } +static bool xen_check_stubdomain(void) +{ + char *dm_path = g_strdup_printf("/local/domain/%d/image", xen_domid); + uint32_t dm_domid; + bool is_stubdom = false; + + if (!xenstore_read_int(dm_path, "device-model-domid", &dm_domid)) + is_stubdom = dm_domid != 0; + + g_free(dm_path); + return is_stubdom; +} + void xen_be_init(void) { xenstore = qemu_xen_xs_open(); @@ -616,6 +629,8 @@ void xen_be_init(void) exit(1); } + xen_is_stubdomain = xen_check_stubdomain(); + xen_sysdev = qdev_new(TYPE_XENSYSDEV); sysbus_realize_and_unref(SYS_BUS_DEVICE(xen_sysdev), &error_fatal); xen_sysbus = qbus_new(TYPE_XENSYSBUS, xen_sysdev, "xen-sysbus"); diff --git a/include/hw/xen/xen.h b/include/hw/xen/xen.h index 37ecc91fc3..ecb89ecfc1 100644 --- a/include/hw/xen/xen.h +++ b/include/hw/xen/xen.h @@ -36,6 +36,7 @@ enum xen_mode { extern uint32_t xen_domid; extern enum xen_mode xen_mode; extern bool xen_domid_restrict; +extern bool xen_is_stubdomain; int xen_pci_slot_get_pirq(PCIDevice *pci_dev, int irq_num); int xen_set_pci_link_route(uint8_t link, uint8_t irq); diff --git a/system/globals.c b/system/globals.c index b6d4e72530..ac27d88bd4 100644 --- a/system/globals.c +++ b/system/globals.c @@ -62,6 +62,7 @@ bool qemu_uuid_set; uint32_t xen_domid; enum xen_mode xen_mode = XEN_DISABLED; bool xen_domid_restrict; +bool xen_is_stubdomain; struct evtchn_backend_ops *xen_evtchn_ops; struct gnttab_backend_ops *xen_gnttab_ops; struct foreignmem_backend_ops *xen_foreignmem_ops; From patchwork Mon Feb 19 18:16:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Marek_Marczykowski-G=C3=B3recki?= X-Patchwork-Id: 13562973 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id D11BCC48BC3 for ; Mon, 19 Feb 2024 18:17:15 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.683046.1062372 (Exim 4.92) (envelope-from ) id 1rc8CK-0001tt-6a; Mon, 19 Feb 2024 18:17:00 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 683046.1062372; Mon, 19 Feb 2024 18:17:00 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1rc8CK-0001tR-1H; Mon, 19 Feb 2024 18:17:00 +0000 Received: by outflank-mailman (input) for mailman id 683046; Mon, 19 Feb 2024 18:16:59 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1rc8CJ-0001rg-5p for xen-devel@lists.xenproject.org; Mon, 19 Feb 2024 18:16:59 +0000 Received: from wfout5-smtp.messagingengine.com (wfout5-smtp.messagingengine.com [64.147.123.148]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 10a6b543-cf53-11ee-8a52-1f161083a0e0; Mon, 19 Feb 2024 19:16:58 +0100 (CET) Received: from compute2.internal (compute2.nyi.internal [10.202.2.46]) by mailfout.west.internal (Postfix) with ESMTP id 55BAA1C000A1; Mon, 19 Feb 2024 13:16:55 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute2.internal (MEProxy); Mon, 19 Feb 2024 13:16:55 -0500 Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 19 Feb 2024 13:16:53 -0500 (EST) X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 10a6b543-cf53-11ee-8a52-1f161083a0e0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= invisiblethingslab.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm3; t=1708366614; x=1708453014; bh=RDKr5KOEyU 1P8HpgXnlUzz0XOtyo12r8pUxs5poAuEI=; b=zuCssHKJsXrqqdpwdr9qMqSihV HJCmLV+5lv6v6kePzbI2GZYLyzLs/4OT0rct2ONgqs1JGp8XO7QKisx+9aHaYG28 N33bWum9KxSS6LQmDbDDW/TZSMyO/CT427ViZVzal5xRNnZyP8z4hNXI0g2soJ1w RsyR1nSHBetSEXP6Xoas6XWuw+4rsEp4BJm1q9Nv/TaLQ2CbqLI8A5uDAdClTpSZ NSlRZH4G6lKdJk9RP2Eloi1rA6K+2aUHaSmidPheFD/mLuvaG5cuSze2tGSRZa2n pvDg/Ja9Dr+q62N/D168YGLYAPb4w7uigVx7u9J+jix1AvwElqheC5mhIEIA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t=1708366614; x= 1708453014; bh=RDKr5KOEyU1P8HpgXnlUzz0XOtyo12r8pUxs5poAuEI=; b=h gMR+6RhKHwa2e2m2gYM0oq0AWmtRxJLoYYcSrU73SNDH5uzaqT/IVAwFUnt7uuKm qAoZhQeg0BLjQJtTn3YLiTdxtrydSwN/nvY8F/wkS2/8YZUlryaam4gGrsLHE0bY TxMLTGH4wK8p5sxshGNr/yfB0pgNidxLujfYH1ZmCxBV2IrGe7C49uQQMAP5rC/T ujd5r6+avwgnlw677EYDZOJXOH+SWHAeLNic4egCQMdNh+7+nSn59GgKIUB+dzA2 EUdiD3bT3Al/shDZ2oP93iS9U3JPSHjWvESHwnQ966lEJKH7XKGWzK6cUlEZ5Zv+ bKC7oeF6YQQWEnmabGsHA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrvdekgddutdelucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhephffvvefufffkofgjfhggtgfgsehtkeertdertdejnecuhfhrohhmpeforghr vghkucforghrtgiihihkohifshhkihdqifpkrhgvtghkihcuoehmrghrmhgrrhgvkhesih hnvhhishhisghlvghthhhinhhgshhlrggsrdgtohhmqeenucggtffrrghtthgvrhhnpefg ueduhefgvdefheehudejheefudevueeghfekhfehleegveduteeuiedugffgffenucevlh hushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehmrghrmhgrrhgv khesihhnvhhishhisghlvghthhhinhhgshhlrggsrdgtohhm X-ME-Proxy: Feedback-ID: i1568416f:Fastmail From: =?utf-8?q?Marek_Marczykowski-G=C3=B3recki?= To: qemu-devel@nongnu.org Cc: =?utf-8?q?Fr=C3=A9d=C3=A9ric_Pierret_=28fepitre=29?= , =?utf-8?q?Marek_Marczykowski-G=C3=B3recki?= , Stefano Stabellini , Anthony Perard , Paul Durrant , xen-devel@lists.xenproject.org (open list:X86 Xen CPUs) Subject: [PATCH 2/2] xen: fix stubdom PCI addr Date: Mon, 19 Feb 2024 19:16:06 +0100 Message-ID: <20240219181627.282097-2-marmarek@invisiblethingslab.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240219181627.282097-1-marmarek@invisiblethingslab.com> References: <20240219181627.282097-1-marmarek@invisiblethingslab.com> MIME-Version: 1.0 From: Frédéric Pierret (fepitre) When running in a stubdomain, the config space access via sysfs needs to use BDF as seen inside stubdomain (connected via xen-pcifront), which is different from the real BDF. For other purposes (hypercall parameters etc), the real BDF needs to be used. Get the in-stubdomain BDF by looking up relevant PV PCI xenstore entries. Signed-off-by: Marek Marczykowski-Górecki --- hw/xen/xen-host-pci-device.c | 77 +++++++++++++++++++++++++++++++++++- hw/xen/xen-host-pci-device.h | 6 +++ 2 files changed, 82 insertions(+), 1 deletion(-) diff --git a/hw/xen/xen-host-pci-device.c b/hw/xen/xen-host-pci-device.c index 8c6e9a1716..3f8a6f84a8 100644 --- a/hw/xen/xen-host-pci-device.c +++ b/hw/xen/xen-host-pci-device.c @@ -9,6 +9,7 @@ #include "qemu/osdep.h" #include "qapi/error.h" #include "qemu/cutils.h" +#include "hw/xen/xen-legacy-backend.h" #include "xen-host-pci-device.h" #define XEN_HOST_PCI_MAX_EXT_CAP \ @@ -33,13 +34,76 @@ #define IORESOURCE_PREFETCH 0x00001000 /* No side effects */ #define IORESOURCE_MEM_64 0x00100000 +static void xen_host_pci_fill_local_addr(XenHostPCIDevice *d, Error **errp) +{ + unsigned int num_devs, len, i; + unsigned int domain, bus, dev, func; + char *be_path = NULL; + char path[80]; + char *msg = NULL; + + be_path = qemu_xen_xs_read(xenstore, 0, "device/pci/0/backend", &len); + if (!be_path) + goto err_out; + snprintf(path, sizeof(path), "%s/num_devs", be_path); + msg = qemu_xen_xs_read(xenstore, 0, path, &len); + if (!msg) + goto err_out; + + if (sscanf(msg, "%u", &num_devs) != 1) { + error_setg(errp, "Failed to parse %s (%s)\n", msg, path); + goto err_out; + } + free(msg); + + for (i = 0; i < num_devs; i++) { + snprintf(path, sizeof(path), "%s/dev-%u", be_path, i); + msg = qemu_xen_xs_read(xenstore, 0, path, &len); + if (!msg) { + error_setg(errp, "Failed to read %s\n", path); + goto err_out; + } + if (sscanf(msg, "%x:%x:%x.%x", &domain, &bus, &dev, &func) != 4) { + error_setg(errp, "Failed to parse %s (%s)\n", msg, path); + goto err_out; + } + free(msg); + if (domain != d->domain || + bus != d->bus || + dev != d->dev || + func!= d->func) + continue; + snprintf(path, sizeof(path), "%s/vdev-%u", be_path, i); + msg = qemu_xen_xs_read(xenstore, 0, path, &len); + if (!msg) { + error_setg(errp, "Failed to read %s\n", path); + goto out; + } + if (sscanf(msg, "%x:%x:%x.%x", &domain, &bus, &dev, &func) != 4) { + error_setg(errp, "Failed to parse %s (%s)\n", msg, path); + goto err_out; + } + free(msg); + d->local_domain = domain; + d->local_bus = bus; + d->local_dev = dev; + d->local_func = func; + goto out; + } + +err_out: + free(msg); +out: + free(be_path); +} + static void xen_host_pci_sysfs_path(const XenHostPCIDevice *d, const char *name, char *buf, ssize_t size) { int rc; rc = snprintf(buf, size, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/%s", - d->domain, d->bus, d->dev, d->func, name); + d->local_domain, d->local_bus, d->local_dev, d->local_func, name); assert(rc >= 0 && rc < size); } @@ -342,6 +406,17 @@ void xen_host_pci_device_get(XenHostPCIDevice *d, uint16_t domain, d->dev = dev; d->func = func; + if (xen_is_stubdomain) { + xen_host_pci_fill_local_addr(d, errp); + if (*errp) + goto error; + } else { + d->local_domain = d->domain; + d->local_bus = d->bus; + d->local_dev = d->dev; + d->local_func = d->func; + } + xen_host_pci_config_open(d, errp); if (*errp) { goto error; diff --git a/hw/xen/xen-host-pci-device.h b/hw/xen/xen-host-pci-device.h index 4d8d34ecb0..270dcb27f7 100644 --- a/hw/xen/xen-host-pci-device.h +++ b/hw/xen/xen-host-pci-device.h @@ -23,6 +23,12 @@ typedef struct XenHostPCIDevice { uint8_t dev; uint8_t func; + /* different from the above in case of stubdomain */ + uint16_t local_domain; + uint8_t local_bus; + uint8_t local_dev; + uint8_t local_func; + uint16_t vendor_id; uint16_t device_id; uint32_t class_code;