Skip to contentSkip to conversation

linux-user/armeb: Fix __kernel_cmpxchg() for armeb

PATCH Superseded Helge Deller 3 participants 5 messages Jul 28, 2023

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 linux-user/armeb: Fix __kernel_cmpxchg() for armeb

Commit 7f4f0d9ea870 ("linux-user/arm: Implement __kernel_cmpxchg with host atomics") switched to use qatomic_cmpxchg() to swap a word with the memory content, but missed to endianess-swap the oldval and newval values when emulating an armeb CPU, which expects words to be stored in big endian in the guest memory.

The bug can be verified with qemu >= v7.2 on any little-endian host, when starting the armeb binary of the upx program, which just hangs without this patch.

--
v2:
- add tswap32() in arm_kernel_cmpxchg64_helper()

linux-user/arm/cpu_loop.c +7 -2
index a992423257..0907cd8c15 100644
@@ -117,8 +117,9 @@ static void arm_kernel_cmpxchg32_helper(CPUARMState *env)
 {
     uint32_t oldval, newval, val, addr, cpsr, *host_addr;
 
-    oldval = env->regs[0];
-    newval = env->regs[1];
+    /* endianess-swap if emulating armeb */
+    oldval = tswap32(env->regs[0]);
+    newval = tswap32(env->regs[1]);
     addr = env->regs[2];
 
     mmap_lock();
@@ -174,6 +175,10 @@ static void arm_kernel_cmpxchg64_helper(CPUARMState *env)
         return;
     }
 
+    /* endianess-swap if emulating armeb */
+    oldval = tswap32(oldval);
+    newval = tswap32(newval);
+
 #ifdef CONFIG_ATOMIC64
     val = qatomic_cmpxchg__nocheck(host_addr, oldval, newval);
     cpsr = (val == oldval) * CPSR_C;
#1 lore raw

Nitpick: the bug was introduced between 6.2.0 and 7.0.0, so "qemu >= v7.0.0"

~Markus

Quoted 51 lines · Helge Deller

Commit 7f4f0d9ea870 ("linux-user/arm: Implement __kernel_cmpxchg with host atomics") switched to use qatomic_cmpxchg() to swap a word with the memory content, but missed to endianess-swap the oldval and newval values when emulating an armeb CPU, which expects words to be stored in big endian in the guest memory.

The bug can be verified with qemu >= v7.2 on any little-endian host, when starting the armeb binary of the upx program, which just hangs without this patch.

--
v2:
- add tswap32() in arm_kernel_cmpxchg64_helper()

linux-user/arm/cpu_loop.c +7 -2
index a992423257..0907cd8c15 100644
@@ -117,8 +117,9 @@ static void arm_kernel_cmpxchg32_helper(CPUARMState *env)
 {
     uint32_t oldval, newval, val, addr, cpsr, *host_addr;
 
-    oldval = env->regs[0];
-    newval = env->regs[1];
+    /* endianess-swap if emulating armeb */
+    oldval = tswap32(env->regs[0]);
+    newval = tswap32(env->regs[1]);
     addr = env->regs[2];
 
     mmap_lock();
@@ -174,6 +175,10 @@ static void arm_kernel_cmpxchg64_helper(CPUARMState *env)
         return;
     }
 
+    /* endianess-swap if emulating armeb */
+    oldval = tswap32(oldval);
+    newval = tswap32(newval);
+
 #ifdef CONFIG_ATOMIC64
     val = qatomic_cmpxchg__nocheck(host_addr, oldval, newval);
     cpsr = (val == oldval) * CPSR_C;
Signature · 1 line
Markus Oberhumer, <[email protected]>, http://www.oberhumer.com/
#2 lore raw
In reply to Helge Deller

The fix in arm_kernel_cmpxchg64_helper probably should use tswap64() instead of tswap32().

~Markus

Quoted 51 lines · Helge Deller

Commit 7f4f0d9ea870 ("linux-user/arm: Implement __kernel_cmpxchg with host atomics") switched to use qatomic_cmpxchg() to swap a word with the memory content, but missed to endianess-swap the oldval and newval values when emulating an armeb CPU, which expects words to be stored in big endian in the guest memory.

The bug can be verified with qemu >= v7.2 on any little-endian host, when starting the armeb binary of the upx program, which just hangs without this patch.

--
v2:
- add tswap32() in arm_kernel_cmpxchg64_helper()

linux-user/arm/cpu_loop.c +7 -2
index a992423257..0907cd8c15 100644
@@ -117,8 +117,9 @@ static void arm_kernel_cmpxchg32_helper(CPUARMState *env)
 {
     uint32_t oldval, newval, val, addr, cpsr, *host_addr;
 
-    oldval = env->regs[0];
-    newval = env->regs[1];
+    /* endianess-swap if emulating armeb */
+    oldval = tswap32(env->regs[0]);
+    newval = tswap32(env->regs[1]);
     addr = env->regs[2];
 
     mmap_lock();
@@ -174,6 +175,10 @@ static void arm_kernel_cmpxchg64_helper(CPUARMState *env)
         return;
     }
 
+    /* endianess-swap if emulating armeb */
+    oldval = tswap32(oldval);
+    newval = tswap32(newval);
+
 #ifdef CONFIG_ATOMIC64
     val = qatomic_cmpxchg__nocheck(host_addr, oldval, newval);
     cpsr = (val == oldval) * CPSR_C;
Signature · 1 line
Markus Oberhumer, <[email protected]>, http://www.oberhumer.com/
#3 lore raw
In reply to Helge Deller

linux-user/arm/cpu_loop.c static void arm_kernel_cmpxchg64_helper(CPUARMState *env) · line 180

Quoted 7 lines · Helge Deller
linux-user/arm/cpu_loop.c +3 -0
@@ -174,6 +175,10 @@ static void arm_kernel_cmpxchg64_helper(CPUARMState *env)
          return;
      }
 
+    /* endianess-swap if emulating armeb */
+    oldval = tswap32(oldval);
+    newval = tswap32(newval);

Must be tswap64.

r~

#4 lore raw
Quoted 10 lines · Richard Henderson

linux-user/arm/cpu_loop.c static void arm_kernel_cmpxchg64_helper(CPUARMState *env) · line 180

Quoted 7 lines · Helge Deller
linux-user/arm/cpu_loop.c +3 -0
@@ -174,6 +175,10 @@ static void arm_kernel_cmpxchg64_helper(CPUARMState *env)
           return;
       }
 
+    /* endianess-swap if emulating armeb */
+    oldval = tswap32(oldval);
+    newval = tswap32(newval);

Must be tswap64.

Oh, of course!
I send a v3.

Helge