source: trunk/gc6.8/os_dep.c@ 132

Last change on this file since 132 was 132, checked in by cinc, 19 years ago

Boehm-Demers-Weiser garbage collector. Single-threaded for OS/2.

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