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
28 #include <sys/types.h>
31 #define WIN32_NO_STATUS
33 #include "ntdll_misc.h"
34 #include "wine/exception.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(seh
);
39 struct x86_thread_data
41 DWORD fs
; /* 1d4 TEB selector */
42 DWORD gs
; /* 1d8 libc selector; update winebuild if you move this! */
43 DWORD dr0
; /* 1dc debug registers */
49 void *exit_frame
; /* 1f4 exit frame pointer */
52 C_ASSERT( sizeof(struct x86_thread_data
) <= 16 * sizeof(void *) );
53 C_ASSERT( offsetof( TEB
, GdiTebBatch
) + offsetof( struct x86_thread_data
, gs
) == 0x1d8 );
54 C_ASSERT( offsetof( TEB
, GdiTebBatch
) + offsetof( struct x86_thread_data
, exit_frame
) == 0x1f4 );
56 static inline struct x86_thread_data
*x86_thread_data(void)
58 return (struct x86_thread_data
*)&NtCurrentTeb()->GdiTebBatch
;
61 struct ldt_copy
*__wine_ldt_copy
= NULL
;
63 /* Exception record for handling exceptions happening inside exception handlers */
66 EXCEPTION_REGISTRATION_RECORD frame
;
67 EXCEPTION_REGISTRATION_RECORD
*prevFrame
;
70 extern DWORD
EXC_CallHandler( EXCEPTION_RECORD
*record
, EXCEPTION_REGISTRATION_RECORD
*frame
,
71 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**dispatcher
,
72 PEXCEPTION_HANDLER handler
, PEXCEPTION_HANDLER nested_handler
);
74 /*******************************************************************
77 static inline BOOL
is_valid_frame( void *frame
)
79 if ((ULONG_PTR
)frame
& 3) return FALSE
;
80 return (frame
>= NtCurrentTeb()->Tib
.StackLimit
&&
81 (void **)frame
< (void **)NtCurrentTeb()->Tib
.StackBase
- 1);
84 /*******************************************************************
87 * Handler for exceptions happening inside a handler.
89 static DWORD
raise_handler( EXCEPTION_RECORD
*rec
, EXCEPTION_REGISTRATION_RECORD
*frame
,
90 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**dispatcher
)
92 if (rec
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
))
93 return ExceptionContinueSearch
;
94 /* We shouldn't get here so we store faulty frame in dispatcher */
95 *dispatcher
= ((EXC_NESTED_FRAME
*)frame
)->prevFrame
;
96 return ExceptionNestedException
;
100 /*******************************************************************
103 * Handler for exceptions happening inside an unwind handler.
105 static DWORD
unwind_handler( EXCEPTION_RECORD
*rec
, EXCEPTION_REGISTRATION_RECORD
*frame
,
106 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**dispatcher
)
108 if (!(rec
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
)))
109 return ExceptionContinueSearch
;
110 /* We shouldn't get here so we store faulty frame in dispatcher */
111 *dispatcher
= ((EXC_NESTED_FRAME
*)frame
)->prevFrame
;
112 return ExceptionCollidedUnwind
;
116 /**********************************************************************
117 * call_stack_handlers
119 * Call the stack handlers chain.
121 static NTSTATUS
call_stack_handlers( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
123 EXCEPTION_REGISTRATION_RECORD
*frame
, *dispatch
, *nested_frame
;
126 frame
= NtCurrentTeb()->Tib
.ExceptionList
;
128 while (frame
!= (EXCEPTION_REGISTRATION_RECORD
*)~0UL)
130 /* Check frame address */
131 if (!is_valid_frame( frame
))
133 rec
->ExceptionFlags
|= EH_STACK_INVALID
;
138 TRACE( "calling handler at %p code=%x flags=%x\n",
139 frame
->Handler
, rec
->ExceptionCode
, rec
->ExceptionFlags
);
140 res
= EXC_CallHandler( rec
, frame
, context
, &dispatch
, frame
->Handler
, raise_handler
);
141 TRACE( "handler at %p returned %x\n", frame
->Handler
, res
);
143 if (frame
== nested_frame
)
145 /* no longer nested */
147 rec
->ExceptionFlags
&= ~EH_NESTED_CALL
;
152 case ExceptionContinueExecution
:
153 if (!(rec
->ExceptionFlags
& EH_NONCONTINUABLE
)) return STATUS_SUCCESS
;
154 return STATUS_NONCONTINUABLE_EXCEPTION
;
155 case ExceptionContinueSearch
:
157 case ExceptionNestedException
:
158 if (nested_frame
< dispatch
) nested_frame
= dispatch
;
159 rec
->ExceptionFlags
|= EH_NESTED_CALL
;
162 return STATUS_INVALID_DISPOSITION
;
166 return STATUS_UNHANDLED_EXCEPTION
;
170 /*******************************************************************
171 * KiUserExceptionDispatcher (NTDLL.@)
173 NTSTATUS WINAPI
dispatch_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
178 TRACE( "code=%x flags=%x addr=%p ip=%08x tid=%04x\n",
179 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
,
180 context
->Eip
, GetCurrentThreadId() );
181 for (c
= 0; c
< rec
->NumberParameters
; c
++)
182 TRACE( " info[%d]=%08lx\n", c
, rec
->ExceptionInformation
[c
] );
184 if (rec
->ExceptionCode
== EXCEPTION_WINE_STUB
)
186 if (rec
->ExceptionInformation
[1] >> 16)
187 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
188 rec
->ExceptionAddress
,
189 (char*)rec
->ExceptionInformation
[0], (char*)rec
->ExceptionInformation
[1] );
191 MESSAGE( "wine: Call from %p to unimplemented function %s.%ld, aborting\n",
192 rec
->ExceptionAddress
,
193 (char*)rec
->ExceptionInformation
[0], rec
->ExceptionInformation
[1] );
195 else if (rec
->ExceptionCode
== EXCEPTION_WINE_NAME_THREAD
&& rec
->ExceptionInformation
[0] == 0x1000)
197 WARN( "Thread %04x renamed to %s\n", (DWORD
)rec
->ExceptionInformation
[2], debugstr_a((char *)rec
->ExceptionInformation
[1]) );
199 else if (rec
->ExceptionCode
== DBG_PRINTEXCEPTION_C
)
201 WARN( "%s\n", debugstr_an((char *)rec
->ExceptionInformation
[1], rec
->ExceptionInformation
[0] - 1) );
203 else if (rec
->ExceptionCode
== DBG_PRINTEXCEPTION_WIDE_C
)
205 WARN( "%s\n", debugstr_wn((WCHAR
*)rec
->ExceptionInformation
[1], rec
->ExceptionInformation
[0] - 1) );
209 if (rec
->ExceptionCode
== STATUS_ASSERTION_FAILURE
)
210 ERR( "%s exception (code=%x) raised\n", debugstr_exception_code(rec
->ExceptionCode
), rec
->ExceptionCode
);
212 WARN( "%s exception (code=%x) raised\n", debugstr_exception_code(rec
->ExceptionCode
), rec
->ExceptionCode
);
214 TRACE(" eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x\n",
215 context
->Eax
, context
->Ebx
, context
->Ecx
,
216 context
->Edx
, context
->Esi
, context
->Edi
);
217 TRACE(" ebp=%08x esp=%08x cs=%04x ds=%04x es=%04x fs=%04x gs=%04x flags=%08x\n",
218 context
->Ebp
, context
->Esp
, context
->SegCs
, context
->SegDs
,
219 context
->SegEs
, context
->SegFs
, context
->SegGs
, context
->EFlags
);
222 if (call_vectored_handlers( rec
, context
) == EXCEPTION_CONTINUE_EXECUTION
)
223 NtContinue( context
, FALSE
);
225 if ((status
= call_stack_handlers( rec
, context
)) == STATUS_SUCCESS
)
226 NtContinue( context
, FALSE
);
228 if (status
!= STATUS_UNHANDLED_EXCEPTION
) RtlRaiseStatus( status
);
229 return NtRaiseException( rec
, context
, FALSE
);
232 __ASM_STDCALL_FUNC( KiUserExceptionDispatcher
, 8,
235 "call " __ASM_STDCALL("dispatch_exception", 8) "\n\t"
239 /*******************************************************************
240 * KiUserApcDispatcher (NTDLL.@)
242 void WINAPI
KiUserApcDispatcher( CONTEXT
*context
, ULONG_PTR ctx
, ULONG_PTR arg1
, ULONG_PTR arg2
,
245 func( ctx
, arg1
, arg2
);
246 NtContinue( context
, TRUE
);
250 /***********************************************************************
253 * Save the thread FPU context.
255 static inline void save_fpu( CONTEXT
*context
)
270 context
->ContextFlags
|= CONTEXT_FLOATING_POINT
;
271 __asm__
__volatile__( "fnsave %0; fwait" : "=m" (context
->FloatSave
) );
273 /* Reset unmasked exceptions status to avoid firing an exception. */
274 memcpy(&float_status
, &context
->FloatSave
, sizeof(float_status
));
275 float_status
.StatusWord
&= float_status
.ControlWord
| 0xffffff80;
277 __asm__
__volatile__( "fldenv %0" : : "m" (float_status
) );
282 /***********************************************************************
285 * Save the thread FPU extended context.
287 static inline void save_fpux( CONTEXT
*context
)
290 /* we have to enforce alignment by hand */
291 char buffer
[sizeof(XSAVE_FORMAT
) + 16];
292 XSAVE_FORMAT
*state
= (XSAVE_FORMAT
*)(((ULONG_PTR
)buffer
+ 15) & ~15);
294 context
->ContextFlags
|= CONTEXT_EXTENDED_REGISTERS
;
295 __asm__
__volatile__( "fxsave %0" : "=m" (*state
) );
296 memcpy( context
->ExtendedRegisters
, state
, sizeof(*state
) );
301 /***********************************************************************
302 * RtlCaptureContext (NTDLL.@)
304 __ASM_STDCALL_FUNC( RtlCaptureContext
, 4,
306 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
307 "movl 8(%esp),%eax\n\t" /* context */
308 "movl $0x10007,(%eax)\n\t" /* context->ContextFlags */
309 "movw %gs,0x8c(%eax)\n\t" /* context->SegGs */
310 "movw %fs,0x90(%eax)\n\t" /* context->SegFs */
311 "movw %es,0x94(%eax)\n\t" /* context->SegEs */
312 "movw %ds,0x98(%eax)\n\t" /* context->SegDs */
313 "movl %edi,0x9c(%eax)\n\t" /* context->Edi */
314 "movl %esi,0xa0(%eax)\n\t" /* context->Esi */
315 "movl %ebx,0xa4(%eax)\n\t" /* context->Ebx */
316 "movl %edx,0xa8(%eax)\n\t" /* context->Edx */
317 "movl %ecx,0xac(%eax)\n\t" /* context->Ecx */
318 "movl 0(%ebp),%edx\n\t"
319 "movl %edx,0xb4(%eax)\n\t" /* context->Ebp */
320 "movl 4(%ebp),%edx\n\t"
321 "movl %edx,0xb8(%eax)\n\t" /* context->Eip */
322 "movw %cs,0xbc(%eax)\n\t" /* context->SegCs */
324 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
325 "popl 0xc0(%eax)\n\t" /* context->EFlags */
326 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
327 "leal 8(%ebp),%edx\n\t"
328 "movl %edx,0xc4(%eax)\n\t" /* context->Esp */
329 "movw %ss,0xc8(%eax)\n\t" /* context->SegSs */
330 "popl 0xb0(%eax)\n\t" /* context->Eax */
331 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
335 /*******************************************************************
336 * RtlUnwind (NTDLL.@)
338 void WINAPI DECLSPEC_HIDDEN
__regs_RtlUnwind( EXCEPTION_REGISTRATION_RECORD
* pEndFrame
, PVOID targetIp
,
339 PEXCEPTION_RECORD pRecord
, PVOID retval
, CONTEXT
*context
)
341 EXCEPTION_RECORD record
;
342 EXCEPTION_REGISTRATION_RECORD
*frame
, *dispatch
;
345 context
->Eax
= (DWORD
)retval
;
347 /* build an exception record, if we do not have one */
350 record
.ExceptionCode
= STATUS_UNWIND
;
351 record
.ExceptionFlags
= 0;
352 record
.ExceptionRecord
= NULL
;
353 record
.ExceptionAddress
= (void *)context
->Eip
;
354 record
.NumberParameters
= 0;
358 pRecord
->ExceptionFlags
|= EH_UNWINDING
| (pEndFrame
? 0 : EH_EXIT_UNWIND
);
360 TRACE( "code=%x flags=%x\n", pRecord
->ExceptionCode
, pRecord
->ExceptionFlags
);
361 TRACE( "eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x\n",
362 context
->Eax
, context
->Ebx
, context
->Ecx
, context
->Edx
, context
->Esi
, context
->Edi
);
363 TRACE( "ebp=%08x esp=%08x eip=%08x cs=%04x ds=%04x fs=%04x gs=%04x flags=%08x\n",
364 context
->Ebp
, context
->Esp
, context
->Eip
, LOWORD(context
->SegCs
), LOWORD(context
->SegDs
),
365 LOWORD(context
->SegFs
), LOWORD(context
->SegGs
), context
->EFlags
);
367 /* get chain of exception frames */
368 frame
= NtCurrentTeb()->Tib
.ExceptionList
;
369 while ((frame
!= (EXCEPTION_REGISTRATION_RECORD
*)~0UL) && (frame
!= pEndFrame
))
371 /* Check frame address */
372 if (pEndFrame
&& (frame
> pEndFrame
))
373 raise_status( STATUS_INVALID_UNWIND_TARGET
, pRecord
);
375 if (!is_valid_frame( frame
)) raise_status( STATUS_BAD_STACK
, pRecord
);
378 TRACE( "calling handler at %p code=%x flags=%x\n",
379 frame
->Handler
, pRecord
->ExceptionCode
, pRecord
->ExceptionFlags
);
380 res
= EXC_CallHandler( pRecord
, frame
, context
, &dispatch
, frame
->Handler
, unwind_handler
);
381 TRACE( "handler at %p returned %x\n", frame
->Handler
, res
);
385 case ExceptionContinueSearch
:
387 case ExceptionCollidedUnwind
:
391 raise_status( STATUS_INVALID_DISPOSITION
, pRecord
);
394 frame
= __wine_pop_frame( frame
);
396 NtContinue( context
, FALSE
);
398 __ASM_STDCALL_FUNC( RtlUnwind
, 16,
400 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
401 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
403 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
404 "leal -(0x2cc+8)(%esp),%esp\n\t" /* sizeof(CONTEXT) + alignment */
406 "leal 4(%esp),%eax\n\t" /* context */
407 "xchgl %eax,(%esp)\n\t"
408 "call " __ASM_STDCALL("RtlCaptureContext",4) "\n\t"
409 "leal 24(%ebp),%eax\n\t"
410 "movl %eax,0xc4(%esp)\n\t" /* context->Esp */
416 "call " __ASM_STDCALL("__regs_RtlUnwind",20) "\n\t"
418 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
419 __ASM_CFI(".cfi_same_value %ebp\n\t")
420 "ret $16" ) /* actually never returns */
423 /*******************************************************************
424 * raise_exception_full_context
426 * Raise an exception with the full CPU context.
428 void raise_exception_full_context( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
431 save_fpux( context
);
433 context
->Dr0
= x86_thread_data()->dr0
;
434 context
->Dr1
= x86_thread_data()->dr1
;
435 context
->Dr2
= x86_thread_data()->dr2
;
436 context
->Dr3
= x86_thread_data()->dr3
;
437 context
->Dr6
= x86_thread_data()->dr6
;
438 context
->Dr7
= x86_thread_data()->dr7
;
439 context
->ContextFlags
|= CONTEXT_DEBUG_REGISTERS
;
441 RtlRaiseStatus( NtRaiseException( rec
, context
, TRUE
));
445 /***********************************************************************
446 * RtlRaiseException (NTDLL.@)
448 __ASM_STDCALL_FUNC( RtlRaiseException
, 4,
450 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
451 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
453 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
454 "leal -0x2cc(%esp),%esp\n\t" /* sizeof(CONTEXT) */
455 "pushl %esp\n\t" /* context */
456 "call " __ASM_STDCALL("RtlCaptureContext",4) "\n\t"
457 "movl 4(%ebp),%eax\n\t" /* return address */
458 "movl 8(%ebp),%ecx\n\t" /* rec */
459 "movl %eax,12(%ecx)\n\t" /* rec->ExceptionAddress */
460 "leal 12(%ebp),%eax\n\t"
461 "movl %eax,0xc4(%esp)\n\t" /* context->Esp */
465 "call " __ASM_NAME("raise_exception_full_context") "\n\t"
467 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
468 __ASM_CFI(".cfi_same_value %ebp\n\t")
469 "ret $4" ) /* actually never returns */
472 /*************************************************************************
473 * RtlCaptureStackBackTrace (NTDLL.@)
475 USHORT WINAPI
RtlCaptureStackBackTrace( ULONG skip
, ULONG count
, PVOID
*buffer
, ULONG
*hash
)
481 RtlCaptureContext( &context
);
483 frame
= (ULONG
*)context
.Ebp
;
487 if (!is_valid_frame( frame
)) return 0;
488 frame
= (ULONG
*)*frame
;
491 for (i
= 0; i
< count
; i
++)
493 if (!is_valid_frame( frame
)) break;
494 buffer
[i
] = (void *)frame
[1];
495 if (hash
) *hash
+= frame
[1];
496 frame
= (ULONG
*)*frame
;
502 /***********************************************************************
503 * signal_start_thread
505 __ASM_GLOBAL_FUNC( signal_start_thread
,
506 "movl 4(%esp),%esi\n\t" /* context */
507 "leal -12(%esi),%edi\n\t"
508 /* clear the thread stack */
509 "andl $~0xfff,%edi\n\t" /* round down to page size */
510 "movl $0xf0000,%ecx\n\t"
516 /* switch to the initial context */
517 "leal -12(%esi),%esp\n\t"
518 "movl $1,4(%esp)\n\t"
519 "movl %esi,(%esp)\n\t"
520 "call " __ASM_STDCALL("NtContinue", 8) )
522 /**********************************************************************
523 * DbgBreakPoint (NTDLL.@)
525 __ASM_STDCALL_FUNC( DbgBreakPoint
, 0, "int $3; ret"
526 "\n\tnop; nop; nop; nop; nop; nop; nop; nop"
527 "\n\tnop; nop; nop; nop; nop; nop" );
529 /**********************************************************************
530 * DbgUserBreakPoint (NTDLL.@)
532 __ASM_STDCALL_FUNC( DbgUserBreakPoint
, 0, "int $3; ret"
533 "\n\tnop; nop; nop; nop; nop; nop; nop; nop"
534 "\n\tnop; nop; nop; nop; nop; nop" );
536 /**********************************************************************
537 * NtCurrentTeb (NTDLL.@)
539 __ASM_STDCALL_FUNC( NtCurrentTeb
, 0, ".byte 0x64\n\tmovl 0x18,%eax\n\tret" )
542 /**************************************************************************
545 __ASM_GLOBAL_FUNC( _chkstk
,
548 "xchgl %esp,%eax\n\t"
549 "movl 0(%eax),%eax\n\t" /* copy return address from old location */
550 "movl %eax,0(%esp)\n\t"
553 /**************************************************************************
554 * _alloca_probe (NTDLL.@)
556 __ASM_GLOBAL_FUNC( _alloca_probe
,
559 "xchgl %esp,%eax\n\t"
560 "movl 0(%eax),%eax\n\t" /* copy return address from old location */
561 "movl %eax,0(%esp)\n\t"
565 /**********************************************************************
566 * EXC_CallHandler (internal)
568 * Some exception handlers depend on EBP to have a fixed position relative to
569 * the exception frame.
570 * Shrinker depends on (*1) doing what it does,
571 * (*2) being the exact instruction it is and (*3) beginning with 0x64
572 * (i.e. the %fs prefix to the movl instruction). It also depends on the
573 * function calling the handler having only 5 parameters (*4).
575 __ASM_GLOBAL_FUNC( EXC_CallHandler
,
577 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
578 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
580 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
582 __ASM_CFI(".cfi_rel_offset %ebx,-4\n\t")
583 "movl 28(%ebp), %edx\n\t" /* ugly hack to pass the 6th param needed because of Shrinker */
589 "call " __ASM_NAME("call_exception_handler") "\n\t"
591 __ASM_CFI(".cfi_same_value %ebx\n\t")
593 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
594 __ASM_CFI(".cfi_same_value %ebp\n\t")
596 __ASM_GLOBAL_FUNC(call_exception_handler
,
598 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
599 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
601 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
603 "pushl 12(%ebp)\n\t" /* make any exceptions in this... */
604 "pushl %edx\n\t" /* handler be handled by... */
606 "pushl (0)\n\t" /* nested_handler (passed in edx). */
608 "movl %esp,(0)\n\t" /* push the new exception frame onto the exception stack. */
613 "movl 24(%ebp), %ecx\n\t" /* (*1) */
614 "call *%ecx\n\t" /* call handler. (*2) */
616 "movl (0), %esp\n\t" /* restore previous... (*3) */
618 "popl (0)\n\t" /* exception frame. */
619 "movl %ebp, %esp\n\t" /* restore saved stack, in case it was corrupted */
621 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
622 __ASM_CFI(".cfi_same_value %ebp\n\t")
623 "ret $20" ) /* (*4) */
625 #endif /* __i386__ */