2 * Copyright 1988, 1989, 2010 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"
709 # define OS_TYPE "SUNOS5"
710 extern int _etext[], _end[];
711 ptr_t GC_SysVGetDataStart(size_t, ptr_t);
712 # define DATASTART GC_SysVGetDataStart(0x1000, (ptr_t)_etext)
713 # define DATAEND (ptr_t)(_end)
714 /* # define STACKBOTTOM ((ptr_t)(_start)) worked through 2.7, */
715 /* but reportedly breaks under 2.8. It appears that the stack */
716 /* base is a property of the executable, so this should not break */
717 /* old executables. */
718 /* HEURISTIC2 probably works, but this appears to be preferable. */
720 # define STACKBOTTOM ((ptr_t) USRSTACK)
725 # define HEAP_START (ptr_t)0x40000000
727 # define HEAP_START DATAEND
730 /* HEURISTIC1 reportedly no longer works under 2.7. Thus we */
731 /* switched to HEURISTIC2, eventhough it creates some debugging */
735 # define GETPAGESIZE() sysconf(_SC_PAGESIZE)
736 /* getpagesize() appeared to be missing from at least one */
737 /* Solaris 5.4 installation. Weird. */
738 # define DYNAMIC_LOADING
741 # define OS_TYPE "SUNOS4"
742 /* [If you have a weak stomach, don't read this.] */
743 /* We would like to use: */
744 /* # define DATASTART ((ptr_t)((((word) (&etext)) + 0x1fff) & ~0x1fff)) */
745 /* This fails occasionally, due to an ancient, but very */
746 /* persistent ld bug. &etext is set 32 bytes too high. */
747 /* We instead read the text segment size from the a.out */
748 /* header, which happens to be mapped into our address space */
749 /* at the start of the text segment. The detective work here */
750 /* was done by Robert Ehrlich, Manuel Serrano, and Bernard */
751 /* Serpette of INRIA. */
752 /* This assumes ZMAGIC, i.e. demand-loadable executables. */
753 # define TEXTSTART 0x2000
754 # define DATASTART ((ptr_t)(*(int *)(TEXTSTART+0x4)+TEXTSTART))
755 # define MPROTECT_VDB
757 # define DYNAMIC_LOADING
760 # define CPP_WORDSZ 32
761 # define OS_TYPE "DRSNX"
762 extern char * GC_SysVGetDataStart();
764 # define DATASTART (ptr_t)GC_SysVGetDataStart(0x10000, &etext)
765 # define MPROTECT_VDB
766 # define STACKBOTTOM ((ptr_t) 0xdfff0000)
767 # define DYNAMIC_LOADING
770 # define OS_TYPE "LINUX"
772 # define LINUX_DATA_START
773 # define DYNAMIC_LOADING
775 Linux Sparc non elf ?
778 # define DATAEND (&_end)
780 # define STACKBOTTOM ((ptr_t) 0xf0000000)
783 # define OS_TYPE "OPENBSD"
784 # define STACKBOTTOM ((ptr_t) 0xf8000000)
785 # define DATASTART ((ptr_t)(&etext))
790 # define MACH_TYPE "I386"
791 # define ALIGNMENT 4 /* Appears to hold for all "32 bit" compilers */
792 /* except Borland. The -a4 option fixes */
794 /* Ivan Demakov: For Watcom the option is -zp4. */
795 # ifndef SMALL_CONFIG
796 # define ALIGN_DOUBLE /* Not strictly necessary, but may give speed */
797 /* improvement on Pentiums. */
800 # define OS_TYPE "SEQUENT"
802 # define DATASTART ((ptr_t)((((word) (&etext)) + 0xfff) & ~0xfff))
803 # define STACKBOTTOM ((ptr_t) 0x3ffff000)
806 # define OS_TYPE "SUNOS5"
807 extern int _etext[], _end[];
808 ptr_t GC_SysVGetDataStart(size_t, ptr_t);
809 # define DATASTART GC_SysVGetDataStart(0x1000, (ptr_t)_etext)
810 # define DATAEND (ptr_t)(_end)
811 /* # define STACKBOTTOM ((ptr_t)(_start)) worked through 2.7, */
812 /* but reportedly breaks under 2.8. It appears that the stack */
813 /* base is a property of the executable, so this should not break */
814 /* old executables. */
815 /* HEURISTIC2 probably works, but this appears to be preferable. */
817 # define STACKBOTTOM ((ptr_t) USRSTACK)
818 /** At least in Solaris 2.5, PROC_VDB gives wrong values for dirty bits. */
819 /*# define PROC_VDB*/
820 # define DYNAMIC_LOADING
825 # define HEAP_START (ptr_t)0x40000000
827 # define HEAP_START DATAEND
831 # define OS_TYPE "SCO"
833 # define DATASTART ((ptr_t)((((word) (&etext)) + 0x3fffff) \
835 +((word)&etext & 0xfff))
836 # define STACKBOTTOM ((ptr_t) 0x7ffffffc)
839 # define OS_TYPE "SCO_ELF"
841 # define DATASTART ((ptr_t)(&etext))
842 # define STACKBOTTOM ((ptr_t) 0x08048000)
843 # define DYNAMIC_LOADING
844 # define ELF_CLASS ELFCLASS32
847 # define OS_TYPE "LINUX"
848 # define LINUX_STACKBOTTOM
852 # define STACK_GRAN 0x10000000
853 /* STACKBOTTOM is usually 0xc0000000, but this changes with */
854 /* different kernel configurations. In particular, systems */
855 /* with 2GB physical memory will usually move the user */
856 /* address space limit, and hence initial SP to 0x80000000. */
858 # if !defined(LINUX_THREADS) || !defined(REDIRECT_MALLOC)
859 # define MPROTECT_VDB
861 /* We seem to get random errors in incremental mode, */
862 /* possibly because Linux threads is itself a malloc client */
863 /* and can't deal with the signals. */
866 # define DYNAMIC_LOADING
867 # ifdef UNDEFINED /* includes ro data */
869 # define DATASTART ((ptr_t)((((word) (&_etext)) + 0xfff) & ~0xfff))
871 # include <features.h>
872 # if defined(__GLIBC__) && __GLIBC__ >= 2
873 # define LINUX_DATA_START
875 extern char **__environ;
876 # define DATASTART ((ptr_t)(&__environ))
877 /* hideous kludge: __environ is the first */
878 /* word in crt0.o, and delimits the start */
879 /* of the data segment, no matter which */
880 /* ld options were passed through. */
881 /* We could use _etext instead, but that */
882 /* would include .rodata, which may */
883 /* contain large read-only data tables */
884 /* that we'd rather not scan. */
887 # define DATAEND (&_end)
890 # define DATASTART ((ptr_t)((((word) (&etext)) + 0xfff) & ~0xfff))
892 # ifdef USE_I686_PREFETCH
893 # define PREFETCH(x) \
894 __asm__ __volatile__ (" prefetchnta %0": : "m"(*(char *)(x)))
895 /* Empirically prefetcht0 is much more effective at reducing */
896 /* cache miss stalls for the targetted load instructions. But it */
897 /* seems to interfere enough with other cache traffic that the net */
898 /* result is worse than prefetchnta. */
900 /* Using prefetches for write seems to have a slight negative */
901 /* impact on performance, at least for a PIII/500. */
902 # define PREFETCH_FOR_WRITE(x) \
903 __asm__ __volatile__ (" prefetcht0 %0": : "m"(*(char *)(x)))
906 # ifdef USE_3DNOW_PREFETCH
907 # define PREFETCH(x) \
908 __asm__ __volatile__ (" prefetch %0": : "m"(*(char *)(x)))
909 # define PREFETCH_FOR_WRITE(x)
910 __asm__ __volatile__ (" prefetchw %0": : "m"(*(char *)(x)))
914 # define OS_TYPE "CYGWIN32"
915 extern int _data_start__;
916 extern int _data_end__;
917 extern int _bss_start__;
918 extern int _bss_end__;
919 /* For binutils 2.9.1, we have */
920 /* DATASTART = _data_start__ */
921 /* DATAEND = _bss_end__ */
922 /* whereas for some earlier versions it was */
923 /* DATASTART = _bss_start__ */
924 /* DATAEND = _data_end__ */
925 /* To get it right for both, we take the */
926 /* minumum/maximum of the two. */
927 # define MAX(x,y) ((x) > (y) ? (x) : (y))
928 # define MIN(x,y) ((x) < (y) ? (x) : (y))
929 # define DATASTART ((ptr_t) MIN(&_data_start__, &_bss_start__))
930 # define DATAEND ((ptr_t) MAX(&_data_end__, &_bss_end__))
932 # define STACK_GRAN 0x10000
936 # define OS_TYPE "OS2"
937 /* STACKBOTTOM and DATASTART are handled specially in */
938 /* os_dep.c. OS2 actually has the right */
940 # define DATAEND /* not needed */
943 # define OS_TYPE "MSWIN32"
944 /* STACKBOTTOM and DATASTART are handled specially in */
947 # define MPROTECT_VDB
949 # define DATAEND /* not needed */
952 # define OS_TYPE "DJGPP"
953 # include "stubinfo.h"
956 extern int __djgpp_stack_limit;
957 # define DATASTART ((ptr_t)((((word) (&etext)) + 0x1ff) & ~0x1ff))
958 /* # define STACKBOTTOM ((ptr_t)((word) _stubinfo + _stubinfo->size \
960 # define STACKBOTTOM ((ptr_t)((word) __djgpp_stack_limit + _stklen))
961 /* This may not be right. */
964 # define OS_TYPE "OPENBSD"
967 # define OS_TYPE "FREEBSD"
968 # define MPROTECT_VDB
971 # define OS_TYPE "NETBSD"
974 # define OS_TYPE "THREE86BSD"
977 # define OS_TYPE "BSDI"
979 # if defined(OPENBSD) || defined(FREEBSD) || defined(NETBSD) \
980 || defined(THREE86BSD) || defined(BSDI)
983 # define DATASTART ((ptr_t)(&etext))
986 # define OS_TYPE "NEXT"
987 # define DATASTART ((ptr_t) get_etext())
988 # define STACKBOTTOM ((ptr_t)0xc0000000)
989 # define DATAEND /* not needed */
992 # define OS_TYPE "DOS4GW"
993 extern long __nullarea;
995 extern char *_STACKTOP;
996 /* Depending on calling conventions Watcom C either precedes
997 or does not precedes with undescore names of C-variables.
998 Make sure startup code variables always have the same names. */
999 #pragma aux __nullarea "*";
1000 #pragma aux _end "*";
1001 # define STACKBOTTOM ((ptr_t) _STACKTOP)
1002 /* confused? me too. */
1003 # define DATASTART ((ptr_t) &__nullarea)
1004 # define DATAEND ((ptr_t) &_end)
1007 # define OS_TYPE "GNU"
1012 # define MACH_TYPE "NS32K"
1013 # define ALIGNMENT 4
1014 extern char **environ;
1015 # define DATASTART ((ptr_t)(&environ))
1016 /* hideous kludge: environ is the first */
1017 /* word in crt0.o, and delimits the start */
1018 /* of the data segment, no matter which */
1019 /* ld options were passed through. */
1020 # define STACKBOTTOM ((ptr_t) 0xfffff000) /* for Encore */
1024 # define MACH_TYPE "MIPS"
1026 # define CPP_WORDSZ _MIPS_SZPTR
1027 # define OS_TYPE "LINUX"
1028 # define ALIGNMENT 4
1029 # define ALIGN_DOUBLE
1031 # define DATASTART ((ptr_t)(&_fdata))
1033 # define DATAEND ((ptr_t)(&_end))
1034 # define STACKBOTTOM ((ptr_t)0x7fff8000)
1035 # define USE_GENERIC_PUSH_REGS 1
1036 # define DYNAMIC_LOADING
1040 # define DATASTART (ptr_t)0x10000000
1041 /* Could probably be slightly higher since */
1042 /* startup code allocates lots of stuff. */
1043 # define OS_TYPE "ULTRIX"
1044 # define ALIGNMENT 4
1048 # define DATASTART (ptr_t)0x10000000
1049 # define OS_TYPE "RISCOS"
1050 # define ALIGNMENT 4 /* Required by hardware */
1055 # define DATASTART ((ptr_t)(&_fdata))
1057 # define HEAP_START (ptr_t)0x30000000
1059 # define HEAP_START DATASTART
1061 /* Lowest plausible heap address. */
1062 /* In the MMAP case, we map there. */
1063 /* In either case it is used to identify */
1064 /* heap sections so they're not */
1065 /* considered as roots. */
1066 # define OS_TYPE "IRIX5"
1067 # define MPROTECT_VDB
1069 # define CPP_WORDSZ _MIPS_SZPTR
1070 # define ALIGNMENT (_MIPS_SZPTR/8)
1071 # if CPP_WORDSZ != 64
1072 # define ALIGN_DOUBLE
1075 # define ALIGNMENT 4
1076 # define ALIGN_DOUBLE
1078 # define DYNAMIC_LOADING
1083 # define MACH_TYPE "RS6000"
1084 # define ALIGNMENT 4
1085 # define DATASTART ((ptr_t)0x20000000)
1087 # define STACKBOTTOM ((ptr_t)((ulong)&errno))
1088 # define DYNAMIC_LOADING
1089 /* For really old versions of AIX, this may have to be removed. */
1093 /* OS is assumed to be HP/UX */
1094 # define MACH_TYPE "HP_PA"
1095 # define OS_TYPE "HPUX"
1097 # define CPP_WORDSZ 64
1098 # define ALIGNMENT 8
1100 # define CPP_WORDSZ 32
1101 # define ALIGNMENT 4
1102 # define ALIGN_DOUBLE
1104 extern int __data_start;
1105 # define DATASTART ((ptr_t)(&__data_start))
1107 /* The following appears to work for 7xx systems running HP/UX */
1108 /* 9.xx Furthermore, it might result in much faster */
1109 /* collections than HEURISTIC2, which may involve scanning */
1110 /* segments that directly precede the stack. It is not the */
1111 /* default, since it may not work on older machine/OS */
1112 /* combinations. (Thanks to Raymond X.T. Nijssen for uncovering */
1114 # define STACKBOTTOM ((ptr_t) 0x7b033000) /* from /etc/conf/h/param.h */
1116 /* Gustavo Rodriguez-Rivera suggested changing HEURISTIC2 */
1117 /* to this. We'll probably do this on other platforms, too. */
1118 /* For now I'll use it where I can test it. */
1119 extern char ** environ;
1120 # define STACKBOTTOM ((ptr_t)environ)
1122 # ifndef SCM_STACK_GROWS_UP /* don't fight with scmconfig.h */
1123 # define SCM_STACK_GROWS_UP 1
1125 # define DYNAMIC_LOADING
1126 # ifndef HPUX_THREADS
1127 # define MPROTECT_VDB
1129 # include <unistd.h>
1130 # define GETPAGESIZE() sysconf(_SC_PAGE_SIZE)
1134 # define MACH_TYPE "ALPHA"
1135 # define ALIGNMENT 8
1136 # define USE_GENERIC_PUSH_REGS
1137 /* Gcc and probably the DEC/Compaq compiler spill pointers to preserved */
1138 /* fp registers in some cases when the target is a 21264. The assembly */
1139 /* code doesn't handle that yet, and version dependencies make that a */
1140 /* bit tricky. Do the easy thing for now. */
1142 # define OS_TYPE "OSF1"
1143 # define DATASTART ((ptr_t) 0x140000000)
1145 # define DATAEND ((ptr_t) &_end)
1147 /* Normally HEURISTIC2 is too conervative, since */
1148 /* the text segment immediately follows the stack. */
1149 /* Hence we give an upper pound. */
1151 # define HEURISTIC2_LIMIT ((ptr_t)((word)(&__start) & ~(getpagesize()-1)))
1152 # define CPP_WORDSZ 64
1153 # define MPROTECT_VDB
1154 # define DYNAMIC_LOADING
1157 # define OS_TYPE "LINUX"
1158 # define CPP_WORDSZ 64
1159 # define STACKBOTTOM ((ptr_t) 0x120000000)
1161 # define LINUX_DATA_START
1162 # define DYNAMIC_LOADING
1163 /* This doesn't work if the collector is in a dynamic library. */
1165 # define DATASTART ((ptr_t) 0x140000000)
1168 # define DATAEND (&_end)
1169 # define MPROTECT_VDB
1170 /* Has only been superficially tested. May not */
1171 /* work on all versions. */
1176 # define MACH_TYPE "IA64"
1177 # define ALIGN_DOUBLE
1178 /* Requires 16 byte alignment for malloc */
1179 # define ALIGNMENT 8
1180 # define USE_GENERIC_PUSH_REGS
1181 /* We need to get preserved registers in addition to register windows. */
1182 /* That's easiest to do with setjmp. */
1187 # define OS_TYPE "LINUX"
1188 # define CPP_WORDSZ 64
1189 /* This should really be done through /proc, but that */
1190 /* requires we run on an IA64 kernel. */
1191 # define STACKBOTTOM ((ptr_t) 0xa000000000000000l)
1192 /* We also need the base address of the register stack */
1193 /* backing store. There is probably a better way to */
1194 /* get that, too ... */
1195 # define BACKING_STORE_BASE ((ptr_t) 0x9fffffff80000000l)
1197 # define SEARCH_FOR_DATA_START
1198 # define DATASTART GC_data_start
1200 extern int data_start;
1201 # define DATASTART ((ptr_t)(&data_start))
1203 # define DYNAMIC_LOADING
1204 # define MPROTECT_VDB
1205 /* Requires Linux 2.3.47 or later. */
1207 # define DATAEND (&_end)
1208 # define PREFETCH(x) \
1209 __asm__ (" lfetch [%0]": : "r"((void *)(x)))
1210 # define PREFETCH_FOR_WRITE(x) \
1211 __asm__ (" lfetch.excl [%0]": : "r"((void *)(x)))
1212 # define CLEAR_DOUBLE(x) \
1213 __asm__ (" stf.spill [%0]=f0": : "r"((void *)(x)))
1218 # define MACH_TYPE "M88K"
1219 # define ALIGNMENT 4
1220 # define ALIGN_DOUBLE
1223 # define OS_TYPE "CX_UX"
1224 # define DATASTART ((((word)&etext + 0x3fffff) & ~0x3fffff) + 0x10000)
1227 # define OS_TYPE "DGUX"
1228 extern char * GC_SysVGetDataStart();
1229 # define DATASTART (ptr_t)GC_SysVGetDataStart(0x10000, &etext)
1231 # define STACKBOTTOM ((char*)0xf0000000) /* determined empirically */
1235 # define MACH_TYPE "S370"
1236 # define OS_TYPE "UTS4"
1237 # define ALIGNMENT 4 /* Required by hardware */
1241 extern char * GC_SysVGetDataStart();
1242 # define DATASTART (ptr_t)GC_SysVGetDataStart(0x10000, &_etext)
1243 # define DATAEND (&_end)
1248 # define ALIGNMENT 4
1250 # define DATASTART ((ptr_t)(&_etext))
1255 # define CPP_WORDSZ 32
1256 # define MACH_TYPE "ARM32"
1257 # define ALIGNMENT 4
1259 # define OS_TYPE "NETBSD"
1262 # define DATASTART ((ptr_t)(&etext))
1263 # define USE_GENERIC_PUSH_REGS
1266 # define OS_TYPE "LINUX"
1269 # define STACK_GRAN 0x10000000
1270 # define USE_GENERIC_PUSH_REGS
1272 # define DYNAMIC_LOADING
1273 # include <features.h>
1274 # if defined(__GLIBC__) && __GLIBC__ >= 2
1275 # define LINUX_DATA_START
1277 extern char **__environ;
1278 # define DATASTART ((ptr_t)(&__environ))
1279 /* hideous kludge: __environ is the first */
1280 /* word in crt0.o, and delimits the start */
1281 /* of the data segment, no matter which */
1282 /* ld options were passed through. */
1283 /* We could use _etext instead, but that */
1284 /* would include .rodata, which may */
1285 /* contain large read-only data tables */
1286 /* that we'd rather not scan. */
1289 # define DATAEND (&_end)
1292 # define DATASTART ((ptr_t)((((word) (&etext)) + 0xfff) & ~0xfff))
1297 #ifdef LINUX_DATA_START
1298 /* Some Linux distributions arrange to define __data_start. Some */
1299 /* define data_start as a weak symbol. The latter is technically */
1300 /* broken, since the user program may define data_start, in which */
1301 /* case we lose. Nonetheless, we try both, prefering __data_start. */
1302 /* We assume gcc. */
1303 # pragma weak __data_start
1304 extern int __data_start;
1305 # pragma weak data_start
1306 extern int data_start;
1307 # define DATASTART ((ptr_t)(&__data_start != 0? &__data_start : &data_start))
1310 # if SCM_STACK_GROWS_UP
1311 # define STACK_GROWS_DOWN 0
1313 # define STACK_GROWS_DOWN 1
1317 # define CPP_WORDSZ 32
1326 # define DATAEND (&end)
1329 # if defined(SVR4) && !defined(GETPAGESIZE)
1330 # include <unistd.h>
1331 # define GETPAGESIZE() sysconf(_SC_PAGESIZE)
1334 # ifndef GETPAGESIZE
1335 # if defined(SUNOS5) || defined(IRIX5)
1336 # include <unistd.h>
1338 # define GETPAGESIZE() getpagesize()
1341 # if defined(SUNOS5) || defined(DRSNX) || defined(UTS4)
1342 /* OS has SVR4 generic features. Probably others also qualify. */
1346 # if defined(SUNOS5) || defined(DRSNX)
1347 /* OS has SUNOS5 style semi-undocumented interface to dynamic */
1350 /* OS has SUNOS5 style signal handlers. */
1358 # if CPP_WORDSZ != 32 && CPP_WORDSZ != 64
1363 # undef DYNAMIC_LOADING
1368 # undef MPROTECT_VDB
1373 /* Postponed for now. */
1375 # undef MPROTECT_VDB
1378 # ifdef SMALL_CONFIG
1379 /* Presumably not worth the space it takes. */
1381 # undef MPROTECT_VDB
1385 # undef MPROTECT_VDB /* Can't deal with address space holes. */
1388 # if !defined(PCR_VDB) && !defined(PROC_VDB) && !defined(MPROTECT_VDB)
1389 # define DEFAULT_VDB
1393 # define PREFETCH(x)
1394 # define NO_PREFETCH
1397 # ifndef PREFETCH_FOR_WRITE
1398 # define PREFETCH_FOR_WRITE(x)
1399 # define NO_PREFETCH_FOR_WRITE
1402 # ifndef CACHE_LINE_SIZE
1403 # define CACHE_LINE_SIZE 32 /* Wild guess */
1406 # ifndef CLEAR_DOUBLE
1407 # define CLEAR_DOUBLE(x) \
1408 ((word*)x)[0] = 0; \
1410 # endif /* CLEAR_DOUBLE */
1412 # if defined(_SOLARIS_PTHREADS) && !defined(SOLARIS_THREADS)
1413 # define SOLARIS_THREADS
1415 # if defined(IRIX_THREADS) && !defined(IRIX5)
1416 --> inconsistent configuration
1418 # if defined(IRIX_JDK_THREADS) && !defined(IRIX5)
1419 --> inconsistent configuration
1421 # if defined(LINUX_THREADS) && !defined(LINUX)
1422 --> inconsistent configuration
1424 # if defined(SOLARIS_THREADS) && !defined(SUNOS5)
1425 --> inconsistent configuration
1427 # if defined(HPUX_THREADS) && !defined(HPUX)
1428 --> inconsistent configuration
1430 # if defined(PCR) || defined(SRC_M3) || \
1431 defined(SOLARIS_THREADS) || defined(WIN32_THREADS) || \
1432 defined(IRIX_THREADS) || defined(LINUX_THREADS) || \
1433 defined(IRIX_JDK_THREADS) || defined(HPUX_THREADS)
1437 # if defined(HP_PA) || defined(M88K) || defined(POWERPC) \
1438 || (defined(I386) && defined(OS2)) || defined(UTS4) || defined(LINT)
1439 /* Use setjmp based hack to mark from callee-save registers. */
1440 # define USE_GENERIC_PUSH_REGS
1442 # if defined(SPARC) && !defined(LINUX)
1443 # define SAVE_CALL_CHAIN
1444 # define ASM_CLEAR_CODE /* Stack clearing is crucial, and we */
1445 /* include assembly code to do it well. */
1448 # if defined(LINUX) && !defined(POWERPC)
1451 # include <linux/version.h>
1452 # if (LINUX_VERSION_CODE <= 0x10400)
1453 /* Ugly hack to get struct sigcontext_struct definition. Required */
1454 /* for some early 1.3.X releases. Will hopefully go away soon. */
1455 /* in some later Linux releases, asm/sigcontext.h may have to */
1456 /* be included instead. */
1458 # include <asm/signal.h>
1464 /* Kernels prior to 2.1.1 defined struct sigcontext_struct instead of */
1465 /* struct sigcontext. libc6 (glibc2) uses "struct sigcontext" in */
1466 /* prototypes, so we have to include the top-level sigcontext.h to */
1467 /* make sure the former gets defined to be the latter if appropriate. */
1468 # include <features.h>
1470 # if 2 == __GLIBC__ && 0 == __GLIBC_MINOR__
1471 /* glibc 2.1 no longer has sigcontext.h. But signal.h */
1472 /* has the right declaration for glibc 2.1. */
1473 # include <sigcontext.h>
1474 # endif /* 0 == __GLIBC_MINOR__ */
1475 # else /* not 2 <= __GLIBC__ */
1476 /* libc5 doesn't have <sigcontext.h>: go directly with the kernel */
1477 /* one. Check LINUX_VERSION_CODE to see which we should reference. */
1478 # include <asm/sigcontext.h>
1479 # endif /* 2 <= __GLIBC__ */
1482 # if !defined(OS2) && !defined(PCR) && !defined(AMIGA) && !defined(MACOS)
1483 # include <sys/types.h>
1484 # if !defined(MSWIN32) && !defined(SUNOS4)
1485 # include <unistd.h>
1489 # include <signal.h>
1491 /* Blatantly OS dependent routines, except for those that are related */
1492 /* to dynamic loading. */
1494 # if !defined(THREADS) && !defined(STACKBOTTOM) && defined(HEURISTIC2)
1495 # define NEED_FIND_LIMIT
1498 # if defined(IRIX_THREADS) || defined(HPUX_THREADS)
1499 # define NEED_FIND_LIMIT
1502 # if (defined(SUNOS4) && defined(DYNAMIC_LOADING)) && !defined(PCR)
1503 # define NEED_FIND_LIMIT
1506 # if (defined(SVR4) || defined(AUX) || defined(DGUX)) && !defined(PCR)
1507 # define NEED_FIND_LIMIT
1510 # if defined(LINUX) && \
1511 (defined(POWERPC) || defined(SPARC) || defined(ALPHA) || defined(IA64) \
1513 # define NEED_FIND_LIMIT
1516 #ifdef NEED_FIND_LIMIT
1517 # include <setjmp.h>
1521 # include <machine/trap.h>
1525 # include <proto/exec.h>
1526 # include <proto/dos.h>
1527 # include <dos/dosextens.h>
1528 # include <workbench/startup.h>
1532 # define WIN32_LEAN_AND_MEAN
1534 # include <windows.h>
1538 # include <Processes.h>
1542 # include <sys/uio.h>
1543 # include <malloc.h> /* for locking */
1546 # include <sys/types.h>
1547 # include <sys/mman.h>
1548 # include <sys/stat.h>
1553 # include <sys/siginfo.h>
1556 # define setjmp(env) sigsetjmp(env, 1)
1557 # define longjmp(env, val) siglongjmp(env, val)
1558 # define jmp_buf sigjmp_buf
1562 /* Apparently necessary for djgpp 2.01. May casuse problems with */
1563 /* other versions. */
1564 typedef long unsigned int caddr_t;
1568 # include "il/PCR_IL.h"
1569 # include "th/PCR_ThCtl.h"
1570 # include "mm/PCR_MM.h"
1573 #if !defined(NO_EXECUTE_PERMISSION)
1574 # define OPT_PROT_EXEC PROT_EXEC
1576 # define OPT_PROT_EXEC 0
1581 # include <stddef.h>
1583 # if !defined(__IBMC__) && !defined(__WATCOMC__) /* e.g. EMX */
1585 # else /* IBM's compiler */
1587 /* A kludge to get around what appears to be a header file bug */
1589 # define WORD unsigned short
1592 # define DWORD unsigned long
1596 # include <newexe.h>
1597 # include <exe386.h>
1599 # endif /* __IBMC__ */
1601 # define INCL_DOSEXCEPTIONS
1602 # define INCL_DOSPROCESS
1603 # define INCL_DOSERRORS
1604 # define INCL_DOSMODULEMGR
1605 # define INCL_DOSMEMMGR
1611 * Find the base of the stack.
1612 * Used only in single-threaded environment.
1613 * With threads, GC_mark_roots needs to know how to do this.
1614 * Called with allocator lock held.
1617 # define is_writable(prot) ((prot) == PAGE_READWRITE \
1618 || (prot) == PAGE_WRITECOPY \
1619 || (prot) == PAGE_EXECUTE_READWRITE \
1620 || (prot) == PAGE_EXECUTE_WRITECOPY)
1621 /* Return the number of bytes that are writable starting at p. */
1622 /* The pointer p is assumed to be page aligned. */
1623 /* If base is not 0, *base becomes the beginning of the */
1624 /* allocation region containing p. */
1625 static word GC_get_writable_length(ptr_t p, ptr_t *base)
1627 MEMORY_BASIC_INFORMATION buf;
1631 result = VirtualQuery(p, &buf, sizeof(buf));
1632 if (result != sizeof(buf)) ABORT("Weird VirtualQuery result");
1633 if (base != 0) *base = (ptr_t)(buf.AllocationBase);
1634 protect = (buf.Protect & ~(PAGE_GUARD | PAGE_NOCACHE));
1635 if (!is_writable(protect)) {
1638 if (buf.State != MEM_COMMIT) return(0);
1639 return(buf.RegionSize);
1642 void *scm_get_stack_base()
1645 ptr_t sp = (ptr_t)(&dummy);
1648 static word GC_page_size = 0;
1649 if (!GC_page_size) {
1650 SYSTEM_INFO sysinfo;
1651 GetSystemInfo(&sysinfo);
1652 GC_page_size = sysinfo.dwPageSize;
1654 trunc_sp = (ptr_t)((word)sp & ~(GC_page_size - 1));
1655 size = GC_get_writable_length(trunc_sp, 0);
1656 return(trunc_sp + size);
1664 void *scm_get_stack_base()
1669 if (DosGetInfoBlocks(&ptib, &ppib) != NO_ERROR) {
1670 GC_err_printf0("DosGetInfoBlocks failed\n");
1671 ABORT("DosGetInfoBlocks failed\n");
1673 return((ptr_t)(ptib -> tib_pstacklimit));
1680 void *scm_get_stack_base()
1682 struct Process *proc = (struct Process*)SysBase->ThisTask;
1684 /* Reference: Amiga Guru Book Pages: 42,567,574 */
1685 if (proc->pr_Task.tc_Node.ln_Type==NT_PROCESS
1686 && proc->pr_CLI != NULL) {
1687 /* first ULONG is StackSize */
1688 /*longPtr = proc->pr_ReturnAddr;
1689 size = longPtr[0];*/
1691 return (char *)proc->pr_ReturnAddr + sizeof(ULONG);
1693 return (char *)proc->pr_Task.tc_SPUpper;
1697 #if 0 /* old version */
1698 void *scm_get_stack_base()
1700 extern struct WBStartup *_WBenchMsg;
1702 extern long __stack;
1704 struct Process *proc;
1705 struct CommandLineInterface *cli;
1708 if ((task = FindTask(0)) == 0) {
1709 GC_err_puts("Cannot find own task structure\n");
1710 ABORT("task missing");
1712 proc = (struct Process *)task;
1713 cli = BADDR(proc->pr_CLI);
1715 if (_WBenchMsg != 0 || cli == 0) {
1716 size = (char *)task->tc_SPUpper - (char *)task->tc_SPLower;
1718 size = cli->cli_DefaultStack * 4;
1720 return (ptr_t)(__base + GC_max(size, __stack));
1724 # else /* !AMIGA, !OS2, ... */
1726 # ifdef NEED_FIND_LIMIT
1727 /* Some tools to implement HEURISTIC2 */
1728 # define MIN_PAGE_SIZE 256 /* Smallest conceivable page size, bytes */
1729 /* static */ jmp_buf GC_jmp_buf;
1732 static void GC_fault_handler(sig)
1735 longjmp(GC_jmp_buf, 1);
1739 typedef void (*handler)(int);
1741 typedef void (*handler)();
1744 # if defined(SUNOS5SIGS) || defined(IRIX5) || defined(OSF1)
1745 static struct sigaction old_segv_act;
1746 # if defined(_sigargs) || defined(HPUX) /* !Irix6.x */
1747 static struct sigaction old_bus_act;
1750 static handler old_segv_handler, old_bus_handler;
1753 static void GC_setup_temporary_fault_handler()
1755 # if defined(SUNOS5SIGS) || defined(IRIX5) || defined(OSF1)
1756 struct sigaction act;
1758 act.sa_handler = GC_fault_handler;
1759 act.sa_flags = SA_RESTART | SA_NODEFER;
1760 /* The presence of SA_NODEFER represents yet another gross */
1761 /* hack. Under Solaris 2.3, siglongjmp doesn't appear to */
1762 /* interact correctly with -lthread. We hide the confusion */
1763 /* by making sure that signal handling doesn't affect the */
1766 (void) sigemptyset(&act.sa_mask);
1767 # ifdef IRIX_THREADS
1768 /* Older versions have a bug related to retrieving and */
1769 /* and setting a handler at the same time. */
1770 (void) sigaction(SIGSEGV, 0, &old_segv_act);
1771 (void) sigaction(SIGSEGV, &act, 0);
1773 (void) sigaction(SIGSEGV, &act, &old_segv_act);
1774 # if defined(IRIX5) && defined(_sigargs) /* Irix 5.x, not 6.x */ \
1776 /* Under Irix 5.x or HP/UX, we may get SIGBUS. */
1777 /* Pthreads doesn't exist under Irix 5.x, so we */
1778 /* don't have to worry in the threads case. */
1779 (void) sigaction(SIGBUS, &act, &old_bus_act);
1781 # endif /* IRIX_THREADS */
1783 old_segv_handler = signal(SIGSEGV, GC_fault_handler);
1785 old_bus_handler = signal(SIGBUS, GC_fault_handler);
1790 static void GC_reset_fault_handler()
1792 # if defined(SUNOS5SIGS) || defined(IRIX5) || defined(OSF1)
1793 (void) sigaction(SIGSEGV, &old_segv_act, 0);
1794 # if defined(IRIX5) && defined(_sigargs) /* Irix 5.x, not 6.x */ \
1796 (void) sigaction(SIGBUS, &old_bus_act, 0);
1799 (void) signal(SIGSEGV, old_segv_handler);
1801 (void) signal(SIGBUS, old_bus_handler);
1806 /* Single argument version, robust against whole program analysis. */
1811 static VOLATILE word sink;
1815 /* Return the first nonaddressible location > p (up) or */
1816 /* the smallest location q s.t. [q,p] is addressible (!up). */
1817 static ptr_t GC_find_limit(p, up)
1821 static VOLATILE ptr_t result;
1822 /* Needs to be static, since otherwise it may not be */
1823 /* preserved across the longjmp. Can safely be */
1824 /* static since it's only called once, with the */
1825 /* allocation lock held. */
1828 GC_setup_temporary_fault_handler();
1829 if (setjmp(GC_jmp_buf) == 0) {
1830 result = (ptr_t)(((word)(p))
1831 & ~(MIN_PAGE_SIZE-1));
1834 result += MIN_PAGE_SIZE;
1836 result -= MIN_PAGE_SIZE;
1838 GC_noop1((word)(*result));
1841 GC_reset_fault_handler();
1843 result += MIN_PAGE_SIZE;
1850 #ifdef LINUX_STACKBOTTOM
1852 #include <sys/types.h>
1853 #include <sys/stat.h>
1856 # define STAT_SKIP 27 /* Number of fields preceding startstack */
1857 /* field in /proc/self/stat */
1859 static ptr_t GC_linux_stack_base(void)
1861 /* We read the stack base value from /proc/self/stat. We do this */
1862 /* using direct I/O system calls in order to avoid calling malloc */
1863 /* in case REDIRECT_MALLOC is defined. */
1864 # define STAT_BUF_SIZE 4096
1866 # define STAT_READ __real_read
1868 # define STAT_READ read
1870 char stat_buf[STAT_BUF_SIZE];
1874 size_t i, buf_offset = 0;
1876 f = open("/proc/self/stat", O_RDONLY);
1877 if (f < 0 || STAT_READ(f, stat_buf, STAT_BUF_SIZE) < 2 * STAT_SKIP) {
1878 ABORT("Couldn't read /proc/self/stat");
1880 c = stat_buf[buf_offset++];
1881 /* Skip the required number of fields. This number is hopefully */
1882 /* constant across all Linux implementations. */
1883 for (i = 0; i < STAT_SKIP; ++i) {
1884 while (isspace(c)) c = stat_buf[buf_offset++];
1885 while (!isspace(c)) c = stat_buf[buf_offset++];
1887 while (isspace(c)) c = stat_buf[buf_offset++];
1888 while (isdigit(c)) {
1891 c = stat_buf[buf_offset++];
1894 if (result < 0x10000000) ABORT("Absurd stack bottom value");
1895 return (ptr_t)result;
1898 #endif /* LINUX_STACKBOTTOM */
1900 void *scm_get_stack_base()
1905 result = &dummy; /* initialize to silence compiler */
1907 # define STACKBOTTOM_ALIGNMENT_M1 ((word)STACK_GRAN - 1)
1910 return(STACKBOTTOM);
1913 # if STACK_GROWS_DOWN
1914 result = (ptr_t)((((word)(&dummy))
1915 + STACKBOTTOM_ALIGNMENT_M1)
1916 & ~STACKBOTTOM_ALIGNMENT_M1);
1918 result = (ptr_t)(((word)(&dummy))
1919 & ~STACKBOTTOM_ALIGNMENT_M1);
1921 # endif /* HEURISTIC1 */
1922 # ifdef LINUX_STACKBOTTOM
1923 result = GC_linux_stack_base();
1926 # if STACK_GROWS_DOWN
1927 result = GC_find_limit((ptr_t)(&dummy), TRUE);
1928 # ifdef HEURISTIC2_LIMIT
1929 if ((ptr_t)result > HEURISTIC2_LIMIT
1930 && (ptr_t)(&dummy) < HEURISTIC2_LIMIT) {
1931 result = HEURISTIC2_LIMIT;
1935 result = GC_find_limit((ptr_t)(&dummy), FALSE);
1936 # ifdef HEURISTIC2_LIMIT
1937 if (result < HEURISTIC2_LIMIT
1938 && (ptr_t)(&dummy) > HEURISTIC2_LIMIT) {
1939 result = HEURISTIC2_LIMIT;
1944 # endif /* HEURISTIC2 */
1945 # if STACK_GROWS_DOWN
1946 if (result == 0) result = (ptr_t)(signed_word)(-sizeof(ptr_t));
1949 # endif /* STACKBOTTOM */
1952 # endif /* ! AMIGA */
1954 # endif /* ! MSWIN32 */
1956 #endif /* mach_type_known */
1957 #endif /* ! HAVE_LIBC_STACK_END */