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