Skip to contentSkip to conversation

Fifth MPU Series

PATCH Superseded Luca Fancellu 3 participants 9 messages Apr 8 – Apr 13

v3 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v3 cover Fifth MPU Series

This series continues to further the ongoing work to introduce support for MPU systems in Xen.

The patches in this series aim to implement MPU specific p2m functions.

Harry Ramsey (2):
arm/mpu: Introduce v8r_el1_msa device tree property for domains arm/mpu: Implement p2m tables

Penny Zheng (1):
arm/mpu: implement setup_virt_paging for MPU system

 docs/misc/arm/device-tree/booting.txt    | 12 ++++
 xen/arch/arm/arm64/mpu/p2m.c             | 80 +++++++++++++++++++++++-
 xen/arch/arm/dom0less-build.c            | 24 +++++++
 xen/arch/arm/domain.c                    |  4 ++
 xen/arch/arm/include/asm/arm32/mpu.h     |  2 +
 xen/arch/arm/include/asm/arm64/mpu.h     |  2 +
 xen/arch/arm/include/asm/arm64/sysregs.h |  4 ++
 xen/arch/arm/include/asm/cpufeature.h    | 13 +++-
 xen/arch/arm/include/asm/domain.h        |  7 +++
 xen/arch/arm/include/asm/mpu.h           |  5 ++
 xen/arch/arm/include/asm/mpu/p2m.h       | 12 ++++
 xen/arch/arm/include/asm/p2m.h           |  5 ++
 xen/arch/arm/include/asm/processor.h     |  8 +++
 xen/arch/arm/mpu/arm32/mm.c              |  5 ++
 xen/arch/arm/mpu/arm64/mm.c              |  5 ++
 xen/arch/arm/mpu/p2m.c                   | 78 ++++++++++++++++++++++-
 xen/include/public/arch-arm.h            |  2 +
 17 files changed, 261 insertions(+), 7 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH v3 1/3 arm/mpu: implement setup_virt_paging for MPU system

From: Penny Zheng <[email protected]>

Implement setup_virt_paging for aarch64 MPU systems, taking tare of stage 2 address translation regime, IPA bits, supported VMID length configuration and vtcr_el2/vstcr_el2 register programming.

Implement also the Armv8-R specific changes to ID_AA64MMFR0_EL1, related to the supported memory system architecture (PMSA/VMSA) and check that when MPU is built, the underlying HW is compatible with PMSA. By default MPU at EL2 and EL1 is required.

---
v3:
- Refactor unused code to more relevant commits.
- Add P2M print information
- Formatting issues
- Update commit message
v2:
- Seperate commit into multiple commits

---
 xen/arch/arm/arm64/mpu/p2m.c             | 80 +++++++++++++++++++++++-
 xen/arch/arm/include/asm/arm64/sysregs.h |  4 ++
 xen/arch/arm/include/asm/cpufeature.h    | 13 +++-
 xen/arch/arm/include/asm/processor.h     |  8 +++
 4 files changed, 101 insertions(+), 4 deletions(-)
xen/arch/arm/arm64/mpu/p2m.c +79 -1
index b6d8b2777b58..fda512dc7c8f 100644
@@ -2,11 +2,89 @@
 
 #include <xen/bug.h>
 #include <xen/init.h>
+#include <xen/lib.h>
 #include <asm/p2m.h>
 
 void __init setup_virt_paging(void)
 {
-    BUG_ON("unimplemented");
+    register_t vtcr_el2 = READ_SYSREG(VTCR_EL2);
+    register_t vstcr_el2 = READ_SYSREG(VSTCR_EL2);
+
+    /* PA size */
+    const unsigned int pa_range_info[] = {32, 36, 40, 42, 44, 48, 52, 0,
+                                          /* Invalid */};
+
+    /*
+     * Restrict "p2m_ipa_bits" if needed. As P2M table is always configured
+     * with IPA bits == PA bits, compare against "pabits".
+     */
+    if ( pa_range_info[system_cpuinfo.mm64.pa_range] < p2m_ipa_bits )
+        p2m_ipa_bits = pa_range_info[system_cpuinfo.mm64.pa_range];
+
+    /*
+     * The MSA and MSA_frac fields in the ID_AA64MMFR0_EL1 register identify the
+     * memory system configurations supported. In Armv8-R AArch64, the
+     * only permitted value for ID_AA64MMFR0_EL1.MSA is 0b1111.
+     */
+    if ( system_cpuinfo.mm64.msa != MM64_MSA_PMSA_SUPPORT )
+        goto fault;
+
+    /* Permitted values for ID_AA64MMFR0_EL1.MSA_frac are 0b0001 and 0b0010. */
+    if ( (system_cpuinfo.mm64.msa_frac != MM64_MSA_FRAC_PMSA_SUPPORT) &&
+         (system_cpuinfo.mm64.msa_frac != MM64_MSA_FRAC_VMSA_SUPPORT) )
+        goto fault;
+
+    /* Stage 1 EL1&0 translation regime uses PMSAv8 by default */
+    vtcr_el2 &= ~VTCR_MSA;
+
+    /*
+     * Clear VTCR_EL2.NSA bit to configure non-secure stage 2 translation output
+     * address space to access the Secure PA space as Armv8r only implements
+     * secure state.
+     */
+    vtcr_el2 &= ~VTCR_NSA;
+
+    /*
+     * cpuinfo sanitization makes sure we support 16bits VMID only if all cores
+     * are supporting it.
+     *
+     * Set the VS bit only if 16 bit VIMD is supported.
+     */
+    if ( system_cpuinfo.mm64.vmid_bits == MM64_VMID_16_BITS_SUPPORT )
+    {
+        vtcr_el2 |= VTCR_VS;
+        max_vmid = MAX_VMID_16_BIT;
+    }
+    else
+        vtcr_el2 &= ~VTCR_VS;
+
+    p2m_vmid_allocator_init();
+
+    WRITE_SYSREG(vtcr_el2, VTCR_EL2);
+
+    /*
+     * VSTCR_EL2.SA defines secure stage 2 translation output address space.
+     * To make sure that all stage 2 translations for the Secure PA space access
+     * the Secure PA space, we keep SA bit as 0.
+     *
+     * VSTCR_EL2.SC is NS check enable bit. To make sure that Stage 2 NS
+     * configuration is checked against stage 1 NS configuration in EL1&0
+     * translation regime for the given address, and generates a fault if they
+     * are different, we set SC bit 1.
+     */
+    vstcr_el2 &= ~VSTCR_EL2_SA;
+    vstcr_el2 |= VSTCR_EL2_SC;
+    WRITE_SYSREG(vstcr_el2, VSTCR_EL2);
+
+    printk("P2M: %d-bit IPA with %d-bit PA and %d-bit VMID\n",
+           p2m_ipa_bits,
+           pa_range_info[system_cpuinfo.mm64.pa_range],
+           ( MAX_VMID == MAX_VMID_16_BIT ) ? 16 : 8);
+
+    return;
+
+ fault:
+    panic("Hardware with no PMSAv8-64 support in any translation regime\n");
 }
 
 /*
xen/arch/arm/include/asm/arm64/sysregs.h +4 -0
index 19d409d3eb5a..f3c11d871e86 100644
@@ -462,6 +462,10 @@
 #define ZCR_ELx_LEN_SIZE             9
 #define ZCR_ELx_LEN_MASK             0x1ff
 
+/* Virtualization Secure Translation Control Register */
+#define VSTCR_EL2_SA                 (_AC(0x1,U) << 30)
+#define VSTCR_EL2_SC                 (_AC(0x1,U) << 20)
+
 #ifdef CONFIG_MPU
 /*
  * The Armv8-R AArch64 architecture always executes code in Secure
xen/arch/arm/include/asm/cpufeature.h +10 -3
index 13353c8e1ad1..bf902a397068 100644
@@ -248,6 +248,12 @@ struct cpuinfo_arm {
             unsigned long tgranule_16K:4;
             unsigned long tgranule_64K:4;
             unsigned long tgranule_4K:4;
+#ifdef CONFIG_MPU
+            unsigned long __res0:16;
+            unsigned long msa:4;
+            unsigned long msa_frac:4;
+            unsigned long __res1:8;
+#else
             unsigned long tgranule_16k_2:4;
             unsigned long tgranule_64k_2:4;
             unsigned long tgranule_4k_2:4;
@@ -255,6 +261,7 @@ struct cpuinfo_arm {
             unsigned long __res0:8;
             unsigned long fgt:4;
             unsigned long ecv:4;
+#endif
 
             /* MMFR1 */
             unsigned long hafdbs:4;
@@ -267,13 +274,13 @@ struct cpuinfo_arm {
             unsigned long xnx:4;
             unsigned long twed:4;
             unsigned long ets:4;
-            unsigned long __res1:4;
+            unsigned long __res2:4;
             unsigned long afp:4;
-            unsigned long __res2:12;
+            unsigned long __res3:12;
             unsigned long ecbhb:4;
 
             /* MMFR2 */
-            unsigned long __res3:64;
+            unsigned long __res4:64;
         };
     } mm64;
 
xen/arch/arm/include/asm/processor.h +8 -0
index 1a48c9ff3b39..895d7cd50244 100644
@@ -403,6 +403,9 @@
 
 #define VTCR_RES1       (_AC(1,UL)<<31)
 
+#define VTCR_MSA        (_AC(0x1,UL)<<31)
+#define VTCR_NSA        (_AC(0x1,UL)<<30)
+
 /* HCPTR Hyp. Coprocessor Trap Register */
 #define HCPTR_TAM       ((_AC(1,U)<<30))
 #define HCPTR_TTA       ((_AC(1,U)<<20))        /* Trap trace registers */
@@ -464,6 +467,11 @@
 #define MM64_VMID_16_BITS_SUPPORT   0x2
 #endif
 
+#define MM64_MSA_PMSA_SUPPORT       0xf
+#define MM64_MSA_FRAC_NONE_SUPPORT  0x0
+#define MM64_MSA_FRAC_PMSA_SUPPORT  0x1
+#define MM64_MSA_FRAC_VMSA_SUPPORT  0x2
+
 #ifndef __ASSEMBLER__
 
 extern register_t __cpu_logical_map[];
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH v3 1/3] arm/mpu: implement setup_virt_paging for MPU system

Quoted 85 lines · Luca Fancellu

From: Penny Zheng <[email protected]>

Implement setup_virt_paging for aarch64 MPU systems, taking tare of stage 2 address translation regime, IPA bits, supported VMID length configuration and vtcr_el2/vstcr_el2 register programming.

Implement also the Armv8-R specific changes to ID_AA64MMFR0_EL1, related to the supported memory system architecture (PMSA/VMSA) and check that when MPU is built, the underlying HW is compatible with PMSA. By default MPU at EL2 and EL1 is required.

---
v3:
- Refactor unused code to more relevant commits.
- Add P2M print information
- Formatting issues
- Update commit message
v2:
- Seperate commit into multiple commits

---
 xen/arch/arm/arm64/mpu/p2m.c             | 80 +++++++++++++++++++++++-
 xen/arch/arm/include/asm/arm64/sysregs.h |  4 ++
 xen/arch/arm/include/asm/cpufeature.h    | 13 +++-
 xen/arch/arm/include/asm/processor.h     |  8 +++
 4 files changed, 101 insertions(+), 4 deletions(-)
xen/arch/arm/arm64/mpu/p2m.c +41 -1
index b6d8b2777b58..fda512dc7c8f 100644
@@ -2,11 +2,89 @@
 
 #include <xen/bug.h>
 #include <xen/init.h>
+#include <xen/lib.h>
 #include <asm/p2m.h>
 
 void __init setup_virt_paging(void)
 {
-    BUG_ON("unimplemented");
+    register_t vtcr_el2 = READ_SYSREG(VTCR_EL2);
+    register_t vstcr_el2 = READ_SYSREG(VSTCR_EL2);
+
+    /* PA size */
+    const unsigned int pa_range_info[] = {32, 36, 40, 42, 44, 48, 52, 0,
+                                          /* Invalid */};
+
+    /*
+     * Restrict "p2m_ipa_bits" if needed. As P2M table is always configured
+     * with IPA bits == PA bits, compare against "pabits".
+     */
+    if ( pa_range_info[system_cpuinfo.mm64.pa_range] < p2m_ipa_bits )
+        p2m_ipa_bits = pa_range_info[system_cpuinfo.mm64.pa_range];
+
+    /*
+     * The MSA and MSA_frac fields in the ID_AA64MMFR0_EL1 register identify the
+     * memory system configurations supported. In Armv8-R AArch64, the
+     * only permitted value for ID_AA64MMFR0_EL1.MSA is 0b1111.
+     */
+    if ( system_cpuinfo.mm64.msa != MM64_MSA_PMSA_SUPPORT )
+        goto fault;
+
+    /* Permitted values for ID_AA64MMFR0_EL1.MSA_frac are 0b0001 and 0b0010. */
+    if ( (system_cpuinfo.mm64.msa_frac != MM64_MSA_FRAC_PMSA_SUPPORT) &&
+         (system_cpuinfo.mm64.msa_frac != MM64_MSA_FRAC_VMSA_SUPPORT) )
+        goto fault;
+
+    /* Stage 1 EL1&0 translation regime uses PMSAv8 by default */
+    vtcr_el2 &= ~VTCR_MSA;
+
+    /*
+     * Clear VTCR_EL2.NSA bit to configure non-secure stage 2 translation output
+     * address space to access the Secure PA space as Armv8r only implements
+     * secure state.
+     */
+    vtcr_el2 &= ~VTCR_NSA;
+
+    /*
+     * cpuinfo sanitization makes sure we support 16bits VMID only if all cores
+     * are supporting it.

xen/arch/arm/arm64/mpu/p2m.c line 59

Quoted 10 lines
xen/arch/arm/arm64/mpu/p2m.c +10 -0
+     *
+     * Set the VS bit only if 16 bit VIMD is supported.
+     */
+    if ( system_cpuinfo.mm64.vmid_bits == MM64_VMID_16_BITS_SUPPORT )
+    {
+        vtcr_el2 |= VTCR_VS;
+        max_vmid = MAX_VMID_16_BIT;
+    }
+    else
+        vtcr_el2 &= ~VTCR_VS;

This is the last change to vtcr_el2. Why do you put p2m_vmid_allocator_init() in-between this and write to vtcr_el2 register? It looks odd.

Quoted 10 lines
xen/arch/arm/arm64/mpu/p2m.c +10 -0
+
+    p2m_vmid_allocator_init();
+
+    WRITE_SYSREG(vtcr_el2, VTCR_EL2);
+
+    /*
+     * VSTCR_EL2.SA defines secure stage 2 translation output address space.
+     * To make sure that all stage 2 translations for the Secure PA space access
+     * the Secure PA space, we keep SA bit as 0.
+     *

xen/arch/arm/arm64/mpu/p2m.c line 79

Quoted 10 lines
xen/arch/arm/arm64/mpu/p2m.c +10 -0
+     * VSTCR_EL2.SC is NS check enable bit. To make sure that Stage 2 NS
+     * configuration is checked against stage 1 NS configuration in EL1&0
+     * translation regime for the given address, and generates a fault if they
+     * are different, we set SC bit 1.
+     */
+    vstcr_el2 &= ~VSTCR_EL2_SA;
+    vstcr_el2 |= VSTCR_EL2_SC;
+    WRITE_SYSREG(vstcr_el2, VSTCR_EL2);
+
+    printk("P2M: %d-bit IPA with %d-bit PA and %d-bit VMID\n",

All these are unsigned, so %u.

~Michal

#3 lore raw

Re: [PATCH v3 1/3] arm/mpu: implement setup_virt_paging for MPU system

Hi Michal,

Quoted 48 lines
Quoted 48 lines

void __init setup_virt_paging(void)
{

xen/arch/arm/arm64/mpu/p2m.c +44 -1
-    BUG_ON("unimplemented");
+    register_t vtcr_el2 = READ_SYSREG(VTCR_EL2);
+    register_t vstcr_el2 = READ_SYSREG(VSTCR_EL2);
+
+    /* PA size */
+    const unsigned int pa_range_info[] = {32, 36, 40, 42, 44, 48, 52, 0,
+                                          /* Invalid */};
+
+    /*
+     * Restrict "p2m_ipa_bits" if needed. As P2M table is always configured
+     * with IPA bits == PA bits, compare against "pabits".
+     */
+    if ( pa_range_info[system_cpuinfo.mm64.pa_range] < p2m_ipa_bits )
+        p2m_ipa_bits = pa_range_info[system_cpuinfo.mm64.pa_range];
+
+    /*
+     * The MSA and MSA_frac fields in the ID_AA64MMFR0_EL1 register identify the
+     * memory system configurations supported. In Armv8-R AArch64, the
+     * only permitted value for ID_AA64MMFR0_EL1.MSA is 0b1111.
+     */
+    if ( system_cpuinfo.mm64.msa != MM64_MSA_PMSA_SUPPORT )
+        goto fault;
+
+    /* Permitted values for ID_AA64MMFR0_EL1.MSA_frac are 0b0001 and 0b0010. */
+    if ( (system_cpuinfo.mm64.msa_frac != MM64_MSA_FRAC_PMSA_SUPPORT) &&
+         (system_cpuinfo.mm64.msa_frac != MM64_MSA_FRAC_VMSA_SUPPORT) )
+        goto fault;
+
+    /* Stage 1 EL1&0 translation regime uses PMSAv8 by default */
+    vtcr_el2 &= ~VTCR_MSA;
+
+    /*
+     * Clear VTCR_EL2.NSA bit to configure non-secure stage 2 translation output
+     * address space to access the Secure PA space as Armv8r only implements
+     * secure state.
+     */
+    vtcr_el2 &= ~VTCR_NSA;
+
+    /*
+     * cpuinfo sanitization makes sure we support 16bits VMID only if all cores
+     * are supporting it.
+     *
+     * Set the VS bit only if 16 bit VIMD is supported.
+     */
+    if ( system_cpuinfo.mm64.vmid_bits == MM64_VMID_16_BITS_SUPPORT )
Quoted 8 lines

xen/arch/arm/arm64/mpu/p2m.c line 59

Quoted 6 lines
xen/arch/arm/arm64/mpu/p2m.c +6 -0
+    {
+        vtcr_el2 |= VTCR_VS;
+        max_vmid = MAX_VMID_16_BIT;
+    }
+    else
+        vtcr_el2 &= ~VTCR_VS;

This is the last change to vtcr_el2. Why do you put p2m_vmid_allocator_init() in-between this and write to vtcr_el2 register? It looks odd.

ok I can have the write to the register before calling p2m_vmid_allocator_init, in the end what matters is only to call p2m_vmid_allocator_init() after setting max_vmid.

Quoted 14 lines
Quoted 14 lines
xen/arch/arm/arm64/mpu/p2m.c +13 -0
+
+    p2m_vmid_allocator_init();
+
+    WRITE_SYSREG(vtcr_el2, VTCR_EL2);
+
+    /*
+     * VSTCR_EL2.SA defines secure stage 2 translation output address space.
+     * To make sure that all stage 2 translations for the Secure PA space access
+     * the Secure PA space, we keep SA bit as 0.
+     *
+     * VSTCR_EL2.SC is NS check enable bit. To make sure that Stage 2 NS
+     * configuration is checked against stage 1 NS configuration in EL1&0
+     * translation regime for the given address, and generates a fault if they
Quoted 8 lines

xen/arch/arm/arm64/mpu/p2m.c line 79

Quoted 7 lines
xen/arch/arm/arm64/mpu/p2m.c +7 -0
+     * are different, we set SC bit 1.
+     */
+    vstcr_el2 &= ~VSTCR_EL2_SA;
+    vstcr_el2 |= VSTCR_EL2_SC;
+    WRITE_SYSREG(vstcr_el2, VSTCR_EL2);
+
+    printk("P2M: %d-bit IPA with %d-bit PA and %d-bit VMID\n",

All these are unsigned, so %u.

Oops, blindly copying from MMU code, I realise we have the issue also there

Cheers,
Luca

#4 lore raw

Re: [PATCH v3 1/3] arm/mpu: implement setup_virt_paging for MPU system

Quoted 85 lines · Luca Fancellu

Hi Michal,

Quoted 48 lines
Quoted 48 lines

void __init setup_virt_paging(void)
{

xen/arch/arm/arm64/mpu/p2m.c +44 -1
-    BUG_ON("unimplemented");
+    register_t vtcr_el2 = READ_SYSREG(VTCR_EL2);
+    register_t vstcr_el2 = READ_SYSREG(VSTCR_EL2);
+
+    /* PA size */
+    const unsigned int pa_range_info[] = {32, 36, 40, 42, 44, 48, 52, 0,
+                                          /* Invalid */};
+
+    /*
+     * Restrict "p2m_ipa_bits" if needed. As P2M table is always configured
+     * with IPA bits == PA bits, compare against "pabits".
+     */
+    if ( pa_range_info[system_cpuinfo.mm64.pa_range] < p2m_ipa_bits )
+        p2m_ipa_bits = pa_range_info[system_cpuinfo.mm64.pa_range];
+
+    /*
+     * The MSA and MSA_frac fields in the ID_AA64MMFR0_EL1 register identify the
+     * memory system configurations supported. In Armv8-R AArch64, the
+     * only permitted value for ID_AA64MMFR0_EL1.MSA is 0b1111.
+     */
+    if ( system_cpuinfo.mm64.msa != MM64_MSA_PMSA_SUPPORT )
+        goto fault;
+
+    /* Permitted values for ID_AA64MMFR0_EL1.MSA_frac are 0b0001 and 0b0010. */
+    if ( (system_cpuinfo.mm64.msa_frac != MM64_MSA_FRAC_PMSA_SUPPORT) &&
+         (system_cpuinfo.mm64.msa_frac != MM64_MSA_FRAC_VMSA_SUPPORT) )
+        goto fault;
+
+    /* Stage 1 EL1&0 translation regime uses PMSAv8 by default */
+    vtcr_el2 &= ~VTCR_MSA;
+
+    /*
+     * Clear VTCR_EL2.NSA bit to configure non-secure stage 2 translation output
+     * address space to access the Secure PA space as Armv8r only implements
+     * secure state.
+     */
+    vtcr_el2 &= ~VTCR_NSA;
+
+    /*
+     * cpuinfo sanitization makes sure we support 16bits VMID only if all cores
+     * are supporting it.
+     *
+     * Set the VS bit only if 16 bit VIMD is supported.
+     */
+    if ( system_cpuinfo.mm64.vmid_bits == MM64_VMID_16_BITS_SUPPORT )
Quoted 8 lines

xen/arch/arm/arm64/mpu/p2m.c line 59

Quoted 6 lines
xen/arch/arm/arm64/mpu/p2m.c +6 -0
+    {
+        vtcr_el2 |= VTCR_VS;
+        max_vmid = MAX_VMID_16_BIT;
+    }
+    else
+        vtcr_el2 &= ~VTCR_VS;

This is the last change to vtcr_el2. Why do you put p2m_vmid_allocator_init() in-between this and write to vtcr_el2 register? It looks odd.

ok I can have the write to the register before calling p2m_vmid_allocator_init, in the end what matters is only to call p2m_vmid_allocator_init() after setting max_vmid.

Quoted 14 lines
Quoted 14 lines
xen/arch/arm/arm64/mpu/p2m.c +13 -0
+
+    p2m_vmid_allocator_init();
+
+    WRITE_SYSREG(vtcr_el2, VTCR_EL2);
+
+    /*
+     * VSTCR_EL2.SA defines secure stage 2 translation output address space.
+     * To make sure that all stage 2 translations for the Secure PA space access
+     * the Secure PA space, we keep SA bit as 0.
+     *
+     * VSTCR_EL2.SC is NS check enable bit. To make sure that Stage 2 NS
+     * configuration is checked against stage 1 NS configuration in EL1&0
+     * translation regime for the given address, and generates a fault if they
Quoted 8 lines

xen/arch/arm/arm64/mpu/p2m.c line 79

Quoted 7 lines
xen/arch/arm/arm64/mpu/p2m.c +7 -0
+     * are different, we set SC bit 1.
+     */
+    vstcr_el2 &= ~VSTCR_EL2_SA;
+    vstcr_el2 |= VSTCR_EL2_SC;
+    WRITE_SYSREG(vstcr_el2, VSTCR_EL2);
+
+    printk("P2M: %d-bit IPA with %d-bit PA and %d-bit VMID\n",

All these are unsigned, so %u.

Quoted 1 line

Oops, blindly copying from MMU code, I realise we have the issue also there

There are many other places in Xen using incorrect printk identifiers but for the new code we should use the correct ones.

~Michal

#5 lore raw

PATCH v3 2/3 arm/mpu: Introduce `v8r_el1_msa` device tree property for domains

In reply to Luca Fancellu

From: Harry Ramsey <[email protected]>

Add a new device tree property v8r_el1_msa to select the MSA (memory system architecture) at EL1 i.e. MPU(default) or MMU.

---
v3:
- Improve commit message and device tree property description
- Remove macro protection
- Remove unused function is_mpu_domain
- Code formatting

---
 docs/misc/arm/device-tree/booting.txt | 12 ++++++++++++
 xen/arch/arm/dom0less-build.c         | 24 ++++++++++++++++++++++++
 xen/arch/arm/domain.c                 |  4 ++++
 xen/arch/arm/include/asm/domain.h     |  7 +++++++
 xen/arch/arm/include/asm/mpu.h        |  5 +++++
 xen/arch/arm/mpu/arm32/mm.c           |  5 +++++
 xen/arch/arm/mpu/arm64/mm.c           |  5 +++++
 xen/include/public/arch-arm.h         |  2 ++
 8 files changed, 64 insertions(+)
docs/misc/arm/device-tree/booting.txt +12 -0
index 977b4286082f..b1a329c0e8d9 100644
@@ -322,6 +322,18 @@ with the following properties:
     Should be used together with scmi-smc-passthrough Xen command line
     option.
 
+- v8r_el1_msa
+
+    A string property specifying whether, on Armv8-R systems, a domain
+    should use PMSAv8 (MPU) at EL1 or VMSAv8 (MMU) at EL1.
+
+    - "mmu"
+    Enables VMSAv8 at EL1. This requires hardware support and is only
+    optionally available on AArch64.
+
+    - "mpu"
+    Enables PMSAv8 at EL1. (Default)
+
 Under the "xen,domain" compatible node, one or more sub-nodes are present
 for the DomU kernel and ramdisk.
 
xen/arch/arm/dom0less-build.c +24 -0
index 4181c105389a..456dc553bb23 100644
@@ -24,6 +24,7 @@
 #include <asm/domain_build.h>
 #include <asm/firmware/sci.h>
 #include <asm/grant_table.h>
+#include <asm/mpu.h>
 #include <asm/setup.h>
 
 #ifdef CONFIG_VGICV2
@@ -315,6 +316,7 @@ int __init arch_parse_dom0less_node(struct dt_device_node *node,
     struct xen_domctl_createdomain *d_cfg = &bd->create_cfg;
     unsigned int flags = bd->create_flags;
     uint32_t val;
+    const char *v8r_el1_msa;
 
     d_cfg->arch.gic_version = XEN_DOMCTL_CONFIG_GIC_NATIVE;
     d_cfg->flags |= XEN_DOMCTL_CDF_hvm | XEN_DOMCTL_CDF_hap;
@@ -322,6 +324,28 @@ int __init arch_parse_dom0less_node(struct dt_device_node *node,
     if ( domu_dt_sci_parse(node, d_cfg) )
         panic("Error getting SCI configuration\n");
 
+    if (!dt_property_read_string(node, "v8r_el1_msa", &v8r_el1_msa))
+    {
+#ifdef CONFIG_MPU
+        if ( !strcmp(v8r_el1_msa, "mmu") )
+        {
+            if ( !has_v8r_vmsa_support() )
+                panic("Platform does not support VMSA at EL1 (v8r_el1_msa)\n");
+            d_cfg->arch.v8r_el1_msa = MPU_EL1_VMSA;
+        }
+        else if ( !strcmp(v8r_el1_msa, "mpu") )
+        {
+            d_cfg->arch.v8r_el1_msa = MPU_EL1_PMSA;
+            if ( !(flags & CDF_staticmem) || !(flags & CDF_directmap) )
+                panic("PMSA is not valid for domain without static allocation and direct map (v8r_el1_msa)\n");
+        }
+        else
+            panic("Invalid device tree option for v8r_el1_msa\n");
+#else
+        panic("'v8r_el1_msa' property found, but CONFIG_MPU not selected\n");
+#endif
+    }
+
     if ( !dt_property_read_u32(node, "nr_spis", &d_cfg->arch.nr_spis) )
     {
         int vpl011_virq = GUEST_VPL011_SPI;
xen/arch/arm/domain.c +4 -0
index 26380a807cad..c044293acd39 100644
@@ -725,6 +725,10 @@ int arch_domain_create(struct domain *d,
     if ( (rc = sci_domain_init(d, config)) != 0 )
         goto fail;
 
+#ifdef CONFIG_MPU
+    d->arch.v8r_el1_msa = config->arch.v8r_el1_msa;
+#endif
+
     return 0;
 
 fail:
xen/arch/arm/include/asm/domain.h +7 -0
index ffe5d0d9f0a6..fd0a83046893 100644
@@ -30,6 +30,9 @@ enum domain_type {
 #define is_64bit_domain(d) (0)
 #endif
 
+#define MPU_EL1_PMSA 0
+#define MPU_EL1_VMSA 1
+
 /*
  * Is the domain using the host memory layout?
  *
@@ -128,6 +131,10 @@ struct arch_domain
 #endif
 
     struct resume_info resume_ctx;
+
+#ifdef CONFIG_MPU
+    uint8_t v8r_el1_msa;
+#endif
 }  __cacheline_aligned;
 
 struct arch_vcpu
xen/arch/arm/include/asm/mpu.h +5 -0
index 72fa5b00b861..8a8c01086206 100644
@@ -27,6 +27,11 @@
 
 #ifndef __ASSEMBLER__
 
+/*
+ * Utility function to determine if an Armv8-R processor supports VMSA.
+ */
+bool has_v8r_vmsa_support(void);
+
 /*
  * Set base address of MPU protection region.
  *
xen/arch/arm/mpu/arm32/mm.c +5 -0
index a4673c351141..5eaeb3400e6c 100644
@@ -38,6 +38,11 @@
         break;                                            \
     }
 
+bool has_v8r_vmsa_support(void)
+{
+    return false;
+}
+
 /*
  * Armv8-R supports direct access and indirect access to the MPU regions through
  * registers:
xen/arch/arm/mpu/arm64/mm.c +5 -0
index ed643cad4073..b07e729a7d05 100644
@@ -32,6 +32,11 @@
         break;                                                  \
     }
 
+bool has_v8r_vmsa_support(void)
+{
+    return system_cpuinfo.mm64.msa_frac == MM64_MSA_FRAC_VMSA_SUPPORT;
+}
+
 /*
  * Armv8-R supports direct access and indirect access to the MPU regions through
  * registers:
xen/include/public/arch-arm.h +2 -0
index cd563cf70684..23df2f24cb09 100644
@@ -355,6 +355,8 @@ struct xen_arch_domainconfig {
     uint32_t clock_frequency;
     /* IN */
     uint8_t arm_sci_type;
+    /* IN */
+    uint8_t v8r_el1_msa;
 };
 #endif /* __XEN__ || __XEN_TOOLS__ */
 
Signature · 1 line
2.34.1
#6 lore raw

Re: [PATCH v3 2/3] arm/mpu: Introduce `v8r_el1_msa` device tree property for domains

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

Quoted 13 lines · Luca Fancellu
xen/include/public/arch-arm.h +2 -0
index cd563cf70684..23df2f24cb09 100644
@@ -355,6 +355,8 @@ struct xen_arch_domainconfig {
     uint32_t clock_frequency;
     /* IN */
     uint8_t arm_sci_type;
+    /* IN */
+    uint8_t v8r_el1_msa;
 };
 #endif /* __XEN__ || __XEN_TOOLS__ */
 

This isn't ok as the singular change to the public headers.  (Also, I haven't had time to fix the other API abuses that have crept into ARM's arch domain config)

You're introducing a new hypercall input parameter (which is fine), but has no defines (ones private to xen/arch/arm/include/asm/domain.h are not permitted for use in the public API), and for which there is no input validation.  Userspace can currently pass any arbitrary byte here which will be copied into d->arch.v8r_el1_msa, not that there's any consumer of this field I can see in the series.

Frankly, I think this series is chopped up too much.  You really must not be introducing new hypercall parameters like this without the whole series which lets you xl create such a VM.

~Andrew

#7 lore raw

Re: [PATCH v3 2/3] arm/mpu: Introduce `v8r_el1_msa` device tree property for domains

Hi Andrew,

Quoted 28 lines · Andrew Cooper

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

Quoted 13 lines · Luca Fancellu
xen/include/public/arch-arm.h +2 -0
index cd563cf70684..23df2f24cb09 100644
@@ -355,6 +355,8 @@ struct xen_arch_domainconfig {
    uint32_t clock_frequency;
    /* IN */
    uint8_t arm_sci_type;
+    /* IN */
+    uint8_t v8r_el1_msa;
 };
 #endif /* __XEN__ || __XEN_TOOLS__ */
 

This isn't ok as the singular change to the public headers. (Also, I haven't had time to fix the other API abuses that have crept into ARM's arch domain config)

You're introducing a new hypercall input parameter (which is fine), but has no defines (ones private to xen/arch/arm/include/asm/domain.h are not permitted for use in the public API), and for which there is no input validation. Userspace can currently pass any arbitrary byte here which will be copied into d->arch.v8r_el1_msa, not that there's any consumer of this field I can see in the series.

Quoted 3 lines

Frankly, I think this series is chopped up too much. You really must not be introducing new hypercall parameters like this without the whole series which lets you xl create such a VM.

Thanks for this feedback, while reworking this patch I was wrongly assuming I had always zero from the toolstack as this is not supported atm, while instead it’s not true for malicious userspace hypercall callers.

The full branch is available at https://gitlab.com/xen-project/people/lucafancellu/xen/-/commits/r82_rebased?ref_type=heads,
but it doesn’t support domain creation by the toolstack, only by Dom0less.

For the defines, should I have something in the file xen/xen/include/public/arch-arm.h like this:

#define XEN_DOMCTL_CONFIG_ARM_V8R_EL1_MSA_PMSA 0
#define XEN_DOMCTL_CONFIG_ARM_V8R_EL1_MSA_VMSA 1

And check the value of config->arch.v8r_el1_msa against them in arch_sanitise_domain_config?

Cheers,
Luca

#8 lore raw

PATCH v3 3/3 arm/mpu: Implement p2m tables

In reply to Luca Fancellu

From: Harry Ramsey <[email protected]>

Implement p2m_alloc_table, p2m_init and p2m_final_teardown for MPU systems.

---
v3:
- Check for alloc_xenheap_pages allocation
- Clear additional page allocated for ARM64
- Add check for INVALID_VCPU_ID
- Remove unnecessary function generate_vsctlr
- Code formatting fixes

---
 xen/arch/arm/include/asm/arm32/mpu.h |  2 +
 xen/arch/arm/include/asm/arm64/mpu.h |  2 +
 xen/arch/arm/include/asm/mpu/p2m.h   | 12 +++++
 xen/arch/arm/include/asm/p2m.h       |  5 ++
 xen/arch/arm/mpu/p2m.c               | 78 ++++++++++++++++++++++++++--
 5 files changed, 96 insertions(+), 3 deletions(-)
xen/arch/arm/include/asm/arm32/mpu.h +2 -0
index 2cf0f8cbacae..d565230f84ee 100644
@@ -11,6 +11,8 @@
  */
 #define MPU_REGION_RES0       0x0
 
+#define VSCTLR_VMID_SHIFT     16
+
 /* Hypervisor Protection Region Base Address Register */
 typedef union {
     struct {
xen/arch/arm/include/asm/arm64/mpu.h +2 -0
index 4f694190a8a3..8b86a03fee44 100644
@@ -7,6 +7,8 @@
 
 #define MPU_REGION_RES0        (0xFFFFULL << 48)
 
+#define VSCTLR_VMID_SHIFT      48
+
 /* Protection Region Base Address Register */
 typedef union {
     struct __packed {
xen/arch/arm/include/asm/mpu/p2m.h +12 -0
index e46d9e757a1d..39fc0c944916 100644
@@ -3,8 +3,20 @@
 #ifndef __ARM_MPU_P2M_H__
 #define __ARM_MPU_P2M_H__
 
+#include <xen/bitops.h>
+#include <xen/macros.h>
+#include <xen/page-size.h>
+
 struct p2m_domain;
 
+/*
+ * The architecture allows at most 255 EL2 MPU memory regions. The size of the
+ * MPU structure entry (pr_t) is 32 Bytes on AArch64 (requiring two 4KB pages)
+ * and 16 bytes on AArch32 (requiring one 4KB page).
+ */
+#define P2M_ROOT_PAGES DIV_ROUND_UP(255 * sizeof(pr_t), PAGE_SIZE)
+#define P2M_ROOT_ORDER get_count_order(P2M_ROOT_PAGES)
+
 static inline void p2m_clear_root_pages(struct p2m_domain *p2m) {}
 
 static inline void p2m_tlb_flush_sync(struct p2m_domain *p2m) {}
xen/arch/arm/include/asm/p2m.h +5 -0
index 010ce8c9ebbd..ed1b6dd40f40 100644
@@ -48,8 +48,13 @@ struct p2m_domain {
     /* Current VMID in use */
     uint16_t vmid;
 
+#ifdef CONFIG_MMU
     /* Current Translation Table Base Register for the p2m */
     uint64_t vttbr;
+#else
+    /* Current Virtualization System Control Register for the p2m */
+    register_t vsctlr;
+#endif
 
     /* Highest guest frame that's ever been mapped in the p2m */
     gfn_t max_mapped_gfn;
xen/arch/arm/mpu/p2m.c +75 -3

92 lines not on this page · raw message

Signature · 1 line
2.34.1