Skip to contentSkip to conversation

xen/arm: traps: Fix sign extension logic in sign_extend()

PATCH In review Michal Orzel 2 participants 2 messages Nov 18 – Nov 19

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen/arm: traps: Fix sign extension logic in sign_extend()

At the moment, we always sign extend to the full register width, i.e. 64-bit on Arm64, even though for byte and half-word accesses the destination can also be a W (32-bit) register (LDRSB Wt, LDRSH Wt). In that case, the architecture requires bits [63:32] of Xt to be zero, but a subsequent use of Xt by the guest sees them set to ones. Fix it.

Fixes ac0ca6895b65 ("xen/arm: io: Support sign-extension for every read access")
---
 xen/arch/arm/include/asm/traps.h | 6 ++++++
 1 file changed, 6 insertions(+)
xen/arch/arm/include/asm/traps.h +6 -0
index 3b40afe26259..c5b6ff5b788b 100644
@@ -111,6 +111,12 @@ static inline register_t sign_extend(const struct hsr_dabt dabt, register_t r)
          */
         BUILD_BUG_ON(sizeof(register_t) != sizeof(unsigned long));
         r |= (~0UL) << size;
+
+#ifdef CONFIG_ARM_64
+        /* When loading to W register we should only sign extend to 32-bit */
+        if ( !dabt.sf )
+            r = (uint32_t)r;
+#endif
     }
 
     return r;
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Michal Orzel

At the moment, we always sign extend to the full register width, i.e. 64-bit on Arm64, even though for byte and half-word accesses the destination can also be a W (32-bit) register (LDRSB Wt, LDRSH Wt). In that case, the architecture requires bits [63:32] of Xt to be zero, but a subsequent use of Xt by the guest sees them set to ones. Fix it.

Fixes ac0ca6895b65 ("xen/arm: io: Support sign-extension for every read access")
Quoted 24 lines
---
 xen/arch/arm/include/asm/traps.h | 6 ++++++
 1 file changed, 6 insertions(+)
xen/arch/arm/include/asm/traps.h +6 -0
index 3b40afe26259..c5b6ff5b788b 100644
@@ -111,6 +111,12 @@ static inline register_t sign_extend(const struct hsr_dabt dabt, register_t r)
          */
         BUILD_BUG_ON(sizeof(register_t) != sizeof(unsigned long));
         r |= (~0UL) << size;
+
+#ifdef CONFIG_ARM_64
+        /* When loading to W register we should only sign extend to 32-bit */
+        if ( !dabt.sf )
+            r = (uint32_t)r;
+#endif
     }
 
     return r;
Signature · 1 line
2.43.0