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

Michal Orzel posted 1 patch 5 days, 13 hours ago
xen/arch/arm/include/asm/traps.h | 6 ++++++
1 file changed, 6 insertions(+)
[PATCH] xen/arm: traps: Fix sign extension logic in sign_extend()
Posted by Michal Orzel 5 days, 13 hours ago
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")
Signed-off-by: Michal Orzel <michal.orzel@amd.com>
---
 xen/arch/arm/include/asm/traps.h | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/xen/arch/arm/include/asm/traps.h b/xen/arch/arm/include/asm/traps.h
index 3b40afe26259..c5b6ff5b788b 100644
--- a/xen/arch/arm/include/asm/traps.h
+++ b/xen/arch/arm/include/asm/traps.h
@@ -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;
-- 
2.43.0
Re: [PATCH] xen/arm: traps: Fix sign extension logic in sign_extend()
Posted by Stefano Stabellini 4 days, 21 hours ago
On Wed, 18 Nov 2026, Michal Orzel wrote:
> 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")
> Signed-off-by: Michal Orzel <michal.orzel@amd.com>

Reviewed-by: Stefano Stabellini <sstabellini@kernel.org>


> ---
>  xen/arch/arm/include/asm/traps.h | 6 ++++++
>  1 file changed, 6 insertions(+)
> 
> diff --git a/xen/arch/arm/include/asm/traps.h b/xen/arch/arm/include/asm/traps.h
> index 3b40afe26259..c5b6ff5b788b 100644
> --- a/xen/arch/arm/include/asm/traps.h
> +++ b/xen/arch/arm/include/asm/traps.h
> @@ -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;
> -- 
> 2.43.0
>