0.7.6.1:
[sbcl.git] / src / runtime / linux-os.c
index 9d8ee0a..0bee001 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,54 +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();
-
-    SET_FPU_CONTROL_WORD(0x1372|4|8|16|32); /* no interrupts */
-}
-
-/* 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;
+    /* This could just as well be in arch_init(), but it's not. */
+#ifdef __i386__
+    /* FIXME: This used to be here.  However, I have just removed it
+       with no apparent ill effects (it may be that earlier kernels
+       started up a process with a different set of traps, or
+       something?) Find out what this was meant to do, and reenable it
+       or delete it if possible. -- CSR, 2002-07-15 */
+    /* SET_FPU_CONTROL_WORD(0x1372|4|8|16|32);  no interrupts */
+#endif
 }
 
 /* In Debian CMU CL ca. 2.4.9, it was possible to get an infinite
@@ -117,15 +101,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 +199,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 +208,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 +221,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 +242,8 @@ 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 +252,49 @@ 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);
+    if (!gencgc_handle_wp_violation(fault_addr)) 
+       if(!handle_control_stack_guard_triggered(context,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;
+
+    addr = arch_get_bad_addr(signal,info,context);
+    if (addr != NULL && 
+       *os_context_register_addr(context,reg_ALLOC) & (1L<<63)){
+       
+       /* Alpha stuff: 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(!interrupt_maybe_gc(signal, info, context))
+           if(!handle_control_stack_guard_triggered(context,addr))
+               interrupt_handle_now(signal, info, context);
     }
 }
+#endif
+
 void
 os_install_interrupt_handlers(void)
 {
-    interrupt_install_low_level_handler(SIGSEGV, sigsegv_handler);
+    undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
+                                                sigsegv_handler);
 }
 
-#endif