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, 2002 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.
29 #include "libguile/gc.h"
30 #include "libguile/scmconfig.h"
32 #ifdef HAVE_LIBC_STACK_END
34 extern void *__libc_stack_end
;
39 return __libc_stack_end
;
44 #define ABORT(msg) abort ()
46 typedef char * ptr_t
; /* A generic pointer to which we can add */
47 /* byte displacements. */
48 /* Preferably identical to caddr_t, if it */
51 /* Define word and signed_word to be unsigned and signed types of the */
52 /* size as char * or void *. There seems to be no way to do this */
53 /* even semi-portably. The following is probably no better/worse */
54 /* than almost anything else. */
55 /* The ANSI standard suggests that size_t and ptr_diff_t might be */
56 /* better choices. But those appear to have incorrect definitions */
57 /* on may systems. Notably "typedef int size_t" seems to be both */
58 /* frequent and WRONG. */
59 typedef unsigned long GC_word
;
60 typedef long GC_signed_word
;
63 typedef GC_signed_word signed_word
;
71 # if !(defined( sony_news ) )
74 # define VOLATILE volatile
82 /* Machine dependent parameters. Some tuning parameters can be found */
83 /* near the top of gc_private.h. */
85 /* Machine specific parts contributed by various people. See README file. */
87 /* First a unified test for Linux: */
88 # if defined(linux) || defined(__linux__)
92 /* Determine the machine type: */
93 # if defined(sun) && defined(mc68000)
96 # define mach_type_known
98 # if defined(hp9000s300)
101 # define mach_type_known
103 # if defined(__OpenBSD__) && defined(m68k)
106 # define mach_type_known
108 # if defined(__OpenBSD__) && defined(__sparc__)
111 # define mach_type_known
113 # if defined(__NetBSD__) && defined(m68k)
116 # define mach_type_known
118 # if defined(__NetBSD__) && defined(arm32)
121 # define mach_type_known
123 # if defined(__NetBSD__) && defined(__sparc__)
126 # define mach_type_known
135 # define mach_type_known
137 # if defined(mips) || defined(__mips)
140 # if defined(ultrix) || defined(__ultrix) || defined(__NetBSD__)
143 # if defined(_SYSTYPE_SVR4) || defined(SYSTYPE_SVR4) \
144 || defined(__SYSTYPE_SVR4__)
145 # define IRIX5 /* or IRIX 6.X */
147 # define RISCOS /* or IRIX 4.X */
151 # define mach_type_known
153 # if defined(sequent) && defined(i386)
156 # define mach_type_known
158 # if defined(sun) && defined(i386)
161 # define mach_type_known
163 # if (defined(__OS2__) || defined(__EMX__)) && defined(__32BIT__)
166 # define mach_type_known
170 # define mach_type_known
172 # if defined(sun) && (defined(sparc) || defined(__sparc))
174 /* Test for SunOS 5.x */
181 # define mach_type_known
183 # if defined(sparc) && defined(unix) && !defined(sun) && !defined(linux) \
184 && !defined(__OpenBSD__)
187 # define mach_type_known
191 # define mach_type_known
193 # if defined(_M_XENIX) && defined(_M_SYSV) && defined(_M_I386)
194 /* The above test may need refinement */
196 # if defined(_SCO_ELF)
201 # define mach_type_known
203 # if defined(_AUX_SOURCE)
206 # define mach_type_known
208 # if defined(_PA_RISC1_0) || defined(_PA_RISC1_1) || defined(_PA_RISC2_0) \
209 || defined(hppa) || defined(__hppa__)
214 # define mach_type_known
216 # if defined(LINUX) && (defined(i386) || defined(__i386__))
218 # define mach_type_known
220 # if defined(LINUX) && (defined(__ia64__) || defined(__ia64))
222 # define mach_type_known
224 # if defined(LINUX) && defined(powerpc)
226 # define mach_type_known
228 # if defined(LINUX) && defined(__mc68000__)
230 # define mach_type_known
232 # if defined(LINUX) && (defined(sparc) || defined(__sparc__))
234 # define mach_type_known
236 # if defined(LINUX) && defined(arm)
238 # define mach_type_known
240 # if defined(__alpha) || defined(__alpha__)
243 # define OSF1 /* a.k.a Digital Unix */
245 # define mach_type_known
247 # if defined(_AMIGA) && !defined(AMIGA)
252 # define mach_type_known
254 # if defined(THINK_C) || defined(__MWERKS__) && !defined(__powerc)
257 # define mach_type_known
259 # if defined(__MWERKS__) && defined(__powerc)
262 # define mach_type_known
267 # define mach_type_known
269 # if defined(NeXT) && defined(mc68000)
272 # define mach_type_known
274 # if defined(NeXT) && defined(i386)
277 # define mach_type_known
279 # if defined(__OpenBSD__) && (defined(i386) || defined(__i386__))
282 # define mach_type_known
284 # if defined(__FreeBSD__) && defined(i386)
287 # define mach_type_known
289 # if defined(__NetBSD__) && defined(i386)
292 # define mach_type_known
294 # if defined(bsdi) && defined(i386)
297 # define mach_type_known
299 # if !defined(mach_type_known) && defined(__386BSD__)
302 # define mach_type_known
304 # if defined(_CX_UX) && defined(_M88K)
307 # define mach_type_known
312 # define mach_type_known
314 # if (defined(_MSDOS) || defined(_MSC_VER)) && (_M_IX86 >= 300) \
315 || defined(_WIN32) && !defined(__CYGWIN32__) && !defined(__CYGWIN__)
317 # define MSWIN32 /* or Win32s */
318 # define mach_type_known
320 # if defined(__DJGPP__)
323 # define DJGPP /* MSDOS running the DJGPP port of GCC */
325 # define mach_type_known
327 # if defined(__CYGWIN32__) || defined(__CYGWIN__)
330 # define mach_type_known
332 # if defined(__MINGW32__)
335 # define mach_type_known
337 # if defined(__BORLANDC__)
340 # define mach_type_known
342 # if defined(_UTS) && !defined(mach_type_known)
345 # define mach_type_known
349 # define mach_type_known
352 # if defined(__WATCOMC__) && defined(__386__)
354 # if !defined(OS2) && !defined(MSWIN32) && !defined(DOS4GW)
355 # if defined(__OS2__)
358 # if defined(__WINDOWS_386__) || defined(__NT__)
365 # define mach_type_known
367 # if defined(__GNU__)
370 # define mach_type_known
373 /* Feel free to add more clauses here */
375 /* Or manually define the machine type here. A machine type is */
376 /* characterized by the architecture. Some */
377 /* machine types are further subdivided by OS. */
378 /* the macros ULTRIX, RISCOS, and BSD to distinguish. */
379 /* Note that SGI IRIX is treated identically to RISCOS. */
380 /* SYSV on an M68K actually means A/UX. */
381 /* The distinction in these cases is usually the stack starting address */
382 # ifndef mach_type_known
383 --> unknown machine type
385 /* Mapping is: M68K ==> Motorola 680X0 */
386 /* (SUNOS4,HP,NEXT, and SYSV (A/UX), */
387 /* MACOS and AMIGA variants) */
388 /* I386 ==> Intel 386 */
389 /* (SEQUENT, OS2, SCO, LINUX, NETBSD, */
390 /* FREEBSD, THREE86BSD, MSWIN32, */
391 /* BSDI,SUNOS5, NEXT, other variants) */
392 /* NS32K ==> Encore Multimax */
393 /* MIPS ==> R2000 or R3000 */
394 /* (RISCOS, ULTRIX variants) */
395 /* VAX ==> DEC VAX */
396 /* (BSD, ULTRIX variants) */
397 /* RS6000 ==> IBM RS/6000 AIX3.X */
398 /* RT ==> IBM PC/RT */
399 /* HP_PA ==> HP9000/700 & /800 */
401 /* SPARC ==> SPARC under SunOS */
402 /* (SUNOS4, SUNOS5, */
403 /* DRSNX variants) */
404 /* ALPHA ==> DEC Alpha */
405 /* (OSF1 and LINUX variants) */
406 /* M88K ==> Motorola 88XX0 */
407 /* (CX_UX and DGUX) */
408 /* S370 ==> 370-like machine */
409 /* running Amdahl UTS4 */
410 /* ARM32 ==> Intel StrongARM */
411 /* IA64 ==> Intel IA64 */
416 * For each architecture and OS, the following need to be defined:
418 * CPP_WORD_SZ is a simple integer constant representing the word size.
419 * in bits. We assume byte addressibility, where a byte has 8 bits.
420 * We also assume CPP_WORD_SZ is either 32 or 64.
421 * (We care about the length of pointers, not hardware
422 * bus widths. Thus a 64 bit processor with a C compiler that uses
423 * 32 bit pointers should use CPP_WORD_SZ of 32, not 64. Default is 32.)
425 * MACH_TYPE is a string representation of the machine type.
426 * OS_TYPE is analogous for the OS.
428 * ALIGNMENT is the largest N, such that
429 * all pointer are guaranteed to be aligned on N byte boundaries.
430 * defining it to be 1 will always work, but perform poorly.
432 * DATASTART is the beginning of the data segment.
433 * On UNIX systems, the collector will scan the area between DATASTART
434 * and DATAEND for root pointers.
436 * DATAEND, if not &end.
438 * ALIGN_DOUBLE of GC_malloc should return blocks aligned to twice
441 * STACKBOTTOM is the cool end of the stack, which is usually the
442 * highest address in the stack.
443 * Under PCR or OS/2, we have other ways of finding thread stacks.
444 * For each machine, the following should:
445 * 1) define SCM_STACK_GROWS_UP if the stack grows toward higher addresses, and
446 * 2) define exactly one of
447 * STACKBOTTOM (should be defined to be an expression)
450 * If either of the last two macros are defined, then STACKBOTTOM is computed
451 * during collector startup using one of the following two heuristics:
452 * HEURISTIC1: Take an address inside GC_init's frame, and round it up to
453 * the next multiple of STACK_GRAN.
454 * HEURISTIC2: Take an address inside GC_init's frame, increment it repeatedly
455 * in small steps (decrement if SCM_STACK_GROWS_UP), and read the value
456 * at each location. Remember the value when the first
457 * Segmentation violation or Bus error is signalled. Round that
458 * to the nearest plausible page boundary, and use that instead
461 * Gustavo Rodriguez-Rivera points out that on most (all?) Unix machines,
462 * the value of environ is a pointer that can serve as STACKBOTTOM.
463 * I expect that HEURISTIC2 can be replaced by this approach, which
464 * interferes far less with debugging.
466 * If no expression for STACKBOTTOM can be found, and neither of the above
467 * heuristics are usable, the collector can still be used with all of the above
468 * undefined, provided one of the following is done:
469 * 1) GC_mark_roots can be changed to somehow mark from the correct stack(s)
470 * without reference to STACKBOTTOM. This is appropriate for use in
471 * conjunction with thread packages, since there will be multiple stacks.
472 * (Allocating thread stacks in the heap, and treating them as ordinary
473 * heap data objects is also possible as a last resort. However, this is
474 * likely to introduce significant amounts of excess storage retention
475 * unless the dead parts of the thread stacks are periodically cleared.)
476 * 2) Client code may set GC_stackbottom before calling any GC_ routines.
477 * If the author of the client code controls the main program, this is
478 * easily accomplished by introducing a new main program, setting
479 * GC_stackbottom to the address of a local variable, and then calling
480 * the original main program. The new main program would read something
483 * # include "gc_private.h"
485 * main(argc, argv, envp)
487 * char **argv, **envp;
491 * GC_stackbottom = (ptr_t)(&dummy);
492 * return(real_main(argc, argv, envp));
496 * Each architecture may also define the style of virtual dirty bit
497 * implementation to be used:
498 * MPROTECT_VDB: Write protect the heap and catch faults.
499 * PROC_VDB: Use the SVR4 /proc primitives to read dirty bits.
501 * An architecture may define DYNAMIC_LOADING if dynamic_load.c
502 * defined GC_register_dynamic_libraries() for the architecture.
504 * An architecture may define PREFETCH(x) to preload the cache with *x.
505 * This defaults to a no-op.
507 * PREFETCH_FOR_WRITE(x) is used if *x is about to be written.
509 * An architecture may also define CLEAR_DOUBLE(x) to be a fast way to
510 * clear the two words at GC_malloc-aligned address x. By default,
511 * word stores of 0 are used instead.
515 # define STACK_GRAN 0x1000000
517 # define MACH_TYPE "M68K"
520 # define OS_TYPE "OPENBSD"
523 # define DATASTART ((ptr_t)(&etext))
526 # define OS_TYPE "NETBSD"
529 # define DATASTART ((ptr_t)(&etext))
532 # define OS_TYPE "LINUX"
533 # define STACKBOTTOM ((ptr_t)0xf0000000)
534 # define MPROTECT_VDB
536 # define DYNAMIC_LOADING
537 extern char **__environ
;
538 # define DATASTART ((ptr_t)(&__environ))
539 /* hideous kludge: __environ is the first */
540 /* word in crt0.o, and delimits the start */
541 /* of the data segment, no matter which */
542 /* ld options were passed through. */
543 /* We could use _etext instead, but that */
544 /* would include .rodata, which may */
545 /* contain large read-only data tables */
546 /* that we'd rather not scan. */
548 # define DATAEND (&_end)
551 # define DATASTART ((ptr_t)((((word) (&etext)) + 0xfff) & ~0xfff))
555 # define OS_TYPE "SUNOS4"
557 # define DATASTART ((ptr_t)((((word) (&etext)) + 0x1ffff) & ~0x1ffff))
558 # define HEURISTIC1 /* differs */
559 # define DYNAMIC_LOADING
562 # define OS_TYPE "HP"
564 # define DATASTART ((ptr_t)((((word) (&etext)) + 0xfff) & ~0xfff))
565 # define STACKBOTTOM ((ptr_t) 0xffeffffc)
566 /* empirically determined. seems to work. */
568 # define GETPAGESIZE() sysconf(_SC_PAGE_SIZE)
571 # define OS_TYPE "SYSV"
573 # define DATASTART ((ptr_t)((((word) (&etext)) + 0x3fffff) \
575 +((word)&etext & 0x1fff))
576 /* This only works for shared-text binaries with magic number 0413.
577 The other sorts of SysV binaries put the data at the end of the text,
578 in which case the default of &etext would work. Unfortunately,
579 handling both would require having the magic-number available.
582 # define STACKBOTTOM ((ptr_t)0xFFFFFFFE)
583 /* The stack starts at the top of memory, but */
584 /* 0x0 cannot be used as setjump_test complains */
585 /* that the stack direction is incorrect. Two */
586 /* bytes down from 0x0 should be safe enough. */
588 # include <sys/mmu.h>
589 # define GETPAGESIZE() PAGESIZE /* Is this still right? */
592 # define OS_TYPE "AMIGA"
593 /* STACKBOTTOM and DATASTART handled specially */
595 # define DATAEND /* not needed */
596 # define GETPAGESIZE() 4096
602 # define OS_TYPE "MACOS"
603 /* see os_dep.c for details of global data segments. */
604 # define STACKBOTTOM ((ptr_t) LMGetCurStackBase())
605 # define DATAEND /* not needed */
606 # define GETPAGESIZE() 4096
609 # define OS_TYPE "NEXT"
610 # define DATASTART ((ptr_t) get_etext())
611 # define STACKBOTTOM ((ptr_t) 0x4000000)
612 # define DATAEND /* not needed */
617 # define MACH_TYPE "POWERPC"
619 # define ALIGNMENT 2 /* Still necessary? Could it be 4? */
623 # define OS_TYPE "MACOS"
624 /* see os_dep.c for details of global data segments. */
625 # define STACKBOTTOM ((ptr_t) LMGetCurStackBase())
626 # define DATAEND /* not needed */
629 # define ALIGNMENT 4 /* Guess. Can someone verify? */
630 /* This was 2, but that didn't sound right. */
631 # define OS_TYPE "LINUX"
633 # define DYNAMIC_LOADING
635 # define STACK_GRAN 0x10000000
636 /* Stack usually starts at 0x80000000 */
637 # define LINUX_DATA_START
639 # define DATAEND (&_end)
643 # define OS_TYPE "MACOSX"
644 # define DATASTART ((ptr_t) get_etext())
645 # define STACKBOTTOM ((ptr_t) 0xc0000000)
646 # define DATAEND /* not needed */
651 # define MACH_TYPE "VAX"
652 # define ALIGNMENT 4 /* Pointers are longword aligned by 4.2 C compiler */
654 # define DATASTART ((ptr_t)(&etext))
656 # define OS_TYPE "BSD"
658 /* HEURISTIC2 may be OK, but it's hard to test. */
661 # define OS_TYPE "ULTRIX"
662 # define STACKBOTTOM ((ptr_t) 0x7fffc800)
667 # define MACH_TYPE "RT"
669 # define DATASTART ((ptr_t) 0x10000000)
670 # define STACKBOTTOM ((ptr_t) 0x1fffd800)
674 # define MACH_TYPE "SPARC"
675 # define ALIGNMENT 4 /* Required by hardware */
676 # define ALIGN_DOUBLE
679 # define OS_TYPE "SUNOS5"
682 extern char * GC_SysVGetDataStart();
683 # define DATASTART (ptr_t)GC_SysVGetDataStart(0x10000, &_etext)
684 # define DATAEND (&_end)
689 # define HEAP_START (ptr_t)0x40000000
691 # define HEAP_START DATAEND
694 /* HEURISTIC1 reportedly no longer works under 2.7. Thus we */
695 /* switched to HEURISTIC2, eventhough it creates some debugging */
699 # define GETPAGESIZE() sysconf(_SC_PAGESIZE)
700 /* getpagesize() appeared to be missing from at least one */
701 /* Solaris 5.4 installation. Weird. */
702 # define DYNAMIC_LOADING
705 # define OS_TYPE "SUNOS4"
706 /* [If you have a weak stomach, don't read this.] */
707 /* We would like to use: */
708 /* # define DATASTART ((ptr_t)((((word) (&etext)) + 0x1fff) & ~0x1fff)) */
709 /* This fails occasionally, due to an ancient, but very */
710 /* persistent ld bug. &etext is set 32 bytes too high. */
711 /* We instead read the text segment size from the a.out */
712 /* header, which happens to be mapped into our address space */
713 /* at the start of the text segment. The detective work here */
714 /* was done by Robert Ehrlich, Manuel Serrano, and Bernard */
715 /* Serpette of INRIA. */
716 /* This assumes ZMAGIC, i.e. demand-loadable executables. */
717 # define TEXTSTART 0x2000
718 # define DATASTART ((ptr_t)(*(int *)(TEXTSTART+0x4)+TEXTSTART))
719 # define MPROTECT_VDB
721 # define DYNAMIC_LOADING
724 # define CPP_WORDSZ 32
725 # define OS_TYPE "DRSNX"
726 extern char * GC_SysVGetDataStart();
728 # define DATASTART (ptr_t)GC_SysVGetDataStart(0x10000, &etext)
729 # define MPROTECT_VDB
730 # define STACKBOTTOM ((ptr_t) 0xdfff0000)
731 # define DYNAMIC_LOADING
734 # define OS_TYPE "LINUX"
736 # define LINUX_DATA_START
737 # define DYNAMIC_LOADING
739 Linux Sparc non elf
?
742 # define DATAEND (&_end)
744 # define STACKBOTTOM ((ptr_t) 0xf0000000)
747 # define OS_TYPE "OPENBSD"
748 # define STACKBOTTOM ((ptr_t) 0xf8000000)
749 # define DATASTART ((ptr_t)(&etext))
754 # define MACH_TYPE "I386"
755 # define ALIGNMENT 4 /* Appears to hold for all "32 bit" compilers */
756 /* except Borland. The -a4 option fixes */
758 /* Ivan Demakov: For Watcom the option is -zp4. */
759 # ifndef SMALL_CONFIG
760 # define ALIGN_DOUBLE /* Not strictly necessary, but may give speed */
761 /* improvement on Pentiums. */
764 # define OS_TYPE "SEQUENT"
766 # define DATASTART ((ptr_t)((((word) (&etext)) + 0xfff) & ~0xfff))
767 # define STACKBOTTOM ((ptr_t) 0x3ffff000)
770 # define OS_TYPE "SUNOS5"
771 extern int etext
, _start
;
772 extern char * GC_SysVGetDataStart();
773 # define DATASTART GC_SysVGetDataStart(0x1000, &etext)
774 # define STACKBOTTOM ((ptr_t)(&_start))
775 /** At least in Solaris 2.5, PROC_VDB gives wrong values for dirty bits. */
776 /*# define PROC_VDB*/
777 # define DYNAMIC_LOADING
782 # define HEAP_START (ptr_t)0x40000000
784 # define HEAP_START DATAEND
788 # define OS_TYPE "SCO"
790 # define DATASTART ((ptr_t)((((word) (&etext)) + 0x3fffff) \
792 +((word)&etext & 0xfff))
793 # define STACKBOTTOM ((ptr_t) 0x7ffffffc)
796 # define OS_TYPE "SCO_ELF"
798 # define DATASTART ((ptr_t)(&etext))
799 # define STACKBOTTOM ((ptr_t) 0x08048000)
800 # define DYNAMIC_LOADING
801 # define ELF_CLASS ELFCLASS32
804 # define OS_TYPE "LINUX"
805 # define LINUX_STACKBOTTOM
809 # define STACK_GRAN 0x10000000
810 /* STACKBOTTOM is usually 0xc0000000, but this changes with */
811 /* different kernel configurations. In particular, systems */
812 /* with 2GB physical memory will usually move the user */
813 /* address space limit, and hence initial SP to 0x80000000. */
815 # if !defined(LINUX_THREADS) || !defined(REDIRECT_MALLOC)
816 # define MPROTECT_VDB
818 /* We seem to get random errors in incremental mode, */
819 /* possibly because Linux threads is itself a malloc client */
820 /* and can't deal with the signals. */
823 # define DYNAMIC_LOADING
824 # ifdef UNDEFINED /* includes ro data */
826 # define DATASTART ((ptr_t)((((word) (&_etext)) + 0xfff) & ~0xfff))
828 # include <features.h>
829 # if defined(__GLIBC__) && __GLIBC__ >= 2
830 # define LINUX_DATA_START
832 extern char **__environ
;
833 # define DATASTART ((ptr_t)(&__environ))
834 /* hideous kludge: __environ is the first */
835 /* word in crt0.o, and delimits the start */
836 /* of the data segment, no matter which */
837 /* ld options were passed through. */
838 /* We could use _etext instead, but that */
839 /* would include .rodata, which may */
840 /* contain large read-only data tables */
841 /* that we'd rather not scan. */
844 # define DATAEND (&_end)
847 # define DATASTART ((ptr_t)((((word) (&etext)) + 0xfff) & ~0xfff))
849 # ifdef USE_I686_PREFETCH
850 # define PREFETCH(x) \
851 __asm__ __volatile__ (" prefetchnta %0": : "m"(*(char *)(x)))
852 /* Empirically prefetcht0 is much more effective at reducing */
853 /* cache miss stalls for the targetted load instructions. But it */
854 /* seems to interfere enough with other cache traffic that the net */
855 /* result is worse than prefetchnta. */
857 /* Using prefetches for write seems to have a slight negative */
858 /* impact on performance, at least for a PIII/500. */
859 # define PREFETCH_FOR_WRITE(x) \
860 __asm__ __volatile__ (" prefetcht0 %0": : "m"(*(char *)(x)))
863 # ifdef USE_3DNOW_PREFETCH
864 # define PREFETCH(x) \
865 __asm__ __volatile__ (" prefetch %0": : "m"(*(char *)(x)))
866 # define PREFETCH_FOR_WRITE(x)
867 __asm__
__volatile__ (" prefetchw %0": : "m"(*(char *)(x
)))
871 # define OS_TYPE "CYGWIN32"
872 extern int _data_start__
;
873 extern int _data_end__
;
874 extern int _bss_start__
;
875 extern int _bss_end__
;
876 /* For binutils 2.9.1, we have */
877 /* DATASTART = _data_start__ */
878 /* DATAEND = _bss_end__ */
879 /* whereas for some earlier versions it was */
880 /* DATASTART = _bss_start__ */
881 /* DATAEND = _data_end__ */
882 /* To get it right for both, we take the */
883 /* minumum/maximum of the two. */
884 # define MAX(x,y) ((x) > (y) ? (x) : (y))
885 # define MIN(x,y) ((x) < (y) ? (x) : (y))
886 # define DATASTART ((ptr_t) MIN(&_data_start__, &_bss_start__))
887 # define DATAEND ((ptr_t) MAX(&_data_end__, &_bss_end__))
889 # define STACK_GRAN 0x10000
893 # define OS_TYPE "OS2"
894 /* STACKBOTTOM and DATASTART are handled specially in */
895 /* os_dep.c. OS2 actually has the right */
897 # define DATAEND /* not needed */
900 # define OS_TYPE "MSWIN32"
901 /* STACKBOTTOM and DATASTART are handled specially in */
904 # define MPROTECT_VDB
906 # define DATAEND /* not needed */
909 # define OS_TYPE "DJGPP"
910 # include "stubinfo.h"
913 extern int __djgpp_stack_limit
;
914 # define DATASTART ((ptr_t)((((word) (&etext)) + 0x1ff) & ~0x1ff))
915 /* # define STACKBOTTOM ((ptr_t)((word) _stubinfo + _stubinfo->size \
917 # define STACKBOTTOM ((ptr_t)((word) __djgpp_stack_limit + _stklen))
918 /* This may not be right. */
921 # define OS_TYPE "OPENBSD"
924 # define OS_TYPE "FREEBSD"
925 # define MPROTECT_VDB
928 # define OS_TYPE "NETBSD"
931 # define OS_TYPE "THREE86BSD"
934 # define OS_TYPE "BSDI"
936 # if defined(OPENBSD) || defined(FREEBSD) || defined(NETBSD) \
937 || defined(THREE86BSD) || defined(BSDI)
940 # define DATASTART ((ptr_t)(&etext))
943 # define OS_TYPE "NEXT"
944 # define DATASTART ((ptr_t) get_etext())
945 # define STACKBOTTOM ((ptr_t)0xc0000000)
946 # define DATAEND /* not needed */
949 # define OS_TYPE "DOS4GW"
950 extern long __nullarea
;
952 extern char *_STACKTOP
;
953 /* Depending on calling conventions Watcom C either precedes
954 or does not precedes with undescore names of C-variables.
955 Make sure startup code variables always have the same names. */
956 #pragma aux __nullarea "*";
957 #pragma aux _end "*";
958 # define STACKBOTTOM ((ptr_t) _STACKTOP)
959 /* confused? me too. */
960 # define DATASTART ((ptr_t) &__nullarea)
961 # define DATAEND ((ptr_t) &_end)
964 # define OS_TYPE "GNU"
969 # define MACH_TYPE "NS32K"
971 extern char **environ
;
972 # define DATASTART ((ptr_t)(&environ))
973 /* hideous kludge: environ is the first */
974 /* word in crt0.o, and delimits the start */
975 /* of the data segment, no matter which */
976 /* ld options were passed through. */
977 # define STACKBOTTOM ((ptr_t) 0xfffff000) /* for Encore */
981 # define MACH_TYPE "MIPS"
982 /* # define STACKBOTTOM ((ptr_t)0x7fff8000) sometimes also works. */
984 /* This was developed for a linuxce style platform. Probably */
985 /* needs to be tweaked for workstation class machines. */
986 # define OS_TYPE "LINUX"
987 extern int __data_start
;
988 # define DATASTART ((ptr_t)(&__data_start))
990 # define USE_GENERIC_PUSH_REGS 1
991 # define STACKBOTTOM 0x80000000
992 /* In many cases, this should probably use LINUX_STACKBOTTOM */
993 /* instead. But some kernel versions seem to give the wrong */
994 /* value from /proc. */
998 # define DATASTART (ptr_t)0x10000000
999 /* Could probably be slightly higher since */
1000 /* startup code allocates lots of stuff. */
1001 # define OS_TYPE "ULTRIX"
1002 # define ALIGNMENT 4
1006 # define DATASTART (ptr_t)0x10000000
1007 # define OS_TYPE "RISCOS"
1008 # define ALIGNMENT 4 /* Required by hardware */
1013 # define DATASTART ((ptr_t)(&_fdata))
1015 # define HEAP_START (ptr_t)0x30000000
1017 # define HEAP_START DATASTART
1019 /* Lowest plausible heap address. */
1020 /* In the MMAP case, we map there. */
1021 /* In either case it is used to identify */
1022 /* heap sections so they're not */
1023 /* considered as roots. */
1024 # define OS_TYPE "IRIX5"
1025 # define MPROTECT_VDB
1027 # define CPP_WORDSZ _MIPS_SZPTR
1028 # define ALIGNMENT (_MIPS_SZPTR/8)
1029 # if CPP_WORDSZ != 64
1030 # define ALIGN_DOUBLE
1033 # define ALIGNMENT 4
1034 # define ALIGN_DOUBLE
1036 # define DYNAMIC_LOADING
1041 # define MACH_TYPE "RS6000"
1042 # define ALIGNMENT 4
1043 # define DATASTART ((ptr_t)0x20000000)
1045 # define STACKBOTTOM ((ptr_t)((ulong)&errno))
1046 # define DYNAMIC_LOADING
1047 /* For really old versions of AIX, this may have to be removed. */
1051 /* OS is assumed to be HP/UX */
1052 # define MACH_TYPE "HP_PA"
1053 # define OS_TYPE "HPUX"
1055 # define CPP_WORDSZ 64
1056 # define ALIGNMENT 8
1058 # define CPP_WORDSZ 32
1059 # define ALIGNMENT 4
1060 # define ALIGN_DOUBLE
1062 extern int __data_start
;
1063 # define DATASTART ((ptr_t)(&__data_start))
1065 /* The following appears to work for 7xx systems running HP/UX */
1066 /* 9.xx Furthermore, it might result in much faster */
1067 /* collections than HEURISTIC2, which may involve scanning */
1068 /* segments that directly precede the stack. It is not the */
1069 /* default, since it may not work on older machine/OS */
1070 /* combinations. (Thanks to Raymond X.T. Nijssen for uncovering */
1072 # define STACKBOTTOM ((ptr_t) 0x7b033000) /* from /etc/conf/h/param.h */
1074 /* Gustavo Rodriguez-Rivera suggested changing HEURISTIC2 */
1075 /* to this. We'll probably do this on other platforms, too. */
1076 /* For now I'll use it where I can test it. */
1077 extern char ** environ
;
1078 # define STACKBOTTOM ((ptr_t)environ)
1080 # ifndef SCM_STACK_GROWS_UP /* don't fight with scmconfig.h */
1081 # define SCM_STACK_GROWS_UP 1
1083 # define DYNAMIC_LOADING
1084 # ifndef HPUX_THREADS
1085 # define MPROTECT_VDB
1087 # include <unistd.h>
1088 # define GETPAGESIZE() sysconf(_SC_PAGE_SIZE)
1092 # define MACH_TYPE "ALPHA"
1093 # define ALIGNMENT 8
1094 # define USE_GENERIC_PUSH_REGS
1095 /* Gcc and probably the DEC/Compaq compiler spill pointers to preserved */
1096 /* fp registers in some cases when the target is a 21264. The assembly */
1097 /* code doesn't handle that yet, and version dependencies make that a */
1098 /* bit tricky. Do the easy thing for now. */
1100 # define OS_TYPE "OSF1"
1101 # define DATASTART ((ptr_t) 0x140000000)
1103 # define DATAEND ((ptr_t) &_end)
1105 /* Normally HEURISTIC2 is too conervative, since */
1106 /* the text segment immediately follows the stack. */
1107 /* Hence we give an upper pound. */
1109 # define HEURISTIC2_LIMIT ((ptr_t)((word)(&__start) & ~(getpagesize()-1)))
1110 # define CPP_WORDSZ 64
1111 # define MPROTECT_VDB
1112 # define DYNAMIC_LOADING
1115 # define OS_TYPE "LINUX"
1116 # define CPP_WORDSZ 64
1117 # define STACKBOTTOM ((ptr_t) 0x120000000)
1119 # define LINUX_DATA_START
1120 # define DYNAMIC_LOADING
1121 /* This doesn't work if the collector is in a dynamic library. */
1123 # define DATASTART ((ptr_t) 0x140000000)
1126 # define DATAEND (&_end)
1127 # define MPROTECT_VDB
1128 /* Has only been superficially tested. May not */
1129 /* work on all versions. */
1134 # define MACH_TYPE "IA64"
1135 # define ALIGN_DOUBLE
1136 /* Requires 16 byte alignment for malloc */
1137 # define ALIGNMENT 8
1138 # define USE_GENERIC_PUSH_REGS
1139 /* We need to get preserved registers in addition to register windows. */
1140 /* That's easiest to do with setjmp. */
1145 # define OS_TYPE "LINUX"
1146 # define CPP_WORDSZ 64
1147 /* This should really be done through /proc, but that */
1148 /* requires we run on an IA64 kernel. */
1149 # define STACKBOTTOM ((ptr_t) 0xa000000000000000l)
1150 /* We also need the base address of the register stack */
1151 /* backing store. There is probably a better way to */
1152 /* get that, too ... */
1153 # define BACKING_STORE_BASE ((ptr_t) 0x9fffffff80000000l)
1155 # define SEARCH_FOR_DATA_START
1156 # define DATASTART GC_data_start
1158 extern int data_start
;
1159 # define DATASTART ((ptr_t)(&data_start))
1161 # define DYNAMIC_LOADING
1162 # define MPROTECT_VDB
1163 /* Requires Linux 2.3.47 or later. */
1165 # define DATAEND (&_end)
1166 # define PREFETCH(x) \
1167 __asm__ (" lfetch [%0]": : "r"((void *)(x)))
1168 # define PREFETCH_FOR_WRITE(x) \
1169 __asm__ (" lfetch.excl [%0]": : "r"((void *)(x)))
1170 # define CLEAR_DOUBLE(x) \
1171 __asm__ (" stf.spill [%0]=f0": : "r"((void *)(x)))
1176 # define MACH_TYPE "M88K"
1177 # define ALIGNMENT 4
1178 # define ALIGN_DOUBLE
1181 # define OS_TYPE "CX_UX"
1182 # define DATASTART ((((word)&etext + 0x3fffff) & ~0x3fffff) + 0x10000)
1185 # define OS_TYPE "DGUX"
1186 extern char * GC_SysVGetDataStart();
1187 # define DATASTART (ptr_t)GC_SysVGetDataStart(0x10000, &etext)
1189 # define STACKBOTTOM ((char*)0xf0000000) /* determined empirically */
1193 # define MACH_TYPE "S370"
1194 # define OS_TYPE "UTS4"
1195 # define ALIGNMENT 4 /* Required by hardware */
1199 extern char * GC_SysVGetDataStart();
1200 # define DATASTART (ptr_t)GC_SysVGetDataStart(0x10000, &_etext)
1201 # define DATAEND (&_end)
1206 # define ALIGNMENT 4
1208 # define DATASTART ((ptr_t)(&_etext))
1213 # define CPP_WORDSZ 32
1214 # define MACH_TYPE "ARM32"
1215 # define ALIGNMENT 4
1217 # define OS_TYPE "NETBSD"
1220 # define DATASTART ((ptr_t)(&etext))
1221 # define USE_GENERIC_PUSH_REGS
1224 # define OS_TYPE "LINUX"
1227 # define STACK_GRAN 0x10000000
1228 # define USE_GENERIC_PUSH_REGS
1230 # define DYNAMIC_LOADING
1231 # include <features.h>
1232 # if defined(__GLIBC__) && __GLIBC__ >= 2
1233 # define LINUX_DATA_START
1235 extern char **__environ
;
1236 # define DATASTART ((ptr_t)(&__environ))
1237 /* hideous kludge: __environ is the first */
1238 /* word in crt0.o, and delimits the start */
1239 /* of the data segment, no matter which */
1240 /* ld options were passed through. */
1241 /* We could use _etext instead, but that */
1242 /* would include .rodata, which may */
1243 /* contain large read-only data tables */
1244 /* that we'd rather not scan. */
1247 # define DATAEND (&_end)
1250 # define DATASTART ((ptr_t)((((word) (&etext)) + 0xfff) & ~0xfff))
1255 #ifdef LINUX_DATA_START
1256 /* Some Linux distributions arrange to define __data_start. Some */
1257 /* define data_start as a weak symbol. The latter is technically */
1258 /* broken, since the user program may define data_start, in which */
1259 /* case we lose. Nonetheless, we try both, prefering __data_start. */
1260 /* We assume gcc. */
1261 # pragma weak __data_start
1262 extern int __data_start
;
1263 # pragma weak data_start
1264 extern int data_start
;
1265 # define DATASTART ((ptr_t)(&__data_start != 0? &__data_start : &data_start))
1268 # if SCM_STACK_GROWS_UP
1269 # define STACK_GROWS_DOWN 0
1271 # define STACK_GROWS_DOWN 1
1275 # define CPP_WORDSZ 32
1284 # define DATAEND (&end)
1287 # if defined(SVR4) && !defined(GETPAGESIZE)
1288 # include <unistd.h>
1289 # define GETPAGESIZE() sysconf(_SC_PAGESIZE)
1292 # ifndef GETPAGESIZE
1293 # if defined(SUNOS5) || defined(IRIX5)
1294 # include <unistd.h>
1296 # define GETPAGESIZE() getpagesize()
1299 # if defined(SUNOS5) || defined(DRSNX) || defined(UTS4)
1300 /* OS has SVR4 generic features. Probably others also qualify. */
1304 # if defined(SUNOS5) || defined(DRSNX)
1305 /* OS has SUNOS5 style semi-undocumented interface to dynamic */
1308 /* OS has SUNOS5 style signal handlers. */
1316 # if CPP_WORDSZ != 32 && CPP_WORDSZ != 64
1321 # undef DYNAMIC_LOADING
1326 # undef MPROTECT_VDB
1331 /* Postponed for now. */
1333 # undef MPROTECT_VDB
1336 # ifdef SMALL_CONFIG
1337 /* Presumably not worth the space it takes. */
1339 # undef MPROTECT_VDB
1343 # undef MPROTECT_VDB /* Can't deal with address space holes. */
1346 # if !defined(PCR_VDB) && !defined(PROC_VDB) && !defined(MPROTECT_VDB)
1347 # define DEFAULT_VDB
1351 # define PREFETCH(x)
1352 # define NO_PREFETCH
1355 # ifndef PREFETCH_FOR_WRITE
1356 # define PREFETCH_FOR_WRITE(x)
1357 # define NO_PREFETCH_FOR_WRITE
1360 # ifndef CACHE_LINE_SIZE
1361 # define CACHE_LINE_SIZE 32 /* Wild guess */
1364 # ifndef CLEAR_DOUBLE
1365 # define CLEAR_DOUBLE(x) \
1366 ((word*)x)[0] = 0; \
1368 # endif /* CLEAR_DOUBLE */
1370 # if defined(_SOLARIS_PTHREADS) && !defined(SOLARIS_THREADS)
1371 # define SOLARIS_THREADS
1373 # if defined(IRIX_THREADS) && !defined(IRIX5)
1374 --> inconsistent configuration
1376 # if defined(IRIX_JDK_THREADS) && !defined(IRIX5)
1377 --> inconsistent configuration
1379 # if defined(LINUX_THREADS) && !defined(LINUX)
1380 --> inconsistent configuration
1382 # if defined(SOLARIS_THREADS) && !defined(SUNOS5)
1383 --> inconsistent configuration
1385 # if defined(HPUX_THREADS) && !defined(HPUX)
1386 --> inconsistent configuration
1388 # if defined(PCR) || defined(SRC_M3) || \
1389 defined(SOLARIS_THREADS) || defined(WIN32_THREADS) || \
1390 defined(IRIX_THREADS) || defined(LINUX_THREADS) || \
1391 defined(IRIX_JDK_THREADS) || defined(HPUX_THREADS)
1395 # if defined(HP_PA) || defined(M88K) || defined(POWERPC) \
1396 || (defined(I386) && defined(OS2)) || defined(UTS4) || defined(LINT)
1397 /* Use setjmp based hack to mark from callee-save registers. */
1398 # define USE_GENERIC_PUSH_REGS
1400 # if defined(SPARC) && !defined(LINUX)
1401 # define SAVE_CALL_CHAIN
1402 # define ASM_CLEAR_CODE /* Stack clearing is crucial, and we */
1403 /* include assembly code to do it well. */
1406 # if defined(LINUX) && !defined(POWERPC)
1409 # include <linux/version.h>
1410 # if (LINUX_VERSION_CODE <= 0x10400)
1411 /* Ugly hack to get struct sigcontext_struct definition. Required */
1412 /* for some early 1.3.X releases. Will hopefully go away soon. */
1413 /* in some later Linux releases, asm/sigcontext.h may have to */
1414 /* be included instead. */
1416 # include <asm/signal.h>
1422 /* Kernels prior to 2.1.1 defined struct sigcontext_struct instead of */
1423 /* struct sigcontext. libc6 (glibc2) uses "struct sigcontext" in */
1424 /* prototypes, so we have to include the top-level sigcontext.h to */
1425 /* make sure the former gets defined to be the latter if appropriate. */
1426 # include <features.h>
1428 # if 2 == __GLIBC__ && 0 == __GLIBC_MINOR__
1429 /* glibc 2.1 no longer has sigcontext.h. But signal.h */
1430 /* has the right declaration for glibc 2.1. */
1431 # include <sigcontext.h>
1432 # endif /* 0 == __GLIBC_MINOR__ */
1433 # else /* not 2 <= __GLIBC__ */
1434 /* libc5 doesn't have <sigcontext.h>: go directly with the kernel */
1435 /* one. Check LINUX_VERSION_CODE to see which we should reference. */
1436 # include <asm/sigcontext.h>
1437 # endif /* 2 <= __GLIBC__ */
1440 # if !defined(OS2) && !defined(PCR) && !defined(AMIGA) && !defined(MACOS)
1441 # include <sys/types.h>
1442 # if !defined(MSWIN32) && !defined(SUNOS4)
1443 # include <unistd.h>
1447 # include <signal.h>
1449 /* Blatantly OS dependent routines, except for those that are related */
1450 /* to dynamic loading. */
1452 # if !defined(THREADS) && !defined(STACKBOTTOM) && defined(HEURISTIC2)
1453 # define NEED_FIND_LIMIT
1456 # if defined(IRIX_THREADS) || defined(HPUX_THREADS)
1457 # define NEED_FIND_LIMIT
1460 # if (defined(SUNOS4) && defined(DYNAMIC_LOADING)) && !defined(PCR)
1461 # define NEED_FIND_LIMIT
1464 # if (defined(SVR4) || defined(AUX) || defined(DGUX)) && !defined(PCR)
1465 # define NEED_FIND_LIMIT
1468 # if defined(LINUX) && \
1469 (defined(POWERPC) || defined(SPARC) || defined(ALPHA) || defined(IA64) \
1471 # define NEED_FIND_LIMIT
1474 #ifdef NEED_FIND_LIMIT
1475 # include <setjmp.h>
1479 # include <machine/trap.h>
1483 # include <proto/exec.h>
1484 # include <proto/dos.h>
1485 # include <dos/dosextens.h>
1486 # include <workbench/startup.h>
1490 # define WIN32_LEAN_AND_MEAN
1492 # include <windows.h>
1496 # include <Processes.h>
1500 # include <sys/uio.h>
1501 # include <malloc.h> /* for locking */
1504 # include <sys/types.h>
1505 # include <sys/mman.h>
1506 # include <sys/stat.h>
1511 # include <sys/siginfo.h>
1514 # define setjmp(env) sigsetjmp(env, 1)
1515 # define longjmp(env, val) siglongjmp(env, val)
1516 # define jmp_buf sigjmp_buf
1520 /* Apparently necessary for djgpp 2.01. May casuse problems with */
1521 /* other versions. */
1522 typedef long unsigned int caddr_t
;
1526 # include "il/PCR_IL.h"
1527 # include "th/PCR_ThCtl.h"
1528 # include "mm/PCR_MM.h"
1531 #if !defined(NO_EXECUTE_PERMISSION)
1532 # define OPT_PROT_EXEC PROT_EXEC
1534 # define OPT_PROT_EXEC 0
1539 # include <stddef.h>
1541 # if !defined(__IBMC__) && !defined(__WATCOMC__) /* e.g. EMX */
1543 # else /* IBM's compiler */
1545 /* A kludge to get around what appears to be a header file bug */
1547 # define WORD unsigned short
1550 # define DWORD unsigned long
1554 # include <newexe.h>
1555 # include <exe386.h>
1557 # endif /* __IBMC__ */
1559 # define INCL_DOSEXCEPTIONS
1560 # define INCL_DOSPROCESS
1561 # define INCL_DOSERRORS
1562 # define INCL_DOSMODULEMGR
1563 # define INCL_DOSMEMMGR
1569 * Find the base of the stack.
1570 * Used only in single-threaded environment.
1571 * With threads, GC_mark_roots needs to know how to do this.
1572 * Called with allocator lock held.
1575 # define is_writable(prot) ((prot) == PAGE_READWRITE \
1576 || (prot) == PAGE_WRITECOPY \
1577 || (prot) == PAGE_EXECUTE_READWRITE \
1578 || (prot) == PAGE_EXECUTE_WRITECOPY)
1579 /* Return the number of bytes that are writable starting at p. */
1580 /* The pointer p is assumed to be page aligned. */
1581 /* If base is not 0, *base becomes the beginning of the */
1582 /* allocation region containing p. */
1583 static word
GC_get_writable_length(ptr_t p
, ptr_t
*base
)
1585 MEMORY_BASIC_INFORMATION buf
;
1589 result
= VirtualQuery(p
, &buf
, sizeof(buf
));
1590 if (result
!= sizeof(buf
)) ABORT("Weird VirtualQuery result");
1591 if (base
!= 0) *base
= (ptr_t
)(buf
.AllocationBase
);
1592 protect
= (buf
.Protect
& ~(PAGE_GUARD
| PAGE_NOCACHE
));
1593 if (!is_writable(protect
)) {
1596 if (buf
.State
!= MEM_COMMIT
) return(0);
1597 return(buf
.RegionSize
);
1600 void *scm_get_stack_base()
1603 ptr_t sp
= (ptr_t
)(&dummy
);
1606 static word GC_page_size
= 0;
1607 if (!GC_page_size
) {
1608 SYSTEM_INFO sysinfo
;
1609 GetSystemInfo(&sysinfo
);
1610 GC_page_size
= sysinfo
.dwPageSize
;
1612 trunc_sp
= (ptr_t
)((word
)sp
& ~(GC_page_size
- 1));
1613 size
= GC_get_writable_length(trunc_sp
, 0);
1614 return(trunc_sp
+ size
);
1622 void *scm_get_stack_base()
1627 if (DosGetInfoBlocks(&ptib
, &ppib
) != NO_ERROR
) {
1628 GC_err_printf0("DosGetInfoBlocks failed\n");
1629 ABORT("DosGetInfoBlocks failed\n");
1631 return((ptr_t
)(ptib
-> tib_pstacklimit
));
1638 void *scm_get_stack_base()
1640 struct Process
*proc
= (struct Process
*)SysBase
->ThisTask
;
1642 /* Reference: Amiga Guru Book Pages: 42,567,574 */
1643 if (proc
->pr_Task
.tc_Node
.ln_Type
==NT_PROCESS
1644 && proc
->pr_CLI
!= NULL
) {
1645 /* first ULONG is StackSize */
1646 /*longPtr = proc->pr_ReturnAddr;
1647 size = longPtr[0];*/
1649 return (char *)proc
->pr_ReturnAddr
+ sizeof(ULONG
);
1651 return (char *)proc
->pr_Task
.tc_SPUpper
;
1655 #if 0 /* old version */
1656 void *scm_get_stack_base()
1658 extern struct WBStartup
*_WBenchMsg
;
1660 extern long __stack
;
1662 struct Process
*proc
;
1663 struct CommandLineInterface
*cli
;
1666 if ((task
= FindTask(0)) == 0) {
1667 GC_err_puts("Cannot find own task structure\n");
1668 ABORT("task missing");
1670 proc
= (struct Process
*)task
;
1671 cli
= BADDR(proc
->pr_CLI
);
1673 if (_WBenchMsg
!= 0 || cli
== 0) {
1674 size
= (char *)task
->tc_SPUpper
- (char *)task
->tc_SPLower
;
1676 size
= cli
->cli_DefaultStack
* 4;
1678 return (ptr_t
)(__base
+ GC_max(size
, __stack
));
1682 # else /* !AMIGA, !OS2, ... */
1684 # ifdef NEED_FIND_LIMIT
1685 /* Some tools to implement HEURISTIC2 */
1686 # define MIN_PAGE_SIZE 256 /* Smallest conceivable page size, bytes */
1687 /* static */ jmp_buf GC_jmp_buf
;
1690 static void GC_fault_handler(sig
)
1693 longjmp(GC_jmp_buf
, 1);
1697 typedef void (*handler
)(int);
1699 typedef void (*handler
)();
1702 # if defined(SUNOS5SIGS) || defined(IRIX5) || defined(OSF1)
1703 static struct sigaction old_segv_act
;
1704 # if defined(_sigargs) || defined(HPUX) /* !Irix6.x */
1705 static struct sigaction old_bus_act
;
1708 static handler old_segv_handler
, old_bus_handler
;
1711 static void GC_setup_temporary_fault_handler()
1713 # if defined(SUNOS5SIGS) || defined(IRIX5) || defined(OSF1)
1714 struct sigaction act
;
1716 act
.sa_handler
= GC_fault_handler
;
1717 act
.sa_flags
= SA_RESTART
| SA_NODEFER
;
1718 /* The presence of SA_NODEFER represents yet another gross */
1719 /* hack. Under Solaris 2.3, siglongjmp doesn't appear to */
1720 /* interact correctly with -lthread. We hide the confusion */
1721 /* by making sure that signal handling doesn't affect the */
1724 (void) sigemptyset(&act
.sa_mask
);
1725 # ifdef IRIX_THREADS
1726 /* Older versions have a bug related to retrieving and */
1727 /* and setting a handler at the same time. */
1728 (void) sigaction(SIGSEGV
, 0, &old_segv_act
);
1729 (void) sigaction(SIGSEGV
, &act
, 0);
1731 (void) sigaction(SIGSEGV
, &act
, &old_segv_act
);
1732 # if defined(IRIX5) && defined(_sigargs) /* Irix 5.x, not 6.x */ \
1734 /* Under Irix 5.x or HP/UX, we may get SIGBUS. */
1735 /* Pthreads doesn't exist under Irix 5.x, so we */
1736 /* don't have to worry in the threads case. */
1737 (void) sigaction(SIGBUS
, &act
, &old_bus_act
);
1739 # endif /* IRIX_THREADS */
1741 old_segv_handler
= signal(SIGSEGV
, GC_fault_handler
);
1743 old_bus_handler
= signal(SIGBUS
, GC_fault_handler
);
1748 static void GC_reset_fault_handler()
1750 # if defined(SUNOS5SIGS) || defined(IRIX5) || defined(OSF1)
1751 (void) sigaction(SIGSEGV
, &old_segv_act
, 0);
1752 # if defined(IRIX5) && defined(_sigargs) /* Irix 5.x, not 6.x */ \
1754 (void) sigaction(SIGBUS
, &old_bus_act
, 0);
1757 (void) signal(SIGSEGV
, old_segv_handler
);
1759 (void) signal(SIGBUS
, old_bus_handler
);
1764 /* Single argument version, robust against whole program analysis. */
1769 static VOLATILE word sink
;
1773 /* Return the first nonaddressible location > p (up) or */
1774 /* the smallest location q s.t. [q,p] is addressible (!up). */
1775 static ptr_t
GC_find_limit(p
, up
)
1779 static VOLATILE ptr_t result
;
1780 /* Needs to be static, since otherwise it may not be */
1781 /* preserved across the longjmp. Can safely be */
1782 /* static since it's only called once, with the */
1783 /* allocation lock held. */
1786 GC_setup_temporary_fault_handler();
1787 if (setjmp(GC_jmp_buf
) == 0) {
1788 result
= (ptr_t
)(((word
)(p
))
1789 & ~(MIN_PAGE_SIZE
-1));
1792 result
+= MIN_PAGE_SIZE
;
1794 result
-= MIN_PAGE_SIZE
;
1796 GC_noop1((word
)(*result
));
1799 GC_reset_fault_handler();
1801 result
+= MIN_PAGE_SIZE
;
1808 #ifdef LINUX_STACKBOTTOM
1810 #include <sys/types.h>
1811 #include <sys/stat.h>
1814 # define STAT_SKIP 27 /* Number of fields preceding startstack */
1815 /* field in /proc/self/stat */
1817 static ptr_t
GC_linux_stack_base(void)
1819 /* We read the stack base value from /proc/self/stat. We do this */
1820 /* using direct I/O system calls in order to avoid calling malloc */
1821 /* in case REDIRECT_MALLOC is defined. */
1822 # define STAT_BUF_SIZE 4096
1824 # define STAT_READ __real_read
1826 # define STAT_READ read
1828 char stat_buf
[STAT_BUF_SIZE
];
1832 size_t i
, buf_offset
= 0;
1834 f
= open("/proc/self/stat", O_RDONLY
);
1835 if (f
< 0 || STAT_READ(f
, stat_buf
, STAT_BUF_SIZE
) < 2 * STAT_SKIP
) {
1836 ABORT("Couldn't read /proc/self/stat");
1838 c
= stat_buf
[buf_offset
++];
1839 /* Skip the required number of fields. This number is hopefully */
1840 /* constant across all Linux implementations. */
1841 for (i
= 0; i
< STAT_SKIP
; ++i
) {
1842 while (isspace(c
)) c
= stat_buf
[buf_offset
++];
1843 while (!isspace(c
)) c
= stat_buf
[buf_offset
++];
1845 while (isspace(c
)) c
= stat_buf
[buf_offset
++];
1846 while (isdigit(c
)) {
1849 c
= stat_buf
[buf_offset
++];
1852 if (result
< 0x10000000) ABORT("Absurd stack bottom value");
1853 return (ptr_t
)result
;
1856 #endif /* LINUX_STACKBOTTOM */
1858 void *scm_get_stack_base()
1861 void *result
= &dummy
; /* initialize to silence compiler */
1863 # define STACKBOTTOM_ALIGNMENT_M1 ((word)STACK_GRAN - 1)
1866 return(STACKBOTTOM
);
1869 # if STACK_GROWS_DOWN
1870 result
= (ptr_t
)((((word
)(&dummy
))
1871 + STACKBOTTOM_ALIGNMENT_M1
)
1872 & ~STACKBOTTOM_ALIGNMENT_M1
);
1874 result
= (ptr_t
)(((word
)(&dummy
))
1875 & ~STACKBOTTOM_ALIGNMENT_M1
);
1877 # endif /* HEURISTIC1 */
1878 # ifdef LINUX_STACKBOTTOM
1879 result
= GC_linux_stack_base();
1882 # if STACK_GROWS_DOWN
1883 result
= GC_find_limit((ptr_t
)(&dummy
), TRUE
);
1884 # ifdef HEURISTIC2_LIMIT
1885 if (result
> HEURISTIC2_LIMIT
1886 && (ptr_t
)(&dummy
) < HEURISTIC2_LIMIT
) {
1887 result
= HEURISTIC2_LIMIT
;
1891 result
= GC_find_limit((ptr_t
)(&dummy
), FALSE
);
1892 # ifdef HEURISTIC2_LIMIT
1893 if (result
< HEURISTIC2_LIMIT
1894 && (ptr_t
)(&dummy
) > HEURISTIC2_LIMIT
) {
1895 result
= HEURISTIC2_LIMIT
;
1900 # endif /* HEURISTIC2 */
1901 # if STACK_GROWS_DOWN
1902 if (result
== 0) result
= (ptr_t
)(signed_word
)(-sizeof(ptr_t
));
1905 # endif /* STACKBOTTOM */
1908 # endif /* ! AMIGA */
1910 # endif /* ! MSWIN32 */
1912 #endif /* ! HAVE_LIBC_STACK_END */