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] );
197 TRACE(" eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x\n",
198 context
->Eax
, context
->Ebx
, context
->Ecx
,
199 context
->Edx
, context
->Esi
, context
->Edi
);
200 TRACE(" ebp=%08x esp=%08x cs=%04x ds=%04x es=%04x fs=%04x gs=%04x flags=%08x\n",
201 context
->Ebp
, context
->Esp
, context
->SegCs
, context
->SegDs
,
202 context
->SegEs
, context
->SegFs
, context
->SegGs
, context
->EFlags
);
205 if (call_vectored_handlers( rec
, context
) == EXCEPTION_CONTINUE_EXECUTION
)
206 NtContinue( context
, FALSE
);
208 if ((status
= call_stack_handlers( rec
, context
)) == STATUS_SUCCESS
)
209 NtContinue( context
, FALSE
);
211 if (status
!= STATUS_UNHANDLED_EXCEPTION
) RtlRaiseStatus( status
);
212 return NtRaiseException( rec
, context
, FALSE
);
215 __ASM_STDCALL_FUNC( KiUserExceptionDispatcher
, 8,
218 "call " __ASM_STDCALL("dispatch_exception", 8) "\n\t"
221 /***********************************************************************
224 * Save the thread FPU context.
226 static inline void save_fpu( CONTEXT
*context
)
241 context
->ContextFlags
|= CONTEXT_FLOATING_POINT
;
242 __asm__
__volatile__( "fnsave %0; fwait" : "=m" (context
->FloatSave
) );
244 /* Reset unmasked exceptions status to avoid firing an exception. */
245 memcpy(&float_status
, &context
->FloatSave
, sizeof(float_status
));
246 float_status
.StatusWord
&= float_status
.ControlWord
| 0xffffff80;
248 __asm__
__volatile__( "fldenv %0" : : "m" (float_status
) );
253 /***********************************************************************
256 * Save the thread FPU extended context.
258 static inline void save_fpux( CONTEXT
*context
)
261 /* we have to enforce alignment by hand */
262 char buffer
[sizeof(XSAVE_FORMAT
) + 16];
263 XSAVE_FORMAT
*state
= (XSAVE_FORMAT
*)(((ULONG_PTR
)buffer
+ 15) & ~15);
265 context
->ContextFlags
|= CONTEXT_EXTENDED_REGISTERS
;
266 __asm__
__volatile__( "fxsave %0" : "=m" (*state
) );
267 memcpy( context
->ExtendedRegisters
, state
, sizeof(*state
) );
272 /***********************************************************************
273 * RtlCaptureContext (NTDLL.@)
275 __ASM_STDCALL_FUNC( RtlCaptureContext
, 4,
277 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
278 "movl 8(%esp),%eax\n\t" /* context */
279 "movl $0x10007,(%eax)\n\t" /* context->ContextFlags */
280 "movw %gs,0x8c(%eax)\n\t" /* context->SegGs */
281 "movw %fs,0x90(%eax)\n\t" /* context->SegFs */
282 "movw %es,0x94(%eax)\n\t" /* context->SegEs */
283 "movw %ds,0x98(%eax)\n\t" /* context->SegDs */
284 "movl %edi,0x9c(%eax)\n\t" /* context->Edi */
285 "movl %esi,0xa0(%eax)\n\t" /* context->Esi */
286 "movl %ebx,0xa4(%eax)\n\t" /* context->Ebx */
287 "movl %edx,0xa8(%eax)\n\t" /* context->Edx */
288 "movl %ecx,0xac(%eax)\n\t" /* context->Ecx */
289 "movl 0(%ebp),%edx\n\t"
290 "movl %edx,0xb4(%eax)\n\t" /* context->Ebp */
291 "movl 4(%ebp),%edx\n\t"
292 "movl %edx,0xb8(%eax)\n\t" /* context->Eip */
293 "movw %cs,0xbc(%eax)\n\t" /* context->SegCs */
295 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
296 "popl 0xc0(%eax)\n\t" /* context->EFlags */
297 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
298 "leal 8(%ebp),%edx\n\t"
299 "movl %edx,0xc4(%eax)\n\t" /* context->Esp */
300 "movw %ss,0xc8(%eax)\n\t" /* context->SegSs */
301 "popl 0xb0(%eax)\n\t" /* context->Eax */
302 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
306 /*******************************************************************
307 * RtlUnwind (NTDLL.@)
309 void WINAPI DECLSPEC_HIDDEN
__regs_RtlUnwind( EXCEPTION_REGISTRATION_RECORD
* pEndFrame
, PVOID targetIp
,
310 PEXCEPTION_RECORD pRecord
, PVOID retval
, CONTEXT
*context
)
312 EXCEPTION_RECORD record
;
313 EXCEPTION_REGISTRATION_RECORD
*frame
, *dispatch
;
316 context
->Eax
= (DWORD
)retval
;
318 /* build an exception record, if we do not have one */
321 record
.ExceptionCode
= STATUS_UNWIND
;
322 record
.ExceptionFlags
= 0;
323 record
.ExceptionRecord
= NULL
;
324 record
.ExceptionAddress
= (void *)context
->Eip
;
325 record
.NumberParameters
= 0;
329 pRecord
->ExceptionFlags
|= EH_UNWINDING
| (pEndFrame
? 0 : EH_EXIT_UNWIND
);
331 TRACE( "code=%x flags=%x\n", pRecord
->ExceptionCode
, pRecord
->ExceptionFlags
);
332 TRACE( "eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x\n",
333 context
->Eax
, context
->Ebx
, context
->Ecx
, context
->Edx
, context
->Esi
, context
->Edi
);
334 TRACE( "ebp=%08x esp=%08x eip=%08x cs=%04x ds=%04x fs=%04x gs=%04x flags=%08x\n",
335 context
->Ebp
, context
->Esp
, context
->Eip
, LOWORD(context
->SegCs
), LOWORD(context
->SegDs
),
336 LOWORD(context
->SegFs
), LOWORD(context
->SegGs
), context
->EFlags
);
338 /* get chain of exception frames */
339 frame
= NtCurrentTeb()->Tib
.ExceptionList
;
340 while ((frame
!= (EXCEPTION_REGISTRATION_RECORD
*)~0UL) && (frame
!= pEndFrame
))
342 /* Check frame address */
343 if (pEndFrame
&& (frame
> pEndFrame
))
344 raise_status( STATUS_INVALID_UNWIND_TARGET
, pRecord
);
346 if (!is_valid_frame( frame
)) raise_status( STATUS_BAD_STACK
, pRecord
);
349 TRACE( "calling handler at %p code=%x flags=%x\n",
350 frame
->Handler
, pRecord
->ExceptionCode
, pRecord
->ExceptionFlags
);
351 res
= EXC_CallHandler( pRecord
, frame
, context
, &dispatch
, frame
->Handler
, unwind_handler
);
352 TRACE( "handler at %p returned %x\n", frame
->Handler
, res
);
356 case ExceptionContinueSearch
:
358 case ExceptionCollidedUnwind
:
362 raise_status( STATUS_INVALID_DISPOSITION
, pRecord
);
365 frame
= __wine_pop_frame( frame
);
367 NtContinue( context
, FALSE
);
369 __ASM_STDCALL_FUNC( RtlUnwind
, 16,
371 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
372 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
374 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
375 "leal -(0x2cc+8)(%esp),%esp\n\t" /* sizeof(CONTEXT) + alignment */
377 "leal 4(%esp),%eax\n\t" /* context */
378 "xchgl %eax,(%esp)\n\t"
379 "call " __ASM_STDCALL("RtlCaptureContext",4) "\n\t"
380 "leal 24(%ebp),%eax\n\t"
381 "movl %eax,0xc4(%esp)\n\t" /* context->Esp */
387 "call " __ASM_STDCALL("__regs_RtlUnwind",20) "\n\t"
389 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
390 __ASM_CFI(".cfi_same_value %ebp\n\t")
391 "ret $16" ) /* actually never returns */
394 /*******************************************************************
395 * raise_exception_full_context
397 * Raise an exception with the full CPU context.
399 void raise_exception_full_context( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
402 save_fpux( context
);
404 context
->Dr0
= x86_thread_data()->dr0
;
405 context
->Dr1
= x86_thread_data()->dr1
;
406 context
->Dr2
= x86_thread_data()->dr2
;
407 context
->Dr3
= x86_thread_data()->dr3
;
408 context
->Dr6
= x86_thread_data()->dr6
;
409 context
->Dr7
= x86_thread_data()->dr7
;
410 context
->ContextFlags
|= CONTEXT_DEBUG_REGISTERS
;
412 RtlRaiseStatus( NtRaiseException( rec
, context
, TRUE
));
416 /***********************************************************************
417 * RtlRaiseException (NTDLL.@)
419 __ASM_STDCALL_FUNC( RtlRaiseException
, 4,
421 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
422 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
424 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
425 "leal -0x2cc(%esp),%esp\n\t" /* sizeof(CONTEXT) */
426 "pushl %esp\n\t" /* context */
427 "call " __ASM_STDCALL("RtlCaptureContext",4) "\n\t"
428 "movl 4(%ebp),%eax\n\t" /* return address */
429 "movl 8(%ebp),%ecx\n\t" /* rec */
430 "movl %eax,12(%ecx)\n\t" /* rec->ExceptionAddress */
431 "leal 12(%ebp),%eax\n\t"
432 "movl %eax,0xc4(%esp)\n\t" /* context->Esp */
436 "call " __ASM_NAME("raise_exception_full_context") "\n\t"
438 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
439 __ASM_CFI(".cfi_same_value %ebp\n\t")
440 "ret $4" ) /* actually never returns */
443 /*************************************************************************
444 * RtlCaptureStackBackTrace (NTDLL.@)
446 USHORT WINAPI
RtlCaptureStackBackTrace( ULONG skip
, ULONG count
, PVOID
*buffer
, ULONG
*hash
)
452 RtlCaptureContext( &context
);
454 frame
= (ULONG
*)context
.Ebp
;
458 if (!is_valid_frame( frame
)) return 0;
459 frame
= (ULONG
*)*frame
;
462 for (i
= 0; i
< count
; i
++)
464 if (!is_valid_frame( frame
)) break;
465 buffer
[i
] = (void *)frame
[1];
466 if (hash
) *hash
+= frame
[1];
467 frame
= (ULONG
*)*frame
;
473 /***********************************************************************
474 * signal_start_thread
476 __ASM_GLOBAL_FUNC( signal_start_thread
,
477 "movl 4(%esp),%esi\n\t" /* context */
478 "leal -12(%esi),%ecx\n\t"
479 /* clear the thread stack */
480 "andl $~0xfff,%ecx\n\t" /* round down to page size */
481 "movl %fs:8,%edi\n\t" /* NtCurrentTeb()->Tib.StackLimit */
482 "addl $0x1000,%edi\n\t"
488 /* switch to the initial context */
489 "leal -12(%esi),%esp\n\t"
490 "movl $1,4(%esp)\n\t"
491 "movl %esi,(%esp)\n\t"
492 "call " __ASM_STDCALL("NtContinue", 8) )
494 /**********************************************************************
495 * DbgBreakPoint (NTDLL.@)
497 __ASM_STDCALL_FUNC( DbgBreakPoint
, 0, "int $3; ret"
498 "\n\tnop; nop; nop; nop; nop; nop; nop; nop"
499 "\n\tnop; nop; nop; nop; nop; nop" );
501 /**********************************************************************
502 * DbgUserBreakPoint (NTDLL.@)
504 __ASM_STDCALL_FUNC( DbgUserBreakPoint
, 0, "int $3; ret"
505 "\n\tnop; nop; nop; nop; nop; nop; nop; nop"
506 "\n\tnop; nop; nop; nop; nop; nop" );
508 /**********************************************************************
509 * NtCurrentTeb (NTDLL.@)
511 __ASM_STDCALL_FUNC( NtCurrentTeb
, 0, ".byte 0x64\n\tmovl 0x18,%eax\n\tret" )
514 /**************************************************************************
517 __ASM_GLOBAL_FUNC( _chkstk
,
520 "xchgl %esp,%eax\n\t"
521 "movl 0(%eax),%eax\n\t" /* copy return address from old location */
522 "movl %eax,0(%esp)\n\t"
525 /**************************************************************************
526 * _alloca_probe (NTDLL.@)
528 __ASM_GLOBAL_FUNC( _alloca_probe
,
531 "xchgl %esp,%eax\n\t"
532 "movl 0(%eax),%eax\n\t" /* copy return address from old location */
533 "movl %eax,0(%esp)\n\t"
537 /**********************************************************************
538 * EXC_CallHandler (internal)
540 * Some exception handlers depend on EBP to have a fixed position relative to
541 * the exception frame.
542 * Shrinker depends on (*1) doing what it does,
543 * (*2) being the exact instruction it is and (*3) beginning with 0x64
544 * (i.e. the %fs prefix to the movl instruction). It also depends on the
545 * function calling the handler having only 5 parameters (*4).
547 __ASM_GLOBAL_FUNC( EXC_CallHandler
,
549 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
550 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
552 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
554 __ASM_CFI(".cfi_rel_offset %ebx,-4\n\t")
555 "movl 28(%ebp), %edx\n\t" /* ugly hack to pass the 6th param needed because of Shrinker */
561 "call " __ASM_NAME("call_exception_handler") "\n\t"
563 __ASM_CFI(".cfi_same_value %ebx\n\t")
565 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
566 __ASM_CFI(".cfi_same_value %ebp\n\t")
568 __ASM_GLOBAL_FUNC(call_exception_handler
,
570 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
571 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
573 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
575 "pushl 12(%ebp)\n\t" /* make any exceptions in this... */
576 "pushl %edx\n\t" /* handler be handled by... */
578 "pushl (0)\n\t" /* nested_handler (passed in edx). */
580 "movl %esp,(0)\n\t" /* push the new exception frame onto the exception stack. */
585 "movl 24(%ebp), %ecx\n\t" /* (*1) */
586 "call *%ecx\n\t" /* call handler. (*2) */
588 "movl (0), %esp\n\t" /* restore previous... (*3) */
590 "popl (0)\n\t" /* exception frame. */
591 "movl %ebp, %esp\n\t" /* restore saved stack, in case it was corrupted */
593 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
594 __ASM_CFI(".cfi_same_value %ebp\n\t")
595 "ret $20" ) /* (*4) */
597 #endif /* __i386__ */