From nobody Mon Nov 23 22:32:25 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=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1795122505; cv=none; d=zohomail.com; s=zohoarc; b=MO+P5mfbyAJ1Yd2jo0Dbi4HS3zhXy2qD6U/Mt0araFOIspvWDNU5y4wGXdBsqISOADws1glAgL4EDo3JYBmD6xj0onw1AW0V6vK/ak1cQsfHrH/O8QM9YRe/ZgAGT5LWIuD/2ZWXLA5x6tds9wFdy23NrY5c0V8G1HfHASn7Qnk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795122505; 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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=RlvWFjY3FA1UlZtoLIasB8sIzj16ovPz8d5rWMaMYrQ=; b=XDsxS50GFx0pjQmXUWelvd5u/voICDp4/+S8SzMUiKCvZF2reIwjEqHu+Sll2pbU4/es8swh3FcEPf+8BDl0yn2eJo3eYvG/Pu3VW1ZW9gxQ+f1m+KtQey1ludkTGfz+8LKEOsx82W+ImzIP/6sIgxCpLN1knAwXjWYce3ubApI= 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=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1795122505057147.74705348886732; Thu, 19 Nov 2026 13:08:25 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1497011.1695290 (Exim 4.92) (envelope-from ) id 1xU9MF-0000z4-54; Thu, 19 Nov 2026 21:07:51 +0000 Received: by outflank-mailman (output) from mailman id 1497011.1695290; Thu, 19 Nov 2026 21:07:51 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9MF-0000yv-27; Thu, 19 Nov 2026 21:07:51 +0000 Received: by outflank-mailman (input) for mailman id 1497011; Thu, 19 Nov 2026 21:07:49 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9MD-0000s0-7I for xen-devel@lists.xenproject.org; Thu, 19 Nov 2026 21:07:49 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xU9MC-00000006sow-1BeK; Thu, 19 Nov 2026 21:07:48 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xU9MC-0000000CVBH-1Gdd; Thu, 19 Nov 2026 21:07:48 +0000 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" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:Content-Type: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=RlvWFjY3FA1UlZtoLIasB8sIzj16ovPz8d5rWMaMYrQ=; b=jbSQKDjmsp/Kukm5vtVqPSTxwB VkcTauoiP2opD1HVTj40jfkVoPs4+YFsdCoRrLbX52kXLbi3m3K76sKx/XleDnWzSuRbbvfIFbrfn cPPYNUjPYGJhBK/nXhqhIbGjMvp1j3D/e32lh0E5uOwDu93e+WSqbBtVReD1ppDvOKyw=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie Subject: [PATCH v7 01/50] x86/mm: introduce populate_perdomain_mapping() Date: Thu, 19 Nov 2026 21:06:52 +0000 Message-ID: <20261119-asi-hvm-v7-1-6551b6d5c879@xenproject.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> References: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1795122506234158500 From: Roger Pau Monn=C3=A9 The per-domain area has central machinery for building its page-tables and for managing the backing pages it owns itself: create_perdomain_mapping() / destroy_perdomain_mapping(), used by the mapcache and the compat argument-translation area. What the interface lacks is a way to install a caller's own pages at a chosen address, which a per-vCPU state window, introduced by a later patch, needs: its users allocate their pages themselves and map them there. Introduce populate_perdomain_mapping(v, va, mfn, nr, flags) for this. It writes the given MFNs, with the given page-table flags, into v's view of the per-domain area, walking the per-domain page-table structures with transient mappings, so v need not be current. Callers don't need to know where the page-tables live or how the area is structured. We require the range to already have been populated down to the L1 tables by create_perdomain_mapping(). TLB flushing is left to the caller. A present entry not owned by the area (!_PAGE_AVAIL0) is replaced. A present entry owned by the area (_PAGE_AVAIL0, installed by create_perdomain_mapping() itself) is never expected: such a page is referenced only by the mapping, and no caller replaces area-owned backing. Should one ever do so, the page is leaked with a message rather than freed. The function is not for contexts with interrupts off: its users map their pages when allocating them. Signed-off-by: Roger Pau Monn=C3=A9 Assisted-by: Claude Code:claude-fable-5, Claude Code:claude-opus-4-8, Claud= e Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Changes in v7: - Standalone on staging, without per-vCPU per-domain areas: the domain's own L3, d->arch.perdomain_l3_pg, in place of perdomain_l3(); the message no longer speaks of an area of v's own. Changes in v5: - Comment above populate_perdomain_mapping(): the range lies within one sub-area (one PERDOMAIN_VIRT_SLOT()), not one "per-domain slot". - Message: "whether v has a per-domain area of its own or shares its domain's". Changes in v4: - Re-introduced into the series. - HVM only: the users are the state window's, all of which map their pages at allocation time, so drop the linear-mapping fast path and the IRQs-off walk that context-switch callers needed, along with the map_domain_page_irqoff() variants. --- xen/arch/x86/include/asm/mm.h | 3 ++ xen/arch/x86/mm.c | 62 +++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/xen/arch/x86/include/asm/mm.h b/xen/arch/x86/include/asm/mm.h index 1cdd30f7e2..5de04b32aa 100644 --- a/xen/arch/x86/include/asm/mm.h +++ b/xen/arch/x86/include/asm/mm.h @@ -610,6 +610,9 @@ struct page_info *allocate_perdomain_l3(struct domain *= d); int create_perdomain_mapping(struct domain *d, unsigned long va, unsigned int nr, l1_pgentry_t **pl1tab, struct page_info **ppg); +void populate_perdomain_mapping(const struct vcpu *v, unsigned long va, + const mfn_t *mfn, unsigned int nr, + unsigned int flags); void destroy_perdomain_mapping(const struct vcpu *v, unsigned long va, unsigned int nr); void free_perdomain_mappings(struct domain *d); diff --git a/xen/arch/x86/mm.c b/xen/arch/x86/mm.c index 2a01bc235e..9e313e2a82 100644 --- a/xen/arch/x86/mm.c +++ b/xen/arch/x86/mm.c @@ -6356,6 +6356,68 @@ int create_perdomain_mapping(struct domain *d, unsig= ned long va, return rc; } =20 +/* + * Map @nr pages, @mfn[0..nr-1], at consecutive pages from @va in v's view= of + * the per-domain area, with page-table @flags. The range must lie within= a + * single sub-area (one PERDOMAIN_VIRT_SLOT()), and must already have been + * plumbed down to the L1 tables by create_perdomain_mapping(). Present + * entries are replaced, and no TLB flushing is done. + * + * The walk maps the per-domain page-table pages transiently, so v need not + * be current. It is not for contexts with interrupts disabled. + */ +void populate_perdomain_mapping(const struct vcpu *v, unsigned long va, + const mfn_t *mfn, unsigned int nr, + unsigned int flags) +{ + struct page_info *l3_pg =3D v->domain->arch.perdomain_l3_pg; + const l3_pgentry_t *l3tab; + const l2_pgentry_t *l2tab; + l1_pgentry_t *l1tab =3D NULL; + unsigned int i; + + ASSERT(va >=3D PERDOMAIN_VIRT_START && + va < PERDOMAIN_VIRT_SLOT(PERDOMAIN_SLOTS)); + ASSERT(!nr || !l3_table_offset(va ^ (va + nr * PAGE_SIZE - 1))); + ASSERT(local_irq_is_enabled()); + + if ( !nr ) + return; + + BUG_ON(!l3_pg); + l3tab =3D __map_domain_page(l3_pg); + BUG_ON(!(l3e_get_flags(l3tab[l3_table_offset(va)]) & _PAGE_PRESENT)); + l2tab =3D map_l2t_from_l3e(l3tab[l3_table_offset(va)]); + unmap_domain_page(l3tab); + + for ( i =3D 0; i < nr; i++, va +=3D PAGE_SIZE ) + { + l1_pgentry_t *pl1e; + + if ( !l1tab || !l1_table_offset(va) ) + { + const l2_pgentry_t *pl2e =3D &l2tab[l2_table_offset(va)]; + + unmap_domain_page(l1tab); + BUG_ON(!(l2e_get_flags(*pl2e) & _PAGE_PRESENT)); + l1tab =3D map_l1t_from_l2e(*pl2e); + } + + pl1e =3D &l1tab[l1_table_offset(va)]; + if ( perdomain_l1e_needs_freeing(*pl1e) ) + { + printk(XENLOG_ERR + "%pv: leaking area-owned page %"PRI_mfn" at %#lx\n", + v, mfn_x(l1e_get_mfn(*pl1e)), va); + ASSERT_UNREACHABLE(); + } + l1e_write(pl1e, l1e_from_mfn(mfn[i], flags)); + } + + unmap_domain_page(l1tab); + unmap_domain_page(l2tab); +} + void destroy_perdomain_mapping(const struct vcpu *v, unsigned long va, unsigned int nr) { --=20 2.56.0 From nobody Mon Nov 23 22:32:25 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=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1795122497; cv=none; d=zohomail.com; s=zohoarc; b=USL1CpTvtbzLMs1Je7I6Pq66EEyjQF5h22JO4iNkFFdv4pR5G7nyyQzlFRjbjDl7n2A24AhF3NNYb1ZXKgdTqwUDyCAXcaPHekYgqmPzeqbMa3naEhJvjeR5H8meXNFvRAejiKc7TZLKmMIq2lp70XiBy4phSVQdNd8+rhI2zAM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795122497; h=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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=eWMtDPgJrHXBwMZpbsBQzy6ElcDHbQygGOMEssB+fO8=; b=TsR/UjcbkXmuHr4jIPrOPKzpmge1ITfDZUT9WtHji7/58BzzmNVtcAbokedNBbu78RCHjSpbeKLETe5Ib8s0UsNyJAy3OIn+KuUnykcgDKfY85ktLCJ55zmS4iGReX068jnfj6N+Hr9K0NEn4WtxE3D/FuKUKV7CEuwjW5X+G8c= 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=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1795122497650398.94689620599013; Thu, 19 Nov 2026 13:08:17 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1497012.1695296 (Exim 4.92) (envelope-from ) id 1xU9MF-00011V-El; Thu, 19 Nov 2026 21:07:51 +0000 Received: by outflank-mailman (output) from mailman id 1497012.1695296; Thu, 19 Nov 2026 21:07:51 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9MF-00010e-8h; Thu, 19 Nov 2026 21:07:51 +0000 Received: by outflank-mailman (input) for mailman id 1497012; Thu, 19 Nov 2026 21:07:49 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9MD-0000xZ-OE for xen-devel@lists.xenproject.org; Thu, 19 Nov 2026 21:07:49 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xU9MD-00000006sp4-0e0o; Thu, 19 Nov 2026 21:07:49 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xU9MD-0000000CVBH-0w4X; Thu, 19 Nov 2026 21:07:49 +0000 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" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=eWMtDPgJrHXBwMZpbsBQzy6ElcDHbQygGOMEssB+fO8=; b=rRJYE0sZH+rfoGHsC9sQtjIBj5 npQrHfZVB6+AZZlHdVPL73ShhN29EOEypCHB1vCXy4J01v/qdHsNzq7H9IurUncjxvPIfn71pI4Ip 722lDG6f747JBbykyYmfnoIlE7KdRBLDop7sWyvtp7xvJYlbssCHSGa7Y8aKy4aCeVnw=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie Subject: [PATCH v7 02/50] x86/mm: introduce a per-vCPU state window in the perdomain area Date: Thu, 19 Nov 2026 21:06:53 +0000 Message-ID: <20261119-asi-hvm-v7-2-6551b6d5c879@xenproject.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> References: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1795122498272158500 Content-Type: text/plain; charset="utf-8" Guest-owned register state that Xen keeps in always-mapped memory, on the xenheap or in the xmalloc pool, is to move to the domain heap, reached through mappings of its own rather than through the directmap. The long-lived per-vCPU structures involved (the SVM VMCB, the VMX guest MSR load/save list, the guest XSAVE area, and later the aggregated small per-vCPU allocations) all want the same shape: one or more domain-heap pages, mapped once at fixed per-vCPU virtual addresses for the life of the vCPU. Add a fourth per-domain-mapping sub-area providing that destination: a small per-vCPU stride of page slots, with a fixed index assigned to each relocated structure and the page tables created at vCPU creation time. Consumers allocate their own pages and install them with populate_perdomain_mapping(); the backing pages remain caller-owned, so their lifetime stays with the consumer, while the page tables are torn down with the rest of the per-domain area. Before freeing their pages, consumers take them out of the window with unmap_vcpu_state(), on failure paths as at teardown: vCPU creation can fail once a consumer has mapped its pages, and the domain lives on. It flushes the domain's dirty CPUs: the window is in the per-domain area that all of the domain's vCPUs share, so these are the CPUs whose page-tables carry it. So far that mask is empty on every call, as the vCPU has not run or the domain is paused. A consumer reaching a page from another context, through a transient map_domain_page() mapping, drops the mapping with unmap_vcpu_state_page(), which also flushes this CPU's translations of it, there and at its alias in the per-domain mirror: the mapcache defers that flush, and the translations would otherwise outlive the mapping. The window has the same domain-wide visibility as the other per-domain sub-areas: each vCPU's stride, at virtual addresses keyed on vcpu_id, is mapped in the page tables of all of the domain's vCPUs. This is infrastructure for moving the SVM VMCB, the VMX MSR load/save lists, the saved register frames, the HVM emulator's caches and the guest XSAVE areas off the xenheap. Slots specific to AMD or Intel can share the same space. Nothing occupies the area yet; the only behavioural change is the page-table allocation itself and the failure path that comes with it: vCPU creation can now fail with -ENOMEM. Both apply to every guest vCPU, PV ones included. A domain gets one L2 plus an L1 per 64 vCPUs: 2 pages for up to 64 vCPUs. A PV domain's window stays empty until the register frames and XSAVE areas move there. Assisted-by: Claude Code:claude-fable-5, Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Changes in v7: - Standalone on staging, without per-vCPU page tables or the sparse directmap view: create_perdomain_mapping() is given the domain, and the comments and the message describe the domain-wide area and staging's mapcache alone. The window's comment in config.h says its structures are kept on the domain heap, not out of always-mapped memory. - Have AMD- and Intel-specific sections that share the same slots (Jan Beul= ich) Changes in v6: - Port over change to L3 allocation. Changes in v5: - Say that the PV register frames move into the window too, not only the XSAVE areas. - Message and comments: "per-domain area", and which vCPUs share it. Changes in v4: - New in this version. --- xen/arch/x86/domain.c | 12 +++++++++ xen/arch/x86/include/asm/config.h | 23 ++++++++++++++++- xen/arch/x86/include/asm/mm.h | 2 ++ xen/arch/x86/mm.c | 42 +++++++++++++++++++++++++++++++ 4 files changed, 78 insertions(+), 1 deletion(-) diff --git a/xen/arch/x86/domain.c b/xen/arch/x86/domain.c index 74513a79a7..2407c575d9 100644 --- a/xen/arch/x86/domain.c +++ b/xen/arch/x86/domain.c @@ -517,6 +517,18 @@ int arch_vcpu_create(struct vcpu *v) =20 if ( !is_idle_domain(d) ) { + BUILD_BUG_ON((MAX_VIRT_CPUS << VCPU_STATE_VA_SHIFT) > + (PERDOMAIN_SLOT_MBYTES << 20)); + BUILD_BUG_ON(VCPU_STATE_IDX_VMCB >=3D VCPU_STATE_NR_PAGES); + BUILD_BUG_ON(VCPU_STATE_IDX_N2VMCB >=3D VCPU_STATE_NR_PAGES); + BUILD_BUG_ON(VCPU_STATE_IDX_VMX_MSR >=3D VCPU_STATE_NR_PAGES); + /* Page tables only; torn down with the rest of the per-domain are= a. */ + rc =3D create_perdomain_mapping(d, VCPU_STATE_START(v), + VCPU_STATE_NR_PAGES, + NIL(l1_pgentry_t *), NULL); + if ( rc !=3D 0 ) + goto fail; + paging_vcpu_init(v); =20 if ( (rc =3D vcpu_init_fpu(v)) !=3D 0 ) diff --git a/xen/arch/x86/include/asm/config.h b/xen/arch/x86/include/asm/c= onfig.h index aebd3e8f45..b05dbd4a4b 100644 --- a/xen/arch/x86/include/asm/config.h +++ b/xen/arch/x86/include/asm/config.h @@ -158,7 +158,7 @@ /* Slot 260: per-domain mappings (including map cache). */ #define PERDOMAIN_VIRT_START (PML4_ADDR(260)) #define PERDOMAIN_SLOT_MBYTES (PML4_ENTRY_BYTES >> (20 + PAGETABLE_ORDER= )) -#define PERDOMAIN_SLOTS 3 +#define PERDOMAIN_SLOTS 4 #define PERDOMAIN_VIRT_SLOT(s) (PERDOMAIN_VIRT_START + (s) * \ (PERDOMAIN_SLOT_MBYTES << 20)) /* Slot 4: mirror of per-domain mappings (for compat xlat area accesses). = */ @@ -291,6 +291,27 @@ extern unsigned long xen_phys_start; #define ARG_XLAT_START(v) \ (ARG_XLAT_VIRT_START + ((v)->vcpu_id << ARG_XLAT_VA_SHIFT)) =20 +/* + * Per-vCPU state window. The fourth per-domain-mapping sub-area: a small + * per-vCPU stride of page slots holding long-lived per-vCPU structures ke= pt + * on the domain heap, each mapped once for the life of the vCPU at a fixed + * index within the stride. + */ +#define VCPU_STATE_SLOT 3 +#define VCPU_STATE_VIRT_START PERDOMAIN_VIRT_SLOT(VCPU_STATE_SLOT) +#define VCPU_STATE_ORDER 3 +#define VCPU_STATE_NR_PAGES (1U << VCPU_STATE_ORDER) +#define VCPU_STATE_VA_SHIFT (VCPU_STATE_ORDER + PAGE_SHIFT) +#define VCPU_STATE_START(v) \ + (VCPU_STATE_VIRT_START + ((v)->vcpu_id << VCPU_STATE_VA_SHIFT)) +#define VCPU_STATE_ADDR(v, idx) \ + (VCPU_STATE_START(v) + (idx) * PAGE_SIZE) +/* AMD-specific slots */ +#define VCPU_STATE_IDX_VMCB 0 +#define VCPU_STATE_IDX_N2VMCB 1 +/* Intel-specific slots */ +#define VCPU_STATE_IDX_VMX_MSR 0 + #define ELFSIZE 64 =20 #define ARCH_CRASH_SAVE_VMCOREINFO diff --git a/xen/arch/x86/include/asm/mm.h b/xen/arch/x86/include/asm/mm.h index 5de04b32aa..bbce8c401e 100644 --- a/xen/arch/x86/include/asm/mm.h +++ b/xen/arch/x86/include/asm/mm.h @@ -615,6 +615,8 @@ void populate_perdomain_mapping(const struct vcpu *v, u= nsigned long va, unsigned int flags); void destroy_perdomain_mapping(const struct vcpu *v, unsigned long va, unsigned int nr); +void unmap_vcpu_state(const struct vcpu *v, unsigned int idx, unsigned int= nr); +void unmap_vcpu_state_page(const void *va); void free_perdomain_mappings(struct domain *d); =20 void __iomem *ioremap_wc(paddr_t pa, size_t len); diff --git a/xen/arch/x86/mm.c b/xen/arch/x86/mm.c index 9e313e2a82..7cfeceb8c6 100644 --- a/xen/arch/x86/mm.c +++ b/xen/arch/x86/mm.c @@ -6470,6 +6470,48 @@ void destroy_perdomain_mapping(const struct vcpu *v,= unsigned long va, unmap_domain_page(l3tab); } =20 +/* + * Take @nr slots of @v's state window, from @idx, out of the per-domain + * area, before the caller frees the pages behind them: the pages are the + * caller's own, and must not stay mapped once freed. The window's + * translations are usable only on CPUs whose page-tables carry it: those = of + * any of the domain's vCPUs, all in the domain's dirty mask. + */ +void unmap_vcpu_state(const struct vcpu *v, unsigned int idx, unsigned int= nr) +{ + const struct domain *d =3D v->domain; + + /* Beyond the stride is the next vCPU's window. */ + BUG_ON(idx >=3D VCPU_STATE_NR_PAGES || nr > VCPU_STATE_NR_PAGES - idx); + + destroy_perdomain_mapping(v, VCPU_STATE_ADDR(v, idx), nr); + + flush_tlb_mask(d->dirty_cpumask); +} + +/* + * Drop a transient mapping of a page behind a vCPU's state window, and th= is + * CPU's translations of it: at its address, and at its alias in the + * per-domain mirror, which every domain but a 64-bit PV one has (flushing + * that address there is harmless). The mapcache does not drop them with + * the slot's last mapping: it defers the flush, and CPUs running other vC= PUs + * of its domain may have cached the slot too. A directmap address, where + * the caller has no mapcache, needs nothing. + */ +void unmap_vcpu_state_page(const void *va) +{ + unsigned long addr =3D (unsigned long)va; + + unmap_domain_page(va); + + if ( addr < DIRECTMAP_VIRT_START ) + { + flush_tlb_one_local(addr); + flush_tlb_one_local(addr - PERDOMAIN_VIRT_START + + PERDOMAIN_ALT_VIRT_START); + } +} + void free_perdomain_mappings(struct domain *d) { l3_pgentry_t *l3tab; --=20 2.56.0 From nobody Mon Nov 23 22:32:25 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=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1795122504; cv=none; d=zohomail.com; s=zohoarc; b=UXaDP93dfQOP7+GK+IJ9UAnjEgjZfj9S1loAZNxmZ+sBrO6GlaTPGOSgLAWXfsMxULgvDkUk0hetlDKIjdgnzFrgYqysmwGcuMsLkLGOXSeyw8F9zez7ZW8SdcQWl73sjG7AVO0KomuMlr9ydmVN2uWGN8/40oosDLVHJO+K+x4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795122504; h=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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=ALf2ZDHIfDIpPP1rzyeJjYBq7kEVHWK/9UnsOPcRlu8=; b=nKI8M2scWyt7vS4yKJdRBV44Del4Kd8vC73KkxpIOS0Ouj+Q2nsCnFelpRpFKISONEenFQaDEuOtWt+rsoqnZgordB4PgZ4jnHPPrSE1UfyJkFXIO61LXSbHXbCnrSE0/kAmYietBezn2e7iuNVM1dYnJ303rOrs6Fr4NDVczy0= 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=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1795122504752104.89920527330742; Thu, 19 Nov 2026 13:08:24 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1497013.1695308 (Exim 4.92) (envelope-from ) id 1xU9MG-0001O6-Lm; Thu, 19 Nov 2026 21:07:52 +0000 Received: by outflank-mailman (output) from mailman id 1497013.1695308; Thu, 19 Nov 2026 21:07:52 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9MG-0001Nw-IN; Thu, 19 Nov 2026 21:07:52 +0000 Received: by outflank-mailman (input) for mailman id 1497013; Thu, 19 Nov 2026 21:07:51 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9MF-00011E-8w for xen-devel@lists.xenproject.org; Thu, 19 Nov 2026 21:07:51 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xU9ME-00000006spF-20GU; Thu, 19 Nov 2026 21:07:50 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xU9ME-0000000CVBH-1RNc; Thu, 19 Nov 2026 21:07:50 +0000 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" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=ALf2ZDHIfDIpPP1rzyeJjYBq7kEVHWK/9UnsOPcRlu8=; b=g8MBxWCBXv94o+/B6mYk491jZs cqaood92EzoNJ2d47FY786WACm0SkjxLu2uT8Roz5Ph0jEKgjsDGTUKH5HpBTZWcYV0Al9e+1MRL5 0LnLhuGwyTHXZnCD6xA+e7AIxRddEuoUQ8cgEOvlbusF5B/HP2T7xTK7OsMVUcvtyrb8=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Ross Lagerwall , Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Jason Andryuk , Teddy Astie Subject: [PATCH v7 03/50] x86/svm: introduce SVM_MAP_VMCB() and SVM_UNMAP_VMCB() Date: Thu, 19 Nov 2026 21:06:54 +0000 Message-ID: <20261119-asi-hvm-v7-3-6551b6d5c879@xenproject.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> References: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1795122506275158500 Content-Type: text/plain; charset="utf-8" Some accessors of a vCPU's VMCB can run with that vCPU paused, from another vCPU's context: context save/load, the segment and CPL getters, PAT/TSC/intercept updates reachable from toolstack operations, vm_event, the 'v' debug key, and construct_vmcb() itself, which runs before the vCPU ever entered guest context. svm_nested_features_on_efer_update() is among them: it is reachable remotely via hvm_load_cpu_ctxt() -> svm_update_guest_efer(). Route them through the new SVM_MAP_VMCB() and SVM_UNMAP_VMCB(); construct_vmcb() takes the VMCB from its caller. svm_set_tsc_offset() needs both VMCBs rather than just the active one, so it maps them individually via SVM_MAP_VMCB_ONE(). A vCPU stays in nested guest mode across a deschedule or a pause, so this path can run remotely with the L2 VMCB active; guest mode does not imply v =3D=3D current. Hot vmexit-path accesses all have v =3D=3D current and keep dereferencing svm->vmcb directly. SVM_MAP_VMCB(), SVM_MAP_VMCB_ONE() and SVM_UNMAP_VMCB() are macros over svm_vmcb_map(), svm_vmcb_map_one() and svm_vmcb_unmap(), so that SVM_UNMAP_VMCB() can clear the caller's pointer, as VCPU_UNMAP_XSAVE_AREA() does. No functional change: the helpers return the VMCB's existing address. They prepare for moving VMCBs off the xenheap, behind a mapping for use in their own vCPU's context only. Assisted-by: Claude Code:claude-fable-5, Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap --- CC: Ross Lagerwall Changes in v7: - Rebase over "x86/svm: Drop 'struct' from the vmcb_struct type". - Message: VMCBs move off the xenheap, behind a mapping for use in their own vCPU's context only. Changes in v5: - Map and unmap through SVM_MAP_VMCB(), SVM_MAP_VMCB_ONE() and SVM_UNMAP_VMCB(), which clears the pointer (George). - Retitle (was: "x86/svm: introduce svm_vmcb_map() and svm_vmcb_unmap()"). Changes in v4: - New in this version. --- xen/arch/x86/hvm/svm/nestedsvm.c | 4 +- xen/arch/x86/hvm/svm/svm.c | 133 +++++++++++++++++++++++-------- xen/arch/x86/hvm/svm/vmcb.c | 29 ++++++- xen/arch/x86/hvm/svm/vmcb.h | 18 +++++ 4 files changed, 145 insertions(+), 39 deletions(-) diff --git a/xen/arch/x86/hvm/svm/nestedsvm.c b/xen/arch/x86/hvm/svm/nested= svm.c index 366178ff54..fcd422a21e 100644 --- a/xen/arch/x86/hvm/svm/nestedsvm.c +++ b/xen/arch/x86/hvm/svm/nestedsvm.c @@ -1521,7 +1521,7 @@ void svm_vmexit_do_clgi(struct cpu_user_regs *regs, s= truct vcpu *v) */ void svm_nested_features_on_efer_update(struct vcpu *v) { - struct vmcb *vmcb =3D v->arch.hvm.svm.vmcb; + struct vmcb *vmcb =3D SVM_MAP_VMCB(v); struct nestedsvm *svm =3D &vcpu_nestedsvm(v); u32 general2_intercepts; vintr_t vintr; @@ -1576,6 +1576,8 @@ void svm_nested_features_on_efer_update(struct vcpu *= v) vmcb_set_general2_intercepts(vmcb, general2_intercepts); } } + + SVM_UNMAP_VMCB(v, vmcb); } =20 void __init start_nested_svm(struct hvm_function_table *hvm_function_table) diff --git a/xen/arch/x86/hvm/svm/svm.c b/xen/arch/x86/hvm/svm/svm.c index bfca1eda4a..00315e30d0 100644 --- a/xen/arch/x86/hvm/svm/svm.c +++ b/xen/arch/x86/hvm/svm/svm.c @@ -107,7 +107,7 @@ static void cf_check svm_cpu_down(void) static void cf_check svm_update_guest_cr( struct vcpu *v, unsigned int cr, unsigned int flags) { - struct vmcb *vmcb =3D v->arch.hvm.svm.vmcb; + struct vmcb *vmcb =3D SVM_MAP_VMCB(v); uint64_t value; =20 switch ( cr ) @@ -173,11 +173,13 @@ static void cf_check svm_update_guest_cr( default: BUG(); } + + SVM_UNMAP_VMCB(v, vmcb); } =20 static void cf_check svm_update_guest_efer(struct vcpu *v) { - struct vmcb *vmcb =3D v->arch.hvm.svm.vmcb; + struct vmcb *vmcb =3D SVM_MAP_VMCB(v); unsigned long guest_efer =3D v->arch.hvm.guest_efer, xen_efer =3D read_efer(); =20 @@ -204,6 +206,7 @@ static void cf_check svm_update_guest_efer(struct vcpu = *v) guest_efer |=3D EFER_SVME; =20 vmcb_set_efer(vmcb, guest_efer); + SVM_UNMAP_VMCB(v, vmcb); =20 ASSERT(nestedhvm_enabled(v->domain) || !nsvm_efer_svm_enabled(v)); @@ -352,7 +355,7 @@ static void svm_restore_dr(struct vcpu *v) =20 static int svm_vmcb_save(struct vcpu *v, struct hvm_hw_cpu *c) { - struct vmcb *vmcb =3D v->arch.hvm.svm.vmcb; + struct vmcb *vmcb =3D SVM_MAP_VMCB(v); =20 if ( vmcb->event_inj.v && hvm_event_needs_reinjection(vmcb->event_inj.type, @@ -362,13 +365,15 @@ static int svm_vmcb_save(struct vcpu *v, struct hvm_h= w_cpu *c) c->error_code =3D vmcb->event_inj.ec; } =20 + SVM_UNMAP_VMCB(v, vmcb); + return 1; } =20 static int svm_vmcb_restore(struct vcpu *v, struct hvm_hw_cpu *c) { struct page_info *page =3D NULL; - struct vmcb *vmcb =3D v->arch.hvm.svm.vmcb; + struct vmcb *vmcb; struct p2m_domain *p2m =3D p2m_get_hostp2m(v->domain); =20 if ( c->pending_valid ) @@ -417,6 +422,8 @@ static int svm_vmcb_restore(struct vcpu *v, struct hvm_= hw_cpu *c) svm_update_guest_cr(v, 0, 0); svm_update_guest_cr(v, 4, 0); =20 + vmcb =3D SVM_MAP_VMCB(v); + if ( !paging_mode_shadow(v->domain) ) { vmcb_set_np(vmcb, true); @@ -436,6 +443,8 @@ static int svm_vmcb_restore(struct vcpu *v, struct hvm_= hw_cpu *c) vmcb->event_inj.raw =3D 0; =20 vmcb->cleanbits.raw =3D 0; + SVM_UNMAP_VMCB(v, vmcb); + paging_update_paging_modes(v); =20 return 0; @@ -443,7 +452,7 @@ static int svm_vmcb_restore(struct vcpu *v, struct hvm_= hw_cpu *c) =20 static void svm_save_cpu_state(struct vcpu *v, struct hvm_hw_cpu *data) { - struct vmcb *vmcb =3D v->arch.hvm.svm.vmcb; + const struct vmcb *vmcb =3D SVM_MAP_VMCB(v); =20 data->sysenter_cs =3D vmcb->sysenter_cs; data->sysenter_esp =3D vmcb->sysenter_esp; @@ -453,11 +462,13 @@ static void svm_save_cpu_state(struct vcpu *v, struct= hvm_hw_cpu *data) data->msr_star =3D vmcb->star; data->msr_cstar =3D vmcb->cstar; data->msr_syscall_mask =3D vmcb->sfmask; + + SVM_UNMAP_VMCB(v, vmcb); } =20 static void svm_load_cpu_state(struct vcpu *v, struct hvm_hw_cpu *data) { - struct vmcb *vmcb =3D v->arch.hvm.svm.vmcb; + struct vmcb *vmcb =3D SVM_MAP_VMCB(v); =20 vmcb->lstar =3D data->msr_lstar; vmcb->star =3D data->msr_star; @@ -467,6 +478,9 @@ static void svm_load_cpu_state(struct vcpu *v, struct h= vm_hw_cpu *data) vmcb->sysenter_cs =3D data->sysenter_cs; vmcb->sysenter_esp =3D data->sysenter_esp; vmcb->sysenter_eip =3D data->sysenter_eip; + + SVM_UNMAP_VMCB(v, vmcb); + v->arch.hvm.guest_efer =3D data->msr_efer; svm_update_guest_efer(v); } @@ -492,7 +506,7 @@ static int cf_check svm_load_vmcb_ctxt(struct vcpu *v, = struct hvm_hw_cpu *ctxt) =20 static unsigned cf_check int svm_get_interrupt_shadow(struct vcpu *v) { - struct vmcb *vmcb =3D v->arch.hvm.svm.vmcb; + struct vmcb *vmcb =3D SVM_MAP_VMCB(v); unsigned int intr_shadow =3D 0; =20 if ( vmcb->int_stat.intr_shadow ) @@ -503,13 +517,15 @@ static unsigned cf_check int svm_get_interrupt_shadow= (struct vcpu *v) : (vmcb_get_general1_intercepts(vmcb) & GENERAL1_INTERCEPT_IRET) ) intr_shadow |=3D HVM_INTR_SHADOW_NMI; =20 + SVM_UNMAP_VMCB(v, vmcb); + return intr_shadow; } =20 static void cf_check svm_set_interrupt_shadow( struct vcpu *v, unsigned int intr_shadow) { - struct vmcb *vmcb =3D v->arch.hvm.svm.vmcb; + struct vmcb *vmcb =3D SVM_MAP_VMCB(v); bool block_nmi =3D intr_shadow & HVM_INTR_SHADOW_NMI; vintr_t vintr =3D vmcb_get_vintr(vmcb); =20 @@ -531,10 +547,13 @@ static void cf_check svm_set_interrupt_shadow( =20 vmcb_set_general1_intercepts(vmcb, gen1); } + + SVM_UNMAP_VMCB(v, vmcb); } =20 static int cf_check svm_guest_x86_mode(struct vcpu *v) { + /* Current only (see guest_cpu_user_regs() below): no mapping needed. = */ struct vmcb *vmcb =3D v->arch.hvm.svm.vmcb; =20 if ( unlikely(!(v->arch.hvm.guest_cr[0] & X86_CR0_PE)) ) @@ -548,8 +567,7 @@ static int cf_check svm_guest_x86_mode(struct vcpu *v) =20 static void cf_check svm_cpuid_policy_changed(struct vcpu *v) { - struct svm_vcpu *svm =3D &v->arch.hvm.svm; - struct vmcb *vmcb =3D svm->vmcb; + struct vmcb *vmcb =3D SVM_MAP_VMCB(v); const struct cpu_policy *cp =3D v->domain->arch.cpu_policy; u32 bitmap =3D vmcb_get_exception_intercepts(vmcb); =20 @@ -559,6 +577,7 @@ static void cf_check svm_cpuid_policy_changed(struct vc= pu *v) bitmap &=3D ~(1U << X86_EXC_UD); =20 vmcb_set_exception_intercepts(vmcb, bitmap); + SVM_UNMAP_VMCB(v, vmcb); =20 /* Give access to MSR_SPEC_CTRL if the guest has been told about it. */ svm_intercept_msr(v, MSR_SPEC_CTRL, @@ -602,13 +621,18 @@ void svm_sync_vmcb(struct vcpu *v, enum vmcb_sync_sta= te new_state) =20 static unsigned int cf_check svm_get_cpl(struct vcpu *v) { - return vmcb_get_cpl(v->arch.hvm.svm.vmcb); + const struct vmcb *vmcb =3D SVM_MAP_VMCB(v); + unsigned int cpl =3D vmcb_get_cpl(vmcb); + + SVM_UNMAP_VMCB(v, vmcb); + + return cpl; } =20 static void cf_check svm_get_segment_register( struct vcpu *v, enum x86_segment seg, struct segment_register *reg) { - struct vmcb *vmcb =3D v->arch.hvm.svm.vmcb; + struct vmcb *vmcb =3D SVM_MAP_VMCB(v); =20 ASSERT((v =3D=3D current) || !vcpu_runnable(v)); =20 @@ -649,12 +673,14 @@ static void cf_check svm_get_segment_register( *reg =3D (struct segment_register){}; break; } + + SVM_UNMAP_VMCB(v, vmcb); } =20 static void cf_check svm_set_segment_register( struct vcpu *v, enum x86_segment seg, struct segment_register *reg) { - struct vmcb *vmcb =3D v->arch.hvm.svm.vmcb; + struct vmcb *vmcb =3D SVM_MAP_VMCB(v); =20 ASSERT((v =3D=3D current) || !vcpu_runnable(v)); =20 @@ -683,6 +709,7 @@ static void cf_check svm_set_segment_register( default: ASSERT_UNREACHABLE(); domain_crash(v->domain); + SVM_UNMAP_VMCB(v, vmcb); return; } =20 @@ -720,33 +747,41 @@ static void cf_check svm_set_segment_register( ASSERT_UNREACHABLE(); break; } + + SVM_UNMAP_VMCB(v, vmcb); } =20 static int cf_check svm_set_guest_pat(struct vcpu *v, u64 gpat) { - struct vmcb *vmcb =3D v->arch.hvm.svm.vmcb; + struct vmcb *vmcb; =20 if ( paging_mode_shadow(v->domain) ) return 0; =20 + vmcb =3D SVM_MAP_VMCB(v); vmcb_set_g_pat(vmcb, gpat); + SVM_UNMAP_VMCB(v, vmcb); + return 1; } =20 static int cf_check svm_get_guest_pat(struct vcpu *v, u64 *gpat) { - struct vmcb *vmcb =3D v->arch.hvm.svm.vmcb; + const struct vmcb *vmcb; =20 if ( paging_mode_shadow(v->domain) ) return 0; =20 + vmcb =3D SVM_MAP_VMCB(v); *gpat =3D vmcb_get_g_pat(vmcb); + SVM_UNMAP_VMCB(v, vmcb); + return 1; } =20 static void cf_check svm_set_tsc_offset(struct vcpu *v, uint64_t offset) { - struct vmcb *vmcb =3D v->arch.hvm.svm.vmcb; + struct vmcb *vmcb =3D SVM_MAP_VMCB(v); struct vmcb *n1vmcb, *n2vmcb; uint64_t n2_tsc_offset =3D 0; struct domain *d =3D v->domain; @@ -754,25 +789,36 @@ static void cf_check svm_set_tsc_offset(struct vcpu *= v, uint64_t offset) if ( !nestedhvm_enabled(d) ) { vmcb_set_tsc_offset(vmcb, offset); + SVM_UNMAP_VMCB(v, vmcb); return; } =20 - n1vmcb =3D vcpu_nestedhvm(v).nv_n1vmcx; - n2vmcb =3D vcpu_nestedhvm(v).nv_n2vmcx; - + /* + * A vCPU stays in nested guest mode across a deschedule or a pause, so + * this can run remotely with the L2 VMCB active: map both explicitly. + */ if ( nestedhvm_vcpu_in_guestmode(v) ) { + const struct nestedvcpu *nv =3D &vcpu_nestedhvm(v); + + n1vmcb =3D SVM_MAP_VMCB_ONE(v, nv->nv_n1vmcx, nv->nv_n1vmcx_pa); + n2vmcb =3D SVM_MAP_VMCB_ONE(v, nv->nv_n2vmcx, nv->nv_n2vmcx_pa); + n2_tsc_offset =3D vmcb_get_tsc_offset(n2vmcb) - vmcb_get_tsc_offset(n1vmcb); vmcb_set_tsc_offset(n1vmcb, offset); + + SVM_UNMAP_VMCB(v, n2vmcb); + SVM_UNMAP_VMCB(v, n1vmcb); } =20 vmcb_set_tsc_offset(vmcb, offset + n2_tsc_offset); + SVM_UNMAP_VMCB(v, vmcb); } =20 static void cf_check svm_set_rdtsc_exiting(struct vcpu *v, bool enable) { - struct vmcb *vmcb =3D v->arch.hvm.svm.vmcb; + struct vmcb *vmcb =3D SVM_MAP_VMCB(v); u32 general1_intercepts =3D vmcb_get_general1_intercepts(vmcb); u32 general2_intercepts =3D vmcb_get_general2_intercepts(vmcb); =20 @@ -787,13 +833,14 @@ static void cf_check svm_set_rdtsc_exiting(struct vcp= u *v, bool enable) =20 vmcb_set_general1_intercepts(vmcb, general1_intercepts); vmcb_set_general2_intercepts(vmcb, general2_intercepts); + SVM_UNMAP_VMCB(v, vmcb); } =20 #ifdef CONFIG_VM_EVENT static void cf_check svm_set_descriptor_access_exiting( struct vcpu *v, bool enable) { - struct vmcb *vmcb =3D v->arch.hvm.svm.vmcb; + struct vmcb *vmcb =3D SVM_MAP_VMCB(v); u32 general1_intercepts =3D vmcb_get_general1_intercepts(vmcb); u32 mask =3D GENERAL1_INTERCEPT_IDTR_READ | GENERAL1_INTERCEPT_GDTR_RE= AD | GENERAL1_INTERCEPT_LDTR_READ | GENERAL1_INTERCEPT_TR_READ @@ -806,6 +853,7 @@ static void cf_check svm_set_descriptor_access_exiting( general1_intercepts &=3D ~mask; =20 vmcb_set_general1_intercepts(vmcb, general1_intercepts); + SVM_UNMAP_VMCB(v, vmcb); } #endif /* CONFIG_VM_EVENT */ =20 @@ -1363,7 +1411,12 @@ static void cf_check svm_inject_event(const struct x= 86_event *event) =20 static bool cf_check svm_event_pending(const struct vcpu *v) { - return v->arch.hvm.svm.vmcb->event_inj.v; + const struct vmcb *vmcb =3D SVM_MAP_VMCB(v); + bool pending =3D vmcb->event_inj.v; + + SVM_UNMAP_VMCB(v, vmcb); + + return pending; } =20 static void cf_check svm_cpu_dead(unsigned int cpu) @@ -2321,45 +2374,55 @@ static void cf_check svm_invlpg(struct vcpu *v, uns= igned long linear) static bool cf_check svm_get_pending_event( struct vcpu *v, struct x86_event *info) { - const struct vmcb *vmcb =3D v->arch.hvm.svm.vmcb; + const struct vmcb *vmcb =3D SVM_MAP_VMCB(v); + bool pending =3D vmcb->event_inj.v; =20 - if ( !vmcb->event_inj.v ) - return false; + if ( pending ) + { + info->vector =3D vmcb->event_inj.vector; + info->type =3D vmcb->event_inj.type; + info->error_code =3D vmcb->event_inj.ec; + } =20 - info->vector =3D vmcb->event_inj.vector; - info->type =3D vmcb->event_inj.type; - info->error_code =3D vmcb->event_inj.ec; + SVM_UNMAP_VMCB(v, vmcb); =20 - return true; + return pending; } =20 static uint64_t cf_check svm_get_reg(struct vcpu *v, unsigned int reg) { struct vcpu *curr =3D current; - const struct vmcb *vmcb =3D v->arch.hvm.svm.vmcb; + const struct vmcb *vmcb =3D SVM_MAP_VMCB(v); struct domain *d =3D v->domain; + uint64_t val =3D 0; =20 switch ( reg ) { case MSR_SPEC_CTRL: - return vmcb->spec_ctrl; + val =3D vmcb->spec_ctrl; + break; =20 case MSR_SHADOW_GS_BASE: if ( v =3D=3D curr ) svm_sync_vmcb(v, vmcb_in_sync); - return vmcb->kerngsbase; + val =3D vmcb->kerngsbase; + break; =20 default: printk(XENLOG_G_ERR "%s(%pv, 0x%08x) Bad register\n", __func__, v, reg); domain_crash(d); - return 0; + break; } + + SVM_UNMAP_VMCB(v, vmcb); + + return val; } =20 static void cf_check svm_set_reg(struct vcpu *v, unsigned int reg, uint64_= t val) { - struct vmcb *vmcb =3D v->arch.hvm.svm.vmcb; + struct vmcb *vmcb =3D SVM_MAP_VMCB(v); struct domain *d =3D v->domain; =20 switch ( reg ) @@ -2374,6 +2437,8 @@ static void cf_check svm_set_reg(struct vcpu *v, unsi= gned int reg, uint64_t val) domain_crash(d); break; } + + SVM_UNMAP_VMCB(v, vmcb); } =20 static struct hvm_function_table __initdata_cf_clobber svm_function_table = =3D { diff --git a/xen/arch/x86/hvm/svm/vmcb.c b/xen/arch/x86/hvm/svm/vmcb.c index 65b7bdf925..5c893fde95 100644 --- a/xen/arch/x86/hvm/svm/vmcb.c +++ b/xen/arch/x86/hvm/svm/vmcb.c @@ -43,11 +43,25 @@ void free_vmcb(struct vmcb *vmcb) free_xenheap_page(vmcb); } =20 +struct vmcb *svm_vmcb_map_one(const struct vcpu *v, + struct vmcb *va, paddr_t pa) +{ + return va; +} + +struct vmcb *svm_vmcb_map(const struct vcpu *v) +{ + return svm_vmcb_map_one(v, v->arch.hvm.svm.vmcb, v->arch.hvm.svm.vmcb_= pa); +} + +void svm_vmcb_unmap(const struct vcpu *v, const struct vmcb *vmcb) +{ +} + /* This function can directly access fields which are covered by clean bit= s. */ -static int construct_vmcb(struct vcpu *v) +static int construct_vmcb(struct vcpu *v, struct vmcb *vmcb) { struct svm_vcpu *svm =3D &v->arch.hvm.svm; - struct vmcb *vmcb =3D svm->vmcb; =20 vmcb->_general1_intercepts =3D GENERAL1_INTERCEPT_INTR | GENERAL1_INTERCEPT_NMI | @@ -201,6 +215,7 @@ int svm_create_vmcb(struct vcpu *v) { struct nestedvcpu *nv =3D &vcpu_nestedhvm(v); struct svm_vcpu *svm =3D &v->arch.hvm.svm; + struct vmcb *vmcb; int rc; =20 if ( (nv->nv_n1vmcx =3D=3D NULL) && @@ -211,7 +226,10 @@ int svm_create_vmcb(struct vcpu *v) } =20 svm->vmcb =3D nv->nv_n1vmcx; - rc =3D construct_vmcb(v); + + vmcb =3D SVM_MAP_VMCB(v); + rc =3D construct_vmcb(v, vmcb); + SVM_UNMAP_VMCB(v, vmcb); if ( rc !=3D 0 ) { free_vmcb(nv->nv_n1vmcx); @@ -407,6 +425,7 @@ static void cf_check vmcb_dump(unsigned char ch) { struct domain *d; struct vcpu *v; + struct vmcb *vmcb; =20 printk("*********** VMCB Areas **************\n"); =20 @@ -425,7 +444,9 @@ static void cf_check vmcb_dump(unsigned char ch) continue; } printk("\tVCPU %d\n", v->vcpu_id); - svm_vmcb_dump("key_handler", v->arch.hvm.svm.vmcb); + vmcb =3D SVM_MAP_VMCB(v); + svm_vmcb_dump("key_handler", vmcb); + SVM_UNMAP_VMCB(v, vmcb); =20 process_pending_softirqs(); } diff --git a/xen/arch/x86/hvm/svm/vmcb.h b/xen/arch/x86/hvm/svm/vmcb.h index ba10ff9854..2ffb6b1b99 100644 --- a/xen/arch/x86/hvm/svm/vmcb.h +++ b/xen/arch/x86/hvm/svm/vmcb.h @@ -539,6 +539,24 @@ struct vmcb { struct vmcb *alloc_vmcb(void); void free_vmcb(struct vmcb *vmcb); =20 +/* + * Access a VMCB of a vCPU that may not be current. SVM_MAP_VMCB() takes + * the active VMCB, SVM_MAP_VMCB_ONE() a specific one. + */ +struct vmcb *svm_vmcb_map_one(const struct vcpu *v, + struct vmcb *va, paddr_t pa); +struct vmcb *svm_vmcb_map(const struct vcpu *v); +void svm_vmcb_unmap(const struct vcpu *v, const struct vmcb *vmcb); + +#define SVM_MAP_VMCB(v) svm_vmcb_map(v) +#define SVM_MAP_VMCB_ONE(v, va, pa) svm_vmcb_map_one(v, va, pa) + +/* Drops the mapping of a VMCB and nullifies its pointer. */ +#define SVM_UNMAP_VMCB(v, vmcb) do { \ + svm_vmcb_unmap(v, vmcb); \ + (vmcb) =3D NULL; \ +} while ( false ) + int svm_create_vmcb(struct vcpu *v); void svm_destroy_vmcb(struct vcpu *v); =20 --=20 2.56.0 From nobody Mon Nov 23 22:32:25 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=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1795122495; cv=none; d=zohomail.com; s=zohoarc; b=MVzbfJw3gAROUJhpjQ45Qo43k2bWJZ4QSb5GIrk1cczFbI/VwJhWD2GxX2HQcyT6n7HG9uOyPTbR55dT6kgc4AdaHW34J+88BruIDCs/q4rWLYqG3eobt0cr0fcmsROLOKf2Mtqhld5NE96ohoN/9Ero92iM8xuaVRTQK1/7UBQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795122495; h=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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=zLHsYZMmLdbgys4XIsnrAsiGwoLasz6qTkDC55i0Da0=; b=W8y1DLwO5TlKy+ROC3uu/xVJauTELsy8ge2wjIuTFlxwsNm3ynzLyRaGzBkGy8mQpV/WbHQYirj24ZEPhT4wElBRIpYfPls+bIDKGTD2uv4SQZpDExAyw6PPUIXpO2D46kXaoHgoe7skQCDmHO5Iwts28SzDwArpfArOGeM+jBE= 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=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1795122495112484.3052766635882; Thu, 19 Nov 2026 13:08:15 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1497014.1695318 (Exim 4.92) (envelope-from ) id 1xU9MI-0001dC-3r; Thu, 19 Nov 2026 21:07:54 +0000 Received: by outflank-mailman (output) from mailman id 1497014.1695318; Thu, 19 Nov 2026 21:07:54 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9MH-0001d1-W0; Thu, 19 Nov 2026 21:07:53 +0000 Received: by outflank-mailman (input) for mailman id 1497014; Thu, 19 Nov 2026 21:07:52 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9MG-0001Nr-G1 for xen-devel@lists.xenproject.org; Thu, 19 Nov 2026 21:07:52 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xU9MF-00000006spN-1nDX; Thu, 19 Nov 2026 21:07:51 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xU9MF-0000000CVBH-20X1; Thu, 19 Nov 2026 21:07:51 +0000 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" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=zLHsYZMmLdbgys4XIsnrAsiGwoLasz6qTkDC55i0Da0=; b=QWUaqxcTjZ8y75wsqSaEm+3Hes oJDwFVrx+7eiu9ptjV7z2INHbqbcF9KBdRC3RSoVuU4ogy49gVUx0PIeOn/h/mjxNzJqebAOlQO5z F4PSx02sJf0Y8NjzhL/DigO3N845QZ3tliDy6sIylYSjqAJshB2dtnS8dGyd4l/k4FZw=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Ross Lagerwall , Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Jason Andryuk , Teddy Astie Subject: [PATCH v7 04/50] x86/svm: restore guest debug registers in svm_do_resume() Date: Thu, 19 Nov 2026 21:06:55 +0000 Message-ID: <20261119-asi-hvm-v7-4-6551b6d5c879@xenproject.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> References: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1795122496044158500 Content-Type: text/plain; charset="utf-8" svm_ctxt_switch_to() restores the guest's debug registers when its DR7 enables any of them, which also updates the VMCB. Do it in svm_do_resume() instead, which follows it whenever the vCPU is scheduled in. svm_do_resume() also runs when a vCPU keeps running on its pCPU (continue_running()), without svm_ctxt_switch_to(). Where the guest's debug registers are already loaded, the restore does nothing: flag_dr_dirty records that they are, and only svm_save_dr() clears it, from svm_ctxt_switch_from(). This prepares for moving VMCBs off the xenheap and behind the vCPU state window: svm_ctxt_switch_to() runs before write_ptbase(), where the incoming vCPU's VMCB need not be mapped. Not yet run with a guest using debug registers. Assisted-by: Claude Code:claude-fable-5, Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap --- CC: Ross Lagerwall Changes in v7: - Message: VMCBs move off the xenheap; before write_ptbase() the incoming vCPU's VMCB need not be mapped. Changes in v4: - New in this version. --- xen/arch/x86/hvm/svm/svm.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xen/arch/x86/hvm/svm/svm.c b/xen/arch/x86/hvm/svm/svm.c index 00315e30d0..6419f3011e 100644 --- a/xen/arch/x86/hvm/svm/svm.c +++ b/xen/arch/x86/hvm/svm/svm.c @@ -957,8 +957,6 @@ static void cf_check svm_ctxt_switch_to(struct vcpu *v) */ disable_each_ist(per_cpu(idt, cpu)); =20 - svm_restore_dr(v); - svm_tsc_ratio_load(v); =20 if ( cpu_has_msr_tsc_aux ) @@ -982,6 +980,8 @@ static void noreturn cf_check svm_do_resume(void) bool vcpu_guestmode =3D false; struct vlapic *vlapic =3D vcpu_vlapic(v); =20 + svm_restore_dr(v); + if ( nestedhvm_enabled(v->domain) && nestedhvm_vcpu_in_guestmode(v) ) vcpu_guestmode =3D 1; =20 --=20 2.56.0 From nobody Mon Nov 23 22:32:25 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=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1795122496; cv=none; d=zohomail.com; s=zohoarc; b=CLJyYk1mmtuqCKf+1xwHwSbSbvZVNRKqNmFhgjDKurhxYJzrfabIKDaLZZcsJb9CqwwDeVvUEsACSQ4FugTH13OsWnE7MOjIFA2J/cPhFhQbMGblhkN/MTFXHSWreUvB3a3m7nV4j651MxuN1hK20CdWUXd1zfCjKe2fuFUI2vM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795122496; h=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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=1skpsV1b2ZOyD148I7gOD6OP0JnbmLzJMFRON5POrPo=; b=C9rHGwl0xQgx9KVCBDZe1yP9yAae7wBdXoFF2HorPVs0/cx/l9k0syRkYqsmlkdxibLMZyzwQZ9zEoTaud6PKgP+13yjnRt1LAH3k/+wL8DBoHVzXGTvLgOSajMC1HxTWC/3nsqjz9MfNdXYohP8F+IUOOF+/TfFr3eD7q632TY= 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=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1795122496888316.43556564632456; Thu, 19 Nov 2026 13:08:16 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1497015.1695326 (Exim 4.92) (envelope-from ) id 1xU9MJ-0001rK-Au; Thu, 19 Nov 2026 21:07:55 +0000 Received: by outflank-mailman (output) from mailman id 1497015.1695326; Thu, 19 Nov 2026 21:07:55 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9MJ-0001r8-7O; Thu, 19 Nov 2026 21:07:55 +0000 Received: by outflank-mailman (input) for mailman id 1497015; Thu, 19 Nov 2026 21:07:53 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9MH-0001aD-Ge for xen-devel@lists.xenproject.org; Thu, 19 Nov 2026 21:07:53 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xU9MG-00000006spW-2BnM; Thu, 19 Nov 2026 21:07:52 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xU9MG-0000000CVBH-2WfO; Thu, 19 Nov 2026 21:07:52 +0000 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" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=1skpsV1b2ZOyD148I7gOD6OP0JnbmLzJMFRON5POrPo=; b=JG0I/F5EzYqvTpHBS1JmgwaZOb eVJ7Nm5CeyE7yzv6kSBXnCC9OO0OjX1m/DEXF/0NMyH82ZTLL+Km5HrgezsiEsKA4ggPO1E/MuJ6B GUExyHQAmZz9WHdRI4B5NhnPGIHqraeI/ZwQoIlhM0wOJAlUQ3P6qzUx5NpccNJSYm1U=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Ross Lagerwall , Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Jason Andryuk , Teddy Astie Subject: [PATCH v7 05/50] x86/svm: Pass struct vcpu to svm_vmcb_dump() Date: Thu, 19 Nov 2026 21:06:56 +0000 Message-ID: <20261119-asi-hvm-v7-5-6551b6d5c879@xenproject.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> References: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1795122498216158501 Content-Type: text/plain; charset="utf-8" svm_vmcb_dump() prints virt_to_maddr() of the VMCB it's passed, which is right only while VMCBs are reached through the directmap. Pass instead the vcpu, fish out the physical address from that, and map it ourselves. This prepares for moving VMCBs off the xenheap. Assisted-by: Claude Code:claude-fable-5, Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap --- CC: Ross Lagerwall Changes in v7: - Rebase over "x86/svm: Drop 'struct' from the vmcb_struct type". - Pass `struct vcpu` so that we can print the paddr (Jan Beulich). - Retitle (was: "x86/svm: drop svm_vmcb_dump()'s paddr"). - Assert in svm_vmexit_handler() that its vmcb is still the vCPU's before dumping it. - Message: VMCBs move off the xenheap. Changes in v5: - Keep VCPU_svm_vmcb: svm_asm_do_resume() now uses it, since df8d101a3854 ("x86/svm: Work around VMRUN's corruption of VMCB.INTR_SHADOW") in staging. - Retitle (was: "x86/svm: drop svm_vmcb_dump()'s paddr and the unused VCPU_svm_vmcb"). Changes in v4: - New in this version. --- xen/arch/x86/hvm/svm/svm.c | 3 ++- xen/arch/x86/hvm/svm/vmcb.c | 19 ++++++++++++------- xen/arch/x86/hvm/svm/vmcb.h | 2 +- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/xen/arch/x86/hvm/svm/svm.c b/xen/arch/x86/hvm/svm/svm.c index 6419f3011e..a846328802 100644 --- a/xen/arch/x86/hvm/svm/svm.c +++ b/xen/arch/x86/hvm/svm/svm.c @@ -2693,7 +2693,8 @@ void asmlinkage svm_vmexit_handler(void) if ( unlikely(exit_reason =3D=3D VMEXIT_INVALID) ) { gdprintk(XENLOG_ERR, "invalid VMCB state:\n"); - svm_vmcb_dump(__func__, vmcb); + ASSERT(vmcb =3D=3D v->arch.hvm.svm.vmcb); + svm_vmcb_dump(__func__, v); domain_crash(v->domain); goto out; } diff --git a/xen/arch/x86/hvm/svm/vmcb.c b/xen/arch/x86/hvm/svm/vmcb.c index 5c893fde95..bbeee4ec67 100644 --- a/xen/arch/x86/hvm/svm/vmcb.c +++ b/xen/arch/x86/hvm/svm/vmcb.c @@ -268,20 +268,26 @@ static void svm_dump_sel(const char *name, const stru= ct segment_register *s) name, s->sel, s->attr, s->limit, s->base); } =20 -void svm_vmcb_dump(const char *from, const struct vmcb *vmcb) +void svm_vmcb_dump(const char *from, const struct vcpu *v) { struct vcpu *curr =3D current; + paddr_t pa =3D v->arch.hvm.svm.vmcb_pa; + struct vmcb *vmcb =3D SVM_MAP_VMCB_ONE(v, v->arch.hvm.svm.vmcb, pa); =20 /* * If we are dumping the VMCB currently in context, some guest state m= ay * still be cached in hardware. Retrieve it. */ - if ( vmcb =3D=3D curr->arch.hvm.svm.vmcb ) + if ( v =3D=3D curr ) svm_sync_vmcb(curr, vmcb_in_sync); =20 printk("Dumping guest's current state at %s...\n", from); + /* + * Always print the vaddr from the vcpu struct; the local vmcb + * pointer variable may be a transient mapping. + */ printk("Size of VMCB =3D %zu, paddr =3D %"PRIpaddr", vaddr =3D %p\n", - sizeof(struct vmcb), virt_to_maddr(vmcb), vmcb); + sizeof(struct vmcb), pa, v->arch.hvm.svm.vmcb); =20 printk("cr_intercepts =3D %#x dr_intercepts =3D %#x " "exception_intercepts =3D %#x\n", @@ -343,6 +349,8 @@ void svm_vmcb_dump(const char *from, const struct vmcb = *vmcb) svm_dump_sel("LDTR", &vmcb->ldtr); svm_dump_sel("IDTR", &vmcb->idtr); svm_dump_sel(" TR", &vmcb->tr); + + SVM_UNMAP_VMCB(v, vmcb); } =20 bool svm_vmcb_isvalid( @@ -425,7 +433,6 @@ static void cf_check vmcb_dump(unsigned char ch) { struct domain *d; struct vcpu *v; - struct vmcb *vmcb; =20 printk("*********** VMCB Areas **************\n"); =20 @@ -444,9 +451,7 @@ static void cf_check vmcb_dump(unsigned char ch) continue; } printk("\tVCPU %d\n", v->vcpu_id); - vmcb =3D SVM_MAP_VMCB(v); - svm_vmcb_dump("key_handler", vmcb); - SVM_UNMAP_VMCB(v, vmcb); + svm_vmcb_dump("key_handler", v); =20 process_pending_softirqs(); } diff --git a/xen/arch/x86/hvm/svm/vmcb.h b/xen/arch/x86/hvm/svm/vmcb.h index 2ffb6b1b99..070a44ff6c 100644 --- a/xen/arch/x86/hvm/svm/vmcb.h +++ b/xen/arch/x86/hvm/svm/vmcb.h @@ -561,7 +561,7 @@ int svm_create_vmcb(struct vcpu *v); void svm_destroy_vmcb(struct vcpu *v); =20 void setup_vmcb_dump(void); -void svm_vmcb_dump(const char *from, const struct vmcb *vmcb); +void svm_vmcb_dump(const char *from, const struct vcpu *v); bool svm_vmcb_isvalid(const char *from, const struct vmcb *vmcb, const struct vcpu *v, bool verbose); =20 --=20 2.56.0 From nobody Mon Nov 23 22:32:25 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=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1795122497; cv=none; d=zohomail.com; s=zohoarc; b=M3Ga8xMJBiBbbIKWIawy9ttTLVgAmdkD081tu+Y17wl3u+n2cpPpmIv3kE0ryhipEzqRQ+VZ92zfaZoiJ+ionYs0/QgCSD6HYMzbLFwJ2I5NW/WC0CmhxlpO4VOvBB6rNzQ1FHSkKa5Xs/R7Ee0FfM8quFvzmObeOajVzxX3L8I= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795122497; h=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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=QX6LPitNcaMMSHbFzwtqwxuSj8fohpVVckzWpV8PEuI=; b=h1+qvS4/Qj1hfDR0R8yjqqR5frGAt8M3L09PJGNdMOFL6Y1mg7GyC+BWmwqWL4zMlKS++vsDx6iPSIZ7WFTrFB0dO2P3vKxtuYUoMbuOyCwpz/SmsHC0KrFb6PeBV4OvzG3DDokCsD/GNgzRWlWGd/E40QXuUV94HKy8+2w+fgQ= 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=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1795122497495613.822293687418; Thu, 19 Nov 2026 13:08:17 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1497016.1695335 (Exim 4.92) (envelope-from ) id 1xU9MK-00025w-JX; Thu, 19 Nov 2026 21:07:56 +0000 Received: by outflank-mailman (output) from mailman id 1497016.1695335; Thu, 19 Nov 2026 21:07:56 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9MK-000253-FZ; Thu, 19 Nov 2026 21:07:56 +0000 Received: by outflank-mailman (input) for mailman id 1497016; Thu, 19 Nov 2026 21:07:54 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9MI-0001jJ-FS for xen-devel@lists.xenproject.org; Thu, 19 Nov 2026 21:07:54 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xU9MH-00000006spf-2nct; Thu, 19 Nov 2026 21:07:53 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xU9MH-0000000CVBH-350g; Thu, 19 Nov 2026 21:07:53 +0000 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" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=QX6LPitNcaMMSHbFzwtqwxuSj8fohpVVckzWpV8PEuI=; b=YmDCvsAkaWtLAEz3XiDfi1d0f8 yD5u3/hWWSMFiHAYaqpKZuAGHXY9XSCDLJBUxMH9bsZMndbewvTVhBYNA3yVD2zciMAumJkVN7PSY p+WRIFpu0Uv0vTGkIZDOxzWZ1dCoz4vjAJ/kbJqLvy0aEKLULTNifVgW0Gb4xdVC8S3Y=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Ross Lagerwall , Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Jason Andryuk , Teddy Astie Subject: [PATCH v7 06/50] x86/svm: allocate VMCBs from the domain heap behind the vCPU state window Date: Thu, 19 Nov 2026 21:06:57 +0000 Message-ID: <20261119-asi-hvm-v7-6-6551b6d5c879@xenproject.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> References: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1795122498271158500 Content-Type: text/plain; charset="utf-8" The VMCB holds the full guest register save area, and lives on the xenheap. Move it to the domain heap and map it at a fixed slot in the owning vCPU's state window. Hardware is unaffected: VMRUN, VMLOAD and VMSAVE address the VMCB physically, and vmcb_pa now derives from the page rather than from virt_to_maddr() on a directmap VA. A vCPU can have two VMCBs: the primary (nv_n1vmcx) and the nested L2 one (nv_n2vmcx). Both move, taking the first two slots in the window stride. The nested code's swapping of svm->vmcb / svm->vmcb_pa between the two works unchanged, now swapping between two window VAs. free_vmcb() takes a VMCB out of the window with unmap_vcpu_state() before freeing the page: the window is in the per-domain area that all its domain's vCPUs share, which outlives a vCPU whose creation fails. The window VA is for use in the owning vCPU's context only. SVM_MAP_VMCB() and SVM_MAP_VMCB_ONE() return it for v =3D=3D current, and a transient map_domain_page() mapping otherwise, which SVM_UNMAP_VMCB() drops with unmap_vcpu_state_page(), along with this CPU's translations of it. svm_ctxt_switch_to() no longer touches the VMCB, as it runs before write_ptbase(); svm_ctxt_switch_from() keeps dereferencing the window VA, as the outgoing page tables are still loaded at that point. svm_asm_do_resume() reads the VMCB's interrupt shadow through the window VA just before VMRUN, with the vCPU's own page tables loaded. Assisted-by: Claude Code:claude-fable-5, Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap --- CC: Ross Lagerwall Changes in v7: - Rebase over "x86/svm: Drop 'struct' from the vmcb_struct type". - Standalone on staging, without per-vCPU page tables or the sparse directmap view: the message and two comments say that the window VA is for use in the owning vCPU's context only, and need not be mapped before write_ptbase(), as the domain's vCPUs share the window. Changes in v5: - Describe the mapping in terms of SVM_MAP_VMCB(), SVM_MAP_VMCB_ONE() and SVM_UNMAP_VMCB(), which clears the pointer (George). - v4's 26/30, "x86/svm: clear the VMCB clean bits in svm_ctxt_switch_from()", is dropped. Ross's "x86/svm: Don't clear clean bits on every context switch", now in staging as 69633efbca3c, removes the clearing from svm_ctxt_switch_to() instead. - svm_asm_do_resume() now reads the VMCB through svm.vmcb (staging's "x86/svm: Work around VMRUN's corruption of VMCB.INTR_SHADOW"); it runs in the vCPU's own context, where the window VA is valid. Changes in v4: - New in this version. --- xen/arch/x86/hvm/svm/nestedsvm.c | 8 +++--- xen/arch/x86/hvm/svm/svm.c | 10 ++++++++ xen/arch/x86/hvm/svm/vmcb.c | 43 ++++++++++++++++++++++---------- xen/arch/x86/hvm/svm/vmcb.h | 15 ++++++++--- 4 files changed, 56 insertions(+), 20 deletions(-) diff --git a/xen/arch/x86/hvm/svm/nestedsvm.c b/xen/arch/x86/hvm/svm/nested= svm.c index fcd422a21e..aad878e0b7 100644 --- a/xen/arch/x86/hvm/svm/nestedsvm.c +++ b/xen/arch/x86/hvm/svm/nestedsvm.c @@ -71,10 +71,9 @@ int cf_check nsvm_vcpu_initialise(struct vcpu *v) goto err; memset(msrpm, 0x0, MSRPM_SIZE); =20 - nv->nv_n2vmcx =3D alloc_vmcb(); + nv->nv_n2vmcx =3D alloc_vmcb(v, VCPU_STATE_IDX_N2VMCB, &nv->nv_n2vmcx_= pa); if ( nv->nv_n2vmcx =3D=3D NULL ) goto err; - nv->nv_n2vmcx_pa =3D virt_to_maddr(nv->nv_n2vmcx); =20 return 0; =20 @@ -95,7 +94,10 @@ void cf_check nsvm_vcpu_destroy(struct vcpu *v) * of l1 vmcb page. */ if ( nv->nv_n1vmcx ) + { v->arch.hvm.svm.vmcb =3D nv->nv_n1vmcx; + v->arch.hvm.svm.vmcb_pa =3D nv->nv_n1vmcx_pa; + } =20 if ( svm->ns_cached_msrpm ) { @@ -113,7 +115,7 @@ void cf_check nsvm_vcpu_destroy(struct vcpu *v) =20 if ( nv->nv_n2vmcx ) { - free_vmcb(nv->nv_n2vmcx); + free_vmcb(v, VCPU_STATE_IDX_N2VMCB, nv->nv_n2vmcx_pa); nv->nv_n2vmcx =3D NULL; nv->nv_n2vmcx_pa =3D INVALID_PADDR; } diff --git a/xen/arch/x86/hvm/svm/svm.c b/xen/arch/x86/hvm/svm/svm.c index a846328802..c4d91947ba 100644 --- a/xen/arch/x86/hvm/svm/svm.c +++ b/xen/arch/x86/hvm/svm/svm.c @@ -265,6 +265,10 @@ static void cf_check svm_enable_msr_interception(struc= t domain *d, uint32_t msr) =20 static void svm_save_dr(struct vcpu *v) { + /* + * @v is the outgoing vCPU, whose page tables are still loaded: the + * VMCB's window VA serves, although current has moved on. + */ struct vmcb *vmcb =3D v->arch.hvm.svm.vmcb; unsigned int flag_dr_dirty =3D v->arch.hvm.flag_dr_dirty; =20 @@ -944,6 +948,12 @@ static void cf_check svm_ctxt_switch_to(struct vcpu *v) { int cpu =3D smp_processor_id(); =20 + /* + * NB we cannot access the VMCB here: this runs before + * write_ptbase(), so the incoming vCPU's state window need not + * be reachable yet. + */ + /* * This is required, because VMRUN does consistency check and some of = the * DOM0 selectors are pointing to invalid GDT locations, and cause AMD diff --git a/xen/arch/x86/hvm/svm/vmcb.c b/xen/arch/x86/hvm/svm/vmcb.c index bbeee4ec67..420ec7f85e 100644 --- a/xen/arch/x86/hvm/svm/vmcb.c +++ b/xen/arch/x86/hvm/svm/vmcb.c @@ -6,6 +6,7 @@ * */ =20 +#include #include #include #include @@ -23,30 +24,40 @@ #include "svm.h" #include "vmcb.h" =20 -struct vmcb *alloc_vmcb(void) +struct vmcb *alloc_vmcb(struct vcpu *v, unsigned int idx, paddr_t *pa) { - struct vmcb *vmcb; + struct page_info *pg =3D alloc_domheap_page(v->domain, MEMF_no_owner); + mfn_t mfn; + + ASSERT(idx < VCPU_STATE_NR_PAGES); =20 - vmcb =3D alloc_xenheap_page(); - if ( vmcb =3D=3D NULL ) + if ( pg =3D=3D NULL ) { printk(XENLOG_WARNING "Warning: failed to allocate vmcb.\n"); return NULL; } =20 - clear_page(vmcb); - return vmcb; + mfn =3D page_to_mfn(pg); + clear_domain_page(mfn); + populate_perdomain_mapping(v, VCPU_STATE_ADDR(v, idx), &mfn, 1, + __PAGE_HYPERVISOR_RW); + *pa =3D mfn_to_maddr(mfn); + + return (struct vmcb *)VCPU_STATE_ADDR(v, idx); } =20 -void free_vmcb(struct vmcb *vmcb) +void free_vmcb(struct vcpu *v, unsigned int idx, paddr_t pa) { - free_xenheap_page(vmcb); + ASSERT(idx < VCPU_STATE_NR_PAGES); + + unmap_vcpu_state(v, idx, 1); + free_domheap_page(maddr_to_page(pa)); } =20 struct vmcb *svm_vmcb_map_one(const struct vcpu *v, struct vmcb *va, paddr_t pa) { - return va; + return v =3D=3D current ? va : map_domain_page(maddr_to_mfn(pa)); } =20 struct vmcb *svm_vmcb_map(const struct vcpu *v) @@ -56,6 +67,8 @@ struct vmcb *svm_vmcb_map(const struct vcpu *v) =20 void svm_vmcb_unmap(const struct vcpu *v, const struct vmcb *vmcb) { + if ( v !=3D current ) + unmap_vcpu_state_page(vmcb); } =20 /* This function can directly access fields which are covered by clean bit= s. */ @@ -219,26 +232,29 @@ int svm_create_vmcb(struct vcpu *v) int rc; =20 if ( (nv->nv_n1vmcx =3D=3D NULL) && - (nv->nv_n1vmcx =3D alloc_vmcb()) =3D=3D NULL ) + (nv->nv_n1vmcx =3D alloc_vmcb(v, VCPU_STATE_IDX_VMCB, + &nv->nv_n1vmcx_pa)) =3D=3D NULL ) { printk("Failed to create a new VMCB\n"); return -ENOMEM; } =20 svm->vmcb =3D nv->nv_n1vmcx; + svm->vmcb_pa =3D nv->nv_n1vmcx_pa; =20 vmcb =3D SVM_MAP_VMCB(v); rc =3D construct_vmcb(v, vmcb); SVM_UNMAP_VMCB(v, vmcb); if ( rc !=3D 0 ) { - free_vmcb(nv->nv_n1vmcx); + free_vmcb(v, VCPU_STATE_IDX_VMCB, nv->nv_n1vmcx_pa); nv->nv_n1vmcx =3D NULL; + nv->nv_n1vmcx_pa =3D INVALID_PADDR; svm->vmcb =3D NULL; + svm->vmcb_pa =3D INVALID_PADDR; return rc; } =20 - svm->vmcb_pa =3D nv->nv_n1vmcx_pa =3D virt_to_maddr(svm->vmcb); return 0; } =20 @@ -248,7 +264,7 @@ void svm_destroy_vmcb(struct vcpu *v) struct svm_vcpu *svm =3D &v->arch.hvm.svm; =20 if ( nv->nv_n1vmcx !=3D NULL ) - free_vmcb(nv->nv_n1vmcx); + free_vmcb(v, VCPU_STATE_IDX_VMCB, nv->nv_n1vmcx_pa); =20 if ( svm->msrpm !=3D NULL ) { @@ -260,6 +276,7 @@ void svm_destroy_vmcb(struct vcpu *v) nv->nv_n1vmcx =3D NULL; nv->nv_n1vmcx_pa =3D INVALID_PADDR; svm->vmcb =3D NULL; + svm->vmcb_pa =3D INVALID_PADDR; } =20 static void svm_dump_sel(const char *name, const struct segment_register *= s) diff --git a/xen/arch/x86/hvm/svm/vmcb.h b/xen/arch/x86/hvm/svm/vmcb.h index 070a44ff6c..6aaeb68507 100644 --- a/xen/arch/x86/hvm/svm/vmcb.h +++ b/xen/arch/x86/hvm/svm/vmcb.h @@ -536,12 +536,19 @@ struct vmcb { uint64_t res18[291]; }; =20 -struct vmcb *alloc_vmcb(void); -void free_vmcb(struct vmcb *vmcb); +/* + * VMCBs are domain-heap pages: hardware addresses them physically, and + * Xen maps each one at a fixed slot in the owning vCPU's state window, + * so the returned virtual address is for use in that vCPU's context only. + * @idx is the VCPU_STATE_IDX_* slot to place the mapping in. + */ +struct vmcb *alloc_vmcb(struct vcpu *v, unsigned int idx, paddr_t *pa); +void free_vmcb(struct vcpu *v, unsigned int idx, paddr_t pa); =20 /* - * Access a VMCB of a vCPU that may not be current. SVM_MAP_VMCB() takes - * the active VMCB, SVM_MAP_VMCB_ONE() a specific one. + * Access a VMCB of a vCPU that may not be current: the window VA for + * current, a transient mapping otherwise. SVM_MAP_VMCB() takes the active + * VMCB, SVM_MAP_VMCB_ONE() a specific one. */ struct vmcb *svm_vmcb_map_one(const struct vcpu *v, struct vmcb *va, paddr_t pa); --=20 2.56.0 From nobody Mon Nov 23 22:32:25 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=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1795122503; cv=none; d=zohomail.com; s=zohoarc; b=GjFa30GU3Azc+NgpVbW+hfdgHuh1s1EbB64Hu87/py1LogCpBVh+qS8T2WLc9RfjSvNmy39QBUmDjpsIZp8LCnloJHjIquIpaB5xvl18+njlKFowADPmZ4KqSkJNbbJlUdg8OCTldhWuMGDj4sDybRt1TBH2hVssWtSpk331CfM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795122503; h=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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=AWPcjH0VyAouWW+vbXCgoR3O+3Ba6GBMW9AQhOmm6N8=; b=mZgg6Y6vwQeR0OpZBldX85M8XMhBruTdgUiHIiMpo7Hblo78Wlw8jB3XUr6anE+qJseOS7SUo67UPU0HdDxv07Hn7e8srrI+vnj7xpn1TMUV5dAmHFC2ti5LUy5TRhGkqqntOiiAMoJZxZrtMiWx9ULbCKVv+4Eq/2iQW/89FeI= 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=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 179512250361232.15503482404199; Thu, 19 Nov 2026 13:08:23 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1497017.1695341 (Exim 4.92) (envelope-from ) id 1xU9ML-000298-2X; Thu, 19 Nov 2026 21:07:57 +0000 Received: by outflank-mailman (output) from mailman id 1497017.1695341; Thu, 19 Nov 2026 21:07:57 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9MK-00028j-RL; Thu, 19 Nov 2026 21:07:56 +0000 Received: by outflank-mailman (input) for mailman id 1497017; Thu, 19 Nov 2026 21:07:55 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9MJ-0001pJ-1e for xen-devel@lists.xenproject.org; Thu, 19 Nov 2026 21:07:55 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xU9MI-00000006spo-2QGq; Thu, 19 Nov 2026 21:07:54 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xU9MI-0000000CVBH-2itt; Thu, 19 Nov 2026 21:07:54 +0000 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" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=AWPcjH0VyAouWW+vbXCgoR3O+3Ba6GBMW9AQhOmm6N8=; b=hGrKDiEmBJxeUmwXL31IX7zTPA Q42wgtT9W51cGm+A5dEthVvNvS2LlEUDp7vG7+GFAVoXOnwHV0psGtQfYLhyNWQltRwyaviy+a7OV YAiA9DieZnBz5scAsbg8t+i+0guz6vL1544+tLTK21GHmhLKyJkX9qIp/HfFrLVxkMlM=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie Subject: [PATCH v7 07/50] x86/vmx: move the guest MSR load/save list off the xenheap Date: Thu, 19 Nov 2026 21:06:58 +0000 Message-ID: <20261119-asi-hvm-v7-7-6551b6d5c879@xenproject.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> References: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1795122504336158500 Content-Type: text/plain; charset="utf-8" The guest MSR load/save list holds guest MSR values (SPEC_CTRL, LBR, PT, vPMU global control, and EFER on hardware without dedicated controls), and lives on the xenheap. Move the page to the domain heap and map it at its fixed slot in the owning vCPU's state window. Hardware is unaffected: the VMCS MSR-area pointers hold the physical address, now derived from the page rather than from virt_to_maddr() on a directmap VA. The host MSR load list is left on the xenheap: it holds Xen's own exit-time values, not guest data, so relocating it would serve no isolation purpose. The window VA is for use in the owning vCPU's context only. Almost all accesses have v =3D=3D current and keep going through vmx->msr_area unchanged (the vmentry-path LBR fixups, the failed-vmentry dump, vPMU, and the MSR intercepts). The remote accessors -- vmx_add_msr() / vmx_del_msr() from vCPU construction, policy updates and context load, and single-entry reads/writes from the HVM context save/load and get/set-reg paths, all of which already require the target vCPU to be paused -- dispatch through a transient mapping of the page, dropped with unmap_vcpu_state_page(), which also drops this CPU's translations of it: * vmx_add_msr() and vmx_del_msr() map internally around their search/insert/remove. * vmx_read_guest_msr(), vmx_read_guest_loadonly_msr() and vmx_write_guest_msr() move out of line and do the same, via vmx_find_msr_in(), the existing lookup parameterised on the mapped area. vmx_find_msr() itself is unchanged for its remaining callers, which all run in current context and dereference the returned pointer through the window. vmx_destroy_vmcs() takes the page out of the window before freeing it, with unmap_vcpu_state(): the window is in the per-domain area that all its domain's vCPUs share, which outlives a vCPU whose creation fails. The new page pointer goes with the existing page_info pointers after pi_desc rather than next to msr_area: struct pi_desc is 64-byte aligned, so inserting eight bytes ahead of it would have added 56 bytes of padding, growing struct vmx_vcpu from 448 to 512 bytes and, via the union in struct arch_vcpu, every struct vcpu by 64 bytes (x86_64 defconfig). Assisted-by: Claude Code:claude-fable-5, Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Changes in v7: - Retitle (was: "x86/vmx: move the guest MSR load/save list off always-mapped memory"): on its own, the move takes the list off the xenheap; the directmap still maps the domain-heap page. - Standalone on staging, without per-vCPU page tables or the sparse directmap view: the message and two comments in vmcs.h say that the window VA is for use in the owning vCPU's context only, as the domain's vCPUs share the window. Changes in v5: - Map and unmap through VMX_MAP_MSR_AREA() and VMX_UNMAP_MSR_AREA(), which clears the pointer (George). Changes in v4: - New in this version. --- xen/arch/x86/hvm/vmx/vmcs.c | 142 ++++++++++++++++++++++-- xen/arch/x86/include/asm/hvm/vmx/vmcs.h | 63 +++-------- 2 files changed, 147 insertions(+), 58 deletions(-) diff --git a/xen/arch/x86/hvm/vmx/vmcs.c b/xen/arch/x86/hvm/vmx/vmcs.c index 8ec7ae7403..592722f1ca 100644 --- a/xen/arch/x86/hvm/vmx/vmcs.c +++ b/xen/arch/x86/hvm/vmx/vmcs.c @@ -1416,8 +1416,11 @@ static struct vmx_msr_entry *locate_msr_entry( return start; } =20 -struct vmx_msr_entry *vmx_find_msr(const struct vcpu *v, uint32_t msr, - enum vmx_msr_list_type type) +/* As vmx_find_msr(), but reaching the guest MSR area through @area. */ +static struct vmx_msr_entry *vmx_find_msr_in(const struct vcpu *v, + struct vmx_msr_entry *area, + uint32_t msr, + enum vmx_msr_list_type type) { const struct vmx_vcpu *vmx =3D &v->arch.hvm.vmx; struct vmx_msr_entry *start =3D NULL, *ent, *end; @@ -1435,11 +1438,11 @@ struct vmx_msr_entry *vmx_find_msr(const struct vcp= u *v, uint32_t msr, break; =20 case VMX_MSR_GUEST: - start =3D vmx->msr_area; + start =3D area; break; =20 case VMX_MSR_GUEST_LOADONLY: - start =3D vmx->msr_area; + start =3D area; substart =3D subend; subend =3D total; break; @@ -1458,6 +1461,77 @@ struct vmx_msr_entry *vmx_find_msr(const struct vcpu= *v, uint32_t msr, return ((ent < end) && (ent->index =3D=3D msr)) ? ent : NULL; } =20 +struct vmx_msr_entry *vmx_find_msr(const struct vcpu *v, uint32_t msr, + enum vmx_msr_list_type type) +{ + ASSERT(v =3D=3D current || type =3D=3D VMX_MSR_HOST); + + return vmx_find_msr_in(v, v->arch.hvm.vmx.msr_area, msr, type); +} + +static struct vmx_msr_entry *vmx_msr_area_map(const struct vcpu *v) +{ + const struct vmx_vcpu *vmx =3D &v->arch.hvm.vmx; + + if ( v =3D=3D current || !vmx->msr_area ) + return vmx->msr_area; + + return map_domain_page(page_to_mfn(vmx->msr_area_pg)); +} + +static void vmx_msr_area_unmap(const struct vcpu *v, + const struct vmx_msr_entry *area) +{ + if ( v !=3D current && area ) + unmap_vcpu_state_page(area); +} + +#define VMX_MAP_MSR_AREA(v) vmx_msr_area_map(v) + +/* Drops the mapping of a guest MSR area and nullifies its pointer. */ +#define VMX_UNMAP_MSR_AREA(v, area) do { \ + vmx_msr_area_unmap(v, area); \ + (area) =3D NULL; \ +} while ( false ) + +int vmx_read_guest_msr(const struct vcpu *v, uint32_t msr, uint64_t *val) +{ + struct vmx_msr_entry *area =3D VMX_MAP_MSR_AREA(v); + const struct vmx_msr_entry *ent =3D + area ? vmx_find_msr_in(v, area, msr, VMX_MSR_GUEST) : NULL; + + *val =3D ent ? ent->data : 0; + VMX_UNMAP_MSR_AREA(v, area); + + return ent ? 0 : -ESRCH; +} + +int vmx_read_guest_loadonly_msr(const struct vcpu *v, uint32_t msr, + uint64_t *val) +{ + struct vmx_msr_entry *area =3D VMX_MAP_MSR_AREA(v); + const struct vmx_msr_entry *ent =3D + area ? vmx_find_msr_in(v, area, msr, VMX_MSR_GUEST_LOADONLY) : NUL= L; + + *val =3D ent ? ent->data : 0; + VMX_UNMAP_MSR_AREA(v, area); + + return ent ? 0 : -ESRCH; +} + +int vmx_write_guest_msr(struct vcpu *v, uint32_t msr, uint64_t val) +{ + struct vmx_msr_entry *area =3D VMX_MAP_MSR_AREA(v); + struct vmx_msr_entry *ent =3D + area ? vmx_find_msr_in(v, area, msr, VMX_MSR_GUEST) : NULL; + + if ( ent ) + ent->data =3D val; + VMX_UNMAP_MSR_AREA(v, area); + + return ent ? 0 : -ESRCH; +} + int vmx_add_msr(struct vcpu *v, uint32_t msr, uint64_t val, enum vmx_msr_list_type type) { @@ -1503,13 +1577,38 @@ int vmx_add_msr(struct vcpu *v, uint32_t msr, uint6= 4_t val, { paddr_t addr; =20 - if ( (*ptr =3D alloc_xenheap_page()) =3D=3D NULL ) + if ( type =3D=3D VMX_MSR_HOST ) { - rc =3D -ENOMEM; - goto out; + if ( (*ptr =3D alloc_xenheap_page()) =3D=3D NULL ) + { + rc =3D -ENOMEM; + goto out; + } + + addr =3D virt_to_maddr(*ptr); } + else + { + struct page_info *pg =3D alloc_domheap_page(v->domain, + MEMF_no_owner); + mfn_t mfn; =20 - addr =3D virt_to_maddr(*ptr); + if ( pg =3D=3D NULL ) + { + rc =3D -ENOMEM; + goto out; + } + + mfn =3D page_to_mfn(pg); + clear_domain_page(mfn); + populate_perdomain_mapping(v, + VCPU_STATE_ADDR(v, + VCPU_STATE_IDX_VMX_= MSR), + &mfn, 1, __PAGE_HYPERVISOR_RW); + vmx->msr_area_pg =3D pg; + *ptr =3D (void *)VCPU_STATE_ADDR(v, VCPU_STATE_IDX_VMX_MSR); + addr =3D mfn_to_maddr(mfn); + } =20 switch ( type ) { @@ -1525,7 +1624,7 @@ int vmx_add_msr(struct vcpu *v, uint32_t msr, uint64_= t val, } } =20 - start =3D *ptr; + start =3D type =3D=3D VMX_MSR_HOST ? *ptr : VMX_MAP_MSR_AREA(v); end =3D start + total; ent =3D locate_msr_entry(start + substart, start + subend, msr); =20 @@ -1565,6 +1664,8 @@ int vmx_add_msr(struct vcpu *v, uint32_t msr, uint64_= t val, rc =3D 0; =20 out: + if ( type !=3D VMX_MSR_HOST ) + VMX_UNMAP_MSR_AREA(v, start); vmx_vmcs_exit(v); =20 return rc; @@ -1576,6 +1677,7 @@ int vmx_del_msr(struct vcpu *v, uint32_t msr, enum vm= x_msr_list_type type) struct vmx_msr_entry *start =3D NULL, *ent, *end; unsigned int substart =3D 0, subend =3D vmx->msr_save_count; unsigned int total =3D vmx->msr_load_count; + int rc =3D 0; =20 ASSERT(v =3D=3D current || !vcpu_runnable(v)); =20 @@ -1605,11 +1707,17 @@ int vmx_del_msr(struct vcpu *v, uint32_t msr, enum = vmx_msr_list_type type) if ( !start ) return -ESRCH; =20 + if ( type !=3D VMX_MSR_HOST ) + start =3D VMX_MAP_MSR_AREA(v); + end =3D start + total; ent =3D locate_msr_entry(start + substart, start + subend, msr); =20 if ( (ent =3D=3D end) || (ent->index !=3D msr) ) - return -ESRCH; + { + rc =3D -ESRCH; + goto out; + } =20 memmove(ent, ent + 1, sizeof(*ent) * (end - ent - 1)); =20 @@ -1632,7 +1740,11 @@ int vmx_del_msr(struct vcpu *v, uint32_t msr, enum v= mx_msr_list_type type) =20 vmx_vmcs_exit(v); =20 - return 0; + out: + if ( type !=3D VMX_MSR_HOST ) + VMX_UNMAP_MSR_AREA(v, start); + + return rc; } =20 void vmx_set_eoi_exit_bitmap(struct vcpu *v, u8 vector) @@ -1829,7 +1941,13 @@ void vmx_destroy_vmcs(struct vcpu *v) vmx_free_vmcs(vmx->vmcs_pa); =20 free_xenheap_page(v->arch.hvm.vmx.host_msr_area); - free_xenheap_page(v->arch.hvm.vmx.msr_area); + if ( v->arch.hvm.vmx.msr_area_pg ) + { + unmap_vcpu_state(v, VCPU_STATE_IDX_VMX_MSR, 1); + free_domheap_page(v->arch.hvm.vmx.msr_area_pg); + v->arch.hvm.vmx.msr_area_pg =3D NULL; + v->arch.hvm.vmx.msr_area =3D NULL; + } free_xenheap_page(v->arch.hvm.vmx.msr_bitmap); } =20 diff --git a/xen/arch/x86/include/asm/hvm/vmx/vmcs.h b/xen/arch/x86/include= /asm/hvm/vmx/vmcs.h index d0716e97d2..8ef07a7393 100644 --- a/xen/arch/x86/include/asm/hvm/vmx/vmcs.h +++ b/xen/arch/x86/include/asm/hvm/vmx/vmcs.h @@ -140,6 +140,10 @@ struct vmx_vcpu { * context. However, the data can be modified by toolstack/migration * actions. Remote access is only permitted for paused vcpus, and is * protected under the domctl lock. + * + * The guest list is a domain-heap page (msr_area_pg, below) mapped at= a + * fixed slot in the owning vCPU's state window: msr_area holds that + * window VA, for use in the owning vCPU's context only. */ struct vmx_msr_entry *msr_area; struct vmx_msr_entry *host_msr_area; @@ -185,6 +189,9 @@ struct vmx_vcpu { =20 struct page_info *pml_pg; =20 + /* Backing page of msr_area; see the comment there. */ + struct page_info *msr_area_pg; + /* Bitmask of trapped CR4 bits. */ unsigned long cr4_host_mask; =20 @@ -669,55 +676,19 @@ static inline int vmx_add_host_load_msr(struct vcpu *= v, uint32_t msr, return vmx_add_msr(v, msr, val, VMX_MSR_HOST); } =20 +/* + * Look an MSR up in the guest or host list of @v. The returned pointer + * reaches the guest lists through the vCPU state window, so for those it + * is for use (and the call permitted) only in current context; the + * single-entry accessors below handle paused remote vCPUs. + */ struct vmx_msr_entry *vmx_find_msr(const struct vcpu *v, uint32_t msr, enum vmx_msr_list_type type); =20 -static inline int vmx_read_guest_msr(const struct vcpu *v, uint32_t msr, - uint64_t *val) -{ - const struct vmx_msr_entry *ent =3D vmx_find_msr(v, msr, VMX_MSR_GUEST= ); - - if ( !ent ) - { - *val =3D 0; - return -ESRCH; - } - - *val =3D ent->data; - - return 0; -} - -static inline int vmx_read_guest_loadonly_msr( - const struct vcpu *v, uint32_t msr, uint64_t *val) -{ - const struct vmx_msr_entry *ent =3D - vmx_find_msr(v, msr, VMX_MSR_GUEST_LOADONLY); - - if ( !ent ) - { - *val =3D 0; - return -ESRCH; - } - - *val =3D ent->data; - - return 0; -} - -static inline int vmx_write_guest_msr(struct vcpu *v, uint32_t msr, - uint64_t val) -{ - struct vmx_msr_entry *ent =3D vmx_find_msr(v, msr, VMX_MSR_GUEST); - - if ( !ent ) - return -ESRCH; - - ent->data =3D val; - - return 0; -} - +int vmx_read_guest_msr(const struct vcpu *v, uint32_t msr, uint64_t *val); +int vmx_read_guest_loadonly_msr(const struct vcpu *v, uint32_t msr, + uint64_t *val); +int vmx_write_guest_msr(struct vcpu *v, uint32_t msr, uint64_t val); =20 /* MSR intercept bitmap infrastructure. */ enum vmx_msr_intercept_type { --=20 2.56.0 From nobody Mon Nov 23 22:32:25 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=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1795122509; cv=none; d=zohomail.com; s=zohoarc; b=CH9FsXbtt2VBZMQ4BycMDhWiLY3sbQ/gk6u/wZX0sm25rw8WpO8oqKVibJSSIl7P2LPfC6HPt2YCAnULizS4rz5E37ruV+xI2FC43J9mGXY8MSGfH0EbGU+dbmyXsL4oY4y2KE7jPsMZt1twPRLxMIzWMGRvg+00pLf79C6MKLs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795122509; h=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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=ogTOvJBAUjZJKHeOF2ihQ0VnU5FlIhqlbPbHU7Vl3p4=; b=Tsf2m64w8ov+Fvp4JoPBZ8nja/AH+GVNVs5sPkFxsZ989ltqi4b1bRpD8ERlqat7WCtE+G3NVIstNyK+sZcmat1jNZb3XTuF8yiZkDD6KeqWgL9ZS7rcqCcG1eoX12VxlWM05+tA0KOPd8AYzq2dvF8U4hnBrO1hZWtfm7e3kX4= 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=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1795122509680126.6783659978795; Thu, 19 Nov 2026 13:08:29 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1497018.1695348 (Exim 4.92) (envelope-from ) id 1xU9ML-0002Fg-JF; Thu, 19 Nov 2026 21:07:57 +0000 Received: by outflank-mailman (output) from mailman id 1497018.1695348; Thu, 19 Nov 2026 21:07:57 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9ML-0002FF-8W; Thu, 19 Nov 2026 21:07:57 +0000 Received: by outflank-mailman (input) for mailman id 1497018; Thu, 19 Nov 2026 21:07:56 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9MK-00024Y-Du for xen-devel@lists.xenproject.org; Thu, 19 Nov 2026 21:07:56 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xU9MJ-00000006spx-27g9; Thu, 19 Nov 2026 21:07:55 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xU9MJ-0000000CVBH-2Qp3; Thu, 19 Nov 2026 21:07:55 +0000 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" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=ogTOvJBAUjZJKHeOF2ihQ0VnU5FlIhqlbPbHU7Vl3p4=; b=4G/Uz24V3a5GlRGT/OcWaRgeFC IhZDQwOs2pdj4/0lXZILZgLxGvlm1D7QiRazcMpttRfsDfbAcOWMY17AtitmZDq7b2Aig9BdvBtSw q6fAm8qcwa/HeGek8t6uCb0zJsVQ7oGqh81gjnU7DIIK057pL2seouSmxQcEsMx3bHfU=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie Subject: [PATCH v7 08/50] x86: access a vCPU's saved register frame through map/unmap helpers Date: Thu, 19 Nov 2026 21:06:59 +0000 Message-ID: <20261119-asi-hvm-v7-8-6551b6d5c879@xenproject.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> References: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1795122510379158500 Content-Type: text/plain; charset="utf-8" When a vCPU is switched out, __context_switch() copies its register frame from the stack into struct vcpu, and back when the vCPU is switched in. Code working on a vCPU other than the one running uses that copy: vCPU creation, XEN_DOMCTL_{get,set}vcpucontext, saving and loading the HVM CPU record, VCPUOP_initialise, INIT/SIPI and S3 resets, the result of a hypercall continued on another CPU, PV dom0 construction, and the debug-key dumps. The frame is to move out of struct vcpu, on the xenheap, into a domain-heap page of the vCPU's own, mapped in its state window. Its users then need a transient mapping whenever they run in another context. Prepare for that: introduce VCPU_MAP_REGS() and VCPU_UNMAP_REGS(), after VCPU_MAP_XSAVE_AREA(), and convert every user but the context switch, which moves the frame between the stack and its storage. The field becomes _user_regs, so that an access bypassing the helpers fails to build: once the frame can live elsewhere, such an access would silently use a stale copy. vcpu_show_registers() now prints from a copy of the frame for PV vCPUs as well as HVM ones, so that it holds no mapping while printing, and vmx_set_info_guest() reads EFLAGS.TF before engaging the VMCS. No functional change. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Changes in v7: - Message: the frame moves to a domain-heap page mapped in the vCPU's state window. Changes in v5: - New in this version. --- xen/arch/x86/domain.c | 94 ++++++++++++++++++------------- xen/arch/x86/domctl.c | 72 ++++++++++++----------- xen/arch/x86/hvm/domain.c | 6 +- xen/arch/x86/hvm/hvm.c | 89 ++++++++++++++++------------- xen/arch/x86/hvm/vmx/vmcs.c | 9 +-- xen/arch/x86/hvm/vmx/vmx.c | 7 ++- xen/arch/x86/hypercall.c | 4 +- xen/arch/x86/include/asm/domain.h | 28 ++++++++- xen/arch/x86/pv/dom0_build.c | 3 +- xen/arch/x86/traps.c | 20 ++++--- 10 files changed, 204 insertions(+), 128 deletions(-) diff --git a/xen/arch/x86/domain.c b/xen/arch/x86/domain.c index 2407c575d9..fb8587d214 100644 --- a/xen/arch/x86/domain.c +++ b/xen/arch/x86/domain.c @@ -496,8 +496,12 @@ unsigned int arch_domain_struct_memflags(void) /* Initialise various registers to their architectural INIT/RESET state. */ void arch_vcpu_regs_init(struct vcpu *v) { - memset(&v->arch.user_regs, 0, sizeof(v->arch.user_regs)); - v->arch.user_regs.eflags =3D X86_EFLAGS_MBS; + struct cpu_user_regs *regs =3D VCPU_MAP_REGS(v); + + memset(regs, 0, sizeof(*regs)); + regs->eflags =3D X86_EFLAGS_MBS; + + VCPU_UNMAP_REGS(v, regs); =20 memset(v->arch.dr, 0, sizeof(v->arch.dr)); v->arch.dr6 =3D X86_DR6_DEFAULT; @@ -1141,6 +1145,7 @@ int arch_set_info_guest( { struct domain *d =3D v->domain; const struct cpu_policy *p =3D d->arch.cpu_policy; + struct cpu_user_regs *regs; unsigned int i; unsigned long flags; bool compat; @@ -1268,30 +1273,32 @@ int arch_set_info_guest( else vcpu_reset_fpu(v); =20 - memset(&v->arch.user_regs, 0, sizeof(v->arch.user_regs)); + regs =3D VCPU_MAP_REGS(v); + memset(regs, 0, sizeof(*regs)); =20 if ( !compat ) { - v->arch.user_regs.r15 =3D c.nat->user_regs.r15; - v->arch.user_regs.r14 =3D c.nat->user_regs.r14; - v->arch.user_regs.r13 =3D c.nat->user_regs.r13; - v->arch.user_regs.r12 =3D c.nat->user_regs.r12; - v->arch.user_regs.rbp =3D c.nat->user_regs.rbp; - v->arch.user_regs.rbx =3D c.nat->user_regs.rbx; - v->arch.user_regs.r11 =3D c.nat->user_regs.r11; - v->arch.user_regs.r10 =3D c.nat->user_regs.r10; - v->arch.user_regs.r9 =3D c.nat->user_regs.r9; - v->arch.user_regs.r8 =3D c.nat->user_regs.r8; - v->arch.user_regs.rax =3D c.nat->user_regs.rax; - v->arch.user_regs.rcx =3D c.nat->user_regs.rcx; - v->arch.user_regs.rdx =3D c.nat->user_regs.rdx; - v->arch.user_regs.rsi =3D c.nat->user_regs.rsi; - v->arch.user_regs.rdi =3D c.nat->user_regs.rdi; - v->arch.user_regs.rip =3D c.nat->user_regs.rip; - v->arch.user_regs.cs =3D c.nat->user_regs.cs; - v->arch.user_regs.rflags =3D (c.nat->user_regs.rflags &= X86_EFLAGS_ALL) | X86_EFLAGS_MBS; - v->arch.user_regs.rsp =3D c.nat->user_regs.rsp; - v->arch.user_regs.ss =3D c.nat->user_regs.ss; + regs->r15 =3D c.nat->user_regs.r15; + regs->r14 =3D c.nat->user_regs.r14; + regs->r13 =3D c.nat->user_regs.r13; + regs->r12 =3D c.nat->user_regs.r12; + regs->rbp =3D c.nat->user_regs.rbp; + regs->rbx =3D c.nat->user_regs.rbx; + regs->r11 =3D c.nat->user_regs.r11; + regs->r10 =3D c.nat->user_regs.r10; + regs->r9 =3D c.nat->user_regs.r9; + regs->r8 =3D c.nat->user_regs.r8; + regs->rax =3D c.nat->user_regs.rax; + regs->rcx =3D c.nat->user_regs.rcx; + regs->rdx =3D c.nat->user_regs.rdx; + regs->rsi =3D c.nat->user_regs.rsi; + regs->rdi =3D c.nat->user_regs.rdi; + regs->rip =3D c.nat->user_regs.rip; + regs->cs =3D c.nat->user_regs.cs; + regs->rflags =3D + (c.nat->user_regs.rflags & X86_EFLAGS_ALL) | X86_EFLAGS_MBS; + regs->rsp =3D c.nat->user_regs.rsp; + regs->ss =3D c.nat->user_regs.ss; v->arch.pv.es =3D c.nat->user_regs.es; v->arch.pv.ds =3D c.nat->user_regs.ds; v->arch.pv.fs =3D c.nat->user_regs.fs; @@ -1304,18 +1311,19 @@ int arch_set_info_guest( #ifdef CONFIG_COMPAT else { - v->arch.user_regs.ebx =3D c.cmp->user_regs.ebx; - v->arch.user_regs.ecx =3D c.cmp->user_regs.ecx; - v->arch.user_regs.edx =3D c.cmp->user_regs.edx; - v->arch.user_regs.esi =3D c.cmp->user_regs.esi; - v->arch.user_regs.edi =3D c.cmp->user_regs.edi; - v->arch.user_regs.ebp =3D c.cmp->user_regs.ebp; - v->arch.user_regs.eax =3D c.cmp->user_regs.eax; - v->arch.user_regs.eip =3D c.cmp->user_regs.eip; - v->arch.user_regs.cs =3D c.cmp->user_regs.cs; - v->arch.user_regs.eflags =3D (c.cmp->user_regs.eflags &= X86_EFLAGS_ALL) | X86_EFLAGS_MBS; - v->arch.user_regs.esp =3D c.cmp->user_regs.esp; - v->arch.user_regs.ss =3D c.cmp->user_regs.ss; + regs->ebx =3D c.cmp->user_regs.ebx; + regs->ecx =3D c.cmp->user_regs.ecx; + regs->edx =3D c.cmp->user_regs.edx; + regs->esi =3D c.cmp->user_regs.esi; + regs->edi =3D c.cmp->user_regs.edi; + regs->ebp =3D c.cmp->user_regs.ebp; + regs->eax =3D c.cmp->user_regs.eax; + regs->eip =3D c.cmp->user_regs.eip; + regs->cs =3D c.cmp->user_regs.cs; + regs->eflags =3D + (c.cmp->user_regs.eflags & X86_EFLAGS_ALL) | X86_EFLAGS_MBS; + regs->esp =3D c.cmp->user_regs.esp; + regs->ss =3D c.cmp->user_regs.ss; v->arch.pv.es =3D c.cmp->user_regs.es; v->arch.pv.ds =3D c.cmp->user_regs.ds; v->arch.pv.fs =3D c.cmp->user_regs.fs; @@ -1330,6 +1338,8 @@ int arch_set_info_guest( } #endif =20 + VCPU_UNMAP_REGS(v, regs); + if ( v->vcpu_id =3D=3D 0 && (c(vm_assist) & ~arch_vm_assist_valid_mask= (d)) ) return -EINVAL; =20 @@ -1348,12 +1358,16 @@ int arch_set_info_guest( } =20 #ifdef CONFIG_PV + regs =3D VCPU_MAP_REGS(v); + /* IOPL privileges are virtualised. */ - v->arch.pv.iopl =3D v->arch.user_regs.eflags & X86_EFLAGS_IOPL; - v->arch.user_regs.eflags &=3D ~X86_EFLAGS_IOPL; + v->arch.pv.iopl =3D regs->eflags & X86_EFLAGS_IOPL; + regs->eflags &=3D ~X86_EFLAGS_IOPL; =20 /* Ensure real hardware interrupts are enabled. */ - v->arch.user_regs.eflags |=3D X86_EFLAGS_IF; + regs->eflags |=3D X86_EFLAGS_IF; + + VCPU_UNMAP_REGS(v, regs); =20 nr_gdt_frames =3D DIV_ROUND_UP(c(gdt_ents), 512); if ( nr_gdt_frames > ARRAY_SIZE(v->arch.pv.gdt_frames) ) @@ -2124,7 +2138,7 @@ static void __context_switch(void) if ( !is_idle_domain(pd) ) { ASSERT(read_atomic(&p->dirty_cpu) =3D=3D cpu); - memcpy(&p->arch.user_regs, stack_regs, CTXT_SWITCH_STACK_BYTES); + memcpy(&p->arch._user_regs, stack_regs, CTXT_SWITCH_STACK_BYTES); vcpu_save_fpu(p); pd->arch.ctxt_switch->from(p); } @@ -2140,7 +2154,7 @@ static void __context_switch(void) =20 if ( !is_idle_domain(nd) ) { - memcpy(stack_regs, &n->arch.user_regs, CTXT_SWITCH_STACK_BYTES); + memcpy(stack_regs, &n->arch._user_regs, CTXT_SWITCH_STACK_BYTES); if ( cpu_has_xsave ) { if ( !set_xcr0(n->arch.xcr0 ?: XSTATE_FP_SSE) ) diff --git a/xen/arch/x86/domctl.c b/xen/arch/x86/domctl.c index bb363d606b..1eaa609b95 100644 --- a/xen/arch/x86/domctl.c +++ b/xen/arch/x86/domctl.c @@ -1461,6 +1461,7 @@ void arch_get_info_guest(struct vcpu *v, vcpu_guest_c= ontext_u c) const struct domain *d =3D v->domain; bool compat =3D is_pv_32bit_domain(d); const struct xsave_struct *xsave_area; + const struct cpu_user_regs *regs; #ifdef CONFIG_COMPAT #define c(fld) (!compat ? (c.nat->fld) : (c.cmp->fld)) #else @@ -1479,29 +1480,32 @@ void arch_get_info_guest(struct vcpu *v, vcpu_guest= _context_u c) c(flags |=3D VGCF_i387_valid); if ( !(v->pause_flags & VPF_down) ) c(flags |=3D VGCF_online); + + regs =3D VCPU_MAP_REGS(v); + if ( !compat ) { /* Backing memory is pre-zeroed. */ - c.nat->user_regs.r15 =3D v->arch.user_regs.r15; - c.nat->user_regs.r14 =3D v->arch.user_regs.r14; - c.nat->user_regs.r13 =3D v->arch.user_regs.r13; - c.nat->user_regs.r12 =3D v->arch.user_regs.r12; - c.nat->user_regs.rbp =3D v->arch.user_regs.rbp; - c.nat->user_regs.rbx =3D v->arch.user_regs.rbx; - c.nat->user_regs.r11 =3D v->arch.user_regs.r11; - c.nat->user_regs.r10 =3D v->arch.user_regs.r10; - c.nat->user_regs.r9 =3D v->arch.user_regs.r9; - c.nat->user_regs.r8 =3D v->arch.user_regs.r8; - c.nat->user_regs.rax =3D v->arch.user_regs.rax; - c.nat->user_regs.rcx =3D v->arch.user_regs.rcx; - c.nat->user_regs.rdx =3D v->arch.user_regs.rdx; - c.nat->user_regs.rsi =3D v->arch.user_regs.rsi; - c.nat->user_regs.rdi =3D v->arch.user_regs.rdi; - c.nat->user_regs.rip =3D v->arch.user_regs.rip; - c.nat->user_regs.cs =3D v->arch.user_regs.cs; - c.nat->user_regs.rflags =3D v->arch.user_regs.rflags; - c.nat->user_regs.rsp =3D v->arch.user_regs.rsp; - c.nat->user_regs.ss =3D v->arch.user_regs.ss; + c.nat->user_regs.r15 =3D regs->r15; + c.nat->user_regs.r14 =3D regs->r14; + c.nat->user_regs.r13 =3D regs->r13; + c.nat->user_regs.r12 =3D regs->r12; + c.nat->user_regs.rbp =3D regs->rbp; + c.nat->user_regs.rbx =3D regs->rbx; + c.nat->user_regs.r11 =3D regs->r11; + c.nat->user_regs.r10 =3D regs->r10; + c.nat->user_regs.r9 =3D regs->r9; + c.nat->user_regs.r8 =3D regs->r8; + c.nat->user_regs.rax =3D regs->rax; + c.nat->user_regs.rcx =3D regs->rcx; + c.nat->user_regs.rdx =3D regs->rdx; + c.nat->user_regs.rsi =3D regs->rsi; + c.nat->user_regs.rdi =3D regs->rdi; + c.nat->user_regs.rip =3D regs->rip; + c.nat->user_regs.cs =3D regs->cs; + c.nat->user_regs.rflags =3D regs->rflags; + c.nat->user_regs.rsp =3D regs->rsp; + c.nat->user_regs.ss =3D regs->ss; c.nat->user_regs.es =3D v->arch.pv.es; c.nat->user_regs.ds =3D v->arch.pv.ds; c.nat->user_regs.fs =3D v->arch.pv.fs; @@ -1515,18 +1519,18 @@ void arch_get_info_guest(struct vcpu *v, vcpu_guest= _context_u c) else { /* Backing memory is pre-zeroed. */ - c.cmp->user_regs.ebp =3D v->arch.user_regs.ebp; - c.cmp->user_regs.ebx =3D v->arch.user_regs.ebx; - c.cmp->user_regs.eax =3D v->arch.user_regs.eax; - c.cmp->user_regs.ecx =3D v->arch.user_regs.ecx; - c.cmp->user_regs.edx =3D v->arch.user_regs.edx; - c.cmp->user_regs.esi =3D v->arch.user_regs.esi; - c.cmp->user_regs.edi =3D v->arch.user_regs.edi; - c.cmp->user_regs.eip =3D v->arch.user_regs.eip; - c.cmp->user_regs.cs =3D v->arch.user_regs.cs; - c.cmp->user_regs.eflags =3D v->arch.user_regs.eflags; - c.cmp->user_regs.esp =3D v->arch.user_regs.esp; - c.cmp->user_regs.ss =3D v->arch.user_regs.ss; + c.cmp->user_regs.ebp =3D regs->ebp; + c.cmp->user_regs.ebx =3D regs->ebx; + c.cmp->user_regs.eax =3D regs->eax; + c.cmp->user_regs.ecx =3D regs->ecx; + c.cmp->user_regs.edx =3D regs->edx; + c.cmp->user_regs.esi =3D regs->esi; + c.cmp->user_regs.edi =3D regs->edi; + c.cmp->user_regs.eip =3D regs->eip; + c.cmp->user_regs.cs =3D regs->cs; + c.cmp->user_regs.eflags =3D regs->eflags; + c.cmp->user_regs.esp =3D regs->esp; + c.cmp->user_regs.ss =3D regs->ss; c.cmp->user_regs.es =3D v->arch.pv.es; c.cmp->user_regs.ds =3D v->arch.pv.ds; c.cmp->user_regs.fs =3D v->arch.pv.fs; @@ -1638,10 +1642,12 @@ void arch_get_info_guest(struct vcpu *v, vcpu_guest= _context_u c) } #endif =20 - if ( guest_kernel_mode(v, &v->arch.user_regs) ) + if ( guest_kernel_mode(v, regs) ) c(flags |=3D VGCF_in_kernel); } =20 + VCPU_UNMAP_REGS(v, regs); + c(vm_assist =3D d->vm_assist); #undef c } diff --git a/xen/arch/x86/hvm/domain.c b/xen/arch/x86/hvm/domain.c index 978d8b2222..71e16e25ba 100644 --- a/xen/arch/x86/hvm/domain.c +++ b/xen/arch/x86/hvm/domain.c @@ -103,7 +103,7 @@ static int check_segment(struct segment_register *reg, = enum x86_segment seg) int arch_set_info_hvm_guest(struct vcpu *v, const struct vcpu_hvm_context = *ctx) { const struct domain *d =3D v->domain; - struct cpu_user_regs *uregs =3D &v->arch.user_regs; + struct cpu_user_regs *uregs; struct segment_register cs, ds, ss, es, tr; const char *errstr; int rc; @@ -188,6 +188,7 @@ int arch_set_info_hvm_guest(struct vcpu *v, const struc= t vcpu_hvm_context *ctx) return -EINVAL; } =20 + uregs =3D VCPU_MAP_REGS(v); uregs->rax =3D regs->eax; uregs->rcx =3D regs->ecx; uregs->rdx =3D regs->edx; @@ -198,6 +199,7 @@ int arch_set_info_hvm_guest(struct vcpu *v, const struc= t vcpu_hvm_context *ctx) uregs->rdi =3D regs->edi; uregs->rip =3D regs->eip; uregs->rflags =3D (regs->eflags & X86_EFLAGS_ALL) | X86_EFLAGS_MBS; + VCPU_UNMAP_REGS(v, uregs); =20 v->arch.hvm.guest_cr[0] =3D regs->cr0; v->arch.hvm.guest_cr[3] =3D regs->cr3; @@ -239,6 +241,7 @@ int arch_set_info_hvm_guest(struct vcpu *v, const struc= t vcpu_hvm_context *ctx) return -EINVAL; } =20 + uregs =3D VCPU_MAP_REGS(v); uregs->rax =3D regs->rax; uregs->rcx =3D regs->rcx; uregs->rdx =3D regs->rdx; @@ -249,6 +252,7 @@ int arch_set_info_hvm_guest(struct vcpu *v, const struc= t vcpu_hvm_context *ctx) uregs->rdi =3D regs->rdi; uregs->rip =3D regs->rip; uregs->rflags =3D (regs->rflags & X86_EFLAGS_ALL) | X86_EFLAGS_MBS; + VCPU_UNMAP_REGS(v, uregs); =20 v->arch.hvm.guest_cr[0] =3D regs->cr0; v->arch.hvm.guest_cr[3] =3D regs->cr3; diff --git a/xen/arch/x86/hvm/hvm.c b/xen/arch/x86/hvm/hvm.c index 4334ff678d..2f58d4d3b1 100644 --- a/xen/arch/x86/hvm/hvm.c +++ b/xen/arch/x86/hvm/hvm.c @@ -827,28 +827,29 @@ HVM_REGISTER_SAVE_RESTORE(TSC_ADJUST, hvm_save_tsc_ad= just, NULL, =20 static int cf_check hvm_save_cpu_ctxt(struct vcpu *v, hvm_domain_context_t= *h) { + const struct cpu_user_regs *regs =3D VCPU_MAP_REGS(v); struct segment_register seg; struct hvm_hw_cpu ctxt =3D { .tsc =3D hvm_get_guest_tsc_fixed(v, v->domain->arch.hvm.sync_tsc), .msr_tsc_aux =3D v->arch.msrs->tsc_aux, - .rax =3D v->arch.user_regs.rax, - .rbx =3D v->arch.user_regs.rbx, - .rcx =3D v->arch.user_regs.rcx, - .rdx =3D v->arch.user_regs.rdx, - .rbp =3D v->arch.user_regs.rbp, - .rsi =3D v->arch.user_regs.rsi, - .rdi =3D v->arch.user_regs.rdi, - .rsp =3D v->arch.user_regs.rsp, - .rip =3D v->arch.user_regs.rip, - .rflags =3D v->arch.user_regs.rflags, - .r8 =3D v->arch.user_regs.r8, - .r9 =3D v->arch.user_regs.r9, - .r10 =3D v->arch.user_regs.r10, - .r11 =3D v->arch.user_regs.r11, - .r12 =3D v->arch.user_regs.r12, - .r13 =3D v->arch.user_regs.r13, - .r14 =3D v->arch.user_regs.r14, - .r15 =3D v->arch.user_regs.r15, + .rax =3D regs->rax, + .rbx =3D regs->rbx, + .rcx =3D regs->rcx, + .rdx =3D regs->rdx, + .rbp =3D regs->rbp, + .rsi =3D regs->rsi, + .rdi =3D regs->rdi, + .rsp =3D regs->rsp, + .rip =3D regs->rip, + .rflags =3D regs->rflags, + .r8 =3D regs->r8, + .r9 =3D regs->r9, + .r10 =3D regs->r10, + .r11 =3D regs->r11, + .r12 =3D regs->r12, + .r13 =3D regs->r13, + .r14 =3D regs->r14, + .r15 =3D regs->r15, .cr0 =3D v->arch.hvm.guest_cr[0], .cr2 =3D v->arch.hvm.guest_cr[2], .cr3 =3D v->arch.hvm.guest_cr[3], @@ -863,6 +864,8 @@ static int cf_check hvm_save_cpu_ctxt(struct vcpu *v, h= vm_domain_context_t *h) }; const struct xsave_struct *xsave_area; =20 + VCPU_UNMAP_REGS(v, regs); + /* * We don't need to save state for a vcpu that is down; the restore * code will leave it down if there is nothing saved. @@ -1020,6 +1023,7 @@ static int cf_check hvm_load_cpu_ctxt(struct domain *= d, hvm_domain_context_t *h) const struct cpu_policy *p =3D d->arch.cpu_policy; unsigned int vcpuid =3D hvm_load_instance(h); struct vcpu *v; + struct cpu_user_regs *regs; struct hvm_hw_cpu ctxt; struct segment_register seg; const char *errstr; @@ -1182,24 +1186,26 @@ static int cf_check hvm_load_cpu_ctxt(struct domain= *d, hvm_domain_context_t *h) else vcpu_reset_fpu(v); =20 - v->arch.user_regs.rax =3D ctxt.rax; - v->arch.user_regs.rbx =3D ctxt.rbx; - v->arch.user_regs.rcx =3D ctxt.rcx; - v->arch.user_regs.rdx =3D ctxt.rdx; - v->arch.user_regs.rbp =3D ctxt.rbp; - v->arch.user_regs.rsi =3D ctxt.rsi; - v->arch.user_regs.rdi =3D ctxt.rdi; - v->arch.user_regs.rsp =3D ctxt.rsp; - v->arch.user_regs.rip =3D ctxt.rip; - v->arch.user_regs.rflags =3D ctxt.rflags | X86_EFLAGS_MBS; - v->arch.user_regs.r8 =3D ctxt.r8; - v->arch.user_regs.r9 =3D ctxt.r9; - v->arch.user_regs.r10 =3D ctxt.r10; - v->arch.user_regs.r11 =3D ctxt.r11; - v->arch.user_regs.r12 =3D ctxt.r12; - v->arch.user_regs.r13 =3D ctxt.r13; - v->arch.user_regs.r14 =3D ctxt.r14; - v->arch.user_regs.r15 =3D ctxt.r15; + regs =3D VCPU_MAP_REGS(v); + regs->rax =3D ctxt.rax; + regs->rbx =3D ctxt.rbx; + regs->rcx =3D ctxt.rcx; + regs->rdx =3D ctxt.rdx; + regs->rbp =3D ctxt.rbp; + regs->rsi =3D ctxt.rsi; + regs->rdi =3D ctxt.rdi; + regs->rsp =3D ctxt.rsp; + regs->rip =3D ctxt.rip; + regs->rflags =3D ctxt.rflags | X86_EFLAGS_MBS; + regs->r8 =3D ctxt.r8; + regs->r9 =3D ctxt.r9; + regs->r10 =3D ctxt.r10; + regs->r11 =3D ctxt.r11; + regs->r12 =3D ctxt.r12; + regs->r13 =3D ctxt.r13; + regs->r14 =3D ctxt.r14; + regs->r15 =3D ctxt.r15; + VCPU_UNMAP_REGS(v, regs); v->arch.dr[0] =3D ctxt.dr0; v->arch.dr[1] =3D ctxt.dr1; v->arch.dr[2] =3D ctxt.dr2; @@ -2569,7 +2575,7 @@ bool hvm_vcpu_virtual_to_linear( { unsigned long addr =3D offset, last_byte; const struct cpu_user_regs *regs =3D v =3D=3D current ? guest_cpu_user= _regs() - : &v->arch.user_regs; + : VCPU_MAP_REGS(v); bool okay =3D false; =20 ASSERT(v =3D=3D current || !vcpu_runnable(v)); @@ -2694,6 +2700,10 @@ bool hvm_vcpu_virtual_to_linear( * failed. The permissions failure is not relevant to some callers. */ *linear_addr =3D addr; + + if ( v !=3D current ) + VCPU_UNMAP_REGS(v, regs); + return okay; } =20 @@ -3964,6 +3974,7 @@ void hvm_init_hypercall_page(struct domain *d, void *= ptr) void hvm_vcpu_reset_state(struct vcpu *v, uint16_t cs, uint16_t ip) { struct domain *d =3D v->domain; + struct cpu_user_regs *regs; struct segment_register reg; =20 domain_lock(d); @@ -3981,7 +3992,9 @@ void hvm_vcpu_reset_state(struct vcpu *v, uint16_t cs= , uint16_t ip) vcpu_reset_fpu(v); =20 arch_vcpu_regs_init(v); - v->arch.user_regs.rip =3D ip; + regs =3D VCPU_MAP_REGS(v); + regs->rip =3D ip; + VCPU_UNMAP_REGS(v, regs); =20 v->arch.hvm.guest_cr[0] =3D X86_CR0_ET; hvm_update_guest_cr(v, 0); diff --git a/xen/arch/x86/hvm/vmx/vmcs.c b/xen/arch/x86/hvm/vmx/vmcs.c index 592722f1ca..78b0af29f2 100644 --- a/xen/arch/x86/hvm/vmx/vmcs.c +++ b/xen/arch/x86/hvm/vmx/vmcs.c @@ -2083,15 +2083,13 @@ static void vmx_dump_sel2(const char *name, uint32_= t lim) =20 void vmcs_dump_vcpu(struct vcpu *v) { - struct cpu_user_regs *regs =3D &v->arch.user_regs; + const struct cpu_user_regs *regs =3D v =3D=3D current ? guest_cpu_user= _regs() + : VCPU_MAP_REGS(v); uint32_t vmentry_ctl, vmexit_ctl; unsigned long cr4; uint64_t efer; unsigned int i, n; =20 - if ( v =3D=3D current ) - regs =3D guest_cpu_user_regs(); - vmx_vmcs_enter(v); =20 vmentry_ctl =3D vmr32(VM_ENTRY_CONTROLS), @@ -2236,6 +2234,9 @@ void vmcs_dump_vcpu(struct vcpu *v) vmr16(VIRTUAL_PROCESSOR_ID), vmr(VM_FUNCTION_CONTROL)); =20 vmx_vmcs_exit(v); + + if ( v !=3D current ) + VCPU_UNMAP_REGS(v, regs); } =20 static void cf_check vmcs_dump(unsigned char ch) diff --git a/xen/arch/x86/hvm/vmx/vmx.c b/xen/arch/x86/hvm/vmx/vmx.c index 3c7374a9ff..bdbcd0ad4d 100644 --- a/xen/arch/x86/hvm/vmx/vmx.c +++ b/xen/arch/x86/hvm/vmx/vmx.c @@ -2159,8 +2159,12 @@ static bool cf_check vmx_event_pending(const struct = vcpu *v) =20 static void cf_check vmx_set_info_guest(struct vcpu *v) { + const struct cpu_user_regs *regs =3D VCPU_MAP_REGS(v); + bool single_step =3D regs->eflags & X86_EFLAGS_TF; unsigned long intr_shadow; =20 + VCPU_UNMAP_REGS(v, regs); + vmx_vmcs_enter(v); =20 __vmwrite(GUEST_DR7, v->arch.dr7); @@ -2174,8 +2178,7 @@ static void cf_check vmx_set_info_guest(struct vcpu *= v) * immediately vmexit and hence make no progress. */ __vmread(GUEST_INTERRUPTIBILITY_INFO, &intr_shadow); - if ( v->domain->debugger_attached && - (v->arch.user_regs.eflags & X86_EFLAGS_TF) && + if ( v->domain->debugger_attached && single_step && (intr_shadow & VMX_INTR_SHADOW_STI) ) { intr_shadow &=3D ~VMX_INTR_SHADOW_STI; diff --git a/xen/arch/x86/hypercall.c b/xen/arch/x86/hypercall.c index dc0a90ca09..bfdc462874 100644 --- a/xen/arch/x86/hypercall.c +++ b/xen/arch/x86/hypercall.c @@ -107,9 +107,11 @@ unsigned long hypercall_create_continuation( =20 void arch_hypercall_tasklet_result(struct vcpu *v, long res) { - struct cpu_user_regs *regs =3D &v->arch.user_regs; + struct cpu_user_regs *regs =3D VCPU_MAP_REGS(v); =20 regs->rax =3D res; + + VCPU_UNMAP_REGS(v, regs); } =20 int hypercall_xlat_continuation(unsigned int *id, unsigned int nr, diff --git a/xen/arch/x86/include/asm/domain.h b/xen/arch/x86/include/asm/d= omain.h index 3394513e43..edbafdca53 100644 --- a/xen/arch/x86/include/asm/domain.h +++ b/xen/arch/x86/include/asm/domain.h @@ -608,7 +608,12 @@ struct pv_vcpu =20 struct arch_vcpu { - struct cpu_user_regs user_regs; + /* + * The vCPU's register frame while it is not running: __context_switch= () + * copies it here from the stack when the vCPU is switched out, and ba= ck + * when it is switched in. Everything else goes through VCPU_MAP_REGS= (). + */ + struct cpu_user_regs _user_regs; =20 /* Debug registers. */ unsigned long dr[4]; @@ -711,6 +716,27 @@ static inline void free_vcpu_guest_context(struct vcpu= _guest_context *vgc) vfree(vgc); } =20 +/* + * Fetch a pointer to the register frame a vCPU was last switched out with, + * or which a toolstack, debugger or reset has put there since: the vCPU's + * registers for code working on a vCPU other than the one running (whose + * registers are on the stack, see guest_cpu_user_regs()). + * + * The pointer is valid until VCPU_UNMAP_REGS(), which clears it. A mappi= ng + * must not be held across a scheduling point. + * + * @param v Owner of the register frame + */ +#define VCPU_MAP_REGS(v) (&(v)->arch._user_regs) + +/* + * Drops the mapping of a vCPU's register frame and nullifies its pointer. + * + * @param v Owner of the register frame + * @param r Register frame of v, from VCPU_MAP_REGS() + */ +#define VCPU_UNMAP_REGS(v, r) do { (void)(v); (r) =3D NULL; } while ( fals= e ) + void arch_vcpu_regs_init(struct vcpu *v); =20 struct vcpu_hvm_context; diff --git a/xen/arch/x86/pv/dom0_build.c b/xen/arch/x86/pv/dom0_build.c index 24f2fa0b7f..9aa815ef28 100644 --- a/xen/arch/x86/pv/dom0_build.c +++ b/xen/arch/x86/pv/dom0_build.c @@ -1003,7 +1003,7 @@ static int __init dom0_construct(const struct boot_do= main *bd) * rSI =3D start_info * [rAX,rBX,rCX,rDX,rDI,rBP,R8-R15 are zero] */ - regs =3D &v->arch.user_regs; + regs =3D VCPU_MAP_REGS(v); v->arch.pv.ds =3D (compat ? FLAT_COMPAT_KERNEL_DS : FLAT_KERNEL_DS); v->arch.pv.es =3D (compat ? FLAT_COMPAT_KERNEL_DS : FLAT_KERNEL_DS); v->arch.pv.fs =3D (compat ? FLAT_COMPAT_KERNEL_DS : FLAT_KERNEL_DS); @@ -1014,6 +1014,7 @@ static int __init dom0_construct(const struct boot_do= main *bd) regs->rsp =3D vstack_end; regs->rsi =3D vstartinfo_start; regs->eflags =3D X86_EFLAGS_IF | X86_EFLAGS_MBS; + VCPU_UNMAP_REGS(v, regs); =20 /* * We don't call arch_set_info_guest(), so some initialisation needs d= oing diff --git a/xen/arch/x86/traps.c b/xen/arch/x86/traps.c index 410226d0f0..6b62040afe 100644 --- a/xen/arch/x86/traps.c +++ b/xen/arch/x86/traps.c @@ -273,16 +273,17 @@ void show_registers(const struct cpu_user_regs *regs) =20 void vcpu_show_registers(struct vcpu *v) { - const struct cpu_user_regs *regs =3D &v->arch.user_regs; - struct cpu_user_regs aux_regs; + const struct cpu_user_regs *regs =3D VCPU_MAP_REGS(v); + struct cpu_user_regs aux_regs =3D *regs; struct extra_state state; enum context context; =20 + VCPU_UNMAP_REGS(v, regs); + regs =3D &aux_regs; + if ( is_hvm_vcpu(v) ) { - aux_regs =3D *regs; get_hvm_registers(v, &aux_regs, &state); - regs =3D &aux_regs; context =3D CTXT_hvm_guest; } else @@ -903,6 +904,7 @@ void cf_check show_execution_state(const struct cpu_use= r_regs *regs) =20 void vcpu_show_execution_state(struct vcpu *v) { + const struct cpu_user_regs *regs; unsigned long flags =3D 0; =20 if ( !is_vcpu_online(v) ) @@ -948,12 +950,16 @@ void vcpu_show_execution_state(struct vcpu *v) */ console_unlock_recursive_irqrestore(flags); =20 - show_hvm_stack(v, &v->arch.user_regs); + regs =3D VCPU_MAP_REGS(v); + show_hvm_stack(v, regs); + VCPU_UNMAP_REGS(v, regs); } else { - if ( guest_kernel_mode(v, &v->arch.user_regs) ) - show_guest_stack(v, &v->arch.user_regs); + regs =3D VCPU_MAP_REGS(v); + if ( guest_kernel_mode(v, regs) ) + show_guest_stack(v, regs); + VCPU_UNMAP_REGS(v, regs); =20 console_unlock_recursive_irqrestore(flags); } --=20 2.56.0 From nobody Mon Nov 23 22:32:25 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=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1795122496; cv=none; d=zohomail.com; s=zohoarc; b=PJrnOTdVXN+JqR0PDfUuASr8jTvK7pDHYGANNReYx7xT0VLQE3jjtjKGCZlsKYOUc30ItxFTVVut0QFSZj7mh2Nt2znVRfTtLwgB6bAFpKQw8ub0h+q7USPjvdFkz7KQnuGv9Jd0JBCIbx6qDdoxmj34Kk5/4pLP1HVCQzfGLao= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795122496; h=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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=NA0CgP4Q/cvQSwoTMGUQpga3jXUgrFvZZLC9lp5Ud18=; b=QGm6Ub7ueyyMRw0RIhHCJfQ02D8bSAxVZHIKFrKKSnbfrFfkSL9T7HmkMC+xa5GYwQy91tpy4uKI5BbzLKy4dOCqlrPc+d20NxXWcEFDG3ZSHo+tdzBU5p78+WgD8w7mXZO/1R+NNbaikqVvV2bnLDlyThGSS8FE8WmbkE66X30= 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=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1795122496942732.3154817548619; Thu, 19 Nov 2026 13:08:16 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1497019.1695359 (Exim 4.92) (envelope-from ) id 1xU9MM-0002gD-TE; Thu, 19 Nov 2026 21:07:58 +0000 Received: by outflank-mailman (output) from mailman id 1497019.1695359; Thu, 19 Nov 2026 21:07:58 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9MM-0002eO-Lv; Thu, 19 Nov 2026 21:07:58 +0000 Received: by outflank-mailman (input) for mailman id 1497019; Thu, 19 Nov 2026 21:07:57 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9MK-00028v-SE for xen-devel@lists.xenproject.org; Thu, 19 Nov 2026 21:07:56 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xU9MK-00000006sq5-1lpQ; Thu, 19 Nov 2026 21:07:56 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xU9MK-0000000CVBH-27BE; Thu, 19 Nov 2026 21:07:56 +0000 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" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=NA0CgP4Q/cvQSwoTMGUQpga3jXUgrFvZZLC9lp5Ud18=; b=xsYIALuzz1+wMrFO+z8BsY2yCu vtft2x/xsbxNU3byxkU6FrX4guTx36AoMWc65cuUJCFyHVYSB0FOcdS0WOJCi2TMSRRi+ELpoe1pu eDlzs5SFkGy+YEKnK8ihgcuK822v11jUKqIWVBbwutV63I9ZCfh8sOWic3oTqwSueFkA=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie Subject: [PATCH v7 09/50] x86: move the saved register frame behind the vCPU state window Date: Thu, 19 Nov 2026 21:07:00 +0000 Message-ID: <20261119-asi-hvm-v7-9-6551b6d5c879@xenproject.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> References: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1795122498253158500 Content-Type: text/plain; charset="utf-8" While a vCPU is not running, its register frame is saved in struct vcpu, a xenheap page. Keep the frame instead in a domain-heap page of the vCPU's own, mapped for the life of the vCPU at a new slot of its state window, like the SVM VMCB and the VMX MSR list. This slot is not vendor-specific, so it comes after the AMD- and Intel-specific ones. Like the VMCB and MSR list moves, this applies to every guest vCPU, PV and HVM: struct vcpu no longer holds a copy. The page is allocated in arch_vcpu_create() ahead of arch_vcpu_regs_init(), which initialises the frame, and unmapped before it is freed, on the failure path as at teardown. The window is in the per-domain area that all its domain's vCPUs share, which only that domain's page tables map; the directmap still maps the page, as it does every domain-heap page. __context_switch() saves the outgoing vCPU's frame while its page tables are still loaded, and now restores the incoming vCPU's only after write_ptbase(), once its window is mapped. Nothing in between reads the frame: set_xcr0(), the FPU restore, the ctxt_switch->to() hooks, and the PSR and GDT updates. Both copies go through the window address, with interrupts off; a transient mapping would complete the lazy context switch and re-enter __context_switch(). The save is skipped if the outgoing vCPU's page tables are no longer loaded. Only the shadow-mode paths which abandon a PV vCPU's page tables early (update_cr3() failure in new_guest_cr3() and _toggle_guest_pt()) leave them so, loading the idle page tables once they have crashed the domain; anything else reaching that point is a page-table tracking bug, fatal in every build. After such a crash the vCPU's saved frame is the one it was last switched in with, not its registers at the crash, and that is what XEN_DOMCTL_getvcpucontext and the debug keys report for it. VCPU_MAP_REGS() returns the window address when the vCPU is running here on its own page tables (v =3D=3D current and this_cpu(pgtable_vcpu) = =3D=3D v), and otherwise a transient mapping of the page, which, for a single page, cannot fail. Its users all work on a vCPU other than the one running here, normally paused or not yet run: vCPU creation, the toolstack's vCPU context, the HVM CPU record, VCPUOP_initialise, INIT/SIPI and S3 resets, a hypercall continued on another CPU, PV dom0 construction, and the debug-key dumps. The vCPU's own page tables being loaded is not enough: while a CPU holds a vCPU's state lazily they are loaded there, but an IPI syncing that state for another CPU can switch them away at any time. VCPU_UNMAP_REGS() drops a transient mapping with unmap_vcpu_state_page(), which also flushes this CPU's translation. The live frame on the pCPU's stack, which holds a running vCPU's registers, is not affected. Cost: a page per guest vCPU, and a transient mapping on each of the cold paths above. On every switch into a guest vCPU, the restore reaches the frame through a 4KiB, non-global PTE in the per-domain area, just after switch_cr3_cr4() has flushed the TLB, so it takes a page walk, where struct vcpu was reached through a superpage of the directmap. struct vcpu shrinks from 2880 to 2688 bytes (x86_64 defconfig). Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Changes in v7: - Standalone on staging, without per-vCPU page tables or the sparse directmap view: the message describes the domain-wide window alone, and struct vcpu's sizes are those of this base. - Make this the first slot in a "common" section. Changes in v5: - New in this version. --- xen/arch/x86/domain.c | 98 ++++++++++++++++++++++++++++++- xen/arch/x86/include/asm/config.h | 2 + xen/arch/x86/include/asm/domain.h | 26 +++++--- 3 files changed, 117 insertions(+), 9 deletions(-) diff --git a/xen/arch/x86/domain.c b/xen/arch/x86/domain.c index fb8587d214..b8dea08903 100644 --- a/xen/arch/x86/domain.c +++ b/xen/arch/x86/domain.c @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -493,6 +494,66 @@ unsigned int arch_domain_struct_memflags(void) return MEMF_bits(bits); } =20 +/* + * A vCPU keeps its register frame, while it is not running, in a domain-h= eap + * page of its own, mapped for the life of the vCPU at a fixed slot of its + * state window. + */ +static struct cpu_user_regs *regs_window(const struct vcpu *v) +{ + return (void *)VCPU_STATE_ADDR(v, VCPU_STATE_IDX_REGS); +} + +static int vcpu_regs_alloc(struct vcpu *v) +{ + struct page_info *pg =3D alloc_domheap_page(v->domain, MEMF_no_owner); + mfn_t mfn; + + if ( !pg ) + return -ENOMEM; + + mfn =3D page_to_mfn(pg); + clear_domain_page(mfn); + populate_perdomain_mapping(v, VCPU_STATE_ADDR(v, VCPU_STATE_IDX_REGS), + &mfn, 1, __PAGE_HYPERVISOR_RW); + v->arch.regs_pg =3D pg; + + return 0; +} + +static void vcpu_regs_free(struct vcpu *v) +{ + struct page_info *pg =3D v->arch.regs_pg; + + if ( !pg ) + return; + + unmap_vcpu_state(v, VCPU_STATE_IDX_REGS, 1); + v->arch.regs_pg =3D NULL; + free_domheap_page(pg); +} + +struct cpu_user_regs *vcpu_map_regs(const struct vcpu *v) +{ + /* + * v's page tables being loaded is not enough: while this CPU holds v's + * state lazily, an IPI syncing that state for another CPU can switch + * them away at any time. + */ + if ( v =3D=3D current && this_cpu(pgtable_vcpu) =3D=3D v ) + return regs_window(v); + + return __map_domain_page(v->arch.regs_pg); +} + +void vcpu_unmap_regs(const struct vcpu *v, const struct cpu_user_regs *reg= s) +{ + if ( !regs || regs =3D=3D regs_window(v) ) + return; + + unmap_vcpu_state_page(regs); +} + /* Initialise various registers to their architectural INIT/RESET state. */ void arch_vcpu_regs_init(struct vcpu *v) { @@ -526,6 +587,7 @@ int arch_vcpu_create(struct vcpu *v) BUILD_BUG_ON(VCPU_STATE_IDX_VMCB >=3D VCPU_STATE_NR_PAGES); BUILD_BUG_ON(VCPU_STATE_IDX_N2VMCB >=3D VCPU_STATE_NR_PAGES); BUILD_BUG_ON(VCPU_STATE_IDX_VMX_MSR >=3D VCPU_STATE_NR_PAGES); + BUILD_BUG_ON(VCPU_STATE_IDX_REGS >=3D VCPU_STATE_NR_PAGES); /* Page tables only; torn down with the rest of the per-domain are= a. */ rc =3D create_perdomain_mapping(d, VCPU_STATE_START(v), VCPU_STATE_NR_PAGES, @@ -540,6 +602,10 @@ int arch_vcpu_create(struct vcpu *v) =20 vmce_init_vcpu(v); =20 + /* The frame's storage, ahead of its initialisation. */ + if ( (rc =3D vcpu_regs_alloc(v)) !=3D 0 ) + goto fail; + arch_vcpu_regs_init(v); =20 if ( (rc =3D init_vcpu_msr_policy(v)) ) @@ -582,6 +648,7 @@ int arch_vcpu_create(struct vcpu *v) fail: paging_vcpu_teardown(v); vcpu_destroy_fpu(v); + vcpu_regs_free(v); xfree(v->arch.msrs); v->arch.msrs =3D NULL; =20 @@ -604,6 +671,8 @@ void arch_vcpu_destroy(struct vcpu *v) pv_vcpu_destroy(v); else ASSERT_UNREACHABLE(); + + vcpu_regs_free(v); } =20 int arch_sanitise_domain_config(const struct xen_domctl_createdomain *conf= ig) @@ -2124,6 +2193,19 @@ static void load_default_gdt(unsigned int cpu) per_cpu(full_gdt_loaded, cpu) =3D false; } =20 +/* + * The register frame of p or n, for __context_switch(), which runs with + * interrupts off and with the vCPU's own page tables loaded: the window + * address serves. A transient mapping is not an option from here: it wou= ld + * complete the lazy context switch and re-enter __context_switch(). + */ +static struct cpu_user_regs *vcpu_user_regs(const struct vcpu *v) +{ + BUG_ON(this_cpu(pgtable_vcpu) !=3D v); + + return regs_window(v); +} + static void __context_switch(void) { struct cpu_user_regs *stack_regs =3D guest_cpu_user_regs(); @@ -2138,7 +2220,17 @@ static void __context_switch(void) if ( !is_idle_domain(pd) ) { ASSERT(read_atomic(&p->dirty_cpu) =3D=3D cpu); - memcpy(&p->arch._user_regs, stack_regs, CTXT_SWITCH_STACK_BYTES); + /* + * p's frame is reached through its state window, mapped while p's + * own page tables are loaded, as they normally still are here. N= ot + * so after the shadow-mode paths which abandon a vCPU's tables ea= rly + * (update_cr3() failure), which have crashed the domain: the frame + * is left unsaved. A transient mapping would re-enter this funct= ion. + */ + if ( likely(this_cpu(pgtable_vcpu) =3D=3D p) ) + memcpy(vcpu_user_regs(p), stack_regs, CTXT_SWITCH_STACK_BYTES); + else + BUG_ON(!domain_shutting_down(pd)); vcpu_save_fpu(p); pd->arch.ctxt_switch->from(p); } @@ -2154,7 +2246,6 @@ static void __context_switch(void) =20 if ( !is_idle_domain(nd) ) { - memcpy(stack_regs, &n->arch._user_regs, CTXT_SWITCH_STACK_BYTES); if ( cpu_has_xsave ) { if ( !set_xcr0(n->arch.xcr0 ?: XSTATE_FP_SSE) ) @@ -2178,6 +2269,9 @@ static void __context_switch(void) =20 write_ptbase(n); =20 + if ( !is_idle_domain(nd) ) + memcpy(stack_regs, vcpu_user_regs(n), CTXT_SWITCH_STACK_BYTES); + #ifdef CONFIG_PV /* Prefetch the VMCB if we expect to use it later in the context switc= h */ if ( using_svm() && is_pv_64bit_domain(nd) && !is_idle_domain(nd) ) diff --git a/xen/arch/x86/include/asm/config.h b/xen/arch/x86/include/asm/c= onfig.h index b05dbd4a4b..789748b54c 100644 --- a/xen/arch/x86/include/asm/config.h +++ b/xen/arch/x86/include/asm/config.h @@ -311,6 +311,8 @@ extern unsigned long xen_phys_start; #define VCPU_STATE_IDX_N2VMCB 1 /* Intel-specific slots */ #define VCPU_STATE_IDX_VMX_MSR 0 +/* Common slots */ +#define VCPU_STATE_IDX_REGS 2 =20 #define ELFSIZE 64 =20 diff --git a/xen/arch/x86/include/asm/domain.h b/xen/arch/x86/include/asm/d= omain.h index edbafdca53..6ba59deeb5 100644 --- a/xen/arch/x86/include/asm/domain.h +++ b/xen/arch/x86/include/asm/domain.h @@ -609,11 +609,12 @@ struct pv_vcpu struct arch_vcpu { /* - * The vCPU's register frame while it is not running: __context_switch= () - * copies it here from the stack when the vCPU is switched out, and ba= ck - * when it is switched in. Everything else goes through VCPU_MAP_REGS= (). + * The page holding the vCPU's register frame while it is not running, + * mapped in its state window: __context_switch() copies the frame the= re + * from the stack when the vCPU is switched out, and back when it is + * switched in. Everything else goes through VCPU_MAP_REGS(). */ - struct cpu_user_regs _user_regs; + struct page_info *regs_pg; =20 /* Debug registers. */ unsigned long dr[4]; @@ -722,12 +723,20 @@ static inline void free_vcpu_guest_context(struct vcp= u_guest_context *vgc) * registers for code working on a vCPU other than the one running (whose * registers are on the stack, see guest_cpu_user_regs()). * + * The frame is in the vCPU's state window: the window address serves while + * the vCPU runs here on its own page tables, and the page is mapped + * transiently otherwise, which cannot fail. + * * The pointer is valid until VCPU_UNMAP_REGS(), which clears it. A mappi= ng - * must not be held across a scheduling point. + * must not be held across a scheduling point: a transient one is only val= id + * on the pCPU which made it. * * @param v Owner of the register frame */ -#define VCPU_MAP_REGS(v) (&(v)->arch._user_regs) +struct cpu_user_regs *vcpu_map_regs(const struct vcpu *v); +void vcpu_unmap_regs(const struct vcpu *v, const struct cpu_user_regs *reg= s); + +#define VCPU_MAP_REGS(v) vcpu_map_regs(v) =20 /* * Drops the mapping of a vCPU's register frame and nullifies its pointer. @@ -735,7 +744,10 @@ static inline void free_vcpu_guest_context(struct vcpu= _guest_context *vgc) * @param v Owner of the register frame * @param r Register frame of v, from VCPU_MAP_REGS() */ -#define VCPU_UNMAP_REGS(v, r) do { (void)(v); (r) =3D NULL; } while ( fals= e ) +#define VCPU_UNMAP_REGS(v, r) do { \ + vcpu_unmap_regs(v, r); \ + (r) =3D NULL; \ +} while ( false ) =20 void arch_vcpu_regs_init(struct vcpu *v); =20 --=20 2.56.0 From nobody Mon Nov 23 22:32:25 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=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1795123880; cv=none; d=zohomail.com; s=zohoarc; b=XDGQm9mL68zS8l9xmn+ZALG0PqHlGAtPrPw+tgmauXLj41062NR9XambhpraZxGstzET1MJw2L8GC0F5861ZikfkLLwPAR3YfPFgy/bw5ubk3hyRoIfpowySYHK9kF9SR/DLrMwImku1B+6EJ1zhVmDW4BpcCJaDa6m+MdgmMHE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795123880; h=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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=EJ8nI9zELO37qSctxwOmwUmwj+cQVNwUVD+rgOtQBFc=; b=oHzsR7+nWRSSwlRUTZq7Qo1ql5pvvUQfh7MqCAM69AfTrLOILSBtxFcJ8eDiWp/HDA/MTDqgOJll3j64niHEEz2On8r7Gj+LuEsCxjGhOY+fDwaloIitUenUpRwndbRqmiv65EvDMi0fGBSRbqkIx0mjrqLerr0jrS6BU0uguuw= 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=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1795123880073184.82654805510367; Thu, 19 Nov 2026 13:31:20 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1497131.1695550 (Exim 4.92) (envelope-from ) id 1xU9iP-0006Et-86; Thu, 19 Nov 2026 21:30:45 +0000 Received: by outflank-mailman (output) from mailman id 1497131.1695550; Thu, 19 Nov 2026 21:30: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 1xU9iN-00066m-NZ; Thu, 19 Nov 2026 21:30:43 +0000 Received: by outflank-mailman (input) for mailman id 1497131; Thu, 19 Nov 2026 21:30:19 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9hz-0001Di-NF for xen-devel@lists.xenproject.org; Thu, 19 Nov 2026 21:30:19 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xU9hz-00000006tIs-16Uv; Thu, 19 Nov 2026 21:30:19 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xU9ML-0000000CVBH-1md6; Thu, 19 Nov 2026 21:07:57 +0000 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" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=EJ8nI9zELO37qSctxwOmwUmwj+cQVNwUVD+rgOtQBFc=; b=1LfP/xLIhuKIb1y3UIOaGTYhSY 0VAH/Ev0qHku5joC/SsJPKomJZQ07MevcDGLBA54FpiNbKQaS62u3ZWdFgpQb5fpaEB6K80jRZve0 hH62ljcfiB1y/KZj+P0ktwgFUyW33Pl8JgqY56kfsdVhPcUGEU+g3SKvyxGy1vCla1ZE=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie Subject: [PATCH v7 10/50] x86/hvm: move the emulator's operand caches behind the vCPU state window Date: Thu, 19 Nov 2026 21:07:01 +0000 Message-ID: <20261119-asi-hvm-v7-10-6551b6d5c879@xenproject.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> References: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1795123881729158500 Content-Type: text/plain; charset="utf-8" The HVM emulator keeps two per-vCPU caches of guest data: the read cache (struct hvmemul_cache), which records every guest-memory read made while an instruction is emulated -- guest page-table entries, instruction bytes and memory operands -- so that a re-execution observes the same values, and the three MMIO caches, which hold the operand bytes of an instruction's MMIO accesses. Both are xvmalloc() allocations, from the xmalloc pool on the xenheap, and so mapped in every context. Disabling the read cache when emulation completes leaves its entries in place. Put both in a domain-heap page of the vCPU's own, mapped for the life of the vCPU at a new common slot of its state window: the read cache, counts included, then the MMIO caches, 4032 bytes of the page. As "x86: move the saved register frame behind the vCPU state window" does for the register frame, this applies to every HVM vCPU; the window is in the per-domain area that all its domain's vCPUs share, which only that domain's page tables map. The pointers in struct hvm_vcpu_io hold window addresses. hvmemul_cache_init() sets the page up through a transient mapping, as the vCPU is still being built, and hvmemul_cache_destroy(), now out of line, unmaps it before freeing it. The emulator only ever works on the vCPU running, on its own page tables, and reaches the caches through the window address; hvmemul_find_mmio_cache() now asserts as much. The read cache also has users in other contexts: a debugger's guest page walk on a paused vCPU (gdbsx, and the guest stack dump of the debug keys), which reads and extends it if the vCPU was paused mid-emulation, and hvmemul_cancel(), which disables it, on toolstack, VCPUOP_initialise and INIT/SIPI paths. Its accessors therefore map the page transiently unless the vCPU is running on its own page tables, as VCPU_MAP_REGS() does: a single page, so no new failure paths. They drop the mapping with unmap_vcpu_state_page(), which also flushes this CPU's translation. The read and write paths are split into an outer function, which checks and maps the cache once, and a recursive inner one. Cost: a page per HVM vCPU, where the caches took about 4KiB of the xmalloc pool, and the emulator reaches them through a 4KiB mapping in the per-domain area instead of the directmap. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Changes in v7: - Message: on staging alone, without per-vCPU page tables or the sparse directmap view. - Port to new "shared architecture-specific slots" system. - Remove extraneous BUILD_BUG_ON Changes in v5: - New in this version. Notes for reviewers: the sizes before and after, on x86-64 (CONFIG_PAGING_LEVELS 4, MAX_INST_LEN 15). before after read cache ents 16*2*(4+1) + 15*(4+1) (16*2 + 15) * (4+1) =3D 235 (nents) =3D 235 (HVMEMUL_CACHE_ENTS) read cache bytes 8 + 235*16 =3D 3768 the same offsetof(), at 0 MMIO buffer bytes 64 (max_bytes) 64 (MMIO_CACHE_BYTES) MMIO cache bytes 24 + 64 =3D 88, three 88 (MMIO_CACHE_SIZE), three The page: offset bytes contents 0 8 num_ents, max_ents 8 3760 ents[235], 16 bytes each 3768 88 mmio_cache[0]: 24-byte header, buffer[64] 3856 88 mmio_cache[1] 3944 88 mmio_cache[2] 4032 64 unused The four old allocations together asked for 4032 bytes, the span the page now uses. The limits the code checks, max_ents (235) and space (64), are unchanged. The BUILD_BUG_ON() in hvmemul_cache_init() keeps the end within the page. --- xen/arch/x86/domain.c | 1 + xen/arch/x86/hvm/emulate.c | 196 +++++++++++++++++++------ xen/arch/x86/include/asm/config.h | 1 + xen/arch/x86/include/asm/hvm/emulate.h | 9 +- xen/arch/x86/include/asm/hvm/vcpu.h | 7 + 5 files changed, 164 insertions(+), 50 deletions(-) diff --git a/xen/arch/x86/domain.c b/xen/arch/x86/domain.c index b8dea08903..e605936797 100644 --- a/xen/arch/x86/domain.c +++ b/xen/arch/x86/domain.c @@ -588,6 +588,7 @@ int arch_vcpu_create(struct vcpu *v) BUILD_BUG_ON(VCPU_STATE_IDX_N2VMCB >=3D VCPU_STATE_NR_PAGES); BUILD_BUG_ON(VCPU_STATE_IDX_VMX_MSR >=3D VCPU_STATE_NR_PAGES); BUILD_BUG_ON(VCPU_STATE_IDX_REGS >=3D VCPU_STATE_NR_PAGES); + BUILD_BUG_ON(VCPU_STATE_IDX_EMUL >=3D VCPU_STATE_NR_PAGES); /* Page tables only; torn down with the rest of the per-domain are= a. */ rc =3D create_perdomain_mapping(d, VCPU_STATE_START(v), VCPU_STATE_NR_PAGES, diff --git a/xen/arch/x86/hvm/emulate.c b/xen/arch/x86/hvm/emulate.c index cd2831d34b..e2cf982c25 100644 --- a/xen/arch/x86/hvm/emulate.c +++ b/xen/arch/x86/hvm/emulate.c @@ -9,6 +9,7 @@ * Keir Fraser */ =20 +#include #include #include #include @@ -56,6 +57,26 @@ struct hvmemul_cache } ents[]; }; =20 +/* + * AVX512F scatter/gather insns can access up to 16 independent linear + * addresses, up to 8 bytes size. Each such linear range can span a page + * boundary, i.e. may require two page walks. Account for each insn byte + * individually, both for simplicity and to leave some slack space. + */ +#define HVMEMUL_CACHE_ENTS \ + ((16 * 2 + MAX_INST_LEN) * (CONFIG_PAGING_LEVELS + 1)) + +/* Operand bytes per MMIO cache: up to m512 or a tile row. */ +#define MMIO_CACHE_BYTES 64 + +/* Layout of the caches' page: the read cache, then the MMIO caches. */ +#define MMIO_CACHE_SIZE \ + ROUNDUP(offsetof(struct hvm_mmio_cache, buffer[MMIO_CACHE_BYTES]), \ + __alignof__(struct hvm_mmio_cache)) +#define EMUL_PAGE_MMIO_CACHE(i) \ + (ROUNDUP(offsetof(struct hvmemul_cache, ents[HVMEMUL_CACHE_ENTS]), \ + __alignof__(struct hvm_mmio_cache)) + (i) * MMIO_CACHE_SIZE) + static void hvmtrace_io_assist(const ioreq_t *p) { unsigned int size, event; @@ -1130,6 +1151,9 @@ static struct hvm_mmio_cache *hvmemul_find_mmio_cache( unsigned int i; struct hvm_mmio_cache *cache; =20 + ASSERT(hvio =3D=3D ¤t->arch.hvm.hvm_io); + ASSERT(this_cpu(pgtable_vcpu) =3D=3D current); + for ( i =3D 0; i < hvio->mmio_cache_count; i ++ ) { cache =3D hvio->mmio_cache[i]; @@ -3057,77 +3081,133 @@ void hvm_dump_emulation_state(const char *loglvl, = const char *prefix, =20 int hvmemul_cache_init(struct vcpu *v) { - /* - * AVX512F scatter/gather insns can access up to 16 independent linear - * addresses, up to 8 bytes size. Each such linear range can span a pa= ge - * boundary, i.e. may require two page walks. - */ - unsigned int nents =3D 16 * 2 * (CONFIG_PAGING_LEVELS + 1); - unsigned int i, max_bytes =3D 64; + struct hvm_vcpu_io *hvio =3D &v->arch.hvm.hvm_io; + struct page_info *pg =3D alloc_domheap_page(v->domain, MEMF_no_owner); + void *va =3D (void *)VCPU_STATE_ADDR(v, VCPU_STATE_IDX_EMUL), *p; struct hvmemul_cache *cache; + unsigned int i; + mfn_t mfn; =20 - /* - * Account for each insn byte individually, both for simplicity and to - * leave some slack space. - */ - nents +=3D MAX_INST_LEN * (CONFIG_PAGING_LEVELS + 1); + BUILD_BUG_ON(EMUL_PAGE_MMIO_CACHE(ARRAY_SIZE(hvio->mmio_cache)) > + PAGE_SIZE); =20 - cache =3D xvmalloc_flex_struct(struct hvmemul_cache, ents, nents); - if ( !cache ) + if ( !pg ) return -ENOMEM; =20 - /* Cache is disabled initially. */ - cache->num_ents =3D nents + 1; - cache->max_ents =3D nents; + mfn =3D page_to_mfn(pg); + clear_domain_page(mfn); =20 - v->arch.hvm.hvm_io.cache =3D cache; + /* v !=3D current, so we can't use the vCPU state window. */ + p =3D map_domain_page(mfn); =20 - for ( i =3D 0; i < ARRAY_SIZE(v->arch.hvm.hvm_io.mmio_cache); ++i ) + /* Cache is disabled initially. */ + cache =3D p; + cache->num_ents =3D HVMEMUL_CACHE_ENTS + 1; + cache->max_ents =3D HVMEMUL_CACHE_ENTS; + + for ( i =3D 0; i < ARRAY_SIZE(hvio->mmio_cache); ++i ) { - v->arch.hvm.hvm_io.mmio_cache[i] =3D - xvmalloc_flex_struct(struct hvm_mmio_cache, buffer, max_bytes); - if ( !v->arch.hvm.hvm_io.mmio_cache[i] ) - return -ENOMEM; - v->arch.hvm.hvm_io.mmio_cache[i]->space =3D max_bytes; + struct hvm_mmio_cache *mmio =3D p + EMUL_PAGE_MMIO_CACHE(i); + + mmio->space =3D MMIO_CACHE_BYTES; } =20 + unmap_domain_page(p); + + populate_perdomain_mapping(v, (unsigned long)va, &mfn, 1, + __PAGE_HYPERVISOR_RW); + + hvio->emul_pg =3D pg; + hvio->cache =3D va; + for ( i =3D 0; i < ARRAY_SIZE(hvio->mmio_cache); ++i ) + hvio->mmio_cache[i] =3D va + EMUL_PAGE_MMIO_CACHE(i); + return 0; } =20 +void hvmemul_cache_destroy(struct vcpu *v) +{ + struct hvm_vcpu_io *hvio =3D &v->arch.hvm.hvm_io; + unsigned int i; + + if ( !hvio->emul_pg ) + return; + + unmap_vcpu_state(v, VCPU_STATE_IDX_EMUL, 1); + free_domheap_page(hvio->emul_pg); + hvio->emul_pg =3D NULL; + hvio->cache =3D NULL; + for ( i =3D 0; i < ARRAY_SIZE(hvio->mmio_cache); ++i ) + hvio->mmio_cache[i] =3D NULL; +} + +/* + * The read cache of @v, for access from any context: the window address in + * struct hvm_vcpu_io where v runs here on its own page tables, a transient + * mapping of the page otherwise, and the NULL left there once the cache is + * destroyed. v's page tables being loaded is not enough: while this CPU + * holds v's state lazily, an IPI syncing that state for another CPU can + * switch them away at any time. + */ +static struct hvmemul_cache *map_cache(const struct vcpu *v) +{ + const struct hvm_vcpu_io *hvio =3D &v->arch.hvm.hvm_io; + + if ( !hvio->emul_pg || (v =3D=3D current && this_cpu(pgtable_vcpu) =3D= =3D v) ) + return hvio->cache; + + /* The read cache is at the start of the page. */ + return __map_domain_page(hvio->emul_pg); +} + +static void unmap_cache(const struct vcpu *v, + const struct hvmemul_cache *cache) +{ + if ( cache !=3D v->arch.hvm.hvm_io.cache ) + unmap_vcpu_state_page(cache); +} + +#define HVMEMUL_MAP_CACHE(v) map_cache(v) + +/* Drops the mapping of a vCPU's read cache and nullifies its pointer. */ +#define HVMEMUL_UNMAP_CACHE(v, cache) do { \ + unmap_cache(v, cache); \ + (cache) =3D NULL; \ +} while ( false ) + unsigned int hvmemul_cache_disable(struct vcpu *v) { - struct hvmemul_cache *cache =3D v->arch.hvm.hvm_io.cache; + struct hvmemul_cache *cache =3D HVMEMUL_MAP_CACHE(v); unsigned int token =3D cache->num_ents; =20 cache->num_ents =3D cache->max_ents + 1; =20 + HVMEMUL_UNMAP_CACHE(v, cache); + return token; } =20 void hvmemul_cache_restore(struct vcpu *v, unsigned int token) { - struct hvmemul_cache *cache =3D v->arch.hvm.hvm_io.cache; + struct hvmemul_cache *cache =3D HVMEMUL_MAP_CACHE(v); =20 ASSERT(cache->num_ents > cache->max_ents); cache->num_ents =3D token; + + HVMEMUL_UNMAP_CACHE(v, cache); } =20 -bool hvmemul_read_cache(const struct vcpu *v, paddr_t gpa, - void *buffer, unsigned int size) +static bool read_cache(const struct hvmemul_cache *cache, paddr_t gpa, + void *buffer, unsigned int size) { - const struct hvmemul_cache *cache =3D v->arch.hvm.hvm_io.cache; unsigned int i; =20 - /* Cache unavailable? */ - if ( !is_hvm_vcpu(v) || cache->num_ents > cache->max_ents ) - return false; - while ( size > sizeof(cache->ents->data) ) { i =3D gpa & (sizeof(cache->ents->data) - 1) ? -gpa & (sizeof(cache->ents->data) - 1) : sizeof(cache->ents->data); - if ( !hvmemul_read_cache(v, gpa, buffer, i) ) + if ( !read_cache(cache, gpa, buffer, i) ) return false; gpa +=3D i; buffer +=3D i; @@ -3144,22 +3224,37 @@ bool hvmemul_read_cache(const struct vcpu *v, paddr= _t gpa, return false; } =20 -void hvmemul_write_cache(const struct vcpu *v, paddr_t gpa, - const void *buffer, unsigned int size) +bool hvmemul_read_cache(const struct vcpu *v, paddr_t gpa, + void *buffer, unsigned int size) { - struct hvmemul_cache *cache =3D v->arch.hvm.hvm_io.cache; - unsigned int i; + const struct hvmemul_cache *cache; + bool hit =3D false; =20 - /* Cache unavailable? */ - if ( !is_hvm_vcpu(v) || cache->num_ents > cache->max_ents ) - return; + if ( !is_hvm_vcpu(v) ) + return false; + + cache =3D HVMEMUL_MAP_CACHE(v); + + /* Cache available? */ + if ( cache->num_ents <=3D cache->max_ents ) + hit =3D read_cache(cache, gpa, buffer, size); + + HVMEMUL_UNMAP_CACHE(v, cache); + + return hit; +} + +static void write_cache(const struct vcpu *v, struct hvmemul_cache *cache, + paddr_t gpa, const void *buffer, unsigned int size) +{ + unsigned int i; =20 while ( size > sizeof(cache->ents->data) ) { i =3D gpa & (sizeof(cache->ents->data) - 1) ? -gpa & (sizeof(cache->ents->data) - 1) : sizeof(cache->ents->data); - hvmemul_write_cache(v, gpa, buffer, i); + write_cache(v, cache, gpa, buffer, i); gpa +=3D i; buffer +=3D i; size -=3D i; @@ -3186,6 +3281,23 @@ void hvmemul_write_cache(const struct vcpu *v, paddr= _t gpa, cache->num_ents =3D i + 1; } =20 +void hvmemul_write_cache(const struct vcpu *v, paddr_t gpa, + const void *buffer, unsigned int size) +{ + struct hvmemul_cache *cache; + + if ( !is_hvm_vcpu(v) ) + return; + + cache =3D HVMEMUL_MAP_CACHE(v); + + /* Cache available? */ + if ( cache->num_ents <=3D cache->max_ents ) + write_cache(v, cache, gpa, buffer, size); + + HVMEMUL_UNMAP_CACHE(v, cache); +} + /* * Local variables: * mode: C diff --git a/xen/arch/x86/include/asm/config.h b/xen/arch/x86/include/asm/c= onfig.h index 789748b54c..b3c68308ad 100644 --- a/xen/arch/x86/include/asm/config.h +++ b/xen/arch/x86/include/asm/config.h @@ -313,6 +313,7 @@ extern unsigned long xen_phys_start; #define VCPU_STATE_IDX_VMX_MSR 0 /* Common slots */ #define VCPU_STATE_IDX_REGS 2 +#define VCPU_STATE_IDX_EMUL 3 =20 #define ELFSIZE 64 =20 diff --git a/xen/arch/x86/include/asm/hvm/emulate.h b/xen/arch/x86/include/= asm/hvm/emulate.h index 178ac32e15..16491a92b1 100644 --- a/xen/arch/x86/include/asm/hvm/emulate.h +++ b/xen/arch/x86/include/asm/hvm/emulate.h @@ -117,14 +117,7 @@ int hvmemul_do_pio_buffer(uint16_t port, * further re-execution. */ int __must_check hvmemul_cache_init(struct vcpu *v); -static inline void hvmemul_cache_destroy(struct vcpu *v) -{ - unsigned int i; - - for ( i =3D 0; i < ARRAY_SIZE(v->arch.hvm.hvm_io.mmio_cache); ++i ) - XVFREE(v->arch.hvm.hvm_io.mmio_cache[i]); - XVFREE(v->arch.hvm.hvm_io.cache); -} +void hvmemul_cache_destroy(struct vcpu *v); bool hvmemul_read_cache(const struct vcpu *v, paddr_t gpa, void *buffer, unsigned int size); void hvmemul_write_cache(const struct vcpu *v, paddr_t gpa, diff --git a/xen/arch/x86/include/asm/hvm/vcpu.h b/xen/arch/x86/include/asm= /hvm/vcpu.h index 876c230784..ee7bc8be00 100644 --- a/xen/arch/x86/include/asm/hvm/vcpu.h +++ b/xen/arch/x86/include/asm/hvm/vcpu.h @@ -46,6 +46,13 @@ struct hvm_vcpu_io { unsigned char mmio_insn[16]; struct hvmemul_cache *cache; =20 + /* + * The page holding the caches above, mapped in the vCPU's state windo= w: + * mmio_cache[] and cache hold addresses in the window, for use while = the + * vCPU's own page tables are loaded. + */ + struct page_info *emul_pg; + /* * For string instruction emulation we need to be able to signal a * necessary retry through other than function return codes. --=20 2.56.0 From nobody Mon Nov 23 22:32:25 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=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1795123891; cv=none; d=zohomail.com; s=zohoarc; b=Uppj9ox4YbgZd0Kn6GP/pahCUtZZDVEhV1ftvdJ/kNUKuZ6xfBR7rl1zl08EL00QArAgN41srD952xayK5A0VCUBD41nQSw9LKzETZRjEf7Zeh6in8G0vT8eQYMMg7+6M6xf2wIg7T1K2+jFgg+D4eUCGq8NJYDCwNpU/dB7WRE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795123891; h=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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=xMTLrvxxwpwmKfoWs2RiIcYg6g+Liai9ytC5nmpdgLQ=; b=NRRa8heRI0BG5tpmYX5KUcsaQIFvsJxVqehrmSJC5wqN4VWn1EAh+8F8gKRf8TFkfkC5K172A16npOIp0RdOePfR2OS65poY9SyGle97p3sAFpqzNAV+A0+sTJ4UZxV2hPG/UCJmc01ty0YRekHhWuk3UVI4h4MAZCRmeQ47dbs= 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=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1795123891824133.40699339810328; Thu, 19 Nov 2026 13:31:31 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1497140.1695647 (Exim 4.92) (envelope-from ) id 1xU9in-0002vn-4I; Thu, 19 Nov 2026 21:31:09 +0000 Received: by outflank-mailman (output) from mailman id 1497140.1695647; Thu, 19 Nov 2026 21:31:08 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9il-0002oa-7T; Thu, 19 Nov 2026 21:31:07 +0000 Received: by outflank-mailman (input) for mailman id 1497140; Thu, 19 Nov 2026 21:30:20 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9i0-0001ET-1k for xen-devel@lists.xenproject.org; Thu, 19 Nov 2026 21:30:20 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xU9hz-00000006tK8-2fCS; Thu, 19 Nov 2026 21:30:19 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xU9MM-0000000CVBH-1U15; Thu, 19 Nov 2026 21:07:58 +0000 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" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=xMTLrvxxwpwmKfoWs2RiIcYg6g+Liai9ytC5nmpdgLQ=; b=CAR/50bCIb+XQ2zmY6h4Cru7sT +1DxcUFm/9auP8NDoG6wDbJ1jUhzWyVXSYpGgrmnWNeHZVvMY7Oc2uGOldqXw/YMbaxvCmWevpiFW GdZkKlOrjJejWv6SJDPdhfZfw4nTd0e9y3L31TD+obV+y2E4ZYKihe5RmhUY24jauDEE=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie Subject: [PATCH v7 11/50] x86/hvm: clear the vCPU's copy of an emulated I/O request once consumed Date: Thu, 19 Nov 2026 21:07:02 +0000 Message-ID: <20261119-asi-hvm-v7-11-6551b6d5c879@xenproject.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> References: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1795123893802158500 Content-Type: text/plain; charset="utf-8" hvmemul_do_io() keeps the request it is emulating in the vCPU's struct vcpu_io: the data written, or the response once a device model has supplied it, with the address and repeat count. Once the access is done only the state is set back to STATE_IOREQ_NONE, so the operand stays in struct vcpu, a xenheap page mapped in every context, until the next emulated I/O overwrites it. Drop the copy wherever it is done with: when the response has been taken into hvmemul_do_io()'s local copy on the instruction's re-execution, when Xen's own handlers have completed the access, when the request is retried, suspended or not sent, and in hvmemul_cancel(). A request sent to a device model stays while it is in flight: the re-execution checks it, and the port-I/O completion reads its address and size before re-issuing it. One which needs no completion -- a port write, or a request whose data is a guest buffer's address -- loses its address, data and count as soon as it is sent: the device model has its own copy, and the completion path reads only the state, type, direction and data_is_ptr. Nothing reads the copy after STATE_IOREQ_NONE. Only the x86 emulator changes. struct vcpu_io is common code, and Arm's MMIO completion reads the response from it. Moving the in-flight copy out of struct vcpu belongs with narrowing the ioreq server pages, which carry the same operand while a device model handles it. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Changes in v5: - New in this version. --- xen/arch/x86/hvm/emulate.c | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/xen/arch/x86/hvm/emulate.c b/xen/arch/x86/hvm/emulate.c index e2cf982c25..da7c08b5d9 100644 --- a/xen/arch/x86/hvm/emulate.c +++ b/xen/arch/x86/hvm/emulate.c @@ -167,6 +167,16 @@ static const struct hvm_io_handler ioreq_server_handle= r =3D { .ops =3D &ioreq_server_ops }; =20 +/* + * The vCPU's copy of an emulated I/O request is done with: nothing reads = it + * before the next request overwrites it. Clear it all, so that the opera= nd + * and the address do not stay in struct vcpu. + */ +static void ioreq_done(struct vcpu_io *vio) +{ + vio->req =3D (ioreq_t){ .state =3D STATE_IOREQ_NONE }; +} + /* * Drop all records of in-flight emulation. This is needed whenever a vCPU= 's * register state may have changed behind the emulator's back. @@ -175,7 +185,7 @@ void hvmemul_cancel(struct vcpu *v) { struct hvm_vcpu_io *hvio =3D &v->arch.hvm.hvm_io; =20 - v->io.req.state =3D STATE_IOREQ_NONE; + ioreq_done(&v->io); v->io.completion =3D VIO_no_completion; hvio->mmio_cache_count =3D 0; hvio->mmio_insn_bytes =3D 0; @@ -265,8 +275,9 @@ static int hvmemul_do_io( case STATE_IOREQ_NONE: break; case STATE_IORESP_READY: - vio->req.state =3D STATE_IOREQ_NONE; + /* The local copy carries the response from here. */ p =3D vio->req; + ioreq_done(vio); =20 /* Verify the emulation request has been correctly re-issued */ if ( (p.type !=3D (is_mmio ? IOREQ_TYPE_COPY : IOREQ_TYPE_PIO)) || @@ -330,7 +341,7 @@ static int hvmemul_do_io( { case X86EMUL_OKAY: case X86EMUL_RETRY: - vio->req.state =3D STATE_IOREQ_NONE; + ioreq_done(vio); break; case X86EMUL_UNHANDLEABLE: { @@ -383,7 +394,7 @@ static int hvmemul_do_io( if ( s =3D=3D NULL ) { rc =3D X86EMUL_RETRY; - vio->req.state =3D STATE_IOREQ_NONE; + ioreq_done(vio); break; } =20 @@ -394,7 +405,7 @@ static int hvmemul_do_io( if ( dir =3D=3D IOREQ_READ ) { rc =3D hvm_process_io_intercept(&ioreq_server_handler,= &p); - vio->req.state =3D STATE_IOREQ_NONE; + ioreq_done(vio); break; } } @@ -440,7 +451,7 @@ static int hvmemul_do_io( paddr_to_pfn(addr)); =20 rc =3D X86EMUL_RETRY; - vio->req.state =3D STATE_IOREQ_NONE; + ioreq_done(vio); break; } =20 @@ -455,15 +466,24 @@ static int hvmemul_do_io( dir ? "read from" : "write to", addr, size); } rc =3D hvm_process_io_intercept(&null_handler, &p); - vio->req.state =3D STATE_IOREQ_NONE; + ioreq_done(vio); } else { rc =3D ioreq_send(s, &p, 0); if ( rc !=3D X86EMUL_RETRY || vio->suspended ) - vio->req.state =3D STATE_IOREQ_NONE; + ioreq_done(vio); else if ( !ioreq_needs_completion(&vio->req) ) + { + /* + * Nothing comes back, and the device model has its own co= py + * of the request: keep only what completion checks. + */ + vio->req.addr =3D 0; + vio->req.data =3D 0; + vio->req.count =3D 0; rc =3D X86EMUL_OKAY; + } } break; } --=20 2.56.0 From nobody Mon Nov 23 22:32:25 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=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1795123839; cv=none; d=zohomail.com; s=zohoarc; b=Xihutz90Rs7G3/yAf0EVPQqG0GJNs/rPOz+5/2QWH3OFHpT5VCyyj/XUQlGDlDfzQTW/HdqyYTYRQYFF/t4e6QsCoii01zzn6Wu87m4i9PzNa27RyqboWvf3z0dPRlVrKlvIlFEBtMW7oWZyZkPU1hyLhmX/bbdf0xiOAbLiTqI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795123839; h=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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=WJ3aymglgYVjgjhwjh7T6Sw3B0M8iQ/bEW93NzEf/2U=; b=f37UxR3BaK0ypZ84b7xiGsNwciXk4PTwR9SnzC0LpdPXsmzZVEXXMc9YddpB/3AFxwjwmgMkPH2wFZExPtSEgmqwiot5EXAXvl0TUGcKdVCuRy4GKi292aJfFIZFtc0KJRFVLIVA1SpRqEj4mtMOpwjmM54Dk4xq5ZH+KGPo72A= 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=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1795123839888483.69160949186494; Thu, 19 Nov 2026 13:30:39 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1497113.1695403 (Exim 4.92) (envelope-from ) id 1xU9i1-0001WC-Qd; Thu, 19 Nov 2026 21:30:21 +0000 Received: by outflank-mailman (output) from mailman id 1497113.1695403; Thu, 19 Nov 2026 21:30:21 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9i1-0001Tf-Jo; Thu, 19 Nov 2026 21:30:21 +0000 Received: by outflank-mailman (input) for mailman id 1497113; Thu, 19 Nov 2026 21:30:19 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9hz-0001Bx-07 for xen-devel@lists.xenproject.org; Thu, 19 Nov 2026 21:30:19 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xU9hy-00000006tG9-1iLK; Thu, 19 Nov 2026 21:30:18 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xU9MN-0000000CVBH-2vu6; Thu, 19 Nov 2026 21:07:59 +0000 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" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=WJ3aymglgYVjgjhwjh7T6Sw3B0M8iQ/bEW93NzEf/2U=; b=wNcyFSN+LXrrTZJeBAYTkDY7Un YF9j0JV8jOqPs7bLGh2Y+8r72BPd5WMfDIoLEG8ZTzTfNH7eyBtV6nDyNuxMeBJQHGLyvmi2Sj4wX OHl8VHPu1FX00c/zGuDJXfEV9koeS+VAknCFZF99oxyULlERg7gbKmCDGpm2d9dcq4CY=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie , Anthony PERARD , Michal Orzel , Julien Grall , Stefano Stabellini Subject: [PATCH v7 12/50] x86/domain_page: introduce map_domain_pages() Date: Thu, 19 Nov 2026 21:07:03 +0000 Message-ID: <20261119-asi-hvm-v7-12-6551b6d5c879@xenproject.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> References: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1795123841418158500 Content-Type: text/plain; charset="utf-8" map_domain_page() maps a single page, so callers needing a virtually contiguous mapping of several (not necessarily physically contiguous) frames have had to fall back to vmap(), which maps whatever the frames hold in every context for the lifetime of the mapping, and whose vunmap() flushes every CPU's TLB, once per page. Provide a multipage counterpart which maps them in the per-domain area of the calling vCPU's domain instead, which only that domain's page tables carry: map_domain_pages(mfn, nr) / unmap_domain_pages(ptr, nr) Each vCPU has MAPCACHE_MULTIPAGE_ENTRIES slots of its own for such mappings, a fixed block at the end of the mapcache sub-area. No single-page mapcache is needed: the multipage slots are in the same VA range whether or not the domain has one. The mappings are order-aligned and tracked in a per-vCPU bitmap, and bypass the maphash and the deferred garbage/epoch machinery. At unmap a mapping's PTEs are cleared, and a local flush done. Since each vCPU has its own slots, the local flush suffices for correctness, although every vCPU's slots are in the domain's one L3: even if a stray TLB entry is speculatively loaded on another processor, no other vCPU will use that mapping, and it will be flushed before the vCPU which owns the slots runs on that CPU. With regard to exposure: the domain's other vCPUs may be able to reach the mapping speculatively while it exists, and afterwards through a stale TLB entry until their CPU next flushes. This doesn't expose anything new, as the page is already mapped in the directmap anyway (unless it's a PV domain and the page is past the short directmap), and it is less than vmap() exposes: every context carries the vmap area. An unmap will BUG() when given a pointer or a @nr which indexes outside the vCPU's slots, unless that pointer is in the vmap area (see below). These slots are not available before the first page-table switch in boot, nor with the idle vCPU's page tables loaded. Currently no calls are made during boot, and the only known way to reach map_domain_pages() with the idle page tables loaded is on an error path that will already ultimately crash the host. Nonetheless, for robustness, if called from a context where no mapping is available, it will fall back to using vmap(); and unmap recognizes such an address and vunmap()'s it. This briefly exposes contents to every context, and requires a system-wide TLB shootdown on unmap, but allows execution to continue. The alternative would be to BUG() in such a context. The first fallback is logged. The handling of a lazy context switch in mapcache_current_vcpu() moves into pgtable_current_vcpu(), which map_domain_pages() uses whatever the domain: an IPI can switch away page tables held lazily at any time. Eight slots hold the largest mapping a known caller needs, a three-page XSAVE area (four slots, order 2), with room for a two-page mapping beside it. The multipage area takes 256MiB of address space for MAX_VIRT_CPUS, and one L1 table per 64 vCPUs. The slot bitmap is a field of struct arch_vcpu, as struct mapcache_vcpu exists for PV vCPUs only; it sits in the struct's tail padding, which leaves its size unchanged. Later patches convert the two known in-tree consumers. Moving the XSAVE areas off the xenheap needs the primitive too: the XSAVE area exceeds one page on AMX-class hardware, and XSAVE/XRSTOR require it to be virtually contiguous. Assisted-by: Claude Code:claude-fable-5, Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Changes in v7: - Standalone on staging, without the per-vCPU mapcache or the asi=3D option: the multipage slots are each vCPU's block of the domain-wide multipage area, the slot bitmap is a field of struct arch_vcpu (in its tail padding), and there is no vmap-strict sub-option; the vmap() fallback and its message stay. Changes in v5: - New in this version. --- xen/arch/x86/domain_page.c | 166 +++++++++++++++++++++++++++--- xen/arch/x86/include/asm/config.h | 11 ++ xen/arch/x86/include/asm/domain.h | 3 + xen/include/xen/domain_page.h | 21 ++++ 4 files changed, 188 insertions(+), 13 deletions(-) diff --git a/xen/arch/x86/domain_page.c b/xen/arch/x86/domain_page.c index f02be1675b..ea759155da 100644 --- a/xen/arch/x86/domain_page.c +++ b/xen/arch/x86/domain_page.c @@ -6,6 +6,7 @@ * Copyright (c) 2003-2006, Keir Fraser */ =20 +#include #include #include #include @@ -18,24 +19,22 @@ #include #include =20 -static inline struct vcpu *mapcache_current_vcpu(void) +/* + * The vCPU whose page tables are loaded here, and stay loaded until the + * caller is done with what it maps in them. + */ +static struct vcpu *pgtable_current_vcpu(void) { - const struct vcpu *v =3D this_cpu(pgtable_vcpu); const struct vcpu *curr =3D current; =20 - /* - * During early boot pgtable_vcpu is not set, callers must handle NULL. - * Non-PV domains don't have a mapcache, the directmap covers all phys= ical - * address space. - */ - if ( !v || !is_pv_vcpu(v) ) + /* During early boot pgtable_vcpu is not set, callers must handle NULL= . */ + if ( !this_cpu(pgtable_vcpu) ) return NULL; =20 /* - * If we are in a lazy context-switch state from a PV vCPU do a full s= witch - * to the idle vCPU now, otherwise an incoming FLUSH_VCPU_STATE IPI wo= uld - * change the page tables under our feet an invalidate any in-use mapc= ache - * entries. + * If we are in a lazy context-switch state do a full switch to the id= le + * vCPU now, otherwise an incoming FLUSH_VCPU_STATE IPI would change t= he + * page tables under our feet and invalidate any in-use mappings in th= em. */ if ( unlikely(this_cpu(curr_vcpu) !=3D curr) ) { @@ -54,6 +53,21 @@ static inline struct vcpu *mapcache_current_vcpu(void) return ACCESS_ONCE(this_cpu(pgtable_vcpu)); } =20 +static inline struct vcpu *mapcache_current_vcpu(void) +{ + const struct vcpu *v =3D this_cpu(pgtable_vcpu); + + /* + * During early boot pgtable_vcpu is not set, callers must handle NULL. + * Non-PV domains don't have a mapcache, the directmap covers all phys= ical + * address space. + */ + if ( !v || !is_pv_vcpu(v) ) + return NULL; + + return pgtable_current_vcpu(); +} + #define mapcache_l2_entry(e) ((e) >> PAGETABLE_ORDER) #define MAPCACHE_L2_ENTRIES (mapcache_l2_entry(MAPCACHE_ENTRIES - 1) + 1) #define MAPCACHE_L1ENT(idx) \ @@ -223,6 +237,119 @@ void unmap_domain_page(const void *ptr) local_irq_restore(flags); } =20 +/* + * The mapcache index of @v's first map_domain_pages() slot: at its stride= of + * the multipage area. + */ +static unsigned int multipage_first_slot(const struct vcpu *v) +{ + return PFN_DOWN(MAPCACHE_MULTIPAGE_START(v) - MAPCACHE_VIRT_START); +} + +/* + * Multipage mappings come from slots of the vCPU whose page tables are + * loaded, which no other vCPU uses, and bypass the maphash and the + * garbage/epoch machinery: at unmap a mapping's PTEs are zapped, and their + * translations dropped from this CPU's TLB, at their addresses and at the= ir + * aliases in the per-domain mirror. + */ +void *map_domain_pages(const mfn_t *mfn, unsigned int nr) +{ + unsigned long flags; + unsigned int i, first; + struct vcpu *v; + int idx; + + if ( !nr || nr > MAPCACHE_MULTIPAGE_ENTRIES ) + { + ASSERT_UNREACHABLE(); + return NULL; + } + + if ( nr =3D=3D 1 ) + return map_domain_page(mfn[0]); + + /* Early boot, or the idle vCPU's page tables: no multipage slots. */ + v =3D pgtable_current_vcpu(); + if ( !v || is_idle_vcpu(v) ) + { + printk_once(XENLOG_WARNING + "%pS: map_domain_pages() falling back to vmap()\n", + __builtin_return_address(0)); + return vmap(mfn, nr); + } + + first =3D multipage_first_slot(v); + + local_irq_save(flags); + + idx =3D bitmap_find_free_region(&v->arch.multipage_inuse, + MAPCACHE_MULTIPAGE_ENTRIES, + get_order_from_pages(nr)); + if ( idx >=3D 0 ) + for ( i =3D 0; i < nr; i++ ) + l1e_write(&MAPCACHE_L1ENT(first + idx + i), + l1e_from_mfn(mfn[i], __PAGE_HYPERVISOR_RW)); + + local_irq_restore(flags); + + if ( idx < 0 ) + return NULL; + + return (void *)MAPCACHE_VIRT_START + pfn_to_paddr(first + idx); +} + +void unmap_domain_pages(const void *ptr, unsigned int nr) +{ + unsigned long va =3D (unsigned long)ptr & PAGE_MASK; + unsigned long flags, slot; + unsigned int idx, first, i; + struct vcpu *v; + + BUG_ON(!nr); + + if ( nr =3D=3D 1 ) + { + unmap_domain_page(ptr); + return; + } + + if ( va >=3D VMAP_VIRT_START && va < VMAP_VIRT_END ) + { + vunmap((void *)va); + return; + } + + v =3D pgtable_current_vcpu(); + BUG_ON(!v || is_idle_vcpu(v) || va < MAPCACHE_VIRT_START); + + first =3D multipage_first_slot(v); + slot =3D PFN_DOWN(va - MAPCACHE_VIRT_START); + BUG_ON(slot < first || slot - first >=3D MAPCACHE_MULTIPAGE_ENTRIES); + + idx =3D slot - first; + BUG_ON(nr > MAPCACHE_MULTIPAGE_ENTRIES - idx); + + local_irq_save(flags); + + for ( i =3D 0; i < nr; i++ ) + { + unsigned long addr =3D va + i * PAGE_SIZE; + + ASSERT(l1e_get_flags(MAPCACHE_L1ENT(slot + i)) & _PAGE_PRESENT); + l1e_write(&MAPCACHE_L1ENT(slot + i), l1e_empty()); + flush_tlb_one_local(addr); + if ( !is_pv_64bit_vcpu(v) ) + flush_tlb_one_local(addr - PERDOMAIN_VIRT_START + + PERDOMAIN_ALT_VIRT_START); + } + + bitmap_release_region(&v->arch.multipage_inuse, idx, + get_order_from_pages(nr)); + + local_irq_restore(flags); +} + void mapcache_domain_init(struct domain *d) { struct mapcache_domain *dcache =3D &d->arch.pv.mapcache; @@ -237,7 +364,7 @@ void mapcache_domain_init(struct domain *d) =20 BUILD_BUG_ON(MAPCACHE_VIRT_END + PAGE_SIZE * (3 + 2 * PFN_UP(BITS_TO_LONGS(MAPCACHE_ENTRIES) * sizeof(long)= )) > - MAPCACHE_VIRT_START + (PERDOMAIN_SLOT_MBYTES << 20)); + MAPCACHE_MULTIPAGE_VIRT_START); bitmap_pages =3D PFN_UP(BITS_TO_LONGS(MAPCACHE_ENTRIES) * sizeof(long)= ); dcache->inuse =3D (void *)MAPCACHE_VIRT_END + PAGE_SIZE; dcache->garbage =3D dcache->inuse + @@ -254,6 +381,19 @@ int mapcache_vcpu_init(struct vcpu *v) unsigned int ents =3D d->max_vcpus * MAPCACHE_VCPU_ENTRIES; unsigned int nr =3D PFN_UP(BITS_TO_LONGS(ents) * sizeof(long)); =20 + BUILD_BUG_ON(MAPCACHE_MULTIPAGE_ENTRIES > BITS_PER_LONG); + + /* The vCPU's stride of the multipage area. */ + if ( !is_idle_domain(d) ) + { + int rc =3D create_perdomain_mapping(d, MAPCACHE_MULTIPAGE_START(v), + MAPCACHE_MULTIPAGE_ENTRIES, + NIL(l1_pgentry_t *), NULL); + + if ( rc ) + return rc; + } + if ( !is_pv_vcpu(v) || !dcache->inuse ) return 0; =20 diff --git a/xen/arch/x86/include/asm/config.h b/xen/arch/x86/include/asm/c= onfig.h index b3c68308ad..8e9b856a6c 100644 --- a/xen/arch/x86/include/asm/config.h +++ b/xen/arch/x86/include/asm/config.h @@ -279,10 +279,21 @@ extern unsigned long xen_phys_start; =20 /* map_domain_page() map cache. The second per-domain-mapping sub-area. */ #define MAPCACHE_VCPU_ENTRIES (CONFIG_PAGING_LEVELS * CONFIG_PAGING_LEV= ELS) +/* Multipage slots of each vCPU, at its stride of the multipage area. */ +#define MAPCACHE_MULTIPAGE_ORDER 3 +#define MAPCACHE_MULTIPAGE_ENTRIES (1U << MAPCACHE_MULTIPAGE_ORDER) #define MAPCACHE_ENTRIES (MAX_VIRT_CPUS * MAPCACHE_VCPU_ENTRIES) #define MAPCACHE_VIRT_START PERDOMAIN_VIRT_SLOT(1) #define MAPCACHE_VIRT_END (MAPCACHE_VIRT_START + \ MAPCACHE_ENTRIES * PAGE_SIZE) +/* The multipage area, at the end of the sub-area. */ +#define MAPCACHE_MULTIPAGE_VA_SHIFT (MAPCACHE_MULTIPAGE_ORDER + PAGE_SHIFT) +#define MAPCACHE_MULTIPAGE_VIRT_START \ + (MAPCACHE_VIRT_START + (PERDOMAIN_SLOT_MBYTES << 20) - \ + (MAX_VIRT_CPUS << MAPCACHE_MULTIPAGE_VA_SHIFT)) +#define MAPCACHE_MULTIPAGE_START(v) \ + (MAPCACHE_MULTIPAGE_VIRT_START + \ + ((v)->vcpu_id << MAPCACHE_MULTIPAGE_VA_SHIFT)) =20 /* Argument translation area. The third per-domain-mapping sub-area. */ #define ARG_XLAT_VIRT_START PERDOMAIN_VIRT_SLOT(2) diff --git a/xen/arch/x86/include/asm/domain.h b/xen/arch/x86/include/asm/d= omain.h index 6ba59deeb5..3980d6228a 100644 --- a/xen/arch/x86/include/asm/domain.h +++ b/xen/arch/x86/include/asm/domain.h @@ -685,6 +685,9 @@ struct arch_vcpu =20 struct vcpu_msrs *msrs; =20 + /* Which of the vCPU's map_domain_pages() slots are in use. */ + unsigned long multipage_inuse; + struct { bool next_interrupt_enabled; } monitor; diff --git a/xen/include/xen/domain_page.h b/xen/include/xen/domain_page.h index c89b149e54..bf45a4cf55 100644 --- a/xen/include/xen/domain_page.h +++ b/xen/include/xen/domain_page.h @@ -31,6 +31,27 @@ void *map_domain_page(mfn_t mfn); */ void unmap_domain_page(const void *ptr); =20 +/* + * Map @nr page frames, not necessarily physically contiguous, at + * consecutive virtual addresses. Returns the virtual address of the + * first frame, or NULL when no space is available. As with + * map_domain_page(), the mapping is only valid within the current VCPU, + * and must be unmapped by it; unmap_domain_pages() takes the same @nr, + * and accepts a pointer carrying an in-page offset. Implemented on x86 + * only at present. + * + * @nr has an implementation-defined upper bound (x86: + * MAPCACHE_MULTIPAGE_ENTRIES). Exceeding it is a caller bug rather than a + * transient failure, and fails the mapping; callers needing more pages + * than a per-VCPU window holds want vmap() directly. + * + * unmap_domain_pages() is fatal given a pointer or an @nr which indexes + * outside the current VCPU's multipage slots. A wrong @nr within them + * is not detected: callers must pass the @nr they mapped with. + */ +void *map_domain_pages(const mfn_t *mfn, unsigned int nr); +void unmap_domain_pages(const void *ptr, unsigned int nr); + /* * Given a VA from map_domain_page(), return its underlying MFN. */ --=20 2.56.0 From nobody Mon Nov 23 22:32:25 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=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1795123902; cv=none; d=zohomail.com; s=zohoarc; b=DnvVPkMopQ16npvHebyzywFImnXsLmJH03hzo7x0HGbPbwioe25X0/Av65/myFJJHHxozDThe9cG4RpCPpemSUToSKM7XxhuWTch7+vBBlrHq3HuOu+095i4x958+w7thxrZJPvu2XlqLOPDhxMIouW1YZGf6ScHxv3xzVx180I= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795123902; h=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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=VrTHOM+oapzwonGC8+HHLpefXsYrozgjan9bVuXwpDQ=; b=F5IemgveSlHR046MRs7LYNfvyCdYyl4b6QTJuYy41Cnc5XxuPiGqtREdbnENKQc4GqOlILGuDKlh8yPAfRC3fRo/xTy1BTfTDZwUfQGalBHXlXHpbcObGKs5kYyCjR1WSIOfpbgB1QMIHGfLkIwiEexQ8olu4UfKgUV5Nf2oLWg= 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=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1795123902324239.12089967433542; Thu, 19 Nov 2026 13:31:42 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1497149.1695706 (Exim 4.92) (envelope-from ) id 1xU9j2-000653-98; Thu, 19 Nov 2026 21:31:24 +0000 Received: by outflank-mailman (output) from mailman id 1497149.1695706; Thu, 19 Nov 2026 21:31:23 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9j0-0005yA-Jr; Thu, 19 Nov 2026 21:31:22 +0000 Received: by outflank-mailman (input) for mailman id 1497149; Thu, 19 Nov 2026 21:30:20 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9i0-0001FE-Ao for xen-devel@lists.xenproject.org; Thu, 19 Nov 2026 21:30:20 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xU9hz-00000006tJs-2Elt; Thu, 19 Nov 2026 21:30:19 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xU9MO-0000000CVBH-3AOZ; Thu, 19 Nov 2026 21:08:00 +0000 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" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=VrTHOM+oapzwonGC8+HHLpefXsYrozgjan9bVuXwpDQ=; b=lIO1an+vDD30Xj9VxtPDhmkbm1 hg+6axfnqngfdTTZI4ivVHHzSIWwTVpiQeKLlLWtD/b5e48n0pDnwuLArRJk6vqdOKnFYghRF2lgp YwYBl4oGuoiJyFJlXCZxTQubLAW1meyEQvafExcqEz2xnO1DmCtp4FFK4IKiA0l7fCJ8=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Jan Beulich , Andrew Cooper , Tim Deegan , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie Subject: [PATCH v7 13/50] x86/shadow: use map_domain_pages() for cross-page emulated writes Date: Thu, 19 Nov 2026 21:07:04 +0000 Message-ID: <20261119-asi-hvm-v7-13-6551b6d5c879@xenproject.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> References: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1795123903831158500 Content-Type: text/plain; charset="utf-8" sh_emulate_map_dest() picks its mapping method purely on whether the write straddles a page boundary: a single page goes through map_domain_page(), two pages through vmap() -- mapping the destination in every context for the duration, and flushing every CPU's TLB, once per page, at vunmap(). map_domain_pages() maps it only in the page tables of the guest's own domain, and flushes only this CPU's TLB; use it for the two-page case. The NULL return handling is unchanged: the primitive can fail only for want of a free slot, and the emulation then bails out as unhandleable exactly as before. This is one of the two in-tree consumers of the primitive. Assisted-by: Claude Code:claude-fable-5, Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Changes in v7: - Message: without the sparse directmap view, the gain is the flush. Changes in v5: - New in this version. --- xen/arch/x86/mm/shadow/hvm.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xen/arch/x86/mm/shadow/hvm.c b/xen/arch/x86/mm/shadow/hvm.c index e6fb97c4b6..6c554ef29d 100644 --- a/xen/arch/x86/mm/shadow/hvm.c +++ b/xen/arch/x86/mm/shadow/hvm.c @@ -506,7 +506,7 @@ static void *sh_emulate_map_dest(struct vcpu *v, unsign= ed long vaddr, /* Cross-page writes mean probably not a pagetable. */ sh_remove_shadows(d, sh_ctxt->mfn[1], 0, 0 /* Slow, can fail. */ ); =20 - map =3D vmap(sh_ctxt->mfn, 2); + map =3D map_domain_pages(sh_ctxt->mfn, 2); if ( !map ) { put_page(mfn_to_page(sh_ctxt->mfn[0])); @@ -670,7 +670,7 @@ static void sh_emulate_unmap_dest(struct vcpu *v, void = *addr, if ( unlikely(mfn_valid(sh_ctxt->mfn[1])) ) { put_page(mfn_to_page(sh_ctxt->mfn[1])); - vunmap((void *)((unsigned long)addr & PAGE_MASK)); + unmap_domain_pages(addr, 2); } else unmap_domain_page(addr); --=20 2.56.0 From nobody Mon Nov 23 22:32:25 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=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1795123848; cv=none; d=zohomail.com; s=zohoarc; b=KcIUHXFHODdnfEsN2cSHI5KqhR+OSdM1XeYP9yoHHNreBSdyPyTjPig8Vi+i0VMmkEsOAphfCCe3qlHkj0cu314Ol4caWgdyK7OaErDLsuXsjXHtUgWy0i92P8Ab/Ss0YsiHB2WVmGl962FS56aIRZFjThgDUPIQ7oqaJxcUx80= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795123848; h=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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=g4iK0SndlB5IzbcVnNbOzmkNOR3Ygl5f3vD8v691NXg=; b=axGrg2HkFwaw7rOjztCG+gDjGZq9/3hD4my/UDL4/RV3NsYyBl3Z/AA7B5rVJtJxUWiPUUMbIoU/aR6k3JgmTP0dymIfBDcIOBajPjlgYxy1bBLYowTa/n4eyu7WXm4ZFQdiZ+fiCPnmFzeqFmaW/DAJbevoZ3fcAai91katDaU= 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=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1795123848719939.3698920513556; Thu, 19 Nov 2026 13:30:48 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1497111.1695389 (Exim 4.92) (envelope-from ) id 1xU9i1-0001L4-7n; Thu, 19 Nov 2026 21:30:21 +0000 Received: by outflank-mailman (output) from mailman id 1497111.1695389; Thu, 19 Nov 2026 21:30:21 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9i1-0001Jt-2t; Thu, 19 Nov 2026 21:30:21 +0000 Received: by outflank-mailman (input) for mailman id 1497111; Thu, 19 Nov 2026 21:30:19 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9hy-0001Br-Vi for xen-devel@lists.xenproject.org; Thu, 19 Nov 2026 21:30:18 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xU9hy-00000006tGB-1tNf; Thu, 19 Nov 2026 21:30:18 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xU9MP-0000000CVBH-2rIm; Thu, 19 Nov 2026 21:08:01 +0000 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" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=g4iK0SndlB5IzbcVnNbOzmkNOR3Ygl5f3vD8v691NXg=; b=GFbTrTAiTE2KcAZi2g7Hc19KeV xHYqcOh+riPYUH3UwzfBYQlBXGclMk/pfUDVGCc8pq0Zw65LFmrEWLim7Tl45kpl8LnuRVVFXKIeo pmtwNhUafQOopofbpZUirVeMJrBdwmr7lP99SP2WXWGFRReQ917OamGetojMuuBd8Odk=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie Subject: [PATCH v7 14/50] x86/hvm: use map_domain_pages() for multi-frame emulated accesses Date: Thu, 19 Nov 2026 21:07:05 +0000 Message-ID: <20261119-asi-hvm-v7-14-6551b6d5c879@xenproject.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> References: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1795123849353158500 Content-Type: text/plain; charset="utf-8" hvmemul_map_linear_addr() picks its mapping method purely on the frame count: one frame goes through map_domain_page(), more through vmap() -- mapping the guest data in every context for the duration, and flushing every CPU's TLB, once per page, at vunmap(). map_domain_pages() maps them only in the page tables of the guest's own domain, and flushes only this CPU's TLB; use it for the multi-frame case. The NULL return handling is unchanged: the primitive can fail only for want of a free slot, and the access then bails out as unhandleable exactly as before. This is one of the two in-tree consumers of the primitive. Other long-lived global mappings of guest pages (the ioreq server pages among them) remain in the vmap area. Assisted-by: Claude Code:claude-fable-5, Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Changes in v7: - Message: without the sparse directmap view, the gain is the flush. Changes in v5: - New in this version. --- xen/arch/x86/hvm/emulate.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/xen/arch/x86/hvm/emulate.c b/xen/arch/x86/hvm/emulate.c index da7c08b5d9..7e7beccb93 100644 --- a/xen/arch/x86/hvm/emulate.c +++ b/xen/arch/x86/hvm/emulate.c @@ -735,7 +735,8 @@ static void *hvmemul_map_linear_addr( =20 /* * The maximum access size depends on the number of adjacent mfns[] wh= ich - * can be vmap()'d, accouting for possible misalignment within the reg= ion. + * can be mapped together, accounting for possible misalignment within + * the region. * The higher level emulation callers are responsible for ensuring that * mfns[] is large enough for the requested access size. */ @@ -824,9 +825,9 @@ static void *hvmemul_map_linear_addr( /* Entire access within a single frame? */ if ( nr_frames =3D=3D 1 ) mapping =3D map_domain_page(hvmemul_ctxt->mfn[0]); - /* Multiple frames? Need to vmap(). */ - else if ( (mapping =3D vmap(hvmemul_ctxt->mfn, - nr_frames)) =3D=3D NULL ) + /* Multiple frames? Need a multi-page mapping. */ + else if ( (mapping =3D map_domain_pages(hvmemul_ctxt->mfn, + nr_frames)) =3D=3D NULL ) goto unhandleable; =20 #ifndef NDEBUG /* Poision unused mfn[]s with INVALID_MFN. */ @@ -867,7 +868,7 @@ static void hvmemul_unmap_linear_addr( if ( nr_frames =3D=3D 1 ) unmap_domain_page(mapping); else - vunmap(mapping); + unmap_domain_pages(mapping, nr_frames); =20 for ( i =3D 0; i < nr_frames; i++ ) { --=20 2.56.0 From nobody Mon Nov 23 22:32:25 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=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1795123917; cv=none; d=zohomail.com; s=zohoarc; b=dVg3iNcYlO+/AkcsSDPtJobeB86scnQiF4OSrNbFeOA6yEQ0UMECgY7uY8oSekPKuTpq178Pxl+QBS49PSkhlw4eZ96QcapQd9zxC7Yl4AaPsVJ8zStrCkip9HqpoQZ4ZqxhCkQNqfHZF263NRMrzypwmYzGlno4lufCp3eYbps= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795123917; h=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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=FlX8i68BurZZbddP5JFg/suxuQm3LKHJ87QfUKq0fho=; b=K9/Qt7Sh2OQH/qYZAWQIXIjF1bIm8o8fgiS5ro2G9GvQP/MuwIzF+UIUv3713HBqnXVN4/vjvgWQWOTZdWF9q0UD1OWVllDpb2No3JjYfdXkRP+mIeYvEHYHjGEmq3KtsiFsHJZKPLl4kTEQ39lxy+DgIQhP398QP/j/9bCGwEk= 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=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1795123917602705.4630894465059; Thu, 19 Nov 2026 13:31:57 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1497146.1695699 (Exim 4.92) (envelope-from ) id 1xU9j1-0005mh-10; Thu, 19 Nov 2026 21:31:23 +0000 Received: by outflank-mailman (output) from mailman id 1497146.1695699; Thu, 19 Nov 2026 21:31:22 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9iz-0005fm-8z; Thu, 19 Nov 2026 21:31:21 +0000 Received: by outflank-mailman (input) for mailman id 1497146; Thu, 19 Nov 2026 21:30:20 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9i0-0001Ey-8C for xen-devel@lists.xenproject.org; Thu, 19 Nov 2026 21:30:20 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xU9hz-00000006tKm-3NB2; Thu, 19 Nov 2026 21:30:20 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xU9MQ-0000000CVBH-32fx; Thu, 19 Nov 2026 21:08:02 +0000 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" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=FlX8i68BurZZbddP5JFg/suxuQm3LKHJ87QfUKq0fho=; b=YOjLFlDHmYqKURnApbfl5Yf+e/ qODu0DDGGPUeVqhppAlcD+a4LR/NkEU3fN9zq8AqHbcRHtXtU91+rdH6Gx9Hs1r3EX3LnIDwkFK5a hDX7epSDl0+Z1iPbuj7LDxD88RW7c24ypqjjByt+GLM5SMwPWq58Rlhhh4Y2q4fvrnV8=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Alejandro Vallejo , Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie Subject: [PATCH v7 15/50] x86/emulator: Refactor FXSAVE_AREA to use wrappers Date: Thu, 19 Nov 2026 21:07:06 +0000 Message-ID: <20261119-asi-hvm-v7-15-6551b6d5c879@xenproject.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> References: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1795123917969158500 Content-Type: text/plain; charset="utf-8" From: Alejandro Vallejo The FXSAVE/FXRSTOR emulation scratch buffer is the calling vCPU's own XSAVE area, reached today through a raw pointer dereference. Route it through the VCPU_MAP_XSAVE_AREA()/VCPU_UNMAP_XSAVE_AREA() seam instead, so the buffer keeps working when guest XSAVE areas move behind per-vCPU mappings. This path always operates on `current`, for which the map has a fastpath; the unmap is a no-op outside Xen builds. The macro pair is named MAP_FXSAVE_AREA()/UNMAP_FXSAVE_AREA() rather than keeping the bare FXSAVE_AREA name, so that the mapping operation is visible at the use sites. blk_fxsave() saves directly into the caller's buffer when it is large enough, so it needs a second variable: the save destination and the mapped area are not the same pointer, and only the latter may be handed to the unmap. Signed-off-by: Alejandro Vallejo Assisted-by: Claude Code:claude-fable-5, Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Revived from "x86: Address Space Isolation FPU preparations" v3 patch 08/12 (<20250110132823.24348-9-alejandro.vallejo@cloud.com>, Acked-by Jan Beulich modulo the rename below; no v4 was ever posted). Changes in v5: - New in this version. Changes since the previously posted version: - Renamed FXSAVE_AREA to MAP_FXSAVE_AREA(), as requested by Jan Beulich on the original thread ("the existing macro better would be adjusted to become MAP_FXSAVE_AREA()"), making the map operation visible at the use sites. - Introduced the separate save-destination variable in blk_fxsave(). The original unmapped `fxsr` after that variable had been repointed at the caller's buffer, so the unmap received a pointer that was never mapped. Harmless while the unmap was a stub, but a latent bug, and exactly the class of mistake Jan predicted in the message quoted above when he objected to the mapping being invisible at the use sites. - Reworded the commit message. - Unmap before raising #GP for a reserved MXCSR bit in blk_fxrstor: generate_exception_if() leaves through the done: label, past the unmap. --- xen/arch/x86/x86_emulate/blk.c | 36 +++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/xen/arch/x86/x86_emulate/blk.c b/xen/arch/x86/x86_emulate/blk.c index 9fddf649ee..b3055c8084 100644 --- a/xen/arch/x86/x86_emulate/blk.c +++ b/xen/arch/x86/x86_emulate/blk.c @@ -11,9 +11,12 @@ !defined(X86EMUL_NO_SIMD) # ifdef __XEN__ # include -# define FXSAVE_AREA ((void *)¤t->arch.xsave_area->fpu_sse) +/* Has a fastpath for `current`, so there's no actual map */ +# define MAP_FXSAVE_AREA() ((void *)VCPU_MAP_XSAVE_AREA(current)) +# define UNMAP_FXSAVE_AREA(x) VCPU_UNMAP_XSAVE_AREA(current, x) # else -# define FXSAVE_AREA get_fpu_save_area() +# define MAP_FXSAVE_AREA() get_fpu_save_area() +# define UNMAP_FXSAVE_AREA(x) ((void)(x)) # endif #endif =20 @@ -254,7 +257,7 @@ int x86_emul_blk( =20 case blk_fxrstor: { - struct x86_fxsr *fxsr =3D FXSAVE_AREA; + struct x86_fxsr *fxsr =3D MAP_FXSAVE_AREA(); =20 ASSERT(!data); ASSERT(bytes =3D=3D sizeof(*fxsr)); @@ -277,18 +280,26 @@ int x86_emul_blk( (unsigned int)offsetof(struct x86_fxsr, rsvd= ))); memset(fxsr->rsvd, 0, sizeof(*fxsr) - offsetof(struct x86_fxsr, rs= vd)); =20 - generate_exception_if(fxsr->mxcsr & ~mxcsr_mask, X86_EXC_GP, 0); + if ( fxsr->mxcsr & ~mxcsr_mask ) + { + UNMAP_FXSAVE_AREA(fxsr); + generate_exception(X86_EXC_GP, 0); + } =20 if ( s->rex_prefix & REX_W ) asm volatile ( "fxrstorq %0" :: "m" (*fxsr) ); else asm volatile ( "fxrstor %0" :: "m" (*fxsr) ); + + UNMAP_FXSAVE_AREA(fxsr); + break; } =20 case blk_fxsave: { - struct x86_fxsr *fxsr =3D FXSAVE_AREA; + struct x86_fxsr *fxsr =3D MAP_FXSAVE_AREA(); + struct x86_fxsr *dst =3D fxsr; =20 ASSERT(!data); ASSERT(bytes =3D=3D sizeof(*fxsr)); @@ -296,17 +307,20 @@ int x86_emul_blk( =20 if ( s->op_bytes < sizeof(*fxsr) ) /* Don't chance consuming uninitialized data. */ - memset(fxsr, 0, s->op_bytes); + memset(dst, 0, s->op_bytes); else - fxsr =3D ptr; + dst =3D ptr; =20 if ( s->rex_prefix & REX_W ) - asm volatile ( "fxsaveq %0" : "=3Dm" (*fxsr) ); + asm volatile ( "fxsaveq %0" : "=3Dm" (*dst) ); else - asm volatile ( "fxsave %0" : "=3Dm" (*fxsr) ); + asm volatile ( "fxsave %0" : "=3Dm" (*dst) ); + + if ( dst !=3D ptr ) /* i.e. s->op_bytes < sizeof(*fxsr) */ + memcpy(ptr, dst, s->op_bytes); + + UNMAP_FXSAVE_AREA(fxsr); =20 - if ( fxsr !=3D ptr ) /* i.e. s->op_bytes < sizeof(*fxsr) */ - memcpy(ptr, fxsr, s->op_bytes); break; } =20 --=20 2.56.0 From nobody Mon Nov 23 22:32:25 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=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1795123860; cv=none; d=zohomail.com; s=zohoarc; b=iaIavhuCUtOEP3aq2kUpY98sgh5LZkyZlefHfvAzsF+81eSqliVQmBw1dY94+AyJUBEpJplBqiUHmw7Stss4qLMPzW+xxnLfFvZxUAC298qdWN7kcR+T8IWxPvQaXwf9duHKZ+La71oam5MdoN6qRao+6F9XIEDX+m3OkA1rxfU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795123860; h=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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=78UJyNn4VY/h+N1P7RYObuj4ivXfajt4ZYGSI254v8U=; b=jmy+C8BtP/gGh8uYuXcjD0bL/9bnt3D6CH/3I7gJ30AFSWJq4mZINkfOE+DDtq15j5YoU9rKnhKKIKII7n27kVZES8/Qq8glGL1Ea1Gy+Cb4v4nLk+2NPc9HmzO9gmFgNEnwOZHyINyw0U00Fu9raYrq6nLgZ3+CpYyN0XSsJ+c= 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=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1795123860002347.83280571422665; Thu, 19 Nov 2026 13:31:00 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1497128.1695524 (Exim 4.92) (envelope-from ) id 1xU9iI-00053a-U6; Thu, 19 Nov 2026 21:30:38 +0000 Received: by outflank-mailman (output) from mailman id 1497128.1695524; Thu, 19 Nov 2026 21:30:38 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9iH-0004xO-D5; Thu, 19 Nov 2026 21:30:37 +0000 Received: by outflank-mailman (input) for mailman id 1497128; Thu, 19 Nov 2026 21:30:19 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9hz-0001DS-Jm for xen-devel@lists.xenproject.org; Thu, 19 Nov 2026 21:30:19 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xU9hz-00000006tIe-0mcx; Thu, 19 Nov 2026 21:30:19 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xU9MR-0000000CVBH-35OS; Thu, 19 Nov 2026 21:08:03 +0000 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" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=78UJyNn4VY/h+N1P7RYObuj4ivXfajt4ZYGSI254v8U=; b=YZYZUX9ZbwN5tJ0WrhL3XlV9P6 2hzN71deuNTW1ACFD30/dyceQA8ydKYEeMCBJKBkrX58kNoCvPL+hf8mP/vaus3Ln92npKrCv1ugc VsiLw4npCmUSmV7AzukM6rVxMEawurszb1ryWdSwr7CbIS/PiPXnKQ+nLvJVZVvR0SOw=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Alejandro Vallejo , Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie Subject: [PATCH v7 16/50] x86/mpx: Map/unmap xsave area in read_bndcfgu() Date: Thu, 19 Nov 2026 21:07:07 +0000 Message-ID: <20261119-asi-hvm-v7-16-6551b6d5c879@xenproject.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> References: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1795123861420158500 Content-Type: text/plain; charset="utf-8" From: Alejandro Vallejo The BNDCSR read executes XSAVE into the local idle vCPU's save area as scratch space and pulls BNDCFGU out of the result. Fetch that buffer through the VCPU_MAP_XSAVE_AREA() seam rather than a raw dereference, so the function is indifferent to where vCPU save areas live. This was the last raw access to another vCPU's save-area pointer outside the seam. No functional change. Signed-off-by: Alejandro Vallejo Assisted-by: Claude Code:claude-fable-5 Signed-off-by: George Dunlap --- Revived from "x86: Address Space Isolation FPU preparations" v3 patch 09/12 (<20250110132823.24348-10-alejandro.vallejo@cloud.com>; no v4 was ever posted). Changes in v5: - New in this version. Changes since the previously posted version: - Constified the vcpu pointer, as requested by Jan Beulich on the original thread (the xstate pointer itself stays mutable: the asm()s write through it, as Jan noted in withdrawing that half of the request). - Expanded the commit message. --- xen/arch/x86/xstate.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/xen/arch/x86/xstate.c b/xen/arch/x86/xstate.c index 8336414c52..2eae3f098d 100644 --- a/xen/arch/x86/xstate.c +++ b/xen/arch/x86/xstate.c @@ -1023,9 +1023,10 @@ int handle_xsetbv(u32 index, u64 new_bv) =20 uint64_t read_bndcfgu(void) { + uint64_t bndcfgu =3D 0; unsigned long cr0 =3D read_cr0(); - struct xsave_struct *xstate - =3D idle_vcpu[smp_processor_id()]->arch.xsave_area; + const struct vcpu *v =3D idle_vcpu[smp_processor_id()]; + struct xsave_struct *xstate =3D VCPU_MAP_XSAVE_AREA(v); const struct xstate_bndcsr *bndcsr; =20 ASSERT(cpu_has_mpx); @@ -1051,7 +1052,12 @@ uint64_t read_bndcfgu(void) if ( cr0 & X86_CR0_TS ) write_cr0(cr0); =20 - return xstate->xsave_hdr.xstate_bv & X86_XCR0_BNDCSR ? bndcsr->bndcfgu= : 0; + if ( xstate->xsave_hdr.xstate_bv & X86_XCR0_BNDCSR ) + bndcfgu =3D bndcsr->bndcfgu; + + VCPU_UNMAP_XSAVE_AREA(v, xstate); + + return bndcfgu; } =20 void xstate_set_init(uint64_t mask) --=20 2.56.0 From nobody Mon Nov 23 22:32:25 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=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1795123859; cv=none; d=zohomail.com; s=zohoarc; b=FmVni/asNOmP5LZ2iWv8QRzMEx8zKFT8cF7a+95Tt3fY/m2/ZuIxcC4uTAfsHEPyNfCyDlGP6NgkfLbUYEnA/6JMnjpr505y96ujpsD1vUxyFcBRjJMK9/QHw+nmCBIiQ3Ym1gDU/zNTfWKRn+U0Men8Y1LM985Ig4EPx/SJvYk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795123859; h=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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=+ze/mUts7B6alTNouyX2ejvqaet9bE/wAgVhHkz5gYM=; b=j27WzSKPDq0sfc28f94AjmrcKEQFLyDn7ALLwNaaQElikbJNZaIsQBkMG6JzXeWVFdfzDXmOwjFa5kBTsahRJqEc9Oy/k+UeQyFWs4rQvxx7OFcQJeOGle2P/zoSGRBLNctdfRvlZsXvsbKXiq2RpaiQyWZKNPTnTG9t0bA0RUA= 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=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1795123859791496.37289021470895; Thu, 19 Nov 2026 13:30:59 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1497130.1695539 (Exim 4.92) (envelope-from ) id 1xU9iM-0005pY-Pi; Thu, 19 Nov 2026 21:30:42 +0000 Received: by outflank-mailman (output) from mailman id 1497130.1695539; Thu, 19 Nov 2026 21:30: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 1xU9iL-0005jC-IZ; Thu, 19 Nov 2026 21:30:41 +0000 Received: by outflank-mailman (input) for mailman id 1497130; Thu, 19 Nov 2026 21:30:19 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9hz-0001DP-JA for xen-devel@lists.xenproject.org; Thu, 19 Nov 2026 21:30:19 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xU9hy-00000006tHv-435x; Thu, 19 Nov 2026 21:30:19 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xU9MS-0000000CVBH-38W1; Thu, 19 Nov 2026 21:08:04 +0000 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" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=+ze/mUts7B6alTNouyX2ejvqaet9bE/wAgVhHkz5gYM=; b=ilmG/nwFH4OSj3N8LzNSXGXlVh w39aXs5ImZz9gXsu3ax2DRKxXASCTcVw19UDfsZrH7yo8W4ZAKX3trNr7LyPI0J6HgO+V3+DaEo1G kSnpWiAmPlcSnGy0LicN2m79heiFoGXMZ1sF7APIfCKFtBBuFcXskk73e9NqqM3AV6co=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Alejandro Vallejo , Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie Subject: [PATCH v7 17/50] x86/fpu: Pass explicit xsave areas to fpu_(f)xsave() Date: Thu, 19 Nov 2026 21:07:08 +0000 Message-ID: <20261119-asi-hvm-v7-17-6551b6d5c879@xenproject.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> References: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1795123861450158500 Content-Type: text/plain; charset="utf-8" From: Alejandro Vallejo Have the save entry point fetch the vCPU's XSAVE area once, through the VCPU_MAP_XSAVE_AREA() seam, and pass the resulting pointer down explicitly: xsave() gains an area parameter in place of its internal dereference, and fpu_fxsave() takes the legacy region directly. The lower-level helpers no longer know where a vCPU's save area lives, which is the property the coming relocation of guest save areas relies on; the vcpu pointers become const on the way. No functional change. Signed-off-by: Alejandro Vallejo Assisted-by: Claude Code:claude-fable-5 Signed-off-by: George Dunlap --- Revived from "x86: Address Space Isolation FPU preparations" v3 patch 10/12 (<20250110132823.24348-11-alejandro.vallejo@cloud.com>, Acked-by Jan Beulich; no v4 was ever posted). Changes in v5: - New in this version. Changes since the previously posted version: - Regenerated against the eager-FPU-only tree: the entry point is now vcpu_save_fpu() (vcpu_xsave_mask()/_vcpu_save_fpu() no longer exist), and the mask is always XSTATE_ALL. - Expanded the commit message. --- xen/arch/x86/i387.c | 18 ++++++++++++------ xen/arch/x86/include/asm/xstate.h | 2 +- xen/arch/x86/xstate.c | 3 +-- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/xen/arch/x86/i387.c b/xen/arch/x86/i387.c index c6796bc97b..65491b5697 100644 --- a/xen/arch/x86/i387.c +++ b/xen/arch/x86/i387.c @@ -109,7 +109,8 @@ static inline void fpu_fxrstor(struct vcpu *v) /*******************************/ =20 /* Save x87 extended state */ -static inline void fpu_xsave(struct vcpu *v) +static inline void fpu_xsave(const struct vcpu *v, + struct xsave_struct *xsave_area) { bool ok; =20 @@ -119,15 +120,14 @@ static inline void fpu_xsave(struct vcpu *v) */ ok =3D set_xcr0(v->arch.xcr0_accum | XSTATE_FP_SSE); ASSERT(ok); - xsave(v, XSTATE_ALL); + xsave(v, xsave_area, XSTATE_ALL); ok =3D set_xcr0(v->arch.xcr0 ?: XSTATE_FP_SSE); ASSERT(ok); } =20 /* Save x87 FPU, MMX, SSE and SSE2 state */ -static inline void fpu_fxsave(struct vcpu *v) +static inline void fpu_fxsave(const struct vcpu *v, fpusse_t *fpu_ctxt) { - fpusse_t *fpu_ctxt =3D &v->arch.xsave_area->fpu_sse; unsigned int fip_width =3D v->domain->arch.x87_fip_width; =20 if ( fip_width !=3D 4 ) @@ -192,15 +192,21 @@ void vcpu_restore_fpu(struct vcpu *v) */ void vcpu_save_fpu(struct vcpu *v) { + struct xsave_struct *xsave_area; + ASSERT(!is_idle_vcpu(v)); =20 /* This can happen, if a paravirtualised guest OS has set its CR0.TS. = */ clts(); =20 + xsave_area =3D VCPU_MAP_XSAVE_AREA(v); + if ( cpu_has_xsave ) - fpu_xsave(v); + fpu_xsave(v, xsave_area); else - fpu_fxsave(v); + fpu_fxsave(v, &xsave_area->fpu_sse); + + VCPU_UNMAP_XSAVE_AREA(v, xsave_area); } =20 /* Initialize FPU's context save area */ diff --git a/xen/arch/x86/include/asm/xstate.h b/xen/arch/x86/include/asm/x= state.h index 0519379edb..40bb6f152e 100644 --- a/xen/arch/x86/include/asm/xstate.h +++ b/xen/arch/x86/include/asm/xstate.h @@ -95,7 +95,7 @@ uint64_t get_xcr0(void); void set_msr_xss(u64 xss); uint64_t get_msr_xss(void); uint64_t read_bndcfgu(void); -void xsave(struct vcpu *v, uint64_t mask); +void xsave(const struct vcpu *v, struct xsave_struct *ptr, uint64_t mask); void xrstor(struct vcpu *v, uint64_t mask); void xstate_set_init(uint64_t mask); bool xsave_enabled(const struct vcpu *v); diff --git a/xen/arch/x86/xstate.c b/xen/arch/x86/xstate.c index 2eae3f098d..058bc4c0bc 100644 --- a/xen/arch/x86/xstate.c +++ b/xen/arch/x86/xstate.c @@ -304,9 +304,8 @@ void compress_xsave_states(struct vcpu *v, const void *= src, unsigned int size) VCPU_UNMAP_XSAVE_AREA(v, xstate); } =20 -void xsave(struct vcpu *v, uint64_t mask) +void xsave(const struct vcpu *v, struct xsave_struct *ptr, uint64_t mask) { - struct xsave_struct *ptr =3D v->arch.xsave_area; uint32_t hmask =3D mask >> 32; uint32_t lmask =3D mask; unsigned int fip_width =3D v->domain->arch.x87_fip_width; --=20 2.56.0 From nobody Mon Nov 23 22:32:25 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=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1795123851; cv=none; d=zohomail.com; s=zohoarc; b=IS+WQ22QpVlopzi4AyxvlmSeTa8Oj5J8cmZnK7Oc5FZbKFHHqWZZczAcLOpFFL57WOMNRn8MXvdufAbTpdD6uTr823ZM6FsTTm8ozm4gea6apK6anx1K43Uq8qhhNw0Zrs3M/j/4eAsPZ6VTnFYrdibKdIirvj3ty8Y7CtVEhy4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795123851; h=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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=O3n3UQQODatRF1IOFe5HjC1fiBbAzhiwh2RBel9BdYM=; b=aw3iJ1ADtZwJjjs++eJQuPxdLknqF2Yls1QOPWxBjh14NS0R0itOnpvRVH0hOfK6euTQxE4o+FT3KI4xTCyf0TB1qADkzosPtpVvohZmEoJo0aAHuOgr1O2c/cmjGETOuMfUMcXK/zyc95jYM3SDCLNHn47pHQQcyNH5zQwNpjU= 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=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1795123851958255.16723480725727; Thu, 19 Nov 2026 13:30:51 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1497127.1695508 (Exim 4.92) (envelope-from ) id 1xU9iF-0004Kv-2m; Thu, 19 Nov 2026 21:30:35 +0000 Received: by outflank-mailman (output) from mailman id 1497127.1695508; Thu, 19 Nov 2026 21:30:34 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9iD-0004Hl-U2; Thu, 19 Nov 2026 21:30:33 +0000 Received: by outflank-mailman (input) for mailman id 1497127; Thu, 19 Nov 2026 21:30:19 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9hz-0001DE-GD for xen-devel@lists.xenproject.org; Thu, 19 Nov 2026 21:30:19 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xU9hz-00000006tIJ-0JCC; Thu, 19 Nov 2026 21:30:19 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xU9MT-0000000CVBH-3C3O; Thu, 19 Nov 2026 21:08:05 +0000 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" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=O3n3UQQODatRF1IOFe5HjC1fiBbAzhiwh2RBel9BdYM=; b=lzi6zaI55aAj7uMgNyl4Gz+lrj xnVEK4CaxzVJM14PLqdcbEEeE2pUdriz8GC+na9gCEWru+GENpq6M6UVfxnMRNy6WJ8w7WKWI8EJZ PcBfuEhRFZf0LwmD4qAmq0i/ZHb+7hyOOSpB4eugYzzbEFOAnGuNjdUwlpftnDdI01gY=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Alejandro Vallejo , Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie Subject: [PATCH v7 18/50] x86/fpu: Pass explicit xsave areas to fpu_(f)xrstor() Date: Thu, 19 Nov 2026 21:07:09 +0000 Message-ID: <20261119-asi-hvm-v7-18-6551b6d5c879@xenproject.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> References: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1795123853384158500 Content-Type: text/plain; charset="utf-8" From: Alejandro Vallejo Mirror of the save-side change: the restore entry point fetches the vCPU's XSAVE area once, through the VCPU_MAP_XSAVE_AREA() seam, and passes the pointer down explicitly. xrstor() gains an area parameter in place of its internal dereference (its fault-recovery writes go through the same mapped pointer), fpu_fxrstor() reduces to taking just the legacy region, and the two direct xrstor() callers -- XSETBV emulation and xstate_set_init() -- pass the pointers they already hold mapped. In handle_xsetbv() this moves the xrstor inside the mapped section, where it belongs. No functional change. Signed-off-by: Alejandro Vallejo Assisted-by: Claude Code:claude-fable-5 Signed-off-by: George Dunlap --- Revived from "x86: Address Space Isolation FPU preparations" v3 patch 11/12 (<20250110132823.24348-12-alejandro.vallejo@cloud.com>, Acked-by Jan Beulich; no v4 was ever posted). Changes in v5: - New in this version. Changes since the previously posted version: - Regenerated against the eager-FPU-only tree: the entry point is now vcpu_restore_fpu() (the lazy/nonlazy split no longer exists), and the mask is always XSTATE_ALL. - Moved handle_xsetbv()'s xrstor() call inside its map/unmap bracket, which the explicit-area form makes possible. - Expanded the commit message. --- xen/arch/x86/i387.c | 19 ++++++++++++------- xen/arch/x86/include/asm/xstate.h | 2 +- xen/arch/x86/xstate.c | 7 +++---- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/xen/arch/x86/i387.c b/xen/arch/x86/i387.c index 65491b5697..b796687eeb 100644 --- a/xen/arch/x86/i387.c +++ b/xen/arch/x86/i387.c @@ -20,7 +20,8 @@ /* FPU Restore Functions */ /*******************************/ /* Restore x87 extended state */ -static inline void fpu_xrstor(struct vcpu *v, uint64_t mask) +static inline void fpu_xrstor(const struct vcpu *v, + struct xsave_struct *xsave_area, uint64_t ma= sk) { bool ok; =20 @@ -30,16 +31,14 @@ static inline void fpu_xrstor(struct vcpu *v, uint64_t = mask) */ ok =3D set_xcr0(v->arch.xcr0_accum | XSTATE_FP_SSE); ASSERT(ok); - xrstor(v, mask); + xrstor(v, xsave_area, mask); ok =3D set_xcr0(v->arch.xcr0 ?: XSTATE_FP_SSE); ASSERT(ok); } =20 /* Restore x87 FPU, MMX, SSE and SSE2 state */ -static inline void fpu_fxrstor(struct vcpu *v) +static inline void fpu_fxrstor(const fpusse_t *fpu_ctxt) { - const fpusse_t *fpu_ctxt =3D &v->arch.xsave_area->fpu_sse; - /* * Some CPUs don't save/restore FDP/FIP/FOP unless an exception * is pending. Clear the x87 state here by setting it to fixed @@ -174,12 +173,18 @@ static inline void fpu_fxsave(const struct vcpu *v, f= pusse_t *fpu_ctxt) /* Restore FPU state whenever VCPU is schduled in. */ void vcpu_restore_fpu(struct vcpu *v) { + struct xsave_struct *xsave_area; + ASSERT(!is_idle_vcpu(v)); =20 + xsave_area =3D VCPU_MAP_XSAVE_AREA(v); + if ( cpu_has_xsave ) - fpu_xrstor(v, XSTATE_ALL); + fpu_xrstor(v, xsave_area, XSTATE_ALL); else - fpu_fxrstor(v); + fpu_fxrstor(&xsave_area->fpu_sse); + + VCPU_UNMAP_XSAVE_AREA(v, xsave_area); =20 /* Xen doesn't need TS set, but the guest might. */ if ( is_pv_vcpu(v) && (v->arch.pv.ctrlreg[0] & X86_CR0_TS) ) diff --git a/xen/arch/x86/include/asm/xstate.h b/xen/arch/x86/include/asm/x= state.h index 40bb6f152e..3d57aaadd8 100644 --- a/xen/arch/x86/include/asm/xstate.h +++ b/xen/arch/x86/include/asm/xstate.h @@ -96,7 +96,7 @@ void set_msr_xss(u64 xss); uint64_t get_msr_xss(void); uint64_t read_bndcfgu(void); void xsave(const struct vcpu *v, struct xsave_struct *ptr, uint64_t mask); -void xrstor(struct vcpu *v, uint64_t mask); +void xrstor(const struct vcpu *v, struct xsave_struct *ptr, uint64_t mask); void xstate_set_init(uint64_t mask); bool xsave_enabled(const struct vcpu *v); int __must_check validate_xstate(const struct domain *d, diff --git a/xen/arch/x86/xstate.c b/xen/arch/x86/xstate.c index 058bc4c0bc..3c9be271a3 100644 --- a/xen/arch/x86/xstate.c +++ b/xen/arch/x86/xstate.c @@ -378,11 +378,10 @@ void xsave(const struct vcpu *v, struct xsave_struct = *ptr, uint64_t mask) ptr->fpu_sse.x[FPU_WORD_SIZE_OFFSET] =3D fip_width; } =20 -void xrstor(struct vcpu *v, uint64_t mask) +void xrstor(const struct vcpu *v, struct xsave_struct *ptr, uint64_t mask) { uint32_t hmask =3D mask >> 32; uint32_t lmask =3D mask; - struct xsave_struct *ptr =3D v->arch.xsave_area; unsigned int faults, prev_faults; =20 /* @@ -1011,8 +1010,8 @@ int handle_xsetbv(u32 index, u64 new_bv) clts(); =20 asm ( "stmxcsr %0" : "=3Dm" (xsave_area->fpu_sse.mxcsr) ); + xrstor(curr, xsave_area, mask); VCPU_UNMAP_XSAVE_AREA(curr, xsave_area); - xrstor(curr, mask); if ( cr0 & X86_CR0_TS ) write_cr0(cr0); } @@ -1079,7 +1078,7 @@ void xstate_set_init(uint64_t mask) =20 xstate =3D VCPU_MAP_XSAVE_AREA(v); memset(&xstate->xsave_hdr, 0, sizeof(xstate->xsave_hdr)); - xrstor(v, mask); + xrstor(v, xstate, mask); VCPU_UNMAP_XSAVE_AREA(v, xstate); =20 if ( cr0 & X86_CR0_TS ) --=20 2.56.0 From nobody Mon Nov 23 22:32:25 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=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1795123876; cv=none; d=zohomail.com; s=zohoarc; b=PqhAIEbBJplxMY4OIlFrkhL242kx7CB+VvRw5JgSYGxzkYC4uWExJnnc2jANvaI7RSkeY3T0M7vQmr2t5t91UABcSGuTBDb4uVxJRcj3mi+QXbu2wDXA3DE/jYCCbDIESlKY8/EeVKvzL0SVn7Wm8kTD4KqqWc1VghdctDOf5IA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795123876; h=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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=t6vSblg3+mFM8xbVls8OEkkHG1fTYd0cuQ0cUu1dFJc=; b=GuExcyRt47byxA0vJHt0amuRPu4wdKkRa0qwXgYnjF7WvDrTwZG8VlEivGmShZCOBvKDinbeXh6Rm1RpZhYLeKRqI3S5ZnmW/TmdsZdgAcylrRTX02tHsSXmh6+cI7toEwKbGCR7bVxVyVA8XhYetoQ29oB96szRzHLRA9GGUyU= 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=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1795123876835428.72993266371816; Thu, 19 Nov 2026 13:31:16 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1497134.1695574 (Exim 4.92) (envelope-from ) id 1xU9iV-0007m6-PZ; Thu, 19 Nov 2026 21:30:51 +0000 Received: by outflank-mailman (output) from mailman id 1497134.1695574; Thu, 19 Nov 2026 21:30:51 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9iU-0007d8-77; Thu, 19 Nov 2026 21:30:50 +0000 Received: by outflank-mailman (input) for mailman id 1497134; Thu, 19 Nov 2026 21:30:19 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9hz-0001Du-Pc for xen-devel@lists.xenproject.org; Thu, 19 Nov 2026 21:30:19 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xU9hz-00000006tIB-09tA; Thu, 19 Nov 2026 21:30:19 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xU9MU-0000000CVBH-2vGZ; Thu, 19 Nov 2026 21:08:06 +0000 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" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=t6vSblg3+mFM8xbVls8OEkkHG1fTYd0cuQ0cUu1dFJc=; b=WcNXVKFcU2/uL5aOHDvLi/a9yL uXHRr/V2yH1A4FHtZdeyCSg6yZWunaO4xDcuTzyMc9yFKOzJF9zpinZJL22v/i112EBsNF7jSEUQy FB1hKLGI1riUu8woA9zXa6rzmH3nyYlKvHN5mUQ+mG3lhqHfOGeWWu7P6iGrrIz5vV7g=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie Subject: [PATCH v7 19/50] x86/xstate: classify xsave-area seam accesses by failure mode Date: Thu, 19 Nov 2026 21:07:10 +0000 Message-ID: <20261119-asi-hvm-v7-19-6551b6d5c879@xenproject.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> References: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1795123877709158500 Content-Type: text/plain; charset="utf-8" The VCPU_MAP_XSAVE_AREA() seam currently cannot fail: the map is a pointer fetch. Once guest XSAVE areas move behind per-vCPU mappings, mapping another vCPU's multi-page area becomes an operation that can run out of resources. Split the seam's users along that line ahead of the change: - expand_xsave_states() and compress_xsave_states() address the full area; they gain an error return, which their four callers (the vcpuextstate domctls and the HVM context save/load handlers) propagate. The two deserialising callers (the setvcpuextstate domctl and the HVM xsave-state load handler) restore the previously-set XCR0 fields if the deserialisation fails, so a failed restore leaves the vCPU's state as it was. - Accessors touching only the legacy region and the XSAVE header -- vcpu_reset_fpu(), vcpu_setup_fpu(), hvm_save_cpu_ctxt() and arch_get_info_guest() -- switch to a new VCPU_MAP_XSAVE_AREA_PREFIX() variant. The prefix always fits the area's first page, so that variant can remain infallible, and no error plumbing is needed on the paths (INIT/SIPI, S3 resume) that have no way to fail. No functional change: both variants are still pointer fetches. Assisted-by: Claude Code:claude-fable-5 Signed-off-by: George Dunlap --- Changes in v5: - New in this version. --- xen/arch/x86/domctl.c | 26 ++++++++++++++++++-------- xen/arch/x86/hvm/hvm.c | 27 ++++++++++++++++++--------- xen/arch/x86/i387.c | 8 ++++---- xen/arch/x86/include/asm/xstate.h | 13 +++++++++++-- xen/arch/x86/xstate.c | 18 +++++++++++++++--- 5 files changed, 66 insertions(+), 26 deletions(-) diff --git a/xen/arch/x86/domctl.c b/xen/arch/x86/domctl.c index 1eaa609b95..5efd5ed23f 100644 --- a/xen/arch/x86/domctl.c +++ b/xen/arch/x86/domctl.c @@ -1010,10 +1010,10 @@ long arch_do_domctl( goto vcpuextstate_out; } =20 - expand_xsave_states(v, xsave_area, xsave_size); + ret =3D expand_xsave_states(v, xsave_area, xsave_size); =20 - if ( copy_to_guest_offset(evc->buffer, offset, xsave_area, - xsave_size) ) + if ( !ret && copy_to_guest_offset(evc->buffer, offset, + xsave_area, xsave_size) ) ret =3D -EFAULT; xfree(xsave_area); } @@ -1077,14 +1077,24 @@ long arch_do_domctl( ret =3D -EINVAL; /* Not legitimate data. */ else { + uint64_t old_xcr0, old_xcr0_accum; + vcpu_pause(v); =20 + old_xcr0 =3D v->arch.xcr0; + old_xcr0_accum =3D v->arch.xcr0_accum; v->arch.xcr0 =3D _xcr0; v->arch.xcr0_accum =3D _xcr0_accum; - compress_xsave_states(v, _xsave_area, - evc->size - PV_XSAVE_HDR_SIZE); + ret =3D compress_xsave_states(v, _xsave_area, + evc->size - PV_XSAVE_HDR_SIZE); =20 - if ( is_hvm_domain(d) ) + if ( ret ) + { + /* Leave the vCPU's state as it was. */ + v->arch.xcr0 =3D old_xcr0; + v->arch.xcr0_accum =3D old_xcr0_accum; + } + else if ( is_hvm_domain(d) ) hvmemul_cancel(v); =20 vcpu_unpause(v); @@ -1468,10 +1478,10 @@ void arch_get_info_guest(struct vcpu *v, vcpu_guest= _context_u c) #define c(fld) (c.nat->fld) #endif =20 - xsave_area =3D VCPU_MAP_XSAVE_AREA(v); + xsave_area =3D VCPU_MAP_XSAVE_AREA_PREFIX(v); BUILD_BUG_ON(sizeof(c.nat->fpu_ctxt) !=3D sizeof(xsave_area->fpu_sse)); memcpy(&c.nat->fpu_ctxt, &xsave_area->fpu_sse, sizeof(c.nat->fpu_ctxt)= ); - VCPU_UNMAP_XSAVE_AREA(v, xsave_area); + VCPU_UNMAP_XSAVE_AREA_PREFIX(v, xsave_area); =20 if ( is_pv_domain(d) ) c(flags =3D v->arch.pv.vgc_flags & ~VGCF_in_kernel); diff --git a/xen/arch/x86/hvm/hvm.c b/xen/arch/x86/hvm/hvm.c index 2f58d4d3b1..4f95d27344 100644 --- a/xen/arch/x86/hvm/hvm.c +++ b/xen/arch/x86/hvm/hvm.c @@ -932,10 +932,10 @@ static int cf_check hvm_save_cpu_ctxt(struct vcpu *v,= hvm_domain_context_t *h) ctxt.ldtr_base =3D seg.base; ctxt.ldtr_arbytes =3D seg.attr; =20 - xsave_area =3D VCPU_MAP_XSAVE_AREA(v); + xsave_area =3D VCPU_MAP_XSAVE_AREA_PREFIX(v); BUILD_BUG_ON(sizeof(ctxt.fpu_regs) !=3D sizeof(xsave_area->fpu_sse)); memcpy(ctxt.fpu_regs, &xsave_area->fpu_sse, sizeof(ctxt.fpu_regs)); - VCPU_UNMAP_XSAVE_AREA(v, xsave_area); + VCPU_UNMAP_XSAVE_AREA_PREFIX(v, xsave_area); ctxt.flags =3D XEN_X86_FPU_INITIALISED; =20 return hvm_save_entry(CPU, v->vcpu_id, h, &ctxt); @@ -1251,10 +1251,8 @@ static int cf_check hvm_save_cpu_xsave_states( ctxt->xcr0 =3D v->arch.xcr0; ctxt->xcr0_accum =3D v->arch.xcr0_accum; =20 - expand_xsave_states(v, &ctxt->save_area, - size - offsetof(typeof(*ctxt), save_area)); - - return 0; + return expand_xsave_states(v, &ctxt->save_area, + size - offsetof(typeof(*ctxt), save_area)); } =20 /* @@ -1279,6 +1277,7 @@ static int cf_check hvm_load_cpu_xsave_states( struct hvm_hw_cpu_xsave *ctxt; const struct hvm_save_descriptor *desc; unsigned int i, desc_start, desc_length; + uint64_t old_xcr0, old_xcr0_accum; =20 /* Which vcpu is this? */ vcpuid =3D hvm_load_instance(h); @@ -1383,12 +1382,22 @@ static int cf_check hvm_load_cpu_xsave_states( } /* Checking finished */ =20 + old_xcr0 =3D v->arch.xcr0; + old_xcr0_accum =3D v->arch.xcr0_accum; v->arch.xcr0 =3D ctxt->xcr0; v->arch.xcr0_accum =3D ctxt->xcr0_accum; - compress_xsave_states(v, &ctxt->save_area, - size - offsetof(struct hvm_hw_cpu_xsave, save_ar= ea)); =20 - return 0; + err =3D compress_xsave_states(v, &ctxt->save_area, + size - offsetof(struct hvm_hw_cpu_xsave, + save_area)); + if ( err ) + { + /* Leave the vCPU's state as it was. */ + v->arch.xcr0 =3D old_xcr0; + v->arch.xcr0_accum =3D old_xcr0_accum; + } + + return err; } =20 #define HVM_CPU_MSR_SIZE(cnt) offsetof(struct hvm_msr, msr[cnt]) diff --git a/xen/arch/x86/i387.c b/xen/arch/x86/i387.c index b796687eeb..d49e391e99 100644 --- a/xen/arch/x86/i387.c +++ b/xen/arch/x86/i387.c @@ -222,7 +222,7 @@ int vcpu_init_fpu(struct vcpu *v) =20 void vcpu_reset_fpu(struct vcpu *v) { - struct xsave_struct *xsave_area =3D VCPU_MAP_XSAVE_AREA(v); + struct xsave_struct *xsave_area =3D VCPU_MAP_XSAVE_AREA_PREFIX(v); =20 *xsave_area =3D (struct xsave_struct) { .xsave_hdr.xstate_bv =3D X86_XCR0_X87, @@ -233,19 +233,19 @@ void vcpu_reset_fpu(struct vcpu *v) xsave_area->fpu_sse.fcw =3D FCW_RESET; xsave_area->fpu_sse.ftw =3D FXSAVE_FTW_RESET; =20 - VCPU_UNMAP_XSAVE_AREA(v, xsave_area); + VCPU_UNMAP_XSAVE_AREA_PREFIX(v, xsave_area); } =20 void vcpu_setup_fpu(struct vcpu *v, const void *data) { - struct xsave_struct *xsave_area =3D VCPU_MAP_XSAVE_AREA(v); + struct xsave_struct *xsave_area =3D VCPU_MAP_XSAVE_AREA_PREFIX(v); =20 *xsave_area =3D (struct xsave_struct) { .fpu_sse =3D *(const fpusse_t*)data, .xsave_hdr.xstate_bv =3D XSTATE_FP_SSE, }; =20 - VCPU_UNMAP_XSAVE_AREA(v, xsave_area); + VCPU_UNMAP_XSAVE_AREA_PREFIX(v, xsave_area); } =20 /* Free FPU's context save area */ diff --git a/xen/arch/x86/include/asm/xstate.h b/xen/arch/x86/include/asm/x= state.h index 3d57aaadd8..c23308cbe2 100644 --- a/xen/arch/x86/include/asm/xstate.h +++ b/xen/arch/x86/include/asm/xstate.h @@ -103,8 +103,8 @@ int __must_check validate_xstate(const struct domain *d, uint64_t xcr0, uint64_t xcr0_accum, const struct xsave_hdr *hdr); int __must_check handle_xsetbv(u32 index, u64 new_bv); -void expand_xsave_states(const struct vcpu *v, void *dest, unsigned int si= ze); -void compress_xsave_states(struct vcpu *v, const void *src, unsigned int s= ize); +int expand_xsave_states(const struct vcpu *v, void *dest, unsigned int siz= e); +int compress_xsave_states(struct vcpu *v, const void *src, unsigned int si= ze); =20 /* extended state init and cleanup functions */ void xstate_free_save_area(struct vcpu *v); @@ -171,4 +171,13 @@ xsave_area_compressed(const struct xsave_struct *xsave= _area) */ #define VCPU_UNMAP_XSAVE_AREA(v, x) do { (void)(v); (x) =3D NULL; } while = (0) =20 +/* + * Variant of VCPU_MAP_XSAVE_AREA() for accessors which touch nothing + * beyond the legacy region and the XSAVE header (the first + * XSTATE_AREA_MIN_SIZE bytes). That prefix always fits within the + * area's first page, so this variant cannot fail. + */ +#define VCPU_MAP_XSAVE_AREA_PREFIX(v) VCPU_MAP_XSAVE_AREA(v) +#define VCPU_UNMAP_XSAVE_AREA_PREFIX(v, x) VCPU_UNMAP_XSAVE_AREA(v, x) + #endif /* __ASM_XSTATE_H */ diff --git a/xen/arch/x86/xstate.c b/xen/arch/x86/xstate.c index 3c9be271a3..2dd1699aba 100644 --- a/xen/arch/x86/xstate.c +++ b/xen/arch/x86/xstate.c @@ -177,14 +177,19 @@ static void setup_xstate_comp(uint16_t *comp_offsets, * It is the callers responsibility to ensure that there is xsave state to * serialise, and that the provided buffer is exactly the right size. */ -void expand_xsave_states(const struct vcpu *v, void *dest, unsigned int si= ze) +int expand_xsave_states(const struct vcpu *v, void *dest, unsigned int siz= e) { const struct xsave_struct *xstate =3D VCPU_MAP_XSAVE_AREA(v); const void *src; uint16_t comp_offsets[sizeof(xfeature_mask)*8]; - u64 xstate_bv =3D xstate->xsave_hdr.xstate_bv; + u64 xstate_bv; u64 valid; =20 + if ( !xstate ) + return -ENOMEM; + + xstate_bv =3D xstate->xsave_hdr.xstate_bv; + /* Check there is state to serialise (i.e. at least an XSAVE_HDR) */ BUG_ON(!v->arch.xcr0_accum); /* Check there is the correct room to decompress into. */ @@ -233,6 +238,8 @@ void expand_xsave_states(const struct vcpu *v, void *de= st, unsigned int size) =20 out: VCPU_UNMAP_XSAVE_AREA(v, xstate); + + return 0; } =20 /* @@ -245,13 +252,16 @@ void expand_xsave_states(const struct vcpu *v, void *= dest, unsigned int size) * It is the callers responsibility to ensure that the source buffer conta= ins * xsave state, is uncompressed, and is exactly the right size. */ -void compress_xsave_states(struct vcpu *v, const void *src, unsigned int s= ize) +int compress_xsave_states(struct vcpu *v, const void *src, unsigned int si= ze) { struct xsave_struct *xstate =3D VCPU_MAP_XSAVE_AREA(v); void *dest; uint16_t comp_offsets[sizeof(xfeature_mask)*8]; u64 xstate_bv, valid; =20 + if ( !xstate ) + return -ENOMEM; + BUG_ON(!v->arch.xcr0_accum); BUG_ON(size !=3D xstate_uncompressed_size(v->arch.xcr0_accum)); ASSERT(!xsave_area_compressed(src)); @@ -302,6 +312,8 @@ void compress_xsave_states(struct vcpu *v, const void *= src, unsigned int size) =20 out: VCPU_UNMAP_XSAVE_AREA(v, xstate); + + return 0; } =20 void xsave(const struct vcpu *v, struct xsave_struct *ptr, uint64_t mask) --=20 2.56.0 From nobody Mon Nov 23 22:32:25 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=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1795123854; cv=none; d=zohomail.com; s=zohoarc; b=fJYi9ZQ8F8VWKf2C2DNmjDOMa2ml/LQm3E0uL1g+G9Rgg4lCiMdNh8j8Gd4yjYFBTaHLfLb9MBiX5Ip/8yz4H9SlpbV2qkh4oTlV8+YbDsQMTq5AjnAqJPN1SRvlHALokPozhQWDFUd/MIxCBE4Q+r0PRPcGMy6ioYVlGAtB8gw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795123854; h=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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=Y4mhj3jCPZOoipEGwmPIT9w36kLKqqmGJrKUF7TJVS4=; b=Z0379rcXGQJsivrRD2jOB20Ts+zdKSHh01SgM5VAulcJVMWlI5zUtRUNO39DWzgSfLqPH4JDWfNb3RA+hKIcQW74d8dSv8mYcXQnk80GWzVPgWs2WlfwHrxQjY9dpAXdmIxSxJ3RxB4tvW7fU/bk5AAR+RJ1bty3wRIAthooMbI= 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=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 179512385492658.655160150056986; Thu, 19 Nov 2026 13:30:54 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1497119.1695450 (Exim 4.92) (envelope-from ) id 1xU9i5-0002ZT-T4; Thu, 19 Nov 2026 21:30:25 +0000 Received: by outflank-mailman (output) from mailman id 1497119.1695450; Thu, 19 Nov 2026 21:30:25 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9i5-0002WF-8C; Thu, 19 Nov 2026 21:30:25 +0000 Received: by outflank-mailman (input) for mailman id 1497119; Thu, 19 Nov 2026 21:30:19 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9hz-0001Cb-8y for xen-devel@lists.xenproject.org; Thu, 19 Nov 2026 21:30:19 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xU9hy-00000006tHD-3YjF; Thu, 19 Nov 2026 21:30:19 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xU9MV-0000000CVBH-2aJV; Thu, 19 Nov 2026 21:08:07 +0000 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" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=Y4mhj3jCPZOoipEGwmPIT9w36kLKqqmGJrKUF7TJVS4=; b=LHSSTvVyEByN1kFPCbtAWb/kVy MOz8ww3dT38SYzqyzl51YPD3Czv+KLeBNkzK7nDyT5ATdaQ0Ay1wg4NyFC4IGkBjxFFO3+g7oNZbT nu2nTuVju21lXknCwRubbZqcBmad6mhJ+ApLWFphInz5rIQp2aVtI0vkoHwBe4pAQbAY=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie Subject: [PATCH v7 20/50] x86/domain: restore FPU state after switching to the new page tables Date: Thu, 19 Nov 2026 21:07:11 +0000 Message-ID: <20261119-asi-hvm-v7-20-6551b6d5c879@xenproject.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> References: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1795123855404158500 Content-Type: text/plain; charset="utf-8" __context_switch() restores the incoming vCPU's FPU state (vcpu_restore_fpu(n)) while still running on the outgoing vCPU's page tables, before write_ptbase(n). That dereferences n's XSAVE area through whatever address it happens to have in p's address space -- fine today, when the area is globally mapped, but not once guest XSAVE state moves to a per-vCPU mapping that need not exist in another vCPU's address space (upcoming Address Space Isolation work). Move the eager restore to just after write_ptbase(n), so it runs with n's page tables loaded. The XCR0/XSS setup stays where it is (it programs control registers / an MSR, independent of the page tables, and XSS must be set before the supervisor-state xrstor). Nothing between the save and the new restore point touches guest FPU state: the ctxt_switch->to() hooks, psr and GDT handling are all FPU-free, and Xen is built without SSE. No functional change with the current global XSAVE-area mapping. Assisted-by: Claude Code:claude-opus-4-8 Signed-off-by: George Dunlap --- Changes in v5: - New in this version. --- xen/arch/x86/domain.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/xen/arch/x86/domain.c b/xen/arch/x86/domain.c index e605936797..a71c11cf52 100644 --- a/xen/arch/x86/domain.c +++ b/xen/arch/x86/domain.c @@ -2255,7 +2255,6 @@ static void __context_switch(void) if ( cpu_has_xsaves && is_hvm_vcpu(n) ) set_msr_xss(n->arch.msrs->xss.raw); } - vcpu_restore_fpu(n); nd->arch.ctxt_switch->to(n); } =20 @@ -2271,7 +2270,10 @@ static void __context_switch(void) write_ptbase(n); =20 if ( !is_idle_domain(nd) ) + { memcpy(stack_regs, vcpu_user_regs(n), CTXT_SWITCH_STACK_BYTES); + vcpu_restore_fpu(n); + } =20 #ifdef CONFIG_PV /* Prefetch the VMCB if we expect to use it later in the context switc= h */ --=20 2.56.0 From nobody Mon Nov 23 22:32:25 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=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1795123903; cv=none; d=zohomail.com; s=zohoarc; b=Al/jTeUilCWil65OmQmNJb8T9Rz6FLm6GT+b2xL3nIUFuOXJC/vi0pyqNNrXJlbQDJG2Khm8wd4/I9zFwmuRVMfWDrPs53DdxR7rkegFxv8WJr8GDVbzOcSDD9eYlZUMfFNkNpLhwOu9zd7O3aQDzozvlsrL0qW6sZEjT9Eprbc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795123903; h=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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=G1L4csWbNlSFjhZzvEectVYc0vkgUuapa/lumJKCuok=; b=Xp6+0igLoaqRMVz5WPYc/blfFGAMn0fnBuWGRDJ7j2kXOA7PI/1vGa1VGhwxfnDtjEIk+JOZ+8M/P3vA0pGFI9Rz/L4SczbOY25xGUOjrZ4xLWSDt1eyuonUY4C8EBYV7+MVIBNE3b0ZA4dfrb8I6cSjv3uWqcR1D4sUPxq2APY= 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=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1795123903738846.7597196249266; Thu, 19 Nov 2026 13:31:43 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1497145.1695691 (Exim 4.92) (envelope-from ) id 1xU9iz-0005RZ-CI; Thu, 19 Nov 2026 21:31:21 +0000 Received: by outflank-mailman (output) from mailman id 1497145.1695691; Thu, 19 Nov 2026 21:31:20 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9ix-0005JN-L4; Thu, 19 Nov 2026 21:31:19 +0000 Received: by outflank-mailman (input) for mailman id 1497145; Thu, 19 Nov 2026 21:30:20 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9i0-0001Eu-7Q for xen-devel@lists.xenproject.org; Thu, 19 Nov 2026 21:30:20 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xU9hz-00000006tKs-3Uow; Thu, 19 Nov 2026 21:30:20 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xU9MW-0000000CVBH-2H1B; Thu, 19 Nov 2026 21:08:08 +0000 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" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=G1L4csWbNlSFjhZzvEectVYc0vkgUuapa/lumJKCuok=; b=s8mGFzYs7CkJmXOXrX0gHcVta/ Vn6nuD6TqBnB8ghA/BPS3bHBmWhmMNMxiqbfzbOWZzlTYBpo3bObw3l4QVbhSKRF+B9FapUyrfRkt mz4DEftmo1E5CGcW6bt9k8z0pTThzBD7hI5D7dpP6AOwl3HfpKZoIoPPhia+6f8NwBv4=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie Subject: [PATCH v7 21/50] x86/xstate: move guest XSAVE areas behind the vCPU state window Date: Thu, 19 Nov 2026 21:07:12 +0000 Message-ID: <20261119-asi-hvm-v7-21-6551b6d5c879@xenproject.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> References: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1795123905938158500 Content-Type: text/plain; charset="utf-8" Guest vCPUs' XSAVE areas hold guest-owned register state, and live in always-mapped memory: xenheap objects when they fit a page, vmap-backed allocations on hardware (AMX-class) where they do not. Move them to individual domain-heap pages, installed once at vCPU creation at a fixed reservation in the common area of the per-vCPU state window, following the pattern set by the SVM VMCB and VMX MSR list relocations. The seam introduced by the VCPU_MAP_XSAVE_AREA() wrappers now gains its real implementation. The window address serves when v runs here on its own page tables: v =3D=3D current, and this_cpu(pgtable_vcpu) =3D=3D= v. Neither test suffices alone. Inside the EFI runtime bracket, and after a shadow-mode update_cr3() failure, `current`'s own page tables are not loaded. While a CPU holds v's state lazily, v's page tables are loaded, but an IPI syncing that state for another CPU can switch them away at any time. The page-table test also structurally rules out one vCPU reaching through a same-vcpu_id window address into another domain's state, which a dispatch on `current` alone would have made possible to get wrong silently. With the owning vCPU running on its tables, or for an idle vCPU, the map is a pointer fetch, so the hot paths map nothing. Otherwise (a paused, never-started or lazily held vCPU) the full-area map uses map_domain_pages() and can fail; those callers gained error handling in "x86/xstate: classify xsave-area seam accesses by failure mode". The prefix variant maps only the area's first page, which the legacy region and header always fit, and hence cannot fail. A mapping of a single page is dropped with unmap_vcpu_state_page(), which also flushes this CPU's translation of it; unmap_domain_pages() flushes a multipage mapping itself. __context_switch() saves the outgoing vCPU's FPU state after `current` has moved to the incoming vCPU, with interrupts off, while the outgoing vCPU's page tables -- and therefore its window -- are normally still loaded. It takes the window address through a path of its own, vcpu_save_fpu_ctxt_switch(): the transient-mapping fallback must not run from there, as it would sync the local execution state and re-enter __context_switch(). The shadow-mode paths which abandon a vCPU's page tables early (update_cr3() failure in new_guest_cr3() and _toggle_guest_pt()) leave the idle tables loaded with the outgoing vCPU's state still in registers. Those paths have already crashed the domain, so there is nothing worth saving, and the save is skipped there, as the register frame's already is: both sit under one test of the loaded page tables. Anything else failing it is a page-table tracking bug rather than a recoverable state, so it is fatal in every build. The window reservation is fixed at three pages, sized for the largest save area current hardware produces (AMX TILEDATA); xstate_init() refuses, with an explanatory panic, hardware whose enabled feature set exceeds it. It does so on every boot: every guest vCPU's area moves. Idle vCPUs keep a globally-reachable area (xenheap, or vmap space where it exceeds a page, as with AMX) deliberately: it is scratch for accesses which execute with a guest's page tables loaded (read_bndcfgu(), xstate_set_init()) and holds no guest register state. On such paths the area a guest's MPX state is transiently saved into remains reachable from other contexts, as today. The backing pages are caller-owned (no _PAGE_AVAIL0): the per-domain teardown reclaims only the page tables, so xstate_free_save_area() unmaps the pages from the window and frees them itself. The window is in the per-domain area that all its domain's vCPUs share, which outlives a vCPU whose creation fails. A failed partial allocation unwinds in place, and xstate_free_save_area() tolerates the resulting half-built state. Memory cost: sub-page areas (all non-AMX hardware) round up from a 2.7KiB xenheap object to a full page per vCPU. The move applies to PV vCPUs as to HVM ones. A context switch now calls vcpu_xsave_area_ctxt_switch() for the save, and vcpu_map_xsave_area() and vcpu_unmap_xsave_area() around the restore, out of line, instead of reading v->arch.xsave_area. The XRSTOR following write_ptbase() reaches the area through a 4KiB, non-global PTE in the per-domain area. As switch_cr3_cr4() has just flushed every non-global translation, that takes a page walk on every switch to a guest vCPU, where a sub-page area, a xenheap object, was reached through a superpage of the directmap; the walk goes through the L1 table which the register frame's restore has just walked. On an Intel NUC, a 64-bit PV guest's vCPU context switch takes 15-30ns longer, with XPTI off or on and against either the tree before the XSAVE work or the one just before this change; code placement alone moves that measurement by up to about 20ns between builds. That was measured while the PV guest's register frames were still in struct vcpu. On AMX-class hardware, where the area takes three pages, a full-area map for a vCPU other than the one running here goes through map_domain_pages(): XEN_DOMCTL_getvcpuextstate and XEN_DOMCTL_setvcpuextstate make one for PV save, restore and migration, as the HVM XSAVE save record does for HVM guests. The area is then mapped in the calling vCPU's multipage slots, which only the calling domain's page tables carry, and dropping the mapping flushes the calling CPU's translations alone. Each of those operations can now fail with -ENOMEM. Assisted-by: Claude Code:claude-fable-5, Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Changes in v7: - Standalone on staging, without per-vCPU page tables or the sparse directmap view: the message no longer speaks of asi=3D, of per-vCPU page tables, or of a failure path staging does not take, and one comment no longer of a per-vCPU mapcache. - Move to end of the slots. - Message: Remove "All slots are in use", as the slot-sharing has freed up one slot. - Remove excessive BUILD_BUG_ON. Changes in v5: - New in this version. xstate_init() now panics if the enabled XSAVE features need more than the three pages the window reserves. No current hardware does. Would you rather this did more, for instance size the window's XSAVE range from the size found at boot, or fall back to xenheap areas on such hardware? --- xen/arch/x86/domain.c | 17 +-- xen/arch/x86/i387.c | 23 +++- xen/arch/x86/include/asm/config.h | 3 + xen/arch/x86/include/asm/domain.h | 15 ++- xen/arch/x86/include/asm/i387.h | 1 + xen/arch/x86/include/asm/xstate.h | 60 +++++------ xen/arch/x86/xstate.c | 171 +++++++++++++++++++++++++++++- 7 files changed, 240 insertions(+), 50 deletions(-) diff --git a/xen/arch/x86/domain.c b/xen/arch/x86/domain.c index a71c11cf52..938523b789 100644 --- a/xen/arch/x86/domain.c +++ b/xen/arch/x86/domain.c @@ -587,6 +587,8 @@ int arch_vcpu_create(struct vcpu *v) BUILD_BUG_ON(VCPU_STATE_IDX_VMCB >=3D VCPU_STATE_NR_PAGES); BUILD_BUG_ON(VCPU_STATE_IDX_N2VMCB >=3D VCPU_STATE_NR_PAGES); BUILD_BUG_ON(VCPU_STATE_IDX_VMX_MSR >=3D VCPU_STATE_NR_PAGES); + BUILD_BUG_ON(VCPU_STATE_IDX_XSAVE + VCPU_STATE_XSAVE_PAGES > + VCPU_STATE_NR_PAGES); BUILD_BUG_ON(VCPU_STATE_IDX_REGS >=3D VCPU_STATE_NR_PAGES); BUILD_BUG_ON(VCPU_STATE_IDX_EMUL >=3D VCPU_STATE_NR_PAGES); /* Page tables only; torn down with the rest of the per-domain are= a. */ @@ -2222,17 +2224,20 @@ static void __context_switch(void) { ASSERT(read_atomic(&p->dirty_cpu) =3D=3D cpu); /* - * p's frame is reached through its state window, mapped while p's - * own page tables are loaded, as they normally still are here. N= ot - * so after the shadow-mode paths which abandon a vCPU's tables ea= rly - * (update_cr3() failure), which have crashed the domain: the frame - * is left unsaved. A transient mapping would re-enter this funct= ion. + * p's frame and XSAVE area are reached through its state window, + * mapped while p's own page tables are loaded, as they normally + * still are here. Not so after the shadow-mode paths which aband= on + * a vCPU's tables early (update_cr3() failure), which have crashed + * the domain: both are left unsaved. A transient mapping would + * re-enter this function. */ if ( likely(this_cpu(pgtable_vcpu) =3D=3D p) ) + { memcpy(vcpu_user_regs(p), stack_regs, CTXT_SWITCH_STACK_BYTES); + vcpu_save_fpu_ctxt_switch(p); + } else BUG_ON(!domain_shutting_down(pd)); - vcpu_save_fpu(p); pd->arch.ctxt_switch->from(p); } =20 diff --git a/xen/arch/x86/i387.c b/xen/arch/x86/i387.c index d49e391e99..239404b37b 100644 --- a/xen/arch/x86/i387.c +++ b/xen/arch/x86/i387.c @@ -177,7 +177,9 @@ void vcpu_restore_fpu(struct vcpu *v) =20 ASSERT(!is_idle_vcpu(v)); =20 + /* v runs here on its own page tables: the map cannot fail. */ xsave_area =3D VCPU_MAP_XSAVE_AREA(v); + BUG_ON(!xsave_area); =20 if ( cpu_has_xsave ) fpu_xrstor(v, xsave_area, XSTATE_ALL); @@ -195,25 +197,36 @@ void vcpu_restore_fpu(struct vcpu *v) * On each context switch, save the necessary FPU info of VCPU being switc= h=20 * out. It dispatches saving operation based on CPU's capability. */ -void vcpu_save_fpu(struct vcpu *v) +static void save_fpu(struct vcpu *v, struct xsave_struct *xsave_area) { - struct xsave_struct *xsave_area; - ASSERT(!is_idle_vcpu(v)); =20 /* This can happen, if a paravirtualised guest OS has set its CR0.TS. = */ clts(); =20 - xsave_area =3D VCPU_MAP_XSAVE_AREA(v); - if ( cpu_has_xsave ) fpu_xsave(v, xsave_area); else fpu_fxsave(v, &xsave_area->fpu_sse); +} =20 +/* Save the FPU state of v, the vCPU running here. */ +void vcpu_save_fpu(struct vcpu *v) +{ + /* As in vcpu_restore_fpu(): the fastpath, which cannot fail. */ + struct xsave_struct *xsave_area =3D VCPU_MAP_XSAVE_AREA(v); + + BUG_ON(!xsave_area); + save_fpu(v, xsave_area); VCPU_UNMAP_XSAVE_AREA(v, xsave_area); } =20 +/* As vcpu_save_fpu(), for __context_switch() switching v out. */ +void vcpu_save_fpu_ctxt_switch(struct vcpu *v) +{ + save_fpu(v, vcpu_xsave_area_ctxt_switch(v)); +} + /* Initialize FPU's context save area */ int vcpu_init_fpu(struct vcpu *v) { diff --git a/xen/arch/x86/include/asm/config.h b/xen/arch/x86/include/asm/c= onfig.h index 8e9b856a6c..cea4cd4662 100644 --- a/xen/arch/x86/include/asm/config.h +++ b/xen/arch/x86/include/asm/config.h @@ -325,6 +325,9 @@ extern unsigned long xen_phys_start; /* Common slots */ #define VCPU_STATE_IDX_REGS 2 #define VCPU_STATE_IDX_EMUL 3 +/* Pages reserved for the XSAVE area; bounded at boot in xstate_init(). */ +#define VCPU_STATE_XSAVE_PAGES 3 +#define VCPU_STATE_IDX_XSAVE 4 /* First of VCPU_STATE_XSAVE_PAGES slot= s */ =20 #define ELFSIZE 64 =20 diff --git a/xen/arch/x86/include/asm/domain.h b/xen/arch/x86/include/asm/d= omain.h index 3980d6228a..2386cedcd7 100644 --- a/xen/arch/x86/include/asm/domain.h +++ b/xen/arch/x86/include/asm/domain.h @@ -655,12 +655,19 @@ struct arch_vcpu unsigned long cr3; /* (MA) value to install in HW CR3= */ =20 /* - * The save area for Processor Extended States and the bitmask of the - * XSAVE/XRSTOR features. They are used by: 1) when a vcpu (which has - * dirtied FPU/SSE) is scheduled out we XSAVE the states here; 2) in - * #NM handler, we XRSTOR the states we XSAVE-ed; + * The save area for Processor Extended States, XSAVEd on context + * switch out and XRSTORed on context switch in. For guest vCPUs + * the backing pages (xsave_pg[], xsave_pgcnt of them) come from the + * domain heap and are mapped once, for the life of the vCPU, at a + * fixed spot in the per-vCPU state window; xsave_area holds that + * window address, valid whenever the owning vCPU's page tables are + * loaded. Idle vCPUs' areas are ordinary Xen allocations (xenheap, + * or vmap space when larger than a page), reachable from every + * context. Any other access goes through VCPU_MAP_XSAVE_AREA(). */ struct xsave_struct *xsave_area; + struct page_info *xsave_pg[VCPU_STATE_XSAVE_PAGES]; + unsigned int xsave_pgcnt; uint64_t xcr0; /* Accumulated eXtended features mask for using XSAVE/XRESTORE by Xen * itself, as we can never know whether guest OS depends on content diff --git a/xen/arch/x86/include/asm/i387.h b/xen/arch/x86/include/asm/i38= 7.h index 0717005d31..8d28de3ba8 100644 --- a/xen/arch/x86/include/asm/i387.h +++ b/xen/arch/x86/include/asm/i387.h @@ -29,6 +29,7 @@ struct ix87_env { =20 void vcpu_restore_fpu(struct vcpu *v); void vcpu_save_fpu(struct vcpu *v); +void vcpu_save_fpu_ctxt_switch(struct vcpu *v); int vcpu_init_fpu(struct vcpu *v); void vcpu_destroy_fpu(struct vcpu *v); =20 diff --git a/xen/arch/x86/include/asm/xstate.h b/xen/arch/x86/include/asm/x= state.h index c23308cbe2..a906df11ad 100644 --- a/xen/arch/x86/include/asm/xstate.h +++ b/xen/arch/x86/include/asm/xstate.h @@ -130,46 +130,43 @@ xsave_area_compressed(const struct xsave_struct *xsav= e_area) } =20 /* - * Fetch a pointer to a vCPU's XSAVE area + * Fetch a pointer to a vCPU's XSAVE area. * - * TL;DR: If v =3D=3D current, the mapping is guaranteed to already exist. + * Guest vCPUs' areas live on the domain heap, mapped once for the life + * of the vCPU at a fixed spot in the owning vCPU's state window. For a + * vCPU running here on its own page tables, and for an idle vCPU, the + * "map" is a pointer fetch. * - * Despite the name, this macro might not actually map anything. The only = case - * in which a mutation of page tables is strictly required is when ASI=3D= =3Don && - * v!=3Dcurrent. For everything else the mapping already exists and needs = not - * be created nor destroyed. + * Otherwise -- a paused, never-started or lazily held vCPU -- the area + * is genuinely mapped, and mapping the full, possibly multi-page area + * can fail: VCPU_MAP_XSAVE_AREA() may return NULL, and callers must + * handle that. The _PREFIX variant below cannot fail. * - * +-----------------+--------------+ - * | v =3D=3D current | v !=3D current | - * +--------------+-----------------+--------------+ - * | ASI enabled | per-vCPU fixmap | actual map | - * +--------------+-----------------+--------------+ - * | ASI disabled | directmap | - * +--------------+--------------------------------+ - * - * There MUST NOT be outstanding maps of XSAVE areas of the non-current vC= PU - * at the point of context switch. Otherwise, the unmap operation will - * misbehave. - * - * TODO: Expand the macro to the ASI cases after infra to do so is in plac= e. + * There MUST NOT be outstanding maps of another vCPU's XSAVE area at + * the point of context switch: such a mapping is only valid on the + * pCPU that created it, and only until the next reschedule. * * @param v Owner of the XSAVE area */ -#define VCPU_MAP_XSAVE_AREA(v) ((v)->arch.xsave_area) +struct xsave_struct *vcpu_map_xsave_area(const struct vcpu *v); +void vcpu_unmap_xsave_area(const struct vcpu *v, const void *area); +struct xsave_struct *vcpu_map_xsave_area_prefix(const struct vcpu *v); +void vcpu_unmap_xsave_area_prefix(const struct vcpu *v, const void *area); +struct xsave_struct *vcpu_xsave_area_ctxt_switch(const struct vcpu *v); + +#define VCPU_MAP_XSAVE_AREA(v) vcpu_map_xsave_area(v) =20 /* - * Drops the mapping of a vCPU's XSAVE area and nullifies its pointer on e= xit - * - * See VCPU_MAP_XSAVE_AREA() for additional information on the persistence= of - * these mappings. This macro only tears down the mappings in the ASI=3Don= && - * v!=3Dcurrent case. - * - * TODO: Expand the macro to the ASI cases after infra to do so is in plac= e. + * Drops the mapping of a vCPU's XSAVE area and nullifies the local + * pointer on exit. * * @param v Owner of the XSAVE area * @param x XSAVE blob of v */ -#define VCPU_UNMAP_XSAVE_AREA(v, x) do { (void)(v); (x) =3D NULL; } while = (0) +#define VCPU_UNMAP_XSAVE_AREA(v, x) do { \ + vcpu_unmap_xsave_area(v, x); \ + (x) =3D NULL; \ +} while ( false ) =20 /* * Variant of VCPU_MAP_XSAVE_AREA() for accessors which touch nothing @@ -177,7 +174,10 @@ xsave_area_compressed(const struct xsave_struct *xsave= _area) * XSTATE_AREA_MIN_SIZE bytes). That prefix always fits within the * area's first page, so this variant cannot fail. */ -#define VCPU_MAP_XSAVE_AREA_PREFIX(v) VCPU_MAP_XSAVE_AREA(v) -#define VCPU_UNMAP_XSAVE_AREA_PREFIX(v, x) VCPU_UNMAP_XSAVE_AREA(v, x) +#define VCPU_MAP_XSAVE_AREA_PREFIX(v) vcpu_map_xsave_area_prefix(v) +#define VCPU_UNMAP_XSAVE_AREA_PREFIX(v, x) do { \ + vcpu_unmap_xsave_area_prefix(v, x); \ + (x) =3D NULL; \ +} while ( false ) =20 #endif /* __ASM_XSTATE_H */ diff --git a/xen/arch/x86/xstate.c b/xen/arch/x86/xstate.c index 2dd1699aba..c7b750db0a 100644 --- a/xen/arch/x86/xstate.c +++ b/xen/arch/x86/xstate.c @@ -5,6 +5,7 @@ * */ =20 +#include #include #include #include @@ -14,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -555,9 +557,61 @@ int xstate_alloc_save_area(struct vcpu *v) =20 /* XSAVE/XRSTOR requires the save area be 64-byte-boundary aligned. */ BUILD_BUG_ON(__alignof(*save_area) < 64); - save_area =3D _xvzalloc(size, __alignof(*save_area)); - if ( save_area =3D=3D NULL ) - return -ENOMEM; + + if ( !is_idle_vcpu(v) ) + { + /* + * The pages are the vCPU's own (no _PAGE_AVAIL0): the area teardo= wn + * leaves them alone, and xstate_free_save_area() frees them. + */ + unsigned int nr =3D PFN_UP(size), i; + mfn_t mfns[VCPU_STATE_XSAVE_PAGES]; + + /* xstate_init() bounds the host size against the reservation. */ + BUG_ON(nr > VCPU_STATE_XSAVE_PAGES); + + for ( i =3D 0; i < nr; i++ ) + { + struct page_info *pg =3D alloc_domheap_page(v->domain, + MEMF_no_owner); + + if ( !pg ) + { + while ( i-- ) + { + free_domheap_page(v->arch.xsave_pg[i]); + v->arch.xsave_pg[i] =3D NULL; + } + return -ENOMEM; + } + + v->arch.xsave_pg[i] =3D pg; + mfns[i] =3D page_to_mfn(pg); + clear_domain_page(mfns[i]); + } + v->arch.xsave_pgcnt =3D nr; + + populate_perdomain_mapping(v, VCPU_STATE_ADDR(v, VCPU_STATE_IDX_XS= AVE), + mfns, nr, __PAGE_HYPERVISOR_RW); + + /* + * The window VA is not reachable from the creating context (v is + * still being built); seed the initial register values through a + * transient mapping of the first page instead. + */ + save_area =3D map_domain_page(mfns[0]); + } + else + { + /* + * Idle vCPUs' areas must be reachable from every context: they are + * scratch for accesses made with a guest's page tables loaded + * (read_bndcfgu(), xstate_set_init()). + */ + save_area =3D _xvzalloc(size, __alignof(*save_area)); + if ( save_area =3D=3D NULL ) + return -ENOMEM; + } =20 /* * We're creating a vCPU, so conceptually we should be choosing the @@ -578,7 +632,15 @@ int xstate_alloc_save_area(struct vcpu *v) save_area->fpu_sse.ftw =3D FXSAVE_FTW_RESET; save_area->fpu_sse.mxcsr =3D MXCSR_DEFAULT; =20 - v->arch.xsave_area =3D save_area; + if ( !is_idle_vcpu(v) ) + { + unmap_domain_page(save_area); + v->arch.xsave_area =3D + (void *)VCPU_STATE_ADDR(v, VCPU_STATE_IDX_XSAVE); + } + else + v->arch.xsave_area =3D save_area; + v->arch.xcr0 =3D 0; v->arch.xcr0_accum =3D 0; =20 @@ -587,7 +649,102 @@ int xstate_alloc_save_area(struct vcpu *v) =20 void xstate_free_save_area(struct vcpu *v) { - XVFREE(v->arch.xsave_area); + if ( !is_idle_vcpu(v) ) + { + unsigned int i, nr =3D v->arch.xsave_pgcnt; + + if ( nr ) + { + unmap_vcpu_state(v, VCPU_STATE_IDX_XSAVE, nr); + for ( i =3D 0; i < nr; i++ ) + { + free_domheap_page(v->arch.xsave_pg[i]); + v->arch.xsave_pg[i] =3D NULL; + } + v->arch.xsave_pgcnt =3D 0; + } + + v->arch.xsave_area =3D NULL; + } + else + XVFREE(v->arch.xsave_area); +} + +struct xsave_struct *vcpu_map_xsave_area(const struct vcpu *v) +{ + mfn_t mfns[VCPU_STATE_XSAVE_PAGES]; + unsigned int i; + + if ( is_idle_vcpu(v) ) + return v->arch.xsave_area; + + /* + * The window VA is valid while v's own page tables are loaded, but th= at + * alone is not enough: while this CPU holds v's state lazily, an IPI + * syncing that state for another CPU can switch v's page tables away + * at any time. Hence the window only for v running here on its own + * page tables, which current's are not inside the EFI runtime bracket + * or after a shadow-mode update_cr3() failure. + */ + if ( v =3D=3D current && this_cpu(pgtable_vcpu) =3D=3D v ) + return v->arch.xsave_area; + + for ( i =3D 0; i < v->arch.xsave_pgcnt; i++ ) + mfns[i] =3D page_to_mfn(v->arch.xsave_pg[i]); + + return map_domain_pages(mfns, v->arch.xsave_pgcnt); +} + +void vcpu_unmap_xsave_area(const struct vcpu *v, const void *area) +{ + if ( !area || is_idle_vcpu(v) || + area =3D=3D (const void *)v->arch.xsave_area ) + return; + + /* + * A single page comes from the single-page mapcache, which defers its + * flushes; unmap_domain_pages() flushes a multipage mapping itself. + */ + if ( v->arch.xsave_pgcnt =3D=3D 1 ) + unmap_vcpu_state_page(area); + else + unmap_domain_pages(area, v->arch.xsave_pgcnt); +} + +struct xsave_struct *vcpu_map_xsave_area_prefix(const struct vcpu *v) +{ + /* The legacy region and header always fit the area's first page. */ + BUILD_BUG_ON(sizeof(struct xsave_struct) > PAGE_SIZE); + + /* The window VA under vcpu_map_xsave_area()'s conditions. */ + if ( is_idle_vcpu(v) || + (v =3D=3D current && this_cpu(pgtable_vcpu) =3D=3D v) ) + return v->arch.xsave_area; + + return map_domain_page(page_to_mfn(v->arch.xsave_pg[0])); +} + +void vcpu_unmap_xsave_area_prefix(const struct vcpu *v, const void *area) +{ + if ( !area || is_idle_vcpu(v) || + area =3D=3D (const void *)v->arch.xsave_area ) + return; + + unmap_vcpu_state_page(area); +} + +/* + * The XSAVE area of @v for __context_switch() saving it: `current` has + * moved on to the incoming vCPU, but @v's page tables are still loaded, a= nd + * interrupts are off, so the window VA serves. A transient mapping is not + * an option from there: it would sync the lazy state and re-enter + * __context_switch(). + */ +struct xsave_struct *vcpu_xsave_area_ctxt_switch(const struct vcpu *v) +{ + BUG_ON(is_idle_vcpu(v) || this_cpu(pgtable_vcpu) !=3D v); + + return v->arch.xsave_area; } =20 static bool valid_xcr0(uint64_t xcr0) @@ -934,6 +1091,10 @@ void xstate_init(struct cpuinfo_x86 *c) xsave_cntxt_size =3D cpuid_count_ebx(0xd, 0); printk("xstate: size: %#x and states: %#"PRIx64"\n", xsave_cntxt_size, xfeature_mask); + + if ( PFN_UP(xsave_cntxt_size) > VCPU_STATE_XSAVE_PAGES ) + panic("XSAVE area needs %lu pages, vCPU state window reserves = %u\n", + PFN_UP(xsave_cntxt_size), VCPU_STATE_XSAVE_PAGES); } else { --=20 2.56.0 From nobody Mon Nov 23 22:32:25 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=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1795123859; cv=none; d=zohomail.com; s=zohoarc; b=E4vAdP38lJsbF90UxA5TelsfnXCxVA1kjHhC7V5cZSE/jdYIqBoEUflhg0YUbfZL3ieTHK+nfNrHNYJi4/gX0cYEkmLW79gSAz0h6ygEp43UlnqOhlRUUKATP1GmbSfj/cfivWLG3fcgABJR1A1dlO9hCkN9FW5+Ofx07jzlTH0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795123859; 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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=xdrO5K/YS2R835ir7OGtBuIoygfpIodW4HkAjPJnUxc=; b=Iwk88x18yVLp2Ehu5ThemSwqhFtp1giLVSDkRk3FRPhE+Op0+S8iM3itixHOw7k3vtYj1gfmc8OM2dsbsQxDggfk3qaFdF9Ss3MEAlcFu91zUqKpBQEbWcNqS9sG1inuF/Q1umR/n4HwNuoIh5oSu8X/ZV3+Pra8xFngPsrXC3c= 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=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1795123859197376.570005124824; Thu, 19 Nov 2026 13:30:59 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1497123.1695480 (Exim 4.92) (envelope-from ) id 1xU9i9-0003KJ-86; Thu, 19 Nov 2026 21:30:29 +0000 Received: by outflank-mailman (output) from mailman id 1497123.1695480; Thu, 19 Nov 2026 21:30:29 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9i8-0003GW-ED; Thu, 19 Nov 2026 21:30:28 +0000 Received: by outflank-mailman (input) for mailman id 1497123; Thu, 19 Nov 2026 21:30:19 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9hz-0001Cv-BF for xen-devel@lists.xenproject.org; Thu, 19 Nov 2026 21:30:19 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xU9hy-00000006tHb-3s0e; Thu, 19 Nov 2026 21:30:19 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xU9MX-0000000CVBH-2VNJ; Thu, 19 Nov 2026 21:08:09 +0000 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" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:Content-Type: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=xdrO5K/YS2R835ir7OGtBuIoygfpIodW4HkAjPJnUxc=; b=S6Co78Iw7ihcudJ9kcvJnMi8xr n4oVQRJSCVmBKLWgR6vgbrfl3YuYsN2oVQGaCU1npDukSkMdCGJXInAQyJQ4DE4gRc5ak0ZEFfARF 5xOIpGb77j4rLnJcFXzl3mjP1sEBDE6eO9HOuU6Ke+04mhhGk4YUER2Z2nwK5XPn+Lw8=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie Subject: [PATCH v7 22/50] x86/mm: prepare create_perdomain_mapping() for per-vCPU perdomain areas Date: Thu, 19 Nov 2026 21:07:13 +0000 Message-ID: <20261119-asi-hvm-v7-22-6551b6d5c879@xenproject.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> References: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1795123859472158500 From: Roger Pau Monn=C3=A9 We want to change per-domain mappings to be per-vCPU mappings. In preparation for that, we want to arrange that create_perdomain_mapping() work either with a single perdomain area, or with a per-vCPU perdomain area. The remaining callers are already in a vCPU context. This is no accident: much of the perdomain area has always been laid out in per-vCPU slices -- each vCPU has its own GDT/LDT window and its own COMPAT_ARG_XLAT pages, and now its own state window and map_domain_pages() slots -- and each vCPU's slice is set up as that vCPU is created. For these callers, we just need to change the parameter from a domain pointer to a vCPU pointer. Once the perdomain area itself becomes per-vCPU, the same calls will populate the owning vCPU's own area rather than slices of a shared one. Note that we cannot yet do a parallel movement of free_perdomain_mappings(): the per-domain page-table hierarchy is still a single domain-wide structure shared by all vCPUs, so tearing it down from a per-vCPU path would pull the mappings out from under sibling vCPUs (e.g. on a partially failed, retryable XEN_DOMCTL_max_vcpus), and vCPU-create error paths can rely on domain destruction to free a partially set up hierarchy. Once a domain's area is per-vCPU, a later change gives its vCPUs a teardown of their own; the domain-wide teardown stays for domains with a shared area, PV ones included. Signed-off-by: Roger Pau Monn=C3=A9 Assisted-by: Claude Code:claude-fable-5, Claude Code:claude-opus-4-8, Claud= e Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Changes in v7: - Convert the calls for the vCPU state window and the map_domain_pages() slots too, which now come earlier in the series. Changes in v6: - Rebase over "x86/mm: allocate the per-domain L3 in arch_domain_create()". - No longer add an implicit L3 creation in arch_vcpu_create(). Changes in v5: - Message: the domain-wide mapcache is one pool, not per-vCPU windows: drop it from the per-vCPU slices, and say "much of" the area. Changes in v4: - Message: drop a sentence repeating its paragraph, and say that the domain-wide teardown stays for domains with a shared area (Roger). - End the comment in arch_vcpu_create() at the colon (Roger). Changes in v3: - Rebased onto staging without the GDT/LDT conversions (for the HVM-only series): the capture parameters stay, as the PV GDT/LDT stash still uses them. Changes in v2: - Added to the series Changes since the previously posted version: - Keep free_perdomain_mappings() (and hence perdomain teardown) domain-scoped. - Keep the idle domain without a perdomain area. - Retitle (was: "x86/mm: switch {create,destroy}_perdomain_mapping() domain parameter to vCPU"); destroy_perdomain_mapping() was switched in a separate patch. - Split the removal of mapcache_domain_init()'s redundant create_perdomain_mapping() call into its own (preceding) patch. --- xen/arch/x86/domain.c | 2 +- xen/arch/x86/domain_page.c | 8 ++++---- xen/arch/x86/include/asm/mm.h | 2 +- xen/arch/x86/mm.c | 3 ++- xen/arch/x86/pv/domain.c | 2 +- xen/arch/x86/x86_64/mm.c | 2 +- 6 files changed, 10 insertions(+), 9 deletions(-) diff --git a/xen/arch/x86/domain.c b/xen/arch/x86/domain.c index 938523b789..6e33d56588 100644 --- a/xen/arch/x86/domain.c +++ b/xen/arch/x86/domain.c @@ -592,7 +592,7 @@ int arch_vcpu_create(struct vcpu *v) BUILD_BUG_ON(VCPU_STATE_IDX_REGS >=3D VCPU_STATE_NR_PAGES); BUILD_BUG_ON(VCPU_STATE_IDX_EMUL >=3D VCPU_STATE_NR_PAGES); /* Page tables only; torn down with the rest of the per-domain are= a. */ - rc =3D create_perdomain_mapping(d, VCPU_STATE_START(v), + rc =3D create_perdomain_mapping(v, VCPU_STATE_START(v), VCPU_STATE_NR_PAGES, NIL(l1_pgentry_t *), NULL); if ( rc !=3D 0 ) diff --git a/xen/arch/x86/domain_page.c b/xen/arch/x86/domain_page.c index ea759155da..dce66beccd 100644 --- a/xen/arch/x86/domain_page.c +++ b/xen/arch/x86/domain_page.c @@ -386,7 +386,7 @@ int mapcache_vcpu_init(struct vcpu *v) /* The vCPU's stride of the multipage area. */ if ( !is_idle_domain(d) ) { - int rc =3D create_perdomain_mapping(d, MAPCACHE_MULTIPAGE_START(v), + int rc =3D create_perdomain_mapping(v, MAPCACHE_MULTIPAGE_START(v), MAPCACHE_MULTIPAGE_ENTRIES, NIL(l1_pgentry_t *), NULL); =20 @@ -400,15 +400,15 @@ int mapcache_vcpu_init(struct vcpu *v) if ( ents > dcache->entries ) { /* Populate page tables. */ - int rc =3D create_perdomain_mapping(d, MAPCACHE_VIRT_START, ents, + int rc =3D create_perdomain_mapping(v, MAPCACHE_VIRT_START, ents, NIL(l1_pgentry_t *), NULL); =20 /* Populate bit maps. */ if ( !rc ) - rc =3D create_perdomain_mapping(d, (unsigned long)dcache->inus= e, + rc =3D create_perdomain_mapping(v, (unsigned long)dcache->inus= e, nr, NULL, NIL(struct page_info *= )); if ( !rc ) - rc =3D create_perdomain_mapping(d, (unsigned long)dcache->garb= age, + rc =3D create_perdomain_mapping(v, (unsigned long)dcache->garb= age, nr, NULL, NIL(struct page_info *= )); =20 if ( rc ) diff --git a/xen/arch/x86/include/asm/mm.h b/xen/arch/x86/include/asm/mm.h index bbce8c401e..1ef05dcd46 100644 --- a/xen/arch/x86/include/asm/mm.h +++ b/xen/arch/x86/include/asm/mm.h @@ -607,7 +607,7 @@ int compat_arch_memory_op(unsigned long cmd, XEN_GUEST_= HANDLE_PARAM(void) arg); #define IS_NIL(ptr) (!((uintptr_t)(ptr) + sizeof(*(ptr)))) =20 struct page_info *allocate_perdomain_l3(struct domain *d); -int create_perdomain_mapping(struct domain *d, unsigned long va, +int create_perdomain_mapping(struct vcpu *v, unsigned long va, unsigned int nr, l1_pgentry_t **pl1tab, struct page_info **ppg); void populate_perdomain_mapping(const struct vcpu *v, unsigned long va, diff --git a/xen/arch/x86/mm.c b/xen/arch/x86/mm.c index 7cfeceb8c6..26bc2cc229 100644 --- a/xen/arch/x86/mm.c +++ b/xen/arch/x86/mm.c @@ -6245,10 +6245,11 @@ struct page_info *allocate_perdomain_l3(struct doma= in *d) return pg; } =20 -int create_perdomain_mapping(struct domain *d, unsigned long va, +int create_perdomain_mapping(struct vcpu *v, unsigned long va, unsigned int nr, l1_pgentry_t **pl1tab, struct page_info **ppg) { + struct domain *d =3D v->domain; struct page_info *pg; l3_pgentry_t *l3tab; l2_pgentry_t *l2tab; diff --git a/xen/arch/x86/pv/domain.c b/xen/arch/x86/pv/domain.c index 28ea0742ab..f3c711b85b 100644 --- a/xen/arch/x86/pv/domain.c +++ b/xen/arch/x86/pv/domain.c @@ -313,7 +313,7 @@ int switch_compat(struct domain *d) =20 static int pv_create_gdt_ldt_l1tab(struct vcpu *v) { - return create_perdomain_mapping(v->domain, GDT_VIRT_START(v), + return create_perdomain_mapping(v, GDT_VIRT_START(v), 1U << GDT_LDT_VCPU_SHIFT, v->domain->arch.pv.gdt_ldt_l1tab, NULL); diff --git a/xen/arch/x86/x86_64/mm.c b/xen/arch/x86/x86_64/mm.c index f467ed426b..7b04537066 100644 --- a/xen/arch/x86/x86_64/mm.c +++ b/xen/arch/x86/x86_64/mm.c @@ -732,7 +732,7 @@ void __init zap_low_mappings(void) =20 int setup_compat_arg_xlat(struct vcpu *v) { - return create_perdomain_mapping(v->domain, ARG_XLAT_START(v), + return create_perdomain_mapping(v, ARG_XLAT_START(v), PFN_UP(COMPAT_ARG_XLAT_SIZE), NULL, NIL(struct page_info *)); } --=20 2.56.0 From nobody Mon Nov 23 22:32:25 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=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1795123849; cv=none; d=zohomail.com; s=zohoarc; b=E38kKK8gvXYS0KuTKJq7o6evIySKnx2Cy8xBxB8HzgymES8QzgTMRwhkTGvIZSjHBahIDLLv52eOpwHd0eq/hVR3hmjJbEdWLEM2AD7rzqCVOdmIyfpN4x72n8as2ost6VA9dJh43riPbUL6rikjzv8OZlxkPgJe6rUinpXi3cc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795123849; 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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=TxpbNUT6Z3P5nJwaQ6OfVBegzrsNl6uHC7c+HRzS2+g=; b=bTLmQCst1wx+EXbPm3qjOsvc2+7bGlW+eBvWeM6IisEGj4t5rlD3Zkf9yFsDUXuo+K5LRMaY8kwhkXacs+JQwPV2NUd905Ar8gGWvT3iOxqIlK93WycE2GaiALJ+WDkkBalB+w6FKk9NZjzHVvuTRP0+hK4vZH2qSDf3SOr8970= 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=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1795123849301139.12824327926376; Thu, 19 Nov 2026 13:30:49 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1497110.1695381 (Exim 4.92) (envelope-from ) id 1xU9i0-0001FQ-NT; Thu, 19 Nov 2026 21:30:20 +0000 Received: by outflank-mailman (output) from mailman id 1497110.1695381; Thu, 19 Nov 2026 21:30:20 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9i0-0001F2-AA; Thu, 19 Nov 2026 21:30:20 +0000 Received: by outflank-mailman (input) for mailman id 1497110; Thu, 19 Nov 2026 21:30:19 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9hy-0001Bh-Ue for xen-devel@lists.xenproject.org; Thu, 19 Nov 2026 21:30:18 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xU9hy-00000006tG7-1V7w; Thu, 19 Nov 2026 21:30:18 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xU9MZ-0000000CVBH-07gf; Thu, 19 Nov 2026 21:08:11 +0000 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" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:Content-Type: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=TxpbNUT6Z3P5nJwaQ6OfVBegzrsNl6uHC7c+HRzS2+g=; b=hFQaTVqP6j83bE9yDw0oGXPvY3 IIpckpyPVaSWstWK9afrPbm1uLohoDE8iZoRD/ZMgN7pSPg6F88PcUN+CbDAxcAPgXouEi8oOc6I7 qeYYaFu97o1gHDdRCZX05BSkAzqMyoBh5tAfJ7XG2845aQ3vANfRaSJgvpjMmxz8r7pI=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Andrew Cooper , Anthony PERARD , Michal Orzel , Jan Beulich , Julien Grall , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Stefano Stabellini , Teddy Astie Subject: [PATCH v7 23/50] x86/spec-ctrl: introduce Address Space Isolation command line option Date: Thu, 19 Nov 2026 21:07:14 +0000 Message-ID: <20261119-asi-hvm-v7-23-6551b6d5c879@xenproject.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> References: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1795123851409158500 From: Roger Pau Monn=C3=A9 Introduce the `asi=3D` command line option, and the opt_vcpu_pt_hvm and opt_vcpu_pt_hwdom knobs plus the per-domain d->arch.asi.vcpu_pt setting they control. The option is introduced ahead of the functionality it enables, so that the newly added code can be keyed on it from the start; all knobs currently default to off, and any use of the option, `asi=3D0` included, adds a boot-time warning that the feature is not functional and is meant for development only. The mechanisms this option controls apply to HVM domains: an HVM vCPU already runs on its own monitor table, so giving each vCPU its own per-domain area needs no work at context switch. d->arch.asi.vcpu_pt is therefore only ever set for HVM domains, a PVH hardware domain included; PV domains, a PV hardware domain included, are unaffected. Besides the plain boolean, `pv=3D`, `hvm=3D` and `dom0=3D` select the featu= res for PV domains or HVM domains other than the hardware domain, or for the hardware domain, PV or PVH. None of the features applies to PV domains, so `pv=3D` has no effect beyond a boot-log line saying it is ignored, and `dom0=3D` acts on a PVH hardware domain only. The boot log gains "ASI features for ..." lines for Dom0 and HVM domains, so hardware-domain-only configurations remain visible. Further per-mechanism tokens arrive with their mechanisms in later patches. Signed-off-by: Roger Pau Monn=C3=A9 Assisted-by: Claude Code:claude-fable-5, Claude Code:claude-opus-4-8, Claud= e Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Changes in v5: - Rename opt_pvh_vcpu_pt_hwdom back to opt_vcpu_pt_hwdom: that it acts on a PVH hardware domain only is a limitation of the implementation, not of the design (Roger). - pv=3D logs that it is ignored (Roger). - parse_asi() adds the development warning, so any use of asi=3D gives it and later sub-options need not extend a condition (Roger). - print_details(): IS_ENABLED(CONFIG_HVM) rather than #ifdef (Roger). Changes in v4: - Docs: "domains" throughout, not "guests" (Roger). - Group the per-domain settings as one-bit fields of d->arch.asi (Roger). - Rename opt_vcpu_pt_hwdom to opt_pvh_vcpu_pt_hwdom: it acts on a PVH hardware domain only (Jan). - parse_asi(): break in the success branch and fall through to default:, rather than a default: label after an else (MISRA C:2012 Rule 16.2; Jan). - Message: enabling a knob adds a boot-time warning; it does not taint (Roger). - Add the pv=3D and dom0=3D sub-options here, beside hvm=3D (Roger). Changes in v3: - HVM only: drop the PV knob and its interaction with XPTI. vCPU-PT applies to HVM domains and to a PVH hardware domain; the Dom0 boot log line says None for a PV one. Changes in v2: - Added to the series Changes since the previously posted version: - Include the hardware domain in the development warning and the boot log summary. - Make the opt_vcpu_pt_* knobs plain booleans preinitialised to false, dropping the late -1 resolution. - Documentation: mention possible protection against unmitigated attacks, and state that hvm=3D does not affect the hardware domain. - Rewrite the commit message. --- docs/misc/xen-command-line.pandoc | 29 ++++++++++ xen/arch/x86/include/asm/domain.h | 6 ++ xen/arch/x86/include/asm/spec_ctrl.h | 2 + xen/arch/x86/spec_ctrl.c | 87 ++++++++++++++++++++++++++++ 4 files changed, 124 insertions(+) diff --git a/docs/misc/xen-command-line.pandoc b/docs/misc/xen-command-line= .pandoc index a6f08463d1..839d06da26 100644 --- a/docs/misc/xen-command-line.pandoc +++ b/docs/misc/xen-command-line.pandoc @@ -202,6 +202,35 @@ to appropriate auditing by Xen. Argo is disabled by d= efault. This option is disabled by default, to protect domains from a DoS by a buggy or malicious other domain spamming the ring. =20 +### asi (x86) +> `=3D List of [ , {pv,hvm,dom0}=3D, +> vcpu-pt=3D | vcpu-pt=3Dhvm=3D ]` + +> Default: `false` + +Offers control over whether the hypervisor will engage in Address Space +Isolation, by not having potentially sensitive information permanently map= ped +in the VMM page-tables. Using this option might avoid the need to apply +mitigations for certain speculative related attacks, at the cost of mapping +sensitive information on-demand. It might also offer some protection agai= nst +unmitigated speculation-related attacks. + +The mechanisms currently implemented apply to HVM domains, including a PVH +hardware domain. PV domains, including a PV hardware domain, are unaffect= ed. + +* `pv=3D`, `hvm=3D` and `dom0=3D` enable the features for PV domains or HV= M domains + other than the hardware domain, or for the hardware domain, PV or PVH. = The + plain boolean does all three: `asi=3D1,dom0=3D0` leaves the hardware dom= ain + out. The forms naming no domain type (an un-suffixed `vcpu-pt=3D`) + cover the hardware domain too. + +**WARNING: manual de-selection of enabled options will invalidate any +protection offered by the feature. The fine grained options provided below +are meant to be used for debugging purposes only.** + +* `vcpu-pt` gives each vCPU its own per-domain area: the per-domain slot of + each vCPU's monitor table maps a region private to that vCPU. + ### asid (x86) > `=3D ` =20 diff --git a/xen/arch/x86/include/asm/domain.h b/xen/arch/x86/include/asm/d= omain.h index 2386cedcd7..0ec0db0b91 100644 --- a/xen/arch/x86/include/asm/domain.h +++ b/xen/arch/x86/include/asm/domain.h @@ -471,6 +471,12 @@ struct arch_domain /* Don't unconditionally inject #GP for unhandled MSRs. */ bool msr_relaxed; =20 + /* Fine grained ASI related options. */ + struct { + /* Give each vCPU its own per-domain area (HVM only). */ + bool vcpu_pt:1; + } asi; + /* Emulated devices enabled bitmap. */ uint32_t emulation_flags; } __cacheline_aligned; diff --git a/xen/arch/x86/include/asm/spec_ctrl.h b/xen/arch/x86/include/as= m/spec_ctrl.h index 8f82533c41..b62229ab5d 100644 --- a/xen/arch/x86/include/asm/spec_ctrl.h +++ b/xen/arch/x86/include/asm/spec_ctrl.h @@ -87,6 +87,8 @@ extern uint8_t default_scf; =20 extern int8_t opt_xpti_hwdom, opt_xpti_domu; =20 +extern bool opt_vcpu_pt_hwdom, opt_vcpu_pt_hvm; + extern bool cpu_has_bug_l1tf; extern int8_t opt_pv_l1tf_hwdom, opt_pv_l1tf_domu; extern bool opt_bp_spec_reduce; diff --git a/xen/arch/x86/spec_ctrl.c b/xen/arch/x86/spec_ctrl.c index fb015cac5e..d97a80695b 100644 --- a/xen/arch/x86/spec_ctrl.c +++ b/xen/arch/x86/spec_ctrl.c @@ -86,6 +86,10 @@ bool __ro_after_init opt_bp_spec_reduce =3D true; =20 static bool __initdata opt_ibpb_alt; =20 +/* Give each vCPU of an HVM domain its own per-domain area (vCPU-PT). */ +bool __ro_after_init opt_vcpu_pt_hvm; +bool __ro_after_init opt_vcpu_pt_hwdom; + static int __init cf_check parse_spec_ctrl(const char *s) { const char *ss; @@ -487,6 +491,80 @@ static int __init cf_check parse_pv_l1tf(const char *s) } custom_param("pv-l1tf", parse_pv_l1tf); =20 +static int __init cf_check parse_asi(const char *s) +{ + static bool __initdata warned; + const char *ss; + int val, rc =3D 0; + + if ( !warned ) + { + warned =3D true; + warning_add( + "Address Space Isolation is not functional, this option is\n" + "intended to be used only for development purposes.\n"); + } + + /* Interpret 'asi' alone in its positive boolean form. */ + if ( *s =3D=3D '\0' ) + opt_vcpu_pt_hwdom =3D opt_vcpu_pt_hvm =3D true; + + do { + ss =3D strchr(s, ','); + if ( !ss ) + ss =3D strchr(s, '\0'); + + val =3D parse_bool(s, ss); + switch ( val ) + { + case 0: + case 1: + opt_vcpu_pt_hwdom =3D opt_vcpu_pt_hvm =3D val; + break; + + default: + if ( (val =3D parse_boolean("pv", s, ss)) >=3D 0 ) + printk(XENLOG_WARNING + "No ASI feature applies to PV domains - ignoring 'a= si=3D%.*s' setting\n", + (int)(ss - s), s); + else if ( (val =3D parse_boolean("hvm", s, ss)) >=3D 0 ) + opt_vcpu_pt_hvm =3D val; + else if ( (val =3D parse_boolean("dom0", s, ss)) >=3D 0 ) + opt_vcpu_pt_hwdom =3D val; + else if ( (val =3D parse_boolean("vcpu-pt", s, ss)) !=3D -1 ) + { + switch ( val ) + { + case 1: + case 0: + opt_vcpu_pt_hwdom =3D opt_vcpu_pt_hvm =3D val; + break; + + case -2: + s +=3D strlen("vcpu-pt=3D"); + if ( (val =3D parse_boolean("hvm", s, ss)) >=3D 0 ) + { + opt_vcpu_pt_hvm =3D val; + break; + } + fallthrough; + default: + rc =3D -EINVAL; + break; + } + } + else if ( *s ) + rc =3D -EINVAL; + break; + } + + s =3D ss + 1; + } while ( *ss ); + + return rc; +} +custom_param("asi", parse_asi); + static void __init print_details(enum ind_thunk thunk) { unsigned int _7d0 =3D 0, _7d2 =3D 0, e8b =3D 0, e21a =3D 0, e21c =3D 0= , max =3D 0, tmp; @@ -681,6 +759,12 @@ static void __init print_details(enum ind_thunk thunk) opt_pv_l1tf_hwdom ? "enabled" : "disabled", opt_pv_l1tf_domu ? "enabled" : "disabled"); #endif + + printk(" ASI features for Dom0:%s\n", + opt_dom0_pvh && opt_vcpu_pt_hwdom ? " vCPU-PT" : " None"); + if ( IS_ENABLED(CONFIG_HVM) ) + printk(" ASI features for HVM VMs:%s\n", + opt_vcpu_pt_hvm ? " vCPU-PT" : " None"); } =20 static bool __init check_smt_enabled(void) @@ -1898,6 +1982,9 @@ void spec_ctrl_init_domain(struct domain *d) if ( pv ) d->arch.pv.xpti =3D is_hardware_domain(d) ? opt_xpti_hwdom : opt_xpti_domu; + + d->arch.asi.vcpu_pt =3D !pv && (is_hardware_domain(d) ? opt_vcpu_pt_hw= dom + : opt_vcpu_pt_hvm); } =20 void __init init_speculation_mitigations(void) --=20 2.56.0 From nobody Mon Nov 23 22:32:25 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=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1795123890; cv=none; d=zohomail.com; s=zohoarc; b=CNVlrRCDjrUAe9Zlud8FB64R+ZV3wUuwcFtl+Obqqd0fKP+Z2N87Wv6/GOxEn6o2QQe7afSj0oTA/YdcTg1ymShE2i8NIf7zjtzz5pOXLVivKcZnWI5oMUx799sflGl1lR30Uo9WUizqmzNT6iBgNB9H4+MNMhFaz4RhOtYK1Zs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795123890; h=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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=kemElQ7DAT1EHkT2gb6DePCFRqG9VLW6eR0f2GKZPiw=; b=VVVJmC3jExFqKcxd67P3n7mOyzeDat62PRw7grIyvZ1CbiBspXybXrScuiE+iz2dOA9BKkle1F8u1V63ZoVsgd1HaCM6YfupHdm2q9tr2TQrgIvMuLSO8KyoWuFmGyyS8D+VkIMsx9Vkz7wR5OaR8FizXxTpvsGZXYk5xwienQo= 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=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1795123890754383.11521681463296; Thu, 19 Nov 2026 13:31:30 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1497137.1695602 (Exim 4.92) (envelope-from ) id 1xU9ic-0000i3-Q3; Thu, 19 Nov 2026 21:30:58 +0000 Received: by outflank-mailman (output) from mailman id 1497137.1695602; Thu, 19 Nov 2026 21:30:58 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9ia-0000ZI-IK; Thu, 19 Nov 2026 21:30:56 +0000 Received: by outflank-mailman (input) for mailman id 1497137; Thu, 19 Nov 2026 21:30:20 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9hz-0001E0-Ql for xen-devel@lists.xenproject.org; Thu, 19 Nov 2026 21:30:19 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xU9hz-00000006tIN-0QNk; Thu, 19 Nov 2026 21:30:19 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xU9Ma-0000000CVBH-0JR6; Thu, 19 Nov 2026 21:08:12 +0000 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" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=kemElQ7DAT1EHkT2gb6DePCFRqG9VLW6eR0f2GKZPiw=; b=SbZ2IudkUUYSIOMTWvJJn8GHiR AMP1YaimzAkAbAyzPQJ6gUyEcfWRQS0suz1NIr/sClllnUWljyt92fAXEBivLRrNOvaU2cbYoveCZ WWvwrVTBPEygcWQU4TThr7PYp3q7HydIK3TajRKImqHJVIy/dDRGgi0G0p66/5FZsy7A=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie , Tim Deegan Subject: [PATCH v7 24/50] x86/mm: have init_xen_l4_slots() take a vCPU in place of the domain Date: Thu, 19 Nov 2026 21:07:15 +0000 Message-ID: <20261119-asi-hvm-v7-24-6551b6d5c879@xenproject.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> References: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1795123891862158500 Content-Type: text/plain; charset="utf-8" A later change gives the vCPUs of some domains per-domain areas of their own, and slot 260 of a table private to one vCPU must then hold that vCPU's L3. Eventually, all guest types may be able to support either per-domain L3s or per-vCPU L3s. HVM domains always have one root page table per vCPU; this does not change. So init_xen_l4_slots() needs the appropriate L3 at vCPU creation, either the domain-wide L3 or the per-vCPU L3. PV domains with a domain-wide L3 should behave as now: the Xen slots of guest root pagetables need the domain-wide L3 installed. But PV guests with per-vCPU page tables can never run directly on guest pagetables; they will always need some separate page table, which can be initialized with the per-vCPU L3. So, create two versions of init_xen_l4_slots(). The first is used in contexts where we may be initializing a non-guest root, and must have a vCPU L3 if per-vCPU L3s are in use; this takes a vCPU as an argument. The second is used for roots that all of a domain's vCPUs share (a PV guest's own L4s, and their shadows), which only need an L3 if domain-wide L3s are in use; this takes a domain as an argument. Both wrap an internal function, which takes a domain and an L3 pointer, and leaves the slot empty if this pointer is NULL. At the moment, both always pass on the domain-wide L3, but in the future this will change if the guest is using per-vCPU pagetables. Otherwise no functional change: slot 260 still gets the domain's per-domain L3. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Changes in v6: - Add init_xen_l4_slots_pv() form, which only takes a domain as an argument; promote_l4_table(), the PV dom0 builder and the shadows of PV guests' L4s use it, so promote_l4_table() no longer needs vCPU 0. Changes in v5: - promote_l4_table() checks for vCPU 0 itself, now that "x86/mm: refuse to promote an L4 while the domain has no per-domain L3" tests for the L3 instead. Changes in v4: - New in this version, split out of "x86/hvm: introduce per-vCPU L3 page-table": init_xen_l4_slots() takes a vCPU in place of the domain (Roger, Jan). - Reflow promote_l4_table()'s call to init_xen_l4_slots() (Jan). --- xen/arch/x86/include/asm/mm.h | 4 +++- xen/arch/x86/mm.c | 35 +++++++++++++++++++++++++++++----- xen/arch/x86/mm/hap/hap.c | 2 +- xen/arch/x86/mm/shadow/hvm.c | 2 +- xen/arch/x86/mm/shadow/multi.c | 5 +++-- xen/arch/x86/pv/dom0_build.c | 4 ++-- xen/arch/x86/pv/domain.c | 2 +- 7 files changed, 41 insertions(+), 13 deletions(-) diff --git a/xen/arch/x86/include/asm/mm.h b/xen/arch/x86/include/asm/mm.h index 1ef05dcd46..3fa70e420b 100644 --- a/xen/arch/x86/include/asm/mm.h +++ b/xen/arch/x86/include/asm/mm.h @@ -370,7 +370,9 @@ int devalidate_page(struct page_info *page, unsigned lo= ng type, =20 void init_xen_pae_l2_slots(l2_pgentry_t *l2t, const struct domain *d); void init_xen_l4_slots(l4_pgentry_t *l4t, mfn_t l4mfn, - const struct domain *d, mfn_t sl4mfn, bool ro_mpt); + const struct vcpu *v, mfn_t sl4mfn, bool ro_mpt); +void init_xen_l4_slots_pv(l4_pgentry_t *l4t, mfn_t l4mfn, + const struct domain *d, mfn_t sl4mfn, bool ro_mp= t); bool fill_ro_mpt(mfn_t mfn); void zap_ro_mpt(mfn_t mfn); =20 diff --git a/xen/arch/x86/mm.c b/xen/arch/x86/mm.c index 26bc2cc229..446732911b 100644 --- a/xen/arch/x86/mm.c +++ b/xen/arch/x86/mm.c @@ -1696,8 +1696,10 @@ static int promote_l3_table(struct page_info *page) * appropriate slots. Pagetables not shared with guests will gain the * extended directmap. */ -void init_xen_l4_slots(l4_pgentry_t *l4t, mfn_t l4mfn, - const struct domain *d, mfn_t sl4mfn, bool ro_mpt) +static void _init_xen_l4_slots(l4_pgentry_t *l4t, mfn_t l4mfn, + const struct domain *d, + const struct page_info *l3_pg, + mfn_t sl4mfn, bool ro_mpt) { /* * PV vcpus need a shortened directmap. HVM and Idle vcpus get the fu= ll @@ -1726,7 +1728,7 @@ void init_xen_l4_slots(l4_pgentry_t *l4t, mfn_t l4mfn, =20 /* Slot 260: Per-domain mappings. */ l4t[l4_table_offset(PERDOMAIN_VIRT_START)] =3D - l4e_from_page(d->arch.perdomain_l3_pg, __PAGE_HYPERVISOR_RW); + l3_pg ? l4e_from_page(l3_pg, __PAGE_HYPERVISOR_RW) : l4e_empty(); =20 /* Slot 4: Per-domain mappings mirror. */ BUILD_BUG_ON(IS_ENABLED(CONFIG_PV32) && @@ -1773,6 +1775,29 @@ void init_xen_l4_slots(l4_pgentry_t *l4t, mfn_t l4mf= n, } } =20 +/* + * v is the vCPU the table is private to. For shared root pagetables, use + * init_xen_l4_slots_pv(). + */ +void init_xen_l4_slots(l4_pgentry_t *l4t, mfn_t l4mfn, + const struct vcpu *v, mfn_t sl4mfn, bool ro_mpt) +{ + const struct domain *d =3D v->domain; + + _init_xen_l4_slots(l4t, l4mfn, d, d->arch.perdomain_l3_pg, sl4mfn, ro_= mpt); +} + +/* + * This is a version of init_xen_l4_slots for shared root pagetables; + * it only propagates domain-wide perdomain L3s, and so only needs a + * domain pointer. + */ +void init_xen_l4_slots_pv(l4_pgentry_t *l4t, mfn_t l4mfn, + const struct domain *d, mfn_t sl4mfn, bool ro_mp= t) +{ + _init_xen_l4_slots(l4t, l4mfn, d, d->arch.perdomain_l3_pg, sl4mfn, ro_= mpt); +} + bool fill_ro_mpt(mfn_t mfn) { l4_pgentry_t *l4tab =3D map_domain_page(mfn); @@ -1881,8 +1906,8 @@ static int promote_l4_table(struct page_info *page) =20 if ( !rc ) { - init_xen_l4_slots(pl4e, l4mfn, - d, INVALID_MFN, VM_ASSIST(d, m2p_strict)); + init_xen_l4_slots_pv(pl4e, l4mfn, d, INVALID_MFN, + VM_ASSIST(d, m2p_strict)); atomic_inc(&d->arch.pv.nr_l4_pages); } unmap_domain_page(pl4e); diff --git a/xen/arch/x86/mm/hap/hap.c b/xen/arch/x86/mm/hap/hap.c index 0ede4181a0..aba4f77df9 100644 --- a/xen/arch/x86/mm/hap/hap.c +++ b/xen/arch/x86/mm/hap/hap.c @@ -407,7 +407,7 @@ static mfn_t hap_make_monitor_table(struct vcpu *v) m4mfn =3D page_to_mfn(pg); l4e =3D map_domain_page(m4mfn); =20 - init_xen_l4_slots(l4e, m4mfn, d, INVALID_MFN, false); + init_xen_l4_slots(l4e, m4mfn, v, INVALID_MFN, false); unmap_domain_page(l4e); =20 return m4mfn; diff --git a/xen/arch/x86/mm/shadow/hvm.c b/xen/arch/x86/mm/shadow/hvm.c index 6c554ef29d..b1c3d5e637 100644 --- a/xen/arch/x86/mm/shadow/hvm.c +++ b/xen/arch/x86/mm/shadow/hvm.c @@ -760,7 +760,7 @@ mfn_t sh_make_monitor_table(const struct vcpu *v, unsig= ned int shadow_levels) * shadow-linear mapping will either be inserted below when creating * lower level monitor tables, or later in sh_update_cr3(). */ - init_xen_l4_slots(l4e, m4mfn, d, INVALID_MFN, false); + init_xen_l4_slots(l4e, m4mfn, v, INVALID_MFN, false); =20 if ( shadow_levels < 4 ) { diff --git a/xen/arch/x86/mm/shadow/multi.c b/xen/arch/x86/mm/shadow/multi.c index 7f72b4b7b2..1831091b95 100644 --- a/xen/arch/x86/mm/shadow/multi.c +++ b/xen/arch/x86/mm/shadow/multi.c @@ -974,8 +974,9 @@ sh_make_shadow(struct vcpu *v, mfn_t gmfn, u32 shadow_t= ype) =20 BUILD_BUG_ON(sizeof(l4_pgentry_t) !=3D sizeof(shadow_l4e_t)); =20 - init_xen_l4_slots(l4t, gmfn, d, smfn, (!is_pv_32bit_domain(d) = && - VM_ASSIST(d, m2p_strict= ))); + init_xen_l4_slots_pv(l4t, gmfn, d, smfn, + (!is_pv_32bit_domain(d) && + VM_ASSIST(d, m2p_strict))); unmap_domain_page(l4t); } break; diff --git a/xen/arch/x86/pv/dom0_build.c b/xen/arch/x86/pv/dom0_build.c index 9aa815ef28..d11e38fd20 100644 --- a/xen/arch/x86/pv/dom0_build.c +++ b/xen/arch/x86/pv/dom0_build.c @@ -724,8 +724,8 @@ static int __init dom0_construct(const struct boot_doma= in *bd) maddr_to_page(mpt_alloc)->u.inuse.type_info =3D PGT_l4_page_table; l4start =3D l4tab =3D __va(mpt_alloc); mpt_alloc +=3D PAGE_SIZE; clear_page(l4tab); - init_xen_l4_slots(l4tab, _mfn(virt_to_mfn(l4start)), - d, INVALID_MFN, true); + init_xen_l4_slots_pv(l4tab, _mfn(virt_to_mfn(l4start)), + d, INVALID_MFN, true); v->arch.guest_table =3D pagetable_from_paddr(__pa(l4start)); } else diff --git a/xen/arch/x86/pv/domain.c b/xen/arch/x86/pv/domain.c index f3c711b85b..f139d8f83c 100644 --- a/xen/arch/x86/pv/domain.c +++ b/xen/arch/x86/pv/domain.c @@ -127,7 +127,7 @@ static int setup_compat_l4(struct vcpu *v) mfn =3D page_to_mfn(pg); l4tab =3D map_domain_page(mfn); clear_page(l4tab); - init_xen_l4_slots(l4tab, mfn, v->domain, INVALID_MFN, false); + init_xen_l4_slots(l4tab, mfn, v, INVALID_MFN, false); unmap_domain_page(l4tab); =20 /* This page needs to look like a pagetable so that it can be shadowed= */ --=20 2.56.0 From nobody Mon Nov 23 22:32:25 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=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1795123846; cv=none; d=zohomail.com; s=zohoarc; b=asVlJBKxeAgjEJGwZP6r6YQ1nTCaKoD0CzU5AEE87ElQkKDko5VARBwEqAuKNXiTK+g0SYdTmlTU7ntyFU6HUf0NoYWBWNMrl/8/0L84Mz8xI9HofEcawcczvABtHCNVepFPTW5baIysXoL3oi8OiH7A6mMQm/Vk7Q7LxMQxZLE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795123846; 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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=nrwbDtXKt+31nm2TnyNnVLPFupNYjy9rQsu/1DwGk0k=; b=WZSCk9p298xC+TIbW5qC9jyXnN8MVlL68YGpLNMQlimydxyOhhe5RjQDQgqLkQNK2KM3SVTBIMZ21HouxDDeHbdj3w60OmCH0tW994HwAeKNRsgmOgbZHm9mUqKaApn+24k3ZQ/hW6bwTJYtre2Y1abb2A9aRwjFNPrIFfD44f4= 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=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 179512384653632.444458487333804; Thu, 19 Nov 2026 13:30:46 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1497116.1695423 (Exim 4.92) (envelope-from ) id 1xU9i3-0001sR-9O; Thu, 19 Nov 2026 21:30:23 +0000 Received: by outflank-mailman (output) from mailman id 1497116.1695423; Thu, 19 Nov 2026 21:30:23 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9i2-0001pf-MC; Thu, 19 Nov 2026 21:30:22 +0000 Received: by outflank-mailman (input) for mailman id 1497116; Thu, 19 Nov 2026 21:30:19 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9hz-0001CK-5k for xen-devel@lists.xenproject.org; Thu, 19 Nov 2026 21:30:19 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xU9hy-00000006tGt-2y6e; Thu, 19 Nov 2026 21:30:19 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xU9Mb-0000000CVBH-0TiD; Thu, 19 Nov 2026 21:08:13 +0000 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" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:Content-Type: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=nrwbDtXKt+31nm2TnyNnVLPFupNYjy9rQsu/1DwGk0k=; b=xUcokWrc00QBw7bsoOGB48jGEL S18+JUhfg5CkJGxSPSxsajTK02iJPTuyH3BkxwlON9sV6FfmP5BTc8N/YDYUeyu05eDfq9oZdZJrc y4AHJrnIv3zSXA5ykqwagy6qx3MyNgegQ/Oa8ZQT4+oUjzjz6vJu6YOaOHKmHo5ciVAc=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie Subject: [PATCH v7 25/50] x86/hvm: introduce per-vCPU L3 page-table Date: Thu, 19 Nov 2026 21:07:16 +0000 Message-ID: <20261119-asi-hvm-v7-25-6551b6d5c879@xenproject.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> References: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1795123847688158500 From: Roger Pau Monn=C3=A9 Currently all of a domain's vCPUs share its per-domain area: one L3, referenced from every root page-table associated with the domain, so every mapping in it is visible to every vCPU of the domain. Much of the area is already laid out in per-vCPU slices (each vCPU's COMPAT_ARG_XLAT pages, its state window and its map_domain_pages() slots, and for PV each vCPU's GDT/LDT window), but the visibility is domain-wide. The per-vCPU state Xen keeps behind the state window -- the VMCB, the VMX guest MSR load/save list, the register frame, FPU/XSAVE state -- is therefore mapped in the page-tables of every vCPU of the domain, not only in its owner's. For HVM domains, allow each vCPU a per-domain area of its own instead ("vCPU-PT"). HVM monitor tables are already per-vCPU, so this needs nothing at context switch: every monitor table carries its vCPU's own L3 from creation, and the per-vCPU mappings, the state window's included, are isolated from other running vCPUs at once. We will later build on this, adding a per-vCPU mapcache. Add pervcpu_l3_pg to the arch_vcpu struct, to correspond to the perdomain_l3_pg in the domain struct. (We retain both so that vCPU-PT can be chosen domain by domain; PV domains always use the domain-wide L3.) Modify arch_domain_create() to only allocate a per-domain L3 if vcpu_pt is not enabled for the domain; modify arch_vcpu_create() to allocate a per-vCPU L3 if it is. In {create,populate,destroy}_perdomain_mapping(), if d->arch.asi.vcpu_pt, use pervcpu_l3_pg as the per-domain L3; otherwise, use perdomain_l3_pg. Introduce a helper, perdomain_l3(), to consistently choose the correct one. Introduce free_pervcpu_mappings() to free this tree, called from arch_vcpu_destroy() on normal teardown. Since the vcpu structure holds the only reference to pervcpu_l3_pg, arch_vcpu_create() must also call it on its error paths: nothing else records the allocation once the vcpu struct is torn down. The domain-wide free_perdomain_mappings() keeps covering non-vCPU-PT domains. init_xen_l4_slots() is given the vCPU that a table is private to. Have it install that vCPU's perdomain_l3() in slot 260. Signed-off-by: Roger Pau Monn=C3=A9 Assisted-by: Claude Code:claude-fable-5, Claude Code:claude-opus-4-8, Claud= e Code:claude-fable-5-1, Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Changes in v7: - Rebased over the vCPU state window, which now comes earlier in the series: populate_perdomain_mapping() takes its L3 from perdomain_l3() too, the comments of unmap_vcpu_state() say what per-vCPU page-tables change, and the message no longer promises the window. Changes in v6: - Port to new explicit L3 allocation regime. Changes in v5: - init_xen_l4_slots()'s BUG_ON() follows slot 260 to perdomain_l3(). - Message: all of a domain's vCPUs share its per-domain area; the mapcache is not among the per-vCPU slices; the mode is "vCPU-PT". The comment above init_xen_l4_slots() names perdomain_l3(v) and the domain-wide L3. Changes in v4: - Rebase over earlier changes - d->arch.vcpu_pt is now d->arch.asi.vcpu_pt. - init_xen_l4_slots() taking a vCPU in place of the domain, and promote_l4_table() failing while the domain has none, are now patches of their own (Roger, Jan). - Drop the fail_early label: every cleanup step after fail: is safe before its setup has run (Roger). - free_pervcpu_mappings(): clear the pointer, then free, as XFREE() does (Roger). - Likewise in free_perdomain_mappings(). Changes in v3: - HVM only: PV domains keep the domain-wide area, so the PV parts go (the single-vCPU limit, the shadow refusals, the XPTI slot install). init_xen_l4_slots() takes the vCPU as an extra, optional argument rather than in place of the domain, so PV guest L4s, which no vCPU owns, are promoted exactly as before. - Retitle (was: "x86/mm: introduce per-vCPU L3 page-table"). - free_compat_arg_xlat()'s comment names free_pervcpu_mappings() as the teardown for a vCPU-PT domain. Changes in v2: - Added to the series Changes since the previously posted version: - Expand the commit message with the motivation and the design rationale. - Keep domain-wide freeing intact and introduce a vCPU-scoped free_pervcpu_mappings() instead of re-scoping free_perdomain_mappings(); fix the arch_vcpu_create() error-path leaks of a partially built per-vCPU hierarchy. - Add a perdomain_l3() helper for the root selection, rather than open-coding the vcpu_pt choice (and testing both root pointers) at each site. --- xen/arch/x86/domain.c | 17 +++++-- xen/arch/x86/include/asm/domain.h | 4 ++ xen/arch/x86/include/asm/mm.h | 2 + xen/arch/x86/mm.c | 73 +++++++++++++++++++++++-------- 4 files changed, 74 insertions(+), 22 deletions(-) diff --git a/xen/arch/x86/domain.c b/xen/arch/x86/domain.c index 6e33d56588..1df6763530 100644 --- a/xen/arch/x86/domain.c +++ b/xen/arch/x86/domain.c @@ -576,9 +576,16 @@ int arch_vcpu_create(struct vcpu *v) =20 v->arch.flags =3D TF_kernel_mode; =20 + if ( d->arch.asi.vcpu_pt && + (v->arch.pervcpu_l3_pg =3D allocate_perdomain_l3(d)) =3D=3D NULL ) + { + rc =3D -ENOMEM; + goto fail; + } + rc =3D mapcache_vcpu_init(v); if ( rc ) - return rc; + goto fail; =20 if ( !is_idle_domain(d) ) { @@ -601,7 +608,7 @@ int arch_vcpu_create(struct vcpu *v) paging_vcpu_init(v); =20 if ( (rc =3D vcpu_init_fpu(v)) !=3D 0 ) - return rc; + goto fail; =20 vmce_init_vcpu(v); =20 @@ -654,6 +661,7 @@ int arch_vcpu_create(struct vcpu *v) vcpu_regs_free(v); xfree(v->arch.msrs); v->arch.msrs =3D NULL; + free_pervcpu_mappings(v); =20 return rc; } @@ -676,6 +684,8 @@ void arch_vcpu_destroy(struct vcpu *v) ASSERT_UNREACHABLE(); =20 vcpu_regs_free(v); + + free_pervcpu_mappings(v); } =20 int arch_sanitise_domain_config(const struct xen_domctl_createdomain *conf= ig) @@ -989,7 +999,8 @@ int arch_domain_create(struct domain *d, =20 psr_domain_init(d); =20 - if ( (d->arch.perdomain_l3_pg =3D allocate_perdomain_l3(d)) =3D=3D NUL= L ) + if ( !d->arch.asi.vcpu_pt && + (d->arch.perdomain_l3_pg =3D allocate_perdomain_l3(d)) =3D=3D NUL= L ) { rc =3D -ENOMEM; goto fail; diff --git a/xen/arch/x86/include/asm/domain.h b/xen/arch/x86/include/asm/d= omain.h index 0ec0db0b91..17903d62cb 100644 --- a/xen/arch/x86/include/asm/domain.h +++ b/xen/arch/x86/include/asm/domain.h @@ -329,6 +329,7 @@ struct monitor_write_data { =20 struct arch_domain { + /* Domain-wide L3 for the per-domain slot; NULL in a vCPU-PT domain. */ struct page_info *perdomain_l3_pg; =20 /* I/O-port admin-specified access capabilities. */ @@ -701,6 +702,9 @@ struct arch_vcpu /* Which of the vCPU's map_domain_pages() slots are in use. */ unsigned long multipage_inuse; =20 + /* Per-vCPU L3 for the per-domain slot; NULL in a non-vCPU-PT domain. = */ + struct page_info *pervcpu_l3_pg; + struct { bool next_interrupt_enabled; } monitor; diff --git a/xen/arch/x86/include/asm/mm.h b/xen/arch/x86/include/asm/mm.h index 3fa70e420b..c66969c093 100644 --- a/xen/arch/x86/include/asm/mm.h +++ b/xen/arch/x86/include/asm/mm.h @@ -369,6 +369,7 @@ int devalidate_page(struct page_info *page, unsigned lo= ng type, int preemptible); =20 void init_xen_pae_l2_slots(l2_pgentry_t *l2t, const struct domain *d); +struct page_info *perdomain_l3(const struct vcpu *v); void init_xen_l4_slots(l4_pgentry_t *l4t, mfn_t l4mfn, const struct vcpu *v, mfn_t sl4mfn, bool ro_mpt); void init_xen_l4_slots_pv(l4_pgentry_t *l4t, mfn_t l4mfn, @@ -620,6 +621,7 @@ void destroy_perdomain_mapping(const struct vcpu *v, un= signed long va, void unmap_vcpu_state(const struct vcpu *v, unsigned int idx, unsigned int= nr); void unmap_vcpu_state_page(const void *va); void free_perdomain_mappings(struct domain *d); +void free_pervcpu_mappings(struct vcpu *v); =20 void __iomem *ioremap_wc(paddr_t pa, size_t len); =20 diff --git a/xen/arch/x86/mm.c b/xen/arch/x86/mm.c index 446732911b..d1fc57cf82 100644 --- a/xen/arch/x86/mm.c +++ b/xen/arch/x86/mm.c @@ -1685,6 +1685,18 @@ static int promote_l3_table(struct page_info *page) } #endif /* CONFIG_PV */ =20 +/* + * The root of the per-domain area in use by @v: the vCPU's own L3 for a + * vCPU-PT domain, the domain-wide one otherwise. + */ +struct page_info *perdomain_l3(const struct vcpu *v) +{ + const struct domain *d =3D v->domain; + + return d->arch.asi.vcpu_pt ? v->arch.pervcpu_l3_pg + : d->arch.perdomain_l3_pg; +} + /* * Fill an L4 with Xen entries. * @@ -1783,8 +1795,11 @@ void init_xen_l4_slots(l4_pgentry_t *l4t, mfn_t l4mf= n, const struct vcpu *v, mfn_t sl4mfn, bool ro_mpt) { const struct domain *d =3D v->domain; + struct page_info *l3_pg =3D perdomain_l3(v); =20 - _init_xen_l4_slots(l4t, l4mfn, d, d->arch.perdomain_l3_pg, sl4mfn, ro_= mpt); + BUG_ON(!l3_pg); + + _init_xen_l4_slots(l4t, l4mfn, d, l3_pg, sl4mfn, ro_mpt); } =20 /* @@ -6275,7 +6290,7 @@ int create_perdomain_mapping(struct vcpu *v, unsigned= long va, struct page_info **ppg) { struct domain *d =3D v->domain; - struct page_info *pg; + struct page_info *pg, *l3_pg =3D perdomain_l3(v); l3_pgentry_t *l3tab; l2_pgentry_t *l2tab; l1_pgentry_t *l1tab; @@ -6284,12 +6299,12 @@ int create_perdomain_mapping(struct vcpu *v, unsign= ed long va, ASSERT(va >=3D PERDOMAIN_VIRT_START && va < PERDOMAIN_VIRT_SLOT(PERDOMAIN_SLOTS)); =20 - BUG_ON(!d->arch.perdomain_l3_pg); + BUG_ON(!l3_pg); =20 if ( !nr ) return 0; =20 - l3tab =3D __map_domain_page(d->arch.perdomain_l3_pg); + l3tab =3D __map_domain_page(l3_pg); =20 ASSERT(!l3_table_offset(va ^ (va + nr * PAGE_SIZE - 1))); =20 @@ -6396,7 +6411,7 @@ void populate_perdomain_mapping(const struct vcpu *v,= unsigned long va, const mfn_t *mfn, unsigned int nr, unsigned int flags) { - struct page_info *l3_pg =3D v->domain->arch.perdomain_l3_pg; + struct page_info *l3_pg =3D perdomain_l3(v); const l3_pgentry_t *l3tab; const l2_pgentry_t *l2tab; l1_pgentry_t *l1tab =3D NULL; @@ -6448,16 +6463,16 @@ void destroy_perdomain_mapping(const struct vcpu *v= , unsigned long va, unsigned int nr) { const l3_pgentry_t *l3tab, *pl3e; - const struct domain *d =3D v->domain; + struct page_info *l3_pg =3D perdomain_l3(v); =20 ASSERT(va >=3D PERDOMAIN_VIRT_START && va < PERDOMAIN_VIRT_SLOT(PERDOMAIN_SLOTS)); ASSERT(!nr || !l3_table_offset(va ^ (va + nr * PAGE_SIZE - 1))); =20 - if ( !d->arch.perdomain_l3_pg ) + if ( !l3_pg ) return; =20 - l3tab =3D __map_domain_page(d->arch.perdomain_l3_pg); + l3tab =3D __map_domain_page(l3_pg); pl3e =3D l3tab + l3_table_offset(va); =20 if ( l3e_get_flags(*pl3e) & _PAGE_PRESENT ) @@ -6500,14 +6515,18 @@ void destroy_perdomain_mapping(const struct vcpu *v= , unsigned long va, * Take @nr slots of @v's state window, from @idx, out of the per-domain * area, before the caller frees the pages behind them: the pages are the * caller's own, and must not stay mapped once freed. The window's - * translations are usable only on CPUs whose page-tables carry it: those = of - * any of the domain's vCPUs, all in the domain's dirty mask. + * translations are usable only on CPUs whose page-tables carry it, all in + * the domain's dirty mask: with per-vCPU page-tables, the CPU running v or + * holding it lazily; otherwise, those of any of the domain's vCPUs. */ void unmap_vcpu_state(const struct vcpu *v, unsigned int idx, unsigned int= nr) { const struct domain *d =3D v->domain; =20 - /* Beyond the stride is the next vCPU's window. */ + /* + * Beyond the stride is the next vCPU's window, where the domain's vCP= Us + * share a per-domain area. + */ BUG_ON(idx >=3D VCPU_STATE_NR_PAGES || nr > VCPU_STATE_NR_PAGES - idx); =20 destroy_perdomain_mapping(v, VCPU_STATE_ADDR(v, idx), nr); @@ -6538,16 +6557,11 @@ void unmap_vcpu_state_page(const void *va) } } =20 -void free_perdomain_mappings(struct domain *d) +static void free_perdomain_l3(struct page_info *l3pg) { - l3_pgentry_t *l3tab; + l3_pgentry_t *l3tab =3D __map_domain_page(l3pg); unsigned int i; =20 - if ( !d->arch.perdomain_l3_pg ) - return; - - l3tab =3D __map_domain_page(d->arch.perdomain_l3_pg); - for ( i =3D 0; i < PERDOMAIN_SLOTS; ++i) if ( l3e_get_flags(l3tab[i]) & _PAGE_PRESENT ) { @@ -6583,8 +6597,29 @@ void free_perdomain_mappings(struct domain *d) } =20 unmap_domain_page(l3tab); - free_domheap_page(d->arch.perdomain_l3_pg); + free_domheap_page(l3pg); +} + +void free_perdomain_mappings(struct domain *d) +{ + struct page_info *l3pg =3D d->arch.perdomain_l3_pg; + + if ( !l3pg ) + return; + d->arch.perdomain_l3_pg =3D NULL; + free_perdomain_l3(l3pg); +} + +void free_pervcpu_mappings(struct vcpu *v) +{ + struct page_info *l3pg =3D v->arch.pervcpu_l3_pg; + + if ( !l3pg ) + return; + + v->arch.pervcpu_l3_pg =3D NULL; + free_perdomain_l3(l3pg); } =20 static void write_sss_token(unsigned long *ptr) --=20 2.56.0 From nobody Mon Nov 23 22:32:25 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=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1795123905; cv=none; d=zohomail.com; s=zohoarc; b=lUOJ1E7UN5SW3Nx/ktX1L1midlLfOe6IQ9qeM1q2HLCE7AKuuACkSz0lOCCIw2zns+TIYPgOr6p2TvVUsbQu2yvSLLfEoJminR6R2j1MortoWYNyvIzy8sCLl/tVsJoGjD6vhnH0NFM8gy61Jwx0cT2AVMC9v9O2qgJP1TX+J1Y= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795123905; h=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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=Ry5LFgpqOHWglQrJ8PEPb024J0pG+e1oELER+m/DZAo=; b=AeTg50fs08RgOz58m3atcTsG4v/nWUJ9IVnx23cUXC1BJCwIjS8WvSK95hc+gb9tpTTXt2/YfSTzm/0JxV+lFpDd+pqVfivwOR+dktUsOsKph0paiQlIg4gCG6nqHNZ+BRVKrz2bv6QJM+aD/+xWnhoJ2rr6JMbxYnZk6mOR1pw= 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=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1795123905935942.3799152635521; Thu, 19 Nov 2026 13:31:45 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1497147.1695683 (Exim 4.92) (envelope-from ) id 1xU9ix-00058u-77; Thu, 19 Nov 2026 21:31:19 +0000 Received: by outflank-mailman (output) from mailman id 1497147.1695683; Thu, 19 Nov 2026 21:31:18 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9iv-00051S-Mq; Thu, 19 Nov 2026 21:31:17 +0000 Received: by outflank-mailman (input) for mailman id 1497147; Thu, 19 Nov 2026 21:30:20 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9i0-0001FA-AW for xen-devel@lists.xenproject.org; Thu, 19 Nov 2026 21:30:20 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xU9hz-00000006tKa-3ETf; Thu, 19 Nov 2026 21:30:20 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xU9Mc-0000000CVBH-091k; Thu, 19 Nov 2026 21:08:14 +0000 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" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=Ry5LFgpqOHWglQrJ8PEPb024J0pG+e1oELER+m/DZAo=; b=D2PNnJ6agbEp3RETAzAUVeXCN4 7u0a796C4EUc4WmR3eYp06PQ8eu8mZMsdRIV6/zJgbijEQAoqXAMndLrbUC+DacqBAPy6FE3qEPn+ RYPLxZdvagHEbRwEjea9KTgqDCZ6GWWbXrNk0OJ07TdZAe4X0xwD5Rtub/qUKBPwKGt4=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie Subject: [PATCH v7 26/50] x86/domain_page: drop redundant is_pv_vcpu() checks Date: Thu, 19 Nov 2026 21:07:17 +0000 Message-ID: <20261119-asi-hvm-v7-26-6551b6d5c879@xenproject.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> References: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1795123907926158500 Content-Type: text/plain; charset="utf-8" The is_pv_vcpu() checks after mapcache_current_vcpu() never fail: they have been redundant since 622c9a5ba95d ("x86/mm: accurately track which vCPU page-tables are loaded") made mapcache_current_vcpu() do the check itself. mapcache_current_vcpu() is now guaranteed to return a PV vCPU with a caching structure (which is what is needed for the subsequent dcache->inuse check): either a scheduled PV vCPU, or this CPU's idle vCPU (possibly after finishing a lazy context switch). Drop the checks; their speculation barriers (evaluate_nospec()) go too. mapcache_current_vcpu() returns NULL if this CPU's pgtable_vcpu is not yet set (early boot) or is not a PV vCPU. Otherwise it reads pgtable_vcpu again and returns that. In between, pgtable_vcpu changes only when a lazy context switch from a PV vCPU is completed, by mapcache_current_vcpu() itself (sync_local_execstate()) or first by an interrupt such as a FLUSH_VCPU_STATE IPI; completing it loads the idle vCPU's page-tables and sets pgtable_vcpu to the idle vCPU. No other writer of pgtable_vcpu runs in an interrupt that returns to the code it interrupted. During an EFI runtime call pgtable_vcpu is the idle vCPU: efi_rs_enter() completes any lazy switch, then loads EFI's page-tables, which carry the directmap. So the vCPU returned is either the PV vCPU checked or this CPU's idle vCPU. The idle vCPU passes is_pv_vcpu(), as the idle domain is created without XEN_DOMCTL_CDF_hvm, but its mapcache is never set up: domain_create() returns for the idle domain before calling arch_domain_create(), which is what calls mapcache_domain_init(). With its dcache->inuse NULL, map_domain_page() uses the directmap, and unmap_domain_page()'s ASSERT() on dcache->inuse still catches a mapcache address unmapped with no mapcache in use. In builds without CONFIG_PV, is_pv_vcpu() is always false and mapcache_current_vcpu() always returns NULL. No functional change. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Changes in v4: - New in this version, split out of "x86/domain_page: prepare the mapcache for per-vCPU accounting", with a justification of its own. NB I've included Claude's complete analysis in the commit message. I've reworded the commit message so that the middle two paragraphs can be dropped, or reworded more briefly on commit. --- xen/arch/x86/domain_page.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xen/arch/x86/domain_page.c b/xen/arch/x86/domain_page.c index dce66beccd..5e804fec92 100644 --- a/xen/arch/x86/domain_page.c +++ b/xen/arch/x86/domain_page.c @@ -88,7 +88,7 @@ void *map_domain_page(mfn_t mfn) #endif =20 v =3D mapcache_current_vcpu(); - if ( !v || !is_pv_vcpu(v) ) + if ( !v ) return mfn_to_virt(mfn_x(mfn)); =20 dcache =3D &v->domain->arch.pv.mapcache; @@ -193,7 +193,7 @@ void unmap_domain_page(const void *ptr) ASSERT(va >=3D MAPCACHE_VIRT_START && va < MAPCACHE_VIRT_END); =20 v =3D mapcache_current_vcpu(); - ASSERT(v && is_pv_vcpu(v)); + ASSERT(v); =20 dcache =3D &v->domain->arch.pv.mapcache; ASSERT(dcache->inuse); --=20 2.56.0 From nobody Mon Nov 23 22:32:25 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=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1795123893; cv=none; d=zohomail.com; s=zohoarc; b=Rn93CgzDpfK56gU7HvTmPZby61J7ChSecJ9oHN9kslyAH/2FjcWAz3nsE/s2od1JtW6pkvXh3Hrtq6UilqsDjY9xvfpRzzHAEzIQq8IXebwcWDFAvz2RxGg4VVj4tC1S5REBOCtBgxls3//kwp3A6dZsZo0bMisDIbMPG1UMubU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795123893; 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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=itWkUG4hhOI8hhkiui6XRwFzAv2sK67vP9HJVJzUsfY=; b=Qj5Yr0YCT0qNj7c3XuNpScxtom27EYunAtZ7NCkOWHX/fdW0c84LmxrDFrJpEv6gsuuWsU63ARnYKefX5AdJOFC8yrGvCIm2NJwW4rJuz30g4Lg2NEMS3xT4azdg8wtT4mJrl4x61hAOyDb8mRWynqamhiyBmEmgcvusJL0A94U= 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=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1795123893537863.2951091044333; Thu, 19 Nov 2026 13:31:33 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1497144.1695663 (Exim 4.92) (envelope-from ) id 1xU9iq-0003m0-Rz; Thu, 19 Nov 2026 21:31:12 +0000 Received: by outflank-mailman (output) from mailman id 1497144.1695663; Thu, 19 Nov 2026 21:31:12 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9io-0003ap-VB; Thu, 19 Nov 2026 21:31:10 +0000 Received: by outflank-mailman (input) for mailman id 1497144; Thu, 19 Nov 2026 21:30:20 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9i0-0001Ef-4T for xen-devel@lists.xenproject.org; Thu, 19 Nov 2026 21:30:20 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xU9hz-00000006tKR-371f; Thu, 19 Nov 2026 21:30:20 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xU9Md-0000000CVBH-0LFb; Thu, 19 Nov 2026 21:08:15 +0000 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" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:Content-Type: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=itWkUG4hhOI8hhkiui6XRwFzAv2sK67vP9HJVJzUsfY=; b=sLYWMbb+NeirKZODLV6J3fvkDx wzo4rKmQcwLQELMFYGz0s3za9drFbNdRrcDZ2RG2nqM/n++Dbm5J04No4gzQsZy1DqxiLTofxn3a+ FkjnnS/3GZ1QNuotZrnjzR0rZTWeMrAj/QOoPtghF/h/wrGM9+UU+ofvl3uYTGlXOTcU=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie Subject: [PATCH v7 27/50] x86/domain_page: prepare the mapcache for per-vCPU accounting Date: Thu, 19 Nov 2026 21:07:18 +0000 Message-ID: <20261119-asi-hvm-v7-27-6551b6d5c879@xenproject.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> References: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1795123893825158500 From: Roger Pau Monn=C3=A9 Currently, only PV domains have a mapcache, which is a domain-wide one. In preparation for enabling per-vCPU mapcaches for HVM domains, split the accounting out into a struct mapcache, embedded in struct mapcache_domain. Introduce has_mapcache(), for whether a vCPU has a mapcache, and vcpu_mapcache(), which returns the accounting for a vCPU's mapcache and, in *dcache, the domain-wide mapcache whose lock and TLB epoch the caller has to take into account. For now has_mapcache() is is_pv_vcpu(), and vcpu_mapcache() always returns the domain's accounting. Move map_domain_page()'s handling of the domain-wide lock and TLB epoch into three helpers: mapcache_enter() takes the lock and catches up with a wrap another vCPU caused, mapcache_new_epoch() starts a new epoch after a wrap, and mapcache_exit() drops the lock. A per-vCPU mapcache can then skip them without map_domain_page() being re-indented. struct mapcache_domain keeps its size (40 bytes, x86_64 debug build), and so do the structures containing it. No functional change. Signed-off-by: Roger Pau Monn=C3=A9 Assisted-by: Claude Code:claude-fable-5, Claude Code:claude-opus-4-8, Claud= e Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Changes in v7: - Rebased over map_domain_pages(), which now comes earlier in the series: has_mapcache() goes above pgtable_current_vcpu(), and mapcache_vcpu_init() tests it after setting up the vCPU's map_domain_pages() slots. Changes in v4: - Reword has_mapcache()'s comment (Roger). - Move the comment on the domain-wide lock and TLB epoch into the function body (Roger). - Rename mapcache_lock() / mapcache_unlock() to mapcache_enter() / mapcache_exit() (Roger). - mapcache_new_epoch(): assert that the domain-wide lock is held (Roger). - mapcache_vcpu_init(): drop the comment on the early return for a disabled domain-wide mapcache (Jan). - mapcache_vcpu_init(): initialise ents and nr at their declarations, as before (Jan). - Dropping the redundant is_pv_vcpu() checks in map_domain_page() and unmap_domain_page() is now a patch of its own (Jan). Changes in v3: - New in this version, split out of "x86/hvm: introduce per-vCPU mapcache for vCPU-PT domains" for review. --- xen/arch/x86/domain_page.c | 137 ++++++++++++++++++++---------- xen/arch/x86/include/asm/domain.h | 19 +++-- 2 files changed, 102 insertions(+), 54 deletions(-) diff --git a/xen/arch/x86/domain_page.c b/xen/arch/x86/domain_page.c index 5e804fec92..13b50d89e5 100644 --- a/xen/arch/x86/domain_page.c +++ b/xen/arch/x86/domain_page.c @@ -19,6 +19,12 @@ #include #include =20 +/* Whether the vCPU supports mapcache usage, might not be in use. */ +static bool has_mapcache(const struct vcpu *v) +{ + return is_pv_vcpu(v); +} + /* * The vCPU whose page tables are loaded here, and stay loaded until the * caller is done with what it maps in them. @@ -57,17 +63,59 @@ static inline struct vcpu *mapcache_current_vcpu(void) { const struct vcpu *v =3D this_cpu(pgtable_vcpu); =20 - /* - * During early boot pgtable_vcpu is not set, callers must handle NULL. - * Non-PV domains don't have a mapcache, the directmap covers all phys= ical - * address space. - */ - if ( !v || !is_pv_vcpu(v) ) + /* During early boot pgtable_vcpu is not set, callers must handle NULL= . */ + if ( !v || !has_mapcache(v) ) return NULL; =20 return pgtable_current_vcpu(); } =20 +/* + * The accounting for v's mapcache: the domain's, with *dcache set so the + * caller takes the domain-wide lock and TLB-epoch machinery into account. + */ +static struct mapcache *vcpu_mapcache(struct vcpu *v, + struct mapcache_domain **dcache) +{ + struct domain *d =3D v->domain; + + *dcache =3D &d->arch.pv.mapcache; + return &(*dcache)->cache; +} + +static void mapcache_enter(struct mapcache_domain *dcache, + struct mapcache_vcpu *vcache) +{ + /* The domain's vCPUs share the mapcache's slots. */ + spin_lock(&dcache->lock); + + /* Has some other CPU caused a wrap? We must flush if so. */ + if ( unlikely(dcache->epoch !=3D vcache->shadow_epoch) ) + { + vcache->shadow_epoch =3D dcache->epoch; + if ( NEED_FLUSH(this_cpu(tlbflush_time), dcache->tlbflush_timestam= p) ) + { + perfc_incr(domain_page_tlb_flush); + flush_tlb_local(); + } + } +} + +/* A wrap has just flushed this CPU's TLB: start a new epoch. */ +static void mapcache_new_epoch(struct mapcache_domain *dcache, + struct mapcache_vcpu *vcache) +{ + ASSERT(spin_is_locked(&dcache->lock)); + + vcache->shadow_epoch =3D ++dcache->epoch; + dcache->tlbflush_timestamp =3D tlbflush_current_time(); +} + +static void mapcache_exit(struct mapcache_domain *dcache) +{ + spin_unlock(&dcache->lock); +} + #define mapcache_l2_entry(e) ((e) >> PAGETABLE_ORDER) #define MAPCACHE_L2_ENTRIES (mapcache_l2_entry(MAPCACHE_ENTRIES - 1) + 1) #define MAPCACHE_L1ENT(idx) \ @@ -80,6 +128,7 @@ void *map_domain_page(mfn_t mfn) struct vcpu *v; struct mapcache_domain *dcache; struct mapcache_vcpu *vcache; + struct mapcache *cache; struct vcpu_maphash_entry *hashent; =20 #ifdef NDEBUG @@ -91,9 +140,9 @@ void *map_domain_page(mfn_t mfn) if ( !v ) return mfn_to_virt(mfn_x(mfn)); =20 - dcache =3D &v->domain->arch.pv.mapcache; + cache =3D vcpu_mapcache(v, &dcache); vcache =3D &v->arch.pv.mapcache; - if ( !dcache->inuse ) + if ( !cache->inuse ) return mfn_to_virt(mfn_x(mfn)); =20 perfc_incr(map_domain_page_count); @@ -104,41 +153,30 @@ void *map_domain_page(mfn_t mfn) if ( hashent->mfn =3D=3D mfn_x(mfn) ) { idx =3D hashent->idx; - ASSERT(idx < dcache->entries); + ASSERT(idx < cache->entries); hashent->refcnt++; ASSERT(hashent->refcnt); ASSERT(mfn_eq(l1e_get_mfn(MAPCACHE_L1ENT(idx)), mfn)); goto out; } =20 - spin_lock(&dcache->lock); - - /* Has some other CPU caused a wrap? We must flush if so. */ - if ( unlikely(dcache->epoch !=3D vcache->shadow_epoch) ) - { - vcache->shadow_epoch =3D dcache->epoch; - if ( NEED_FLUSH(this_cpu(tlbflush_time), dcache->tlbflush_timestam= p) ) - { - perfc_incr(domain_page_tlb_flush); - flush_tlb_local(); - } - } + mapcache_enter(dcache, vcache); =20 - idx =3D find_next_zero_bit(dcache->inuse, dcache->entries, dcache->cur= sor); - if ( unlikely(idx >=3D dcache->entries) ) + idx =3D find_next_zero_bit(cache->inuse, cache->entries, cache->cursor= ); + if ( unlikely(idx >=3D cache->entries) ) { unsigned long accum =3D 0, prev =3D 0; =20 /* /First/, clean the garbage map and update the inuse list. */ - for ( i =3D 0; i < BITS_TO_LONGS(dcache->entries); i++ ) + for ( i =3D 0; i < BITS_TO_LONGS(cache->entries); i++ ) { accum |=3D prev; - dcache->inuse[i] &=3D ~xchg(&dcache->garbage[i], 0); - prev =3D ~dcache->inuse[i]; + cache->inuse[i] &=3D ~xchg(&cache->garbage[i], 0); + prev =3D ~cache->inuse[i]; } =20 - if ( accum | (prev & BITMAP_LAST_WORD_MASK(dcache->entries)) ) - idx =3D find_first_zero_bit(dcache->inuse, dcache->entries); + if ( accum | (prev & BITMAP_LAST_WORD_MASK(cache->entries)) ) + idx =3D find_first_zero_bit(cache->inuse, cache->entries); else { /* Replace a hash entry instead. */ @@ -158,19 +196,18 @@ void *map_domain_page(mfn_t mfn) i =3D 0; } while ( i !=3D MAPHASH_HASHFN(mfn_x(mfn)) ); } - BUG_ON(idx >=3D dcache->entries); + BUG_ON(idx >=3D cache->entries); =20 /* /Second/, flush TLBs. */ perfc_incr(domain_page_tlb_flush); flush_tlb_local(); - vcache->shadow_epoch =3D ++dcache->epoch; - dcache->tlbflush_timestamp =3D tlbflush_current_time(); + mapcache_new_epoch(dcache, vcache); } =20 - set_bit(idx, dcache->inuse); - dcache->cursor =3D idx + 1; + set_bit(idx, cache->inuse); + cache->cursor =3D idx + 1; =20 - spin_unlock(&dcache->lock); + mapcache_exit(dcache); =20 l1e_write(&MAPCACHE_L1ENT(idx), l1e_from_mfn(mfn, __PAGE_HYPERVISOR_RW= )); =20 @@ -184,6 +221,7 @@ void unmap_domain_page(const void *ptr) unsigned int idx; struct vcpu *v; struct mapcache_domain *dcache; + struct mapcache *cache; unsigned long va =3D (unsigned long)ptr, mfn, flags; struct vcpu_maphash_entry *hashent; =20 @@ -195,8 +233,8 @@ void unmap_domain_page(const void *ptr) v =3D mapcache_current_vcpu(); ASSERT(v); =20 - dcache =3D &v->domain->arch.pv.mapcache; - ASSERT(dcache->inuse); + cache =3D vcpu_mapcache(v, &dcache); + ASSERT(cache->inuse); =20 idx =3D PFN_DOWN(va - MAPCACHE_VIRT_START); mfn =3D l1e_get_pfn(MAPCACHE_L1ENT(idx)); @@ -219,7 +257,7 @@ void unmap_domain_page(const void *ptr) hashent->mfn); l1e_write(&MAPCACHE_L1ENT(hashent->idx), l1e_empty()); /* /Second/, mark as garbage. */ - set_bit(hashent->idx, dcache->garbage); + set_bit(hashent->idx, cache->garbage); } =20 /* Add newly-freed mapping to the maphash. */ @@ -231,7 +269,7 @@ void unmap_domain_page(const void *ptr) /* /First/, zap the PTE. */ l1e_write(&MAPCACHE_L1ENT(idx), l1e_empty()); /* /Second/, mark as garbage. */ - set_bit(idx, dcache->garbage); + set_bit(idx, cache->garbage); } =20 local_irq_restore(flags); @@ -366,9 +404,9 @@ void mapcache_domain_init(struct domain *d) 2 * PFN_UP(BITS_TO_LONGS(MAPCACHE_ENTRIES) * sizeof(long)= )) > MAPCACHE_MULTIPAGE_VIRT_START); bitmap_pages =3D PFN_UP(BITS_TO_LONGS(MAPCACHE_ENTRIES) * sizeof(long)= ); - dcache->inuse =3D (void *)MAPCACHE_VIRT_END + PAGE_SIZE; - dcache->garbage =3D dcache->inuse + - (bitmap_pages + 1) * PAGE_SIZE / sizeof(long); + dcache->cache.inuse =3D (void *)MAPCACHE_VIRT_END + PAGE_SIZE; + dcache->cache.garbage =3D dcache->cache.inuse + + (bitmap_pages + 1) * PAGE_SIZE / sizeof(long); =20 spin_lock_init(&dcache->lock); } @@ -376,7 +414,8 @@ void mapcache_domain_init(struct domain *d) int mapcache_vcpu_init(struct vcpu *v) { struct domain *d =3D v->domain; - struct mapcache_domain *dcache =3D &d->arch.pv.mapcache; + struct mapcache_domain *dcache; + struct mapcache *cache; unsigned long i; unsigned int ents =3D d->max_vcpus * MAPCACHE_VCPU_ENTRIES; unsigned int nr =3D PFN_UP(BITS_TO_LONGS(ents) * sizeof(long)); @@ -394,10 +433,14 @@ int mapcache_vcpu_init(struct vcpu *v) return rc; } =20 - if ( !is_pv_vcpu(v) || !dcache->inuse ) + if ( !has_mapcache(v) ) + return 0; + + cache =3D vcpu_mapcache(v, &dcache); + if ( !cache->inuse ) return 0; =20 - if ( ents > dcache->entries ) + if ( ents > cache->entries ) { /* Populate page tables. */ int rc =3D create_perdomain_mapping(v, MAPCACHE_VIRT_START, ents, @@ -405,16 +448,16 @@ int mapcache_vcpu_init(struct vcpu *v) =20 /* Populate bit maps. */ if ( !rc ) - rc =3D create_perdomain_mapping(v, (unsigned long)dcache->inus= e, + rc =3D create_perdomain_mapping(v, (unsigned long)cache->inuse, nr, NULL, NIL(struct page_info *= )); if ( !rc ) - rc =3D create_perdomain_mapping(v, (unsigned long)dcache->garb= age, + rc =3D create_perdomain_mapping(v, (unsigned long)cache->garba= ge, nr, NULL, NIL(struct page_info *= )); =20 if ( rc ) return rc; =20 - dcache->entries =3D ents; + cache->entries =3D ents; } =20 /* Mark all maphash entries as not in use. */ diff --git a/xen/arch/x86/include/asm/domain.h b/xen/arch/x86/include/asm/d= omain.h index 17903d62cb..39d9680bcf 100644 --- a/xen/arch/x86/include/asm/domain.h +++ b/xen/arch/x86/include/asm/domain.h @@ -54,6 +54,16 @@ struct trap_bounce { unsigned long eip; }; =20 +struct mapcache { + /* The number of array entries, and a cursor into the array. */ + unsigned int entries; + unsigned int cursor; + + /* Which mappings are in use, and which are garbage to reap next epoch= ? */ + unsigned long *inuse; + unsigned long *garbage; +}; + #define MAPHASH_ENTRIES 8 #define MAPHASH_HASHFN(pfn) ((pfn) & (MAPHASH_ENTRIES-1)) #define MAPHASHENT_NOTINUSE ((u32)~0U) @@ -70,10 +80,6 @@ struct mapcache_vcpu { }; =20 struct mapcache_domain { - /* The number of array entries, and a cursor into the array. */ - unsigned int entries; - unsigned int cursor; - /* Protects map_domain_page(). */ spinlock_t lock; =20 @@ -81,9 +87,8 @@ struct mapcache_domain { unsigned int epoch; u32 tlbflush_timestamp; =20 - /* Which mappings are in use, and which are garbage to reap next epoch= ? */ - unsigned long *inuse; - unsigned long *garbage; + /* Accounting of the domain-wide mapcache. */ + struct mapcache cache; }; =20 void mapcache_domain_init(struct domain *d); --=20 2.56.0 From nobody Mon Nov 23 22:32:25 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=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1795123877; cv=none; d=zohomail.com; s=zohoarc; b=N27Dr6RTG8HeUxiFbNjR9sAcyZtLXLC1YGN851jtV9Z2AN4//MSkvbOBOMNIvmlBLhJVGuzxKgxDZZiVmdaAVNMBNpkGQdAd2nCrRXiPxdEDhtBqAArr6EOQYzsNMPUkjmLpaCYvZJYz3T6j4Bbm4H84y95D0wVU1ErxS9QX51U= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795123877; 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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=dsCX5Nuo4b1o1vo+zAtU8JHiOOJTv3P1TTbOwNApgC4=; b=O9J+yfb9xHb0kB4G5ToqO2CWxKA9oFp3CeZFKIFV+/5iKbFV2XKubasBBAP2InGq2QnAru0SqRmOCtIsHgNr61193EiUxXd1ZMIyVTl4YxTD6NmxyZNRR/k2P2VudX4nTs/GqJT2FIkqiKnaj/GkIHuI+nEOCU0UXvKkWme0puk= 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=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1795123877263453.9023895819728; Thu, 19 Nov 2026 13:31:17 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1497138.1695609 (Exim 4.92) (envelope-from ) id 1xU9ie-00018f-66; Thu, 19 Nov 2026 21:31:00 +0000 Received: by outflank-mailman (output) from mailman id 1497138.1695609; Thu, 19 Nov 2026 21:30: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 1xU9ic-00011V-FK; Thu, 19 Nov 2026 21:30:58 +0000 Received: by outflank-mailman (input) for mailman id 1497138; Thu, 19 Nov 2026 21:30:20 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9hz-0001EK-Td for xen-devel@lists.xenproject.org; Thu, 19 Nov 2026 21:30:19 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xU9hz-00000006tJh-24TK; Thu, 19 Nov 2026 21:30:19 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xU9Me-0000000CVBH-08WG; Thu, 19 Nov 2026 21:08:16 +0000 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" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:Content-Type: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=dsCX5Nuo4b1o1vo+zAtU8JHiOOJTv3P1TTbOwNApgC4=; b=3AaLheAUO880XPW7svD33Ifd+t UTeYs0m1K4Awfw23gCNUCkNdaI5LH+qUpf4eoOgqt+9MzWAv7nN8FQp9zwoFqtKL59O6r8SoEA0BB 5SLCU7mbqsn3Sd5m5n2lHtN2LFELzKmKdIOjyBkb7vLuEVy41vJ3jjHnHZSHrslXQLLk=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Jan Beulich , Andrew Cooper , Teddy Astie Subject: [PATCH v7 28/50] x86/domain_page: move struct mapcache_vcpu to struct arch_vcpu Date: Thu, 19 Nov 2026 21:07:19 +0000 Message-ID: <20261119-asi-hvm-v7-28-6551b6d5c879@xenproject.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> References: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1795123877625158500 From: Roger Pau Monn=C3=A9 struct mapcache_vcpu, the per-vCPU part of the mapcache (the maphash and the vCPU's shadow of the domain's TLB epoch), lives in struct pv_vcpu, as only PV vCPUs have a mapcache. In preparation for implementing mapcaches for HVM domains, move it to struct arch_vcpu: v->arch.pv.mapcache becomes v->arch.mapcache. struct pv_vcpu shrinks from 512 to 376 bytes, and struct vcpu grows from 2688 to 2880 bytes (x86_64, debug build), within its page: from here on HVM vCPUs carry the 136-byte struct mapcache_vcpu too, ahead of its use by those of vCPU-PT domains. No functional change. Signed-off-by: Roger Pau Monn=C3=A9 Assisted-by: Claude Code:claude-fable-5, Claude Code:claude-opus-4-8, Claud= e Code:claude-opus-5-5 Signed-off-by: George Dunlap Reviewed-by: Roger Pau Monn=C3=A9 --- Changes in v7: - struct vcpu's sizes measured again: the register frame leaves it earlier in the series now. Changes in v3: - New in this version, split out of "x86/hvm: introduce per-vCPU mapcache for vCPU-PT domains" for review. --- xen/arch/x86/domain_page.c | 6 +++--- xen/arch/x86/include/asm/domain.h | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/xen/arch/x86/domain_page.c b/xen/arch/x86/domain_page.c index 13b50d89e5..2ade2f9fd0 100644 --- a/xen/arch/x86/domain_page.c +++ b/xen/arch/x86/domain_page.c @@ -141,7 +141,7 @@ void *map_domain_page(mfn_t mfn) return mfn_to_virt(mfn_x(mfn)); =20 cache =3D vcpu_mapcache(v, &dcache); - vcache =3D &v->arch.pv.mapcache; + vcache =3D &v->arch.mapcache; if ( !cache->inuse ) return mfn_to_virt(mfn_x(mfn)); =20 @@ -238,7 +238,7 @@ void unmap_domain_page(const void *ptr) =20 idx =3D PFN_DOWN(va - MAPCACHE_VIRT_START); mfn =3D l1e_get_pfn(MAPCACHE_L1ENT(idx)); - hashent =3D &v->arch.pv.mapcache.hash[MAPHASH_HASHFN(mfn)]; + hashent =3D &v->arch.mapcache.hash[MAPHASH_HASHFN(mfn)]; =20 local_irq_save(flags); =20 @@ -464,7 +464,7 @@ int mapcache_vcpu_init(struct vcpu *v) BUILD_BUG_ON(MAPHASHENT_NOTINUSE < MAPCACHE_ENTRIES); for ( i =3D 0; i < MAPHASH_ENTRIES; i++ ) { - struct vcpu_maphash_entry *hashent =3D &v->arch.pv.mapcache.hash[i= ]; + struct vcpu_maphash_entry *hashent =3D &v->arch.mapcache.hash[i]; =20 hashent->mfn =3D ~0UL; /* never valid to map */ hashent->idx =3D MAPHASHENT_NOTINUSE; diff --git a/xen/arch/x86/include/asm/domain.h b/xen/arch/x86/include/asm/d= omain.h index 39d9680bcf..d2515cd01a 100644 --- a/xen/arch/x86/include/asm/domain.h +++ b/xen/arch/x86/include/asm/domain.h @@ -542,9 +542,6 @@ struct arch_domain =20 struct pv_vcpu { - /* map_domain_page() mapping cache. */ - struct mapcache_vcpu mapcache; - unsigned int vgc_flags; =20 struct trap_info *trap_ctxt; @@ -646,6 +643,9 @@ struct arch_vcpu #define async_exception_state(t) async_exception_state[(t)-1] uint8_t async_exception_mask; =20 + /* map_domain_page() mapping cache. */ + struct mapcache_vcpu mapcache; + /* Virtual Machine Extensions */ union { struct pv_vcpu pv; --=20 2.56.0 From nobody Mon Nov 23 22:32:25 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=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1795123839; cv=none; d=zohomail.com; s=zohoarc; b=nHUSOoDtYMn2yR4kiT/0/X3ZO4M4WXJ5NLFmZ0siAmXGnAiUCpE7GL/URubvURAx8i5t27f4JcNQsVxyonK9GyaDhuhHGRUWWE4hMD8pBZ8A6Ek+7XSykO9v8nSqzPuG+bA4P1+7R01G9fjsO74pPrwQkVUpMaw/85kf2yMEz64= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795123839; 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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=m2oN67Q6MdMj+BGnNG7mV2Sbn0XOpbnC48m8q6ubOXU=; b=nxGYIgYCMwB8Cey51XqRiYT3Qm48XPsCos5wmbiv6wHBYI069/J/idmo8Gfp0S7t5FSmS5t3JWh7vF+2o1Eiy9EhKBhxwqxuiHAEaWRs6CNfPoHQDL63x6FcI83P4rO6TQPVXuUtszFj/euYQEwautQjRZ4JBrybBVKg2lE+Euk= 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=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1795123839622329.6147996344657; Thu, 19 Nov 2026 13:30:39 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1497109.1695385 (Exim 4.92) (envelope-from ) id 1xU9i0-0001Gc-W3; Thu, 19 Nov 2026 21:30:20 +0000 Received: by outflank-mailman (output) from mailman id 1497109.1695385; Thu, 19 Nov 2026 21:30:20 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9i0-0001G7-QA; Thu, 19 Nov 2026 21:30:20 +0000 Received: by outflank-mailman (input) for mailman id 1497109; Thu, 19 Nov 2026 21:30:18 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9hy-0001Bi-UJ for xen-devel@lists.xenproject.org; Thu, 19 Nov 2026 21:30:18 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xU9hy-00000006tGF-2CXp; Thu, 19 Nov 2026 21:30:18 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xU9Me-0000000CVBH-3wqb; Thu, 19 Nov 2026 21:08:17 +0000 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" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:Content-Type: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=m2oN67Q6MdMj+BGnNG7mV2Sbn0XOpbnC48m8q6ubOXU=; b=Y4NP1kVlnCJNPXE9dI/pO8c/k2 KFrKMtEnWBTooxzND43CXuyewS7/MrJIrX55OfKoUP9qQp0SYijAakHl+jyBGGu6qbZIUwNhJP8PI EtUSWldVLgSsdGnF/XPzjhuIYgN5KCJLwQW+Aw9h7U4LRaS8XzHW6L5FKUWVFpJS0ba8=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Jan Beulich , Andrew Cooper , Teddy Astie Subject: [PATCH v7 29/50] x86/domain_page: don't unmap a mapcache entry that is not present Date: Thu, 19 Nov 2026 21:07:20 +0000 Message-ID: <20261119-asi-hvm-v7-29-6551b6d5c879@xenproject.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> References: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1795123841341158500 Mappings must be torn down by the vCPU that created them, and only once. Have unmap_domain_page() check that the entry it is handed is present and bail out otherwise (ASSERT_UNREACHABLE()), rather than read an MFN out of an empty PTE and act on it, as a second unmap of an entry the first one zapped would. (A second unmap of an entry the maphash kept is caught, as before, only by the refcount ASSERT() in debug builds.) Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap Reviewed-by: Roger Pau Monn=C3=A9 --- Changes in v4: - Reword the comment on the new check (Roger). Changes in v3: - New in this version, split out of "x86/hvm: introduce per-vCPU mapcache for vCPU-PT domains" for review. --- xen/arch/x86/domain_page.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/xen/arch/x86/domain_page.c b/xen/arch/x86/domain_page.c index 2ade2f9fd0..1cce58a466 100644 --- a/xen/arch/x86/domain_page.c +++ b/xen/arch/x86/domain_page.c @@ -237,6 +237,15 @@ void unmap_domain_page(const void *ptr) ASSERT(cache->inuse); =20 idx =3D PFN_DOWN(va - MAPCACHE_VIRT_START); + /* + * Sanity check: an entry that isn't present means a double or stray + * unmap. Play it safe: don't act on the MFN in an empty PTE. + */ + if ( !(l1e_get_flags(MAPCACHE_L1ENT(idx)) & _PAGE_PRESENT) ) + { + ASSERT_UNREACHABLE(); + return; + } mfn =3D l1e_get_pfn(MAPCACHE_L1ENT(idx)); hashent =3D &v->arch.mapcache.hash[MAPHASH_HASHFN(mfn)]; =20 --=20 2.56.0 From nobody Mon Nov 23 22:32:25 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=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1795123900; cv=none; d=zohomail.com; s=zohoarc; b=J9e47YoDcwFbCjnE8T3hlVh+4JANNxtIILjOIJReRU7wn5A95Wd7mDHC5auoHpsC1nBcamWD3gTYuL/Rn/w5/WSmMzKdlwgY+8De78MB4PdbvXzjKPvZ+AVW9A6V4pPDJ296NUoeJvofLUPf1bu4cDvUhLyumAUcgto1mMdB/RQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795123900; 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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=HGL2d4pfoMGCE9kjBG8zX8+XeOTokJ943X2UQzHnats=; b=XscuoI+HIpa6yH36DsgKy9OR68EKHrGG1A1opO16UTDayBHY77UAGd9X75gMa36aLE7ZoLbVSiInWMvxmHzDT61HBLiXK2yRo01DI6PTp7O/NnNKG1lg8ZQ5PJ7PaGyqXXXRLwVSAUGvdvGFGGFjXAGexBQh9xAAfrZhcD3hVd0= 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=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1795123900342881.4171172553026; Thu, 19 Nov 2026 13:31:40 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1497143.1695631 (Exim 4.92) (envelope-from ) id 1xU9ik-0002OM-3Q; Thu, 19 Nov 2026 21:31:06 +0000 Received: by outflank-mailman (output) from mailman id 1497143.1695631; Thu, 19 Nov 2026 21:31:05 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9ii-0002IV-UP; Thu, 19 Nov 2026 21:31:04 +0000 Received: by outflank-mailman (input) for mailman id 1497143; Thu, 19 Nov 2026 21:30:20 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9i0-0001Eb-45 for xen-devel@lists.xenproject.org; Thu, 19 Nov 2026 21:30:20 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xU9hz-00000006tKI-2yO4; Thu, 19 Nov 2026 21:30:20 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xU9Mf-0000000CVBH-452j; Thu, 19 Nov 2026 21:08:18 +0000 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" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:Content-Type: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=HGL2d4pfoMGCE9kjBG8zX8+XeOTokJ943X2UQzHnats=; b=xdT15CbaS/gcv8OMkqN4CSlZlm R0yA6pN6alujYDGGtGvpN9y9HEUAxJLVpCTMhZmvHrCL5LfaqmfyGS9W9qYzoe9vyN8lUM+eM5vHb 6eleOHtDb25wVWacFcS2TZ2PwpRiPAPpcsS8PoW/jr8L758JHVQe4wnLSH/CWZmgLW6M=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie Subject: [PATCH v7 30/50] x86/hvm: introduce per-vCPU mapcache for vCPU-PT domains Date: Thu, 19 Nov 2026 21:07:21 +0000 Message-ID: <20261119-asi-hvm-v7-30-6551b6d5c879@xenproject.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> References: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1795123901876158500 From: Roger Pau Monn=C3=A9 Add a struct mapcache to struct mapcache_vcpu for the per-vCPU accounting, with MAPCACHE_PERVCPU_ENTRIES (64) slots. Its inuse and garbage bitmaps are then an unsigned long each: declare them inline in struct mapcache_vcpu, rather than mapping them in the per-domain area as the domain-wide bitmaps are. A per-vCPU mapcache is only ever used by its own vCPU, and so by one pCPU at a time. We can therefore skip both the lock and the TLB-epoch tracking the domain-wide mapcache needs; a wrap still flushes the local TLB. struct mapcache_vcpu grows from 136 to 176 bytes, into padding ahead of a 64-byte-aligned member of struct arch_vcpu: struct vcpu stays at 2880 bytes (x86_64, debug build). Unlike a domain-wide mapcache, whose one pool all the domain's vCPUs draw on, a per-vCPU one cannot, by design, use more than its own slots: MAPCACHE_PERVCPU_ENTRIES (64) is the most single-page mappings such a vCPU can hold at once, interrupt handlers' included, and one more hits the BUG_ON() in map_domain_page(). The PoD zero-page sweep held up to 16 on top of its callers' until 41ddcfe808ec ("x86/PoD: map one page at a time in p2m_pod_zero_check()"). A domain-wide mapcache is sized at MAPCACHE_VCPU_ENTRIES (16) slots per vCPU, in one shared pool; a per-vCPU one gets 64, for a wider margin. The extra slots take no memory: the area is a whole L1 page whatever the number of slots, and each bitmap is still one unsigned long. Wraps, each of which flushes the local TLB, also come less often. mapcache_current_vcpu()'s lazy-context-switch handling (sync_local_execstate() plus a pgtable_vcpu re-read) now applies to any vCPU with a mapcache: when the idle vCPU runs lazily on an HVM vCPU's monitor table, the full switch happens before the idle context uses a mapcache. In release builds map_domain_page() still tries the directmap first, for any vCPU; debug builds exercise the new mapcache. For a vCPU-PT domain, the slots of map_domain_pages() move too: a vCPU's MAPCACHE_MULTIPAGE_ENTRIES slots are just above the single-page ones of its own mapcache, and its block of the domain-wide multipage area is no longer set up. Signed-off-by: Roger Pau Monn=C3=A9 Assisted-by: Claude Code:claude-fable-5, Claude Code:claude-opus-4-8, Claud= e Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Changes in v7: - Rebased over map_domain_pages(), which now comes earlier in the series: a vCPU-PT domain's multipage slots are above its per-vCPU mapcache, as that patch had them. struct vcpu's size measured again. Changes in v6: - Message: "x86/PoD: map one page at a time in p2m_pod_zero_check()" is in staging now, so cite it by its hash. Changes in v5: - Message: a domain-wide mapcache is one pool that all the domain's vCPUs draw on, not per-vCPU slots another vCPU could borrow. Changes in v4: - d->arch.vcpu_pt is now d->arch.asi.vcpu_pt. - mapcache_lock() / mapcache_unlock() are now mapcache_enter() / mapcache_exit(). has_mapcache()'s comment no longer changes, and the early return in mapcache_enter() has a one-line comment instead of a sentence added to the block (Roger). - mapcache_vcpu_init(): ents and nr become assignments here, ents now depending on the kind of mapcache (Jan). - A per-vCPU mapcache gets 64 slots (MAPCACHE_PERVCPU_ENTRIES) in place of MAPCACHE_VCPU_ENTRIES (16), which still sizes the domain-wide mapcache; its bitmaps, now full, are described as an unsigned long each (Roger). - Move the comment on why a per-vCPU mapcache needs only a local flush from map_domain_page()'s TLB flush to mapcache_new_epoch()'s early return, as one line (Roger). Changes in v3: - HVM only, and per-vCPU from the start: the HVM enablement of "x86: Initialize the mapcache for PV, HVM, and idle domains" (from the directmap removal series) is folded in, without the idle domain and without a domain-wide HVM mapcache. - Retitle (was: "x86/mm: introduce a per-vCPU mapcache when using ASI"). - Say which second unmaps the not-present check in unmap_domain_page() catches: those of an entry the first unmap zapped. - State the budget of single-page mappings a vCPU can hold at once. - The mapcache accounting refactor, the move of struct mapcache_vcpu to struct arch_vcpu and the not-present check in unmap_domain_page() are split out, for review, into the three patches before this one. --- xen/arch/x86/domain_page.c | 74 +++++++++++++++++++++++-------- xen/arch/x86/include/asm/config.h | 7 ++- xen/arch/x86/include/asm/domain.h | 6 +++ xen/arch/x86/mm.c | 8 ++-- 4 files changed, 72 insertions(+), 23 deletions(-) diff --git a/xen/arch/x86/domain_page.c b/xen/arch/x86/domain_page.c index 1cce58a466..7bd6399ca6 100644 --- a/xen/arch/x86/domain_page.c +++ b/xen/arch/x86/domain_page.c @@ -22,7 +22,7 @@ /* Whether the vCPU supports mapcache usage, might not be in use. */ static bool has_mapcache(const struct vcpu *v) { - return is_pv_vcpu(v); + return is_pv_vcpu(v) || v->domain->arch.asi.vcpu_pt; } =20 /* @@ -71,14 +71,21 @@ static inline struct vcpu *mapcache_current_vcpu(void) } =20 /* - * The accounting for v's mapcache: the domain's, with *dcache set so the - * caller takes the domain-wide lock and TLB-epoch machinery into account. + * The accounting for v's mapcache: its own for a vCPU-PT domain, the doma= in's + * otherwise, in which case *dcache is set so the caller takes the domain-= wide + * lock and TLB-epoch machinery into account. */ static struct mapcache *vcpu_mapcache(struct vcpu *v, struct mapcache_domain **dcache) { struct domain *d =3D v->domain; =20 + if ( d->arch.asi.vcpu_pt ) + { + *dcache =3D NULL; + return &v->arch.mapcache.cache; + } + *dcache =3D &d->arch.pv.mapcache; return &(*dcache)->cache; } @@ -86,6 +93,10 @@ static struct mapcache *vcpu_mapcache(struct vcpu *v, static void mapcache_enter(struct mapcache_domain *dcache, struct mapcache_vcpu *vcache) { + /* Only its own vCPU uses a per-vCPU mapcache: no lock or epoch needed= . */ + if ( !dcache ) + return; + /* The domain's vCPUs share the mapcache's slots. */ spin_lock(&dcache->lock); =20 @@ -105,6 +116,10 @@ static void mapcache_enter(struct mapcache_domain *dca= che, static void mapcache_new_epoch(struct mapcache_domain *dcache, struct mapcache_vcpu *vcache) { + /* Per-vCPU: no epoch, as page-table loads flush its non-global entrie= s. */ + if ( !dcache ) + return; + ASSERT(spin_is_locked(&dcache->lock)); =20 vcache->shadow_epoch =3D ++dcache->epoch; @@ -113,7 +128,8 @@ static void mapcache_new_epoch(struct mapcache_domain *= dcache, =20 static void mapcache_exit(struct mapcache_domain *dcache) { - spin_unlock(&dcache->lock); + if ( dcache ) + spin_unlock(&dcache->lock); } =20 #define mapcache_l2_entry(e) ((e) >> PAGETABLE_ORDER) @@ -285,11 +301,15 @@ void unmap_domain_page(const void *ptr) } =20 /* - * The mapcache index of @v's first map_domain_pages() slot: at its stride= of - * the multipage area. + * The mapcache index of @v's first map_domain_pages() slot: above the + * single-page slots of its own mapcache with per-vCPU page tables, otherw= ise + * at its stride of the domain-wide multipage area. */ static unsigned int multipage_first_slot(const struct vcpu *v) { + if ( v->domain->arch.asi.vcpu_pt ) + return MAPCACHE_PERVCPU_ENTRIES; + return PFN_DOWN(MAPCACHE_MULTIPAGE_START(v) - MAPCACHE_VIRT_START); } =20 @@ -426,13 +446,12 @@ int mapcache_vcpu_init(struct vcpu *v) struct mapcache_domain *dcache; struct mapcache *cache; unsigned long i; - unsigned int ents =3D d->max_vcpus * MAPCACHE_VCPU_ENTRIES; - unsigned int nr =3D PFN_UP(BITS_TO_LONGS(ents) * sizeof(long)); + unsigned int ents, nr; =20 BUILD_BUG_ON(MAPCACHE_MULTIPAGE_ENTRIES > BITS_PER_LONG); =20 - /* The vCPU's stride of the multipage area. */ - if ( !is_idle_domain(d) ) + /* Without per-vCPU page tables, the vCPU's stride of the multipage ar= ea. */ + if ( !is_idle_domain(d) && !d->arch.asi.vcpu_pt ) { int rc =3D create_perdomain_mapping(v, MAPCACHE_MULTIPAGE_START(v), MAPCACHE_MULTIPAGE_ENTRIES, @@ -446,22 +465,41 @@ int mapcache_vcpu_init(struct vcpu *v) return 0; =20 cache =3D vcpu_mapcache(v, &dcache); - if ( !cache->inuse ) + if ( !dcache ) + { + BUILD_BUG_ON(MAPCACHE_PERVCPU_ENTRIES + MAPCACHE_MULTIPAGE_ENTRIES= > + MAPCACHE_ENTRIES); + + /* Per-vCPU bitmaps are an unsigned long each, in the vCPU itself.= */ + cache->inuse =3D v->arch.mapcache.inuse; + cache->garbage =3D v->arch.mapcache.garbage; + ents =3D MAPCACHE_PERVCPU_ENTRIES; + } + else if ( !cache->inuse ) return 0; + else + ents =3D d->max_vcpus * MAPCACHE_VCPU_ENTRIES; + + nr =3D PFN_UP(BITS_TO_LONGS(ents) * sizeof(long)); =20 if ( ents > cache->entries ) { - /* Populate page tables. */ - int rc =3D create_perdomain_mapping(v, MAPCACHE_VIRT_START, ents, + /* Populate page tables, a per-vCPU cache's multipage slots includ= ed. */ + int rc =3D create_perdomain_mapping(v, MAPCACHE_VIRT_START, + ents + (dcache ? 0 + : MAPCACHE_MULTIPAGE_ENT= RIES), NIL(l1_pgentry_t *), NULL); =20 - /* Populate bit maps. */ - if ( !rc ) + /* Populate the domain-wide bit maps (kept in the per-domain area)= . */ + if ( !rc && dcache ) + { rc =3D create_perdomain_mapping(v, (unsigned long)cache->inuse, nr, NULL, NIL(struct page_info *= )); - if ( !rc ) - rc =3D create_perdomain_mapping(v, (unsigned long)cache->garba= ge, - nr, NULL, NIL(struct page_info *= )); + if ( !rc ) + rc =3D create_perdomain_mapping(v, (unsigned long)cache->g= arbage, + nr, NULL, + NIL(struct page_info *)); + } =20 if ( rc ) return rc; diff --git a/xen/arch/x86/include/asm/config.h b/xen/arch/x86/include/asm/c= onfig.h index cea4cd4662..5768b6efe3 100644 --- a/xen/arch/x86/include/asm/config.h +++ b/xen/arch/x86/include/asm/config.h @@ -279,7 +279,12 @@ extern unsigned long xen_phys_start; =20 /* map_domain_page() map cache. The second per-domain-mapping sub-area. */ #define MAPCACHE_VCPU_ENTRIES (CONFIG_PAGING_LEVELS * CONFIG_PAGING_LEV= ELS) -/* Multipage slots of each vCPU, at its stride of the multipage area. */ +/* Single-page slots of a per-vCPU mapcache (vCPU-PT domains). */ +#define MAPCACHE_PERVCPU_ENTRIES 64 +/* + * Multipage slots of each vCPU: above the single-page ones of a per-vCPU + * mapcache, otherwise at the vCPU's stride of the multipage area. + */ #define MAPCACHE_MULTIPAGE_ORDER 3 #define MAPCACHE_MULTIPAGE_ENTRIES (1U << MAPCACHE_MULTIPAGE_ORDER) #define MAPCACHE_ENTRIES (MAX_VIRT_CPUS * MAPCACHE_VCPU_ENTRIES) diff --git a/xen/arch/x86/include/asm/domain.h b/xen/arch/x86/include/asm/d= omain.h index d2515cd01a..5084848730 100644 --- a/xen/arch/x86/include/asm/domain.h +++ b/xen/arch/x86/include/asm/domain.h @@ -77,6 +77,12 @@ struct mapcache_vcpu { uint32_t idx; uint32_t refcnt; } hash[MAPHASH_ENTRIES]; + + /* Accounting of a per-vCPU mapcache (vCPU-PT domains). */ + struct mapcache cache; + /* Backing for cache.{inuse,garbage}. */ + DECLARE_BITMAP(inuse, MAPCACHE_PERVCPU_ENTRIES); + DECLARE_BITMAP(garbage, MAPCACHE_PERVCPU_ENTRIES); }; =20 struct mapcache_domain { diff --git a/xen/arch/x86/mm.c b/xen/arch/x86/mm.c index d1fc57cf82..8900cde2d0 100644 --- a/xen/arch/x86/mm.c +++ b/xen/arch/x86/mm.c @@ -6538,10 +6538,10 @@ void unmap_vcpu_state(const struct vcpu *v, unsigne= d int idx, unsigned int nr) * Drop a transient mapping of a page behind a vCPU's state window, and th= is * CPU's translations of it: at its address, and at its alias in the * per-domain mirror, which every domain but a 64-bit PV one has (flushing - * that address there is harmless). The mapcache does not drop them with - * the slot's last mapping: it defers the flush, and CPUs running other vC= PUs - * of its domain may have cached the slot too. A directmap address, where - * the caller has no mapcache, needs nothing. + * that address there is harmless). Neither kind of mapcache drops them w= ith + * the slot's last mapping: each defers the flush, and CPUs running other + * vCPUs of the domain may have cached a domain-wide one's slot too. A + * directmap address, where the caller has no mapcache, needs nothing. */ void unmap_vcpu_state_page(const void *va) { --=20 2.56.0 From nobody Mon Nov 23 22:32:25 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=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1795123866; cv=none; d=zohomail.com; s=zohoarc; b=Ihq7M5gdTJHt+hVshIfJWBqfDk54TspZt4TEBJF72tjmZAftohJ3nFr45pdae6Qu8pNsuq1N4kARSP0kGv4j4eSr3Vtyzef+Q+N3vLRiJoSTvbT+ZIKHfg+aI1D789SSt/kEPOrx5vEqKo/sKOtMAGtLPL5QKGU65WZJTkc+CCo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795123866; h=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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=WGBBAjL5SoV6p/uwARv81mOfJpOcQHDltCOvr+FxjIQ=; b=RrMHFC9QOdUAlzPREq6zcdJ7Zg25pygnfeugebndC83O8qJbp9vrQTvnIFyITcUp80NglIZjqoqa/O8ax5f3bpYLfQNsgSVqZvRwRcA1DXgLO0QxCOwYYrGhVhAvE/WbZ5np5UufFm7fzvnw0LZBD49poWGW9tYhvfwtBMsZfy8= 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=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1795123866276320.92931840039375; Thu, 19 Nov 2026 13:31:06 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1497132.1695556 (Exim 4.92) (envelope-from ) id 1xU9iR-0006jH-4x; Thu, 19 Nov 2026 21:30:47 +0000 Received: by outflank-mailman (output) from mailman id 1497132.1695556; Thu, 19 Nov 2026 21:30:46 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9iP-0006Wz-Ce; Thu, 19 Nov 2026 21:30:45 +0000 Received: by outflank-mailman (input) for mailman id 1497132; Thu, 19 Nov 2026 21:30:19 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9hz-0001Dh-Mi for xen-devel@lists.xenproject.org; Thu, 19 Nov 2026 21:30:19 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xU9hz-00000006tJ2-1GzB; Thu, 19 Nov 2026 21:30:19 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xU9Mg-0000000CVBH-3mQ4; Thu, 19 Nov 2026 21:08:19 +0000 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" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=WGBBAjL5SoV6p/uwARv81mOfJpOcQHDltCOvr+FxjIQ=; b=T/eZkDO4M+ix0OlzEiYkhqm5GU hY14AugBrxlBZZ8MXiAs7akxUie0nCkTbLKFfTawJn0xPqVoHi2VgyiP+rPLhVwGPxbTz4joA2aA+ H9mHIMAHp1WAw9dT36ixy+FrdSfsVvzBUbTPE7cWrvzXmLYFIBNHl5y3W++lGikdrA/M=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie Subject: [PATCH v7 31/50] x86/mm: let Xen's page-table walker operate on a given root Date: Thu, 19 Nov 2026 21:07:22 +0000 Message-ID: <20261119-asi-hvm-v7-31-6551b6d5c879@xenproject.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> References: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1795123867610158500 Content-Type: text/plain; charset="utf-8" map_pages_to_xen() and modify_xen_mappings() maintain Xen's own mappings, walking from idle_pg_table, whose Xen slots every other root page-table copies. A later patch introduces a second, sparse directmap hierarchy: page tables for the directmap virtual address range that some guest contexts use in place of idle_pg_table's. It needs the same maintenance at the same virtual addresses. Give virt_to_xen_l{3,2,1}e() the L4 to walk from, and move the bodies of map_pages_to_xen() and modify_xen_mappings() into map_pages_in() and modify_mappings_in(), which take the root as well. The public functions pass idle_pg_table. The EFI runtime-services page-tables mirror idle_pg_table's L4 entries only, so a newly allocated L3 is propagated to them only when the root is idle_pg_table. map_pages_in() and modify_mappings_in() free page-table pages through free_pagetable_in(), which takes the root too and for now only calls free_xen_pagetable(). TLB flushing is unaffected: flushes are by virtual address and go to every online CPU, which covers both hierarchies. No functional change. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Changes in v5: - Free page-table pages through free_pagetable_in(), for now a plain wrapper, split out of "x86/mm: build and maintain a sparse view of the directmap". Changes in v4: - Message: name the second hierarchy as sparse; flushes reach every CPU (Roger). Changes in v3: - New in this version. --- xen/arch/x86/mm.c | 78 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 56 insertions(+), 22 deletions(-) diff --git a/xen/arch/x86/mm.c b/xen/arch/x86/mm.c index 8900cde2d0..73bf438922 100644 --- a/xen/arch/x86/mm.c +++ b/xen/arch/x86/mm.c @@ -5247,13 +5247,14 @@ static DEFINE_SPINLOCK(map_pgdir_lock); =20 /* * For virt_to_xen_lXe() functions, they take a linear address and return a - * pointer to Xen's LX entry. Caller needs to unmap the pointer. + * pointer to Xen's LX entry in the page-table hierarchy rooted at @root + * (idle_pg_table for Xen's own mappings). Caller needs to unmap the poin= ter. */ -static l3_pgentry_t *virt_to_xen_l3e(unsigned long v) +static l3_pgentry_t *virt_to_xen_l3e(l4_pgentry_t *root, unsigned long v) { l4_pgentry_t *pl4e; =20 - pl4e =3D &idle_pg_table[l4_table_offset(v)]; + pl4e =3D &root[l4_table_offset(v)]; if ( !(l4e_get_flags(*pl4e) & _PAGE_PRESENT) ) { bool locking =3D system_state > SYS_STATE_boot; @@ -5269,7 +5270,8 @@ static l3_pgentry_t *virt_to_xen_l3e(unsigned long v) l4_pgentry_t l4e =3D l4e_from_mfn(l3mfn, __PAGE_HYPERVISOR); =20 l4e_write(pl4e, l4e); - efi_update_l4_pgtable(l4_table_offset(v), l4e); + if ( root =3D=3D idle_pg_table ) + efi_update_l4_pgtable(l4_table_offset(v), l4e); l3mfn =3D INVALID_MFN; } if ( locking ) @@ -5280,11 +5282,11 @@ static l3_pgentry_t *virt_to_xen_l3e(unsigned long = v) return map_l3t_from_l4e(*pl4e) + l3_table_offset(v); } =20 -static l2_pgentry_t *virt_to_xen_l2e(unsigned long v) +static l2_pgentry_t *virt_to_xen_l2e(l4_pgentry_t *root, unsigned long v) { l3_pgentry_t *pl3e, l3e; =20 - pl3e =3D virt_to_xen_l3e(v); + pl3e =3D virt_to_xen_l3e(root, v); if ( !pl3e ) return NULL; =20 @@ -5318,11 +5320,11 @@ static l2_pgentry_t *virt_to_xen_l2e(unsigned long = v) return map_l2t_from_l3e(l3e) + l2_table_offset(v); } =20 -static l1_pgentry_t *virt_to_xen_l1e(unsigned long v) +static l1_pgentry_t *virt_to_xen_l1e(l4_pgentry_t *root, unsigned long v) { l2_pgentry_t *pl2e, l2e; =20 - pl2e =3D virt_to_xen_l2e(v); + pl2e =3D virt_to_xen_l2e(root, v); if ( !pl2e ) return NULL; =20 @@ -5398,7 +5400,7 @@ mfn_t xen_map_to_mfn(unsigned long va) bool locking =3D system_state > SYS_STATE_boot; unsigned int l2_offset =3D l2_table_offset(va); unsigned int l1_offset =3D l1_table_offset(va); - const l3_pgentry_t *pl3e =3D virt_to_xen_l3e(va); + const l3_pgentry_t *pl3e =3D virt_to_xen_l3e(idle_pg_table, va); const l2_pgentry_t *pl2e =3D NULL; const l1_pgentry_t *pl1e =3D NULL; struct page_info *l3page; @@ -5438,7 +5440,21 @@ mfn_t xen_map_to_mfn(unsigned long va) return ret; } =20 -int map_pages_to_xen( +/* + * Free a page-table page that the hierarchy rooted at @root has stopped + * using. + */ +static void free_pagetable_in(const l4_pgentry_t *root, mfn_t mfn) +{ + free_xen_pagetable(mfn); +} + +/* + * map_pages_to_xen() in the hierarchy rooted at @root: idle_pg_table's, or + * another hierarchy sharing its virtual addresses. + */ +static int map_pages_in( + l4_pgentry_t *root, unsigned long virt, mfn_t mfn, unsigned long nr_mfns, @@ -5489,7 +5505,7 @@ int map_pages_to_xen( UNMAP_DOMAIN_PAGE(pl3e); UNMAP_DOMAIN_PAGE(pl2e); =20 - pl3e =3D virt_to_xen_l3e(virt); + pl3e =3D virt_to_xen_l3e(root, virt); if ( !pl3e ) goto out; =20 @@ -5542,10 +5558,10 @@ int map_pages_to_xen( ol2e =3D l2t[i]; if ( (l2e_get_flags(ol2e) & _PAGE_PRESENT) && !(l2e_get_flags(ol2e) & _PAGE_PSE) ) - free_xen_pagetable(l2e_get_mfn(ol2e)); + free_pagetable_in(root, l2e_get_mfn(ol2e)); } unmap_domain_page(l2t); - free_xen_pagetable(l3e_get_mfn(ol3e)); + free_pagetable_in(root, l3e_get_mfn(ol3e)); } } =20 @@ -5613,7 +5629,7 @@ int map_pages_to_xen( free_xen_pagetable(l2mfn); } =20 - pl2e =3D virt_to_xen_l2e(virt); + pl2e =3D virt_to_xen_l2e(root, virt); if ( !pl2e ) goto out; =20 @@ -5643,7 +5659,7 @@ int map_pages_to_xen( flush_flags(l1e_get_flags(l1t[i])); flush_area(virt, flush_flags); unmap_domain_page(l1t); - free_xen_pagetable(l2e_get_mfn(ol2e)); + free_pagetable_in(root, l2e_get_mfn(ol2e)); } } =20 @@ -5658,7 +5674,7 @@ int map_pages_to_xen( /* Normal page mapping. */ if ( !(l2e_get_flags(*pl2e) & _PAGE_PRESENT) ) { - pl1e =3D virt_to_xen_l1e(virt); + pl1e =3D virt_to_xen_l1e(root, virt); if ( pl1e =3D=3D NULL ) goto out; } @@ -5775,7 +5791,7 @@ int map_pages_to_xen( flush_area(virt - PAGE_SIZE, FLUSH_TLB_GLOBAL | FLUSH_ORDER(PAGETABLE_ORDER)); - free_xen_pagetable(l2e_get_mfn(ol2e)); + free_pagetable_in(root, l2e_get_mfn(ol2e)); } else if ( locking ) spin_unlock(&map_pgdir_lock); @@ -5822,7 +5838,7 @@ int map_pages_to_xen( flush_area(virt - PAGE_SIZE, FLUSH_TLB_GLOBAL | FLUSH_ORDER(2*PAGETABLE_ORDER)); - free_xen_pagetable(l3e_get_mfn(ol3e)); + free_pagetable_in(root, l3e_get_mfn(ol3e)); } else if ( locking ) spin_unlock(&map_pgdir_lock); @@ -5843,6 +5859,15 @@ int map_pages_to_xen( return rc; } =20 +int map_pages_to_xen( + unsigned long virt, + mfn_t mfn, + unsigned long nr_mfns, + pte_attr_t flags) +{ + return map_pages_in(idle_pg_table, virt, mfn, nr_mfns, flags); +} + int __init populate_pt_range(unsigned long virt, unsigned long nr_mfns) { return map_pages_to_xen(virt, INVALID_MFN, nr_mfns, MAP_SMALL_PAGES); @@ -5859,8 +5884,12 @@ int __init populate_pt_range(unsigned long virt, uns= igned long nr_mfns) * flags are ignored. * * It is an error to call with present flags over an unpopulated range. + * + * modify_mappings_in() does so in the hierarchy rooted at @root, as + * map_pages_in() does. */ -int modify_xen_mappings(unsigned long s, unsigned long e, pte_attr_t nf) +static int modify_mappings_in(l4_pgentry_t *root, unsigned long s, + unsigned long e, pte_attr_t nf) { bool locking =3D system_state > SYS_STATE_boot; l3_pgentry_t *pl3e =3D NULL; @@ -5887,7 +5916,7 @@ int modify_xen_mappings(unsigned long s, unsigned lon= g e, pte_attr_t nf) UNMAP_DOMAIN_PAGE(pl2e); UNMAP_DOMAIN_PAGE(pl3e); =20 - pl3e =3D virt_to_xen_l3e(v); + pl3e =3D virt_to_xen_l3e(root, v); if ( !pl3e ) goto out; =20 @@ -6070,7 +6099,7 @@ int modify_xen_mappings(unsigned long s, unsigned lon= g e, pte_attr_t nf) if ( locking ) spin_unlock(&map_pgdir_lock); flush_area(NULL, FLUSH_TLB_GLOBAL); /* flush before free */ - free_xen_pagetable(l1mfn); + free_pagetable_in(root, l1mfn); } else if ( locking ) spin_unlock(&map_pgdir_lock); @@ -6114,7 +6143,7 @@ int modify_xen_mappings(unsigned long s, unsigned lon= g e, pte_attr_t nf) if ( locking ) spin_unlock(&map_pgdir_lock); flush_area(NULL, FLUSH_TLB_GLOBAL); /* flush before free */ - free_xen_pagetable(l2mfn); + free_pagetable_in(root, l2mfn); } else if ( locking ) spin_unlock(&map_pgdir_lock); @@ -6138,6 +6167,11 @@ int modify_xen_mappings(unsigned long s, unsigned lo= ng e, pte_attr_t nf) =20 #undef flush_area =20 +int modify_xen_mappings(unsigned long s, unsigned long e, pte_attr_t nf) +{ + return modify_mappings_in(idle_pg_table, s, e, nf); +} + int destroy_xen_mappings(unsigned long s, unsigned long e) { return modify_xen_mappings(s, e, _PAGE_NONE); --=20 2.56.0 From nobody Mon Nov 23 22:32:25 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=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1795123886; cv=none; d=zohomail.com; s=zohoarc; b=O9ZMsDdUgvl/MweYDjBk1C8Pi9JcvQKNBb4B+eXHTH8q3R+5TfUq+NP8ZRiqleLnqDf+9AVTmskbafMC2Gwmyp1eKD6YnxBsya2mSYOhYxv1p9PjeYsl81pZiF9B5/2IFwlvG0JF/2hk/666FYWsnbDu97NZgCCvuIq4rrqPsjU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795123886; h=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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=M8LJEwp0ZIhnYJU2cagzivxFzd99+hQjL7GP0+5PuT8=; b=akud3SkI0OWBkUia99afd7vivgFphSSYTRaXWJDAxr/EIqz6n9KqEn+AilJtr8R9dflxG8+nCxbIQIMEGjrQHg1fqw4FUU84Qs88OyUuTZLCmQSlRhcMUXlG9zfFiqUCb/1ZZHhNILlunkIm3sweXqJDbo4Z7gZjRw3QOGz4hgs= 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=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1795123886739568.1842752368135; Thu, 19 Nov 2026 13:31:26 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1497142.1695617 (Exim 4.92) (envelope-from ) id 1xU9ig-0001VZ-CX; Thu, 19 Nov 2026 21:31:02 +0000 Received: by outflank-mailman (output) from mailman id 1497142.1695617; Thu, 19 Nov 2026 21:31:01 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9ie-0001NJ-9A; Thu, 19 Nov 2026 21:31:00 +0000 Received: by outflank-mailman (input) for mailman id 1497142; Thu, 19 Nov 2026 21:30:20 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9i0-0001Ek-5a for xen-devel@lists.xenproject.org; Thu, 19 Nov 2026 21:30:20 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xU9hz-00000006tKF-2nsE; Thu, 19 Nov 2026 21:30:19 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xU9Mi-0000000CVBH-0zzW; Thu, 19 Nov 2026 21:08:20 +0000 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" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=M8LJEwp0ZIhnYJU2cagzivxFzd99+hQjL7GP0+5PuT8=; b=l5Cxnw49TNpeJ8SroOhuNkA2hO AtrcNKplxSJtJQS0UvM4DQZtin2dgha9gE8HOUKtvgk5GHLot2zIvlq3NzK+/XpqsmBgaiGsDC4E3 Oq6K2DRVa0eobgqRVz7BvybltRg2am/K3/32pMtXBGthgfxkyk7LK1NAkFCJ8wCxOus0=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie , Anthony PERARD , Michal Orzel , Julien Grall , Stefano Stabellini Subject: [PATCH v7 32/50] x86/mm: add hooks for a sparse view of the directmap Date: Thu, 19 Nov 2026 21:07:23 +0000 Message-ID: <20261119-asi-hvm-v7-32-6551b6d5c879@xenproject.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> References: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1795123887822158500 Content-Type: text/plain; charset="utf-8" The directmap maps all of RAM, and every root page-table carries it, so while any guest context runs, all guest memory is reachable from Xen's address space. For ASI, guest contexts are to run on a sparse view of the directmap which maps only what Xen must reach through it at all times, leaving guest memory to transient mappings (map_domain_page()). A later patch builds such a sparse view alongside the full directmap, once the boot allocator has handed its memory to the heap. It is then kept in step: - xenheap allocations are added, and taken out again before the pages are freed. If that fails the pages stay mapped, and are leaked with a warning rather than reused; - memory handed to the heap later (boot modules, .init) is taken out, and is not handed over if that fails; - a PV dom0's initrd, assigned to dom0 in place, is taken out; - page-tables from the boot allocator, which the sparse view maps with every other boot-time allocation, are taken out when idle_pg_table's hierarchy frees them, and are not freed if that fails, so that the sparse view never keeps a page the heap may hand out; - changes to the full directmap through map_pages_to_xen() and modify_xen_mappings() are mirrored. After boot, map_pages_to_xen() maps into the directmap only memory Xen keeps (stack guard and shadow stack pages, firmware tables); memory hot-add maps the new range into the full directmap only, so that the sparse view never needs page-table pages for it. Add a hook at each of these points, and one where the view is to be built; x86's are stubs in a new asm/sparse-directmap.h. map_pages_in() and modify_mappings_in() are no longer static: memory hot-add calls map_pages_in() on idle_pg_table, and the sparse view will use both. Common code gains four hooks for this, arch_heap_pages_init(), arch_xenheap_needs_scrub() and arch_xenheap_pages_{alloc,free}(), no-ops until an architecture implements them. A failed arch_xenheap_pages_alloc() may have mapped part of the pages: alloc_xenheap_pages() then takes them out through arch_xenheap_pages_free(), as a free would, before freeing them, and leaks them if that fails too. alloc_xenheap_pages() accepts pages still awaiting scrubbing (MEMF_no_scrub). That is harmless while the directmap maps all memory anyway, but the sparse view would make such a page's contents, a dying domain's data for instance, reachable from every context on it until overwritten. With a sparse view configured, arch_xenheap_needs_scrub() makes xenheap allocations scrub those pages first, as domain heap allocations do; clean pages need nothing extra. arch_mfns_in_directmap() says whether an MFN range is reachable through the directmap; it now answers for every context, so false once a sparse view is configured. Its one caller, init_node_heap(), then keeps a NUMA node's heap metadata in the xenheap instead of carving it out of the node's memory through the directmap: carved out of a range handed to the heap, it would be left out of the sparse view, which every heap allocation needs it in. The hooks for now are just stubs. No functional change. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Changes in v5: - New in this version, split out of "x86/mm: build and maintain a sparse view of the directmap". --- xen/arch/x86/include/asm/mm.h | 12 +++++- xen/arch/x86/include/asm/sparse-directmap.h | 30 +++++++++++++++ xen/arch/x86/mm.c | 30 ++++++++++++--- xen/arch/x86/pv/dom0_build.c | 4 ++ xen/arch/x86/setup.c | 4 ++ xen/arch/x86/x86_64/mm.c | 10 ++++- xen/common/page_alloc.c | 41 ++++++++++++++++++++- xen/include/xen/mm.h | 21 +++++++++++ 8 files changed, 142 insertions(+), 10 deletions(-) create mode 100644 xen/arch/x86/include/asm/sparse-directmap.h diff --git a/xen/arch/x86/include/asm/mm.h b/xen/arch/x86/include/asm/mm.h index c66969c093..7855a1540f 100644 --- a/xen/arch/x86/include/asm/mm.h +++ b/xen/arch/x86/include/asm/mm.h @@ -7,6 +7,7 @@ #include #include #include +#include #include =20 /* @@ -599,6 +600,11 @@ mfn_t alloc_xen_pagetable(void); void free_xen_pagetable(mfn_t mfn); void *alloc_mapped_pagetable(mfn_t *pmfn); =20 +int map_pages_in(l4_pgentry_t *root, unsigned long virt, mfn_t mfn, + unsigned long nr_mfns, pte_attr_t flags); +int modify_mappings_in(l4_pgentry_t *root, unsigned long s, unsigned long = e, + pte_attr_t nf); + int __sync_local_execstate(void); =20 /* Arch-specific portion of memory_op hypercall. */ @@ -665,12 +671,16 @@ void write_32bit_pse_identmap(uint32_t *l2); =20 /* * x86 maps part of physical memory via the directmap region. - * Return whether the range of MFN falls in the directmap region. + * Return whether the range of MFN falls in the directmap region in every + * context: never, with a sparse directmap view configured. */ static inline bool arch_mfns_in_directmap(unsigned long mfn, unsigned long= nr) { unsigned long eva =3D min(DIRECTMAP_VIRT_END, HYPERVISOR_VIRT_END); =20 + if ( sparse_dmap_configured() ) + return false; + return (mfn + nr) <=3D (virt_to_mfn(eva - 1) + 1); } =20 diff --git a/xen/arch/x86/include/asm/sparse-directmap.h b/xen/arch/x86/inc= lude/asm/sparse-directmap.h new file mode 100644 index 0000000000..53747a98ad --- /dev/null +++ b/xen/arch/x86/include/asm/sparse-directmap.h @@ -0,0 +1,30 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef X86_SPARSE_DIRECTMAP_H +#define X86_SPARSE_DIRECTMAP_H + +#include +#include +#include + +static inline bool sparse_dmap_configured(void) { return false; } +static inline void sparse_dmap_init(void) {} +static inline int sparse_dmap_mirror_map(unsigned long virt, mfn_t mfn, + unsigned long nr, pte_attr_t flag= s) +{ + return 0; +} +static inline int sparse_dmap_mirror_modify(unsigned long s, unsigned long= e, + pte_attr_t nf) +{ + return 0; +} +static inline int sparse_dmap_remove(mfn_t mfn, unsigned long nr) +{ + return 0; +} +static inline int sparse_dmap_drop_pagetable(mfn_t mfn) +{ + return 0; +} + +#endif /* X86_SPARSE_DIRECTMAP_H */ diff --git a/xen/arch/x86/mm.c b/xen/arch/x86/mm.c index 73bf438922..4821893023 100644 --- a/xen/arch/x86/mm.c +++ b/xen/arch/x86/mm.c @@ -128,6 +128,7 @@ #include #include #include +#include #include #include #include @@ -5442,10 +5443,17 @@ mfn_t xen_map_to_mfn(unsigned long va) =20 /* * Free a page-table page that the hierarchy rooted at @root has stopped - * using. + * using. One of idle_pg_table's may be in the sparse directmap view: it = is + * taken out first, under the sparse view's L3 lock inside an L3 lock of + * idle_pg_table's hierarchy (the only order in which the two nest), and + * kept if that fails. The sparse view's own page-tables are not in it, a= nd + * are freed directly: the caller may hold the sparse view's L3 lock. */ static void free_pagetable_in(const l4_pgentry_t *root, mfn_t mfn) { + if ( root =3D=3D idle_pg_table && sparse_dmap_drop_pagetable(mfn) ) + return; + free_xen_pagetable(mfn); } =20 @@ -5453,7 +5461,7 @@ static void free_pagetable_in(const l4_pgentry_t *roo= t, mfn_t mfn) * map_pages_to_xen() in the hierarchy rooted at @root: idle_pg_table's, or * another hierarchy sharing its virtual addresses. */ -static int map_pages_in( +int map_pages_in( l4_pgentry_t *root, unsigned long virt, mfn_t mfn, @@ -5865,7 +5873,12 @@ int map_pages_to_xen( unsigned long nr_mfns, pte_attr_t flags) { - return map_pages_in(idle_pg_table, virt, mfn, nr_mfns, flags); + int rc =3D map_pages_in(idle_pg_table, virt, mfn, nr_mfns, flags); + + if ( !rc ) + rc =3D sparse_dmap_mirror_map(virt, mfn, nr_mfns, flags); + + return rc; } =20 int __init populate_pt_range(unsigned long virt, unsigned long nr_mfns) @@ -5888,8 +5901,8 @@ int __init populate_pt_range(unsigned long virt, unsi= gned long nr_mfns) * modify_mappings_in() does so in the hierarchy rooted at @root, as * map_pages_in() does. */ -static int modify_mappings_in(l4_pgentry_t *root, unsigned long s, - unsigned long e, pte_attr_t nf) +int modify_mappings_in(l4_pgentry_t *root, unsigned long s, + unsigned long e, pte_attr_t nf) { bool locking =3D system_state > SYS_STATE_boot; l3_pgentry_t *pl3e =3D NULL; @@ -6169,7 +6182,12 @@ static int modify_mappings_in(l4_pgentry_t *root, un= signed long s, =20 int modify_xen_mappings(unsigned long s, unsigned long e, pte_attr_t nf) { - return modify_mappings_in(idle_pg_table, s, e, nf); + int rc =3D modify_mappings_in(idle_pg_table, s, e, nf); + + if ( !rc ) + rc =3D sparse_dmap_mirror_modify(s, e, nf); + + return rc; } =20 int destroy_xen_mappings(unsigned long s, unsigned long e) diff --git a/xen/arch/x86/pv/dom0_build.c b/xen/arch/x86/pv/dom0_build.c index d11e38fd20..a08362df16 100644 --- a/xen/arch/x86/pv/dom0_build.c +++ b/xen/arch/x86/pv/dom0_build.c @@ -22,6 +22,7 @@ #include #include #include +#include =20 /* Allow ring-3 access in long mode as guest cannot use ring 1 ... */ #define BASE_PROT (_PAGE_PRESENT|_PAGE_RW|_PAGE_ACCESSED|_PAGE_USER) @@ -650,6 +651,9 @@ static int __init dom0_construct(const struct boot_doma= in *bd) { if ( assign_pages(mfn_to_page(_mfn(initrd_mfn)), count, d, 0) ) BUG(); + /* The initrd is dom0's now: out of the sparse directmap. */ + if ( sparse_dmap_remove(_mfn(initrd_mfn), PFN_UP(initrd_len)) ) + panic("Cannot unmap dom0's initrd\n"); /* * We have mapped the initrd directly into dom0, and assigned = the * pages. Tell the boot_module handling that we've freed it, s= o the diff --git a/xen/arch/x86/setup.c b/xen/arch/x86/setup.c index df7cfcc563..e23ded0c86 100644 --- a/xen/arch/x86/setup.c +++ b/xen/arch/x86/setup.c @@ -54,6 +54,7 @@ #include #include #include +#include #include #include =20 @@ -1946,6 +1947,9 @@ void asmlinkage __init noreturn __start_xen(void) } } =20 + /* The heap now has all memory, and page-tables come from it. */ + sparse_dmap_init(); + bsp_stack =3D cpu_alloc_stack(0); /* Needs to know IDT vs FRED */ if ( !bsp_stack ) panic("No memory for BSP stack\n"); diff --git a/xen/arch/x86/x86_64/mm.c b/xen/arch/x86/x86_64/mm.c index 7b04537066..c5d9dd64a3 100644 --- a/xen/arch/x86/x86_64/mm.c +++ b/xen/arch/x86/x86_64/mm.c @@ -1231,8 +1231,14 @@ int memory_add(unsigned long spfn, unsigned long epf= n, unsigned int pxm) i =3D virt_to_mfn(HYPERVISOR_VIRT_END - 1) + 1; if ( spfn < i ) { - ret =3D map_pages_to_xen((unsigned long)mfn_to_virt(spfn), _mfn(sp= fn), - min(epfn, i) - spfn, PAGE_HYPERVISOR); + /* + * Into the full directmap only, not a sparse directmap view: the + * frame table and M2P carved from the start of the range are + * reached through their own mappings, and the rest goes to the + * heap, which the sparse view leaves out. + */ + ret =3D map_pages_in(idle_pg_table, (unsigned long)mfn_to_virt(spf= n), + _mfn(spfn), min(epfn, i) - spfn, PAGE_HYPERVISO= R); if ( ret ) goto destroy_directmap; } diff --git a/xen/common/page_alloc.c b/xen/common/page_alloc.c index 5243dba29f..2150dcbd5e 100644 --- a/xen/common/page_alloc.c +++ b/xen/common/page_alloc.c @@ -1971,6 +1971,9 @@ static void init_heap_pages( unsigned long i; bool need_scrub =3D scrub_debug; =20 + if ( !arch_heap_pages_init(page_to_mfn(pg), nr_pages) ) + return; + /* * Keep MFN 0 away from the buddy allocator to avoid crossing zone * boundary when merging two buddies. @@ -2540,10 +2543,33 @@ void *alloc_xenheap_pages(unsigned int order, unsig= ned int memflags) if ( !(memflags >> _MEMF_bits) ) memflags |=3D MEMF_bits(xenheap_bits); =20 - pg =3D alloc_domheap_pages(NULL, order, memflags | MEMF_no_scrub); + /* + * Pages still awaiting scrubbing may do as xenheap pages: the directm= ap + * maps them anyway. Not so if an arch's sparse directmap view makes = them + * reachable where the rest of the heap's memory is not. + */ + if ( !arch_xenheap_needs_scrub() ) + memflags |=3D MEMF_no_scrub; + + pg =3D alloc_domheap_pages(NULL, order, memflags); if ( unlikely(pg =3D=3D NULL) ) return NULL; =20 + if ( arch_xenheap_pages_alloc(page_to_mfn(pg), order) ) + { + /* + * The hook may have mapped part of the pages before failing: take + * them out again as a free would, and leak them if that fails too. + */ + if ( arch_xenheap_pages_free(page_to_mfn(pg), order) ) + printk(XENLOG_WARNING + "Leaking xenheap pages %"PRI_mfn"+%u: still mapped\n", + mfn_x(page_to_mfn(pg)), 1U << order); + else + free_domheap_pages(pg, order); + return NULL; + } + for ( i =3D 0; i < (1u << order); i++ ) pg[i].count_info |=3D PGC_xen_heap; =20 @@ -2562,6 +2588,19 @@ void free_xenheap_pages(void *v, unsigned int order) =20 pg =3D virt_to_page(v); =20 + /* + * If the pages cannot be taken out of an arch's sparse directmap view, + * they may remain reachable through it: leak them rather than hand th= em + * out. + */ + if ( arch_xenheap_pages_free(page_to_mfn(pg), order) ) + { + printk(XENLOG_WARNING + "Leaking xenheap pages %"PRI_mfn"+%u: still mapped\n", + mfn_x(page_to_mfn(pg)), 1U << order); + return; + } + for ( i =3D 0; i < (1u << order); i++ ) pg[i].count_info &=3D ~PGC_xen_heap; =20 diff --git a/xen/include/xen/mm.h b/xen/include/xen/mm.h index 8f4bba9491..f9cf7d39c7 100644 --- a/xen/include/xen/mm.h +++ b/xen/include/xen/mm.h @@ -94,6 +94,27 @@ bool scrub_free_pages(void); #define alloc_xenheap_page() (alloc_xenheap_pages(0,0)) #define free_xenheap_page(v) (free_xenheap_pages(v,0)) =20 +/* + * Hooks for an architecture keeping a second, sparse view of its directma= p, + * which maps the xenheap but not the rest of the heap's memory. + */ +static inline bool arch_heap_pages_init(mfn_t mfn, unsigned long nr) +{ + return true; +} +static inline bool arch_xenheap_needs_scrub(void) +{ + return false; +} +static inline int arch_xenheap_pages_alloc(mfn_t mfn, unsigned int order) +{ + return 0; +} +static inline int arch_xenheap_pages_free(mfn_t mfn, unsigned int order) +{ + return 0; +} + /* Free an allocation, and zero the pointer to it. */ #define FREE_XENHEAP_PAGES(p, o) do { \ void *_ptr_ =3D (p); \ --=20 2.56.0 From nobody Mon Nov 23 22:32:25 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=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1795123890; cv=none; d=zohomail.com; s=zohoarc; b=CZkg1lci3ZPX5sczstXwvdXg3MDcTXKAfjnWyVR8iQeijY035EbtLnKtqxjalLfaBzVgstcWffEy/o6ijw1ECecP7/Q9Nrxb+Ygg6FaMuv3oHkPMRHhm8MQYRg5dmnRsGR5IORWZeH+arGwpAX+ny2PsJNis2a71rIPc3cNKg1I= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795123890; h=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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=ZVJ9VF0EjZgHm0CkZPfqu4y07H/L2TWKfjMe4SurFq4=; b=dxfsC6qi3bXVfZX1PZ2vr31mWJhiDUSUQ1D8ynlKhsu3z9CfD1m/W2AsNE+40hY6gcI0cQVLUWkoc204Yc0bi7Kt/oJPenwaprJocXbs7Iz4eDUhqfyllgiN7bbKeBpEHsyTvezj+oMlgTDVFY1f6Olw8bmyOI3kZizaMNUQVfM= 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=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1795123890515745.4031559109376; Thu, 19 Nov 2026 13:31:30 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1497135.1695585 (Exim 4.92) (envelope-from ) id 1xU9iY-0008Jh-Lx; Thu, 19 Nov 2026 21:30:54 +0000 Received: by outflank-mailman (output) from mailman id 1497135.1695585; Thu, 19 Nov 2026 21:30:54 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9iW-0008Be-UI; Thu, 19 Nov 2026 21:30:52 +0000 Received: by outflank-mailman (input) for mailman id 1497135; Thu, 19 Nov 2026 21:30:19 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9hz-0001Dz-Pc for xen-devel@lists.xenproject.org; Thu, 19 Nov 2026 21:30:19 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xU9hz-00000006tJ6-1S9m; Thu, 19 Nov 2026 21:30:19 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xU9Mj-0000000CVBH-2R2g; Thu, 19 Nov 2026 21:08:21 +0000 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" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=ZVJ9VF0EjZgHm0CkZPfqu4y07H/L2TWKfjMe4SurFq4=; b=kk9HYia0agtNoyIMqA5uqOQLM4 JGLh78y84eXXuusZ2TQ9SCdg91l7I7X2gISB3ZhmNzpbZkfELYObWRNsYp0AHf7YIIsQTgQwYkAM2 wOHfkC+gnUhPQZYz1Y3nIszZtD3QYE6KbtrUoJyLoMYrU2pRvvjR2zQoXSVgrjH86ct8=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie , Anthony PERARD , Michal Orzel , Julien Grall , Stefano Stabellini Subject: [PATCH v7 33/50] x86/mm: build and maintain a sparse view of the directmap Date: Thu, 19 Nov 2026 21:07:24 +0000 Message-ID: <20261119-asi-hvm-v7-33-6551b6d5c879@xenproject.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> References: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1795123891857158500 Content-Type: text/plain; charset="utf-8" The directmap maps all of RAM, and every root page-table carries it, so while any guest context runs, all guest memory is reachable from Xen's address space. For ASI, guest contexts are to run on a view of the directmap which maps only what Xen must reach through it at all times, leaving guest memory to transient mappings (map_domain_page()). Rather than remove the directmap for everyone, keep the full directmap as it is, for the idle domain and for guest contexts not using ASI, and build a second, sparse view: page-tables for the same virtual addresses (the part of the directmap PV guests can see, where everything Xen reaches through the directmap lives), mapping a subset of what the full directmap maps, each entry a copy of the full directmap's. Boot and the idle domain keep the full directmap, so nothing in them needs converting. The sparse view maps the xenheap, and memory never handed to the heap allocator (Xen's image, boot-time allocations, firmware tables). It is built once the boot allocator has handed its memory to the heap, from the full directmap minus the ranges the heap was given, plus the xenheap allocations made meanwhile. The page-tables then come from the heap, and no other CPU is up. The sparse view has an L3 for each of its L4 slots from the start, so its L4 entries never change and root page-tables can take copies of them. It is then kept in step through the hooks added by "x86/mm: add hooks for a sparse view of the directmap", which this patch implements for CONFIG_SPARSE_DIRECTMAP. modify_xen_mappings_lite() needs no change: it only changes the mappings of Xen's image, in L4 slot 261, which every root page-table shares with idle_pg_table, outside the range the sparse view replaces. xen_mapping_lookup(), built only with CONFIG_SPARSE_DIRECTMAP, reads an entry of either hierarchy under the L3 table lock. The allocator may still have to scrub a page before returning it; this will be mapped with map_domain_page(). This is fine for HVM domains on the sparse view, as the mapcache is per-vCPU, so no other vCPU can read unscrubbed data. PV domains' mapcache is still domain-wide, so for PV domains on the sparse view, this opens up a window where other vCPUs could potentially speculatively access the unscrubbed data. This would be fixed by having per-vCPU mapcaches for PV domains, or synchronously scrubbing pages on free. Neither are in the scope of the current work. The sparse view is built only when some domain type is to use it, which nothing can request yet: this patch changes nothing on its own. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Changes in v5: - Split: free_pagetable_in() moves to "x86/mm: let Xen's page-table walker operate on a given root", and the hooks with their stubs and callers to the new "x86/mm: add hooks for a sparse view of the directmap". Changes in v4: - Message: "domain type", not "guest type". - Wrap a line that reached 80 columns. - Memory hot-add maps the new range into the full directmap only. - Define opt_sparse_dmap_hwdom here, beside the PV and HVM knobs (Roger). - add_to_sparse_view(): assert that the lookup finds the MFN expected (Roger). - cmp_mfn_range(): explicit return values, as in cmp_node() (Roger). - Comments and variable name changes in sparse_dmap_init()'s loop over the heap's ranges (Roger). - Message: drop "(NUMA node heap metadata)" (Roger). - Message: drop the reference to the directmap removal series (Roger). - Message: modify_xen_mappings_lite() needs no change (Roger). - Message: note the limit of scrubbing at allocation for PV domains (Roger). - Message: only boot and the idle domain keep the full directmap. - Set the NX bit in this patch, rather than adding it in a later patch. Changes in v3: - New in this version. --- xen/arch/x86/Kconfig | 1 + xen/arch/x86/Makefile | 1 + xen/arch/x86/include/asm/mm.h | 4 + xen/arch/x86/include/asm/sparse-directmap.h | 37 ++ xen/arch/x86/mm.c | 74 ++++ xen/arch/x86/sparse-directmap.c | 431 ++++++++++++++++++++ xen/common/Kconfig | 18 + xen/include/xen/mm.h | 14 + 8 files changed, 580 insertions(+) create mode 100644 xen/arch/x86/sparse-directmap.c diff --git a/xen/arch/x86/Kconfig b/xen/arch/x86/Kconfig index 170b4d63dc..1d599bf467 100644 --- a/xen/arch/x86/Kconfig +++ b/xen/arch/x86/Kconfig @@ -32,6 +32,7 @@ config X86 select HAS_SCHED_GRANULARITY select HAS_SHARED_INFO imply HAS_SOFT_RESET + select HAS_SPARSE_DIRECTMAP select HAS_UBSAN select HAS_VMAP select HAS_VPCI if HVM diff --git a/xen/arch/x86/Makefile b/xen/arch/x86/Makefile index 2be90ea098..86adf042e9 100644 --- a/xen/arch/x86/Makefile +++ b/xen/arch/x86/Makefile @@ -63,6 +63,7 @@ obj-y +=3D shutdown.o obj-y +=3D smp.o obj-y +=3D smpboot.o obj-y +=3D spec_ctrl.o +obj-$(CONFIG_SPARSE_DIRECTMAP) +=3D sparse-directmap.o obj-y +=3D srat.o obj-y +=3D string.o obj-$(CONFIG_SYSCTL) +=3D sysctl.o diff --git a/xen/arch/x86/include/asm/mm.h b/xen/arch/x86/include/asm/mm.h index 7855a1540f..9374908443 100644 --- a/xen/arch/x86/include/asm/mm.h +++ b/xen/arch/x86/include/asm/mm.h @@ -604,6 +604,10 @@ int map_pages_in(l4_pgentry_t *root, unsigned long vir= t, mfn_t mfn, unsigned long nr_mfns, pte_attr_t flags); int modify_mappings_in(l4_pgentry_t *root, unsigned long s, unsigned long = e, pte_attr_t nf); +#ifdef CONFIG_SPARSE_DIRECTMAP +mfn_t xen_mapping_lookup(l4_pgentry_t *root, unsigned long va, + pte_attr_t *flags, unsigned long *nr); +#endif =20 int __sync_local_execstate(void); =20 diff --git a/xen/arch/x86/include/asm/sparse-directmap.h b/xen/arch/x86/inc= lude/asm/sparse-directmap.h index 53747a98ad..ea14d44334 100644 --- a/xen/arch/x86/include/asm/sparse-directmap.h +++ b/xen/arch/x86/include/asm/sparse-directmap.h @@ -6,6 +6,41 @@ #include #include =20 +#ifdef CONFIG_SPARSE_DIRECTMAP + +/* + * The part of the directmap the sparse view covers: the part PV guests can + * see, which is where everything Xen reaches through the directmap lives. + * SPARSE_DMAP_END_MFN is the first MFN past it. + */ +#define SPARSE_DMAP_START DIRECTMAP_VIRT_START +#define SPARSE_DMAP_END HYPERVISOR_VIRT_END +#define SPARSE_DMAP_END_MFN (virt_to_mfn(SPARSE_DMAP_END - 1) + 1) + +/* + * Which domains use the sparse view: PV domains and HVM domains other than + * the hardware domain, and the hardware domain, PV or PVH. + */ +extern bool opt_sparse_dmap_pv, opt_sparse_dmap_hvm, opt_sparse_dmap_hwdom; + +/* Root of the sparse view, once built (NULL before, or if not configured)= . */ +extern l4_pgentry_t *sparse_dmap_root; + +static inline bool sparse_dmap_configured(void) +{ + return opt_sparse_dmap_pv || opt_sparse_dmap_hvm || opt_sparse_dmap_hw= dom; +} + +void sparse_dmap_init(void); +int sparse_dmap_mirror_map(unsigned long virt, mfn_t mfn, unsigned long nr, + pte_attr_t flags); +int sparse_dmap_mirror_modify(unsigned long s, unsigned long e, + pte_attr_t nf); +int sparse_dmap_remove(mfn_t mfn, unsigned long nr); +int sparse_dmap_drop_pagetable(mfn_t mfn); + +#else /* !CONFIG_SPARSE_DIRECTMAP */ + static inline bool sparse_dmap_configured(void) { return false; } static inline void sparse_dmap_init(void) {} static inline int sparse_dmap_mirror_map(unsigned long virt, mfn_t mfn, @@ -27,4 +62,6 @@ static inline int sparse_dmap_drop_pagetable(mfn_t mfn) return 0; } =20 +#endif /* CONFIG_SPARSE_DIRECTMAP */ + #endif /* X86_SPARSE_DIRECTMAP_H */ diff --git a/xen/arch/x86/mm.c b/xen/arch/x86/mm.c index 4821893023..9ac7ce656b 100644 --- a/xen/arch/x86/mm.c +++ b/xen/arch/x86/mm.c @@ -5441,6 +5441,80 @@ mfn_t xen_map_to_mfn(unsigned long va) return ret; } =20 +#ifdef CONFIG_SPARSE_DIRECTMAP +/* + * Look @va up in the hierarchy rooted at @root, without allocating anythi= ng. + * Returns the MFN mapped at @va, or INVALID_MFN, with the mapping's flags= in + * L1 form in *flags, and in *nr the number of pages from @va to the end of + * the entry that maps @va (or that finds it unmapped). + */ +mfn_t xen_mapping_lookup(l4_pgentry_t *root, unsigned long va, + pte_attr_t *flags, unsigned long *nr) +{ + bool locking =3D system_state > SYS_STATE_boot; + l4_pgentry_t l4e =3D root[l4_table_offset(va)]; + const l3_pgentry_t *pl3e =3D NULL; + const l2_pgentry_t *pl2e =3D NULL; + const l1_pgentry_t *pl1e =3D NULL; + struct page_info *l3page; + unsigned int shift =3D L4_PAGETABLE_SHIFT; + mfn_t ret =3D INVALID_MFN; + + L3T_INIT(l3page); + *flags =3D 0; + + if ( !(l4e_get_flags(l4e) & _PAGE_PRESENT) ) + goto out; + + pl3e =3D map_l3t_from_l4e(l4e) + l3_table_offset(va); + l3page =3D l4e_get_page(l4e); + L3T_LOCK(l3page); + + shift =3D L3_PAGETABLE_SHIFT; + if ( !(l3e_get_flags(*pl3e) & _PAGE_PRESENT) ) + goto out; + if ( l3e_get_flags(*pl3e) & _PAGE_PSE ) + { + /* The superpage PAT bit sits among the address bits. */ + *flags =3D lNf_to_l1f(l3e_get_flags(*pl3e)) | + (l3e_get_intpte(*pl3e) & _PAGE_PSE_PAT ? _PAGE_PAT : 0); + ret =3D _mfn((l3e_get_pfn(*pl3e) & + ~((1UL << (2 * PAGETABLE_ORDER)) - 1)) + + (PFN_DOWN(va) & ((1UL << (2 * PAGETABLE_ORDER)) - 1))); + goto out; + } + + pl2e =3D map_l2t_from_l3e(*pl3e) + l2_table_offset(va); + shift =3D L2_PAGETABLE_SHIFT; + if ( !(l2e_get_flags(*pl2e) & _PAGE_PRESENT) ) + goto out; + if ( l2e_get_flags(*pl2e) & _PAGE_PSE ) + { + *flags =3D lNf_to_l1f(l2e_get_flags(*pl2e)) | + (l2e_get_intpte(*pl2e) & _PAGE_PSE_PAT ? _PAGE_PAT : 0); + ret =3D _mfn((l2e_get_pfn(*pl2e) & ~((1UL << PAGETABLE_ORDER) - 1)= ) + + l1_table_offset(va)); + goto out; + } + + pl1e =3D map_l1t_from_l2e(*pl2e) + l1_table_offset(va); + shift =3D PAGE_SHIFT; + if ( l1e_get_flags(*pl1e) & _PAGE_PRESENT ) + { + *flags =3D l1e_get_flags(*pl1e); + ret =3D l1e_get_mfn(*pl1e); + } + + out: + L3T_UNLOCK(l3page); + unmap_domain_page(pl1e); + unmap_domain_page(pl2e); + unmap_domain_page(pl3e); + *nr =3D ((((va >> shift) + 1) << shift) - va) >> PAGE_SHIFT; + return ret; +} +#endif /* CONFIG_SPARSE_DIRECTMAP */ + /* * Free a page-table page that the hierarchy rooted at @root has stopped * using. One of idle_pg_table's may be in the sparse directmap view: it = is diff --git a/xen/arch/x86/sparse-directmap.c b/xen/arch/x86/sparse-directma= p.c new file mode 100644 index 0000000000..d35a8d6979 --- /dev/null +++ b/xen/arch/x86/sparse-directmap.c @@ -0,0 +1,431 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * A sparse view of the directmap. + * + * Xen maps all of RAM at DIRECTMAP_VIRT_START, and every root page-table + * carries those mappings. The sparse view is a second set of page-tables + * for the same virtual addresses (the part of the directmap PV guests can + * see), mapping only what Xen needs to reach through the directmap at all + * times: + * + * - Memory never handed to the heap: Xen's image, boot-time allocations, + * firmware tables. Not a PV dom0's initrd, used in place, nor the frame + * table and M2P pages carved from hot-added memory. + * - Memory handed to the heap, then allocated to the xenheap. + * + * The rest of the heap's memory, where guest memory comes from, is not + * mapped. Every mapping in the sparse view is either identical to the + * full directmap's at the same address, or empty. + * + * Guest contexts selected on the command line run on root page-tables + * carrying the sparse view's L3s in the directmap slots. + */ + +#include +#include +#include +#include +#include +#include + +#include +#include + +bool __ro_after_init opt_sparse_dmap_pv; +bool __ro_after_init opt_sparse_dmap_hvm; +bool __ro_after_init opt_sparse_dmap_hwdom; + +/* Root of the sparse view: only ever walked and copied from, never loaded= . */ +static l4_pgentry_t __aligned(PAGE_SIZE) sparse_l4[L4_PAGETABLE_ENTRIES]; +l4_pgentry_t *__ro_after_init sparse_dmap_root; + +/* + * Before the sparse view is built: the MFN ranges handed to the heap, whi= ch + * it leaves out, and the xenheap allocations made since, which it puts ba= ck. + */ +struct mfn_range { + unsigned long s, e; +}; +static struct mfn_range early_heap[256]; +static unsigned int nr_early_heap; +static struct mfn_range early_xenheap[2 * MAX_NUMNODES]; +static unsigned int nr_early_xenheap; +static bool early_overflow; + +/* + * Clip [*mfn, *mfn + *nr) to what the sparse view covers; false if nothin= g is + * left. + */ +static bool clip_to_sparse_view(unsigned long *mfn, unsigned long *nr) +{ + unsigned long end =3D SPARSE_DMAP_END_MFN; + + if ( *mfn >=3D end ) + return false; + if ( *nr > end - *mfn ) + *nr =3D end - *mfn; + + return *nr; +} + +static void early_range_add(struct mfn_range *r, unsigned int *nr, + unsigned int max, unsigned long s, unsigned lo= ng e) +{ + if ( *nr && r[*nr - 1].e =3D=3D s ) + r[*nr - 1].e =3D e; + else if ( *nr < max ) + r[(*nr)++] =3D (struct mfn_range){ s, e }; + else + early_overflow =3D true; +} + +static void early_range_del(struct mfn_range *r, unsigned int *nr, + unsigned long s, unsigned long e) +{ + unsigned int i; + + for ( i =3D 0; i < *nr; i++ ) + if ( r[i].s =3D=3D s && r[i].e =3D=3D e ) + { + r[i] =3D r[--*nr]; + return; + } +} + +/* + * Copy the full directmap's mappings of MFNs [mfn, mfn + nr) into the spa= rse + * view. + */ +static int add_to_sparse_view(l4_pgentry_t *root, unsigned long mfn, + unsigned long nr) +{ + unsigned long start =3D (unsigned long)mfn_to_virt(mfn); + unsigned long va =3D start, end =3D start + (nr << PAGE_SHIFT); + + while ( va < end ) + { + pte_attr_t flags; + unsigned long n; + mfn_t va_mfn =3D xen_mapping_lookup(idle_pg_table, va, &flags, &n); + int rc; + + ASSERT(mfn_eq(va_mfn, INVALID_MFN) || + mfn_x(va_mfn) =3D=3D mfn + ((va - start) >> PAGE_SHIFT)); + n =3D min(n, (end - va) >> PAGE_SHIFT); + if ( !mfn_eq(va_mfn, INVALID_MFN) && + (rc =3D map_pages_in(root, va, va_mfn, n, flags)) ) + return rc; + va +=3D n << PAGE_SHIFT; + } + + return 0; +} + +static int remove_from_sparse_view(unsigned long mfn, unsigned long nr) +{ + unsigned long va =3D (unsigned long)mfn_to_virt(mfn); + + return modify_mappings_in(sparse_dmap_root, va, va + (nr << PAGE_SHIFT= ), + _PAGE_NONE); +} + +/* Memory is about to be handed to the heap allocator. */ +bool arch_heap_pages_init(mfn_t mfn, unsigned long nr) +{ + unsigned long s =3D mfn_x(mfn); + + if ( !sparse_dmap_configured() || !clip_to_sparse_view(&s, &nr) ) + return true; + + if ( !sparse_dmap_root ) + { + early_range_add(early_heap, &nr_early_heap, ARRAY_SIZE(early_heap), + s, s + nr); + return true; + } + + if ( remove_from_sparse_view(s, nr) ) + { + printk(XENLOG_ERR + "sparse directmap: cannot unmap MFNs %#lx-%#lx, not freeing= \n", + s, s + nr - 1); + return false; + } + + return true; +} + +/* + * Xenheap pages enter the sparse view, which guest contexts run on: those + * still holding a previous owner's data must be scrubbed first. + */ +bool arch_xenheap_needs_scrub(void) +{ + return sparse_dmap_configured(); +} + +/* Pages have been allocated from the heap as xenheap pages. */ +int arch_xenheap_pages_alloc(mfn_t mfn, unsigned int order) +{ + unsigned long s =3D mfn_x(mfn), nr =3D 1UL << order; + + if ( !sparse_dmap_configured() ) + return 0; + + /* Xenheap pages must be reachable through the sparse view. */ + BUG_ON(s + nr > SPARSE_DMAP_END_MFN); + + if ( !sparse_dmap_root ) + { + early_range_add(early_xenheap, &nr_early_xenheap, + ARRAY_SIZE(early_xenheap), s, s + nr); + return 0; + } + + return add_to_sparse_view(sparse_dmap_root, s, nr); +} + +/* + * Xenheap pages are about to be freed. A failure means the pages may sti= ll + * be reachable through the sparse view, and must not be reused. + */ +int arch_xenheap_pages_free(mfn_t mfn, unsigned int order) +{ + unsigned long s =3D mfn_x(mfn), nr =3D 1UL << order; + + if ( !sparse_dmap_configured() ) + return 0; + + BUG_ON(s + nr > SPARSE_DMAP_END_MFN); + + if ( !sparse_dmap_root ) + { + early_range_del(early_xenheap, &nr_early_xenheap, s, s + nr); + return 0; + } + + return remove_from_sparse_view(s, nr); +} + +/* Memory leaves Xen's hands without passing through the heap. */ +int sparse_dmap_remove(mfn_t mfn, unsigned long nr) +{ + return arch_heap_pages_init(mfn, nr) ? 0 : -ENOMEM; +} + +/* + * A page-table page of idle_pg_table's hierarchy is about to be freed. O= ne + * the boot allocator provided is in the sparse view, as every boot-time + * allocation is: take it out before the heap may hand it on. One allocat= ed + * since came from the heap and is not in the sparse view, which a lookup + * finds without changing, or flushing, anything. Non-zero: the page may + * still be mapped, and must not be freed. + */ +int sparse_dmap_drop_pagetable(mfn_t mfn) +{ + unsigned long s =3D mfn_x(mfn), nr =3D 1, n; + pte_attr_t flags; + int rc; + + if ( !sparse_dmap_root || !clip_to_sparse_view(&s, &nr) || + mfn_eq(xen_mapping_lookup(sparse_dmap_root, + (unsigned long)mfn_to_virt(s), &flags, = &n), + INVALID_MFN) ) + return 0; + + rc =3D remove_from_sparse_view(s, 1); + if ( rc ) + printk(XENLOG_WARNING + "Leaking page-table page %"PRI_mfn": still in the sparse di= rectmap\n", + mfn_x(mfn)); + + return rc; +} + +int sparse_dmap_mirror_map(unsigned long virt, mfn_t mfn, unsigned long nr, + pte_attr_t flags) +{ + if ( !sparse_dmap_root || virt < SPARSE_DMAP_START || + virt >=3D SPARSE_DMAP_END ) + return 0; + + nr =3D min(nr, (SPARSE_DMAP_END - virt) >> PAGE_SHIFT); + + return map_pages_in(sparse_dmap_root, virt, mfn, nr, flags); +} + +int sparse_dmap_mirror_modify(unsigned long s, unsigned long e, + pte_attr_t nf) +{ + unsigned long va; + + if ( !sparse_dmap_root || e <=3D SPARSE_DMAP_START || + s >=3D SPARSE_DMAP_END ) + return 0; + + s =3D max(s, SPARSE_DMAP_START); + e =3D min(e, SPARSE_DMAP_END); + + if ( !(nf & _PAGE_PRESENT) ) + return modify_mappings_in(sparse_dmap_root, s, e, nf); + + /* + * A permission change applies where the sparse view maps anything: it + * never creates mappings, and the sparse view need not map all of [s,= e). + */ + for ( va =3D s; va < e; ) + { + pte_attr_t flags; + unsigned long n; + mfn_t mfn =3D xen_mapping_lookup(sparse_dmap_root, va, &flags, &n); + int rc; + + n =3D min(n, (e - va) >> PAGE_SHIFT); + if ( !mfn_eq(mfn, INVALID_MFN) && + (rc =3D modify_mappings_in(sparse_dmap_root, va, + va + (n << PAGE_SHIFT), nf)) ) + return rc; + va +=3D n << PAGE_SHIFT; + } + + return 0; +} + +static int __init cf_check cmp_mfn_range(const void *a, const void *b) +{ + const struct mfn_range *l =3D a, *r =3D b; + + if ( l->s > r->s ) + return 1; + if ( l->s < r->s ) + return -1; + + return 0; +} + +static void __init cf_check swap_mfn_range(void *a, void *b) +{ + SWAP(*(struct mfn_range *)a, *(struct mfn_range *)b); +} + +/* + * Build the sparse view: the full directmap, minus the ranges handed to t= he + * heap so far, plus the xenheap allocations made from them. Called once = the + * heap has taken over from the boot allocator, so that the page-tables co= me + * from the heap, and before any other CPU is up. + */ +void __init sparse_dmap_init(void) +{ + unsigned long va, mapped =3D 0; + unsigned int i, heap_idx; + + if ( !sparse_dmap_configured() ) + return; + + if ( early_overflow ) + { + printk(XENLOG_WARNING + "Sparse directmap: too many early heap ranges, disabled\n"); + opt_sparse_dmap_pv =3D opt_sparse_dmap_hvm =3D opt_sparse_dmap_hwd= om =3D + false; + return; + } + + /* An L3 for every slot, so that the L4 entries never change. */ + for ( va =3D SPARSE_DMAP_START; va < SPARSE_DMAP_END; + va +=3D 1UL << L4_PAGETABLE_SHIFT ) + { + mfn_t l3mfn; + void *l3t =3D alloc_mapped_pagetable(&l3mfn); + + if ( !l3t ) + panic("Sparse directmap: out of memory\n"); + unmap_domain_page(l3t); + sparse_l4[l4_table_offset(va)] =3D + l4e_from_mfn(l3mfn, __PAGE_HYPERVISOR); + } + + sort(early_heap, nr_early_heap, sizeof(*early_heap), cmp_mfn_range, + swap_mfn_range); + + /* + * The full directmap, leaf by leaf, minus the ranges added to the + * heap. Leaves come in address order, which is MFN order in the + * directmap, and the heap ranges are sorted and disjoint, so + * heap_idx only moves forward. + */ + for ( va =3D SPARSE_DMAP_START, heap_idx =3D 0; va < SPARSE_DMAP_END; ) + { + pte_attr_t flags; + unsigned long n, s, e; + + /* Get information about the leaf PTE at va. */ + mfn_t mfn =3D xen_mapping_lookup(idle_pg_table, va, &flags, &n); + + /* Clip the va range we're considering to SPARSE_DMAP_END. */ + n =3D min(n, (SPARSE_DMAP_END - va) >> PAGE_SHIFT); + if ( mfn_eq(mfn, INVALID_MFN) ) + { + va +=3D n << PAGE_SHIFT; + continue; + } + + /* + * Walk through the range [mfn, mfn + n) looking for ranges not + * handed to the heap. + */ + for ( s =3D mfn_x(mfn), e =3D s + n; s < e; ) + { + unsigned long upto =3D e; + + /* Step past heap ranges that end at or before s. */ + while ( heap_idx < nr_early_heap && early_heap[heap_idx].e <= =3D s ) + heap_idx++; + + /* s lies in a heap range: skip to its end. */ + if ( heap_idx < nr_early_heap && early_heap[heap_idx].s <=3D s= ) + { + s =3D min(e, early_heap[heap_idx].e); + continue; + } + + /* Map up to the next heap range, or to the end of the leaf. */ + if ( heap_idx < nr_early_heap ) + upto =3D min(e, early_heap[heap_idx].s); + + if ( map_pages_in(sparse_l4, va + ((s - mfn_x(mfn)) << PAGE_SH= IFT), + _mfn(s), upto - s, flags) ) + panic("Sparse directmap: out of memory\n"); + mapped +=3D upto - s; + s =3D upto; + } + + va +=3D n << PAGE_SHIFT; + } + + /* + * Now add back in ranges from the heap which were allocated to + * the xenheap. + */ + for ( i =3D 0; i < nr_early_xenheap; i++ ) + { + unsigned long nr =3D early_xenheap[i].e - early_xenheap[i].s; + + if ( add_to_sparse_view(sparse_l4, early_xenheap[i].s, nr) ) + panic("Sparse directmap: out of memory\n"); + mapped +=3D nr; + } + + /* + * Nothing in the directmap is executed: its slots are NX, as the full + * directmap's are. + */ + if ( cpu_has_nx ) + for ( i =3D l4_table_offset(SPARSE_DMAP_START); + i < l4_table_offset(SPARSE_DMAP_END); i++ ) + l4e_add_flags(sparse_l4[i], _PAGE_NX_BIT); + + sparse_dmap_root =3D sparse_l4; + + printk(XENLOG_INFO "Sparse directmap: %lu pages mapped\n", mapped); +} diff --git a/xen/common/Kconfig b/xen/common/Kconfig index a619f2cef1..759f2caa45 100644 --- a/xen/common/Kconfig +++ b/xen/common/Kconfig @@ -170,6 +170,9 @@ config HAS_STATIC_MEMORY config HAS_SOFT_RESET bool =20 +config HAS_SPARSE_DIRECTMAP + bool + config HAS_STACK_PROTECTOR bool =20 @@ -302,6 +305,21 @@ config SPECULATIVE_HARDEN_LOCK This option is disabled by default at run time, and needs to be enabled on the command line. =20 + +config SPARSE_DIRECTMAP + bool "Sparse directmap view" + depends on HAS_SPARSE_DIRECTMAP + help + Build support for a sparse view of the directmap, alongside the + full one: it maps only the memory Xen needs to reach at all times + (the Xen heap, Xen's own image, firmware tables), while guest + memory is reached through transient mappings. Guest contexts + selected with the `asi=3D` command line option run on the sparse + directmap view, narrowing the memory reachable from Xen's address + space while they run, and hence the memory disclosure surface of + speculative side channels. + + If unsure, say N. endmenu =20 menu "Other hardening" diff --git a/xen/include/xen/mm.h b/xen/include/xen/mm.h index f9cf7d39c7..719d265130 100644 --- a/xen/include/xen/mm.h +++ b/xen/include/xen/mm.h @@ -98,6 +98,19 @@ bool scrub_free_pages(void); * Hooks for an architecture keeping a second, sparse view of its directma= p, * which maps the xenheap but not the rest of the heap's memory. */ +#ifdef CONFIG_SPARSE_DIRECTMAP +/* Memory is about to be handed to the heap. False: don't, keep it out. */ +bool arch_heap_pages_init(mfn_t mfn, unsigned long nr); +/* Xenheap pages are to be allocated. True: scrub those needing it first.= */ +bool arch_xenheap_needs_scrub(void); +/* + * Xenheap pages were allocated. Non-zero: failed, possibly part-way; the + * pages go through arch_xenheap_pages_free() before being freed again. + */ +int arch_xenheap_pages_alloc(mfn_t mfn, unsigned int order); +/* Xenheap pages are to be freed. Non-zero: failed, don't reuse them. */ +int arch_xenheap_pages_free(mfn_t mfn, unsigned int order); +#else static inline bool arch_heap_pages_init(mfn_t mfn, unsigned long nr) { return true; @@ -114,6 +127,7 @@ static inline int arch_xenheap_pages_free(mfn_t mfn, un= signed int order) { return 0; } +#endif =20 /* Free an allocation, and zero the pointer to it. */ #define FREE_XENHEAP_PAGES(p, o) do { \ --=20 2.56.0 From nobody Mon Nov 23 22:32:25 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=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1795123840; cv=none; d=zohomail.com; s=zohoarc; b=ecb0ciqjKb9zhHp3Ak8mpOYYNcn6984QvHmvNsCY1uzjTuBWDYHUuC3T0ZzYJ52xFSrw19FcDBnAaKL3bJNYk55fKfF3aEHaCjakDaErqgTlX47shCCMuisMOk7xc+OvgMIrHxMBn5ICuRzwLWGS1w0ZYt/7RskBWRPRUKL5Tts= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795123840; h=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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=QyEx/2ClqK0nemyLsXAboGn35CnE2TUKWS0G7IK3h+A=; b=VHO0lJYxGeuxwiAQPWsoFaTM6Ph7DePhLzMoKW8fNeqK1+UIFMbtDoGLLYe6IIEAuN/J9D5vbACvCuPwH8hhODD0cnJZbLuopd1NdCuWqX8WixunJmThWtOopJMxniWRYIz3mxcqz4J8AmE8wfyZRX70kEC42BcFHX/NbD+Oj48= 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=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1795123840776877.4797906539407; Thu, 19 Nov 2026 13:30:40 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1497114.1695406 (Exim 4.92) (envelope-from ) id 1xU9i2-0001eV-3E; Thu, 19 Nov 2026 21:30:22 +0000 Received: by outflank-mailman (output) from mailman id 1497114.1695406; Thu, 19 Nov 2026 21:30:22 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9i1-0001ca-TB; Thu, 19 Nov 2026 21:30:21 +0000 Received: by outflank-mailman (input) for mailman id 1497114; Thu, 19 Nov 2026 21:30:19 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9hz-0001C5-3R for xen-devel@lists.xenproject.org; Thu, 19 Nov 2026 21:30:19 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xU9hy-00000006tGX-2esC; Thu, 19 Nov 2026 21:30:18 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xU9Mk-0000000CVBH-28Db; Thu, 19 Nov 2026 21:08:22 +0000 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" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=QyEx/2ClqK0nemyLsXAboGn35CnE2TUKWS0G7IK3h+A=; b=OJkDmQyl4JzuRa3Z5JI3gdg9Io 8yKQGA8M6K8HYadWf49t9lDh4UI1nu3etvvO5D5JVJ6VYqg95h9tguHcKylnQc4adVOQZu8TM9ur8 ReXBVfmuzVQbh068FlXRJdwY46Z6NyoRd5zQFlzm1eLxf4HtxVO5/OK/nCvg6UY9+K6I=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie Subject: [PATCH v7 34/50] x86/domain_page: key the directmap fast paths on the loaded view Date: Thu, 19 Nov 2026 21:07:25 +0000 Message-ID: <20261119-asi-hvm-v7-34-6551b6d5c879@xenproject.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> References: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1795123841390158500 Content-Type: text/plain; charset="utf-8" map_domain_page() returns directmap addresses whenever it can: for any MFN the directmap covers in release builds, and for vCPUs without a mapcache. Once guest contexts may run on the sparse view of the directmap, modify this to take the fast path returning the directmap address only when the full directmap is loaded. The release-build shortcut ahead of the vCPU lookup looks at the vCPU whose page-tables are loaded, without completing a lazy context switch: if that vCPU is not on the sparse view, its page-tables map the page through the directmap, and completing the switch would only move the CPU to the idle vCPU's page-tables, which map it too. A vCPU on the sparse view always has a mapcache: HVM ones through per-vCPU page-tables, PV ones through the domain-wide mapcache, which mapcache_domain_init() now sets up for a domain using the sparse view in release builds too, rather than only when the directmap does not cover all memory. map_domain_page_global() mappings must be valid in every context: use vmap() for all of them once a sparse view is configured, as arch_mfns_in_directmap() then says. Add d->arch.asi.sparse_dmap for this; nothing sets it yet. No functional change. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Changes in v4: - d->arch.sparse_dmap is now a one-bit field of d->arch.asi, beside vcpu_pt. - Wrap a line that reached 80 columns. - Drop sparse_dmap_active: d->arch.asi.sparse_dmap alone says whether a domain's root page-tables carry the sparse view (Roger). Builds without CONFIG_SPARSE_DIRECTMAP compile the test out, as before. - Drop the second directmap check after the vCPU lookup: the !cache->inuse case covers it. Changes in v3: - New in this version. --- xen/arch/x86/domain_page.c | 34 ++++++++++++++++++++++++++++--- xen/arch/x86/include/asm/domain.h | 3 +++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/xen/arch/x86/domain_page.c b/xen/arch/x86/domain_page.c index 7bd6399ca6..86a91c3571 100644 --- a/xen/arch/x86/domain_page.c +++ b/xen/arch/x86/domain_page.c @@ -70,6 +70,13 @@ static inline struct vcpu *mapcache_current_vcpu(void) return pgtable_current_vcpu(); } =20 +/* Whether @v's page-tables carry the sparse view of the directmap. */ +static bool on_sparse_dmap(const struct vcpu *v) +{ + return IS_ENABLED(CONFIG_SPARSE_DIRECTMAP) && + v->domain->arch.asi.sparse_dmap; +} + /* * The accounting for v's mapcache: its own for a vCPU-PT domain, the doma= in's * otherwise, in which case *dcache is set so the caller takes the domain-= wide @@ -149,9 +156,19 @@ void *map_domain_page(mfn_t mfn) =20 #ifdef NDEBUG if ( mfn_x(mfn) <=3D PFN_DOWN(__pa(HYPERVISOR_VIRT_END - 1)) ) - return mfn_to_virt(mfn_x(mfn)); + { + const struct vcpu *pt_vcpu =3D this_cpu(pgtable_vcpu); + + /* + * The page-tables loaded map @mfn through the directmap, unless t= hey + * carry the sparse view. + */ + if ( !pt_vcpu || !on_sparse_dmap(pt_vcpu) ) + return mfn_to_virt(mfn_x(mfn)); + } #endif =20 + /* No vCPU (early boot), or no mapcache: the full directmap is loaded.= */ v =3D mapcache_current_vcpu(); if ( !v ) return mfn_to_virt(mfn_x(mfn)); @@ -159,7 +176,10 @@ void *map_domain_page(mfn_t mfn) cache =3D vcpu_mapcache(v, &dcache); vcache =3D &v->arch.mapcache; if ( !cache->inuse ) + { + ASSERT(!on_sparse_dmap(v)); return mfn_to_virt(mfn_x(mfn)); + } =20 perfc_incr(map_domain_page_count); =20 @@ -425,7 +445,11 @@ void mapcache_domain_init(struct domain *d) ASSERT(is_pv_domain(d)); =20 #ifdef NDEBUG - if ( !mem_hotplug && max_page <=3D PFN_DOWN(__pa(HYPERVISOR_VIRT_END -= 1)) ) + /* + * Not needed when the directmap loaded for the domain covers everythi= ng. + */ + if ( !d->arch.asi.sparse_dmap && !mem_hotplug && + max_page <=3D PFN_DOWN(__pa(HYPERVISOR_VIRT_END - 1)) ) return; #endif =20 @@ -527,8 +551,12 @@ void *map_domain_page_global(mfn_t mfn) system_state < SYS_STATE_active) || local_irq_is_enabled())); =20 + /* + * Global mappings must hold in every context: a sparse view of the + * directmap rules the directmap out. + */ #ifdef NDEBUG - if ( mfn_x(mfn) <=3D PFN_DOWN(__pa(HYPERVISOR_VIRT_END - 1)) ) + if ( arch_mfns_in_directmap(mfn_x(mfn), 1) ) return mfn_to_virt(mfn_x(mfn)); #endif =20 diff --git a/xen/arch/x86/include/asm/domain.h b/xen/arch/x86/include/asm/d= omain.h index 5084848730..c4b06785aa 100644 --- a/xen/arch/x86/include/asm/domain.h +++ b/xen/arch/x86/include/asm/domain.h @@ -487,6 +487,9 @@ struct arch_domain struct { /* Give each vCPU its own per-domain area (HVM only). */ bool vcpu_pt:1; + + /* The domain's root page-tables carry the sparse directmap view. = */ + bool sparse_dmap:1; } asi; =20 /* Emulated devices enabled bitmap. */ --=20 2.56.0 From nobody Mon Nov 23 22:32:25 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=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1795123841; cv=none; d=zohomail.com; s=zohoarc; b=JFLbPy1P/lNNAdwCUgycFq7Sf4///hD1DNaN3lH8h/pNxSaTSJHINU01PW2YHvKcYGfYVEaDiXRb5j17UhRSwVj3yjGYqbKL+c0qZKnebXVIjatEQKsLEw44QtVgR4P1/8CGY1VvQj6Kusk/grBiU0Drwhh1ph6hxC6uriU/Nj4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795123841; h=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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=lVipAUCmGONwCt5LCT1zFu1hK5zv9sdEbfda0XvBF2E=; b=MHDXzJO+2Gsyl0yxJdmCcgMsdLQzg4FQHaCO4zG3n0J0HAIuQLOF21e5Kxz93Aa1siuo9x2LGBciUSaj6TN7wBOxrebSZ1puSGQTsgr5mWW8+k0CVVADBrA+D0QRP+UNMQV/6DMNJS2D4JWjL+UHo824hGt0nsJy4r1LE6/WFIU= 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=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 179512384158672.34867478151409; Thu, 19 Nov 2026 13:30:41 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1497118.1695439 (Exim 4.92) (envelope-from ) id 1xU9i5-0002Pb-16; Thu, 19 Nov 2026 21:30:25 +0000 Received: by outflank-mailman (output) from mailman id 1497118.1695439; Thu, 19 Nov 2026 21:30:24 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9i4-0002On-Nv; Thu, 19 Nov 2026 21:30:24 +0000 Received: by outflank-mailman (input) for mailman id 1497118; Thu, 19 Nov 2026 21:30:19 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9hz-0001CY-8R for xen-devel@lists.xenproject.org; Thu, 19 Nov 2026 21:30:19 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xU9hy-00000006tGD-22Sn; Thu, 19 Nov 2026 21:30:18 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xU9Ml-0000000CVBH-2DZE; Thu, 19 Nov 2026 21:08:23 +0000 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" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=lVipAUCmGONwCt5LCT1zFu1hK5zv9sdEbfda0XvBF2E=; b=M+vliMFkAACrgh2n4cZhtCvQun Nh6AJ21M1XdsZtAB05hpOKXO2QxzM9/YIbzOAZiNOBM53e6E3NQoymSsVm6G7kGKXtWvTEVSXCgl2 RvCMHajBUkA0Xol2jG+w3xbmscV05gwg8h3uE9DLWTsB6k0rgIH5rikU9W3A0SEaXwhE=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie , Jason Andryuk Subject: [PATCH v7 35/50] x86/domain_page: clear a mapcache slot at its last unmap Date: Thu, 19 Nov 2026 21:07:26 +0000 Message-ID: <20261119-asi-hvm-v7-35-6551b6d5c879@xenproject.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> References: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1795123843338158500 Content-Type: text/plain; charset="utf-8" unmap_domain_page() keeps a recently unmapped slot in the vCPU's maphash (MAPHASH_ENTRIES, 8 per vCPU) for a quick remap of the same MFN, and leaves its PTE in place until the entry is displaced by a later unmap or reclaimed by a full cache. Until then the page stays mapped, although nothing refers to the mapping and the mapcache holds no reference to the page. Where the mapcache clears a slot's PTE, it defers the flush: the slot's translation can stay in the TLB of the CPU which used it until that CPU next flushes. While the full directmap is loaded, that reaches nothing it does not map anyway. On the sparse view of the directmap, Xen reaches domain-heap memory only through mappings made for the purpose. A mapping or a translation left behind keeps the page reachable after its last unmap: while Xen serves another of the guest's contexts on the same vCPU, and, once the page is freed and handed to another domain, from a context which has nothing to do with it. So clear a slot's PTE when its last reference goes, maphash slots included: the hash entry keeps the slot, not the mapping, and a later hit maps the page again in the same slot. The slot has mapped nothing else in between, so that needs no flush. Reusing the slot for another page still goes through the garbage map, or through the replacement of a hash entry when the cache is full, both of which flush first. In a per-vCPU mapcache, as vCPU-PT domains have, also mark the local CPU when a slot's PTE is cleared. When the mark is set, reload CR3 as the last step before the next VM entry, and at a lazy switch to the idle vCPU, which keeps the vCPU's page-tables loaded while the CPU idles. A full TLB flush, and a CPU loading other page-tables (switch_cr3_cr4()), drop the slot's translation too, and clear the mark. No other CPU holds the translation, as the vCPU's page-tables are loaded on one CPU at a time. A CR3 reload is enough. The slot has a second address, in the mirror of the per-domain area that every domain but a 64-bit PV one gets for the compat argument translation area. The mapcache's PTEs are not global, and HVM page-tables never run with CR4.PCIDE, so the reload drops every non-global translation of the loaded page-tables, the slot's at both its addresses included. It leaves Xen's global translations alone, and the guest's own, tagged by VPID or ASID. So the guest never runs with a stale slot translation: none outlives the exit in which the slot was cleared, and later exits, whichever of the guest's contexts they serve, reach nothing through one. What remains is the rest of that exit: a page freed and handed out again in that time stays reachable from that CPU until the exit ends. In builds with PV support, the flush the allocator makes before reusing a page freed by its owner reaches every CPU not fully flushed since the free, which covers owned pages. It does not cover a page with no owner, such as an IOMMU page-table not shared with the p2m, nor any page in a build without PV support. A PV domain's domain-wide mapcache has its PTEs cleared but marks no CPU, as CPUs running the domain's other vCPUs may hold the slot's translation too, which a local flush would not reach. For it, as before, builds with PV support flush a page freed by its owner from TLBs before the page is handed out again. The cost is one PTE write per unmap into the maphash and per remap from it; in a per-vCPU mapcache, one CR3 reload per exit that cleared a slot (at its VM entry, or at the lazy switch to idle); and a test of a per-CPU flag on every VM entry, with or without asi=3D. The reload does not move the TLB clock, so filtered flushes still count the CPU as not flushed since its last full flush, which errs on the side of flushing. A second unmap of an entry in the maphash now finds its PTE not present and is caught by the check unmap_domain_page() has for entries the first unmap zapped, rather than by the reference count's ASSERT() in debug builds only. The change does not depend on asi=3D or on a sparse view of the directmap being configured: PV vCPUs use the domain-wide mapcache on the default command line too. For them it adds the PTE writes above, and leaves up to MAPHASH_ENTRIES fewer pages per vCPU mapped in the domain-wide mapcache. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Changes in v7: - Replace the two INVLPGs at a per-vCPU mapcache slot's last unmap with a mark on the CPU, and a CR3 reload when it is set, as the last step before the next VM entry and at a lazy switch to idle; a full flush or a page-table switch clears the mark. This reverses Roger's request for v4, and takes the first of the alternatives he named on v3 15/18: a flush before returning to the guest. On an Intel box the INVLPGs cost a PVH guest's yield ping-pong 1.81 us a round trip, the flush 0.24 us; a port read that QEMU answers, 4,537 ns and 339 ns. - Drop the note for reviewers on dropping the maphash: with the flush deferred, a freed slot must not map another page before a flush, so the garbage map and the wrap-time flush stay. - Rebased over the vCPU state window and the XSAVE area's move, which now come earlier in the series: the comments of unmap_vcpu_state_page() and vcpu_unmap_xsave_area() say what a per-vCPU mapcache now does. Changes in v4: - Merge v3's 15/18 ("x86/mm: flush owned pages before reuse with the sparse directmap view") and 16/18 ("x86/domain_page: keep the maphash's slots, not their mappings"). Rather than flush pages freed by their owner before reuse wherever a sparse view is configured, invalidate a per-vCPU mapcache slot's translations on the local CPU right after its PTE is cleared (Roger). - Message: don't restate when PV vCPUs use the mapcache (Roger). Changes in v3: - New in this version. --- xen/arch/x86/domain.c | 6 +++ xen/arch/x86/domain_page.c | 75 +++++++++++++++++++++++++++---- xen/arch/x86/flushtlb.c | 6 +++ xen/arch/x86/hvm/svm/svm.c | 3 ++ xen/arch/x86/hvm/vmx/vmx.c | 3 ++ xen/arch/x86/include/asm/domain.h | 9 +++- xen/arch/x86/mm.c | 7 +-- xen/arch/x86/xstate.c | 5 ++- 8 files changed, 99 insertions(+), 15 deletions(-) diff --git a/xen/arch/x86/domain.c b/xen/arch/x86/domain.c index 1df6763530..f3e393088f 100644 --- a/xen/arch/x86/domain.c +++ b/xen/arch/x86/domain.c @@ -2341,6 +2341,12 @@ void noreturn context_switch(struct vcpu *prev, stru= ct vcpu *next) if ( (per_cpu(curr_vcpu, cpu) =3D=3D next) || (is_idle_domain(nextd) && cpu_online(cpu)) ) { + /* + * Idling keeps prev's page-tables loaded: drop the translations of + * mapcache slots zapped on them, as a VM entry would. + */ + if ( is_idle_domain(nextd) ) + mapcache_flush_zapped(); local_irq_enable(); } else diff --git a/xen/arch/x86/domain_page.c b/xen/arch/x86/domain_page.c index 86a91c3571..5fcce759dd 100644 --- a/xen/arch/x86/domain_page.c +++ b/xen/arch/x86/domain_page.c @@ -144,6 +144,47 @@ static void mapcache_exit(struct mapcache_domain *dcac= he) #define MAPCACHE_L1ENT(idx) \ __linear_l1_table[l1_linear_offset(MAPCACHE_VIRT_START + pfn_to_paddr(= idx))] =20 +/* + * Set when this CPU zaps a slot of a per-vCPU mapcache, whose translations + * its TLB may then hold; cleared once they are dropped. + */ +DEFINE_PER_CPU(bool, mapcache_flush_pending); + +/* + * Clear a slot's PTE. For a per-vCPU mapcache (HVM domains only), then a= lso + * mark this CPU: its TLB may hold the slot's translations, at its address= and + * at its alias in the per-domain mirror, which every HVM domain has, until + * mapcache_flush_zapped() or a flush drops them. No other CPU can hold t= hem: + * the vCPU's page-tables are loaded on one CPU at a time, and loading oth= er + * page-tables flushes non-global translations. + */ +static void mapcache_zap(const struct mapcache_domain *dcache, + unsigned int idx) +{ + l1e_write(&MAPCACHE_L1ENT(idx), l1e_empty()); + if ( dcache ) + return; + + this_cpu(mapcache_flush_pending) =3D true; +} + +/* + * Drop the translations of the per-vCPU mapcache slots this CPU has zappe= d, + * if any. The mapcache's PTEs are not global, and HVM page-tables run + * without CR4.PCIDE, so a CR3 reload drops them, with every other non-glo= bal + * translation of the loaded page-tables. + */ +void mapcache_flush_zapped(void) +{ + ASSERT(!local_irq_is_enabled()); + + if ( likely(!this_cpu(mapcache_flush_pending)) ) + return; + + write_cr3(read_cr3()); + this_cpu(mapcache_flush_pending) =3D false; +} + void *map_domain_page(mfn_t mfn) { unsigned long flags; @@ -190,6 +231,14 @@ void *map_domain_page(mfn_t mfn) { idx =3D hashent->idx; ASSERT(idx < cache->entries); + /* + * An entry the maphash kept holds on to its slot, not to its mapp= ing + * (see unmap_domain_page()): map the page again. No flush is nee= ded, + * as the slot has mapped nothing else since. + */ + if ( !hashent->refcnt ) + l1e_write(&MAPCACHE_L1ENT(idx), + l1e_from_mfn(mfn, __PAGE_HYPERVISOR_RW)); hashent->refcnt++; ASSERT(hashent->refcnt); ASSERT(mfn_eq(l1e_get_mfn(MAPCACHE_L1ENT(idx)), mfn)); @@ -222,8 +271,8 @@ void *map_domain_page(mfn_t mfn) if ( hashent->idx !=3D MAPHASHENT_NOTINUSE && !hashent->re= fcnt ) { idx =3D hashent->idx; - ASSERT(l1e_get_pfn(MAPCACHE_L1ENT(idx)) =3D=3D hashent= ->mfn); - l1e_write(&MAPCACHE_L1ENT(idx), l1e_empty()); + ASSERT(!(l1e_get_flags(MAPCACHE_L1ENT(idx)) & + _PAGE_PRESENT)); hashent->idx =3D MAPHASHENT_NOTINUSE; hashent->mfn =3D ~0UL; break; @@ -287,32 +336,40 @@ void unmap_domain_page(const void *ptr) =20 local_irq_save(flags); =20 + /* + * The maphash keeps a freed slot, not its mapping: the page may + * change hands meanwhile. A domain-wide mapcache's translation + * left in a TLB is covered by the flush the allocator makes + * before reusing an owned page; a per-vCPU mapcache's translation + * is flushed at the next guest entry. + */ if ( hashent->idx =3D=3D idx ) { ASSERT(hashent->mfn =3D=3D mfn); ASSERT(hashent->refcnt); hashent->refcnt--; + if ( !hashent->refcnt ) + mapcache_zap(dcache, idx); } else if ( !hashent->refcnt ) { + /* The entry replaced has no mapping left: mark its slot as garbag= e. */ if ( hashent->idx !=3D MAPHASHENT_NOTINUSE ) { - /* /First/, zap the PTE. */ - ASSERT(l1e_get_pfn(MAPCACHE_L1ENT(hashent->idx)) =3D=3D - hashent->mfn); - l1e_write(&MAPCACHE_L1ENT(hashent->idx), l1e_empty()); - /* /Second/, mark as garbage. */ + ASSERT(!(l1e_get_flags(MAPCACHE_L1ENT(hashent->idx)) & + _PAGE_PRESENT)); set_bit(hashent->idx, cache->garbage); } =20 - /* Add newly-freed mapping to the maphash. */ + /* Add the newly-freed slot to the maphash, without its mapping. */ + mapcache_zap(dcache, idx); hashent->mfn =3D mfn; hashent->idx =3D idx; } else { /* /First/, zap the PTE. */ - l1e_write(&MAPCACHE_L1ENT(idx), l1e_empty()); + mapcache_zap(dcache, idx); /* /Second/, mark as garbage. */ set_bit(idx, cache->garbage); } diff --git a/xen/arch/x86/flushtlb.c b/xen/arch/x86/flushtlb.c index 5e2ed50ec9..ca7d76749f 100644 --- a/xen/arch/x86/flushtlb.c +++ b/xen/arch/x86/flushtlb.c @@ -98,6 +98,9 @@ static void do_tlb_flush(void) else write_cr3(read_cr3()); =20 + /* No translation of a zapped mapcache slot is left. */ + this_cpu(mapcache_flush_pending) =3D false; + if ( tlb_clk_enabled ) post_flush(t); =20 @@ -172,6 +175,9 @@ void switch_cr3_cr4(struct vcpu *v, unsigned long cr3, = unsigned long cr4) if ( cr4 & X86_CR4_PCIDE ) invpcid_flush_all_nonglobals(); =20 + /* No translation of a zapped mapcache slot is left. */ + this_cpu(mapcache_flush_pending) =3D false; + if ( tlb_clk_enabled ) post_flush(t); =20 diff --git a/xen/arch/x86/hvm/svm/svm.c b/xen/arch/x86/hvm/svm/svm.c index c4d91947ba..09500d1510 100644 --- a/xen/arch/x86/hvm/svm/svm.c +++ b/xen/arch/x86/hvm/svm/svm.c @@ -1050,6 +1050,9 @@ void asmlinkage svm_vmenter_helper(void) vmcb->rip =3D regs->rip; vmcb->rsp =3D regs->rsp; vmcb->rflags =3D regs->rflags | X86_EFLAGS_MBS; + + /* Last: drop zapped mapcache slots' translations before the guest run= s. */ + mapcache_flush_zapped(); } =20 static void svm_guest_osvw_init(struct domain *d) diff --git a/xen/arch/x86/hvm/vmx/vmx.c b/xen/arch/x86/hvm/vmx/vmx.c index bdbcd0ad4d..ba58fd3b45 100644 --- a/xen/arch/x86/hvm/vmx/vmx.c +++ b/xen/arch/x86/hvm/vmx/vmx.c @@ -5029,6 +5029,9 @@ bool asmlinkage vmx_vmenter_helper(const struct cpu_u= ser_regs *regs) __vmwrite(GUEST_RSP, regs->rsp); __vmwrite(GUEST_RFLAGS, regs->rflags | X86_EFLAGS_MBS); =20 + /* Last: drop zapped mapcache slots' translations before the guest run= s. */ + mapcache_flush_zapped(); + return true; } =20 diff --git a/xen/arch/x86/include/asm/domain.h b/xen/arch/x86/include/asm/d= omain.h index c4b06785aa..68764f8d39 100644 --- a/xen/arch/x86/include/asm/domain.h +++ b/xen/arch/x86/include/asm/domain.h @@ -71,7 +71,10 @@ struct mapcache_vcpu { /* Shadow of mapcache_domain.epoch. */ unsigned int shadow_epoch; =20 - /* Lock-free per-VCPU hash of recently-used mappings. */ + /* + * Lock-free per-VCPU hash of recently-used mappings. An entry without + * references keeps its slot but not its mapping (see unmap_domain_pag= e()). + */ struct vcpu_maphash_entry { unsigned long mfn; uint32_t idx; @@ -100,6 +103,10 @@ struct mapcache_domain { void mapcache_domain_init(struct domain *d); int mapcache_vcpu_init(struct vcpu *v); =20 +/* Whether this CPU's TLB may hold translations of zapped mapcache slots. = */ +DECLARE_PER_CPU(bool, mapcache_flush_pending); +void mapcache_flush_zapped(void); + /* x86/64: toggle guest between kernel and user modes. */ void toggle_guest_mode(struct vcpu *v); /* x86/64: toggle guest page tables between kernel and user modes. */ diff --git a/xen/arch/x86/mm.c b/xen/arch/x86/mm.c index 9ac7ce656b..fd6ff4d3b6 100644 --- a/xen/arch/x86/mm.c +++ b/xen/arch/x86/mm.c @@ -6665,9 +6665,10 @@ void unmap_vcpu_state(const struct vcpu *v, unsigned= int idx, unsigned int nr) * CPU's translations of it: at its address, and at its alias in the * per-domain mirror, which every domain but a 64-bit PV one has (flushing * that address there is harmless). Neither kind of mapcache drops them w= ith - * the slot's last mapping: each defers the flush, and CPUs running other - * vCPUs of the domain may have cached a domain-wide one's slot too. A - * directmap address, where the caller has no mapcache, needs nothing. + * the slot's last mapping: a per-vCPU one flushes them before the CPU next + * enters the guest, a domain-wide one defers the flush, and CPUs running + * other vCPUs of its domain may have cached the slot too. A directmap + * address, where the caller has no mapcache, needs nothing. */ void unmap_vcpu_state_page(const void *va) { diff --git a/xen/arch/x86/xstate.c b/xen/arch/x86/xstate.c index c7b750db0a..7543edc160 100644 --- a/xen/arch/x86/xstate.c +++ b/xen/arch/x86/xstate.c @@ -702,8 +702,9 @@ void vcpu_unmap_xsave_area(const struct vcpu *v, const = void *area) return; =20 /* - * A single page comes from the single-page mapcache, which defers its - * flushes; unmap_domain_pages() flushes a multipage mapping itself. + * A single page comes from the single-page mapcache, whose + * domain-wide form defers its flushes; unmap_domain_pages() flushes a + * multipage mapping itself. */ if ( v->arch.xsave_pgcnt =3D=3D 1 ) unmap_vcpu_state_page(area); --=20 2.56.0 From nobody Mon Nov 23 22:32:25 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=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1795123876; cv=none; d=zohomail.com; s=zohoarc; b=TKH0OvKJx6mmSuV0smO8bQbQsqp0TrgpABNypovtspEcfP9e/cGFJ3tXqFcYWZ56OS66PqhZiESAyfFEcCzLXsRsE+BOYXPVYDNZ95+Jo5jRALtALF3SvfGTLiV09c0j0C72xb12tifJx+/jEn/S1HA/W2ERamcxiOui6PXTVG4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795123876; h=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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=iwjbmzKUTv1SHSGr4groI16aIVreIIVrYYnkt+83RW8=; b=ZW2JfCAbRhgm2i0gzAYDBrVxDsmWuyZrjkiPimyUEZ8Svj5JuqNOAEJJMM2QanRb91xqewC5+EgtqpE8KodLIHz7uFNOtDGfkCcjFgHU27geUuerz5qkiy59Ybn4XkwLCy47+48OUiCP4wcgLUPySQXUsnl5QM9ZjESfsuKVlDo= 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=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1795123876413612.8818260607758; Thu, 19 Nov 2026 13:31:16 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1497129.1695533 (Exim 4.92) (envelope-from ) id 1xU9iL-0005TH-34; Thu, 19 Nov 2026 21:30:41 +0000 Received: by outflank-mailman (output) from mailman id 1497129.1695533; Thu, 19 Nov 2026 21:30:40 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9iJ-0005NR-Au; Thu, 19 Nov 2026 21:30:39 +0000 Received: by outflank-mailman (input) for mailman id 1497129; Thu, 19 Nov 2026 21:30:19 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9hz-0001DK-I5 for xen-devel@lists.xenproject.org; Thu, 19 Nov 2026 21:30:19 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xU9hz-00000006tIV-0Ze4; Thu, 19 Nov 2026 21:30:19 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xU9Mm-0000000CVBH-1viV; Thu, 19 Nov 2026 21:08:24 +0000 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" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=iwjbmzKUTv1SHSGr4groI16aIVreIIVrYYnkt+83RW8=; b=meMKMY3BKl0yaQgDy2a7Fg9VvN oK5FPHVcdIp0LDn8dX8Bf41kY0Nskyh97ju6bXJHLTWBgJXpdwX7RtkTeAmkAkfj/O6KT9DorIYaY drodttXQnPW1jwXaKO7ZxteIXsnP+DRSEqZSGPmEMm8ln+9243R18FBVtFkDJi8CvXbY=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie Subject: [PATCH v7 36/50] x86/mm: run guest contexts on the sparse directmap view Date: Thu, 19 Nov 2026 21:07:27 +0000 Message-ID: <20261119-asi-hvm-v7-36-6551b6d5c879@xenproject.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> References: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1795123877730158500 Content-Type: text/plain; charset="utf-8" In _init_xen_l4_slots(), if d->arch.asi.sparse_dmap is set, point the directmap slots at the sparse view, copying from idle_pg_table only the Xen slots below them. A root page-table not shared with a PV guest gets no extended directmap, which the sparse view does not cover. d->arch.asi.sparse_dmap is decided at domain creation: PV domains all have a mapcache, HVM domains need per-vCPU page-tables for one. No option asks for the sparse view yet. The sparse view is built before any domain is created, so the root page-tables of every domain using it carry it from their creation, dom0's included. Dom0's builders reach dom0's memory only through guest virtual addresses and map_domain_page(). In PV shim mode, replace_va_mapping() reads the guest's L4 through the directmap; map it with map_domain_page() instead, as the lower levels already are. Shadow L4s, dom0=3Dshadow's included, get the sparse view from init_xen_l4_slots_pv(). The two directmaps differ where the sparse view leaves memory out, so no translation of the full directmap may remain usable on page-tables carrying the sparse view. Directmap mappings stay global nevertheless: every CR3 write which can move a CPU onto the sparse view goes through switch_cr3_cr4(), which flushes the old page-tables' translations, global entries included, before it returns. The CR3 writes which keep entries -- a PV vCPU's switch between its kernel and user page-tables, XPTI's entry and exit -- stay within one vCPU's page-tables, which carry the same directmap, or go to XPTI's per-CPU root, which maps only Xen's own per-CPU pages there. Removals from the sparse view are flushed on every CPU, global entries included. PV guests that run with global pages, among them a PV dom0 not using the sparse view, therefore keep their directmap TLB entries across switches between guest kernel and user mode. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Changes in v6: - Port onto new dual-init_xen_l4_slots system. Changes in v4: - d->arch.vcpu_pt and d->arch.sparse_dmap are now d->arch.asi.vcpu_pt and d->arch.asi.sparse_dmap. - Drop sparse_dmap_active and sparse_dmap_activate(): root page-tables carry the sparse view from their creation, dom0's included (Roger). - The sparse view's slots are now marked NX in "x86/mm: build and maintain a sparse view of the directmap". - PV shim: replace_va_mapping() maps the guest's L4 with map_domain_page(). - Give sparse_dmap_wanted() its hardware-domain case (Roger). - init_xen_l4_slots() takes the directmap slots from the sparse view, rather than copying the full directmap's and overwriting them; sparse_dmap_install() goes (Roger). - Message: drop the sentence on NX (Roger). Changes in v3: - New in this version. --- xen/arch/x86/include/asm/sparse-directmap.h | 9 +++++++ xen/arch/x86/mm.c | 28 +++++++++++++++++++-- xen/arch/x86/pv/shim.c | 4 ++- xen/arch/x86/spec_ctrl.c | 7 ++++++ 4 files changed, 45 insertions(+), 3 deletions(-) diff --git a/xen/arch/x86/include/asm/sparse-directmap.h b/xen/arch/x86/inc= lude/asm/sparse-directmap.h index ea14d44334..2f8b94f4c1 100644 --- a/xen/arch/x86/include/asm/sparse-directmap.h +++ b/xen/arch/x86/include/asm/sparse-directmap.h @@ -31,6 +31,14 @@ static inline bool sparse_dmap_configured(void) return opt_sparse_dmap_pv || opt_sparse_dmap_hvm || opt_sparse_dmap_hw= dom; } =20 +/* Whether a new domain of the given type is to use the sparse view. */ +static inline bool sparse_dmap_wanted(bool pv, bool hwdom) +{ + return sparse_dmap_root && + (hwdom ? opt_sparse_dmap_hwdom + : pv ? opt_sparse_dmap_pv : opt_sparse_dmap_hvm); +} + void sparse_dmap_init(void); int sparse_dmap_mirror_map(unsigned long virt, mfn_t mfn, unsigned long nr, pte_attr_t flags); @@ -42,6 +50,7 @@ int sparse_dmap_drop_pagetable(mfn_t mfn); #else /* !CONFIG_SPARSE_DIRECTMAP */ =20 static inline bool sparse_dmap_configured(void) { return false; } +static inline bool sparse_dmap_wanted(bool pv, bool hwdom) { return false;= } static inline void sparse_dmap_init(void) {} static inline int sparse_dmap_mirror_map(unsigned long virt, mfn_t mfn, unsigned long nr, pte_attr_t flag= s) diff --git a/xen/arch/x86/mm.c b/xen/arch/x86/mm.c index fd6ff4d3b6..d4a1894cd6 100644 --- a/xen/arch/x86/mm.c +++ b/xen/arch/x86/mm.c @@ -1707,7 +1707,8 @@ struct page_info *perdomain_l3(const struct vcpu *v) * l4t, l4mfn, and d are mandatory, but l4mfn doesn't need to be the mfn u= nder * *l4t. All other parameters are optional and will either fill or zero t= he * appropriate slots. Pagetables not shared with guests will gain the - * extended directmap. + * extended directmap, unless the domain runs on the sparse view of the + * directmap, which replaces the directmap slots. */ static void _init_xen_l4_slots(l4_pgentry_t *l4t, mfn_t l4mfn, const struct domain *d, @@ -1716,7 +1717,7 @@ static void _init_xen_l4_slots(l4_pgentry_t *l4t, mfn= _t l4mfn, { /* * PV vcpus need a shortened directmap. HVM and Idle vcpus get the fu= ll - * directmap. + * directmap, unless the domain runs on the sparse view (below). */ bool short_directmap =3D !paging_mode_external(d); =20 @@ -1751,6 +1752,29 @@ static void _init_xen_l4_slots(l4_pgentry_t *l4t, mf= n_t l4mfn, l4t[l4_table_offset(PERDOMAIN_VIRT_START)]; =20 /* Slot 261-: text/data/bss, RW M2P, vmap, frametable, directmap. */ +#ifdef CONFIG_SPARSE_DIRECTMAP + if ( d->arch.asi.sparse_dmap ) + { + unsigned int i =3D l4_table_offset(SPARSE_DMAP_START); + + memcpy(&l4t[l4_table_offset(XEN_VIRT_START)], + &idle_pg_table[l4_table_offset(XEN_VIRT_START)], + (i - l4_table_offset(XEN_VIRT_START)) * sizeof(*l4t)); + + /* + * Every slot the sparse view covers gets its L3, whatever the full + * directmap has there, so that memory added later is covered too.= A + * PV root has guest slots above; any other root loses the rest of= the + * directmap, which the sparse view does not cover. + */ + for ( ; i < l4_table_offset(SPARSE_DMAP_END); i++ ) + l4t[i] =3D sparse_dmap_root[i]; + if ( !short_directmap ) + for ( ; i < l4_table_offset(DIRECTMAP_VIRT_END); i++ ) + l4t[i] =3D l4e_empty(); + } + else +#endif #ifndef NDEBUG if ( short_directmap && unlikely(root_pgt_pv_xen_slots < ROOT_PAGETABLE_PV_XEN_SLOTS) ) diff --git a/xen/arch/x86/pv/shim.c b/xen/arch/x86/pv/shim.c index 8ec03d7295..a1e5661719 100644 --- a/xen/arch/x86/pv/shim.c +++ b/xen/arch/x86/pv/shim.c @@ -148,12 +148,14 @@ const struct platform_bad_page *__init pv_shim_reserv= ed_pages(unsigned int *size static void __init replace_va_mapping(struct domain *d, l4_pgentry_t *l4st= art, unsigned long va, mfn_t mfn) { - l4_pgentry_t l4e =3D l4start[l4_table_offset(va)]; + const l4_pgentry_t *l4t =3D map_domain_page(virt_to_mfn(l4start)); + l4_pgentry_t l4e =3D l4t[l4_table_offset(va)]; l3_pgentry_t l3e =3D l3e_from_l4e(l4e, l3_table_offset(va)); l2_pgentry_t l2e =3D l2e_from_l3e(l3e, l2_table_offset(va)); l1_pgentry_t *pl1e =3D map_l1t_from_l2e(l2e) + l1_table_offset(va); struct page_info *page =3D mfn_to_page(l1e_get_mfn(*pl1e)); =20 + unmap_domain_page(l4t); put_page_and_type(page); =20 *pl1e =3D l1e_from_mfn(mfn, (!is_pv_32bit_domain(d) ? L1_PROT diff --git a/xen/arch/x86/spec_ctrl.c b/xen/arch/x86/spec_ctrl.c index d97a80695b..8b31e8413d 100644 --- a/xen/arch/x86/spec_ctrl.c +++ b/xen/arch/x86/spec_ctrl.c @@ -1985,6 +1985,13 @@ void spec_ctrl_init_domain(struct domain *d) =20 d->arch.asi.vcpu_pt =3D !pv && (is_hardware_domain(d) ? opt_vcpu_pt_hw= dom : opt_vcpu_pt_hvm); + + /* + * The sparse directmap view needs a mapcache: PV domains all have one, + * HVM domains through per-vCPU page-tables. + */ + d->arch.asi.sparse_dmap =3D (pv || d->arch.asi.vcpu_pt) && + sparse_dmap_wanted(pv, is_hardware_domain(d)= ); } =20 void __init init_speculation_mitigations(void) --=20 2.56.0 From nobody Mon Nov 23 22:32:25 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=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1795123850; cv=none; d=zohomail.com; s=zohoarc; b=HGBYegYw5VXnJdjye/G4Dkl7wV3uBhOe82foEQfnfiMptIBxPTzpcR/ZUX2vtCN+d+3dY0bTxaAZwbhm/JUFJjB9FLogu728HEit/E8xBKRVzzxqATJYch7bEWDRqIgL9+B7CdSLSRo8ytK/Mm+2T64EJU6UGFh3Fto7R5LtCho= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795123850; h=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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=UaTpMaaiNhZLtunGLwpQuzHLfFPW9Iq0cqtwywAoEKg=; b=QdDOVW7LDAQpq2wKMHLYNgaPMjr4i/z8tovshSNjyyMlS5fO/t8mIVOsgliVNP+cFNw+HcmHHZxpuKYosTgG1D/5j+QAGnmZ/UFALRHrqMwYYSmrV3Wl3w7dnHw/CXOpsesdWyZfLS2EwakRFoG2jl93i5oBvTPxdEbA8mT0KJQ= 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=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 179512385025116.211237746008692; Thu, 19 Nov 2026 13:30:50 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1497120.1695464 (Exim 4.92) (envelope-from ) id 1xU9i7-0002vr-Em; Thu, 19 Nov 2026 21:30:27 +0000 Received: by outflank-mailman (output) from mailman id 1497120.1695464; Thu, 19 Nov 2026 21:30:27 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9i6-0002tA-PN; Thu, 19 Nov 2026 21:30:26 +0000 Received: by outflank-mailman (input) for mailman id 1497120; Thu, 19 Nov 2026 21:30:19 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9hz-0001CU-7L for xen-devel@lists.xenproject.org; Thu, 19 Nov 2026 21:30:19 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xU9hy-00000006tGL-2MDi; Thu, 19 Nov 2026 21:30:18 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xU9Mn-0000000CVBH-3Hyl; Thu, 19 Nov 2026 21:08:25 +0000 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" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=UaTpMaaiNhZLtunGLwpQuzHLfFPW9Iq0cqtwywAoEKg=; b=eFcRCySC5XtANy8QGnsu1W+jG0 coNKUUpjsxJYZBWDl4cTo2EUnldBV33LXnCj4U+xe3/UogTxBnSHrDkbi5n7W+DmLWnfYcReLcpMq mBpUMhafMn+WVR7+lGhcC/OaGNFd5XmmEqe9/8cjYv3LDUPkzcbNRPuoruhVaccLGc5I=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Andrew Cooper , Anthony PERARD , Michal Orzel , Jan Beulich , Julien Grall , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Stefano Stabellini , Teddy Astie Subject: [PATCH v7 37/50] x86/spec-ctrl: wire the sparse directmap view into the asi= option Date: Thu, 19 Nov 2026 21:07:28 +0000 Message-ID: <20261119-asi-hvm-v7-37-6551b6d5c879@xenproject.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> References: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1795123851418158500 Content-Type: text/plain; charset="utf-8" Let asi=3D choose which domains run on the sparse view of the directmap, when the hypervisor is built with CONFIG_SPARSE_DIRECTMAP: * `asi` and `asi=3D` enable or disable the sparse view, along with vCPU-PT, for every domain including the hardware domain, PV or PVH. HVM domains, a PVH hardware domain included, use the sparse view together with vCPU-PT, which gives them the mapcache it needs; PV domains all have one. A PV domain on the sparse view keeps its domain-wide mapcache and per-domain mappings: the sparse view separates it from other domains, not its vCPUs from each other. * `asi=3Dhvm=3D`, `asi=3Dpv=3D` and `asi=3Ddom0=3D` do t= he same for HVM or PV domains other than the hardware domain, or for the hardware domain, whatever its type: `asi=3D1,dom0=3D0` leaves the hardware domain out of ASI altogether. * A new `asi=3Ddirectmap=3D` selects the sparse view for all domains, as `asi=3Dvcpu-pt=3D` selects vCPU-PT; `vcpu-pt=3D` alone leaves t= he sparse view off. On a hypervisor built without CONFIG_SPARSE_DIRECTMAP the forms enabling all features leave the sparse view off, and `directmap=3D` is rejected as invalid, as compiled-out `spec-ctrl=3D` sub-options are. A domain type the build lacks is ignored, so that the sparse view is not built for nothing. * For now, a PV domain uses one of XPTI and the sparse view, not both. Enabled explicitly, for dom0, domUs or both, XPTI takes precedence: the PV domains it covers keep the full directmap, and the boot log says so. Left to its default, XPTI yields: it is disabled for the PV domains using the sparse view, with a boot-log warning where it would otherwise be enabled, as the sparse view does not replace it yet (it still maps the xenheap and Xen's stacks). Each domain's case is settled at its creation, and again if it becomes 32-bit, which XPTI does not cover: switch_compat() then puts a domain that XPTI kept off the sparse view on it, with the domain-wide mapcache, which release builds may not have set up for it. The boot log gains a line with the directmap mode, "sparse" once a sparse view is configured and "full" otherwise, and the "ASI features for ..." lines, one for PV domUs now among them, say which domains use the sparse view. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Changes in v5: - Follow "x86/spec-ctrl: introduce Address Space Isolation command line option": opt_vcpu_pt_hwdom's name, the development warning in parse_asi(), IS_ENABLED() in the boot log; pv=3D now replaces the line saying it is ignored. Changes in v4: - Docs, comments and message: "domains" throughout, not "guests" (Roger). - Follow the rename of opt_vcpu_pt_hwdom to opt_pvh_vcpu_pt_hwdom and the move of the per-domain settings into d->arch.asi. - pv=3D and dom0=3D move to the patch introducing asi=3D, beside hvm=3D; opt_sparse_dmap_hwdom and sparse_dmap_wanted()'s hardware-domain case to the patches that add the other knobs and sparse_dmap_wanted() (Roger). - directmap=3D on a build without CONFIG_SPARSE_DIRECTMAP is rejected as invalid, as compiled-out spec-ctrl=3D sub-options are, rather than ignored with a warning (Roger). - A PV domain that XPTI kept off the sparse view goes on it when it becomes 32-bit, which ends XPTI; it used to end up with neither. Changes in v3: - New in this version. --- docs/misc/xen-command-line.pandoc | 30 +++++- xen/arch/x86/include/asm/sparse-directmap.h | 7 ++ xen/arch/x86/pv/domain.c | 16 +++ xen/arch/x86/spec_ctrl.c | 102 +++++++++++++++++--- 4 files changed, 138 insertions(+), 17 deletions(-) diff --git a/docs/misc/xen-command-line.pandoc b/docs/misc/xen-command-line= .pandoc index 839d06da26..092c858f34 100644 --- a/docs/misc/xen-command-line.pandoc +++ b/docs/misc/xen-command-line.pandoc @@ -203,7 +203,7 @@ to appropriate auditing by Xen. Argo is disabled by de= fault. buggy or malicious other domain spamming the ring. =20 ### asi (x86) -> `=3D List of [ , {pv,hvm,dom0}=3D, +> `=3D List of [ , {pv,hvm,dom0}=3D, directmap=3D, > vcpu-pt=3D | vcpu-pt=3Dhvm=3D ]` =20 > Default: `false` @@ -215,14 +215,16 @@ mitigations for certain speculative related attacks, = at the cost of mapping sensitive information on-demand. It might also offer some protection agai= nst unmitigated speculation-related attacks. =20 -The mechanisms currently implemented apply to HVM domains, including a PVH -hardware domain. PV domains, including a PV hardware domain, are unaffect= ed. +The mechanisms currently implemented are `vcpu-pt`, for HVM domains includ= ing +a PVH hardware domain, and `directmap`, for all domains including the hard= ware +domain, PV or PVH. The plain boolean enables or disables both, wherever t= hey +apply. =20 * `pv=3D`, `hvm=3D` and `dom0=3D` enable the features for PV domains or HV= M domains other than the hardware domain, or for the hardware domain, PV or PVH. = The plain boolean does all three: `asi=3D1,dom0=3D0` leaves the hardware dom= ain - out. The forms naming no domain type (an un-suffixed `vcpu-pt=3D`) - cover the hardware domain too. + out. The forms naming no domain type (an un-suffixed `vcpu-pt=3D`= , and + `directmap=3D`) cover the hardware domain too. =20 **WARNING: manual de-selection of enabled options will invalidate any protection offered by the feature. The fine grained options provided below @@ -231,6 +233,18 @@ are meant to be used for debugging purposes only.** * `vcpu-pt` gives each vCPU its own per-domain area: the per-domain slot of each vCPU's monitor table maps a region private to that vCPU. =20 +* `directmap` (only available when Xen is built with + `CONFIG_SPARSE_DIRECTMAP`) runs guest contexts on a sparse view of the + directmap, which maps the memory Xen needs to reach at all times (the Xen + heap, Xen's own image, firmware tables), while guest memory is mapped + transiently. This selection is system-wide; `pv=3D`, `hvm=3D` and `dom0= =3D` + make it for one domain type, along with the other features. HVM domains= use + the sparse view only together with `vcpu-pt`, and PV domains only without + XPTI, which yields to the sparse view unless enabled explicitly (see + `xpti`). The whole-feature forms (`asi`, `asi=3D`, and `pv=3D`, `= hvm=3D` + and `dom0=3D`) set this too, on builds that support it; on other builds = they + leave it off. + ### asid (x86) > `=3D ` =20 @@ -3132,6 +3146,12 @@ Meltdown for all domains. With `dom0` and `domu` it is possible to control page table isolation for dom0 or guest domains only. =20 +With `asi`, XPTI and the sparse directmap view (`asi=3Ddirectmap`) exclude= each +other for a PV domain. Enabled explicitly, as a whole or with `dom0` or +`domu`, XPTI keeps the PV domains it covers off the sparse view, and the b= oot +log says so. Left to its default, XPTI is disabled for the PV domains usi= ng +the sparse view, with a boot-log warning where it would otherwise be enabl= ed. + ### xsave (x86) > `=3D ` =20 diff --git a/xen/arch/x86/include/asm/sparse-directmap.h b/xen/arch/x86/inc= lude/asm/sparse-directmap.h index 2f8b94f4c1..db4df82215 100644 --- a/xen/arch/x86/include/asm/sparse-directmap.h +++ b/xen/arch/x86/include/asm/sparse-directmap.h @@ -31,6 +31,10 @@ static inline bool sparse_dmap_configured(void) return opt_sparse_dmap_pv || opt_sparse_dmap_hvm || opt_sparse_dmap_hw= dom; } =20 +static inline bool sparse_dmap_pv(void) { return opt_sparse_dmap_pv; } +static inline bool sparse_dmap_hvm(void) { return opt_sparse_dmap_hvm; } +static inline bool sparse_dmap_hwdom(void) { return opt_sparse_dmap_hwdom;= } + /* Whether a new domain of the given type is to use the sparse view. */ static inline bool sparse_dmap_wanted(bool pv, bool hwdom) { @@ -51,6 +55,9 @@ int sparse_dmap_drop_pagetable(mfn_t mfn); =20 static inline bool sparse_dmap_configured(void) { return false; } static inline bool sparse_dmap_wanted(bool pv, bool hwdom) { return false;= } +static inline bool sparse_dmap_pv(void) { return false; } +static inline bool sparse_dmap_hvm(void) { return false; } +static inline bool sparse_dmap_hwdom(void) { return false; } static inline void sparse_dmap_init(void) {} static inline int sparse_dmap_mirror_map(unsigned long virt, mfn_t mfn, unsigned long nr, pte_attr_t flag= s) diff --git a/xen/arch/x86/pv/domain.c b/xen/arch/x86/pv/domain.c index f139d8f83c..579d6133f5 100644 --- a/xen/arch/x86/pv/domain.c +++ b/xen/arch/x86/pv/domain.c @@ -248,6 +248,20 @@ int switch_compat(struct domain *d) d->arch.has_32bit_shinfo =3D 1; d->arch.pv.is_32bit =3D true; =20 + /* + * XPTI, cleared below, is for 64-bit domains. A domain it kept off t= he + * sparse directmap view goes on it now if wanted, and the domain-wide + * mapcache the view needs is set up for the domain and its vCPUs. + */ + if ( d->arch.pv.xpti && sparse_dmap_wanted(true, is_hardware_domain(d)= ) ) + { + d->arch.asi.sparse_dmap =3D true; + mapcache_domain_init(d); + for_each_vcpu ( d, v ) + if ( (rc =3D mapcache_vcpu_init(v)) ) + goto undo_and_fail; + } + /* * For 32bit PV guests the shared_info machine address must fit in a 3= 2-bit * field within the guest's start_info structure. We might need to fr= ee @@ -301,6 +315,8 @@ int switch_compat(struct domain *d) =20 undo_and_fail: d->arch.pv.is_32bit =3D d->arch.has_32bit_shinfo =3D false; + if ( d->arch.pv.xpti ) + d->arch.asi.sparse_dmap =3D false; for_each_vcpu( d, v ) { free_compat_arg_xlat(v); diff --git a/xen/arch/x86/spec_ctrl.c b/xen/arch/x86/spec_ctrl.c index 8b31e8413d..5ed81b950c 100644 --- a/xen/arch/x86/spec_ctrl.c +++ b/xen/arch/x86/spec_ctrl.c @@ -387,6 +387,19 @@ int8_t __ro_after_init opt_xpti_domu =3D -1; =20 static __init void xpti_init_default(void) { + /* + * XPTI and the sparse directmap view exclude each other for a PV doma= in + * (see spec_ctrl_init_domain()). Enabled explicitly, XPTI takes + * precedence: the PV domains it covers stay off the sparse view. Lef= t to + * its default, it yields: it is off where the sparse view is asked fo= r. + */ + if ( !opt_dom0_pvh && opt_xpti_hwdom =3D=3D 1 && sparse_dmap_hwdom() ) + printk(XENLOG_ERR + "XPTI enabled, disabling Dom0 sparse-directmap\n"); + if ( opt_xpti_domu =3D=3D 1 && sparse_dmap_pv() ) + printk(XENLOG_ERR + "XPTI enabled, disabling PV DomU sparse-directmap\n"); + if ( (boot_cpu_data.vendor & (X86_VENDOR_AMD | X86_VENDOR_HYGON)) || cpu_has_rdcl_no ) { @@ -397,10 +410,21 @@ static __init void xpti_init_default(void) } else { + bool yield_hwdom =3D opt_xpti_hwdom < 0 && !opt_dom0_pvh && + sparse_dmap_hwdom(); + bool yield_domu =3D opt_xpti_domu < 0 && sparse_dmap_pv(); + + /* The default would be on here: say where it yields. */ + if ( yield_hwdom || yield_domu ) + printk(XENLOG_WARNING + "XPTI defaults off for PV %s on the sparse directmap\n", + yield_hwdom ? (yield_domu ? "Dom0 and DomU" : "Dom0") + : "DomU"); + if ( opt_xpti_hwdom < 0 ) - opt_xpti_hwdom =3D 1; + opt_xpti_hwdom =3D !sparse_dmap_hwdom(); if ( opt_xpti_domu < 0 ) - opt_xpti_domu =3D 1; + opt_xpti_domu =3D !sparse_dmap_pv(); } } =20 @@ -491,6 +515,23 @@ static int __init cf_check parse_pv_l1tf(const char *s) } custom_param("pv-l1tf", parse_pv_l1tf); =20 +/* + * Set the sparse directmap view's knob for each domain type given. A type + * the build lacks stays off, so that no view is built for it, and without + * CONFIG_SPARSE_DIRECTMAP all stay off. + */ +static void __init set_sparse_dmap(bool pv, bool hvm, bool hwdom, bool val) +{ +#ifdef CONFIG_SPARSE_DIRECTMAP + if ( pv && IS_ENABLED(CONFIG_PV) ) + opt_sparse_dmap_pv =3D val; + if ( hvm && IS_ENABLED(CONFIG_HVM) ) + opt_sparse_dmap_hvm =3D val; + if ( hwdom ) + opt_sparse_dmap_hwdom =3D val; +#endif +} + static int __init cf_check parse_asi(const char *s) { static bool __initdata warned; @@ -507,7 +548,10 @@ static int __init cf_check parse_asi(const char *s) =20 /* Interpret 'asi' alone in its positive boolean form. */ if ( *s =3D=3D '\0' ) + { opt_vcpu_pt_hwdom =3D opt_vcpu_pt_hvm =3D true; + set_sparse_dmap(true, true, true, true); + } =20 do { ss =3D strchr(s, ','); @@ -520,17 +564,33 @@ static int __init cf_check parse_asi(const char *s) case 0: case 1: opt_vcpu_pt_hwdom =3D opt_vcpu_pt_hvm =3D val; + set_sparse_dmap(true, true, true, val); break; =20 default: if ( (val =3D parse_boolean("pv", s, ss)) >=3D 0 ) - printk(XENLOG_WARNING - "No ASI feature applies to PV domains - ignoring 'a= si=3D%.*s' setting\n", - (int)(ss - s), s); + set_sparse_dmap(true, false, false, val); else if ( (val =3D parse_boolean("hvm", s, ss)) >=3D 0 ) + { opt_vcpu_pt_hvm =3D val; + set_sparse_dmap(false, true, false, val); + } else if ( (val =3D parse_boolean("dom0", s, ss)) >=3D 0 ) + { opt_vcpu_pt_hwdom =3D val; + set_sparse_dmap(false, false, true, val); + } + else if ( (val =3D parse_boolean("directmap", s, ss)) >=3D 0 ) + { + /* System-wide: every domain type, the hardware domain too= . */ + if ( IS_ENABLED(CONFIG_SPARSE_DIRECTMAP) ) + set_sparse_dmap(true, true, true, val); + else + { + no_config_param("SPARSE_DIRECTMAP", "asi", s, ss); + rc =3D -EINVAL; + } + } else if ( (val =3D parse_boolean("vcpu-pt", s, ss)) !=3D -1 ) { switch ( val ) @@ -760,11 +820,28 @@ static void __init print_details(enum ind_thunk thunk) opt_pv_l1tf_domu ? "enabled" : "disabled"); #endif =20 - printk(" ASI features for Dom0:%s\n", - opt_dom0_pvh && opt_vcpu_pt_hwdom ? " vCPU-PT" : " None"); - if ( IS_ENABLED(CONFIG_HVM) ) - printk(" ASI features for HVM VMs:%s\n", - opt_vcpu_pt_hvm ? " vCPU-PT" : " None"); + { + bool hwdom_pt =3D opt_dom0_pvh && opt_vcpu_pt_hwdom; + bool hwdom_sd =3D sparse_dmap_hwdom() && + (opt_dom0_pvh ? hwdom_pt : !opt_xpti_hwdom); + + printk(" ASI directmap mode: %s\n", + sparse_dmap_configured() ? "sparse" : "full"); + printk(" ASI features for Dom0:%s%s%s\n", + hwdom_pt || hwdom_sd ? "" : " None", + hwdom_pt ? " vCPU-PT" : "", + hwdom_sd ? " sparse-directmap" : ""); + if ( IS_ENABLED(CONFIG_HVM) ) + printk(" ASI features for HVM VMs:%s%s%s\n", + opt_vcpu_pt_hvm ? "" : " None", + opt_vcpu_pt_hvm ? " vCPU-PT" : "", + opt_vcpu_pt_hvm && sparse_dmap_hvm() + ? " sparse-directmap" : ""); + if ( IS_ENABLED(CONFIG_PV) ) + printk(" ASI features for PV VMs:%s\n", + sparse_dmap_pv() && !opt_xpti_domu + ? " sparse-directmap" : " None"); + } } =20 static bool __init check_smt_enabled(void) @@ -1988,9 +2065,10 @@ void spec_ctrl_init_domain(struct domain *d) =20 /* * The sparse directmap view needs a mapcache: PV domains all have one, - * HVM domains through per-vCPU page-tables. + * HVM domains through per-vCPU page-tables. A PV domain using XPTI + * stays on the full directmap. */ - d->arch.asi.sparse_dmap =3D (pv || d->arch.asi.vcpu_pt) && + d->arch.asi.sparse_dmap =3D (pv ? !d->arch.pv.xpti : d->arch.asi.vcpu_= pt) && sparse_dmap_wanted(pv, is_hardware_domain(d)= ); } =20 --=20 2.56.0 From nobody Mon Nov 23 22:32:25 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=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1795123882; cv=none; d=zohomail.com; s=zohoarc; b=DICKLtKSFz4V593taCAWnb/6qVZycbM1HCL6IiAGGUhEyJLXYYkwrBDI1396y5DH/QDU2RRyWm0LHUKUvOyrALISLeOVPj5A9sK6aysrqLHusKlJfAwl3QrsrFK9xWAia3EEUmaqpWIlZwZY4OfiRAOpezDLdZjf8H9q0PAPBVM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795123882; h=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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=bmEQ9cEAXosZ62ttzIN9wMbJOrfPVactWu4o1BwmH3g=; b=gTqtBV+OzDXonvfrzqx8/OFLEENh4Bo7VWl3UXjwzj0zIqpeDmCyZPnZ9eXutBCeHwjRdjRZge53PlFmCux9IAknGqgbQupc4msQTXDBY6YBwk9kjDcWpvoy7/KlcXgXr9pftJ8LfNAZrWt5vNMbz7pTNUC+p+UaUhI9R839rnM= 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=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1795123882953691.3945924461766; Thu, 19 Nov 2026 13:31:22 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1497139.1695630 (Exim 4.92) (envelope-from ) id 1xU9ij-000245-4G; Thu, 19 Nov 2026 21:31:05 +0000 Received: by outflank-mailman (output) from mailman id 1497139.1695630; Thu, 19 Nov 2026 21:31:04 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9ih-0001wJ-3Z; Thu, 19 Nov 2026 21:31:03 +0000 Received: by outflank-mailman (input) for mailman id 1497139; Thu, 19 Nov 2026 21:30:20 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9i0-0001EP-0K for xen-devel@lists.xenproject.org; Thu, 19 Nov 2026 21:30:20 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xU9hz-00000006tJK-1fL2; Thu, 19 Nov 2026 21:30:19 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xU9Mp-0000000CVBH-0WZy; Thu, 19 Nov 2026 21:08:27 +0000 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" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=bmEQ9cEAXosZ62ttzIN9wMbJOrfPVactWu4o1BwmH3g=; b=dP2NRuiYSS+vRr5zADSv3L9jLy l1QGc0Iim+76FmMRJSyOue52/xk/HqkuON+uyJlUi/3xhxmMvUdVG5ObkqO3RkCzopFTluYIE+qp1 6wGFBVfoNANYq1O5Z/ec8Nd1SdygrBb9c89mD/LI4mX6VUzrlbxozJTTFehuRoBZ+YIM=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Andrew Cooper , Anthony PERARD , Michal Orzel , Jan Beulich , Julien Grall , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Stefano Stabellini , Teddy Astie Subject: [PATCH v7 38/50] x86/spec-ctrl: add the vmap-strict sub-option of asi= Date: Thu, 19 Nov 2026 21:07:29 +0000 Message-ID: <20261119-asi-hvm-v7-38-6551b6d5c879@xenproject.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> References: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1795123883704158500 Content-Type: text/plain; charset="utf-8" map_domain_pages() falls back to vmap() where the loaded page tables have no slots for a multipage mapping: before the first page-table switch in boot, or with the idle vCPU's page tables loaded. Currently no calls are made during boot, and the only known way to reach map_domain_pages() with the idle page tables loaded is on an error path that will already ultimately crash the host. The fallback briefly exposes the pages to every context, and requires a system-wide TLB shootdown on unmap, but allows execution to continue. For those who want stricter isolation guarantees, at the risk that more situations might come up in the future, a new sub-option, asi=3Dvmap-strict, off by default, makes the fallback a BUG() instead. The boot log reports its setting. Assisted-by: Claude Code:claude-fable-5, Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Changes in v7: - Split out of "x86/domain_page: introduce map_domain_pages()", which now comes before the asi=3D option. --- docs/misc/xen-command-line.pandoc | 10 +++++++++- xen/arch/x86/domain_page.c | 2 ++ xen/arch/x86/include/asm/spec_ctrl.h | 1 + xen/arch/x86/spec_ctrl.c | 7 +++++++ 4 files changed, 19 insertions(+), 1 deletion(-) diff --git a/docs/misc/xen-command-line.pandoc b/docs/misc/xen-command-line= .pandoc index 092c858f34..52e10eb393 100644 --- a/docs/misc/xen-command-line.pandoc +++ b/docs/misc/xen-command-line.pandoc @@ -203,7 +203,7 @@ to appropriate auditing by Xen. Argo is disabled by de= fault. buggy or malicious other domain spamming the ring. =20 ### asi (x86) -> `=3D List of [ , {pv,hvm,dom0}=3D, directmap=3D, +> `=3D List of [ , {pv,hvm,dom0}=3D, {directmap,vmap-strict}= =3D, > vcpu-pt=3D | vcpu-pt=3Dhvm=3D ]` =20 > Default: `false` @@ -245,6 +245,14 @@ are meant to be used for debugging purposes only.** and `dom0=3D`) set this too, on builds that support it; on other builds = they leave it off. =20 +* `vmap-strict` crashes the host when a multipage transient mapping + (`map_domain_pages()`) is requested where the loaded page-tables + have no slots for one: in early boot, or on the idle vCPU's + page-tables. By default such a mapping falls back to `vmap()`, + which maps the pages in every context. The first such mapping is + logged. The option is for hosts where a crash is preferable to such + a mapping; none is known to be made in normal operation. + ### asid (x86) > `=3D ` =20 diff --git a/xen/arch/x86/domain_page.c b/xen/arch/x86/domain_page.c index 5fcce759dd..df1b78ccc5 100644 --- a/xen/arch/x86/domain_page.c +++ b/xen/arch/x86/domain_page.c @@ -18,6 +18,7 @@ #include #include #include +#include =20 /* Whether the vCPU supports mapcache usage, might not be in use. */ static bool has_mapcache(const struct vcpu *v) @@ -417,6 +418,7 @@ void *map_domain_pages(const mfn_t *mfn, unsigned int n= r) v =3D pgtable_current_vcpu(); if ( !v || is_idle_vcpu(v) ) { + BUG_ON(opt_vmap_strict); printk_once(XENLOG_WARNING "%pS: map_domain_pages() falling back to vmap()\n", __builtin_return_address(0)); diff --git a/xen/arch/x86/include/asm/spec_ctrl.h b/xen/arch/x86/include/as= m/spec_ctrl.h index b62229ab5d..d9ad94ddb0 100644 --- a/xen/arch/x86/include/asm/spec_ctrl.h +++ b/xen/arch/x86/include/asm/spec_ctrl.h @@ -88,6 +88,7 @@ extern uint8_t default_scf; extern int8_t opt_xpti_hwdom, opt_xpti_domu; =20 extern bool opt_vcpu_pt_hwdom, opt_vcpu_pt_hvm; +extern bool opt_vmap_strict; =20 extern bool cpu_has_bug_l1tf; extern int8_t opt_pv_l1tf_hwdom, opt_pv_l1tf_domu; diff --git a/xen/arch/x86/spec_ctrl.c b/xen/arch/x86/spec_ctrl.c index 5ed81b950c..3a1755aba4 100644 --- a/xen/arch/x86/spec_ctrl.c +++ b/xen/arch/x86/spec_ctrl.c @@ -86,6 +86,9 @@ bool __ro_after_init opt_bp_spec_reduce =3D true; =20 static bool __initdata opt_ibpb_alt; =20 +/* BUG() where map_domain_pages() would fall back to vmap() (vmap-strict).= */ +bool __ro_after_init opt_vmap_strict; + /* Give each vCPU of an HVM domain its own per-domain area (vCPU-PT). */ bool __ro_after_init opt_vcpu_pt_hvm; bool __ro_after_init opt_vcpu_pt_hwdom; @@ -591,6 +594,8 @@ static int __init cf_check parse_asi(const char *s) rc =3D -EINVAL; } } + else if ( (val =3D parse_boolean("vmap-strict", s, ss)) >=3D 0= ) + opt_vmap_strict =3D val; else if ( (val =3D parse_boolean("vcpu-pt", s, ss)) !=3D -1 ) { switch ( val ) @@ -841,6 +846,8 @@ static void __init print_details(enum ind_thunk thunk) printk(" ASI features for PV VMs:%s\n", sparse_dmap_pv() && !opt_xpti_domu ? " sparse-directmap" : " None"); + printk(" ASI vmap-strict: %s\n", + opt_vmap_strict ? "enabled" : "disable= d"); } } =20 --=20 2.56.0 From nobody Mon Nov 23 22:32:25 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=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1795123906; cv=none; d=zohomail.com; s=zohoarc; b=KFt3T+nZaqi8espBeT3nRZ05cO6WSb0pTClL/bim+uomawyw/U55Erl4Us13Is9bYGQxK6lZgNgk4pq4llcIQIoBT4N3LGF4CZtwOF0jlGEGix8EHT2e7XUvTaCVNLM14r1DLxU6oSv2kJZ8AmD2x7cO2WxGTiNv/1tGxdp1rRk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795123906; h=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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=B7m2WUghWIyUkzhekUlna4CACam8I4377nJwfBDgTSY=; b=AYTr1bxD5QCyGrCZADzGeHbv7H4t8RUbmjZuQJteWtSF6Sm2ogyvcEF17KKwoxeJT5InDbmGjlV9mAKQ4Xu7QxxIM5sATxasI+f0OS8ex6vuvo9PuEzt0IJAT2+2ThxZIHEBR9yXvmMdTkmbPlQLlUx4E8paCuaw10SdB2a9feI= 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=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1795123906169431.7521637221487; Thu, 19 Nov 2026 13:31:46 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1497141.1695655 (Exim 4.92) (envelope-from ) id 1xU9io-0003Hr-EB; Thu, 19 Nov 2026 21:31:10 +0000 Received: by outflank-mailman (output) from mailman id 1497141.1695655; Thu, 19 Nov 2026 21:31:09 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9in-00039G-10; Thu, 19 Nov 2026 21:31:09 +0000 Received: by outflank-mailman (input) for mailman id 1497141; Thu, 19 Nov 2026 21:30:20 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9i0-0001EX-2d for xen-devel@lists.xenproject.org; Thu, 19 Nov 2026 21:30:20 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xU9hz-00000006tK4-2TeM; Thu, 19 Nov 2026 21:30:19 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xU9Mq-0000000CVBH-1tGR; Thu, 19 Nov 2026 21:08:28 +0000 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" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=B7m2WUghWIyUkzhekUlna4CACam8I4377nJwfBDgTSY=; b=pi9/cotjWlr6Usf3RGqzcNre4u a/ficgwP4hWEZEksa27fAnB4CfBYd85mkBozAdtWf3WbLeZ9XY6fKajiZnCgFAhQkNg4HWAi2grbb O24GrgOhoD8/lVpEJRJO/tdGNI9Q8gd53RHAb7zcu5KwOmJLiCV14t29m7aZxApDXAKQ=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie , Anthony PERARD , Michal Orzel , Julien Grall , Stefano Stabellini Subject: [PATCH v7 39/50] x86/mm: split a sparse directmap superpage with a page being freed Date: Thu, 19 Nov 2026 21:07:30 +0000 Message-ID: <20261119-asi-hvm-v7-39-6551b6d5c879@xenproject.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> References: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1795123908049158500 Content-Type: text/plain; charset="utf-8" A xenheap free takes its pages out of the sparse directmap view. Where a superpage covers them, the superpage has to be split, and modify_mappings_in() allocates the page-table page for that. The sparse view has such superpages wherever a xenheap block of 2MiB or more was mapped whole, and wherever map_pages_in() merged the entries of a 2MiB region into one, as it does in the full directmap, once all 512 pages were mapped. The allocation can fail once the hardware domain exists, and the free then keeps its pages out of the heap for good, with a warning. Use one of the pages being freed instead. The sparse view's free hook makes the first of them what alloc_xen_pagetable() would return, and passes it to modify_mappings_in(), which uses it, should a split need a page, before allocating one. The new L1 maps the rest of the superpage, and the page's own entry is cleared along with the others being freed. The hook tells free_xenheap_pages() whether it kept the page, and free_xenheap_pages() then frees the rest of the block as blocks of orders 0 to order - 1. The page goes back to the heap once the L1 has emptied, as any page-table page of the sparse view does. One page is enough if the sparse view has no 1GiB pages: a block smaller than 2MiB lies within one 2MiB page and needs one L1, and a larger block covers whole 2MiB pages and needs none, where a block within a 1GiB page could need an L2 as well. So map_pages_in() makes 1GiB pages, by mapping or by merging, only in idle_pg_table's hierarchy; the sparse view keeps its 2MiB pages, mapped and merged. A xenheap free then allocates nothing in the sparse view. The costs: a 2MiB region split this way can merge again only once it has emptied, as its L1 is one of its own pages; where the sparse view maps a whole 1GiB region, which takes memory never handed to the heap and xenheap memory filling it, contexts on it use 2MiB pages instead of one 1GiB page; and arch_xenheap_pages_free() gains a parameter. The other removals from the sparse view (memory handed to the heap, idle_pg_table's boot-time page-tables, and the undo of a failed xenheap allocation) still allocate when they split a superpage. The sparse view is correct without this, as a free that cannot split its superpage fails safe. It is kept apart from "x86/mm: build and maintain a sparse view of the directmap" so that it can be weighed on its own. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Changes in v5: - New patch This is a fairly complicated patch to handle a very niche corner case: - All the 4k pages in a superpage happen to have been allocated into the xenheap (and the page table automatically merged into a superpage) - The system is completely out of singleton pages to allocate as pagetables - A free_xenheap_page() is called on a subset of those pages Without this patch, the freed page is leaked; with this patch, one of the freed pages is used as a pagetable page instead. Happy to drop this patch, or move it further to the end of the series, if it helps the main bit of functionality get in. --- xen/arch/x86/include/asm/mm.h | 2 +- xen/arch/x86/mm.c | 34 ++++++++++++++++++++++------ xen/arch/x86/sparse-directmap.c | 40 ++++++++++++++++++++++++++------- xen/common/page_alloc.c | 14 ++++++++---- xen/include/xen/mm.h | 11 ++++++--- 5 files changed, 78 insertions(+), 23 deletions(-) diff --git a/xen/arch/x86/include/asm/mm.h b/xen/arch/x86/include/asm/mm.h index 9374908443..b253f4a807 100644 --- a/xen/arch/x86/include/asm/mm.h +++ b/xen/arch/x86/include/asm/mm.h @@ -603,7 +603,7 @@ void *alloc_mapped_pagetable(mfn_t *pmfn); int map_pages_in(l4_pgentry_t *root, unsigned long virt, mfn_t mfn, unsigned long nr_mfns, pte_attr_t flags); int modify_mappings_in(l4_pgentry_t *root, unsigned long s, unsigned long = e, - pte_attr_t nf); + pte_attr_t nf, mfn_t *spare); #ifdef CONFIG_SPARSE_DIRECTMAP mfn_t xen_mapping_lookup(l4_pgentry_t *root, unsigned long va, pte_attr_t *flags, unsigned long *nr); diff --git a/xen/arch/x86/mm.c b/xen/arch/x86/mm.c index d4a1894cd6..09b641f846 100644 --- a/xen/arch/x86/mm.c +++ b/xen/arch/x86/mm.c @@ -5558,6 +5558,10 @@ static void free_pagetable_in(const l4_pgentry_t *ro= ot, mfn_t mfn) /* * map_pages_to_xen() in the hierarchy rooted at @root: idle_pg_table's, or * another hierarchy sharing its virtual addresses. + * + * Only idle_pg_table's hierarchy gets 1GiB pages: a xenheap free splits a + * superpage of the sparse directmap view with one of the pages it frees, + * which is enough for a 2MiB page only. */ int map_pages_in( l4_pgentry_t *root, @@ -5567,6 +5571,7 @@ int map_pages_in( pte_attr_t flags) { bool locking =3D system_state > SYS_STATE_boot; + bool page1gb =3D cpu_has_page1gb && root =3D=3D idle_pg_table; l3_pgentry_t *pl3e =3D NULL, ol3e; l2_pgentry_t *pl2e =3D NULL, ol2e; l1_pgentry_t *pl1e, ol1e; @@ -5619,7 +5624,7 @@ int map_pages_in( L3T_LOCK(current_l3page); ol3e =3D *pl3e; =20 - if ( cpu_has_page1gb && + if ( page1gb && IS_L3E_ALIGNED(virt, flags & _PAGE_PRESENT ? mfn : _mfn(0)) && nr_mfns >=3D (1UL << (L3_PAGETABLE_SHIFT - PAGE_SHIFT)) && !(flags & (_PAGE_PAT | MAP_SMALL_PAGES)) ) @@ -5905,7 +5910,7 @@ int map_pages_in( } =20 check_l3: - if ( cpu_has_page1gb && + if ( page1gb && (flags =3D=3D PAGE_HYPERVISOR) && ((nr_mfns =3D=3D 0) || IS_L3E_ALIGNED(virt, mfn)) ) { @@ -5984,6 +5989,18 @@ int __init populate_pt_range(unsigned long virt, uns= igned long nr_mfns) return map_pages_to_xen(virt, INVALID_MFN, nr_mfns, MAP_SMALL_PAGES); } =20 +static mfn_t take_or_alloc_pagetable(mfn_t *spare) +{ + mfn_t mfn =3D spare ? *spare : INVALID_MFN; + + if ( mfn_eq(mfn, INVALID_MFN) ) + return alloc_xen_pagetable(); + + *spare =3D INVALID_MFN; + + return mfn; +} + /* * Alter the permissions of a range of Xen virtual address space. * @@ -5997,10 +6014,13 @@ int __init populate_pt_range(unsigned long virt, un= signed long nr_mfns) * It is an error to call with present flags over an unpopulated range. * * modify_mappings_in() does so in the hierarchy rooted at @root, as - * map_pages_in() does. + * map_pages_in() does. @spare, if not NULL, may hold a page the caller + * gives up for a page-table page, which shattering a superpage then uses + * before allocating one. *@spare reads INVALID_MFN once it has been used, + * and the page is then no longer the caller's. */ int modify_mappings_in(l4_pgentry_t *root, unsigned long s, - unsigned long e, pte_attr_t nf) + unsigned long e, pte_attr_t nf, mfn_t *spare) { bool locking =3D system_state > SYS_STATE_boot; l3_pgentry_t *pl3e =3D NULL; @@ -6064,7 +6084,7 @@ int modify_mappings_in(l4_pgentry_t *root, unsigned l= ong s, } =20 /* PAGE1GB: shatter the superpage and fall through. */ - l2mfn =3D alloc_xen_pagetable(); + l2mfn =3D take_or_alloc_pagetable(spare); if ( mfn_eq(l2mfn, INVALID_MFN) ) goto out; =20 @@ -6122,7 +6142,7 @@ int modify_mappings_in(l4_pgentry_t *root, unsigned l= ong s, { l1_pgentry_t *l1t; /* PSE: shatter the superpage and try again. */ - mfn_t l1mfn =3D alloc_xen_pagetable(); + mfn_t l1mfn =3D take_or_alloc_pagetable(spare); =20 if ( mfn_eq(l1mfn, INVALID_MFN) ) goto out; @@ -6280,7 +6300,7 @@ int modify_mappings_in(l4_pgentry_t *root, unsigned l= ong s, =20 int modify_xen_mappings(unsigned long s, unsigned long e, pte_attr_t nf) { - int rc =3D modify_mappings_in(idle_pg_table, s, e, nf); + int rc =3D modify_mappings_in(idle_pg_table, s, e, nf, NULL); =20 if ( !rc ) rc =3D sparse_dmap_mirror_modify(s, e, nf); diff --git a/xen/arch/x86/sparse-directmap.c b/xen/arch/x86/sparse-directma= p.c index d35a8d6979..adec474f72 100644 --- a/xen/arch/x86/sparse-directmap.c +++ b/xen/arch/x86/sparse-directmap.c @@ -121,12 +121,13 @@ static int add_to_sparse_view(l4_pgentry_t *root, uns= igned long mfn, return 0; } =20 -static int remove_from_sparse_view(unsigned long mfn, unsigned long nr) +static int remove_from_sparse_view(unsigned long mfn, unsigned long nr, + mfn_t *spare) { unsigned long va =3D (unsigned long)mfn_to_virt(mfn); =20 return modify_mappings_in(sparse_dmap_root, va, va + (nr << PAGE_SHIFT= ), - _PAGE_NONE); + _PAGE_NONE, spare); } =20 /* Memory is about to be handed to the heap allocator. */ @@ -144,7 +145,7 @@ bool arch_heap_pages_init(mfn_t mfn, unsigned long nr) return true; } =20 - if ( remove_from_sparse_view(s, nr) ) + if ( remove_from_sparse_view(s, nr, NULL) ) { printk(XENLOG_ERR "sparse directmap: cannot unmap MFNs %#lx-%#lx, not freeing= \n", @@ -188,10 +189,18 @@ int arch_xenheap_pages_alloc(mfn_t mfn, unsigned int = order) /* * Xenheap pages are about to be freed. A failure means the pages may sti= ll * be reachable through the sparse view, and must not be reused. + * + * If the caller allows, the first page serves as the page-table page that + * splitting a superpage of the sparse view needs: the new L1 maps the rest + * of the superpage, and the page leaves the sparse view with the others. + * One page is enough: the sparse view has no 1GiB pages, a block below 2M= iB + * lies within one 2MiB page, and a larger one covers whole ones. */ -int arch_xenheap_pages_free(mfn_t mfn, unsigned int order) +int arch_xenheap_pages_free(mfn_t mfn, unsigned int order, bool *first_kep= t) { unsigned long s =3D mfn_x(mfn), nr =3D 1UL << order; + mfn_t spare =3D INVALID_MFN; + int rc; =20 if ( !sparse_dmap_configured() ) return 0; @@ -204,7 +213,22 @@ int arch_xenheap_pages_free(mfn_t mfn, unsigned int or= der) return 0; } =20 - return remove_from_sparse_view(s, nr); + if ( first_kept ) + { + struct page_info *pg =3D mfn_to_page(mfn); + + /* Made what alloc_xen_pagetable() would return, in case it is use= d. */ + pg->count_info &=3D ~PGC_xen_heap; + page_set_owner(pg, NULL); + spare =3D mfn; + } + + rc =3D remove_from_sparse_view(s, nr, &spare); + + if ( first_kept && mfn_eq(spare, INVALID_MFN) ) + *first_kept =3D true; + + return rc; } =20 /* Memory leaves Xen's hands without passing through the heap. */ @@ -233,7 +257,7 @@ int sparse_dmap_drop_pagetable(mfn_t mfn) INVALID_MFN) ) return 0; =20 - rc =3D remove_from_sparse_view(s, 1); + rc =3D remove_from_sparse_view(s, 1, NULL); if ( rc ) printk(XENLOG_WARNING "Leaking page-table page %"PRI_mfn": still in the sparse di= rectmap\n", @@ -267,7 +291,7 @@ int sparse_dmap_mirror_modify(unsigned long s, unsigned= long e, e =3D min(e, SPARSE_DMAP_END); =20 if ( !(nf & _PAGE_PRESENT) ) - return modify_mappings_in(sparse_dmap_root, s, e, nf); + return modify_mappings_in(sparse_dmap_root, s, e, nf, NULL); =20 /* * A permission change applies where the sparse view maps anything: it @@ -283,7 +307,7 @@ int sparse_dmap_mirror_modify(unsigned long s, unsigned= long e, n =3D min(n, (e - va) >> PAGE_SHIFT); if ( !mfn_eq(mfn, INVALID_MFN) && (rc =3D modify_mappings_in(sparse_dmap_root, va, - va + (n << PAGE_SHIFT), nf)) ) + va + (n << PAGE_SHIFT), nf, NULL)) ) return rc; va +=3D n << PAGE_SHIFT; } diff --git a/xen/common/page_alloc.c b/xen/common/page_alloc.c index 2150dcbd5e..349de3ddf3 100644 --- a/xen/common/page_alloc.c +++ b/xen/common/page_alloc.c @@ -2561,7 +2561,7 @@ void *alloc_xenheap_pages(unsigned int order, unsigne= d int memflags) * The hook may have mapped part of the pages before failing: take * them out again as a free would, and leak them if that fails too. */ - if ( arch_xenheap_pages_free(page_to_mfn(pg), order) ) + if ( arch_xenheap_pages_free(page_to_mfn(pg), order, NULL) ) printk(XENLOG_WARNING "Leaking xenheap pages %"PRI_mfn"+%u: still mapped\n", mfn_x(page_to_mfn(pg)), 1U << order); @@ -2580,6 +2580,7 @@ void free_xenheap_pages(void *v, unsigned int order) { struct page_info *pg; unsigned int i; + bool first_kept =3D false; =20 ASSERT_ALLOC_CONTEXT(); =20 @@ -2593,7 +2594,7 @@ void free_xenheap_pages(void *v, unsigned int order) * they may remain reachable through it: leak them rather than hand th= em * out. */ - if ( arch_xenheap_pages_free(page_to_mfn(pg), order) ) + if ( arch_xenheap_pages_free(page_to_mfn(pg), order, &first_kept) ) { printk(XENLOG_WARNING "Leaking xenheap pages %"PRI_mfn"+%u: still mapped\n", @@ -2601,10 +2602,15 @@ void free_xenheap_pages(void *v, unsigned int order) return; } =20 - for ( i =3D 0; i < (1u << order); i++ ) + for ( i =3D first_kept ? 1 : 0; i < (1u << order); i++ ) pg[i].count_info &=3D ~PGC_xen_heap; =20 - free_heap_pages(pg, order, true); + if ( !first_kept ) + free_heap_pages(pg, order, true); + else + /* The rest of the block, as the blocks of orders 0 to order - 1. = */ + for ( i =3D 0; i < order; i++ ) + free_heap_pages(pg + (1u << i), i, true); } =20 #endif /* CONFIG_SEPARATE_XENHEAP */ diff --git a/xen/include/xen/mm.h b/xen/include/xen/mm.h index 719d265130..c441f903c2 100644 --- a/xen/include/xen/mm.h +++ b/xen/include/xen/mm.h @@ -108,8 +108,12 @@ bool arch_xenheap_needs_scrub(void); * pages go through arch_xenheap_pages_free() before being freed again. */ int arch_xenheap_pages_alloc(mfn_t mfn, unsigned int order); -/* Xenheap pages are to be freed. Non-zero: failed, don't reuse them. */ -int arch_xenheap_pages_free(mfn_t mfn, unsigned int order); +/* + * Xenheap pages are to be freed. Non-zero: failed, don't reuse them. Wi= th + * @first_kept not NULL, the arch may keep the first page for itself, and + * then sets *@first_kept to true: that page is not the caller's to free. + */ +int arch_xenheap_pages_free(mfn_t mfn, unsigned int order, bool *first_kep= t); #else static inline bool arch_heap_pages_init(mfn_t mfn, unsigned long nr) { @@ -123,7 +127,8 @@ static inline int arch_xenheap_pages_alloc(mfn_t mfn, u= nsigned int order) { return 0; } -static inline int arch_xenheap_pages_free(mfn_t mfn, unsigned int order) +static inline int arch_xenheap_pages_free(mfn_t mfn, unsigned int order, + bool *first_kept) { return 0; } --=20 2.56.0 From nobody Mon Nov 23 22:32:25 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=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1795123850; cv=none; d=zohomail.com; s=zohoarc; b=GvU8bX15yUou/RLOxUnb2EWQ3x5mneXSwbieVj3hIWxkgnowjgQYZB0+/U/QoolbOaw/A3YCRLckNP0vQM28X7zcivEc4SGGF/0fI10oLvLRZhWFcfWpCuZ6CADCn78wKNtUu0wutwsGe8eCZLoFiPm3o2DDvWhal81Nz7fG4n0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795123850; h=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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=n43747u3O82/EPJ62jqYKY40YYs4o7PKgFzEzkqBCKU=; b=hfiPlMdxidFgZGVZhaaTEbNiYKEh/FqI08TAKKkfm+8s64DD0TSGlMLP9hqJYjZ+jIh2/VPkxqcJWZovNTQrfKb6dkbPQAc1dlZEFKnIuPKQ+ghPdRlCvIZu6//gXMehD5sWsQHdf7uP5z0JAwY9bGLS9qVYODAyzb7PnwPc0gA= 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=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1795123850141707.7417688266481; Thu, 19 Nov 2026 13:30:50 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1497115.1695416 (Exim 4.92) (envelope-from ) id 1xU9i2-0001kN-Lv; Thu, 19 Nov 2026 21:30:22 +0000 Received: by outflank-mailman (output) from mailman id 1497115.1695416; Thu, 19 Nov 2026 21:30:22 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9i2-0001iV-9D; Thu, 19 Nov 2026 21:30:22 +0000 Received: by outflank-mailman (input) for mailman id 1497115; Thu, 19 Nov 2026 21:30:19 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9hz-0001C9-3w for xen-devel@lists.xenproject.org; Thu, 19 Nov 2026 21:30:19 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xU9hy-00000006tGh-2qPi; Thu, 19 Nov 2026 21:30:19 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xU9Mr-0000000CVBH-1bAa; Thu, 19 Nov 2026 21:08:29 +0000 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" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=n43747u3O82/EPJ62jqYKY40YYs4o7PKgFzEzkqBCKU=; b=NkWsHYgGTBr6scTOOlPx2DpST3 iNJ77BKAsFDJB7DfHbZX8ixAljzqg9SAmx87lypUG67GxHuAFXhlJnDGSyWKw/lVhFfr5tf7AFSl4 mENURIyTXh020w2WYyCd8Wpvft9RKlJZf+eSQqa6zo0HvUgAYLUIpOuYeo6Z95bdBUXw=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie Subject: [PATCH v7 40/50] x86/mtrr: move the set_mtrr() rendezvous data off the stack Date: Thu, 19 Nov 2026 21:07:31 +0000 Message-ID: <20261119-asi-hvm-v7-40-6551b6d5c879@xenproject.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> References: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1795123851339158500 Content-Type: text/plain; charset="utf-8" Per-CPU stack mappings, to come as part of Address Space Isolation, give some contexts page-tables which map only the stack of the CPU they run on. A CPU in such a context cannot reach another CPU's stack, so an IPI handler cannot dereference a pointer into the initiating CPU's stack. Payloads handed to other CPUs via on_selected_cpus() must live somewhere globally reachable. set_mtrr() passes a pointer to a stack-allocated struct set_mtrr_data to all other CPUs. The payload is a live rendezvous object: the IPI handler decrements data->count and spins on data->gate in three phases, while the initiating CPU re-arms count and toggles gate. The dispatch is asynchronous (@wait =3D=3D 0), but set_mtrr() supplies its own barrier - it spins on data->count through the entire protocol and does not return until the rendezvous has completed - so the frame stays alive for as long as the callees use the data. The current code is correct; it just relies on cross-CPU stack reachability, which is going away. Make the rendezvous data a function-scope static. All fields are (re)initialised on every invocation before the IPI is sent, so the one new requirement is that set_mtrr() invocations be serialised: a static shares a single object across invocations where each call previously had its own private stack copy. mtrr_add_page() and mtrr_del_page() already call set_mtrr() with mtrr_mutex held; take the lock in mtrr_aps_sync_end() as well, and assert in set_mtrr() that it is held. Groundwork for per-CPU stack mappings, which take away a CPU's access to other CPUs' stacks: besides get_cpu_current(), cross-CPU rendezvous and call-function payloads are what reads another CPU's stack at run time. Assisted-by: Claude Code:claude-opus-4-8, Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Changes in v7: - New in this version. --- xen/arch/x86/cpu/mtrr/main.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/xen/arch/x86/cpu/mtrr/main.c b/xen/arch/x86/cpu/mtrr/main.c index 432d6f6761..2a7b544bb1 100644 --- a/xen/arch/x86/cpu/mtrr/main.c +++ b/xen/arch/x86/cpu/mtrr/main.c @@ -202,9 +202,16 @@ static void set_mtrr(unsigned int reg, unsigned long b= ase, { cpumask_t allbutself; unsigned int nr_cpus; - struct set_mtrr_data data; + /* + * Shared with the IPI handlers on the other CPUs, so not on the + * stack, which other CPUs need not be able to reach. All callers + * hold mtrr_mutex, which serialises access to the shared object. + */ + static struct set_mtrr_data data; unsigned long flags; =20 + ASSERT(spin_is_locked(&mtrr_mutex)); + cpumask_andnot(&allbutself, &cpu_online_map, cpumask_of(smp_processor_id())); nr_cpus =3D cpumask_weight(&allbutself); @@ -606,9 +613,11 @@ void mtrr_aps_sync_begin(void) =20 void mtrr_aps_sync_end(void) { + mutex_lock(&mtrr_mutex); if (mtrr_if) set_mtrr(~0U, 0, 0, 0); hold_mtrr_updates_on_aps =3D false; + mutex_unlock(&mtrr_mutex); } =20 void asmlinkage mtrr_bp_restore(void) --=20 2.56.0 From nobody Mon Nov 23 22:32:25 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=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1795123842; cv=none; d=zohomail.com; s=zohoarc; b=laG0uMR9jQsaCc+py/f+K9WPGjljVJX4vDg2MARp2RLP90scGx8IkEKEzdyZbrEtUEM/oCFiBTDBbqHbJW14cDAhsNFGmKDXLi0f8KnKq+VkigCDjOv4QJ+v4ksFQ6r5kNFKbg11Q8dPyMyo/u0Bynn8tT6lraTMCe24w/cGwrI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795123842; h=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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=rvQ243qMZ7smOnfCgSgEld4GpfGrWULEH1ValMJu3WU=; b=UuRO2ROz5iXuhbgtzN4jgyFsuuAXWj7DXF2HiZUr4CMTLXYzi71CKlNDPvRlZrr+1PMFgv9p1rionxvcAilDgrG95s5ZC4scmrlTnGpHPTv2Ye410FL0/ZDoicvp299zSdW+app32Tg/J68U2ZFhcgBPtcwCo8YvoN5TCpNUrUg= 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=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 179512384254180.76809439747967; Thu, 19 Nov 2026 13:30:42 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1497121.1695459 (Exim 4.92) (envelope-from ) id 1xU9i6-0002jN-NL; Thu, 19 Nov 2026 21:30:26 +0000 Received: by outflank-mailman (output) from mailman id 1497121.1695459; Thu, 19 Nov 2026 21:30:26 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9i6-0002gZ-1i; Thu, 19 Nov 2026 21:30:26 +0000 Received: by outflank-mailman (input) for mailman id 1497121; Thu, 19 Nov 2026 21:30:19 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9hz-0001CZ-8L for xen-devel@lists.xenproject.org; Thu, 19 Nov 2026 21:30:19 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xU9hy-00000006tH9-3Oey; Thu, 19 Nov 2026 21:30:19 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xU9Ms-0000000CVBH-1FqD; Thu, 19 Nov 2026 21:08:30 +0000 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" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=rvQ243qMZ7smOnfCgSgEld4GpfGrWULEH1ValMJu3WU=; b=G6IAnD/OnYQfXPzoiWBtWesCDI FN/4P/aUAqCwlcdNfGzL3dNlqylk/UWLp/9Fq3RNuRtJOKJ/ipQxwh23Df3QPgwbEIWX5Lh+1PX6b kf0M7QI5pD7dk1L3jSPLvidYbGLGWVj0Z+5Nj6jDboxSPh1I3MJ0wVZBnyW8MmSGo6RQ=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie Subject: [PATCH v7 41/50] x86/smp: move smp_send_stop()'s AP-stop flag off the stack Date: Thu, 19 Nov 2026 21:07:32 +0000 Message-ID: <20261119-asi-hvm-v7-41-6551b6d5c879@xenproject.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> References: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1795123843277158500 Content-Type: text/plain; charset="utf-8" Per-CPU stack mappings, to come as part of Address Space Isolation, give some contexts page-tables which map only the stack of the CPU they run on. A CPU in such a context cannot reach another CPU's stack, so an IPI handler cannot dereference a pointer into the initiating CPU's stack. Payloads handed to other CPUs via smp_call_function() must live somewhere globally reachable. smp_send_stop() hands the address of a stack-local bool to the other CPUs via an asynchronous (@wait =3D=3D 0) smp_call_function(). The callees then poll that bool from stop_this_cpu(), waiting for the initiating CPU to set it - which it does further down the same function, once the interrupt controllers have been disabled, before waiting for the APs to go offline. The initiator is executing smp_send_stop() the whole time the APs poll, so the frame stays alive and the current code is correct; but with per-CPU mapped stacks the flag would no longer be reachable and the poll would fault. Make the flag a function-scope static, re-initialised at function entry. The reset must remain an explicit statement rather than become an initialiser: on sequential reuse a stale "true" would release the APs before the interrupt controllers are quiesced. The signalling protocol is otherwise unchanged. Concurrent invocations of smp_send_stop() were not usefully supported before (the function disables interrupt controllers system-wide); a static flag does not change that. Groundwork for per-CPU stack mappings, which take away a CPU's access to other CPUs' stacks: besides get_cpu_current(), cross-CPU rendezvous and call-function payloads are what reads another CPU's stack at run time. Assisted-by: Claude Code:claude-opus-4-8, Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Changes in v7: - New in this version. --- xen/arch/x86/smp.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/xen/arch/x86/smp.c b/xen/arch/x86/smp.c index ae4e5a66d7..8a26c001e0 100644 --- a/xen/arch/x86/smp.c +++ b/xen/arch/x86/smp.c @@ -360,7 +360,15 @@ static void noreturn cf_check stop_this_cpu(void *dumm= y) void smp_send_stop(void) { unsigned int cpu =3D smp_processor_id(); - bool stop_aps =3D false; + /* + * Polled by the APs from the IPI handler, so not on the stack, which + * other CPUs need not be able to reach. Reset on every call, not by = an + * initialiser, so that a stale "true" cannot release the APs before t= he + * interrupt controllers below are quiesced. + */ + static bool stop_aps; + + stop_aps =3D false; =20 /* * Perform AP offlining and disabling of interrupt controllers with all --=20 2.56.0 From nobody Mon Nov 23 22:32:25 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=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1795123871; cv=none; d=zohomail.com; s=zohoarc; b=DlrWBFPg0j+nSZx3rQPfDrf+BmzC9gx2lPh2cTx9mdXgcPjpRHoAoOUTTW9v/cPItx2hn6KZ5P19U4qFSsIc051dHEK9z6nvZccTqBVCcVB/8nMhBtyw/dE3YQrSK/F+I4KLbwoR/EbmBPLf+J7E9eAUvel3BBstnhk1Tw4hK+4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795123871; h=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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=FImSAGTBdbDPxSFj5AW6ozzC1uAMZgaHWtEj5p+AjoI=; b=bgAba3yhp/fzo0KZZzuFyIp3TbdG1JyrdqveF17llnvAEf6FU2gzH1gfZYj2vLv/9oQoXT2LOQWqZHyv0H0uHddQ0RtgGCHbiK2dJqQo2s0uxbQWUNBqjn9yPQv1ak+mX2l4p03NJ2clRnw99JOIZhwVe3C7ldljMSdCKwxjCAA= 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=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1795123871416543.8599860703418; Thu, 19 Nov 2026 13:31:11 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1497136.1695592 (Exim 4.92) (envelope-from ) id 1xU9ia-0000G3-AG; Thu, 19 Nov 2026 21:30:56 +0000 Received: by outflank-mailman (output) from mailman id 1497136.1695592; Thu, 19 Nov 2026 21:30:55 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9iY-0000AU-IX; Thu, 19 Nov 2026 21:30:54 +0000 Received: by outflank-mailman (input) for mailman id 1497136; Thu, 19 Nov 2026 21:30:20 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9hz-0001E4-Ra for xen-devel@lists.xenproject.org; Thu, 19 Nov 2026 21:30:19 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xU9hz-00000006tJR-1qvb; Thu, 19 Nov 2026 21:30:19 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xU9Mt-0000000CVBH-0tCM; Thu, 19 Nov 2026 21:08:31 +0000 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" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=FImSAGTBdbDPxSFj5AW6ozzC1uAMZgaHWtEj5p+AjoI=; b=BcvwbX+DGTufA+o8BtYHkNw2Tx x6lVLxzScZaJqxoaB6nswgikYZAsOGnKy2m7v9DBUl4reuPTbcv5IuBJrvA4APdzkv/v+kPTudDIn OYtxK2mUFjg8V7ww9IJ8wdMWqYORpjUT083KSbv0K1D7GzaZWvSJNju87UChrpEUeTyA=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie Subject: [PATCH v7 42/50] x86/time: move the calibration rendezvous data off the stack Date: Thu, 19 Nov 2026 21:07:33 +0000 Message-ID: <20261119-asi-hvm-v7-42-6551b6d5c879@xenproject.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> References: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1795123873537158500 Content-Type: text/plain; charset="utf-8" Per-CPU stack mappings, to come as part of Address Space Isolation, give some contexts page-tables which map only the stack of the CPU they run on. A CPU in such a context cannot reach another CPU's stack, so an IPI handler cannot dereference a pointer into the initiating CPU's stack. Payloads handed to other CPUs via on_selected_cpus() must live somewhere globally reachable. time_calibration() passes a pointer to a stack-allocated struct calibration_rendezvous to all online CPUs. The payload is a shared rendezvous object: the handlers synchronise with each other through r->semaphore, the CPU0 handler publishes master_stime for the others to read, and the TSC variant additionally cmpxchg-loops on r->max_tsc_stamp. All of that requires every participating CPU to be able to reach the object. Make the rendezvous data a function-scope static, fully re-initialised on entry. Serialisation is already guaranteed: invocations come from calibration_timer, which is bound to CPU0, and at boot directly from verify_tsc_reliability(), also on CPU0, and the call uses @wait =3D=3D 1 precisely so that all CPUs are done with @r before the caller returns (the existing comment says as much), hence also before the next invocation can re-initialise it. Groundwork for per-CPU stack mappings, which take away a CPU's access to other CPUs' stacks: besides get_cpu_current(), cross-CPU rendezvous and call-function payloads are what reads another CPU's stack at run time. Assisted-by: Claude Code:claude-opus-4-8, Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Changes in v7: - New in this version. --- xen/arch/x86/time.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/xen/arch/x86/time.c b/xen/arch/x86/time.c index 0e5d06a9a8..68719a43d3 100644 --- a/xen/arch/x86/time.c +++ b/xen/arch/x86/time.c @@ -2390,7 +2390,14 @@ static void (*time_calibration_rendezvous_fn)(void *= ) =3D =20 static void cf_check time_calibration(void *unused) { - struct calibration_rendezvous r =3D { + /* + * Shared by the calibration handlers on every CPU, so not on the stac= k, + * which other CPUs need not be able to reach. Invocations are serial= ised + * (calibration_timer and verify_tsc_reliability(), both on CPU0). + */ + static struct calibration_rendezvous r; + + r =3D (struct calibration_rendezvous){ .semaphore =3D ATOMIC_INIT(0) }; =20 @@ -2403,7 +2410,7 @@ static void cf_check time_calibration(void *unused) =20 cpumask_copy(&r.cpu_calibration_map, &cpu_online_map); =20 - /* @wait=3D1 because we must wait for all cpus before freeing @r. */ + /* @wait=3D1 because we must wait for all cpus before reusing @r. */ on_selected_cpus(&r.cpu_calibration_map, time_calibration_rendezvous_fn, &r, 1); --=20 2.56.0 From nobody Mon Nov 23 22:32:25 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=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1795123862; cv=none; d=zohomail.com; s=zohoarc; b=B3SPhI1o0unwfvrW/XbCTU8jOx4OU1/kT98aMUyjDAjsrS6bVDUIXErpJ1Ci/LEBZD6P1cvHV4hjiWrl7NNI1ECyFPiKGm3qWfnIL6V2Z8TsDFYA8jPExthC3UIaZ9fwMYpWBkki7DdGGwmCHgFMHqATmcppKWL0HLamO1NaJPY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795123862; h=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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=qSfxZQRzDXOcJAE4M94MtHKjLXjCQE+CWFbbOYphgpY=; b=RTxUXsGeI0vs7wFMWpLSxyYu+YqRLRqirSIYAyxbuGQPJMVEIhMDMIIOAnBaB+tTwfsknrrOc1NayR4skqQhvvs04D8fSmgYGwBk7VVr2EtM54jAG6gUZB2A9HpOkC7QQv9DPzFvoMEWJNDBVUkeEJ2R+x5auM4hwWzO4j73P1Q= 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=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1795123862527582.740557991727; Thu, 19 Nov 2026 13:31:02 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1497124.1695500 (Exim 4.92) (envelope-from ) id 1xU9iD-00043o-NN; Thu, 19 Nov 2026 21:30:33 +0000 Received: by outflank-mailman (output) from mailman id 1497124.1695500; Thu, 19 Nov 2026 21:30:33 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9iC-000404-DD; Thu, 19 Nov 2026 21:30:32 +0000 Received: by outflank-mailman (input) for mailman id 1497124; Thu, 19 Nov 2026 21:30:19 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9hz-0001Cw-Bm for xen-devel@lists.xenproject.org; Thu, 19 Nov 2026 21:30:19 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xU9hy-00000006tH1-3GbN; Thu, 19 Nov 2026 21:30:19 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xU9Mu-0000000CVBH-05m2; Thu, 19 Nov 2026 21:08:32 +0000 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" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=qSfxZQRzDXOcJAE4M94MtHKjLXjCQE+CWFbbOYphgpY=; b=4HJJYeqDyh87R31aBLuLEBuPS3 E3cfL/ZsEATkBpzypBZa3e2CwCiTwWf1A4vbdX1++XE2PCACm1mYcCh5Dn/ZC+VzOlls61PTGQbXF mIAFQDUcwHyxp8oHRBZh/qp/gdfEnhr4o8cI+P3uI4FXhCICbLLLp2kB6O8ejnwrzokw=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie Subject: [PATCH v7 43/50] x86/ucode: move the stop_machine payload off the stack Date: Thu, 19 Nov 2026 21:07:34 +0000 Message-ID: <20261119-asi-hvm-v7-43-6551b6d5c879@xenproject.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> References: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1795123863454158500 Content-Type: text/plain; charset="utf-8" Per-CPU stack mappings, to come as part of Address Space Isolation, give some contexts page-tables which map only the stack of the CPU they run on. A CPU in such a context cannot reach another CPU's stack, so payloads handed to other CPUs - here via stop_machine_run() - must live somewhere globally reachable. ucode_update_hcall_cont() passes the address of a stack-local struct patch_with_flags to stop_machine_run(), which stashes the raw pointer in the (static) stopmachine_data and has every online CPU dereference it from do_microcode_update(). Make it a function-scope static, mirroring the adjacent nmi_patch. Both fields are written on every invocation before stop_machine_run() is called. Serialisation is already guaranteed: the continuation is always queued via continue_hypercall_on_cpu(0, ...), so invocations run one at a time in CPU0 tasklet context. Groundwork for per-CPU stack mappings, which take away a CPU's access to other CPUs' stacks: besides get_cpu_current(), cross-CPU rendezvous and call-function payloads are what reads another CPU's stack at run time. Assisted-by: Claude Code:claude-opus-4-8, Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Changes in v7: - New in this version. --- xen/arch/x86/cpu/microcode/core.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/xen/arch/x86/cpu/microcode/core.c b/xen/arch/x86/cpu/microcode= /core.c index 9b8d1e09cb..6f27d0b885 100644 --- a/xen/arch/x86/cpu/microcode/core.c +++ b/xen/arch/x86/cpu/microcode/core.c @@ -492,7 +492,12 @@ static long cf_check __maybe_unused ucode_update_hcall= _cont(void *data) int ret, result; struct ucode_buf *buffer =3D data; unsigned int cpu, updated; - struct patch_with_flags patch_with_flags; + /* + * Dereferenced by every CPU from the stop_machine context below, so n= ot + * on the stack, which other CPUs need not be able to reach. Invocati= ons + * are serialised: each runs as a CPU0 tasklet. + */ + static struct patch_with_flags patch_with_flags; bool ucode_force =3D buffer->flags & XENPF_UCODE_FORCE; =20 /* cpu_online_map must not change during update */ --=20 2.56.0 From nobody Mon Nov 23 22:32:25 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=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1795123850; cv=none; d=zohomail.com; s=zohoarc; b=bvcgNOfMEGRmKOjNgeJYaXeZU754KHv/WKo1unIthCih3C2QhWXDkapHILcZhMQam0ZiNoBjYg67LvVKKD9XlCfadoSP8fauHmquYrWvEVZINe/M6h8r2+4v1qJ9zNPWzHO7he8xLTw9Zr2QfKchGJrBu/KTSWfGeoHzOU8OlAk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795123850; h=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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=Bw8vBiNvfe8nBemBKR33mvKmNqZxsGnzT2Dqeg/h8o8=; b=MKv8saGhLeG5ojCijHORDc5SA/YahtcTg3rirIl6mjEKkh6ah6c27Zbu+O+LcC/pIpakhS3jdSiyDC3KUIL1NvgZd6HMphWqRW1c3cJ8G+VrZWm5qWb850JWH86miAZnSbSuYsMhMabv0Q4+Iqu2qJhaBBIvnwinB2G6RlfRzUs= 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=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1795123850433240.44740057074318; Thu, 19 Nov 2026 13:30:50 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1497117.1695426 (Exim 4.92) (envelope-from ) id 1xU9i3-0001z4-Jt; Thu, 19 Nov 2026 21:30:23 +0000 Received: by outflank-mailman (output) from mailman id 1497117.1695426; Thu, 19 Nov 2026 21:30:23 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9i3-0001wr-4B; Thu, 19 Nov 2026 21:30:23 +0000 Received: by outflank-mailman (input) for mailman id 1497117; Thu, 19 Nov 2026 21:30:19 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9hz-0001CI-5f for xen-devel@lists.xenproject.org; Thu, 19 Nov 2026 21:30:19 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xU9hy-00000006tGR-2Vi2; Thu, 19 Nov 2026 21:30:18 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xU9Mu-0000000CVBH-3yfc; Thu, 19 Nov 2026 21:08:33 +0000 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" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=Bw8vBiNvfe8nBemBKR33mvKmNqZxsGnzT2Dqeg/h8o8=; b=TMVMWliWxfDJAbxV+u8QfhYgCm FXsGLBrkSxb033O1ZTsYYaA6RUQhNvR/EgD511rYXxxBAglGeDJX5radCHhzUE/TRP98pMl1ti/hu Lytlhmeu8tfUT2Rt+pVnQCqJ2xlvTiLmurBgHgnYcioBg6+Fd6En+j0BBhCwe9vwsdbM=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie Subject: [PATCH v7 44/50] x86/MCE: pass NULL to x86_mc_mceinject() Date: Thu, 19 Nov 2026 21:07:35 +0000 Message-ID: <20261119-asi-hvm-v7-44-6551b6d5c879@xenproject.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> References: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1795123851325158500 Content-Type: text/plain; charset="utf-8" x86_mc_mceinject() simulates a machine check on the CPU it runs on, and never reads its argument. The XEN_MC_inject_v2 calls pass it NULL, but the two XEN_MC_mceinject calls pass a pointer to do_mca()'s copy of the hypercall argument, which is on do_mca()'s stack. Pass NULL there too, and name the parameter "unused". No functional change. Groundwork for per-CPU stack mappings, to come as part of Address Space Isolation, which take away a CPU's access to other CPUs' stacks: a subsequent change asserts that a call-function payload passed by pointer does not point into the initiator's stack, and these calls have no payload to pass. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Changes in v7: - New in this version. --- xen/arch/x86/cpu/mcheck/mce.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/xen/arch/x86/cpu/mcheck/mce.c b/xen/arch/x86/cpu/mcheck/mce.c index 1888f22d15..842a80977c 100644 --- a/xen/arch/x86/cpu/mcheck/mce.c +++ b/xen/arch/x86/cpu/mcheck/mce.c @@ -1247,7 +1247,7 @@ static void cf_check __maybe_unused x86_mc_msrinject(= void *data) x86_mc_hwcr_wren_restore(hwcr); } =20 -static void cf_check __maybe_unused x86_mc_mceinject(void *data) +static void cf_check __maybe_unused x86_mc_mceinject(void *unused) { printk("Simulating #MC on cpu %d\n", smp_processor_id()); __asm__ __volatile__("int $0x12"); @@ -1556,10 +1556,9 @@ long do_mca(XEN_GUEST_HANDLE_PARAM(xen_mc_t) u_xen_m= c) add_taint(TAINT_ERROR_INJECT); =20 if ( mce_broadcast ) - on_each_cpu(x86_mc_mceinject, mc_mceinject, 1); + on_each_cpu(x86_mc_mceinject, NULL, 1); else - on_selected_cpus(cpumask_of(target), x86_mc_mceinject, - mc_mceinject, 1); + on_selected_cpus(cpumask_of(target), x86_mc_mceinject, NULL, 1= ); break; =20 case XEN_MC_inject_v2: --=20 2.56.0 From nobody Mon Nov 23 22:32:25 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=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1795123877; cv=none; d=zohomail.com; s=zohoarc; b=Nwn02Ow1zqZ4FKSko9YRJqIjfvaSBp8699jYHAgIlg+h2iR1Hsp5SoRbpTTJ03F/tli22k6aYiw+oYVs0J910uhBIGZ45csAf/uaqMiAdXzyVCd9XR+kRLTzMwtu4/ibnIzV9h2eMrV8bJbHXTEGhuxBVyrLKGo8kefOCpgDzc0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795123877; h=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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=Soc8NB4lvXkdupVTEB5EOLyLQSbxZ3jeOfxme/bIcNg=; b=LPrGitLm4ArBEYs3Wu/uqpjxKyzHi6mybkP+iRKJ1vzKVLG3IK7a2wEYHq0p1ZVC/ZdJjI5AjSUD7QtKMWRSNu7tbiALUV/dhsCLxgb6JfjAETpVsIbi4eu0Nzo+EPxaYkfmbChFGkhQJB8epFok1kUL7QZGZoWPH9heiglafwQ= 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=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1795123877653201.98162822748372; Thu, 19 Nov 2026 13:31:17 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1497133.1695567 (Exim 4.92) (envelope-from ) id 1xU9iT-0007H5-No; Thu, 19 Nov 2026 21:30:49 +0000 Received: by outflank-mailman (output) from mailman id 1497133.1695567; Thu, 19 Nov 2026 21:30:48 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9iR-000775-I8; Thu, 19 Nov 2026 21:30:47 +0000 Received: by outflank-mailman (input) for mailman id 1497133; Thu, 19 Nov 2026 21:30:19 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9hz-0001Dg-Mp for xen-devel@lists.xenproject.org; Thu, 19 Nov 2026 21:30:19 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xU9hz-00000006tIm-0w8g; Thu, 19 Nov 2026 21:30:19 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xU9Mw-0000000CVBH-20wh; Thu, 19 Nov 2026 21:08:34 +0000 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" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=Soc8NB4lvXkdupVTEB5EOLyLQSbxZ3jeOfxme/bIcNg=; b=GbllUmmWHScCsRTJfUEzeSVfJa nRgjDrL/LqZNUq4nTasncU0e8gh6EkIPfw4kcXRWVabYw4GpS+lZJDlBbzHoemya8iOYlogzhHFFj 9/RGCoHkrFJHpAEoJVvNsG2HTS1Ut1YUS5uF1gfR50EKHy0Wfs7t8FIfKJXickosyJL0=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Andrew Cooper , Anthony PERARD , Michal Orzel , Jan Beulich , Julien Grall , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Stefano Stabellini Subject: [PATCH v7 45/50] xen/smp: introduce by-value payloads for call-function IPIs Date: Thu, 19 Nov 2026 21:07:36 +0000 Message-ID: <20261119-asi-hvm-v7-45-6551b6d5c879@xenproject.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> References: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1795123879749158500 Content-Type: text/plain; charset="utf-8" Per-CPU stack mappings, to come as part of Address Space Isolation, give some contexts page-tables which map only the stack of the CPU they run on. A CPU in such a context cannot reach another CPU's stack, so a call-function callee cannot dereference a pointer into the initiator's stack frame, however long that frame stays alive. on_selected_cpus() passes @info as an opaque pointer, and a number of callers point it at a local variable; those payloads need a globally reachable home. The in-tree callers are correct today. The @wait =3D=3D 1 ones hold their frame across the call by definition, and every @wait =3D=3D 0 one keeps its frame alive for as long as the callees use the payload - by spinning on its own completion protocol, or by never returning. But the API itself imposes no lifetime rule: with @wait =3D=3D 0 the callee acknowledges the IPI before invoking @func, so nothing stops a future caller from unwinding while a callee still holds the pointer. The new API closes off that possible misuse as well. Add on_selected_cpus_data() and on_selected_cpus_data_inout(), which take an additional @size and pass the payload by value: @size bytes are copied into a bounce buffer within the (call_lock-serialised, statically allocated) call_data, and callees receive a pointer to that copy. The two differ in the payload's direction: * on_selected_cpus_data(): IN only, with either @wait. It copies nothing back. With @wait =3D=3D 1, callees run on the shared bounce copy directly - the buffer cannot be reused while the initiator holds call_lock. In debug builds the initiator checks, once every callee has acknowledged, that the copy still matches the caller's object. A callee which writes an IN payload means that its caller chose the wrong call; in a release build the result is lost, nothing worse, so the check is an ASSERT(). With @wait =3D=3D 0, each callee copies the payload to a private buffer strictly before clearing its bit in call_data.selected. The initiator spins on that mask (even for @wait =3D=3D 0) before releasing call_lock, so every callee's copy completes before the slot can be reused, and after the ACK a callee only touches its private copy: payload lifetime is independent of the caller's frame by construction. * on_selected_cpus_data_inout(): IN/OUT, always synchronous, as an asynchronous call could not return results. Callees run on the shared bounce copy, and its final contents are copied back to the caller's object before return. OUT parameters and atomic RMW payload fields behave exactly as with a plain pointer today. Both take a plain pointer, even for IN only: their shared implementation copies from and, for IN/OUT, back to the caller's object through that one pointer, rather than through two pointers to the same object. The initiator also issues an smp_rmb() between observing the final ACK and its subsequent reads. This orders the copy-back and the debug check after the ACK-observing loads on architectures with relaxed load-load ordering, and equally covers the pattern of a caller reading callee-written data through a by-pointer @info after return. Interior pointers are copied as-is, so anything a callee dereferences through them must be globally reachable; this, the size cap and the 16-byte alignment cap (from the callee-side private copy) are spelled out at the declaration. The typed wrappers, on_selected_cpus_obj() and on_selected_cpus_obj_inout(), infer the size from the pointed-to type and turn an oversized or over-aligned payload into a build failure. CALLFUNC_DATA_MAX is 192 bytes: the largest in-tree by-value payload is struct xen_mc_msrinject (144 bytes), followed by struct hw_residencies (112). Note that no payload embeds a cpumask_t, keeping the buffer size independent of CONFIG_NR_CPUS. on_selected_cpus() shares their implementation, a static _on_selected_cpus(), and passes its pointer through as before. smp_call_function() and on_each_cpu() get no by-value variants: none of their callers passes a pointer into its stack. Apart from the smp_rmb(), a compiler barrier on x86, no functional change: nothing calls the new functions yet. Groundwork for per-CPU stack mappings, which take away a CPU's access to other CPUs' stacks: besides get_cpu_current(), cross-CPU rendezvous and call-function payloads are what reads another CPU's stack at run time. Assisted-by: Claude Code:claude-opus-4-8, Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Changes in v7: - New in this version. --- xen/common/smp.c | 89 ++++++++++++++++++++++++++++++++++++++++++- xen/include/xen/smp.h | 66 +++++++++++++++++++++++++++++++- 2 files changed, 152 insertions(+), 3 deletions(-) diff --git a/xen/common/smp.c b/xen/common/smp.c index a011f541f1..eb419a3aeb 100644 --- a/xen/common/smp.c +++ b/xen/common/smp.c @@ -18,8 +18,10 @@ =20 #include #include +#include #include #include +#include =20 /* * Structure and data for smp_call_function()/on_selected_cpus(). @@ -29,7 +31,10 @@ static struct call_data_struct { void (*func) (void *info); void *info; int wait; + unsigned int size; cpumask_t selected; + /* Bounce buffer for payloads passed by value (@size !=3D 0). */ + uint8_t buf[CALLFUNC_DATA_MAX] __aligned(SMP_CACHE_BYTES); } call_data; =20 void smp_call_function( @@ -44,14 +49,24 @@ void smp_call_function( on_selected_cpus(&allbutself, func, info, wait); } =20 -void on_selected_cpus( +/* + * The by-pointer and by-value calls in one. By pointer (@size =3D=3D 0),= @func + * gets @info itself. By value, @func gets a copy of the @size bytes at + * @info, made under call_lock; with @copy_back, a synchronous call copies + * the results back to @info before returning. + */ +static void _on_selected_cpus( const cpumask_t *selected, void (*func) (void *info), void *info, + unsigned int size, + bool copy_back, int wait) { ASSERT(local_irq_is_enabled()); ASSERT(cpumask_subset(selected, &cpu_online_map)); + BUG_ON(size > CALLFUNC_DATA_MAX); + BUG_ON(size && !info); =20 spin_lock(&call_lock); =20 @@ -61,18 +76,74 @@ void on_selected_cpus( goto out; =20 call_data.func =3D func; - call_data.info =3D info; call_data.wait =3D wait; + call_data.size =3D size; + if ( size ) + { + memcpy(call_data.buf, info, size); + call_data.info =3D call_data.buf; + } + else + call_data.info =3D info; =20 smp_send_call_function_mask(&call_data.selected); =20 while ( !cpumask_empty(&call_data.selected) ) cpu_relax(); =20 + /* + * Order the loads which observed the ACKs before reads of what the + * callees wrote: the copy-back or check below, and the caller's own + * reads through a by-pointer @info. + */ + smp_rmb(); + + /* + * With @wait =3D=3D 1 the callees operated directly on the bounce buf= fer + * (which cannot be reused while call_lock is held): propagate their + * results back into the caller's object, or check that none of them + * wrote an IN-only payload. + */ + if ( size && wait ) + { + if ( copy_back ) + memcpy(info, call_data.buf, size); + else + ASSERT(!memcmp(call_data.buf, info, size)); + } + out: spin_unlock(&call_lock); } =20 +void on_selected_cpus( + const cpumask_t *selected, + void (*func) (void *info), + void *info, + int wait) +{ + _on_selected_cpus(selected, func, info, 0, false, wait); +} + +void on_selected_cpus_data( + const cpumask_t *selected, + void (*func) (void *info), + void *in, + unsigned int size, + int wait) +{ + _on_selected_cpus(selected, func, in, size, false, wait); +} + +void on_selected_cpus_data_inout( + const cpumask_t *selected, + void (*func) (void *info), + void *inout, + unsigned int size) +{ + _on_selected_cpus(selected, func, inout, size, true, 1); +} + void smp_call_function_interrupt(void) { void (*func)(void *info) =3D call_data.func; @@ -94,6 +165,20 @@ void smp_call_function_interrupt(void) smp_mb(); cpumask_clear_cpu(cpu, &call_data.selected); } + else if ( call_data.size ) + { + /* + * Asynchronous call with a by-value payload: copy it before the A= CK, + * after which the initiator may return and the buffer be reused. + */ + uint8_t buf[CALLFUNC_DATA_MAX] __aligned(16); + unsigned int size =3D call_data.size; + + memcpy(buf, call_data.buf, size); + smp_mb(); + cpumask_clear_cpu(cpu, &call_data.selected); + (*func)(buf); + } else { smp_mb(); diff --git a/xen/include/xen/smp.h b/xen/include/xen/smp.h index 2ca9ff1bfc..73bf7075d6 100644 --- a/xen/include/xen/smp.h +++ b/xen/include/xen/smp.h @@ -1,6 +1,8 @@ #ifndef __XEN_SMP_H__ #define __XEN_SMP_H__ =20 +#include + #include =20 #ifndef cpu_is_offline @@ -36,7 +38,7 @@ extern void smp_call_function( void *info, int wait); =20 -/*=20 +/* * Call a function on a selection of processors */ extern void on_selected_cpus( @@ -45,6 +47,68 @@ extern void on_selected_cpus( void *info, int wait); =20 +/* + * Maximum size of a by-value payload. Objects whose size scales with the + * configuration (such as a cpumask_t at large NR_CPUS) must not be passed= by + * value. Payloads must not require alignment beyond 16 bytes (the + * callee-side copy's alignment); the _obj() wrappers enforce both bounds = at + * build time. + */ +#define CALLFUNC_DATA_MAX 192 + +/* + * Call a function on a selection of processors, passing the payload by + * value: @size bytes at @in are copied into a global bounce buffer + * under the call lock, and @func is invoked with a pointer to that copy. + * Use this whenever the payload would otherwise live on the caller's + * stack, which other CPUs need not be able to reach. + * + * The payload is IN only: nothing is copied back, and @func must not + * write it. With @wait =3D=3D 1 all callees share the (locked) bounce bu= ffer, + * and debug builds check that it is unchanged on return. With @wait =3D= =3D 0 + * each callee consumes a private copy before acknowledging, making payload + * lifetime independent of the caller's frame. + * + * Interior pointers within the payload are copied as-is: whatever a callee + * dereferences through them must itself be globally reachable (not + * stack-allocated). + */ +extern void on_selected_cpus_data( + const cpumask_t *selected, + void (*func) (void *info), + void *in, + unsigned int size, + int wait); + +/* + * As on_selected_cpus_data(), for a payload which @func writes results to + * (IN/OUT). The call is synchronous, since an asynchronous one could not + * return results: all callees share the bounce buffer, and its final + * contents are copied back to @inout before return, so OUT parameters and + * atomic read-modify-write payload fields keep working. + */ +extern void on_selected_cpus_data_inout( + const cpumask_t *selected, + void (*func) (void *info), + void *inout, + unsigned int size); + +/* + * Typed convenience wrappers: pass the object @objp points at by value, + * with the size inferred and bounds-checked at build time. + */ +#define on_selected_cpus_obj(mask, func, objp, wait) ({ \ + BUILD_BUG_ON(sizeof(*(objp)) > CALLFUNC_DATA_MAX); \ + BUILD_BUG_ON(alignof(*(objp)) > 16); \ + on_selected_cpus_data(mask, func, objp, sizeof(*(objp)), wait); \ +}) + +#define on_selected_cpus_obj_inout(mask, func, objp) ({ \ + BUILD_BUG_ON(sizeof(*(objp)) > CALLFUNC_DATA_MAX); \ + BUILD_BUG_ON(alignof(*(objp)) > 16); \ + on_selected_cpus_data_inout(mask, func, objp, sizeof(*(objp))); \ +}) + /* * Mark the boot cpu "online" so that it can call console drivers in * printk() and can access its per-cpu storage. --=20 2.56.0 From nobody Mon Nov 23 22:32:25 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=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1795123859; cv=none; d=zohomail.com; s=zohoarc; b=Wl2b0d/PzpQ8FIrOp9GLoYTsW/9m5axEWglWAPY91hwT9uQ6t7t26lLM5F1miS7HetmdU8F9yXPs4yFJ/waTHwFLyDmqnw5+zpZWyuXt+NL0ms5vVIV93ag38lNEDUqIkadbvrRXJqOBVUaZhs1EeS2dlHrZpivtz7/1CgfihMY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795123859; h=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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=sTjqPTsK2pi81b/09BzAb/eLP8sEK4iy5zRkCRfHDW0=; b=XUdYcG9FbFAl7EUYjIirTgCIMJcBCgb9Uu7eVEMNfqYBAKGxciSkKUY7u7RxJAeg+3SoBWYVB8cr97ASRU5WZqXeiLnShk548oqZHMy4LjVXOK+jVx5LLbp2SE4w6iRU92viHMSSMCeVqFQ8YxK7pd3WibK/NZwy66goyzW5VkQ= 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=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1795123859962466.88377385711226; Thu, 19 Nov 2026 13:30:59 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1497126.1695515 (Exim 4.92) (envelope-from ) id 1xU9iH-0004eW-1F; Thu, 19 Nov 2026 21:30:37 +0000 Received: by outflank-mailman (output) from mailman id 1497126.1695515; Thu, 19 Nov 2026 21:30:36 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9iF-0004YI-C5; Thu, 19 Nov 2026 21:30:35 +0000 Received: by outflank-mailman (input) for mailman id 1497126; Thu, 19 Nov 2026 21:30:19 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9hz-0001D8-F6 for xen-devel@lists.xenproject.org; Thu, 19 Nov 2026 21:30:19 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xU9hy-00000006tHR-3jIy; Thu, 19 Nov 2026 21:30:19 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xU9Mx-0000000CVBH-1fqM; Thu, 19 Nov 2026 21:08:35 +0000 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" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=sTjqPTsK2pi81b/09BzAb/eLP8sEK4iy5zRkCRfHDW0=; b=S4up3/DHzCHOKIGFdVJG22hkro tmfg2V1atfdWR1N8wLqNJJoXuYmizxcRET+UIJRqNitqomHdRk9s0DS+UeRrBdRAQ6FNMRDj/bZfV Eura4W4LRw70HVNoxwVt+FikfwA3RYIBtiAf1sLpvUv86YeHwwErXyEBmPlpBXeyntPQ=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie Subject: [PATCH v7 46/50] x86/cpufreq: move the CPU mask out of struct drv_cmd Date: Thu, 19 Nov 2026 21:07:37 +0000 Message-ID: <20261119-asi-hvm-v7-46-6551b6d5c879@xenproject.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> References: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1795123861437158500 Content-Type: text/plain; charset="utf-8" drv_read() and drv_write() take a struct drv_cmd, whose mask field says which CPUs to run the access on. Only the initiator uses the mask, to route the call: do_drv_read() and do_drv_write(), which run on the target CPUs, never read it. It may point at the initiator's stack (acpi_cpufreq_target()'s online_policy_cpus). Drop the field and pass the mask to drv_read() and drv_write() as a parameter, so that the structure handed to the target CPUs holds only what they use. No functional change. Groundwork for per-CPU stack mappings, to come as part of Address Space Isolation, which take away a CPU's access to other CPUs' stacks: a subsequent change passes the structure to the target CPUs by value, and a copy must carry no stack address. Assisted-by: Claude Code:claude-opus-4-8, Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Changes in v7: - New in this version. --- xen/arch/x86/acpi/cpufreq/acpi.c | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/xen/arch/x86/acpi/cpufreq/acpi.c b/xen/arch/x86/acpi/cpufreq/a= cpi.c index de67f1aee2..7f843bab60 100644 --- a/xen/arch/x86/acpi/cpufreq/acpi.c +++ b/xen/arch/x86/acpi/cpufreq/acpi.c @@ -90,7 +90,6 @@ typedef union { =20 struct drv_cmd { unsigned int type; - const cpumask_t *mask; drv_addr_union addr; u32 val; }; @@ -137,25 +136,25 @@ static void cf_check do_drv_write(void *drvcmd) } } =20 -static void drv_read(struct drv_cmd *cmd) +static void drv_read(const cpumask_t *mask, struct drv_cmd *cmd) { cmd->val =3D 0; =20 - ASSERT(cpumask_weight(cmd->mask) =3D=3D 1); + ASSERT(cpumask_weight(mask) =3D=3D 1); =20 /* to reduce IPI for the sake of performance */ - if (likely(cpumask_test_cpu(smp_processor_id(), cmd->mask))) + if (likely(cpumask_test_cpu(smp_processor_id(), mask))) do_drv_read((void *)cmd); else - on_selected_cpus(cmd->mask, do_drv_read, cmd, 1); + on_selected_cpus(mask, do_drv_read, cmd, 1); } =20 -static void drv_write(struct drv_cmd *cmd) +static void drv_write(const cpumask_t *mask, struct drv_cmd *cmd) { - if (cpumask_equal(cmd->mask, cpumask_of(smp_processor_id()))) + if (cpumask_equal(mask, cpumask_of(smp_processor_id()))) do_drv_write((void *)cmd); else - on_selected_cpus(cmd->mask, do_drv_write, cmd, 1); + on_selected_cpus(mask, do_drv_write, cmd, 1); } =20 static u32 get_cur_val(const cpumask_t *mask) @@ -193,9 +192,7 @@ static u32 get_cur_val(const cpumask_t *mask) return 0; } =20 - cmd.mask =3D cpumask_of(cpu); - - drv_read(&cmd); + drv_read(cpumask_of(cpu), &cmd); return cmd.val; } =20 @@ -256,6 +253,7 @@ static int cf_check acpi_cpufreq_target( struct processor_performance *perf; struct cpufreq_freqs freqs; cpumask_t online_policy_cpus; + const cpumask_t *mask; struct drv_cmd cmd; unsigned int next_state =3D 0; /* Index into freq_table */ unsigned int next_perf_state =3D 0; /* Index into perf table */ @@ -304,16 +302,16 @@ static int cf_check acpi_cpufreq_target( } =20 if (policy->shared_type !=3D CPUFREQ_SHARED_TYPE_ANY) - cmd.mask =3D &online_policy_cpus; + mask =3D &online_policy_cpus; else - cmd.mask =3D cpumask_of(policy->cpu); + mask =3D cpumask_of(policy->cpu); =20 freqs.old =3D perf->states[perf->state].core_frequency * 1000; freqs.new =3D data->freq_table[next_state].frequency; =20 - drv_write(&cmd); + drv_write(mask, &cmd); =20 - if (acpi_pstate_strict && !check_freqs(cmd.mask, freqs.new, data)) { + if (acpi_pstate_strict && !check_freqs(mask, freqs.new, data)) { printk(KERN_WARNING "Fail transfer to new freq %d\n", freqs.new); return -EAGAIN; } --=20 2.56.0 From nobody Mon Nov 23 22:32:25 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=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1795123911; cv=none; d=zohomail.com; s=zohoarc; b=cOtLGMbHV74uZlMVFhdt89krmuqh33E1uhqz7hlFQ1k00su+NsoMO/jCCQYcWeAktx0OYRcxfAO46ep1ivLZNmd4agikcNKFmATQZV/rBozFf++TrfEp0le0x9cb3frFfJC7HUFPQln5o7xr0SS9Kevxgmr53zqC++P2PjMgXME= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795123911; h=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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=UiGsh13eTDf46yW7Z6NV5/v75M5dRGpgpJvWXGlTvhQ=; b=ahCVAGIXrbcB++WYavCjXa01QkMjDskA6bpC/yF8jr6lpBGzkA1O4RDYw5gdkhnYZs4uebU/AqHQZPEv71tuy/zT6JJeEdruw0EKcMnEpccNWFrq2MWo75tygteHQTPaTQB2GlQbSTPmAuWc+KAIcQRZiAbE4ORtHuDGVSr52xY= 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=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 179512391169019.84720293471355; Thu, 19 Nov 2026 13:31:51 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1497148.1695710 (Exim 4.92) (envelope-from ) id 1xU9j3-0006UI-Oa; Thu, 19 Nov 2026 21:31:25 +0000 Received: by outflank-mailman (output) from mailman id 1497148.1695710; Thu, 19 Nov 2026 21:31:25 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9j2-0006NN-Er; Thu, 19 Nov 2026 21:31:24 +0000 Received: by outflank-mailman (input) for mailman id 1497148; Thu, 19 Nov 2026 21:30:20 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9i0-0001F5-9z for xen-devel@lists.xenproject.org; Thu, 19 Nov 2026 21:30:20 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xU9hz-00000006tKy-3eKd; Thu, 19 Nov 2026 21:30:20 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xU9My-0000000CVBH-1JqW; Thu, 19 Nov 2026 21:08:36 +0000 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" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=UiGsh13eTDf46yW7Z6NV5/v75M5dRGpgpJvWXGlTvhQ=; b=f45usI235udj1YiYdPMynlhUXq 58dDrrMMnRF9ZiFhuph2oeVWIoqij6F8aI/Emn1tjVODAEmANJ5qypfQCcZOB2N2VMpr7BTcqtqOL GI0Hnz9hI1/12pV/czb0ZEP9uBgwijgaxZEElde6zpFmsv6KpbUHmKZbTIm10ufvQpIA=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie Subject: [PATCH v7 47/50] x86/acpi: pass stack-allocated callfunc payloads by value Date: Thu, 19 Nov 2026 21:07:38 +0000 Message-ID: <20261119-asi-hvm-v7-47-6551b6d5c879@xenproject.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> References: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1795123911918158500 Content-Type: text/plain; charset="utf-8" Per-CPU stack mappings, to come as part of Address Space Isolation, give some contexts page-tables which map only the stack of the CPU they run on. A CPU in such a context cannot reach another CPU's stack, so pointers into a caller's stack cannot be dereferenced by call-function callees. Several ACPI/cpufreq paths hand another CPU a pointer to a local variable via on_selected_cpus(), for the callee to read and (in most cases) write results back through: * drv_read()/drv_write(): struct drv_cmd on the caller's stack; the callee reads it and do_drv_read() writes cmd->val back. * get_measured_perf(): struct perf_pair, filled by read_measured_perf_ctrs() on the target CPU. * powernow transition_pstate(): the next P-state number. * powernow get_cpu_data(): struct amd_cpu_data; max_hw_pstate comes back, info->perf points at the (heap) processor_pminfo. * get_hw_residencies(): struct hw_residencies, filled by the callee. * arch_acpi_set_pdc_bits(): the MWAIT CPUID ecx value. Switch these calls to pass the payload by value. Where the callee writes results back (do_drv_read(), read_measured_perf_ctrs(), get_cpu_data(), do_get_hw_residencies(), get_mwait_ecx()), use on_selected_cpus_obj_inout(), which copies them back on return; where it only reads (do_drv_write(), transition_pstate()), use on_selected_cpus_obj(). All the calls are synchronous (@wait =3D=3D 1), so behaviour is unchanged. amd_cpu_data's interior pointer (perf) references heap data, which remains globally reachable. drv_cmd carries no stack address: its mask, which could point at the initiator's stack, is a parameter of drv_read() and drv_write(). No functional change. Groundwork for per-CPU stack mappings, which take away a CPU's access to other CPUs' stacks: besides get_cpu_current(), cross-CPU rendezvous and call-function payloads are what reads another CPU's stack at run time. Assisted-by: Claude Code:claude-opus-4-8, Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Changes in v7: - New in this version. --- xen/arch/x86/acpi/cpu_idle.c | 3 ++- xen/arch/x86/acpi/cpufreq/acpi.c | 4 ++-- xen/arch/x86/acpi/cpufreq/cpufreq.c | 4 ++-- xen/arch/x86/acpi/cpufreq/powernow.c | 6 +++--- xen/arch/x86/acpi/lib.c | 3 ++- 5 files changed, 11 insertions(+), 9 deletions(-) diff --git a/xen/arch/x86/acpi/cpu_idle.c b/xen/arch/x86/acpi/cpu_idle.c index 874346199e..53b7e79ed7 100644 --- a/xen/arch/x86/acpi/cpu_idle.c +++ b/xen/arch/x86/acpi/cpu_idle.c @@ -298,7 +298,8 @@ static void get_hw_residencies(uint32_t cpu, struct hw_= residencies *hw_res) if ( smp_processor_id() =3D=3D cpu ) do_get_hw_residencies(hw_res); else - on_selected_cpus(cpumask_of(cpu), do_get_hw_residencies, hw_res, 1= ); + on_selected_cpus_obj_inout(cpumask_of(cpu), do_get_hw_residencies, + hw_res); } =20 static void print_hw_residencies(uint32_t cpu) diff --git a/xen/arch/x86/acpi/cpufreq/acpi.c b/xen/arch/x86/acpi/cpufreq/a= cpi.c index 7f843bab60..b16058201f 100644 --- a/xen/arch/x86/acpi/cpufreq/acpi.c +++ b/xen/arch/x86/acpi/cpufreq/acpi.c @@ -146,7 +146,7 @@ static void drv_read(const cpumask_t *mask, struct drv_= cmd *cmd) if (likely(cpumask_test_cpu(smp_processor_id(), mask))) do_drv_read((void *)cmd); else - on_selected_cpus(mask, do_drv_read, cmd, 1); + on_selected_cpus_obj_inout(mask, do_drv_read, cmd); } =20 static void drv_write(const cpumask_t *mask, struct drv_cmd *cmd) @@ -154,7 +154,7 @@ static void drv_write(const cpumask_t *mask, struct drv= _cmd *cmd) if (cpumask_equal(mask, cpumask_of(smp_processor_id()))) do_drv_write((void *)cmd); else - on_selected_cpus(mask, do_drv_write, cmd, 1); + on_selected_cpus_obj(mask, do_drv_write, cmd, 1); } =20 static u32 get_cur_val(const cpumask_t *mask) diff --git a/xen/arch/x86/acpi/cpufreq/cpufreq.c b/xen/arch/x86/acpi/cpufre= q/cpufreq.c index 9ef62b3538..683139731f 100644 --- a/xen/arch/x86/acpi/cpufreq/cpufreq.c +++ b/xen/arch/x86/acpi/cpufreq/cpufreq.c @@ -100,8 +100,8 @@ unsigned int get_measured_perf(unsigned int cpu, unsign= ed int flag) if (cpu =3D=3D smp_processor_id()) { read_measured_perf_ctrs((void *)&readin); } else { - on_selected_cpus(cpumask_of(cpu), read_measured_perf_ctrs, - &readin, 1); + on_selected_cpus_obj_inout(cpumask_of(cpu), read_measured_perf_ctr= s, + &readin); } =20 cur.aperf.whole =3D readin.aperf.whole - saved->aperf.whole; diff --git a/xen/arch/x86/acpi/cpufreq/powernow.c b/xen/arch/x86/acpi/cpufr= eq/powernow.c index 55166eac72..4ab3d916ca 100644 --- a/xen/arch/x86/acpi/cpufreq/powernow.c +++ b/xen/arch/x86/acpi/cpufreq/powernow.c @@ -121,8 +121,8 @@ static int cf_check powernow_cpufreq_target( =20 if (policy->shared_type =3D=3D CPUFREQ_SHARED_TYPE_ALL || unlikely(policy->cpu !=3D smp_processor_id())) - on_selected_cpus(&online_policy_cpus, transition_pstate, - &next_perf_state, 1); + on_selected_cpus_obj(&online_policy_cpus, transition_pstate, + &next_perf_state, 1); else transition_pstate(&next_perf_state); =20 @@ -252,7 +252,7 @@ static int cf_check powernow_cpufreq_cpu_init(struct cp= ufreq_policy *policy) =20 policy->governor =3D cpufreq_opt_governor ? : CPUFREQ_DEFAULT_GOVERNOR; =20 - on_selected_cpus(cpumask_of(cpu), get_cpu_data, &info, 1); + on_selected_cpus_obj_inout(cpumask_of(cpu), get_cpu_data, &info); =20 /* table init */ for (i =3D 0; i < perf->state_count && i <=3D info.max_hw_pstate; i++)= { diff --git a/xen/arch/x86/acpi/lib.c b/xen/arch/x86/acpi/lib.c index 51cb082ca0..02061cdd30 100644 --- a/xen/arch/x86/acpi/lib.c +++ b/xen/arch/x86/acpi/lib.c @@ -137,7 +137,8 @@ int arch_acpi_set_pdc_bits(u32 acpi_id, u32 *pdc, u32 m= ask) else if (c =3D=3D &boot_cpu_data || cpu =3D=3D smp_processor_id()) ecx =3D cpuid_ecx(CPUID_MWAIT_LEAF); else - on_selected_cpus(cpumask_of(cpu), get_mwait_ecx, &ecx, 1); + on_selected_cpus_obj_inout(cpumask_of(cpu), get_mwait_ecx, + &ecx); if (!(ecx & CPUID5_ECX_EXTENSIONS_SUPPORTED) || !(ecx & CPUID5_ECX_INTERRUPT_BREAK)) pdc[2] &=3D ~(ACPI_PDC_C_C1_FFH | ACPI_PDC_C_C2C3_FFH); --=20 2.56.0 From nobody Mon Nov 23 22:32:25 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=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1795123846; cv=none; d=zohomail.com; s=zohoarc; b=WY7R8Q/Ul3huZg+u6S7PkRi+TVtHfi2UqCHk2y74IQJtRZk8TjpieoY5wiDVrw+pMFgiqIMv3keeUO1a6UyrCMtwSkE9P0YYKmBLETAVYBuFRkLltAFowoWQFNYIKygr2g1tSzaSN9RCUzPnxo5OE8rRcZWlsnU9RD4otYqcrq4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795123846; h=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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=tAygwFUjAXnlmyhTw8QJTNzxArPZrorABohzNVZLFdg=; b=OaIEhutzxplbUL6DcdCmU1Wr8GSqC2ZjYtPsx/6xfBwlGbGfwhbFd5+f4gJtNQTrl6aNhVcYg4HOE2/npEgyw1R64NBCTcIGMy88Acin9ljpM5vY3TpVctS3Wo82s4fvfhBiC7xYcQHn901JN3oBKE5PfTUAOX7c2ZCNzAPcp7g= 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=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1795123846498233.71569243059707; Thu, 19 Nov 2026 13:30:46 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1497125.1695488 (Exim 4.92) (envelope-from ) id 1xU9iB-0003fk-BQ; Thu, 19 Nov 2026 21:30:31 +0000 Received: by outflank-mailman (output) from mailman id 1497125.1695488; Thu, 19 Nov 2026 21:30:31 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9iA-0003cF-El; Thu, 19 Nov 2026 21:30:30 +0000 Received: by outflank-mailman (input) for mailman id 1497125; Thu, 19 Nov 2026 21:30:19 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9hz-0001D3-D7 for xen-devel@lists.xenproject.org; Thu, 19 Nov 2026 21:30:19 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xU9hz-00000006tI5-00kd; Thu, 19 Nov 2026 21:30:19 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xU9Mz-0000000CVBH-0xMS; Thu, 19 Nov 2026 21:08:37 +0000 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" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=tAygwFUjAXnlmyhTw8QJTNzxArPZrorABohzNVZLFdg=; b=OFiOTva0ztPL9l4fQ/8zuTixTy 4tjgsgWqybsLeQtLNIj8Aa7jCjuuqJ9W5ZR6sD8+p+1Opq9wPmCZaDI4qHu0jOQHzvjVNMKDbNbrk xv7fstmp6YHGqM96OnRC1ifQggI3e7yOw3t1XwmUC3NCo1vS6bqwSPMUbliX+wJWbnes=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie Subject: [PATCH v7 48/50] x86/mce: pass the error-injection callfunc payload by value Date: Thu, 19 Nov 2026 21:07:39 +0000 Message-ID: <20261119-asi-hvm-v7-48-6551b6d5c879@xenproject.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> References: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1795123847597158500 Content-Type: text/plain; charset="utf-8" Per-CPU stack mappings, to come as part of Address Space Isolation, give some contexts page-tables which map only the stack of the CPU they run on. A CPU in such a context cannot reach another CPU's stack, so pointers into a caller's stack cannot be dereferenced by call-function callees. do_mca() copies the hypercall argument into a stack local and then, for XEN_MC_msrinject, points another CPU at its embedded struct xen_mc_msrinject (144 bytes, the largest by-value payload in the tree) via on_selected_cpus(). The XEN_MC_mceinject calls pass no payload. Switch to on_selected_cpus_obj(), passing the structure by value. The call is synchronous (@wait =3D=3D 1) and x86_mc_msrinject() only reads the payload, so behaviour is unchanged. No functional change. Groundwork for per-CPU stack mappings, which take away a CPU's access to other CPUs' stacks: besides get_cpu_current(), cross-CPU rendezvous and call-function payloads are what reads another CPU's stack at run time. Assisted-by: Claude Code:claude-opus-4-8, Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Changes in v7: - New in this version. --- xen/arch/x86/cpu/mcheck/mce.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xen/arch/x86/cpu/mcheck/mce.c b/xen/arch/x86/cpu/mcheck/mce.c index 842a80977c..e435e13ad7 100644 --- a/xen/arch/x86/cpu/mcheck/mce.c +++ b/xen/arch/x86/cpu/mcheck/mce.c @@ -1532,8 +1532,8 @@ long do_mca(XEN_GUEST_HANDLE_PARAM(xen_mc_t) u_xen_mc) =20 add_taint(TAINT_ERROR_INJECT); =20 - on_selected_cpus(cpumask_of(target), x86_mc_msrinject, - mc_msrinject, 1); + on_selected_cpus_obj(cpumask_of(target), x86_mc_msrinject, + mc_msrinject, 1); =20 break; =20 --=20 2.56.0 From nobody Mon Nov 23 22:32:25 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=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1795123833; cv=none; d=zohomail.com; s=zohoarc; b=mBPG964Eti/yl0t+wlwUsU+vcHU5ag7QUIMFy1B0Mn6ACgNwDo6S9YWMO0YzFOavgdZx3q6iPPjFezv4hrkqKATSBRd5vsjSrZlW2exgapiRHLR9aGaJm2ICbqcDfpPGzfss+mFgE4ZY8WzyCBgGrO7zz9s48nBUk7GVdaQ14Mo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795123833; h=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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=sU58dz1V0l9Snp12bw91EngqfwnAifwGZQVIV9FmOTs=; b=PHtz8oaxyyVZFDRGaTguu2DnW6EZF3OjYjV89WZsnVo9P85dB9zzjTSfFM5o/MKWOueSAVvt6n9OgULCdCf8M/e9N/J+CvYuoZijSFkbt1QfG3ZT9Z7B5qfOhOKSG5cLWLm5DiXG6Vqb49OoPynEbRoCGARXQbWc6ZmwLJRAyjA= 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=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1795123833586677.7534277318845; Thu, 19 Nov 2026 13:30:33 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1497112.1695399 (Exim 4.92) (envelope-from ) id 1xU9i1-0001RE-IF; Thu, 19 Nov 2026 21:30:21 +0000 Received: by outflank-mailman (output) from mailman id 1497112.1695399; Thu, 19 Nov 2026 21:30:21 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9i1-0001Pw-BZ; Thu, 19 Nov 2026 21:30:21 +0000 Received: by outflank-mailman (input) for mailman id 1497112; Thu, 19 Nov 2026 21:30:19 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9hy-0001Bg-UN for xen-devel@lists.xenproject.org; Thu, 19 Nov 2026 21:30:18 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xU9hy-00000006tG5-1AX2; Thu, 19 Nov 2026 21:30:18 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xU9N0-0000000CVBH-0bLJ; Thu, 19 Nov 2026 21:08:38 +0000 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" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=sU58dz1V0l9Snp12bw91EngqfwnAifwGZQVIV9FmOTs=; b=Lq+u7PCxy9AD2pkPhG7Yu04K8L opbMVcFBd8qyN8uQ/q4PkbuYc/dg5rbnfOQHJ79U03w856ldCQ4fATdhAcxU1aTLb5lz5a3qJuVZV xcuVagheeYtZk/A7wh5YlgCJM9lgLbV7AXyfnge1Fd7y2tDI7i8/7nc/W6WvxOGrxR9g=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie Subject: [PATCH v7 49/50] x86: pass remaining stack-allocated callfunc payloads by value Date: Thu, 19 Nov 2026 21:07:40 +0000 Message-ID: <20261119-asi-hvm-v7-49-6551b6d5c879@xenproject.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> References: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1795123835236158500 Content-Type: text/plain; charset="utf-8" Per-CPU stack mappings, to come as part of Address Space Isolation, give some contexts page-tables which map only the stack of the CPU they run on. A CPU in such a context cannot reach another CPU's stack, so pointers into a caller's stack cannot be dereferenced by call-function callees. Convert the remaining call sites that point another CPU at a local variable via on_selected_cpus(), with on_selected_cpus_obj_inout() where the callee writes results back (resource_access(), l3_cache_get()) and on_selected_cpus_obj() where it only reads (do_write_psr_msrs(), __machine_restart()): * XENPF_resource_op: struct resource_access on the do_platform_op() stack; the callee writes ra.nr_done back, and ra.entries points at a (heap) array the callee also writes - both keep working, the former via the copy-back, the latter because the array stays globally reachable. * psr do_write_psr_msrs(): struct cos_write_info, which the callee only reads; the embedded val pointer references a heap array. * machine_restart(): the hop to the boot CPU passes delay_millisecs with @wait =3D=3D 0 and then halts this CPU forever. This is the one asynchronous case: the halted caller's frame stays alive for the callee (correct today), but with per-CPU mapped stacks it would no longer be reachable. Passing by value removes the cross-CPU reference altogether. * XEN_SYSCTL_psr_cmt_op: struct l3_cache_info, filled by the callee. No functional change. Groundwork for per-CPU stack mappings, which take away a CPU's access to other CPUs' stacks: besides get_cpu_current(), cross-CPU rendezvous and call-function payloads are what reads another CPU's stack at run time. Assisted-by: Claude Code:claude-opus-4-8, Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Changes in v7: - New in this version. --- xen/arch/x86/platform_hypercall.c | 2 +- xen/arch/x86/psr.c | 2 +- xen/arch/x86/shutdown.c | 4 ++-- xen/arch/x86/sysctl.c | 3 ++- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/xen/arch/x86/platform_hypercall.c b/xen/arch/x86/platform_hype= rcall.c index 0f3c791fc2..5516a232e4 100644 --- a/xen/arch/x86/platform_hypercall.c +++ b/xen/arch/x86/platform_hypercall.c @@ -876,7 +876,7 @@ ret_t do_platform_op( if ( cpu =3D=3D smp_processor_id() ) resource_access(&ra); else - on_selected_cpus(cpumask_of(cpu), resource_access, &ra, 1); + on_selected_cpus_obj_inout(cpumask_of(cpu), resource_access, &= ra); =20 /* Copy all if succeeded or up to the failed entry. */ if ( __copy_to_guest(guest_entries, ra.entries, diff --git a/xen/arch/x86/psr.c b/xen/arch/x86/psr.c index cce7020868..29b8fad96e 100644 --- a/xen/arch/x86/psr.c +++ b/xen/arch/x86/psr.c @@ -1337,7 +1337,7 @@ static int write_psr_msrs(unsigned int socket, unsign= ed int cos, =20 if ( cpu >=3D nr_cpu_ids ) return -ENOTSOCK; - on_selected_cpus(cpumask_of(cpu), do_write_psr_msrs, &data, 1); + on_selected_cpus_obj(cpumask_of(cpu), do_write_psr_msrs, &data, 1); } =20 return 0; diff --git a/xen/arch/x86/shutdown.c b/xen/arch/x86/shutdown.c index 902076cf67..820eaa7e30 100644 --- a/xen/arch/x86/shutdown.c +++ b/xen/arch/x86/shutdown.c @@ -550,8 +550,8 @@ void machine_restart(unsigned int delay_millisecs) if ( get_apic_id() !=3D boot_cpu_physical_apicid ) { /* Send IPI to the boot CPU (logical cpu 0). */ - on_selected_cpus(cpumask_of(0), __machine_restart, - &delay_millisecs, 0); + on_selected_cpus_obj(cpumask_of(0), __machine_restart, + &delay_millisecs, 0); for ( ; ; ) halt(); } diff --git a/xen/arch/x86/sysctl.c b/xen/arch/x86/sysctl.c index 6bd4e191a7..b1a15dce97 100644 --- a/xen/arch/x86/sysctl.c +++ b/xen/arch/x86/sysctl.c @@ -195,7 +195,8 @@ long arch_do_sysctl( if ( cpu =3D=3D smp_processor_id() ) l3_cache_get(&info); else - on_selected_cpus(cpumask_of(cpu), l3_cache_get, &info, 1); + on_selected_cpus_obj_inout(cpumask_of(cpu), l3_cache_get, + &info); =20 ret =3D info.ret; sysctl->u.psr_cmt_op.u.data =3D (ret ? 0 : info.size); --=20 2.56.0 From nobody Mon Nov 23 22:32:25 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=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1795123845; cv=none; d=zohomail.com; s=zohoarc; b=J186Yv0IuJaprLKO77Xcu01ZvHsluFMQE0OMhfmvINH2yqKMvsQ7H06qiIwt3OW6zyTD2yoGzWuh79jh/i5vl3EXvNO+3F073ObEh9Q3KvffS7WUb1GUGSKC1hps2qRR8CTppfUMv91h1fZq4KVoHLdajd753cpYui9eqAdvBSI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795123845; h=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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=UCjDQx67loKQbb+wlH6w8/IgKDJNeVDNbRCBwgxwHW8=; b=A1OYKOHF6zbvARExxJh2dKxAbIJablsXS1iWtaucofnlJz89VCvavqA1mfZaWMr4lZjwffm1F1JttpTvqCS6yM96UHXwLuRm2dxGbq/X7jHD5HGSTxnogse/2UxbSuKESsTlq4wtLZKniGkP7NFS3QroERM5wSrGvts7BtwMwhQ= 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=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1795123845575707.1910838273967; Thu, 19 Nov 2026 13:30:45 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1497122.1695468 (Exim 4.92) (envelope-from ) id 1xU9i8-000377-3A; Thu, 19 Nov 2026 21:30:28 +0000 Received: by outflank-mailman (output) from mailman id 1497122.1695468; Thu, 19 Nov 2026 21:30:27 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9i7-00033f-Fa; Thu, 19 Nov 2026 21:30:27 +0000 Received: by outflank-mailman (input) for mailman id 1497122; Thu, 19 Nov 2026 21:30:19 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xU9hz-0001CV-7M for xen-devel@lists.xenproject.org; Thu, 19 Nov 2026 21:30:19 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xU9hy-00000006tGv-37yB; Thu, 19 Nov 2026 21:30:19 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xU9N1-0000000CVBH-1aGR; Thu, 19 Nov 2026 21:08:39 +0000 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" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=UCjDQx67loKQbb+wlH6w8/IgKDJNeVDNbRCBwgxwHW8=; b=KJLxeXmdfb54la4raFcpwfAPjJ IAeDDwKzxXrnmQSuoY4t9wkSGlVUMHykM2E+dqv8LrMNpk/yZ/X6ZYcfk9vVlbDN34UiHWzZRUoat Xs6D7UuyIQ7NpTwzYppGDWPx6PmOer7NzplzKlsvNf5SVY7poIjjHeZhfglqnhLrR/hc=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Andrew Cooper , Anthony PERARD , Michal Orzel , Jan Beulich , Julien Grall , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Stefano Stabellini Subject: [PATCH v7 50/50] xen/smp: assert that by-pointer callfunc payloads are not on the stack Date: Thu, 19 Nov 2026 21:07:41 +0000 Message-ID: <20261119-asi-hvm-v7-50-6551b6d5c879@xenproject.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> References: <20261119-asi-hvm-v7-0-6551b6d5c879@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1795123847643158500 Content-Type: text/plain; charset="utf-8" All in-tree callers of on_selected_cpus() / smp_call_function() / stop_machine_run() that used to pass a pointer to a stack local have been converted: rendezvous-style payloads moved to statics, message- and result-style payloads are passed by value. Per-CPU stack mappings, to come as part of Address Space Isolation, give some contexts page-tables which map only the stack of the CPU they run on; stack payloads are not reachable from a CPU in such a context, so the conversion must stay complete. Enforce the resulting contract with a debug-build assert: a payload passed by pointer must not point into the stack the initiator is running on. The check is deliberately unconditional (not tied to any Address Space Isolation option): with per-CPU mapped stacks a violating caller faults on the remote CPU, typically in a path that boot testing never exercises (MTRR/PSR/cpufreq/MCE inject). Asserting on every debug build turns a future reintroduction of the pattern into an immediate, attributable failure at the call site, regardless of configuration. For the same uniformity the check applies even to a self-only target mask (where a stack payload would happen to work): the API contract is one rule, not a per-mask case analysis. The helper derives the current stack block from the address of a local variable, relying on stacks being STACK_SIZE-aligned blocks, as they are on x86, Arm and PPC, whose get_cpu_info() depends on it. RISC-V's vCPU stacks come from vzalloc() and are only page-aligned, but nothing runs on them yet. A payload referencing a different mapping of the same stack pages (such as a directmap alias) is not caught - inherent to any VA-comparison scheme; a payload taken from a local variable's address names the stack being run on, not an alias of it. Groundwork for per-CPU stack mappings, which take away a CPU's access to other CPUs' stacks: besides get_cpu_current(), cross-CPU rendezvous and call-function payloads are what reads another CPU's stack at run time. Assisted-by: Claude Code:claude-opus-4-8, Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Changes in v7: - New in this version. --- xen/common/smp.c | 6 ++++++ xen/common/stop_machine.c | 6 ++++++ xen/include/xen/smp.h | 16 ++++++++++++++++ 3 files changed, 28 insertions(+) diff --git a/xen/common/smp.c b/xen/common/smp.c index eb419a3aeb..ef068c014e 100644 --- a/xen/common/smp.c +++ b/xen/common/smp.c @@ -67,6 +67,12 @@ static void _on_selected_cpus( ASSERT(cpumask_subset(selected, &cpu_online_map)); BUG_ON(size > CALLFUNC_DATA_MAX); BUG_ON(size && !info); + /* + * By-pointer payloads must be reachable from the target CPUs, which a + * CPU's stack need not be: pass such payloads by value (@size !=3D 0)= , or + * move the datum off the stack. + */ + ASSERT(size || !info || !ptr_on_current_stack(info)); =20 spin_lock(&call_lock); =20 diff --git a/xen/common/stop_machine.c b/xen/common/stop_machine.c index 398cfd507c..e7cdc4ce5a 100644 --- a/xen/common/stop_machine.c +++ b/xen/common/stop_machine.c @@ -81,6 +81,12 @@ int stop_machine_run(int (*fn)(void *data), void *data, = unsigned int cpu) =20 BUG_ON(!local_irq_is_enabled()); BUG_ON(!is_idle_vcpu(current)); + /* + * @data is dereferenced from other CPUs' stop-machine contexts, so it + * must not point into a CPU's stack, which other CPUs need not be able + * to reach. + */ + ASSERT(!data || !ptr_on_current_stack(data)); =20 /* cpu_online_map must not change. */ if ( !get_cpu_maps() ) diff --git a/xen/include/xen/smp.h b/xen/include/xen/smp.h index 73bf7075d6..4b9afa18d9 100644 --- a/xen/include/xen/smp.h +++ b/xen/include/xen/smp.h @@ -126,6 +126,22 @@ static inline void on_each_cpu( on_selected_cpus(&cpu_online_map, func, info, wait); } =20 +/* + * Does @p point into the stack we are currently running on? Stacks + * are STACK_SIZE-aligned blocks (on x86, Arm and PPC; RISC-V's vCPU + * stacks, which nothing runs on yet, are only page-aligned), so the + * containing block can be derived from any live stack address, such + * as that of a local variable. Note that other mappings of the same + * stack (e.g. a directmap alias of the underlying pages) compare as + * not-on-stack by design. + */ +static inline bool ptr_on_current_stack(const void *p) +{ + unsigned long here =3D (unsigned long)&here; + + return !(((unsigned long)p ^ here) & ~(STACK_SIZE - 1)); +} + /* * Call a function on the current CPU */ --=20 2.56.0