Skip to contentSkip to conversation

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

PATCH In review Helge Deller 3 participants 6 messages Jul 28, 2023 – Jul 31, 2023

lore
Share & tools
lore
#0 lore raw

PATCH v3 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.0 on any little-endian host, when starting the armeb binary of the upx program, which just hangs without this patch.

--
v3:
- use tswap64() in arm_kernel_cmpxchg64_helper() [review from Richard]
- mention that bug exists since qemu v7.0 [info from Markus] v2:
- add tswap32() in arm_kernel_cmpxchg64_helper() [bisected by John]

linux-user/arm/cpu_loop.c +7 -2
index a992423257..f58154108e 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 = tswap64(oldval);
+    newval = tswap64(newval);
+
 #ifdef CONFIG_ATOMIC64
     val = qatomic_cmpxchg__nocheck(host_addr, oldval, newval);
     cpsr = (val == oldval) * CPSR_C;
Reviewed-by on v3 1/1
#1 lore raw
Quoted 18 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.0 on any little-endian host, when starting the armeb binary of the upx program, which just hangs without this patch.

Quoted 6 lines

--
v3:
- use tswap64() in arm_kernel_cmpxchg64_helper() [review from Richard]
- mention that bug exists since qemu v7.0 [info from Markus] v2:
- add tswap32() in arm_kernel_cmpxchg64_helper() [bisected by John]

r~

#2 lore raw
In reply to Helge Deller

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

Quoted 3 lines · Helge Deller
linux-user/arm/cpu_loop.c +3 -0
+    /* endianess-swap if emulating armeb */
+    oldval = tswap64(oldval);
+    newval = tswap64(newval);

Oh btw, it's not about arm vs armeb, but guest vs host. This also fixes armel on big-endian hosts.

r~

#3 lore raw
Quoted 7 lines · Richard Henderson
Quoted 3 lines · Helge Deller

+    /* endianess-swap if emulating armeb */
+    oldval = tswap64(oldval);
+    newval = tswap64(newval);

Oh btw, it's not about arm vs armeb, but guest vs host. This also fixes armel on big-endian hosts.

Anyway, I adjusted the comment and queued.

r~

#4 lore raw
Quoted 8 lines · Richard Henderson
Quoted 7 lines · Richard Henderson
Quoted 3 lines · Helge Deller

+    /* endianess-swap if emulating armeb */
+    oldval = tswap64(oldval);
+    newval = tswap64(newval);

Oh btw, it's not about arm vs armeb, but guest vs host. This also fixes armel on big-endian hosts.

Even better: One patch fixes multiple issues :-)

Quoted 1 line

Anyway, I adjusted the comment and queued.

Thank you!

Helge

Reviewed-by on v3 1/1
#5 lore raw
In reply to Helge Deller
Quoted 18 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.0 on any little-endian host, when starting the armeb binary of the upx program, which just hangs without this patch.

Quoted 6 lines

--
v3:
- use tswap64() in arm_kernel_cmpxchg64_helper() [review from Richard]
- mention that bug exists since qemu v7.0 [info from Markus] v2:
- add tswap32() in arm_kernel_cmpxchg64_helper() [bisected by John]