2 * UNIX signal handling for processor emulation for support of ntoskrnl of libcaptive
3 * Copyright (C) 2002 Jan Kratochvil <project-captive@jankratochvil.net>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; exactly version 2 of June 1991 is required
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #define _GNU_SOURCE 1 /* for sys/ucontext.h for REG_* */
24 #include "captive/signal.h" /* self */
25 #include <glib/gtypes.h>
26 #include <glib/gmessages.h>
28 #include "captive/macros.h"
29 #include <sys/ucontext.h> /* for struct ucontext */
30 #include "captive/mm.h" /* for captive_mmap_map_get() */
32 #include "reactos/internal/mm.h" /* for PAGE_SIZE */
33 #include "captive/ldr.h" /* for captive_ModuleList_patchpoint_find() */
34 #include "captive/ldr_exports.h" /* for struct captive_ModuleList_patchpoint */
38 * _abnormal_termination:
40 * This call can be also accessed as AbnormalTermination() or abnormal_termination().
42 * Returns whether some exception occured (FIXME: in what scope?).
43 * Exception handlers are registered from W32 binary in stack frames stored in "fs:[0x00000000]"
44 * value which gets mapped by libcaptive/ps/signal.c to #fs_KPCR_ExceptionList
47 * libcaptive currently does not raise any exceptions therefore this call always returns value %0.
48 * See RtlpDispatchException().
50 * Returns: non-zero if some exception is now registered and pending.
52 int _abnormal_termination(void)
59 * RtlpDispatchException:
60 * @ExceptionRecord: Ignored by libcaptive.
61 * @Context: Ignored by libcaptive.
63 * Function definition to prevent inclusion of real RtlpDispatchException() implementation.
64 * Currently libcaptive never raises any exception - fix _abnormal_termination() if it changes.
66 * Returns: Never returns. Value %0 if it returns although it is impossible.
68 ULONG RtlpDispatchException(IN PEXCEPTION_RECORD ExceptionRecord,IN PCONTEXT Context)
70 g_assert_not_reached();
71 g_return_val_if_reached(0);
75 /* =='KeGetCurrentKPCR()->ExceptionList';
76 * libcaptive has reduced KPCR (named 'captive_KPCR') which
77 * does not contain this field
79 static greg_t fs_KPCR_ExceptionList=(greg_t)-1;
81 static gboolean instr_mov_greg_to_fsmem(int greg,const void *fsmem,struct ucontext *ucontext)
83 if (fsmem==(const void *)0x00000000) { /* exception stack top pointer */
84 /* moving from %esp is required to pass! */
85 fs_KPCR_ExceptionList=ucontext->uc_mcontext.gregs[greg];
88 g_return_val_if_reached(FALSE);
91 static gboolean instr_mov_fsmem_to_greg(const void *fsmem,int greg,struct ucontext *ucontext)
93 if (fsmem==(const void *)0x00000000) { /* exception stack top pointer */
94 /* moving to %esp is required to pass! */
95 ucontext->uc_mcontext.gregs[greg]=fs_KPCR_ExceptionList;
98 if (fsmem==(const void *)0x00000124) { /* =='KeGetCurrentKPCR()->CurrentThread' */
99 g_return_val_if_fail(greg!=REG_ESP,FALSE);
100 ucontext->uc_mcontext.gregs[greg]=(greg_t)captive_KeGetCurrentKPCR()->CurrentThread;
103 g_return_val_if_reached(FALSE);
106 static int op_regcode_to_greg(guint8 regcode)
109 case 0x00: return REG_EAX;
110 case 0x01: return REG_ECX;
111 case 0x02: return REG_EDX;
112 case 0x03: return REG_EBX;
113 case 0x04: return REG_ESP;
114 case 0x05: return REG_EBP;
115 case 0x06: return REG_ESI;
116 case 0x07: return REG_EDI;
118 g_return_val_if_reached(REG_EAX);
121 static void sigaction_SIGSEGV(int signo,siginfo_t *siginfo,struct ucontext *ucontext)
124 const void *reg_eip_aligned;
126 g_return_if_fail(signo==SIGSEGV);
127 g_return_if_fail(siginfo->si_signo==SIGSEGV);
128 /* siginfo->si_code is weird, seen to have value 128 */
130 reg_eip=(void *)ucontext->uc_mcontext.gregs[REG_EIP];
132 /* 'reg_eip' is not yet PAGE_SIZE-aligned but we need the aligned ptr for captive_mmap_map_get().
133 * glib NOTE: YOU MAY NOT STORE POINTERS IN INTEGERS.
135 reg_eip_aligned=(const void *)(((char *)reg_eip)-(GPOINTER_TO_UINT(reg_eip)&(PAGE_SIZE-1)));
136 g_assert(reg_eip_aligned!=NULL);
137 g_return_if_fail(!(captive_mmap_map_get(reg_eip_aligned)&PROT_EXEC));
139 /* all instruction notation comments are written in AT&T 'instr src,dest' syntax! */
140 if (*reg_eip==0x64) { /* prefix '%fs:' */
142 /* TODO:thread; %fs: is CPU-dependent */
143 if (*reg_eip==0xA3) { /* 'mov %eax,%fs:{reg_eip[1..4]}' */
145 if (instr_mov_greg_to_fsmem(REG_EAX,*(const void **)reg_eip,ucontext)) {
149 g_assert_not_reached();
151 if (*reg_eip==0x89) { /* prefix 0x89 */
153 if ((*reg_eip & ~0x38)==0x05) { /* 'mov %{op_regcode_to_greg(*reg_eip[b3..b5])},%fs:{reg_eip[1..4]} */
155 if (instr_mov_greg_to_fsmem(op_regcode_to_greg(reg_eip[-1]>>3U),*(const void **)reg_eip,ucontext)) {
159 g_assert_not_reached();
161 g_assert_not_reached();
163 if (*reg_eip==0xA1) { /* 'mov %fs:{reg_eip[1..4]},%eax' */
165 if (instr_mov_fsmem_to_greg(*(const void **)reg_eip,REG_EAX,ucontext)) {
169 g_assert_not_reached();
171 if (*reg_eip==0x8B) { /* prefix 0x8B */
173 if ((*reg_eip & ~0x38)==0x05) { /* 'mov %fs:{reg_eip[1..4]},%{op_regcode_to_greg(*reg_eip[b3..b5])} */
175 if (instr_mov_fsmem_to_greg(*(const void **)reg_eip,op_regcode_to_greg(reg_eip[-1]>>3U),ucontext)) {
179 g_assert_not_reached();
181 g_assert_not_reached();
183 g_assert_not_reached();
186 if (*reg_eip==0xF4) { /* hlt; from captive_ModuleList_patch() */
187 struct captive_ModuleList_patchpoint *patchpoint;
188 const gchar *funcname_disabled;
190 g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"%s: reg_eip=%p; 0xF4 hit",G_STRLOC,reg_eip);
192 if ((funcname_disabled=captive_ModuleList_function_disable_find(
193 reg_eip))) { /* ExportAddress */
194 g_error("%s: Reached disabled W32 function: %s",G_STRLOC,funcname_disabled);
195 g_assert_not_reached();
197 patchpoint=captive_ModuleList_patchpoint_find(
198 reg_eip); /* ExportAddress */
199 g_assert(patchpoint!=NULL);
200 if (reg_eip==patchpoint->orig_w32_func) {
201 g_assert(0xF4 /* hlt */ ==*patchpoint->orig_w32_func);
202 g_assert(patchpoint->orig_w32_2ndinstr_byte ==*patchpoint->orig_w32_2ndinstr);
203 if (patchpoint->through_w32_func) {
204 *patchpoint->orig_w32_func=patchpoint->orig_w32_func_byte;
205 *patchpoint->orig_w32_2ndinstr=0xF4; /* hlt */
207 else { /* !patchpoint->through_w32_func */
208 reg_eip=(guint8 *)patchpoint->wrap_wrap_func;
212 if (reg_eip==patchpoint->orig_w32_2ndinstr) {
213 g_assert(patchpoint->orig_w32_func_byte ==*patchpoint->orig_w32_func);
214 g_assert(0xF4 /* hlt */ ==*patchpoint->orig_w32_2ndinstr);
215 g_assert(patchpoint->through_w32_func==TRUE);
216 *patchpoint->orig_w32_func=0xF4; /* hlt */
217 *patchpoint->orig_w32_2ndinstr=patchpoint->orig_w32_2ndinstr_byte;
218 patchpoint->through_w32_func=FALSE;
221 g_assert_not_reached();
224 if (*reg_eip==0xFA) { /* cli */
225 g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"%s: reg_eip=%p; CLI neutralized",G_STRLOC,reg_eip);
226 *reg_eip=0x90; /* nop */
230 g_assert_not_reached();
233 ucontext->uc_mcontext.gregs[REG_EIP]=(greg_t)reg_eip;
238 * captive_signal_init:
240 * Initialize UNIX signal handling to be able to emulate foreign W32
241 * instructions. These instructions must be located inside address
242 * space of foreign W32 binary code which is identified by successful
243 * call to captive_mmap_map_get() returning #PROT_EXEC bit set.
244 * This bit should be set from MmAllocateSection() called from
245 * ntoskrnl/ldr/loader.c/LdrPEProcessModule().
247 * Currently emulated set is the access to %fs register offset %0
248 * where the exception stack top pointer is located.
250 * Returns: %TRUE if successful.
252 gboolean captive_signal_init(void)
255 struct sigaction sigaction_struct;
258 CAPTIVE_MEMZERO(&sigaction_struct); /* this structure may have unpredictable fields */
260 /* Init 'sigaction_struct.sa_mask'. */
261 errint=sigemptyset(&sigaction_struct.sa_mask);
262 g_return_val_if_fail(errint==0,FALSE);
263 errint=sigaddset(&sigaction_struct.sa_mask,SIGSEGV);
264 g_return_val_if_fail(errint==0,FALSE);
266 /* Set the signal sigaction handler. */
267 sigaction_struct.sa_sigaction=(void (*)(int,siginfo_t *,void *))sigaction_SIGSEGV;
268 sigaction_struct.sa_flags=0
269 |SA_SIGINFO; /* Use 'sa_sigaction' (not 'sa_handler') */
270 errint=sigaction(SIGSEGV,
271 &sigaction_struct, /* act */
273 g_return_val_if_fail(errint==0,FALSE);
275 /* Enable SIGSEGV signal (should be default). */
276 errint=sigemptyset(&sigset);
277 g_return_val_if_fail(errint==0,FALSE);
278 errint=sigaddset(&sigset,SIGSEGV);
279 g_return_val_if_fail(errint==0,FALSE);
280 errint=sigprocmask(SIG_UNBLOCK,
283 g_return_val_if_fail(errint==0,FALSE);