0.7.4.17: Mostly Alpha fixes
[sbcl.git] / src / runtime / linux-os.c
index 9d8ee0a..d44f19f 100644 (file)
@@ -1,5 +1,6 @@
 /*
- * the Linux incarnation of OS-dependent routines
+ * the Linux incarnation of OS-dependent routines.  See also
+ * $(sbcl_arch)-linux-os.c
  *
  * This file (along with os.h) exports an OS-independent interface to
  * the operating system VM facilities. Surprise surprise, this
  * files for more information.
  */
 
-/*
- * $Header$
- */
-
 #include <stdio.h>
 #include <sys/param.h>
 #include <sys/file.h>
@@ -31,6 +28,7 @@
 #include "arch.h"
 #include "globals.h"
 #include "interrupt.h"
+#include "interr.h"
 #include "lispregs.h"
 #include "sbcl.h"
 #include <sys/socket.h>
 #include <sys/stat.h>
 #include <unistd.h>
 
-#include "x86-validate.h"
+#include "validate.h"
 size_t os_vm_page_size;
 
 #if defined GENCGC
 #include "gencgc.h"
 #endif
 \f
+
+#ifdef sparc
+int early_kernel = 0;
+#endif
 void os_init(void)
 {
     /* Early versions of Linux don't support the mmap(..) functionality
@@ -57,55 +59,36 @@ void os_init(void)
     {
         struct utsname name;
        int major_version;
+#ifdef sparc
+       int minor_version;
+#endif
        uname(&name);
        major_version = atoi(name.release);
        if (major_version < 2) {
            lose("linux major version=%d (can't run in version < 2.0.0)",
                 major_version);
        }
+#ifdef sparc
+       /* KLUDGE: This will break if Linux moves to a uname() version number
+        * that has more than one digit initially -- CSR, 2002-02-12 */
+       minor_version = atoi(name.release+2);
+       if (minor_version < 4) {
+           FSHOW((stderr,"linux minor version=%d;\n enabling workarounds for SPARC kernel bugs in signal handling.\n", minor_version));
+           early_kernel = 1;
+       }
+#endif
     }
 
     os_vm_page_size = getpagesize();
-
+   /* This could just as well be in arch_init(), but it's not. */
+#ifdef __i386__
     SET_FPU_CONTROL_WORD(0x1372|4|8|16|32); /* no interrupts */
+#endif
 }
 
-/* KLUDGE: As of kernel 2.2.14 on Red Hat 6.2, there's code in the
- * <sys/ucontext.h> file to define symbolic names for offsets into
- * gregs[], but it's conditional on __USE_GNU and not defined, so
- * we need to do this nasty absolute index magic number thing
- * instead. */
-int *
-os_context_register_addr(os_context_t *context, int offset)
-{
-    switch(offset) {
-    case  0: return &context->uc_mcontext.gregs[11]; /* EAX */
-    case  2: return &context->uc_mcontext.gregs[10]; /* ECX */
-    case  4: return &context->uc_mcontext.gregs[9]; /* EDX */
-    case  6: return &context->uc_mcontext.gregs[8]; /* EBX */
-    case  8: return &context->uc_mcontext.gregs[7]; /* ESP */
-    case 10: return &context->uc_mcontext.gregs[6]; /* EBP */
-    case 12: return &context->uc_mcontext.gregs[5]; /* ESI */
-    case 14: return &context->uc_mcontext.gregs[4]; /* EDI */
-    default: return 0;
-    }
-}
-int *
-os_context_pc_addr(os_context_t *context)
-{
-    return &context->uc_mcontext.gregs[14];
-}
-int *
-os_context_sp_addr(os_context_t *context)
-{
-    return &context->uc_mcontext.gregs[17];
-}
-
-sigset_t *
-os_context_sigmask_addr(os_context_t *context)
-{
-    return &context->uc_sigmask;
-}
+/* various os_context_*_addr accessors moved to {x86,alpha}-linux-os.c 
+ * -dan 20010125
+ */
 
 /* In Debian CMU CL ca. 2.4.9, it was possible to get an infinite
  * cascade of errors from do_mmap(..). This variable is a counter to
@@ -117,15 +100,15 @@ int n_do_mmap_ignorable_errors = 3;
 static int
 do_mmap(os_vm_address_t *addr, os_vm_size_t len, int flags)
 {
-    /* We *must* have the memory where we want it. */
-    os_vm_address_t old_addr=*addr;
+    /* We *must* have the memory where we expect it. */
+    os_vm_address_t old_addr = *addr;
 
     *addr = mmap(*addr, len, OS_VM_PROT_ALL, flags, -1, 0);
     if (*addr == MAP_FAILED ||
        ((old_addr != NULL) && (*addr != old_addr))) {
         FSHOW((stderr,
-              "error in allocating memory from the OS\n"
-              "(addr=%lx, len=%lx, flags=%lx)\n",
+              "/retryable error in allocating memory from the OS\n"
+              "(addr=0x%lx, len=0x%lx, flags=0x%lx)\n",
               (long) addr,
               (long) len,
               (long) flags));
@@ -215,7 +198,7 @@ os_map(int fd, int offset, os_vm_address_t addr, os_vm_size_t len)
                MAP_PRIVATE | MAP_FILE | MAP_FIXED,
                fd, (off_t) offset);
 
-    if(addr == MAP_FAILED) {
+    if (addr == MAP_FAILED) {
        perror("mmap");
        lose("unexpected mmap(..) failure");
     }
@@ -224,11 +207,6 @@ os_map(int fd, int offset, os_vm_address_t addr, os_vm_size_t len)
 }
 
 void
-os_flush_icache(os_vm_address_t address, os_vm_size_t length)
-{
-}
-
-void
 os_protect(os_vm_address_t address, os_vm_size_t length, os_vm_prot_t prot)
 {
     if (mprotect(address, length, prot) == -1) {
@@ -242,8 +220,8 @@ os_protect(os_vm_address_t address, os_vm_size_t length, os_vm_prot_t prot)
 static boolean
 in_range_p(os_vm_address_t a, lispobj sbeg, size_t slen)
 {
-    char* beg = (char*)sbeg;
-    char* end = (char*)sbeg + slen;
+    char* beg = (char*)((long)sbeg);
+    char* end = (char*)((long)sbeg) + slen;
     char* adr = (char*)a;
     return (adr >= beg && adr < end);
 }
@@ -263,13 +241,7 @@ is_valid_lisp_addr(os_vm_address_t addr)
  * any OS-dependent special low-level handling for signals
  */
 
-#if !defined GENCGC
-
-void
-os_install_interrupt_handlers(void)
-{}
-
-#else
+#if defined GENCGC
 
 /*
  * The GENCGC needs to be hooked into whatever signal is raised for
@@ -278,16 +250,64 @@ os_install_interrupt_handlers(void)
 void
 sigsegv_handler(int signal, siginfo_t *info, void* void_context)
 {
-    os_context_t *context = (os_context_t*)void_context;
+    os_context_t *context = arch_os_get_context(&void_context);
     void* fault_addr = (void*)context->uc_mcontext.cr2;
     if (!gencgc_handle_wp_violation(fault_addr)) {
        interrupt_handle_now(signal, info, void_context);
     }
 }
+
+#else
+
+static void
+sigsegv_handler(int signal, siginfo_t *info, void* void_context)
+{
+    os_context_t *context = arch_os_get_context(&void_context);
+    os_vm_address_t addr;
+
+#ifdef __i386__
+    interrupt_handle_now(signal,contextstruct);
+#else
+    char *control_stack_top = (char*)CONTROL_STACK_START + CONTROL_STACK_SIZE;
+    
+    addr = arch_get_bad_addr(signal,info,context);
+
+    if (addr != NULL && 
+       *os_context_register_addr(context,reg_ALLOC) & (1L<<63)){
+       
+       /* This is the end of a pseudo-atomic section during which
+        * a signal was received.  We must deal with the pending interrupt
+        * (see also interrupt.c, ../code/interrupt.lisp)
+        */
+       /* (how we got here: when interrupting, we set bit 63 in
+        * reg_Alloc.  At the end of the atomic section we tried to
+        * write to reg_ALLOC, got a SIGSEGV (there's nothing mapped
+        * there) so ended up here
+        */
+       *os_context_register_addr(context,reg_ALLOC) -= (1L<<63);
+       interrupt_handle_pending(context);
+    } else if (addr > control_stack_top && addr < BINDING_STACK_START) {
+       fprintf(stderr,
+               "Possible stack overflow at 0x%016lX:\n"
+               "control_stack_top=%lx, BINDING_STACK_START=%lx\n",
+               addr,
+               control_stack_top,
+               BINDING_STACK_START);
+       /* Try to fix control frame pointer. */
+       while ( ! (CONTROL_STACK_START <= *current_control_frame_pointer &&
+                  *current_control_frame_pointer <= control_stack_top))
+           ((char*)current_control_frame_pointer) -= sizeof(lispobj);
+       monitor_or_something();
+    } else if (!interrupt_maybe_gc(signal, info, context)) {
+       interrupt_handle_now(signal, info, context);
+    }
+#endif
+}
+#endif
+
 void
 os_install_interrupt_handlers(void)
 {
-    interrupt_install_low_level_handler(SIGSEGV, sigsegv_handler);
+    undoably_install_low_level_interrupt_handler(SIGSEGV, sigsegv_handler);
 }
 
-#endif