_abnormal_termination(): Disabled here as it is now "pass"ed to ntoskrnl.
[captive.git] / src / libcaptive / ps / signal.c
1 /* $Id$
2  * UNIX signal handling for processor emulation for support of ntoskrnl of libcaptive
3  * Copyright (C) 2002 Jan Kratochvil <project-captive@jankratochvil.net>
4  * 
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
8  * 
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.
13  * 
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
17  */
18
19
20 #define _GNU_SOURCE 1   /* for sys/ucontext.h for REG_* */
21
22 #include "config.h"
23
24 #include "captive/signal.h"     /* self */
25 #include <glib/gtypes.h>
26 #include <glib/gmessages.h>
27 #include <signal.h>
28 #include "captive/macros.h"
29 #include <sys/ucontext.h>       /* for struct ucontext */
30 #include "captive/mm.h" /* for captive_mmap_map_get() */
31 #include <sys/mman.h>
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 */
35
36
37 #if 0   /* Currently implemented for real by "pass" to native ntoskrnl. */
38 /**
39  * _abnormal_termination:
40  *
41  * This call can be also accessed as AbnormalTermination() or abnormal_termination().
42  *
43  * Returns whether some exception occured (FIXME: in what scope?).
44  * Exception handlers are registered from W32 binary in stack frames stored in "fs:[0x00000000]"
45  * value which gets mapped by libcaptive/ps/signal.c to #fs_KPCR_ExceptionList
46  * variable.
47  *
48  * libcaptive currently does not raise any exceptions therefore this call always returns value %0.
49  * See RtlpDispatchException().
50  *
51  * Returns: non-zero if some exception is now registered and pending.
52  */
53 int _abnormal_termination(void)
54 {
55         return 0;
56 }
57 #endif
58
59
60 #if 0
61
62 /**
63  * RtlpDispatchException:
64  * @ExceptionRecord: Ignored by libcaptive.
65  * @Context: Ignored by libcaptive.
66  *
67  * Function definition to prevent inclusion of real RtlpDispatchException() implementation.
68  * Currently libcaptive never raises any exception - fix _abnormal_termination() if it changes.
69  *
70  * Returns: Never returns. Value %0 if it returns although it is impossible.
71  */
72 ULONG RtlpDispatchException(IN PEXCEPTION_RECORD ExceptionRecord,IN PCONTEXT Context)
73 {
74         g_assert_not_reached();
75         g_return_val_if_reached(0);
76 }
77
78 #endif
79
80
81 /* =='KeGetCurrentKPCR()->ExceptionList';
82  * libcaptive has reduced KPCR (named 'captive_KPCR') which
83  * does not contain this field
84  */
85 greg_t fs_KPCR_ExceptionList=(greg_t)-1;
86
87 /* FIXME */
88 static greg_t fs_KPCR_Unknown638=0;
89
90
91 static gboolean instr_mov_greg_to_fsmem(int greg,const void *fsmem,struct ucontext *ucontext)
92 {
93         if (fsmem==(const void *)0x00000000) {  /* exception stack top pointer */
94                 /* moving from %esp is required to pass! */
95                 fs_KPCR_ExceptionList=ucontext->uc_mcontext.gregs[greg];
96                 return TRUE;
97                 }
98         g_return_val_if_reached(FALSE);
99 }
100
101 static gboolean instr_mov_immed_to_fsmem(greg_t immed,const void *fsmem,struct ucontext *ucontext)
102 {
103         if (fsmem==(const void *)0x00000000) {  /* exception stack top pointer */
104                 fs_KPCR_ExceptionList=immed;
105                 return TRUE;
106                 }
107         g_return_val_if_reached(FALSE);
108 }
109
110 static gboolean instr_mov_fsmem_to_greg(const void *fsmem,int greg,struct ucontext *ucontext)
111 {
112         if (fsmem==(const void *)0x00000000) {  /* exception stack top pointer */
113                 /* moving to %esp is required to pass! */
114                 ucontext->uc_mcontext.gregs[greg]=fs_KPCR_ExceptionList;
115                 return TRUE;
116                 }
117         if (fsmem==(const void *)0x00000051) {  /* =='KeGetCurrentKPCR()->Number' */
118                 g_return_val_if_fail(greg!=REG_ESP,FALSE);
119                 ucontext->uc_mcontext.gregs[greg]=(greg_t)0;    /* ==libcaptive version of KeGetCurrentProcessorNumber() */
120                 return TRUE;
121                 }
122         if (fsmem==(const void *)0x00000124) {  /* =='KeGetCurrentKPCR()->CurrentThread' */
123                 g_return_val_if_fail(greg!=REG_ESP,FALSE);
124                 ucontext->uc_mcontext.gregs[greg]=(greg_t)captive_KeGetCurrentKPCR()->CurrentThread;
125                 return TRUE;
126                 }
127         g_return_val_if_reached(FALSE);
128 }
129
130 static gboolean instr_push_fsmem(const void *fsmem,struct ucontext *ucontext)
131 {
132         if (fsmem==(const void *)0x00000000) {  /* exception stack top pointer */
133                 ucontext->uc_mcontext.gregs[REG_ESP]-=4;
134                 *(greg_t *)ucontext->uc_mcontext.gregs[REG_ESP]=fs_KPCR_ExceptionList;
135                 return TRUE;
136                 }
137         g_return_val_if_reached(FALSE);
138 }
139
140 static gboolean instr_pop_fsmem(const void *fsmem,struct ucontext *ucontext)
141 {
142         if (fsmem==(const void *)0x00000000) {  /* exception stack top pointer */
143                 fs_KPCR_ExceptionList=*(greg_t *)ucontext->uc_mcontext.gregs[REG_ESP];
144                 ucontext->uc_mcontext.gregs[REG_ESP]+=4;
145                 return TRUE;
146                 }
147         g_return_val_if_reached(FALSE);
148 }
149
150 static gboolean instr_incl_fsmem(const void *fsmem,struct ucontext *ucontext)
151 {
152         if (fsmem==(const void *)0x00000638) {  /* Unknown638 */
153                 fs_KPCR_Unknown638++;
154                 return TRUE;
155                 }
156         g_return_val_if_reached(FALSE);
157 }
158
159 static int op_regcode_to_greg(guint8 regcode)
160 {
161         switch (regcode) {
162                 case 0x00: return REG_EAX;
163                 case 0x01: return REG_ECX;
164                 case 0x02: return REG_EDX;
165                 case 0x03: return REG_EBX;
166                 case 0x04: return REG_ESP;
167                 case 0x05: return REG_EBP;
168                 case 0x06: return REG_ESI;
169                 case 0x07: return REG_EDI;
170                 }
171         g_return_val_if_reached(REG_EAX);
172 }
173
174 static void sigaction_SIGSEGV(int signo,siginfo_t *siginfo,struct ucontext *ucontext)
175 {
176 guint8 *reg_eip;
177 const void *reg_eip_aligned;
178
179         g_return_if_fail(signo==SIGSEGV);
180         g_return_if_fail(siginfo->si_signo==SIGSEGV);
181         /* siginfo->si_code is weird, seen to have value 128 */
182
183         reg_eip=(void *)ucontext->uc_mcontext.gregs[REG_EIP];
184
185         /* 'reg_eip' is not yet PAGE_SIZE-aligned but we need the aligned ptr for captive_mmap_map_get().
186          * glib NOTE: YOU MAY NOT STORE POINTERS IN INTEGERS.
187          */
188         reg_eip_aligned=(const void *)(((char *)reg_eip)-(GPOINTER_TO_UINT(reg_eip)&(PAGE_SIZE-1)));
189         g_assert(reg_eip_aligned!=NULL);
190         g_return_if_fail(!(captive_mmap_map_get(reg_eip_aligned)&PROT_EXEC));
191         
192         /* all instruction notation comments are written in AT&T 'instr src,dest' syntax! */
193         if (*reg_eip==0x64) {   /* prefix '%fs:' */
194                 reg_eip++;
195                 /* TODO:thread; %fs: is CPU-dependent */
196                 if (*reg_eip==0x0F) {   /* two-byte opcode */
197                         reg_eip++;
198                         if (*reg_eip==0xB6) {   /* ??? */
199                                 reg_eip++;
200                                 if (*reg_eip==0x05) {   /* movzbl %fs:{reg_eip[1..4]},%eax */
201                                         reg_eip++;
202                                         if (instr_mov_fsmem_to_greg(*(const void **)reg_eip,REG_EAX,ucontext)) {
203                                                 reg_eip+=4;
204                                                 goto ok;
205                                                 }
206                                         g_assert_not_reached();
207                                         }
208                                 g_assert_not_reached();
209                                 }
210                         g_assert_not_reached();
211                         }
212                 if (*reg_eip==0xA3) {   /* 'mov %eax,%fs:{reg_eip[1..4]}' */
213                         reg_eip++;
214                         if (instr_mov_greg_to_fsmem(REG_EAX,*(const void **)reg_eip,ucontext)) {
215                                 reg_eip+=4;
216                                 goto ok;
217                                 }
218                         g_assert_not_reached();
219                         }
220                 if (*reg_eip==0x89) {   /* prefix 0x89 */
221                         reg_eip++;
222                         if ((*reg_eip & ~0x38)==0x05)   { /* 'mov %{op_regcode_to_greg(*reg_eip[b3..b5])},%fs:{reg_eip[1..4]} */
223                                 reg_eip++;
224                                 if (instr_mov_greg_to_fsmem(op_regcode_to_greg(reg_eip[-1]>>3U),*(const void **)reg_eip,ucontext)) {
225                                         reg_eip+=4;
226                                         goto ok;
227                                         }
228                                 g_assert_not_reached();
229                                 }
230                         g_assert_not_reached();
231                         }
232                 if (*reg_eip==0xA1) {   /* 'mov %fs:{reg_eip[1..4]},%eax' */
233                         reg_eip++;
234                         if (instr_mov_fsmem_to_greg(*(const void **)reg_eip,REG_EAX,ucontext)) {
235                                 reg_eip+=4;
236                                 goto ok;
237                                 }
238                         g_assert_not_reached();
239                         }
240                 if (*reg_eip==0x8B) {   /* prefix 0x8B */
241                         reg_eip++;
242                         if ((*reg_eip & ~0x38)==0x05) { /* 'mov %fs:{reg_eip[1..4]},%{op_regcode_to_greg(*reg_eip[b3..b5])}' */
243                                 reg_eip++;
244                                 if (instr_mov_fsmem_to_greg(*(const void **)reg_eip,op_regcode_to_greg(reg_eip[-1]>>3U),ucontext)) {
245                                         reg_eip+=4;
246                                         goto ok;
247                                         }
248                                 g_assert_not_reached();
249                                 }
250                         g_assert_not_reached();
251                         }
252                 if (*reg_eip==0xFF) {   /* prefix 0xFF */
253                         reg_eip++;
254                         if (*reg_eip==0x05) {   /* 'incl %fs:{reg_eip[1..4]}' */
255                                 reg_eip++;
256                                 if (instr_incl_fsmem(*(const void **)reg_eip,ucontext)) {
257                                         reg_eip+=4;
258                                         goto ok;
259                                         }
260                                 g_assert_not_reached();
261                                 }
262                         if (*reg_eip==0x35) {   /* 'pushl %fs:{reg_eip[1..4]}' */
263                                 reg_eip++;
264                                 if (instr_push_fsmem(*(const void **)reg_eip,ucontext)) {
265                                         reg_eip+=4;
266                                         goto ok;
267                                         }
268                                 g_assert_not_reached();
269                                 }
270                         g_assert_not_reached();
271                         }
272                 if (*reg_eip==0x8F) {   /* prefix 0x0F */
273                         reg_eip++;
274                         if (*reg_eip==0x05) {   /* 'popl %fs:{reg_eip[1..4]}' */
275                                 reg_eip++;
276                                 if (instr_pop_fsmem(*(const void **)reg_eip,ucontext)) {
277                                         reg_eip+=4;
278                                         goto ok;
279                                         }
280                                 g_assert_not_reached();
281                                 }
282                         g_assert_not_reached();
283                         }
284                 if (*reg_eip==0xC7) {   /* prefix 0xC7 */
285                         reg_eip++;
286                         if (*reg_eip==0x05) {   /* 'movl ${reg_eip[5..8]},%fs:{reg_eip[1..4]}' */
287                                 reg_eip++;
288                                 if (instr_mov_immed_to_fsmem(((greg_t *)reg_eip)[1],*(const void **)reg_eip,ucontext)) {
289                                         reg_eip+=4+4;
290                                         goto ok;
291                                         }
292                                 g_assert_not_reached();
293                                 }
294                         g_assert_not_reached();
295                         }
296                 g_assert_not_reached();
297                 }
298
299         /* all instruction notation comments are written in AT&T 'instr src,dest' syntax! */
300         if (*reg_eip==0x66) {   /* prefix '%fs:' */
301                 reg_eip++;
302                 /* TODO:thread; %fs: is CPU-dependent */
303                 if (*reg_eip==0x8E) {   /* two-byte opcode */
304                         reg_eip++;
305                         if (*reg_eip==0xE3) {   /* 'mov %bx,%fs' */
306                                 reg_eip++;
307                                 g_assert(0x30==(0xFFFF&ucontext->uc_mcontext.gregs[REG_EBX]));
308                                 /* 'reload' of %fs can be ignored */
309                                 goto ok;
310                                 }
311                         g_assert_not_reached();
312                         }
313                 g_assert_not_reached();
314                 }
315
316         if (*reg_eip==0xF4) {   /* hlt; from captive_ModuleList_patch() */
317 struct captive_ModuleList_patchpoint *patchpoint;
318 const gchar *funcname_disabled;
319
320                 g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"%s: reg_eip=%p; 0xF4 hit",G_STRLOC,reg_eip);
321
322                 if ((funcname_disabled=captive_ModuleList_function_disable_find(
323                                                 reg_eip)))      { /* ExportAddress */
324                         g_error("%s: Reached disabled W32 function: %s",G_STRLOC,funcname_disabled);
325                         g_assert_not_reached();
326                         }
327                 patchpoint=captive_ModuleList_patchpoint_find(
328                                 reg_eip);       /* ExportAddress */
329                 g_assert(patchpoint!=NULL);
330                 if (reg_eip==patchpoint->orig_w32_func) {
331                         g_assert(0xF4 /* hlt */ ==*patchpoint->orig_w32_func);
332                         g_assert(patchpoint->orig_w32_2ndinstr_byte ==*patchpoint->orig_w32_2ndinstr);
333                         if (patchpoint->through_w32_func) {
334                                 *patchpoint->orig_w32_func=patchpoint->orig_w32_func_byte;
335                                 *patchpoint->orig_w32_2ndinstr=0xF4;    /* hlt */
336                                 }
337                         else {  /* !patchpoint->through_w32_func */
338                                 reg_eip=(guint8 *)patchpoint->wrap_wrap_func;
339                                 }
340                         goto ok;
341                         }
342                 if (reg_eip==patchpoint->orig_w32_2ndinstr) {
343                         g_assert(patchpoint->orig_w32_func_byte ==*patchpoint->orig_w32_func);
344                         g_assert(0xF4 /* hlt */ ==*patchpoint->orig_w32_2ndinstr);
345                         g_assert(patchpoint->through_w32_func==TRUE);
346                         *patchpoint->orig_w32_func=0xF4;        /* hlt */
347                         *patchpoint->orig_w32_2ndinstr=patchpoint->orig_w32_2ndinstr_byte;
348                         patchpoint->through_w32_func=FALSE;
349                         goto ok;
350                         }
351                 g_assert_not_reached();
352                 }
353
354         if (*reg_eip==0xFA) {   /* cli */
355                 g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"%s: reg_eip=%p; CLI neutralized",G_STRLOC,reg_eip);
356                 *reg_eip=0x90;  /* nop */
357                 goto ok;
358                 }
359
360         if (*reg_eip==0xFB) {   /* sti */
361                 g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"%s: reg_eip=%p; STI neutralized",G_STRLOC,reg_eip);
362                 *reg_eip=0x90;  /* nop */
363                 goto ok;
364                 }
365
366         g_assert_not_reached();
367
368 ok:
369         ucontext->uc_mcontext.gregs[REG_EIP]=(greg_t)reg_eip;
370         /* success */
371 }
372
373 /**
374  * captive_signal_init:
375  *
376  * Initialize UNIX signal handling to be able to emulate foreign W32
377  * instructions. These instructions must be located inside address
378  * space of foreign W32 binary code which is identified by successful
379  * call to captive_mmap_map_get() returning #PROT_EXEC bit set.
380  * This bit should be set from MmAllocateSection() called from
381  * ntoskrnl/ldr/loader.c/LdrPEProcessModule().
382  *
383  * Currently emulated set is the access to %fs register offset %0
384  * where the exception stack top pointer is located.
385  *
386  * Returns: %TRUE if successful.
387  */
388 gboolean captive_signal_init(void)
389 {
390 gint errint;
391 struct sigaction sigaction_struct;
392 sigset_t sigset;
393
394         CAPTIVE_MEMZERO(&sigaction_struct);     /* this structure may have unpredictable fields */
395
396         /* Init 'sigaction_struct.sa_mask'. */
397         errint=sigemptyset(&sigaction_struct.sa_mask);
398         g_return_val_if_fail(errint==0,FALSE);
399         errint=sigaddset(&sigaction_struct.sa_mask,SIGSEGV);
400         g_return_val_if_fail(errint==0,FALSE);
401
402         /* Set the signal sigaction handler. */
403         sigaction_struct.sa_sigaction=(void (*)(int,siginfo_t *,void *))sigaction_SIGSEGV;
404         sigaction_struct.sa_flags=0
405                         |SA_SIGINFO;    /* Use 'sa_sigaction' (not 'sa_handler') */
406         errint=sigaction(SIGSEGV,
407                         &sigaction_struct,      /* act */
408                         NULL);  /* oldact */
409         g_return_val_if_fail(errint==0,FALSE);
410
411         /* Enable SIGSEGV signal (should be default). */
412         errint=sigemptyset(&sigset);
413         g_return_val_if_fail(errint==0,FALSE);
414         errint=sigaddset(&sigset,SIGSEGV);
415         g_return_val_if_fail(errint==0,FALSE);
416         errint=sigprocmask(SIG_UNBLOCK,
417                         &sigset,        /* set */
418                         NULL);  /* oldset */
419         g_return_val_if_fail(errint==0,FALSE);
420
421         return TRUE;
422 }