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
25 #include "wine/port.h"
36 #ifdef HAVE_SYS_PARAM_H
37 # include <sys/param.h>
42 # ifdef HAVE_SYS_SYSCALL_H
43 # include <sys/syscall.h>
46 #ifdef HAVE_SYS_SIGNAL_H
47 # include <sys/signal.h>
51 #define WIN32_NO_STATUS
54 #include "wine/library.h"
55 #include "wine/exception.h"
56 #include "ntdll_misc.h"
57 #include "wine/debug.h"
60 WINE_DEFAULT_DEBUG_CHANNEL(seh);
62 static pthread_key_t teb_key;
64 /***********************************************************************
65 * signal context platform-specific definitions
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 */
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)
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 /***********************************************************************
106 static inline int dispatch_signal(unsigned int sig)
108 if (handlers[sig] == NULL) return 0;
109 return handlers[sig](sig);
112 /*******************************************************************
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 /***********************************************************************
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);
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 /***********************************************************************
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);
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 /***********************************************************************
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 /***********************************************************************
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,
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 */
215 "str r1, [r0, #0x44]\n\t" /* context->Cpsr */
220 /***********************************************************************
223 * Set the new CPU context.
225 /* FIXME: What about the CPSR? */
226 __ASM_GLOBAL_FUNC( set_cpu_context,
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 /***********************************************************************
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)
259 to->Cpsr = from->Cpsr;
261 if (flags & CONTEXT_INTEGER)
280 /***********************************************************************
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) );
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 extern void raise_func_trampoline_thumb( EXCEPTION_RECORD *rec, CONTEXT *context, raise_func func );
360 __ASM_GLOBAL_FUNC( raise_func_trampoline_thumb,
365 extern void raise_func_trampoline_arm( EXCEPTION_RECORD *rec, CONTEXT *context, raise_func func );
366 __ASM_GLOBAL_FUNC( raise_func_trampoline_arm,
371 /***********************************************************************
372 * setup_exception_record
374 * Setup the exception record and context on the thread stack.
376 static EXCEPTION_RECORD *setup_exception( SIGCONTEXT *sigcontext, raise_func func )
381 EXCEPTION_RECORD rec;
383 DWORD exception_code = 0;
385 stack = (struct stack_layout *)(SP_sig(sigcontext) & ~3);
386 stack--; /* push the stack_layout structure */
388 stack->rec.ExceptionRecord = NULL;
389 stack->rec.ExceptionCode = exception_code;
390 stack->rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
391 stack->rec.ExceptionAddress = (LPVOID)PC_sig(sigcontext);
392 stack->rec.NumberParameters = 0;
394 save_context( &stack->context, sigcontext );
396 /* now modify the sigcontext to return to the raise function */
397 SP_sig(sigcontext) = (DWORD)stack;
398 if (CPSR_sig(sigcontext) & 0x20)
399 PC_sig(sigcontext) = (DWORD)raise_func_trampoline_thumb;
401 PC_sig(sigcontext) = (DWORD)raise_func_trampoline_arm;
402 REGn_sig(0, sigcontext) = (DWORD)&stack->rec; /* first arg for raise_func */
403 REGn_sig(1, sigcontext) = (DWORD)&stack->context; /* second arg for raise_func */
404 REGn_sig(2, sigcontext) = (DWORD)func; /* the raise_func as third arg for the trampoline */
410 /**********************************************************************
411 * raise_segv_exception
413 static void WINAPI raise_segv_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
417 switch(rec->ExceptionCode)
419 case EXCEPTION_ACCESS_VIOLATION:
420 if (rec->NumberParameters == 2)
422 if (!(rec->ExceptionCode = virtual_handle_fault( (void *)rec->ExceptionInformation[1],
423 rec->ExceptionInformation[0] )))
428 status = NtRaiseException( rec, context, TRUE );
429 if (status) raise_status( status, rec );
431 set_cpu_context( context );
434 /**********************************************************************
435 * call_stack_handlers
437 * Call the stack handlers chain.
439 static NTSTATUS call_stack_handlers( EXCEPTION_RECORD *rec, CONTEXT *context )
441 EXCEPTION_REGISTRATION_RECORD *frame, *dispatch, *nested_frame;
444 frame = NtCurrentTeb()->Tib.ExceptionList;
446 while (frame != (EXCEPTION_REGISTRATION_RECORD*)~0UL)
448 /* Check frame address */
449 if (!is_valid_frame( frame ))
451 rec->ExceptionFlags |= EH_STACK_INVALID;
456 TRACE( "calling handler at %p code=%x flags=%x\n",
457 frame->Handler, rec->ExceptionCode, rec->ExceptionFlags );
458 res = frame->Handler( rec, frame, context, &dispatch );
459 TRACE( "handler at %p returned %x\n", frame->Handler, res );
461 if (frame == nested_frame)
463 /* no longer nested */
465 rec->ExceptionFlags &= ~EH_NESTED_CALL;
470 case ExceptionContinueExecution:
471 if (!(rec->ExceptionFlags & EH_NONCONTINUABLE)) return STATUS_SUCCESS;
472 return STATUS_NONCONTINUABLE_EXCEPTION;
473 case ExceptionContinueSearch:
475 case ExceptionNestedException:
476 if (nested_frame < dispatch) nested_frame = dispatch;
477 rec->ExceptionFlags |= EH_NESTED_CALL;
480 return STATUS_INVALID_DISPOSITION;
484 return STATUS_UNHANDLED_EXCEPTION;
488 /*******************************************************************
491 * Implementation of NtRaiseException.
493 static NTSTATUS raise_exception( EXCEPTION_RECORD *rec, CONTEXT *context, BOOL first_chance )
501 for (c = 0; c < rec->NumberParameters; c++)
502 TRACE( " info[%d]=%08lx\n", c, rec->ExceptionInformation[c] );
503 if (rec->ExceptionCode == EXCEPTION_WINE_STUB)
505 if (rec->ExceptionInformation[1] >> 16)
506 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
507 rec->ExceptionAddress,
508 (char*)rec->ExceptionInformation[0], (char*)rec->ExceptionInformation[1] );
510 MESSAGE( "wine: Call from %p to unimplemented function %s.%ld, aborting\n",
511 rec->ExceptionAddress,
512 (char*)rec->ExceptionInformation[0], rec->ExceptionInformation[1] );
516 TRACE(" Pc:%04x Sp:%04x Lr:%04x Cpsr:%04x r0:%04x r1:%04x r2:%04x r3:%04x\n",
517 context->Pc, context->Sp, context->Lr, context->Cpsr,
518 context->R0, context->R1, context->R2, context->R3);
519 TRACE(" r4:%04x r5:%04x r6:%04x r7:%04x r8:%04x r9:%04x r10:%04x Fp:%04x Ip:%04x\n",
520 context->R4, context->R5, context->R6, context->R7, context->R8,
521 context->R9, context->R10, context->Fp, context->Ip );
524 status = send_debug_event( rec, TRUE, context );
525 if (status == DBG_CONTINUE || status == DBG_EXCEPTION_HANDLED)
526 return STATUS_SUCCESS;
528 if (call_vectored_handlers( rec, context ) == EXCEPTION_CONTINUE_EXECUTION)
529 return STATUS_SUCCESS;
531 if ((status = call_stack_handlers( rec, context )) != STATUS_UNHANDLED_EXCEPTION)
535 /* last chance exception */
537 status = send_debug_event( rec, FALSE, context );
538 if (status != DBG_CONTINUE)
540 if (rec->ExceptionFlags & EH_STACK_INVALID)
541 ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
542 else if (rec->ExceptionCode == STATUS_NONCONTINUABLE_EXCEPTION)
543 ERR("Process attempted to continue execution after noncontinuable exception.\n");
545 ERR("Unhandled exception code %x flags %x addr %p\n",
546 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
547 NtTerminateProcess( NtCurrentProcess(), rec->ExceptionCode );
549 return STATUS_SUCCESS;
553 /**********************************************************************
556 * Handler for SIGSEGV and related errors.
558 static void segv_handler( int signal, siginfo_t *info, void *ucontext )
560 EXCEPTION_RECORD *rec;
561 SIGCONTEXT *context = ucontext;
563 /* check for page fault inside the thread stack */
564 if (TRAP_sig(context) == TRAP_ARM_PAGEFLT &&
565 (char *)info->si_addr >= (char *)NtCurrentTeb()->DeallocationStack &&
566 (char *)info->si_addr < (char *)NtCurrentTeb()->Tib.StackBase &&
567 virtual_handle_stack_fault( info->si_addr ))
569 /* check if this was the last guard page */
570 if ((char *)info->si_addr < (char *)NtCurrentTeb()->DeallocationStack + 2*4096)
572 rec = setup_exception( context, raise_segv_exception );
573 rec->ExceptionCode = EXCEPTION_STACK_OVERFLOW;
578 rec = setup_exception( context, raise_segv_exception );
579 if (rec->ExceptionCode == EXCEPTION_STACK_OVERFLOW) return;
581 switch(TRAP_sig(context))
583 case TRAP_ARM_PRIVINFLT: /* Invalid opcode exception */
584 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
586 case TRAP_ARM_PAGEFLT: /* Page fault */
587 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
588 rec->NumberParameters = 2;
589 rec->ExceptionInformation[0] = (ERROR_sig(context) & 0x800) != 0;
590 rec->ExceptionInformation[1] = (ULONG_PTR)info->si_addr;
592 case TRAP_ARM_ALIGNFLT: /* Alignment check exception */
593 rec->ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
596 ERR("Got unexpected trap %ld\n", TRAP_sig(context));
597 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
602 /**********************************************************************
605 * Handler for SIGTRAP.
607 static void trap_handler( int signal, siginfo_t *info, void *ucontext )
609 EXCEPTION_RECORD rec;
613 switch ( info->si_code )
616 rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
620 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
624 save_context( &context, ucontext );
625 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
626 rec.ExceptionRecord = NULL;
627 rec.ExceptionAddress = (LPVOID)context.Pc;
628 rec.NumberParameters = 0;
629 status = raise_exception( &rec, &context, TRUE );
630 if (status) raise_status( status, &rec );
631 restore_context( &context, ucontext );
634 /**********************************************************************
637 * Handler for SIGFPE.
639 static void fpe_handler( int signal, siginfo_t *siginfo, void *sigcontext )
641 EXCEPTION_RECORD rec;
645 save_fpu( &context, sigcontext );
646 save_context( &context, sigcontext );
648 switch (siginfo->si_code & 0xffff )
652 rec.ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
657 rec.ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
662 rec.ExceptionCode = EXCEPTION_INT_OVERFLOW;
667 rec.ExceptionCode = EXCEPTION_FLT_DIVIDE_BY_ZERO;
672 rec.ExceptionCode = EXCEPTION_FLT_OVERFLOW;
677 rec.ExceptionCode = EXCEPTION_FLT_UNDERFLOW;
682 rec.ExceptionCode = EXCEPTION_FLT_INEXACT_RESULT;
689 rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
692 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
693 rec.ExceptionRecord = NULL;
694 rec.ExceptionAddress = (LPVOID)context.Pc;
695 rec.NumberParameters = 0;
696 status = raise_exception( &rec, &context, TRUE );
697 if (status) raise_status( status, &rec );
699 restore_context( &context, sigcontext );
700 restore_fpu( &context, sigcontext );
703 /**********************************************************************
706 * Handler for SIGINT.
708 static void int_handler( int signal, siginfo_t *siginfo, void *sigcontext )
710 if (!dispatch_signal(SIGINT))
712 EXCEPTION_RECORD rec;
716 save_context( &context, sigcontext );
717 rec.ExceptionCode = CONTROL_C_EXIT;
718 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
719 rec.ExceptionRecord = NULL;
720 rec.ExceptionAddress = (LPVOID)context.Pc;
721 rec.NumberParameters = 0;
722 status = raise_exception( &rec, &context, TRUE );
723 if (status) raise_status( status, &rec );
724 restore_context( &context, sigcontext );
729 /**********************************************************************
732 * Handler for SIGABRT.
734 static void abrt_handler( int signal, siginfo_t *siginfo, void *sigcontext )
736 EXCEPTION_RECORD rec;
740 save_context( &context, sigcontext );
741 rec.ExceptionCode = EXCEPTION_WINE_ASSERTION;
742 rec.ExceptionFlags = EH_NONCONTINUABLE;
743 rec.ExceptionRecord = NULL;
744 rec.ExceptionAddress = (LPVOID)context.Pc;
745 rec.NumberParameters = 0;
746 status = raise_exception( &rec, &context, TRUE );
747 if (status) raise_status( status, &rec );
748 restore_context( &context, sigcontext );
752 /**********************************************************************
755 * Handler for SIGQUIT.
757 static void quit_handler( int signal, siginfo_t *siginfo, void *sigcontext )
763 /**********************************************************************
766 * Handler for SIGUSR1, used to signal a thread that it got suspended.
768 static void usr1_handler( int signal, siginfo_t *siginfo, void *sigcontext )
772 save_context( &context, sigcontext );
773 wait_suspend( &context );
774 restore_context( &context, sigcontext );
778 /***********************************************************************
779 * __wine_set_signal_handler (NTDLL.@)
781 int CDECL __wine_set_signal_handler(unsigned int sig, wine_signal_handler wsh)
783 if (sig > sizeof(handlers) / sizeof(handlers[0])) return -1;
784 if (handlers[sig] != NULL) return -2;
790 /**********************************************************************
791 * signal_alloc_thread
793 NTSTATUS signal_alloc_thread( TEB **teb )
795 static size_t sigstack_zero_bits;
799 if (!sigstack_zero_bits)
801 size_t min_size = getpagesize(); /* this is just for the TEB, we don't use a signal stack yet */
802 /* find the first power of two not smaller than min_size */
803 while ((1u << sigstack_zero_bits) < min_size) sigstack_zero_bits++;
804 assert( sizeof(TEB) <= min_size );
807 size = 1 << sigstack_zero_bits;
809 if (!(status = NtAllocateVirtualMemory( NtCurrentProcess(), (void **)teb, sigstack_zero_bits,
810 &size, MEM_COMMIT | MEM_TOP_DOWN, PAGE_READWRITE )))
812 (*teb)->Tib.Self = &(*teb)->Tib;
813 (*teb)->Tib.ExceptionList = (void *)~0UL;
819 /**********************************************************************
822 void signal_free_thread( TEB *teb )
826 if (teb->DeallocationStack)
829 NtFreeVirtualMemory( GetCurrentProcess(), &teb->DeallocationStack, &size, MEM_RELEASE );
832 NtFreeVirtualMemory( NtCurrentProcess(), (void **)&teb, &size, MEM_RELEASE );
836 /**********************************************************************
839 void signal_init_thread( TEB *teb )
841 static int init_done;
845 pthread_key_create( &teb_key, NULL );
848 pthread_setspecific( teb_key, teb );
852 /**********************************************************************
853 * signal_init_process
855 void signal_init_process(void)
857 struct sigaction sig_act;
859 sig_act.sa_mask = server_block_set;
860 sig_act.sa_flags = SA_RESTART | SA_SIGINFO;
862 sig_act.sa_sigaction = int_handler;
863 if (sigaction( SIGINT, &sig_act, NULL ) == -1) goto error;
864 sig_act.sa_sigaction = fpe_handler;
865 if (sigaction( SIGFPE, &sig_act, NULL ) == -1) goto error;
866 sig_act.sa_sigaction = abrt_handler;
867 if (sigaction( SIGABRT, &sig_act, NULL ) == -1) goto error;
868 sig_act.sa_sigaction = quit_handler;
869 if (sigaction( SIGQUIT, &sig_act, NULL ) == -1) goto error;
870 sig_act.sa_sigaction = usr1_handler;
871 if (sigaction( SIGUSR1, &sig_act, NULL ) == -1) goto error;
873 sig_act.sa_sigaction = segv_handler;
874 if (sigaction( SIGSEGV, &sig_act, NULL ) == -1) goto error;
875 if (sigaction( SIGILL, &sig_act, NULL ) == -1) goto error;
877 if (sigaction( SIGBUS, &sig_act, NULL ) == -1) goto error;
881 sig_act.sa_sigaction = trap_handler;
882 if (sigaction( SIGTRAP, &sig_act, NULL ) == -1) goto error;
892 /**********************************************************************
893 * __wine_enter_vm86 (NTDLL.@)
895 void __wine_enter_vm86( CONTEXT *context )
897 MESSAGE("vm86 mode not supported on this platform\n");
900 /***********************************************************************
901 * RtlUnwind (NTDLL.@)
903 void WINAPI RtlUnwind( PVOID pEndFrame, PVOID targetIp, PEXCEPTION_RECORD pRecord, PVOID retval )
905 FIXME( "Not implemented on ARM\n" );
908 /*******************************************************************
909 * NtRaiseException (NTDLL.@)
911 NTSTATUS WINAPI NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context, BOOL first_chance )
913 NTSTATUS status = raise_exception( rec, context, first_chance );
914 if (status == STATUS_SUCCESS) NtSetContextThread( GetCurrentThread(), context );
918 /***********************************************************************
919 * RtlRaiseException (NTDLL.@)
921 void WINAPI RtlRaiseException( EXCEPTION_RECORD *rec )
926 RtlCaptureContext( &context );
927 rec->ExceptionAddress = (LPVOID)context.Pc;
928 status = raise_exception( rec, &context, TRUE );
929 if (status) raise_status( status, rec );
932 /*************************************************************************
933 * RtlCaptureStackBackTrace (NTDLL.@)
935 USHORT WINAPI RtlCaptureStackBackTrace( ULONG skip, ULONG count, PVOID *buffer, ULONG *hash )
937 FIXME( "(%d, %d, %p, %p) stub!\n", skip, count, buffer, hash );
941 /***********************************************************************
942 * call_thread_entry_point
944 void call_thread_entry_point( LPTHREAD_START_ROUTINE entry, void *arg )
948 exit_thread( entry( arg ));
950 __EXCEPT(unhandled_exception_filter)
952 NtTerminateThread( GetCurrentThread(), GetExceptionCode() );
955 abort(); /* should not be reached */
958 /***********************************************************************
959 * RtlExitUserThread (NTDLL.@)
961 void WINAPI RtlExitUserThread( ULONG status )
963 exit_thread( status );
966 /***********************************************************************
969 void abort_thread( int status )
971 terminate_thread( status );
974 /**********************************************************************
975 * DbgBreakPoint (NTDLL.@)
977 void WINAPI DbgBreakPoint(void)
979 kill(getpid(), SIGTRAP);
982 /**********************************************************************
983 * DbgUserBreakPoint (NTDLL.@)
985 void WINAPI DbgUserBreakPoint(void)
987 kill(getpid(), SIGTRAP);
990 /**********************************************************************
991 * NtCurrentTeb (NTDLL.@)
993 TEB * WINAPI NtCurrentTeb(void)
995 return pthread_getspecific( teb_key );