[2/3] linux-user/i386: Split out gen_signal
diff mbox series

Message ID 20200114210921.11216-3-richard.henderson@linaro.org
State New
Headers show
Series
  • linux-user: Implement x86_64 vsyscalls
Related show

Commit Message

Richard Henderson Jan. 14, 2020, 9:09 p.m. UTC
This is a bit tidier than open-coding the 5 lines necessary
to initialize the target_siginfo_t.  In addition, this zeros
the remaining bytes of the target_siginfo_t, rather than
passing in garbage.

Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
---
 linux-user/i386/cpu_loop.c | 93 ++++++++++++++------------------------
 1 file changed, 33 insertions(+), 60 deletions(-)

Comments

Philippe Mathieu-Daudé Jan. 15, 2020, 7:22 a.m. UTC | #1
On 1/14/20 10:09 PM, Richard Henderson wrote:
> This is a bit tidier than open-coding the 5 lines necessary
> to initialize the target_siginfo_t.  In addition, this zeros
> the remaining bytes of the target_siginfo_t, rather than
> passing in garbage.
> 
> Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
> ---
>   linux-user/i386/cpu_loop.c | 93 ++++++++++++++------------------------
>   1 file changed, 33 insertions(+), 60 deletions(-)
> 
> diff --git a/linux-user/i386/cpu_loop.c b/linux-user/i386/cpu_loop.c
> index 024b6f4d58..e217cca5ee 100644
> --- a/linux-user/i386/cpu_loop.c
> +++ b/linux-user/i386/cpu_loop.c
> @@ -81,13 +81,23 @@ static void set_idt(int n, unsigned int dpl)
>   }
>   #endif
>   
> +static void gen_signal(CPUX86State *env, int sig, int code, abi_ptr addr)
> +{
> +    target_siginfo_t info = {
> +        .si_signo = sig,
> +        .si_code = code,
> +        ._sifields._sigfault._addr = addr
> +    };
> +
> +    queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
> +}
> +
>   void cpu_loop(CPUX86State *env)
>   {
>       CPUState *cs = env_cpu(env);
>       int trapnr;
>       abi_ulong pc;
>       abi_ulong ret;
> -    target_siginfo_t info;
>   
>       for(;;) {
>           cpu_exec_start(cs);
> @@ -134,70 +144,45 @@ void cpu_loop(CPUX86State *env)
>   #endif
>           case EXCP0B_NOSEG:
>           case EXCP0C_STACK:
> -            info.si_signo = TARGET_SIGBUS;
> -            info.si_errno = 0;
> -            info.si_code = TARGET_SI_KERNEL;
> -            info._sifields._sigfault._addr = 0;
> -            queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
> +            gen_signal(env, TARGET_SIGBUS, TARGET_SI_KERNEL, 0);
>               break;
>           case EXCP0D_GPF:
>               /* XXX: potential problem if ABI32 */
>   #ifndef TARGET_X86_64
>               if (env->eflags & VM_MASK) {
>                   handle_vm86_fault(env);
> -            } else
> -#endif
> -            {
> -                info.si_signo = TARGET_SIGSEGV;
> -                info.si_errno = 0;
> -                info.si_code = TARGET_SI_KERNEL;
> -                info._sifields._sigfault._addr = 0;
> -                queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
> +                break;
>               }
> +#endif
> +            gen_signal(env, TARGET_SIGSEGV, TARGET_SI_KERNEL, 0);
>               break;
>           case EXCP0E_PAGE:
> -            info.si_signo = TARGET_SIGSEGV;
> -            info.si_errno = 0;
> -            if (!(env->error_code & 1))
> -                info.si_code = TARGET_SEGV_MAPERR;
> -            else
> -                info.si_code = TARGET_SEGV_ACCERR;
> -            info._sifields._sigfault._addr = env->cr[2];
> -            queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
> +            gen_signal(env, TARGET_SIGSEGV,
> +                       (env->error_code & 1 ?
> +                        TARGET_SEGV_ACCERR : TARGET_SEGV_MAPERR),
> +                       env->cr[2]);
>               break;
>           case EXCP00_DIVZ:
>   #ifndef TARGET_X86_64
>               if (env->eflags & VM_MASK) {
>                   handle_vm86_trap(env, trapnr);
> -            } else
> -#endif
> -            {
> -                /* division by zero */
> -                info.si_signo = TARGET_SIGFPE;
> -                info.si_errno = 0;
> -                info.si_code = TARGET_FPE_INTDIV;
> -                info._sifields._sigfault._addr = env->eip;
> -                queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
> +                break;
>               }
> +#endif
> +            gen_signal(env, TARGET_SIGFPE, TARGET_FPE_INTDIV, env->eip);
>               break;
>           case EXCP01_DB:
>           case EXCP03_INT3:
>   #ifndef TARGET_X86_64
>               if (env->eflags & VM_MASK) {
>                   handle_vm86_trap(env, trapnr);
> -            } else
> +                break;
> +            }
>   #endif
> -            {
> -                info.si_signo = TARGET_SIGTRAP;
> -                info.si_errno = 0;
> -                if (trapnr == EXCP01_DB) {
> -                    info.si_code = TARGET_TRAP_BRKPT;
> -                    info._sifields._sigfault._addr = env->eip;
> -                } else {
> -                    info.si_code = TARGET_SI_KERNEL;
> -                    info._sifields._sigfault._addr = 0;
> -                }
> -                queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
> +            if (trapnr == EXCP01_DB) {
> +                gen_signal(env, TARGET_SIGTRAP, TARGET_TRAP_BRKPT, env->eip);
> +            } else {
> +                gen_signal(env, TARGET_SIGTRAP, TARGET_SI_KERNEL, 0);
>               }
>               break;
>           case EXCP04_INTO:
> @@ -205,31 +190,19 @@ void cpu_loop(CPUX86State *env)
>   #ifndef TARGET_X86_64
>               if (env->eflags & VM_MASK) {
>                   handle_vm86_trap(env, trapnr);
> -            } else
> -#endif
> -            {
> -                info.si_signo = TARGET_SIGSEGV;
> -                info.si_errno = 0;
> -                info.si_code = TARGET_SI_KERNEL;
> -                info._sifields._sigfault._addr = 0;
> -                queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
> +                break;
>               }
> +#endif
> +            gen_signal(env, TARGET_SIGSEGV, TARGET_SI_KERNEL, 0);
>               break;
>           case EXCP06_ILLOP:
> -            info.si_signo = TARGET_SIGILL;
> -            info.si_errno = 0;
> -            info.si_code = TARGET_ILL_ILLOPN;
> -            info._sifields._sigfault._addr = env->eip;
> -            queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
> +            gen_signal(env, TARGET_SIGILL, TARGET_ILL_ILLOPN, env->eip);
>               break;
>           case EXCP_INTERRUPT:
>               /* just indicate that signals should be handled asap */
>               break;
>           case EXCP_DEBUG:
> -            info.si_signo = TARGET_SIGTRAP;
> -            info.si_errno = 0;
> -            info.si_code = TARGET_TRAP_BRKPT;
> -            queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
> +            gen_signal(env, TARGET_SIGTRAP, TARGET_TRAP_BRKPT, 0);
>               break;
>           case EXCP_ATOMIC:
>               cpu_exec_step_atomic(cs);
> 

Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Alex Bennée Jan. 15, 2020, 9:58 a.m. UTC | #2
Richard Henderson <richard.henderson@linaro.org> writes:

> This is a bit tidier than open-coding the 5 lines necessary
> to initialize the target_siginfo_t.  In addition, this zeros
> the remaining bytes of the target_siginfo_t, rather than
> passing in garbage.
>
> Signed-off-by: Richard Henderson <richard.henderson@linaro.org>

Reviewed-by: Alex Bennée <alex.bennee@linaro.org>

> ---
>  linux-user/i386/cpu_loop.c | 93 ++++++++++++++------------------------
>  1 file changed, 33 insertions(+), 60 deletions(-)
>
> diff --git a/linux-user/i386/cpu_loop.c b/linux-user/i386/cpu_loop.c
> index 024b6f4d58..e217cca5ee 100644
> --- a/linux-user/i386/cpu_loop.c
> +++ b/linux-user/i386/cpu_loop.c
> @@ -81,13 +81,23 @@ static void set_idt(int n, unsigned int dpl)
>  }
>  #endif
>  
> +static void gen_signal(CPUX86State *env, int sig, int code, abi_ptr addr)
> +{
> +    target_siginfo_t info = {
> +        .si_signo = sig,
> +        .si_code = code,
> +        ._sifields._sigfault._addr = addr
> +    };
> +
> +    queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
> +}
> +
>  void cpu_loop(CPUX86State *env)
>  {
>      CPUState *cs = env_cpu(env);
>      int trapnr;
>      abi_ulong pc;
>      abi_ulong ret;
> -    target_siginfo_t info;
>  
>      for(;;) {
>          cpu_exec_start(cs);
> @@ -134,70 +144,45 @@ void cpu_loop(CPUX86State *env)
>  #endif
>          case EXCP0B_NOSEG:
>          case EXCP0C_STACK:
> -            info.si_signo = TARGET_SIGBUS;
> -            info.si_errno = 0;
> -            info.si_code = TARGET_SI_KERNEL;
> -            info._sifields._sigfault._addr = 0;
> -            queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
> +            gen_signal(env, TARGET_SIGBUS, TARGET_SI_KERNEL, 0);
>              break;
>          case EXCP0D_GPF:
>              /* XXX: potential problem if ABI32 */
>  #ifndef TARGET_X86_64
>              if (env->eflags & VM_MASK) {
>                  handle_vm86_fault(env);
> -            } else
> -#endif
> -            {
> -                info.si_signo = TARGET_SIGSEGV;
> -                info.si_errno = 0;
> -                info.si_code = TARGET_SI_KERNEL;
> -                info._sifields._sigfault._addr = 0;
> -                queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
> +                break;
>              }
> +#endif
> +            gen_signal(env, TARGET_SIGSEGV, TARGET_SI_KERNEL, 0);
>              break;
>          case EXCP0E_PAGE:
> -            info.si_signo = TARGET_SIGSEGV;
> -            info.si_errno = 0;
> -            if (!(env->error_code & 1))
> -                info.si_code = TARGET_SEGV_MAPERR;
> -            else
> -                info.si_code = TARGET_SEGV_ACCERR;
> -            info._sifields._sigfault._addr = env->cr[2];
> -            queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
> +            gen_signal(env, TARGET_SIGSEGV,
> +                       (env->error_code & 1 ?
> +                        TARGET_SEGV_ACCERR : TARGET_SEGV_MAPERR),
> +                       env->cr[2]);
>              break;
>          case EXCP00_DIVZ:
>  #ifndef TARGET_X86_64
>              if (env->eflags & VM_MASK) {
>                  handle_vm86_trap(env, trapnr);
> -            } else
> -#endif
> -            {
> -                /* division by zero */
> -                info.si_signo = TARGET_SIGFPE;
> -                info.si_errno = 0;
> -                info.si_code = TARGET_FPE_INTDIV;
> -                info._sifields._sigfault._addr = env->eip;
> -                queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
> +                break;
>              }
> +#endif
> +            gen_signal(env, TARGET_SIGFPE, TARGET_FPE_INTDIV, env->eip);
>              break;
>          case EXCP01_DB:
>          case EXCP03_INT3:
>  #ifndef TARGET_X86_64
>              if (env->eflags & VM_MASK) {
>                  handle_vm86_trap(env, trapnr);
> -            } else
> +                break;
> +            }
>  #endif
> -            {
> -                info.si_signo = TARGET_SIGTRAP;
> -                info.si_errno = 0;
> -                if (trapnr == EXCP01_DB) {
> -                    info.si_code = TARGET_TRAP_BRKPT;
> -                    info._sifields._sigfault._addr = env->eip;
> -                } else {
> -                    info.si_code = TARGET_SI_KERNEL;
> -                    info._sifields._sigfault._addr = 0;
> -                }
> -                queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
> +            if (trapnr == EXCP01_DB) {
> +                gen_signal(env, TARGET_SIGTRAP, TARGET_TRAP_BRKPT, env->eip);
> +            } else {
> +                gen_signal(env, TARGET_SIGTRAP, TARGET_SI_KERNEL, 0);
>              }
>              break;
>          case EXCP04_INTO:
> @@ -205,31 +190,19 @@ void cpu_loop(CPUX86State *env)
>  #ifndef TARGET_X86_64
>              if (env->eflags & VM_MASK) {
>                  handle_vm86_trap(env, trapnr);
> -            } else
> -#endif
> -            {
> -                info.si_signo = TARGET_SIGSEGV;
> -                info.si_errno = 0;
> -                info.si_code = TARGET_SI_KERNEL;
> -                info._sifields._sigfault._addr = 0;
> -                queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
> +                break;
>              }
> +#endif
> +            gen_signal(env, TARGET_SIGSEGV, TARGET_SI_KERNEL, 0);
>              break;
>          case EXCP06_ILLOP:
> -            info.si_signo = TARGET_SIGILL;
> -            info.si_errno = 0;
> -            info.si_code = TARGET_ILL_ILLOPN;
> -            info._sifields._sigfault._addr = env->eip;
> -            queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
> +            gen_signal(env, TARGET_SIGILL, TARGET_ILL_ILLOPN, env->eip);
>              break;
>          case EXCP_INTERRUPT:
>              /* just indicate that signals should be handled asap */
>              break;
>          case EXCP_DEBUG:
> -            info.si_signo = TARGET_SIGTRAP;
> -            info.si_errno = 0;
> -            info.si_code = TARGET_TRAP_BRKPT;
> -            queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
> +            gen_signal(env, TARGET_SIGTRAP, TARGET_TRAP_BRKPT, 0);
>              break;
>          case EXCP_ATOMIC:
>              cpu_exec_step_atomic(cs);

Patch
diff mbox series

diff --git a/linux-user/i386/cpu_loop.c b/linux-user/i386/cpu_loop.c
index 024b6f4d58..e217cca5ee 100644
--- a/linux-user/i386/cpu_loop.c
+++ b/linux-user/i386/cpu_loop.c
@@ -81,13 +81,23 @@  static void set_idt(int n, unsigned int dpl)
 }
 #endif
 
+static void gen_signal(CPUX86State *env, int sig, int code, abi_ptr addr)
+{
+    target_siginfo_t info = {
+        .si_signo = sig,
+        .si_code = code,
+        ._sifields._sigfault._addr = addr
+    };
+
+    queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
+}
+
 void cpu_loop(CPUX86State *env)
 {
     CPUState *cs = env_cpu(env);
     int trapnr;
     abi_ulong pc;
     abi_ulong ret;
-    target_siginfo_t info;
 
     for(;;) {
         cpu_exec_start(cs);
@@ -134,70 +144,45 @@  void cpu_loop(CPUX86State *env)
 #endif
         case EXCP0B_NOSEG:
         case EXCP0C_STACK:
-            info.si_signo = TARGET_SIGBUS;
-            info.si_errno = 0;
-            info.si_code = TARGET_SI_KERNEL;
-            info._sifields._sigfault._addr = 0;
-            queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
+            gen_signal(env, TARGET_SIGBUS, TARGET_SI_KERNEL, 0);
             break;
         case EXCP0D_GPF:
             /* XXX: potential problem if ABI32 */
 #ifndef TARGET_X86_64
             if (env->eflags & VM_MASK) {
                 handle_vm86_fault(env);
-            } else
-#endif
-            {
-                info.si_signo = TARGET_SIGSEGV;
-                info.si_errno = 0;
-                info.si_code = TARGET_SI_KERNEL;
-                info._sifields._sigfault._addr = 0;
-                queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
+                break;
             }
+#endif
+            gen_signal(env, TARGET_SIGSEGV, TARGET_SI_KERNEL, 0);
             break;
         case EXCP0E_PAGE:
-            info.si_signo = TARGET_SIGSEGV;
-            info.si_errno = 0;
-            if (!(env->error_code & 1))
-                info.si_code = TARGET_SEGV_MAPERR;
-            else
-                info.si_code = TARGET_SEGV_ACCERR;
-            info._sifields._sigfault._addr = env->cr[2];
-            queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
+            gen_signal(env, TARGET_SIGSEGV,
+                       (env->error_code & 1 ?
+                        TARGET_SEGV_ACCERR : TARGET_SEGV_MAPERR),
+                       env->cr[2]);
             break;
         case EXCP00_DIVZ:
 #ifndef TARGET_X86_64
             if (env->eflags & VM_MASK) {
                 handle_vm86_trap(env, trapnr);
-            } else
-#endif
-            {
-                /* division by zero */
-                info.si_signo = TARGET_SIGFPE;
-                info.si_errno = 0;
-                info.si_code = TARGET_FPE_INTDIV;
-                info._sifields._sigfault._addr = env->eip;
-                queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
+                break;
             }
+#endif
+            gen_signal(env, TARGET_SIGFPE, TARGET_FPE_INTDIV, env->eip);
             break;
         case EXCP01_DB:
         case EXCP03_INT3:
 #ifndef TARGET_X86_64
             if (env->eflags & VM_MASK) {
                 handle_vm86_trap(env, trapnr);
-            } else
+                break;
+            }
 #endif
-            {
-                info.si_signo = TARGET_SIGTRAP;
-                info.si_errno = 0;
-                if (trapnr == EXCP01_DB) {
-                    info.si_code = TARGET_TRAP_BRKPT;
-                    info._sifields._sigfault._addr = env->eip;
-                } else {
-                    info.si_code = TARGET_SI_KERNEL;
-                    info._sifields._sigfault._addr = 0;
-                }
-                queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
+            if (trapnr == EXCP01_DB) {
+                gen_signal(env, TARGET_SIGTRAP, TARGET_TRAP_BRKPT, env->eip);
+            } else {
+                gen_signal(env, TARGET_SIGTRAP, TARGET_SI_KERNEL, 0);
             }
             break;
         case EXCP04_INTO:
@@ -205,31 +190,19 @@  void cpu_loop(CPUX86State *env)
 #ifndef TARGET_X86_64
             if (env->eflags & VM_MASK) {
                 handle_vm86_trap(env, trapnr);
-            } else
-#endif
-            {
-                info.si_signo = TARGET_SIGSEGV;
-                info.si_errno = 0;
-                info.si_code = TARGET_SI_KERNEL;
-                info._sifields._sigfault._addr = 0;
-                queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
+                break;
             }
+#endif
+            gen_signal(env, TARGET_SIGSEGV, TARGET_SI_KERNEL, 0);
             break;
         case EXCP06_ILLOP:
-            info.si_signo = TARGET_SIGILL;
-            info.si_errno = 0;
-            info.si_code = TARGET_ILL_ILLOPN;
-            info._sifields._sigfault._addr = env->eip;
-            queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
+            gen_signal(env, TARGET_SIGILL, TARGET_ILL_ILLOPN, env->eip);
             break;
         case EXCP_INTERRUPT:
             /* just indicate that signals should be handled asap */
             break;
         case EXCP_DEBUG:
-            info.si_signo = TARGET_SIGTRAP;
-            info.si_errno = 0;
-            info.si_code = TARGET_TRAP_BRKPT;
-            queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
+            gen_signal(env, TARGET_SIGTRAP, TARGET_TRAP_BRKPT, 0);
             break;
         case EXCP_ATOMIC:
             cpu_exec_step_atomic(cs);