Skip to contentSkip to conversation

linux-user/arm: Reset CPSR_E when entering a signal handler

PATCH Superseded Amanieu d'Antras 3 participants 8 messages May 7, 2020 – May 16, 2020

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 linux-user/arm: Reset CPSR_E when entering a signal handler

This fixes signal handlers running with the wrong endianness if the interrupted code used SETEND to dynamically switch endianness.

---
 linux-user/arm/signal.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
linux-user/arm/signal.c +8 -1
index d96fc27ce1..8aca5f61b7 100644
@@ -244,6 +244,12 @@ setup_return(CPUARMState *env, struct target_sigaction *ka,
     } else {
         cpsr &= ~CPSR_T;
     }
+    cpsr &= ~CPSR_E;
+#ifdef TARGET_WORDS_BIGENDIAN
+    if (env->cp15.sctlr_el[1] & SCTLR_E0E) {
+        cpsr |= CPSR_E;
+    }
+#endif
 
     if (ka->sa_flags & TARGET_SA_RESTORER) {
         if (is_fdpic) {
@@ -287,7 +293,8 @@ setup_return(CPUARMState *env, struct target_sigaction *ka,
     env->regs[13] = frame_addr;
     env->regs[14] = retcode;
     env->regs[15] = handler & (thumb ? ~1 : ~3);
-    cpsr_write(env, cpsr, CPSR_IT | CPSR_T, CPSRWriteByInstr);
+    cpsr_write(env, cpsr, CPSR_IT | CPSR_T | CPSR_E, CPSRWriteByInstr);
+    arm_rebuild_hflags(env);
 
     return 0;
 }
Signature · 1 line
2.26.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 14 lines · Amanieu d'Antras

This fixes signal handlers running with the wrong endianness if the interrupted code used SETEND to dynamically switch endianness.

---
 linux-user/arm/signal.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
linux-user/arm/signal.c +0 -0
index d96fc27ce1..8aca5f61b7 100644

linux-user/arm/signal.c setup_return(CPUARMState *env, struct target_sigaction *ka, · line 252

Quoted 9 lines
linux-user/arm/signal.c +6 -0
@@ -244,6 +244,12 @@ setup_return(CPUARMState *env, struct target_sigaction *ka,
     } else {
         cpsr &= ~CPSR_T;
     }
+    cpsr &= ~CPSR_E;
+#ifdef TARGET_WORDS_BIGENDIAN
+    if (env->cp15.sctlr_el[1] & SCTLR_E0E) {
+        cpsr |= CPSR_E;
+    }
+#endif

The #ifdef isn't incorrect, but I don't think we need it -- if we're emulating a little-endian binary then SCTLR_E0E will be 0 and we won't set CPSR.E. So we can drop the #ifdef and the code is a little cleaner to read.

Otherwise

thanks
-- PMM

#2 lore raw
In reply to Amanieu d'Antras
Quoted 27 lines · Amanieu d'Antras

This fixes signal handlers running with the wrong endianness if the interrupted code used SETEND to dynamically switch endianness.

---
 linux-user/arm/signal.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
linux-user/arm/signal.c +6 -0
index d96fc27ce1..8aca5f61b7 100644
@@ -244,6 +244,12 @@ setup_return(CPUARMState *env, struct target_sigaction *ka,
     } else {
         cpsr &= ~CPSR_T;
     }
+    cpsr &= ~CPSR_E;
+#ifdef TARGET_WORDS_BIGENDIAN
+    if (env->cp15.sctlr_el[1] & SCTLR_E0E) {
+        cpsr |= CPSR_E;
+    }
+#endif
 
     if (ka->sa_flags & TARGET_SA_RESTORER) {
         if (is_fdpic) {

linux-user/arm/signal.c setup_return(CPUARMState *env, struct target_sigaction *ka, · line 297

Quoted 6 lines
linux-user/arm/signal.c +2 -1
@@ -287,7 +293,8 @@ setup_return(CPUARMState *env, struct target_sigaction *ka,
     env->regs[13] = frame_addr;
     env->regs[14] = retcode;
     env->regs[15] = handler & (thumb ? ~1 : ~3);
-    cpsr_write(env, cpsr, CPSR_IT | CPSR_T, CPSRWriteByInstr);
+    cpsr_write(env, cpsr, CPSR_IT | CPSR_T | CPSR_E, CPSRWriteByInstr);
+    arm_rebuild_hflags(env);

I was just looking at the signal code's handling of CPSR for a different reason, and I noticed that at the moment we don't allow CPSR.E to be updated from the signal frame when the signal handler returns (because CPSR_USER doesn't contain CPSR_E and that's what we use in restore_sigcontext() to define what bits from the frame we allow updating). Don't you find that when the interrupted code returns from the signal handler that it ends up running with the wrong endianness (ie the endianness the handler used) ?

I'm going to fix this by putting CPSR_E in CPSR_USER, anyway.

thanks
-- PMM

#3 lore raw
Quoted 36 lines · Peter Maydell
Quoted 27 lines · Amanieu d'Antras

This fixes signal handlers running with the wrong endianness if the interrupted code used SETEND to dynamically switch endianness.

---
 linux-user/arm/signal.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
linux-user/arm/signal.c +6 -0
index d96fc27ce1..8aca5f61b7 100644
@@ -244,6 +244,12 @@ setup_return(CPUARMState *env, struct target_sigaction *ka,
     } else {
         cpsr &= ~CPSR_T;
     }
+    cpsr &= ~CPSR_E;
+#ifdef TARGET_WORDS_BIGENDIAN
+    if (env->cp15.sctlr_el[1] & SCTLR_E0E) {
+        cpsr |= CPSR_E;
+    }
+#endif
 
     if (ka->sa_flags & TARGET_SA_RESTORER) {
         if (is_fdpic) {

linux-user/arm/signal.c setup_return(CPUARMState *env, struct target_sigaction *ka, · line 297

Quoted 6 lines
linux-user/arm/signal.c +2 -1
@@ -287,7 +293,8 @@ setup_return(CPUARMState *env, struct target_sigaction *ka,
     env->regs[13] = frame_addr;
     env->regs[14] = retcode;
     env->regs[15] = handler & (thumb ? ~1 : ~3);
-    cpsr_write(env, cpsr, CPSR_IT | CPSR_T, CPSRWriteByInstr);
+    cpsr_write(env, cpsr, CPSR_IT | CPSR_T | CPSR_E, CPSRWriteByInstr);
+    arm_rebuild_hflags(env);
Quoted 8 lines

I was just looking at the signal code's handling of CPSR for a different reason, and I noticed that at the moment we don't allow CPSR.E to be updated from the signal frame when the signal handler returns (because CPSR_USER doesn't contain CPSR_E and that's what we use in restore_sigcontext() to define what bits from the frame we allow updating). Don't you find that when the interrupted code returns from the signal handler that it ends up running with the wrong endianness (ie the endianness the handler used) ?

I actually found this while trying to test the SETEND instruction under risu. The signal handler was crashing because it loaded a pointer with the wrong endianness, which was pretty obvious. However I missed the fact that code was now running with the wrong endianness after returning from the signal handler since I had both the master and the apprentice running under qemu-arm.

Quoted 1 line

I'm going to fix this by putting CPSR_E in CPSR_USER, anyway.

You also need to call arm_rebuild_hflags() after modifying CPSR_E otherwise the change doesn't take effect.

-- Amanieu

#4 lore raw
Quoted 17 lines · Amanieu d'Antras
Quoted 8 lines · Peter Maydell

I was just looking at the signal code's handling of CPSR for a different reason, and I noticed that at the moment we don't allow CPSR.E to be updated from the signal frame when the signal handler returns (because CPSR_USER doesn't contain CPSR_E and that's what we use in restore_sigcontext() to define what bits from the frame we allow updating). Don't you find that when the interrupted code returns from the signal handler that it ends up running with the wrong endianness (ie the endianness the handler used) ?

I actually found this while trying to test the SETEND instruction under risu. The signal handler was crashing because it loaded a pointer with the wrong endianness, which was pretty obvious. However I missed the fact that code was now running with the wrong endianness after returning from the signal handler since I had both the master and the apprentice running under qemu-arm.

Quoted 4 lines
Quoted 1 line

I'm going to fix this by putting CPSR_E in CPSR_USER, anyway.

You also need to call arm_rebuild_hflags() after modifying CPSR_E otherwise the change doesn't take effect.

Hmm. I was expecting cpsr_write() to take care of that if we updated a cpsr flag that was in the hflags, but it looks like the rebuild_hflags() is in the HELPER() wrapper but not in cpsr_write() itself. Richard, does anything go wrong if cpsr_write() proper does the hflags rebuild ?

thanks
-- PMM

#5 lore raw
Quoted 8 lines · Peter Maydell
Quoted 2 lines

You also need to call arm_rebuild_hflags() after modifying CPSR_E otherwise the change doesn't take effect.

Hmm. I was expecting cpsr_write() to take care of that if we updated a cpsr flag that was in the hflags, but it looks like the rebuild_hflags() is in the HELPER() wrapper but not in cpsr_write() itself. Richard, does anything go wrong if cpsr_write() proper does the hflags rebuild ?

We wind up rebuilding hflags multiple times, is all.

Most of the time we call cpsr_write we also do something else that also requires a rebuild. So we do it once after all updates.

We could probably rearrange code such that the cpsr_write is last, and then sink the rebuild. But I don't know if that's clearer, hiding the side effect.

r~

#6 lore raw
Quoted 11 lines · Richard Henderson
Quoted 8 lines · Peter Maydell
Quoted 2 lines

You also need to call arm_rebuild_hflags() after modifying CPSR_E otherwise the change doesn't take effect.

Hmm. I was expecting cpsr_write() to take care of that if we updated a cpsr flag that was in the hflags, but it looks like the rebuild_hflags() is in the HELPER() wrapper but not in cpsr_write() itself. Richard, does anything go wrong if cpsr_write() proper does the hflags rebuild ?

Quoted 4 lines

We wind up rebuilding hflags multiple times, is all.

Most of the time we call cpsr_write we also do something else that also requires a rebuild. So we do it once after all updates.

The downside is that it leaves a trap which makes it really easy to introduce bugs where hflags aren't rebuilt: as a caller of cpsr_write() I don't really want to have to care which cpsr flags happen to be in the hflags or not, and it's particularly awkward that simply fixing which flags belong in CPSR_USER suddenly means that a call that happened to be OK before is now buggy.

thanks
-- PMM

#7 lore raw
Quoted 18 lines · Peter Maydell
Quoted 11 lines · Richard Henderson
Quoted 8 lines · Peter Maydell
Quoted 2 lines

You also need to call arm_rebuild_hflags() after modifying CPSR_E otherwise the change doesn't take effect.

Hmm. I was expecting cpsr_write() to take care of that if we updated a cpsr flag that was in the hflags, but it looks like the rebuild_hflags() is in the HELPER() wrapper but not in cpsr_write() itself. Richard, does anything go wrong if cpsr_write() proper does the hflags rebuild ?

Quoted 4 lines

We wind up rebuilding hflags multiple times, is all.

Most of the time we call cpsr_write we also do something else that also requires a rebuild. So we do it once after all updates.

Quoted 7 lines

The downside is that it leaves a trap which makes it really easy to introduce bugs where hflags aren't rebuilt: as a caller of cpsr_write() I don't really want to have to care which cpsr flags happen to be in the hflags or not, and it's particularly awkward that simply fixing which flags belong in CPSR_USER suddenly means that a call that happened to be OK before is now buggy.

I don't see any way around that.

As I said, if we put the rebuild in cpsr_write, then we should also rearrange the code that calls cpsr_write to assume that's where the rebuild gets done.

r~