Skip to contentSkip to conversation

sh4: add signal handling support for user space emulator

PATCH In review Magnus Damm 2 participants 3 messages Nov 21, 2007 – Nov 22, 2007

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sh4: add signal handling support for user space emulator

Hi everyone,

This adds SH support to the user space signal emulation code. Please apply.

Thanks,

/ magnus

linux-user/main.c +12 -0
@@ -1617,6 +1617,9 @@ void cpu_loop (CPUState *env)
             env->gregs[0] = ret;
             env->pc += 2;
             break;
+        case EXCP_INTERRUPT:
+            /* just indicate that signals should be handled asap */
+            break;
         case EXCP_DEBUG:
             {
                 int sig;
@@ -1631,6 +1634,15 @@ void cpu_loop (CPUState *env)
                   }
             }
             break;
+	case 0xa0:
+	case 0xc0:
+            info.si_signo = SIGSEGV;
+            info.si_errno = 0;
+            info.si_code = TARGET_SEGV_MAPERR;
+            info._sifields._sigfault._addr = env->tea;
+            queue_signal(info.si_signo, &info);
+	    break;
+
         default:
             printf ("Unhandled trap: 0x%x\n", trapnr);
             cpu_dump_state(env, stderr, fprintf, 0);
linux-user/sh4/target_signal.h +5 -0
@@ -21,4 +21,9 @@ typedef struct target_sigaltstack {
 #define TARGET_MINSIGSTKSZ    2048
 #define TARGET_SIGSTKSZ       8192
 
+static inline abi_ulong get_sp_from_cpustate(CPUSH4State *state)
+{
+    return state->gregs[15];
+}
+
 #endif /* TARGET_SIGNAL_H */
linux-user/signal.c +322 -10

356 lines not on this page · raw message

target-sh4/helper.c +3 -3
@@ -39,16 +39,16 @@ int cpu_sh4_handle_mmu_fault(CPUState * 
 			     int mmu_idx, int is_softmmu)
 {
     env->tea = address;
+    env->exception_index = 0;
     switch (rw) {
     case 0:
+	env->tea = address;
         env->exception_index = 0x0a0;
         break;
     case 1:
+	env->tea = address;
         env->exception_index = 0x0c0;
         break;
-    case 2:
-        env->exception_index = 0x0a0;
-        break;
     }
     return 1;
 }
translate-all.c +2 -0
@@ -298,6 +298,8 @@ int cpu_restore_state(TranslationBlock *
     env->hflags |= gen_opc_hflags[j];
 #elif defined(TARGET_ALPHA)
     env->pc = gen_opc_pc[j];
+#elif defined(TARGET_SH4)
+    env->pc = gen_opc_pc[j];
 #endif
     return 0;
 }
#1 lore raw

linux-user/signal.c long do_rt_sigreturn(CPUState *env) · line 2461

Quoted 3 lines · Magnus Damm
linux-user/signal.c +3 -0
+#define MOVW(n)  (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
+#define TRAP_NOARG 0xc310         /* Syscall w/no args (NR in R3) SH3/4 */
+#define OR_R0_R0 0x200b           /* or r0,r0 (insert to avoid hardware bug) */

This is legacy SH-4 errata, and does not pop up on any of the other parts. These days all SH-4 parts return through the vsyscall page, where we deal with this errata directly without having to push it in to the return frame. While you might not want to deal with implementing the vsyscall page, there's definitely no point in crapping up the return frame with this chip errata in the general case.

#2 lore raw
Quoted 10 lines · Paul Mundt

Review

Quoted 3 lines · Magnus Damm
(quoted diff) +3 -0
+#define MOVW(n)  (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
+#define TRAP_NOARG 0xc310         /* Syscall w/no args (NR in R3) SH3/4 */
+#define OR_R0_R0 0x200b           /* or r0,r0 (insert to avoid hardware bug) */

This is legacy SH-4 errata, and does not pop up on any of the other parts. These days all SH-4 parts return through the vsyscall page, where we deal with this errata directly without having to push it in to the return frame. While you might not want to deal with implementing the vsyscall page, there's definitely no point in crapping up the return frame with this chip errata in the general case.

Thanks for your feedback. vsyscall sounds nice and all, but I prefer to do this in incremental steps. So let's start with this.

I've attached a new version of the patch where the OR instructions are removed. Please apply.

Thanks,

/ magnus

linux-user/main.c +12 -0
@@ -1617,6 +1617,9 @@ void cpu_loop (CPUState *env)
             env->gregs[0] = ret;
             env->pc += 2;
             break;
+        case EXCP_INTERRUPT:
+            /* just indicate that signals should be handled asap */
+            break;
         case EXCP_DEBUG:
             {
                 int sig;
@@ -1631,6 +1634,15 @@ void cpu_loop (CPUState *env)
                   }
             }
             break;
+	case 0xa0:
+	case 0xc0:
+            info.si_signo = SIGSEGV;
+            info.si_errno = 0;
+            info.si_code = TARGET_SEGV_MAPERR;
+            info._sifields._sigfault._addr = env->tea;
+            queue_signal(info.si_signo, &info);
+	    break;
+
         default:
             printf ("Unhandled trap: 0x%x\n", trapnr);
             cpu_dump_state(env, stderr, fprintf, 0);
linux-user/sh4/target_signal.h +5 -0
@@ -21,4 +21,9 @@ typedef struct target_sigaltstack {
 #define TARGET_MINSIGSTKSZ    2048
 #define TARGET_SIGSTKSZ       8192
 
+static inline abi_ulong get_sp_from_cpustate(CPUSH4State *state)
+{
+    return state->gregs[15];
+}
+
 #endif /* TARGET_SIGNAL_H */
linux-user/signal.c +311 -10

345 lines not on this page · raw message

target-sh4/helper.c +3 -3
@@ -39,16 +39,16 @@ int cpu_sh4_handle_mmu_fault(CPUState * 
 			     int mmu_idx, int is_softmmu)
 {
     env->tea = address;
+    env->exception_index = 0;
     switch (rw) {
     case 0:
+	env->tea = address;
         env->exception_index = 0x0a0;
         break;
     case 1:
+	env->tea = address;
         env->exception_index = 0x0c0;
         break;
-    case 2:
-        env->exception_index = 0x0a0;
-        break;
     }
     return 1;
 }
translate-all.c +2 -0
@@ -298,6 +298,8 @@ int cpu_restore_state(TranslationBlock *
     env->hflags |= gen_opc_hflags[j];
 #elif defined(TARGET_ALPHA)
     env->pc = gen_opc_pc[j];
+#elif defined(TARGET_SH4)
+    env->pc = gen_opc_pc[j];
 #endif
     return 0;
 }