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