35c9f1b6e81b2e545abac167da6f14e93d9789c5
[wine/wine64.git] / dlls / ntdll / signal_x86_64.c
blob35c9f1b6e81b2e545abac167da6f14e93d9789c5
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 "windef.h"
44 #include "winternl.h"
45 #include "wine/library.h"
46 #include "wine/exception.h"
47 #include "ntdll_misc.h"
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(seh);
53 /***********************************************************************
54 * signal context platform-specific definitions
56 #ifdef linux
58 typedef struct ucontext SIGCONTEXT;
60 # define HANDLER_DEF(name) void name( int __signal, struct siginfo *__siginfo, SIGCONTEXT *__context )
61 # define HANDLER_CONTEXT (__context)
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 #define FAULT_CODE (__siginfo->si_code)
92 #define FAULT_ADDRESS (__siginfo->si_addr)
94 #endif /* linux */
96 enum i386_trap_code
98 TRAP_x86_UNKNOWN = -1, /* Unknown fault (TRAP_sig not defined) */
99 TRAP_x86_DIVIDE = 0, /* Division by zero exception */
100 TRAP_x86_TRCTRAP = 1, /* Single-step exception */
101 TRAP_x86_NMI = 2, /* NMI interrupt */
102 TRAP_x86_BPTFLT = 3, /* Breakpoint exception */
103 TRAP_x86_OFLOW = 4, /* Overflow exception */
104 TRAP_x86_BOUND = 5, /* Bound range exception */
105 TRAP_x86_PRIVINFLT = 6, /* Invalid opcode exception */
106 TRAP_x86_DNA = 7, /* Device not available exception */
107 TRAP_x86_DOUBLEFLT = 8, /* Double fault exception */
108 TRAP_x86_FPOPFLT = 9, /* Coprocessor segment overrun */
109 TRAP_x86_TSSFLT = 10, /* Invalid TSS exception */
110 TRAP_x86_SEGNPFLT = 11, /* Segment not present exception */
111 TRAP_x86_STKFLT = 12, /* Stack fault */
112 TRAP_x86_PROTFLT = 13, /* General protection fault */
113 TRAP_x86_PAGEFLT = 14, /* Page fault */
114 TRAP_x86_ARITHTRAP = 16, /* Floating point exception */
115 TRAP_x86_ALIGNFLT = 17, /* Alignment check exception */
116 TRAP_x86_MCHK = 18, /* Machine check exception */
117 TRAP_x86_CACHEFLT = 19 /* Cache flush exception */
120 typedef int (*wine_signal_handler)(unsigned int sig);
122 static wine_signal_handler handlers[256];
124 /***********************************************************************
125 * dispatch_signal
127 static inline int dispatch_signal(unsigned int sig)
129 if (handlers[sig] == NULL) return 0;
130 return handlers[sig](sig);
133 /***********************************************************************
134 * save_context
136 * Set the register values from a sigcontext.
138 static void save_context( CONTEXT *context, const SIGCONTEXT *sigcontext )
140 context->Rax = RAX_sig(sigcontext);
141 context->Rcx = RCX_sig(sigcontext);
142 context->Rdx = RDX_sig(sigcontext);
143 context->Rbx = RBX_sig(sigcontext);
144 context->Rsp = RSP_sig(sigcontext);
145 context->Rbp = RBP_sig(sigcontext);
146 context->Rsi = RSI_sig(sigcontext);
147 context->Rdi = RDI_sig(sigcontext);
148 context->R8 = R8_sig(sigcontext);
149 context->R9 = R9_sig(sigcontext);
150 context->R10 = R10_sig(sigcontext);
151 context->R11 = R11_sig(sigcontext);
152 context->R12 = R12_sig(sigcontext);
153 context->R13 = R13_sig(sigcontext);
154 context->R14 = R14_sig(sigcontext);
155 context->R15 = R15_sig(sigcontext);
156 context->Rip = RIP_sig(sigcontext);
157 context->SegCs = CS_sig(sigcontext);
158 context->SegFs = FS_sig(sigcontext);
159 context->SegGs = GS_sig(sigcontext);
160 context->EFlags = EFL_sig(sigcontext);
161 context->SegDs = 0; /* FIXME */
162 context->SegEs = 0; /* FIXME */
163 context->SegSs = 0; /* FIXME */
164 context->MxCsr = 0; /* FIXME */
165 if (FPU_sig(sigcontext)) context->u.FltSave = *FPU_sig(sigcontext);
169 /***********************************************************************
170 * restore_context
172 * Build a sigcontext from the register values.
174 static void restore_context( const CONTEXT *context, SIGCONTEXT *sigcontext )
176 RAX_sig(sigcontext) = context->Rax;
177 RCX_sig(sigcontext) = context->Rcx;
178 RDX_sig(sigcontext) = context->Rdx;
179 RBX_sig(sigcontext) = context->Rbx;
180 RSP_sig(sigcontext) = context->Rsp;
181 RBP_sig(sigcontext) = context->Rbp;
182 RSI_sig(sigcontext) = context->Rsi;
183 RDI_sig(sigcontext) = context->Rdi;
184 R8_sig(sigcontext) = context->R8;
185 R9_sig(sigcontext) = context->R9;
186 R10_sig(sigcontext) = context->R10;
187 R11_sig(sigcontext) = context->R11;
188 R12_sig(sigcontext) = context->R12;
189 R13_sig(sigcontext) = context->R13;
190 R14_sig(sigcontext) = context->R14;
191 R15_sig(sigcontext) = context->R15;
192 RIP_sig(sigcontext) = context->Rip;
193 CS_sig(sigcontext) = context->SegCs;
194 FS_sig(sigcontext) = context->SegFs;
195 GS_sig(sigcontext) = context->SegGs;
196 EFL_sig(sigcontext) = context->EFlags;
197 if (FPU_sig(sigcontext)) *FPU_sig(sigcontext) = context->u.FltSave;
201 /***********************************************************************
202 * get_cpu_context
204 * Get the context of the current thread.
206 void get_cpu_context( CONTEXT *context )
208 FIXME("not implemented\n");
212 /***********************************************************************
213 * set_cpu_context
215 * Set the new CPU context.
217 void set_cpu_context( const CONTEXT *context )
219 FIXME("not implemented\n");
223 /**********************************************************************
224 * segv_handler
226 * Handler for SIGSEGV and related errors.
228 static HANDLER_DEF(segv_handler)
230 EXCEPTION_RECORD rec;
231 CONTEXT context;
233 save_context( &context, HANDLER_CONTEXT );
235 rec.ExceptionRecord = NULL;
236 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
237 rec.ExceptionAddress = (LPVOID)context.Rip;
238 rec.NumberParameters = 0;
240 switch(TRAP_sig(HANDLER_CONTEXT))
242 case TRAP_x86_OFLOW: /* Overflow exception */
243 rec.ExceptionCode = EXCEPTION_INT_OVERFLOW;
244 break;
245 case TRAP_x86_BOUND: /* Bound range exception */
246 rec.ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
247 break;
248 case TRAP_x86_PRIVINFLT: /* Invalid opcode exception */
249 rec.ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
250 break;
251 case TRAP_x86_STKFLT: /* Stack fault */
252 rec.ExceptionCode = EXCEPTION_STACK_OVERFLOW;
253 break;
254 case TRAP_x86_SEGNPFLT: /* Segment not present exception */
255 case TRAP_x86_PROTFLT: /* General protection fault */
256 case TRAP_x86_UNKNOWN: /* Unknown fault code */
257 rec.ExceptionCode = ERROR_sig(HANDLER_CONTEXT) ? EXCEPTION_ACCESS_VIOLATION
258 : EXCEPTION_PRIV_INSTRUCTION;
259 break;
260 case TRAP_x86_PAGEFLT: /* Page fault */
261 rec.ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
262 #ifdef FAULT_ADDRESS
263 rec.NumberParameters = 2;
264 rec.ExceptionInformation[0] = (ERROR_sig(HANDLER_CONTEXT) & 2) != 0;
265 rec.ExceptionInformation[1] = (ULONG_PTR)FAULT_ADDRESS;
266 if (!(rec.ExceptionCode = virtual_handle_fault( FAULT_ADDRESS, rec.ExceptionInformation[0] )))
267 goto done;
268 #endif
269 break;
270 case TRAP_x86_ALIGNFLT: /* Alignment check exception */
271 rec.ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
272 break;
273 default:
274 ERR( "Got unexpected trap %ld\n", TRAP_sig(HANDLER_CONTEXT) );
275 /* fall through */
276 case TRAP_x86_NMI: /* NMI interrupt */
277 case TRAP_x86_DNA: /* Device not available exception */
278 case TRAP_x86_DOUBLEFLT: /* Double fault exception */
279 case TRAP_x86_TSSFLT: /* Invalid TSS exception */
280 case TRAP_x86_MCHK: /* Machine check exception */
281 case TRAP_x86_CACHEFLT: /* Cache flush exception */
282 rec.ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
283 break;
286 __regs_RtlRaiseException( &rec, &context );
287 done:
288 restore_context( &context, HANDLER_CONTEXT );
291 /**********************************************************************
292 * trap_handler
294 * Handler for SIGTRAP.
296 static HANDLER_DEF(trap_handler)
298 EXCEPTION_RECORD rec;
299 CONTEXT context;
301 save_context( &context, HANDLER_CONTEXT );
302 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
303 rec.ExceptionRecord = NULL;
304 rec.ExceptionAddress = (LPVOID)context.Rip;
305 rec.NumberParameters = 0;
307 switch(FAULT_CODE)
309 case TRAP_TRACE: /* Single-step exception */
310 rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
311 EFL_sig(HANDLER_CONTEXT) &= ~0x100; /* clear single-step flag */
312 break;
313 case TRAP_BRKPT: /* Breakpoint exception */
314 rec.ExceptionAddress = (char *)rec.ExceptionAddress - 1; /* back up over the int3 instruction */
315 /* fall through */
316 default:
317 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
318 break;
321 __regs_RtlRaiseException( &rec, &context );
322 restore_context( &context, HANDLER_CONTEXT );
325 /**********************************************************************
326 * fpe_handler
328 * Handler for SIGFPE.
330 static HANDLER_DEF(fpe_handler)
332 EXCEPTION_RECORD rec;
333 CONTEXT context;
335 save_context( &context, HANDLER_CONTEXT );
336 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
337 rec.ExceptionRecord = NULL;
338 rec.ExceptionAddress = (LPVOID)context.Rip;
339 rec.NumberParameters = 0;
341 switch (FAULT_CODE)
343 case FPE_FLTSUB:
344 rec.ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
345 break;
346 case FPE_INTDIV:
347 rec.ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
348 break;
349 case FPE_INTOVF:
350 rec.ExceptionCode = EXCEPTION_INT_OVERFLOW;
351 break;
352 case FPE_FLTDIV:
353 rec.ExceptionCode = EXCEPTION_FLT_DIVIDE_BY_ZERO;
354 break;
355 case FPE_FLTOVF:
356 rec.ExceptionCode = EXCEPTION_FLT_OVERFLOW;
357 break;
358 case FPE_FLTUND:
359 rec.ExceptionCode = EXCEPTION_FLT_UNDERFLOW;
360 break;
361 case FPE_FLTRES:
362 rec.ExceptionCode = EXCEPTION_FLT_INEXACT_RESULT;
363 break;
364 case FPE_FLTINV:
365 default:
366 rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
367 break;
370 __regs_RtlRaiseException( &rec, &context );
371 restore_context( &context, HANDLER_CONTEXT );
374 /**********************************************************************
375 * int_handler
377 * Handler for SIGINT.
379 static HANDLER_DEF(int_handler)
381 if (!dispatch_signal(SIGINT))
383 EXCEPTION_RECORD rec;
384 CONTEXT context;
386 save_context( &context, HANDLER_CONTEXT );
387 rec.ExceptionCode = CONTROL_C_EXIT;
388 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
389 rec.ExceptionRecord = NULL;
390 rec.ExceptionAddress = (LPVOID)context.Rip;
391 rec.NumberParameters = 0;
392 __regs_RtlRaiseException( &rec, &context );
393 restore_context( &context, HANDLER_CONTEXT );
398 /**********************************************************************
399 * abrt_handler
401 * Handler for SIGABRT.
403 static HANDLER_DEF(abrt_handler)
405 EXCEPTION_RECORD rec;
406 CONTEXT context;
408 save_context( &context, HANDLER_CONTEXT );
409 rec.ExceptionCode = EXCEPTION_WINE_ASSERTION;
410 rec.ExceptionFlags = EH_NONCONTINUABLE;
411 rec.ExceptionRecord = NULL;
412 rec.ExceptionAddress = (LPVOID)context.Rip;
413 rec.NumberParameters = 0;
414 __regs_RtlRaiseException( &rec, &context ); /* Should never return.. */
415 restore_context( &context, HANDLER_CONTEXT );
419 /**********************************************************************
420 * quit_handler
422 * Handler for SIGQUIT.
424 static HANDLER_DEF(quit_handler)
426 server_abort_thread(0);
430 /**********************************************************************
431 * usr1_handler
433 * Handler for SIGUSR1, used to signal a thread that it got suspended.
435 static HANDLER_DEF(usr1_handler)
437 CONTEXT context;
439 save_context( &context, HANDLER_CONTEXT );
440 wait_suspend( &context );
441 restore_context( &context, HANDLER_CONTEXT );
445 /**********************************************************************
446 * get_signal_stack_total_size
448 * Retrieve the size to allocate for the signal stack, including the TEB at the bottom.
449 * Must be a power of two.
451 size_t get_signal_stack_total_size(void)
453 assert( sizeof(TEB) <= 2*getpagesize() );
454 return 2*getpagesize(); /* this is just for the TEB, we don't need a signal stack */
458 /***********************************************************************
459 * set_handler
461 * Set a signal handler
463 static int set_handler( int sig, void (*func)() )
465 struct sigaction sig_act;
467 sig_act.sa_sigaction = func;
468 sig_act.sa_mask = server_block_set;
469 sig_act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
470 return sigaction( sig, &sig_act, NULL );
474 /***********************************************************************
475 * __wine_set_signal_handler (NTDLL.@)
477 int CDECL __wine_set_signal_handler(unsigned int sig, wine_signal_handler wsh)
479 if (sig > sizeof(handlers) / sizeof(handlers[0])) return -1;
480 if (handlers[sig] != NULL) return -2;
481 handlers[sig] = wsh;
482 return 0;
486 /**********************************************************************
487 * signal_init_thread
489 void signal_init_thread(void)
493 /**********************************************************************
494 * signal_init_process
496 void signal_init_process(void)
498 if (set_handler( SIGINT, (void (*)())int_handler ) == -1) goto error;
499 if (set_handler( SIGFPE, (void (*)())fpe_handler ) == -1) goto error;
500 if (set_handler( SIGSEGV, (void (*)())segv_handler ) == -1) goto error;
501 if (set_handler( SIGILL, (void (*)())segv_handler ) == -1) goto error;
502 if (set_handler( SIGABRT, (void (*)())abrt_handler ) == -1) goto error;
503 if (set_handler( SIGQUIT, (void (*)())quit_handler ) == -1) goto error;
504 if (set_handler( SIGUSR1, (void (*)())usr1_handler ) == -1) goto error;
505 #ifdef SIGBUS
506 if (set_handler( SIGBUS, (void (*)())segv_handler ) == -1) goto error;
507 #endif
508 #ifdef SIGTRAP
509 if (set_handler( SIGTRAP, (void (*)())trap_handler ) == -1) goto error;
510 #endif
511 signal_init_thread();
512 return;
514 error:
515 perror("sigaction");
516 exit(1);
520 /**********************************************************************
521 * RtlLookupFunctionEntry (NTDLL.@)
523 PRUNTIME_FUNCTION WINAPI RtlLookupFunctionEntry( ULONG64 pc, ULONG64 *base,
524 UNWIND_HISTORY_TABLE *table )
526 FIXME("stub\n");
527 return NULL;
531 /**********************************************************************
532 * RtlVirtualUnwind (NTDLL.@)
534 PVOID WINAPI RtlVirtualUnwind ( ULONG type, ULONG64 base, ULONG64 pc,
535 RUNTIME_FUNCTION *function, CONTEXT *context,
536 PVOID *data, ULONG64 *frame,
537 KNONVOLATILE_CONTEXT_POINTERS *ctx_ptr )
539 FIXME("stub\n");
540 return NULL;
544 /**********************************************************************
545 * __wine_enter_vm86 (NTDLL.@)
547 void __wine_enter_vm86( CONTEXT *context )
549 MESSAGE("vm86 mode not supported on this platform\n");
552 /**********************************************************************
553 * DbgBreakPoint (NTDLL.@)
555 __ASM_GLOBAL_FUNC( DbgBreakPoint, "int $3; ret")
557 /**********************************************************************
558 * DbgUserBreakPoint (NTDLL.@)
560 __ASM_GLOBAL_FUNC( DbgUserBreakPoint, "int $3; ret")
562 #endif /* __x86_64__ */