ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/sigsegv.cpp
(Generate patch)

Comparing BasiliskII/src/Unix/sigsegv.cpp (file contents):
Revision 1.24 by gbeauche, 2003-09-29T07:02:58Z vs.
Revision 1.33 by gbeauche, 2003-10-21T23:10:19Z

# Line 4 | Line 4
4   *  Derived from Bruno Haible's work on his SIGSEGV library for clisp
5   *  <http://clisp.sourceforge.net/>
6   *
7 + *  MacOS X support derived from the post by Timothy J. Wood to the
8 + *  omnigroup macosx-dev list:
9 + *    Mach Exception Handlers 101 (Was Re: ptrace, gdb)
10 + *    tjw@omnigroup.com Sun, 4 Jun 2000
11 + *    www.omnigroup.com/mailman/archive/macosx-dev/2000-June/002030.html
12 + *
13   *  Basilisk II (C) 1997-2002 Christian Bauer
14   *
15   *  This program is free software; you can redistribute it and/or modify
# Line 212 | Line 218 | static void powerpc_decode_instruction(i
218   #define SIGSEGV_ALL_SIGNALS                             FAULT_HANDLER(SIGSEGV)
219   #endif
220   #define SIGSEGV_FAULT_HANDLER_ARGLIST   int sig, siginfo_t *sip, void *scp
221 + #define SIGSEGV_FAULT_HANDLER_ARGLIST_1 siginfo_t *sip, void *scp
222 + #define SIGSEGV_FAULT_HANDLER_ARGS              sip, scp
223   #define SIGSEGV_FAULT_ADDRESS                   sip->si_addr
224 < #if defined(__NetBSD__) || defined(__FreeBSD__)
224 > #if defined(__sun__)
225 > #if (defined(sparc) || defined(__sparc__))
226 > #include <sys/ucontext.h>
227 > #define SIGSEGV_CONTEXT_REGS                    (((ucontext_t *)scp)->uc_mcontext.gregs)
228 > #define SIGSEGV_FAULT_INSTRUCTION               SIGSEGV_CONTEXT_REGS[REG_PC]
229 > #endif
230 > #endif
231 > #if defined(__FreeBSD__)
232   #if (defined(i386) || defined(__i386__))
233   #define SIGSEGV_FAULT_INSTRUCTION               (((struct sigcontext *)scp)->sc_eip)
234   #define SIGSEGV_REGISTER_FILE                   ((unsigned int *)&(((struct sigcontext *)scp)->sc_edi)) /* EDI is the first GPR (even below EIP) in sigcontext */
# Line 254 | Line 269 | static void powerpc_decode_instruction(i
269   #if (defined(i386) || defined(__i386__))
270   #include <asm/sigcontext.h>
271   #define SIGSEGV_FAULT_HANDLER_ARGLIST   int sig, struct sigcontext scs
272 < #define SIGSEGV_FAULT_ADDRESS                   scs.cr2
273 < #define SIGSEGV_FAULT_INSTRUCTION               scs.eip
274 < #define SIGSEGV_REGISTER_FILE                   (unsigned int *)(&scs)
272 > #define SIGSEGV_FAULT_HANDLER_ARGLIST_1 struct sigcontext *scp
273 > #define SIGSEGV_FAULT_HANDLER_ARGS              &scs
274 > #define SIGSEGV_FAULT_ADDRESS                   scp->cr2
275 > #define SIGSEGV_FAULT_INSTRUCTION               scp->eip
276 > #define SIGSEGV_REGISTER_FILE                   (unsigned int *)scp
277   #define SIGSEGV_SKIP_INSTRUCTION                ix86_skip_instruction
278   #endif
279   #if (defined(sparc) || defined(__sparc__))
280   #include <asm/sigcontext.h>
281   #define SIGSEGV_FAULT_HANDLER_ARGLIST   int sig, int code, struct sigcontext *scp, char *addr
282 + #define SIGSEGV_FAULT_HANDLER_ARGS              sig, code, scp, addr
283   #define SIGSEGV_FAULT_ADDRESS                   addr
284   #endif
285   #if (defined(powerpc) || defined(__powerpc__))
286   #include <asm/sigcontext.h>
287   #define SIGSEGV_FAULT_HANDLER_ARGLIST   int sig, struct sigcontext *scp
288 + #define SIGSEGV_FAULT_HANDLER_ARGS              sig, scp
289   #define SIGSEGV_FAULT_ADDRESS                   scp->regs->dar
290   #define SIGSEGV_FAULT_INSTRUCTION               scp->regs->nip
291   #define SIGSEGV_REGISTER_FILE                   (unsigned int *)&scp->regs->nip, (unsigned int *)(scp->regs->gpr)
# Line 275 | Line 294 | static void powerpc_decode_instruction(i
294   #if (defined(alpha) || defined(__alpha__))
295   #include <asm/sigcontext.h>
296   #define SIGSEGV_FAULT_HANDLER_ARGLIST   int sig, int code, struct sigcontext *scp
297 + #define SIGSEGV_FAULT_HANDLER_ARGS              sig, code, scp
298   #define SIGSEGV_FAULT_ADDRESS                   get_fault_address(scp)
299   #define SIGSEGV_FAULT_INSTRUCTION               scp->sc_pc
280
281 // From Boehm's GC 6.0alpha8
282 static sigsegv_address_t get_fault_address(struct sigcontext *scp)
283 {
284        unsigned int instruction = *((unsigned int *)(scp->sc_pc));
285        unsigned long fault_address = scp->sc_regs[(instruction >> 16) & 0x1f];
286        fault_address += (signed long)(signed short)(instruction & 0xffff);
287        return (sigsegv_address_t)fault_address;
288 }
300   #endif
301   #endif
302  
# Line 293 | Line 304 | static sigsegv_address_t get_fault_addre
304   #if (defined(sgi) || defined(__sgi)) && (defined(SYSTYPE_SVR4) || defined(__SYSTYPE_SVR4))
305   #include <ucontext.h>
306   #define SIGSEGV_FAULT_HANDLER_ARGLIST   int sig, int code, struct sigcontext *scp
307 + #define SIGSEGV_FAULT_HANDLER_ARGS              sig, code, scp
308   #define SIGSEGV_FAULT_ADDRESS                   scp->sc_badvaddr
309   #define SIGSEGV_ALL_SIGNALS                             FAULT_HANDLER(SIGSEGV)
310   #endif
# Line 300 | Line 312 | static sigsegv_address_t get_fault_addre
312   // HP-UX
313   #if (defined(hpux) || defined(__hpux__))
314   #define SIGSEGV_FAULT_HANDLER_ARGLIST   int sig, int code, struct sigcontext *scp
315 + #define SIGSEGV_FAULT_HANDLER_ARGS              sig, code, scp
316   #define SIGSEGV_FAULT_ADDRESS                   scp->sc_sl.sl_ss.ss_narrow.ss_cr21
317   #define SIGSEGV_ALL_SIGNALS                             FAULT_HANDLER(SIGSEGV) FAULT_HANDLER(SIGBUS)
318   #endif
# Line 308 | Line 321 | static sigsegv_address_t get_fault_addre
321   #if defined(__osf__)
322   #include <ucontext.h>
323   #define SIGSEGV_FAULT_HANDLER_ARGLIST   int sig, int code, struct sigcontext *scp
324 + #define SIGSEGV_FAULT_HANDLER_ARGS              sig, code, scp
325   #define SIGSEGV_FAULT_ADDRESS                   scp->sc_traparg_a0
326   #define SIGSEGV_ALL_SIGNALS                             FAULT_HANDLER(SIGSEGV)
327   #endif
# Line 315 | Line 329 | static sigsegv_address_t get_fault_addre
329   // AIX
330   #if defined(_AIX)
331   #define SIGSEGV_FAULT_HANDLER_ARGLIST   int sig, int code, struct sigcontext *scp
332 + #define SIGSEGV_FAULT_HANDLER_ARGS              sig, code, scp
333   #define SIGSEGV_FAULT_ADDRESS                   scp->sc_jmpbuf.jmp_context.o_vaddr
334   #define SIGSEGV_ALL_SIGNALS                             FAULT_HANDLER(SIGSEGV)
335   #endif
336  
337 < // NetBSD or FreeBSD
338 < #if defined(__NetBSD__) || defined(__FreeBSD__)
337 > // NetBSD
338 > #if defined(__NetBSD__)
339   #if (defined(m68k) || defined(__m68k__))
340   #include <m68k/frame.h>
341   #define SIGSEGV_FAULT_HANDLER_ARGLIST   int sig, int code, struct sigcontext *scp
342 + #define SIGSEGV_FAULT_HANDLER_ARGS              sig, code, scp
343   #define SIGSEGV_FAULT_ADDRESS                   get_fault_address(scp)
344   #define SIGSEGV_ALL_SIGNALS                             FAULT_HANDLER(SIGSEGV)
345  
# Line 347 | Line 363 | static sigsegv_address_t get_fault_addre
363          }
364          return (sigsegv_address_t)fault_addr;
365   }
366 < #else
367 < #define SIGSEGV_FAULT_HANDLER_ARGLIST   int sig, int code, void *scp, char *addr
368 < #define SIGSEGV_FAULT_ADDRESS                   addr
366 > #endif
367 > #if (defined(alpha) || defined(__alpha__))
368 > #define SIGSEGV_FAULT_HANDLER_ARGLIST   int sig, int code, struct sigcontext *scp
369 > #define SIGSEGV_FAULT_HANDLER_ARGS              sig, code, scp
370 > #define SIGSEGV_FAULT_ADDRESS                   get_fault_address(scp)
371 > #define SIGSEGV_ALL_SIGNALS                             FAULT_HANDLER(SIGBUS)
372 > #endif
373 > #if (defined(i386) || defined(__i386__))
374 > #error "FIXME: need to decode instruction and compute EA"
375 > #define SIGSEGV_FAULT_HANDLER_ARGLIST   int sig, int code, struct sigcontext *scp
376 > #define SIGSEGV_FAULT_HANDLER_ARGS              sig, code, scp
377 > #define SIGSEGV_ALL_SIGNALS                             FAULT_HANDLER(SIGSEGV)
378 > #endif
379 > #endif
380 > #if defined(__FreeBSD__)
381   #define SIGSEGV_ALL_SIGNALS                             FAULT_HANDLER(SIGBUS)
382 + #if (defined(i386) || defined(__i386__))
383 + #define SIGSEGV_FAULT_HANDLER_ARGLIST   int sig, int code, struct sigcontext *scp, char *addr
384 + #define SIGSEGV_FAULT_HANDLER_ARGS              sig, code, scp, addr
385 + #define SIGSEGV_FAULT_ADDRESS                   addr
386 + #define SIGSEGV_FAULT_INSTRUCTION               scp->sc_eip
387 + #define SIGSEGV_REGISTER_FILE                   ((unsigned int *)&scp->sc_edi)
388 + #define SIGSEGV_SKIP_INSTRUCTION                ix86_skip_instruction
389 + #endif
390   #endif
391 +
392 + // Extract fault address out of a sigcontext
393 + #if (defined(alpha) || defined(__alpha__))
394 + // From Boehm's GC 6.0alpha8
395 + static sigsegv_address_t get_fault_address(struct sigcontext *scp)
396 + {
397 +        unsigned int instruction = *((unsigned int *)(scp->sc_pc));
398 +        unsigned long fault_address = scp->sc_regs[(instruction >> 16) & 0x1f];
399 +        fault_address += (signed long)(signed short)(instruction & 0xffff);
400 +        return (sigsegv_address_t)fault_address;
401 + }
402   #endif
403  
404 < // MacOS X
404 >
405 > // MacOS X, not sure which version this works in. Under 10.1
406 > // vm_protect does not appear to work from a signal handler. Under
407 > // 10.2 signal handlers get siginfo type arguments but the si_addr
408 > // field is the address of the faulting instruction and not the
409 > // address that caused the SIGBUS. Maybe this works in 10.0? In any
410 > // case with Mach exception handlers there is a way to do what this
411 > // was meant to do.
412 > #ifndef HAVE_MACH_EXCEPTIONS
413   #if defined(__APPLE__) && defined(__MACH__)
414   #if (defined(ppc) || defined(__ppc__))
415   #define SIGSEGV_FAULT_HANDLER_ARGLIST   int sig, int code, struct sigcontext *scp
416 + #define SIGSEGV_FAULT_HANDLER_ARGS              sig, code, scp
417   #define SIGSEGV_FAULT_ADDRESS                   get_fault_address(scp)
418   #define SIGSEGV_FAULT_INSTRUCTION               scp->sc_ir
419   #define SIGSEGV_ALL_SIGNALS                             FAULT_HANDLER(SIGBUS)
# Line 377 | Line 433 | static sigsegv_address_t get_fault_addre
433   #endif
434   #endif
435   #endif
436 + #endif
437 +
438 + #if HAVE_MACH_EXCEPTIONS
439 +
440 + // This can easily be extended to other Mach systems, but really who
441 + // uses HURD (oops GNU/HURD), Darwin/x86, NextStep, Rhapsody, or CMU
442 + // Mach 2.5/3.0?
443 + #if defined(__APPLE__) && defined(__MACH__)
444 +
445 + #include <sys/types.h>
446 + #include <stdlib.h>
447 + #include <stdio.h>
448 + #include <pthread.h>
449 +
450 + /*
451 + * If you are familiar with MIG then you will understand the frustration
452 + * that was necessary to get these embedded into C++ code by hand.
453 + */
454 + extern "C" {
455 + #include <mach/mach.h>
456 + #include <mach/mach_error.h>
457 +
458 + extern boolean_t exc_server(mach_msg_header_t *, mach_msg_header_t *);
459 + extern kern_return_t catch_exception_raise(mach_port_t, mach_port_t,
460 +        mach_port_t, exception_type_t, exception_data_t, mach_msg_type_number_t);
461 + extern kern_return_t exception_raise(mach_port_t, mach_port_t, mach_port_t,
462 +        exception_type_t, exception_data_t, mach_msg_type_number_t);
463 + extern kern_return_t exception_raise_state(mach_port_t, exception_type_t,
464 +        exception_data_t, mach_msg_type_number_t, thread_state_flavor_t *,
465 +        thread_state_t, mach_msg_type_number_t, thread_state_t, mach_msg_type_number_t *);
466 + extern kern_return_t exception_raise_state_identity(mach_port_t, mach_port_t, mach_port_t,
467 +        exception_type_t, exception_data_t, mach_msg_type_number_t, thread_state_flavor_t *,
468 +        thread_state_t, mach_msg_type_number_t, thread_state_t, mach_msg_type_number_t *);
469 + }
470 +
471 + // Could make this dynamic by looking for a result of MIG_ARRAY_TOO_LARGE
472 + #define HANDLER_COUNT 64
473 +
474 + // structure to tuck away existing exception handlers
475 + typedef struct _ExceptionPorts {
476 +        mach_msg_type_number_t maskCount;
477 +        exception_mask_t masks[HANDLER_COUNT];
478 +        exception_handler_t handlers[HANDLER_COUNT];
479 +        exception_behavior_t behaviors[HANDLER_COUNT];
480 +        thread_state_flavor_t flavors[HANDLER_COUNT];
481 + } ExceptionPorts;
482 +
483 + // exception handler thread
484 + static pthread_t exc_thread;
485 +
486 + // place where old exception handler info is stored
487 + static ExceptionPorts ports;
488 +
489 + // our exception port
490 + static mach_port_t _exceptionPort = MACH_PORT_NULL;
491 +
492 + #define MACH_CHECK_ERROR(name,ret) \
493 + if (ret != KERN_SUCCESS) { \
494 +        mach_error(#name, ret); \
495 +        exit (1); \
496 + }
497 +
498 + #define SIGSEGV_FAULT_ADDRESS                   code[1]
499 + #define SIGSEGV_FAULT_INSTRUCTION               get_fault_instruction(thread, state)
500 + #define SIGSEGV_FAULT_HANDLER_INVOKE(ADDR, IP)  ((code[0] == KERN_PROTECTION_FAILURE) ? sigsegv_fault_handler(ADDR, IP) : SIGSEGV_RETURN_FAILURE)
501 + #define SIGSEGV_FAULT_HANDLER_ARGLIST   mach_port_t thread, exception_data_t code, ppc_thread_state_t *state
502 + #define SIGSEGV_FAULT_HANDLER_ARGS              thread, code, &state
503 + #define SIGSEGV_SKIP_INSTRUCTION                powerpc_skip_instruction
504 + #define SIGSEGV_REGISTER_FILE                   &state->srr0, &state->r0
505 +
506 + // Given a suspended thread, stuff the current instruction and
507 + // registers into state.
508 + //
509 + // It would have been nice to have this be ppc/x86 independant which
510 + // could have been done easily with a thread_state_t instead of
511 + // ppc_thread_state_t, but because of the way this is called it is
512 + // easier to do it this way.
513 + #if (defined(ppc) || defined(__ppc__))
514 + static inline sigsegv_address_t get_fault_instruction(mach_port_t thread, ppc_thread_state_t *state)
515 + {
516 +        kern_return_t krc;
517 +        mach_msg_type_number_t count;
518 +
519 +        count = MACHINE_THREAD_STATE_COUNT;
520 +        krc = thread_get_state(thread, MACHINE_THREAD_STATE, (thread_state_t)state, &count);
521 +        MACH_CHECK_ERROR (thread_get_state, krc);
522 +
523 +        return (sigsegv_address_t)state->srr0;
524 + }
525 + #endif
526 +
527 + // Since there can only be one exception thread running at any time
528 + // this is not a problem.
529 + #define MSG_SIZE 512
530 + static char msgbuf[MSG_SIZE];
531 + static char replybuf[MSG_SIZE];
532 +
533 + /*
534 + * This is the entry point for the exception handler thread. The job
535 + * of this thread is to wait for exception messages on the exception
536 + * port that was setup beforehand and to pass them on to exc_server.
537 + * exc_server is a MIG generated function that is a part of Mach.
538 + * Its job is to decide what to do with the exception message. In our
539 + * case exc_server calls catch_exception_raise on our behalf. After
540 + * exc_server returns, it is our responsibility to send the reply.
541 + */
542 + static void *
543 + handleExceptions(void *priv)
544 + {
545 +        mach_msg_header_t *msg, *reply;
546 +        kern_return_t krc;
547 +
548 +        msg = (mach_msg_header_t *)msgbuf;
549 +        reply = (mach_msg_header_t *)replybuf;
550 +
551 +        for (;;) {
552 +                krc = mach_msg(msg, MACH_RCV_MSG, MSG_SIZE, MSG_SIZE,
553 +                                _exceptionPort, 0, MACH_PORT_NULL);
554 +                MACH_CHECK_ERROR(mach_msg, krc);
555 +
556 +                if (!exc_server(msg, reply)) {
557 +                        fprintf(stderr, "exc_server hated the message\n");
558 +                        exit(1);
559 +                }
560 +
561 +                krc = mach_msg(reply, MACH_SEND_MSG, reply->msgh_size, 0,
562 +                                 msg->msgh_local_port, 0, MACH_PORT_NULL);
563 +                if (krc != KERN_SUCCESS) {
564 +                        fprintf(stderr, "Error sending message to original reply port, krc = %d, %s",
565 +                                krc, mach_error_string(krc));
566 +                        exit(1);
567 +                }
568 +        }
569 + }
570 + #endif
571 + #endif
572  
573  
574   /*
# Line 616 | Line 808 | static bool powerpc_skip_instruction(uns
808   #ifndef SIGSEGV_FAULT_INSTRUCTION
809   #define SIGSEGV_FAULT_INSTRUCTION               SIGSEGV_INVALID_PC
810   #endif
811 + #ifndef SIGSEGV_FAULT_HANDLER_ARGLIST_1
812 + #define SIGSEGV_FAULT_HANDLER_ARGLIST_1 SIGSEGV_FAULT_HANDLER_ARGLIST
813 + #endif
814 + #ifndef SIGSEGV_FAULT_HANDLER_INVOKE
815 + #define SIGSEGV_FAULT_HANDLER_INVOKE(ADDR, IP)  sigsegv_fault_handler(ADDR, IP)
816 + #endif
817  
818   // SIGSEGV recovery supported ?
819   #if defined(SIGSEGV_ALL_SIGNALS) && defined(SIGSEGV_FAULT_HANDLER_ARGLIST) && defined(SIGSEGV_FAULT_ADDRESS)
# Line 627 | Line 825 | static bool powerpc_skip_instruction(uns
825   *  SIGSEGV global handler
826   */
827  
828 < #ifdef HAVE_SIGSEGV_RECOVERY
829 < static void sigsegv_handler(SIGSEGV_FAULT_HANDLER_ARGLIST)
828 > #if defined(HAVE_SIGSEGV_RECOVERY) || defined(HAVE_MACH_EXCEPTIONS)
829 > // This function handles the badaccess to memory.
830 > // It is called from the signal handler or the exception handler.
831 > static bool handle_badaccess(SIGSEGV_FAULT_HANDLER_ARGLIST_1)
832   {
833          sigsegv_address_t fault_address = (sigsegv_address_t)SIGSEGV_FAULT_ADDRESS;
834          sigsegv_address_t fault_instruction = (sigsegv_address_t)SIGSEGV_FAULT_INSTRUCTION;
635        bool fault_recovered = false;
835          
836          // Call user's handler and reinstall the global handler, if required
837 <        switch (sigsegv_fault_handler(fault_address, fault_instruction)) {
837 >        switch (SIGSEGV_FAULT_HANDLER_INVOKE(fault_address, fault_instruction)) {
838          case SIGSEGV_RETURN_SUCCESS:
839 < #if (defined(HAVE_SIGACTION) ? defined(SIGACTION_NEED_REINSTALL) : defined(SIGNAL_NEED_REINSTALL))
840 <                sigsegv_do_install_handler(sig);
642 < #endif
643 <                fault_recovered = true;
644 <                break;
839 >                return true;
840 >
841   #if HAVE_SIGSEGV_SKIP_INSTRUCTION
842          case SIGSEGV_RETURN_SKIP_INSTRUCTION:
843 <                // Call the instruction skipper with the register file available
844 <                if (SIGSEGV_SKIP_INSTRUCTION(SIGSEGV_REGISTER_FILE))
845 <                        fault_recovered = true;
843 >                // Call the instruction skipper with the register file
844 >                // available
845 >                if (SIGSEGV_SKIP_INSTRUCTION(SIGSEGV_REGISTER_FILE)) {
846 > #ifdef HAVE_MACH_EXCEPTIONS
847 >                        // Unlike UNIX signals where the thread state
848 >                        // is modified off of the stack, in Mach we
849 >                        // need to actually call thread_set_state to
850 >                        // have the register values updated.
851 >                        kern_return_t krc;
852 >
853 >                        krc = thread_set_state(thread,
854 >                                                                   MACHINE_THREAD_STATE, (thread_state_t)state,
855 >                                                                   MACHINE_THREAD_STATE_COUNT);
856 >                        MACH_CHECK_ERROR (thread_get_state, krc);
857 > #endif
858 >                        return true;
859 >                }
860                  break;
861   #endif
862          }
863 +        
864 +        // We can't do anything with the fault_address, dump state?
865 +        if (sigsegv_state_dumper != 0)
866 +                sigsegv_state_dumper(fault_address, fault_instruction);
867  
868 <        if (!fault_recovered) {
869 <                // Failure: reinstall default handler for "safe" crash
868 >        return false;
869 > }
870 > #endif
871 >
872 >
873 > /*
874 > * There are two mechanisms for handling a bad memory access,
875 > * Mach exceptions and UNIX signals. The implementation specific
876 > * code appears below. Its reponsibility is to call handle_badaccess
877 > * which is the routine that handles the fault in an implementation
878 > * agnostic manner. The implementation specific code below is then
879 > * reponsible for checking whether handle_badaccess was able
880 > * to handle the memory access error and perform any implementation
881 > * specific tasks necessary afterwards.
882 > */
883 >
884 > #ifdef HAVE_MACH_EXCEPTIONS
885 > /*
886 > * We need to forward all exceptions that we do not handle.
887 > * This is important, there are many exceptions that may be
888 > * handled by other exception handlers. For example debuggers
889 > * use exceptions and the exception hander is in another
890 > * process in such a case. (Timothy J. Wood states in his
891 > * message to the list that he based this code on that from
892 > * gdb for Darwin.)
893 > */
894 > static inline kern_return_t
895 > forward_exception(mach_port_t thread_port,
896 >                                  mach_port_t task_port,
897 >                                  exception_type_t exception_type,
898 >                                  exception_data_t exception_data,
899 >                                  mach_msg_type_number_t data_count,
900 >                                  ExceptionPorts *oldExceptionPorts)
901 > {
902 >        kern_return_t kret;
903 >        unsigned int portIndex;
904 >        mach_port_t port;
905 >        exception_behavior_t behavior;
906 >        thread_state_flavor_t flavor;
907 >        thread_state_t thread_state;
908 >        mach_msg_type_number_t thread_state_count;
909 >
910 >        for (portIndex = 0; portIndex < oldExceptionPorts->maskCount; portIndex++) {
911 >                if (oldExceptionPorts->masks[portIndex] & (1 << exception_type)) {
912 >                        // This handler wants the exception
913 >                        break;
914 >                }
915 >        }
916 >
917 >        if (portIndex >= oldExceptionPorts->maskCount) {
918 >                fprintf(stderr, "No handler for exception_type = %d. Not fowarding\n", exception_type);
919 >                return KERN_FAILURE;
920 >        }
921 >
922 >        port = oldExceptionPorts->handlers[portIndex];
923 >        behavior = oldExceptionPorts->behaviors[portIndex];
924 >        flavor = oldExceptionPorts->flavors[portIndex];
925 >
926 >        /*
927 >         fprintf(stderr, "forwarding exception, port = 0x%x, behaviour = %d, flavor = %d\n", port, behavior, flavor);
928 >         */
929 >
930 >        if (behavior != EXCEPTION_DEFAULT) {
931 >                thread_state_count = THREAD_STATE_MAX;
932 >                kret = thread_get_state (thread_port, flavor, thread_state,
933 >                                                                 &thread_state_count);
934 >                MACH_CHECK_ERROR (thread_get_state, kret);
935 >        }
936 >
937 >        switch (behavior) {
938 >        case EXCEPTION_DEFAULT:
939 >          // fprintf(stderr, "forwarding to exception_raise\n");
940 >          kret = exception_raise(port, thread_port, task_port, exception_type,
941 >                                                         exception_data, data_count);
942 >          MACH_CHECK_ERROR (exception_raise, kret);
943 >          break;
944 >        case EXCEPTION_STATE:
945 >          // fprintf(stderr, "forwarding to exception_raise_state\n");
946 >          kret = exception_raise_state(port, exception_type, exception_data,
947 >                                                                   data_count, &flavor,
948 >                                                                   thread_state, thread_state_count,
949 >                                                                   thread_state, &thread_state_count);
950 >          MACH_CHECK_ERROR (exception_raise_state, kret);
951 >          break;
952 >        case EXCEPTION_STATE_IDENTITY:
953 >          // fprintf(stderr, "forwarding to exception_raise_state_identity\n");
954 >          kret = exception_raise_state_identity(port, thread_port, task_port,
955 >                                                                                        exception_type, exception_data,
956 >                                                                                        data_count, &flavor,
957 >                                                                                        thread_state, thread_state_count,
958 >                                                                                        thread_state, &thread_state_count);
959 >          MACH_CHECK_ERROR (exception_raise_state_identity, kret);
960 >          break;
961 >        default:
962 >          fprintf(stderr, "forward_exception got unknown behavior\n");
963 >          break;
964 >        }
965 >
966 >        if (behavior != EXCEPTION_DEFAULT) {
967 >                kret = thread_set_state (thread_port, flavor, thread_state,
968 >                                                                 thread_state_count);
969 >                MACH_CHECK_ERROR (thread_set_state, kret);
970 >        }
971 >
972 >        return KERN_SUCCESS;
973 > }
974 >
975 > /*
976 > * This is the code that actually handles the exception.
977 > * It is called by exc_server. For Darwin 5 Apple changed
978 > * this a bit from how this family of functions worked in
979 > * Mach. If you are familiar with that it is a little
980 > * different. The main variation that concerns us here is
981 > * that code is an array of exception specific codes and
982 > * codeCount is a count of the number of codes in the code
983 > * array. In typical Mach all exceptions have a code
984 > * and sub-code. It happens to be the case that for a
985 > * EXC_BAD_ACCESS exception the first entry is the type of
986 > * bad access that occurred and the second entry is the
987 > * faulting address so these entries correspond exactly to
988 > * how the code and sub-code are used on Mach.
989 > *
990 > * This is a MIG interface. No code in Basilisk II should
991 > * call this directley. This has to have external C
992 > * linkage because that is what exc_server expects.
993 > */
994 > kern_return_t
995 > catch_exception_raise(mach_port_t exception_port,
996 >                                          mach_port_t thread,
997 >                                          mach_port_t task,
998 >                                          exception_type_t exception,
999 >                                          exception_data_t code,
1000 >                                          mach_msg_type_number_t codeCount)
1001 > {
1002 >        ppc_thread_state_t state;
1003 >        kern_return_t krc;
1004 >
1005 >        if ((exception == EXC_BAD_ACCESS)  && (codeCount >= 2)) {
1006 >                if (handle_badaccess(SIGSEGV_FAULT_HANDLER_ARGS))
1007 >                        return KERN_SUCCESS;
1008 >        }
1009 >
1010 >        // In Mach we do not need to remove the exception handler.
1011 >        // If we forward the exception, eventually some exception handler
1012 >        // will take care of this exception.
1013 >        krc = forward_exception(thread, task, exception, code, codeCount, &ports);
1014 >
1015 >        return krc;
1016 > }
1017 > #endif
1018 >
1019 > #ifdef HAVE_SIGSEGV_RECOVERY
1020 > // Handle bad memory accesses with signal handler
1021 > static void sigsegv_handler(SIGSEGV_FAULT_HANDLER_ARGLIST)
1022 > {
1023 >        // Call handler and reinstall the global handler, if required
1024 >        if (handle_badaccess(SIGSEGV_FAULT_HANDLER_ARGS)) {
1025 > #if (defined(HAVE_SIGACTION) ? defined(SIGACTION_NEED_REINSTALL) : defined(SIGNAL_NEED_REINSTALL))
1026 >                sigsegv_do_install_handler(sig);
1027 > #endif
1028 >                return;
1029 >        }
1030 >
1031 >        // Failure: reinstall default handler for "safe" crash
1032   #define FAULT_HANDLER(sig) signal(sig, SIG_DFL);
1033 <                SIGSEGV_ALL_SIGNALS
1033 >        SIGSEGV_ALL_SIGNALS
1034   #undef FAULT_HANDLER
659                
660                // We can't do anything with the fault_address, dump state?
661                if (sigsegv_state_dumper != 0)
662                        sigsegv_state_dumper(fault_address, fault_instruction);
663        }
1035   }
1036   #endif
1037  
# Line 705 | Line 1076 | static bool sigsegv_do_install_handler(i
1076   }
1077   #endif
1078  
1079 < bool sigsegv_install_handler(sigsegv_fault_handler_t handler)
1079 > #if defined(HAVE_MACH_EXCEPTIONS)
1080 > static bool sigsegv_do_install_handler(sigsegv_fault_handler_t handler)
1081   {
1082 < #ifdef HAVE_SIGSEGV_RECOVERY
1082 >        /*
1083 >         * Except for the exception port functions, this should be
1084 >         * pretty much stock Mach. If later you choose to support
1085 >         * other Mach's besides Darwin, just check for __MACH__
1086 >         * here and __APPLE__ where the actual differences are.
1087 >         */
1088 > #if defined(__APPLE__) && defined(__MACH__)
1089 >        if (sigsegv_fault_handler != NULL) {
1090 >                sigsegv_fault_handler = handler;
1091 >                return true;
1092 >        }
1093 >
1094 >        kern_return_t krc;
1095 >
1096 >        // create the the exception port
1097 >        krc = mach_port_allocate(mach_task_self(),
1098 >                          MACH_PORT_RIGHT_RECEIVE, &_exceptionPort);
1099 >        if (krc != KERN_SUCCESS) {
1100 >                mach_error("mach_port_allocate", krc);
1101 >                return false;
1102 >        }
1103 >
1104 >        // add a port send right
1105 >        krc = mach_port_insert_right(mach_task_self(),
1106 >                              _exceptionPort, _exceptionPort,
1107 >                              MACH_MSG_TYPE_MAKE_SEND);
1108 >        if (krc != KERN_SUCCESS) {
1109 >                mach_error("mach_port_insert_right", krc);
1110 >                return false;
1111 >        }
1112 >
1113 >        // get the old exception ports
1114 >        ports.maskCount = sizeof (ports.masks) / sizeof (ports.masks[0]);
1115 >        krc = thread_get_exception_ports(mach_thread_self(), EXC_MASK_BAD_ACCESS, ports.masks,
1116 >                                &ports.maskCount, ports.handlers, ports.behaviors, ports.flavors);
1117 >        if (krc != KERN_SUCCESS) {
1118 >                mach_error("thread_get_exception_ports", krc);
1119 >                return false;
1120 >        }
1121 >
1122 >        // set the new exception port
1123 >        //
1124 >        // We could have used EXCEPTION_STATE_IDENTITY instead of
1125 >        // EXCEPTION_DEFAULT to get the thread state in the initial
1126 >        // message, but it turns out that in the common case this is not
1127 >        // neccessary. If we need it we can later ask for it from the
1128 >        // suspended thread.
1129 >        //
1130 >        // Even with THREAD_STATE_NONE, Darwin provides the program
1131 >        // counter in the thread state.  The comments in the header file
1132 >        // seem to imply that you can count on the GPR's on an exception
1133 >        // as well but just to be safe I use MACHINE_THREAD_STATE because
1134 >        // you have to ask for all of the GPR's anyway just to get the
1135 >        // program counter. In any case because of update effective
1136 >        // address from immediate and update address from effective
1137 >        // addresses of ra and rb modes (as good an name as any for these
1138 >        // addressing modes) used in PPC instructions, you will need the
1139 >        // GPR state anyway.
1140 >        krc = thread_set_exception_ports(mach_thread_self(), EXC_MASK_BAD_ACCESS, _exceptionPort,
1141 >                                EXCEPTION_DEFAULT, MACHINE_THREAD_STATE);
1142 >        if (krc != KERN_SUCCESS) {
1143 >                mach_error("thread_set_exception_ports", krc);
1144 >                return false;
1145 >        }
1146 >
1147 >        // create the exception handler thread
1148 >        if (pthread_create(&exc_thread, NULL, &handleExceptions, NULL) != 0) {
1149 >                (void)fprintf(stderr, "creation of exception thread failed\n");
1150 >                return false;
1151 >        }
1152 >
1153 >        // do not care about the exception thread any longer, let is run standalone
1154 >        (void)pthread_detach(exc_thread);
1155 >
1156          sigsegv_fault_handler = handler;
1157 +        return true;
1158 + #else
1159 +        return false;
1160 + #endif
1161 + }
1162 + #endif
1163 +
1164 + bool sigsegv_install_handler(sigsegv_fault_handler_t handler)
1165 + {
1166 + #if defined(HAVE_SIGSEGV_RECOVERY)
1167          bool success = true;
1168   #define FAULT_HANDLER(sig) success = success && sigsegv_do_install_handler(sig);
1169          SIGSEGV_ALL_SIGNALS
1170   #undef FAULT_HANDLER
1171 +        if (success)
1172 +            sigsegv_fault_handler = handler;
1173          return success;
1174 + #elif defined(HAVE_MACH_EXCEPTIONS)
1175 +        return sigsegv_do_install_handler(handler);
1176   #else
1177          // FAIL: no siginfo_t nor sigcontext subterfuge is available
1178          return false;
# Line 727 | Line 1186 | bool sigsegv_install_handler(sigsegv_fau
1186  
1187   void sigsegv_deinstall_handler(void)
1188   {
1189 +  // We do nothing for Mach exceptions, the thread would need to be
1190 +  // suspended if not already so, and we might mess with other
1191 +  // exception handlers that came after we registered ours. There is
1192 +  // no need to remove the exception handler, in fact this function is
1193 +  // not called anywhere in Basilisk II.
1194   #ifdef HAVE_SIGSEGV_RECOVERY
1195          sigsegv_fault_handler = 0;
1196   #define FAULT_HANDLER(sig) signal(sig, SIG_DFL);
# Line 757 | Line 1221 | void sigsegv_set_dump_state(sigsegv_stat
1221   #include <sys/mman.h>
1222   #include "vm_alloc.h"
1223  
1224 + const int REF_INDEX = 123;
1225 + const int REF_VALUE = 45;
1226 +
1227   static int page_size;
1228   static volatile char * page = 0;
1229   static volatile int handler_called = 0;
1230  
1231 + #ifdef __GNUC__
1232 + // Code range where we expect the fault to come from
1233 + static void *b_region, *e_region;
1234 + #endif
1235 +
1236   static sigsegv_return_t sigsegv_test_handler(sigsegv_address_t fault_address, sigsegv_address_t instruction_address)
1237   {
1238          handler_called++;
1239 <        if ((fault_address - 123) != page)
1240 <                exit(1);
1239 >        if ((fault_address - REF_INDEX) != page)
1240 >                exit(10);
1241 > #ifdef __GNUC__
1242 >        // Make sure reported fault instruction address falls into
1243 >        // expected code range
1244 >        if (instruction_address != SIGSEGV_INVALID_PC
1245 >                && ((instruction_address <  (sigsegv_address_t)b_region) ||
1246 >                        (instruction_address >= (sigsegv_address_t)e_region)))
1247 >                exit(11);
1248 > #endif
1249          if (vm_protect((char *)((unsigned long)fault_address & -page_size), page_size, VM_PAGE_READ | VM_PAGE_WRITE) != 0)
1250 <                exit(1);
1250 >                exit(12);
1251          return SIGSEGV_RETURN_SUCCESS;
1252   }
1253  
1254   #ifdef HAVE_SIGSEGV_SKIP_INSTRUCTION
1255   static sigsegv_return_t sigsegv_insn_handler(sigsegv_address_t fault_address, sigsegv_address_t instruction_address)
1256   {
1257 <        if (((unsigned long)fault_address - (unsigned long)page) < page_size)
1258 <                return SIGSEGV_SRETURN_KIP_INSTRUCTION;
1257 >        if (((unsigned long)fault_address - (unsigned long)page) < page_size) {
1258 > #ifdef __GNUC__
1259 >                // Make sure reported fault instruction address falls into
1260 >                // expected code range
1261 >                if (instruction_address != SIGSEGV_INVALID_PC
1262 >                        && ((instruction_address <  (sigsegv_address_t)b_region) ||
1263 >                                (instruction_address >= (sigsegv_address_t)e_region)))
1264 >                        return SIGSEGV_RETURN_FAILURE;
1265 > #endif
1266 >                return SIGSEGV_RETURN_SKIP_INSTRUCTION;
1267 >        }
1268 >
1269          return SIGSEGV_RETURN_FAILURE;
1270   }
1271   #endif
# Line 787 | Line 1277 | int main(void)
1277  
1278          page_size = getpagesize();
1279          if ((page = (char *)vm_acquire(page_size)) == VM_MAP_FAILED)
1280 <                return 1;
1280 >                return 2;
1281          
1282 +        memset((void *)page, 0, page_size);
1283          if (vm_protect((char *)page, page_size, VM_PAGE_READ) < 0)
1284 <                return 1;
1284 >                return 3;
1285          
1286          if (!sigsegv_install_handler(sigsegv_test_handler))
1287 <                return 1;
797 <        
798 <        page[123] = 45;
799 <        page[123] = 45;
1287 >                return 4;
1288          
1289 + #ifdef __GNUC__
1290 +        b_region = &&L_b_region1;
1291 +        e_region = &&L_e_region1;
1292 + #endif
1293 + L_b_region1:
1294 +        page[REF_INDEX] = REF_VALUE;
1295 +        if (page[REF_INDEX] != REF_VALUE)
1296 +          exit(20);
1297 +        page[REF_INDEX] = REF_VALUE;
1298 + L_e_region1:
1299 +
1300          if (handler_called != 1)
1301 <                return 1;
1301 >                return 5;
1302  
1303   #ifdef HAVE_SIGSEGV_SKIP_INSTRUCTION
1304          if (!sigsegv_install_handler(sigsegv_insn_handler))
1305 <                return 1;
1305 >                return 6;
1306          
1307          if (vm_protect((char *)page, page_size, VM_PAGE_READ | VM_PAGE_WRITE) < 0)
1308 <                return 1;
1308 >                return 7;
1309          
1310          for (int i = 0; i < page_size; i++)
1311                  page[i] = (i + 1) % page_size;
1312          
1313          if (vm_protect((char *)page, page_size, VM_PAGE_NOACCESS) < 0)
1314 <                return 1;
1314 >                return 8;
1315          
1316   #define TEST_SKIP_INSTRUCTION(TYPE) do {                                \
1317                  const unsigned int TAG = 0x12345678;                    \
1318                  TYPE data = *((TYPE *)(page + sizeof(TYPE)));   \
1319                  volatile unsigned int effect = data + TAG;              \
1320                  if (effect != TAG)                                                              \
1321 <                        return 1;                                                                       \
1321 >                        return 9;                                                                       \
1322          } while (0)
1323          
1324 + #ifdef __GNUC__
1325 +        b_region = &&L_b_region2;
1326 +        e_region = &&L_e_region2;
1327 + #endif
1328 + L_b_region2:
1329          TEST_SKIP_INSTRUCTION(unsigned char);
1330          TEST_SKIP_INSTRUCTION(unsigned short);
1331          TEST_SKIP_INSTRUCTION(unsigned int);
1332 + L_e_region2:
1333   #endif
1334  
1335          vm_exit();
1336          return 0;
1337   }
1338   #endif
1339 +
1340 +
1341 +
1342 +
1343 +
1344 +
1345 +
1346 +
1347 +
1348 +
1349 +
1350 +
1351 +
1352 +
1353 +

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines