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