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