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.
7 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
8 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
10 * Permission is hereby granted to use or copy this program
11 * for any purpose, provided the above notices are retained on all copies.
12 * Permission to modify the code and to distribute modified code is granted,
13 * provided the above notices are retained, and a notice that the code was
14 * modified is included with the above copyright notice.
19 # include "private/gc_priv.h"
21 # if defined(LINUX) && !defined(POWERPC)
22 # include <linux/version.h>
23 # if (LINUX_VERSION_CODE <= 0x10400)
24 /* Ugly hack to get struct sigcontext_struct definition. Required */
25 /* for some early 1.3.X releases. Will hopefully go away soon. */
26 /* in some later Linux releases, asm/sigcontext.h may have to */
27 /* be included instead. */
29 # include <asm/signal.h>
32 /* Kernels prior to 2.1.1 defined struct sigcontext_struct instead of */
33 /* struct sigcontext. libc6 (glibc2) uses "struct sigcontext" in */
34 /* prototypes, so we have to include the top-level sigcontext.h to */
35 /* make sure the former gets defined to be the latter if appropriate. */
36 # include <features.h>
38 # if 2 == __GLIBC__ && 0 == __GLIBC_MINOR__
39 /* glibc 2.1 no longer has sigcontext.h. But signal.h */
40 /* has the right declaration for glibc 2.1. */
41 # include <sigcontext.h>
42 # endif /* 0 == __GLIBC_MINOR__ */
43 # else /* not 2 <= __GLIBC__ */
44 /* libc5 doesn't have <sigcontext.h>: go directly with the kernel */
45 /* one. Check LINUX_VERSION_CODE to see which we should reference. */
46 # include <asm/sigcontext.h>
47 # endif /* 2 <= __GLIBC__ */
50 # if !defined(OS2) && !defined(PCR) && !defined(AMIGA) && !defined(MACOS) \
52 # include <sys/types.h>
53 # if !defined(MSWIN32) && !defined(SUNOS4)
60 # define SIGSEGV 0 /* value is irrelevant */
65 #if defined(LINUX) || defined(LINUX_STACKBOTTOM)
69 /* Blatantly OS dependent routines, except for those that are related */
70 /* to dynamic loading. */
72 # if defined(HEURISTIC2) || defined(SEARCH_FOR_DATA_START)
73 # define NEED_FIND_LIMIT
76 # if !defined(STACKBOTTOM) && defined(HEURISTIC2)
77 # define NEED_FIND_LIMIT
80 # if (defined(SUNOS4) && defined(DYNAMIC_LOADING)) && !defined(PCR)
81 # define NEED_FIND_LIMIT
84 # if (defined(SVR4) || defined(AUX) || defined(DGUX) \
85 || (defined(LINUX) && defined(SPARC))) && !defined(PCR)
86 # define NEED_FIND_LIMIT
89 #if defined(FREEBSD) && (defined(I386) || defined(powerpc) || defined(__powerpc__))
90 # include <machine/trap.h>
92 # define NEED_FIND_LIMIT
96 #if (defined(NETBSD) || defined(OPENBSD)) && defined(__ELF__) \
97 && !defined(NEED_FIND_LIMIT)
98 /* Used by GC_init_netbsd_elf() below. */
99 # define NEED_FIND_LIMIT
102 #ifdef NEED_FIND_LIMIT
107 # define GC_AMIGA_DEF
108 # include "AmigaOS.c"
112 #if defined(MSWIN32) || defined(MSWINCE)
113 # define WIN32_LEAN_AND_MEAN
115 # include <windows.h>
119 # include <Processes.h>
123 # include <sys/uio.h>
124 # include <malloc.h> /* for locking */
126 #if defined(USE_MMAP) || defined(USE_MUNMAP)
128 --> USE_MUNMAP requires USE_MMAP
130 # include <sys/types.h>
131 # include <sys/mman.h>
132 # include <sys/stat.h>
138 # if defined(SUNOS5SIGS) && !defined(FREEBSD)
139 # include <sys/siginfo.h>
141 /* Define SETJMP and friends to be the version that restores */
142 /* the signal mask. */
143 # define SETJMP(env) sigsetjmp(env, 1)
144 # define LONGJMP(env, val) siglongjmp(env, val)
145 # define JMP_BUF sigjmp_buf
147 # define SETJMP(env) setjmp(env)
148 # define LONGJMP(env, val) longjmp(env, val)
149 # define JMP_BUF jmp_buf
153 /* for get_etext and friends */
154 #include <mach-o/getsect.h>
158 /* Apparently necessary for djgpp 2.01. May cause problems with */
159 /* other versions. */
160 typedef long unsigned int caddr_t;
164 # include "il/PCR_IL.h"
165 # include "th/PCR_ThCtl.h"
166 # include "mm/PCR_MM.h"
169 #if !defined(NO_EXECUTE_PERMISSION)
170 # define OPT_PROT_EXEC PROT_EXEC
172 # define OPT_PROT_EXEC 0
175 #if defined(LINUX) && \
176 (defined(USE_PROC_FOR_LIBRARIES) || defined(IA64) || !defined(SMALL_CONFIG))
178 /* We need to parse /proc/self/maps, either to find dynamic libraries, */
179 /* and/or to find the register backing store base (IA64). Do it once */
184 /* Repeatedly perform a read call until the buffer is filled or */
185 /* we encounter EOF. */
186 ssize_t GC_repeat_read(int fd, char *buf, size_t count)
188 ssize_t num_read = 0;
191 while (num_read < count) {
192 result = READ(fd, buf + num_read, count - num_read);
193 if (result < 0) return result;
194 if (result == 0) break;
201 * Apply fn to a buffer containing the contents of /proc/self/maps.
202 * Return the result of fn or, if we failed, 0.
203 * We currently do nothing to /proc/self/maps other than simply read
204 * it. This code could be simplified if we could determine its size
208 word GC_apply_to_maps(word (*fn)(char *))
212 size_t maps_size = 4000; /* Initial guess. */
213 static char init_buf[1];
214 static char *maps_buf = init_buf;
215 static size_t maps_buf_sz = 1;
217 /* Read /proc/self/maps, growing maps_buf as necessary. */
218 /* Note that we may not allocate conventionally, and */
219 /* thus can't use stdio. */
221 if (maps_size >= maps_buf_sz) {
222 /* Grow only by powers of 2, since we leak "too small" buffers. */
223 while (maps_size >= maps_buf_sz) maps_buf_sz *= 2;
224 maps_buf = GC_scratch_alloc(maps_buf_sz);
225 if (maps_buf == 0) return 0;
227 f = open("/proc/self/maps", O_RDONLY);
228 if (-1 == f) return 0;
231 result = GC_repeat_read(f, maps_buf, maps_buf_sz-1);
232 if (result <= 0) return 0;
234 } while (result == maps_buf_sz-1);
236 } while (maps_size >= maps_buf_sz);
237 maps_buf[maps_size] = '\0';
239 /* Apply fn to result. */
243 #endif /* Need GC_apply_to_maps */
245 #if defined(LINUX) && (defined(USE_PROC_FOR_LIBRARIES) || defined(IA64))
247 // GC_parse_map_entry parses an entry from /proc/self/maps so we can
248 // locate all writable data segments that belong to shared libraries.
249 // The format of one of these entries and the fields we care about
251 // XXXXXXXX-XXXXXXXX r-xp 00000000 30:05 260537 name of mapping...\n
252 // ^^^^^^^^ ^^^^^^^^ ^^^^ ^^
253 // start end prot maj_dev
255 // Note that since about auguat 2003 kernels, the columns no longer have
256 // fixed offsets on 64-bit kernels. Hence we no longer rely on fixed offsets
257 // anywhere, which is safer anyway.
261 * Assign various fields of the first line in buf_ptr to *start, *end,
262 * *prot_buf and *maj_dev. Only *prot_buf may be set for unwritable maps.
264 char *GC_parse_map_entry(char *buf_ptr, word *start, word *end,
265 char *prot_buf, unsigned int *maj_dev)
267 char *start_start, *end_start, *prot_start, *maj_dev_start;
271 if (buf_ptr == NULL || *buf_ptr == '\0') {
276 while (isspace(*p)) ++p;
278 GC_ASSERT(isxdigit(*start_start));
279 *start = strtoul(start_start, &endp, 16); p = endp;
284 GC_ASSERT(isxdigit(*end_start));
285 *end = strtoul(end_start, &endp, 16); p = endp;
286 GC_ASSERT(isspace(*p));
288 while (isspace(*p)) ++p;
290 GC_ASSERT(*prot_start == 'r' || *prot_start == '-');
291 memcpy(prot_buf, prot_start, 4);
293 if (prot_buf[1] == 'w') {/* we can skip the rest if it's not writable. */
294 /* Skip past protection field to offset field */
295 while (!isspace(*p)) ++p; while (isspace(*p)) ++p;
296 GC_ASSERT(isxdigit(*p));
297 /* Skip past offset field, which we ignore */
298 while (!isspace(*p)) ++p; while (isspace(*p)) ++p;
300 GC_ASSERT(isxdigit(*maj_dev_start));
301 *maj_dev = strtoul(maj_dev_start, NULL, 16);
304 while (*p && *p++ != '\n');
309 #endif /* Need to parse /proc/self/maps. */
311 #if defined(SEARCH_FOR_DATA_START)
312 /* The I386 case can be handled without a search. The Alpha case */
313 /* used to be handled differently as well, but the rules changed */
314 /* for recent Linux versions. This seems to be the easiest way to */
315 /* cover all versions. */
318 /* Some Linux distributions arrange to define __data_start. Some */
319 /* define data_start as a weak symbol. The latter is technically */
320 /* broken, since the user program may define data_start, in which */
321 /* case we lose. Nonetheless, we try both, prefering __data_start. */
322 /* We assume gcc-compatible pragmas. */
323 # pragma weak __data_start
324 extern int __data_start[];
325 # pragma weak data_start
326 extern int data_start[];
332 void GC_init_linux_data_start()
334 extern ptr_t GC_find_limit();
337 /* Try the easy approaches first: */
338 if ((ptr_t)__data_start != 0) {
339 GC_data_start = (ptr_t)(__data_start);
342 if ((ptr_t)data_start != 0) {
343 GC_data_start = (ptr_t)(data_start);
347 GC_data_start = GC_find_limit((ptr_t)(_end), FALSE);
353 # ifndef ECOS_GC_MEMORY_SIZE
354 # define ECOS_GC_MEMORY_SIZE (448 * 1024)
355 # endif /* ECOS_GC_MEMORY_SIZE */
357 // setjmp() function, as described in ANSI para 7.6.1.1
359 #define SETJMP( __env__ ) hal_setjmp( __env__ )
361 // FIXME: This is a simple way of allocating memory which is
362 // compatible with ECOS early releases. Later releases use a more
363 // sophisticated means of allocating memory than this simple static
364 // allocator, but this method is at least bound to work.
365 static char memory[ECOS_GC_MEMORY_SIZE];
366 static char *brk = memory;
368 static void *tiny_sbrk(ptrdiff_t increment)
374 if (brk > memory + sizeof memory)
382 #define sbrk tiny_sbrk
385 #if (defined(NETBSD) || defined(OPENBSD)) && defined(__ELF__)
388 void GC_init_netbsd_elf()
390 extern ptr_t GC_find_limit();
391 extern char **environ;
392 /* This may need to be environ, without the underscore, for */
394 GC_data_start = GC_find_limit((ptr_t)&environ, FALSE);
402 # if !defined(__IBMC__) && !defined(__WATCOMC__) /* e.g. EMX */
405 unsigned short magic_number;
406 unsigned short padding[29];
410 #define E_MAGIC(x) (x).magic_number
411 #define EMAGIC 0x5A4D
412 #define E_LFANEW(x) (x).new_exe_offset
415 unsigned char magic_number[2];
416 unsigned char byte_order;
417 unsigned char word_order;
418 unsigned long exe_format_level;
421 unsigned long padding1[13];
422 unsigned long object_table_offset;
423 unsigned long object_count;
424 unsigned long padding2[31];
427 #define E32_MAGIC1(x) (x).magic_number[0]
428 #define E32MAGIC1 'L'
429 #define E32_MAGIC2(x) (x).magic_number[1]
430 #define E32MAGIC2 'X'
431 #define E32_BORDER(x) (x).byte_order
433 #define E32_WORDER(x) (x).word_order
435 #define E32_CPU(x) (x).cpu
437 #define E32_OBJTAB(x) (x).object_table_offset
438 #define E32_OBJCNT(x) (x).object_count
444 unsigned long pagemap;
445 unsigned long mapsize;
446 unsigned long reserved;
449 #define O32_FLAGS(x) (x).flags
450 #define OBJREAD 0x0001L
451 #define OBJWRITE 0x0002L
452 #define OBJINVALID 0x0080L
453 #define O32_SIZE(x) (x).size
454 #define O32_BASE(x) (x).base
456 # else /* IBM's compiler */
458 /* A kludge to get around what appears to be a header file bug */
460 # define WORD unsigned short
463 # define DWORD unsigned long
470 # endif /* __IBMC__ */
472 # define INCL_DOSEXCEPTIONS
473 # define INCL_DOSPROCESS
474 # define INCL_DOSERRORS
475 # define INCL_DOSMODULEMGR
476 # define INCL_DOSMEMMGR
480 /* Disable and enable signals during nontrivial allocations */
482 void GC_disable_signals(void)
486 DosEnterMustComplete(&nest);
487 if (nest != 1) ABORT("nested GC_disable_signals");
490 void GC_enable_signals(void)
494 DosExitMustComplete(&nest);
495 if (nest != 0) ABORT("GC_enable_signals");
501 # if !defined(PCR) && !defined(AMIGA) && !defined(MSWIN32) \
502 && !defined(MSWINCE) \
503 && !defined(MACOS) && !defined(DJGPP) && !defined(DOS4GW) \
504 && !defined(NOSYS) && !defined(ECOS)
506 # if defined(sigmask) && !defined(UTS4) && !defined(HURD)
507 /* Use the traditional BSD interface */
508 # define SIGSET_T int
509 # define SIG_DEL(set, signal) (set) &= ~(sigmask(signal))
510 # define SIG_FILL(set) (set) = 0x7fffffff
511 /* Setting the leading bit appears to provoke a bug in some */
512 /* longjmp implementations. Most systems appear not to have */
514 # define SIGSETMASK(old, new) (old) = sigsetmask(new)
516 /* Use POSIX/SYSV interface */
517 # define SIGSET_T sigset_t
518 # define SIG_DEL(set, signal) sigdelset(&(set), (signal))
519 # define SIG_FILL(set) sigfillset(&set)
520 # define SIGSETMASK(old, new) sigprocmask(SIG_SETMASK, &(new), &(old))
523 static GC_bool mask_initialized = FALSE;
525 static SIGSET_T new_mask;
527 static SIGSET_T old_mask;
529 static SIGSET_T dummy;
531 #if defined(PRINTSTATS) && !defined(THREADS)
532 # define CHECK_SIGNALS
533 int GC_sig_disabled = 0;
536 void GC_disable_signals()
538 if (!mask_initialized) {
541 SIG_DEL(new_mask, SIGSEGV);
542 SIG_DEL(new_mask, SIGILL);
543 SIG_DEL(new_mask, SIGQUIT);
545 SIG_DEL(new_mask, SIGBUS);
548 SIG_DEL(new_mask, SIGIOT);
551 SIG_DEL(new_mask, SIGEMT);
554 SIG_DEL(new_mask, SIGTRAP);
556 mask_initialized = TRUE;
558 # ifdef CHECK_SIGNALS
559 if (GC_sig_disabled != 0) ABORT("Nested disables");
562 SIGSETMASK(old_mask,new_mask);
565 void GC_enable_signals()
567 # ifdef CHECK_SIGNALS
568 if (GC_sig_disabled != 1) ABORT("Unmatched enable");
571 SIGSETMASK(dummy,old_mask);
578 /* Ivan Demakov: simplest way (to me) */
580 void GC_disable_signals() { }
581 void GC_enable_signals() { }
584 /* Find the page size */
587 # if defined(MSWIN32) || defined(MSWINCE)
588 void GC_setpagesize()
590 GetSystemInfo(&GC_sysinfo);
591 GC_page_size = GC_sysinfo.dwPageSize;
595 # if defined(MPROTECT_VDB) || defined(PROC_VDB) || defined(USE_MMAP) \
596 || defined(USE_MUNMAP)
597 void GC_setpagesize()
599 GC_page_size = GETPAGESIZE();
602 /* It's acceptable to fake it. */
603 void GC_setpagesize()
605 GC_page_size = HBLKSIZE;
611 * Find the base of the stack.
612 * Used only in single-threaded environment.
613 * With threads, GC_mark_roots needs to know how to do this.
614 * Called with allocator lock held.
616 # if defined(MSWIN32) || defined(MSWINCE)
617 # define is_writable(prot) ((prot) == PAGE_READWRITE \
618 || (prot) == PAGE_WRITECOPY \
619 || (prot) == PAGE_EXECUTE_READWRITE \
620 || (prot) == PAGE_EXECUTE_WRITECOPY)
621 /* Return the number of bytes that are writable starting at p. */
622 /* The pointer p is assumed to be page aligned. */
623 /* If base is not 0, *base becomes the beginning of the */
624 /* allocation region containing p. */
625 word GC_get_writable_length(ptr_t p, ptr_t *base)
627 MEMORY_BASIC_INFORMATION buf;
631 result = VirtualQuery(p, &buf, sizeof(buf));
632 if (result != sizeof(buf)) ABORT("Weird VirtualQuery result");
633 if (base != 0) *base = (ptr_t)(buf.AllocationBase);
634 protect = (buf.Protect & ~(PAGE_GUARD | PAGE_NOCACHE));
635 if (!is_writable(protect)) {
638 if (buf.State != MEM_COMMIT) return(0);
639 return(buf.RegionSize);
642 ptr_t GC_get_stack_base()
645 ptr_t sp = (ptr_t)(&dummy);
646 ptr_t trunc_sp = (ptr_t)((word)sp & ~(GC_page_size - 1));
647 word size = GC_get_writable_length(trunc_sp, 0);
649 return(trunc_sp + size);
653 # endif /* MS Windows */
656 # include <kernel/OS.h>
657 ptr_t GC_get_stack_base(){
659 get_thread_info(find_thread(NULL),&th);
667 ptr_t GC_get_stack_base()
672 if (DosGetInfoBlocks(&ptib, &ppib) != NO_ERROR) {
673 GC_err_printf0("DosGetInfoBlocks failed\n");
674 ABORT("DosGetInfoBlocks failed\n");
676 return((ptr_t)(ptib -> tib_pstacklimit));
683 # include "AmigaOS.c"
687 # if defined(NEED_FIND_LIMIT) || defined(UNIX_LIKE)
690 typedef void (*handler)(int);
692 typedef void (*handler)();
695 # if defined(SUNOS5SIGS) || defined(IRIX5) || defined(OSF1) \
696 || defined(HURD) || defined(NETBSD)
697 static struct sigaction old_segv_act;
698 # if defined(IRIX5) || defined(HPUX) \
699 || defined(HURD) || defined(NETBSD)
700 static struct sigaction old_bus_act;
703 static handler old_segv_handler, old_bus_handler;
707 void GC_set_and_save_fault_handler(handler h)
709 void GC_set_and_save_fault_handler(h)
713 # if defined(SUNOS5SIGS) || defined(IRIX5) \
714 || defined(OSF1) || defined(HURD) || defined(NETBSD)
715 struct sigaction act;
718 # if 0 /* Was necessary for Solaris 2.3 and very temporary */
720 act.sa_flags = SA_RESTART | SA_NODEFER;
722 act.sa_flags = SA_RESTART;
725 (void) sigemptyset(&act.sa_mask);
726 # ifdef GC_IRIX_THREADS
727 /* Older versions have a bug related to retrieving and */
728 /* and setting a handler at the same time. */
729 (void) sigaction(SIGSEGV, 0, &old_segv_act);
730 (void) sigaction(SIGSEGV, &act, 0);
731 (void) sigaction(SIGBUS, 0, &old_bus_act);
732 (void) sigaction(SIGBUS, &act, 0);
734 (void) sigaction(SIGSEGV, &act, &old_segv_act);
735 # if defined(IRIX5) \
736 || defined(HPUX) || defined(HURD) || defined(NETBSD)
737 /* Under Irix 5.x or HP/UX, we may get SIGBUS. */
738 /* Pthreads doesn't exist under Irix 5.x, so we */
739 /* don't have to worry in the threads case. */
740 (void) sigaction(SIGBUS, &act, &old_bus_act);
742 # endif /* GC_IRIX_THREADS */
744 old_segv_handler = signal(SIGSEGV, h);
746 old_bus_handler = signal(SIGBUS, h);
750 # endif /* NEED_FIND_LIMIT || UNIX_LIKE */
752 # ifdef NEED_FIND_LIMIT
753 /* Some tools to implement HEURISTIC2 */
754 # define MIN_PAGE_SIZE 256 /* Smallest conceivable page size, bytes */
755 /* static */ JMP_BUF GC_jmp_buf;
758 void GC_fault_handler(sig)
761 LONGJMP(GC_jmp_buf, 1);
764 void GC_setup_temporary_fault_handler()
766 GC_set_and_save_fault_handler(GC_fault_handler);
769 void GC_reset_fault_handler()
771 # if defined(SUNOS5SIGS) || defined(IRIX5) \
772 || defined(OSF1) || defined(HURD) || defined(NETBSD)
773 (void) sigaction(SIGSEGV, &old_segv_act, 0);
774 # if defined(IRIX5) \
775 || defined(HPUX) || defined(HURD) || defined(NETBSD)
776 (void) sigaction(SIGBUS, &old_bus_act, 0);
779 (void) signal(SIGSEGV, old_segv_handler);
781 (void) signal(SIGBUS, old_bus_handler);
786 /* Return the first nonaddressible location > p (up) or */
787 /* the smallest location q s.t. [q,p) is addressable (!up). */
788 /* We assume that p (up) or p-1 (!up) is addressable. */
789 ptr_t GC_find_limit(p, up)
793 static VOLATILE ptr_t result;
794 /* Needs to be static, since otherwise it may not be */
795 /* preserved across the longjmp. Can safely be */
796 /* static since it's only called once, with the */
797 /* allocation lock held. */
800 GC_setup_temporary_fault_handler();
801 if (SETJMP(GC_jmp_buf) == 0) {
802 result = (ptr_t)(((word)(p))
803 & ~(MIN_PAGE_SIZE-1));
806 result += MIN_PAGE_SIZE;
808 result -= MIN_PAGE_SIZE;
810 GC_noop1((word)(*result));
813 GC_reset_fault_handler();
815 result += MIN_PAGE_SIZE;
821 #if defined(ECOS) || defined(NOSYS)
822 ptr_t GC_get_stack_base()
828 #ifdef HPUX_STACKBOTTOM
830 #include <sys/param.h>
831 #include <sys/pstat.h>
833 ptr_t GC_get_register_stack_base(void)
835 struct pst_vm_status vm_status;
838 while (pstat_getprocvm(&vm_status, sizeof(vm_status), 0, i++) == 1) {
839 if (vm_status.pst_type == PS_RSESTACK) {
840 return (ptr_t) vm_status.pst_vaddr;
844 /* old way to get the register stackbottom */
845 return (ptr_t)(((word)GC_stackbottom - BACKING_STORE_DISPLACEMENT - 1)
846 & ~(BACKING_STORE_ALIGNMENT - 1));
849 #endif /* HPUX_STACK_BOTTOM */
851 #ifdef LINUX_STACKBOTTOM
853 #include <sys/types.h>
854 #include <sys/stat.h>
856 # define STAT_SKIP 27 /* Number of fields preceding startstack */
857 /* field in /proc/self/stat */
859 #ifdef USE_LIBC_PRIVATES
860 # pragma weak __libc_stack_end
861 extern ptr_t __libc_stack_end;
865 /* Try to read the backing store base from /proc/self/maps. */
866 /* We look for the writable mapping with a 0 major device, */
867 /* which is as close to our frame as possible, but below it.*/
868 static word backing_store_base_from_maps(char *maps)
871 char *buf_ptr = maps;
873 unsigned int maj_dev;
874 word current_best = 0;
878 buf_ptr = GC_parse_map_entry(buf_ptr, &start, &end, prot_buf, &maj_dev);
879 if (buf_ptr == NULL) return current_best;
880 if (prot_buf[1] == 'w' && maj_dev == 0) {
881 if (end < (word)(&dummy) && start > current_best) current_best = start;
887 static word backing_store_base_from_proc(void)
889 return GC_apply_to_maps(backing_store_base_from_maps);
892 # ifdef USE_LIBC_PRIVATES
893 # pragma weak __libc_ia64_register_backing_store_base
894 extern ptr_t __libc_ia64_register_backing_store_base;
897 ptr_t GC_get_register_stack_base(void)
899 # ifdef USE_LIBC_PRIVATES
900 if (0 != &__libc_ia64_register_backing_store_base
901 && 0 != __libc_ia64_register_backing_store_base) {
902 /* Glibc 2.2.4 has a bug such that for dynamically linked */
903 /* executables __libc_ia64_register_backing_store_base is */
904 /* defined but uninitialized during constructor calls. */
905 /* Hence we check for both nonzero address and value. */
906 return __libc_ia64_register_backing_store_base;
909 word result = backing_store_base_from_proc();
911 /* Use dumb heuristics. Works only for default configuration. */
912 result = (word)GC_stackbottom - BACKING_STORE_DISPLACEMENT;
913 result += BACKING_STORE_ALIGNMENT - 1;
914 result &= ~(BACKING_STORE_ALIGNMENT - 1);
915 /* Verify that it's at least readable. If not, we goofed. */
916 GC_noop1(*(word *)result);
918 return (ptr_t)result;
922 ptr_t GC_linux_stack_base(void)
924 /* We read the stack base value from /proc/self/stat. We do this */
925 /* using direct I/O system calls in order to avoid calling malloc */
926 /* in case REDIRECT_MALLOC is defined. */
927 # define STAT_BUF_SIZE 4096
928 # define STAT_READ read
929 /* Should probably call the real read, if read is wrapped. */
930 char stat_buf[STAT_BUF_SIZE];
934 size_t i, buf_offset = 0;
936 /* First try the easy way. This should work for glibc 2.2 */
937 /* This fails in a prelinked ("prelink" command) executable */
938 /* since the correct value of __libc_stack_end never */
939 /* becomes visible to us. The second test works around */
941 # ifdef USE_LIBC_PRIVATES
942 if (0 != &__libc_stack_end && 0 != __libc_stack_end ) {
944 /* Some versions of glibc set the address 16 bytes too */
945 /* low while the initialization code is running. */
946 if (((word)__libc_stack_end & 0xfff) + 0x10 < 0x1000) {
947 return __libc_stack_end + 0x10;
948 } /* Otherwise it's not safe to add 16 bytes and we fall */
949 /* back to using /proc. */
952 /* Older versions of glibc for 64-bit Sparc do not set
953 * this variable correctly, it gets set to either zero
956 if (__libc_stack_end != (ptr_t) (unsigned long)0x1)
957 return __libc_stack_end;
959 return __libc_stack_end;
964 f = open("/proc/self/stat", O_RDONLY);
965 if (f < 0 || STAT_READ(f, stat_buf, STAT_BUF_SIZE) < 2 * STAT_SKIP) {
966 ABORT("Couldn't read /proc/self/stat");
968 c = stat_buf[buf_offset++];
969 /* Skip the required number of fields. This number is hopefully */
970 /* constant across all Linux implementations. */
971 for (i = 0; i < STAT_SKIP; ++i) {
972 while (isspace(c)) c = stat_buf[buf_offset++];
973 while (!isspace(c)) c = stat_buf[buf_offset++];
975 while (isspace(c)) c = stat_buf[buf_offset++];
979 c = stat_buf[buf_offset++];
982 if (result < 0x10000000) ABORT("Absurd stack bottom value");
983 return (ptr_t)result;
986 #endif /* LINUX_STACKBOTTOM */
988 #ifdef FREEBSD_STACKBOTTOM
990 /* This uses an undocumented sysctl call, but at least one expert */
991 /* believes it will stay. */
994 #include <sys/types.h>
995 #include <sys/sysctl.h>
997 ptr_t GC_freebsd_stack_base(void)
999 int nm[2] = {CTL_KERN, KERN_USRSTACK};
1001 size_t len = sizeof(ptr_t);
1002 int r = sysctl(nm, 2, &base, &len, NULL, 0);
1004 if (r) ABORT("Error getting stack base");
1009 #endif /* FREEBSD_STACKBOTTOM */
1011 #if !defined(BEOS) && !defined(AMIGA) && !defined(MSWIN32) \
1012 && !defined(MSWINCE) && !defined(OS2) && !defined(NOSYS) && !defined(ECOS)
1014 ptr_t GC_get_stack_base()
1016 # if defined(HEURISTIC1) || defined(HEURISTIC2) || \
1017 defined(LINUX_STACKBOTTOM) || defined(FREEBSD_STACKBOTTOM)
1022 # define STACKBOTTOM_ALIGNMENT_M1 ((word)STACK_GRAN - 1)
1025 return(STACKBOTTOM);
1028 # ifdef STACK_GROWS_DOWN
1029 result = (ptr_t)((((word)(&dummy))
1030 + STACKBOTTOM_ALIGNMENT_M1)
1031 & ~STACKBOTTOM_ALIGNMENT_M1);
1033 result = (ptr_t)(((word)(&dummy))
1034 & ~STACKBOTTOM_ALIGNMENT_M1);
1036 # endif /* HEURISTIC1 */
1037 # ifdef LINUX_STACKBOTTOM
1038 result = GC_linux_stack_base();
1040 # ifdef FREEBSD_STACKBOTTOM
1041 result = GC_freebsd_stack_base();
1044 # ifdef STACK_GROWS_DOWN
1045 result = GC_find_limit((ptr_t)(&dummy), TRUE);
1046 # ifdef HEURISTIC2_LIMIT
1047 if (result > HEURISTIC2_LIMIT
1048 && (ptr_t)(&dummy) < HEURISTIC2_LIMIT) {
1049 result = HEURISTIC2_LIMIT;
1053 result = GC_find_limit((ptr_t)(&dummy), FALSE);
1054 # ifdef HEURISTIC2_LIMIT
1055 if (result < HEURISTIC2_LIMIT
1056 && (ptr_t)(&dummy) > HEURISTIC2_LIMIT) {
1057 result = HEURISTIC2_LIMIT;
1062 # endif /* HEURISTIC2 */
1063 # ifdef STACK_GROWS_DOWN
1064 if (result == 0) result = (ptr_t)(signed_word)(-sizeof(ptr_t));
1067 # endif /* STACKBOTTOM */
1070 # endif /* ! AMIGA, !OS 2, ! MS Windows, !BEOS, !NOSYS, !ECOS */
1073 * Register static data segment(s) as roots.
1074 * If more data segments are added later then they need to be registered
1075 * add that point (as we do with SunOS dynamic loading),
1076 * or GC_mark_roots needs to check for them (as we do with PCR).
1077 * Called with allocator lock held.
1082 void GC_register_data_segments()
1086 HMODULE module_handle;
1087 # define PBUFSIZ 512
1088 UCHAR path[PBUFSIZ];
1090 struct exe_hdr hdrdos; /* MSDOS header. */
1091 struct e32_exe hdr386; /* Real header for my executable */
1092 struct o32_obj seg; /* Currrent segment */
1096 if (DosGetInfoBlocks(&ptib, &ppib) != NO_ERROR) {
1097 GC_err_printf0("DosGetInfoBlocks failed\n");
1098 ABORT("DosGetInfoBlocks failed\n");
1100 module_handle = ppib -> pib_hmte;
1101 if (DosQueryModuleName(module_handle, PBUFSIZ, path) != NO_ERROR) {
1102 GC_err_printf0("DosQueryModuleName failed\n");
1103 ABORT("DosGetInfoBlocks failed\n");
1105 myexefile = fopen(path, "rb");
1106 if (myexefile == 0) {
1107 GC_err_puts("Couldn't open executable ");
1108 GC_err_puts(path); GC_err_puts("\n");
1109 ABORT("Failed to open executable\n");
1111 if (fread((char *)(&hdrdos), 1, sizeof hdrdos, myexefile) < sizeof hdrdos) {
1112 GC_err_puts("Couldn't read MSDOS header from ");
1113 GC_err_puts(path); GC_err_puts("\n");
1114 ABORT("Couldn't read MSDOS header");
1116 if (E_MAGIC(hdrdos) != EMAGIC) {
1117 GC_err_puts("Executable has wrong DOS magic number: ");
1118 GC_err_puts(path); GC_err_puts("\n");
1119 ABORT("Bad DOS magic number");
1121 if (fseek(myexefile, E_LFANEW(hdrdos), SEEK_SET) != 0) {
1122 GC_err_puts("Seek to new header failed in ");
1123 GC_err_puts(path); GC_err_puts("\n");
1124 ABORT("Bad DOS magic number");
1126 if (fread((char *)(&hdr386), 1, sizeof hdr386, myexefile) < sizeof hdr386) {
1127 GC_err_puts("Couldn't read MSDOS header from ");
1128 GC_err_puts(path); GC_err_puts("\n");
1129 ABORT("Couldn't read OS/2 header");
1131 if (E32_MAGIC1(hdr386) != E32MAGIC1 || E32_MAGIC2(hdr386) != E32MAGIC2) {
1132 GC_err_puts("Executable has wrong OS/2 magic number:");
1133 GC_err_puts(path); GC_err_puts("\n");
1134 ABORT("Bad OS/2 magic number");
1136 if ( E32_BORDER(hdr386) != E32LEBO || E32_WORDER(hdr386) != E32LEWO) {
1137 GC_err_puts("Executable %s has wrong byte order: ");
1138 GC_err_puts(path); GC_err_puts("\n");
1139 ABORT("Bad byte order");
1141 if ( E32_CPU(hdr386) == E32CPU286) {
1142 GC_err_puts("GC can't handle 80286 executables: ");
1143 GC_err_puts(path); GC_err_puts("\n");
1146 if (fseek(myexefile, E_LFANEW(hdrdos) + E32_OBJTAB(hdr386),
1148 GC_err_puts("Seek to object table failed: ");
1149 GC_err_puts(path); GC_err_puts("\n");
1150 ABORT("Seek to object table failed");
1152 for (nsegs = E32_OBJCNT(hdr386); nsegs > 0; nsegs--) {
1154 if (fread((char *)(&seg), 1, sizeof seg, myexefile) < sizeof seg) {
1155 GC_err_puts("Couldn't read obj table entry from ");
1156 GC_err_puts(path); GC_err_puts("\n");
1157 ABORT("Couldn't read obj table entry");
1159 flags = O32_FLAGS(seg);
1160 if (!(flags & OBJWRITE)) continue;
1161 if (!(flags & OBJREAD)) continue;
1162 if (flags & OBJINVALID) {
1163 GC_err_printf0("Object with invalid pages?\n");
1166 GC_add_roots_inner(O32_BASE(seg), O32_BASE(seg)+O32_SIZE(seg), FALSE);
1172 # if defined(MSWIN32) || defined(MSWINCE)
1175 /* Unfortunately, we have to handle win32s very differently from NT, */
1176 /* Since VirtualQuery has very different semantics. In particular, */
1177 /* under win32s a VirtualQuery call on an unmapped page returns an */
1178 /* invalid result. Under NT, GC_register_data_segments is a noop and */
1179 /* all real work is done by GC_register_dynamic_libraries. Under */
1180 /* win32s, we cannot find the data segments associated with dll's. */
1181 /* We register the main data segment here. */
1182 GC_bool GC_no_win32_dlls = FALSE;
1183 /* This used to be set for gcc, to avoid dealing with */
1184 /* the structured exception handling issues. But we now have */
1185 /* assembly code to do that right. */
1186 GC_bool GC_wnt = FALSE;
1187 /* This is a Windows NT derivative, i.e. NT, W2K, XP or later. */
1189 void GC_init_win32()
1191 /* if we're running under win32s, assume that no DLLs will be loaded */
1192 DWORD v = GetVersion();
1193 GC_wnt = !(v & 0x80000000);
1194 GC_no_win32_dlls |= ((!GC_wnt) && (v & 0xff) <= 3);
1197 /* Return the smallest address a such that VirtualQuery */
1198 /* returns correct results for all addresses between a and start. */
1199 /* Assumes VirtualQuery returns correct information for start. */
1200 ptr_t GC_least_described_address(ptr_t start)
1202 MEMORY_BASIC_INFORMATION buf;
1208 limit = GC_sysinfo.lpMinimumApplicationAddress;
1209 p = (ptr_t)((word)start & ~(GC_page_size - 1));
1211 q = (LPVOID)(p - GC_page_size);
1212 if ((ptr_t)q > (ptr_t)p /* underflow */ || q < limit) break;
1213 result = VirtualQuery(q, &buf, sizeof(buf));
1214 if (result != sizeof(buf) || buf.AllocationBase == 0) break;
1215 p = (ptr_t)(buf.AllocationBase);
1221 # ifndef REDIRECT_MALLOC
1222 /* We maintain a linked list of AllocationBase values that we know */
1223 /* correspond to malloc heap sections. Currently this is only called */
1224 /* during a GC. But there is some hope that for long running */
1225 /* programs we will eventually see most heap sections. */
1227 /* In the long run, it would be more reliable to occasionally walk */
1228 /* the malloc heap with HeapWalk on the default heap. But that */
1229 /* apparently works only for NT-based Windows. */
1231 /* In the long run, a better data structure would also be nice ... */
1232 struct GC_malloc_heap_list {
1233 void * allocation_base;
1234 struct GC_malloc_heap_list *next;
1235 } *GC_malloc_heap_l = 0;
1237 /* Is p the base of one of the malloc heap sections we already know */
1239 GC_bool GC_is_malloc_heap_base(ptr_t p)
1241 struct GC_malloc_heap_list *q = GC_malloc_heap_l;
1244 if (q -> allocation_base == p) return TRUE;
1250 void *GC_get_allocation_base(void *p)
1252 MEMORY_BASIC_INFORMATION buf;
1253 DWORD result = VirtualQuery(p, &buf, sizeof(buf));
1254 if (result != sizeof(buf)) {
1255 ABORT("Weird VirtualQuery result");
1257 return buf.AllocationBase;
1260 size_t GC_max_root_size = 100000; /* Appr. largest root size. */
1262 void GC_add_current_malloc_heap()
1264 struct GC_malloc_heap_list *new_l =
1265 malloc(sizeof(struct GC_malloc_heap_list));
1266 void * candidate = GC_get_allocation_base(new_l);
1268 if (new_l == 0) return;
1269 if (GC_is_malloc_heap_base(candidate)) {
1270 /* Try a little harder to find malloc heap. */
1271 size_t req_size = 10000;
1273 void *p = malloc(req_size);
1274 if (0 == p) { free(new_l); return; }
1275 candidate = GC_get_allocation_base(p);
1278 } while (GC_is_malloc_heap_base(candidate)
1279 && req_size < GC_max_root_size/10 && req_size < 500000);
1280 if (GC_is_malloc_heap_base(candidate)) {
1281 free(new_l); return;
1286 GC_printf1("Found new system malloc AllocationBase at 0x%lx\n",
1289 new_l -> allocation_base = candidate;
1290 new_l -> next = GC_malloc_heap_l;
1291 GC_malloc_heap_l = new_l;
1293 # endif /* REDIRECT_MALLOC */
1295 /* Is p the start of either the malloc heap, or of one of our */
1296 /* heap sections? */
1297 GC_bool GC_is_heap_base (ptr_t p)
1302 # ifndef REDIRECT_MALLOC
1303 static word last_gc_no = -1;
1305 if (last_gc_no != GC_gc_no) {
1306 GC_add_current_malloc_heap();
1307 last_gc_no = GC_gc_no;
1309 if (GC_root_size > GC_max_root_size) GC_max_root_size = GC_root_size;
1310 if (GC_is_malloc_heap_base(p)) return TRUE;
1312 for (i = 0; i < GC_n_heap_bases; i++) {
1313 if (GC_heap_bases[i] == p) return TRUE;
1319 void GC_register_root_section(ptr_t static_root)
1321 MEMORY_BASIC_INFORMATION buf;
1326 char * limit, * new_limit;
1328 if (!GC_no_win32_dlls) return;
1329 p = base = limit = GC_least_described_address(static_root);
1330 while (p < GC_sysinfo.lpMaximumApplicationAddress) {
1331 result = VirtualQuery(p, &buf, sizeof(buf));
1332 if (result != sizeof(buf) || buf.AllocationBase == 0
1333 || GC_is_heap_base(buf.AllocationBase)) break;
1334 new_limit = (char *)p + buf.RegionSize;
1335 protect = buf.Protect;
1336 if (buf.State == MEM_COMMIT
1337 && is_writable(protect)) {
1338 if ((char *)p == limit) {
1341 if (base != limit) GC_add_roots_inner(base, limit, FALSE);
1346 if (p > (LPVOID)new_limit /* overflow */) break;
1347 p = (LPVOID)new_limit;
1349 if (base != limit) GC_add_roots_inner(base, limit, FALSE);
1353 void GC_register_data_segments()
1357 GC_register_root_section((ptr_t)(&dummy));
1361 # else /* !OS2 && !Windows */
1363 # if (defined(SVR4) || defined(AUX) || defined(DGUX) \
1364 || (defined(LINUX) && defined(SPARC))) && !defined(PCR)
1365 ptr_t GC_SysVGetDataStart(max_page_size, etext_addr)
1369 word text_end = ((word)(etext_addr) + sizeof(word) - 1)
1370 & ~(sizeof(word) - 1);
1371 /* etext rounded to word boundary */
1372 word next_page = ((text_end + (word)max_page_size - 1)
1373 & ~((word)max_page_size - 1));
1374 word page_offset = (text_end & ((word)max_page_size - 1));
1375 VOLATILE char * result = (char *)(next_page + page_offset);
1376 /* Note that this isnt equivalent to just adding */
1377 /* max_page_size to &etext if &etext is at a page boundary */
1379 GC_setup_temporary_fault_handler();
1380 if (SETJMP(GC_jmp_buf) == 0) {
1381 /* Try writing to the address. */
1383 GC_reset_fault_handler();
1385 GC_reset_fault_handler();
1386 /* We got here via a longjmp. The address is not readable. */
1387 /* This is known to happen under Solaris 2.4 + gcc, which place */
1388 /* string constants in the text segment, but after etext. */
1389 /* Use plan B. Note that we now know there is a gap between */
1390 /* text and data segments, so plan A bought us something. */
1391 result = (char *)GC_find_limit((ptr_t)(DATAEND), FALSE);
1393 return((ptr_t)result);
1397 # if defined(FREEBSD) && (defined(I386) || defined(powerpc) || defined(__powerpc__)) && !defined(PCR)
1398 /* Its unclear whether this should be identical to the above, or */
1399 /* whether it should apply to non-X86 architectures. */
1400 /* For now we don't assume that there is always an empty page after */
1401 /* etext. But in some cases there actually seems to be slightly more. */
1402 /* This also deals with holes between read-only data and writable data. */
1403 ptr_t GC_FreeBSDGetDataStart(max_page_size, etext_addr)
1407 word text_end = ((word)(etext_addr) + sizeof(word) - 1)
1408 & ~(sizeof(word) - 1);
1409 /* etext rounded to word boundary */
1410 VOLATILE word next_page = (text_end + (word)max_page_size - 1)
1411 & ~((word)max_page_size - 1);
1412 VOLATILE ptr_t result = (ptr_t)text_end;
1413 GC_setup_temporary_fault_handler();
1414 if (SETJMP(GC_jmp_buf) == 0) {
1415 /* Try reading at the address. */
1416 /* This should happen before there is another thread. */
1417 for (; next_page < (word)(DATAEND); next_page += (word)max_page_size)
1418 *(VOLATILE char *)next_page;
1419 GC_reset_fault_handler();
1421 GC_reset_fault_handler();
1422 /* As above, we go to plan B */
1423 result = GC_find_limit((ptr_t)(DATAEND), FALSE);
1433 # define GC_AMIGA_DS
1434 # include "AmigaOS.c"
1437 #else /* !OS2 && !Windows && !AMIGA */
1439 void GC_register_data_segments()
1441 # if !defined(PCR) && !defined(SRC_M3) && !defined(MACOS)
1442 # if defined(REDIRECT_MALLOC) && defined(GC_SOLARIS_THREADS)
1443 /* As of Solaris 2.3, the Solaris threads implementation */
1444 /* allocates the data structure for the initial thread with */
1445 /* sbrk at process startup. It needs to be scanned, so that */
1446 /* we don't lose some malloc allocated data structures */
1447 /* hanging from it. We're on thin ice here ... */
1448 extern caddr_t sbrk();
1450 GC_add_roots_inner(DATASTART, (char *)sbrk(0), FALSE);
1452 GC_add_roots_inner(DATASTART, (char *)(DATAEND), FALSE);
1453 # if defined(DATASTART2)
1454 GC_add_roots_inner(DATASTART2, (char *)(DATAEND2), FALSE);
1460 # if defined(THINK_C)
1461 extern void* GC_MacGetDataStart(void);
1462 /* globals begin above stack and end at a5. */
1463 GC_add_roots_inner((ptr_t)GC_MacGetDataStart(),
1464 (ptr_t)LMGetCurrentA5(), FALSE);
1466 # if defined(__MWERKS__)
1468 extern void* GC_MacGetDataStart(void);
1469 /* MATTHEW: Function to handle Far Globals (CW Pro 3) */
1470 # if __option(far_data)
1471 extern void* GC_MacGetDataEnd(void);
1473 /* globals begin above stack and end at a5. */
1474 GC_add_roots_inner((ptr_t)GC_MacGetDataStart(),
1475 (ptr_t)LMGetCurrentA5(), FALSE);
1476 /* MATTHEW: Handle Far Globals */
1477 # if __option(far_data)
1478 /* Far globals follow he QD globals: */
1479 GC_add_roots_inner((ptr_t)LMGetCurrentA5(),
1480 (ptr_t)GC_MacGetDataEnd(), FALSE);
1483 extern char __data_start__[], __data_end__[];
1484 GC_add_roots_inner((ptr_t)&__data_start__,
1485 (ptr_t)&__data_end__, FALSE);
1486 # endif /* __POWERPC__ */
1487 # endif /* __MWERKS__ */
1488 # endif /* !THINK_C */
1492 /* Dynamic libraries are added at every collection, since they may */
1496 # endif /* ! AMIGA */
1497 # endif /* ! MSWIN32 && ! MSWINCE*/
1501 * Auxiliary routines for obtaining memory from OS.
1504 # if !defined(OS2) && !defined(PCR) && !defined(AMIGA) \
1505 && !defined(MSWIN32) && !defined(MSWINCE) \
1506 && !defined(MACOS) && !defined(DOS4GW) && !defined(NONSTOP)
1509 extern caddr_t sbrk();
1512 # define SBRK_ARG_T ptrdiff_t
1514 # define SBRK_ARG_T int
1518 # if 0 && defined(RS6000) /* We now use mmap */
1519 /* The compiler seems to generate speculative reads one past the end of */
1520 /* an allocated object. Hence we need to make sure that the page */
1521 /* following the last heap page is also mapped. */
1522 ptr_t GC_unix_get_mem(bytes)
1525 caddr_t cur_brk = (caddr_t)sbrk(0);
1527 SBRK_ARG_T lsbs = (word)cur_brk & (GC_page_size-1);
1528 static caddr_t my_brk_val = 0;
1530 if ((SBRK_ARG_T)bytes < 0) return(0); /* too big */
1532 if((caddr_t)(sbrk(GC_page_size - lsbs)) == (caddr_t)(-1)) return(0);
1534 if (cur_brk == my_brk_val) {
1535 /* Use the extra block we allocated last time. */
1536 result = (ptr_t)sbrk((SBRK_ARG_T)bytes);
1537 if (result == (caddr_t)(-1)) return(0);
1538 result -= GC_page_size;
1540 result = (ptr_t)sbrk(GC_page_size + (SBRK_ARG_T)bytes);
1541 if (result == (caddr_t)(-1)) return(0);
1543 my_brk_val = result + bytes + GC_page_size; /* Always page aligned */
1544 return((ptr_t)result);
1547 #else /* Not RS6000 */
1549 #if defined(USE_MMAP) || defined(USE_MUNMAP)
1551 #ifdef USE_MMAP_FIXED
1552 # define GC_MMAP_FLAGS MAP_FIXED | MAP_PRIVATE
1553 /* Seems to yield better performance on Solaris 2, but can */
1554 /* be unreliable if something is already mapped at the address. */
1556 # define GC_MMAP_FLAGS MAP_PRIVATE
1559 #ifdef USE_MMAP_ANON
1561 # if defined(MAP_ANONYMOUS)
1562 # define OPT_MAP_ANON MAP_ANONYMOUS
1564 # define OPT_MAP_ANON MAP_ANON
1568 # define OPT_MAP_ANON 0
1571 #endif /* defined(USE_MMAP) || defined(USE_MUNMAP) */
1573 #if defined(USE_MMAP)
1574 /* Tested only under Linux, IRIX5 and Solaris 2 */
1577 # define HEAP_START 0
1580 ptr_t GC_unix_get_mem(bytes)
1584 static ptr_t last_addr = HEAP_START;
1586 # ifndef USE_MMAP_ANON
1587 static GC_bool initialized = FALSE;
1590 zero_fd = open("/dev/zero", O_RDONLY);
1591 fcntl(zero_fd, F_SETFD, FD_CLOEXEC);
1596 if (bytes & (GC_page_size -1)) ABORT("Bad GET_MEM arg");
1597 result = mmap(last_addr, bytes, PROT_READ | PROT_WRITE | OPT_PROT_EXEC,
1598 GC_MMAP_FLAGS | OPT_MAP_ANON, zero_fd, 0/* offset */);
1599 if (result == MAP_FAILED) return(0);
1600 last_addr = (ptr_t)result + bytes + GC_page_size - 1;
1601 last_addr = (ptr_t)((word)last_addr & ~(GC_page_size - 1));
1602 # if !defined(LINUX)
1603 if (last_addr == 0) {
1604 /* Oops. We got the end of the address space. This isn't */
1605 /* usable by arbitrary C code, since one-past-end pointers */
1606 /* don't work, so we discard it and try again. */
1607 munmap(result, (size_t)(-GC_page_size) - (size_t)result);
1608 /* Leave last page mapped, so we can't repeat. */
1609 return GC_unix_get_mem(bytes);
1612 GC_ASSERT(last_addr != 0);
1614 return((ptr_t)result);
1617 #else /* Not RS6000, not USE_MMAP */
1618 ptr_t GC_unix_get_mem(bytes)
1623 /* Bare sbrk isn't thread safe. Play by malloc rules. */
1624 /* The equivalent may be needed on other systems as well. */
1628 ptr_t cur_brk = (ptr_t)sbrk(0);
1629 SBRK_ARG_T lsbs = (word)cur_brk & (GC_page_size-1);
1631 if ((SBRK_ARG_T)bytes < 0) {
1632 result = 0; /* too big */
1636 if((ptr_t)sbrk(GC_page_size - lsbs) == (ptr_t)(-1)) {
1641 result = (ptr_t)sbrk((SBRK_ARG_T)bytes);
1642 if (result == (ptr_t)(-1)) result = 0;
1651 #endif /* Not USE_MMAP */
1652 #endif /* Not RS6000 */
1658 void * os2_alloc(size_t bytes)
1662 if (DosAllocMem(&result, bytes, PAG_EXECUTE | PAG_READ |
1663 PAG_WRITE | PAG_COMMIT)
1667 if (result == 0) return(os2_alloc(bytes));
1674 # if defined(MSWIN32) || defined(MSWINCE)
1675 SYSTEM_INFO GC_sysinfo;
1680 # ifdef USE_GLOBAL_ALLOC
1681 # define GLOBAL_ALLOC_TEST 1
1683 # define GLOBAL_ALLOC_TEST GC_no_win32_dlls
1686 word GC_n_heap_bases = 0;
1688 ptr_t GC_win32_get_mem(bytes)
1693 if (GLOBAL_ALLOC_TEST) {
1694 /* VirtualAlloc doesn't like PAGE_EXECUTE_READWRITE. */
1695 /* There are also unconfirmed rumors of other */
1696 /* problems, so we dodge the issue. */
1697 result = (ptr_t) GlobalAlloc(0, bytes + HBLKSIZE);
1698 result = (ptr_t)(((word)result + HBLKSIZE) & ~(HBLKSIZE-1));
1700 /* VirtualProtect only works on regions returned by a */
1701 /* single VirtualAlloc call. Thus we allocate one */
1702 /* extra page, which will prevent merging of blocks */
1703 /* in separate regions, and eliminate any temptation */
1704 /* to call VirtualProtect on a range spanning regions. */
1705 /* This wastes a small amount of memory, and risks */
1706 /* increased fragmentation. But better alternatives */
1707 /* would require effort. */
1708 result = (ptr_t) VirtualAlloc(NULL, bytes + 1,
1709 MEM_COMMIT | MEM_RESERVE,
1710 PAGE_EXECUTE_READWRITE);
1712 if (HBLKDISPL(result) != 0) ABORT("Bad VirtualAlloc result");
1713 /* If I read the documentation correctly, this can */
1714 /* only happen if HBLKSIZE > 64k or not a power of 2. */
1715 if (GC_n_heap_bases >= MAX_HEAP_SECTS) ABORT("Too many heap sections");
1716 GC_heap_bases[GC_n_heap_bases++] = result;
1720 void GC_win32_free_heap ()
1722 if (GC_no_win32_dlls) {
1723 while (GC_n_heap_bases > 0) {
1724 GlobalFree (GC_heap_bases[--GC_n_heap_bases]);
1725 GC_heap_bases[GC_n_heap_bases] = 0;
1732 # define GC_AMIGA_AM
1733 # include "AmigaOS.c"
1739 word GC_n_heap_bases = 0;
1741 ptr_t GC_wince_get_mem(bytes)
1747 /* Round up allocation size to multiple of page size */
1748 bytes = (bytes + GC_page_size-1) & ~(GC_page_size-1);
1750 /* Try to find reserved, uncommitted pages */
1751 for (i = 0; i < GC_n_heap_bases; i++) {
1752 if (((word)(-(signed_word)GC_heap_lengths[i])
1753 & (GC_sysinfo.dwAllocationGranularity-1))
1755 result = GC_heap_bases[i] + GC_heap_lengths[i];
1760 if (i == GC_n_heap_bases) {
1761 /* Reserve more pages */
1762 word res_bytes = (bytes + GC_sysinfo.dwAllocationGranularity-1)
1763 & ~(GC_sysinfo.dwAllocationGranularity-1);
1764 /* If we ever support MPROTECT_VDB here, we will probably need to */
1765 /* ensure that res_bytes is strictly > bytes, so that VirtualProtect */
1766 /* never spans regions. It seems to be OK for a VirtualFree argument */
1767 /* to span regions, so we should be OK for now. */
1768 result = (ptr_t) VirtualAlloc(NULL, res_bytes,
1769 MEM_RESERVE | MEM_TOP_DOWN,
1770 PAGE_EXECUTE_READWRITE);
1771 if (HBLKDISPL(result) != 0) ABORT("Bad VirtualAlloc result");
1772 /* If I read the documentation correctly, this can */
1773 /* only happen if HBLKSIZE > 64k or not a power of 2. */
1774 if (GC_n_heap_bases >= MAX_HEAP_SECTS) ABORT("Too many heap sections");
1775 GC_heap_bases[GC_n_heap_bases] = result;
1776 GC_heap_lengths[GC_n_heap_bases] = 0;
1781 result = (ptr_t) VirtualAlloc(result, bytes,
1783 PAGE_EXECUTE_READWRITE);
1784 if (result != NULL) {
1785 if (HBLKDISPL(result) != 0) ABORT("Bad VirtualAlloc result");
1786 GC_heap_lengths[i] += bytes;
1795 /* For now, this only works on Win32/WinCE and some Unix-like */
1796 /* systems. If you have something else, don't define */
1798 /* We assume ANSI C to support this feature. */
1800 #if !defined(MSWIN32) && !defined(MSWINCE)
1803 #include <sys/mman.h>
1804 #include <sys/stat.h>
1805 #include <sys/types.h>
1809 /* Compute a page aligned starting address for the unmap */
1810 /* operation on a block of size bytes starting at start. */
1811 /* Return 0 if the block is too small to make this feasible. */
1812 ptr_t GC_unmap_start(ptr_t start, word bytes)
1814 ptr_t result = start;
1815 /* Round start to next page boundary. */
1816 result += GC_page_size - 1;
1817 result = (ptr_t)((word)result & ~(GC_page_size - 1));
1818 if (result + GC_page_size > start + bytes) return 0;
1822 /* Compute end address for an unmap operation on the indicated */
1824 ptr_t GC_unmap_end(ptr_t start, word bytes)
1826 ptr_t end_addr = start + bytes;
1827 end_addr = (ptr_t)((word)end_addr & ~(GC_page_size - 1));
1831 /* Under Win32/WinCE we commit (map) and decommit (unmap) */
1832 /* memory using VirtualAlloc and VirtualFree. These functions */
1833 /* work on individual allocations of virtual memory, made */
1834 /* previously using VirtualAlloc with the MEM_RESERVE flag. */
1835 /* The ranges we need to (de)commit may span several of these */
1836 /* allocations; therefore we use VirtualQuery to check */
1837 /* allocation lengths, and split up the range as necessary. */
1839 /* We assume that GC_remap is called on exactly the same range */
1840 /* as a previous call to GC_unmap. It is safe to consistently */
1841 /* round the endpoints in both places. */
1842 void GC_unmap(ptr_t start, word bytes)
1844 ptr_t start_addr = GC_unmap_start(start, bytes);
1845 ptr_t end_addr = GC_unmap_end(start, bytes);
1846 word len = end_addr - start_addr;
1847 if (0 == start_addr) return;
1848 # if defined(MSWIN32) || defined(MSWINCE)
1850 MEMORY_BASIC_INFORMATION mem_info;
1852 if (VirtualQuery(start_addr, &mem_info, sizeof(mem_info))
1853 != sizeof(mem_info))
1854 ABORT("Weird VirtualQuery result");
1855 free_len = (len < mem_info.RegionSize) ? len : mem_info.RegionSize;
1856 if (!VirtualFree(start_addr, free_len, MEM_DECOMMIT))
1857 ABORT("VirtualFree failed");
1858 GC_unmapped_bytes += free_len;
1859 start_addr += free_len;
1863 /* We immediately remap it to prevent an intervening mmap from */
1864 /* accidentally grabbing the same address space. */
1867 result = mmap(start_addr, len, PROT_NONE,
1868 MAP_PRIVATE | MAP_FIXED | OPT_MAP_ANON,
1869 zero_fd, 0/* offset */);
1870 if (result != (void *)start_addr) ABORT("mmap(...PROT_NONE...) failed");
1872 GC_unmapped_bytes += len;
1877 void GC_remap(ptr_t start, word bytes)
1879 ptr_t start_addr = GC_unmap_start(start, bytes);
1880 ptr_t end_addr = GC_unmap_end(start, bytes);
1881 word len = end_addr - start_addr;
1883 # if defined(MSWIN32) || defined(MSWINCE)
1886 if (0 == start_addr) return;
1888 MEMORY_BASIC_INFORMATION mem_info;
1890 if (VirtualQuery(start_addr, &mem_info, sizeof(mem_info))
1891 != sizeof(mem_info))
1892 ABORT("Weird VirtualQuery result");
1893 alloc_len = (len < mem_info.RegionSize) ? len : mem_info.RegionSize;
1894 result = VirtualAlloc(start_addr, alloc_len,
1896 PAGE_EXECUTE_READWRITE);
1897 if (result != start_addr) {
1898 ABORT("VirtualAlloc remapping failed");
1900 GC_unmapped_bytes -= alloc_len;
1901 start_addr += alloc_len;
1905 /* It was already remapped with PROT_NONE. */
1908 if (0 == start_addr) return;
1909 result = mprotect(start_addr, len,
1910 PROT_READ | PROT_WRITE | OPT_PROT_EXEC);
1913 "Mprotect failed at 0x%lx (length %ld) with errno %ld\n",
1914 start_addr, len, errno);
1915 ABORT("Mprotect remapping failed");
1917 GC_unmapped_bytes -= len;
1921 /* Two adjacent blocks have already been unmapped and are about to */
1922 /* be merged. Unmap the whole block. This typically requires */
1923 /* that we unmap a small section in the middle that was not previously */
1924 /* unmapped due to alignment constraints. */
1925 void GC_unmap_gap(ptr_t start1, word bytes1, ptr_t start2, word bytes2)
1927 ptr_t start1_addr = GC_unmap_start(start1, bytes1);
1928 ptr_t end1_addr = GC_unmap_end(start1, bytes1);
1929 ptr_t start2_addr = GC_unmap_start(start2, bytes2);
1930 ptr_t end2_addr = GC_unmap_end(start2, bytes2);
1931 ptr_t start_addr = end1_addr;
1932 ptr_t end_addr = start2_addr;
1934 GC_ASSERT(start1 + bytes1 == start2);
1935 if (0 == start1_addr) start_addr = GC_unmap_start(start1, bytes1 + bytes2);
1936 if (0 == start2_addr) end_addr = GC_unmap_end(start1, bytes1 + bytes2);
1937 if (0 == start_addr) return;
1938 len = end_addr - start_addr;
1939 # if defined(MSWIN32) || defined(MSWINCE)
1941 MEMORY_BASIC_INFORMATION mem_info;
1943 if (VirtualQuery(start_addr, &mem_info, sizeof(mem_info))
1944 != sizeof(mem_info))
1945 ABORT("Weird VirtualQuery result");
1946 free_len = (len < mem_info.RegionSize) ? len : mem_info.RegionSize;
1947 if (!VirtualFree(start_addr, free_len, MEM_DECOMMIT))
1948 ABORT("VirtualFree failed");
1949 GC_unmapped_bytes += free_len;
1950 start_addr += free_len;
1954 if (len != 0 && munmap(start_addr, len) != 0) ABORT("munmap failed");
1955 GC_unmapped_bytes += len;
1959 #endif /* USE_MUNMAP */
1961 /* Routine for pushing any additional roots. In THREADS */
1962 /* environment, this is also responsible for marking from */
1963 /* thread stacks. */
1965 void (*GC_push_other_roots)() = 0;
1969 PCR_ERes GC_push_thread_stack(PCR_Th_T *t, PCR_Any dummy)
1971 struct PCR_ThCtl_TInfoRep info;
1974 info.ti_stkLow = info.ti_stkHi = 0;
1975 result = PCR_ThCtl_GetInfo(t, &info);
1976 GC_push_all_stack((ptr_t)(info.ti_stkLow), (ptr_t)(info.ti_stkHi));
1980 /* Push the contents of an old object. We treat this as stack */
1981 /* data only becasue that makes it robust against mark stack */
1983 PCR_ERes GC_push_old_obj(void *p, size_t size, PCR_Any data)
1985 GC_push_all_stack((ptr_t)p, (ptr_t)p + size);
1986 return(PCR_ERes_okay);
1990 void GC_default_push_other_roots GC_PROTO((void))
1992 /* Traverse data allocated by previous memory managers. */
1994 extern struct PCR_MM_ProcsRep * GC_old_allocator;
1996 if ((*(GC_old_allocator->mmp_enumerate))(PCR_Bool_false,
1999 ABORT("Old object enumeration failed");
2002 /* Traverse all thread stacks. */
2004 PCR_ThCtl_ApplyToAllOtherThreads(GC_push_thread_stack,0))
2005 || PCR_ERes_IsErr(GC_push_thread_stack(PCR_Th_CurrThread(), 0))) {
2006 ABORT("Thread stack marking failed\n");
2014 # ifdef ALL_INTERIOR_POINTERS
2018 void GC_push_thread_structures GC_PROTO((void))
2020 /* Not our responsibibility. */
2023 extern void ThreadF__ProcessStacks();
2025 void GC_push_thread_stack(start, stop)
2028 GC_push_all_stack((ptr_t)start, (ptr_t)stop + sizeof(word));
2031 /* Push routine with M3 specific calling convention. */
2032 GC_m3_push_root(dummy1, p, dummy2, dummy3)
2034 ptr_t dummy1, dummy2;
2039 GC_PUSH_ONE_STACK(q, p);
2042 /* M3 set equivalent to RTHeap.TracedRefTypes */
2043 typedef struct { int elts[1]; } RefTypeSet;
2044 RefTypeSet GC_TracedRefTypes = {{0x1}};
2046 void GC_default_push_other_roots GC_PROTO((void))
2048 /* Use the M3 provided routine for finding static roots. */
2049 /* This is a bit dubious, since it presumes no C roots. */
2050 /* We handle the collector roots explicitly in GC_push_roots */
2051 RTMain__GlobalMapProc(GC_m3_push_root, 0, GC_TracedRefTypes);
2052 if (GC_words_allocd > 0) {
2053 ThreadF__ProcessStacks(GC_push_thread_stack);
2055 /* Otherwise this isn't absolutely necessary, and we have */
2056 /* startup ordering problems. */
2059 # endif /* SRC_M3 */
2061 # if defined(GC_SOLARIS_THREADS) || defined(GC_PTHREADS) || \
2062 defined(GC_WIN32_THREADS)
2064 extern void GC_push_all_stacks();
2066 void GC_default_push_other_roots GC_PROTO((void))
2068 GC_push_all_stacks();
2071 # endif /* GC_SOLARIS_THREADS || GC_PTHREADS */
2073 void (*GC_push_other_roots) GC_PROTO((void)) = GC_default_push_other_roots;
2075 #endif /* THREADS */
2078 * Routines for accessing dirty bits on virtual pages.
2079 * We plan to eventually implement four strategies for doing so:
2080 * DEFAULT_VDB: A simple dummy implementation that treats every page
2081 * as possibly dirty. This makes incremental collection
2082 * useless, but the implementation is still correct.
2083 * PCR_VDB: Use PPCRs virtual dirty bit facility.
2084 * PROC_VDB: Use the /proc facility for reading dirty bits. Only
2085 * works under some SVR4 variants. Even then, it may be
2086 * too slow to be entirely satisfactory. Requires reading
2087 * dirty bits for entire address space. Implementations tend
2088 * to assume that the client is a (slow) debugger.
2089 * MPROTECT_VDB:Protect pages and then catch the faults to keep track of
2090 * dirtied pages. The implementation (and implementability)
2091 * is highly system dependent. This usually fails when system
2092 * calls write to a protected page. We prevent the read system
2093 * call from doing so. It is the clients responsibility to
2094 * make sure that other system calls are similarly protected
2095 * or write only to the stack.
2097 GC_bool GC_dirty_maintained = FALSE;
2101 /* All of the following assume the allocation lock is held, and */
2102 /* signals are disabled. */
2104 /* The client asserts that unallocated pages in the heap are never */
2107 /* Initialize virtual dirty bit implementation. */
2108 void GC_dirty_init()
2111 GC_printf0("Initializing DEFAULT_VDB...\n");
2113 GC_dirty_maintained = TRUE;
2116 /* Retrieve system dirty bits for heap to a local buffer. */
2117 /* Restore the systems notion of which pages are dirty. */
2118 void GC_read_dirty()
2121 /* Is the HBLKSIZE sized page at h marked dirty in the local buffer? */
2122 /* If the actual page size is different, this returns TRUE if any */
2123 /* of the pages overlapping h are dirty. This routine may err on the */
2124 /* side of labelling pages as dirty (and this implementation does). */
2126 GC_bool GC_page_was_dirty(h)
2133 * The following two routines are typically less crucial. They matter
2134 * most with large dynamic libraries, or if we can't accurately identify
2135 * stacks, e.g. under Solaris 2.X. Otherwise the following default
2136 * versions are adequate.
2139 /* Could any valid GC heap pointer ever have been written to this page? */
2141 GC_bool GC_page_was_ever_dirty(h)
2147 /* Reset the n pages starting at h to "was never dirty" status. */
2148 void GC_is_fresh(h, n)
2155 /* I) hints that [h, h+nblocks) is about to be written. */
2156 /* II) guarantees that protection is removed. */
2157 /* (I) may speed up some dirty bit implementations. */
2158 /* (II) may be essential if we need to ensure that */
2159 /* pointer-free system call buffers in the heap are */
2160 /* not protected. */
2162 void GC_remove_protection(h, nblocks, is_ptrfree)
2169 # endif /* DEFAULT_VDB */
2172 # ifdef MPROTECT_VDB
2175 * See DEFAULT_VDB for interface descriptions.
2179 * This implementation maintains dirty bits itself by catching write
2180 * faults and keeping track of them. We assume nobody else catches
2181 * SIGBUS or SIGSEGV. We assume no write faults occur in system calls.
2182 * This means that clients must ensure that system calls don't write
2183 * to the write-protected heap. Probably the best way to do this is to
2184 * ensure that system calls write at most to POINTERFREE objects in the
2185 * heap, and do even that only if we are on a platform on which those
2186 * are not protected. Another alternative is to wrap system calls
2187 * (see example for read below), but the current implementation holds
2188 * a lock across blocking calls, making it problematic for multithreaded
2190 * We assume the page size is a multiple of HBLKSIZE.
2191 * We prefer them to be the same. We avoid protecting POINTERFREE
2192 * objects only if they are the same.
2195 # if !defined(MSWIN32) && !defined(MSWINCE) && !defined(DARWIN)
2197 # include <sys/mman.h>
2198 # include <signal.h>
2199 # include <sys/syscall.h>
2201 # define PROTECT(addr, len) \
2202 if (mprotect((caddr_t)(addr), (size_t)(len), \
2203 PROT_READ | OPT_PROT_EXEC) < 0) { \
2204 ABORT("mprotect failed"); \
2206 # define UNPROTECT(addr, len) \
2207 if (mprotect((caddr_t)(addr), (size_t)(len), \
2208 PROT_WRITE | PROT_READ | OPT_PROT_EXEC ) < 0) { \
2209 ABORT("un-mprotect failed"); \
2215 /* Using vm_protect (mach syscall) over mprotect (BSD syscall) seems to
2216 decrease the likelihood of some of the problems described below. */
2217 #include <mach/vm_map.h>
2218 static mach_port_t GC_task_self;
2219 #define PROTECT(addr,len) \
2220 if(vm_protect(GC_task_self,(vm_address_t)(addr),(vm_size_t)(len), \
2221 FALSE,VM_PROT_READ) != KERN_SUCCESS) { \
2222 ABORT("vm_portect failed"); \
2224 #define UNPROTECT(addr,len) \
2225 if(vm_protect(GC_task_self,(vm_address_t)(addr),(vm_size_t)(len), \
2226 FALSE,VM_PROT_READ|VM_PROT_WRITE) != KERN_SUCCESS) { \
2227 ABORT("vm_portect failed"); \
2232 # include <signal.h>
2235 static DWORD protect_junk;
2236 # define PROTECT(addr, len) \
2237 if (!VirtualProtect((addr), (len), PAGE_EXECUTE_READ, \
2239 DWORD last_error = GetLastError(); \
2240 GC_printf1("Last error code: %lx\n", last_error); \
2241 ABORT("VirtualProtect failed"); \
2243 # define UNPROTECT(addr, len) \
2244 if (!VirtualProtect((addr), (len), PAGE_EXECUTE_READWRITE, \
2246 ABORT("un-VirtualProtect failed"); \
2248 # endif /* !DARWIN */
2249 # endif /* MSWIN32 || MSWINCE || DARWIN */
2251 #if defined(SUNOS4) || (defined(FREEBSD) && !defined(SUNOS5SIGS))
2252 typedef void (* SIG_PF)();
2253 #endif /* SUNOS4 || (FREEBSD && !SUNOS5SIGS) */
2255 #if defined(SUNOS5SIGS) || defined(OSF1) || defined(LINUX) \
2258 typedef void (* SIG_PF)(int);
2260 typedef void (* SIG_PF)();
2262 #endif /* SUNOS5SIGS || OSF1 || LINUX || HURD */
2264 #if defined(MSWIN32)
2265 typedef LPTOP_LEVEL_EXCEPTION_FILTER SIG_PF;
2267 # define SIG_DFL (LPTOP_LEVEL_EXCEPTION_FILTER) (-1)
2269 #if defined(MSWINCE)
2270 typedef LONG (WINAPI *SIG_PF)(struct _EXCEPTION_POINTERS *);
2272 # define SIG_DFL (SIG_PF) (-1)
2275 #if defined(IRIX5) || defined(OSF1) || defined(HURD)
2276 typedef void (* REAL_SIG_PF)(int, int, struct sigcontext *);
2277 #endif /* IRIX5 || OSF1 || HURD */
2279 #if defined(SUNOS5SIGS)
2280 # if defined(HPUX) || defined(FREEBSD)
2281 # define SIGINFO_T siginfo_t
2283 # define SIGINFO_T struct siginfo
2286 typedef void (* REAL_SIG_PF)(int, SIGINFO_T *, void *);
2288 typedef void (* REAL_SIG_PF)();
2290 #endif /* SUNOS5SIGS */
2293 # if __GLIBC__ > 2 || __GLIBC__ == 2 && __GLIBC_MINOR__ >= 2
2294 typedef struct sigcontext s_c;
2295 # else /* glibc < 2.2 */
2296 # include <linux/version.h>
2297 # if (LINUX_VERSION_CODE >= 0x20100) && !defined(M68K) || defined(ALPHA) || defined(ARM32)
2298 typedef struct sigcontext s_c;
2300 typedef struct sigcontext_struct s_c;
2302 # endif /* glibc < 2.2 */
2303 # if defined(ALPHA) || defined(M68K)
2304 typedef void (* REAL_SIG_PF)(int, int, s_c *);
2306 # if defined(IA64) || defined(HP_PA) || defined(X86_64)
2307 typedef void (* REAL_SIG_PF)(int, siginfo_t *, s_c *);
2309 /* According to SUSV3, the last argument should have type */
2310 /* void * or ucontext_t * */
2312 typedef void (* REAL_SIG_PF)(int, s_c);
2316 /* Retrieve fault address from sigcontext structure by decoding */
2318 char * get_fault_addr(s_c *sc) {
2322 instr = *((unsigned *)(sc->sc_pc));
2323 faultaddr = sc->sc_regs[(instr >> 16) & 0x1f];
2324 faultaddr += (word) (((int)instr << 16) >> 16);
2325 return (char *)faultaddr;
2327 # endif /* !ALPHA */
2331 SIG_PF GC_old_bus_handler;
2332 SIG_PF GC_old_segv_handler; /* Also old MSWIN32 ACCESS_VIOLATION filter */
2333 #endif /* !DARWIN */
2335 #if defined(THREADS)
2336 /* We need to lock around the bitmap update in the write fault handler */
2337 /* in order to avoid the risk of losing a bit. We do this with a */
2338 /* test-and-set spin lock if we know how to do that. Otherwise we */
2339 /* check whether we are already in the handler and use the dumb but */
2340 /* safe fallback algorithm of setting all bits in the word. */
2341 /* Contention should be very rare, so we do the minimum to handle it */
2343 #ifdef GC_TEST_AND_SET_DEFINED
2344 static VOLATILE unsigned int fault_handler_lock = 0;
2345 void async_set_pht_entry_from_index(VOLATILE page_hash_table db, int index) {
2346 while (GC_test_and_set(&fault_handler_lock)) {}
2347 /* Could also revert to set_pht_entry_from_index_safe if initial */
2348 /* GC_test_and_set fails. */
2349 set_pht_entry_from_index(db, index);
2350 GC_clear(&fault_handler_lock);
2352 #else /* !GC_TEST_AND_SET_DEFINED */
2353 /* THIS IS INCORRECT! The dirty bit vector may be temporarily wrong, */
2354 /* just before we notice the conflict and correct it. We may end up */
2355 /* looking at it while it's wrong. But this requires contention */
2356 /* exactly when a GC is triggered, which seems far less likely to */
2357 /* fail than the old code, which had no reported failures. Thus we */
2358 /* leave it this way while we think of something better, or support */
2359 /* GC_test_and_set on the remaining platforms. */
2360 static VOLATILE word currently_updating = 0;
2361 void async_set_pht_entry_from_index(VOLATILE page_hash_table db, int index) {
2362 unsigned int update_dummy;
2363 currently_updating = (word)(&update_dummy);
2364 set_pht_entry_from_index(db, index);
2365 /* If we get contention in the 10 or so instruction window here, */
2366 /* and we get stopped by a GC between the two updates, we lose! */
2367 if (currently_updating != (word)(&update_dummy)) {
2368 set_pht_entry_from_index_safe(db, index);
2369 /* We claim that if two threads concurrently try to update the */
2370 /* dirty bit vector, the first one to execute UPDATE_START */
2371 /* will see it changed when UPDATE_END is executed. (Note that */
2372 /* &update_dummy must differ in two distinct threads.) It */
2373 /* will then execute set_pht_entry_from_index_safe, thus */
2374 /* returning us to a safe state, though not soon enough. */
2377 #endif /* !GC_TEST_AND_SET_DEFINED */
2378 #else /* !THREADS */
2379 # define async_set_pht_entry_from_index(db, index) \
2380 set_pht_entry_from_index(db, index)
2381 #endif /* !THREADS */
2384 #if !defined(DARWIN)
2385 # if defined (SUNOS4) || (defined(FREEBSD) && !defined(SUNOS5SIGS))
2386 void GC_write_fault_handler(sig, code, scp, addr)
2388 struct sigcontext *scp;
2391 # define SIG_OK (sig == SIGSEGV || sig == SIGBUS)
2392 # define CODE_OK (FC_CODE(code) == FC_PROT \
2393 || (FC_CODE(code) == FC_OBJERR \
2394 && FC_ERRNO(code) == FC_PROT))
2397 # define SIG_OK (sig == SIGBUS)
2398 # define CODE_OK TRUE
2400 # endif /* SUNOS4 || (FREEBSD && !SUNOS5SIGS) */
2402 # if defined(IRIX5) || defined(OSF1) || defined(HURD)
2404 void GC_write_fault_handler(int sig, int code, struct sigcontext *scp)
2406 # define SIG_OK (sig == SIGSEGV)
2407 # define CODE_OK (code == 2 /* experimentally determined */)
2410 # define SIG_OK (sig == SIGSEGV)
2411 # define CODE_OK (code == EACCES)
2414 # define SIG_OK (sig == SIGBUS || sig == SIGSEGV)
2415 # define CODE_OK TRUE
2417 # endif /* IRIX5 || OSF1 || HURD */
2420 # if defined(ALPHA) || defined(M68K)
2421 void GC_write_fault_handler(int sig, int code, s_c * sc)
2423 # if defined(IA64) || defined(HP_PA) || defined(X86_64)
2424 void GC_write_fault_handler(int sig, siginfo_t * si, s_c * scp)
2427 void GC_write_fault_handler(int sig, int a2, int a3, int a4, s_c sc)
2429 void GC_write_fault_handler(int sig, s_c sc)
2433 # define SIG_OK (sig == SIGSEGV)
2434 # define CODE_OK TRUE
2435 /* Empirically c.trapno == 14, on IA32, but is that useful? */
2436 /* Should probably consider alignment issues on other */
2437 /* architectures. */
2440 # if defined(SUNOS5SIGS)
2442 void GC_write_fault_handler(int sig, SIGINFO_T *scp, void * context)
2444 void GC_write_fault_handler(sig, scp, context)
2450 # define SIG_OK (sig == SIGSEGV || sig == SIGBUS)
2451 # define CODE_OK (scp -> si_code == SEGV_ACCERR) \
2452 || (scp -> si_code == BUS_ADRERR) \
2453 || (scp -> si_code == BUS_UNKNOWN) \
2454 || (scp -> si_code == SEGV_UNKNOWN) \
2455 || (scp -> si_code == BUS_OBJERR)
2458 # define SIG_OK (sig == SIGBUS)
2459 # define CODE_OK (scp -> si_code == BUS_PAGE_FAULT)
2461 # define SIG_OK (sig == SIGSEGV)
2462 # define CODE_OK (scp -> si_code == SEGV_ACCERR)
2465 # endif /* SUNOS5SIGS */
2467 # if defined(MSWIN32) || defined(MSWINCE)
2468 LONG WINAPI GC_write_fault_handler(struct _EXCEPTION_POINTERS *exc_info)
2469 # define SIG_OK (exc_info -> ExceptionRecord -> ExceptionCode == \
2470 STATUS_ACCESS_VIOLATION)
2471 # define CODE_OK (exc_info -> ExceptionRecord -> ExceptionInformation[0] == 1)
2473 # endif /* MSWIN32 || MSWINCE */
2475 register unsigned i;
2477 char *addr = (char *) code;
2480 char * addr = (char *) (size_t) (scp -> sc_badvaddr);
2482 # if defined(OSF1) && defined(ALPHA)
2483 char * addr = (char *) (scp -> sc_traparg_a0);
2486 char * addr = (char *) (scp -> si_addr);
2490 char * addr = (char *) (sc.cr2);
2495 struct sigcontext *scp = (struct sigcontext *)(sc);
2497 int format = (scp->sc_formatvec >> 12) & 0xf;
2498 unsigned long *framedata = (unsigned long *)(scp + 1);
2501 if (format == 0xa || format == 0xb) {
2504 } else if (format == 7) {
2507 if (framedata[1] & 0x08000000) {
2508 /* correct addr on misaligned access */
2509 ea = (ea+4095)&(~4095);
2511 } else if (format == 4) {
2514 if (framedata[1] & 0x08000000) {
2515 /* correct addr on misaligned access */
2516 ea = (ea+4095)&(~4095);
2522 char * addr = get_fault_addr(sc);
2524 # if defined(IA64) || defined(HP_PA) || defined(X86_64)
2525 char * addr = si -> si_addr;
2526 /* I believe this is claimed to work on all platforms for */
2527 /* Linux 2.3.47 and later. Hopefully we don't have to */
2528 /* worry about earlier kernels on IA64. */
2530 # if defined(POWERPC)
2531 char * addr = (char *) (sc.regs->dar);
2534 char * addr = (char *)sc.fault_address;
2537 char * addr = (char *)sc.regs.csraddr;
2539 --> architecture not supported
2548 # if defined(MSWIN32) || defined(MSWINCE)
2549 char * addr = (char *) (exc_info -> ExceptionRecord
2550 -> ExceptionInformation[1]);
2551 # define sig SIGSEGV
2554 if (SIG_OK && CODE_OK) {
2555 register struct hblk * h =
2556 (struct hblk *)((word)addr & ~(GC_page_size-1));
2557 GC_bool in_allocd_block;
2560 /* Address is only within the correct physical page. */
2561 in_allocd_block = FALSE;
2562 for (i = 0; i < divHBLKSZ(GC_page_size); i++) {
2563 if (HDR(h+i) != 0) {
2564 in_allocd_block = TRUE;
2568 in_allocd_block = (HDR(addr) != 0);
2570 if (!in_allocd_block) {
2571 /* FIXME - We should make sure that we invoke the */
2572 /* old handler with the appropriate calling */
2573 /* sequence, which often depends on SA_SIGINFO. */
2575 /* Heap blocks now begin and end on page boundaries */
2578 if (sig == SIGSEGV) {
2579 old_handler = GC_old_segv_handler;
2581 old_handler = GC_old_bus_handler;
2583 if (old_handler == SIG_DFL) {
2584 # if !defined(MSWIN32) && !defined(MSWINCE)
2585 GC_err_printf1("Segfault at 0x%lx\n", addr);
2586 ABORT("Unexpected bus error or segmentation fault");
2588 return(EXCEPTION_CONTINUE_SEARCH);
2591 # if defined (SUNOS4) \
2592 || (defined(FREEBSD) && !defined(SUNOS5SIGS))
2593 (*old_handler) (sig, code, scp, addr);
2596 # if defined (SUNOS5SIGS)
2598 * FIXME: For FreeBSD, this code should check if the
2599 * old signal handler used the traditional BSD style and
2600 * if so call it using that style.
2602 (*(REAL_SIG_PF)old_handler) (sig, scp, context);
2605 # if defined (LINUX)
2606 # if defined(ALPHA) || defined(M68K)
2607 (*(REAL_SIG_PF)old_handler) (sig, code, sc);
2609 # if defined(IA64) || defined(HP_PA) || defined(X86_64)
2610 (*(REAL_SIG_PF)old_handler) (sig, si, scp);
2612 (*(REAL_SIG_PF)old_handler) (sig, sc);
2617 # if defined (IRIX5) || defined(OSF1) || defined(HURD)
2618 (*(REAL_SIG_PF)old_handler) (sig, code, scp);
2622 return((*old_handler)(exc_info));
2626 UNPROTECT(h, GC_page_size);
2627 /* We need to make sure that no collection occurs between */
2628 /* the UNPROTECT and the setting of the dirty bit. Otherwise */
2629 /* a write by a third thread might go unnoticed. Reversing */
2630 /* the order is just as bad, since we would end up unprotecting */
2631 /* a page in a GC cycle during which it's not marked. */
2632 /* Currently we do this by disabling the thread stopping */
2633 /* signals while this handler is running. An alternative might */
2634 /* be to record the fact that we're about to unprotect, or */
2635 /* have just unprotected a page in the GC's thread structure, */
2636 /* and then to have the thread stopping code set the dirty */
2637 /* flag, if necessary. */
2638 for (i = 0; i < divHBLKSZ(GC_page_size); i++) {
2639 register int index = PHT_HASH(h+i);
2641 async_set_pht_entry_from_index(GC_dirty_pages, index);
2644 /* These reset the signal handler each time by default. */
2645 signal(SIGSEGV, (SIG_PF) GC_write_fault_handler);
2647 /* The write may not take place before dirty bits are read. */
2648 /* But then we'll fault again ... */
2649 # if defined(MSWIN32) || defined(MSWINCE)
2650 return(EXCEPTION_CONTINUE_EXECUTION);
2655 #if defined(MSWIN32) || defined(MSWINCE)
2656 return EXCEPTION_CONTINUE_SEARCH;
2658 GC_err_printf1("Segfault at 0x%lx\n", addr);
2659 ABORT("Unexpected bus error or segmentation fault");
2662 #endif /* !DARWIN */
2665 * We hold the allocation lock. We expect block h to be written
2666 * shortly. Ensure that all pages containing any part of the n hblks
2667 * starting at h are no longer protected. If is_ptrfree is false,
2668 * also ensure that they will subsequently appear to be dirty.
2670 void GC_remove_protection(h, nblocks, is_ptrfree)
2675 struct hblk * h_trunc; /* Truncated to page boundary */
2676 struct hblk * h_end; /* Page boundary following block end */
2677 struct hblk * current;
2678 GC_bool found_clean;
2680 if (!GC_dirty_maintained) return;
2681 h_trunc = (struct hblk *)((word)h & ~(GC_page_size-1));
2682 h_end = (struct hblk *)(((word)(h + nblocks) + GC_page_size-1)
2683 & ~(GC_page_size-1));
2684 found_clean = FALSE;
2685 for (current = h_trunc; current < h_end; ++current) {
2686 int index = PHT_HASH(current);
2688 if (!is_ptrfree || current < h || current >= h + nblocks) {
2689 async_set_pht_entry_from_index(GC_dirty_pages, index);
2692 UNPROTECT(h_trunc, (ptr_t)h_end - (ptr_t)h_trunc);
2695 #if !defined(DARWIN)
2696 void GC_dirty_init()
2698 # if defined(SUNOS5SIGS) || defined(IRIX5) || defined(LINUX) || \
2699 defined(OSF1) || defined(HURD)
2700 struct sigaction act, oldact;
2701 /* We should probably specify SA_SIGINFO for Linux, and handle */
2702 /* the different architectures more uniformly. */
2703 # if defined(IRIX5) || defined(LINUX) && !defined(X86_64) \
2704 || defined(OSF1) || defined(HURD)
2705 act.sa_flags = SA_RESTART;
2706 act.sa_handler = (SIG_PF)GC_write_fault_handler;
2708 act.sa_flags = SA_RESTART | SA_SIGINFO;
2709 act.sa_sigaction = GC_write_fault_handler;
2711 (void)sigemptyset(&act.sa_mask);
2713 /* Arrange to postpone SIG_SUSPEND while we're in a write fault */
2714 /* handler. This effectively makes the handler atomic w.r.t. */
2715 /* stopping the world for GC. */
2716 (void)sigaddset(&act.sa_mask, SIG_SUSPEND);
2717 # endif /* SIG_SUSPEND */
2720 GC_printf0("Inititalizing mprotect virtual dirty bit implementation\n");
2722 GC_dirty_maintained = TRUE;
2723 if (GC_page_size % HBLKSIZE != 0) {
2724 GC_err_printf0("Page size not multiple of HBLKSIZE\n");
2725 ABORT("Page size not multiple of HBLKSIZE");
2727 # if defined(SUNOS4) || (defined(FREEBSD) && !defined(SUNOS5SIGS))
2728 GC_old_bus_handler = signal(SIGBUS, GC_write_fault_handler);
2729 if (GC_old_bus_handler == SIG_IGN) {
2730 GC_err_printf0("Previously ignored bus error!?");
2731 GC_old_bus_handler = SIG_DFL;
2733 if (GC_old_bus_handler != SIG_DFL) {
2735 GC_err_printf0("Replaced other SIGBUS handler\n");
2739 # if defined(SUNOS4)
2740 GC_old_segv_handler = signal(SIGSEGV, (SIG_PF)GC_write_fault_handler);
2741 if (GC_old_segv_handler == SIG_IGN) {
2742 GC_err_printf0("Previously ignored segmentation violation!?");
2743 GC_old_segv_handler = SIG_DFL;
2745 if (GC_old_segv_handler != SIG_DFL) {
2747 GC_err_printf0("Replaced other SIGSEGV handler\n");
2751 # if (defined(SUNOS5SIGS) && !defined(FREEBSD)) || defined(IRIX5) \
2752 || defined(LINUX) || defined(OSF1) || defined(HURD)
2753 /* SUNOS5SIGS includes HPUX */
2754 # if defined(GC_IRIX_THREADS)
2755 sigaction(SIGSEGV, 0, &oldact);
2756 sigaction(SIGSEGV, &act, 0);
2759 int res = sigaction(SIGSEGV, &act, &oldact);
2760 if (res != 0) ABORT("Sigaction failed");
2763 # if defined(_sigargs) || defined(HURD) || !defined(SA_SIGINFO)
2764 /* This is Irix 5.x, not 6.x. Irix 5.x does not have */
2766 GC_old_segv_handler = oldact.sa_handler;
2767 # else /* Irix 6.x or SUNOS5SIGS or LINUX */
2768 if (oldact.sa_flags & SA_SIGINFO) {
2769 GC_old_segv_handler = (SIG_PF)(oldact.sa_sigaction);
2771 GC_old_segv_handler = oldact.sa_handler;
2774 if (GC_old_segv_handler == SIG_IGN) {
2775 GC_err_printf0("Previously ignored segmentation violation!?");
2776 GC_old_segv_handler = SIG_DFL;
2778 if (GC_old_segv_handler != SIG_DFL) {
2780 GC_err_printf0("Replaced other SIGSEGV handler\n");
2783 # endif /* (SUNOS5SIGS && !FREEBSD) || IRIX5 || LINUX || OSF1 || HURD */
2784 # if defined(HPUX) || defined(LINUX) || defined(HURD) \
2785 || (defined(FREEBSD) && defined(SUNOS5SIGS))
2786 sigaction(SIGBUS, &act, &oldact);
2787 GC_old_bus_handler = oldact.sa_handler;
2788 if (GC_old_bus_handler == SIG_IGN) {
2789 GC_err_printf0("Previously ignored bus error!?");
2790 GC_old_bus_handler = SIG_DFL;
2792 if (GC_old_bus_handler != SIG_DFL) {
2794 GC_err_printf0("Replaced other SIGBUS handler\n");
2797 # endif /* HPUX || LINUX || HURD || (FREEBSD && SUNOS5SIGS) */
2798 # if defined(MSWIN32)
2799 GC_old_segv_handler = SetUnhandledExceptionFilter(GC_write_fault_handler);
2800 if (GC_old_segv_handler != NULL) {
2802 GC_err_printf0("Replaced other UnhandledExceptionFilter\n");
2805 GC_old_segv_handler = SIG_DFL;
2809 #endif /* !DARWIN */
2811 int GC_incremental_protection_needs()
2813 if (GC_page_size == HBLKSIZE) {
2814 return GC_PROTECTS_POINTER_HEAP;
2816 return GC_PROTECTS_POINTER_HEAP | GC_PROTECTS_PTRFREE_HEAP;
2820 #define HAVE_INCREMENTAL_PROTECTION_NEEDS
2822 #define IS_PTRFREE(hhdr) ((hhdr)->hb_descr == 0)
2824 #define PAGE_ALIGNED(x) !((word)(x) & (GC_page_size - 1))
2825 void GC_protect_heap()
2829 struct hblk * current;
2830 struct hblk * current_start; /* Start of block to be protected. */
2831 struct hblk * limit;
2833 GC_bool protect_all =
2834 (0 != (GC_incremental_protection_needs() & GC_PROTECTS_PTRFREE_HEAP));
2835 for (i = 0; i < GC_n_heap_sects; i++) {
2836 start = GC_heap_sects[i].hs_start;
2837 len = GC_heap_sects[i].hs_bytes;
2839 PROTECT(start, len);
2841 GC_ASSERT(PAGE_ALIGNED(len))
2842 GC_ASSERT(PAGE_ALIGNED(start))
2843 current_start = current = (struct hblk *)start;
2844 limit = (struct hblk *)(start + len);
2845 while (current < limit) {
2850 GC_ASSERT(PAGE_ALIGNED(current));
2851 GET_HDR(current, hhdr);
2852 if (IS_FORWARDING_ADDR_OR_NIL(hhdr)) {
2853 /* This can happen only if we're at the beginning of a */
2854 /* heap segment, and a block spans heap segments. */
2855 /* We will handle that block as part of the preceding */
2857 GC_ASSERT(current_start == current);
2858 current_start = ++current;
2861 if (HBLK_IS_FREE(hhdr)) {
2862 GC_ASSERT(PAGE_ALIGNED(hhdr -> hb_sz));
2863 nhblks = divHBLKSZ(hhdr -> hb_sz);
2864 is_ptrfree = TRUE; /* dirty on alloc */
2866 nhblks = OBJ_SZ_TO_BLOCKS(hhdr -> hb_sz);
2867 is_ptrfree = IS_PTRFREE(hhdr);
2870 if (current_start < current) {
2871 PROTECT(current_start, (ptr_t)current - (ptr_t)current_start);
2873 current_start = (current += nhblks);
2878 if (current_start < current) {
2879 PROTECT(current_start, (ptr_t)current - (ptr_t)current_start);
2885 /* We assume that either the world is stopped or its OK to lose dirty */
2886 /* bits while this is happenning (as in GC_enable_incremental). */
2887 void GC_read_dirty()
2889 BCOPY((word *)GC_dirty_pages, GC_grungy_pages,
2890 (sizeof GC_dirty_pages));
2891 BZERO((word *)GC_dirty_pages, (sizeof GC_dirty_pages));
2895 GC_bool GC_page_was_dirty(h)
2898 register word index = PHT_HASH(h);
2900 return(HDR(h) == 0 || get_pht_entry_from_index(GC_grungy_pages, index));
2904 * Acquiring the allocation lock here is dangerous, since this
2905 * can be called from within GC_call_with_alloc_lock, and the cord
2906 * package does so. On systems that allow nested lock acquisition, this
2908 * On other systems, SET_LOCK_HOLDER and friends must be suitably defined.
2911 static GC_bool syscall_acquired_lock = FALSE; /* Protected by GC lock. */
2913 void GC_begin_syscall()
2915 if (!I_HOLD_LOCK()) {
2917 syscall_acquired_lock = TRUE;
2921 void GC_end_syscall()
2923 if (syscall_acquired_lock) {
2924 syscall_acquired_lock = FALSE;
2929 void GC_unprotect_range(addr, len)
2933 struct hblk * start_block;
2934 struct hblk * end_block;
2935 register struct hblk *h;
2938 if (!GC_dirty_maintained) return;
2939 obj_start = GC_base(addr);
2940 if (obj_start == 0) return;
2941 if (GC_base(addr + len - 1) != obj_start) {
2942 ABORT("GC_unprotect_range(range bigger than object)");
2944 start_block = (struct hblk *)((word)addr & ~(GC_page_size - 1));
2945 end_block = (struct hblk *)((word)(addr + len - 1) & ~(GC_page_size - 1));
2946 end_block += GC_page_size/HBLKSIZE - 1;
2947 for (h = start_block; h <= end_block; h++) {
2948 register word index = PHT_HASH(h);
2950 async_set_pht_entry_from_index(GC_dirty_pages, index);
2952 UNPROTECT(start_block,
2953 ((ptr_t)end_block - (ptr_t)start_block) + HBLKSIZE);
2958 /* We no longer wrap read by default, since that was causing too many */
2959 /* problems. It is preferred that the client instead avoids writing */
2960 /* to the write-protected heap with a system call. */
2961 /* This still serves as sample code if you do want to wrap system calls.*/
2963 #if !defined(MSWIN32) && !defined(MSWINCE) && !defined(GC_USE_LD_WRAP)
2964 /* Replacement for UNIX system call. */
2965 /* Other calls that write to the heap should be handled similarly. */
2966 /* Note that this doesn't work well for blocking reads: It will hold */
2967 /* the allocation lock for the entire duration of the call. Multithreaded */
2968 /* clients should really ensure that it won't block, either by setting */
2969 /* the descriptor nonblocking, or by calling select or poll first, to */
2970 /* make sure that input is available. */
2971 /* Another, preferred alternative is to ensure that system calls never */
2972 /* write to the protected heap (see above). */
2973 # if defined(__STDC__) && !defined(SUNOS4)
2974 # include <unistd.h>
2975 # include <sys/uio.h>
2976 ssize_t read(int fd, void *buf, size_t nbyte)
2979 int read(fd, buf, nbyte)
2981 int GC_read(fd, buf, nbyte)
2991 GC_unprotect_range(buf, (word)nbyte);
2992 # if defined(IRIX5) || defined(GC_LINUX_THREADS)
2993 /* Indirect system call may not always be easily available. */
2994 /* We could call _read, but that would interfere with the */
2995 /* libpthread interception of read. */
2996 /* On Linux, we have to be careful with the linuxthreads */
2997 /* read interception. */
3002 iov.iov_len = nbyte;
3003 result = readv(fd, &iov, 1);
3007 result = __read(fd, buf, nbyte);
3009 /* The two zero args at the end of this list are because one
3010 IA-64 syscall() implementation actually requires six args
3011 to be passed, even though they aren't always used. */
3012 result = syscall(SYS_read, fd, buf, nbyte, 0, 0);
3018 #endif /* !MSWIN32 && !MSWINCE && !GC_LINUX_THREADS */
3020 #if defined(GC_USE_LD_WRAP) && !defined(THREADS)
3021 /* We use the GNU ld call wrapping facility. */
3022 /* This requires that the linker be invoked with "--wrap read". */
3023 /* This can be done by passing -Wl,"--wrap read" to gcc. */
3024 /* I'm not sure that this actually wraps whatever version of read */
3025 /* is called by stdio. That code also mentions __read. */
3026 # include <unistd.h>
3027 ssize_t __wrap_read(int fd, void *buf, size_t nbyte)
3032 GC_unprotect_range(buf, (word)nbyte);
3033 result = __real_read(fd, buf, nbyte);
3038 /* We should probably also do this for __read, or whatever stdio */
3039 /* actually calls. */
3045 GC_bool GC_page_was_ever_dirty(h)
3051 /* Reset the n pages starting at h to "was never dirty" status. */
3053 void GC_is_fresh(h, n)
3059 # endif /* MPROTECT_VDB */
3064 * See DEFAULT_VDB for interface descriptions.
3068 * This implementaion assumes a Solaris 2.X like /proc pseudo-file-system
3069 * from which we can read page modified bits. This facility is far from
3070 * optimal (e.g. we would like to get the info for only some of the
3071 * address space), but it avoids intercepting system calls.
3075 #include <sys/types.h>
3076 #include <sys/signal.h>
3077 #include <sys/fault.h>
3078 #include <sys/syscall.h>
3079 #include <sys/procfs.h>
3080 #include <sys/stat.h>
3082 #define INITIAL_BUF_SZ 16384
3083 word GC_proc_buf_size = INITIAL_BUF_SZ;
3086 #ifdef GC_SOLARIS_THREADS
3087 /* We don't have exact sp values for threads. So we count on */
3088 /* occasionally declaring stack pages to be fresh. Thus we */
3089 /* need a real implementation of GC_is_fresh. We can't clear */
3090 /* entries in GC_written_pages, since that would declare all */
3091 /* pages with the given hash address to be fresh. */
3092 # define MAX_FRESH_PAGES 8*1024 /* Must be power of 2 */
3093 struct hblk ** GC_fresh_pages; /* A direct mapped cache. */
3094 /* Collisions are dropped. */
3096 # define FRESH_PAGE_SLOT(h) (divHBLKSZ((word)(h)) & (MAX_FRESH_PAGES-1))
3097 # define ADD_FRESH_PAGE(h) \
3098 GC_fresh_pages[FRESH_PAGE_SLOT(h)] = (h)
3099 # define PAGE_IS_FRESH(h) \
3100 (GC_fresh_pages[FRESH_PAGE_SLOT(h)] == (h) && (h) != 0)
3103 /* Add all pages in pht2 to pht1 */
3104 void GC_or_pages(pht1, pht2)
3105 page_hash_table pht1, pht2;
3109 for (i = 0; i < PHT_SIZE; i++) pht1[i] |= pht2[i];
3114 void GC_dirty_init()
3119 GC_dirty_maintained = TRUE;
3120 if (GC_words_allocd != 0 || GC_words_allocd_before_gc != 0) {
3123 for (i = 0; i < PHT_SIZE; i++) GC_written_pages[i] = (word)(-1);
3125 GC_printf1("Allocated words:%lu:all pages may have been written\n",
3127 (GC_words_allocd + GC_words_allocd_before_gc));
3130 sprintf(buf, "/proc/%d", getpid());
3131 fd = open(buf, O_RDONLY);
3133 ABORT("/proc open failed");
3135 GC_proc_fd = syscall(SYS_ioctl, fd, PIOCOPENPD, 0);
3137 syscall(SYS_fcntl, GC_proc_fd, F_SETFD, FD_CLOEXEC);
3138 if (GC_proc_fd < 0) {
3139 ABORT("/proc ioctl failed");
3141 GC_proc_buf = GC_scratch_alloc(GC_proc_buf_size);
3142 # ifdef GC_SOLARIS_THREADS
3143 GC_fresh_pages = (struct hblk **)
3144 GC_scratch_alloc(MAX_FRESH_PAGES * sizeof (struct hblk *));
3145 if (GC_fresh_pages == 0) {
3146 GC_err_printf0("No space for fresh pages\n");
3149 BZERO(GC_fresh_pages, MAX_FRESH_PAGES * sizeof (struct hblk *));
3153 /* Ignore write hints. They don't help us here. */
3155 void GC_remove_protection(h, nblocks, is_ptrfree)
3162 #ifdef GC_SOLARIS_THREADS
3163 # define READ(fd,buf,nbytes) syscall(SYS_read, fd, buf, nbytes)
3165 # define READ(fd,buf,nbytes) read(fd, buf, nbytes)
3168 void GC_read_dirty()
3170 unsigned long ps, np;
3173 struct prasmap * map;
3175 ptr_t current_addr, limit;
3179 BZERO(GC_grungy_pages, (sizeof GC_grungy_pages));
3182 if (READ(GC_proc_fd, bufp, GC_proc_buf_size) <= 0) {
3184 GC_printf1("/proc read failed: GC_proc_buf_size = %lu\n",
3188 /* Retry with larger buffer. */
3189 word new_size = 2 * GC_proc_buf_size;
3190 char * new_buf = GC_scratch_alloc(new_size);
3193 GC_proc_buf = bufp = new_buf;
3194 GC_proc_buf_size = new_size;
3196 if (READ(GC_proc_fd, bufp, GC_proc_buf_size) <= 0) {
3197 WARN("Insufficient space for /proc read\n", 0);
3199 memset(GC_grungy_pages, 0xff, sizeof (page_hash_table));
3200 memset(GC_written_pages, 0xff, sizeof(page_hash_table));
3201 # ifdef GC_SOLARIS_THREADS
3202 BZERO(GC_fresh_pages,
3203 MAX_FRESH_PAGES * sizeof (struct hblk *));
3209 /* Copy dirty bits into GC_grungy_pages */
3210 nmaps = ((struct prpageheader *)bufp) -> pr_nmap;
3211 /* printf( "nmaps = %d, PG_REFERENCED = %d, PG_MODIFIED = %d\n",
3212 nmaps, PG_REFERENCED, PG_MODIFIED); */
3213 bufp = bufp + sizeof(struct prpageheader);
3214 for (i = 0; i < nmaps; i++) {
3215 map = (struct prasmap *)bufp;
3216 vaddr = (ptr_t)(map -> pr_vaddr);
3217 ps = map -> pr_pagesize;
3218 np = map -> pr_npage;
3219 /* printf("vaddr = 0x%X, ps = 0x%X, np = 0x%X\n", vaddr, ps, np); */
3220 limit = vaddr + ps * np;
3221 bufp += sizeof (struct prasmap);
3222 for (current_addr = vaddr;
3223 current_addr < limit; current_addr += ps){
3224 if ((*bufp++) & PG_MODIFIED) {
3225 register struct hblk * h = (struct hblk *) current_addr;
3227 while ((ptr_t)h < current_addr + ps) {
3228 register word index = PHT_HASH(h);
3230 set_pht_entry_from_index(GC_grungy_pages, index);
3231 # ifdef GC_SOLARIS_THREADS
3233 register int slot = FRESH_PAGE_SLOT(h);
3235 if (GC_fresh_pages[slot] == h) {
3236 GC_fresh_pages[slot] = 0;
3244 bufp += sizeof(long) - 1;
3245 bufp = (char *)((unsigned long)bufp & ~(sizeof(long)-1));
3247 /* Update GC_written_pages. */
3248 GC_or_pages(GC_written_pages, GC_grungy_pages);
3249 # ifdef GC_SOLARIS_THREADS
3250 /* Make sure that old stacks are considered completely clean */
3251 /* unless written again. */
3252 GC_old_stacks_are_fresh();
3258 GC_bool GC_page_was_dirty(h)
3261 register word index = PHT_HASH(h);
3262 register GC_bool result;
3264 result = get_pht_entry_from_index(GC_grungy_pages, index);
3265 # ifdef GC_SOLARIS_THREADS
3266 if (result && PAGE_IS_FRESH(h)) result = FALSE;
3267 /* This happens only if page was declared fresh since */
3268 /* the read_dirty call, e.g. because it's in an unused */
3269 /* thread stack. It's OK to treat it as clean, in */
3270 /* that case. And it's consistent with */
3271 /* GC_page_was_ever_dirty. */
3276 GC_bool GC_page_was_ever_dirty(h)
3279 register word index = PHT_HASH(h);
3280 register GC_bool result;
3282 result = get_pht_entry_from_index(GC_written_pages, index);
3283 # ifdef GC_SOLARIS_THREADS
3284 if (result && PAGE_IS_FRESH(h)) result = FALSE;
3289 /* Caller holds allocation lock. */
3290 void GC_is_fresh(h, n)
3295 register word index;
3297 # ifdef GC_SOLARIS_THREADS
3300 if (GC_fresh_pages != 0) {
3301 for (i = 0; i < n; i++) {
3302 ADD_FRESH_PAGE(h + i);
3308 # endif /* PROC_VDB */
3313 # include "vd/PCR_VD.h"
3315 # define NPAGES (32*1024) /* 128 MB */
3317 PCR_VD_DB GC_grungy_bits[NPAGES];
3319 ptr_t GC_vd_base; /* Address corresponding to GC_grungy_bits[0] */
3320 /* HBLKSIZE aligned. */
3322 void GC_dirty_init()
3324 GC_dirty_maintained = TRUE;
3325 /* For the time being, we assume the heap generally grows up */
3326 GC_vd_base = GC_heap_sects[0].hs_start;
3327 if (GC_vd_base == 0) {
3328 ABORT("Bad initial heap segment");
3330 if (PCR_VD_Start(HBLKSIZE, GC_vd_base, NPAGES*HBLKSIZE)
3332 ABORT("dirty bit initialization failed");
3336 void GC_read_dirty()
3338 /* lazily enable dirty bits on newly added heap sects */
3340 static int onhs = 0;
3341 int nhs = GC_n_heap_sects;
3342 for( ; onhs < nhs; onhs++ ) {
3343 PCR_VD_WriteProtectEnable(
3344 GC_heap_sects[onhs].hs_start,
3345 GC_heap_sects[onhs].hs_bytes );
3350 if (PCR_VD_Clear(GC_vd_base, NPAGES*HBLKSIZE, GC_grungy_bits)
3352 ABORT("dirty bit read failed");
3356 GC_bool GC_page_was_dirty(h)
3359 if((ptr_t)h < GC_vd_base || (ptr_t)h >= GC_vd_base + NPAGES*HBLKSIZE) {
3362 return(GC_grungy_bits[h - (struct hblk *)GC_vd_base] & PCR_VD_DB_dirtyBit);
3366 void GC_remove_protection(h, nblocks, is_ptrfree)
3371 PCR_VD_WriteProtectDisable(h, nblocks*HBLKSIZE);
3372 PCR_VD_WriteProtectEnable(h, nblocks*HBLKSIZE);
3375 # endif /* PCR_VDB */
3377 #if defined(MPROTECT_VDB) && defined(DARWIN)
3378 /* The following sources were used as a *reference* for this exception handling
3380 1. Apple's mach/xnu documentation
3381 2. Timothy J. Wood's "Mach Exception Handlers 101" post to the
3382 omnigroup's macosx-dev list.
3383 www.omnigroup.com/mailman/archive/macosx-dev/2000-June/002030.html
3384 3. macosx-nat.c from Apple's GDB source code.
3387 /* The bug that caused all this trouble should now be fixed. This should
3388 eventually be removed if all goes well. */
3389 /* define BROKEN_EXCEPTION_HANDLING */
3391 #include <mach/mach.h>
3392 #include <mach/mach_error.h>
3393 #include <mach/thread_status.h>
3394 #include <mach/exception.h>
3395 #include <mach/task.h>
3396 #include <pthread.h>
3398 /* These are not defined in any header, although they are documented */
3399 extern boolean_t exc_server(mach_msg_header_t *,mach_msg_header_t *);
3400 extern kern_return_t exception_raise(
3401 mach_port_t,mach_port_t,mach_port_t,
3402 exception_type_t,exception_data_t,mach_msg_type_number_t);
3403 extern kern_return_t exception_raise_state(
3404 mach_port_t,mach_port_t,mach_port_t,
3405 exception_type_t,exception_data_t,mach_msg_type_number_t,
3406 thread_state_flavor_t*,thread_state_t,mach_msg_type_number_t,
3407 thread_state_t,mach_msg_type_number_t*);
3408 extern kern_return_t exception_raise_state_identity(
3409 mach_port_t,mach_port_t,mach_port_t,
3410 exception_type_t,exception_data_t,mach_msg_type_number_t,
3411 thread_state_flavor_t*,thread_state_t,mach_msg_type_number_t,
3412 thread_state_t,mach_msg_type_number_t*);
3415 #define MAX_EXCEPTION_PORTS 16
3418 mach_msg_type_number_t count;
3419 exception_mask_t masks[MAX_EXCEPTION_PORTS];
3420 exception_handler_t ports[MAX_EXCEPTION_PORTS];
3421 exception_behavior_t behaviors[MAX_EXCEPTION_PORTS];
3422 thread_state_flavor_t flavors[MAX_EXCEPTION_PORTS];
3426 mach_port_t exception;
3427 #if defined(THREADS)
3433 mach_msg_header_t head;
3437 GC_MP_NORMAL, GC_MP_DISCARDING, GC_MP_STOPPED
3438 } GC_mprotect_state_t;
3440 /* FIXME: 1 and 2 seem to be safe to use in the msgh_id field,
3441 but it isn't documented. Use the source and see if they
3446 /* These values are only used on the reply port */
3449 #if defined(THREADS)
3451 GC_mprotect_state_t GC_mprotect_state;
3453 /* The following should ONLY be called when the world is stopped */
3454 static void GC_mprotect_thread_notify(mach_msg_id_t id) {
3457 mach_msg_trailer_t trailer;
3459 mach_msg_return_t r;
3461 buf.msg.head.msgh_bits =
3462 MACH_MSGH_BITS(MACH_MSG_TYPE_MAKE_SEND,0);
3463 buf.msg.head.msgh_size = sizeof(buf.msg);
3464 buf.msg.head.msgh_remote_port = GC_ports.exception;
3465 buf.msg.head.msgh_local_port = MACH_PORT_NULL;
3466 buf.msg.head.msgh_id = id;
3470 MACH_SEND_MSG|MACH_RCV_MSG|MACH_RCV_LARGE,
3474 MACH_MSG_TIMEOUT_NONE,
3476 if(r != MACH_MSG_SUCCESS)
3477 ABORT("mach_msg failed in GC_mprotect_thread_notify");
3478 if(buf.msg.head.msgh_id != ID_ACK)
3479 ABORT("invalid ack in GC_mprotect_thread_notify");
3482 /* Should only be called by the mprotect thread */
3483 static void GC_mprotect_thread_reply() {
3485 mach_msg_return_t r;
3487 msg.head.msgh_bits =
3488 MACH_MSGH_BITS(MACH_MSG_TYPE_MAKE_SEND,0);
3489 msg.head.msgh_size = sizeof(msg);
3490 msg.head.msgh_remote_port = GC_ports.reply;
3491 msg.head.msgh_local_port = MACH_PORT_NULL;
3492 msg.head.msgh_id = ID_ACK;
3500 MACH_MSG_TIMEOUT_NONE,
3502 if(r != MACH_MSG_SUCCESS)
3503 ABORT("mach_msg failed in GC_mprotect_thread_reply");
3506 void GC_mprotect_stop() {
3507 GC_mprotect_thread_notify(ID_STOP);
3509 void GC_mprotect_resume() {
3510 GC_mprotect_thread_notify(ID_RESUME);
3513 #else /* !THREADS */
3514 /* The compiler should optimize away any GC_mprotect_state computations */
3515 #define GC_mprotect_state GC_MP_NORMAL
3518 static void *GC_mprotect_thread(void *arg) {
3519 mach_msg_return_t r;
3520 /* These two structures contain some private kernel data. We don't need to
3521 access any of it so we don't bother defining a proper struct. The
3522 correct definitions are in the xnu source code. */
3524 mach_msg_header_t head;
3528 mach_msg_header_t head;
3529 mach_msg_body_t msgh_body;
3535 GC_darwin_register_mach_handler_thread(mach_thread_self());
3540 MACH_RCV_MSG|MACH_RCV_LARGE|
3541 (GC_mprotect_state == GC_MP_DISCARDING ? MACH_RCV_TIMEOUT : 0),
3545 GC_mprotect_state == GC_MP_DISCARDING ? 0 : MACH_MSG_TIMEOUT_NONE,
3548 id = r == MACH_MSG_SUCCESS ? msg.head.msgh_id : -1;
3550 #if defined(THREADS)
3551 if(GC_mprotect_state == GC_MP_DISCARDING) {
3552 if(r == MACH_RCV_TIMED_OUT) {
3553 GC_mprotect_state = GC_MP_STOPPED;
3554 GC_mprotect_thread_reply();
3557 if(r == MACH_MSG_SUCCESS && (id == ID_STOP || id == ID_RESUME))
3558 ABORT("out of order mprotect thread request");
3562 if(r != MACH_MSG_SUCCESS) {
3563 GC_err_printf2("mach_msg failed with %d %s\n",
3564 (int)r,mach_error_string(r));
3565 ABORT("mach_msg failed");
3569 #if defined(THREADS)
3571 if(GC_mprotect_state != GC_MP_NORMAL)
3572 ABORT("Called mprotect_stop when state wasn't normal");
3573 GC_mprotect_state = GC_MP_DISCARDING;
3576 if(GC_mprotect_state != GC_MP_STOPPED)
3577 ABORT("Called mprotect_resume when state wasn't stopped");
3578 GC_mprotect_state = GC_MP_NORMAL;
3579 GC_mprotect_thread_reply();
3581 #endif /* THREADS */
3583 /* Handle the message (calls catch_exception_raise) */
3584 if(!exc_server(&msg.head,&reply.head))
3585 ABORT("exc_server failed");
3586 /* Send the reply */
3590 reply.head.msgh_size,
3593 MACH_MSG_TIMEOUT_NONE,
3595 if(r != MACH_MSG_SUCCESS) {
3596 /* This will fail if the thread dies, but the thread shouldn't
3598 #ifdef BROKEN_EXCEPTION_HANDLING
3600 "mach_msg failed with %d %s while sending exc reply\n",
3601 (int)r,mach_error_string(r));
3603 ABORT("mach_msg failed while sending exception reply");
3612 /* All this SIGBUS code shouldn't be necessary. All protection faults should
3613 be going throught the mach exception handler. However, it seems a SIGBUS is
3614 occasionally sent for some unknown reason. Even more odd, it seems to be
3615 meaningless and safe to ignore. */
3616 #ifdef BROKEN_EXCEPTION_HANDLING
3618 typedef void (* SIG_PF)();
3619 static SIG_PF GC_old_bus_handler;
3621 /* Updates to this aren't atomic, but the SIGBUSs seem pretty rare.
3622 Even if this doesn't get updated property, it isn't really a problem */
3623 static int GC_sigbus_count;
3625 static void GC_darwin_sigbus(int num,siginfo_t *sip,void *context) {
3626 if(num != SIGBUS) ABORT("Got a non-sigbus signal in the sigbus handler");
3628 /* Ugh... some seem safe to ignore, but too many in a row probably means
3629 trouble. GC_sigbus_count is reset for each mach exception that is
3631 if(GC_sigbus_count >= 8) {
3632 ABORT("Got more than 8 SIGBUSs in a row!");
3635 GC_err_printf0("GC: WARNING: Ignoring SIGBUS.\n");
3638 #endif /* BROKEN_EXCEPTION_HANDLING */
3640 void GC_dirty_init() {
3644 pthread_attr_t attr;
3645 exception_mask_t mask;
3648 GC_printf0("Inititalizing mach/darwin mprotect virtual dirty bit "
3649 "implementation\n");
3651 # ifdef BROKEN_EXCEPTION_HANDLING
3652 GC_err_printf0("GC: WARNING: Enabling workarounds for various darwin "
3653 "exception handling bugs.\n");
3655 GC_dirty_maintained = TRUE;
3656 if (GC_page_size % HBLKSIZE != 0) {
3657 GC_err_printf0("Page size not multiple of HBLKSIZE\n");
3658 ABORT("Page size not multiple of HBLKSIZE");
3661 GC_task_self = me = mach_task_self();
3663 r = mach_port_allocate(me,MACH_PORT_RIGHT_RECEIVE,&GC_ports.exception);
3664 if(r != KERN_SUCCESS) ABORT("mach_port_allocate failed (exception port)");
3666 r = mach_port_insert_right(me,GC_ports.exception,GC_ports.exception,
3667 MACH_MSG_TYPE_MAKE_SEND);
3668 if(r != KERN_SUCCESS)
3669 ABORT("mach_port_insert_right failed (exception port)");
3671 #if defined(THREADS)
3672 r = mach_port_allocate(me,MACH_PORT_RIGHT_RECEIVE,&GC_ports.reply);
3673 if(r != KERN_SUCCESS) ABORT("mach_port_allocate failed (reply port)");
3676 /* The exceptions we want to catch */
3677 mask = EXC_MASK_BAD_ACCESS;
3679 r = task_get_exception_ports(
3682 GC_old_exc_ports.masks,
3683 &GC_old_exc_ports.count,
3684 GC_old_exc_ports.ports,
3685 GC_old_exc_ports.behaviors,
3686 GC_old_exc_ports.flavors
3688 if(r != KERN_SUCCESS) ABORT("task_get_exception_ports failed");
3690 r = task_set_exception_ports(
3695 MACHINE_THREAD_STATE
3697 if(r != KERN_SUCCESS) ABORT("task_set_exception_ports failed");
3699 if(pthread_attr_init(&attr) != 0) ABORT("pthread_attr_init failed");
3700 if(pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED) != 0)
3701 ABORT("pthread_attr_setdetachedstate failed");
3703 # undef pthread_create
3704 /* This will call the real pthread function, not our wrapper */
3705 if(pthread_create(&thread,&attr,GC_mprotect_thread,NULL) != 0)
3706 ABORT("pthread_create failed");
3707 pthread_attr_destroy(&attr);
3709 /* Setup the sigbus handler for ignoring the meaningless SIGBUSs */
3710 #ifdef BROKEN_EXCEPTION_HANDLING
3712 struct sigaction sa, oldsa;
3713 sa.sa_handler = (SIG_PF)GC_darwin_sigbus;
3714 sigemptyset(&sa.sa_mask);
3715 sa.sa_flags = SA_RESTART|SA_SIGINFO;
3716 if(sigaction(SIGBUS,&sa,&oldsa) < 0) ABORT("sigaction");
3717 GC_old_bus_handler = (SIG_PF)oldsa.sa_handler;
3718 if (GC_old_bus_handler != SIG_DFL) {
3720 GC_err_printf0("Replaced other SIGBUS handler\n");
3724 #endif /* BROKEN_EXCEPTION_HANDLING */
3727 /* The source code for Apple's GDB was used as a reference for the exception
3728 forwarding code. This code is similar to be GDB code only because there is
3729 only one way to do it. */
3730 static kern_return_t GC_forward_exception(
3733 exception_type_t exception,
3734 exception_data_t data,
3735 mach_msg_type_number_t data_count
3740 exception_behavior_t behavior;
3741 thread_state_flavor_t flavor;
3743 thread_state_t thread_state;
3744 mach_msg_type_number_t thread_state_count = THREAD_STATE_MAX;
3746 for(i=0;i<GC_old_exc_ports.count;i++)
3747 if(GC_old_exc_ports.masks[i] & (1 << exception))
3749 if(i==GC_old_exc_ports.count) ABORT("No handler for exception!");
3751 port = GC_old_exc_ports.ports[i];
3752 behavior = GC_old_exc_ports.behaviors[i];
3753 flavor = GC_old_exc_ports.flavors[i];
3755 if(behavior != EXCEPTION_DEFAULT) {
3756 r = thread_get_state(thread,flavor,thread_state,&thread_state_count);
3757 if(r != KERN_SUCCESS)
3758 ABORT("thread_get_state failed in forward_exception");
3762 case EXCEPTION_DEFAULT:
3763 r = exception_raise(port,thread,task,exception,data,data_count);
3765 case EXCEPTION_STATE:
3766 r = exception_raise_state(port,thread,task,exception,data,
3767 data_count,&flavor,thread_state,thread_state_count,
3768 thread_state,&thread_state_count);
3770 case EXCEPTION_STATE_IDENTITY:
3771 r = exception_raise_state_identity(port,thread,task,exception,data,
3772 data_count,&flavor,thread_state,thread_state_count,
3773 thread_state,&thread_state_count);
3776 r = KERN_FAILURE; /* make gcc happy */
3777 ABORT("forward_exception: unknown behavior");
3781 if(behavior != EXCEPTION_DEFAULT) {
3782 r = thread_set_state(thread,flavor,thread_state,thread_state_count);
3783 if(r != KERN_SUCCESS)
3784 ABORT("thread_set_state failed in forward_exception");
3790 #define FWD() GC_forward_exception(thread,task,exception,code,code_count)
3792 /* This violates the namespace rules but there isn't anything that can be done
3793 about it. The exception handling stuff is hard coded to call this */
3795 catch_exception_raise(
3796 mach_port_t exception_port,mach_port_t thread,mach_port_t task,
3797 exception_type_t exception,exception_data_t code,
3798 mach_msg_type_number_t code_count
3804 # if defined(POWERPC)
3805 # if CPP_WORDSZ == 32
3806 thread_state_flavor_t flavor = PPC_EXCEPTION_STATE;
3807 mach_msg_type_number_t exc_state_count = PPC_EXCEPTION_STATE_COUNT;
3808 ppc_exception_state_t exc_state;
3810 thread_state_flavor_t flavor = PPC_EXCEPTION_STATE64;
3811 mach_msg_type_number_t exc_state_count = PPC_EXCEPTION_STATE64_COUNT;
3812 ppc_exception_state64_t exc_state;
3814 # elif defined(I386)
3815 thread_state_flavor_t flavor = i386_EXCEPTION_STATE;
3816 mach_msg_type_number_t exc_state_count = i386_EXCEPTION_STATE_COUNT;
3817 i386_exception_state_t exc_state;
3819 # error FIXME for non-ppc/x86 darwin
3823 if(exception != EXC_BAD_ACCESS || code[0] != KERN_PROTECTION_FAILURE) {
3824 #ifdef DEBUG_EXCEPTION_HANDLING
3825 /* We aren't interested, pass it on to the old handler */
3826 GC_printf3("Exception: 0x%x Code: 0x%x 0x%x in catch....\n",
3828 code_count > 0 ? code[0] : -1,
3829 code_count > 1 ? code[1] : -1);
3834 r = thread_get_state(thread,flavor,
3835 (natural_t*)&exc_state,&exc_state_count);
3836 if(r != KERN_SUCCESS) {
3837 /* The thread is supposed to be suspended while the exception handler
3838 is called. This shouldn't fail. */
3839 #ifdef BROKEN_EXCEPTION_HANDLING
3840 GC_err_printf0("thread_get_state failed in "
3841 "catch_exception_raise\n");
3842 return KERN_SUCCESS;
3844 ABORT("thread_get_state failed in catch_exception_raise");
3848 /* This is the address that caused the fault */
3849 #if defined(POWERPC)
3850 addr = (char*) exc_state.dar;
3851 #elif defined (I386)
3852 addr = (char*) exc_state.faultvaddr;
3854 # error FIXME for non POWERPC/I386
3857 if((HDR(addr)) == 0) {
3858 /* Ugh... just like the SIGBUS problem above, it seems we get a bogus
3859 KERN_PROTECTION_FAILURE every once and a while. We wait till we get
3860 a bunch in a row before doing anything about it. If a "real" fault
3861 ever occurres it'll just keep faulting over and over and we'll hit
3862 the limit pretty quickly. */
3863 #ifdef BROKEN_EXCEPTION_HANDLING
3864 static char *last_fault;
3865 static int last_fault_count;
3867 if(addr != last_fault) {
3869 last_fault_count = 0;
3871 if(++last_fault_count < 32) {
3872 if(last_fault_count == 1)
3874 "GC: WARNING: Ignoring KERN_PROTECTION_FAILURE at %p\n",
3876 return KERN_SUCCESS;
3879 GC_err_printf1("Unexpected KERN_PROTECTION_FAILURE at %p\n",addr);
3880 /* Can't pass it along to the signal handler because that is
3881 ignoring SIGBUS signals. We also shouldn't call ABORT here as
3882 signals don't always work too well from the exception handler. */
3883 GC_err_printf0("Aborting\n");
3885 #else /* BROKEN_EXCEPTION_HANDLING */
3886 /* Pass it along to the next exception handler
3887 (which should call SIGBUS/SIGSEGV) */
3889 #endif /* !BROKEN_EXCEPTION_HANDLING */
3892 #ifdef BROKEN_EXCEPTION_HANDLING
3893 /* Reset the number of consecutive SIGBUSs */
3894 GC_sigbus_count = 0;
3897 if(GC_mprotect_state == GC_MP_NORMAL) { /* common case */
3898 h = (struct hblk*)((word)addr & ~(GC_page_size-1));
3899 UNPROTECT(h, GC_page_size);
3900 for (i = 0; i < divHBLKSZ(GC_page_size); i++) {
3901 register int index = PHT_HASH(h+i);
3902 async_set_pht_entry_from_index(GC_dirty_pages, index);
3904 } else if(GC_mprotect_state == GC_MP_DISCARDING) {
3905 /* Lie to the thread for now. No sense UNPROTECT()ing the memory
3906 when we're just going to PROTECT() it again later. The thread
3907 will just fault again once it resumes */
3909 /* Shouldn't happen, i don't think */
3910 GC_printf0("KERN_PROTECTION_FAILURE while world is stopped\n");
3913 return KERN_SUCCESS;
3917 /* These should never be called, but just in case... */
3918 kern_return_t catch_exception_raise_state(mach_port_name_t exception_port,
3919 int exception, exception_data_t code, mach_msg_type_number_t codeCnt,
3920 int flavor, thread_state_t old_state, int old_stateCnt,
3921 thread_state_t new_state, int new_stateCnt)
3923 ABORT("catch_exception_raise_state");
3924 return(KERN_INVALID_ARGUMENT);
3926 kern_return_t catch_exception_raise_state_identity(
3927 mach_port_name_t exception_port, mach_port_t thread, mach_port_t task,
3928 int exception, exception_data_t code, mach_msg_type_number_t codeCnt,
3929 int flavor, thread_state_t old_state, int old_stateCnt,
3930 thread_state_t new_state, int new_stateCnt)
3932 ABORT("catch_exception_raise_state_identity");
3933 return(KERN_INVALID_ARGUMENT);
3937 #endif /* DARWIN && MPROTECT_VDB */
3939 # ifndef HAVE_INCREMENTAL_PROTECTION_NEEDS
3940 int GC_incremental_protection_needs()
3942 return GC_PROTECTS_NONE;
3944 # endif /* !HAVE_INCREMENTAL_PROTECTION_NEEDS */
3947 * Call stack save code for debugging.
3948 * Should probably be in mach_dep.c, but that requires reorganization.
3951 /* I suspect the following works for most X86 *nix variants, so */
3952 /* long as the frame pointer is explicitly stored. In the case of gcc, */
3953 /* compiler flags (e.g. -fomit-frame-pointer) determine whether it is. */
3954 #if defined(I386) && defined(LINUX) && defined(SAVE_CALL_CHAIN)
3955 # include <features.h>
3958 struct frame *fr_savfp;
3960 long fr_arg[NARGS]; /* All the arguments go here. */
3966 # include <features.h>
3971 struct frame *fr_savfp;
3980 # if defined(SUNOS4)
3981 # include <machine/frame.h>
3983 # if defined (DRSNX)
3984 # include <sys/sparc/frame.h>
3986 # if defined(OPENBSD)
3989 # if defined(FREEBSD) || defined(NETBSD)
3990 # include <machine/frame.h>
3992 # include <sys/frame.h>
3999 --> We only know how to to get the first 6 arguments
4003 #ifdef NEED_CALLINFO
4004 /* Fill in the pc and argument information for up to NFRAMES of my */
4005 /* callers. Ignore my frame and my callers frame. */
4008 # include <unistd.h>
4011 #endif /* NEED_CALLINFO */
4013 #if defined(GC_HAVE_BUILTIN_BACKTRACE)
4014 # include <execinfo.h>
4017 #ifdef SAVE_CALL_CHAIN
4019 #if NARGS == 0 && NFRAMES % 2 == 0 /* No padding */ \
4020 && defined(GC_HAVE_BUILTIN_BACKTRACE)
4022 #ifdef REDIRECT_MALLOC
4023 /* Deal with possible malloc calls in backtrace by omitting */
4024 /* the infinitely recursing backtrace. */
4026 __thread /* If your compiler doesn't understand this */
4027 /* you could use something like pthread_getspecific. */
4029 GC_in_save_callers = FALSE;
4032 void GC_save_callers (info)
4033 struct callinfo info[NFRAMES];
4035 void * tmp_info[NFRAMES + 1];
4037 # define IGNORE_FRAMES 1
4039 /* We retrieve NFRAMES+1 pc values, but discard the first, since it */
4040 /* points to our own frame. */
4041 # ifdef REDIRECT_MALLOC
4042 if (GC_in_save_callers) {
4043 info[0].ci_pc = (word)(&GC_save_callers);
4044 for (i = 1; i < NFRAMES; ++i) info[i].ci_pc = 0;
4047 GC_in_save_callers = TRUE;
4049 GC_ASSERT(sizeof(struct callinfo) == sizeof(void *));
4050 npcs = backtrace((void **)tmp_info, NFRAMES + IGNORE_FRAMES);
4051 BCOPY(tmp_info+IGNORE_FRAMES, info, (npcs - IGNORE_FRAMES) * sizeof(void *));
4052 for (i = npcs - IGNORE_FRAMES; i < NFRAMES; ++i) info[i].ci_pc = 0;
4053 # ifdef REDIRECT_MALLOC
4054 GC_in_save_callers = FALSE;
4058 #else /* No builtin backtrace; do it ourselves */
4060 #if (defined(OPENBSD) || defined(NETBSD) || defined(FREEBSD)) && defined(SPARC)
4061 # define FR_SAVFP fr_fp
4062 # define FR_SAVPC fr_pc
4064 # define FR_SAVFP fr_savfp
4065 # define FR_SAVPC fr_savpc
4068 #if defined(SPARC) && (defined(__arch64__) || defined(__sparcv9))
4074 void GC_save_callers (info)
4075 struct callinfo info[NFRAMES];
4077 struct frame *frame;
4081 /* We assume this is turned on only with gcc as the compiler. */
4082 asm("movl %%ebp,%0" : "=r"(frame));
4085 frame = (struct frame *) GC_save_regs_in_stack ();
4086 fp = (struct frame *)((long) frame -> FR_SAVFP + BIAS);
4089 for (; (!(fp HOTTER_THAN frame) && !(GC_stackbottom HOTTER_THAN (ptr_t)fp)
4090 && (nframes < NFRAMES));
4091 fp = (struct frame *)((long) fp -> FR_SAVFP + BIAS), nframes++) {
4094 info[nframes].ci_pc = fp->FR_SAVPC;
4096 for (i = 0; i < NARGS; i++) {
4097 info[nframes].ci_arg[i] = ~(fp->fr_arg[i]);
4099 # endif /* NARGS > 0 */
4101 if (nframes < NFRAMES) info[nframes].ci_pc = 0;
4104 #endif /* No builtin backtrace */
4106 #endif /* SAVE_CALL_CHAIN */
4108 #ifdef NEED_CALLINFO
4110 /* Print info to stderr. We do NOT hold the allocation lock */
4111 void GC_print_callers (info)
4112 struct callinfo info[NFRAMES];
4115 static int reentry_count = 0;
4116 GC_bool stop = FALSE;
4118 /* FIXME: This should probably use a different lock, so that we */
4119 /* become callable with or without the allocation lock. */
4125 GC_err_printf0("\tCaller at allocation:\n");
4127 GC_err_printf0("\tCall chain at allocation:\n");
4129 for (i = 0; i < NFRAMES && !stop ; i++) {
4130 if (info[i].ci_pc == 0) break;
4135 GC_err_printf0("\t\targs: ");
4136 for (j = 0; j < NARGS; j++) {
4137 if (j != 0) GC_err_printf0(", ");
4138 GC_err_printf2("%d (0x%X)", ~(info[i].ci_arg[j]),
4139 ~(info[i].ci_arg[j]));
4141 GC_err_printf0("\n");
4144 if (reentry_count > 1) {
4145 /* We were called during an allocation during */
4146 /* a previous GC_print_callers call; punt. */
4147 GC_err_printf1("\t\t##PC##= 0x%lx\n", info[i].ci_pc);
4154 # if defined(GC_HAVE_BUILTIN_BACKTRACE) \
4155 && !defined(GC_BACKTRACE_SYMBOLS_BROKEN)
4157 backtrace_symbols((void **)(&(info[i].ci_pc)), 1);
4158 char *name = sym_name[0];
4162 sprintf(buf, "##PC##= 0x%lx", info[i].ci_pc);
4164 # if defined(LINUX) && !defined(SMALL_CONFIG)
4165 /* Try for a line number. */
4168 static char exe_name[EXE_SZ];
4170 char cmd_buf[CMD_SZ];
4171 # define RESULT_SZ 200
4172 static char result_buf[RESULT_SZ];
4175 # define PRELOAD_SZ 200
4176 char preload_buf[PRELOAD_SZ];
4177 static GC_bool found_exe_name = FALSE;
4178 static GC_bool will_fail = FALSE;
4180 /* Try to get it via a hairy and expensive scheme. */
4181 /* First we get the name of the executable: */
4182 if (will_fail) goto out;
4183 if (!found_exe_name) {
4184 ret_code = readlink("/proc/self/exe", exe_name, EXE_SZ);
4185 if (ret_code < 0 || ret_code >= EXE_SZ
4186 || exe_name[0] != '/') {
4187 will_fail = TRUE; /* Dont try again. */
4190 exe_name[ret_code] = '\0';
4191 found_exe_name = TRUE;
4193 /* Then we use popen to start addr2line -e <exe> <addr> */
4194 /* There are faster ways to do this, but hopefully this */
4195 /* isn't time critical. */
4196 sprintf(cmd_buf, "/usr/bin/addr2line -f -e %s 0x%lx", exe_name,
4197 (unsigned long)info[i].ci_pc);
4198 old_preload = getenv ("LD_PRELOAD");
4199 if (0 != old_preload) {
4200 if (strlen (old_preload) >= PRELOAD_SZ) {
4204 strcpy (preload_buf, old_preload);
4205 unsetenv ("LD_PRELOAD");
4207 pipe = popen(cmd_buf, "r");
4208 if (0 != old_preload
4209 && 0 != setenv ("LD_PRELOAD", preload_buf, 0)) {
4210 WARN("Failed to reset LD_PRELOAD\n", 0);
4213 || (result_len = fread(result_buf, 1, RESULT_SZ - 1, pipe))
4215 if (pipe != NULL) pclose(pipe);
4219 if (result_buf[result_len - 1] == '\n') --result_len;
4220 result_buf[result_len] = 0;
4221 if (result_buf[0] == '?'
4222 || result_buf[result_len-2] == ':'
4223 && result_buf[result_len-1] == '0') {
4227 /* Get rid of embedded newline, if any. Test for "main" */
4229 char * nl = strchr(result_buf, '\n');
4230 if (nl != NULL && nl < result_buf + result_len) {
4233 if (strncmp(result_buf, "main", nl - result_buf) == 0) {
4237 if (result_len < RESULT_SZ - 25) {
4238 /* Add in hex address */
4239 sprintf(result_buf + result_len, " [0x%lx]",
4240 (unsigned long)info[i].ci_pc);
4247 GC_err_printf1("\t\t%s\n", name);
4248 # if defined(GC_HAVE_BUILTIN_BACKTRACE) \
4249 && !defined(GC_BACKTRACE_SYMBOLS_BROKEN)
4250 free(sym_name); /* May call GC_free; that's OK */
4259 #endif /* NEED_CALLINFO */
4263 #if defined(LINUX) && defined(__ELF__) && !defined(SMALL_CONFIG)
4265 /* Dump /proc/self/maps to GC_stderr, to enable looking up names for
4266 addresses in FIND_LEAK output. */
4268 static word dump_maps(char *maps)
4270 GC_err_write(maps, strlen(maps));
4274 void GC_print_address_map()
4276 GC_err_printf0("---------- Begin address map ----------\n");
4277 GC_apply_to_maps(dump_maps);
4278 GC_err_printf0("---------- End address map ----------\n");