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
);
65 WINE_DECLARE_DEBUG_CHANNEL(relay
);
67 static pthread_key_t teb_key
;
69 /***********************************************************************
70 * signal context platform-specific definitions
74 #if defined(__ANDROID__) && !defined(HAVE_SYS_UCONTEXT_H)
75 typedef struct ucontext
77 unsigned long uc_flags
;
78 struct ucontext
*uc_link
;
80 struct sigcontext uc_mcontext
;
82 unsigned long uc_regspace
[128] __attribute__((__aligned__(8)));
86 /* All Registers access - only for local access */
87 # define REG_sig(reg_name, context) ((context)->uc_mcontext.reg_name)
88 # define REGn_sig(reg_num, context) ((context)->uc_mcontext.arm_r##reg_num)
90 /* Special Registers access */
91 # define SP_sig(context) REG_sig(arm_sp, context) /* Stack pointer */
92 # define LR_sig(context) REG_sig(arm_lr, context) /* Link register */
93 # define PC_sig(context) REG_sig(arm_pc, context) /* Program counter */
94 # define CPSR_sig(context) REG_sig(arm_cpsr, context) /* Current State Register */
95 # define IP_sig(context) REG_sig(arm_ip, context) /* Intra-Procedure-call scratch register */
96 # define FP_sig(context) REG_sig(arm_fp, context) /* Frame pointer */
99 # define ERROR_sig(context) REG_sig(error_code, context)
100 # define TRAP_sig(context) REG_sig(trap_no, context)
102 #elif defined(__FreeBSD__)
104 /* All Registers access - only for local access */
105 # define REGn_sig(reg_num, context) ((context)->uc_mcontext.__gregs[reg_num])
107 /* Special Registers access */
108 # define SP_sig(context) REGn_sig(_REG_SP, context) /* Stack pointer */
109 # define LR_sig(context) REGn_sig(_REG_LR, context) /* Link register */
110 # define PC_sig(context) REGn_sig(_REG_PC, context) /* Program counter */
111 # define CPSR_sig(context) REGn_sig(_REG_CPSR, context) /* Current State Register */
112 # define IP_sig(context) REGn_sig(_REG_R12, context) /* Intra-Procedure-call scratch register */
113 # define FP_sig(context) REGn_sig(_REG_FP, context) /* Frame pointer */
119 TRAP_ARM_UNKNOWN
= -1, /* Unknown fault (TRAP_sig not defined) */
120 TRAP_ARM_PRIVINFLT
= 6, /* Invalid opcode exception */
121 TRAP_ARM_PAGEFLT
= 14, /* Page fault */
122 TRAP_ARM_ALIGNFLT
= 17, /* Alignment check exception */
125 typedef void (WINAPI
*raise_func
)( EXCEPTION_RECORD
*rec
, CONTEXT
*context
);
126 typedef int (*wine_signal_handler
)(unsigned int sig
);
128 static wine_signal_handler handlers
[256];
133 WORD function_length
;
140 /***********************************************************************
143 * Get the trap code for a signal.
145 static inline enum arm_trap_code
get_trap_code( const ucontext_t
*sigcontext
)
148 return TRAP_sig(sigcontext
);
150 return TRAP_ARM_UNKNOWN
; /* unknown trap code */
154 /***********************************************************************
157 * Get the error code for a signal.
159 static inline WORD
get_error_code( const ucontext_t
*sigcontext
)
162 return ERROR_sig(sigcontext
);
168 /***********************************************************************
171 static inline int dispatch_signal(unsigned int sig
)
173 if (handlers
[sig
] == NULL
) return 0;
174 return handlers
[sig
](sig
);
177 /*******************************************************************
180 static inline BOOL
is_valid_frame( void *frame
)
182 if ((ULONG_PTR
)frame
& 3) return FALSE
;
183 return (frame
>= NtCurrentTeb()->Tib
.StackLimit
&&
184 (void **)frame
< (void **)NtCurrentTeb()->Tib
.StackBase
- 1);
187 /***********************************************************************
190 * Set the register values from a sigcontext.
192 static void save_context( CONTEXT
*context
, const ucontext_t
*sigcontext
)
194 #define C(x) context->R##x = REGn_sig(x,sigcontext)
195 /* Save normal registers */
196 C(0); C(1); C(2); C(3); C(4); C(5); C(6); C(7); C(8); C(9); C(10);
199 context
->ContextFlags
= CONTEXT_FULL
;
200 context
->Sp
= SP_sig(sigcontext
); /* Stack pointer */
201 context
->Lr
= LR_sig(sigcontext
); /* Link register */
202 context
->Pc
= PC_sig(sigcontext
); /* Program Counter */
203 context
->Cpsr
= CPSR_sig(sigcontext
); /* Current State Register */
204 context
->R11
= FP_sig(sigcontext
); /* Frame pointer */
205 context
->R12
= IP_sig(sigcontext
); /* Intra-Procedure-call scratch register */
209 /***********************************************************************
212 * Build a sigcontext from the register values.
214 static void restore_context( const CONTEXT
*context
, ucontext_t
*sigcontext
)
216 #define C(x) REGn_sig(x,sigcontext) = context->R##x
217 /* Restore normal registers */
218 C(0); C(1); C(2); C(3); C(4); C(5); C(6); C(7); C(8); C(9); C(10);
221 SP_sig(sigcontext
) = context
->Sp
; /* Stack pointer */
222 LR_sig(sigcontext
) = context
->Lr
; /* Link register */
223 PC_sig(sigcontext
) = context
->Pc
; /* Program Counter */
224 CPSR_sig(sigcontext
) = context
->Cpsr
; /* Current State Register */
225 FP_sig(sigcontext
) = context
->R11
; /* Frame pointer */
226 IP_sig(sigcontext
) = context
->R12
; /* Intra-Procedure-call scratch register */
230 /***********************************************************************
233 * Set the FPU context from a sigcontext.
235 static inline void save_fpu( CONTEXT
*context
, const ucontext_t
*sigcontext
)
237 FIXME("not implemented\n");
241 /***********************************************************************
244 * Restore the FPU context to a sigcontext.
246 static inline void restore_fpu( CONTEXT
*context
, const ucontext_t
*sigcontext
)
248 FIXME("not implemented\n");
251 /**************************************************************************
254 * Incoming r4 contains words to allocate, converting to bytes then return
256 __ASM_GLOBAL_FUNC( __chkstk
, "lsl r4, r4, #2\n\t"
259 /***********************************************************************
260 * RtlCaptureContext (NTDLL.@)
262 /* FIXME: Use the Stack instead of the actual register values */
263 __ASM_STDCALL_FUNC( RtlCaptureContext
, 4,
265 "stmib r0, {r0-r12}\n\t" /* context->R0..R12 */
266 "mov r1, #0x0200000\n\t" /* CONTEXT_ARM */
267 "add r1, r1, #0x3\n\t" /* CONTEXT_FULL */
268 "str r1, [r0]\n\t" /* context->ContextFlags */
269 "str SP, [r0, #0x38]\n\t" /* context->Sp */
270 "str LR, [r0, #0x3c]\n\t" /* context->Lr */
271 "str PC, [r0, #0x40]\n\t" /* context->Pc */
273 "str r1, [r0, #0x44]\n\t" /* context->Cpsr */
278 /***********************************************************************
281 * Set the new CPU context.
283 void DECLSPEC_HIDDEN
set_cpu_context( const CONTEXT
*context
);
284 __ASM_GLOBAL_FUNC( set_cpu_context
,
286 "ldr r1, [r0, #0x44]\n\t" /* context->Cpsr */
288 "ldr r1, [r0, #0x40]\n\t" /* context->Pc */
289 "ldr lr, [r0, #0x3c]\n\t" /* context->Lr */
290 "ldr sp, [r0, #0x38]\n\t" /* context->Sp */
292 "ldmib r0, {r0-r12}\n\t" /* context->R0..R12 */
296 /***********************************************************************
297 * get_server_context_flags
299 * Convert CPU-specific flags to generic server flags
301 static unsigned int get_server_context_flags( DWORD flags
)
303 unsigned int ret
= 0;
305 flags
&= ~CONTEXT_ARM
; /* get rid of CPU id */
306 if (flags
& CONTEXT_CONTROL
) ret
|= SERVER_CTX_CONTROL
;
307 if (flags
& CONTEXT_INTEGER
) ret
|= SERVER_CTX_INTEGER
;
308 if (flags
& CONTEXT_FLOATING_POINT
) ret
|= SERVER_CTX_FLOATING_POINT
;
309 if (flags
& CONTEXT_DEBUG_REGISTERS
) ret
|= SERVER_CTX_DEBUG_REGISTERS
;
314 /***********************************************************************
317 * Copy a register context according to the flags.
319 static void copy_context( CONTEXT
*to
, const CONTEXT
*from
, DWORD flags
)
321 flags
&= ~CONTEXT_ARM
; /* get rid of CPU id */
322 if (flags
& CONTEXT_CONTROL
)
327 to
->Cpsr
= from
->Cpsr
;
329 if (flags
& CONTEXT_INTEGER
)
345 if (flags
& CONTEXT_FLOATING_POINT
)
347 to
->Fpscr
= from
->Fpscr
;
348 memcpy( to
->u
.D
, from
->u
.D
, sizeof(to
->u
.D
) );
353 /***********************************************************************
356 * Convert a register context to the server format.
358 NTSTATUS
context_to_server( context_t
*to
, const CONTEXT
*from
)
360 DWORD i
, flags
= from
->ContextFlags
& ~CONTEXT_ARM
; /* get rid of CPU id */
362 memset( to
, 0, sizeof(*to
) );
365 if (flags
& CONTEXT_CONTROL
)
367 to
->flags
|= SERVER_CTX_CONTROL
;
368 to
->ctl
.arm_regs
.sp
= from
->Sp
;
369 to
->ctl
.arm_regs
.lr
= from
->Lr
;
370 to
->ctl
.arm_regs
.pc
= from
->Pc
;
371 to
->ctl
.arm_regs
.cpsr
= from
->Cpsr
;
373 if (flags
& CONTEXT_INTEGER
)
375 to
->flags
|= SERVER_CTX_INTEGER
;
376 to
->integer
.arm_regs
.r
[0] = from
->R0
;
377 to
->integer
.arm_regs
.r
[1] = from
->R1
;
378 to
->integer
.arm_regs
.r
[2] = from
->R2
;
379 to
->integer
.arm_regs
.r
[3] = from
->R3
;
380 to
->integer
.arm_regs
.r
[4] = from
->R4
;
381 to
->integer
.arm_regs
.r
[5] = from
->R5
;
382 to
->integer
.arm_regs
.r
[6] = from
->R6
;
383 to
->integer
.arm_regs
.r
[7] = from
->R7
;
384 to
->integer
.arm_regs
.r
[8] = from
->R8
;
385 to
->integer
.arm_regs
.r
[9] = from
->R9
;
386 to
->integer
.arm_regs
.r
[10] = from
->R10
;
387 to
->integer
.arm_regs
.r
[11] = from
->R11
;
388 to
->integer
.arm_regs
.r
[12] = from
->R12
;
390 if (flags
& CONTEXT_FLOATING_POINT
)
392 to
->flags
|= SERVER_CTX_FLOATING_POINT
;
393 for (i
= 0; i
< 32; i
++) to
->fp
.arm_regs
.d
[i
] = from
->u
.D
[i
];
394 to
->fp
.arm_regs
.fpscr
= from
->Fpscr
;
396 if (flags
& CONTEXT_DEBUG_REGISTERS
)
398 to
->flags
|= SERVER_CTX_DEBUG_REGISTERS
;
399 for (i
= 0; i
< ARM_MAX_BREAKPOINTS
; i
++) to
->debug
.arm_regs
.bvr
[i
] = from
->Bvr
[i
];
400 for (i
= 0; i
< ARM_MAX_BREAKPOINTS
; i
++) to
->debug
.arm_regs
.bcr
[i
] = from
->Bcr
[i
];
401 for (i
= 0; i
< ARM_MAX_WATCHPOINTS
; i
++) to
->debug
.arm_regs
.wvr
[i
] = from
->Wvr
[i
];
402 for (i
= 0; i
< ARM_MAX_WATCHPOINTS
; i
++) to
->debug
.arm_regs
.wcr
[i
] = from
->Wcr
[i
];
404 return STATUS_SUCCESS
;
408 /***********************************************************************
409 * context_from_server
411 * Convert a register context from the server format.
413 NTSTATUS
context_from_server( CONTEXT
*to
, const context_t
*from
)
417 if (from
->cpu
!= CPU_ARM
) return STATUS_INVALID_PARAMETER
;
419 to
->ContextFlags
= CONTEXT_ARM
;
420 if (from
->flags
& SERVER_CTX_CONTROL
)
422 to
->ContextFlags
|= CONTEXT_CONTROL
;
423 to
->Sp
= from
->ctl
.arm_regs
.sp
;
424 to
->Lr
= from
->ctl
.arm_regs
.lr
;
425 to
->Pc
= from
->ctl
.arm_regs
.pc
;
426 to
->Cpsr
= from
->ctl
.arm_regs
.cpsr
;
428 if (from
->flags
& SERVER_CTX_INTEGER
)
430 to
->ContextFlags
|= CONTEXT_INTEGER
;
431 to
->R0
= from
->integer
.arm_regs
.r
[0];
432 to
->R1
= from
->integer
.arm_regs
.r
[1];
433 to
->R2
= from
->integer
.arm_regs
.r
[2];
434 to
->R3
= from
->integer
.arm_regs
.r
[3];
435 to
->R4
= from
->integer
.arm_regs
.r
[4];
436 to
->R5
= from
->integer
.arm_regs
.r
[5];
437 to
->R6
= from
->integer
.arm_regs
.r
[6];
438 to
->R7
= from
->integer
.arm_regs
.r
[7];
439 to
->R8
= from
->integer
.arm_regs
.r
[8];
440 to
->R9
= from
->integer
.arm_regs
.r
[9];
441 to
->R10
= from
->integer
.arm_regs
.r
[10];
442 to
->R11
= from
->integer
.arm_regs
.r
[11];
443 to
->R12
= from
->integer
.arm_regs
.r
[12];
445 if (from
->flags
& SERVER_CTX_FLOATING_POINT
)
447 to
->ContextFlags
|= CONTEXT_FLOATING_POINT
;
448 for (i
= 0; i
< 32; i
++) to
->u
.D
[i
] = from
->fp
.arm_regs
.d
[i
];
449 to
->Fpscr
= from
->fp
.arm_regs
.fpscr
;
451 if (from
->flags
& SERVER_CTX_DEBUG_REGISTERS
)
453 to
->ContextFlags
|= CONTEXT_DEBUG_REGISTERS
;
454 for (i
= 0; i
< ARM_MAX_BREAKPOINTS
; i
++) to
->Bvr
[i
] = from
->debug
.arm_regs
.bvr
[i
];
455 for (i
= 0; i
< ARM_MAX_BREAKPOINTS
; i
++) to
->Bcr
[i
] = from
->debug
.arm_regs
.bcr
[i
];
456 for (i
= 0; i
< ARM_MAX_WATCHPOINTS
; i
++) to
->Wvr
[i
] = from
->debug
.arm_regs
.wvr
[i
];
457 for (i
= 0; i
< ARM_MAX_WATCHPOINTS
; i
++) to
->Wcr
[i
] = from
->debug
.arm_regs
.wcr
[i
];
459 return STATUS_SUCCESS
;
462 /***********************************************************************
463 * NtSetContextThread (NTDLL.@)
464 * ZwSetContextThread (NTDLL.@)
466 NTSTATUS WINAPI
NtSetContextThread( HANDLE handle
, const CONTEXT
*context
)
470 context_t server_context
;
472 context_to_server( &server_context
, context
);
473 ret
= set_thread_context( handle
, &server_context
, &self
);
474 if (self
&& ret
== STATUS_SUCCESS
) set_cpu_context( context
);
479 /***********************************************************************
480 * NtGetContextThread (NTDLL.@)
481 * ZwGetContextThread (NTDLL.@)
483 NTSTATUS WINAPI
NtGetContextThread( HANDLE handle
, CONTEXT
*context
)
486 DWORD needed_flags
= context
->ContextFlags
;
487 BOOL self
= (handle
== GetCurrentThread());
491 context_t server_context
;
492 unsigned int server_flags
= get_server_context_flags( context
->ContextFlags
);
494 if ((ret
= get_thread_context( handle
, &server_context
, server_flags
, &self
))) return ret
;
495 if ((ret
= context_from_server( context
, &server_context
))) return ret
;
496 needed_flags
&= ~context
->ContextFlags
;
499 if (self
&& needed_flags
)
502 RtlCaptureContext( &ctx
);
503 copy_context( context
, &ctx
, ctx
.ContextFlags
& needed_flags
);
504 context
->ContextFlags
|= ctx
.ContextFlags
& needed_flags
;
506 return STATUS_SUCCESS
;
510 extern void raise_func_trampoline_thumb( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, raise_func func
);
511 __ASM_GLOBAL_FUNC( raise_func_trampoline_thumb
,
516 extern void raise_func_trampoline_arm( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, raise_func func
);
517 __ASM_GLOBAL_FUNC( raise_func_trampoline_arm
,
522 /***********************************************************************
523 * setup_exception_record
525 * Setup the exception record and context on the thread stack.
527 static EXCEPTION_RECORD
*setup_exception( ucontext_t
*sigcontext
, raise_func func
)
532 EXCEPTION_RECORD rec
;
534 DWORD exception_code
= 0;
536 stack
= (struct stack_layout
*)(SP_sig(sigcontext
) & ~3);
537 stack
--; /* push the stack_layout structure */
539 stack
->rec
.ExceptionRecord
= NULL
;
540 stack
->rec
.ExceptionCode
= exception_code
;
541 stack
->rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
542 stack
->rec
.ExceptionAddress
= (LPVOID
)PC_sig(sigcontext
);
543 stack
->rec
.NumberParameters
= 0;
545 save_context( &stack
->context
, sigcontext
);
547 /* now modify the sigcontext to return to the raise function */
548 SP_sig(sigcontext
) = (DWORD
)stack
;
549 if (CPSR_sig(sigcontext
) & 0x20)
550 PC_sig(sigcontext
) = (DWORD
)raise_func_trampoline_thumb
;
552 PC_sig(sigcontext
) = (DWORD
)raise_func_trampoline_arm
;
553 REGn_sig(0, sigcontext
) = (DWORD
)&stack
->rec
; /* first arg for raise_func */
554 REGn_sig(1, sigcontext
) = (DWORD
)&stack
->context
; /* second arg for raise_func */
555 REGn_sig(2, sigcontext
) = (DWORD
)func
; /* the raise_func as third arg for the trampoline */
561 /**********************************************************************
562 * raise_segv_exception
564 static void WINAPI
raise_segv_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
568 switch(rec
->ExceptionCode
)
570 case EXCEPTION_ACCESS_VIOLATION
:
571 if (rec
->NumberParameters
== 2)
573 if (!(rec
->ExceptionCode
= virtual_handle_fault( (void *)rec
->ExceptionInformation
[1],
574 rec
->ExceptionInformation
[0], FALSE
)))
579 status
= NtRaiseException( rec
, context
, TRUE
);
580 if (status
) raise_status( status
, rec
);
582 set_cpu_context( context
);
585 /**********************************************************************
586 * call_stack_handlers
588 * Call the stack handlers chain.
590 static NTSTATUS
call_stack_handlers( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
592 EXCEPTION_REGISTRATION_RECORD
*frame
, *dispatch
, *nested_frame
;
595 frame
= NtCurrentTeb()->Tib
.ExceptionList
;
597 while (frame
!= (EXCEPTION_REGISTRATION_RECORD
*)~0UL)
599 /* Check frame address */
600 if (!is_valid_frame( frame
))
602 rec
->ExceptionFlags
|= EH_STACK_INVALID
;
607 TRACE( "calling handler at %p code=%x flags=%x\n",
608 frame
->Handler
, rec
->ExceptionCode
, rec
->ExceptionFlags
);
609 res
= frame
->Handler( rec
, frame
, context
, &dispatch
);
610 TRACE( "handler at %p returned %x\n", frame
->Handler
, res
);
612 if (frame
== nested_frame
)
614 /* no longer nested */
616 rec
->ExceptionFlags
&= ~EH_NESTED_CALL
;
621 case ExceptionContinueExecution
:
622 if (!(rec
->ExceptionFlags
& EH_NONCONTINUABLE
)) return STATUS_SUCCESS
;
623 return STATUS_NONCONTINUABLE_EXCEPTION
;
624 case ExceptionContinueSearch
:
626 case ExceptionNestedException
:
627 if (nested_frame
< dispatch
) nested_frame
= dispatch
;
628 rec
->ExceptionFlags
|= EH_NESTED_CALL
;
631 return STATUS_INVALID_DISPOSITION
;
635 return STATUS_UNHANDLED_EXCEPTION
;
639 /*******************************************************************
642 * Implementation of NtRaiseException.
644 static NTSTATUS
raise_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, BOOL first_chance
)
652 TRACE( "code=%x flags=%x addr=%p pc=%08x tid=%04x\n",
653 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
,
654 context
->Pc
, GetCurrentThreadId() );
655 for (c
= 0; c
< rec
->NumberParameters
; c
++)
656 TRACE( " info[%d]=%08lx\n", c
, rec
->ExceptionInformation
[c
] );
657 if (rec
->ExceptionCode
== EXCEPTION_WINE_STUB
)
659 if (rec
->ExceptionInformation
[1] >> 16)
660 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
661 rec
->ExceptionAddress
,
662 (char*)rec
->ExceptionInformation
[0], (char*)rec
->ExceptionInformation
[1] );
664 MESSAGE( "wine: Call from %p to unimplemented function %s.%ld, aborting\n",
665 rec
->ExceptionAddress
,
666 (char*)rec
->ExceptionInformation
[0], rec
->ExceptionInformation
[1] );
670 TRACE( " r0=%08x r1=%08x r2=%08x r3=%08x r4=%08x r5=%08x\n",
671 context
->R0
, context
->R1
, context
->R2
, context
->R3
, context
->R4
, context
->R5
);
672 TRACE( " r6=%08x r7=%08x r8=%08x r9=%08x r10=%08x r11=%08x\n",
673 context
->R6
, context
->R7
, context
->R8
, context
->R9
, context
->R10
, context
->R11
);
674 TRACE( " r12=%08x sp=%08x lr=%08x pc=%08x cpsr=%08x\n",
675 context
->R12
, context
->Sp
, context
->Lr
, context
->Pc
, context
->Cpsr
);
678 status
= send_debug_event( rec
, TRUE
, context
);
679 if (status
== DBG_CONTINUE
|| status
== DBG_EXCEPTION_HANDLED
)
680 return STATUS_SUCCESS
;
682 if (call_vectored_handlers( rec
, context
) == EXCEPTION_CONTINUE_EXECUTION
)
683 return STATUS_SUCCESS
;
685 if ((status
= call_stack_handlers( rec
, context
)) != STATUS_UNHANDLED_EXCEPTION
)
689 /* last chance exception */
691 status
= send_debug_event( rec
, FALSE
, context
);
692 if (status
!= DBG_CONTINUE
)
694 if (rec
->ExceptionFlags
& EH_STACK_INVALID
)
695 ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
696 else if (rec
->ExceptionCode
== STATUS_NONCONTINUABLE_EXCEPTION
)
697 ERR("Process attempted to continue execution after noncontinuable exception.\n");
699 ERR("Unhandled exception code %x flags %x addr %p\n",
700 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
);
701 NtTerminateProcess( NtCurrentProcess(), rec
->ExceptionCode
);
703 return STATUS_SUCCESS
;
707 /**********************************************************************
710 * Handler for SIGSEGV and related errors.
712 static void segv_handler( int signal
, siginfo_t
*info
, void *ucontext
)
714 EXCEPTION_RECORD
*rec
;
715 ucontext_t
*context
= ucontext
;
717 /* check for page fault inside the thread stack */
718 if (get_trap_code(context
) == TRAP_ARM_PAGEFLT
&&
719 (char *)info
->si_addr
>= (char *)NtCurrentTeb()->DeallocationStack
&&
720 (char *)info
->si_addr
< (char *)NtCurrentTeb()->Tib
.StackBase
&&
721 virtual_handle_stack_fault( info
->si_addr
))
723 /* check if this was the last guard page */
724 if ((char *)info
->si_addr
< (char *)NtCurrentTeb()->DeallocationStack
+ 2*4096)
726 rec
= setup_exception( context
, raise_segv_exception
);
727 rec
->ExceptionCode
= EXCEPTION_STACK_OVERFLOW
;
732 rec
= setup_exception( context
, raise_segv_exception
);
733 if (rec
->ExceptionCode
== EXCEPTION_STACK_OVERFLOW
) return;
735 switch(get_trap_code(context
))
737 case TRAP_ARM_PRIVINFLT
: /* Invalid opcode exception */
738 rec
->ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
740 case TRAP_ARM_PAGEFLT
: /* Page fault */
741 rec
->ExceptionCode
= EXCEPTION_ACCESS_VIOLATION
;
742 rec
->NumberParameters
= 2;
743 rec
->ExceptionInformation
[0] = (get_error_code(context
) & 0x800) != 0;
744 rec
->ExceptionInformation
[1] = (ULONG_PTR
)info
->si_addr
;
746 case TRAP_ARM_ALIGNFLT
: /* Alignment check exception */
747 rec
->ExceptionCode
= EXCEPTION_DATATYPE_MISALIGNMENT
;
749 case TRAP_ARM_UNKNOWN
: /* Unknown fault code */
750 rec
->ExceptionCode
= EXCEPTION_ACCESS_VIOLATION
;
751 rec
->NumberParameters
= 2;
752 rec
->ExceptionInformation
[0] = 0;
753 rec
->ExceptionInformation
[1] = 0xffffffff;
756 ERR("Got unexpected trap %d\n", get_trap_code(context
));
757 rec
->ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
762 /**********************************************************************
765 * Handler for SIGTRAP.
767 static void trap_handler( int signal
, siginfo_t
*info
, void *ucontext
)
769 EXCEPTION_RECORD rec
;
773 switch ( info
->si_code
)
776 rec
.ExceptionCode
= EXCEPTION_SINGLE_STEP
;
780 rec
.ExceptionCode
= EXCEPTION_BREAKPOINT
;
784 save_context( &context
, ucontext
);
785 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
786 rec
.ExceptionRecord
= NULL
;
787 rec
.ExceptionAddress
= (LPVOID
)context
.Pc
;
788 rec
.NumberParameters
= 0;
789 status
= raise_exception( &rec
, &context
, TRUE
);
790 if (status
) raise_status( status
, &rec
);
791 restore_context( &context
, ucontext
);
794 /**********************************************************************
797 * Handler for SIGFPE.
799 static void fpe_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
801 EXCEPTION_RECORD rec
;
805 save_fpu( &context
, sigcontext
);
806 save_context( &context
, sigcontext
);
808 switch (siginfo
->si_code
& 0xffff )
812 rec
.ExceptionCode
= EXCEPTION_ARRAY_BOUNDS_EXCEEDED
;
817 rec
.ExceptionCode
= EXCEPTION_INT_DIVIDE_BY_ZERO
;
822 rec
.ExceptionCode
= EXCEPTION_INT_OVERFLOW
;
827 rec
.ExceptionCode
= EXCEPTION_FLT_DIVIDE_BY_ZERO
;
832 rec
.ExceptionCode
= EXCEPTION_FLT_OVERFLOW
;
837 rec
.ExceptionCode
= EXCEPTION_FLT_UNDERFLOW
;
842 rec
.ExceptionCode
= EXCEPTION_FLT_INEXACT_RESULT
;
849 rec
.ExceptionCode
= EXCEPTION_FLT_INVALID_OPERATION
;
852 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
853 rec
.ExceptionRecord
= NULL
;
854 rec
.ExceptionAddress
= (LPVOID
)context
.Pc
;
855 rec
.NumberParameters
= 0;
856 status
= raise_exception( &rec
, &context
, TRUE
);
857 if (status
) raise_status( status
, &rec
);
859 restore_context( &context
, sigcontext
);
860 restore_fpu( &context
, sigcontext
);
863 /**********************************************************************
866 * Handler for SIGINT.
868 static void int_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
870 if (!dispatch_signal(SIGINT
))
872 EXCEPTION_RECORD rec
;
876 save_context( &context
, sigcontext
);
877 rec
.ExceptionCode
= CONTROL_C_EXIT
;
878 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
879 rec
.ExceptionRecord
= NULL
;
880 rec
.ExceptionAddress
= (LPVOID
)context
.Pc
;
881 rec
.NumberParameters
= 0;
882 status
= raise_exception( &rec
, &context
, TRUE
);
883 if (status
) raise_status( status
, &rec
);
884 restore_context( &context
, sigcontext
);
889 /**********************************************************************
892 * Handler for SIGABRT.
894 static void abrt_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
896 EXCEPTION_RECORD rec
;
900 save_context( &context
, sigcontext
);
901 rec
.ExceptionCode
= EXCEPTION_WINE_ASSERTION
;
902 rec
.ExceptionFlags
= EH_NONCONTINUABLE
;
903 rec
.ExceptionRecord
= NULL
;
904 rec
.ExceptionAddress
= (LPVOID
)context
.Pc
;
905 rec
.NumberParameters
= 0;
906 status
= raise_exception( &rec
, &context
, TRUE
);
907 if (status
) raise_status( status
, &rec
);
908 restore_context( &context
, sigcontext
);
912 /**********************************************************************
915 * Handler for SIGQUIT.
917 static void quit_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
923 /**********************************************************************
926 * Handler for SIGUSR1, used to signal a thread that it got suspended.
928 static void usr1_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
932 save_context( &context
, sigcontext
);
933 wait_suspend( &context
);
934 restore_context( &context
, sigcontext
);
938 /***********************************************************************
939 * __wine_set_signal_handler (NTDLL.@)
941 int CDECL
__wine_set_signal_handler(unsigned int sig
, wine_signal_handler wsh
)
943 if (sig
>= ARRAY_SIZE(handlers
)) return -1;
944 if (handlers
[sig
] != NULL
) return -2;
950 /**********************************************************************
951 * signal_alloc_thread
953 NTSTATUS
signal_alloc_thread( TEB
**teb
)
955 static size_t sigstack_zero_bits
;
959 if (!sigstack_zero_bits
)
961 size_t min_size
= page_size
;
962 /* find the first power of two not smaller than min_size */
963 while ((1u << sigstack_zero_bits
) < min_size
) sigstack_zero_bits
++;
964 assert( sizeof(TEB
) <= min_size
);
967 size
= 1 << sigstack_zero_bits
;
969 if (!(status
= NtAllocateVirtualMemory( NtCurrentProcess(), (void **)teb
, sigstack_zero_bits
,
970 &size
, MEM_COMMIT
| MEM_TOP_DOWN
, PAGE_READWRITE
)))
972 (*teb
)->Tib
.Self
= &(*teb
)->Tib
;
973 (*teb
)->Tib
.ExceptionList
= (void *)~0UL;
979 /**********************************************************************
982 void signal_free_thread( TEB
*teb
)
986 NtFreeVirtualMemory( NtCurrentProcess(), (void **)&teb
, &size
, MEM_RELEASE
);
990 /**********************************************************************
993 void signal_init_thread( TEB
*teb
)
995 static BOOL init_done
;
999 pthread_key_create( &teb_key
, NULL
);
1003 #if defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_8A__)
1004 /* Win32/ARM applications expect the TEB pointer to be in the TPIDRURW register. */
1005 __asm__
__volatile__( "mcr p15, 0, %0, c13, c0, 2" : : "r" (teb
) );
1008 pthread_setspecific( teb_key
, teb
);
1012 /**********************************************************************
1013 * signal_init_process
1015 void signal_init_process(void)
1017 struct sigaction sig_act
;
1019 sig_act
.sa_mask
= server_block_set
;
1020 sig_act
.sa_flags
= SA_RESTART
| SA_SIGINFO
;
1022 sig_act
.sa_sigaction
= int_handler
;
1023 if (sigaction( SIGINT
, &sig_act
, NULL
) == -1) goto error
;
1024 sig_act
.sa_sigaction
= fpe_handler
;
1025 if (sigaction( SIGFPE
, &sig_act
, NULL
) == -1) goto error
;
1026 sig_act
.sa_sigaction
= abrt_handler
;
1027 if (sigaction( SIGABRT
, &sig_act
, NULL
) == -1) goto error
;
1028 sig_act
.sa_sigaction
= quit_handler
;
1029 if (sigaction( SIGQUIT
, &sig_act
, NULL
) == -1) goto error
;
1030 sig_act
.sa_sigaction
= usr1_handler
;
1031 if (sigaction( SIGUSR1
, &sig_act
, NULL
) == -1) goto error
;
1033 sig_act
.sa_sigaction
= segv_handler
;
1034 if (sigaction( SIGSEGV
, &sig_act
, NULL
) == -1) goto error
;
1035 if (sigaction( SIGILL
, &sig_act
, NULL
) == -1) goto error
;
1037 if (sigaction( SIGBUS
, &sig_act
, NULL
) == -1) goto error
;
1041 sig_act
.sa_sigaction
= trap_handler
;
1042 if (sigaction( SIGTRAP
, &sig_act
, NULL
) == -1) goto error
;
1047 perror("sigaction");
1052 /**********************************************************************
1053 * RtlAddFunctionTable (NTDLL.@)
1055 BOOLEAN CDECL
RtlAddFunctionTable( RUNTIME_FUNCTION
*table
, DWORD count
, DWORD addr
)
1057 FIXME( "%p %u %x: stub\n", table
, count
, addr
);
1062 /**********************************************************************
1063 * RtlDeleteFunctionTable (NTDLL.@)
1065 BOOLEAN CDECL
RtlDeleteFunctionTable( RUNTIME_FUNCTION
*table
)
1067 FIXME( "%p: stub\n", table
);
1071 /**********************************************************************
1072 * find_function_info
1074 static RUNTIME_FUNCTION
*find_function_info( ULONG_PTR pc
, HMODULE module
,
1075 RUNTIME_FUNCTION
*func
, ULONG size
)
1078 int max
= size
/sizeof(*func
) - 1;
1082 int pos
= (min
+ max
) / 2;
1083 DWORD begin
= (func
[pos
].BeginAddress
& ~1), end
;
1084 if (func
[pos
].u
.s
.Flag
)
1085 end
= begin
+ func
[pos
].u
.s
.FunctionLength
* 2;
1088 struct UNWIND_INFO
*info
;
1089 info
= (struct UNWIND_INFO
*)((char *)module
+ func
[pos
].u
.UnwindData
);
1090 end
= begin
+ info
->function_length
* 2;
1093 if ((char *)pc
< (char *)module
+ begin
) max
= pos
- 1;
1094 else if ((char *)pc
>= (char *)module
+ end
) min
= pos
+ 1;
1095 else return func
+ pos
;
1100 /**********************************************************************
1101 * RtlLookupFunctionEntry (NTDLL.@)
1103 PRUNTIME_FUNCTION WINAPI
RtlLookupFunctionEntry( ULONG_PTR pc
, DWORD
*base
,
1104 UNWIND_HISTORY_TABLE
*table
)
1107 RUNTIME_FUNCTION
*func
;
1110 /* FIXME: should use the history table to make things faster */
1112 if (LdrFindEntryForAddress( (void *)pc
, &module
))
1114 WARN( "module not found for %lx\n", pc
);
1117 if (!(func
= RtlImageDirectoryEntryToData( module
->BaseAddress
, TRUE
,
1118 IMAGE_DIRECTORY_ENTRY_EXCEPTION
, &size
)))
1120 WARN( "no exception table found in module %p pc %lx\n", module
->BaseAddress
, pc
);
1123 func
= find_function_info( pc
, module
->BaseAddress
, func
, size
);
1124 if (func
) *base
= (DWORD
)module
->BaseAddress
;
1128 /***********************************************************************
1129 * RtlUnwind (NTDLL.@)
1131 void WINAPI
RtlUnwind( void *endframe
, void *target_ip
, EXCEPTION_RECORD
*rec
, void *retval
)
1134 EXCEPTION_RECORD record
;
1135 EXCEPTION_REGISTRATION_RECORD
*frame
, *dispatch
;
1138 RtlCaptureContext( &context
);
1139 context
.R0
= (DWORD
)retval
;
1141 /* build an exception record, if we do not have one */
1144 record
.ExceptionCode
= STATUS_UNWIND
;
1145 record
.ExceptionFlags
= 0;
1146 record
.ExceptionRecord
= NULL
;
1147 record
.ExceptionAddress
= (void *)context
.Pc
;
1148 record
.NumberParameters
= 0;
1152 rec
->ExceptionFlags
|= EH_UNWINDING
| (endframe
? 0 : EH_EXIT_UNWIND
);
1154 TRACE( "code=%x flags=%x\n", rec
->ExceptionCode
, rec
->ExceptionFlags
);
1156 /* get chain of exception frames */
1157 frame
= NtCurrentTeb()->Tib
.ExceptionList
;
1158 while ((frame
!= (EXCEPTION_REGISTRATION_RECORD
*)~0UL) && (frame
!= endframe
))
1160 /* Check frame address */
1161 if (endframe
&& ((void*)frame
> endframe
))
1162 raise_status( STATUS_INVALID_UNWIND_TARGET
, rec
);
1164 if (!is_valid_frame( frame
)) raise_status( STATUS_BAD_STACK
, rec
);
1167 TRACE( "calling handler at %p code=%x flags=%x\n",
1168 frame
->Handler
, rec
->ExceptionCode
, rec
->ExceptionFlags
);
1169 res
= frame
->Handler(rec
, frame
, &context
, &dispatch
);
1170 TRACE( "handler at %p returned %x\n", frame
->Handler
, res
);
1174 case ExceptionContinueSearch
:
1176 case ExceptionCollidedUnwind
:
1180 raise_status( STATUS_INVALID_DISPOSITION
, rec
);
1183 frame
= __wine_pop_frame( frame
);
1187 /*******************************************************************
1188 * NtRaiseException (NTDLL.@)
1190 NTSTATUS WINAPI
NtRaiseException( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, BOOL first_chance
)
1192 NTSTATUS status
= raise_exception( rec
, context
, first_chance
);
1193 if (status
== STATUS_SUCCESS
) NtSetContextThread( GetCurrentThread(), context
);
1197 /***********************************************************************
1198 * RtlRaiseException (NTDLL.@)
1200 void WINAPI
RtlRaiseException( EXCEPTION_RECORD
*rec
)
1205 RtlCaptureContext( &context
);
1206 rec
->ExceptionAddress
= (LPVOID
)context
.Pc
;
1207 status
= raise_exception( rec
, &context
, TRUE
);
1208 if (status
) raise_status( status
, rec
);
1211 /*************************************************************************
1212 * RtlCaptureStackBackTrace (NTDLL.@)
1214 USHORT WINAPI
RtlCaptureStackBackTrace( ULONG skip
, ULONG count
, PVOID
*buffer
, ULONG
*hash
)
1216 FIXME( "(%d, %d, %p, %p) stub!\n", skip
, count
, buffer
, hash
);
1220 /***********************************************************************
1221 * call_thread_entry_point
1223 static void call_thread_entry_point( LPTHREAD_START_ROUTINE entry
, void *arg
)
1227 TRACE_(relay
)( "\1Starting thread proc %p (arg=%p)\n", entry
, arg
);
1228 RtlExitUserThread( entry( arg
));
1230 __EXCEPT(call_unhandled_exception_filter
)
1232 NtTerminateThread( GetCurrentThread(), GetExceptionCode() );
1235 abort(); /* should not be reached */
1238 extern void DECLSPEC_NORETURN
start_thread( LPTHREAD_START_ROUTINE entry
, void *arg
, BOOL suspend
,
1239 void *relay
, TEB
*teb
);
1240 __ASM_GLOBAL_FUNC( start_thread
,
1242 "push {r4-r12,lr}\n\t"
1243 /* store exit frame */
1244 "ldr r4, [sp, #40]\n\t" /* teb */
1245 "str sp, [r4, #0x1d4]\n\t" /* teb->SystemReserved2 */
1246 /* switch to thread stack */
1247 "ldr r4, [r4, #4]\n\t" /* teb->Tib.StackBase */
1248 "sub sp, r4, #0x1000\n\t"
1250 "bl " __ASM_NAME("attach_thread") "\n\t"
1252 /* clear the stack */
1253 "and r0, #~0xff0\n\t" /* round down to page size */
1254 "bl " __ASM_NAME("virtual_clear_thread_stack") "\n\t"
1255 /* switch to the initial context */
1257 "b " __ASM_NAME("set_cpu_context") )
1259 extern void DECLSPEC_NORETURN
call_thread_exit_func( int status
, void (*func
)(int), TEB
*teb
);
1260 __ASM_GLOBAL_FUNC( call_thread_exit_func
,
1262 "ldr r3, [r2, #0x1d4]\n\t" /* teb->SystemReserved2 */
1264 "str ip, [r2, #0x1d4]\n\t"
1269 /***********************************************************************
1270 * init_thread_context
1272 static void init_thread_context( CONTEXT
*context
, LPTHREAD_START_ROUTINE entry
, void *arg
, void *relay
)
1274 context
->R0
= (DWORD
)entry
;
1275 context
->R1
= (DWORD
)arg
;
1276 context
->Sp
= (DWORD
)NtCurrentTeb()->Tib
.StackBase
;
1277 context
->Pc
= (DWORD
)relay
;
1281 /***********************************************************************
1284 PCONTEXT DECLSPEC_HIDDEN
attach_thread( LPTHREAD_START_ROUTINE entry
, void *arg
,
1285 BOOL suspend
, void *relay
)
1291 CONTEXT context
= { CONTEXT_ALL
};
1293 init_thread_context( &context
, entry
, arg
, relay
);
1294 wait_suspend( &context
);
1295 ctx
= (CONTEXT
*)((ULONG_PTR
)context
.Sp
& ~15) - 1;
1300 ctx
= (CONTEXT
*)NtCurrentTeb()->Tib
.StackBase
- 1;
1301 init_thread_context( ctx
, entry
, arg
, relay
);
1303 ctx
->ContextFlags
= CONTEXT_FULL
;
1304 attach_dlls( ctx
, (void **)&ctx
->R0
);
1309 /***********************************************************************
1310 * signal_start_thread
1312 * Thread startup sequence:
1313 * signal_start_thread()
1314 * -> thread_startup()
1315 * -> call_thread_entry_point()
1317 void signal_start_thread( LPTHREAD_START_ROUTINE entry
, void *arg
, BOOL suspend
)
1319 start_thread( entry
, arg
, suspend
, call_thread_entry_point
, NtCurrentTeb() );
1322 /**********************************************************************
1323 * signal_start_process
1325 * Process startup sequence:
1326 * signal_start_process()
1327 * -> thread_startup()
1328 * -> kernel32_start_process()
1330 void signal_start_process( LPTHREAD_START_ROUTINE entry
, BOOL suspend
)
1332 start_thread( entry
, NtCurrentTeb()->Peb
, suspend
, kernel32_start_process
, NtCurrentTeb() );
1335 /***********************************************************************
1336 * signal_exit_thread
1338 void signal_exit_thread( int status
)
1340 call_thread_exit_func( status
, exit_thread
, NtCurrentTeb() );
1343 /***********************************************************************
1344 * signal_exit_process
1346 void signal_exit_process( int status
)
1348 call_thread_exit_func( status
, exit
, NtCurrentTeb() );
1351 /**********************************************************************
1352 * DbgBreakPoint (NTDLL.@)
1354 void WINAPI
DbgBreakPoint(void)
1356 kill(getpid(), SIGTRAP
);
1359 /**********************************************************************
1360 * DbgUserBreakPoint (NTDLL.@)
1362 void WINAPI
DbgUserBreakPoint(void)
1364 kill(getpid(), SIGTRAP
);
1367 /**********************************************************************
1368 * NtCurrentTeb (NTDLL.@)
1370 TEB
* WINAPI
NtCurrentTeb(void)
1372 return pthread_getspecific( teb_key
);
1375 #endif /* __arm__ */