include: Specify calling convention explicitly in idl files where needed.
[wine/hramrach.git] / dlls / ntdll / signal_i386.c
blobc08693e69cf9f86c717eb34eaca31fb4b914f0b3
1 /*
2 * i386 signal handling routines
4 * Copyright 1999 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #ifdef __i386__
23 #include "config.h"
24 #include "wine/port.h"
26 #include <errno.h>
27 #include <signal.h>
28 #include <stdlib.h>
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include <sys/types.h>
32 #ifdef HAVE_UNISTD_H
33 # include <unistd.h>
34 #endif
36 #ifdef HAVE_SYS_PARAM_H
37 # include <sys/param.h>
38 #endif
39 #ifdef HAVE_SYSCALL_H
40 # include <syscall.h>
41 #else
42 # ifdef HAVE_SYS_SYSCALL_H
43 # include <sys/syscall.h>
44 # endif
45 #endif
47 #ifdef HAVE_SYS_VM86_H
48 # include <sys/vm86.h>
49 #endif
51 #ifdef HAVE_SYS_SIGNAL_H
52 # include <sys/signal.h>
53 #endif
54 #ifdef HAVE_SYS_SYSCTL_H
55 # include <sys/sysctl.h>
56 #endif
58 #include "ntstatus.h"
59 #define WIN32_NO_STATUS
60 #include "windef.h"
61 #include "wine/library.h"
62 #include "ntdll_misc.h"
63 #include "wine/exception.h"
64 #include "wine/debug.h"
66 #ifdef HAVE_VALGRIND_MEMCHECK_H
67 #include <valgrind/memcheck.h>
68 #endif
70 #undef ERR /* Solaris needs to define this */
72 /* not defined for x86, so copy the x86_64 definition */
73 typedef struct DECLSPEC_ALIGN(16) _M128A
75 ULONGLONG Low;
76 LONGLONG High;
77 } M128A;
79 typedef struct
81 WORD ControlWord;
82 WORD StatusWord;
83 BYTE TagWord;
84 BYTE Reserved1;
85 WORD ErrorOpcode;
86 DWORD ErrorOffset;
87 WORD ErrorSelector;
88 WORD Reserved2;
89 DWORD DataOffset;
90 WORD DataSelector;
91 WORD Reserved3;
92 DWORD MxCsr;
93 DWORD MxCsr_Mask;
94 M128A FloatRegisters[8];
95 M128A XmmRegisters[16];
96 BYTE Reserved4[96];
97 } XMM_SAVE_AREA32;
99 /***********************************************************************
100 * signal context platform-specific definitions
103 #ifdef linux
105 typedef ucontext_t SIGCONTEXT;
107 #define EAX_sig(context) ((context)->uc_mcontext.gregs[REG_EAX])
108 #define EBX_sig(context) ((context)->uc_mcontext.gregs[REG_EBX])
109 #define ECX_sig(context) ((context)->uc_mcontext.gregs[REG_ECX])
110 #define EDX_sig(context) ((context)->uc_mcontext.gregs[REG_EDX])
111 #define ESI_sig(context) ((context)->uc_mcontext.gregs[REG_ESI])
112 #define EDI_sig(context) ((context)->uc_mcontext.gregs[REG_EDI])
113 #define EBP_sig(context) ((context)->uc_mcontext.gregs[REG_EBP])
114 #define ESP_sig(context) ((context)->uc_mcontext.gregs[REG_ESP])
116 #define CS_sig(context) ((context)->uc_mcontext.gregs[REG_CS])
117 #define DS_sig(context) ((context)->uc_mcontext.gregs[REG_DS])
118 #define ES_sig(context) ((context)->uc_mcontext.gregs[REG_ES])
119 #define SS_sig(context) ((context)->uc_mcontext.gregs[REG_SS])
120 #define FS_sig(context) ((context)->uc_mcontext.gregs[REG_FS])
121 #define GS_sig(context) ((context)->uc_mcontext.gregs[REG_GS])
123 #define EFL_sig(context) ((context)->uc_mcontext.gregs[REG_EFL])
124 #define EIP_sig(context) ((context)->uc_mcontext.gregs[REG_EIP])
125 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
126 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
128 #define FPU_sig(context) ((FLOATING_SAVE_AREA*)((context)->uc_mcontext.fpregs))
129 #define FPUX_sig(context) (FPU_sig(context) && !((context)->uc_mcontext.fpregs->status >> 16) ? (XMM_SAVE_AREA32 *)(FPU_sig(context) + 1) : NULL)
131 #define VM86_EAX 0 /* the %eax value while vm86_enter is executing */
132 #define VIF_FLAG 0x00080000
133 #define VIP_FLAG 0x00100000
135 int vm86_enter( void **vm86_ptr );
136 void vm86_return(void);
137 void vm86_return_end(void);
138 __ASM_GLOBAL_FUNC(vm86_enter,
139 "pushl %ebp\n\t"
140 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
141 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
142 "movl %esp,%ebp\n\t"
143 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
144 "pushl %ebx\n\t"
145 __ASM_CFI(".cfi_rel_offset %ebx,-4\n\t")
146 "movl $166,%eax\n\t" /*SYS_vm86*/
147 "movl 8(%ebp),%ecx\n\t" /* vm86_ptr */
148 "movl (%ecx),%ecx\n\t"
149 "movl $1,%ebx\n\t" /*VM86_ENTER*/
150 "pushl %ecx\n\t" /* put vm86plus_struct ptr somewhere we can find it */
151 "pushl %fs\n\t"
152 "pushl %gs\n\t"
153 "int $0x80\n"
154 ".globl " __ASM_NAME("vm86_return") "\n\t"
155 __ASM_FUNC("vm86_return") "\n"
156 __ASM_NAME("vm86_return") ":\n\t"
157 "popl %gs\n\t"
158 "popl %fs\n\t"
159 "popl %ecx\n\t"
160 "popl %ebx\n\t"
161 __ASM_CFI(".cfi_same_value %ebx\n\t")
162 "popl %ebp\n\t"
163 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
164 __ASM_CFI(".cfi_same_value %ebp\n\t")
165 "testl %eax,%eax\n\t"
166 "jl 0f\n\t"
167 "cmpb $0,%al\n\t" /* VM86_SIGNAL */
168 "je " __ASM_NAME("vm86_enter") "\n\t"
169 "0:\n\t"
170 "movl 4(%esp),%ecx\n\t" /* vm86_ptr */
171 "movl $0,(%ecx)\n\t"
172 ".globl " __ASM_NAME("vm86_return_end") "\n\t"
173 __ASM_FUNC("vm86_return_end") "\n"
174 __ASM_NAME("vm86_return_end") ":\n\t"
175 "ret" )
177 #ifdef HAVE_SYS_VM86_H
178 # define __HAVE_VM86
179 #endif
181 #endif /* linux */
183 #ifdef BSDI
185 #include <machine/frame.h>
186 typedef struct trapframe SIGCONTEXT;
188 #define EAX_sig(context) ((context)->tf_eax)
189 #define EBX_sig(context) ((context)->tf_ebx)
190 #define ECX_sig(context) ((context)->tf_ecx)
191 #define EDX_sig(context) ((context)->tf_edx)
192 #define ESI_sig(context) ((context)->tf_esi)
193 #define EDI_sig(context) ((context)->tf_edi)
194 #define EBP_sig(context) ((context)->tf_ebp)
196 #define CS_sig(context) ((context)->tf_cs)
197 #define DS_sig(context) ((context)->tf_ds)
198 #define ES_sig(context) ((context)->tf_es)
199 #define SS_sig(context) ((context)->tf_ss)
201 #define EFL_sig(context) ((context)->tf_eflags)
203 #define EIP_sig(context) (*((unsigned long*)&(context)->tf_eip))
204 #define ESP_sig(context) (*((unsigned long*)&(context)->tf_esp))
206 #define FPU_sig(context) NULL /* FIXME */
207 #define FPUX_sig(context) NULL /* FIXME */
209 #endif /* bsdi */
211 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
213 typedef struct sigcontext SIGCONTEXT;
215 #define EAX_sig(context) ((context)->sc_eax)
216 #define EBX_sig(context) ((context)->sc_ebx)
217 #define ECX_sig(context) ((context)->sc_ecx)
218 #define EDX_sig(context) ((context)->sc_edx)
219 #define ESI_sig(context) ((context)->sc_esi)
220 #define EDI_sig(context) ((context)->sc_edi)
221 #define EBP_sig(context) ((context)->sc_ebp)
223 #define CS_sig(context) ((context)->sc_cs)
224 #define DS_sig(context) ((context)->sc_ds)
225 #define ES_sig(context) ((context)->sc_es)
226 #define FS_sig(context) ((context)->sc_fs)
227 #define GS_sig(context) ((context)->sc_gs)
228 #define SS_sig(context) ((context)->sc_ss)
230 #define TRAP_sig(context) ((context)->sc_trapno)
231 #define ERROR_sig(context) ((context)->sc_err)
232 #define EFL_sig(context) ((context)->sc_eflags)
234 #define EIP_sig(context) ((context)->sc_eip)
235 #define ESP_sig(context) ((context)->sc_esp)
237 #define FPU_sig(context) NULL /* FIXME */
238 #define FPUX_sig(context) NULL /* FIXME */
240 #endif /* __FreeBSD__ */
242 #ifdef __OpenBSD__
244 typedef struct sigcontext SIGCONTEXT;
246 #define EAX_sig(context) ((context)->sc_eax)
247 #define EBX_sig(context) ((context)->sc_ebx)
248 #define ECX_sig(context) ((context)->sc_ecx)
249 #define EDX_sig(context) ((context)->sc_edx)
250 #define ESI_sig(context) ((context)->sc_esi)
251 #define EDI_sig(context) ((context)->sc_edi)
252 #define EBP_sig(context) ((context)->sc_ebp)
254 #define CS_sig(context) ((context)->sc_cs)
255 #define DS_sig(context) ((context)->sc_ds)
256 #define ES_sig(context) ((context)->sc_es)
257 #define FS_sig(context) ((context)->sc_fs)
258 #define GS_sig(context) ((context)->sc_gs)
259 #define SS_sig(context) ((context)->sc_ss)
261 #define TRAP_sig(context) ((context)->sc_trapno)
262 #define ERROR_sig(context) ((context)->sc_err)
263 #define EFL_sig(context) ((context)->sc_eflags)
265 #define EIP_sig(context) ((context)->sc_eip)
266 #define ESP_sig(context) ((context)->sc_esp)
268 #define FPU_sig(context) NULL /* FIXME */
269 #define FPUX_sig(context) NULL /* FIXME */
271 #define T_MCHK T_MACHK
272 #define T_XMMFLT T_XFTRAP
274 #endif /* __OpenBSD__ */
276 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
278 #ifdef _SCO_DS
279 #include <sys/regset.h>
280 #endif
281 #include <sys/ucontext.h>
282 typedef struct ucontext SIGCONTEXT;
284 #ifdef _SCO_DS
285 #define gregs regs
286 #endif
288 #define EAX_sig(context) ((context)->uc_mcontext.gregs[EAX])
289 #define EBX_sig(context) ((context)->uc_mcontext.gregs[EBX])
290 #define ECX_sig(context) ((context)->uc_mcontext.gregs[ECX])
291 #define EDX_sig(context) ((context)->uc_mcontext.gregs[EDX])
292 #define ESI_sig(context) ((context)->uc_mcontext.gregs[ESI])
293 #define EDI_sig(context) ((context)->uc_mcontext.gregs[EDI])
294 #define EBP_sig(context) ((context)->uc_mcontext.gregs[EBP])
296 #define CS_sig(context) ((context)->uc_mcontext.gregs[CS])
297 #define DS_sig(context) ((context)->uc_mcontext.gregs[DS])
298 #define ES_sig(context) ((context)->uc_mcontext.gregs[ES])
299 #define SS_sig(context) ((context)->uc_mcontext.gregs[SS])
301 #define FS_sig(context) ((context)->uc_mcontext.gregs[FS])
302 #define GS_sig(context) ((context)->uc_mcontext.gregs[GS])
304 #define EFL_sig(context) ((context)->uc_mcontext.gregs[EFL])
306 #define EIP_sig(context) ((context)->uc_mcontext.gregs[EIP])
307 #ifdef UESP
308 #define ESP_sig(context) ((context)->uc_mcontext.gregs[UESP])
309 #elif defined(R_ESP)
310 #define ESP_sig(context) ((context)->uc_mcontext.gregs[R_ESP])
311 #else
312 #define ESP_sig(context) ((context)->uc_mcontext.gregs[ESP])
313 #endif
314 #ifdef ERR
315 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[ERR])
316 #endif
317 #ifdef TRAPNO
318 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[TRAPNO])
319 #endif
321 #define FPU_sig(context) NULL /* FIXME */
322 #define FPUX_sig(context) NULL /* FIXME */
324 #endif /* svr4 || SCO_DS */
326 #ifdef __APPLE__
327 # include <sys/ucontext.h>
329 typedef ucontext_t SIGCONTEXT;
331 /* work around silly renaming of struct members in OS X 10.5 */
332 #if __DARWIN_UNIX03 && defined(_STRUCT_X86_EXCEPTION_STATE32)
333 #define EAX_sig(context) ((context)->uc_mcontext->__ss.__eax)
334 #define EBX_sig(context) ((context)->uc_mcontext->__ss.__ebx)
335 #define ECX_sig(context) ((context)->uc_mcontext->__ss.__ecx)
336 #define EDX_sig(context) ((context)->uc_mcontext->__ss.__edx)
337 #define ESI_sig(context) ((context)->uc_mcontext->__ss.__esi)
338 #define EDI_sig(context) ((context)->uc_mcontext->__ss.__edi)
339 #define EBP_sig(context) ((context)->uc_mcontext->__ss.__ebp)
340 #define CS_sig(context) ((context)->uc_mcontext->__ss.__cs)
341 #define DS_sig(context) ((context)->uc_mcontext->__ss.__ds)
342 #define ES_sig(context) ((context)->uc_mcontext->__ss.__es)
343 #define FS_sig(context) ((context)->uc_mcontext->__ss.__fs)
344 #define GS_sig(context) ((context)->uc_mcontext->__ss.__gs)
345 #define SS_sig(context) ((context)->uc_mcontext->__ss.__ss)
346 #define EFL_sig(context) ((context)->uc_mcontext->__ss.__eflags)
347 #define EIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->__ss.__eip))
348 #define ESP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->__ss.__esp))
349 #define TRAP_sig(context) ((context)->uc_mcontext->__es.__trapno)
350 #define ERROR_sig(context) ((context)->uc_mcontext->__es.__err)
351 #define FPU_sig(context) NULL
352 #define FPUX_sig(context) ((XMM_SAVE_AREA32 *)&(context)->uc_mcontext->__fs.__fpu_fcw)
353 #else
354 #define EAX_sig(context) ((context)->uc_mcontext->ss.eax)
355 #define EBX_sig(context) ((context)->uc_mcontext->ss.ebx)
356 #define ECX_sig(context) ((context)->uc_mcontext->ss.ecx)
357 #define EDX_sig(context) ((context)->uc_mcontext->ss.edx)
358 #define ESI_sig(context) ((context)->uc_mcontext->ss.esi)
359 #define EDI_sig(context) ((context)->uc_mcontext->ss.edi)
360 #define EBP_sig(context) ((context)->uc_mcontext->ss.ebp)
361 #define CS_sig(context) ((context)->uc_mcontext->ss.cs)
362 #define DS_sig(context) ((context)->uc_mcontext->ss.ds)
363 #define ES_sig(context) ((context)->uc_mcontext->ss.es)
364 #define FS_sig(context) ((context)->uc_mcontext->ss.fs)
365 #define GS_sig(context) ((context)->uc_mcontext->ss.gs)
366 #define SS_sig(context) ((context)->uc_mcontext->ss.ss)
367 #define EFL_sig(context) ((context)->uc_mcontext->ss.eflags)
368 #define EIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->ss.eip))
369 #define ESP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->ss.esp))
370 #define TRAP_sig(context) ((context)->uc_mcontext->es.trapno)
371 #define ERROR_sig(context) ((context)->uc_mcontext->es.err)
372 #define FPU_sig(context) NULL
373 #define FPUX_sig(context) ((XMM_SAVE_AREA32 *)&(context)->uc_mcontext->fs.fpu_fcw)
374 #endif
376 #endif /* __APPLE__ */
378 #if defined(__NetBSD__)
379 # include <sys/ucontext.h>
380 # include <sys/types.h>
381 # include <signal.h>
383 typedef ucontext_t SIGCONTEXT;
385 #define EAX_sig(context) ((context)->uc_mcontext.__gregs[_REG_EAX])
386 #define EBX_sig(context) ((context)->uc_mcontext.__gregs[_REG_EBX])
387 #define ECX_sig(context) ((context)->uc_mcontext.__gregs[_REG_ECX])
388 #define EDX_sig(context) ((context)->uc_mcontext.__gregs[_REG_EDX])
389 #define ESI_sig(context) ((context)->uc_mcontext.__gregs[_REG_ESI])
390 #define EDI_sig(context) ((context)->uc_mcontext.__gregs[_REG_EDI])
391 #define EBP_sig(context) ((context)->uc_mcontext.__gregs[_REG_EBP])
392 #define ESP_sig(context) _UC_MACHINE_SP(context)
394 #define CS_sig(context) ((context)->uc_mcontext.__gregs[_REG_CS])
395 #define DS_sig(context) ((context)->uc_mcontext.__gregs[_REG_DS])
396 #define ES_sig(context) ((context)->uc_mcontext.__gregs[_REG_ES])
397 #define SS_sig(context) ((context)->uc_mcontext.__gregs[_REG_SS])
398 #define FS_sig(context) ((context)->uc_mcontext.__gregs[_REG_FS])
399 #define GS_sig(context) ((context)->uc_mcontext.__gregs[_REG_GS])
401 #define EFL_sig(context) ((context)->uc_mcontext.__gregs[_REG_EFL])
402 #define EIP_sig(context) _UC_MACHINE_PC(context)
403 #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
404 #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR])
406 #define FPU_sig(context) NULL
407 #define FPUX_sig(context) ((XMM_SAVE_AREA32 *)&((context)->uc_mcontext.__fpregs))
409 #define T_MCHK T_MCA
410 #define T_XMMFLT T_XMM
412 #endif /* __NetBSD__ */
414 WINE_DEFAULT_DEBUG_CHANNEL(seh);
416 typedef int (*wine_signal_handler)(unsigned int sig);
418 static const size_t teb_size = 4096; /* we reserve one page for the TEB */
419 static size_t signal_stack_mask;
420 static size_t signal_stack_size;
422 static wine_signal_handler handlers[256];
424 static int fpux_support; /* whether the CPU support extended fpu context */
426 extern void DECLSPEC_NORETURN __wine_restore_regs( const CONTEXT *context );
428 enum i386_trap_code
430 TRAP_x86_UNKNOWN = -1, /* Unknown fault (TRAP_sig not defined) */
431 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
432 TRAP_x86_DIVIDE = T_DIVIDE, /* Division by zero exception */
433 TRAP_x86_TRCTRAP = T_TRCTRAP, /* Single-step exception */
434 TRAP_x86_NMI = T_NMI, /* NMI interrupt */
435 TRAP_x86_BPTFLT = T_BPTFLT, /* Breakpoint exception */
436 TRAP_x86_OFLOW = T_OFLOW, /* Overflow exception */
437 TRAP_x86_BOUND = T_BOUND, /* Bound range exception */
438 TRAP_x86_PRIVINFLT = T_PRIVINFLT, /* Invalid opcode exception */
439 TRAP_x86_DNA = T_DNA, /* Device not available exception */
440 TRAP_x86_DOUBLEFLT = T_DOUBLEFLT, /* Double fault exception */
441 TRAP_x86_FPOPFLT = T_FPOPFLT, /* Coprocessor segment overrun */
442 TRAP_x86_TSSFLT = T_TSSFLT, /* Invalid TSS exception */
443 TRAP_x86_SEGNPFLT = T_SEGNPFLT, /* Segment not present exception */
444 TRAP_x86_STKFLT = T_STKFLT, /* Stack fault */
445 TRAP_x86_PROTFLT = T_PROTFLT, /* General protection fault */
446 TRAP_x86_PAGEFLT = T_PAGEFLT, /* Page fault */
447 TRAP_x86_ARITHTRAP = T_ARITHTRAP, /* Floating point exception */
448 TRAP_x86_ALIGNFLT = T_ALIGNFLT, /* Alignment check exception */
449 TRAP_x86_MCHK = T_MCHK, /* Machine check exception */
450 TRAP_x86_CACHEFLT = T_XMMFLT /* Cache flush exception */
451 #else
452 TRAP_x86_DIVIDE = 0, /* Division by zero exception */
453 TRAP_x86_TRCTRAP = 1, /* Single-step exception */
454 TRAP_x86_NMI = 2, /* NMI interrupt */
455 TRAP_x86_BPTFLT = 3, /* Breakpoint exception */
456 TRAP_x86_OFLOW = 4, /* Overflow exception */
457 TRAP_x86_BOUND = 5, /* Bound range exception */
458 TRAP_x86_PRIVINFLT = 6, /* Invalid opcode exception */
459 TRAP_x86_DNA = 7, /* Device not available exception */
460 TRAP_x86_DOUBLEFLT = 8, /* Double fault exception */
461 TRAP_x86_FPOPFLT = 9, /* Coprocessor segment overrun */
462 TRAP_x86_TSSFLT = 10, /* Invalid TSS exception */
463 TRAP_x86_SEGNPFLT = 11, /* Segment not present exception */
464 TRAP_x86_STKFLT = 12, /* Stack fault */
465 TRAP_x86_PROTFLT = 13, /* General protection fault */
466 TRAP_x86_PAGEFLT = 14, /* Page fault */
467 TRAP_x86_ARITHTRAP = 16, /* Floating point exception */
468 TRAP_x86_ALIGNFLT = 17, /* Alignment check exception */
469 TRAP_x86_MCHK = 18, /* Machine check exception */
470 TRAP_x86_CACHEFLT = 19 /* SIMD exception (via SIGFPE) if CPU is SSE capable
471 otherwise Cache flush exception (via SIGSEV) */
472 #endif
475 /* Exception record for handling exceptions happening inside exception handlers */
476 typedef struct
478 EXCEPTION_REGISTRATION_RECORD frame;
479 EXCEPTION_REGISTRATION_RECORD *prevFrame;
480 } EXC_NESTED_FRAME;
482 extern DWORD EXC_CallHandler( EXCEPTION_RECORD *record, EXCEPTION_REGISTRATION_RECORD *frame,
483 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher,
484 PEXCEPTION_HANDLER handler, PEXCEPTION_HANDLER nested_handler );
486 /***********************************************************************
487 * dispatch_signal
489 static inline int dispatch_signal(unsigned int sig)
491 if (handlers[sig] == NULL) return 0;
492 return handlers[sig](sig);
496 /***********************************************************************
497 * get_trap_code
499 * Get the trap code for a signal.
501 static inline enum i386_trap_code get_trap_code( const SIGCONTEXT *sigcontext )
503 #ifdef TRAP_sig
504 return TRAP_sig(sigcontext);
505 #else
506 return TRAP_x86_UNKNOWN; /* unknown trap code */
507 #endif
510 /***********************************************************************
511 * get_error_code
513 * Get the error code for a signal.
515 static inline WORD get_error_code( const SIGCONTEXT *sigcontext )
517 #ifdef ERROR_sig
518 return ERROR_sig(sigcontext);
519 #else
520 return 0;
521 #endif
524 /***********************************************************************
525 * get_signal_stack
527 * Get the base of the signal stack for the current thread.
529 static inline void *get_signal_stack(void)
531 return (char *)NtCurrentTeb() + 4096;
535 /***********************************************************************
536 * get_current_teb
538 * Get the current teb based on the stack pointer.
540 static inline TEB *get_current_teb(void)
542 unsigned long esp;
543 __asm__("movl %%esp,%0" : "=g" (esp) );
544 return (TEB *)(esp & ~signal_stack_mask);
548 /*******************************************************************
549 * raise_handler
551 * Handler for exceptions happening inside a handler.
553 static DWORD raise_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
554 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
556 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
557 return ExceptionContinueSearch;
558 /* We shouldn't get here so we store faulty frame in dispatcher */
559 *dispatcher = ((EXC_NESTED_FRAME*)frame)->prevFrame;
560 return ExceptionNestedException;
564 /*******************************************************************
565 * unwind_handler
567 * Handler for exceptions happening inside an unwind handler.
569 static DWORD unwind_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
570 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
572 if (!(rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
573 return ExceptionContinueSearch;
574 /* We shouldn't get here so we store faulty frame in dispatcher */
575 *dispatcher = ((EXC_NESTED_FRAME*)frame)->prevFrame;
576 return ExceptionCollidedUnwind;
580 /**********************************************************************
581 * call_stack_handlers
583 * Call the stack handlers chain.
585 static NTSTATUS call_stack_handlers( EXCEPTION_RECORD *rec, CONTEXT *context )
587 EXCEPTION_REGISTRATION_RECORD *frame, *dispatch, *nested_frame;
588 DWORD res;
590 frame = NtCurrentTeb()->Tib.ExceptionList;
591 nested_frame = NULL;
592 while (frame != (EXCEPTION_REGISTRATION_RECORD*)~0UL)
594 /* Check frame address */
595 if (((void*)frame < NtCurrentTeb()->Tib.StackLimit) ||
596 ((void*)(frame+1) > NtCurrentTeb()->Tib.StackBase) ||
597 (ULONG_PTR)frame & 3)
599 rec->ExceptionFlags |= EH_STACK_INVALID;
600 break;
603 /* Call handler */
604 TRACE( "calling handler at %p code=%x flags=%x\n",
605 frame->Handler, rec->ExceptionCode, rec->ExceptionFlags );
606 res = EXC_CallHandler( rec, frame, context, &dispatch, frame->Handler, raise_handler );
607 TRACE( "handler at %p returned %x\n", frame->Handler, res );
609 if (frame == nested_frame)
611 /* no longer nested */
612 nested_frame = NULL;
613 rec->ExceptionFlags &= ~EH_NESTED_CALL;
616 switch(res)
618 case ExceptionContinueExecution:
619 if (!(rec->ExceptionFlags & EH_NONCONTINUABLE)) return STATUS_SUCCESS;
620 return STATUS_NONCONTINUABLE_EXCEPTION;
621 case ExceptionContinueSearch:
622 break;
623 case ExceptionNestedException:
624 if (nested_frame < dispatch) nested_frame = dispatch;
625 rec->ExceptionFlags |= EH_NESTED_CALL;
626 break;
627 default:
628 return STATUS_INVALID_DISPOSITION;
630 frame = frame->Prev;
632 return STATUS_UNHANDLED_EXCEPTION;
636 /*******************************************************************
637 * raise_exception
639 * Implementation of NtRaiseException.
641 static NTSTATUS raise_exception( EXCEPTION_RECORD *rec, CONTEXT *context, BOOL first_chance )
643 NTSTATUS status;
645 if (first_chance)
647 DWORD c;
649 TRACE( "code=%x flags=%x addr=%p ip=%08x tid=%04x\n",
650 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
651 context->Eip, GetCurrentThreadId() );
652 for (c = 0; c < rec->NumberParameters; c++)
653 TRACE( " info[%d]=%08lx\n", c, rec->ExceptionInformation[c] );
654 if (rec->ExceptionCode == EXCEPTION_WINE_STUB)
656 if (rec->ExceptionInformation[1] >> 16)
657 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
658 rec->ExceptionAddress,
659 (char*)rec->ExceptionInformation[0], (char*)rec->ExceptionInformation[1] );
660 else
661 MESSAGE( "wine: Call from %p to unimplemented function %s.%ld, aborting\n",
662 rec->ExceptionAddress,
663 (char*)rec->ExceptionInformation[0], rec->ExceptionInformation[1] );
665 else
667 TRACE(" eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x\n",
668 context->Eax, context->Ebx, context->Ecx,
669 context->Edx, context->Esi, context->Edi );
670 TRACE(" ebp=%08x esp=%08x cs=%04x ds=%04x es=%04x fs=%04x gs=%04x flags=%08x\n",
671 context->Ebp, context->Esp, context->SegCs, context->SegDs,
672 context->SegEs, context->SegFs, context->SegGs, context->EFlags );
674 status = send_debug_event( rec, TRUE, context );
675 if (status == DBG_CONTINUE || status == DBG_EXCEPTION_HANDLED)
676 return STATUS_SUCCESS;
678 /* fix up instruction pointer in context for EXCEPTION_BREAKPOINT */
679 if (rec->ExceptionCode == EXCEPTION_BREAKPOINT) context->Eip--;
681 if (call_vectored_handlers( rec, context ) == EXCEPTION_CONTINUE_EXECUTION)
682 return STATUS_SUCCESS;
684 if ((status = call_stack_handlers( rec, context )) != STATUS_UNHANDLED_EXCEPTION)
685 return status;
688 /* last chance exception */
690 status = send_debug_event( rec, FALSE, context );
691 if (status != DBG_CONTINUE)
693 if (rec->ExceptionFlags & EH_STACK_INVALID)
694 WINE_ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
695 else if (rec->ExceptionCode == STATUS_NONCONTINUABLE_EXCEPTION)
696 WINE_ERR("Process attempted to continue execution after noncontinuable exception.\n");
697 else
698 WINE_ERR("Unhandled exception code %x flags %x addr %p\n",
699 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
700 NtTerminateProcess( NtCurrentProcess(), rec->ExceptionCode );
702 return STATUS_SUCCESS;
706 #ifdef __HAVE_VM86
707 /***********************************************************************
708 * save_vm86_context
710 * Set the register values from a vm86 structure.
712 static void save_vm86_context( CONTEXT *context, const struct vm86plus_struct *vm86 )
714 context->ContextFlags = CONTEXT_FULL;
715 context->Eax = vm86->regs.eax;
716 context->Ebx = vm86->regs.ebx;
717 context->Ecx = vm86->regs.ecx;
718 context->Edx = vm86->regs.edx;
719 context->Esi = vm86->regs.esi;
720 context->Edi = vm86->regs.edi;
721 context->Esp = vm86->regs.esp;
722 context->Ebp = vm86->regs.ebp;
723 context->Eip = vm86->regs.eip;
724 context->SegCs = vm86->regs.cs;
725 context->SegDs = vm86->regs.ds;
726 context->SegEs = vm86->regs.es;
727 context->SegFs = vm86->regs.fs;
728 context->SegGs = vm86->regs.gs;
729 context->SegSs = vm86->regs.ss;
730 context->EFlags = vm86->regs.eflags;
734 /***********************************************************************
735 * restore_vm86_context
737 * Build a vm86 structure from the register values.
739 static void restore_vm86_context( const CONTEXT *context, struct vm86plus_struct *vm86 )
741 vm86->regs.eax = context->Eax;
742 vm86->regs.ebx = context->Ebx;
743 vm86->regs.ecx = context->Ecx;
744 vm86->regs.edx = context->Edx;
745 vm86->regs.esi = context->Esi;
746 vm86->regs.edi = context->Edi;
747 vm86->regs.esp = context->Esp;
748 vm86->regs.ebp = context->Ebp;
749 vm86->regs.eip = context->Eip;
750 vm86->regs.cs = context->SegCs;
751 vm86->regs.ds = context->SegDs;
752 vm86->regs.es = context->SegEs;
753 vm86->regs.fs = context->SegFs;
754 vm86->regs.gs = context->SegGs;
755 vm86->regs.ss = context->SegSs;
756 vm86->regs.eflags = context->EFlags;
760 /**********************************************************************
761 * merge_vm86_pending_flags
763 * Merges TEB.vm86_ptr and TEB.vm86_pending VIP flags and
764 * raises exception if there are pending events and VIF flag
765 * has been turned on.
767 * Called from __wine_enter_vm86 because vm86_enter
768 * doesn't check for pending events.
770 * Called from raise_vm86_sti_exception to check for
771 * pending events in a signal safe way.
773 static void merge_vm86_pending_flags( EXCEPTION_RECORD *rec )
775 BOOL check_pending = TRUE;
776 struct vm86plus_struct *vm86 =
777 (struct vm86plus_struct*)(ntdll_get_thread_data()->vm86_ptr);
780 * In order to prevent a race when SIGUSR2 occurs while
781 * we are returning from exception handler, pending events
782 * will be rechecked after each raised exception.
784 while (check_pending && get_vm86_teb_info()->vm86_pending)
786 check_pending = FALSE;
787 ntdll_get_thread_data()->vm86_ptr = NULL;
790 * If VIF is set, throw exception.
791 * Note that SIGUSR2 may turn VIF flag off so
792 * VIF check must occur only when TEB.vm86_ptr is NULL.
794 if (vm86->regs.eflags & VIF_FLAG)
796 CONTEXT vcontext;
797 save_vm86_context( &vcontext, vm86 );
799 rec->ExceptionCode = EXCEPTION_VM86_STI;
800 rec->ExceptionFlags = EXCEPTION_CONTINUABLE;
801 rec->ExceptionRecord = NULL;
802 rec->NumberParameters = 0;
803 rec->ExceptionAddress = (LPVOID)vcontext.Eip;
805 vcontext.EFlags &= ~VIP_FLAG;
806 get_vm86_teb_info()->vm86_pending = 0;
807 raise_exception( rec, &vcontext, TRUE );
809 restore_vm86_context( &vcontext, vm86 );
810 check_pending = TRUE;
813 ntdll_get_thread_data()->vm86_ptr = vm86;
817 * Merge VIP flags in a signal safe way. This requires
818 * that the following operation compiles into atomic
819 * instruction.
821 vm86->regs.eflags |= get_vm86_teb_info()->vm86_pending;
823 #endif /* __HAVE_VM86 */
826 #ifdef __sun
828 /* We have to workaround two Solaris breakages:
829 * - Solaris doesn't restore %ds and %es before calling the signal handler so exceptions in 16-bit
830 * code crash badly.
831 * - Solaris inserts a libc trampoline to call our handler, but the trampoline expects that registers
832 * are setup correctly. So we need to insert our own trampoline below the libc trampoline to set %gs.
835 extern int sigaction_syscall( int sig, const struct sigaction *new, struct sigaction *old );
836 __ASM_GLOBAL_FUNC( sigaction_syscall,
837 "movl $0x62,%eax\n\t"
838 "int $0x91\n\t"
839 "ret" )
841 /* assume the same libc handler is used for all signals */
842 static void (*libc_sigacthandler)( int signal, siginfo_t *siginfo, void *context );
844 static void wine_sigacthandler( int signal, siginfo_t *siginfo, void *sigcontext )
846 struct ntdll_thread_data *thread_data;
848 __asm__ __volatile__("mov %ss,%ax; mov %ax,%ds; mov %ax,%es");
850 thread_data = (struct ntdll_thread_data *)get_current_teb()->SpareBytes1;
851 wine_set_fs( thread_data->fs );
852 wine_set_gs( thread_data->gs );
854 libc_sigacthandler( signal, siginfo, sigcontext );
857 static int solaris_sigaction( int sig, const struct sigaction *new, struct sigaction *old )
859 struct sigaction real_act;
861 if (sigaction( sig, new, old ) == -1) return -1;
863 /* retrieve the real handler and flags with a direct syscall */
864 sigaction_syscall( sig, NULL, &real_act );
865 libc_sigacthandler = real_act.sa_sigaction;
866 real_act.sa_sigaction = wine_sigacthandler;
867 sigaction_syscall( sig, &real_act, NULL );
868 return 0;
870 #define sigaction(sig,new,old) solaris_sigaction(sig,new,old)
872 #endif
874 typedef void (WINAPI *raise_func)( EXCEPTION_RECORD *rec, CONTEXT *context );
876 extern void clear_alignment_flag(void);
877 __ASM_GLOBAL_FUNC( clear_alignment_flag,
878 "pushfl\n\t"
879 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
880 "andl $~0x40000,(%esp)\n\t"
881 "popfl\n\t"
882 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
883 "ret" )
886 /***********************************************************************
887 * init_handler
889 * Handler initialization when the full context is not needed.
890 * Return the stack pointer to use for pushing the exception data.
892 static inline void *init_handler( const SIGCONTEXT *sigcontext, WORD *fs, WORD *gs )
894 TEB *teb = get_current_teb();
896 clear_alignment_flag();
898 /* get %fs and %gs at time of the fault */
899 #ifdef FS_sig
900 *fs = LOWORD(FS_sig(sigcontext));
901 #else
902 *fs = wine_get_fs();
903 #endif
904 #ifdef GS_sig
905 *gs = LOWORD(GS_sig(sigcontext));
906 #else
907 *gs = wine_get_gs();
908 #endif
910 #ifndef __sun /* see above for Solaris handling */
912 struct ntdll_thread_data *thread_data = (struct ntdll_thread_data *)teb->SpareBytes1;
913 wine_set_fs( thread_data->fs );
914 wine_set_gs( thread_data->gs );
916 #endif
918 if (!wine_ldt_is_system(CS_sig(sigcontext)) ||
919 !wine_ldt_is_system(SS_sig(sigcontext))) /* 16-bit mode */
922 * Win16 or DOS protected mode. Note that during switch
923 * from 16-bit mode to linear mode, CS may be set to system
924 * segment before FS is restored. Fortunately, in this case
925 * SS is still non-system segment. This is why both CS and SS
926 * are checked.
928 return teb->WOW32Reserved;
930 return (void *)(ESP_sig(sigcontext) & ~3);
934 /***********************************************************************
935 * save_fpu
937 * Save the thread FPU context.
939 static inline void save_fpu( CONTEXT *context )
941 #ifdef __GNUC__
942 context->ContextFlags |= CONTEXT_FLOATING_POINT;
943 __asm__ __volatile__( "fnsave %0; fwait" : "=m" (context->FloatSave) );
944 #endif
948 /***********************************************************************
949 * save_fpux
951 * Save the thread FPU extended context.
953 static inline void save_fpux( CONTEXT *context )
955 #ifdef __GNUC__
956 /* we have to enforce alignment by hand */
957 char buffer[sizeof(XMM_SAVE_AREA32) + 16];
958 XMM_SAVE_AREA32 *state = (XMM_SAVE_AREA32 *)(((ULONG_PTR)buffer + 15) & ~15);
960 __asm__ __volatile__( "fxsave %0" : "=m" (*state) );
961 context->ContextFlags |= CONTEXT_EXTENDED_REGISTERS;
962 memcpy( context->ExtendedRegisters, state, sizeof(*state) );
963 #endif
967 /***********************************************************************
968 * restore_fpu
970 * Restore the FPU context to a sigcontext.
972 static inline void restore_fpu( const CONTEXT *context )
974 FLOATING_SAVE_AREA float_status = context->FloatSave;
975 /* reset the current interrupt status */
976 float_status.StatusWord &= float_status.ControlWord | 0xffffff80;
977 #ifdef __GNUC__
978 __asm__ __volatile__( "frstor %0; fwait" : : "m" (float_status) );
979 #endif /* __GNUC__ */
983 /***********************************************************************
984 * restore_fpux
986 * Restore the FPU extended context to a sigcontext.
988 static inline void restore_fpux( const CONTEXT *context )
990 #ifdef __GNUC__
991 /* we have to enforce alignment by hand */
992 char buffer[sizeof(XMM_SAVE_AREA32) + 16];
993 XMM_SAVE_AREA32 *state = (XMM_SAVE_AREA32 *)(((ULONG_PTR)buffer + 15) & ~15);
995 memcpy( state, context->ExtendedRegisters, sizeof(*state) );
996 /* reset the current interrupt status */
997 state->StatusWord &= state->ControlWord | 0xff80;
998 __asm__ __volatile__( "fxrstor %0" : : "m" (*state) );
999 #endif
1003 /***********************************************************************
1004 * fpux_to_fpu
1006 * Build a standard FPU context from an extended one.
1008 static void fpux_to_fpu( FLOATING_SAVE_AREA *fpu, const XMM_SAVE_AREA32 *fpux )
1010 unsigned int i, tag, stack_top;
1012 fpu->ControlWord = fpux->ControlWord | 0xffff0000;
1013 fpu->StatusWord = fpux->StatusWord | 0xffff0000;
1014 fpu->ErrorOffset = fpux->ErrorOffset;
1015 fpu->ErrorSelector = fpux->ErrorSelector | (fpux->ErrorOpcode << 16);
1016 fpu->DataOffset = fpux->DataOffset;
1017 fpu->DataSelector = fpux->DataSelector;
1018 fpu->Cr0NpxState = fpux->StatusWord | 0xffff0000;
1020 stack_top = (fpux->StatusWord >> 11) & 7;
1021 fpu->TagWord = 0xffff0000;
1022 for (i = 0; i < 8; i++)
1024 memcpy( &fpu->RegisterArea[10 * i], &fpux->FloatRegisters[i], 10 );
1025 if (!(fpux->TagWord & (1 << i))) tag = 3; /* empty */
1026 else
1028 const M128A *reg = &fpux->FloatRegisters[(i - stack_top) & 7];
1029 if ((reg->High & 0x7fff) == 0x7fff) /* exponent all ones */
1031 tag = 2; /* special */
1033 else if (!(reg->High & 0x7fff)) /* exponent all zeroes */
1035 if (reg->Low) tag = 2; /* special */
1036 else tag = 1; /* zero */
1038 else
1040 if (reg->Low >> 63) tag = 0; /* valid */
1041 else tag = 2; /* special */
1044 fpu->TagWord |= tag << (2 * i);
1049 /***********************************************************************
1050 * save_context
1052 * Build a context structure from the signal info.
1054 static inline void save_context( CONTEXT *context, const SIGCONTEXT *sigcontext, WORD fs, WORD gs )
1056 struct ntdll_thread_data * const regs = ntdll_get_thread_data();
1057 FLOATING_SAVE_AREA *fpu = FPU_sig(sigcontext);
1058 XMM_SAVE_AREA32 *fpux = FPUX_sig(sigcontext);
1060 memset(context, 0, sizeof(*context));
1061 context->ContextFlags = CONTEXT_FULL | CONTEXT_DEBUG_REGISTERS;
1062 context->Eax = EAX_sig(sigcontext);
1063 context->Ebx = EBX_sig(sigcontext);
1064 context->Ecx = ECX_sig(sigcontext);
1065 context->Edx = EDX_sig(sigcontext);
1066 context->Esi = ESI_sig(sigcontext);
1067 context->Edi = EDI_sig(sigcontext);
1068 context->Ebp = EBP_sig(sigcontext);
1069 context->EFlags = EFL_sig(sigcontext);
1070 context->Eip = EIP_sig(sigcontext);
1071 context->Esp = ESP_sig(sigcontext);
1072 context->SegCs = LOWORD(CS_sig(sigcontext));
1073 context->SegDs = LOWORD(DS_sig(sigcontext));
1074 context->SegEs = LOWORD(ES_sig(sigcontext));
1075 context->SegFs = fs;
1076 context->SegGs = gs;
1077 context->SegSs = LOWORD(SS_sig(sigcontext));
1078 context->Dr0 = regs->dr0;
1079 context->Dr1 = regs->dr1;
1080 context->Dr2 = regs->dr2;
1081 context->Dr3 = regs->dr3;
1082 context->Dr6 = regs->dr6;
1083 context->Dr7 = regs->dr7;
1085 if (fpu)
1087 context->ContextFlags |= CONTEXT_FLOATING_POINT;
1088 context->FloatSave = *fpu;
1090 if (fpux)
1092 context->ContextFlags |= CONTEXT_FLOATING_POINT | CONTEXT_EXTENDED_REGISTERS;
1093 memcpy( context->ExtendedRegisters, fpux, sizeof(*fpux) );
1094 fpux_support = 1;
1095 if (!fpu) fpux_to_fpu( &context->FloatSave, fpux );
1097 if (!fpu && !fpux) save_fpu( context );
1101 /***********************************************************************
1102 * restore_context
1104 * Restore the signal info from the context.
1106 static inline void restore_context( const CONTEXT *context, SIGCONTEXT *sigcontext )
1108 struct ntdll_thread_data * const regs = ntdll_get_thread_data();
1109 FLOATING_SAVE_AREA *fpu = FPU_sig(sigcontext);
1110 XMM_SAVE_AREA32 *fpux = FPUX_sig(sigcontext);
1112 regs->dr0 = context->Dr0;
1113 regs->dr1 = context->Dr1;
1114 regs->dr2 = context->Dr2;
1115 regs->dr3 = context->Dr3;
1116 regs->dr6 = context->Dr6;
1117 regs->dr7 = context->Dr7;
1118 EAX_sig(sigcontext) = context->Eax;
1119 EBX_sig(sigcontext) = context->Ebx;
1120 ECX_sig(sigcontext) = context->Ecx;
1121 EDX_sig(sigcontext) = context->Edx;
1122 ESI_sig(sigcontext) = context->Esi;
1123 EDI_sig(sigcontext) = context->Edi;
1124 EBP_sig(sigcontext) = context->Ebp;
1125 EFL_sig(sigcontext) = context->EFlags;
1126 EIP_sig(sigcontext) = context->Eip;
1127 ESP_sig(sigcontext) = context->Esp;
1128 CS_sig(sigcontext) = context->SegCs;
1129 DS_sig(sigcontext) = context->SegDs;
1130 ES_sig(sigcontext) = context->SegEs;
1131 SS_sig(sigcontext) = context->SegSs;
1132 #ifdef GS_sig
1133 GS_sig(sigcontext) = context->SegGs;
1134 #else
1135 wine_set_gs( context->SegGs );
1136 #endif
1137 #ifdef FS_sig
1138 FS_sig(sigcontext) = context->SegFs;
1139 #else
1140 wine_set_fs( context->SegFs );
1141 #endif
1143 if (fpu) *fpu = context->FloatSave;
1144 if (fpux) memcpy( fpux, context->ExtendedRegisters, sizeof(*fpux) );
1145 if (!fpu && !fpux) restore_fpu( context );
1149 /***********************************************************************
1150 * RtlCaptureContext (NTDLL.@)
1152 void WINAPI __regs_RtlCaptureContext( CONTEXT *context, CONTEXT *regs )
1154 *context = *regs;
1155 if (fpux_support) save_fpux( context );
1156 else save_fpu( context );
1158 DEFINE_REGS_ENTRYPOINT( RtlCaptureContext, 1 )
1161 /***********************************************************************
1162 * set_cpu_context
1164 * Set the new CPU context. Used by NtSetContextThread.
1166 void set_cpu_context( const CONTEXT *context )
1168 DWORD flags = context->ContextFlags & ~CONTEXT_i386;
1170 if ((flags & CONTEXT_EXTENDED_REGISTERS) && fpux_support) restore_fpux( context );
1171 else if (flags & CONTEXT_FLOATING_POINT) restore_fpu( context );
1173 if (flags & CONTEXT_DEBUG_REGISTERS)
1175 ntdll_get_thread_data()->dr0 = context->Dr0;
1176 ntdll_get_thread_data()->dr1 = context->Dr1;
1177 ntdll_get_thread_data()->dr2 = context->Dr2;
1178 ntdll_get_thread_data()->dr3 = context->Dr3;
1179 ntdll_get_thread_data()->dr6 = context->Dr6;
1180 ntdll_get_thread_data()->dr7 = context->Dr7;
1182 if (flags & CONTEXT_FULL)
1184 if (!(flags & CONTEXT_CONTROL))
1185 FIXME( "setting partial context (%x) not supported\n", flags );
1186 else if (flags & CONTEXT_SEGMENTS)
1187 __wine_restore_regs( context );
1188 else
1190 CONTEXT newcontext = *context;
1191 newcontext.SegDs = wine_get_ds();
1192 newcontext.SegEs = wine_get_es();
1193 newcontext.SegFs = wine_get_fs();
1194 newcontext.SegGs = wine_get_gs();
1195 __wine_restore_regs( &newcontext );
1201 /***********************************************************************
1202 * set_debug_registers
1204 static void set_debug_registers( const CONTEXT *context )
1206 DWORD flags = context->ContextFlags & ~CONTEXT_i386;
1207 context_t server_context;
1209 if (!(flags & CONTEXT_DEBUG_REGISTERS)) return;
1210 if (ntdll_get_thread_data()->dr0 == context->Dr0 &&
1211 ntdll_get_thread_data()->dr1 == context->Dr1 &&
1212 ntdll_get_thread_data()->dr2 == context->Dr2 &&
1213 ntdll_get_thread_data()->dr3 == context->Dr3 &&
1214 ntdll_get_thread_data()->dr6 == context->Dr6 &&
1215 ntdll_get_thread_data()->dr7 == context->Dr7) return;
1217 context_to_server( &server_context, context );
1219 SERVER_START_REQ( set_thread_context )
1221 req->handle = wine_server_obj_handle( GetCurrentThread() );
1222 req->suspend = 0;
1223 wine_server_add_data( req, &server_context, sizeof(server_context) );
1224 wine_server_call( req );
1226 SERVER_END_REQ;
1230 /***********************************************************************
1231 * copy_context
1233 * Copy a register context according to the flags.
1235 void copy_context( CONTEXT *to, const CONTEXT *from, DWORD flags )
1237 flags &= ~CONTEXT_i386; /* get rid of CPU id */
1238 if (flags & CONTEXT_INTEGER)
1240 to->Eax = from->Eax;
1241 to->Ebx = from->Ebx;
1242 to->Ecx = from->Ecx;
1243 to->Edx = from->Edx;
1244 to->Esi = from->Esi;
1245 to->Edi = from->Edi;
1247 if (flags & CONTEXT_CONTROL)
1249 to->Ebp = from->Ebp;
1250 to->Esp = from->Esp;
1251 to->Eip = from->Eip;
1252 to->SegCs = from->SegCs;
1253 to->SegSs = from->SegSs;
1254 to->EFlags = from->EFlags;
1256 if (flags & CONTEXT_SEGMENTS)
1258 to->SegDs = from->SegDs;
1259 to->SegEs = from->SegEs;
1260 to->SegFs = from->SegFs;
1261 to->SegGs = from->SegGs;
1263 if (flags & CONTEXT_DEBUG_REGISTERS)
1265 to->Dr0 = from->Dr0;
1266 to->Dr1 = from->Dr1;
1267 to->Dr2 = from->Dr2;
1268 to->Dr3 = from->Dr3;
1269 to->Dr6 = from->Dr6;
1270 to->Dr7 = from->Dr7;
1272 if (flags & CONTEXT_FLOATING_POINT)
1274 to->FloatSave = from->FloatSave;
1276 if (flags & CONTEXT_EXTENDED_REGISTERS)
1278 memcpy( to->ExtendedRegisters, from->ExtendedRegisters, sizeof(to->ExtendedRegisters) );
1283 /***********************************************************************
1284 * context_to_server
1286 * Convert a register context to the server format.
1288 NTSTATUS context_to_server( context_t *to, const CONTEXT *from )
1290 DWORD flags = from->ContextFlags & ~CONTEXT_i386; /* get rid of CPU id */
1292 memset( to, 0, sizeof(*to) );
1293 to->cpu = CPU_x86;
1295 if (flags & CONTEXT_CONTROL)
1297 to->flags |= SERVER_CTX_CONTROL;
1298 to->ctl.i386_regs.ebp = from->Ebp;
1299 to->ctl.i386_regs.esp = from->Esp;
1300 to->ctl.i386_regs.eip = from->Eip;
1301 to->ctl.i386_regs.cs = from->SegCs;
1302 to->ctl.i386_regs.ss = from->SegSs;
1303 to->ctl.i386_regs.eflags = from->EFlags;
1305 if (flags & CONTEXT_INTEGER)
1307 to->flags |= SERVER_CTX_INTEGER;
1308 to->integer.i386_regs.eax = from->Eax;
1309 to->integer.i386_regs.ebx = from->Ebx;
1310 to->integer.i386_regs.ecx = from->Ecx;
1311 to->integer.i386_regs.edx = from->Edx;
1312 to->integer.i386_regs.esi = from->Esi;
1313 to->integer.i386_regs.edi = from->Edi;
1315 if (flags & CONTEXT_SEGMENTS)
1317 to->flags |= SERVER_CTX_SEGMENTS;
1318 to->seg.i386_regs.ds = from->SegDs;
1319 to->seg.i386_regs.es = from->SegEs;
1320 to->seg.i386_regs.fs = from->SegFs;
1321 to->seg.i386_regs.gs = from->SegGs;
1323 if (flags & CONTEXT_FLOATING_POINT)
1325 to->flags |= SERVER_CTX_FLOATING_POINT;
1326 to->fp.i386_regs.ctrl = from->FloatSave.ControlWord;
1327 to->fp.i386_regs.status = from->FloatSave.StatusWord;
1328 to->fp.i386_regs.tag = from->FloatSave.TagWord;
1329 to->fp.i386_regs.err_off = from->FloatSave.ErrorOffset;
1330 to->fp.i386_regs.err_sel = from->FloatSave.ErrorSelector;
1331 to->fp.i386_regs.data_off = from->FloatSave.DataOffset;
1332 to->fp.i386_regs.data_sel = from->FloatSave.DataSelector;
1333 to->fp.i386_regs.cr0npx = from->FloatSave.Cr0NpxState;
1334 memcpy( to->fp.i386_regs.regs, from->FloatSave.RegisterArea, sizeof(to->fp.i386_regs.regs) );
1336 if (flags & CONTEXT_DEBUG_REGISTERS)
1338 to->flags |= SERVER_CTX_DEBUG_REGISTERS;
1339 to->debug.i386_regs.dr0 = from->Dr0;
1340 to->debug.i386_regs.dr1 = from->Dr1;
1341 to->debug.i386_regs.dr2 = from->Dr2;
1342 to->debug.i386_regs.dr3 = from->Dr3;
1343 to->debug.i386_regs.dr6 = from->Dr6;
1344 to->debug.i386_regs.dr7 = from->Dr7;
1346 if (flags & CONTEXT_EXTENDED_REGISTERS)
1348 to->flags |= SERVER_CTX_EXTENDED_REGISTERS;
1349 memcpy( to->ext.i386_regs, from->ExtendedRegisters, sizeof(to->ext.i386_regs) );
1351 return STATUS_SUCCESS;
1355 /***********************************************************************
1356 * context_from_server
1358 * Convert a register context from the server format.
1360 NTSTATUS context_from_server( CONTEXT *to, const context_t *from )
1362 if (from->cpu != CPU_x86) return STATUS_INVALID_PARAMETER;
1364 to->ContextFlags = CONTEXT_i386;
1365 if (from->flags & SERVER_CTX_CONTROL)
1367 to->ContextFlags |= CONTEXT_CONTROL;
1368 to->Ebp = from->ctl.i386_regs.ebp;
1369 to->Esp = from->ctl.i386_regs.esp;
1370 to->Eip = from->ctl.i386_regs.eip;
1371 to->SegCs = from->ctl.i386_regs.cs;
1372 to->SegSs = from->ctl.i386_regs.ss;
1373 to->EFlags = from->ctl.i386_regs.eflags;
1375 if (from->flags & SERVER_CTX_INTEGER)
1377 to->ContextFlags |= CONTEXT_INTEGER;
1378 to->Eax = from->integer.i386_regs.eax;
1379 to->Ebx = from->integer.i386_regs.ebx;
1380 to->Ecx = from->integer.i386_regs.ecx;
1381 to->Edx = from->integer.i386_regs.edx;
1382 to->Esi = from->integer.i386_regs.esi;
1383 to->Edi = from->integer.i386_regs.edi;
1385 if (from->flags & SERVER_CTX_SEGMENTS)
1387 to->ContextFlags |= CONTEXT_SEGMENTS;
1388 to->SegDs = from->seg.i386_regs.ds;
1389 to->SegEs = from->seg.i386_regs.es;
1390 to->SegFs = from->seg.i386_regs.fs;
1391 to->SegGs = from->seg.i386_regs.gs;
1393 if (from->flags & SERVER_CTX_FLOATING_POINT)
1395 to->ContextFlags |= CONTEXT_FLOATING_POINT;
1396 to->FloatSave.ControlWord = from->fp.i386_regs.ctrl;
1397 to->FloatSave.StatusWord = from->fp.i386_regs.status;
1398 to->FloatSave.TagWord = from->fp.i386_regs.tag;
1399 to->FloatSave.ErrorOffset = from->fp.i386_regs.err_off;
1400 to->FloatSave.ErrorSelector = from->fp.i386_regs.err_sel;
1401 to->FloatSave.DataOffset = from->fp.i386_regs.data_off;
1402 to->FloatSave.DataSelector = from->fp.i386_regs.data_sel;
1403 to->FloatSave.Cr0NpxState = from->fp.i386_regs.cr0npx;
1404 memcpy( to->FloatSave.RegisterArea, from->fp.i386_regs.regs, sizeof(to->FloatSave.RegisterArea) );
1406 if (from->flags & SERVER_CTX_DEBUG_REGISTERS)
1408 to->ContextFlags |= CONTEXT_DEBUG_REGISTERS;
1409 to->Dr0 = from->debug.i386_regs.dr0;
1410 to->Dr1 = from->debug.i386_regs.dr1;
1411 to->Dr2 = from->debug.i386_regs.dr2;
1412 to->Dr3 = from->debug.i386_regs.dr3;
1413 to->Dr6 = from->debug.i386_regs.dr6;
1414 to->Dr7 = from->debug.i386_regs.dr7;
1416 if (from->flags & SERVER_CTX_EXTENDED_REGISTERS)
1418 to->ContextFlags |= CONTEXT_EXTENDED_REGISTERS;
1419 memcpy( to->ExtendedRegisters, from->ext.i386_regs, sizeof(to->ExtendedRegisters) );
1421 return STATUS_SUCCESS;
1425 /***********************************************************************
1426 * is_privileged_instr
1428 * Check if the fault location is a privileged instruction.
1429 * Based on the instruction emulation code in dlls/kernel/instr.c.
1431 static inline DWORD is_privileged_instr( CONTEXT86 *context )
1433 const BYTE *instr;
1434 unsigned int prefix_count = 0;
1436 if (!wine_ldt_is_system( context->SegCs )) return 0;
1437 instr = (BYTE *)context->Eip;
1439 for (;;) switch(*instr)
1441 /* instruction prefixes */
1442 case 0x2e: /* %cs: */
1443 case 0x36: /* %ss: */
1444 case 0x3e: /* %ds: */
1445 case 0x26: /* %es: */
1446 case 0x64: /* %fs: */
1447 case 0x65: /* %gs: */
1448 case 0x66: /* opcode size */
1449 case 0x67: /* addr size */
1450 case 0xf0: /* lock */
1451 case 0xf2: /* repne */
1452 case 0xf3: /* repe */
1453 if (++prefix_count >= 15) return EXCEPTION_ILLEGAL_INSTRUCTION;
1454 instr++;
1455 continue;
1457 case 0x0f: /* extended instruction */
1458 switch(instr[1])
1460 case 0x20: /* mov crX, reg */
1461 case 0x21: /* mov drX, reg */
1462 case 0x22: /* mov reg, crX */
1463 case 0x23: /* mov reg drX */
1464 return EXCEPTION_PRIV_INSTRUCTION;
1466 return 0;
1467 case 0x6c: /* insb (%dx) */
1468 case 0x6d: /* insl (%dx) */
1469 case 0x6e: /* outsb (%dx) */
1470 case 0x6f: /* outsl (%dx) */
1471 case 0xcd: /* int $xx */
1472 case 0xe4: /* inb al,XX */
1473 case 0xe5: /* in (e)ax,XX */
1474 case 0xe6: /* outb XX,al */
1475 case 0xe7: /* out XX,(e)ax */
1476 case 0xec: /* inb (%dx),%al */
1477 case 0xed: /* inl (%dx),%eax */
1478 case 0xee: /* outb %al,(%dx) */
1479 case 0xef: /* outl %eax,(%dx) */
1480 case 0xf4: /* hlt */
1481 case 0xfa: /* cli */
1482 case 0xfb: /* sti */
1483 return EXCEPTION_PRIV_INSTRUCTION;
1484 default:
1485 return 0;
1490 #include "pshpack1.h"
1491 struct atl_thunk
1493 DWORD movl; /* movl this,4(%esp) */
1494 DWORD this;
1495 BYTE jmp; /* jmp func */
1496 int func;
1498 #include "poppack.h"
1500 /**********************************************************************
1501 * check_atl_thunk
1503 * Check if code destination is an ATL thunk, and emulate it if so.
1505 static BOOL check_atl_thunk( EXCEPTION_RECORD *rec, CONTEXT *context )
1507 const struct atl_thunk *thunk = (const struct atl_thunk *)rec->ExceptionInformation[1];
1508 BOOL ret = FALSE;
1510 __TRY
1512 if (thunk->movl == 0x042444c7 && thunk->jmp == 0xe9)
1514 *((DWORD *)context->Esp + 1) = thunk->this;
1515 context->Eip = (DWORD_PTR)(&thunk->func + 1) + thunk->func;
1516 TRACE( "emulating ATL thunk at %p, func=%08x arg=%08x\n",
1517 thunk, context->Eip, *((DWORD *)context->Esp + 1) );
1518 ret = TRUE;
1521 __EXCEPT_PAGE_FAULT
1523 return FALSE;
1525 __ENDTRY
1526 return ret;
1530 /***********************************************************************
1531 * setup_exception_record
1533 * Setup the exception record and context on the thread stack.
1535 static EXCEPTION_RECORD *setup_exception_record( SIGCONTEXT *sigcontext, void *stack_ptr,
1536 WORD fs, WORD gs, raise_func func )
1538 struct stack_layout
1540 void *ret_addr; /* return address from raise_func */
1541 EXCEPTION_RECORD *rec_ptr; /* first arg for raise_func */
1542 CONTEXT *context_ptr; /* second arg for raise_func */
1543 CONTEXT context;
1544 EXCEPTION_RECORD rec;
1545 DWORD ebp;
1546 DWORD eip;
1547 } *stack = stack_ptr;
1548 DWORD exception_code = 0;
1550 /* stack sanity checks */
1552 if ((char *)stack >= (char *)get_signal_stack() &&
1553 (char *)stack < (char *)get_signal_stack() + signal_stack_size)
1555 WINE_ERR( "nested exception on signal stack in thread %04x eip %08x esp %08x stack %p-%p\n",
1556 GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1557 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->Tib.StackLimit,
1558 NtCurrentTeb()->Tib.StackBase );
1559 abort_thread(1);
1562 if (stack - 1 > stack || /* check for overflow in subtraction */
1563 (char *)stack <= (char *)NtCurrentTeb()->DeallocationStack ||
1564 (char *)stack > (char *)NtCurrentTeb()->Tib.StackBase)
1566 WARN( "exception outside of stack limits in thread %04x eip %08x esp %08x stack %p-%p\n",
1567 GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1568 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->Tib.StackLimit,
1569 NtCurrentTeb()->Tib.StackBase );
1571 else if ((char *)(stack - 1) < (char *)NtCurrentTeb()->DeallocationStack + 4096)
1573 /* stack overflow on last page, unrecoverable */
1574 UINT diff = (char *)NtCurrentTeb()->DeallocationStack + 4096 - (char *)(stack - 1);
1575 WINE_ERR( "stack overflow %u bytes in thread %04x eip %08x esp %08x stack %p-%p-%p\n",
1576 diff, GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1577 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->DeallocationStack,
1578 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1579 abort_thread(1);
1581 else if ((char *)(stack - 1) < (char *)NtCurrentTeb()->Tib.StackLimit)
1583 /* stack access below stack limit, may be recoverable */
1584 if (virtual_handle_stack_fault( stack - 1 )) exception_code = EXCEPTION_STACK_OVERFLOW;
1585 else
1587 UINT diff = (char *)NtCurrentTeb()->Tib.StackLimit - (char *)(stack - 1);
1588 WINE_ERR( "stack overflow %u bytes in thread %04x eip %08x esp %08x stack %p-%p-%p\n",
1589 diff, GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1590 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->DeallocationStack,
1591 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1592 abort_thread(1);
1596 stack--; /* push the stack_layout structure */
1597 #if defined(VALGRIND_MAKE_MEM_UNDEFINED)
1598 VALGRIND_MAKE_MEM_UNDEFINED(stack, sizeof(*stack));
1599 #elif defined(VALGRIND_MAKE_WRITABLE)
1600 VALGRIND_MAKE_WRITABLE(stack, sizeof(*stack));
1601 #endif
1602 stack->ret_addr = (void *)0xdeadbabe; /* raise_func must not return */
1603 stack->rec_ptr = &stack->rec;
1604 stack->context_ptr = &stack->context;
1606 stack->rec.ExceptionRecord = NULL;
1607 stack->rec.ExceptionCode = exception_code;
1608 stack->rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
1609 stack->rec.ExceptionAddress = (LPVOID)EIP_sig(sigcontext);
1610 stack->rec.NumberParameters = 0;
1612 save_context( &stack->context, sigcontext, fs, gs );
1614 /* now modify the sigcontext to return to the raise function */
1615 ESP_sig(sigcontext) = (DWORD)stack;
1616 EIP_sig(sigcontext) = (DWORD)func;
1617 /* clear single-step, direction, and align check flag */
1618 EFL_sig(sigcontext) &= ~(0x100|0x400|0x40000);
1619 CS_sig(sigcontext) = wine_get_cs();
1620 DS_sig(sigcontext) = wine_get_ds();
1621 ES_sig(sigcontext) = wine_get_es();
1622 FS_sig(sigcontext) = wine_get_fs();
1623 GS_sig(sigcontext) = wine_get_gs();
1624 SS_sig(sigcontext) = wine_get_ss();
1626 return stack->rec_ptr;
1630 /***********************************************************************
1631 * setup_exception
1633 * Setup a proper stack frame for the raise function, and modify the
1634 * sigcontext so that the return from the signal handler will call
1635 * the raise function.
1637 static EXCEPTION_RECORD *setup_exception( SIGCONTEXT *sigcontext, raise_func func )
1639 WORD fs, gs;
1640 void *stack = init_handler( sigcontext, &fs, &gs );
1642 return setup_exception_record( sigcontext, stack, fs, gs, func );
1646 /***********************************************************************
1647 * get_exception_context
1649 * Get a pointer to the context built by setup_exception.
1651 static inline CONTEXT *get_exception_context( EXCEPTION_RECORD *rec )
1653 return (CONTEXT *)rec - 1; /* cf. stack_layout structure */
1657 /**********************************************************************
1658 * get_fpu_code
1660 * Get the FPU exception code from the FPU status.
1662 static inline DWORD get_fpu_code( const CONTEXT *context )
1664 DWORD status = context->FloatSave.StatusWord & ~(context->FloatSave.ControlWord & 0x3f);
1666 if (status & 0x01) /* IE */
1668 if (status & 0x40) /* SF */
1669 return EXCEPTION_FLT_STACK_CHECK;
1670 else
1671 return EXCEPTION_FLT_INVALID_OPERATION;
1673 if (status & 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND; /* DE flag */
1674 if (status & 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO; /* ZE flag */
1675 if (status & 0x08) return EXCEPTION_FLT_OVERFLOW; /* OE flag */
1676 if (status & 0x10) return EXCEPTION_FLT_UNDERFLOW; /* UE flag */
1677 if (status & 0x20) return EXCEPTION_FLT_INEXACT_RESULT; /* PE flag */
1678 return EXCEPTION_FLT_INVALID_OPERATION; /* generic error */
1682 /**********************************************************************
1683 * raise_segv_exception
1685 static void WINAPI raise_segv_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1687 NTSTATUS status;
1689 switch(rec->ExceptionCode)
1691 case EXCEPTION_ACCESS_VIOLATION:
1692 if (rec->NumberParameters == 2)
1694 if (rec->ExceptionInformation[0] == EXCEPTION_EXECUTE_FAULT && check_atl_thunk( rec, context ))
1695 goto done;
1696 if (!(rec->ExceptionCode = virtual_handle_fault( (void *)rec->ExceptionInformation[1],
1697 rec->ExceptionInformation[0] )))
1698 goto done;
1699 /* send EXCEPTION_EXECUTE_FAULT only if data execution prevention is enabled */
1700 if (rec->ExceptionInformation[0] == EXCEPTION_EXECUTE_FAULT)
1702 ULONG flags;
1703 NtQueryInformationProcess( GetCurrentProcess(), ProcessExecuteFlags,
1704 &flags, sizeof(flags), NULL );
1705 if (!(flags & MEM_EXECUTE_OPTION_DISABLE))
1706 rec->ExceptionInformation[0] = EXCEPTION_READ_FAULT;
1709 break;
1710 case EXCEPTION_DATATYPE_MISALIGNMENT:
1711 /* FIXME: pass through exception handler first? */
1712 if (context->EFlags & 0x00040000)
1714 /* Disable AC flag, return */
1715 context->EFlags &= ~0x00040000;
1716 goto done;
1718 break;
1720 status = NtRaiseException( rec, context, TRUE );
1721 raise_status( status, rec );
1722 done:
1723 set_cpu_context( context );
1727 /**********************************************************************
1728 * raise_trap_exception
1730 static void WINAPI raise_trap_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1732 NTSTATUS status;
1734 if (rec->ExceptionCode == EXCEPTION_SINGLE_STEP)
1736 /* when single stepping can't tell whether this is a hw bp or a
1737 * single step interrupt. try to avoid as much overhead as possible
1738 * and only do a server call if there is any hw bp enabled. */
1740 if( !(context->EFlags & 0x100) || (ntdll_get_thread_data()->dr7 & 0xff) )
1742 /* (possible) hardware breakpoint, fetch the debug registers */
1743 context->ContextFlags = CONTEXT_DEBUG_REGISTERS;
1744 NtGetContextThread(GetCurrentThread(), context);
1745 context->ContextFlags |= CONTEXT_FULL; /* restore flags */
1748 context->EFlags &= ~0x100; /* clear single-step flag */
1751 status = NtRaiseException( rec, context, TRUE );
1752 raise_status( status, rec );
1756 /**********************************************************************
1757 * raise_generic_exception
1759 * Generic raise function for exceptions that don't need special treatment.
1761 static void WINAPI raise_generic_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1763 NTSTATUS status;
1765 status = NtRaiseException( rec, context, TRUE );
1766 raise_status( status, rec );
1770 #ifdef __HAVE_VM86
1771 /**********************************************************************
1772 * raise_vm86_sti_exception
1774 static void WINAPI raise_vm86_sti_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1776 /* merge_vm86_pending_flags merges the vm86_pending flag in safely */
1777 get_vm86_teb_info()->vm86_pending |= VIP_FLAG;
1779 if (ntdll_get_thread_data()->vm86_ptr)
1781 if (((char*)context->Eip >= (char*)vm86_return) &&
1782 ((char*)context->Eip <= (char*)vm86_return_end) &&
1783 (VM86_TYPE(context->Eax) != VM86_SIGNAL)) {
1784 /* exiting from VM86, can't throw */
1785 goto done;
1787 merge_vm86_pending_flags( rec );
1789 else if (get_vm86_teb_info()->dpmi_vif &&
1790 !wine_ldt_is_system(context->SegCs) &&
1791 !wine_ldt_is_system(context->SegSs))
1793 /* Executing DPMI code and virtual interrupts are enabled. */
1794 get_vm86_teb_info()->vm86_pending = 0;
1795 NtRaiseException( rec, context, TRUE );
1797 done:
1798 set_cpu_context( context );
1802 /**********************************************************************
1803 * usr2_handler
1805 * Handler for SIGUSR2.
1806 * We use it to signal that the running __wine_enter_vm86() should
1807 * immediately set VIP_FLAG, causing pending events to be handled
1808 * as early as possible.
1810 static void usr2_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1812 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_vm86_sti_exception );
1813 rec->ExceptionCode = EXCEPTION_VM86_STI;
1815 #endif /* __HAVE_VM86 */
1818 /**********************************************************************
1819 * segv_handler
1821 * Handler for SIGSEGV and related errors.
1823 static void segv_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1825 WORD fs, gs;
1826 EXCEPTION_RECORD *rec;
1827 SIGCONTEXT *context = sigcontext;
1828 void *stack = init_handler( sigcontext, &fs, &gs );
1830 /* check for page fault inside the thread stack */
1831 if (get_trap_code(context) == TRAP_x86_PAGEFLT &&
1832 (char *)siginfo->si_addr >= (char *)NtCurrentTeb()->DeallocationStack &&
1833 (char *)siginfo->si_addr < (char *)NtCurrentTeb()->Tib.StackBase &&
1834 virtual_handle_stack_fault( siginfo->si_addr ))
1836 /* check if this was the last guard page */
1837 if ((char *)siginfo->si_addr < (char *)NtCurrentTeb()->DeallocationStack + 2*4096)
1839 rec = setup_exception_record( context, stack, fs, gs, raise_segv_exception );
1840 rec->ExceptionCode = EXCEPTION_STACK_OVERFLOW;
1842 return;
1845 rec = setup_exception_record( context, stack, fs, gs, raise_segv_exception );
1846 if (rec->ExceptionCode == EXCEPTION_STACK_OVERFLOW) return;
1848 switch(get_trap_code(context))
1850 case TRAP_x86_OFLOW: /* Overflow exception */
1851 rec->ExceptionCode = EXCEPTION_INT_OVERFLOW;
1852 break;
1853 case TRAP_x86_BOUND: /* Bound range exception */
1854 rec->ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
1855 break;
1856 case TRAP_x86_PRIVINFLT: /* Invalid opcode exception */
1857 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
1858 break;
1859 case TRAP_x86_STKFLT: /* Stack fault */
1860 rec->ExceptionCode = EXCEPTION_STACK_OVERFLOW;
1861 break;
1862 case TRAP_x86_SEGNPFLT: /* Segment not present exception */
1863 case TRAP_x86_PROTFLT: /* General protection fault */
1864 case TRAP_x86_UNKNOWN: /* Unknown fault code */
1866 WORD err = get_error_code(context);
1867 if (!err && (rec->ExceptionCode = is_privileged_instr( get_exception_context(rec) ))) break;
1868 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
1869 rec->NumberParameters = 2;
1870 rec->ExceptionInformation[0] = 0;
1871 /* if error contains a LDT selector, use that as fault address */
1872 if ((err & 7) == 4 && !wine_ldt_is_system( err | 7 ))
1873 rec->ExceptionInformation[1] = err & ~7;
1874 else
1875 rec->ExceptionInformation[1] = 0xffffffff;
1877 break;
1878 case TRAP_x86_PAGEFLT: /* Page fault */
1879 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
1880 rec->NumberParameters = 2;
1881 rec->ExceptionInformation[0] = (get_error_code(context) >> 1) & 0x09;
1882 rec->ExceptionInformation[1] = (ULONG_PTR)siginfo->si_addr;
1883 break;
1884 case TRAP_x86_ALIGNFLT: /* Alignment check exception */
1885 rec->ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
1886 break;
1887 default:
1888 WINE_ERR( "Got unexpected trap %d\n", get_trap_code(context) );
1889 /* fall through */
1890 case TRAP_x86_NMI: /* NMI interrupt */
1891 case TRAP_x86_DNA: /* Device not available exception */
1892 case TRAP_x86_DOUBLEFLT: /* Double fault exception */
1893 case TRAP_x86_TSSFLT: /* Invalid TSS exception */
1894 case TRAP_x86_MCHK: /* Machine check exception */
1895 case TRAP_x86_CACHEFLT: /* Cache flush exception */
1896 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
1897 break;
1902 /**********************************************************************
1903 * trap_handler
1905 * Handler for SIGTRAP.
1907 static void trap_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1909 SIGCONTEXT *context = sigcontext;
1910 EXCEPTION_RECORD *rec = setup_exception( context, raise_trap_exception );
1912 switch(get_trap_code(context))
1914 case TRAP_x86_TRCTRAP: /* Single-step exception */
1915 rec->ExceptionCode = EXCEPTION_SINGLE_STEP;
1916 break;
1917 case TRAP_x86_BPTFLT: /* Breakpoint exception */
1918 rec->ExceptionAddress = (char *)rec->ExceptionAddress - 1; /* back up over the int3 instruction */
1919 /* fall through */
1920 default:
1921 rec->ExceptionCode = EXCEPTION_BREAKPOINT;
1922 break;
1927 /**********************************************************************
1928 * fpe_handler
1930 * Handler for SIGFPE.
1932 static void fpe_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1934 CONTEXT *win_context;
1935 SIGCONTEXT *context = sigcontext;
1936 EXCEPTION_RECORD *rec = setup_exception( context, raise_generic_exception );
1938 win_context = get_exception_context( rec );
1940 switch(get_trap_code(context))
1942 case TRAP_x86_DIVIDE: /* Division by zero exception */
1943 rec->ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
1944 break;
1945 case TRAP_x86_FPOPFLT: /* Coprocessor segment overrun */
1946 rec->ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
1947 break;
1948 case TRAP_x86_ARITHTRAP: /* Floating point exception */
1949 case TRAP_x86_UNKNOWN: /* Unknown fault code */
1950 rec->ExceptionCode = get_fpu_code( win_context );
1951 rec->ExceptionAddress = (LPVOID)win_context->FloatSave.ErrorOffset;
1952 break;
1953 case TRAP_x86_CACHEFLT: /* SIMD exception */
1954 /* TODO:
1955 * Behaviour only tested for divide-by-zero exceptions
1956 * Check for other SIMD exceptions as well */
1957 if(siginfo->si_code != FPE_FLTDIV)
1958 FIXME("untested SIMD exception: %#x. Might not work correctly\n",
1959 siginfo->si_code);
1961 rec->ExceptionCode = STATUS_FLOAT_MULTIPLE_TRAPS;
1962 rec->NumberParameters = 1;
1963 /* no idea what meaning is actually behind this but that's what native does */
1964 rec->ExceptionInformation[0] = 0;
1965 break;
1966 default:
1967 WINE_ERR( "Got unexpected trap %d\n", get_trap_code(context) );
1968 rec->ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
1969 break;
1974 /**********************************************************************
1975 * int_handler
1977 * Handler for SIGINT.
1979 * FIXME: should not be calling external functions on the signal stack.
1981 static void int_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1983 WORD fs, gs;
1984 init_handler( sigcontext, &fs, &gs );
1985 if (!dispatch_signal(SIGINT))
1987 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_generic_exception );
1988 rec->ExceptionCode = CONTROL_C_EXIT;
1992 /**********************************************************************
1993 * abrt_handler
1995 * Handler for SIGABRT.
1997 static void abrt_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1999 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_generic_exception );
2000 rec->ExceptionCode = EXCEPTION_WINE_ASSERTION;
2001 rec->ExceptionFlags = EH_NONCONTINUABLE;
2005 /**********************************************************************
2006 * quit_handler
2008 * Handler for SIGQUIT.
2010 static void quit_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2012 WORD fs, gs;
2013 init_handler( sigcontext, &fs, &gs );
2014 abort_thread(0);
2018 /**********************************************************************
2019 * usr1_handler
2021 * Handler for SIGUSR1, used to signal a thread that it got suspended.
2023 static void usr1_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2025 CONTEXT context;
2026 WORD fs, gs;
2028 init_handler( sigcontext, &fs, &gs );
2029 save_context( &context, sigcontext, fs, gs );
2030 wait_suspend( &context );
2031 restore_context( &context, sigcontext );
2035 /***********************************************************************
2036 * __wine_set_signal_handler (NTDLL.@)
2038 int CDECL __wine_set_signal_handler(unsigned int sig, wine_signal_handler wsh)
2040 if (sig >= sizeof(handlers) / sizeof(handlers[0])) return -1;
2041 if (handlers[sig] != NULL) return -2;
2042 handlers[sig] = wsh;
2043 return 0;
2047 /***********************************************************************
2048 * locking for LDT routines
2050 static RTL_CRITICAL_SECTION ldt_section;
2051 static RTL_CRITICAL_SECTION_DEBUG critsect_debug =
2053 0, 0, &ldt_section,
2054 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
2055 0, 0, { (DWORD_PTR)(__FILE__ ": ldt_section") }
2057 static RTL_CRITICAL_SECTION ldt_section = { &critsect_debug, -1, 0, 0, 0, 0 };
2058 static sigset_t ldt_sigset;
2060 static void ldt_lock(void)
2062 sigset_t sigset;
2064 pthread_sigmask( SIG_BLOCK, &server_block_set, &sigset );
2065 RtlEnterCriticalSection( &ldt_section );
2066 if (ldt_section.RecursionCount == 1) ldt_sigset = sigset;
2069 static void ldt_unlock(void)
2071 if (ldt_section.RecursionCount == 1)
2073 sigset_t sigset = ldt_sigset;
2074 RtlLeaveCriticalSection( &ldt_section );
2075 pthread_sigmask( SIG_SETMASK, &sigset, NULL );
2077 else RtlLeaveCriticalSection( &ldt_section );
2081 /**********************************************************************
2082 * signal_alloc_thread
2084 NTSTATUS signal_alloc_thread( TEB **teb )
2086 static size_t sigstack_zero_bits;
2087 struct ntdll_thread_data *thread_data;
2088 struct ntdll_thread_data *parent_data = NULL;
2089 SIZE_T size;
2090 void *addr = NULL;
2091 NTSTATUS status;
2093 if (!sigstack_zero_bits)
2095 size_t min_size = teb_size + max( MINSIGSTKSZ, 8192 );
2096 /* find the first power of two not smaller than min_size */
2097 sigstack_zero_bits = 12;
2098 while ((1u << sigstack_zero_bits) < min_size) sigstack_zero_bits++;
2099 signal_stack_mask = (1 << sigstack_zero_bits) - 1;
2100 signal_stack_size = (1 << sigstack_zero_bits) - teb_size;
2102 else parent_data = ntdll_get_thread_data();
2104 size = signal_stack_mask + 1;
2105 if (!(status = NtAllocateVirtualMemory( NtCurrentProcess(), &addr, sigstack_zero_bits,
2106 &size, MEM_COMMIT | MEM_TOP_DOWN, PAGE_READWRITE )))
2108 *teb = addr;
2109 (*teb)->Tib.Self = &(*teb)->Tib;
2110 (*teb)->Tib.ExceptionList = (void *)~0UL;
2111 thread_data = (struct ntdll_thread_data *)(*teb)->SpareBytes1;
2112 if (!(thread_data->fs = wine_ldt_alloc_fs()))
2114 size = 0;
2115 NtFreeVirtualMemory( NtCurrentProcess(), &addr, &size, MEM_RELEASE );
2116 status = STATUS_TOO_MANY_THREADS;
2118 if (parent_data)
2120 /* inherit debug registers from parent thread */
2121 thread_data->dr0 = parent_data->dr0;
2122 thread_data->dr1 = parent_data->dr1;
2123 thread_data->dr2 = parent_data->dr2;
2124 thread_data->dr3 = parent_data->dr3;
2125 thread_data->dr6 = parent_data->dr6;
2126 thread_data->dr7 = parent_data->dr7;
2130 return status;
2134 /**********************************************************************
2135 * signal_free_thread
2137 void signal_free_thread( TEB *teb )
2139 SIZE_T size;
2140 struct ntdll_thread_data *thread_data = (struct ntdll_thread_data *)teb->SpareBytes1;
2142 if (thread_data) wine_ldt_free_fs( thread_data->fs );
2143 if (teb->DeallocationStack)
2145 size = 0;
2146 NtFreeVirtualMemory( GetCurrentProcess(), &teb->DeallocationStack, &size, MEM_RELEASE );
2148 size = 0;
2149 NtFreeVirtualMemory( NtCurrentProcess(), (void **)&teb, &size, MEM_RELEASE );
2153 /**********************************************************************
2154 * signal_init_thread
2156 void signal_init_thread( TEB *teb )
2158 const WORD fpu_cw = 0x27f;
2159 struct ntdll_thread_data *thread_data = (struct ntdll_thread_data *)teb->SpareBytes1;
2160 LDT_ENTRY fs_entry;
2161 stack_t ss;
2163 #ifdef __APPLE__
2164 int mib[2], val = 1;
2166 mib[0] = CTL_KERN;
2167 mib[1] = KERN_THALTSTACK;
2168 sysctl( mib, 2, NULL, NULL, &val, sizeof(val) );
2169 #endif
2171 ss.ss_sp = (char *)teb + teb_size;
2172 ss.ss_size = signal_stack_size;
2173 ss.ss_flags = 0;
2174 if (sigaltstack(&ss, NULL) == -1) perror( "sigaltstack" );
2176 wine_ldt_set_base( &fs_entry, teb );
2177 wine_ldt_set_limit( &fs_entry, teb_size - 1 );
2178 wine_ldt_set_flags( &fs_entry, WINE_LDT_FLAGS_DATA|WINE_LDT_FLAGS_32BIT );
2179 wine_ldt_init_fs( thread_data->fs, &fs_entry );
2180 thread_data->gs = wine_get_gs();
2182 #ifdef __GNUC__
2183 __asm__ volatile ("fninit; fldcw %0" : : "m" (fpu_cw));
2184 #else
2185 FIXME("FPU setup not implemented for this platform.\n");
2186 #endif
2189 /**********************************************************************
2190 * signal_init_process
2192 void signal_init_process(void)
2194 struct sigaction sig_act;
2196 sig_act.sa_mask = server_block_set;
2197 sig_act.sa_flags = SA_SIGINFO | SA_RESTART;
2198 #ifdef SA_ONSTACK
2199 sig_act.sa_flags |= SA_ONSTACK;
2200 #endif
2202 sig_act.sa_sigaction = int_handler;
2203 if (sigaction( SIGINT, &sig_act, NULL ) == -1) goto error;
2204 sig_act.sa_sigaction = fpe_handler;
2205 if (sigaction( SIGFPE, &sig_act, NULL ) == -1) goto error;
2206 sig_act.sa_sigaction = abrt_handler;
2207 if (sigaction( SIGABRT, &sig_act, NULL ) == -1) goto error;
2208 sig_act.sa_sigaction = quit_handler;
2209 if (sigaction( SIGQUIT, &sig_act, NULL ) == -1) goto error;
2210 sig_act.sa_sigaction = usr1_handler;
2211 if (sigaction( SIGUSR1, &sig_act, NULL ) == -1) goto error;
2213 sig_act.sa_sigaction = segv_handler;
2214 if (sigaction( SIGSEGV, &sig_act, NULL ) == -1) goto error;
2215 if (sigaction( SIGILL, &sig_act, NULL ) == -1) goto error;
2216 #ifdef SIGBUS
2217 if (sigaction( SIGBUS, &sig_act, NULL ) == -1) goto error;
2218 #endif
2220 #ifdef SIGTRAP
2221 sig_act.sa_sigaction = trap_handler;
2222 if (sigaction( SIGTRAP, &sig_act, NULL ) == -1) goto error;
2223 #endif
2225 #ifdef __HAVE_VM86
2226 sig_act.sa_sigaction = usr2_handler;
2227 if (sigaction( SIGUSR2, &sig_act, NULL ) == -1) goto error;
2228 #endif
2230 wine_ldt_init_locking( ldt_lock, ldt_unlock );
2231 return;
2233 error:
2234 perror("sigaction");
2235 exit(1);
2239 #ifdef __HAVE_VM86
2240 /**********************************************************************
2241 * __wine_enter_vm86 (NTDLL.@)
2243 * Enter vm86 mode with the specified register context.
2245 void __wine_enter_vm86( CONTEXT *context )
2247 EXCEPTION_RECORD rec;
2248 int res;
2249 struct vm86plus_struct vm86;
2251 memset( &vm86, 0, sizeof(vm86) );
2252 for (;;)
2254 restore_vm86_context( context, &vm86 );
2256 ntdll_get_thread_data()->vm86_ptr = &vm86;
2257 merge_vm86_pending_flags( &rec );
2259 res = vm86_enter( &ntdll_get_thread_data()->vm86_ptr ); /* uses and clears teb->vm86_ptr */
2260 if (res < 0)
2262 errno = -res;
2263 return;
2266 save_vm86_context( context, &vm86 );
2268 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
2269 rec.ExceptionRecord = NULL;
2270 rec.ExceptionAddress = (LPVOID)context->Eip;
2271 rec.NumberParameters = 0;
2273 switch(VM86_TYPE(res))
2275 case VM86_UNKNOWN: /* unhandled GP fault - IO-instruction or similar */
2276 rec.ExceptionCode = EXCEPTION_PRIV_INSTRUCTION;
2277 break;
2278 case VM86_TRAP: /* return due to DOS-debugger request */
2279 switch(VM86_ARG(res))
2281 case TRAP_x86_TRCTRAP: /* Single-step exception */
2282 rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
2283 break;
2284 case TRAP_x86_BPTFLT: /* Breakpoint exception */
2285 rec.ExceptionAddress = (char *)rec.ExceptionAddress - 1; /* back up over the int3 instruction */
2286 /* fall through */
2287 default:
2288 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
2289 break;
2291 break;
2292 case VM86_INTx: /* int3/int x instruction (ARG = x) */
2293 rec.ExceptionCode = EXCEPTION_VM86_INTx;
2294 rec.NumberParameters = 1;
2295 rec.ExceptionInformation[0] = VM86_ARG(res);
2296 break;
2297 case VM86_STI: /* sti/popf/iret instruction enabled virtual interrupts */
2298 context->EFlags |= VIF_FLAG;
2299 context->EFlags &= ~VIP_FLAG;
2300 get_vm86_teb_info()->vm86_pending = 0;
2301 rec.ExceptionCode = EXCEPTION_VM86_STI;
2302 break;
2303 case VM86_PICRETURN: /* return due to pending PIC request */
2304 rec.ExceptionCode = EXCEPTION_VM86_PICRETURN;
2305 break;
2306 case VM86_SIGNAL: /* cannot happen because vm86_enter handles this case */
2307 default:
2308 WINE_ERR( "unhandled result from vm86 mode %x\n", res );
2309 continue;
2311 raise_exception( &rec, context, TRUE );
2315 #else /* __HAVE_VM86 */
2316 /**********************************************************************
2317 * __wine_enter_vm86 (NTDLL.@)
2319 void __wine_enter_vm86( CONTEXT *context )
2321 MESSAGE("vm86 mode not supported on this platform\n");
2323 #endif /* __HAVE_VM86 */
2326 /*******************************************************************
2327 * RtlUnwind (NTDLL.@)
2329 void WINAPI __regs_RtlUnwind( EXCEPTION_REGISTRATION_RECORD* pEndFrame, PVOID targetIp,
2330 PEXCEPTION_RECORD pRecord, PVOID retval, CONTEXT *context )
2332 EXCEPTION_RECORD record;
2333 EXCEPTION_REGISTRATION_RECORD *frame, *dispatch;
2334 DWORD res;
2336 context->Eax = (DWORD)retval;
2338 /* build an exception record, if we do not have one */
2339 if (!pRecord)
2341 record.ExceptionCode = STATUS_UNWIND;
2342 record.ExceptionFlags = 0;
2343 record.ExceptionRecord = NULL;
2344 record.ExceptionAddress = (void *)context->Eip;
2345 record.NumberParameters = 0;
2346 pRecord = &record;
2349 pRecord->ExceptionFlags |= EH_UNWINDING | (pEndFrame ? 0 : EH_EXIT_UNWIND);
2351 TRACE( "code=%x flags=%x\n", pRecord->ExceptionCode, pRecord->ExceptionFlags );
2353 /* get chain of exception frames */
2354 frame = NtCurrentTeb()->Tib.ExceptionList;
2355 while ((frame != (EXCEPTION_REGISTRATION_RECORD*)~0UL) && (frame != pEndFrame))
2357 /* Check frame address */
2358 if (pEndFrame && (frame > pEndFrame))
2359 raise_status( STATUS_INVALID_UNWIND_TARGET, pRecord );
2361 if (((void*)frame < NtCurrentTeb()->Tib.StackLimit) ||
2362 ((void*)(frame+1) > NtCurrentTeb()->Tib.StackBase) ||
2363 (UINT_PTR)frame & 3)
2364 raise_status( STATUS_BAD_STACK, pRecord );
2366 /* Call handler */
2367 TRACE( "calling handler at %p code=%x flags=%x\n",
2368 frame->Handler, pRecord->ExceptionCode, pRecord->ExceptionFlags );
2369 res = EXC_CallHandler( pRecord, frame, context, &dispatch, frame->Handler, unwind_handler );
2370 TRACE( "handler at %p returned %x\n", frame->Handler, res );
2372 switch(res)
2374 case ExceptionContinueSearch:
2375 break;
2376 case ExceptionCollidedUnwind:
2377 frame = dispatch;
2378 break;
2379 default:
2380 raise_status( STATUS_INVALID_DISPOSITION, pRecord );
2381 break;
2383 frame = __wine_pop_frame( frame );
2386 DEFINE_REGS_ENTRYPOINT( RtlUnwind, 4 )
2389 /*******************************************************************
2390 * NtRaiseException (NTDLL.@)
2392 NTSTATUS WINAPI NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context, BOOL first_chance )
2394 NTSTATUS status = raise_exception( rec, context, first_chance );
2395 if (status == STATUS_SUCCESS)
2397 set_debug_registers( context );
2398 set_cpu_context( context );
2400 return status;
2404 /***********************************************************************
2405 * RtlRaiseException (NTDLL.@)
2407 void WINAPI __regs_RtlRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context )
2409 NTSTATUS status;
2411 rec->ExceptionAddress = (void *)context->Eip;
2412 status = raise_exception( rec, context, TRUE );
2413 if (status != STATUS_SUCCESS) raise_status( status, rec );
2415 DEFINE_REGS_ENTRYPOINT( RtlRaiseException, 1 )
2418 /*************************************************************************
2419 * RtlCaptureStackBackTrace (NTDLL.@)
2421 USHORT WINAPI RtlCaptureStackBackTrace( ULONG skip, ULONG count, PVOID *buffer, ULONG *hash )
2423 CONTEXT context;
2424 ULONG i;
2425 ULONG *frame;
2427 RtlCaptureContext( &context );
2428 if (hash) *hash = 0;
2429 frame = (ULONG *)context.Ebp;
2431 while (skip--)
2433 if (((void *)frame < NtCurrentTeb()->Tib.StackLimit) ||
2434 ((void *)(frame + 1) >= NtCurrentTeb()->Tib.StackBase)) return 0;
2435 frame = (ULONG *)*frame;
2438 for (i = 0; i < count; i++)
2440 if (((void *)frame < NtCurrentTeb()->Tib.StackLimit) ||
2441 ((void *)(frame + 1) >= NtCurrentTeb()->Tib.StackBase)) break;
2442 buffer[i] = (void *)frame[1];
2443 if (hash) *hash += frame[1];
2444 frame = (ULONG *)*frame;
2446 return i;
2450 /* wrapper for apps that don't declare the thread function correctly */
2451 extern void DECLSPEC_NORETURN call_thread_func( LPTHREAD_START_ROUTINE entry, void *arg );
2452 __ASM_GLOBAL_FUNC(call_thread_func,
2453 "pushl %ebp\n\t"
2454 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2455 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2456 "movl %esp,%ebp\n\t"
2457 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2458 "subl $4,%esp\n\t"
2459 "pushl 12(%ebp)\n\t"
2460 "call *8(%ebp)\n\t"
2461 "leal -4(%ebp),%esp\n\t"
2462 "pushl %eax\n\t"
2463 "call " __ASM_NAME("exit_thread") "\n\t"
2464 "int $3" )
2466 /***********************************************************************
2467 * call_thread_entry_point
2469 void call_thread_entry_point( LPTHREAD_START_ROUTINE entry, void *arg )
2471 __TRY
2473 call_thread_func( entry, arg );
2475 __EXCEPT(unhandled_exception_filter)
2477 NtTerminateThread( GetCurrentThread(), GetExceptionCode() );
2479 __ENDTRY
2480 abort(); /* should not be reached */
2483 /***********************************************************************
2484 * RtlExitUserThread (NTDLL.@)
2486 void WINAPI RtlExitUserThread( ULONG status )
2488 exit_thread( status );
2491 /***********************************************************************
2492 * abort_thread
2494 void abort_thread( int status )
2496 terminate_thread( status );
2499 /**********************************************************************
2500 * DbgBreakPoint (NTDLL.@)
2502 __ASM_STDCALL_FUNC( DbgBreakPoint, 0, "int $3; ret")
2504 /**********************************************************************
2505 * DbgUserBreakPoint (NTDLL.@)
2507 __ASM_STDCALL_FUNC( DbgUserBreakPoint, 0, "int $3; ret")
2509 /**********************************************************************
2510 * NtCurrentTeb (NTDLL.@)
2512 __ASM_STDCALL_FUNC( NtCurrentTeb, 0, ".byte 0x64\n\tmovl 0x18,%eax\n\tret" )
2515 /**********************************************************************
2516 * EXC_CallHandler (internal)
2518 * Some exception handlers depend on EBP to have a fixed position relative to
2519 * the exception frame.
2520 * Shrinker depends on (*1) doing what it does,
2521 * (*2) being the exact instruction it is and (*3) beginning with 0x64
2522 * (i.e. the %fs prefix to the movl instruction). It also depends on the
2523 * function calling the handler having only 5 parameters (*4).
2525 __ASM_GLOBAL_FUNC( EXC_CallHandler,
2526 "pushl %ebp\n\t"
2527 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2528 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2529 "movl %esp,%ebp\n\t"
2530 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2531 "pushl %ebx\n\t"
2532 __ASM_CFI(".cfi_rel_offset %ebx,-4\n\t")
2533 "movl 28(%ebp), %edx\n\t" /* ugly hack to pass the 6th param needed because of Shrinker */
2534 "pushl 24(%ebp)\n\t"
2535 "pushl 20(%ebp)\n\t"
2536 "pushl 16(%ebp)\n\t"
2537 "pushl 12(%ebp)\n\t"
2538 "pushl 8(%ebp)\n\t"
2539 "call " __ASM_NAME("call_exception_handler") "\n\t"
2540 "popl %ebx\n\t"
2541 __ASM_CFI(".cfi_same_value %ebx\n\t")
2542 "leave\n"
2543 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
2544 __ASM_CFI(".cfi_same_value %ebp\n\t")
2545 "ret" )
2546 __ASM_GLOBAL_FUNC(call_exception_handler,
2547 "pushl %ebp\n\t"
2548 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2549 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2550 "movl %esp,%ebp\n\t"
2551 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2552 "subl $12,%esp\n\t"
2553 "pushl 12(%ebp)\n\t" /* make any exceptions in this... */
2554 "pushl %edx\n\t" /* handler be handled by... */
2555 ".byte 0x64\n\t"
2556 "pushl (0)\n\t" /* nested_handler (passed in edx). */
2557 ".byte 0x64\n\t"
2558 "movl %esp,(0)\n\t" /* push the new exception frame onto the exception stack. */
2559 "pushl 20(%ebp)\n\t"
2560 "pushl 16(%ebp)\n\t"
2561 "pushl 12(%ebp)\n\t"
2562 "pushl 8(%ebp)\n\t"
2563 "movl 24(%ebp), %ecx\n\t" /* (*1) */
2564 "call *%ecx\n\t" /* call handler. (*2) */
2565 ".byte 0x64\n\t"
2566 "movl (0), %esp\n\t" /* restore previous... (*3) */
2567 ".byte 0x64\n\t"
2568 "popl (0)\n\t" /* exception frame. */
2569 "movl %ebp, %esp\n\t" /* restore saved stack, in case it was corrupted */
2570 "popl %ebp\n\t"
2571 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
2572 __ASM_CFI(".cfi_same_value %ebp\n\t")
2573 "ret $20" ) /* (*4) */
2575 #endif /* __i386__ */