2 * ARM signal handling routines
4 * Copyright 2002 Marcus Meissner, SuSE Linux AG
5 * Copyright 2010-2013, 2015 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"
35 #ifdef HAVE_SYS_PARAM_H
36 # include <sys/param.h>
41 # ifdef HAVE_SYS_SYSCALL_H
42 # include <sys/syscall.h>
45 #ifdef HAVE_SYS_SIGNAL_H
46 # include <sys/signal.h>
48 #ifdef HAVE_SYS_UCONTEXT_H
49 # include <sys/ucontext.h>
52 #define NONAMELESSUNION
53 #define NONAMELESSSTRUCT
55 #define WIN32_NO_STATUS
58 #include "wine/library.h"
59 #include "wine/exception.h"
60 #include "ntdll_misc.h"
61 #include "wine/debug.h"
64 WINE_DEFAULT_DEBUG_CHANNEL(seh
);
66 static pthread_key_t teb_key
;
68 /***********************************************************************
69 * signal context platform-specific definitions
73 #if defined(__ANDROID__) && !defined(HAVE_SYS_UCONTEXT_H)
74 typedef struct ucontext
76 unsigned long uc_flags
;
77 struct ucontext
*uc_link
;
79 struct sigcontext uc_mcontext
;
81 unsigned long uc_regspace
[128] __attribute__((__aligned__(8)));
85 /* All Registers access - only for local access */
86 # define REG_sig(reg_name, context) ((context)->uc_mcontext.reg_name)
87 # define REGn_sig(reg_num, context) ((context)->uc_mcontext.arm_r##reg_num)
89 /* Special Registers access */
90 # define SP_sig(context) REG_sig(arm_sp, context) /* Stack pointer */
91 # define LR_sig(context) REG_sig(arm_lr, context) /* Link register */
92 # define PC_sig(context) REG_sig(arm_pc, context) /* Program counter */
93 # define CPSR_sig(context) REG_sig(arm_cpsr, context) /* Current State Register */
94 # define IP_sig(context) REG_sig(arm_ip, context) /* Intra-Procedure-call scratch register */
95 # define FP_sig(context) REG_sig(arm_fp, context) /* Frame pointer */
98 # define ERROR_sig(context) REG_sig(error_code, context)
99 # define TRAP_sig(context) REG_sig(trap_no, context)
101 #elif defined(__FreeBSD__)
103 /* All Registers access - only for local access */
104 # define REGn_sig(reg_num, context) ((context)->uc_mcontext.__gregs[reg_num])
106 /* Special Registers access */
107 # define SP_sig(context) REGn_sig(_REG_SP, context) /* Stack pointer */
108 # define LR_sig(context) REGn_sig(_REG_LR, context) /* Link register */
109 # define PC_sig(context) REGn_sig(_REG_PC, context) /* Program counter */
110 # define CPSR_sig(context) REGn_sig(_REG_CPSR, context) /* Current State Register */
111 # define IP_sig(context) REGn_sig(_REG_R12, context) /* Intra-Procedure-call scratch register */
112 # define FP_sig(context) REGn_sig(_REG_FP, context) /* Frame pointer */
118 TRAP_ARM_UNKNOWN
= -1, /* Unknown fault (TRAP_sig not defined) */
119 TRAP_ARM_PRIVINFLT
= 6, /* Invalid opcode exception */
120 TRAP_ARM_PAGEFLT
= 14, /* Page fault */
121 TRAP_ARM_ALIGNFLT
= 17, /* Alignment check exception */
124 typedef void (WINAPI
*raise_func
)( EXCEPTION_RECORD
*rec
, CONTEXT
*context
);
125 typedef int (*wine_signal_handler
)(unsigned int sig
);
127 static wine_signal_handler handlers
[256];
132 WORD function_length
;
139 /***********************************************************************
142 * Get the trap code for a signal.
144 static inline enum arm_trap_code
get_trap_code( const ucontext_t
*sigcontext
)
147 return TRAP_sig(sigcontext
);
149 return TRAP_ARM_UNKNOWN
; /* unknown trap code */
153 /***********************************************************************
156 * Get the error code for a signal.
158 static inline WORD
get_error_code( const ucontext_t
*sigcontext
)
161 return ERROR_sig(sigcontext
);
167 /***********************************************************************
170 static inline int dispatch_signal(unsigned int sig
)
172 if (handlers
[sig
] == NULL
) return 0;
173 return handlers
[sig
](sig
);
176 /*******************************************************************
179 static inline BOOL
is_valid_frame( void *frame
)
181 if ((ULONG_PTR
)frame
& 3) return FALSE
;
182 return (frame
>= NtCurrentTeb()->Tib
.StackLimit
&&
183 (void **)frame
< (void **)NtCurrentTeb()->Tib
.StackBase
- 1);
186 /***********************************************************************
189 * Set the register values from a sigcontext.
191 static void save_context( CONTEXT
*context
, const ucontext_t
*sigcontext
)
193 #define C(x) context->R##x = REGn_sig(x,sigcontext)
194 /* Save normal registers */
195 C(0); C(1); C(2); C(3); C(4); C(5); C(6); C(7); C(8); C(9); C(10);
198 context
->ContextFlags
= CONTEXT_FULL
;
199 context
->Sp
= SP_sig(sigcontext
); /* Stack pointer */
200 context
->Lr
= LR_sig(sigcontext
); /* Link register */
201 context
->Pc
= PC_sig(sigcontext
); /* Program Counter */
202 context
->Cpsr
= CPSR_sig(sigcontext
); /* Current State Register */
203 context
->Ip
= IP_sig(sigcontext
); /* Intra-Procedure-call scratch register */
204 context
->Fp
= FP_sig(sigcontext
); /* Frame pointer */
208 /***********************************************************************
211 * Build a sigcontext from the register values.
213 static void restore_context( const CONTEXT
*context
, ucontext_t
*sigcontext
)
215 #define C(x) REGn_sig(x,sigcontext) = context->R##x
216 /* Restore normal registers */
217 C(0); C(1); C(2); C(3); C(4); C(5); C(6); C(7); C(8); C(9); C(10);
220 SP_sig(sigcontext
) = context
->Sp
; /* Stack pointer */
221 LR_sig(sigcontext
) = context
->Lr
; /* Link register */
222 PC_sig(sigcontext
) = context
->Pc
; /* Program Counter */
223 CPSR_sig(sigcontext
) = context
->Cpsr
; /* Current State Register */
224 IP_sig(sigcontext
) = context
->Ip
; /* Intra-Procedure-call scratch register */
225 FP_sig(sigcontext
) = context
->Fp
; /* Frame pointer */
229 /***********************************************************************
232 * Set the FPU context from a sigcontext.
234 static inline void save_fpu( CONTEXT
*context
, const ucontext_t
*sigcontext
)
236 FIXME("not implemented\n");
240 /***********************************************************************
243 * Restore the FPU context to a sigcontext.
245 static inline void restore_fpu( CONTEXT
*context
, const ucontext_t
*sigcontext
)
247 FIXME("not implemented\n");
250 /**************************************************************************
253 * Incoming r4 contains words to allocate, converting to bytes then return
255 __ASM_GLOBAL_FUNC( __chkstk
, "lsl r4, r4, #2\n\t"
258 /***********************************************************************
259 * RtlCaptureContext (NTDLL.@)
261 /* FIXME: Use the Stack instead of the actual register values */
262 __ASM_STDCALL_FUNC( RtlCaptureContext
, 4,
264 "stmfd SP!, {r1}\n\t"
265 "mov r1, #0x0200000\n\t"/* CONTEXT_ARM */
266 "add r1, r1, #0x3\n\t" /* CONTEXT_FULL */
267 "str r1, [r0]\n\t" /* context->ContextFlags */
268 "ldmfd SP!, {r1}\n\t"
269 "str r0, [r0, #0x4]\n\t" /* context->R0 */
270 "str r1, [r0, #0x8]\n\t" /* context->R1 */
271 "str r2, [r0, #0xc]\n\t" /* context->R2 */
272 "str r3, [r0, #0x10]\n\t" /* context->R3 */
273 "str r4, [r0, #0x14]\n\t" /* context->R4 */
274 "str r5, [r0, #0x18]\n\t" /* context->R5 */
275 "str r6, [r0, #0x1c]\n\t" /* context->R6 */
276 "str r7, [r0, #0x20]\n\t" /* context->R7 */
277 "str r8, [r0, #0x24]\n\t" /* context->R8 */
278 "str r9, [r0, #0x28]\n\t" /* context->R9 */
279 "str r10, [r0, #0x2c]\n\t" /* context->R10 */
280 "str r11, [r0, #0x30]\n\t" /* context->Fp */
281 "str IP, [r0, #0x34]\n\t" /* context->Ip */
282 "str SP, [r0, #0x38]\n\t" /* context->Sp */
283 "str LR, [r0, #0x3c]\n\t" /* context->Lr */
284 "str PC, [r0, #0x40]\n\t" /* context->Pc */
286 "str r1, [r0, #0x44]\n\t" /* context->Cpsr */
291 /***********************************************************************
294 * Set the new CPU context.
296 /* FIXME: What about the CPSR? */
297 __ASM_GLOBAL_FUNC( set_cpu_context
,
299 "ldr r0, [IP, #0x4]\n\t" /* context->R0 */
300 "ldr r1, [IP, #0x8]\n\t" /* context->R1 */
301 "ldr r2, [IP, #0xc]\n\t" /* context->R2 */
302 "ldr r3, [IP, #0x10]\n\t" /* context->R3 */
303 "ldr r4, [IP, #0x14]\n\t" /* context->R4 */
304 "ldr r5, [IP, #0x18]\n\t" /* context->R5 */
305 "ldr r6, [IP, #0x1c]\n\t" /* context->R6 */
306 "ldr r7, [IP, #0x20]\n\t" /* context->R7 */
307 "ldr r8, [IP, #0x24]\n\t" /* context->R8 */
308 "ldr r9, [IP, #0x28]\n\t" /* context->R9 */
309 "ldr r10, [IP, #0x2c]\n\t" /* context->R10 */
310 "ldr r11, [IP, #0x30]\n\t" /* context->Fp */
311 "ldr SP, [IP, #0x38]\n\t" /* context->Sp */
312 "ldr LR, [IP, #0x3c]\n\t" /* context->Lr */
313 "ldr PC, [IP, #0x40]\n\t" /* context->Pc */
317 /***********************************************************************
320 * Copy a register context according to the flags.
322 void copy_context( CONTEXT
*to
, const CONTEXT
*from
, DWORD flags
)
324 flags
&= ~CONTEXT_ARM
; /* get rid of CPU id */
325 if (flags
& CONTEXT_CONTROL
)
330 to
->Cpsr
= from
->Cpsr
;
332 if (flags
& CONTEXT_INTEGER
)
351 /***********************************************************************
354 * Convert a register context to the server format.
356 NTSTATUS
context_to_server( context_t
*to
, const CONTEXT
*from
)
358 DWORD flags
= from
->ContextFlags
& ~CONTEXT_ARM
; /* get rid of CPU id */
360 memset( to
, 0, sizeof(*to
) );
363 if (flags
& CONTEXT_CONTROL
)
365 to
->flags
|= SERVER_CTX_CONTROL
;
366 to
->ctl
.arm_regs
.sp
= from
->Sp
;
367 to
->ctl
.arm_regs
.lr
= from
->Lr
;
368 to
->ctl
.arm_regs
.pc
= from
->Pc
;
369 to
->ctl
.arm_regs
.cpsr
= from
->Cpsr
;
371 if (flags
& CONTEXT_INTEGER
)
373 to
->flags
|= SERVER_CTX_INTEGER
;
374 to
->integer
.arm_regs
.r
[0] = from
->R0
;
375 to
->integer
.arm_regs
.r
[1] = from
->R1
;
376 to
->integer
.arm_regs
.r
[2] = from
->R2
;
377 to
->integer
.arm_regs
.r
[3] = from
->R3
;
378 to
->integer
.arm_regs
.r
[4] = from
->R4
;
379 to
->integer
.arm_regs
.r
[5] = from
->R5
;
380 to
->integer
.arm_regs
.r
[6] = from
->R6
;
381 to
->integer
.arm_regs
.r
[7] = from
->R7
;
382 to
->integer
.arm_regs
.r
[8] = from
->R8
;
383 to
->integer
.arm_regs
.r
[9] = from
->R9
;
384 to
->integer
.arm_regs
.r
[10] = from
->R10
;
385 to
->integer
.arm_regs
.r
[11] = from
->Fp
;
386 to
->integer
.arm_regs
.r
[12] = from
->Ip
;
388 return STATUS_SUCCESS
;
392 /***********************************************************************
393 * context_from_server
395 * Convert a register context from the server format.
397 NTSTATUS
context_from_server( CONTEXT
*to
, const context_t
*from
)
399 if (from
->cpu
!= CPU_ARM
) return STATUS_INVALID_PARAMETER
;
401 to
->ContextFlags
= CONTEXT_ARM
;
402 if (from
->flags
& SERVER_CTX_CONTROL
)
404 to
->ContextFlags
|= CONTEXT_CONTROL
;
405 to
->Sp
= from
->ctl
.arm_regs
.sp
;
406 to
->Lr
= from
->ctl
.arm_regs
.lr
;
407 to
->Pc
= from
->ctl
.arm_regs
.pc
;
408 to
->Cpsr
= from
->ctl
.arm_regs
.cpsr
;
410 if (from
->flags
& SERVER_CTX_INTEGER
)
412 to
->ContextFlags
|= CONTEXT_INTEGER
;
413 to
->R0
= from
->integer
.arm_regs
.r
[0];
414 to
->R1
= from
->integer
.arm_regs
.r
[1];
415 to
->R2
= from
->integer
.arm_regs
.r
[2];
416 to
->R3
= from
->integer
.arm_regs
.r
[3];
417 to
->R4
= from
->integer
.arm_regs
.r
[4];
418 to
->R5
= from
->integer
.arm_regs
.r
[5];
419 to
->R6
= from
->integer
.arm_regs
.r
[6];
420 to
->R7
= from
->integer
.arm_regs
.r
[7];
421 to
->R8
= from
->integer
.arm_regs
.r
[8];
422 to
->R9
= from
->integer
.arm_regs
.r
[9];
423 to
->R10
= from
->integer
.arm_regs
.r
[10];
424 to
->Fp
= from
->integer
.arm_regs
.r
[11];
425 to
->Ip
= from
->integer
.arm_regs
.r
[12];
427 return STATUS_SUCCESS
;
430 extern void raise_func_trampoline_thumb( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, raise_func func
);
431 __ASM_GLOBAL_FUNC( raise_func_trampoline_thumb
,
436 extern void raise_func_trampoline_arm( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, raise_func func
);
437 __ASM_GLOBAL_FUNC( raise_func_trampoline_arm
,
442 /***********************************************************************
443 * setup_exception_record
445 * Setup the exception record and context on the thread stack.
447 static EXCEPTION_RECORD
*setup_exception( ucontext_t
*sigcontext
, raise_func func
)
452 EXCEPTION_RECORD rec
;
454 DWORD exception_code
= 0;
456 stack
= (struct stack_layout
*)(SP_sig(sigcontext
) & ~3);
457 stack
--; /* push the stack_layout structure */
459 stack
->rec
.ExceptionRecord
= NULL
;
460 stack
->rec
.ExceptionCode
= exception_code
;
461 stack
->rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
462 stack
->rec
.ExceptionAddress
= (LPVOID
)PC_sig(sigcontext
);
463 stack
->rec
.NumberParameters
= 0;
465 save_context( &stack
->context
, sigcontext
);
467 /* now modify the sigcontext to return to the raise function */
468 SP_sig(sigcontext
) = (DWORD
)stack
;
469 if (CPSR_sig(sigcontext
) & 0x20)
470 PC_sig(sigcontext
) = (DWORD
)raise_func_trampoline_thumb
;
472 PC_sig(sigcontext
) = (DWORD
)raise_func_trampoline_arm
;
473 REGn_sig(0, sigcontext
) = (DWORD
)&stack
->rec
; /* first arg for raise_func */
474 REGn_sig(1, sigcontext
) = (DWORD
)&stack
->context
; /* second arg for raise_func */
475 REGn_sig(2, sigcontext
) = (DWORD
)func
; /* the raise_func as third arg for the trampoline */
481 /**********************************************************************
482 * raise_segv_exception
484 static void WINAPI
raise_segv_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
488 switch(rec
->ExceptionCode
)
490 case EXCEPTION_ACCESS_VIOLATION
:
491 if (rec
->NumberParameters
== 2)
493 if (!(rec
->ExceptionCode
= virtual_handle_fault( (void *)rec
->ExceptionInformation
[1],
494 rec
->ExceptionInformation
[0], FALSE
)))
499 status
= NtRaiseException( rec
, context
, TRUE
);
500 if (status
) raise_status( status
, rec
);
502 set_cpu_context( context
);
505 /**********************************************************************
506 * call_stack_handlers
508 * Call the stack handlers chain.
510 static NTSTATUS
call_stack_handlers( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
512 EXCEPTION_REGISTRATION_RECORD
*frame
, *dispatch
, *nested_frame
;
515 frame
= NtCurrentTeb()->Tib
.ExceptionList
;
517 while (frame
!= (EXCEPTION_REGISTRATION_RECORD
*)~0UL)
519 /* Check frame address */
520 if (!is_valid_frame( frame
))
522 rec
->ExceptionFlags
|= EH_STACK_INVALID
;
527 TRACE( "calling handler at %p code=%x flags=%x\n",
528 frame
->Handler
, rec
->ExceptionCode
, rec
->ExceptionFlags
);
529 res
= frame
->Handler( rec
, frame
, context
, &dispatch
);
530 TRACE( "handler at %p returned %x\n", frame
->Handler
, res
);
532 if (frame
== nested_frame
)
534 /* no longer nested */
536 rec
->ExceptionFlags
&= ~EH_NESTED_CALL
;
541 case ExceptionContinueExecution
:
542 if (!(rec
->ExceptionFlags
& EH_NONCONTINUABLE
)) return STATUS_SUCCESS
;
543 return STATUS_NONCONTINUABLE_EXCEPTION
;
544 case ExceptionContinueSearch
:
546 case ExceptionNestedException
:
547 if (nested_frame
< dispatch
) nested_frame
= dispatch
;
548 rec
->ExceptionFlags
|= EH_NESTED_CALL
;
551 return STATUS_INVALID_DISPOSITION
;
555 return STATUS_UNHANDLED_EXCEPTION
;
559 /*******************************************************************
562 * Implementation of NtRaiseException.
564 static NTSTATUS
raise_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, BOOL first_chance
)
572 TRACE( "code=%x flags=%x addr=%p pc=%08x tid=%04x\n",
573 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
,
574 context
->Pc
, GetCurrentThreadId() );
575 for (c
= 0; c
< rec
->NumberParameters
; c
++)
576 TRACE( " info[%d]=%08lx\n", c
, rec
->ExceptionInformation
[c
] );
577 if (rec
->ExceptionCode
== EXCEPTION_WINE_STUB
)
579 if (rec
->ExceptionInformation
[1] >> 16)
580 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
581 rec
->ExceptionAddress
,
582 (char*)rec
->ExceptionInformation
[0], (char*)rec
->ExceptionInformation
[1] );
584 MESSAGE( "wine: Call from %p to unimplemented function %s.%ld, aborting\n",
585 rec
->ExceptionAddress
,
586 (char*)rec
->ExceptionInformation
[0], rec
->ExceptionInformation
[1] );
590 TRACE( " r0=%08x r1=%08x r2=%08x r3=%08x r4=%08x r5=%08x\n",
591 context
->R0
, context
->R1
, context
->R2
, context
->R3
, context
->R4
, context
->R5
);
592 TRACE( " r6=%08x r7=%08x r8=%08x r9=%08x r10=%08x fp=%08x\n",
593 context
->R6
, context
->R7
, context
->R8
, context
->R9
, context
->R10
, context
->Fp
);
594 TRACE( " ip=%08x sp=%08x lr=%08x pc=%08x cpsr=%08x\n",
595 context
->Ip
, context
->Sp
, context
->Lr
, context
->Pc
, context
->Cpsr
);
598 status
= send_debug_event( rec
, TRUE
, context
);
599 if (status
== DBG_CONTINUE
|| status
== DBG_EXCEPTION_HANDLED
)
600 return STATUS_SUCCESS
;
602 if (call_vectored_handlers( rec
, context
) == EXCEPTION_CONTINUE_EXECUTION
)
603 return STATUS_SUCCESS
;
605 if ((status
= call_stack_handlers( rec
, context
)) != STATUS_UNHANDLED_EXCEPTION
)
609 /* last chance exception */
611 status
= send_debug_event( rec
, FALSE
, context
);
612 if (status
!= DBG_CONTINUE
)
614 if (rec
->ExceptionFlags
& EH_STACK_INVALID
)
615 ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
616 else if (rec
->ExceptionCode
== STATUS_NONCONTINUABLE_EXCEPTION
)
617 ERR("Process attempted to continue execution after noncontinuable exception.\n");
619 ERR("Unhandled exception code %x flags %x addr %p\n",
620 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
);
621 NtTerminateProcess( NtCurrentProcess(), rec
->ExceptionCode
);
623 return STATUS_SUCCESS
;
627 /**********************************************************************
630 * Handler for SIGSEGV and related errors.
632 static void segv_handler( int signal
, siginfo_t
*info
, void *ucontext
)
634 EXCEPTION_RECORD
*rec
;
635 ucontext_t
*context
= ucontext
;
637 /* check for page fault inside the thread stack */
638 if (get_trap_code(context
) == TRAP_ARM_PAGEFLT
&&
639 (char *)info
->si_addr
>= (char *)NtCurrentTeb()->DeallocationStack
&&
640 (char *)info
->si_addr
< (char *)NtCurrentTeb()->Tib
.StackBase
&&
641 virtual_handle_stack_fault( info
->si_addr
))
643 /* check if this was the last guard page */
644 if ((char *)info
->si_addr
< (char *)NtCurrentTeb()->DeallocationStack
+ 2*4096)
646 rec
= setup_exception( context
, raise_segv_exception
);
647 rec
->ExceptionCode
= EXCEPTION_STACK_OVERFLOW
;
652 rec
= setup_exception( context
, raise_segv_exception
);
653 if (rec
->ExceptionCode
== EXCEPTION_STACK_OVERFLOW
) return;
655 switch(get_trap_code(context
))
657 case TRAP_ARM_PRIVINFLT
: /* Invalid opcode exception */
658 rec
->ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
660 case TRAP_ARM_PAGEFLT
: /* Page fault */
661 rec
->ExceptionCode
= EXCEPTION_ACCESS_VIOLATION
;
662 rec
->NumberParameters
= 2;
663 rec
->ExceptionInformation
[0] = (get_error_code(context
) & 0x800) != 0;
664 rec
->ExceptionInformation
[1] = (ULONG_PTR
)info
->si_addr
;
666 case TRAP_ARM_ALIGNFLT
: /* Alignment check exception */
667 rec
->ExceptionCode
= EXCEPTION_DATATYPE_MISALIGNMENT
;
669 case TRAP_ARM_UNKNOWN
: /* Unknown fault code */
670 rec
->ExceptionCode
= EXCEPTION_ACCESS_VIOLATION
;
671 rec
->NumberParameters
= 2;
672 rec
->ExceptionInformation
[0] = 0;
673 rec
->ExceptionInformation
[1] = 0xffffffff;
676 ERR("Got unexpected trap %d\n", get_trap_code(context
));
677 rec
->ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
682 /**********************************************************************
685 * Handler for SIGTRAP.
687 static void trap_handler( int signal
, siginfo_t
*info
, void *ucontext
)
689 EXCEPTION_RECORD rec
;
693 switch ( info
->si_code
)
696 rec
.ExceptionCode
= EXCEPTION_SINGLE_STEP
;
700 rec
.ExceptionCode
= EXCEPTION_BREAKPOINT
;
704 save_context( &context
, ucontext
);
705 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
706 rec
.ExceptionRecord
= NULL
;
707 rec
.ExceptionAddress
= (LPVOID
)context
.Pc
;
708 rec
.NumberParameters
= 0;
709 status
= raise_exception( &rec
, &context
, TRUE
);
710 if (status
) raise_status( status
, &rec
);
711 restore_context( &context
, ucontext
);
714 /**********************************************************************
717 * Handler for SIGFPE.
719 static void fpe_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
721 EXCEPTION_RECORD rec
;
725 save_fpu( &context
, sigcontext
);
726 save_context( &context
, sigcontext
);
728 switch (siginfo
->si_code
& 0xffff )
732 rec
.ExceptionCode
= EXCEPTION_ARRAY_BOUNDS_EXCEEDED
;
737 rec
.ExceptionCode
= EXCEPTION_INT_DIVIDE_BY_ZERO
;
742 rec
.ExceptionCode
= EXCEPTION_INT_OVERFLOW
;
747 rec
.ExceptionCode
= EXCEPTION_FLT_DIVIDE_BY_ZERO
;
752 rec
.ExceptionCode
= EXCEPTION_FLT_OVERFLOW
;
757 rec
.ExceptionCode
= EXCEPTION_FLT_UNDERFLOW
;
762 rec
.ExceptionCode
= EXCEPTION_FLT_INEXACT_RESULT
;
769 rec
.ExceptionCode
= EXCEPTION_FLT_INVALID_OPERATION
;
772 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
773 rec
.ExceptionRecord
= NULL
;
774 rec
.ExceptionAddress
= (LPVOID
)context
.Pc
;
775 rec
.NumberParameters
= 0;
776 status
= raise_exception( &rec
, &context
, TRUE
);
777 if (status
) raise_status( status
, &rec
);
779 restore_context( &context
, sigcontext
);
780 restore_fpu( &context
, sigcontext
);
783 /**********************************************************************
786 * Handler for SIGINT.
788 static void int_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
790 if (!dispatch_signal(SIGINT
))
792 EXCEPTION_RECORD rec
;
796 save_context( &context
, sigcontext
);
797 rec
.ExceptionCode
= CONTROL_C_EXIT
;
798 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
799 rec
.ExceptionRecord
= NULL
;
800 rec
.ExceptionAddress
= (LPVOID
)context
.Pc
;
801 rec
.NumberParameters
= 0;
802 status
= raise_exception( &rec
, &context
, TRUE
);
803 if (status
) raise_status( status
, &rec
);
804 restore_context( &context
, sigcontext
);
809 /**********************************************************************
812 * Handler for SIGABRT.
814 static void abrt_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
816 EXCEPTION_RECORD rec
;
820 save_context( &context
, sigcontext
);
821 rec
.ExceptionCode
= EXCEPTION_WINE_ASSERTION
;
822 rec
.ExceptionFlags
= EH_NONCONTINUABLE
;
823 rec
.ExceptionRecord
= NULL
;
824 rec
.ExceptionAddress
= (LPVOID
)context
.Pc
;
825 rec
.NumberParameters
= 0;
826 status
= raise_exception( &rec
, &context
, TRUE
);
827 if (status
) raise_status( status
, &rec
);
828 restore_context( &context
, sigcontext
);
832 /**********************************************************************
835 * Handler for SIGQUIT.
837 static void quit_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
843 /**********************************************************************
846 * Handler for SIGUSR1, used to signal a thread that it got suspended.
848 static void usr1_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
852 save_context( &context
, sigcontext
);
853 wait_suspend( &context
);
854 restore_context( &context
, sigcontext
);
858 /***********************************************************************
859 * __wine_set_signal_handler (NTDLL.@)
861 int CDECL
__wine_set_signal_handler(unsigned int sig
, wine_signal_handler wsh
)
863 if (sig
>= sizeof(handlers
) / sizeof(handlers
[0])) return -1;
864 if (handlers
[sig
] != NULL
) return -2;
870 /**********************************************************************
871 * signal_alloc_thread
873 NTSTATUS
signal_alloc_thread( TEB
**teb
)
875 static size_t sigstack_zero_bits
;
879 if (!sigstack_zero_bits
)
881 size_t min_size
= page_size
;
882 /* find the first power of two not smaller than min_size */
883 while ((1u << sigstack_zero_bits
) < min_size
) sigstack_zero_bits
++;
884 assert( sizeof(TEB
) <= min_size
);
887 size
= 1 << sigstack_zero_bits
;
889 if (!(status
= NtAllocateVirtualMemory( NtCurrentProcess(), (void **)teb
, sigstack_zero_bits
,
890 &size
, MEM_COMMIT
| MEM_TOP_DOWN
, PAGE_READWRITE
)))
892 (*teb
)->Tib
.Self
= &(*teb
)->Tib
;
893 (*teb
)->Tib
.ExceptionList
= (void *)~0UL;
899 /**********************************************************************
902 void signal_free_thread( TEB
*teb
)
906 if (teb
->DeallocationStack
)
909 NtFreeVirtualMemory( GetCurrentProcess(), &teb
->DeallocationStack
, &size
, MEM_RELEASE
);
912 NtFreeVirtualMemory( NtCurrentProcess(), (void **)&teb
, &size
, MEM_RELEASE
);
916 /**********************************************************************
919 void signal_init_thread( TEB
*teb
)
921 static BOOL init_done
;
925 pthread_key_create( &teb_key
, NULL
);
929 #if defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_8A__)
930 /* Win32/ARM applications expect the TEB pointer to be in the TPIDRURW register. */
931 __asm__
__volatile__( "mcr p15, 0, %0, c13, c0, 2" : : "r" (teb
) );
934 pthread_setspecific( teb_key
, teb
);
938 /**********************************************************************
939 * signal_init_process
941 void signal_init_process(void)
943 struct sigaction sig_act
;
945 sig_act
.sa_mask
= server_block_set
;
946 sig_act
.sa_flags
= SA_RESTART
| SA_SIGINFO
;
948 sig_act
.sa_sigaction
= int_handler
;
949 if (sigaction( SIGINT
, &sig_act
, NULL
) == -1) goto error
;
950 sig_act
.sa_sigaction
= fpe_handler
;
951 if (sigaction( SIGFPE
, &sig_act
, NULL
) == -1) goto error
;
952 sig_act
.sa_sigaction
= abrt_handler
;
953 if (sigaction( SIGABRT
, &sig_act
, NULL
) == -1) goto error
;
954 sig_act
.sa_sigaction
= quit_handler
;
955 if (sigaction( SIGQUIT
, &sig_act
, NULL
) == -1) goto error
;
956 sig_act
.sa_sigaction
= usr1_handler
;
957 if (sigaction( SIGUSR1
, &sig_act
, NULL
) == -1) goto error
;
959 sig_act
.sa_sigaction
= segv_handler
;
960 if (sigaction( SIGSEGV
, &sig_act
, NULL
) == -1) goto error
;
961 if (sigaction( SIGILL
, &sig_act
, NULL
) == -1) goto error
;
963 if (sigaction( SIGBUS
, &sig_act
, NULL
) == -1) goto error
;
967 sig_act
.sa_sigaction
= trap_handler
;
968 if (sigaction( SIGTRAP
, &sig_act
, NULL
) == -1) goto error
;
978 /**********************************************************************
979 * __wine_enter_vm86 (NTDLL.@)
981 void __wine_enter_vm86( CONTEXT
*context
)
983 MESSAGE("vm86 mode not supported on this platform\n");
987 /**********************************************************************
988 * RtlAddFunctionTable (NTDLL.@)
990 BOOLEAN CDECL
RtlAddFunctionTable( RUNTIME_FUNCTION
*table
, DWORD count
, DWORD addr
)
992 FIXME( "%p %u %x: stub\n", table
, count
, addr
);
997 /**********************************************************************
998 * RtlDeleteFunctionTable (NTDLL.@)
1000 BOOLEAN CDECL
RtlDeleteFunctionTable( RUNTIME_FUNCTION
*table
)
1002 FIXME( "%p: stub\n", table
);
1006 /**********************************************************************
1007 * find_function_info
1009 static RUNTIME_FUNCTION
*find_function_info( ULONG_PTR pc
, HMODULE module
,
1010 RUNTIME_FUNCTION
*func
, ULONG size
)
1013 int max
= size
/sizeof(*func
) - 1;
1017 int pos
= (min
+ max
) / 2;
1018 DWORD begin
= (func
[pos
].BeginAddress
& ~1), end
;
1019 if (func
[pos
].u
.s
.Flag
)
1020 end
= begin
+ func
[pos
].u
.s
.FunctionLength
* 2;
1023 struct UNWIND_INFO
*info
;
1024 info
= (struct UNWIND_INFO
*)((char *)module
+ func
[pos
].u
.UnwindData
);
1025 end
= begin
+ info
->function_length
* 2;
1028 if ((char *)pc
< (char *)module
+ begin
) max
= pos
- 1;
1029 else if ((char *)pc
>= (char *)module
+ end
) min
= pos
+ 1;
1030 else return func
+ pos
;
1035 /**********************************************************************
1036 * RtlLookupFunctionEntry (NTDLL.@)
1038 PRUNTIME_FUNCTION WINAPI
RtlLookupFunctionEntry( ULONG_PTR pc
, DWORD
*base
,
1039 UNWIND_HISTORY_TABLE
*table
)
1042 RUNTIME_FUNCTION
*func
;
1045 /* FIXME: should use the history table to make things faster */
1047 if (LdrFindEntryForAddress( (void *)pc
, &module
))
1049 WARN( "module not found for %lx\n", pc
);
1052 if (!(func
= RtlImageDirectoryEntryToData( module
->BaseAddress
, TRUE
,
1053 IMAGE_DIRECTORY_ENTRY_EXCEPTION
, &size
)))
1055 WARN( "no exception table found in module %p pc %lx\n", module
->BaseAddress
, pc
);
1058 func
= find_function_info( pc
, module
->BaseAddress
, func
, size
);
1059 if (func
) *base
= (DWORD
)module
->BaseAddress
;
1063 /***********************************************************************
1064 * RtlUnwind (NTDLL.@)
1066 void WINAPI
RtlUnwind( void *endframe
, void *target_ip
, EXCEPTION_RECORD
*rec
, void *retval
)
1069 EXCEPTION_RECORD record
;
1070 EXCEPTION_REGISTRATION_RECORD
*frame
, *dispatch
;
1073 RtlCaptureContext( &context
);
1074 context
.R0
= (DWORD
)retval
;
1076 /* build an exception record, if we do not have one */
1079 record
.ExceptionCode
= STATUS_UNWIND
;
1080 record
.ExceptionFlags
= 0;
1081 record
.ExceptionRecord
= NULL
;
1082 record
.ExceptionAddress
= (void *)context
.Pc
;
1083 record
.NumberParameters
= 0;
1087 rec
->ExceptionFlags
|= EH_UNWINDING
| (endframe
? 0 : EH_EXIT_UNWIND
);
1089 TRACE( "code=%x flags=%x\n", rec
->ExceptionCode
, rec
->ExceptionFlags
);
1091 /* get chain of exception frames */
1092 frame
= NtCurrentTeb()->Tib
.ExceptionList
;
1093 while ((frame
!= (EXCEPTION_REGISTRATION_RECORD
*)~0UL) && (frame
!= endframe
))
1095 /* Check frame address */
1096 if (endframe
&& ((void*)frame
> endframe
))
1097 raise_status( STATUS_INVALID_UNWIND_TARGET
, rec
);
1099 if (!is_valid_frame( frame
)) raise_status( STATUS_BAD_STACK
, rec
);
1102 TRACE( "calling handler at %p code=%x flags=%x\n",
1103 frame
->Handler
, rec
->ExceptionCode
, rec
->ExceptionFlags
);
1104 res
= frame
->Handler(rec
, frame
, &context
, &dispatch
);
1105 TRACE( "handler at %p returned %x\n", frame
->Handler
, res
);
1109 case ExceptionContinueSearch
:
1111 case ExceptionCollidedUnwind
:
1115 raise_status( STATUS_INVALID_DISPOSITION
, rec
);
1118 frame
= __wine_pop_frame( frame
);
1122 /*******************************************************************
1123 * NtRaiseException (NTDLL.@)
1125 NTSTATUS WINAPI
NtRaiseException( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, BOOL first_chance
)
1127 NTSTATUS status
= raise_exception( rec
, context
, first_chance
);
1128 if (status
== STATUS_SUCCESS
) NtSetContextThread( GetCurrentThread(), context
);
1132 /***********************************************************************
1133 * RtlRaiseException (NTDLL.@)
1135 void WINAPI
RtlRaiseException( EXCEPTION_RECORD
*rec
)
1140 RtlCaptureContext( &context
);
1141 rec
->ExceptionAddress
= (LPVOID
)context
.Pc
;
1142 status
= raise_exception( rec
, &context
, TRUE
);
1143 if (status
) raise_status( status
, rec
);
1146 /*************************************************************************
1147 * RtlCaptureStackBackTrace (NTDLL.@)
1149 USHORT WINAPI
RtlCaptureStackBackTrace( ULONG skip
, ULONG count
, PVOID
*buffer
, ULONG
*hash
)
1151 FIXME( "(%d, %d, %p, %p) stub!\n", skip
, count
, buffer
, hash
);
1155 /***********************************************************************
1156 * call_thread_entry_point
1158 void call_thread_entry_point( LPTHREAD_START_ROUTINE entry
, void *arg
)
1162 exit_thread( entry( arg
));
1164 __EXCEPT(unhandled_exception_filter
)
1166 NtTerminateThread( GetCurrentThread(), GetExceptionCode() );
1169 abort(); /* should not be reached */
1172 /***********************************************************************
1173 * RtlExitUserThread (NTDLL.@)
1175 void WINAPI
RtlExitUserThread( ULONG status
)
1177 exit_thread( status
);
1180 /***********************************************************************
1183 void abort_thread( int status
)
1185 terminate_thread( status
);
1188 /**********************************************************************
1189 * DbgBreakPoint (NTDLL.@)
1191 void WINAPI
DbgBreakPoint(void)
1193 kill(getpid(), SIGTRAP
);
1196 /**********************************************************************
1197 * DbgUserBreakPoint (NTDLL.@)
1199 void WINAPI
DbgUserBreakPoint(void)
1201 kill(getpid(), SIGTRAP
);
1204 /**********************************************************************
1205 * NtCurrentTeb (NTDLL.@)
1207 TEB
* WINAPI
NtCurrentTeb(void)
1209 return pthread_getspecific( teb_key
);
1212 #endif /* __arm__ */