From nobody Mon Feb 9 17:36:21 2026 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=1701683024; cv=none; d=zohomail.com; s=zohoarc; b=SESDyXD3cJ2EfbJsj6G+YaHQF46kgVbPUJJEO+4m4gGmMZUQg8h5SMn3RY5zzeQHYKihOq9OBoZh5NW8/13zQvgRX1HtrJwzwysQVJIPtNfQi40WFqjtY+na83VFAaeKNPE8LSiRK4akZDK4xsAsmIT4UOSGtbIxfpC1WHfdRvg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1701683024; 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=uhvbsGVggmpzL6VhFDTXJNRWZSL+PUA9PR1Ri8iXGLA=; b=f7gAt2b/KyfINUNJuaJbkT83gTmyGb3TznavK5GT1OGrXy/Y35cHBbNmOu14IGbVugerIrWjm5R/G88xlw6iM8aZfO/eRemtC1BAALrRmAjlB4T0BlQBiQ2MV7JpcB9RWCSUq5nlBSQzRzRFQsETlx7vp/dQ1kscxpPRUvl9TeY= 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 1701683024313200.9552518388664; Mon, 4 Dec 2023 01:43:44 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.646679.1009218 (Exim 4.92) (envelope-from ) id 1rA5Tx-00042f-2e; Mon, 04 Dec 2023 09:43:17 +0000 Received: by outflank-mailman (output) from mailman id 646679.1009218; Mon, 04 Dec 2023 09:43:17 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1rA5Tw-00042C-Sm; Mon, 04 Dec 2023 09:43:16 +0000 Received: by outflank-mailman (input) for mailman id 646679; Mon, 04 Dec 2023 09:43:15 +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 1rA5Tu-0003MS-VC for xen-devel@lists.xenproject.org; Mon, 04 Dec 2023 09:43:14 +0000 Received: from mail-wm1-x32b.google.com (mail-wm1-x32b.google.com [2a00:1450:4864:20::32b]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 8abe08a9-9289-11ee-9b0f-b553b5be7939; Mon, 04 Dec 2023 10:43:13 +0100 (CET) Received: by mail-wm1-x32b.google.com with SMTP id 5b1f17b1804b1-40c0e37cfafso1939595e9.0 for ; Mon, 04 Dec 2023 01:43:13 -0800 (PST) Received: from localhost ([213.195.113.99]) by smtp.gmail.com with ESMTPSA id jg23-20020a05600ca01700b0040b30be6244sm14376982wmb.24.2023.12.04.01.43.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Dec 2023 01:43:11 -0800 (PST) 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: 8abe08a9-9289-11ee-9b0f-b553b5be7939 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=citrix.com; s=google; t=1701682992; x=1702287792; 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=uhvbsGVggmpzL6VhFDTXJNRWZSL+PUA9PR1Ri8iXGLA=; b=CliS9MysGcFD0fTcqL/ZY5smT34J9ksrIrzqr5q94/gJXy2oU8Mzq7lVp0EogdMQGC eyIOI8NoY37snuQAYv/E/77IzGHiDKnsHkAfCnRhTA2oeqVB7C63OMyWwuFqcuB4Xumr BmrF1k9nUIx0Ixz8OztsnuBgIbHtb5Wpd8HjA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701682992; x=1702287792; 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=uhvbsGVggmpzL6VhFDTXJNRWZSL+PUA9PR1Ri8iXGLA=; b=arxf30jkxw+06ZkiByNjxbhcsp9658smc2WK2yV/1aQdB/Qr3S9GO4e10qR+x+Xfgt VYZwjer4FyVFGVMy72RDOxqg2Mm99VUL67TNcMb6EJoBnhj1FqdUTcmAl3VAIT2pZTBX OpwMX1wwqibEFX4jXfxx6RUwfcUUikpUyfi00CN/KuEgGqdrXObo0Sdkb7RTUOOiNf6B KnusvQWsnc+JMwG8YpliSQH6jkOGYOIRbVraf+XAWlveFH91sGRu6pJRQhJdg6/h+BfB Mm2WF1b1YzDOWugi4hkhQhK+Yr4GjXUKCuYlvtO9PV3qXzpGp/z48yLatrUud8HVgBD6 Mktg== X-Gm-Message-State: AOJu0YyEjpAW29EOB6nmRpcQPIPNV3/s/zbuWMSqv542wTk4Qm5YK30L YGajOpg9iDnXtzSVzXQStcd4582/loFrujFgDZE= X-Google-Smtp-Source: AGHT+IHGbm6svOID28oBbJ+PecxwhKiaRzO5RQK1pDm0gQJ0G0iWUsdq17d4C0+yP1bsmBSFLCBdoA== X-Received: by 2002:a05:600c:601a:b0:40b:5e59:c557 with SMTP id az26-20020a05600c601a00b0040b5e59c557mr2262756wmb.129.1701682991843; Mon, 04 Dec 2023 01:43:11 -0800 (PST) From: Roger Pau Monne To: xen-devel@lists.xenproject.org Cc: Roger Pau Monne , Jan Beulich , Paul Durrant Subject: [PATCH v2 3/6] x86/iommu: introduce a rangeset to perform hwdom IOMMU setup Date: Mon, 4 Dec 2023 10:43:02 +0100 Message-ID: <20231204094305.59267-4-roger.pau@citrix.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20231204094305.59267-1-roger.pau@citrix.com> References: <20231204094305.59267-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: 1701683026514100005 This change just introduces the boilerplate code in order to use a rangeset when setting up the hardware domain IOMMU mappings. The rangeset is never populated in this patch, so it's a non-functional change as far as the mapp= ings the domain gets established. Note there's a change for HVM domains (ie: PVH dom0) that will get switched= to create the p2m mappings using map_mmio_regions() instead of p2m_add_identity_entry(), so that ranges can be mapped with a single functi= on call if possible. Note that the interface of map_mmio_regions() doesn't al= low creating read-only mappings, but so far there are no such mappings created = for PVH dom0 in arch_iommu_hwdom_init(). Signed-off-by: Roger Pau Monn=C3=A9 --- Changes since v1: - Split from bigger patch. --- xen/drivers/passthrough/x86/iommu.c | 89 +++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/xen/drivers/passthrough/x86/iommu.c b/xen/drivers/passthrough/= x86/iommu.c index 857dccb6a465..531a428f6496 100644 --- a/xen/drivers/passthrough/x86/iommu.c +++ b/xen/drivers/passthrough/x86/iommu.c @@ -370,10 +370,77 @@ static unsigned int __hwdom_init hwdom_iommu_map(cons= t struct domain *d, return perms; } =20 +struct map_data { + struct domain *d; + unsigned int flush_flags; + bool ro; +}; + +static int __hwdom_init cf_check identity_map(unsigned long s, unsigned lo= ng e, + void *data) +{ + struct map_data *info =3D data; + struct domain *d =3D info->d; + long rc; + + if ( iommu_verbose ) + printk(XENLOG_INFO " [%010lx, %010lx] R%c\n", + s, e, info->ro ? 'O' : 'W'); + + if ( paging_mode_translate(d) ) + { + if ( info->ro ) + { + ASSERT_UNREACHABLE(); + return 0; + } + while ( (rc =3D map_mmio_regions(d, _gfn(s), e - s + 1, _mfn(s))) = > 0 ) + { + s +=3D rc; + process_pending_softirqs(); + } + } + else + { + const unsigned int perms =3D IOMMUF_readable | IOMMUF_preempt | + (info->ro ? 0 : IOMMUF_writable); + + if ( info->ro && !iomem_access_permitted(d, s, e) ) + { + /* + * Should be more fine grained in order to not map the forbidd= en + * frame instead of rejecting the region as a whole, but it's = only + * for read-only MMIO regions, which are very limited. + */ + printk(XENLOG_DEBUG + "IOMMU read-only mapping of region [%lx, %lx] forbidden= \n", + s, e); + return 0; + } + while ( (rc =3D iommu_map(d, _dfn(s), _mfn(s), e - s + 1, + perms, &info->flush_flags)) > 0 ) + { + s +=3D rc; + process_pending_softirqs(); + } + } + ASSERT(rc <=3D 0); + if ( rc ) + printk(XENLOG_WARNING + "IOMMU identity mapping of [%lx, %lx] failed: %ld\n", + s, e, rc); + + /* Ignore errors and attempt to map the remaining regions. */ + return 0; +} + void __hwdom_init arch_iommu_hwdom_init(struct domain *d) { unsigned long i, top, max_pfn, start, count; unsigned int flush_flags =3D 0, start_perms =3D 0; + struct rangeset *map; + struct map_data map_data =3D { .d =3D d }; + int rc; =20 BUG_ON(!is_hardware_domain(d)); =20 @@ -397,6 +464,10 @@ void __hwdom_init arch_iommu_hwdom_init(struct domain = *d) if ( iommu_hwdom_passthrough ) return; =20 + map =3D rangeset_new(NULL, NULL, 0); + if ( !map ) + panic("IOMMU init: unable to allocate rangeset\n"); + max_pfn =3D (GB(4) >> PAGE_SHIFT) - 1; top =3D max(max_pdx, pfn_to_pdx(max_pfn) + 1); =20 @@ -451,6 +522,24 @@ void __hwdom_init arch_iommu_hwdom_init(struct domain = *d) goto commit; } =20 + if ( iommu_verbose ) + printk(XENLOG_INFO "d%u: identity mappings for IOMMU:\n", + d->domain_id); + + rc =3D rangeset_report_ranges(map, 0, ~0UL, identity_map, &map_data); + if ( rc ) + panic("IOMMU unable to create mappings: %d\n", rc); + if ( is_pv_domain(d) ) + { + map_data.ro =3D true; + rc =3D rangeset_report_ranges(mmio_ro_ranges, 0, ~0UL, identity_ma= p, + &map_data); + if ( rc ) + panic("IOMMU unable to create read-only mappings: %d\n", rc); + } + + rangeset_destroy(map); + /* Use if to avoid compiler warning */ if ( iommu_iotlb_flush_all(d, flush_flags) ) return; --=20 2.43.0