2005-08-04 Paul Brook <paul@codesourcery.com>
[official-gcc.git] / boehm-gc / os_dep.c
blobaadd5b9625151df41628ed6a33059320b3d31cc2
1 /*
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.
17 # include "private/gc_priv.h"
19 # if defined(LINUX) && !defined(POWERPC)
20 # include <linux/version.h>
21 # if (LINUX_VERSION_CODE <= 0x10400)
22 /* Ugly hack to get struct sigcontext_struct definition. Required */
23 /* for some early 1.3.X releases. Will hopefully go away soon. */
24 /* in some later Linux releases, asm/sigcontext.h may have to */
25 /* be included instead. */
26 # define __KERNEL__
27 # include <asm/signal.h>
28 # undef __KERNEL__
29 # else
30 /* Kernels prior to 2.1.1 defined struct sigcontext_struct instead of */
31 /* struct sigcontext. libc6 (glibc2) uses "struct sigcontext" in */
32 /* prototypes, so we have to include the top-level sigcontext.h to */
33 /* make sure the former gets defined to be the latter if appropriate. */
34 # include <features.h>
35 # if 2 <= __GLIBC__
36 # if 2 == __GLIBC__ && 0 == __GLIBC_MINOR__
37 /* glibc 2.1 no longer has sigcontext.h. But signal.h */
38 /* has the right declaration for glibc 2.1. */
39 # include <sigcontext.h>
40 # endif /* 0 == __GLIBC_MINOR__ */
41 # else /* not 2 <= __GLIBC__ */
42 /* libc5 doesn't have <sigcontext.h>: go directly with the kernel */
43 /* one. Check LINUX_VERSION_CODE to see which we should reference. */
44 # include <asm/sigcontext.h>
45 # endif /* 2 <= __GLIBC__ */
46 # endif
47 # endif
48 # if !defined(OS2) && !defined(PCR) && !defined(AMIGA) && !defined(MACOS) \
49 && !defined(MSWINCE)
50 # include <sys/types.h>
51 # if !defined(MSWIN32) && !defined(SUNOS4)
52 # include <unistd.h>
53 # endif
54 # endif
56 # include <stdio.h>
57 # if defined(MSWINCE)
58 # define SIGSEGV 0 /* value is irrelevant */
59 # else
60 # include <signal.h>
61 # endif
63 /* Blatantly OS dependent routines, except for those that are related */
64 /* to dynamic loading. */
66 # if defined(HEURISTIC2) || defined(SEARCH_FOR_DATA_START)
67 # define NEED_FIND_LIMIT
68 # endif
70 # if !defined(STACKBOTTOM) && defined(HEURISTIC2)
71 # define NEED_FIND_LIMIT
72 # endif
74 # if (defined(SUNOS4) && defined(DYNAMIC_LOADING)) && !defined(PCR)
75 # define NEED_FIND_LIMIT
76 # endif
78 # if (defined(SVR4) || defined(AUX) || defined(DGUX) \
79 || (defined(LINUX) && defined(SPARC))) && !defined(PCR)
80 # define NEED_FIND_LIMIT
81 # endif
83 #if defined(FREEBSD) && (defined(I386) || defined(powerpc) || defined(__powerpc__))
84 # include <machine/trap.h>
85 # if !defined(PCR)
86 # define NEED_FIND_LIMIT
87 # endif
88 #endif
90 #if (defined(NETBSD) || defined(OPENBSD)) && defined(__ELF__) \
91 && !defined(NEED_FIND_LIMIT)
92 /* Used by GC_init_netbsd_elf() below. */
93 # define NEED_FIND_LIMIT
94 #endif
96 #ifdef NEED_FIND_LIMIT
97 # include <setjmp.h>
98 #endif
100 #ifdef AMIGA
101 # define GC_AMIGA_DEF
102 # include "AmigaOS.c"
103 # undef GC_AMIGA_DEF
104 #endif
106 #if defined(MSWIN32) || defined(MSWINCE)
107 # define WIN32_LEAN_AND_MEAN
108 # define NOSERVICE
109 # include <windows.h>
110 #endif
112 #ifdef MACOS
113 # include <Processes.h>
114 #endif
116 #ifdef IRIX5
117 # include <sys/uio.h>
118 # include <malloc.h> /* for locking */
119 #endif
120 #if defined(USE_MMAP) || defined(USE_MUNMAP)
121 # ifndef USE_MMAP
122 --> USE_MUNMAP requires USE_MMAP
123 # endif
124 # include <sys/types.h>
125 # include <sys/mman.h>
126 # include <sys/stat.h>
127 # include <errno.h>
128 #endif
130 #ifdef UNIX_LIKE
131 # include <fcntl.h>
132 # if defined(SUNOS5SIGS) && !defined(FREEBSD)
133 # include <sys/siginfo.h>
134 # endif
135 /* Define SETJMP and friends to be the version that restores */
136 /* the signal mask. */
137 # define SETJMP(env) sigsetjmp(env, 1)
138 # define LONGJMP(env, val) siglongjmp(env, val)
139 # define JMP_BUF sigjmp_buf
140 #else
141 # define SETJMP(env) setjmp(env)
142 # define LONGJMP(env, val) longjmp(env, val)
143 # define JMP_BUF jmp_buf
144 #endif
146 #ifdef DARWIN
147 /* for get_etext and friends */
148 #include <mach-o/getsect.h>
149 #endif
151 #ifdef DJGPP
152 /* Apparently necessary for djgpp 2.01. May cause problems with */
153 /* other versions. */
154 typedef long unsigned int caddr_t;
155 #endif
157 #ifdef PCR
158 # include "il/PCR_IL.h"
159 # include "th/PCR_ThCtl.h"
160 # include "mm/PCR_MM.h"
161 #endif
163 #if !defined(NO_EXECUTE_PERMISSION)
164 # define OPT_PROT_EXEC PROT_EXEC
165 #else
166 # define OPT_PROT_EXEC 0
167 #endif
169 #if defined(LINUX) && \
170 (defined(USE_PROC_FOR_LIBRARIES) || defined(IA64) || !defined(SMALL_CONFIG))
172 /* We need to parse /proc/self/maps, either to find dynamic libraries, */
173 /* and/or to find the register backing store base (IA64). Do it once */
174 /* here. */
176 #define READ read
178 /* Repeatedly perform a read call until the buffer is filled or */
179 /* we encounter EOF. */
180 ssize_t GC_repeat_read(int fd, char *buf, size_t count)
182 ssize_t num_read = 0;
183 ssize_t result;
185 while (num_read < count) {
186 result = READ(fd, buf + num_read, count - num_read);
187 if (result < 0) return result;
188 if (result == 0) break;
189 num_read += result;
191 return num_read;
195 * Apply fn to a buffer containing the contents of /proc/self/maps.
196 * Return the result of fn or, if we failed, 0.
197 * We currently do nothing to /proc/self/maps other than simply read
198 * it. This code could be simplified if we could determine its size
199 * ahead of time.
202 word GC_apply_to_maps(word (*fn)(char *))
204 int f;
205 int result;
206 size_t maps_size = 4000; /* Initial guess. */
207 static char init_buf[1];
208 static char *maps_buf = init_buf;
209 static size_t maps_buf_sz = 1;
211 /* Read /proc/self/maps, growing maps_buf as necessary. */
212 /* Note that we may not allocate conventionally, and */
213 /* thus can't use stdio. */
214 do {
215 if (maps_size >= maps_buf_sz) {
216 /* Grow only by powers of 2, since we leak "too small" buffers. */
217 while (maps_size >= maps_buf_sz) maps_buf_sz *= 2;
218 maps_buf = GC_scratch_alloc(maps_buf_sz);
219 if (maps_buf == 0) return 0;
221 f = open("/proc/self/maps", O_RDONLY);
222 if (-1 == f) return 0;
223 maps_size = 0;
224 do {
225 result = GC_repeat_read(f, maps_buf, maps_buf_sz-1);
226 if (result <= 0) return 0;
227 maps_size += result;
228 } while (result == maps_buf_sz-1);
229 close(f);
230 } while (maps_size >= maps_buf_sz);
231 maps_buf[maps_size] = '\0';
233 /* Apply fn to result. */
234 return fn(maps_buf);
237 #endif /* Need GC_apply_to_maps */
239 #if defined(LINUX) && (defined(USE_PROC_FOR_LIBRARIES) || defined(IA64))
241 // GC_parse_map_entry parses an entry from /proc/self/maps so we can
242 // locate all writable data segments that belong to shared libraries.
243 // The format of one of these entries and the fields we care about
244 // is as follows:
245 // XXXXXXXX-XXXXXXXX r-xp 00000000 30:05 260537 name of mapping...\n
246 // ^^^^^^^^ ^^^^^^^^ ^^^^ ^^
247 // start end prot maj_dev
248 // 0 9 18 32
250 // For 64 bit ABIs:
251 // 0 17 34 56
253 // The parser is called with a pointer to the entry and the return value
254 // is either NULL or is advanced to the next entry(the byte after the
255 // trailing '\n'.)
257 #if CPP_WORDSZ == 32
258 # define OFFSET_MAP_START 0
259 # define OFFSET_MAP_END 9
260 # define OFFSET_MAP_PROT 18
261 # define OFFSET_MAP_MAJDEV 32
262 # define ADDR_WIDTH 8
263 #endif
265 #if CPP_WORDSZ == 64
266 # define OFFSET_MAP_START 0
267 # define OFFSET_MAP_END 17
268 # define OFFSET_MAP_PROT 34
269 # define OFFSET_MAP_MAJDEV 56
270 # define ADDR_WIDTH 16
271 #endif
274 * Assign various fields of the first line in buf_ptr to *start, *end,
275 * *prot_buf and *maj_dev. Only *prot_buf may be set for unwritable maps.
277 char *GC_parse_map_entry(char *buf_ptr, word *start, word *end,
278 char *prot_buf, unsigned int *maj_dev)
280 int i;
281 char *tok;
283 if (buf_ptr == NULL || *buf_ptr == '\0') {
284 return NULL;
287 memcpy(prot_buf, buf_ptr+OFFSET_MAP_PROT, 4);
288 /* do the protections first. */
289 prot_buf[4] = '\0';
291 if (prot_buf[1] == 'w') {/* we can skip all of this if it's not writable. */
293 tok = buf_ptr;
294 buf_ptr[OFFSET_MAP_START+ADDR_WIDTH] = '\0';
295 *start = strtoul(tok, NULL, 16);
297 tok = buf_ptr+OFFSET_MAP_END;
298 buf_ptr[OFFSET_MAP_END+ADDR_WIDTH] = '\0';
299 *end = strtoul(tok, NULL, 16);
301 buf_ptr += OFFSET_MAP_MAJDEV;
302 tok = buf_ptr;
303 while (*buf_ptr != ':') buf_ptr++;
304 *buf_ptr++ = '\0';
305 *maj_dev = strtoul(tok, NULL, 16);
308 while (*buf_ptr && *buf_ptr++ != '\n');
310 return buf_ptr;
313 #endif /* Need to parse /proc/self/maps. */
315 #if defined(SEARCH_FOR_DATA_START)
316 /* The I386 case can be handled without a search. The Alpha case */
317 /* used to be handled differently as well, but the rules changed */
318 /* for recent Linux versions. This seems to be the easiest way to */
319 /* cover all versions. */
321 # ifdef LINUX
322 /* Some Linux distributions arrange to define __data_start. Some */
323 /* define data_start as a weak symbol. The latter is technically */
324 /* broken, since the user program may define data_start, in which */
325 /* case we lose. Nonetheless, we try both, prefering __data_start. */
326 /* We assume gcc-compatible pragmas. */
327 # pragma weak __data_start
328 extern int __data_start[];
329 # pragma weak data_start
330 extern int data_start[];
331 # endif /* LINUX */
332 extern int _end[];
334 ptr_t GC_data_start;
336 void GC_init_linux_data_start()
338 extern ptr_t GC_find_limit();
340 # ifdef LINUX
341 /* Try the easy approaches first: */
342 if ((ptr_t)__data_start != 0) {
343 GC_data_start = (ptr_t)(__data_start);
344 return;
346 if ((ptr_t)data_start != 0) {
347 GC_data_start = (ptr_t)(data_start);
348 return;
350 # endif /* LINUX */
351 GC_data_start = GC_find_limit((ptr_t)(_end), FALSE);
353 #endif
355 # ifdef ECOS
357 # ifndef ECOS_GC_MEMORY_SIZE
358 # define ECOS_GC_MEMORY_SIZE (448 * 1024)
359 # endif /* ECOS_GC_MEMORY_SIZE */
361 // setjmp() function, as described in ANSI para 7.6.1.1
362 #undef SETJMP
363 #define SETJMP( __env__ ) hal_setjmp( __env__ )
365 // FIXME: This is a simple way of allocating memory which is
366 // compatible with ECOS early releases. Later releases use a more
367 // sophisticated means of allocating memory than this simple static
368 // allocator, but this method is at least bound to work.
369 static char memory[ECOS_GC_MEMORY_SIZE];
370 static char *brk = memory;
372 static void *tiny_sbrk(ptrdiff_t increment)
374 void *p = brk;
376 brk += increment;
378 if (brk > memory + sizeof memory)
380 brk -= increment;
381 return NULL;
384 return p;
386 #define sbrk tiny_sbrk
387 # endif /* ECOS */
389 #if (defined(NETBSD) || defined(OPENBSD)) && defined(__ELF__)
390 ptr_t GC_data_start;
392 void GC_init_netbsd_elf()
394 extern ptr_t GC_find_limit();
395 extern char **environ;
396 /* This may need to be environ, without the underscore, for */
397 /* some versions. */
398 GC_data_start = GC_find_limit((ptr_t)&environ, FALSE);
400 #endif
402 # ifdef OS2
404 # include <stddef.h>
406 # if !defined(__IBMC__) && !defined(__WATCOMC__) /* e.g. EMX */
408 struct exe_hdr {
409 unsigned short magic_number;
410 unsigned short padding[29];
411 long new_exe_offset;
414 #define E_MAGIC(x) (x).magic_number
415 #define EMAGIC 0x5A4D
416 #define E_LFANEW(x) (x).new_exe_offset
418 struct e32_exe {
419 unsigned char magic_number[2];
420 unsigned char byte_order;
421 unsigned char word_order;
422 unsigned long exe_format_level;
423 unsigned short cpu;
424 unsigned short os;
425 unsigned long padding1[13];
426 unsigned long object_table_offset;
427 unsigned long object_count;
428 unsigned long padding2[31];
431 #define E32_MAGIC1(x) (x).magic_number[0]
432 #define E32MAGIC1 'L'
433 #define E32_MAGIC2(x) (x).magic_number[1]
434 #define E32MAGIC2 'X'
435 #define E32_BORDER(x) (x).byte_order
436 #define E32LEBO 0
437 #define E32_WORDER(x) (x).word_order
438 #define E32LEWO 0
439 #define E32_CPU(x) (x).cpu
440 #define E32CPU286 1
441 #define E32_OBJTAB(x) (x).object_table_offset
442 #define E32_OBJCNT(x) (x).object_count
444 struct o32_obj {
445 unsigned long size;
446 unsigned long base;
447 unsigned long flags;
448 unsigned long pagemap;
449 unsigned long mapsize;
450 unsigned long reserved;
453 #define O32_FLAGS(x) (x).flags
454 #define OBJREAD 0x0001L
455 #define OBJWRITE 0x0002L
456 #define OBJINVALID 0x0080L
457 #define O32_SIZE(x) (x).size
458 #define O32_BASE(x) (x).base
460 # else /* IBM's compiler */
462 /* A kludge to get around what appears to be a header file bug */
463 # ifndef WORD
464 # define WORD unsigned short
465 # endif
466 # ifndef DWORD
467 # define DWORD unsigned long
468 # endif
470 # define EXE386 1
471 # include <newexe.h>
472 # include <exe386.h>
474 # endif /* __IBMC__ */
476 # define INCL_DOSEXCEPTIONS
477 # define INCL_DOSPROCESS
478 # define INCL_DOSERRORS
479 # define INCL_DOSMODULEMGR
480 # define INCL_DOSMEMMGR
481 # include <os2.h>
484 /* Disable and enable signals during nontrivial allocations */
486 void GC_disable_signals(void)
488 ULONG nest;
490 DosEnterMustComplete(&nest);
491 if (nest != 1) ABORT("nested GC_disable_signals");
494 void GC_enable_signals(void)
496 ULONG nest;
498 DosExitMustComplete(&nest);
499 if (nest != 0) ABORT("GC_enable_signals");
503 # else
505 # if !defined(PCR) && !defined(AMIGA) && !defined(MSWIN32) \
506 && !defined(MSWINCE) \
507 && !defined(MACOS) && !defined(DJGPP) && !defined(DOS4GW) \
508 && !defined(NOSYS) && !defined(ECOS)
510 # if defined(sigmask) && !defined(UTS4) && !defined(HURD)
511 /* Use the traditional BSD interface */
512 # define SIGSET_T int
513 # define SIG_DEL(set, signal) (set) &= ~(sigmask(signal))
514 # define SIG_FILL(set) (set) = 0x7fffffff
515 /* Setting the leading bit appears to provoke a bug in some */
516 /* longjmp implementations. Most systems appear not to have */
517 /* a signal 32. */
518 # define SIGSETMASK(old, new) (old) = sigsetmask(new)
519 # else
520 /* Use POSIX/SYSV interface */
521 # define SIGSET_T sigset_t
522 # define SIG_DEL(set, signal) sigdelset(&(set), (signal))
523 # define SIG_FILL(set) sigfillset(&set)
524 # define SIGSETMASK(old, new) sigprocmask(SIG_SETMASK, &(new), &(old))
525 # endif
527 static GC_bool mask_initialized = FALSE;
529 static SIGSET_T new_mask;
531 static SIGSET_T old_mask;
533 static SIGSET_T dummy;
535 #if defined(PRINTSTATS) && !defined(THREADS)
536 # define CHECK_SIGNALS
537 int GC_sig_disabled = 0;
538 #endif
540 void GC_disable_signals()
542 if (!mask_initialized) {
543 SIG_FILL(new_mask);
545 SIG_DEL(new_mask, SIGSEGV);
546 SIG_DEL(new_mask, SIGILL);
547 SIG_DEL(new_mask, SIGQUIT);
548 # ifdef SIGBUS
549 SIG_DEL(new_mask, SIGBUS);
550 # endif
551 # ifdef SIGIOT
552 SIG_DEL(new_mask, SIGIOT);
553 # endif
554 # ifdef SIGEMT
555 SIG_DEL(new_mask, SIGEMT);
556 # endif
557 # ifdef SIGTRAP
558 SIG_DEL(new_mask, SIGTRAP);
559 # endif
560 mask_initialized = TRUE;
562 # ifdef CHECK_SIGNALS
563 if (GC_sig_disabled != 0) ABORT("Nested disables");
564 GC_sig_disabled++;
565 # endif
566 SIGSETMASK(old_mask,new_mask);
569 void GC_enable_signals()
571 # ifdef CHECK_SIGNALS
572 if (GC_sig_disabled != 1) ABORT("Unmatched enable");
573 GC_sig_disabled--;
574 # endif
575 SIGSETMASK(dummy,old_mask);
578 # endif /* !PCR */
580 # endif /*!OS/2 */
582 /* Ivan Demakov: simplest way (to me) */
583 #if defined (DOS4GW)
584 void GC_disable_signals() { }
585 void GC_enable_signals() { }
586 #endif
588 /* Find the page size */
589 word GC_page_size;
591 # if defined(MSWIN32) || defined(MSWINCE)
592 void GC_setpagesize()
594 GetSystemInfo(&GC_sysinfo);
595 GC_page_size = GC_sysinfo.dwPageSize;
598 # else
599 # if defined(MPROTECT_VDB) || defined(PROC_VDB) || defined(USE_MMAP) \
600 || defined(USE_MUNMAP)
601 void GC_setpagesize()
603 GC_page_size = GETPAGESIZE();
605 # else
606 /* It's acceptable to fake it. */
607 void GC_setpagesize()
609 GC_page_size = HBLKSIZE;
611 # endif
612 # endif
615 * Find the base of the stack.
616 * Used only in single-threaded environment.
617 * With threads, GC_mark_roots needs to know how to do this.
618 * Called with allocator lock held.
620 # if defined(MSWIN32) || defined(MSWINCE)
621 # define is_writable(prot) ((prot) == PAGE_READWRITE \
622 || (prot) == PAGE_WRITECOPY \
623 || (prot) == PAGE_EXECUTE_READWRITE \
624 || (prot) == PAGE_EXECUTE_WRITECOPY)
625 /* Return the number of bytes that are writable starting at p. */
626 /* The pointer p is assumed to be page aligned. */
627 /* If base is not 0, *base becomes the beginning of the */
628 /* allocation region containing p. */
629 word GC_get_writable_length(ptr_t p, ptr_t *base)
631 MEMORY_BASIC_INFORMATION buf;
632 word result;
633 word protect;
635 result = VirtualQuery(p, &buf, sizeof(buf));
636 if (result != sizeof(buf)) ABORT("Weird VirtualQuery result");
637 if (base != 0) *base = (ptr_t)(buf.AllocationBase);
638 protect = (buf.Protect & ~(PAGE_GUARD | PAGE_NOCACHE));
639 if (!is_writable(protect)) {
640 return(0);
642 if (buf.State != MEM_COMMIT) return(0);
643 return(buf.RegionSize);
646 ptr_t GC_get_stack_base()
648 int dummy;
649 ptr_t sp = (ptr_t)(&dummy);
650 ptr_t trunc_sp = (ptr_t)((word)sp & ~(GC_page_size - 1));
651 word size = GC_get_writable_length(trunc_sp, 0);
653 return(trunc_sp + size);
657 # endif /* MS Windows */
659 # ifdef BEOS
660 # include <kernel/OS.h>
661 ptr_t GC_get_stack_base(){
662 thread_info th;
663 get_thread_info(find_thread(NULL),&th);
664 return th.stack_end;
666 # endif /* BEOS */
669 # ifdef OS2
671 ptr_t GC_get_stack_base()
673 PTIB ptib;
674 PPIB ppib;
676 if (DosGetInfoBlocks(&ptib, &ppib) != NO_ERROR) {
677 GC_err_printf0("DosGetInfoBlocks failed\n");
678 ABORT("DosGetInfoBlocks failed\n");
680 return((ptr_t)(ptib -> tib_pstacklimit));
683 # endif /* OS2 */
685 # ifdef AMIGA
686 # define GC_AMIGA_SB
687 # include "AmigaOS.c"
688 # undef GC_AMIGA_SB
689 # endif /* AMIGA */
691 # if defined(NEED_FIND_LIMIT) || defined(UNIX_LIKE)
693 # ifdef __STDC__
694 typedef void (*handler)(int);
695 # else
696 typedef void (*handler)();
697 # endif
699 # if defined(SUNOS5SIGS) || defined(IRIX5) || defined(OSF1) \
700 || defined(HURD) || defined(NETBSD)
701 static struct sigaction old_segv_act;
702 # if defined(_sigargs) /* !Irix6.x */ || defined(HPUX) \
703 || defined(HURD) || defined(NETBSD)
704 static struct sigaction old_bus_act;
705 # endif
706 # else
707 static handler old_segv_handler, old_bus_handler;
708 # endif
710 # ifdef __STDC__
711 void GC_set_and_save_fault_handler(handler h)
712 # else
713 void GC_set_and_save_fault_handler(h)
714 handler h;
715 # endif
717 # if defined(SUNOS5SIGS) || defined(IRIX5) \
718 || defined(OSF1) || defined(HURD) || defined(NETBSD)
719 struct sigaction act;
721 act.sa_handler = h;
722 # if 0 /* Was necessary for Solaris 2.3 and very temporary */
723 /* NetBSD bugs. */
724 act.sa_flags = SA_RESTART | SA_NODEFER;
725 # else
726 act.sa_flags = SA_RESTART;
727 # endif
729 (void) sigemptyset(&act.sa_mask);
730 # ifdef GC_IRIX_THREADS
731 /* Older versions have a bug related to retrieving and */
732 /* and setting a handler at the same time. */
733 (void) sigaction(SIGSEGV, 0, &old_segv_act);
734 (void) sigaction(SIGSEGV, &act, 0);
735 # else
736 (void) sigaction(SIGSEGV, &act, &old_segv_act);
737 # if defined(IRIX5) && defined(_sigargs) /* Irix 5.x, not 6.x */ \
738 || defined(HPUX) || defined(HURD) || defined(NETBSD)
739 /* Under Irix 5.x or HP/UX, we may get SIGBUS. */
740 /* Pthreads doesn't exist under Irix 5.x, so we */
741 /* don't have to worry in the threads case. */
742 (void) sigaction(SIGBUS, &act, &old_bus_act);
743 # endif
744 # endif /* GC_IRIX_THREADS */
745 # else
746 old_segv_handler = signal(SIGSEGV, h);
747 # ifdef SIGBUS
748 old_bus_handler = signal(SIGBUS, h);
749 # endif
750 # endif
752 # endif /* NEED_FIND_LIMIT || UNIX_LIKE */
754 # ifdef NEED_FIND_LIMIT
755 /* Some tools to implement HEURISTIC2 */
756 # define MIN_PAGE_SIZE 256 /* Smallest conceivable page size, bytes */
757 /* static */ JMP_BUF GC_jmp_buf;
759 /*ARGSUSED*/
760 void GC_fault_handler(sig)
761 int sig;
763 LONGJMP(GC_jmp_buf, 1);
766 void GC_setup_temporary_fault_handler()
768 GC_set_and_save_fault_handler(GC_fault_handler);
771 void GC_reset_fault_handler()
773 # if defined(SUNOS5SIGS) || defined(IRIX5) \
774 || defined(OSF1) || defined(HURD) || defined(NETBSD)
775 (void) sigaction(SIGSEGV, &old_segv_act, 0);
776 # if defined(IRIX5) && defined(_sigargs) /* Irix 5.x, not 6.x */ \
777 || defined(HPUX) || defined(HURD) || defined(NETBSD)
778 (void) sigaction(SIGBUS, &old_bus_act, 0);
779 # endif
780 # else
781 (void) signal(SIGSEGV, old_segv_handler);
782 # ifdef SIGBUS
783 (void) signal(SIGBUS, old_bus_handler);
784 # endif
785 # endif
788 /* Return the first nonaddressible location > p (up) or */
789 /* the smallest location q s.t. [q,p) is addressable (!up). */
790 /* We assume that p (up) or p-1 (!up) is addressable. */
791 ptr_t GC_find_limit(p, up)
792 ptr_t p;
793 GC_bool up;
795 static VOLATILE ptr_t result;
796 /* Needs to be static, since otherwise it may not be */
797 /* preserved across the longjmp. Can safely be */
798 /* static since it's only called once, with the */
799 /* allocation lock held. */
802 GC_setup_temporary_fault_handler();
803 if (SETJMP(GC_jmp_buf) == 0) {
804 result = (ptr_t)(((word)(p))
805 & ~(MIN_PAGE_SIZE-1));
806 for (;;) {
807 if (up) {
808 result += MIN_PAGE_SIZE;
809 } else {
810 result -= MIN_PAGE_SIZE;
812 GC_noop1((word)(*result));
815 GC_reset_fault_handler();
816 if (!up) {
817 result += MIN_PAGE_SIZE;
819 return(result);
821 # endif
823 #if defined(ECOS) || defined(NOSYS)
824 ptr_t GC_get_stack_base()
826 return STACKBOTTOM;
828 #endif
830 #ifdef HPUX_STACKBOTTOM
832 #include <sys/param.h>
833 #include <sys/pstat.h>
835 ptr_t GC_get_register_stack_base(void)
837 struct pst_vm_status vm_status;
839 int i = 0;
840 while (pstat_getprocvm(&vm_status, sizeof(vm_status), 0, i++) == 1) {
841 if (vm_status.pst_type == PS_RSESTACK) {
842 return (ptr_t) vm_status.pst_vaddr;
846 /* old way to get the register stackbottom */
847 return (ptr_t)(((word)GC_stackbottom - BACKING_STORE_DISPLACEMENT - 1)
848 & ~(BACKING_STORE_ALIGNMENT - 1));
851 #endif /* HPUX_STACK_BOTTOM */
853 #ifdef LINUX_STACKBOTTOM
855 #include <sys/types.h>
856 #include <sys/stat.h>
857 #include <ctype.h>
859 # define STAT_SKIP 27 /* Number of fields preceding startstack */
860 /* field in /proc/self/stat */
862 # pragma weak __libc_stack_end
863 extern ptr_t __libc_stack_end;
865 # ifdef IA64
866 /* Try to read the backing store base from /proc/self/maps. */
867 /* We look for the writable mapping with a 0 major device, */
868 /* which is as close to our frame as possible, but below it.*/
869 static word backing_store_base_from_maps(char *maps)
871 char prot_buf[5];
872 char *buf_ptr = maps;
873 word start, end;
874 unsigned int maj_dev;
875 word current_best = 0;
876 word dummy;
878 for (;;) {
879 buf_ptr = GC_parse_map_entry(buf_ptr, &start, &end, prot_buf, &maj_dev);
880 if (buf_ptr == NULL) return current_best;
881 if (prot_buf[1] == 'w' && maj_dev == 0) {
882 if (end < (word)(&dummy) && start > current_best) current_best = start;
885 return current_best;
888 static word backing_store_base_from_proc(void)
890 return GC_apply_to_maps(backing_store_base_from_maps);
893 # pragma weak __libc_ia64_register_backing_store_base
894 extern ptr_t __libc_ia64_register_backing_store_base;
896 ptr_t GC_get_register_stack_base(void)
898 if (0 != &__libc_ia64_register_backing_store_base
899 && 0 != __libc_ia64_register_backing_store_base) {
900 /* Glibc 2.2.4 has a bug such that for dynamically linked */
901 /* executables __libc_ia64_register_backing_store_base is */
902 /* defined but uninitialized during constructor calls. */
903 /* Hence we check for both nonzero address and value. */
904 return __libc_ia64_register_backing_store_base;
905 } else {
906 word result = backing_store_base_from_proc();
907 if (0 == result) {
908 /* Use dumb heuristics. Works only for default configuration. */
909 result = (word)GC_stackbottom - BACKING_STORE_DISPLACEMENT;
910 result += BACKING_STORE_ALIGNMENT - 1;
911 result &= ~(BACKING_STORE_ALIGNMENT - 1);
912 /* Verify that it's at least readable. If not, we goofed. */
913 GC_noop1(*(word *)result);
915 return (ptr_t)result;
918 # endif
920 ptr_t GC_linux_stack_base(void)
922 /* We read the stack base value from /proc/self/stat. We do this */
923 /* using direct I/O system calls in order to avoid calling malloc */
924 /* in case REDIRECT_MALLOC is defined. */
925 # define STAT_BUF_SIZE 4096
926 # define STAT_READ read
927 /* Should probably call the real read, if read is wrapped. */
928 char stat_buf[STAT_BUF_SIZE];
929 int f;
930 char c;
931 word result = 0;
932 size_t i, buf_offset = 0;
934 /* First try the easy way. This should work for glibc 2.2 */
935 /* This fails in a prelinked ("prelink" command) executable */
936 /* since the correct value of __libc_stack_end never */
937 /* becomes visible to us. The second test works around */
938 /* this. */
939 if (0 != &__libc_stack_end && 0 != __libc_stack_end ) {
940 # ifdef IA64
941 /* Some versions of glibc set the address 16 bytes too */
942 /* low while the initialization code is running. */
943 if (((word)__libc_stack_end & 0xfff) + 0x10 < 0x1000) {
944 return __libc_stack_end + 0x10;
945 } /* Otherwise it's not safe to add 16 bytes and we fall */
946 /* back to using /proc. */
947 # else
948 # ifdef SPARC
949 /* Older versions of glibc for 64-bit Sparc do not set
950 * this variable correctly, it gets set to either zero
951 * or one.
953 if (__libc_stack_end != (ptr_t) (unsigned long)0x1)
954 return __libc_stack_end;
955 # else
956 return __libc_stack_end;
957 # endif
958 # endif
960 f = open("/proc/self/stat", O_RDONLY);
961 if (f < 0 || STAT_READ(f, stat_buf, STAT_BUF_SIZE) < 2 * STAT_SKIP) {
962 ABORT("Couldn't read /proc/self/stat");
964 c = stat_buf[buf_offset++];
965 /* Skip the required number of fields. This number is hopefully */
966 /* constant across all Linux implementations. */
967 for (i = 0; i < STAT_SKIP; ++i) {
968 while (isspace(c)) c = stat_buf[buf_offset++];
969 while (!isspace(c)) c = stat_buf[buf_offset++];
971 while (isspace(c)) c = stat_buf[buf_offset++];
972 while (isdigit(c)) {
973 result *= 10;
974 result += c - '0';
975 c = stat_buf[buf_offset++];
977 close(f);
978 if (result < 0x10000000) ABORT("Absurd stack bottom value");
979 return (ptr_t)result;
982 #endif /* LINUX_STACKBOTTOM */
984 #ifdef FREEBSD_STACKBOTTOM
986 /* This uses an undocumented sysctl call, but at least one expert */
987 /* believes it will stay. */
989 #include <unistd.h>
990 #include <sys/types.h>
991 #include <sys/sysctl.h>
993 ptr_t GC_freebsd_stack_base(void)
995 int nm[2] = {CTL_KERN, KERN_USRSTACK};
996 ptr_t base;
997 size_t len = sizeof(ptr_t);
998 int r = sysctl(nm, 2, &base, &len, NULL, 0);
1000 if (r) ABORT("Error getting stack base");
1002 return base;
1005 #endif /* FREEBSD_STACKBOTTOM */
1007 #if !defined(BEOS) && !defined(AMIGA) && !defined(MSWIN32) \
1008 && !defined(MSWINCE) && !defined(OS2) && !defined(NOSYS) && !defined(ECOS)
1010 ptr_t GC_get_stack_base()
1012 # if defined(HEURISTIC1) || defined(HEURISTIC2) || \
1013 defined(LINUX_STACKBOTTOM) || defined(FREEBSD_STACKBOTTOM)
1014 word dummy;
1015 ptr_t result;
1016 # endif
1018 # define STACKBOTTOM_ALIGNMENT_M1 ((word)STACK_GRAN - 1)
1020 # ifdef STACKBOTTOM
1021 return(STACKBOTTOM);
1022 # else
1023 # ifdef HEURISTIC1
1024 # ifdef STACK_GROWS_DOWN
1025 result = (ptr_t)((((word)(&dummy))
1026 + STACKBOTTOM_ALIGNMENT_M1)
1027 & ~STACKBOTTOM_ALIGNMENT_M1);
1028 # else
1029 result = (ptr_t)(((word)(&dummy))
1030 & ~STACKBOTTOM_ALIGNMENT_M1);
1031 # endif
1032 # endif /* HEURISTIC1 */
1033 # ifdef LINUX_STACKBOTTOM
1034 result = GC_linux_stack_base();
1035 # endif
1036 # ifdef FREEBSD_STACKBOTTOM
1037 result = GC_freebsd_stack_base();
1038 # endif
1039 # ifdef HEURISTIC2
1040 # ifdef STACK_GROWS_DOWN
1041 result = GC_find_limit((ptr_t)(&dummy), TRUE);
1042 # ifdef HEURISTIC2_LIMIT
1043 if (result > HEURISTIC2_LIMIT
1044 && (ptr_t)(&dummy) < HEURISTIC2_LIMIT) {
1045 result = HEURISTIC2_LIMIT;
1047 # endif
1048 # else
1049 result = GC_find_limit((ptr_t)(&dummy), FALSE);
1050 # ifdef HEURISTIC2_LIMIT
1051 if (result < HEURISTIC2_LIMIT
1052 && (ptr_t)(&dummy) > HEURISTIC2_LIMIT) {
1053 result = HEURISTIC2_LIMIT;
1055 # endif
1056 # endif
1058 # endif /* HEURISTIC2 */
1059 # ifdef STACK_GROWS_DOWN
1060 if (result == 0) result = (ptr_t)(signed_word)(-sizeof(ptr_t));
1061 # endif
1062 return(result);
1063 # endif /* STACKBOTTOM */
1066 # endif /* ! AMIGA, !OS 2, ! MS Windows, !BEOS, !NOSYS, !ECOS */
1069 * Register static data segment(s) as roots.
1070 * If more data segments are added later then they need to be registered
1071 * add that point (as we do with SunOS dynamic loading),
1072 * or GC_mark_roots needs to check for them (as we do with PCR).
1073 * Called with allocator lock held.
1076 # ifdef OS2
1078 void GC_register_data_segments()
1080 PTIB ptib;
1081 PPIB ppib;
1082 HMODULE module_handle;
1083 # define PBUFSIZ 512
1084 UCHAR path[PBUFSIZ];
1085 FILE * myexefile;
1086 struct exe_hdr hdrdos; /* MSDOS header. */
1087 struct e32_exe hdr386; /* Real header for my executable */
1088 struct o32_obj seg; /* Currrent segment */
1089 int nsegs;
1092 if (DosGetInfoBlocks(&ptib, &ppib) != NO_ERROR) {
1093 GC_err_printf0("DosGetInfoBlocks failed\n");
1094 ABORT("DosGetInfoBlocks failed\n");
1096 module_handle = ppib -> pib_hmte;
1097 if (DosQueryModuleName(module_handle, PBUFSIZ, path) != NO_ERROR) {
1098 GC_err_printf0("DosQueryModuleName failed\n");
1099 ABORT("DosGetInfoBlocks failed\n");
1101 myexefile = fopen(path, "rb");
1102 if (myexefile == 0) {
1103 GC_err_puts("Couldn't open executable ");
1104 GC_err_puts(path); GC_err_puts("\n");
1105 ABORT("Failed to open executable\n");
1107 if (fread((char *)(&hdrdos), 1, sizeof hdrdos, myexefile) < sizeof hdrdos) {
1108 GC_err_puts("Couldn't read MSDOS header from ");
1109 GC_err_puts(path); GC_err_puts("\n");
1110 ABORT("Couldn't read MSDOS header");
1112 if (E_MAGIC(hdrdos) != EMAGIC) {
1113 GC_err_puts("Executable has wrong DOS magic number: ");
1114 GC_err_puts(path); GC_err_puts("\n");
1115 ABORT("Bad DOS magic number");
1117 if (fseek(myexefile, E_LFANEW(hdrdos), SEEK_SET) != 0) {
1118 GC_err_puts("Seek to new header failed in ");
1119 GC_err_puts(path); GC_err_puts("\n");
1120 ABORT("Bad DOS magic number");
1122 if (fread((char *)(&hdr386), 1, sizeof hdr386, myexefile) < sizeof hdr386) {
1123 GC_err_puts("Couldn't read MSDOS header from ");
1124 GC_err_puts(path); GC_err_puts("\n");
1125 ABORT("Couldn't read OS/2 header");
1127 if (E32_MAGIC1(hdr386) != E32MAGIC1 || E32_MAGIC2(hdr386) != E32MAGIC2) {
1128 GC_err_puts("Executable has wrong OS/2 magic number:");
1129 GC_err_puts(path); GC_err_puts("\n");
1130 ABORT("Bad OS/2 magic number");
1132 if ( E32_BORDER(hdr386) != E32LEBO || E32_WORDER(hdr386) != E32LEWO) {
1133 GC_err_puts("Executable %s has wrong byte order: ");
1134 GC_err_puts(path); GC_err_puts("\n");
1135 ABORT("Bad byte order");
1137 if ( E32_CPU(hdr386) == E32CPU286) {
1138 GC_err_puts("GC can't handle 80286 executables: ");
1139 GC_err_puts(path); GC_err_puts("\n");
1140 EXIT();
1142 if (fseek(myexefile, E_LFANEW(hdrdos) + E32_OBJTAB(hdr386),
1143 SEEK_SET) != 0) {
1144 GC_err_puts("Seek to object table failed: ");
1145 GC_err_puts(path); GC_err_puts("\n");
1146 ABORT("Seek to object table failed");
1148 for (nsegs = E32_OBJCNT(hdr386); nsegs > 0; nsegs--) {
1149 int flags;
1150 if (fread((char *)(&seg), 1, sizeof seg, myexefile) < sizeof seg) {
1151 GC_err_puts("Couldn't read obj table entry from ");
1152 GC_err_puts(path); GC_err_puts("\n");
1153 ABORT("Couldn't read obj table entry");
1155 flags = O32_FLAGS(seg);
1156 if (!(flags & OBJWRITE)) continue;
1157 if (!(flags & OBJREAD)) continue;
1158 if (flags & OBJINVALID) {
1159 GC_err_printf0("Object with invalid pages?\n");
1160 continue;
1162 GC_add_roots_inner(O32_BASE(seg), O32_BASE(seg)+O32_SIZE(seg), FALSE);
1166 # else /* !OS2 */
1168 # if defined(MSWIN32) || defined(MSWINCE)
1170 # ifdef MSWIN32
1171 /* Unfortunately, we have to handle win32s very differently from NT, */
1172 /* Since VirtualQuery has very different semantics. In particular, */
1173 /* under win32s a VirtualQuery call on an unmapped page returns an */
1174 /* invalid result. Under NT, GC_register_data_segments is a noop and */
1175 /* all real work is done by GC_register_dynamic_libraries. Under */
1176 /* win32s, we cannot find the data segments associated with dll's. */
1177 /* We register the main data segment here. */
1178 GC_bool GC_no_win32_dlls = FALSE;
1179 /* This used to be set for gcc, to avoid dealing with */
1180 /* the structured exception handling issues. But we now have */
1181 /* assembly code to do that right. */
1183 void GC_init_win32()
1185 /* if we're running under win32s, assume that no DLLs will be loaded */
1186 DWORD v = GetVersion();
1187 GC_no_win32_dlls |= ((v & 0x80000000) && (v & 0xff) <= 3);
1190 /* Return the smallest address a such that VirtualQuery */
1191 /* returns correct results for all addresses between a and start. */
1192 /* Assumes VirtualQuery returns correct information for start. */
1193 ptr_t GC_least_described_address(ptr_t start)
1195 MEMORY_BASIC_INFORMATION buf;
1196 DWORD result;
1197 LPVOID limit;
1198 ptr_t p;
1199 LPVOID q;
1201 limit = GC_sysinfo.lpMinimumApplicationAddress;
1202 p = (ptr_t)((word)start & ~(GC_page_size - 1));
1203 for (;;) {
1204 q = (LPVOID)(p - GC_page_size);
1205 if ((ptr_t)q > (ptr_t)p /* underflow */ || q < limit) break;
1206 result = VirtualQuery(q, &buf, sizeof(buf));
1207 if (result != sizeof(buf) || buf.AllocationBase == 0) break;
1208 p = (ptr_t)(buf.AllocationBase);
1210 return(p);
1212 # endif
1214 # ifndef REDIRECT_MALLOC
1215 /* We maintain a linked list of AllocationBase values that we know */
1216 /* correspond to malloc heap sections. Currently this is only called */
1217 /* during a GC. But there is some hope that for long running */
1218 /* programs we will eventually see most heap sections. */
1220 /* In the long run, it would be more reliable to occasionally walk */
1221 /* the malloc heap with HeapWalk on the default heap. But that */
1222 /* apparently works only for NT-based Windows. */
1224 /* In the long run, a better data structure would also be nice ... */
1225 struct GC_malloc_heap_list {
1226 void * allocation_base;
1227 struct GC_malloc_heap_list *next;
1228 } *GC_malloc_heap_l = 0;
1230 /* Is p the base of one of the malloc heap sections we already know */
1231 /* about? */
1232 GC_bool GC_is_malloc_heap_base(ptr_t p)
1234 struct GC_malloc_heap_list *q = GC_malloc_heap_l;
1236 while (0 != q) {
1237 if (q -> allocation_base == p) return TRUE;
1238 q = q -> next;
1240 return FALSE;
1243 void *GC_get_allocation_base(void *p)
1245 MEMORY_BASIC_INFORMATION buf;
1246 DWORD result = VirtualQuery(p, &buf, sizeof(buf));
1247 if (result != sizeof(buf)) {
1248 ABORT("Weird VirtualQuery result");
1250 return buf.AllocationBase;
1253 size_t GC_max_root_size = 100000; /* Appr. largest root size. */
1255 void GC_add_current_malloc_heap()
1257 struct GC_malloc_heap_list *new_l =
1258 malloc(sizeof(struct GC_malloc_heap_list));
1259 void * candidate = GC_get_allocation_base(new_l);
1261 if (new_l == 0) return;
1262 if (GC_is_malloc_heap_base(candidate)) {
1263 /* Try a little harder to find malloc heap. */
1264 size_t req_size = 10000;
1265 do {
1266 void *p = malloc(req_size);
1267 if (0 == p) { free(new_l); return; }
1268 candidate = GC_get_allocation_base(p);
1269 free(p);
1270 req_size *= 2;
1271 } while (GC_is_malloc_heap_base(candidate)
1272 && req_size < GC_max_root_size/10 && req_size < 500000);
1273 if (GC_is_malloc_heap_base(candidate)) {
1274 free(new_l); return;
1277 # ifdef CONDPRINT
1278 if (GC_print_stats)
1279 GC_printf1("Found new system malloc AllocationBase at 0x%lx\n",
1280 candidate);
1281 # endif
1282 new_l -> allocation_base = candidate;
1283 new_l -> next = GC_malloc_heap_l;
1284 GC_malloc_heap_l = new_l;
1286 # endif /* REDIRECT_MALLOC */
1288 /* Is p the start of either the malloc heap, or of one of our */
1289 /* heap sections? */
1290 GC_bool GC_is_heap_base (ptr_t p)
1293 unsigned i;
1295 # ifndef REDIRECT_MALLOC
1296 static word last_gc_no = -1;
1298 if (last_gc_no != GC_gc_no) {
1299 GC_add_current_malloc_heap();
1300 last_gc_no = GC_gc_no;
1302 if (GC_root_size > GC_max_root_size) GC_max_root_size = GC_root_size;
1303 if (GC_is_malloc_heap_base(p)) return TRUE;
1304 # endif
1305 for (i = 0; i < GC_n_heap_bases; i++) {
1306 if (GC_heap_bases[i] == p) return TRUE;
1308 return FALSE ;
1311 # ifdef MSWIN32
1312 void GC_register_root_section(ptr_t static_root)
1314 MEMORY_BASIC_INFORMATION buf;
1315 DWORD result;
1316 DWORD protect;
1317 LPVOID p;
1318 char * base;
1319 char * limit, * new_limit;
1321 if (!GC_no_win32_dlls) return;
1322 p = base = limit = GC_least_described_address(static_root);
1323 while (p < GC_sysinfo.lpMaximumApplicationAddress) {
1324 result = VirtualQuery(p, &buf, sizeof(buf));
1325 if (result != sizeof(buf) || buf.AllocationBase == 0
1326 || GC_is_heap_base(buf.AllocationBase)) break;
1327 new_limit = (char *)p + buf.RegionSize;
1328 protect = buf.Protect;
1329 if (buf.State == MEM_COMMIT
1330 && is_writable(protect)) {
1331 if ((char *)p == limit) {
1332 limit = new_limit;
1333 } else {
1334 if (base != limit) GC_add_roots_inner(base, limit, FALSE);
1335 base = p;
1336 limit = new_limit;
1339 if (p > (LPVOID)new_limit /* overflow */) break;
1340 p = (LPVOID)new_limit;
1342 if (base != limit) GC_add_roots_inner(base, limit, FALSE);
1344 #endif
1346 void GC_register_data_segments()
1348 # ifdef MSWIN32
1349 static char dummy;
1350 GC_register_root_section((ptr_t)(&dummy));
1351 # endif
1354 # else /* !OS2 && !Windows */
1356 # if (defined(SVR4) || defined(AUX) || defined(DGUX) \
1357 || (defined(LINUX) && defined(SPARC))) && !defined(PCR)
1358 ptr_t GC_SysVGetDataStart(max_page_size, etext_addr)
1359 int max_page_size;
1360 int * etext_addr;
1362 word text_end = ((word)(etext_addr) + sizeof(word) - 1)
1363 & ~(sizeof(word) - 1);
1364 /* etext rounded to word boundary */
1365 word next_page = ((text_end + (word)max_page_size - 1)
1366 & ~((word)max_page_size - 1));
1367 word page_offset = (text_end & ((word)max_page_size - 1));
1368 VOLATILE char * result = (char *)(next_page + page_offset);
1369 /* Note that this isnt equivalent to just adding */
1370 /* max_page_size to &etext if &etext is at a page boundary */
1372 GC_setup_temporary_fault_handler();
1373 if (SETJMP(GC_jmp_buf) == 0) {
1374 /* Try writing to the address. */
1375 *result = *result;
1376 GC_reset_fault_handler();
1377 } else {
1378 GC_reset_fault_handler();
1379 /* We got here via a longjmp. The address is not readable. */
1380 /* This is known to happen under Solaris 2.4 + gcc, which place */
1381 /* string constants in the text segment, but after etext. */
1382 /* Use plan B. Note that we now know there is a gap between */
1383 /* text and data segments, so plan A bought us something. */
1384 result = (char *)GC_find_limit((ptr_t)(DATAEND), FALSE);
1386 return((ptr_t)result);
1388 # endif
1390 # if defined(FREEBSD) && (defined(I386) || defined(powerpc) || defined(__powerpc__)) && !defined(PCR)
1391 /* Its unclear whether this should be identical to the above, or */
1392 /* whether it should apply to non-X86 architectures. */
1393 /* For now we don't assume that there is always an empty page after */
1394 /* etext. But in some cases there actually seems to be slightly more. */
1395 /* This also deals with holes between read-only data and writable data. */
1396 ptr_t GC_FreeBSDGetDataStart(max_page_size, etext_addr)
1397 int max_page_size;
1398 int * etext_addr;
1400 word text_end = ((word)(etext_addr) + sizeof(word) - 1)
1401 & ~(sizeof(word) - 1);
1402 /* etext rounded to word boundary */
1403 VOLATILE word next_page = (text_end + (word)max_page_size - 1)
1404 & ~((word)max_page_size - 1);
1405 VOLATILE ptr_t result = (ptr_t)text_end;
1406 GC_setup_temporary_fault_handler();
1407 if (SETJMP(GC_jmp_buf) == 0) {
1408 /* Try reading at the address. */
1409 /* This should happen before there is another thread. */
1410 for (; next_page < (word)(DATAEND); next_page += (word)max_page_size)
1411 *(VOLATILE char *)next_page;
1412 GC_reset_fault_handler();
1413 } else {
1414 GC_reset_fault_handler();
1415 /* As above, we go to plan B */
1416 result = GC_find_limit((ptr_t)(DATAEND), FALSE);
1418 return(result);
1421 # endif
1424 #ifdef AMIGA
1426 # define GC_AMIGA_DS
1427 # include "AmigaOS.c"
1428 # undef GC_AMIGA_DS
1430 #else /* !OS2 && !Windows && !AMIGA */
1432 void GC_register_data_segments()
1434 # if !defined(PCR) && !defined(SRC_M3) && !defined(MACOS)
1435 # if defined(REDIRECT_MALLOC) && defined(GC_SOLARIS_THREADS)
1436 /* As of Solaris 2.3, the Solaris threads implementation */
1437 /* allocates the data structure for the initial thread with */
1438 /* sbrk at process startup. It needs to be scanned, so that */
1439 /* we don't lose some malloc allocated data structures */
1440 /* hanging from it. We're on thin ice here ... */
1441 extern caddr_t sbrk();
1443 GC_add_roots_inner(DATASTART, (char *)sbrk(0), FALSE);
1444 # else
1445 GC_add_roots_inner(DATASTART, (char *)(DATAEND), FALSE);
1446 # if defined(DATASTART2)
1447 GC_add_roots_inner(DATASTART2, (char *)(DATAEND2), FALSE);
1448 # endif
1449 # endif
1450 # endif
1451 # if defined(MACOS)
1453 # if defined(THINK_C)
1454 extern void* GC_MacGetDataStart(void);
1455 /* globals begin above stack and end at a5. */
1456 GC_add_roots_inner((ptr_t)GC_MacGetDataStart(),
1457 (ptr_t)LMGetCurrentA5(), FALSE);
1458 # else
1459 # if defined(__MWERKS__)
1460 # if !__POWERPC__
1461 extern void* GC_MacGetDataStart(void);
1462 /* MATTHEW: Function to handle Far Globals (CW Pro 3) */
1463 # if __option(far_data)
1464 extern void* GC_MacGetDataEnd(void);
1465 # endif
1466 /* globals begin above stack and end at a5. */
1467 GC_add_roots_inner((ptr_t)GC_MacGetDataStart(),
1468 (ptr_t)LMGetCurrentA5(), FALSE);
1469 /* MATTHEW: Handle Far Globals */
1470 # if __option(far_data)
1471 /* Far globals follow he QD globals: */
1472 GC_add_roots_inner((ptr_t)LMGetCurrentA5(),
1473 (ptr_t)GC_MacGetDataEnd(), FALSE);
1474 # endif
1475 # else
1476 extern char __data_start__[], __data_end__[];
1477 GC_add_roots_inner((ptr_t)&__data_start__,
1478 (ptr_t)&__data_end__, FALSE);
1479 # endif /* __POWERPC__ */
1480 # endif /* __MWERKS__ */
1481 # endif /* !THINK_C */
1483 # endif /* MACOS */
1485 /* Dynamic libraries are added at every collection, since they may */
1486 /* change. */
1489 # endif /* ! AMIGA */
1490 # endif /* ! MSWIN32 && ! MSWINCE*/
1491 # endif /* ! OS2 */
1494 * Auxiliary routines for obtaining memory from OS.
1497 # if !defined(OS2) && !defined(PCR) && !defined(AMIGA) \
1498 && !defined(MSWIN32) && !defined(MSWINCE) \
1499 && !defined(MACOS) && !defined(DOS4GW)
1501 # ifdef SUNOS4
1502 extern caddr_t sbrk();
1503 # endif
1504 # ifdef __STDC__
1505 # define SBRK_ARG_T ptrdiff_t
1506 # else
1507 # define SBRK_ARG_T int
1508 # endif
1511 # ifdef RS6000
1512 /* The compiler seems to generate speculative reads one past the end of */
1513 /* an allocated object. Hence we need to make sure that the page */
1514 /* following the last heap page is also mapped. */
1515 ptr_t GC_unix_get_mem(bytes)
1516 word bytes;
1518 caddr_t cur_brk = (caddr_t)sbrk(0);
1519 caddr_t result;
1520 SBRK_ARG_T lsbs = (word)cur_brk & (GC_page_size-1);
1521 static caddr_t my_brk_val = 0;
1523 if ((SBRK_ARG_T)bytes < 0) return(0); /* too big */
1524 if (lsbs != 0) {
1525 if((caddr_t)(sbrk(GC_page_size - lsbs)) == (caddr_t)(-1)) return(0);
1527 if (cur_brk == my_brk_val) {
1528 /* Use the extra block we allocated last time. */
1529 result = (ptr_t)sbrk((SBRK_ARG_T)bytes);
1530 if (result == (caddr_t)(-1)) return(0);
1531 result -= GC_page_size;
1532 } else {
1533 result = (ptr_t)sbrk(GC_page_size + (SBRK_ARG_T)bytes);
1534 if (result == (caddr_t)(-1)) return(0);
1536 my_brk_val = result + bytes + GC_page_size; /* Always page aligned */
1537 return((ptr_t)result);
1540 #else /* Not RS6000 */
1542 #if defined(USE_MMAP) || defined(USE_MUNMAP)
1544 #ifdef USE_MMAP_FIXED
1545 # define GC_MMAP_FLAGS MAP_FIXED | MAP_PRIVATE
1546 /* Seems to yield better performance on Solaris 2, but can */
1547 /* be unreliable if something is already mapped at the address. */
1548 #else
1549 # define GC_MMAP_FLAGS MAP_PRIVATE
1550 #endif
1552 #ifdef USE_MMAP_ANON
1553 # define zero_fd -1
1554 # if defined(MAP_ANONYMOUS)
1555 # define OPT_MAP_ANON MAP_ANONYMOUS
1556 # else
1557 # define OPT_MAP_ANON MAP_ANON
1558 # endif
1559 #else
1560 static int zero_fd;
1561 # define OPT_MAP_ANON 0
1562 #endif
1564 #endif /* defined(USE_MMAP) || defined(USE_MUNMAP) */
1566 #if defined(USE_MMAP)
1567 /* Tested only under Linux, IRIX5 and Solaris 2 */
1569 #ifndef HEAP_START
1570 # define HEAP_START 0
1571 #endif
1573 ptr_t GC_unix_get_mem(bytes)
1574 word bytes;
1576 void *result;
1577 static ptr_t last_addr = HEAP_START;
1579 # ifndef USE_MMAP_ANON
1580 static GC_bool initialized = FALSE;
1582 if (!initialized) {
1583 zero_fd = open("/dev/zero", O_RDONLY);
1584 fcntl(zero_fd, F_SETFD, FD_CLOEXEC);
1585 initialized = TRUE;
1587 # endif
1589 if (bytes & (GC_page_size -1)) ABORT("Bad GET_MEM arg");
1590 result = mmap(last_addr, bytes, PROT_READ | PROT_WRITE | OPT_PROT_EXEC,
1591 GC_MMAP_FLAGS | OPT_MAP_ANON, zero_fd, 0/* offset */);
1592 if (result == MAP_FAILED) return(0);
1593 last_addr = (ptr_t)result + bytes + GC_page_size - 1;
1594 last_addr = (ptr_t)((word)last_addr & ~(GC_page_size - 1));
1595 # if !defined(LINUX)
1596 if (last_addr == 0) {
1597 /* Oops. We got the end of the address space. This isn't */
1598 /* usable by arbitrary C code, since one-past-end pointers */
1599 /* don't work, so we discard it and try again. */
1600 munmap(result, (size_t)(-GC_page_size) - (size_t)result);
1601 /* Leave last page mapped, so we can't repeat. */
1602 return GC_unix_get_mem(bytes);
1604 # else
1605 GC_ASSERT(last_addr != 0);
1606 # endif
1607 return((ptr_t)result);
1610 #else /* Not RS6000, not USE_MMAP */
1611 ptr_t GC_unix_get_mem(bytes)
1612 word bytes;
1614 ptr_t result;
1615 # ifdef IRIX5
1616 /* Bare sbrk isn't thread safe. Play by malloc rules. */
1617 /* The equivalent may be needed on other systems as well. */
1618 __LOCK_MALLOC();
1619 # endif
1621 ptr_t cur_brk = (ptr_t)sbrk(0);
1622 SBRK_ARG_T lsbs = (word)cur_brk & (GC_page_size-1);
1624 if ((SBRK_ARG_T)bytes < 0) return(0); /* too big */
1625 if (lsbs != 0) {
1626 if((ptr_t)sbrk(GC_page_size - lsbs) == (ptr_t)(-1)) return(0);
1628 result = (ptr_t)sbrk((SBRK_ARG_T)bytes);
1629 if (result == (ptr_t)(-1)) result = 0;
1631 # ifdef IRIX5
1632 __UNLOCK_MALLOC();
1633 # endif
1634 return(result);
1637 #endif /* Not USE_MMAP */
1638 #endif /* Not RS6000 */
1640 # endif /* UN*X */
1642 # ifdef OS2
1644 void * os2_alloc(size_t bytes)
1646 void * result;
1648 if (DosAllocMem(&result, bytes, PAG_EXECUTE | PAG_READ |
1649 PAG_WRITE | PAG_COMMIT)
1650 != NO_ERROR) {
1651 return(0);
1653 if (result == 0) return(os2_alloc(bytes));
1654 return(result);
1657 # endif /* OS2 */
1660 # if defined(MSWIN32) || defined(MSWINCE)
1661 SYSTEM_INFO GC_sysinfo;
1662 # endif
1664 # ifdef MSWIN32
1666 # ifdef USE_GLOBAL_ALLOC
1667 # define GLOBAL_ALLOC_TEST 1
1668 # else
1669 # define GLOBAL_ALLOC_TEST GC_no_win32_dlls
1670 # endif
1672 word GC_n_heap_bases = 0;
1674 ptr_t GC_win32_get_mem(bytes)
1675 word bytes;
1677 ptr_t result;
1679 if (GLOBAL_ALLOC_TEST) {
1680 /* VirtualAlloc doesn't like PAGE_EXECUTE_READWRITE. */
1681 /* There are also unconfirmed rumors of other */
1682 /* problems, so we dodge the issue. */
1683 result = (ptr_t) GlobalAlloc(0, bytes + HBLKSIZE);
1684 result = (ptr_t)(((word)result + HBLKSIZE) & ~(HBLKSIZE-1));
1685 } else {
1686 /* VirtualProtect only works on regions returned by a */
1687 /* single VirtualAlloc call. Thus we allocate one */
1688 /* extra page, which will prevent merging of blocks */
1689 /* in separate regions, and eliminate any temptation */
1690 /* to call VirtualProtect on a range spanning regions. */
1691 /* This wastes a small amount of memory, and risks */
1692 /* increased fragmentation. But better alternatives */
1693 /* would require effort. */
1694 result = (ptr_t) VirtualAlloc(NULL, bytes + 1,
1695 MEM_COMMIT | MEM_RESERVE,
1696 PAGE_EXECUTE_READWRITE);
1698 if (HBLKDISPL(result) != 0) ABORT("Bad VirtualAlloc result");
1699 /* If I read the documentation correctly, this can */
1700 /* only happen if HBLKSIZE > 64k or not a power of 2. */
1701 if (GC_n_heap_bases >= MAX_HEAP_SECTS) ABORT("Too many heap sections");
1702 GC_heap_bases[GC_n_heap_bases++] = result;
1703 return(result);
1706 void GC_win32_free_heap ()
1708 if (GC_no_win32_dlls) {
1709 while (GC_n_heap_bases > 0) {
1710 GlobalFree (GC_heap_bases[--GC_n_heap_bases]);
1711 GC_heap_bases[GC_n_heap_bases] = 0;
1715 # endif
1717 #ifdef AMIGA
1718 # define GC_AMIGA_AM
1719 # include "AmigaOS.c"
1720 # undef GC_AMIGA_AM
1721 #endif
1724 # ifdef MSWINCE
1725 word GC_n_heap_bases = 0;
1727 ptr_t GC_wince_get_mem(bytes)
1728 word bytes;
1730 ptr_t result;
1731 word i;
1733 /* Round up allocation size to multiple of page size */
1734 bytes = (bytes + GC_page_size-1) & ~(GC_page_size-1);
1736 /* Try to find reserved, uncommitted pages */
1737 for (i = 0; i < GC_n_heap_bases; i++) {
1738 if (((word)(-(signed_word)GC_heap_lengths[i])
1739 & (GC_sysinfo.dwAllocationGranularity-1))
1740 >= bytes) {
1741 result = GC_heap_bases[i] + GC_heap_lengths[i];
1742 break;
1746 if (i == GC_n_heap_bases) {
1747 /* Reserve more pages */
1748 word res_bytes = (bytes + GC_sysinfo.dwAllocationGranularity-1)
1749 & ~(GC_sysinfo.dwAllocationGranularity-1);
1750 /* If we ever support MPROTECT_VDB here, we will probably need to */
1751 /* ensure that res_bytes is strictly > bytes, so that VirtualProtect */
1752 /* never spans regions. It seems to be OK for a VirtualFree argument */
1753 /* to span regions, so we should be OK for now. */
1754 result = (ptr_t) VirtualAlloc(NULL, res_bytes,
1755 MEM_RESERVE | MEM_TOP_DOWN,
1756 PAGE_EXECUTE_READWRITE);
1757 if (HBLKDISPL(result) != 0) ABORT("Bad VirtualAlloc result");
1758 /* If I read the documentation correctly, this can */
1759 /* only happen if HBLKSIZE > 64k or not a power of 2. */
1760 if (GC_n_heap_bases >= MAX_HEAP_SECTS) ABORT("Too many heap sections");
1761 GC_heap_bases[GC_n_heap_bases] = result;
1762 GC_heap_lengths[GC_n_heap_bases] = 0;
1763 GC_n_heap_bases++;
1766 /* Commit pages */
1767 result = (ptr_t) VirtualAlloc(result, bytes,
1768 MEM_COMMIT,
1769 PAGE_EXECUTE_READWRITE);
1770 if (result != NULL) {
1771 if (HBLKDISPL(result) != 0) ABORT("Bad VirtualAlloc result");
1772 GC_heap_lengths[i] += bytes;
1775 return(result);
1777 # endif
1779 #ifdef USE_MUNMAP
1781 /* For now, this only works on Win32/WinCE and some Unix-like */
1782 /* systems. If you have something else, don't define */
1783 /* USE_MUNMAP. */
1784 /* We assume ANSI C to support this feature. */
1786 #if !defined(MSWIN32) && !defined(MSWINCE)
1788 #include <unistd.h>
1789 #include <sys/mman.h>
1790 #include <sys/stat.h>
1791 #include <sys/types.h>
1793 #endif
1795 /* Compute a page aligned starting address for the unmap */
1796 /* operation on a block of size bytes starting at start. */
1797 /* Return 0 if the block is too small to make this feasible. */
1798 ptr_t GC_unmap_start(ptr_t start, word bytes)
1800 ptr_t result = start;
1801 /* Round start to next page boundary. */
1802 result += GC_page_size - 1;
1803 result = (ptr_t)((word)result & ~(GC_page_size - 1));
1804 if (result + GC_page_size > start + bytes) return 0;
1805 return result;
1808 /* Compute end address for an unmap operation on the indicated */
1809 /* block. */
1810 ptr_t GC_unmap_end(ptr_t start, word bytes)
1812 ptr_t end_addr = start + bytes;
1813 end_addr = (ptr_t)((word)end_addr & ~(GC_page_size - 1));
1814 return end_addr;
1817 /* Under Win32/WinCE we commit (map) and decommit (unmap) */
1818 /* memory using VirtualAlloc and VirtualFree. These functions */
1819 /* work on individual allocations of virtual memory, made */
1820 /* previously using VirtualAlloc with the MEM_RESERVE flag. */
1821 /* The ranges we need to (de)commit may span several of these */
1822 /* allocations; therefore we use VirtualQuery to check */
1823 /* allocation lengths, and split up the range as necessary. */
1825 /* We assume that GC_remap is called on exactly the same range */
1826 /* as a previous call to GC_unmap. It is safe to consistently */
1827 /* round the endpoints in both places. */
1828 void GC_unmap(ptr_t start, word bytes)
1830 ptr_t start_addr = GC_unmap_start(start, bytes);
1831 ptr_t end_addr = GC_unmap_end(start, bytes);
1832 word len = end_addr - start_addr;
1833 if (0 == start_addr) return;
1834 # if defined(MSWIN32) || defined(MSWINCE)
1835 while (len != 0) {
1836 MEMORY_BASIC_INFORMATION mem_info;
1837 GC_word free_len;
1838 if (VirtualQuery(start_addr, &mem_info, sizeof(mem_info))
1839 != sizeof(mem_info))
1840 ABORT("Weird VirtualQuery result");
1841 free_len = (len < mem_info.RegionSize) ? len : mem_info.RegionSize;
1842 if (!VirtualFree(start_addr, free_len, MEM_DECOMMIT))
1843 ABORT("VirtualFree failed");
1844 GC_unmapped_bytes += free_len;
1845 start_addr += free_len;
1846 len -= free_len;
1848 # else
1849 /* We immediately remap it to prevent an intervening mmap from */
1850 /* accidentally grabbing the same address space. */
1852 void * result;
1853 result = mmap(start_addr, len, PROT_NONE,
1854 MAP_PRIVATE | MAP_FIXED | OPT_MAP_ANON,
1855 zero_fd, 0/* offset */);
1856 if (result != (void *)start_addr) ABORT("mmap(...PROT_NONE...) failed");
1858 GC_unmapped_bytes += len;
1859 # endif
1863 void GC_remap(ptr_t start, word bytes)
1865 ptr_t start_addr = GC_unmap_start(start, bytes);
1866 ptr_t end_addr = GC_unmap_end(start, bytes);
1867 word len = end_addr - start_addr;
1869 # if defined(MSWIN32) || defined(MSWINCE)
1870 ptr_t result;
1872 if (0 == start_addr) return;
1873 while (len != 0) {
1874 MEMORY_BASIC_INFORMATION mem_info;
1875 GC_word alloc_len;
1876 if (VirtualQuery(start_addr, &mem_info, sizeof(mem_info))
1877 != sizeof(mem_info))
1878 ABORT("Weird VirtualQuery result");
1879 alloc_len = (len < mem_info.RegionSize) ? len : mem_info.RegionSize;
1880 result = VirtualAlloc(start_addr, alloc_len,
1881 MEM_COMMIT,
1882 PAGE_EXECUTE_READWRITE);
1883 if (result != start_addr) {
1884 ABORT("VirtualAlloc remapping failed");
1886 GC_unmapped_bytes -= alloc_len;
1887 start_addr += alloc_len;
1888 len -= alloc_len;
1890 # else
1891 /* It was already remapped with PROT_NONE. */
1892 int result;
1894 if (0 == start_addr) return;
1895 result = mprotect(start_addr, len,
1896 PROT_READ | PROT_WRITE | OPT_PROT_EXEC);
1897 if (result != 0) {
1898 GC_err_printf3(
1899 "Mprotect failed at 0x%lx (length %ld) with errno %ld\n",
1900 start_addr, len, errno);
1901 ABORT("Mprotect remapping failed");
1903 GC_unmapped_bytes -= len;
1904 # endif
1907 /* Two adjacent blocks have already been unmapped and are about to */
1908 /* be merged. Unmap the whole block. This typically requires */
1909 /* that we unmap a small section in the middle that was not previously */
1910 /* unmapped due to alignment constraints. */
1911 void GC_unmap_gap(ptr_t start1, word bytes1, ptr_t start2, word bytes2)
1913 ptr_t start1_addr = GC_unmap_start(start1, bytes1);
1914 ptr_t end1_addr = GC_unmap_end(start1, bytes1);
1915 ptr_t start2_addr = GC_unmap_start(start2, bytes2);
1916 ptr_t end2_addr = GC_unmap_end(start2, bytes2);
1917 ptr_t start_addr = end1_addr;
1918 ptr_t end_addr = start2_addr;
1919 word len;
1920 GC_ASSERT(start1 + bytes1 == start2);
1921 if (0 == start1_addr) start_addr = GC_unmap_start(start1, bytes1 + bytes2);
1922 if (0 == start2_addr) end_addr = GC_unmap_end(start1, bytes1 + bytes2);
1923 if (0 == start_addr) return;
1924 len = end_addr - start_addr;
1925 # if defined(MSWIN32) || defined(MSWINCE)
1926 while (len != 0) {
1927 MEMORY_BASIC_INFORMATION mem_info;
1928 GC_word free_len;
1929 if (VirtualQuery(start_addr, &mem_info, sizeof(mem_info))
1930 != sizeof(mem_info))
1931 ABORT("Weird VirtualQuery result");
1932 free_len = (len < mem_info.RegionSize) ? len : mem_info.RegionSize;
1933 if (!VirtualFree(start_addr, free_len, MEM_DECOMMIT))
1934 ABORT("VirtualFree failed");
1935 GC_unmapped_bytes += free_len;
1936 start_addr += free_len;
1937 len -= free_len;
1939 # else
1940 if (len != 0 && munmap(start_addr, len) != 0) ABORT("munmap failed");
1941 GC_unmapped_bytes += len;
1942 # endif
1945 #endif /* USE_MUNMAP */
1947 /* Routine for pushing any additional roots. In THREADS */
1948 /* environment, this is also responsible for marking from */
1949 /* thread stacks. */
1950 #ifndef THREADS
1951 void (*GC_push_other_roots)() = 0;
1952 #else /* THREADS */
1954 # ifdef PCR
1955 PCR_ERes GC_push_thread_stack(PCR_Th_T *t, PCR_Any dummy)
1957 struct PCR_ThCtl_TInfoRep info;
1958 PCR_ERes result;
1960 info.ti_stkLow = info.ti_stkHi = 0;
1961 result = PCR_ThCtl_GetInfo(t, &info);
1962 GC_push_all_stack((ptr_t)(info.ti_stkLow), (ptr_t)(info.ti_stkHi));
1963 return(result);
1966 /* Push the contents of an old object. We treat this as stack */
1967 /* data only becasue that makes it robust against mark stack */
1968 /* overflow. */
1969 PCR_ERes GC_push_old_obj(void *p, size_t size, PCR_Any data)
1971 GC_push_all_stack((ptr_t)p, (ptr_t)p + size);
1972 return(PCR_ERes_okay);
1976 void GC_default_push_other_roots GC_PROTO((void))
1978 /* Traverse data allocated by previous memory managers. */
1980 extern struct PCR_MM_ProcsRep * GC_old_allocator;
1982 if ((*(GC_old_allocator->mmp_enumerate))(PCR_Bool_false,
1983 GC_push_old_obj, 0)
1984 != PCR_ERes_okay) {
1985 ABORT("Old object enumeration failed");
1988 /* Traverse all thread stacks. */
1989 if (PCR_ERes_IsErr(
1990 PCR_ThCtl_ApplyToAllOtherThreads(GC_push_thread_stack,0))
1991 || PCR_ERes_IsErr(GC_push_thread_stack(PCR_Th_CurrThread(), 0))) {
1992 ABORT("Thread stack marking failed\n");
1996 # endif /* PCR */
1998 # ifdef SRC_M3
2000 # ifdef ALL_INTERIOR_POINTERS
2001 --> misconfigured
2002 # endif
2004 void GC_push_thread_structures GC_PROTO((void))
2006 /* Not our responsibibility. */
2009 extern void ThreadF__ProcessStacks();
2011 void GC_push_thread_stack(start, stop)
2012 word start, stop;
2014 GC_push_all_stack((ptr_t)start, (ptr_t)stop + sizeof(word));
2017 /* Push routine with M3 specific calling convention. */
2018 GC_m3_push_root(dummy1, p, dummy2, dummy3)
2019 word *p;
2020 ptr_t dummy1, dummy2;
2021 int dummy3;
2023 word q = *p;
2025 GC_PUSH_ONE_STACK(q, p);
2028 /* M3 set equivalent to RTHeap.TracedRefTypes */
2029 typedef struct { int elts[1]; } RefTypeSet;
2030 RefTypeSet GC_TracedRefTypes = {{0x1}};
2032 void GC_default_push_other_roots GC_PROTO((void))
2034 /* Use the M3 provided routine for finding static roots. */
2035 /* This is a bit dubious, since it presumes no C roots. */
2036 /* We handle the collector roots explicitly in GC_push_roots */
2037 RTMain__GlobalMapProc(GC_m3_push_root, 0, GC_TracedRefTypes);
2038 if (GC_words_allocd > 0) {
2039 ThreadF__ProcessStacks(GC_push_thread_stack);
2041 /* Otherwise this isn't absolutely necessary, and we have */
2042 /* startup ordering problems. */
2045 # endif /* SRC_M3 */
2047 # if defined(GC_SOLARIS_THREADS) || defined(GC_PTHREADS) || \
2048 defined(GC_WIN32_THREADS)
2050 extern void GC_push_all_stacks();
2052 void GC_default_push_other_roots GC_PROTO((void))
2054 GC_push_all_stacks();
2057 # endif /* GC_SOLARIS_THREADS || GC_PTHREADS */
2059 void (*GC_push_other_roots) GC_PROTO((void)) = GC_default_push_other_roots;
2061 #endif /* THREADS */
2064 * Routines for accessing dirty bits on virtual pages.
2065 * We plan to eventually implement four strategies for doing so:
2066 * DEFAULT_VDB: A simple dummy implementation that treats every page
2067 * as possibly dirty. This makes incremental collection
2068 * useless, but the implementation is still correct.
2069 * PCR_VDB: Use PPCRs virtual dirty bit facility.
2070 * PROC_VDB: Use the /proc facility for reading dirty bits. Only
2071 * works under some SVR4 variants. Even then, it may be
2072 * too slow to be entirely satisfactory. Requires reading
2073 * dirty bits for entire address space. Implementations tend
2074 * to assume that the client is a (slow) debugger.
2075 * MPROTECT_VDB:Protect pages and then catch the faults to keep track of
2076 * dirtied pages. The implementation (and implementability)
2077 * is highly system dependent. This usually fails when system
2078 * calls write to a protected page. We prevent the read system
2079 * call from doing so. It is the clients responsibility to
2080 * make sure that other system calls are similarly protected
2081 * or write only to the stack.
2083 GC_bool GC_dirty_maintained = FALSE;
2085 # ifdef DEFAULT_VDB
2087 /* All of the following assume the allocation lock is held, and */
2088 /* signals are disabled. */
2090 /* The client asserts that unallocated pages in the heap are never */
2091 /* written. */
2093 /* Initialize virtual dirty bit implementation. */
2094 void GC_dirty_init()
2096 # ifdef PRINTSTATS
2097 GC_printf0("Initializing DEFAULT_VDB...\n");
2098 # endif
2099 GC_dirty_maintained = TRUE;
2102 /* Retrieve system dirty bits for heap to a local buffer. */
2103 /* Restore the systems notion of which pages are dirty. */
2104 void GC_read_dirty()
2107 /* Is the HBLKSIZE sized page at h marked dirty in the local buffer? */
2108 /* If the actual page size is different, this returns TRUE if any */
2109 /* of the pages overlapping h are dirty. This routine may err on the */
2110 /* side of labelling pages as dirty (and this implementation does). */
2111 /*ARGSUSED*/
2112 GC_bool GC_page_was_dirty(h)
2113 struct hblk *h;
2115 return(TRUE);
2119 * The following two routines are typically less crucial. They matter
2120 * most with large dynamic libraries, or if we can't accurately identify
2121 * stacks, e.g. under Solaris 2.X. Otherwise the following default
2122 * versions are adequate.
2125 /* Could any valid GC heap pointer ever have been written to this page? */
2126 /*ARGSUSED*/
2127 GC_bool GC_page_was_ever_dirty(h)
2128 struct hblk *h;
2130 return(TRUE);
2133 /* Reset the n pages starting at h to "was never dirty" status. */
2134 void GC_is_fresh(h, n)
2135 struct hblk *h;
2136 word n;
2140 /* A call that: */
2141 /* I) hints that [h, h+nblocks) is about to be written. */
2142 /* II) guarantees that protection is removed. */
2143 /* (I) may speed up some dirty bit implementations. */
2144 /* (II) may be essential if we need to ensure that */
2145 /* pointer-free system call buffers in the heap are */
2146 /* not protected. */
2147 /*ARGSUSED*/
2148 void GC_remove_protection(h, nblocks, is_ptrfree)
2149 struct hblk *h;
2150 word nblocks;
2151 GC_bool is_ptrfree;
2155 # endif /* DEFAULT_VDB */
2158 # ifdef MPROTECT_VDB
2161 * See DEFAULT_VDB for interface descriptions.
2165 * This implementation maintains dirty bits itself by catching write
2166 * faults and keeping track of them. We assume nobody else catches
2167 * SIGBUS or SIGSEGV. We assume no write faults occur in system calls.
2168 * This means that clients must ensure that system calls don't write
2169 * to the write-protected heap. Probably the best way to do this is to
2170 * ensure that system calls write at most to POINTERFREE objects in the
2171 * heap, and do even that only if we are on a platform on which those
2172 * are not protected. Another alternative is to wrap system calls
2173 * (see example for read below), but the current implementation holds
2174 * a lock across blocking calls, making it problematic for multithreaded
2175 * applications.
2176 * We assume the page size is a multiple of HBLKSIZE.
2177 * We prefer them to be the same. We avoid protecting POINTERFREE
2178 * objects only if they are the same.
2181 # if !defined(MSWIN32) && !defined(MSWINCE) && !defined(DARWIN)
2183 # include <sys/mman.h>
2184 # include <signal.h>
2185 # include <sys/syscall.h>
2187 # define PROTECT(addr, len) \
2188 if (mprotect((caddr_t)(addr), (size_t)(len), \
2189 PROT_READ | OPT_PROT_EXEC) < 0) { \
2190 ABORT("mprotect failed"); \
2192 # define UNPROTECT(addr, len) \
2193 if (mprotect((caddr_t)(addr), (size_t)(len), \
2194 PROT_WRITE | PROT_READ | OPT_PROT_EXEC ) < 0) { \
2195 ABORT("un-mprotect failed"); \
2198 # else
2200 # ifdef DARWIN
2201 /* Using vm_protect (mach syscall) over mprotect (BSD syscall) seems to
2202 decrease the likelihood of some of the problems described below. */
2203 #include <mach/vm_map.h>
2204 static mach_port_t GC_task_self;
2205 #define PROTECT(addr,len) \
2206 if(vm_protect(GC_task_self,(vm_address_t)(addr),(vm_size_t)(len), \
2207 FALSE,VM_PROT_READ) != KERN_SUCCESS) { \
2208 ABORT("vm_portect failed"); \
2210 #define UNPROTECT(addr,len) \
2211 if(vm_protect(GC_task_self,(vm_address_t)(addr),(vm_size_t)(len), \
2212 FALSE,VM_PROT_READ|VM_PROT_WRITE) != KERN_SUCCESS) { \
2213 ABORT("vm_portect failed"); \
2215 # else
2217 # ifndef MSWINCE
2218 # include <signal.h>
2219 # endif
2221 static DWORD protect_junk;
2222 # define PROTECT(addr, len) \
2223 if (!VirtualProtect((addr), (len), PAGE_EXECUTE_READ, \
2224 &protect_junk)) { \
2225 DWORD last_error = GetLastError(); \
2226 GC_printf1("Last error code: %lx\n", last_error); \
2227 ABORT("VirtualProtect failed"); \
2229 # define UNPROTECT(addr, len) \
2230 if (!VirtualProtect((addr), (len), PAGE_EXECUTE_READWRITE, \
2231 &protect_junk)) { \
2232 ABORT("un-VirtualProtect failed"); \
2234 # endif /* !DARWIN */
2235 # endif /* MSWIN32 || MSWINCE || DARWIN */
2237 #if defined(SUNOS4) || (defined(FREEBSD) && !defined(SUNOS5SIGS))
2238 typedef void (* SIG_PF)();
2239 #endif /* SUNOS4 || (FREEBSD && !SUNOS5SIGS) */
2241 #if defined(SUNOS5SIGS) || defined(OSF1) || defined(LINUX) \
2242 || defined(HURD)
2243 # ifdef __STDC__
2244 typedef void (* SIG_PF)(int);
2245 # else
2246 typedef void (* SIG_PF)();
2247 # endif
2248 #endif /* SUNOS5SIGS || OSF1 || LINUX || HURD */
2250 #if defined(MSWIN32)
2251 typedef LPTOP_LEVEL_EXCEPTION_FILTER SIG_PF;
2252 # undef SIG_DFL
2253 # define SIG_DFL (LPTOP_LEVEL_EXCEPTION_FILTER) (-1)
2254 #endif
2255 #if defined(MSWINCE)
2256 typedef LONG (WINAPI *SIG_PF)(struct _EXCEPTION_POINTERS *);
2257 # undef SIG_DFL
2258 # define SIG_DFL (SIG_PF) (-1)
2259 #endif
2261 #if defined(IRIX5) || defined(OSF1) || defined(HURD)
2262 typedef void (* REAL_SIG_PF)(int, int, struct sigcontext *);
2263 #endif /* IRIX5 || OSF1 || HURD */
2265 #if defined(SUNOS5SIGS)
2266 # if defined(HPUX) || defined(FREEBSD)
2267 # define SIGINFO_T siginfo_t
2268 # else
2269 # define SIGINFO_T struct siginfo
2270 # endif
2271 # ifdef __STDC__
2272 typedef void (* REAL_SIG_PF)(int, SIGINFO_T *, void *);
2273 # else
2274 typedef void (* REAL_SIG_PF)();
2275 # endif
2276 #endif /* SUNOS5SIGS */
2278 #if defined(LINUX)
2279 # if __GLIBC__ > 2 || __GLIBC__ == 2 && __GLIBC_MINOR__ >= 2
2280 typedef struct sigcontext s_c;
2281 # else /* glibc < 2.2 */
2282 # include <linux/version.h>
2283 # if (LINUX_VERSION_CODE >= 0x20100) && !defined(M68K) || defined(ALPHA) || defined(ARM32)
2284 typedef struct sigcontext s_c;
2285 # else
2286 typedef struct sigcontext_struct s_c;
2287 # endif
2288 # endif /* glibc < 2.2 */
2289 # if defined(ALPHA) || defined(M68K)
2290 typedef void (* REAL_SIG_PF)(int, int, s_c *);
2291 # else
2292 # if defined(IA64) || defined(HP_PA) || defined(X86_64)
2293 typedef void (* REAL_SIG_PF)(int, siginfo_t *, s_c *);
2294 /* FIXME: */
2295 /* According to SUSV3, the last argument should have type */
2296 /* void * or ucontext_t * */
2297 # else
2298 typedef void (* REAL_SIG_PF)(int, s_c);
2299 # endif
2300 # endif
2301 # ifdef ALPHA
2302 /* Retrieve fault address from sigcontext structure by decoding */
2303 /* instruction. */
2304 char * get_fault_addr(s_c *sc) {
2305 unsigned instr;
2306 word faultaddr;
2308 instr = *((unsigned *)(sc->sc_pc));
2309 faultaddr = sc->sc_regs[(instr >> 16) & 0x1f];
2310 faultaddr += (word) (((int)instr << 16) >> 16);
2311 return (char *)faultaddr;
2313 # endif /* !ALPHA */
2314 # endif /* LINUX */
2316 #ifndef DARWIN
2317 SIG_PF GC_old_bus_handler;
2318 SIG_PF GC_old_segv_handler; /* Also old MSWIN32 ACCESS_VIOLATION filter */
2319 #endif /* !DARWIN */
2321 #if defined(THREADS)
2322 /* We need to lock around the bitmap update in the write fault handler */
2323 /* in order to avoid the risk of losing a bit. We do this with a */
2324 /* test-and-set spin lock if we know how to do that. Otherwise we */
2325 /* check whether we are already in the handler and use the dumb but */
2326 /* safe fallback algorithm of setting all bits in the word. */
2327 /* Contention should be very rare, so we do the minimum to handle it */
2328 /* correctly. */
2329 #ifdef GC_TEST_AND_SET_DEFINED
2330 static VOLATILE unsigned int fault_handler_lock = 0;
2331 void async_set_pht_entry_from_index(VOLATILE page_hash_table db, int index) {
2332 while (GC_test_and_set(&fault_handler_lock)) {}
2333 /* Could also revert to set_pht_entry_from_index_safe if initial */
2334 /* GC_test_and_set fails. */
2335 set_pht_entry_from_index(db, index);
2336 GC_clear(&fault_handler_lock);
2338 #else /* !GC_TEST_AND_SET_DEFINED */
2339 /* THIS IS INCORRECT! The dirty bit vector may be temporarily wrong, */
2340 /* just before we notice the conflict and correct it. We may end up */
2341 /* looking at it while it's wrong. But this requires contention */
2342 /* exactly when a GC is triggered, which seems far less likely to */
2343 /* fail than the old code, which had no reported failures. Thus we */
2344 /* leave it this way while we think of something better, or support */
2345 /* GC_test_and_set on the remaining platforms. */
2346 static VOLATILE word currently_updating = 0;
2347 void async_set_pht_entry_from_index(VOLATILE page_hash_table db, int index) {
2348 unsigned int update_dummy;
2349 currently_updating = (word)(&update_dummy);
2350 set_pht_entry_from_index(db, index);
2351 /* If we get contention in the 10 or so instruction window here, */
2352 /* and we get stopped by a GC between the two updates, we lose! */
2353 if (currently_updating != (word)(&update_dummy)) {
2354 set_pht_entry_from_index_safe(db, index);
2355 /* We claim that if two threads concurrently try to update the */
2356 /* dirty bit vector, the first one to execute UPDATE_START */
2357 /* will see it changed when UPDATE_END is executed. (Note that */
2358 /* &update_dummy must differ in two distinct threads.) It */
2359 /* will then execute set_pht_entry_from_index_safe, thus */
2360 /* returning us to a safe state, though not soon enough. */
2363 #endif /* !GC_TEST_AND_SET_DEFINED */
2364 #else /* !THREADS */
2365 # define async_set_pht_entry_from_index(db, index) \
2366 set_pht_entry_from_index(db, index)
2367 #endif /* !THREADS */
2369 /*ARGSUSED*/
2370 #if !defined(DARWIN)
2371 # if defined (SUNOS4) || (defined(FREEBSD) && !defined(SUNOS5SIGS))
2372 void GC_write_fault_handler(sig, code, scp, addr)
2373 int sig, code;
2374 struct sigcontext *scp;
2375 char * addr;
2376 # ifdef SUNOS4
2377 # define SIG_OK (sig == SIGSEGV || sig == SIGBUS)
2378 # define CODE_OK (FC_CODE(code) == FC_PROT \
2379 || (FC_CODE(code) == FC_OBJERR \
2380 && FC_ERRNO(code) == FC_PROT))
2381 # endif
2382 # ifdef FREEBSD
2383 # define SIG_OK (sig == SIGBUS)
2384 # define CODE_OK (code == BUS_PAGE_FAULT)
2385 # endif
2386 # endif /* SUNOS4 || (FREEBSD && !SUNOS5SIGS) */
2388 # if defined(IRIX5) || defined(OSF1) || defined(HURD)
2389 # include <errno.h>
2390 void GC_write_fault_handler(int sig, int code, struct sigcontext *scp)
2391 # ifdef OSF1
2392 # define SIG_OK (sig == SIGSEGV)
2393 # define CODE_OK (code == 2 /* experimentally determined */)
2394 # endif
2395 # ifdef IRIX5
2396 # define SIG_OK (sig == SIGSEGV)
2397 # define CODE_OK (code == EACCES)
2398 # endif
2399 # ifdef HURD
2400 # define SIG_OK (sig == SIGBUS || sig == SIGSEGV)
2401 # define CODE_OK TRUE
2402 # endif
2403 # endif /* IRIX5 || OSF1 || HURD */
2405 # if defined(LINUX)
2406 # if defined(ALPHA) || defined(M68K)
2407 void GC_write_fault_handler(int sig, int code, s_c * sc)
2408 # else
2409 # if defined(IA64) || defined(HP_PA) || defined(X86_64)
2410 void GC_write_fault_handler(int sig, siginfo_t * si, s_c * scp)
2411 # else
2412 # if defined(ARM32)
2413 void GC_write_fault_handler(int sig, int a2, int a3, int a4, s_c sc)
2414 # else
2415 void GC_write_fault_handler(int sig, s_c sc)
2416 # endif
2417 # endif
2418 # endif
2419 # define SIG_OK (sig == SIGSEGV)
2420 # define CODE_OK TRUE
2421 /* Empirically c.trapno == 14, on IA32, but is that useful? */
2422 /* Should probably consider alignment issues on other */
2423 /* architectures. */
2424 # endif /* LINUX */
2426 # if defined(SUNOS5SIGS)
2427 # ifdef __STDC__
2428 void GC_write_fault_handler(int sig, SIGINFO_T *scp, void * context)
2429 # else
2430 void GC_write_fault_handler(sig, scp, context)
2431 int sig;
2432 SIGINFO_T *scp;
2433 void * context;
2434 # endif
2435 # ifdef HPUX
2436 # define SIG_OK (sig == SIGSEGV || sig == SIGBUS)
2437 # define CODE_OK (scp -> si_code == SEGV_ACCERR) \
2438 || (scp -> si_code == BUS_ADRERR) \
2439 || (scp -> si_code == BUS_UNKNOWN) \
2440 || (scp -> si_code == SEGV_UNKNOWN) \
2441 || (scp -> si_code == BUS_OBJERR)
2442 # else
2443 # ifdef FREEBSD
2444 # define SIG_OK (sig == SIGBUS)
2445 # define CODE_OK (scp -> si_code == BUS_PAGE_FAULT)
2446 # else
2447 # define SIG_OK (sig == SIGSEGV)
2448 # define CODE_OK (scp -> si_code == SEGV_ACCERR)
2449 # endif
2450 # endif
2451 # endif /* SUNOS5SIGS */
2453 # if defined(MSWIN32) || defined(MSWINCE)
2454 LONG WINAPI GC_write_fault_handler(struct _EXCEPTION_POINTERS *exc_info)
2455 # define SIG_OK (exc_info -> ExceptionRecord -> ExceptionCode == \
2456 STATUS_ACCESS_VIOLATION)
2457 # define CODE_OK (exc_info -> ExceptionRecord -> ExceptionInformation[0] == 1)
2458 /* Write fault */
2459 # endif /* MSWIN32 || MSWINCE */
2461 register unsigned i;
2462 # if defined(HURD)
2463 char *addr = (char *) code;
2464 # endif
2465 # ifdef IRIX5
2466 char * addr = (char *) (size_t) (scp -> sc_badvaddr);
2467 # endif
2468 # if defined(OSF1) && defined(ALPHA)
2469 char * addr = (char *) (scp -> sc_traparg_a0);
2470 # endif
2471 # ifdef SUNOS5SIGS
2472 char * addr = (char *) (scp -> si_addr);
2473 # endif
2474 # ifdef LINUX
2475 # if defined(I386)
2476 char * addr = (char *) (sc.cr2);
2477 # else
2478 # if defined(M68K)
2479 char * addr = NULL;
2481 struct sigcontext *scp = (struct sigcontext *)(sc);
2483 int format = (scp->sc_formatvec >> 12) & 0xf;
2484 unsigned long *framedata = (unsigned long *)(scp + 1);
2485 unsigned long ea;
2487 if (format == 0xa || format == 0xb) {
2488 /* 68020/030 */
2489 ea = framedata[2];
2490 } else if (format == 7) {
2491 /* 68040 */
2492 ea = framedata[3];
2493 if (framedata[1] & 0x08000000) {
2494 /* correct addr on misaligned access */
2495 ea = (ea+4095)&(~4095);
2497 } else if (format == 4) {
2498 /* 68060 */
2499 ea = framedata[0];
2500 if (framedata[1] & 0x08000000) {
2501 /* correct addr on misaligned access */
2502 ea = (ea+4095)&(~4095);
2505 addr = (char *)ea;
2506 # else
2507 # ifdef ALPHA
2508 char * addr = get_fault_addr(sc);
2509 # else
2510 # if defined(IA64) || defined(HP_PA) || defined(X86_64)
2511 char * addr = si -> si_addr;
2512 /* I believe this is claimed to work on all platforms for */
2513 /* Linux 2.3.47 and later. Hopefully we don't have to */
2514 /* worry about earlier kernels on IA64. */
2515 # else
2516 # if defined(POWERPC)
2517 char * addr = (char *) (sc.regs->dar);
2518 # else
2519 # if defined(ARM32)
2520 char * addr = (char *)sc.fault_address;
2521 # else
2522 # if defined(CRIS)
2523 char * addr = (char *)sc.regs.csraddr;
2524 # else
2525 --> architecture not supported
2526 # endif
2527 # endif
2528 # endif
2529 # endif
2530 # endif
2531 # endif
2532 # endif
2533 # endif
2534 # if defined(MSWIN32) || defined(MSWINCE)
2535 char * addr = (char *) (exc_info -> ExceptionRecord
2536 -> ExceptionInformation[1]);
2537 # define sig SIGSEGV
2538 # endif
2540 if (SIG_OK && CODE_OK) {
2541 register struct hblk * h =
2542 (struct hblk *)((word)addr & ~(GC_page_size-1));
2543 GC_bool in_allocd_block;
2545 # ifdef SUNOS5SIGS
2546 /* Address is only within the correct physical page. */
2547 in_allocd_block = FALSE;
2548 for (i = 0; i < divHBLKSZ(GC_page_size); i++) {
2549 if (HDR(h+i) != 0) {
2550 in_allocd_block = TRUE;
2553 # else
2554 in_allocd_block = (HDR(addr) != 0);
2555 # endif
2556 if (!in_allocd_block) {
2557 /* FIXME - We should make sure that we invoke the */
2558 /* old handler with the appropriate calling */
2559 /* sequence, which often depends on SA_SIGINFO. */
2561 /* Heap blocks now begin and end on page boundaries */
2562 SIG_PF old_handler;
2564 if (sig == SIGSEGV) {
2565 old_handler = GC_old_segv_handler;
2566 } else {
2567 old_handler = GC_old_bus_handler;
2569 if (old_handler == SIG_DFL) {
2570 # if !defined(MSWIN32) && !defined(MSWINCE)
2571 GC_err_printf1("Segfault at 0x%lx\n", addr);
2572 ABORT("Unexpected bus error or segmentation fault");
2573 # else
2574 return(EXCEPTION_CONTINUE_SEARCH);
2575 # endif
2576 } else {
2577 # if defined (SUNOS4) \
2578 || (defined(FREEBSD) && !defined(SUNOS5SIGS))
2579 (*old_handler) (sig, code, scp, addr);
2580 return;
2581 # endif
2582 # if defined (SUNOS5SIGS)
2584 * FIXME: For FreeBSD, this code should check if the
2585 * old signal handler used the traditional BSD style and
2586 * if so call it using that style.
2588 (*(REAL_SIG_PF)old_handler) (sig, scp, context);
2589 return;
2590 # endif
2591 # if defined (LINUX)
2592 # if defined(ALPHA) || defined(M68K)
2593 (*(REAL_SIG_PF)old_handler) (sig, code, sc);
2594 # else
2595 # if defined(IA64) || defined(HP_PA) || defined(X86_64)
2596 (*(REAL_SIG_PF)old_handler) (sig, si, scp);
2597 # else
2598 (*(REAL_SIG_PF)old_handler) (sig, sc);
2599 # endif
2600 # endif
2601 return;
2602 # endif
2603 # if defined (IRIX5) || defined(OSF1) || defined(HURD)
2604 (*(REAL_SIG_PF)old_handler) (sig, code, scp);
2605 return;
2606 # endif
2607 # ifdef MSWIN32
2608 return((*old_handler)(exc_info));
2609 # endif
2612 UNPROTECT(h, GC_page_size);
2613 /* We need to make sure that no collection occurs between */
2614 /* the UNPROTECT and the setting of the dirty bit. Otherwise */
2615 /* a write by a third thread might go unnoticed. Reversing */
2616 /* the order is just as bad, since we would end up unprotecting */
2617 /* a page in a GC cycle during which it's not marked. */
2618 /* Currently we do this by disabling the thread stopping */
2619 /* signals while this handler is running. An alternative might */
2620 /* be to record the fact that we're about to unprotect, or */
2621 /* have just unprotected a page in the GC's thread structure, */
2622 /* and then to have the thread stopping code set the dirty */
2623 /* flag, if necessary. */
2624 for (i = 0; i < divHBLKSZ(GC_page_size); i++) {
2625 register int index = PHT_HASH(h+i);
2627 async_set_pht_entry_from_index(GC_dirty_pages, index);
2629 # if defined(OSF1)
2630 /* These reset the signal handler each time by default. */
2631 signal(SIGSEGV, (SIG_PF) GC_write_fault_handler);
2632 # endif
2633 /* The write may not take place before dirty bits are read. */
2634 /* But then we'll fault again ... */
2635 # if defined(MSWIN32) || defined(MSWINCE)
2636 return(EXCEPTION_CONTINUE_EXECUTION);
2637 # else
2638 return;
2639 # endif
2641 #if defined(MSWIN32) || defined(MSWINCE)
2642 return EXCEPTION_CONTINUE_SEARCH;
2643 #else
2644 GC_err_printf1("Segfault at 0x%lx\n", addr);
2645 ABORT("Unexpected bus error or segmentation fault");
2646 #endif
2648 #endif /* !DARWIN */
2651 * We hold the allocation lock. We expect block h to be written
2652 * shortly. Ensure that all pages containing any part of the n hblks
2653 * starting at h are no longer protected. If is_ptrfree is false,
2654 * also ensure that they will subsequently appear to be dirty.
2656 void GC_remove_protection(h, nblocks, is_ptrfree)
2657 struct hblk *h;
2658 word nblocks;
2659 GC_bool is_ptrfree;
2661 struct hblk * h_trunc; /* Truncated to page boundary */
2662 struct hblk * h_end; /* Page boundary following block end */
2663 struct hblk * current;
2664 GC_bool found_clean;
2666 if (!GC_dirty_maintained) return;
2667 h_trunc = (struct hblk *)((word)h & ~(GC_page_size-1));
2668 h_end = (struct hblk *)(((word)(h + nblocks) + GC_page_size-1)
2669 & ~(GC_page_size-1));
2670 found_clean = FALSE;
2671 for (current = h_trunc; current < h_end; ++current) {
2672 int index = PHT_HASH(current);
2674 if (!is_ptrfree || current < h || current >= h + nblocks) {
2675 async_set_pht_entry_from_index(GC_dirty_pages, index);
2678 UNPROTECT(h_trunc, (ptr_t)h_end - (ptr_t)h_trunc);
2681 #if !defined(DARWIN)
2682 void GC_dirty_init()
2684 # if defined(SUNOS5SIGS) || defined(IRIX5) || defined(LINUX) || \
2685 defined(OSF1) || defined(HURD)
2686 struct sigaction act, oldact;
2687 /* We should probably specify SA_SIGINFO for Linux, and handle */
2688 /* the different architectures more uniformly. */
2689 # if defined(IRIX5) || defined(LINUX) && !defined(X86_64) \
2690 || defined(OSF1) || defined(HURD)
2691 act.sa_flags = SA_RESTART;
2692 act.sa_handler = (SIG_PF)GC_write_fault_handler;
2693 # else
2694 act.sa_flags = SA_RESTART | SA_SIGINFO;
2695 act.sa_sigaction = GC_write_fault_handler;
2696 # endif
2697 (void)sigemptyset(&act.sa_mask);
2698 # ifdef SIG_SUSPEND
2699 /* Arrange to postpone SIG_SUSPEND while we're in a write fault */
2700 /* handler. This effectively makes the handler atomic w.r.t. */
2701 /* stopping the world for GC. */
2702 (void)sigaddset(&act.sa_mask, SIG_SUSPEND);
2703 # endif /* SIG_SUSPEND */
2704 # endif
2705 # ifdef PRINTSTATS
2706 GC_printf0("Inititalizing mprotect virtual dirty bit implementation\n");
2707 # endif
2708 GC_dirty_maintained = TRUE;
2709 if (GC_page_size % HBLKSIZE != 0) {
2710 GC_err_printf0("Page size not multiple of HBLKSIZE\n");
2711 ABORT("Page size not multiple of HBLKSIZE");
2713 # if defined(SUNOS4) || (defined(FREEBSD) && !defined(SUNOS5SIGS))
2714 GC_old_bus_handler = signal(SIGBUS, GC_write_fault_handler);
2715 if (GC_old_bus_handler == SIG_IGN) {
2716 GC_err_printf0("Previously ignored bus error!?");
2717 GC_old_bus_handler = SIG_DFL;
2719 if (GC_old_bus_handler != SIG_DFL) {
2720 # ifdef PRINTSTATS
2721 GC_err_printf0("Replaced other SIGBUS handler\n");
2722 # endif
2724 # endif
2725 # if defined(SUNOS4)
2726 GC_old_segv_handler = signal(SIGSEGV, (SIG_PF)GC_write_fault_handler);
2727 if (GC_old_segv_handler == SIG_IGN) {
2728 GC_err_printf0("Previously ignored segmentation violation!?");
2729 GC_old_segv_handler = SIG_DFL;
2731 if (GC_old_segv_handler != SIG_DFL) {
2732 # ifdef PRINTSTATS
2733 GC_err_printf0("Replaced other SIGSEGV handler\n");
2734 # endif
2736 # endif
2737 # if (defined(SUNOS5SIGS) && !defined(FREEBSD)) || defined(IRIX5) \
2738 || defined(LINUX) || defined(OSF1) || defined(HURD)
2739 /* SUNOS5SIGS includes HPUX */
2740 # if defined(GC_IRIX_THREADS)
2741 sigaction(SIGSEGV, 0, &oldact);
2742 sigaction(SIGSEGV, &act, 0);
2743 # else
2745 int res = sigaction(SIGSEGV, &act, &oldact);
2746 if (res != 0) ABORT("Sigaction failed");
2748 # endif
2749 # if defined(_sigargs) || defined(HURD) || !defined(SA_SIGINFO)
2750 /* This is Irix 5.x, not 6.x. Irix 5.x does not have */
2751 /* sa_sigaction. */
2752 GC_old_segv_handler = oldact.sa_handler;
2753 # else /* Irix 6.x or SUNOS5SIGS or LINUX */
2754 if (oldact.sa_flags & SA_SIGINFO) {
2755 GC_old_segv_handler = (SIG_PF)(oldact.sa_sigaction);
2756 } else {
2757 GC_old_segv_handler = oldact.sa_handler;
2759 # endif
2760 if (GC_old_segv_handler == SIG_IGN) {
2761 GC_err_printf0("Previously ignored segmentation violation!?");
2762 GC_old_segv_handler = SIG_DFL;
2764 if (GC_old_segv_handler != SIG_DFL) {
2765 # ifdef PRINTSTATS
2766 GC_err_printf0("Replaced other SIGSEGV handler\n");
2767 # endif
2769 # endif /* (SUNOS5SIGS && !FREEBSD) || IRIX5 || LINUX || OSF1 || HURD */
2770 # if defined(HPUX) || defined(LINUX) || defined(HURD) \
2771 || (defined(FREEBSD) && defined(SUNOS5SIGS))
2772 sigaction(SIGBUS, &act, &oldact);
2773 GC_old_bus_handler = oldact.sa_handler;
2774 if (GC_old_bus_handler == SIG_IGN) {
2775 GC_err_printf0("Previously ignored bus error!?");
2776 GC_old_bus_handler = SIG_DFL;
2778 if (GC_old_bus_handler != SIG_DFL) {
2779 # ifdef PRINTSTATS
2780 GC_err_printf0("Replaced other SIGBUS handler\n");
2781 # endif
2783 # endif /* HPUX || LINUX || HURD || (FREEBSD && SUNOS5SIGS) */
2784 # if defined(MSWIN32)
2785 GC_old_segv_handler = SetUnhandledExceptionFilter(GC_write_fault_handler);
2786 if (GC_old_segv_handler != NULL) {
2787 # ifdef PRINTSTATS
2788 GC_err_printf0("Replaced other UnhandledExceptionFilter\n");
2789 # endif
2790 } else {
2791 GC_old_segv_handler = SIG_DFL;
2793 # endif
2795 #endif /* !DARWIN */
2797 int GC_incremental_protection_needs()
2799 if (GC_page_size == HBLKSIZE) {
2800 return GC_PROTECTS_POINTER_HEAP;
2801 } else {
2802 return GC_PROTECTS_POINTER_HEAP | GC_PROTECTS_PTRFREE_HEAP;
2806 #define HAVE_INCREMENTAL_PROTECTION_NEEDS
2808 #define IS_PTRFREE(hhdr) ((hhdr)->hb_descr == 0)
2810 #define PAGE_ALIGNED(x) !((word)(x) & (GC_page_size - 1))
2811 void GC_protect_heap()
2813 ptr_t start;
2814 word len;
2815 struct hblk * current;
2816 struct hblk * current_start; /* Start of block to be protected. */
2817 struct hblk * limit;
2818 unsigned i;
2819 GC_bool protect_all =
2820 (0 != (GC_incremental_protection_needs() & GC_PROTECTS_PTRFREE_HEAP));
2821 for (i = 0; i < GC_n_heap_sects; i++) {
2822 start = GC_heap_sects[i].hs_start;
2823 len = GC_heap_sects[i].hs_bytes;
2824 if (protect_all) {
2825 PROTECT(start, len);
2826 } else {
2827 GC_ASSERT(PAGE_ALIGNED(len))
2828 GC_ASSERT(PAGE_ALIGNED(start))
2829 current_start = current = (struct hblk *)start;
2830 limit = (struct hblk *)(start + len);
2831 while (current < limit) {
2832 hdr * hhdr;
2833 word nhblks;
2834 GC_bool is_ptrfree;
2836 GC_ASSERT(PAGE_ALIGNED(current));
2837 GET_HDR(current, hhdr);
2838 if (IS_FORWARDING_ADDR_OR_NIL(hhdr)) {
2839 /* This can happen only if we're at the beginning of a */
2840 /* heap segment, and a block spans heap segments. */
2841 /* We will handle that block as part of the preceding */
2842 /* segment. */
2843 GC_ASSERT(current_start == current);
2844 current_start = ++current;
2845 continue;
2847 if (HBLK_IS_FREE(hhdr)) {
2848 GC_ASSERT(PAGE_ALIGNED(hhdr -> hb_sz));
2849 nhblks = divHBLKSZ(hhdr -> hb_sz);
2850 is_ptrfree = TRUE; /* dirty on alloc */
2851 } else {
2852 nhblks = OBJ_SZ_TO_BLOCKS(hhdr -> hb_sz);
2853 is_ptrfree = IS_PTRFREE(hhdr);
2855 if (is_ptrfree) {
2856 if (current_start < current) {
2857 PROTECT(current_start, (ptr_t)current - (ptr_t)current_start);
2859 current_start = (current += nhblks);
2860 } else {
2861 current += nhblks;
2864 if (current_start < current) {
2865 PROTECT(current_start, (ptr_t)current - (ptr_t)current_start);
2871 /* We assume that either the world is stopped or its OK to lose dirty */
2872 /* bits while this is happenning (as in GC_enable_incremental). */
2873 void GC_read_dirty()
2875 BCOPY((word *)GC_dirty_pages, GC_grungy_pages,
2876 (sizeof GC_dirty_pages));
2877 BZERO((word *)GC_dirty_pages, (sizeof GC_dirty_pages));
2878 GC_protect_heap();
2881 GC_bool GC_page_was_dirty(h)
2882 struct hblk * h;
2884 register word index = PHT_HASH(h);
2886 return(HDR(h) == 0 || get_pht_entry_from_index(GC_grungy_pages, index));
2890 * Acquiring the allocation lock here is dangerous, since this
2891 * can be called from within GC_call_with_alloc_lock, and the cord
2892 * package does so. On systems that allow nested lock acquisition, this
2893 * happens to work.
2894 * On other systems, SET_LOCK_HOLDER and friends must be suitably defined.
2897 static GC_bool syscall_acquired_lock = FALSE; /* Protected by GC lock. */
2899 void GC_begin_syscall()
2901 if (!I_HOLD_LOCK()) {
2902 LOCK();
2903 syscall_acquired_lock = TRUE;
2907 void GC_end_syscall()
2909 if (syscall_acquired_lock) {
2910 syscall_acquired_lock = FALSE;
2911 UNLOCK();
2915 void GC_unprotect_range(addr, len)
2916 ptr_t addr;
2917 word len;
2919 struct hblk * start_block;
2920 struct hblk * end_block;
2921 register struct hblk *h;
2922 ptr_t obj_start;
2924 if (!GC_dirty_maintained) return;
2925 obj_start = GC_base(addr);
2926 if (obj_start == 0) return;
2927 if (GC_base(addr + len - 1) != obj_start) {
2928 ABORT("GC_unprotect_range(range bigger than object)");
2930 start_block = (struct hblk *)((word)addr & ~(GC_page_size - 1));
2931 end_block = (struct hblk *)((word)(addr + len - 1) & ~(GC_page_size - 1));
2932 end_block += GC_page_size/HBLKSIZE - 1;
2933 for (h = start_block; h <= end_block; h++) {
2934 register word index = PHT_HASH(h);
2936 async_set_pht_entry_from_index(GC_dirty_pages, index);
2938 UNPROTECT(start_block,
2939 ((ptr_t)end_block - (ptr_t)start_block) + HBLKSIZE);
2942 #if 0
2944 /* We no longer wrap read by default, since that was causing too many */
2945 /* problems. It is preferred that the client instead avoids writing */
2946 /* to the write-protected heap with a system call. */
2947 /* This still serves as sample code if you do want to wrap system calls.*/
2949 #if !defined(MSWIN32) && !defined(MSWINCE) && !defined(GC_USE_LD_WRAP)
2950 /* Replacement for UNIX system call. */
2951 /* Other calls that write to the heap should be handled similarly. */
2952 /* Note that this doesn't work well for blocking reads: It will hold */
2953 /* the allocation lock for the entire duration of the call. Multithreaded */
2954 /* clients should really ensure that it won't block, either by setting */
2955 /* the descriptor nonblocking, or by calling select or poll first, to */
2956 /* make sure that input is available. */
2957 /* Another, preferred alternative is to ensure that system calls never */
2958 /* write to the protected heap (see above). */
2959 # if defined(__STDC__) && !defined(SUNOS4)
2960 # include <unistd.h>
2961 # include <sys/uio.h>
2962 ssize_t read(int fd, void *buf, size_t nbyte)
2963 # else
2964 # ifndef LINT
2965 int read(fd, buf, nbyte)
2966 # else
2967 int GC_read(fd, buf, nbyte)
2968 # endif
2969 int fd;
2970 char *buf;
2971 int nbyte;
2972 # endif
2974 int result;
2976 GC_begin_syscall();
2977 GC_unprotect_range(buf, (word)nbyte);
2978 # if defined(IRIX5) || defined(GC_LINUX_THREADS)
2979 /* Indirect system call may not always be easily available. */
2980 /* We could call _read, but that would interfere with the */
2981 /* libpthread interception of read. */
2982 /* On Linux, we have to be careful with the linuxthreads */
2983 /* read interception. */
2985 struct iovec iov;
2987 iov.iov_base = buf;
2988 iov.iov_len = nbyte;
2989 result = readv(fd, &iov, 1);
2991 # else
2992 # if defined(HURD)
2993 result = __read(fd, buf, nbyte);
2994 # else
2995 /* The two zero args at the end of this list are because one
2996 IA-64 syscall() implementation actually requires six args
2997 to be passed, even though they aren't always used. */
2998 result = syscall(SYS_read, fd, buf, nbyte, 0, 0);
2999 # endif /* !HURD */
3000 # endif
3001 GC_end_syscall();
3002 return(result);
3004 #endif /* !MSWIN32 && !MSWINCE && !GC_LINUX_THREADS */
3006 #if defined(GC_USE_LD_WRAP) && !defined(THREADS)
3007 /* We use the GNU ld call wrapping facility. */
3008 /* This requires that the linker be invoked with "--wrap read". */
3009 /* This can be done by passing -Wl,"--wrap read" to gcc. */
3010 /* I'm not sure that this actually wraps whatever version of read */
3011 /* is called by stdio. That code also mentions __read. */
3012 # include <unistd.h>
3013 ssize_t __wrap_read(int fd, void *buf, size_t nbyte)
3015 int result;
3017 GC_begin_syscall();
3018 GC_unprotect_range(buf, (word)nbyte);
3019 result = __real_read(fd, buf, nbyte);
3020 GC_end_syscall();
3021 return(result);
3024 /* We should probably also do this for __read, or whatever stdio */
3025 /* actually calls. */
3026 #endif
3028 #endif /* 0 */
3030 /*ARGSUSED*/
3031 GC_bool GC_page_was_ever_dirty(h)
3032 struct hblk *h;
3034 return(TRUE);
3037 /* Reset the n pages starting at h to "was never dirty" status. */
3038 /*ARGSUSED*/
3039 void GC_is_fresh(h, n)
3040 struct hblk *h;
3041 word n;
3045 # endif /* MPROTECT_VDB */
3047 # ifdef PROC_VDB
3050 * See DEFAULT_VDB for interface descriptions.
3054 * This implementaion assumes a Solaris 2.X like /proc pseudo-file-system
3055 * from which we can read page modified bits. This facility is far from
3056 * optimal (e.g. we would like to get the info for only some of the
3057 * address space), but it avoids intercepting system calls.
3060 #include <errno.h>
3061 #include <sys/types.h>
3062 #include <sys/signal.h>
3063 #include <sys/fault.h>
3064 #include <sys/syscall.h>
3065 #include <sys/procfs.h>
3066 #include <sys/stat.h>
3068 #define INITIAL_BUF_SZ 16384
3069 word GC_proc_buf_size = INITIAL_BUF_SZ;
3070 char *GC_proc_buf;
3072 #ifdef GC_SOLARIS_THREADS
3073 /* We don't have exact sp values for threads. So we count on */
3074 /* occasionally declaring stack pages to be fresh. Thus we */
3075 /* need a real implementation of GC_is_fresh. We can't clear */
3076 /* entries in GC_written_pages, since that would declare all */
3077 /* pages with the given hash address to be fresh. */
3078 # define MAX_FRESH_PAGES 8*1024 /* Must be power of 2 */
3079 struct hblk ** GC_fresh_pages; /* A direct mapped cache. */
3080 /* Collisions are dropped. */
3082 # define FRESH_PAGE_SLOT(h) (divHBLKSZ((word)(h)) & (MAX_FRESH_PAGES-1))
3083 # define ADD_FRESH_PAGE(h) \
3084 GC_fresh_pages[FRESH_PAGE_SLOT(h)] = (h)
3085 # define PAGE_IS_FRESH(h) \
3086 (GC_fresh_pages[FRESH_PAGE_SLOT(h)] == (h) && (h) != 0)
3087 #endif
3089 /* Add all pages in pht2 to pht1 */
3090 void GC_or_pages(pht1, pht2)
3091 page_hash_table pht1, pht2;
3093 register int i;
3095 for (i = 0; i < PHT_SIZE; i++) pht1[i] |= pht2[i];
3098 int GC_proc_fd;
3100 void GC_dirty_init()
3102 int fd;
3103 char buf[30];
3105 GC_dirty_maintained = TRUE;
3106 if (GC_words_allocd != 0 || GC_words_allocd_before_gc != 0) {
3107 register int i;
3109 for (i = 0; i < PHT_SIZE; i++) GC_written_pages[i] = (word)(-1);
3110 # ifdef PRINTSTATS
3111 GC_printf1("Allocated words:%lu:all pages may have been written\n",
3112 (unsigned long)
3113 (GC_words_allocd + GC_words_allocd_before_gc));
3114 # endif
3116 sprintf(buf, "/proc/%d", getpid());
3117 fd = open(buf, O_RDONLY);
3118 if (fd < 0) {
3119 ABORT("/proc open failed");
3121 GC_proc_fd = syscall(SYS_ioctl, fd, PIOCOPENPD, 0);
3122 close(fd);
3123 syscall(SYS_fcntl, GC_proc_fd, F_SETFD, FD_CLOEXEC);
3124 if (GC_proc_fd < 0) {
3125 ABORT("/proc ioctl failed");
3127 GC_proc_buf = GC_scratch_alloc(GC_proc_buf_size);
3128 # ifdef GC_SOLARIS_THREADS
3129 GC_fresh_pages = (struct hblk **)
3130 GC_scratch_alloc(MAX_FRESH_PAGES * sizeof (struct hblk *));
3131 if (GC_fresh_pages == 0) {
3132 GC_err_printf0("No space for fresh pages\n");
3133 EXIT();
3135 BZERO(GC_fresh_pages, MAX_FRESH_PAGES * sizeof (struct hblk *));
3136 # endif
3139 /* Ignore write hints. They don't help us here. */
3140 /*ARGSUSED*/
3141 void GC_remove_protection(h, nblocks, is_ptrfree)
3142 struct hblk *h;
3143 word nblocks;
3144 GC_bool is_ptrfree;
3148 #ifdef GC_SOLARIS_THREADS
3149 # define READ(fd,buf,nbytes) syscall(SYS_read, fd, buf, nbytes)
3150 #else
3151 # define READ(fd,buf,nbytes) read(fd, buf, nbytes)
3152 #endif
3154 void GC_read_dirty()
3156 unsigned long ps, np;
3157 int nmaps;
3158 ptr_t vaddr;
3159 struct prasmap * map;
3160 char * bufp;
3161 ptr_t current_addr, limit;
3162 int i;
3163 int dummy;
3165 BZERO(GC_grungy_pages, (sizeof GC_grungy_pages));
3167 bufp = GC_proc_buf;
3168 if (READ(GC_proc_fd, bufp, GC_proc_buf_size) <= 0) {
3169 # ifdef PRINTSTATS
3170 GC_printf1("/proc read failed: GC_proc_buf_size = %lu\n",
3171 GC_proc_buf_size);
3172 # endif
3174 /* Retry with larger buffer. */
3175 word new_size = 2 * GC_proc_buf_size;
3176 char * new_buf = GC_scratch_alloc(new_size);
3178 if (new_buf != 0) {
3179 GC_proc_buf = bufp = new_buf;
3180 GC_proc_buf_size = new_size;
3182 if (READ(GC_proc_fd, bufp, GC_proc_buf_size) <= 0) {
3183 WARN("Insufficient space for /proc read\n", 0);
3184 /* Punt: */
3185 memset(GC_grungy_pages, 0xff, sizeof (page_hash_table));
3186 memset(GC_written_pages, 0xff, sizeof(page_hash_table));
3187 # ifdef GC_SOLARIS_THREADS
3188 BZERO(GC_fresh_pages,
3189 MAX_FRESH_PAGES * sizeof (struct hblk *));
3190 # endif
3191 return;
3195 /* Copy dirty bits into GC_grungy_pages */
3196 nmaps = ((struct prpageheader *)bufp) -> pr_nmap;
3197 /* printf( "nmaps = %d, PG_REFERENCED = %d, PG_MODIFIED = %d\n",
3198 nmaps, PG_REFERENCED, PG_MODIFIED); */
3199 bufp = bufp + sizeof(struct prpageheader);
3200 for (i = 0; i < nmaps; i++) {
3201 map = (struct prasmap *)bufp;
3202 vaddr = (ptr_t)(map -> pr_vaddr);
3203 ps = map -> pr_pagesize;
3204 np = map -> pr_npage;
3205 /* printf("vaddr = 0x%X, ps = 0x%X, np = 0x%X\n", vaddr, ps, np); */
3206 limit = vaddr + ps * np;
3207 bufp += sizeof (struct prasmap);
3208 for (current_addr = vaddr;
3209 current_addr < limit; current_addr += ps){
3210 if ((*bufp++) & PG_MODIFIED) {
3211 register struct hblk * h = (struct hblk *) current_addr;
3213 while ((ptr_t)h < current_addr + ps) {
3214 register word index = PHT_HASH(h);
3216 set_pht_entry_from_index(GC_grungy_pages, index);
3217 # ifdef GC_SOLARIS_THREADS
3219 register int slot = FRESH_PAGE_SLOT(h);
3221 if (GC_fresh_pages[slot] == h) {
3222 GC_fresh_pages[slot] = 0;
3225 # endif
3226 h++;
3230 bufp += sizeof(long) - 1;
3231 bufp = (char *)((unsigned long)bufp & ~(sizeof(long)-1));
3233 /* Update GC_written_pages. */
3234 GC_or_pages(GC_written_pages, GC_grungy_pages);
3235 # ifdef GC_SOLARIS_THREADS
3236 /* Make sure that old stacks are considered completely clean */
3237 /* unless written again. */
3238 GC_old_stacks_are_fresh();
3239 # endif
3242 #undef READ
3244 GC_bool GC_page_was_dirty(h)
3245 struct hblk *h;
3247 register word index = PHT_HASH(h);
3248 register GC_bool result;
3250 result = get_pht_entry_from_index(GC_grungy_pages, index);
3251 # ifdef GC_SOLARIS_THREADS
3252 if (result && PAGE_IS_FRESH(h)) result = FALSE;
3253 /* This happens only if page was declared fresh since */
3254 /* the read_dirty call, e.g. because it's in an unused */
3255 /* thread stack. It's OK to treat it as clean, in */
3256 /* that case. And it's consistent with */
3257 /* GC_page_was_ever_dirty. */
3258 # endif
3259 return(result);
3262 GC_bool GC_page_was_ever_dirty(h)
3263 struct hblk *h;
3265 register word index = PHT_HASH(h);
3266 register GC_bool result;
3268 result = get_pht_entry_from_index(GC_written_pages, index);
3269 # ifdef GC_SOLARIS_THREADS
3270 if (result && PAGE_IS_FRESH(h)) result = FALSE;
3271 # endif
3272 return(result);
3275 /* Caller holds allocation lock. */
3276 void GC_is_fresh(h, n)
3277 struct hblk *h;
3278 word n;
3281 register word index;
3283 # ifdef GC_SOLARIS_THREADS
3284 register word i;
3286 if (GC_fresh_pages != 0) {
3287 for (i = 0; i < n; i++) {
3288 ADD_FRESH_PAGE(h + i);
3291 # endif
3294 # endif /* PROC_VDB */
3297 # ifdef PCR_VDB
3299 # include "vd/PCR_VD.h"
3301 # define NPAGES (32*1024) /* 128 MB */
3303 PCR_VD_DB GC_grungy_bits[NPAGES];
3305 ptr_t GC_vd_base; /* Address corresponding to GC_grungy_bits[0] */
3306 /* HBLKSIZE aligned. */
3308 void GC_dirty_init()
3310 GC_dirty_maintained = TRUE;
3311 /* For the time being, we assume the heap generally grows up */
3312 GC_vd_base = GC_heap_sects[0].hs_start;
3313 if (GC_vd_base == 0) {
3314 ABORT("Bad initial heap segment");
3316 if (PCR_VD_Start(HBLKSIZE, GC_vd_base, NPAGES*HBLKSIZE)
3317 != PCR_ERes_okay) {
3318 ABORT("dirty bit initialization failed");
3322 void GC_read_dirty()
3324 /* lazily enable dirty bits on newly added heap sects */
3326 static int onhs = 0;
3327 int nhs = GC_n_heap_sects;
3328 for( ; onhs < nhs; onhs++ ) {
3329 PCR_VD_WriteProtectEnable(
3330 GC_heap_sects[onhs].hs_start,
3331 GC_heap_sects[onhs].hs_bytes );
3336 if (PCR_VD_Clear(GC_vd_base, NPAGES*HBLKSIZE, GC_grungy_bits)
3337 != PCR_ERes_okay) {
3338 ABORT("dirty bit read failed");
3342 GC_bool GC_page_was_dirty(h)
3343 struct hblk *h;
3345 if((ptr_t)h < GC_vd_base || (ptr_t)h >= GC_vd_base + NPAGES*HBLKSIZE) {
3346 return(TRUE);
3348 return(GC_grungy_bits[h - (struct hblk *)GC_vd_base] & PCR_VD_DB_dirtyBit);
3351 /*ARGSUSED*/
3352 void GC_remove_protection(h, nblocks, is_ptrfree)
3353 struct hblk *h;
3354 word nblocks;
3355 GC_bool is_ptrfree;
3357 PCR_VD_WriteProtectDisable(h, nblocks*HBLKSIZE);
3358 PCR_VD_WriteProtectEnable(h, nblocks*HBLKSIZE);
3361 # endif /* PCR_VDB */
3363 #if defined(MPROTECT_VDB) && defined(DARWIN)
3364 /* The following sources were used as a *reference* for this exception handling
3365 code:
3366 1. Apple's mach/xnu documentation
3367 2. Timothy J. Wood's "Mach Exception Handlers 101" post to the
3368 omnigroup's macosx-dev list.
3369 www.omnigroup.com/mailman/archive/macosx-dev/2000-June/002030.html
3370 3. macosx-nat.c from Apple's GDB source code.
3373 /* The bug that caused all this trouble should now be fixed. This should
3374 eventually be removed if all goes well. */
3375 /* define BROKEN_EXCEPTION_HANDLING */
3377 #include <mach/mach.h>
3378 #include <mach/mach_error.h>
3379 #include <mach/thread_status.h>
3380 #include <mach/exception.h>
3381 #include <mach/task.h>
3382 #include <pthread.h>
3384 /* These are not defined in any header, although they are documented */
3385 extern boolean_t exc_server(mach_msg_header_t *,mach_msg_header_t *);
3386 extern kern_return_t exception_raise(
3387 mach_port_t,mach_port_t,mach_port_t,
3388 exception_type_t,exception_data_t,mach_msg_type_number_t);
3389 extern kern_return_t exception_raise_state(
3390 mach_port_t,mach_port_t,mach_port_t,
3391 exception_type_t,exception_data_t,mach_msg_type_number_t,
3392 thread_state_flavor_t*,thread_state_t,mach_msg_type_number_t,
3393 thread_state_t,mach_msg_type_number_t*);
3394 extern kern_return_t exception_raise_state_identity(
3395 mach_port_t,mach_port_t,mach_port_t,
3396 exception_type_t,exception_data_t,mach_msg_type_number_t,
3397 thread_state_flavor_t*,thread_state_t,mach_msg_type_number_t,
3398 thread_state_t,mach_msg_type_number_t*);
3401 #define MAX_EXCEPTION_PORTS 16
3403 static struct {
3404 mach_msg_type_number_t count;
3405 exception_mask_t masks[MAX_EXCEPTION_PORTS];
3406 exception_handler_t ports[MAX_EXCEPTION_PORTS];
3407 exception_behavior_t behaviors[MAX_EXCEPTION_PORTS];
3408 thread_state_flavor_t flavors[MAX_EXCEPTION_PORTS];
3409 } GC_old_exc_ports;
3411 static struct {
3412 mach_port_t exception;
3413 #if defined(THREADS)
3414 mach_port_t reply;
3415 #endif
3416 } GC_ports;
3418 typedef struct {
3419 mach_msg_header_t head;
3420 } GC_msg_t;
3422 typedef enum {
3423 GC_MP_NORMAL, GC_MP_DISCARDING, GC_MP_STOPPED
3424 } GC_mprotect_state_t;
3426 /* FIXME: 1 and 2 seem to be safe to use in the msgh_id field,
3427 but it isn't documented. Use the source and see if they
3428 should be ok. */
3429 #define ID_STOP 1
3430 #define ID_RESUME 2
3432 /* These values are only used on the reply port */
3433 #define ID_ACK 3
3435 #if defined(THREADS)
3437 GC_mprotect_state_t GC_mprotect_state;
3439 /* The following should ONLY be called when the world is stopped */
3440 static void GC_mprotect_thread_notify(mach_msg_id_t id) {
3441 struct {
3442 GC_msg_t msg;
3443 mach_msg_trailer_t trailer;
3444 } buf;
3445 mach_msg_return_t r;
3446 /* remote, local */
3447 buf.msg.head.msgh_bits =
3448 MACH_MSGH_BITS(MACH_MSG_TYPE_MAKE_SEND,0);
3449 buf.msg.head.msgh_size = sizeof(buf.msg);
3450 buf.msg.head.msgh_remote_port = GC_ports.exception;
3451 buf.msg.head.msgh_local_port = MACH_PORT_NULL;
3452 buf.msg.head.msgh_id = id;
3454 r = mach_msg(
3455 &buf.msg.head,
3456 MACH_SEND_MSG|MACH_RCV_MSG|MACH_RCV_LARGE,
3457 sizeof(buf.msg),
3458 sizeof(buf),
3459 GC_ports.reply,
3460 MACH_MSG_TIMEOUT_NONE,
3461 MACH_PORT_NULL);
3462 if(r != MACH_MSG_SUCCESS)
3463 ABORT("mach_msg failed in GC_mprotect_thread_notify");
3464 if(buf.msg.head.msgh_id != ID_ACK)
3465 ABORT("invalid ack in GC_mprotect_thread_notify");
3468 /* Should only be called by the mprotect thread */
3469 static void GC_mprotect_thread_reply() {
3470 GC_msg_t msg;
3471 mach_msg_return_t r;
3472 /* remote, local */
3473 msg.head.msgh_bits =
3474 MACH_MSGH_BITS(MACH_MSG_TYPE_MAKE_SEND,0);
3475 msg.head.msgh_size = sizeof(msg);
3476 msg.head.msgh_remote_port = GC_ports.reply;
3477 msg.head.msgh_local_port = MACH_PORT_NULL;
3478 msg.head.msgh_id = ID_ACK;
3480 r = mach_msg(
3481 &msg.head,
3482 MACH_SEND_MSG,
3483 sizeof(msg),
3485 MACH_PORT_NULL,
3486 MACH_MSG_TIMEOUT_NONE,
3487 MACH_PORT_NULL);
3488 if(r != MACH_MSG_SUCCESS)
3489 ABORT("mach_msg failed in GC_mprotect_thread_reply");
3492 void GC_mprotect_stop() {
3493 GC_mprotect_thread_notify(ID_STOP);
3495 void GC_mprotect_resume() {
3496 GC_mprotect_thread_notify(ID_RESUME);
3499 #else /* !THREADS */
3500 /* The compiler should optimize away any GC_mprotect_state computations */
3501 #define GC_mprotect_state GC_MP_NORMAL
3502 #endif
3504 static void *GC_mprotect_thread(void *arg) {
3505 mach_msg_return_t r;
3506 /* These two structures contain some private kernel data. We don't need to
3507 access any of it so we don't bother defining a proper struct. The
3508 correct definitions are in the xnu source code. */
3509 struct {
3510 mach_msg_header_t head;
3511 char data[256];
3512 } reply;
3513 struct {
3514 mach_msg_header_t head;
3515 mach_msg_body_t msgh_body;
3516 char data[1024];
3517 } msg;
3519 mach_msg_id_t id;
3521 GC_darwin_register_mach_handler_thread(mach_thread_self());
3523 for(;;) {
3524 r = mach_msg(
3525 &msg.head,
3526 MACH_RCV_MSG|MACH_RCV_LARGE|
3527 (GC_mprotect_state == GC_MP_DISCARDING ? MACH_RCV_TIMEOUT : 0),
3529 sizeof(msg),
3530 GC_ports.exception,
3531 GC_mprotect_state == GC_MP_DISCARDING ? 0 : MACH_MSG_TIMEOUT_NONE,
3532 MACH_PORT_NULL);
3534 id = r == MACH_MSG_SUCCESS ? msg.head.msgh_id : -1;
3536 #if defined(THREADS)
3537 if(GC_mprotect_state == GC_MP_DISCARDING) {
3538 if(r == MACH_RCV_TIMED_OUT) {
3539 GC_mprotect_state = GC_MP_STOPPED;
3540 GC_mprotect_thread_reply();
3541 continue;
3543 if(r == MACH_MSG_SUCCESS && (id == ID_STOP || id == ID_RESUME))
3544 ABORT("out of order mprotect thread request");
3546 #endif
3548 if(r != MACH_MSG_SUCCESS) {
3549 GC_err_printf2("mach_msg failed with %d %s\n",
3550 (int)r,mach_error_string(r));
3551 ABORT("mach_msg failed");
3554 switch(id) {
3555 #if defined(THREADS)
3556 case ID_STOP:
3557 if(GC_mprotect_state != GC_MP_NORMAL)
3558 ABORT("Called mprotect_stop when state wasn't normal");
3559 GC_mprotect_state = GC_MP_DISCARDING;
3560 break;
3561 case ID_RESUME:
3562 if(GC_mprotect_state != GC_MP_STOPPED)
3563 ABORT("Called mprotect_resume when state wasn't stopped");
3564 GC_mprotect_state = GC_MP_NORMAL;
3565 GC_mprotect_thread_reply();
3566 break;
3567 #endif /* THREADS */
3568 default:
3569 /* Handle the message (calls catch_exception_raise) */
3570 if(!exc_server(&msg.head,&reply.head))
3571 ABORT("exc_server failed");
3572 /* Send the reply */
3573 r = mach_msg(
3574 &reply.head,
3575 MACH_SEND_MSG,
3576 reply.head.msgh_size,
3578 MACH_PORT_NULL,
3579 MACH_MSG_TIMEOUT_NONE,
3580 MACH_PORT_NULL);
3581 if(r != MACH_MSG_SUCCESS) {
3582 /* This will fail if the thread dies, but the thread shouldn't
3583 die... */
3584 #ifdef BROKEN_EXCEPTION_HANDLING
3585 GC_err_printf2(
3586 "mach_msg failed with %d %s while sending exc reply\n",
3587 (int)r,mach_error_string(r));
3588 #else
3589 ABORT("mach_msg failed while sending exception reply");
3590 #endif
3592 } /* switch */
3593 } /* for(;;) */
3594 /* NOT REACHED */
3595 return NULL;
3598 /* All this SIGBUS code shouldn't be necessary. All protection faults should
3599 be going throught the mach exception handler. However, it seems a SIGBUS is
3600 occasionally sent for some unknown reason. Even more odd, it seems to be
3601 meaningless and safe to ignore. */
3602 #ifdef BROKEN_EXCEPTION_HANDLING
3604 typedef void (* SIG_PF)();
3605 static SIG_PF GC_old_bus_handler;
3607 /* Updates to this aren't atomic, but the SIGBUSs seem pretty rare.
3608 Even if this doesn't get updated property, it isn't really a problem */
3609 static int GC_sigbus_count;
3611 static void GC_darwin_sigbus(int num,siginfo_t *sip,void *context) {
3612 if(num != SIGBUS) ABORT("Got a non-sigbus signal in the sigbus handler");
3614 /* Ugh... some seem safe to ignore, but too many in a row probably means
3615 trouble. GC_sigbus_count is reset for each mach exception that is
3616 handled */
3617 if(GC_sigbus_count >= 8) {
3618 ABORT("Got more than 8 SIGBUSs in a row!");
3619 } else {
3620 GC_sigbus_count++;
3621 GC_err_printf0("GC: WARNING: Ignoring SIGBUS.\n");
3624 #endif /* BROKEN_EXCEPTION_HANDLING */
3626 void GC_dirty_init() {
3627 kern_return_t r;
3628 mach_port_t me;
3629 pthread_t thread;
3630 pthread_attr_t attr;
3631 exception_mask_t mask;
3633 # ifdef PRINTSTATS
3634 GC_printf0("Inititalizing mach/darwin mprotect virtual dirty bit "
3635 "implementation\n");
3636 # endif
3637 # ifdef BROKEN_EXCEPTION_HANDLING
3638 GC_err_printf0("GC: WARNING: Enabling workarounds for various darwin "
3639 "exception handling bugs.\n");
3640 # endif
3641 GC_dirty_maintained = TRUE;
3642 if (GC_page_size % HBLKSIZE != 0) {
3643 GC_err_printf0("Page size not multiple of HBLKSIZE\n");
3644 ABORT("Page size not multiple of HBLKSIZE");
3647 GC_task_self = me = mach_task_self();
3649 r = mach_port_allocate(me,MACH_PORT_RIGHT_RECEIVE,&GC_ports.exception);
3650 if(r != KERN_SUCCESS) ABORT("mach_port_allocate failed (exception port)");
3652 r = mach_port_insert_right(me,GC_ports.exception,GC_ports.exception,
3653 MACH_MSG_TYPE_MAKE_SEND);
3654 if(r != KERN_SUCCESS)
3655 ABORT("mach_port_insert_right failed (exception port)");
3657 #if defined(THREADS)
3658 r = mach_port_allocate(me,MACH_PORT_RIGHT_RECEIVE,&GC_ports.reply);
3659 if(r != KERN_SUCCESS) ABORT("mach_port_allocate failed (reply port)");
3660 #endif
3662 /* The exceptions we want to catch */
3663 mask = EXC_MASK_BAD_ACCESS;
3665 r = task_get_exception_ports(
3667 mask,
3668 GC_old_exc_ports.masks,
3669 &GC_old_exc_ports.count,
3670 GC_old_exc_ports.ports,
3671 GC_old_exc_ports.behaviors,
3672 GC_old_exc_ports.flavors
3674 if(r != KERN_SUCCESS) ABORT("task_get_exception_ports failed");
3676 r = task_set_exception_ports(
3678 mask,
3679 GC_ports.exception,
3680 EXCEPTION_DEFAULT,
3681 MACHINE_THREAD_STATE
3683 if(r != KERN_SUCCESS) ABORT("task_set_exception_ports failed");
3685 if(pthread_attr_init(&attr) != 0) ABORT("pthread_attr_init failed");
3686 if(pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED) != 0)
3687 ABORT("pthread_attr_setdetachedstate failed");
3689 # undef pthread_create
3690 /* This will call the real pthread function, not our wrapper */
3691 if(pthread_create(&thread,&attr,GC_mprotect_thread,NULL) != 0)
3692 ABORT("pthread_create failed");
3693 pthread_attr_destroy(&attr);
3695 /* Setup the sigbus handler for ignoring the meaningless SIGBUSs */
3696 #ifdef BROKEN_EXCEPTION_HANDLING
3698 struct sigaction sa, oldsa;
3699 sa.sa_handler = (SIG_PF)GC_darwin_sigbus;
3700 sigemptyset(&sa.sa_mask);
3701 sa.sa_flags = SA_RESTART|SA_SIGINFO;
3702 if(sigaction(SIGBUS,&sa,&oldsa) < 0) ABORT("sigaction");
3703 GC_old_bus_handler = (SIG_PF)oldsa.sa_handler;
3704 if (GC_old_bus_handler != SIG_DFL) {
3705 # ifdef PRINTSTATS
3706 GC_err_printf0("Replaced other SIGBUS handler\n");
3707 # endif
3710 #endif /* BROKEN_EXCEPTION_HANDLING */
3713 /* The source code for Apple's GDB was used as a reference for the exception
3714 forwarding code. This code is similar to be GDB code only because there is
3715 only one way to do it. */
3716 static kern_return_t GC_forward_exception(
3717 mach_port_t thread,
3718 mach_port_t task,
3719 exception_type_t exception,
3720 exception_data_t data,
3721 mach_msg_type_number_t data_count
3723 int i;
3724 kern_return_t r;
3725 mach_port_t port;
3726 exception_behavior_t behavior;
3727 thread_state_flavor_t flavor;
3729 thread_state_data_t thread_state;
3730 mach_msg_type_number_t thread_state_count = THREAD_STATE_MAX;
3732 for(i=0;i<GC_old_exc_ports.count;i++)
3733 if(GC_old_exc_ports.masks[i] & (1 << exception))
3734 break;
3735 if(i==GC_old_exc_ports.count) ABORT("No handler for exception!");
3737 port = GC_old_exc_ports.ports[i];
3738 behavior = GC_old_exc_ports.behaviors[i];
3739 flavor = GC_old_exc_ports.flavors[i];
3741 if(behavior != EXCEPTION_DEFAULT) {
3742 r = thread_get_state(thread,flavor,thread_state,&thread_state_count);
3743 if(r != KERN_SUCCESS)
3744 ABORT("thread_get_state failed in forward_exception");
3747 switch(behavior) {
3748 case EXCEPTION_DEFAULT:
3749 r = exception_raise(port,thread,task,exception,data,data_count);
3750 break;
3751 case EXCEPTION_STATE:
3752 r = exception_raise_state(port,thread,task,exception,data,
3753 data_count,&flavor,thread_state,thread_state_count,
3754 thread_state,&thread_state_count);
3755 break;
3756 case EXCEPTION_STATE_IDENTITY:
3757 r = exception_raise_state_identity(port,thread,task,exception,data,
3758 data_count,&flavor,thread_state,thread_state_count,
3759 thread_state,&thread_state_count);
3760 break;
3761 default:
3762 r = KERN_FAILURE; /* make gcc happy */
3763 ABORT("forward_exception: unknown behavior");
3764 break;
3767 if(behavior != EXCEPTION_DEFAULT) {
3768 r = thread_set_state(thread,flavor,thread_state,thread_state_count);
3769 if(r != KERN_SUCCESS)
3770 ABORT("thread_set_state failed in forward_exception");
3773 return r;
3776 #define FWD() GC_forward_exception(thread,task,exception,code,code_count)
3778 /* This violates the namespace rules but there isn't anything that can be done
3779 about it. The exception handling stuff is hard coded to call this */
3780 kern_return_t
3781 catch_exception_raise(
3782 mach_port_t exception_port,mach_port_t thread,mach_port_t task,
3783 exception_type_t exception,exception_data_t code,
3784 mach_msg_type_number_t code_count
3786 kern_return_t r;
3787 char *addr;
3788 struct hblk *h;
3789 int i;
3790 #ifdef POWERPC
3791 thread_state_flavor_t flavor = PPC_EXCEPTION_STATE;
3792 mach_msg_type_number_t exc_state_count = PPC_EXCEPTION_STATE_COUNT;
3793 ppc_exception_state_t exc_state;
3794 #else
3795 # error FIXME for non-ppc darwin
3796 #endif
3799 if(exception != EXC_BAD_ACCESS || code[0] != KERN_PROTECTION_FAILURE) {
3800 #ifdef DEBUG_EXCEPTION_HANDLING
3801 /* We aren't interested, pass it on to the old handler */
3802 GC_printf3("Exception: 0x%x Code: 0x%x 0x%x in catch....\n",
3803 exception,
3804 code_count > 0 ? code[0] : -1,
3805 code_count > 1 ? code[1] : -1);
3806 #endif
3807 return FWD();
3810 r = thread_get_state(thread,flavor,
3811 (natural_t*)&exc_state,&exc_state_count);
3812 if(r != KERN_SUCCESS) {
3813 /* The thread is supposed to be suspended while the exception handler
3814 is called. This shouldn't fail. */
3815 #ifdef BROKEN_EXCEPTION_HANDLING
3816 GC_err_printf0("thread_get_state failed in "
3817 "catch_exception_raise\n");
3818 return KERN_SUCCESS;
3819 #else
3820 ABORT("thread_get_state failed in catch_exception_raise");
3821 #endif
3824 /* This is the address that caused the fault */
3825 addr = (char*) exc_state.dar;
3827 if((HDR(addr)) == 0) {
3828 /* Ugh... just like the SIGBUS problem above, it seems we get a bogus
3829 KERN_PROTECTION_FAILURE every once and a while. We wait till we get
3830 a bunch in a row before doing anything about it. If a "real" fault
3831 ever occurres it'll just keep faulting over and over and we'll hit
3832 the limit pretty quickly. */
3833 #ifdef BROKEN_EXCEPTION_HANDLING
3834 static char *last_fault;
3835 static int last_fault_count;
3837 if(addr != last_fault) {
3838 last_fault = addr;
3839 last_fault_count = 0;
3841 if(++last_fault_count < 32) {
3842 if(last_fault_count == 1)
3843 GC_err_printf1(
3844 "GC: WARNING: Ignoring KERN_PROTECTION_FAILURE at %p\n",
3845 addr);
3846 return KERN_SUCCESS;
3849 GC_err_printf1("Unexpected KERN_PROTECTION_FAILURE at %p\n",addr);
3850 /* Can't pass it along to the signal handler because that is
3851 ignoring SIGBUS signals. We also shouldn't call ABORT here as
3852 signals don't always work too well from the exception handler. */
3853 GC_err_printf0("Aborting\n");
3854 exit(EXIT_FAILURE);
3855 #else /* BROKEN_EXCEPTION_HANDLING */
3856 /* Pass it along to the next exception handler
3857 (which should call SIGBUS/SIGSEGV) */
3858 return FWD();
3859 #endif /* !BROKEN_EXCEPTION_HANDLING */
3862 #ifdef BROKEN_EXCEPTION_HANDLING
3863 /* Reset the number of consecutive SIGBUSs */
3864 GC_sigbus_count = 0;
3865 #endif
3867 if(GC_mprotect_state == GC_MP_NORMAL) { /* common case */
3868 h = (struct hblk*)((word)addr & ~(GC_page_size-1));
3869 UNPROTECT(h, GC_page_size);
3870 for (i = 0; i < divHBLKSZ(GC_page_size); i++) {
3871 register int index = PHT_HASH(h+i);
3872 async_set_pht_entry_from_index(GC_dirty_pages, index);
3874 } else if(GC_mprotect_state == GC_MP_DISCARDING) {
3875 /* Lie to the thread for now. No sense UNPROTECT()ing the memory
3876 when we're just going to PROTECT() it again later. The thread
3877 will just fault again once it resumes */
3878 } else {
3879 /* Shouldn't happen, i don't think */
3880 GC_printf0("KERN_PROTECTION_FAILURE while world is stopped\n");
3881 return FWD();
3883 return KERN_SUCCESS;
3885 #undef FWD
3887 /* These should never be called, but just in case... */
3888 kern_return_t catch_exception_raise_state(mach_port_name_t exception_port,
3889 int exception, exception_data_t code, mach_msg_type_number_t codeCnt,
3890 int flavor, thread_state_t old_state, int old_stateCnt,
3891 thread_state_t new_state, int new_stateCnt)
3893 ABORT("catch_exception_raise_state");
3894 return(KERN_INVALID_ARGUMENT);
3896 kern_return_t catch_exception_raise_state_identity(
3897 mach_port_name_t exception_port, mach_port_t thread, mach_port_t task,
3898 int exception, exception_data_t code, mach_msg_type_number_t codeCnt,
3899 int flavor, thread_state_t old_state, int old_stateCnt,
3900 thread_state_t new_state, int new_stateCnt)
3902 ABORT("catch_exception_raise_state_identity");
3903 return(KERN_INVALID_ARGUMENT);
3907 #endif /* DARWIN && MPROTECT_VDB */
3909 # ifndef HAVE_INCREMENTAL_PROTECTION_NEEDS
3910 int GC_incremental_protection_needs()
3912 return GC_PROTECTS_NONE;
3914 # endif /* !HAVE_INCREMENTAL_PROTECTION_NEEDS */
3917 * Call stack save code for debugging.
3918 * Should probably be in mach_dep.c, but that requires reorganization.
3921 /* I suspect the following works for most X86 *nix variants, so */
3922 /* long as the frame pointer is explicitly stored. In the case of gcc, */
3923 /* compiler flags (e.g. -fomit-frame-pointer) determine whether it is. */
3924 #if defined(I386) && defined(LINUX) && defined(SAVE_CALL_CHAIN)
3925 # include <features.h>
3927 struct frame {
3928 struct frame *fr_savfp;
3929 long fr_savpc;
3930 long fr_arg[NARGS]; /* All the arguments go here. */
3932 #endif
3934 #if defined(SPARC)
3935 # if defined(LINUX)
3936 # include <features.h>
3938 struct frame {
3939 long fr_local[8];
3940 long fr_arg[6];
3941 struct frame *fr_savfp;
3942 long fr_savpc;
3943 # ifndef __arch64__
3944 char *fr_stret;
3945 # endif
3946 long fr_argd[6];
3947 long fr_argx[0];
3949 # else
3950 # if defined(SUNOS4)
3951 # include <machine/frame.h>
3952 # else
3953 # if defined (DRSNX)
3954 # include <sys/sparc/frame.h>
3955 # else
3956 # if defined(OPENBSD) || defined(NETBSD)
3957 # include <frame.h>
3958 # else
3959 # include <sys/frame.h>
3960 # endif
3961 # endif
3962 # endif
3963 # endif
3964 # if NARGS > 6
3965 --> We only know how to to get the first 6 arguments
3966 # endif
3967 #endif /* SPARC */
3969 #ifdef NEED_CALLINFO
3970 /* Fill in the pc and argument information for up to NFRAMES of my */
3971 /* callers. Ignore my frame and my callers frame. */
3973 #ifdef LINUX
3974 # include <unistd.h>
3975 #endif
3977 #endif /* NEED_CALLINFO */
3979 #if defined(GC_HAVE_BUILTIN_BACKTRACE)
3980 # include <execinfo.h>
3981 #endif
3983 #ifdef SAVE_CALL_CHAIN
3985 #if NARGS == 0 && NFRAMES % 2 == 0 /* No padding */ \
3986 && defined(GC_HAVE_BUILTIN_BACKTRACE)
3988 void GC_save_callers (info)
3989 struct callinfo info[NFRAMES];
3991 void * tmp_info[NFRAMES + 1];
3992 int npcs, i;
3993 # define IGNORE_FRAMES 1
3995 /* We retrieve NFRAMES+1 pc values, but discard the first, since it */
3996 /* points to our own frame. */
3997 GC_ASSERT(sizeof(struct callinfo) == sizeof(void *));
3998 npcs = backtrace((void **)tmp_info, NFRAMES + IGNORE_FRAMES);
3999 BCOPY(tmp_info+IGNORE_FRAMES, info, (npcs - IGNORE_FRAMES) * sizeof(void *));
4000 for (i = npcs - IGNORE_FRAMES; i < NFRAMES; ++i) info[i].ci_pc = 0;
4003 #else /* No builtin backtrace; do it ourselves */
4005 #if (defined(OPENBSD) || defined(NETBSD)) && defined(SPARC)
4006 # define FR_SAVFP fr_fp
4007 # define FR_SAVPC fr_pc
4008 #else
4009 # define FR_SAVFP fr_savfp
4010 # define FR_SAVPC fr_savpc
4011 #endif
4013 #if defined(SPARC) && (defined(__arch64__) || defined(__sparcv9))
4014 # define BIAS 2047
4015 #else
4016 # define BIAS 0
4017 #endif
4019 void GC_save_callers (info)
4020 struct callinfo info[NFRAMES];
4022 struct frame *frame;
4023 struct frame *fp;
4024 int nframes = 0;
4025 # ifdef I386
4026 /* We assume this is turned on only with gcc as the compiler. */
4027 asm("movl %%ebp,%0" : "=r"(frame));
4028 fp = frame;
4029 # else
4030 frame = (struct frame *) GC_save_regs_in_stack ();
4031 fp = (struct frame *)((long) frame -> FR_SAVFP + BIAS);
4032 #endif
4034 for (; (!(fp HOTTER_THAN frame) && !(GC_stackbottom HOTTER_THAN (ptr_t)fp)
4035 && (nframes < NFRAMES));
4036 fp = (struct frame *)((long) fp -> FR_SAVFP + BIAS), nframes++) {
4037 register int i;
4039 info[nframes].ci_pc = fp->FR_SAVPC;
4040 # if NARGS > 0
4041 for (i = 0; i < NARGS; i++) {
4042 info[nframes].ci_arg[i] = ~(fp->fr_arg[i]);
4044 # endif /* NARGS > 0 */
4046 if (nframes < NFRAMES) info[nframes].ci_pc = 0;
4049 #endif /* No builtin backtrace */
4051 #endif /* SAVE_CALL_CHAIN */
4053 #ifdef NEED_CALLINFO
4055 /* Print info to stderr. We do NOT hold the allocation lock */
4056 void GC_print_callers (info)
4057 struct callinfo info[NFRAMES];
4059 register int i;
4060 static int reentry_count = 0;
4061 GC_bool stop = FALSE;
4063 /* FIXME: This should probably use a different lock, so that we */
4064 /* become callable with or without the allocation lock. */
4065 LOCK();
4066 ++reentry_count;
4067 UNLOCK();
4069 # if NFRAMES == 1
4070 GC_err_printf0("\tCaller at allocation:\n");
4071 # else
4072 GC_err_printf0("\tCall chain at allocation:\n");
4073 # endif
4074 for (i = 0; i < NFRAMES && !stop ; i++) {
4075 if (info[i].ci_pc == 0) break;
4076 # if NARGS > 0
4078 int j;
4080 GC_err_printf0("\t\targs: ");
4081 for (j = 0; j < NARGS; j++) {
4082 if (j != 0) GC_err_printf0(", ");
4083 GC_err_printf2("%d (0x%X)", ~(info[i].ci_arg[j]),
4084 ~(info[i].ci_arg[j]));
4086 GC_err_printf0("\n");
4088 # endif
4089 if (reentry_count > 1) {
4090 /* We were called during an allocation during */
4091 /* a previous GC_print_callers call; punt. */
4092 GC_err_printf1("\t\t##PC##= 0x%lx\n", info[i].ci_pc);
4093 continue;
4096 # ifdef LINUX
4097 FILE *pipe;
4098 # endif
4099 # if defined(GC_HAVE_BUILTIN_BACKTRACE) \
4100 && !defined(GC_BACKTRACE_SYMBOLS_BROKEN)
4101 char **sym_name =
4102 backtrace_symbols((void **)(&(info[i].ci_pc)), 1);
4103 char *name = sym_name[0];
4104 # else
4105 char buf[40];
4106 char *name = buf;
4107 sprintf(buf, "##PC##= 0x%lx", info[i].ci_pc);
4108 # endif
4109 # if defined(LINUX) && !defined(SMALL_CONFIG)
4110 /* Try for a line number. */
4112 # define EXE_SZ 100
4113 static char exe_name[EXE_SZ];
4114 # define CMD_SZ 200
4115 char cmd_buf[CMD_SZ];
4116 # define RESULT_SZ 200
4117 static char result_buf[RESULT_SZ];
4118 size_t result_len;
4119 char *old_preload;
4120 # define PRELOAD_SZ 200
4121 char preload_buf[PRELOAD_SZ];
4122 static GC_bool found_exe_name = FALSE;
4123 static GC_bool will_fail = FALSE;
4124 int ret_code;
4125 /* Try to get it via a hairy and expensive scheme. */
4126 /* First we get the name of the executable: */
4127 if (will_fail) goto out;
4128 if (!found_exe_name) {
4129 ret_code = readlink("/proc/self/exe", exe_name, EXE_SZ);
4130 if (ret_code < 0 || ret_code >= EXE_SZ
4131 || exe_name[0] != '/') {
4132 will_fail = TRUE; /* Dont try again. */
4133 goto out;
4135 exe_name[ret_code] = '\0';
4136 found_exe_name = TRUE;
4138 /* Then we use popen to start addr2line -e <exe> <addr> */
4139 /* There are faster ways to do this, but hopefully this */
4140 /* isn't time critical. */
4141 sprintf(cmd_buf, "/usr/bin/addr2line -f -e %s 0x%lx", exe_name,
4142 (unsigned long)info[i].ci_pc);
4143 old_preload = getenv ("LD_PRELOAD");
4144 if (0 != old_preload) {
4145 if (strlen (old_preload) >= PRELOAD_SZ) {
4146 will_fail = TRUE;
4147 goto out;
4149 strcpy (preload_buf, old_preload);
4150 unsetenv ("LD_PRELOAD");
4152 pipe = popen(cmd_buf, "r");
4153 if (0 != old_preload
4154 && 0 != setenv ("LD_PRELOAD", preload_buf, 0)) {
4155 WARN("Failed to reset LD_PRELOAD\n", 0);
4157 if (pipe == NULL
4158 || (result_len = fread(result_buf, 1, RESULT_SZ - 1, pipe))
4159 == 0) {
4160 if (pipe != NULL) pclose(pipe);
4161 will_fail = TRUE;
4162 goto out;
4164 if (result_buf[result_len - 1] == '\n') --result_len;
4165 result_buf[result_len] = 0;
4166 if (result_buf[0] == '?'
4167 || result_buf[result_len-2] == ':'
4168 && result_buf[result_len-1] == '0') {
4169 pclose(pipe);
4170 goto out;
4172 /* Get rid of embedded newline, if any. Test for "main" */
4174 char * nl = strchr(result_buf, '\n');
4175 if (nl != NULL && nl < result_buf + result_len) {
4176 *nl = ':';
4178 if (strncmp(result_buf, "main", nl - result_buf) == 0) {
4179 stop = TRUE;
4182 if (result_len < RESULT_SZ - 25) {
4183 /* Add in hex address */
4184 sprintf(result_buf + result_len, " [0x%lx]",
4185 (unsigned long)info[i].ci_pc);
4187 name = result_buf;
4188 pclose(pipe);
4189 out:;
4191 # endif /* LINUX */
4192 GC_err_printf1("\t\t%s\n", name);
4193 # if defined(GC_HAVE_BUILTIN_BACKTRACE) \
4194 && !defined(GC_BACKTRACE_SYMBOLS_BROKEN)
4195 free(sym_name); /* May call GC_free; that's OK */
4196 # endif
4199 LOCK();
4200 --reentry_count;
4201 UNLOCK();
4204 #endif /* NEED_CALLINFO */
4208 #if defined(LINUX) && defined(__ELF__) && !defined(SMALL_CONFIG)
4210 /* Dump /proc/self/maps to GC_stderr, to enable looking up names for
4211 addresses in FIND_LEAK output. */
4213 static word dump_maps(char *maps)
4215 GC_err_write(maps, strlen(maps));
4216 return 1;
4219 void GC_print_address_map()
4221 GC_err_printf0("---------- Begin address map ----------\n");
4222 GC_apply_to_maps(dump_maps);
4223 GC_err_printf0("---------- End address map ----------\n");
4226 #endif