wined3d: Pass a wined3d_resource_desc structure to wined3d_texture_create_3d().
[wine/multimedia.git] / dlls / ntdll / signal_arm.c
blob6334a6e0dad3efb11bbb7c704e1ed96a07b82ce4
1 /*
2 * ARM signal handling routines
4 * Copyright 2002 Marcus Meissner, SuSE Linux AG
5 * Copyright 2010, 2011 André Hentschel
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.
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.
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
22 #ifdef __arm__
24 #include "config.h"
25 #include "wine/port.h"
27 #include <assert.h>
28 #include <signal.h>
29 #include <stdlib.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #ifdef HAVE_UNISTD_H
33 # include <unistd.h>
34 #endif
36 #ifdef HAVE_SYS_PARAM_H
37 # include <sys/param.h>
38 #endif
39 #ifdef HAVE_SYSCALL_H
40 # include <syscall.h>
41 #else
42 # ifdef HAVE_SYS_SYSCALL_H
43 # include <sys/syscall.h>
44 # endif
45 #endif
46 #ifdef HAVE_SYS_SIGNAL_H
47 # include <sys/signal.h>
48 #endif
50 #define NONAMELESSUNION
51 #define NONAMELESSSTRUCT
52 #include "ntstatus.h"
53 #define WIN32_NO_STATUS
54 #include "windef.h"
55 #include "winternl.h"
56 #include "wine/library.h"
57 #include "wine/exception.h"
58 #include "ntdll_misc.h"
59 #include "wine/debug.h"
60 #include "winnt.h"
62 WINE_DEFAULT_DEBUG_CHANNEL(seh);
64 static pthread_key_t teb_key;
66 /***********************************************************************
67 * signal context platform-specific definitions
69 #ifdef linux
71 typedef ucontext_t SIGCONTEXT;
73 /* All Registers access - only for local access */
74 # define REG_sig(reg_name, context) ((context)->uc_mcontext.reg_name)
75 # define REGn_sig(reg_num, context) ((context)->uc_mcontext.arm_r##reg_num)
77 /* Special Registers access */
78 # define SP_sig(context) REG_sig(arm_sp, context) /* Stack pointer */
79 # define LR_sig(context) REG_sig(arm_lr, context) /* Link register */
80 # define PC_sig(context) REG_sig(arm_pc, context) /* Program counter */
81 # define CPSR_sig(context) REG_sig(arm_cpsr, context) /* Current State Register */
82 # define IP_sig(context) REG_sig(arm_ip, context) /* Intra-Procedure-call scratch register */
83 # define FP_sig(context) REG_sig(arm_fp, context) /* Frame pointer */
85 /* Exceptions */
86 # define ERROR_sig(context) REG_sig(error_code, context)
87 # define FAULT_sig(context) REG_sig(fault_address, context)
88 # define TRAP_sig(context) REG_sig(trap_no, context)
90 #endif /* linux */
92 enum arm_trap_code
94 TRAP_ARM_UNKNOWN = -1, /* Unknown fault (TRAP_sig not defined) */
95 TRAP_ARM_PRIVINFLT = 6, /* Invalid opcode exception */
96 TRAP_ARM_PAGEFLT = 14, /* Page fault */
97 TRAP_ARM_ALIGNFLT = 17, /* Alignment check exception */
100 typedef void (WINAPI *raise_func)( EXCEPTION_RECORD *rec, CONTEXT *context );
101 typedef int (*wine_signal_handler)(unsigned int sig);
103 static wine_signal_handler handlers[256];
106 struct UNWIND_INFO
108 WORD function_length;
109 WORD unknown1 : 7;
110 WORD count : 5;
111 WORD unknown2 : 4;
114 /***********************************************************************
115 * dispatch_signal
117 static inline int dispatch_signal(unsigned int sig)
119 if (handlers[sig] == NULL) return 0;
120 return handlers[sig](sig);
123 /*******************************************************************
124 * is_valid_frame
126 static inline BOOL is_valid_frame( void *frame )
128 if ((ULONG_PTR)frame & 3) return FALSE;
129 return (frame >= NtCurrentTeb()->Tib.StackLimit &&
130 (void **)frame < (void **)NtCurrentTeb()->Tib.StackBase - 1);
133 /***********************************************************************
134 * save_context
136 * Set the register values from a sigcontext.
138 static void save_context( CONTEXT *context, const SIGCONTEXT *sigcontext )
140 #define C(x) context->R##x = REGn_sig(x,sigcontext)
141 /* Save normal registers */
142 C(0); C(1); C(2); C(3); C(4); C(5); C(6); C(7); C(8); C(9); C(10);
143 #undef C
145 context->ContextFlags = CONTEXT_FULL;
146 context->Sp = SP_sig(sigcontext); /* Stack pointer */
147 context->Lr = LR_sig(sigcontext); /* Link register */
148 context->Pc = PC_sig(sigcontext); /* Program Counter */
149 context->Cpsr = CPSR_sig(sigcontext); /* Current State Register */
150 context->Ip = IP_sig(sigcontext); /* Intra-Procedure-call scratch register */
151 context->Fp = FP_sig(sigcontext); /* Frame pointer */
155 /***********************************************************************
156 * restore_context
158 * Build a sigcontext from the register values.
160 static void restore_context( const CONTEXT *context, SIGCONTEXT *sigcontext )
162 #define C(x) REGn_sig(x,sigcontext) = context->R##x
163 /* Restore normal registers */
164 C(0); C(1); C(2); C(3); C(4); C(5); C(6); C(7); C(8); C(9); C(10);
165 #undef C
167 SP_sig(sigcontext) = context->Sp; /* Stack pointer */
168 LR_sig(sigcontext) = context->Lr ; /* Link register */
169 PC_sig(sigcontext) = context->Pc; /* Program Counter */
170 CPSR_sig(sigcontext) = context->Cpsr; /* Current State Register */
171 IP_sig(sigcontext) = context->Ip; /* Intra-Procedure-call scratch register */
172 FP_sig(sigcontext) = context->Fp; /* Frame pointer */
176 /***********************************************************************
177 * save_fpu
179 * Set the FPU context from a sigcontext.
181 static inline void save_fpu( CONTEXT *context, const SIGCONTEXT *sigcontext )
183 FIXME("not implemented\n");
187 /***********************************************************************
188 * restore_fpu
190 * Restore the FPU context to a sigcontext.
192 static inline void restore_fpu( CONTEXT *context, const SIGCONTEXT *sigcontext )
194 FIXME("not implemented\n");
198 /***********************************************************************
199 * RtlCaptureContext (NTDLL.@)
201 /* FIXME: Use the Stack instead of the actual register values */
202 __ASM_STDCALL_FUNC( RtlCaptureContext, 4,
203 ".arm\n\t"
204 "stmfd SP!, {r1}\n\t"
205 "mov r1, #0x40\n\t" /* CONTEXT_ARM */
206 "add r1, r1, #0x3\n\t" /* CONTEXT_FULL */
207 "str r1, [r0]\n\t" /* context->ContextFlags */
208 "ldmfd SP!, {r1}\n\t"
209 "str r0, [r0, #0x4]\n\t" /* context->R0 */
210 "str r1, [r0, #0x8]\n\t" /* context->R1 */
211 "str r2, [r0, #0xc]\n\t" /* context->R2 */
212 "str r3, [r0, #0x10]\n\t" /* context->R3 */
213 "str r4, [r0, #0x14]\n\t" /* context->R4 */
214 "str r5, [r0, #0x18]\n\t" /* context->R5 */
215 "str r6, [r0, #0x1c]\n\t" /* context->R6 */
216 "str r7, [r0, #0x20]\n\t" /* context->R7 */
217 "str r8, [r0, #0x24]\n\t" /* context->R8 */
218 "str r9, [r0, #0x28]\n\t" /* context->R9 */
219 "str r10, [r0, #0x2c]\n\t" /* context->R10 */
220 "str r11, [r0, #0x30]\n\t" /* context->Fp */
221 "str IP, [r0, #0x34]\n\t" /* context->Ip */
222 "str SP, [r0, #0x38]\n\t" /* context->Sp */
223 "str LR, [r0, #0x3c]\n\t" /* context->Lr */
224 "str PC, [r0, #0x40]\n\t" /* context->Pc */
225 "mrs r1, CPSR\n\t"
226 "str r1, [r0, #0x44]\n\t" /* context->Cpsr */
227 "mov PC, LR\n"
231 /***********************************************************************
232 * set_cpu_context
234 * Set the new CPU context.
236 /* FIXME: What about the CPSR? */
237 __ASM_GLOBAL_FUNC( set_cpu_context,
238 "mov IP, r0\n\t"
239 "ldr r0, [IP, #0x4]\n\t" /* context->R0 */
240 "ldr r1, [IP, #0x8]\n\t" /* context->R1 */
241 "ldr r2, [IP, #0xc]\n\t" /* context->R2 */
242 "ldr r3, [IP, #0x10]\n\t" /* context->R3 */
243 "ldr r4, [IP, #0x14]\n\t" /* context->R4 */
244 "ldr r5, [IP, #0x18]\n\t" /* context->R5 */
245 "ldr r6, [IP, #0x1c]\n\t" /* context->R6 */
246 "ldr r7, [IP, #0x20]\n\t" /* context->R7 */
247 "ldr r8, [IP, #0x24]\n\t" /* context->R8 */
248 "ldr r9, [IP, #0x28]\n\t" /* context->R9 */
249 "ldr r10, [IP, #0x2c]\n\t" /* context->R10 */
250 "ldr r11, [IP, #0x30]\n\t" /* context->Fp */
251 "ldr SP, [IP, #0x38]\n\t" /* context->Sp */
252 "ldr LR, [IP, #0x3c]\n\t" /* context->Lr */
253 "ldr PC, [IP, #0x40]\n\t" /* context->Pc */
257 /***********************************************************************
258 * copy_context
260 * Copy a register context according to the flags.
262 void copy_context( CONTEXT *to, const CONTEXT *from, DWORD flags )
264 flags &= ~CONTEXT_ARM; /* get rid of CPU id */
265 if (flags & CONTEXT_CONTROL)
267 to->Sp = from->Sp;
268 to->Lr = from->Lr;
269 to->Pc = from->Pc;
270 to->Cpsr = from->Cpsr;
272 if (flags & CONTEXT_INTEGER)
274 to->R0 = from->R0;
275 to->R1 = from->R1;
276 to->R2 = from->R2;
277 to->R3 = from->R3;
278 to->R4 = from->R4;
279 to->R5 = from->R5;
280 to->R6 = from->R6;
281 to->R7 = from->R7;
282 to->R8 = from->R8;
283 to->R9 = from->R9;
284 to->R10 = from->R10;
285 to->Ip = from->Ip;
286 to->Fp = from->Fp;
291 /***********************************************************************
292 * context_to_server
294 * Convert a register context to the server format.
296 NTSTATUS context_to_server( context_t *to, const CONTEXT *from )
298 DWORD flags = from->ContextFlags & ~CONTEXT_ARM; /* get rid of CPU id */
300 memset( to, 0, sizeof(*to) );
301 to->cpu = CPU_ARM;
303 if (flags & CONTEXT_CONTROL)
305 to->flags |= SERVER_CTX_CONTROL;
306 to->ctl.arm_regs.sp = from->Sp;
307 to->ctl.arm_regs.lr = from->Lr;
308 to->ctl.arm_regs.pc = from->Pc;
309 to->ctl.arm_regs.cpsr = from->Cpsr;
311 if (flags & CONTEXT_INTEGER)
313 to->flags |= SERVER_CTX_INTEGER;
314 to->integer.arm_regs.r[0] = from->R0;
315 to->integer.arm_regs.r[1] = from->R1;
316 to->integer.arm_regs.r[2] = from->R2;
317 to->integer.arm_regs.r[3] = from->R3;
318 to->integer.arm_regs.r[4] = from->R4;
319 to->integer.arm_regs.r[5] = from->R5;
320 to->integer.arm_regs.r[6] = from->R6;
321 to->integer.arm_regs.r[7] = from->R7;
322 to->integer.arm_regs.r[8] = from->R8;
323 to->integer.arm_regs.r[9] = from->R9;
324 to->integer.arm_regs.r[10] = from->R10;
325 to->integer.arm_regs.r[11] = from->Fp;
326 to->integer.arm_regs.r[12] = from->Ip;
328 return STATUS_SUCCESS;
332 /***********************************************************************
333 * context_from_server
335 * Convert a register context from the server format.
337 NTSTATUS context_from_server( CONTEXT *to, const context_t *from )
339 if (from->cpu != CPU_ARM) return STATUS_INVALID_PARAMETER;
341 to->ContextFlags = CONTEXT_ARM;
342 if (from->flags & SERVER_CTX_CONTROL)
344 to->ContextFlags |= CONTEXT_CONTROL;
345 to->Sp = from->ctl.arm_regs.sp;
346 to->Lr = from->ctl.arm_regs.lr;
347 to->Pc = from->ctl.arm_regs.pc;
348 to->Cpsr = from->ctl.arm_regs.cpsr;
350 if (from->flags & SERVER_CTX_INTEGER)
352 to->ContextFlags |= CONTEXT_INTEGER;
353 to->R0 = from->integer.arm_regs.r[0];
354 to->R1 = from->integer.arm_regs.r[1];
355 to->R2 = from->integer.arm_regs.r[2];
356 to->R3 = from->integer.arm_regs.r[3];
357 to->R4 = from->integer.arm_regs.r[4];
358 to->R5 = from->integer.arm_regs.r[5];
359 to->R6 = from->integer.arm_regs.r[6];
360 to->R7 = from->integer.arm_regs.r[7];
361 to->R8 = from->integer.arm_regs.r[8];
362 to->R9 = from->integer.arm_regs.r[9];
363 to->R10 = from->integer.arm_regs.r[10];
364 to->Fp = from->integer.arm_regs.r[11];
365 to->Ip = from->integer.arm_regs.r[12];
367 return STATUS_SUCCESS;
370 extern void raise_func_trampoline_thumb( EXCEPTION_RECORD *rec, CONTEXT *context, raise_func func );
371 __ASM_GLOBAL_FUNC( raise_func_trampoline_thumb,
372 ".thumb\n\t"
373 "blx r2\n\t"
374 "bkpt")
376 extern void raise_func_trampoline_arm( EXCEPTION_RECORD *rec, CONTEXT *context, raise_func func );
377 __ASM_GLOBAL_FUNC( raise_func_trampoline_arm,
378 ".arm\n\t"
379 "blx r2\n\t"
380 "bkpt")
382 /***********************************************************************
383 * setup_exception_record
385 * Setup the exception record and context on the thread stack.
387 static EXCEPTION_RECORD *setup_exception( SIGCONTEXT *sigcontext, raise_func func )
389 struct stack_layout
391 CONTEXT context;
392 EXCEPTION_RECORD rec;
393 } *stack;
394 DWORD exception_code = 0;
396 stack = (struct stack_layout *)(SP_sig(sigcontext) & ~3);
397 stack--; /* push the stack_layout structure */
399 stack->rec.ExceptionRecord = NULL;
400 stack->rec.ExceptionCode = exception_code;
401 stack->rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
402 stack->rec.ExceptionAddress = (LPVOID)PC_sig(sigcontext);
403 stack->rec.NumberParameters = 0;
405 save_context( &stack->context, sigcontext );
407 /* now modify the sigcontext to return to the raise function */
408 SP_sig(sigcontext) = (DWORD)stack;
409 if (CPSR_sig(sigcontext) & 0x20)
410 PC_sig(sigcontext) = (DWORD)raise_func_trampoline_thumb;
411 else
412 PC_sig(sigcontext) = (DWORD)raise_func_trampoline_arm;
413 REGn_sig(0, sigcontext) = (DWORD)&stack->rec; /* first arg for raise_func */
414 REGn_sig(1, sigcontext) = (DWORD)&stack->context; /* second arg for raise_func */
415 REGn_sig(2, sigcontext) = (DWORD)func; /* the raise_func as third arg for the trampoline */
418 return &stack->rec;
421 /**********************************************************************
422 * raise_segv_exception
424 static void WINAPI raise_segv_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
426 NTSTATUS status;
428 switch(rec->ExceptionCode)
430 case EXCEPTION_ACCESS_VIOLATION:
431 if (rec->NumberParameters == 2)
433 if (!(rec->ExceptionCode = virtual_handle_fault( (void *)rec->ExceptionInformation[1],
434 rec->ExceptionInformation[0] )))
435 goto done;
437 break;
439 status = NtRaiseException( rec, context, TRUE );
440 if (status) raise_status( status, rec );
441 done:
442 set_cpu_context( context );
445 /**********************************************************************
446 * call_stack_handlers
448 * Call the stack handlers chain.
450 static NTSTATUS call_stack_handlers( EXCEPTION_RECORD *rec, CONTEXT *context )
452 EXCEPTION_REGISTRATION_RECORD *frame, *dispatch, *nested_frame;
453 DWORD res;
455 frame = NtCurrentTeb()->Tib.ExceptionList;
456 nested_frame = NULL;
457 while (frame != (EXCEPTION_REGISTRATION_RECORD*)~0UL)
459 /* Check frame address */
460 if (!is_valid_frame( frame ))
462 rec->ExceptionFlags |= EH_STACK_INVALID;
463 break;
466 /* Call handler */
467 TRACE( "calling handler at %p code=%x flags=%x\n",
468 frame->Handler, rec->ExceptionCode, rec->ExceptionFlags );
469 res = frame->Handler( rec, frame, context, &dispatch );
470 TRACE( "handler at %p returned %x\n", frame->Handler, res );
472 if (frame == nested_frame)
474 /* no longer nested */
475 nested_frame = NULL;
476 rec->ExceptionFlags &= ~EH_NESTED_CALL;
479 switch(res)
481 case ExceptionContinueExecution:
482 if (!(rec->ExceptionFlags & EH_NONCONTINUABLE)) return STATUS_SUCCESS;
483 return STATUS_NONCONTINUABLE_EXCEPTION;
484 case ExceptionContinueSearch:
485 break;
486 case ExceptionNestedException:
487 if (nested_frame < dispatch) nested_frame = dispatch;
488 rec->ExceptionFlags |= EH_NESTED_CALL;
489 break;
490 default:
491 return STATUS_INVALID_DISPOSITION;
493 frame = frame->Prev;
495 return STATUS_UNHANDLED_EXCEPTION;
499 /*******************************************************************
500 * raise_exception
502 * Implementation of NtRaiseException.
504 static NTSTATUS raise_exception( EXCEPTION_RECORD *rec, CONTEXT *context, BOOL first_chance )
506 NTSTATUS status;
508 if (first_chance)
510 DWORD c;
512 for (c = 0; c < rec->NumberParameters; c++)
513 TRACE( " info[%d]=%08lx\n", c, rec->ExceptionInformation[c] );
514 if (rec->ExceptionCode == EXCEPTION_WINE_STUB)
516 if (rec->ExceptionInformation[1] >> 16)
517 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
518 rec->ExceptionAddress,
519 (char*)rec->ExceptionInformation[0], (char*)rec->ExceptionInformation[1] );
520 else
521 MESSAGE( "wine: Call from %p to unimplemented function %s.%ld, aborting\n",
522 rec->ExceptionAddress,
523 (char*)rec->ExceptionInformation[0], rec->ExceptionInformation[1] );
525 else
527 TRACE(" Pc:%04x Sp:%04x Lr:%04x Cpsr:%04x r0:%04x r1:%04x r2:%04x r3:%04x\n",
528 context->Pc, context->Sp, context->Lr, context->Cpsr,
529 context->R0, context->R1, context->R2, context->R3);
530 TRACE(" r4:%04x r5:%04x r6:%04x r7:%04x r8:%04x r9:%04x r10:%04x Fp:%04x Ip:%04x\n",
531 context->R4, context->R5, context->R6, context->R7, context->R8,
532 context->R9, context->R10, context->Fp, context->Ip );
535 status = send_debug_event( rec, TRUE, context );
536 if (status == DBG_CONTINUE || status == DBG_EXCEPTION_HANDLED)
537 return STATUS_SUCCESS;
539 if (call_vectored_handlers( rec, context ) == EXCEPTION_CONTINUE_EXECUTION)
540 return STATUS_SUCCESS;
542 if ((status = call_stack_handlers( rec, context )) != STATUS_UNHANDLED_EXCEPTION)
543 return status;
546 /* last chance exception */
548 status = send_debug_event( rec, FALSE, context );
549 if (status != DBG_CONTINUE)
551 if (rec->ExceptionFlags & EH_STACK_INVALID)
552 ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
553 else if (rec->ExceptionCode == STATUS_NONCONTINUABLE_EXCEPTION)
554 ERR("Process attempted to continue execution after noncontinuable exception.\n");
555 else
556 ERR("Unhandled exception code %x flags %x addr %p\n",
557 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
558 NtTerminateProcess( NtCurrentProcess(), rec->ExceptionCode );
560 return STATUS_SUCCESS;
564 /**********************************************************************
565 * segv_handler
567 * Handler for SIGSEGV and related errors.
569 static void segv_handler( int signal, siginfo_t *info, void *ucontext )
571 EXCEPTION_RECORD *rec;
572 SIGCONTEXT *context = ucontext;
574 /* check for page fault inside the thread stack */
575 if (TRAP_sig(context) == TRAP_ARM_PAGEFLT &&
576 (char *)info->si_addr >= (char *)NtCurrentTeb()->DeallocationStack &&
577 (char *)info->si_addr < (char *)NtCurrentTeb()->Tib.StackBase &&
578 virtual_handle_stack_fault( info->si_addr ))
580 /* check if this was the last guard page */
581 if ((char *)info->si_addr < (char *)NtCurrentTeb()->DeallocationStack + 2*4096)
583 rec = setup_exception( context, raise_segv_exception );
584 rec->ExceptionCode = EXCEPTION_STACK_OVERFLOW;
586 return;
589 rec = setup_exception( context, raise_segv_exception );
590 if (rec->ExceptionCode == EXCEPTION_STACK_OVERFLOW) return;
592 switch(TRAP_sig(context))
594 case TRAP_ARM_PRIVINFLT: /* Invalid opcode exception */
595 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
596 break;
597 case TRAP_ARM_PAGEFLT: /* Page fault */
598 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
599 rec->NumberParameters = 2;
600 rec->ExceptionInformation[0] = (ERROR_sig(context) & 0x800) != 0;
601 rec->ExceptionInformation[1] = (ULONG_PTR)info->si_addr;
602 break;
603 case TRAP_ARM_ALIGNFLT: /* Alignment check exception */
604 rec->ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
605 break;
606 default:
607 ERR("Got unexpected trap %ld\n", TRAP_sig(context));
608 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
609 break;
613 /**********************************************************************
614 * trap_handler
616 * Handler for SIGTRAP.
618 static void trap_handler( int signal, siginfo_t *info, void *ucontext )
620 EXCEPTION_RECORD rec;
621 CONTEXT context;
622 NTSTATUS status;
624 switch ( info->si_code )
626 case TRAP_TRACE:
627 rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
628 break;
629 case TRAP_BRKPT:
630 default:
631 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
632 break;
635 save_context( &context, ucontext );
636 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
637 rec.ExceptionRecord = NULL;
638 rec.ExceptionAddress = (LPVOID)context.Pc;
639 rec.NumberParameters = 0;
640 status = raise_exception( &rec, &context, TRUE );
641 if (status) raise_status( status, &rec );
642 restore_context( &context, ucontext );
645 /**********************************************************************
646 * fpe_handler
648 * Handler for SIGFPE.
650 static void fpe_handler( int signal, siginfo_t *siginfo, void *sigcontext )
652 EXCEPTION_RECORD rec;
653 CONTEXT context;
654 NTSTATUS status;
656 save_fpu( &context, sigcontext );
657 save_context( &context, sigcontext );
659 switch (siginfo->si_code & 0xffff )
661 #ifdef FPE_FLTSUB
662 case FPE_FLTSUB:
663 rec.ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
664 break;
665 #endif
666 #ifdef FPE_INTDIV
667 case FPE_INTDIV:
668 rec.ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
669 break;
670 #endif
671 #ifdef FPE_INTOVF
672 case FPE_INTOVF:
673 rec.ExceptionCode = EXCEPTION_INT_OVERFLOW;
674 break;
675 #endif
676 #ifdef FPE_FLTDIV
677 case FPE_FLTDIV:
678 rec.ExceptionCode = EXCEPTION_FLT_DIVIDE_BY_ZERO;
679 break;
680 #endif
681 #ifdef FPE_FLTOVF
682 case FPE_FLTOVF:
683 rec.ExceptionCode = EXCEPTION_FLT_OVERFLOW;
684 break;
685 #endif
686 #ifdef FPE_FLTUND
687 case FPE_FLTUND:
688 rec.ExceptionCode = EXCEPTION_FLT_UNDERFLOW;
689 break;
690 #endif
691 #ifdef FPE_FLTRES
692 case FPE_FLTRES:
693 rec.ExceptionCode = EXCEPTION_FLT_INEXACT_RESULT;
694 break;
695 #endif
696 #ifdef FPE_FLTINV
697 case FPE_FLTINV:
698 #endif
699 default:
700 rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
701 break;
703 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
704 rec.ExceptionRecord = NULL;
705 rec.ExceptionAddress = (LPVOID)context.Pc;
706 rec.NumberParameters = 0;
707 status = raise_exception( &rec, &context, TRUE );
708 if (status) raise_status( status, &rec );
710 restore_context( &context, sigcontext );
711 restore_fpu( &context, sigcontext );
714 /**********************************************************************
715 * int_handler
717 * Handler for SIGINT.
719 static void int_handler( int signal, siginfo_t *siginfo, void *sigcontext )
721 if (!dispatch_signal(SIGINT))
723 EXCEPTION_RECORD rec;
724 CONTEXT context;
725 NTSTATUS status;
727 save_context( &context, sigcontext );
728 rec.ExceptionCode = CONTROL_C_EXIT;
729 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
730 rec.ExceptionRecord = NULL;
731 rec.ExceptionAddress = (LPVOID)context.Pc;
732 rec.NumberParameters = 0;
733 status = raise_exception( &rec, &context, TRUE );
734 if (status) raise_status( status, &rec );
735 restore_context( &context, sigcontext );
740 /**********************************************************************
741 * abrt_handler
743 * Handler for SIGABRT.
745 static void abrt_handler( int signal, siginfo_t *siginfo, void *sigcontext )
747 EXCEPTION_RECORD rec;
748 CONTEXT context;
749 NTSTATUS status;
751 save_context( &context, sigcontext );
752 rec.ExceptionCode = EXCEPTION_WINE_ASSERTION;
753 rec.ExceptionFlags = EH_NONCONTINUABLE;
754 rec.ExceptionRecord = NULL;
755 rec.ExceptionAddress = (LPVOID)context.Pc;
756 rec.NumberParameters = 0;
757 status = raise_exception( &rec, &context, TRUE );
758 if (status) raise_status( status, &rec );
759 restore_context( &context, sigcontext );
763 /**********************************************************************
764 * quit_handler
766 * Handler for SIGQUIT.
768 static void quit_handler( int signal, siginfo_t *siginfo, void *sigcontext )
770 abort_thread(0);
774 /**********************************************************************
775 * usr1_handler
777 * Handler for SIGUSR1, used to signal a thread that it got suspended.
779 static void usr1_handler( int signal, siginfo_t *siginfo, void *sigcontext )
781 CONTEXT context;
783 save_context( &context, sigcontext );
784 wait_suspend( &context );
785 restore_context( &context, sigcontext );
789 /***********************************************************************
790 * __wine_set_signal_handler (NTDLL.@)
792 int CDECL __wine_set_signal_handler(unsigned int sig, wine_signal_handler wsh)
794 if (sig > sizeof(handlers) / sizeof(handlers[0])) return -1;
795 if (handlers[sig] != NULL) return -2;
796 handlers[sig] = wsh;
797 return 0;
801 /**********************************************************************
802 * signal_alloc_thread
804 NTSTATUS signal_alloc_thread( TEB **teb )
806 static size_t sigstack_zero_bits;
807 SIZE_T size;
808 NTSTATUS status;
810 if (!sigstack_zero_bits)
812 size_t min_size = page_size;
813 /* find the first power of two not smaller than min_size */
814 while ((1u << sigstack_zero_bits) < min_size) sigstack_zero_bits++;
815 assert( sizeof(TEB) <= min_size );
818 size = 1 << sigstack_zero_bits;
819 *teb = NULL;
820 if (!(status = NtAllocateVirtualMemory( NtCurrentProcess(), (void **)teb, sigstack_zero_bits,
821 &size, MEM_COMMIT | MEM_TOP_DOWN, PAGE_READWRITE )))
823 (*teb)->Tib.Self = &(*teb)->Tib;
824 (*teb)->Tib.ExceptionList = (void *)~0UL;
826 return status;
830 /**********************************************************************
831 * signal_free_thread
833 void signal_free_thread( TEB *teb )
835 SIZE_T size;
837 if (teb->DeallocationStack)
839 size = 0;
840 NtFreeVirtualMemory( GetCurrentProcess(), &teb->DeallocationStack, &size, MEM_RELEASE );
842 size = 0;
843 NtFreeVirtualMemory( NtCurrentProcess(), (void **)&teb, &size, MEM_RELEASE );
846 /**********************************************************************
847 * set_tpidrurw
849 * Win32/ARM applications expect the TEB pointer to be in the TPIDRURW register.
851 #ifdef __ARM_ARCH_7A__
852 extern void set_tpidrurw( TEB *teb );
853 __ASM_GLOBAL_FUNC( set_tpidrurw,
854 "mcr p15, 0, r0, c13, c0, 2\n\t" /* TEB -> TPIDRURW */
855 "blx lr" )
856 #else
857 void set_tpidrurw( TEB *teb )
860 #endif
862 /**********************************************************************
863 * signal_init_thread
865 void signal_init_thread( TEB *teb )
867 static int init_done;
869 if (!init_done)
871 pthread_key_create( &teb_key, NULL );
872 init_done = 1;
874 set_tpidrurw( teb );
875 pthread_setspecific( teb_key, teb );
879 /**********************************************************************
880 * signal_init_process
882 void signal_init_process(void)
884 struct sigaction sig_act;
886 sig_act.sa_mask = server_block_set;
887 sig_act.sa_flags = SA_RESTART | SA_SIGINFO;
889 sig_act.sa_sigaction = int_handler;
890 if (sigaction( SIGINT, &sig_act, NULL ) == -1) goto error;
891 sig_act.sa_sigaction = fpe_handler;
892 if (sigaction( SIGFPE, &sig_act, NULL ) == -1) goto error;
893 sig_act.sa_sigaction = abrt_handler;
894 if (sigaction( SIGABRT, &sig_act, NULL ) == -1) goto error;
895 sig_act.sa_sigaction = quit_handler;
896 if (sigaction( SIGQUIT, &sig_act, NULL ) == -1) goto error;
897 sig_act.sa_sigaction = usr1_handler;
898 if (sigaction( SIGUSR1, &sig_act, NULL ) == -1) goto error;
900 sig_act.sa_sigaction = segv_handler;
901 if (sigaction( SIGSEGV, &sig_act, NULL ) == -1) goto error;
902 if (sigaction( SIGILL, &sig_act, NULL ) == -1) goto error;
903 #ifdef SIGBUS
904 if (sigaction( SIGBUS, &sig_act, NULL ) == -1) goto error;
905 #endif
907 #ifdef SIGTRAP
908 sig_act.sa_sigaction = trap_handler;
909 if (sigaction( SIGTRAP, &sig_act, NULL ) == -1) goto error;
910 #endif
911 return;
913 error:
914 perror("sigaction");
915 exit(1);
919 /**********************************************************************
920 * __wine_enter_vm86 (NTDLL.@)
922 void __wine_enter_vm86( CONTEXT *context )
924 MESSAGE("vm86 mode not supported on this platform\n");
928 /**********************************************************************
929 * RtlAddFunctionTable (NTDLL.@)
931 BOOLEAN CDECL RtlAddFunctionTable( RUNTIME_FUNCTION *table, DWORD count, DWORD addr )
933 FIXME( "%p %u %x: stub\n", table, count, addr );
934 return TRUE;
938 /**********************************************************************
939 * RtlDeleteFunctionTable (NTDLL.@)
941 BOOLEAN CDECL RtlDeleteFunctionTable( RUNTIME_FUNCTION *table )
943 FIXME( "%p: stub\n", table );
944 return TRUE;
947 /**********************************************************************
948 * find_function_info
950 static RUNTIME_FUNCTION *find_function_info( ULONG_PTR pc, HMODULE module,
951 RUNTIME_FUNCTION *func, ULONG size )
953 int min = 0;
954 int max = size/sizeof(*func) - 1;
956 while (min <= max)
958 int pos = (min + max) / 2;
959 DWORD begin = (func[pos].BeginAddress & ~1), end;
960 if (func[pos].u.s.Flag)
961 end = begin + func[pos].u.s.FunctionLength * 2;
962 else
964 struct UNWIND_INFO *info;
965 info = (struct UNWIND_INFO *)((char *)module + func[pos].u.UnwindData);
966 end = begin + info->function_length * 2;
969 if ((char *)pc < (char *)module + begin) max = pos - 1;
970 else if ((char *)pc >= (char *)module + end) min = pos + 1;
971 else return func + pos;
973 return NULL;
976 /**********************************************************************
977 * RtlLookupFunctionEntry (NTDLL.@)
979 PRUNTIME_FUNCTION WINAPI RtlLookupFunctionEntry( ULONG_PTR pc, DWORD *base,
980 UNWIND_HISTORY_TABLE *table )
982 LDR_MODULE *module;
983 RUNTIME_FUNCTION *func;
984 ULONG size;
986 /* FIXME: should use the history table to make things faster */
988 if (LdrFindEntryForAddress( (void *)pc, &module ))
990 WARN( "module not found for %lx\n", pc );
991 return NULL;
993 if (!(func = RtlImageDirectoryEntryToData( module->BaseAddress, TRUE,
994 IMAGE_DIRECTORY_ENTRY_EXCEPTION, &size )))
996 WARN( "no exception table found in module %p pc %lx\n", module->BaseAddress, pc );
997 return NULL;
999 func = find_function_info( pc, module->BaseAddress, func, size );
1000 if (func) *base = (DWORD)module->BaseAddress;
1001 return func;
1004 /***********************************************************************
1005 * RtlUnwind (NTDLL.@)
1007 void WINAPI RtlUnwind( void *endframe, void *target_ip, EXCEPTION_RECORD *rec, void *retval )
1009 CONTEXT context;
1010 EXCEPTION_RECORD record;
1011 EXCEPTION_REGISTRATION_RECORD *frame, *dispatch;
1012 DWORD res;
1014 RtlCaptureContext( &context );
1015 context.R0 = (DWORD)retval;
1017 /* build an exception record, if we do not have one */
1018 if (!rec)
1020 record.ExceptionCode = STATUS_UNWIND;
1021 record.ExceptionFlags = 0;
1022 record.ExceptionRecord = NULL;
1023 record.ExceptionAddress = (void *)context.Pc;
1024 record.NumberParameters = 0;
1025 rec = &record;
1028 rec->ExceptionFlags |= EH_UNWINDING | (endframe ? 0 : EH_EXIT_UNWIND);
1030 TRACE( "code=%x flags=%x\n", rec->ExceptionCode, rec->ExceptionFlags );
1032 /* get chain of exception frames */
1033 frame = NtCurrentTeb()->Tib.ExceptionList;
1034 while ((frame != (EXCEPTION_REGISTRATION_RECORD*)~0UL) && (frame != endframe))
1036 /* Check frame address */
1037 if (endframe && ((void*)frame > endframe))
1038 raise_status( STATUS_INVALID_UNWIND_TARGET, rec );
1040 if (!is_valid_frame( frame )) raise_status( STATUS_BAD_STACK, rec );
1042 /* Call handler */
1043 TRACE( "calling handler at %p code=%x flags=%x\n",
1044 frame->Handler, rec->ExceptionCode, rec->ExceptionFlags );
1045 res = frame->Handler(rec, frame, &context, &dispatch);
1046 TRACE( "handler at %p returned %x\n", frame->Handler, res );
1048 switch(res)
1050 case ExceptionContinueSearch:
1051 break;
1052 case ExceptionCollidedUnwind:
1053 frame = dispatch;
1054 break;
1055 default:
1056 raise_status( STATUS_INVALID_DISPOSITION, rec );
1057 break;
1059 frame = __wine_pop_frame( frame );
1063 /*******************************************************************
1064 * NtRaiseException (NTDLL.@)
1066 NTSTATUS WINAPI NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context, BOOL first_chance )
1068 NTSTATUS status = raise_exception( rec, context, first_chance );
1069 if (status == STATUS_SUCCESS) NtSetContextThread( GetCurrentThread(), context );
1070 return status;
1073 /***********************************************************************
1074 * RtlRaiseException (NTDLL.@)
1076 void WINAPI RtlRaiseException( EXCEPTION_RECORD *rec )
1078 CONTEXT context;
1079 NTSTATUS status;
1081 RtlCaptureContext( &context );
1082 rec->ExceptionAddress = (LPVOID)context.Pc;
1083 status = raise_exception( rec, &context, TRUE );
1084 if (status) raise_status( status, rec );
1087 /*************************************************************************
1088 * RtlCaptureStackBackTrace (NTDLL.@)
1090 USHORT WINAPI RtlCaptureStackBackTrace( ULONG skip, ULONG count, PVOID *buffer, ULONG *hash )
1092 FIXME( "(%d, %d, %p, %p) stub!\n", skip, count, buffer, hash );
1093 return 0;
1096 /***********************************************************************
1097 * call_thread_entry_point
1099 void call_thread_entry_point( LPTHREAD_START_ROUTINE entry, void *arg )
1101 __TRY
1103 exit_thread( entry( arg ));
1105 __EXCEPT(unhandled_exception_filter)
1107 NtTerminateThread( GetCurrentThread(), GetExceptionCode() );
1109 __ENDTRY
1110 abort(); /* should not be reached */
1113 /***********************************************************************
1114 * RtlExitUserThread (NTDLL.@)
1116 void WINAPI RtlExitUserThread( ULONG status )
1118 exit_thread( status );
1121 /***********************************************************************
1122 * abort_thread
1124 void abort_thread( int status )
1126 terminate_thread( status );
1129 /**********************************************************************
1130 * DbgBreakPoint (NTDLL.@)
1132 void WINAPI DbgBreakPoint(void)
1134 kill(getpid(), SIGTRAP);
1137 /**********************************************************************
1138 * DbgUserBreakPoint (NTDLL.@)
1140 void WINAPI DbgUserBreakPoint(void)
1142 kill(getpid(), SIGTRAP);
1145 /**********************************************************************
1146 * NtCurrentTeb (NTDLL.@)
1148 TEB * WINAPI NtCurrentTeb(void)
1150 return pthread_getspecific( teb_key );
1153 #endif /* __arm__ */