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, 2003, 2004, 2006, 2008 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(__alpha__)
116 # define mach_type_known
118 # if defined(__NetBSD__) && defined(__powerpc__)
121 # define mach_type_known
123 /* in netbsd 2.0 only __m68k__ is defined, not m68k */
124 # if defined(__NetBSD__) && (defined(m68k) || defined(__m68k__))
127 # define mach_type_known
129 /* in netbsd 2.0 only __arm__ is defined, not arm32 */
130 # if defined(__NetBSD__) && (defined(arm32) || defined(__arm__))
133 # define mach_type_known
135 # if defined(__NetBSD__) && defined(__sparc__)
138 # define mach_type_known
147 # define mach_type_known
149 # if defined(mips) || defined(__mips)
152 # if defined(ultrix) || defined(__ultrix) || defined(__NetBSD__)
155 # if defined(_SYSTYPE_SVR4) || defined(SYSTYPE_SVR4) \
156 || defined(__SYSTYPE_SVR4__)
157 # define IRIX5 /* or IRIX 6.X */
159 # define RISCOS /* or IRIX 4.X */
163 # define mach_type_known
165 # if defined(sequent) && defined(i386)
168 # define mach_type_known
170 # if defined(sun) && defined(i386)
173 # define mach_type_known
175 # if (defined(__OS2__) || defined(__EMX__)) && defined(__32BIT__)
178 # define mach_type_known
182 # define mach_type_known
184 # if defined(sun) && (defined(sparc) || defined(__sparc))
186 /* Test for SunOS 5.x */
193 # define mach_type_known
195 # if defined(sparc) && defined(unix) && !defined(sun) && !defined(linux) \
196 && !defined(__OpenBSD__)
199 # define mach_type_known
203 # define mach_type_known
205 # if defined(_M_XENIX) && defined(_M_SYSV) && defined(_M_I386)
206 /* The above test may need refinement */
208 # if defined(_SCO_ELF)
213 # define mach_type_known
215 # if defined(_AUX_SOURCE)
218 # define mach_type_known
220 # if defined(_PA_RISC1_0) || defined(_PA_RISC1_1) || defined(_PA_RISC2_0) \
221 || defined(hppa) || defined(__hppa__)
226 # define mach_type_known
228 # if defined(LINUX) && (defined(i386) || defined(__i386__))
230 # define mach_type_known
232 # if defined(LINUX) && (defined(__ia64__) || defined(__ia64))
234 # define mach_type_known
236 # if defined(LINUX) && defined(powerpc)
238 # define mach_type_known
240 # if defined(LINUX) && defined(__mc68000__)
242 # define mach_type_known
244 # if defined(LINUX) && (defined(sparc) || defined(__sparc__))
246 # define mach_type_known
248 # if defined(LINUX) && (defined(arm) || defined (__arm__))
250 # define mach_type_known
252 # if defined(__alpha) || defined(__alpha__)
254 # if !defined(LINUX) && !defined (NETBSD)
255 # define OSF1 /* a.k.a Digital Unix */
257 # define mach_type_known
259 # if defined(_AMIGA) && !defined(AMIGA)
264 # define mach_type_known
266 # if defined(THINK_C) || defined(__MWERKS__) && !defined(__powerc)
269 # define mach_type_known
271 # if defined(__MWERKS__) && defined(__powerc)
274 # define mach_type_known
276 # if defined(macosx) || \
277 (defined(__APPLE__) && defined(__MACH__) && defined(__ppc__))
280 # define mach_type_known
282 # if defined(NeXT) && defined(mc68000)
285 # define mach_type_known
287 # if defined(NeXT) && defined(i386)
290 # define mach_type_known
292 # if defined(__OpenBSD__) && (defined(i386) || defined(__i386__))
295 # define mach_type_known
297 # if defined(__FreeBSD__) && defined(i386)
300 # define mach_type_known
302 # if defined(__NetBSD__) && defined(i386)
305 # define mach_type_known
307 # if defined(bsdi) && defined(i386)
310 # define mach_type_known
312 # if !defined(mach_type_known) && defined(__386BSD__)
315 # define mach_type_known
317 # if defined(_CX_UX) && defined(_M88K)
320 # define mach_type_known
325 # define mach_type_known
327 # if (defined(_MSDOS) || defined(_MSC_VER)) && (_M_IX86 >= 300) \
328 || defined(_WIN32) && !defined(__CYGWIN32__) && !defined(__CYGWIN__)
330 # define MSWIN32 /* or Win32s */
331 # define mach_type_known
333 # if defined(__DJGPP__)
336 # define DJGPP /* MSDOS running the DJGPP port of GCC */
338 # define mach_type_known
340 # if defined(__CYGWIN32__) || defined(__CYGWIN__)
343 # define mach_type_known
345 # if defined(__MINGW32__)
348 # define mach_type_known
350 # if defined(__BORLANDC__)
353 # define mach_type_known
355 # if defined(_UTS) && !defined(mach_type_known)
358 # define mach_type_known
362 # define mach_type_known
365 # if defined(__WATCOMC__) && defined(__386__)
367 # if !defined(OS2) && !defined(MSWIN32) && !defined(DOS4GW)
368 # if defined(__OS2__)
371 # if defined(__WINDOWS_386__) || defined(__NT__)
378 # define mach_type_known
380 # if defined(__s390__) && defined(LINUX)
382 # define mach_type_known
384 # if defined(__GNU__)
387 # define mach_type_known
389 # if defined(__SCO_VERSION__)
392 # define mach_type_known
395 /* Feel free to add more clauses here */
397 /* Or manually define the machine type here. A machine type is */
398 /* characterized by the architecture. Some */
399 /* machine types are further subdivided by OS. */
400 /* the macros ULTRIX, RISCOS, and BSD to distinguish. */
401 /* Note that SGI IRIX is treated identically to RISCOS. */
402 /* SYSV on an M68K actually means A/UX. */
403 /* The distinction in these cases is usually the stack starting address */
404 # ifndef mach_type_known
407 scm_get_stack_base ()
409 ABORT ("Can't determine stack base");
414 /* Mapping is: M68K ==> Motorola 680X0 */
415 /* (SUNOS4,HP,NEXT, and SYSV (A/UX), */
416 /* MACOS and AMIGA variants) */
417 /* I386 ==> Intel 386 */
418 /* (SEQUENT, OS2, SCO, LINUX, NETBSD, */
419 /* FREEBSD, THREE86BSD, MSWIN32, */
420 /* BSDI,SUNOS5, NEXT, other variants) */
421 /* NS32K ==> Encore Multimax */
422 /* MIPS ==> R2000 or R3000 */
423 /* (RISCOS, ULTRIX variants) */
424 /* VAX ==> DEC VAX */
425 /* (BSD, ULTRIX variants) */
426 /* RS6000 ==> IBM RS/6000 AIX3.X */
427 /* RT ==> IBM PC/RT */
428 /* HP_PA ==> HP9000/700 & /800 */
430 /* SPARC ==> SPARC under SunOS */
431 /* (SUNOS4, SUNOS5, */
432 /* DRSNX variants) */
433 /* ALPHA ==> DEC Alpha */
434 /* (OSF1 and LINUX variants) */
435 /* M88K ==> Motorola 88XX0 */
436 /* (CX_UX and DGUX) */
437 /* S370 ==> 370-like machine */
438 /* running Amdahl UTS4 */
439 /* ARM32 ==> Intel StrongARM */
440 /* IA64 ==> Intel IA64 */
445 * For each architecture and OS, the following need to be defined:
447 * CPP_WORD_SZ is a simple integer constant representing the word size.
448 * in bits. We assume byte addressibility, where a byte has 8 bits.
449 * We also assume CPP_WORD_SZ is either 32 or 64.
450 * (We care about the length of pointers, not hardware
451 * bus widths. Thus a 64 bit processor with a C compiler that uses
452 * 32 bit pointers should use CPP_WORD_SZ of 32, not 64. Default is 32.)
454 * MACH_TYPE is a string representation of the machine type.
455 * OS_TYPE is analogous for the OS.
457 * ALIGNMENT is the largest N, such that
458 * all pointer are guaranteed to be aligned on N byte boundaries.
459 * defining it to be 1 will always work, but perform poorly.
461 * DATASTART is the beginning of the data segment.
462 * On UNIX systems, the collector will scan the area between DATASTART
463 * and DATAEND for root pointers.
465 * DATAEND, if not &end.
467 * ALIGN_DOUBLE of GC_malloc should return blocks aligned to twice
470 * STACKBOTTOM is the cool end of the stack, which is usually the
471 * highest address in the stack.
472 * Under PCR or OS/2, we have other ways of finding thread stacks.
473 * For each machine, the following should:
474 * 1) define SCM_STACK_GROWS_UP if the stack grows toward higher addresses, and
475 * 2) define exactly one of
476 * STACKBOTTOM (should be defined to be an expression)
479 * If either of the last two macros are defined, then STACKBOTTOM is computed
480 * during collector startup using one of the following two heuristics:
481 * HEURISTIC1: Take an address inside GC_init's frame, and round it up to
482 * the next multiple of STACK_GRAN.
483 * HEURISTIC2: Take an address inside GC_init's frame, increment it repeatedly
484 * in small steps (decrement if SCM_STACK_GROWS_UP), and read the value
485 * at each location. Remember the value when the first
486 * Segmentation violation or Bus error is signalled. Round that
487 * to the nearest plausible page boundary, and use that instead
490 * Gustavo Rodriguez-Rivera points out that on most (all?) Unix machines,
491 * the value of environ is a pointer that can serve as STACKBOTTOM.
492 * I expect that HEURISTIC2 can be replaced by this approach, which
493 * interferes far less with debugging.
495 * If no expression for STACKBOTTOM can be found, and neither of the above
496 * heuristics are usable, the collector can still be used with all of the above
497 * undefined, provided one of the following is done:
498 * 1) GC_mark_roots can be changed to somehow mark from the correct stack(s)
499 * without reference to STACKBOTTOM. This is appropriate for use in
500 * conjunction with thread packages, since there will be multiple stacks.
501 * (Allocating thread stacks in the heap, and treating them as ordinary
502 * heap data objects is also possible as a last resort. However, this is
503 * likely to introduce significant amounts of excess storage retention
504 * unless the dead parts of the thread stacks are periodically cleared.)
505 * 2) Client code may set GC_stackbottom before calling any GC_ routines.
506 * If the author of the client code controls the main program, this is
507 * easily accomplished by introducing a new main program, setting
508 * GC_stackbottom to the address of a local variable, and then calling
509 * the original main program. The new main program would read something
512 * # include "gc_private.h"
514 * main(argc, argv, envp)
516 * char **argv, **envp;
520 * GC_stackbottom = (ptr_t)(&dummy);
521 * return(real_main(argc, argv, envp));
525 * Each architecture may also define the style of virtual dirty bit
526 * implementation to be used:
527 * MPROTECT_VDB: Write protect the heap and catch faults.
528 * PROC_VDB: Use the SVR4 /proc primitives to read dirty bits.
530 * An architecture may define DYNAMIC_LOADING if dynamic_load.c
531 * defined GC_register_dynamic_libraries() for the architecture.
533 * An architecture may define PREFETCH(x) to preload the cache with *x.
534 * This defaults to a no-op.
536 * PREFETCH_FOR_WRITE(x) is used if *x is about to be written.
538 * An architecture may also define CLEAR_DOUBLE(x) to be a fast way to
539 * clear the two words at GC_malloc-aligned address x. By default,
540 * word stores of 0 are used instead.
544 # define STACK_GRAN 0x1000000
546 # define MACH_TYPE "M68K"
549 # define OS_TYPE "OPENBSD"
552 # define DATASTART ((ptr_t)(&etext))
555 # define OS_TYPE "NETBSD"
558 # define DATASTART ((ptr_t)(&etext))
561 # define OS_TYPE "LINUX"
562 # define STACKBOTTOM ((ptr_t)0xf0000000)
563 # define MPROTECT_VDB
565 # define DYNAMIC_LOADING
566 extern char **__environ
;
567 # define DATASTART ((ptr_t)(&__environ))
568 /* hideous kludge: __environ is the first */
569 /* word in crt0.o, and delimits the start */
570 /* of the data segment, no matter which */
571 /* ld options were passed through. */
572 /* We could use _etext instead, but that */
573 /* would include .rodata, which may */
574 /* contain large read-only data tables */
575 /* that we'd rather not scan. */
577 # define DATAEND (&_end)
580 # define DATASTART ((ptr_t)((((word) (&etext)) + 0xfff) & ~0xfff))
584 # define OS_TYPE "SUNOS4"
586 # define DATASTART ((ptr_t)((((word) (&etext)) + 0x1ffff) & ~0x1ffff))
587 # define HEURISTIC1 /* differs */
588 # define DYNAMIC_LOADING
591 # define OS_TYPE "HP"
593 # define DATASTART ((ptr_t)((((word) (&etext)) + 0xfff) & ~0xfff))
594 # define STACKBOTTOM ((ptr_t) 0xffeffffc)
595 /* empirically determined. seems to work. */
597 # define GETPAGESIZE() sysconf(_SC_PAGE_SIZE)
600 # define OS_TYPE "SYSV"
602 # define DATASTART ((ptr_t)((((word) (&etext)) + 0x3fffff) \
604 +((word)&etext & 0x1fff))
605 /* This only works for shared-text binaries with magic number 0413.
606 The other sorts of SysV binaries put the data at the end of the text,
607 in which case the default of &etext would work. Unfortunately,
608 handling both would require having the magic-number available.
611 # define STACKBOTTOM ((ptr_t)0xFFFFFFFE)
612 /* The stack starts at the top of memory, but */
613 /* 0x0 cannot be used as setjump_test complains */
614 /* that the stack direction is incorrect. Two */
615 /* bytes down from 0x0 should be safe enough. */
617 # include <sys/mmu.h>
618 # define GETPAGESIZE() PAGESIZE /* Is this still right? */
621 # define OS_TYPE "AMIGA"
622 /* STACKBOTTOM and DATASTART handled specially */
624 # define DATAEND /* not needed */
625 # define GETPAGESIZE() 4096
631 # define OS_TYPE "MACOS"
632 /* see os_dep.c for details of global data segments. */
633 # define STACKBOTTOM ((ptr_t) LMGetCurStackBase())
634 # define DATAEND /* not needed */
635 # define GETPAGESIZE() 4096
638 # define OS_TYPE "NEXT"
639 # define DATASTART ((ptr_t) get_etext())
640 # define STACKBOTTOM ((ptr_t) 0x4000000)
641 # define DATAEND /* not needed */
646 # define MACH_TYPE "POWERPC"
648 # define ALIGNMENT 2 /* Still necessary? Could it be 4? */
652 # define OS_TYPE "MACOS"
653 /* see os_dep.c for details of global data segments. */
654 # define STACKBOTTOM ((ptr_t) LMGetCurStackBase())
655 # define DATAEND /* not needed */
658 # define ALIGNMENT 4 /* Guess. Can someone verify? */
659 /* This was 2, but that didn't sound right. */
660 # define OS_TYPE "LINUX"
662 # define DYNAMIC_LOADING
664 # define STACK_GRAN 0x10000000
665 /* Stack usually starts at 0x80000000 */
666 # define LINUX_DATA_START
668 # define DATAEND (&_end)
672 # define OS_TYPE "MACOSX"
673 # define DATASTART ((ptr_t) get_etext())
674 # define STACKBOTTOM ((ptr_t) 0xc0000000)
675 # define DATAEND /* not needed */
680 # define MACH_TYPE "VAX"
681 # define ALIGNMENT 4 /* Pointers are longword aligned by 4.2 C compiler */
683 # define DATASTART ((ptr_t)(&etext))
685 # define OS_TYPE "BSD"
687 /* HEURISTIC2 may be OK, but it's hard to test. */
690 # define OS_TYPE "ULTRIX"
691 # define STACKBOTTOM ((ptr_t) 0x7fffc800)
696 # define MACH_TYPE "RT"
698 # define DATASTART ((ptr_t) 0x10000000)
699 # define STACKBOTTOM ((ptr_t) 0x1fffd800)
703 # define MACH_TYPE "SPARC"
704 # define ALIGNMENT 4 /* Required by hardware */
705 # define ALIGN_DOUBLE
708 # define OS_TYPE "SUNOS5"
711 extern char * GC_SysVGetDataStart();
712 # define DATASTART (ptr_t)GC_SysVGetDataStart(0x10000, &_etext)
713 # define DATAEND (&_end)
718 # define HEAP_START (ptr_t)0x40000000
720 # define HEAP_START DATAEND
723 /* HEURISTIC1 reportedly no longer works under 2.7. Thus we */
724 /* switched to HEURISTIC2, eventhough it creates some debugging */
728 # define GETPAGESIZE() sysconf(_SC_PAGESIZE)
729 /* getpagesize() appeared to be missing from at least one */
730 /* Solaris 5.4 installation. Weird. */
731 # define DYNAMIC_LOADING
734 # define OS_TYPE "SUNOS4"
735 /* [If you have a weak stomach, don't read this.] */
736 /* We would like to use: */
737 /* # define DATASTART ((ptr_t)((((word) (&etext)) + 0x1fff) & ~0x1fff)) */
738 /* This fails occasionally, due to an ancient, but very */
739 /* persistent ld bug. &etext is set 32 bytes too high. */
740 /* We instead read the text segment size from the a.out */
741 /* header, which happens to be mapped into our address space */
742 /* at the start of the text segment. The detective work here */
743 /* was done by Robert Ehrlich, Manuel Serrano, and Bernard */
744 /* Serpette of INRIA. */
745 /* This assumes ZMAGIC, i.e. demand-loadable executables. */
746 # define TEXTSTART 0x2000
747 # define DATASTART ((ptr_t)(*(int *)(TEXTSTART+0x4)+TEXTSTART))
748 # define MPROTECT_VDB
750 # define DYNAMIC_LOADING
753 # define CPP_WORDSZ 32
754 # define OS_TYPE "DRSNX"
755 extern char * GC_SysVGetDataStart();
757 # define DATASTART (ptr_t)GC_SysVGetDataStart(0x10000, &etext)
758 # define MPROTECT_VDB
759 # define STACKBOTTOM ((ptr_t) 0xdfff0000)
760 # define DYNAMIC_LOADING
763 # define OS_TYPE "LINUX"
765 # define LINUX_DATA_START
766 # define DYNAMIC_LOADING
768 Linux Sparc non elf
?
771 # define DATAEND (&_end)
773 # define STACKBOTTOM ((ptr_t) 0xf0000000)
776 # define OS_TYPE "OPENBSD"
777 # define STACKBOTTOM ((ptr_t) 0xf8000000)
778 # define DATASTART ((ptr_t)(&etext))
783 # define MACH_TYPE "I386"
784 # define ALIGNMENT 4 /* Appears to hold for all "32 bit" compilers */
785 /* except Borland. The -a4 option fixes */
787 /* Ivan Demakov: For Watcom the option is -zp4. */
788 # ifndef SMALL_CONFIG
789 # define ALIGN_DOUBLE /* Not strictly necessary, but may give speed */
790 /* improvement on Pentiums. */
793 # define OS_TYPE "SEQUENT"
795 # define DATASTART ((ptr_t)((((word) (&etext)) + 0xfff) & ~0xfff))
796 # define STACKBOTTOM ((ptr_t) 0x3ffff000)
799 # define OS_TYPE "SUNOS5"
800 extern int etext
, _start
;
801 extern char * GC_SysVGetDataStart();
802 # define DATASTART GC_SysVGetDataStart(0x1000, &etext)
803 # define STACKBOTTOM ((ptr_t)(&_start))
804 /** At least in Solaris 2.5, PROC_VDB gives wrong values for dirty bits. */
805 /*# define PROC_VDB*/
806 # define DYNAMIC_LOADING
811 # define HEAP_START (ptr_t)0x40000000
813 # define HEAP_START DATAEND
817 # define OS_TYPE "SCO"
819 # define DATASTART ((ptr_t)((((word) (&etext)) + 0x3fffff) \
821 +((word)&etext & 0xfff))
822 # define STACKBOTTOM ((ptr_t) 0x7ffffffc)
825 # define OS_TYPE "SCO_ELF"
827 # define DATASTART ((ptr_t)(&etext))
828 # define STACKBOTTOM ((ptr_t) 0x08048000)
829 # define DYNAMIC_LOADING
830 # define ELF_CLASS ELFCLASS32
833 # define OS_TYPE "LINUX"
834 # define LINUX_STACKBOTTOM
838 # define STACK_GRAN 0x10000000
839 /* STACKBOTTOM is usually 0xc0000000, but this changes with */
840 /* different kernel configurations. In particular, systems */
841 /* with 2GB physical memory will usually move the user */
842 /* address space limit, and hence initial SP to 0x80000000. */
844 # if !defined(LINUX_THREADS) || !defined(REDIRECT_MALLOC)
845 # define MPROTECT_VDB
847 /* We seem to get random errors in incremental mode, */
848 /* possibly because Linux threads is itself a malloc client */
849 /* and can't deal with the signals. */
852 # define DYNAMIC_LOADING
853 # ifdef UNDEFINED /* includes ro data */
855 # define DATASTART ((ptr_t)((((word) (&_etext)) + 0xfff) & ~0xfff))
857 # include <features.h>
858 # if defined(__GLIBC__) && __GLIBC__ >= 2
859 # define LINUX_DATA_START
861 extern char **__environ
;
862 # define DATASTART ((ptr_t)(&__environ))
863 /* hideous kludge: __environ is the first */
864 /* word in crt0.o, and delimits the start */
865 /* of the data segment, no matter which */
866 /* ld options were passed through. */
867 /* We could use _etext instead, but that */
868 /* would include .rodata, which may */
869 /* contain large read-only data tables */
870 /* that we'd rather not scan. */
873 # define DATAEND (&_end)
876 # define DATASTART ((ptr_t)((((word) (&etext)) + 0xfff) & ~0xfff))
878 # ifdef USE_I686_PREFETCH
879 # define PREFETCH(x) \
880 __asm__ __volatile__ (" prefetchnta %0": : "m"(*(char *)(x)))
881 /* Empirically prefetcht0 is much more effective at reducing */
882 /* cache miss stalls for the targetted load instructions. But it */
883 /* seems to interfere enough with other cache traffic that the net */
884 /* result is worse than prefetchnta. */
886 /* Using prefetches for write seems to have a slight negative */
887 /* impact on performance, at least for a PIII/500. */
888 # define PREFETCH_FOR_WRITE(x) \
889 __asm__ __volatile__ (" prefetcht0 %0": : "m"(*(char *)(x)))
892 # ifdef USE_3DNOW_PREFETCH
893 # define PREFETCH(x) \
894 __asm__ __volatile__ (" prefetch %0": : "m"(*(char *)(x)))
895 # define PREFETCH_FOR_WRITE(x)
896 __asm__
__volatile__ (" prefetchw %0": : "m"(*(char *)(x
)))
900 # define OS_TYPE "CYGWIN32"
901 extern int _data_start__
;
902 extern int _data_end__
;
903 extern int _bss_start__
;
904 extern int _bss_end__
;
905 /* For binutils 2.9.1, we have */
906 /* DATASTART = _data_start__ */
907 /* DATAEND = _bss_end__ */
908 /* whereas for some earlier versions it was */
909 /* DATASTART = _bss_start__ */
910 /* DATAEND = _data_end__ */
911 /* To get it right for both, we take the */
912 /* minumum/maximum of the two. */
913 # define MAX(x,y) ((x) > (y) ? (x) : (y))
914 # define MIN(x,y) ((x) < (y) ? (x) : (y))
915 # define DATASTART ((ptr_t) MIN(&_data_start__, &_bss_start__))
916 # define DATAEND ((ptr_t) MAX(&_data_end__, &_bss_end__))
918 # define STACK_GRAN 0x10000
922 # define OS_TYPE "OS2"
923 /* STACKBOTTOM and DATASTART are handled specially in */
924 /* os_dep.c. OS2 actually has the right */
926 # define DATAEND /* not needed */
929 # define OS_TYPE "MSWIN32"
930 /* STACKBOTTOM and DATASTART are handled specially in */
933 # define MPROTECT_VDB
935 # define DATAEND /* not needed */
938 # define OS_TYPE "DJGPP"
939 # include "stubinfo.h"
942 extern int __djgpp_stack_limit
;
943 # define DATASTART ((ptr_t)((((word) (&etext)) + 0x1ff) & ~0x1ff))
944 /* # define STACKBOTTOM ((ptr_t)((word) _stubinfo + _stubinfo->size \
946 # define STACKBOTTOM ((ptr_t)((word) __djgpp_stack_limit + _stklen))
947 /* This may not be right. */
950 # define OS_TYPE "OPENBSD"
953 # define OS_TYPE "FREEBSD"
954 # define MPROTECT_VDB
957 # define OS_TYPE "NETBSD"
960 # define OS_TYPE "THREE86BSD"
963 # define OS_TYPE "BSDI"
965 # if defined(OPENBSD) || defined(FREEBSD) || defined(NETBSD) \
966 || defined(THREE86BSD) || defined(BSDI)
969 # define DATASTART ((ptr_t)(&etext))
972 # define OS_TYPE "NEXT"
973 # define DATASTART ((ptr_t) get_etext())
974 # define STACKBOTTOM ((ptr_t)0xc0000000)
975 # define DATAEND /* not needed */
978 # define OS_TYPE "DOS4GW"
979 extern long __nullarea
;
981 extern char *_STACKTOP
;
982 /* Depending on calling conventions Watcom C either precedes
983 or does not precedes with undescore names of C-variables.
984 Make sure startup code variables always have the same names. */
985 #pragma aux __nullarea "*";
986 #pragma aux _end "*";
987 # define STACKBOTTOM ((ptr_t) _STACKTOP)
988 /* confused? me too. */
989 # define DATASTART ((ptr_t) &__nullarea)
990 # define DATAEND ((ptr_t) &_end)
993 # define OS_TYPE "GNU"
998 # define MACH_TYPE "NS32K"
1000 extern char **environ
;
1001 # define DATASTART ((ptr_t)(&environ))
1002 /* hideous kludge: environ is the first */
1003 /* word in crt0.o, and delimits the start */
1004 /* of the data segment, no matter which */
1005 /* ld options were passed through. */
1006 # define STACKBOTTOM ((ptr_t) 0xfffff000) /* for Encore */
1010 # define MACH_TYPE "MIPS"
1012 # define CPP_WORDSZ _MIPS_SZPTR
1013 # define OS_TYPE "LINUX"
1014 # define ALIGNMENT 4
1015 # define ALIGN_DOUBLE
1017 # define DATASTART ((ptr_t)(&_fdata))
1019 # define DATAEND ((ptr_t)(&_end))
1020 # define STACKBOTTOM ((ptr_t)0x7fff8000)
1021 # define USE_GENERIC_PUSH_REGS 1
1022 # define DYNAMIC_LOADING
1026 # define DATASTART (ptr_t)0x10000000
1027 /* Could probably be slightly higher since */
1028 /* startup code allocates lots of stuff. */
1029 # define OS_TYPE "ULTRIX"
1030 # define ALIGNMENT 4
1034 # define DATASTART (ptr_t)0x10000000
1035 # define OS_TYPE "RISCOS"
1036 # define ALIGNMENT 4 /* Required by hardware */
1041 # define DATASTART ((ptr_t)(&_fdata))
1043 # define HEAP_START (ptr_t)0x30000000
1045 # define HEAP_START DATASTART
1047 /* Lowest plausible heap address. */
1048 /* In the MMAP case, we map there. */
1049 /* In either case it is used to identify */
1050 /* heap sections so they're not */
1051 /* considered as roots. */
1052 # define OS_TYPE "IRIX5"
1053 # define MPROTECT_VDB
1055 # define CPP_WORDSZ _MIPS_SZPTR
1056 # define ALIGNMENT (_MIPS_SZPTR/8)
1057 # if CPP_WORDSZ != 64
1058 # define ALIGN_DOUBLE
1061 # define ALIGNMENT 4
1062 # define ALIGN_DOUBLE
1064 # define DYNAMIC_LOADING
1069 # define MACH_TYPE "RS6000"
1070 # define ALIGNMENT 4
1071 # define DATASTART ((ptr_t)0x20000000)
1073 # define STACKBOTTOM ((ptr_t)((ulong)&errno))
1074 # define DYNAMIC_LOADING
1075 /* For really old versions of AIX, this may have to be removed. */
1079 /* OS is assumed to be HP/UX */
1080 # define MACH_TYPE "HP_PA"
1081 # define OS_TYPE "HPUX"
1083 # define CPP_WORDSZ 64
1084 # define ALIGNMENT 8
1086 # define CPP_WORDSZ 32
1087 # define ALIGNMENT 4
1088 # define ALIGN_DOUBLE
1090 extern int __data_start
;
1091 # define DATASTART ((ptr_t)(&__data_start))
1093 /* The following appears to work for 7xx systems running HP/UX */
1094 /* 9.xx Furthermore, it might result in much faster */
1095 /* collections than HEURISTIC2, which may involve scanning */
1096 /* segments that directly precede the stack. It is not the */
1097 /* default, since it may not work on older machine/OS */
1098 /* combinations. (Thanks to Raymond X.T. Nijssen for uncovering */
1100 # define STACKBOTTOM ((ptr_t) 0x7b033000) /* from /etc/conf/h/param.h */
1102 /* Gustavo Rodriguez-Rivera suggested changing HEURISTIC2 */
1103 /* to this. We'll probably do this on other platforms, too. */
1104 /* For now I'll use it where I can test it. */
1105 extern char ** environ
;
1106 # define STACKBOTTOM ((ptr_t)environ)
1108 # ifndef SCM_STACK_GROWS_UP /* don't fight with scmconfig.h */
1109 # define SCM_STACK_GROWS_UP 1
1111 # define DYNAMIC_LOADING
1112 # ifndef HPUX_THREADS
1113 # define MPROTECT_VDB
1115 # include <unistd.h>
1116 # define GETPAGESIZE() sysconf(_SC_PAGE_SIZE)
1120 # define MACH_TYPE "ALPHA"
1121 # define ALIGNMENT 8
1122 # define USE_GENERIC_PUSH_REGS
1123 /* Gcc and probably the DEC/Compaq compiler spill pointers to preserved */
1124 /* fp registers in some cases when the target is a 21264. The assembly */
1125 /* code doesn't handle that yet, and version dependencies make that a */
1126 /* bit tricky. Do the easy thing for now. */
1128 # define OS_TYPE "OSF1"
1129 # define DATASTART ((ptr_t) 0x140000000)
1131 # define DATAEND ((ptr_t) &_end)
1133 /* Normally HEURISTIC2 is too conervative, since */
1134 /* the text segment immediately follows the stack. */
1135 /* Hence we give an upper pound. */
1137 # define HEURISTIC2_LIMIT ((ptr_t)((word)(&__start) & ~(getpagesize()-1)))
1138 # define CPP_WORDSZ 64
1139 # define MPROTECT_VDB
1140 # define DYNAMIC_LOADING
1143 # define OS_TYPE "LINUX"
1144 # define CPP_WORDSZ 64
1145 # define STACKBOTTOM ((ptr_t) 0x120000000)
1147 # define LINUX_DATA_START
1148 # define DYNAMIC_LOADING
1149 /* This doesn't work if the collector is in a dynamic library. */
1151 # define DATASTART ((ptr_t) 0x140000000)
1154 # define DATAEND (&_end)
1155 # define MPROTECT_VDB
1156 /* Has only been superficially tested. May not */
1157 /* work on all versions. */
1162 # define MACH_TYPE "IA64"
1163 # define ALIGN_DOUBLE
1164 /* Requires 16 byte alignment for malloc */
1165 # define ALIGNMENT 8
1166 # define USE_GENERIC_PUSH_REGS
1167 /* We need to get preserved registers in addition to register windows. */
1168 /* That's easiest to do with setjmp. */
1173 # define OS_TYPE "LINUX"
1174 # define CPP_WORDSZ 64
1175 /* This should really be done through /proc, but that */
1176 /* requires we run on an IA64 kernel. */
1177 # define STACKBOTTOM ((ptr_t) 0xa000000000000000l)
1178 /* We also need the base address of the register stack */
1179 /* backing store. There is probably a better way to */
1180 /* get that, too ... */
1181 # define BACKING_STORE_BASE ((ptr_t) 0x9fffffff80000000l)
1183 # define SEARCH_FOR_DATA_START
1184 # define DATASTART GC_data_start
1186 extern int data_start
;
1187 # define DATASTART ((ptr_t)(&data_start))
1189 # define DYNAMIC_LOADING
1190 # define MPROTECT_VDB
1191 /* Requires Linux 2.3.47 or later. */
1193 # define DATAEND (&_end)
1194 # define PREFETCH(x) \
1195 __asm__ (" lfetch [%0]": : "r"((void *)(x)))
1196 # define PREFETCH_FOR_WRITE(x) \
1197 __asm__ (" lfetch.excl [%0]": : "r"((void *)(x)))
1198 # define CLEAR_DOUBLE(x) \
1199 __asm__ (" stf.spill [%0]=f0": : "r"((void *)(x)))
1204 # define MACH_TYPE "M88K"
1205 # define ALIGNMENT 4
1206 # define ALIGN_DOUBLE
1209 # define OS_TYPE "CX_UX"
1210 # define DATASTART ((((word)&etext + 0x3fffff) & ~0x3fffff) + 0x10000)
1213 # define OS_TYPE "DGUX"
1214 extern char * GC_SysVGetDataStart();
1215 # define DATASTART (ptr_t)GC_SysVGetDataStart(0x10000, &etext)
1217 # define STACKBOTTOM ((char*)0xf0000000) /* determined empirically */
1221 # define MACH_TYPE "S370"
1222 # define OS_TYPE "UTS4"
1223 # define ALIGNMENT 4 /* Required by hardware */
1227 extern char * GC_SysVGetDataStart();
1228 # define DATASTART (ptr_t)GC_SysVGetDataStart(0x10000, &_etext)
1229 # define DATAEND (&_end)
1234 # define ALIGNMENT 4
1236 # define DATASTART ((ptr_t)(&_etext))
1241 # define CPP_WORDSZ 32
1242 # define MACH_TYPE "ARM32"
1243 # define ALIGNMENT 4
1245 # define OS_TYPE "NETBSD"
1248 # define DATASTART ((ptr_t)(&etext))
1249 # define USE_GENERIC_PUSH_REGS
1252 # define OS_TYPE "LINUX"
1255 # define STACK_GRAN 0x10000000
1256 # define USE_GENERIC_PUSH_REGS
1258 # define DYNAMIC_LOADING
1259 # include <features.h>
1260 # if defined(__GLIBC__) && __GLIBC__ >= 2
1261 # define LINUX_DATA_START
1263 extern char **__environ
;
1264 # define DATASTART ((ptr_t)(&__environ))
1265 /* hideous kludge: __environ is the first */
1266 /* word in crt0.o, and delimits the start */
1267 /* of the data segment, no matter which */
1268 /* ld options were passed through. */
1269 /* We could use _etext instead, but that */
1270 /* would include .rodata, which may */
1271 /* contain large read-only data tables */
1272 /* that we'd rather not scan. */
1275 # define DATAEND (&_end)
1278 # define DATASTART ((ptr_t)((((word) (&etext)) + 0xfff) & ~0xfff))
1283 #ifdef LINUX_DATA_START
1284 /* Some Linux distributions arrange to define __data_start. Some */
1285 /* define data_start as a weak symbol. The latter is technically */
1286 /* broken, since the user program may define data_start, in which */
1287 /* case we lose. Nonetheless, we try both, prefering __data_start. */
1288 /* We assume gcc. */
1289 # pragma weak __data_start
1290 extern int __data_start
;
1291 # pragma weak data_start
1292 extern int data_start
;
1293 # define DATASTART ((ptr_t)(&__data_start != 0? &__data_start : &data_start))
1296 # if SCM_STACK_GROWS_UP
1297 # define STACK_GROWS_DOWN 0
1299 # define STACK_GROWS_DOWN 1
1303 # define CPP_WORDSZ 32
1312 # define DATAEND (&end)
1315 # if defined(SVR4) && !defined(GETPAGESIZE)
1316 # include <unistd.h>
1317 # define GETPAGESIZE() sysconf(_SC_PAGESIZE)
1320 # ifndef GETPAGESIZE
1321 # if defined(SUNOS5) || defined(IRIX5)
1322 # include <unistd.h>
1324 # define GETPAGESIZE() getpagesize()
1327 # if defined(SUNOS5) || defined(DRSNX) || defined(UTS4)
1328 /* OS has SVR4 generic features. Probably others also qualify. */
1332 # if defined(SUNOS5) || defined(DRSNX)
1333 /* OS has SUNOS5 style semi-undocumented interface to dynamic */
1336 /* OS has SUNOS5 style signal handlers. */
1344 # if CPP_WORDSZ != 32 && CPP_WORDSZ != 64
1349 # undef DYNAMIC_LOADING
1354 # undef MPROTECT_VDB
1359 /* Postponed for now. */
1361 # undef MPROTECT_VDB
1364 # ifdef SMALL_CONFIG
1365 /* Presumably not worth the space it takes. */
1367 # undef MPROTECT_VDB
1371 # undef MPROTECT_VDB /* Can't deal with address space holes. */
1374 # if !defined(PCR_VDB) && !defined(PROC_VDB) && !defined(MPROTECT_VDB)
1375 # define DEFAULT_VDB
1379 # define PREFETCH(x)
1380 # define NO_PREFETCH
1383 # ifndef PREFETCH_FOR_WRITE
1384 # define PREFETCH_FOR_WRITE(x)
1385 # define NO_PREFETCH_FOR_WRITE
1388 # ifndef CACHE_LINE_SIZE
1389 # define CACHE_LINE_SIZE 32 /* Wild guess */
1392 # ifndef CLEAR_DOUBLE
1393 # define CLEAR_DOUBLE(x) \
1394 ((word*)x)[0] = 0; \
1396 # endif /* CLEAR_DOUBLE */
1398 # if defined(_SOLARIS_PTHREADS) && !defined(SOLARIS_THREADS)
1399 # define SOLARIS_THREADS
1401 # if defined(IRIX_THREADS) && !defined(IRIX5)
1402 --> inconsistent configuration
1404 # if defined(IRIX_JDK_THREADS) && !defined(IRIX5)
1405 --> inconsistent configuration
1407 # if defined(LINUX_THREADS) && !defined(LINUX)
1408 --> inconsistent configuration
1410 # if defined(SOLARIS_THREADS) && !defined(SUNOS5)
1411 --> inconsistent configuration
1413 # if defined(HPUX_THREADS) && !defined(HPUX)
1414 --> inconsistent configuration
1416 # if defined(PCR) || defined(SRC_M3) || \
1417 defined(SOLARIS_THREADS) || defined(WIN32_THREADS) || \
1418 defined(IRIX_THREADS) || defined(LINUX_THREADS) || \
1419 defined(IRIX_JDK_THREADS) || defined(HPUX_THREADS)
1423 # if defined(HP_PA) || defined(M88K) || defined(POWERPC) \
1424 || (defined(I386) && defined(OS2)) || defined(UTS4) || defined(LINT)
1425 /* Use setjmp based hack to mark from callee-save registers. */
1426 # define USE_GENERIC_PUSH_REGS
1428 # if defined(SPARC) && !defined(LINUX)
1429 # define SAVE_CALL_CHAIN
1430 # define ASM_CLEAR_CODE /* Stack clearing is crucial, and we */
1431 /* include assembly code to do it well. */
1434 # if defined(LINUX) && !defined(POWERPC)
1437 # include <linux/version.h>
1438 # if (LINUX_VERSION_CODE <= 0x10400)
1439 /* Ugly hack to get struct sigcontext_struct definition. Required */
1440 /* for some early 1.3.X releases. Will hopefully go away soon. */
1441 /* in some later Linux releases, asm/sigcontext.h may have to */
1442 /* be included instead. */
1444 # include <asm/signal.h>
1450 /* Kernels prior to 2.1.1 defined struct sigcontext_struct instead of */
1451 /* struct sigcontext. libc6 (glibc2) uses "struct sigcontext" in */
1452 /* prototypes, so we have to include the top-level sigcontext.h to */
1453 /* make sure the former gets defined to be the latter if appropriate. */
1454 # include <features.h>
1456 # if 2 == __GLIBC__ && 0 == __GLIBC_MINOR__
1457 /* glibc 2.1 no longer has sigcontext.h. But signal.h */
1458 /* has the right declaration for glibc 2.1. */
1459 # include <sigcontext.h>
1460 # endif /* 0 == __GLIBC_MINOR__ */
1461 # else /* not 2 <= __GLIBC__ */
1462 /* libc5 doesn't have <sigcontext.h>: go directly with the kernel */
1463 /* one. Check LINUX_VERSION_CODE to see which we should reference. */
1464 # include <asm/sigcontext.h>
1465 # endif /* 2 <= __GLIBC__ */
1468 # if !defined(OS2) && !defined(PCR) && !defined(AMIGA) && !defined(MACOS)
1469 # include <sys/types.h>
1470 # if !defined(MSWIN32) && !defined(SUNOS4)
1471 # include <unistd.h>
1475 # include <signal.h>
1477 /* Blatantly OS dependent routines, except for those that are related */
1478 /* to dynamic loading. */
1480 # if !defined(THREADS) && !defined(STACKBOTTOM) && defined(HEURISTIC2)
1481 # define NEED_FIND_LIMIT
1484 # if defined(IRIX_THREADS) || defined(HPUX_THREADS)
1485 # define NEED_FIND_LIMIT
1488 # if (defined(SUNOS4) && defined(DYNAMIC_LOADING)) && !defined(PCR)
1489 # define NEED_FIND_LIMIT
1492 # if (defined(SVR4) || defined(AUX) || defined(DGUX)) && !defined(PCR)
1493 # define NEED_FIND_LIMIT
1496 # if defined(LINUX) && \
1497 (defined(POWERPC) || defined(SPARC) || defined(ALPHA) || defined(IA64) \
1499 # define NEED_FIND_LIMIT
1502 #ifdef NEED_FIND_LIMIT
1503 # include <setjmp.h>
1507 # include <machine/trap.h>
1511 # include <proto/exec.h>
1512 # include <proto/dos.h>
1513 # include <dos/dosextens.h>
1514 # include <workbench/startup.h>
1518 # define WIN32_LEAN_AND_MEAN
1520 # include <windows.h>
1524 # include <Processes.h>
1528 # include <sys/uio.h>
1529 # include <malloc.h> /* for locking */
1532 # include <sys/types.h>
1533 # include <sys/mman.h>
1534 # include <sys/stat.h>
1539 # include <sys/siginfo.h>
1542 # define setjmp(env) sigsetjmp(env, 1)
1543 # define longjmp(env, val) siglongjmp(env, val)
1544 # define jmp_buf sigjmp_buf
1548 /* Apparently necessary for djgpp 2.01. May casuse problems with */
1549 /* other versions. */
1550 typedef long unsigned int caddr_t
;
1554 # include "il/PCR_IL.h"
1555 # include "th/PCR_ThCtl.h"
1556 # include "mm/PCR_MM.h"
1559 #if !defined(NO_EXECUTE_PERMISSION)
1560 # define OPT_PROT_EXEC PROT_EXEC
1562 # define OPT_PROT_EXEC 0
1567 # include <stddef.h>
1569 # if !defined(__IBMC__) && !defined(__WATCOMC__) /* e.g. EMX */
1571 # else /* IBM's compiler */
1573 /* A kludge to get around what appears to be a header file bug */
1575 # define WORD unsigned short
1578 # define DWORD unsigned long
1582 # include <newexe.h>
1583 # include <exe386.h>
1585 # endif /* __IBMC__ */
1587 # define INCL_DOSEXCEPTIONS
1588 # define INCL_DOSPROCESS
1589 # define INCL_DOSERRORS
1590 # define INCL_DOSMODULEMGR
1591 # define INCL_DOSMEMMGR
1597 * Find the base of the stack.
1598 * Used only in single-threaded environment.
1599 * With threads, GC_mark_roots needs to know how to do this.
1600 * Called with allocator lock held.
1603 # define is_writable(prot) ((prot) == PAGE_READWRITE \
1604 || (prot) == PAGE_WRITECOPY \
1605 || (prot) == PAGE_EXECUTE_READWRITE \
1606 || (prot) == PAGE_EXECUTE_WRITECOPY)
1607 /* Return the number of bytes that are writable starting at p. */
1608 /* The pointer p is assumed to be page aligned. */
1609 /* If base is not 0, *base becomes the beginning of the */
1610 /* allocation region containing p. */
1611 static word
GC_get_writable_length(ptr_t p
, ptr_t
*base
)
1613 MEMORY_BASIC_INFORMATION buf
;
1617 result
= VirtualQuery(p
, &buf
, sizeof(buf
));
1618 if (result
!= sizeof(buf
)) ABORT("Weird VirtualQuery result");
1619 if (base
!= 0) *base
= (ptr_t
)(buf
.AllocationBase
);
1620 protect
= (buf
.Protect
& ~(PAGE_GUARD
| PAGE_NOCACHE
));
1621 if (!is_writable(protect
)) {
1624 if (buf
.State
!= MEM_COMMIT
) return(0);
1625 return(buf
.RegionSize
);
1628 void *scm_get_stack_base()
1631 ptr_t sp
= (ptr_t
)(&dummy
);
1634 static word GC_page_size
= 0;
1635 if (!GC_page_size
) {
1636 SYSTEM_INFO sysinfo
;
1637 GetSystemInfo(&sysinfo
);
1638 GC_page_size
= sysinfo
.dwPageSize
;
1640 trunc_sp
= (ptr_t
)((word
)sp
& ~(GC_page_size
- 1));
1641 size
= GC_get_writable_length(trunc_sp
, 0);
1642 return(trunc_sp
+ size
);
1650 void *scm_get_stack_base()
1655 if (DosGetInfoBlocks(&ptib
, &ppib
) != NO_ERROR
) {
1656 GC_err_printf0("DosGetInfoBlocks failed\n");
1657 ABORT("DosGetInfoBlocks failed\n");
1659 return((ptr_t
)(ptib
-> tib_pstacklimit
));
1666 void *scm_get_stack_base()
1668 struct Process
*proc
= (struct Process
*)SysBase
->ThisTask
;
1670 /* Reference: Amiga Guru Book Pages: 42,567,574 */
1671 if (proc
->pr_Task
.tc_Node
.ln_Type
==NT_PROCESS
1672 && proc
->pr_CLI
!= NULL
) {
1673 /* first ULONG is StackSize */
1674 /*longPtr = proc->pr_ReturnAddr;
1675 size = longPtr[0];*/
1677 return (char *)proc
->pr_ReturnAddr
+ sizeof(ULONG
);
1679 return (char *)proc
->pr_Task
.tc_SPUpper
;
1683 #if 0 /* old version */
1684 void *scm_get_stack_base()
1686 extern struct WBStartup
*_WBenchMsg
;
1688 extern long __stack
;
1690 struct Process
*proc
;
1691 struct CommandLineInterface
*cli
;
1694 if ((task
= FindTask(0)) == 0) {
1695 GC_err_puts("Cannot find own task structure\n");
1696 ABORT("task missing");
1698 proc
= (struct Process
*)task
;
1699 cli
= BADDR(proc
->pr_CLI
);
1701 if (_WBenchMsg
!= 0 || cli
== 0) {
1702 size
= (char *)task
->tc_SPUpper
- (char *)task
->tc_SPLower
;
1704 size
= cli
->cli_DefaultStack
* 4;
1706 return (ptr_t
)(__base
+ GC_max(size
, __stack
));
1710 # else /* !AMIGA, !OS2, ... */
1712 # ifdef NEED_FIND_LIMIT
1713 /* Some tools to implement HEURISTIC2 */
1714 # define MIN_PAGE_SIZE 256 /* Smallest conceivable page size, bytes */
1715 /* static */ jmp_buf GC_jmp_buf
;
1718 static void GC_fault_handler(sig
)
1721 longjmp(GC_jmp_buf
, 1);
1725 typedef void (*handler
)(int);
1727 typedef void (*handler
)();
1730 # if defined(SUNOS5SIGS) || defined(IRIX5) || defined(OSF1)
1731 static struct sigaction old_segv_act
;
1732 # if defined(_sigargs) || defined(HPUX) /* !Irix6.x */
1733 static struct sigaction old_bus_act
;
1736 static handler old_segv_handler
, old_bus_handler
;
1739 static void GC_setup_temporary_fault_handler()
1741 # if defined(SUNOS5SIGS) || defined(IRIX5) || defined(OSF1)
1742 struct sigaction act
;
1744 act
.sa_handler
= GC_fault_handler
;
1745 act
.sa_flags
= SA_RESTART
| SA_NODEFER
;
1746 /* The presence of SA_NODEFER represents yet another gross */
1747 /* hack. Under Solaris 2.3, siglongjmp doesn't appear to */
1748 /* interact correctly with -lthread. We hide the confusion */
1749 /* by making sure that signal handling doesn't affect the */
1752 (void) sigemptyset(&act
.sa_mask
);
1753 # ifdef IRIX_THREADS
1754 /* Older versions have a bug related to retrieving and */
1755 /* and setting a handler at the same time. */
1756 (void) sigaction(SIGSEGV
, 0, &old_segv_act
);
1757 (void) sigaction(SIGSEGV
, &act
, 0);
1759 (void) sigaction(SIGSEGV
, &act
, &old_segv_act
);
1760 # if defined(IRIX5) && defined(_sigargs) /* Irix 5.x, not 6.x */ \
1762 /* Under Irix 5.x or HP/UX, we may get SIGBUS. */
1763 /* Pthreads doesn't exist under Irix 5.x, so we */
1764 /* don't have to worry in the threads case. */
1765 (void) sigaction(SIGBUS
, &act
, &old_bus_act
);
1767 # endif /* IRIX_THREADS */
1769 old_segv_handler
= signal(SIGSEGV
, GC_fault_handler
);
1771 old_bus_handler
= signal(SIGBUS
, GC_fault_handler
);
1776 static void GC_reset_fault_handler()
1778 # if defined(SUNOS5SIGS) || defined(IRIX5) || defined(OSF1)
1779 (void) sigaction(SIGSEGV
, &old_segv_act
, 0);
1780 # if defined(IRIX5) && defined(_sigargs) /* Irix 5.x, not 6.x */ \
1782 (void) sigaction(SIGBUS
, &old_bus_act
, 0);
1785 (void) signal(SIGSEGV
, old_segv_handler
);
1787 (void) signal(SIGBUS
, old_bus_handler
);
1792 /* Single argument version, robust against whole program analysis. */
1797 static VOLATILE word sink
;
1801 /* Return the first nonaddressible location > p (up) or */
1802 /* the smallest location q s.t. [q,p] is addressible (!up). */
1803 static ptr_t
GC_find_limit(p
, up
)
1807 static VOLATILE ptr_t result
;
1808 /* Needs to be static, since otherwise it may not be */
1809 /* preserved across the longjmp. Can safely be */
1810 /* static since it's only called once, with the */
1811 /* allocation lock held. */
1814 GC_setup_temporary_fault_handler();
1815 if (setjmp(GC_jmp_buf
) == 0) {
1816 result
= (ptr_t
)(((word
)(p
))
1817 & ~(MIN_PAGE_SIZE
-1));
1820 result
+= MIN_PAGE_SIZE
;
1822 result
-= MIN_PAGE_SIZE
;
1824 GC_noop1((word
)(*result
));
1827 GC_reset_fault_handler();
1829 result
+= MIN_PAGE_SIZE
;
1836 #ifdef LINUX_STACKBOTTOM
1838 #include <sys/types.h>
1839 #include <sys/stat.h>
1842 # define STAT_SKIP 27 /* Number of fields preceding startstack */
1843 /* field in /proc/self/stat */
1845 static ptr_t
GC_linux_stack_base(void)
1847 /* We read the stack base value from /proc/self/stat. We do this */
1848 /* using direct I/O system calls in order to avoid calling malloc */
1849 /* in case REDIRECT_MALLOC is defined. */
1850 # define STAT_BUF_SIZE 4096
1852 # define STAT_READ __real_read
1854 # define STAT_READ read
1856 char stat_buf
[STAT_BUF_SIZE
];
1860 size_t i
, buf_offset
= 0;
1862 f
= open("/proc/self/stat", O_RDONLY
);
1863 if (f
< 0 || STAT_READ(f
, stat_buf
, STAT_BUF_SIZE
) < 2 * STAT_SKIP
) {
1864 ABORT("Couldn't read /proc/self/stat");
1866 c
= stat_buf
[buf_offset
++];
1867 /* Skip the required number of fields. This number is hopefully */
1868 /* constant across all Linux implementations. */
1869 for (i
= 0; i
< STAT_SKIP
; ++i
) {
1870 while (isspace(c
)) c
= stat_buf
[buf_offset
++];
1871 while (!isspace(c
)) c
= stat_buf
[buf_offset
++];
1873 while (isspace(c
)) c
= stat_buf
[buf_offset
++];
1874 while (isdigit(c
)) {
1877 c
= stat_buf
[buf_offset
++];
1880 if (result
< 0x10000000) ABORT("Absurd stack bottom value");
1881 return (ptr_t
)result
;
1884 #endif /* LINUX_STACKBOTTOM */
1886 void *scm_get_stack_base()
1891 result
= &dummy
; /* initialize to silence compiler */
1893 # define STACKBOTTOM_ALIGNMENT_M1 ((word)STACK_GRAN - 1)
1896 return(STACKBOTTOM
);
1899 # if STACK_GROWS_DOWN
1900 result
= (ptr_t
)((((word
)(&dummy
))
1901 + STACKBOTTOM_ALIGNMENT_M1
)
1902 & ~STACKBOTTOM_ALIGNMENT_M1
);
1904 result
= (ptr_t
)(((word
)(&dummy
))
1905 & ~STACKBOTTOM_ALIGNMENT_M1
);
1907 # endif /* HEURISTIC1 */
1908 # ifdef LINUX_STACKBOTTOM
1909 result
= GC_linux_stack_base();
1912 # if STACK_GROWS_DOWN
1913 result
= GC_find_limit((ptr_t
)(&dummy
), TRUE
);
1914 # ifdef HEURISTIC2_LIMIT
1915 if (result
> HEURISTIC2_LIMIT
1916 && (ptr_t
)(&dummy
) < HEURISTIC2_LIMIT
) {
1917 result
= HEURISTIC2_LIMIT
;
1921 result
= GC_find_limit((ptr_t
)(&dummy
), FALSE
);
1922 # ifdef HEURISTIC2_LIMIT
1923 if (result
< HEURISTIC2_LIMIT
1924 && (ptr_t
)(&dummy
) > HEURISTIC2_LIMIT
) {
1925 result
= HEURISTIC2_LIMIT
;
1930 # endif /* HEURISTIC2 */
1931 # if STACK_GROWS_DOWN
1932 if (result
== 0) result
= (ptr_t
)(signed_word
)(-sizeof(ptr_t
));
1935 # endif /* STACKBOTTOM */
1938 # endif /* ! AMIGA */
1940 # endif /* ! MSWIN32 */
1942 #endif /* mach_type_known */
1943 #endif /* ! HAVE_LIBC_STACK_END */