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