Skip to contentSkip to conversation

target: hppa: Fix unaligned double word accesses for hppa64

PATCH Superseded Guenter Roeck 4 participants 5 messages Feb 16, 2024

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 target: hppa: Fix unaligned double word accesses for hppa64

Unaligned 64-bit accesses were found in Linux to clobber carry bits, resulting in bad results if an arithmetic operation involving a carry bit was executed after an unaligned 64-bit operation.

hppa 2.0 defines additional carry bits in PSW register bits 32..39. When restoring PSW after executing an unaligned instruction trap, those bits were not cleared and ended up to be active all the time. Clearing bit 32..39 in psw prior to restoring it solves the problem.

Fixes 931adff31478 ("target/hppa: Update cpu_hppa_get/put_psw for hppa64")
Cc Richard Henderson <[email protected]>
Cc Charlie Jenkins <[email protected]>
Cc Helge Deller <[email protected]>
---
 target/hppa/helper.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
target/hppa/helper.c +6 -1
index 859644c47a..7b798d1227 100644
@@ -76,7 +76,12 @@ void cpu_hppa_put_psw(CPUHPPAState *env, target_ulong psw)
     }
     psw &= ~reserved;
 
-    env->psw = psw & ~(PSW_N | PSW_V | PSW_CB);
+    if (hppa_is_pa20(env)) {
+        env->psw = psw & ~(PSW_N | PSW_V | PSW_CB | 0xff00000000ull);
+    } else {
+        env->psw = psw & ~(PSW_N | PSW_V | PSW_CB);
+    }
+
     env->psw_n = (psw / PSW_N) & 1;
     env->psw_v = -((psw / PSW_V) & 1);
 
Signature · 1 line
2.39.2
#1 lore raw
Quoted 23 lines · Guenter Roeck

Unaligned 64-bit accesses were found in Linux to clobber carry bits, resulting in bad results if an arithmetic operation involving a carry bit was executed after an unaligned 64-bit operation.

hppa 2.0 defines additional carry bits in PSW register bits 32..39. When restoring PSW after executing an unaligned instruction trap, those bits were not cleared and ended up to be active all the time. Clearing bit 32..39 in psw prior to restoring it solves the problem.

Fixes 931adff31478 ("target/hppa: Update cpu_hppa_get/put_psw for hppa64")
Cc Richard Henderson <[email protected]>
Cc Charlie Jenkins <[email protected]>
Cc Helge Deller <[email protected]>
---
 target/hppa/helper.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
target/hppa/helper.c +0 -0
index 859644c47a..7b798d1227 100644

target/hppa/helper.c void cpu_hppa_put_psw(CPUHPPAState *env, target_ulong psw) · line 80

Quoted 6 lines
target/hppa/helper.c +2 -1
@@ -76,7 +76,12 @@ void cpu_hppa_put_psw(CPUHPPAState *env, target_ulong psw)
     }
     psw &= ~reserved;
 
-    env->psw = psw & ~(PSW_N | PSW_V | PSW_CB);
+    if (hppa_is_pa20(env)) {
+        env->psw = psw & ~(PSW_N | PSW_V | PSW_CB | 0xff00000000ull);

I thought there was something fishy in this function but was slow on the uptake...

How about defining a new macro (PSW_CB_HIGH) to hold this value?

- Charlie

Quoted 10 lines
target/hppa/helper.c +4 -0
+    } else {
+        env->psw = psw & ~(PSW_N | PSW_V | PSW_CB);
+    }
+
     env->psw_n = (psw / PSW_N) & 1;
     env->psw_v = -((psw / PSW_V) & 1);
 
Signature · 1 line
2.39.2
#2 lore raw
Quoted 27 lines · Charlie Jenkins
Quoted 26 lines · Guenter Roeck

Unaligned 64-bit accesses were found in Linux to clobber carry bits, resulting in bad results if an arithmetic operation involving a carry bit was executed after an unaligned 64-bit operation.

hppa 2.0 defines additional carry bits in PSW register bits 32..39. When restoring PSW after executing an unaligned instruction trap, those bits were not cleared and ended up to be active all the time. Clearing bit 32..39 in psw prior to restoring it solves the problem.

Fixes 931adff31478 ("target/hppa: Update cpu_hppa_get/put_psw for hppa64")
Cc Richard Henderson <[email protected]>
Cc Charlie Jenkins <[email protected]>
Cc Helge Deller <[email protected]>
---
  target/hppa/helper.c | 7 ++++++-
  1 file changed, 6 insertions(+), 1 deletion(-)
target/hppa/helper.c +0 -0
index 859644c47a..7b798d1227 100644
@@ -76,7 +76,12 @@ void cpu_hppa_put_psw(CPUHPPAState *env, target_ulong psw)
      }
      psw &= ~reserved;
 
Quoted 8 lines

target/hppa/helper.c void cpu_hppa_put_psw(CPUHPPAState *env, target_ulong psw) · line 80

Quoted 3 lines
target/hppa/helper.c +2 -1
-    env->psw = psw & ~(PSW_N | PSW_V | PSW_CB);
+    if (hppa_is_pa20(env)) {
+        env->psw = psw & ~(PSW_N | PSW_V | PSW_CB | 0xff00000000ull);

I thought there was something fishy in this function but was slow on the uptake...

How about defining a new macro (PSW_CB_HIGH) to hold this value?

...and avoid the hppa_is_pa20() by using PSW_CB_HIGH unconditionally on 32-bit too (which then gets optimized-out by the compiler).

Nice catch btw!
I wonder if this finally fixes 64-bit Linux kernels on qemu-hppa20....?

Helge

Quoted 12 lines

- Charlie

Quoted 10 lines
target/hppa/helper.c +4 -1
+    } else {
+        env->psw = psw & ~(PSW_N | PSW_V | PSW_CB);
+    }
+
      env->psw_n = (psw / PSW_N) & 1;
      env->psw_v = -((psw / PSW_V) & 1);
 
--

2.39.2

Reviewed-by on v1 1/1
#3 lore raw
In reply to Guenter Roeck

target/hppa/helper.c void cpu_hppa_put_psw(CPUHPPAState *env, target_ulong psw) · line 83

Quoted 6 lines · Guenter Roeck
target/hppa/helper.c +5 -1
-    env->psw = psw & ~(PSW_N | PSW_V | PSW_CB);
+    if (hppa_is_pa20(env)) {
+        env->psw = psw & ~(PSW_N | PSW_V | PSW_CB | 0xff00000000ull);
+    } else {
+        env->psw = psw & ~(PSW_N | PSW_V | PSW_CB);
+    }

There are never any bits above 31 set in env->psw, because all of the CB bits are supposed to be stored in env->psw_cb. Thus

env->psw = psw & (uint32_t)~(...)

with no need for the pa20 check.

With that,

r~

#4 lore raw
Quoted 10 lines · Richard Henderson
Quoted 6 lines · Guenter Roeck

-    env->psw = psw & ~(PSW_N | PSW_V | PSW_CB);
+    if (hppa_is_pa20(env)) {
+        env->psw = psw & ~(PSW_N | PSW_V | PSW_CB | 0xff00000000ull); +    } else {
+        env->psw = psw & ~(PSW_N | PSW_V | PSW_CB);
+    }

There are never any bits above 31 set in env->psw, because all of the CB bits are supposed to be stored in env->psw_cb.  Thus

Quoted 8 lines

  env->psw = psw & (uint32_t)~(...)

with no need for the pa20 check.

With that,

sgtm. I'll test that and send v2 tomorrow (it is getting late).

Thanks,
Guenter