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