ntdll: Use standard siginfo signal handlers on all platforms.
[wine/multimedia.git] / dlls / ntdll / signal_x86_64.c
blob8e9b8c40424fa0741b7e55972a603b19de43f799
1 /*
2 * x86-64 signal handling routines
4 * Copyright 1999, 2005 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 __x86_64__
23 #include "config.h"
24 #include "wine/port.h"
26 #include <assert.h>
27 #include <signal.h>
28 #include <stdlib.h>
29 #include <stdarg.h>
30 #include <stdio.h>
31 #ifdef HAVE_UNISTD_H
32 # include <unistd.h>
33 #endif
35 #ifdef HAVE_SYS_PARAM_H
36 # include <sys/param.h>
37 #endif
38 #ifdef HAVE_SYS_SIGNAL_H
39 # include <sys/signal.h>
40 #endif
42 #define NONAMELESSUNION
43 #include "ntstatus.h"
44 #define WIN32_NO_STATUS
45 #include "windef.h"
46 #include "winternl.h"
47 #include "wine/library.h"
48 #include "wine/exception.h"
49 #include "ntdll_misc.h"
50 #include "wine/debug.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(seh);
55 /***********************************************************************
56 * signal context platform-specific definitions
58 #ifdef linux
60 #include <asm/prctl.h>
61 extern int arch_prctl(int func, void *ptr);
63 #define RAX_sig(context) ((context)->uc_mcontext.gregs[REG_RAX])
64 #define RBX_sig(context) ((context)->uc_mcontext.gregs[REG_RBX])
65 #define RCX_sig(context) ((context)->uc_mcontext.gregs[REG_RCX])
66 #define RDX_sig(context) ((context)->uc_mcontext.gregs[REG_RDX])
67 #define RSI_sig(context) ((context)->uc_mcontext.gregs[REG_RSI])
68 #define RDI_sig(context) ((context)->uc_mcontext.gregs[REG_RDI])
69 #define RBP_sig(context) ((context)->uc_mcontext.gregs[REG_RBP])
70 #define R8_sig(context) ((context)->uc_mcontext.gregs[REG_R8])
71 #define R9_sig(context) ((context)->uc_mcontext.gregs[REG_R9])
72 #define R10_sig(context) ((context)->uc_mcontext.gregs[REG_R10])
73 #define R11_sig(context) ((context)->uc_mcontext.gregs[REG_R11])
74 #define R12_sig(context) ((context)->uc_mcontext.gregs[REG_R12])
75 #define R13_sig(context) ((context)->uc_mcontext.gregs[REG_R13])
76 #define R14_sig(context) ((context)->uc_mcontext.gregs[REG_R14])
77 #define R15_sig(context) ((context)->uc_mcontext.gregs[REG_R15])
79 #define CS_sig(context) (*((WORD *)&(context)->uc_mcontext.gregs[REG_CSGSFS] + 0))
80 #define GS_sig(context) (*((WORD *)&(context)->uc_mcontext.gregs[REG_CSGSFS] + 1))
81 #define FS_sig(context) (*((WORD *)&(context)->uc_mcontext.gregs[REG_CSGSFS] + 2))
83 #define RSP_sig(context) ((context)->uc_mcontext.gregs[REG_RSP])
84 #define RIP_sig(context) ((context)->uc_mcontext.gregs[REG_RIP])
85 #define EFL_sig(context) ((context)->uc_mcontext.gregs[REG_EFL])
86 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
87 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
89 #define FPU_sig(context) ((XMM_SAVE_AREA32 *)((context)->uc_mcontext.fpregs))
91 #endif /* linux */
93 #if defined(__NetBSD__)
94 # include <sys/ucontext.h>
95 # include <sys/types.h>
96 # include <signal.h>
98 #define RAX_sig(context) ((context)->uc_mcontext.__gregs[_REG_RAX])
99 #define RBX_sig(context) ((context)->uc_mcontext.__gregs[_REG_RBX])
100 #define RCX_sig(context) ((context)->uc_mcontext.__gregs[_REG_RCX])
101 #define RDX_sig(context) ((context)->uc_mcontext.__gregs[_REG_RDX])
102 #define RSI_sig(context) ((context)->uc_mcontext.__gregs[_REG_RSI])
103 #define RDI_sig(context) ((context)->uc_mcontext.__gregs[_REG_RDI])
104 #define RBP_sig(context) ((context)->uc_mcontext.__gregs[_REG_RBP])
105 #define R8_sig(context) ((context)->uc_mcontext.__gregs[_REG_R8])
106 #define R9_sig(context) ((context)->uc_mcontext.__gregs[_REG_R9])
107 #define R10_sig(context) ((context)->uc_mcontext.__gregs[_REG_R10])
108 #define R11_sig(context) ((context)->uc_mcontext.__gregs[_REG_R11])
109 #define R12_sig(context) ((context)->uc_mcontext.__gregs[_REG_R12])
110 #define R13_sig(context) ((context)->uc_mcontext.__gregs[_REG_R13])
111 #define R14_sig(context) ((context)->uc_mcontext.__gregs[_REG_R14])
112 #define R15_sig(context) ((context)->uc_mcontext.__gregs[_REG_R15])
114 #define CS_sig(context) ((context)->uc_mcontext.__gregs[_REG_CS])
115 #define DS_sig(context) ((context)->uc_mcontext.__gregs[_REG_DS])
116 #define ES_sig(context) ((context)->uc_mcontext.__gregs[_REG_ES])
117 #define FS_sig(context) ((context)->uc_mcontext.__gregs[_REG_FS])
118 #define GS_sig(context) ((context)->uc_mcontext.__gregs[_REG_GS])
119 #define SS_sig(context) ((context)->uc_mcontext.__gregs[_REG_SS])
121 #define EFL_sig(context) ((context)->uc_mcontext.__gregs[_REG_RFL])
123 #define RIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext.__gregs[_REG_RIP]))
124 #define RSP_sig(context) (*((unsigned long*)&(context)->uc_mcontext.__gregs[_REG_URSP]))
126 #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
127 #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR])
129 #define FPU_sig(context) ((XMM_SAVE_AREA32 *)((context)->uc_mcontext.__fpregs))
130 #endif /* __NetBSD__ */
132 enum i386_trap_code
134 TRAP_x86_UNKNOWN = -1, /* Unknown fault (TRAP_sig not defined) */
135 TRAP_x86_DIVIDE = 0, /* Division by zero exception */
136 TRAP_x86_TRCTRAP = 1, /* Single-step exception */
137 TRAP_x86_NMI = 2, /* NMI interrupt */
138 TRAP_x86_BPTFLT = 3, /* Breakpoint exception */
139 TRAP_x86_OFLOW = 4, /* Overflow exception */
140 TRAP_x86_BOUND = 5, /* Bound range exception */
141 TRAP_x86_PRIVINFLT = 6, /* Invalid opcode exception */
142 TRAP_x86_DNA = 7, /* Device not available exception */
143 TRAP_x86_DOUBLEFLT = 8, /* Double fault exception */
144 TRAP_x86_FPOPFLT = 9, /* Coprocessor segment overrun */
145 TRAP_x86_TSSFLT = 10, /* Invalid TSS exception */
146 TRAP_x86_SEGNPFLT = 11, /* Segment not present exception */
147 TRAP_x86_STKFLT = 12, /* Stack fault */
148 TRAP_x86_PROTFLT = 13, /* General protection fault */
149 TRAP_x86_PAGEFLT = 14, /* Page fault */
150 TRAP_x86_ARITHTRAP = 16, /* Floating point exception */
151 TRAP_x86_ALIGNFLT = 17, /* Alignment check exception */
152 TRAP_x86_MCHK = 18, /* Machine check exception */
153 TRAP_x86_CACHEFLT = 19 /* Cache flush exception */
156 typedef int (*wine_signal_handler)(unsigned int sig);
158 static wine_signal_handler handlers[256];
160 /***********************************************************************
161 * dispatch_signal
163 static inline int dispatch_signal(unsigned int sig)
165 if (handlers[sig] == NULL) return 0;
166 return handlers[sig](sig);
169 /***********************************************************************
170 * save_context
172 * Set the register values from a sigcontext.
174 static void save_context( CONTEXT *context, const ucontext_t *sigcontext )
176 context->ContextFlags = CONTEXT_CONTROL | CONTEXT_INTEGER | CONTEXT_SEGMENTS;
177 context->Rax = RAX_sig(sigcontext);
178 context->Rcx = RCX_sig(sigcontext);
179 context->Rdx = RDX_sig(sigcontext);
180 context->Rbx = RBX_sig(sigcontext);
181 context->Rsp = RSP_sig(sigcontext);
182 context->Rbp = RBP_sig(sigcontext);
183 context->Rsi = RSI_sig(sigcontext);
184 context->Rdi = RDI_sig(sigcontext);
185 context->R8 = R8_sig(sigcontext);
186 context->R9 = R9_sig(sigcontext);
187 context->R10 = R10_sig(sigcontext);
188 context->R11 = R11_sig(sigcontext);
189 context->R12 = R12_sig(sigcontext);
190 context->R13 = R13_sig(sigcontext);
191 context->R14 = R14_sig(sigcontext);
192 context->R15 = R15_sig(sigcontext);
193 context->Rip = RIP_sig(sigcontext);
194 context->SegCs = CS_sig(sigcontext);
195 context->SegFs = FS_sig(sigcontext);
196 context->SegGs = GS_sig(sigcontext);
197 context->EFlags = EFL_sig(sigcontext);
198 context->SegDs = 0; /* FIXME */
199 context->SegEs = 0; /* FIXME */
200 context->SegSs = 0; /* FIXME */
201 context->MxCsr = 0; /* FIXME */
202 if (FPU_sig(sigcontext))
204 context->ContextFlags |= CONTEXT_FLOATING_POINT;
205 context->u.FltSave = *FPU_sig(sigcontext);
210 /***********************************************************************
211 * restore_context
213 * Build a sigcontext from the register values.
215 static void restore_context( const CONTEXT *context, ucontext_t *sigcontext )
217 RAX_sig(sigcontext) = context->Rax;
218 RCX_sig(sigcontext) = context->Rcx;
219 RDX_sig(sigcontext) = context->Rdx;
220 RBX_sig(sigcontext) = context->Rbx;
221 RSP_sig(sigcontext) = context->Rsp;
222 RBP_sig(sigcontext) = context->Rbp;
223 RSI_sig(sigcontext) = context->Rsi;
224 RDI_sig(sigcontext) = context->Rdi;
225 R8_sig(sigcontext) = context->R8;
226 R9_sig(sigcontext) = context->R9;
227 R10_sig(sigcontext) = context->R10;
228 R11_sig(sigcontext) = context->R11;
229 R12_sig(sigcontext) = context->R12;
230 R13_sig(sigcontext) = context->R13;
231 R14_sig(sigcontext) = context->R14;
232 R15_sig(sigcontext) = context->R15;
233 RIP_sig(sigcontext) = context->Rip;
234 CS_sig(sigcontext) = context->SegCs;
235 FS_sig(sigcontext) = context->SegFs;
236 GS_sig(sigcontext) = context->SegGs;
237 EFL_sig(sigcontext) = context->EFlags;
238 if (FPU_sig(sigcontext)) *FPU_sig(sigcontext) = context->u.FltSave;
242 /***********************************************************************
243 * RtlCaptureContext (NTDLL.@)
245 void WINAPI __regs_RtlCaptureContext( CONTEXT *context, CONTEXT *regs )
247 *context = *regs;
249 DEFINE_REGS_ENTRYPOINT( RtlCaptureContext, 1 )
252 /***********************************************************************
253 * set_cpu_context
255 * Set the new CPU context.
257 void set_cpu_context( const CONTEXT *context )
259 FIXME("not implemented\n");
263 /***********************************************************************
264 * copy_context
266 * Copy a register context according to the flags.
268 void copy_context( CONTEXT *to, const CONTEXT *from, DWORD flags )
270 flags &= ~CONTEXT_AMD64; /* get rid of CPU id */
271 if (flags & CONTEXT_CONTROL)
273 to->Rbp = from->Rbp;
274 to->Rip = from->Rip;
275 to->Rsp = from->Rsp;
276 to->SegCs = from->SegCs;
277 to->SegSs = from->SegSs;
278 to->EFlags = from->EFlags;
279 to->MxCsr = from->MxCsr;
281 if (flags & CONTEXT_INTEGER)
283 to->Rax = from->Rax;
284 to->Rcx = from->Rcx;
285 to->Rdx = from->Rdx;
286 to->Rbx = from->Rbx;
287 to->Rsi = from->Rsi;
288 to->Rdi = from->Rdi;
289 to->R8 = from->R8;
290 to->R9 = from->R9;
291 to->R10 = from->R10;
292 to->R11 = from->R11;
293 to->R12 = from->R12;
294 to->R13 = from->R13;
295 to->R14 = from->R14;
296 to->R15 = from->R15;
298 if (flags & CONTEXT_SEGMENTS)
300 to->SegDs = from->SegDs;
301 to->SegEs = from->SegEs;
302 to->SegFs = from->SegFs;
303 to->SegGs = from->SegGs;
305 if (flags & CONTEXT_FLOATING_POINT)
307 to->u.FltSave = from->u.FltSave;
309 if (flags & CONTEXT_DEBUG_REGISTERS)
311 to->Dr0 = from->Dr0;
312 to->Dr1 = from->Dr1;
313 to->Dr2 = from->Dr2;
314 to->Dr3 = from->Dr3;
315 to->Dr6 = from->Dr6;
316 to->Dr7 = from->Dr7;
321 /***********************************************************************
322 * context_to_server
324 * Convert a register context to the server format.
326 NTSTATUS context_to_server( context_t *to, const CONTEXT *from )
328 DWORD flags = from->ContextFlags & ~CONTEXT_AMD64; /* get rid of CPU id */
330 memset( to, 0, sizeof(*to) );
331 to->cpu = CPU_x86_64;
333 if (flags & CONTEXT_CONTROL)
335 to->flags |= SERVER_CTX_CONTROL;
336 to->ctl.x86_64_regs.rbp = from->Rbp;
337 to->ctl.x86_64_regs.rip = from->Rip;
338 to->ctl.x86_64_regs.rsp = from->Rsp;
339 to->ctl.x86_64_regs.cs = from->SegCs;
340 to->ctl.x86_64_regs.ss = from->SegSs;
341 to->ctl.x86_64_regs.flags = from->EFlags;
342 to->ctl.x86_64_regs.mxcsr = from->MxCsr;
344 if (flags & CONTEXT_INTEGER)
346 to->flags |= SERVER_CTX_INTEGER;
347 to->integer.x86_64_regs.rax = from->Rax;
348 to->integer.x86_64_regs.rcx = from->Rcx;
349 to->integer.x86_64_regs.rdx = from->Rdx;
350 to->integer.x86_64_regs.rbx = from->Rbx;
351 to->integer.x86_64_regs.rsi = from->Rsi;
352 to->integer.x86_64_regs.rdi = from->Rdi;
353 to->integer.x86_64_regs.r8 = from->R8;
354 to->integer.x86_64_regs.r9 = from->R9;
355 to->integer.x86_64_regs.r10 = from->R10;
356 to->integer.x86_64_regs.r11 = from->R11;
357 to->integer.x86_64_regs.r12 = from->R12;
358 to->integer.x86_64_regs.r13 = from->R13;
359 to->integer.x86_64_regs.r14 = from->R14;
360 to->integer.x86_64_regs.r15 = from->R15;
362 if (flags & CONTEXT_SEGMENTS)
364 to->flags |= SERVER_CTX_SEGMENTS;
365 to->seg.x86_64_regs.ds = from->SegDs;
366 to->seg.x86_64_regs.es = from->SegEs;
367 to->seg.x86_64_regs.fs = from->SegFs;
368 to->seg.x86_64_regs.gs = from->SegGs;
370 if (flags & CONTEXT_FLOATING_POINT)
372 to->flags |= SERVER_CTX_FLOATING_POINT;
373 memcpy( to->fp.x86_64_regs.fpregs, &from->u.FltSave, sizeof(to->fp.x86_64_regs.fpregs) );
375 if (flags & CONTEXT_DEBUG_REGISTERS)
377 to->flags |= SERVER_CTX_DEBUG_REGISTERS;
378 to->debug.x86_64_regs.dr0 = from->Dr0;
379 to->debug.x86_64_regs.dr1 = from->Dr1;
380 to->debug.x86_64_regs.dr2 = from->Dr2;
381 to->debug.x86_64_regs.dr3 = from->Dr3;
382 to->debug.x86_64_regs.dr6 = from->Dr6;
383 to->debug.x86_64_regs.dr7 = from->Dr7;
385 return STATUS_SUCCESS;
389 /***********************************************************************
390 * context_from_server
392 * Convert a register context from the server format.
394 NTSTATUS context_from_server( CONTEXT *to, const context_t *from )
396 if (from->cpu != CPU_x86_64) return STATUS_INVALID_PARAMETER;
398 to->ContextFlags = CONTEXT_AMD64;
399 if (from->flags & SERVER_CTX_CONTROL)
401 to->ContextFlags |= CONTEXT_CONTROL;
402 to->Rbp = from->ctl.x86_64_regs.rbp;
403 to->Rip = from->ctl.x86_64_regs.rip;
404 to->Rsp = from->ctl.x86_64_regs.rsp;
405 to->SegCs = from->ctl.x86_64_regs.cs;
406 to->SegSs = from->ctl.x86_64_regs.ss;
407 to->EFlags = from->ctl.x86_64_regs.flags;
408 to->MxCsr = from->ctl.x86_64_regs.mxcsr;
411 if (from->flags & SERVER_CTX_INTEGER)
413 to->ContextFlags |= CONTEXT_INTEGER;
414 to->Rax = from->integer.x86_64_regs.rax;
415 to->Rcx = from->integer.x86_64_regs.rcx;
416 to->Rdx = from->integer.x86_64_regs.rdx;
417 to->Rbx = from->integer.x86_64_regs.rbx;
418 to->Rsi = from->integer.x86_64_regs.rsi;
419 to->Rdi = from->integer.x86_64_regs.rdi;
420 to->R8 = from->integer.x86_64_regs.r8;
421 to->R9 = from->integer.x86_64_regs.r9;
422 to->R10 = from->integer.x86_64_regs.r10;
423 to->R11 = from->integer.x86_64_regs.r11;
424 to->R12 = from->integer.x86_64_regs.r12;
425 to->R13 = from->integer.x86_64_regs.r13;
426 to->R14 = from->integer.x86_64_regs.r14;
427 to->R15 = from->integer.x86_64_regs.r15;
429 if (from->flags & SERVER_CTX_SEGMENTS)
431 to->ContextFlags |= CONTEXT_SEGMENTS;
432 to->SegDs = from->seg.x86_64_regs.ds;
433 to->SegEs = from->seg.x86_64_regs.es;
434 to->SegFs = from->seg.x86_64_regs.fs;
435 to->SegGs = from->seg.x86_64_regs.gs;
437 if (from->flags & SERVER_CTX_FLOATING_POINT)
439 to->ContextFlags |= CONTEXT_FLOATING_POINT;
440 memcpy( &to->u.FltSave, from->fp.x86_64_regs.fpregs, sizeof(from->fp.x86_64_regs.fpregs) );
442 if (from->flags & SERVER_CTX_DEBUG_REGISTERS)
444 to->ContextFlags |= CONTEXT_DEBUG_REGISTERS;
445 to->Dr0 = from->debug.x86_64_regs.dr0;
446 to->Dr1 = from->debug.x86_64_regs.dr1;
447 to->Dr2 = from->debug.x86_64_regs.dr2;
448 to->Dr3 = from->debug.x86_64_regs.dr3;
449 to->Dr6 = from->debug.x86_64_regs.dr6;
450 to->Dr7 = from->debug.x86_64_regs.dr7;
452 return STATUS_SUCCESS;
456 /**********************************************************************
457 * call_stack_handlers
459 * Call the stack handlers chain.
461 static NTSTATUS call_stack_handlers( EXCEPTION_RECORD *rec, CONTEXT *context )
463 EXCEPTION_POINTERS ptrs;
465 FIXME( "not implemented on x86_64\n" );
467 /* hack: call unhandled exception filter directly */
468 ptrs.ExceptionRecord = rec;
469 ptrs.ContextRecord = context;
470 unhandled_exception_filter( &ptrs );
471 return STATUS_UNHANDLED_EXCEPTION;
475 /*******************************************************************
476 * raise_exception
478 * Implementation of NtRaiseException.
480 static NTSTATUS raise_exception( EXCEPTION_RECORD *rec, CONTEXT *context, BOOL first_chance )
482 NTSTATUS status;
484 if (first_chance)
486 DWORD c;
488 TRACE( "code=%x flags=%x addr=%p ip=%lx tid=%04x\n",
489 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
490 context->Rip, GetCurrentThreadId() );
491 for (c = 0; c < rec->NumberParameters; c++)
492 TRACE( " info[%d]=%08lx\n", c, rec->ExceptionInformation[c] );
493 if (rec->ExceptionCode == EXCEPTION_WINE_STUB)
495 if (rec->ExceptionInformation[1] >> 16)
496 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
497 rec->ExceptionAddress,
498 (char*)rec->ExceptionInformation[0], (char*)rec->ExceptionInformation[1] );
499 else
500 MESSAGE( "wine: Call from %p to unimplemented function %s.%ld, aborting\n",
501 rec->ExceptionAddress,
502 (char*)rec->ExceptionInformation[0], rec->ExceptionInformation[1] );
504 else
506 TRACE(" rax=%016lx rbx=%016lx rcx=%016lx rdx=%016lx\n",
507 context->Rax, context->Rbx, context->Rcx, context->Rdx );
508 TRACE(" rsi=%016lx rdi=%016lx rbp=%016lx rsp=%016lx\n",
509 context->Rsi, context->Rdi, context->Rbp, context->Rsp );
510 TRACE(" r8=%016lx r9=%016lx r10=%016lx r11=%016lx\n",
511 context->R8, context->R9, context->R10, context->R11 );
512 TRACE(" r12=%016lx r13=%016lx r14=%016lx r15=%016lx\n",
513 context->R12, context->R13, context->R14, context->R15 );
515 status = send_debug_event( rec, TRUE, context );
516 if (status == DBG_CONTINUE || status == DBG_EXCEPTION_HANDLED)
517 return STATUS_SUCCESS;
519 if (call_vectored_handlers( rec, context ) == EXCEPTION_CONTINUE_EXECUTION)
520 return STATUS_SUCCESS;
522 if ((status = call_stack_handlers( rec, context )) != STATUS_UNHANDLED_EXCEPTION)
523 return status;
526 /* last chance exception */
528 status = send_debug_event( rec, FALSE, context );
529 if (status != DBG_CONTINUE)
531 if (rec->ExceptionFlags & EH_STACK_INVALID)
532 ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
533 else if (rec->ExceptionCode == STATUS_NONCONTINUABLE_EXCEPTION)
534 ERR("Process attempted to continue execution after noncontinuable exception.\n");
535 else
536 ERR("Unhandled exception code %x flags %x addr %p\n",
537 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
538 NtTerminateProcess( NtCurrentProcess(), rec->ExceptionCode );
540 return STATUS_SUCCESS;
544 /**********************************************************************
545 * segv_handler
547 * Handler for SIGSEGV and related errors.
549 static void segv_handler( int signal, siginfo_t *siginfo, void *sigcontext )
551 EXCEPTION_RECORD rec;
552 CONTEXT context;
553 NTSTATUS status;
554 ucontext_t *ucontext = sigcontext;
556 save_context( &context, ucontext );
558 rec.ExceptionRecord = NULL;
559 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
560 rec.ExceptionAddress = (LPVOID)context.Rip;
561 rec.NumberParameters = 0;
563 switch(TRAP_sig(ucontext))
565 case TRAP_x86_OFLOW: /* Overflow exception */
566 rec.ExceptionCode = EXCEPTION_INT_OVERFLOW;
567 break;
568 case TRAP_x86_BOUND: /* Bound range exception */
569 rec.ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
570 break;
571 case TRAP_x86_PRIVINFLT: /* Invalid opcode exception */
572 rec.ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
573 break;
574 case TRAP_x86_STKFLT: /* Stack fault */
575 rec.ExceptionCode = EXCEPTION_STACK_OVERFLOW;
576 break;
577 case TRAP_x86_SEGNPFLT: /* Segment not present exception */
578 case TRAP_x86_PROTFLT: /* General protection fault */
579 case TRAP_x86_UNKNOWN: /* Unknown fault code */
580 rec.ExceptionCode = ERROR_sig(ucontext) ? EXCEPTION_ACCESS_VIOLATION
581 : EXCEPTION_PRIV_INSTRUCTION;
582 break;
583 case TRAP_x86_PAGEFLT: /* Page fault */
584 rec.ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
585 rec.NumberParameters = 2;
586 rec.ExceptionInformation[0] = (ERROR_sig(ucontext) & 2) != 0;
587 rec.ExceptionInformation[1] = (ULONG_PTR)siginfo->si_addr;
588 if (!(rec.ExceptionCode = virtual_handle_fault( siginfo->si_addr, rec.ExceptionInformation[0] )))
589 goto done;
590 break;
591 case TRAP_x86_ALIGNFLT: /* Alignment check exception */
592 rec.ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
593 break;
594 default:
595 ERR( "Got unexpected trap %ld\n", TRAP_sig(ucontext) );
596 /* fall through */
597 case TRAP_x86_NMI: /* NMI interrupt */
598 case TRAP_x86_DNA: /* Device not available exception */
599 case TRAP_x86_DOUBLEFLT: /* Double fault exception */
600 case TRAP_x86_TSSFLT: /* Invalid TSS exception */
601 case TRAP_x86_MCHK: /* Machine check exception */
602 case TRAP_x86_CACHEFLT: /* Cache flush exception */
603 rec.ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
604 break;
607 status = raise_exception( &rec, &context, TRUE );
608 if (status) raise_status( status, &rec );
609 done:
610 restore_context( &context, ucontext );
613 /**********************************************************************
614 * trap_handler
616 * Handler for SIGTRAP.
618 static void trap_handler( int signal, siginfo_t *siginfo, void *sigcontext )
620 EXCEPTION_RECORD rec;
621 CONTEXT context;
622 NTSTATUS status;
623 ucontext_t *ucontext = sigcontext;
625 save_context( &context, ucontext );
626 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
627 rec.ExceptionRecord = NULL;
628 rec.ExceptionAddress = (LPVOID)context.Rip;
629 rec.NumberParameters = 0;
631 switch (siginfo->si_code)
633 case TRAP_TRACE: /* Single-step exception */
634 rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
635 EFL_sig(ucontext) &= ~0x100; /* clear single-step flag */
636 break;
637 case TRAP_BRKPT: /* Breakpoint exception */
638 rec.ExceptionAddress = (char *)rec.ExceptionAddress - 1; /* back up over the int3 instruction */
639 /* fall through */
640 default:
641 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
642 break;
645 status = raise_exception( &rec, &context, TRUE );
646 if (status) raise_status( status, &rec );
647 restore_context( &context, ucontext );
650 /**********************************************************************
651 * fpe_handler
653 * Handler for SIGFPE.
655 static void fpe_handler( int signal, siginfo_t *siginfo, void *ucontext )
657 EXCEPTION_RECORD rec;
658 CONTEXT context;
659 NTSTATUS status;
661 save_context( &context, ucontext );
662 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
663 rec.ExceptionRecord = NULL;
664 rec.ExceptionAddress = (LPVOID)context.Rip;
665 rec.NumberParameters = 0;
667 switch (siginfo->si_code)
669 case FPE_FLTSUB:
670 rec.ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
671 break;
672 case FPE_INTDIV:
673 rec.ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
674 break;
675 case FPE_INTOVF:
676 rec.ExceptionCode = EXCEPTION_INT_OVERFLOW;
677 break;
678 case FPE_FLTDIV:
679 rec.ExceptionCode = EXCEPTION_FLT_DIVIDE_BY_ZERO;
680 break;
681 case FPE_FLTOVF:
682 rec.ExceptionCode = EXCEPTION_FLT_OVERFLOW;
683 break;
684 case FPE_FLTUND:
685 rec.ExceptionCode = EXCEPTION_FLT_UNDERFLOW;
686 break;
687 case FPE_FLTRES:
688 rec.ExceptionCode = EXCEPTION_FLT_INEXACT_RESULT;
689 break;
690 case FPE_FLTINV:
691 default:
692 rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
693 break;
696 status = raise_exception( &rec, &context, TRUE );
697 if (status) raise_status( status, &rec );
698 restore_context( &context, ucontext );
701 /**********************************************************************
702 * int_handler
704 * Handler for SIGINT.
706 static void int_handler( int signal, siginfo_t *siginfo, void *ucontext )
708 if (!dispatch_signal(SIGINT))
710 EXCEPTION_RECORD rec;
711 CONTEXT context;
712 NTSTATUS status;
714 save_context( &context, ucontext );
715 rec.ExceptionCode = CONTROL_C_EXIT;
716 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
717 rec.ExceptionRecord = NULL;
718 rec.ExceptionAddress = (LPVOID)context.Rip;
719 rec.NumberParameters = 0;
720 status = raise_exception( &rec, &context, TRUE );
721 if (status) raise_status( status, &rec );
722 restore_context( &context, ucontext );
727 /**********************************************************************
728 * abrt_handler
730 * Handler for SIGABRT.
732 static void abrt_handler( int signal, siginfo_t *siginfo, void *ucontext )
734 EXCEPTION_RECORD rec;
735 CONTEXT context;
736 NTSTATUS status;
738 save_context( &context, ucontext );
739 rec.ExceptionCode = EXCEPTION_WINE_ASSERTION;
740 rec.ExceptionFlags = EH_NONCONTINUABLE;
741 rec.ExceptionRecord = NULL;
742 rec.ExceptionAddress = (LPVOID)context.Rip;
743 rec.NumberParameters = 0;
744 status = raise_exception( &rec, &context, TRUE );
745 if (status) raise_status( status, &rec );
746 restore_context( &context, ucontext );
750 /**********************************************************************
751 * quit_handler
753 * Handler for SIGQUIT.
755 static void quit_handler( int signal, siginfo_t *siginfo, void *ucontext )
757 abort_thread(0);
761 /**********************************************************************
762 * usr1_handler
764 * Handler for SIGUSR1, used to signal a thread that it got suspended.
766 static void usr1_handler( int signal, siginfo_t *siginfo, void *ucontext )
768 CONTEXT context;
770 save_context( &context, ucontext );
771 wait_suspend( &context );
772 restore_context( &context, ucontext );
776 /**********************************************************************
777 * get_signal_stack_total_size
779 * Retrieve the size to allocate for the signal stack, including the TEB at the bottom.
780 * Must be a power of two.
782 size_t get_signal_stack_total_size(void)
784 assert( sizeof(TEB) <= 2*getpagesize() );
785 return 2*getpagesize(); /* this is just for the TEB, we don't need a signal stack */
789 /***********************************************************************
790 * __wine_set_signal_handler (NTDLL.@)
792 int CDECL __wine_set_signal_handler(unsigned int sig, wine_signal_handler wsh)
794 if (sig > sizeof(handlers) / sizeof(handlers[0])) return -1;
795 if (handlers[sig] != NULL) return -2;
796 handlers[sig] = wsh;
797 return 0;
801 /**********************************************************************
802 * signal_init_thread
804 void signal_init_thread( TEB *teb )
806 #ifdef __linux__
807 arch_prctl( ARCH_SET_GS, teb );
808 #else
809 # error Please define setting %gs for your architecture
810 #endif
813 /**********************************************************************
814 * signal_init_process
816 void signal_init_process(void)
818 struct sigaction sig_act;
820 sig_act.sa_mask = server_block_set;
821 sig_act.sa_flags = SA_RESTART | SA_SIGINFO;
823 sig_act.sa_sigaction = int_handler;
824 if (sigaction( SIGINT, &sig_act, NULL ) == -1) goto error;
825 sig_act.sa_sigaction = fpe_handler;
826 if (sigaction( SIGFPE, &sig_act, NULL ) == -1) goto error;
827 sig_act.sa_sigaction = abrt_handler;
828 if (sigaction( SIGABRT, &sig_act, NULL ) == -1) goto error;
829 sig_act.sa_sigaction = quit_handler;
830 if (sigaction( SIGQUIT, &sig_act, NULL ) == -1) goto error;
831 sig_act.sa_sigaction = usr1_handler;
832 if (sigaction( SIGUSR1, &sig_act, NULL ) == -1) goto error;
834 sig_act.sa_sigaction = segv_handler;
835 if (sigaction( SIGSEGV, &sig_act, NULL ) == -1) goto error;
836 if (sigaction( SIGILL, &sig_act, NULL ) == -1) goto error;
837 #ifdef SIGBUS
838 if (sigaction( SIGBUS, &sig_act, NULL ) == -1) goto error;
839 #endif
841 #ifdef SIGTRAP
842 sig_act.sa_sigaction = trap_handler;
843 if (sigaction( SIGTRAP, &sig_act, NULL ) == -1) goto error;
844 #endif
845 return;
847 error:
848 perror("sigaction");
849 exit(1);
853 /**********************************************************************
854 * RtlLookupFunctionEntry (NTDLL.@)
856 PRUNTIME_FUNCTION WINAPI RtlLookupFunctionEntry( ULONG64 pc, ULONG64 *base,
857 UNWIND_HISTORY_TABLE *table )
859 FIXME("stub\n");
860 return NULL;
864 /**********************************************************************
865 * RtlVirtualUnwind (NTDLL.@)
867 PVOID WINAPI RtlVirtualUnwind ( ULONG type, ULONG64 base, ULONG64 pc,
868 RUNTIME_FUNCTION *function, CONTEXT *context,
869 PVOID *data, ULONG64 *frame,
870 KNONVOLATILE_CONTEXT_POINTERS *ctx_ptr )
872 FIXME("stub\n");
873 return NULL;
877 /*******************************************************************
878 * RtlUnwind (NTDLL.@)
880 void WINAPI __regs_RtlUnwind( EXCEPTION_REGISTRATION_RECORD* pEndFrame, PVOID targetIp,
881 PEXCEPTION_RECORD pRecord, PVOID retval, CONTEXT *context )
883 EXCEPTION_RECORD record;
885 /* build an exception record, if we do not have one */
886 if (!pRecord)
888 record.ExceptionCode = STATUS_UNWIND;
889 record.ExceptionFlags = 0;
890 record.ExceptionRecord = NULL;
891 record.ExceptionAddress = (void *)context->Rip;
892 record.NumberParameters = 0;
893 pRecord = &record;
896 pRecord->ExceptionFlags |= EH_UNWINDING | (pEndFrame ? 0 : EH_EXIT_UNWIND);
898 FIXME( "code=%x flags=%x not implemented on x86_64\n",
899 pRecord->ExceptionCode, pRecord->ExceptionFlags );
900 NtTerminateProcess( GetCurrentProcess(), 1 );
902 DEFINE_REGS_ENTRYPOINT( RtlUnwind, 4 )
905 /*******************************************************************
906 * NtRaiseException (NTDLL.@)
908 NTSTATUS WINAPI NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context, BOOL first_chance )
910 NTSTATUS status = raise_exception( rec, context, first_chance );
911 if (status == STATUS_SUCCESS) NtSetContextThread( GetCurrentThread(), context );
912 return status;
916 /***********************************************************************
917 * RtlRaiseException (NTDLL.@)
919 void WINAPI __regs_RtlRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context )
921 NTSTATUS status;
923 rec->ExceptionAddress = (void *)context->Rip;
924 status = raise_exception( rec, context, TRUE );
925 if (status != STATUS_SUCCESS) raise_status( status, rec );
927 DEFINE_REGS_ENTRYPOINT( RtlRaiseException, 1 )
930 /**********************************************************************
931 * __wine_enter_vm86 (NTDLL.@)
933 void __wine_enter_vm86( CONTEXT *context )
935 MESSAGE("vm86 mode not supported on this platform\n");
938 /**********************************************************************
939 * DbgBreakPoint (NTDLL.@)
941 __ASM_GLOBAL_FUNC( DbgBreakPoint, "int $3; ret")
943 /**********************************************************************
944 * DbgUserBreakPoint (NTDLL.@)
946 __ASM_GLOBAL_FUNC( DbgUserBreakPoint, "int $3; ret")
948 #endif /* __x86_64__ */