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 #if 0 /* currently unused (as of 2001-07-12) */
67 /* Single argument version, robust against whole program analysis. */
72 static VOLATILE word sink
;
79 /* Machine dependent parameters. Some tuning parameters can be found */
80 /* near the top of gc_private.h. */
82 /* Machine specific parts contributed by various people. See README file. */
84 /* First a unified test for Linux: */
85 # if defined(linux) || defined(__linux__)
89 /* Determine the machine type: */
90 # if defined(sun) && defined(mc68000)
93 # define mach_type_known
95 # if defined(hp9000s300)
98 # define mach_type_known
100 # if defined(__OpenBSD__) && defined(m68k)
103 # define mach_type_known
105 # if defined(__OpenBSD__) && defined(__sparc__)
108 # define mach_type_known
110 # if defined(__NetBSD__) && defined(m68k)
113 # define mach_type_known
115 # if defined(__NetBSD__) && defined(arm32)
118 # define mach_type_known
127 # define mach_type_known
129 # if defined(mips) || defined(__mips)
132 # if defined(ultrix) || defined(__ultrix) || defined(__NetBSD__)
135 # if defined(_SYSTYPE_SVR4) || defined(SYSTYPE_SVR4) \
136 || defined(__SYSTYPE_SVR4__)
137 # define IRIX5 /* or IRIX 6.X */
139 # define RISCOS /* or IRIX 4.X */
143 # define mach_type_known
145 # if defined(sequent) && defined(i386)
148 # define mach_type_known
150 # if defined(sun) && defined(i386)
153 # define mach_type_known
155 # if (defined(__OS2__) || defined(__EMX__)) && defined(__32BIT__)
158 # define mach_type_known
162 # define mach_type_known
164 # if defined(sun) && (defined(sparc) || defined(__sparc))
166 /* Test for SunOS 5.x */
173 # define mach_type_known
175 # if defined(sparc) && defined(unix) && !defined(sun) && !defined(linux) \
176 && !defined(__OpenBSD__)
179 # define mach_type_known
183 # define mach_type_known
185 # if defined(_M_XENIX) && defined(_M_SYSV) && defined(_M_I386)
186 /* The above test may need refinement */
188 # if defined(_SCO_ELF)
193 # define mach_type_known
195 # if defined(_AUX_SOURCE)
198 # define mach_type_known
200 # if defined(_PA_RISC1_0) || defined(_PA_RISC1_1) || defined(_PA_RISC2_0) \
201 || defined(hppa) || defined(__hppa__)
206 # define mach_type_known
208 # if defined(LINUX) && (defined(i386) || defined(__i386__))
210 # define mach_type_known
212 # if defined(LINUX) && (defined(__ia64__) || defined(__ia64))
214 # define mach_type_known
216 # if defined(LINUX) && defined(powerpc)
218 # define mach_type_known
220 # if defined(LINUX) && defined(__mc68000__)
222 # define mach_type_known
224 # if defined(LINUX) && (defined(sparc) || defined(__sparc__))
226 # define mach_type_known
228 # if defined(LINUX) && defined(arm)
230 # define mach_type_known
232 # if defined(__alpha) || defined(__alpha__)
235 # define OSF1 /* a.k.a Digital Unix */
237 # define mach_type_known
239 # if defined(_AMIGA) && !defined(AMIGA)
244 # define mach_type_known
246 # if defined(THINK_C) || defined(__MWERKS__) && !defined(__powerc)
249 # define mach_type_known
251 # if defined(__MWERKS__) && defined(__powerc)
254 # define mach_type_known
259 # define mach_type_known
261 # if defined(NeXT) && defined(mc68000)
264 # define mach_type_known
266 # if defined(NeXT) && defined(i386)
269 # define mach_type_known
271 # if defined(__OpenBSD__) && defined(i386)
274 # define mach_type_known
276 # if defined(__FreeBSD__) && defined(i386)
279 # define mach_type_known
281 # if defined(__NetBSD__) && defined(i386)
284 # define mach_type_known
286 # if defined(bsdi) && defined(i386)
289 # define mach_type_known
291 # if !defined(mach_type_known) && defined(__386BSD__)
294 # define mach_type_known
296 # if defined(_CX_UX) && defined(_M88K)
299 # define mach_type_known
304 # define mach_type_known
306 # if (defined(_MSDOS) || defined(_MSC_VER)) && (_M_IX86 >= 300) \
307 || defined(_WIN32) && !defined(__CYGWIN32__) && !defined(__CYGWIN__)
309 # define MSWIN32 /* or Win32s */
310 # define mach_type_known
312 # if defined(__DJGPP__)
315 # define DJGPP /* MSDOS running the DJGPP port of GCC */
317 # define mach_type_known
319 # if defined(__CYGWIN32__) || defined(__CYGWIN__)
322 # define mach_type_known
324 # if defined(__MINGW32__)
327 # define mach_type_known
329 # if defined(__BORLANDC__)
332 # define mach_type_known
334 # if defined(_UTS) && !defined(mach_type_known)
337 # define mach_type_known
341 # define mach_type_known
344 # if defined(__WATCOMC__) && defined(__386__)
346 # if !defined(OS2) && !defined(MSWIN32) && !defined(DOS4GW)
347 # if defined(__OS2__)
350 # if defined(__WINDOWS_386__) || defined(__NT__)
357 # define mach_type_known
360 /* Feel free to add more clauses here */
362 /* Or manually define the machine type here. A machine type is */
363 /* characterized by the architecture. Some */
364 /* machine types are further subdivided by OS. */
365 /* the macros ULTRIX, RISCOS, and BSD to distinguish. */
366 /* Note that SGI IRIX is treated identically to RISCOS. */
367 /* SYSV on an M68K actually means A/UX. */
368 /* The distinction in these cases is usually the stack starting address */
369 # ifndef mach_type_known
370 --> unknown machine type
372 /* Mapping is: M68K ==> Motorola 680X0 */
373 /* (SUNOS4,HP,NEXT, and SYSV (A/UX), */
374 /* MACOS and AMIGA variants) */
375 /* I386 ==> Intel 386 */
376 /* (SEQUENT, OS2, SCO, LINUX, NETBSD, */
377 /* FREEBSD, THREE86BSD, MSWIN32, */
378 /* BSDI,SUNOS5, NEXT, other variants) */
379 /* NS32K ==> Encore Multimax */
380 /* MIPS ==> R2000 or R3000 */
381 /* (RISCOS, ULTRIX variants) */
382 /* VAX ==> DEC VAX */
383 /* (BSD, ULTRIX variants) */
384 /* RS6000 ==> IBM RS/6000 AIX3.X */
385 /* RT ==> IBM PC/RT */
386 /* HP_PA ==> HP9000/700 & /800 */
388 /* SPARC ==> SPARC under SunOS */
389 /* (SUNOS4, SUNOS5, */
390 /* DRSNX variants) */
391 /* ALPHA ==> DEC Alpha */
392 /* (OSF1 and LINUX variants) */
393 /* M88K ==> Motorola 88XX0 */
394 /* (CX_UX and DGUX) */
395 /* S370 ==> 370-like machine */
396 /* running Amdahl UTS4 */
397 /* ARM32 ==> Intel StrongARM */
398 /* IA64 ==> Intel IA64 */
403 * For each architecture and OS, the following need to be defined:
405 * CPP_WORD_SZ is a simple integer constant representing the word size.
406 * in bits. We assume byte addressibility, where a byte has 8 bits.
407 * We also assume CPP_WORD_SZ is either 32 or 64.
408 * (We care about the length of pointers, not hardware
409 * bus widths. Thus a 64 bit processor with a C compiler that uses
410 * 32 bit pointers should use CPP_WORD_SZ of 32, not 64. Default is 32.)
412 * MACH_TYPE is a string representation of the machine type.
413 * OS_TYPE is analogous for the OS.
415 * ALIGNMENT is the largest N, such that
416 * all pointer are guaranteed to be aligned on N byte boundaries.
417 * defining it to be 1 will always work, but perform poorly.
419 * DATASTART is the beginning of the data segment.
420 * On UNIX systems, the collector will scan the area between DATASTART
421 * and DATAEND for root pointers.
423 * DATAEND, if not &end.
425 * ALIGN_DOUBLE of GC_malloc should return blocks aligned to twice
428 * STACKBOTTOM is the cool end of the stack, which is usually the
429 * highest address in the stack.
430 * Under PCR or OS/2, we have other ways of finding thread stacks.
431 * For each machine, the following should:
432 * 1) define STACK_GROWS_UP if the stack grows toward higher addresses, and
433 * 2) define exactly one of
434 * STACKBOTTOM (should be defined to be an expression)
437 * If either of the last two macros are defined, then STACKBOTTOM is computed
438 * during collector startup using one of the following two heuristics:
439 * HEURISTIC1: Take an address inside GC_init's frame, and round it up to
440 * the next multiple of STACK_GRAN.
441 * HEURISTIC2: Take an address inside GC_init's frame, increment it repeatedly
442 * in small steps (decrement if STACK_GROWS_UP), and read the value
443 * at each location. Remember the value when the first
444 * Segmentation violation or Bus error is signalled. Round that
445 * to the nearest plausible page boundary, and use that instead
448 * Gustavo Rodriguez-Rivera points out that on most (all?) Unix machines,
449 * the value of environ is a pointer that can serve as STACKBOTTOM.
450 * I expect that HEURISTIC2 can be replaced by this approach, which
451 * interferes far less with debugging.
453 * If no expression for STACKBOTTOM can be found, and neither of the above
454 * heuristics are usable, the collector can still be used with all of the above
455 * undefined, provided one of the following is done:
456 * 1) GC_mark_roots can be changed to somehow mark from the correct stack(s)
457 * without reference to STACKBOTTOM. This is appropriate for use in
458 * conjunction with thread packages, since there will be multiple stacks.
459 * (Allocating thread stacks in the heap, and treating them as ordinary
460 * heap data objects is also possible as a last resort. However, this is
461 * likely to introduce significant amounts of excess storage retention
462 * unless the dead parts of the thread stacks are periodically cleared.)
463 * 2) Client code may set GC_stackbottom before calling any GC_ routines.
464 * If the author of the client code controls the main program, this is
465 * easily accomplished by introducing a new main program, setting
466 * GC_stackbottom to the address of a local variable, and then calling
467 * the original main program. The new main program would read something
470 * # include "gc_private.h"
472 * main(argc, argv, envp)
474 * char **argv, **envp;
478 * GC_stackbottom = (ptr_t)(&dummy);
479 * return(real_main(argc, argv, envp));
483 * Each architecture may also define the style of virtual dirty bit
484 * implementation to be used:
485 * MPROTECT_VDB: Write protect the heap and catch faults.
486 * PROC_VDB: Use the SVR4 /proc primitives to read dirty bits.
488 * An architecture may define DYNAMIC_LOADING if dynamic_load.c
489 * defined GC_register_dynamic_libraries() for the architecture.
491 * An architecture may define PREFETCH(x) to preload the cache with *x.
492 * This defaults to a no-op.
494 * PREFETCH_FOR_WRITE(x) is used if *x is about to be written.
496 * An architecture may also define CLEAR_DOUBLE(x) to be a fast way to
497 * clear the two words at GC_malloc-aligned address x. By default,
498 * word stores of 0 are used instead.
502 # define STACK_GRAN 0x1000000
504 # define MACH_TYPE "M68K"
507 # define OS_TYPE "OPENBSD"
510 # define DATASTART ((ptr_t)(&etext))
513 # define OS_TYPE "NETBSD"
516 # define DATASTART ((ptr_t)(&etext))
519 # define OS_TYPE "LINUX"
520 # define STACKBOTTOM ((ptr_t)0xf0000000)
521 # define MPROTECT_VDB
523 # define DYNAMIC_LOADING
524 extern char **__environ
;
525 # define DATASTART ((ptr_t)(&__environ))
526 /* hideous kludge: __environ is the first */
527 /* word in crt0.o, and delimits the start */
528 /* of the data segment, no matter which */
529 /* ld options were passed through. */
530 /* We could use _etext instead, but that */
531 /* would include .rodata, which may */
532 /* contain large read-only data tables */
533 /* that we'd rather not scan. */
535 # define DATAEND (&_end)
538 # define DATASTART ((ptr_t)((((word) (&etext)) + 0xfff) & ~0xfff))
542 # define OS_TYPE "SUNOS4"
544 # define DATASTART ((ptr_t)((((word) (&etext)) + 0x1ffff) & ~0x1ffff))
545 # define HEURISTIC1 /* differs */
546 # define DYNAMIC_LOADING
549 # define OS_TYPE "HP"
551 # define DATASTART ((ptr_t)((((word) (&etext)) + 0xfff) & ~0xfff))
552 # define STACKBOTTOM ((ptr_t) 0xffeffffc)
553 /* empirically determined. seems to work. */
555 # define GETPAGESIZE() sysconf(_SC_PAGE_SIZE)
558 # define OS_TYPE "SYSV"
560 # define DATASTART ((ptr_t)((((word) (&etext)) + 0x3fffff) \
562 +((word)&etext & 0x1fff))
563 /* This only works for shared-text binaries with magic number 0413.
564 The other sorts of SysV binaries put the data at the end of the text,
565 in which case the default of &etext would work. Unfortunately,
566 handling both would require having the magic-number available.
569 # define STACKBOTTOM ((ptr_t)0xFFFFFFFE)
570 /* The stack starts at the top of memory, but */
571 /* 0x0 cannot be used as setjump_test complains */
572 /* that the stack direction is incorrect. Two */
573 /* bytes down from 0x0 should be safe enough. */
575 # include <sys/mmu.h>
576 # define GETPAGESIZE() PAGESIZE /* Is this still right? */
579 # define OS_TYPE "AMIGA"
580 /* STACKBOTTOM and DATASTART handled specially */
582 # define DATAEND /* not needed */
583 # define GETPAGESIZE() 4096
589 # define OS_TYPE "MACOS"
590 /* see os_dep.c for details of global data segments. */
591 # define STACKBOTTOM ((ptr_t) LMGetCurStackBase())
592 # define DATAEND /* not needed */
593 # define GETPAGESIZE() 4096
596 # define OS_TYPE "NEXT"
597 # define DATASTART ((ptr_t) get_etext())
598 # define STACKBOTTOM ((ptr_t) 0x4000000)
599 # define DATAEND /* not needed */
604 # define MACH_TYPE "POWERPC"
606 # define ALIGNMENT 2 /* Still necessary? Could it be 4? */
610 # define OS_TYPE "MACOS"
611 /* see os_dep.c for details of global data segments. */
612 # define STACKBOTTOM ((ptr_t) LMGetCurStackBase())
613 # define DATAEND /* not needed */
616 # define ALIGNMENT 4 /* Guess. Can someone verify? */
617 /* This was 2, but that didn't sound right. */
618 # define OS_TYPE "LINUX"
620 # define DYNAMIC_LOADING
622 # define STACK_GRAN 0x10000000
623 /* Stack usually starts at 0x80000000 */
624 # define LINUX_DATA_START
626 # define DATAEND (&_end)
630 # define OS_TYPE "MACOSX"
631 # define DATASTART ((ptr_t) get_etext())
632 # define STACKBOTTOM ((ptr_t) 0xc0000000)
633 # define DATAEND /* not needed */
638 # define MACH_TYPE "VAX"
639 # define ALIGNMENT 4 /* Pointers are longword aligned by 4.2 C compiler */
641 # define DATASTART ((ptr_t)(&etext))
643 # define OS_TYPE "BSD"
645 /* HEURISTIC2 may be OK, but it's hard to test. */
648 # define OS_TYPE "ULTRIX"
649 # define STACKBOTTOM ((ptr_t) 0x7fffc800)
654 # define MACH_TYPE "RT"
656 # define DATASTART ((ptr_t) 0x10000000)
657 # define STACKBOTTOM ((ptr_t) 0x1fffd800)
661 # define MACH_TYPE "SPARC"
662 # define ALIGNMENT 4 /* Required by hardware */
663 # define ALIGN_DOUBLE
666 # define OS_TYPE "SUNOS5"
669 extern char * GC_SysVGetDataStart();
670 # define DATASTART (ptr_t)GC_SysVGetDataStart(0x10000, &_etext)
671 # define DATAEND (&_end)
676 # define HEAP_START (ptr_t)0x40000000
678 # define HEAP_START DATAEND
681 /* HEURISTIC1 reportedly no longer works under 2.7. Thus we */
682 /* switched to HEURISTIC2, eventhough it creates some debugging */
686 # define GETPAGESIZE() sysconf(_SC_PAGESIZE)
687 /* getpagesize() appeared to be missing from at least one */
688 /* Solaris 5.4 installation. Weird. */
689 # define DYNAMIC_LOADING
692 # define OS_TYPE "SUNOS4"
693 /* [If you have a weak stomach, don't read this.] */
694 /* We would like to use: */
695 /* # define DATASTART ((ptr_t)((((word) (&etext)) + 0x1fff) & ~0x1fff)) */
696 /* This fails occasionally, due to an ancient, but very */
697 /* persistent ld bug. &etext is set 32 bytes too high. */
698 /* We instead read the text segment size from the a.out */
699 /* header, which happens to be mapped into our address space */
700 /* at the start of the text segment. The detective work here */
701 /* was done by Robert Ehrlich, Manuel Serrano, and Bernard */
702 /* Serpette of INRIA. */
703 /* This assumes ZMAGIC, i.e. demand-loadable executables. */
704 # define TEXTSTART 0x2000
705 # define DATASTART ((ptr_t)(*(int *)(TEXTSTART+0x4)+TEXTSTART))
706 # define MPROTECT_VDB
708 # define DYNAMIC_LOADING
711 # define CPP_WORDSZ 32
712 # define OS_TYPE "DRSNX"
713 extern char * GC_SysVGetDataStart();
715 # define DATASTART (ptr_t)GC_SysVGetDataStart(0x10000, &etext)
716 # define MPROTECT_VDB
717 # define STACKBOTTOM ((ptr_t) 0xdfff0000)
718 # define DYNAMIC_LOADING
721 # define OS_TYPE "LINUX"
723 # define LINUX_DATA_START
724 # define DYNAMIC_LOADING
726 Linux Sparc non elf
?
729 # define DATAEND (&_end)
731 # define STACKBOTTOM ((ptr_t) 0xf0000000)
734 # define OS_TYPE "OPENBSD"
735 # define STACKBOTTOM ((ptr_t) 0xf8000000)
736 # define DATASTART ((ptr_t)(&etext))
741 # define MACH_TYPE "I386"
742 # define ALIGNMENT 4 /* Appears to hold for all "32 bit" compilers */
743 /* except Borland. The -a4 option fixes */
745 /* Ivan Demakov: For Watcom the option is -zp4. */
746 # ifndef SMALL_CONFIG
747 # define ALIGN_DOUBLE /* Not strictly necessary, but may give speed */
748 /* improvement on Pentiums. */
751 # define OS_TYPE "SEQUENT"
753 # define DATASTART ((ptr_t)((((word) (&etext)) + 0xfff) & ~0xfff))
754 # define STACKBOTTOM ((ptr_t) 0x3ffff000)
757 # define OS_TYPE "SUNOS5"
758 extern int etext
, _start
;
759 extern char * GC_SysVGetDataStart();
760 # define DATASTART GC_SysVGetDataStart(0x1000, &etext)
761 # define STACKBOTTOM ((ptr_t)(&_start))
762 /** At least in Solaris 2.5, PROC_VDB gives wrong values for dirty bits. */
763 /*# define PROC_VDB*/
764 # define DYNAMIC_LOADING
769 # define HEAP_START (ptr_t)0x40000000
771 # define HEAP_START DATAEND
775 # define OS_TYPE "SCO"
777 # define DATASTART ((ptr_t)((((word) (&etext)) + 0x3fffff) \
779 +((word)&etext & 0xfff))
780 # define STACKBOTTOM ((ptr_t) 0x7ffffffc)
783 # define OS_TYPE "SCO_ELF"
785 # define DATASTART ((ptr_t)(&etext))
786 # define STACKBOTTOM ((ptr_t) 0x08048000)
787 # define DYNAMIC_LOADING
788 # define ELF_CLASS ELFCLASS32
791 # define OS_TYPE "LINUX"
792 # define LINUX_STACKBOTTOM
796 # define STACK_GRAN 0x10000000
797 /* STACKBOTTOM is usually 0xc0000000, but this changes with */
798 /* different kernel configurations. In particular, systems */
799 /* with 2GB physical memory will usually move the user */
800 /* address space limit, and hence initial SP to 0x80000000. */
802 # if !defined(LINUX_THREADS) || !defined(REDIRECT_MALLOC)
803 # define MPROTECT_VDB
805 /* We seem to get random errors in incremental mode, */
806 /* possibly because Linux threads is itself a malloc client */
807 /* and can't deal with the signals. */
810 # define DYNAMIC_LOADING
811 # ifdef UNDEFINED /* includes ro data */
813 # define DATASTART ((ptr_t)((((word) (&_etext)) + 0xfff) & ~0xfff))
815 # include <features.h>
816 # if defined(__GLIBC__) && __GLIBC__ >= 2
817 # define LINUX_DATA_START
819 extern char **__environ
;
820 # define DATASTART ((ptr_t)(&__environ))
821 /* hideous kludge: __environ is the first */
822 /* word in crt0.o, and delimits the start */
823 /* of the data segment, no matter which */
824 /* ld options were passed through. */
825 /* We could use _etext instead, but that */
826 /* would include .rodata, which may */
827 /* contain large read-only data tables */
828 /* that we'd rather not scan. */
831 # define DATAEND (&_end)
834 # define DATASTART ((ptr_t)((((word) (&etext)) + 0xfff) & ~0xfff))
836 # ifdef USE_I686_PREFETCH
837 # define PREFETCH(x) \
838 __asm__ __volatile__ (" prefetchnta %0": : "m"(*(char *)(x)))
839 /* Empirically prefetcht0 is much more effective at reducing */
840 /* cache miss stalls for the targetted load instructions. But it */
841 /* seems to interfere enough with other cache traffic that the net */
842 /* result is worse than prefetchnta. */
844 /* Using prefetches for write seems to have a slight negative */
845 /* impact on performance, at least for a PIII/500. */
846 # define PREFETCH_FOR_WRITE(x) \
847 __asm__ __volatile__ (" prefetcht0 %0": : "m"(*(char *)(x)))
850 # ifdef USE_3DNOW_PREFETCH
851 # define PREFETCH(x) \
852 __asm__ __volatile__ (" prefetch %0": : "m"(*(char *)(x)))
853 # define PREFETCH_FOR_WRITE(x)
854 __asm__
__volatile__ (" prefetchw %0": : "m"(*(char *)(x
)))
858 # define OS_TYPE "CYGWIN32"
859 extern int _data_start__
;
860 extern int _data_end__
;
861 extern int _bss_start__
;
862 extern int _bss_end__
;
863 /* For binutils 2.9.1, we have */
864 /* DATASTART = _data_start__ */
865 /* DATAEND = _bss_end__ */
866 /* whereas for some earlier versions it was */
867 /* DATASTART = _bss_start__ */
868 /* DATAEND = _data_end__ */
869 /* To get it right for both, we take the */
870 /* minumum/maximum of the two. */
871 # define MAX(x,y) ((x) > (y) ? (x) : (y))
872 # define MIN(x,y) ((x) < (y) ? (x) : (y))
873 # define DATASTART ((ptr_t) MIN(&_data_start__, &_bss_start__))
874 # define DATAEND ((ptr_t) MAX(&_data_end__, &_bss_end__))
876 # define STACK_GRAN 0x10000
880 # define OS_TYPE "OS2"
881 /* STACKBOTTOM and DATASTART are handled specially in */
882 /* os_dep.c. OS2 actually has the right */
884 # define DATAEND /* not needed */
887 # define OS_TYPE "MSWIN32"
888 /* STACKBOTTOM and DATASTART are handled specially in */
891 # define MPROTECT_VDB
893 # define DATAEND /* not needed */
896 # define OS_TYPE "DJGPP"
897 # include "stubinfo.h"
900 extern int __djgpp_stack_limit
;
901 # define DATASTART ((ptr_t)((((word) (&etext)) + 0x1ff) & ~0x1ff))
902 /* # define STACKBOTTOM ((ptr_t)((word) _stubinfo + _stubinfo->size \
904 # define STACKBOTTOM ((ptr_t)((word) __djgpp_stack_limit + _stklen))
905 /* This may not be right. */
908 # define OS_TYPE "OPENBSD"
911 # define OS_TYPE "FREEBSD"
912 # define MPROTECT_VDB
915 # define OS_TYPE "NETBSD"
918 # define OS_TYPE "THREE86BSD"
921 # define OS_TYPE "BSDI"
923 # if defined(OPENBSD) || defined(FREEBSD) || defined(NETBSD) \
924 || defined(THREE86BSD) || defined(BSDI)
927 # define DATASTART ((ptr_t)(&etext))
930 # define OS_TYPE "NEXT"
931 # define DATASTART ((ptr_t) get_etext())
932 # define STACKBOTTOM ((ptr_t)0xc0000000)
933 # define DATAEND /* not needed */
936 # define OS_TYPE "DOS4GW"
937 extern long __nullarea
;
939 extern char *_STACKTOP
;
940 /* Depending on calling conventions Watcom C either precedes
941 or does not precedes with undescore names of C-variables.
942 Make sure startup code variables always have the same names. */
943 #pragma aux __nullarea "*";
944 #pragma aux _end "*";
945 # define STACKBOTTOM ((ptr_t) _STACKTOP)
946 /* confused? me too. */
947 # define DATASTART ((ptr_t) &__nullarea)
948 # define DATAEND ((ptr_t) &_end)
953 # define MACH_TYPE "NS32K"
955 extern char **environ
;
956 # define DATASTART ((ptr_t)(&environ))
957 /* hideous kludge: environ is the first */
958 /* word in crt0.o, and delimits the start */
959 /* of the data segment, no matter which */
960 /* ld options were passed through. */
961 # define STACKBOTTOM ((ptr_t) 0xfffff000) /* for Encore */
965 # define MACH_TYPE "MIPS"
966 /* # define STACKBOTTOM ((ptr_t)0x7fff8000) sometimes also works. */
968 /* This was developed for a linuxce style platform. Probably */
969 /* needs to be tweaked for workstation class machines. */
970 # define OS_TYPE "LINUX"
971 extern int __data_start
;
972 # define DATASTART ((ptr_t)(&__data_start))
974 # define USE_GENERIC_PUSH_REGS 1
975 # define STACKBOTTOM 0x80000000
976 /* In many cases, this should probably use LINUX_STACKBOTTOM */
977 /* instead. But some kernel versions seem to give the wrong */
978 /* value from /proc. */
982 # define DATASTART (ptr_t)0x10000000
983 /* Could probably be slightly higher since */
984 /* startup code allocates lots of stuff. */
985 # define OS_TYPE "ULTRIX"
990 # define DATASTART (ptr_t)0x10000000
991 # define OS_TYPE "RISCOS"
992 # define ALIGNMENT 4 /* Required by hardware */
997 # define DATASTART ((ptr_t)(&_fdata))
999 # define HEAP_START (ptr_t)0x30000000
1001 # define HEAP_START DATASTART
1003 /* Lowest plausible heap address. */
1004 /* In the MMAP case, we map there. */
1005 /* In either case it is used to identify */
1006 /* heap sections so they're not */
1007 /* considered as roots. */
1008 # define OS_TYPE "IRIX5"
1009 # define MPROTECT_VDB
1011 # define CPP_WORDSZ _MIPS_SZPTR
1012 # define ALIGNMENT (_MIPS_SZPTR/8)
1013 # if CPP_WORDSZ != 64
1014 # define ALIGN_DOUBLE
1017 # define ALIGNMENT 4
1018 # define ALIGN_DOUBLE
1020 # define DYNAMIC_LOADING
1025 # define MACH_TYPE "RS6000"
1026 # define ALIGNMENT 4
1027 # define DATASTART ((ptr_t)0x20000000)
1029 # define STACKBOTTOM ((ptr_t)((ulong)&errno))
1030 # define DYNAMIC_LOADING
1031 /* For really old versions of AIX, this may have to be removed. */
1035 /* OS is assumed to be HP/UX */
1036 # define MACH_TYPE "HP_PA"
1037 # define OS_TYPE "HPUX"
1039 # define CPP_WORDSZ 64
1040 # define ALIGNMENT 8
1042 # define CPP_WORDSZ 32
1043 # define ALIGNMENT 4
1044 # define ALIGN_DOUBLE
1046 extern int __data_start
;
1047 # define DATASTART ((ptr_t)(&__data_start))
1049 /* The following appears to work for 7xx systems running HP/UX */
1050 /* 9.xx Furthermore, it might result in much faster */
1051 /* collections than HEURISTIC2, which may involve scanning */
1052 /* segments that directly precede the stack. It is not the */
1053 /* default, since it may not work on older machine/OS */
1054 /* combinations. (Thanks to Raymond X.T. Nijssen for uncovering */
1056 # define STACKBOTTOM ((ptr_t) 0x7b033000) /* from /etc/conf/h/param.h */
1058 /* Gustavo Rodriguez-Rivera suggested changing HEURISTIC2 */
1059 /* to this. We'll probably do this on other platforms, too. */
1060 /* For now I'll use it where I can test it. */
1061 extern char ** environ
;
1062 # define STACKBOTTOM ((ptr_t)environ)
1064 # define STACK_GROWS_UP
1065 # define DYNAMIC_LOADING
1066 # ifndef HPUX_THREADS
1067 # define MPROTECT_VDB
1069 # include <unistd.h>
1070 # define GETPAGESIZE() sysconf(_SC_PAGE_SIZE)
1074 # define MACH_TYPE "ALPHA"
1075 # define ALIGNMENT 8
1076 # define USE_GENERIC_PUSH_REGS
1077 /* Gcc and probably the DEC/Compaq compiler spill pointers to preserved */
1078 /* fp registers in some cases when the target is a 21264. The assembly */
1079 /* code doesn't handle that yet, and version dependencies make that a */
1080 /* bit tricky. Do the easy thing for now. */
1082 # define OS_TYPE "OSF1"
1083 # define DATASTART ((ptr_t) 0x140000000)
1085 # define DATAEND ((ptr_t) &_end)
1087 /* Normally HEURISTIC2 is too conervative, since */
1088 /* the text segment immediately follows the stack. */
1089 /* Hence we give an upper pound. */
1091 # define HEURISTIC2_LIMIT ((ptr_t)((word)(&__start) & ~(getpagesize()-1)))
1092 # define CPP_WORDSZ 64
1093 # define MPROTECT_VDB
1094 # define DYNAMIC_LOADING
1097 # define OS_TYPE "LINUX"
1098 # define CPP_WORDSZ 64
1099 # define STACKBOTTOM ((ptr_t) 0x120000000)
1101 # define LINUX_DATA_START
1102 # define DYNAMIC_LOADING
1103 /* This doesn't work if the collector is in a dynamic library. */
1105 # define DATASTART ((ptr_t) 0x140000000)
1108 # define DATAEND (&_end)
1109 # define MPROTECT_VDB
1110 /* Has only been superficially tested. May not */
1111 /* work on all versions. */
1116 # define MACH_TYPE "IA64"
1117 # define ALIGN_DOUBLE
1118 /* Requires 16 byte alignment for malloc */
1119 # define ALIGNMENT 8
1120 # define USE_GENERIC_PUSH_REGS
1121 /* We need to get preserved registers in addition to register windows. */
1122 /* That's easiest to do with setjmp. */
1127 # define OS_TYPE "LINUX"
1128 # define CPP_WORDSZ 64
1129 /* This should really be done through /proc, but that */
1130 /* requires we run on an IA64 kernel. */
1131 # define STACKBOTTOM ((ptr_t) 0xa000000000000000l)
1132 /* We also need the base address of the register stack */
1133 /* backing store. There is probably a better way to */
1134 /* get that, too ... */
1135 # define BACKING_STORE_BASE ((ptr_t) 0x9fffffff80000000l)
1137 # define SEARCH_FOR_DATA_START
1138 # define DATASTART GC_data_start
1140 extern int data_start
;
1141 # define DATASTART ((ptr_t)(&data_start))
1143 # define DYNAMIC_LOADING
1144 # define MPROTECT_VDB
1145 /* Requires Linux 2.3.47 or later. */
1147 # define DATAEND (&_end)
1148 # define PREFETCH(x) \
1149 __asm__ (" lfetch [%0]": : "r"((void *)(x)))
1150 # define PREFETCH_FOR_WRITE(x) \
1151 __asm__ (" lfetch.excl [%0]": : "r"((void *)(x)))
1152 # define CLEAR_DOUBLE(x) \
1153 __asm__ (" stf.spill [%0]=f0": : "r"((void *)(x)))
1158 # define MACH_TYPE "M88K"
1159 # define ALIGNMENT 4
1160 # define ALIGN_DOUBLE
1163 # define OS_TYPE "CX_UX"
1164 # define DATASTART ((((word)&etext + 0x3fffff) & ~0x3fffff) + 0x10000)
1167 # define OS_TYPE "DGUX"
1168 extern char * GC_SysVGetDataStart();
1169 # define DATASTART (ptr_t)GC_SysVGetDataStart(0x10000, &etext)
1171 # define STACKBOTTOM ((char*)0xf0000000) /* determined empirically */
1175 # define MACH_TYPE "S370"
1176 # define OS_TYPE "UTS4"
1177 # define ALIGNMENT 4 /* Required by hardware */
1181 extern char * GC_SysVGetDataStart();
1182 # define DATASTART (ptr_t)GC_SysVGetDataStart(0x10000, &_etext)
1183 # define DATAEND (&_end)
1188 # define ALIGNMENT 4
1190 # define DATASTART ((ptr_t)(&_etext))
1195 # define CPP_WORDSZ 32
1196 # define MACH_TYPE "ARM32"
1197 # define ALIGNMENT 4
1199 # define OS_TYPE "NETBSD"
1202 # define DATASTART ((ptr_t)(&etext))
1203 # define USE_GENERIC_PUSH_REGS
1206 # define OS_TYPE "LINUX"
1209 # define STACK_GRAN 0x10000000
1210 # define USE_GENERIC_PUSH_REGS
1212 # define DYNAMIC_LOADING
1213 # include <features.h>
1214 # if defined(__GLIBC__) && __GLIBC__ >= 2
1215 # define LINUX_DATA_START
1217 extern char **__environ
;
1218 # define DATASTART ((ptr_t)(&__environ))
1219 /* hideous kludge: __environ is the first */
1220 /* word in crt0.o, and delimits the start */
1221 /* of the data segment, no matter which */
1222 /* ld options were passed through. */
1223 /* We could use _etext instead, but that */
1224 /* would include .rodata, which may */
1225 /* contain large read-only data tables */
1226 /* that we'd rather not scan. */
1229 # define DATAEND (&_end)
1232 # define DATASTART ((ptr_t)((((word) (&etext)) + 0xfff) & ~0xfff))
1237 #ifdef LINUX_DATA_START
1238 /* Some Linux distributions arrange to define __data_start. Some */
1239 /* define data_start as a weak symbol. The latter is technically */
1240 /* broken, since the user program may define data_start, in which */
1241 /* case we lose. Nonetheless, we try both, prefering __data_start. */
1242 /* We assume gcc. */
1243 # pragma weak __data_start
1244 extern int __data_start
;
1245 # pragma weak data_start
1246 extern int data_start
;
1247 # define DATASTART ((ptr_t)(&__data_start != 0? &__data_start : &data_start))
1250 # ifndef STACK_GROWS_UP
1251 # define STACK_GROWS_DOWN
1255 # define CPP_WORDSZ 32
1264 # define DATAEND (&end)
1267 # if defined(SVR4) && !defined(GETPAGESIZE)
1268 # include <unistd.h>
1269 # define GETPAGESIZE() sysconf(_SC_PAGESIZE)
1272 # ifndef GETPAGESIZE
1273 # if defined(SUNOS5) || defined(IRIX5)
1274 # include <unistd.h>
1276 # define GETPAGESIZE() getpagesize()
1279 # if defined(SUNOS5) || defined(DRSNX) || defined(UTS4)
1280 /* OS has SVR4 generic features. Probably others also qualify. */
1284 # if defined(SUNOS5) || defined(DRSNX)
1285 /* OS has SUNOS5 style semi-undocumented interface to dynamic */
1288 /* OS has SUNOS5 style signal handlers. */
1296 # if CPP_WORDSZ != 32 && CPP_WORDSZ != 64
1301 # undef DYNAMIC_LOADING
1306 # undef MPROTECT_VDB
1311 /* Postponed for now. */
1313 # undef MPROTECT_VDB
1316 # ifdef SMALL_CONFIG
1317 /* Presumably not worth the space it takes. */
1319 # undef MPROTECT_VDB
1323 # undef MPROTECT_VDB /* Can't deal with address space holes. */
1326 # if !defined(PCR_VDB) && !defined(PROC_VDB) && !defined(MPROTECT_VDB)
1327 # define DEFAULT_VDB
1331 # define PREFETCH(x)
1332 # define NO_PREFETCH
1335 # ifndef PREFETCH_FOR_WRITE
1336 # define PREFETCH_FOR_WRITE(x)
1337 # define NO_PREFETCH_FOR_WRITE
1340 # ifndef CACHE_LINE_SIZE
1341 # define CACHE_LINE_SIZE 32 /* Wild guess */
1344 # ifndef CLEAR_DOUBLE
1345 # define CLEAR_DOUBLE(x) \
1346 ((word*)x)[0] = 0; \
1348 # endif /* CLEAR_DOUBLE */
1350 # if defined(_SOLARIS_PTHREADS) && !defined(SOLARIS_THREADS)
1351 # define SOLARIS_THREADS
1353 # if defined(IRIX_THREADS) && !defined(IRIX5)
1354 --> inconsistent configuration
1356 # if defined(IRIX_JDK_THREADS) && !defined(IRIX5)
1357 --> inconsistent configuration
1359 # if defined(LINUX_THREADS) && !defined(LINUX)
1360 --> inconsistent configuration
1362 # if defined(SOLARIS_THREADS) && !defined(SUNOS5)
1363 --> inconsistent configuration
1365 # if defined(HPUX_THREADS) && !defined(HPUX)
1366 --> inconsistent configuration
1368 # if defined(PCR) || defined(SRC_M3) || \
1369 defined(SOLARIS_THREADS) || defined(WIN32_THREADS) || \
1370 defined(IRIX_THREADS) || defined(LINUX_THREADS) || \
1371 defined(IRIX_JDK_THREADS) || defined(HPUX_THREADS)
1375 # if defined(HP_PA) || defined(M88K) || defined(POWERPC) \
1376 || (defined(I386) && defined(OS2)) || defined(UTS4) || defined(LINT)
1377 /* Use setjmp based hack to mark from callee-save registers. */
1378 # define USE_GENERIC_PUSH_REGS
1380 # if defined(SPARC) && !defined(LINUX)
1381 # define SAVE_CALL_CHAIN
1382 # define ASM_CLEAR_CODE /* Stack clearing is crucial, and we */
1383 /* include assembly code to do it well. */
1386 # if defined(LINUX) && !defined(POWERPC)
1389 # include <linux/version.h>
1390 # if (LINUX_VERSION_CODE <= 0x10400)
1391 /* Ugly hack to get struct sigcontext_struct definition. Required */
1392 /* for some early 1.3.X releases. Will hopefully go away soon. */
1393 /* in some later Linux releases, asm/sigcontext.h may have to */
1394 /* be included instead. */
1396 # include <asm/signal.h>
1402 /* Kernels prior to 2.1.1 defined struct sigcontext_struct instead of */
1403 /* struct sigcontext. libc6 (glibc2) uses "struct sigcontext" in */
1404 /* prototypes, so we have to include the top-level sigcontext.h to */
1405 /* make sure the former gets defined to be the latter if appropriate. */
1406 # include <features.h>
1408 # if 2 == __GLIBC__ && 0 == __GLIBC_MINOR__
1409 /* glibc 2.1 no longer has sigcontext.h. But signal.h */
1410 /* has the right declaration for glibc 2.1. */
1411 # include <sigcontext.h>
1412 # endif /* 0 == __GLIBC_MINOR__ */
1413 # else /* not 2 <= __GLIBC__ */
1414 /* libc5 doesn't have <sigcontext.h>: go directly with the kernel */
1415 /* one. Check LINUX_VERSION_CODE to see which we should reference. */
1416 # include <asm/sigcontext.h>
1417 # endif /* 2 <= __GLIBC__ */
1420 # if !defined(OS2) && !defined(PCR) && !defined(AMIGA) && !defined(MACOS)
1421 # include <sys/types.h>
1422 # if !defined(MSWIN32) && !defined(SUNOS4)
1423 # include <unistd.h>
1427 # include <signal.h>
1429 /* Blatantly OS dependent routines, except for those that are related */
1430 /* to dynamic loading. */
1432 # if !defined(THREADS) && !defined(STACKBOTTOM) && defined(HEURISTIC2)
1433 # define NEED_FIND_LIMIT
1436 # if defined(IRIX_THREADS) || defined(HPUX_THREADS)
1437 # define NEED_FIND_LIMIT
1440 # if (defined(SUNOS4) && defined(DYNAMIC_LOADING)) && !defined(PCR)
1441 # define NEED_FIND_LIMIT
1444 # if (defined(SVR4) || defined(AUX) || defined(DGUX)) && !defined(PCR)
1445 # define NEED_FIND_LIMIT
1448 # if defined(LINUX) && \
1449 (defined(POWERPC) || defined(SPARC) || defined(ALPHA) || defined(IA64) \
1451 # define NEED_FIND_LIMIT
1454 #ifdef NEED_FIND_LIMIT
1455 # include <setjmp.h>
1459 # include <machine/trap.h>
1463 # include <proto/exec.h>
1464 # include <proto/dos.h>
1465 # include <dos/dosextens.h>
1466 # include <workbench/startup.h>
1470 # define WIN32_LEAN_AND_MEAN
1472 # include <windows.h>
1476 # include <Processes.h>
1480 # include <sys/uio.h>
1481 # include <malloc.h> /* for locking */
1484 # include <sys/types.h>
1485 # include <sys/mman.h>
1486 # include <sys/stat.h>
1491 # include <sys/siginfo.h>
1494 # define setjmp(env) sigsetjmp(env, 1)
1495 # define longjmp(env, val) siglongjmp(env, val)
1496 # define jmp_buf sigjmp_buf
1500 /* Apparently necessary for djgpp 2.01. May casuse problems with */
1501 /* other versions. */
1502 typedef long unsigned int caddr_t
;
1506 # include "il/PCR_IL.h"
1507 # include "th/PCR_ThCtl.h"
1508 # include "mm/PCR_MM.h"
1511 #if !defined(NO_EXECUTE_PERMISSION)
1512 # define OPT_PROT_EXEC PROT_EXEC
1514 # define OPT_PROT_EXEC 0
1519 # include <stddef.h>
1521 # if !defined(__IBMC__) && !defined(__WATCOMC__) /* e.g. EMX */
1523 # else /* IBM's compiler */
1525 /* A kludge to get around what appears to be a header file bug */
1527 # define WORD unsigned short
1530 # define DWORD unsigned long
1534 # include <newexe.h>
1535 # include <exe386.h>
1537 # endif /* __IBMC__ */
1539 # define INCL_DOSEXCEPTIONS
1540 # define INCL_DOSPROCESS
1541 # define INCL_DOSERRORS
1542 # define INCL_DOSMODULEMGR
1543 # define INCL_DOSMEMMGR
1549 * Find the base of the stack.
1550 * Used only in single-threaded environment.
1551 * With threads, GC_mark_roots needs to know how to do this.
1552 * Called with allocator lock held.
1555 # define is_writable(prot) ((prot) == PAGE_READWRITE \
1556 || (prot) == PAGE_WRITECOPY \
1557 || (prot) == PAGE_EXECUTE_READWRITE \
1558 || (prot) == PAGE_EXECUTE_WRITECOPY)
1559 /* Return the number of bytes that are writable starting at p. */
1560 /* The pointer p is assumed to be page aligned. */
1561 /* If base is not 0, *base becomes the beginning of the */
1562 /* allocation region containing p. */
1563 static word
GC_get_writable_length(ptr_t p
, ptr_t
*base
)
1565 MEMORY_BASIC_INFORMATION buf
;
1569 result
= VirtualQuery(p
, &buf
, sizeof(buf
));
1570 if (result
!= sizeof(buf
)) ABORT("Weird VirtualQuery result");
1571 if (base
!= 0) *base
= (ptr_t
)(buf
.AllocationBase
);
1572 protect
= (buf
.Protect
& ~(PAGE_GUARD
| PAGE_NOCACHE
));
1573 if (!is_writable(protect
)) {
1576 if (buf
.State
!= MEM_COMMIT
) return(0);
1577 return(buf
.RegionSize
);
1580 void *scm_get_stack_base()
1583 ptr_t sp
= (ptr_t
)(&dummy
);
1586 static word GC_page_size
= 0;
1587 if (!GC_page_size
) {
1588 SYSTEM_INFO sysinfo
;
1589 GetSystemInfo(&sysinfo
);
1590 GC_page_size
= sysinfo
.dwPageSize
;
1592 trunc_sp
= (ptr_t
)((word
)sp
& ~(GC_page_size
- 1));
1593 size
= GC_get_writable_length(trunc_sp
, 0);
1594 return(trunc_sp
+ size
);
1602 void *scm_get_stack_base()
1607 if (DosGetInfoBlocks(&ptib
, &ppib
) != NO_ERROR
) {
1608 GC_err_printf0("DosGetInfoBlocks failed\n");
1609 ABORT("DosGetInfoBlocks failed\n");
1611 return((ptr_t
)(ptib
-> tib_pstacklimit
));
1618 void *scm_get_stack_base()
1620 struct Process
*proc
= (struct Process
*)SysBase
->ThisTask
;
1622 /* Reference: Amiga Guru Book Pages: 42,567,574 */
1623 if (proc
->pr_Task
.tc_Node
.ln_Type
==NT_PROCESS
1624 && proc
->pr_CLI
!= NULL
) {
1625 /* first ULONG is StackSize */
1626 /*longPtr = proc->pr_ReturnAddr;
1627 size = longPtr[0];*/
1629 return (char *)proc
->pr_ReturnAddr
+ sizeof(ULONG
);
1631 return (char *)proc
->pr_Task
.tc_SPUpper
;
1635 #if 0 /* old version */
1636 void *scm_get_stack_base()
1638 extern struct WBStartup
*_WBenchMsg
;
1640 extern long __stack
;
1642 struct Process
*proc
;
1643 struct CommandLineInterface
*cli
;
1646 if ((task
= FindTask(0)) == 0) {
1647 GC_err_puts("Cannot find own task structure\n");
1648 ABORT("task missing");
1650 proc
= (struct Process
*)task
;
1651 cli
= BADDR(proc
->pr_CLI
);
1653 if (_WBenchMsg
!= 0 || cli
== 0) {
1654 size
= (char *)task
->tc_SPUpper
- (char *)task
->tc_SPLower
;
1656 size
= cli
->cli_DefaultStack
* 4;
1658 return (ptr_t
)(__base
+ GC_max(size
, __stack
));
1662 # else /* !AMIGA, !OS2, ... */
1664 # ifdef NEED_FIND_LIMIT
1665 /* Some tools to implement HEURISTIC2 */
1666 # define MIN_PAGE_SIZE 256 /* Smallest conceivable page size, bytes */
1667 /* static */ jmp_buf GC_jmp_buf
;
1670 static void GC_fault_handler(sig
)
1673 longjmp(GC_jmp_buf
, 1);
1677 typedef void (*handler
)(int);
1679 typedef void (*handler
)();
1682 # if defined(SUNOS5SIGS) || defined(IRIX5) || defined(OSF1)
1683 static struct sigaction old_segv_act
;
1684 # if defined(_sigargs) || defined(HPUX) /* !Irix6.x */
1685 static struct sigaction old_bus_act
;
1688 static handler old_segv_handler
, old_bus_handler
;
1691 static void GC_setup_temporary_fault_handler()
1693 # if defined(SUNOS5SIGS) || defined(IRIX5) || defined(OSF1)
1694 struct sigaction act
;
1696 act
.sa_handler
= GC_fault_handler
;
1697 act
.sa_flags
= SA_RESTART
| SA_NODEFER
;
1698 /* The presence of SA_NODEFER represents yet another gross */
1699 /* hack. Under Solaris 2.3, siglongjmp doesn't appear to */
1700 /* interact correctly with -lthread. We hide the confusion */
1701 /* by making sure that signal handling doesn't affect the */
1704 (void) sigemptyset(&act
.sa_mask
);
1705 # ifdef IRIX_THREADS
1706 /* Older versions have a bug related to retrieving and */
1707 /* and setting a handler at the same time. */
1708 (void) sigaction(SIGSEGV
, 0, &old_segv_act
);
1709 (void) sigaction(SIGSEGV
, &act
, 0);
1711 (void) sigaction(SIGSEGV
, &act
, &old_segv_act
);
1712 # if defined(IRIX5) && defined(_sigargs) /* Irix 5.x, not 6.x */ \
1714 /* Under Irix 5.x or HP/UX, we may get SIGBUS. */
1715 /* Pthreads doesn't exist under Irix 5.x, so we */
1716 /* don't have to worry in the threads case. */
1717 (void) sigaction(SIGBUS
, &act
, &old_bus_act
);
1719 # endif /* IRIX_THREADS */
1721 old_segv_handler
= signal(SIGSEGV
, GC_fault_handler
);
1723 old_bus_handler
= signal(SIGBUS
, GC_fault_handler
);
1728 static void GC_reset_fault_handler()
1730 # if defined(SUNOS5SIGS) || defined(IRIX5) || defined(OSF1)
1731 (void) sigaction(SIGSEGV
, &old_segv_act
, 0);
1732 # if defined(IRIX5) && defined(_sigargs) /* Irix 5.x, not 6.x */ \
1734 (void) sigaction(SIGBUS
, &old_bus_act
, 0);
1737 (void) signal(SIGSEGV
, old_segv_handler
);
1739 (void) signal(SIGBUS
, old_bus_handler
);
1744 /* Return the first nonaddressible location > p (up) or */
1745 /* the smallest location q s.t. [q,p] is addressible (!up). */
1746 static ptr_t
GC_find_limit(p
, up
)
1750 static VOLATILE ptr_t result
;
1751 /* Needs to be static, since otherwise it may not be */
1752 /* preserved across the longjmp. Can safely be */
1753 /* static since it's only called once, with the */
1754 /* allocation lock held. */
1757 GC_setup_temporary_fault_handler();
1758 if (setjmp(GC_jmp_buf
) == 0) {
1759 result
= (ptr_t
)(((word
)(p
))
1760 & ~(MIN_PAGE_SIZE
-1));
1763 result
+= MIN_PAGE_SIZE
;
1765 result
-= MIN_PAGE_SIZE
;
1767 GC_noop1((word
)(*result
));
1770 GC_reset_fault_handler();
1772 result
+= MIN_PAGE_SIZE
;
1778 #ifdef LINUX_STACKBOTTOM
1780 #include <sys/types.h>
1781 #include <sys/stat.h>
1784 # define STAT_SKIP 27 /* Number of fields preceding startstack */
1785 /* field in /proc/self/stat */
1787 static ptr_t
GC_linux_stack_base(void)
1789 /* We read the stack base value from /proc/self/stat. We do this */
1790 /* using direct I/O system calls in order to avoid calling malloc */
1791 /* in case REDIRECT_MALLOC is defined. */
1792 # define STAT_BUF_SIZE 4096
1794 # define STAT_READ __real_read
1796 # define STAT_READ read
1798 char stat_buf
[STAT_BUF_SIZE
];
1802 size_t i
, buf_offset
= 0;
1804 f
= open("/proc/self/stat", O_RDONLY
);
1805 if (f
< 0 || STAT_READ(f
, stat_buf
, STAT_BUF_SIZE
) < 2 * STAT_SKIP
) {
1806 ABORT("Couldn't read /proc/self/stat");
1808 c
= stat_buf
[buf_offset
++];
1809 /* Skip the required number of fields. This number is hopefully */
1810 /* constant across all Linux implementations. */
1811 for (i
= 0; i
< STAT_SKIP
; ++i
) {
1812 while (isspace(c
)) c
= stat_buf
[buf_offset
++];
1813 while (!isspace(c
)) c
= stat_buf
[buf_offset
++];
1815 while (isspace(c
)) c
= stat_buf
[buf_offset
++];
1816 while (isdigit(c
)) {
1819 c
= stat_buf
[buf_offset
++];
1822 if (result
< 0x10000000) ABORT("Absurd stack bottom value");
1823 return (ptr_t
)result
;
1826 #endif /* LINUX_STACKBOTTOM */
1828 void *scm_get_stack_base()
1831 void *result
= &dummy
; /* initialize to silence compiler */
1833 # define STACKBOTTOM_ALIGNMENT_M1 ((word)STACK_GRAN - 1)
1836 return(STACKBOTTOM
);
1839 # ifdef STACK_GROWS_DOWN
1840 result
= (ptr_t
)((((word
)(&dummy
))
1841 + STACKBOTTOM_ALIGNMENT_M1
)
1842 & ~STACKBOTTOM_ALIGNMENT_M1
);
1844 result
= (ptr_t
)(((word
)(&dummy
))
1845 & ~STACKBOTTOM_ALIGNMENT_M1
);
1847 # endif /* HEURISTIC1 */
1848 # ifdef LINUX_STACKBOTTOM
1849 result
= GC_linux_stack_base();
1852 # ifdef STACK_GROWS_DOWN
1853 result
= GC_find_limit((ptr_t
)(&dummy
), TRUE
);
1854 # ifdef HEURISTIC2_LIMIT
1855 if (result
> HEURISTIC2_LIMIT
1856 && (ptr_t
)(&dummy
) < HEURISTIC2_LIMIT
) {
1857 result
= HEURISTIC2_LIMIT
;
1861 result
= GC_find_limit((ptr_t
)(&dummy
), FALSE
);
1862 # ifdef HEURISTIC2_LIMIT
1863 if (result
< HEURISTIC2_LIMIT
1864 && (ptr_t
)(&dummy
) > HEURISTIC2_LIMIT
) {
1865 result
= HEURISTIC2_LIMIT
;
1870 # endif /* HEURISTIC2 */
1871 # ifdef STACK_GROWS_DOWN
1872 if (result
== 0) result
= (ptr_t
)(signed_word
)(-sizeof(ptr_t
));
1875 # endif /* STACKBOTTOM */
1878 # endif /* ! AMIGA */
1880 # endif /* ! MSWIN32 */