[BACK]Return to os_dep.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib2 / asir2000 / gc

Annotation of OpenXM_contrib2/asir2000/gc/os_dep.c, Revision 1.4

1.1       noro        1: int ox_usr1_sent, ox_int_received, critical_when_signal;
                      2: static int inside_critical_section;
                      3:
                      4: /*
1.2       noro        5:  * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
1.1       noro        6:  * Copyright (c) 1991-1995 by Xerox Corporation.  All rights reserved.
1.2       noro        7:  * Copyright (c) 1996-1999 by Silicon Graphics.  All rights reserved.
                      8:  * Copyright (c) 1999 by Hewlett-Packard Company.  All rights reserved.
1.1       noro        9:  *
                     10:  * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
                     11:  * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
                     12:  *
                     13:  * Permission is hereby granted to use or copy this program
                     14:  * for any purpose,  provided the above notices are retained on all copies.
                     15:  * Permission to modify the code and to distribute modified code is granted,
                     16:  * provided the above notices are retained, and a notice that the code was
                     17:  * modified is included with the above copyright notice.
                     18:  */
                     19:
1.4     ! noro       20: # include "private/gc_priv.h"
1.1       noro       21:
                     22: # if defined(LINUX) && !defined(POWERPC)
                     23: #   include <linux/version.h>
                     24: #   if (LINUX_VERSION_CODE <= 0x10400)
                     25:       /* Ugly hack to get struct sigcontext_struct definition.  Required      */
                     26:       /* for some early 1.3.X releases.  Will hopefully go away soon. */
                     27:       /* in some later Linux releases, asm/sigcontext.h may have to   */
                     28:       /* be included instead.                                         */
                     29: #     define __KERNEL__
                     30: #     include <asm/signal.h>
                     31: #     undef __KERNEL__
                     32: #   else
                     33:       /* Kernels prior to 2.1.1 defined struct sigcontext_struct instead of */
                     34:       /* struct sigcontext.  libc6 (glibc2) uses "struct sigcontext" in     */
                     35:       /* prototypes, so we have to include the top-level sigcontext.h to    */
                     36:       /* make sure the former gets defined to be the latter if appropriate. */
                     37: #     include <features.h>
                     38: #     if 2 <= __GLIBC__
1.2       noro       39: #       if 2 == __GLIBC__ && 0 == __GLIBC_MINOR__
1.1       noro       40:          /* glibc 2.1 no longer has sigcontext.h.  But signal.h        */
                     41:          /* has the right declaration for glibc 2.1.                   */
                     42: #         include <sigcontext.h>
                     43: #       endif /* 0 == __GLIBC_MINOR__ */
                     44: #     else /* not 2 <= __GLIBC__ */
                     45:         /* libc5 doesn't have <sigcontext.h>: go directly with the kernel   */
                     46:         /* one.  Check LINUX_VERSION_CODE to see which we should reference. */
                     47: #       include <asm/sigcontext.h>
                     48: #     endif /* 2 <= __GLIBC__ */
                     49: #   endif
                     50: # endif
1.4     ! noro       51: # if !defined(OS2) && !defined(PCR) && !defined(AMIGA) && !defined(MACOS) \
        !            52:     && !defined(MSWINCE)
1.1       noro       53: #   include <sys/types.h>
                     54: #   if !defined(MSWIN32) && !defined(SUNOS4)
                     55: #      include <unistd.h>
                     56: #   endif
                     57: # endif
                     58:
                     59: # include <stdio.h>
1.4     ! noro       60: # if defined(MSWINCE)
        !            61: #   define SIGSEGV 0 /* value is irrelevant */
        !            62: # else
        !            63: #   include <signal.h>
        !            64: # endif
1.1       noro       65:
                     66: /* Blatantly OS dependent routines, except for those that are related  */
1.2       noro       67: /* to dynamic loading.                                                 */
1.1       noro       68:
                     69: # if !defined(THREADS) && !defined(STACKBOTTOM) && defined(HEURISTIC2)
                     70: #   define NEED_FIND_LIMIT
                     71: # endif
                     72:
1.2       noro       73: # if defined(IRIX_THREADS) || defined(HPUX_THREADS)
1.1       noro       74: #   define NEED_FIND_LIMIT
                     75: # endif
                     76:
1.3       noro       77: # if (defined(SUNOS4) && defined(DYNAMIC_LOADING)) && !defined(PCR)
1.1       noro       78: #   define NEED_FIND_LIMIT
                     79: # endif
                     80:
                     81: # if (defined(SVR4) || defined(AUX) || defined(DGUX)) && !defined(PCR)
                     82: #   define NEED_FIND_LIMIT
                     83: # endif
                     84:
1.2       noro       85: # if defined(LINUX) && \
1.3       noro       86:      (defined(POWERPC) || defined(SPARC) || defined(ALPHA) || defined(IA64) \
                     87:       || defined(MIPS))
1.1       noro       88: #   define NEED_FIND_LIMIT
                     89: # endif
                     90:
                     91: #ifdef NEED_FIND_LIMIT
                     92: #   include <setjmp.h>
                     93: #endif
                     94:
                     95: #ifdef FREEBSD
                     96: #  include <machine/trap.h>
                     97: #endif
                     98:
                     99: #ifdef AMIGA
1.4     ! noro      100: # define GC_AMIGA_DEF
        !           101: # include "AmigaOS.c"
        !           102: # undef GC_AMIGA_DEF
1.1       noro      103: #endif
                    104:
1.4     ! noro      105: #if defined(MSWIN32) || defined(MSWINCE)
1.1       noro      106: # define WIN32_LEAN_AND_MEAN
                    107: # define NOSERVICE
                    108: # include <windows.h>
                    109: #endif
                    110:
                    111: #ifdef MACOS
                    112: # include <Processes.h>
                    113: #endif
                    114:
                    115: #ifdef IRIX5
                    116: # include <sys/uio.h>
                    117: # include <malloc.h>   /* for locking */
                    118: #endif
                    119: #ifdef USE_MMAP
                    120: # include <sys/types.h>
                    121: # include <sys/mman.h>
                    122: # include <sys/stat.h>
1.4     ! noro      123: #endif
        !           124:
        !           125: #ifdef UNIX_LIKE
1.1       noro      126: # include <fcntl.h>
                    127: #endif
                    128:
                    129: #ifdef SUNOS5SIGS
                    130: # include <sys/siginfo.h>
                    131: # undef setjmp
                    132: # undef longjmp
                    133: # define setjmp(env) sigsetjmp(env, 1)
                    134: # define longjmp(env, val) siglongjmp(env, val)
                    135: # define jmp_buf sigjmp_buf
                    136: #endif
                    137:
                    138: #ifdef DJGPP
1.4     ! noro      139:   /* Apparently necessary for djgpp 2.01.  May cause problems with     */
1.1       noro      140:   /* other versions.                                                   */
                    141:   typedef long unsigned int caddr_t;
                    142: #endif
                    143:
                    144: #ifdef PCR
                    145: # include "il/PCR_IL.h"
                    146: # include "th/PCR_ThCtl.h"
                    147: # include "mm/PCR_MM.h"
                    148: #endif
                    149:
                    150: #if !defined(NO_EXECUTE_PERMISSION)
                    151: # define OPT_PROT_EXEC PROT_EXEC
                    152: #else
                    153: # define OPT_PROT_EXEC 0
                    154: #endif
                    155:
1.3       noro      156: #if defined(SEARCH_FOR_DATA_START)
1.1       noro      157:   /* The I386 case can be handled without a search.  The Alpha case    */
                    158:   /* used to be handled differently as well, but the rules changed     */
                    159:   /* for recent Linux versions.  This seems to be the easiest way to   */
                    160:   /* cover all versions.                                               */
1.4     ! noro      161:
        !           162: # ifdef LINUX
        !           163: #   pragma weak __data_start
        !           164:     extern int __data_start;
        !           165: #   pragma weak data_start
        !           166:     extern int data_start;
        !           167: # endif /* LINUX */
        !           168:   extern int _end;
        !           169:
1.1       noro      170:   ptr_t GC_data_start;
                    171:
                    172:   void GC_init_linux_data_start()
                    173:   {
                    174:     extern ptr_t GC_find_limit();
                    175:
1.4     ! noro      176: #   ifdef LINUX
        !           177:       /* Try the easy approaches first:        */
        !           178:       if (&__data_start != 0) {
        !           179:          GC_data_start = (ptr_t)(&__data_start);
        !           180:          return;
        !           181:       }
        !           182:       if (&data_start != 0) {
        !           183:          GC_data_start = (ptr_t)(&data_start);
        !           184:          return;
        !           185:       }
        !           186: #   endif /* LINUX */
        !           187:     GC_data_start = GC_find_limit((ptr_t)(&_end), FALSE);
        !           188:   }
        !           189: #endif
        !           190:
        !           191: #if defined(NETBSD) && defined(__ELF__)
        !           192:   ptr_t GC_data_start;
        !           193:
        !           194:   void GC_init_netbsd_elf()
        !           195:   {
        !           196:     extern ptr_t GC_find_limit();
        !           197:     extern char **environ;
        !           198:        /* This may need to be environ, without the underscore, for     */
        !           199:        /* some versions.                                               */
        !           200:     GC_data_start = GC_find_limit((ptr_t)&environ, FALSE);
1.1       noro      201:   }
                    202: #endif
                    203:
                    204: # ifdef OS2
                    205:
                    206: # include <stddef.h>
                    207:
                    208: # if !defined(__IBMC__) && !defined(__WATCOMC__) /* e.g. EMX */
                    209:
                    210: struct exe_hdr {
                    211:     unsigned short      magic_number;
                    212:     unsigned short      padding[29];
                    213:     long                new_exe_offset;
                    214: };
                    215:
                    216: #define E_MAGIC(x)      (x).magic_number
                    217: #define EMAGIC          0x5A4D
                    218: #define E_LFANEW(x)     (x).new_exe_offset
                    219:
                    220: struct e32_exe {
                    221:     unsigned char       magic_number[2];
                    222:     unsigned char       byte_order;
                    223:     unsigned char       word_order;
                    224:     unsigned long       exe_format_level;
                    225:     unsigned short      cpu;
                    226:     unsigned short      os;
                    227:     unsigned long       padding1[13];
                    228:     unsigned long       object_table_offset;
                    229:     unsigned long       object_count;
                    230:     unsigned long       padding2[31];
                    231: };
                    232:
                    233: #define E32_MAGIC1(x)   (x).magic_number[0]
                    234: #define E32MAGIC1       'L'
                    235: #define E32_MAGIC2(x)   (x).magic_number[1]
                    236: #define E32MAGIC2       'X'
                    237: #define E32_BORDER(x)   (x).byte_order
                    238: #define E32LEBO         0
                    239: #define E32_WORDER(x)   (x).word_order
                    240: #define E32LEWO         0
                    241: #define E32_CPU(x)      (x).cpu
                    242: #define E32CPU286       1
                    243: #define E32_OBJTAB(x)   (x).object_table_offset
                    244: #define E32_OBJCNT(x)   (x).object_count
                    245:
                    246: struct o32_obj {
                    247:     unsigned long       size;
                    248:     unsigned long       base;
                    249:     unsigned long       flags;
                    250:     unsigned long       pagemap;
                    251:     unsigned long       mapsize;
                    252:     unsigned long       reserved;
                    253: };
                    254:
                    255: #define O32_FLAGS(x)    (x).flags
                    256: #define OBJREAD         0x0001L
                    257: #define OBJWRITE        0x0002L
                    258: #define OBJINVALID      0x0080L
                    259: #define O32_SIZE(x)     (x).size
                    260: #define O32_BASE(x)     (x).base
                    261:
                    262: # else  /* IBM's compiler */
                    263:
                    264: /* A kludge to get around what appears to be a header file bug */
                    265: # ifndef WORD
                    266: #   define WORD unsigned short
                    267: # endif
                    268: # ifndef DWORD
                    269: #   define DWORD unsigned long
                    270: # endif
                    271:
                    272: # define EXE386 1
                    273: # include <newexe.h>
                    274: # include <exe386.h>
                    275:
                    276: # endif  /* __IBMC__ */
                    277:
                    278: # define INCL_DOSEXCEPTIONS
                    279: # define INCL_DOSPROCESS
                    280: # define INCL_DOSERRORS
                    281: # define INCL_DOSMODULEMGR
                    282: # define INCL_DOSMEMMGR
                    283: # include <os2.h>
                    284:
                    285:
                    286: /* Disable and enable signals during nontrivial allocations    */
                    287:
                    288: void GC_disable_signals(void)
                    289: {
                    290:     ULONG nest;
                    291:
                    292:     DosEnterMustComplete(&nest);
                    293:     if (nest != 1) ABORT("nested GC_disable_signals");
                    294: }
                    295:
                    296: void GC_enable_signals(void)
                    297: {
                    298:     ULONG nest;
                    299:
                    300:     DosExitMustComplete(&nest);
                    301:     if (nest != 0) ABORT("GC_enable_signals");
                    302: }
                    303:
                    304:
                    305: # else
                    306:
                    307: #  if !defined(PCR) && !defined(AMIGA) && !defined(MSWIN32) \
1.4     ! noro      308:       && !defined(MSWINCE) \
1.1       noro      309:       && !defined(MACOS) && !defined(DJGPP) && !defined(DOS4GW)
                    310:
                    311: #   if defined(sigmask) && !defined(UTS4)
                    312:        /* Use the traditional BSD interface */
                    313: #      define SIGSET_T int
                    314: #      define SIG_DEL(set, signal) (set) &= ~(sigmask(signal))
                    315: #      define SIG_FILL(set)  (set) = 0x7fffffff
                    316:          /* Setting the leading bit appears to provoke a bug in some   */
                    317:          /* longjmp implementations.  Most systems appear not to have  */
                    318:          /* a signal 32.                                               */
                    319: #      define SIGSETMASK(old, new) (old) = sigsetmask(new)
                    320: #   else
                    321:        /* Use POSIX/SYSV interface     */
                    322: #      define SIGSET_T sigset_t
                    323: #      define SIG_DEL(set, signal) sigdelset(&(set), (signal))
                    324: #      define SIG_FILL(set) sigfillset(&set)
                    325: #      define SIGSETMASK(old, new) sigprocmask(SIG_SETMASK, &(new), &(old))
                    326: #   endif
                    327:
                    328: static GC_bool mask_initialized = FALSE;
                    329:
                    330: static SIGSET_T new_mask;
                    331:
                    332: static SIGSET_T old_mask;
                    333:
                    334: static SIGSET_T dummy;
                    335:
                    336: #if defined(PRINTSTATS) && !defined(THREADS)
                    337: # define CHECK_SIGNALS
                    338:   int GC_sig_disabled = 0;
                    339: #endif
                    340:
                    341: void GC_disable_signals()
                    342: {
                    343:     if (!mask_initialized) {
                    344:        SIG_FILL(new_mask);
                    345:
                    346:        SIG_DEL(new_mask, SIGSEGV);
                    347:        SIG_DEL(new_mask, SIGILL);
                    348:        SIG_DEL(new_mask, SIGQUIT);
                    349: #      ifdef SIGBUS
                    350:            SIG_DEL(new_mask, SIGBUS);
                    351: #      endif
                    352: #      ifdef SIGIOT
                    353:            SIG_DEL(new_mask, SIGIOT);
                    354: #      endif
                    355: #      ifdef SIGEMT
                    356:            SIG_DEL(new_mask, SIGEMT);
                    357: #      endif
                    358: #      ifdef SIGTRAP
                    359:            SIG_DEL(new_mask, SIGTRAP);
                    360: #      endif
                    361:        mask_initialized = TRUE;
                    362:     }
                    363: #   ifdef CHECK_SIGNALS
                    364:        if (GC_sig_disabled != 0) ABORT("Nested disables");
                    365:        GC_sig_disabled++;
                    366: #   endif
                    367:     SIGSETMASK(old_mask,new_mask);
                    368:        if ( critical_when_signal )
                    369:                inside_critical_section = 1;
                    370:        else {
                    371:                inside_critical_section = 0;
                    372:                critical_when_signal = 1;
                    373:        }
                    374: }
                    375:
                    376: void GC_enable_signals()
                    377: {
                    378: #   ifdef CHECK_SIGNALS
                    379:        if (GC_sig_disabled != 1) ABORT("Unmatched enable");
                    380:        GC_sig_disabled--;
                    381: #   endif
                    382:     SIGSETMASK(dummy,old_mask);
                    383:        if ( !inside_critical_section ) {
                    384:                critical_when_signal = 0;
                    385:                if ( ox_usr1_sent ) {
                    386:                        ox_usr1_sent = 0; ox_usr1_handler();
                    387:                }
                    388:                if ( ox_int_received ) {
                    389:                        ox_int_received = 0; int_handler();
                    390:                }
                    391:        } else
                    392:                inside_critical_section = 0;
                    393: }
                    394:
                    395: #  endif  /* !PCR */
                    396:
                    397: # endif /*!OS/2 */
                    398:
                    399: /* Ivan Demakov: simplest way (to me) */
                    400: #ifdef DOS4GW
                    401:   void GC_disable_signals() { }
                    402:   void GC_enable_signals() { }
                    403: #endif
                    404:
                    405: /* Find the page size */
                    406: word GC_page_size;
                    407:
1.4     ! noro      408: # if defined(MSWIN32) || defined(MSWINCE)
1.1       noro      409:   void GC_setpagesize()
                    410:   {
1.4     ! noro      411:     GetSystemInfo(&GC_sysinfo);
        !           412:     GC_page_size = GC_sysinfo.dwPageSize;
1.1       noro      413:   }
                    414:
                    415: # else
                    416: #   if defined(MPROTECT_VDB) || defined(PROC_VDB) || defined(USE_MMAP) \
                    417:        || defined(USE_MUNMAP)
                    418:        void GC_setpagesize()
                    419:        {
                    420:            GC_page_size = GETPAGESIZE();
                    421:        }
                    422: #   else
                    423:        /* It's acceptable to fake it. */
                    424:        void GC_setpagesize()
                    425:        {
                    426:            GC_page_size = HBLKSIZE;
                    427:        }
                    428: #   endif
                    429: # endif
                    430:
                    431: /*
                    432:  * Find the base of the stack.
                    433:  * Used only in single-threaded environment.
                    434:  * With threads, GC_mark_roots needs to know how to do this.
                    435:  * Called with allocator lock held.
                    436:  */
1.4     ! noro      437: # if defined(MSWIN32) || defined(MSWINCE)
1.1       noro      438: # define is_writable(prot) ((prot) == PAGE_READWRITE \
                    439:                            || (prot) == PAGE_WRITECOPY \
                    440:                            || (prot) == PAGE_EXECUTE_READWRITE \
                    441:                            || (prot) == PAGE_EXECUTE_WRITECOPY)
                    442: /* Return the number of bytes that are writable starting at p. */
                    443: /* The pointer p is assumed to be page aligned.                        */
                    444: /* If base is not 0, *base becomes the beginning of the        */
                    445: /* allocation region containing p.                             */
                    446: word GC_get_writable_length(ptr_t p, ptr_t *base)
                    447: {
                    448:     MEMORY_BASIC_INFORMATION buf;
                    449:     word result;
                    450:     word protect;
                    451:
                    452:     result = VirtualQuery(p, &buf, sizeof(buf));
                    453:     if (result != sizeof(buf)) ABORT("Weird VirtualQuery result");
                    454:     if (base != 0) *base = (ptr_t)(buf.AllocationBase);
                    455:     protect = (buf.Protect & ~(PAGE_GUARD | PAGE_NOCACHE));
                    456:     if (!is_writable(protect)) {
                    457:         return(0);
                    458:     }
                    459:     if (buf.State != MEM_COMMIT) return(0);
                    460:     return(buf.RegionSize);
                    461: }
                    462:
                    463: ptr_t GC_get_stack_base()
                    464: {
                    465:     int dummy;
                    466:     ptr_t sp = (ptr_t)(&dummy);
                    467:     ptr_t trunc_sp = (ptr_t)((word)sp & ~(GC_page_size - 1));
                    468:     word size = GC_get_writable_length(trunc_sp, 0);
                    469:
                    470:     return(trunc_sp + size);
                    471: }
                    472:
                    473:
1.4     ! noro      474: # endif /* MS Windows */
        !           475:
        !           476: # ifdef BEOS
        !           477: # include <kernel/OS.h>
        !           478: ptr_t GC_get_stack_base(){
        !           479:        thread_info th;
        !           480:        get_thread_info(find_thread(NULL),&th);
        !           481:        return th.stack_end;
        !           482: }
        !           483: # endif /* BEOS */
        !           484:
1.1       noro      485:
                    486: # ifdef OS2
                    487:
                    488: ptr_t GC_get_stack_base()
                    489: {
                    490:     PTIB ptib;
                    491:     PPIB ppib;
                    492:
                    493:     if (DosGetInfoBlocks(&ptib, &ppib) != NO_ERROR) {
                    494:        GC_err_printf0("DosGetInfoBlocks failed\n");
                    495:        ABORT("DosGetInfoBlocks failed\n");
                    496:     }
                    497:     return((ptr_t)(ptib -> tib_pstacklimit));
                    498: }
                    499:
1.4     ! noro      500: # endif /* OS2 */
1.1       noro      501:
                    502: # ifdef AMIGA
1.4     ! noro      503: #   define GC_AMIGA_SB
        !           504: #   include "AmigaOS.c"
        !           505: #   undef GC_AMIGA_SB
        !           506: # endif /* AMIGA */
1.1       noro      507:
1.4     ! noro      508: # if defined(NEED_FIND_LIMIT) || defined(UNIX_LIKE)
1.1       noro      509:
                    510: #   ifdef __STDC__
                    511:        typedef void (*handler)(int);
                    512: #   else
                    513:        typedef void (*handler)();
                    514: #   endif
                    515:
                    516: #   if defined(SUNOS5SIGS) || defined(IRIX5) || defined(OSF1)
                    517:        static struct sigaction old_segv_act;
1.2       noro      518: #      if defined(_sigargs) || defined(HPUX) /* !Irix6.x */
1.1       noro      519:            static struct sigaction old_bus_act;
                    520: #      endif
                    521: #   else
                    522:         static handler old_segv_handler, old_bus_handler;
                    523: #   endif
                    524:
1.4     ! noro      525: #   ifdef __STDC__
        !           526:       void GC_set_and_save_fault_handler(handler h)
        !           527: #   else
        !           528:       void GC_set_and_save_fault_handler(h)
        !           529:       handler h;
        !           530: #   endif
1.1       noro      531:     {
                    532: #      if defined(SUNOS5SIGS) || defined(IRIX5) || defined(OSF1)
                    533:          struct sigaction      act;
                    534:
1.4     ! noro      535:          act.sa_handler        = h;
1.1       noro      536:           act.sa_flags          = SA_RESTART | SA_NODEFER;
                    537:           /* The presence of SA_NODEFER represents yet another gross    */
                    538:           /* hack.  Under Solaris 2.3, siglongjmp doesn't appear to     */
                    539:           /* interact correctly with -lthread.  We hide the confusion   */
                    540:           /* by making sure that signal handling doesn't affect the     */
                    541:           /* signal mask.                                               */
                    542:
                    543:          (void) sigemptyset(&act.sa_mask);
                    544: #        ifdef IRIX_THREADS
                    545:                /* Older versions have a bug related to retrieving and  */
                    546:                /* and setting a handler at the same time.              */
                    547:                (void) sigaction(SIGSEGV, 0, &old_segv_act);
                    548:                (void) sigaction(SIGSEGV, &act, 0);
                    549: #        else
                    550:                (void) sigaction(SIGSEGV, &act, &old_segv_act);
1.2       noro      551: #              if defined(IRIX5) && defined(_sigargs) /* Irix 5.x, not 6.x */ \
                    552:                   || defined(HPUX)
                    553:                    /* Under Irix 5.x or HP/UX, we may get SIGBUS.      */
                    554:                    /* Pthreads doesn't exist under Irix 5.x, so we     */
                    555:                    /* don't have to worry in the threads case.         */
1.1       noro      556:                    (void) sigaction(SIGBUS, &act, &old_bus_act);
                    557: #              endif
                    558: #        endif /* IRIX_THREADS */
                    559: #      else
1.4     ! noro      560:          old_segv_handler = signal(SIGSEGV, h);
1.1       noro      561: #        ifdef SIGBUS
1.4     ! noro      562:            old_bus_handler = signal(SIGBUS, h);
1.1       noro      563: #        endif
                    564: #      endif
                    565:     }
1.4     ! noro      566: # endif /* NEED_FIND_LIMIT || UNIX_LIKE */
        !           567:
        !           568: # ifdef NEED_FIND_LIMIT
        !           569:   /* Some tools to implement HEURISTIC2        */
        !           570: #   define MIN_PAGE_SIZE 256   /* Smallest conceivable page size, bytes */
        !           571:     /* static */ jmp_buf GC_jmp_buf;
        !           572:
        !           573:     /*ARGSUSED*/
        !           574:     void GC_fault_handler(sig)
        !           575:     int sig;
        !           576:     {
        !           577:         longjmp(GC_jmp_buf, 1);
        !           578:     }
        !           579:
        !           580:     void GC_setup_temporary_fault_handler()
        !           581:     {
        !           582:        GC_set_and_save_fault_handler(GC_fault_handler);
        !           583:     }
1.1       noro      584:
                    585:     void GC_reset_fault_handler()
                    586:     {
                    587: #       if defined(SUNOS5SIGS) || defined(IRIX5) || defined(OSF1)
                    588:          (void) sigaction(SIGSEGV, &old_segv_act, 0);
1.2       noro      589: #        if defined(IRIX5) && defined(_sigargs) /* Irix 5.x, not 6.x */ \
                    590:             || defined(HPUX)
1.1       noro      591:              (void) sigaction(SIGBUS, &old_bus_act, 0);
                    592: #        endif
                    593: #       else
                    594:          (void) signal(SIGSEGV, old_segv_handler);
                    595: #        ifdef SIGBUS
                    596:            (void) signal(SIGBUS, old_bus_handler);
                    597: #        endif
                    598: #       endif
                    599:     }
                    600:
                    601:     /* Return the first nonaddressible location > p (up) or    */
                    602:     /* the smallest location q s.t. [q,p] is addressible (!up).        */
                    603:     ptr_t GC_find_limit(p, up)
                    604:     ptr_t p;
                    605:     GC_bool up;
                    606:     {
                    607:         static VOLATILE ptr_t result;
                    608:                /* Needs to be static, since otherwise it may not be    */
                    609:                /* preserved across the longjmp.  Can safely be         */
                    610:                /* static since it's only called once, with the         */
                    611:                /* allocation lock held.                                */
                    612:
                    613:
                    614:        GC_setup_temporary_fault_handler();
                    615:        if (setjmp(GC_jmp_buf) == 0) {
                    616:            result = (ptr_t)(((word)(p))
                    617:                              & ~(MIN_PAGE_SIZE-1));
                    618:            for (;;) {
                    619:                if (up) {
                    620:                    result += MIN_PAGE_SIZE;
                    621:                } else {
                    622:                    result -= MIN_PAGE_SIZE;
                    623:                }
                    624:                GC_noop1((word)(*result));
                    625:            }
                    626:        }
                    627:        GC_reset_fault_handler();
                    628:        if (!up) {
                    629:            result += MIN_PAGE_SIZE;
                    630:        }
                    631:        return(result);
                    632:     }
                    633: # endif
                    634:
1.2       noro      635: #ifdef LINUX_STACKBOTTOM
                    636:
1.3       noro      637: #include <sys/types.h>
                    638: #include <sys/stat.h>
                    639:
1.2       noro      640: # define STAT_SKIP 27   /* Number of fields preceding startstack       */
1.3       noro      641:                        /* field in /proc/self/stat                     */
1.2       noro      642:
1.4     ! noro      643: # pragma weak __libc_stack_end
        !           644:   extern ptr_t __libc_stack_end;
        !           645:
        !           646: # ifdef IA64
        !           647: #   pragma weak __libc_ia64_register_backing_store_base
        !           648:     extern ptr_t __libc_ia64_register_backing_store_base;
        !           649:
        !           650:     ptr_t GC_get_register_stack_base(void)
        !           651:     {
        !           652:       if (0 != &__libc_ia64_register_backing_store_base) {
        !           653:        return __libc_ia64_register_backing_store_base;
        !           654:       } else {
        !           655:        word result = (word)GC_stackbottom - BACKING_STORE_DISPLACEMENT;
        !           656:        result += BACKING_STORE_ALIGNMENT - 1;
        !           657:        result &= ~(BACKING_STORE_ALIGNMENT - 1);
        !           658:        return (ptr_t)result;
        !           659:       }
        !           660:     }
        !           661: # endif
        !           662:
1.2       noro      663:   ptr_t GC_linux_stack_base(void)
                    664:   {
1.3       noro      665:     /* We read the stack base value from /proc/self/stat.  We do this  */
                    666:     /* using direct I/O system calls in order to avoid calling malloc   */
                    667:     /* in case REDIRECT_MALLOC is defined.                             */
                    668: #   define STAT_BUF_SIZE 4096
1.4     ! noro      669: #   if defined(GC_USE_LD_WRAP)
1.3       noro      670: #      define STAT_READ __real_read
                    671: #   else
                    672: #      define STAT_READ read
                    673: #   endif
                    674:     char stat_buf[STAT_BUF_SIZE];
                    675:     int f;
1.2       noro      676:     char c;
                    677:     word result = 0;
1.3       noro      678:     size_t i, buf_offset = 0;
1.2       noro      679:
1.4     ! noro      680:     /* First try the easy way.  This should work for glibc 2.2 */
        !           681:       if (0 != &__libc_stack_end) {
        !           682:        return __libc_stack_end;
        !           683:       }
1.3       noro      684:     f = open("/proc/self/stat", O_RDONLY);
                    685:     if (f < 0 || STAT_READ(f, stat_buf, STAT_BUF_SIZE) < 2 * STAT_SKIP) {
                    686:        ABORT("Couldn't read /proc/self/stat");
                    687:     }
                    688:     c = stat_buf[buf_offset++];
1.2       noro      689:     /* Skip the required number of fields.  This number is hopefully   */
                    690:     /* constant across all Linux implementations.                      */
                    691:       for (i = 0; i < STAT_SKIP; ++i) {
1.3       noro      692:        while (isspace(c)) c = stat_buf[buf_offset++];
                    693:        while (!isspace(c)) c = stat_buf[buf_offset++];
1.2       noro      694:       }
1.3       noro      695:     while (isspace(c)) c = stat_buf[buf_offset++];
1.2       noro      696:     while (isdigit(c)) {
                    697:       result *= 10;
                    698:       result += c - '0';
1.3       noro      699:       c = stat_buf[buf_offset++];
1.2       noro      700:     }
1.3       noro      701:     close(f);
1.2       noro      702:     if (result < 0x10000000) ABORT("Absurd stack bottom value");
                    703:     return (ptr_t)result;
                    704:   }
                    705:
                    706: #endif /* LINUX_STACKBOTTOM */
1.1       noro      707:
1.4     ! noro      708: #ifdef FREEBSD_STACKBOTTOM
        !           709:
        !           710: /* This uses an undocumented sysctl call, but at least one expert      */
        !           711: /* believes it will stay.                                              */
        !           712:
        !           713: #include <unistd.h>
        !           714: #include <sys/types.h>
        !           715: #include <sys/sysctl.h>
        !           716:
        !           717:   ptr_t GC_freebsd_stack_base(void)
        !           718:   {
        !           719:     int nm[2] = { CTL_KERN, KERN_USRSTACK}, base, len, r;
        !           720:
        !           721:     len = sizeof(int);
        !           722:     r = sysctl(nm, 2, &base, &len, NULL, 0);
        !           723:
        !           724:     if (r) ABORT("Error getting stack base");
        !           725:
        !           726:     return (ptr_t)base;
        !           727:   }
        !           728:
        !           729: #endif /* FREEBSD_STACKBOTTOM */
        !           730:
        !           731: #if !defined(BEOS) && !defined(AMIGA) && !defined(MSWIN32) \
        !           732:     && !defined(MSWINCE) && !defined(OS2)
        !           733:
1.1       noro      734: ptr_t GC_get_stack_base()
                    735: {
                    736:     word dummy;
                    737:     ptr_t result;
                    738:
                    739: #   define STACKBOTTOM_ALIGNMENT_M1 ((word)STACK_GRAN - 1)
                    740:
                    741: #   ifdef STACKBOTTOM
                    742:        return(STACKBOTTOM);
                    743: #   else
                    744: #      ifdef HEURISTIC1
                    745: #         ifdef STACK_GROWS_DOWN
                    746:             result = (ptr_t)((((word)(&dummy))
                    747:                               + STACKBOTTOM_ALIGNMENT_M1)
                    748:                              & ~STACKBOTTOM_ALIGNMENT_M1);
                    749: #         else
                    750:             result = (ptr_t)(((word)(&dummy))
                    751:                              & ~STACKBOTTOM_ALIGNMENT_M1);
                    752: #         endif
                    753: #      endif /* HEURISTIC1 */
1.2       noro      754: #      ifdef LINUX_STACKBOTTOM
                    755:           result = GC_linux_stack_base();
                    756: #      endif
1.4     ! noro      757: #      ifdef FREEBSD_STACKBOTTOM
        !           758:           result = GC_freebsd_stack_base();
        !           759: #      endif
1.1       noro      760: #      ifdef HEURISTIC2
                    761: #          ifdef STACK_GROWS_DOWN
                    762:                result = GC_find_limit((ptr_t)(&dummy), TRUE);
                    763: #              ifdef HEURISTIC2_LIMIT
                    764:                    if (result > HEURISTIC2_LIMIT
                    765:                        && (ptr_t)(&dummy) < HEURISTIC2_LIMIT) {
                    766:                            result = HEURISTIC2_LIMIT;
                    767:                    }
                    768: #              endif
                    769: #          else
                    770:                result = GC_find_limit((ptr_t)(&dummy), FALSE);
                    771: #              ifdef HEURISTIC2_LIMIT
                    772:                    if (result < HEURISTIC2_LIMIT
                    773:                        && (ptr_t)(&dummy) > HEURISTIC2_LIMIT) {
                    774:                            result = HEURISTIC2_LIMIT;
                    775:                    }
                    776: #              endif
                    777: #          endif
                    778:
                    779: #      endif /* HEURISTIC2 */
                    780: #      ifdef STACK_GROWS_DOWN
                    781:            if (result == 0) result = (ptr_t)(signed_word)(-sizeof(ptr_t));
                    782: #      endif
                    783:        return(result);
                    784: #   endif /* STACKBOTTOM */
                    785: }
                    786:
1.4     ! noro      787: # endif /* ! AMIGA, !OS 2, ! MS Windows, !BEOS */
1.1       noro      788:
                    789: /*
                    790:  * Register static data segment(s) as roots.
                    791:  * If more data segments are added later then they need to be registered
                    792:  * add that point (as we do with SunOS dynamic loading),
                    793:  * or GC_mark_roots needs to check for them (as we do with PCR).
                    794:  * Called with allocator lock held.
                    795:  */
                    796:
                    797: # ifdef OS2
                    798:
                    799: void GC_register_data_segments()
                    800: {
                    801:     PTIB ptib;
                    802:     PPIB ppib;
                    803:     HMODULE module_handle;
                    804: #   define PBUFSIZ 512
                    805:     UCHAR path[PBUFSIZ];
                    806:     FILE * myexefile;
                    807:     struct exe_hdr hdrdos;     /* MSDOS header.        */
                    808:     struct e32_exe hdr386;     /* Real header for my executable */
                    809:     struct o32_obj seg;        /* Currrent segment */
                    810:     int nsegs;
                    811:
                    812:
                    813:     if (DosGetInfoBlocks(&ptib, &ppib) != NO_ERROR) {
                    814:        GC_err_printf0("DosGetInfoBlocks failed\n");
                    815:        ABORT("DosGetInfoBlocks failed\n");
                    816:     }
                    817:     module_handle = ppib -> pib_hmte;
                    818:     if (DosQueryModuleName(module_handle, PBUFSIZ, path) != NO_ERROR) {
                    819:        GC_err_printf0("DosQueryModuleName failed\n");
                    820:        ABORT("DosGetInfoBlocks failed\n");
                    821:     }
                    822:     myexefile = fopen(path, "rb");
                    823:     if (myexefile == 0) {
                    824:         GC_err_puts("Couldn't open executable ");
                    825:         GC_err_puts(path); GC_err_puts("\n");
                    826:         ABORT("Failed to open executable\n");
                    827:     }
                    828:     if (fread((char *)(&hdrdos), 1, sizeof hdrdos, myexefile) < sizeof hdrdos) {
                    829:         GC_err_puts("Couldn't read MSDOS header from ");
                    830:         GC_err_puts(path); GC_err_puts("\n");
                    831:         ABORT("Couldn't read MSDOS header");
                    832:     }
                    833:     if (E_MAGIC(hdrdos) != EMAGIC) {
                    834:         GC_err_puts("Executable has wrong DOS magic number: ");
                    835:         GC_err_puts(path); GC_err_puts("\n");
                    836:         ABORT("Bad DOS magic number");
                    837:     }
                    838:     if (fseek(myexefile, E_LFANEW(hdrdos), SEEK_SET) != 0) {
                    839:         GC_err_puts("Seek to new header failed in ");
                    840:         GC_err_puts(path); GC_err_puts("\n");
                    841:         ABORT("Bad DOS magic number");
                    842:     }
                    843:     if (fread((char *)(&hdr386), 1, sizeof hdr386, myexefile) < sizeof hdr386) {
                    844:         GC_err_puts("Couldn't read MSDOS header from ");
                    845:         GC_err_puts(path); GC_err_puts("\n");
                    846:         ABORT("Couldn't read OS/2 header");
                    847:     }
                    848:     if (E32_MAGIC1(hdr386) != E32MAGIC1 || E32_MAGIC2(hdr386) != E32MAGIC2) {
                    849:         GC_err_puts("Executable has wrong OS/2 magic number:");
                    850:         GC_err_puts(path); GC_err_puts("\n");
                    851:         ABORT("Bad OS/2 magic number");
                    852:     }
                    853:     if ( E32_BORDER(hdr386) != E32LEBO || E32_WORDER(hdr386) != E32LEWO) {
                    854:         GC_err_puts("Executable %s has wrong byte order: ");
                    855:         GC_err_puts(path); GC_err_puts("\n");
                    856:         ABORT("Bad byte order");
                    857:     }
                    858:     if ( E32_CPU(hdr386) == E32CPU286) {
                    859:         GC_err_puts("GC can't handle 80286 executables: ");
                    860:         GC_err_puts(path); GC_err_puts("\n");
                    861:         EXIT();
                    862:     }
                    863:     if (fseek(myexefile, E_LFANEW(hdrdos) + E32_OBJTAB(hdr386),
                    864:              SEEK_SET) != 0) {
                    865:         GC_err_puts("Seek to object table failed: ");
                    866:         GC_err_puts(path); GC_err_puts("\n");
                    867:         ABORT("Seek to object table failed");
                    868:     }
                    869:     for (nsegs = E32_OBJCNT(hdr386); nsegs > 0; nsegs--) {
                    870:       int flags;
                    871:       if (fread((char *)(&seg), 1, sizeof seg, myexefile) < sizeof seg) {
                    872:         GC_err_puts("Couldn't read obj table entry from ");
                    873:         GC_err_puts(path); GC_err_puts("\n");
                    874:         ABORT("Couldn't read obj table entry");
                    875:       }
                    876:       flags = O32_FLAGS(seg);
                    877:       if (!(flags & OBJWRITE)) continue;
                    878:       if (!(flags & OBJREAD)) continue;
                    879:       if (flags & OBJINVALID) {
                    880:           GC_err_printf0("Object with invalid pages?\n");
                    881:           continue;
                    882:       }
                    883:       GC_add_roots_inner(O32_BASE(seg), O32_BASE(seg)+O32_SIZE(seg), FALSE);
                    884:     }
                    885: }
                    886:
1.4     ! noro      887: # else /* !OS2 */
        !           888:
        !           889: # if defined(MSWIN32) || defined(MSWINCE)
1.1       noro      890:
                    891: # ifdef MSWIN32
                    892:   /* Unfortunately, we have to handle win32s very differently from NT,         */
                    893:   /* Since VirtualQuery has very different semantics.  In particular,  */
                    894:   /* under win32s a VirtualQuery call on an unmapped page returns an   */
                    895:   /* invalid result.  Under GC_register_data_segments is a noop and    */
                    896:   /* all real work is done by GC_register_dynamic_libraries.  Under    */
                    897:   /* win32s, we cannot find the data segments associated with dll's.   */
                    898:   /* We rgister the main data segment here.                            */
                    899:   GC_bool GC_win32s = FALSE;   /* We're running under win32s.  */
                    900:
                    901:   GC_bool GC_is_win32s()
                    902:   {
                    903:       DWORD v = GetVersion();
                    904:
                    905:       /* Check that this is not NT, and Windows major version <= 3     */
                    906:       return ((v & 0x80000000) && (v & 0xff) <= 3);
                    907:   }
                    908:
                    909:   void GC_init_win32()
                    910:   {
                    911:       GC_win32s = GC_is_win32s();
                    912:   }
1.4     ! noro      913:
1.1       noro      914:   /* Return the smallest address a such that VirtualQuery              */
                    915:   /* returns correct results for all addresses between a and start.    */
                    916:   /* Assumes VirtualQuery returns correct information for start.       */
                    917:   ptr_t GC_least_described_address(ptr_t start)
                    918:   {
                    919:     MEMORY_BASIC_INFORMATION buf;
                    920:     DWORD result;
                    921:     LPVOID limit;
                    922:     ptr_t p;
                    923:     LPVOID q;
                    924:
1.4     ! noro      925:     limit = GC_sysinfo.lpMinimumApplicationAddress;
1.1       noro      926:     p = (ptr_t)((word)start & ~(GC_page_size - 1));
                    927:     for (;;) {
                    928:        q = (LPVOID)(p - GC_page_size);
                    929:        if ((ptr_t)q > (ptr_t)p /* underflow */ || q < limit) break;
                    930:        result = VirtualQuery(q, &buf, sizeof(buf));
                    931:        if (result != sizeof(buf) || buf.AllocationBase == 0) break;
                    932:        p = (ptr_t)(buf.AllocationBase);
                    933:     }
                    934:     return(p);
                    935:   }
1.4     ! noro      936: # endif
1.1       noro      937:
                    938:   /* Is p the start of either the malloc heap, or of one of our */
                    939:   /* heap sections?                                            */
                    940:   GC_bool GC_is_heap_base (ptr_t p)
                    941:   {
                    942:
                    943:      register unsigned i;
                    944:
                    945: #    ifndef REDIRECT_MALLOC
                    946:        static ptr_t malloc_heap_pointer = 0;
                    947:
                    948:        if (0 == malloc_heap_pointer) {
                    949:          MEMORY_BASIC_INFORMATION buf;
1.4     ! noro      950:          void *pTemp = malloc( 1 );
        !           951:          register DWORD result = VirtualQuery(pTemp, &buf, sizeof(buf));
        !           952:
        !           953:          free( pTemp );
        !           954:
1.1       noro      955:
                    956:          if (result != sizeof(buf)) {
                    957:              ABORT("Weird VirtualQuery result");
                    958:          }
                    959:          malloc_heap_pointer = (ptr_t)(buf.AllocationBase);
                    960:        }
                    961:        if (p == malloc_heap_pointer) return(TRUE);
                    962: #    endif
                    963:      for (i = 0; i < GC_n_heap_bases; i++) {
                    964:          if (GC_heap_bases[i] == p) return(TRUE);
                    965:      }
                    966:      return(FALSE);
                    967:   }
1.4     ! noro      968:
        !           969: # ifdef MSWIN32
1.1       noro      970:   void GC_register_root_section(ptr_t static_root)
                    971:   {
                    972:       MEMORY_BASIC_INFORMATION buf;
                    973:       DWORD result;
                    974:       DWORD protect;
                    975:       LPVOID p;
                    976:       char * base;
                    977:       char * limit, * new_limit;
                    978:
                    979:       if (!GC_win32s) return;
                    980:       p = base = limit = GC_least_described_address(static_root);
1.4     ! noro      981:       while (p < GC_sysinfo.lpMaximumApplicationAddress) {
1.1       noro      982:         result = VirtualQuery(p, &buf, sizeof(buf));
                    983:         if (result != sizeof(buf) || buf.AllocationBase == 0
                    984:             || GC_is_heap_base(buf.AllocationBase)) break;
                    985:         new_limit = (char *)p + buf.RegionSize;
                    986:         protect = buf.Protect;
                    987:         if (buf.State == MEM_COMMIT
                    988:             && is_writable(protect)) {
                    989:             if ((char *)p == limit) {
                    990:                 limit = new_limit;
                    991:             } else {
                    992:                 if (base != limit) GC_add_roots_inner(base, limit, FALSE);
                    993:                 base = p;
                    994:                 limit = new_limit;
                    995:             }
                    996:         }
                    997:         if (p > (LPVOID)new_limit /* overflow */) break;
                    998:         p = (LPVOID)new_limit;
                    999:       }
                   1000:       if (base != limit) GC_add_roots_inner(base, limit, FALSE);
                   1001:   }
1.4     ! noro     1002: #endif
1.1       noro     1003:
                   1004:   void GC_register_data_segments()
                   1005:   {
1.4     ! noro     1006: #     ifdef MSWIN32
1.1       noro     1007:       static char dummy;
                   1008:       GC_register_root_section((ptr_t)(&dummy));
                   1009: #     endif
                   1010:   }
                   1011:
1.4     ! noro     1012: # else /* !OS2 && !Windows */
1.1       noro     1013:
1.4     ! noro     1014: # if (defined(SVR4) || defined(AUX) || defined(DGUX) \
        !          1015:       || (defined(LINUX) && defined(SPARC))) && !defined(PCR)
1.1       noro     1016: char * GC_SysVGetDataStart(max_page_size, etext_addr)
                   1017: int max_page_size;
                   1018: int * etext_addr;
                   1019: {
                   1020:     word text_end = ((word)(etext_addr) + sizeof(word) - 1)
                   1021:                    & ~(sizeof(word) - 1);
                   1022:        /* etext rounded to word boundary       */
                   1023:     word next_page = ((text_end + (word)max_page_size - 1)
                   1024:                      & ~((word)max_page_size - 1));
                   1025:     word page_offset = (text_end & ((word)max_page_size - 1));
                   1026:     VOLATILE char * result = (char *)(next_page + page_offset);
                   1027:     /* Note that this isnt equivalent to just adding           */
                   1028:     /* max_page_size to &etext if &etext is at a page boundary */
                   1029:
                   1030:     GC_setup_temporary_fault_handler();
                   1031:     if (setjmp(GC_jmp_buf) == 0) {
                   1032:        /* Try writing to the address.  */
                   1033:        *result = *result;
                   1034:         GC_reset_fault_handler();
                   1035:     } else {
                   1036:         GC_reset_fault_handler();
                   1037:        /* We got here via a longjmp.  The address is not readable.     */
                   1038:        /* This is known to happen under Solaris 2.4 + gcc, which place */
                   1039:        /* string constants in the text segment, but after etext.       */
                   1040:        /* Use plan B.  Note that we now know there is a gap between    */
                   1041:        /* text and data segments, so plan A bought us something.       */
                   1042:        result = (char *)GC_find_limit((ptr_t)(DATAEND) - MIN_PAGE_SIZE, FALSE);
                   1043:     }
                   1044:     return((char *)result);
                   1045: }
                   1046: # endif
                   1047:
                   1048:
1.4     ! noro     1049: #ifdef AMIGA
        !          1050:
        !          1051: #  define GC_AMIGA_DS
        !          1052: #  include "AmigaOS.c"
        !          1053: #  undef GC_AMIGA_DS
        !          1054:
        !          1055: #else /* !OS2 && !Windows && !AMIGA */
        !          1056:
1.1       noro     1057: void GC_register_data_segments()
                   1058: {
                   1059: #   if !defined(PCR) && !defined(SRC_M3) && !defined(NEXT) && !defined(MACOS) \
                   1060:        && !defined(MACOSX)
                   1061: #     if defined(REDIRECT_MALLOC) && defined(SOLARIS_THREADS)
                   1062:        /* As of Solaris 2.3, the Solaris threads implementation        */
                   1063:        /* allocates the data structure for the initial thread with     */
                   1064:        /* sbrk at process startup.  It needs to be scanned, so that    */
                   1065:        /* we don't lose some malloc allocated data structures          */
                   1066:        /* hanging from it.  We're on thin ice here ...                 */
                   1067:         extern caddr_t sbrk();
                   1068:
                   1069:        GC_add_roots_inner(DATASTART, (char *)sbrk(0), FALSE);
                   1070: #     else
                   1071:        GC_add_roots_inner(DATASTART, (char *)(DATAEND), FALSE);
                   1072: #     endif
                   1073: #   endif
                   1074: #   if !defined(PCR) && (defined(NEXT) || defined(MACOSX))
                   1075:       GC_add_roots_inner(DATASTART, (char *) get_end(), FALSE);
                   1076: #   endif
                   1077: #   if defined(MACOS)
                   1078:     {
                   1079: #   if defined(THINK_C)
                   1080:        extern void* GC_MacGetDataStart(void);
                   1081:        /* globals begin above stack and end at a5. */
                   1082:        GC_add_roots_inner((ptr_t)GC_MacGetDataStart(),
                   1083:                           (ptr_t)LMGetCurrentA5(), FALSE);
                   1084: #   else
                   1085: #     if defined(__MWERKS__)
                   1086: #       if !__POWERPC__
                   1087:          extern void* GC_MacGetDataStart(void);
                   1088:          /* MATTHEW: Function to handle Far Globals (CW Pro 3) */
                   1089: #         if __option(far_data)
                   1090:          extern void* GC_MacGetDataEnd(void);
                   1091: #         endif
                   1092:          /* globals begin above stack and end at a5. */
                   1093:          GC_add_roots_inner((ptr_t)GC_MacGetDataStart(),
                   1094:                             (ptr_t)LMGetCurrentA5(), FALSE);
                   1095:          /* MATTHEW: Handle Far Globals */
                   1096: #         if __option(far_data)
                   1097:       /* Far globals follow he QD globals: */
                   1098:          GC_add_roots_inner((ptr_t)LMGetCurrentA5(),
                   1099:                             (ptr_t)GC_MacGetDataEnd(), FALSE);
                   1100: #         endif
                   1101: #       else
                   1102:          extern char __data_start__[], __data_end__[];
                   1103:          GC_add_roots_inner((ptr_t)&__data_start__,
                   1104:                             (ptr_t)&__data_end__, FALSE);
                   1105: #       endif /* __POWERPC__ */
                   1106: #     endif /* __MWERKS__ */
                   1107: #   endif /* !THINK_C */
                   1108:     }
                   1109: #   endif /* MACOS */
                   1110:
                   1111:     /* Dynamic libraries are added at every collection, since they may  */
                   1112:     /* change.                                                         */
                   1113: }
                   1114:
                   1115: # endif  /* ! AMIGA */
1.4     ! noro     1116: # endif  /* ! MSWIN32 && ! MSWINCE*/
1.1       noro     1117: # endif  /* ! OS2 */
                   1118:
                   1119: /*
                   1120:  * Auxiliary routines for obtaining memory from OS.
                   1121:  */
1.4     ! noro     1122:
1.1       noro     1123: # if !defined(OS2) && !defined(PCR) && !defined(AMIGA) \
1.4     ! noro     1124:        && !defined(MSWIN32) && !defined(MSWINCE) \
        !          1125:        && !defined(MACOS) && !defined(DOS4GW)
1.1       noro     1126:
                   1127: # ifdef SUNOS4
                   1128:     extern caddr_t sbrk();
                   1129: # endif
                   1130: # ifdef __STDC__
                   1131: #   define SBRK_ARG_T ptrdiff_t
                   1132: # else
                   1133: #   define SBRK_ARG_T int
                   1134: # endif
                   1135:
1.4     ! noro     1136:
1.1       noro     1137: # ifdef RS6000
                   1138: /* The compiler seems to generate speculative reads one past the end of        */
                   1139: /* an allocated object.  Hence we need to make sure that the page      */
                   1140: /* following the last heap page is also mapped.                                */
                   1141: ptr_t GC_unix_get_mem(bytes)
                   1142: word bytes;
                   1143: {
                   1144:     caddr_t cur_brk = (caddr_t)sbrk(0);
                   1145:     caddr_t result;
                   1146:     SBRK_ARG_T lsbs = (word)cur_brk & (GC_page_size-1);
                   1147:     static caddr_t my_brk_val = 0;
                   1148:
                   1149:     if ((SBRK_ARG_T)bytes < 0) return(0); /* too big */
                   1150:     if (lsbs != 0) {
                   1151:         if((caddr_t)(sbrk(GC_page_size - lsbs)) == (caddr_t)(-1)) return(0);
                   1152:     }
                   1153:     if (cur_brk == my_brk_val) {
                   1154:        /* Use the extra block we allocated last time. */
                   1155:         result = (ptr_t)sbrk((SBRK_ARG_T)bytes);
                   1156:         if (result == (caddr_t)(-1)) return(0);
                   1157:         result -= GC_page_size;
                   1158:     } else {
                   1159:         result = (ptr_t)sbrk(GC_page_size + (SBRK_ARG_T)bytes);
                   1160:         if (result == (caddr_t)(-1)) return(0);
                   1161:     }
                   1162:     my_brk_val = result + bytes + GC_page_size;        /* Always page aligned */
                   1163:     return((ptr_t)result);
                   1164: }
                   1165:
                   1166: #else  /* Not RS6000 */
                   1167:
                   1168: #if defined(USE_MMAP)
1.4     ! noro     1169: /* Tested only under Linux, IRIX5 and Solaris 2 */
1.1       noro     1170:
                   1171: #ifdef USE_MMAP_FIXED
                   1172: #   define GC_MMAP_FLAGS MAP_FIXED | MAP_PRIVATE
                   1173:        /* Seems to yield better performance on Solaris 2, but can      */
                   1174:        /* be unreliable if something is already mapped at the address. */
                   1175: #else
                   1176: #   define GC_MMAP_FLAGS MAP_PRIVATE
                   1177: #endif
                   1178:
1.4     ! noro     1179: #ifndef HEAP_START
        !          1180: #   define HEAP_START 0
        !          1181: #endif
        !          1182:
1.1       noro     1183: ptr_t GC_unix_get_mem(bytes)
                   1184: word bytes;
                   1185: {
                   1186:     static GC_bool initialized = FALSE;
                   1187:     static int fd;
                   1188:     void *result;
                   1189:     static ptr_t last_addr = HEAP_START;
                   1190:
                   1191:     if (!initialized) {
                   1192:        fd = open("/dev/zero", O_RDONLY);
                   1193:        initialized = TRUE;
                   1194:     }
                   1195:     if (bytes & (GC_page_size -1)) ABORT("Bad GET_MEM arg");
                   1196:     result = mmap(last_addr, bytes, PROT_READ | PROT_WRITE | OPT_PROT_EXEC,
                   1197:                  GC_MMAP_FLAGS, fd, 0/* offset */);
                   1198:     if (result == MAP_FAILED) return(0);
                   1199:     last_addr = (ptr_t)result + bytes + GC_page_size - 1;
                   1200:     last_addr = (ptr_t)((word)last_addr & ~(GC_page_size - 1));
1.4     ! noro     1201: #   if !defined(LINUX)
        !          1202:       if (last_addr == 0) {
        !          1203:         /* Oops.  We got the end of the address space.  This isn't     */
        !          1204:        /* usable by arbitrary C code, since one-past-end pointers      */
        !          1205:        /* don't work, so we discard it and try again.                  */
        !          1206:        munmap(result, (size_t)(-GC_page_size) - (size_t)result);
        !          1207:                        /* Leave last page mapped, so we can't repeat. */
        !          1208:        return GC_unix_get_mem(bytes);
        !          1209:       }
        !          1210: #   else
        !          1211:       GC_ASSERT(last_addr != 0);
        !          1212: #   endif
1.1       noro     1213:     return((ptr_t)result);
                   1214: }
                   1215:
                   1216: #else /* Not RS6000, not USE_MMAP */
                   1217: ptr_t GC_unix_get_mem(bytes)
                   1218: word bytes;
                   1219: {
                   1220:   ptr_t result;
                   1221: # ifdef IRIX5
                   1222:     /* Bare sbrk isn't thread safe.  Play by malloc rules.     */
                   1223:     /* The equivalent may be needed on other systems as well.  */
                   1224:     __LOCK_MALLOC();
                   1225: # endif
                   1226:   {
                   1227:     ptr_t cur_brk = (ptr_t)sbrk(0);
                   1228:     SBRK_ARG_T lsbs = (word)cur_brk & (GC_page_size-1);
                   1229:
                   1230:     if ((SBRK_ARG_T)bytes < 0) return(0); /* too big */
                   1231:     if (lsbs != 0) {
                   1232:         if((ptr_t)sbrk(GC_page_size - lsbs) == (ptr_t)(-1)) return(0);
                   1233:     }
                   1234:     result = (ptr_t)sbrk((SBRK_ARG_T)bytes);
                   1235:     if (result == (ptr_t)(-1)) result = 0;
                   1236:   }
                   1237: # ifdef IRIX5
                   1238:     __UNLOCK_MALLOC();
                   1239: # endif
                   1240:   return(result);
                   1241: }
                   1242:
                   1243: #endif /* Not USE_MMAP */
                   1244: #endif /* Not RS6000 */
                   1245:
                   1246: # endif /* UN*X */
                   1247:
                   1248: # ifdef OS2
                   1249:
                   1250: void * os2_alloc(size_t bytes)
                   1251: {
                   1252:     void * result;
                   1253:
                   1254:     if (DosAllocMem(&result, bytes, PAG_EXECUTE | PAG_READ |
                   1255:                                    PAG_WRITE | PAG_COMMIT)
                   1256:                    != NO_ERROR) {
                   1257:        return(0);
                   1258:     }
                   1259:     if (result == 0) return(os2_alloc(bytes));
                   1260:     return(result);
                   1261: }
                   1262:
                   1263: # endif /* OS2 */
                   1264:
                   1265:
1.4     ! noro     1266: # if defined(MSWIN32) || defined(MSWINCE)
        !          1267: SYSTEM_INFO GC_sysinfo;
        !          1268: # endif
        !          1269:
        !          1270:
1.1       noro     1271: # ifdef MSWIN32
                   1272: word GC_n_heap_bases = 0;
                   1273:
                   1274: ptr_t GC_win32_get_mem(bytes)
                   1275: word bytes;
                   1276: {
                   1277:     ptr_t result;
1.4     ! noro     1278:
1.1       noro     1279:     if (GC_win32s) {
                   1280:        /* VirtualAlloc doesn't like PAGE_EXECUTE_READWRITE.    */
                   1281:        /* There are also unconfirmed rumors of other           */
                   1282:        /* problems, so we dodge the issue.                     */
                   1283:         result = (ptr_t) GlobalAlloc(0, bytes + HBLKSIZE);
                   1284:         result = (ptr_t)(((word)result + HBLKSIZE) & ~(HBLKSIZE-1));
                   1285:     } else {
                   1286:         result = (ptr_t) VirtualAlloc(NULL, bytes,
                   1287:                                      MEM_COMMIT | MEM_RESERVE,
                   1288:                                      PAGE_EXECUTE_READWRITE);
                   1289:     }
                   1290:     if (HBLKDISPL(result) != 0) ABORT("Bad VirtualAlloc result");
                   1291:        /* If I read the documentation correctly, this can      */
                   1292:        /* only happen if HBLKSIZE > 64k or not a power of 2.   */
                   1293:     if (GC_n_heap_bases >= MAX_HEAP_SECTS) ABORT("Too many heap sections");
                   1294:     GC_heap_bases[GC_n_heap_bases++] = result;
                   1295:     return(result);
                   1296: }
                   1297:
                   1298: void GC_win32_free_heap ()
                   1299: {
                   1300:     if (GC_win32s) {
                   1301:        while (GC_n_heap_bases > 0) {
                   1302:            GlobalFree (GC_heap_bases[--GC_n_heap_bases]);
                   1303:            GC_heap_bases[GC_n_heap_bases] = 0;
                   1304:        }
                   1305:     }
                   1306: }
1.4     ! noro     1307: # endif
        !          1308:
        !          1309: #ifdef AMIGA
        !          1310: # define GC_AMIGA_AM
        !          1311: # include "AmigaOS.c"
        !          1312: # undef GC_AMIGA_AM
        !          1313: #endif
1.1       noro     1314:
                   1315:
1.4     ! noro     1316: # ifdef MSWINCE
        !          1317: word GC_n_heap_bases = 0;
        !          1318:
        !          1319: ptr_t GC_wince_get_mem(bytes)
        !          1320: word bytes;
        !          1321: {
        !          1322:     ptr_t result;
        !          1323:     word i;
        !          1324:
        !          1325:     /* Round up allocation size to multiple of page size */
        !          1326:     bytes = (bytes + GC_page_size-1) & ~(GC_page_size-1);
        !          1327:
        !          1328:     /* Try to find reserved, uncommitted pages */
        !          1329:     for (i = 0; i < GC_n_heap_bases; i++) {
        !          1330:        if (((word)(-(signed_word)GC_heap_lengths[i])
        !          1331:             & (GC_sysinfo.dwAllocationGranularity-1))
        !          1332:            >= bytes) {
        !          1333:            result = GC_heap_bases[i] + GC_heap_lengths[i];
        !          1334:            break;
        !          1335:        }
        !          1336:     }
        !          1337:
        !          1338:     if (i == GC_n_heap_bases) {
        !          1339:        /* Reserve more pages */
        !          1340:        word res_bytes = (bytes + GC_sysinfo.dwAllocationGranularity-1)
        !          1341:                         & ~(GC_sysinfo.dwAllocationGranularity-1);
        !          1342:        result = (ptr_t) VirtualAlloc(NULL, res_bytes,
        !          1343:                                      MEM_RESERVE | MEM_TOP_DOWN,
        !          1344:                                      PAGE_EXECUTE_READWRITE);
        !          1345:        if (HBLKDISPL(result) != 0) ABORT("Bad VirtualAlloc result");
        !          1346:            /* If I read the documentation correctly, this can  */
        !          1347:            /* only happen if HBLKSIZE > 64k or not a power of 2.       */
        !          1348:        if (GC_n_heap_bases >= MAX_HEAP_SECTS) ABORT("Too many heap sections");
        !          1349:        GC_heap_bases[GC_n_heap_bases] = result;
        !          1350:        GC_heap_lengths[GC_n_heap_bases] = 0;
        !          1351:        GC_n_heap_bases++;
        !          1352:     }
        !          1353:
        !          1354:     /* Commit pages */
        !          1355:     result = (ptr_t) VirtualAlloc(result, bytes,
        !          1356:                                  MEM_COMMIT,
        !          1357:                                  PAGE_EXECUTE_READWRITE);
        !          1358:     if (result != NULL) {
        !          1359:        if (HBLKDISPL(result) != 0) ABORT("Bad VirtualAlloc result");
        !          1360:        GC_heap_lengths[i] += bytes;
        !          1361:     }
        !          1362:
        !          1363:     return(result);
        !          1364: }
1.1       noro     1365: # endif
                   1366:
                   1367: #ifdef USE_MUNMAP
                   1368:
1.4     ! noro     1369: /* For now, this only works on Win32/WinCE and some Unix-like  */
        !          1370: /* systems.  If you have something else, don't define          */
        !          1371: /* USE_MUNMAP.                                                 */
1.1       noro     1372: /* We assume ANSI C to support this feature.                   */
1.4     ! noro     1373:
        !          1374: #if !defined(MSWIN32) && !defined(MSWINCE)
        !          1375:
1.1       noro     1376: #include <unistd.h>
                   1377: #include <sys/mman.h>
                   1378: #include <sys/stat.h>
                   1379: #include <sys/types.h>
1.4     ! noro     1380:
        !          1381: #endif
1.1       noro     1382:
                   1383: /* Compute a page aligned starting address for the unmap       */
                   1384: /* operation on a block of size bytes starting at start.       */
                   1385: /* Return 0 if the block is too small to make this feasible.   */
                   1386: ptr_t GC_unmap_start(ptr_t start, word bytes)
                   1387: {
                   1388:     ptr_t result = start;
                   1389:     /* Round start to next page boundary.       */
                   1390:         result += GC_page_size - 1;
                   1391:         result = (ptr_t)((word)result & ~(GC_page_size - 1));
                   1392:     if (result + GC_page_size > start + bytes) return 0;
                   1393:     return result;
                   1394: }
                   1395:
                   1396: /* Compute end address for an unmap operation on the indicated */
                   1397: /* block.                                                      */
                   1398: ptr_t GC_unmap_end(ptr_t start, word bytes)
                   1399: {
                   1400:     ptr_t end_addr = start + bytes;
                   1401:     end_addr = (ptr_t)((word)end_addr & ~(GC_page_size - 1));
                   1402:     return end_addr;
                   1403: }
                   1404:
1.4     ! noro     1405: /* Under Win32/WinCE we commit (map) and decommit (unmap)      */
        !          1406: /* memory using        VirtualAlloc and VirtualFree.  These functions  */
        !          1407: /* work on individual allocations of virtual memory, made      */
        !          1408: /* previously using VirtualAlloc with the MEM_RESERVE flag.    */
        !          1409: /* The ranges we need to (de)commit may span several of these  */
        !          1410: /* allocations; therefore we use VirtualQuery to check         */
        !          1411: /* allocation lengths, and split up the range as necessary.    */
        !          1412:
1.1       noro     1413: /* We assume that GC_remap is called on exactly the same range */
                   1414: /* as a previous call to GC_unmap.  It is safe to consistently */
                   1415: /* round the endpoints in both places.                         */
                   1416: void GC_unmap(ptr_t start, word bytes)
                   1417: {
                   1418:     ptr_t start_addr = GC_unmap_start(start, bytes);
                   1419:     ptr_t end_addr = GC_unmap_end(start, bytes);
                   1420:     word len = end_addr - start_addr;
                   1421:     if (0 == start_addr) return;
1.4     ! noro     1422: #   if defined(MSWIN32) || defined(MSWINCE)
        !          1423:       while (len != 0) {
        !          1424:           MEMORY_BASIC_INFORMATION mem_info;
        !          1425:          GC_word free_len;
        !          1426:          if (VirtualQuery(start_addr, &mem_info, sizeof(mem_info))
        !          1427:              != sizeof(mem_info))
        !          1428:              ABORT("Weird VirtualQuery result");
        !          1429:          free_len = (len < mem_info.RegionSize) ? len : mem_info.RegionSize;
        !          1430:          if (!VirtualFree(start_addr, free_len, MEM_DECOMMIT))
        !          1431:              ABORT("VirtualFree failed");
        !          1432:          GC_unmapped_bytes += free_len;
        !          1433:          start_addr += free_len;
        !          1434:          len -= free_len;
        !          1435:       }
        !          1436: #   else
        !          1437:       if (munmap(start_addr, len) != 0) ABORT("munmap failed");
        !          1438:       GC_unmapped_bytes += len;
        !          1439: #   endif
1.1       noro     1440: }
                   1441:
                   1442:
                   1443: void GC_remap(ptr_t start, word bytes)
                   1444: {
                   1445:     static int zero_descr = -1;
                   1446:     ptr_t start_addr = GC_unmap_start(start, bytes);
                   1447:     ptr_t end_addr = GC_unmap_end(start, bytes);
                   1448:     word len = end_addr - start_addr;
                   1449:     ptr_t result;
                   1450:
1.4     ! noro     1451: #   if defined(MSWIN32) || defined(MSWINCE)
        !          1452:       if (0 == start_addr) return;
        !          1453:       while (len != 0) {
        !          1454:           MEMORY_BASIC_INFORMATION mem_info;
        !          1455:          GC_word alloc_len;
        !          1456:          if (VirtualQuery(start_addr, &mem_info, sizeof(mem_info))
        !          1457:              != sizeof(mem_info))
        !          1458:              ABORT("Weird VirtualQuery result");
        !          1459:          alloc_len = (len < mem_info.RegionSize) ? len : mem_info.RegionSize;
        !          1460:          result = VirtualAlloc(start_addr, alloc_len,
        !          1461:                                MEM_COMMIT,
        !          1462:                                PAGE_EXECUTE_READWRITE);
        !          1463:          if (result != start_addr) {
        !          1464:              ABORT("VirtualAlloc remapping failed");
        !          1465:          }
        !          1466:          GC_unmapped_bytes -= alloc_len;
        !          1467:          start_addr += alloc_len;
        !          1468:          len -= alloc_len;
        !          1469:       }
        !          1470: #   else
        !          1471:       if (-1 == zero_descr) zero_descr = open("/dev/zero", O_RDWR);
        !          1472:       if (0 == start_addr) return;
        !          1473:       result = mmap(start_addr, len, PROT_READ | PROT_WRITE | OPT_PROT_EXEC,
        !          1474:                    MAP_FIXED | MAP_PRIVATE, zero_descr, 0);
        !          1475:       if (result != start_addr) {
        !          1476:          ABORT("mmap remapping failed");
        !          1477:       }
        !          1478:       GC_unmapped_bytes -= len;
        !          1479: #   endif
1.1       noro     1480: }
                   1481:
                   1482: /* Two adjacent blocks have already been unmapped and are about to     */
                   1483: /* be merged.  Unmap the whole block.  This typically requires         */
                   1484: /* that we unmap a small section in the middle that was not previously */
                   1485: /* unmapped due to alignment constraints.                              */
                   1486: void GC_unmap_gap(ptr_t start1, word bytes1, ptr_t start2, word bytes2)
                   1487: {
                   1488:     ptr_t start1_addr = GC_unmap_start(start1, bytes1);
                   1489:     ptr_t end1_addr = GC_unmap_end(start1, bytes1);
                   1490:     ptr_t start2_addr = GC_unmap_start(start2, bytes2);
                   1491:     ptr_t end2_addr = GC_unmap_end(start2, bytes2);
                   1492:     ptr_t start_addr = end1_addr;
                   1493:     ptr_t end_addr = start2_addr;
                   1494:     word len;
                   1495:     GC_ASSERT(start1 + bytes1 == start2);
                   1496:     if (0 == start1_addr) start_addr = GC_unmap_start(start1, bytes1 + bytes2);
                   1497:     if (0 == start2_addr) end_addr = GC_unmap_end(start1, bytes1 + bytes2);
                   1498:     if (0 == start_addr) return;
                   1499:     len = end_addr - start_addr;
1.4     ! noro     1500: #   if defined(MSWIN32) || defined(MSWINCE)
        !          1501:       while (len != 0) {
        !          1502:           MEMORY_BASIC_INFORMATION mem_info;
        !          1503:          GC_word free_len;
        !          1504:          if (VirtualQuery(start_addr, &mem_info, sizeof(mem_info))
        !          1505:              != sizeof(mem_info))
        !          1506:              ABORT("Weird VirtualQuery result");
        !          1507:          free_len = (len < mem_info.RegionSize) ? len : mem_info.RegionSize;
        !          1508:          if (!VirtualFree(start_addr, free_len, MEM_DECOMMIT))
        !          1509:              ABORT("VirtualFree failed");
        !          1510:          GC_unmapped_bytes += free_len;
        !          1511:          start_addr += free_len;
        !          1512:          len -= free_len;
        !          1513:       }
        !          1514: #   else
        !          1515:       if (len != 0 && munmap(start_addr, len) != 0) ABORT("munmap failed");
        !          1516:       GC_unmapped_bytes += len;
        !          1517: #   endif
1.1       noro     1518: }
                   1519:
                   1520: #endif /* USE_MUNMAP */
                   1521:
                   1522: /* Routine for pushing any additional roots.  In THREADS       */
                   1523: /* environment, this is also responsible for marking from      */
1.4     ! noro     1524: /* thread stacks.                                              */
1.1       noro     1525: #ifndef THREADS
                   1526: void (*GC_push_other_roots)() = 0;
                   1527: #else /* THREADS */
                   1528:
                   1529: # ifdef PCR
                   1530: PCR_ERes GC_push_thread_stack(PCR_Th_T *t, PCR_Any dummy)
                   1531: {
                   1532:     struct PCR_ThCtl_TInfoRep info;
                   1533:     PCR_ERes result;
                   1534:
                   1535:     info.ti_stkLow = info.ti_stkHi = 0;
                   1536:     result = PCR_ThCtl_GetInfo(t, &info);
                   1537:     GC_push_all_stack((ptr_t)(info.ti_stkLow), (ptr_t)(info.ti_stkHi));
                   1538:     return(result);
                   1539: }
                   1540:
                   1541: /* Push the contents of an old object. We treat this as stack  */
                   1542: /* data only becasue that makes it robust against mark stack   */
                   1543: /* overflow.                                                   */
                   1544: PCR_ERes GC_push_old_obj(void *p, size_t size, PCR_Any data)
                   1545: {
                   1546:     GC_push_all_stack((ptr_t)p, (ptr_t)p + size);
                   1547:     return(PCR_ERes_okay);
                   1548: }
                   1549:
                   1550:
1.4     ! noro     1551: void GC_default_push_other_roots GC_PROTO((void))
1.1       noro     1552: {
                   1553:     /* Traverse data allocated by previous memory managers.            */
                   1554:        {
                   1555:          extern struct PCR_MM_ProcsRep * GC_old_allocator;
                   1556:
                   1557:          if ((*(GC_old_allocator->mmp_enumerate))(PCR_Bool_false,
                   1558:                                                   GC_push_old_obj, 0)
                   1559:              != PCR_ERes_okay) {
                   1560:              ABORT("Old object enumeration failed");
                   1561:          }
                   1562:        }
                   1563:     /* Traverse all thread stacks. */
                   1564:        if (PCR_ERes_IsErr(
                   1565:                 PCR_ThCtl_ApplyToAllOtherThreads(GC_push_thread_stack,0))
                   1566:               || PCR_ERes_IsErr(GC_push_thread_stack(PCR_Th_CurrThread(), 0))) {
                   1567:               ABORT("Thread stack marking failed\n");
                   1568:        }
                   1569: }
                   1570:
                   1571: # endif /* PCR */
                   1572:
                   1573: # ifdef SRC_M3
                   1574:
                   1575: # ifdef ALL_INTERIOR_POINTERS
                   1576:     --> misconfigured
                   1577: # endif
                   1578:
1.4     ! noro     1579: void GC_push_thread_structures GC_PROTO((void))
        !          1580: {
        !          1581:     /* Not our responsibibility. */
        !          1582: }
1.1       noro     1583:
                   1584: extern void ThreadF__ProcessStacks();
                   1585:
                   1586: void GC_push_thread_stack(start, stop)
                   1587: word start, stop;
                   1588: {
                   1589:    GC_push_all_stack((ptr_t)start, (ptr_t)stop + sizeof(word));
                   1590: }
                   1591:
                   1592: /* Push routine with M3 specific calling convention. */
                   1593: GC_m3_push_root(dummy1, p, dummy2, dummy3)
                   1594: word *p;
                   1595: ptr_t dummy1, dummy2;
                   1596: int dummy3;
                   1597: {
                   1598:     word q = *p;
                   1599:
1.4     ! noro     1600:     GC_PUSH_ONE_STACK(q, p);
1.1       noro     1601: }
                   1602:
                   1603: /* M3 set equivalent to RTHeap.TracedRefTypes */
                   1604: typedef struct { int elts[1]; }  RefTypeSet;
                   1605: RefTypeSet GC_TracedRefTypes = {{0x1}};
                   1606:
1.4     ! noro     1607: void GC_default_push_other_roots GC_PROTO((void))
1.1       noro     1608: {
1.4     ! noro     1609:     /* Use the M3 provided routine for finding static roots.    */
        !          1610:     /* This is a bit dubious, since it presumes no C roots.     */
        !          1611:     /* We handle the collector roots explicitly in GC_push_roots */
        !          1612:        RTMain__GlobalMapProc(GC_m3_push_root, 0, GC_TracedRefTypes);
1.1       noro     1613:        if (GC_words_allocd > 0) {
                   1614:            ThreadF__ProcessStacks(GC_push_thread_stack);
                   1615:        }
                   1616:        /* Otherwise this isn't absolutely necessary, and we have       */
                   1617:        /* startup ordering problems.                                   */
                   1618: }
                   1619:
                   1620: # endif /* SRC_M3 */
                   1621:
                   1622: # if defined(SOLARIS_THREADS) || defined(WIN32_THREADS) \
                   1623:      || defined(IRIX_THREADS) || defined(LINUX_THREADS) \
1.4     ! noro     1624:      || defined(HPUX_THREADS)
1.1       noro     1625:
                   1626: extern void GC_push_all_stacks();
                   1627:
1.4     ! noro     1628: void GC_default_push_other_roots GC_PROTO((void))
1.1       noro     1629: {
                   1630:     GC_push_all_stacks();
                   1631: }
                   1632:
                   1633: # endif /* SOLARIS_THREADS || ... */
                   1634:
1.4     ! noro     1635: void (*GC_push_other_roots) GC_PROTO((void)) = GC_default_push_other_roots;
1.1       noro     1636:
                   1637: #endif
                   1638:
                   1639: /*
                   1640:  * Routines for accessing dirty  bits on virtual pages.
1.4     ! noro     1641:  * We plan to eventually implement four strategies for doing so:
1.1       noro     1642:  * DEFAULT_VDB:        A simple dummy implementation that treats every page
                   1643:  *             as possibly dirty.  This makes incremental collection
                   1644:  *             useless, but the implementation is still correct.
                   1645:  * PCR_VDB:    Use PPCRs virtual dirty bit facility.
                   1646:  * PROC_VDB:   Use the /proc facility for reading dirty bits.  Only
                   1647:  *             works under some SVR4 variants.  Even then, it may be
                   1648:  *             too slow to be entirely satisfactory.  Requires reading
                   1649:  *             dirty bits for entire address space.  Implementations tend
                   1650:  *             to assume that the client is a (slow) debugger.
                   1651:  * MPROTECT_VDB:Protect pages and then catch the faults to keep track of
                   1652:  *             dirtied pages.  The implementation (and implementability)
                   1653:  *             is highly system dependent.  This usually fails when system
                   1654:  *             calls write to a protected page.  We prevent the read system
                   1655:  *             call from doing so.  It is the clients responsibility to
                   1656:  *             make sure that other system calls are similarly protected
                   1657:  *             or write only to the stack.
                   1658:  */
                   1659:
                   1660: GC_bool GC_dirty_maintained = FALSE;
                   1661:
                   1662: # ifdef DEFAULT_VDB
                   1663:
                   1664: /* All of the following assume the allocation lock is held, and        */
                   1665: /* signals are disabled.                                       */
                   1666:
                   1667: /* The client asserts that unallocated pages in the heap are never     */
                   1668: /* written.                                                            */
                   1669:
                   1670: /* Initialize virtual dirty bit implementation.                        */
                   1671: void GC_dirty_init()
                   1672: {
                   1673:     GC_dirty_maintained = TRUE;
                   1674: }
                   1675:
                   1676: /* Retrieve system dirty bits for heap to a local buffer.      */
                   1677: /* Restore the systems notion of which pages are dirty.                */
                   1678: void GC_read_dirty()
                   1679: {}
                   1680:
                   1681: /* Is the HBLKSIZE sized page at h marked dirty in the local buffer?   */
                   1682: /* If the actual page size is different, this returns TRUE if any      */
                   1683: /* of the pages overlapping h are dirty.  This routine may err on the  */
                   1684: /* side of labelling pages as dirty (and this implementation does).    */
                   1685: /*ARGSUSED*/
                   1686: GC_bool GC_page_was_dirty(h)
                   1687: struct hblk *h;
                   1688: {
                   1689:     return(TRUE);
                   1690: }
                   1691:
                   1692: /*
                   1693:  * The following two routines are typically less crucial.  They matter
                   1694:  * most with large dynamic libraries, or if we can't accurately identify
                   1695:  * stacks, e.g. under Solaris 2.X.  Otherwise the following default
                   1696:  * versions are adequate.
                   1697:  */
                   1698:
                   1699: /* Could any valid GC heap pointer ever have been written to this page?        */
                   1700: /*ARGSUSED*/
                   1701: GC_bool GC_page_was_ever_dirty(h)
                   1702: struct hblk *h;
                   1703: {
                   1704:     return(TRUE);
                   1705: }
                   1706:
                   1707: /* Reset the n pages starting at h to "was never dirty" status.        */
                   1708: void GC_is_fresh(h, n)
                   1709: struct hblk *h;
                   1710: word n;
                   1711: {
                   1712: }
                   1713:
                   1714: /* A call hints that h is about to be written. */
                   1715: /* May speed up some dirty bit implementations.        */
                   1716: /*ARGSUSED*/
                   1717: void GC_write_hint(h)
                   1718: struct hblk *h;
                   1719: {
                   1720: }
                   1721:
                   1722: # endif /* DEFAULT_VDB */
                   1723:
                   1724:
                   1725: # ifdef MPROTECT_VDB
                   1726:
                   1727: /*
                   1728:  * See DEFAULT_VDB for interface descriptions.
                   1729:  */
                   1730:
                   1731: /*
                   1732:  * This implementation maintains dirty bits itself by catching write
                   1733:  * faults and keeping track of them.  We assume nobody else catches
                   1734:  * SIGBUS or SIGSEGV.  We assume no write faults occur in system calls
                   1735:  * except as a result of a read system call.  This means clients must
                   1736:  * either ensure that system calls do not touch the heap, or must
                   1737:  * provide their own wrappers analogous to the one for read.
                   1738:  * We assume the page size is a multiple of HBLKSIZE.
                   1739:  * This implementation is currently SunOS 4.X and IRIX 5.X specific, though we
                   1740:  * tried to use portable code where easily possible.  It is known
                   1741:  * not to work under a number of other systems.
                   1742:  */
                   1743:
1.4     ! noro     1744: # if !defined(MSWIN32) && !defined(MSWINCE)
1.1       noro     1745:
                   1746: #   include <sys/mman.h>
                   1747: #   include <signal.h>
                   1748: #   include <sys/syscall.h>
                   1749:
                   1750: #   define PROTECT(addr, len) \
1.2       noro     1751:          if (mprotect((caddr_t)(addr), (size_t)(len), \
1.1       noro     1752:                       PROT_READ | OPT_PROT_EXEC) < 0) { \
                   1753:            ABORT("mprotect failed"); \
                   1754:          }
                   1755: #   define UNPROTECT(addr, len) \
1.2       noro     1756:          if (mprotect((caddr_t)(addr), (size_t)(len), \
1.1       noro     1757:                       PROT_WRITE | PROT_READ | OPT_PROT_EXEC ) < 0) { \
                   1758:            ABORT("un-mprotect failed"); \
                   1759:          }
                   1760:
                   1761: # else
                   1762:
1.4     ! noro     1763: #   ifndef MSWINCE
        !          1764: #     include <signal.h>
        !          1765: #   endif
1.1       noro     1766:
                   1767:     static DWORD protect_junk;
                   1768: #   define PROTECT(addr, len) \
                   1769:          if (!VirtualProtect((addr), (len), PAGE_EXECUTE_READ, \
                   1770:                              &protect_junk)) { \
                   1771:            DWORD last_error = GetLastError(); \
                   1772:            GC_printf1("Last error code: %lx\n", last_error); \
                   1773:            ABORT("VirtualProtect failed"); \
                   1774:          }
                   1775: #   define UNPROTECT(addr, len) \
                   1776:          if (!VirtualProtect((addr), (len), PAGE_EXECUTE_READWRITE, \
                   1777:                              &protect_junk)) { \
                   1778:            ABORT("un-VirtualProtect failed"); \
                   1779:          }
                   1780:
                   1781: # endif
                   1782:
                   1783: #if defined(SUNOS4) || defined(FREEBSD)
                   1784:     typedef void (* SIG_PF)();
                   1785: #endif
1.4     ! noro     1786: #if defined(SUNOS5SIGS) || defined(OSF1) || defined(LINUX) || defined(MACOSX)
1.2       noro     1787: # ifdef __STDC__
1.1       noro     1788:     typedef void (* SIG_PF)(int);
1.2       noro     1789: # else
                   1790:     typedef void (* SIG_PF)();
                   1791: # endif
1.1       noro     1792: #endif
                   1793: #if defined(MSWIN32)
                   1794:     typedef LPTOP_LEVEL_EXCEPTION_FILTER SIG_PF;
                   1795: #   undef SIG_DFL
                   1796: #   define SIG_DFL (LPTOP_LEVEL_EXCEPTION_FILTER) (-1)
                   1797: #endif
1.4     ! noro     1798: #if defined(MSWINCE)
        !          1799:     typedef LONG (WINAPI *SIG_PF)(struct _EXCEPTION_POINTERS *);
        !          1800: #   undef SIG_DFL
        !          1801: #   define SIG_DFL (SIG_PF) (-1)
        !          1802: #endif
1.1       noro     1803:
                   1804: #if defined(IRIX5) || defined(OSF1)
                   1805:     typedef void (* REAL_SIG_PF)(int, int, struct sigcontext *);
                   1806: #endif
                   1807: #if defined(SUNOS5SIGS)
1.2       noro     1808: # ifdef HPUX
                   1809: #   define SIGINFO __siginfo
                   1810: # else
                   1811: #   define SIGINFO siginfo
                   1812: # endif
                   1813: # ifdef __STDC__
                   1814:     typedef void (* REAL_SIG_PF)(int, struct SIGINFO *, void *);
                   1815: # else
                   1816:     typedef void (* REAL_SIG_PF)();
                   1817: # endif
1.1       noro     1818: #endif
                   1819: #if defined(LINUX)
                   1820: #   include <linux/version.h>
1.2       noro     1821: #   if (LINUX_VERSION_CODE >= 0x20100) && !defined(M68K) || defined(ALPHA) || defined(IA64)
1.1       noro     1822:       typedef struct sigcontext s_c;
                   1823: #   else
                   1824:       typedef struct sigcontext_struct s_c;
                   1825: #   endif
1.2       noro     1826: #   if defined(ALPHA) || defined(M68K)
                   1827:       typedef void (* REAL_SIG_PF)(int, int, s_c *);
                   1828: #   else
1.4     ! noro     1829: #     if defined(IA64) || defined(HP_PA)
1.2       noro     1830:         typedef void (* REAL_SIG_PF)(int, siginfo_t *, s_c *);
                   1831: #     else
                   1832:         typedef void (* REAL_SIG_PF)(int, s_c);
                   1833: #     endif
                   1834: #   endif
1.1       noro     1835: #   ifdef ALPHA
                   1836:     /* Retrieve fault address from sigcontext structure by decoding    */
                   1837:     /* instruction.                                                    */
                   1838:     char * get_fault_addr(s_c *sc) {
                   1839:         unsigned instr;
                   1840:        word faultaddr;
                   1841:
                   1842:        instr = *((unsigned *)(sc->sc_pc));
                   1843:        faultaddr = sc->sc_regs[(instr >> 16) & 0x1f];
                   1844:        faultaddr += (word) (((int)instr << 16) >> 16);
                   1845:        return (char *)faultaddr;
                   1846:     }
                   1847: #   endif /* !ALPHA */
                   1848: # endif
                   1849:
1.4     ! noro     1850: # if defined(MACOSX) /* Should also test for PowerPC? */
        !          1851:     typedef void (* REAL_SIG_PF)(int, int, struct sigcontext *);
        !          1852:
        !          1853: /* Decodes the machine instruction which was responsible for the sending of the
        !          1854:    SIGBUS signal. Sadly this is the only way to find the faulting address because
        !          1855:    the signal handler doesn't get it directly from the kernel (although it is
        !          1856:    available on the Mach level, but droppped by the BSD personality before it
        !          1857:    calls our signal handler...)
        !          1858:    This code should be able to deal correctly with all PPCs starting from the
        !          1859:    601 up to and including the G4s (including Velocity Engine). */
        !          1860: #define EXTRACT_OP1(iw)     (((iw) & 0xFC000000) >> 26)
        !          1861: #define EXTRACT_OP2(iw)     (((iw) & 0x000007FE) >> 1)
        !          1862: #define EXTRACT_REGA(iw)    (((iw) & 0x001F0000) >> 16)
        !          1863: #define EXTRACT_REGB(iw)    (((iw) & 0x03E00000) >> 21)
        !          1864: #define EXTRACT_REGC(iw)    (((iw) & 0x0000F800) >> 11)
        !          1865: #define EXTRACT_DISP(iw)    ((short *) &(iw))[1]
        !          1866:
        !          1867: static char *get_fault_addr(struct sigcontext *scp)
        !          1868: {
        !          1869:    unsigned int   instr = *((unsigned int *) scp->sc_ir);
        !          1870:    unsigned int * regs = &((unsigned int *) scp->sc_regs)[2];
        !          1871:    int            disp = 0, tmp;
        !          1872:    unsigned int   baseA = 0, baseB = 0;
        !          1873:    unsigned int   addr, alignmask = 0xFFFFFFFF;
        !          1874:
        !          1875: #ifdef GC_DEBUG_DECODER
        !          1876:    GC_err_printf1("Instruction: 0x%lx\n", instr);
        !          1877:    GC_err_printf1("Opcode 1: d\n", (int)EXTRACT_OP1(instr));
        !          1878: #endif
        !          1879:    switch(EXTRACT_OP1(instr)) {
        !          1880:       case 38:   /* stb */
        !          1881:       case 39:   /* stbu */
        !          1882:       case 54:   /* stfd */
        !          1883:       case 55:   /* stfdu */
        !          1884:       case 52:   /* stfs */
        !          1885:       case 53:   /* stfsu */
        !          1886:       case 44:   /* sth */
        !          1887:       case 45:   /* sthu */
        !          1888:       case 47:   /* stmw */
        !          1889:       case 36:   /* stw */
        !          1890:       case 37:   /* stwu */
        !          1891:             tmp = EXTRACT_REGA(instr);
        !          1892:             if(tmp > 0)
        !          1893:                baseA = regs[tmp];
        !          1894:             disp = EXTRACT_DISP(instr);
        !          1895:             break;
        !          1896:       case 31:
        !          1897: #ifdef GC_DEBUG_DECODER
        !          1898:             GC_err_printf1("Opcode 2: %d\n", (int)EXTRACT_OP2(instr));
        !          1899: #endif
        !          1900:             switch(EXTRACT_OP2(instr)) {
        !          1901:                case 86:    /* dcbf */
        !          1902:                case 54:    /* dcbst */
        !          1903:                case 1014:  /* dcbz */
        !          1904:                case 247:   /* stbux */
        !          1905:                case 215:   /* stbx */
        !          1906:                case 759:   /* stfdux */
        !          1907:                case 727:   /* stfdx */
        !          1908:                case 983:   /* stfiwx */
        !          1909:                case 695:   /* stfsux */
        !          1910:                case 663:   /* stfsx */
        !          1911:                case 918:   /* sthbrx */
        !          1912:                case 439:   /* sthux */
        !          1913:                case 407:   /* sthx */
        !          1914:                case 661:   /* stswx */
        !          1915:                case 662:   /* stwbrx */
        !          1916:                case 150:   /* stwcx. */
        !          1917:                case 183:   /* stwux */
        !          1918:                case 151:   /* stwx */
        !          1919:                case 135:   /* stvebx */
        !          1920:                case 167:   /* stvehx */
        !          1921:                case 199:   /* stvewx */
        !          1922:                case 231:   /* stvx */
        !          1923:                case 487:   /* stvxl */
        !          1924:                      tmp = EXTRACT_REGA(instr);
        !          1925:                      if(tmp > 0)
        !          1926:                         baseA = regs[tmp];
        !          1927:                         baseB = regs[EXTRACT_REGC(instr)];
        !          1928:                         /* determine Altivec alignment mask */
        !          1929:                         switch(EXTRACT_OP2(instr)) {
        !          1930:                            case 167:   /* stvehx */
        !          1931:                                  alignmask = 0xFFFFFFFE;
        !          1932:                                  break;
        !          1933:                            case 199:   /* stvewx */
        !          1934:                                  alignmask = 0xFFFFFFFC;
        !          1935:                                  break;
        !          1936:                            case 231:   /* stvx */
        !          1937:                                  alignmask = 0xFFFFFFF0;
        !          1938:                                  break;
        !          1939:                            case 487:  /* stvxl */
        !          1940:                                  alignmask = 0xFFFFFFF0;
        !          1941:                                  break;
        !          1942:                         }
        !          1943:                         break;
        !          1944:                case 725:   /* stswi */
        !          1945:                      tmp = EXTRACT_REGA(instr);
        !          1946:                      if(tmp > 0)
        !          1947:                         baseA = regs[tmp];
        !          1948:                         break;
        !          1949:                default:   /* ignore instruction */
        !          1950: #ifdef GC_DEBUG_DECODER
        !          1951:                      GC_err_printf("Ignored by inner handler\n");
        !          1952: #endif
        !          1953:                      return NULL;
        !          1954:                     break;
        !          1955:             }
        !          1956:             break;
        !          1957:       default:   /* ignore instruction */
        !          1958: #ifdef GC_DEBUG_DECODER
        !          1959:             GC_err_printf("Ignored by main handler\n");
        !          1960: #endif
        !          1961:             return NULL;
        !          1962:             break;
        !          1963:    }
        !          1964:
        !          1965:    addr = (baseA + baseB) + disp;
        !          1966:   addr &= alignmask;
        !          1967: #ifdef GC_DEBUG_DECODER
        !          1968:    GC_err_printf1("BaseA: %d\n", baseA);
        !          1969:    GC_err_printf1("BaseB: %d\n", baseB);
        !          1970:    GC_err_printf1("Disp:  %d\n", disp);
        !          1971:    GC_err_printf1("Address: %d\n", addr);
        !          1972: #endif
        !          1973:    return (char *)addr;
        !          1974: }
        !          1975: #endif /* MACOSX */
        !          1976:
1.1       noro     1977: SIG_PF GC_old_bus_handler;
                   1978: SIG_PF GC_old_segv_handler;    /* Also old MSWIN32 ACCESS_VIOLATION filter */
                   1979:
1.4     ! noro     1980: #ifdef THREADS
        !          1981: /* We need to lock around the bitmap update in the write fault handler */
        !          1982: /* in order to avoid the risk of losing a bit.  We do this with a      */
        !          1983: /* test-and-set spin lock if we know how to do that.  Otherwise we     */
        !          1984: /* check whether we are already in the handler and use the dumb but    */
        !          1985: /* safe fallback algorithm of setting all bits in the word.            */
        !          1986: /* Contention should be very rare, so we do the minimum to handle it   */
        !          1987: /* correctly.                                                          */
        !          1988: #ifdef GC_TEST_AND_SET_DEFINED
        !          1989:   static VOLATILE unsigned int fault_handler_lock = 0;
        !          1990:   void async_set_pht_entry_from_index(VOLATILE page_hash_table db, int index) {
        !          1991:     while (GC_test_and_set(&fault_handler_lock));
        !          1992:     /* Could also revert to set_pht_entry_from_index_safe if initial   */
        !          1993:     /* GC_test_and_set fails.                                          */
        !          1994:     set_pht_entry_from_index(db, index);
        !          1995:     GC_clear(&fault_handler_lock);
        !          1996:   }
        !          1997: #else /* !GC_TEST_AND_SET_DEFINED */
        !          1998:   /* THIS IS INCORRECT! The dirty bit vector may be temporarily wrong, */
        !          1999:   /* just before we notice the conflict and correct it. We may end up   */
        !          2000:   /* looking at it while it's wrong.  But this requires contention     */
        !          2001:   /* exactly when a GC is triggered, which seems far less likely to    */
        !          2002:   /* fail than the old code, which had no reported failures.  Thus we  */
        !          2003:   /* leave it this way while we think of something better, or support  */
        !          2004:   /* GC_test_and_set on the remaining platforms.                       */
        !          2005:   static VOLATILE word currently_updating = 0;
        !          2006:   void async_set_pht_entry_from_index(VOLATILE page_hash_table db, int index) {
        !          2007:     unsigned int update_dummy;
        !          2008:     currently_updating = (word)(&update_dummy);
        !          2009:     set_pht_entry_from_index(db, index);
        !          2010:     /* If we get contention in the 10 or so instruction window here,   */
        !          2011:     /* and we get stopped by a GC between the two updates, we lose!    */
        !          2012:     if (currently_updating != (word)(&update_dummy)) {
        !          2013:        set_pht_entry_from_index_safe(db, index);
        !          2014:        /* We claim that if two threads concurrently try to update the  */
        !          2015:        /* dirty bit vector, the first one to execute UPDATE_START      */
        !          2016:        /* will see it changed when UPDATE_END is executed.  (Note that */
        !          2017:        /* &update_dummy must differ in two distinct threads.)  It      */
        !          2018:        /* will then execute set_pht_entry_from_index_safe, thus        */
        !          2019:        /* returning us to a safe state, though not soon enough.        */
        !          2020:     }
        !          2021:   }
        !          2022: #endif /* !GC_TEST_AND_SET_DEFINED */
        !          2023: #else /* !THREADS */
        !          2024: # define async_set_pht_entry_from_index(db, index) \
        !          2025:        set_pht_entry_from_index(db, index)
        !          2026: #endif /* !THREADS */
        !          2027:
1.1       noro     2028: /*ARGSUSED*/
                   2029: # if defined (SUNOS4) || defined(FREEBSD)
                   2030:     void GC_write_fault_handler(sig, code, scp, addr)
                   2031:     int sig, code;
                   2032:     struct sigcontext *scp;
                   2033:     char * addr;
                   2034: #   ifdef SUNOS4
                   2035: #     define SIG_OK (sig == SIGSEGV || sig == SIGBUS)
                   2036: #     define CODE_OK (FC_CODE(code) == FC_PROT \
                   2037:                    || (FC_CODE(code) == FC_OBJERR \
                   2038:                       && FC_ERRNO(code) == FC_PROT))
                   2039: #   endif
                   2040: #   ifdef FREEBSD
                   2041: #     define SIG_OK (sig == SIGBUS)
                   2042: #     define CODE_OK (code == BUS_PAGE_FAULT)
                   2043: #   endif
                   2044: # endif
                   2045: # if defined(IRIX5) || defined(OSF1)
                   2046: #   include <errno.h>
                   2047:     void GC_write_fault_handler(int sig, int code, struct sigcontext *scp)
                   2048: #   define SIG_OK (sig == SIGSEGV)
                   2049: #   ifdef OSF1
                   2050: #     define CODE_OK (code == 2 /* experimentally determined */)
                   2051: #   endif
                   2052: #   ifdef IRIX5
                   2053: #     define CODE_OK (code == EACCES)
                   2054: #   endif
                   2055: # endif
                   2056: # if defined(LINUX)
1.2       noro     2057: #   if defined(ALPHA) || defined(M68K)
1.1       noro     2058:       void GC_write_fault_handler(int sig, int code, s_c * sc)
                   2059: #   else
1.4     ! noro     2060: #     if defined(IA64) || defined(HP_PA)
1.2       noro     2061:         void GC_write_fault_handler(int sig, siginfo_t * si, s_c * scp)
                   2062: #     else
                   2063:         void GC_write_fault_handler(int sig, s_c sc)
                   2064: #     endif
1.1       noro     2065: #   endif
                   2066: #   define SIG_OK (sig == SIGSEGV)
                   2067: #   define CODE_OK TRUE
1.2       noro     2068:        /* Empirically c.trapno == 14, on IA32, but is that useful?     */
                   2069:        /* Should probably consider alignment issues on other           */
                   2070:        /* architectures.                                               */
1.1       noro     2071: # endif
                   2072: # if defined(SUNOS5SIGS)
1.2       noro     2073: #  ifdef __STDC__
                   2074:     void GC_write_fault_handler(int sig, struct SIGINFO *scp, void * context)
                   2075: #  else
                   2076:     void GC_write_fault_handler(sig, scp, context)
                   2077:     int sig;
                   2078:     struct SIGINFO *scp;
                   2079:     void * context;
                   2080: #  endif
                   2081: #   ifdef HPUX
                   2082: #     define SIG_OK (sig == SIGSEGV || sig == SIGBUS)
                   2083: #     define CODE_OK (scp -> si_code == SEGV_ACCERR) \
                   2084:                     || (scp -> si_code == BUS_ADRERR) \
                   2085:                     || (scp -> si_code == BUS_UNKNOWN) \
                   2086:                     || (scp -> si_code == SEGV_UNKNOWN) \
                   2087:                     || (scp -> si_code == BUS_OBJERR)
                   2088: #   else
                   2089: #     define SIG_OK (sig == SIGSEGV)
                   2090: #     define CODE_OK (scp -> si_code == SEGV_ACCERR)
                   2091: #   endif
1.1       noro     2092: # endif
1.4     ! noro     2093:
        !          2094: # if defined(MACOSX)
        !          2095:     void GC_write_fault_handler(int sig, int code, struct sigcontext *scp)
        !          2096: #   define SIG_OK (sig == SIGBUS)
        !          2097: #   define CODE_OK (code == 0 /* experimentally determined */)
        !          2098: # endif
        !          2099:
        !          2100: # if defined(MSWIN32) || defined(MSWINCE)
1.1       noro     2101:     LONG WINAPI GC_write_fault_handler(struct _EXCEPTION_POINTERS *exc_info)
                   2102: #   define SIG_OK (exc_info -> ExceptionRecord -> ExceptionCode == \
1.4     ! noro     2103:                        STATUS_ACCESS_VIOLATION)
1.1       noro     2104: #   define CODE_OK (exc_info -> ExceptionRecord -> ExceptionInformation[0] == 1)
                   2105:                        /* Write fault */
                   2106: # endif
                   2107: {
                   2108:     register unsigned i;
                   2109: #   ifdef IRIX5
                   2110:        char * addr = (char *) (size_t) (scp -> sc_badvaddr);
                   2111: #   endif
                   2112: #   if defined(OSF1) && defined(ALPHA)
                   2113:        char * addr = (char *) (scp -> sc_traparg_a0);
                   2114: #   endif
                   2115: #   ifdef SUNOS5SIGS
                   2116:        char * addr = (char *) (scp -> si_addr);
                   2117: #   endif
                   2118: #   ifdef LINUX
                   2119: #     ifdef I386
                   2120:        char * addr = (char *) (sc.cr2);
                   2121: #     else
                   2122: #      if defined(M68K)
                   2123:           char * addr = NULL;
                   2124:
1.4     ! noro     2125:          struct sigcontext *scp = (struct sigcontext *)(sc);
1.1       noro     2126:
                   2127:          int format = (scp->sc_formatvec >> 12) & 0xf;
                   2128:          unsigned long *framedata = (unsigned long *)(scp + 1);
                   2129:          unsigned long ea;
                   2130:
                   2131:          if (format == 0xa || format == 0xb) {
                   2132:                /* 68020/030 */
                   2133:                ea = framedata[2];
                   2134:          } else if (format == 7) {
                   2135:                /* 68040 */
                   2136:                ea = framedata[3];
1.4     ! noro     2137:                if (framedata[1] & 0x08000000) {
        !          2138:                        /* correct addr on misaligned access */
        !          2139:                        ea = (ea+4095)&(~4095);
        !          2140:                }
1.1       noro     2141:          } else if (format == 4) {
                   2142:                /* 68060 */
                   2143:                ea = framedata[0];
                   2144:                if (framedata[1] & 0x08000000) {
                   2145:                        /* correct addr on misaligned access */
                   2146:                        ea = (ea+4095)&(~4095);
                   2147:                }
                   2148:          }
                   2149:          addr = (char *)ea;
                   2150: #      else
                   2151: #        ifdef ALPHA
                   2152:             char * addr = get_fault_addr(sc);
                   2153: #        else
1.4     ! noro     2154: #          if defined(IA64) || defined(HP_PA)
1.2       noro     2155:              char * addr = si -> si_addr;
1.3       noro     2156:              /* I believe this is claimed to work on all platforms for */
                   2157:              /* Linux 2.3.47 and later.  Hopefully we don't have to    */
                   2158:              /* worry about earlier kernels on IA64.                   */
1.2       noro     2159: #          else
                   2160: #             if defined(POWERPC)
                   2161:                 char * addr = (char *) (sc.regs->dar);
                   2162: #            else
1.1       noro     2163:                --> architecture not supported
1.2       noro     2164: #            endif
                   2165: #          endif
1.1       noro     2166: #        endif
                   2167: #      endif
                   2168: #     endif
                   2169: #   endif
1.4     ! noro     2170: #   if defined(MACOSX)
        !          2171:         char * addr = get_fault_addr(scp);
        !          2172: #   endif
        !          2173: #   if defined(MSWIN32) || defined(MSWINCE)
1.1       noro     2174:        char * addr = (char *) (exc_info -> ExceptionRecord
                   2175:                                -> ExceptionInformation[1]);
                   2176: #      define sig SIGSEGV
                   2177: #   endif
                   2178:
                   2179:     if (SIG_OK && CODE_OK) {
                   2180:         register struct hblk * h =
                   2181:                        (struct hblk *)((word)addr & ~(GC_page_size-1));
                   2182:         GC_bool in_allocd_block;
                   2183:
                   2184: #      ifdef SUNOS5SIGS
                   2185:            /* Address is only within the correct physical page.        */
                   2186:            in_allocd_block = FALSE;
                   2187:             for (i = 0; i < divHBLKSZ(GC_page_size); i++) {
                   2188:               if (HDR(h+i) != 0) {
                   2189:                 in_allocd_block = TRUE;
                   2190:               }
                   2191:             }
                   2192: #      else
                   2193:            in_allocd_block = (HDR(addr) != 0);
                   2194: #      endif
                   2195:         if (!in_allocd_block) {
                   2196:            /* Heap blocks now begin and end on page boundaries */
                   2197:             SIG_PF old_handler;
                   2198:
                   2199:             if (sig == SIGSEGV) {
                   2200:                old_handler = GC_old_segv_handler;
                   2201:             } else {
                   2202:                 old_handler = GC_old_bus_handler;
                   2203:             }
                   2204:             if (old_handler == SIG_DFL) {
1.4     ! noro     2205: #              if !defined(MSWIN32) && !defined(MSWINCE)
1.1       noro     2206:                    GC_err_printf1("Segfault at 0x%lx\n", addr);
                   2207:                     ABORT("Unexpected bus error or segmentation fault");
                   2208: #              else
                   2209:                    return(EXCEPTION_CONTINUE_SEARCH);
                   2210: #              endif
                   2211:             } else {
                   2212: #              if defined (SUNOS4) || defined(FREEBSD)
                   2213:                    (*old_handler) (sig, code, scp, addr);
                   2214:                    return;
                   2215: #              endif
                   2216: #              if defined (SUNOS5SIGS)
                   2217:                    (*(REAL_SIG_PF)old_handler) (sig, scp, context);
                   2218:                    return;
                   2219: #              endif
                   2220: #              if defined (LINUX)
1.2       noro     2221: #                  if defined(ALPHA) || defined(M68K)
1.1       noro     2222:                        (*(REAL_SIG_PF)old_handler) (sig, code, sc);
                   2223: #                  else
1.4     ! noro     2224: #                    if defined(IA64) || defined(HP_PA)
1.2       noro     2225:                        (*(REAL_SIG_PF)old_handler) (sig, si, scp);
                   2226: #                    else
1.1       noro     2227:                        (*(REAL_SIG_PF)old_handler) (sig, sc);
1.2       noro     2228: #                    endif
1.1       noro     2229: #                  endif
                   2230:                    return;
                   2231: #              endif
                   2232: #              if defined (IRIX5) || defined(OSF1)
                   2233:                    (*(REAL_SIG_PF)old_handler) (sig, code, scp);
                   2234:                    return;
                   2235: #              endif
1.4     ! noro     2236: #              ifdef MACOSX
        !          2237:                    (*(REAL_SIG_PF)old_handler) (sig, code, scp);
        !          2238: #              endif
1.1       noro     2239: #              ifdef MSWIN32
                   2240:                    return((*old_handler)(exc_info));
                   2241: #              endif
                   2242:             }
                   2243:         }
                   2244:         for (i = 0; i < divHBLKSZ(GC_page_size); i++) {
                   2245:             register int index = PHT_HASH(h+i);
                   2246:
1.4     ! noro     2247:             async_set_pht_entry_from_index(GC_dirty_pages, index);
1.1       noro     2248:         }
                   2249:         UNPROTECT(h, GC_page_size);
                   2250: #      if defined(OSF1) || defined(LINUX)
                   2251:            /* These reset the signal handler each time by default. */
                   2252:            signal(SIGSEGV, (SIG_PF) GC_write_fault_handler);
                   2253: #      endif
                   2254:        /* The write may not take place before dirty bits are read.     */
                   2255:        /* But then we'll fault again ...                               */
1.4     ! noro     2256: #      if defined(MSWIN32) || defined(MSWINCE)
1.1       noro     2257:            return(EXCEPTION_CONTINUE_EXECUTION);
                   2258: #      else
                   2259:            return;
                   2260: #      endif
                   2261:     }
1.4     ! noro     2262: #if defined(MSWIN32) || defined(MSWINCE)
1.1       noro     2263:     return EXCEPTION_CONTINUE_SEARCH;
                   2264: #else
                   2265:     GC_err_printf1("Segfault at 0x%lx\n", addr);
                   2266:     ABORT("Unexpected bus error or segmentation fault");
                   2267: #endif
                   2268: }
                   2269:
                   2270: /*
                   2271:  * We hold the allocation lock.  We expect block h to be written
                   2272:  * shortly.
                   2273:  */
                   2274: void GC_write_hint(h)
                   2275: struct hblk *h;
                   2276: {
                   2277:     register struct hblk * h_trunc;
                   2278:     register unsigned i;
                   2279:     register GC_bool found_clean;
                   2280:
                   2281:     if (!GC_dirty_maintained) return;
                   2282:     h_trunc = (struct hblk *)((word)h & ~(GC_page_size-1));
                   2283:     found_clean = FALSE;
                   2284:     for (i = 0; i < divHBLKSZ(GC_page_size); i++) {
                   2285:         register int index = PHT_HASH(h_trunc+i);
                   2286:
                   2287:         if (!get_pht_entry_from_index(GC_dirty_pages, index)) {
                   2288:             found_clean = TRUE;
1.4     ! noro     2289:             async_set_pht_entry_from_index(GC_dirty_pages, index);
1.1       noro     2290:         }
                   2291:     }
                   2292:     if (found_clean) {
                   2293:        UNPROTECT(h_trunc, GC_page_size);
                   2294:     }
                   2295: }
                   2296:
                   2297: void GC_dirty_init()
                   2298: {
1.4     ! noro     2299: #   if defined(SUNOS5SIGS) || defined(IRIX5) /* || defined(OSF1) */
        !          2300:       struct sigaction act, oldact;
        !          2301: #     ifdef IRIX5
1.1       noro     2302:        act.sa_flags    = SA_RESTART;
                   2303:         act.sa_handler  = GC_write_fault_handler;
1.4     ! noro     2304: #     else
1.1       noro     2305:        act.sa_flags    = SA_RESTART | SA_SIGINFO;
                   2306:         act.sa_sigaction = GC_write_fault_handler;
1.4     ! noro     2307: #     endif
        !          2308:       (void)sigemptyset(&act.sa_mask);
        !          2309: #    endif
        !          2310: #   if defined(MACOSX)
        !          2311:       struct sigaction act, oldact;
        !          2312:
        !          2313:       act.sa_flags = SA_RESTART;
        !          2314:       act.sa_handler = GC_write_fault_handler;
        !          2315:       sigemptyset(&act.sa_mask);
1.1       noro     2316: #   endif
                   2317: #   ifdef PRINTSTATS
                   2318:        GC_printf0("Inititalizing mprotect virtual dirty bit implementation\n");
                   2319: #   endif
                   2320:     GC_dirty_maintained = TRUE;
                   2321:     if (GC_page_size % HBLKSIZE != 0) {
                   2322:         GC_err_printf0("Page size not multiple of HBLKSIZE\n");
                   2323:         ABORT("Page size not multiple of HBLKSIZE");
                   2324:     }
                   2325: #   if defined(SUNOS4) || defined(FREEBSD)
                   2326:       GC_old_bus_handler = signal(SIGBUS, GC_write_fault_handler);
                   2327:       if (GC_old_bus_handler == SIG_IGN) {
                   2328:         GC_err_printf0("Previously ignored bus error!?");
                   2329:         GC_old_bus_handler = SIG_DFL;
                   2330:       }
                   2331:       if (GC_old_bus_handler != SIG_DFL) {
                   2332: #      ifdef PRINTSTATS
                   2333:           GC_err_printf0("Replaced other SIGBUS handler\n");
                   2334: #      endif
                   2335:       }
                   2336: #   endif
                   2337: #   if defined(OSF1) || defined(SUNOS4) || defined(LINUX)
                   2338:       GC_old_segv_handler = signal(SIGSEGV, (SIG_PF)GC_write_fault_handler);
                   2339:       if (GC_old_segv_handler == SIG_IGN) {
                   2340:         GC_err_printf0("Previously ignored segmentation violation!?");
                   2341:         GC_old_segv_handler = SIG_DFL;
                   2342:       }
                   2343:       if (GC_old_segv_handler != SIG_DFL) {
                   2344: #      ifdef PRINTSTATS
                   2345:           GC_err_printf0("Replaced other SIGSEGV handler\n");
                   2346: #      endif
                   2347:       }
                   2348: #   endif
                   2349: #   if defined(SUNOS5SIGS) || defined(IRIX5)
1.4     ! noro     2350: #     if defined(IRIX_THREADS)
1.1       noro     2351:        sigaction(SIGSEGV, 0, &oldact);
                   2352:        sigaction(SIGSEGV, &act, 0);
                   2353: #     else
                   2354:        sigaction(SIGSEGV, &act, &oldact);
                   2355: #     endif
                   2356: #     if defined(_sigargs)
                   2357:        /* This is Irix 5.x, not 6.x.  Irix 5.x does not have   */
                   2358:        /* sa_sigaction.                                        */
                   2359:        GC_old_segv_handler = oldact.sa_handler;
                   2360: #     else /* Irix 6.x or SUNOS5SIGS */
                   2361:         if (oldact.sa_flags & SA_SIGINFO) {
                   2362:           GC_old_segv_handler = (SIG_PF)(oldact.sa_sigaction);
                   2363:         } else {
                   2364:           GC_old_segv_handler = oldact.sa_handler;
                   2365:         }
                   2366: #     endif
                   2367:       if (GC_old_segv_handler == SIG_IGN) {
                   2368:             GC_err_printf0("Previously ignored segmentation violation!?");
                   2369:             GC_old_segv_handler = SIG_DFL;
                   2370:       }
                   2371:       if (GC_old_segv_handler != SIG_DFL) {
                   2372: #       ifdef PRINTSTATS
                   2373:          GC_err_printf0("Replaced other SIGSEGV handler\n");
                   2374: #       endif
                   2375:       }
1.4     ! noro     2376: #   endif
        !          2377: #   if defined(MACOSX) || defined(HPUX)
        !          2378:       sigaction(SIGBUS, &act, &oldact);
        !          2379:       GC_old_bus_handler = oldact.sa_handler;
        !          2380:       if (GC_old_bus_handler == SIG_IGN) {
        !          2381:             GC_err_printf0("Previously ignored bus error!?");
        !          2382:             GC_old_bus_handler = SIG_DFL;
        !          2383:       }
        !          2384:       if (GC_old_bus_handler != SIG_DFL) {
        !          2385: #       ifdef PRINTSTATS
        !          2386:          GC_err_printf0("Replaced other SIGBUS handler\n");
        !          2387: #       endif
        !          2388:       }
        !          2389: #   endif /* MACOS || HPUX */
1.1       noro     2390: #   if defined(MSWIN32)
                   2391:       GC_old_segv_handler = SetUnhandledExceptionFilter(GC_write_fault_handler);
                   2392:       if (GC_old_segv_handler != NULL) {
                   2393: #      ifdef PRINTSTATS
                   2394:           GC_err_printf0("Replaced other UnhandledExceptionFilter\n");
                   2395: #      endif
                   2396:       } else {
                   2397:           GC_old_segv_handler = SIG_DFL;
                   2398:       }
                   2399: #   endif
                   2400: }
                   2401:
                   2402:
                   2403:
                   2404: void GC_protect_heap()
                   2405: {
                   2406:     ptr_t start;
                   2407:     word len;
                   2408:     unsigned i;
                   2409:
                   2410:     for (i = 0; i < GC_n_heap_sects; i++) {
                   2411:         start = GC_heap_sects[i].hs_start;
                   2412:         len = GC_heap_sects[i].hs_bytes;
                   2413:         PROTECT(start, len);
                   2414:     }
                   2415: }
                   2416:
                   2417: /* We assume that either the world is stopped or its OK to lose dirty  */
                   2418: /* bits while this is happenning (as in GC_enable_incremental).                */
                   2419: void GC_read_dirty()
                   2420: {
                   2421:     BCOPY((word *)GC_dirty_pages, GC_grungy_pages,
                   2422:           (sizeof GC_dirty_pages));
                   2423:     BZERO((word *)GC_dirty_pages, (sizeof GC_dirty_pages));
                   2424:     GC_protect_heap();
                   2425: }
                   2426:
                   2427: GC_bool GC_page_was_dirty(h)
                   2428: struct hblk * h;
                   2429: {
                   2430:     register word index = PHT_HASH(h);
                   2431:
                   2432:     return(HDR(h) == 0 || get_pht_entry_from_index(GC_grungy_pages, index));
                   2433: }
                   2434:
                   2435: /*
                   2436:  * Acquiring the allocation lock here is dangerous, since this
                   2437:  * can be called from within GC_call_with_alloc_lock, and the cord
                   2438:  * package does so.  On systems that allow nested lock acquisition, this
                   2439:  * happens to work.
                   2440:  * On other systems, SET_LOCK_HOLDER and friends must be suitably defined.
                   2441:  */
1.4     ! noro     2442:
        !          2443: static GC_bool syscall_acquired_lock = FALSE;  /* Protected by GC lock. */
1.1       noro     2444:
                   2445: void GC_begin_syscall()
                   2446: {
1.4     ! noro     2447:     if (!I_HOLD_LOCK()) {
        !          2448:        LOCK();
        !          2449:        syscall_acquired_lock = TRUE;
        !          2450:     }
1.1       noro     2451: }
                   2452:
                   2453: void GC_end_syscall()
                   2454: {
1.4     ! noro     2455:     if (syscall_acquired_lock) {
        !          2456:        syscall_acquired_lock = FALSE;
        !          2457:        UNLOCK();
        !          2458:     }
1.1       noro     2459: }
                   2460:
                   2461: void GC_unprotect_range(addr, len)
                   2462: ptr_t addr;
                   2463: word len;
                   2464: {
                   2465:     struct hblk * start_block;
                   2466:     struct hblk * end_block;
                   2467:     register struct hblk *h;
                   2468:     ptr_t obj_start;
                   2469:
                   2470:     if (!GC_incremental) return;
                   2471:     obj_start = GC_base(addr);
                   2472:     if (obj_start == 0) return;
                   2473:     if (GC_base(addr + len - 1) != obj_start) {
                   2474:         ABORT("GC_unprotect_range(range bigger than object)");
                   2475:     }
                   2476:     start_block = (struct hblk *)((word)addr & ~(GC_page_size - 1));
                   2477:     end_block = (struct hblk *)((word)(addr + len - 1) & ~(GC_page_size - 1));
                   2478:     end_block += GC_page_size/HBLKSIZE - 1;
                   2479:     for (h = start_block; h <= end_block; h++) {
                   2480:         register word index = PHT_HASH(h);
                   2481:
1.4     ! noro     2482:         async_set_pht_entry_from_index(GC_dirty_pages, index);
1.1       noro     2483:     }
                   2484:     UNPROTECT(start_block,
                   2485:              ((ptr_t)end_block - (ptr_t)start_block) + HBLKSIZE);
                   2486: }
                   2487:
1.4     ! noro     2488: #if !defined(MSWIN32) && !defined(MSWINCE) && !defined(LINUX_THREADS) \
        !          2489:     && !defined(GC_USE_LD_WRAP)
1.1       noro     2490: /* Replacement for UNIX system call.    */
                   2491: /* Other calls that write to the heap   */
                   2492: /* should be handled similarly.                 */
                   2493: # if defined(__STDC__) && !defined(SUNOS4)
                   2494: #   include <unistd.h>
1.3       noro     2495: #   include <sys/uio.h>
1.1       noro     2496:     ssize_t read(int fd, void *buf, size_t nbyte)
                   2497: # else
                   2498: #   ifndef LINT
                   2499:       int read(fd, buf, nbyte)
                   2500: #   else
                   2501:       int GC_read(fd, buf, nbyte)
                   2502: #   endif
                   2503:     int fd;
                   2504:     char *buf;
                   2505:     int nbyte;
                   2506: # endif
                   2507: {
                   2508:     int result;
                   2509:
                   2510:     GC_begin_syscall();
                   2511:     GC_unprotect_range(buf, (word)nbyte);
1.3       noro     2512: #   if defined(IRIX5) || defined(LINUX_THREADS)
1.1       noro     2513:        /* Indirect system call may not always be easily available.     */
                   2514:        /* We could call _read, but that would interfere with the       */
                   2515:        /* libpthread interception of read.                             */
1.3       noro     2516:        /* On Linux, we have to be careful with the linuxthreads        */
                   2517:        /* read interception.                                           */
1.1       noro     2518:        {
                   2519:            struct iovec iov;
                   2520:
                   2521:            iov.iov_base = buf;
                   2522:            iov.iov_len = nbyte;
                   2523:            result = readv(fd, &iov, 1);
                   2524:        }
                   2525: #   else
1.4     ! noro     2526:        /* The two zero args at the end of this list are because one
        !          2527:           IA-64 syscall() implementation actually requires six args
        !          2528:           to be passed, even though they aren't always used. */
        !          2529:        result = syscall(SYS_read, fd, buf, nbyte, 0, 0);
1.1       noro     2530: #   endif
                   2531:     GC_end_syscall();
                   2532:     return(result);
                   2533: }
1.4     ! noro     2534: #endif /* !MSWIN32 && !MSWINCE && !LINUX_THREADS */
1.3       noro     2535:
1.4     ! noro     2536: #ifdef GC_USE_LD_WRAP
1.3       noro     2537:     /* We use the GNU ld call wrapping facility.                       */
                   2538:     /* This requires that the linker be invoked with "--wrap read".    */
                   2539:     /* This can be done by passing -Wl,"--wrap read" to gcc.           */
                   2540:     /* I'm not sure that this actually wraps whatever version of read  */
                   2541:     /* is called by stdio.  That code also mentions __read.            */
                   2542: #   include <unistd.h>
                   2543:     ssize_t __wrap_read(int fd, void *buf, size_t nbyte)
                   2544:     {
                   2545:        int result;
                   2546:
                   2547:        GC_begin_syscall();
                   2548:        GC_unprotect_range(buf, (word)nbyte);
                   2549:        result = __real_read(fd, buf, nbyte);
                   2550:        GC_end_syscall();
                   2551:        return(result);
                   2552:     }
                   2553:
                   2554:     /* We should probably also do this for __read, or whatever stdio   */
                   2555:     /* actually calls.                                                 */
                   2556: #endif
1.1       noro     2557:
                   2558: /*ARGSUSED*/
                   2559: GC_bool GC_page_was_ever_dirty(h)
                   2560: struct hblk *h;
                   2561: {
                   2562:     return(TRUE);
                   2563: }
                   2564:
                   2565: /* Reset the n pages starting at h to "was never dirty" status.        */
                   2566: /*ARGSUSED*/
                   2567: void GC_is_fresh(h, n)
                   2568: struct hblk *h;
                   2569: word n;
                   2570: {
                   2571: }
                   2572:
1.4     ! noro     2573: # else /* !MPROTECT_VDB */
        !          2574:
        !          2575: #   ifdef GC_USE_LD_WRAP
        !          2576:       ssize_t __wrap_read(int fd, void *buf, size_t nbyte)
        !          2577:       { return __real_read(fd, buf, nbyte); }
        !          2578: #   endif
        !          2579:
1.1       noro     2580: # endif /* MPROTECT_VDB */
                   2581:
                   2582: # ifdef PROC_VDB
                   2583:
                   2584: /*
                   2585:  * See DEFAULT_VDB for interface descriptions.
                   2586:  */
                   2587:
                   2588: /*
                   2589:  * This implementaion assumes a Solaris 2.X like /proc pseudo-file-system
                   2590:  * from which we can read page modified bits.  This facility is far from
                   2591:  * optimal (e.g. we would like to get the info for only some of the
                   2592:  * address space), but it avoids intercepting system calls.
                   2593:  */
                   2594:
                   2595: #include <errno.h>
                   2596: #include <sys/types.h>
                   2597: #include <sys/signal.h>
                   2598: #include <sys/fault.h>
                   2599: #include <sys/syscall.h>
                   2600: #include <sys/procfs.h>
                   2601: #include <sys/stat.h>
                   2602:
                   2603: #define INITIAL_BUF_SZ 4096
                   2604: word GC_proc_buf_size = INITIAL_BUF_SZ;
                   2605: char *GC_proc_buf;
                   2606:
                   2607: #ifdef SOLARIS_THREADS
                   2608: /* We don't have exact sp values for threads.  So we count on  */
                   2609: /* occasionally declaring stack pages to be fresh.  Thus we    */
                   2610: /* need a real implementation of GC_is_fresh.  We can't clear  */
                   2611: /* entries in GC_written_pages, since that would declare all   */
                   2612: /* pages with the given hash address to be fresh.              */
                   2613: #   define MAX_FRESH_PAGES 8*1024      /* Must be power of 2 */
                   2614:     struct hblk ** GC_fresh_pages;     /* A direct mapped cache.       */
                   2615:                                        /* Collisions are dropped.      */
                   2616:
                   2617: #   define FRESH_PAGE_SLOT(h) (divHBLKSZ((word)(h)) & (MAX_FRESH_PAGES-1))
                   2618: #   define ADD_FRESH_PAGE(h) \
                   2619:        GC_fresh_pages[FRESH_PAGE_SLOT(h)] = (h)
                   2620: #   define PAGE_IS_FRESH(h) \
                   2621:        (GC_fresh_pages[FRESH_PAGE_SLOT(h)] == (h) && (h) != 0)
                   2622: #endif
                   2623:
                   2624: /* Add all pages in pht2 to pht1 */
                   2625: void GC_or_pages(pht1, pht2)
                   2626: page_hash_table pht1, pht2;
                   2627: {
                   2628:     register int i;
                   2629:
                   2630:     for (i = 0; i < PHT_SIZE; i++) pht1[i] |= pht2[i];
                   2631: }
                   2632:
                   2633: int GC_proc_fd;
                   2634:
                   2635: void GC_dirty_init()
                   2636: {
                   2637:     int fd;
                   2638:     char buf[30];
                   2639:
                   2640:     GC_dirty_maintained = TRUE;
                   2641:     if (GC_words_allocd != 0 || GC_words_allocd_before_gc != 0) {
                   2642:        register int i;
                   2643:
                   2644:         for (i = 0; i < PHT_SIZE; i++) GC_written_pages[i] = (word)(-1);
                   2645: #       ifdef PRINTSTATS
                   2646:            GC_printf1("Allocated words:%lu:all pages may have been written\n",
                   2647:                       (unsigned long)
                   2648:                                (GC_words_allocd + GC_words_allocd_before_gc));
                   2649: #      endif
                   2650:     }
                   2651:     sprintf(buf, "/proc/%d", getpid());
                   2652:     fd = open(buf, O_RDONLY);
                   2653:     if (fd < 0) {
                   2654:        ABORT("/proc open failed");
                   2655:     }
                   2656:     GC_proc_fd = syscall(SYS_ioctl, fd, PIOCOPENPD, 0);
                   2657:     close(fd);
                   2658:     if (GC_proc_fd < 0) {
                   2659:        ABORT("/proc ioctl failed");
                   2660:     }
                   2661:     GC_proc_buf = GC_scratch_alloc(GC_proc_buf_size);
                   2662: #   ifdef SOLARIS_THREADS
                   2663:        GC_fresh_pages = (struct hblk **)
                   2664:          GC_scratch_alloc(MAX_FRESH_PAGES * sizeof (struct hblk *));
                   2665:        if (GC_fresh_pages == 0) {
                   2666:            GC_err_printf0("No space for fresh pages\n");
                   2667:            EXIT();
                   2668:        }
                   2669:        BZERO(GC_fresh_pages, MAX_FRESH_PAGES * sizeof (struct hblk *));
                   2670: #   endif
                   2671: }
                   2672:
                   2673: /* Ignore write hints. They don't help us here.        */
                   2674: /*ARGSUSED*/
                   2675: void GC_write_hint(h)
                   2676: struct hblk *h;
                   2677: {
                   2678: }
                   2679:
                   2680: #ifdef SOLARIS_THREADS
                   2681: #   define READ(fd,buf,nbytes) syscall(SYS_read, fd, buf, nbytes)
                   2682: #else
                   2683: #   define READ(fd,buf,nbytes) read(fd, buf, nbytes)
                   2684: #endif
                   2685:
                   2686: void GC_read_dirty()
                   2687: {
                   2688:     unsigned long ps, np;
                   2689:     int nmaps;
                   2690:     ptr_t vaddr;
                   2691:     struct prasmap * map;
                   2692:     char * bufp;
                   2693:     ptr_t current_addr, limit;
                   2694:     int i;
                   2695: int dummy;
                   2696:
                   2697:     BZERO(GC_grungy_pages, (sizeof GC_grungy_pages));
                   2698:
                   2699:     bufp = GC_proc_buf;
                   2700:     if (READ(GC_proc_fd, bufp, GC_proc_buf_size) <= 0) {
                   2701: #      ifdef PRINTSTATS
                   2702:             GC_printf1("/proc read failed: GC_proc_buf_size = %lu\n",
                   2703:                       GC_proc_buf_size);
                   2704: #      endif
                   2705:         {
                   2706:             /* Retry with larger buffer. */
                   2707:             word new_size = 2 * GC_proc_buf_size;
                   2708:             char * new_buf = GC_scratch_alloc(new_size);
                   2709:
                   2710:             if (new_buf != 0) {
                   2711:                 GC_proc_buf = bufp = new_buf;
                   2712:                 GC_proc_buf_size = new_size;
                   2713:             }
                   2714:             if (syscall(SYS_read, GC_proc_fd, bufp, GC_proc_buf_size) <= 0) {
                   2715:                 WARN("Insufficient space for /proc read\n", 0);
                   2716:                 /* Punt:       */
                   2717:                memset(GC_grungy_pages, 0xff, sizeof (page_hash_table));
                   2718:                memset(GC_written_pages, 0xff, sizeof(page_hash_table));
                   2719: #              ifdef SOLARIS_THREADS
                   2720:                    BZERO(GC_fresh_pages,
                   2721:                          MAX_FRESH_PAGES * sizeof (struct hblk *));
                   2722: #              endif
                   2723:                return;
                   2724:             }
                   2725:         }
                   2726:     }
                   2727:     /* Copy dirty bits into GC_grungy_pages */
                   2728:        nmaps = ((struct prpageheader *)bufp) -> pr_nmap;
                   2729:        /* printf( "nmaps = %d, PG_REFERENCED = %d, PG_MODIFIED = %d\n",
                   2730:                     nmaps, PG_REFERENCED, PG_MODIFIED); */
                   2731:        bufp = bufp + sizeof(struct prpageheader);
                   2732:        for (i = 0; i < nmaps; i++) {
                   2733:            map = (struct prasmap *)bufp;
                   2734:            vaddr = (ptr_t)(map -> pr_vaddr);
                   2735:            ps = map -> pr_pagesize;
                   2736:            np = map -> pr_npage;
                   2737:            /* printf("vaddr = 0x%X, ps = 0x%X, np = 0x%X\n", vaddr, ps, np); */
                   2738:            limit = vaddr + ps * np;
                   2739:            bufp += sizeof (struct prasmap);
                   2740:            for (current_addr = vaddr;
                   2741:                 current_addr < limit; current_addr += ps){
                   2742:                if ((*bufp++) & PG_MODIFIED) {
                   2743:                    register struct hblk * h = (struct hblk *) current_addr;
                   2744:
                   2745:                    while ((ptr_t)h < current_addr + ps) {
                   2746:                        register word index = PHT_HASH(h);
                   2747:
                   2748:                        set_pht_entry_from_index(GC_grungy_pages, index);
                   2749: #                      ifdef SOLARIS_THREADS
                   2750:                          {
                   2751:                            register int slot = FRESH_PAGE_SLOT(h);
                   2752:
                   2753:                            if (GC_fresh_pages[slot] == h) {
                   2754:                                GC_fresh_pages[slot] = 0;
                   2755:                            }
                   2756:                          }
                   2757: #                      endif
                   2758:                        h++;
                   2759:                    }
                   2760:                }
                   2761:            }
                   2762:            bufp += sizeof(long) - 1;
                   2763:            bufp = (char *)((unsigned long)bufp & ~(sizeof(long)-1));
                   2764:        }
                   2765:     /* Update GC_written_pages. */
                   2766:         GC_or_pages(GC_written_pages, GC_grungy_pages);
                   2767: #   ifdef SOLARIS_THREADS
                   2768:       /* Make sure that old stacks are considered completely clean     */
                   2769:       /* unless written again.                                         */
                   2770:        GC_old_stacks_are_fresh();
                   2771: #   endif
                   2772: }
                   2773:
                   2774: #undef READ
                   2775:
                   2776: GC_bool GC_page_was_dirty(h)
                   2777: struct hblk *h;
                   2778: {
                   2779:     register word index = PHT_HASH(h);
                   2780:     register GC_bool result;
                   2781:
                   2782:     result = get_pht_entry_from_index(GC_grungy_pages, index);
                   2783: #   ifdef SOLARIS_THREADS
                   2784:        if (result && PAGE_IS_FRESH(h)) result = FALSE;
                   2785:        /* This happens only if page was declared fresh since   */
                   2786:        /* the read_dirty call, e.g. because it's in an unused  */
                   2787:        /* thread stack.  It's OK to treat it as clean, in      */
                   2788:        /* that case.  And it's consistent with                 */
                   2789:        /* GC_page_was_ever_dirty.                              */
                   2790: #   endif
                   2791:     return(result);
                   2792: }
                   2793:
                   2794: GC_bool GC_page_was_ever_dirty(h)
                   2795: struct hblk *h;
                   2796: {
                   2797:     register word index = PHT_HASH(h);
                   2798:     register GC_bool result;
                   2799:
                   2800:     result = get_pht_entry_from_index(GC_written_pages, index);
                   2801: #   ifdef SOLARIS_THREADS
                   2802:        if (result && PAGE_IS_FRESH(h)) result = FALSE;
                   2803: #   endif
                   2804:     return(result);
                   2805: }
                   2806:
                   2807: /* Caller holds allocation lock.       */
                   2808: void GC_is_fresh(h, n)
                   2809: struct hblk *h;
                   2810: word n;
                   2811: {
                   2812:
                   2813:     register word index;
                   2814:
                   2815: #   ifdef SOLARIS_THREADS
                   2816:       register word i;
                   2817:
                   2818:       if (GC_fresh_pages != 0) {
                   2819:         for (i = 0; i < n; i++) {
                   2820:           ADD_FRESH_PAGE(h + i);
                   2821:         }
                   2822:       }
                   2823: #   endif
                   2824: }
                   2825:
                   2826: # endif /* PROC_VDB */
                   2827:
                   2828:
                   2829: # ifdef PCR_VDB
                   2830:
                   2831: # include "vd/PCR_VD.h"
                   2832:
                   2833: # define NPAGES (32*1024)      /* 128 MB */
                   2834:
                   2835: PCR_VD_DB  GC_grungy_bits[NPAGES];
                   2836:
                   2837: ptr_t GC_vd_base;      /* Address corresponding to GC_grungy_bits[0]   */
                   2838:                        /* HBLKSIZE aligned.                            */
                   2839:
                   2840: void GC_dirty_init()
                   2841: {
                   2842:     GC_dirty_maintained = TRUE;
                   2843:     /* For the time being, we assume the heap generally grows up */
                   2844:     GC_vd_base = GC_heap_sects[0].hs_start;
                   2845:     if (GC_vd_base == 0) {
                   2846:        ABORT("Bad initial heap segment");
                   2847:     }
                   2848:     if (PCR_VD_Start(HBLKSIZE, GC_vd_base, NPAGES*HBLKSIZE)
                   2849:        != PCR_ERes_okay) {
                   2850:        ABORT("dirty bit initialization failed");
                   2851:     }
                   2852: }
                   2853:
                   2854: void GC_read_dirty()
                   2855: {
                   2856:     /* lazily enable dirty bits on newly added heap sects */
                   2857:     {
                   2858:         static int onhs = 0;
                   2859:         int nhs = GC_n_heap_sects;
                   2860:         for( ; onhs < nhs; onhs++ ) {
                   2861:             PCR_VD_WriteProtectEnable(
                   2862:                     GC_heap_sects[onhs].hs_start,
                   2863:                     GC_heap_sects[onhs].hs_bytes );
                   2864:         }
                   2865:     }
                   2866:
                   2867:
                   2868:     if (PCR_VD_Clear(GC_vd_base, NPAGES*HBLKSIZE, GC_grungy_bits)
                   2869:         != PCR_ERes_okay) {
                   2870:        ABORT("dirty bit read failed");
                   2871:     }
                   2872: }
                   2873:
                   2874: GC_bool GC_page_was_dirty(h)
                   2875: struct hblk *h;
                   2876: {
                   2877:     if((ptr_t)h < GC_vd_base || (ptr_t)h >= GC_vd_base + NPAGES*HBLKSIZE) {
                   2878:        return(TRUE);
                   2879:     }
                   2880:     return(GC_grungy_bits[h - (struct hblk *)GC_vd_base] & PCR_VD_DB_dirtyBit);
                   2881: }
                   2882:
                   2883: /*ARGSUSED*/
                   2884: void GC_write_hint(h)
                   2885: struct hblk *h;
                   2886: {
                   2887:     PCR_VD_WriteProtectDisable(h, HBLKSIZE);
                   2888:     PCR_VD_WriteProtectEnable(h, HBLKSIZE);
                   2889: }
                   2890:
                   2891: # endif /* PCR_VDB */
                   2892:
                   2893: /*
                   2894:  * Call stack save code for debugging.
                   2895:  * Should probably be in mach_dep.c, but that requires reorganization.
                   2896:  */
1.4     ! noro     2897:
        !          2898: /* I suspect the following works for most X86 *nix variants, so        */
        !          2899: /* long as the frame pointer is explicitly stored.  In the case of gcc,        */
        !          2900: /* compiler flags (e.g. -fomit-frame-pointer) determine whether it is. */
        !          2901: #if defined(I386) && defined(LINUX) && defined(SAVE_CALL_CHAIN)
        !          2902:     struct frame {
        !          2903:        struct frame *fr_savfp;
        !          2904:        long    fr_savpc;
        !          2905:         long   fr_arg[NARGS];  /* All the arguments go here.   */
        !          2906:     };
        !          2907: #endif
        !          2908:
        !          2909: #if defined(SPARC)
        !          2910: #  if defined(LINUX)
        !          2911:      struct frame {
        !          2912:        long    fr_local[8];
        !          2913:        long    fr_arg[6];
        !          2914:        struct frame *fr_savfp;
        !          2915:        long    fr_savpc;
        !          2916: #       ifndef __arch64__
        !          2917:          char  *fr_stret;
        !          2918: #       endif
        !          2919:        long    fr_argd[6];
        !          2920:        long    fr_argx[0];
        !          2921:      };
        !          2922: #  else
        !          2923: #    if defined(SUNOS4)
        !          2924: #      include <machine/frame.h>
        !          2925: #    else
        !          2926: #      if defined (DRSNX)
        !          2927: #       include <sys/sparc/frame.h>
        !          2928: #      else
        !          2929: #       if defined(OPENBSD) || defined(NETBSD)
        !          2930: #         include <frame.h>
        !          2931: #       else
        !          2932: #         include <sys/frame.h>
        !          2933: #       endif
        !          2934: #      endif
        !          2935: #    endif
        !          2936: #  endif
        !          2937: #  if NARGS > 6
1.1       noro     2938:        --> We only know how to to get the first 6 arguments
1.4     ! noro     2939: #  endif
        !          2940: #endif /* SPARC */
1.1       noro     2941:
                   2942: #ifdef SAVE_CALL_CHAIN
                   2943: /* Fill in the pc and argument information for up to NFRAMES of my     */
                   2944: /* callers.  Ignore my frame and my callers frame.                     */
                   2945:
1.4     ! noro     2946: #if (defined(OPENBSD) || defined(NETBSD)) && defined(SPARC)
1.1       noro     2947: #  define FR_SAVFP fr_fp
                   2948: #  define FR_SAVPC fr_pc
                   2949: #else
                   2950: #  define FR_SAVFP fr_savfp
                   2951: #  define FR_SAVPC fr_savpc
                   2952: #endif
                   2953:
1.4     ! noro     2954: #if defined(SPARC) && (defined(__arch64__) || defined(__sparcv9))
        !          2955: #   define BIAS 2047
        !          2956: #else
        !          2957: #   define BIAS 0
        !          2958: #endif
        !          2959:
1.1       noro     2960: void GC_save_callers (info)
                   2961: struct callinfo info[NFRAMES];
                   2962: {
                   2963:   struct frame *frame;
                   2964:   struct frame *fp;
                   2965:   int nframes = 0;
1.4     ! noro     2966: # ifdef I386
        !          2967:     /* We assume this is turned on only with gcc as the compiler. */
        !          2968:     asm("movl %%ebp,%0" : "=r"(frame));
        !          2969:     fp = frame;
        !          2970: # else
        !          2971:     word GC_save_regs_in_stack();
1.1       noro     2972:
1.4     ! noro     2973:     frame = (struct frame *) GC_save_regs_in_stack ();
        !          2974:     fp = (struct frame *)((long) frame -> FR_SAVFP + BIAS);
        !          2975: #endif
1.1       noro     2976:
1.4     ! noro     2977:    for (; (!(fp HOTTER_THAN frame) && !(GC_stackbottom HOTTER_THAN (ptr_t)fp)
        !          2978:           && (nframes < NFRAMES));
        !          2979:        fp = (struct frame *)((long) fp -> FR_SAVFP + BIAS), nframes++) {
1.1       noro     2980:       register int i;
                   2981:
                   2982:       info[nframes].ci_pc = fp->FR_SAVPC;
                   2983:       for (i = 0; i < NARGS; i++) {
                   2984:        info[nframes].ci_arg[i] = ~(fp->fr_arg[i]);
                   2985:       }
                   2986:   }
                   2987:   if (nframes < NFRAMES) info[nframes].ci_pc = 0;
                   2988: }
                   2989:
                   2990: #endif /* SAVE_CALL_CHAIN */
                   2991:
1.4     ! noro     2992: #if defined(LINUX) && defined(__ELF__) && \
        !          2993:     (!defined(SMALL_CONFIG) || defined(USE_PROC_FOR_LIBRARIES))
        !          2994: #ifdef GC_USE_LD_WRAP
        !          2995: #   define READ __real_read
        !          2996: #else
        !          2997: #   define READ read
        !          2998: #endif
        !          2999:
        !          3000:
        !          3001: /* Repeatedly perform a read call until the buffer is filled or        */
        !          3002: /* we encounter EOF.                                           */
        !          3003: ssize_t GC_repeat_read(int fd, char *buf, size_t count)
        !          3004: {
        !          3005:     ssize_t num_read = 0;
        !          3006:     ssize_t result;
        !          3007:
        !          3008:     while (num_read < count) {
        !          3009:        result = READ(fd, buf + num_read, count - num_read);
        !          3010:        if (result < 0) return result;
        !          3011:        if (result == 0) break;
        !          3012:        num_read += result;
        !          3013:     }
        !          3014:     return num_read;
        !          3015: }
        !          3016: #endif /* LINUX && ... */
        !          3017:
        !          3018:
        !          3019: #if defined(LINUX) && defined(__ELF__) && !defined(SMALL_CONFIG)
        !          3020:
        !          3021: /* Dump /proc/self/maps to GC_stderr, to enable looking up names for
        !          3022:    addresses in FIND_LEAK output. */
        !          3023:
        !          3024: void GC_print_address_map()
        !          3025: {
        !          3026:     int f;
        !          3027:     int result;
        !          3028:     char maps_temp[32768];
        !          3029:     GC_err_printf0("---------- Begin address map ----------\n");
        !          3030:         f = open("/proc/self/maps", O_RDONLY);
        !          3031:         if (-1 == f) ABORT("Couldn't open /proc/self/maps");
        !          3032:        do {
        !          3033:            result = GC_repeat_read(f, maps_temp, sizeof(maps_temp));
        !          3034:            if (result <= 0) ABORT("Couldn't read /proc/self/maps");
        !          3035:            GC_err_write(maps_temp, result);
        !          3036:        } while (result == sizeof(maps_temp));
        !          3037:
        !          3038:     GC_err_printf0("---------- End address map ----------\n");
        !          3039: }
        !          3040:
        !          3041: #endif
1.1       noro     3042:
                   3043:

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>