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>
50 #define NONAMELESSUNION
51 #define NONAMELESSSTRUCT
53 #define WIN32_NO_STATUS
56 #include "wine/library.h"
57 #include "wine/exception.h"
58 #include "ntdll_misc.h"
59 #include "wine/debug.h"
62 WINE_DEFAULT_DEBUG_CHANNEL(seh
);
64 static pthread_key_t teb_key
;
66 /***********************************************************************
67 * signal context platform-specific definitions
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 */
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)
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];
108 WORD function_length
;
114 /***********************************************************************
117 static inline int dispatch_signal(unsigned int sig
)
119 if (handlers
[sig
] == NULL
) return 0;
120 return handlers
[sig
](sig
);
123 /*******************************************************************
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 /***********************************************************************
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);
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 /***********************************************************************
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);
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 /***********************************************************************
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 /***********************************************************************
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,
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 */
226 "str r1, [r0, #0x44]\n\t" /* context->Cpsr */
231 /***********************************************************************
234 * Set the new CPU context.
236 /* FIXME: What about the CPSR? */
237 __ASM_GLOBAL_FUNC( set_cpu_context
,
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 /***********************************************************************
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
)
270 to
->Cpsr
= from
->Cpsr
;
272 if (flags
& CONTEXT_INTEGER
)
291 /***********************************************************************
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
) );
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
,
376 extern void raise_func_trampoline_arm( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, raise_func func
);
377 __ASM_GLOBAL_FUNC( raise_func_trampoline_arm
,
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
)
392 EXCEPTION_RECORD rec
;
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
;
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 */
421 /**********************************************************************
422 * raise_segv_exception
424 static void WINAPI
raise_segv_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
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] )))
439 status
= NtRaiseException( rec
, context
, TRUE
);
440 if (status
) raise_status( status
, rec
);
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
;
455 frame
= NtCurrentTeb()->Tib
.ExceptionList
;
457 while (frame
!= (EXCEPTION_REGISTRATION_RECORD
*)~0UL)
459 /* Check frame address */
460 if (!is_valid_frame( frame
))
462 rec
->ExceptionFlags
|= EH_STACK_INVALID
;
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 */
476 rec
->ExceptionFlags
&= ~EH_NESTED_CALL
;
481 case ExceptionContinueExecution
:
482 if (!(rec
->ExceptionFlags
& EH_NONCONTINUABLE
)) return STATUS_SUCCESS
;
483 return STATUS_NONCONTINUABLE_EXCEPTION
;
484 case ExceptionContinueSearch
:
486 case ExceptionNestedException
:
487 if (nested_frame
< dispatch
) nested_frame
= dispatch
;
488 rec
->ExceptionFlags
|= EH_NESTED_CALL
;
491 return STATUS_INVALID_DISPOSITION
;
495 return STATUS_UNHANDLED_EXCEPTION
;
499 /*******************************************************************
502 * Implementation of NtRaiseException.
504 static NTSTATUS
raise_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, BOOL first_chance
)
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] );
521 MESSAGE( "wine: Call from %p to unimplemented function %s.%ld, aborting\n",
522 rec
->ExceptionAddress
,
523 (char*)rec
->ExceptionInformation
[0], rec
->ExceptionInformation
[1] );
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
)
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");
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 /**********************************************************************
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
;
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
;
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
;
603 case TRAP_ARM_ALIGNFLT
: /* Alignment check exception */
604 rec
->ExceptionCode
= EXCEPTION_DATATYPE_MISALIGNMENT
;
607 ERR("Got unexpected trap %ld\n", TRAP_sig(context
));
608 rec
->ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
613 /**********************************************************************
616 * Handler for SIGTRAP.
618 static void trap_handler( int signal
, siginfo_t
*info
, void *ucontext
)
620 EXCEPTION_RECORD rec
;
624 switch ( info
->si_code
)
627 rec
.ExceptionCode
= EXCEPTION_SINGLE_STEP
;
631 rec
.ExceptionCode
= EXCEPTION_BREAKPOINT
;
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 /**********************************************************************
648 * Handler for SIGFPE.
650 static void fpe_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
652 EXCEPTION_RECORD rec
;
656 save_fpu( &context
, sigcontext
);
657 save_context( &context
, sigcontext
);
659 switch (siginfo
->si_code
& 0xffff )
663 rec
.ExceptionCode
= EXCEPTION_ARRAY_BOUNDS_EXCEEDED
;
668 rec
.ExceptionCode
= EXCEPTION_INT_DIVIDE_BY_ZERO
;
673 rec
.ExceptionCode
= EXCEPTION_INT_OVERFLOW
;
678 rec
.ExceptionCode
= EXCEPTION_FLT_DIVIDE_BY_ZERO
;
683 rec
.ExceptionCode
= EXCEPTION_FLT_OVERFLOW
;
688 rec
.ExceptionCode
= EXCEPTION_FLT_UNDERFLOW
;
693 rec
.ExceptionCode
= EXCEPTION_FLT_INEXACT_RESULT
;
700 rec
.ExceptionCode
= EXCEPTION_FLT_INVALID_OPERATION
;
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 /**********************************************************************
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
;
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 /**********************************************************************
743 * Handler for SIGABRT.
745 static void abrt_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
747 EXCEPTION_RECORD rec
;
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 /**********************************************************************
766 * Handler for SIGQUIT.
768 static void quit_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
774 /**********************************************************************
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
)
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;
801 /**********************************************************************
802 * signal_alloc_thread
804 NTSTATUS
signal_alloc_thread( TEB
**teb
)
806 static size_t sigstack_zero_bits
;
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
;
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;
830 /**********************************************************************
833 void signal_free_thread( TEB
*teb
)
837 if (teb
->DeallocationStack
)
840 NtFreeVirtualMemory( GetCurrentProcess(), &teb
->DeallocationStack
, &size
, MEM_RELEASE
);
843 NtFreeVirtualMemory( NtCurrentProcess(), (void **)&teb
, &size
, MEM_RELEASE
);
846 /**********************************************************************
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 */
857 void set_tpidrurw( TEB
*teb
)
862 /**********************************************************************
865 void signal_init_thread( TEB
*teb
)
867 static int init_done
;
871 pthread_key_create( &teb_key
, NULL
);
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
;
904 if (sigaction( SIGBUS
, &sig_act
, NULL
) == -1) goto error
;
908 sig_act
.sa_sigaction
= trap_handler
;
909 if (sigaction( SIGTRAP
, &sig_act
, NULL
) == -1) goto error
;
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
);
938 /**********************************************************************
939 * RtlDeleteFunctionTable (NTDLL.@)
941 BOOLEAN CDECL
RtlDeleteFunctionTable( RUNTIME_FUNCTION
*table
)
943 FIXME( "%p: stub\n", table
);
947 /**********************************************************************
950 static RUNTIME_FUNCTION
*find_function_info( ULONG_PTR pc
, HMODULE module
,
951 RUNTIME_FUNCTION
*func
, ULONG size
)
954 int max
= size
/sizeof(*func
) - 1;
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;
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
;
976 /**********************************************************************
977 * RtlLookupFunctionEntry (NTDLL.@)
979 PRUNTIME_FUNCTION WINAPI
RtlLookupFunctionEntry( ULONG_PTR pc
, DWORD
*base
,
980 UNWIND_HISTORY_TABLE
*table
)
983 RUNTIME_FUNCTION
*func
;
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
);
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
);
999 func
= find_function_info( pc
, module
->BaseAddress
, func
, size
);
1000 if (func
) *base
= (DWORD
)module
->BaseAddress
;
1004 /***********************************************************************
1005 * RtlUnwind (NTDLL.@)
1007 void WINAPI
RtlUnwind( void *endframe
, void *target_ip
, EXCEPTION_RECORD
*rec
, void *retval
)
1010 EXCEPTION_RECORD record
;
1011 EXCEPTION_REGISTRATION_RECORD
*frame
, *dispatch
;
1014 RtlCaptureContext( &context
);
1015 context
.R0
= (DWORD
)retval
;
1017 /* build an exception record, if we do not have one */
1020 record
.ExceptionCode
= STATUS_UNWIND
;
1021 record
.ExceptionFlags
= 0;
1022 record
.ExceptionRecord
= NULL
;
1023 record
.ExceptionAddress
= (void *)context
.Pc
;
1024 record
.NumberParameters
= 0;
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
);
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
);
1050 case ExceptionContinueSearch
:
1052 case ExceptionCollidedUnwind
:
1056 raise_status( STATUS_INVALID_DISPOSITION
, rec
);
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
);
1073 /***********************************************************************
1074 * RtlRaiseException (NTDLL.@)
1076 void WINAPI
RtlRaiseException( EXCEPTION_RECORD
*rec
)
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
);
1096 /***********************************************************************
1097 * call_thread_entry_point
1099 void call_thread_entry_point( LPTHREAD_START_ROUTINE entry
, void *arg
)
1103 exit_thread( entry( arg
));
1105 __EXCEPT(unhandled_exception_filter
)
1107 NtTerminateThread( GetCurrentThread(), GetExceptionCode() );
1110 abort(); /* should not be reached */
1113 /***********************************************************************
1114 * RtlExitUserThread (NTDLL.@)
1116 void WINAPI
RtlExitUserThread( ULONG status
)
1118 exit_thread( status
);
1121 /***********************************************************************
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__ */