Skip to contentSkip to conversation

Armv8-R AArch64 MPU support (single core)

PATCH In review Luca Fancellu 5 participants 21 messages Apr 20 – Jul 10

lore
Share & tools
lore
#0 lore raw

cover Armv8-R AArch64 MPU support (single core)

Hi,

this serie complete the support for Armv8-R AArch64 MPU support for dom0less DomU boot on single core, it also build for Armv8-R AArch32 but the support is not complete.

The serie is based on another serie in the mailing list: "Fifth MPU Series".

I've spoken with Oleksii and the Arm maintainer to have the possibilty to have this on the Xen release, of course provided that maintainers have enough bandwidth and depending on the priority of the series to be in.

Asking for Oleksii Ack for the release, the changes in this serie are mostly related to MPU apart from few patches that touches common Arm code.

Luca Fancellu (2):
arm/mpu: Save/restore guest EL1 MPU-related context arm/mpu: Save/restore VTCR_EL2 on vCPU context switch

Penny Zheng (6):
xen/arm: Implement p2m_set_entry for MPU systems xen/arm: Implement p2m_get_entry for MPU systems xen/mpu: enable device passthrough in MPU system arm/mpu: Support vCPU context switch on MPU systems xen/arm: enable MMIO region trap in MPU system xen/arm: dump debug message in MPU system

 xen/arch/arm/arm64/mpu/p2m.c                |  17 ++
 xen/arch/arm/domain.c                       |  51 +++-
 xen/arch/arm/include/asm/arm32/mpu.h        |   3 +-
 xen/arch/arm/include/asm/arm64/mpu.h        |   3 +-
 xen/arch/arm/include/asm/domain.h           |  10 +
 xen/arch/arm/include/asm/mm.h               |   3 +
 xen/arch/arm/include/asm/mmu/domain-build.h |   2 +
 xen/arch/arm/include/asm/mmu/mm.h           |  12 +
 xen/arch/arm/include/asm/mpu.h              |   3 +
 xen/arch/arm/include/asm/mpu/cpregs.h       |   7 +
 xen/arch/arm/include/asm/mpu/domain-build.h |   6 +
 xen/arch/arm/include/asm/mpu/mm.h           |  13 +-
 xen/arch/arm/include/asm/mpu/p2m.h          |  25 ++
 xen/arch/arm/include/asm/p2m.h              |   3 +
 xen/arch/arm/mpu/arm32/mm.c                 |  10 +
 xen/arch/arm/mpu/arm64/mm.c                 | 136 +++++++++
 xen/arch/arm/mpu/domain-build.c             |  17 ++
 xen/arch/arm/mpu/mm.c                       | 101 +++++--
 xen/arch/arm/mpu/p2m.c                      | 295 +++++++++++++++++++-
 xen/arch/arm/traps.c                        |  91 ++++--
 20 files changed, 739 insertions(+), 69 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/8 xen/arm: Implement p2m_set_entry for MPU systems

From: Penny Zheng <[email protected]>

Implement the function p2m_set_entry, which is responsible for inserting a new entry into the p2m tables, for MPU systems.

---
 xen/arch/arm/include/asm/arm32/mpu.h |   3 +-
 xen/arch/arm/include/asm/arm64/mpu.h |   3 +-
 xen/arch/arm/include/asm/mpu/mm.h    |   3 +-
 xen/arch/arm/include/asm/mpu/p2m.h   |  10 +++
 xen/arch/arm/include/asm/p2m.h       |   3 +
 xen/arch/arm/mpu/mm.c                |  43 ++++++----
 xen/arch/arm/mpu/p2m.c               | 123 ++++++++++++++++++++++++++-
 7 files changed, 167 insertions(+), 21 deletions(-)
xen/arch/arm/include/asm/arm32/mpu.h +2 -1
index d565230f84ee..ab58df079920 100644
@@ -42,7 +42,8 @@ typedef struct {
     prbar_t prbar;
     prlar_t prlar;
     uint8_t refcount;
-    uint8_t pad[7];     /* Pad structure to 16 Bytes */
+    uint8_t p2m_type;
+    uint8_t pad[6];     /* Pad structure to 16 Bytes */
 } pr_t;
 
 #endif /* __ASSEMBLER__ */
xen/arch/arm/include/asm/arm64/mpu.h +2 -1
index 8b86a03fee44..c82624f0f2cf 100644
@@ -41,7 +41,8 @@ typedef struct {
     prbar_t prbar;
     prlar_t prlar;
     uint8_t refcount;
-    uint8_t pad[15];    /* Pad structure to 32 Bytes */
+    uint8_t p2m_type;
+    uint8_t pad[14];    /* Pad structure to 32 Bytes */
 } pr_t;
 
 #endif /* __ASSEMBLER__ */
xen/arch/arm/include/asm/mpu/mm.h +2 -1
index 1b5ffa5b644d..24bffdee4fb6 100644
@@ -75,9 +75,10 @@ void write_protection_region(const pr_t *pr_write, uint8_t sel);
  * @param base      Base address of the range to map (inclusive).
  * @param limit     Limit address of the range to map (exclusive).
  * @param flags     Flags for the memory range to map.
+ * @param p2m       True for a stage 2 mapping, otherwise False.
  * @return          0 on success, negative on error.
  */
-int xen_mpumap_update(paddr_t base, paddr_t limit, unsigned int flags);
+int xen_mpumap_update(paddr_t base, paddr_t limit, unsigned int flags, bool p2m);
 
 /*
  * Creates a pr_t structure describing a protection region.
xen/arch/arm/include/asm/mpu/p2m.h +10 -0
index 39fc0c944916..b9c7be2d9dcc 100644
@@ -21,6 +21,16 @@ static inline void p2m_clear_root_pages(struct p2m_domain *p2m) {}
 
 static inline void p2m_tlb_flush_sync(struct p2m_domain *p2m) {}
 
+static inline void region_set_p2m(pr_t *pr, p2m_type_t p2m_type)
+{
+    pr->p2m_type = p2m_type;
+}
+
+static inline p2m_type_t region_get_p2m(pr_t *pr)
+{
+    return pr->p2m_type;
+}
+
 #endif /* __ARM_MPU_P2M_H__ */
 
 /*
xen/arch/arm/include/asm/p2m.h +3 -0
index ed1b6dd40f40..43b383885da0 100644
@@ -54,6 +54,9 @@ struct p2m_domain {
 #else
     /* Current Virtualization System Control Register for the p2m */
     register_t vsctlr;
+
+    /* Number of MPU memory regions in P2M MPU memory mapping table. */
+    uint8_t nr_regions;
 #endif
 
     /* Highest guest frame that's ever been mapped in the p2m */
xen/arch/arm/mpu/mm.c +27 -16

114 lines not on this page · raw message

xen/arch/arm/mpu/p2m.c +121 -2

133 lines not on this page · raw message

Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH 1/8] xen/arm: Implement p2m_set_entry for MPU systems

Quoted 5 lines · Luca Fancellu

From: Penny Zheng <[email protected]>

Implement the function p2m_set_entry, which is responsible for inserting a new entry into the p2m tables, for MPU systems.

Hello Luca,

It seems this patch does not apply on master if I'm not mistaken. Regards,

Signature · 6 lines
--
Yann Sionneau | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

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

Re: [PATCH 1/8] xen/arm: Implement p2m_set_entry for MPU systems

Hi Yann,

Quoted 11 lines · Yann Sionneau
Quoted 5 lines · Luca Fancellu

From: Penny Zheng <[email protected]>

Implement the function p2m_set_entry, which is responsible for inserting a new entry into the p2m tables, for MPU systems.

Hello Luca,

It seems this patch does not apply on master if I'm not mistaken.

This serie applies on top of these patches:
https://patchwork.kernel.org/project/xen-devel/list/?series=1083371

In the cover letter I wrote:

```
[…]
The serie is based on another serie in the mailing list: "Fifth MPU Series".
[…]
```

Cheers,
Luca

#4 lore raw

Re: [PATCH 1/8] xen/arm: Implement p2m_set_entry for MPU systems

Quoted 19 lines · Luca Fancellu

Hi Yann,

Quoted 11 lines · Yann Sionneau
Quoted 5 lines · Luca Fancellu

From: Penny Zheng <[email protected]>

Implement the function p2m_set_entry, which is responsible for inserting a new entry into the p2m tables, for MPU systems.

Hello Luca,

It seems this patch does not apply on master if I'm not mistaken.

This serie applies on top of these patches:
https://patchwork.kernel.org/project/xen-devel/list/?series=1083371

In the cover letter I wrote:

Quoted 10 lines
```
[…]
The serie is based on another serie in the mailing list: "Fifth MPU Series".
[…]
```

Cheers,
Luca

Oopsy, sorry for the noise, I checked but ... it seems I did not read the cover letter carefully enough :)

Regards,

--

--
Yann Sionneau | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech

#5 lore raw

Re: [PATCH 1/8] xen/arm: Implement p2m_set_entry for MPU systems

In reply to Luca Fancellu

Hi Luca,

Quoted 69 lines · Luca Fancellu

From: Penny Zheng <[email protected]>

Implement the function p2m_set_entry, which is responsible for inserting a new entry into the p2m tables, for MPU systems.

---
  xen/arch/arm/include/asm/arm32/mpu.h |   3 +-
  xen/arch/arm/include/asm/arm64/mpu.h |   3 +-
  xen/arch/arm/include/asm/mpu/mm.h    |   3 +-
  xen/arch/arm/include/asm/mpu/p2m.h   |  10 +++
  xen/arch/arm/include/asm/p2m.h       |   3 +
  xen/arch/arm/mpu/mm.c                |  43 ++++++----
  xen/arch/arm/mpu/p2m.c               | 123 ++++++++++++++++++++++++++-
  7 files changed, 167 insertions(+), 21 deletions(-)
xen/arch/arm/include/asm/arm32/mpu.h +2 -1
index d565230f84ee..ab58df079920 100644
@@ -42,7 +42,8 @@ typedef struct {
      prbar_t prbar;
      prlar_t prlar;
      uint8_t refcount;
-    uint8_t pad[7];     /* Pad structure to 16 Bytes */
+    uint8_t p2m_type;
+    uint8_t pad[6];     /* Pad structure to 16 Bytes */
  } pr_t;
 
  #endif /* __ASSEMBLER__ */
xen/arch/arm/include/asm/arm64/mpu.h +2 -1
index 8b86a03fee44..c82624f0f2cf 100644
@@ -41,7 +41,8 @@ typedef struct {
      prbar_t prbar;
      prlar_t prlar;
      uint8_t refcount;
-    uint8_t pad[15];    /* Pad structure to 32 Bytes */
+    uint8_t p2m_type;
+    uint8_t pad[14];    /* Pad structure to 32 Bytes */
  } pr_t;
 
  #endif /* __ASSEMBLER__ */
xen/arch/arm/include/asm/mpu/mm.h +2 -1
index 1b5ffa5b644d..24bffdee4fb6 100644
@@ -75,9 +75,10 @@ void write_protection_region(const pr_t *pr_write, uint8_t sel);
   * @param base      Base address of the range to map (inclusive).
   * @param limit     Limit address of the range to map (exclusive).
   * @param flags     Flags for the memory range to map.
+ * @param p2m       True for a stage 2 mapping, otherwise False.
   * @return          0 on success, negative on error.
   */
-int xen_mpumap_update(paddr_t base, paddr_t limit, unsigned int flags);
+int xen_mpumap_update(paddr_t base, paddr_t limit, unsigned int flags, bool p2m);
 
  /*
   * Creates a pr_t structure describing a protection region.
xen/arch/arm/include/asm/mpu/p2m.h +0 -0
index 39fc0c944916..b9c7be2d9dcc 100644

xen/arch/arm/include/asm/mpu/p2m.h static inline void p2m_clear_root_pages(struct p2m_domain *p2m) {} · line 29

Quoted 9 lines
xen/arch/arm/include/asm/mpu/p2m.h +6 -0
@@ -21,6 +21,16 @@ static inline void p2m_clear_root_pages(struct p2m_domain *p2m) {}
 
  static inline void p2m_tlb_flush_sync(struct p2m_domain *p2m) {}
 
+static inline void region_set_p2m(pr_t *pr, p2m_type_t p2m_type)
+{
+    pr->p2m_type = p2m_type;
+}
+
+static inline p2m_type_t region_get_p2m(pr_t *pr)

The pointer is read-only here; take const pr_t *. Same for region_set_p2m's sibling accessors added in this series

Quoted 57 lines
xen/arch/arm/include/asm/mpu/p2m.h +4 -0
+{
+    return pr->p2m_type;
+}
+
  #endif /* __ARM_MPU_P2M_H__ */
 
  /*
xen/arch/arm/include/asm/p2m.h +3 -0
index ed1b6dd40f40..43b383885da0 100644
@@ -54,6 +54,9 @@ struct p2m_domain {
  #else
      /* Current Virtualization System Control Register for the p2m */
      register_t vsctlr;
+
+    /* Number of MPU memory regions in P2M MPU memory mapping table. */
+    uint8_t nr_regions;
  #endif
 
      /* Highest guest frame that's ever been mapped in the p2m */
xen/arch/arm/mpu/mm.c +7 -4
index aff88bd3a9c1..4ee58ded5ad6 100644
@@ -317,13 +317,14 @@ static int xen_mpumap_free_entry(uint8_t idx, int region_found_type)
   * Update the entry in the MPU memory region mapping table (xen_mpumap) for the
   * given memory range and flags, creating one if none exists.
   *
- * @param base  Base address (inclusive).
- * @param limit Limit address (exclusive).
- * @param flags Region attributes (a combination of PAGE_HYPERVISOR_XXX)
+ * @param base      Base address (inclusive).
+ * @param limit     Limit address (exclusive).
+ * @param flags     Region attributes (a combination of PAGE_HYPERVISOR_XXX)
+ * @param p2m       True for a stage 2 mapping, otherwise False.
   * @return      0 on success, otherwise negative on error.
   */
  static int xen_mpumap_update_entry(paddr_t base, paddr_t limit,
-                                   unsigned int flags)
+                                   unsigned int flags, bool p2m)
  {
      bool flags_has_page_present;
      uint8_t idx;
@@ -399,6 +400,8 @@ static int xen_mpumap_update_entry(paddr_t base, paddr_t limit,
              return -ENOENT;
 
          xen_mpumap[idx] = pr_of_addr(base, limit, flags);
+        /* AP[0] always 1 for stage 2 */
+        xen_mpumap[idx].prbar.reg.ap_0 = (p2m ? 1 : 0);
 
          write_protection_region(&xen_mpumap[idx], idx);
      }
@@ -418,33 +421,41 @@ static int xen_mpumap_update_entry(paddr_t base, paddr_t limit,
      return 0;
  }
 

xen/arch/arm/mpu/mm.c static int xen_mpumap_update_entry(paddr_t base, paddr_t limit, · line 429

Quoted 10 lines
xen/arch/arm/mpu/mm.c +2 -4
-int xen_mpumap_update(paddr_t base, paddr_t limit, unsigned int flags)
+int check_mpu_mapping(paddr_t base, paddr_t limit, unsigned int flags)
  {
-    int rc;
-
      if ( flags_has_rwx(flags) )
      {
          printk("Mappings should not be both Writeable and Executable\n");
-        return -EINVAL;
+        return false;

This function returns bool but is typed int, and has external linkage with no prototype. It becomes static bool in patch 4 - please make it static bool here so it is correct when introduced, and drop the churn in 4.

Quoted 148 lines
xen/arch/arm/mpu/mm.c +18 -8
      }
 
      if ( base >= limit )
      {
          printk("Base address %#"PRIpaddr" must be smaller than limit address %#"PRIpaddr"\n",
                 base, limit);
-        return -EINVAL;
+        return false;
      }
 
      if ( !IS_ALIGNED(base, PAGE_SIZE) || !IS_ALIGNED(limit, PAGE_SIZE) )
      {
          printk("base address %#"PRIpaddr", or limit address %#"PRIpaddr" is not page aligned\n",
                 base, limit);
-        return -EINVAL;
+        return false;
      }
 
+    return true;
+}
+
+int xen_mpumap_update(paddr_t base, paddr_t limit, unsigned int flags, bool p2m)
+{
+    int rc;
+
+    if ( !check_mpu_mapping(base, limit, flags) )
+        return -EINVAL;
+
      spin_lock(&xen_mpumap_lock);
 
-    rc = xen_mpumap_update_entry(base, limit, flags);
+    rc = xen_mpumap_update_entry(base, limit, flags, p2m);
      if ( !rc )
          context_sync_mpu();
 
@@ -459,7 +470,7 @@ int destroy_xen_mappings(unsigned long s, unsigned long e)
      ASSERT(IS_ALIGNED(e, PAGE_SIZE));
      ASSERT(s < e);
 
-    return xen_mpumap_update(s, e, 0);
+    return xen_mpumap_update(s, e, 0, false);
  }
 
  int destroy_xen_mapping_containing(paddr_t s)
@@ -499,7 +510,7 @@ int map_pages_to_xen(unsigned long virt, mfn_t mfn, unsigned long nr_mfns,
                       unsigned int flags)
  {
      /* MPU systems have no translation, ma == va, so pass virt directly */
-    return xen_mpumap_update(virt, mfn_to_maddr(mfn_add(mfn, nr_mfns)), flags);
+    return xen_mpumap_update(virt, mfn_to_maddr(mfn_add(mfn, nr_mfns)), flags, false);
  }
 
  /*
@@ -520,7 +531,7 @@ void __init setup_mm_helper(void)
              paddr_t bank_end = bank_start + bank_size;
 
              /* Map static heap with one MPU protection region */
-            if ( xen_mpumap_update(bank_start, bank_end, PAGE_HYPERVISOR) )
+            if ( xen_mpumap_update(bank_start, bank_end, PAGE_HYPERVISOR, false) )
                  panic("Failed to map static heap\n");
 
              break;
@@ -533,7 +544,7 @@ void __init setup_mm_helper(void)
 
  int modify_xen_mappings(unsigned long s, unsigned long e, unsigned int nf)
  {
-    return xen_mpumap_update(s, e, nf);
+    return xen_mpumap_update(s, e, nf, false);
  }
 
  void dump_hyp_walk(vaddr_t addr)
@@ -598,7 +609,7 @@ void __iomem *ioremap_attr(paddr_t start, size_t len, unsigned int flags)
      paddr_t start_pg = round_pgdown(start);
      paddr_t end_pg = round_pgup(start + len);
 
-    if ( xen_mpumap_update(start_pg, end_pg, flags) )
+    if ( xen_mpumap_update(start_pg, end_pg, flags, false) )
          return NULL;
 
      /* Mapped or already mapped */
xen/arch/arm/mpu/p2m.c +60 -0
index ec8f630acd90..4a8595b1b25e 100644
@@ -8,12 +8,131 @@
  #include <xen/sched.h>
  #include <xen/types.h>
  #include <asm/p2m.h>
+#include <asm/mpu.h>
+
+static inline unsigned int build_p2m_flags(p2m_type_t t)
+{
+    unsigned int flags = 0;
+
+    BUILD_BUG_ON(p2m_max_real_type > (1 << 4));
+
+    switch ( t )
+    {
+    case p2m_ram_rw:
+        /* Nothing to do, XN=0, RO=0 */
+        break;
+
+    case p2m_ram_ro:
+        flags |= _PAGE_RO;
+        break;
+
+    case p2m_invalid:
+        flags |= _PAGE_XN | _PAGE_RO;
+        break;
+
+    case p2m_max_real_type:
+        BUG();
+        break;
+
+    case p2m_mmio_direct_dev:
+    case p2m_mmio_direct_nc:
+    case p2m_mmio_direct_c:
+    case p2m_iommu_map_ro:
+    case p2m_iommu_map_rw:
+    case p2m_map_foreign_ro:
+    case p2m_map_foreign_rw:
+    case p2m_grant_map_ro:
+    case p2m_grant_map_rw:
+        panic(XENLOG_G_ERR "p2m: UNIMPLEMENTED p2m permission in MPU system\n");
+        break;
+    }
+
+    flags |= MT_NORMAL;
+
+    return flags;
+}
+
+/*
+ * Check whether guest memory region [`sgfn`, `sgfn` + `nr_gfns`) is mapped in
+ * mpumap `table`.
+ *
+ * If the memory region is mapped, `idx` is set to the index of the associated
+ * MPU memory region and 0 is returned.
+ * If the memory region is not mapped, -ENOENT is returned.
+ */
+static int is_gfns_mapped(pr_t *table, uint8_t nr_regions, gfn_t sgfn,
+                          unsigned long nr_gfns, uint8_t *idx)
+{
+    paddr_t gbase = gfn_to_gaddr(sgfn);
+    paddr_t glimit = gfn_to_gaddr(gfn_add(sgfn, nr_gfns));
+    int rc;
+
+    rc = mpumap_contains_region(table, nr_regions, gbase, glimit, idx);

xen/arch/arm/mpu/p2m.c line 80

Quoted 10 lines
xen/arch/arm/mpu/p2m.c +10 -0
+    if ( MPUMAP_REGION_OVERLAP == rc )
+        return -EINVAL;
+
+    if ( MPUMAP_REGION_NOTFOUND == rc )
+        return -ENOENT;
+
+    return 0;
+}
+
+static int __p2m_set_entry(struct p2m_domain *p2m, gfn_t sgfn, unsigned int nr,

p2m_set_entry takes unsigned long nr and passes it here as unsigned int, so the count is silently truncated on the way in. Keep the type consistent.

Quoted 25 lines
xen/arch/arm/mpu/p2m.c +25 -0
+                    mfn_t smfn, p2m_type_t t, p2m_access_t a)
+{
+    pr_t *table;
+    mfn_t emfn = mfn_add(smfn, nr);
+    unsigned int flags;
+    uint8_t idx = INVALID_REGION_IDX;
+
+    /*
+     * In all cases other than when removing a mapping (mfn == MFN_INVALID),
+     * gfn == mfn on MPU systems.
+     */
+    if ( !mfn_eq(smfn, INVALID_MFN) && gfn_x(sgfn) != mfn_x(smfn) )
+    {
+        printk(XENLOG_G_ERR "Unable to map MFN %#"PRI_mfn" at %#"PRI_mfn"\n",
+               mfn_x(smfn), gfn_x(sgfn));
+        return -EINVAL;
+    }
+
+    table = (pr_t *)page_to_virt(p2m->root);
+    if ( !table )
+        return -EINVAL;
+
+    /* Already mapped */
+    if ( is_gfns_mapped(table, p2m->nr_regions, sgfn, nr, &idx) != -ENOENT )
+    {

xen/arch/arm/mpu/p2m.c line 115

Quoted 10 lines
xen/arch/arm/mpu/p2m.c +10 -0
+        printk(XENLOG_G_ERR "Unable to insert P2M MPU memory region %#"PRIpaddr"-%#"PRIpaddr"\n",
+               gfn_to_gaddr(sgfn), gfn_to_gaddr(gfn_add(sgfn, nr)));
+        return -EINVAL;
+    }
+
+    flags = build_p2m_flags(t);
+    table[p2m->nr_regions] = pr_of_addr(mfn_to_maddr(smfn),
+                                        mfn_to_maddr(mfn_add(smfn, nr)), flags);
+    region_set_p2m(&table[p2m->nr_regions], t);
+    p2m->nr_regions++;

Should we check that against the upperbound MAX_MPU_REGION_NR. And panic when it reaches the max , so that an overflow is prevented.

Quoted 15 lines
xen/arch/arm/mpu/p2m.c +9 -2
+
+    p2m->max_mapped_gfn = gfn_max(p2m->max_mapped_gfn, _gfn(mfn_x(emfn)));
+    p2m->lowest_mapped_gfn = gfn_min(p2m->lowest_mapped_gfn, _gfn(mfn_x(smfn)));
+
+    return 0;
+}
 
  int p2m_set_entry(struct p2m_domain *p2m, gfn_t sgfn, unsigned long nr,
                    mfn_t smfn, p2m_type_t t, p2m_access_t a)
  {
-    BUG_ON("unimplemented");
-    return -EINVAL;
+    /*
+     * Any reference taken by the P2M mappings (e.g. foreign mapping) will
+     * be dropped in relinquish_p2m_mapping(). As the P2M will still

xen/arch/arm/mpu/p2m.c line 138

Quoted 10 lines
xen/arch/arm/mpu/p2m.c +7 -0
+     * be accessible after, we need to prevent mapping to be added when the
+     * domain is dying.
+     */
+    if ( unlikely(p2m->domain->is_dying) )
+        return -ENOMEM;
+
+    return __p2m_set_entry(p2m, sgfn, nr, smfn, t, a);
  }
 
  mfn_t p2m_get_entry(struct p2m_domain *p2m, gfn_t gfn, p2m_type_t *t,

- Ayan

#6 lore raw

PATCH 2/8 xen/arm: Implement p2m_get_entry for MPU systems

In reply to Luca Fancellu

From: Penny Zheng <[email protected]>

Implement the function p2m_get_entry for MPU systems, which is responsible for looking up an entry in the p2m table.

---
 xen/arch/arm/include/asm/mpu/p2m.h |  5 ++
 xen/arch/arm/mpu/p2m.c             | 76 ++++++++++++++++++++++++++++--
 2 files changed, 77 insertions(+), 4 deletions(-)
xen/arch/arm/include/asm/mpu/p2m.h +5 -0
index b9c7be2d9dcc..d0ec8a77a15a 100644
@@ -31,6 +31,11 @@ static inline p2m_type_t region_get_p2m(pr_t *pr)
     return pr->p2m_type;
 }
 
+static inline bool region_is_p2m_valid(pr_t *pr)
+{
+    return (pr->p2m_type != p2m_invalid);
+}
+
 #endif /* __ARM_MPU_P2M_H__ */
 
 /*
xen/arch/arm/mpu/p2m.c +72 -4

83 lines not on this page · raw message

Signature · 1 line
2.34.1
#7 lore raw

Re: [PATCH 2/8] xen/arm: Implement p2m_get_entry for MPU systems

Hi Luca,

Quoted 19 lines · Luca Fancellu

From: Penny Zheng <[email protected]>

Implement the function p2m_get_entry for MPU systems, which is responsible for looking up an entry in the p2m table.

---
  xen/arch/arm/include/asm/mpu/p2m.h |  5 ++
  xen/arch/arm/mpu/p2m.c             | 76 ++++++++++++++++++++++++++++--
  2 files changed, 77 insertions(+), 4 deletions(-)
xen/arch/arm/include/asm/mpu/p2m.h +0 -0
index b9c7be2d9dcc..d0ec8a77a15a 100644

xen/arch/arm/include/asm/mpu/p2m.h static inline p2m_type_t region_get_p2m(pr_t *pr) · line 34

Quoted 4 lines
xen/arch/arm/include/asm/mpu/p2m.h +1 -0
@@ -31,6 +31,11 @@ static inline p2m_type_t region_get_p2m(pr_t *pr)
      return pr->p2m_type;
  }
 
+static inline bool region_is_p2m_valid(pr_t *pr)

const pr_t *

As this is just accessed.

Quoted 45 lines
xen/arch/arm/include/asm/mpu/p2m.h +4 -0
+{
+    return (pr->p2m_type != p2m_invalid);
+}
+
  #endif /* __ARM_MPU_P2M_H__ */
 
  /*
xen/arch/arm/mpu/p2m.c +25 -4
index 4a8595b1b25e..681717eacf37 100644
@@ -135,11 +135,79 @@ int p2m_set_entry(struct p2m_domain *p2m, gfn_t sgfn, unsigned long nr,
      return __p2m_set_entry(p2m, sgfn, nr, smfn, t, a);
  }
 
-mfn_t p2m_get_entry(struct p2m_domain *p2m, gfn_t gfn, p2m_type_t *t,
-                    p2m_access_t *a, unsigned int *page_order, bool *valid)
+/*
+ * Get the details of guest MPU memory region [gfn, gfn + nr_gfns).
+ *
+ * If the region is mapped, `t` is set to the p2m_type of the region and the
+ * starting MFN is returned.
+ * If the region is not mapped, INVALID_MFN is returned.
+ */
+static mfn_t p2m_get_mpu_region(struct p2m_domain *p2m, gfn_t gfn,
+                                unsigned long nr_gfns, p2m_type_t *type,
+                                bool *valid)
  {
-    BUG_ON("unimplemented");
-    return INVALID_MFN;
+    pr_t *table = NULL;
+    pr_t *region = NULL;
+    uint8_t idx = INVALID_REGION_IDX;
+    gfn_t egfn = gfn_add(gfn, nr_gfns);
+    p2m_type_t optional_type = p2m_invalid;
+    bool optional_valid = false;
+    mfn_t ret = INVALID_MFN;
+
+    ASSERT(p2m_is_locked(p2m));
+
+    /*
+     * Check if the ending gfn is higher than the highest the p2m map
+     * currently holds, or the starting gfn lower than the lowest it holds.
+     */
+    if ( (gfn_x(egfn) > gfn_x(p2m->max_mapped_gfn)) ||

xen/arch/arm/mpu/p2m.c int p2m_set_entry(struct p2m_domain *p2m, gfn_t sgfn, unsigned long nr, · line 173

Quoted 10 lines
xen/arch/arm/mpu/p2m.c +10 -0
+         (gfn_x(gfn) < gfn_x(p2m->lowest_mapped_gfn)) )
+        goto out;
+
+    table = (pr_t *)page_to_virt(p2m->root);
+    /* The table should always be non-NULL and is always present. */
+    if ( !table )
+        ASSERT_UNREACHABLE();
+
+    if ( is_gfns_mapped(table, p2m->nr_regions, gfn, nr_gfns, &idx) )
+        goto out;

ASSERT_UNREACHABLE() is a no-op in a release build, so on a NULL table you fall straight through and dereference it in is_gfns_mapped(). Add a goto out (as __p2m_set_entry already does with its return -EINVAL).

Quoted 27 lines
xen/arch/arm/mpu/p2m.c +27 -0
+
+    region = &table[idx];
+    if ( region_is_p2m_valid(region) )
+    {
+        optional_type = region_get_p2m(region);
+        optional_valid = region_is_valid(region);
+    }
+
+    /* Always GFN == MFN on MPU systems. */
+    ret = _mfn(gfn_x(gfn));
+
+out:
+    if ( type )
+        *type = optional_type;
+
+    if ( valid )
+        *valid = optional_valid;
+
+    return ret;
+}
+
+/*
+ * Get the details of a given gfn.
+ *
+ * If the entry is present, the associated MFN will be returned and the
+ * p2m type gets filled up.
+ * If the entry is not present, INVALID_MFN will be returned

xen/arch/arm/mpu/p2m.c int p2m_set_entry(struct p2m_domain *p2m, gfn_t sgfn, unsigned long nr, · line 210

Quoted 10 lines
xen/arch/arm/mpu/p2m.c +10 -0
+ *
+ * The page_order is meaningless in MPU system, and we keep it here
+ * to be compatible with MMU system.
+ */
+mfn_t p2m_get_entry(struct p2m_domain *p2m, gfn_t gfn,
+                    p2m_type_t *t, p2m_access_t *a,
+                    unsigned int *page_order,
+                    bool *valid)
+{
+    return p2m_get_mpu_region(p2m, gfn, 1, t, valid);

The a (p2m_access_t *) out-parameter is never written. A caller that passes a non-NULL a reads stale stack. Either fill it or document why access is not tracked on MPU.

xen/arch/arm/mpu/p2m.c int p2m_set_entry(struct p2m_domain *p2m, gfn_t sgfn, unsigned long nr, · line 213

Quoted 3 lines
xen/arch/arm/mpu/p2m.c +0 -0
  }
 
  void p2m_dump_info(struct domain *d)

- Ayan

#8 lore raw

PATCH 3/8 xen/mpu: enable device passthrough in MPU system

In reply to Luca Fancellu

From: Penny Zheng <[email protected]>

In order to enable device passthrough in MPU system, we only need to provide p2m_mmio_direct_dev permission set up.

---
 xen/arch/arm/mpu/p2m.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
xen/arch/arm/mpu/p2m.c +7 -2
index 681717eacf37..f2482237412b 100644
@@ -13,6 +13,7 @@
 static inline unsigned int build_p2m_flags(p2m_type_t t)
 {
     unsigned int flags = 0;
+    unsigned int attr_idx = MT_NORMAL;
 
     BUILD_BUG_ON(p2m_max_real_type > (1 << 4));
 
@@ -30,11 +31,15 @@ static inline unsigned int build_p2m_flags(p2m_type_t t)
         flags |= _PAGE_XN | _PAGE_RO;
         break;
 
+    case p2m_mmio_direct_dev:
+        flags |= _PAGE_XN;
+        attr_idx = MT_DEVICE_nGnRE;
+        break;
+
     case p2m_max_real_type:
         BUG();
         break;
 
-    case p2m_mmio_direct_dev:
     case p2m_mmio_direct_nc:
     case p2m_mmio_direct_c:
     case p2m_iommu_map_ro:
@@ -47,7 +52,7 @@ static inline unsigned int build_p2m_flags(p2m_type_t t)
         break;
     }
 
-    flags |= MT_NORMAL;
+    flags |= attr_idx;
 
     return flags;
 }
Signature · 1 line
2.34.1
#9 lore raw

PATCH 4/8 arm/mpu: Support vCPU context switch on MPU systems

In reply to Luca Fancellu

From: Penny Zheng <[email protected]>

Implement the functions p2m_save_state and p2m_restore_state for MPU systems. Unlike on MMU systems, where we simply update VTTBR_EL2 with the incoming guest's p2m table on context switch, we have to disable the outgoing guest's p2m memory regions and enable the incoming guest's p2m memory regions.

---
 xen/arch/arm/include/asm/mpu/cpregs.h |  4 +++
 xen/arch/arm/mpu/mm.c                 | 11 +++++--
 xen/arch/arm/mpu/p2m.c                | 47 +++++++++++++++++++++++++--
 3 files changed, 57 insertions(+), 5 deletions(-)
xen/arch/arm/include/asm/mpu/cpregs.h +4 -0
index 9f3b32acd79f..5a3d92cf5389 100644
@@ -6,6 +6,9 @@
 /* CP15 CR0: MPU Type Register */
 #define HMPUIR          p15,4,c0,c0,4
 
+/* CP15 CR2: Virtualization System Control register */
+#define VSCTLR          p15,4,c2,c0,1
+
 /* CP15 CR6: Protection Region Enable Register */
 #define HPRENR          p15,4,c6,c1,1
 
@@ -88,6 +91,7 @@
 #define PRENR_EL2       HPRENR
 #define PRLAR_EL2       HPRLAR
 #define PRSELR_EL2      HPRSELR
+#define VSCTLR_EL2      VSCTLR
 #endif /* CONFIG_ARM_32 */
 
 #endif /* __ARM_MPU_CPREGS_H */
xen/arch/arm/mpu/mm.c +8 -3
index 4ee58ded5ad6..5ed77355a5f9 100644
@@ -421,9 +421,14 @@ static int xen_mpumap_update_entry(paddr_t base, paddr_t limit,
     return 0;
 }
 
-int check_mpu_mapping(paddr_t base, paddr_t limit, unsigned int flags)
+static bool check_mpu_mapping(paddr_t base, paddr_t limit, unsigned int flags,
+                              bool p2m)
 {
-    if ( flags_has_rwx(flags) )
+    /*
+     * Mappings should not be both Writeable and Executable, unless
+     * it is for guest P2M mapping.
+     */
+    if ( flags_has_rwx(flags) && !p2m )
     {
         printk("Mappings should not be both Writeable and Executable\n");
         return false;
@@ -450,7 +455,7 @@ int xen_mpumap_update(paddr_t base, paddr_t limit, unsigned int flags, bool p2m)
 {
     int rc;
 
-    if ( !check_mpu_mapping(base, limit, flags) )
+    if ( !check_mpu_mapping(base, limit, flags, p2m) )
         return -EINVAL;
 
     spin_lock(&xen_mpumap_lock);
xen/arch/arm/mpu/p2m.c +45 -2

59 lines not on this page · raw message

Signature · 1 line
2.34.1
#10 lore raw

Re: [PATCH 4/8] arm/mpu: Support vCPU context switch on MPU systems

Hi Luca,

Quoted 46 lines · Luca Fancellu

From: Penny Zheng <[email protected]>

Implement the functions p2m_save_state and p2m_restore_state for MPU systems. Unlike on MMU systems, where we simply update VTTBR_EL2 with the incoming guest's p2m table on context switch, we have to disable the outgoing guest's p2m memory regions and enable the incoming guest's p2m memory regions.

---
  xen/arch/arm/include/asm/mpu/cpregs.h |  4 +++
  xen/arch/arm/mpu/mm.c                 | 11 +++++--
  xen/arch/arm/mpu/p2m.c                | 47 +++++++++++++++++++++++++--
  3 files changed, 57 insertions(+), 5 deletions(-)
xen/arch/arm/include/asm/mpu/cpregs.h +4 -0
index 9f3b32acd79f..5a3d92cf5389 100644
@@ -6,6 +6,9 @@
  /* CP15 CR0: MPU Type Register */
  #define HMPUIR          p15,4,c0,c0,4
 
+/* CP15 CR2: Virtualization System Control register */
+#define VSCTLR          p15,4,c2,c0,1
+
  /* CP15 CR6: Protection Region Enable Register */
  #define HPRENR          p15,4,c6,c1,1
 
@@ -88,6 +91,7 @@
  #define PRENR_EL2       HPRENR
  #define PRLAR_EL2       HPRLAR
  #define PRSELR_EL2      HPRSELR
+#define VSCTLR_EL2      VSCTLR
  #endif /* CONFIG_ARM_32 */
 
  #endif /* __ARM_MPU_CPREGS_H */
xen/arch/arm/mpu/mm.c +0 -0
index 4ee58ded5ad6..5ed77355a5f9 100644

xen/arch/arm/mpu/mm.c static int xen_mpumap_update_entry(paddr_t base, paddr_t limit, · line 425

Quoted 6 lines
xen/arch/arm/mpu/mm.c +2 -1
@@ -421,9 +421,14 @@ static int xen_mpumap_update_entry(paddr_t base, paddr_t limit,
      return 0;
  }
 
-int check_mpu_mapping(paddr_t base, paddr_t limit, unsigned int flags)
+static bool check_mpu_mapping(paddr_t base, paddr_t limit, unsigned int flags,
+                              bool p2m)

The static bool conversion belongs in patch 1 where the function is introduced; only the new p2m parameter is in scope
for this patch.

Quoted 73 lines
xen/arch/arm/mpu/mm.c +6 -2
  {
-    if ( flags_has_rwx(flags) )
+    /*
+     * Mappings should not be both Writeable and Executable, unless
+     * it is for guest P2M mapping.
+     */
+    if ( flags_has_rwx(flags) && !p2m )
      {
          printk("Mappings should not be both Writeable and Executable\n");
          return false;
@@ -450,7 +455,7 @@ int xen_mpumap_update(paddr_t base, paddr_t limit, unsigned int flags, bool p2m)
  {
      int rc;
 
-    if ( !check_mpu_mapping(base, limit, flags) )
+    if ( !check_mpu_mapping(base, limit, flags, p2m) )
          return -EINVAL;
 
      spin_lock(&xen_mpumap_lock);
xen/arch/arm/mpu/p2m.c +38 -2
index f2482237412b..bf87c65c106c 100644
@@ -285,14 +285,57 @@ int p2m_init(struct domain *d)
      return 0;
  }
 
+static int p2m_xenmpu_update(struct p2m_domain *p2m, bool online)
+{
+    pr_t *p2m_table;
+    unsigned int flags = online ? _PAGE_PRESENT : 0;
+
+    p2m_table = (pr_t *)page_to_virt(p2m->root);
+    if ( !p2m_table )
+        return -EINVAL;
+
+    for ( unsigned int i = 0; i < p2m->nr_regions; i++ )
+    {
+        paddr_t base = pr_get_base(&p2m_table[i]);
+        paddr_t limit = pr_get_limit(&p2m_table[i]) + 1;
+        unsigned int region_flags;
+
+        region_flags = build_p2m_flags(region_get_p2m(&p2m_table[i])) | flags;
+        if ( xen_mpumap_update(base, limit, region_flags, true) )
+        {
+            printk(XENLOG_G_ERR "Unable to update MPU memory mapping with P2M region %#"PRIpaddr"-%#"PRIpaddr"\n",
+                   base, limit);
+            return -EINVAL;
+        }
+    }
+
+    return 0;
+}
+
+/* p2m_save_state and p2m_restore_state work in pair. */
  void p2m_save_state(struct vcpu *p)
  {
-    BUG_ON("unimplemented");
+    struct p2m_domain *p2m = p2m_get_hostp2m(p->domain);
+
+    p->arch.sctlr = READ_SYSREG(SCTLR_EL1);
+
+    if ( p2m_xenmpu_update(p2m, false) )
+        panic("Failed to offline P2M MPU memory mapping\n");
  }
 
  void p2m_restore_state(struct vcpu *n)
  {
-    BUG_ON("unimplemented");
+    struct p2m_domain *p2m = p2m_get_hostp2m(n->domain);
+    uint8_t *last_vcpu_ran = &p2m->last_vcpu_ran[smp_processor_id()];
+
+    WRITE_SYSREG(n->arch.sctlr, SCTLR_EL1);

xen/arch/arm/mpu/p2m.c int p2m_init(struct domain *d) · line 341

Quoted 10 lines
xen/arch/arm/mpu/p2m.c +7 -0
+    WRITE_SYSREG(n->arch.hcr_el2, HCR_EL2);
+
+    WRITE_SYSREG(p2m->vsctlr, VSCTLR_EL2);
+    if ( p2m_xenmpu_update(p2m, true) )
+        panic("Failed to online P2M MPU memory mapping\n");
+
+    *last_vcpu_ran = n->vcpu_id;
  }
 
  void p2m_final_teardown(struct domain *d)

- Ayan

#11 lore raw

PATCH 5/8 xen/arm: enable MMIO region trap in MPU system

In reply to Luca Fancellu

From: Penny Zheng <[email protected]>

MMIO region traps, generating from insufficient access permissions, will lead to data abort with Permission Fault in MPU system. It is different with MMU system, which generates Translation Fault instead.

We extract common codes for dealing with MMIO trap into a new helper do_mmio_trap_stage2_abort_guest().

---
 xen/arch/arm/traps.c | 83 ++++++++++++++++++++++++++++++--------------
 1 file changed, 56 insertions(+), 27 deletions(-)
xen/arch/arm/traps.c +56 -27
index 0c01f37ad6b4..081bd2e51979 100644
@@ -1815,6 +1815,47 @@ static inline bool check_p2m(bool is_data, paddr_t gpa)
     return false;
 }
 
+static int do_mmio_trap_stage2_abort_guest(struct cpu_user_regs *regs,
+                                           const union hsr hsr,
+                                           mmio_info_t *info,
+                                           vaddr_t gva, paddr_t gpa)
+{
+    enum io_state state;
+
+    state = try_handle_mmio(regs, info);
+
+    switch ( state )
+    {
+        case IO_ABORT:
+            gdprintk(XENLOG_DEBUG,
+                    "HSR=%#"PRIregister" pc=%#"PRIregister" gva=%#"PRIvaddr" gpa=%#"PRIpaddr"\n",
+                    hsr.bits, regs->pc, gva, gpa);
+            inject_dabt_exception(regs, gva);
+            break;
+        case IO_HANDLED:
+            /*
+             * If the instruction was decoded and has executed successfully
+             * on the MMIO region, then Xen should execute the next part of
+             * the instruction. (for eg increment the rn if it is a
+             * post-indexing instruction.
+             */
+            finalize_instr_emulation(&info->dabt_instr);
+            advance_pc(regs, hsr);
+            break;
+        case IO_RETRY:
+            /* finish later */
+            break;
+        case IO_UNHANDLED:
+            /* IO unhandled, try another way to handle it. */
+            return -EFAULT;
+        default:
+            ASSERT_UNREACHABLE();
+            return -EFAULT;
+    }
+
+    return 0;
+}
+
 static void do_trap_stage2_abort_guest(struct cpu_user_regs *regs,
                                        const union hsr hsr)
 {
@@ -1829,7 +1870,6 @@ static void do_trap_stage2_abort_guest(struct cpu_user_regs *regs,
     uint8_t fsc = xabt.fsc & ~FSC_LL_MASK;
     bool is_data = (hsr.ec == HSR_EC_DATA_ABORT_LOWER_EL);
     mmio_info_t info;
-    enum io_state state;
 
     /*
      * If this bit has been set, it means that this stage-2 abort is caused
@@ -1863,6 +1903,8 @@ static void do_trap_stage2_abort_guest(struct cpu_user_regs *regs,
             return; /* Try again */
     }
 
+    info.gpa = gpa;
+    info.dabt = hsr.dabt;
     switch ( fsc )
     {
     case FSC_FLT_PERM:
@@ -1876,6 +1918,17 @@ static void do_trap_stage2_abort_guest(struct cpu_user_regs *regs,
         };
 
         p2m_mem_access_check(gpa, gva, npfec);
+
+#if !defined(CONFIG_MMU)
+        /*
+         * MMIO region traps, generating from insufficient access permissions,
+         * will lead to data abort with Permission fault.
+         */
+        if ( is_data && (do_mmio_trap_stage2_abort_guest(regs, hsr, &info, gva,
+                                                         gpa) == 0) )
+            return;
+#endif
+
         /*
          * The only way to get here right now is because of mem_access,
          * thus reinjecting the exception to the guest is never required.
@@ -1884,9 +1937,6 @@ static void do_trap_stage2_abort_guest(struct cpu_user_regs *regs,
     }
     case FSC_FLT_TRANS:
     {
-        info.gpa = gpa;
-        info.dabt = hsr.dabt;
-
         /*
          * Assumption :- Most of the times when we get a data abort and the ISS
          * is invalid or an instruction abort, the underlying cause is that the
@@ -1923,29 +1973,8 @@ static void do_trap_stage2_abort_guest(struct cpu_user_regs *regs,
         if ( (info.dabt_instr.state == INSTR_VALID) && check_p2m(is_data, gpa) )
             return;
 
-        state = try_handle_mmio(regs, &info);
-
-        switch ( state )
-        {
-            case IO_ABORT:
-                goto inject_abt;
-            case IO_HANDLED:
-                /*
-                 * If the instruction was decoded and has executed successfully
-                 * on the MMIO region, then Xen should execute the next part of
-                 * the instruction. (for eg increment the rn if it is a
-                 * post-indexing instruction.
-                 */
-                finalize_instr_emulation(&info.dabt_instr);
-                advance_pc(regs, hsr);
-                return;
-            case IO_RETRY:
-                /* finish later */
-                return;
-            case IO_UNHANDLED:
-                /* IO unhandled, try another way to handle it. */
-                break;
-        }
+        if ( do_mmio_trap_stage2_abort_guest(regs, hsr, &info, gva, gpa) == 0 )
+            return;
 
         break;
     }
Signature · 1 line
2.34.1
#12 lore raw

PATCH 6/8 xen/arm: dump debug message in MPU system

In reply to Luca Fancellu

From: Penny Zheng <[email protected]>

A set of helpers dump_xxx and show_registers are responsible for dumping memory mapping info and register info when debugging. In this commit, we implement them all in MPU system too.

---
 xen/arch/arm/mpu/mm.c  | 12 +++++++++++-
 xen/arch/arm/mpu/p2m.c | 40 +++++++++++++++++++++++++++++++++++++++-
 xen/arch/arm/traps.c   |  8 ++++++++
 3 files changed, 58 insertions(+), 2 deletions(-)
xen/arch/arm/mpu/mm.c +11 -1
index 5ed77355a5f9..459fb952d125 100644
@@ -554,7 +554,17 @@ int modify_xen_mappings(unsigned long s, unsigned long e, unsigned int nf)
 
 void dump_hyp_walk(vaddr_t addr)
 {
-    BUG_ON("unimplemented");
+    uint8_t i = 0;
+    pr_t region;
+
+    for ( i = 0; i < max_mpu_regions; i++ )
+    {
+        read_protection_region(&region, i);
+        if ( region_is_valid(&region) )
+            printk(XENLOG_INFO
+                   "Walking hypervisor MPU memory region [%u]: 0x%"PRIpaddr"-0x%"PRIpaddr"\n",
+                   i, pr_get_base(&region), pr_get_limit(&region));
+    }
 }
 
 /* Release all __init and __initdata ranges to be reused */
xen/arch/arm/mpu/p2m.c +39 -1
index bf87c65c106c..06c92a3ef41b 100644
@@ -215,9 +215,47 @@ mfn_t p2m_get_entry(struct p2m_domain *p2m, gfn_t gfn,
     return p2m_get_mpu_region(p2m, gfn, 1, t, valid);
 }
 
+static void dump_mpu_walk(pr_t *table, uint8_t nr_regions)
+{
+    uint8_t i = 0;
+
+    for ( ; i < nr_regions; i++ )
+    {
+        paddr_t base, limit;
+
+        if ( region_is_valid(&table[i]) )
+        {
+            base = pr_get_base(&table[i]);
+            limit = pr_get_limit(&table[i]);
+
+            printk(XENLOG_INFO
+                   "Walking MPU memory mapping table: Region[%u]: 0x%"PRIpaddr"-0x%"PRIpaddr"\n",
+                   i, base, limit);
+        }
+    }
+}
+
+void dump_p2m_lookup(struct domain *d, paddr_t addr)
+{
+    struct p2m_domain *p2m = p2m_get_hostp2m(d);
+
+    printk("dom%d IPA 0x%"PRIpaddr"\n", d->domain_id, addr);
+
+    printk("P2M @ %p mfn:%#"PRI_mfn"\n",
+           p2m->root, mfn_x(page_to_mfn(p2m->root)));
+
+    dump_mpu_walk((pr_t *)page_to_virt(p2m->root), p2m->nr_regions);
+}
+
 void p2m_dump_info(struct domain *d)
 {
-    BUG_ON("unimplemented");
+    struct p2m_domain *p2m = p2m_get_hostp2m(d);
+
+    p2m_read_lock(p2m);
+    printk("p2m mappings for domain %d (vmid %d):\n",
+           d->domain_id, p2m->vmid);
+    printk("  Number of P2M Memory Region: %u \n", p2m->nr_regions);
+    p2m_read_unlock(p2m);
 }
 
 static int p2m_alloc_table(struct domain *d)
xen/arch/arm/traps.c +8 -0
index 081bd2e51979..fba7d6c00e37 100644
@@ -718,6 +718,8 @@ struct reg_ctxt {
 #ifdef CONFIG_MMU
     /* Hypervisor-side state */
     uint64_t vttbr_el2;
+#else
+    uint64_t vsctlr_el2;
 #endif
 };
 
@@ -918,6 +920,8 @@ static void _show_registers(const struct cpu_user_regs *regs,
     printk("  VTCR_EL2: %"PRIregister"\n", READ_SYSREG(VTCR_EL2));
 #ifdef CONFIG_MMU
     printk(" VTTBR_EL2: %016"PRIx64"\n", ctxt->vttbr_el2);
+#else
+    printk(" VSCTLR_EL2: %016"PRIx64"\n", ctxt->vsctlr_el2);
 #endif
     printk("\n");
 
@@ -959,6 +963,8 @@ void show_registers(const struct cpu_user_regs *regs)
 #endif
 #ifdef CONFIG_MMU
     ctxt.vttbr_el2 = READ_SYSREG64(VTTBR_EL2);
+#else
+    ctxt.vsctlr_el2 = READ_SYSREG(VSCTLR_EL2);
 #endif
 
     _show_registers(regs, &ctxt, guest_mode(regs), current);
@@ -985,6 +991,8 @@ void vcpu_show_registers(struct vcpu *v)
 
 #ifdef CONFIG_MMU
     ctxt.vttbr_el2 = v->domain->arch.p2m.vttbr;
+#else
+    ctxt.vsctlr_el2 = v->domain->arch.p2m.vsctlr;
 #endif
 
     _show_registers(&v->arch.cpu_info->guest_cpu_user_regs, &ctxt, 1, v);
Signature · 1 line
2.34.1
#13 lore raw

Re: [PATCH 6/8] xen/arm: dump debug message in MPU system

Hi Luca,

Quoted 43 lines · Luca Fancellu

From: Penny Zheng <[email protected]>

A set of helpers dump_xxx and show_registers are responsible for dumping memory mapping info and register info when debugging. In this commit, we implement them all in MPU system too.

---
  xen/arch/arm/mpu/mm.c  | 12 +++++++++++-
  xen/arch/arm/mpu/p2m.c | 40 +++++++++++++++++++++++++++++++++++++++-
  xen/arch/arm/traps.c   |  8 ++++++++
  3 files changed, 58 insertions(+), 2 deletions(-)
xen/arch/arm/mpu/mm.c +11 -1
index 5ed77355a5f9..459fb952d125 100644
@@ -554,7 +554,17 @@ int modify_xen_mappings(unsigned long s, unsigned long e, unsigned int nf)
 
  void dump_hyp_walk(vaddr_t addr)
  {
-    BUG_ON("unimplemented");
+    uint8_t i = 0;
+    pr_t region;
+
+    for ( i = 0; i < max_mpu_regions; i++ )
+    {
+        read_protection_region(&region, i);
+        if ( region_is_valid(&region) )
+            printk(XENLOG_INFO
+                   "Walking hypervisor MPU memory region [%u]: 0x%"PRIpaddr"-0x%"PRIpaddr"\n",
+                   i, pr_get_base(&region), pr_get_limit(&region));
+    }
  }
 
  /* Release all __init and __initdata ranges to be reused */
xen/arch/arm/mpu/p2m.c +0 -0
index bf87c65c106c..06c92a3ef41b 100644

xen/arch/arm/mpu/p2m.c mfn_t p2m_get_entry(struct p2m_domain *p2m, gfn_t gfn, · line 222

Quoted 8 lines
xen/arch/arm/mpu/p2m.c +5 -0
@@ -215,9 +215,47 @@ mfn_t p2m_get_entry(struct p2m_domain *p2m, gfn_t gfn,
      return p2m_get_mpu_region(p2m, gfn, 1, t, valid);
  }
 
+static void dump_mpu_walk(pr_t *table, uint8_t nr_regions)
+{
+    uint8_t i = 0;
+
+    for ( ; i < nr_regions; i++ )

NIT: for ( uint8_t i = 0; ... ).

Quoted 10 lines
xen/arch/arm/mpu/p2m.c +10 -0
+    {
+        paddr_t base, limit;
+
+        if ( region_is_valid(&table[i]) )
+        {
+            base = pr_get_base(&table[i]);
+            limit = pr_get_limit(&table[i]);
+
+            printk(XENLOG_INFO
+                   "Walking MPU memory mapping table: Region[%u]: 0x%"PRIpaddr"-0x%"PRIpaddr"\n",

xen/arch/arm/mpu/p2m.c mfn_t p2m_get_entry(struct p2m_domain *p2m, gfn_t gfn, · line 242

Quoted 10 lines
xen/arch/arm/mpu/p2m.c +10 -0
+                   i, base, limit);
+        }
+    }
+}
+
+void dump_p2m_lookup(struct domain *d, paddr_t addr)
+{
+    struct p2m_domain *p2m = p2m_get_hostp2m(d);
+
+    printk("dom%d IPA 0x%"PRIpaddr"\n", d->domain_id, addr);

Shouldn't we take a lock here like we do for the next function ?

p2m_read_lock(p2m);

Quoted 6 lines
xen/arch/arm/mpu/p2m.c +6 -0
+
+    printk("P2M @ %p mfn:%#"PRI_mfn"\n",
+           p2m->root, mfn_x(page_to_mfn(p2m->root)));
+
+    dump_mpu_walk((pr_t *)page_to_virt(p2m->root), p2m->nr_regions);
+}

xen/arch/arm/mpu/p2m.c mfn_t p2m_get_entry(struct p2m_domain *p2m, gfn_t gfn, · line 257

Quoted 10 lines
xen/arch/arm/mpu/p2m.c +7 -1
+
  void p2m_dump_info(struct domain *d)
  {
-    BUG_ON("unimplemented");
+    struct p2m_domain *p2m = p2m_get_hostp2m(d);
+
+    p2m_read_lock(p2m);
+    printk("p2m mappings for domain %d (vmid %d):\n",
+           d->domain_id, p2m->vmid);
+    printk("  Number of P2M Memory Region: %u \n", p2m->nr_regions);

Trailing whitespace before the newline.

Quoted 36 lines
xen/arch/arm/mpu/p2m.c +1 -0
+    p2m_read_unlock(p2m);
  }
 
  static int p2m_alloc_table(struct domain *d)
xen/arch/arm/traps.c +6 -0
index 081bd2e51979..fba7d6c00e37 100644
@@ -718,6 +718,8 @@ struct reg_ctxt {
  #ifdef CONFIG_MMU
      /* Hypervisor-side state */
      uint64_t vttbr_el2;
+#else
+    uint64_t vsctlr_el2;
  #endif
  };
 
@@ -918,6 +920,8 @@ static void _show_registers(const struct cpu_user_regs *regs,
      printk("  VTCR_EL2: %"PRIregister"\n", READ_SYSREG(VTCR_EL2));
  #ifdef CONFIG_MMU
      printk(" VTTBR_EL2: %016"PRIx64"\n", ctxt->vttbr_el2);
+#else
+    printk(" VSCTLR_EL2: %016"PRIx64"\n", ctxt->vsctlr_el2);
  #endif
      printk("\n");
 
@@ -959,6 +963,8 @@ void show_registers(const struct cpu_user_regs *regs)
  #endif
  #ifdef CONFIG_MMU
      ctxt.vttbr_el2 = READ_SYSREG64(VTTBR_EL2);
+#else
+    ctxt.vsctlr_el2 = READ_SYSREG(VSCTLR_EL2);
  #endif
 
      _show_registers(regs, &ctxt, guest_mode(regs), current);

xen/arch/arm/traps.c void vcpu_show_registers(struct vcpu *v) · line 998

Quoted 8 lines
xen/arch/arm/traps.c +2 -0
@@ -985,6 +991,8 @@ void vcpu_show_registers(struct vcpu *v)
 
  #ifdef CONFIG_MMU
      ctxt.vttbr_el2 = v->domain->arch.p2m.vttbr;
+#else
+    ctxt.vsctlr_el2 = v->domain->arch.p2m.vsctlr;
  #endif
 
      _show_registers(&v->arch.cpu_info->guest_cpu_user_regs, &ctxt, 1, v);

- Ayan

#14 lore raw

PATCH 7/8 arm/mpu: Save/restore guest EL1 MPU-related context

In reply to Luca Fancellu

Implement save/restore of guest MPU-related context, as all MPU memory regions must be saved/restored on vCPU context switch when domain is using PMSAv8-64 translation regime.

Introduce is_mpu_domain() helper function that is always false on MMU and it checks if PMSA is selected at EL1 for MPU domains.

Split the save/restore functions into MPU arm64 and arm32, the latter is currently not implemented, the former is fully implemented and uses the PR{B,L}AR<n>_EL1 register access in order to issue the minimum amount of ISB as possible while changing the selector and writing/reading the registers.

struct arch_vcpu now includes a pointer to the allocated storage for the additional MPU regions to be saved on context switch.

---
 xen/arch/arm/arm64/mpu/p2m.c          |   2 +
 xen/arch/arm/domain.c                 |  49 +++++++---
 xen/arch/arm/include/asm/domain.h     |   5 +
 xen/arch/arm/include/asm/mm.h         |   3 +
 xen/arch/arm/include/asm/mmu/mm.h     |  12 +++
 xen/arch/arm/include/asm/mpu.h        |   3 +
 xen/arch/arm/include/asm/mpu/cpregs.h |   3 +
 xen/arch/arm/include/asm/mpu/mm.h     |  10 ++
 xen/arch/arm/mpu/arm32/mm.c           |  10 ++
 xen/arch/arm/mpu/arm64/mm.c           | 136 ++++++++++++++++++++++++++
 xen/arch/arm/mpu/mm.c                 |  39 ++++++++
 11 files changed, 260 insertions(+), 12 deletions(-)
xen/arch/arm/arm64/mpu/p2m.c +2 -0
index a39a1fc38946..a1ec9fcd6195 100644
@@ -62,6 +62,8 @@ void __init setup_virt_paging(void)
 
     p2m_vmid_allocator_init();
 
+    load_nr_mpu_regions_el1();
+
     /*
      * 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
xen/arch/arm/domain.c +37 -12

87 lines not on this page · raw message

xen/arch/arm/include/asm/domain.h +5 -0
index 4a3fb825962b..44c304ef8aa9 100644
@@ -202,6 +202,11 @@ struct arch_vcpu
     register_t actlr;
     uint32_t cpacr;
 
+#ifdef CONFIG_MPU
+    /* EL1 MPU memory regions */
+    pr_t *mpu_regions;
+#endif
+
     uint32_t contextidr;
     register_t tpidr_el0;
     register_t tpidr_el1;
xen/arch/arm/include/asm/mm.h +3 -0
index 72a692862420..9e35800aba5e 100644
@@ -202,6 +202,9 @@ extern void setup_frametable_mappings(paddr_t ps, paddr_t pe);
 void setup_mm_helper(void);
 /* map a physical range in virtual memory */
 void __iomem *ioremap_attr(paddr_t start, size_t len, unsigned int attributes);
+/* Allocate space for EL1 MPU region info */
+void allocate_el1_mpu_regions(struct vcpu *v);
+void free_el1_mpu_regions(struct vcpu *v);
 
 static inline void __iomem *ioremap_nocache(paddr_t start, size_t len)
 {
xen/arch/arm/include/asm/mmu/mm.h +12 -0
index 7f4d59137d0d..c441af50c3a9 100644
@@ -9,6 +9,8 @@
 #include <asm/mmu/layout.h>
 #include <asm/page.h>
 
+struct domain;
+
 /* Non-boot CPUs use this to find the correct pagetables. */
 extern uint64_t init_ttbr;
 
@@ -91,6 +93,16 @@ static inline struct page_info *virt_to_page(const void *v)
     return frame_table + pdx - frametable_base_pdx;
 }
 
+/* If this domain should use PMSAv8-64 translation regime (MPU) at EL1. */
+static inline bool is_mpu_domain(struct domain *d)
+{
+    return false;
+}
+
+/* Stub for MPU EL1 region save/restore */
+static inline void save_el1_mpu_regions(struct vcpu *p) { }
+static inline void restore_el1_mpu_regions(struct vcpu *n) { }
+
 /*
  * Print a walk of a page table or p2m
  *
xen/arch/arm/include/asm/mpu.h +3 -0
index 8a8c01086206..525944987713 100644
@@ -27,6 +27,9 @@
 
 #ifndef __ASSEMBLER__
 
+/* Load the number of regions supported by the EL1 MPU from MPUIR_EL1. */
+void load_nr_mpu_regions_el1(void);
+
 /*
  * Utility function to determine if an Armv8-R processor supports VMSA.
  */
xen/arch/arm/include/asm/mpu/cpregs.h +3 -0
index 5a3d92cf5389..f6fb3c93c032 100644
@@ -5,6 +5,7 @@
 
 /* CP15 CR0: MPU Type Register */
 #define HMPUIR          p15,4,c0,c0,4
+#define MPUIR           p15,0,c0,c0,4
 
 /* CP15 CR2: Virtualization System Control register */
 #define VSCTLR          p15,4,c2,c0,1
@@ -16,6 +17,7 @@
 #define HPRSELR         p15,4,c6,c2,1
 #define HPRBAR          p15,4,c6,c3,0
 #define HPRLAR          p15,4,c6,c3,1
+#define PRSELR          p15,0,c6,c2,1
 
 /* CP15 CR6: MPU Protection Region Base/Limit Address Register */
 #define HPRBAR0         p15,4,c6,c8,0
@@ -86,6 +88,7 @@
 /* Aliases of AArch64 names for use in common code */
 #ifdef CONFIG_ARM_32
 /* Alphabetically... */
+#define MPUIR_EL1       MPUIR
 #define MPUIR_EL2       HMPUIR
 #define PRBAR_EL2       HPRBAR
 #define PRENR_EL2       HPRENR
xen/arch/arm/include/asm/mpu/mm.h +10 -0
index 24bffdee4fb6..a536ec4431df 100644
@@ -20,6 +20,7 @@
 extern struct page_info *frame_table;
 
 extern uint8_t max_mpu_regions;
+extern uint8_t max_mpu_regions_el1;
 
 extern DECLARE_BITMAP(xen_mpumap_mask, MAX_MPU_REGION_NR);
 
@@ -55,6 +56,15 @@ static inline void context_sync_mpu(void)
     isb();
 }
 
+/* If this domain should use PMSAv8-64 translation regime (MPU) at EL1. */
+bool is_mpu_domain(struct domain *d);
+
+/* Save EL1 MPU base and limit registers. */
+void save_el1_mpu_regions(struct vcpu *p);
+
+/* Restore EL1 MPU base and limit registers. */
+void restore_el1_mpu_regions(struct vcpu *n);
+
 /*
  * The following API requires context_sync_mpu() after being used to modify MPU
  * regions:
xen/arch/arm/mpu/arm32/mm.c +10 -0
index 5eaeb3400e6c..3dd71228b4ed 100644
@@ -159,6 +159,16 @@ void write_protection_region(const pr_t *pr_write, uint8_t sel)
     }
 }
 
+void save_el1_mpu_regions(struct vcpu *p)
+{
+    BUG_ON("unimplemented");
+}
+
+void restore_el1_mpu_regions(struct vcpu *n)
+{
+    BUG_ON("unimplemented");
+}
+
 /*
  * Local variables:
  * mode: C
xen/arch/arm/mpu/arm64/mm.c +136 -0

165 lines not on this page · raw message

xen/arch/arm/mpu/mm.c +39 -0
index 459fb952d125..d6703bf0c005 100644
@@ -7,6 +7,7 @@
 #include <xen/mm.h>
 #include <xen/pfn.h>
 #include <xen/sizes.h>
+#include <xen/sched.h>
 #include <xen/spinlock.h>
 #include <xen/types.h>
 #include <asm/mpu.h>
@@ -14,6 +15,7 @@
 #include <asm/page.h>
 #include <asm/setup.h>
 #include <asm/sysregs.h>
+#include <public/arch-arm.h>
 
 #define MPU_ATTR_XN_RO_MISMATCH     -1
 #define MPU_ATTR_AI_MISMATCH        -2
@@ -23,6 +25,9 @@ struct page_info *frame_table;
 /* Maximum number of supported MPU memory regions by the EL2 MPU. */
 uint8_t __ro_after_init max_mpu_regions;
 
+/* Maximum number of supported MPU memory regions by the EL1 MPU. */
+uint8_t __read_mostly max_mpu_regions_el1;
+
 /*
  * Bitmap xen_mpumap_mask is to record the usage of EL2 MPU memory regions.
  * Bit 0 represents MPU memory region 0, bit 1 represents MPU memory
@@ -631,6 +636,40 @@ void __iomem *ioremap_attr(paddr_t start, size_t len, unsigned int flags)
     return maddr_to_virt(start);
 }
 
+bool is_mpu_domain(struct domain *d)
+{
+    return (d->arch.v8r_el1_msa == XEN_DOMCTL_CONFIG_ARM_V8R_EL1_MSA_NONE) ||
+           (d->arch.v8r_el1_msa == XEN_DOMCTL_CONFIG_ARM_V8R_EL1_MSA_PMSA);
+}
+
+void load_nr_mpu_regions_el1(void)
+{
+    max_mpu_regions_el1 = (uint8_t)(READ_SYSREG(MPUIR_EL1) &
+                                    NUM_MPU_REGIONS_MASK);
+}
+
+void allocate_el1_mpu_regions(struct vcpu *v)
+{
+    if ( max_mpu_regions_el1 == 0 )
+        return;
+
+    if ( v->arch.mpu_regions )
+        return;
+
+    v->arch.mpu_regions = (pr_t *)_xzalloc(sizeof(pr_t) * max_mpu_regions_el1,
+                                           SMP_CACHE_BYTES);
+    if ( !v->arch.mpu_regions )
+        panic("DOM%pd: mpu: no memory to store EL1 MPU memory region info\n",
+              v->domain);
+    memset(v->arch.mpu_regions, 0, sizeof(pr_t) * max_mpu_regions_el1);
+}
+
+void free_el1_mpu_regions(struct vcpu *v)
+{
+    if ( v->arch.mpu_regions )
+        xfree(v->arch.mpu_regions);
+}
+
 /*
  * Local variables:
  * mode: C
Signature · 1 line
2.34.1
#15 lore raw

Re: [PATCH 7/8] arm/mpu: Save/restore guest EL1 MPU-related context

Hi Luca,

Quoted 355 lines · Luca Fancellu

Implement save/restore of guest MPU-related context, as all MPU memory regions must be saved/restored on vCPU context switch when domain is using PMSAv8-64 translation regime.

Introduce is_mpu_domain() helper function that is always false on MMU and it checks if PMSA is selected at EL1 for MPU domains.

Split the save/restore functions into MPU arm64 and arm32, the latter is currently not implemented, the former is fully implemented and uses the PR{B,L}AR<n>_EL1 register access in order to issue the minimum amount of ISB as possible while changing the selector and writing/reading the registers.

struct arch_vcpu now includes a pointer to the allocated storage for the additional MPU regions to be saved on context switch.

---
  xen/arch/arm/arm64/mpu/p2m.c          |   2 +
  xen/arch/arm/domain.c                 |  49 +++++++---
  xen/arch/arm/include/asm/domain.h     |   5 +
  xen/arch/arm/include/asm/mm.h         |   3 +
  xen/arch/arm/include/asm/mmu/mm.h     |  12 +++
  xen/arch/arm/include/asm/mpu.h        |   3 +
  xen/arch/arm/include/asm/mpu/cpregs.h |   3 +
  xen/arch/arm/include/asm/mpu/mm.h     |  10 ++
  xen/arch/arm/mpu/arm32/mm.c           |  10 ++
  xen/arch/arm/mpu/arm64/mm.c           | 136 ++++++++++++++++++++++++++
  xen/arch/arm/mpu/mm.c                 |  39 ++++++++
  11 files changed, 260 insertions(+), 12 deletions(-)
xen/arch/arm/arm64/mpu/p2m.c +2 -0
index a39a1fc38946..a1ec9fcd6195 100644
@@ -62,6 +62,8 @@ void __init setup_virt_paging(void)
 
      p2m_vmid_allocator_init();
 
+    load_nr_mpu_regions_el1();
+
      /*
       * 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
xen/arch/arm/domain.c +37 -12
index dfa7ace1141b..221b1f8a7f9d 100644
@@ -123,11 +123,23 @@ static void ctxt_switch_from(struct vcpu *p)
 
      isb();
 
-    /* MMU */
+    /* MMU/MPU */
+    if ( is_mpu_domain(p->domain) )
+    {
+        /*
+         * Domain is using PMSAv8-64 translation regime at EL1, so we need to
+         * save EL1 MPU memory regions.
+         */
+        save_el1_mpu_regions(p);
+    }
+    else
+    {
+        /* Domain is using VMSAv8-64 translation regime via MMU. */
+        p->arch.ttbcr = READ_SYSREG(TCR_EL1);
+        p->arch.ttbr0 = READ_SYSREG64(TTBR0_EL1);
+        p->arch.ttbr1 = READ_SYSREG64(TTBR1_EL1);
+    }
      p->arch.vbar = READ_SYSREG(VBAR_EL1);
-    p->arch.ttbcr = READ_SYSREG(TCR_EL1);
-    p->arch.ttbr0 = READ_SYSREG64(TTBR0_EL1);
-    p->arch.ttbr1 = READ_SYSREG64(TTBR1_EL1);
      if ( is_32bit_domain(p->domain) )
          p->arch.dacr = READ_SYSREG(DACR32_EL2);
      p->arch.par = read_sysreg_par();
@@ -156,8 +168,6 @@ static void ctxt_switch_from(struct vcpu *p)
      p->arch.afsr0 = READ_SYSREG(AFSR0_EL1);
      p->arch.afsr1 = READ_SYSREG(AFSR1_EL1);
 
-    /* XXX MPU */
-
      /* VGIC */
      gic_save_state(p);
 
@@ -181,8 +191,6 @@ static void ctxt_switch_to(struct vcpu *n)
      /* VGIC */
      gic_restore_state(n);
 
-    /* XXX MPU */
-
      /* Fault Status */
  #if defined(CONFIG_ARM_32)
      WRITE_CP32(n->arch.dfar, DFAR);
@@ -198,11 +206,23 @@ static void ctxt_switch_to(struct vcpu *n)
      WRITE_SYSREG(n->arch.afsr0, AFSR0_EL1);
      WRITE_SYSREG(n->arch.afsr1, AFSR1_EL1);
 
-    /* MMU */
+    /* MMU/MPU */
+    if ( is_mpu_domain(n->domain) )
+    {
+        /*
+         * Domain is using PMSAv8-64 translation regime at EL1, so we need to
+         * restore EL1 MPU memory regions.
+         */
+        restore_el1_mpu_regions(n);
+    }
+    else
+    {
+        /* Domain is using VMSAv8-64 translation regime via MMU. */
+        WRITE_SYSREG(n->arch.ttbcr, TCR_EL1);
+        WRITE_SYSREG64(n->arch.ttbr0, TTBR0_EL1);
+        WRITE_SYSREG64(n->arch.ttbr1, TTBR1_EL1);
+    }
      WRITE_SYSREG(n->arch.vbar, VBAR_EL1);
-    WRITE_SYSREG(n->arch.ttbcr, TCR_EL1);
-    WRITE_SYSREG64(n->arch.ttbr0, TTBR0_EL1);
-    WRITE_SYSREG64(n->arch.ttbr1, TTBR1_EL1);
 
      /*
       * Erratum #852523 (Cortex-A57) or erratum #853709 (Cortex-A72):
@@ -518,6 +538,9 @@ int arch_vcpu_create(struct vcpu *v)
      if ( get_ssbd_state() == ARM_SSBD_RUNTIME )
          v->arch.cpu_info->flags |= CPUINFO_WORKAROUND_2_FLAG;
 
+    if ( is_mpu_domain(v->domain) )
+        allocate_el1_mpu_regions(v);
+
      return rc;
 
  fail:
@@ -532,6 +555,8 @@ void arch_vcpu_destroy(struct vcpu *v)
      vcpu_timer_destroy(v);
      vcpu_vgic_free(v);
      free_xenheap_pages(v->arch.stack, STACK_ORDER);
+    if ( is_mpu_domain(v->domain) )
+        free_el1_mpu_regions(v);
  }
 
  void vcpu_switch_to_aarch64_mode(struct vcpu *v)
xen/arch/arm/include/asm/domain.h +5 -0
index 4a3fb825962b..44c304ef8aa9 100644
@@ -202,6 +202,11 @@ struct arch_vcpu
      register_t actlr;
      uint32_t cpacr;
 
+#ifdef CONFIG_MPU
+    /* EL1 MPU memory regions */
+    pr_t *mpu_regions;
+#endif
+
      uint32_t contextidr;
      register_t tpidr_el0;
      register_t tpidr_el1;
xen/arch/arm/include/asm/mm.h +3 -0
index 72a692862420..9e35800aba5e 100644
@@ -202,6 +202,9 @@ extern void setup_frametable_mappings(paddr_t ps, paddr_t pe);
  void setup_mm_helper(void);
  /* map a physical range in virtual memory */
  void __iomem *ioremap_attr(paddr_t start, size_t len, unsigned int attributes);
+/* Allocate space for EL1 MPU region info */
+void allocate_el1_mpu_regions(struct vcpu *v);
+void free_el1_mpu_regions(struct vcpu *v);
 
  static inline void __iomem *ioremap_nocache(paddr_t start, size_t len)
  {
xen/arch/arm/include/asm/mmu/mm.h +12 -0
index 7f4d59137d0d..c441af50c3a9 100644
@@ -9,6 +9,8 @@
  #include <asm/mmu/layout.h>
  #include <asm/page.h>
 
+struct domain;
+
  /* Non-boot CPUs use this to find the correct pagetables. */
  extern uint64_t init_ttbr;
 
@@ -91,6 +93,16 @@ static inline struct page_info *virt_to_page(const void *v)
      return frame_table + pdx - frametable_base_pdx;
  }
 
+/* If this domain should use PMSAv8-64 translation regime (MPU) at EL1. */
+static inline bool is_mpu_domain(struct domain *d)
+{
+    return false;
+}
+
+/* Stub for MPU EL1 region save/restore */
+static inline void save_el1_mpu_regions(struct vcpu *p) { }
+static inline void restore_el1_mpu_regions(struct vcpu *n) { }
+
  /*
   * Print a walk of a page table or p2m
   *
xen/arch/arm/include/asm/mpu.h +3 -0
index 8a8c01086206..525944987713 100644
@@ -27,6 +27,9 @@
 
  #ifndef __ASSEMBLER__
 
+/* Load the number of regions supported by the EL1 MPU from MPUIR_EL1. */
+void load_nr_mpu_regions_el1(void);
+
  /*
   * Utility function to determine if an Armv8-R processor supports VMSA.
   */
xen/arch/arm/include/asm/mpu/cpregs.h +3 -0
index 5a3d92cf5389..f6fb3c93c032 100644
@@ -5,6 +5,7 @@
 
  /* CP15 CR0: MPU Type Register */
  #define HMPUIR          p15,4,c0,c0,4
+#define MPUIR           p15,0,c0,c0,4
 
  /* CP15 CR2: Virtualization System Control register */
  #define VSCTLR          p15,4,c2,c0,1
@@ -16,6 +17,7 @@
  #define HPRSELR         p15,4,c6,c2,1
  #define HPRBAR          p15,4,c6,c3,0
  #define HPRLAR          p15,4,c6,c3,1
+#define PRSELR          p15,0,c6,c2,1
 
  /* CP15 CR6: MPU Protection Region Base/Limit Address Register */
  #define HPRBAR0         p15,4,c6,c8,0
@@ -86,6 +88,7 @@
  /* Aliases of AArch64 names for use in common code */
  #ifdef CONFIG_ARM_32
  /* Alphabetically... */
+#define MPUIR_EL1       MPUIR
  #define MPUIR_EL2       HMPUIR
  #define PRBAR_EL2       HPRBAR
  #define PRENR_EL2       HPRENR
xen/arch/arm/include/asm/mpu/mm.h +10 -0
index 24bffdee4fb6..a536ec4431df 100644
@@ -20,6 +20,7 @@
  extern struct page_info *frame_table;
 
  extern uint8_t max_mpu_regions;
+extern uint8_t max_mpu_regions_el1;
 
  extern DECLARE_BITMAP(xen_mpumap_mask, MAX_MPU_REGION_NR);
 
@@ -55,6 +56,15 @@ static inline void context_sync_mpu(void)
      isb();
  }
 
+/* If this domain should use PMSAv8-64 translation regime (MPU) at EL1. */
+bool is_mpu_domain(struct domain *d);
+
+/* Save EL1 MPU base and limit registers. */
+void save_el1_mpu_regions(struct vcpu *p);
+
+/* Restore EL1 MPU base and limit registers. */
+void restore_el1_mpu_regions(struct vcpu *n);
+
  /*
   * The following API requires context_sync_mpu() after being used to modify MPU
   * regions:
xen/arch/arm/mpu/arm32/mm.c +10 -0
index 5eaeb3400e6c..3dd71228b4ed 100644
@@ -159,6 +159,16 @@ void write_protection_region(const pr_t *pr_write, uint8_t sel)
      }
  }
 
+void save_el1_mpu_regions(struct vcpu *p)
+{
+    BUG_ON("unimplemented");
+}
+
+void restore_el1_mpu_regions(struct vcpu *n)
+{
+    BUG_ON("unimplemented");
+}
+
  /*
   * Local variables:
   * mode: C
xen/arch/arm/mpu/arm64/mm.c +29 -0
index b07e729a7d05..ce9947851173 100644
@@ -1,8 +1,10 @@
  /* SPDX-License-Identifier: GPL-2.0-only */
 
  #include <xen/bug.h>
+#include <xen/sched.h>
  #include <xen/types.h>
  #include <asm/mpu.h>
+#include <asm/mpu/mm.h>
  #include <asm/sysregs.h>
  #include <asm/system.h>
 
@@ -10,9 +12,13 @@
   * The following are needed for the cases: GENERATE_WRITE_PR_REG_CASE
   * and GENERATE_READ_PR_REG_CASE with num==0
   */
+#define PRBAR0_EL1 PRBAR_EL1
+#define PRLAR0_EL1 PRLAR_EL1
  #define PRBAR0_EL2 PRBAR_EL2
  #define PRLAR0_EL2 PRLAR_EL2
 
+#define PRBAR_EL1_(n)   PRBAR##n##_EL1
+#define PRLAR_EL1_(n)   PRLAR##n##_EL1
  #define PRBAR_EL2_(n)   PRBAR##n##_EL2
  #define PRLAR_EL2_(n)   PRLAR##n##_EL2
 
@@ -32,6 +38,22 @@
          break;                                                  \
      }
 
+#define GENERATE_SAVE_EL1_PR_REG_CASE(num, pr, sel)             \
+    case num:                                                   \
+    {                                                           \
+        pr->prbar.bits = READ_SYSREG(PRBAR_EL1_(num));          \
+        pr->prlar.bits = READ_SYSREG(PRLAR_EL1_(num));          \
+        sel--;                                                  \
+    }
+
+#define GENERATE_RESTORE_EL1_PR_REG_CASE(num, pr, sel)                      \
+    case num:                                                               \
+    {                                                                       \
+        WRITE_SYSREG(pr->prbar.bits & ~MPU_REGION_RES0, PRBAR_EL1_(num));   \
+        WRITE_SYSREG(pr->prlar.bits & ~MPU_REGION_RES0, PRLAR_EL1_(num));   \
+        sel--;                                                              \
+    }
+
  bool has_v8r_vmsa_support(void)
  {
      return system_cpuinfo.mm64.msa_frac == MM64_MSA_FRAC_VMSA_SUPPORT;
@@ -125,6 +147,120 @@ void write_protection_region(const pr_t *pr_write, uint8_t sel)
      }
  }
 
+void save_el1_mpu_regions(struct vcpu *p)
+{
+    int sel = max_mpu_regions_el1 - 1;
+    pr_t *table;
+
+    if ( max_mpu_regions_el1 == 0 )
+        return;

xen/arch/arm/mpu/arm64/mm.c void write_protection_region(const pr_t *pr_write, uint8_t sel) · line 166

Quoted 10 lines
xen/arch/arm/mpu/arm64/mm.c +10 -0
+
+    if ( p->arch.mpu_regions == NULL )
+        return;
+    table = p->arch.mpu_regions;
+
+    while ( sel >= 0 )
+    {
+        WRITE_SYSREG( (sel & 0xF0), PRSELR_EL1);
+        isb();
+        switch ( sel & 0xF ) {

Stray space after the opening paren in WRITE_SYSREG, and the switch brace should be on its own line. The case-label indentation in the ladder below is also uneven (default is indented deeper than the generated cases).

Quoted 131 lines
xen/arch/arm/mpu/arm64/mm.c +97 -0
+            GENERATE_SAVE_EL1_PR_REG_CASE(15, (&table[sel]), sel);
+            fallthrough;
+            GENERATE_SAVE_EL1_PR_REG_CASE(14, (&table[sel]), sel);
+            fallthrough;
+            GENERATE_SAVE_EL1_PR_REG_CASE(13, (&table[sel]), sel);
+            fallthrough;
+            GENERATE_SAVE_EL1_PR_REG_CASE(12, (&table[sel]), sel);
+            fallthrough;
+            GENERATE_SAVE_EL1_PR_REG_CASE(11, (&table[sel]), sel);
+            fallthrough;
+            GENERATE_SAVE_EL1_PR_REG_CASE(10, (&table[sel]), sel);
+            fallthrough;
+            GENERATE_SAVE_EL1_PR_REG_CASE(9, (&table[sel]), sel);
+            fallthrough;
+            GENERATE_SAVE_EL1_PR_REG_CASE(8, (&table[sel]), sel);
+            fallthrough;
+            GENERATE_SAVE_EL1_PR_REG_CASE(7, (&table[sel]), sel);
+            fallthrough;
+            GENERATE_SAVE_EL1_PR_REG_CASE(6, (&table[sel]), sel);
+            fallthrough;
+            GENERATE_SAVE_EL1_PR_REG_CASE(5, (&table[sel]), sel);
+            fallthrough;
+            GENERATE_SAVE_EL1_PR_REG_CASE(4, (&table[sel]), sel);
+            fallthrough;
+            GENERATE_SAVE_EL1_PR_REG_CASE(3, (&table[sel]), sel);
+            fallthrough;
+            GENERATE_SAVE_EL1_PR_REG_CASE(2, (&table[sel]), sel);
+            fallthrough;
+            GENERATE_SAVE_EL1_PR_REG_CASE(1, (&table[sel]), sel);
+            fallthrough;
+            GENERATE_SAVE_EL1_PR_REG_CASE(0, (&table[sel]), sel);
+            break;
+            default:
+                BUG(); /* Can't happen */
+                break;
+        }
+    }
+}
+
+void restore_el1_mpu_regions(struct vcpu *n)
+{
+    int sel = max_mpu_regions_el1 - 1;
+    pr_t *table;
+
+    if ( max_mpu_regions_el1 == 0 )
+        return;
+
+    if ( !n->arch.mpu_regions )
+        return;
+    table = n->arch.mpu_regions;
+
+    while ( sel >= 0 )
+    {
+        WRITE_SYSREG( (sel & 0xF0), PRSELR_EL1);
+        isb();
+        switch ( sel & 0xF ) {
+            GENERATE_RESTORE_EL1_PR_REG_CASE(15, (&table[sel]), sel);
+            fallthrough;
+            GENERATE_RESTORE_EL1_PR_REG_CASE(14, (&table[sel]), sel);
+            fallthrough;
+            GENERATE_RESTORE_EL1_PR_REG_CASE(13, (&table[sel]), sel);
+            fallthrough;
+            GENERATE_RESTORE_EL1_PR_REG_CASE(12, (&table[sel]), sel);
+            fallthrough;
+            GENERATE_RESTORE_EL1_PR_REG_CASE(11, (&table[sel]), sel);
+            fallthrough;
+            GENERATE_RESTORE_EL1_PR_REG_CASE(10, (&table[sel]), sel);
+            fallthrough;
+            GENERATE_RESTORE_EL1_PR_REG_CASE(9, (&table[sel]), sel);
+            fallthrough;
+            GENERATE_RESTORE_EL1_PR_REG_CASE(8, (&table[sel]), sel);
+            fallthrough;
+            GENERATE_RESTORE_EL1_PR_REG_CASE(7, (&table[sel]), sel);
+            fallthrough;
+            GENERATE_RESTORE_EL1_PR_REG_CASE(6, (&table[sel]), sel);
+            fallthrough;
+            GENERATE_RESTORE_EL1_PR_REG_CASE(5, (&table[sel]), sel);
+            fallthrough;
+            GENERATE_RESTORE_EL1_PR_REG_CASE(4, (&table[sel]), sel);
+            fallthrough;
+            GENERATE_RESTORE_EL1_PR_REG_CASE(3, (&table[sel]), sel);
+            fallthrough;
+            GENERATE_RESTORE_EL1_PR_REG_CASE(2, (&table[sel]), sel);
+            fallthrough;
+            GENERATE_RESTORE_EL1_PR_REG_CASE(1, (&table[sel]), sel);
+            fallthrough;
+            GENERATE_RESTORE_EL1_PR_REG_CASE(0, (&table[sel]), sel);
+            break;
+            default:
+                BUG(); /* Can't happen */
+                break;
+        }
+    }
+
+    context_sync_mpu();
+}
+
  /*
   * Local variables:
   * mode: C
xen/arch/arm/mpu/mm.c +5 -0
index 459fb952d125..d6703bf0c005 100644
@@ -7,6 +7,7 @@
  #include <xen/mm.h>
  #include <xen/pfn.h>
  #include <xen/sizes.h>
+#include <xen/sched.h>
  #include <xen/spinlock.h>
  #include <xen/types.h>
  #include <asm/mpu.h>
@@ -14,6 +15,7 @@
  #include <asm/page.h>
  #include <asm/setup.h>
  #include <asm/sysregs.h>
+#include <public/arch-arm.h>
 
  #define MPU_ATTR_XN_RO_MISMATCH     -1
  #define MPU_ATTR_AI_MISMATCH        -2
@@ -23,6 +25,9 @@ struct page_info *frame_table;
  /* Maximum number of supported MPU memory regions by the EL2 MPU. */
  uint8_t __ro_after_init max_mpu_regions;
 
+/* Maximum number of supported MPU memory regions by the EL1 MPU. */
+uint8_t __read_mostly max_mpu_regions_el1;
+
  /*
   * Bitmap xen_mpumap_mask is to record the usage of EL2 MPU memory regions.
   * Bit 0 represents MPU memory region 0, bit 1 represents MPU memory

xen/arch/arm/mpu/mm.c void __iomem *ioremap_attr(paddr_t start, size_t len, unsigned int flags) · line 639

Quoted 4 lines
xen/arch/arm/mpu/mm.c +1 -0
@@ -631,6 +636,40 @@ void __iomem *ioremap_attr(paddr_t start, size_t len, unsigned int flags)
      return maddr_to_virt(start);
  }
 
+bool is_mpu_domain(struct domain *d)

const struct domain *

Quoted 15 lines
xen/arch/arm/mpu/mm.c +15 -0
+{
+    return (d->arch.v8r_el1_msa == XEN_DOMCTL_CONFIG_ARM_V8R_EL1_MSA_NONE) ||
+           (d->arch.v8r_el1_msa == XEN_DOMCTL_CONFIG_ARM_V8R_EL1_MSA_PMSA);
+}
+
+void load_nr_mpu_regions_el1(void)
+{
+    max_mpu_regions_el1 = (uint8_t)(READ_SYSREG(MPUIR_EL1) &
+                                    NUM_MPU_REGIONS_MASK);
+}
+
+void allocate_el1_mpu_regions(struct vcpu *v)
+{
+    if ( max_mpu_regions_el1 == 0 )
+        return;

xen/arch/arm/mpu/mm.c void __iomem *ioremap_attr(paddr_t start, size_t len, unsigned int flags) · line 664

Quoted 10 lines
xen/arch/arm/mpu/mm.c +10 -0
+
+    if ( v->arch.mpu_regions )
+        return;
+
+    v->arch.mpu_regions = (pr_t *)_xzalloc(sizeof(pr_t) * max_mpu_regions_el1,
+                                           SMP_CACHE_BYTES);
+    if ( !v->arch.mpu_regions )
+        panic("DOM%pd: mpu: no memory to store EL1 MPU memory region info\n",
+              v->domain);
+    memset(v->arch.mpu_regions, 0, sizeof(pr_t) * max_mpu_regions_el1);

_xzalloc already zeroes the allocation, so the memset is redundant. The

(pr_t *) cast on the void * return is also unnecessary.

The panic format is
"DOM%pd" but %pd already expands to "dX"/"dom0" - drop the DOM prefix.

xen/arch/arm/mpu/mm.c void __iomem *ioremap_attr(paddr_t start, size_t len, unsigned int flags) · line 675

Quoted 11 lines
xen/arch/arm/mpu/mm.c +8 -0
+}
+
+void free_el1_mpu_regions(struct vcpu *v)
+{
+    if ( v->arch.mpu_regions )
+        xfree(v->arch.mpu_regions);
+}
+
  /*
   * Local variables:
   * mode: C

- Ayan

#16 lore raw

PATCH 8/8 arm/mpu: Save/restore VTCR_EL2 on vCPU context switch

In reply to Luca Fancellu

On AArch64 MPU systems, the VTCR register contains the MSA bit that determines if the guest is using MPU or MMU at EL1, which is required to be saved/restored on vCPU context switch.

On AArch64 MPU systems the VTCR_EL2 register configuration will be saved during setup_virt_paging and will be placed in a new member of 'struct arch_vcpu' named vtcr_el2, to be retrieved by context switch.

AArch32 MPU systems and MMU systems don't need to save/restore this register because they don't have any MSA bit and related functionality, so for these architecture a stub is provided.

---
 xen/arch/arm/arm64/mpu/p2m.c                | 15 +++++++++++++++
 xen/arch/arm/domain.c                       |  2 ++
 xen/arch/arm/include/asm/domain.h           |  5 +++++
 xen/arch/arm/include/asm/mmu/domain-build.h |  2 ++
 xen/arch/arm/include/asm/mpu/domain-build.h |  6 ++++++
 xen/arch/arm/include/asm/mpu/p2m.h          | 10 ++++++++++
 xen/arch/arm/mpu/domain-build.c             | 17 +++++++++++++++++
 xen/arch/arm/mpu/p2m.c                      |  4 ++++
 8 files changed, 61 insertions(+)
xen/arch/arm/arm64/mpu/p2m.c +15 -0
index a1ec9fcd6195..350cbd7ae94c 100644
@@ -3,8 +3,12 @@
 #include <xen/bug.h>
 #include <xen/init.h>
 #include <xen/lib.h>
+#include <xen/sched.h>
 #include <asm/p2m.h>
 
+/* VTCR_EL2 value to be configured for the boot CPU. */
+register_t __read_mostly vtcr;
+
 void __init setup_virt_paging(void)
 {
     register_t vtcr_el2 = READ_SYSREG(VTCR_EL2);
@@ -58,6 +62,7 @@ void __init setup_virt_paging(void)
     else
         vtcr_el2 &= ~VTCR_VS;
 
+    vtcr = vtcr_el2;
     WRITE_SYSREG(vtcr_el2, VTCR_EL2);
 
     p2m_vmid_allocator_init();
@@ -89,6 +94,16 @@ void __init setup_virt_paging(void)
     panic("Hardware with no PMSAv8-64 support in any translation regime\n");
 }
 
+void p2m_save_vtcr(struct vcpu *p)
+{
+    p->arch.vtcr_el2 = READ_SYSREG(VTCR_EL2);
+}
+
+void p2m_restore_vtcr(struct vcpu *n)
+{
+    WRITE_SYSREG(n->arch.vtcr_el2, VTCR_EL2);
+}
+
 /*
  * Local variables:
  * mode: C
xen/arch/arm/domain.c +2 -0
index 221b1f8a7f9d..e928301d7cb0 100644
@@ -538,6 +538,8 @@ int arch_vcpu_create(struct vcpu *v)
     if ( get_ssbd_state() == ARM_SSBD_RUNTIME )
         v->arch.cpu_info->flags |= CPUINFO_WORKAROUND_2_FLAG;
 
+    arch_set_vtcr_msa(v);
+
     if ( is_mpu_domain(v->domain) )
         allocate_el1_mpu_regions(v);
 
xen/arch/arm/include/asm/domain.h +5 -0
index 44c304ef8aa9..3ab004518427 100644
@@ -203,6 +203,11 @@ struct arch_vcpu
     uint32_t cpacr;
 
 #ifdef CONFIG_MPU
+#ifdef CONFIG_ARM_64
+    /* Virtualization Translation Control Register */
+    uint64_t vtcr_el2;
+#endif
+
     /* EL1 MPU memory regions */
     pr_t *mpu_regions;
 #endif
xen/arch/arm/include/asm/mmu/domain-build.h +2 -0
index 3e0d9a6a2a08..0919c0097da9 100644
@@ -34,6 +34,8 @@ int arch_set_v8r_el1_msa(struct domain *d,
     return 0;
 }
 
+static inline void arch_set_vtcr_msa(struct vcpu *v) {}
+
 #endif /* __ARM_MMU_DOMAIN_BUILD_H__ */
 
 /*
xen/arch/arm/include/asm/mpu/domain-build.h +6 -0
index 463cd85b5b7e..8d3a743219ce 100644
@@ -15,6 +15,12 @@ int arch_set_v8r_el1_msa(struct domain *d,
                          const struct xen_domctl_createdomain *config,
                          unsigned int flags);
 
+#ifdef CONFIG_ARM_64
+void arch_set_vtcr_msa(struct vcpu *v);
+#else
+static inline void arch_set_vtcr_msa(struct vcpu *v) {}
+#endif
+
 #endif /* __ARM_MPU_DOMAIN_BUILD_H__ */
 
 /*
xen/arch/arm/include/asm/mpu/p2m.h +10 -0
index d0ec8a77a15a..1628148ba578 100644
@@ -9,6 +9,8 @@
 
 struct p2m_domain;
 
+extern register_t vtcr;
+
 /*
  * 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)
@@ -36,6 +38,14 @@ static inline bool region_is_p2m_valid(pr_t *pr)
     return (pr->p2m_type != p2m_invalid);
 }
 
+#ifdef CONFIG_ARM_64
+void p2m_save_vtcr(struct vcpu *p);
+void p2m_restore_vtcr(struct vcpu *n);
+#else
+static inline void p2m_save_vtcr(struct vcpu *p) {}
+static inline void p2m_restore_vtcr(struct vcpu *n) {}
+#endif
+
 #endif /* __ARM_MPU_P2M_H__ */
 
 /*
xen/arch/arm/mpu/domain-build.c +17 -0
index 1bdd0ffedebb..19e504f7149e 100644
@@ -8,6 +8,7 @@
 #include <xen/sched.h>
 #include <asm/mpu.h>
 #include <asm/mpu/domain-build.h>
+#include <asm/mpu/p2m.h>
 #include <public/arch-arm.h>
 #include <public/domctl.h>
 
@@ -66,6 +67,22 @@ int arch_set_v8r_el1_msa(struct domain *d,
     return 0;
 }
 
+#ifdef CONFIG_ARM_64
+void arch_set_vtcr_msa(struct vcpu *v)
+{
+    /*
+     * When ID_AA64MMFR0_EL1.MSA_frac is 0b0010 (MM64_MSA_FRAC_VMSA_SUPPORT),
+     * then VTCR_EL2.MSA determines the memory system architecture enabled
+     * at stage 1 of the Secure EL1&0 translation regime.
+     */
+    v->arch.vtcr_el2 = vtcr;
+    if ( is_mpu_domain(v->domain) )
+        v->arch.vtcr_el2 &= ~VTCR_MSA;
+    else
+        v->arch.vtcr_el2 |= VTCR_MSA;
+}
+#endif
+
 /*
  * Local variables:
  * mode: C
xen/arch/arm/mpu/p2m.c +4 -0
index 06c92a3ef41b..b41abd8f2d37 100644
@@ -357,6 +357,8 @@ void p2m_save_state(struct vcpu *p)
 
     p->arch.sctlr = READ_SYSREG(SCTLR_EL1);
 
+    p2m_save_vtcr(p);
+
     if ( p2m_xenmpu_update(p2m, false) )
         panic("Failed to offline P2M MPU memory mapping\n");
 }
@@ -369,6 +371,8 @@ void p2m_restore_state(struct vcpu *n)
     WRITE_SYSREG(n->arch.sctlr, SCTLR_EL1);
     WRITE_SYSREG(n->arch.hcr_el2, HCR_EL2);
 
+    p2m_restore_vtcr(n);
+
     WRITE_SYSREG(p2m->vsctlr, VSCTLR_EL2);
     if ( p2m_xenmpu_update(p2m, true) )
         panic("Failed to online P2M MPU memory mapping\n");
Signature · 1 line
2.34.1
#17 lore raw

Re: [PATCH 8/8] arm/mpu: Save/restore VTCR_EL2 on vCPU context switch

Hi Luca,

Quoted 50 lines · Luca Fancellu

On AArch64 MPU systems, the VTCR register contains the MSA bit that determines if the guest is using MPU or MMU at EL1, which is required to be saved/restored on vCPU context switch.

On AArch64 MPU systems the VTCR_EL2 register configuration will be saved during setup_virt_paging and will be placed in a new member of 'struct arch_vcpu' named vtcr_el2, to be retrieved by context switch.

AArch32 MPU systems and MMU systems don't need to save/restore this register because they don't have any MSA bit and related functionality, so for these architecture a stub is provided.

---
  xen/arch/arm/arm64/mpu/p2m.c                | 15 +++++++++++++++
  xen/arch/arm/domain.c                       |  2 ++
  xen/arch/arm/include/asm/domain.h           |  5 +++++
  xen/arch/arm/include/asm/mmu/domain-build.h |  2 ++
  xen/arch/arm/include/asm/mpu/domain-build.h |  6 ++++++
  xen/arch/arm/include/asm/mpu/p2m.h          | 10 ++++++++++
  xen/arch/arm/mpu/domain-build.c             | 17 +++++++++++++++++
  xen/arch/arm/mpu/p2m.c                      |  4 ++++
  8 files changed, 61 insertions(+)
xen/arch/arm/arm64/mpu/p2m.c +5 -0
index a1ec9fcd6195..350cbd7ae94c 100644
@@ -3,8 +3,12 @@
  #include <xen/bug.h>
  #include <xen/init.h>
  #include <xen/lib.h>
+#include <xen/sched.h>
  #include <asm/p2m.h>
 
+/* VTCR_EL2 value to be configured for the boot CPU. */
+register_t __read_mostly vtcr;
+
  void __init setup_virt_paging(void)
  {
      register_t vtcr_el2 = READ_SYSREG(VTCR_EL2);
@@ -58,6 +62,7 @@ void __init setup_virt_paging(void)
      else
          vtcr_el2 &= ~VTCR_VS;
 
+    vtcr = vtcr_el2;
      WRITE_SYSREG(vtcr_el2, VTCR_EL2);
 
      p2m_vmid_allocator_init();

xen/arch/arm/arm64/mpu/p2m.c void __init setup_virt_paging(void) · line 101

Quoted 8 lines
xen/arch/arm/arm64/mpu/p2m.c +5 -0
@@ -89,6 +94,16 @@ void __init setup_virt_paging(void)
      panic("Hardware with no PMSAv8-64 support in any translation regime\n");
  }
 
+void p2m_save_vtcr(struct vcpu *p)
+{
+    p->arch.vtcr_el2 = READ_SYSREG(VTCR_EL2);
+}
+

VTCR_EL2 is an EL2 register the guest cannot touch, and set_v8r_vtcr_msa() already fixes each vCPU's value at creation. Reading it back on every save is redundant

Quoted 144 lines
xen/arch/arm/arm64/mpu/p2m.c +5 -0
+void p2m_restore_vtcr(struct vcpu *n)
+{
+    WRITE_SYSREG(n->arch.vtcr_el2, VTCR_EL2);
+}
+
  /*
   * Local variables:
   * mode: C
xen/arch/arm/domain.c +2 -0
index 221b1f8a7f9d..e928301d7cb0 100644
@@ -538,6 +538,8 @@ int arch_vcpu_create(struct vcpu *v)
      if ( get_ssbd_state() == ARM_SSBD_RUNTIME )
          v->arch.cpu_info->flags |= CPUINFO_WORKAROUND_2_FLAG;
 
+    arch_set_vtcr_msa(v);
+
      if ( is_mpu_domain(v->domain) )
          allocate_el1_mpu_regions(v);
 
xen/arch/arm/include/asm/domain.h +5 -0
index 44c304ef8aa9..3ab004518427 100644
@@ -203,6 +203,11 @@ struct arch_vcpu
      uint32_t cpacr;
 
  #ifdef CONFIG_MPU
+#ifdef CONFIG_ARM_64
+    /* Virtualization Translation Control Register */
+    uint64_t vtcr_el2;
+#endif
+
      /* EL1 MPU memory regions */
      pr_t *mpu_regions;
  #endif
xen/arch/arm/include/asm/mmu/domain-build.h +2 -0
index 3e0d9a6a2a08..0919c0097da9 100644
@@ -34,6 +34,8 @@ int arch_set_v8r_el1_msa(struct domain *d,
      return 0;
  }
 
+static inline void arch_set_vtcr_msa(struct vcpu *v) {}
+
  #endif /* __ARM_MMU_DOMAIN_BUILD_H__ */
 
  /*
xen/arch/arm/include/asm/mpu/domain-build.h +6 -0
index 463cd85b5b7e..8d3a743219ce 100644
@@ -15,6 +15,12 @@ int arch_set_v8r_el1_msa(struct domain *d,
                           const struct xen_domctl_createdomain *config,
                           unsigned int flags);
 
+#ifdef CONFIG_ARM_64
+void arch_set_vtcr_msa(struct vcpu *v);
+#else
+static inline void arch_set_vtcr_msa(struct vcpu *v) {}
+#endif
+
  #endif /* __ARM_MPU_DOMAIN_BUILD_H__ */
 
  /*
xen/arch/arm/include/asm/mpu/p2m.h +10 -0
index d0ec8a77a15a..1628148ba578 100644
@@ -9,6 +9,8 @@
 
  struct p2m_domain;
 
+extern register_t vtcr;
+
  /*
   * 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)
@@ -36,6 +38,14 @@ static inline bool region_is_p2m_valid(pr_t *pr)
      return (pr->p2m_type != p2m_invalid);
  }
 
+#ifdef CONFIG_ARM_64
+void p2m_save_vtcr(struct vcpu *p);
+void p2m_restore_vtcr(struct vcpu *n);
+#else
+static inline void p2m_save_vtcr(struct vcpu *p) {}
+static inline void p2m_restore_vtcr(struct vcpu *n) {}
+#endif
+
  #endif /* __ARM_MPU_P2M_H__ */
 
  /*
xen/arch/arm/mpu/domain-build.c +17 -0
index 1bdd0ffedebb..19e504f7149e 100644
@@ -8,6 +8,7 @@
  #include <xen/sched.h>
  #include <asm/mpu.h>
  #include <asm/mpu/domain-build.h>
+#include <asm/mpu/p2m.h>
  #include <public/arch-arm.h>
  #include <public/domctl.h>
 
@@ -66,6 +67,22 @@ int arch_set_v8r_el1_msa(struct domain *d,
      return 0;
  }
 
+#ifdef CONFIG_ARM_64
+void arch_set_vtcr_msa(struct vcpu *v)
+{
+    /*
+     * When ID_AA64MMFR0_EL1.MSA_frac is 0b0010 (MM64_MSA_FRAC_VMSA_SUPPORT),
+     * then VTCR_EL2.MSA determines the memory system architecture enabled
+     * at stage 1 of the Secure EL1&0 translation regime.
+     */
+    v->arch.vtcr_el2 = vtcr;
+    if ( is_mpu_domain(v->domain) )
+        v->arch.vtcr_el2 &= ~VTCR_MSA;
+    else
+        v->arch.vtcr_el2 |= VTCR_MSA;
+}
+#endif
+
  /*
   * Local variables:
   * mode: C
xen/arch/arm/mpu/p2m.c +2 -0
index 06c92a3ef41b..b41abd8f2d37 100644
@@ -357,6 +357,8 @@ void p2m_save_state(struct vcpu *p)
 
      p->arch.sctlr = READ_SYSREG(SCTLR_EL1);
 
+    p2m_save_vtcr(p);
+
      if ( p2m_xenmpu_update(p2m, false) )
          panic("Failed to offline P2M MPU memory mapping\n");
  }

xen/arch/arm/mpu/p2m.c void p2m_restore_state(struct vcpu *n) · line 378

Quoted 8 lines
xen/arch/arm/mpu/p2m.c +2 -0
@@ -369,6 +371,8 @@ void p2m_restore_state(struct vcpu *n)
      WRITE_SYSREG(n->arch.sctlr, SCTLR_EL1);
      WRITE_SYSREG(n->arch.hcr_el2, HCR_EL2);
 
+    p2m_restore_vtcr(n);
+
      WRITE_SYSREG(p2m->vsctlr, VSCTLR_EL2);
      if ( p2m_xenmpu_update(p2m, true) )
          panic("Failed to online P2M MPU memory mapping\n");

- Ayan

#18 lore raw

Re: [PATCH 8/8] arm/mpu: Save/restore VTCR_EL2 on vCPU context switch

Hi,

Quoted 25 lines · Ayan Kumar
Quoted 25 lines
xen/arch/arm/arm64/mpu/p2m.c +4 -0
index a1ec9fcd6195..350cbd7ae94c 100644
@@ -3,8 +3,12 @@
   #include <xen/bug.h>
   #include <xen/init.h>
   #include <xen/lib.h>
+#include <xen/sched.h>
   #include <asm/p2m.h>
+/* VTCR_EL2 value to be configured for the boot CPU. */
+register_t __read_mostly vtcr;
+
   void __init setup_virt_paging(void)
   {
       register_t vtcr_el2 = READ_SYSREG(VTCR_EL2);
@@ -58,6 +62,7 @@ void __init setup_virt_paging(void)

      else
          vtcr_el2 &= ~VTCR_VS;
+    vtcr = vtcr_el2;
      WRITE_SYSREG(vtcr_el2, VTCR_EL2);
      p2m_vmid_allocator_init();

xen/arch/arm/arm64/mpu/p2m.c +0 -0
@@ -89,6 +94,16 @@ void __init setup_virt_paging(void)
       panic("Hardware with no PMSAv8-64 support in any translation regime\n");
   }
Quoted 8 lines

xen/arch/arm/arm64/mpu/p2m.c void __init setup_virt_paging(void) · line 101

Quoted 5 lines
xen/arch/arm/arm64/mpu/p2m.c +5 -0
+void p2m_save_vtcr(struct vcpu *p)
+{
+    p->arch.vtcr_el2 = READ_SYSREG(VTCR_EL2);
+}
+

VTCR_EL2 is an EL2 register the guest cannot touch, and set_v8r_vtcr_msa() already fixes each vCPU's value at creation. Reading it back on every save is redundant

I would at least consider adding an ASSERT() to confirm the two matches. This will catch any issues during debug build.

Cheers,

Signature · 1 line
Julien Grall
#19 lore raw

Re: [PATCH 8/8] arm/mpu: Save/restore VTCR_EL2 on vCPU context switch

In reply to Luca Fancellu

Hi Luca,

Quoted 29 lines · Luca Fancellu

On AArch64 MPU systems, the VTCR register contains the MSA bit that determines if the guest is using MPU or MMU at EL1, which is required to be saved/restored on vCPU context switch.

On AArch64 MPU systems the VTCR_EL2 register configuration will be saved during setup_virt_paging and will be placed in a new member of 'struct arch_vcpu' named vtcr_el2, to be retrieved by context switch.

AArch32 MPU systems and MMU systems don't need to save/restore this register because they don't have any MSA bit and related functionality, so for these architecture a stub is provided.

---
  xen/arch/arm/arm64/mpu/p2m.c                | 15 +++++++++++++++
  xen/arch/arm/domain.c                       |  2 ++
  xen/arch/arm/include/asm/domain.h           |  5 +++++
  xen/arch/arm/include/asm/mmu/domain-build.h |  2 ++
  xen/arch/arm/include/asm/mpu/domain-build.h |  6 ++++++
  xen/arch/arm/include/asm/mpu/p2m.h          | 10 ++++++++++
  xen/arch/arm/mpu/domain-build.c             | 17 +++++++++++++++++
  xen/arch/arm/mpu/p2m.c                      |  4 ++++
  8 files changed, 61 insertions(+)
xen/arch/arm/arm64/mpu/p2m.c +0 -0
index a1ec9fcd6195..350cbd7ae94c 100644

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

Quoted 8 lines
xen/arch/arm/arm64/mpu/p2m.c +3 -0
@@ -3,8 +3,12 @@
  #include <xen/bug.h>
  #include <xen/init.h>
  #include <xen/lib.h>
+#include <xen/sched.h>
  #include <asm/p2m.h>
 
+/* VTCR_EL2 value to be configured for the boot CPU. */
+register_t __read_mostly vtcr;

You don't seem to use it. Is it necessary? But the name of the global variable is a bit too generic. If we want one, it ought to be "boot_vtcr". Also doesn't this need to be writeable after boot? If not, it should be called "boot_vtcr".

Cheers,

Signature · 1 line
Julien Grall
#20 lore raw
In reply to Luca Fancellu

Hello everyone,

Quoted 6 lines · Luca Fancellu

Hi,

this serie complete the support for Armv8-R AArch64 MPU support for dom0less DomU boot on single core, it also build for Armv8-R AArch32 but the support is not complete.

Quoted 8 lines

The serie is based on another serie in the mailing list: "Fifth MPU Series".

I've spoken with Oleksii and the Arm maintainer to have the possibilty to have this on the Xen release, of course provided that maintainers have enough bandwidth and depending on the priority of the series to be in.

Asking for Oleksii Ack for the release, the changes in this serie are mostly related to MPU apart from few patches that touches common Arm code.

Despite the fact that this series was submitted just one day after the deadline, it appears to provide useful functionality. If maintainers have time to review it, it would be great to include it in this release.

Thanks.

~ Oleksii