Sweden-Number/dlls/ntdll/signal_arm64.c

1181 lines
39 KiB
C
Raw Normal View History

2013-01-16 00:50:30 +01:00
/*
* ARM64 signal handling routines
*
* Copyright 2010-2013 André Hentschel
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#ifdef __aarch64__
#include "config.h"
#include "wine/port.h"
#include <assert.h>
#include <signal.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#ifdef HAVE_SYS_PARAM_H
# include <sys/param.h>
#endif
#ifdef HAVE_SYSCALL_H
# include <syscall.h>
#else
# ifdef HAVE_SYS_SYSCALL_H
# include <sys/syscall.h>
# endif
#endif
#ifdef HAVE_SYS_SIGNAL_H
# include <sys/signal.h>
#endif
#ifdef HAVE_SYS_UCONTEXT_H
# include <sys/ucontext.h>
#endif
2013-01-16 00:50:30 +01:00
#define NONAMELESSUNION
#define NONAMELESSSTRUCT
2013-01-16 00:50:30 +01:00
#include "ntstatus.h"
#define WIN32_NO_STATUS
#include "windef.h"
#include "winternl.h"
#include "wine/library.h"
#include "wine/exception.h"
#include "ntdll_misc.h"
#include "wine/debug.h"
#include "winnt.h"
WINE_DEFAULT_DEBUG_CHANNEL(seh);
WINE_DECLARE_DEBUG_CHANNEL(relay);
2013-01-16 00:50:30 +01:00
static pthread_key_t teb_key;
/***********************************************************************
* signal context platform-specific definitions
*/
#ifdef linux
/* All Registers access - only for local access */
# define REG_sig(reg_name, context) ((context)->uc_mcontext.reg_name)
# define REGn_sig(reg_num, context) ((context)->uc_mcontext.regs[reg_num])
/* Special Registers access */
# define SP_sig(context) REG_sig(sp, context) /* Stack pointer */
# define PC_sig(context) REG_sig(pc, context) /* Program counter */
# define PSTATE_sig(context) REG_sig(pstate, context) /* Current State Register */
# define FP_sig(context) REGn_sig(29, context) /* Frame pointer */
# define LR_sig(context) REGn_sig(30, context) /* Link Register */
static struct _aarch64_ctx *get_extended_sigcontext( ucontext_t *sigcontext, unsigned int magic )
{
struct _aarch64_ctx *ctx = (struct _aarch64_ctx *)sigcontext->uc_mcontext.__reserved;
while ((char *)ctx < (char *)(&sigcontext->uc_mcontext + 1) && ctx->magic && ctx->size)
{
if (ctx->magic == magic) return ctx;
ctx = (struct _aarch64_ctx *)((char *)ctx + ctx->size);
}
return NULL;
}
static struct fpsimd_context *get_fpsimd_context( ucontext_t *sigcontext )
{
return (struct fpsimd_context *)get_extended_sigcontext( sigcontext, FPSIMD_MAGIC );
}
2013-01-16 00:50:30 +01:00
static DWORD64 get_fault_esr( ucontext_t *sigcontext )
{
struct esr_context *esr = (struct esr_context *)get_extended_sigcontext( sigcontext, ESR_MAGIC );
if (esr) return esr->esr;
return 0;
}
2013-01-16 00:50:30 +01:00
#endif /* linux */
static const size_t teb_size = 0x2000; /* we reserve two pages for the TEB */
static size_t signal_stack_size;
typedef void (WINAPI *raise_func)( EXCEPTION_RECORD *rec, CONTEXT *context );
typedef int (*wine_signal_handler)(unsigned int sig);
static wine_signal_handler handlers[256];
struct arm64_thread_data
{
void *exit_frame; /* exit frame pointer */
CONTEXT *context; /* context to set with SIGUSR2 */
};
C_ASSERT( sizeof(struct arm64_thread_data) <= sizeof(((TEB *)0)->SystemReserved2) );
C_ASSERT( offsetof( TEB, SystemReserved2 ) + offsetof( struct arm64_thread_data, exit_frame ) == 0x300 );
static inline struct arm64_thread_data *arm64_thread_data(void)
{
return (struct arm64_thread_data *)NtCurrentTeb()->SystemReserved2;
}
2013-01-16 00:50:30 +01:00
/***********************************************************************
* dispatch_signal
*/
static inline int dispatch_signal(unsigned int sig)
{
if (handlers[sig] == NULL) return 0;
return handlers[sig](sig);
}
/*******************************************************************
* is_valid_frame
*/
static inline BOOL is_valid_frame( void *frame )
{
if ((ULONG_PTR)frame & 3) return FALSE;
return (frame >= NtCurrentTeb()->Tib.StackLimit &&
(void **)frame < (void **)NtCurrentTeb()->Tib.StackBase - 1);
}
/***********************************************************************
* save_context
*
* Set the register values from a sigcontext.
*/
static void save_context( CONTEXT *context, const ucontext_t *sigcontext )
2013-01-16 00:50:30 +01:00
{
DWORD i;
2013-01-16 00:50:30 +01:00
context->ContextFlags = CONTEXT_FULL;
context->u.s.Fp = FP_sig(sigcontext); /* Frame pointer */
context->u.s.Lr = LR_sig(sigcontext); /* Link register */
2013-01-16 00:50:30 +01:00
context->Sp = SP_sig(sigcontext); /* Stack pointer */
context->Pc = PC_sig(sigcontext); /* Program Counter */
context->Cpsr = PSTATE_sig(sigcontext); /* Current State Register */
for (i = 0; i <= 28; i++) context->u.X[i] = REGn_sig( i, sigcontext );
2013-01-16 00:50:30 +01:00
}
/***********************************************************************
* restore_context
*
* Build a sigcontext from the register values.
*/
static void restore_context( const CONTEXT *context, ucontext_t *sigcontext )
2013-01-16 00:50:30 +01:00
{
DWORD i;
2013-01-16 00:50:30 +01:00
FP_sig(sigcontext) = context->u.s.Fp; /* Frame pointer */
LR_sig(sigcontext) = context->u.s.Lr; /* Link register */
2013-01-16 00:50:30 +01:00
SP_sig(sigcontext) = context->Sp; /* Stack pointer */
PC_sig(sigcontext) = context->Pc; /* Program Counter */
PSTATE_sig(sigcontext) = context->Cpsr; /* Current State Register */
for (i = 0; i <= 28; i++) REGn_sig( i, sigcontext ) = context->u.X[i];
2013-01-16 00:50:30 +01:00
}
/***********************************************************************
* save_fpu
*
* Set the FPU context from a sigcontext.
*/
static void save_fpu( CONTEXT *context, ucontext_t *sigcontext )
2013-01-16 00:50:30 +01:00
{
struct fpsimd_context *fp = get_fpsimd_context( sigcontext );
if (!fp) return;
context->ContextFlags |= CONTEXT_FLOATING_POINT;
context->Fpcr = fp->fpcr;
context->Fpsr = fp->fpsr;
memcpy( context->V, fp->vregs, sizeof(context->V) );
2013-01-16 00:50:30 +01:00
}
/***********************************************************************
* restore_fpu
*
* Restore the FPU context to a sigcontext.
*/
static void restore_fpu( CONTEXT *context, ucontext_t *sigcontext )
2013-01-16 00:50:30 +01:00
{
struct fpsimd_context *fp = get_fpsimd_context( sigcontext );
if (!fp) return;
fp->fpcr = context->Fpcr;
fp->fpsr = context->Fpsr;
memcpy( fp->vregs, context->V, sizeof(fp->vregs) );
2013-01-16 00:50:30 +01:00
}
/***********************************************************************
* RtlCaptureContext (NTDLL.@)
*/
__ASM_STDCALL_FUNC( RtlCaptureContext, 8,
"stp x1, x2, [x0, #0x10]\n\t" /* context->X1,X2 */
"stp x3, x4, [x0, #0x20]\n\t" /* context->X3,X4 */
"stp x5, x6, [x0, #0x30]\n\t" /* context->X5,X6 */
"stp x7, x8, [x0, #0x40]\n\t" /* context->X7,X8 */
"stp x9, x10, [x0, #0x50]\n\t" /* context->X9,X10 */
"stp x11, x12, [x0, #0x60]\n\t" /* context->X11,X12 */
"stp x13, x14, [x0, #0x70]\n\t" /* context->X13,X14 */
"stp x15, x16, [x0, #0x80]\n\t" /* context->X15,X16 */
"stp x17, x18, [x0, #0x90]\n\t" /* context->X17,X18 */
"stp x19, x20, [x0, #0xa0]\n\t" /* context->X19,X20 */
"stp x21, x22, [x0, #0xb0]\n\t" /* context->X21,X22 */
"stp x23, x24, [x0, #0xc0]\n\t" /* context->X23,X24 */
"stp x25, x26, [x0, #0xd0]\n\t" /* context->X25,X26 */
"stp x27, x28, [x0, #0xe0]\n\t" /* context->X27,X28 */
"ldp x1, x2, [x29]\n\t"
"stp x1, x2, [x0, #0xf0]\n\t" /* context->Fp,Lr */
"add x1, x29, #0x10\n\t"
"stp x1, x30, [x0, #0x100]\n\t" /* context->Sp,Pc */
"mov w1, #0x400000\n\t" /* CONTEXT_ARM64 */
"add w1, w1, #0x3\n\t" /* CONTEXT_FULL */
"str w1, [x0]\n\t" /* context->ContextFlags */
"mrs x1, NZCV\n\t"
"str w1, [x0, #0x4]\n\t" /* context->Cpsr */
"ret" )
2013-01-16 00:50:30 +01:00
/***********************************************************************
* set_cpu_context
*
* Set the new CPU context.
*/
static void set_cpu_context( const CONTEXT *context )
2013-01-16 00:50:30 +01:00
{
interlocked_xchg_ptr( (void **)&arm64_thread_data()->context, (void *)context );
raise( SIGUSR2 );
2013-01-16 00:50:30 +01:00
}
/***********************************************************************
* get_server_context_flags
*
* Convert CPU-specific flags to generic server flags
*/
static unsigned int get_server_context_flags( DWORD flags )
{
unsigned int ret = 0;
flags &= ~CONTEXT_ARM64; /* get rid of CPU id */
if (flags & CONTEXT_CONTROL) ret |= SERVER_CTX_CONTROL;
if (flags & CONTEXT_INTEGER) ret |= SERVER_CTX_INTEGER;
if (flags & CONTEXT_FLOATING_POINT) ret |= SERVER_CTX_FLOATING_POINT;
if (flags & CONTEXT_DEBUG_REGISTERS) ret |= SERVER_CTX_DEBUG_REGISTERS;
return ret;
}
2013-01-16 00:50:30 +01:00
/***********************************************************************
* copy_context
*
* Copy a register context according to the flags.
*/
static void copy_context( CONTEXT *to, const CONTEXT *from, DWORD flags )
2013-01-16 00:50:30 +01:00
{
flags &= ~CONTEXT_ARM64; /* get rid of CPU id */
if (flags & CONTEXT_CONTROL)
{
to->u.s.Fp = from->u.s.Fp;
to->u.s.Lr = from->u.s.Lr;
2013-01-16 00:50:30 +01:00
to->Sp = from->Sp;
to->Pc = from->Pc;
to->Cpsr = from->Cpsr;
2013-01-16 00:50:30 +01:00
}
if (flags & CONTEXT_INTEGER)
{
memcpy( to->u.X, from->u.X, sizeof(to->u.X) );
2013-01-16 00:50:30 +01:00
}
if (flags & CONTEXT_FLOATING_POINT)
{
memcpy( to->V, from->V, sizeof(to->V) );
to->Fpcr = from->Fpcr;
to->Fpsr = from->Fpsr;
}
if (flags & CONTEXT_DEBUG_REGISTERS)
{
memcpy( to->Bcr, from->Bcr, sizeof(to->Bcr) );
memcpy( to->Bvr, from->Bvr, sizeof(to->Bvr) );
memcpy( to->Wcr, from->Wcr, sizeof(to->Wcr) );
memcpy( to->Wvr, from->Wvr, sizeof(to->Wvr) );
}
2013-01-16 00:50:30 +01:00
}
/***********************************************************************
* context_to_server
*
* Convert a register context to the server format.
*/
NTSTATUS context_to_server( context_t *to, const CONTEXT *from )
{
DWORD i, flags = from->ContextFlags & ~CONTEXT_ARM64; /* get rid of CPU id */
2013-01-16 00:50:30 +01:00
memset( to, 0, sizeof(*to) );
to->cpu = CPU_ARM64;
if (flags & CONTEXT_CONTROL)
{
to->flags |= SERVER_CTX_CONTROL;
to->integer.arm64_regs.x[29] = from->u.s.Fp;
to->integer.arm64_regs.x[30] = from->u.s.Lr;
2013-01-16 00:50:30 +01:00
to->ctl.arm64_regs.sp = from->Sp;
to->ctl.arm64_regs.pc = from->Pc;
to->ctl.arm64_regs.pstate = from->Cpsr;
2013-01-16 00:50:30 +01:00
}
if (flags & CONTEXT_INTEGER)
{
to->flags |= SERVER_CTX_INTEGER;
for (i = 0; i <= 28; i++) to->integer.arm64_regs.x[i] = from->u.X[i];
2013-01-16 00:50:30 +01:00
}
if (flags & CONTEXT_FLOATING_POINT)
{
to->flags |= SERVER_CTX_FLOATING_POINT;
for (i = 0; i < 64; i++) to->fp.arm64_regs.d[i] = from->V[i / 2].D[i % 2];
to->fp.arm64_regs.fpcr = from->Fpcr;
to->fp.arm64_regs.fpsr = from->Fpsr;
}
if (flags & CONTEXT_DEBUG_REGISTERS)
{
to->flags |= SERVER_CTX_DEBUG_REGISTERS;
for (i = 0; i < ARM64_MAX_BREAKPOINTS; i++) to->debug.arm64_regs.bcr[i] = from->Bcr[i];
for (i = 0; i < ARM64_MAX_BREAKPOINTS; i++) to->debug.arm64_regs.bvr[i] = from->Bvr[i];
for (i = 0; i < ARM64_MAX_WATCHPOINTS; i++) to->debug.arm64_regs.wcr[i] = from->Wcr[i];
for (i = 0; i < ARM64_MAX_WATCHPOINTS; i++) to->debug.arm64_regs.wvr[i] = from->Wvr[i];
}
2013-01-16 00:50:30 +01:00
return STATUS_SUCCESS;
}
/***********************************************************************
* context_from_server
*
* Convert a register context from the server format.
*/
NTSTATUS context_from_server( CONTEXT *to, const context_t *from )
{
DWORD i;
2013-01-16 00:50:30 +01:00
if (from->cpu != CPU_ARM64) return STATUS_INVALID_PARAMETER;
to->ContextFlags = CONTEXT_ARM64;
if (from->flags & SERVER_CTX_CONTROL)
{
to->ContextFlags |= CONTEXT_CONTROL;
to->u.s.Fp = from->integer.arm64_regs.x[29];
to->u.s.Lr = from->integer.arm64_regs.x[30];
2013-01-16 00:50:30 +01:00
to->Sp = from->ctl.arm64_regs.sp;
to->Pc = from->ctl.arm64_regs.pc;
to->Cpsr = from->ctl.arm64_regs.pstate;
2013-01-16 00:50:30 +01:00
}
if (from->flags & SERVER_CTX_INTEGER)
{
to->ContextFlags |= CONTEXT_INTEGER;
for (i = 0; i <= 28; i++) to->u.X[i] = from->integer.arm64_regs.x[i];
}
if (from->flags & SERVER_CTX_FLOATING_POINT)
{
to->ContextFlags |= CONTEXT_FLOATING_POINT;
for (i = 0; i < 64; i++) to->V[i / 2].D[i % 2] = from->fp.arm64_regs.d[i];
to->Fpcr = from->fp.arm64_regs.fpcr;
to->Fpsr = from->fp.arm64_regs.fpsr;
}
if (from->flags & SERVER_CTX_DEBUG_REGISTERS)
{
to->ContextFlags |= CONTEXT_DEBUG_REGISTERS;
for (i = 0; i < ARM64_MAX_BREAKPOINTS; i++) to->Bcr[i] = from->debug.arm64_regs.bcr[i];
for (i = 0; i < ARM64_MAX_BREAKPOINTS; i++) to->Bvr[i] = from->debug.arm64_regs.bvr[i];
for (i = 0; i < ARM64_MAX_WATCHPOINTS; i++) to->Wcr[i] = from->debug.arm64_regs.wcr[i];
for (i = 0; i < ARM64_MAX_WATCHPOINTS; i++) to->Wvr[i] = from->debug.arm64_regs.wvr[i];
}
2013-01-16 00:50:30 +01:00
return STATUS_SUCCESS;
}
/***********************************************************************
* NtSetContextThread (NTDLL.@)
* ZwSetContextThread (NTDLL.@)
*/
NTSTATUS WINAPI NtSetContextThread( HANDLE handle, const CONTEXT *context )
{
NTSTATUS ret;
BOOL self;
context_t server_context;
context_to_server( &server_context, context );
ret = set_thread_context( handle, &server_context, &self );
if (self && ret == STATUS_SUCCESS) set_cpu_context( context );
return ret;
}
/***********************************************************************
* NtGetContextThread (NTDLL.@)
* ZwGetContextThread (NTDLL.@)
*/
NTSTATUS WINAPI NtGetContextThread( HANDLE handle, CONTEXT *context )
{
NTSTATUS ret;
DWORD needed_flags = context->ContextFlags;
BOOL self = (handle == GetCurrentThread());
if (!self)
{
context_t server_context;
unsigned int server_flags = get_server_context_flags( context->ContextFlags );
if ((ret = get_thread_context( handle, &server_context, server_flags, &self ))) return ret;
if ((ret = context_from_server( context, &server_context ))) return ret;
needed_flags &= ~context->ContextFlags;
}
if (self && needed_flags)
{
CONTEXT ctx;
RtlCaptureContext( &ctx );
copy_context( context, &ctx, ctx.ContextFlags & needed_flags );
context->ContextFlags |= ctx.ContextFlags & needed_flags;
}
return STATUS_SUCCESS;
}
2013-01-16 00:50:30 +01:00
/***********************************************************************
* setup_exception_record
*
* Setup the exception record and context on the thread stack.
*/
static EXCEPTION_RECORD *setup_exception( ucontext_t *sigcontext, raise_func func )
2013-01-16 00:50:30 +01:00
{
struct stack_layout
{
CONTEXT context;
EXCEPTION_RECORD rec;
void *redzone[2];
2013-01-16 00:50:30 +01:00
} *stack;
DWORD exception_code = 0;
/* push the stack_layout structure */
stack = (struct stack_layout *)((SP_sig(sigcontext) - sizeof(*stack)) & ~15);
2013-01-16 00:50:30 +01:00
stack->rec.ExceptionRecord = NULL;
stack->rec.ExceptionCode = exception_code;
stack->rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
stack->rec.ExceptionAddress = (LPVOID)PC_sig(sigcontext);
stack->rec.NumberParameters = 0;
save_context( &stack->context, sigcontext );
save_fpu( &stack->context, sigcontext );
2013-01-16 00:50:30 +01:00
/* now modify the sigcontext to return to the raise function */
SP_sig(sigcontext) = (ULONG_PTR)stack;
PC_sig(sigcontext) = (ULONG_PTR)func;
REGn_sig(0, sigcontext) = (ULONG_PTR)&stack->rec; /* first arg for raise_func */
REGn_sig(1, sigcontext) = (ULONG_PTR)&stack->context; /* second arg for raise_func */
return &stack->rec;
}
/**********************************************************************
* raise_segv_exception
*/
static void WINAPI raise_segv_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
{
NTSTATUS status;
switch(rec->ExceptionCode)
{
case EXCEPTION_ACCESS_VIOLATION:
if (rec->NumberParameters == 2)
{
if (!(rec->ExceptionCode = virtual_handle_fault( (void *)rec->ExceptionInformation[1],
rec->ExceptionInformation[0], FALSE )))
2013-01-16 00:50:30 +01:00
goto done;
}
break;
}
status = NtRaiseException( rec, context, TRUE );
raise_status( status, rec );
2013-01-16 00:50:30 +01:00
done:
set_cpu_context( context );
}
/**********************************************************************
* raise_trap_exception
*/
static void WINAPI raise_trap_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
{
NTSTATUS status;
if (rec->ExceptionCode == EXCEPTION_BREAKPOINT) context->Pc += 4;
status = NtRaiseException( rec, context, TRUE );
raise_status( status, rec );
}
/**********************************************************************
* raise_generic_exception
*/
static void WINAPI raise_generic_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
{
NTSTATUS status = NtRaiseException( rec, context, TRUE );
raise_status( status, rec );
}
2013-01-16 00:50:30 +01:00
/**********************************************************************
* call_stack_handlers
*
* Call the stack handlers chain.
*/
static NTSTATUS call_stack_handlers( EXCEPTION_RECORD *rec, CONTEXT *context )
{
EXCEPTION_REGISTRATION_RECORD *frame, *dispatch, *nested_frame;
DWORD res;
frame = NtCurrentTeb()->Tib.ExceptionList;
nested_frame = NULL;
while (frame != (EXCEPTION_REGISTRATION_RECORD*)~0UL)
{
/* Check frame address */
if (!is_valid_frame( frame ))
{
rec->ExceptionFlags |= EH_STACK_INVALID;
break;
}
/* Call handler */
TRACE( "calling handler at %p code=%x flags=%x\n",
frame->Handler, rec->ExceptionCode, rec->ExceptionFlags );
res = frame->Handler( rec, frame, context, &dispatch );
TRACE( "handler at %p returned %x\n", frame->Handler, res );
if (frame == nested_frame)
{
/* no longer nested */
nested_frame = NULL;
rec->ExceptionFlags &= ~EH_NESTED_CALL;
}
switch(res)
{
case ExceptionContinueExecution:
if (!(rec->ExceptionFlags & EH_NONCONTINUABLE)) return STATUS_SUCCESS;
return STATUS_NONCONTINUABLE_EXCEPTION;
case ExceptionContinueSearch:
break;
case ExceptionNestedException:
if (nested_frame < dispatch) nested_frame = dispatch;
rec->ExceptionFlags |= EH_NESTED_CALL;
break;
default:
return STATUS_INVALID_DISPOSITION;
}
frame = frame->Prev;
}
return STATUS_UNHANDLED_EXCEPTION;
}
/*******************************************************************
* raise_exception
*
* Implementation of NtRaiseException.
*/
static NTSTATUS raise_exception( EXCEPTION_RECORD *rec, CONTEXT *context, BOOL first_chance )
{
NTSTATUS status;
if (first_chance)
{
DWORD c;
TRACE( "code=%x flags=%x addr=%p pc=%lx tid=%04x\n",
rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
context->Pc, GetCurrentThreadId() );
2013-01-16 00:50:30 +01:00
for (c = 0; c < rec->NumberParameters; c++)
TRACE( " info[%d]=%016lx\n", c, rec->ExceptionInformation[c] );
if (rec->ExceptionCode == EXCEPTION_WINE_STUB)
{
if (rec->ExceptionInformation[1] >> 16)
MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
rec->ExceptionAddress,
(char*)rec->ExceptionInformation[0], (char*)rec->ExceptionInformation[1] );
else
MESSAGE( "wine: Call from %p to unimplemented function %s.%ld, aborting\n",
rec->ExceptionAddress,
(char*)rec->ExceptionInformation[0], rec->ExceptionInformation[1] );
}
else
{
TRACE(" x0=%016lx x1=%016lx x2=%016lx x3=%016lx\n",
context->u.s.X0, context->u.s.X1, context->u.s.X2, context->u.s.X3 );
TRACE(" x4=%016lx x5=%016lx x6=%016lx x7=%016lx\n",
context->u.s.X4, context->u.s.X5, context->u.s.X6, context->u.s.X7 );
TRACE(" x8=%016lx x9=%016lx x10=%016lx x11=%016lx\n",
context->u.s.X8, context->u.s.X9, context->u.s.X10, context->u.s.X11 );
TRACE(" x12=%016lx x13=%016lx x14=%016lx x15=%016lx\n",
context->u.s.X12, context->u.s.X13, context->u.s.X14, context->u.s.X15 );
TRACE(" x16=%016lx x17=%016lx x18=%016lx x19=%016lx\n",
context->u.s.X16, context->u.s.X17, context->u.s.X18, context->u.s.X19 );
TRACE(" x20=%016lx x21=%016lx x22=%016lx x23=%016lx\n",
context->u.s.X20, context->u.s.X21, context->u.s.X22, context->u.s.X23 );
TRACE(" x24=%016lx x25=%016lx x26=%016lx x27=%016lx\n",
context->u.s.X24, context->u.s.X25, context->u.s.X26, context->u.s.X27 );
TRACE(" x28=%016lx fp=%016lx lr=%016lx sp=%016lx\n",
context->u.s.X28, context->u.s.Fp, context->u.s.Lr, context->Sp );
TRACE(" pc=%016lx\n",
context->Pc );
2013-01-16 00:50:30 +01:00
}
status = send_debug_event( rec, TRUE, context );
if (status == DBG_CONTINUE || status == DBG_EXCEPTION_HANDLED)
return STATUS_SUCCESS;
if (call_vectored_handlers( rec, context ) == EXCEPTION_CONTINUE_EXECUTION)
return STATUS_SUCCESS;
if ((status = call_stack_handlers( rec, context )) != STATUS_UNHANDLED_EXCEPTION)
return status;
}
/* last chance exception */
status = send_debug_event( rec, FALSE, context );
if (status != DBG_CONTINUE)
{
if (rec->ExceptionFlags & EH_STACK_INVALID)
ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
else if (rec->ExceptionCode == STATUS_NONCONTINUABLE_EXCEPTION)
ERR("Process attempted to continue execution after noncontinuable exception.\n");
else
ERR("Unhandled exception code %x flags %x addr %p\n",
rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
NtTerminateProcess( NtCurrentProcess(), rec->ExceptionCode );
}
return STATUS_SUCCESS;
}
/**********************************************************************
* segv_handler
*
* Handler for SIGSEGV and related errors.
*/
static void segv_handler( int signal, siginfo_t *info, void *ucontext )
{
EXCEPTION_RECORD *rec;
ucontext_t *context = ucontext;
2013-01-16 00:50:30 +01:00
/* check for page fault inside the thread stack */
if (signal == SIGSEGV &&
(char *)info->si_addr >= (char *)NtCurrentTeb()->DeallocationStack &&
(char *)info->si_addr < (char *)NtCurrentTeb()->Tib.StackBase &&
virtual_handle_stack_fault( info->si_addr ))
{
/* check if this was the last guard page */
if ((char *)info->si_addr < (char *)NtCurrentTeb()->DeallocationStack + 2*4096)
{
rec = setup_exception( context, raise_segv_exception );
rec->ExceptionCode = EXCEPTION_STACK_OVERFLOW;
}
return;
}
rec = setup_exception( context, raise_segv_exception );
if (rec->ExceptionCode == EXCEPTION_STACK_OVERFLOW) return;
switch(signal)
{
case SIGILL: /* Invalid opcode exception */
rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
break;
case SIGSEGV: /* Segmentation fault */
rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
rec->NumberParameters = 2;
rec->ExceptionInformation[0] = (get_fault_esr( context ) & 0x40) != 0;
2013-01-16 00:50:30 +01:00
rec->ExceptionInformation[1] = (ULONG_PTR)info->si_addr;
break;
case SIGBUS: /* Alignment check exception */
rec->ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
break;
default:
ERR("Got unexpected signal %i\n", signal);
rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
break;
}
}
/**********************************************************************
* trap_handler
*
* Handler for SIGTRAP.
*/
static void trap_handler( int signal, siginfo_t *info, void *ucontext )
{
ucontext_t *context = ucontext;
EXCEPTION_RECORD *rec = setup_exception( context, raise_trap_exception );
2013-01-16 00:50:30 +01:00
switch (info->si_code)
2013-01-16 00:50:30 +01:00
{
case TRAP_TRACE:
rec->ExceptionCode = EXCEPTION_SINGLE_STEP;
2013-01-16 00:50:30 +01:00
break;
case TRAP_BRKPT:
default:
rec->ExceptionCode = EXCEPTION_BREAKPOINT;
2013-01-16 00:50:30 +01:00
break;
}
}
/**********************************************************************
* fpe_handler
*
* Handler for SIGFPE.
*/
static void fpe_handler( int signal, siginfo_t *siginfo, void *sigcontext )
{
EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_generic_exception );
2013-01-16 00:50:30 +01:00
switch (siginfo->si_code & 0xffff )
{
#ifdef FPE_FLTSUB
case FPE_FLTSUB:
rec->ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
2013-01-16 00:50:30 +01:00
break;
#endif
#ifdef FPE_INTDIV
case FPE_INTDIV:
rec->ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
2013-01-16 00:50:30 +01:00
break;
#endif
#ifdef FPE_INTOVF
case FPE_INTOVF:
rec->ExceptionCode = EXCEPTION_INT_OVERFLOW;
2013-01-16 00:50:30 +01:00
break;
#endif
#ifdef FPE_FLTDIV
case FPE_FLTDIV:
rec->ExceptionCode = EXCEPTION_FLT_DIVIDE_BY_ZERO;
2013-01-16 00:50:30 +01:00
break;
#endif
#ifdef FPE_FLTOVF
case FPE_FLTOVF:
rec->ExceptionCode = EXCEPTION_FLT_OVERFLOW;
2013-01-16 00:50:30 +01:00
break;
#endif
#ifdef FPE_FLTUND
case FPE_FLTUND:
rec->ExceptionCode = EXCEPTION_FLT_UNDERFLOW;
2013-01-16 00:50:30 +01:00
break;
#endif
#ifdef FPE_FLTRES
case FPE_FLTRES:
rec->ExceptionCode = EXCEPTION_FLT_INEXACT_RESULT;
2013-01-16 00:50:30 +01:00
break;
#endif
#ifdef FPE_FLTINV
case FPE_FLTINV:
#endif
default:
rec->ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
2013-01-16 00:50:30 +01:00
break;
}
}
/**********************************************************************
* int_handler
*
* Handler for SIGINT.
*/
static void int_handler( int signal, siginfo_t *siginfo, void *sigcontext )
{
if (!dispatch_signal(SIGINT))
{
EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_generic_exception );
rec->ExceptionCode = CONTROL_C_EXIT;
2013-01-16 00:50:30 +01:00
}
}
/**********************************************************************
* abrt_handler
*
* Handler for SIGABRT.
*/
static void abrt_handler( int signal, siginfo_t *siginfo, void *sigcontext )
{
EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_generic_exception );
2013-01-16 00:50:30 +01:00
rec->ExceptionCode = EXCEPTION_WINE_ASSERTION;
rec->ExceptionFlags = EH_NONCONTINUABLE;
2013-01-16 00:50:30 +01:00
}
/**********************************************************************
* quit_handler
*
* Handler for SIGQUIT.
*/
static void quit_handler( int signal, siginfo_t *siginfo, void *sigcontext )
{
abort_thread(0);
}
/**********************************************************************
* usr1_handler
*
* Handler for SIGUSR1, used to signal a thread that it got suspended.
*/
static void usr1_handler( int signal, siginfo_t *siginfo, void *sigcontext )
{
CONTEXT context;
save_context( &context, sigcontext );
wait_suspend( &context );
restore_context( &context, sigcontext );
}
/**********************************************************************
* usr2_handler
*
* Handler for SIGUSR2, used to set a thread context.
*/
static void usr2_handler( int signal, siginfo_t *siginfo, void *sigcontext )
{
CONTEXT *context = interlocked_xchg_ptr( (void **)&arm64_thread_data()->context, NULL );
if (!context) return;
if ((context->ContextFlags & ~CONTEXT_ARM64) & CONTEXT_FLOATING_POINT)
restore_fpu( context, sigcontext );
restore_context( context, sigcontext );
}
2013-01-16 00:50:30 +01:00
/***********************************************************************
* __wine_set_signal_handler (NTDLL.@)
*/
int CDECL __wine_set_signal_handler(unsigned int sig, wine_signal_handler wsh)
{
if (sig >= ARRAY_SIZE(handlers)) return -1;
2013-01-16 00:50:30 +01:00
if (handlers[sig] != NULL) return -2;
handlers[sig] = wsh;
return 0;
}
/**********************************************************************
* signal_alloc_thread
*/
NTSTATUS signal_alloc_thread( TEB **teb )
{
static size_t sigstack_alignment;
2013-01-16 00:50:30 +01:00
SIZE_T size;
NTSTATUS status;
if (!sigstack_alignment)
2013-01-16 00:50:30 +01:00
{
size_t min_size = teb_size + max( MINSIGSTKSZ, 8192 );
/* find the first power of two not smaller than min_size */
sigstack_alignment = 12;
while ((1u << sigstack_alignment) < min_size) sigstack_alignment++;
signal_stack_size = (1 << sigstack_alignment) - teb_size;
2013-01-16 00:50:30 +01:00
assert( sizeof(TEB) <= teb_size );
}
size = 1 << sigstack_alignment;
2013-01-16 00:50:30 +01:00
*teb = NULL;
if (!(status = virtual_alloc_aligned( (void **)teb, 0, &size, MEM_COMMIT | MEM_TOP_DOWN,
PAGE_READWRITE, sigstack_alignment )))
2013-01-16 00:50:30 +01:00
{
(*teb)->Tib.Self = &(*teb)->Tib;
(*teb)->Tib.ExceptionList = (void *)~0UL;
}
return status;
}
/**********************************************************************
* signal_free_thread
*/
void signal_free_thread( TEB *teb )
{
SIZE_T size = 0;
2013-01-16 00:50:30 +01:00
NtFreeVirtualMemory( NtCurrentProcess(), (void **)&teb, &size, MEM_RELEASE );
}
/**********************************************************************
* signal_init_thread
*/
void signal_init_thread( TEB *teb )
{
static BOOL init_done;
2013-01-16 00:50:30 +01:00
if (!init_done)
{
pthread_key_create( &teb_key, NULL );
init_done = TRUE;
2013-01-16 00:50:30 +01:00
}
/* Win64/ARM applications expect the TEB pointer to be in the x18 platform register. */
__asm__ __volatile__( "mov x18, %0" : : "r" (teb) );
2013-01-16 00:50:30 +01:00
pthread_setspecific( teb_key, teb );
}
/**********************************************************************
* signal_init_process
*/
void signal_init_process(void)
2013-01-16 00:50:30 +01:00
{
struct sigaction sig_act;
sig_act.sa_mask = server_block_set;
sig_act.sa_flags = SA_RESTART | SA_SIGINFO;
sig_act.sa_sigaction = int_handler;
if (sigaction( SIGINT, &sig_act, NULL ) == -1) goto error;
sig_act.sa_sigaction = fpe_handler;
if (sigaction( SIGFPE, &sig_act, NULL ) == -1) goto error;
sig_act.sa_sigaction = abrt_handler;
if (sigaction( SIGABRT, &sig_act, NULL ) == -1) goto error;
sig_act.sa_sigaction = quit_handler;
if (sigaction( SIGQUIT, &sig_act, NULL ) == -1) goto error;
sig_act.sa_sigaction = usr1_handler;
if (sigaction( SIGUSR1, &sig_act, NULL ) == -1) goto error;
sig_act.sa_sigaction = usr2_handler;
if (sigaction( SIGUSR2, &sig_act, NULL ) == -1) goto error;
2013-01-16 00:50:30 +01:00
sig_act.sa_sigaction = segv_handler;
if (sigaction( SIGSEGV, &sig_act, NULL ) == -1) goto error;
if (sigaction( SIGILL, &sig_act, NULL ) == -1) goto error;
#ifdef SIGBUS
if (sigaction( SIGBUS, &sig_act, NULL ) == -1) goto error;
#endif
#ifdef SIGTRAP
sig_act.sa_sigaction = trap_handler;
if (sigaction( SIGTRAP, &sig_act, NULL ) == -1) goto error;
#endif
return;
error:
perror("sigaction");
exit(1);
}
/***********************************************************************
* RtlUnwind (NTDLL.@)
*/
void WINAPI RtlUnwind( PVOID pEndFrame, PVOID targetIp, PEXCEPTION_RECORD pRecord, PVOID retval )
{
FIXME( "Not implemented on ARM64\n" );
}
/*******************************************************************
* NtRaiseException (NTDLL.@)
*/
NTSTATUS WINAPI NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context, BOOL first_chance )
{
NTSTATUS status = raise_exception( rec, context, first_chance );
if (status == STATUS_SUCCESS) NtSetContextThread( GetCurrentThread(), context );
return status;
}
/***********************************************************************
* RtlRaiseException (NTDLL.@)
*/
void WINAPI RtlRaiseException( EXCEPTION_RECORD *rec )
{
CONTEXT context;
NTSTATUS status;
RtlCaptureContext( &context );
rec->ExceptionAddress = (LPVOID)context.Pc;
status = raise_exception( rec, &context, TRUE );
if (status) raise_status( status, rec );
}
/*************************************************************************
* RtlCaptureStackBackTrace (NTDLL.@)
*/
USHORT WINAPI RtlCaptureStackBackTrace( ULONG skip, ULONG count, PVOID *buffer, ULONG *hash )
{
FIXME( "(%d, %d, %p, %p) stub!\n", skip, count, buffer, hash );
return 0;
}
/***********************************************************************
* call_thread_entry_point
*/
static void WINAPI call_thread_entry_point( LPTHREAD_START_ROUTINE entry, void *arg )
2013-01-16 00:50:30 +01:00
{
__TRY
{
TRACE_(relay)( "\1Starting thread proc %p (arg=%p)\n", entry, arg );
RtlExitUserThread( entry( arg ));
2013-01-16 00:50:30 +01:00
}
__EXCEPT(call_unhandled_exception_filter)
2013-01-16 00:50:30 +01:00
{
NtTerminateThread( GetCurrentThread(), GetExceptionCode() );
}
__ENDTRY
abort(); /* should not be reached */
}
extern void DECLSPEC_NORETURN start_thread( LPTHREAD_START_ROUTINE entry, void *arg, BOOL suspend,
void *relay, TEB *teb );
__ASM_GLOBAL_FUNC( start_thread,
"stp x29, x30, [sp,#-16]!\n\t"
"mov x18, x4\n\t" /* teb */
/* store exit frame */
"mov x29, sp\n\t"
"str x29, [x4, #0x300]\n\t" /* arm64_thread_data()->exit_frame */
/* switch to thread stack */
"ldr x5, [x4, #8]\n\t" /* teb->Tib.StackBase */
"sub sp, x5, #0x1000\n\t"
/* attach dlls */
"bl " __ASM_NAME("attach_thread") "\n\t"
"mov sp, x0\n\t"
/* clear the stack */
"and x0, x0, #~0xfff\n\t" /* round down to page size */
"bl " __ASM_NAME("virtual_clear_thread_stack") "\n\t"
/* switch to the initial context */
"mov x0, sp\n\t"
"ldp x1, x2, [x0, #0x10]\n\t" /* context->X1,2 */
"ldp x3, x4, [x0, #0x20]\n\t" /* context->X3,4 */
"ldp x5, x6, [x0, #0x30]\n\t" /* context->X5,6 */
"ldp x7, x8, [x0, #0x40]\n\t" /* context->X7,8 */
"ldp x9, x10, [x0, #0x50]\n\t" /* context->X9,10 */
"ldp x11, x12, [x0, #0x60]\n\t" /* context->X11,12 */
"ldp x13, x14, [x0, #0x70]\n\t" /* context->X13,14 */
"ldp x15, x16, [x0, #0x80]\n\t" /* context->X15,16 */
"ldp x17, x18, [x0, #0x90]\n\t" /* context->X17,18 */
"ldp x19, x20, [x0, #0xa0]\n\t" /* context->X19,20 */
"ldp x21, x22, [x0, #0xb0]\n\t" /* context->X21,22 */
"ldp x23, x24, [x0, #0xc0]\n\t" /* context->X23,24 */
"ldp x25, x26, [x0, #0xd0]\n\t" /* context->X25,26 */
"ldp x27, x28, [x0, #0xe0]\n\t" /* context->X27,28 */
"ldp x29, x30, [x0, #0xf0]\n\t" /* context->Fp,Lr */
"ldr x17, [x0, #0x100]\n\t" /* context->Sp */
"mov sp, x17\n\t"
"ldr x17, [x0, #0x108]\n\t" /* context->Pc */
"ldr x0, [x0, #0x8]\n\t" /* context->X0 */
"br x17" )
extern void DECLSPEC_NORETURN call_thread_exit_func( int status, void (*func)(int), TEB *teb );
__ASM_GLOBAL_FUNC( call_thread_exit_func,
"ldr x3, [x2, #0x300]\n\t" /* arm64_thread_data()->exit_frame */
"str xzr, [x2, #0x300]\n\t"
"cbz x3, 1f\n\t"
"mov sp, x3\n"
"1:\tblr x1" )
/***********************************************************************
* init_thread_context
*/
static void init_thread_context( CONTEXT *context, LPTHREAD_START_ROUTINE entry, void *arg, void *relay )
{
context->u.s.X0 = (DWORD64)entry;
context->u.s.X1 = (DWORD64)arg;
context->u.s.X18 = (DWORD64)NtCurrentTeb();
context->Sp = (DWORD64)NtCurrentTeb()->Tib.StackBase;
context->Pc = (DWORD64)relay;
}
/***********************************************************************
* attach_thread
*/
PCONTEXT DECLSPEC_HIDDEN attach_thread( LPTHREAD_START_ROUTINE entry, void *arg,
BOOL suspend, void *relay )
{
CONTEXT *ctx;
if (suspend)
{
CONTEXT context = { CONTEXT_ALL };
init_thread_context( &context, entry, arg, relay );
wait_suspend( &context );
ctx = (CONTEXT *)((ULONG_PTR)context.Sp & ~15) - 1;
*ctx = context;
}
else
{
ctx = (CONTEXT *)NtCurrentTeb()->Tib.StackBase - 1;
init_thread_context( ctx, entry, arg, relay );
}
ctx->ContextFlags = CONTEXT_FULL;
LdrInitializeThunk( ctx, (void **)&ctx->u.s.X0, 0, 0 );
return ctx;
}
/***********************************************************************
* signal_start_thread
*
* Thread startup sequence:
* signal_start_thread()
* -> start_thread()
* -> call_thread_func()
*/
void signal_start_thread( LPTHREAD_START_ROUTINE entry, void *arg, BOOL suspend )
{
start_thread( entry, arg, suspend, call_thread_entry_point, NtCurrentTeb() );
}
/**********************************************************************
* signal_start_process
*
* Process startup sequence:
* signal_start_process()
* -> start_thread()
* -> kernel32_start_process()
*/
void signal_start_process( LPTHREAD_START_ROUTINE entry, BOOL suspend )
{
start_thread( entry, NtCurrentTeb()->Peb, suspend, kernel32_start_process, NtCurrentTeb() );
}
2013-01-16 00:50:30 +01:00
/***********************************************************************
* signal_exit_thread
2013-01-16 00:50:30 +01:00
*/
void signal_exit_thread( int status )
2013-01-16 00:50:30 +01:00
{
call_thread_exit_func( status, exit_thread, NtCurrentTeb() );
2013-01-16 00:50:30 +01:00
}
/***********************************************************************
* signal_exit_process
2013-01-16 00:50:30 +01:00
*/
void signal_exit_process( int status )
2013-01-16 00:50:30 +01:00
{
call_thread_exit_func( status, exit, NtCurrentTeb() );
2013-01-16 00:50:30 +01:00
}
/**********************************************************************
* DbgBreakPoint (NTDLL.@)
*/
__ASM_STDCALL_FUNC( DbgBreakPoint, 0, "brk #0; ret")
2013-01-16 00:50:30 +01:00
/**********************************************************************
* DbgUserBreakPoint (NTDLL.@)
*/
__ASM_STDCALL_FUNC( DbgUserBreakPoint, 0, "brk #0; ret")
2013-01-16 00:50:30 +01:00
/**********************************************************************
* NtCurrentTeb (NTDLL.@)
*/
TEB * WINAPI NtCurrentTeb(void)
{
return pthread_getspecific( teb_key );
}
#endif /* __aarch64__ */