Skip to contentSkip to conversation

SVE feature for arm guests

RFC PATCH In review Luca Fancellu 3 participants 22 messages Jan 11, 2023 – Jan 25, 2023

lore
Share & tools
lore
#0 lore raw

cover SVE feature for arm guests

This serie is introducing the possibility for Dom0 and DomU guests to use sve/sve2 instructions.

SVE feature introduces new instruction and registers to improve performances on floating point operations.

The SVE feature is advertised using the ID_AA64PFR0_EL1 register, SVE field, and when available the ID_AA64ZFR0_EL1 register provides additional information about the implemented version and other SVE feature.

New registers added by the SVE feature are Z0-Z31, P0-P15, FFR, ZCR_ELx.

Z0-Z31 are scalable vector register whose size is implementation defined and goes from 128 bits to maximum 2048, the term vector length will be used to refer to this quantity.
P0-P15 are predicate registers and the size is the vector length divided by 8, same size is the FFR (First Fault Register).
ZCR_ELx is a register that can control and restrict the maximum vector length used by the <x> exception level and all the lower exception levels, so for example EL3 can restrict the vector length usable by EL3,2,1,0.

The platform has a maximum implemented vector length, so for every value written in ZCR register, if this value is above the implemented length, then the lower value will be used. The RDVL instruction can be used to check what vector length is the HW using after setting ZCR.

For an SVE guest, the V0-V31 registers are part of the Z0-Z31, so there is no need to save them separately, saving Z0-Z31 will save implicitly also V0-V31.

SVE usage can be trapped using a flag in CPTR_EL2, hence in this serie the register is added to the domain state, to be able to trap only the guests that are not allowed to use SVE.

This serie is introducing a command line parameter to enable Dom0 to use SVE and to set its maximum vector length that by default is 0 which means the guest is not allowed to use SVE. Values from 128 to 2048 mean the guest can use SVE with the selected value used as maximum allowed vector length (which could be lower if the implemented one is lower).
For DomUs, an XL parameter with the same way of use is introduced and a dom0less DTB binding is created.

The context switch is the most critical part because there can be big registers to be saved, in this serie an easy approach is used and the context is saved/restored every time for the guests that are allowed to use SVE.

Luca Fancellu (8):
xen/arm: enable SVE extension for Xen xen/arm: add sve_vl_bits field to domain xen/arm: Expose SVE feature to the guest xen/arm: add SVE exception class handling arm/sve: save/restore SVE context switch xen/arm: enable Dom0 to use SVE feature xen/tools: add sve parameter in XL configuration xen/arm: add sve property for dom0less domUs

 docs/man/xl.cfg.5.pod.in                 |  11 ++
 docs/misc/arm/device-tree/booting.txt    |   7 +
 docs/misc/xen-command-line.pandoc        |  12 ++
 tools/golang/xenlight/helpers.gen.go     |   2 +
 tools/golang/xenlight/types.gen.go       |   1 +
 tools/include/libxl.h                    |   5 +
 tools/libs/light/libxl_arm.c             |   2 +
 tools/libs/light/libxl_types.idl         |   1 +
 tools/xl/xl_parse.c                      |  10 ++
 xen/arch/arm/Kconfig                     |   3 +-
 xen/arch/arm/arm64/Makefile              |   1 +
 xen/arch/arm/arm64/cpufeature.c          |   7 +-
 xen/arch/arm/arm64/sve.c                 | 104 +++++++++++++
 xen/arch/arm/arm64/sve_asm.S             | 189 +++++++++++++++++++++++
 xen/arch/arm/arm64/vfp.c                 |  79 ++++++----
 xen/arch/arm/arm64/vsysreg.c             |  39 ++++-
 xen/arch/arm/cpufeature.c                |   6 +-
 xen/arch/arm/domain.c                    |  61 ++++++++
 xen/arch/arm/domain_build.c              |  11 ++
 xen/arch/arm/include/asm/arm64/sve.h     |  72 +++++++++
 xen/arch/arm/include/asm/arm64/sysregs.h |   4 +
 xen/arch/arm/include/asm/arm64/vfp.h     |  10 ++
 xen/arch/arm/include/asm/cpufeature.h    |  14 ++
 xen/arch/arm/include/asm/domain.h        |   8 +
 xen/arch/arm/include/asm/processor.h     |   3 +
 xen/arch/arm/setup.c                     |   5 +-
 xen/arch/arm/traps.c                     |  46 ++++--
 xen/include/public/arch-arm.h            |   2 +
 xen/include/public/domctl.h              |   2 +-
 29 files changed, 661 insertions(+), 56 deletions(-)
 create mode 100644 xen/arch/arm/arm64/sve.c
 create mode 100644 xen/arch/arm/arm64/sve_asm.S
 create mode 100644 xen/arch/arm/include/asm/arm64/sve.h
Signature · 1 line
2.17.1
#1 lore raw

RFC PATCH 1/8 xen/arm: enable SVE extension for Xen

Enable Xen to handle the SVE extension, add code in cpufeature module to handle ZCR SVE register, disable trapping SVE feature on system boot, it will be restored later on vcpu creation and running. While there, correct coding style for the comment on coprocessor trapping.

Change the KConfig entry to make ARM64_SVE symbol selectable, by default it will be not selected.

Create sve module and sve_asm.S that contains assembly routines for the SVE feature, this code is inspired from linux and it uses instruction encoding to be compatible with compilers that does not support SVE.

---
 xen/arch/arm/Kconfig                     |  3 +-
 xen/arch/arm/arm64/Makefile              |  1 +
 xen/arch/arm/arm64/cpufeature.c          |  7 ++--
 xen/arch/arm/arm64/sve.c                 | 38 +++++++++++++++++++
 xen/arch/arm/arm64/sve_asm.S             | 48 ++++++++++++++++++++++++
 xen/arch/arm/cpufeature.c                |  6 ++-
 xen/arch/arm/domain.c                    |  4 ++
 xen/arch/arm/include/asm/arm64/sve.h     | 43 +++++++++++++++++++++
 xen/arch/arm/include/asm/arm64/sysregs.h |  1 +
 xen/arch/arm/include/asm/cpufeature.h    | 14 +++++++
 xen/arch/arm/include/asm/domain.h        |  1 +
 xen/arch/arm/include/asm/processor.h     |  2 +
 xen/arch/arm/setup.c                     |  5 ++-
 xen/arch/arm/traps.c                     | 34 ++++++++++++-----
 14 files changed, 188 insertions(+), 19 deletions(-)
 create mode 100644 xen/arch/arm/arm64/sve.c
 create mode 100644 xen/arch/arm/arm64/sve_asm.S
 create mode 100644 xen/arch/arm/include/asm/arm64/sve.h
xen/arch/arm/Kconfig +1 -2
index 239d3aed3c7f..2a5151f3c718 100644
@@ -112,11 +112,10 @@ config ARM64_PTR_AUTH
 	  This feature is not supported in Xen.
 
 config ARM64_SVE
-	def_bool n
+	bool "Enable Scalar Vector Extension support" if EXPERT
 	depends on ARM_64
 	help
 	  Scalar Vector Extension support.
-	  This feature is not supported in Xen.
 
 config ARM64_MTE
 	def_bool n
xen/arch/arm/arm64/Makefile +1 -0
index 6d507da0d44d..1d59c3b0ec89 100644
@@ -12,6 +12,7 @@ obj-y += insn.o
 obj-$(CONFIG_LIVEPATCH) += livepatch.o
 obj-y += smc.o
 obj-y += smpboot.o
+obj-$(CONFIG_ARM64_SVE) += sve.o sve_asm.o
 obj-y += traps.o
 obj-y += vfp.o
 obj-y += vsysreg.o
xen/arch/arm/arm64/cpufeature.c +3 -4
index d9039d37b2d1..b4656ff4d80f 100644
@@ -455,15 +455,11 @@ static const struct arm64_ftr_bits ftr_id_dfr1[] = {
 	ARM64_FTR_END,
 };
 
-#if 0
-/* TODO: use this to sanitize SVE once we support it */
-
 static const struct arm64_ftr_bits ftr_zcr[] = {
 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE,
 		ZCR_ELx_LEN_SHIFT, ZCR_ELx_LEN_SIZE, 0),	/* LEN */
 	ARM64_FTR_END,
 };
-#endif
 
 /*
  * Common ftr bits for a 32bit register with all hidden, strict
@@ -603,6 +599,9 @@ void update_system_features(const struct cpuinfo_arm *new)
 
 	SANITIZE_ID_REG(zfr64, 0, aa64zfr0);
 
+	if ( cpu_has_sve )
+		SANITIZE_REG(zcr64, 0, zcr);
+
 	/*
 	 * Comment from Linux:
 	 * Userspace may perform DC ZVA instructions. Mismatched block sizes
xen/arch/arm/arm64/sve.c new file +38 -0
new file mode 100644
index 000000000000..326389278292
@@ -0,0 +1,38 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Arm SVE feature code
+ *
+ * Copyright (C) 2022 ARM Ltd.
+ */
+
+#include <xen/types.h>
+#include <asm/arm64/sve.h>
+#include <asm/arm64/sysregs.h>
+
+extern unsigned int sve_get_hw_vl(void);
+
+register_t compute_max_zcr(void)
+{
+    register_t zcr = vl_to_zcr(SVE_VL_MAX_BITS);
+    unsigned int hw_vl;
+
+    /*
+     * Set the maximum SVE vector length, doing that we will know the VL
+     * supported by the platform, calling sve_get_hw_vl()
+     */
+    WRITE_SYSREG(zcr, ZCR_EL2);
+
+    /*
+     * Read the maximum VL, which could be lower than what we imposed before,
+     * hw_vl contains VL in bytes, multiply it by 8 to use vl_to_zcr() later
+     */
+    hw_vl = sve_get_hw_vl() * 8U;
+
+    return vl_to_zcr(hw_vl);
+}
+
+/* Takes a vector length in bits and returns the ZCR_ELx encoding */
+register_t vl_to_zcr(uint16_t vl)
+{
+    return ((vl / SVE_VL_MULTIPLE_VAL) - 1U) & ZCR_ELx_LEN_MASK;
+}
xen/arch/arm/arm64/sve_asm.S new file +48 -0

48 lines not on this page · raw message

xen/arch/arm/cpufeature.c +5 -1
index c4ec38bb2554..83b84368f6d5 100644
@@ -9,6 +9,7 @@
 #include <xen/init.h>
 #include <xen/smp.h>
 #include <xen/stop_machine.h>
+#include <asm/arm64/sve.h>
 #include <asm/cpufeature.h>
 
 DECLARE_BITMAP(cpu_hwcaps, ARM_NCAPS);
@@ -143,6 +144,9 @@ void identify_cpu(struct cpuinfo_arm *c)
 
     c->zfr64.bits[0] = READ_SYSREG(ID_AA64ZFR0_EL1);
 
+    if ( cpu_has_sve )
+        c->zcr64.bits[0] = compute_max_zcr();
+
     c->dczid.bits[0] = READ_SYSREG(DCZID_EL0);
 
     c->ctr.bits[0] = READ_SYSREG(CTR_EL0);
@@ -199,7 +203,7 @@ static int __init create_guest_cpuinfo(void)
     guest_cpuinfo.pfr64.mpam = 0;
     guest_cpuinfo.pfr64.mpam_frac = 0;
 
-    /* Hide SVE as Xen does not support it */
+    /* Hide SVE by default to the guests */
     guest_cpuinfo.pfr64.sve = 0;
     guest_cpuinfo.zfr64.bits[0] = 0;
 
xen/arch/arm/domain.c +4 -0
index 99577adb6c69..8ea3843ea8e8 100644
@@ -181,6 +181,8 @@ static void ctxt_switch_to(struct vcpu *n)
     /* VGIC */
     gic_restore_state(n);
 
+    WRITE_SYSREG(n->arch.cptr_el2, CPTR_EL2);
+
     /* VFP */
     vfp_restore_state(n);
 
@@ -548,6 +550,8 @@ int arch_vcpu_create(struct vcpu *v)
 
     v->arch.vmpidr = MPIDR_SMP | vcpuid_to_vaffinity(v->vcpu_id);
 
+    v->arch.cptr_el2 = get_default_cptr_flags();
+
     v->arch.hcr_el2 = get_default_hcr_flags();
 
     v->arch.mdcr_el2 = HDCR_TDRA | HDCR_TDOSA | HDCR_TDA;
xen/arch/arm/include/asm/arm64/sve.h new file +43 -0

43 lines not on this page · raw message

xen/arch/arm/include/asm/arm64/sysregs.h +1 -0
index 463899951414..4cabb9eb4d5e 100644
@@ -24,6 +24,7 @@
 #define ICH_EISR_EL2              S3_4_C12_C11_3
 #define ICH_ELSR_EL2              S3_4_C12_C11_5
 #define ICH_VMCR_EL2              S3_4_C12_C11_7
+#define ZCR_EL2                   S3_4_C1_C2_0
 
 #define __LR0_EL2(x)              S3_4_C12_C12_ ## x
 #define __LR8_EL2(x)              S3_4_C12_C13_ ## x
xen/arch/arm/include/asm/cpufeature.h +14 -0
index c62cf6293fd6..6d703e051906 100644
@@ -32,6 +32,12 @@
 #define cpu_has_thumbee   (boot_cpu_feature32(thumbee) == 1)
 #define cpu_has_aarch32   (cpu_has_arm || cpu_has_thumb)
 
+#ifdef CONFIG_ARM64_SVE
+#define cpu_has_sve       (boot_cpu_feature64(sve) == 1)
+#else
+#define cpu_has_sve       (0)
+#endif
+
 #ifdef CONFIG_ARM_32
 #define cpu_has_gicv3     (boot_cpu_feature32(gic) >= 1)
 #define cpu_has_gentimer  (boot_cpu_feature32(gentimer) == 1)
@@ -323,6 +329,14 @@ struct cpuinfo_arm {
         };
     } isa64;
 
+    union {
+        register_t bits[1];
+        struct {
+            unsigned long len:4;
+            unsigned long __res0:60;
+        };
+    } zcr64;
+
     struct {
         register_t bits[1];
     } zfr64;
xen/arch/arm/include/asm/domain.h +1 -0
index 0e310601e846..42eb5df320a7 100644
@@ -190,6 +190,7 @@ struct arch_vcpu
     register_t tpidrro_el0;
 
     /* HYP configuration */
+    register_t cptr_el2;
     register_t hcr_el2;
     register_t mdcr_el2;
 
xen/arch/arm/include/asm/processor.h +2 -0
index 1dd81d7d528f..0e38926b94db 100644
@@ -583,6 +583,8 @@ void do_trap_guest_serror(struct cpu_user_regs *regs);
 
 register_t get_default_hcr_flags(void);
 
+register_t get_default_cptr_flags(void);
+
 /*
  * Synchronize SError unless the feature is selected.
  * This is relying on the SErrors are currently unmasked.
xen/arch/arm/setup.c +3 -2
index 1f26f67b90e3..5459cc4f5e62 100644
@@ -135,10 +135,11 @@ static void __init processor_id(void)
            cpu_has_el2_32 ? "64+32" : cpu_has_el2_64 ? "64" : "No",
            cpu_has_el1_32 ? "64+32" : cpu_has_el1_64 ? "64" : "No",
            cpu_has_el0_32 ? "64+32" : cpu_has_el0_64 ? "64" : "No");
-    printk("    Extensions:%s%s%s\n",
+    printk("    Extensions:%s%s%s%s\n",
            cpu_has_fp ? " FloatingPoint" : "",
            cpu_has_simd ? " AdvancedSIMD" : "",
-           cpu_has_gicv3 ? " GICv3-SysReg" : "");
+           cpu_has_gicv3 ? " GICv3-SysReg" : "",
+           cpu_has_sve ? " SVE" : "");
 
     /* Warn user if we find unknown floating-point features */
     if ( cpu_has_fp && (boot_cpu_feature64(fp) >= 2) )
xen/arch/arm/traps.c +24 -10
index 061c92acbd68..45163fd3afb0 100644
@@ -93,6 +93,21 @@ register_t get_default_hcr_flags(void)
              HCR_TID3|HCR_TSC|HCR_TAC|HCR_SWIO|HCR_TIDCP|HCR_FB|HCR_TSW);
 }
 
+register_t get_default_cptr_flags(void)
+{
+    /*
+     * Trap all coprocessor registers (0-13) except cp10 and
+     * cp11 for VFP.
+     *
+     * /!\ All coprocessors except cp10 and cp11 cannot be used in Xen.
+     *
+     * On ARM64 the TCPx bits which we set here (0..9,12,13) are all
+     * RES1, i.e. they would trap whether we did this write or not.
+     */
+    return  ((HCPTR_CP_MASK & ~(HCPTR_CP(10) | HCPTR_CP(11))) |
+             HCPTR_TTA | HCPTR_TAM);
+}
+
 static enum {
     SERRORS_DIVERSE,
     SERRORS_PANIC,
@@ -122,6 +137,7 @@ __initcall(update_serrors_cpu_caps);
 
 void init_traps(void)
 {
+    register_t cptr_bits = get_default_cptr_flags();
     /*
      * Setup Hyp vector base. Note they might get updated with the
      * branch predictor hardening.
@@ -135,17 +151,15 @@ void init_traps(void)
     /* Trap CP15 c15 used for implementation defined registers */
     WRITE_SYSREG(HSTR_T(15), HSTR_EL2);
 
-    /* Trap all coprocessor registers (0-13) except cp10 and
-     * cp11 for VFP.
-     *
-     * /!\ All coprocessors except cp10 and cp11 cannot be used in Xen.
-     *
-     * On ARM64 the TCPx bits which we set here (0..9,12,13) are all
-     * RES1, i.e. they would trap whether we did this write or not.
+#ifdef CONFIG_ARM64_SVE
+    /*
+     * Don't trap SVE now, Xen might need to access ZCR reg in cpufeature code,
+     * trapping again or not will be handled on vcpu creation/scheduling later
      */
-    WRITE_SYSREG((HCPTR_CP_MASK & ~(HCPTR_CP(10) | HCPTR_CP(11))) |
-                 HCPTR_TTA | HCPTR_TAM,
-                 CPTR_EL2);
+    cptr_bits &= ~HCPTR_CP(8);
+#endif
+
+    WRITE_SYSREG(cptr_bits, CPTR_EL2);
 
     /*
      * Configure HCR_EL2 with the bare minimum to run Xen until a guest
Signature · 1 line
2.17.1
#2 lore raw

Re: [RFC PATCH 1/8] xen/arm: enable SVE extension for Xen

Hi Luca,

As this is an RFC, I will be mostly making general comments.

xen/arch/arm/domain.c static void ctxt_switch_to(struct vcpu *n) · line 184

Quoted 9 lines · Luca Fancellu
xen/arch/arm/domain.c +1 -0
index 99577adb6c69..8ea3843ea8e8 100644
@@ -181,6 +181,8 @@ static void ctxt_switch_to(struct vcpu *n)
      /* VGIC */
      gic_restore_state(n);
 
+    WRITE_SYSREG(n->arch.cptr_el2, CPTR_EL2);

Shouldn't this need an isb() afterwards do ensure that any previously trapped will be accessible?

[...]

Quoted 14 lines
xen/arch/arm/domain.c +1 -2
@@ -122,6 +137,7 @@ __initcall(update_serrors_cpu_caps);
 
  void init_traps(void)
  {
+    register_t cptr_bits = get_default_cptr_flags();
      /*
       * Setup Hyp vector base. Note they might get updated with the
       * branch predictor hardening.
@@ -135,17 +151,15 @@ void init_traps(void)
      /* Trap CP15 c15 used for implementation defined registers */
      WRITE_SYSREG(HSTR_T(15), HSTR_EL2);
 
-    /* Trap all coprocessor registers (0-13) except cp10 and
-     * cp11 for VFP.

xen/arch/arm/domain.c void init_traps(void) · line 158

Quoted 10 lines
xen/arch/arm/domain.c +4 -5
-     *
-     * /!\ All coprocessors except cp10 and cp11 cannot be used in Xen.
-     *
-     * On ARM64 the TCPx bits which we set here (0..9,12,13) are all
-     * RES1, i.e. they would trap whether we did this write or not.
+#ifdef CONFIG_ARM64_SVE
+    /*
+     * Don't trap SVE now, Xen might need to access ZCR reg in cpufeature code,
+     * trapping again or not will be handled on vcpu creation/scheduling later
       */

Instead of enable by default at boot, can we try to enable/disable only when this is strictly needed?

xen/arch/arm/domain.c void init_traps(void) · line 165

Quoted 10 lines
xen/arch/arm/domain.c +4 -3
-    WRITE_SYSREG((HCPTR_CP_MASK & ~(HCPTR_CP(10) | HCPTR_CP(11))) |
-                 HCPTR_TTA | HCPTR_TAM,
-                 CPTR_EL2);
+    cptr_bits &= ~HCPTR_CP(8);
+#endif
+
+    WRITE_SYSREG(cptr_bits, CPTR_EL2);
 
      /*
       * Configure HCR_EL2 with the bare minimum to run Xen until a guest

Cheers,

Signature · 1 line
Julien Grall
#3 lore raw

Re: [RFC PATCH 1/8] xen/arm: enable SVE extension for Xen

Quoted 7 lines · Julien Grall

Hi Luca,

As this is an RFC, I will be mostly making general comments.

Hi Julien,

Thank you.

Quoted 12 lines · Luca Fancellu

xen/arch/arm/domain.c static void ctxt_switch_to(struct vcpu *n) · line 183

Quoted 8 lines
xen/arch/arm/domain.c +0 -0
index 99577adb6c69..8ea3843ea8e8 100644
@@ -181,6 +181,8 @@ static void ctxt_switch_to(struct vcpu *n)
     /* VGIC */
     gic_restore_state(n);
 +    WRITE_SYSREG(n->arch.cptr_el2, CPTR_EL2);

Shouldn't this need an isb() afterwards do ensure that any previously trapped will be accessible?

Yes you are right, would it be ok for you if I move this before gic_restore_state because it inside has an isb()? This to limit the isb() usage. I could put also a comment to don’t forget it.

Otherwise I will add the barrier.

Quoted 19 lines

[...]

Quoted 16 lines
xen/arch/arm/domain.c +1 -5
@@ -122,6 +137,7 @@ __initcall(update_serrors_cpu_caps);
   void init_traps(void)
 {
+    register_t cptr_bits = get_default_cptr_flags();
     /*
      * Setup Hyp vector base. Note they might get updated with the
      * branch predictor hardening.
@@ -135,17 +151,15 @@ void init_traps(void)
     /* Trap CP15 c15 used for implementation defined registers */
     WRITE_SYSREG(HSTR_T(15), HSTR_EL2);
 -    /* Trap all coprocessor registers (0-13) except cp10 and
-     * cp11 for VFP.
-     *
-     * /!\ All coprocessors except cp10 and cp11 cannot be used in Xen.
-     *
-     * On ARM64 the TCPx bits which we set here (0..9,12,13) are all
Quoted 8 lines

xen/arch/arm/domain.c void init_traps(void) · line 158

Quoted 6 lines
xen/arch/arm/domain.c +4 -1
-     * RES1, i.e. they would trap whether we did this write or not.
+#ifdef CONFIG_ARM64_SVE
+    /*
+     * Don't trap SVE now, Xen might need to access ZCR reg in cpufeature code,
+     * trapping again or not will be handled on vcpu creation/scheduling later
      */

Instead of enable by default at boot, can we try to enable/disable only when this is strictly needed?

Yes we could un-trap inside compute_max_zcr() just before accessing SVE resources and trap it again when finished. Would it be ok for you this approach?

Quoted 14 lines

xen/arch/arm/domain.c void init_traps(void) · line 164

Quoted 10 lines
xen/arch/arm/domain.c +4 -3
-    WRITE_SYSREG((HCPTR_CP_MASK & ~(HCPTR_CP(10) | HCPTR_CP(11))) |
-                 HCPTR_TTA | HCPTR_TAM,
-                 CPTR_EL2);
+    cptr_bits &= ~HCPTR_CP(8);
+#endif
+
+    WRITE_SYSREG(cptr_bits, CPTR_EL2);
       /*
      * Configure HCR_EL2 with the bare minimum to run Xen until a guest

Cheers,

Signature · 1 line
Julien Grall
#4 lore raw

Re: [RFC PATCH 1/8] xen/arm: enable SVE extension for Xen

Hi Luca,

Quoted 25 lines · Luca Fancellu
Quoted 7 lines · Julien Grall

Hi Luca,

As this is an RFC, I will be mostly making general comments.

Hi Julien,

Thank you.

Quoted 12 lines · Luca Fancellu

xen/arch/arm/domain.c static void ctxt_switch_to(struct vcpu *n) · line 183

Quoted 8 lines
xen/arch/arm/domain.c +0 -0
index 99577adb6c69..8ea3843ea8e8 100644
@@ -181,6 +181,8 @@ static void ctxt_switch_to(struct vcpu *n)
      /* VGIC */
      gic_restore_state(n);
  +    WRITE_SYSREG(n->arch.cptr_el2, CPTR_EL2);

Shouldn't this need an isb() afterwards do ensure that any previously trapped will be accessible?

Quoted 2 lines

Yes you are right, would it be ok for you if I move this before gic_restore_state because it inside has an isb()? This to limit the isb() usage. I could put also a comment to don’t forget it.

I would rather prefer if we don't rely on gic_restore_state() to have an isb() because this could change in the future (although unlikely).

Looking at the context switch code, I think we can move the call to restore the floating register towards the end of the helper and use one of the existing isb() for our purpose.

Quoted 25 lines
Quoted 16 lines
Quoted 16 lines
xen/arch/arm/domain.c +1 -5
@@ -122,6 +137,7 @@ __initcall(update_serrors_cpu_caps);
    void init_traps(void)
  {
+    register_t cptr_bits = get_default_cptr_flags();
      /*
       * Setup Hyp vector base. Note they might get updated with the
       * branch predictor hardening.
@@ -135,17 +151,15 @@ void init_traps(void)
      /* Trap CP15 c15 used for implementation defined registers */
      WRITE_SYSREG(HSTR_T(15), HSTR_EL2);
  -    /* Trap all coprocessor registers (0-13) except cp10 and
-     * cp11 for VFP.
-     *
-     * /!\ All coprocessors except cp10 and cp11 cannot be used in Xen.
-     *
-     * On ARM64 the TCPx bits which we set here (0..9,12,13) are all
Quoted 8 lines

xen/arch/arm/domain.c void init_traps(void) · line 158

Quoted 6 lines
xen/arch/arm/domain.c +4 -1
-     * RES1, i.e. they would trap whether we did this write or not.
+#ifdef CONFIG_ARM64_SVE
+    /*
+     * Don't trap SVE now, Xen might need to access ZCR reg in cpufeature code,
+     * trapping again or not will be handled on vcpu creation/scheduling later
       */

Instead of enable by default at boot, can we try to enable/disable only when this is strictly needed?

Quoted 2 lines

Yes we could un-trap inside compute_max_zcr() just before accessing SVE resources and trap it again when finished. Would it be ok for you this approach?

Yes.

Cheers,

Signature · 1 line
Julien Grall
#5 lore raw

Re: [RFC PATCH 1/8] xen/arm: enable SVE extension for Xen

Quoted 26 lines · Julien Grall

Hi Luca,

Quoted 19 lines · Luca Fancellu
Quoted 5 lines · Julien Grall

Hi Luca,

As this is an RFC, I will be mostly making general comments.

Hi Julien,
Thank you.

Quoted 12 lines · Luca Fancellu

xen/arch/arm/domain.c static void ctxt_switch_to(struct vcpu *n) · line 183

Quoted 8 lines
xen/arch/arm/domain.c +0 -0
index 99577adb6c69..8ea3843ea8e8 100644
@@ -181,6 +181,8 @@ static void ctxt_switch_to(struct vcpu *n)
     /* VGIC */
     gic_restore_state(n);
 +    WRITE_SYSREG(n->arch.cptr_el2, CPTR_EL2);

Shouldn't this need an isb() afterwards do ensure that any previously trapped will be accessible?

Quoted 6 lines
Quoted 2 lines

Yes you are right, would it be ok for you if I move this before gic_restore_state because it inside has an isb()? This to limit the isb() usage. I could put also a comment to don’t forget it.

I would rather prefer if we don't rely on gic_restore_state() to have an isb() because this could change in the future (although unlikely).

Looking at the context switch code, I think we can move the call to restore the floating register towards the end of the helper and use one of the existing isb() for our purpose.

Sounds good to me

Quoted 35 lines
Quoted 26 lines
Quoted 18 lines
Quoted 18 lines
xen/arch/arm/domain.c +1 -5
@@ -122,6 +137,7 @@ __initcall(update_serrors_cpu_caps);
   void init_traps(void)
 {
+    register_t cptr_bits = get_default_cptr_flags();
     /*
      * Setup Hyp vector base. Note they might get updated with the
      * branch predictor hardening.
@@ -135,17 +151,15 @@ void init_traps(void)
     /* Trap CP15 c15 used for implementation defined registers */
     WRITE_SYSREG(HSTR_T(15), HSTR_EL2);
 -    /* Trap all coprocessor registers (0-13) except cp10 and
-     * cp11 for VFP.
-     *
-     * /!\ All coprocessors except cp10 and cp11 cannot be used in Xen.
-     *
-     * On ARM64 the TCPx bits which we set here (0..9,12,13) are all
Quoted 8 lines

xen/arch/arm/domain.c void init_traps(void) · line 158

Quoted 6 lines
xen/arch/arm/domain.c +4 -1
-     * RES1, i.e. they would trap whether we did this write or not.
+#ifdef CONFIG_ARM64_SVE
+    /*
+     * Don't trap SVE now, Xen might need to access ZCR reg in cpufeature code,
+     * trapping again or not will be handled on vcpu creation/scheduling later
      */

Instead of enable by default at boot, can we try to enable/disable only when this is strictly needed?

Quoted 2 lines

Yes we could un-trap inside compute_max_zcr() just before accessing SVE resources and trap it again when finished. Would it be ok for you this approach?

Yes.

Cheers,

Signature · 1 line
Julien Grall
#6 lore raw

RFC PATCH 2/8 xen/arm: add sve_vl_bits field to domain

In reply to Luca Fancellu

Add sve_vl_bits field to arch_domain and xen_arch_domainconfig structure, to allow the domain to have an information about the SVE feature and the number of SVE register bits that are allowed for this domain.

The field is used also to allow or forbid a domain to use SVE, because a value equal to zero means the guest is not allowed to use the feature.

When the guest is allowed to use SVE, the zcr_el2 register is updated on context switch to restict the domain on the allowed number of bits chosen, this value is the minimum among the chosen value and the platform supported value.

---
 xen/arch/arm/arm64/sve.c             |  9 ++++++
 xen/arch/arm/domain.c                | 45 ++++++++++++++++++++++++++++
 xen/arch/arm/include/asm/arm64/sve.h | 12 ++++++++
 xen/arch/arm/include/asm/domain.h    |  6 ++++
 xen/include/public/arch-arm.h        |  2 ++
 xen/include/public/domctl.h          |  2 +-
 6 files changed, 75 insertions(+), 1 deletion(-)
xen/arch/arm/arm64/sve.c +9 -0
index 326389278292..b7695834f4ba 100644
@@ -6,6 +6,7 @@
  */
 
 #include <xen/types.h>
+#include <asm/cpufeature.h>
 #include <asm/arm64/sve.h>
 #include <asm/arm64/sysregs.h>
 
@@ -36,3 +37,11 @@ register_t vl_to_zcr(uint16_t vl)
 {
     return ((vl / SVE_VL_MULTIPLE_VAL) - 1U) & ZCR_ELx_LEN_MASK;
 }
+
+/* Get the system sanitized value for VL in bits */
+uint16_t get_sys_vl_len(void)
+{
+    /* ZCR_ELx len field is ((len+1) * 128) = vector bits length */
+    return ((system_cpuinfo.zcr64.bits[0] & ZCR_ELx_LEN_MASK) + 1U) *
+            SVE_VL_MULTIPLE_VAL;
+}
xen/arch/arm/domain.c +45 -0

81 lines not on this page · raw message

xen/arch/arm/include/asm/arm64/sve.h +12 -0
index bd56e2f24230..f4a660e402ca 100644
@@ -13,10 +13,17 @@
 /* Vector length must be multiple of 128 */
 #define SVE_VL_MULTIPLE_VAL (128U)
 
+static inline bool is_vl_valid(uint16_t vl)
+{
+    /* SVE vector length is multiple of 128 and maximum 2048 */
+    return ((vl % SVE_VL_MULTIPLE_VAL) == 0) && (vl <= SVE_VL_MAX_BITS);
+}
+
 #ifdef CONFIG_ARM64_SVE
 
 register_t compute_max_zcr(void);
 register_t vl_to_zcr(uint16_t vl);
+uint16_t get_sys_vl_len(void);
 
 #else /* !CONFIG_ARM64_SVE */
 
@@ -30,6 +37,11 @@ static inline register_t vl_to_zcr(uint16_t vl)
     return 0;
 }
 
+static inline uint16_t get_sys_vl_len(void)
+{
+    return 0;
+}
+
 #endif
 
 #endif /* _ARM_ARM64_SVE_H */
xen/arch/arm/include/asm/domain.h +6 -0
index 42eb5df320a7..e4794a9fd2ab 100644
@@ -31,6 +31,8 @@ enum domain_type {
 
 #define is_domain_direct_mapped(d) ((d)->cdf & CDF_directmap)
 
+#define is_sve_domain(d) ((d)->arch.sve_vl_bits > 0)
+
 /*
  * Is the domain using the host memory layout?
  *
@@ -114,6 +116,9 @@ struct arch_domain
     void *tee;
 #endif
 
+    /* max SVE vector length in bits */
+    uint16_t sve_vl_bits;
+
 }  __cacheline_aligned;
 
 struct arch_vcpu
@@ -190,6 +195,7 @@ struct arch_vcpu
     register_t tpidrro_el0;
 
     /* HYP configuration */
+    register_t zcr_el2;
     register_t cptr_el2;
     register_t hcr_el2;
     register_t mdcr_el2;
xen/include/public/arch-arm.h +2 -0
index 1528ced5097a..e18a075105f0 100644
@@ -304,6 +304,8 @@ struct xen_arch_domainconfig {
     uint16_t tee_type;
     /* IN */
     uint32_t nr_spis;
+    /* IN */
+    uint16_t sve_vl_bits;
     /*
      * OUT
      * Based on the property clock-frequency in the DT timer node.
xen/include/public/domctl.h +1 -1
index 51be28c3de7c..616d7a1c070d 100644
@@ -21,7 +21,7 @@
 #include "hvm/save.h"
 #include "memory.h"
 
-#define XEN_DOMCTL_INTERFACE_VERSION 0x00000015
+#define XEN_DOMCTL_INTERFACE_VERSION 0x00000016
 
 /*
  * NB. xen_domctl.domain is an IN/OUT parameter for this operation.
Signature · 1 line
2.17.1
#7 lore raw

Re: [RFC PATCH 2/8] xen/arm: add sve_vl_bits field to domain

Hi Luca,

Quoted 61 lines · Luca Fancellu

Add sve_vl_bits field to arch_domain and xen_arch_domainconfig structure, to allow the domain to have an information about the SVE feature and the number of SVE register bits that are allowed for this domain.

The field is used also to allow or forbid a domain to use SVE, because a value equal to zero means the guest is not allowed to use the feature.

When the guest is allowed to use SVE, the zcr_el2 register is updated on context switch to restict the domain on the allowed number of bits chosen, this value is the minimum among the chosen value and the platform supported value.

---
  xen/arch/arm/arm64/sve.c             |  9 ++++++
  xen/arch/arm/domain.c                | 45 ++++++++++++++++++++++++++++
  xen/arch/arm/include/asm/arm64/sve.h | 12 ++++++++
  xen/arch/arm/include/asm/domain.h    |  6 ++++
  xen/include/public/arch-arm.h        |  2 ++
  xen/include/public/domctl.h          |  2 +-
  6 files changed, 75 insertions(+), 1 deletion(-)
xen/arch/arm/arm64/sve.c +9 -0
index 326389278292..b7695834f4ba 100644
@@ -6,6 +6,7 @@
   */
 
  #include <xen/types.h>
+#include <asm/cpufeature.h>
  #include <asm/arm64/sve.h>
  #include <asm/arm64/sysregs.h>
 
@@ -36,3 +37,11 @@ register_t vl_to_zcr(uint16_t vl)
  {
      return ((vl / SVE_VL_MULTIPLE_VAL) - 1U) & ZCR_ELx_LEN_MASK;
  }
+
+/* Get the system sanitized value for VL in bits */
+uint16_t get_sys_vl_len(void)
+{
+    /* ZCR_ELx len field is ((len+1) * 128) = vector bits length */
+    return ((system_cpuinfo.zcr64.bits[0] & ZCR_ELx_LEN_MASK) + 1U) *
+            SVE_VL_MULTIPLE_VAL;
+}
xen/arch/arm/domain.c +1 -0
index 8ea3843ea8e8..27f38729302b 100644
@@ -13,6 +13,7 @@
  #include <xen/wait.h>
 
  #include <asm/alternative.h>
+#include <asm/arm64/sve.h>
  #include <asm/cpuerrata.h>
  #include <asm/cpufeature.h>
  #include <asm/current.h>

xen/arch/arm/domain.c static void ctxt_switch_to(struct vcpu *n) · line 190

Quoted 7 lines
xen/arch/arm/domain.c +4 -0
@@ -183,6 +184,11 @@ static void ctxt_switch_to(struct vcpu *n)
 
      WRITE_SYSREG(n->arch.cptr_el2, CPTR_EL2);
 
+#ifdef CONFIG_ARM64_SVE
+    if ( is_sve_domain(n->domain) )
+        WRITE_SYSREG(n->arch.zcr_el2, ZCR_EL2);
+#endif

I would actually expect that is_sve_domain() returns false when the SVE is not enabled. So can we simply remove the #ifdef?

Quoted 46 lines
xen/arch/arm/domain.c +25 -0
+
      /* VFP */
      vfp_restore_state(n);
 
@@ -551,6 +557,11 @@ int arch_vcpu_create(struct vcpu *v)
      v->arch.vmpidr = MPIDR_SMP | vcpuid_to_vaffinity(v->vcpu_id);
 
      v->arch.cptr_el2 = get_default_cptr_flags();
+    if ( is_sve_domain(v->domain) )
+    {
+        v->arch.cptr_el2 &= ~HCPTR_CP(8);
+        v->arch.zcr_el2 = vl_to_zcr(v->domain->arch.sve_vl_bits);
+    }
 
      v->arch.hcr_el2 = get_default_hcr_flags();
 
@@ -595,6 +606,7 @@ int arch_sanitise_domain_config(struct xen_domctl_createdomain *config)
      unsigned int max_vcpus;
      unsigned int flags_required = (XEN_DOMCTL_CDF_hvm | XEN_DOMCTL_CDF_hap);
      unsigned int flags_optional = (XEN_DOMCTL_CDF_iommu | XEN_DOMCTL_CDF_vpmu);
+    unsigned int sve_vl_bits = config->arch.sve_vl_bits;
 
      if ( (config->flags & ~flags_optional) != flags_required )
      {
@@ -603,6 +615,36 @@ int arch_sanitise_domain_config(struct xen_domctl_createdomain *config)
          return -EINVAL;
      }
 
+    /* Check feature flags */
+    if ( sve_vl_bits > 0 ) {
+        unsigned int zcr_max_bits;
+
+        if ( !cpu_has_sve )
+        {
+            dprintk(XENLOG_INFO, "SVE is unsupported on this machine.\n");
+            return -EINVAL;
+        }
+        else if ( !is_vl_valid(sve_vl_bits) )
+        {
+            dprintk(XENLOG_INFO, "Unsupported SVE vector length (%u)\n",
+                    sve_vl_bits);
+            return -EINVAL;
+        }
+        /*
+         * get_sys_vl_len() is the common safe value among all cpus, so if the
+         * value specified by the user is above that value, use the safe value

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

Quoted 10 lines
xen/arch/arm/domain.c +10 -0
+         * instead.
+         */
+        zcr_max_bits = get_sys_vl_len();
+        if ( sve_vl_bits > zcr_max_bits )
+        {
+            config->arch.sve_vl_bits = zcr_max_bits;
+            dprintk(XENLOG_INFO,
+                    "SVE vector length lowered to %u, safe value among CPUs\n",
+                    zcr_max_bits);
+        }

I don't think Xen should "downgrade" the value. Instead, this should be the decision from the tools. So we want to find a different way to reproduce the value (Andrew may have some ideas here as he was looking at it).

Quoted 85 lines
xen/arch/arm/domain.c +5 -0
+    }
+
      /* The P2M table must always be shared between the CPU and the IOMMU */
      if ( config->iommu_opts & XEN_DOMCTL_IOMMU_no_sharept )
      {
@@ -745,6 +787,9 @@ int arch_domain_create(struct domain *d,
      if ( (rc = domain_vpci_init(d)) != 0 )
          goto fail;
 
+    /* Copy sve_vl_bits to the domain configuration */
+    d->arch.sve_vl_bits = config->arch.sve_vl_bits;
+
      return 0;
 
  fail:
xen/arch/arm/include/asm/arm64/sve.h +12 -0
index bd56e2f24230..f4a660e402ca 100644
@@ -13,10 +13,17 @@
  /* Vector length must be multiple of 128 */
  #define SVE_VL_MULTIPLE_VAL (128U)
 
+static inline bool is_vl_valid(uint16_t vl)
+{
+    /* SVE vector length is multiple of 128 and maximum 2048 */
+    return ((vl % SVE_VL_MULTIPLE_VAL) == 0) && (vl <= SVE_VL_MAX_BITS);
+}
+
  #ifdef CONFIG_ARM64_SVE
 
  register_t compute_max_zcr(void);
  register_t vl_to_zcr(uint16_t vl);
+uint16_t get_sys_vl_len(void);
 
  #else /* !CONFIG_ARM64_SVE */
 
@@ -30,6 +37,11 @@ static inline register_t vl_to_zcr(uint16_t vl)
      return 0;
  }
 
+static inline uint16_t get_sys_vl_len(void)
+{
+    return 0;
+}
+
  #endif
 
  #endif /* _ARM_ARM64_SVE_H */
xen/arch/arm/include/asm/domain.h +6 -0
index 42eb5df320a7..e4794a9fd2ab 100644
@@ -31,6 +31,8 @@ enum domain_type {
 
  #define is_domain_direct_mapped(d) ((d)->cdf & CDF_directmap)
 
+#define is_sve_domain(d) ((d)->arch.sve_vl_bits > 0)
+
  /*
   * Is the domain using the host memory layout?
   *
@@ -114,6 +116,9 @@ struct arch_domain
      void *tee;
  #endif
 
+    /* max SVE vector length in bits */
+    uint16_t sve_vl_bits;
+
  }  __cacheline_aligned;
 
  struct arch_vcpu
@@ -190,6 +195,7 @@ struct arch_vcpu
      register_t tpidrro_el0;
 
      /* HYP configuration */
+    register_t zcr_el2;
      register_t cptr_el2;
      register_t hcr_el2;
      register_t mdcr_el2;
xen/include/public/arch-arm.h +0 -0
index 1528ced5097a..e18a075105f0 100644

xen/include/public/arch-arm.h struct xen_arch_domainconfig { · line 308

Quoted 5 lines
xen/include/public/arch-arm.h +2 -0
@@ -304,6 +304,8 @@ struct xen_arch_domainconfig {
      uint16_t tee_type;
      /* IN */
      uint32_t nr_spis;
+    /* IN */
+    uint16_t sve_vl_bits;

Please spell out the padding clearly (even though I know there is one in this structure that is not mentioned).

Quoted 8 lines
xen/include/public/arch-arm.h +0 -0
      /*
       * OUT
       * Based on the property clock-frequency in the DT timer node.
xen/include/public/domctl.h +0 -0
index 51be28c3de7c..616d7a1c070d 100644

xen/include/public/domctl.h line 27

Quoted 8 lines
xen/include/public/domctl.h +1 -1
@@ -21,7 +21,7 @@
  #include "hvm/save.h"
  #include "memory.h"
 
-#define XEN_DOMCTL_INTERFACE_VERSION 0x00000015
+#define XEN_DOMCTL_INTERFACE_VERSION 0x00000016
 
  /*
   * NB. xen_domctl.domain is an IN/OUT parameter for this operation.

Cheers,

Signature · 1 line
Julien Grall
#8 lore raw

Re: [RFC PATCH 2/8] xen/arm: add sve_vl_bits field to domain

Quoted 60 lines · Julien Grall

Hi Luca,

Quoted 53 lines · Luca Fancellu

Add sve_vl_bits field to arch_domain and xen_arch_domainconfig structure, to allow the domain to have an information about the SVE feature and the number of SVE register bits that are allowed for this domain.
The field is used also to allow or forbid a domain to use SVE, because a value equal to zero means the guest is not allowed to use the feature.
When the guest is allowed to use SVE, the zcr_el2 register is updated on context switch to restict the domain on the allowed number of bits chosen, this value is the minimum among the chosen value and the platform supported value.

---
 xen/arch/arm/arm64/sve.c             |  9 ++++++
 xen/arch/arm/domain.c                | 45 ++++++++++++++++++++++++++++
 xen/arch/arm/include/asm/arm64/sve.h | 12 ++++++++
 xen/arch/arm/include/asm/domain.h    |  6 ++++
 xen/include/public/arch-arm.h        |  2 ++
 xen/include/public/domctl.h          |  2 +-
 6 files changed, 75 insertions(+), 1 deletion(-)
xen/arch/arm/arm64/sve.c +1 -0
index 326389278292..b7695834f4ba 100644
@@ -6,6 +6,7 @@
  */
   #include <xen/types.h>
+#include <asm/cpufeature.h>
 #include <asm/arm64/sve.h>
 #include <asm/arm64/sysregs.h>
 @@ -36,3 +37,11 @@ register_t vl_to_zcr(uint16_t vl)
 {
     return ((vl / SVE_VL_MULTIPLE_VAL) - 1U) & ZCR_ELx_LEN_MASK;

}

xen/arch/arm/arm64/sve.c +8 -0
+
+/* Get the system sanitized value for VL in bits */
+uint16_t get_sys_vl_len(void)
+{
+    /* ZCR_ELx len field is ((len+1) * 128) = vector bits length */
+    return ((system_cpuinfo.zcr64.bits[0] & ZCR_ELx_LEN_MASK) + 1U) *
+            SVE_VL_MULTIPLE_VAL;
+}
xen/arch/arm/domain.c +1 -0
index 8ea3843ea8e8..27f38729302b 100644
@@ -13,6 +13,7 @@
 #include <xen/wait.h>
   #include <asm/alternative.h>
+#include <asm/arm64/sve.h>
 #include <asm/cpuerrata.h>
 #include <asm/cpufeature.h>
 #include <asm/current.h>
Quoted 8 lines

xen/arch/arm/domain.c static void ctxt_switch_to(struct vcpu *n) · line 188

Quoted 6 lines
xen/arch/arm/domain.c +3 -0
@@ -183,6 +184,11 @@ static void ctxt_switch_to(struct vcpu *n)
       WRITE_SYSREG(n->arch.cptr_el2, CPTR_EL2);
 +#ifdef CONFIG_ARM64_SVE
+    if ( is_sve_domain(n->domain) )
+        WRITE_SYSREG(n->arch.zcr_el2, ZCR_EL2);
+#endif

I would actually expect that is_sve_domain() returns false when the SVE is not enabled. So can we simply remove the #ifdef?

I was tricked by it too, the arm32 build will fail because it doesn’t know ZCR_EL2

Quoted 45 lines
Quoted 45 lines
xen/arch/arm/domain.c +28 -0
+
     /* VFP */
     vfp_restore_state(n);
 @@ -551,6 +557,11 @@ int arch_vcpu_create(struct vcpu *v)
     v->arch.vmpidr = MPIDR_SMP | vcpuid_to_vaffinity(v->vcpu_id);
       v->arch.cptr_el2 = get_default_cptr_flags();
+    if ( is_sve_domain(v->domain) )
+    {
+        v->arch.cptr_el2 &= ~HCPTR_CP(8);
+        v->arch.zcr_el2 = vl_to_zcr(v->domain->arch.sve_vl_bits);
+    }
       v->arch.hcr_el2 = get_default_hcr_flags();
 @@ -595,6 +606,7 @@ int arch_sanitise_domain_config(struct xen_domctl_createdomain *config)
     unsigned int max_vcpus;
     unsigned int flags_required = (XEN_DOMCTL_CDF_hvm | XEN_DOMCTL_CDF_hap);
     unsigned int flags_optional = (XEN_DOMCTL_CDF_iommu | XEN_DOMCTL_CDF_vpmu);
+    unsigned int sve_vl_bits = config->arch.sve_vl_bits;
       if ( (config->flags & ~flags_optional) != flags_required )
     {
@@ -603,6 +615,36 @@ int arch_sanitise_domain_config(struct xen_domctl_createdomain *config)
         return -EINVAL;
     }
 +    /* Check feature flags */
+    if ( sve_vl_bits > 0 ) {
+        unsigned int zcr_max_bits;
+
+        if ( !cpu_has_sve )
+        {
+            dprintk(XENLOG_INFO, "SVE is unsupported on this machine.\n");
+            return -EINVAL;
+        }
+        else if ( !is_vl_valid(sve_vl_bits) )
+        {
+            dprintk(XENLOG_INFO, "Unsupported SVE vector length (%u)\n",
+                    sve_vl_bits);
+            return -EINVAL;
+        }
+        /*
+         * get_sys_vl_len() is the common safe value among all cpus, so if the
+         * value specified by the user is above that value, use the safe value
+         * instead.
+         */
+        zcr_max_bits = get_sys_vl_len();
+        if ( sve_vl_bits > zcr_max_bits )
Quoted 8 lines

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

Quoted 6 lines
xen/arch/arm/domain.c +6 -0
+        {
+            config->arch.sve_vl_bits = zcr_max_bits;
+            dprintk(XENLOG_INFO,
+                    "SVE vector length lowered to %u, safe value among CPUs\n",
+                    zcr_max_bits);
+        }

I don't think Xen should "downgrade" the value. Instead, this should be the decision from the tools. So we want to find a different way to reproduce the value (Andrew may have some ideas here as he was looking at it).

Can you explain this in more details? By the tools you mean xl? This would be ok for DomUs, but how to do it for Dom0? I’ll wait suggestions also from Andrew.

Quoted 72 lines
Quoted 72 lines
xen/arch/arm/domain.c +4 -0
+    }
+
     /* The P2M table must always be shared between the CPU and the IOMMU */
     if ( config->iommu_opts & XEN_DOMCTL_IOMMU_no_sharept )
     {
@@ -745,6 +787,9 @@ int arch_domain_create(struct domain *d,
     if ( (rc = domain_vpci_init(d)) != 0 )
         goto fail;
 +    /* Copy sve_vl_bits to the domain configuration */
+    d->arch.sve_vl_bits = config->arch.sve_vl_bits;
+
     return 0;
   fail:
xen/arch/arm/include/asm/arm64/sve.h +10 -0
index bd56e2f24230..f4a660e402ca 100644
@@ -13,10 +13,17 @@
 /* Vector length must be multiple of 128 */
 #define SVE_VL_MULTIPLE_VAL (128U)
 +static inline bool is_vl_valid(uint16_t vl)
+{
+    /* SVE vector length is multiple of 128 and maximum 2048 */
+    return ((vl % SVE_VL_MULTIPLE_VAL) == 0) && (vl <= SVE_VL_MAX_BITS);
+}
+
 #ifdef CONFIG_ARM64_SVE
   register_t compute_max_zcr(void);
 register_t vl_to_zcr(uint16_t vl);
+uint16_t get_sys_vl_len(void);
   #else /* !CONFIG_ARM64_SVE */
 @@ -30,6 +37,11 @@ static inline register_t vl_to_zcr(uint16_t vl)
     return 0;
 }
 +static inline uint16_t get_sys_vl_len(void)
+{
+    return 0;
+}
+
 #endif
   #endif /* _ARM_ARM64_SVE_H */
xen/arch/arm/include/asm/domain.h +4 -0
index 42eb5df320a7..e4794a9fd2ab 100644
@@ -31,6 +31,8 @@ enum domain_type {
   #define is_domain_direct_mapped(d) ((d)->cdf & CDF_directmap)
 +#define is_sve_domain(d) ((d)->arch.sve_vl_bits > 0)
+
 /*
  * Is the domain using the host memory layout?
  *
@@ -114,6 +116,9 @@ struct arch_domain
     void *tee;
 #endif
 +    /* max SVE vector length in bits */
+    uint16_t sve_vl_bits;
+
 }  __cacheline_aligned;
   struct arch_vcpu
@@ -190,6 +195,7 @@ struct arch_vcpu
     register_t tpidrro_el0;
       /* HYP configuration */
+    register_t zcr_el2;
     register_t cptr_el2;
     register_t hcr_el2;
     register_t mdcr_el2;
xen/include/public/arch-arm.h +0 -0
index 1528ced5097a..e18a075105f0 100644
Quoted 8 lines

xen/include/public/arch-arm.h struct xen_arch_domainconfig { · line 308

Quoted 6 lines
xen/include/public/arch-arm.h +2 -0
@@ -304,6 +304,8 @@ struct xen_arch_domainconfig {
     uint16_t tee_type;
     /* IN */
     uint32_t nr_spis;
+    /* IN */
+    uint16_t sve_vl_bits;

Please spell out the padding clearly (even though I know there is one in this structure that is not mentioned).

Ok I will

Quoted 20 lines
Quoted 9 lines
xen/include/public/arch-arm.h +0 -0
     /*
      * OUT
      * Based on the property clock-frequency in the DT timer node.
xen/include/public/domctl.h +0 -0
index 51be28c3de7c..616d7a1c070d 100644

xen/include/public/domctl.h line 26

Quoted 6 lines
xen/include/public/domctl.h +1 -0
@@ -21,7 +21,7 @@
 #include "hvm/save.h"
 #include "memory.h"
 -#define XEN_DOMCTL_INTERFACE_VERSION 0x00000015
+#define XEN_DOMCTL_INTERFACE_VERSION 0x00000016
   /*
  * NB. xen_domctl.domain is an IN/OUT parameter for this operation.

Cheers,

Signature · 1 line
Julien Grall
#9 lore raw

Re: [RFC PATCH 2/8] xen/arm: add sve_vl_bits field to domain

Hi Luca,

Quoted 67 lines · Luca Fancellu
Quoted 58 lines · Julien Grall

Hi Luca,

Quoted 53 lines · Luca Fancellu

Add sve_vl_bits field to arch_domain and xen_arch_domainconfig structure, to allow the domain to have an information about the SVE feature and the number of SVE register bits that are allowed for this domain.
The field is used also to allow or forbid a domain to use SVE, because a value equal to zero means the guest is not allowed to use the feature.
When the guest is allowed to use SVE, the zcr_el2 register is updated on context switch to restict the domain on the allowed number of bits chosen, this value is the minimum among the chosen value and the platform supported value.

---
  xen/arch/arm/arm64/sve.c             |  9 ++++++
  xen/arch/arm/domain.c                | 45 ++++++++++++++++++++++++++++
  xen/arch/arm/include/asm/arm64/sve.h | 12 ++++++++
  xen/arch/arm/include/asm/domain.h    |  6 ++++
  xen/include/public/arch-arm.h        |  2 ++
  xen/include/public/domctl.h          |  2 +-
  6 files changed, 75 insertions(+), 1 deletion(-)
xen/arch/arm/arm64/sve.c +1 -0
index 326389278292..b7695834f4ba 100644
@@ -6,6 +6,7 @@
   */
    #include <xen/types.h>
+#include <asm/cpufeature.h>
  #include <asm/arm64/sve.h>
  #include <asm/arm64/sysregs.h>
  @@ -36,3 +37,11 @@ register_t vl_to_zcr(uint16_t vl)
  {
      return ((vl / SVE_VL_MULTIPLE_VAL) - 1U) & ZCR_ELx_LEN_MASK;

}

xen/arch/arm/arm64/sve.c +8 -0
+
+/* Get the system sanitized value for VL in bits */
+uint16_t get_sys_vl_len(void)
+{
+    /* ZCR_ELx len field is ((len+1) * 128) = vector bits length */
+    return ((system_cpuinfo.zcr64.bits[0] & ZCR_ELx_LEN_MASK) + 1U) *
+            SVE_VL_MULTIPLE_VAL;
+}
xen/arch/arm/domain.c +1 -0
index 8ea3843ea8e8..27f38729302b 100644
@@ -13,6 +13,7 @@
  #include <xen/wait.h>
    #include <asm/alternative.h>
+#include <asm/arm64/sve.h>
  #include <asm/cpuerrata.h>
  #include <asm/cpufeature.h>
  #include <asm/current.h>
Quoted 8 lines

xen/arch/arm/domain.c static void ctxt_switch_to(struct vcpu *n) · line 188

Quoted 6 lines
xen/arch/arm/domain.c +3 -0
@@ -183,6 +184,11 @@ static void ctxt_switch_to(struct vcpu *n)
        WRITE_SYSREG(n->arch.cptr_el2, CPTR_EL2);
  +#ifdef CONFIG_ARM64_SVE
+    if ( is_sve_domain(n->domain) )
+        WRITE_SYSREG(n->arch.zcr_el2, ZCR_EL2);
+#endif

I would actually expect that is_sve_domain() returns false when the SVE is not enabled. So can we simply remove the #ifdef?

Quoted 1 line

I was tricked by it too, the arm32 build will fail because it doesn’t know ZCR_EL2

Ok. In which case, I think we should move the call to sve_restore_state().

Quoted 55 lines
Quoted 46 lines
Quoted 46 lines
xen/arch/arm/domain.c +28 -0
+
      /* VFP */
      vfp_restore_state(n);
  @@ -551,6 +557,11 @@ int arch_vcpu_create(struct vcpu *v)
      v->arch.vmpidr = MPIDR_SMP | vcpuid_to_vaffinity(v->vcpu_id);
        v->arch.cptr_el2 = get_default_cptr_flags();
+    if ( is_sve_domain(v->domain) )
+    {
+        v->arch.cptr_el2 &= ~HCPTR_CP(8);
+        v->arch.zcr_el2 = vl_to_zcr(v->domain->arch.sve_vl_bits);
+    }
        v->arch.hcr_el2 = get_default_hcr_flags();
  @@ -595,6 +606,7 @@ int arch_sanitise_domain_config(struct xen_domctl_createdomain *config)
      unsigned int max_vcpus;
      unsigned int flags_required = (XEN_DOMCTL_CDF_hvm | XEN_DOMCTL_CDF_hap);
      unsigned int flags_optional = (XEN_DOMCTL_CDF_iommu | XEN_DOMCTL_CDF_vpmu);
+    unsigned int sve_vl_bits = config->arch.sve_vl_bits;
        if ( (config->flags & ~flags_optional) != flags_required )
      {
@@ -603,6 +615,36 @@ int arch_sanitise_domain_config(struct xen_domctl_createdomain *config)
          return -EINVAL;
      }
  +    /* Check feature flags */
+    if ( sve_vl_bits > 0 ) {
+        unsigned int zcr_max_bits;
+
+        if ( !cpu_has_sve )
+        {
+            dprintk(XENLOG_INFO, "SVE is unsupported on this machine.\n");
+            return -EINVAL;
+        }
+        else if ( !is_vl_valid(sve_vl_bits) )
+        {
+            dprintk(XENLOG_INFO, "Unsupported SVE vector length (%u)\n",
+                    sve_vl_bits);
+            return -EINVAL;
+        }
+        /*
+         * get_sys_vl_len() is the common safe value among all cpus, so if the
+         * value specified by the user is above that value, use the safe value
+         * instead.
+         */
+        zcr_max_bits = get_sys_vl_len();
+        if ( sve_vl_bits > zcr_max_bits )
Quoted 8 lines

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

Quoted 6 lines
xen/arch/arm/domain.c +6 -0
+        {
+            config->arch.sve_vl_bits = zcr_max_bits;
+            dprintk(XENLOG_INFO,
+                    "SVE vector length lowered to %u, safe value among CPUs\n",
+                    zcr_max_bits);
+        }

I don't think Xen should "downgrade" the value. Instead, this should be the decision from the tools. So we want to find a different way to reproduce the value (Andrew may have some ideas here as he was looking at it).

Quoted 1 line

Can you explain this in more details?

I would extend XEN_SYSCTL_physinfo to return the maximum SVE vecto length supported by the Hardware.

Libxl would then read the value and compare to what the user requested. This would then be up to the toolstack to decide what to do.

Quoted 1 line

By the tools you mean xl?

I think libxl should do strict checking. If we also want to reduce what the user requested, then this part should be in xl.

Quoted 1 line

This would be ok for DomUs, but how to do it for Dom0?

Then we should fail to create dom0 and say the vector-length requested is not supported.

Cheers,

Signature · 1 line
Julien Grall
#10 lore raw

Re: [RFC PATCH 2/8] xen/arm: add sve_vl_bits field to domain

Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
Quoted 13 lines
xen/arch/arm/domain.c +1 -0
index 8ea3843ea8e8..27f38729302b 100644
@@ -13,6 +13,7 @@
 #include <xen/wait.h>
   #include <asm/alternative.h>
+#include <asm/arm64/sve.h>
 #include <asm/cpuerrata.h>
 #include <asm/cpufeature.h>
 #include <asm/current.h>

xen/arch/arm/domain.c static void ctxt_switch_to(struct vcpu *n) · line 188

Quoted 5 lines
xen/arch/arm/domain.c +3 -0
@@ -183,6 +184,11 @@ static void ctxt_switch_to(struct vcpu *n)
       WRITE_SYSREG(n->arch.cptr_el2, CPTR_EL2);
 +#ifdef CONFIG_ARM64_SVE
+    if ( is_sve_domain(n->domain) )
+        WRITE_SYSREG(n->arch.zcr_el2, ZCR_EL2);
+#endif

I would actually expect that is_sve_domain() returns false when the SVE is not enabled. So can we simply remove the #ifdef?

Quoted 3 lines
Quoted 1 line

I was tricked by it too, the arm32 build will fail because it doesn’t know ZCR_EL2

Ok. In which case, I think we should move the call to sve_restore_state().

Ok for me, I will move the zcr_el2 introduction together with the context switch code introduced by the patch later.

Quoted 54 lines
Quoted 54 lines
Quoted 54 lines
Quoted 42 lines
xen/arch/arm/domain.c +24 -0
+
     /* VFP */
     vfp_restore_state(n);
 @@ -551,6 +557,11 @@ int arch_vcpu_create(struct vcpu *v)
     v->arch.vmpidr = MPIDR_SMP | vcpuid_to_vaffinity(v->vcpu_id);
       v->arch.cptr_el2 = get_default_cptr_flags();
+    if ( is_sve_domain(v->domain) )
+    {
+        v->arch.cptr_el2 &= ~HCPTR_CP(8);
+        v->arch.zcr_el2 = vl_to_zcr(v->domain->arch.sve_vl_bits);
+    }
       v->arch.hcr_el2 = get_default_hcr_flags();
 @@ -595,6 +606,7 @@ int arch_sanitise_domain_config(struct xen_domctl_createdomain *config)
     unsigned int max_vcpus;
     unsigned int flags_required = (XEN_DOMCTL_CDF_hvm | XEN_DOMCTL_CDF_hap);
     unsigned int flags_optional = (XEN_DOMCTL_CDF_iommu | XEN_DOMCTL_CDF_vpmu);
+    unsigned int sve_vl_bits = config->arch.sve_vl_bits;
       if ( (config->flags & ~flags_optional) != flags_required )
     {
@@ -603,6 +615,36 @@ int arch_sanitise_domain_config(struct xen_domctl_createdomain *config)
         return -EINVAL;
     }
 +    /* Check feature flags */
+    if ( sve_vl_bits > 0 ) {
+        unsigned int zcr_max_bits;
+
+        if ( !cpu_has_sve )
+        {
+            dprintk(XENLOG_INFO, "SVE is unsupported on this machine.\n");
+            return -EINVAL;
+        }
+        else if ( !is_vl_valid(sve_vl_bits) )
+        {
+            dprintk(XENLOG_INFO, "Unsupported SVE vector length (%u)\n",
+                    sve_vl_bits);
+            return -EINVAL;
+        }
+        /*
+         * get_sys_vl_len() is the common safe value among all cpus, so if the
+         * value specified by the user is above that value, use the safe value

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

Quoted 10 lines
xen/arch/arm/domain.c +10 -0
+         * instead.
+         */
+        zcr_max_bits = get_sys_vl_len();
+        if ( sve_vl_bits > zcr_max_bits )
+        {
+            config->arch.sve_vl_bits = zcr_max_bits;
+            dprintk(XENLOG_INFO,
+                    "SVE vector length lowered to %u, safe value among CPUs\n",
+                    zcr_max_bits);
+        }

I don't think Xen should "downgrade" the value. Instead, this should be the decision from the tools. So we want to find a different way to reproduce the value (Andrew may have some ideas here as he was looking at it).

Quoted 5 lines
Quoted 1 line

Can you explain this in more details?

I would extend XEN_SYSCTL_physinfo to return the maximum SVE vecto length supported by the Hardware.

Libxl would then read the value and compare to what the user requested. This would then be up to the toolstack to decide what to do.

Sounds good, looking into struct xen_sysctl_physinfo, seems that I might be the first user of the arch_capabilities field (as well as introducing a new one for the VL value), where can I put the define for the arch_capabilities flag? Is it ok inside sysctl.h something along these lines:

#define XEN_SYSCTL_PHYSCAP_ARM_SVE (1u << 0)

or

#define XEN_SYSCTL_PHYSCAP_ARM_SVE (1u)

And, is it ok to put the VL value in the struct xen_sysctl_physinfo even if it’s just for arm64?

Quoted 7 lines
Quoted 2 lines

By the tools you mean xl?

I think libxl should do strict checking. If we also want to reduce what the user requested, then this part should be in xl.

Quoted 1 line

This would be ok for DomUs, but how to do it for Dom0?

Then we should fail to create dom0 and say the vector-length requested is not supported.

Fine for me

Quoted 5 lines

Cheers,

Signature · 1 line
Julien Grall
#11 lore raw

RFC PATCH 3/8 xen/arm: Expose SVE feature to the guest

In reply to Luca Fancellu

When a guest is allowed to use SVE, expose the SVE features through the identification registers.

---
 xen/arch/arm/arm64/vsysreg.c | 39 ++++++++++++++++++++++++++++++++++--
 1 file changed, 37 insertions(+), 2 deletions(-)
xen/arch/arm/arm64/vsysreg.c +37 -2
index 758750983c11..10048bb4d221 100644
@@ -18,6 +18,7 @@
 
 #include <xen/sched.h>
 
+#include <asm/arm64/cpufeature.h>
 #include <asm/current.h>
 #include <asm/regs.h>
 #include <asm/traps.h>
@@ -295,7 +296,28 @@ void do_sysreg(struct cpu_user_regs *regs,
     GENERATE_TID3_INFO(MVFR0_EL1, mvfr, 0)
     GENERATE_TID3_INFO(MVFR1_EL1, mvfr, 1)
     GENERATE_TID3_INFO(MVFR2_EL1, mvfr, 2)
-    GENERATE_TID3_INFO(ID_AA64PFR0_EL1, pfr64, 0)
+
+    case HSR_SYSREG_ID_AA64PFR0_EL1:
+    {
+        register_t guest_reg_value = guest_cpuinfo.pfr64.bits[0];
+
+        if ( is_sve_domain(v->domain) )
+        {
+            /* 4 is the SVE field width in id_aa64pfr0_el1 */
+            uint64_t mask = GENMASK(ID_AA64PFR0_SVE_SHIFT + 4 - 1,
+                                    ID_AA64PFR0_SVE_SHIFT);
+            /* sysval is the sve field on the system */
+            uint64_t sysval = cpuid_feature_extract_unsigned_field_width(
+                                system_cpuinfo.pfr64.bits[0],
+                                ID_AA64PFR0_SVE_SHIFT, 4);
+            guest_reg_value &= ~mask;
+            guest_reg_value |= (sysval << ID_AA64PFR0_SVE_SHIFT) & mask;
+        }
+
+        return handle_ro_read_val(regs, regidx, hsr.sysreg.read, hsr, 1,
+                                  guest_reg_value);
+    }
+
     GENERATE_TID3_INFO(ID_AA64PFR1_EL1, pfr64, 1)
     GENERATE_TID3_INFO(ID_AA64DFR0_EL1, dbg64, 0)
     GENERATE_TID3_INFO(ID_AA64DFR1_EL1, dbg64, 1)
@@ -306,7 +328,20 @@ void do_sysreg(struct cpu_user_regs *regs,
     GENERATE_TID3_INFO(ID_AA64MMFR2_EL1, mm64, 2)
     GENERATE_TID3_INFO(ID_AA64AFR0_EL1, aux64, 0)
     GENERATE_TID3_INFO(ID_AA64AFR1_EL1, aux64, 1)
-    GENERATE_TID3_INFO(ID_AA64ZFR0_EL1, zfr64, 0)
+
+    case HSR_SYSREG_ID_AA64ZFR0_EL1:
+    {
+        /*
+         * When the guest has the SVE feature enabled, the whole id_aa64zfr0_el1
+         * needs to be exposed.
+         */
+        register_t guest_reg_value = guest_cpuinfo.zfr64.bits[0];
+        if ( is_sve_domain(v->domain) )
+            guest_reg_value = system_cpuinfo.zfr64.bits[0];
+
+        return handle_ro_read_val(regs, regidx, hsr.sysreg.read, hsr, 1,
+                                  guest_reg_value);
+    }
 
     /*
      * Those cases are catching all Reserved registers trapped by TID3 which
Signature · 1 line
2.17.1
#12 lore raw

RFC PATCH 4/8 xen/arm: add SVE exception class handling

In reply to Luca Fancellu

SVE has a new exception class with code 0x19, introduce the new code and handle the exception.

---
 xen/arch/arm/include/asm/processor.h |  1 +
 xen/arch/arm/traps.c                 | 12 ++++++++++++
 2 files changed, 13 insertions(+)
xen/arch/arm/include/asm/processor.h +1 -0
index 0e38926b94db..625c2bd0cd6c 100644
@@ -426,6 +426,7 @@
 #define HSR_EC_HVC64                0x16
 #define HSR_EC_SMC64                0x17
 #define HSR_EC_SYSREG               0x18
+#define HSR_EC_SVE                  0x19
 #endif
 #define HSR_EC_INSTR_ABORT_LOWER_EL 0x20
 #define HSR_EC_INSTR_ABORT_CURR_EL  0x21
xen/arch/arm/traps.c +12 -0
index 45163fd3afb0..66e07197aea5 100644
@@ -2168,6 +2168,13 @@ void do_trap_guest_sync(struct cpu_user_regs *regs)
         perfc_incr(trap_sysreg);
         do_sysreg(regs, hsr);
         break;
+    case HSR_EC_SVE:
+        GUEST_BUG_ON(regs_mode_is_32bit(regs));
+        gprintk(XENLOG_WARNING,
+                "Domain id %d tried to use SVE while not allowed\n",
+                current->domain->domain_id);
+        inject_undef_exception(regs, hsr);
+        break;
 #endif
 
     case HSR_EC_INSTR_ABORT_LOWER_EL:
@@ -2197,6 +2204,11 @@ void do_trap_hyp_sync(struct cpu_user_regs *regs)
     case HSR_EC_BRK:
         do_trap_brk(regs, hsr);
         break;
+    case HSR_EC_SVE:
+        /* An SVE exception is a bug somewhere in hypervisor code */
+        printk("SVE trap at EL2.\n");
+        do_unexpected_trap("Hypervisor", regs);
+        break;
 #endif
     case HSR_EC_DATA_ABORT_CURR_EL:
     case HSR_EC_INSTR_ABORT_CURR_EL:
Signature · 1 line
2.17.1
#13 lore raw

RFC PATCH 5/8 arm/sve: save/restore SVE context switch

In reply to Luca Fancellu

Save/restore context switch for SVE, allocate memory to contain the Z0-31 registers whose length is maximum 2048 bits each and FFR who can be maximum 256 bits, the allocated memory depends on how many bits is the vector length for the domain and how many bits are supported by the platform.

Save P0-15 whose length is maximum 256 bits each, in this case the memory used is from the fpregs field in struct vfp_state, because V0-31 are part of Z0-31 and this space would have been unused for SVE domain otherwise.

Create zcr_el1 field in arch_vcpu and save/restore ZCR_EL1 value on context switch.

Remove headers from sve.c that are already included using xen/sched.h.

---
 xen/arch/arm/arm64/sve.c                 |  58 +++++++++-
 xen/arch/arm/arm64/sve_asm.S             | 141 +++++++++++++++++++++++
 xen/arch/arm/arm64/vfp.c                 |  79 +++++++------
 xen/arch/arm/domain.c                    |  12 ++
 xen/arch/arm/include/asm/arm64/sve.h     |  13 +++
 xen/arch/arm/include/asm/arm64/sysregs.h |   3 +
 xen/arch/arm/include/asm/arm64/vfp.h     |  10 ++
 xen/arch/arm/include/asm/domain.h        |   1 +
 8 files changed, 280 insertions(+), 37 deletions(-)
xen/arch/arm/arm64/sve.c +55 -3
index b7695834f4ba..c7b325700fe4 100644
@@ -5,12 +5,29 @@
  * Copyright (C) 2022 ARM Ltd.
  */
 
-#include <xen/types.h>
-#include <asm/cpufeature.h>
+#include <xen/sched.h>
+#include <xen/sizes.h>
 #include <asm/arm64/sve.h>
-#include <asm/arm64/sysregs.h>
 
 extern unsigned int sve_get_hw_vl(void);
+extern void sve_save_ctx(uint64_t *sve_ctx, uint64_t *pregs, int save_ffr);
+extern void sve_load_ctx(uint64_t const *sve_ctx, uint64_t const *pregs,
+                         int restore_ffr);
+
+static inline uint16_t sve_zreg_ctx_size(uint16_t vl)
+{
+    /*
+     * Z0-31 registers size in bytes is computed from VL that is in bits, so VL
+     * in bytes is VL/8.
+     */
+    return (vl / 8U) * 32U;
+}
+
+static inline uint16_t sve_ffrreg_ctx_size(uint16_t vl)
+{
+    /* FFR register size is VL/8, which is in bytes (VL/8)/8 */
+    return (vl / 64U);
+}
 
 register_t compute_max_zcr(void)
 {
@@ -45,3 +62,38 @@ uint16_t get_sys_vl_len(void)
     return ((system_cpuinfo.zcr64.bits[0] & ZCR_ELx_LEN_MASK) + 1U) *
             SVE_VL_MULTIPLE_VAL;
 }
+
+int sve_context_init(struct vcpu *v)
+{
+    uint64_t *ctx = _xzalloc(sve_zreg_ctx_size(v->domain->arch.sve_vl_bits) +
+                             sve_ffrreg_ctx_size(v->domain->arch.sve_vl_bits),
+                             L1_CACHE_BYTES);
+
+    if ( !ctx )
+        return -ENOMEM;
+
+    v->arch.vfp.sve_context = ctx;
+
+    return 0;
+}
+
+void sve_context_free(struct vcpu *v)
+{
+    xfree(v->arch.vfp.sve_context);
+}
+
+void sve_save_state(struct vcpu *v)
+{
+    uint64_t *sve_ctx_zreg_end = v->arch.vfp.sve_context +
+            (sve_zreg_ctx_size(v->domain->arch.sve_vl_bits) / sizeof(uint64_t));
+
+    sve_save_ctx(sve_ctx_zreg_end, v->arch.vfp.fpregs, 1);
+}
+
+void sve_restore_state(struct vcpu *v)
+{
+    uint64_t *sve_ctx_zreg_end = v->arch.vfp.sve_context +
+            (sve_zreg_ctx_size(v->domain->arch.sve_vl_bits) / sizeof(uint64_t));
+
+    sve_load_ctx(sve_ctx_zreg_end, v->arch.vfp.fpregs, 1);
+}
xen/arch/arm/arm64/sve_asm.S +141 -0

164 lines not on this page · raw message

xen/arch/arm/arm64/vfp.c +45 -34

97 lines not on this page · raw message

xen/arch/arm/domain.c +12 -0
index 27f38729302b..228cd2f7627e 100644
@@ -159,6 +159,11 @@ static void ctxt_switch_from(struct vcpu *p)
     /* VFP */
     vfp_save_state(p);
 
+#ifdef CONFIG_ARM64_SVE
+    if ( is_sve_domain(p->domain) )
+        p->arch.zcr_el1 = READ_SYSREG(ZCR_EL1);
+#endif
+
     /* VGIC */
     gic_save_state(p);
 
@@ -186,7 +191,10 @@ static void ctxt_switch_to(struct vcpu *n)
 
 #ifdef CONFIG_ARM64_SVE
     if ( is_sve_domain(n->domain) )
+    {
+        WRITE_SYSREG(n->arch.zcr_el1, ZCR_EL1);
         WRITE_SYSREG(n->arch.zcr_el2, ZCR_EL2);
+    }
 #endif
 
     /* VFP */
@@ -559,6 +567,8 @@ int arch_vcpu_create(struct vcpu *v)
     v->arch.cptr_el2 = get_default_cptr_flags();
     if ( is_sve_domain(v->domain) )
     {
+        if ( (rc = sve_context_init(v)) != 0 )
+            goto fail;
         v->arch.cptr_el2 &= ~HCPTR_CP(8);
         v->arch.zcr_el2 = vl_to_zcr(v->domain->arch.sve_vl_bits);
     }
@@ -591,6 +601,8 @@ fail:
 
 void arch_vcpu_destroy(struct vcpu *v)
 {
+    if ( is_sve_domain(v->domain) )
+        sve_context_free(v);
     vcpu_timer_destroy(v);
     vcpu_vgic_free(v);
     free_xenheap_pages(v->arch.stack, STACK_ORDER);
xen/arch/arm/include/asm/arm64/sve.h +13 -0
index f4a660e402ca..28c31b329233 100644
@@ -24,6 +24,10 @@ static inline bool is_vl_valid(uint16_t vl)
 register_t compute_max_zcr(void);
 register_t vl_to_zcr(uint16_t vl);
 uint16_t get_sys_vl_len(void);
+int sve_context_init(struct vcpu *v);
+void sve_context_free(struct vcpu *v);
+void sve_save_state(struct vcpu *v);
+void sve_restore_state(struct vcpu *v);
 
 #else /* !CONFIG_ARM64_SVE */
 
@@ -42,6 +46,15 @@ static inline uint16_t get_sys_vl_len(void)
     return 0;
 }
 
+static inline int sve_context_init(struct vcpu *v)
+{
+    return 0;
+}
+
+static inline void sve_context_free(struct vcpu *v) {}
+static inline void sve_save_state(struct vcpu *v) {}
+static inline void sve_restore_state(struct vcpu *v) {}
+
 #endif
 
 #endif /* _ARM_ARM64_SVE_H */
xen/arch/arm/include/asm/arm64/sysregs.h +3 -0
index 4cabb9eb4d5e..3fdeb9d8cdef 100644
@@ -88,6 +88,9 @@
 #ifndef ID_AA64ISAR2_EL1
 #define ID_AA64ISAR2_EL1            S3_0_C0_C6_2
 #endif
+#ifndef ZCR_EL1
+#define ZCR_EL1                     S3_0_C1_C2_0
+#endif
 
 /* ID registers (imported from arm64/include/asm/sysreg.h in Linux) */
 
xen/arch/arm/include/asm/arm64/vfp.h +10 -0
index e6e8c363bc16..8af714cb8ecc 100644
@@ -6,7 +6,17 @@
 
 struct vfp_state
 {
+    /*
+     * When SVE is enabled for the guest, fpregs memory will be used to
+     * save/restore P0-P15 registers, otherwise it will be used for the V0-V31
+     * registers.
+     */
     uint64_t fpregs[64] __vfp_aligned;
+    /*
+     * When SVE is enabled for the guest, sve_context contains memory to
+     * save/restore Z0-Z31 registers and FFR.
+     */
+    uint64_t *sve_context;
     register_t fpcr;
     register_t fpexc32_el2;
     register_t fpsr;
xen/arch/arm/include/asm/domain.h +1 -0
index e4794a9fd2ab..4d1066750a9b 100644
@@ -195,6 +195,7 @@ struct arch_vcpu
     register_t tpidrro_el0;
 
     /* HYP configuration */
+    register_t zcr_el1;
     register_t zcr_el2;
     register_t cptr_el2;
     register_t hcr_el2;
Signature · 1 line
2.17.1
#14 lore raw

RFC PATCH 6/8 xen/arm: enable Dom0 to use SVE feature

In reply to Luca Fancellu

Add a command line parameter to allow Dom0 the use of SVE resources, the command line parameter dom0_sve controls the feature on this domain and sets the maximum SVE vector length for Dom0.

---
 docs/misc/xen-command-line.pandoc    | 12 ++++++++++++
 xen/arch/arm/arm64/sve.c             |  5 +++++
 xen/arch/arm/domain_build.c          |  4 ++++
 xen/arch/arm/include/asm/arm64/sve.h |  4 ++++
 4 files changed, 25 insertions(+)
docs/misc/xen-command-line.pandoc +12 -0
index 923910f553c5..940a96f4207c 100644
@@ -995,6 +995,18 @@ restrictions set up here. Note that the values to be specified here are
 ACPI PXM ones, not Xen internal node numbers. `relaxed` sets up vCPU
 affinities to prefer but be not limited to the specified node(s).
 
+### dom0_sve (arm)
+> `= <integer>`
+
+> Default: `0`
+
+Enable arm SVE usage for Dom0 domain and sets the maximum SVE vector length.
+Values above 0 means feature is enabled for Dom0, otherwise feature is disabled.
+Possible values are from 0 to maximum 2048, being multiple of 128, that will be
+the maximum vector length.
+Please note that the specified value is a maximum allowed vector length, so if
+the platform supports only a lower value, the lower one will be chosen.
+
 ### dom0_vcpus_pin
 > `= <boolean>`
 
xen/arch/arm/arm64/sve.c +5 -0
index c7b325700fe4..9f8c5d21a59f 100644
@@ -5,10 +5,15 @@
  * Copyright (C) 2022 ARM Ltd.
  */
 
+#include <xen/param.h>
 #include <xen/sched.h>
 #include <xen/sizes.h>
 #include <asm/arm64/sve.h>
 
+/* opt_dom0_sve: allow Dom0 to use SVE and set maximum vector length. */
+unsigned int __initdata opt_dom0_sve;
+integer_param("dom0_sve", opt_dom0_sve);
+
 extern unsigned int sve_get_hw_vl(void);
 extern void sve_save_ctx(uint64_t *sve_ctx, uint64_t *pregs, int save_ffr);
 extern void sve_load_ctx(uint64_t const *sve_ctx, uint64_t const *pregs,
xen/arch/arm/domain_build.c +4 -0
index 829cea8de84f..48c3fdc28063 100644
@@ -26,6 +26,7 @@
 #include <asm/platform.h>
 #include <asm/psci.h>
 #include <asm/setup.h>
+#include <asm/arm64/sve.h>
 #include <asm/cpufeature.h>
 #include <asm/domain_build.h>
 #include <xen/event.h>
@@ -4075,6 +4076,9 @@ void __init create_dom0(void)
     if ( iommu_enabled )
         dom0_cfg.flags |= XEN_DOMCTL_CDF_iommu;
 
+    if ( opt_dom0_sve > 0 )
+        dom0_cfg.arch.sve_vl_bits = opt_dom0_sve;
+
     dom0 = domain_create(0, &dom0_cfg, CDF_privileged | CDF_directmap);
     if ( IS_ERR(dom0) || (alloc_dom0_vcpu0(dom0) == NULL) )
         panic("Error creating domain 0\n");
xen/arch/arm/include/asm/arm64/sve.h +4 -0
index 28c31b329233..dc6e747cec9e 100644
@@ -21,6 +21,8 @@ static inline bool is_vl_valid(uint16_t vl)
 
 #ifdef CONFIG_ARM64_SVE
 
+extern unsigned int opt_dom0_sve;
+
 register_t compute_max_zcr(void);
 register_t vl_to_zcr(uint16_t vl);
 uint16_t get_sys_vl_len(void);
@@ -31,6 +33,8 @@ void sve_restore_state(struct vcpu *v);
 
 #else /* !CONFIG_ARM64_SVE */
 
+#define opt_dom0_sve (0)
+
 static inline register_t compute_max_zcr(void)
 {
     return 0;
Signature · 1 line
2.17.1
#15 lore raw

RFC PATCH 7/8 xen/tools: add sve parameter in XL configuration

In reply to Luca Fancellu

Add sve parameter in XL configuration to allow guests to use SVE feature.

---
 docs/man/xl.cfg.5.pod.in             | 11 +++++++++++
 tools/golang/xenlight/helpers.gen.go |  2 ++
 tools/golang/xenlight/types.gen.go   |  1 +
 tools/include/libxl.h                |  5 +++++
 tools/libs/light/libxl_arm.c         |  2 ++
 tools/libs/light/libxl_types.idl     |  1 +
 tools/xl/xl_parse.c                  | 10 ++++++++++
 7 files changed, 32 insertions(+)
docs/man/xl.cfg.5.pod.in +11 -0
index 024bceeb61b2..60412f7e32a0 100644
@@ -2903,6 +2903,17 @@ Currently, only the "sbsa_uart" model is supported for ARM.
 
 =back
 
+=item B<sve="NUMBER">
+
+To enable SVE, user must specify a number different from zero, maximum 2048 and
+multiple of 128. That value will be the maximum number of SVE registers bits
+that the hypervisor will impose to this guest. If the platform has a lower bits
+value, then the lower value will be chosen.
+A value equal to zero is the default and it means this guest is not allowed to
+use SVE.
+
+=back
+
 =head3 x86
 
 =over 4
tools/golang/xenlight/helpers.gen.go +2 -0
index 3ac4938858f2..7f3b1e758b00 100644
@@ -1117,6 +1117,7 @@ default:
 return fmt.Errorf("invalid union key '%v'", x.Type)}
 x.ArchArm.GicVersion = GicVersion(xc.arch_arm.gic_version)
 x.ArchArm.Vuart = VuartType(xc.arch_arm.vuart)
+x.ArchArm.Sve = int(xc.arch_arm.sve)
 if err := x.ArchX86.MsrRelaxed.fromC(&xc.arch_x86.msr_relaxed);err != nil {
 return fmt.Errorf("converting field ArchX86.MsrRelaxed: %v", err)
 }
@@ -1602,6 +1603,7 @@ default:
 return fmt.Errorf("invalid union key '%v'", x.Type)}
 xc.arch_arm.gic_version = C.libxl_gic_version(x.ArchArm.GicVersion)
 xc.arch_arm.vuart = C.libxl_vuart_type(x.ArchArm.Vuart)
+xc.arch_arm.sve = C.int(x.Sve)
 if err := x.ArchX86.MsrRelaxed.toC(&xc.arch_x86.msr_relaxed); err != nil {
 return fmt.Errorf("converting field ArchX86.MsrRelaxed: %v", err)
 }
tools/golang/xenlight/types.gen.go +1 -0
index 16ce879e3fb7..ed144325682e 100644
@@ -537,6 +537,7 @@ TypeUnion DomainBuildInfoTypeUnion
 ArchArm struct {
 GicVersion GicVersion
 Vuart VuartType
+Sve uint32
 }
 ArchX86 struct {
 MsrRelaxed Defbool
tools/include/libxl.h +5 -0
index d652895075a0..1057962e2e3f 100644
@@ -278,6 +278,11 @@
  */
 #define LIBXL_HAVE_BUILDINFO_ARCH_ARM_TEE 1
 
+/*
+ * libxl_domain_build_info has the arch_arm.sve field.
+ */
+#define LIBXL_HAVE_BUILDINFO_ARCH_ARM_SVE 1
+
 /*
  * LIBXL_HAVE_SOFT_RESET indicates that libxl supports performing
  * 'soft reset' for domains and there is 'soft_reset' shutdown reason
tools/libs/light/libxl_arm.c +2 -0
index ddc7b2a15975..31f30e054bf4 100644
@@ -211,6 +211,8 @@ int libxl__arch_domain_prepare_config(libxl__gc *gc,
         return ERROR_FAIL;
     }
 
+    config->arch.sve_vl_bits = d_config->b_info.arch_arm.sve;
+
     return 0;
 }
 
tools/libs/light/libxl_types.idl +1 -0
index 0cfad8508dbd..27e22523c7c2 100644
@@ -663,6 +663,7 @@ libxl_domain_build_info = Struct("domain_build_info",[
 
     ("arch_arm", Struct(None, [("gic_version", libxl_gic_version),
                                ("vuart", libxl_vuart_type),
+                               ("sve", uint32),
                               ])),
     ("arch_x86", Struct(None, [("msr_relaxed", libxl_defbool),
                               ])),
tools/xl/xl_parse.c +10 -0
index 853e9f357a1a..49b2f28807e5 100644
@@ -2828,6 +2828,16 @@ skip_usbdev:
         }
     }
 
+    if (!xlu_cfg_get_long (config, "sve", &l, 0)) {
+        if (((l % 128) != 0) || (l > 2048)) {
+            fprintf(stderr,
+                    "Invalid sve value: %ld. Needs to be <= 2048 and multiple"
+                    " of 128\n", l);
+            exit(-ERROR_FAIL);
+        }
+        b_info->arch_arm.sve = l;
+    }
+
     parse_vkb_list(config, d_config);
 
     d_config->virtios = NULL;
Signature · 1 line
2.17.1
#16 lore raw

RFC PATCH 8/8 xen/arm: add sve property for dom0less domUs

In reply to Luca Fancellu

Add a device tree property in the dom0less domU configuration to enable the guest to use SVE.

Update documentation.

---
 docs/misc/arm/device-tree/booting.txt | 7 +++++++
 xen/arch/arm/domain_build.c           | 7 +++++++
 2 files changed, 14 insertions(+)
docs/misc/arm/device-tree/booting.txt +7 -0
index 3879340b5e0a..3d1ce652317e 100644
@@ -193,6 +193,13 @@ with the following properties:
     Optional. Handle to a xen,cpupool device tree node that identifies the
     cpupool where the guest will be started at boot.
 
+- sve
+
+    Optional. A number that, when above 0, enables SVE for this guest and sets
+    its maximum SVE vector length. The default value is 0, that means this
+    guest is not allowed to use SVE, the maximum value allowed is 2048, any
+    other value must be multiple of 128.
+
 - xen,enhanced
 
     A string property. Possible property values are:
xen/arch/arm/domain_build.c +7 -0
index 48c3fdc28063..05b2bfc9195f 100644
@@ -3959,6 +3959,13 @@ void __init create_domUs(void)
             d_cfg.max_maptrack_frames = val;
         }
 
+        if ( dt_property_read_u32(node, "sve", &val) )
+        {
+            if ( val > UINT16_MAX )
+                panic("sve property value (%"PRIu32") overflow\n", val);
+            d_cfg.arch.sve_vl_bits = val;
+        }
+
         /*
          * The variable max_init_domid is initialized with zero, so here it's
          * very important to use the pre-increment operator to call
Signature · 1 line
2.17.1
#17 lore raw
In reply to Luca Fancellu

Hi Luca,

Quoted 41 lines · Luca Fancellu

This serie is introducing the possibility for Dom0 and DomU guests to use sve/sve2 instructions.

SVE feature introduces new instruction and registers to improve performances on floating point operations.

The SVE feature is advertised using the ID_AA64PFR0_EL1 register, SVE field, and when available the ID_AA64ZFR0_EL1 register provides additional information about the implemented version and other SVE feature.

New registers added by the SVE feature are Z0-Z31, P0-P15, FFR, ZCR_ELx.

Z0-Z31 are scalable vector register whose size is implementation defined and goes from 128 bits to maximum 2048, the term vector length will be used to refer to this quantity.
P0-P15 are predicate registers and the size is the vector length divided by 8, same size is the FFR (First Fault Register).
ZCR_ELx is a register that can control and restrict the maximum vector length used by the <x> exception level and all the lower exception levels, so for example EL3 can restrict the vector length usable by EL3,2,1,0.

The platform has a maximum implemented vector length, so for every value written in ZCR register, if this value is above the implemented length, then the lower value will be used. The RDVL instruction can be used to check what vector length is the HW using after setting ZCR.

For an SVE guest, the V0-V31 registers are part of the Z0-Z31, so there is no need to save them separately, saving Z0-Z31 will save implicitly also V0-V31.

SVE usage can be trapped using a flag in CPTR_EL2, hence in this serie the register is added to the domain state, to be able to trap only the guests that are not allowed to use SVE.

This serie is introducing a command line parameter to enable Dom0 to use SVE and to set its maximum vector length that by default is 0 which means the guest is not allowed to use SVE. Values from 128 to 2048 mean the guest can use SVE with the selected value used as maximum allowed vector length (which could be lower if the implemented one is lower).
For DomUs, an XL parameter with the same way of use is introduced and a dom0less DTB binding is created.

Quoted 3 lines

The context switch is the most critical part because there can be big registers to be saved, in this serie an easy approach is used and the context is saved/restored every time for the guests that are allowed to use SVE.

This would be OK for an initial approach. But I would be worry to officially support SVE because of the potential large impact on other users.

What's the long term plan?

Cheers,

Signature · 1 line
Julien Grall
#18 lore raw
Quoted 43 lines · Julien Grall

Hi Luca,

Quoted 27 lines · Luca Fancellu

This serie is introducing the possibility for Dom0 and DomU guests to use sve/sve2 instructions.
SVE feature introduces new instruction and registers to improve performances on floating point operations.
The SVE feature is advertised using the ID_AA64PFR0_EL1 register, SVE field, and when available the ID_AA64ZFR0_EL1 register provides additional information about the implemented version and other SVE feature.
New registers added by the SVE feature are Z0-Z31, P0-P15, FFR, ZCR_ELx. Z0-Z31 are scalable vector register whose size is implementation defined and goes from 128 bits to maximum 2048, the term vector length will be used to refer to this quantity.
P0-P15 are predicate registers and the size is the vector length divided by 8, same size is the FFR (First Fault Register).
ZCR_ELx is a register that can control and restrict the maximum vector length used by the <x> exception level and all the lower exception levels, so for example EL3 can restrict the vector length usable by EL3,2,1,0.
The platform has a maximum implemented vector length, so for every value written in ZCR register, if this value is above the implemented length, then the lower value will be used. The RDVL instruction can be used to check what vector length is the HW using after setting ZCR.
For an SVE guest, the V0-V31 registers are part of the Z0-Z31, so there is no need to save them separately, saving Z0-Z31 will save implicitly also V0-V31. SVE usage can be trapped using a flag in CPTR_EL2, hence in this serie the register is added to the domain state, to be able to trap only the guests that are not allowed to use SVE.
This serie is introducing a command line parameter to enable Dom0 to use SVE and to set its maximum vector length that by default is 0 which means the guest is

Quoted 8 lines

not allowed to use SVE. Values from 128 to 2048 mean the guest can use SVE with the selected value used as maximum allowed vector length (which could be lower if the implemented one is lower).
For DomUs, an XL parameter with the same way of use is introduced and a dom0less DTB binding is created.
The context switch is the most critical part because there can be big registers to be saved, in this serie an easy approach is used and the context is saved/restored every time for the guests that are allowed to use SVE.

Quoted 3 lines

This would be OK for an initial approach. But I would be worry to officially support SVE because of the potential large impact on other users.

What's the long term plan?

Hi Julien,

For the future we can plan some work and decide together how to handle the context switch, we might need some suggestions from you (arm maintainers) to design that part in the best way for functional and security perspective.

For now we might flag the feature as unsupported, explaining in the Kconfig help that switching between SVE and non-SVE guests, or between SVE guests, might add latency compared to switching between non-SVE guests.

What do you think?

Cheers,
Luca

Quoted 5 lines

Cheers,

Signature · 1 line
Julien Grall
#19 lore raw

Hi Luca,

Quoted 40 lines · Luca Fancellu
Quoted 40 lines · Julien Grall
Quoted 27 lines · Luca Fancellu

This serie is introducing the possibility for Dom0 and DomU guests to use sve/sve2 instructions.
SVE feature introduces new instruction and registers to improve performances on floating point operations.
The SVE feature is advertised using the ID_AA64PFR0_EL1 register, SVE field, and when available the ID_AA64ZFR0_EL1 register provides additional information about the implemented version and other SVE feature.
New registers added by the SVE feature are Z0-Z31, P0-P15, FFR, ZCR_ELx. Z0-Z31 are scalable vector register whose size is implementation defined and goes from 128 bits to maximum 2048, the term vector length will be used to refer to this quantity.
P0-P15 are predicate registers and the size is the vector length divided by 8, same size is the FFR (First Fault Register).
ZCR_ELx is a register that can control and restrict the maximum vector length used by the <x> exception level and all the lower exception levels, so for example EL3 can restrict the vector length usable by EL3,2,1,0.
The platform has a maximum implemented vector length, so for every value written in ZCR register, if this value is above the implemented length, then the lower value will be used. The RDVL instruction can be used to check what vector length is the HW using after setting ZCR.
For an SVE guest, the V0-V31 registers are part of the Z0-Z31, so there is no need to save them separately, saving Z0-Z31 will save implicitly also V0-V31. SVE usage can be trapped using a flag in CPTR_EL2, hence in this serie the register is added to the domain state, to be able to trap only the guests that are not allowed to use SVE.
This serie is introducing a command line parameter to enable Dom0 to use SVE and to set its maximum vector length that by default is 0 which means the guest is

Quoted 8 lines

not allowed to use SVE. Values from 128 to 2048 mean the guest can use SVE with the selected value used as maximum allowed vector length (which could be lower if the implemented one is lower).
For DomUs, an XL parameter with the same way of use is introduced and a dom0less DTB binding is created.
The context switch is the most critical part because there can be big registers to be saved, in this serie an easy approach is used and the context is saved/restored every time for the guests that are allowed to use SVE.

This would be OK for an initial approach. But I would be worry to officially support SVE because of the potential large impact on other users.

Quoted 7 lines
Quoted 1 line

What's the long term plan?

Hi Julien,

For the future we can plan some work and decide together how to handle the context switch, we might need some suggestions from you (arm maintainers) to design that part in the best way for functional and security perspective.

I think SVE will need to be lazily saved/restored. So on context switch, we would tell that the context belongs to the a previous domain. The first time after the current domain tries to access SVE, then we would load it.

Quoted 4 lines

For now we might flag the feature as unsupported, explaining in the Kconfig help that switching between SVE and non-SVE guests, or between SVE guests, might add latency compared to switching between non-SVE guests.

I am OK with that. I actually like the idea to spell it out because that helps us to remember what are the gaps in the code :).

Cheers,

Signature · 1 line
Julien Grall
#20 lore raw

Hi Julien,

Quoted 43 lines · Julien Grall

Hi Luca,

Quoted 38 lines · Luca Fancellu
Quoted 38 lines · Julien Grall
Quoted 35 lines · Luca Fancellu

This serie is introducing the possibility for Dom0 and DomU guests to use sve/sve2 instructions.
SVE feature introduces new instruction and registers to improve performances on floating point operations.
The SVE feature is advertised using the ID_AA64PFR0_EL1 register, SVE field, and when available the ID_AA64ZFR0_EL1 register provides additional information about the implemented version and other SVE feature.
New registers added by the SVE feature are Z0-Z31, P0-P15, FFR, ZCR_ELx. Z0-Z31 are scalable vector register whose size is implementation defined and goes from 128 bits to maximum 2048, the term vector length will be used to refer to this quantity.
P0-P15 are predicate registers and the size is the vector length divided by 8, same size is the FFR (First Fault Register).
ZCR_ELx is a register that can control and restrict the maximum vector length used by the <x> exception level and all the lower exception levels, so for example EL3 can restrict the vector length usable by EL3,2,1,0.
The platform has a maximum implemented vector length, so for every value written in ZCR register, if this value is above the implemented length, then the lower value will be used. The RDVL instruction can be used to check what vector length is the HW using after setting ZCR.
For an SVE guest, the V0-V31 registers are part of the Z0-Z31, so there is no need to save them separately, saving Z0-Z31 will save implicitly also V0-V31. SVE usage can be trapped using a flag in CPTR_EL2, hence in this serie the register is added to the domain state, to be able to trap only the guests that are not allowed to use SVE.
This serie is introducing a command line parameter to enable Dom0 to use SVE and to set its maximum vector length that by default is 0 which means the guest is not allowed to use SVE. Values from 128 to 2048 mean the guest can use SVE with the selected value used as maximum allowed vector length (which could be lower if the implemented one is lower).
For DomUs, an XL parameter with the same way of use is introduced and a dom0less DTB binding is created.
The context switch is the most critical part because there can be big registers to be saved, in this serie an easy approach is used and the context is saved/restored every time for the guests that are allowed to use SVE.

Quoted 8 lines
Quoted 7 lines
Quoted 3 lines

This would be OK for an initial approach. But I would be worry to officially support SVE because of the potential large impact on other users.

What's the long term plan?

Hi Julien,
For the future we can plan some work and decide together how to handle the context switch, we might need some suggestions from you (arm maintainers) to design that part in the best way for functional and security perspective.

I think SVE will need to be lazily saved/restored. So on context switch, we would tell that the context belongs to the a previous domain. The first time after the current domain tries to access SVE, then we would load it.

We should try to prevent those kind of things because it makes the real time analysis a lot more complex.
The only use case where this would make the system a lot faster is if there is only one guest using SVE (which might be a use case), other than that case this will just create delays when someone else is trying to use SVE instead of having a fix delay at context switch.

Quoted 6 lines
Quoted 4 lines

For now we might flag the feature as unsupported, explaining in the Kconfig help that switching between SVE and non-SVE guests, or between SVE guests, might add latency compared to switching between non-SVE guests.

I am OK with that. I actually like the idea to spell it out because that helps us to remember what are the gaps in the code :).

I like this solution to.

Cheers
Bertrand

Quoted 5 lines

Cheers,

Signature · 1 line
Julien Grall
#21 lore raw

Hi Bertrand,

Quoted 45 lines · Bertrand Marquis
Quoted 45 lines · Julien Grall

Hi Luca,

Quoted 40 lines · Luca Fancellu
Quoted 40 lines · Julien Grall
Quoted 27 lines · Luca Fancellu

This serie is introducing the possibility for Dom0 and DomU guests to use sve/sve2 instructions.
SVE feature introduces new instruction and registers to improve performances on floating point operations.
The SVE feature is advertised using the ID_AA64PFR0_EL1 register, SVE field, and when available the ID_AA64ZFR0_EL1 register provides additional information about the implemented version and other SVE feature.
New registers added by the SVE feature are Z0-Z31, P0-P15, FFR, ZCR_ELx. Z0-Z31 are scalable vector register whose size is implementation defined and goes from 128 bits to maximum 2048, the term vector length will be used to refer to this quantity.
P0-P15 are predicate registers and the size is the vector length divided by 8, same size is the FFR (First Fault Register).
ZCR_ELx is a register that can control and restrict the maximum vector length used by the <x> exception level and all the lower exception levels, so for example EL3 can restrict the vector length usable by EL3,2,1,0.
The platform has a maximum implemented vector length, so for every value written in ZCR register, if this value is above the implemented length, then the lower value will be used. The RDVL instruction can be used to check what vector length is the HW using after setting ZCR.
For an SVE guest, the V0-V31 registers are part of the Z0-Z31, so there is no need to save them separately, saving Z0-Z31 will save implicitly also V0-V31. SVE usage can be trapped using a flag in CPTR_EL2, hence in this serie the register is added to the domain state, to be able to trap only the guests that are not allowed to use SVE.
This serie is introducing a command line parameter to enable Dom0 to use SVE and to set its maximum vector length that by default is 0 which means the guest is

Quoted 8 lines

not allowed to use SVE. Values from 128 to 2048 mean the guest can use SVE with the selected value used as maximum allowed vector length (which could be lower if the implemented one is lower).
For DomUs, an XL parameter with the same way of use is introduced and a dom0less DTB binding is created.
The context switch is the most critical part because there can be big registers to be saved, in this serie an easy approach is used and the context is saved/restored every time for the guests that are allowed to use SVE.

This would be OK for an initial approach. But I would be worry to officially support SVE because of the potential large impact on other users.

Quoted 8 lines
Quoted 6 lines
Quoted 5 lines
Quoted 1 line

What's the long term plan?

Hi Julien,
For the future we can plan some work and decide together how to handle the context switch, we might need some suggestions from you (arm maintainers) to design that part in the best way for functional and security perspective.

I think SVE will need to be lazily saved/restored. So on context switch, we would tell that the context belongs to the a previous domain. The first time after the current domain tries to access SVE, then we would load it.

We should try to prevent those kind of things because it makes the real time analysis a lot more complex.

The choice of SVE (including the vector length) is per-domain. If all the VMs are using the same vector length. Then the delay would indeed be fixed. Otherwise, the delay will vary depending on the scheduling choice.

It is not clear to me how this is better for real time analysis.

Quoted 1 line

The only use case where this would make the system a lot faster is if there is only one guest using SVE (which might be a use case), other than that case this will just create delays when someone else is trying to use SVE instead of having a fix delay at context swit

Even in the case you mention, I think it will highly depend on the cost of context switching SVE. I have been told this is quite large, and one surely don't want to spend an extra thousand cycles when receiving an interrupt (I don't expect handler to use SVE).

I think we need to understand the workload (and cost) in order to decide whether it should be eager/lazy.

At least, I know that in Linux, only the part common with VFP are guaranteed to be preserved (see [1]). So the expectation seems that SVE use will be short-lived.

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

For now we might flag the feature as unsupported, explaining in the Kconfig help that switching between SVE and non-SVE guests, or between SVE guests, might add latency compared to switching between non-SVE guests.

Quoted 13 lines
Quoted 1 line

I am OK with that. I actually like the idea to spell it out because that helps us to remember what are the gaps in the code :).

I like this solution to.

Cheers
Bertrand

Quoted 5 lines

Cheers,

Signature · 1 line
Julien Grall

[1] https://www.kernel.org/doc/Documentation/arm64/sve.txt

Signature · 1 line
Julien Grall