wshom.ocx: Dump VARIANT parameters, fix instance leak.
[wine/multimedia.git] / dlls / ntdll / signal_arm.c
blob6f9b9bf7a567cde9de76a7365545aeb2433bb238
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 #include "ntstatus.h"
51 #define WIN32_NO_STATUS
52 #include "windef.h"
53 #include "winternl.h"
54 #include "wine/library.h"
55 #include "wine/exception.h"
56 #include "ntdll_misc.h"
57 #include "wine/debug.h"
58 #include "winnt.h"
60 WINE_DEFAULT_DEBUG_CHANNEL(seh);
62 static pthread_key_t teb_key;
64 /***********************************************************************
65 * signal context platform-specific definitions
67 #ifdef linux
69 typedef ucontext_t SIGCONTEXT;
71 /* All Registers access - only for local access */
72 # define REG_sig(reg_name, context) ((context)->uc_mcontext.reg_name)
73 # define REGn_sig(reg_num, context) ((context)->uc_mcontext.arm_r##reg_num)
75 /* Special Registers access */
76 # define SP_sig(context) REG_sig(arm_sp, context) /* Stack pointer */
77 # define LR_sig(context) REG_sig(arm_lr, context) /* Link register */
78 # define PC_sig(context) REG_sig(arm_pc, context) /* Program counter */
79 # define CPSR_sig(context) REG_sig(arm_cpsr, context) /* Current State Register */
80 # define IP_sig(context) REG_sig(arm_ip, context) /* Intra-Procedure-call scratch register */
81 # define FP_sig(context) REG_sig(arm_fp, context) /* Frame pointer */
83 /* Exceptions */
84 # define ERROR_sig(context) REG_sig(error_code, context)
85 # define FAULT_sig(context) REG_sig(fault_address, context)
86 # define TRAP_sig(context) REG_sig(trap_no, context)
88 #endif /* linux */
90 enum arm_trap_code
92 TRAP_ARM_UNKNOWN = -1, /* Unknown fault (TRAP_sig not defined) */
93 TRAP_ARM_PRIVINFLT = 6, /* Invalid opcode exception */
94 TRAP_ARM_PAGEFLT = 14, /* Page fault */
95 TRAP_ARM_ALIGNFLT = 17, /* Alignment check exception */
98 typedef void (WINAPI *raise_func)( EXCEPTION_RECORD *rec, CONTEXT *context );
99 typedef int (*wine_signal_handler)(unsigned int sig);
101 static wine_signal_handler handlers[256];
103 /***********************************************************************
104 * dispatch_signal
106 static inline int dispatch_signal(unsigned int sig)
108 if (handlers[sig] == NULL) return 0;
109 return handlers[sig](sig);
112 /*******************************************************************
113 * is_valid_frame
115 static inline BOOL is_valid_frame( void *frame )
117 if ((ULONG_PTR)frame & 3) return FALSE;
118 return (frame >= NtCurrentTeb()->Tib.StackLimit &&
119 (void **)frame < (void **)NtCurrentTeb()->Tib.StackBase - 1);
122 /***********************************************************************
123 * save_context
125 * Set the register values from a sigcontext.
127 static void save_context( CONTEXT *context, const ucontext_t *sigcontext )
129 #define C(x) context->R##x = REGn_sig(x,sigcontext)
130 /* Save normal registers */
131 C(0); C(1); C(2); C(3); C(4); C(5); C(6); C(7); C(8); C(9); C(10);
132 #undef C
134 context->ContextFlags = CONTEXT_FULL;
135 context->Sp = SP_sig(sigcontext); /* Stack pointer */
136 context->Lr = LR_sig(sigcontext); /* Link register */
137 context->Pc = PC_sig(sigcontext); /* Program Counter */
138 context->Cpsr = CPSR_sig(sigcontext); /* Current State Register */
139 context->Ip = IP_sig(sigcontext); /* Intra-Procedure-call scratch register */
140 context->Fp = FP_sig(sigcontext); /* Frame pointer */
144 /***********************************************************************
145 * restore_context
147 * Build a sigcontext from the register values.
149 static void restore_context( const CONTEXT *context, ucontext_t *sigcontext )
151 #define C(x) REGn_sig(x,sigcontext) = context->R##x
152 /* Restore normal registers */
153 C(0); C(1); C(2); C(3); C(4); C(5); C(6); C(7); C(8); C(9); C(10);
154 #undef C
156 SP_sig(sigcontext) = context->Sp; /* Stack pointer */
157 LR_sig(sigcontext) = context->Lr ; /* Link register */
158 PC_sig(sigcontext) = context->Pc; /* Program Counter */
159 CPSR_sig(sigcontext) = context->Cpsr; /* Current State Register */
160 IP_sig(sigcontext) = context->Ip; /* Intra-Procedure-call scratch register */
161 FP_sig(sigcontext) = context->Fp; /* Frame pointer */
165 /***********************************************************************
166 * save_fpu
168 * Set the FPU context from a sigcontext.
170 static inline void save_fpu( CONTEXT *context, const ucontext_t *sigcontext )
172 FIXME("not implemented\n");
176 /***********************************************************************
177 * restore_fpu
179 * Restore the FPU context to a sigcontext.
181 static inline void restore_fpu( CONTEXT *context, const ucontext_t *sigcontext )
183 FIXME("not implemented\n");
187 /***********************************************************************
188 * RtlCaptureContext (NTDLL.@)
190 /* FIXME: Use the Stack instead of the actual register values */
191 __ASM_STDCALL_FUNC( RtlCaptureContext, 4,
192 ".arm\n\t"
193 "stmfd SP!, {r1}\n\t"
194 "mov r1, #0x40\n\t" /* CONTEXT_ARM */
195 "add r1, r1, #0x3\n\t" /* CONTEXT_FULL */
196 "str r1, [r0]\n\t" /* context->ContextFlags */
197 "ldmfd SP!, {r1}\n\t"
198 "str r0, [r0, #0x4]\n\t" /* context->R0 */
199 "str r1, [r0, #0x8]\n\t" /* context->R1 */
200 "str r2, [r0, #0xc]\n\t" /* context->R2 */
201 "str r3, [r0, #0x10]\n\t" /* context->R3 */
202 "str r4, [r0, #0x14]\n\t" /* context->R4 */
203 "str r5, [r0, #0x18]\n\t" /* context->R5 */
204 "str r6, [r0, #0x1c]\n\t" /* context->R6 */
205 "str r7, [r0, #0x20]\n\t" /* context->R7 */
206 "str r8, [r0, #0x24]\n\t" /* context->R8 */
207 "str r9, [r0, #0x28]\n\t" /* context->R9 */
208 "str r10, [r0, #0x2c]\n\t" /* context->R10 */
209 "str r11, [r0, #0x30]\n\t" /* context->Fp */
210 "str IP, [r0, #0x34]\n\t" /* context->Ip */
211 "str SP, [r0, #0x38]\n\t" /* context->Sp */
212 "str LR, [r0, #0x3c]\n\t" /* context->Lr */
213 "str LR, [r0, #0x40]\n\t" /* context->Pc */
214 "mrs r1, CPSR\n\t"
215 "str r1, [r0, #0x44]\n\t" /* context->Cpsr */
216 "mov PC, LR\n"
220 /***********************************************************************
221 * set_cpu_context
223 * Set the new CPU context.
225 /* FIXME: What about the CPSR? */
226 __ASM_GLOBAL_FUNC( set_cpu_context,
227 "mov IP, r0\n\t"
228 "ldr r0, [IP, #0x4]\n\t" /* context->R0 */
229 "ldr r1, [IP, #0x8]\n\t" /* context->R1 */
230 "ldr r2, [IP, #0xc]\n\t" /* context->R2 */
231 "ldr r3, [IP, #0x10]\n\t" /* context->R3 */
232 "ldr r4, [IP, #0x14]\n\t" /* context->R4 */
233 "ldr r5, [IP, #0x18]\n\t" /* context->R5 */
234 "ldr r6, [IP, #0x1c]\n\t" /* context->R6 */
235 "ldr r7, [IP, #0x20]\n\t" /* context->R7 */
236 "ldr r8, [IP, #0x24]\n\t" /* context->R8 */
237 "ldr r9, [IP, #0x28]\n\t" /* context->R9 */
238 "ldr r10, [IP, #0x2c]\n\t" /* context->R10 */
239 "ldr r11, [IP, #0x30]\n\t" /* context->Fp */
240 "ldr SP, [IP, #0x38]\n\t" /* context->Sp */
241 "ldr LR, [IP, #0x3c]\n\t" /* context->Lr */
242 "ldr PC, [IP, #0x40]\n\t" /* context->Pc */
246 /***********************************************************************
247 * copy_context
249 * Copy a register context according to the flags.
251 void copy_context( CONTEXT *to, const CONTEXT *from, DWORD flags )
253 flags &= ~CONTEXT_ARM; /* get rid of CPU id */
254 if (flags & CONTEXT_CONTROL)
256 to->Sp = from->Sp;
257 to->Lr = from->Lr;
258 to->Pc = from->Pc;
259 to->Cpsr = from->Cpsr;
261 if (flags & CONTEXT_INTEGER)
263 to->R0 = from->R0;
264 to->R1 = from->R1;
265 to->R2 = from->R2;
266 to->R3 = from->R3;
267 to->R4 = from->R4;
268 to->R5 = from->R5;
269 to->R6 = from->R6;
270 to->R7 = from->R7;
271 to->R8 = from->R8;
272 to->R9 = from->R9;
273 to->R10 = from->R10;
274 to->Ip = from->Ip;
275 to->Fp = from->Fp;
280 /***********************************************************************
281 * context_to_server
283 * Convert a register context to the server format.
285 NTSTATUS context_to_server( context_t *to, const CONTEXT *from )
287 DWORD flags = from->ContextFlags & ~CONTEXT_ARM; /* get rid of CPU id */
289 memset( to, 0, sizeof(*to) );
290 to->cpu = CPU_ARM;
292 if (flags & CONTEXT_CONTROL)
294 to->flags |= SERVER_CTX_CONTROL;
295 to->ctl.arm_regs.sp = from->Sp;
296 to->ctl.arm_regs.lr = from->Lr;
297 to->ctl.arm_regs.pc = from->Pc;
298 to->ctl.arm_regs.cpsr = from->Cpsr;
300 if (flags & CONTEXT_INTEGER)
302 to->flags |= SERVER_CTX_INTEGER;
303 to->integer.arm_regs.r[0] = from->R0;
304 to->integer.arm_regs.r[1] = from->R1;
305 to->integer.arm_regs.r[2] = from->R2;
306 to->integer.arm_regs.r[3] = from->R3;
307 to->integer.arm_regs.r[4] = from->R4;
308 to->integer.arm_regs.r[5] = from->R5;
309 to->integer.arm_regs.r[6] = from->R6;
310 to->integer.arm_regs.r[7] = from->R7;
311 to->integer.arm_regs.r[8] = from->R8;
312 to->integer.arm_regs.r[9] = from->R9;
313 to->integer.arm_regs.r[10] = from->R10;
314 to->integer.arm_regs.r[11] = from->Fp;
315 to->integer.arm_regs.r[12] = from->Ip;
317 return STATUS_SUCCESS;
321 /***********************************************************************
322 * context_from_server
324 * Convert a register context from the server format.
326 NTSTATUS context_from_server( CONTEXT *to, const context_t *from )
328 if (from->cpu != CPU_ARM) return STATUS_INVALID_PARAMETER;
330 to->ContextFlags = CONTEXT_ARM;
331 if (from->flags & SERVER_CTX_CONTROL)
333 to->ContextFlags |= CONTEXT_CONTROL;
334 to->Sp = from->ctl.arm_regs.sp;
335 to->Lr = from->ctl.arm_regs.lr;
336 to->Pc = from->ctl.arm_regs.pc;
337 to->Cpsr = from->ctl.arm_regs.cpsr;
339 if (from->flags & SERVER_CTX_INTEGER)
341 to->ContextFlags |= CONTEXT_INTEGER;
342 to->R0 = from->integer.arm_regs.r[0];
343 to->R1 = from->integer.arm_regs.r[1];
344 to->R2 = from->integer.arm_regs.r[2];
345 to->R3 = from->integer.arm_regs.r[3];
346 to->R4 = from->integer.arm_regs.r[4];
347 to->R5 = from->integer.arm_regs.r[5];
348 to->R6 = from->integer.arm_regs.r[6];
349 to->R7 = from->integer.arm_regs.r[7];
350 to->R8 = from->integer.arm_regs.r[8];
351 to->R9 = from->integer.arm_regs.r[9];
352 to->R10 = from->integer.arm_regs.r[10];
353 to->Fp = from->integer.arm_regs.r[11];
354 to->Ip = from->integer.arm_regs.r[12];
356 return STATUS_SUCCESS;
359 /***********************************************************************
360 * setup_exception_record
362 * Setup the exception record and context on the thread stack.
364 static EXCEPTION_RECORD *setup_exception_record( SIGCONTEXT *sigcontext, void *stack_ptr, raise_func func )
366 struct stack_layout
368 CONTEXT context;
369 EXCEPTION_RECORD rec;
370 } *stack = stack_ptr;
371 DWORD exception_code = 0;
373 stack--; /* push the stack_layout structure */
375 stack->rec.ExceptionRecord = NULL;
376 stack->rec.ExceptionCode = exception_code;
377 stack->rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
378 stack->rec.ExceptionAddress = (LPVOID)PC_sig(sigcontext);
379 stack->rec.NumberParameters = 0;
381 save_context( &stack->context, sigcontext );
383 /* now modify the sigcontext to return to the raise function */
384 SP_sig(sigcontext) = (DWORD)stack;
385 PC_sig(sigcontext) = (DWORD)func;
386 REGn_sig(0, sigcontext) = (DWORD)&stack->rec; /* first arg for raise_func */
387 REGn_sig(1, sigcontext) = (DWORD)&stack->context; /* second arg for raise_func */
390 return &stack->rec;
393 /**********************************************************************
394 * raise_segv_exception
396 static void WINAPI raise_segv_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
398 NTSTATUS status;
400 switch(rec->ExceptionCode)
402 case EXCEPTION_ACCESS_VIOLATION:
403 if (rec->NumberParameters == 2)
405 if (!(rec->ExceptionCode = virtual_handle_fault( (void *)rec->ExceptionInformation[1],
406 rec->ExceptionInformation[0] )))
407 goto done;
409 break;
411 status = NtRaiseException( rec, context, TRUE );
412 if (status) raise_status( status, rec );
413 done:
414 set_cpu_context( context );
417 /**********************************************************************
418 * call_stack_handlers
420 * Call the stack handlers chain.
422 static NTSTATUS call_stack_handlers( EXCEPTION_RECORD *rec, CONTEXT *context )
424 EXCEPTION_REGISTRATION_RECORD *frame, *dispatch, *nested_frame;
425 DWORD res;
427 frame = NtCurrentTeb()->Tib.ExceptionList;
428 nested_frame = NULL;
429 while (frame != (EXCEPTION_REGISTRATION_RECORD*)~0UL)
431 /* Check frame address */
432 if (!is_valid_frame( frame ))
434 rec->ExceptionFlags |= EH_STACK_INVALID;
435 break;
438 /* Call handler */
439 TRACE( "calling handler at %p code=%x flags=%x\n",
440 frame->Handler, rec->ExceptionCode, rec->ExceptionFlags );
441 res = frame->Handler( rec, frame, context, &dispatch );
442 TRACE( "handler at %p returned %x\n", frame->Handler, res );
444 if (frame == nested_frame)
446 /* no longer nested */
447 nested_frame = NULL;
448 rec->ExceptionFlags &= ~EH_NESTED_CALL;
451 switch(res)
453 case ExceptionContinueExecution:
454 if (!(rec->ExceptionFlags & EH_NONCONTINUABLE)) return STATUS_SUCCESS;
455 return STATUS_NONCONTINUABLE_EXCEPTION;
456 case ExceptionContinueSearch:
457 break;
458 case ExceptionNestedException:
459 if (nested_frame < dispatch) nested_frame = dispatch;
460 rec->ExceptionFlags |= EH_NESTED_CALL;
461 break;
462 default:
463 return STATUS_INVALID_DISPOSITION;
465 frame = frame->Prev;
467 return STATUS_UNHANDLED_EXCEPTION;
471 /*******************************************************************
472 * raise_exception
474 * Implementation of NtRaiseException.
476 static NTSTATUS raise_exception( EXCEPTION_RECORD *rec, CONTEXT *context, BOOL first_chance )
478 NTSTATUS status;
480 if (first_chance)
482 DWORD c;
484 for (c = 0; c < rec->NumberParameters; c++)
485 TRACE( " info[%d]=%08lx\n", c, rec->ExceptionInformation[c] );
486 if (rec->ExceptionCode == EXCEPTION_WINE_STUB)
488 if (rec->ExceptionInformation[1] >> 16)
489 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
490 rec->ExceptionAddress,
491 (char*)rec->ExceptionInformation[0], (char*)rec->ExceptionInformation[1] );
492 else
493 MESSAGE( "wine: Call from %p to unimplemented function %s.%ld, aborting\n",
494 rec->ExceptionAddress,
495 (char*)rec->ExceptionInformation[0], rec->ExceptionInformation[1] );
497 else
499 TRACE(" Pc:%04x Sp:%04x Lr:%04x Cpsr:%04x r0:%04x r1:%04x r2:%04x r3:%04x\n",
500 context->Pc, context->Sp, context->Lr, context->Cpsr,
501 context->R0, context->R1, context->R2, context->R3);
502 TRACE(" r4:%04x r5:%04x r6:%04x r7:%04x r8:%04x r9:%04x r10:%04x Fp:%04x Ip:%04x\n",
503 context->R4, context->R5, context->R6, context->R7, context->R8,
504 context->R9, context->R10, context->Fp, context->Ip );
507 status = send_debug_event( rec, TRUE, context );
508 if (status == DBG_CONTINUE || status == DBG_EXCEPTION_HANDLED)
509 return STATUS_SUCCESS;
511 if (call_vectored_handlers( rec, context ) == EXCEPTION_CONTINUE_EXECUTION)
512 return STATUS_SUCCESS;
514 if ((status = call_stack_handlers( rec, context )) != STATUS_UNHANDLED_EXCEPTION)
515 return status;
518 /* last chance exception */
520 status = send_debug_event( rec, FALSE, context );
521 if (status != DBG_CONTINUE)
523 if (rec->ExceptionFlags & EH_STACK_INVALID)
524 ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
525 else if (rec->ExceptionCode == STATUS_NONCONTINUABLE_EXCEPTION)
526 ERR("Process attempted to continue execution after noncontinuable exception.\n");
527 else
528 ERR("Unhandled exception code %x flags %x addr %p\n",
529 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
530 NtTerminateProcess( NtCurrentProcess(), rec->ExceptionCode );
532 return STATUS_SUCCESS;
536 /**********************************************************************
537 * segv_handler
539 * Handler for SIGSEGV and related errors.
541 static void segv_handler( int signal, siginfo_t *info, void *ucontext )
543 EXCEPTION_RECORD *rec;
544 SIGCONTEXT *context = ucontext;
545 void *stack = (void *) (SP_sig(context) & ~3);
547 /* check for page fault inside the thread stack */
548 if (TRAP_sig(context) == TRAP_ARM_PAGEFLT &&
549 (char *)info->si_addr >= (char *)NtCurrentTeb()->DeallocationStack &&
550 (char *)info->si_addr < (char *)NtCurrentTeb()->Tib.StackBase &&
551 virtual_handle_stack_fault( info->si_addr ))
553 /* check if this was the last guard page */
554 if ((char *)info->si_addr < (char *)NtCurrentTeb()->DeallocationStack + 2*4096)
556 rec = setup_exception_record( context, stack, raise_segv_exception );
557 rec->ExceptionCode = EXCEPTION_STACK_OVERFLOW;
559 return;
562 rec = setup_exception_record( context, stack, raise_segv_exception );
563 if (rec->ExceptionCode == EXCEPTION_STACK_OVERFLOW) return;
565 switch(TRAP_sig(context))
567 case TRAP_ARM_PRIVINFLT: /* Invalid opcode exception */
568 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
569 break;
570 case TRAP_ARM_PAGEFLT: /* Page fault */
571 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
572 rec->NumberParameters = 2;
573 rec->ExceptionInformation[0] = (ERROR_sig(context) & 0x800) != 0;
574 rec->ExceptionInformation[1] = (ULONG_PTR)info->si_addr;
575 break;
576 case TRAP_ARM_ALIGNFLT: /* Alignment check exception */
577 rec->ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
578 break;
579 default:
580 WINE_ERR( "Got unexpected trap %ld\n", TRAP_sig(context) );
581 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
582 break;
586 /**********************************************************************
587 * trap_handler
589 * Handler for SIGTRAP.
591 static void trap_handler( int signal, siginfo_t *info, void *ucontext )
593 EXCEPTION_RECORD rec;
594 CONTEXT context;
595 NTSTATUS status;
597 switch ( info->si_code )
599 case TRAP_TRACE:
600 rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
601 break;
602 case TRAP_BRKPT:
603 default:
604 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
605 break;
608 save_context( &context, ucontext );
609 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
610 rec.ExceptionRecord = NULL;
611 rec.ExceptionAddress = (LPVOID)context.Pc;
612 rec.NumberParameters = 0;
613 status = raise_exception( &rec, &context, TRUE );
614 if (status) raise_status( status, &rec );
615 restore_context( &context, ucontext );
618 /**********************************************************************
619 * fpe_handler
621 * Handler for SIGFPE.
623 static void fpe_handler( int signal, siginfo_t *siginfo, void *sigcontext )
625 EXCEPTION_RECORD rec;
626 CONTEXT context;
627 NTSTATUS status;
629 save_fpu( &context, sigcontext );
630 save_context( &context, sigcontext );
632 switch (siginfo->si_code & 0xffff )
634 #ifdef FPE_FLTSUB
635 case FPE_FLTSUB:
636 rec.ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
637 break;
638 #endif
639 #ifdef FPE_INTDIV
640 case FPE_INTDIV:
641 rec.ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
642 break;
643 #endif
644 #ifdef FPE_INTOVF
645 case FPE_INTOVF:
646 rec.ExceptionCode = EXCEPTION_INT_OVERFLOW;
647 break;
648 #endif
649 #ifdef FPE_FLTDIV
650 case FPE_FLTDIV:
651 rec.ExceptionCode = EXCEPTION_FLT_DIVIDE_BY_ZERO;
652 break;
653 #endif
654 #ifdef FPE_FLTOVF
655 case FPE_FLTOVF:
656 rec.ExceptionCode = EXCEPTION_FLT_OVERFLOW;
657 break;
658 #endif
659 #ifdef FPE_FLTUND
660 case FPE_FLTUND:
661 rec.ExceptionCode = EXCEPTION_FLT_UNDERFLOW;
662 break;
663 #endif
664 #ifdef FPE_FLTRES
665 case FPE_FLTRES:
666 rec.ExceptionCode = EXCEPTION_FLT_INEXACT_RESULT;
667 break;
668 #endif
669 #ifdef FPE_FLTINV
670 case FPE_FLTINV:
671 #endif
672 default:
673 rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
674 break;
676 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
677 rec.ExceptionRecord = NULL;
678 rec.ExceptionAddress = (LPVOID)context.Pc;
679 rec.NumberParameters = 0;
680 status = raise_exception( &rec, &context, TRUE );
681 if (status) raise_status( status, &rec );
683 restore_context( &context, sigcontext );
684 restore_fpu( &context, sigcontext );
687 /**********************************************************************
688 * int_handler
690 * Handler for SIGINT.
692 static void int_handler( int signal, siginfo_t *siginfo, void *sigcontext )
694 if (!dispatch_signal(SIGINT))
696 EXCEPTION_RECORD rec;
697 CONTEXT context;
698 NTSTATUS status;
700 save_context( &context, sigcontext );
701 rec.ExceptionCode = CONTROL_C_EXIT;
702 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
703 rec.ExceptionRecord = NULL;
704 rec.ExceptionAddress = (LPVOID)context.Pc;
705 rec.NumberParameters = 0;
706 status = raise_exception( &rec, &context, TRUE );
707 if (status) raise_status( status, &rec );
708 restore_context( &context, sigcontext );
713 /**********************************************************************
714 * abrt_handler
716 * Handler for SIGABRT.
718 static void abrt_handler( int signal, siginfo_t *siginfo, void *sigcontext )
720 EXCEPTION_RECORD rec;
721 CONTEXT context;
722 NTSTATUS status;
724 save_context( &context, sigcontext );
725 rec.ExceptionCode = EXCEPTION_WINE_ASSERTION;
726 rec.ExceptionFlags = EH_NONCONTINUABLE;
727 rec.ExceptionRecord = NULL;
728 rec.ExceptionAddress = (LPVOID)context.Pc;
729 rec.NumberParameters = 0;
730 status = raise_exception( &rec, &context, TRUE );
731 if (status) raise_status( status, &rec );
732 restore_context( &context, sigcontext );
736 /**********************************************************************
737 * quit_handler
739 * Handler for SIGQUIT.
741 static void quit_handler( int signal, siginfo_t *siginfo, void *sigcontext )
743 abort_thread(0);
747 /**********************************************************************
748 * usr1_handler
750 * Handler for SIGUSR1, used to signal a thread that it got suspended.
752 static void usr1_handler( int signal, siginfo_t *siginfo, void *sigcontext )
754 CONTEXT context;
756 save_context( &context, sigcontext );
757 wait_suspend( &context );
758 restore_context( &context, sigcontext );
762 /***********************************************************************
763 * __wine_set_signal_handler (NTDLL.@)
765 int CDECL __wine_set_signal_handler(unsigned int sig, wine_signal_handler wsh)
767 if (sig > sizeof(handlers) / sizeof(handlers[0])) return -1;
768 if (handlers[sig] != NULL) return -2;
769 handlers[sig] = wsh;
770 return 0;
774 /**********************************************************************
775 * signal_alloc_thread
777 NTSTATUS signal_alloc_thread( TEB **teb )
779 static size_t sigstack_zero_bits;
780 SIZE_T size;
781 NTSTATUS status;
783 if (!sigstack_zero_bits)
785 size_t min_size = getpagesize(); /* this is just for the TEB, we don't use a signal stack yet */
786 /* find the first power of two not smaller than min_size */
787 while ((1u << sigstack_zero_bits) < min_size) sigstack_zero_bits++;
788 assert( sizeof(TEB) <= min_size );
791 size = 1 << sigstack_zero_bits;
792 *teb = NULL;
793 if (!(status = NtAllocateVirtualMemory( NtCurrentProcess(), (void **)teb, sigstack_zero_bits,
794 &size, MEM_COMMIT | MEM_TOP_DOWN, PAGE_READWRITE )))
796 (*teb)->Tib.Self = &(*teb)->Tib;
797 (*teb)->Tib.ExceptionList = (void *)~0UL;
799 return status;
803 /**********************************************************************
804 * signal_free_thread
806 void signal_free_thread( TEB *teb )
808 SIZE_T size;
810 if (teb->DeallocationStack)
812 size = 0;
813 NtFreeVirtualMemory( GetCurrentProcess(), &teb->DeallocationStack, &size, MEM_RELEASE );
815 size = 0;
816 NtFreeVirtualMemory( NtCurrentProcess(), (void **)&teb, &size, MEM_RELEASE );
820 /**********************************************************************
821 * signal_init_thread
823 void signal_init_thread( TEB *teb )
825 static int init_done;
827 if (!init_done)
829 pthread_key_create( &teb_key, NULL );
830 init_done = 1;
832 pthread_setspecific( teb_key, teb );
836 /**********************************************************************
837 * signal_init_process
839 void signal_init_process(void)
841 struct sigaction sig_act;
843 sig_act.sa_mask = server_block_set;
844 sig_act.sa_flags = SA_RESTART | SA_SIGINFO;
846 sig_act.sa_sigaction = int_handler;
847 if (sigaction( SIGINT, &sig_act, NULL ) == -1) goto error;
848 sig_act.sa_sigaction = fpe_handler;
849 if (sigaction( SIGFPE, &sig_act, NULL ) == -1) goto error;
850 sig_act.sa_sigaction = abrt_handler;
851 if (sigaction( SIGABRT, &sig_act, NULL ) == -1) goto error;
852 sig_act.sa_sigaction = quit_handler;
853 if (sigaction( SIGQUIT, &sig_act, NULL ) == -1) goto error;
854 sig_act.sa_sigaction = usr1_handler;
855 if (sigaction( SIGUSR1, &sig_act, NULL ) == -1) goto error;
857 sig_act.sa_sigaction = segv_handler;
858 if (sigaction( SIGSEGV, &sig_act, NULL ) == -1) goto error;
859 if (sigaction( SIGILL, &sig_act, NULL ) == -1) goto error;
860 #ifdef SIGBUS
861 if (sigaction( SIGBUS, &sig_act, NULL ) == -1) goto error;
862 #endif
864 #ifdef SIGTRAP
865 sig_act.sa_sigaction = trap_handler;
866 if (sigaction( SIGTRAP, &sig_act, NULL ) == -1) goto error;
867 #endif
868 return;
870 error:
871 perror("sigaction");
872 exit(1);
876 /**********************************************************************
877 * __wine_enter_vm86 (NTDLL.@)
879 void __wine_enter_vm86( CONTEXT *context )
881 MESSAGE("vm86 mode not supported on this platform\n");
884 /***********************************************************************
885 * RtlUnwind (NTDLL.@)
887 void WINAPI RtlUnwind( PVOID pEndFrame, PVOID targetIp, PEXCEPTION_RECORD pRecord, PVOID retval )
889 FIXME( "Not implemented on ARM\n" );
892 /*******************************************************************
893 * NtRaiseException (NTDLL.@)
895 NTSTATUS WINAPI NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context, BOOL first_chance )
897 NTSTATUS status = raise_exception( rec, context, first_chance );
898 if (status == STATUS_SUCCESS) NtSetContextThread( GetCurrentThread(), context );
899 return status;
902 /***********************************************************************
903 * RtlRaiseException (NTDLL.@)
905 void WINAPI RtlRaiseException( EXCEPTION_RECORD *rec )
907 CONTEXT context;
908 NTSTATUS status;
910 RtlCaptureContext( &context );
911 rec->ExceptionAddress = (LPVOID)context.Pc;
912 status = raise_exception( rec, &context, TRUE );
913 if (status) raise_status( status, rec );
916 /*************************************************************************
917 * RtlCaptureStackBackTrace (NTDLL.@)
919 USHORT WINAPI RtlCaptureStackBackTrace( ULONG skip, ULONG count, PVOID *buffer, ULONG *hash )
921 FIXME( "(%d, %d, %p, %p) stub!\n", skip, count, buffer, hash );
922 return 0;
925 /***********************************************************************
926 * call_thread_entry_point
928 void call_thread_entry_point( LPTHREAD_START_ROUTINE entry, void *arg )
930 __TRY
932 exit_thread( entry( arg ));
934 __EXCEPT(unhandled_exception_filter)
936 NtTerminateThread( GetCurrentThread(), GetExceptionCode() );
938 __ENDTRY
939 abort(); /* should not be reached */
942 /***********************************************************************
943 * RtlExitUserThread (NTDLL.@)
945 void WINAPI RtlExitUserThread( ULONG status )
947 exit_thread( status );
950 /***********************************************************************
951 * abort_thread
953 void abort_thread( int status )
955 terminate_thread( status );
958 /**********************************************************************
959 * DbgBreakPoint (NTDLL.@)
961 void WINAPI DbgBreakPoint(void)
963 kill(getpid(), SIGTRAP);
966 /**********************************************************************
967 * DbgUserBreakPoint (NTDLL.@)
969 void WINAPI DbgUserBreakPoint(void)
971 kill(getpid(), SIGTRAP);
974 /**********************************************************************
975 * NtCurrentTeb (NTDLL.@)
977 TEB * WINAPI NtCurrentTeb(void)
979 return pthread_getspecific( teb_key );
982 #endif /* __arm__ */