0.9.10.30:
[sbcl.git] / src / runtime / bsd-os.c
index 7e9d51f..f9cf70c 100644 (file)
@@ -53,6 +53,7 @@ static void netbsd_init();
 
 #ifdef __FreeBSD__
 #include <sys/sysctl.h>
+#include <osreldate.h>
 
 static void freebsd_init();
 #endif /* __FreeBSD__ */
@@ -78,6 +79,8 @@ int *os_context_pc_addr(os_context_t *context)
     return CONTEXT_ADDR_FROM_STEM(pc);
 #elif defined __NetBSD__
     return CONTEXT_ADDR_FROM_STEM(EIP);
+#elif defined(LISP_FEATURE_DARWIN) && defined(LISP_FEATURE_X86)
+    return CONTEXT_ADDR_FROM_STEM(eip);
 #elif defined LISP_FEATURE_DARWIN
     return &context->uc_mcontext->ss.srr0;
 #else
@@ -91,9 +94,9 @@ os_context_sigmask_addr(os_context_t *context)
     /* (Unlike most of the other context fields that we access, the
      * signal mask field is a field of the basic, outermost context
      * struct itself both in FreeBSD 4.0 and in OpenBSD 2.6.) */
-#if defined __FreeBSD__  || __NetBSD__ || defined LISP_FEATURE_DARWIN
+#if defined(__FreeBSD__)  || defined(__NetBSD__) || defined(LISP_FEATURE_DARWIN)
     return &context->uc_sigmask;
-#elif defined __OpenBSD__
+#elif defined (__OpenBSD__)
     return &context->sc_mask;
 #else
 #error unsupported BSD variant
@@ -188,25 +191,32 @@ is_valid_lisp_addr(os_vm_address_t addr)
 static void
 memory_fault_handler(int signal, siginfo_t *siginfo, void *void_context)
 {
-    /* The way that we extract low level information like the fault
-     * address is not specified by POSIX. */
-#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
-    void *fault_addr = siginfo->si_addr;
-#elif defined LISP_FEATURE_DARWIN
-    void *fault_addr = siginfo->si_addr;
-#else
-#error unsupported BSD variant
+    os_context_t *context = arch_os_get_context(&void_context);
+    void *fault_addr = arch_get_bad_addr(signal, siginfo, context);
+
+#if defined(MEMORY_FAULT_DEBUG)
+    fprintf(stderr, "Memory fault at: %p, PC: %x\n", fault_addr, *os_context_pc_addr(context));
+#if defined(ARCH_HAS_STACK_POINTER)
+    fprintf(stderr, "Stack pointer: %x\n", *os_context_sp_addr(context));
+#endif
 #endif
 
-    os_context_t *context = arch_os_get_context(&void_context);
     if (!gencgc_handle_wp_violation(fault_addr))
-        if(!handle_guard_page_triggered(context,fault_addr))
+        if(!handle_guard_page_triggered(context,fault_addr)) {
 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
             arrange_return_to_lisp_function(context, SymbolFunction(MEMORY_FAULT_ERROR));
 #else
-            interrupt_handle_now(signal, siginfo, context);
+            if (!interrupt_maybe_gc_int(signal, siginfo, context)) {
+                interrupt_handle_now(signal, siginfo, context);
+            }
+#if defined(LISP_FEATURE_DARWIN)
+            /* Work around G5 bug; fix courtesy gbyers */
+            DARWIN_FIX_CONTEXT(context);
+#endif
 #endif
+        }
 }
+
 void
 os_install_interrupt_handlers(void)
 {
@@ -339,35 +349,35 @@ int arch_os_thread_cleanup(struct thread *thread) {
 #ifdef LISP_FEATURE_DARWIN
 /* defined in ppc-darwin-os.c instead */
 #elif defined(LISP_FEATURE_FREEBSD)
+#ifndef KERN_PROC_PATHNAME
+#define KERN_PROC_PATHNAME 12
+#endif
+
 char *
 os_get_runtime_executable_path()
 {
     char path[PATH_MAX + 1];
-    /* Defined on FreeBSD 6 and later. -- JES, 2006-02-06. */
-#ifdef KERN_PROC_PATHNAME
-    size_t len = PATH_MAX + 1;
-    int mib[4];
 
-    mib[0] = CTL_KERN;
-    mib[1] = KERN_PROC;
-    mib[2] = KERN_PROC_PATHNAME;
-    mib[3] = -1;
-    if (sysctl(mib, 4, &path, &len, NULL, 0) != 0)
-        /* FIXME: Should we rather fall back into the /proc/-based
-         * code below, so that an SBCL executable core made on
-         * FreeBSD 6 would have at least some chance of working on
-         * FreeBSD 4? -- JES, 2006-02-06.
-         */
-        return NULL;
-#else
-    int size;
-    size = readlink("/proc/curproc/file", path, sizeof(path) - 1);
-    if (size < 0)
-        return NULL;
-    path[size] = '\0';
+    if (getosreldate() >= 600024) {
+        /* KERN_PROC_PATHNAME is available */
+        size_t len = PATH_MAX + 1;
+        int mib[4];
+
+        mib[0] = CTL_KERN;
+        mib[1] = KERN_PROC;
+        mib[2] = KERN_PROC_PATHNAME;
+        mib[3] = -1;
+        if (sysctl(mib, 4, &path, &len, NULL, 0) != 0)
+            return NULL;
+    } else {
+        int size;
+        size = readlink("/proc/curproc/file", path, sizeof(path) - 1);
+        if (size < 0)
+            return NULL;
+        path[size] = '\0';
+    }
     if (strcmp(path, "unknown") == 0)
         return NULL;
-#endif
     return copied_string(path);
 }
 #else /* Not DARWIN or FREEBSD */