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( int signal
, const ucontext_t
*sigcontext
)
148 enum arm_trap_code trap
= TRAP_sig(sigcontext
);
156 return TRAP_ARM_PRIVINFLT
;
158 return TRAP_ARM_PAGEFLT
;
160 return TRAP_ARM_ALIGNFLT
;
162 return TRAP_ARM_UNKNOWN
;
166 /***********************************************************************
169 * Get the error code for a signal.
171 static inline WORD
get_error_code( const ucontext_t
*sigcontext
)
174 return ERROR_sig(sigcontext
);
180 /***********************************************************************
183 static inline int dispatch_signal(unsigned int sig
)
185 if (handlers
[sig
] == NULL
) return 0;
186 return handlers
[sig
](sig
);
189 /*******************************************************************
192 static inline BOOL
is_valid_frame( void *frame
)
194 if ((ULONG_PTR
)frame
& 3) return FALSE
;
195 return (frame
>= NtCurrentTeb()->Tib
.StackLimit
&&
196 (void **)frame
< (void **)NtCurrentTeb()->Tib
.StackBase
- 1);
199 /***********************************************************************
202 * Set the register values from a sigcontext.
204 static void save_context( CONTEXT
*context
, const ucontext_t
*sigcontext
)
206 #define C(x) context->R##x = REGn_sig(x,sigcontext)
207 /* Save normal registers */
208 C(0); C(1); C(2); C(3); C(4); C(5); C(6); C(7); C(8); C(9); C(10);
211 context
->ContextFlags
= CONTEXT_FULL
;
212 context
->Sp
= SP_sig(sigcontext
); /* Stack pointer */
213 context
->Lr
= LR_sig(sigcontext
); /* Link register */
214 context
->Pc
= PC_sig(sigcontext
); /* Program Counter */
215 context
->Cpsr
= CPSR_sig(sigcontext
); /* Current State Register */
216 context
->R11
= FP_sig(sigcontext
); /* Frame pointer */
217 context
->R12
= IP_sig(sigcontext
); /* Intra-Procedure-call scratch register */
221 /***********************************************************************
224 * Build a sigcontext from the register values.
226 static void restore_context( const CONTEXT
*context
, ucontext_t
*sigcontext
)
228 #define C(x) REGn_sig(x,sigcontext) = context->R##x
229 /* Restore normal registers */
230 C(0); C(1); C(2); C(3); C(4); C(5); C(6); C(7); C(8); C(9); C(10);
233 SP_sig(sigcontext
) = context
->Sp
; /* Stack pointer */
234 LR_sig(sigcontext
) = context
->Lr
; /* Link register */
235 PC_sig(sigcontext
) = context
->Pc
; /* Program Counter */
236 CPSR_sig(sigcontext
) = context
->Cpsr
; /* Current State Register */
237 FP_sig(sigcontext
) = context
->R11
; /* Frame pointer */
238 IP_sig(sigcontext
) = context
->R12
; /* Intra-Procedure-call scratch register */
242 /***********************************************************************
245 * Set the FPU context from a sigcontext.
247 static inline void save_fpu( CONTEXT
*context
, const ucontext_t
*sigcontext
)
249 FIXME("not implemented\n");
253 /***********************************************************************
256 * Restore the FPU context to a sigcontext.
258 static inline void restore_fpu( CONTEXT
*context
, const ucontext_t
*sigcontext
)
260 FIXME("not implemented\n");
263 /**************************************************************************
266 * Incoming r4 contains words to allocate, converting to bytes then return
268 __ASM_GLOBAL_FUNC( __chkstk
, "lsl r4, r4, #2\n\t"
271 /***********************************************************************
272 * RtlCaptureContext (NTDLL.@)
274 /* FIXME: Use the Stack instead of the actual register values */
275 __ASM_STDCALL_FUNC( RtlCaptureContext
, 4,
277 "stmib r0, {r0-r12}\n\t" /* context->R0..R12 */
278 "mov r1, #0x0200000\n\t" /* CONTEXT_ARM */
279 "add r1, r1, #0x3\n\t" /* CONTEXT_FULL */
280 "str r1, [r0]\n\t" /* context->ContextFlags */
281 "str SP, [r0, #0x38]\n\t" /* context->Sp */
282 "str LR, [r0, #0x3c]\n\t" /* context->Lr */
283 "str PC, [r0, #0x40]\n\t" /* context->Pc */
285 "str r1, [r0, #0x44]\n\t" /* context->Cpsr */
290 /***********************************************************************
293 * Set the new CPU context.
295 void DECLSPEC_HIDDEN
set_cpu_context( const CONTEXT
*context
);
296 __ASM_GLOBAL_FUNC( set_cpu_context
,
298 "ldr r2, [r0, #0x44]\n\t" /* context->Cpsr */
299 "tst r2, #0x20\n\t" /* thumb? */
300 "ldr r1, [r0, #0x40]\n\t" /* context->Pc */
301 "orrne r1, r1, #1\n\t" /* Adjust PC according to thumb */
302 "biceq r1, r1, #1\n\t" /* Adjust PC according to arm */
304 "ldr lr, [r0, #0x3c]\n\t" /* context->Lr */
305 "ldr sp, [r0, #0x38]\n\t" /* context->Sp */
307 "ldmib r0, {r0-r12}\n\t" /* context->R0..R12 */
311 /***********************************************************************
312 * get_server_context_flags
314 * Convert CPU-specific flags to generic server flags
316 static unsigned int get_server_context_flags( DWORD flags
)
318 unsigned int ret
= 0;
320 flags
&= ~CONTEXT_ARM
; /* get rid of CPU id */
321 if (flags
& CONTEXT_CONTROL
) ret
|= SERVER_CTX_CONTROL
;
322 if (flags
& CONTEXT_INTEGER
) ret
|= SERVER_CTX_INTEGER
;
323 if (flags
& CONTEXT_FLOATING_POINT
) ret
|= SERVER_CTX_FLOATING_POINT
;
324 if (flags
& CONTEXT_DEBUG_REGISTERS
) ret
|= SERVER_CTX_DEBUG_REGISTERS
;
329 /***********************************************************************
332 * Copy a register context according to the flags.
334 static void copy_context( CONTEXT
*to
, const CONTEXT
*from
, DWORD flags
)
336 flags
&= ~CONTEXT_ARM
; /* get rid of CPU id */
337 if (flags
& CONTEXT_CONTROL
)
342 to
->Cpsr
= from
->Cpsr
;
344 if (flags
& CONTEXT_INTEGER
)
360 if (flags
& CONTEXT_FLOATING_POINT
)
362 to
->Fpscr
= from
->Fpscr
;
363 memcpy( to
->u
.D
, from
->u
.D
, sizeof(to
->u
.D
) );
368 /***********************************************************************
371 * Convert a register context to the server format.
373 NTSTATUS
context_to_server( context_t
*to
, const CONTEXT
*from
)
375 DWORD i
, flags
= from
->ContextFlags
& ~CONTEXT_ARM
; /* get rid of CPU id */
377 memset( to
, 0, sizeof(*to
) );
380 if (flags
& CONTEXT_CONTROL
)
382 to
->flags
|= SERVER_CTX_CONTROL
;
383 to
->ctl
.arm_regs
.sp
= from
->Sp
;
384 to
->ctl
.arm_regs
.lr
= from
->Lr
;
385 to
->ctl
.arm_regs
.pc
= from
->Pc
;
386 to
->ctl
.arm_regs
.cpsr
= from
->Cpsr
;
388 if (flags
& CONTEXT_INTEGER
)
390 to
->flags
|= SERVER_CTX_INTEGER
;
391 to
->integer
.arm_regs
.r
[0] = from
->R0
;
392 to
->integer
.arm_regs
.r
[1] = from
->R1
;
393 to
->integer
.arm_regs
.r
[2] = from
->R2
;
394 to
->integer
.arm_regs
.r
[3] = from
->R3
;
395 to
->integer
.arm_regs
.r
[4] = from
->R4
;
396 to
->integer
.arm_regs
.r
[5] = from
->R5
;
397 to
->integer
.arm_regs
.r
[6] = from
->R6
;
398 to
->integer
.arm_regs
.r
[7] = from
->R7
;
399 to
->integer
.arm_regs
.r
[8] = from
->R8
;
400 to
->integer
.arm_regs
.r
[9] = from
->R9
;
401 to
->integer
.arm_regs
.r
[10] = from
->R10
;
402 to
->integer
.arm_regs
.r
[11] = from
->R11
;
403 to
->integer
.arm_regs
.r
[12] = from
->R12
;
405 if (flags
& CONTEXT_FLOATING_POINT
)
407 to
->flags
|= SERVER_CTX_FLOATING_POINT
;
408 for (i
= 0; i
< 32; i
++) to
->fp
.arm_regs
.d
[i
] = from
->u
.D
[i
];
409 to
->fp
.arm_regs
.fpscr
= from
->Fpscr
;
411 if (flags
& CONTEXT_DEBUG_REGISTERS
)
413 to
->flags
|= SERVER_CTX_DEBUG_REGISTERS
;
414 for (i
= 0; i
< ARM_MAX_BREAKPOINTS
; i
++) to
->debug
.arm_regs
.bvr
[i
] = from
->Bvr
[i
];
415 for (i
= 0; i
< ARM_MAX_BREAKPOINTS
; i
++) to
->debug
.arm_regs
.bcr
[i
] = from
->Bcr
[i
];
416 for (i
= 0; i
< ARM_MAX_WATCHPOINTS
; i
++) to
->debug
.arm_regs
.wvr
[i
] = from
->Wvr
[i
];
417 for (i
= 0; i
< ARM_MAX_WATCHPOINTS
; i
++) to
->debug
.arm_regs
.wcr
[i
] = from
->Wcr
[i
];
419 return STATUS_SUCCESS
;
423 /***********************************************************************
424 * context_from_server
426 * Convert a register context from the server format.
428 NTSTATUS
context_from_server( CONTEXT
*to
, const context_t
*from
)
432 if (from
->cpu
!= CPU_ARM
) return STATUS_INVALID_PARAMETER
;
434 to
->ContextFlags
= CONTEXT_ARM
;
435 if (from
->flags
& SERVER_CTX_CONTROL
)
437 to
->ContextFlags
|= CONTEXT_CONTROL
;
438 to
->Sp
= from
->ctl
.arm_regs
.sp
;
439 to
->Lr
= from
->ctl
.arm_regs
.lr
;
440 to
->Pc
= from
->ctl
.arm_regs
.pc
;
441 to
->Cpsr
= from
->ctl
.arm_regs
.cpsr
;
443 if (from
->flags
& SERVER_CTX_INTEGER
)
445 to
->ContextFlags
|= CONTEXT_INTEGER
;
446 to
->R0
= from
->integer
.arm_regs
.r
[0];
447 to
->R1
= from
->integer
.arm_regs
.r
[1];
448 to
->R2
= from
->integer
.arm_regs
.r
[2];
449 to
->R3
= from
->integer
.arm_regs
.r
[3];
450 to
->R4
= from
->integer
.arm_regs
.r
[4];
451 to
->R5
= from
->integer
.arm_regs
.r
[5];
452 to
->R6
= from
->integer
.arm_regs
.r
[6];
453 to
->R7
= from
->integer
.arm_regs
.r
[7];
454 to
->R8
= from
->integer
.arm_regs
.r
[8];
455 to
->R9
= from
->integer
.arm_regs
.r
[9];
456 to
->R10
= from
->integer
.arm_regs
.r
[10];
457 to
->R11
= from
->integer
.arm_regs
.r
[11];
458 to
->R12
= from
->integer
.arm_regs
.r
[12];
460 if (from
->flags
& SERVER_CTX_FLOATING_POINT
)
462 to
->ContextFlags
|= CONTEXT_FLOATING_POINT
;
463 for (i
= 0; i
< 32; i
++) to
->u
.D
[i
] = from
->fp
.arm_regs
.d
[i
];
464 to
->Fpscr
= from
->fp
.arm_regs
.fpscr
;
466 if (from
->flags
& SERVER_CTX_DEBUG_REGISTERS
)
468 to
->ContextFlags
|= CONTEXT_DEBUG_REGISTERS
;
469 for (i
= 0; i
< ARM_MAX_BREAKPOINTS
; i
++) to
->Bvr
[i
] = from
->debug
.arm_regs
.bvr
[i
];
470 for (i
= 0; i
< ARM_MAX_BREAKPOINTS
; i
++) to
->Bcr
[i
] = from
->debug
.arm_regs
.bcr
[i
];
471 for (i
= 0; i
< ARM_MAX_WATCHPOINTS
; i
++) to
->Wvr
[i
] = from
->debug
.arm_regs
.wvr
[i
];
472 for (i
= 0; i
< ARM_MAX_WATCHPOINTS
; i
++) to
->Wcr
[i
] = from
->debug
.arm_regs
.wcr
[i
];
474 return STATUS_SUCCESS
;
477 /***********************************************************************
478 * NtSetContextThread (NTDLL.@)
479 * ZwSetContextThread (NTDLL.@)
481 NTSTATUS WINAPI
NtSetContextThread( HANDLE handle
, const CONTEXT
*context
)
485 context_t server_context
;
487 context_to_server( &server_context
, context
);
488 ret
= set_thread_context( handle
, &server_context
, &self
);
489 if (self
&& ret
== STATUS_SUCCESS
) set_cpu_context( context
);
494 /***********************************************************************
495 * NtGetContextThread (NTDLL.@)
496 * ZwGetContextThread (NTDLL.@)
498 NTSTATUS WINAPI
NtGetContextThread( HANDLE handle
, CONTEXT
*context
)
501 DWORD needed_flags
= context
->ContextFlags
;
502 BOOL self
= (handle
== GetCurrentThread());
506 context_t server_context
;
507 unsigned int server_flags
= get_server_context_flags( context
->ContextFlags
);
509 if ((ret
= get_thread_context( handle
, &server_context
, server_flags
, &self
))) return ret
;
510 if ((ret
= context_from_server( context
, &server_context
))) return ret
;
511 needed_flags
&= ~context
->ContextFlags
;
514 if (self
&& needed_flags
)
517 RtlCaptureContext( &ctx
);
518 copy_context( context
, &ctx
, ctx
.ContextFlags
& needed_flags
);
519 context
->ContextFlags
|= ctx
.ContextFlags
& needed_flags
;
521 return STATUS_SUCCESS
;
525 extern void raise_func_trampoline_thumb( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, raise_func func
);
526 __ASM_GLOBAL_FUNC( raise_func_trampoline_thumb
,
531 extern void raise_func_trampoline_arm( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, raise_func func
);
532 __ASM_GLOBAL_FUNC( raise_func_trampoline_arm
,
537 /***********************************************************************
538 * setup_exception_record
540 * Setup the exception record and context on the thread stack.
542 static EXCEPTION_RECORD
*setup_exception( ucontext_t
*sigcontext
, raise_func func
)
547 EXCEPTION_RECORD rec
;
549 DWORD exception_code
= 0;
551 stack
= (struct stack_layout
*)(SP_sig(sigcontext
) & ~3);
552 stack
--; /* push the stack_layout structure */
554 stack
->rec
.ExceptionRecord
= NULL
;
555 stack
->rec
.ExceptionCode
= exception_code
;
556 stack
->rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
557 stack
->rec
.ExceptionAddress
= (LPVOID
)PC_sig(sigcontext
);
558 stack
->rec
.NumberParameters
= 0;
560 save_context( &stack
->context
, sigcontext
);
562 /* now modify the sigcontext to return to the raise function */
563 SP_sig(sigcontext
) = (DWORD
)stack
;
564 if (CPSR_sig(sigcontext
) & 0x20)
565 PC_sig(sigcontext
) = (DWORD
)raise_func_trampoline_thumb
;
567 PC_sig(sigcontext
) = (DWORD
)raise_func_trampoline_arm
;
568 REGn_sig(0, sigcontext
) = (DWORD
)&stack
->rec
; /* first arg for raise_func */
569 REGn_sig(1, sigcontext
) = (DWORD
)&stack
->context
; /* second arg for raise_func */
570 REGn_sig(2, sigcontext
) = (DWORD
)func
; /* the raise_func as third arg for the trampoline */
576 /**********************************************************************
577 * raise_segv_exception
579 static void WINAPI
raise_segv_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
583 switch(rec
->ExceptionCode
)
585 case EXCEPTION_ACCESS_VIOLATION
:
586 if (rec
->NumberParameters
== 2)
588 if (!(rec
->ExceptionCode
= virtual_handle_fault( (void *)rec
->ExceptionInformation
[1],
589 rec
->ExceptionInformation
[0], FALSE
)))
594 status
= NtRaiseException( rec
, context
, TRUE
);
595 if (status
) raise_status( status
, rec
);
597 set_cpu_context( context
);
600 /**********************************************************************
601 * call_stack_handlers
603 * Call the stack handlers chain.
605 static NTSTATUS
call_stack_handlers( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
607 EXCEPTION_REGISTRATION_RECORD
*frame
, *dispatch
, *nested_frame
;
610 frame
= NtCurrentTeb()->Tib
.ExceptionList
;
612 while (frame
!= (EXCEPTION_REGISTRATION_RECORD
*)~0UL)
614 /* Check frame address */
615 if (!is_valid_frame( frame
))
617 rec
->ExceptionFlags
|= EH_STACK_INVALID
;
622 TRACE( "calling handler at %p code=%x flags=%x\n",
623 frame
->Handler
, rec
->ExceptionCode
, rec
->ExceptionFlags
);
624 res
= frame
->Handler( rec
, frame
, context
, &dispatch
);
625 TRACE( "handler at %p returned %x\n", frame
->Handler
, res
);
627 if (frame
== nested_frame
)
629 /* no longer nested */
631 rec
->ExceptionFlags
&= ~EH_NESTED_CALL
;
636 case ExceptionContinueExecution
:
637 if (!(rec
->ExceptionFlags
& EH_NONCONTINUABLE
)) return STATUS_SUCCESS
;
638 return STATUS_NONCONTINUABLE_EXCEPTION
;
639 case ExceptionContinueSearch
:
641 case ExceptionNestedException
:
642 if (nested_frame
< dispatch
) nested_frame
= dispatch
;
643 rec
->ExceptionFlags
|= EH_NESTED_CALL
;
646 return STATUS_INVALID_DISPOSITION
;
650 return STATUS_UNHANDLED_EXCEPTION
;
654 /*******************************************************************
657 * Implementation of NtRaiseException.
659 static NTSTATUS
raise_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, BOOL first_chance
)
667 TRACE( "code=%x flags=%x addr=%p pc=%08x tid=%04x\n",
668 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
,
669 context
->Pc
, GetCurrentThreadId() );
670 for (c
= 0; c
< rec
->NumberParameters
; c
++)
671 TRACE( " info[%d]=%08lx\n", c
, rec
->ExceptionInformation
[c
] );
672 if (rec
->ExceptionCode
== EXCEPTION_WINE_STUB
)
674 if (rec
->ExceptionInformation
[1] >> 16)
675 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
676 rec
->ExceptionAddress
,
677 (char*)rec
->ExceptionInformation
[0], (char*)rec
->ExceptionInformation
[1] );
679 MESSAGE( "wine: Call from %p to unimplemented function %s.%ld, aborting\n",
680 rec
->ExceptionAddress
,
681 (char*)rec
->ExceptionInformation
[0], rec
->ExceptionInformation
[1] );
685 TRACE( " r0=%08x r1=%08x r2=%08x r3=%08x r4=%08x r5=%08x\n",
686 context
->R0
, context
->R1
, context
->R2
, context
->R3
, context
->R4
, context
->R5
);
687 TRACE( " r6=%08x r7=%08x r8=%08x r9=%08x r10=%08x r11=%08x\n",
688 context
->R6
, context
->R7
, context
->R8
, context
->R9
, context
->R10
, context
->R11
);
689 TRACE( " r12=%08x sp=%08x lr=%08x pc=%08x cpsr=%08x\n",
690 context
->R12
, context
->Sp
, context
->Lr
, context
->Pc
, context
->Cpsr
);
693 status
= send_debug_event( rec
, TRUE
, context
);
694 if (status
== DBG_CONTINUE
|| status
== DBG_EXCEPTION_HANDLED
)
695 return STATUS_SUCCESS
;
697 if (call_vectored_handlers( rec
, context
) == EXCEPTION_CONTINUE_EXECUTION
)
698 return STATUS_SUCCESS
;
700 if ((status
= call_stack_handlers( rec
, context
)) != STATUS_UNHANDLED_EXCEPTION
)
704 /* last chance exception */
706 status
= send_debug_event( rec
, FALSE
, context
);
707 if (status
!= DBG_CONTINUE
)
709 if (rec
->ExceptionFlags
& EH_STACK_INVALID
)
710 ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
711 else if (rec
->ExceptionCode
== STATUS_NONCONTINUABLE_EXCEPTION
)
712 ERR("Process attempted to continue execution after noncontinuable exception.\n");
714 ERR("Unhandled exception code %x flags %x addr %p\n",
715 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
);
716 NtTerminateProcess( NtCurrentProcess(), rec
->ExceptionCode
);
718 return STATUS_SUCCESS
;
722 /**********************************************************************
725 * Handler for SIGSEGV and related errors.
727 static void segv_handler( int signal
, siginfo_t
*info
, void *ucontext
)
729 EXCEPTION_RECORD
*rec
;
730 ucontext_t
*context
= ucontext
;
732 /* check for page fault inside the thread stack */
733 if (get_trap_code(signal
, context
) == TRAP_ARM_PAGEFLT
&&
734 (char *)info
->si_addr
>= (char *)NtCurrentTeb()->DeallocationStack
&&
735 (char *)info
->si_addr
< (char *)NtCurrentTeb()->Tib
.StackBase
&&
736 virtual_handle_stack_fault( info
->si_addr
))
738 /* check if this was the last guard page */
739 if ((char *)info
->si_addr
< (char *)NtCurrentTeb()->DeallocationStack
+ 2*4096)
741 rec
= setup_exception( context
, raise_segv_exception
);
742 rec
->ExceptionCode
= EXCEPTION_STACK_OVERFLOW
;
747 rec
= setup_exception( context
, raise_segv_exception
);
748 if (rec
->ExceptionCode
== EXCEPTION_STACK_OVERFLOW
) return;
750 switch(get_trap_code(signal
, context
))
752 case TRAP_ARM_PRIVINFLT
: /* Invalid opcode exception */
753 rec
->ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
755 case TRAP_ARM_PAGEFLT
: /* Page fault */
756 rec
->ExceptionCode
= EXCEPTION_ACCESS_VIOLATION
;
757 rec
->NumberParameters
= 2;
758 rec
->ExceptionInformation
[0] = (get_error_code(context
) & 0x800) != 0;
759 rec
->ExceptionInformation
[1] = (ULONG_PTR
)info
->si_addr
;
761 case TRAP_ARM_ALIGNFLT
: /* Alignment check exception */
762 rec
->ExceptionCode
= EXCEPTION_DATATYPE_MISALIGNMENT
;
764 case TRAP_ARM_UNKNOWN
: /* Unknown fault code */
765 rec
->ExceptionCode
= EXCEPTION_ACCESS_VIOLATION
;
766 rec
->NumberParameters
= 2;
767 rec
->ExceptionInformation
[0] = 0;
768 rec
->ExceptionInformation
[1] = 0xffffffff;
771 ERR("Got unexpected trap %d\n", get_trap_code(signal
, context
));
772 rec
->ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
777 /**********************************************************************
780 * Handler for SIGTRAP.
782 static void trap_handler( int signal
, siginfo_t
*info
, void *ucontext
)
784 EXCEPTION_RECORD rec
;
788 switch ( info
->si_code
)
791 rec
.ExceptionCode
= EXCEPTION_SINGLE_STEP
;
795 rec
.ExceptionCode
= EXCEPTION_BREAKPOINT
;
799 save_context( &context
, ucontext
);
800 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
801 rec
.ExceptionRecord
= NULL
;
802 rec
.ExceptionAddress
= (LPVOID
)context
.Pc
;
803 rec
.NumberParameters
= 0;
804 status
= raise_exception( &rec
, &context
, TRUE
);
805 if (status
) raise_status( status
, &rec
);
806 restore_context( &context
, ucontext
);
809 /**********************************************************************
812 * Handler for SIGFPE.
814 static void fpe_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
816 EXCEPTION_RECORD rec
;
820 save_fpu( &context
, sigcontext
);
821 save_context( &context
, sigcontext
);
823 switch (siginfo
->si_code
& 0xffff )
827 rec
.ExceptionCode
= EXCEPTION_ARRAY_BOUNDS_EXCEEDED
;
832 rec
.ExceptionCode
= EXCEPTION_INT_DIVIDE_BY_ZERO
;
837 rec
.ExceptionCode
= EXCEPTION_INT_OVERFLOW
;
842 rec
.ExceptionCode
= EXCEPTION_FLT_DIVIDE_BY_ZERO
;
847 rec
.ExceptionCode
= EXCEPTION_FLT_OVERFLOW
;
852 rec
.ExceptionCode
= EXCEPTION_FLT_UNDERFLOW
;
857 rec
.ExceptionCode
= EXCEPTION_FLT_INEXACT_RESULT
;
864 rec
.ExceptionCode
= EXCEPTION_FLT_INVALID_OPERATION
;
867 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
868 rec
.ExceptionRecord
= NULL
;
869 rec
.ExceptionAddress
= (LPVOID
)context
.Pc
;
870 rec
.NumberParameters
= 0;
871 status
= raise_exception( &rec
, &context
, TRUE
);
872 if (status
) raise_status( status
, &rec
);
874 restore_context( &context
, sigcontext
);
875 restore_fpu( &context
, sigcontext
);
878 /**********************************************************************
881 * Handler for SIGINT.
883 static void int_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
885 if (!dispatch_signal(SIGINT
))
887 EXCEPTION_RECORD rec
;
891 save_context( &context
, sigcontext
);
892 rec
.ExceptionCode
= CONTROL_C_EXIT
;
893 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
894 rec
.ExceptionRecord
= NULL
;
895 rec
.ExceptionAddress
= (LPVOID
)context
.Pc
;
896 rec
.NumberParameters
= 0;
897 status
= raise_exception( &rec
, &context
, TRUE
);
898 if (status
) raise_status( status
, &rec
);
899 restore_context( &context
, sigcontext
);
904 /**********************************************************************
907 * Handler for SIGABRT.
909 static void abrt_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
911 EXCEPTION_RECORD rec
;
915 save_context( &context
, sigcontext
);
916 rec
.ExceptionCode
= EXCEPTION_WINE_ASSERTION
;
917 rec
.ExceptionFlags
= EH_NONCONTINUABLE
;
918 rec
.ExceptionRecord
= NULL
;
919 rec
.ExceptionAddress
= (LPVOID
)context
.Pc
;
920 rec
.NumberParameters
= 0;
921 status
= raise_exception( &rec
, &context
, TRUE
);
922 if (status
) raise_status( status
, &rec
);
923 restore_context( &context
, sigcontext
);
927 /**********************************************************************
930 * Handler for SIGQUIT.
932 static void quit_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
938 /**********************************************************************
941 * Handler for SIGUSR1, used to signal a thread that it got suspended.
943 static void usr1_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
947 save_context( &context
, sigcontext
);
948 wait_suspend( &context
);
949 restore_context( &context
, sigcontext
);
953 /***********************************************************************
954 * __wine_set_signal_handler (NTDLL.@)
956 int CDECL
__wine_set_signal_handler(unsigned int sig
, wine_signal_handler wsh
)
958 if (sig
>= ARRAY_SIZE(handlers
)) return -1;
959 if (handlers
[sig
] != NULL
) return -2;
965 /**********************************************************************
966 * signal_alloc_thread
968 NTSTATUS
signal_alloc_thread( TEB
**teb
)
970 static size_t sigstack_zero_bits
;
974 if (!sigstack_zero_bits
)
976 size_t min_size
= page_size
;
977 /* find the first power of two not smaller than min_size */
978 while ((1u << sigstack_zero_bits
) < min_size
) sigstack_zero_bits
++;
979 assert( sizeof(TEB
) <= min_size
);
982 size
= 1 << sigstack_zero_bits
;
984 if (!(status
= NtAllocateVirtualMemory( NtCurrentProcess(), (void **)teb
, sigstack_zero_bits
,
985 &size
, MEM_COMMIT
| MEM_TOP_DOWN
, PAGE_READWRITE
)))
987 (*teb
)->Tib
.Self
= &(*teb
)->Tib
;
988 (*teb
)->Tib
.ExceptionList
= (void *)~0UL;
994 /**********************************************************************
997 void signal_free_thread( TEB
*teb
)
1001 NtFreeVirtualMemory( NtCurrentProcess(), (void **)&teb
, &size
, MEM_RELEASE
);
1005 /**********************************************************************
1006 * signal_init_thread
1008 void signal_init_thread( TEB
*teb
)
1010 static BOOL init_done
;
1014 pthread_key_create( &teb_key
, NULL
);
1018 #if defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_8A__)
1019 /* Win32/ARM applications expect the TEB pointer to be in the TPIDRURW register. */
1020 __asm__
__volatile__( "mcr p15, 0, %0, c13, c0, 2" : : "r" (teb
) );
1023 pthread_setspecific( teb_key
, teb
);
1027 /**********************************************************************
1028 * signal_init_process
1030 void signal_init_process(void)
1032 struct sigaction sig_act
;
1034 sig_act
.sa_mask
= server_block_set
;
1035 sig_act
.sa_flags
= SA_RESTART
| SA_SIGINFO
;
1037 sig_act
.sa_sigaction
= int_handler
;
1038 if (sigaction( SIGINT
, &sig_act
, NULL
) == -1) goto error
;
1039 sig_act
.sa_sigaction
= fpe_handler
;
1040 if (sigaction( SIGFPE
, &sig_act
, NULL
) == -1) goto error
;
1041 sig_act
.sa_sigaction
= abrt_handler
;
1042 if (sigaction( SIGABRT
, &sig_act
, NULL
) == -1) goto error
;
1043 sig_act
.sa_sigaction
= quit_handler
;
1044 if (sigaction( SIGQUIT
, &sig_act
, NULL
) == -1) goto error
;
1045 sig_act
.sa_sigaction
= usr1_handler
;
1046 if (sigaction( SIGUSR1
, &sig_act
, NULL
) == -1) goto error
;
1048 sig_act
.sa_sigaction
= segv_handler
;
1049 if (sigaction( SIGSEGV
, &sig_act
, NULL
) == -1) goto error
;
1050 if (sigaction( SIGILL
, &sig_act
, NULL
) == -1) goto error
;
1052 if (sigaction( SIGBUS
, &sig_act
, NULL
) == -1) goto error
;
1056 sig_act
.sa_sigaction
= trap_handler
;
1057 if (sigaction( SIGTRAP
, &sig_act
, NULL
) == -1) goto error
;
1062 perror("sigaction");
1067 /**********************************************************************
1068 * RtlAddFunctionTable (NTDLL.@)
1070 BOOLEAN CDECL
RtlAddFunctionTable( RUNTIME_FUNCTION
*table
, DWORD count
, DWORD addr
)
1072 FIXME( "%p %u %x: stub\n", table
, count
, addr
);
1076 /**********************************************************************
1077 * RtlInstallFunctionTableCallback (NTDLL.@)
1079 BOOLEAN CDECL
RtlInstallFunctionTableCallback( DWORD table
, DWORD base
, DWORD length
,
1080 PGET_RUNTIME_FUNCTION_CALLBACK callback
, PVOID context
, PCWSTR dll
)
1082 FIXME( "%x %x %d %p %p %s: stub\n", table
, base
, length
, callback
, context
, wine_dbgstr_w(dll
) );
1086 /*************************************************************************
1087 * RtlAddGrowableFunctionTable (NTDLL.@)
1089 DWORD WINAPI
RtlAddGrowableFunctionTable( void **table
, RUNTIME_FUNCTION
*functions
, DWORD count
, DWORD max_count
,
1090 ULONG_PTR base
, ULONG_PTR end
)
1092 FIXME( "(%p, %p, %d, %d, %ld, %ld) stub!\n", table
, functions
, count
, max_count
, base
, end
);
1093 if (table
) *table
= NULL
;
1094 return STATUS_SUCCESS
;
1097 /*************************************************************************
1098 * RtlGrowFunctionTable (NTDLL.@)
1100 void WINAPI
RtlGrowFunctionTable( void *table
, DWORD count
)
1102 FIXME( "(%p, %d) stub!\n", table
, count
);
1105 /*************************************************************************
1106 * RtlDeleteGrowableFunctionTable (NTDLL.@)
1108 void WINAPI
RtlDeleteGrowableFunctionTable( void *table
)
1110 FIXME( "(%p) stub!\n", table
);
1113 /**********************************************************************
1114 * RtlDeleteFunctionTable (NTDLL.@)
1116 BOOLEAN CDECL
RtlDeleteFunctionTable( RUNTIME_FUNCTION
*table
)
1118 FIXME( "%p: stub\n", table
);
1122 /**********************************************************************
1123 * find_function_info
1125 static RUNTIME_FUNCTION
*find_function_info( ULONG_PTR pc
, HMODULE module
,
1126 RUNTIME_FUNCTION
*func
, ULONG size
)
1129 int max
= size
/sizeof(*func
) - 1;
1133 int pos
= (min
+ max
) / 2;
1134 DWORD begin
= (func
[pos
].BeginAddress
& ~1), end
;
1135 if (func
[pos
].u
.s
.Flag
)
1136 end
= begin
+ func
[pos
].u
.s
.FunctionLength
* 2;
1139 struct UNWIND_INFO
*info
;
1140 info
= (struct UNWIND_INFO
*)((char *)module
+ func
[pos
].u
.UnwindData
);
1141 end
= begin
+ info
->function_length
* 2;
1144 if ((char *)pc
< (char *)module
+ begin
) max
= pos
- 1;
1145 else if ((char *)pc
>= (char *)module
+ end
) min
= pos
+ 1;
1146 else return func
+ pos
;
1151 /**********************************************************************
1152 * RtlLookupFunctionEntry (NTDLL.@)
1154 PRUNTIME_FUNCTION WINAPI
RtlLookupFunctionEntry( ULONG_PTR pc
, DWORD
*base
,
1155 UNWIND_HISTORY_TABLE
*table
)
1158 RUNTIME_FUNCTION
*func
;
1161 /* FIXME: should use the history table to make things faster */
1163 if (LdrFindEntryForAddress( (void *)pc
, &module
))
1165 WARN( "module not found for %lx\n", pc
);
1168 if (!(func
= RtlImageDirectoryEntryToData( module
->BaseAddress
, TRUE
,
1169 IMAGE_DIRECTORY_ENTRY_EXCEPTION
, &size
)))
1171 WARN( "no exception table found in module %p pc %lx\n", module
->BaseAddress
, pc
);
1174 func
= find_function_info( pc
, module
->BaseAddress
, func
, size
);
1175 if (func
) *base
= (DWORD
)module
->BaseAddress
;
1179 /***********************************************************************
1180 * RtlUnwind (NTDLL.@)
1182 void WINAPI
RtlUnwind( void *endframe
, void *target_ip
, EXCEPTION_RECORD
*rec
, void *retval
)
1185 EXCEPTION_RECORD record
;
1186 EXCEPTION_REGISTRATION_RECORD
*frame
, *dispatch
;
1189 RtlCaptureContext( &context
);
1190 context
.R0
= (DWORD
)retval
;
1192 /* build an exception record, if we do not have one */
1195 record
.ExceptionCode
= STATUS_UNWIND
;
1196 record
.ExceptionFlags
= 0;
1197 record
.ExceptionRecord
= NULL
;
1198 record
.ExceptionAddress
= (void *)context
.Pc
;
1199 record
.NumberParameters
= 0;
1203 rec
->ExceptionFlags
|= EH_UNWINDING
| (endframe
? 0 : EH_EXIT_UNWIND
);
1205 TRACE( "code=%x flags=%x\n", rec
->ExceptionCode
, rec
->ExceptionFlags
);
1207 /* get chain of exception frames */
1208 frame
= NtCurrentTeb()->Tib
.ExceptionList
;
1209 while ((frame
!= (EXCEPTION_REGISTRATION_RECORD
*)~0UL) && (frame
!= endframe
))
1211 /* Check frame address */
1212 if (endframe
&& ((void*)frame
> endframe
))
1213 raise_status( STATUS_INVALID_UNWIND_TARGET
, rec
);
1215 if (!is_valid_frame( frame
)) raise_status( STATUS_BAD_STACK
, rec
);
1218 TRACE( "calling handler at %p code=%x flags=%x\n",
1219 frame
->Handler
, rec
->ExceptionCode
, rec
->ExceptionFlags
);
1220 res
= frame
->Handler(rec
, frame
, &context
, &dispatch
);
1221 TRACE( "handler at %p returned %x\n", frame
->Handler
, res
);
1225 case ExceptionContinueSearch
:
1227 case ExceptionCollidedUnwind
:
1231 raise_status( STATUS_INVALID_DISPOSITION
, rec
);
1234 frame
= __wine_pop_frame( frame
);
1238 /*******************************************************************
1239 * NtRaiseException (NTDLL.@)
1241 NTSTATUS WINAPI
NtRaiseException( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, BOOL first_chance
)
1243 NTSTATUS status
= raise_exception( rec
, context
, first_chance
);
1244 if (status
== STATUS_SUCCESS
) NtSetContextThread( GetCurrentThread(), context
);
1248 /***********************************************************************
1249 * RtlRaiseException (NTDLL.@)
1251 void WINAPI
RtlRaiseException( EXCEPTION_RECORD
*rec
)
1256 RtlCaptureContext( &context
);
1257 rec
->ExceptionAddress
= (LPVOID
)context
.Pc
;
1258 status
= raise_exception( rec
, &context
, TRUE
);
1259 if (status
) raise_status( status
, rec
);
1262 /*************************************************************************
1263 * RtlCaptureStackBackTrace (NTDLL.@)
1265 USHORT WINAPI
RtlCaptureStackBackTrace( ULONG skip
, ULONG count
, PVOID
*buffer
, ULONG
*hash
)
1267 FIXME( "(%d, %d, %p, %p) stub!\n", skip
, count
, buffer
, hash
);
1271 /***********************************************************************
1272 * call_thread_entry_point
1274 static void call_thread_entry_point( LPTHREAD_START_ROUTINE entry
, void *arg
)
1278 TRACE_(relay
)( "\1Starting thread proc %p (arg=%p)\n", entry
, arg
);
1279 RtlExitUserThread( entry( arg
));
1281 __EXCEPT(call_unhandled_exception_filter
)
1283 NtTerminateThread( GetCurrentThread(), GetExceptionCode() );
1286 abort(); /* should not be reached */
1289 extern void DECLSPEC_NORETURN
start_thread( LPTHREAD_START_ROUTINE entry
, void *arg
, BOOL suspend
,
1290 void *relay
, TEB
*teb
);
1291 __ASM_GLOBAL_FUNC( start_thread
,
1293 "push {r4-r12,lr}\n\t"
1294 /* store exit frame */
1295 "ldr r4, [sp, #40]\n\t" /* teb */
1296 "str sp, [r4, #0x1d4]\n\t" /* teb->SystemReserved2 */
1297 /* switch to thread stack */
1298 "ldr r4, [r4, #4]\n\t" /* teb->Tib.StackBase */
1299 "sub sp, r4, #0x1000\n\t"
1301 "bl " __ASM_NAME("attach_thread") "\n\t"
1303 /* clear the stack */
1304 "and r0, #~0xff0\n\t" /* round down to page size */
1305 "bl " __ASM_NAME("virtual_clear_thread_stack") "\n\t"
1306 /* switch to the initial context */
1308 "b " __ASM_NAME("set_cpu_context") )
1310 extern void DECLSPEC_NORETURN
call_thread_exit_func( int status
, void (*func
)(int), TEB
*teb
);
1311 __ASM_GLOBAL_FUNC( call_thread_exit_func
,
1313 "ldr r3, [r2, #0x1d4]\n\t" /* teb->SystemReserved2 */
1315 "str ip, [r2, #0x1d4]\n\t"
1320 /***********************************************************************
1321 * init_thread_context
1323 static void init_thread_context( CONTEXT
*context
, LPTHREAD_START_ROUTINE entry
, void *arg
, void *relay
)
1325 context
->R0
= (DWORD
)entry
;
1326 context
->R1
= (DWORD
)arg
;
1327 context
->Sp
= (DWORD
)NtCurrentTeb()->Tib
.StackBase
;
1328 context
->Pc
= (DWORD
)relay
;
1332 /***********************************************************************
1335 PCONTEXT DECLSPEC_HIDDEN
attach_thread( LPTHREAD_START_ROUTINE entry
, void *arg
,
1336 BOOL suspend
, void *relay
)
1342 CONTEXT context
= { CONTEXT_ALL
};
1344 init_thread_context( &context
, entry
, arg
, relay
);
1345 wait_suspend( &context
);
1346 ctx
= (CONTEXT
*)((ULONG_PTR
)context
.Sp
& ~15) - 1;
1351 ctx
= (CONTEXT
*)NtCurrentTeb()->Tib
.StackBase
- 1;
1352 init_thread_context( ctx
, entry
, arg
, relay
);
1354 ctx
->ContextFlags
= CONTEXT_FULL
;
1355 LdrInitializeThunk( ctx
, (void **)&ctx
->R0
, 0, 0 );
1360 /***********************************************************************
1361 * signal_start_thread
1363 * Thread startup sequence:
1364 * signal_start_thread()
1365 * -> thread_startup()
1366 * -> call_thread_entry_point()
1368 void signal_start_thread( LPTHREAD_START_ROUTINE entry
, void *arg
, BOOL suspend
)
1370 start_thread( entry
, arg
, suspend
, call_thread_entry_point
, NtCurrentTeb() );
1373 /**********************************************************************
1374 * signal_start_process
1376 * Process startup sequence:
1377 * signal_start_process()
1378 * -> thread_startup()
1379 * -> kernel32_start_process()
1381 void signal_start_process( LPTHREAD_START_ROUTINE entry
, BOOL suspend
)
1383 start_thread( entry
, NtCurrentTeb()->Peb
, suspend
, kernel32_start_process
, NtCurrentTeb() );
1386 /***********************************************************************
1387 * signal_exit_thread
1389 void signal_exit_thread( int status
)
1391 call_thread_exit_func( status
, exit_thread
, NtCurrentTeb() );
1394 /***********************************************************************
1395 * signal_exit_process
1397 void signal_exit_process( int status
)
1399 call_thread_exit_func( status
, exit
, NtCurrentTeb() );
1402 /**********************************************************************
1403 * DbgBreakPoint (NTDLL.@)
1405 void WINAPI
DbgBreakPoint(void)
1407 kill(getpid(), SIGTRAP
);
1410 /**********************************************************************
1411 * DbgUserBreakPoint (NTDLL.@)
1413 void WINAPI
DbgUserBreakPoint(void)
1415 kill(getpid(), SIGTRAP
);
1418 /**********************************************************************
1419 * NtCurrentTeb (NTDLL.@)
1421 TEB
* WINAPI
NtCurrentTeb(void)
1423 return pthread_getspecific( teb_key
);
1426 #endif /* __arm__ */