Skip to contentSkip to conversation

xen/arm: report proper GIC version via XEN_DOMCTL_getdomaininfo

PATCH Superseded Julian Vetter 2 participants 10 messages Aug 20 – Aug 27 2 messages not in the archive

Subsystems (2)

v4 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

PATCH v4 1/6 xen/arm: report proper GIC version via XEN_DOMCTL_getdomaininfo

When creating a domain on ARM, and passing XEN_DOMCTL_CONFIG_GIC_NATIVE for the gic_version field in the struct xen_arch_domainconfig, arch_sanitise_domain_config() resolves this to the approrpiate GIC_V2 or GIC_V3 version the domain actually has, based on the host's gic_hw_version(). That value is stored in the domain as d->arch.vgic.version, but can't be queried through any other domctl later. Toolstacks that create and build a domain in the same call already have this info from the createdomain reply and never need to ask again.

Toolstacks that create a domain and build it later from a separate process do need to ask again. But, the ARM implementation only fills in info->flags and info->gpaddr_bits. info->arch_config is left zeroed, so XEN_DOMCTL_getdomaininfo always reports gic_version as XEN_DOMCTL_CONFIG_GIC_NATIVE (0) regardless of what was actually configured earlier.

---
Changes in v4:
- Added 'Reviewed-by'

---
 xen/arch/arm/domctl.c | 2 ++
 1 file changed, 2 insertions(+)
xen/arch/arm/domctl.c +2 -0
index 6c9a3f9920..b76af56fad 100644
@@ -24,6 +24,8 @@ void arch_get_domain_info(const struct domain *d,
     info->flags |= XEN_DOMINF_hap;
 
     info->gpaddr_bits = p2m_ipa_bits;
+
+    info->arch_config.gic_version = d->arch.vgic.version;
 }
 
 static int handle_vuart_init(struct domain *d, 
Signature · 10 lines
2.53.0



--
Julian Vetter | Vates Hypervisor & Kernel Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech

A message that is not in the archive (sent off-list or not yet received): [email protected]

#3 lore raw

PATCH v4 2/6 ARM/sysctl: Expose the supported guest GIC modes in physinfo

From: Andrew Cooper <[email protected]>

In preparation to simplify the domain creation logic surrounding GIC version.

On a GICv3 host, also report support for GICv2-compatible guests when the hardware's vGICv2 compatibility mode is enabled, rather than just the native GIC version.

---
Changes in v4:
- Report GICv2 support when a GICv3 host has vGICv2 compatibility mode enabled
- Add ASSERT_UNREACHABLE() for the GIC_INVALID case
- Fix a typo in a comment

---
 xen/arch/arm/include/asm/vgic.h |  6 ++++++
 xen/arch/arm/sysctl.c           | 34 +++++++++++++++++++++++++++++++++
 xen/arch/arm/vgic-v2.c          |  5 +++++
 xen/include/public/sysctl.h     |  2 ++
 4 files changed, 47 insertions(+)
xen/arch/arm/include/asm/vgic.h +6 -0
index 6f9ab1c98c..26c53aaf3c 100644
@@ -433,6 +433,12 @@ unsigned int vgic_max_vcpus(unsigned int domctl_vgic_version);
 void vgic_v2_setup_hw(paddr_t dbase, paddr_t cbase, paddr_t csize,
                       paddr_t vbase, uint32_t aliased_offset);
 
+#ifdef CONFIG_VGICV2
+bool vgic_v2_hw_enabled(void);
+#else
+static inline bool vgic_v2_hw_enabled(void) { return false; }
+#endif
+
 #ifdef CONFIG_GICV3
 struct rdist_region;
 void vgic_v3_setup_hw(paddr_t dbase,
xen/arch/arm/sysctl.c +34 -0
index 32cab4feff..8411deb7e2 100644
@@ -12,7 +12,11 @@
 #include <xen/dt-overlay.h>
 #include <xen/errno.h>
 #include <xen/hypercall.h>
+
 #include <asm/arm64/sve.h>
+#include <asm/gic.h>
+#include <asm/vgic.h>
+
 #include <public/sysctl.h>
 
 void arch_do_physinfo(struct xen_sysctl_physinfo *pi)
@@ -21,6 +25,36 @@ void arch_do_physinfo(struct xen_sysctl_physinfo *pi)
 
     pi->arch_capabilities |= MASK_INSR(sve_encode_vl(get_sys_vl_len()),
                                        XEN_SYSCTL_PHYSCAP_ARM_SVE_MASK);
+
+    /*
+     * The GIC version(s) we're happy creating guests with. Right now for
+     * simplicity it is tied to the active hardware version, but this will
+     * cease to be the case if/when the compatibility modes are enabled.
+     */
+    switch ( gic_hw_version() )
+    {
+    case GIC_V2:
+        pi->arch_capabilities |= XEN_SYSCTL_PHYSCAP_ARM_GIC_V2;
+        break;
+
+    case GIC_V3:
+        pi->arch_capabilities |= XEN_SYSCTL_PHYSCAP_ARM_GIC_V3;
+
+        /* GICv3 may additionally support GICv2-compatible guests. */
+        if ( vgic_v2_hw_enabled() )
+            pi->arch_capabilities |= XEN_SYSCTL_PHYSCAP_ARM_GIC_V2;
+        break;
+
+    case GIC_INVALID:
+        /*
+         * Running a control domain without having the GIC sorted yet?
+         * Something's broken, but there's nothing we can do about it here.
+         */
+        ASSERT_UNREACHABLE();
+        printk_once(XENLOG_ERR "Unrecognised GIC version %d\n",
+                    gic_hw_version());
+        break;
+    }
 }
 
 long arch_do_sysctl(struct xen_sysctl *sysctl,
xen/arch/arm/vgic-v2.c +5 -0
index 642407fd5b..5d758dd93b 100644
@@ -49,6 +49,11 @@ void vgic_v2_setup_hw(paddr_t dbase, paddr_t cbase, paddr_t csize,
     vgic_v2_hw.aliased_offset = aliased_offset;
 }
 
+bool vgic_v2_hw_enabled(void)
+{
+    return vgic_v2_hw.enabled;
+}
+
 #define NR_TARGETS_PER_ITARGETSR    4U
 #define NR_BITS_PER_TARGET  (32U / NR_TARGETS_PER_ITARGETSR)
 
xen/include/public/sysctl.h +2 -0
index c7cd9b4eb0..d20ebf3644 100644
@@ -106,6 +106,8 @@ struct xen_sysctl_tbuf_op {
 
 #if defined(__arm__) || defined(__aarch64__)
 #define XEN_SYSCTL_PHYSCAP_ARM_SVE_MASK  (0x1FU)
+#define XEN_SYSCTL_PHYSCAP_ARM_GIC_V2    (1U << 5)
+#define XEN_SYSCTL_PHYSCAP_ARM_GIC_V3    (1U << 6)
 #endif
 
 struct xen_sysctl_physinfo {
Signature · 10 lines
2.53.0



--
Julian Vetter | Vates Hypervisor & Kernel Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech
#4 lore raw

Re: [PATCH v4 2/6] ARM/sysctl: Expose the supported guest GIC modes in physinfo

Quoted 99 lines · Julian Vetter

From: Andrew Cooper <[email protected]>

In preparation to simplify the domain creation logic surrounding GIC version.

On a GICv3 host, also report support for GICv2-compatible guests when the hardware's vGICv2 compatibility mode is enabled, rather than just the native GIC version.

---
Changes in v4:
- Report GICv2 support when a GICv3 host has vGICv2 compatibility mode enabled
- Add ASSERT_UNREACHABLE() for the GIC_INVALID case
- Fix a typo in a comment

---
 xen/arch/arm/include/asm/vgic.h |  6 ++++++
 xen/arch/arm/sysctl.c           | 34 +++++++++++++++++++++++++++++++++
 xen/arch/arm/vgic-v2.c          |  5 +++++
 xen/include/public/sysctl.h     |  2 ++
 4 files changed, 47 insertions(+)
xen/arch/arm/include/asm/vgic.h +6 -0
index 6f9ab1c98c..26c53aaf3c 100644
@@ -433,6 +433,12 @@ unsigned int vgic_max_vcpus(unsigned int domctl_vgic_version);
 void vgic_v2_setup_hw(paddr_t dbase, paddr_t cbase, paddr_t csize,
                       paddr_t vbase, uint32_t aliased_offset);
 
+#ifdef CONFIG_VGICV2
+bool vgic_v2_hw_enabled(void);
+#else
+static inline bool vgic_v2_hw_enabled(void) { return false; }
+#endif
+
 #ifdef CONFIG_GICV3
 struct rdist_region;
 void vgic_v3_setup_hw(paddr_t dbase,
xen/arch/arm/sysctl.c +34 -0
index 32cab4feff..8411deb7e2 100644
@@ -12,7 +12,11 @@
 #include <xen/dt-overlay.h>
 #include <xen/errno.h>
 #include <xen/hypercall.h>
+
 #include <asm/arm64/sve.h>
+#include <asm/gic.h>
+#include <asm/vgic.h>
+
 #include <public/sysctl.h>
 
 void arch_do_physinfo(struct xen_sysctl_physinfo *pi)
@@ -21,6 +25,36 @@ void arch_do_physinfo(struct xen_sysctl_physinfo *pi)
 
     pi->arch_capabilities |= MASK_INSR(sve_encode_vl(get_sys_vl_len()),
                                        XEN_SYSCTL_PHYSCAP_ARM_SVE_MASK);
+
+    /*
+     * The GIC version(s) we're happy creating guests with. Right now for
+     * simplicity it is tied to the active hardware version, but this will
+     * cease to be the case if/when the compatibility modes are enabled.
+     */
+    switch ( gic_hw_version() )
+    {
+    case GIC_V2:
+        pi->arch_capabilities |= XEN_SYSCTL_PHYSCAP_ARM_GIC_V2;
+        break;
+
+    case GIC_V3:
+        pi->arch_capabilities |= XEN_SYSCTL_PHYSCAP_ARM_GIC_V3;
+
+        /* GICv3 may additionally support GICv2-compatible guests. */
+        if ( vgic_v2_hw_enabled() )
+            pi->arch_capabilities |= XEN_SYSCTL_PHYSCAP_ARM_GIC_V2;
+        break;
+
+    case GIC_INVALID:
+        /*
+         * Running a control domain without having the GIC sorted yet?
+         * Something's broken, but there's nothing we can do about it here.
+         */
+        ASSERT_UNREACHABLE();
+        printk_once(XENLOG_ERR "Unrecognised GIC version %d\n",
+                    gic_hw_version());
+        break;
+    }
 }
 
 long arch_do_sysctl(struct xen_sysctl *sysctl,
xen/arch/arm/vgic-v2.c +0 -0
index 642407fd5b..5d758dd93b 100644

xen/arch/arm/vgic-v2.c void vgic_v2_setup_hw(paddr_t dbase, paddr_t cbase, paddr_t csize, · line 55

Quoted 7 lines
xen/arch/arm/vgic-v2.c +4 -0
@@ -49,6 +49,11 @@ void vgic_v2_setup_hw(paddr_t dbase, paddr_t cbase, paddr_t csize,
     vgic_v2_hw.aliased_offset = aliased_offset;
 }
 
+bool vgic_v2_hw_enabled(void)
+{
+    return vgic_v2_hw.enabled;
+}

There are two implementations of vgicv2 - the "old" default one and a new one gated by CONFIG_NEW_VGIC. You need to also add this helper to the latter one.

~Michal

#5 lore raw

PATCH v4 3/6 tools/arm: choose GIC version explicitly instead of relying on GIC_NATIVE

In reply to a message not in the archive

XEN_DOMCTL_CONFIG_GIC_NATIVE lets the toolstack ask Xen to silently resolve the domain's GIC version to whatever the host hardware has. Xen then writes the resolved value back into the same in/out xen_arch_domainconfig the toolstack used as input, which is the kind of API abuse we're trying to get rid of. The struct passed to createdomain should only be an input parameter.

Move the "pick the best available GIC version" decision to the toolstack, using the XEN_SYSCTL_PHYSCAP_ARM_GIC_V2/V3 capability bits already exposed via XEN_SYSCTL_physinfo:

* libxl__arch_domain_build_info_setdefault() resolves LIBXL_GIC_VERSION_DEFAULT to v3 if available, else v2, else fails, before the config is built.
* The Python xc.domain_create() binding does the same via a call to xc_physinfo().
* libxl__arch_domain_prepare_config() therefore only ever sees a concrete v2/v3 request and just validates it. The GIC_NATIVE case is dropped since setdefault() always resolves it first.

This guarantees no toolstack path can still produce XEN_DOMCTL_CONFIG_GIC_NATIVE, in preparation for removing it from the Xen side and from the ABI entirely.

---
Changes in v4:
- Fix a missing closing bracket in arch_capabilities_arm_has()

---
 .../include/xen-tools/arm-arch-capabilities.h | 17 ++++++++++++++++
 tools/libs/light/libxl_arm.c                  | 17 +++++++++++++---
 tools/python/xen/lowlevel/xc/xc.c             | 20 ++++++++++++++++++-
 3 files changed, 50 insertions(+), 4 deletions(-)
tools/include/xen-tools/arm-arch-capabilities.h +17 -0
index 4aa4c6c34a..ce1b3bdd71 100644
@@ -6,6 +6,7 @@
 #ifndef ARM_ARCH_CAPABILITIES_H
 #define ARM_ARCH_CAPABILITIES_H
 
+#include <stdbool.h>
 #include <stdint.h>
 #include <xen/sysctl.h>
 
@@ -25,4 +26,20 @@ unsigned int arch_capabilities_arm_sve(unsigned int arch_capabilities)
 #endif
 }
 
+/*
+ * Generic test for any single-bit XEN_SYSCTL_PHYSCAP_ARM_* capability, e.g.
+ * arch_capabilities_arm_has(caps, XEN_SYSCTL_PHYSCAP_ARM_GIC_V2). Multi-bit
+ * fields (like the SVE vector length above) still need their own decoder.
+ */
+static inline
+bool arch_capabilities_arm_has(unsigned int arch_capabilities,
+                               unsigned int mask)
+{
+#if defined(__arm__) || defined(__aarch64__)
+    return !!(arch_capabilities & mask);
+#else
+    return false;
+#endif
+}
+
 #endif /* ARM_ARCH_CAPABILITIES_H */
tools/libs/light/libxl_arm.c +14 -3
index 7e9f8a1bc3..926d651857 100644
@@ -196,9 +196,6 @@ int libxl__arch_domain_prepare_config(libxl__gc *gc,
     LOG(DEBUG, " - Allocate %u SPIs", config->arch.nr_spis);
 
     switch (d_config->b_info.arch_arm.gic_version) {
-    case LIBXL_GIC_VERSION_DEFAULT:
-        config->arch.gic_version = XEN_DOMCTL_CONFIG_GIC_NATIVE;
-        break;
     case LIBXL_GIC_VERSION_V2:
         config->arch.gic_version = XEN_DOMCTL_CONFIG_GIC_V2;
         break;
@@ -1800,6 +1797,20 @@ int libxl__arch_domain_build_info_setdefault(libxl__gc *gc,
     /* Trapping of unmapped accesses enabled by default.  */
     libxl_defbool_setdefault(&b_info->trap_unmapped_accesses, true);
 
+    /* Pick the best GIC version available if none was requested. */
+    if (b_info->arch_arm.gic_version == LIBXL_GIC_VERSION_DEFAULT) {
+        if (arch_capabilities_arm_has(physinfo->arch_capabilities,
+                                      XEN_SYSCTL_PHYSCAP_ARM_GIC_V3))
+            b_info->arch_arm.gic_version = LIBXL_GIC_VERSION_V3;
+        else if (arch_capabilities_arm_has(physinfo->arch_capabilities,
+                                           XEN_SYSCTL_PHYSCAP_ARM_GIC_V2))
+            b_info->arch_arm.gic_version = LIBXL_GIC_VERSION_V2;
+        else {
+            LOG(ERROR, "No supported GIC version found on this host");
+            return ERROR_FAIL;
+        }
+    }
+
     /* Sanitise SVE parameter */
     if (b_info->arch_arm.sve_vl) {
         unsigned int max_sve_vl =
tools/python/xen/lowlevel/xc/xc.c +19 -1
index 7a4bf54597..e9e1d8572e 100644
@@ -163,7 +163,25 @@ static PyObject *pyxc_domain_create(XcObject *self,
                                       ~(XEN_X86_EMU_VPCI |
                                         XEN_X86_EMU_USE_PIRQ);
 #elif defined (__arm__) || defined(__aarch64__)
-    config.arch.gic_version = XEN_DOMCTL_CONFIG_GIC_NATIVE;
+    {
+        xc_physinfo_t pinfo;
+
+        if ( xc_physinfo(self->xc_handle, &pinfo) != 0 )
+            return pyxc_error_to_exception(self->xc_handle);
+
+        if ( arch_capabilities_arm_has(pinfo.arch_capabilities,
+                                       XEN_SYSCTL_PHYSCAP_ARM_GIC_V3) )
+            config.arch.gic_version = XEN_DOMCTL_CONFIG_GIC_V3;
+        else if ( arch_capabilities_arm_has(pinfo.arch_capabilities,
+                                            XEN_SYSCTL_PHYSCAP_ARM_GIC_V2) )
+            config.arch.gic_version = XEN_DOMCTL_CONFIG_GIC_V2;
+        else
+        {
+            errno = EINVAL;
+            PyErr_SetFromErrno(xc_error_obj);
+            return NULL;
+        }
+    }
 #else
 #error Architecture not supported
 #endif
Signature · 10 lines
2.53.0



--
Julian Vetter | Vates Hypervisor & Kernel Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech
#6 lore raw

Re: [PATCH v4 3/6] tools/arm: choose GIC version explicitly instead of relying on GIC_NATIVE

Quoted 7 lines · Julian Vetter

XEN_DOMCTL_CONFIG_GIC_NATIVE lets the toolstack ask Xen to silently resolve the domain's GIC version to whatever the host hardware has. Xen then writes the resolved value back into the same in/out xen_arch_domainconfig the toolstack used as input, which is the kind of API abuse we're trying to get rid of. The struct passed to createdomain should only be an input parameter.

Quoted 6 lines

Move the "pick the best available GIC version" decision to the toolstack, using the XEN_SYSCTL_PHYSCAP_ARM_GIC_V2/V3 capability bits already exposed via XEN_SYSCTL_physinfo:

* libxl__arch_domain_build_info_setdefault() resolves LIBXL_GIC_VERSION_DEFAULT to v3 if available, else v2, else fails,

LIBXL_GIC_VERSION_DEFAULT should be renamed to LIBXL_GIC_VERSION_NONE to denote that the user did not set any particular version. With your change there is no default and libxl__arch_domain_build_info_setdefault() resolves it before anything else can see it.

Quoted 11 lines

before the config is built.
* The Python xc.domain_create() binding does the same via a call to xc_physinfo().
* libxl__arch_domain_prepare_config() therefore only ever sees a concrete v2/v3 request and just validates it. The GIC_NATIVE case is dropped since setdefault() always resolves it first.

This guarantees no toolstack path can still produce XEN_DOMCTL_CONFIG_GIC_NATIVE, in preparation for removing it from the Xen side and from the ABI entirely.

Quoted 8 lines

---
Changes in v4:
- Fix a missing closing bracket in arch_capabilities_arm_has()

---
 .../include/xen-tools/arm-arch-capabilities.h | 17 ++++++++++++++++
 tools/libs/light/libxl_arm.c                  | 17 +++++++++++++---
 tools/python/xen/lowlevel/xc/xc.c             | 20 ++++++++++++++++++-

You need to update the documentation (i.e. xl.cfg) too.

Quoted 15 lines
 3 files changed, 50 insertions(+), 4 deletions(-)
tools/include/xen-tools/arm-arch-capabilities.h +1 -0
index 4aa4c6c34a..ce1b3bdd71 100644
@@ -6,6 +6,7 @@
 #ifndef ARM_ARCH_CAPABILITIES_H
 #define ARM_ARCH_CAPABILITIES_H
 
+#include <stdbool.h>
 #include <stdint.h>
 #include <xen/sysctl.h>
 

tools/include/xen-tools/arm-arch-capabilities.h unsigned int arch_capabilities_arm_sve(unsigned int arch_capabilities) · line 30

Quoted 5 lines
tools/include/xen-tools/arm-arch-capabilities.h +2 -0
@@ -25,4 +26,20 @@ unsigned int arch_capabilities_arm_sve(unsigned int arch_capabilities)
 #endif
 }
 
+/*
+ * Generic test for any single-bit XEN_SYSCTL_PHYSCAP_ARM_* capability, e.g.

What makes the implementation single-bit?

Otherwise, LGTM.

~Michal

#7 lore raw

PATCH v4 4/6 xen/arm: remove XEN_DOMCTL_CONFIG_GIC_NATIVE from the ABI

In reply to a message not in the archive

Now that the toolstack always resolves a concrete GIC_V2 or GIC_V3 before calling createdomain, nothing on the Xen side needs to resolve GIC_NATIVE either:

* A new gic_domctl_version() helper returns the XEN_DOMCTL_CONFIG_GIC_* value matching the host's gic_hw_version().
* arch_sanitise_domain_config() uses it to validate that the requested version is compatible with the hardware, rather than resolving GIC_NATIVE and writing the result back into config->arch.gic_version. There's currently no support to run a guest on a GIC version other than the host's, so this is just an equality check.
* create_dom0() and arch_parse_dom0less_node(), which both always want a vGIC that exactly matches the hardware, use the same helper instead of GIC_NATIVE.

With nothing left resolving or relying on it, drop XEN_DOMCTL_CONFIG_GIC_NATIVE from the public ABI. Every caller must now request a concrete GIC_V2 or GIC_V3.

This is an incompatible change for any toolstack still passing 0 (formerly GIC_NATIVE) expecting Xen to auto-select a version. Add a CHANGELOG.md entry, noting that available GIC versions can be queried via XEN_SYSCTL_physinfo.

---
Changes in v4:
- Don't bump XEN_DOMCTL_INTERFACE_VERSION for this: it's an API change, not an ABI one. Leave a comment marking XEN_DOMCTL_CONFIG_GIC_NATIVE as removed instead, and mention in the CHANGELOG.md entry that available GIC versions can be queried via XEN_SYSCTL_physinfo
- Simplify comment above the GIC version check
- Report requested GIC version in the "Unsupported GIC version" print

---
 CHANGELOG.md                   |  4 ++++
 xen/arch/arm/dom0less-build.c  |  3 ++-
 xen/arch/arm/domain.c          | 24 ++++++++----------------
 xen/arch/arm/domain_build.c    |  3 ++-
 xen/arch/arm/gic.c             | 16 ++++++++++++++++
 xen/arch/arm/include/asm/gic.h |  6 ++++++
 xen/include/public/arch-arm.h  |  2 +-
 7 files changed, 39 insertions(+), 19 deletions(-)
CHANGELOG.md +4 -0
index 356be88351..5dfa242030 100644
@@ -13,6 +13,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
 ### Added
 
 ### Removed
+ - On Arm:
+   - XEN_DOMCTL_CONFIG_GIC_NATIVE has been removed. Toolstacks must now
+     explicitly request GIC_V2 or GIC_V3 when creating a domain.
+     Available GIC versions can be queried via XEN_SYSCTL_physinfo.
  - On x86:
    - The kexec "v1" interface, which was declared obsolete in Xen 4.4 (2013).
      The only known user was the classic-xen fork of Linux.  This does not
xen/arch/arm/dom0less-build.c +2 -1
index 3f48f74226..5b01843db4 100644
@@ -23,6 +23,7 @@
 #include <asm/arm64/sve.h>
 #include <asm/domain_build.h>
 #include <asm/firmware/sci.h>
+#include <asm/gic.h>
 #include <asm/grant_table.h>
 #include <asm/setup.h>
 
@@ -368,7 +369,7 @@ int __init arch_parse_dom0less_node(struct dt_device_node *node,
     unsigned int flags = bd->create_flags;
     uint32_t val;
 
-    d_cfg->arch.gic_version = XEN_DOMCTL_CONFIG_GIC_NATIVE;
+    d_cfg->arch.gic_version = gic_domctl_version();
     d_cfg->flags |= XEN_DOMCTL_CDF_hvm | XEN_DOMCTL_CDF_hap;
 
     if ( domu_dt_sci_parse(node, d_cfg) )
xen/arch/arm/domain.c +8 -16
index baa3a5d708..94f8f077e9 100644
@@ -609,23 +609,15 @@ int arch_sanitise_domain_config(struct xen_domctl_createdomain *config)
         return -EINVAL;
     }
 
-    /* Fill in the native GIC version, passed back to the toolstack. */
-    if ( config->arch.gic_version == XEN_DOMCTL_CONFIG_GIC_NATIVE )
+    /*
+     * There's currently no support to run a guest on a GIC version other
+     * than the host's.
+     */
+    if ( config->arch.gic_version != gic_domctl_version() )
     {
-        switch ( gic_hw_version() )
-        {
-        case GIC_V2:
-            config->arch.gic_version = XEN_DOMCTL_CONFIG_GIC_V2;
-            break;
-
-        case GIC_V3:
-            config->arch.gic_version = XEN_DOMCTL_CONFIG_GIC_V3;
-            break;
-
-        default:
-            ASSERT_UNREACHABLE();
-            return -EINVAL;
-        }
+        dprintk(XENLOG_INFO, "Unsupported GIC version %u\n",
+                config->arch.gic_version);
+        return -EINVAL;
     }
 
     /* max_vcpus depends on the GIC version, and Xen's compiled limit. */
xen/arch/arm/domain_build.c +2 -1
index 72d5316180..cd9509d7b9 100644
@@ -26,6 +26,7 @@
 #include <xen/warning.h>
 #include <xen/static-shmem.h>
 #include <asm/device.h>
+#include <asm/gic.h>
 #include <asm/setup.h>
 #include <asm/tee/tee.h>
 #include <asm/pci.h>
@@ -1960,7 +1961,7 @@ void __init create_dom0(void)
     int rc;
 
     /* The vGIC for DOM0 is exactly emulating the hardware GIC */
-    dom0_cfg.arch.gic_version = XEN_DOMCTL_CONFIG_GIC_NATIVE;
+    dom0_cfg.arch.gic_version = gic_domctl_version();
     dom0_cfg.arch.nr_spis = vgic_def_nr_spis();
     dom0_cfg.arch.tee_type = tee_get_type();
     dom0_cfg.max_vcpus = dom0_max_vcpus();
xen/arch/arm/gic.c +16 -0
index ee75258fc3..fc55a65159 100644
@@ -56,6 +56,22 @@ enum gic_version gic_hw_version(void)
    return gic_hw_ops->info->hw_version;
 }
 
+uint8_t gic_domctl_version(void)
+{
+    switch ( gic_hw_version() )
+    {
+    case GIC_V2:
+        return XEN_DOMCTL_CONFIG_GIC_V2;
+
+    case GIC_V3:
+        return XEN_DOMCTL_CONFIG_GIC_V3;
+
+    default:
+        ASSERT_UNREACHABLE();
+        return 0;
+    }
+}
+
 unsigned int gic_number_lines(void)
 {
     return gic_hw_ops->info->nr_lines;
xen/arch/arm/include/asm/gic.h +6 -0
index ff22dea40d..de6eabfadd 100644
@@ -262,6 +262,12 @@ DECLARE_PER_CPU(uint64_t, lr_mask);
 
 extern enum gic_version gic_hw_version(void);
 
+/*
+ * The XEN_DOMCTL_CONFIG_GIC_* value matching the GIC version actually
+ * present on this host.
+ */
+extern uint8_t gic_domctl_version(void);
+
 /* Program the IRQ type into the GIC */
 void gic_set_irq_type(struct irq_desc *desc, unsigned int type);
 
xen/include/public/arch-arm.h +1 -1
index 7d6f87e8b2..e95fa33eb7 100644
@@ -319,7 +319,7 @@ DEFINE_XEN_GUEST_HANDLE(vcpu_guest_context_t);
  * struct xen_arch_domainconfig's ABI is covered by
  * XEN_DOMCTL_INTERFACE_VERSION.
  */
-#define XEN_DOMCTL_CONFIG_GIC_NATIVE    0
+/*      XEN_DOMCTL_CONFIG_GIC_NATIVE    0 - removed in Xen 4.23 */
 #define XEN_DOMCTL_CONFIG_GIC_V2        1
 #define XEN_DOMCTL_CONFIG_GIC_V3        2
 
Signature · 10 lines
2.53.0



--
Julian Vetter | Vates Hypervisor & Kernel Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech
#8 lore raw

Re: [PATCH v4 4/6] xen/arm: remove XEN_DOMCTL_CONFIG_GIC_NATIVE from the ABI

Quoted 103 lines · Julian Vetter

Now that the toolstack always resolves a concrete GIC_V2 or GIC_V3 before calling createdomain, nothing on the Xen side needs to resolve GIC_NATIVE either:

* A new gic_domctl_version() helper returns the XEN_DOMCTL_CONFIG_GIC_* value matching the host's gic_hw_version().
* arch_sanitise_domain_config() uses it to validate that the requested version is compatible with the hardware, rather than resolving GIC_NATIVE and writing the result back into config->arch.gic_version. There's currently no support to run a guest on a GIC version other than the host's, so this is just an equality check.
* create_dom0() and arch_parse_dom0less_node(), which both always want a vGIC that exactly matches the hardware, use the same helper instead of GIC_NATIVE.

With nothing left resolving or relying on it, drop XEN_DOMCTL_CONFIG_GIC_NATIVE from the public ABI. Every caller must now request a concrete GIC_V2 or GIC_V3.

This is an incompatible change for any toolstack still passing 0 (formerly GIC_NATIVE) expecting Xen to auto-select a version. Add a CHANGELOG.md entry, noting that available GIC versions can be queried via XEN_SYSCTL_physinfo.

---
Changes in v4:
- Don't bump XEN_DOMCTL_INTERFACE_VERSION for this: it's an API change, not an ABI one. Leave a comment marking XEN_DOMCTL_CONFIG_GIC_NATIVE as removed instead, and mention in the CHANGELOG.md entry that available GIC versions can be queried via XEN_SYSCTL_physinfo
- Simplify comment above the GIC version check
- Report requested GIC version in the "Unsupported GIC version" print

---
 CHANGELOG.md                   |  4 ++++
 xen/arch/arm/dom0less-build.c  |  3 ++-
 xen/arch/arm/domain.c          | 24 ++++++++----------------
 xen/arch/arm/domain_build.c    |  3 ++-
 xen/arch/arm/gic.c             | 16 ++++++++++++++++
 xen/arch/arm/include/asm/gic.h |  6 ++++++
 xen/include/public/arch-arm.h  |  2 +-
 7 files changed, 39 insertions(+), 19 deletions(-)
CHANGELOG.md +4 -0
index 356be88351..5dfa242030 100644
@@ -13,6 +13,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
 ### Added
 
 ### Removed
+ - On Arm:
+   - XEN_DOMCTL_CONFIG_GIC_NATIVE has been removed. Toolstacks must now
+     explicitly request GIC_V2 or GIC_V3 when creating a domain.
+     Available GIC versions can be queried via XEN_SYSCTL_physinfo.
  - On x86:
    - The kexec "v1" interface, which was declared obsolete in Xen 4.4 (2013).
      The only known user was the classic-xen fork of Linux.  This does not
xen/arch/arm/dom0less-build.c +2 -1
index 3f48f74226..5b01843db4 100644
@@ -23,6 +23,7 @@
 #include <asm/arm64/sve.h>
 #include <asm/domain_build.h>
 #include <asm/firmware/sci.h>
+#include <asm/gic.h>
 #include <asm/grant_table.h>
 #include <asm/setup.h>
 
@@ -368,7 +369,7 @@ int __init arch_parse_dom0less_node(struct dt_device_node *node,
     unsigned int flags = bd->create_flags;
     uint32_t val;
 
-    d_cfg->arch.gic_version = XEN_DOMCTL_CONFIG_GIC_NATIVE;
+    d_cfg->arch.gic_version = gic_domctl_version();
     d_cfg->flags |= XEN_DOMCTL_CDF_hvm | XEN_DOMCTL_CDF_hap;
 
     if ( domu_dt_sci_parse(node, d_cfg) )
xen/arch/arm/domain.c +5 -10
index baa3a5d708..94f8f077e9 100644
@@ -609,23 +609,15 @@ int arch_sanitise_domain_config(struct xen_domctl_createdomain *config)
         return -EINVAL;
     }
 
-    /* Fill in the native GIC version, passed back to the toolstack. */
-    if ( config->arch.gic_version == XEN_DOMCTL_CONFIG_GIC_NATIVE )
+    /*
+     * There's currently no support to run a guest on a GIC version other
+     * than the host's.
+     */
+    if ( config->arch.gic_version != gic_domctl_version() )
     {
-        switch ( gic_hw_version() )
-        {
-        case GIC_V2:
-            config->arch.gic_version = XEN_DOMCTL_CONFIG_GIC_V2;
-            break;
-
-        case GIC_V3:
-            config->arch.gic_version = XEN_DOMCTL_CONFIG_GIC_V3;

xen/arch/arm/domain.c int arch_sanitise_domain_config(struct xen_domctl_createdomain *config) · line 621

Quoted 10 lines
xen/arch/arm/domain.c +3 -6
-            break;
-
-        default:
-            ASSERT_UNREACHABLE();
-            return -EINVAL;
-        }
+        dprintk(XENLOG_INFO, "Unsupported GIC version %u\n",
+                config->arch.gic_version);
+        return -EINVAL;
     }

I don't understand. How are you going to support a rightful request (supported today) for a vGICv2 on a host GICv3 that supports GICv2 by legacy mode? Your previous patches already expose this configuration via sysctl. We should allow it and in arch_sanitise_domain_config() Xen should just check if the passed domctl GIC version is indeed supported.

At this point I also realized that your previous patch only uses sysctl information if no GIC version was selected by the guest. I think it would be better for the toolstack to read it also when the version is explicitly selected to allow a faster failure not requiring the trip to Xen.

Quoted 28 lines
xen/arch/arm/domain.c +0 -0
     /* max_vcpus depends on the GIC version, and Xen's compiled limit. */
xen/arch/arm/domain_build.c +2 -1
index 72d5316180..cd9509d7b9 100644
@@ -26,6 +26,7 @@
 #include <xen/warning.h>
 #include <xen/static-shmem.h>
 #include <asm/device.h>
+#include <asm/gic.h>
 #include <asm/setup.h>
 #include <asm/tee/tee.h>
 #include <asm/pci.h>
@@ -1960,7 +1961,7 @@ void __init create_dom0(void)
     int rc;
 
     /* The vGIC for DOM0 is exactly emulating the hardware GIC */
-    dom0_cfg.arch.gic_version = XEN_DOMCTL_CONFIG_GIC_NATIVE;
+    dom0_cfg.arch.gic_version = gic_domctl_version();
     dom0_cfg.arch.nr_spis = vgic_def_nr_spis();
     dom0_cfg.arch.tee_type = tee_get_type();
     dom0_cfg.max_vcpus = dom0_max_vcpus();
xen/arch/arm/gic.c +0 -0
index ee75258fc3..fc55a65159 100644

xen/arch/arm/gic.c enum gic_version gic_hw_version(void) · line 59

Quoted 4 lines
xen/arch/arm/gic.c +1 -0
@@ -56,6 +56,22 @@ enum gic_version gic_hw_version(void)
    return gic_hw_ops->info->hw_version;
 }
 
+uint8_t gic_domctl_version(void)

gic_domctl_hw_version() would be a better fit to clearly denote that it returns the HW version translated to domctl field.

Quoted 40 lines
xen/arch/arm/gic.c +15 -0
+{
+    switch ( gic_hw_version() )
+    {
+    case GIC_V2:
+        return XEN_DOMCTL_CONFIG_GIC_V2;
+
+    case GIC_V3:
+        return XEN_DOMCTL_CONFIG_GIC_V3;
+
+    default:
+        ASSERT_UNREACHABLE();
+        return 0;
+    }
+}
+
 unsigned int gic_number_lines(void)
 {
     return gic_hw_ops->info->nr_lines;
xen/arch/arm/include/asm/gic.h +6 -0
index ff22dea40d..de6eabfadd 100644
@@ -262,6 +262,12 @@ DECLARE_PER_CPU(uint64_t, lr_mask);
 
 extern enum gic_version gic_hw_version(void);
 
+/*
+ * The XEN_DOMCTL_CONFIG_GIC_* value matching the GIC version actually
+ * present on this host.
+ */
+extern uint8_t gic_domctl_version(void);
+
 /* Program the IRQ type into the GIC */
 void gic_set_irq_type(struct irq_desc *desc, unsigned int type);
 
xen/include/public/arch-arm.h +0 -0
index 7d6f87e8b2..e95fa33eb7 100644

xen/include/public/arch-arm.h DEFINE_XEN_GUEST_HANDLE(vcpu_guest_context_t); · line 325

Quoted 8 lines
xen/include/public/arch-arm.h +1 -1
@@ -319,7 +319,7 @@ DEFINE_XEN_GUEST_HANDLE(vcpu_guest_context_t);
  * struct xen_arch_domainconfig's ABI is covered by
  * XEN_DOMCTL_INTERFACE_VERSION.
  */
-#define XEN_DOMCTL_CONFIG_GIC_NATIVE    0
+/*      XEN_DOMCTL_CONFIG_GIC_NATIVE    0 - removed in Xen 4.23 */
 #define XEN_DOMCTL_CONFIG_GIC_V2        1
 #define XEN_DOMCTL_CONFIG_GIC_V3        2
 

~Michal

#9 lore raw

PATCH v4 5/6 xen/arm: report clock_frequency via sysctl physinfo, not createdomain

In reply to a message not in the archive

The xen_arch_domainconfig.clock_frequency value is populated in domain_vtimer_init() during XEN_DOMCTL_createdomain from the global timer_dt_clock_frequency, which comes from the host's DT timer node and has nothing to do with the domain being created. Like now removed GIC_NATIVE resolution, this is a host-wide system property being smuggled out through a domain-creation IN struct.

Expose it instead as a new arch_clock_frequency_hz field in XEN_SYSCTL_physinfo, populated via arch_do_physinfo(), and mirroring how the GIC capability bits were already moved there.

Rather than making the field dependant on DT boot, make Xen always report the timer frequency, either via the DT "clock-frequency" node, or directly via CNTFRQ_EL0. preinit_xen_time() already computes cpu_khz for every boot path. The renamed timer_clock_frequency_hz now captures whichever of the two produced that value, in full Hz precision, instead of only recording the DT case. So, ACPI guests get a real value too, allowing to drop the special case.

Although the CNTFRQ_EL0 register is 64 bits wide, and some current timer implementations run at 1GHz, a 32bit value is sufficient to store the timer value, because it only mirrors the DT 'clock-frequency' property, which the bindings define as a single 32-bit cell. The XEN_DOMCTL_INTERFACE_VERSION doesn't need to be bumped, because only a previously zero'ed / ignored field is now used.

The xen_arch_domainconfig parameter passed to domain_vtimer_init() is no longer needed, so drop that parameter entirely. libxl now fetches the frequency via libxl_get_physinfo() in libxl__arch_domain_save_config() instead of reading it back out of the createdomain reply. The OCaml xen_arch_domainconfig mirror drops the field too.

---
Changes in v4:
- Rename timer_dt_clock_frequency to timer_clock_frequency_hz
- Always populate the frequency, and record it on the CNTFRQ_EL0 path in preinit_xen_time(), instead of only when booting via DT
- ACPI guests now get a real frequency value instead of 0

---
 tools/libs/light/libxl.c          |  1 +
 tools/libs/light/libxl_arm.c      | 13 ++++++++++++-
 tools/libs/light/libxl_types.idl  |  1 +
 tools/ocaml/libs/xc/xenctrl.ml    |  1 -
 tools/ocaml/libs/xc/xenctrl.mli   |  1 -
 xen/arch/arm/domain.c             |  2 +-
 xen/arch/arm/include/asm/time.h   |  6 +++---
 xen/arch/arm/include/asm/vtimer.h |  3 +--
 xen/arch/arm/sysctl.c             |  3 +++
 xen/arch/arm/time.c               |  7 ++++---
 xen/arch/arm/vtimer.c             |  4 +---
 xen/include/public/arch-arm.h     | 16 +---------------
 xen/include/public/sysctl.h       | 12 +++++++++++-
 13 files changed, 39 insertions(+), 31 deletions(-)
tools/libs/light/libxl.c +1 -0
index a1fe16274d..ec7e6d3f65 100644
@@ -410,6 +410,7 @@ int libxl_get_physinfo(libxl_ctx *ctx, libxl_physinfo *physinfo)
     physinfo->cap_gnttab_v2 =
         !!(xcphysinfo.capabilities & XEN_SYSCTL_PHYSCAP_gnttab_v2);
     physinfo->arch_capabilities = xcphysinfo.arch_capabilities;
+    physinfo->arch_clock_frequency_hz = xcphysinfo.arch_clock_frequency_hz;
 
     GC_FREE;
     return 0;
tools/libs/light/libxl_arm.c +12 -1
index 926d651857..cf441f0737 100644
@@ -252,6 +252,9 @@ int libxl__arch_domain_save_config(libxl__gc *gc,
                                    libxl__domain_build_state *state,
                                    const struct xen_domctl_createdomain *config)
 {
+    libxl_physinfo info;
+    int rc;
+
     switch (config->arch.gic_version) {
     case XEN_DOMCTL_CONFIG_GIC_V2:
         d_config->b_info.arch_arm.gic_version = LIBXL_GIC_VERSION_V2;
@@ -264,7 +267,15 @@ int libxl__arch_domain_save_config(libxl__gc *gc,
         return ERROR_FAIL;
     }
 
-    state->clock_frequency = config->arch.clock_frequency;
+    libxl_physinfo_init(&info);
+    rc = libxl_get_physinfo(CTX, &info);
+    if (rc) {
+        LOG(ERROR, "failed to get physinfo");
+        libxl_physinfo_dispose(&info);
+        return ERROR_FAIL;
+    }
+    state->clock_frequency = info.arch_clock_frequency_hz;
+    libxl_physinfo_dispose(&info);
 
     return 0;
 }
tools/libs/light/libxl_types.idl +1 -0
index a7893460f0..e24a3253f8 100644
@@ -1201,6 +1201,7 @@ libxl_physinfo = Struct("physinfo", [
     ("cap_gnttab_v1", bool),
     ("cap_gnttab_v2", bool),
     ("arch_capabilities", uint32),
+    ("arch_clock_frequency_hz", uint32), # ARM only
     ], dir=DIR_OUT)
 
 libxl_connectorinfo = Struct("connectorinfo", [
tools/ocaml/libs/xc/xenctrl.ml +0 -1
index 147afa62c2..582897af6d 100644
@@ -32,7 +32,6 @@ type xen_arm_arch_domainconfig =
   {
     gic_version: int;
     nr_spis: int;
-    clock_frequency: int32;
   }
 
 type x86_arch_emulation_flags =
tools/ocaml/libs/xc/xenctrl.mli +0 -1
index 9fccb2c2c2..9414b87164 100644
@@ -26,7 +26,6 @@ type vcpuinfo = {
 type xen_arm_arch_domainconfig = {
   gic_version: int;
   nr_spis: int;
-  clock_frequency: int32;
 }
 
 type x86_arch_emulation_flags =
xen/arch/arm/domain.c +1 -1
index 94f8f077e9..96dc65270b 100644
@@ -710,7 +710,7 @@ int arch_domain_create(struct domain *d,
     if ( (rc = domain_vgic_init(d, config->arch.nr_spis)) != 0 )
         goto fail;
 
-    if ( (rc = domain_vtimer_init(d, &config->arch)) != 0 )
+    if ( (rc = domain_vtimer_init(d)) != 0 )
         goto fail;
 
     if ( (rc = tee_domain_init(d, config->arch.tee_type)) != 0 )
xen/arch/arm/include/asm/time.h +3 -3
index c194dbb9f5..07091648a3 100644
@@ -87,10 +87,10 @@ enum timer_ppi
 };
 
 /*
- * Value of "clock-frequency" in the DT timer node if present.
- * 0 means the property doesn't exist.
+ * The timer frequency, in Hz, that Xen ended up using: either the DT
+ * "clock-frequency" override, or a direct CNTFRQ_EL0 read.
  */
-extern uint32_t timer_dt_clock_frequency;
+extern uint32_t timer_clock_frequency_hz;
 
 /* Get one of the timer IRQ number */
 unsigned int timer_get_irq(enum timer_ppi ppi);
xen/arch/arm/include/asm/vtimer.h +1 -2
index 9d4fb4c6e8..6bbfcf4e69 100644
@@ -20,8 +20,7 @@
 #ifndef __ARCH_ARM_VTIMER_H__
 #define __ARCH_ARM_VTIMER_H__
 
-extern int domain_vtimer_init(struct domain *d,
-                              struct xen_arch_domainconfig *config);
+extern int domain_vtimer_init(struct domain *d);
 extern int vcpu_vtimer_init(struct vcpu *v);
 extern bool vtimer_emulate(struct cpu_user_regs *regs, union hsr hsr);
 extern void virt_timer_save(struct vcpu *v);
xen/arch/arm/sysctl.c +3 -0
index 8411deb7e2..789ad78a14 100644
@@ -15,6 +15,7 @@
 
 #include <asm/arm64/sve.h>
 #include <asm/gic.h>
+#include <asm/time.h>
 #include <asm/vgic.h>
 
 #include <public/sysctl.h>
@@ -26,6 +27,8 @@ void arch_do_physinfo(struct xen_sysctl_physinfo *pi)
     pi->arch_capabilities |= MASK_INSR(sve_encode_vl(get_sys_vl_len()),
                                        XEN_SYSCTL_PHYSCAP_ARM_SVE_MASK);
 
+    pi->arch_clock_frequency_hz = timer_clock_frequency_hz;
+
     /*
      * The GIC version(s) we're happy creating guests with. Right now for
      * simplicity it is tied to the active hardware version, but this will
xen/arch/arm/time.c +4 -3
index 6955b2788f..bcbd038cb2 100644
@@ -35,7 +35,7 @@ uint64_t __read_mostly boot_count;
  * register-mapped time source in the SoC. */
 unsigned long __read_mostly cpu_khz;  /* CPU clock frequency in kHz. */
 
-uint32_t __read_mostly timer_dt_clock_frequency;
+uint32_t __read_mostly timer_clock_frequency_hz;
 
 static unsigned int timer_irq[MAX_TIMER_PPI];
 
@@ -120,7 +120,7 @@ static void __init preinit_dt_xen_time(void)
     {
         cpu_khz = DIV_ROUND(rate, 1000);
         validate_timer_frequency();
-        timer_dt_clock_frequency = rate;
+        timer_clock_frequency_hz = rate;
     }
 }
 
@@ -136,7 +136,8 @@ void __init preinit_xen_time(void)
 
     if ( !cpu_khz )
     {
-        cpu_khz = DIV_ROUND(READ_SYSREG(CNTFRQ_EL0) & CNTFRQ_MASK, 1000);
+        timer_clock_frequency_hz = READ_SYSREG(CNTFRQ_EL0) & CNTFRQ_MASK;
+        cpu_khz = DIV_ROUND(timer_clock_frequency_hz, 1000);
         validate_timer_frequency();
     }
 
xen/arch/arm/vtimer.c +1 -3
index 2e85ff2b6e..18f5676158 100644
@@ -52,7 +52,7 @@ static void virt_timer_expired(void *data)
     perfc_incr(vtimer_virt_inject);
 }
 
-int domain_vtimer_init(struct domain *d, struct xen_arch_domainconfig *config)
+int domain_vtimer_init(struct domain *d)
 {
     d->arch.virt_timer_base.offset = get_cycles();
     d->arch.virt_timer_base.nanoseconds =
@@ -60,8 +60,6 @@ int domain_vtimer_init(struct domain *d, struct xen_arch_domainconfig *config)
     d->time_offset.seconds = d->arch.virt_timer_base.nanoseconds;
     do_div(d->time_offset.seconds, 1000000000);
 
-    config->clock_frequency = timer_dt_clock_frequency;
-
     /*
      * Per the ACPI specification, providing a secure EL1 timer
      * interrupt is optional and will be ignored by non-secure OS.
xen/include/public/arch-arm.h +1 -15
index e95fa33eb7..c7118e7884 100644
@@ -335,7 +335,7 @@ DEFINE_XEN_GUEST_HANDLE(vcpu_guest_context_t);
 #define XEN_DOMCTL_CONFIG_ARM_V8R_EL1_MSA_VMSA    2
 
 struct xen_arch_domainconfig {
-    /* IN/OUT */
+    /* IN */
     uint8_t gic_version;
     /* IN - Contains SVE vector length divided by 128 */
     uint8_t sve_vl;
@@ -343,20 +343,6 @@ struct xen_arch_domainconfig {
     uint16_t tee_type;
     /* IN */
     uint32_t nr_spis;
-    /*
-     * OUT
-     * Based on the property clock-frequency in the DT timer node.
-     * The property may be present when the bootloader/firmware doesn't
-     * set correctly CNTFRQ which hold the timer frequency.
-     *
-     * As it's not possible to trap this register, we have to replicate
-     * the value in the guest DT.
-     *
-     * = 0 => property not present
-     * > 0 => Value of the property
-     *
-     */
-    uint32_t clock_frequency;
     /* IN */
     uint8_t arm_sci_type;
     /* IN */
xen/include/public/sysctl.h +11 -1
index d20ebf3644..04cb1f3bdc 100644
@@ -120,7 +120,17 @@ struct xen_sysctl_physinfo {
     uint32_t cpu_khz;
     uint32_t capabilities;/* XEN_SYSCTL_PHYSCAP_??? */
     uint32_t arch_capabilities;/* XEN_SYSCTL_PHYSCAP_{X86,ARM,...}_??? */
-    uint32_t pad;
+    /*
+     * ARM only. The timer frequency, in Hz, that Xen is using. Either
+     * "clock-frequency" property from DT timer node or CNTFRQ_EL0 value.
+     *
+     * As it's not possible to trap this register, we have to replicate the
+     * value in the guest DT.
+     *
+     * = 0 => non-ARM, or the frequency could not be determined.
+     * > 0 => The frequency, in Hz.
+     */
+    uint32_t arch_clock_frequency_hz;
     uint64_aligned_t total_pages;
     uint64_aligned_t free_pages;
     uint64_aligned_t scrub_pages;
Signature · 10 lines
2.53.0



--
Julian Vetter | Vates Hypervisor & Kernel Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech
#10 lore raw

Re: [PATCH v4 5/6] xen/arm: report clock_frequency via sysctl physinfo, not createdomain

Quoted 19 lines · Julian Vetter

The xen_arch_domainconfig.clock_frequency value is populated in domain_vtimer_init() during XEN_DOMCTL_createdomain from the global timer_dt_clock_frequency, which comes from the host's DT timer node and has nothing to do with the domain being created. Like now removed GIC_NATIVE resolution, this is a host-wide system property being smuggled out through a domain-creation IN struct.

Expose it instead as a new arch_clock_frequency_hz field in XEN_SYSCTL_physinfo, populated via arch_do_physinfo(), and mirroring how the GIC capability bits were already moved there.

Rather than making the field dependant on DT boot, make Xen always report the timer frequency, either via the DT "clock-frequency" node, or directly via CNTFRQ_EL0. preinit_xen_time() already computes cpu_khz for every boot path. The renamed timer_clock_frequency_hz now captures whichever of the two produced that value, in full Hz precision, instead of only recording the DT case. So, ACPI guests get a real value too, allowing to drop the special case.

Quoted 6 lines

Although the CNTFRQ_EL0 register is 64 bits wide, and some current timer implementations run at 1GHz, a 32bit value is sufficient to store the timer value, because it only mirrors the DT 'clock-frequency' property, which the bindings define as a single 32-bit cell. The XEN_DOMCTL_INTERFACE_VERSION doesn't need to be bumped, because only a previously zero'ed / ignored field is now used.

I think you meant sysctl here where you're re-using the padding. For domctl, you remove the member in the middle so the following fields have changed offsets and the struct changes size from 16 to 12B. I think the version should be bumped.

Quoted 59 lines

The xen_arch_domainconfig parameter passed to domain_vtimer_init() is no longer needed, so drop that parameter entirely. libxl now fetches the frequency via libxl_get_physinfo() in libxl__arch_domain_save_config() instead of reading it back out of the createdomain reply. The OCaml xen_arch_domainconfig mirror drops the field too.

---
Changes in v4:
- Rename timer_dt_clock_frequency to timer_clock_frequency_hz
- Always populate the frequency, and record it on the CNTFRQ_EL0 path in preinit_xen_time(), instead of only when booting via DT
- ACPI guests now get a real frequency value instead of 0

---
 tools/libs/light/libxl.c          |  1 +
 tools/libs/light/libxl_arm.c      | 13 ++++++++++++-
 tools/libs/light/libxl_types.idl  |  1 +
 tools/ocaml/libs/xc/xenctrl.ml    |  1 -
 tools/ocaml/libs/xc/xenctrl.mli   |  1 -
 xen/arch/arm/domain.c             |  2 +-
 xen/arch/arm/include/asm/time.h   |  6 +++---
 xen/arch/arm/include/asm/vtimer.h |  3 +--
 xen/arch/arm/sysctl.c             |  3 +++
 xen/arch/arm/time.c               |  7 ++++---
 xen/arch/arm/vtimer.c             |  4 +---
 xen/include/public/arch-arm.h     | 16 +---------------
 xen/include/public/sysctl.h       | 12 +++++++++++-
 13 files changed, 39 insertions(+), 31 deletions(-)
tools/libs/light/libxl.c +1 -0
index a1fe16274d..ec7e6d3f65 100644
@@ -410,6 +410,7 @@ int libxl_get_physinfo(libxl_ctx *ctx, libxl_physinfo *physinfo)
     physinfo->cap_gnttab_v2 =
         !!(xcphysinfo.capabilities & XEN_SYSCTL_PHYSCAP_gnttab_v2);
     physinfo->arch_capabilities = xcphysinfo.arch_capabilities;
+    physinfo->arch_clock_frequency_hz = xcphysinfo.arch_clock_frequency_hz;
 
     GC_FREE;
     return 0;
tools/libs/light/libxl_arm.c +3 -0
index 926d651857..cf441f0737 100644
@@ -252,6 +252,9 @@ int libxl__arch_domain_save_config(libxl__gc *gc,
                                    libxl__domain_build_state *state,
                                    const struct xen_domctl_createdomain *config)
 {
+    libxl_physinfo info;
+    int rc;
+
     switch (config->arch.gic_version) {
     case XEN_DOMCTL_CONFIG_GIC_V2:
         d_config->b_info.arch_arm.gic_version = LIBXL_GIC_VERSION_V2;
@@ -264,7 +267,15 @@ int libxl__arch_domain_save_config(libxl__gc *gc,
         return ERROR_FAIL;
     }

tools/libs/light/libxl_arm.c int libxl__arch_domain_save_config(libxl__gc *gc, · line 277

Quoted 10 lines
tools/libs/light/libxl_arm.c +8 -1
 
-    state->clock_frequency = config->arch.clock_frequency;
+    libxl_physinfo_init(&info);
+    rc = libxl_get_physinfo(CTX, &info);
+    if (rc) {
+        LOG(ERROR, "failed to get physinfo");
+        libxl_physinfo_dispose(&info);
+        return ERROR_FAIL;
+    }
+    state->clock_frequency = info.arch_clock_frequency_hz;

make_timer_node() will now put clock-frequency into every libxl guest DT which is not correct. I'm not sure how you want to signal to the toolstack that the frequency comes from the DT property and not from the CNTFRQ.

Quoted 201 lines
tools/libs/light/libxl_arm.c +1 -0
+    libxl_physinfo_dispose(&info);
 
     return 0;
 }
tools/libs/light/libxl_types.idl +1 -0
index a7893460f0..e24a3253f8 100644
@@ -1201,6 +1201,7 @@ libxl_physinfo = Struct("physinfo", [
     ("cap_gnttab_v1", bool),
     ("cap_gnttab_v2", bool),
     ("arch_capabilities", uint32),
+    ("arch_clock_frequency_hz", uint32), # ARM only
     ], dir=DIR_OUT)
 
 libxl_connectorinfo = Struct("connectorinfo", [
tools/ocaml/libs/xc/xenctrl.ml +0 -1
index 147afa62c2..582897af6d 100644
@@ -32,7 +32,6 @@ type xen_arm_arch_domainconfig =
   {
     gic_version: int;
     nr_spis: int;
-    clock_frequency: int32;
   }
 
 type x86_arch_emulation_flags =
tools/ocaml/libs/xc/xenctrl.mli +0 -1
index 9fccb2c2c2..9414b87164 100644
@@ -26,7 +26,6 @@ type vcpuinfo = {
 type xen_arm_arch_domainconfig = {
   gic_version: int;
   nr_spis: int;
-  clock_frequency: int32;
 }
 
 type x86_arch_emulation_flags =
xen/arch/arm/domain.c +1 -1
index 94f8f077e9..96dc65270b 100644
@@ -710,7 +710,7 @@ int arch_domain_create(struct domain *d,
     if ( (rc = domain_vgic_init(d, config->arch.nr_spis)) != 0 )
         goto fail;
 
-    if ( (rc = domain_vtimer_init(d, &config->arch)) != 0 )
+    if ( (rc = domain_vtimer_init(d)) != 0 )
         goto fail;
 
     if ( (rc = tee_domain_init(d, config->arch.tee_type)) != 0 )
xen/arch/arm/include/asm/time.h +3 -3
index c194dbb9f5..07091648a3 100644
@@ -87,10 +87,10 @@ enum timer_ppi
 };
 
 /*
- * Value of "clock-frequency" in the DT timer node if present.
- * 0 means the property doesn't exist.
+ * The timer frequency, in Hz, that Xen ended up using: either the DT
+ * "clock-frequency" override, or a direct CNTFRQ_EL0 read.
  */
-extern uint32_t timer_dt_clock_frequency;
+extern uint32_t timer_clock_frequency_hz;
 
 /* Get one of the timer IRQ number */
 unsigned int timer_get_irq(enum timer_ppi ppi);
xen/arch/arm/include/asm/vtimer.h +1 -2
index 9d4fb4c6e8..6bbfcf4e69 100644
@@ -20,8 +20,7 @@
 #ifndef __ARCH_ARM_VTIMER_H__
 #define __ARCH_ARM_VTIMER_H__
 
-extern int domain_vtimer_init(struct domain *d,
-                              struct xen_arch_domainconfig *config);
+extern int domain_vtimer_init(struct domain *d);
 extern int vcpu_vtimer_init(struct vcpu *v);
 extern bool vtimer_emulate(struct cpu_user_regs *regs, union hsr hsr);
 extern void virt_timer_save(struct vcpu *v);
xen/arch/arm/sysctl.c +3 -0
index 8411deb7e2..789ad78a14 100644
@@ -15,6 +15,7 @@
 
 #include <asm/arm64/sve.h>
 #include <asm/gic.h>
+#include <asm/time.h>
 #include <asm/vgic.h>
 
 #include <public/sysctl.h>
@@ -26,6 +27,8 @@ void arch_do_physinfo(struct xen_sysctl_physinfo *pi)
     pi->arch_capabilities |= MASK_INSR(sve_encode_vl(get_sys_vl_len()),
                                        XEN_SYSCTL_PHYSCAP_ARM_SVE_MASK);
 
+    pi->arch_clock_frequency_hz = timer_clock_frequency_hz;
+
     /*
      * The GIC version(s) we're happy creating guests with. Right now for
      * simplicity it is tied to the active hardware version, but this will
xen/arch/arm/time.c +4 -3
index 6955b2788f..bcbd038cb2 100644
@@ -35,7 +35,7 @@ uint64_t __read_mostly boot_count;
  * register-mapped time source in the SoC. */
 unsigned long __read_mostly cpu_khz;  /* CPU clock frequency in kHz. */
 
-uint32_t __read_mostly timer_dt_clock_frequency;
+uint32_t __read_mostly timer_clock_frequency_hz;
 
 static unsigned int timer_irq[MAX_TIMER_PPI];
 
@@ -120,7 +120,7 @@ static void __init preinit_dt_xen_time(void)
     {
         cpu_khz = DIV_ROUND(rate, 1000);
         validate_timer_frequency();
-        timer_dt_clock_frequency = rate;
+        timer_clock_frequency_hz = rate;
     }
 }
 
@@ -136,7 +136,8 @@ void __init preinit_xen_time(void)
 
     if ( !cpu_khz )
     {
-        cpu_khz = DIV_ROUND(READ_SYSREG(CNTFRQ_EL0) & CNTFRQ_MASK, 1000);
+        timer_clock_frequency_hz = READ_SYSREG(CNTFRQ_EL0) & CNTFRQ_MASK;
+        cpu_khz = DIV_ROUND(timer_clock_frequency_hz, 1000);
         validate_timer_frequency();
     }
 
xen/arch/arm/vtimer.c +1 -3
index 2e85ff2b6e..18f5676158 100644
@@ -52,7 +52,7 @@ static void virt_timer_expired(void *data)
     perfc_incr(vtimer_virt_inject);
 }
 
-int domain_vtimer_init(struct domain *d, struct xen_arch_domainconfig *config)
+int domain_vtimer_init(struct domain *d)
 {
     d->arch.virt_timer_base.offset = get_cycles();
     d->arch.virt_timer_base.nanoseconds =
@@ -60,8 +60,6 @@ int domain_vtimer_init(struct domain *d, struct xen_arch_domainconfig *config)
     d->time_offset.seconds = d->arch.virt_timer_base.nanoseconds;
     do_div(d->time_offset.seconds, 1000000000);
 
-    config->clock_frequency = timer_dt_clock_frequency;
-
     /*
      * Per the ACPI specification, providing a secure EL1 timer
      * interrupt is optional and will be ignored by non-secure OS.
xen/include/public/arch-arm.h +1 -15
index e95fa33eb7..c7118e7884 100644
@@ -335,7 +335,7 @@ DEFINE_XEN_GUEST_HANDLE(vcpu_guest_context_t);
 #define XEN_DOMCTL_CONFIG_ARM_V8R_EL1_MSA_VMSA    2
 
 struct xen_arch_domainconfig {
-    /* IN/OUT */
+    /* IN */
     uint8_t gic_version;
     /* IN - Contains SVE vector length divided by 128 */
     uint8_t sve_vl;
@@ -343,20 +343,6 @@ struct xen_arch_domainconfig {
     uint16_t tee_type;
     /* IN */
     uint32_t nr_spis;
-    /*
-     * OUT
-     * Based on the property clock-frequency in the DT timer node.
-     * The property may be present when the bootloader/firmware doesn't
-     * set correctly CNTFRQ which hold the timer frequency.
-     *
-     * As it's not possible to trap this register, we have to replicate
-     * the value in the guest DT.
-     *
-     * = 0 => property not present
-     * > 0 => Value of the property
-     *
-     */
-    uint32_t clock_frequency;
     /* IN */
     uint8_t arm_sci_type;
     /* IN */
xen/include/public/sysctl.h +0 -0
index d20ebf3644..04cb1f3bdc 100644
@@ -120,7 +120,17 @@ struct xen_sysctl_physinfo {
     uint32_t cpu_khz;
     uint32_t capabilities;/* XEN_SYSCTL_PHYSCAP_??? */

xen/include/public/sysctl.h struct xen_sysctl_physinfo { · line 130

Quoted 10 lines
xen/include/public/sysctl.h +8 -1
     uint32_t arch_capabilities;/* XEN_SYSCTL_PHYSCAP_{X86,ARM,...}_??? */
-    uint32_t pad;
+    /*
+     * ARM only. The timer frequency, in Hz, that Xen is using. Either
+     * "clock-frequency" property from DT timer node or CNTFRQ_EL0 value.
+     *
+     * As it's not possible to trap this register, we have to replicate the
+     * value in the guest DT.
+     *
+     * = 0 => non-ARM, or the frequency could not be determined.

I'm struggle to understand the second condition. On Arm we validate the frequency to be at least 1kHZ, so this field cannot be 0 on Arm.

xen/include/public/sysctl.h struct xen_sysctl_physinfo { · line 136

Quoted 6 lines
xen/include/public/sysctl.h +3 -0
+     * > 0 => The frequency, in Hz.
+     */
+    uint32_t arch_clock_frequency_hz;
     uint64_aligned_t total_pages;
     uint64_aligned_t free_pages;
     uint64_aligned_t scrub_pages;

~Michal

#11 lore raw

PATCH v4 6/6 xen: make config argument const

In reply to a message not in the archive

arch_sanitise_domain_config() validates the configuration requested by the toolstack, and should not fill anything in. The config struct passed to createdomain is supposed to be pure input. ARM used to abuse this (GIC_NATIVE resolution, now removed) to smuggle output back to the toolstack. Making the parameter const stops that type of abuse from happening on any architecture.

The x86 implementation turned out to have its own instance of the same issue. It set XEN_DOMCTL_CDF_oos_off into config->flags for non-HVM guests. Since The sanitisation runs before the function domain_create() copies config->flags into d->options, this relied on mutating the toolstack's config to take effect. Move the default onto d->options directly in arch_domain_create() (which runs after d->options is populated), where all the remaining domain options are resolved. This has the same effect and no mutation of the input config is required.

ARM, PPC and RISC-V need no equivalent change, Their implementations were already read-only.

---
Changes in v4:
- Simplify arch_sanitise_domain_config() comment

---
 xen/arch/arm/domain.c                   |  2 +-
 xen/arch/arm/firmware/sci.c             |  2 +-
 xen/arch/arm/firmware/scmi-smc.c        |  2 +-
 xen/arch/arm/include/asm/firmware/sci.h |  6 +++---
 xen/arch/ppc/stubs.c                    |  2 +-
 xen/arch/riscv/domain.c                 |  2 +-
 xen/arch/x86/domain.c                   | 16 ++++++++--------
 xen/include/xen/sched.h                 |  5 ++---
 8 files changed, 18 insertions(+), 19 deletions(-)
xen/arch/arm/domain.c +1 -1
index 96dc65270b..71fff7692d 100644
@@ -557,7 +557,7 @@ static bool v8r_el1_msa_domain_sanitise_config(
     }
 }
 
-int arch_sanitise_domain_config(struct xen_domctl_createdomain *config)
+int arch_sanitise_domain_config(const struct xen_domctl_createdomain *config)
 {
     unsigned int max_vcpus;
     unsigned int flags_required = (XEN_DOMCTL_CDF_hvm | XEN_DOMCTL_CDF_hap);
xen/arch/arm/firmware/sci.c +1 -1
index aa93cda7f0..f73ed06092 100644
@@ -45,7 +45,7 @@ int sci_domain_init(struct domain *d, struct xen_domctl_createdomain *config)
     return cur_mediator->domain_init(d, config);
 }
 
-int sci_domain_sanitise_config(struct xen_domctl_createdomain *config)
+int sci_domain_sanitise_config(const struct xen_domctl_createdomain *config)
 {
     if ( !cur_mediator )
         return 0;
xen/arch/arm/firmware/scmi-smc.c +1 -1
index 0835ddeeec..a973679eaf 100644
@@ -82,7 +82,7 @@ static bool scmi_handle_smc(struct cpu_user_regs *regs)
 }
 
 static int
-scmi_smc_domain_sanitise_config(struct xen_domctl_createdomain *config)
+scmi_smc_domain_sanitise_config(const struct xen_domctl_createdomain *config)
 {
     if ( config->arch.arm_sci_type != XEN_DOMCTL_CONFIG_ARM_SCI_NONE &&
          config->arch.arm_sci_type != XEN_DOMCTL_CONFIG_ARM_SCI_SCMI_SMC )
xen/arch/arm/include/asm/firmware/sci.h +3 -3
index 485ce211c9..1d566be8e2 100644
@@ -32,7 +32,7 @@ struct sci_mediator_ops {
      * it to sanitize domain SCI configuration parameters.
      * Optional.
      */
-    int (*domain_sanitise_config)(struct xen_domctl_createdomain *config);
+    int (*domain_sanitise_config)(const struct xen_domctl_createdomain *config);
 
     /*
      * Called during domain destruction, releases all resources, that
@@ -101,7 +101,7 @@ int sci_domain_init(struct domain *d, struct xen_domctl_createdomain *config);
  * Sanitise domain configuration parameters.
  *
  */
-int sci_domain_sanitise_config(struct xen_domctl_createdomain *config);
+int sci_domain_sanitise_config(const struct xen_domctl_createdomain *config);
 
 /*
  * Destroy SCI domain instance.
@@ -162,7 +162,7 @@ static inline int sci_domain_init(struct domain *d,
 }
 
 static inline int
-sci_domain_sanitise_config(struct xen_domctl_createdomain *config)
+sci_domain_sanitise_config(const struct xen_domctl_createdomain *config)
 {
     if ( config->arch.arm_sci_type != XEN_DOMCTL_CONFIG_ARM_SCI_NONE )
         return -EINVAL;
xen/arch/ppc/stubs.c +1 -1
index a333f06119..82a289af85 100644
@@ -162,7 +162,7 @@ void arch_vcpu_destroy(struct vcpu *v)
     BUG_ON("unimplemented");
 }
 
-int arch_sanitise_domain_config(struct xen_domctl_createdomain *config)
+int arch_sanitise_domain_config(const struct xen_domctl_createdomain *config)
 {
     BUG_ON("unimplemented");
 }
xen/arch/riscv/domain.c +1 -1
index 2819ff4e7c..e096a53cb5 100644
@@ -289,7 +289,7 @@ void sync_vcpu_execstate(struct vcpu *v)
     /* Nothing to do -- no lazy switching */
 }
 
-int arch_sanitise_domain_config(struct xen_domctl_createdomain *config)
+int arch_sanitise_domain_config(const struct xen_domctl_createdomain *config)
 {
     return 0;
 }
xen/arch/x86/domain.c +8 -8
index 4252339978..35f591ab5d 100644
@@ -590,7 +590,7 @@ void arch_vcpu_destroy(struct vcpu *v)
         ASSERT_UNREACHABLE();
 }
 
-int arch_sanitise_domain_config(struct xen_domctl_createdomain *config)
+int arch_sanitise_domain_config(const struct xen_domctl_createdomain *config)
 {
     bool hvm = config->flags & XEN_DOMCTL_CDF_hvm;
     bool hap = config->flags & XEN_DOMCTL_CDF_hap;
@@ -633,13 +633,6 @@ int arch_sanitise_domain_config(struct xen_domctl_createdomain *config)
         return -EINVAL;
     }
 
-    if ( !hvm )
-        /*
-         * It is only meaningful for XEN_DOMCTL_CDF_oos_off to be clear
-         * for HVM guests.
-         */
-        config->flags |= XEN_DOMCTL_CDF_oos_off;
-
     if ( nested_virt && !hvm_nested_virt_supported() )
     {
         dprintk(XENLOG_INFO, "Nested virt requested but not available\n");
@@ -833,6 +826,13 @@ int arch_domain_create(struct domain *d,
 
     spin_lock_init(&d->arch.e820_lock);
 
+    /*
+     * It is only meaningful for XEN_DOMCTL_CDF_oos_off to be clear for HVM
+     * guests.
+     */
+    if ( !is_hvm_domain(d) )
+        d->options |= XEN_DOMCTL_CDF_oos_off;
+
     if ( d->domain_id && cpu_has_amd_erratum(&boot_cpu_data, AMD_ERRATUM_121) )
     {
         if ( !opt_allow_unsafe )
xen/include/xen/sched.h +2 -3
index 011292e9f7..42a6bb545a 100644
@@ -755,10 +755,9 @@ static inline void domain_update_node_affinity(struct domain *d)
 }
 
 /*
- * To be implemented by each architecture, sanity checking the configuration
- * and filling in any appropriate defaults.
+ * To be implemented by each architecture, sanity checking the configuration.
  */
-int arch_sanitise_domain_config(struct xen_domctl_createdomain *config);
+int arch_sanitise_domain_config(const struct xen_domctl_createdomain *config);
 
 /*
  * Create a domain: the configuration is only necessary for real domain
Signature · 10 lines
2.53.0



--
Julian Vetter | Vates Hypervisor & Kernel Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech