home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / sa104os2.zip / SATHR104.ZIP / SATHER / SYSTEM / GC / OS_DEP.C < prev    next >
Text File  |  1995-02-03  |  53KB  |  1,878 lines

  1. /*
  2.  * Copyright (c) 1991-1994 by Xerox Corporation.  All rights reserved.
  3.  *
  4.  * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
  5.  * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
  6.  *
  7.  * Permission is hereby granted to use or copy this program
  8.  * for any purpose,  provided the above notices are retained on all copies.
  9.  * Permission to modify the code and to distribute modified code is granted,
  10.  * provided the above notices are retained, and a notice that the code was
  11.  * modified is included with the above copyright notice.
  12.  */
  13. /* Boehm, November 4, 1994 4:23 pm PST */
  14.  
  15. # include "gc_priv.h"
  16. # if !defined(OS2) && !defined(PCR) && !defined(AMIGA) && !defined(MACOS)
  17. #   include <sys/types.h>
  18. # endif
  19. # include <stdio.h>
  20. # include <signal.h>
  21.  
  22. /* Blatantly OS dependent routines, except for those that are related     */
  23. /* dynamic loading.                            */
  24.  
  25. #ifdef FREEBSD
  26. #  include <machine/trap.h>
  27. #endif
  28.  
  29. #ifdef AMIGA
  30. # include <proto/exec.h>
  31. # include <proto/dos.h>
  32. # include <dos/dosextens.h>
  33. # include <workbench/startup.h>
  34. #endif
  35.  
  36. #ifdef MSWIN32
  37. # define WIN32_LEAN_AND_MEAN
  38. # define NOSERVICE
  39. # include <windows.h>
  40. #endif
  41.  
  42. #ifdef MACOS
  43. # include <Processes.h>
  44. #endif
  45.  
  46. #ifdef IRIX5
  47. # include <sys/uio.h>
  48. #endif
  49.  
  50. #ifdef SUNOS5SIGS
  51. # include <sys/siginfo.h>
  52. #endif
  53.  
  54. #ifdef PCR
  55. # include "il/PCR_IL.h"
  56. # include "th/PCR_ThCtl.h"
  57. # include "mm/PCR_MM.h"
  58. #endif
  59.  
  60. # ifdef OS2
  61.  
  62. # include <stddef.h>
  63.  
  64. # ifndef __IBMC__ /* e.g. EMX */
  65.  
  66. struct exe_hdr {
  67.     unsigned short      magic_number;
  68.     unsigned short      padding[29];
  69.     long                new_exe_offset;
  70. };
  71.  
  72. #define E_MAGIC(x)      (x).magic_number
  73. #define EMAGIC          0x5A4D  
  74. #define E_LFANEW(x)     (x).new_exe_offset
  75.  
  76. struct e32_exe {
  77.     unsigned char       magic_number[2]; 
  78.     unsigned char       byte_order; 
  79.     unsigned char       word_order; 
  80.     unsigned long       exe_format_level;
  81.     unsigned short      cpu;       
  82.     unsigned short      os;
  83.     unsigned long       padding1[13];
  84.     unsigned long       object_table_offset;
  85.     unsigned long       object_count;    
  86.     unsigned long       padding2[31];
  87. };
  88.  
  89. #define E32_MAGIC1(x)   (x).magic_number[0]
  90. #define E32MAGIC1       'L'
  91. #define E32_MAGIC2(x)   (x).magic_number[1]
  92. #define E32MAGIC2       'X'
  93. #define E32_BORDER(x)   (x).byte_order
  94. #define E32LEBO         0
  95. #define E32_WORDER(x)   (x).word_order
  96. #define E32LEWO         0
  97. #define E32_CPU(x)      (x).cpu
  98. #define E32CPU286       1
  99. #define E32_OBJTAB(x)   (x).object_table_offset
  100. #define E32_OBJCNT(x)   (x).object_count
  101.  
  102. struct o32_obj {
  103.     unsigned long       size;  
  104.     unsigned long       base;
  105.     unsigned long       flags;  
  106.     unsigned long       pagemap;
  107.     unsigned long       mapsize; 
  108.     unsigned long       reserved;
  109. };
  110.  
  111. #define O32_FLAGS(x)    (x).flags
  112. #define OBJREAD         0x0001L
  113. #define OBJWRITE        0x0002L
  114. #define OBJINVALID      0x0080L
  115. #define O32_SIZE(x)     (x).size
  116. #define O32_BASE(x)     (x).base
  117.  
  118. # else  /* IBM's compiler */
  119.  
  120. /* A kludge to get around what appears to be a header file bug */
  121. # ifndef WORD
  122. #   define WORD unsigned short
  123. # endif
  124. # ifndef DWORD
  125. #   define DWORD unsigned long
  126. # endif
  127.  
  128. # define EXE386 1
  129. # include <newexe.h>
  130. # include <exe386.h>
  131.  
  132. # endif  /* __IBMC__ */
  133.  
  134. # define INCL_DOSEXCEPTIONS
  135. # define INCL_DOSPROCESS
  136. # define INCL_DOSERRORS
  137. # define INCL_DOSMODULEMGR
  138. # define INCL_DOSMEMMGR
  139. # include <os2.h>
  140.  
  141.  
  142. /* Disable and enable signals during nontrivial allocations    */
  143.  
  144. void GC_disable_signals(void)
  145. {
  146.     ULONG nest;
  147.     
  148.     DosEnterMustComplete(&nest);
  149.     if (nest != 1) ABORT("nested GC_disable_signals");
  150. }
  151.  
  152. void GC_enable_signals(void)
  153. {
  154.     ULONG nest;
  155.     
  156.     DosExitMustComplete(&nest);
  157.     if (nest != 0) ABORT("GC_enable_signals");
  158. }
  159.  
  160.  
  161. # else
  162.  
  163. #  if !defined(PCR) && !defined(AMIGA) && !defined(MSWIN32) && !defined(MACOS)
  164.  
  165. #   ifdef sigmask
  166.     /* Use the traditional BSD interface */
  167. #    define SIGSET_T int
  168. #    define SIG_DEL(set, signal) (set) &= ~(sigmask(signal))
  169. #    define SIG_FILL(set)  (set) = 0x7fffffff
  170.           /* Setting the leading bit appears to provoke a bug in some    */
  171.           /* longjmp implementations.  Most systems appear not to have    */
  172.           /* a signal 32.                        */
  173. #    define SIGSETMASK(old, new) (old) = sigsetmask(new)
  174. #   else
  175.     /* Use POSIX/SYSV interface    */
  176. #    define SIGSET_T sigset_t
  177. #    define SIG_DEL(set, signal) sigdelset(&(set), (signal))
  178. #    define SIG_FILL(set) sigfillset(&set)
  179. #    define SIGSETMASK(old, new) sigprocmask(SIG_SETMASK, &(new), &(old))
  180. #   endif
  181.  
  182. static bool mask_initialized = FALSE;
  183.  
  184. static SIGSET_T new_mask;
  185.  
  186. static SIGSET_T old_mask;
  187.  
  188. static SIGSET_T dummy;
  189.  
  190. #if defined(PRINTSTATS) && !defined(THREADS)
  191. # define CHECK_SIGNALS
  192.   int GC_sig_disabled = 0;
  193. #endif
  194.  
  195. void GC_disable_signals()
  196. {
  197.     if (!mask_initialized) {
  198.         SIG_FILL(new_mask);
  199.  
  200.     SIG_DEL(new_mask, SIGSEGV);
  201.     SIG_DEL(new_mask, SIGILL);
  202.     SIG_DEL(new_mask, SIGQUIT);
  203. #    ifdef SIGBUS
  204.         SIG_DEL(new_mask, SIGBUS);
  205. #    endif
  206. #    ifdef SIGIOT
  207.         SIG_DEL(new_mask, SIGIOT);
  208. #    endif
  209. #    ifdef SIGEMT
  210.         SIG_DEL(new_mask, SIGEMT);
  211. #    endif
  212. #    ifdef SIGTRAP
  213.         SIG_DEL(new_mask, SIGTRAP);
  214. #    endif 
  215.     mask_initialized = TRUE;
  216.     }
  217. #   ifdef CHECK_SIGNALS
  218.     if (GC_sig_disabled != 0) ABORT("Nested disables");
  219.     GC_sig_disabled++;
  220. #   endif
  221.     SIGSETMASK(old_mask,new_mask);
  222. }
  223.  
  224. void GC_enable_signals()
  225. {
  226. #   ifdef CHECK_SIGNALS
  227.     if (GC_sig_disabled != 1) ABORT("Unmatched enable");
  228.     GC_sig_disabled--;
  229. #   endif
  230.     SIGSETMASK(dummy,old_mask);
  231. }
  232.  
  233. #  endif  /* !PCR */
  234.  
  235. # endif /*!OS/2 */
  236.  
  237. /*
  238.  * Find the base of the stack.
  239.  * Used only in single-threaded environment.
  240.  * With threads, GC_mark_roots needs to know how to do this.
  241.  * Called with allocator lock held.
  242.  */
  243. # ifdef MSWIN32
  244.  
  245. /* Get the page size.    */
  246. word GC_page_size = 0;
  247.  
  248. word GC_get_page_size()
  249. {
  250.     SYSTEM_INFO sysinfo;
  251.     
  252.     if (GC_page_size == 0) {
  253.         GetSystemInfo(&sysinfo);
  254.         GC_page_size = sysinfo.dwPageSize;
  255.     }
  256.     return(GC_page_size);
  257. }
  258.  
  259. # define is_writable(prot) ((prot) == PAGE_READWRITE \
  260.                 || (prot) == PAGE_WRITECOPY \
  261.                 || (prot) == PAGE_EXECUTE_READWRITE \
  262.                 || (prot) == PAGE_EXECUTE_WRITECOPY)
  263. /* Return the number of bytes that are writable starting at p.    */
  264. /* The pointer p is assumed to be page aligned.            */
  265. /* If base is not 0, *base becomes the beginning of the     */
  266. /* allocation region containing p.                */
  267. word GC_get_writable_length(ptr_t p, ptr_t *base)
  268. {
  269.     MEMORY_BASIC_INFORMATION buf;
  270.     word result;
  271.     word protect;
  272.     
  273.     result = VirtualQuery(p, &buf, sizeof(buf));
  274.     if (result != sizeof(buf)) ABORT("Weird VirtualQuery result");
  275.     if (base != 0) *base = (ptr_t)(buf.AllocationBase);
  276.     protect = (buf.Protect & ~(PAGE_GUARD | PAGE_NOCACHE));
  277.     if (!is_writable(protect)) {
  278.         return(0);
  279.     }
  280.     if (buf.State != MEM_COMMIT) return(0);
  281.     return(buf.RegionSize);
  282. }
  283.  
  284. ptr_t GC_get_stack_base()
  285. {
  286.     int dummy;
  287.     ptr_t sp = (ptr_t)(&dummy);
  288.     ptr_t trunc_sp = (ptr_t)((word)sp & ~(GC_get_page_size() - 1));
  289.     word size = GC_get_writable_length(trunc_sp, 0);
  290.    
  291.     return(trunc_sp + size);
  292. }
  293.  
  294.  
  295. # else
  296.  
  297. # ifdef OS2
  298.  
  299. ptr_t GC_get_stack_base()
  300. {
  301.     PTIB ptib;
  302.     PPIB ppib;
  303.     
  304.     if (DosGetInfoBlocks(&ptib, &ppib) != NO_ERROR) {
  305.         GC_err_printf0("DosGetInfoBlocks failed\n");
  306.         ABORT("DosGetInfoBlocks failed\n");
  307.     }
  308.     return((ptr_t)(ptib -> tib_pstacklimit));
  309. }
  310.  
  311. # else
  312.  
  313. # ifdef AMIGA
  314.  
  315. ptr_t GC_get_stack_base()
  316. {
  317.     extern struct WBStartup *_WBenchMsg;
  318.     extern long __base;
  319.     extern long __stack;
  320.     struct Task *task;
  321.     struct Process *proc;
  322.     struct CommandLineInterface *cli;
  323.     long size;
  324.  
  325.     if ((task = FindTask(0)) == 0) {
  326.     GC_err_puts("Cannot find own task structure\n");
  327.     ABORT("task missing");
  328.     }
  329.     proc = (struct Process *)task;
  330.     cli = BADDR(proc->pr_CLI);
  331.  
  332.     if (_WBenchMsg != 0 || cli == 0) {
  333.     size = (char *)task->tc_SPUpper - (char *)task->tc_SPLower;
  334.     } else {
  335.     size = cli->cli_DefaultStack * 4;
  336.     }
  337.     return (ptr_t)(__base + GC_max(size, __stack));
  338. }
  339.  
  340. # else
  341.  
  342. # if !defined(THREADS) && !defined(STACKBOTTOM) && defined(HEURISTIC2)
  343. #   define NEED_FIND_LIMIT
  344. # endif
  345.  
  346. # if defined(SUNOS4) & defined(DYNAMIC_LOADING)
  347. #   define NEED_FIND_LIMIT
  348. # endif
  349.  
  350. # ifdef NEED_FIND_LIMIT
  351.   /* Some tools to implement HEURISTIC2    */
  352. #   define MIN_PAGE_SIZE 256    /* Smallest conceivable page size, bytes */
  353. #   include <setjmp.h>
  354.     /* static */ jmp_buf GC_jmp_buf;
  355.     
  356.     /*ARGSUSED*/
  357.     void GC_fault_handler(sig)
  358.     int sig;
  359.     {
  360.         longjmp(GC_jmp_buf, 1);
  361.     }
  362.  
  363. #   ifdef __STDC__
  364.     typedef void (*handler)(int);
  365. #   else
  366.     typedef void (*handler)();
  367. #   endif
  368.  
  369.     /* Return the first nonaddressible location > p (up) or     */
  370.     /* the smallest location q s.t. [q,p] is addressible (!up).    */
  371.     ptr_t GC_find_limit(p, up)
  372.     ptr_t p;
  373.     bool up;
  374.     {
  375.         static VOLATILE ptr_t result;
  376.             /* Needs to be static, since otherwise it may not be    */
  377.             /* preserved across the longjmp.  Can safely be     */
  378.             /* static since it's only called once, with the        */
  379.             /* allocation lock held.                */
  380.  
  381. #    ifdef SUNOS5SIGS
  382.       struct sigaction    act, oldact;
  383.  
  384.       act.sa_handler        = GC_fault_handler;
  385.       act.sa_flags        = SA_RESTART | SA_SIGINFO;
  386.       (void) sigemptyset(&act.sa_mask);
  387.       (void) sigaction(SIGSEGV, &act, &oldact);
  388. #    else
  389.           static handler old_segv_handler, old_bus_handler;
  390.               /* See above for static declaration.            */
  391.  
  392.           old_segv_handler = signal(SIGSEGV, GC_fault_handler);
  393. #      ifdef SIGBUS
  394.         old_bus_handler = signal(SIGBUS, GC_fault_handler);
  395. #      endif
  396. #    endif
  397.     if (setjmp(GC_jmp_buf) == 0) {
  398.         result = (ptr_t)(((word)(p))
  399.                   & ~(MIN_PAGE_SIZE-1));
  400.         for (;;) {
  401.              if (up) {
  402.             result += MIN_PAGE_SIZE;
  403.              } else {
  404.             result -= MIN_PAGE_SIZE;
  405.              }
  406.         GC_noop(*result);
  407.         }
  408.     }
  409. #       ifdef SUNOS5SIGS
  410.       (void) sigaction(SIGSEGV, &oldact, 0);
  411. #       else
  412.         (void) signal(SIGSEGV, old_segv_handler);
  413. #      ifdef SIGBUS
  414.         (void) signal(SIGBUS, old_bus_handler);
  415. #      endif
  416. #       endif
  417.      if (!up) {
  418.         result += MIN_PAGE_SIZE;
  419.      }
  420.     return(result);
  421.     }
  422. # endif
  423.  
  424.  
  425. ptr_t GC_get_stack_base()
  426. {
  427.     word dummy;
  428.     ptr_t result;
  429.  
  430. #   define STACKBOTTOM_ALIGNMENT_M1 ((word)STACK_GRAN - 1)
  431.  
  432. #   ifdef STACKBOTTOM
  433.     return(STACKBOTTOM);
  434. #   else
  435. #    ifdef HEURISTIC1
  436. #       ifdef STACK_GROWS_DOWN
  437.          result = (ptr_t)((((word)(&dummy))
  438.                         + STACKBOTTOM_ALIGNMENT_M1)
  439.                   & ~STACKBOTTOM_ALIGNMENT_M1);
  440. #       else
  441.          result = (ptr_t)(((word)(&dummy))
  442.                   & ~STACKBOTTOM_ALIGNMENT_M1);
  443. #       endif
  444. #    endif /* HEURISTIC1 */
  445. #    ifdef HEURISTIC2
  446. #        ifdef STACK_GROWS_DOWN
  447.         result = GC_find_limit((ptr_t)(&dummy), TRUE);
  448. #               ifdef HEURISTIC2_LIMIT
  449.             if (result > HEURISTIC2_LIMIT
  450.                 && (ptr_t)(&dummy) < HEURISTIC2_LIMIT) {
  451.                     result = HEURISTIC2_LIMIT;
  452.             }
  453. #            endif
  454. #        else
  455.         result = GC_find_limit((ptr_t)(&dummy), FALSE);
  456. #               ifdef HEURISTIC2_LIMIT
  457.             if (result < HEURISTIC2_LIMIT
  458.                 && (ptr_t)(&dummy) > HEURISTIC2_LIMIT) {
  459.                     result = HEURISTIC2_LIMIT;
  460.             }
  461. #            endif
  462. #        endif
  463.  
  464. #    endif /* HEURISTIC2 */
  465.         return(result);
  466. #   endif /* STACKBOTTOM */
  467. }
  468.  
  469. # endif /* ! AMIGA */
  470. # endif /* ! OS2 */
  471. # endif /* ! MSWIN32 */
  472.  
  473. /*
  474.  * Register static data segment(s) as roots.
  475.  * If more data segments are added later then they need to be registered
  476.  * add that point (as we do with SunOS dynamic loading),
  477.  * or GC_mark_roots needs to check for them (as we do with PCR).
  478.  * Called with allocator lock held.
  479.  */
  480.  
  481. # ifdef OS2
  482.  
  483. void GC_register_data_segments()
  484. {
  485.     PTIB ptib;
  486.     PPIB ppib;
  487.     HMODULE module_handle;
  488. #   define PBUFSIZ 512
  489.     UCHAR path[PBUFSIZ];
  490.     FILE * myexefile;
  491.     struct exe_hdr hdrdos;    /* MSDOS header.    */
  492.     struct e32_exe hdr386;    /* Real header for my executable */
  493.     struct o32_obj seg;    /* Currrent segment */
  494.     int nsegs;
  495.     
  496.     
  497.     if (DosGetInfoBlocks(&ptib, &ppib) != NO_ERROR) {
  498.         GC_err_printf0("DosGetInfoBlocks failed\n");
  499.         ABORT("DosGetInfoBlocks failed\n");
  500.     }
  501.     module_handle = ppib -> pib_hmte;
  502.     if (DosQueryModuleName(module_handle, PBUFSIZ, path) != NO_ERROR) {
  503.         GC_err_printf0("DosQueryModuleName failed\n");
  504.         ABORT("DosGetInfoBlocks failed\n");
  505.     }
  506.     myexefile = fopen(path, "rb");
  507.     if (myexefile == 0) {
  508.         GC_err_puts("Couldn't open executable ");
  509.         GC_err_puts(path); GC_err_puts("\n");
  510.         ABORT("Failed to open executable\n");
  511.     }
  512.     if (fread((char *)(&hdrdos), 1, sizeof hdrdos, myexefile) < sizeof hdrdos) {
  513.         GC_err_puts("Couldn't read MSDOS header from ");
  514.         GC_err_puts(path); GC_err_puts("\n");
  515.         ABORT("Couldn't read MSDOS header");
  516.     }
  517.     if (E_MAGIC(hdrdos) != EMAGIC) {
  518.         GC_err_puts("Executable has wrong DOS magic number: ");
  519.         GC_err_puts(path); GC_err_puts("\n");
  520.         ABORT("Bad DOS magic number");
  521.     }
  522.     if (fseek(myexefile, E_LFANEW(hdrdos), SEEK_SET) != 0) {
  523.         GC_err_puts("Seek to new header failed in ");
  524.         GC_err_puts(path); GC_err_puts("\n");
  525.         ABORT("Bad DOS magic number");
  526.     }
  527.     if (fread((char *)(&hdr386), 1, sizeof hdr386, myexefile) < sizeof hdr386) {
  528.         GC_err_puts("Couldn't read MSDOS header from ");
  529.         GC_err_puts(path); GC_err_puts("\n");
  530.         ABORT("Couldn't read OS/2 header");
  531.     }
  532.     if (E32_MAGIC1(hdr386) != E32MAGIC1 || E32_MAGIC2(hdr386) != E32MAGIC2) {
  533.         GC_err_puts("Executable has wrong OS/2 magic number:");
  534.         GC_err_puts(path); GC_err_puts("\n");
  535.         ABORT("Bad OS/2 magic number");
  536.     }
  537.     if ( E32_BORDER(hdr386) != E32LEBO || E32_WORDER(hdr386) != E32LEWO) {
  538.         GC_err_puts("Executable %s has wrong byte order: ");
  539.         GC_err_puts(path); GC_err_puts("\n");
  540.         ABORT("Bad byte order");
  541.     }
  542.     if ( E32_CPU(hdr386) == E32CPU286) {
  543.         GC_err_puts("GC can't handle 80286 executables: ");
  544.         GC_err_puts(path); GC_err_puts("\n");
  545.         EXIT();
  546.     }
  547. # ifdef __IBMC__                                                             /* -- NLP */
  548.  
  549. /*  This code is very specific to the .exe files as produced by the             -- NLP
  550.  *  IBM ICC compiler/linker w/respect to the code generated by the              -- NLP
  551.  *  Sather cs compiler as modified by me.                                       -- NLP */
  552.  
  553.         GC_add_roots_inner((char *)(ptib->tib_pstack),                       /* -- NLP */
  554.         (char *)(((ULONG)(ptib->tib_pstacklimit))+1-E32_STACKSIZE(hdr386))); /* -- NLP */
  555.  
  556. # else                                                                       /* -- NLP */
  557.     if (fseek(myexefile, E_LFANEW(hdrdos) + E32_OBJTAB(hdr386),
  558.               SEEK_SET) != 0) {
  559.         GC_err_puts("Seek to object table failed: ");
  560.         GC_err_puts(path); GC_err_puts("\n");
  561.         ABORT("Seek to object table failed");
  562.     }
  563.     for (nsegs = E32_OBJCNT(hdr386); nsegs > 0; nsegs--) {
  564.       int flags;
  565.       if (fread((char *)(&seg), 1, sizeof seg, myexefile) < sizeof seg) {
  566.         GC_err_puts("Couldn't read obj table entry from ");
  567.         GC_err_puts(path); GC_err_puts("\n");
  568.         ABORT("Couldn't read obj table entry");
  569.       }
  570.       flags = O32_FLAGS(seg);
  571.       if (!(flags & OBJWRITE)) continue;
  572.       if (!(flags & OBJREAD)) continue;
  573.       if (flags & OBJINVALID) {
  574.           GC_err_printf0("Object with invalid pages?\n");
  575.           continue;
  576.       } 
  577.       GC_add_roots_inner(O32_BASE(seg), O32_BASE(seg)+O32_SIZE(seg));
  578.     }
  579. # endif                                                                      /* -- NLP */
  580.     fclose(myexefile);                                                       /* -- NLP */
  581. }
  582.  
  583. # else
  584.  
  585. # ifdef MSWIN32
  586.   /* Unfortunately, we have to handle win32s very differently from NT,     */
  587.   /* Since VirtualQuery has very different semantics.  In particular,    */
  588.   /* under win32s a VirtualQuery call on an unmapped page returns an    */
  589.   /* invalid result.  Under GC_register_data_segments is a noop and    */
  590.   /* all real work is done by GC_register_dynamic_libraries.  Under    */
  591.   /* win32s, we cannot find the data segments associated with dll's.    */
  592.   /* We rgister the main data segment here.                */
  593.   bool GC_win32s = FALSE;    /* We're running under win32s.    */
  594.   
  595.   void GC_init_win32()
  596.   {
  597.       if (GetVersion() & 0x80000000) GC_win32s = TRUE;
  598.   }
  599.   
  600.   /* Return the smallest address a such that VirtualQuery        */
  601.   /* returns correct results for all addresses between a and start.    */
  602.   /* Assumes VirtualQuery returns correct information for start.    */
  603.   ptr_t GC_least_described_address(ptr_t start)
  604.   {  
  605.     MEMORY_BASIC_INFORMATION buf;
  606.     SYSTEM_INFO sysinfo;
  607.     DWORD result;
  608.     LPVOID limit;
  609.     ptr_t p;
  610.     LPVOID q;
  611.     
  612.     GetSystemInfo(&sysinfo);
  613.     limit = sysinfo.lpMinimumApplicationAddress;
  614.     p = (ptr_t)((word)start & ~(GC_get_page_size() - 1));
  615.     for (;;) {
  616.         q = (LPVOID)(p - GC_get_page_size());
  617.         if ((ptr_t)q > (ptr_t)p /* underflow */ || q < limit) break;
  618.         result = VirtualQuery(q, &buf, sizeof(buf));
  619.         if (result != sizeof(buf)) break;
  620.         p = (ptr_t)(buf.AllocationBase);
  621.     }
  622.     return(p);
  623.   }
  624.   
  625.   /* Is p the start of either the malloc heap, or of one of our */
  626.   /* heap sections?                        */
  627.   bool GC_is_heap_base (ptr_t p)
  628.   {
  629.      static ptr_t malloc_heap_pointer = 0;
  630.      register unsigned i;
  631.      register DWORD result;
  632.      
  633.      if (malloc_heap_pointer = 0) {
  634.         MEMORY_BASIC_INFORMATION buf;
  635.         result = VirtualQuery(malloc(1), &buf, sizeof(buf));
  636.         if (result != sizeof(buf)) {
  637.             ABORT("Weird VirtualQuery result");
  638.         }
  639.         malloc_heap_pointer = (ptr_t)(buf.AllocationBase);
  640.      }
  641.      if (p == malloc_heap_pointer) return(TRUE);
  642.      for (i = 0; i < GC_n_heap_bases; i++) {
  643.          if (GC_heap_bases[i] == p) return(TRUE);
  644.      }
  645.      return(FALSE);
  646.   }
  647.   
  648.   void GC_register_root_section(ptr_t static_root)
  649.   {
  650.       MEMORY_BASIC_INFORMATION buf;
  651.       SYSTEM_INFO sysinfo;
  652.       DWORD result;
  653.       DWORD protect;
  654.       LPVOID p;
  655.       char * base;
  656.       char * limit, * new_limit;
  657.     
  658.       if (!GC_win32s) return;
  659.       p = base = limit = GC_least_described_address(static_root);
  660.       GetSystemInfo(&sysinfo);
  661.       while (p < sysinfo.lpMaximumApplicationAddress) {
  662.         result = VirtualQuery(p, &buf, sizeof(buf));
  663.         if (result != sizeof(buf) || GC_is_heap_base(buf.AllocationBase)) break;
  664.         new_limit = (char *)p + buf.RegionSize;
  665.         protect = buf.Protect;
  666.         if (buf.State == MEM_COMMIT
  667.             && is_writable(protect)) {
  668.             if ((char *)p == limit) {
  669.                 limit = new_limit;
  670.             } else {
  671.                 if (base != limit) GC_add_roots_inner(base, limit);
  672.                 base = p;
  673.                 limit = new_limit;
  674.             }
  675.         }
  676.         if (p > (LPVOID)new_limit /* overflow */) break;
  677.         p = (LPVOID)new_limit;
  678.       }
  679.       if (base != limit) GC_add_roots_inner(base, limit);
  680.   }
  681.   
  682.   void GC_register_data_segments()
  683.   {
  684.       static char dummy;
  685.       
  686.       GC_register_root_section((ptr_t)(&dummy));
  687.   }
  688. # else
  689. # ifdef AMIGA
  690.  
  691.   void GC_register_data_segments()
  692.   {
  693.     extern struct WBStartup *_WBenchMsg;
  694.     struct Process    *proc;
  695.     struct CommandLineInterface *cli;
  696.     BPTR myseglist;
  697.     ULONG *data;
  698.  
  699.     if ( _WBenchMsg != 0 ) {
  700.     if ((myseglist = _WBenchMsg->sm_Segment) == 0) {
  701.         GC_err_puts("No seglist from workbench\n");
  702.         return;
  703.     }
  704.     } else {
  705.     if ((proc = (struct Process *)FindTask(0)) == 0) {
  706.         GC_err_puts("Cannot find process structure\n");
  707.         return;
  708.     }
  709.     if ((cli = BADDR(proc->pr_CLI)) == 0) {
  710.         GC_err_puts("No CLI\n");
  711.         return;
  712.     }
  713.     if ((myseglist = cli->cli_Module) == 0) {
  714.         GC_err_puts("No seglist from CLI\n");
  715.         return;
  716.     }
  717.     }
  718.  
  719.     for (data = (ULONG *)BADDR(myseglist); data != 0;
  720.          data = (ULONG *)BADDR(data[0])) {
  721.     GC_add_roots_inner((char *)&data[1], ((char *)&data[1]) + data[-1]);
  722.     }
  723.   }
  724.  
  725.  
  726. # else
  727.  
  728. # if defined(SVR4) || defined(AUX) || defined(DGUX)
  729. char * GC_SysVGetDataStart(max_page_size, etext_addr)
  730. int max_page_size;
  731. int * etext_addr;
  732. {
  733.     word text_end = ((word)(etext_addr) + sizeof(word) - 1)
  734.                 & ~(sizeof(word) - 1);
  735.         /* etext rounded to word boundary    */
  736.     word next_page = ((text_end + (word)max_page_size - 1)
  737.                   & ~((word)max_page_size - 1));
  738.     word page_offset = (text_end & ((word)max_page_size - 1));
  739.     
  740.     return((char *)(next_page + page_offset));
  741. }
  742. # endif
  743.  
  744.  
  745. void GC_register_data_segments()
  746. {
  747. #   if !defined(NEXT) && !defined(MACOS)
  748.         extern int end;
  749. #   endif
  750.  
  751. #   if !defined(PCR) && !defined(SRC_M3) && !defined(NEXT) && !defined(MACOS)
  752.       GC_add_roots_inner(DATASTART, (char *)(&end));
  753. #   endif
  754. #   if !defined(PCR) && defined(NEXT)
  755.       GC_add_roots_inner(DATASTART, (char *) get_end());
  756. #   endif
  757. #   if defined(MACOS)
  758.     {
  759. #   if defined(THINK_C)
  760.     extern void* GC_MacGetDataStart(void);
  761.     /* globals begin above stack and end at a5. */
  762.     GC_add_roots_inner((ptr_t)GC_MacGetDataStart(),
  763.                (ptr_t)LMGetCurrentA5());
  764. #   else
  765. #     if defined(__MWERKS__)
  766.     extern long __datastart, __dataend;
  767.     GC_add_roots_inner((ptr_t)&__datastart, (ptr_t)&__dataend);
  768. #     endif
  769. #   endif
  770.     }
  771. #   endif /* MACOS */
  772.  
  773.     /* Dynamic libraries are added at every collection, since they may  */
  774.     /* change.                                */
  775. }
  776.  
  777. # endif  /* ! AMIGA */
  778. # endif  /* ! MSWIN32 */
  779. # endif  /* ! OS2 */
  780.  
  781. /*
  782.  * Auxiliary routines for obtaining memory from OS.
  783.  */
  784.  
  785. # if !defined(OS2) && !defined(PCR) && !defined(AMIGA) \
  786.     && !defined(MSWIN32) && !defined(MACOS)
  787.  
  788. extern caddr_t sbrk();
  789. # ifdef __STDC__
  790. #   define SBRK_ARG_T size_t
  791. # else
  792. #   define SBRK_ARG_T int
  793. # endif
  794.  
  795. # ifdef RS6000
  796. /* The compiler seems to generate speculative reads one past the end of    */
  797. /* an allocated object.  Hence we need to make sure that the page     */
  798. /* following the last heap page is also mapped.                */
  799. ptr_t GC_unix_get_mem(bytes)
  800. word bytes;
  801. {
  802.     caddr_t cur_brk = sbrk(0);
  803.     caddr_t result;
  804.     SBRK_ARG_T lsbs = (word)cur_brk & (HBLKSIZE-1);
  805.     static caddr_t my_brk_val = 0;
  806.     
  807.     if (lsbs != 0) {
  808.         if(sbrk(HBLKSIZE - lsbs) == (caddr_t)(-1)) return(0);
  809.     }
  810.     if (cur_brk == my_brk_val) {
  811.         /* Use the extra block we allocated last time. */
  812.         result = (ptr_t)sbrk((SBRK_ARG_T)bytes);
  813.         if (result == (caddr_t)(-1)) return(0);
  814.         result -= HBLKSIZE;
  815.     } else {
  816.         result = (ptr_t)sbrk(HBLKSIZE + (SBRK_ARG_T)bytes);
  817.         if (result == (caddr_t)(-1)) return(0);
  818.     }
  819.     my_brk_val = result + bytes + HBLKSIZE;    /* Always HBLKSIZE aligned */
  820.     return((ptr_t)result);
  821. }
  822.  
  823. #else
  824. ptr_t GC_unix_get_mem(bytes)
  825. word bytes;
  826. {
  827.     caddr_t cur_brk = sbrk(0);
  828.     caddr_t result;
  829.     SBRK_ARG_T lsbs = (word)cur_brk & (HBLKSIZE-1);
  830.     
  831.     if (lsbs != 0) {
  832.         if(sbrk(HBLKSIZE - lsbs) == (caddr_t)(-1)) return(0);
  833.     }
  834.     result = sbrk((SBRK_ARG_T)bytes);
  835.     if (result == (caddr_t)(-1)) return(0);
  836.     return((ptr_t)result);
  837. }
  838. #endif
  839.  
  840. # endif
  841.  
  842. # ifdef OS2
  843.  
  844. void * os2_alloc(size_t bytes)
  845. {
  846.     void * result;
  847.  
  848.     if (DosAllocMem(&result, bytes, PAG_EXECUTE | PAG_READ |
  849.                         PAG_WRITE | PAG_COMMIT)
  850.             != NO_ERROR) {
  851.     return(0);
  852.     }
  853.     if (result == 0) return(os2_alloc(bytes));
  854.     return(result);
  855. }
  856.  
  857. # endif /* OS2 */
  858.  
  859.  
  860. # ifdef MSWIN32
  861. word GC_n_heap_bases = 0;
  862.  
  863. ptr_t GC_win32_get_mem(bytes)
  864. word bytes;
  865. {
  866.     ptr_t result;
  867.     
  868.     if (GC_win32s) {
  869.         /* VirtualAlloc doesn't like PAGE_EXECUTE_READWRITE.    */
  870.         /* There are also unconfirmed rumors of other        */
  871.         /* problems, so we dodge the issue.            */
  872.         result = (ptr_t) GlobalAlloc(0, bytes + HBLKSIZE);
  873.         result = (ptr_t)(((word)result + HBLKSIZE) & ~(HBLKSIZE-1));
  874.     } else {
  875.         result = (ptr_t) VirtualAlloc(NULL, bytes,
  876.                           MEM_COMMIT | MEM_RESERVE,
  877.                           PAGE_EXECUTE_READWRITE);
  878.     }
  879.     if (HBLKDISPL(result) != 0) ABORT("Bad VirtualAlloc result");
  880.         /* If I read the documentation correctly, this can    */
  881.         /* only happen if HBLKSIZE > 64k or not a power of 2.    */
  882.     if (GC_n_heap_bases >= MAX_HEAP_SECTS) ABORT("Too many heap sections");
  883.     GC_heap_bases[GC_n_heap_bases++] = result;
  884.     return(result);              
  885. }
  886.  
  887. # endif
  888.  
  889. /* Routine for pushing any additional roots.  In THREADS     */
  890. /* environment, this is also responsible for marking from     */
  891. /* thread stacks.  In the SRC_M3 case, it also handles        */
  892. /* global variables.                        */
  893. #ifndef THREADS
  894. void (*GC_push_other_roots)() = 0;
  895. #else /* THREADS */
  896.  
  897. # ifdef PCR
  898. PCR_ERes GC_push_thread_stack(PCR_Th_T *t, PCR_Any dummy)
  899. {
  900.     struct PCR_ThCtl_TInfoRep info;
  901.     PCR_ERes result;
  902.     
  903.     info.ti_stkLow = info.ti_stkHi = 0;
  904.     result = PCR_ThCtl_GetInfo(t, &info);
  905.     GC_push_all_stack((ptr_t)(info.ti_stkLow), (ptr_t)(info.ti_stkHi));
  906.     return(result);
  907. }
  908.  
  909. /* Push the contents of an old object. We treat this as stack    */
  910. /* data only becasue that makes it robust against mark stack    */
  911. /* overflow.                            */
  912. PCR_ERes GC_push_old_obj(void *p, size_t size, PCR_Any data)
  913. {
  914.     GC_push_all_stack((ptr_t)p, (ptr_t)p + size);
  915.     return(PCR_ERes_okay);
  916. }
  917.  
  918.  
  919. void GC_default_push_other_roots()
  920. {
  921.     /* Traverse data allocated by previous memory managers.        */
  922.     {
  923.       extern struct PCR_MM_ProcsRep * GC_old_allocator;
  924.       
  925.       if ((*(GC_old_allocator->mmp_enumerate))(PCR_Bool_false,
  926.                              GC_push_old_obj, 0)
  927.           != PCR_ERes_okay) {
  928.           ABORT("Old object enumeration failed");
  929.       }
  930.     }
  931.     /* Traverse all thread stacks. */
  932.     if (PCR_ERes_IsErr(
  933.                 PCR_ThCtl_ApplyToAllOtherThreads(GC_push_thread_stack,0))
  934.               || PCR_ERes_IsErr(GC_push_thread_stack(PCR_Th_CurrThread(), 0))) {
  935.               ABORT("Thread stack marking failed\n");
  936.     }
  937. }
  938.  
  939. # endif /* PCR */
  940.  
  941. # ifdef SRC_M3
  942.  
  943. # ifdef ALL_INTERIOR_POINTERS
  944.     --> misconfigured
  945. # endif
  946.  
  947.  
  948. extern void ThreadF__ProcessStacks();
  949.  
  950. void GC_push_thread_stack(start, stop)
  951. word start, stop;
  952. {
  953.    GC_push_all_stack((ptr_t)start, (ptr_t)stop + sizeof(word));
  954. }
  955.  
  956. /* Push routine with M3 specific calling convention. */
  957. GC_m3_push_root(dummy1, p, dummy2, dummy3)
  958. word *p;
  959. ptr_t dummy1, dummy2;
  960. int dummy3;
  961. {
  962.     word q = *p;
  963.     
  964.     if ((ptr_t)(q) >= GC_least_plausible_heap_addr
  965.      && (ptr_t)(q) < GC_greatest_plausible_heap_addr) {
  966.      GC_push_one_checked(q,FALSE);
  967.     }
  968. }
  969.  
  970. /* M3 set equivalent to RTHeap.TracedRefTypes */
  971. typedef struct { int elts[1]; }  RefTypeSet;
  972. RefTypeSet GC_TracedRefTypes = {{0x1}};
  973.  
  974. /* From finalize.c */
  975. extern void GC_push_finalizer_structures();
  976.  
  977. /* From stubborn.c: */
  978. # ifdef STUBBORN_ALLOC
  979.     extern extern_ptr_t * GC_changing_list_start;
  980. # endif
  981.  
  982.  
  983. void GC_default_push_other_roots()
  984. {
  985.     /* Use the M3 provided routine for finding static roots.    */
  986.     /* This is a bit dubious, since it presumes no C roots.    */
  987.     /* We handle the collector roots explicitly.        */
  988.        {
  989. #      ifdef STUBBORN_ALLOC
  990.            GC_push_one(GC_changing_list_start);
  991. #     endif
  992.            GC_push_finalizer_structures();
  993.            RTMain__GlobalMapProc(GC_m3_push_root, 0, GC_TracedRefTypes);
  994.        }
  995.     if (GC_words_allocd > 0) {
  996.         ThreadF__ProcessStacks(GC_push_thread_stack);
  997.     }
  998.     /* Otherwise this isn't absolutely necessary, and we have    */
  999.     /* startup ordering problems.                    */
  1000. }
  1001.  
  1002. # endif /* SRC_M3 */
  1003.  
  1004. # ifdef SOLARIS_THREADS
  1005.  
  1006. void GC_default_push_other_roots()
  1007. {
  1008.     GC_push_all_stacks();
  1009. }
  1010.  
  1011. # endif /* SOLARIS_THREADS */
  1012.  
  1013. void (*GC_push_other_roots)() = GC_default_push_other_roots;
  1014.  
  1015. #endif
  1016.  
  1017. /*
  1018.  * Routines for accessing dirty  bits on virtual pages.
  1019.  * We plan to eventaually implement four strategies for doing so:
  1020.  * DEFAULT_VDB:    A simple dummy implementation that treats every page
  1021.  *        as possibly dirty.  This makes incremental collection
  1022.  *        useless, but the implementation is still correct.
  1023.  * PCR_VDB:    Use PPCRs virtual dirty bit facility.
  1024.  * PROC_VDB:    Use the /proc facility for reading dirty bits.  Only
  1025.  *        works under some SVR4 variants.  Even then, it may be
  1026.  *        too slow to be entirely satisfactory.  Requires reading
  1027.  *        dirty bits for entire address space.  Implementations tend
  1028.  *        to assume that the client is a (slow) debugger.
  1029.  * MPROTECT_VDB:Protect pages and then catch the faults to keep track of
  1030.  *        dirtied pages.  The implementation (and implementability)
  1031.  *        is highly system dependent.  This usually fails when system
  1032.  *        calls write to a protected page.  We prevent the read system
  1033.  *        call from doing so.  It is the clients responsibility to
  1034.  *        make sure that other system calls are similarly protected
  1035.  *        or write only to the stack.
  1036.  */
  1037.  
  1038. bool GC_dirty_maintained;
  1039.  
  1040. # ifdef DEFAULT_VDB
  1041.  
  1042. /* All of the following assume the allocation lock is held, and    */
  1043. /* signals are disabled.                    */
  1044.  
  1045. /* The client asserts that unallocated pages in the heap are never    */
  1046. /* written.                                */
  1047.  
  1048. /* Initialize virtual dirty bit implementation.            */
  1049. void GC_dirty_init()
  1050. {
  1051. }
  1052.  
  1053. /* Retrieve system dirty bits for heap to a local buffer.    */
  1054. /* Restore the systems notion of which pages are dirty.        */
  1055. void GC_read_dirty()
  1056. {}
  1057.  
  1058. /* Is the HBLKSIZE sized page at h marked dirty in the local buffer?    */
  1059. /* If the actual page size is different, this returns TRUE if any    */
  1060. /* of the pages overlapping h are dirty.  This routine may err on the    */
  1061. /* side of labelling pages as dirty (and this implementation does).    */
  1062. /*ARGSUSED*/
  1063. bool GC_page_was_dirty(h)
  1064. struct hblk *h;
  1065. {
  1066.     return(TRUE);
  1067. }
  1068.  
  1069. /*
  1070.  * The following two routines are typically less crucial.  They matter
  1071.  * most with large dynamic libraries, or if we can't accurately identify
  1072.  * stacks, e.g. under Solaris 2.X.  Otherwise the following default
  1073.  * versions are adequate.
  1074.  */
  1075.  
  1076. /* Could any valid GC heap pointer ever have been written to this page?    */
  1077. /*ARGSUSED*/
  1078. bool GC_page_was_ever_dirty(h)
  1079. struct hblk *h;
  1080. {
  1081.     return(TRUE);
  1082. }
  1083.  
  1084. /* Reset the n pages starting at h to "was never dirty" status.    */
  1085. void GC_is_fresh(h, n)
  1086. struct hblk *h;
  1087. word n;
  1088. {
  1089. }
  1090.  
  1091. /* A call hints that h is about to be written.    */
  1092. /* May speed up some dirty bit implementations.    */
  1093. /*ARGSUSED*/
  1094. void GC_write_hint(h)
  1095. struct hblk *h;
  1096. {
  1097. }
  1098.  
  1099. # endif /* DEFAULT_VDB */
  1100.  
  1101.  
  1102. # ifdef MPROTECT_VDB
  1103.  
  1104. /*
  1105.  * See DEFAULT_VDB for interface descriptions.
  1106.  */
  1107.  
  1108. /*
  1109.  * This implementation maintains dirty bits itself by catching write
  1110.  * faults and keeping track of them.  We assume nobody else catches
  1111.  * SIGBUS or SIGSEGV.  We assume no write faults occur in system calls
  1112.  * except as a result of a read system call.  This means clients must
  1113.  * either ensure that system calls do not touch the heap, or must
  1114.  * provide their own wrappers analogous to the one for read.
  1115.  * We assume the page size is a multiple of HBLKSIZE.
  1116.  * This implementation is currently SunOS 4.X and IRIX 5.X specific, though we
  1117.  * tried to use portable code where easily possible.  It is known
  1118.  * not to work under a number of other systems.
  1119.  */
  1120.  
  1121. # include <sys/mman.h>
  1122. # include <signal.h>
  1123. # include <sys/syscall.h>
  1124.  
  1125. VOLATILE page_hash_table GC_dirty_pages;
  1126.                 /* Pages dirtied since last GC_read_dirty. */
  1127.  
  1128. word GC_page_size;
  1129.  
  1130. bool GC_just_outside_heap(addr)
  1131. word addr;
  1132. {
  1133.     register int i;
  1134.     register word start;
  1135.     register word end;
  1136.     word mask = GC_page_size-1;
  1137.     
  1138.     for (i = 0; i < GC_n_heap_sects; i++) {
  1139.         start = (word) GC_heap_sects[i].hs_start;
  1140.         end = start + (word)GC_heap_sects[i].hs_bytes;
  1141.         if (addr < start && addr >= (start & ~mask)
  1142.             || addr >= end && addr < ((end + mask) & ~mask)) {
  1143.             return(TRUE);
  1144.         }
  1145.     }
  1146.     return(FALSE);
  1147. }
  1148.  
  1149. #if defined(SUNOS4) || defined(FREEBSD)
  1150.     typedef void (* SIG_PF)();
  1151. #endif
  1152. #if defined(SUNOS5SIGS) || defined(ALPHA) /* OSF1 */
  1153.     typedef void (* SIG_PF)(int);
  1154. #endif
  1155.  
  1156. #if defined(IRIX5) || defined(ALPHA) /* OSF1 */
  1157.     typedef void (* REAL_SIG_PF)(int, int, struct sigcontext *);
  1158. #endif
  1159. #if defined(SUNOS5SIGS)
  1160.     typedef void (* REAL_SIG_PF)(int, struct siginfo *, void *);
  1161. #endif
  1162.  
  1163. SIG_PF GC_old_bus_handler;
  1164. SIG_PF GC_old_segv_handler;
  1165.  
  1166. /*ARGSUSED*/
  1167. # if defined (SUNOS4) || defined(FREEBSD)
  1168.     void GC_write_fault_handler(sig, code, scp, addr)
  1169.     int sig, code;
  1170.     struct sigcontext *scp;
  1171.     char * addr;
  1172. #   ifdef SUNOS4
  1173. #     define SIG_OK (sig == SIGSEGV || sig == SIGBUS)
  1174. #     define CODE_OK (FC_CODE(code) == FC_PROT \
  1175.                       || (FC_CODE(code) == FC_OBJERR \
  1176.                          && FC_ERRNO(code) == FC_PROT))
  1177. #   endif
  1178. #   ifdef FREEBSD
  1179. #     define SIG_OK (sig == SIGBUS)
  1180. #     define CODE_OK (code == BUS_PAGE_FAULT)
  1181. #   endif
  1182. # endif
  1183. # if defined(IRIX5) || defined(ALPHA) /* OSF1 */
  1184. #   include <errno.h>
  1185.     void GC_write_fault_handler(int sig, int code, struct sigcontext *scp)
  1186. #   define SIG_OK (sig == SIGSEGV)
  1187. #   ifdef ALPHA
  1188. #     define CODE_OK (code == 2 /* experimentally determined */)
  1189. #   endif
  1190. #   ifdef IRIX5
  1191. #     define CODE_OK (code == EACCES)
  1192. #   endif
  1193. # endif
  1194. # if defined(SUNOS5SIGS)
  1195.     void GC_write_fault_handler(int sig, struct siginfo *scp, void * context)
  1196. #   define SIG_OK (sig == SIGSEGV)
  1197. #   define CODE_OK (scp -> si_code == SEGV_ACCERR)
  1198. # endif
  1199. {
  1200.     register int i;
  1201. #   ifdef IRIX5
  1202.     char * addr = (char *) (scp -> sc_badvaddr);
  1203. #   endif
  1204. #   ifdef ALPHA
  1205.     char * addr = (char *) (scp -> sc_traparg_a0);
  1206. #   endif
  1207. #   ifdef SUNOS5SIGS
  1208.     char * addr = (char *) (scp -> si_addr);
  1209. #   endif
  1210.     
  1211.     if (SIG_OK && CODE_OK) {
  1212.         register struct hblk * h =
  1213.                 (struct hblk *)((word)addr & ~(GC_page_size-1));
  1214.         
  1215.         if (HDR(addr) == 0 && !GC_just_outside_heap((word)addr)) {
  1216.             SIG_PF old_handler;
  1217.             
  1218.             if (sig == SIGSEGV) {
  1219.                 old_handler = GC_old_segv_handler;
  1220.             } else {
  1221.                 old_handler = GC_old_bus_handler;
  1222.             }
  1223.             if (old_handler == SIG_DFL) {
  1224.                 ABORT("Unexpected bus error or segmentation fault");
  1225.             } else {
  1226. #        if defined (SUNOS4) || defined(FREEBSD)
  1227.           (*old_handler) (sig, code, scp, addr);
  1228. #        else
  1229. #          if defined (SUNOS5SIGS)
  1230.             (*(REAL_SIG_PF)old_handler) (sig, scp, context);
  1231. #          else
  1232.             (*(REAL_SIG_PF)old_handler) (sig, code, scp);
  1233. #          endif
  1234. #        endif
  1235.         return;
  1236.             }
  1237.         }
  1238.         for (i = 0; i < divHBLKSZ(GC_page_size); i++) {
  1239.             register int index = PHT_HASH(h+i);
  1240.             
  1241.             set_pht_entry_from_index(GC_dirty_pages, index);
  1242.         }
  1243.         if (mprotect((caddr_t)h, (int)GC_page_size,
  1244.             PROT_WRITE | PROT_READ | PROT_EXEC) < 0) {
  1245.             ABORT("mprotect failed in handler");
  1246.         }
  1247. #    if defined(IRIX5) || defined(ALPHA)
  1248.         /* IRIX resets the signal handler each time. */
  1249.         signal(SIGSEGV, (SIG_PF) GC_write_fault_handler);
  1250. #    endif
  1251.         /* The write may not take place before dirty bits are read.    */
  1252.         /* But then we'll fault again ...                */
  1253.         return;
  1254.     }
  1255.  
  1256.     ABORT("Unexpected bus error or segmentation fault");
  1257. }
  1258.  
  1259. void GC_write_hint(h)
  1260. struct hblk *h;
  1261. {
  1262.     register struct hblk * h_trunc =
  1263.                 (struct hblk *)((word)h & ~(GC_page_size-1));
  1264.     register int i;
  1265.     register bool found_clean = FALSE;
  1266.     
  1267.     for (i = 0; i < divHBLKSZ(GC_page_size); i++) {
  1268.         register int index = PHT_HASH(h_trunc+i);
  1269.             
  1270.         if (!get_pht_entry_from_index(GC_dirty_pages, index)) {
  1271.             found_clean = TRUE;
  1272.             set_pht_entry_from_index(GC_dirty_pages, index);
  1273.         }
  1274.     }
  1275.     if (found_clean) {
  1276.        if (mprotect((caddr_t)h_trunc, (int)GC_page_size,
  1277.             PROT_WRITE | PROT_READ | PROT_EXEC) < 0) {
  1278.             ABORT("mprotect failed in GC_write_hint");
  1279.         }
  1280.     }
  1281. }
  1282.  
  1283. #if defined(SUNOS5) || defined(DRSNX)
  1284. #include <unistd.h>
  1285. int
  1286. GC_getpagesize()
  1287. {
  1288.     return sysconf(_SC_PAGESIZE);
  1289. }
  1290. #else
  1291. # define GC_getpagesize() getpagesize()
  1292. #endif
  1293.                  
  1294. void GC_dirty_init()
  1295. {
  1296. #if defined(SUNOS5SIGS)
  1297.     struct sigaction    act, oldact;
  1298.     act.sa_sigaction    = GC_write_fault_handler;
  1299.     act.sa_flags    = SA_RESTART | SA_SIGINFO;
  1300.     (void)sigemptyset(&act.sa_mask); 
  1301. #endif
  1302.     GC_dirty_maintained = TRUE;
  1303.     GC_page_size = GC_getpagesize();
  1304.     if (GC_page_size % HBLKSIZE != 0) {
  1305.         GC_err_printf0("Page size not multiple of HBLKSIZE\n");
  1306.         ABORT("Page size not multiple of HBLKSIZE");
  1307.     }
  1308. #   if defined(SUNOS4) || defined(FREEBSD)
  1309.       GC_old_bus_handler = signal(SIGBUS, GC_write_fault_handler);
  1310.       if (GC_old_bus_handler == SIG_IGN) {
  1311.         GC_err_printf0("Previously ignored bus error!?");
  1312.         GC_old_bus_handler == SIG_DFL;
  1313.       }
  1314.       if (GC_old_bus_handler != SIG_DFL) {
  1315. #    ifdef PRINTSTATS
  1316.           GC_err_printf0("Replaced other SIGBUS handler\n");
  1317. #    endif
  1318.       }
  1319. #   endif
  1320. #   if defined(IRIX5) || defined(ALPHA) || defined(SUNOS4)
  1321.       GC_old_segv_handler = signal(SIGSEGV, (SIG_PF)GC_write_fault_handler);
  1322.       if (GC_old_segv_handler == SIG_IGN) {
  1323.         GC_err_printf0("Previously ignored segmentation violation!?");
  1324.         GC_old_segv_handler == SIG_DFL;
  1325.       }
  1326.       if (GC_old_segv_handler != SIG_DFL) {
  1327. #    ifdef PRINTSTATS
  1328.           GC_err_printf0("Replaced other SIGSEGV handler\n");
  1329. #    endif
  1330.       }
  1331. #   endif
  1332. #   if defined(SUNOS5SIGS)
  1333.       sigaction(SIGSEGV, &act, &oldact);
  1334.       if (oldact.sa_flags & SA_SIGINFO) {
  1335.           GC_old_segv_handler = (SIG_PF)(oldact.sa_sigaction);
  1336.       } else {
  1337.           GC_old_segv_handler = oldact.sa_handler;
  1338.       }
  1339.       if (GC_old_segv_handler == SIG_IGN) {
  1340.          GC_err_printf0("Previously ignored segmentation violation!?");
  1341.          GC_old_segv_handler == SIG_DFL;
  1342.       }
  1343.       if (GC_old_segv_handler != SIG_DFL) {
  1344. #       ifdef PRINTSTATS
  1345.       GC_err_printf0("Replaced other SIGSEGV handler\n");
  1346. #       endif
  1347.       }
  1348. #    endif
  1349. }
  1350.  
  1351.  
  1352.  
  1353. void GC_protect_heap()
  1354. {
  1355.     word ps = GC_page_size;
  1356.     word pmask = (ps-1);
  1357.     ptr_t start;
  1358.     word offset;
  1359.     word len;
  1360.     int i;
  1361.     
  1362.     for (i = 0; i < GC_n_heap_sects; i++) {
  1363.         offset = (word)(GC_heap_sects[i].hs_start) & pmask;
  1364.         start = GC_heap_sects[i].hs_start - offset;
  1365.         len = GC_heap_sects[i].hs_bytes + offset;
  1366.         len += ps-1; len &= ~pmask;
  1367.         if (mprotect((caddr_t)start, (int)len, PROT_READ | PROT_EXEC) < 0) {
  1368.             ABORT("mprotect failed");
  1369.         }
  1370.     }
  1371. }
  1372.  
  1373. # ifdef THREADS
  1374. --> The following is broken.  We can lose dirty bits.  We would need
  1375. --> the signal handler to cooperate, as in PCR.
  1376. # endif
  1377.  
  1378. void GC_read_dirty()
  1379. {
  1380.     BCOPY((word *)GC_dirty_pages, GC_grungy_pages,
  1381.           (sizeof GC_dirty_pages));
  1382.     BZERO((word *)GC_dirty_pages, (sizeof GC_dirty_pages));
  1383.     GC_protect_heap();
  1384. }
  1385.  
  1386. bool GC_page_was_dirty(h)
  1387. struct hblk * h;
  1388. {
  1389.     register word index = PHT_HASH(h);
  1390.     
  1391.     return(HDR(h) == 0 || get_pht_entry_from_index(GC_grungy_pages, index));
  1392. }
  1393.  
  1394. /*
  1395.  * If this code needed to be thread-safe, the following would need to
  1396.  * acquire and release the allocation lock.  This is tricky, since e.g.
  1397.  * the cord package issues a read while it already holds the allocation lock.
  1398.  */
  1399.  
  1400. # ifdef THREADS
  1401.     --> fix this
  1402. # endif
  1403. void GC_begin_syscall()
  1404. {
  1405. }
  1406.  
  1407. void GC_end_syscall()
  1408. {
  1409. }
  1410.  
  1411. void GC_unprotect_range(addr, len)
  1412. ptr_t addr;
  1413. word len;
  1414. {
  1415.     struct hblk * start_block;
  1416.     struct hblk * end_block;
  1417.     register struct hblk *h;
  1418.     ptr_t obj_start;
  1419.     
  1420.     if (!GC_incremental) return;
  1421.     obj_start = GC_base(addr);
  1422.     if (obj_start == 0) return;
  1423.     if (GC_base(addr + len - 1) != obj_start) {
  1424.         ABORT("GC_unprotect_range(range bigger than object)");
  1425.     }
  1426.     start_block = (struct hblk *)((word)addr & ~(GC_page_size - 1));
  1427.     end_block = (struct hblk *)((word)(addr + len - 1) & ~(GC_page_size - 1));
  1428.     end_block += GC_page_size/HBLKSIZE - 1;
  1429.     for (h = start_block; h <= end_block; h++) {
  1430.         register word index = PHT_HASH(h);
  1431.         
  1432.         set_pht_entry_from_index(GC_dirty_pages, index);
  1433.     }
  1434.     if (mprotect((caddr_t)start_block,
  1435.                  (int)((ptr_t)end_block - (ptr_t)start_block)
  1436.                  + HBLKSIZE,
  1437.                  PROT_WRITE | PROT_READ | PROT_EXEC) < 0) {
  1438.         ABORT("mprotect failed in GC_unprotect_range:");
  1439.     }
  1440. }
  1441.  
  1442. /* Replacement for UNIX system call.     */
  1443. /* Other calls that write to the heap     */
  1444. /* should be handled similarly.         */
  1445. # ifndef LINT
  1446.   int read(fd, buf, nbyte)
  1447. # else
  1448.   int GC_read(fd, buf, nbyte)
  1449. # endif
  1450. int fd;
  1451. char *buf;
  1452. int nbyte;
  1453. {
  1454.     int result;
  1455.     
  1456.     GC_begin_syscall();
  1457.     GC_unprotect_range(buf, (word)nbyte);
  1458. #   ifdef IRIX5
  1459.     /* Indirect system call exists, but is undocumented, and    */
  1460.     /* always seems to return EINVAL.  There seems to be no        */
  1461.     /* general way to wrap system calls, since the system call    */
  1462.     /* convention appears to require an immediate argument for    */
  1463.     /* the system call number, and building the required code    */
  1464.     /* in the data segment also seems dangerous.  We can fake it    */
  1465.     /* for read; anything else is up to the client.            */
  1466.     {
  1467.         struct iovec iov;
  1468.  
  1469.         iov.iov_base = buf;
  1470.         iov.iov_len = nbyte;
  1471.         result = readv(fd, &iov, 1);
  1472.     }
  1473. #   else
  1474.         result = syscall(SYS_read, fd, buf, nbyte);
  1475. #   endif
  1476.     GC_end_syscall();
  1477.     return(result);
  1478. }
  1479.  
  1480. /*ARGSUSED*/
  1481. bool GC_page_was_ever_dirty(h)
  1482. struct hblk *h;
  1483. {
  1484.     return(TRUE);
  1485. }
  1486.  
  1487. /* Reset the n pages starting at h to "was never dirty" status.    */
  1488. /*ARGSUSED*/
  1489. void GC_is_fresh(h, n)
  1490. struct hblk *h;
  1491. word n;
  1492. {
  1493. }
  1494.  
  1495. # endif /* MPROTECT_VDB */
  1496.  
  1497. # ifdef PROC_VDB
  1498.  
  1499. /*
  1500.  * See DEFAULT_VDB for interface descriptions.
  1501.  */
  1502.  
  1503. /*
  1504.  * This implementaion assumes a Solaris 2.X like /proc pseudo-file-system
  1505.  * from which we can read page modified bits.  This facility is far from
  1506.  * optimal (e.g. we would like to get the info for only some of the
  1507.  * address space), but it avoids intercepting system calls.
  1508.  */
  1509.  
  1510. #include <sys/types.h>
  1511. #include <sys/signal.h>
  1512. #include <sys/fault.h>
  1513. #include <sys/syscall.h>
  1514. #include <sys/procfs.h>
  1515. #include <sys/stat.h>
  1516. #include <fcntl.h>
  1517.  
  1518. #define INITIAL_BUF_SZ 4096
  1519. word GC_proc_buf_size = INITIAL_BUF_SZ;
  1520. char *GC_proc_buf;
  1521.  
  1522. page_hash_table GC_written_pages = { 0 };    /* Pages ever dirtied    */
  1523.  
  1524. #ifdef SOLARIS_THREADS
  1525. /* We don't have exact sp values for threads.  So we count on    */
  1526. /* occasionally declaring stack pages to be fresh.  Thus we     */
  1527. /* need a real implementation of GC_is_fresh.  We can't clear    */
  1528. /* entries in GC_written_pages, since that would declare all    */
  1529. /* pages with the given hash address to be fresh.        */
  1530. #   define MAX_FRESH_PAGES 8*1024    /* Must be power of 2 */
  1531.     struct hblk ** GC_fresh_pages;    /* A direct mapped cache.    */
  1532.                         /* Collisions are dropped.    */
  1533.  
  1534. #   define FRESH_PAGE_SLOT(h) (divHBLKSZ((word)(h)) & (MAX_FRESH_PAGES-1))
  1535. #   define ADD_FRESH_PAGE(h) \
  1536.     GC_fresh_pages[FRESH_PAGE_SLOT(h)] = (h)
  1537. #   define PAGE_IS_FRESH(h) \
  1538.     (GC_fresh_pages[FRESH_PAGE_SLOT(h)] == (h) && (h) != 0)
  1539. #endif
  1540.  
  1541. /* Add all pages in pht2 to pht1 */
  1542. void GC_or_pages(pht1, pht2)
  1543. page_hash_table pht1, pht2;
  1544. {
  1545.     register int i;
  1546.     
  1547.     for (i = 0; i < PHT_SIZE; i++) pht1[i] |= pht2[i];
  1548. }
  1549.  
  1550. int GC_proc_fd;
  1551.  
  1552. void GC_dirty_init()
  1553. {
  1554.     int fd;
  1555.     char buf[30];
  1556.  
  1557.     GC_dirty_maintained = TRUE;
  1558.     if (GC_words_allocd != 0 || GC_words_allocd_before_gc != 0) {
  1559.         register int i;
  1560.     
  1561.         for (i = 0; i < PHT_SIZE; i++) GC_written_pages[i] = (word)(-1);
  1562. #       ifdef PRINTSTATS
  1563.         GC_printf1("Allocated words:%lu:all pages may have been written\n",
  1564.                    (unsigned long)
  1565.                           (GC_words_allocd + GC_words_allocd_before_gc));
  1566. #    endif       
  1567.     }
  1568.     sprintf(buf, "/proc/%d", getpid());
  1569.     fd = open(buf, O_RDONLY);
  1570.     if (fd < 0) {
  1571.         ABORT("/proc open failed");
  1572.     }
  1573.     GC_proc_fd = syscall(SYS_ioctl, fd, PIOCOPENPD, 0);
  1574.     if (GC_proc_fd < 0) {
  1575.         ABORT("/proc ioctl failed");
  1576.     }
  1577.     GC_proc_buf = GC_scratch_alloc(GC_proc_buf_size);
  1578. #   ifdef SOLARIS_THREADS
  1579.     GC_fresh_pages = (struct hblk **)
  1580.       GC_scratch_alloc(MAX_FRESH_PAGES * sizeof (struct hblk *));
  1581.     if (GC_fresh_pages == 0) {
  1582.         GC_err_printf0("No space for fresh pages\n");
  1583.         EXIT();
  1584.     }
  1585.     BZERO(GC_fresh_pages, MAX_FRESH_PAGES * sizeof (struct hblk *));
  1586. #   endif
  1587. }
  1588.  
  1589. /* Ignore write hints. They don't help us here.    */
  1590. /*ARGSUSED*/
  1591. void GC_write_hint(h)
  1592. struct hblk *h;
  1593. {
  1594. }
  1595.  
  1596. #ifdef SOLARIS_THREADS
  1597. #   define READ(fd,buf,nbytes) syscall(SYS_read, fd, buf, nbytes)
  1598. #else
  1599. #   define READ(fd,buf,nbytes) read(fd, buf, nbytes)
  1600. #endif
  1601.  
  1602. void GC_read_dirty()
  1603. {
  1604.     unsigned long ps, np;
  1605.     int nmaps;
  1606.     ptr_t vaddr;
  1607.     struct prasmap * map;
  1608.     char * bufp;
  1609.     ptr_t current_addr, limit;
  1610.     int i;
  1611. int dummy;
  1612.  
  1613.     BZERO(GC_grungy_pages, (sizeof GC_grungy_pages));
  1614.     
  1615.     bufp = GC_proc_buf;
  1616.     if (READ(GC_proc_fd, bufp, GC_proc_buf_size) <= 0) {
  1617. #    ifdef PRINTSTATS
  1618.             GC_printf1("/proc read failed: GC_proc_buf_size = %lu\n",
  1619.                        GC_proc_buf_size);
  1620. #    endif       
  1621.         {
  1622.             /* Retry with larger buffer. */
  1623.             word new_size = 2 * GC_proc_buf_size;
  1624.             char * new_buf = GC_scratch_alloc(new_size);
  1625.             
  1626.             if (new_buf != 0) {
  1627.                 GC_proc_buf = bufp = new_buf;
  1628.                 GC_proc_buf_size = new_size;
  1629.             }
  1630.             if (syscall(SYS_read, GC_proc_fd, bufp, GC_proc_buf_size) <= 0) {
  1631.                 WARN("Insufficient space for /proc read\n");
  1632.                 /* Punt:    */
  1633.             memset(GC_grungy_pages, 0xff, sizeof (page_hash_table));
  1634. #        ifdef SOLARIS_THREADS
  1635.             BZERO(GC_fresh_pages,
  1636.                   MAX_FRESH_PAGES * sizeof (struct hblk *)); 
  1637. #        endif
  1638.         return;
  1639.             }
  1640.         }
  1641.     }
  1642.     /* Copy dirty bits into GC_grungy_pages */
  1643.         nmaps = ((struct prpageheader *)bufp) -> pr_nmap;
  1644.     /* printf( "nmaps = %d, PG_REFERENCED = %d, PG_MODIFIED = %d\n",
  1645.              nmaps, PG_REFERENCED, PG_MODIFIED); */
  1646.     bufp = bufp + sizeof(struct prpageheader);
  1647.     for (i = 0; i < nmaps; i++) {
  1648.         map = (struct prasmap *)bufp;
  1649.         vaddr = (ptr_t)(map -> pr_vaddr);
  1650.         ps = map -> pr_pagesize;
  1651.         np = map -> pr_npage;
  1652.         /* printf("vaddr = 0x%X, ps = 0x%X, np = 0x%X\n", vaddr, ps, np); */
  1653.         limit = vaddr + ps * np;
  1654.         bufp += sizeof (struct prasmap);
  1655.         for (current_addr = vaddr;
  1656.              current_addr < limit; current_addr += ps){
  1657.             if ((*bufp++) & PG_MODIFIED) {
  1658.                 register struct hblk * h = (struct hblk *) current_addr;
  1659.                 
  1660.                 while ((ptr_t)h < current_addr + ps) {
  1661.                     register word index = PHT_HASH(h);
  1662.                     
  1663.                     set_pht_entry_from_index(GC_grungy_pages, index);
  1664. #            ifdef SOLARIS_THREADS
  1665.               {
  1666.                 register int slot = FRESH_PAGE_SLOT(h);
  1667.                 
  1668.                 if (GC_fresh_pages[slot] == h) {
  1669.                     GC_fresh_pages[slot] = 0;
  1670.                 }
  1671.               }
  1672. #            endif
  1673.                     h++;
  1674.                 }
  1675.             }
  1676.         }
  1677.         bufp += sizeof(long) - 1;
  1678.         bufp = (char *)((unsigned long)bufp & ~(sizeof(long)-1));
  1679.     }
  1680.     /* Update GC_written_pages. */
  1681.         GC_or_pages(GC_written_pages, GC_grungy_pages);
  1682. #   ifdef SOLARIS_THREADS
  1683.       /* Make sure that old stacks are considered completely clean    */
  1684.       /* unless written again.                        */
  1685.     GC_old_stacks_are_fresh();
  1686. #   endif
  1687. }
  1688.  
  1689. #undef READ
  1690.  
  1691. bool GC_page_was_dirty(h)
  1692. struct hblk *h;
  1693. {
  1694.     register word index = PHT_HASH(h);
  1695.     register bool result;
  1696.     
  1697.     result = get_pht_entry_from_index(GC_grungy_pages, index);
  1698. #   ifdef SOLARIS_THREADS
  1699.     if (result && PAGE_IS_FRESH(h)) result = FALSE;
  1700.     /* This happens only if page was declared fresh since    */
  1701.     /* the read_dirty call, e.g. because it's in an unused  */
  1702.     /* thread stack.  It's OK to treat it as clean, in    */
  1703.     /* that case.  And it's consistent with         */
  1704.     /* GC_page_was_ever_dirty.                */
  1705. #   endif
  1706.     return(result);
  1707. }
  1708.  
  1709. bool GC_page_was_ever_dirty(h)
  1710. struct hblk *h;
  1711. {
  1712.     register word index = PHT_HASH(h);
  1713.     register bool result;
  1714.     
  1715.     result = get_pht_entry_from_index(GC_written_pages, index);
  1716. #   ifdef SOLARIS_THREADS
  1717.     if (result && PAGE_IS_FRESH(h)) result = FALSE;
  1718. #   endif
  1719.     return(result);
  1720. }
  1721.  
  1722. /* Caller holds allocation lock.    */
  1723. void GC_is_fresh(h, n)
  1724. struct hblk *h;
  1725. word n;
  1726. {
  1727.  
  1728.     register word index;
  1729.     
  1730. #   ifdef SOLARIS_THREADS
  1731.       register word i;
  1732.       
  1733.       if (GC_fresh_pages != 0) {
  1734.         for (i = 0; i < n; i++) {
  1735.           ADD_FRESH_PAGE(h + i);
  1736.         }
  1737.       }
  1738. #   endif
  1739. }
  1740.  
  1741. # endif /* PROC_VDB */
  1742.  
  1743.  
  1744. # ifdef PCR_VDB
  1745.  
  1746. # include "vd/PCR_VD.h"
  1747.  
  1748. # define NPAGES (32*1024)    /* 128 MB */
  1749.  
  1750. PCR_VD_DB  GC_grungy_bits[NPAGES];
  1751.  
  1752. ptr_t GC_vd_base;    /* Address corresponding to GC_grungy_bits[0]    */
  1753.             /* HBLKSIZE aligned.                */
  1754.  
  1755. void GC_dirty_init()
  1756. {
  1757.     GC_dirty_maintained = TRUE;
  1758.     /* For the time being, we assume the heap generally grows up */
  1759.     GC_vd_base = GC_heap_sects[0].hs_start;
  1760.     if (GC_vd_base == 0) {
  1761.        ABORT("Bad initial heap segment");
  1762.     }
  1763.     if (PCR_VD_Start(HBLKSIZE, GC_vd_base, NPAGES*HBLKSIZE)
  1764.     != PCR_ERes_okay) {
  1765.     ABORT("dirty bit initialization failed");
  1766.     }
  1767. }
  1768.  
  1769. void GC_read_dirty()
  1770. {
  1771.     /* lazily enable dirty bits on newly added heap sects */
  1772.     {
  1773.         static int onhs = 0;
  1774.         int nhs = GC_n_heap_sects;
  1775.         for( ; onhs < nhs; onhs++ ) {
  1776.             PCR_VD_WriteProtectEnable(
  1777.                     GC_heap_sects[onhs].hs_start,
  1778.                     GC_heap_sects[onhs].hs_bytes );
  1779.         }
  1780.     }
  1781.  
  1782.  
  1783.     if (PCR_VD_Clear(GC_vd_base, NPAGES*HBLKSIZE, GC_grungy_bits)
  1784.         != PCR_ERes_okay) {
  1785.     ABORT("dirty bit read failed");
  1786.     }
  1787. }
  1788.  
  1789. bool GC_page_was_dirty(h)
  1790. struct hblk *h;
  1791. {
  1792.     if((ptr_t)h < GC_vd_base || (ptr_t)h >= GC_vd_base + NPAGES*HBLKSIZE) {
  1793.     return(TRUE);
  1794.     }
  1795.     return(GC_grungy_bits[h - (struct hblk *)GC_vd_base] & PCR_VD_DB_dirtyBit);
  1796. }
  1797.  
  1798. /*ARGSUSED*/
  1799. void GC_write_hint(h)
  1800. struct hblk *h;
  1801. {
  1802.     PCR_VD_WriteProtectDisable(h, HBLKSIZE);
  1803.     PCR_VD_WriteProtectEnable(h, HBLKSIZE);
  1804. }
  1805.  
  1806. # endif /* PCR_VDB */
  1807.  
  1808. /*
  1809.  * Call stack save code for debugging.
  1810.  * Should probably be in mach_dep.c, but that requires reorganization.
  1811.  */
  1812. #if defined(SPARC)
  1813. #   if defined(SUNOS4)
  1814. #     include <machine/frame.h>
  1815. #   else
  1816. #     if defined (DRSNX)
  1817. #    include <sys/sparc/frame.h>
  1818. #     else
  1819. #       include <sys/frame.h>
  1820. #     endif
  1821. #   endif
  1822. #   if NARGS > 6
  1823.     --> We only know how to to get the first 6 arguments
  1824. #   endif
  1825.  
  1826. /* Fill in the pc and argument information for up to NFRAMES of my    */
  1827. /* callers.  Ignore my frame and my callers frame.            */
  1828. void GC_save_callers (info) 
  1829. struct callinfo info[NFRAMES];
  1830. {
  1831.   struct frame *frame;
  1832.   struct frame *fp;
  1833.   int nframes = 0;
  1834.   word GC_save_regs_in_stack();
  1835.  
  1836.   frame = (struct frame *) GC_save_regs_in_stack ();
  1837.   
  1838.   for (fp = frame -> fr_savfp; fp != 0 && nframes < NFRAMES;
  1839.        fp = fp -> fr_savfp, nframes++) {
  1840.       register int i;
  1841.       
  1842.       info[nframes].ci_pc = fp->fr_savpc;
  1843.       for (i = 0; i < NARGS; i++) {
  1844.     info[nframes].ci_arg[i] = ~(fp->fr_arg[i]);
  1845.       }
  1846.   }
  1847.   if (nframes < NFRAMES) info[nframes].ci_pc = 0;
  1848. }
  1849.  
  1850. #endif /* SPARC */
  1851.  
  1852. #ifdef SAVE_CALL_CHAIN
  1853.  
  1854. void GC_print_callers (info)
  1855. struct callinfo info[NFRAMES];
  1856. {
  1857.     register int i,j;
  1858.     
  1859.     GC_err_printf0("\tCall chain at allocation:\n");
  1860.     for (i = 0; i < NFRAMES; i++) {
  1861.          if (info[i].ci_pc == 0) break;
  1862.          GC_err_printf1("\t##PC##= 0x%X\n\t\targs: ", info[i].ci_pc);
  1863.          for (j = 0; j < NARGS; j++) {
  1864.              if (j != 0) GC_err_printf0(", ");
  1865.              GC_err_printf2("%d (0x%X)", ~(info[i].ci_arg[j]),
  1866.                              ~(info[i].ci_arg[j]));
  1867.          }
  1868.          GC_err_printf0("\n");
  1869.     }
  1870. }
  1871.  
  1872. #endif /* SAVE_CALL_CHAIN */
  1873.  
  1874.  
  1875.  
  1876.  
  1877.  
  1878.