[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.3

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

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