2 * Support functions for Wine exception handling
4 * Copyright (c) 1999, 2010 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
23 #include "wine/exception.h"
26 #if defined(__GNUC__) || defined(__clang__)
30 __ASM_GLOBAL_FUNC( __wine_setjmpex
,
31 "movl 4(%esp),%ecx\n\t" /* jmp_buf */
32 "movl %ebp,0(%ecx)\n\t" /* jmp_buf.Ebp */
33 "movl %ebx,4(%ecx)\n\t" /* jmp_buf.Ebx */
34 "movl %edi,8(%ecx)\n\t" /* jmp_buf.Edi */
35 "movl %esi,12(%ecx)\n\t" /* jmp_buf.Esi */
36 "movl %esp,16(%ecx)\n\t" /* jmp_buf.Esp */
37 "movl 0(%esp),%eax\n\t"
38 "movl %eax,20(%ecx)\n\t" /* jmp_buf.Eip */
42 __ASM_GLOBAL_FUNC( __wine_longjmp
,
43 "movl 4(%esp),%ecx\n\t" /* jmp_buf */
44 "movl 8(%esp),%eax\n\t" /* retval */
45 "movl 0(%ecx),%ebp\n\t" /* jmp_buf.Ebp */
46 "movl 4(%ecx),%ebx\n\t" /* jmp_buf.Ebx */
47 "movl 8(%ecx),%edi\n\t" /* jmp_buf.Edi */
48 "movl 12(%ecx),%esi\n\t" /* jmp_buf.Esi */
49 "movl 16(%ecx),%esp\n\t" /* jmp_buf.Esp */
50 "addl $4,%esp\n\t" /* get rid of return address */
51 "jmp *20(%ecx)\n\t" /* jmp_buf.Eip */ )
53 __ASM_GLOBAL_FUNC( __wine_rtl_unwind
,
55 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
56 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
58 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
60 "pushl $0\n\t" /* retval */
61 "pushl 12(%ebp)\n\t" /* record */
62 "pushl 16(%ebp)\n\t" /* target */
63 "pushl 8(%ebp)\n\t" /* frame */
64 "call " __ASM_STDCALL("RtlUnwind",16) "\n\t"
67 #elif defined(__x86_64__)
69 __ASM_GLOBAL_FUNC( __wine_setjmpex
,
70 "movq %rdx,(%rcx)\n\t" /* jmp_buf->Frame */
71 "movq %rbx,0x8(%rcx)\n\t" /* jmp_buf->Rbx */
72 "leaq 0x8(%rsp),%rax\n\t"
73 "movq %rax,0x10(%rcx)\n\t" /* jmp_buf->Rsp */
74 "movq %rbp,0x18(%rcx)\n\t" /* jmp_buf->Rbp */
75 "movq %rsi,0x20(%rcx)\n\t" /* jmp_buf->Rsi */
76 "movq %rdi,0x28(%rcx)\n\t" /* jmp_buf->Rdi */
77 "movq %r12,0x30(%rcx)\n\t" /* jmp_buf->R12 */
78 "movq %r13,0x38(%rcx)\n\t" /* jmp_buf->R13 */
79 "movq %r14,0x40(%rcx)\n\t" /* jmp_buf->R14 */
80 "movq %r15,0x48(%rcx)\n\t" /* jmp_buf->R15 */
81 "movq (%rsp),%rax\n\t"
82 "movq %rax,0x50(%rcx)\n\t" /* jmp_buf->Rip */
83 "movdqa %xmm6,0x60(%rcx)\n\t" /* jmp_buf->Xmm6 */
84 "movdqa %xmm7,0x70(%rcx)\n\t" /* jmp_buf->Xmm7 */
85 "movdqa %xmm8,0x80(%rcx)\n\t" /* jmp_buf->Xmm8 */
86 "movdqa %xmm9,0x90(%rcx)\n\t" /* jmp_buf->Xmm9 */
87 "movdqa %xmm10,0xa0(%rcx)\n\t" /* jmp_buf->Xmm10 */
88 "movdqa %xmm11,0xb0(%rcx)\n\t" /* jmp_buf->Xmm11 */
89 "movdqa %xmm12,0xc0(%rcx)\n\t" /* jmp_buf->Xmm12 */
90 "movdqa %xmm13,0xd0(%rcx)\n\t" /* jmp_buf->Xmm13 */
91 "movdqa %xmm14,0xe0(%rcx)\n\t" /* jmp_buf->Xmm14 */
92 "movdqa %xmm15,0xf0(%rcx)\n\t" /* jmp_buf->Xmm15 */
96 __ASM_GLOBAL_FUNC( __wine_longjmp
,
97 "movq %rdx,%rax\n\t" /* retval */
98 "movq 0x8(%rcx),%rbx\n\t" /* jmp_buf->Rbx */
99 "movq 0x18(%rcx),%rbp\n\t" /* jmp_buf->Rbp */
100 "movq 0x20(%rcx),%rsi\n\t" /* jmp_buf->Rsi */
101 "movq 0x28(%rcx),%rdi\n\t" /* jmp_buf->Rdi */
102 "movq 0x30(%rcx),%r12\n\t" /* jmp_buf->R12 */
103 "movq 0x38(%rcx),%r13\n\t" /* jmp_buf->R13 */
104 "movq 0x40(%rcx),%r14\n\t" /* jmp_buf->R14 */
105 "movq 0x48(%rcx),%r15\n\t" /* jmp_buf->R15 */
106 "movdqa 0x60(%rcx),%xmm6\n\t" /* jmp_buf->Xmm6 */
107 "movdqa 0x70(%rcx),%xmm7\n\t" /* jmp_buf->Xmm7 */
108 "movdqa 0x80(%rcx),%xmm8\n\t" /* jmp_buf->Xmm8 */
109 "movdqa 0x90(%rcx),%xmm9\n\t" /* jmp_buf->Xmm9 */
110 "movdqa 0xa0(%rcx),%xmm10\n\t" /* jmp_buf->Xmm10 */
111 "movdqa 0xb0(%rcx),%xmm11\n\t" /* jmp_buf->Xmm11 */
112 "movdqa 0xc0(%rcx),%xmm12\n\t" /* jmp_buf->Xmm12 */
113 "movdqa 0xd0(%rcx),%xmm13\n\t" /* jmp_buf->Xmm13 */
114 "movdqa 0xe0(%rcx),%xmm14\n\t" /* jmp_buf->Xmm14 */
115 "movdqa 0xf0(%rcx),%xmm15\n\t" /* jmp_buf->Xmm15 */
116 "movq 0x50(%rcx),%rdx\n\t" /* jmp_buf->Rip */
117 "movq 0x10(%rcx),%rsp\n\t" /* jmp_buf->Rsp */
120 __ASM_GLOBAL_FUNC( __wine_rtl_unwind
,
122 __ASM_SEH(".seh_pushreg %rbp\n\t")
123 __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t")
124 __ASM_CFI(".cfi_rel_offset %rbp,0\n\t")
126 __ASM_SEH(".seh_setframe %rbp,0\n\t")
127 __ASM_CFI(".cfi_def_cfa_register %rbp\n\t")
128 "subq $0x20,%rsp\n\t"
129 __ASM_SEH(".seh_stackalloc 0x20\n\t")
130 __ASM_SEH(".seh_endprologue\n\t")
131 "movq %r8,%r9\n\t" /* retval = final target */
132 "movq %rdx,%r8\n\t" /* record */
133 "leaq __wine_unwind_trampoline(%rip),%rdx\n\t" /* target = trampoline */
134 "call " __ASM_NAME("RtlUnwind") "\n"
135 "__wine_unwind_trampoline:\n\t"
136 /* we need an extra call to make sure the stack is correctly aligned */
139 #elif defined(__arm__)
141 __ASM_GLOBAL_FUNC( __wine_setjmpex
,
142 "str r1, [r0]\n\t" /* jmp_buf->Frame */
143 "str r4, [r0, #0x4]\n\t" /* jmp_buf->R4 */
144 "str r5, [r0, #0x8]\n\t" /* jmp_buf->R5 */
145 "str r6, [r0, #0xc]\n\t" /* jmp_buf->R6 */
146 "str r7, [r0, #0x10]\n\t" /* jmp_buf->R7 */
147 "str r8, [r0, #0x14]\n\t" /* jmp_buf->R8 */
148 "str r9, [r0, #0x18]\n\t" /* jmp_buf->R9 */
149 "str r10, [r0, #0x1c]\n\t" /* jmp_buf->R10 */
150 "str r11, [r0, #0x20]\n\t" /* jmp_buf->R11 */
151 "str sp, [r0, #0x24]\n\t" /* jmp_buf->Sp */
152 "str lr, [r0, #0x28]\n\t" /* jmp_buf->Pc */
155 "str r2, [r0, #0x2c]\n\t" /* jmp_buf->Fpscr */
156 "vstr d8, [r0, #0x30]\n\t" /* jmp_buf->D[0] */
157 "vstr d9, [r0, #0x38]\n\t" /* jmp_buf->D[1] */
158 "vstr d10, [r0, #0x40]\n\t" /* jmp_buf->D[2] */
159 "vstr d11, [r0, #0x48]\n\t" /* jmp_buf->D[3] */
160 "vstr d12, [r0, #0x50]\n\t" /* jmp_buf->D[4] */
161 "vstr d13, [r0, #0x58]\n\t" /* jmp_buf->D[5] */
162 "vstr d14, [r0, #0x60]\n\t" /* jmp_buf->D[6] */
163 "vstr d15, [r0, #0x68]\n\t" /* jmp_buf->D[7] */
168 __ASM_GLOBAL_FUNC( __wine_longjmp
,
169 "ldr r4, [r0, #0x4]\n\t" /* jmp_buf->R4 */
170 "ldr r5, [r0, #0x8]\n\t" /* jmp_buf->R5 */
171 "ldr r6, [r0, #0xc]\n\t" /* jmp_buf->R6 */
172 "ldr r7, [r0, #0x10]\n\t" /* jmp_buf->R7 */
173 "ldr r8, [r0, #0x14]\n\t" /* jmp_buf->R8 */
174 "ldr r9, [r0, #0x18]\n\t" /* jmp_buf->R9 */
175 "ldr r10, [r0, #0x1c]\n\t" /* jmp_buf->R10 */
176 "ldr r11, [r0, #0x20]\n\t" /* jmp_buf->R11 */
177 "ldr sp, [r0, #0x24]\n\t" /* jmp_buf->Sp */
178 "ldr r2, [r0, #0x28]\n\t" /* jmp_buf->Pc */
180 "ldr r3, [r0, #0x2c]\n\t" /* jmp_buf->Fpscr */
182 "vldr d8, [r0, #0x30]\n\t" /* jmp_buf->D[0] */
183 "vldr d9, [r0, #0x38]\n\t" /* jmp_buf->D[1] */
184 "vldr d10, [r0, #0x40]\n\t" /* jmp_buf->D[2] */
185 "vldr d11, [r0, #0x48]\n\t" /* jmp_buf->D[3] */
186 "vldr d12, [r0, #0x50]\n\t" /* jmp_buf->D[4] */
187 "vldr d13, [r0, #0x58]\n\t" /* jmp_buf->D[5] */
188 "vldr d14, [r0, #0x60]\n\t" /* jmp_buf->D[6] */
189 "vldr d15, [r0, #0x68]\n\t" /* jmp_buf->D[7] */
191 "mov r0, r1\n\t" /* retval */
194 void __cdecl
__wine_rtl_unwind( EXCEPTION_REGISTRATION_RECORD
* frame
, EXCEPTION_RECORD
*record
,
195 void (*target
)(void) )
197 RtlUnwind( frame
, target
, record
, 0 );
201 #elif defined(__aarch64__)
203 __ASM_GLOBAL_FUNC( __wine_setjmpex
,
204 "str x1, [x0]\n\t" /* jmp_buf->Frame */
205 "stp x19, x20, [x0, #0x10]\n\t" /* jmp_buf->X19, X20 */
206 "stp x21, x22, [x0, #0x20]\n\t" /* jmp_buf->X21, X22 */
207 "stp x23, x24, [x0, #0x30]\n\t" /* jmp_buf->X23, X24 */
208 "stp x25, x26, [x0, #0x40]\n\t" /* jmp_buf->X25, X26 */
209 "stp x27, x28, [x0, #0x50]\n\t" /* jmp_buf->X27, X28 */
210 "stp x29, x30, [x0, #0x60]\n\t" /* jmp_buf->Fp, Lr */
212 "str x2, [x0, #0x70]\n\t" /* jmp_buf->Sp */
214 "str w2, [x0, #0x78]\n\t" /* jmp_buf->Fpcr */
216 "str w2, [x0, #0x7c]\n\t" /* jmp_buf->Fpsr */
217 "stp d8, d9, [x0, #0x80]\n\t" /* jmp_buf->D[0-1] */
218 "stp d10, d11, [x0, #0x90]\n\t" /* jmp_buf->D[2-3] */
219 "stp d12, d13, [x0, #0xa0]\n\t" /* jmp_buf->D[4-5] */
220 "stp d14, d15, [x0, #0xb0]\n\t" /* jmp_buf->D[6-7] */
224 __ASM_GLOBAL_FUNC( __wine_longjmp
,
225 "ldp x19, x20, [x0, #0x10]\n\t" /* jmp_buf->X19, X20 */
226 "ldp x21, x22, [x0, #0x20]\n\t" /* jmp_buf->X21, X22 */
227 "ldp x23, x24, [x0, #0x30]\n\t" /* jmp_buf->X23, X24 */
228 "ldp x25, x26, [x0, #0x40]\n\t" /* jmp_buf->X25, X26 */
229 "ldp x27, x28, [x0, #0x50]\n\t" /* jmp_buf->X27, X28 */
230 "ldp x29, x30, [x0, #0x60]\n\t" /* jmp_buf->Fp, Lr */
231 "ldr x2, [x0, #0x70]\n\t" /* jmp_buf->Sp */
233 "ldr w2, [x0, #0x78]\n\t" /* jmp_buf->Fpcr */
235 "ldr w2, [x0, #0x7c]\n\t" /* jmp_buf->Fpsr */
237 "ldp d8, d9, [x0, #0x80]\n\t" /* jmp_buf->D[0-1] */
238 "ldp d10, d11, [x0, #0x90]\n\t" /* jmp_buf->D[2-3] */
239 "ldp d12, d13, [x0, #0xa0]\n\t" /* jmp_buf->D[4-5] */
240 "ldp d14, d15, [x0, #0xb0]\n\t" /* jmp_buf->D[6-7] */
241 "mov x0, x1\n\t" /* retval */
244 void __cdecl
__wine_rtl_unwind( EXCEPTION_REGISTRATION_RECORD
* frame
, EXCEPTION_RECORD
*record
,
245 void (*target
)(void) )
247 RtlUnwind( frame
, target
, record
, 0 );
253 int __cdecl
__wine_setjmpex( __wine_jmp_buf
*buf
, EXCEPTION_REGISTRATION_RECORD
*frame
)
255 return setjmp( buf
);
258 void __cdecl
__wine_longjmp( __wine_jmp_buf
*buf
, int retval
)
260 for (;;) longjmp( buf
, retval
);
263 void __cdecl
__wine_rtl_unwind( EXCEPTION_REGISTRATION_RECORD
* frame
, EXCEPTION_RECORD
*record
,
264 void (*target
)(void) )
266 RtlUnwind( frame
, target
, record
, 0 );
272 static void DECLSPEC_NORETURN
unwind_target(void)
274 __WINE_FRAME
*wine_frame
= (__WINE_FRAME
*)__wine_get_frame();
275 __wine_pop_frame( &wine_frame
->frame
);
276 for (;;) __wine_longjmp( &wine_frame
->jmp
, 1 );
279 static void DECLSPEC_NORETURN
unwind_frame( EXCEPTION_RECORD
*record
,
280 EXCEPTION_REGISTRATION_RECORD
*frame
)
282 __WINE_FRAME
*wine_frame
= (__WINE_FRAME
*)frame
;
284 /* hack to make GetExceptionCode() work in handler */
285 wine_frame
->ExceptionCode
= record
->ExceptionCode
;
286 wine_frame
->ExceptionRecord
= wine_frame
;
288 __wine_rtl_unwind( frame
, record
, unwind_target
);
291 DWORD __cdecl
__wine_exception_handler( EXCEPTION_RECORD
*record
,
292 EXCEPTION_REGISTRATION_RECORD
*frame
,
294 EXCEPTION_REGISTRATION_RECORD
**pdispatcher
)
296 __WINE_FRAME
*wine_frame
= (__WINE_FRAME
*)frame
;
297 EXCEPTION_POINTERS ptrs
;
299 if (record
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
| EH_NESTED_CALL
))
300 return ExceptionContinueSearch
;
302 ptrs
.ExceptionRecord
= record
;
303 ptrs
.ContextRecord
= context
;
304 switch(wine_frame
->u
.filter( &ptrs
))
306 case EXCEPTION_CONTINUE_SEARCH
:
307 return ExceptionContinueSearch
;
308 case EXCEPTION_CONTINUE_EXECUTION
:
309 return ExceptionContinueExecution
;
310 case EXCEPTION_EXECUTE_HANDLER
:
313 unwind_frame( record
, frame
);
316 DWORD __cdecl
__wine_exception_ctx_handler( EXCEPTION_RECORD
*record
,
317 EXCEPTION_REGISTRATION_RECORD
*frame
,
319 EXCEPTION_REGISTRATION_RECORD
**pdispatcher
)
321 __WINE_FRAME
*wine_frame
= (__WINE_FRAME
*)frame
;
322 EXCEPTION_POINTERS ptrs
;
324 if (record
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
| EH_NESTED_CALL
))
325 return ExceptionContinueSearch
;
327 ptrs
.ExceptionRecord
= record
;
328 ptrs
.ContextRecord
= context
;
329 switch(wine_frame
->u
.filter_ctx( &ptrs
, wine_frame
->ctx
))
331 case EXCEPTION_CONTINUE_SEARCH
:
332 return ExceptionContinueSearch
;
333 case EXCEPTION_CONTINUE_EXECUTION
:
334 return ExceptionContinueExecution
;
335 case EXCEPTION_EXECUTE_HANDLER
:
338 unwind_frame( record
, frame
);
341 DWORD __cdecl
__wine_exception_handler_page_fault( EXCEPTION_RECORD
*record
,
342 EXCEPTION_REGISTRATION_RECORD
*frame
,
344 EXCEPTION_REGISTRATION_RECORD
**pdispatcher
)
346 if (record
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
| EH_NESTED_CALL
))
347 return ExceptionContinueSearch
;
348 if (record
->ExceptionCode
!= STATUS_ACCESS_VIOLATION
)
349 return ExceptionContinueSearch
;
350 unwind_frame( record
, frame
);
353 DWORD __cdecl
__wine_exception_handler_all( EXCEPTION_RECORD
*record
,
354 EXCEPTION_REGISTRATION_RECORD
*frame
,
356 EXCEPTION_REGISTRATION_RECORD
**pdispatcher
)
358 if (record
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
| EH_NESTED_CALL
))
359 return ExceptionContinueSearch
;
360 unwind_frame( record
, frame
);
363 DWORD __cdecl
__wine_finally_handler( EXCEPTION_RECORD
*record
,
364 EXCEPTION_REGISTRATION_RECORD
*frame
,
366 EXCEPTION_REGISTRATION_RECORD
**pdispatcher
)
368 if (record
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
))
370 __WINE_FRAME
*wine_frame
= (__WINE_FRAME
*)frame
;
371 wine_frame
->u
.finally_func( FALSE
);
373 return ExceptionContinueSearch
;
376 DWORD __cdecl
__wine_finally_ctx_handler( EXCEPTION_RECORD
*record
,
377 EXCEPTION_REGISTRATION_RECORD
*frame
,
379 EXCEPTION_REGISTRATION_RECORD
**pdispatcher
)
381 if (record
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
))
383 __WINE_FRAME
*wine_frame
= (__WINE_FRAME
*)frame
;
384 wine_frame
->u
.finally_func_ctx( FALSE
, wine_frame
->ctx
);
386 return ExceptionContinueSearch
;
389 #endif /* __GNUC__ || __clang__ */