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

Annotation of OpenXM_contrib/gc/os_dep.c, Revision 1.1.1.2

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

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