From nobody Fri Oct 31 11:33:02 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=quarantine dis=quarantine) header.from=proton.me Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1748891888952159.36073288470027; Mon, 2 Jun 2025 12:18:08 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1003773.1383385 (Exim 4.92) (envelope-from ) id 1uMAfE-0008U3-An; Mon, 02 Jun 2025 19:17:40 +0000 Received: by outflank-mailman (output) from mailman id 1003773.1383385; Mon, 02 Jun 2025 19:17: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 1uMAfE-0008Tw-6c; Mon, 02 Jun 2025 19:17:40 +0000 Received: by outflank-mailman (input) for mailman id 1003773; Mon, 02 Jun 2025 19:17:39 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uMAfD-0008Fp-AN for xen-devel@lists.xenproject.org; Mon, 02 Jun 2025 19:17:39 +0000 Received: from mail-10628.protonmail.ch (mail-10628.protonmail.ch [79.135.106.28]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 3eb2f106-3fe6-11f0-b894-0df219b8e170; Mon, 02 Jun 2025 21:17:37 +0200 (CEST) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 3eb2f106-3fe6-11f0-b894-0df219b8e170 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=jgi3by2g5zbwtl2dhg6cnbz7oi.protonmail; t=1748891855; x=1749151055; bh=OFArnTWm3efKFTWaDQu3LAnnzQ21K9gfY4s1Z9E7fps=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector:List-Unsubscribe:List-Unsubscribe-Post; b=dTwG7dyQ8lFni+GPW4RKWIAEKIPqm1iyRPjJ7c7224iJOzxQwkSfXywH67ZkDCTmk 5cEgaHvS1/Xk9yGCpv3vvbu/cGHknXiNcauJvJ9GN2zB0IwFD8R8LjW7nPNgjZD72X JN1qJpaDuoZhbXVnXBJtfFLMjLe9IrnqjnhRWu22rbwt1bg3z501YjvllNRCKS2RK9 bQRw2w6u6iXxIKln//eTry6O1xNyCsJim7F14ljmcP1OF1xtL6u31WIjR2GzacPvWc 0OxVt0woO74feQFjHM0gjIcE28L4bl6ldsLyJwf9LPslq/3Aid+BbfAvM4AA/mrWY5 FnRb7iwW+VqZg== Date: Mon, 02 Jun 2025 19:17:30 +0000 To: xen-devel@lists.xenproject.org From: dmkhn@proton.me Cc: andrew.cooper3@citrix.com, anthony.perard@vates.tech, jbeulich@suse.com, julien@xen.org, michal.orzel@amd.com, roger.pau@citrix.com, sstabellini@kernel.org, teddy.astie@vates.tech, dmukhin@ford.com Subject: [PATCH v5 1/2] xen/domain: introduce common hardware emulation flags Message-ID: <20250602191717.148361-2-dmukhin@ford.com> In-Reply-To: <20250602191717.148361-1-dmukhin@ford.com> References: <20250602191717.148361-1-dmukhin@ford.com> Feedback-ID: 123220910:user:proton X-Pm-Message-ID: 44152c72659e4f4e74e9162f3f36e24325a91f72 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1748891889546116600 Content-Type: text/plain; charset="utf-8" From: Denis Mukhin Add common emulation_flags for configuring domain emulation features. Print d->emulation_flags from 'q' keyhandler for better traceability while debugging. Signed-off-by: Denis Mukhin Reviewed-by: Stefano Stabellini --- Changes since v4: - kept Stefano's R-b --- xen/arch/x86/domain.c | 2 +- xen/arch/x86/domctl.c | 2 +- xen/arch/x86/include/asm/domain.h | 25 +++++++++++-------------- xen/common/keyhandler.c | 1 + xen/include/xen/sched.h | 2 ++ 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/xen/arch/x86/domain.c b/xen/arch/x86/domain.c index 7536b6c871..0363ccb384 100644 --- a/xen/arch/x86/domain.c +++ b/xen/arch/x86/domain.c @@ -831,7 +831,7 @@ int arch_domain_create(struct domain *d, emflags); return -EOPNOTSUPP; } - d->arch.emulation_flags =3D emflags; + d->emulation_flags =3D emflags; =20 #ifdef CONFIG_PV32 HYPERVISOR_COMPAT_VIRT_START(d) =3D diff --git a/xen/arch/x86/domctl.c b/xen/arch/x86/domctl.c index 3044f706de..37d848f683 100644 --- a/xen/arch/x86/domctl.c +++ b/xen/arch/x86/domctl.c @@ -144,7 +144,7 @@ void arch_get_domain_info(const struct domain *d, if ( paging_mode_hap(d) ) info->flags |=3D XEN_DOMINF_hap; =20 - info->arch_config.emulation_flags =3D d->arch.emulation_flags; + info->arch_config.emulation_flags =3D d->emulation_flags; info->gpaddr_bits =3D hap_paddr_bits; } =20 diff --git a/xen/arch/x86/include/asm/domain.h b/xen/arch/x86/include/asm/d= omain.h index 8c0dea12a5..eafd5cfc90 100644 --- a/xen/arch/x86/include/asm/domain.h +++ b/xen/arch/x86/include/asm/domain.h @@ -455,9 +455,6 @@ struct arch_domain =20 /* Don't unconditionally inject #GP for unhandled MSRs. */ bool msr_relaxed; - - /* Emulated devices enabled bitmap. */ - uint32_t emulation_flags; } __cacheline_aligned; =20 #ifdef CONFIG_HVM @@ -494,17 +491,17 @@ struct arch_domain X86_EMU_PIT | X86_EMU_USE_PIRQ | \ X86_EMU_VPCI) =20 -#define has_vlapic(d) (!!((d)->arch.emulation_flags & X86_EMU_LAPIC)) -#define has_vhpet(d) (!!((d)->arch.emulation_flags & X86_EMU_HPET)) -#define has_vpm(d) (!!((d)->arch.emulation_flags & X86_EMU_PM)) -#define has_vrtc(d) (!!((d)->arch.emulation_flags & X86_EMU_RTC)) -#define has_vioapic(d) (!!((d)->arch.emulation_flags & X86_EMU_IOAPIC)) -#define has_vpic(d) (!!((d)->arch.emulation_flags & X86_EMU_PIC)) -#define has_vvga(d) (!!((d)->arch.emulation_flags & X86_EMU_VGA)) -#define has_viommu(d) (!!((d)->arch.emulation_flags & X86_EMU_IOMMU)) -#define has_vpit(d) (!!((d)->arch.emulation_flags & X86_EMU_PIT)) -#define has_pirq(d) (!!((d)->arch.emulation_flags & X86_EMU_USE_PIR= Q)) -#define has_vpci(d) (!!((d)->arch.emulation_flags & X86_EMU_VPCI)) +#define has_vlapic(d) (!!((d)->emulation_flags & X86_EMU_LAPIC)) +#define has_vhpet(d) (!!((d)->emulation_flags & X86_EMU_HPET)) +#define has_vpm(d) (!!((d)->emulation_flags & X86_EMU_PM)) +#define has_vrtc(d) (!!((d)->emulation_flags & X86_EMU_RTC)) +#define has_vioapic(d) (!!((d)->emulation_flags & X86_EMU_IOAPIC)) +#define has_vpic(d) (!!((d)->emulation_flags & X86_EMU_PIC)) +#define has_vvga(d) (!!((d)->emulation_flags & X86_EMU_VGA)) +#define has_viommu(d) (!!((d)->emulation_flags & X86_EMU_IOMMU)) +#define has_vpit(d) (!!((d)->emulation_flags & X86_EMU_PIT)) +#define has_pirq(d) (!!((d)->emulation_flags & X86_EMU_USE_PIRQ)) +#define has_vpci(d) (!!((d)->emulation_flags & X86_EMU_VPCI)) =20 #define gdt_ldt_pt_idx(v) \ ((v)->vcpu_id >> (PAGETABLE_ORDER - GDT_LDT_VCPU_SHIFT)) diff --git a/xen/common/keyhandler.c b/xen/common/keyhandler.c index 0bb842ec00..cd731452ba 100644 --- a/xen/common/keyhandler.c +++ b/xen/common/keyhandler.c @@ -306,6 +306,7 @@ static void cf_check dump_domains(unsigned char key) if ( test_bit(i, &d->watchdog_inuse_map) ) printk(" watchdog %d expires in %d seconds\n", i, (u32)((d->watchdog_timer[i].expires - NOW()) >> = 30)); + printk(" emulation_flags %#x\n", d->emulation_flags); =20 arch_dump_domain_info(d); =20 diff --git a/xen/include/xen/sched.h b/xen/include/xen/sched.h index b17aada5f5..1393923986 100644 --- a/xen/include/xen/sched.h +++ b/xen/include/xen/sched.h @@ -651,6 +651,8 @@ struct domain unsigned int num_llc_colors; const unsigned int *llc_colors; #endif + + uint32_t emulation_flags; } __aligned(PAGE_SIZE); =20 static inline struct page_list_head *page_to_list( --=20 2.34.1 From nobody Fri Oct 31 11:33:02 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=quarantine dis=quarantine) header.from=proton.me Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1748891887258632.6285268720663; Mon, 2 Jun 2025 12:18:07 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1003774.1383394 (Exim 4.92) (envelope-from ) id 1uMAfL-0000Kv-G0; Mon, 02 Jun 2025 19:17:47 +0000 Received: by outflank-mailman (output) from mailman id 1003774.1383394; Mon, 02 Jun 2025 19:17:47 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uMAfL-0000Ko-Cl; Mon, 02 Jun 2025 19:17:47 +0000 Received: by outflank-mailman (input) for mailman id 1003774; Mon, 02 Jun 2025 19:17:46 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uMAfK-0000JS-I9 for xen-devel@lists.xenproject.org; Mon, 02 Jun 2025 19:17:46 +0000 Received: from mail-24418.protonmail.ch (mail-24418.protonmail.ch [109.224.244.18]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 43147dee-3fe6-11f0-a300-13f23c93f187; Mon, 02 Jun 2025 21:17:45 +0200 (CEST) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 43147dee-3fe6-11f0-a300-13f23c93f187 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1748891864; x=1749151064; bh=uzDJxgN96Kqct/U5nq9L2/hSZ+SqbrgJbBHgdSzDlw8=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector:List-Unsubscribe:List-Unsubscribe-Post; b=N0t0Aeyiiu2xj4exQknK6b4bzlblBLK1kUHpxCissNgvb507Z4ngR7isvwZyedmZV jSLno16ODafAY5HmtbWfl/F4ZdZ3g9ryH6gAxzCGEPLvK15EpdpML3iVV6yJurcAJf 5qFi/JexUzkjoODs78uVrSOt7GP6NctREv7MkuQCpO9Xy60ROthvRmSOKWhMUIPVzr cLSv/DgjGFw8kt2ApZIguSy+aPHMDHNn8qZuxk6waXSA10Mb8Ih2/CB6S3gfqbGIyh mKOWvoKxELGmVE08DQ1uikGvpNMoQUuRwls2uccnLxDdGRNH8R0Pry+RobYQh/iH46 WNXst4DSGsf8g== Date: Mon, 02 Jun 2025 19:17:38 +0000 To: xen-devel@lists.xenproject.org From: dmkhn@proton.me Cc: andrew.cooper3@citrix.com, anthony.perard@vates.tech, jbeulich@suse.com, julien@xen.org, michal.orzel@amd.com, roger.pau@citrix.com, sstabellini@kernel.org, teddy.astie@vates.tech, dmukhin@ford.com Subject: [PATCH v5 2/2] xen/domain: rewrite emulation_flags_ok() Message-ID: <20250602191717.148361-3-dmukhin@ford.com> In-Reply-To: <20250602191717.148361-1-dmukhin@ford.com> References: <20250602191717.148361-1-dmukhin@ford.com> Feedback-ID: 123220910:user:proton X-Pm-Message-ID: 9ead51012b322b3702c37d34c3ae7cb06b5d2c06 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1748891889777116600 Content-Type: text/plain; charset="utf-8" From: Denis Mukhin Rewrite emulation_flags_ok() to simplify future modifications. No functional change intended. Signed-off-by: Denis Mukhin Reviewed-by: Stefano Stabellini Reviewed-by: Teddy Astie --- Changes since v4: - updated commentaries - added Teddy's R-b, kept Stefano's R-b --- xen/arch/x86/domain.c | 91 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 73 insertions(+), 18 deletions(-) diff --git a/xen/arch/x86/domain.c b/xen/arch/x86/domain.c index 0363ccb384..4f6670ce37 100644 --- a/xen/arch/x86/domain.c +++ b/xen/arch/x86/domain.c @@ -743,32 +743,87 @@ int arch_sanitise_domain_config(struct xen_domctl_cre= atedomain *config) return 0; } =20 +/* + * Verify that the domain's emulation flags resolve to a supported configu= ration. + * + * This ensures we only allow a known, safe subset of emulation combinatio= ns + * (for both functionality and security). Arbitrary mixes are likely to ca= use + * errors (e.g., null pointer dereferences). + * + * NB: use the internal X86_EMU_XXX symbols, not the public XEN_X86_EMU_XXX + * symbols. + */ static bool emulation_flags_ok(const struct domain *d, uint32_t emflags) { + enum { + CAP_PV =3D BIT(0, U), + CAP_HVM =3D BIT(1, U), + CAP_HWDOM =3D BIT(2, U), + CAP_DOMU =3D BIT(3, U), + }; + static const struct { + unsigned int caps; + uint32_t min; + uint32_t opt; + } configs[] =3D { +#ifdef CONFIG_PV + /* PV */ + { + .caps =3D CAP_PV | CAP_DOMU, + .min =3D 0, + .opt =3D 0, + }, + + /* PV dom0 */ + { + .caps =3D CAP_PV | CAP_HWDOM, + .min =3D X86_EMU_PIT, + .opt =3D 0, + }, +#endif /* #ifdef CONFIG_PV */ + +#ifdef CONFIG_HVM + /* PVH dom0 */ + { + .caps =3D CAP_HVM | CAP_HWDOM, + .min =3D X86_EMU_LAPIC | X86_EMU_IOAPIC | X86_EMU_VPCI, + .opt =3D 0, + }, + + /* HVM domU */ + { + .caps =3D CAP_HVM | CAP_DOMU, + .min =3D X86_EMU_ALL & ~(X86_EMU_VPCI | X86_EMU_USE_PIRQ), + /* HVM PIRQ feature is user-selectable. */ + .opt =3D X86_EMU_USE_PIRQ, + }, + + /* PVH domU */ + { + .caps =3D CAP_HVM | CAP_DOMU, + .min =3D X86_EMU_LAPIC, + .opt =3D 0, + }, +#endif /* #ifdef CONFIG_HVM */ + }; + unsigned int i, caps =3D is_hardware_domain(d) ? CAP_HWDOM : CAP_DOMU; + + if ( is_pv_domain(d) ) + caps |=3D CAP_PV; + else if ( is_hvm_domain(d) ) + caps |=3D CAP_HVM; + #ifdef CONFIG_HVM /* This doesn't catch !CONFIG_HVM case but it is better than nothing */ BUILD_BUG_ON(X86_EMU_ALL !=3D XEN_X86_EMU_ALL); #endif =20 - if ( is_hvm_domain(d) ) - { - if ( is_hardware_domain(d) && - emflags !=3D (X86_EMU_VPCI | X86_EMU_LAPIC | X86_EMU_IOAPIC) ) - return false; - if ( !is_hardware_domain(d) && - /* HVM PIRQ feature is user-selectable. */ - (emflags & ~X86_EMU_USE_PIRQ) !=3D - (X86_EMU_ALL & ~(X86_EMU_VPCI | X86_EMU_USE_PIRQ)) && - emflags !=3D X86_EMU_LAPIC ) - return false; - } - else if ( emflags !=3D 0 && emflags !=3D X86_EMU_PIT ) - { - /* PV or classic PVH. */ - return false; - } + for ( i =3D 0; i < ARRAY_SIZE(configs); i++ ) + if ( caps =3D=3D configs[i].caps && + (emflags & ~configs[i].opt) =3D=3D configs[i].min ) + return true; =20 - return true; + return false; } =20 void __init arch_init_idle_domain(struct domain *d) --=20 2.34.1