From nobody Fri Oct 31 16:34:17 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=reject dis=none) header.from=citrix.com ARC-Seal: i=1; a=rsa-sha256; t=1744903617; cv=none; d=zohomail.com; s=zohoarc; b=Op9xzixUh0zAvoPolOQ1j4zSeYHFdu1Di/5Aq7s9gyTCZvqI9cMp8k/wZfvgknripts6Bx4UHTniozuOmMGIN7s5h7WFuhL1or/9b30aAzokXflOJ1JMN/5bs0tyam7mc53HcDrKuXYA53TQZglbvZ6UxnbqVw1i1Xu8zov3rvM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1744903617; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=S5LzAPNb/14/24HUMEXjvdgVaGJA9HOIEBYT1bvFhrY=; b=CzFtifhvqgpfL8thtOeALm2IhbSQ3xVUEFXAmFyPtw0hmQn65+OGkBxCQe5o5Ru9WinB8kcYykSDu6yoyVGsWwQBrvj9R3iNzjn2rR516viuDWtybu9IWAP96/KSlx1wgT2aXYzYK9QMOzI1Zmra39Zn1LlDrj+OxFTuM1dDPFg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1744903617417301.95759645969974; Thu, 17 Apr 2025 08:26:57 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.958058.1351027 (Exim 4.92) (envelope-from ) id 1u5R8R-0007on-EE; Thu, 17 Apr 2025 15:26:39 +0000 Received: by outflank-mailman (output) from mailman id 958058.1351027; Thu, 17 Apr 2025 15:26:39 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1u5R8R-0007nl-6i; Thu, 17 Apr 2025 15:26:39 +0000 Received: by outflank-mailman (input) for mailman id 958058; Thu, 17 Apr 2025 15:26:37 +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 1u5R8P-0007lK-Uc for xen-devel@lists.xenproject.org; Thu, 17 Apr 2025 15:26:37 +0000 Received: from mail-ej1-x635.google.com (mail-ej1-x635.google.com [2a00:1450:4864:20::635]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 5a1f7288-1ba0-11f0-9eb0-5ba50f476ded; Thu, 17 Apr 2025 17:26:37 +0200 (CEST) Received: by mail-ej1-x635.google.com with SMTP id a640c23a62f3a-acb415dd8faso139819866b.2 for ; Thu, 17 Apr 2025 08:26:37 -0700 (PDT) Received: from localhost ([84.78.159.3]) by smtp.gmail.com with UTF8SMTPSA id a640c23a62f3a-acb6ec90950sm8154866b.75.2025.04.17.08.26.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Apr 2025 08:26:35 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version 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: 5a1f7288-1ba0-11f0-9eb0-5ba50f476ded DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=citrix.com; s=google; t=1744903596; x=1745508396; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=S5LzAPNb/14/24HUMEXjvdgVaGJA9HOIEBYT1bvFhrY=; b=ZAIA8FZlImkCzYO1jE6LNJOEyKRuFsPRnRyXZOG6r1DMoCHyaVFPqgqNDFANn4WXBH DfqXEIx1miVNZccydwV9EV5K4KBxAwvRUdcG5Y6Btl/KqGhG6kV1dW5RqN+nsD2XqsJc TZp+cguZHlxXPDS0Q7MKnv6hJvar+SXrxlR5s= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744903596; x=1745508396; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=S5LzAPNb/14/24HUMEXjvdgVaGJA9HOIEBYT1bvFhrY=; b=CeYdyUamlIipLvg5StyF4ELzWhmcaRg5oNJ8xuMKZBt8UkH94f53uMj1Sx91zkldUn 7N5nqIAEArGoQXzR0EmzRpQE4H6udogN7GNcEuZTWE9gWHObjMkKNp9UAdFgwYASr4yA fGvgh1lOupUqCe93hxqLylFYxxFpk46l7tP72SeSMBvNTbL5EiSrxZS5RA3JDXyYuhiG 0336LJRyexrQZnz9/HxmLNS9/q5FJus2VxIT5GFB8We6+AB8uK5ifjgsFsaQJ2XYJIW9 4sNBtB+P9MkJYyWcwFWabxK0DFPTOIfTGUJIieKSg9GowXcKrgYo+NFHFL2WRMv3NPig f7qw== X-Gm-Message-State: AOJu0YycCJiHxtAnW1risVpnqsttftrPkrz0qClytjq0MMpx+0tOPfEu vqtRAO1QFfdD1F9UQzySf+jjNJEH18WOBYgcfYs4kC1VtJcwac8xWqdZLGVcfWZ2TXDTPAI40qw W X-Gm-Gg: ASbGnctl+9u+3y16ZYXWadJf4XeWaVA8rTIdJVtDPa0z95yVbhHYlv748MdV9BQTArv GaqU6TWy196v6TIjMtdGt7sSyk2DaR3bWDf7peZzn4NUtUUSYF6e23NKO6OVWibHozzbWBJpGGq wLPUNeyjTMKTVtPwUVEYn1/q7ORa15iOfnRbNCQGHAee0WaMCvEBNWcxXRWvT9j9FGi0sFW/1kY EBQY1zXEf4JMbr64NoLqnultvsFcJ1L6rFW5ZFJ+bXwxaRutLtwo+vuUJNww8ZAmS75QlJa++g/ NW3Aiw6QSO9MHGcybG7/bVuGG8pI/Snl1kqN8I26M/Of1ZlOobmxK3B1 X-Google-Smtp-Source: AGHT+IH/ckS332y3R6Pq/rnTozg4syB5mCwJzYY2eabMvw5c9KcnwbGic64Qif9TTbjSQDWj21fOwA== X-Received: by 2002:a17:906:478f:b0:ac7:b48f:4cd6 with SMTP id a640c23a62f3a-acb4298229amr559808866b.24.1744903596027; Thu, 17 Apr 2025 08:26:36 -0700 (PDT) From: Roger Pau Monne To: xen-devel@lists.xenproject.org Cc: Roger Pau Monne , Jan Beulich , Andrew Cooper , Anthony PERARD , Michal Orzel , Julien Grall , Stefano Stabellini Subject: [PATCH v3 1/4] xen/io: provide helpers for multi size MMIO accesses Date: Thu, 17 Apr 2025 17:25:11 +0200 Message-ID: <20250417152514.13702-2-roger.pau@citrix.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250417152514.13702-1-roger.pau@citrix.com> References: <20250417152514.13702-1-roger.pau@citrix.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @citrix.com) X-ZM-MESSAGEID: 1744903618356019000 Several handlers have the same necessity of reading or writing from or to an MMIO region using 1, 2, 4 or 8 bytes accesses. So far this has been open-coded in the function itself. Instead provide a new set of handlers that encapsulate the accesses. Since the added helpers are not architecture specific, introduce a new generic io.h header. No functional change intended. Signed-off-by: Roger Pau Monn=C3=A9 Reviewed-by: Jan Beulich --- Changes since v2: - Keep error message when handling non-allowed access sizes. Changes since v1: - Limit {read,write}q() to 64bit architectures. - Always have a default case in switch statement. --- xen/arch/x86/hvm/vmsi.c | 47 ++------------------------- xen/arch/x86/mm.c | 30 +++++------------- xen/drivers/vpci/msix.c | 47 ++------------------------- xen/include/xen/io.h | 70 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 83 insertions(+), 111 deletions(-) create mode 100644 xen/include/xen/io.h diff --git a/xen/arch/x86/hvm/vmsi.c b/xen/arch/x86/hvm/vmsi.c index fd83abb929ec..61b89834d97d 100644 --- a/xen/arch/x86/hvm/vmsi.c +++ b/xen/arch/x86/hvm/vmsi.c @@ -24,6 +24,7 @@ * Will be merged it with virtual IOAPIC logic, since most is the same */ =20 +#include #include #include #include @@ -304,28 +305,7 @@ static void adjacent_read( =20 hwaddr =3D fix_to_virt(fixmap_idx) + PAGE_OFFSET(address); =20 - switch ( len ) - { - case 1: - *pval =3D readb(hwaddr); - break; - - case 2: - *pval =3D readw(hwaddr); - break; - - case 4: - *pval =3D readl(hwaddr); - break; - - case 8: - *pval =3D readq(hwaddr); - break; - - default: - ASSERT_UNREACHABLE(); - break; - } + *pval =3D read_mmio(hwaddr, len); } =20 static void adjacent_write( @@ -344,28 +324,7 @@ static void adjacent_write( =20 hwaddr =3D fix_to_virt(fixmap_idx) + PAGE_OFFSET(address); =20 - switch ( len ) - { - case 1: - writeb(val, hwaddr); - break; - - case 2: - writew(val, hwaddr); - break; - - case 4: - writel(val, hwaddr); - break; - - case 8: - writeq(val, hwaddr); - break; - - default: - ASSERT_UNREACHABLE(); - break; - } + write_mmio(hwaddr, val, len); } =20 static int cf_check msixtbl_read( diff --git a/xen/arch/x86/mm.c b/xen/arch/x86/mm.c index 1cf236516789..8baac132b484 100644 --- a/xen/arch/x86/mm.c +++ b/xen/arch/x86/mm.c @@ -95,6 +95,7 @@ #include #include #include +#include #include #include #include @@ -116,7 +117,6 @@ #include #include #include -#include #include #include #include @@ -5102,7 +5102,7 @@ static void __iomem *subpage_mmio_map_page( static void subpage_mmio_write_emulate( mfn_t mfn, unsigned int offset, - const void *data, + unsigned long data, unsigned int len) { struct subpage_ro_range *entry; @@ -5131,26 +5131,8 @@ static void subpage_mmio_write_emulate( return; } =20 - addr +=3D offset; - switch ( len ) - { - case 1: - writeb(*(const uint8_t*)data, addr); - break; - case 2: - writew(*(const uint16_t*)data, addr); - break; - case 4: - writel(*(const uint32_t*)data, addr); - break; - case 8: - writeq(*(const uint64_t*)data, addr); - break; - default: - /* mmio_ro_emulated_write() already validated the size */ - ASSERT_UNREACHABLE(); + if ( !write_mmio(addr + offset, data, len) ) goto write_ignored; - } } =20 #ifdef CONFIG_HVM @@ -5185,18 +5167,20 @@ int cf_check mmio_ro_emulated_write( struct x86_emulate_ctxt *ctxt) { struct mmio_ro_emulate_ctxt *mmio_ro_ctxt =3D ctxt->data; + unsigned long data =3D 0; =20 /* Only allow naturally-aligned stores at the original %cr2 address. */ if ( ((bytes | offset) & (bytes - 1)) || !bytes || - offset !=3D mmio_ro_ctxt->cr2 ) + offset !=3D mmio_ro_ctxt->cr2 || bytes > sizeof(data) ) { gdprintk(XENLOG_WARNING, "bad access (cr2=3D%lx, addr=3D%lx, bytes= =3D%u)\n", mmio_ro_ctxt->cr2, offset, bytes); return X86EMUL_UNHANDLEABLE; } =20 + memcpy(&data, p_data, bytes); subpage_mmio_write_emulate(mmio_ro_ctxt->mfn, PAGE_OFFSET(offset), - p_data, bytes); + data, bytes); =20 return X86EMUL_OKAY; } diff --git a/xen/drivers/vpci/msix.c b/xen/drivers/vpci/msix.c index 6bd8c55bb48e..6455f2a03a01 100644 --- a/xen/drivers/vpci/msix.c +++ b/xen/drivers/vpci/msix.c @@ -17,6 +17,7 @@ * License along with this program; If not, see . */ =20 +#include #include #include =20 @@ -344,28 +345,7 @@ static int adjacent_read(const struct domain *d, const= struct vpci_msix *msix, return X86EMUL_OKAY; } =20 - switch ( len ) - { - case 1: - *data =3D readb(mem + PAGE_OFFSET(addr)); - break; - - case 2: - *data =3D readw(mem + PAGE_OFFSET(addr)); - break; - - case 4: - *data =3D readl(mem + PAGE_OFFSET(addr)); - break; - - case 8: - *data =3D readq(mem + PAGE_OFFSET(addr)); - break; - - default: - ASSERT_UNREACHABLE(); - break; - } + *data =3D read_mmio(mem + PAGE_OFFSET(addr), len); spin_unlock(&vpci->lock); =20 return X86EMUL_OKAY; @@ -493,28 +473,7 @@ static int adjacent_write(const struct domain *d, cons= t struct vpci_msix *msix, return X86EMUL_OKAY; } =20 - switch ( len ) - { - case 1: - writeb(data, mem + PAGE_OFFSET(addr)); - break; - - case 2: - writew(data, mem + PAGE_OFFSET(addr)); - break; - - case 4: - writel(data, mem + PAGE_OFFSET(addr)); - break; - - case 8: - writeq(data, mem + PAGE_OFFSET(addr)); - break; - - default: - ASSERT_UNREACHABLE(); - break; - } + write_mmio(mem + PAGE_OFFSET(addr), data, len); spin_unlock(&vpci->lock); =20 return X86EMUL_OKAY; diff --git a/xen/include/xen/io.h b/xen/include/xen/io.h new file mode 100644 index 000000000000..164a20c5d7b5 --- /dev/null +++ b/xen/include/xen/io.h @@ -0,0 +1,70 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * IO related routines. + * + * Copyright (c) 2025 Cloud Software Group + */ +#ifndef XEN_IO_H +#define XEN_IO_H + +#include + +#include + +static inline unsigned long read_mmio(const volatile void __iomem *mem, + unsigned int size) +{ + switch ( size ) + { + case 1: + return readb(mem); + + case 2: + return readw(mem); + + case 4: + return readl(mem); + +#ifdef CONFIG_64BIT + case 8: + return readq(mem); +#endif + + default: + ASSERT_UNREACHABLE(); + return ~0UL; + } +} + +static inline bool write_mmio(volatile void __iomem *mem, unsigned long da= ta, + unsigned int size) +{ + switch ( size ) + { + case 1: + writeb(data, mem); + break; + + case 2: + writew(data, mem); + break; + + case 4: + writel(data, mem); + break; + +#ifdef CONFIG_64BIT + case 8: + writeq(data, mem); + break; +#endif + + default: + ASSERT_UNREACHABLE(); + return false; + } + + return true; +} + +#endif /* XEN_IO_H */ --=20 2.48.1 From nobody Fri Oct 31 16:34:17 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=reject dis=none) header.from=citrix.com ARC-Seal: i=1; a=rsa-sha256; t=1744903616; cv=none; d=zohomail.com; s=zohoarc; b=R5kjZ2IomfAC85PORXvidTYWF1BknLnggnb4cbKOFpkua38b5XPIxCiP1l0AJlCqG30zC/CxDbCfdQUc2AwhmEX+PAhr9+OIaB1Pa7G8Odoc79U+54y+3zGbAF3tmCyAjW6C+vOTAqoxhuzeX1I8Jkup0ABSrmMkwVAOGC1OgvY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1744903616; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=mtVvVPkNQY8ZxlkkVuOoPY391iPEFrzuGNcLlQibf5k=; b=KeGiuzmrfMQao/SS/mkAw8MqteZCPjMdwDkocaZU5XNUf7xu5vH9Z42w/+KHoL9kZE8i8NgLcTaCACP+EoQpXMtcXgH4+Hs5T7gknfjbHwWgJmUFGC1Cpuiipg9Kf1vKWQqkf4ZwShunio9EilITeTtssTQ3TBw7hLEuxC8AXVE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1744903616367701.9894044082138; Thu, 17 Apr 2025 08:26:56 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.958059.1351041 (Exim 4.92) (envelope-from ) id 1u5R8U-0008Ek-Ks; Thu, 17 Apr 2025 15:26:42 +0000 Received: by outflank-mailman (output) from mailman id 958059.1351041; Thu, 17 Apr 2025 15:26:42 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1u5R8U-0008Eb-Gl; Thu, 17 Apr 2025 15:26:42 +0000 Received: by outflank-mailman (input) for mailman id 958059; Thu, 17 Apr 2025 15:26:41 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1u5R8T-0008DN-0E for xen-devel@lists.xenproject.org; Thu, 17 Apr 2025 15:26:41 +0000 Received: from mail-ed1-x534.google.com (mail-ed1-x534.google.com [2a00:1450:4864:20::534]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 5b1c09b0-1ba0-11f0-9ffb-bf95429c2676; Thu, 17 Apr 2025 17:26:38 +0200 (CEST) Received: by mail-ed1-x534.google.com with SMTP id 4fb4d7f45d1cf-5e5c7d6b96fso1635924a12.3 for ; Thu, 17 Apr 2025 08:26:38 -0700 (PDT) Received: from localhost ([84.78.159.3]) by smtp.gmail.com with UTF8SMTPSA id a640c23a62f3a-acb6ef9e7e6sm6753266b.162.2025.04.17.08.26.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Apr 2025 08:26:36 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version 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: 5b1c09b0-1ba0-11f0-9ffb-bf95429c2676 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=citrix.com; s=google; t=1744903597; x=1745508397; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=mtVvVPkNQY8ZxlkkVuOoPY391iPEFrzuGNcLlQibf5k=; b=o3l03CDSsp16tCdSLHcSmrIDsu7ztd98PNtpUL7x7reMkkMu9xV346LBoWi6L41Y72 l4+Csl+DidyoW3o6d7rLxdroeXBEXrSEiyslbEyH2YyJXLjijocXo4cvgtkO/IkVbzSn cpezm8ah8tAG2fyg5jqdbo50Vzrjx+HKLo0eQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744903597; x=1745508397; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=mtVvVPkNQY8ZxlkkVuOoPY391iPEFrzuGNcLlQibf5k=; b=u+JWkYfZDeB2NGGTl8qzYA1qeTOEyMJe/6oRJBurPOMy8o7sb8IB6/13IYv4CWdc+A yP5K5hCJ6DTS0NpKsrqbthuEHrQEKoRl2MIyysztI1Qmtq8FkLNj5G8hOxTZwsmlWYVc 0hCtxfLnS7CCHEEl5Kp/edxysmYjZyDhUHR85wJTYBIvb5zLLzjnCekhLIU0ylVmdg+s AybD3YjUeL95WRal6YF5nzI/KpLRCL0sK5hl2js4glVGMephAaWx0kEnTWvLODq5VWqu a55HG47s7UL1rvKoSnQtAmld6Hs7vlUm8DsHDXjXGcjcaAF7b+VvQRX6x+BkPv971DLq M8JA== X-Gm-Message-State: AOJu0YwIjReXnxE8OVy4THPlOHaVA8oE9KmZV048GXJAa9FfbXDNWs24 5AleUkxkYYEjkppazxHo3uIUPcmLEAcbFSW6/PgFJuiys4MM0lX9QRmQTyemZo8h2EmFBvJuORV h X-Gm-Gg: ASbGnctp1UkUEed6cLUjqUZeHGY+xrScP9QlQLnlFf8yRdxunt0vpMMx3xKCxRxV51H 83zpOrPX/hn5iCLrW/HUXLD31hXQmf6Vf21myeJnB4JYluXn/VpSBQWcJng2V5UhingqKzTgrCA CI4yzLs7etUM8aZVmMmlOXfkRlttAkMi20gaiizN5Sw+Z1zu/sPkkr+h/Z+xH8DjxpUj461/L8M AFuziZGYQMC95IltoSOzdFPc7gFHYi4lb+znsNl9mU1OPpa0yaFseD0ebRdENMhZhgi6kOReMLU rkSRCu8FLXbDJJ2gwcH8D2cX3tw6XenDTPACjX6KpI2MUdZ/IhY9GzYj X-Google-Smtp-Source: AGHT+IFChuVjPTJWfta9AUWGP3/WLUQaqpQg41cYyYY5MeNURANJHF5LaM0GPEFqeZJZ6DxJqc/1iA== X-Received: by 2002:a17:907:d8b:b0:ac7:684c:25ca with SMTP id a640c23a62f3a-acb42c154b8mr469829266b.51.1744903597280; Thu, 17 Apr 2025 08:26:37 -0700 (PDT) From: Roger Pau Monne To: xen-devel@lists.xenproject.org Cc: Roger Pau Monne , Jan Beulich , Andrew Cooper Subject: [PATCH v3 2/4] x86/hvm: fix handling of accesses to partial r/o MMIO pages Date: Thu, 17 Apr 2025 17:25:12 +0200 Message-ID: <20250417152514.13702-3-roger.pau@citrix.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250417152514.13702-1-roger.pau@citrix.com> References: <20250417152514.13702-1-roger.pau@citrix.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @citrix.com) X-ZM-MESSAGEID: 1744903618388019000 The current logic to handle accesses to MMIO pages partially read-only is based on the (now removed) logic used to handle accesses to the r/o MMCFG region(s) for PVH v1 dom0. However that has issues when running on AMD hardware, as in that case the guest linear address that triggered the fault is not provided as part of the VM exit. This caused mmio_ro_emulated_write() to always fail before calling subpage_mmio_write_emulate() when running on AMD and called from an HVM context. Take a different approach and convert the handling of partial read-only MMIO page accesses into an HVM MMIO ops handler, as that's the more natural way to handle this kind of emulation for HVM domains. This allows getting rid of hvm_emulate_one_mmio() and it's single call site in hvm_hap_nested_page_fault(). As part of the fix r/o MMIO accesses are now handled by handle_mmio_with_translation(), re-using the same logic that was used for other read-only types part of p2m_is_discard_write(). The usage of emulation for faulting p2m_mmio_direct types is limited to addresses in the r/o MMIO range. The page present check is dropped as type p2m_mmio_direct must have the present bit set in the PTE. Note a small adjustment is needed to the `pf-fixup` dom0 PVH logic: avoid attempting to fixup faults resulting from write accesses to read-only MMIO regions, as handling of those accesses is now done by handle_mmio(). Fixes: 33c19df9a5a0 ('x86/PCI: intercept accesses to RO MMIO from dom0s in = HVM containers') Signed-off-by: Roger Pau Monn=C3=A9 --- Changes since v2: - Attempt to fixup read access to r/o MMIO regions for dom0 PVH. - Use PFN_DOWN() with get_gfn_query(). - Reject unaligned accesses. - Add comment about the need to handle reads. Changes since v1: - Introduce hvm/mmio.c to place the r/o MMIO handlers. - Add comment about pf-fixup and r/o MMIO range checking. - Expand commit message about dropping the PTE present check and usage of the emulator. - Return X86EMUL_OKAY in the read handler if the MMIO region is not found. - Check the faulting address is in the mmio_ro_ranges before sending for emulation. --- xen/arch/x86/hvm/Makefile | 1 + xen/arch/x86/hvm/emulate.c | 54 ++--------- xen/arch/x86/hvm/hvm.c | 17 ++-- xen/arch/x86/hvm/mmio.c | 125 +++++++++++++++++++++++++ xen/arch/x86/include/asm/hvm/emulate.h | 1 - xen/arch/x86/include/asm/hvm/io.h | 3 + xen/arch/x86/include/asm/mm.h | 12 +++ xen/arch/x86/mm.c | 37 +------- 8 files changed, 160 insertions(+), 90 deletions(-) create mode 100644 xen/arch/x86/hvm/mmio.c diff --git a/xen/arch/x86/hvm/Makefile b/xen/arch/x86/hvm/Makefile index 4c1fa5c6c2bf..6ec2c8f2db56 100644 --- a/xen/arch/x86/hvm/Makefile +++ b/xen/arch/x86/hvm/Makefile @@ -15,6 +15,7 @@ obj-y +=3D intercept.o obj-y +=3D io.o obj-y +=3D ioreq.o obj-y +=3D irq.o +obj-y +=3D mmio.o obj-y +=3D monitor.o obj-y +=3D mtrr.o obj-y +=3D nestedhvm.o diff --git a/xen/arch/x86/hvm/emulate.c b/xen/arch/x86/hvm/emulate.c index 79c180b408ac..65b41b964df0 100644 --- a/xen/arch/x86/hvm/emulate.c +++ b/xen/arch/x86/hvm/emulate.c @@ -370,7 +370,15 @@ static int hvmemul_do_io( /* If there is no suitable backing DM, just ignore accesses */ if ( !s ) { - if ( is_mmio && is_hardware_domain(currd) ) + if ( is_mmio && is_hardware_domain(currd) && + /* + * Do not attempt to fixup write accesses to r/o MMIO reg= ions, + * they are expected to be terminated by the null handler + * below. + */ + (!rangeset_contains_singleton(mmio_ro_ranges, + PFN_DOWN(addr)) || + dir =3D=3D IOREQ_READ) ) { /* * PVH dom0 is likely missing MMIO mappings on the p2m, du= e to @@ -2859,50 +2867,6 @@ int hvm_emulate_one( return _hvm_emulate_one(hvmemul_ctxt, &hvm_emulate_ops, completion); } =20 -int hvm_emulate_one_mmio(unsigned long mfn, unsigned long gla) -{ - static const struct x86_emulate_ops hvm_ro_emulate_ops_mmio =3D { - .read =3D x86emul_unhandleable_rw, - .insn_fetch =3D hvmemul_insn_fetch, - .write =3D mmio_ro_emulated_write, - .validate =3D hvmemul_validate, - }; - struct mmio_ro_emulate_ctxt mmio_ro_ctxt =3D { .cr2 =3D gla, .mfn =3D = _mfn(mfn) }; - struct hvm_emulate_ctxt ctxt; - unsigned int seg, bdf; - int rc; - - if ( pci_ro_mmcfg_decode(mfn, &seg, &bdf) ) - { - /* Should be always handled by vPCI for PVH dom0. */ - gdprintk(XENLOG_ERR, "unhandled MMCFG access for %pp\n", - &PCI_SBDF(seg, bdf)); - ASSERT_UNREACHABLE(); - return X86EMUL_UNHANDLEABLE; - } - - hvm_emulate_init_once(&ctxt, x86_insn_is_mem_write, - guest_cpu_user_regs()); - ctxt.ctxt.data =3D &mmio_ro_ctxt; - - switch ( rc =3D _hvm_emulate_one(&ctxt, &hvm_ro_emulate_ops_mmio, - VIO_no_completion) ) - { - case X86EMUL_UNHANDLEABLE: - case X86EMUL_UNIMPLEMENTED: - hvm_dump_emulation_state(XENLOG_G_WARNING, "r/o MMIO", &ctxt, rc); - break; - case X86EMUL_EXCEPTION: - hvm_inject_event(&ctxt.ctxt.event); - /* fallthrough */ - default: - hvm_emulate_writeback(&ctxt); - break; - } - - return rc; -} - void hvm_emulate_one_vm_event(enum emul_kind kind, unsigned int trapnr, unsigned int errcode) { diff --git a/xen/arch/x86/hvm/hvm.c b/xen/arch/x86/hvm/hvm.c index 6f1174c5127e..6b998387e3d8 100644 --- a/xen/arch/x86/hvm/hvm.c +++ b/xen/arch/x86/hvm/hvm.c @@ -8,6 +8,7 @@ */ =20 #include +#include #include #include #include @@ -35,7 +36,6 @@ #include #include #include -#include #include #include #include @@ -692,6 +692,8 @@ int hvm_domain_initialise(struct domain *d, =20 register_portio_handler(d, XEN_HVM_DEBUGCONS_IOPORT, 1, hvm_print_line= ); =20 + register_subpage_ro_handler(d); + if ( hvm_tsc_scaling_supported ) d->arch.hvm.tsc_scaling_ratio =3D hvm_default_tsc_scaling_ratio; =20 @@ -1981,7 +1983,10 @@ int hvm_hap_nested_page_fault(paddr_t gpa, unsigned = long gla, */ if ( (p2mt =3D=3D p2m_mmio_dm) || (npfec.write_access && - (p2m_is_discard_write(p2mt) || (p2mt =3D=3D p2m_ioreq_server))) ) + (p2m_is_discard_write(p2mt) || (p2mt =3D=3D p2m_ioreq_server) || + /* MMIO entries can be r/o if the target mfn is in mmio_ro_rang= es. */ + (p2mt =3D=3D p2m_mmio_direct && + rangeset_contains_singleton(mmio_ro_ranges, mfn_x(mfn))))) ) { if ( !handle_mmio_with_translation(gla, gfn, npfec) ) hvm_inject_hw_exception(X86_EXC_GP, 0); @@ -2033,14 +2038,6 @@ int hvm_hap_nested_page_fault(paddr_t gpa, unsigned = long gla, goto out_put_gfn; } =20 - if ( (p2mt =3D=3D p2m_mmio_direct) && npfec.write_access && npfec.pres= ent && - (is_hardware_domain(currd) || subpage_mmio_write_accept(mfn, gla)= ) && - (hvm_emulate_one_mmio(mfn_x(mfn), gla) =3D=3D X86EMUL_OKAY) ) - { - rc =3D 1; - goto out_put_gfn; - } - /* If we fell through, the vcpu will retry now that access restriction= s have * been removed. It may fault again if the p2m entry type still requir= es so. * Otherwise, this is an error condition. */ diff --git a/xen/arch/x86/hvm/mmio.c b/xen/arch/x86/hvm/mmio.c new file mode 100644 index 000000000000..88e9d0c65d89 --- /dev/null +++ b/xen/arch/x86/hvm/mmio.c @@ -0,0 +1,125 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * MMIO related routines. + * + * Copyright (c) 2025 Cloud Software Group + */ + +#include +#include + +#include + +static int cf_check subpage_mmio_accept(struct vcpu *v, unsigned long addr) +{ + p2m_type_t t; + mfn_t mfn =3D get_gfn_query_unlocked(v->domain, PFN_DOWN(addr), &t); + + return !mfn_eq(mfn, INVALID_MFN) && t =3D=3D p2m_mmio_direct && + subpage_mmio_find_page(mfn); +} + +/* + * The guest has read access to those regions, and consequently read acces= ses + * shouldn't fault. However read-modify-write operations may take this pa= th, + * so handling of reads is necessary. + */ +static int cf_check subpage_mmio_read( + struct vcpu *v, unsigned long addr, unsigned int len, unsigned long *d= ata) +{ + struct domain *d =3D v->domain; + unsigned long gfn =3D PFN_DOWN(addr); + p2m_type_t t; + mfn_t mfn; + struct subpage_ro_range *entry; + volatile void __iomem *mem; + + *data =3D ~0UL; + + if ( !IS_ALIGNED(len | addr, len) ) + { + gprintk(XENLOG_ERR, + "ignoring unaligned read to r/o MMIO subpage %#lx size %u\= n", + addr, len); + return X86EMUL_OKAY; + } + + mfn =3D get_gfn_query(d, gfn, &t); + if ( mfn_eq(mfn, INVALID_MFN) || t !=3D p2m_mmio_direct ) + { + put_gfn(d, gfn); + return X86EMUL_RETRY; + } + + entry =3D subpage_mmio_find_page(mfn); + if ( !entry ) + { + put_gfn(d, gfn); + return X86EMUL_OKAY; + } + + mem =3D subpage_mmio_map_page(entry); + if ( !mem ) + { + put_gfn(d, gfn); + gprintk(XENLOG_ERR, + "Failed to map page for MMIO read at %#lx -> %#lx\n", + addr, mfn_to_maddr(mfn) + PAGE_OFFSET(addr)); + return X86EMUL_OKAY; + } + + *data =3D read_mmio(mem + PAGE_OFFSET(addr), len); + + put_gfn(d, gfn); + return X86EMUL_OKAY; +} + +static int cf_check subpage_mmio_write( + struct vcpu *v, unsigned long addr, unsigned int len, unsigned long da= ta) +{ + struct domain *d =3D v->domain; + unsigned long gfn =3D PFN_DOWN(addr); + p2m_type_t t; + mfn_t mfn; + + if ( !IS_ALIGNED(len | addr, len) ) + { + gprintk(XENLOG_ERR, + "ignoring unaligned write to r/o MMIO subpage %#lx size %u= \n", + addr, len); + return X86EMUL_OKAY; + } + + mfn =3D get_gfn_query(d, gfn, &t); + if ( mfn_eq(mfn, INVALID_MFN) || t !=3D p2m_mmio_direct ) + { + put_gfn(d, gfn); + return X86EMUL_RETRY; + } + + subpage_mmio_write_emulate(mfn, PAGE_OFFSET(addr), data, len); + + put_gfn(d, gfn); + return X86EMUL_OKAY; +} + +void register_subpage_ro_handler(struct domain *d) +{ + static const struct hvm_mmio_ops subpage_mmio_ops =3D { + .check =3D subpage_mmio_accept, + .read =3D subpage_mmio_read, + .write =3D subpage_mmio_write, + }; + + register_mmio_handler(d, &subpage_mmio_ops); +} + +/* + * Local variables: + * mode: C + * c-file-style: "BSD" + * c-basic-offset: 4 + * tab-width: 4 + * indent-tabs-mode: nil + * End: + */ diff --git a/xen/arch/x86/include/asm/hvm/emulate.h b/xen/arch/x86/include/= asm/hvm/emulate.h index c7a2d2a5be4e..178ac32e151f 100644 --- a/xen/arch/x86/include/asm/hvm/emulate.h +++ b/xen/arch/x86/include/asm/hvm/emulate.h @@ -86,7 +86,6 @@ void hvmemul_cancel(struct vcpu *v); struct segment_register *hvmemul_get_seg_reg( enum x86_segment seg, struct hvm_emulate_ctxt *hvmemul_ctxt); -int hvm_emulate_one_mmio(unsigned long mfn, unsigned long gla); =20 static inline bool handle_mmio(void) { diff --git a/xen/arch/x86/include/asm/hvm/io.h b/xen/arch/x86/include/asm/h= vm/io.h index 565bad300d20..c12f099a037c 100644 --- a/xen/arch/x86/include/asm/hvm/io.h +++ b/xen/arch/x86/include/asm/hvm/io.h @@ -135,6 +135,9 @@ void destroy_vpci_mmcfg(struct domain *d); /* Remove MMCFG regions from a domain ->iomem_caps. */ int vpci_mmcfg_deny_access(struct domain *d); =20 +/* r/o MMIO subpage access handler. */ +void register_subpage_ro_handler(struct domain *d); + #endif /* __ASM_X86_HVM_IO_H__ */ =20 =20 diff --git a/xen/arch/x86/include/asm/mm.h b/xen/arch/x86/include/asm/mm.h index a1bc8cc27451..c2e9ef6e5023 100644 --- a/xen/arch/x86/include/asm/mm.h +++ b/xen/arch/x86/include/asm/mm.h @@ -554,6 +554,18 @@ int cf_check mmio_ro_emulated_write( enum x86_segment seg, unsigned long offset, void *p_data, unsigned int bytes, struct x86_emulate_ctxt *ctxt); =20 +/* r/o MMIO subpage access handlers. */ +struct subpage_ro_range { + struct list_head list; + mfn_t mfn; + void __iomem *mapped; + DECLARE_BITMAP(ro_elems, PAGE_SIZE / MMIO_RO_SUBPAGE_GRAN); +}; +struct subpage_ro_range *subpage_mmio_find_page(mfn_t mfn); +void __iomem *subpage_mmio_map_page(struct subpage_ro_range *entry); +void subpage_mmio_write_emulate( + mfn_t mfn, unsigned int offset, unsigned long data, unsigned int len); + int audit_adjust_pgtables(struct domain *d, int dir, int noisy); =20 extern int pagefault_by_memadd(unsigned long addr, struct cpu_user_regs *r= egs); diff --git a/xen/arch/x86/mm.c b/xen/arch/x86/mm.c index 8baac132b484..927beb6f07aa 100644 --- a/xen/arch/x86/mm.c +++ b/xen/arch/x86/mm.c @@ -157,13 +157,6 @@ struct rangeset *__read_mostly mmio_ro_ranges; static uint32_t __ro_after_init base_disallow_mask; =20 /* Handling sub-page read-only MMIO regions */ -struct subpage_ro_range { - struct list_head list; - mfn_t mfn; - void __iomem *mapped; - DECLARE_BITMAP(ro_elems, PAGE_SIZE / MMIO_RO_SUBPAGE_GRAN); -}; - static LIST_HEAD_RO_AFTER_INIT(subpage_ro_ranges); static DEFINE_SPINLOCK(subpage_ro_lock); =20 @@ -4929,7 +4922,7 @@ long arch_memory_op(unsigned long cmd, XEN_GUEST_HAND= LE_PARAM(void) arg) return rc; } =20 -static struct subpage_ro_range *subpage_mmio_find_page(mfn_t mfn) +struct subpage_ro_range *subpage_mmio_find_page(mfn_t mfn) { struct subpage_ro_range *entry; =20 @@ -5074,7 +5067,7 @@ int __init subpage_mmio_ro_add( return rc; } =20 -static void __iomem *subpage_mmio_map_page( +void __iomem *subpage_mmio_map_page( struct subpage_ro_range *entry) { void __iomem *mapped_page; @@ -5099,7 +5092,7 @@ static void __iomem *subpage_mmio_map_page( return entry->mapped; } =20 -static void subpage_mmio_write_emulate( +void subpage_mmio_write_emulate( mfn_t mfn, unsigned int offset, unsigned long data, @@ -5135,30 +5128,6 @@ static void subpage_mmio_write_emulate( goto write_ignored; } =20 -#ifdef CONFIG_HVM -bool subpage_mmio_write_accept(mfn_t mfn, unsigned long gla) -{ - unsigned int offset =3D PAGE_OFFSET(gla); - const struct subpage_ro_range *entry; - - entry =3D subpage_mmio_find_page(mfn); - if ( !entry ) - return false; - - if ( !test_bit(offset / MMIO_RO_SUBPAGE_GRAN, entry->ro_elems) ) - { - /* - * We don't know the write size at this point yet, so it could be - * an unaligned write, but accept it here anyway and deal with it - * later. - */ - return true; - } - - return false; -} -#endif - int cf_check mmio_ro_emulated_write( enum x86_segment seg, unsigned long offset, --=20 2.48.1 From nobody Fri Oct 31 16:34:17 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=reject dis=none) header.from=citrix.com ARC-Seal: i=1; a=rsa-sha256; t=1744903619; cv=none; d=zohomail.com; s=zohoarc; b=iwI0Z0zpcbig8DwX+8iz0wrO/IoGhXyccc8NR7l60M4ZnX9l7AwWpv4wTGn8asA8w+M9Eh1IGdlpudCQnXEYNl2DgoLLZmOAnPDMo02E7lTks6Drw/Pp6nynR9xRLbKXaiQPWjNoa4kRZ7yzi1OwzEMHA/+jJ7t771h0exUluSc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1744903619; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=WlVYLyYWAUO1qIZHmr5fhD6GhFwX8NqKxwynFR0F9X4=; b=ikNNQytK57k6Q5j1snWnd61cNZLK343OYRtfMGDJP/Tpjtqjs+gzHDi3Q3WlHGlBmCvz+8tk30zhcdxjtF8GNLOw9l8iVB1GLi/geOmcY/MViBZvyF9cNelwOo/kPRVb1irWFv9OcyJtcAipd6A/YZKWAcbzd52/VOM+JJXvFFM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1744903619031836.854855874259; Thu, 17 Apr 2025 08:26:59 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.958060.1351044 (Exim 4.92) (envelope-from ) id 1u5R8U-0008Hw-Uz; Thu, 17 Apr 2025 15:26:42 +0000 Received: by outflank-mailman (output) from mailman id 958060.1351044; Thu, 17 Apr 2025 15:26:42 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1u5R8U-0008H1-Ps; Thu, 17 Apr 2025 15:26:42 +0000 Received: by outflank-mailman (input) for mailman id 958060; Thu, 17 Apr 2025 15:26:41 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1u5R8T-0008DN-Mg for xen-devel@lists.xenproject.org; Thu, 17 Apr 2025 15:26:41 +0000 Received: from mail-ej1-x630.google.com (mail-ej1-x630.google.com [2a00:1450:4864:20::630]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 5ba55f2f-1ba0-11f0-9ffb-bf95429c2676; Thu, 17 Apr 2025 17:26:39 +0200 (CEST) Received: by mail-ej1-x630.google.com with SMTP id a640c23a62f3a-ac339f53df9so169421466b.1 for ; Thu, 17 Apr 2025 08:26:39 -0700 (PDT) Received: from localhost ([84.78.159.3]) by smtp.gmail.com with UTF8SMTPSA id a640c23a62f3a-acb6ef49ff9sm6960166b.158.2025.04.17.08.26.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Apr 2025 08:26:38 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version 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: 5ba55f2f-1ba0-11f0-9ffb-bf95429c2676 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=citrix.com; s=google; t=1744903599; x=1745508399; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=WlVYLyYWAUO1qIZHmr5fhD6GhFwX8NqKxwynFR0F9X4=; b=DZPQvM1+YoqBGpkHaSMwMmZ21QeP8ACrOPAFGHUyTG05bTMtpYqoHISsMNj3stplq4 SvlFr/piTu+z75A87bvFIUoTQxGJjLO3JoO/s3Ret9nDxNoqRoeTHR/E9Aa5QbSR7jbt suNFgQB8ojjTTcChke63DFnZeOQENcywlg/+Y= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744903599; x=1745508399; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=WlVYLyYWAUO1qIZHmr5fhD6GhFwX8NqKxwynFR0F9X4=; b=fuLhtwiifTMIRdYI38O/DHx3dF6Alx2WIR7/SiPckDm3+sT4lc659E1isD9cF2o3c/ mS9uyzG2K9U23GrjrvBGqmV7mrWb47x09Ei+Sm7bSnjmX3zZfNRuIJ08O0XVKPo3tFZ2 qu9LtDzr9XSmst6oZMhgkvy989EVTFKFVzOBGdYwnQ+bMCzHWffm7LPdYbEfRH5fhCha GM5l0P3HTwkF/FzmJbI6Web+Ggfmlfs2ClqYpHMz42nBBfN9fdaDTCIy+l31/AuakkCG f/GylwCwMjae+hPrrZBPzEhNtzBoGjV6vSIwNXeTAlkG/yBmHGwymvqOb7W1wEUO+4X1 79XQ== X-Gm-Message-State: AOJu0YxKnQRw/iPf67OZt2Pc/PWQyEibqRoDfiOYA1Lei+20+T0W3jpp j+C71/3R2zM4aCp5MnjTxYdI/8/OmCO0wSV3a6KO/lWBo946vOUDIMAZQHehE7Fce2NP3ai6VvX F X-Gm-Gg: ASbGnctQWuD0AB9zumxij3bMdTk0c1ewUl+GnSutxmdHRtwrMq2YROaLc2pzLmiRUME 8qPICGTf2gIAuoSZ20l9zBFq4R+XbleBY/N8xXJPhy71+6C14zJRo7aj8dPW48tFQk5eG/4msp6 iNLj2uyPR9t8UGMi4vzAYifjezDhLDsYlcF8NIgZ5h7X8RQpb1nNarQvinFGsdgaTSrf/F69GUN Ap0K49c5ZDFDKyIhiAcdiQ7csgjIzddAGI3sz/XKa5nwwfTdvah1xttdGgmFgvVs7zQj6rS/qKb bl9zLx9VSTS5dTMkBq/7I89E63256tGokT+gttJs9150+g== X-Google-Smtp-Source: AGHT+IEjtDGr3KBxOOYrHt3tvSp0cCDG3W62MSRWQ3BSwNHZDAGR5nyG+qaBsUUNVt0KAK0ZN1KaZw== X-Received: by 2002:a17:907:7b87:b0:ac7:f2b9:ec3b with SMTP id a640c23a62f3a-acb428748d0mr562364966b.4.1744903598736; Thu, 17 Apr 2025 08:26:38 -0700 (PDT) From: Roger Pau Monne To: xen-devel@lists.xenproject.org Cc: Roger Pau Monne , Jan Beulich , Andrew Cooper Subject: [PATCH v3 3/4] x86/hvm: only register the r/o subpage ops when needed Date: Thu, 17 Apr 2025 17:25:13 +0200 Message-ID: <20250417152514.13702-4-roger.pau@citrix.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250417152514.13702-1-roger.pau@citrix.com> References: <20250417152514.13702-1-roger.pau@citrix.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @citrix.com) X-ZM-MESSAGEID: 1744903620185019000 MMIO operation handlers can be expensive to process, hence attempt to register only those that will be needed by the domain. Subpage r/o MMIO regions are added exclusively at boot, further limit their addition to strictly before the initial domain gets created, so by the time initial domain creation happens Xen knows whether subpage is required or not. This allows only registering the MMIO handler when there are subpage regions to handle. Signed-off-by: Roger Pau Monn=C3=A9 Reviewed-by: Jan Beulich --- Could possibly be part of the previous patch, but strictly speaking is an improvement, as even before the previous patch subpage r/o was always called even when no subpage regions are registered. --- xen/arch/x86/hvm/hvm.c | 3 ++- xen/arch/x86/include/asm/mm.h | 1 + xen/arch/x86/mm.c | 16 ++++++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/xen/arch/x86/hvm/hvm.c b/xen/arch/x86/hvm/hvm.c index 6b998387e3d8..4cb2e13046d1 100644 --- a/xen/arch/x86/hvm/hvm.c +++ b/xen/arch/x86/hvm/hvm.c @@ -692,7 +692,8 @@ int hvm_domain_initialise(struct domain *d, =20 register_portio_handler(d, XEN_HVM_DEBUGCONS_IOPORT, 1, hvm_print_line= ); =20 - register_subpage_ro_handler(d); + if ( subpage_ro_active() ) + register_subpage_ro_handler(d); =20 if ( hvm_tsc_scaling_supported ) d->arch.hvm.tsc_scaling_ratio =3D hvm_default_tsc_scaling_ratio; diff --git a/xen/arch/x86/include/asm/mm.h b/xen/arch/x86/include/asm/mm.h index c2e9ef6e5023..aeb8ebcf2d56 100644 --- a/xen/arch/x86/include/asm/mm.h +++ b/xen/arch/x86/include/asm/mm.h @@ -561,6 +561,7 @@ struct subpage_ro_range { void __iomem *mapped; DECLARE_BITMAP(ro_elems, PAGE_SIZE / MMIO_RO_SUBPAGE_GRAN); }; +bool subpage_ro_active(void); struct subpage_ro_range *subpage_mmio_find_page(mfn_t mfn); void __iomem *subpage_mmio_map_page(struct subpage_ro_range *entry); void subpage_mmio_write_emulate( diff --git a/xen/arch/x86/mm.c b/xen/arch/x86/mm.c index 927beb6f07aa..0e29bab03a95 100644 --- a/xen/arch/x86/mm.c +++ b/xen/arch/x86/mm.c @@ -4922,6 +4922,11 @@ long arch_memory_op(unsigned long cmd, XEN_GUEST_HAN= DLE_PARAM(void) arg) return rc; } =20 +bool subpage_ro_active(void) +{ + return !list_empty(&subpage_ro_ranges); +} + struct subpage_ro_range *subpage_mmio_find_page(mfn_t mfn) { struct subpage_ro_range *entry; @@ -5011,6 +5016,17 @@ int __init subpage_mmio_ro_add( !IS_ALIGNED(size, MMIO_RO_SUBPAGE_GRAN) ) return -EINVAL; =20 + /* + * Force all r/o subregions to be registered before initial domain + * creation, so that the emulation handlers can be added only when the= re + * are pages registered. + */ + if ( system_state >=3D SYS_STATE_smp_boot ) + { + ASSERT_UNREACHABLE(); + return -EILSEQ; + } + if ( !size ) return 0; =20 --=20 2.48.1 From nobody Fri Oct 31 16:34:17 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=reject dis=none) header.from=citrix.com ARC-Seal: i=1; a=rsa-sha256; t=1744903621; cv=none; d=zohomail.com; s=zohoarc; b=Kutj5PhUAfMK0KpgekrR0jpzb1KBcpARZR535+zlMElI1RJakvGSwLETp+ZHH88pDbW4bwAHUh2U/hvG9jiPDDoN+yU41p3Mf1+npBazmESGF9h4eSyVlsItp5KNWo1OLTMBUjb9eiYJMf+u2k8P9+LTNkFMS+7AuZpMfLZ1rJk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1744903621; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=XbVg709ccWDZ6LNYkLzgs4K1sN1hcawU+4pGGR/lOrY=; b=mgkl4D7gODSKC9UZcnAxK9xLflDiuCWfKkGFyrJQMg2hlHFXA4s8l4Ki34wRr3GGGi7+dCpCWRVcPU6e+OeNxauR601r4G3QwU2XhjZQPdZOTMyzhtCgW8D3H9N+xfgUAEFPhTserPkv9V4llOLNmDtgK9TGUVxG3g8Yk4DDB3c= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1744903621911764.2328251598256; Thu, 17 Apr 2025 08:27:01 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.958061.1351060 (Exim 4.92) (envelope-from ) id 1u5R8W-0000G1-66; Thu, 17 Apr 2025 15:26:44 +0000 Received: by outflank-mailman (output) from mailman id 958061.1351060; Thu, 17 Apr 2025 15:26:44 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1u5R8W-0000FQ-2c; Thu, 17 Apr 2025 15:26:44 +0000 Received: by outflank-mailman (input) for mailman id 958061; Thu, 17 Apr 2025 15:26:42 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1u5R8U-0008DN-Mv for xen-devel@lists.xenproject.org; Thu, 17 Apr 2025 15:26:42 +0000 Received: from mail-ed1-x52c.google.com (mail-ed1-x52c.google.com [2a00:1450:4864:20::52c]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 5c6ca37c-1ba0-11f0-9ffb-bf95429c2676; Thu, 17 Apr 2025 17:26:40 +0200 (CEST) Received: by mail-ed1-x52c.google.com with SMTP id 4fb4d7f45d1cf-5e677f59438so1332279a12.2 for ; Thu, 17 Apr 2025 08:26:40 -0700 (PDT) Received: from localhost ([84.78.159.3]) by smtp.gmail.com with UTF8SMTPSA id a640c23a62f3a-acb6ec046c0sm8618466b.32.2025.04.17.08.26.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Apr 2025 08:26:39 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version 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: 5c6ca37c-1ba0-11f0-9ffb-bf95429c2676 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=citrix.com; s=google; t=1744903600; x=1745508400; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=XbVg709ccWDZ6LNYkLzgs4K1sN1hcawU+4pGGR/lOrY=; b=DYtvnQB36EhoAFShWWXWqFf9rhdTQHwou26yY6DjHPDD1A3SKPF0LLS3pF6/q6v/i0 C8C9WJ0+Z6BTzJEho3dnbsGsfDSfXVlNZApgOUY1+YGwD1QK/L0cP1ndPiRiLz2F61qL S7x+4+q9XGcMVgHBF3isiyKkw2s+dJgFeUDpg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744903600; x=1745508400; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=XbVg709ccWDZ6LNYkLzgs4K1sN1hcawU+4pGGR/lOrY=; b=Ylo78t3EP+fvkY72rNdGj65AlqS0CSbUSUgBPIIB8bTtAL8GZb01eKfu8ZbgLCfQ00 1dQOCDixIUt7wXjFsCfSB/n7PyDaSJv8Vt14m6MCiSIPWtm5d50uiQEtB7RcUHq1WBKh tRshviuu9bx0lh/5eU2K/EjhxXc6Hxb3fiCJnMzwxSAFUxMNaG9hcYuOhjjTvfJTTb+M 8Rcy/yn0I1lNNMrY4fcIWm9KBbUqIVZM1q8a/FdD96e9PEqWQRP8WPLLobcw9E6GSwcI MZiSJLa9dBnwsoCf1jMsHUbqjiXXpMh/Jw5nm7K9zCFlgnxjKUXbEFfR9qv5VMP6nb+p 8BZQ== X-Gm-Message-State: AOJu0YzV49Or20BJOogYnTlbXYIsXPvc5NCnuGjXzRyR+6mr7BRBwk6M KnbsmbVjoY7cYZ/FqddMfv9LWhWxTjZd4AtST0rfpjNoDotjgtPlMTtwIc7qMg83dIj//FQdO95 l X-Gm-Gg: ASbGncv6OVIHjTPYNCQiyjNggYqUHF9FatyJFZC8o7fkjYSHK4l6RSg0XYN+CUxpT/T mFyuRwjt1IevHreN+tgJesNl2TETurKMNWPITQEIouHwKlObP/0GIKV1WQzHGk3p1PDsSajkAGb Sa8W8Zt0rMAa0XZYz/O0LL2PuAAAlNbWw9H/YjUNQdw4n6o//JCqYAmSQv5BwLn7xiYtVq85UWS wEO5j9CqhMxfAEUXc4nLKPGvb8nihgcZIRpIOdGrr3FNKC+Sag2jqmd8AWvkOjDqA4wvyIu3MTO ZbnXvwtDzG2QgiYGmqi4qjZcwBMDLP+q+H0WpyTruQ+kmA== X-Google-Smtp-Source: AGHT+IHzOJRIt25fRgmm3QpyMY/ZAku9BqBUeBglkH0FCpSzrhjAYXYwNSH/8eHTWn/UDlH18PrYVA== X-Received: by 2002:a17:907:3f0a:b0:aca:d6f0:af0c with SMTP id a640c23a62f3a-acb42c5fa5bmr441959566b.59.1744903599924; Thu, 17 Apr 2025 08:26:39 -0700 (PDT) From: Roger Pau Monne To: xen-devel@lists.xenproject.org Cc: Roger Pau Monne , Jan Beulich , Andrew Cooper Subject: [PATCH v3 4/4] x86/mm: move mmio_ro_emulated_write() to PV only file Date: Thu, 17 Apr 2025 17:25:14 +0200 Message-ID: <20250417152514.13702-5-roger.pau@citrix.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250417152514.13702-1-roger.pau@citrix.com> References: <20250417152514.13702-1-roger.pau@citrix.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @citrix.com) X-ZM-MESSAGEID: 1744903622246019000 mmio_ro_emulated_write() is only used in pv/ro-page-fault.c, move the function to that file and make it static. No functional change intended. Signed-off-by: Roger Pau Monn=C3=A9 Reviewed-by: Jan Beulich --- xen/arch/x86/include/asm/mm.h | 12 ------------ xen/arch/x86/mm.c | 26 ------------------------- xen/arch/x86/pv/ro-page-fault.c | 34 +++++++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 38 deletions(-) diff --git a/xen/arch/x86/include/asm/mm.h b/xen/arch/x86/include/asm/mm.h index aeb8ebcf2d56..2665daa6f74f 100644 --- a/xen/arch/x86/include/asm/mm.h +++ b/xen/arch/x86/include/asm/mm.h @@ -542,18 +542,6 @@ void memguard_unguard_stack(void *p); int subpage_mmio_ro_add(paddr_t start, size_t size); bool subpage_mmio_write_accept(mfn_t mfn, unsigned long gla); =20 -struct mmio_ro_emulate_ctxt { - unsigned long cr2; - /* Used only for mmcfg case */ - unsigned int seg, bdf; - /* Used only for non-mmcfg case */ - mfn_t mfn; -}; - -int cf_check mmio_ro_emulated_write( - enum x86_segment seg, unsigned long offset, void *p_data, - unsigned int bytes, struct x86_emulate_ctxt *ctxt); - /* r/o MMIO subpage access handlers. */ struct subpage_ro_range { struct list_head list; diff --git a/xen/arch/x86/mm.c b/xen/arch/x86/mm.c index 0e29bab03a95..9ac855f998dc 100644 --- a/xen/arch/x86/mm.c +++ b/xen/arch/x86/mm.c @@ -5144,32 +5144,6 @@ void subpage_mmio_write_emulate( goto write_ignored; } =20 -int cf_check mmio_ro_emulated_write( - enum x86_segment seg, - unsigned long offset, - void *p_data, - unsigned int bytes, - struct x86_emulate_ctxt *ctxt) -{ - struct mmio_ro_emulate_ctxt *mmio_ro_ctxt =3D ctxt->data; - unsigned long data =3D 0; - - /* Only allow naturally-aligned stores at the original %cr2 address. */ - if ( ((bytes | offset) & (bytes - 1)) || !bytes || - offset !=3D mmio_ro_ctxt->cr2 || bytes > sizeof(data) ) - { - gdprintk(XENLOG_WARNING, "bad access (cr2=3D%lx, addr=3D%lx, bytes= =3D%u)\n", - mmio_ro_ctxt->cr2, offset, bytes); - return X86EMUL_UNHANDLEABLE; - } - - memcpy(&data, p_data, bytes); - subpage_mmio_write_emulate(mmio_ro_ctxt->mfn, PAGE_OFFSET(offset), - data, bytes); - - return X86EMUL_OKAY; -} - /* * For these PTE APIs, the caller must follow the alloc-map-unmap-free * lifecycle, which means explicitly mapping the PTE pages before accessing diff --git a/xen/arch/x86/pv/ro-page-fault.c b/xen/arch/x86/pv/ro-page-faul= t.c index 11b01c479e43..3dd795288379 100644 --- a/xen/arch/x86/pv/ro-page-fault.c +++ b/xen/arch/x86/pv/ro-page-fault.c @@ -298,6 +298,14 @@ static int ptwr_do_page_fault(struct x86_emulate_ctxt = *ctxt, * fault handling for read-only MMIO pages */ =20 +struct mmio_ro_emulate_ctxt { + unsigned long cr2; + /* Used only for mmcfg case */ + unsigned int seg, bdf; + /* Used only for non-mmcfg case */ + mfn_t mfn; +}; + static int cf_check mmcfg_intercept_write( enum x86_segment seg, unsigned long offset, @@ -329,6 +337,32 @@ static int cf_check mmcfg_intercept_write( return X86EMUL_OKAY; } =20 +static int cf_check mmio_ro_emulated_write( + enum x86_segment seg, + unsigned long offset, + void *p_data, + unsigned int bytes, + struct x86_emulate_ctxt *ctxt) +{ + struct mmio_ro_emulate_ctxt *mmio_ro_ctxt =3D ctxt->data; + unsigned long data =3D 0; + + /* Only allow naturally-aligned stores at the original %cr2 address. */ + if ( ((bytes | offset) & (bytes - 1)) || !bytes || + offset !=3D mmio_ro_ctxt->cr2 || bytes > sizeof(data) ) + { + gdprintk(XENLOG_WARNING, "bad access (cr2=3D%lx, addr=3D%lx, bytes= =3D%u)\n", + mmio_ro_ctxt->cr2, offset, bytes); + return X86EMUL_UNHANDLEABLE; + } + + memcpy(&data, p_data, bytes); + subpage_mmio_write_emulate(mmio_ro_ctxt->mfn, PAGE_OFFSET(offset), + data, bytes); + + return X86EMUL_OKAY; +} + static const struct x86_emulate_ops mmio_ro_emulate_ops =3D { .read =3D x86emul_unhandleable_rw, .insn_fetch =3D ptwr_emulated_insn_fetch, --=20 2.48.1