Skip to contentSkip to conversation

xen/arm: Workaround for Cortex-A76 erratum 1165522

PATCH In review Julien Grall 3 participants 10 messages Jan 28, 2019 – Jan 29, 2019

lore
Share & tools
lore
#0 lore raw

v2 cover xen/arm: Workaround for Cortex-A76 erratum 1165522

Hi all,

Early version of Cortex-A76 can end-up with corrupt TLBs if they speculate an AT instruction while the S1/S2 system registers are in an inconsistent state.

This can happen during guest context switch and when invalidating the TLBs for other than the current VMID.

The workaround implemented in Xen will:
- Use an empty stage-2 with a reserved VMID while context switching between 2 guests
- Use an empty stage-2 with the VMID where TLBs need to be flushed

Cheers,

Julien Grall (7):
xen/arm: Only set necessary flags when initializing HCR_EL2 xen/arm: p2m: Provide an helper to generate the VTTBR xen/arm: p2m: Introduce an helper to allocate the root page-table xen/arm: domain_build: Don't switch to the guest P2M when copying data xen/arm: p2m: Only use isb() when it is necessary
xen/arm: Implement workaround for Cortex-A76 erratum 1165522 DO NOT APPLY Allow testing the new AT speculate workaround code

 docs/misc/arm/silicon-errata.txt |   1 +
 xen/arch/arm/cpuerrata.c         |  16 ++++++
 xen/arch/arm/domain.c            |   8 ++-
 xen/arch/arm/domain_build.c      |  13 -----
 xen/arch/arm/p2m.c               | 118 +++++++++++++++++++++++++++++++++++----
 xen/arch/arm/traps.c             |   8 ++-
 xen/include/asm-arm/cpufeature.h |   3 +-
 xen/include/asm-arm/processor.h  |   2 +
 8 files changed, 139 insertions(+), 30 deletions(-)
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#1 lore raw

PATCH v2 1/7 xen/arm: Only set necessary flags when initializing HCR_EL2

Only {A,F,I}MO are necessary to receive interrupts until a guest vCPU is loaded.

The rest have no effect on Xen and it is better to avoid setting them.

---
Changes in v2:
- Fix typo
- Add Andrii's and Stefano's reviewed-by

---
 xen/arch/arm/traps.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
xen/arch/arm/traps.c +6 -2
index 221c762ada..64a78d83a5 100644
@@ -157,8 +157,12 @@ void init_traps(void)
     WRITE_SYSREG((HCPTR_CP_MASK & ~(HCPTR_CP(10) | HCPTR_CP(11))) | HCPTR_TTA,
                  CPTR_EL2);
 
-    /* Setup hypervisor traps */
-    WRITE_SYSREG(get_default_hcr_flags(), HCR_EL2);
+    /*
+     * Configure HCR_EL2 with the bare minimum to run Xen until a guest
+     * is scheduled. {A,I,F}MO bits are set to allow EL2 receiving
+     * interrupts.
+     */
+    WRITE_SYSREG(HCR_AMO | HCR_FMO | HCR_IMO, HCR_EL2);
     isb();
 }
 
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#2 lore raw

PATCH v2 2/7 xen/arm: p2m: Provide an helper to generate the VTTBR

In reply to Julien Grall

A follow-up patch will need to generate the VTTBR in a few places.

---
Changes in v2:
- Add Andrii's and Stefano's reviewed-by

---
 xen/arch/arm/p2m.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
xen/arch/arm/p2m.c +6 -1
index 059a39157a..38bfa9964f 100644
@@ -41,6 +41,11 @@ static const paddr_t level_masks[] =
 static const uint8_t level_orders[] =
     { ZEROETH_ORDER, FIRST_ORDER, SECOND_ORDER, THIRD_ORDER };
 
+static uint64_t generate_vttbr(uint16_t vmid, mfn_t root_mfn)
+{
+    return (mfn_to_maddr(root_mfn) | ((uint64_t)vmid << 48));
+}
+
 /* Unlock the flush and do a P2M TLB flush if necessary */
 void p2m_write_unlock(struct p2m_domain *p2m)
 {
@@ -1364,7 +1369,7 @@ static int p2m_alloc_table(struct domain *d)
 
     p2m->root = page;
 
-    p2m->vttbr = page_to_maddr(p2m->root) | ((uint64_t)p2m->vmid << 48);
+    p2m->vttbr = generate_vttbr(p2m->vmid, page_to_mfn(p2m->root));
 
     /*
      * Make sure that all TLBs corresponding to the new VMID are flushed
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#3 lore raw

PATCH v2 3/7 xen/arm: p2m: Introduce an helper to allocate the root page-table

In reply to Julien Grall

A follow-up patch will require to allocate the root page-table without having a domain in hand.

---
Changes in v2:
- Add Andrii's and Stefano's reviewed-by

---
 xen/arch/arm/p2m.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
xen/arch/arm/p2m.c +12 -4
index 38bfa9964f..9844bfb936 100644
@@ -1353,21 +1353,29 @@ int guest_physmap_remove_page(struct domain *d, gfn_t gfn, mfn_t mfn,
     return p2m_remove_mapping(d, gfn, (1 << page_order), mfn);
 }
 
-static int p2m_alloc_table(struct domain *d)
+static struct page_info *p2m_allocate_root(void)
 {
-    struct p2m_domain *p2m = p2m_get_hostp2m(d);
     struct page_info *page;
     unsigned int i;
 
     page = alloc_domheap_pages(NULL, P2M_ROOT_ORDER, 0);
     if ( page == NULL )
-        return -ENOMEM;
+        return NULL;
 
     /* Clear both first level pages */
     for ( i = 0; i < P2M_ROOT_PAGES; i++ )
         clear_and_clean_page(page + i);
 
-    p2m->root = page;
+    return page;
+}
+
+static int p2m_alloc_table(struct domain *d)
+{
+    struct p2m_domain *p2m = p2m_get_hostp2m(d);
+
+    p2m->root = p2m_allocate_root();
+    if ( !p2m->root )
+        return -ENOMEM;
 
     p2m->vttbr = generate_vttbr(p2m->vmid, page_to_mfn(p2m->root));
 
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#4 lore raw

PATCH v2 4/7 xen/arm: domain_build: Don't switch to the guest P2M when copying data

In reply to Julien Grall

Until recently, kernel/initrd/dtb were loaded using guest VA and therefore requiring to restore temporarily the P2M. This was reworked in a series of commits (up to 9292086 "xen/arm: domain_build: Use copy_to_guest_phys_flush_dcache in dtb_load") to use a guest PA.

This will also help a follow-up patch which will require p2m_{save,restore}_state to work in pair to workaround an erratum.

---
Changes in v2:
- Fix typo in commit message
- Add Stefano's reviewed-by

---
 xen/arch/arm/domain_build.c | 13 -------------
 1 file changed, 13 deletions(-)
xen/arch/arm/domain_build.c +0 -13
index d2c63a89ca..31af989e63 100644
@@ -1923,7 +1923,6 @@ static void __init find_gnttab_region(struct domain *d,
 
 static int __init construct_domain(struct domain *d, struct kernel_info *kinfo)
 {
-    struct vcpu *saved_current;
     int i, cpu;
     struct vcpu *v = d->vcpu[0];
     struct cpu_user_regs *regs = &v->arch.cpu_info->guest_cpu_user_regs;
@@ -1945,14 +1944,6 @@ static int __init construct_domain(struct domain *d, struct kernel_info *kinfo)
 #endif
 
     /*
-     * The following loads use the domain's p2m and require current to
-     * be a vcpu of the domain, temporarily switch
-     */
-    saved_current = current;
-    p2m_restore_state(v);
-    set_current(v);
-
-    /*
      * kernel_load will determine the placement of the kernel as well
      * as the initrd & fdt in RAM, so call it first.
      */
@@ -1961,10 +1952,6 @@ static int __init construct_domain(struct domain *d, struct kernel_info *kinfo)
     initrd_load(kinfo);
     dtb_load(kinfo);
 
-    /* Now that we are done restore the original p2m and current. */
-    set_current(saved_current);
-    p2m_restore_state(saved_current);
-
     memset(regs, 0, sizeof(*regs));
 
     regs->pc = (register_t)kinfo->entry;
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#5 lore raw

PATCH v2 5/7 xen/arm: p2m: Only use isb() when it is necessary

In reply to Julien Grall

The EL1 translation regime is out-of-context when running at EL2. This means the processor cannot speculate memory accesses using the registers associated to that regime.

An isb() is only needed if Xen is going to use the translation regime before returning to the guest (exception returns will synchronize the context).

Remove unnecessary isb() and document the ones left.

---
Changes in v2:
- Remove pointless {}
- Fix typoes
- Add Andrii's reviewed-by

---
 xen/arch/arm/p2m.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
xen/arch/arm/p2m.c +11 -6
index 9844bfb936..44391a5f8c 100644
@@ -106,17 +106,21 @@ void p2m_restore_state(struct vcpu *n)
         return;
 
     WRITE_SYSREG64(p2m->vttbr, VTTBR_EL2);
-    isb();
-
     WRITE_SYSREG(n->arch.sctlr, SCTLR_EL1);
-    isb();
-
     WRITE_SYSREG(n->arch.hcr_el2, HCR_EL2);
-    isb();
 
     last_vcpu_ran = &p2m->last_vcpu_ran[smp_processor_id()];
 
     /*
+     * While we are restoring an out-of-context translation regime
+     * we still need to ensure:
+     *  - VTTBR_EL2 is synchronized before flushing the TLBs
+     *  - All registers for EL1 are synchronized before executing an AT
+     *  instructions targeting S1/S2.
+     */
+    isb();
+
+    /*
      * Flush local TLB for the domain to prevent wrong TLB translation
      * when running multiple vCPU of the same domain on a single pCPU.
      */
@@ -147,6 +151,7 @@ static void p2m_force_tlb_flush_sync(struct p2m_domain *p2m)
     {
         local_irq_save(flags);
         WRITE_SYSREG64(p2m->vttbr, VTTBR_EL2);
+        /* Ensure VTTBR_EL2 is synchronized before flushing the TLBs */
         isb();
     }
 
@@ -155,6 +160,7 @@ static void p2m_force_tlb_flush_sync(struct p2m_domain *p2m)
     if ( ovttbr != READ_SYSREG64(VTTBR_EL2) )
     {
         WRITE_SYSREG64(ovttbr, VTTBR_EL2);
+        /* Ensure VTTBR_EL2 is back in place before continuing. */
         isb();
         local_irq_restore(flags);
     }
@@ -1907,7 +1913,6 @@ static uint32_t __read_mostly vtcr;
 static void setup_virt_paging_one(void *data)
 {
     WRITE_SYSREG32(vtcr, VTCR_EL2);
-    isb();
 }
 
 void __init setup_virt_paging(void)
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#7 lore raw

PATCH v2 6/7 xen/arm: Implement workaround for Cortex-A76 erratum 1165522

In reply to Julien Grall

Early version of Cortex-A76 can end-up with corrupt TLBs if they speculate an AT instruction while the S1/S2 system registers are in an inconsistent state.

This can happen during guest context switch and when invalidating the TLBs for other than the current VMID.

The workaround implemented in Xen will:
- Use an empty stage-2 with a reserved VMID while context switching between 2 guests
- Use an empty stage-2 with the VMID where TLBs need to be flushed

---
Changes in v2:
- Fix typo
- Add Andrii's reviewed-by

---
 docs/misc/arm/silicon-errata.txt |  1 +
 xen/arch/arm/cpuerrata.c         |  6 ++++
 xen/arch/arm/domain.c            |  8 +++--
 xen/arch/arm/p2m.c               | 78 ++++++++++++++++++++++++++++++++++++++--
 xen/include/asm-arm/cpufeature.h |  3 +-
 xen/include/asm-arm/processor.h  |  2 ++
 6 files changed, 93 insertions(+), 5 deletions(-)
docs/misc/arm/silicon-errata.txt +1 -0
index 906bf5fd48..6cd1366f15 100644
@@ -48,4 +48,5 @@ stable hypervisors.
 | ARM            | Cortex-A57      | #852523         | N/A                     |
 | ARM            | Cortex-A57      | #832075         | ARM64_ERRATUM_832075    |
 | ARM            | Cortex-A57      | #834220         | ARM64_ERRATUM_834220    |
+| ARM            | Cortex-A76      | #1165522        | N/A                     |
 | ARM            | MMU-500         | #842869         | N/A                     |
xen/arch/arm/cpuerrata.c +6 -0
index f4815cafb4..4431b244fd 100644
@@ -489,6 +489,12 @@ static const struct arm_cpu_capabilities arm_errata[] = {
         .matches = has_ssbd_mitigation,
     },
 #endif
+    {
+        /* Cortex-A76 r0p0 - r2p0 */
+        .desc = "ARM erratum 116522",
+        .capability = ARM64_WORKAROUND_AT_SPECULATE,
+        MIDR_RANGE(MIDR_CORTEX_A76, 0, 2 << MIDR_VARIANT_SHIFT),
+    },
     {},
 };
 
xen/arch/arm/domain.c +6 -2
index 41f101746e..6dc633ed50 100644
@@ -181,8 +181,6 @@ static void ctxt_switch_to(struct vcpu *n)
     if ( is_idle_vcpu(n) )
         return;
 
-    p2m_restore_state(n);
-
     vpidr = READ_SYSREG32(MIDR_EL1);
     WRITE_SYSREG32(vpidr, VPIDR_EL2);
     WRITE_SYSREG(n->arch.vmpidr, VMPIDR_EL2);
@@ -235,6 +233,12 @@ static void ctxt_switch_to(struct vcpu *n)
 #endif
     isb();
 
+    /*
+     * ARM64_WORKAROUND_AT_SPECULATE: The P2M should be restored after
+     * the stage-1 MMU sysregs have been restored.
+     */
+    p2m_restore_state(n);
+
     /* Control Registers */
     WRITE_SYSREG(n->arch.cpacr, CPACR_EL1);
 
xen/arch/arm/p2m.c +76 -2

127 lines not on this page · raw message

xen/include/asm-arm/cpufeature.h +2 -1
index 17de928467..c2c8f3417c 100644
@@ -45,8 +45,9 @@
 #define ARM_HARDEN_BRANCH_PREDICTOR 7
 #define ARM_SSBD 8
 #define ARM_SMCCC_1_1 9
+#define ARM64_WORKAROUND_AT_SPECULATE 10
 
-#define ARM_NCAPS           10
+#define ARM_NCAPS           11
 
 #ifndef __ASSEMBLY__
 
xen/include/asm-arm/processor.h +2 -0
index cb781751a6..b5f515805d 100644
@@ -52,6 +52,7 @@
 #define ARM_CPU_PART_CORTEX_A72     0xD08
 #define ARM_CPU_PART_CORTEX_A73     0xD09
 #define ARM_CPU_PART_CORTEX_A75     0xD0A
+#define ARM_CPU_PART_CORTEX_A76     0xD0B
 
 #define MIDR_CORTEX_A12 MIDR_CPU_MODEL(ARM_CPU_IMP_ARM, ARM_CPU_PART_CORTEX_A12)
 #define MIDR_CORTEX_A17 MIDR_CPU_MODEL(ARM_CPU_IMP_ARM, ARM_CPU_PART_CORTEX_A17)
@@ -61,6 +62,7 @@
 #define MIDR_CORTEX_A72 MIDR_CPU_MODEL(ARM_CPU_IMP_ARM, ARM_CPU_PART_CORTEX_A72)
 #define MIDR_CORTEX_A73 MIDR_CPU_MODEL(ARM_CPU_IMP_ARM, ARM_CPU_PART_CORTEX_A73)
 #define MIDR_CORTEX_A75 MIDR_CPU_MODEL(ARM_CPU_IMP_ARM, ARM_CPU_PART_CORTEX_A75)
+#define MIDR_CORTEX_A76 MIDR_CPU_MODEL(ARM_CPU_IMP_ARM, ARM_CPU_PART_CORTEX_A76)
 
 /* MPIDR Multiprocessor Affinity Register */
 #define _MPIDR_UP           (30)
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#8 lore raw

PATCH v2 7/7 DO NOT APPLY Allow testing the new AT speculate workaround code

In reply to Julien Grall
---
 xen/arch/arm/cpuerrata.c | 10 ++++++++++
 xen/arch/arm/p2m.c       |  2 ++
 2 files changed, 12 insertions(+)
xen/arch/arm/cpuerrata.c +10 -0
index 4431b244fd..727c67451d 100644
@@ -381,6 +381,11 @@ static bool has_ssbd_mitigation(const struct arm_cpu_capabilities *entry)
 }
 #endif
 
+static bool has_at_speculate(const struct arm_cpu_capabilities *entry)
+{
+    return true;
+}
+
 #define MIDR_RANGE(model, min, max)     \
     .matches = is_affected_midr_range,  \
     .midr_model = model,                \
@@ -495,6 +500,11 @@ static const struct arm_cpu_capabilities arm_errata[] = {
         .capability = ARM64_WORKAROUND_AT_SPECULATE,
         MIDR_RANGE(MIDR_CORTEX_A76, 0, 2 << MIDR_VARIANT_SHIFT),
     },
+    {
+        .desc = "AT speculate",
+        .capability = ARM64_WORKAROUND_AT_SPECULATE,
+        .matches = has_at_speculate,
+    },
     {},
 };
 
xen/arch/arm/p2m.c +2 -0
index c38bd7e16e..2b25706823 100644
@@ -124,6 +124,8 @@ void p2m_restore_state(struct vcpu *n)
     if ( is_idle_vcpu(n) )
         return;
 
+    ASSERT(READ_SYSREG64(VTTBR_EL2) == (generate_vttbr(INVALID_VMID, empty_root_mfn)));
+
     WRITE_SYSREG(n->arch.sctlr, SCTLR_EL1);
     WRITE_SYSREG(n->arch.hcr_el2, HCR_EL2);
 
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#9 lore raw
In reply to Julien Grall
Quoted 28 lines · Julien Grall

Hi all,

Early version of Cortex-A76 can end-up with corrupt TLBs if they speculate an AT instruction while the S1/S2 system registers are in an inconsistent state.

This can happen during guest context switch and when invalidating the TLBs for other than the current VMID.

The workaround implemented in Xen will:
- Use an empty stage-2 with a reserved VMID while context switching between 2 guests
- Use an empty stage-2 with the VMID where TLBs need to be flushed

Cheers,

Julien Grall (7):
xen/arm: Only set necessary flags when initializing HCR_EL2 xen/arm: p2m: Provide an helper to generate the VTTBR xen/arm: p2m: Introduce an helper to allocate the root page-table xen/arm: domain_build: Don't switch to the guest P2M when copying data xen/arm: p2m: Only use isb() when it is necessary
xen/arm: Implement workaround for Cortex-A76 erratum 1165522 DO NOT APPLY Allow testing the new AT speculate workaround code

Quoted 10 lines
 docs/misc/arm/silicon-errata.txt |   1 +
 xen/arch/arm/cpuerrata.c         |  16 ++++++
 xen/arch/arm/domain.c            |   8 ++-
 xen/arch/arm/domain_build.c      |  13 -----
 xen/arch/arm/p2m.c               | 118 +++++++++++++++++++++++++++++++++++----
 xen/arch/arm/traps.c             |   8 ++-
 xen/include/asm-arm/cpufeature.h |   3 +-
 xen/include/asm-arm/processor.h  |   2 +
 8 files changed, 139 insertions(+), 30 deletions(-)

Juergen

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

Reviewed-by on v2 5/7
#6 lore raw

Re: [PATCH for-4.12 v2 5/7] xen/arm: p2m: Only use isb() when it is necessary

In reply to Julien Grall
Quoted 4 lines · Julien Grall

The EL1 translation regime is out-of-context when running at EL2. This means the processor cannot speculate memory accesses using the registers associated to that regime.

Quoted 8 lines

An isb() is only needed if Xen is going to use the translation regime before returning to the guest (exception returns will synchronize the context).

Remove unnecessary isb() and document the ones left.

Quoted 67 lines

---
Changes in v2:
- Remove pointless {}
- Fix typoes
- Add Andrii's reviewed-by

---
 xen/arch/arm/p2m.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
xen/arch/arm/p2m.c +11 -6
index 9844bfb936..44391a5f8c 100644
@@ -106,17 +106,21 @@ void p2m_restore_state(struct vcpu *n)
         return;
 
     WRITE_SYSREG64(p2m->vttbr, VTTBR_EL2);
-    isb();
-
     WRITE_SYSREG(n->arch.sctlr, SCTLR_EL1);
-    isb();
-
     WRITE_SYSREG(n->arch.hcr_el2, HCR_EL2);
-    isb();
 
     last_vcpu_ran = &p2m->last_vcpu_ran[smp_processor_id()];
 
     /*
+     * While we are restoring an out-of-context translation regime
+     * we still need to ensure:
+     *  - VTTBR_EL2 is synchronized before flushing the TLBs
+     *  - All registers for EL1 are synchronized before executing an AT
+     *  instructions targeting S1/S2.
+     */
+    isb();
+
+    /*
      * Flush local TLB for the domain to prevent wrong TLB translation
      * when running multiple vCPU of the same domain on a single pCPU.
      */
@@ -147,6 +151,7 @@ static void p2m_force_tlb_flush_sync(struct p2m_domain *p2m)
     {
         local_irq_save(flags);
         WRITE_SYSREG64(p2m->vttbr, VTTBR_EL2);
+        /* Ensure VTTBR_EL2 is synchronized before flushing the TLBs */
         isb();
     }
 
@@ -155,6 +160,7 @@ static void p2m_force_tlb_flush_sync(struct p2m_domain *p2m)
     if ( ovttbr != READ_SYSREG64(VTTBR_EL2) )
     {
         WRITE_SYSREG64(ovttbr, VTTBR_EL2);
+        /* Ensure VTTBR_EL2 is back in place before continuing. */
         isb();
         local_irq_restore(flags);
     }
@@ -1907,7 +1913,6 @@ static uint32_t __read_mostly vtcr;
 static void setup_virt_paging_one(void *data)
 {
     WRITE_SYSREG32(vtcr, VTCR_EL2);
-    isb();
 }
 
 void __init setup_virt_paging(void)
Signature · 1 line
2.11.0

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel