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
24 #include "wine/port.h"
35 #ifdef HAVE_SYS_PARAM_H
36 # include <sys/param.h>
38 #ifdef HAVE_SYS_SIGNAL_H
39 # include <sys/signal.h>
42 #define NONAMELESSUNION
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
58 #include <asm/prctl.h>
59 extern int arch_prctl(int func
, void *ptr
);
61 typedef struct ucontext SIGCONTEXT
;
63 # define HANDLER_DEF(name) void name( int __signal, struct siginfo *__siginfo, SIGCONTEXT *__context )
64 # define HANDLER_CONTEXT (__context)
66 #define RAX_sig(context) ((context)->uc_mcontext.gregs[REG_RAX])
67 #define RBX_sig(context) ((context)->uc_mcontext.gregs[REG_RBX])
68 #define RCX_sig(context) ((context)->uc_mcontext.gregs[REG_RCX])
69 #define RDX_sig(context) ((context)->uc_mcontext.gregs[REG_RDX])
70 #define RSI_sig(context) ((context)->uc_mcontext.gregs[REG_RSI])
71 #define RDI_sig(context) ((context)->uc_mcontext.gregs[REG_RDI])
72 #define RBP_sig(context) ((context)->uc_mcontext.gregs[REG_RBP])
73 #define R8_sig(context) ((context)->uc_mcontext.gregs[REG_R8])
74 #define R9_sig(context) ((context)->uc_mcontext.gregs[REG_R9])
75 #define R10_sig(context) ((context)->uc_mcontext.gregs[REG_R10])
76 #define R11_sig(context) ((context)->uc_mcontext.gregs[REG_R11])
77 #define R12_sig(context) ((context)->uc_mcontext.gregs[REG_R12])
78 #define R13_sig(context) ((context)->uc_mcontext.gregs[REG_R13])
79 #define R14_sig(context) ((context)->uc_mcontext.gregs[REG_R14])
80 #define R15_sig(context) ((context)->uc_mcontext.gregs[REG_R15])
82 #define CS_sig(context) (*((WORD *)&(context)->uc_mcontext.gregs[REG_CSGSFS] + 0))
83 #define GS_sig(context) (*((WORD *)&(context)->uc_mcontext.gregs[REG_CSGSFS] + 1))
84 #define FS_sig(context) (*((WORD *)&(context)->uc_mcontext.gregs[REG_CSGSFS] + 2))
86 #define RSP_sig(context) ((context)->uc_mcontext.gregs[REG_RSP])
87 #define RIP_sig(context) ((context)->uc_mcontext.gregs[REG_RIP])
88 #define EFL_sig(context) ((context)->uc_mcontext.gregs[REG_EFL])
89 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
90 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
92 #define FPU_sig(context) ((XMM_SAVE_AREA32 *)((context)->uc_mcontext.fpregs))
94 #define FAULT_CODE (__siginfo->si_code)
95 #define FAULT_ADDRESS (__siginfo->si_addr)
99 #if defined(__NetBSD__)
100 # include <sys/ucontext.h>
101 # include <sys/types.h>
104 typedef ucontext_t SIGCONTEXT
;
106 #define RAX_sig(context) ((context)->uc_mcontext.__gregs[_REG_RAX])
107 #define RBX_sig(context) ((context)->uc_mcontext.__gregs[_REG_RBX])
108 #define RCX_sig(context) ((context)->uc_mcontext.__gregs[_REG_RCX])
109 #define RDX_sig(context) ((context)->uc_mcontext.__gregs[_REG_RDX])
110 #define RSI_sig(context) ((context)->uc_mcontext.__gregs[_REG_RSI])
111 #define RDI_sig(context) ((context)->uc_mcontext.__gregs[_REG_RDI])
112 #define RBP_sig(context) ((context)->uc_mcontext.__gregs[_REG_RBP])
113 #define R8_sig(context) ((context)->uc_mcontext.__gregs[_REG_R8])
114 #define R9_sig(context) ((context)->uc_mcontext.__gregs[_REG_R9])
115 #define R10_sig(context) ((context)->uc_mcontext.__gregs[_REG_R10])
116 #define R11_sig(context) ((context)->uc_mcontext.__gregs[_REG_R11])
117 #define R12_sig(context) ((context)->uc_mcontext.__gregs[_REG_R12])
118 #define R13_sig(context) ((context)->uc_mcontext.__gregs[_REG_R13])
119 #define R14_sig(context) ((context)->uc_mcontext.__gregs[_REG_R14])
120 #define R15_sig(context) ((context)->uc_mcontext.__gregs[_REG_R15])
122 #define CS_sig(context) ((context)->uc_mcontext.__gregs[_REG_CS])
123 #define DS_sig(context) ((context)->uc_mcontext.__gregs[_REG_DS])
124 #define ES_sig(context) ((context)->uc_mcontext.__gregs[_REG_ES])
125 #define FS_sig(context) ((context)->uc_mcontext.__gregs[_REG_FS])
126 #define GS_sig(context) ((context)->uc_mcontext.__gregs[_REG_GS])
127 #define SS_sig(context) ((context)->uc_mcontext.__gregs[_REG_SS])
129 #define EFL_sig(context) ((context)->uc_mcontext.__gregs[_REG_RFL])
131 #define RIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext.__gregs[_REG_RIP]))
132 #define RSP_sig(context) (*((unsigned long*)&(context)->uc_mcontext.__gregs[_REG_URSP]))
134 #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
135 #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR])
137 #define FAULT_CODE (__siginfo->si_code)
138 #define FAULT_ADDRESS (__siginfo->si_addr)
140 #define HANDLER_DEF(name) void name( int __signal, siginfo_t *__siginfo, SIGCONTEXT *__context )
141 #define HANDLER_CONTEXT (__context)
143 #define FPU_sig(context) ((XMM_SAVE_AREA32 *)((context)->uc_mcontext.__fpregs))
144 #endif /* __NetBSD__ */
148 TRAP_x86_UNKNOWN
= -1, /* Unknown fault (TRAP_sig not defined) */
149 TRAP_x86_DIVIDE
= 0, /* Division by zero exception */
150 TRAP_x86_TRCTRAP
= 1, /* Single-step exception */
151 TRAP_x86_NMI
= 2, /* NMI interrupt */
152 TRAP_x86_BPTFLT
= 3, /* Breakpoint exception */
153 TRAP_x86_OFLOW
= 4, /* Overflow exception */
154 TRAP_x86_BOUND
= 5, /* Bound range exception */
155 TRAP_x86_PRIVINFLT
= 6, /* Invalid opcode exception */
156 TRAP_x86_DNA
= 7, /* Device not available exception */
157 TRAP_x86_DOUBLEFLT
= 8, /* Double fault exception */
158 TRAP_x86_FPOPFLT
= 9, /* Coprocessor segment overrun */
159 TRAP_x86_TSSFLT
= 10, /* Invalid TSS exception */
160 TRAP_x86_SEGNPFLT
= 11, /* Segment not present exception */
161 TRAP_x86_STKFLT
= 12, /* Stack fault */
162 TRAP_x86_PROTFLT
= 13, /* General protection fault */
163 TRAP_x86_PAGEFLT
= 14, /* Page fault */
164 TRAP_x86_ARITHTRAP
= 16, /* Floating point exception */
165 TRAP_x86_ALIGNFLT
= 17, /* Alignment check exception */
166 TRAP_x86_MCHK
= 18, /* Machine check exception */
167 TRAP_x86_CACHEFLT
= 19 /* Cache flush exception */
170 typedef int (*wine_signal_handler
)(unsigned int sig
);
172 static wine_signal_handler handlers
[256];
174 /***********************************************************************
177 static inline int dispatch_signal(unsigned int sig
)
179 if (handlers
[sig
] == NULL
) return 0;
180 return handlers
[sig
](sig
);
183 /***********************************************************************
186 * Set the register values from a sigcontext.
188 static void save_context( CONTEXT
*context
, const SIGCONTEXT
*sigcontext
)
190 context
->Rax
= RAX_sig(sigcontext
);
191 context
->Rcx
= RCX_sig(sigcontext
);
192 context
->Rdx
= RDX_sig(sigcontext
);
193 context
->Rbx
= RBX_sig(sigcontext
);
194 context
->Rsp
= RSP_sig(sigcontext
);
195 context
->Rbp
= RBP_sig(sigcontext
);
196 context
->Rsi
= RSI_sig(sigcontext
);
197 context
->Rdi
= RDI_sig(sigcontext
);
198 context
->R8
= R8_sig(sigcontext
);
199 context
->R9
= R9_sig(sigcontext
);
200 context
->R10
= R10_sig(sigcontext
);
201 context
->R11
= R11_sig(sigcontext
);
202 context
->R12
= R12_sig(sigcontext
);
203 context
->R13
= R13_sig(sigcontext
);
204 context
->R14
= R14_sig(sigcontext
);
205 context
->R15
= R15_sig(sigcontext
);
206 context
->Rip
= RIP_sig(sigcontext
);
207 context
->SegCs
= CS_sig(sigcontext
);
208 context
->SegFs
= FS_sig(sigcontext
);
209 context
->SegGs
= GS_sig(sigcontext
);
210 context
->EFlags
= EFL_sig(sigcontext
);
211 context
->SegDs
= 0; /* FIXME */
212 context
->SegEs
= 0; /* FIXME */
213 context
->SegSs
= 0; /* FIXME */
214 context
->MxCsr
= 0; /* FIXME */
215 if (FPU_sig(sigcontext
)) context
->u
.FltSave
= *FPU_sig(sigcontext
);
219 /***********************************************************************
222 * Build a sigcontext from the register values.
224 static void restore_context( const CONTEXT
*context
, SIGCONTEXT
*sigcontext
)
226 RAX_sig(sigcontext
) = context
->Rax
;
227 RCX_sig(sigcontext
) = context
->Rcx
;
228 RDX_sig(sigcontext
) = context
->Rdx
;
229 RBX_sig(sigcontext
) = context
->Rbx
;
230 RSP_sig(sigcontext
) = context
->Rsp
;
231 RBP_sig(sigcontext
) = context
->Rbp
;
232 RSI_sig(sigcontext
) = context
->Rsi
;
233 RDI_sig(sigcontext
) = context
->Rdi
;
234 R8_sig(sigcontext
) = context
->R8
;
235 R9_sig(sigcontext
) = context
->R9
;
236 R10_sig(sigcontext
) = context
->R10
;
237 R11_sig(sigcontext
) = context
->R11
;
238 R12_sig(sigcontext
) = context
->R12
;
239 R13_sig(sigcontext
) = context
->R13
;
240 R14_sig(sigcontext
) = context
->R14
;
241 R15_sig(sigcontext
) = context
->R15
;
242 RIP_sig(sigcontext
) = context
->Rip
;
243 CS_sig(sigcontext
) = context
->SegCs
;
244 FS_sig(sigcontext
) = context
->SegFs
;
245 GS_sig(sigcontext
) = context
->SegGs
;
246 EFL_sig(sigcontext
) = context
->EFlags
;
247 if (FPU_sig(sigcontext
)) *FPU_sig(sigcontext
) = context
->u
.FltSave
;
251 /***********************************************************************
252 * RtlCaptureContext (NTDLL.@)
254 void WINAPI
__regs_RtlCaptureContext( CONTEXT
*context
, CONTEXT
*regs
)
258 DEFINE_REGS_ENTRYPOINT( RtlCaptureContext
, 1 )
261 /***********************************************************************
264 * Set the new CPU context.
266 void set_cpu_context( const CONTEXT
*context
)
268 FIXME("not implemented\n");
272 /**********************************************************************
275 * Handler for SIGSEGV and related errors.
277 static HANDLER_DEF(segv_handler
)
279 EXCEPTION_RECORD rec
;
282 save_context( &context
, HANDLER_CONTEXT
);
284 rec
.ExceptionRecord
= NULL
;
285 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
286 rec
.ExceptionAddress
= (LPVOID
)context
.Rip
;
287 rec
.NumberParameters
= 0;
289 switch(TRAP_sig(HANDLER_CONTEXT
))
291 case TRAP_x86_OFLOW
: /* Overflow exception */
292 rec
.ExceptionCode
= EXCEPTION_INT_OVERFLOW
;
294 case TRAP_x86_BOUND
: /* Bound range exception */
295 rec
.ExceptionCode
= EXCEPTION_ARRAY_BOUNDS_EXCEEDED
;
297 case TRAP_x86_PRIVINFLT
: /* Invalid opcode exception */
298 rec
.ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
300 case TRAP_x86_STKFLT
: /* Stack fault */
301 rec
.ExceptionCode
= EXCEPTION_STACK_OVERFLOW
;
303 case TRAP_x86_SEGNPFLT
: /* Segment not present exception */
304 case TRAP_x86_PROTFLT
: /* General protection fault */
305 case TRAP_x86_UNKNOWN
: /* Unknown fault code */
306 rec
.ExceptionCode
= ERROR_sig(HANDLER_CONTEXT
) ? EXCEPTION_ACCESS_VIOLATION
307 : EXCEPTION_PRIV_INSTRUCTION
;
309 case TRAP_x86_PAGEFLT
: /* Page fault */
310 rec
.ExceptionCode
= EXCEPTION_ACCESS_VIOLATION
;
312 rec
.NumberParameters
= 2;
313 rec
.ExceptionInformation
[0] = (ERROR_sig(HANDLER_CONTEXT
) & 2) != 0;
314 rec
.ExceptionInformation
[1] = (ULONG_PTR
)FAULT_ADDRESS
;
315 if (!(rec
.ExceptionCode
= virtual_handle_fault( FAULT_ADDRESS
, rec
.ExceptionInformation
[0] )))
319 case TRAP_x86_ALIGNFLT
: /* Alignment check exception */
320 rec
.ExceptionCode
= EXCEPTION_DATATYPE_MISALIGNMENT
;
323 ERR( "Got unexpected trap %ld\n", TRAP_sig(HANDLER_CONTEXT
) );
325 case TRAP_x86_NMI
: /* NMI interrupt */
326 case TRAP_x86_DNA
: /* Device not available exception */
327 case TRAP_x86_DOUBLEFLT
: /* Double fault exception */
328 case TRAP_x86_TSSFLT
: /* Invalid TSS exception */
329 case TRAP_x86_MCHK
: /* Machine check exception */
330 case TRAP_x86_CACHEFLT
: /* Cache flush exception */
331 rec
.ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
335 __regs_RtlRaiseException( &rec
, &context
);
337 restore_context( &context
, HANDLER_CONTEXT
);
340 /**********************************************************************
343 * Handler for SIGTRAP.
345 static HANDLER_DEF(trap_handler
)
347 EXCEPTION_RECORD rec
;
350 save_context( &context
, HANDLER_CONTEXT
);
351 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
352 rec
.ExceptionRecord
= NULL
;
353 rec
.ExceptionAddress
= (LPVOID
)context
.Rip
;
354 rec
.NumberParameters
= 0;
358 case TRAP_TRACE
: /* Single-step exception */
359 rec
.ExceptionCode
= EXCEPTION_SINGLE_STEP
;
360 EFL_sig(HANDLER_CONTEXT
) &= ~0x100; /* clear single-step flag */
362 case TRAP_BRKPT
: /* Breakpoint exception */
363 rec
.ExceptionAddress
= (char *)rec
.ExceptionAddress
- 1; /* back up over the int3 instruction */
366 rec
.ExceptionCode
= EXCEPTION_BREAKPOINT
;
370 __regs_RtlRaiseException( &rec
, &context
);
371 restore_context( &context
, HANDLER_CONTEXT
);
374 /**********************************************************************
377 * Handler for SIGFPE.
379 static HANDLER_DEF(fpe_handler
)
381 EXCEPTION_RECORD rec
;
384 save_context( &context
, HANDLER_CONTEXT
);
385 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
386 rec
.ExceptionRecord
= NULL
;
387 rec
.ExceptionAddress
= (LPVOID
)context
.Rip
;
388 rec
.NumberParameters
= 0;
393 rec
.ExceptionCode
= EXCEPTION_ARRAY_BOUNDS_EXCEEDED
;
396 rec
.ExceptionCode
= EXCEPTION_INT_DIVIDE_BY_ZERO
;
399 rec
.ExceptionCode
= EXCEPTION_INT_OVERFLOW
;
402 rec
.ExceptionCode
= EXCEPTION_FLT_DIVIDE_BY_ZERO
;
405 rec
.ExceptionCode
= EXCEPTION_FLT_OVERFLOW
;
408 rec
.ExceptionCode
= EXCEPTION_FLT_UNDERFLOW
;
411 rec
.ExceptionCode
= EXCEPTION_FLT_INEXACT_RESULT
;
415 rec
.ExceptionCode
= EXCEPTION_FLT_INVALID_OPERATION
;
419 __regs_RtlRaiseException( &rec
, &context
);
420 restore_context( &context
, HANDLER_CONTEXT
);
423 /**********************************************************************
426 * Handler for SIGINT.
428 static HANDLER_DEF(int_handler
)
430 if (!dispatch_signal(SIGINT
))
432 EXCEPTION_RECORD rec
;
435 save_context( &context
, HANDLER_CONTEXT
);
436 rec
.ExceptionCode
= CONTROL_C_EXIT
;
437 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
438 rec
.ExceptionRecord
= NULL
;
439 rec
.ExceptionAddress
= (LPVOID
)context
.Rip
;
440 rec
.NumberParameters
= 0;
441 __regs_RtlRaiseException( &rec
, &context
);
442 restore_context( &context
, HANDLER_CONTEXT
);
447 /**********************************************************************
450 * Handler for SIGABRT.
452 static HANDLER_DEF(abrt_handler
)
454 EXCEPTION_RECORD rec
;
457 save_context( &context
, HANDLER_CONTEXT
);
458 rec
.ExceptionCode
= EXCEPTION_WINE_ASSERTION
;
459 rec
.ExceptionFlags
= EH_NONCONTINUABLE
;
460 rec
.ExceptionRecord
= NULL
;
461 rec
.ExceptionAddress
= (LPVOID
)context
.Rip
;
462 rec
.NumberParameters
= 0;
463 __regs_RtlRaiseException( &rec
, &context
); /* Should never return.. */
464 restore_context( &context
, HANDLER_CONTEXT
);
468 /**********************************************************************
471 * Handler for SIGQUIT.
473 static HANDLER_DEF(quit_handler
)
479 /**********************************************************************
482 * Handler for SIGUSR1, used to signal a thread that it got suspended.
484 static HANDLER_DEF(usr1_handler
)
488 save_context( &context
, HANDLER_CONTEXT
);
489 wait_suspend( &context
);
490 restore_context( &context
, HANDLER_CONTEXT
);
494 /**********************************************************************
495 * get_signal_stack_total_size
497 * Retrieve the size to allocate for the signal stack, including the TEB at the bottom.
498 * Must be a power of two.
500 size_t get_signal_stack_total_size(void)
502 assert( sizeof(TEB
) <= 2*getpagesize() );
503 return 2*getpagesize(); /* this is just for the TEB, we don't need a signal stack */
507 /***********************************************************************
510 * Set a signal handler
512 static int set_handler( int sig
, void (*func
)() )
514 struct sigaction sig_act
;
516 sig_act
.sa_sigaction
= func
;
517 sig_act
.sa_mask
= server_block_set
;
518 sig_act
.sa_flags
= SA_RESTART
| SA_SIGINFO
| SA_ONSTACK
;
519 return sigaction( sig
, &sig_act
, NULL
);
523 /***********************************************************************
524 * __wine_set_signal_handler (NTDLL.@)
526 int CDECL
__wine_set_signal_handler(unsigned int sig
, wine_signal_handler wsh
)
528 if (sig
> sizeof(handlers
) / sizeof(handlers
[0])) return -1;
529 if (handlers
[sig
] != NULL
) return -2;
535 /**********************************************************************
538 void signal_init_thread( TEB
*teb
)
541 arch_prctl( ARCH_SET_GS
, teb
);
543 # error Please define setting %gs for your architecture
547 /**********************************************************************
548 * signal_init_process
550 void signal_init_process(void)
552 if (set_handler( SIGINT
, (void (*)())int_handler
) == -1) goto error
;
553 if (set_handler( SIGFPE
, (void (*)())fpe_handler
) == -1) goto error
;
554 if (set_handler( SIGSEGV
, (void (*)())segv_handler
) == -1) goto error
;
555 if (set_handler( SIGILL
, (void (*)())segv_handler
) == -1) goto error
;
556 if (set_handler( SIGABRT
, (void (*)())abrt_handler
) == -1) goto error
;
557 if (set_handler( SIGQUIT
, (void (*)())quit_handler
) == -1) goto error
;
558 if (set_handler( SIGUSR1
, (void (*)())usr1_handler
) == -1) goto error
;
560 if (set_handler( SIGBUS
, (void (*)())segv_handler
) == -1) goto error
;
563 if (set_handler( SIGTRAP
, (void (*)())trap_handler
) == -1) goto error
;
573 /**********************************************************************
574 * RtlLookupFunctionEntry (NTDLL.@)
576 PRUNTIME_FUNCTION WINAPI
RtlLookupFunctionEntry( ULONG64 pc
, ULONG64
*base
,
577 UNWIND_HISTORY_TABLE
*table
)
584 /**********************************************************************
585 * RtlVirtualUnwind (NTDLL.@)
587 PVOID WINAPI
RtlVirtualUnwind ( ULONG type
, ULONG64 base
, ULONG64 pc
,
588 RUNTIME_FUNCTION
*function
, CONTEXT
*context
,
589 PVOID
*data
, ULONG64
*frame
,
590 KNONVOLATILE_CONTEXT_POINTERS
*ctx_ptr
)
597 /**********************************************************************
598 * __wine_enter_vm86 (NTDLL.@)
600 void __wine_enter_vm86( CONTEXT
*context
)
602 MESSAGE("vm86 mode not supported on this platform\n");
605 /**********************************************************************
606 * DbgBreakPoint (NTDLL.@)
608 __ASM_GLOBAL_FUNC( DbgBreakPoint
, "int $3; ret")
610 /**********************************************************************
611 * DbgUserBreakPoint (NTDLL.@)
613 __ASM_GLOBAL_FUNC( DbgUserBreakPoint
, "int $3; ret")
615 #endif /* __x86_64__ */