Set up alien stack correctly on non-x86oids.
authorStas Boukarev <stassats@gmail.com>
Thu, 15 Aug 2013 17:52:24 +0000 (21:52 +0400)
committerStas Boukarev <stassats@gmail.com>
Thu, 15 Aug 2013 17:52:24 +0000 (21:52 +0400)
It's assumed that the C stack grows upward everywhere but X86oids,
which is not true. Define two new conditions,
ALIEN_STACK_GROWS_DOWNWARD and ALIEN_STACK_GROWS_UPWARD.

This fixes FFI issues on PPC.

13 files changed:
NEWS
src/runtime/alpha-arch.h
src/runtime/hppa-arch.h
src/runtime/mips-arch.h
src/runtime/ppc-arch.h
src/runtime/sparc-arch.h
src/runtime/thread.c
src/runtime/validate.h
src/runtime/x86-64-arch.h
src/runtime/x86-arch.h
tests/alloca.c [new file with mode: 0644]
tests/run-compiler.sh
tests/threads.impure.lisp

diff --git a/NEWS b/NEWS
index 7327aaa..32b6bd2 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -10,6 +10,7 @@ changes relative to sbcl-1.1.10
   * bug fix: signal a warning when defining a setf-function when a
     setf-expander is already present.
     Patch by Douglas Katzman.
+  * bug fix: improved threading on PPC.
 
 changes in sbcl-1.1.10 relative to sbcl-1.1.9:
   * enhancement: ASDF has been updated to 3.0.2.
index c3d82f7..872846e 100644 (file)
@@ -15,5 +15,6 @@ release_spinlock(lispobj *word)
 }
 
 #define ARCH_HAS_FLOAT_REGISTERS
+#define ALIEN_STACK_GROWS_DOWNWARD
 
 #endif /* _ALPHA_ARCH_H */
index 58cecc2..65d7295 100644 (file)
@@ -15,5 +15,6 @@ release_spinlock(lispobj *word)
 }
 
 #define ARCH_HAS_NPC_REGISTER
+#define ALIEN_STACK_GROWS_UPWARD
 
 #endif /* _HPPA_ARCH_H */
index 4ac1b78..cedd280 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef _MIPS_ARCH_H
 #define _MIPS_ARCH_H
 
+#define ALIEN_STACK_GROWS_DOWNWARD
 
 static inline void
 get_spinlock(volatile lispobj *word, long value)
index 515335f..a5fe1e9 100644 (file)
@@ -49,6 +49,7 @@ swap_lispobjs(volatile lispobj *dest, lispobj value)
 #endif
 
 #define ARCH_HAS_LINK_REGISTER
+#define ALIEN_STACK_GROWS_DOWNWARD
 
 extern void ppc_flush_icache(os_vm_address_t address, os_vm_size_t length);
 
index 6a7b52e..d49190c 100644 (file)
@@ -14,5 +14,6 @@ release_spinlock(lispobj *word)
 }
 
 #define ARCH_HAS_NPC_REGISTER
+#define ALIEN_STACK_GROWS_DOWNWARD
 
 #endif /* _SPARC_ARCH_H */
index 63527ba..1889213 100644 (file)
@@ -695,7 +695,7 @@ create_thread_struct(lispobj initial_function) {
     th->state_not_stopped_waitcount = 0;
 #endif
     th->state=STATE_RUNNING;
-#ifdef LISP_FEATURE_STACK_GROWS_DOWNWARD_NOT_UPWARD
+#ifdef ALIEN_STACK_GROWS_DOWNWARD
     th->alien_stack_pointer=((void *)th->alien_stack_start
                              + ALIEN_STACK_SIZE-N_WORD_BYTES);
 #else
index 7dfb73d..c445e7e 100644 (file)
@@ -38,6 +38,7 @@
 
 #if !defined(LANGUAGE_ASSEMBLY)
 #include <thread.h>
+
 #ifdef LISP_FEATURE_STACK_GROWS_DOWNWARD_NOT_UPWARD
 
 #define CONTROL_STACK_HARD_GUARD_PAGE(th) \
     (CONTROL_STACK_HARD_GUARD_PAGE(th) + os_vm_page_size)
 #define CONTROL_STACK_RETURN_GUARD_PAGE(th) \
     (CONTROL_STACK_GUARD_PAGE(th) + os_vm_page_size)
-
-#define ALIEN_STACK_HARD_GUARD_PAGE(th) \
-    ((os_vm_address_t)(th->alien_stack_start))
-#define ALIEN_STACK_GUARD_PAGE(th) \
-    (ALIEN_STACK_HARD_GUARD_PAGE(th) + os_vm_page_size)
-#define ALIEN_STACK_RETURN_GUARD_PAGE(th) \
-    (ALIEN_STACK_GUARD_PAGE(th) + os_vm_page_size)
-
 #else
 
 #define CONTROL_STACK_HARD_GUARD_PAGE(th) \
 #define CONTROL_STACK_RETURN_GUARD_PAGE(th) \
     (CONTROL_STACK_GUARD_PAGE(th) - os_vm_page_size)
 
+#endif
+
+#ifdef ALIEN_STACK_GROWS_DOWNWARD
+
+#define ALIEN_STACK_HARD_GUARD_PAGE(th)         \
+    ((os_vm_address_t)(th->alien_stack_start))
+#define ALIEN_STACK_GUARD_PAGE(th) \
+    (ALIEN_STACK_HARD_GUARD_PAGE(th) + os_vm_page_size)
+#define ALIEN_STACK_RETURN_GUARD_PAGE(th) \
+    (ALIEN_STACK_GUARD_PAGE(th) + os_vm_page_size)
+
+#elif ALIEN_STACK_GROWS_UPWARD
+
 #define ALIEN_STACK_HARD_GUARD_PAGE(th)                            \
     (((os_vm_address_t)th->alien_stack_start) + ALIEN_STACK_SIZE - \
      os_vm_page_size)
@@ -71,6 +77,8 @@
 #define ALIEN_STACK_RETURN_GUARD_PAGE(th) \
     (ALIEN_STACK_GUARD_PAGE(th) - os_vm_page_size)
 
+#else
+#error ALIEN_STACK_GROWS_DOWNWARD or ALIEN_STACK_GROWS_UPWARD has to be defined
 #endif
 
 #define BINDING_STACK_HARD_GUARD_PAGE(th)                              \
index fe764f6..b25b9d8 100644 (file)
@@ -12,6 +12,7 @@
 #include "interr.h"                     /* for declaration of lose() */
 
 #define ARCH_HAS_STACK_POINTER
+#define ALIEN_STACK_GROWS_DOWNWARD
 
 /* FIXME: Do we also want
  *   #define ARCH_HAS_FLOAT_REGISTERS
index 582bfc7..ad87f45 100644 (file)
@@ -12,6 +12,7 @@
 #include "interr.h"                     /* for declaration of lose() */
 
 #define ARCH_HAS_STACK_POINTER
+#define ALIEN_STACK_GROWS_DOWNWARD
 
 /* FIXME: Do we also want
  *   #define ARCH_HAS_FLOAT_REGISTERS
diff --git a/tests/alloca.c b/tests/alloca.c
new file mode 100644 (file)
index 0000000..1e6ab13
--- /dev/null
@@ -0,0 +1,9 @@
+#include <alloca.h>
+#include <string.h>
+
+#define SIZE 128*1024 // twice the largest page size
+void alloca_test()
+{
+  void* foo = alloca(SIZE);
+  memset(foo, 0xff, SIZE);
+}
index c59baae..8a09dc9 100644 (file)
@@ -25,7 +25,7 @@ while [ $# -gt 0 ]; do
                 FreeBSD-X86-64)  new=-fPIC ;;
                 Linux-MIPS)      new=-fPIC ;;
                 Linux-X86-64)    new=-fPIC ;;
-               Linux-PowerPC)   new=-fPIC ;;
+                Linux-PowerPC)   new=-fPIC ;;
                 NetBSD-PowerPC)  new=-fPIC ;;
                 NetBSD-X86-64)   new=-fPIC ;;
                 OpenBSD-PowerPC) new=-fPIC ;;
index 7ebc17c..c5ff8f2 100644 (file)
                (sb-thread:join-thread reader)))))
       (writer))
     (assert (eq result :ok))))
+
+(with-test (:name :thread-alloca)
+  (sb-ext:run-program "/bin/sh"
+                      '("run-compiler.sh" "-sbcl-pic" "-sbcl-shared"
+                        "alloca.c" "-o" "alloca.so")
+                      :environment (test-util::test-env))
+
+  (load-shared-object (truename "alloca.so"))
+
+  (alien-funcall (extern-alien "alloca_test" (function void)))
+  (sb-thread:join-thread
+   (sb-thread:make-thread
+    (lambda ()
+      (alien-funcall (extern-alien "alloca_test" (function void)))))))