xen/domain: updates to hardware emulation flags
v3 cover xen/domain: updates to hardware emulation flags
Patch 1 introduces emulation_flags in common domain struct for enabling domain emulation features on non-x86 platforms.
Patch 2 rewrites emulation_flags_ok() on x86 with a goal of improving readability and maintainability of the code.
Originally, the code was part of [1], part of NS16550 emulator v3 series.
[1] https://lore.kernel.org/xen-devel/[email protected]/
[2] Link to v2: https://lore.kernel.org/xen-devel/[email protected]/
[3] Link to CI: https://gitlab.com/xen-project/people/dmukhin/xen/-/pipelines/1841708353
Denis Mukhin (2):
xen/domain: introduce common hardware emulation flags xen/domain: rewrite emulation_flags_ok()
xen/arch/x86/domain.c | 94 ++++++++++++++++++++++++------- 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, 90 insertions(+), 34 deletions(-)
Signature · 1 line
2.34.1
PATCH v3 1/2 xen/domain: introduce common hardware emulation flags
From: Denis Mukhin <[email protected]>
From: Denis Mukhin <[email protected]>
Add common emulation_flags for configuring domain emulation features.
Print d->emulation_flags from 'q' keyhandler for better traceability while debugging.
---
Changes since v2:
- move emulation_flags to common domain struct
--- 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(-)
xen/arch/x86/domain.c +1 -1
@@ -831,7 +831,7 @@ int arch_domain_create(struct domain *d, emflags);return -EOPNOTSUPP ; } - d->arch.emulation_flags = emflags; + d->emulation_flags = emflags;#ifdef CONFIG_PV32 HYPERVISOR_COMPAT_VIRT_START(d) =
xen/arch/x86/domctl.c +1 -1
@@ -144,7 +144,7 @@ void arch_get_domain_info(const struct domain *d,if (paging_mode_hap (d) ) info->flags |= XEN_DOMINF_hap; - info->arch_config.emulation_flags = d->arch.emulation_flags; + info->arch_config.emulation_flags = d->emulation_flags; info->gpaddr_bits = hap_paddr_bits; }
xen/arch/x86/include/asm/domain.h +11 -14
@@ -455,9 +455,6 @@ struct arch_domain/* Don't unconditionally inject #GP for unhandled MSRs. */ bool msr_relaxed; - -/* Emulated devices enabled bitmap. */ -uint32_t emulation_flags; } __cacheline_aligned;#ifdef CONFIG_HVM
@@ -494,17 +491,17 @@ struct arch_domain X86_EMU_PIT | X86_EMU_USE_PIRQ | \ X86_EMU_VPCI) -#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_PIRQ)) -#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))#define gdt_ldt_pt_idx (v) \ ((v)->vcpu_id >> (PAGETABLE_ORDER - GDT_LDT_VCPU_SHIFT))
xen/common/keyhandler.c +1 -0
@@ -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);arch_dump_domain_info (d);
xen/include/xen/sched.h +2 -0
@@ -651,6 +651,8 @@ struct domainunsigned int num_llc_colors;const unsigned int *llc_colors;#endif + +uint32_t emulation_flags; }__aligned(PAGE_SIZE) ; static inline struct page_list_head *page_to_list(
Signature · 1 line
2.34.1
Re: [PATCH v3 1/2] xen/domain: introduce common hardware emulation flags
Quoted 105 lines · On Wed, 28 May 2025, [email protected] wrote:
From: Denis Mukhin <[email protected]>
Add common
emulation_flagsfor configuring domain emulation features.d->emulation_flagsfrom 'q' keyhandler for better traceability while debugging.Signed-off-by Denis Mukhin <[email protected]>---
Changes since v2:
- moveemulation_flagsto common domain struct--- 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(-)xen/arch/x86/domain.c +1
-1@@ -831,7 +831,7 @@ int arch_domain_create(struct domain *d, emflags);return -EOPNOTSUPP ; } - d->arch.emulation_flags = emflags; + d->emulation_flags = emflags;#ifdef CONFIG_PV32 HYPERVISOR_COMPAT_VIRT_START(d) =xen/arch/x86/domctl.c +1
-1@@ -144,7 +144,7 @@ void arch_get_domain_info(const struct domain *d,if (paging_mode_hap (d) ) info->flags |= XEN_DOMINF_hap; - info->arch_config.emulation_flags = d->arch.emulation_flags; + info->arch_config.emulation_flags = d->emulation_flags; info->gpaddr_bits = hap_paddr_bits; }xen/arch/x86/include/asm/domain.h +11
-14@@ -455,9 +455,6 @@ struct arch_domain/* Don't unconditionally inject #GP for unhandled MSRs. */ bool msr_relaxed; - -/* Emulated devices enabled bitmap. */ -uint32_t emulation_flags; } __cacheline_aligned;#ifdef CONFIG_HVM @@ -494,17 +491,17 @@ struct arch_domain X86_EMU_PIT | X86_EMU_USE_PIRQ | \ X86_EMU_VPCI) -#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_PIRQ)) -#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))#define gdt_ldt_pt_idx (v) \ ((v)->vcpu_id >> (PAGETABLE_ORDER - GDT_LDT_VCPU_SHIFT))xen/common/keyhandler.c +1
-0@@ -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);arch_dump_domain_info (d);xen/include/xen/sched.h +0
-0
xen/include/xen/sched.h struct domain · line 655
Quoted 5 lines
xen/include/xen/sched.h +2
-0@@ -651,6 +651,8 @@ struct domainunsigned int num_llc_colors;const unsigned int *llc_colors;#endif + +uint32_t emulation_flags;
I think it is a good idea to move emulation_flags to common and I think we can make use of them on ARM too because we have a choice of emulators on ARM as well (pl011, gicv2, gicv3, etc.). I think it makes sense to move at least kinfo->arch.vpl011 to be an emulation_flags instead.
I was going to ask to add an #ifdef CONFIG_X86 around the emulation_flags field as it is currently still unused on other architectures, but then it would break the compilation of keyhandler.c.
So maybe it is OK this way.
Quoted 7 lines
xen/include/xen/sched.h +0
-0}__aligned(PAGE_SIZE) ; static inline struct page_list_head *page_to_list(Signature · 1 line
2.34.1
PATCH v3 2/2 xen/domain: rewrite emulation_flags_ok()
In reply to [email protected]From: Denis Mukhin <[email protected]>
From: Denis Mukhin <[email protected]>
Rewrite emulation_flags_ok() to simplify future modifications.
No functional change intended.
---
Changes since v2:
- addressed review feedback
- added some explanatory comments for emulation_flags_ok()
--- xen/arch/x86/domain.c | 92 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 74 insertions(+), 18 deletions(-)
xen/arch/x86/domain.c +74 -18
@@ -743,32 +743,88 @@ int arch_sanitise_domain_config(struct xen_domctl_createdomain *config)return 0 ; } +/* +* Verify that the domain's emulation flags resolve to a supported configuration. +* +* This ensures we only allow a known, safe subset of emulation combinations +* (for both functionality and security). Arbitrary mixes are likely to cause +* 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 =BIT (0 ,U ), +CAP_HVM =BIT (1 ,U ), +CAP_HWDOM =BIT (2 ,U ), +CAP_DOMU =BIT (3 ,U ), + }; +static const struct { +unsigned int caps; +uint32_t min; +uint32_t opt; + } configs[] = { +#ifdef CONFIG_PV +/* PV */ + { + .caps =CAP_PV |CAP_DOMU , + .min =0 , + .opt =0 , + }, + +/* PV (likely dom0) */ + { + .caps =CAP_PV |CAP_HWDOM , + .min =X86_EMU_PIT , + .opt =0 , + }, +#endif /* #ifdef CONFIG_PV */ + +#ifdef CONFIG_HVM +/* PVH dom0/domU or HVM domU */ + { + .caps =CAP_HVM |CAP_HWDOM , + .min =X86_EMU_LAPIC |X86_EMU_IOAPIC |X86_EMU_VPCI , + .opt =0 , + }, + + +/* HVM domU */ + { + .caps =CAP_HVM |CAP_DOMU , + .min =X86_EMU_ALL & ~(X86_EMU_VPCI |X86_EMU_USE_PIRQ ), +/* HVM PIRQ feature is user-selectable. */ + .opt =X86_EMU_USE_PIRQ , + }, + +/* PVH */ + { + .caps =CAP_HVM |CAP_DOMU , + .min =X86_EMU_LAPIC , + .opt =0 , + }, +#endif /* #ifdef CONFIG_HVM */ + }; +unsigned int i, caps =is_hardware_domain (d) ?CAP_HWDOM :CAP_DOMU ; + +if (is_pv_domain (d) ) + caps |=CAP_PV ; +else if (is_hvm_domain (d) ) + caps |=CAP_HVM ; +#ifdef CONFIG_HVM /* This doesn't catch !CONFIG_HVM case but it is better than nothing */ BUILD_BUG_ON (X86_EMU_ALL !=XEN_X86_EMU_ALL );#endif -if (is_hvm_domain (d) ) - { -if (is_hardware_domain (d) && - emflags != (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 ) != - (X86_EMU_ALL & ~(X86_EMU_VPCI |X86_EMU_USE_PIRQ )) && - emflags !=X86_EMU_LAPIC ) -return false; - } -else if ( emflags !=0 && emflags !=X86_EMU_PIT ) - { -/* PV or classic PVH. */ -return false; - } +for ( i =0 ; i <ARRAY_SIZE (configs); i++ ) +if ( caps == configs[i].caps && + (emflags & ~configs[i].opt) == configs[i].min ) +return true; -return true; +return false; }void __init arch_init_idle_domain (struct domain *d)
Signature · 1 line
2.34.1
Re: [PATCH v3 2/2] xen/domain: rewrite emulation_flags_ok()
Quoted 63 lines · On Wed, 28 May 2025, [email protected] wrote:
From: Denis Mukhin <[email protected]>
From: Denis Mukhin <[email protected]>
Rewrite
emulation_flags_ok()to simplify future modifications.No functional change intended.
Signed-off-by Denis Mukhin <[email protected]>---
Changes since v2:
- addressed review feedback
- added some explanatory comments foremulation_flags_ok()--- xen/arch/x86/domain.c | 92 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 74 insertions(+), 18 deletions(-)xen/arch/x86/domain.c +36
-0@@ -743,32 +743,88 @@ int arch_sanitise_domain_config(struct xen_domctl_createdomain *config)return 0 ; } +/* +* Verify that the domain's emulation flags resolve to a supported configuration. +* +* This ensures we only allow a known, safe subset of emulation combinations +* (for both functionality and security). Arbitrary mixes are likely to cause +* 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 =BIT (0 ,U ), +CAP_HVM =BIT (1 ,U ), +CAP_HWDOM =BIT (2 ,U ), +CAP_DOMU =BIT (3 ,U ), + }; +static const struct { +unsigned int caps; +uint32_t min; +uint32_t opt; + } configs[] = { +#ifdef CONFIG_PV +/* PV */ + { + .caps =CAP_PV |CAP_DOMU , + .min =0 , + .opt =0 , + }, + +/* PV (likely dom0) */ + { + .caps =CAP_PV |CAP_HWDOM , + .min =X86_EMU_PIT , + .opt =0 , + }, +#endif /* #ifdef CONFIG_PV */
xen/arch/x86/domain.c int arch_sanitise_domain_config(struct xen_domctl_createdomain *config) · line 793
Quoted 10 lines
xen/arch/x86/domain.c +10
-0+ +#ifdef CONFIG_HVM + /* PVH dom0/domU or HVM domU */ + { + .caps = CAP_HVM | CAP_HWDOM, + .min = X86_EMU_LAPIC | X86_EMU_IOAPIC | X86_EMU_VPCI, + .opt = 0, + }, + +
NIT: double \n
I checked carefully and the new semantic matches the old one with the extra clarification that X86_EMU_PIT is only for hwdom.
Quoted 59 lines
xen/arch/x86/domain.c +28
-18+/* HVM domU */ + { + .caps = CAP_HVM | CAP_DOMU, + .min = X86_EMU_ALL & ~(X86_EMU_VPCI | X86_EMU_USE_PIRQ), + /* HVM PIRQ feature is user-selectable. */ + .opt = X86_EMU_USE_PIRQ, + }, + +/* PVH */ + { + .caps = CAP_HVM | CAP_DOMU, + .min = X86_EMU_LAPIC, + .opt = 0, + }, +#endif /* #ifdef CONFIG_HVM */ + }; +unsigned int i, caps =is_hardware_domain (d) ?CAP_HWDOM :CAP_DOMU ; + +if (is_pv_domain (d) ) + caps |=CAP_PV ; +else if (is_hvm_domain (d) ) + caps |=CAP_HVM ; +#ifdef CONFIG_HVM /* This doesn't catch !CONFIG_HVM case but it is better than nothing */ BUILD_BUG_ON (X86_EMU_ALL !=XEN_X86_EMU_ALL );#endif -if (is_hvm_domain (d) ) - { -if (is_hardware_domain (d) && - emflags != (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 ) != - (X86_EMU_ALL & ~(X86_EMU_VPCI |X86_EMU_USE_PIRQ )) && - emflags !=X86_EMU_LAPIC ) -return false; - } -else if ( emflags !=0 && emflags !=X86_EMU_PIT ) - { -/* PV or classic PVH. */ -return false; - } +for ( i =0 ; i <ARRAY_SIZE (configs); i++ ) +if ( caps == configs[i].caps && + (emflags & ~configs[i].opt) == configs[i].min ) +return true; -return true; +return false; }void __init arch_init_idle_domain (struct domain *d)Signature · 1 line
2.34.1