wine-wine/dlls/ntdll/signal_arm.c

315 lines
10 KiB
C

/*
* ARM signal handling routines
*
* Copyright 2002 Marcus Meissner, SuSE Linux AG
* Copyright 2010-2013, 2015 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 __arm__
#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
#define NONAMELESSUNION
#define NONAMELESSSTRUCT
#include "ntstatus.h"
#define WIN32_NO_STATUS
#include "windef.h"
#include "winternl.h"
#include "wine/exception.h"
#include "ntdll_misc.h"
#include "wine/debug.h"
#include "winnt.h"
WINE_DEFAULT_DEBUG_CHANNEL(seh);
/*******************************************************************
* 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);
}
/**************************************************************************
* __chkstk (NTDLL.@)
*
* Incoming r4 contains words to allocate, converting to bytes then return
*/
__ASM_GLOBAL_FUNC( __chkstk, "lsl r4, r4, #2\n\t"
"bx lr" )
/***********************************************************************
* RtlCaptureContext (NTDLL.@)
*/
/* FIXME: Use the Stack instead of the actual register values */
__ASM_STDCALL_FUNC( RtlCaptureContext, 4,
".arm\n\t"
"stmib r0, {r0-r12}\n\t" /* context->R0..R12 */
"mov r1, #0x0200000\n\t" /* CONTEXT_ARM */
"add r1, r1, #0x3\n\t" /* CONTEXT_FULL */
"str r1, [r0]\n\t" /* context->ContextFlags */
"str SP, [r0, #0x38]\n\t" /* context->Sp */
"str LR, [r0, #0x3c]\n\t" /* context->Lr */
"str PC, [r0, #0x40]\n\t" /* context->Pc */
"mrs r1, CPSR\n\t"
"str r1, [r0, #0x44]\n\t" /* context->Cpsr */
"bx lr"
)
/***********************************************************************
* set_cpu_context
*
* Set the new CPU context.
*/
void DECLSPEC_HIDDEN set_cpu_context( const CONTEXT *context );
__ASM_GLOBAL_FUNC( set_cpu_context,
".arm\n\t"
"ldr r2, [r0, #0x44]\n\t" /* context->Cpsr */
"tst r2, #0x20\n\t" /* thumb? */
"ldr r1, [r0, #0x40]\n\t" /* context->Pc */
"orrne r1, r1, #1\n\t" /* Adjust PC according to thumb */
"biceq r1, r1, #1\n\t" /* Adjust PC according to arm */
"msr CPSR_f, r2\n\t"
"ldr lr, [r0, #0x3c]\n\t" /* context->Lr */
"ldr sp, [r0, #0x38]\n\t" /* context->Sp */
"push {r1}\n\t"
"ldmib r0, {r0-r12}\n\t" /* context->R0..R12 */
"pop {pc}" )
/**********************************************************************
* 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;
}
/*******************************************************************
* KiUserExceptionDispatcher (NTDLL.@)
*/
NTSTATUS WINAPI KiUserExceptionDispatcher( EXCEPTION_RECORD *rec, CONTEXT *context )
{
NTSTATUS status;
DWORD c;
TRACE( "code=%x flags=%x addr=%p pc=%08x tid=%04x\n",
rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
context->Pc, GetCurrentThreadId() );
for (c = 0; c < rec->NumberParameters; c++)
TRACE( " info[%d]=%08lx\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( " r0=%08x r1=%08x r2=%08x r3=%08x r4=%08x r5=%08x\n",
context->R0, context->R1, context->R2, context->R3, context->R4, context->R5 );
TRACE( " r6=%08x r7=%08x r8=%08x r9=%08x r10=%08x r11=%08x\n",
context->R6, context->R7, context->R8, context->R9, context->R10, context->R11 );
TRACE( " r12=%08x sp=%08x lr=%08x pc=%08x cpsr=%08x\n",
context->R12, context->Sp, context->Lr, context->Pc, context->Cpsr );
}
if (call_vectored_handlers( rec, context ) == EXCEPTION_CONTINUE_EXECUTION)
NtContinue( context, FALSE );
if ((status = call_stack_handlers( rec, context )) == STATUS_SUCCESS)
NtContinue( context, FALSE );
if (status != STATUS_UNHANDLED_EXCEPTION) RtlRaiseStatus( status );
return NtRaiseException( rec, context, FALSE );
}
/***********************************************************************
* RtlUnwind (NTDLL.@)
*/
void WINAPI RtlUnwind( void *endframe, void *target_ip, EXCEPTION_RECORD *rec, void *retval )
{
CONTEXT context;
EXCEPTION_RECORD record;
EXCEPTION_REGISTRATION_RECORD *frame, *dispatch;
DWORD res;
RtlCaptureContext( &context );
context.R0 = (DWORD)retval;
/* build an exception record, if we do not have one */
if (!rec)
{
record.ExceptionCode = STATUS_UNWIND;
record.ExceptionFlags = 0;
record.ExceptionRecord = NULL;
record.ExceptionAddress = (void *)context.Pc;
record.NumberParameters = 0;
rec = &record;
}
rec->ExceptionFlags |= EH_UNWINDING | (endframe ? 0 : EH_EXIT_UNWIND);
TRACE( "code=%x flags=%x\n", rec->ExceptionCode, rec->ExceptionFlags );
/* get chain of exception frames */
frame = NtCurrentTeb()->Tib.ExceptionList;
while ((frame != (EXCEPTION_REGISTRATION_RECORD*)~0UL) && (frame != endframe))
{
/* Check frame address */
if (endframe && ((void*)frame > endframe))
raise_status( STATUS_INVALID_UNWIND_TARGET, rec );
if (!is_valid_frame( frame )) raise_status( STATUS_BAD_STACK, rec );
/* 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 );
switch(res)
{
case ExceptionContinueSearch:
break;
case ExceptionCollidedUnwind:
frame = dispatch;
break;
default:
raise_status( STATUS_INVALID_DISPOSITION, rec );
break;
}
frame = __wine_pop_frame( frame );
}
}
/***********************************************************************
* RtlRaiseException (NTDLL.@)
*/
void WINAPI RtlRaiseException( EXCEPTION_RECORD *rec )
{
CONTEXT context;
RtlCaptureContext( &context );
rec->ExceptionAddress = (LPVOID)context.Pc;
RtlRaiseStatus( NtRaiseException( rec, &context, TRUE ));
}
/*************************************************************************
* 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;
}
/**********************************************************************
* DbgBreakPoint (NTDLL.@)
*/
void WINAPI DbgBreakPoint(void)
{
kill(getpid(), SIGTRAP);
}
/**********************************************************************
* DbgUserBreakPoint (NTDLL.@)
*/
void WINAPI DbgUserBreakPoint(void)
{
kill(getpid(), SIGTRAP);
}
/**********************************************************************
* NtCurrentTeb (NTDLL.@)
*/
TEB * WINAPI NtCurrentTeb(void)
{
return unix_funcs->NtCurrentTeb();
}
#endif /* __arm__ */