]> git.donarmstrong.com Git - lilypond.git/blob - guile18/libguile/gc_os_dep.c
Import guile-1.8 as multiple upstream tarball component
[lilypond.git] / guile18 / libguile / gc_os_dep.c
1 /*
2  * Copyright 1988, 1989, 2010 Hans-J. Boehm, Alan J. Demers
3  * Copyright (c) 1991-1995 by Xerox Corporation.  All rights reserved.
4  * Copyright (c) 1996-1999 by Silicon Graphics.  All rights reserved.
5  * Copyright (c) 1999 by Hewlett-Packard Company.  All rights reserved.
6  * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2006, 2008 Free Software Foundation
7  *
8  * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
9  * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
10  *
11  * Permission is hereby granted to use or copy this program
12  * for any purpose,  provided the above notices are retained on all copies.
13  * Permission to modify the code and to distribute modified code is granted,
14  * provided the above notices are retained, and a notice that the code was
15  * modified is included with the above copyright notice.
16  *
17  */
18
19 /*
20  * Copied from gc5.2, files "os_dep.c", "gc_priv.h", "mark.c" and "gcconfig.h",
21  * and modified for Guile by Marius Vollmer.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #include <ctype.h>
29 #include "libguile/gc.h"
30 #include "libguile/scmconfig.h"
31
32 #ifdef HAVE_LIBC_STACK_END
33
34 extern void *__libc_stack_end;
35
36 void *
37 scm_get_stack_base ()
38 {
39   return __libc_stack_end;
40 }
41
42 #else
43
44 #define ABORT(msg) abort ()
45
46 typedef char * ptr_t;   /* A generic pointer to which we can add        */
47                         /* byte displacements.                          */
48                         /* Preferably identical to caddr_t, if it       */
49                         /* exists.                                      */
50
51 /* Define word and signed_word to be unsigned and signed types of the   */
52 /* size as char * or void *.  There seems to be no way to do this       */
53 /* even semi-portably.  The following is probably no better/worse       */
54 /* than almost anything else.                                           */
55 /* The ANSI standard suggests that size_t and ptr_diff_t might be       */
56 /* better choices.  But those appear to have incorrect definitions      */
57 /* on may systems.  Notably "typedef int size_t" seems to be both       */
58 /* frequent and WRONG.                                                  */
59 typedef unsigned long GC_word;
60 typedef long GC_signed_word;
61
62 typedef GC_word word;
63 typedef GC_signed_word signed_word;
64
65 typedef int GC_bool;
66 # define TRUE 1
67 # define FALSE 0
68
69 #if defined(__STDC__)
70 #   include <stdlib.h>
71 #   if !(defined( sony_news ) )
72 #       include <stddef.h>
73 #   endif
74 #   define VOLATILE volatile
75 #else
76 #   ifdef MSWIN32
77 #       include <stdlib.h>
78 #   endif
79 #   define VOLATILE
80 #endif
81
82 /* Machine dependent parameters.  Some tuning parameters can be found   */
83 /* near the top of gc_private.h.                                        */
84
85 /* Machine specific parts contributed by various people.  See README file. */
86
87 /* First a unified test for Linux: */
88 # if defined(linux) || defined(__linux__)
89 #    define LINUX
90 # endif
91
92 /* Determine the machine type: */
93 # if defined(sun) && defined(mc68000)
94 #    define M68K
95 #    define SUNOS4
96 #    define mach_type_known
97 # endif
98 # if defined(hp9000s300)
99 #    define M68K
100 #    define HP
101 #    define mach_type_known
102 # endif
103 # if defined(__OpenBSD__) && defined(m68k)
104 #    define M68K
105 #    define OPENBSD
106 #    define mach_type_known
107 # endif
108 # if defined(__OpenBSD__) && defined(__sparc__)
109 #    define SPARC
110 #    define OPENBSD
111 #    define mach_type_known
112 # endif
113 # if defined(__NetBSD__) && defined(__alpha__)
114 #    define ALPHA
115 #    define NETBSD
116 #    define mach_type_known
117 # endif
118 # if defined(__NetBSD__) && defined(__powerpc__)
119 #    define POWERPC
120 #    define NETBSD
121 #    define mach_type_known
122 # endif
123 /* in netbsd 2.0 only __m68k__ is defined, not m68k */
124 # if defined(__NetBSD__) && (defined(m68k) || defined(__m68k__))
125 #    define M68K
126 #    define NETBSD
127 #    define mach_type_known
128 # endif
129 /* in netbsd 2.0 only __arm__ is defined, not arm32 */
130 # if defined(__NetBSD__) && (defined(arm32) || defined(__arm__))
131 #    define ARM32
132 #    define NETBSD
133 #    define mach_type_known
134 # endif
135 # if defined(__NetBSD__) && defined(__sparc__)
136 #    define SPARC
137 #    define NETBSD
138 #    define mach_type_known
139 # endif
140 # if defined(vax)
141 #    define VAX
142 #    ifdef ultrix
143 #       define ULTRIX
144 #    else
145 #       define BSD
146 #    endif
147 #    define mach_type_known
148 # endif
149 # if defined(mips) || defined(__mips)
150 #    define MIPS
151 #    if !defined(LINUX)
152 #      if defined(ultrix) || defined(__ultrix) || defined(__NetBSD__)
153 #        define ULTRIX
154 #      else
155 #        if defined(_SYSTYPE_SVR4) || defined(SYSTYPE_SVR4) \
156             || defined(__SYSTYPE_SVR4__)
157 #          define IRIX5   /* or IRIX 6.X */
158 #        else
159 #          define RISCOS  /* or IRIX 4.X */
160 #        endif
161 #      endif
162 #    endif /* !LINUX */
163 #    define mach_type_known
164 # endif
165 # if defined(sequent) && defined(i386)
166 #    define I386
167 #    define SEQUENT
168 #    define mach_type_known
169 # endif
170 # if defined(sun) && defined(i386)
171 #    define I386
172 #    define SUNOS5
173 #    define mach_type_known
174 # endif
175 # if (defined(__OS2__) || defined(__EMX__)) && defined(__32BIT__)
176 #    define I386
177 #    define OS2
178 #    define mach_type_known
179 # endif
180 # if defined(ibm032)
181 #   define RT
182 #   define mach_type_known
183 # endif
184 # if defined(sun) && (defined(sparc) || defined(__sparc))
185 #   define SPARC
186     /* Test for SunOS 5.x */
187 #     include <errno.h>
188 #     ifdef ECHRNG
189 #       define SUNOS5
190 #     else
191 #       define SUNOS4
192 #     endif
193 #   define mach_type_known
194 # endif
195 # if defined(sparc) && defined(unix) && !defined(sun) && !defined(linux) \
196      && !defined(__OpenBSD__)
197 #   define SPARC
198 #   define DRSNX
199 #   define mach_type_known
200 # endif
201 # if defined(_IBMR2)
202 #   define RS6000
203 #   define mach_type_known
204 # endif
205 # if defined(_M_XENIX) && defined(_M_SYSV) && defined(_M_I386)
206         /* The above test may need refinement   */
207 #   define I386
208 #   if defined(_SCO_ELF)
209 #     define SCO_ELF
210 #   else
211 #     define SCO
212 #   endif
213 #   define mach_type_known
214 # endif
215 # if defined(_AUX_SOURCE)
216 #   define M68K
217 #   define SYSV
218 #   define mach_type_known
219 # endif
220 # if defined(_PA_RISC1_0) || defined(_PA_RISC1_1) || defined(_PA_RISC2_0) \
221      || defined(hppa) || defined(__hppa__)
222 #   define HP_PA
223 #   ifndef LINUX
224 #     define HPUX
225 #   endif
226 #   define mach_type_known
227 # endif
228 # if defined(LINUX) && (defined(i386) || defined(__i386__))
229 #    define I386
230 #    define mach_type_known
231 # endif
232 # if defined(LINUX) && (defined(__ia64__) || defined(__ia64))
233 #    define IA64
234 #    define mach_type_known
235 # endif
236 # if defined(LINUX) && defined(powerpc)
237 #    define POWERPC
238 #    define mach_type_known
239 # endif
240 # if defined(LINUX) && defined(__mc68000__)
241 #    define M68K
242 #    define mach_type_known
243 # endif
244 # if defined(LINUX) && (defined(sparc) || defined(__sparc__))
245 #    define SPARC
246 #    define mach_type_known
247 # endif
248 # if defined(LINUX) && (defined(arm) || defined (__arm__))
249 #    define ARM32
250 #    define mach_type_known
251 # endif
252 # if defined(__alpha) || defined(__alpha__)
253 #   define ALPHA
254 #   if !defined(LINUX) && !defined (NETBSD)
255 #     define OSF1       /* a.k.a Digital Unix */
256 #   endif
257 #   define mach_type_known
258 # endif
259 # if defined(_AMIGA) && !defined(AMIGA)
260 #   define AMIGA
261 # endif
262 # ifdef AMIGA
263 #   define M68K
264 #   define mach_type_known
265 # endif
266 # if defined(THINK_C) || defined(__MWERKS__) && !defined(__powerc)
267 #   define M68K
268 #   define MACOS
269 #   define mach_type_known
270 # endif
271 # if defined(__MWERKS__) && defined(__powerc)
272 #   define POWERPC
273 #   define MACOS
274 #   define mach_type_known
275 # endif
276 # if defined(macosx) || \
277      (defined(__APPLE__) && defined(__MACH__) && defined(__ppc__))
278 #    define MACOSX
279 #    define POWERPC
280 #    define mach_type_known
281 # endif
282 # if defined(NeXT) && defined(mc68000)
283 #   define M68K
284 #   define NEXT
285 #   define mach_type_known
286 # endif
287 # if defined(NeXT) && defined(i386)
288 #   define I386
289 #   define NEXT
290 #   define mach_type_known
291 # endif
292 # if defined(__OpenBSD__) && (defined(i386) || defined(__i386__))
293 #   define I386
294 #   define OPENBSD
295 #   define mach_type_known
296 # endif
297 # if defined(__FreeBSD__) && defined(i386)
298 #   define I386
299 #   define FREEBSD
300 #   define mach_type_known
301 # endif
302 # if defined(__NetBSD__) && defined(i386)
303 #   define I386
304 #   define NETBSD
305 #   define mach_type_known
306 # endif
307 # if defined(bsdi) && defined(i386)
308 #    define I386
309 #    define BSDI
310 #    define mach_type_known
311 # endif
312 # if !defined(mach_type_known) && defined(__386BSD__)
313 #   define I386
314 #   define THREE86BSD
315 #   define mach_type_known
316 # endif
317 # if defined(_CX_UX) && defined(_M88K)
318 #   define M88K
319 #   define CX_UX
320 #   define mach_type_known
321 # endif
322 # if defined(DGUX)
323 #   define M88K
324     /* DGUX defined */
325 #   define mach_type_known
326 # endif
327 # if (defined(_MSDOS) || defined(_MSC_VER)) && (_M_IX86 >= 300) \
328      || defined(_WIN32) && !defined(__CYGWIN32__) && !defined(__CYGWIN__)
329 #   define I386
330 #   define MSWIN32      /* or Win32s */
331 #   define mach_type_known
332 # endif
333 # if defined(__DJGPP__)
334 #   define I386
335 #   ifndef DJGPP
336 #     define DJGPP  /* MSDOS running the DJGPP port of GCC */
337 #   endif
338 #   define mach_type_known
339 # endif
340 # if defined(__CYGWIN32__) || defined(__CYGWIN__)
341 #   define I386
342 #   define CYGWIN32
343 #   define mach_type_known
344 # endif
345 # if defined(__MINGW32__)
346 #   define I386
347 #   define MSWIN32
348 #   define mach_type_known
349 # endif
350 # if defined(__BORLANDC__)
351 #   define I386
352 #   define MSWIN32
353 #   define mach_type_known
354 # endif
355 # if defined(_UTS) && !defined(mach_type_known)
356 #   define S370
357 #   define UTS4
358 #   define mach_type_known
359 # endif
360 # if defined(__pj__)
361 #   define PJ
362 #   define mach_type_known
363 # endif
364 /* Ivan Demakov */
365 # if defined(__WATCOMC__) && defined(__386__)
366 #   define I386
367 #   if !defined(OS2) && !defined(MSWIN32) && !defined(DOS4GW)
368 #     if defined(__OS2__)
369 #       define OS2
370 #     else
371 #       if defined(__WINDOWS_386__) || defined(__NT__)
372 #         define MSWIN32
373 #       else
374 #         define DOS4GW
375 #       endif
376 #     endif
377 #   endif
378 #   define mach_type_known
379 # endif
380 # if defined(__s390__) && defined(LINUX)
381 #    define S370
382 #    define mach_type_known
383 # endif
384 # if defined(__GNU__)
385 #    define I386
386 #    define GNU
387 #    define mach_type_known
388 # endif
389 # if defined(__SCO_VERSION__)
390 #    define I386
391 #    define SYSV
392 #    define mach_type_known
393 # endif
394
395 /* Feel free to add more clauses here */
396
397 /* Or manually define the machine type here.  A machine type is         */
398 /* characterized by the architecture.  Some                             */
399 /* machine types are further subdivided by OS.                          */
400 /* the macros ULTRIX, RISCOS, and BSD to distinguish.                   */
401 /* Note that SGI IRIX is treated identically to RISCOS.                 */
402 /* SYSV on an M68K actually means A/UX.                                 */
403 /* The distinction in these cases is usually the stack starting address */
404 # ifndef mach_type_known
405
406 void *
407 scm_get_stack_base ()
408 {
409   ABORT ("Can't determine stack base");
410   return NULL;
411 }
412
413 # else
414                     /* Mapping is: M68K       ==> Motorola 680X0        */
415                     /*             (SUNOS4,HP,NEXT, and SYSV (A/UX),    */
416                     /*             MACOS and AMIGA variants)            */
417                     /*             I386       ==> Intel 386             */
418                     /*              (SEQUENT, OS2, SCO, LINUX, NETBSD,  */
419                     /*               FREEBSD, THREE86BSD, MSWIN32,      */
420                     /*               BSDI,SUNOS5, NEXT, other variants) */
421                     /*             NS32K      ==> Encore Multimax       */
422                     /*             MIPS       ==> R2000 or R3000        */
423                     /*                  (RISCOS, ULTRIX variants)       */
424                     /*             VAX        ==> DEC VAX               */
425                     /*                  (BSD, ULTRIX variants)          */
426                     /*             RS6000     ==> IBM RS/6000 AIX3.X    */
427                     /*             RT         ==> IBM PC/RT             */
428                     /*             HP_PA      ==> HP9000/700 & /800     */
429                     /*                            HP/UX                 */
430                     /*             SPARC      ==> SPARC under SunOS     */
431                     /*                  (SUNOS4, SUNOS5,                */
432                     /*                   DRSNX variants)                */
433                     /*             ALPHA      ==> DEC Alpha             */
434                     /*                  (OSF1 and LINUX variants)       */
435                     /*             M88K       ==> Motorola 88XX0        */
436                     /*                  (CX_UX and DGUX)                */
437                     /*             S370       ==> 370-like machine      */
438                     /*                  running Amdahl UTS4             */
439                     /*             ARM32      ==> Intel StrongARM       */
440                     /*             IA64       ==> Intel IA64            */
441                     /*                            (e.g. Itanium)        */
442
443
444 /*
445  * For each architecture and OS, the following need to be defined:
446  *
447  * CPP_WORD_SZ is a simple integer constant representing the word size.
448  * in bits.  We assume byte addressibility, where a byte has 8 bits.
449  * We also assume CPP_WORD_SZ is either 32 or 64.
450  * (We care about the length of pointers, not hardware
451  * bus widths.  Thus a 64 bit processor with a C compiler that uses
452  * 32 bit pointers should use CPP_WORD_SZ of 32, not 64. Default is 32.)
453  *
454  * MACH_TYPE is a string representation of the machine type.
455  * OS_TYPE is analogous for the OS.
456  *
457  * ALIGNMENT is the largest N, such that
458  * all pointer are guaranteed to be aligned on N byte boundaries.
459  * defining it to be 1 will always work, but perform poorly.
460  *
461  * DATASTART is the beginning of the data segment.
462  * On UNIX systems, the collector will scan the area between DATASTART
463  * and DATAEND for root pointers.
464  *
465  * DATAEND, if not &end.
466  *
467  * ALIGN_DOUBLE of GC_malloc should return blocks aligned to twice
468  * the pointer size.
469  *
470  * STACKBOTTOM is the cool end of the stack, which is usually the
471  * highest address in the stack.
472  * Under PCR or OS/2, we have other ways of finding thread stacks.
473  * For each machine, the following should:
474  * 1) define SCM_STACK_GROWS_UP if the stack grows toward higher addresses, and
475  * 2) define exactly one of
476  *      STACKBOTTOM (should be defined to be an expression)
477  *      HEURISTIC1
478  *      HEURISTIC2
479  * If either of the last two macros are defined, then STACKBOTTOM is computed
480  * during collector startup using one of the following two heuristics:
481  * HEURISTIC1:  Take an address inside GC_init's frame, and round it up to
482  *              the next multiple of STACK_GRAN.
483  * HEURISTIC2:  Take an address inside GC_init's frame, increment it repeatedly
484  *              in small steps (decrement if SCM_STACK_GROWS_UP), and read the value
485  *              at each location.  Remember the value when the first
486  *              Segmentation violation or Bus error is signalled.  Round that
487  *              to the nearest plausible page boundary, and use that instead
488  *              of STACKBOTTOM.
489  *
490  * Gustavo Rodriguez-Rivera points out that on most (all?) Unix machines,
491  * the value of environ is a pointer that can serve as STACKBOTTOM.
492  * I expect that HEURISTIC2 can be replaced by this approach, which
493  * interferes far less with debugging.
494  *
495  * If no expression for STACKBOTTOM can be found, and neither of the above
496  * heuristics are usable, the collector can still be used with all of the above
497  * undefined, provided one of the following is done:
498  * 1) GC_mark_roots can be changed to somehow mark from the correct stack(s)
499  *    without reference to STACKBOTTOM.  This is appropriate for use in
500  *    conjunction with thread packages, since there will be multiple stacks.
501  *    (Allocating thread stacks in the heap, and treating them as ordinary
502  *    heap data objects is also possible as a last resort.  However, this is
503  *    likely to introduce significant amounts of excess storage retention
504  *    unless the dead parts of the thread stacks are periodically cleared.)
505  * 2) Client code may set GC_stackbottom before calling any GC_ routines.
506  *    If the author of the client code controls the main program, this is
507  *    easily accomplished by introducing a new main program, setting
508  *    GC_stackbottom to the address of a local variable, and then calling
509  *    the original main program.  The new main program would read something
510  *    like:
511  *
512  *              # include "gc_private.h"
513  *
514  *              main(argc, argv, envp)
515  *              int argc;
516  *              char **argv, **envp;
517  *              {
518  *                  int dummy;
519  *
520  *                  GC_stackbottom = (ptr_t)(&dummy);
521  *                  return(real_main(argc, argv, envp));
522  *              }
523  *
524  *
525  * Each architecture may also define the style of virtual dirty bit
526  * implementation to be used:
527  *   MPROTECT_VDB: Write protect the heap and catch faults.
528  *   PROC_VDB: Use the SVR4 /proc primitives to read dirty bits.
529  *
530  * An architecture may define DYNAMIC_LOADING if dynamic_load.c
531  * defined GC_register_dynamic_libraries() for the architecture.
532  *
533  * An architecture may define PREFETCH(x) to preload the cache with *x.
534  * This defaults to a no-op.
535  *
536  * PREFETCH_FOR_WRITE(x) is used if *x is about to be written.
537  *
538  * An architecture may also define CLEAR_DOUBLE(x) to be a fast way to
539  * clear the two words at GC_malloc-aligned address x.  By default,
540  * word stores of 0 are used instead.
541  */
542
543
544 # define STACK_GRAN 0x1000000
545 # ifdef M68K
546 #   define MACH_TYPE "M68K"
547 #   define ALIGNMENT 2
548 #   ifdef OPENBSD
549 #       define OS_TYPE "OPENBSD"
550 #       define HEURISTIC2
551         extern char etext;
552 #       define DATASTART ((ptr_t)(&etext))
553 #   endif
554 #   ifdef NETBSD
555 #       define OS_TYPE "NETBSD"
556 #       define HEURISTIC2
557         extern char etext;
558 #       define DATASTART ((ptr_t)(&etext))
559 #   endif
560 #   ifdef LINUX
561 #       define OS_TYPE "LINUX"
562 #       define STACKBOTTOM ((ptr_t)0xf0000000)
563 #       define MPROTECT_VDB
564 #       ifdef __ELF__
565 #            define DYNAMIC_LOADING
566              extern char **__environ;
567 #            define DATASTART ((ptr_t)(&__environ))
568                              /* hideous kludge: __environ is the first */
569                              /* word in crt0.o, and delimits the start */
570                              /* of the data segment, no matter which   */
571                              /* ld options were passed through.        */
572                              /* We could use _etext instead, but that  */
573                              /* would include .rodata, which may       */
574                              /* contain large read-only data tables    */
575                              /* that we'd rather not scan.             */
576              extern int _end;
577 #            define DATAEND (&_end)
578 #       else
579              extern int etext;
580 #            define DATASTART ((ptr_t)((((word) (&etext)) + 0xfff) & ~0xfff))
581 #       endif
582 #   endif
583 #   ifdef SUNOS4
584 #       define OS_TYPE "SUNOS4"
585         extern char etext;
586 #       define DATASTART ((ptr_t)((((word) (&etext)) + 0x1ffff) & ~0x1ffff))
587 #       define HEURISTIC1       /* differs      */
588 #       define DYNAMIC_LOADING
589 #   endif
590 #   ifdef HP
591 #       define OS_TYPE "HP"
592         extern char etext;
593 #       define DATASTART ((ptr_t)((((word) (&etext)) + 0xfff) & ~0xfff))
594 #       define STACKBOTTOM ((ptr_t) 0xffeffffc)
595                               /* empirically determined.  seems to work. */
596 #       include <unistd.h>
597 #       define GETPAGESIZE() sysconf(_SC_PAGE_SIZE)
598 #   endif
599 #   ifdef SYSV
600 #       define OS_TYPE "SYSV"
601         extern etext;
602 #       define DATASTART ((ptr_t)((((word) (&etext)) + 0x3fffff) \
603                                    & ~0x3fffff) \
604                                   +((word)&etext & 0x1fff))
605         /* This only works for shared-text binaries with magic number 0413.
606            The other sorts of SysV binaries put the data at the end of the text,
607            in which case the default of &etext would work.  Unfortunately,
608            handling both would require having the magic-number available.
609                                 -- Parag
610            */
611 #       define STACKBOTTOM ((ptr_t)0xFFFFFFFE)
612                         /* The stack starts at the top of memory, but   */
613                         /* 0x0 cannot be used as setjump_test complains */
614                         /* that the stack direction is incorrect.  Two  */
615                         /* bytes down from 0x0 should be safe enough.   */
616                         /*              --Parag                         */
617 #       include <sys/mmu.h>
618 #       define GETPAGESIZE() PAGESIZE   /* Is this still right? */
619 #   endif
620 #   ifdef AMIGA
621 #       define OS_TYPE "AMIGA"
622                 /* STACKBOTTOM and DATASTART handled specially  */
623                 /* in os_dep.c                                  */
624 #       define DATAEND  /* not needed */
625 #       define GETPAGESIZE() 4096
626 #   endif
627 #   ifdef MACOS
628 #     ifndef __LOWMEM__
629 #     include <LowMem.h>
630 #     endif
631 #     define OS_TYPE "MACOS"
632                         /* see os_dep.c for details of global data segments. */
633 #     define STACKBOTTOM ((ptr_t) LMGetCurStackBase())
634 #     define DATAEND    /* not needed */
635 #     define GETPAGESIZE() 4096
636 #   endif
637 #   ifdef NEXT
638 #       define OS_TYPE "NEXT"
639 #       define DATASTART ((ptr_t) get_etext())
640 #       define STACKBOTTOM ((ptr_t) 0x4000000)
641 #       define DATAEND  /* not needed */
642 #   endif
643 # endif
644
645 # ifdef POWERPC
646 #   define MACH_TYPE "POWERPC"
647 #   ifdef MACOS
648 #     define ALIGNMENT 2  /* Still necessary?  Could it be 4?   */
649 #     ifndef __LOWMEM__
650 #     include <LowMem.h>
651 #     endif
652 #     define OS_TYPE "MACOS"
653                         /* see os_dep.c for details of global data segments. */
654 #     define STACKBOTTOM ((ptr_t) LMGetCurStackBase())
655 #     define DATAEND  /* not needed */
656 #   endif
657 #   ifdef LINUX
658 #     define ALIGNMENT 4        /* Guess.  Can someone verify?  */
659                                 /* This was 2, but that didn't sound right. */
660 #     define OS_TYPE "LINUX"
661 #     define HEURISTIC1
662 #     define DYNAMIC_LOADING
663 #     undef STACK_GRAN
664 #     define STACK_GRAN 0x10000000
665         /* Stack usually starts at 0x80000000 */
666 #     define LINUX_DATA_START
667       extern int _end;
668 #     define DATAEND (&_end)
669 #   endif
670 #   ifdef MACOSX
671 #     define ALIGNMENT 4
672 #     define OS_TYPE "MACOSX"
673 #     define DATASTART ((ptr_t) get_etext())
674 #     define STACKBOTTOM ((ptr_t) 0xc0000000)
675 #     define DATAEND    /* not needed */
676 #   endif
677 # endif
678
679 # ifdef VAX
680 #   define MACH_TYPE "VAX"
681 #   define ALIGNMENT 4  /* Pointers are longword aligned by 4.2 C compiler */
682     extern char etext;
683 #   define DATASTART ((ptr_t)(&etext))
684 #   ifdef BSD
685 #       define OS_TYPE "BSD"
686 #       define HEURISTIC1
687                         /* HEURISTIC2 may be OK, but it's hard to test. */
688 #   endif
689 #   ifdef ULTRIX
690 #       define OS_TYPE "ULTRIX"
691 #       define STACKBOTTOM ((ptr_t) 0x7fffc800)
692 #   endif
693 # endif
694
695 # ifdef RT
696 #   define MACH_TYPE "RT"
697 #   define ALIGNMENT 4
698 #   define DATASTART ((ptr_t) 0x10000000)
699 #   define STACKBOTTOM ((ptr_t) 0x1fffd800)
700 # endif
701
702 # ifdef SPARC
703 #   define MACH_TYPE "SPARC"
704 #   define ALIGNMENT 4  /* Required by hardware */
705 #   define ALIGN_DOUBLE
706     extern int etext;
707 #   ifdef SUNOS5
708 #       define OS_TYPE "SUNOS5"
709 #       define OS_TYPE "SUNOS5"
710         extern int _etext[], _end[];
711         ptr_t GC_SysVGetDataStart(size_t, ptr_t);
712 #       define DATASTART GC_SysVGetDataStart(0x1000, (ptr_t)_etext)
713 #       define DATAEND (ptr_t)(_end)
714 /*      # define STACKBOTTOM ((ptr_t)(_start)) worked through 2.7,      */
715 /*      but reportedly breaks under 2.8.  It appears that the stack     */
716 /*      base is a property of the executable, so this should not break  */
717 /*      old executables.                                                */
718 /*      HEURISTIC2 probably works, but this appears to be preferable.   */
719 #       include <sys/vm.h>
720 #       define STACKBOTTOM ((ptr_t) USRSTACK)
721 #       ifndef USE_MMAP
722 #           define USE_MMAP
723 #       endif
724 #       ifdef USE_MMAP
725 #         define HEAP_START (ptr_t)0x40000000
726 #       else
727 #         define HEAP_START DATAEND
728 #       endif
729 #       define PROC_VDB
730 /*      HEURISTIC1 reportedly no longer works under 2.7.  Thus we       */
731 /*      switched to HEURISTIC2, eventhough it creates some debugging    */
732 /*      issues.                                                         */
733 #       define HEURISTIC2
734 #       include <unistd.h>
735 #       define GETPAGESIZE()  sysconf(_SC_PAGESIZE)
736                 /* getpagesize() appeared to be missing from at least one */
737                 /* Solaris 5.4 installation.  Weird.                      */
738 #       define DYNAMIC_LOADING
739 #   endif
740 #   ifdef SUNOS4
741 #       define OS_TYPE "SUNOS4"
742         /* [If you have a weak stomach, don't read this.]               */
743         /* We would like to use:                                        */
744 /* #       define DATASTART ((ptr_t)((((word) (&etext)) + 0x1fff) & ~0x1fff)) */
745         /* This fails occasionally, due to an ancient, but very         */
746         /* persistent ld bug.  &etext is set 32 bytes too high.         */
747         /* We instead read the text segment size from the a.out         */
748         /* header, which happens to be mapped into our address space    */
749         /* at the start of the text segment.  The detective work here   */
750         /* was done by Robert Ehrlich, Manuel Serrano, and Bernard      */
751         /* Serpette of INRIA.                                           */
752         /* This assumes ZMAGIC, i.e. demand-loadable executables.       */
753 #       define TEXTSTART 0x2000
754 #       define DATASTART ((ptr_t)(*(int *)(TEXTSTART+0x4)+TEXTSTART))
755 #       define MPROTECT_VDB
756 #       define HEURISTIC1
757 #       define DYNAMIC_LOADING
758 #   endif
759 #   ifdef DRSNX
760 #       define CPP_WORDSZ 32
761 #       define OS_TYPE "DRSNX"
762         extern char * GC_SysVGetDataStart();
763         extern int etext;
764 #       define DATASTART (ptr_t)GC_SysVGetDataStart(0x10000, &etext)
765 #       define MPROTECT_VDB
766 #       define STACKBOTTOM ((ptr_t) 0xdfff0000)
767 #       define DYNAMIC_LOADING
768 #   endif
769 #   ifdef LINUX
770 #     define OS_TYPE "LINUX"
771 #     ifdef __ELF__
772 #       define LINUX_DATA_START
773 #       define DYNAMIC_LOADING
774 #     else
775           Linux Sparc non elf ?
776 #     endif
777       extern int _end;
778 #     define DATAEND (&_end)
779 #     define SVR4
780 #     define STACKBOTTOM ((ptr_t) 0xf0000000)
781 #   endif
782 #   ifdef OPENBSD
783 #     define OS_TYPE "OPENBSD"
784 #     define STACKBOTTOM ((ptr_t) 0xf8000000)
785 #     define DATASTART ((ptr_t)(&etext))
786 #   endif
787 # endif
788
789 # ifdef I386
790 #   define MACH_TYPE "I386"
791 #   define ALIGNMENT 4  /* Appears to hold for all "32 bit" compilers   */
792                         /* except Borland.  The -a4 option fixes        */
793                         /* Borland.                                     */
794                         /* Ivan Demakov: For Watcom the option is -zp4. */
795 #   ifndef SMALL_CONFIG
796 #     define ALIGN_DOUBLE /* Not strictly necessary, but may give speed   */
797                           /* improvement on Pentiums.                     */
798 #   endif
799 #   ifdef SEQUENT
800 #       define OS_TYPE "SEQUENT"
801         extern int etext;
802 #       define DATASTART ((ptr_t)((((word) (&etext)) + 0xfff) & ~0xfff))
803 #       define STACKBOTTOM ((ptr_t) 0x3ffff000)
804 #   endif
805 #   ifdef SUNOS5
806 #       define OS_TYPE "SUNOS5"
807         extern int _etext[], _end[];
808         ptr_t GC_SysVGetDataStart(size_t, ptr_t);
809 #       define DATASTART GC_SysVGetDataStart(0x1000, (ptr_t)_etext)
810 #       define DATAEND (ptr_t)(_end)
811 /*      # define STACKBOTTOM ((ptr_t)(_start)) worked through 2.7,      */
812 /*      but reportedly breaks under 2.8.  It appears that the stack     */
813 /*      base is a property of the executable, so this should not break  */
814 /*      old executables.                                                */
815 /*      HEURISTIC2 probably works, but this appears to be preferable.   */
816 #       include <sys/vm.h>
817 #       define STACKBOTTOM ((ptr_t) USRSTACK)
818 /** At least in Solaris 2.5, PROC_VDB gives wrong values for dirty bits. */
819 /*#     define PROC_VDB*/
820 #       define DYNAMIC_LOADING
821 #       ifndef USE_MMAP
822 #           define USE_MMAP
823 #       endif
824 #       ifdef USE_MMAP
825 #         define HEAP_START (ptr_t)0x40000000
826 #       else
827 #         define HEAP_START DATAEND
828 #       endif
829 #   endif
830 #   ifdef SCO
831 #       define OS_TYPE "SCO"
832         extern int etext;
833 #       define DATASTART ((ptr_t)((((word) (&etext)) + 0x3fffff) \
834                                   & ~0x3fffff) \
835                                  +((word)&etext & 0xfff))
836 #       define STACKBOTTOM ((ptr_t) 0x7ffffffc)
837 #   endif
838 #   ifdef SCO_ELF
839 #       define OS_TYPE "SCO_ELF"
840         extern int etext;
841 #       define DATASTART ((ptr_t)(&etext))
842 #       define STACKBOTTOM ((ptr_t) 0x08048000)
843 #       define DYNAMIC_LOADING
844 #       define ELF_CLASS ELFCLASS32
845 #   endif
846 #   ifdef LINUX
847 #       define OS_TYPE "LINUX"
848 #       define LINUX_STACKBOTTOM
849 #       if 0
850 #         define HEURISTIC1
851 #         undef STACK_GRAN
852 #         define STACK_GRAN 0x10000000
853           /* STACKBOTTOM is usually 0xc0000000, but this changes with   */
854           /* different kernel configurations.  In particular, systems   */
855           /* with 2GB physical memory will usually move the user        */
856           /* address space limit, and hence initial SP to 0x80000000.   */
857 #       endif
858 #       if !defined(LINUX_THREADS) || !defined(REDIRECT_MALLOC)
859 #           define MPROTECT_VDB
860 #       else
861             /* We seem to get random errors in incremental mode,        */
862             /* possibly because Linux threads is itself a malloc client */
863             /* and can't deal with the signals.                         */
864 #       endif
865 #       ifdef __ELF__
866 #            define DYNAMIC_LOADING
867 #            ifdef UNDEFINED    /* includes ro data */
868                extern int _etext;
869 #              define DATASTART ((ptr_t)((((word) (&_etext)) + 0xfff) & ~0xfff))
870 #            endif
871 #            include <features.h>
872 #            if defined(__GLIBC__) && __GLIBC__ >= 2
873 #                define LINUX_DATA_START
874 #            else
875                  extern char **__environ;
876 #                define DATASTART ((ptr_t)(&__environ))
877                               /* hideous kludge: __environ is the first */
878                               /* word in crt0.o, and delimits the start */
879                               /* of the data segment, no matter which   */
880                               /* ld options were passed through.        */
881                               /* We could use _etext instead, but that  */
882                               /* would include .rodata, which may       */
883                               /* contain large read-only data tables    */
884                               /* that we'd rather not scan.             */
885 #            endif
886              extern int _end;
887 #            define DATAEND (&_end)
888 #       else
889              extern int etext;
890 #            define DATASTART ((ptr_t)((((word) (&etext)) + 0xfff) & ~0xfff))
891 #       endif
892 #       ifdef USE_I686_PREFETCH
893 #         define PREFETCH(x) \
894             __asm__ __volatile__ ("     prefetchnta     %0": : "m"(*(char *)(x)))
895             /* Empirically prefetcht0 is much more effective at reducing        */
896             /* cache miss stalls for the targetted load instructions.  But it   */
897             /* seems to interfere enough with other cache traffic that the net  */
898             /* result is worse than prefetchnta.                                */
899 #         if 0
900             /* Using prefetches for write seems to have a slight negative       */
901             /* impact on performance, at least for a PIII/500.                  */
902 #           define PREFETCH_FOR_WRITE(x) \
903               __asm__ __volatile__ ("   prefetcht0      %0": : "m"(*(char *)(x)))
904 #         endif
905 #       endif
906 #       ifdef USE_3DNOW_PREFETCH
907 #         define PREFETCH(x) \
908             __asm__ __volatile__ ("     prefetch        %0": : "m"(*(char *)(x)))
909 #         define PREFETCH_FOR_WRITE(x)
910             __asm__ __volatile__ ("     prefetchw       %0": : "m"(*(char *)(x)))
911 #       endif
912 #   endif
913 #   ifdef CYGWIN32
914 #       define OS_TYPE "CYGWIN32"
915           extern int _data_start__;
916           extern int _data_end__;
917           extern int _bss_start__;
918           extern int _bss_end__;
919         /* For binutils 2.9.1, we have                  */
920         /*      DATASTART   = _data_start__             */
921         /*      DATAEND     = _bss_end__                */
922         /* whereas for some earlier versions it was     */
923         /*      DATASTART   = _bss_start__              */
924         /*      DATAEND     = _data_end__               */
925         /* To get it right for both, we take the        */
926         /* minumum/maximum of the two.                  */
927 #       define MAX(x,y) ((x) > (y) ? (x) : (y))
928 #       define MIN(x,y) ((x) < (y) ? (x) : (y))
929 #       define DATASTART ((ptr_t) MIN(&_data_start__, &_bss_start__))
930 #       define DATAEND   ((ptr_t) MAX(&_data_end__, &_bss_end__))
931 #       undef STACK_GRAN
932 #       define STACK_GRAN 0x10000
933 #       define HEURISTIC1
934 #   endif
935 #   ifdef OS2
936 #       define OS_TYPE "OS2"
937                 /* STACKBOTTOM and DATASTART are handled specially in   */
938                 /* os_dep.c. OS2 actually has the right                 */
939                 /* system call!                                         */
940 #       define DATAEND  /* not needed */
941 #   endif
942 #   ifdef MSWIN32
943 #       define OS_TYPE "MSWIN32"
944                 /* STACKBOTTOM and DATASTART are handled specially in   */
945                 /* os_dep.c.                                            */
946 #       ifndef __WATCOMC__
947 #         define MPROTECT_VDB
948 #       endif
949 #       define DATAEND  /* not needed */
950 #   endif
951 #   ifdef DJGPP
952 #       define OS_TYPE "DJGPP"
953 #       include "stubinfo.h"
954         extern int etext;
955         extern int _stklen;
956         extern int __djgpp_stack_limit;
957 #       define DATASTART ((ptr_t)((((word) (&etext)) + 0x1ff) & ~0x1ff))
958 /* #       define STACKBOTTOM ((ptr_t)((word) _stubinfo + _stubinfo->size \
959                                                      + _stklen)) */
960 #       define STACKBOTTOM ((ptr_t)((word) __djgpp_stack_limit + _stklen))
961                 /* This may not be right.  */
962 #   endif
963 #   ifdef OPENBSD
964 #       define OS_TYPE "OPENBSD"
965 #   endif
966 #   ifdef FREEBSD
967 #       define OS_TYPE "FREEBSD"
968 #       define MPROTECT_VDB
969 #   endif
970 #   ifdef NETBSD
971 #       define OS_TYPE "NETBSD"
972 #   endif
973 #   ifdef THREE86BSD
974 #       define OS_TYPE "THREE86BSD"
975 #   endif
976 #   ifdef BSDI
977 #       define OS_TYPE "BSDI"
978 #   endif
979 #   if defined(OPENBSD) || defined(FREEBSD) || defined(NETBSD) \
980         || defined(THREE86BSD) || defined(BSDI)
981 #       define HEURISTIC2
982         extern char etext;
983 #       define DATASTART ((ptr_t)(&etext))
984 #   endif
985 #   ifdef NEXT
986 #       define OS_TYPE "NEXT"
987 #       define DATASTART ((ptr_t) get_etext())
988 #       define STACKBOTTOM ((ptr_t)0xc0000000)
989 #       define DATAEND  /* not needed */
990 #   endif
991 #   ifdef DOS4GW
992 #     define OS_TYPE "DOS4GW"
993       extern long __nullarea;
994       extern char _end;
995       extern char *_STACKTOP;
996       /* Depending on calling conventions Watcom C either precedes
997          or does not precedes with undescore names of C-variables.
998          Make sure startup code variables always have the same names.  */
999       #pragma aux __nullarea "*";
1000       #pragma aux _end "*";
1001 #     define STACKBOTTOM ((ptr_t) _STACKTOP)
1002                          /* confused? me too. */
1003 #     define DATASTART ((ptr_t) &__nullarea)
1004 #     define DATAEND ((ptr_t) &_end)
1005 #   endif
1006 #   ifdef GNU
1007 #      define OS_TYPE "GNU"
1008 #    endif
1009 # endif
1010
1011 # ifdef NS32K
1012 #   define MACH_TYPE "NS32K"
1013 #   define ALIGNMENT 4
1014     extern char **environ;
1015 #   define DATASTART ((ptr_t)(&environ))
1016                               /* hideous kludge: environ is the first   */
1017                               /* word in crt0.o, and delimits the start */
1018                               /* of the data segment, no matter which   */
1019                               /* ld options were passed through.        */
1020 #   define STACKBOTTOM ((ptr_t) 0xfffff000) /* for Encore */
1021 # endif
1022
1023 # ifdef MIPS
1024 #   define MACH_TYPE "MIPS"
1025 #   ifdef LINUX
1026 #       define CPP_WORDSZ _MIPS_SZPTR
1027 #       define OS_TYPE "LINUX"
1028 #       define ALIGNMENT 4
1029 #       define ALIGN_DOUBLE
1030         extern int _fdata;
1031 #       define DATASTART ((ptr_t)(&_fdata))
1032         extern int _end;
1033 #       define DATAEND ((ptr_t)(&_end))
1034 #       define STACKBOTTOM ((ptr_t)0x7fff8000)
1035 #       define USE_GENERIC_PUSH_REGS 1
1036 #       define DYNAMIC_LOADING
1037 #   endif /* Linux */
1038 #   ifdef ULTRIX
1039 #       define HEURISTIC2
1040 #       define DATASTART (ptr_t)0x10000000
1041                               /* Could probably be slightly higher since */
1042                               /* startup code allocates lots of stuff.   */
1043 #       define OS_TYPE "ULTRIX"
1044 #       define ALIGNMENT 4
1045 #   endif
1046 #   ifdef RISCOS
1047 #       define HEURISTIC2
1048 #       define DATASTART (ptr_t)0x10000000
1049 #       define OS_TYPE "RISCOS"
1050 #       define ALIGNMENT 4  /* Required by hardware */
1051 #   endif
1052 #   ifdef IRIX5
1053 #       define HEURISTIC2
1054         extern int _fdata;
1055 #       define DATASTART ((ptr_t)(&_fdata))
1056 #       ifdef USE_MMAP
1057 #         define HEAP_START (ptr_t)0x30000000
1058 #       else
1059 #         define HEAP_START DATASTART
1060 #       endif
1061                               /* Lowest plausible heap address.         */
1062                               /* In the MMAP case, we map there.        */
1063                               /* In either case it is used to identify  */
1064                               /* heap sections so they're not           */
1065                               /* considered as roots.                   */
1066 #       define OS_TYPE "IRIX5"
1067 #       define MPROTECT_VDB
1068 #       ifdef _MIPS_SZPTR
1069 #         define CPP_WORDSZ _MIPS_SZPTR
1070 #         define ALIGNMENT (_MIPS_SZPTR/8)
1071 #         if CPP_WORDSZ != 64
1072 #           define ALIGN_DOUBLE
1073 #         endif
1074 #       else
1075 #         define ALIGNMENT 4
1076 #         define ALIGN_DOUBLE
1077 #       endif
1078 #       define DYNAMIC_LOADING
1079 #   endif
1080 # endif
1081
1082 # ifdef RS6000
1083 #   define MACH_TYPE "RS6000"
1084 #   define ALIGNMENT 4
1085 #   define DATASTART ((ptr_t)0x20000000)
1086     extern int errno;
1087 #   define STACKBOTTOM ((ptr_t)((ulong)&errno))
1088 #   define DYNAMIC_LOADING
1089         /* For really old versions of AIX, this may have to be removed. */
1090 # endif
1091
1092 # ifdef HP_PA
1093     /* OS is assumed to be HP/UX        */
1094 #   define MACH_TYPE "HP_PA"
1095 #   define OS_TYPE "HPUX"
1096 #   ifdef __LP64__
1097 #     define CPP_WORDSZ 64
1098 #     define ALIGNMENT 8
1099 #   else
1100 #     define CPP_WORDSZ 32
1101 #     define ALIGNMENT 4
1102 #     define ALIGN_DOUBLE
1103 #   endif
1104     extern int __data_start;
1105 #   define DATASTART ((ptr_t)(&__data_start))
1106 #   if 0
1107         /* The following appears to work for 7xx systems running HP/UX  */
1108         /* 9.xx Furthermore, it might result in much faster             */
1109         /* collections than HEURISTIC2, which may involve scanning      */
1110         /* segments that directly precede the stack.  It is not the     */
1111         /* default, since it may not work on older machine/OS           */
1112         /* combinations. (Thanks to Raymond X.T. Nijssen for uncovering */
1113         /* this.)                                                       */
1114 #       define STACKBOTTOM ((ptr_t) 0x7b033000)  /* from /etc/conf/h/param.h */
1115 #   else
1116         /* Gustavo Rodriguez-Rivera suggested changing HEURISTIC2       */
1117         /* to this.  We'll probably do this on other platforms, too.    */
1118         /* For now I'll use it where I can test it.                     */
1119         extern char ** environ;
1120 #       define STACKBOTTOM ((ptr_t)environ)
1121 #   endif
1122 #   ifndef SCM_STACK_GROWS_UP /* don't fight with scmconfig.h */
1123 #     define SCM_STACK_GROWS_UP 1
1124 #   endif
1125 #   define DYNAMIC_LOADING
1126 #   ifndef HPUX_THREADS
1127 #     define MPROTECT_VDB
1128 #   endif
1129 #   include <unistd.h>
1130 #   define GETPAGESIZE() sysconf(_SC_PAGE_SIZE)
1131 # endif
1132
1133 # ifdef ALPHA
1134 #   define MACH_TYPE "ALPHA"
1135 #   define ALIGNMENT 8
1136 #   define USE_GENERIC_PUSH_REGS
1137         /* Gcc and probably the DEC/Compaq compiler spill pointers to preserved */
1138         /* fp registers in some cases when the target is a 21264.  The assembly */
1139         /* code doesn't handle that yet, and version dependencies make that a   */
1140         /* bit tricky.  Do the easy thing for now.                              */
1141 #   ifdef OSF1
1142 #       define OS_TYPE "OSF1"
1143 #       define DATASTART ((ptr_t) 0x140000000)
1144         extern int _end;
1145 #       define DATAEND ((ptr_t) &_end)
1146 #       define HEURISTIC2
1147         /* Normally HEURISTIC2 is too conervative, since                */
1148         /* the text segment immediately follows the stack.              */
1149         /* Hence we give an upper pound.                                */
1150         extern int __start;
1151 #       define HEURISTIC2_LIMIT ((ptr_t)((word)(&__start) & ~(getpagesize()-1)))
1152 #       define CPP_WORDSZ 64
1153 #       define MPROTECT_VDB
1154 #       define DYNAMIC_LOADING
1155 #   endif
1156 #   ifdef LINUX
1157 #       define OS_TYPE "LINUX"
1158 #       define CPP_WORDSZ 64
1159 #       define STACKBOTTOM ((ptr_t) 0x120000000)
1160 #       ifdef __ELF__
1161 #         define LINUX_DATA_START
1162 #         define DYNAMIC_LOADING
1163           /* This doesn't work if the collector is in a dynamic library. */
1164 #       else
1165 #           define DATASTART ((ptr_t) 0x140000000)
1166 #       endif
1167         extern int _end;
1168 #       define DATAEND (&_end)
1169 #       define MPROTECT_VDB
1170                 /* Has only been superficially tested.  May not */
1171                 /* work on all versions.                        */
1172 #   endif
1173 # endif
1174
1175 # ifdef IA64
1176 #   define MACH_TYPE "IA64"
1177 #   define ALIGN_DOUBLE
1178         /* Requires 16 byte alignment for malloc */
1179 #   define ALIGNMENT 8
1180 #   define USE_GENERIC_PUSH_REGS
1181         /* We need to get preserved registers in addition to register windows.  */
1182         /* That's easiest to do with setjmp.                                    */
1183 #   ifdef HPUX
1184         --> needs work
1185 #   endif
1186 #   ifdef LINUX
1187 #       define OS_TYPE "LINUX"
1188 #       define CPP_WORDSZ 64
1189         /* This should really be done through /proc, but that   */
1190         /* requires we run on an IA64 kernel.                   */
1191 #       define STACKBOTTOM ((ptr_t) 0xa000000000000000l)
1192         /* We also need the base address of the register stack  */
1193         /* backing store.  There is probably a better way to    */
1194         /* get that, too ...                                    */
1195 #       define BACKING_STORE_BASE ((ptr_t) 0x9fffffff80000000l)
1196 #       if 1
1197 #           define SEARCH_FOR_DATA_START
1198 #           define DATASTART GC_data_start
1199 #       else
1200             extern int data_start;
1201 #           define DATASTART ((ptr_t)(&data_start))
1202 #       endif
1203 #       define DYNAMIC_LOADING
1204 #       define MPROTECT_VDB
1205                 /* Requires Linux 2.3.47 or later.      */
1206         extern int _end;
1207 #       define DATAEND (&_end)
1208 #       define PREFETCH(x) \
1209           __asm__ ("    lfetch  [%0]": : "r"((void *)(x)))
1210 #       define PREFETCH_FOR_WRITE(x) \
1211           __asm__ ("    lfetch.excl     [%0]": : "r"((void *)(x)))
1212 #       define CLEAR_DOUBLE(x) \
1213           __asm__ ("    stf.spill       [%0]=f0": : "r"((void *)(x)))
1214 #   endif
1215 # endif
1216
1217 # ifdef M88K
1218 #   define MACH_TYPE "M88K"
1219 #   define ALIGNMENT 4
1220 #   define ALIGN_DOUBLE
1221     extern int etext;
1222 #   ifdef CX_UX
1223 #       define OS_TYPE "CX_UX"
1224 #       define DATASTART ((((word)&etext + 0x3fffff) & ~0x3fffff) + 0x10000)
1225 #   endif
1226 #   ifdef  DGUX
1227 #       define OS_TYPE "DGUX"
1228         extern char * GC_SysVGetDataStart();
1229 #       define DATASTART (ptr_t)GC_SysVGetDataStart(0x10000, &etext)
1230 #   endif
1231 #   define STACKBOTTOM ((char*)0xf0000000) /* determined empirically */
1232 # endif
1233
1234 # ifdef S370
1235 #   define MACH_TYPE "S370"
1236 #   define OS_TYPE "UTS4"
1237 #   define ALIGNMENT 4  /* Required by hardware */
1238     extern int etext;
1239         extern int _etext;
1240         extern int _end;
1241         extern char * GC_SysVGetDataStart();
1242 #       define DATASTART (ptr_t)GC_SysVGetDataStart(0x10000, &_etext)
1243 #       define DATAEND (&_end)
1244 #       define HEURISTIC2
1245 # endif
1246
1247 # if defined(PJ)
1248 #   define ALIGNMENT 4
1249     extern int _etext;
1250 #   define DATASTART ((ptr_t)(&_etext))
1251 #   define HEURISTIC1
1252 # endif
1253
1254 # ifdef ARM32
1255 #   define CPP_WORDSZ 32
1256 #   define MACH_TYPE "ARM32"
1257 #   define ALIGNMENT 4
1258 #   ifdef NETBSD
1259 #       define OS_TYPE "NETBSD"
1260 #       define HEURISTIC2
1261         extern char etext;
1262 #       define DATASTART ((ptr_t)(&etext))
1263 #       define USE_GENERIC_PUSH_REGS
1264 #   endif
1265 #   ifdef LINUX
1266 #       define OS_TYPE "LINUX"
1267 #       define HEURISTIC1
1268 #       undef STACK_GRAN
1269 #       define STACK_GRAN 0x10000000
1270 #       define USE_GENERIC_PUSH_REGS
1271 #       ifdef __ELF__
1272 #            define DYNAMIC_LOADING
1273 #            include <features.h>
1274 #            if defined(__GLIBC__) && __GLIBC__ >= 2
1275 #                define LINUX_DATA_START
1276 #            else
1277                  extern char **__environ;
1278 #                define DATASTART ((ptr_t)(&__environ))
1279                               /* hideous kludge: __environ is the first */
1280                               /* word in crt0.o, and delimits the start */
1281                               /* of the data segment, no matter which   */
1282                               /* ld options were passed through.        */
1283                               /* We could use _etext instead, but that  */
1284                               /* would include .rodata, which may       */
1285                               /* contain large read-only data tables    */
1286                               /* that we'd rather not scan.             */
1287 #            endif
1288              extern int _end;
1289 #            define DATAEND (&_end)
1290 #       else
1291              extern int etext;
1292 #            define DATASTART ((ptr_t)((((word) (&etext)) + 0xfff) & ~0xfff))
1293 #       endif
1294 #   endif
1295 #endif
1296
1297 #ifdef LINUX_DATA_START
1298     /* Some Linux distributions arrange to define __data_start.  Some   */
1299     /* define data_start as a weak symbol.  The latter is technically   */
1300     /* broken, since the user program may define data_start, in which   */
1301     /* case we lose.  Nonetheless, we try both, prefering __data_start. */
1302     /* We assume gcc.   */
1303 #   pragma weak __data_start
1304     extern int __data_start;
1305 #   pragma weak data_start
1306     extern int data_start;
1307 #   define DATASTART ((ptr_t)(&__data_start != 0? &__data_start : &data_start))
1308 #endif
1309
1310 # if SCM_STACK_GROWS_UP
1311 #   define STACK_GROWS_DOWN 0
1312 # else
1313 #   define STACK_GROWS_DOWN 1
1314 #endif
1315
1316 # ifndef CPP_WORDSZ
1317 #   define CPP_WORDSZ 32
1318 # endif
1319
1320 # ifndef OS_TYPE
1321 #   define OS_TYPE ""
1322 # endif
1323
1324 # ifndef DATAEND
1325     extern int end;
1326 #   define DATAEND (&end)
1327 # endif
1328
1329 # if defined(SVR4) && !defined(GETPAGESIZE)
1330 #    include <unistd.h>
1331 #    define GETPAGESIZE()  sysconf(_SC_PAGESIZE)
1332 # endif
1333
1334 # ifndef GETPAGESIZE
1335 #   if defined(SUNOS5) || defined(IRIX5)
1336 #       include <unistd.h>
1337 #   endif
1338 #   define GETPAGESIZE() getpagesize()
1339 # endif
1340
1341 # if defined(SUNOS5) || defined(DRSNX) || defined(UTS4)
1342     /* OS has SVR4 generic features.  Probably others also qualify.     */
1343 #   define SVR4
1344 # endif
1345
1346 # if defined(SUNOS5) || defined(DRSNX)
1347     /* OS has SUNOS5 style semi-undocumented interface to dynamic       */
1348     /* loader.                                                          */
1349 #   define SUNOS5DL
1350     /* OS has SUNOS5 style signal handlers.                             */
1351 #   define SUNOS5SIGS
1352 # endif
1353
1354 # if defined(HPUX)
1355 #   define SUNOS5SIGS
1356 # endif
1357
1358 # if CPP_WORDSZ != 32 && CPP_WORDSZ != 64
1359    -> bad word size
1360 # endif
1361
1362 # ifdef PCR
1363 #   undef DYNAMIC_LOADING
1364 #   undef STACKBOTTOM
1365 #   undef HEURISTIC1
1366 #   undef HEURISTIC2
1367 #   undef PROC_VDB
1368 #   undef MPROTECT_VDB
1369 #   define PCR_VDB
1370 # endif
1371
1372 # ifdef SRC_M3
1373 /* Postponed for now. */
1374 #   undef PROC_VDB
1375 #   undef MPROTECT_VDB
1376 # endif
1377
1378 # ifdef SMALL_CONFIG
1379 /* Presumably not worth the space it takes. */
1380 #   undef PROC_VDB
1381 #   undef MPROTECT_VDB
1382 # endif
1383
1384 # ifdef USE_MUNMAP
1385 #   undef MPROTECT_VDB  /* Can't deal with address space holes. */
1386 # endif
1387
1388 # if !defined(PCR_VDB) && !defined(PROC_VDB) && !defined(MPROTECT_VDB)
1389 #   define DEFAULT_VDB
1390 # endif
1391
1392 # ifndef PREFETCH
1393 #   define PREFETCH(x)
1394 #   define NO_PREFETCH
1395 # endif
1396
1397 # ifndef PREFETCH_FOR_WRITE
1398 #   define PREFETCH_FOR_WRITE(x)
1399 #   define NO_PREFETCH_FOR_WRITE
1400 # endif
1401
1402 # ifndef CACHE_LINE_SIZE
1403 #   define CACHE_LINE_SIZE 32   /* Wild guess   */
1404 # endif
1405
1406 # ifndef CLEAR_DOUBLE
1407 #   define CLEAR_DOUBLE(x) \
1408         ((word*)x)[0] = 0; \
1409         ((word*)x)[1] = 0;
1410 # endif /* CLEAR_DOUBLE */
1411
1412 # if defined(_SOLARIS_PTHREADS) && !defined(SOLARIS_THREADS)
1413 #   define SOLARIS_THREADS
1414 # endif
1415 # if defined(IRIX_THREADS) && !defined(IRIX5)
1416 --> inconsistent configuration
1417 # endif
1418 # if defined(IRIX_JDK_THREADS) && !defined(IRIX5)
1419 --> inconsistent configuration
1420 # endif
1421 # if defined(LINUX_THREADS) && !defined(LINUX)
1422 --> inconsistent configuration
1423 # endif
1424 # if defined(SOLARIS_THREADS) && !defined(SUNOS5)
1425 --> inconsistent configuration
1426 # endif
1427 # if defined(HPUX_THREADS) && !defined(HPUX)
1428 --> inconsistent configuration
1429 # endif
1430 # if defined(PCR) || defined(SRC_M3) || \
1431         defined(SOLARIS_THREADS) || defined(WIN32_THREADS) || \
1432         defined(IRIX_THREADS) || defined(LINUX_THREADS) || \
1433         defined(IRIX_JDK_THREADS) || defined(HPUX_THREADS)
1434 #   define THREADS
1435 # endif
1436
1437 # if defined(HP_PA) || defined(M88K) || defined(POWERPC) \
1438      || (defined(I386) && defined(OS2)) || defined(UTS4) || defined(LINT)
1439         /* Use setjmp based hack to mark from callee-save registers. */
1440 #       define USE_GENERIC_PUSH_REGS
1441 # endif
1442 # if defined(SPARC) && !defined(LINUX)
1443 #   define SAVE_CALL_CHAIN
1444 #   define ASM_CLEAR_CODE       /* Stack clearing is crucial, and we    */
1445                                 /* include assembly code to do it well. */
1446 # endif
1447
1448 # if defined(LINUX) && !defined(POWERPC)
1449
1450 # if 0
1451 #   include <linux/version.h>
1452 #   if (LINUX_VERSION_CODE <= 0x10400)
1453       /* Ugly hack to get struct sigcontext_struct definition.  Required  */
1454       /* for some early 1.3.X releases.  Will hopefully go away soon. */
1455       /* in some later Linux releases, asm/sigcontext.h may have to   */
1456       /* be included instead.                                         */
1457 #     define __KERNEL__
1458 #     include <asm/signal.h>
1459 #     undef __KERNEL__
1460 #  endif
1461
1462 #  else
1463
1464       /* Kernels prior to 2.1.1 defined struct sigcontext_struct instead of */
1465       /* struct sigcontext.  libc6 (glibc2) uses "struct sigcontext" in     */
1466       /* prototypes, so we have to include the top-level sigcontext.h to    */
1467       /* make sure the former gets defined to be the latter if appropriate. */
1468 #     include <features.h>
1469 #     if 2 <= __GLIBC__
1470 #       if 2 == __GLIBC__ && 0 == __GLIBC_MINOR__
1471           /* glibc 2.1 no longer has sigcontext.h.  But signal.h        */
1472           /* has the right declaration for glibc 2.1.                   */
1473 #         include <sigcontext.h>
1474 #       endif /* 0 == __GLIBC_MINOR__ */
1475 #     else /* not 2 <= __GLIBC__ */
1476         /* libc5 doesn't have <sigcontext.h>: go directly with the kernel   */
1477         /* one.  Check LINUX_VERSION_CODE to see which we should reference. */
1478 #       include <asm/sigcontext.h>
1479 #     endif /* 2 <= __GLIBC__ */
1480 #   endif
1481 # endif
1482 # if !defined(OS2) && !defined(PCR) && !defined(AMIGA) && !defined(MACOS)
1483 #   include <sys/types.h>
1484 #   if !defined(MSWIN32) && !defined(SUNOS4)
1485 #       include <unistd.h>
1486 #   endif
1487 # endif
1488
1489 # include <signal.h>
1490
1491 /* Blatantly OS dependent routines, except for those that are related   */
1492 /* to dynamic loading.                                                  */
1493
1494 # if !defined(THREADS) && !defined(STACKBOTTOM) && defined(HEURISTIC2)
1495 #   define NEED_FIND_LIMIT
1496 # endif
1497
1498 # if defined(IRIX_THREADS) || defined(HPUX_THREADS)
1499 #   define NEED_FIND_LIMIT
1500 # endif
1501
1502 # if (defined(SUNOS4) && defined(DYNAMIC_LOADING)) && !defined(PCR)
1503 #   define NEED_FIND_LIMIT
1504 # endif
1505
1506 # if (defined(SVR4) || defined(AUX) || defined(DGUX)) && !defined(PCR)
1507 #   define NEED_FIND_LIMIT
1508 # endif
1509
1510 # if defined(LINUX) && \
1511      (defined(POWERPC) || defined(SPARC) || defined(ALPHA) || defined(IA64) \
1512       || defined(MIPS))
1513 #   define NEED_FIND_LIMIT
1514 # endif
1515
1516 #ifdef NEED_FIND_LIMIT
1517 #   include <setjmp.h>
1518 #endif
1519
1520 #ifdef FREEBSD
1521 #  include <machine/trap.h>
1522 #endif
1523
1524 #ifdef AMIGA
1525 # include <proto/exec.h>
1526 # include <proto/dos.h>
1527 # include <dos/dosextens.h>
1528 # include <workbench/startup.h>
1529 #endif
1530
1531 #ifdef MSWIN32
1532 # define WIN32_LEAN_AND_MEAN
1533 # define NOSERVICE
1534 # include <windows.h>
1535 #endif
1536
1537 #ifdef MACOS
1538 # include <Processes.h>
1539 #endif
1540
1541 #ifdef IRIX5
1542 # include <sys/uio.h>
1543 # include <malloc.h>   /* for locking */
1544 #endif
1545 #ifdef USE_MMAP
1546 # include <sys/types.h>
1547 # include <sys/mman.h>
1548 # include <sys/stat.h>
1549 # include <fcntl.h>
1550 #endif
1551
1552 #ifdef SUNOS5SIGS
1553 # include <sys/siginfo.h>
1554 # undef setjmp
1555 # undef longjmp
1556 # define setjmp(env) sigsetjmp(env, 1)
1557 # define longjmp(env, val) siglongjmp(env, val)
1558 # define jmp_buf sigjmp_buf
1559 #endif
1560
1561 #ifdef DJGPP
1562   /* Apparently necessary for djgpp 2.01.  May casuse problems with     */
1563   /* other versions.                                                    */
1564   typedef long unsigned int caddr_t;
1565 #endif
1566
1567 #ifdef PCR
1568 # include "il/PCR_IL.h"
1569 # include "th/PCR_ThCtl.h"
1570 # include "mm/PCR_MM.h"
1571 #endif
1572
1573 #if !defined(NO_EXECUTE_PERMISSION)
1574 # define OPT_PROT_EXEC PROT_EXEC
1575 #else
1576 # define OPT_PROT_EXEC 0
1577 #endif
1578
1579 # ifdef OS2
1580
1581 # include <stddef.h>
1582
1583 # if !defined(__IBMC__) && !defined(__WATCOMC__) /* e.g. EMX */
1584
1585 # else  /* IBM's compiler */
1586
1587 /* A kludge to get around what appears to be a header file bug */
1588 # ifndef WORD
1589 #   define WORD unsigned short
1590 # endif
1591 # ifndef DWORD
1592 #   define DWORD unsigned long
1593 # endif
1594
1595 # define EXE386 1
1596 # include <newexe.h>
1597 # include <exe386.h>
1598
1599 # endif  /* __IBMC__ */
1600
1601 # define INCL_DOSEXCEPTIONS
1602 # define INCL_DOSPROCESS
1603 # define INCL_DOSERRORS
1604 # define INCL_DOSMODULEMGR
1605 # define INCL_DOSMEMMGR
1606 # include <os2.h>
1607
1608 # endif /*!OS/2 */
1609
1610 /*
1611  * Find the base of the stack.
1612  * Used only in single-threaded environment.
1613  * With threads, GC_mark_roots needs to know how to do this.
1614  * Called with allocator lock held.
1615  */
1616 # ifdef MSWIN32
1617 # define is_writable(prot) ((prot) == PAGE_READWRITE \
1618                             || (prot) == PAGE_WRITECOPY \
1619                             || (prot) == PAGE_EXECUTE_READWRITE \
1620                             || (prot) == PAGE_EXECUTE_WRITECOPY)
1621 /* Return the number of bytes that are writable starting at p.  */
1622 /* The pointer p is assumed to be page aligned.                 */
1623 /* If base is not 0, *base becomes the beginning of the         */
1624 /* allocation region containing p.                              */
1625 static word GC_get_writable_length(ptr_t p, ptr_t *base)
1626 {
1627     MEMORY_BASIC_INFORMATION buf;
1628     word result;
1629     word protect;
1630
1631     result = VirtualQuery(p, &buf, sizeof(buf));
1632     if (result != sizeof(buf)) ABORT("Weird VirtualQuery result");
1633     if (base != 0) *base = (ptr_t)(buf.AllocationBase);
1634     protect = (buf.Protect & ~(PAGE_GUARD | PAGE_NOCACHE));
1635     if (!is_writable(protect)) {
1636         return(0);
1637     }
1638     if (buf.State != MEM_COMMIT) return(0);
1639     return(buf.RegionSize);
1640 }
1641
1642 void *scm_get_stack_base()
1643 {
1644     int dummy;
1645     ptr_t sp = (ptr_t)(&dummy);
1646     ptr_t trunc_sp;
1647     word size;
1648     static word GC_page_size = 0;
1649     if (!GC_page_size) {
1650         SYSTEM_INFO sysinfo;
1651         GetSystemInfo(&sysinfo);
1652         GC_page_size = sysinfo.dwPageSize;
1653     }
1654     trunc_sp = (ptr_t)((word)sp & ~(GC_page_size - 1));
1655     size = GC_get_writable_length(trunc_sp, 0);
1656     return(trunc_sp + size);
1657 }
1658
1659
1660 # else
1661
1662 # ifdef OS2
1663
1664 void *scm_get_stack_base()
1665 {
1666     PTIB ptib;
1667     PPIB ppib;
1668
1669     if (DosGetInfoBlocks(&ptib, &ppib) != NO_ERROR) {
1670         GC_err_printf0("DosGetInfoBlocks failed\n");
1671         ABORT("DosGetInfoBlocks failed\n");
1672     }
1673     return((ptr_t)(ptib -> tib_pstacklimit));
1674 }
1675
1676 # else
1677
1678 # ifdef AMIGA
1679
1680 void *scm_get_stack_base()
1681 {
1682     struct Process *proc = (struct Process*)SysBase->ThisTask;
1683
1684     /* Reference: Amiga Guru Book Pages: 42,567,574 */
1685     if (proc->pr_Task.tc_Node.ln_Type==NT_PROCESS
1686         && proc->pr_CLI != NULL) {
1687         /* first ULONG is StackSize */
1688         /*longPtr = proc->pr_ReturnAddr;
1689         size = longPtr[0];*/
1690
1691         return (char *)proc->pr_ReturnAddr + sizeof(ULONG);
1692     } else {
1693         return (char *)proc->pr_Task.tc_SPUpper;
1694     }
1695 }
1696
1697 #if 0 /* old version */
1698 void *scm_get_stack_base()
1699 {
1700     extern struct WBStartup *_WBenchMsg;
1701     extern long __base;
1702     extern long __stack;
1703     struct Task *task;
1704     struct Process *proc;
1705     struct CommandLineInterface *cli;
1706     long size;
1707
1708     if ((task = FindTask(0)) == 0) {
1709         GC_err_puts("Cannot find own task structure\n");
1710         ABORT("task missing");
1711     }
1712     proc = (struct Process *)task;
1713     cli = BADDR(proc->pr_CLI);
1714
1715     if (_WBenchMsg != 0 || cli == 0) {
1716         size = (char *)task->tc_SPUpper - (char *)task->tc_SPLower;
1717     } else {
1718         size = cli->cli_DefaultStack * 4;
1719     }
1720     return (ptr_t)(__base + GC_max(size, __stack));
1721 }
1722 #endif /* 0 */
1723
1724 # else /* !AMIGA, !OS2, ... */
1725
1726 # ifdef NEED_FIND_LIMIT
1727   /* Some tools to implement HEURISTIC2 */
1728 #   define MIN_PAGE_SIZE 256    /* Smallest conceivable page size, bytes */
1729     /* static */ jmp_buf GC_jmp_buf;
1730
1731     /*ARGSUSED*/
1732     static void GC_fault_handler(sig)
1733     int sig;
1734     {
1735         longjmp(GC_jmp_buf, 1);
1736     }
1737
1738 #   ifdef __STDC__
1739         typedef void (*handler)(int);
1740 #   else
1741         typedef void (*handler)();
1742 #   endif
1743
1744 #   if defined(SUNOS5SIGS) || defined(IRIX5) || defined(OSF1)
1745         static struct sigaction old_segv_act;
1746 #       if defined(_sigargs) || defined(HPUX) /* !Irix6.x */
1747             static struct sigaction old_bus_act;
1748 #       endif
1749 #   else
1750         static handler old_segv_handler, old_bus_handler;
1751 #   endif
1752
1753     static void GC_setup_temporary_fault_handler()
1754     {
1755 #       if defined(SUNOS5SIGS) || defined(IRIX5) || defined(OSF1)
1756           struct sigaction      act;
1757
1758           act.sa_handler        = GC_fault_handler;
1759           act.sa_flags          = SA_RESTART | SA_NODEFER;
1760           /* The presence of SA_NODEFER represents yet another gross    */
1761           /* hack.  Under Solaris 2.3, siglongjmp doesn't appear to     */
1762           /* interact correctly with -lthread.  We hide the confusion   */
1763           /* by making sure that signal handling doesn't affect the     */
1764           /* signal mask.                                               */
1765
1766           (void) sigemptyset(&act.sa_mask);
1767 #         ifdef IRIX_THREADS
1768                 /* Older versions have a bug related to retrieving and  */
1769                 /* and setting a handler at the same time.              */
1770                 (void) sigaction(SIGSEGV, 0, &old_segv_act);
1771                 (void) sigaction(SIGSEGV, &act, 0);
1772 #         else
1773                 (void) sigaction(SIGSEGV, &act, &old_segv_act);
1774 #               if defined(IRIX5) && defined(_sigargs) /* Irix 5.x, not 6.x */ \
1775                    || defined(HPUX)
1776                     /* Under Irix 5.x or HP/UX, we may get SIGBUS.      */
1777                     /* Pthreads doesn't exist under Irix 5.x, so we     */
1778                     /* don't have to worry in the threads case.         */
1779                     (void) sigaction(SIGBUS, &act, &old_bus_act);
1780 #               endif
1781 #         endif /* IRIX_THREADS */
1782 #       else
1783           old_segv_handler = signal(SIGSEGV, GC_fault_handler);
1784 #         ifdef SIGBUS
1785             old_bus_handler = signal(SIGBUS, GC_fault_handler);
1786 #         endif
1787 #       endif
1788     }
1789
1790     static void GC_reset_fault_handler()
1791     {
1792 #       if defined(SUNOS5SIGS) || defined(IRIX5) || defined(OSF1)
1793           (void) sigaction(SIGSEGV, &old_segv_act, 0);
1794 #         if defined(IRIX5) && defined(_sigargs) /* Irix 5.x, not 6.x */ \
1795              || defined(HPUX)
1796               (void) sigaction(SIGBUS, &old_bus_act, 0);
1797 #         endif
1798 #       else
1799           (void) signal(SIGSEGV, old_segv_handler);
1800 #         ifdef SIGBUS
1801             (void) signal(SIGBUS, old_bus_handler);
1802 #         endif
1803 #       endif
1804     }
1805
1806     /* Single argument version, robust against whole program analysis. */
1807     static void
1808     GC_noop1(x)
1809     word x;
1810     {
1811       static VOLATILE word sink;
1812       sink = x;
1813     }
1814
1815     /* Return the first nonaddressible location > p (up) or     */
1816     /* the smallest location q s.t. [q,p] is addressible (!up). */
1817     static ptr_t GC_find_limit(p, up)
1818     ptr_t p;
1819     GC_bool up;
1820     {
1821         static VOLATILE ptr_t result;
1822                 /* Needs to be static, since otherwise it may not be    */
1823                 /* preserved across the longjmp.  Can safely be         */
1824                 /* static since it's only called once, with the         */
1825                 /* allocation lock held.                                */
1826
1827
1828         GC_setup_temporary_fault_handler();
1829         if (setjmp(GC_jmp_buf) == 0) {
1830             result = (ptr_t)(((word)(p))
1831                               & ~(MIN_PAGE_SIZE-1));
1832             for (;;) {
1833                 if (up) {
1834                     result += MIN_PAGE_SIZE;
1835                 } else {
1836                     result -= MIN_PAGE_SIZE;
1837                 }
1838                 GC_noop1((word)(*result));
1839             }
1840         }
1841         GC_reset_fault_handler();
1842         if (!up) {
1843             result += MIN_PAGE_SIZE;
1844         }
1845         return(result);
1846     }
1847
1848 # endif
1849
1850 #ifdef LINUX_STACKBOTTOM
1851
1852 #include <sys/types.h>
1853 #include <sys/stat.h>
1854 #include <fcntl.h>
1855
1856 # define STAT_SKIP 27   /* Number of fields preceding startstack        */
1857                         /* field in /proc/self/stat                     */
1858
1859   static ptr_t GC_linux_stack_base(void)
1860   {
1861     /* We read the stack base value from /proc/self/stat.  We do this   */
1862     /* using direct I/O system calls in order to avoid calling malloc   */
1863     /* in case REDIRECT_MALLOC is defined.                              */
1864 #   define STAT_BUF_SIZE 4096
1865 #   ifdef USE_LD_WRAP
1866 #       define STAT_READ __real_read
1867 #   else
1868 #       define STAT_READ read
1869 #   endif
1870     char stat_buf[STAT_BUF_SIZE];
1871     int f;
1872     char c;
1873     word result = 0;
1874     size_t i, buf_offset = 0;
1875
1876     f = open("/proc/self/stat", O_RDONLY);
1877     if (f < 0 || STAT_READ(f, stat_buf, STAT_BUF_SIZE) < 2 * STAT_SKIP) {
1878         ABORT("Couldn't read /proc/self/stat");
1879     }
1880     c = stat_buf[buf_offset++];
1881     /* Skip the required number of fields.  This number is hopefully    */
1882     /* constant across all Linux implementations.                       */
1883       for (i = 0; i < STAT_SKIP; ++i) {
1884         while (isspace(c)) c = stat_buf[buf_offset++];
1885         while (!isspace(c)) c = stat_buf[buf_offset++];
1886       }
1887     while (isspace(c)) c = stat_buf[buf_offset++];
1888     while (isdigit(c)) {
1889       result *= 10;
1890       result += c - '0';
1891       c = stat_buf[buf_offset++];
1892     }
1893     close(f);
1894     if (result < 0x10000000) ABORT("Absurd stack bottom value");
1895     return (ptr_t)result;
1896   }
1897
1898 #endif /* LINUX_STACKBOTTOM */
1899
1900 void *scm_get_stack_base()
1901 {
1902     word dummy;
1903     void *result;
1904
1905     result = &dummy;  /* initialize to silence compiler */
1906
1907 #   define STACKBOTTOM_ALIGNMENT_M1 ((word)STACK_GRAN - 1)
1908
1909 #   ifdef STACKBOTTOM
1910         return(STACKBOTTOM);
1911 #   else
1912 #       ifdef HEURISTIC1
1913 #          if STACK_GROWS_DOWN
1914              result = (ptr_t)((((word)(&dummy))
1915                                + STACKBOTTOM_ALIGNMENT_M1)
1916                               & ~STACKBOTTOM_ALIGNMENT_M1);
1917 #          else
1918              result = (ptr_t)(((word)(&dummy))
1919                               & ~STACKBOTTOM_ALIGNMENT_M1);
1920 #          endif
1921 #       endif /* HEURISTIC1 */
1922 #       ifdef LINUX_STACKBOTTOM
1923            result = GC_linux_stack_base();
1924 #       endif
1925 #       ifdef HEURISTIC2
1926 #           if STACK_GROWS_DOWN
1927                 result = GC_find_limit((ptr_t)(&dummy), TRUE);
1928 #               ifdef HEURISTIC2_LIMIT
1929                     if ((ptr_t)result > HEURISTIC2_LIMIT
1930                         && (ptr_t)(&dummy) < HEURISTIC2_LIMIT) {
1931                             result = HEURISTIC2_LIMIT;
1932                     }
1933 #               endif
1934 #           else
1935                 result = GC_find_limit((ptr_t)(&dummy), FALSE);
1936 #               ifdef HEURISTIC2_LIMIT
1937                     if (result < HEURISTIC2_LIMIT
1938                         && (ptr_t)(&dummy) > HEURISTIC2_LIMIT) {
1939                             result = HEURISTIC2_LIMIT;
1940                     }
1941 #               endif
1942 #           endif
1943
1944 #       endif /* HEURISTIC2 */
1945 #       if STACK_GROWS_DOWN
1946             if (result == 0) result = (ptr_t)(signed_word)(-sizeof(ptr_t));
1947 #       endif
1948         return(result);
1949 #   endif /* STACKBOTTOM */
1950 }
1951
1952 # endif /* ! AMIGA */
1953 # endif /* ! OS2 */
1954 # endif /* ! MSWIN32 */
1955
1956 #endif /* mach_type_known */
1957 #endif /* ! HAVE_LIBC_STACK_END */