Index: src/code/debug-int.lisp
===================================================================
RCS file: /cvsroot/sbcl/sbcl/src/code/debug-int.lisp,v
retrieving revision 1.101
diff -u -r1.101 debug-int.lisp
--- src/code/debug-int.lisp	28 Sep 2005 13:42:24 -0000	1.101
+++ src/code/debug-int.lisp	27 Feb 2006 10:52:45 -0000
@@ -559,6 +559,9 @@
 
 (defconstant sb!vm::nargs-offset #.sb!vm::ecx-offset)
 
+(sb!alien:define-alien-variable "user_space_start" sb!alien:unsigned-long)
+(sb!alien:define-alien-variable "user_space_end" sb!alien:unsigned-long)
+
 ;;; Check for a valid return address - it could be any valid C/Lisp
 ;;; address.
 ;;;
@@ -567,11 +570,7 @@
 (defun ra-pointer-valid-p (ra)
   (declare (type system-area-pointer ra))
   (and
-   ;; not the first page (which is unmapped)
-   ;;
-   ;; FIXME: Where is this documented? Is it really true of every CPU
-   ;; architecture? Is it even necessarily true in current SBCL?
-   (>= (sap-int ra) 4096)
+   (>= user-space-end (sap-int ra) user-space-start)
    ;; not a Lisp stack pointer
    (not (control-stack-pointer-valid-p ra))))
 
Index: src/runtime/bsd-os.c
===================================================================
RCS file: /cvsroot/sbcl/sbcl/src/runtime/bsd-os.c,v
retrieving revision 1.36
diff -u -r1.36 bsd-os.c
--- src/runtime/bsd-os.c	21 Feb 2006 23:40:36 -0000	1.36
+++ src/runtime/bsd-os.c	27 Feb 2006 10:52:45 -0000
@@ -53,6 +53,8 @@
 
 #ifdef __FreeBSD__
 #include <sys/sysctl.h>
+#include <link.h>
+#include <dlfcn.h>
 #include <osreldate.h>
 
 static void freebsd_init();
@@ -310,6 +312,25 @@
         fast_bzero_pointer = fast_bzero_detect;
     }
 #endif /* LISP_FEATURE_X86 */
+    /* extract lowermost address from linkmap */
+    {
+        Link_map *map;
+
+        if (dlinfo(RTLD_SELF, RTLD_DI_LINKMAP, &map) == 0
+            && map != NULL && map->l_addr != NULL)
+            user_space_start = (unsigned long)map->l_addr;
+    }
+
+    /* extract uppermost address from "kern.usrstack" mib */
+    {
+        size_t len;
+        unsigned long tmp;
+
+        len = sizeof(tmp);
+        if (sysctlbyname("kern.usrstack", &tmp, &len, NULL, 0) == 0
+            && tmp > user_space_start)
+            user_space_end = tmp;
+    }
 }
 #endif /* __FreeBSD__ */
 
Index: src/runtime/globals.c
===================================================================
RCS file: /cvsroot/sbcl/sbcl/src/runtime/globals.c,v
retrieving revision 1.19
diff -u -r1.19 globals.c
--- src/runtime/globals.c	21 Feb 2006 22:59:33 -0000	1.19
+++ src/runtime/globals.c	27 Feb 2006 10:52:45 -0000
@@ -16,6 +16,7 @@
 #include <stdio.h>
 #include <sys/types.h>
 #include <unistd.h>
+#include <limits.h>
 
 #include "sbcl.h"
 #include "runtime.h"
@@ -54,6 +55,14 @@
 pthread_key_t specials=0;
 #endif
 
+/* Valid user address space.  */
+
+/* not the first page (which is unmapped)
+ * good default for most architectures.  */
+unsigned long user_space_start = 4096;
+
+unsigned long user_space_end = ULONG_MAX;
+
 void globals_init(void)
 {
     /* Space, stack, and free pointer vars are initialized by
Index: src/runtime/globals.h
===================================================================
RCS file: /cvsroot/sbcl/sbcl/src/runtime/globals.h,v
retrieving revision 1.26
diff -u -r1.26 globals.h
--- src/runtime/globals.h	21 Feb 2006 22:59:33 -0000	1.26
+++ src/runtime/globals.h	27 Feb 2006 10:52:46 -0000
@@ -108,4 +108,7 @@
 
 #endif /* LANGUAGE_ASSEMBLY */
 
+extern unsigned long user_space_start;
+extern unsigned long user_space_end;
+
 #endif /* _INCLUDED_GLOBALS_H_ */
