Skip to contentSkip to conversation

xen/arm: traps: enter the SP_EL0 vector group for an exception at EL1t

PATCH In review Stefano Stabellini 3 participants 8 messages Nov 18 – Nov 19 1 message not in the archive

lore
Share & tools
lore

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

#1 lore raw

PATCH v2 1/4 xen/arm: guest_walk: refuse a block descriptor where the granule has none

guest_walk_ld() accepted a block descriptor at any level. No granule has level 0 blocks, and the 16KB and 64KB granules have no level 1 blocks (ARM DDI 0487 M.d D8.3.1.2): the translation table walk makes such an entry a Translation fault (AArch64.BlockDescSupported()). Refuse it.

The check is only valid without 52-bit support (FEAT_LPA, FEAT_LPA2). Note in get_ipa_output_size() that 52-bit support requires updating it.

Fixes 7d623b358a45 ("arm/mem_access: Add long-descriptor based gpt")

---
Cc: Julien Grall <[email protected]>
Cc: Bertrand Marquis <[email protected]>
Cc: Michal Orzel <[email protected]>
Cc: Volodymyr Babchuk <[email protected]>

Changes in v2:
- Cite ARM DDI 0487 M.d instead of A.k.
- Say in the commit message and the comment that the check is only valid without 52-bit support (FEAT_LPA, FEAT_LPA2), and note in get_ipa_output_size() that 52-bit support requires updating it.
- Move the Cc list after ---.

---
 xen/arch/arm/guest_walk.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
xen/arch/arm/guest_walk.c +13 -1
index 09fe486598..151b837541 100644
@@ -201,7 +201,8 @@ static int get_ipa_output_size(struct domain *d, register_t tcr,
          * Return an error on reserved IPA output-sizes and if the IPA
          * output-size is 52bit.
          *
-         * XXX: 52 bit output-size is not supported yet.
+         * XXX: 52 bit output-size is not supported yet. Supporting it also
+         * requires updating the block descriptor check in guest_walk_ld().
          */
         if ( ips > TCR_EL1_IPS_48_BIT )
             return -EFAULT;
@@ -533,6 +534,17 @@ static bool guest_walk_ld(const struct vcpu *v,
     if ( !lpae_is_valid(pte) || !lpae_is_mapping(pte, level) )
         return false;
 
+    /*
+     * A block descriptor is only supported at level 1 and level 2 with the
+     * 4KB granule, and only at level 2 with the 16KB and 64KB granules (ARM
+     * DDI 0487 M.d D8.3.1.2). The block encoding at any other level generates
+     * a Translation fault (AArch64.BlockDescSupported()).
+     *
+     * This is only valid without 52-bit support (FEAT_LPA, FEAT_LPA2).
+     */
+    if ( level == 0 || (level == 1 && gran != GRANULE_SIZE_INDEX_4K) )
+        return false;
+
     /* Make sure that the lower bits of the PTE's base address are zero. */
     mask = GENMASK_ULL(47, grainsizes[gran]);
     *ipa = (pfn_to_paddr(pte.walk.base) & mask) |
Signature · 1 line
2.47.3
Reviewed-by on v2 1/4
#2 lore raw

Re: [PATCH v2 1/4] xen/arm: guest_walk: refuse a block descriptor where the granule has none

Hello Stefano,

Quoted 10 lines · Stefano Stabellini

guest_walk_ld() accepted a block descriptor at any level. No granule has level 0 blocks, and the 16KB and 64KB granules have no level 1 blocks (ARM DDI 0487 M.d D8.3.1.2): the translation table walk makes such an entry a Translation fault (AArch64.BlockDescSupported()). Refuse it.

The check is only valid without 52-bit support (FEAT_LPA, FEAT_LPA2). Note in get_ipa_output_size() that 52-bit support requires updating it.

Fixes 7d623b358a45 ("arm/mem_access: Add long-descriptor based gpt")
Quoted 48 lines

---
Cc: Julien Grall <[email protected]>
Cc: Bertrand Marquis <[email protected]>
Cc: Michal Orzel <[email protected]>
Cc: Volodymyr Babchuk <[email protected]>

Changes in v2:
- Cite ARM DDI 0487 M.d instead of A.k.
- Say in the commit message and the comment that the check is only valid without 52-bit support (FEAT_LPA, FEAT_LPA2), and note in get_ipa_output_size() that 52-bit support requires updating it.
- Move the Cc list after ---.

---
 xen/arch/arm/guest_walk.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
xen/arch/arm/guest_walk.c +13 -1
index 09fe486598..151b837541 100644
@@ -201,7 +201,8 @@ static int get_ipa_output_size(struct domain *d, register_t tcr,
          * Return an error on reserved IPA output-sizes and if the IPA
          * output-size is 52bit.
          *
-         * XXX: 52 bit output-size is not supported yet.
+         * XXX: 52 bit output-size is not supported yet. Supporting it also
+         * requires updating the block descriptor check in guest_walk_ld().
          */
         if ( ips > TCR_EL1_IPS_48_BIT )
             return -EFAULT;
@@ -533,6 +534,17 @@ static bool guest_walk_ld(const struct vcpu *v,
     if ( !lpae_is_valid(pte) || !lpae_is_mapping(pte, level) )
         return false;
 
+    /*
+     * A block descriptor is only supported at level 1 and level 2 with the
+     * 4KB granule, and only at level 2 with the 16KB and 64KB granules (ARM
+     * DDI 0487 M.d D8.3.1.2). The block encoding at any other level generates
+     * a Translation fault (AArch64.BlockDescSupported()).
+     *
+     * This is only valid without 52-bit support (FEAT_LPA, FEAT_LPA2).
+     */
+    if ( level == 0 || (level == 1 && gran != GRANULE_SIZE_INDEX_4K) )
+        return false;
+
     /* Make sure that the lower bits of the PTE's base address are zero. */
     mask = GENMASK_ULL(47, grainsizes[gran]);
     *ipa = (pfn_to_paddr(pte.walk.base) & mask) |
Signature · 1 line
WBR, Volodymyr
Reviewed-by on v2 1/4
#3 lore raw

Re: [PATCH v2 1/4] xen/arm: guest_walk: refuse a block descriptor where the granule has none

In reply to Stefano Stabellini
Quoted 10 lines · Stefano Stabellini

guest_walk_ld() accepted a block descriptor at any level. No granule has level 0 blocks, and the 16KB and 64KB granules have no level 1 blocks (ARM DDI 0487 M.d D8.3.1.2): the translation table walk makes such an entry a Translation fault (AArch64.BlockDescSupported()). Refuse it.

The check is only valid without 52-bit support (FEAT_LPA, FEAT_LPA2). Note in get_ipa_output_size() that 52-bit support requires updating it.

Fixes 7d623b358a45 ("arm/mem_access: Add long-descriptor based gpt")

~Michal

#4 lore raw

PATCH v2 2/4 xen/arm: guest_walk: refuse an address outside both halves of the range

In reply to a message not in the archive

The bits of an input address from its top bit (bit 63, or bit 55 with the top byte ignored) down to the input address size must be all 0 for TTBR0 and all 1 for TTBR1, else the walk faults (ARM DDI 0487 M.d D8.2.4, AArch64.VAIsOutOfRange()). guest_walk_ld() chose the base from the top bit alone and translated such an address. Refuse it before reading any table.

Fixes 7d623b358a45 ("arm/mem_access: Add long-descriptor based gpt")

---
Cc: Julien Grall <[email protected]>
Cc: Bertrand Marquis <[email protected]>
Cc: Michal Orzel <[email protected]>
Cc: Volodymyr Babchuk <[email protected]>

Changes in v2:
- Cite ARM DDI 0487 M.d (D8.2.4, AArch64.VAIsOutOfRange()) instead of A.k, in the commit message and the comment.
- Move the Cc list after ---.

---
 xen/arch/arm/guest_walk.c | 9 +++++++++
 1 file changed, 9 insertions(+)
xen/arch/arm/guest_walk.c +9 -0
index 151b837541..8c73ceb04d 100644
@@ -408,6 +408,15 @@ static bool guest_walk_ld(const struct vcpu *v,
         if ( (input_size > TCR_EL1_IPS_48_BIT_VAL) ||
              (input_size < TCR_EL1_IPS_MIN_VAL) )
             return false;
+
+        /*
+         * The bits of the GVA from the top bit down to the input size must
+         * all be 0 for TTBR0 and all 1 for TTBR1, or the translation faults
+         * (ARM DDI 0487 M.d D8.2.4, AArch64.VAIsOutOfRange()).
+         */
+        mask = GENMASK_ULL(topbit, input_size);
+        if ( (gva & mask) != ((gva & BIT(topbit, ULL)) ? mask : 0) )
+            return false;
     }
     else
     {
Signature · 1 line
2.47.3
Reviewed-by on v2 2/4
#5 lore raw

Re: [PATCH v2 2/4] xen/arm: guest_walk: refuse an address outside both halves of the range

Hi,

Quoted 9 lines · Stefano Stabellini

The bits of an input address from its top bit (bit 63, or bit 55 with the top byte ignored) down to the input address size must be all 0 for TTBR0 and all 1 for TTBR1, else the walk faults (ARM DDI 0487 M.d D8.2.4, AArch64.VAIsOutOfRange()). guest_walk_ld() chose the base from the top bit alone and translated such an address. Refuse it before reading any table.

Fixes 7d623b358a45 ("arm/mem_access: Add long-descriptor based gpt")
Quoted 34 lines

---
Cc: Julien Grall <[email protected]>
Cc: Bertrand Marquis <[email protected]>
Cc: Michal Orzel <[email protected]>
Cc: Volodymyr Babchuk <[email protected]>

Changes in v2:
- Cite ARM DDI 0487 M.d (D8.2.4, AArch64.VAIsOutOfRange()) instead of A.k, in the commit message and the comment.
- Move the Cc list after ---.

---
 xen/arch/arm/guest_walk.c | 9 +++++++++
 1 file changed, 9 insertions(+)
xen/arch/arm/guest_walk.c +9 -0
index 151b837541..8c73ceb04d 100644
@@ -408,6 +408,15 @@ static bool guest_walk_ld(const struct vcpu *v,
         if ( (input_size > TCR_EL1_IPS_48_BIT_VAL) ||
              (input_size < TCR_EL1_IPS_MIN_VAL) )
             return false;
+
+        /*
+         * The bits of the GVA from the top bit down to the input size must
+         * all be 0 for TTBR0 and all 1 for TTBR1, or the translation faults
+         * (ARM DDI 0487 M.d D8.2.4, AArch64.VAIsOutOfRange()).
+         */
+        mask = GENMASK_ULL(topbit, input_size);
+        if ( (gva & mask) != ((gva & BIT(topbit, ULL)) ? mask : 0) )
+            return false;
     }
     else
     {
Signature · 1 line
WBR, Volodymyr
Reviewed-by on v2 2/4
#6 lore raw

Re: [PATCH v2 2/4] xen/arm: guest_walk: refuse an address outside both halves of the range

In reply to Stefano Stabellini
Quoted 9 lines · Stefano Stabellini

The bits of an input address from its top bit (bit 63, or bit 55 with the top byte ignored) down to the input address size must be all 0 for TTBR0 and all 1 for TTBR1, else the walk faults (ARM DDI 0487 M.d D8.2.4, AArch64.VAIsOutOfRange()). guest_walk_ld() chose the base from the top bit alone and translated such an address. Refuse it before reading any table.

Fixes 7d623b358a45 ("arm/mem_access: Add long-descriptor based gpt")

~Michal

#7 lore raw

PATCH v2 3/4 xen/arm: traps: enter the SP_EL0 vector group for an exception at EL1t

In reply to a message not in the archive

An exception taken from the current level is delivered to the SP_EL0 vector group (offset 0x000) when the guest was at EL1t, and to the SP_ELx group (0x200) only at EL1h (ARM DDI 0487 M.d D1.4.1). exception_handler64() used 0x200 for both.

---
Cc: Julien Grall <[email protected]>
Cc: Bertrand Marquis <[email protected]>
Cc: Michal Orzel <[email protected]>
Cc: Volodymyr Babchuk <[email protected]>

Changes in v2:
- Cite ARM DDI 0487 M.d D1.4.1 instead of A.k, in the commit message and the comment.
- Move the Cc list after ---.
- Add Michal's Reviewed-by.

---
 xen/arch/arm/traps.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
xen/arch/arm/traps.c +7 -1
index 47a5c6bdd4..b4ca812233 100644
@@ -532,7 +532,13 @@ static vaddr_t exception_handler64(struct cpu_user_regs *regs, vaddr_t offset)
         base += VECTOR64_LOWER32_BASE;
     else if ( psr_mode(regs->cpsr,PSR_MODE_EL0t) )
         base += VECTOR64_LOWER64_BASE;
-    else /* Otherwise must be from kernel mode */
+    /*
+     * Otherwise must be from kernel mode. The current EL group is chosen by
+     * the stack pointer in use (ARM DDI 0487 M.d D1.4.1).
+     */
+    else if ( psr_mode(regs->cpsr,PSR_MODE_EL1t) )
+        base += VECTOR64_CURRENT_SP0_BASE;
+    else
         base += VECTOR64_CURRENT_SPx_BASE;
 
     return base + offset;
Signature · 1 line
2.47.3
#8 lore raw

PATCH v2 4/4 vpci/header: empty the BAR range sets when modify_bars() fails

In reply to a message not in the archive

On error modify_bars() returned with the ranges already collected still in the per-BAR sets. The next request on the device then trips the ASSERT(rangeset_is_empty()) at the start of modify_bars() in a debug build, and in a release build maps or unmaps the stale ranges along with its own. Purge every set when collecting the ranges fails.

Fixes 622bdd962822 ("vpci/header: handle p2m range sets per BAR")

---
Cc: "Roger Pau Monné" <[email protected]>
Cc: Stewart Hildebrand <[email protected]>
Cc: Jan Beulich <[email protected]>

Changes in v2:
- Mention the ASSERT(rangeset_is_empty()) in the commit message.
- Move the Cc list after ---.
- Add Jan's Reviewed-by.

---
 xen/drivers/vpci/header.c | 25 +++++++++++++++++++------
 1 file changed, 19 insertions(+), 6 deletions(-)
xen/drivers/vpci/header.c +19 -6
index 7e5f4b8a00..0bdc4d6a46 100644
@@ -366,7 +366,8 @@ static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only)
             gprintk(XENLOG_G_WARNING,
                     "%pp: can't map BAR%u - offset mismatch: %#lx vs %#lx\n",
                     &pdev->sbdf, i, bar->guest_addr, bar->addr);
-            return -EINVAL;
+            rc = -EINVAL;
+            goto fail;
         }
 
         rc = rangeset_add_range(bar->mem, start_guest, end_guest);
@@ -374,7 +375,7 @@ static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only)
         {
             printk(XENLOG_G_WARNING "Failed to add [%lx, %lx]: %d\n",
                    start_guest, end_guest, rc);
-            return rc;
+            goto fail;
         }
 
         /* Check for overlap with the already setup BAR ranges. */
@@ -391,7 +392,7 @@ static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only)
                 gprintk(XENLOG_WARNING,
                        "%pp: failed to remove overlapping range [%lx, %lx]: %d\n",
                         &pdev->sbdf, start_guest, end_guest, rc);
-                return rc;
+                goto fail;
             }
         }
 
@@ -401,7 +402,7 @@ static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only)
             gprintk(XENLOG_WARNING,
                     "%pp: failed to sanitize BAR#%u memory: %d\n",
                     &pdev->sbdf, i, rc);
-            return rc;
+            goto fail;
         }
     }
 
@@ -425,7 +426,7 @@ static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only)
                 gprintk(XENLOG_WARNING,
                        "%pp: failed to remove MSIX table [%lx, %lx]: %d\n",
                         &pdev->sbdf, start, end, rc);
-                return rc;
+                goto fail;
             }
         }
     }
@@ -487,7 +488,7 @@ static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only)
                         gprintk(XENLOG_WARNING,
                                 "%pp: failed to remove [%lx, %lx]: %d\n",
                                 &pdev->sbdf, start, end, rc);
-                        return rc;
+                        goto fail;
                     }
                 }
             }
@@ -516,6 +517,18 @@ static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only)
     defer_map(pdev, cmd, rom_only);
 
     return 0;
+
+ fail:
+    /*
+     * Leave no range collected for the request that failed: the next request
+     * expects every BAR's set to start empty, and a range left behind would
+     * be mapped or unmapped with it.
+     */
+    for ( i = 0; i < ARRAY_SIZE(header->bars); i++ )
+        if ( header->bars[i].mem )
+            rangeset_purge(header->bars[i].mem);
+
+    return rc;
 }
 
 static void cf_check cmd_write(
Signature · 1 line
2.47.3