comdlg32: Add IServiceProvider implementation to the Item Dialog.
[wine] / dlls / msvcrt / except.c
1 /*
2  * msvcrt.dll exception handling
3  *
4  * Copyright 2000 Jon Griffiths
5  * Copyright 2005 Juan Lang
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  *
21  * FIXME: Incomplete support for nested exceptions/try block cleanup.
22  */
23
24 #include "config.h"
25 #include "wine/port.h"
26
27 #include <stdarg.h>
28
29 #include "ntstatus.h"
30 #define WIN32_NO_STATUS
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winternl.h"
34 #include "wine/exception.h"
35 #include "msvcrt.h"
36 #include "excpt.h"
37 #include "wincon.h"
38 #include "wine/debug.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(seh);
41
42 static MSVCRT_security_error_handler security_error_handler;
43
44 /* VC++ extensions to Win32 SEH */
45 typedef struct _SCOPETABLE
46 {
47   int previousTryLevel;
48   int (*lpfnFilter)(PEXCEPTION_POINTERS);
49   int (*lpfnHandler)(void);
50 } SCOPETABLE, *PSCOPETABLE;
51
52 typedef struct _MSVCRT_EXCEPTION_FRAME
53 {
54   EXCEPTION_REGISTRATION_RECORD *prev;
55   void (*handler)(PEXCEPTION_RECORD, EXCEPTION_REGISTRATION_RECORD*,
56                   PCONTEXT, PEXCEPTION_RECORD);
57   PSCOPETABLE scopetable;
58   int trylevel;
59   int _ebp;
60   PEXCEPTION_POINTERS xpointers;
61 } MSVCRT_EXCEPTION_FRAME;
62
63 typedef struct
64 {
65   int   gs_cookie_offset;
66   ULONG gs_cookie_xor;
67   int   eh_cookie_offset;
68   ULONG eh_cookie_xor;
69   SCOPETABLE entries[1];
70 } SCOPETABLE_V4;
71
72 #define TRYLEVEL_END (-1) /* End of trylevel list */
73
74 #if defined(__GNUC__) && defined(__i386__)
75 static inline void call_finally_block( void *code_block, void *base_ptr )
76 {
77     __asm__ __volatile__ ("movl %1,%%ebp; call *%%eax"
78                           : : "a" (code_block), "g" (base_ptr));
79 }
80
81 static inline int call_filter( int (*func)(PEXCEPTION_POINTERS), void *arg, void *ebp )
82 {
83     int ret;
84     __asm__ __volatile__ ("pushl %%ebp; pushl %3; movl %2,%%ebp; call *%%eax; popl %%ebp; popl %%ebp"
85                           : "=a" (ret)
86                           : "0" (func), "r" (ebp), "r" (arg)
87                           : "ecx", "edx", "memory" );
88     return ret;
89 }
90
91 static inline int call_unwind_func( int (*func)(void), void *ebp )
92 {
93     int ret;
94     __asm__ __volatile__ ("pushl %%ebp\n\t"
95                           "pushl %%ebx\n\t"
96                           "pushl %%esi\n\t"
97                           "pushl %%edi\n\t"
98                           "movl %2,%%ebp\n\t"
99                           "call *%0\n\t"
100                           "popl %%edi\n\t"
101                           "popl %%esi\n\t"
102                           "popl %%ebx\n\t"
103                           "popl %%ebp"
104                           : "=a" (ret)
105                           : "0" (func), "r" (ebp)
106                           : "ecx", "edx", "memory" );
107     return ret;
108 }
109
110 #endif
111
112
113 #ifdef __i386__
114
115 static const SCOPETABLE_V4 *get_scopetable_v4( MSVCRT_EXCEPTION_FRAME *frame, ULONG_PTR cookie )
116 {
117     return (const SCOPETABLE_V4 *)((ULONG_PTR)frame->scopetable ^ cookie);
118 }
119
120 static DWORD MSVCRT_nested_handler(PEXCEPTION_RECORD rec,
121                                    EXCEPTION_REGISTRATION_RECORD* frame,
122                                    PCONTEXT context,
123                                    EXCEPTION_REGISTRATION_RECORD** dispatch)
124 {
125   if (!(rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
126     return ExceptionContinueSearch;
127   *dispatch = frame;
128   return ExceptionCollidedUnwind;
129 }
130
131
132 /*********************************************************************
133  *              _EH_prolog (MSVCRT.@)
134  */
135
136 /* Provided for VC++ binary compatibility only */
137 __ASM_GLOBAL_FUNC(_EH_prolog,
138                   __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")  /* skip ret addr */
139                   "pushl $-1\n\t"
140                   __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
141                   "pushl %eax\n\t"
142                   __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
143                   "pushl %fs:0\n\t"
144                   __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
145                   "movl  %esp, %fs:0\n\t"
146                   "movl  12(%esp), %eax\n\t"
147                   "movl  %ebp, 12(%esp)\n\t"
148                   "leal  12(%esp), %ebp\n\t"
149                   "pushl %eax\n\t"
150                   __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
151                   "ret")
152
153 static void msvcrt_local_unwind2(MSVCRT_EXCEPTION_FRAME* frame, int trylevel, void *ebp)
154 {
155   EXCEPTION_REGISTRATION_RECORD reg;
156
157   TRACE("(%p,%d,%d)\n",frame, frame->trylevel, trylevel);
158
159   /* Register a handler in case of a nested exception */
160   reg.Handler = MSVCRT_nested_handler;
161   reg.Prev = NtCurrentTeb()->Tib.ExceptionList;
162   __wine_push_frame(&reg);
163
164   while (frame->trylevel != TRYLEVEL_END && frame->trylevel != trylevel)
165   {
166       int level = frame->trylevel;
167       frame->trylevel = frame->scopetable[level].previousTryLevel;
168       if (!frame->scopetable[level].lpfnFilter)
169       {
170           TRACE( "__try block cleanup level %d handler %p ebp %p\n",
171                  level, frame->scopetable[level].lpfnHandler, ebp );
172           call_unwind_func( frame->scopetable[level].lpfnHandler, ebp );
173       }
174   }
175   __wine_pop_frame(&reg);
176   TRACE("unwound OK\n");
177 }
178
179 static void msvcrt_local_unwind4( ULONG *cookie, MSVCRT_EXCEPTION_FRAME* frame, int trylevel, void *ebp )
180 {
181     EXCEPTION_REGISTRATION_RECORD reg;
182     const SCOPETABLE_V4 *scopetable = get_scopetable_v4( frame, *cookie );
183
184     TRACE("(%p,%d,%d)\n",frame, frame->trylevel, trylevel);
185
186     /* Register a handler in case of a nested exception */
187     reg.Handler = MSVCRT_nested_handler;
188     reg.Prev = NtCurrentTeb()->Tib.ExceptionList;
189     __wine_push_frame(&reg);
190
191     while (frame->trylevel != -2 && frame->trylevel != trylevel)
192     {
193         int level = frame->trylevel;
194         frame->trylevel = scopetable->entries[level].previousTryLevel;
195         if (!scopetable->entries[level].lpfnFilter)
196         {
197             TRACE( "__try block cleanup level %d handler %p ebp %p\n",
198                    level, scopetable->entries[level].lpfnHandler, ebp );
199             call_unwind_func( scopetable->entries[level].lpfnHandler, ebp );
200         }
201     }
202     __wine_pop_frame(&reg);
203     TRACE("unwound OK\n");
204 }
205
206 /*******************************************************************
207  *              _local_unwind2 (MSVCRT.@)
208  */
209 void CDECL _local_unwind2(MSVCRT_EXCEPTION_FRAME* frame, int trylevel)
210 {
211     msvcrt_local_unwind2( frame, trylevel, &frame->_ebp );
212 }
213
214 /*******************************************************************
215  *              _local_unwind4 (MSVCRT.@)
216  */
217 void CDECL _local_unwind4( ULONG *cookie, MSVCRT_EXCEPTION_FRAME* frame, int trylevel )
218 {
219     msvcrt_local_unwind4( cookie, frame, trylevel, &frame->_ebp );
220 }
221
222 /*******************************************************************
223  *              _global_unwind2 (MSVCRT.@)
224  */
225 void CDECL _global_unwind2(EXCEPTION_REGISTRATION_RECORD* frame)
226 {
227     TRACE("(%p)\n",frame);
228     RtlUnwind( frame, 0, 0, 0 );
229 }
230
231 /*********************************************************************
232  *              _except_handler2 (MSVCRT.@)
233  */
234 int CDECL _except_handler2(PEXCEPTION_RECORD rec,
235                            EXCEPTION_REGISTRATION_RECORD* frame,
236                            PCONTEXT context,
237                            EXCEPTION_REGISTRATION_RECORD** dispatcher)
238 {
239   FIXME("exception %x flags=%x at %p handler=%p %p %p stub\n",
240         rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
241         frame->Handler, context, dispatcher);
242   return ExceptionContinueSearch;
243 }
244
245 /*********************************************************************
246  *              _except_handler3 (MSVCRT.@)
247  */
248 int CDECL _except_handler3(PEXCEPTION_RECORD rec,
249                            MSVCRT_EXCEPTION_FRAME* frame,
250                            PCONTEXT context, void* dispatcher)
251 {
252   int retval, trylevel;
253   EXCEPTION_POINTERS exceptPtrs;
254   PSCOPETABLE pScopeTable;
255
256   TRACE("exception %x flags=%x at %p handler=%p %p %p semi-stub\n",
257         rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
258         frame->handler, context, dispatcher);
259
260   __asm__ __volatile__ ("cld");
261
262   if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
263   {
264     /* Unwinding the current frame */
265     msvcrt_local_unwind2(frame, TRYLEVEL_END, &frame->_ebp);
266     TRACE("unwound current frame, returning ExceptionContinueSearch\n");
267     return ExceptionContinueSearch;
268   }
269   else
270   {
271     /* Hunting for handler */
272     exceptPtrs.ExceptionRecord = rec;
273     exceptPtrs.ContextRecord = context;
274     *((DWORD *)frame-1) = (DWORD)&exceptPtrs;
275     trylevel = frame->trylevel;
276     pScopeTable = frame->scopetable;
277
278     while (trylevel != TRYLEVEL_END)
279     {
280       TRACE( "level %d prev %d filter %p\n", trylevel, pScopeTable[trylevel].previousTryLevel,
281              pScopeTable[trylevel].lpfnFilter );
282       if (pScopeTable[trylevel].lpfnFilter)
283       {
284         retval = call_filter( pScopeTable[trylevel].lpfnFilter, &exceptPtrs, &frame->_ebp );
285
286         TRACE("filter returned %s\n", retval == EXCEPTION_CONTINUE_EXECUTION ?
287               "CONTINUE_EXECUTION" : retval == EXCEPTION_EXECUTE_HANDLER ?
288               "EXECUTE_HANDLER" : "CONTINUE_SEARCH");
289
290         if (retval == EXCEPTION_CONTINUE_EXECUTION)
291           return ExceptionContinueExecution;
292
293         if (retval == EXCEPTION_EXECUTE_HANDLER)
294         {
295           /* Unwind all higher frames, this one will handle the exception */
296           _global_unwind2((EXCEPTION_REGISTRATION_RECORD*)frame);
297           msvcrt_local_unwind2(frame, trylevel, &frame->_ebp);
298
299           /* Set our trylevel to the enclosing block, and call the __finally
300            * code, which won't return
301            */
302           frame->trylevel = pScopeTable[trylevel].previousTryLevel;
303           TRACE("__finally block %p\n",pScopeTable[trylevel].lpfnHandler);
304           call_finally_block(pScopeTable[trylevel].lpfnHandler, &frame->_ebp);
305           ERR("Returned from __finally block - expect crash!\n");
306        }
307       }
308       trylevel = pScopeTable[trylevel].previousTryLevel;
309     }
310   }
311   TRACE("reached TRYLEVEL_END, returning ExceptionContinueSearch\n");
312   return ExceptionContinueSearch;
313 }
314
315 /*********************************************************************
316  *              _except_handler4_common (MSVCRT.@)
317  */
318 int CDECL _except_handler4_common( ULONG *cookie, void (*check_cookie)(void),
319                                    EXCEPTION_RECORD *rec, MSVCRT_EXCEPTION_FRAME *frame,
320                                    CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
321 {
322     int retval, trylevel;
323     EXCEPTION_POINTERS exceptPtrs;
324     const SCOPETABLE_V4 *scope_table = get_scopetable_v4( frame, *cookie );
325
326     TRACE( "exception %x flags=%x at %p handler=%p %p %p cookie=%x scope table=%p cookies=%d/%x,%d/%x\n",
327            rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
328            frame->handler, context, dispatcher, *cookie, scope_table,
329            scope_table->gs_cookie_offset, scope_table->gs_cookie_xor,
330            scope_table->eh_cookie_offset, scope_table->eh_cookie_xor );
331
332     /* FIXME: no cookie validation yet */
333
334     if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
335     {
336         /* Unwinding the current frame */
337         msvcrt_local_unwind4( cookie, frame, -2, &frame->_ebp );
338         TRACE("unwound current frame, returning ExceptionContinueSearch\n");
339         return ExceptionContinueSearch;
340     }
341     else
342     {
343         /* Hunting for handler */
344         exceptPtrs.ExceptionRecord = rec;
345         exceptPtrs.ContextRecord = context;
346         *((DWORD *)frame-1) = (DWORD)&exceptPtrs;
347         trylevel = frame->trylevel;
348
349         while (trylevel != -2)
350         {
351             TRACE( "level %d prev %d filter %p\n", trylevel,
352                    scope_table->entries[trylevel].previousTryLevel,
353                    scope_table->entries[trylevel].lpfnFilter );
354             if (scope_table->entries[trylevel].lpfnFilter)
355             {
356                 retval = call_filter( scope_table->entries[trylevel].lpfnFilter, &exceptPtrs, &frame->_ebp );
357
358                 TRACE("filter returned %s\n", retval == EXCEPTION_CONTINUE_EXECUTION ?
359                       "CONTINUE_EXECUTION" : retval == EXCEPTION_EXECUTE_HANDLER ?
360                       "EXECUTE_HANDLER" : "CONTINUE_SEARCH");
361
362                 if (retval == EXCEPTION_CONTINUE_EXECUTION)
363                     return ExceptionContinueExecution;
364
365                 if (retval == EXCEPTION_EXECUTE_HANDLER)
366                 {
367                     /* Unwind all higher frames, this one will handle the exception */
368                     _global_unwind2((EXCEPTION_REGISTRATION_RECORD*)frame);
369                     msvcrt_local_unwind4( cookie, frame, trylevel, &frame->_ebp );
370
371                     /* Set our trylevel to the enclosing block, and call the __finally
372                      * code, which won't return
373                      */
374                     frame->trylevel = scope_table->entries[trylevel].previousTryLevel;
375                     TRACE("__finally block %p\n",scope_table->entries[trylevel].lpfnHandler);
376                     call_finally_block(scope_table->entries[trylevel].lpfnHandler, &frame->_ebp);
377                     ERR("Returned from __finally block - expect crash!\n");
378                 }
379             }
380             trylevel = scope_table->entries[trylevel].previousTryLevel;
381         }
382     }
383     TRACE("reached -2, returning ExceptionContinueSearch\n");
384     return ExceptionContinueSearch;
385 }
386
387
388 /*
389  * setjmp/longjmp implementation
390  */
391
392 #define MSVCRT_JMP_MAGIC 0x56433230 /* ID value for new jump structure */
393 typedef void (__stdcall *MSVCRT_unwind_function)(const struct MSVCRT___JUMP_BUFFER *);
394
395 /* define an entrypoint for setjmp/setjmp3 that stores the registers in the jmp buf */
396 /* and then jumps to the C backend function */
397 #define DEFINE_SETJMP_ENTRYPOINT(name) \
398     __ASM_GLOBAL_FUNC( name, \
399                        "movl 4(%esp),%ecx\n\t"   /* jmp_buf */      \
400                        "movl %ebp,0(%ecx)\n\t"   /* jmp_buf.Ebp */  \
401                        "movl %ebx,4(%ecx)\n\t"   /* jmp_buf.Ebx */  \
402                        "movl %edi,8(%ecx)\n\t"   /* jmp_buf.Edi */  \
403                        "movl %esi,12(%ecx)\n\t"  /* jmp_buf.Esi */  \
404                        "movl %esp,16(%ecx)\n\t"  /* jmp_buf.Esp */  \
405                        "movl 0(%esp),%eax\n\t"                      \
406                        "movl %eax,20(%ecx)\n\t"  /* jmp_buf.Eip */  \
407                        "jmp " __ASM_NAME("__regs_") # name )
408
409 /* restore the registers from the jmp buf upon longjmp */
410 extern void DECLSPEC_NORETURN longjmp_set_regs( struct MSVCRT___JUMP_BUFFER *jmp, int retval );
411 __ASM_GLOBAL_FUNC( longjmp_set_regs,
412                    "movl 4(%esp),%ecx\n\t"   /* jmp_buf */
413                    "movl 8(%esp),%eax\n\t"   /* retval */
414                    "movl 0(%ecx),%ebp\n\t"   /* jmp_buf.Ebp */
415                    "movl 4(%ecx),%ebx\n\t"   /* jmp_buf.Ebx */
416                    "movl 8(%ecx),%edi\n\t"   /* jmp_buf.Edi */
417                    "movl 12(%ecx),%esi\n\t"  /* jmp_buf.Esi */
418                    "movl 16(%ecx),%esp\n\t"  /* jmp_buf.Esp */
419                    "addl $4,%esp\n\t"        /* get rid of return address */
420                    "jmp *20(%ecx)\n\t"       /* jmp_buf.Eip */ )
421
422 /*
423  * The signatures of the setjmp/longjmp functions do not match that
424  * declared in the setjmp header so they don't follow the regular naming
425  * convention to avoid conflicts.
426  */
427
428 /*******************************************************************
429  *              _setjmp (MSVCRT.@)
430  */
431 DEFINE_SETJMP_ENTRYPOINT(MSVCRT__setjmp)
432 int CDECL __regs_MSVCRT__setjmp(struct MSVCRT___JUMP_BUFFER *jmp)
433 {
434     jmp->Registration = (unsigned long)NtCurrentTeb()->Tib.ExceptionList;
435     if (jmp->Registration == ~0UL)
436         jmp->TryLevel = TRYLEVEL_END;
437     else
438         jmp->TryLevel = ((MSVCRT_EXCEPTION_FRAME*)jmp->Registration)->trylevel;
439
440     TRACE("buf=%p ebx=%08lx esi=%08lx edi=%08lx ebp=%08lx esp=%08lx eip=%08lx frame=%08lx\n",
441           jmp, jmp->Ebx, jmp->Esi, jmp->Edi, jmp->Ebp, jmp->Esp, jmp->Eip, jmp->Registration );
442     return 0;
443 }
444
445 /*******************************************************************
446  *              _setjmp3 (MSVCRT.@)
447  */
448 DEFINE_SETJMP_ENTRYPOINT( MSVCRT__setjmp3 )
449 int CDECL __regs_MSVCRT__setjmp3(struct MSVCRT___JUMP_BUFFER *jmp, int nb_args, ...)
450 {
451     jmp->Cookie = MSVCRT_JMP_MAGIC;
452     jmp->UnwindFunc = 0;
453     jmp->Registration = (unsigned long)NtCurrentTeb()->Tib.ExceptionList;
454     if (jmp->Registration == ~0UL)
455     {
456         jmp->TryLevel = TRYLEVEL_END;
457     }
458     else
459     {
460         int i;
461         va_list args;
462
463         va_start( args, nb_args );
464         if (nb_args > 0) jmp->UnwindFunc = va_arg( args, unsigned long );
465         if (nb_args > 1) jmp->TryLevel = va_arg( args, unsigned long );
466         else jmp->TryLevel = ((MSVCRT_EXCEPTION_FRAME*)jmp->Registration)->trylevel;
467         for (i = 0; i < 6 && i < nb_args - 2; i++)
468             jmp->UnwindData[i] = va_arg( args, unsigned long );
469         va_end( args );
470     }
471
472     TRACE("buf=%p ebx=%08lx esi=%08lx edi=%08lx ebp=%08lx esp=%08lx eip=%08lx frame=%08lx\n",
473           jmp, jmp->Ebx, jmp->Esi, jmp->Edi, jmp->Ebp, jmp->Esp, jmp->Eip, jmp->Registration );
474     return 0;
475 }
476
477 /*********************************************************************
478  *              longjmp (MSVCRT.@)
479  */
480 void CDECL MSVCRT_longjmp(struct MSVCRT___JUMP_BUFFER *jmp, int retval)
481 {
482     unsigned long cur_frame = 0;
483
484     TRACE("buf=%p ebx=%08lx esi=%08lx edi=%08lx ebp=%08lx esp=%08lx eip=%08lx frame=%08lx retval=%08x\n",
485           jmp, jmp->Ebx, jmp->Esi, jmp->Edi, jmp->Ebp, jmp->Esp, jmp->Eip, jmp->Registration, retval );
486
487     cur_frame=(unsigned long)NtCurrentTeb()->Tib.ExceptionList;
488     TRACE("cur_frame=%lx\n",cur_frame);
489
490     if (cur_frame != jmp->Registration)
491         _global_unwind2((EXCEPTION_REGISTRATION_RECORD*)jmp->Registration);
492
493     if (jmp->Registration)
494     {
495         if (!IsBadReadPtr(&jmp->Cookie, sizeof(long)) &&
496             jmp->Cookie == MSVCRT_JMP_MAGIC && jmp->UnwindFunc)
497         {
498             MSVCRT_unwind_function unwind_func;
499
500             unwind_func=(MSVCRT_unwind_function)jmp->UnwindFunc;
501             unwind_func(jmp);
502         }
503         else
504             msvcrt_local_unwind2((MSVCRT_EXCEPTION_FRAME*)jmp->Registration,
505                                  jmp->TryLevel, (void *)jmp->Ebp);
506     }
507
508     if (!retval)
509         retval = 1;
510
511     longjmp_set_regs( jmp, retval );
512 }
513
514 /*********************************************************************
515  *              _seh_longjmp_unwind (MSVCRT.@)
516  */
517 void __stdcall _seh_longjmp_unwind(struct MSVCRT___JUMP_BUFFER *jmp)
518 {
519     msvcrt_local_unwind2( (MSVCRT_EXCEPTION_FRAME *)jmp->Registration, jmp->TryLevel, (void *)jmp->Ebp );
520 }
521
522 #elif defined(__x86_64__)
523
524 /*******************************************************************
525  *              _setjmp (MSVCRT.@)
526  */
527 __ASM_GLOBAL_FUNC( MSVCRT__setjmp,
528                    "xorq %rdx,%rdx\n\t"  /* frame */
529                    "jmp " __ASM_NAME("MSVCRT__setjmpex") );
530
531 /*******************************************************************
532  *              _setjmpex (MSVCRT.@)
533  */
534 __ASM_GLOBAL_FUNC( MSVCRT__setjmpex,
535                    "movq %rdx,(%rcx)\n\t"          /* jmp_buf->Frame */
536                    "movq %rbx,0x8(%rcx)\n\t"       /* jmp_buf->Rbx */
537                    "leaq 0x8(%rsp),%rax\n\t"
538                    "movq %rax,0x10(%rcx)\n\t"      /* jmp_buf->Rsp */
539                    "movq %rbp,0x18(%rcx)\n\t"      /* jmp_buf->Rbp */
540                    "movq %rsi,0x20(%rcx)\n\t"      /* jmp_buf->Rsi */
541                    "movq %rdi,0x28(%rcx)\n\t"      /* jmp_buf->Rdi */
542                    "movq %r12,0x30(%rcx)\n\t"      /* jmp_buf->R12 */
543                    "movq %r13,0x38(%rcx)\n\t"      /* jmp_buf->R13 */
544                    "movq %r14,0x40(%rcx)\n\t"      /* jmp_buf->R14 */
545                    "movq %r15,0x48(%rcx)\n\t"      /* jmp_buf->R15 */
546                    "movq (%rsp),%rax\n\t"
547                    "movq %rax,0x50(%rcx)\n\t"      /* jmp_buf->Rip */
548                    "movdqa %xmm6,0x60(%rcx)\n\t"   /* jmp_buf->Xmm6 */
549                    "movdqa %xmm7,0x70(%rcx)\n\t"   /* jmp_buf->Xmm7 */
550                    "movdqa %xmm8,0x80(%rcx)\n\t"   /* jmp_buf->Xmm8 */
551                    "movdqa %xmm9,0x90(%rcx)\n\t"   /* jmp_buf->Xmm9 */
552                    "movdqa %xmm10,0xa0(%rcx)\n\t"  /* jmp_buf->Xmm10 */
553                    "movdqa %xmm11,0xb0(%rcx)\n\t"  /* jmp_buf->Xmm11 */
554                    "movdqa %xmm12,0xc0(%rcx)\n\t"  /* jmp_buf->Xmm12 */
555                    "movdqa %xmm13,0xd0(%rcx)\n\t"  /* jmp_buf->Xmm13 */
556                    "movdqa %xmm14,0xe0(%rcx)\n\t"  /* jmp_buf->Xmm14 */
557                    "movdqa %xmm15,0xf0(%rcx)\n\t"  /* jmp_buf->Xmm15 */
558                    "xorq %rax,%rax\n\t"
559                    "retq" );
560
561
562 extern void DECLSPEC_NORETURN CDECL longjmp_set_regs( struct MSVCRT___JUMP_BUFFER *jmp, int retval );
563 __ASM_GLOBAL_FUNC( longjmp_set_regs,
564                    "movq %rdx,%rax\n\t"            /* retval */
565                    "movq 0x8(%rcx),%rbx\n\t"       /* jmp_buf->Rbx */
566                    "movq 0x18(%rcx),%rbp\n\t"      /* jmp_buf->Rbp */
567                    "movq 0x20(%rcx),%rsi\n\t"      /* jmp_buf->Rsi */
568                    "movq 0x28(%rcx),%rdi\n\t"      /* jmp_buf->Rdi */
569                    "movq 0x30(%rcx),%r12\n\t"      /* jmp_buf->R12 */
570                    "movq 0x38(%rcx),%r13\n\t"      /* jmp_buf->R13 */
571                    "movq 0x40(%rcx),%r14\n\t"      /* jmp_buf->R14 */
572                    "movq 0x48(%rcx),%r15\n\t"      /* jmp_buf->R15 */
573                    "movdqa 0x60(%rcx),%xmm6\n\t"   /* jmp_buf->Xmm6 */
574                    "movdqa 0x70(%rcx),%xmm7\n\t"   /* jmp_buf->Xmm7 */
575                    "movdqa 0x80(%rcx),%xmm8\n\t"   /* jmp_buf->Xmm8 */
576                    "movdqa 0x90(%rcx),%xmm9\n\t"   /* jmp_buf->Xmm9 */
577                    "movdqa 0xa0(%rcx),%xmm10\n\t"  /* jmp_buf->Xmm10 */
578                    "movdqa 0xb0(%rcx),%xmm11\n\t"  /* jmp_buf->Xmm11 */
579                    "movdqa 0xc0(%rcx),%xmm12\n\t"  /* jmp_buf->Xmm12 */
580                    "movdqa 0xd0(%rcx),%xmm13\n\t"  /* jmp_buf->Xmm13 */
581                    "movdqa 0xe0(%rcx),%xmm14\n\t"  /* jmp_buf->Xmm14 */
582                    "movdqa 0xf0(%rcx),%xmm15\n\t"  /* jmp_buf->Xmm15 */
583                    "movq 0x50(%rcx),%rdx\n\t"      /* jmp_buf->Rip */
584                    "movq 0x10(%rcx),%rsp\n\t"      /* jmp_buf->Rsp */
585                    "jmp *%rdx" );
586
587 /*******************************************************************
588  *              longjmp (MSVCRT.@)
589  */
590 void __cdecl MSVCRT_longjmp( struct MSVCRT___JUMP_BUFFER *jmp, int retval )
591 {
592     EXCEPTION_RECORD rec;
593
594     if (!retval) retval = 1;
595     if (jmp->Frame)
596     {
597         rec.ExceptionCode = STATUS_LONGJUMP;
598         rec.ExceptionFlags = 0;
599         rec.ExceptionRecord = NULL;
600         rec.ExceptionAddress = NULL;
601         rec.NumberParameters = 1;
602         rec.ExceptionInformation[0] = (DWORD_PTR)jmp;
603         RtlUnwind( (void *)jmp->Frame, (void *)jmp->Rip, &rec, IntToPtr(retval) );
604     }
605     longjmp_set_regs( jmp, retval );
606 }
607
608 #endif /* __x86_64__ */
609
610 static MSVCRT___sighandler_t sighandlers[MSVCRT_NSIG] = { MSVCRT_SIG_DFL };
611
612 static BOOL WINAPI msvcrt_console_handler(DWORD ctrlType)
613 {
614     BOOL ret = FALSE;
615
616     switch (ctrlType)
617     {
618     case CTRL_C_EVENT:
619         if (sighandlers[MSVCRT_SIGINT])
620         {
621             if (sighandlers[MSVCRT_SIGINT] != MSVCRT_SIG_IGN)
622                 sighandlers[MSVCRT_SIGINT](MSVCRT_SIGINT);
623             ret = TRUE;
624         }
625         break;
626     }
627     return ret;
628 }
629
630 typedef void (CDECL *float_handler)(int, int);
631
632 /* The exception codes are actually NTSTATUS values */
633 static const struct
634 {
635     NTSTATUS status;
636     int signal;
637 } float_exception_map[] = {
638  { EXCEPTION_FLT_DENORMAL_OPERAND, MSVCRT__FPE_DENORMAL },
639  { EXCEPTION_FLT_DIVIDE_BY_ZERO, MSVCRT__FPE_ZERODIVIDE },
640  { EXCEPTION_FLT_INEXACT_RESULT, MSVCRT__FPE_INEXACT },
641  { EXCEPTION_FLT_INVALID_OPERATION, MSVCRT__FPE_INVALID },
642  { EXCEPTION_FLT_OVERFLOW, MSVCRT__FPE_OVERFLOW },
643  { EXCEPTION_FLT_STACK_CHECK, MSVCRT__FPE_STACKOVERFLOW },
644  { EXCEPTION_FLT_UNDERFLOW, MSVCRT__FPE_UNDERFLOW },
645 };
646
647 static LONG WINAPI msvcrt_exception_filter(struct _EXCEPTION_POINTERS *except)
648 {
649     LONG ret = EXCEPTION_CONTINUE_SEARCH;
650     MSVCRT___sighandler_t handler;
651
652     if (!except || !except->ExceptionRecord)
653         return EXCEPTION_CONTINUE_SEARCH;
654
655     switch (except->ExceptionRecord->ExceptionCode)
656     {
657     case EXCEPTION_ACCESS_VIOLATION:
658         if ((handler = sighandlers[MSVCRT_SIGSEGV]) != MSVCRT_SIG_DFL)
659         {
660             if (handler != MSVCRT_SIG_IGN)
661             {
662                 sighandlers[MSVCRT_SIGSEGV] = MSVCRT_SIG_DFL;
663                 handler(MSVCRT_SIGSEGV);
664             }
665             ret = EXCEPTION_CONTINUE_EXECUTION;
666         }
667         break;
668     /* According to msdn,
669      * the FPE signal handler takes as a second argument the type of
670      * floating point exception.
671      */
672     case EXCEPTION_FLT_DENORMAL_OPERAND:
673     case EXCEPTION_FLT_DIVIDE_BY_ZERO:
674     case EXCEPTION_FLT_INEXACT_RESULT:
675     case EXCEPTION_FLT_INVALID_OPERATION:
676     case EXCEPTION_FLT_OVERFLOW:
677     case EXCEPTION_FLT_STACK_CHECK:
678     case EXCEPTION_FLT_UNDERFLOW:
679         if ((handler = sighandlers[MSVCRT_SIGFPE]) != MSVCRT_SIG_DFL)
680         {
681             if (handler != MSVCRT_SIG_IGN)
682             {
683                 unsigned int i;
684                 int float_signal = MSVCRT__FPE_INVALID;
685
686                 sighandlers[MSVCRT_SIGFPE] = MSVCRT_SIG_DFL;
687                 for (i = 0; i < sizeof(float_exception_map) /
688                          sizeof(float_exception_map[0]); i++)
689                 {
690                     if (float_exception_map[i].status ==
691                         except->ExceptionRecord->ExceptionCode)
692                     {
693                         float_signal = float_exception_map[i].signal;
694                         break;
695                     }
696                 }
697                 ((float_handler)handler)(MSVCRT_SIGFPE, float_signal);
698             }
699             ret = EXCEPTION_CONTINUE_EXECUTION;
700         }
701         break;
702     case EXCEPTION_ILLEGAL_INSTRUCTION:
703     case EXCEPTION_PRIV_INSTRUCTION:
704         if ((handler = sighandlers[MSVCRT_SIGILL]) != MSVCRT_SIG_DFL)
705         {
706             if (handler != MSVCRT_SIG_IGN)
707             {
708                 sighandlers[MSVCRT_SIGILL] = MSVCRT_SIG_DFL;
709                 handler(MSVCRT_SIGILL);
710             }
711             ret = EXCEPTION_CONTINUE_EXECUTION;
712         }
713         break;
714     }
715     return ret;
716 }
717
718 void msvcrt_init_signals(void)
719 {
720     SetConsoleCtrlHandler(msvcrt_console_handler, TRUE);
721     SetUnhandledExceptionFilter(msvcrt_exception_filter);
722 }
723
724 void msvcrt_free_signals(void)
725 {
726     SetConsoleCtrlHandler(msvcrt_console_handler, FALSE);
727     SetUnhandledExceptionFilter(NULL);
728 }
729
730 /*********************************************************************
731  *              signal (MSVCRT.@)
732  * Some signals may never be generated except through an explicit call to
733  * raise.
734  */
735 MSVCRT___sighandler_t CDECL MSVCRT_signal(int sig, MSVCRT___sighandler_t func)
736 {
737     MSVCRT___sighandler_t ret = MSVCRT_SIG_ERR;
738
739     TRACE("(%d, %p)\n", sig, func);
740
741     if (func == MSVCRT_SIG_ERR) return MSVCRT_SIG_ERR;
742
743     switch (sig)
744     {
745     /* Cases handled internally.  Note SIGTERM is never generated by Windows,
746      * so we effectively mask it.
747      */
748     case MSVCRT_SIGABRT:
749     case MSVCRT_SIGFPE:
750     case MSVCRT_SIGILL:
751     case MSVCRT_SIGSEGV:
752     case MSVCRT_SIGINT:
753     case MSVCRT_SIGTERM:
754     case MSVCRT_SIGBREAK:
755         ret = sighandlers[sig];
756         sighandlers[sig] = func;
757         break;
758     default:
759         ret = MSVCRT_SIG_ERR;
760     }
761     return ret;
762 }
763
764 /*********************************************************************
765  *              raise (MSVCRT.@)
766  */
767 int CDECL MSVCRT_raise(int sig)
768 {
769     MSVCRT___sighandler_t handler;
770
771     TRACE("(%d)\n", sig);
772
773     switch (sig)
774     {
775     case MSVCRT_SIGABRT:
776     case MSVCRT_SIGFPE:
777     case MSVCRT_SIGILL:
778     case MSVCRT_SIGSEGV:
779     case MSVCRT_SIGINT:
780     case MSVCRT_SIGTERM:
781     case MSVCRT_SIGBREAK:
782         handler = sighandlers[sig];
783         if (handler == MSVCRT_SIG_DFL) MSVCRT__exit(3);
784         if (handler != MSVCRT_SIG_IGN)
785         {
786             sighandlers[sig] = MSVCRT_SIG_DFL;
787             if (sig == MSVCRT_SIGFPE)
788                 ((float_handler)handler)(sig, MSVCRT__FPE_EXPLICITGEN);
789             else
790                 handler(sig);
791         }
792         break;
793     default:
794         return -1;
795     }
796     return 0;
797 }
798
799 /*********************************************************************
800  *              _XcptFilter (MSVCRT.@)
801  */
802 int CDECL _XcptFilter(NTSTATUS ex, PEXCEPTION_POINTERS ptr)
803 {
804     TRACE("(%08x,%p)\n", ex, ptr);
805     /* I assume ptr->ExceptionRecord->ExceptionCode is the same as ex */
806     return msvcrt_exception_filter(ptr);
807 }
808
809 /*********************************************************************
810  *              _abnormal_termination (MSVCRT.@)
811  */
812 int CDECL _abnormal_termination(void)
813 {
814   FIXME("(void)stub\n");
815   return 0;
816 }
817
818 /******************************************************************
819  *              MSVCRT___uncaught_exception
820  */
821 BOOL CDECL MSVCRT___uncaught_exception(void)
822 {
823     return FALSE;
824 }
825
826 /* _set_security_error_handler - not exported in native msvcrt, added in msvcr70 */
827 MSVCRT_security_error_handler CDECL _set_security_error_handler(
828     MSVCRT_security_error_handler handler )
829 {
830     MSVCRT_security_error_handler old = security_error_handler;
831
832     TRACE("(%p)\n", handler);
833
834     security_error_handler = handler;
835     return old;
836 }