0.9.9.36:
[sbcl.git] / src / runtime / bsd-os.c
index f41911d..9b496c3 100644 (file)
@@ -51,6 +51,12 @@ os_vm_size_t os_vm_page_size;
 static void netbsd_init();
 #endif /* __NetBSD__ */
 
+#ifdef __FreeBSD__
+#include <sys/sysctl.h>
+
+static void freebsd_init();
+#endif /* __FreeBSD__ */
+
 void
 os_init(char *argv[], char *envp[])
 {
@@ -59,6 +65,9 @@ os_init(char *argv[], char *envp[])
 #ifdef __NetBSD__
     netbsd_init();
 #endif /* __NetBSD__ */
+#ifdef __FreeBSD__
+    freebsd_init();
+#endif /* __FreeBSD__ */
 }
 
 int *os_context_pc_addr(os_context_t *context)
@@ -126,7 +135,7 @@ os_map(int fd, int offset, os_vm_address_t addr, os_vm_size_t len)
 
     if (addr == MAP_FAILED) {
         perror("mmap");
-        lose("unexpected mmap(..) failure");
+        lose("unexpected mmap(..) failure\n");
     }
 
     return addr;
@@ -179,31 +188,35 @@ 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
-#endif
-
     os_context_t *context = arch_os_get_context(&void_context);
+    void *fault_addr = arch_get_bad_addr(signal, siginfo, 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)
 {
     SHOW("os_install_interrupt_handlers()/bsd-os/defined(GENCGC)");
     undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
                                                  memory_fault_handler);
+#ifdef SIG_MEMORY_FAULT2
+    undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT2,
+                                                 memory_fault_handler);
+#endif
     SHOW("leaving os_install_interrupt_handlers()");
 }
 
@@ -230,6 +243,10 @@ os_install_interrupt_handlers(void)
     SHOW("os_install_interrupt_handlers()/bsd-os/!defined(GENCGC)");
     undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
                                                  sigsegv_handler);
+#ifdef SIG_MEMORY_FAULT2
+    undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT2,
+                                                 sigsegv_handler);
+#endif
 }
 
 #endif /* defined GENCGC */
@@ -271,6 +288,29 @@ The system may fail to start.\n",
     }
 }
 #endif /* __NetBSD__ */
+
+#ifdef __FreeBSD__
+static void freebsd_init()
+{
+    /* Quote from sbcl-devel (NIIMI Satoshi): "Some OSes, like FreeBSD
+     * 4.x with GENERIC kernel, does not enable SSE support even on
+     * SSE capable CPUs". Detect this situation and skip the
+     * fast_bzero sse/base selection logic that's normally done in
+     * x86-assem.S.
+     */
+#ifdef LISP_FEATURE_X86
+    size_t len;
+    int instruction_sse;
+
+    len = sizeof(instruction_sse);
+    if (sysctlbyname("hw.instruction_sse", &instruction_sse, &len, NULL, 0) == 0
+        && instruction_sse != 0) {
+        /* Use the SSE detector */
+        fast_bzero_pointer = fast_bzero_detect;
+    }
+#endif /* LISP_FEATURE_X86 */
+}
+#endif /* __FreeBSD__ */
 \f
 /* threads */
 
@@ -295,3 +335,45 @@ int arch_os_thread_cleanup(struct thread *thread) {
     return 1;                  /* success */
 }
 #endif
+
+#ifdef LISP_FEATURE_DARWIN
+/* defined in ppc-darwin-os.c instead */
+#elif defined(LISP_FEATURE_FREEBSD)
+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 (strcmp(path, "unknown") == 0)
+        return NULL;
+#endif
+    return copied_string(path);
+}
+#else /* Not DARWIN or FREEBSD */
+char *
+os_get_runtime_executable_path()
+{
+    return NULL;
+}
+#endif