+sigaction_SIGSEGV()
authorshort <>
Tue, 5 Nov 2002 11:09:04 +0000 (11:09 +0000)
committershort <>
Tue, 5 Nov 2002 11:09:04 +0000 (11:09 +0000)
 - currently just '%fs:0x0' (exceptionstack top) access handling

src/libcaptive/client/init.c
src/libcaptive/include/captive/Makefile.am
src/libcaptive/include/captive/signal.h [new file with mode: 0644]
src/libcaptive/ps/Makefile.am
src/libcaptive/ps/signal.c [new file with mode: 0644]

index 09a4a53..6cb933f 100644 (file)
@@ -36,6 +36,7 @@
 #include "reactos/internal/ps.h"       /* for PsInitProcessManagment() and PsInitThreadManagment() */
 #include "reactos/ddk/iofuncs.h"       /* for IoCreateFile() */
 #include "captive/storage.h"
+#include "captive/signal.h"    /* for captive_signal_init() */
 
 
 /* Are we initialized? */
@@ -127,7 +128,10 @@ IO_STATUS_BLOCK root_IoStatusBlock;
                        &DriverObject,  /* DriverEntry_DriverObject */
                        captive_utf8_to_UnicodeString_alloca("\\captive\\filesystem")); /* DriverEntry_RegistryPath */
        g_return_val_if_fail(NT_SUCCESS(err),FALSE);
-       
+
+       /* Begin possible handling of foreign W32 binary code here */
+       captive_signal_init();
+
        /* Do not open "\Cdfs"(anything) as it is just the filesystem implementation.
         * ntoskrnl/io/fs.c/IoMountVolume() will map
         *      FILE_DEVICE_CD_ROM -> FILE_DEVICE_CD_ROM_FILE_SYSTEM
index 7afbf55..6648e51 100644 (file)
@@ -28,6 +28,7 @@ pkginclude_HEADERS+= \
                macros.h \
                mm.h \
                ps_reactos.h \
+               signal.h \
                storage.h \
                unicode.h \
                unicode_reactos.h
diff --git a/src/libcaptive/include/captive/signal.h b/src/libcaptive/include/captive/signal.h
new file mode 100644 (file)
index 0000000..48d9199
--- /dev/null
@@ -0,0 +1,34 @@
+/* $Id$
+ * Include file for UNIX signal handling for processor emulation for support of ntoskrnl of libcaptive
+ * Copyright (C) 2002 Jan Kratochvil <project-captive@jankratochvil.net>
+ * 
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; exactly version 2 of June 1991 is required
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+
+#ifndef _CAPTIVE_SIGNAL_H
+#define _CAPTIVE_SIGNAL_H 1
+
+
+#include <glib/gtypes.h>
+
+
+G_BEGIN_DECLS
+
+gboolean captive_signal_init(void);
+
+G_END_DECLS
+
+
+#endif /* _CAPTIVE_SIGNAL_H */
index 4fa1e9b..16d43cf 100644 (file)
@@ -21,4 +21,5 @@ include $(top_srcdir)/src/libcaptive/Makefile-libcaptive.am
 
 noinst_LTLIBRARIES=libps.la
 libps_la_SOURCES= \
-               ps_reactos.c
+               ps_reactos.c \
+               signal.c
diff --git a/src/libcaptive/ps/signal.c b/src/libcaptive/ps/signal.c
new file mode 100644 (file)
index 0000000..7b841b9
--- /dev/null
@@ -0,0 +1,189 @@
+/* $Id$
+ * UNIX signal handling for processor emulation for support of ntoskrnl of libcaptive
+ * Copyright (C) 2002 Jan Kratochvil <project-captive@jankratochvil.net>
+ * 
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; exactly version 2 of June 1991 is required
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+
+#define _GNU_SOURCE 1  /* for sys/ucontext.h for REG_* */
+
+#include "config.h"
+
+#include "captive/signal.h"    /* self */
+#include <glib/gtypes.h>
+#include <glib/gmessages.h>
+#include <signal.h>
+#include "captive/macros.h"
+#include <sys/ucontext.h>      /* for struct ucontext */
+#include "captive/mm.h"        /* for captive_mmap_map_get() */
+#include <sys/mman.h>
+#include "reactos/internal/mm.h"  /* for PAGE_SIZE */
+
+
+static greg_t val_exceptionstack_top;
+
+static gboolean instr_mov_greg_to_fsmem(int greg,const void *fsmem,struct ucontext *ucontext)
+{
+       if (fsmem==0x00000000) {        /* exception stack top pointer */
+               /* moving from %esp is required to pass! */
+               val_exceptionstack_top=ucontext->uc_mcontext.gregs[greg];
+               return TRUE;
+               }
+       g_return_val_if_reached(FALSE);
+}
+
+static gboolean instr_mov_fsmem_to_greg(const void *fsmem,int greg,struct ucontext *ucontext)
+{
+       if (fsmem==0x00000000) {        /* exception stack top pointer */
+               /* moving to %esp is required to pass! */
+               ucontext->uc_mcontext.gregs[greg]=val_exceptionstack_top;
+               return TRUE;
+               }
+       g_return_val_if_reached(FALSE);
+}
+
+static int op_regcode_to_greg(guint8 regcode)
+{
+       switch (regcode) {
+               case 0x00: return REG_EAX;
+               case 0x01: return REG_ECX;
+               case 0x02: return REG_EDX;
+               case 0x03: return REG_EBX;
+               case 0x04: return REG_ESP;
+               case 0x05: return REG_EBP;
+               case 0x06: return REG_ESI;
+               case 0x07: return REG_EDI;
+               }
+       g_return_val_if_reached(REG_EAX);
+}
+
+static void sigaction_SIGSEGV(int signo,siginfo_t *siginfo,struct ucontext *ucontext)
+{
+const guint8 *reg_eip;
+const void *reg_eip_aligned;
+
+       g_return_if_fail(signo==SIGSEGV);
+       g_return_if_fail(siginfo->si_signo==SIGSEGV);
+       /* siginfo->si_code is weird, seen to have value 128 */
+
+       reg_eip=(void *)ucontext->uc_mcontext.gregs[REG_EIP];
+
+       /* 'reg_eip' is not yet PAGE_SIZE-aligned but we need the aligned ptr for captive_mmap_map_get().
+        * glib NOTE: YOU MAY NOT STORE POINTERS IN INTEGERS.
+        */
+       reg_eip_aligned=(const void *)(((char *)reg_eip)-(GPOINTER_TO_UINT(reg_eip)&(PAGE_SIZE-1)));
+       g_assert(reg_eip_aligned!=NULL);
+       g_return_if_fail(!(captive_mmap_map_get(reg_eip_aligned)&PROT_EXEC));
+       
+       /* all instruction notation comments are written in AT&T 'instr src,dest' syntax! */
+       if (*reg_eip==0x64) {   /* prefix '%fs:' */
+               reg_eip++;
+               if (*reg_eip==0xA3) {   /* 'mov %eax,%fs:{reg_eip[1..4]}' */
+                       reg_eip++;
+                       if (instr_mov_greg_to_fsmem(REG_EAX,*(const void **)reg_eip,ucontext)) {
+                               reg_eip+=4;
+                               goto ok;
+                               }
+                       g_assert_not_reached();
+                       }
+               if (*reg_eip==0x89) {   /* prefix 0x89 */
+                       reg_eip++;
+                       if ((*reg_eip & ~0x38)==0x05)   { /* 'mov %{op_regcode_to_greg(*reg_eip[b3..b5])},%fs:{reg_eip[1..4]} */
+                               reg_eip++;
+                               if (instr_mov_greg_to_fsmem(op_regcode_to_greg(reg_eip[-1]>>3U),*(const void **)reg_eip,ucontext)) {
+                                       reg_eip+=4;
+                                       goto ok;
+                                       }
+                               g_assert_not_reached();
+                               }
+                       g_assert_not_reached();
+                       }
+               if (*reg_eip==0xA1) {   /* 'mov %fs:{reg_eip[1..4]},%eax' */
+                       reg_eip++;
+                       if (instr_mov_fsmem_to_greg(*(const void **)reg_eip,REG_EAX,ucontext)) {
+                               reg_eip+=4;
+                               goto ok;
+                               }
+                       g_assert_not_reached();
+                       }
+               if (*reg_eip==0x8B) {   /* prefix 0x8B */
+                       reg_eip++;
+                       if ((*reg_eip & ~0x38)==0x05) { /* 'mov %fs:{reg_eip[1..4]},%{op_regcode_to_greg(*reg_eip[b3..b5])} */
+                               reg_eip++;
+                               if (instr_mov_fsmem_to_greg(*(const void **)reg_eip,op_regcode_to_greg(reg_eip[-1]>>3U),ucontext)) {
+                                       reg_eip+=4;
+                                       goto ok;
+                                       }
+                               g_assert_not_reached();
+                               }
+                       g_assert_not_reached();
+                       }
+               g_assert_not_reached();
+               }
+       g_assert_not_reached();
+
+ok:
+       ucontext->uc_mcontext.gregs[REG_EIP]=(greg_t)reg_eip;
+       /* success */
+}
+
+/**
+ * captive_signal_init:
+ *
+ * Initialize UNIX signal handling to be able to emulate foreign W32
+ * instructions. These instructions must be located inside address
+ * space of foreign W32 binary code which is identified by successful
+ * call to captive_mmap_map_get() returning #PROT_EXEC bit set.
+ * This bit should be set from MmAllocateSection() called from
+ * ntoskrnl/ldr/loader.c/LdrPEProcessModule().
+ *
+ * Currently emulated set is the access to %fs register offset %0
+ * where the exception stack top pointer is located.
+ */
+gboolean captive_signal_init(void)
+{
+gint errint;
+struct sigaction sigaction_struct;
+sigset_t sigset;
+
+       CAPTIVE_MEMZERO(&sigaction_struct);     /* this structure may have unpredictable fields */
+
+       /* Init 'sigaction_struct.sa_mask'. */
+       errint=sigemptyset(&sigaction_struct.sa_mask);
+       g_return_val_if_fail(errint==0,FALSE);
+       errint=sigaddset(&sigaction_struct.sa_mask,SIGSEGV);
+       g_return_val_if_fail(errint==0,FALSE);
+
+       /* Set the signal sigaction handler. */
+       sigaction_struct.sa_sigaction=(void (*)(int,siginfo_t *,void *))sigaction_SIGSEGV;
+       sigaction_struct.sa_flags=0
+                       |SA_SIGINFO;    /* Use 'sa_sigaction' (not 'sa_handler') */
+       errint=sigaction(SIGSEGV,
+                       &sigaction_struct,      /* act */
+                       NULL);  /* oldact */
+       g_return_val_if_fail(errint==0,FALSE);
+
+       /* Enable SIGSEGV signal (should be default). */
+       errint=sigemptyset(&sigset);
+       g_return_val_if_fail(errint==0,FALSE);
+       errint=sigaddset(&sigset,SIGSEGV);
+       g_return_val_if_fail(errint==0,FALSE);
+       errint=sigprocmask(SIG_UNBLOCK,
+                       &sigset,        /* set */
+                       NULL);  /* oldset */
+       g_return_val_if_fail(errint==0,FALSE);
+
+       return TRUE;
+}