From 1d9fe1f6d7d64aa05993d414c1cd683a2ce231cc Mon Sep 17 00:00:00 2001 From: Stas Boukarev Date: Thu, 15 Aug 2013 21:52:24 +0400 Subject: [PATCH] Set up alien stack correctly on non-x86oids. 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. --- NEWS | 1 + src/runtime/alpha-arch.h | 1 + src/runtime/hppa-arch.h | 1 + src/runtime/mips-arch.h | 1 + src/runtime/ppc-arch.h | 1 + src/runtime/sparc-arch.h | 1 + src/runtime/thread.c | 2 +- src/runtime/validate.h | 24 ++++++++++++++++-------- src/runtime/x86-64-arch.h | 1 + src/runtime/x86-arch.h | 1 + tests/alloca.c | 9 +++++++++ tests/run-compiler.sh | 2 +- tests/threads.impure.lisp | 14 ++++++++++++++ 13 files changed, 49 insertions(+), 10 deletions(-) create mode 100644 tests/alloca.c diff --git a/NEWS b/NEWS index 7327aaa..32b6bd2 100644 --- 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. diff --git a/src/runtime/alpha-arch.h b/src/runtime/alpha-arch.h index c3d82f7..872846e 100644 --- a/src/runtime/alpha-arch.h +++ b/src/runtime/alpha-arch.h @@ -15,5 +15,6 @@ release_spinlock(lispobj *word) } #define ARCH_HAS_FLOAT_REGISTERS +#define ALIEN_STACK_GROWS_DOWNWARD #endif /* _ALPHA_ARCH_H */ diff --git a/src/runtime/hppa-arch.h b/src/runtime/hppa-arch.h index 58cecc2..65d7295 100644 --- a/src/runtime/hppa-arch.h +++ b/src/runtime/hppa-arch.h @@ -15,5 +15,6 @@ release_spinlock(lispobj *word) } #define ARCH_HAS_NPC_REGISTER +#define ALIEN_STACK_GROWS_UPWARD #endif /* _HPPA_ARCH_H */ diff --git a/src/runtime/mips-arch.h b/src/runtime/mips-arch.h index 4ac1b78..cedd280 100644 --- a/src/runtime/mips-arch.h +++ b/src/runtime/mips-arch.h @@ -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) diff --git a/src/runtime/ppc-arch.h b/src/runtime/ppc-arch.h index 515335f..a5fe1e9 100644 --- a/src/runtime/ppc-arch.h +++ b/src/runtime/ppc-arch.h @@ -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); diff --git a/src/runtime/sparc-arch.h b/src/runtime/sparc-arch.h index 6a7b52e..d49190c 100644 --- a/src/runtime/sparc-arch.h +++ b/src/runtime/sparc-arch.h @@ -14,5 +14,6 @@ release_spinlock(lispobj *word) } #define ARCH_HAS_NPC_REGISTER +#define ALIEN_STACK_GROWS_DOWNWARD #endif /* _SPARC_ARCH_H */ diff --git a/src/runtime/thread.c b/src/runtime/thread.c index 63527ba..1889213 100644 --- a/src/runtime/thread.c +++ b/src/runtime/thread.c @@ -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 diff --git a/src/runtime/validate.h b/src/runtime/validate.h index 7dfb73d..c445e7e 100644 --- a/src/runtime/validate.h +++ b/src/runtime/validate.h @@ -38,6 +38,7 @@ #if !defined(LANGUAGE_ASSEMBLY) #include + #ifdef LISP_FEATURE_STACK_GROWS_DOWNWARD_NOT_UPWARD #define CONTROL_STACK_HARD_GUARD_PAGE(th) \ @@ -46,14 +47,6 @@ (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) \ @@ -63,6 +56,19 @@ #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) \ diff --git a/src/runtime/x86-64-arch.h b/src/runtime/x86-64-arch.h index fe764f6..b25b9d8 100644 --- a/src/runtime/x86-64-arch.h +++ b/src/runtime/x86-64-arch.h @@ -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/src/runtime/x86-arch.h b/src/runtime/x86-arch.h index 582bfc7..ad87f45 100644 --- a/src/runtime/x86-arch.h +++ b/src/runtime/x86-arch.h @@ -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 index 0000000..1e6ab13 --- /dev/null +++ b/tests/alloca.c @@ -0,0 +1,9 @@ +#include +#include + +#define SIZE 128*1024 // twice the largest page size +void alloca_test() +{ + void* foo = alloca(SIZE); + memset(foo, 0xff, SIZE); +} diff --git a/tests/run-compiler.sh b/tests/run-compiler.sh index c59baae..8a09dc9 100644 --- a/tests/run-compiler.sh +++ b/tests/run-compiler.sh @@ -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 ;; diff --git a/tests/threads.impure.lisp b/tests/threads.impure.lisp index 7ebc17c..c5ff8f2 100644 --- a/tests/threads.impure.lisp +++ b/tests/threads.impure.lisp @@ -1553,3 +1553,17 @@ (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))))))) -- 1.7.10.4