2 * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
3 * Copyright (c) 1991-1995 by Xerox Corporation. All rights reserved.
4 * Copyright (c) 1996-1999 by Silicon Graphics. All rights reserved.
5 * Copyright (c) 1999 by Hewlett-Packard Company. All rights reserved.
6 * Copyright (c) 2000 Free Software Foundation
8 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
9 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
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.
20 * Copied from gc5.2, files "os_dep.c", "gc_priv.h", "mark.c" and "gcconfig.h",
21 * and modified for Guile by Marius Vollmer.
25 #include "libguile/gc.h"
27 #define ABORT(msg) abort ()
29 typedef char * ptr_t
; /* A generic pointer to which we can add */
30 /* byte displacements. */
31 /* Preferably identical to caddr_t, if it */
34 /* Define word and signed_word to be unsigned and signed types of the */
35 /* size as char * or void *. There seems to be no way to do this */
36 /* even semi-portably. The following is probably no better/worse */
37 /* than almost anything else. */
38 /* The ANSI standard suggests that size_t and ptr_diff_t might be */
39 /* better choices. But those appear to have incorrect definitions */
40 /* on may systems. Notably "typedef int size_t" seems to be both */
41 /* frequent and WRONG. */
42 typedef unsigned long GC_word
;
43 typedef long GC_signed_word
;
46 typedef GC_signed_word signed_word
;
54 # if !(defined( sony_news ) )
57 # define VOLATILE volatile
65 /* Single argument version, robust against whole program analysis. */
66 static void GC_noop1(x
)
69 static VOLATILE word sink
;
74 /* Machine dependent parameters. Some tuning parameters can be found */
75 /* near the top of gc_private.h. */
77 /* Machine specific parts contributed by various people. See README file. */
79 /* First a unified test for Linux: */
80 # if defined(linux) || defined(__linux__)
84 /* Determine the machine type: */
85 # if defined(sun) && defined(mc68000)
88 # define mach_type_known
90 # if defined(hp9000s300)
93 # define mach_type_known
95 # if defined(__OpenBSD__) && defined(m68k)
98 # define mach_type_known
100 # if defined(__OpenBSD__) && defined(__sparc__)
103 # define mach_type_known
105 # if defined(__NetBSD__) && defined(m68k)
108 # define mach_type_known
110 # if defined(__NetBSD__) && defined(arm32)
113 # define mach_type_known
122 # define mach_type_known
124 # if defined(mips) || defined(__mips)
127 # if defined(ultrix) || defined(__ultrix) || defined(__NetBSD__)
130 # if defined(_SYSTYPE_SVR4) || defined(SYSTYPE_SVR4) \
131 || defined(__SYSTYPE_SVR4__)
132 # define IRIX5 /* or IRIX 6.X */
134 # define RISCOS /* or IRIX 4.X */
138 # define mach_type_known
140 # if defined(sequent) && defined(i386)
143 # define mach_type_known
145 # if defined(sun) && defined(i386)
148 # define mach_type_known
150 # if (defined(__OS2__) || defined(__EMX__)) && defined(__32BIT__)
153 # define mach_type_known
157 # define mach_type_known
159 # if defined(sun) && (defined(sparc) || defined(__sparc))
161 /* Test for SunOS 5.x */
168 # define mach_type_known
170 # if defined(sparc) && defined(unix) && !defined(sun) && !defined(linux) \
171 && !defined(__OpenBSD__)
174 # define mach_type_known
178 # define mach_type_known
180 # if defined(_M_XENIX) && defined(_M_SYSV) && defined(_M_I386)
181 /* The above test may need refinement */
183 # if defined(_SCO_ELF)
188 # define mach_type_known
190 # if defined(_AUX_SOURCE)
193 # define mach_type_known
195 # if defined(_PA_RISC1_0) || defined(_PA_RISC1_1) || defined(_PA_RISC2_0) \
196 || defined(hppa) || defined(__hppa__)
201 # define mach_type_known
203 # if defined(LINUX) && (defined(i386) || defined(__i386__))
205 # define mach_type_known
207 # if defined(LINUX) && (defined(__ia64__) || defined(__ia64))
209 # define mach_type_known
211 # if defined(LINUX) && defined(powerpc)
213 # define mach_type_known
215 # if defined(LINUX) && defined(__mc68000__)
217 # define mach_type_known
219 # if defined(LINUX) && (defined(sparc) || defined(__sparc__))
221 # define mach_type_known
223 # if defined(LINUX) && defined(arm)
225 # define mach_type_known
227 # if defined(__alpha) || defined(__alpha__)
230 # define OSF1 /* a.k.a Digital Unix */
232 # define mach_type_known
234 # if defined(_AMIGA) && !defined(AMIGA)
239 # define mach_type_known
241 # if defined(THINK_C) || defined(__MWERKS__) && !defined(__powerc)
244 # define mach_type_known
246 # if defined(__MWERKS__) && defined(__powerc)
249 # define mach_type_known
254 # define mach_type_known
256 # if defined(NeXT) && defined(mc68000)
259 # define mach_type_known
261 # if defined(NeXT) && defined(i386)
264 # define mach_type_known
266 # if defined(__OpenBSD__) && defined(i386)
269 # define mach_type_known
271 # if defined(__FreeBSD__) && defined(i386)
274 # define mach_type_known
276 # if defined(__NetBSD__) && defined(i386)
279 # define mach_type_known
281 # if defined(bsdi) && defined(i386)
284 # define mach_type_known
286 # if !defined(mach_type_known) && defined(__386BSD__)
289 # define mach_type_known
291 # if defined(_CX_UX) && defined(_M88K)
294 # define mach_type_known
299 # define mach_type_known
301 # if (defined(_MSDOS) || defined(_MSC_VER)) && (_M_IX86 >= 300) \
302 || defined(_WIN32) && !defined(__CYGWIN32__) && !defined(__CYGWIN__)
304 # define MSWIN32 /* or Win32s */
305 # define mach_type_known
307 # if defined(__DJGPP__)
310 # define DJGPP /* MSDOS running the DJGPP port of GCC */
312 # define mach_type_known
314 # if defined(__CYGWIN32__) || defined(__CYGWIN__)
317 # define mach_type_known
319 # if defined(__MINGW32__)
322 # define mach_type_known
324 # if defined(__BORLANDC__)
327 # define mach_type_known
329 # if defined(_UTS) && !defined(mach_type_known)
332 # define mach_type_known
336 # define mach_type_known
339 # if defined(__WATCOMC__) && defined(__386__)
341 # if !defined(OS2) && !defined(MSWIN32) && !defined(DOS4GW)
342 # if defined(__OS2__)
345 # if defined(__WINDOWS_386__) || defined(__NT__)
352 # define mach_type_known
355 /* Feel free to add more clauses here */
357 /* Or manually define the machine type here. A machine type is */
358 /* characterized by the architecture. Some */
359 /* machine types are further subdivided by OS. */
360 /* the macros ULTRIX, RISCOS, and BSD to distinguish. */
361 /* Note that SGI IRIX is treated identically to RISCOS. */
362 /* SYSV on an M68K actually means A/UX. */
363 /* The distinction in these cases is usually the stack starting address */
364 # ifndef mach_type_known
365 --> unknown machine type
367 /* Mapping is: M68K ==> Motorola 680X0 */
368 /* (SUNOS4,HP,NEXT, and SYSV (A/UX), */
369 /* MACOS and AMIGA variants) */
370 /* I386 ==> Intel 386 */
371 /* (SEQUENT, OS2, SCO, LINUX, NETBSD, */
372 /* FREEBSD, THREE86BSD, MSWIN32, */
373 /* BSDI,SUNOS5, NEXT, other variants) */
374 /* NS32K ==> Encore Multimax */
375 /* MIPS ==> R2000 or R3000 */
376 /* (RISCOS, ULTRIX variants) */
377 /* VAX ==> DEC VAX */
378 /* (BSD, ULTRIX variants) */
379 /* RS6000 ==> IBM RS/6000 AIX3.X */
380 /* RT ==> IBM PC/RT */
381 /* HP_PA ==> HP9000/700 & /800 */
383 /* SPARC ==> SPARC under SunOS */
384 /* (SUNOS4, SUNOS5, */
385 /* DRSNX variants) */
386 /* ALPHA ==> DEC Alpha */
387 /* (OSF1 and LINUX variants) */
388 /* M88K ==> Motorola 88XX0 */
389 /* (CX_UX and DGUX) */
390 /* S370 ==> 370-like machine */
391 /* running Amdahl UTS4 */
392 /* ARM32 ==> Intel StrongARM */
393 /* IA64 ==> Intel IA64 */
398 * For each architecture and OS, the following need to be defined:
400 * CPP_WORD_SZ is a simple integer constant representing the word size.
401 * in bits. We assume byte addressibility, where a byte has 8 bits.
402 * We also assume CPP_WORD_SZ is either 32 or 64.
403 * (We care about the length of pointers, not hardware
404 * bus widths. Thus a 64 bit processor with a C compiler that uses
405 * 32 bit pointers should use CPP_WORD_SZ of 32, not 64. Default is 32.)
407 * MACH_TYPE is a string representation of the machine type.
408 * OS_TYPE is analogous for the OS.
410 * ALIGNMENT is the largest N, such that
411 * all pointer are guaranteed to be aligned on N byte boundaries.
412 * defining it to be 1 will always work, but perform poorly.
414 * DATASTART is the beginning of the data segment.
415 * On UNIX systems, the collector will scan the area between DATASTART
416 * and DATAEND for root pointers.
418 * DATAEND, if not &end.
420 * ALIGN_DOUBLE of GC_malloc should return blocks aligned to twice
423 * STACKBOTTOM is the cool end of the stack, which is usually the
424 * highest address in the stack.
425 * Under PCR or OS/2, we have other ways of finding thread stacks.
426 * For each machine, the following should:
427 * 1) define STACK_GROWS_UP if the stack grows toward higher addresses, and
428 * 2) define exactly one of
429 * STACKBOTTOM (should be defined to be an expression)
432 * If either of the last two macros are defined, then STACKBOTTOM is computed
433 * during collector startup using one of the following two heuristics:
434 * HEURISTIC1: Take an address inside GC_init's frame, and round it up to
435 * the next multiple of STACK_GRAN.
436 * HEURISTIC2: Take an address inside GC_init's frame, increment it repeatedly
437 * in small steps (decrement if STACK_GROWS_UP), and read the value
438 * at each location. Remember the value when the first
439 * Segmentation violation or Bus error is signalled. Round that
440 * to the nearest plausible page boundary, and use that instead
443 * Gustavo Rodriguez-Rivera points out that on most (all?) Unix machines,
444 * the value of environ is a pointer that can serve as STACKBOTTOM.
445 * I expect that HEURISTIC2 can be replaced by this approach, which
446 * interferes far less with debugging.
448 * If no expression for STACKBOTTOM can be found, and neither of the above
449 * heuristics are usable, the collector can still be used with all of the above
450 * undefined, provided one of the following is done:
451 * 1) GC_mark_roots can be changed to somehow mark from the correct stack(s)
452 * without reference to STACKBOTTOM. This is appropriate for use in
453 * conjunction with thread packages, since there will be multiple stacks.
454 * (Allocating thread stacks in the heap, and treating them as ordinary
455 * heap data objects is also possible as a last resort. However, this is
456 * likely to introduce significant amounts of excess storage retention
457 * unless the dead parts of the thread stacks are periodically cleared.)
458 * 2) Client code may set GC_stackbottom before calling any GC_ routines.
459 * If the author of the client code controls the main program, this is
460 * easily accomplished by introducing a new main program, setting
461 * GC_stackbottom to the address of a local variable, and then calling
462 * the original main program. The new main program would read something
465 * # include "gc_private.h"
467 * main(argc, argv, envp)
469 * char **argv, **envp;
473 * GC_stackbottom = (ptr_t)(&dummy);
474 * return(real_main(argc, argv, envp));
478 * Each architecture may also define the style of virtual dirty bit
479 * implementation to be used:
480 * MPROTECT_VDB: Write protect the heap and catch faults.
481 * PROC_VDB: Use the SVR4 /proc primitives to read dirty bits.
483 * An architecture may define DYNAMIC_LOADING if dynamic_load.c
484 * defined GC_register_dynamic_libraries() for the architecture.
486 * An architecture may define PREFETCH(x) to preload the cache with *x.
487 * This defaults to a no-op.
489 * PREFETCH_FOR_WRITE(x) is used if *x is about to be written.
491 * An architecture may also define CLEAR_DOUBLE(x) to be a fast way to
492 * clear the two words at GC_malloc-aligned address x. By default,
493 * word stores of 0 are used instead.
497 # define STACK_GRAN 0x1000000
499 # define MACH_TYPE "M68K"
502 # define OS_TYPE "OPENBSD"
505 # define DATASTART ((ptr_t)(&etext))
508 # define OS_TYPE "NETBSD"
511 # define DATASTART ((ptr_t)(&etext))
514 # define OS_TYPE "LINUX"
515 # define STACKBOTTOM ((ptr_t)0xf0000000)
516 # define MPROTECT_VDB
518 # define DYNAMIC_LOADING
519 extern char **__environ
;
520 # define DATASTART ((ptr_t)(&__environ))
521 /* hideous kludge: __environ is the first */
522 /* word in crt0.o, and delimits the start */
523 /* of the data segment, no matter which */
524 /* ld options were passed through. */
525 /* We could use _etext instead, but that */
526 /* would include .rodata, which may */
527 /* contain large read-only data tables */
528 /* that we'd rather not scan. */
530 # define DATAEND (&_end)
533 # define DATASTART ((ptr_t)((((word) (&etext)) + 0xfff) & ~0xfff))
537 # define OS_TYPE "SUNOS4"
539 # define DATASTART ((ptr_t)((((word) (&etext)) + 0x1ffff) & ~0x1ffff))
540 # define HEURISTIC1 /* differs */
541 # define DYNAMIC_LOADING
544 # define OS_TYPE "HP"
546 # define DATASTART ((ptr_t)((((word) (&etext)) + 0xfff) & ~0xfff))
547 # define STACKBOTTOM ((ptr_t) 0xffeffffc)
548 /* empirically determined. seems to work. */
550 # define GETPAGESIZE() sysconf(_SC_PAGE_SIZE)
553 # define OS_TYPE "SYSV"
555 # define DATASTART ((ptr_t)((((word) (&etext)) + 0x3fffff) \
557 +((word)&etext & 0x1fff))
558 /* This only works for shared-text binaries with magic number 0413.
559 The other sorts of SysV binaries put the data at the end of the text,
560 in which case the default of &etext would work. Unfortunately,
561 handling both would require having the magic-number available.
564 # define STACKBOTTOM ((ptr_t)0xFFFFFFFE)
565 /* The stack starts at the top of memory, but */
566 /* 0x0 cannot be used as setjump_test complains */
567 /* that the stack direction is incorrect. Two */
568 /* bytes down from 0x0 should be safe enough. */
570 # include <sys/mmu.h>
571 # define GETPAGESIZE() PAGESIZE /* Is this still right? */
574 # define OS_TYPE "AMIGA"
575 /* STACKBOTTOM and DATASTART handled specially */
577 # define DATAEND /* not needed */
578 # define GETPAGESIZE() 4096
584 # define OS_TYPE "MACOS"
585 /* see os_dep.c for details of global data segments. */
586 # define STACKBOTTOM ((ptr_t) LMGetCurStackBase())
587 # define DATAEND /* not needed */
588 # define GETPAGESIZE() 4096
591 # define OS_TYPE "NEXT"
592 # define DATASTART ((ptr_t) get_etext())
593 # define STACKBOTTOM ((ptr_t) 0x4000000)
594 # define DATAEND /* not needed */
599 # define MACH_TYPE "POWERPC"
601 # define ALIGNMENT 2 /* Still necessary? Could it be 4? */
605 # define OS_TYPE "MACOS"
606 /* see os_dep.c for details of global data segments. */
607 # define STACKBOTTOM ((ptr_t) LMGetCurStackBase())
608 # define DATAEND /* not needed */
611 # define ALIGNMENT 4 /* Guess. Can someone verify? */
612 /* This was 2, but that didn't sound right. */
613 # define OS_TYPE "LINUX"
615 # define DYNAMIC_LOADING
617 # define STACK_GRAN 0x10000000
618 /* Stack usually starts at 0x80000000 */
619 # define LINUX_DATA_START
621 # define DATAEND (&_end)
625 # define OS_TYPE "MACOSX"
626 # define DATASTART ((ptr_t) get_etext())
627 # define STACKBOTTOM ((ptr_t) 0xc0000000)
628 # define DATAEND /* not needed */
633 # define MACH_TYPE "VAX"
634 # define ALIGNMENT 4 /* Pointers are longword aligned by 4.2 C compiler */
636 # define DATASTART ((ptr_t)(&etext))
638 # define OS_TYPE "BSD"
640 /* HEURISTIC2 may be OK, but it's hard to test. */
643 # define OS_TYPE "ULTRIX"
644 # define STACKBOTTOM ((ptr_t) 0x7fffc800)
649 # define MACH_TYPE "RT"
651 # define DATASTART ((ptr_t) 0x10000000)
652 # define STACKBOTTOM ((ptr_t) 0x1fffd800)
656 # define MACH_TYPE "SPARC"
657 # define ALIGNMENT 4 /* Required by hardware */
658 # define ALIGN_DOUBLE
661 # define OS_TYPE "SUNOS5"
664 extern char * GC_SysVGetDataStart();
665 # define DATASTART (ptr_t)GC_SysVGetDataStart(0x10000, &_etext)
666 # define DATAEND (&_end)
671 # define HEAP_START (ptr_t)0x40000000
673 # define HEAP_START DATAEND
676 /* HEURISTIC1 reportedly no longer works under 2.7. Thus we */
677 /* switched to HEURISTIC2, eventhough it creates some debugging */
681 # define GETPAGESIZE() sysconf(_SC_PAGESIZE)
682 /* getpagesize() appeared to be missing from at least one */
683 /* Solaris 5.4 installation. Weird. */
684 # define DYNAMIC_LOADING
687 # define OS_TYPE "SUNOS4"
688 /* [If you have a weak stomach, don't read this.] */
689 /* We would like to use: */
690 /* # define DATASTART ((ptr_t)((((word) (&etext)) + 0x1fff) & ~0x1fff)) */
691 /* This fails occasionally, due to an ancient, but very */
692 /* persistent ld bug. &etext is set 32 bytes too high. */
693 /* We instead read the text segment size from the a.out */
694 /* header, which happens to be mapped into our address space */
695 /* at the start of the text segment. The detective work here */
696 /* was done by Robert Ehrlich, Manuel Serrano, and Bernard */
697 /* Serpette of INRIA. */
698 /* This assumes ZMAGIC, i.e. demand-loadable executables. */
699 # define TEXTSTART 0x2000
700 # define DATASTART ((ptr_t)(*(int *)(TEXTSTART+0x4)+TEXTSTART))
701 # define MPROTECT_VDB
703 # define DYNAMIC_LOADING
706 # define CPP_WORDSZ 32
707 # define OS_TYPE "DRSNX"
708 extern char * GC_SysVGetDataStart();
710 # define DATASTART (ptr_t)GC_SysVGetDataStart(0x10000, &etext)
711 # define MPROTECT_VDB
712 # define STACKBOTTOM ((ptr_t) 0xdfff0000)
713 # define DYNAMIC_LOADING
716 # define OS_TYPE "LINUX"
718 # define LINUX_DATA_START
719 # define DYNAMIC_LOADING
721 Linux Sparc non elf
?
724 # define DATAEND (&_end)
726 # define STACKBOTTOM ((ptr_t) 0xf0000000)
729 # define OS_TYPE "OPENBSD"
730 # define STACKBOTTOM ((ptr_t) 0xf8000000)
731 # define DATASTART ((ptr_t)(&etext))
736 # define MACH_TYPE "I386"
737 # define ALIGNMENT 4 /* Appears to hold for all "32 bit" compilers */
738 /* except Borland. The -a4 option fixes */
740 /* Ivan Demakov: For Watcom the option is -zp4. */
741 # ifndef SMALL_CONFIG
742 # define ALIGN_DOUBLE /* Not strictly necessary, but may give speed */
743 /* improvement on Pentiums. */
746 # define OS_TYPE "SEQUENT"
748 # define DATASTART ((ptr_t)((((word) (&etext)) + 0xfff) & ~0xfff))
749 # define STACKBOTTOM ((ptr_t) 0x3ffff000)
752 # define OS_TYPE "SUNOS5"
753 extern int etext
, _start
;
754 extern char * GC_SysVGetDataStart();
755 # define DATASTART GC_SysVGetDataStart(0x1000, &etext)
756 # define STACKBOTTOM ((ptr_t)(&_start))
757 /** At least in Solaris 2.5, PROC_VDB gives wrong values for dirty bits. */
758 /*# define PROC_VDB*/
759 # define DYNAMIC_LOADING
764 # define HEAP_START (ptr_t)0x40000000
766 # define HEAP_START DATAEND
770 # define OS_TYPE "SCO"
772 # define DATASTART ((ptr_t)((((word) (&etext)) + 0x3fffff) \
774 +((word)&etext & 0xfff))
775 # define STACKBOTTOM ((ptr_t) 0x7ffffffc)
778 # define OS_TYPE "SCO_ELF"
780 # define DATASTART ((ptr_t)(&etext))
781 # define STACKBOTTOM ((ptr_t) 0x08048000)
782 # define DYNAMIC_LOADING
783 # define ELF_CLASS ELFCLASS32
786 # define OS_TYPE "LINUX"
787 # define LINUX_STACKBOTTOM
791 # define STACK_GRAN 0x10000000
792 /* STACKBOTTOM is usually 0xc0000000, but this changes with */
793 /* different kernel configurations. In particular, systems */
794 /* with 2GB physical memory will usually move the user */
795 /* address space limit, and hence initial SP to 0x80000000. */
797 # if !defined(LINUX_THREADS) || !defined(REDIRECT_MALLOC)
798 # define MPROTECT_VDB
800 /* We seem to get random errors in incremental mode, */
801 /* possibly because Linux threads is itself a malloc client */
802 /* and can't deal with the signals. */
805 # define DYNAMIC_LOADING
806 # ifdef UNDEFINED /* includes ro data */
808 # define DATASTART ((ptr_t)((((word) (&_etext)) + 0xfff) & ~0xfff))
810 # include <features.h>
811 # if defined(__GLIBC__) && __GLIBC__ >= 2
812 # define LINUX_DATA_START
814 extern char **__environ
;
815 # define DATASTART ((ptr_t)(&__environ))
816 /* hideous kludge: __environ is the first */
817 /* word in crt0.o, and delimits the start */
818 /* of the data segment, no matter which */
819 /* ld options were passed through. */
820 /* We could use _etext instead, but that */
821 /* would include .rodata, which may */
822 /* contain large read-only data tables */
823 /* that we'd rather not scan. */
826 # define DATAEND (&_end)
829 # define DATASTART ((ptr_t)((((word) (&etext)) + 0xfff) & ~0xfff))
831 # ifdef USE_I686_PREFETCH
832 # define PREFETCH(x) \
833 __asm__ __volatile__ (" prefetchnta %0": : "m"(*(char *)(x)))
834 /* Empirically prefetcht0 is much more effective at reducing */
835 /* cache miss stalls for the targetted load instructions. But it */
836 /* seems to interfere enough with other cache traffic that the net */
837 /* result is worse than prefetchnta. */
839 /* Using prefetches for write seems to have a slight negative */
840 /* impact on performance, at least for a PIII/500. */
841 # define PREFETCH_FOR_WRITE(x) \
842 __asm__ __volatile__ (" prefetcht0 %0": : "m"(*(char *)(x)))
845 # ifdef USE_3DNOW_PREFETCH
846 # define PREFETCH(x) \
847 __asm__ __volatile__ (" prefetch %0": : "m"(*(char *)(x)))
848 # define PREFETCH_FOR_WRITE(x)
849 __asm__
__volatile__ (" prefetchw %0": : "m"(*(char *)(x
)))
853 # define OS_TYPE "CYGWIN32"
854 extern int _data_start__
;
855 extern int _data_end__
;
856 extern int _bss_start__
;
857 extern int _bss_end__
;
858 /* For binutils 2.9.1, we have */
859 /* DATASTART = _data_start__ */
860 /* DATAEND = _bss_end__ */
861 /* whereas for some earlier versions it was */
862 /* DATASTART = _bss_start__ */
863 /* DATAEND = _data_end__ */
864 /* To get it right for both, we take the */
865 /* minumum/maximum of the two. */
866 # define MAX(x,y) ((x) > (y) ? (x) : (y))
867 # define MIN(x,y) ((x) < (y) ? (x) : (y))
868 # define DATASTART ((ptr_t) MIN(&_data_start__, &_bss_start__))
869 # define DATAEND ((ptr_t) MAX(&_data_end__, &_bss_end__))
871 # define STACK_GRAN 0x10000
875 # define OS_TYPE "OS2"
876 /* STACKBOTTOM and DATASTART are handled specially in */
877 /* os_dep.c. OS2 actually has the right */
879 # define DATAEND /* not needed */
882 # define OS_TYPE "MSWIN32"
883 /* STACKBOTTOM and DATASTART are handled specially in */
886 # define MPROTECT_VDB
888 # define DATAEND /* not needed */
891 # define OS_TYPE "DJGPP"
892 # include "stubinfo.h"
895 extern int __djgpp_stack_limit
;
896 # define DATASTART ((ptr_t)((((word) (&etext)) + 0x1ff) & ~0x1ff))
897 /* # define STACKBOTTOM ((ptr_t)((word) _stubinfo + _stubinfo->size \
899 # define STACKBOTTOM ((ptr_t)((word) __djgpp_stack_limit + _stklen))
900 /* This may not be right. */
903 # define OS_TYPE "OPENBSD"
906 # define OS_TYPE "FREEBSD"
907 # define MPROTECT_VDB
910 # define OS_TYPE "NETBSD"
913 # define OS_TYPE "THREE86BSD"
916 # define OS_TYPE "BSDI"
918 # if defined(OPENBSD) || defined(FREEBSD) || defined(NETBSD) \
919 || defined(THREE86BSD) || defined(BSDI)
922 # define DATASTART ((ptr_t)(&etext))
925 # define OS_TYPE "NEXT"
926 # define DATASTART ((ptr_t) get_etext())
927 # define STACKBOTTOM ((ptr_t)0xc0000000)
928 # define DATAEND /* not needed */
931 # define OS_TYPE "DOS4GW"
932 extern long __nullarea
;
934 extern char *_STACKTOP
;
935 /* Depending on calling conventions Watcom C either precedes
936 or does not precedes with undescore names of C-variables.
937 Make sure startup code variables always have the same names. */
938 #pragma aux __nullarea "*";
939 #pragma aux _end "*";
940 # define STACKBOTTOM ((ptr_t) _STACKTOP)
941 /* confused? me too. */
942 # define DATASTART ((ptr_t) &__nullarea)
943 # define DATAEND ((ptr_t) &_end)
948 # define MACH_TYPE "NS32K"
950 extern char **environ
;
951 # define DATASTART ((ptr_t)(&environ))
952 /* hideous kludge: environ is the first */
953 /* word in crt0.o, and delimits the start */
954 /* of the data segment, no matter which */
955 /* ld options were passed through. */
956 # define STACKBOTTOM ((ptr_t) 0xfffff000) /* for Encore */
960 # define MACH_TYPE "MIPS"
961 /* # define STACKBOTTOM ((ptr_t)0x7fff8000) sometimes also works. */
963 /* This was developed for a linuxce style platform. Probably */
964 /* needs to be tweaked for workstation class machines. */
965 # define OS_TYPE "LINUX"
966 extern int __data_start
;
967 # define DATASTART ((ptr_t)(&__data_start))
969 # define USE_GENERIC_PUSH_REGS 1
970 # define STACKBOTTOM 0x80000000
971 /* In many cases, this should probably use LINUX_STACKBOTTOM */
972 /* instead. But some kernel versions seem to give the wrong */
973 /* value from /proc. */
977 # define DATASTART (ptr_t)0x10000000
978 /* Could probably be slightly higher since */
979 /* startup code allocates lots of stuff. */
980 # define OS_TYPE "ULTRIX"
985 # define DATASTART (ptr_t)0x10000000
986 # define OS_TYPE "RISCOS"
987 # define ALIGNMENT 4 /* Required by hardware */
992 # define DATASTART ((ptr_t)(&_fdata))
994 # define HEAP_START (ptr_t)0x30000000
996 # define HEAP_START DATASTART
998 /* Lowest plausible heap address. */
999 /* In the MMAP case, we map there. */
1000 /* In either case it is used to identify */
1001 /* heap sections so they're not */
1002 /* considered as roots. */
1003 # define OS_TYPE "IRIX5"
1004 # define MPROTECT_VDB
1006 # define CPP_WORDSZ _MIPS_SZPTR
1007 # define ALIGNMENT (_MIPS_SZPTR/8)
1008 # if CPP_WORDSZ != 64
1009 # define ALIGN_DOUBLE
1012 # define ALIGNMENT 4
1013 # define ALIGN_DOUBLE
1015 # define DYNAMIC_LOADING
1020 # define MACH_TYPE "RS6000"
1021 # define ALIGNMENT 4
1022 # define DATASTART ((ptr_t)0x20000000)
1024 # define STACKBOTTOM ((ptr_t)((ulong)&errno))
1025 # define DYNAMIC_LOADING
1026 /* For really old versions of AIX, this may have to be removed. */
1030 /* OS is assumed to be HP/UX */
1031 # define MACH_TYPE "HP_PA"
1032 # define OS_TYPE "HPUX"
1034 # define CPP_WORDSZ 64
1035 # define ALIGNMENT 8
1037 # define CPP_WORDSZ 32
1038 # define ALIGNMENT 4
1039 # define ALIGN_DOUBLE
1041 extern int __data_start
;
1042 # define DATASTART ((ptr_t)(&__data_start))
1044 /* The following appears to work for 7xx systems running HP/UX */
1045 /* 9.xx Furthermore, it might result in much faster */
1046 /* collections than HEURISTIC2, which may involve scanning */
1047 /* segments that directly precede the stack. It is not the */
1048 /* default, since it may not work on older machine/OS */
1049 /* combinations. (Thanks to Raymond X.T. Nijssen for uncovering */
1051 # define STACKBOTTOM ((ptr_t) 0x7b033000) /* from /etc/conf/h/param.h */
1053 /* Gustavo Rodriguez-Rivera suggested changing HEURISTIC2 */
1054 /* to this. We'll probably do this on other platforms, too. */
1055 /* For now I'll use it where I can test it. */
1056 extern char ** environ
;
1057 # define STACKBOTTOM ((ptr_t)environ)
1059 # define STACK_GROWS_UP
1060 # define DYNAMIC_LOADING
1061 # ifndef HPUX_THREADS
1062 # define MPROTECT_VDB
1064 # include <unistd.h>
1065 # define GETPAGESIZE() sysconf(_SC_PAGE_SIZE)
1069 # define MACH_TYPE "ALPHA"
1070 # define ALIGNMENT 8
1071 # define USE_GENERIC_PUSH_REGS
1072 /* Gcc and probably the DEC/Compaq compiler spill pointers to preserved */
1073 /* fp registers in some cases when the target is a 21264. The assembly */
1074 /* code doesn't handle that yet, and version dependencies make that a */
1075 /* bit tricky. Do the easy thing for now. */
1077 # define OS_TYPE "OSF1"
1078 # define DATASTART ((ptr_t) 0x140000000)
1080 # define DATAEND ((ptr_t) &_end)
1082 /* Normally HEURISTIC2 is too conervative, since */
1083 /* the text segment immediately follows the stack. */
1084 /* Hence we give an upper pound. */
1086 # define HEURISTIC2_LIMIT ((ptr_t)((word)(&__start) & ~(getpagesize()-1)))
1087 # define CPP_WORDSZ 64
1088 # define MPROTECT_VDB
1089 # define DYNAMIC_LOADING
1092 # define OS_TYPE "LINUX"
1093 # define CPP_WORDSZ 64
1094 # define STACKBOTTOM ((ptr_t) 0x120000000)
1096 # define LINUX_DATA_START
1097 # define DYNAMIC_LOADING
1098 /* This doesn't work if the collector is in a dynamic library. */
1100 # define DATASTART ((ptr_t) 0x140000000)
1103 # define DATAEND (&_end)
1104 # define MPROTECT_VDB
1105 /* Has only been superficially tested. May not */
1106 /* work on all versions. */
1111 # define MACH_TYPE "IA64"
1112 # define ALIGN_DOUBLE
1113 /* Requires 16 byte alignment for malloc */
1114 # define ALIGNMENT 8
1115 # define USE_GENERIC_PUSH_REGS
1116 /* We need to get preserved registers in addition to register windows. */
1117 /* That's easiest to do with setjmp. */
1122 # define OS_TYPE "LINUX"
1123 # define CPP_WORDSZ 64
1124 /* This should really be done through /proc, but that */
1125 /* requires we run on an IA64 kernel. */
1126 # define STACKBOTTOM ((ptr_t) 0xa000000000000000l)
1127 /* We also need the base address of the register stack */
1128 /* backing store. There is probably a better way to */
1129 /* get that, too ... */
1130 # define BACKING_STORE_BASE ((ptr_t) 0x9fffffff80000000l)
1132 # define SEARCH_FOR_DATA_START
1133 # define DATASTART GC_data_start
1135 extern int data_start
;
1136 # define DATASTART ((ptr_t)(&data_start))
1138 # define DYNAMIC_LOADING
1139 # define MPROTECT_VDB
1140 /* Requires Linux 2.3.47 or later. */
1142 # define DATAEND (&_end)
1143 # define PREFETCH(x) \
1144 __asm__ (" lfetch [%0]": : "r"((void *)(x)))
1145 # define PREFETCH_FOR_WRITE(x) \
1146 __asm__ (" lfetch.excl [%0]": : "r"((void *)(x)))
1147 # define CLEAR_DOUBLE(x) \
1148 __asm__ (" stf.spill [%0]=f0": : "r"((void *)(x)))
1153 # define MACH_TYPE "M88K"
1154 # define ALIGNMENT 4
1155 # define ALIGN_DOUBLE
1158 # define OS_TYPE "CX_UX"
1159 # define DATASTART ((((word)&etext + 0x3fffff) & ~0x3fffff) + 0x10000)
1162 # define OS_TYPE "DGUX"
1163 extern char * GC_SysVGetDataStart();
1164 # define DATASTART (ptr_t)GC_SysVGetDataStart(0x10000, &etext)
1166 # define STACKBOTTOM ((char*)0xf0000000) /* determined empirically */
1170 # define MACH_TYPE "S370"
1171 # define OS_TYPE "UTS4"
1172 # define ALIGNMENT 4 /* Required by hardware */
1176 extern char * GC_SysVGetDataStart();
1177 # define DATASTART (ptr_t)GC_SysVGetDataStart(0x10000, &_etext)
1178 # define DATAEND (&_end)
1183 # define ALIGNMENT 4
1185 # define DATASTART ((ptr_t)(&_etext))
1190 # define CPP_WORDSZ 32
1191 # define MACH_TYPE "ARM32"
1192 # define ALIGNMENT 4
1194 # define OS_TYPE "NETBSD"
1197 # define DATASTART ((ptr_t)(&etext))
1198 # define USE_GENERIC_PUSH_REGS
1201 # define OS_TYPE "LINUX"
1204 # define STACK_GRAN 0x10000000
1205 # define USE_GENERIC_PUSH_REGS
1207 # define DYNAMIC_LOADING
1208 # include <features.h>
1209 # if defined(__GLIBC__) && __GLIBC__ >= 2
1210 # define LINUX_DATA_START
1212 extern char **__environ
;
1213 # define DATASTART ((ptr_t)(&__environ))
1214 /* hideous kludge: __environ is the first */
1215 /* word in crt0.o, and delimits the start */
1216 /* of the data segment, no matter which */
1217 /* ld options were passed through. */
1218 /* We could use _etext instead, but that */
1219 /* would include .rodata, which may */
1220 /* contain large read-only data tables */
1221 /* that we'd rather not scan. */
1224 # define DATAEND (&_end)
1227 # define DATASTART ((ptr_t)((((word) (&etext)) + 0xfff) & ~0xfff))
1232 #ifdef LINUX_DATA_START
1233 /* Some Linux distributions arrange to define __data_start. Some */
1234 /* define data_start as a weak symbol. The latter is technically */
1235 /* broken, since the user program may define data_start, in which */
1236 /* case we lose. Nonetheless, we try both, prefering __data_start. */
1237 /* We assume gcc. */
1238 # pragma weak __data_start
1239 extern int __data_start
;
1240 # pragma weak data_start
1241 extern int data_start
;
1242 # define DATASTART ((ptr_t)(&__data_start != 0? &__data_start : &data_start))
1245 # ifndef STACK_GROWS_UP
1246 # define STACK_GROWS_DOWN
1250 # define CPP_WORDSZ 32
1259 # define DATAEND (&end)
1262 # if defined(SVR4) && !defined(GETPAGESIZE)
1263 # include <unistd.h>
1264 # define GETPAGESIZE() sysconf(_SC_PAGESIZE)
1267 # ifndef GETPAGESIZE
1268 # if defined(SUNOS5) || defined(IRIX5)
1269 # include <unistd.h>
1271 # define GETPAGESIZE() getpagesize()
1274 # if defined(SUNOS5) || defined(DRSNX) || defined(UTS4)
1275 /* OS has SVR4 generic features. Probably others also qualify. */
1279 # if defined(SUNOS5) || defined(DRSNX)
1280 /* OS has SUNOS5 style semi-undocumented interface to dynamic */
1283 /* OS has SUNOS5 style signal handlers. */
1291 # if CPP_WORDSZ != 32 && CPP_WORDSZ != 64
1296 # undef DYNAMIC_LOADING
1301 # undef MPROTECT_VDB
1306 /* Postponed for now. */
1308 # undef MPROTECT_VDB
1311 # ifdef SMALL_CONFIG
1312 /* Presumably not worth the space it takes. */
1314 # undef MPROTECT_VDB
1318 # undef MPROTECT_VDB /* Can't deal with address space holes. */
1321 # if !defined(PCR_VDB) && !defined(PROC_VDB) && !defined(MPROTECT_VDB)
1322 # define DEFAULT_VDB
1326 # define PREFETCH(x)
1327 # define NO_PREFETCH
1330 # ifndef PREFETCH_FOR_WRITE
1331 # define PREFETCH_FOR_WRITE(x)
1332 # define NO_PREFETCH_FOR_WRITE
1335 # ifndef CACHE_LINE_SIZE
1336 # define CACHE_LINE_SIZE 32 /* Wild guess */
1339 # ifndef CLEAR_DOUBLE
1340 # define CLEAR_DOUBLE(x) \
1341 ((word*)x)[0] = 0; \
1343 # endif /* CLEAR_DOUBLE */
1345 # if defined(_SOLARIS_PTHREADS) && !defined(SOLARIS_THREADS)
1346 # define SOLARIS_THREADS
1348 # if defined(IRIX_THREADS) && !defined(IRIX5)
1349 --> inconsistent configuration
1351 # if defined(IRIX_JDK_THREADS) && !defined(IRIX5)
1352 --> inconsistent configuration
1354 # if defined(LINUX_THREADS) && !defined(LINUX)
1355 --> inconsistent configuration
1357 # if defined(SOLARIS_THREADS) && !defined(SUNOS5)
1358 --> inconsistent configuration
1360 # if defined(HPUX_THREADS) && !defined(HPUX)
1361 --> inconsistent configuration
1363 # if defined(PCR) || defined(SRC_M3) || \
1364 defined(SOLARIS_THREADS) || defined(WIN32_THREADS) || \
1365 defined(IRIX_THREADS) || defined(LINUX_THREADS) || \
1366 defined(IRIX_JDK_THREADS) || defined(HPUX_THREADS)
1370 # if defined(HP_PA) || defined(M88K) || defined(POWERPC) \
1371 || (defined(I386) && defined(OS2)) || defined(UTS4) || defined(LINT)
1372 /* Use setjmp based hack to mark from callee-save registers. */
1373 # define USE_GENERIC_PUSH_REGS
1375 # if defined(SPARC) && !defined(LINUX)
1376 # define SAVE_CALL_CHAIN
1377 # define ASM_CLEAR_CODE /* Stack clearing is crucial, and we */
1378 /* include assembly code to do it well. */
1381 # if defined(LINUX) && !defined(POWERPC)
1384 # include <linux/version.h>
1385 # if (LINUX_VERSION_CODE <= 0x10400)
1386 /* Ugly hack to get struct sigcontext_struct definition. Required */
1387 /* for some early 1.3.X releases. Will hopefully go away soon. */
1388 /* in some later Linux releases, asm/sigcontext.h may have to */
1389 /* be included instead. */
1391 # include <asm/signal.h>
1397 /* Kernels prior to 2.1.1 defined struct sigcontext_struct instead of */
1398 /* struct sigcontext. libc6 (glibc2) uses "struct sigcontext" in */
1399 /* prototypes, so we have to include the top-level sigcontext.h to */
1400 /* make sure the former gets defined to be the latter if appropriate. */
1401 # include <features.h>
1403 # if 2 == __GLIBC__ && 0 == __GLIBC_MINOR__
1404 /* glibc 2.1 no longer has sigcontext.h. But signal.h */
1405 /* has the right declaration for glibc 2.1. */
1406 # include <sigcontext.h>
1407 # endif /* 0 == __GLIBC_MINOR__ */
1408 # else /* not 2 <= __GLIBC__ */
1409 /* libc5 doesn't have <sigcontext.h>: go directly with the kernel */
1410 /* one. Check LINUX_VERSION_CODE to see which we should reference. */
1411 # include <asm/sigcontext.h>
1412 # endif /* 2 <= __GLIBC__ */
1415 # if !defined(OS2) && !defined(PCR) && !defined(AMIGA) && !defined(MACOS)
1416 # include <sys/types.h>
1417 # if !defined(MSWIN32) && !defined(SUNOS4)
1418 # include <unistd.h>
1422 # include <signal.h>
1424 /* Blatantly OS dependent routines, except for those that are related */
1425 /* to dynamic loading. */
1427 # if !defined(THREADS) && !defined(STACKBOTTOM) && defined(HEURISTIC2)
1428 # define NEED_FIND_LIMIT
1431 # if defined(IRIX_THREADS) || defined(HPUX_THREADS)
1432 # define NEED_FIND_LIMIT
1435 # if (defined(SUNOS4) && defined(DYNAMIC_LOADING)) && !defined(PCR)
1436 # define NEED_FIND_LIMIT
1439 # if (defined(SVR4) || defined(AUX) || defined(DGUX)) && !defined(PCR)
1440 # define NEED_FIND_LIMIT
1443 # if defined(LINUX) && \
1444 (defined(POWERPC) || defined(SPARC) || defined(ALPHA) || defined(IA64) \
1446 # define NEED_FIND_LIMIT
1449 #ifdef NEED_FIND_LIMIT
1450 # include <setjmp.h>
1454 # include <machine/trap.h>
1458 # include <proto/exec.h>
1459 # include <proto/dos.h>
1460 # include <dos/dosextens.h>
1461 # include <workbench/startup.h>
1465 # define WIN32_LEAN_AND_MEAN
1467 # include <windows.h>
1471 # include <Processes.h>
1475 # include <sys/uio.h>
1476 # include <malloc.h> /* for locking */
1479 # include <sys/types.h>
1480 # include <sys/mman.h>
1481 # include <sys/stat.h>
1486 # include <sys/siginfo.h>
1489 # define setjmp(env) sigsetjmp(env, 1)
1490 # define longjmp(env, val) siglongjmp(env, val)
1491 # define jmp_buf sigjmp_buf
1495 /* Apparently necessary for djgpp 2.01. May casuse problems with */
1496 /* other versions. */
1497 typedef long unsigned int caddr_t
;
1501 # include "il/PCR_IL.h"
1502 # include "th/PCR_ThCtl.h"
1503 # include "mm/PCR_MM.h"
1506 #if !defined(NO_EXECUTE_PERMISSION)
1507 # define OPT_PROT_EXEC PROT_EXEC
1509 # define OPT_PROT_EXEC 0
1514 # include <stddef.h>
1516 # if !defined(__IBMC__) && !defined(__WATCOMC__) /* e.g. EMX */
1518 # else /* IBM's compiler */
1520 /* A kludge to get around what appears to be a header file bug */
1522 # define WORD unsigned short
1525 # define DWORD unsigned long
1529 # include <newexe.h>
1530 # include <exe386.h>
1532 # endif /* __IBMC__ */
1534 # define INCL_DOSEXCEPTIONS
1535 # define INCL_DOSPROCESS
1536 # define INCL_DOSERRORS
1537 # define INCL_DOSMODULEMGR
1538 # define INCL_DOSMEMMGR
1544 * Find the base of the stack.
1545 * Used only in single-threaded environment.
1546 * With threads, GC_mark_roots needs to know how to do this.
1547 * Called with allocator lock held.
1550 # define is_writable(prot) ((prot) == PAGE_READWRITE \
1551 || (prot) == PAGE_WRITECOPY \
1552 || (prot) == PAGE_EXECUTE_READWRITE \
1553 || (prot) == PAGE_EXECUTE_WRITECOPY)
1554 /* Return the number of bytes that are writable starting at p. */
1555 /* The pointer p is assumed to be page aligned. */
1556 /* If base is not 0, *base becomes the beginning of the */
1557 /* allocation region containing p. */
1558 static word
GC_get_writable_length(ptr_t p
, ptr_t
*base
)
1560 MEMORY_BASIC_INFORMATION buf
;
1564 result
= VirtualQuery(p
, &buf
, sizeof(buf
));
1565 if (result
!= sizeof(buf
)) ABORT("Weird VirtualQuery result");
1566 if (base
!= 0) *base
= (ptr_t
)(buf
.AllocationBase
);
1567 protect
= (buf
.Protect
& ~(PAGE_GUARD
| PAGE_NOCACHE
));
1568 if (!is_writable(protect
)) {
1571 if (buf
.State
!= MEM_COMMIT
) return(0);
1572 return(buf
.RegionSize
);
1575 void *scm_get_stack_base()
1578 ptr_t sp
= (ptr_t
)(&dummy
);
1581 static word GC_page_size
= 0;
1582 if (!GC_page_size
) {
1583 SYSTEM_INFO sysinfo
;
1584 GetSystemInfo(&sysinfo
);
1585 GC_page_size
= sysinfo
.dwPageSize
;
1587 trunc_sp
= (ptr_t
)((word
)sp
& ~(GC_page_size
- 1));
1588 size
= GC_get_writable_length(trunc_sp
, 0);
1589 return(trunc_sp
+ size
);
1597 void *scm_get_stack_base()
1602 if (DosGetInfoBlocks(&ptib
, &ppib
) != NO_ERROR
) {
1603 GC_err_printf0("DosGetInfoBlocks failed\n");
1604 ABORT("DosGetInfoBlocks failed\n");
1606 return((ptr_t
)(ptib
-> tib_pstacklimit
));
1613 void *scm_get_stack_base()
1615 struct Process
*proc
= (struct Process
*)SysBase
->ThisTask
;
1617 /* Reference: Amiga Guru Book Pages: 42,567,574 */
1618 if (proc
->pr_Task
.tc_Node
.ln_Type
==NT_PROCESS
1619 && proc
->pr_CLI
!= NULL
) {
1620 /* first ULONG is StackSize */
1621 /*longPtr = proc->pr_ReturnAddr;
1622 size = longPtr[0];*/
1624 return (char *)proc
->pr_ReturnAddr
+ sizeof(ULONG
);
1626 return (char *)proc
->pr_Task
.tc_SPUpper
;
1630 #if 0 /* old version */
1631 void *scm_get_stack_base()
1633 extern struct WBStartup
*_WBenchMsg
;
1635 extern long __stack
;
1637 struct Process
*proc
;
1638 struct CommandLineInterface
*cli
;
1641 if ((task
= FindTask(0)) == 0) {
1642 GC_err_puts("Cannot find own task structure\n");
1643 ABORT("task missing");
1645 proc
= (struct Process
*)task
;
1646 cli
= BADDR(proc
->pr_CLI
);
1648 if (_WBenchMsg
!= 0 || cli
== 0) {
1649 size
= (char *)task
->tc_SPUpper
- (char *)task
->tc_SPLower
;
1651 size
= cli
->cli_DefaultStack
* 4;
1653 return (ptr_t
)(__base
+ GC_max(size
, __stack
));
1657 # else /* !AMIGA, !OS2, ... */
1659 # ifdef NEED_FIND_LIMIT
1660 /* Some tools to implement HEURISTIC2 */
1661 # define MIN_PAGE_SIZE 256 /* Smallest conceivable page size, bytes */
1662 /* static */ jmp_buf GC_jmp_buf
;
1665 static void GC_fault_handler(sig
)
1668 longjmp(GC_jmp_buf
, 1);
1672 typedef void (*handler
)(int);
1674 typedef void (*handler
)();
1677 # if defined(SUNOS5SIGS) || defined(IRIX5) || defined(OSF1)
1678 static struct sigaction old_segv_act
;
1679 # if defined(_sigargs) || defined(HPUX) /* !Irix6.x */
1680 static struct sigaction old_bus_act
;
1683 static handler old_segv_handler
, old_bus_handler
;
1686 static void GC_setup_temporary_fault_handler()
1688 # if defined(SUNOS5SIGS) || defined(IRIX5) || defined(OSF1)
1689 struct sigaction act
;
1691 act
.sa_handler
= GC_fault_handler
;
1692 act
.sa_flags
= SA_RESTART
| SA_NODEFER
;
1693 /* The presence of SA_NODEFER represents yet another gross */
1694 /* hack. Under Solaris 2.3, siglongjmp doesn't appear to */
1695 /* interact correctly with -lthread. We hide the confusion */
1696 /* by making sure that signal handling doesn't affect the */
1699 (void) sigemptyset(&act
.sa_mask
);
1700 # ifdef IRIX_THREADS
1701 /* Older versions have a bug related to retrieving and */
1702 /* and setting a handler at the same time. */
1703 (void) sigaction(SIGSEGV
, 0, &old_segv_act
);
1704 (void) sigaction(SIGSEGV
, &act
, 0);
1706 (void) sigaction(SIGSEGV
, &act
, &old_segv_act
);
1707 # if defined(IRIX5) && defined(_sigargs) /* Irix 5.x, not 6.x */ \
1709 /* Under Irix 5.x or HP/UX, we may get SIGBUS. */
1710 /* Pthreads doesn't exist under Irix 5.x, so we */
1711 /* don't have to worry in the threads case. */
1712 (void) sigaction(SIGBUS
, &act
, &old_bus_act
);
1714 # endif /* IRIX_THREADS */
1716 old_segv_handler
= signal(SIGSEGV
, GC_fault_handler
);
1718 old_bus_handler
= signal(SIGBUS
, GC_fault_handler
);
1723 static void GC_reset_fault_handler()
1725 # if defined(SUNOS5SIGS) || defined(IRIX5) || defined(OSF1)
1726 (void) sigaction(SIGSEGV
, &old_segv_act
, 0);
1727 # if defined(IRIX5) && defined(_sigargs) /* Irix 5.x, not 6.x */ \
1729 (void) sigaction(SIGBUS
, &old_bus_act
, 0);
1732 (void) signal(SIGSEGV
, old_segv_handler
);
1734 (void) signal(SIGBUS
, old_bus_handler
);
1739 /* Return the first nonaddressible location > p (up) or */
1740 /* the smallest location q s.t. [q,p] is addressible (!up). */
1741 static ptr_t
GC_find_limit(p
, up
)
1745 static VOLATILE ptr_t result
;
1746 /* Needs to be static, since otherwise it may not be */
1747 /* preserved across the longjmp. Can safely be */
1748 /* static since it's only called once, with the */
1749 /* allocation lock held. */
1752 GC_setup_temporary_fault_handler();
1753 if (setjmp(GC_jmp_buf
) == 0) {
1754 result
= (ptr_t
)(((word
)(p
))
1755 & ~(MIN_PAGE_SIZE
-1));
1758 result
+= MIN_PAGE_SIZE
;
1760 result
-= MIN_PAGE_SIZE
;
1762 GC_noop1((word
)(*result
));
1765 GC_reset_fault_handler();
1767 result
+= MIN_PAGE_SIZE
;
1773 #ifdef LINUX_STACKBOTTOM
1775 #include <sys/types.h>
1776 #include <sys/stat.h>
1779 # define STAT_SKIP 27 /* Number of fields preceding startstack */
1780 /* field in /proc/self/stat */
1782 static ptr_t
GC_linux_stack_base(void)
1784 /* We read the stack base value from /proc/self/stat. We do this */
1785 /* using direct I/O system calls in order to avoid calling malloc */
1786 /* in case REDIRECT_MALLOC is defined. */
1787 # define STAT_BUF_SIZE 4096
1789 # define STAT_READ __real_read
1791 # define STAT_READ read
1793 char stat_buf
[STAT_BUF_SIZE
];
1797 size_t i
, buf_offset
= 0;
1799 f
= open("/proc/self/stat", O_RDONLY
);
1800 if (f
< 0 || STAT_READ(f
, stat_buf
, STAT_BUF_SIZE
) < 2 * STAT_SKIP
) {
1801 ABORT("Couldn't read /proc/self/stat");
1803 c
= stat_buf
[buf_offset
++];
1804 /* Skip the required number of fields. This number is hopefully */
1805 /* constant across all Linux implementations. */
1806 for (i
= 0; i
< STAT_SKIP
; ++i
) {
1807 while (isspace(c
)) c
= stat_buf
[buf_offset
++];
1808 while (!isspace(c
)) c
= stat_buf
[buf_offset
++];
1810 while (isspace(c
)) c
= stat_buf
[buf_offset
++];
1811 while (isdigit(c
)) {
1814 c
= stat_buf
[buf_offset
++];
1817 if (result
< 0x10000000) ABORT("Absurd stack bottom value");
1818 return (ptr_t
)result
;
1821 #endif /* LINUX_STACKBOTTOM */
1823 void *scm_get_stack_base()
1826 void *result
= &dummy
; /* initialize to silence compiler */
1828 # define STACKBOTTOM_ALIGNMENT_M1 ((word)STACK_GRAN - 1)
1831 return(STACKBOTTOM
);
1834 # ifdef STACK_GROWS_DOWN
1835 result
= (ptr_t
)((((word
)(&dummy
))
1836 + STACKBOTTOM_ALIGNMENT_M1
)
1837 & ~STACKBOTTOM_ALIGNMENT_M1
);
1839 result
= (ptr_t
)(((word
)(&dummy
))
1840 & ~STACKBOTTOM_ALIGNMENT_M1
);
1842 # endif /* HEURISTIC1 */
1843 # ifdef LINUX_STACKBOTTOM
1844 result
= GC_linux_stack_base();
1847 # ifdef STACK_GROWS_DOWN
1848 result
= GC_find_limit((ptr_t
)(&dummy
), TRUE
);
1849 # ifdef HEURISTIC2_LIMIT
1850 if (result
> HEURISTIC2_LIMIT
1851 && (ptr_t
)(&dummy
) < HEURISTIC2_LIMIT
) {
1852 result
= HEURISTIC2_LIMIT
;
1856 result
= GC_find_limit((ptr_t
)(&dummy
), FALSE
);
1857 # ifdef HEURISTIC2_LIMIT
1858 if (result
< HEURISTIC2_LIMIT
1859 && (ptr_t
)(&dummy
) > HEURISTIC2_LIMIT
) {
1860 result
= HEURISTIC2_LIMIT
;
1865 # endif /* HEURISTIC2 */
1866 # ifdef STACK_GROWS_DOWN
1867 if (result
== 0) result
= (ptr_t
)(signed_word
)(-sizeof(ptr_t
));
1870 # endif /* STACKBOTTOM */
1873 # endif /* ! AMIGA */
1875 # endif /* ! MSWIN32 */