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, 2001 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 /* Machine dependent parameters. Some tuning parameters can be found */
66 /* near the top of gc_private.h. */
68 /* Machine specific parts contributed by various people. See README file. */
70 /* First a unified test for Linux: */
71 # if defined(linux) || defined(__linux__)
75 /* Determine the machine type: */
76 # if defined(sun) && defined(mc68000)
79 # define mach_type_known
81 # if defined(hp9000s300)
84 # define mach_type_known
86 # if defined(__OpenBSD__) && defined(m68k)
89 # define mach_type_known
91 # if defined(__OpenBSD__) && defined(__sparc__)
94 # define mach_type_known
96 # if defined(__NetBSD__) && defined(m68k)
99 # define mach_type_known
101 # if defined(__NetBSD__) && defined(arm32)
104 # define mach_type_known
113 # define mach_type_known
115 # if defined(mips) || defined(__mips)
118 # if defined(ultrix) || defined(__ultrix) || defined(__NetBSD__)
121 # if defined(_SYSTYPE_SVR4) || defined(SYSTYPE_SVR4) \
122 || defined(__SYSTYPE_SVR4__)
123 # define IRIX5 /* or IRIX 6.X */
125 # define RISCOS /* or IRIX 4.X */
129 # define mach_type_known
131 # if defined(sequent) && defined(i386)
134 # define mach_type_known
136 # if defined(sun) && defined(i386)
139 # define mach_type_known
141 # if (defined(__OS2__) || defined(__EMX__)) && defined(__32BIT__)
144 # define mach_type_known
148 # define mach_type_known
150 # if defined(sun) && (defined(sparc) || defined(__sparc))
152 /* Test for SunOS 5.x */
159 # define mach_type_known
161 # if defined(sparc) && defined(unix) && !defined(sun) && !defined(linux) \
162 && !defined(__OpenBSD__)
165 # define mach_type_known
169 # define mach_type_known
171 # if defined(_M_XENIX) && defined(_M_SYSV) && defined(_M_I386)
172 /* The above test may need refinement */
174 # if defined(_SCO_ELF)
179 # define mach_type_known
181 # if defined(_AUX_SOURCE)
184 # define mach_type_known
186 # if defined(_PA_RISC1_0) || defined(_PA_RISC1_1) || defined(_PA_RISC2_0) \
187 || defined(hppa) || defined(__hppa__)
192 # define mach_type_known
194 # if defined(LINUX) && (defined(i386) || defined(__i386__))
196 # define mach_type_known
198 # if defined(LINUX) && (defined(__ia64__) || defined(__ia64))
200 # define mach_type_known
202 # if defined(LINUX) && defined(powerpc)
204 # define mach_type_known
206 # if defined(LINUX) && defined(__mc68000__)
208 # define mach_type_known
210 # if defined(LINUX) && (defined(sparc) || defined(__sparc__))
212 # define mach_type_known
214 # if defined(LINUX) && defined(arm)
216 # define mach_type_known
218 # if defined(__alpha) || defined(__alpha__)
221 # define OSF1 /* a.k.a Digital Unix */
223 # define mach_type_known
225 # if defined(_AMIGA) && !defined(AMIGA)
230 # define mach_type_known
232 # if defined(THINK_C) || defined(__MWERKS__) && !defined(__powerc)
235 # define mach_type_known
237 # if defined(__MWERKS__) && defined(__powerc)
240 # define mach_type_known
245 # define mach_type_known
247 # if defined(NeXT) && defined(mc68000)
250 # define mach_type_known
252 # if defined(NeXT) && defined(i386)
255 # define mach_type_known
257 # if defined(__OpenBSD__) && defined(i386)
260 # define mach_type_known
262 # if defined(__FreeBSD__) && defined(i386)
265 # define mach_type_known
267 # if defined(__NetBSD__) && defined(i386)
270 # define mach_type_known
272 # if defined(bsdi) && defined(i386)
275 # define mach_type_known
277 # if !defined(mach_type_known) && defined(__386BSD__)
280 # define mach_type_known
282 # if defined(_CX_UX) && defined(_M88K)
285 # define mach_type_known
290 # define mach_type_known
292 # if (defined(_MSDOS) || defined(_MSC_VER)) && (_M_IX86 >= 300) \
293 || defined(_WIN32) && !defined(__CYGWIN32__) && !defined(__CYGWIN__)
295 # define MSWIN32 /* or Win32s */
296 # define mach_type_known
298 # if defined(__DJGPP__)
301 # define DJGPP /* MSDOS running the DJGPP port of GCC */
303 # define mach_type_known
305 # if defined(__CYGWIN32__) || defined(__CYGWIN__)
308 # define mach_type_known
310 # if defined(__MINGW32__)
313 # define mach_type_known
315 # if defined(__BORLANDC__)
318 # define mach_type_known
320 # if defined(_UTS) && !defined(mach_type_known)
323 # define mach_type_known
327 # define mach_type_known
330 # if defined(__WATCOMC__) && defined(__386__)
332 # if !defined(OS2) && !defined(MSWIN32) && !defined(DOS4GW)
333 # if defined(__OS2__)
336 # if defined(__WINDOWS_386__) || defined(__NT__)
343 # define mach_type_known
346 /* Feel free to add more clauses here */
348 /* Or manually define the machine type here. A machine type is */
349 /* characterized by the architecture. Some */
350 /* machine types are further subdivided by OS. */
351 /* the macros ULTRIX, RISCOS, and BSD to distinguish. */
352 /* Note that SGI IRIX is treated identically to RISCOS. */
353 /* SYSV on an M68K actually means A/UX. */
354 /* The distinction in these cases is usually the stack starting address */
355 # ifndef mach_type_known
356 --> unknown machine type
358 /* Mapping is: M68K ==> Motorola 680X0 */
359 /* (SUNOS4,HP,NEXT, and SYSV (A/UX), */
360 /* MACOS and AMIGA variants) */
361 /* I386 ==> Intel 386 */
362 /* (SEQUENT, OS2, SCO, LINUX, NETBSD, */
363 /* FREEBSD, THREE86BSD, MSWIN32, */
364 /* BSDI,SUNOS5, NEXT, other variants) */
365 /* NS32K ==> Encore Multimax */
366 /* MIPS ==> R2000 or R3000 */
367 /* (RISCOS, ULTRIX variants) */
368 /* VAX ==> DEC VAX */
369 /* (BSD, ULTRIX variants) */
370 /* RS6000 ==> IBM RS/6000 AIX3.X */
371 /* RT ==> IBM PC/RT */
372 /* HP_PA ==> HP9000/700 & /800 */
374 /* SPARC ==> SPARC under SunOS */
375 /* (SUNOS4, SUNOS5, */
376 /* DRSNX variants) */
377 /* ALPHA ==> DEC Alpha */
378 /* (OSF1 and LINUX variants) */
379 /* M88K ==> Motorola 88XX0 */
380 /* (CX_UX and DGUX) */
381 /* S370 ==> 370-like machine */
382 /* running Amdahl UTS4 */
383 /* ARM32 ==> Intel StrongARM */
384 /* IA64 ==> Intel IA64 */
389 * For each architecture and OS, the following need to be defined:
391 * CPP_WORD_SZ is a simple integer constant representing the word size.
392 * in bits. We assume byte addressibility, where a byte has 8 bits.
393 * We also assume CPP_WORD_SZ is either 32 or 64.
394 * (We care about the length of pointers, not hardware
395 * bus widths. Thus a 64 bit processor with a C compiler that uses
396 * 32 bit pointers should use CPP_WORD_SZ of 32, not 64. Default is 32.)
398 * MACH_TYPE is a string representation of the machine type.
399 * OS_TYPE is analogous for the OS.
401 * ALIGNMENT is the largest N, such that
402 * all pointer are guaranteed to be aligned on N byte boundaries.
403 * defining it to be 1 will always work, but perform poorly.
405 * DATASTART is the beginning of the data segment.
406 * On UNIX systems, the collector will scan the area between DATASTART
407 * and DATAEND for root pointers.
409 * DATAEND, if not &end.
411 * ALIGN_DOUBLE of GC_malloc should return blocks aligned to twice
414 * STACKBOTTOM is the cool end of the stack, which is usually the
415 * highest address in the stack.
416 * Under PCR or OS/2, we have other ways of finding thread stacks.
417 * For each machine, the following should:
418 * 1) define STACK_GROWS_UP if the stack grows toward higher addresses, and
419 * 2) define exactly one of
420 * STACKBOTTOM (should be defined to be an expression)
423 * If either of the last two macros are defined, then STACKBOTTOM is computed
424 * during collector startup using one of the following two heuristics:
425 * HEURISTIC1: Take an address inside GC_init's frame, and round it up to
426 * the next multiple of STACK_GRAN.
427 * HEURISTIC2: Take an address inside GC_init's frame, increment it repeatedly
428 * in small steps (decrement if STACK_GROWS_UP), and read the value
429 * at each location. Remember the value when the first
430 * Segmentation violation or Bus error is signalled. Round that
431 * to the nearest plausible page boundary, and use that instead
434 * Gustavo Rodriguez-Rivera points out that on most (all?) Unix machines,
435 * the value of environ is a pointer that can serve as STACKBOTTOM.
436 * I expect that HEURISTIC2 can be replaced by this approach, which
437 * interferes far less with debugging.
439 * If no expression for STACKBOTTOM can be found, and neither of the above
440 * heuristics are usable, the collector can still be used with all of the above
441 * undefined, provided one of the following is done:
442 * 1) GC_mark_roots can be changed to somehow mark from the correct stack(s)
443 * without reference to STACKBOTTOM. This is appropriate for use in
444 * conjunction with thread packages, since there will be multiple stacks.
445 * (Allocating thread stacks in the heap, and treating them as ordinary
446 * heap data objects is also possible as a last resort. However, this is
447 * likely to introduce significant amounts of excess storage retention
448 * unless the dead parts of the thread stacks are periodically cleared.)
449 * 2) Client code may set GC_stackbottom before calling any GC_ routines.
450 * If the author of the client code controls the main program, this is
451 * easily accomplished by introducing a new main program, setting
452 * GC_stackbottom to the address of a local variable, and then calling
453 * the original main program. The new main program would read something
456 * # include "gc_private.h"
458 * main(argc, argv, envp)
460 * char **argv, **envp;
464 * GC_stackbottom = (ptr_t)(&dummy);
465 * return(real_main(argc, argv, envp));
469 * Each architecture may also define the style of virtual dirty bit
470 * implementation to be used:
471 * MPROTECT_VDB: Write protect the heap and catch faults.
472 * PROC_VDB: Use the SVR4 /proc primitives to read dirty bits.
474 * An architecture may define DYNAMIC_LOADING if dynamic_load.c
475 * defined GC_register_dynamic_libraries() for the architecture.
477 * An architecture may define PREFETCH(x) to preload the cache with *x.
478 * This defaults to a no-op.
480 * PREFETCH_FOR_WRITE(x) is used if *x is about to be written.
482 * An architecture may also define CLEAR_DOUBLE(x) to be a fast way to
483 * clear the two words at GC_malloc-aligned address x. By default,
484 * word stores of 0 are used instead.
488 # define STACK_GRAN 0x1000000
490 # define MACH_TYPE "M68K"
493 # define OS_TYPE "OPENBSD"
496 # define DATASTART ((ptr_t)(&etext))
499 # define OS_TYPE "NETBSD"
502 # define DATASTART ((ptr_t)(&etext))
505 # define OS_TYPE "LINUX"
506 # define STACKBOTTOM ((ptr_t)0xf0000000)
507 # define MPROTECT_VDB
509 # define DYNAMIC_LOADING
510 extern char **__environ
;
511 # define DATASTART ((ptr_t)(&__environ))
512 /* hideous kludge: __environ is the first */
513 /* word in crt0.o, and delimits the start */
514 /* of the data segment, no matter which */
515 /* ld options were passed through. */
516 /* We could use _etext instead, but that */
517 /* would include .rodata, which may */
518 /* contain large read-only data tables */
519 /* that we'd rather not scan. */
521 # define DATAEND (&_end)
524 # define DATASTART ((ptr_t)((((word) (&etext)) + 0xfff) & ~0xfff))
528 # define OS_TYPE "SUNOS4"
530 # define DATASTART ((ptr_t)((((word) (&etext)) + 0x1ffff) & ~0x1ffff))
531 # define HEURISTIC1 /* differs */
532 # define DYNAMIC_LOADING
535 # define OS_TYPE "HP"
537 # define DATASTART ((ptr_t)((((word) (&etext)) + 0xfff) & ~0xfff))
538 # define STACKBOTTOM ((ptr_t) 0xffeffffc)
539 /* empirically determined. seems to work. */
541 # define GETPAGESIZE() sysconf(_SC_PAGE_SIZE)
544 # define OS_TYPE "SYSV"
546 # define DATASTART ((ptr_t)((((word) (&etext)) + 0x3fffff) \
548 +((word)&etext & 0x1fff))
549 /* This only works for shared-text binaries with magic number 0413.
550 The other sorts of SysV binaries put the data at the end of the text,
551 in which case the default of &etext would work. Unfortunately,
552 handling both would require having the magic-number available.
555 # define STACKBOTTOM ((ptr_t)0xFFFFFFFE)
556 /* The stack starts at the top of memory, but */
557 /* 0x0 cannot be used as setjump_test complains */
558 /* that the stack direction is incorrect. Two */
559 /* bytes down from 0x0 should be safe enough. */
561 # include <sys/mmu.h>
562 # define GETPAGESIZE() PAGESIZE /* Is this still right? */
565 # define OS_TYPE "AMIGA"
566 /* STACKBOTTOM and DATASTART handled specially */
568 # define DATAEND /* not needed */
569 # define GETPAGESIZE() 4096
575 # define OS_TYPE "MACOS"
576 /* see os_dep.c for details of global data segments. */
577 # define STACKBOTTOM ((ptr_t) LMGetCurStackBase())
578 # define DATAEND /* not needed */
579 # define GETPAGESIZE() 4096
582 # define OS_TYPE "NEXT"
583 # define DATASTART ((ptr_t) get_etext())
584 # define STACKBOTTOM ((ptr_t) 0x4000000)
585 # define DATAEND /* not needed */
590 # define MACH_TYPE "POWERPC"
592 # define ALIGNMENT 2 /* Still necessary? Could it be 4? */
596 # define OS_TYPE "MACOS"
597 /* see os_dep.c for details of global data segments. */
598 # define STACKBOTTOM ((ptr_t) LMGetCurStackBase())
599 # define DATAEND /* not needed */
602 # define ALIGNMENT 4 /* Guess. Can someone verify? */
603 /* This was 2, but that didn't sound right. */
604 # define OS_TYPE "LINUX"
606 # define DYNAMIC_LOADING
608 # define STACK_GRAN 0x10000000
609 /* Stack usually starts at 0x80000000 */
610 # define LINUX_DATA_START
612 # define DATAEND (&_end)
616 # define OS_TYPE "MACOSX"
617 # define DATASTART ((ptr_t) get_etext())
618 # define STACKBOTTOM ((ptr_t) 0xc0000000)
619 # define DATAEND /* not needed */
624 # define MACH_TYPE "VAX"
625 # define ALIGNMENT 4 /* Pointers are longword aligned by 4.2 C compiler */
627 # define DATASTART ((ptr_t)(&etext))
629 # define OS_TYPE "BSD"
631 /* HEURISTIC2 may be OK, but it's hard to test. */
634 # define OS_TYPE "ULTRIX"
635 # define STACKBOTTOM ((ptr_t) 0x7fffc800)
640 # define MACH_TYPE "RT"
642 # define DATASTART ((ptr_t) 0x10000000)
643 # define STACKBOTTOM ((ptr_t) 0x1fffd800)
647 # define MACH_TYPE "SPARC"
648 # define ALIGNMENT 4 /* Required by hardware */
649 # define ALIGN_DOUBLE
652 # define OS_TYPE "SUNOS5"
655 extern char * GC_SysVGetDataStart();
656 # define DATASTART (ptr_t)GC_SysVGetDataStart(0x10000, &_etext)
657 # define DATAEND (&_end)
662 # define HEAP_START (ptr_t)0x40000000
664 # define HEAP_START DATAEND
667 /* HEURISTIC1 reportedly no longer works under 2.7. Thus we */
668 /* switched to HEURISTIC2, eventhough it creates some debugging */
672 # define GETPAGESIZE() sysconf(_SC_PAGESIZE)
673 /* getpagesize() appeared to be missing from at least one */
674 /* Solaris 5.4 installation. Weird. */
675 # define DYNAMIC_LOADING
678 # define OS_TYPE "SUNOS4"
679 /* [If you have a weak stomach, don't read this.] */
680 /* We would like to use: */
681 /* # define DATASTART ((ptr_t)((((word) (&etext)) + 0x1fff) & ~0x1fff)) */
682 /* This fails occasionally, due to an ancient, but very */
683 /* persistent ld bug. &etext is set 32 bytes too high. */
684 /* We instead read the text segment size from the a.out */
685 /* header, which happens to be mapped into our address space */
686 /* at the start of the text segment. The detective work here */
687 /* was done by Robert Ehrlich, Manuel Serrano, and Bernard */
688 /* Serpette of INRIA. */
689 /* This assumes ZMAGIC, i.e. demand-loadable executables. */
690 # define TEXTSTART 0x2000
691 # define DATASTART ((ptr_t)(*(int *)(TEXTSTART+0x4)+TEXTSTART))
692 # define MPROTECT_VDB
694 # define DYNAMIC_LOADING
697 # define CPP_WORDSZ 32
698 # define OS_TYPE "DRSNX"
699 extern char * GC_SysVGetDataStart();
701 # define DATASTART (ptr_t)GC_SysVGetDataStart(0x10000, &etext)
702 # define MPROTECT_VDB
703 # define STACKBOTTOM ((ptr_t) 0xdfff0000)
704 # define DYNAMIC_LOADING
707 # define OS_TYPE "LINUX"
709 # define LINUX_DATA_START
710 # define DYNAMIC_LOADING
712 Linux Sparc non elf
?
715 # define DATAEND (&_end)
717 # define STACKBOTTOM ((ptr_t) 0xf0000000)
720 # define OS_TYPE "OPENBSD"
721 # define STACKBOTTOM ((ptr_t) 0xf8000000)
722 # define DATASTART ((ptr_t)(&etext))
727 # define MACH_TYPE "I386"
728 # define ALIGNMENT 4 /* Appears to hold for all "32 bit" compilers */
729 /* except Borland. The -a4 option fixes */
731 /* Ivan Demakov: For Watcom the option is -zp4. */
732 # ifndef SMALL_CONFIG
733 # define ALIGN_DOUBLE /* Not strictly necessary, but may give speed */
734 /* improvement on Pentiums. */
737 # define OS_TYPE "SEQUENT"
739 # define DATASTART ((ptr_t)((((word) (&etext)) + 0xfff) & ~0xfff))
740 # define STACKBOTTOM ((ptr_t) 0x3ffff000)
743 # define OS_TYPE "SUNOS5"
744 extern int etext
, _start
;
745 extern char * GC_SysVGetDataStart();
746 # define DATASTART GC_SysVGetDataStart(0x1000, &etext)
747 # define STACKBOTTOM ((ptr_t)(&_start))
748 /** At least in Solaris 2.5, PROC_VDB gives wrong values for dirty bits. */
749 /*# define PROC_VDB*/
750 # define DYNAMIC_LOADING
755 # define HEAP_START (ptr_t)0x40000000
757 # define HEAP_START DATAEND
761 # define OS_TYPE "SCO"
763 # define DATASTART ((ptr_t)((((word) (&etext)) + 0x3fffff) \
765 +((word)&etext & 0xfff))
766 # define STACKBOTTOM ((ptr_t) 0x7ffffffc)
769 # define OS_TYPE "SCO_ELF"
771 # define DATASTART ((ptr_t)(&etext))
772 # define STACKBOTTOM ((ptr_t) 0x08048000)
773 # define DYNAMIC_LOADING
774 # define ELF_CLASS ELFCLASS32
777 # define OS_TYPE "LINUX"
778 # define LINUX_STACKBOTTOM
782 # define STACK_GRAN 0x10000000
783 /* STACKBOTTOM is usually 0xc0000000, but this changes with */
784 /* different kernel configurations. In particular, systems */
785 /* with 2GB physical memory will usually move the user */
786 /* address space limit, and hence initial SP to 0x80000000. */
788 # if !defined(LINUX_THREADS) || !defined(REDIRECT_MALLOC)
789 # define MPROTECT_VDB
791 /* We seem to get random errors in incremental mode, */
792 /* possibly because Linux threads is itself a malloc client */
793 /* and can't deal with the signals. */
796 # define DYNAMIC_LOADING
797 # ifdef UNDEFINED /* includes ro data */
799 # define DATASTART ((ptr_t)((((word) (&_etext)) + 0xfff) & ~0xfff))
801 # include <features.h>
802 # if defined(__GLIBC__) && __GLIBC__ >= 2
803 # define LINUX_DATA_START
805 extern char **__environ
;
806 # define DATASTART ((ptr_t)(&__environ))
807 /* hideous kludge: __environ is the first */
808 /* word in crt0.o, and delimits the start */
809 /* of the data segment, no matter which */
810 /* ld options were passed through. */
811 /* We could use _etext instead, but that */
812 /* would include .rodata, which may */
813 /* contain large read-only data tables */
814 /* that we'd rather not scan. */
817 # define DATAEND (&_end)
820 # define DATASTART ((ptr_t)((((word) (&etext)) + 0xfff) & ~0xfff))
822 # ifdef USE_I686_PREFETCH
823 # define PREFETCH(x) \
824 __asm__ __volatile__ (" prefetchnta %0": : "m"(*(char *)(x)))
825 /* Empirically prefetcht0 is much more effective at reducing */
826 /* cache miss stalls for the targetted load instructions. But it */
827 /* seems to interfere enough with other cache traffic that the net */
828 /* result is worse than prefetchnta. */
830 /* Using prefetches for write seems to have a slight negative */
831 /* impact on performance, at least for a PIII/500. */
832 # define PREFETCH_FOR_WRITE(x) \
833 __asm__ __volatile__ (" prefetcht0 %0": : "m"(*(char *)(x)))
836 # ifdef USE_3DNOW_PREFETCH
837 # define PREFETCH(x) \
838 __asm__ __volatile__ (" prefetch %0": : "m"(*(char *)(x)))
839 # define PREFETCH_FOR_WRITE(x)
840 __asm__
__volatile__ (" prefetchw %0": : "m"(*(char *)(x
)))
844 # define OS_TYPE "CYGWIN32"
845 extern int _data_start__
;
846 extern int _data_end__
;
847 extern int _bss_start__
;
848 extern int _bss_end__
;
849 /* For binutils 2.9.1, we have */
850 /* DATASTART = _data_start__ */
851 /* DATAEND = _bss_end__ */
852 /* whereas for some earlier versions it was */
853 /* DATASTART = _bss_start__ */
854 /* DATAEND = _data_end__ */
855 /* To get it right for both, we take the */
856 /* minumum/maximum of the two. */
857 # define MAX(x,y) ((x) > (y) ? (x) : (y))
858 # define MIN(x,y) ((x) < (y) ? (x) : (y))
859 # define DATASTART ((ptr_t) MIN(&_data_start__, &_bss_start__))
860 # define DATAEND ((ptr_t) MAX(&_data_end__, &_bss_end__))
862 # define STACK_GRAN 0x10000
866 # define OS_TYPE "OS2"
867 /* STACKBOTTOM and DATASTART are handled specially in */
868 /* os_dep.c. OS2 actually has the right */
870 # define DATAEND /* not needed */
873 # define OS_TYPE "MSWIN32"
874 /* STACKBOTTOM and DATASTART are handled specially in */
877 # define MPROTECT_VDB
879 # define DATAEND /* not needed */
882 # define OS_TYPE "DJGPP"
883 # include "stubinfo.h"
886 extern int __djgpp_stack_limit
;
887 # define DATASTART ((ptr_t)((((word) (&etext)) + 0x1ff) & ~0x1ff))
888 /* # define STACKBOTTOM ((ptr_t)((word) _stubinfo + _stubinfo->size \
890 # define STACKBOTTOM ((ptr_t)((word) __djgpp_stack_limit + _stklen))
891 /* This may not be right. */
894 # define OS_TYPE "OPENBSD"
897 # define OS_TYPE "FREEBSD"
898 # define MPROTECT_VDB
901 # define OS_TYPE "NETBSD"
904 # define OS_TYPE "THREE86BSD"
907 # define OS_TYPE "BSDI"
909 # if defined(OPENBSD) || defined(FREEBSD) || defined(NETBSD) \
910 || defined(THREE86BSD) || defined(BSDI)
913 # define DATASTART ((ptr_t)(&etext))
916 # define OS_TYPE "NEXT"
917 # define DATASTART ((ptr_t) get_etext())
918 # define STACKBOTTOM ((ptr_t)0xc0000000)
919 # define DATAEND /* not needed */
922 # define OS_TYPE "DOS4GW"
923 extern long __nullarea
;
925 extern char *_STACKTOP
;
926 /* Depending on calling conventions Watcom C either precedes
927 or does not precedes with undescore names of C-variables.
928 Make sure startup code variables always have the same names. */
929 #pragma aux __nullarea "*";
930 #pragma aux _end "*";
931 # define STACKBOTTOM ((ptr_t) _STACKTOP)
932 /* confused? me too. */
933 # define DATASTART ((ptr_t) &__nullarea)
934 # define DATAEND ((ptr_t) &_end)
939 # define MACH_TYPE "NS32K"
941 extern char **environ
;
942 # define DATASTART ((ptr_t)(&environ))
943 /* hideous kludge: environ is the first */
944 /* word in crt0.o, and delimits the start */
945 /* of the data segment, no matter which */
946 /* ld options were passed through. */
947 # define STACKBOTTOM ((ptr_t) 0xfffff000) /* for Encore */
951 # define MACH_TYPE "MIPS"
952 /* # define STACKBOTTOM ((ptr_t)0x7fff8000) sometimes also works. */
954 /* This was developed for a linuxce style platform. Probably */
955 /* needs to be tweaked for workstation class machines. */
956 # define OS_TYPE "LINUX"
957 extern int __data_start
;
958 # define DATASTART ((ptr_t)(&__data_start))
960 # define USE_GENERIC_PUSH_REGS 1
961 # define STACKBOTTOM 0x80000000
962 /* In many cases, this should probably use LINUX_STACKBOTTOM */
963 /* instead. But some kernel versions seem to give the wrong */
964 /* value from /proc. */
968 # define DATASTART (ptr_t)0x10000000
969 /* Could probably be slightly higher since */
970 /* startup code allocates lots of stuff. */
971 # define OS_TYPE "ULTRIX"
976 # define DATASTART (ptr_t)0x10000000
977 # define OS_TYPE "RISCOS"
978 # define ALIGNMENT 4 /* Required by hardware */
983 # define DATASTART ((ptr_t)(&_fdata))
985 # define HEAP_START (ptr_t)0x30000000
987 # define HEAP_START DATASTART
989 /* Lowest plausible heap address. */
990 /* In the MMAP case, we map there. */
991 /* In either case it is used to identify */
992 /* heap sections so they're not */
993 /* considered as roots. */
994 # define OS_TYPE "IRIX5"
995 # define MPROTECT_VDB
997 # define CPP_WORDSZ _MIPS_SZPTR
998 # define ALIGNMENT (_MIPS_SZPTR/8)
999 # if CPP_WORDSZ != 64
1000 # define ALIGN_DOUBLE
1003 # define ALIGNMENT 4
1004 # define ALIGN_DOUBLE
1006 # define DYNAMIC_LOADING
1011 # define MACH_TYPE "RS6000"
1012 # define ALIGNMENT 4
1013 # define DATASTART ((ptr_t)0x20000000)
1015 # define STACKBOTTOM ((ptr_t)((ulong)&errno))
1016 # define DYNAMIC_LOADING
1017 /* For really old versions of AIX, this may have to be removed. */
1021 /* OS is assumed to be HP/UX */
1022 # define MACH_TYPE "HP_PA"
1023 # define OS_TYPE "HPUX"
1025 # define CPP_WORDSZ 64
1026 # define ALIGNMENT 8
1028 # define CPP_WORDSZ 32
1029 # define ALIGNMENT 4
1030 # define ALIGN_DOUBLE
1032 extern int __data_start
;
1033 # define DATASTART ((ptr_t)(&__data_start))
1035 /* The following appears to work for 7xx systems running HP/UX */
1036 /* 9.xx Furthermore, it might result in much faster */
1037 /* collections than HEURISTIC2, which may involve scanning */
1038 /* segments that directly precede the stack. It is not the */
1039 /* default, since it may not work on older machine/OS */
1040 /* combinations. (Thanks to Raymond X.T. Nijssen for uncovering */
1042 # define STACKBOTTOM ((ptr_t) 0x7b033000) /* from /etc/conf/h/param.h */
1044 /* Gustavo Rodriguez-Rivera suggested changing HEURISTIC2 */
1045 /* to this. We'll probably do this on other platforms, too. */
1046 /* For now I'll use it where I can test it. */
1047 extern char ** environ
;
1048 # define STACKBOTTOM ((ptr_t)environ)
1050 # define STACK_GROWS_UP
1051 # define DYNAMIC_LOADING
1052 # ifndef HPUX_THREADS
1053 # define MPROTECT_VDB
1055 # include <unistd.h>
1056 # define GETPAGESIZE() sysconf(_SC_PAGE_SIZE)
1060 # define MACH_TYPE "ALPHA"
1061 # define ALIGNMENT 8
1062 # define USE_GENERIC_PUSH_REGS
1063 /* Gcc and probably the DEC/Compaq compiler spill pointers to preserved */
1064 /* fp registers in some cases when the target is a 21264. The assembly */
1065 /* code doesn't handle that yet, and version dependencies make that a */
1066 /* bit tricky. Do the easy thing for now. */
1068 # define OS_TYPE "OSF1"
1069 # define DATASTART ((ptr_t) 0x140000000)
1071 # define DATAEND ((ptr_t) &_end)
1073 /* Normally HEURISTIC2 is too conervative, since */
1074 /* the text segment immediately follows the stack. */
1075 /* Hence we give an upper pound. */
1077 # define HEURISTIC2_LIMIT ((ptr_t)((word)(&__start) & ~(getpagesize()-1)))
1078 # define CPP_WORDSZ 64
1079 # define MPROTECT_VDB
1080 # define DYNAMIC_LOADING
1083 # define OS_TYPE "LINUX"
1084 # define CPP_WORDSZ 64
1085 # define STACKBOTTOM ((ptr_t) 0x120000000)
1087 # define LINUX_DATA_START
1088 # define DYNAMIC_LOADING
1089 /* This doesn't work if the collector is in a dynamic library. */
1091 # define DATASTART ((ptr_t) 0x140000000)
1094 # define DATAEND (&_end)
1095 # define MPROTECT_VDB
1096 /* Has only been superficially tested. May not */
1097 /* work on all versions. */
1102 # define MACH_TYPE "IA64"
1103 # define ALIGN_DOUBLE
1104 /* Requires 16 byte alignment for malloc */
1105 # define ALIGNMENT 8
1106 # define USE_GENERIC_PUSH_REGS
1107 /* We need to get preserved registers in addition to register windows. */
1108 /* That's easiest to do with setjmp. */
1113 # define OS_TYPE "LINUX"
1114 # define CPP_WORDSZ 64
1115 /* This should really be done through /proc, but that */
1116 /* requires we run on an IA64 kernel. */
1117 # define STACKBOTTOM ((ptr_t) 0xa000000000000000l)
1118 /* We also need the base address of the register stack */
1119 /* backing store. There is probably a better way to */
1120 /* get that, too ... */
1121 # define BACKING_STORE_BASE ((ptr_t) 0x9fffffff80000000l)
1123 # define SEARCH_FOR_DATA_START
1124 # define DATASTART GC_data_start
1126 extern int data_start
;
1127 # define DATASTART ((ptr_t)(&data_start))
1129 # define DYNAMIC_LOADING
1130 # define MPROTECT_VDB
1131 /* Requires Linux 2.3.47 or later. */
1133 # define DATAEND (&_end)
1134 # define PREFETCH(x) \
1135 __asm__ (" lfetch [%0]": : "r"((void *)(x)))
1136 # define PREFETCH_FOR_WRITE(x) \
1137 __asm__ (" lfetch.excl [%0]": : "r"((void *)(x)))
1138 # define CLEAR_DOUBLE(x) \
1139 __asm__ (" stf.spill [%0]=f0": : "r"((void *)(x)))
1144 # define MACH_TYPE "M88K"
1145 # define ALIGNMENT 4
1146 # define ALIGN_DOUBLE
1149 # define OS_TYPE "CX_UX"
1150 # define DATASTART ((((word)&etext + 0x3fffff) & ~0x3fffff) + 0x10000)
1153 # define OS_TYPE "DGUX"
1154 extern char * GC_SysVGetDataStart();
1155 # define DATASTART (ptr_t)GC_SysVGetDataStart(0x10000, &etext)
1157 # define STACKBOTTOM ((char*)0xf0000000) /* determined empirically */
1161 # define MACH_TYPE "S370"
1162 # define OS_TYPE "UTS4"
1163 # define ALIGNMENT 4 /* Required by hardware */
1167 extern char * GC_SysVGetDataStart();
1168 # define DATASTART (ptr_t)GC_SysVGetDataStart(0x10000, &_etext)
1169 # define DATAEND (&_end)
1174 # define ALIGNMENT 4
1176 # define DATASTART ((ptr_t)(&_etext))
1181 # define CPP_WORDSZ 32
1182 # define MACH_TYPE "ARM32"
1183 # define ALIGNMENT 4
1185 # define OS_TYPE "NETBSD"
1188 # define DATASTART ((ptr_t)(&etext))
1189 # define USE_GENERIC_PUSH_REGS
1192 # define OS_TYPE "LINUX"
1195 # define STACK_GRAN 0x10000000
1196 # define USE_GENERIC_PUSH_REGS
1198 # define DYNAMIC_LOADING
1199 # include <features.h>
1200 # if defined(__GLIBC__) && __GLIBC__ >= 2
1201 # define LINUX_DATA_START
1203 extern char **__environ
;
1204 # define DATASTART ((ptr_t)(&__environ))
1205 /* hideous kludge: __environ is the first */
1206 /* word in crt0.o, and delimits the start */
1207 /* of the data segment, no matter which */
1208 /* ld options were passed through. */
1209 /* We could use _etext instead, but that */
1210 /* would include .rodata, which may */
1211 /* contain large read-only data tables */
1212 /* that we'd rather not scan. */
1215 # define DATAEND (&_end)
1218 # define DATASTART ((ptr_t)((((word) (&etext)) + 0xfff) & ~0xfff))
1223 #ifdef LINUX_DATA_START
1224 /* Some Linux distributions arrange to define __data_start. Some */
1225 /* define data_start as a weak symbol. The latter is technically */
1226 /* broken, since the user program may define data_start, in which */
1227 /* case we lose. Nonetheless, we try both, prefering __data_start. */
1228 /* We assume gcc. */
1229 # pragma weak __data_start
1230 extern int __data_start
;
1231 # pragma weak data_start
1232 extern int data_start
;
1233 # define DATASTART ((ptr_t)(&__data_start != 0? &__data_start : &data_start))
1236 # ifndef STACK_GROWS_UP
1237 # define STACK_GROWS_DOWN
1241 # define CPP_WORDSZ 32
1250 # define DATAEND (&end)
1253 # if defined(SVR4) && !defined(GETPAGESIZE)
1254 # include <unistd.h>
1255 # define GETPAGESIZE() sysconf(_SC_PAGESIZE)
1258 # ifndef GETPAGESIZE
1259 # if defined(SUNOS5) || defined(IRIX5)
1260 # include <unistd.h>
1262 # define GETPAGESIZE() getpagesize()
1265 # if defined(SUNOS5) || defined(DRSNX) || defined(UTS4)
1266 /* OS has SVR4 generic features. Probably others also qualify. */
1270 # if defined(SUNOS5) || defined(DRSNX)
1271 /* OS has SUNOS5 style semi-undocumented interface to dynamic */
1274 /* OS has SUNOS5 style signal handlers. */
1282 # if CPP_WORDSZ != 32 && CPP_WORDSZ != 64
1287 # undef DYNAMIC_LOADING
1292 # undef MPROTECT_VDB
1297 /* Postponed for now. */
1299 # undef MPROTECT_VDB
1302 # ifdef SMALL_CONFIG
1303 /* Presumably not worth the space it takes. */
1305 # undef MPROTECT_VDB
1309 # undef MPROTECT_VDB /* Can't deal with address space holes. */
1312 # if !defined(PCR_VDB) && !defined(PROC_VDB) && !defined(MPROTECT_VDB)
1313 # define DEFAULT_VDB
1317 # define PREFETCH(x)
1318 # define NO_PREFETCH
1321 # ifndef PREFETCH_FOR_WRITE
1322 # define PREFETCH_FOR_WRITE(x)
1323 # define NO_PREFETCH_FOR_WRITE
1326 # ifndef CACHE_LINE_SIZE
1327 # define CACHE_LINE_SIZE 32 /* Wild guess */
1330 # ifndef CLEAR_DOUBLE
1331 # define CLEAR_DOUBLE(x) \
1332 ((word*)x)[0] = 0; \
1334 # endif /* CLEAR_DOUBLE */
1336 # if defined(_SOLARIS_PTHREADS) && !defined(SOLARIS_THREADS)
1337 # define SOLARIS_THREADS
1339 # if defined(IRIX_THREADS) && !defined(IRIX5)
1340 --> inconsistent configuration
1342 # if defined(IRIX_JDK_THREADS) && !defined(IRIX5)
1343 --> inconsistent configuration
1345 # if defined(LINUX_THREADS) && !defined(LINUX)
1346 --> inconsistent configuration
1348 # if defined(SOLARIS_THREADS) && !defined(SUNOS5)
1349 --> inconsistent configuration
1351 # if defined(HPUX_THREADS) && !defined(HPUX)
1352 --> inconsistent configuration
1354 # if defined(PCR) || defined(SRC_M3) || \
1355 defined(SOLARIS_THREADS) || defined(WIN32_THREADS) || \
1356 defined(IRIX_THREADS) || defined(LINUX_THREADS) || \
1357 defined(IRIX_JDK_THREADS) || defined(HPUX_THREADS)
1361 # if defined(HP_PA) || defined(M88K) || defined(POWERPC) \
1362 || (defined(I386) && defined(OS2)) || defined(UTS4) || defined(LINT)
1363 /* Use setjmp based hack to mark from callee-save registers. */
1364 # define USE_GENERIC_PUSH_REGS
1366 # if defined(SPARC) && !defined(LINUX)
1367 # define SAVE_CALL_CHAIN
1368 # define ASM_CLEAR_CODE /* Stack clearing is crucial, and we */
1369 /* include assembly code to do it well. */
1372 # if defined(LINUX) && !defined(POWERPC)
1375 # include <linux/version.h>
1376 # if (LINUX_VERSION_CODE <= 0x10400)
1377 /* Ugly hack to get struct sigcontext_struct definition. Required */
1378 /* for some early 1.3.X releases. Will hopefully go away soon. */
1379 /* in some later Linux releases, asm/sigcontext.h may have to */
1380 /* be included instead. */
1382 # include <asm/signal.h>
1388 /* Kernels prior to 2.1.1 defined struct sigcontext_struct instead of */
1389 /* struct sigcontext. libc6 (glibc2) uses "struct sigcontext" in */
1390 /* prototypes, so we have to include the top-level sigcontext.h to */
1391 /* make sure the former gets defined to be the latter if appropriate. */
1392 # include <features.h>
1394 # if 2 == __GLIBC__ && 0 == __GLIBC_MINOR__
1395 /* glibc 2.1 no longer has sigcontext.h. But signal.h */
1396 /* has the right declaration for glibc 2.1. */
1397 # include <sigcontext.h>
1398 # endif /* 0 == __GLIBC_MINOR__ */
1399 # else /* not 2 <= __GLIBC__ */
1400 /* libc5 doesn't have <sigcontext.h>: go directly with the kernel */
1401 /* one. Check LINUX_VERSION_CODE to see which we should reference. */
1402 # include <asm/sigcontext.h>
1403 # endif /* 2 <= __GLIBC__ */
1406 # if !defined(OS2) && !defined(PCR) && !defined(AMIGA) && !defined(MACOS)
1407 # include <sys/types.h>
1408 # if !defined(MSWIN32) && !defined(SUNOS4)
1409 # include <unistd.h>
1413 # include <signal.h>
1415 /* Blatantly OS dependent routines, except for those that are related */
1416 /* to dynamic loading. */
1418 # if !defined(THREADS) && !defined(STACKBOTTOM) && defined(HEURISTIC2)
1419 # define NEED_FIND_LIMIT
1422 # if defined(IRIX_THREADS) || defined(HPUX_THREADS)
1423 # define NEED_FIND_LIMIT
1426 # if (defined(SUNOS4) && defined(DYNAMIC_LOADING)) && !defined(PCR)
1427 # define NEED_FIND_LIMIT
1430 # if (defined(SVR4) || defined(AUX) || defined(DGUX)) && !defined(PCR)
1431 # define NEED_FIND_LIMIT
1434 # if defined(LINUX) && \
1435 (defined(POWERPC) || defined(SPARC) || defined(ALPHA) || defined(IA64) \
1437 # define NEED_FIND_LIMIT
1440 #ifdef NEED_FIND_LIMIT
1441 # include <setjmp.h>
1445 # include <machine/trap.h>
1449 # include <proto/exec.h>
1450 # include <proto/dos.h>
1451 # include <dos/dosextens.h>
1452 # include <workbench/startup.h>
1456 # define WIN32_LEAN_AND_MEAN
1458 # include <windows.h>
1462 # include <Processes.h>
1466 # include <sys/uio.h>
1467 # include <malloc.h> /* for locking */
1470 # include <sys/types.h>
1471 # include <sys/mman.h>
1472 # include <sys/stat.h>
1477 # include <sys/siginfo.h>
1480 # define setjmp(env) sigsetjmp(env, 1)
1481 # define longjmp(env, val) siglongjmp(env, val)
1482 # define jmp_buf sigjmp_buf
1486 /* Apparently necessary for djgpp 2.01. May casuse problems with */
1487 /* other versions. */
1488 typedef long unsigned int caddr_t
;
1492 # include "il/PCR_IL.h"
1493 # include "th/PCR_ThCtl.h"
1494 # include "mm/PCR_MM.h"
1497 #if !defined(NO_EXECUTE_PERMISSION)
1498 # define OPT_PROT_EXEC PROT_EXEC
1500 # define OPT_PROT_EXEC 0
1505 # include <stddef.h>
1507 # if !defined(__IBMC__) && !defined(__WATCOMC__) /* e.g. EMX */
1509 # else /* IBM's compiler */
1511 /* A kludge to get around what appears to be a header file bug */
1513 # define WORD unsigned short
1516 # define DWORD unsigned long
1520 # include <newexe.h>
1521 # include <exe386.h>
1523 # endif /* __IBMC__ */
1525 # define INCL_DOSEXCEPTIONS
1526 # define INCL_DOSPROCESS
1527 # define INCL_DOSERRORS
1528 # define INCL_DOSMODULEMGR
1529 # define INCL_DOSMEMMGR
1535 * Find the base of the stack.
1536 * Used only in single-threaded environment.
1537 * With threads, GC_mark_roots needs to know how to do this.
1538 * Called with allocator lock held.
1541 # define is_writable(prot) ((prot) == PAGE_READWRITE \
1542 || (prot) == PAGE_WRITECOPY \
1543 || (prot) == PAGE_EXECUTE_READWRITE \
1544 || (prot) == PAGE_EXECUTE_WRITECOPY)
1545 /* Return the number of bytes that are writable starting at p. */
1546 /* The pointer p is assumed to be page aligned. */
1547 /* If base is not 0, *base becomes the beginning of the */
1548 /* allocation region containing p. */
1549 static word
GC_get_writable_length(ptr_t p
, ptr_t
*base
)
1551 MEMORY_BASIC_INFORMATION buf
;
1555 result
= VirtualQuery(p
, &buf
, sizeof(buf
));
1556 if (result
!= sizeof(buf
)) ABORT("Weird VirtualQuery result");
1557 if (base
!= 0) *base
= (ptr_t
)(buf
.AllocationBase
);
1558 protect
= (buf
.Protect
& ~(PAGE_GUARD
| PAGE_NOCACHE
));
1559 if (!is_writable(protect
)) {
1562 if (buf
.State
!= MEM_COMMIT
) return(0);
1563 return(buf
.RegionSize
);
1566 void *scm_get_stack_base()
1569 ptr_t sp
= (ptr_t
)(&dummy
);
1572 static word GC_page_size
= 0;
1573 if (!GC_page_size
) {
1574 SYSTEM_INFO sysinfo
;
1575 GetSystemInfo(&sysinfo
);
1576 GC_page_size
= sysinfo
.dwPageSize
;
1578 trunc_sp
= (ptr_t
)((word
)sp
& ~(GC_page_size
- 1));
1579 size
= GC_get_writable_length(trunc_sp
, 0);
1580 return(trunc_sp
+ size
);
1588 void *scm_get_stack_base()
1593 if (DosGetInfoBlocks(&ptib
, &ppib
) != NO_ERROR
) {
1594 GC_err_printf0("DosGetInfoBlocks failed\n");
1595 ABORT("DosGetInfoBlocks failed\n");
1597 return((ptr_t
)(ptib
-> tib_pstacklimit
));
1604 void *scm_get_stack_base()
1606 struct Process
*proc
= (struct Process
*)SysBase
->ThisTask
;
1608 /* Reference: Amiga Guru Book Pages: 42,567,574 */
1609 if (proc
->pr_Task
.tc_Node
.ln_Type
==NT_PROCESS
1610 && proc
->pr_CLI
!= NULL
) {
1611 /* first ULONG is StackSize */
1612 /*longPtr = proc->pr_ReturnAddr;
1613 size = longPtr[0];*/
1615 return (char *)proc
->pr_ReturnAddr
+ sizeof(ULONG
);
1617 return (char *)proc
->pr_Task
.tc_SPUpper
;
1621 #if 0 /* old version */
1622 void *scm_get_stack_base()
1624 extern struct WBStartup
*_WBenchMsg
;
1626 extern long __stack
;
1628 struct Process
*proc
;
1629 struct CommandLineInterface
*cli
;
1632 if ((task
= FindTask(0)) == 0) {
1633 GC_err_puts("Cannot find own task structure\n");
1634 ABORT("task missing");
1636 proc
= (struct Process
*)task
;
1637 cli
= BADDR(proc
->pr_CLI
);
1639 if (_WBenchMsg
!= 0 || cli
== 0) {
1640 size
= (char *)task
->tc_SPUpper
- (char *)task
->tc_SPLower
;
1642 size
= cli
->cli_DefaultStack
* 4;
1644 return (ptr_t
)(__base
+ GC_max(size
, __stack
));
1648 # else /* !AMIGA, !OS2, ... */
1650 # ifdef NEED_FIND_LIMIT
1651 /* Some tools to implement HEURISTIC2 */
1652 # define MIN_PAGE_SIZE 256 /* Smallest conceivable page size, bytes */
1653 /* static */ jmp_buf GC_jmp_buf
;
1656 static void GC_fault_handler(sig
)
1659 longjmp(GC_jmp_buf
, 1);
1663 typedef void (*handler
)(int);
1665 typedef void (*handler
)();
1668 # if defined(SUNOS5SIGS) || defined(IRIX5) || defined(OSF1)
1669 static struct sigaction old_segv_act
;
1670 # if defined(_sigargs) || defined(HPUX) /* !Irix6.x */
1671 static struct sigaction old_bus_act
;
1674 static handler old_segv_handler
, old_bus_handler
;
1677 static void GC_setup_temporary_fault_handler()
1679 # if defined(SUNOS5SIGS) || defined(IRIX5) || defined(OSF1)
1680 struct sigaction act
;
1682 act
.sa_handler
= GC_fault_handler
;
1683 act
.sa_flags
= SA_RESTART
| SA_NODEFER
;
1684 /* The presence of SA_NODEFER represents yet another gross */
1685 /* hack. Under Solaris 2.3, siglongjmp doesn't appear to */
1686 /* interact correctly with -lthread. We hide the confusion */
1687 /* by making sure that signal handling doesn't affect the */
1690 (void) sigemptyset(&act
.sa_mask
);
1691 # ifdef IRIX_THREADS
1692 /* Older versions have a bug related to retrieving and */
1693 /* and setting a handler at the same time. */
1694 (void) sigaction(SIGSEGV
, 0, &old_segv_act
);
1695 (void) sigaction(SIGSEGV
, &act
, 0);
1697 (void) sigaction(SIGSEGV
, &act
, &old_segv_act
);
1698 # if defined(IRIX5) && defined(_sigargs) /* Irix 5.x, not 6.x */ \
1700 /* Under Irix 5.x or HP/UX, we may get SIGBUS. */
1701 /* Pthreads doesn't exist under Irix 5.x, so we */
1702 /* don't have to worry in the threads case. */
1703 (void) sigaction(SIGBUS
, &act
, &old_bus_act
);
1705 # endif /* IRIX_THREADS */
1707 old_segv_handler
= signal(SIGSEGV
, GC_fault_handler
);
1709 old_bus_handler
= signal(SIGBUS
, GC_fault_handler
);
1714 static void GC_reset_fault_handler()
1716 # if defined(SUNOS5SIGS) || defined(IRIX5) || defined(OSF1)
1717 (void) sigaction(SIGSEGV
, &old_segv_act
, 0);
1718 # if defined(IRIX5) && defined(_sigargs) /* Irix 5.x, not 6.x */ \
1720 (void) sigaction(SIGBUS
, &old_bus_act
, 0);
1723 (void) signal(SIGSEGV
, old_segv_handler
);
1725 (void) signal(SIGBUS
, old_bus_handler
);
1730 /* Single argument version, robust against whole program analysis. */
1735 static VOLATILE word sink
;
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
;
1774 #ifdef LINUX_STACKBOTTOM
1776 #include <sys/types.h>
1777 #include <sys/stat.h>
1780 # define STAT_SKIP 27 /* Number of fields preceding startstack */
1781 /* field in /proc/self/stat */
1783 static ptr_t
GC_linux_stack_base(void)
1785 /* We read the stack base value from /proc/self/stat. We do this */
1786 /* using direct I/O system calls in order to avoid calling malloc */
1787 /* in case REDIRECT_MALLOC is defined. */
1788 # define STAT_BUF_SIZE 4096
1790 # define STAT_READ __real_read
1792 # define STAT_READ read
1794 char stat_buf
[STAT_BUF_SIZE
];
1798 size_t i
, buf_offset
= 0;
1800 f
= open("/proc/self/stat", O_RDONLY
);
1801 if (f
< 0 || STAT_READ(f
, stat_buf
, STAT_BUF_SIZE
) < 2 * STAT_SKIP
) {
1802 ABORT("Couldn't read /proc/self/stat");
1804 c
= stat_buf
[buf_offset
++];
1805 /* Skip the required number of fields. This number is hopefully */
1806 /* constant across all Linux implementations. */
1807 for (i
= 0; i
< STAT_SKIP
; ++i
) {
1808 while (isspace(c
)) c
= stat_buf
[buf_offset
++];
1809 while (!isspace(c
)) c
= stat_buf
[buf_offset
++];
1811 while (isspace(c
)) c
= stat_buf
[buf_offset
++];
1812 while (isdigit(c
)) {
1815 c
= stat_buf
[buf_offset
++];
1818 if (result
< 0x10000000) ABORT("Absurd stack bottom value");
1819 return (ptr_t
)result
;
1822 #endif /* LINUX_STACKBOTTOM */
1824 void *scm_get_stack_base()
1827 void *result
= &dummy
; /* initialize to silence compiler */
1829 # define STACKBOTTOM_ALIGNMENT_M1 ((word)STACK_GRAN - 1)
1832 return(STACKBOTTOM
);
1835 # ifdef STACK_GROWS_DOWN
1836 result
= (ptr_t
)((((word
)(&dummy
))
1837 + STACKBOTTOM_ALIGNMENT_M1
)
1838 & ~STACKBOTTOM_ALIGNMENT_M1
);
1840 result
= (ptr_t
)(((word
)(&dummy
))
1841 & ~STACKBOTTOM_ALIGNMENT_M1
);
1843 # endif /* HEURISTIC1 */
1844 # ifdef LINUX_STACKBOTTOM
1845 result
= GC_linux_stack_base();
1848 # ifdef STACK_GROWS_DOWN
1849 result
= GC_find_limit((ptr_t
)(&dummy
), TRUE
);
1850 # ifdef HEURISTIC2_LIMIT
1851 if (result
> HEURISTIC2_LIMIT
1852 && (ptr_t
)(&dummy
) < HEURISTIC2_LIMIT
) {
1853 result
= HEURISTIC2_LIMIT
;
1857 result
= GC_find_limit((ptr_t
)(&dummy
), FALSE
);
1858 # ifdef HEURISTIC2_LIMIT
1859 if (result
< HEURISTIC2_LIMIT
1860 && (ptr_t
)(&dummy
) > HEURISTIC2_LIMIT
) {
1861 result
= HEURISTIC2_LIMIT
;
1866 # endif /* HEURISTIC2 */
1867 # ifdef STACK_GROWS_DOWN
1868 if (result
== 0) result
= (ptr_t
)(signed_word
)(-sizeof(ptr_t
));
1871 # endif /* STACKBOTTOM */
1874 # endif /* ! AMIGA */
1876 # endif /* ! MSWIN32 */