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