mirror of
https://github.com/kanjitalk755/macemu.git
synced 2025-02-23 15:29:07 +00:00
Mach memory fault recovery from Michael Z. Sliczniak
This commit is contained in:
parent
b2fee2f576
commit
cf3d36a3a7
@ -838,6 +838,7 @@ Contributions by (in alphabetical order):
|
||||
- Nigel Pearson <nigel@ind.tansu.com.au>: Mac OS X port
|
||||
- Lauri Pesonen <lpesonen@nic.fi>: Windows NT port
|
||||
- Bernd Schmidt <crux@pool.informatik.rwth-aachen.de>: UAE 68k emulation
|
||||
- Michael Z. Sliczniak <msliczniak@comcast.net>: Mach memory fault recovery
|
||||
- and others...
|
||||
|
||||
Special thanks to:
|
||||
|
@ -4,6 +4,12 @@
|
||||
* Derived from Bruno Haible's work on his SIGSEGV library for clisp
|
||||
* <http://clisp.sourceforge.net/>
|
||||
*
|
||||
* MacOS X support derived from the post by Timothy J. Wood to the
|
||||
* omnigroup macosx-dev list:
|
||||
* Mach Exception Handlers 101 (Was Re: ptrace, gdb)
|
||||
* tjw@omnigroup.com Sun, 4 Jun 2000
|
||||
* www.omnigroup.com/mailman/archive/macosx-dev/2000-June/002030.html
|
||||
*
|
||||
* Basilisk II (C) 1997-2002 Christian Bauer
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
@ -206,12 +212,14 @@ static void powerpc_decode_instruction(instruction_t *instruction, unsigned int
|
||||
|
||||
#if HAVE_SIGINFO_T
|
||||
// Generic extended signal handler
|
||||
#define SIGSEGV_FAULT_HANDLER sigsegv_fault_handler
|
||||
#if defined(__NetBSD__) || defined(__FreeBSD__)
|
||||
#define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGBUS)
|
||||
#else
|
||||
#define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV)
|
||||
#endif
|
||||
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, siginfo_t *sip, void *scp
|
||||
#define SIGSEGV_FAULT_HANDLER_ARGS sig, sip, scp
|
||||
#define SIGSEGV_FAULT_ADDRESS sip->si_addr
|
||||
#if defined(__NetBSD__) || defined(__FreeBSD__)
|
||||
#if (defined(i386) || defined(__i386__))
|
||||
@ -248,12 +256,14 @@ static void powerpc_decode_instruction(instruction_t *instruction, unsigned int
|
||||
#endif
|
||||
|
||||
#if HAVE_SIGCONTEXT_SUBTERFUGE
|
||||
#define SIGSEGV_FAULT_HANDLER sigsegv_fault_handler
|
||||
// Linux kernels prior to 2.4 ?
|
||||
#if defined(__linux__)
|
||||
#define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV)
|
||||
#if (defined(i386) || defined(__i386__))
|
||||
#include <asm/sigcontext.h>
|
||||
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, struct sigcontext scs
|
||||
#define SIGSEGV_FAULT_HANDLER_ARGS sig, scs
|
||||
#define SIGSEGV_FAULT_ADDRESS scs.cr2
|
||||
#define SIGSEGV_FAULT_INSTRUCTION scs.eip
|
||||
#define SIGSEGV_REGISTER_FILE (unsigned int *)(&scs)
|
||||
@ -262,11 +272,13 @@ static void powerpc_decode_instruction(instruction_t *instruction, unsigned int
|
||||
#if (defined(sparc) || defined(__sparc__))
|
||||
#include <asm/sigcontext.h>
|
||||
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp, char *addr
|
||||
#define SIGSEGV_FAULT_HANDLER_ARGS sig, code, scp, addr
|
||||
#define SIGSEGV_FAULT_ADDRESS addr
|
||||
#endif
|
||||
#if (defined(powerpc) || defined(__powerpc__))
|
||||
#include <asm/sigcontext.h>
|
||||
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, struct sigcontext *scp
|
||||
#define SIGSEGV_FAULT_HANDLER_ARGS sig, scp
|
||||
#define SIGSEGV_FAULT_ADDRESS scp->regs->dar
|
||||
#define SIGSEGV_FAULT_INSTRUCTION scp->regs->nip
|
||||
#define SIGSEGV_REGISTER_FILE (unsigned int *)&scp->regs->nip, (unsigned int *)(scp->regs->gpr)
|
||||
@ -275,6 +287,7 @@ static void powerpc_decode_instruction(instruction_t *instruction, unsigned int
|
||||
#if (defined(alpha) || defined(__alpha__))
|
||||
#include <asm/sigcontext.h>
|
||||
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp
|
||||
#define SIGSEGV_FAULT_HANDLER_ARGS sig, code, scp
|
||||
#define SIGSEGV_FAULT_ADDRESS get_fault_address(scp)
|
||||
#define SIGSEGV_FAULT_INSTRUCTION scp->sc_pc
|
||||
|
||||
@ -293,6 +306,7 @@ static sigsegv_address_t get_fault_address(struct sigcontext *scp)
|
||||
#if (defined(sgi) || defined(__sgi)) && (defined(SYSTYPE_SVR4) || defined(__SYSTYPE_SVR4))
|
||||
#include <ucontext.h>
|
||||
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp
|
||||
#define SIGSEGV_FAULT_HANDLER_ARGS sig, code, scp
|
||||
#define SIGSEGV_FAULT_ADDRESS scp->sc_badvaddr
|
||||
#define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV)
|
||||
#endif
|
||||
@ -300,6 +314,7 @@ static sigsegv_address_t get_fault_address(struct sigcontext *scp)
|
||||
// HP-UX
|
||||
#if (defined(hpux) || defined(__hpux__))
|
||||
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp
|
||||
#define SIGSEGV_FAULT_HANDLER_ARGS sig, code, scp
|
||||
#define SIGSEGV_FAULT_ADDRESS scp->sc_sl.sl_ss.ss_narrow.ss_cr21
|
||||
#define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV) FAULT_HANDLER(SIGBUS)
|
||||
#endif
|
||||
@ -308,6 +323,7 @@ static sigsegv_address_t get_fault_address(struct sigcontext *scp)
|
||||
#if defined(__osf__)
|
||||
#include <ucontext.h>
|
||||
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp
|
||||
#define SIGSEGV_FAULT_HANDLER_ARGS sig, code, scp
|
||||
#define SIGSEGV_FAULT_ADDRESS scp->sc_traparg_a0
|
||||
#define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV)
|
||||
#endif
|
||||
@ -315,6 +331,7 @@ static sigsegv_address_t get_fault_address(struct sigcontext *scp)
|
||||
// AIX
|
||||
#if defined(_AIX)
|
||||
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp
|
||||
#define SIGSEGV_FAULT_HANDLER_ARGS sig, code, scp
|
||||
#define SIGSEGV_FAULT_ADDRESS scp->sc_jmpbuf.jmp_context.o_vaddr
|
||||
#define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV)
|
||||
#endif
|
||||
@ -324,6 +341,7 @@ static sigsegv_address_t get_fault_address(struct sigcontext *scp)
|
||||
#if (defined(m68k) || defined(__m68k__))
|
||||
#include <m68k/frame.h>
|
||||
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp
|
||||
#define SIGSEGV_FAULT_HANDLER_ARGS sig, code, scp
|
||||
#define SIGSEGV_FAULT_ADDRESS get_fault_address(scp)
|
||||
#define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV)
|
||||
|
||||
@ -349,15 +367,24 @@ static sigsegv_address_t get_fault_address(struct sigcontext *scp)
|
||||
}
|
||||
#else
|
||||
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, void *scp, char *addr
|
||||
#define SIGSEGV_FAULT_HANDLER_ARGS sig, code, scp, addr
|
||||
#define SIGSEGV_FAULT_ADDRESS addr
|
||||
#define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGBUS)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// MacOS X
|
||||
// MacOS X, not sure which version this works in. Under 10.1
|
||||
// vm_protect does not appear to work from a signal handler. Under
|
||||
// 10.2 signal handlers get siginfo type arguments but the si_addr
|
||||
// field is the address of the faulting instruction and not the
|
||||
// address that caused the SIGBUS. Maybe this works in 10.0? In any
|
||||
// case with Mach exception handlers there is a way to do what this
|
||||
// was meant to do.
|
||||
#ifndef HAVE_MACH_EXCEPTIONS
|
||||
#if defined(__APPLE__) && defined(__MACH__)
|
||||
#if (defined(ppc) || defined(__ppc__))
|
||||
#define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp
|
||||
#define SIGSEGV_FAULT_HANDLER_ARGS sig, code, scp
|
||||
#define SIGSEGV_FAULT_ADDRESS get_fault_address(scp)
|
||||
#define SIGSEGV_FAULT_INSTRUCTION scp->sc_ir
|
||||
#define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGBUS)
|
||||
@ -377,6 +404,142 @@ static sigsegv_address_t get_fault_address(struct sigcontext *scp)
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if HAVE_MACH_EXCEPTIONS
|
||||
|
||||
// This can easily be extended to other Mach systems, but really who
|
||||
// uses HURD (oops GNU/HURD), Darwin/x86, NextStep, Rhapsody, or CMU
|
||||
// Mach 2.5/3.0?
|
||||
#if defined(__APPLE__) && defined(__MACH__)
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <pthread.h>
|
||||
|
||||
/*
|
||||
* If you are familiar with MIG then you will understand the frustration
|
||||
* that was necessary to get these embedded into C++ code by hand.
|
||||
*/
|
||||
extern "C" {
|
||||
#include <mach/mach.h>
|
||||
#include <mach/mach_error.h>
|
||||
|
||||
extern boolean_t exc_server(mach_msg_header_t *, mach_msg_header_t *);
|
||||
extern kern_return_t catch_exception_raise(mach_port_t, mach_port_t,
|
||||
mach_port_t, exception_type_t, exception_data_t, mach_msg_type_number_t);
|
||||
extern kern_return_t exception_raise(mach_port_t, mach_port_t, mach_port_t,
|
||||
exception_type_t, exception_data_t, mach_msg_type_number_t);
|
||||
extern kern_return_t exception_raise_state(mach_port_t, exception_type_t,
|
||||
exception_data_t, mach_msg_type_number_t, thread_state_flavor_t *,
|
||||
thread_state_t, mach_msg_type_number_t, thread_state_t, mach_msg_type_number_t *);
|
||||
extern kern_return_t exception_raise_state_identity(mach_port_t, mach_port_t, mach_port_t,
|
||||
exception_type_t, exception_data_t, mach_msg_type_number_t, thread_state_flavor_t *,
|
||||
thread_state_t, mach_msg_type_number_t, thread_state_t, mach_msg_type_number_t *);
|
||||
}
|
||||
|
||||
// Could make this dynamic by looking for a result of MIG_ARRAY_TOO_LARGE
|
||||
#define HANDLER_COUNT 64
|
||||
|
||||
// structure to tuck away existing exception handlers
|
||||
typedef struct _ExceptionPorts {
|
||||
mach_msg_type_number_t maskCount;
|
||||
exception_mask_t masks[HANDLER_COUNT];
|
||||
exception_handler_t handlers[HANDLER_COUNT];
|
||||
exception_behavior_t behaviors[HANDLER_COUNT];
|
||||
thread_state_flavor_t flavors[HANDLER_COUNT];
|
||||
} ExceptionPorts;
|
||||
|
||||
// exception handler thread
|
||||
static pthread_t exc_thread;
|
||||
|
||||
// place where old exception handler info is stored
|
||||
static ExceptionPorts ports;
|
||||
|
||||
// our exception port
|
||||
static mach_port_t _exceptionPort = MACH_PORT_NULL;
|
||||
|
||||
#define MACH_CHECK_ERROR(name,ret) \
|
||||
if (ret != KERN_SUCCESS) { \
|
||||
mach_error(#name, ret); \
|
||||
exit (1); \
|
||||
}
|
||||
|
||||
#define SIGSEGV_FAULT_ADDRESS code[1]
|
||||
#define SIGSEGV_FAULT_INSTRUCTION get_fault_instruction(thread, state)
|
||||
#define SIGSEGV_FAULT_HANDLER (code[0] == KERN_PROTECTION_FAILURE) && sigsegv_fault_handler
|
||||
#define SIGSEGV_FAULT_HANDLER_ARGLIST mach_port_t thread, exception_data_t code, ppc_thread_state_t *state
|
||||
#define SIGSEGV_FAULT_HANDLER_ARGS thread, code, &state
|
||||
#define SIGSEGV_SKIP_INSTRUCTION powerpc_skip_instruction
|
||||
#define SIGSEGV_REGISTER_FILE &state->srr0, &state->r0
|
||||
|
||||
// Given a suspended thread, stuff the current instruction and
|
||||
// registers into state.
|
||||
//
|
||||
// It would have been nice to have this be ppc/x86 independant which
|
||||
// could have been done easily with a thread_state_t instead of
|
||||
// ppc_thread_state_t, but because of the way this is called it is
|
||||
// easier to do it this way.
|
||||
#if (defined(ppc) || defined(__ppc__))
|
||||
static inline sigsegv_address_t get_fault_instruction(mach_port_t thread, ppc_thread_state_t *state)
|
||||
{
|
||||
kern_return_t krc;
|
||||
mach_msg_type_number_t count;
|
||||
|
||||
count = MACHINE_THREAD_STATE_COUNT;
|
||||
krc = thread_get_state(thread, MACHINE_THREAD_STATE, (thread_state_t)state, &count);
|
||||
MACH_CHECK_ERROR (thread_get_state, krc);
|
||||
|
||||
return (sigsegv_address_t)state->srr0;
|
||||
}
|
||||
#endif
|
||||
|
||||
// Since there can only be one exception thread running at any time
|
||||
// this is not a problem.
|
||||
#define MSG_SIZE 512
|
||||
static char msgbuf[MSG_SIZE];
|
||||
static char replybuf[MSG_SIZE];
|
||||
|
||||
/*
|
||||
* This is the entry point for the exception handler thread. The job
|
||||
* of this thread is to wait for exception messages on the exception
|
||||
* port that was setup beforehand and to pass them on to exc_server.
|
||||
* exc_server is a MIG generated function that is a part of Mach.
|
||||
* Its job is to decide what to do with the exception message. In our
|
||||
* case exc_server calls catch_exception_raise on our behalf. After
|
||||
* exc_server returns, it is our responsibility to send the reply.
|
||||
*/
|
||||
static void *
|
||||
handleExceptions(void *priv)
|
||||
{
|
||||
mach_msg_header_t *msg, *reply;
|
||||
kern_return_t krc;
|
||||
|
||||
msg = (mach_msg_header_t *)msgbuf;
|
||||
reply = (mach_msg_header_t *)replybuf;
|
||||
|
||||
for (;;) {
|
||||
krc = mach_msg(msg, MACH_RCV_MSG, MSG_SIZE, MSG_SIZE,
|
||||
_exceptionPort, 0, MACH_PORT_NULL);
|
||||
MACH_CHECK_ERROR(mach_msg, krc);
|
||||
|
||||
if (!exc_server(msg, reply)) {
|
||||
fprintf(stderr, "exc_server hated the message\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
krc = mach_msg(reply, MACH_SEND_MSG, reply->msgh_size, 0,
|
||||
msg->msgh_local_port, 0, MACH_PORT_NULL);
|
||||
if (krc != KERN_SUCCESS) {
|
||||
fprintf(stderr, "Error sending message to original reply port, krc = %d, %s",
|
||||
krc, mach_error_string(krc));
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
@ -627,40 +790,213 @@ static bool powerpc_skip_instruction(unsigned int * nip_p, unsigned int * regs)
|
||||
* SIGSEGV global handler
|
||||
*/
|
||||
|
||||
#ifdef HAVE_SIGSEGV_RECOVERY
|
||||
static void sigsegv_handler(SIGSEGV_FAULT_HANDLER_ARGLIST)
|
||||
#if defined(HAVE_SIGSEGV_RECOVERY) || defined(HAVE_MACH_EXCEPTIONS)
|
||||
// This function handles the badaccess to memory.
|
||||
// It is called from the signal handler or the exception handler.
|
||||
static bool handle_badaccess(SIGSEGV_FAULT_HANDLER_ARGLIST)
|
||||
{
|
||||
sigsegv_address_t fault_address = (sigsegv_address_t)SIGSEGV_FAULT_ADDRESS;
|
||||
sigsegv_address_t fault_instruction = (sigsegv_address_t)SIGSEGV_FAULT_INSTRUCTION;
|
||||
bool fault_recovered = false;
|
||||
|
||||
// Call user's handler and reinstall the global handler, if required
|
||||
switch (sigsegv_fault_handler(fault_address, fault_instruction)) {
|
||||
case SIGSEGV_RETURN_SUCCESS:
|
||||
#if (defined(HAVE_SIGACTION) ? defined(SIGACTION_NEED_REINSTALL) : defined(SIGNAL_NEED_REINSTALL))
|
||||
sigsegv_do_install_handler(sig);
|
||||
#endif
|
||||
fault_recovered = true;
|
||||
break;
|
||||
return true;
|
||||
|
||||
#if HAVE_SIGSEGV_SKIP_INSTRUCTION
|
||||
case SIGSEGV_RETURN_SKIP_INSTRUCTION:
|
||||
// Call the instruction skipper with the register file available
|
||||
if (SIGSEGV_SKIP_INSTRUCTION(SIGSEGV_REGISTER_FILE))
|
||||
fault_recovered = true;
|
||||
// Call the instruction skipper with the register file
|
||||
// available
|
||||
if (SIGSEGV_SKIP_INSTRUCTION(SIGSEGV_REGISTER_FILE)) {
|
||||
#ifdef HAVE_MACH_EXCEPTIONS
|
||||
// Unlike UNIX signals where the thread state
|
||||
// is modified off of the stack, in Mach we
|
||||
// need to actually call thread_set_state to
|
||||
// have the register values updated.
|
||||
kern_return_t krc;
|
||||
|
||||
krc = thread_set_state(thread,
|
||||
MACHINE_THREAD_STATE, (thread_state_t)state,
|
||||
MACHINE_THREAD_STATE_COUNT);
|
||||
MACH_CHECK_ERROR (thread_get_state, krc);
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
#endif
|
||||
}
|
||||
|
||||
if (!fault_recovered) {
|
||||
// Failure: reinstall default handler for "safe" crash
|
||||
#define FAULT_HANDLER(sig) signal(sig, SIG_DFL);
|
||||
SIGSEGV_ALL_SIGNALS
|
||||
#undef FAULT_HANDLER
|
||||
|
||||
// We can't do anything with the fault_address, dump state?
|
||||
if (sigsegv_state_dumper != 0)
|
||||
sigsegv_state_dumper(fault_address, fault_instruction);
|
||||
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
* There are two mechanisms for handling a bad memory access,
|
||||
* Mach exceptions and UNIX signals. The implementation specific
|
||||
* code appears below. Its reponsibility is to call handle_badaccess
|
||||
* which is the routine that handles the fault in an implementation
|
||||
* agnostic manner. The implementation specific code below is then
|
||||
* reponsible for checking whether handle_badaccess was able
|
||||
* to handle the memory access error and perform any implementation
|
||||
* specific tasks necessary afterwards.
|
||||
*/
|
||||
|
||||
#ifdef HAVE_MACH_EXCEPTIONS
|
||||
/*
|
||||
* We need to forward all exceptions that we do not handle.
|
||||
* This is important, there are many exceptions that may be
|
||||
* handled by other exception handlers. For example debuggers
|
||||
* use exceptions and the exception hander is in another
|
||||
* process in such a case. (Timothy J. Wood states in his
|
||||
* message to the list that he based this code on that from
|
||||
* gdb for Darwin.)
|
||||
*/
|
||||
static inline kern_return_t
|
||||
forward_exception(mach_port_t thread_port,
|
||||
mach_port_t task_port,
|
||||
exception_type_t exception_type,
|
||||
exception_data_t exception_data,
|
||||
mach_msg_type_number_t data_count,
|
||||
ExceptionPorts *oldExceptionPorts)
|
||||
{
|
||||
kern_return_t kret;
|
||||
unsigned int portIndex;
|
||||
mach_port_t port;
|
||||
exception_behavior_t behavior;
|
||||
thread_state_flavor_t flavor;
|
||||
thread_state_t thread_state;
|
||||
mach_msg_type_number_t thread_state_count;
|
||||
|
||||
for (portIndex = 0; portIndex < oldExceptionPorts->maskCount; portIndex++) {
|
||||
if (oldExceptionPorts->masks[portIndex] & (1 << exception_type)) {
|
||||
// This handler wants the exception
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (portIndex >= oldExceptionPorts->maskCount) {
|
||||
fprintf(stderr, "No handler for exception_type = %d. Not fowarding\n", exception_type);
|
||||
return KERN_FAILURE;
|
||||
}
|
||||
|
||||
port = oldExceptionPorts->handlers[portIndex];
|
||||
behavior = oldExceptionPorts->behaviors[portIndex];
|
||||
flavor = oldExceptionPorts->flavors[portIndex];
|
||||
|
||||
/*
|
||||
fprintf(stderr, "forwarding exception, port = 0x%x, behaviour = %d, flavor = %d\n", port, behavior, flavor);
|
||||
*/
|
||||
|
||||
if (behavior != EXCEPTION_DEFAULT) {
|
||||
thread_state_count = THREAD_STATE_MAX;
|
||||
kret = thread_get_state (thread_port, flavor, thread_state,
|
||||
&thread_state_count);
|
||||
MACH_CHECK_ERROR (thread_get_state, kret);
|
||||
}
|
||||
|
||||
switch (behavior) {
|
||||
case EXCEPTION_DEFAULT:
|
||||
// fprintf(stderr, "forwarding to exception_raise\n");
|
||||
kret = exception_raise(port, thread_port, task_port, exception_type,
|
||||
exception_data, data_count);
|
||||
MACH_CHECK_ERROR (exception_raise, kret);
|
||||
break;
|
||||
case EXCEPTION_STATE:
|
||||
// fprintf(stderr, "forwarding to exception_raise_state\n");
|
||||
kret = exception_raise_state(port, exception_type, exception_data,
|
||||
data_count, &flavor,
|
||||
thread_state, thread_state_count,
|
||||
thread_state, &thread_state_count);
|
||||
MACH_CHECK_ERROR (exception_raise_state, kret);
|
||||
break;
|
||||
case EXCEPTION_STATE_IDENTITY:
|
||||
// fprintf(stderr, "forwarding to exception_raise_state_identity\n");
|
||||
kret = exception_raise_state_identity(port, thread_port, task_port,
|
||||
exception_type, exception_data,
|
||||
data_count, &flavor,
|
||||
thread_state, thread_state_count,
|
||||
thread_state, &thread_state_count);
|
||||
MACH_CHECK_ERROR (exception_raise_state_identity, kret);
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "forward_exception got unknown behavior\n");
|
||||
break;
|
||||
}
|
||||
|
||||
if (behavior != EXCEPTION_DEFAULT) {
|
||||
kret = thread_set_state (thread_port, flavor, thread_state,
|
||||
thread_state_count);
|
||||
MACH_CHECK_ERROR (thread_set_state, kret);
|
||||
}
|
||||
|
||||
return KERN_SUCCESS;
|
||||
}
|
||||
|
||||
/*
|
||||
* This is the code that actually handles the exception.
|
||||
* It is called by exc_server. For Darwin 5 Apple changed
|
||||
* this a bit from how this family of functions worked in
|
||||
* Mach. If you are familiar with that it is a little
|
||||
* different. The main variation that concerns us here is
|
||||
* that code is an array of exception specific codes and
|
||||
* codeCount is a count of the number of codes in the code
|
||||
* array. In typical Mach all exceptions have a code
|
||||
* and sub-code. It happens to be the case that for a
|
||||
* EXC_BAD_ACCESS exception the first entry is the type of
|
||||
* bad access that occurred and the second entry is the
|
||||
* faulting address so these entries correspond exactly to
|
||||
* how the code and sub-code are used on Mach.
|
||||
*
|
||||
* This is a MIG interface. No code in Basilisk II should
|
||||
* call this directley. This has to have external C
|
||||
* linkage because that is what exc_server expects.
|
||||
*/
|
||||
kern_return_t
|
||||
catch_exception_raise(mach_port_t exception_port,
|
||||
mach_port_t thread,
|
||||
mach_port_t task,
|
||||
exception_type_t exception,
|
||||
exception_data_t code,
|
||||
mach_msg_type_number_t codeCount)
|
||||
{
|
||||
ppc_thread_state_t state;
|
||||
kern_return_t krc;
|
||||
|
||||
if ((exception == EXC_BAD_ACCESS) && (codeCount >= 2)) {
|
||||
if (handle_badaccess(SIGSEGV_FAULT_HANDLER_ARGS))
|
||||
return KERN_SUCCESS;
|
||||
}
|
||||
|
||||
// In Mach we do not need to remove the exception handler.
|
||||
// If we forward the exception, eventually some exception handler
|
||||
// will take care of this exception.
|
||||
krc = forward_exception(thread, task, exception, code, codeCount, &ports);
|
||||
|
||||
return krc;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_SIGSEGV_RECOVERY
|
||||
// Handle bad memory accesses with signal handler
|
||||
static void sigsegv_handler(SIGSEGV_FAULT_HANDLER_ARGLIST)
|
||||
{
|
||||
// Call handler and reinstall the global handler, if required
|
||||
if (handle_badaccess(SIGSEGV_FAULT_HANDLER_ARGS)) {
|
||||
#if (defined(HAVE_SIGACTION) ? defined(SIGACTION_NEED_REINSTALL) : defined(SIGNAL_NEED_REINSTALL))
|
||||
sigsegv_do_install_handler(sig);
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
|
||||
// Failure: reinstall default handler for "safe" crash
|
||||
#define FAULT_HANDLER(sig) signal(sig, SIG_DFL);
|
||||
SIGSEGV_ALL_SIGNALS
|
||||
#undef FAULT_HANDLER
|
||||
}
|
||||
#endif
|
||||
|
||||
@ -705,15 +1041,103 @@ static bool sigsegv_do_install_handler(int sig)
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(HAVE_MACH_EXCEPTIONS)
|
||||
static bool sigsegv_do_install_handler(sigsegv_fault_handler_t handler)
|
||||
{
|
||||
/*
|
||||
* Except for the exception port functions, this should be
|
||||
* pretty much stock Mach. If later you choose to support
|
||||
* other Mach's besides Darwin, just check for __MACH__
|
||||
* here and __APPLE__ where the actual differences are.
|
||||
*/
|
||||
#if defined(__APPLE__) && defined(__MACH__)
|
||||
if (sigsegv_fault_handler != NULL) {
|
||||
sigsegv_fault_handler = handler;
|
||||
return true;
|
||||
}
|
||||
|
||||
kern_return_t krc;
|
||||
|
||||
// create the the exception port
|
||||
krc = mach_port_allocate(mach_task_self(),
|
||||
MACH_PORT_RIGHT_RECEIVE, &_exceptionPort);
|
||||
if (krc != KERN_SUCCESS) {
|
||||
mach_error("mach_port_allocate", krc);
|
||||
return false;
|
||||
}
|
||||
|
||||
// add a port send right
|
||||
krc = mach_port_insert_right(mach_task_self(),
|
||||
_exceptionPort, _exceptionPort,
|
||||
MACH_MSG_TYPE_MAKE_SEND);
|
||||
if (krc != KERN_SUCCESS) {
|
||||
mach_error("mach_port_insert_right", krc);
|
||||
return false;
|
||||
}
|
||||
|
||||
// get the old exception ports
|
||||
ports.maskCount = sizeof (ports.masks) / sizeof (ports.masks[0]);
|
||||
krc = thread_get_exception_ports(mach_thread_self(), EXC_MASK_BAD_ACCESS, ports.masks,
|
||||
&ports.maskCount, ports.handlers, ports.behaviors, ports.flavors);
|
||||
if (krc != KERN_SUCCESS) {
|
||||
mach_error("thread_get_exception_ports", krc);
|
||||
return false;
|
||||
}
|
||||
|
||||
// set the new exception port
|
||||
//
|
||||
// We could have used EXCEPTION_STATE_IDENTITY instead of
|
||||
// EXCEPTION_DEFAULT to get the thread state in the initial
|
||||
// message, but it turns out that in the common case this is not
|
||||
// neccessary. If we need it we can later ask for it from the
|
||||
// suspended thread.
|
||||
//
|
||||
// Even with THREAD_STATE_NONE, Darwin provides the program
|
||||
// counter in the thread state. The comments in the header file
|
||||
// seem to imply that you can count on the GPR's on an exception
|
||||
// as well but just to be safe I use MACHINE_THREAD_STATE because
|
||||
// you have to ask for all of the GPR's anyway just to get the
|
||||
// program counter. In any case because of update effective
|
||||
// address from immediate and update address from effective
|
||||
// addresses of ra and rb modes (as good an name as any for these
|
||||
// addressing modes) used in PPC instructions, you will need the
|
||||
// GPR state anyway.
|
||||
krc = thread_set_exception_ports(mach_thread_self(), EXC_MASK_BAD_ACCESS, _exceptionPort,
|
||||
EXCEPTION_DEFAULT, MACHINE_THREAD_STATE);
|
||||
if (krc != KERN_SUCCESS) {
|
||||
mach_error("thread_set_exception_ports", krc);
|
||||
return false;
|
||||
}
|
||||
|
||||
// create the exception handler thread
|
||||
if (pthread_create(&exc_thread, NULL, &handleExceptions, NULL) != 0) {
|
||||
(void)fprintf(stderr, "creation of exception thread failed\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
// do not care about the exception thread any longer, let is run standalone
|
||||
(void)pthread_detach(exc_thread);
|
||||
|
||||
sigsegv_fault_handler = handler;
|
||||
return true;
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
bool sigsegv_install_handler(sigsegv_fault_handler_t handler)
|
||||
{
|
||||
#ifdef HAVE_SIGSEGV_RECOVERY
|
||||
sigsegv_fault_handler = handler;
|
||||
#if defined(HAVE_SIGSEGV_RECOVERY)
|
||||
bool success = true;
|
||||
#define FAULT_HANDLER(sig) success = success && sigsegv_do_install_handler(sig);
|
||||
SIGSEGV_ALL_SIGNALS
|
||||
#undef FAULT_HANDLER
|
||||
if (success)
|
||||
sigsegv_fault_handler = handler;
|
||||
return success;
|
||||
#elif defined(HAVE_MACH_EXCEPTIONS)
|
||||
return sigsegv_do_install_handler(handler);
|
||||
#else
|
||||
// FAIL: no siginfo_t nor sigcontext subterfuge is available
|
||||
return false;
|
||||
@ -727,6 +1151,11 @@ bool sigsegv_install_handler(sigsegv_fault_handler_t handler)
|
||||
|
||||
void sigsegv_deinstall_handler(void)
|
||||
{
|
||||
// We do nothing for Mach exceptions, the thread would need to be
|
||||
// suspended if not already so, and we might mess with other
|
||||
// exception handlers that came after we registered ours. There is
|
||||
// no need to remove the exception handler, in fact this function is
|
||||
// not called anywhere in Basilisk II.
|
||||
#ifdef HAVE_SIGSEGV_RECOVERY
|
||||
sigsegv_fault_handler = 0;
|
||||
#define FAULT_HANDLER(sig) signal(sig, SIG_DFL);
|
||||
|
Loading…
x
Reference in New Issue
Block a user