2 * GENerational Conservative Garbage Collector for SBCL
6 * This software is part of the SBCL system. See the README file for
9 * This software is derived from the CMU CL system, which was
10 * written at Carnegie Mellon University and released into the
11 * public domain. The software is in the public domain and is
12 * provided with absolutely no warranty. See the COPYING and CREDITS
13 * files for more information.
17 * For a review of garbage collection techniques (e.g. generational
18 * GC) and terminology (e.g. "scavenging") see Paul R. Wilson,
19 * "Uniprocessor Garbage Collection Techniques". As of 20000618, this
20 * had been accepted for _ACM Computing Surveys_ and was available
21 * as a PostScript preprint through
22 * <http://www.cs.utexas.edu/users/oops/papers.html>
24 * <ftp://ftp.cs.utexas.edu/pub/garbage/bigsurv.ps>.
37 #include "interrupt.h"
42 #include "gc-internal.h"
45 #include "genesis/vector.h"
46 #include "genesis/weak-pointer.h"
47 #include "genesis/fdefn.h"
48 #include "genesis/simple-fun.h"
50 #include "genesis/hash-table.h"
51 #include "genesis/instance.h"
52 #include "genesis/layout.h"
54 #if defined(LUTEX_WIDETAG)
55 #include "pthread-lutex.h"
58 /* forward declarations */
59 page_index_t gc_find_freeish_pages(long *restart_page_ptr, long nbytes,
67 /* Generations 0-5 are normal collected generations, 6 is only used as
68 * scratch space by the collector, and should never get collected.
71 HIGHEST_NORMAL_GENERATION = 5,
72 PSEUDO_STATIC_GENERATION,
77 /* Should we use page protection to help avoid the scavenging of pages
78 * that don't have pointers to younger generations? */
79 boolean enable_page_protection = 1;
81 /* the minimum size (in bytes) for a large object*/
82 long large_object_size = 4 * PAGE_BYTES;
89 /* the verbosity level. All non-error messages are disabled at level 0;
90 * and only a few rare messages are printed at level 1. */
92 boolean gencgc_verbose = 1;
94 boolean gencgc_verbose = 0;
97 /* FIXME: At some point enable the various error-checking things below
98 * and see what they say. */
100 /* We hunt for pointers to old-space, when GCing generations >= verify_gen.
101 * Set verify_gens to HIGHEST_NORMAL_GENERATION + 1 to disable this kind of
103 generation_index_t verify_gens = HIGHEST_NORMAL_GENERATION + 1;
105 /* Should we do a pre-scan verify of generation 0 before it's GCed? */
106 boolean pre_verify_gen_0 = 0;
108 /* Should we check for bad pointers after gc_free_heap is called
109 * from Lisp PURIFY? */
110 boolean verify_after_free_heap = 0;
112 /* Should we print a note when code objects are found in the dynamic space
113 * during a heap verify? */
114 boolean verify_dynamic_code_check = 0;
116 /* Should we check code objects for fixup errors after they are transported? */
117 boolean check_code_fixups = 0;
119 /* Should we check that newly allocated regions are zero filled? */
120 boolean gencgc_zero_check = 0;
122 /* Should we check that the free space is zero filled? */
123 boolean gencgc_enable_verify_zero_fill = 0;
125 /* Should we check that free pages are zero filled during gc_free_heap
126 * called after Lisp PURIFY? */
127 boolean gencgc_zero_check_during_free_heap = 0;
129 /* When loading a core, don't do a full scan of the memory for the
130 * memory region boundaries. (Set to true by coreparse.c if the core
131 * contained a pagetable entry).
133 boolean gencgc_partial_pickup = 0;
135 /* If defined, free pages are read-protected to ensure that nothing
139 /* #define READ_PROTECT_FREE_PAGES */
143 * GC structures and variables
146 /* the total bytes allocated. These are seen by Lisp DYNAMIC-USAGE. */
147 unsigned long bytes_allocated = 0;
148 unsigned long auto_gc_trigger = 0;
150 /* the source and destination generations. These are set before a GC starts
152 generation_index_t from_space;
153 generation_index_t new_space;
155 /* Set to 1 when in GC */
156 boolean gc_active_p = 0;
158 /* should the GC be conservative on stack. If false (only right before
159 * saving a core), don't scan the stack / mark pages dont_move. */
160 static boolean conservative_stack = 1;
162 /* An array of page structures is allocated on gc initialization.
163 * This helps quickly map between an address its page structure.
164 * page_table_pages is set from the size of the dynamic space. */
165 page_index_t page_table_pages;
166 struct page *page_table;
168 /* To map addresses to page structures the address of the first page
170 static void *heap_base = NULL;
172 /* Calculate the start address for the given page number. */
174 page_address(page_index_t page_num)
176 return (heap_base + (page_num * PAGE_BYTES));
179 /* Calculate the address where the allocation region associated with
180 * the page starts. */
182 page_region_start(page_index_t page_index)
184 return page_address(page_index)-page_table[page_index].region_start_offset;
187 /* Find the page index within the page_table for the given
188 * address. Return -1 on failure. */
190 find_page_index(void *addr)
192 if (addr >= heap_base) {
193 page_index_t index = ((pointer_sized_uint_t)addr -
194 (pointer_sized_uint_t)heap_base) / PAGE_BYTES;
195 if (index < page_table_pages)
202 npage_bytes(long npages)
204 gc_assert(npages>=0);
205 return ((unsigned long)npages)*PAGE_BYTES;
208 /* Check that X is a higher address than Y and return offset from Y to
211 size_t void_diff(void *x, void *y)
214 return (pointer_sized_uint_t)x - (pointer_sized_uint_t)y;
217 /* a structure to hold the state of a generation */
220 /* the first page that gc_alloc() checks on its next call */
221 page_index_t alloc_start_page;
223 /* the first page that gc_alloc_unboxed() checks on its next call */
224 page_index_t alloc_unboxed_start_page;
226 /* the first page that gc_alloc_large (boxed) considers on its next
227 * call. (Although it always allocates after the boxed_region.) */
228 page_index_t alloc_large_start_page;
230 /* the first page that gc_alloc_large (unboxed) considers on its
231 * next call. (Although it always allocates after the
232 * current_unboxed_region.) */
233 page_index_t alloc_large_unboxed_start_page;
235 /* the bytes allocated to this generation */
236 unsigned long bytes_allocated;
238 /* the number of bytes at which to trigger a GC */
239 unsigned long gc_trigger;
241 /* to calculate a new level for gc_trigger */
242 unsigned long bytes_consed_between_gc;
244 /* the number of GCs since the last raise */
247 /* the average age after which a GC will raise objects to the
251 /* the cumulative sum of the bytes allocated to this generation. It is
252 * cleared after a GC on this generations, and update before new
253 * objects are added from a GC of a younger generation. Dividing by
254 * the bytes_allocated will give the average age of the memory in
255 * this generation since its last GC. */
256 unsigned long cum_sum_bytes_allocated;
258 /* a minimum average memory age before a GC will occur helps
259 * prevent a GC when a large number of new live objects have been
260 * added, in which case a GC could be a waste of time */
261 double min_av_mem_age;
263 /* A linked list of lutex structures in this generation, used for
264 * implementing lutex finalization. */
266 struct lutex *lutexes;
272 /* an array of generation structures. There needs to be one more
273 * generation structure than actual generations as the oldest
274 * generation is temporarily raised then lowered. */
275 struct generation generations[NUM_GENERATIONS];
277 /* the oldest generation that is will currently be GCed by default.
278 * Valid values are: 0, 1, ... HIGHEST_NORMAL_GENERATION
280 * The default of HIGHEST_NORMAL_GENERATION enables GC on all generations.
282 * Setting this to 0 effectively disables the generational nature of
283 * the GC. In some applications generational GC may not be useful
284 * because there are no long-lived objects.
286 * An intermediate value could be handy after moving long-lived data
287 * into an older generation so an unnecessary GC of this long-lived
288 * data can be avoided. */
289 generation_index_t gencgc_oldest_gen_to_gc = HIGHEST_NORMAL_GENERATION;
291 /* The maximum free page in the heap is maintained and used to update
292 * ALLOCATION_POINTER which is used by the room function to limit its
293 * search of the heap. XX Gencgc obviously needs to be better
294 * integrated with the Lisp code. */
295 page_index_t last_free_page;
297 /* This lock is to prevent multiple threads from simultaneously
298 * allocating new regions which overlap each other. Note that the
299 * majority of GC is single-threaded, but alloc() may be called from
300 * >1 thread at a time and must be thread-safe. This lock must be
301 * seized before all accesses to generations[] or to parts of
302 * page_table[] that other threads may want to see */
304 #ifdef LISP_FEATURE_SB_THREAD
305 static pthread_mutex_t free_pages_lock = PTHREAD_MUTEX_INITIALIZER;
310 * miscellaneous heap functions
313 /* Count the number of pages which are write-protected within the
314 * given generation. */
316 count_write_protect_generation_pages(generation_index_t generation)
319 unsigned long count = 0;
321 for (i = 0; i < last_free_page; i++)
322 if ((page_table[i].allocated != FREE_PAGE_FLAG)
323 && (page_table[i].gen == generation)
324 && (page_table[i].write_protected == 1))
329 /* Count the number of pages within the given generation. */
331 count_generation_pages(generation_index_t generation)
336 for (i = 0; i < last_free_page; i++)
337 if ((page_table[i].allocated != FREE_PAGE_FLAG)
338 && (page_table[i].gen == generation))
345 count_dont_move_pages(void)
349 for (i = 0; i < last_free_page; i++) {
350 if ((page_table[i].allocated != FREE_PAGE_FLAG)
351 && (page_table[i].dont_move != 0)) {
359 /* Work through the pages and add up the number of bytes used for the
360 * given generation. */
362 count_generation_bytes_allocated (generation_index_t gen)
365 unsigned long result = 0;
366 for (i = 0; i < last_free_page; i++) {
367 if ((page_table[i].allocated != FREE_PAGE_FLAG)
368 && (page_table[i].gen == gen))
369 result += page_table[i].bytes_used;
374 /* Return the average age of the memory in a generation. */
376 gen_av_mem_age(generation_index_t gen)
378 if (generations[gen].bytes_allocated == 0)
382 ((double)generations[gen].cum_sum_bytes_allocated)
383 / ((double)generations[gen].bytes_allocated);
386 /* The verbose argument controls how much to print: 0 for normal
387 * level of detail; 1 for debugging. */
389 print_generation_stats(int verbose) /* FIXME: should take FILE argument */
391 generation_index_t i, gens;
393 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
394 #define FPU_STATE_SIZE 27
395 int fpu_state[FPU_STATE_SIZE];
396 #elif defined(LISP_FEATURE_PPC)
397 #define FPU_STATE_SIZE 32
398 long long fpu_state[FPU_STATE_SIZE];
401 /* This code uses the FP instructions which may be set up for Lisp
402 * so they need to be saved and reset for C. */
405 /* highest generation to print */
407 gens = SCRATCH_GENERATION;
409 gens = PSEUDO_STATIC_GENERATION;
411 /* Print the heap stats. */
413 " Gen StaPg UbSta LaSta LUbSt Boxed Unboxed LB LUB !move Alloc Waste Trig WP GCs Mem-age\n");
415 for (i = 0; i < gens; i++) {
418 long unboxed_cnt = 0;
419 long large_boxed_cnt = 0;
420 long large_unboxed_cnt = 0;
423 for (j = 0; j < last_free_page; j++)
424 if (page_table[j].gen == i) {
426 /* Count the number of boxed pages within the given
428 if (page_table[j].allocated & BOXED_PAGE_FLAG) {
429 if (page_table[j].large_object)
434 if(page_table[j].dont_move) pinned_cnt++;
435 /* Count the number of unboxed pages within the given
437 if (page_table[j].allocated & UNBOXED_PAGE_FLAG) {
438 if (page_table[j].large_object)
445 gc_assert(generations[i].bytes_allocated
446 == count_generation_bytes_allocated(i));
448 " %1d: %5ld %5ld %5ld %5ld %5ld %5ld %5ld %5ld %5ld %8ld %5ld %8ld %4ld %3d %7.4f\n",
450 generations[i].alloc_start_page,
451 generations[i].alloc_unboxed_start_page,
452 generations[i].alloc_large_start_page,
453 generations[i].alloc_large_unboxed_start_page,
459 generations[i].bytes_allocated,
460 (npage_bytes(count_generation_pages(i))
461 - generations[i].bytes_allocated),
462 generations[i].gc_trigger,
463 count_write_protect_generation_pages(i),
464 generations[i].num_gc,
467 fprintf(stderr," Total bytes allocated=%ld\n", bytes_allocated);
469 fpu_restore(fpu_state);
473 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
474 void fast_bzero(void*, size_t); /* in <arch>-assem.S */
477 /* Zero the pages from START to END (inclusive), but use mmap/munmap instead
478 * if zeroing it ourselves, i.e. in practice give the memory back to the
479 * OS. Generally done after a large GC.
481 void zero_pages_with_mmap(page_index_t start, page_index_t end) {
483 void *addr = page_address(start), *new_addr;
484 size_t length = npage_bytes(1+end-start);
489 os_invalidate(addr, length);
490 new_addr = os_validate(addr, length);
491 if (new_addr == NULL || new_addr != addr) {
492 lose("remap_free_pages: page moved, 0x%08x ==> 0x%08x",
496 for (i = start; i <= end; i++) {
497 page_table[i].need_to_zero = 0;
501 /* Zero the pages from START to END (inclusive). Generally done just after
502 * a new region has been allocated.
505 zero_pages(page_index_t start, page_index_t end) {
509 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
510 fast_bzero(page_address(start), npage_bytes(1+end-start));
512 bzero(page_address(start), npage_bytes(1+end-start));
517 /* Zero the pages from START to END (inclusive), except for those
518 * pages that are known to already zeroed. Mark all pages in the
519 * ranges as non-zeroed.
522 zero_dirty_pages(page_index_t start, page_index_t end) {
525 for (i = start; i <= end; i++) {
526 if (page_table[i].need_to_zero == 1) {
527 zero_pages(start, end);
532 for (i = start; i <= end; i++) {
533 page_table[i].need_to_zero = 1;
539 * To support quick and inline allocation, regions of memory can be
540 * allocated and then allocated from with just a free pointer and a
541 * check against an end address.
543 * Since objects can be allocated to spaces with different properties
544 * e.g. boxed/unboxed, generation, ages; there may need to be many
545 * allocation regions.
547 * Each allocation region may start within a partly used page. Many
548 * features of memory use are noted on a page wise basis, e.g. the
549 * generation; so if a region starts within an existing allocated page
550 * it must be consistent with this page.
552 * During the scavenging of the newspace, objects will be transported
553 * into an allocation region, and pointers updated to point to this
554 * allocation region. It is possible that these pointers will be
555 * scavenged again before the allocation region is closed, e.g. due to
556 * trans_list which jumps all over the place to cleanup the list. It
557 * is important to be able to determine properties of all objects
558 * pointed to when scavenging, e.g to detect pointers to the oldspace.
559 * Thus it's important that the allocation regions have the correct
560 * properties set when allocated, and not just set when closed. The
561 * region allocation routines return regions with the specified
562 * properties, and grab all the pages, setting their properties
563 * appropriately, except that the amount used is not known.
565 * These regions are used to support quicker allocation using just a
566 * free pointer. The actual space used by the region is not reflected
567 * in the pages tables until it is closed. It can't be scavenged until
570 * When finished with the region it should be closed, which will
571 * update the page tables for the actual space used returning unused
572 * space. Further it may be noted in the new regions which is
573 * necessary when scavenging the newspace.
575 * Large objects may be allocated directly without an allocation
576 * region, the page tables are updated immediately.
578 * Unboxed objects don't contain pointers to other objects and so
579 * don't need scavenging. Further they can't contain pointers to
580 * younger generations so WP is not needed. By allocating pages to
581 * unboxed objects the whole page never needs scavenging or
582 * write-protecting. */
584 /* We are only using two regions at present. Both are for the current
585 * newspace generation. */
586 struct alloc_region boxed_region;
587 struct alloc_region unboxed_region;
589 /* The generation currently being allocated to. */
590 static generation_index_t gc_alloc_generation;
592 /* Find a new region with room for at least the given number of bytes.
594 * It starts looking at the current generation's alloc_start_page. So
595 * may pick up from the previous region if there is enough space. This
596 * keeps the allocation contiguous when scavenging the newspace.
598 * The alloc_region should have been closed by a call to
599 * gc_alloc_update_page_tables(), and will thus be in an empty state.
601 * To assist the scavenging functions write-protected pages are not
602 * used. Free pages should not be write-protected.
604 * It is critical to the conservative GC that the start of regions be
605 * known. To help achieve this only small regions are allocated at a
608 * During scavenging, pointers may be found to within the current
609 * region and the page generation must be set so that pointers to the
610 * from space can be recognized. Therefore the generation of pages in
611 * the region are set to gc_alloc_generation. To prevent another
612 * allocation call using the same pages, all the pages in the region
613 * are allocated, although they will initially be empty.
616 gc_alloc_new_region(long nbytes, int unboxed, struct alloc_region *alloc_region)
618 page_index_t first_page;
619 page_index_t last_page;
620 unsigned long bytes_found;
626 "/alloc_new_region for %d bytes from gen %d\n",
627 nbytes, gc_alloc_generation));
630 /* Check that the region is in a reset state. */
631 gc_assert((alloc_region->first_page == 0)
632 && (alloc_region->last_page == -1)
633 && (alloc_region->free_pointer == alloc_region->end_addr));
634 ret = thread_mutex_lock(&free_pages_lock);
638 generations[gc_alloc_generation].alloc_unboxed_start_page;
641 generations[gc_alloc_generation].alloc_start_page;
643 last_page=gc_find_freeish_pages(&first_page,nbytes,unboxed);
644 bytes_found=(PAGE_BYTES - page_table[first_page].bytes_used)
645 + npage_bytes(last_page-first_page);
647 /* Set up the alloc_region. */
648 alloc_region->first_page = first_page;
649 alloc_region->last_page = last_page;
650 alloc_region->start_addr = page_table[first_page].bytes_used
651 + page_address(first_page);
652 alloc_region->free_pointer = alloc_region->start_addr;
653 alloc_region->end_addr = alloc_region->start_addr + bytes_found;
655 /* Set up the pages. */
657 /* The first page may have already been in use. */
658 if (page_table[first_page].bytes_used == 0) {
660 page_table[first_page].allocated = UNBOXED_PAGE_FLAG;
662 page_table[first_page].allocated = BOXED_PAGE_FLAG;
663 page_table[first_page].gen = gc_alloc_generation;
664 page_table[first_page].large_object = 0;
665 page_table[first_page].region_start_offset = 0;
669 gc_assert(page_table[first_page].allocated == UNBOXED_PAGE_FLAG);
671 gc_assert(page_table[first_page].allocated == BOXED_PAGE_FLAG);
672 page_table[first_page].allocated |= OPEN_REGION_PAGE_FLAG;
674 gc_assert(page_table[first_page].gen == gc_alloc_generation);
675 gc_assert(page_table[first_page].large_object == 0);
677 for (i = first_page+1; i <= last_page; i++) {
679 page_table[i].allocated = UNBOXED_PAGE_FLAG;
681 page_table[i].allocated = BOXED_PAGE_FLAG;
682 page_table[i].gen = gc_alloc_generation;
683 page_table[i].large_object = 0;
684 /* This may not be necessary for unboxed regions (think it was
686 page_table[i].region_start_offset =
687 void_diff(page_address(i),alloc_region->start_addr);
688 page_table[i].allocated |= OPEN_REGION_PAGE_FLAG ;
690 /* Bump up last_free_page. */
691 if (last_page+1 > last_free_page) {
692 last_free_page = last_page+1;
693 /* do we only want to call this on special occasions? like for
695 set_alloc_pointer((lispobj)page_address(last_free_page));
697 ret = thread_mutex_unlock(&free_pages_lock);
700 #ifdef READ_PROTECT_FREE_PAGES
701 os_protect(page_address(first_page),
702 npage_bytes(1+last_page-first_page),
706 /* If the first page was only partial, don't check whether it's
707 * zeroed (it won't be) and don't zero it (since the parts that
708 * we're interested in are guaranteed to be zeroed).
710 if (page_table[first_page].bytes_used) {
714 zero_dirty_pages(first_page, last_page);
716 /* we can do this after releasing free_pages_lock */
717 if (gencgc_zero_check) {
719 for (p = (long *)alloc_region->start_addr;
720 p < (long *)alloc_region->end_addr; p++) {
722 /* KLUDGE: It would be nice to use %lx and explicit casts
723 * (long) in code like this, so that it is less likely to
724 * break randomly when running on a machine with different
725 * word sizes. -- WHN 19991129 */
726 lose("The new region at %x is not zero (start=%p, end=%p).\n",
727 p, alloc_region->start_addr, alloc_region->end_addr);
733 /* If the record_new_objects flag is 2 then all new regions created
736 * If it's 1 then then it is only recorded if the first page of the
737 * current region is <= new_areas_ignore_page. This helps avoid
738 * unnecessary recording when doing full scavenge pass.
740 * The new_object structure holds the page, byte offset, and size of
741 * new regions of objects. Each new area is placed in the array of
742 * these structures pointer to by new_areas. new_areas_index holds the
743 * offset into new_areas.
745 * If new_area overflows NUM_NEW_AREAS then it stops adding them. The
746 * later code must detect this and handle it, probably by doing a full
747 * scavenge of a generation. */
748 #define NUM_NEW_AREAS 512
749 static int record_new_objects = 0;
750 static page_index_t new_areas_ignore_page;
756 static struct new_area (*new_areas)[];
757 static long new_areas_index;
760 /* Add a new area to new_areas. */
762 add_new_area(page_index_t first_page, size_t offset, size_t size)
764 unsigned long new_area_start,c;
767 /* Ignore if full. */
768 if (new_areas_index >= NUM_NEW_AREAS)
771 switch (record_new_objects) {
775 if (first_page > new_areas_ignore_page)
784 new_area_start = npage_bytes(first_page) + offset;
786 /* Search backwards for a prior area that this follows from. If
787 found this will save adding a new area. */
788 for (i = new_areas_index-1, c = 0; (i >= 0) && (c < 8); i--, c++) {
789 unsigned long area_end =
790 npage_bytes((*new_areas)[i].page)
791 + (*new_areas)[i].offset
792 + (*new_areas)[i].size;
794 "/add_new_area S1 %d %d %d %d\n",
795 i, c, new_area_start, area_end));*/
796 if (new_area_start == area_end) {
798 "/adding to [%d] %d %d %d with %d %d %d:\n",
800 (*new_areas)[i].page,
801 (*new_areas)[i].offset,
802 (*new_areas)[i].size,
806 (*new_areas)[i].size += size;
811 (*new_areas)[new_areas_index].page = first_page;
812 (*new_areas)[new_areas_index].offset = offset;
813 (*new_areas)[new_areas_index].size = size;
815 "/new_area %d page %d offset %d size %d\n",
816 new_areas_index, first_page, offset, size));*/
819 /* Note the max new_areas used. */
820 if (new_areas_index > max_new_areas)
821 max_new_areas = new_areas_index;
824 /* Update the tables for the alloc_region. The region may be added to
827 * When done the alloc_region is set up so that the next quick alloc
828 * will fail safely and thus a new region will be allocated. Further
829 * it is safe to try to re-update the page table of this reset
832 gc_alloc_update_page_tables(int unboxed, struct alloc_region *alloc_region)
835 page_index_t first_page;
836 page_index_t next_page;
837 unsigned long bytes_used;
838 unsigned long orig_first_page_bytes_used;
839 unsigned long region_size;
840 unsigned long byte_cnt;
844 first_page = alloc_region->first_page;
846 /* Catch an unused alloc_region. */
847 if ((first_page == 0) && (alloc_region->last_page == -1))
850 next_page = first_page+1;
852 ret = thread_mutex_lock(&free_pages_lock);
854 if (alloc_region->free_pointer != alloc_region->start_addr) {
855 /* some bytes were allocated in the region */
856 orig_first_page_bytes_used = page_table[first_page].bytes_used;
858 gc_assert(alloc_region->start_addr ==
859 (page_address(first_page)
860 + page_table[first_page].bytes_used));
862 /* All the pages used need to be updated */
864 /* Update the first page. */
866 /* If the page was free then set up the gen, and
867 * region_start_offset. */
868 if (page_table[first_page].bytes_used == 0)
869 gc_assert(page_table[first_page].region_start_offset == 0);
870 page_table[first_page].allocated &= ~(OPEN_REGION_PAGE_FLAG);
873 gc_assert(page_table[first_page].allocated == UNBOXED_PAGE_FLAG);
875 gc_assert(page_table[first_page].allocated == BOXED_PAGE_FLAG);
876 gc_assert(page_table[first_page].gen == gc_alloc_generation);
877 gc_assert(page_table[first_page].large_object == 0);
881 /* Calculate the number of bytes used in this page. This is not
882 * always the number of new bytes, unless it was free. */
884 if ((bytes_used = void_diff(alloc_region->free_pointer,
885 page_address(first_page)))
887 bytes_used = PAGE_BYTES;
890 page_table[first_page].bytes_used = bytes_used;
891 byte_cnt += bytes_used;
894 /* All the rest of the pages should be free. We need to set
895 * their region_start_offset pointer to the start of the
896 * region, and set the bytes_used. */
898 page_table[next_page].allocated &= ~(OPEN_REGION_PAGE_FLAG);
900 gc_assert(page_table[next_page].allocated==UNBOXED_PAGE_FLAG);
902 gc_assert(page_table[next_page].allocated == BOXED_PAGE_FLAG);
903 gc_assert(page_table[next_page].bytes_used == 0);
904 gc_assert(page_table[next_page].gen == gc_alloc_generation);
905 gc_assert(page_table[next_page].large_object == 0);
907 gc_assert(page_table[next_page].region_start_offset ==
908 void_diff(page_address(next_page),
909 alloc_region->start_addr));
911 /* Calculate the number of bytes used in this page. */
913 if ((bytes_used = void_diff(alloc_region->free_pointer,
914 page_address(next_page)))>PAGE_BYTES) {
915 bytes_used = PAGE_BYTES;
918 page_table[next_page].bytes_used = bytes_used;
919 byte_cnt += bytes_used;
924 region_size = void_diff(alloc_region->free_pointer,
925 alloc_region->start_addr);
926 bytes_allocated += region_size;
927 generations[gc_alloc_generation].bytes_allocated += region_size;
929 gc_assert((byte_cnt- orig_first_page_bytes_used) == region_size);
931 /* Set the generations alloc restart page to the last page of
934 generations[gc_alloc_generation].alloc_unboxed_start_page =
937 generations[gc_alloc_generation].alloc_start_page = next_page-1;
939 /* Add the region to the new_areas if requested. */
941 add_new_area(first_page,orig_first_page_bytes_used, region_size);
945 "/gc_alloc_update_page_tables update %d bytes to gen %d\n",
947 gc_alloc_generation));
950 /* There are no bytes allocated. Unallocate the first_page if
951 * there are 0 bytes_used. */
952 page_table[first_page].allocated &= ~(OPEN_REGION_PAGE_FLAG);
953 if (page_table[first_page].bytes_used == 0)
954 page_table[first_page].allocated = FREE_PAGE_FLAG;
957 /* Unallocate any unused pages. */
958 while (next_page <= alloc_region->last_page) {
959 gc_assert(page_table[next_page].bytes_used == 0);
960 page_table[next_page].allocated = FREE_PAGE_FLAG;
963 ret = thread_mutex_unlock(&free_pages_lock);
966 /* alloc_region is per-thread, we're ok to do this unlocked */
967 gc_set_region_empty(alloc_region);
970 static inline void *gc_quick_alloc(long nbytes);
972 /* Allocate a possibly large object. */
974 gc_alloc_large(long nbytes, int unboxed, struct alloc_region *alloc_region)
976 page_index_t first_page;
977 page_index_t last_page;
978 int orig_first_page_bytes_used;
982 page_index_t next_page;
985 ret = thread_mutex_lock(&free_pages_lock);
990 generations[gc_alloc_generation].alloc_large_unboxed_start_page;
992 first_page = generations[gc_alloc_generation].alloc_large_start_page;
994 if (first_page <= alloc_region->last_page) {
995 first_page = alloc_region->last_page+1;
998 last_page=gc_find_freeish_pages(&first_page,nbytes,unboxed);
1000 gc_assert(first_page > alloc_region->last_page);
1002 generations[gc_alloc_generation].alloc_large_unboxed_start_page =
1005 generations[gc_alloc_generation].alloc_large_start_page = last_page;
1007 /* Set up the pages. */
1008 orig_first_page_bytes_used = page_table[first_page].bytes_used;
1010 /* If the first page was free then set up the gen, and
1011 * region_start_offset. */
1012 if (page_table[first_page].bytes_used == 0) {
1014 page_table[first_page].allocated = UNBOXED_PAGE_FLAG;
1016 page_table[first_page].allocated = BOXED_PAGE_FLAG;
1017 page_table[first_page].gen = gc_alloc_generation;
1018 page_table[first_page].region_start_offset = 0;
1019 page_table[first_page].large_object = 1;
1023 gc_assert(page_table[first_page].allocated == UNBOXED_PAGE_FLAG);
1025 gc_assert(page_table[first_page].allocated == BOXED_PAGE_FLAG);
1026 gc_assert(page_table[first_page].gen == gc_alloc_generation);
1027 gc_assert(page_table[first_page].large_object == 1);
1031 /* Calc. the number of bytes used in this page. This is not
1032 * always the number of new bytes, unless it was free. */
1034 if ((bytes_used = nbytes+orig_first_page_bytes_used) > PAGE_BYTES) {
1035 bytes_used = PAGE_BYTES;
1038 page_table[first_page].bytes_used = bytes_used;
1039 byte_cnt += bytes_used;
1041 next_page = first_page+1;
1043 /* All the rest of the pages should be free. We need to set their
1044 * region_start_offset pointer to the start of the region, and set
1045 * the bytes_used. */
1047 gc_assert(page_table[next_page].allocated == FREE_PAGE_FLAG);
1048 gc_assert(page_table[next_page].bytes_used == 0);
1050 page_table[next_page].allocated = UNBOXED_PAGE_FLAG;
1052 page_table[next_page].allocated = BOXED_PAGE_FLAG;
1053 page_table[next_page].gen = gc_alloc_generation;
1054 page_table[next_page].large_object = 1;
1056 page_table[next_page].region_start_offset =
1057 npage_bytes(next_page-first_page) - orig_first_page_bytes_used;
1059 /* Calculate the number of bytes used in this page. */
1061 bytes_used=(nbytes+orig_first_page_bytes_used)-byte_cnt;
1062 if (bytes_used > PAGE_BYTES) {
1063 bytes_used = PAGE_BYTES;
1066 page_table[next_page].bytes_used = bytes_used;
1067 page_table[next_page].write_protected=0;
1068 page_table[next_page].dont_move=0;
1069 byte_cnt += bytes_used;
1073 gc_assert((byte_cnt-orig_first_page_bytes_used) == nbytes);
1075 bytes_allocated += nbytes;
1076 generations[gc_alloc_generation].bytes_allocated += nbytes;
1078 /* Add the region to the new_areas if requested. */
1080 add_new_area(first_page,orig_first_page_bytes_used,nbytes);
1082 /* Bump up last_free_page */
1083 if (last_page+1 > last_free_page) {
1084 last_free_page = last_page+1;
1085 set_alloc_pointer((lispobj)(page_address(last_free_page)));
1087 ret = thread_mutex_unlock(&free_pages_lock);
1088 gc_assert(ret == 0);
1090 #ifdef READ_PROTECT_FREE_PAGES
1091 os_protect(page_address(first_page),
1092 npage_bytes(1+last_page-first_page),
1096 zero_dirty_pages(first_page, last_page);
1098 return page_address(first_page);
1101 static page_index_t gencgc_alloc_start_page = -1;
1104 gc_heap_exhausted_error_or_lose (long available, long requested)
1106 /* Write basic information before doing anything else: if we don't
1107 * call to lisp this is a must, and even if we do there is always
1108 * the danger that we bounce back here before the error has been
1109 * handled, or indeed even printed.
1111 fprintf(stderr, "Heap exhausted during %s: %ld bytes available, %ld requested.\n",
1112 gc_active_p ? "garbage collection" : "allocation",
1113 available, requested);
1114 if (gc_active_p || (available == 0)) {
1115 /* If we are in GC, or totally out of memory there is no way
1116 * to sanely transfer control to the lisp-side of things.
1118 struct thread *thread = arch_os_get_current_thread();
1119 print_generation_stats(1);
1120 fprintf(stderr, "GC control variables:\n");
1121 fprintf(stderr, " *GC-INHIBIT* = %s\n *GC-PENDING* = %s\n",
1122 SymbolValue(GC_INHIBIT,thread)==NIL ? "false" : "true",
1123 SymbolValue(GC_PENDING,thread)==NIL ? "false" : "true");
1124 #ifdef LISP_FEATURE_SB_THREAD
1125 fprintf(stderr, " *STOP-FOR-GC-PENDING* = %s\n",
1126 SymbolValue(STOP_FOR_GC_PENDING,thread)==NIL ? "false" : "true");
1128 lose("Heap exhausted, game over.");
1131 /* FIXME: assert free_pages_lock held */
1132 (void)thread_mutex_unlock(&free_pages_lock);
1133 funcall2(StaticSymbolFunction(HEAP_EXHAUSTED_ERROR),
1134 alloc_number(available), alloc_number(requested));
1135 lose("HEAP-EXHAUSTED-ERROR fell through");
1140 gc_find_freeish_pages(page_index_t *restart_page_ptr, long nbytes, int unboxed)
1142 page_index_t first_page, last_page;
1143 page_index_t restart_page = *restart_page_ptr;
1144 long bytes_found = 0;
1145 long most_bytes_found = 0;
1146 /* FIXME: assert(free_pages_lock is held); */
1148 /* Toggled by gc_and_save for heap compaction, normally -1. */
1149 if (gencgc_alloc_start_page != -1) {
1150 restart_page = gencgc_alloc_start_page;
1153 if (nbytes>=PAGE_BYTES) {
1154 /* Search for a contiguous free space of at least nbytes,
1155 * aligned on a page boundary. The page-alignment is strictly
1156 * speaking needed only for objects at least large_object_size
1159 first_page = restart_page;
1160 while ((first_page < page_table_pages) &&
1161 (page_table[first_page].allocated != FREE_PAGE_FLAG))
1164 last_page = first_page;
1165 bytes_found = PAGE_BYTES;
1166 while ((bytes_found < nbytes) &&
1167 (last_page < (page_table_pages-1)) &&
1168 (page_table[last_page+1].allocated == FREE_PAGE_FLAG)) {
1170 bytes_found += PAGE_BYTES;
1171 gc_assert(page_table[last_page].write_protected == 0);
1173 if (bytes_found > most_bytes_found)
1174 most_bytes_found = bytes_found;
1175 restart_page = last_page + 1;
1176 } while ((restart_page < page_table_pages) && (bytes_found < nbytes));
1179 /* Search for a page with at least nbytes of space. We prefer
1180 * not to split small objects on multiple pages, to reduce the
1181 * number of contiguous allocation regions spaning multiple
1182 * pages: this helps avoid excessive conservativism. */
1183 first_page = restart_page;
1184 while (first_page < page_table_pages) {
1185 if (page_table[first_page].allocated == FREE_PAGE_FLAG)
1187 bytes_found = PAGE_BYTES;
1190 else if ((page_table[first_page].allocated ==
1191 (unboxed ? UNBOXED_PAGE_FLAG : BOXED_PAGE_FLAG)) &&
1192 (page_table[first_page].large_object == 0) &&
1193 (page_table[first_page].gen == gc_alloc_generation) &&
1194 (page_table[first_page].write_protected == 0) &&
1195 (page_table[first_page].dont_move == 0))
1197 bytes_found = PAGE_BYTES
1198 - page_table[first_page].bytes_used;
1199 if (bytes_found > most_bytes_found)
1200 most_bytes_found = bytes_found;
1201 if (bytes_found >= nbytes)
1206 last_page = first_page;
1207 restart_page = first_page + 1;
1210 /* Check for a failure */
1211 if (bytes_found < nbytes) {
1212 gc_assert(restart_page >= page_table_pages);
1213 gc_heap_exhausted_error_or_lose(most_bytes_found, nbytes);
1216 gc_assert(page_table[first_page].write_protected == 0);
1218 *restart_page_ptr = first_page;
1222 /* Allocate bytes. All the rest of the special-purpose allocation
1223 * functions will eventually call this */
1226 gc_alloc_with_region(long nbytes,int unboxed_p, struct alloc_region *my_region,
1229 void *new_free_pointer;
1231 if (nbytes>=large_object_size)
1232 return gc_alloc_large(nbytes,unboxed_p,my_region);
1234 /* Check whether there is room in the current alloc region. */
1235 new_free_pointer = my_region->free_pointer + nbytes;
1237 /* fprintf(stderr, "alloc %d bytes from %p to %p\n", nbytes,
1238 my_region->free_pointer, new_free_pointer); */
1240 if (new_free_pointer <= my_region->end_addr) {
1241 /* If so then allocate from the current alloc region. */
1242 void *new_obj = my_region->free_pointer;
1243 my_region->free_pointer = new_free_pointer;
1245 /* Unless a `quick' alloc was requested, check whether the
1246 alloc region is almost empty. */
1248 void_diff(my_region->end_addr,my_region->free_pointer) <= 32) {
1249 /* If so, finished with the current region. */
1250 gc_alloc_update_page_tables(unboxed_p, my_region);
1251 /* Set up a new region. */
1252 gc_alloc_new_region(32 /*bytes*/, unboxed_p, my_region);
1255 return((void *)new_obj);
1258 /* Else not enough free space in the current region: retry with a
1261 gc_alloc_update_page_tables(unboxed_p, my_region);
1262 gc_alloc_new_region(nbytes, unboxed_p, my_region);
1263 return gc_alloc_with_region(nbytes,unboxed_p,my_region,0);
1266 /* these are only used during GC: all allocation from the mutator calls
1267 * alloc() -> gc_alloc_with_region() with the appropriate per-thread
1271 gc_general_alloc(long nbytes,int unboxed_p,int quick_p)
1273 struct alloc_region *my_region =
1274 unboxed_p ? &unboxed_region : &boxed_region;
1275 return gc_alloc_with_region(nbytes,unboxed_p, my_region,quick_p);
1278 static inline void *
1279 gc_quick_alloc(long nbytes)
1281 return gc_general_alloc(nbytes,ALLOC_BOXED,ALLOC_QUICK);
1284 static inline void *
1285 gc_quick_alloc_large(long nbytes)
1287 return gc_general_alloc(nbytes,ALLOC_BOXED,ALLOC_QUICK);
1290 static inline void *
1291 gc_alloc_unboxed(long nbytes)
1293 return gc_general_alloc(nbytes,ALLOC_UNBOXED,0);
1296 static inline void *
1297 gc_quick_alloc_unboxed(long nbytes)
1299 return gc_general_alloc(nbytes,ALLOC_UNBOXED,ALLOC_QUICK);
1302 static inline void *
1303 gc_quick_alloc_large_unboxed(long nbytes)
1305 return gc_general_alloc(nbytes,ALLOC_UNBOXED,ALLOC_QUICK);
1309 /* Copy a large boxed object. If the object is in a large object
1310 * region then it is simply promoted, else it is copied. If it's large
1311 * enough then it's copied to a large object region.
1313 * Vectors may have shrunk. If the object is not copied the space
1314 * needs to be reclaimed, and the page_tables corrected. */
1316 copy_large_object(lispobj object, long nwords)
1320 page_index_t first_page;
1322 gc_assert(is_lisp_pointer(object));
1323 gc_assert(from_space_p(object));
1324 gc_assert((nwords & 0x01) == 0);
1327 /* Check whether it's in a large object region. */
1328 first_page = find_page_index((void *)object);
1329 gc_assert(first_page >= 0);
1331 if (page_table[first_page].large_object) {
1333 /* Promote the object. */
1335 unsigned long remaining_bytes;
1336 page_index_t next_page;
1337 unsigned long bytes_freed;
1338 unsigned long old_bytes_used;
1340 /* Note: Any page write-protection must be removed, else a
1341 * later scavenge_newspace may incorrectly not scavenge these
1342 * pages. This would not be necessary if they are added to the
1343 * new areas, but let's do it for them all (they'll probably
1344 * be written anyway?). */
1346 gc_assert(page_table[first_page].region_start_offset == 0);
1348 next_page = first_page;
1349 remaining_bytes = nwords*N_WORD_BYTES;
1350 while (remaining_bytes > PAGE_BYTES) {
1351 gc_assert(page_table[next_page].gen == from_space);
1352 gc_assert(page_table[next_page].allocated == BOXED_PAGE_FLAG);
1353 gc_assert(page_table[next_page].large_object);
1354 gc_assert(page_table[next_page].region_start_offset ==
1355 npage_bytes(next_page-first_page));
1356 gc_assert(page_table[next_page].bytes_used == PAGE_BYTES);
1358 page_table[next_page].gen = new_space;
1360 /* Remove any write-protection. We should be able to rely
1361 * on the write-protect flag to avoid redundant calls. */
1362 if (page_table[next_page].write_protected) {
1363 os_protect(page_address(next_page), PAGE_BYTES, OS_VM_PROT_ALL);
1364 page_table[next_page].write_protected = 0;
1366 remaining_bytes -= PAGE_BYTES;
1370 /* Now only one page remains, but the object may have shrunk
1371 * so there may be more unused pages which will be freed. */
1373 /* The object may have shrunk but shouldn't have grown. */
1374 gc_assert(page_table[next_page].bytes_used >= remaining_bytes);
1376 page_table[next_page].gen = new_space;
1377 gc_assert(page_table[next_page].allocated == BOXED_PAGE_FLAG);
1379 /* Adjust the bytes_used. */
1380 old_bytes_used = page_table[next_page].bytes_used;
1381 page_table[next_page].bytes_used = remaining_bytes;
1383 bytes_freed = old_bytes_used - remaining_bytes;
1385 /* Free any remaining pages; needs care. */
1387 while ((old_bytes_used == PAGE_BYTES) &&
1388 (page_table[next_page].gen == from_space) &&
1389 (page_table[next_page].allocated == BOXED_PAGE_FLAG) &&
1390 page_table[next_page].large_object &&
1391 (page_table[next_page].region_start_offset ==
1392 npage_bytes(next_page - first_page))) {
1393 /* Checks out OK, free the page. Don't need to bother zeroing
1394 * pages as this should have been done before shrinking the
1395 * object. These pages shouldn't be write-protected as they
1396 * should be zero filled. */
1397 gc_assert(page_table[next_page].write_protected == 0);
1399 old_bytes_used = page_table[next_page].bytes_used;
1400 page_table[next_page].allocated = FREE_PAGE_FLAG;
1401 page_table[next_page].bytes_used = 0;
1402 bytes_freed += old_bytes_used;
1406 generations[from_space].bytes_allocated -= N_WORD_BYTES*nwords
1408 generations[new_space].bytes_allocated += N_WORD_BYTES*nwords;
1409 bytes_allocated -= bytes_freed;
1411 /* Add the region to the new_areas if requested. */
1412 add_new_area(first_page,0,nwords*N_WORD_BYTES);
1416 /* Get tag of object. */
1417 tag = lowtag_of(object);
1419 /* Allocate space. */
1420 new = gc_quick_alloc_large(nwords*N_WORD_BYTES);
1422 memcpy(new,native_pointer(object),nwords*N_WORD_BYTES);
1424 /* Return Lisp pointer of new object. */
1425 return ((lispobj) new) | tag;
1429 /* to copy unboxed objects */
1431 copy_unboxed_object(lispobj object, long nwords)
1436 gc_assert(is_lisp_pointer(object));
1437 gc_assert(from_space_p(object));
1438 gc_assert((nwords & 0x01) == 0);
1440 /* Get tag of object. */
1441 tag = lowtag_of(object);
1443 /* Allocate space. */
1444 new = gc_quick_alloc_unboxed(nwords*N_WORD_BYTES);
1446 memcpy(new,native_pointer(object),nwords*N_WORD_BYTES);
1448 /* Return Lisp pointer of new object. */
1449 return ((lispobj) new) | tag;
1452 /* to copy large unboxed objects
1454 * If the object is in a large object region then it is simply
1455 * promoted, else it is copied. If it's large enough then it's copied
1456 * to a large object region.
1458 * Bignums and vectors may have shrunk. If the object is not copied
1459 * the space needs to be reclaimed, and the page_tables corrected.
1461 * KLUDGE: There's a lot of cut-and-paste duplication between this
1462 * function and copy_large_object(..). -- WHN 20000619 */
1464 copy_large_unboxed_object(lispobj object, long nwords)
1468 page_index_t first_page;
1470 gc_assert(is_lisp_pointer(object));
1471 gc_assert(from_space_p(object));
1472 gc_assert((nwords & 0x01) == 0);
1474 if ((nwords > 1024*1024) && gencgc_verbose)
1475 FSHOW((stderr, "/copy_large_unboxed_object: %d bytes\n",
1476 nwords*N_WORD_BYTES));
1478 /* Check whether it's a large object. */
1479 first_page = find_page_index((void *)object);
1480 gc_assert(first_page >= 0);
1482 if (page_table[first_page].large_object) {
1483 /* Promote the object. Note: Unboxed objects may have been
1484 * allocated to a BOXED region so it may be necessary to
1485 * change the region to UNBOXED. */
1486 unsigned long remaining_bytes;
1487 page_index_t next_page;
1488 unsigned long bytes_freed;
1489 unsigned long old_bytes_used;
1491 gc_assert(page_table[first_page].region_start_offset == 0);
1493 next_page = first_page;
1494 remaining_bytes = nwords*N_WORD_BYTES;
1495 while (remaining_bytes > PAGE_BYTES) {
1496 gc_assert(page_table[next_page].gen == from_space);
1497 gc_assert((page_table[next_page].allocated == UNBOXED_PAGE_FLAG)
1498 || (page_table[next_page].allocated == BOXED_PAGE_FLAG));
1499 gc_assert(page_table[next_page].large_object);
1500 gc_assert(page_table[next_page].region_start_offset ==
1501 npage_bytes(next_page-first_page));
1502 gc_assert(page_table[next_page].bytes_used == PAGE_BYTES);
1504 page_table[next_page].gen = new_space;
1505 page_table[next_page].allocated = UNBOXED_PAGE_FLAG;
1506 remaining_bytes -= PAGE_BYTES;
1510 /* Now only one page remains, but the object may have shrunk so
1511 * there may be more unused pages which will be freed. */
1513 /* Object may have shrunk but shouldn't have grown - check. */
1514 gc_assert(page_table[next_page].bytes_used >= remaining_bytes);
1516 page_table[next_page].gen = new_space;
1517 page_table[next_page].allocated = UNBOXED_PAGE_FLAG;
1519 /* Adjust the bytes_used. */
1520 old_bytes_used = page_table[next_page].bytes_used;
1521 page_table[next_page].bytes_used = remaining_bytes;
1523 bytes_freed = old_bytes_used - remaining_bytes;
1525 /* Free any remaining pages; needs care. */
1527 while ((old_bytes_used == PAGE_BYTES) &&
1528 (page_table[next_page].gen == from_space) &&
1529 ((page_table[next_page].allocated == UNBOXED_PAGE_FLAG)
1530 || (page_table[next_page].allocated == BOXED_PAGE_FLAG)) &&
1531 page_table[next_page].large_object &&
1532 (page_table[next_page].region_start_offset ==
1533 npage_bytes(next_page - first_page))) {
1534 /* Checks out OK, free the page. Don't need to both zeroing
1535 * pages as this should have been done before shrinking the
1536 * object. These pages shouldn't be write-protected, even if
1537 * boxed they should be zero filled. */
1538 gc_assert(page_table[next_page].write_protected == 0);
1540 old_bytes_used = page_table[next_page].bytes_used;
1541 page_table[next_page].allocated = FREE_PAGE_FLAG;
1542 page_table[next_page].bytes_used = 0;
1543 bytes_freed += old_bytes_used;
1547 if ((bytes_freed > 0) && gencgc_verbose)
1549 "/copy_large_unboxed bytes_freed=%d\n",
1552 generations[from_space].bytes_allocated -=
1553 nwords*N_WORD_BYTES + bytes_freed;
1554 generations[new_space].bytes_allocated += nwords*N_WORD_BYTES;
1555 bytes_allocated -= bytes_freed;
1560 /* Get tag of object. */
1561 tag = lowtag_of(object);
1563 /* Allocate space. */
1564 new = gc_quick_alloc_large_unboxed(nwords*N_WORD_BYTES);
1566 /* Copy the object. */
1567 memcpy(new,native_pointer(object),nwords*N_WORD_BYTES);
1569 /* Return Lisp pointer of new object. */
1570 return ((lispobj) new) | tag;
1579 * code and code-related objects
1582 static lispobj trans_fun_header(lispobj object);
1583 static lispobj trans_boxed(lispobj object);
1586 /* Scan a x86 compiled code object, looking for possible fixups that
1587 * have been missed after a move.
1589 * Two types of fixups are needed:
1590 * 1. Absolute fixups to within the code object.
1591 * 2. Relative fixups to outside the code object.
1593 * Currently only absolute fixups to the constant vector, or to the
1594 * code area are checked. */
1596 sniff_code_object(struct code *code, unsigned long displacement)
1598 #ifdef LISP_FEATURE_X86
1599 long nheader_words, ncode_words, nwords;
1601 void *constants_start_addr = NULL, *constants_end_addr;
1602 void *code_start_addr, *code_end_addr;
1603 int fixup_found = 0;
1605 if (!check_code_fixups)
1608 FSHOW((stderr, "/sniffing code: %p, %lu\n", code, displacement));
1610 ncode_words = fixnum_value(code->code_size);
1611 nheader_words = HeaderValue(*(lispobj *)code);
1612 nwords = ncode_words + nheader_words;
1614 constants_start_addr = (void *)code + 5*N_WORD_BYTES;
1615 constants_end_addr = (void *)code + nheader_words*N_WORD_BYTES;
1616 code_start_addr = (void *)code + nheader_words*N_WORD_BYTES;
1617 code_end_addr = (void *)code + nwords*N_WORD_BYTES;
1619 /* Work through the unboxed code. */
1620 for (p = code_start_addr; p < code_end_addr; p++) {
1621 void *data = *(void **)p;
1622 unsigned d1 = *((unsigned char *)p - 1);
1623 unsigned d2 = *((unsigned char *)p - 2);
1624 unsigned d3 = *((unsigned char *)p - 3);
1625 unsigned d4 = *((unsigned char *)p - 4);
1627 unsigned d5 = *((unsigned char *)p - 5);
1628 unsigned d6 = *((unsigned char *)p - 6);
1631 /* Check for code references. */
1632 /* Check for a 32 bit word that looks like an absolute
1633 reference to within the code adea of the code object. */
1634 if ((data >= (code_start_addr-displacement))
1635 && (data < (code_end_addr-displacement))) {
1636 /* function header */
1638 && (((unsigned)p - 4 - 4*HeaderValue(*((unsigned *)p-1))) ==
1640 /* Skip the function header */
1644 /* the case of PUSH imm32 */
1648 "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1649 p, d6, d5, d4, d3, d2, d1, data));
1650 FSHOW((stderr, "/PUSH $0x%.8x\n", data));
1652 /* the case of MOV [reg-8],imm32 */
1654 && (d2==0x40 || d2==0x41 || d2==0x42 || d2==0x43
1655 || d2==0x45 || d2==0x46 || d2==0x47)
1659 "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1660 p, d6, d5, d4, d3, d2, d1, data));
1661 FSHOW((stderr, "/MOV [reg-8],$0x%.8x\n", data));
1663 /* the case of LEA reg,[disp32] */
1664 if ((d2 == 0x8d) && ((d1 & 0xc7) == 5)) {
1667 "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1668 p, d6, d5, d4, d3, d2, d1, data));
1669 FSHOW((stderr,"/LEA reg,[$0x%.8x]\n", data));
1673 /* Check for constant references. */
1674 /* Check for a 32 bit word that looks like an absolute
1675 reference to within the constant vector. Constant references
1677 if ((data >= (constants_start_addr-displacement))
1678 && (data < (constants_end_addr-displacement))
1679 && (((unsigned)data & 0x3) == 0)) {
1684 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1685 p, d6, d5, d4, d3, d2, d1, data));
1686 FSHOW((stderr,"/MOV eax,0x%.8x\n", data));
1689 /* the case of MOV m32,EAX */
1693 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1694 p, d6, d5, d4, d3, d2, d1, data));
1695 FSHOW((stderr, "/MOV 0x%.8x,eax\n", data));
1698 /* the case of CMP m32,imm32 */
1699 if ((d1 == 0x3d) && (d2 == 0x81)) {
1702 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1703 p, d6, d5, d4, d3, d2, d1, data));
1705 FSHOW((stderr, "/CMP 0x%.8x,immed32\n", data));
1708 /* Check for a mod=00, r/m=101 byte. */
1709 if ((d1 & 0xc7) == 5) {
1714 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1715 p, d6, d5, d4, d3, d2, d1, data));
1716 FSHOW((stderr,"/CMP 0x%.8x,reg\n", data));
1718 /* the case of CMP reg32,m32 */
1722 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1723 p, d6, d5, d4, d3, d2, d1, data));
1724 FSHOW((stderr, "/CMP reg32,0x%.8x\n", data));
1726 /* the case of MOV m32,reg32 */
1730 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1731 p, d6, d5, d4, d3, d2, d1, data));
1732 FSHOW((stderr, "/MOV 0x%.8x,reg32\n", data));
1734 /* the case of MOV reg32,m32 */
1738 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1739 p, d6, d5, d4, d3, d2, d1, data));
1740 FSHOW((stderr, "/MOV reg32,0x%.8x\n", data));
1742 /* the case of LEA reg32,m32 */
1746 "abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1747 p, d6, d5, d4, d3, d2, d1, data));
1748 FSHOW((stderr, "/LEA reg32,0x%.8x\n", data));
1754 /* If anything was found, print some information on the code
1758 "/compiled code object at %x: header words = %d, code words = %d\n",
1759 code, nheader_words, ncode_words));
1761 "/const start = %x, end = %x\n",
1762 constants_start_addr, constants_end_addr));
1764 "/code start = %x, end = %x\n",
1765 code_start_addr, code_end_addr));
1771 gencgc_apply_code_fixups(struct code *old_code, struct code *new_code)
1773 /* x86-64 uses pc-relative addressing instead of this kludge */
1774 #ifndef LISP_FEATURE_X86_64
1775 long nheader_words, ncode_words, nwords;
1776 void *constants_start_addr, *constants_end_addr;
1777 void *code_start_addr, *code_end_addr;
1778 lispobj fixups = NIL;
1779 unsigned long displacement =
1780 (unsigned long)new_code - (unsigned long)old_code;
1781 struct vector *fixups_vector;
1783 ncode_words = fixnum_value(new_code->code_size);
1784 nheader_words = HeaderValue(*(lispobj *)new_code);
1785 nwords = ncode_words + nheader_words;
1787 "/compiled code object at %x: header words = %d, code words = %d\n",
1788 new_code, nheader_words, ncode_words)); */
1789 constants_start_addr = (void *)new_code + 5*N_WORD_BYTES;
1790 constants_end_addr = (void *)new_code + nheader_words*N_WORD_BYTES;
1791 code_start_addr = (void *)new_code + nheader_words*N_WORD_BYTES;
1792 code_end_addr = (void *)new_code + nwords*N_WORD_BYTES;
1795 "/const start = %x, end = %x\n",
1796 constants_start_addr,constants_end_addr));
1798 "/code start = %x; end = %x\n",
1799 code_start_addr,code_end_addr));
1802 /* The first constant should be a pointer to the fixups for this
1803 code objects. Check. */
1804 fixups = new_code->constants[0];
1806 /* It will be 0 or the unbound-marker if there are no fixups (as
1807 * will be the case if the code object has been purified, for
1808 * example) and will be an other pointer if it is valid. */
1809 if ((fixups == 0) || (fixups == UNBOUND_MARKER_WIDETAG) ||
1810 !is_lisp_pointer(fixups)) {
1811 /* Check for possible errors. */
1812 if (check_code_fixups)
1813 sniff_code_object(new_code, displacement);
1818 fixups_vector = (struct vector *)native_pointer(fixups);
1820 /* Could be pointing to a forwarding pointer. */
1821 /* FIXME is this always in from_space? if so, could replace this code with
1822 * forwarding_pointer_p/forwarding_pointer_value */
1823 if (is_lisp_pointer(fixups) &&
1824 (find_page_index((void*)fixups_vector) != -1) &&
1825 (fixups_vector->header == 0x01)) {
1826 /* If so, then follow it. */
1827 /*SHOW("following pointer to a forwarding pointer");*/
1829 (struct vector *)native_pointer((lispobj)fixups_vector->length);
1832 /*SHOW("got fixups");*/
1834 if (widetag_of(fixups_vector->header) == SIMPLE_ARRAY_WORD_WIDETAG) {
1835 /* Got the fixups for the code block. Now work through the vector,
1836 and apply a fixup at each address. */
1837 long length = fixnum_value(fixups_vector->length);
1839 for (i = 0; i < length; i++) {
1840 unsigned long offset = fixups_vector->data[i];
1841 /* Now check the current value of offset. */
1842 unsigned long old_value =
1843 *(unsigned long *)((unsigned long)code_start_addr + offset);
1845 /* If it's within the old_code object then it must be an
1846 * absolute fixup (relative ones are not saved) */
1847 if ((old_value >= (unsigned long)old_code)
1848 && (old_value < ((unsigned long)old_code
1849 + nwords*N_WORD_BYTES)))
1850 /* So add the dispacement. */
1851 *(unsigned long *)((unsigned long)code_start_addr + offset) =
1852 old_value + displacement;
1854 /* It is outside the old code object so it must be a
1855 * relative fixup (absolute fixups are not saved). So
1856 * subtract the displacement. */
1857 *(unsigned long *)((unsigned long)code_start_addr + offset) =
1858 old_value - displacement;
1861 /* This used to just print a note to stderr, but a bogus fixup seems to
1862 * indicate real heap corruption, so a hard hailure is in order. */
1863 lose("fixup vector %p has a bad widetag: %d\n",
1864 fixups_vector, widetag_of(fixups_vector->header));
1867 /* Check for possible errors. */
1868 if (check_code_fixups) {
1869 sniff_code_object(new_code,displacement);
1876 trans_boxed_large(lispobj object)
1879 unsigned long length;
1881 gc_assert(is_lisp_pointer(object));
1883 header = *((lispobj *) native_pointer(object));
1884 length = HeaderValue(header) + 1;
1885 length = CEILING(length, 2);
1887 return copy_large_object(object, length);
1890 /* Doesn't seem to be used, delete it after the grace period. */
1893 trans_unboxed_large(lispobj object)
1896 unsigned long length;
1898 gc_assert(is_lisp_pointer(object));
1900 header = *((lispobj *) native_pointer(object));
1901 length = HeaderValue(header) + 1;
1902 length = CEILING(length, 2);
1904 return copy_large_unboxed_object(object, length);
1910 * Lutexes. Using the normal finalization machinery for finalizing
1911 * lutexes is tricky, since the finalization depends on working lutexes.
1912 * So we track the lutexes in the GC and finalize them manually.
1915 #if defined(LUTEX_WIDETAG)
1918 * Start tracking LUTEX in the GC, by adding it to the linked list of
1919 * lutexes in the nursery generation. The caller is responsible for
1920 * locking, and GCs must be inhibited until the registration is
1924 gencgc_register_lutex (struct lutex *lutex) {
1925 int index = find_page_index(lutex);
1926 generation_index_t gen;
1929 /* This lutex is in static space, so we don't need to worry about
1935 gen = page_table[index].gen;
1937 gc_assert(gen >= 0);
1938 gc_assert(gen < NUM_GENERATIONS);
1940 head = generations[gen].lutexes;
1947 generations[gen].lutexes = lutex;
1951 * Stop tracking LUTEX in the GC by removing it from the appropriate
1952 * linked lists. This will only be called during GC, so no locking is
1956 gencgc_unregister_lutex (struct lutex *lutex) {
1958 lutex->prev->next = lutex->next;
1960 generations[lutex->gen].lutexes = lutex->next;
1964 lutex->next->prev = lutex->prev;
1973 * Mark all lutexes in generation GEN as not live.
1976 unmark_lutexes (generation_index_t gen) {
1977 struct lutex *lutex = generations[gen].lutexes;
1981 lutex = lutex->next;
1986 * Finalize all lutexes in generation GEN that have not been marked live.
1989 reap_lutexes (generation_index_t gen) {
1990 struct lutex *lutex = generations[gen].lutexes;
1993 struct lutex *next = lutex->next;
1995 lutex_destroy((tagged_lutex_t) lutex);
1996 gencgc_unregister_lutex(lutex);
2003 * Mark LUTEX as live.
2006 mark_lutex (lispobj tagged_lutex) {
2007 struct lutex *lutex = (struct lutex*) native_pointer(tagged_lutex);
2013 * Move all lutexes in generation FROM to generation TO.
2016 move_lutexes (generation_index_t from, generation_index_t to) {
2017 struct lutex *tail = generations[from].lutexes;
2019 /* Nothing to move */
2023 /* Change the generation of the lutexes in FROM. */
2024 while (tail->next) {
2030 /* Link the last lutex in the FROM list to the start of the TO list */
2031 tail->next = generations[to].lutexes;
2033 /* And vice versa */
2034 if (generations[to].lutexes) {
2035 generations[to].lutexes->prev = tail;
2038 /* And update the generations structures to match this */
2039 generations[to].lutexes = generations[from].lutexes;
2040 generations[from].lutexes = NULL;
2044 scav_lutex(lispobj *where, lispobj object)
2046 mark_lutex((lispobj) where);
2048 return CEILING(sizeof(struct lutex)/sizeof(lispobj), 2);
2052 trans_lutex(lispobj object)
2054 struct lutex *lutex = (struct lutex *) native_pointer(object);
2056 size_t words = CEILING(sizeof(struct lutex)/sizeof(lispobj), 2);
2057 gc_assert(is_lisp_pointer(object));
2058 copied = copy_object(object, words);
2060 /* Update the links, since the lutex moved in memory. */
2062 lutex->next->prev = (struct lutex *) native_pointer(copied);
2066 lutex->prev->next = (struct lutex *) native_pointer(copied);
2068 generations[lutex->gen].lutexes =
2069 (struct lutex *) native_pointer(copied);
2076 size_lutex(lispobj *where)
2078 return CEILING(sizeof(struct lutex)/sizeof(lispobj), 2);
2080 #endif /* LUTEX_WIDETAG */
2087 /* XX This is a hack adapted from cgc.c. These don't work too
2088 * efficiently with the gencgc as a list of the weak pointers is
2089 * maintained within the objects which causes writes to the pages. A
2090 * limited attempt is made to avoid unnecessary writes, but this needs
2092 #define WEAK_POINTER_NWORDS \
2093 CEILING((sizeof(struct weak_pointer) / sizeof(lispobj)), 2)
2096 scav_weak_pointer(lispobj *where, lispobj object)
2098 /* Since we overwrite the 'next' field, we have to make
2099 * sure not to do so for pointers already in the list.
2100 * Instead of searching the list of weak_pointers each
2101 * time, we ensure that next is always NULL when the weak
2102 * pointer isn't in the list, and not NULL otherwise.
2103 * Since we can't use NULL to denote end of list, we
2104 * use a pointer back to the same weak_pointer.
2106 struct weak_pointer * wp = (struct weak_pointer*)where;
2108 if (NULL == wp->next) {
2109 wp->next = weak_pointers;
2111 if (NULL == wp->next)
2115 /* Do not let GC scavenge the value slot of the weak pointer.
2116 * (That is why it is a weak pointer.) */
2118 return WEAK_POINTER_NWORDS;
2123 search_read_only_space(void *pointer)
2125 lispobj *start = (lispobj *) READ_ONLY_SPACE_START;
2126 lispobj *end = (lispobj *) SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0);
2127 if ((pointer < (void *)start) || (pointer >= (void *)end))
2129 return (gc_search_space(start,
2130 (((lispobj *)pointer)+2)-start,
2131 (lispobj *) pointer));
2135 search_static_space(void *pointer)
2137 lispobj *start = (lispobj *)STATIC_SPACE_START;
2138 lispobj *end = (lispobj *)SymbolValue(STATIC_SPACE_FREE_POINTER,0);
2139 if ((pointer < (void *)start) || (pointer >= (void *)end))
2141 return (gc_search_space(start,
2142 (((lispobj *)pointer)+2)-start,
2143 (lispobj *) pointer));
2146 /* a faster version for searching the dynamic space. This will work even
2147 * if the object is in a current allocation region. */
2149 search_dynamic_space(void *pointer)
2151 page_index_t page_index = find_page_index(pointer);
2154 /* The address may be invalid, so do some checks. */
2155 if ((page_index == -1) ||
2156 (page_table[page_index].allocated == FREE_PAGE_FLAG))
2158 start = (lispobj *)page_region_start(page_index);
2159 return (gc_search_space(start,
2160 (((lispobj *)pointer)+2)-start,
2161 (lispobj *)pointer));
2164 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
2166 /* Helper for valid_lisp_pointer_p and
2167 * possibly_valid_dynamic_space_pointer.
2169 * pointer is the pointer to validate, and start_addr is the address
2170 * of the enclosing object.
2173 looks_like_valid_lisp_pointer_p(lispobj *pointer, lispobj *start_addr)
2175 /* We need to allow raw pointers into Code objects for return
2176 * addresses. This will also pick up pointers to functions in code
2178 if (widetag_of(*start_addr) == CODE_HEADER_WIDETAG)
2179 /* XXX could do some further checks here */
2182 if (!is_lisp_pointer((lispobj)pointer)) {
2186 /* Check that the object pointed to is consistent with the pointer
2188 switch (lowtag_of((lispobj)pointer)) {
2189 case FUN_POINTER_LOWTAG:
2190 /* Start_addr should be the enclosing code object, or a closure
2192 switch (widetag_of(*start_addr)) {
2193 case CODE_HEADER_WIDETAG:
2194 /* This case is probably caught above. */
2196 case CLOSURE_HEADER_WIDETAG:
2197 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
2198 if ((unsigned long)pointer !=
2199 ((unsigned long)start_addr+FUN_POINTER_LOWTAG)) {
2203 pointer, start_addr, *start_addr));
2211 pointer, start_addr, *start_addr));
2215 case LIST_POINTER_LOWTAG:
2216 if ((unsigned long)pointer !=
2217 ((unsigned long)start_addr+LIST_POINTER_LOWTAG)) {
2221 pointer, start_addr, *start_addr));
2224 /* Is it plausible cons? */
2225 if ((is_lisp_pointer(start_addr[0]) ||
2226 is_lisp_immediate(start_addr[0])) &&
2227 (is_lisp_pointer(start_addr[1]) ||
2228 is_lisp_immediate(start_addr[1])))
2234 pointer, start_addr, *start_addr));
2237 case INSTANCE_POINTER_LOWTAG:
2238 if ((unsigned long)pointer !=
2239 ((unsigned long)start_addr+INSTANCE_POINTER_LOWTAG)) {
2243 pointer, start_addr, *start_addr));
2246 if (widetag_of(start_addr[0]) != INSTANCE_HEADER_WIDETAG) {
2250 pointer, start_addr, *start_addr));
2254 case OTHER_POINTER_LOWTAG:
2255 if ((unsigned long)pointer !=
2256 ((unsigned long)start_addr+OTHER_POINTER_LOWTAG)) {
2260 pointer, start_addr, *start_addr));
2263 /* Is it plausible? Not a cons. XXX should check the headers. */
2264 if (is_lisp_pointer(start_addr[0]) || ((start_addr[0] & 3) == 0)) {
2268 pointer, start_addr, *start_addr));
2271 switch (widetag_of(start_addr[0])) {
2272 case UNBOUND_MARKER_WIDETAG:
2273 case NO_TLS_VALUE_MARKER_WIDETAG:
2274 case CHARACTER_WIDETAG:
2275 #if N_WORD_BITS == 64
2276 case SINGLE_FLOAT_WIDETAG:
2281 pointer, start_addr, *start_addr));
2284 /* only pointed to by function pointers? */
2285 case CLOSURE_HEADER_WIDETAG:
2286 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
2290 pointer, start_addr, *start_addr));
2293 case INSTANCE_HEADER_WIDETAG:
2297 pointer, start_addr, *start_addr));
2300 /* the valid other immediate pointer objects */
2301 case SIMPLE_VECTOR_WIDETAG:
2303 case COMPLEX_WIDETAG:
2304 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
2305 case COMPLEX_SINGLE_FLOAT_WIDETAG:
2307 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
2308 case COMPLEX_DOUBLE_FLOAT_WIDETAG:
2310 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
2311 case COMPLEX_LONG_FLOAT_WIDETAG:
2313 case SIMPLE_ARRAY_WIDETAG:
2314 case COMPLEX_BASE_STRING_WIDETAG:
2315 #ifdef COMPLEX_CHARACTER_STRING_WIDETAG
2316 case COMPLEX_CHARACTER_STRING_WIDETAG:
2318 case COMPLEX_VECTOR_NIL_WIDETAG:
2319 case COMPLEX_BIT_VECTOR_WIDETAG:
2320 case COMPLEX_VECTOR_WIDETAG:
2321 case COMPLEX_ARRAY_WIDETAG:
2322 case VALUE_CELL_HEADER_WIDETAG:
2323 case SYMBOL_HEADER_WIDETAG:
2325 case CODE_HEADER_WIDETAG:
2326 case BIGNUM_WIDETAG:
2327 #if N_WORD_BITS != 64
2328 case SINGLE_FLOAT_WIDETAG:
2330 case DOUBLE_FLOAT_WIDETAG:
2331 #ifdef LONG_FLOAT_WIDETAG
2332 case LONG_FLOAT_WIDETAG:
2334 case SIMPLE_BASE_STRING_WIDETAG:
2335 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
2336 case SIMPLE_CHARACTER_STRING_WIDETAG:
2338 case SIMPLE_BIT_VECTOR_WIDETAG:
2339 case SIMPLE_ARRAY_NIL_WIDETAG:
2340 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
2341 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
2342 case SIMPLE_ARRAY_UNSIGNED_BYTE_7_WIDETAG:
2343 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
2344 case SIMPLE_ARRAY_UNSIGNED_BYTE_15_WIDETAG:
2345 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
2346 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_29_WIDETAG
2347 case SIMPLE_ARRAY_UNSIGNED_BYTE_29_WIDETAG:
2349 case SIMPLE_ARRAY_UNSIGNED_BYTE_31_WIDETAG:
2350 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
2351 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_60_WIDETAG
2352 case SIMPLE_ARRAY_UNSIGNED_BYTE_60_WIDETAG:
2354 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG
2355 case SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG:
2357 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG
2358 case SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG:
2360 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
2361 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
2363 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
2364 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
2366 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
2367 case SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG:
2369 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
2370 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
2372 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_61_WIDETAG
2373 case SIMPLE_ARRAY_SIGNED_BYTE_61_WIDETAG:
2375 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG
2376 case SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG:
2378 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
2379 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
2380 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
2381 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
2383 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
2384 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
2386 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
2387 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
2389 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
2390 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
2393 case WEAK_POINTER_WIDETAG:
2394 #ifdef LUTEX_WIDETAG
2403 pointer, start_addr, *start_addr));
2411 pointer, start_addr, *start_addr));
2419 /* Used by the debugger to validate possibly bogus pointers before
2420 * calling MAKE-LISP-OBJ on them.
2422 * FIXME: We would like to make this perfect, because if the debugger
2423 * constructs a reference to a bugs lisp object, and it ends up in a
2424 * location scavenged by the GC all hell breaks loose.
2426 * Whereas possibly_valid_dynamic_space_pointer has to be conservative
2427 * and return true for all valid pointers, this could actually be eager
2428 * and lie about a few pointers without bad results... but that should
2429 * be reflected in the name.
2432 valid_lisp_pointer_p(lispobj *pointer)
2435 if (((start=search_dynamic_space(pointer))!=NULL) ||
2436 ((start=search_static_space(pointer))!=NULL) ||
2437 ((start=search_read_only_space(pointer))!=NULL))
2438 return looks_like_valid_lisp_pointer_p(pointer, start);
2443 /* Is there any possibility that pointer is a valid Lisp object
2444 * reference, and/or something else (e.g. subroutine call return
2445 * address) which should prevent us from moving the referred-to thing?
2446 * This is called from preserve_pointers() */
2448 possibly_valid_dynamic_space_pointer(lispobj *pointer)
2450 lispobj *start_addr;
2452 /* Find the object start address. */
2453 if ((start_addr = search_dynamic_space(pointer)) == NULL) {
2457 return looks_like_valid_lisp_pointer_p(pointer, start_addr);
2460 /* Adjust large bignum and vector objects. This will adjust the
2461 * allocated region if the size has shrunk, and move unboxed objects
2462 * into unboxed pages. The pages are not promoted here, and the
2463 * promoted region is not added to the new_regions; this is really
2464 * only designed to be called from preserve_pointer(). Shouldn't fail
2465 * if this is missed, just may delay the moving of objects to unboxed
2466 * pages, and the freeing of pages. */
2468 maybe_adjust_large_object(lispobj *where)
2470 page_index_t first_page;
2471 page_index_t next_page;
2474 unsigned long remaining_bytes;
2475 unsigned long bytes_freed;
2476 unsigned long old_bytes_used;
2480 /* Check whether it's a vector or bignum object. */
2481 switch (widetag_of(where[0])) {
2482 case SIMPLE_VECTOR_WIDETAG:
2483 boxed = BOXED_PAGE_FLAG;
2485 case BIGNUM_WIDETAG:
2486 case SIMPLE_BASE_STRING_WIDETAG:
2487 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
2488 case SIMPLE_CHARACTER_STRING_WIDETAG:
2490 case SIMPLE_BIT_VECTOR_WIDETAG:
2491 case SIMPLE_ARRAY_NIL_WIDETAG:
2492 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
2493 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
2494 case SIMPLE_ARRAY_UNSIGNED_BYTE_7_WIDETAG:
2495 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
2496 case SIMPLE_ARRAY_UNSIGNED_BYTE_15_WIDETAG:
2497 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
2498 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_29_WIDETAG
2499 case SIMPLE_ARRAY_UNSIGNED_BYTE_29_WIDETAG:
2501 case SIMPLE_ARRAY_UNSIGNED_BYTE_31_WIDETAG:
2502 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
2503 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_60_WIDETAG
2504 case SIMPLE_ARRAY_UNSIGNED_BYTE_60_WIDETAG:
2506 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG
2507 case SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG:
2509 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG
2510 case SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG:
2512 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
2513 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
2515 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
2516 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
2518 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
2519 case SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG:
2521 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
2522 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
2524 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_61_WIDETAG
2525 case SIMPLE_ARRAY_SIGNED_BYTE_61_WIDETAG:
2527 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG
2528 case SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG:
2530 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
2531 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
2532 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
2533 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
2535 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
2536 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
2538 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
2539 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
2541 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
2542 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
2544 boxed = UNBOXED_PAGE_FLAG;
2550 /* Find its current size. */
2551 nwords = (sizetab[widetag_of(where[0])])(where);
2553 first_page = find_page_index((void *)where);
2554 gc_assert(first_page >= 0);
2556 /* Note: Any page write-protection must be removed, else a later
2557 * scavenge_newspace may incorrectly not scavenge these pages.
2558 * This would not be necessary if they are added to the new areas,
2559 * but lets do it for them all (they'll probably be written
2562 gc_assert(page_table[first_page].region_start_offset == 0);
2564 next_page = first_page;
2565 remaining_bytes = nwords*N_WORD_BYTES;
2566 while (remaining_bytes > PAGE_BYTES) {
2567 gc_assert(page_table[next_page].gen == from_space);
2568 gc_assert((page_table[next_page].allocated == BOXED_PAGE_FLAG)
2569 || (page_table[next_page].allocated == UNBOXED_PAGE_FLAG));
2570 gc_assert(page_table[next_page].large_object);
2571 gc_assert(page_table[next_page].region_start_offset ==
2572 npage_bytes(next_page-first_page));
2573 gc_assert(page_table[next_page].bytes_used == PAGE_BYTES);
2575 page_table[next_page].allocated = boxed;
2577 /* Shouldn't be write-protected at this stage. Essential that the
2579 gc_assert(!page_table[next_page].write_protected);
2580 remaining_bytes -= PAGE_BYTES;
2584 /* Now only one page remains, but the object may have shrunk so
2585 * there may be more unused pages which will be freed. */
2587 /* Object may have shrunk but shouldn't have grown - check. */
2588 gc_assert(page_table[next_page].bytes_used >= remaining_bytes);
2590 page_table[next_page].allocated = boxed;
2591 gc_assert(page_table[next_page].allocated ==
2592 page_table[first_page].allocated);
2594 /* Adjust the bytes_used. */
2595 old_bytes_used = page_table[next_page].bytes_used;
2596 page_table[next_page].bytes_used = remaining_bytes;
2598 bytes_freed = old_bytes_used - remaining_bytes;
2600 /* Free any remaining pages; needs care. */
2602 while ((old_bytes_used == PAGE_BYTES) &&
2603 (page_table[next_page].gen == from_space) &&
2604 ((page_table[next_page].allocated == UNBOXED_PAGE_FLAG)
2605 || (page_table[next_page].allocated == BOXED_PAGE_FLAG)) &&
2606 page_table[next_page].large_object &&
2607 (page_table[next_page].region_start_offset ==
2608 npage_bytes(next_page - first_page))) {
2609 /* It checks out OK, free the page. We don't need to both zeroing
2610 * pages as this should have been done before shrinking the
2611 * object. These pages shouldn't be write protected as they
2612 * should be zero filled. */
2613 gc_assert(page_table[next_page].write_protected == 0);
2615 old_bytes_used = page_table[next_page].bytes_used;
2616 page_table[next_page].allocated = FREE_PAGE_FLAG;
2617 page_table[next_page].bytes_used = 0;
2618 bytes_freed += old_bytes_used;
2622 if ((bytes_freed > 0) && gencgc_verbose) {
2624 "/maybe_adjust_large_object() freed %d\n",
2628 generations[from_space].bytes_allocated -= bytes_freed;
2629 bytes_allocated -= bytes_freed;
2634 /* Take a possible pointer to a Lisp object and mark its page in the
2635 * page_table so that it will not be relocated during a GC.
2637 * This involves locating the page it points to, then backing up to
2638 * the start of its region, then marking all pages dont_move from there
2639 * up to the first page that's not full or has a different generation
2641 * It is assumed that all the page static flags have been cleared at
2642 * the start of a GC.
2644 * It is also assumed that the current gc_alloc() region has been
2645 * flushed and the tables updated. */
2648 preserve_pointer(void *addr)
2650 page_index_t addr_page_index = find_page_index(addr);
2651 page_index_t first_page;
2653 unsigned int region_allocation;
2655 /* quick check 1: Address is quite likely to have been invalid. */
2656 if ((addr_page_index == -1)
2657 || (page_table[addr_page_index].allocated == FREE_PAGE_FLAG)
2658 || (page_table[addr_page_index].bytes_used == 0)
2659 || (page_table[addr_page_index].gen != from_space)
2660 /* Skip if already marked dont_move. */
2661 || (page_table[addr_page_index].dont_move != 0))
2663 gc_assert(!(page_table[addr_page_index].allocated&OPEN_REGION_PAGE_FLAG));
2664 /* (Now that we know that addr_page_index is in range, it's
2665 * safe to index into page_table[] with it.) */
2666 region_allocation = page_table[addr_page_index].allocated;
2668 /* quick check 2: Check the offset within the page.
2671 if (((unsigned long)addr & (PAGE_BYTES - 1)) >
2672 page_table[addr_page_index].bytes_used)
2675 /* Filter out anything which can't be a pointer to a Lisp object
2676 * (or, as a special case which also requires dont_move, a return
2677 * address referring to something in a CodeObject). This is
2678 * expensive but important, since it vastly reduces the
2679 * probability that random garbage will be bogusly interpreted as
2680 * a pointer which prevents a page from moving. */
2681 if (!(possibly_valid_dynamic_space_pointer(addr)))
2684 /* Find the beginning of the region. Note that there may be
2685 * objects in the region preceding the one that we were passed a
2686 * pointer to: if this is the case, we will write-protect all the
2687 * previous objects' pages too. */
2690 /* I think this'd work just as well, but without the assertions.
2691 * -dan 2004.01.01 */
2692 first_page = find_page_index(page_region_start(addr_page_index))
2694 first_page = addr_page_index;
2695 while (page_table[first_page].region_start_offset != 0) {
2697 /* Do some checks. */
2698 gc_assert(page_table[first_page].bytes_used == PAGE_BYTES);
2699 gc_assert(page_table[first_page].gen == from_space);
2700 gc_assert(page_table[first_page].allocated == region_allocation);
2704 /* Adjust any large objects before promotion as they won't be
2705 * copied after promotion. */
2706 if (page_table[first_page].large_object) {
2707 maybe_adjust_large_object(page_address(first_page));
2708 /* If a large object has shrunk then addr may now point to a
2709 * free area in which case it's ignored here. Note it gets
2710 * through the valid pointer test above because the tail looks
2712 if ((page_table[addr_page_index].allocated == FREE_PAGE_FLAG)
2713 || (page_table[addr_page_index].bytes_used == 0)
2714 /* Check the offset within the page. */
2715 || (((unsigned long)addr & (PAGE_BYTES - 1))
2716 > page_table[addr_page_index].bytes_used)) {
2718 "weird? ignore ptr 0x%x to freed area of large object\n",
2722 /* It may have moved to unboxed pages. */
2723 region_allocation = page_table[first_page].allocated;
2726 /* Now work forward until the end of this contiguous area is found,
2727 * marking all pages as dont_move. */
2728 for (i = first_page; ;i++) {
2729 gc_assert(page_table[i].allocated == region_allocation);
2731 /* Mark the page static. */
2732 page_table[i].dont_move = 1;
2734 /* Move the page to the new_space. XX I'd rather not do this
2735 * but the GC logic is not quite able to copy with the static
2736 * pages remaining in the from space. This also requires the
2737 * generation bytes_allocated counters be updated. */
2738 page_table[i].gen = new_space;
2739 generations[new_space].bytes_allocated += page_table[i].bytes_used;
2740 generations[from_space].bytes_allocated -= page_table[i].bytes_used;
2742 /* It is essential that the pages are not write protected as
2743 * they may have pointers into the old-space which need
2744 * scavenging. They shouldn't be write protected at this
2746 gc_assert(!page_table[i].write_protected);
2748 /* Check whether this is the last page in this contiguous block.. */
2749 if ((page_table[i].bytes_used < PAGE_BYTES)
2750 /* ..or it is PAGE_BYTES and is the last in the block */
2751 || (page_table[i+1].allocated == FREE_PAGE_FLAG)
2752 || (page_table[i+1].bytes_used == 0) /* next page free */
2753 || (page_table[i+1].gen != from_space) /* diff. gen */
2754 || (page_table[i+1].region_start_offset == 0))
2758 /* Check that the page is now static. */
2759 gc_assert(page_table[addr_page_index].dont_move != 0);
2762 #endif // defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
2765 /* If the given page is not write-protected, then scan it for pointers
2766 * to younger generations or the top temp. generation, if no
2767 * suspicious pointers are found then the page is write-protected.
2769 * Care is taken to check for pointers to the current gc_alloc()
2770 * region if it is a younger generation or the temp. generation. This
2771 * frees the caller from doing a gc_alloc_update_page_tables(). Actually
2772 * the gc_alloc_generation does not need to be checked as this is only
2773 * called from scavenge_generation() when the gc_alloc generation is
2774 * younger, so it just checks if there is a pointer to the current
2777 * We return 1 if the page was write-protected, else 0. */
2779 update_page_write_prot(page_index_t page)
2781 generation_index_t gen = page_table[page].gen;
2784 void **page_addr = (void **)page_address(page);
2785 long num_words = page_table[page].bytes_used / N_WORD_BYTES;
2787 /* Shouldn't be a free page. */
2788 gc_assert(page_table[page].allocated != FREE_PAGE_FLAG);
2789 gc_assert(page_table[page].bytes_used != 0);
2791 /* Skip if it's already write-protected, pinned, or unboxed */
2792 if (page_table[page].write_protected
2793 /* FIXME: What's the reason for not write-protecting pinned pages? */
2794 || page_table[page].dont_move
2795 || (page_table[page].allocated & UNBOXED_PAGE_FLAG))
2798 /* Scan the page for pointers to younger generations or the
2799 * top temp. generation. */
2801 for (j = 0; j < num_words; j++) {
2802 void *ptr = *(page_addr+j);
2803 page_index_t index = find_page_index(ptr);
2805 /* Check that it's in the dynamic space */
2807 if (/* Does it point to a younger or the temp. generation? */
2808 ((page_table[index].allocated != FREE_PAGE_FLAG)
2809 && (page_table[index].bytes_used != 0)
2810 && ((page_table[index].gen < gen)
2811 || (page_table[index].gen == SCRATCH_GENERATION)))
2813 /* Or does it point within a current gc_alloc() region? */
2814 || ((boxed_region.start_addr <= ptr)
2815 && (ptr <= boxed_region.free_pointer))
2816 || ((unboxed_region.start_addr <= ptr)
2817 && (ptr <= unboxed_region.free_pointer))) {
2824 /* Write-protect the page. */
2825 /*FSHOW((stderr, "/write-protecting page %d gen %d\n", page, gen));*/
2827 os_protect((void *)page_addr,
2829 OS_VM_PROT_READ|OS_VM_PROT_EXECUTE);
2831 /* Note the page as protected in the page tables. */
2832 page_table[page].write_protected = 1;
2838 /* Scavenge all generations from FROM to TO, inclusive, except for
2839 * new_space which needs special handling, as new objects may be
2840 * added which are not checked here - use scavenge_newspace generation.
2842 * Write-protected pages should not have any pointers to the
2843 * from_space so do need scavenging; thus write-protected pages are
2844 * not always scavenged. There is some code to check that these pages
2845 * are not written; but to check fully the write-protected pages need
2846 * to be scavenged by disabling the code to skip them.
2848 * Under the current scheme when a generation is GCed the younger
2849 * generations will be empty. So, when a generation is being GCed it
2850 * is only necessary to scavenge the older generations for pointers
2851 * not the younger. So a page that does not have pointers to younger
2852 * generations does not need to be scavenged.
2854 * The write-protection can be used to note pages that don't have
2855 * pointers to younger pages. But pages can be written without having
2856 * pointers to younger generations. After the pages are scavenged here
2857 * they can be scanned for pointers to younger generations and if
2858 * there are none the page can be write-protected.
2860 * One complication is when the newspace is the top temp. generation.
2862 * Enabling SC_GEN_CK scavenges the write-protected pages and checks
2863 * that none were written, which they shouldn't be as they should have
2864 * no pointers to younger generations. This breaks down for weak
2865 * pointers as the objects contain a link to the next and are written
2866 * if a weak pointer is scavenged. Still it's a useful check. */
2868 scavenge_generations(generation_index_t from, generation_index_t to)
2875 /* Clear the write_protected_cleared flags on all pages. */
2876 for (i = 0; i < page_table_pages; i++)
2877 page_table[i].write_protected_cleared = 0;
2880 for (i = 0; i < last_free_page; i++) {
2881 generation_index_t generation = page_table[i].gen;
2882 if ((page_table[i].allocated & BOXED_PAGE_FLAG)
2883 && (page_table[i].bytes_used != 0)
2884 && (generation != new_space)
2885 && (generation >= from)
2886 && (generation <= to)) {
2887 page_index_t last_page,j;
2888 int write_protected=1;
2890 /* This should be the start of a region */
2891 gc_assert(page_table[i].region_start_offset == 0);
2893 /* Now work forward until the end of the region */
2894 for (last_page = i; ; last_page++) {
2896 write_protected && page_table[last_page].write_protected;
2897 if ((page_table[last_page].bytes_used < PAGE_BYTES)
2898 /* Or it is PAGE_BYTES and is the last in the block */
2899 || (!(page_table[last_page+1].allocated & BOXED_PAGE_FLAG))
2900 || (page_table[last_page+1].bytes_used == 0)
2901 || (page_table[last_page+1].gen != generation)
2902 || (page_table[last_page+1].region_start_offset == 0))
2905 if (!write_protected) {
2906 scavenge(page_address(i),
2907 ((unsigned long)(page_table[last_page].bytes_used
2908 + npage_bytes(last_page-i)))
2911 /* Now scan the pages and write protect those that
2912 * don't have pointers to younger generations. */
2913 if (enable_page_protection) {
2914 for (j = i; j <= last_page; j++) {
2915 num_wp += update_page_write_prot(j);
2918 if ((gencgc_verbose > 1) && (num_wp != 0)) {
2920 "/write protected %d pages within generation %d\n",
2921 num_wp, generation));
2929 /* Check that none of the write_protected pages in this generation
2930 * have been written to. */
2931 for (i = 0; i < page_table_pages; i++) {
2932 if ((page_table[i].allocation != FREE_PAGE_FLAG)
2933 && (page_table[i].bytes_used != 0)
2934 && (page_table[i].gen == generation)
2935 && (page_table[i].write_protected_cleared != 0)) {
2936 FSHOW((stderr, "/scavenge_generation() %d\n", generation));
2938 "/page bytes_used=%d region_start_offset=%lu dont_move=%d\n",
2939 page_table[i].bytes_used,
2940 page_table[i].region_start_offset,
2941 page_table[i].dont_move));
2942 lose("write to protected page %d in scavenge_generation()\n", i);
2949 /* Scavenge a newspace generation. As it is scavenged new objects may
2950 * be allocated to it; these will also need to be scavenged. This
2951 * repeats until there are no more objects unscavenged in the
2952 * newspace generation.
2954 * To help improve the efficiency, areas written are recorded by
2955 * gc_alloc() and only these scavenged. Sometimes a little more will be
2956 * scavenged, but this causes no harm. An easy check is done that the
2957 * scavenged bytes equals the number allocated in the previous
2960 * Write-protected pages are not scanned except if they are marked
2961 * dont_move in which case they may have been promoted and still have
2962 * pointers to the from space.
2964 * Write-protected pages could potentially be written by alloc however
2965 * to avoid having to handle re-scavenging of write-protected pages
2966 * gc_alloc() does not write to write-protected pages.
2968 * New areas of objects allocated are recorded alternatively in the two
2969 * new_areas arrays below. */
2970 static struct new_area new_areas_1[NUM_NEW_AREAS];
2971 static struct new_area new_areas_2[NUM_NEW_AREAS];
2973 /* Do one full scan of the new space generation. This is not enough to
2974 * complete the job as new objects may be added to the generation in
2975 * the process which are not scavenged. */
2977 scavenge_newspace_generation_one_scan(generation_index_t generation)
2982 "/starting one full scan of newspace generation %d\n",
2984 for (i = 0; i < last_free_page; i++) {
2985 /* Note that this skips over open regions when it encounters them. */
2986 if ((page_table[i].allocated & BOXED_PAGE_FLAG)
2987 && (page_table[i].bytes_used != 0)
2988 && (page_table[i].gen == generation)
2989 && ((page_table[i].write_protected == 0)
2990 /* (This may be redundant as write_protected is now
2991 * cleared before promotion.) */
2992 || (page_table[i].dont_move == 1))) {
2993 page_index_t last_page;
2996 /* The scavenge will start at the region_start_offset of
2999 * We need to find the full extent of this contiguous
3000 * block in case objects span pages.
3002 * Now work forward until the end of this contiguous area
3003 * is found. A small area is preferred as there is a
3004 * better chance of its pages being write-protected. */
3005 for (last_page = i; ;last_page++) {
3006 /* If all pages are write-protected and movable,
3007 * then no need to scavenge */
3008 all_wp=all_wp && page_table[last_page].write_protected &&
3009 !page_table[last_page].dont_move;
3011 /* Check whether this is the last page in this
3012 * contiguous block */
3013 if ((page_table[last_page].bytes_used < PAGE_BYTES)
3014 /* Or it is PAGE_BYTES and is the last in the block */
3015 || (!(page_table[last_page+1].allocated & BOXED_PAGE_FLAG))
3016 || (page_table[last_page+1].bytes_used == 0)
3017 || (page_table[last_page+1].gen != generation)
3018 || (page_table[last_page+1].region_start_offset == 0))
3022 /* Do a limited check for write-protected pages. */
3024 long nwords = (((unsigned long)
3025 (page_table[last_page].bytes_used
3026 + npage_bytes(last_page-i)
3027 + page_table[i].region_start_offset))
3029 new_areas_ignore_page = last_page;
3031 scavenge(page_region_start(i), nwords);
3038 "/done with one full scan of newspace generation %d\n",
3042 /* Do a complete scavenge of the newspace generation. */
3044 scavenge_newspace_generation(generation_index_t generation)
3048 /* the new_areas array currently being written to by gc_alloc() */
3049 struct new_area (*current_new_areas)[] = &new_areas_1;
3050 long current_new_areas_index;
3052 /* the new_areas created by the previous scavenge cycle */
3053 struct new_area (*previous_new_areas)[] = NULL;
3054 long previous_new_areas_index;
3056 /* Flush the current regions updating the tables. */
3057 gc_alloc_update_all_page_tables();
3059 /* Turn on the recording of new areas by gc_alloc(). */
3060 new_areas = current_new_areas;
3061 new_areas_index = 0;
3063 /* Don't need to record new areas that get scavenged anyway during
3064 * scavenge_newspace_generation_one_scan. */
3065 record_new_objects = 1;
3067 /* Start with a full scavenge. */
3068 scavenge_newspace_generation_one_scan(generation);
3070 /* Record all new areas now. */
3071 record_new_objects = 2;
3073 /* Give a chance to weak hash tables to make other objects live.
3074 * FIXME: The algorithm implemented here for weak hash table gcing
3075 * is O(W^2+N) as Bruno Haible warns in
3076 * http://www.haible.de/bruno/papers/cs/weak/WeakDatastructures-writeup.html
3077 * see "Implementation 2". */
3078 scav_weak_hash_tables();
3080 /* Flush the current regions updating the tables. */
3081 gc_alloc_update_all_page_tables();
3083 /* Grab new_areas_index. */
3084 current_new_areas_index = new_areas_index;
3087 "The first scan is finished; current_new_areas_index=%d.\n",
3088 current_new_areas_index));*/
3090 while (current_new_areas_index > 0) {
3091 /* Move the current to the previous new areas */
3092 previous_new_areas = current_new_areas;
3093 previous_new_areas_index = current_new_areas_index;
3095 /* Scavenge all the areas in previous new areas. Any new areas
3096 * allocated are saved in current_new_areas. */
3098 /* Allocate an array for current_new_areas; alternating between
3099 * new_areas_1 and 2 */
3100 if (previous_new_areas == &new_areas_1)
3101 current_new_areas = &new_areas_2;
3103 current_new_areas = &new_areas_1;
3105 /* Set up for gc_alloc(). */
3106 new_areas = current_new_areas;
3107 new_areas_index = 0;
3109 /* Check whether previous_new_areas had overflowed. */
3110 if (previous_new_areas_index >= NUM_NEW_AREAS) {
3112 /* New areas of objects allocated have been lost so need to do a
3113 * full scan to be sure! If this becomes a problem try
3114 * increasing NUM_NEW_AREAS. */
3116 SHOW("new_areas overflow, doing full scavenge");
3118 /* Don't need to record new areas that get scavenged
3119 * anyway during scavenge_newspace_generation_one_scan. */
3120 record_new_objects = 1;
3122 scavenge_newspace_generation_one_scan(generation);
3124 /* Record all new areas now. */
3125 record_new_objects = 2;
3127 scav_weak_hash_tables();
3129 /* Flush the current regions updating the tables. */
3130 gc_alloc_update_all_page_tables();
3134 /* Work through previous_new_areas. */
3135 for (i = 0; i < previous_new_areas_index; i++) {
3136 page_index_t page = (*previous_new_areas)[i].page;
3137 size_t offset = (*previous_new_areas)[i].offset;
3138 size_t size = (*previous_new_areas)[i].size / N_WORD_BYTES;
3139 gc_assert((*previous_new_areas)[i].size % N_WORD_BYTES == 0);
3140 scavenge(page_address(page)+offset, size);
3143 scav_weak_hash_tables();
3145 /* Flush the current regions updating the tables. */
3146 gc_alloc_update_all_page_tables();
3149 current_new_areas_index = new_areas_index;
3152 "The re-scan has finished; current_new_areas_index=%d.\n",
3153 current_new_areas_index));*/
3156 /* Turn off recording of areas allocated by gc_alloc(). */
3157 record_new_objects = 0;
3160 /* Check that none of the write_protected pages in this generation
3161 * have been written to. */
3162 for (i = 0; i < page_table_pages; i++) {
3163 if ((page_table[i].allocation != FREE_PAGE_FLAG)
3164 && (page_table[i].bytes_used != 0)
3165 && (page_table[i].gen == generation)
3166 && (page_table[i].write_protected_cleared != 0)
3167 && (page_table[i].dont_move == 0)) {
3168 lose("write protected page %d written to in scavenge_newspace_generation\ngeneration=%d dont_move=%d\n",
3169 i, generation, page_table[i].dont_move);
3175 /* Un-write-protect all the pages in from_space. This is done at the
3176 * start of a GC else there may be many page faults while scavenging
3177 * the newspace (I've seen drive the system time to 99%). These pages
3178 * would need to be unprotected anyway before unmapping in
3179 * free_oldspace; not sure what effect this has on paging.. */
3181 unprotect_oldspace(void)
3185 for (i = 0; i < last_free_page; i++) {
3186 if ((page_table[i].allocated != FREE_PAGE_FLAG)
3187 && (page_table[i].bytes_used != 0)
3188 && (page_table[i].gen == from_space)) {
3191 page_start = (void *)page_address(i);
3193 /* Remove any write-protection. We should be able to rely
3194 * on the write-protect flag to avoid redundant calls. */
3195 if (page_table[i].write_protected) {
3196 os_protect(page_start, PAGE_BYTES, OS_VM_PROT_ALL);
3197 page_table[i].write_protected = 0;
3203 /* Work through all the pages and free any in from_space. This
3204 * assumes that all objects have been copied or promoted to an older
3205 * generation. Bytes_allocated and the generation bytes_allocated
3206 * counter are updated. The number of bytes freed is returned. */
3207 static unsigned long
3210 unsigned long bytes_freed = 0;
3211 page_index_t first_page, last_page;
3216 /* Find a first page for the next region of pages. */
3217 while ((first_page < last_free_page)
3218 && ((page_table[first_page].allocated == FREE_PAGE_FLAG)
3219 || (page_table[first_page].bytes_used == 0)
3220 || (page_table[first_page].gen != from_space)))
3223 if (first_page >= last_free_page)
3226 /* Find the last page of this region. */
3227 last_page = first_page;
3230 /* Free the page. */
3231 bytes_freed += page_table[last_page].bytes_used;
3232 generations[page_table[last_page].gen].bytes_allocated -=
3233 page_table[last_page].bytes_used;
3234 page_table[last_page].allocated = FREE_PAGE_FLAG;
3235 page_table[last_page].bytes_used = 0;
3237 /* Remove any write-protection. We should be able to rely
3238 * on the write-protect flag to avoid redundant calls. */
3240 void *page_start = (void *)page_address(last_page);
3242 if (page_table[last_page].write_protected) {
3243 os_protect(page_start, PAGE_BYTES, OS_VM_PROT_ALL);
3244 page_table[last_page].write_protected = 0;
3249 while ((last_page < last_free_page)
3250 && (page_table[last_page].allocated != FREE_PAGE_FLAG)
3251 && (page_table[last_page].bytes_used != 0)
3252 && (page_table[last_page].gen == from_space));
3254 #ifdef READ_PROTECT_FREE_PAGES
3255 os_protect(page_address(first_page),
3256 npage_bytes(last_page-first_page),
3259 first_page = last_page;
3260 } while (first_page < last_free_page);
3262 bytes_allocated -= bytes_freed;
3267 /* Print some information about a pointer at the given address. */
3269 print_ptr(lispobj *addr)
3271 /* If addr is in the dynamic space then out the page information. */
3272 page_index_t pi1 = find_page_index((void*)addr);
3275 fprintf(stderr," %x: page %d alloc %d gen %d bytes_used %d offset %lu dont_move %d\n",
3276 (unsigned long) addr,
3278 page_table[pi1].allocated,
3279 page_table[pi1].gen,
3280 page_table[pi1].bytes_used,
3281 page_table[pi1].region_start_offset,
3282 page_table[pi1].dont_move);
3283 fprintf(stderr," %x %x %x %x (%x) %x %x %x %x\n",
3297 verify_space(lispobj *start, size_t words)
3299 int is_in_dynamic_space = (find_page_index((void*)start) != -1);
3300 int is_in_readonly_space =
3301 (READ_ONLY_SPACE_START <= (unsigned long)start &&
3302 (unsigned long)start < SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0));
3306 lispobj thing = *(lispobj*)start;
3308 if (is_lisp_pointer(thing)) {
3309 page_index_t page_index = find_page_index((void*)thing);
3310 long to_readonly_space =
3311 (READ_ONLY_SPACE_START <= thing &&
3312 thing < SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0));
3313 long to_static_space =
3314 (STATIC_SPACE_START <= thing &&
3315 thing < SymbolValue(STATIC_SPACE_FREE_POINTER,0));
3317 /* Does it point to the dynamic space? */
3318 if (page_index != -1) {
3319 /* If it's within the dynamic space it should point to a used
3320 * page. XX Could check the offset too. */
3321 if ((page_table[page_index].allocated != FREE_PAGE_FLAG)
3322 && (page_table[page_index].bytes_used == 0))
3323 lose ("Ptr %x @ %x sees free page.\n", thing, start);
3324 /* Check that it doesn't point to a forwarding pointer! */
3325 if (*((lispobj *)native_pointer(thing)) == 0x01) {
3326 lose("Ptr %x @ %x sees forwarding ptr.\n", thing, start);
3328 /* Check that its not in the RO space as it would then be a
3329 * pointer from the RO to the dynamic space. */
3330 if (is_in_readonly_space) {
3331 lose("ptr to dynamic space %x from RO space %x\n",
3334 /* Does it point to a plausible object? This check slows
3335 * it down a lot (so it's commented out).
3337 * "a lot" is serious: it ate 50 minutes cpu time on
3338 * my duron 950 before I came back from lunch and
3341 * FIXME: Add a variable to enable this
3344 if (!possibly_valid_dynamic_space_pointer((lispobj *)thing)) {
3345 lose("ptr %x to invalid object %x\n", thing, start);
3349 /* Verify that it points to another valid space. */
3350 if (!to_readonly_space && !to_static_space) {
3351 lose("Ptr %x @ %x sees junk.\n", thing, start);
3355 if (!(fixnump(thing))) {
3357 switch(widetag_of(*start)) {
3360 case SIMPLE_VECTOR_WIDETAG:
3362 case COMPLEX_WIDETAG:
3363 case SIMPLE_ARRAY_WIDETAG:
3364 case COMPLEX_BASE_STRING_WIDETAG:
3365 #ifdef COMPLEX_CHARACTER_STRING_WIDETAG
3366 case COMPLEX_CHARACTER_STRING_WIDETAG:
3368 case COMPLEX_VECTOR_NIL_WIDETAG:
3369 case COMPLEX_BIT_VECTOR_WIDETAG:
3370 case COMPLEX_VECTOR_WIDETAG:
3371 case COMPLEX_ARRAY_WIDETAG:
3372 case CLOSURE_HEADER_WIDETAG:
3373 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
3374 case VALUE_CELL_HEADER_WIDETAG:
3375 case SYMBOL_HEADER_WIDETAG:
3376 case CHARACTER_WIDETAG:
3377 #if N_WORD_BITS == 64
3378 case SINGLE_FLOAT_WIDETAG:
3380 case UNBOUND_MARKER_WIDETAG:
3385 case INSTANCE_HEADER_WIDETAG:
3388 long ntotal = HeaderValue(thing);
3389 lispobj layout = ((struct instance *)start)->slots[0];
3394 nuntagged = ((struct layout *)
3395 native_pointer(layout))->n_untagged_slots;
3396 verify_space(start + 1,
3397 ntotal - fixnum_value(nuntagged));
3401 case CODE_HEADER_WIDETAG:
3403 lispobj object = *start;
3405 long nheader_words, ncode_words, nwords;
3407 struct simple_fun *fheaderp;
3409 code = (struct code *) start;
3411 /* Check that it's not in the dynamic space.
3412 * FIXME: Isn't is supposed to be OK for code
3413 * objects to be in the dynamic space these days? */
3414 if (is_in_dynamic_space
3415 /* It's ok if it's byte compiled code. The trace
3416 * table offset will be a fixnum if it's x86
3417 * compiled code - check.
3419 * FIXME: #^#@@! lack of abstraction here..
3420 * This line can probably go away now that
3421 * there's no byte compiler, but I've got
3422 * too much to worry about right now to try
3423 * to make sure. -- WHN 2001-10-06 */
3424 && fixnump(code->trace_table_offset)
3425 /* Only when enabled */
3426 && verify_dynamic_code_check) {
3428 "/code object at %x in the dynamic space\n",
3432 ncode_words = fixnum_value(code->code_size);
3433 nheader_words = HeaderValue(object);
3434 nwords = ncode_words + nheader_words;
3435 nwords = CEILING(nwords, 2);
3436 /* Scavenge the boxed section of the code data block */
3437 verify_space(start + 1, nheader_words - 1);
3439 /* Scavenge the boxed section of each function
3440 * object in the code data block. */
3441 fheaderl = code->entry_points;
3442 while (fheaderl != NIL) {
3444 (struct simple_fun *) native_pointer(fheaderl);
3445 gc_assert(widetag_of(fheaderp->header) ==
3446 SIMPLE_FUN_HEADER_WIDETAG);
3447 verify_space(&fheaderp->name, 1);
3448 verify_space(&fheaderp->arglist, 1);
3449 verify_space(&fheaderp->type, 1);
3450 fheaderl = fheaderp->next;
3456 /* unboxed objects */
3457 case BIGNUM_WIDETAG:
3458 #if N_WORD_BITS != 64
3459 case SINGLE_FLOAT_WIDETAG:
3461 case DOUBLE_FLOAT_WIDETAG:
3462 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
3463 case LONG_FLOAT_WIDETAG:
3465 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
3466 case COMPLEX_SINGLE_FLOAT_WIDETAG:
3468 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
3469 case COMPLEX_DOUBLE_FLOAT_WIDETAG:
3471 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
3472 case COMPLEX_LONG_FLOAT_WIDETAG:
3474 case SIMPLE_BASE_STRING_WIDETAG:
3475 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
3476 case SIMPLE_CHARACTER_STRING_WIDETAG:
3478 case SIMPLE_BIT_VECTOR_WIDETAG:
3479 case SIMPLE_ARRAY_NIL_WIDETAG:
3480 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
3481 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
3482 case SIMPLE_ARRAY_UNSIGNED_BYTE_7_WIDETAG:
3483 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
3484 case SIMPLE_ARRAY_UNSIGNED_BYTE_15_WIDETAG:
3485 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
3486 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_29_WIDETAG
3487 case SIMPLE_ARRAY_UNSIGNED_BYTE_29_WIDETAG:
3489 case SIMPLE_ARRAY_UNSIGNED_BYTE_31_WIDETAG:
3490 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
3491 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_60_WIDETAG
3492 case SIMPLE_ARRAY_UNSIGNED_BYTE_60_WIDETAG:
3494 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG
3495 case SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG:
3497 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG
3498 case SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG:
3500 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
3501 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
3503 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
3504 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
3506 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
3507 case SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG:
3509 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
3510 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
3512 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_61_WIDETAG
3513 case SIMPLE_ARRAY_SIGNED_BYTE_61_WIDETAG:
3515 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG
3516 case SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG:
3518 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
3519 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
3520 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
3521 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
3523 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
3524 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
3526 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
3527 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
3529 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
3530 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
3533 case WEAK_POINTER_WIDETAG:
3534 #ifdef LUTEX_WIDETAG
3537 #ifdef NO_TLS_VALUE_MARKER_WIDETAG
3538 case NO_TLS_VALUE_MARKER_WIDETAG:
3540 count = (sizetab[widetag_of(*start)])(start);
3544 lose("Unhandled widetag 0x%x at 0x%x\n",
3545 widetag_of(*start), start);
3557 /* FIXME: It would be nice to make names consistent so that
3558 * foo_size meant size *in* *bytes* instead of size in some
3559 * arbitrary units. (Yes, this caused a bug, how did you guess?:-)
3560 * Some counts of lispobjs are called foo_count; it might be good
3561 * to grep for all foo_size and rename the appropriate ones to
3563 long read_only_space_size =
3564 (lispobj*)SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0)
3565 - (lispobj*)READ_ONLY_SPACE_START;
3566 long static_space_size =
3567 (lispobj*)SymbolValue(STATIC_SPACE_FREE_POINTER,0)
3568 - (lispobj*)STATIC_SPACE_START;
3570 for_each_thread(th) {
3571 long binding_stack_size =
3572 (lispobj*)get_binding_stack_pointer(th)
3573 - (lispobj*)th->binding_stack_start;
3574 verify_space(th->binding_stack_start, binding_stack_size);
3576 verify_space((lispobj*)READ_ONLY_SPACE_START, read_only_space_size);
3577 verify_space((lispobj*)STATIC_SPACE_START , static_space_size);
3581 verify_generation(generation_index_t generation)
3585 for (i = 0; i < last_free_page; i++) {
3586 if ((page_table[i].allocated != FREE_PAGE_FLAG)
3587 && (page_table[i].bytes_used != 0)
3588 && (page_table[i].gen == generation)) {
3589 page_index_t last_page;
3590 int region_allocation = page_table[i].allocated;
3592 /* This should be the start of a contiguous block */
3593 gc_assert(page_table[i].region_start_offset == 0);
3595 /* Need to find the full extent of this contiguous block in case
3596 objects span pages. */
3598 /* Now work forward until the end of this contiguous area is
3600 for (last_page = i; ;last_page++)
3601 /* Check whether this is the last page in this contiguous
3603 if ((page_table[last_page].bytes_used < PAGE_BYTES)
3604 /* Or it is PAGE_BYTES and is the last in the block */
3605 || (page_table[last_page+1].allocated != region_allocation)
3606 || (page_table[last_page+1].bytes_used == 0)
3607 || (page_table[last_page+1].gen != generation)
3608 || (page_table[last_page+1].region_start_offset == 0))
3611 verify_space(page_address(i),
3613 (page_table[last_page].bytes_used
3614 + npage_bytes(last_page-i)))
3621 /* Check that all the free space is zero filled. */
3623 verify_zero_fill(void)
3627 for (page = 0; page < last_free_page; page++) {
3628 if (page_table[page].allocated == FREE_PAGE_FLAG) {
3629 /* The whole page should be zero filled. */
3630 long *start_addr = (long *)page_address(page);
3633 for (i = 0; i < size; i++) {
3634 if (start_addr[i] != 0) {
3635 lose("free page not zero at %x\n", start_addr + i);
3639 long free_bytes = PAGE_BYTES - page_table[page].bytes_used;
3640 if (free_bytes > 0) {
3641 long *start_addr = (long *)((unsigned long)page_address(page)
3642 + page_table[page].bytes_used);
3643 long size = free_bytes / N_WORD_BYTES;
3645 for (i = 0; i < size; i++) {
3646 if (start_addr[i] != 0) {
3647 lose("free region not zero at %x\n", start_addr + i);
3655 /* External entry point for verify_zero_fill */
3657 gencgc_verify_zero_fill(void)
3659 /* Flush the alloc regions updating the tables. */
3660 gc_alloc_update_all_page_tables();
3661 SHOW("verifying zero fill");
3666 verify_dynamic_space(void)
3668 generation_index_t i;
3670 for (i = 0; i <= HIGHEST_NORMAL_GENERATION; i++)
3671 verify_generation(i);
3673 if (gencgc_enable_verify_zero_fill)
3677 /* Write-protect all the dynamic boxed pages in the given generation. */
3679 write_protect_generation_pages(generation_index_t generation)
3683 gc_assert(generation < SCRATCH_GENERATION);
3685 for (start = 0; start < last_free_page; start++) {
3686 if ((page_table[start].allocated == BOXED_PAGE_FLAG)
3687 && (page_table[start].bytes_used != 0)
3688 && !page_table[start].dont_move
3689 && (page_table[start].gen == generation)) {
3693 /* Note the page as protected in the page tables. */
3694 page_table[start].write_protected = 1;
3696 for (last = start + 1; last < last_free_page; last++) {
3697 if ((page_table[last].allocated != BOXED_PAGE_FLAG)
3698 || (page_table[last].bytes_used == 0)
3699 || page_table[last].dont_move
3700 || (page_table[last].gen != generation))
3702 page_table[last].write_protected = 1;
3705 page_start = (void *)page_address(start);
3707 os_protect(page_start,
3708 npage_bytes(last - start),
3709 OS_VM_PROT_READ | OS_VM_PROT_EXECUTE);
3715 if (gencgc_verbose > 1) {
3717 "/write protected %d of %d pages in generation %d\n",
3718 count_write_protect_generation_pages(generation),
3719 count_generation_pages(generation),
3724 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
3727 scavenge_control_stack()
3729 unsigned long control_stack_size;
3731 /* This is going to be a big problem when we try to port threads
3733 struct thread *th = arch_os_get_current_thread();
3734 lispobj *control_stack =
3735 (lispobj *)(th->control_stack_start);
3737 control_stack_size = current_control_stack_pointer - control_stack;
3738 scavenge(control_stack, control_stack_size);
3741 /* Scavenging Interrupt Contexts */
3743 static int boxed_registers[] = BOXED_REGISTERS;
3746 scavenge_interrupt_context(os_context_t * context)
3752 unsigned long lip_offset;
3753 int lip_register_pair;
3755 unsigned long pc_code_offset;
3757 #ifdef ARCH_HAS_LINK_REGISTER
3758 unsigned long lr_code_offset;
3760 #ifdef ARCH_HAS_NPC_REGISTER
3761 unsigned long npc_code_offset;
3765 /* Find the LIP's register pair and calculate it's offset */
3766 /* before we scavenge the context. */
3769 * I (RLT) think this is trying to find the boxed register that is
3770 * closest to the LIP address, without going past it. Usually, it's
3771 * reg_CODE or reg_LRA. But sometimes, nothing can be found.
3773 lip = *os_context_register_addr(context, reg_LIP);
3774 lip_offset = 0x7FFFFFFF;
3775 lip_register_pair = -1;
3776 for (i = 0; i < (sizeof(boxed_registers) / sizeof(int)); i++) {
3781 index = boxed_registers[i];
3782 reg = *os_context_register_addr(context, index);
3783 if ((reg & ~((1L<<N_LOWTAG_BITS)-1)) <= lip) {
3785 if (offset < lip_offset) {
3786 lip_offset = offset;
3787 lip_register_pair = index;
3791 #endif /* reg_LIP */
3793 /* Compute the PC's offset from the start of the CODE */
3795 pc_code_offset = *os_context_pc_addr(context)
3796 - *os_context_register_addr(context, reg_CODE);
3797 #ifdef ARCH_HAS_NPC_REGISTER
3798 npc_code_offset = *os_context_npc_addr(context)
3799 - *os_context_register_addr(context, reg_CODE);
3800 #endif /* ARCH_HAS_NPC_REGISTER */
3802 #ifdef ARCH_HAS_LINK_REGISTER
3804 *os_context_lr_addr(context) -
3805 *os_context_register_addr(context, reg_CODE);
3808 /* Scanvenge all boxed registers in the context. */
3809 for (i = 0; i < (sizeof(boxed_registers) / sizeof(int)); i++) {
3813 index = boxed_registers[i];
3814 foo = *os_context_register_addr(context, index);
3816 *os_context_register_addr(context, index) = foo;
3818 scavenge((lispobj*) &(*os_context_register_addr(context, index)), 1);
3825 * But what happens if lip_register_pair is -1?
3826 * *os_context_register_addr on Solaris (see
3827 * solaris_register_address in solaris-os.c) will return
3828 * &context->uc_mcontext.gregs[2]. But gregs[2] is REG_nPC. Is
3829 * that what we really want? My guess is that that is not what we
3830 * want, so if lip_register_pair is -1, we don't touch reg_LIP at
3831 * all. But maybe it doesn't really matter if LIP is trashed?
3833 if (lip_register_pair >= 0) {
3834 *os_context_register_addr(context, reg_LIP) =
3835 *os_context_register_addr(context, lip_register_pair)
3838 #endif /* reg_LIP */
3840 /* Fix the PC if it was in from space */
3841 if (from_space_p(*os_context_pc_addr(context)))
3842 *os_context_pc_addr(context) =
3843 *os_context_register_addr(context, reg_CODE) + pc_code_offset;
3845 #ifdef ARCH_HAS_LINK_REGISTER
3846 /* Fix the LR ditto; important if we're being called from
3847 * an assembly routine that expects to return using blr, otherwise
3849 if (from_space_p(*os_context_lr_addr(context)))
3850 *os_context_lr_addr(context) =
3851 *os_context_register_addr(context, reg_CODE) + lr_code_offset;
3854 #ifdef ARCH_HAS_NPC_REGISTER
3855 if (from_space_p(*os_context_npc_addr(context)))
3856 *os_context_npc_addr(context) =
3857 *os_context_register_addr(context, reg_CODE) + npc_code_offset;
3858 #endif /* ARCH_HAS_NPC_REGISTER */
3862 scavenge_interrupt_contexts(void)
3865 os_context_t *context;
3867 struct thread *th=arch_os_get_current_thread();
3869 index = fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,0));
3871 #if defined(DEBUG_PRINT_CONTEXT_INDEX)
3872 printf("Number of active contexts: %d\n", index);
3875 for (i = 0; i < index; i++) {
3876 context = th->interrupt_contexts[i];
3877 scavenge_interrupt_context(context);
3883 #if defined(LISP_FEATURE_SB_THREAD)
3885 preserve_context_registers (os_context_t *c)
3888 /* On Darwin the signal context isn't a contiguous block of memory,
3889 * so just preserve_pointering its contents won't be sufficient.
3891 #if defined(LISP_FEATURE_DARWIN)
3892 #if defined LISP_FEATURE_X86
3893 preserve_pointer((void*)*os_context_register_addr(c,reg_EAX));
3894 preserve_pointer((void*)*os_context_register_addr(c,reg_ECX));
3895 preserve_pointer((void*)*os_context_register_addr(c,reg_EDX));
3896 preserve_pointer((void*)*os_context_register_addr(c,reg_EBX));
3897 preserve_pointer((void*)*os_context_register_addr(c,reg_ESI));
3898 preserve_pointer((void*)*os_context_register_addr(c,reg_EDI));
3899 preserve_pointer((void*)*os_context_pc_addr(c));
3900 #elif defined LISP_FEATURE_X86_64
3901 preserve_pointer((void*)*os_context_register_addr(c,reg_RAX));
3902 preserve_pointer((void*)*os_context_register_addr(c,reg_RCX));
3903 preserve_pointer((void*)*os_context_register_addr(c,reg_RDX));
3904 preserve_pointer((void*)*os_context_register_addr(c,reg_RBX));
3905 preserve_pointer((void*)*os_context_register_addr(c,reg_RSI));
3906 preserve_pointer((void*)*os_context_register_addr(c,reg_RDI));
3907 preserve_pointer((void*)*os_context_register_addr(c,reg_R8));
3908 preserve_pointer((void*)*os_context_register_addr(c,reg_R9));
3909 preserve_pointer((void*)*os_context_register_addr(c,reg_R10));
3910 preserve_pointer((void*)*os_context_register_addr(c,reg_R11));
3911 preserve_pointer((void*)*os_context_register_addr(c,reg_R12));
3912 preserve_pointer((void*)*os_context_register_addr(c,reg_R13));
3913 preserve_pointer((void*)*os_context_register_addr(c,reg_R14));
3914 preserve_pointer((void*)*os_context_register_addr(c,reg_R15));
3915 preserve_pointer((void*)*os_context_pc_addr(c));
3917 #error "preserve_context_registers needs to be tweaked for non-x86 Darwin"
3920 for(ptr = ((void **)(c+1))-1; ptr>=(void **)c; ptr--) {
3921 preserve_pointer(*ptr);
3926 /* Garbage collect a generation. If raise is 0 then the remains of the
3927 * generation are not raised to the next generation. */
3929 garbage_collect_generation(generation_index_t generation, int raise)
3931 unsigned long bytes_freed;
3933 unsigned long static_space_size;
3934 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
3937 gc_assert(generation <= HIGHEST_NORMAL_GENERATION);
3939 /* The oldest generation can't be raised. */
3940 gc_assert((generation != HIGHEST_NORMAL_GENERATION) || (raise == 0));
3942 /* Check if weak hash tables were processed in the previous GC. */
3943 gc_assert(weak_hash_tables == NULL);
3945 /* Initialize the weak pointer list. */
3946 weak_pointers = NULL;
3948 #ifdef LUTEX_WIDETAG
3949 unmark_lutexes(generation);
3952 /* When a generation is not being raised it is transported to a
3953 * temporary generation (NUM_GENERATIONS), and lowered when
3954 * done. Set up this new generation. There should be no pages
3955 * allocated to it yet. */
3957 gc_assert(generations[SCRATCH_GENERATION].bytes_allocated == 0);
3960 /* Set the global src and dest. generations */
3961 from_space = generation;
3963 new_space = generation+1;
3965 new_space = SCRATCH_GENERATION;
3967 /* Change to a new space for allocation, resetting the alloc_start_page */
3968 gc_alloc_generation = new_space;
3969 generations[new_space].alloc_start_page = 0;
3970 generations[new_space].alloc_unboxed_start_page = 0;
3971 generations[new_space].alloc_large_start_page = 0;
3972 generations[new_space].alloc_large_unboxed_start_page = 0;
3974 /* Before any pointers are preserved, the dont_move flags on the
3975 * pages need to be cleared. */
3976 for (i = 0; i < last_free_page; i++)
3977 if(page_table[i].gen==from_space)
3978 page_table[i].dont_move = 0;
3980 /* Un-write-protect the old-space pages. This is essential for the
3981 * promoted pages as they may contain pointers into the old-space
3982 * which need to be scavenged. It also helps avoid unnecessary page
3983 * faults as forwarding pointers are written into them. They need to
3984 * be un-protected anyway before unmapping later. */
3985 unprotect_oldspace();
3987 /* Scavenge the stacks' conservative roots. */
3989 /* there are potentially two stacks for each thread: the main
3990 * stack, which may contain Lisp pointers, and the alternate stack.
3991 * We don't ever run Lisp code on the altstack, but it may
3992 * host a sigcontext with lisp objects in it */
3994 /* what we need to do: (1) find the stack pointer for the main
3995 * stack; scavenge it (2) find the interrupt context on the
3996 * alternate stack that might contain lisp values, and scavenge
3999 /* we assume that none of the preceding applies to the thread that
4000 * initiates GC. If you ever call GC from inside an altstack
4001 * handler, you will lose. */
4003 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
4004 /* And if we're saving a core, there's no point in being conservative. */
4005 if (conservative_stack) {
4006 for_each_thread(th) {
4008 void **esp=(void **)-1;
4009 #ifdef LISP_FEATURE_SB_THREAD
4011 if(th==arch_os_get_current_thread()) {
4012 /* Somebody is going to burn in hell for this, but casting
4013 * it in two steps shuts gcc up about strict aliasing. */
4014 esp = (void **)((void *)&raise);
4017 free=fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,th));
4018 for(i=free-1;i>=0;i--) {
4019 os_context_t *c=th->interrupt_contexts[i];
4020 esp1 = (void **) *os_context_register_addr(c,reg_SP);
4021 if (esp1>=(void **)th->control_stack_start &&
4022 esp1<(void **)th->control_stack_end) {
4023 if(esp1<esp) esp=esp1;
4024 preserve_context_registers(c);
4029 esp = (void **)((void *)&raise);
4031 for (ptr = ((void **)th->control_stack_end)-1; ptr >= esp; ptr--) {
4032 preserve_pointer(*ptr);
4039 if (gencgc_verbose > 1) {
4040 long num_dont_move_pages = count_dont_move_pages();
4042 "/non-movable pages due to conservative pointers = %d (%d bytes)\n",
4043 num_dont_move_pages,
4044 npage_bytes(num_dont_move_pages);
4048 /* Scavenge all the rest of the roots. */
4050 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
4052 * If not x86, we need to scavenge the interrupt context(s) and the
4055 scavenge_interrupt_contexts();
4056 scavenge_control_stack();
4059 /* Scavenge the Lisp functions of the interrupt handlers, taking
4060 * care to avoid SIG_DFL and SIG_IGN. */
4061 for (i = 0; i < NSIG; i++) {
4062 union interrupt_handler handler = interrupt_handlers[i];
4063 if (!ARE_SAME_HANDLER(handler.c, SIG_IGN) &&
4064 !ARE_SAME_HANDLER(handler.c, SIG_DFL)) {
4065 scavenge((lispobj *)(interrupt_handlers + i), 1);
4068 /* Scavenge the binding stacks. */
4071 for_each_thread(th) {
4072 long len= (lispobj *)get_binding_stack_pointer(th) -
4073 th->binding_stack_start;
4074 scavenge((lispobj *) th->binding_stack_start,len);
4075 #ifdef LISP_FEATURE_SB_THREAD
4076 /* do the tls as well */
4077 len=fixnum_value(SymbolValue(FREE_TLS_INDEX,0)) -
4078 (sizeof (struct thread))/(sizeof (lispobj));
4079 scavenge((lispobj *) (th+1),len);
4084 /* The original CMU CL code had scavenge-read-only-space code
4085 * controlled by the Lisp-level variable
4086 * *SCAVENGE-READ-ONLY-SPACE*. It was disabled by default, and it
4087 * wasn't documented under what circumstances it was useful or
4088 * safe to turn it on, so it's been turned off in SBCL. If you
4089 * want/need this functionality, and can test and document it,
4090 * please submit a patch. */
4092 if (SymbolValue(SCAVENGE_READ_ONLY_SPACE) != NIL) {
4093 unsigned long read_only_space_size =
4094 (lispobj*)SymbolValue(READ_ONLY_SPACE_FREE_POINTER) -
4095 (lispobj*)READ_ONLY_SPACE_START;
4097 "/scavenge read only space: %d bytes\n",
4098 read_only_space_size * sizeof(lispobj)));
4099 scavenge( (lispobj *) READ_ONLY_SPACE_START, read_only_space_size);
4103 /* Scavenge static space. */
4105 (lispobj *)SymbolValue(STATIC_SPACE_FREE_POINTER,0) -
4106 (lispobj *)STATIC_SPACE_START;
4107 if (gencgc_verbose > 1) {
4109 "/scavenge static space: %d bytes\n",
4110 static_space_size * sizeof(lispobj)));
4112 scavenge( (lispobj *) STATIC_SPACE_START, static_space_size);
4114 /* All generations but the generation being GCed need to be
4115 * scavenged. The new_space generation needs special handling as
4116 * objects may be moved in - it is handled separately below. */
4117 scavenge_generations(generation+1, PSEUDO_STATIC_GENERATION);
4119 /* Finally scavenge the new_space generation. Keep going until no
4120 * more objects are moved into the new generation */
4121 scavenge_newspace_generation(new_space);
4123 /* FIXME: I tried reenabling this check when debugging unrelated
4124 * GC weirdness ca. sbcl-0.6.12.45, and it failed immediately.
4125 * Since the current GC code seems to work well, I'm guessing that
4126 * this debugging code is just stale, but I haven't tried to
4127 * figure it out. It should be figured out and then either made to
4128 * work or just deleted. */
4129 #define RESCAN_CHECK 0
4131 /* As a check re-scavenge the newspace once; no new objects should
4134 long old_bytes_allocated = bytes_allocated;
4135 long bytes_allocated;
4137 /* Start with a full scavenge. */
4138 scavenge_newspace_generation_one_scan(new_space);
4140 /* Flush the current regions, updating the tables. */
4141 gc_alloc_update_all_page_tables();
4143 bytes_allocated = bytes_allocated - old_bytes_allocated;
4145 if (bytes_allocated != 0) {
4146 lose("Rescan of new_space allocated %d more bytes.\n",
4152 scan_weak_hash_tables();
4153 scan_weak_pointers();
4155 /* Flush the current regions, updating the tables. */
4156 gc_alloc_update_all_page_tables();
4158 /* Free the pages in oldspace, but not those marked dont_move. */
4159 bytes_freed = free_oldspace();
4161 /* If the GC is not raising the age then lower the generation back
4162 * to its normal generation number */
4164 for (i = 0; i < last_free_page; i++)
4165 if ((page_table[i].bytes_used != 0)
4166 && (page_table[i].gen == SCRATCH_GENERATION))
4167 page_table[i].gen = generation;
4168 gc_assert(generations[generation].bytes_allocated == 0);
4169 generations[generation].bytes_allocated =
4170 generations[SCRATCH_GENERATION].bytes_allocated;
4171 generations[SCRATCH_GENERATION].bytes_allocated = 0;
4174 /* Reset the alloc_start_page for generation. */
4175 generations[generation].alloc_start_page = 0;
4176 generations[generation].alloc_unboxed_start_page = 0;
4177 generations[generation].alloc_large_start_page = 0;
4178 generations[generation].alloc_large_unboxed_start_page = 0;
4180 if (generation >= verify_gens) {
4184 verify_dynamic_space();
4187 /* Set the new gc trigger for the GCed generation. */
4188 generations[generation].gc_trigger =
4189 generations[generation].bytes_allocated
4190 + generations[generation].bytes_consed_between_gc;
4193 generations[generation].num_gc = 0;
4195 ++generations[generation].num_gc;
4197 #ifdef LUTEX_WIDETAG
4198 reap_lutexes(generation);
4200 move_lutexes(generation, generation+1);
4204 /* Update last_free_page, then SymbolValue(ALLOCATION_POINTER). */
4206 update_dynamic_space_free_pointer(void)
4208 page_index_t last_page = -1, i;
4210 for (i = 0; i < last_free_page; i++)
4211 if ((page_table[i].allocated != FREE_PAGE_FLAG)
4212 && (page_table[i].bytes_used != 0))
4215 last_free_page = last_page+1;
4217 set_alloc_pointer((lispobj)(page_address(last_free_page)));
4218 return 0; /* dummy value: return something ... */
4222 remap_free_pages (page_index_t from, page_index_t to)
4224 page_index_t first_page, last_page;
4226 for (first_page = from; first_page <= to; first_page++) {
4227 if (page_table[first_page].allocated != FREE_PAGE_FLAG ||
4228 page_table[first_page].need_to_zero == 0) {
4232 last_page = first_page + 1;
4233 while (page_table[last_page].allocated == FREE_PAGE_FLAG &&
4235 page_table[last_page].need_to_zero == 1) {
4239 /* There's a mysterious Solaris/x86 problem with using mmap
4240 * tricks for memory zeroing. See sbcl-devel thread
4241 * "Re: patch: standalone executable redux".
4243 #if defined(LISP_FEATURE_SUNOS)
4244 zero_pages(first_page, last_page-1);
4246 zero_pages_with_mmap(first_page, last_page-1);
4249 first_page = last_page;
4253 generation_index_t small_generation_limit = 1;
4255 /* GC all generations newer than last_gen, raising the objects in each
4256 * to the next older generation - we finish when all generations below
4257 * last_gen are empty. Then if last_gen is due for a GC, or if
4258 * last_gen==NUM_GENERATIONS (the scratch generation? eh?) we GC that
4259 * too. The valid range for last_gen is: 0,1,...,NUM_GENERATIONS.
4261 * We stop collecting at gencgc_oldest_gen_to_gc, even if this is less than
4262 * last_gen (oh, and note that by default it is NUM_GENERATIONS-1) */
4264 collect_garbage(generation_index_t last_gen)
4266 generation_index_t gen = 0, i;
4269 /* The largest value of last_free_page seen since the time
4270 * remap_free_pages was called. */
4271 static page_index_t high_water_mark = 0;
4273 FSHOW((stderr, "/entering collect_garbage(%d)\n", last_gen));
4277 if (last_gen > HIGHEST_NORMAL_GENERATION+1) {
4279 "/collect_garbage: last_gen = %d, doing a level 0 GC\n",
4284 /* Flush the alloc regions updating the tables. */
4285 gc_alloc_update_all_page_tables();
4287 /* Verify the new objects created by Lisp code. */
4288 if (pre_verify_gen_0) {
4289 FSHOW((stderr, "pre-checking generation 0\n"));
4290 verify_generation(0);
4293 if (gencgc_verbose > 1)
4294 print_generation_stats(0);
4297 /* Collect the generation. */
4299 if (gen >= gencgc_oldest_gen_to_gc) {
4300 /* Never raise the oldest generation. */
4305 || (generations[gen].num_gc >= generations[gen].trigger_age);
4308 if (gencgc_verbose > 1) {
4310 "starting GC of generation %d with raise=%d alloc=%d trig=%d GCs=%d\n",
4313 generations[gen].bytes_allocated,
4314 generations[gen].gc_trigger,
4315 generations[gen].num_gc));
4318 /* If an older generation is being filled, then update its
4321 generations[gen+1].cum_sum_bytes_allocated +=
4322 generations[gen+1].bytes_allocated;
4325 garbage_collect_generation(gen, raise);
4327 /* Reset the memory age cum_sum. */
4328 generations[gen].cum_sum_bytes_allocated = 0;
4330 if (gencgc_verbose > 1) {
4331 FSHOW((stderr, "GC of generation %d finished:\n", gen));
4332 print_generation_stats(0);
4336 } while ((gen <= gencgc_oldest_gen_to_gc)
4337 && ((gen < last_gen)
4338 || ((gen <= gencgc_oldest_gen_to_gc)
4340 && (generations[gen].bytes_allocated
4341 > generations[gen].gc_trigger)
4342 && (gen_av_mem_age(gen)
4343 > generations[gen].min_av_mem_age))));
4345 /* Now if gen-1 was raised all generations before gen are empty.
4346 * If it wasn't raised then all generations before gen-1 are empty.
4348 * Now objects within this gen's pages cannot point to younger
4349 * generations unless they are written to. This can be exploited
4350 * by write-protecting the pages of gen; then when younger
4351 * generations are GCed only the pages which have been written
4356 gen_to_wp = gen - 1;
4358 /* There's not much point in WPing pages in generation 0 as it is
4359 * never scavenged (except promoted pages). */
4360 if ((gen_to_wp > 0) && enable_page_protection) {
4361 /* Check that they are all empty. */
4362 for (i = 0; i < gen_to_wp; i++) {
4363 if (generations[i].bytes_allocated)
4364 lose("trying to write-protect gen. %d when gen. %d nonempty\n",
4367 write_protect_generation_pages(gen_to_wp);
4370 /* Set gc_alloc() back to generation 0. The current regions should
4371 * be flushed after the above GCs. */
4372 gc_assert((boxed_region.free_pointer - boxed_region.start_addr) == 0);
4373 gc_alloc_generation = 0;
4375 /* Save the high-water mark before updating last_free_page */
4376 if (last_free_page > high_water_mark)
4377 high_water_mark = last_free_page;
4379 update_dynamic_space_free_pointer();
4381 auto_gc_trigger = bytes_allocated + bytes_consed_between_gcs;
4383 fprintf(stderr,"Next gc when %ld bytes have been consed\n",
4386 /* If we did a big GC (arbitrarily defined as gen > 1), release memory
4389 if (gen > small_generation_limit) {
4390 if (last_free_page > high_water_mark)
4391 high_water_mark = last_free_page;
4392 remap_free_pages(0, high_water_mark);
4393 high_water_mark = 0;
4398 SHOW("returning from collect_garbage");
4401 /* This is called by Lisp PURIFY when it is finished. All live objects
4402 * will have been moved to the RO and Static heaps. The dynamic space
4403 * will need a full re-initialization. We don't bother having Lisp
4404 * PURIFY flush the current gc_alloc() region, as the page_tables are
4405 * re-initialized, and every page is zeroed to be sure. */
4411 if (gencgc_verbose > 1)
4412 SHOW("entering gc_free_heap");
4414 for (page = 0; page < page_table_pages; page++) {
4415 /* Skip free pages which should already be zero filled. */
4416 if (page_table[page].allocated != FREE_PAGE_FLAG) {
4417 void *page_start, *addr;
4419 /* Mark the page free. The other slots are assumed invalid
4420 * when it is a FREE_PAGE_FLAG and bytes_used is 0 and it
4421 * should not be write-protected -- except that the
4422 * generation is used for the current region but it sets
4424 page_table[page].allocated = FREE_PAGE_FLAG;
4425 page_table[page].bytes_used = 0;
4427 #ifndef LISP_FEATURE_WIN32 /* Pages already zeroed on win32? Not sure
4428 * about this change. */
4429 /* Zero the page. */
4430 page_start = (void *)page_address(page);
4432 /* First, remove any write-protection. */
4433 os_protect(page_start, PAGE_BYTES, OS_VM_PROT_ALL);
4434 page_table[page].write_protected = 0;
4436 os_invalidate(page_start,PAGE_BYTES);
4437 addr = os_validate(page_start,PAGE_BYTES);
4438 if (addr == NULL || addr != page_start) {
4439 lose("gc_free_heap: page moved, 0x%08x ==> 0x%08x\n",
4444 page_table[page].write_protected = 0;
4446 } else if (gencgc_zero_check_during_free_heap) {
4447 /* Double-check that the page is zero filled. */
4450 gc_assert(page_table[page].allocated == FREE_PAGE_FLAG);
4451 gc_assert(page_table[page].bytes_used == 0);
4452 page_start = (long *)page_address(page);
4453 for (i=0; i<1024; i++) {
4454 if (page_start[i] != 0) {
4455 lose("free region not zero at %x\n", page_start + i);
4461 bytes_allocated = 0;
4463 /* Initialize the generations. */
4464 for (page = 0; page < NUM_GENERATIONS; page++) {
4465 generations[page].alloc_start_page = 0;
4466 generations[page].alloc_unboxed_start_page = 0;
4467 generations[page].alloc_large_start_page = 0;
4468 generations[page].alloc_large_unboxed_start_page = 0;
4469 generations[page].bytes_allocated = 0;
4470 generations[page].gc_trigger = 2000000;
4471 generations[page].num_gc = 0;
4472 generations[page].cum_sum_bytes_allocated = 0;
4473 generations[page].lutexes = NULL;
4476 if (gencgc_verbose > 1)
4477 print_generation_stats(0);
4479 /* Initialize gc_alloc(). */
4480 gc_alloc_generation = 0;
4482 gc_set_region_empty(&boxed_region);
4483 gc_set_region_empty(&unboxed_region);
4486 set_alloc_pointer((lispobj)((char *)heap_base));
4488 if (verify_after_free_heap) {
4489 /* Check whether purify has left any bad pointers. */
4490 FSHOW((stderr, "checking after free_heap\n"));
4500 /* Compute the number of pages needed for the dynamic space.
4501 * Dynamic space size should be aligned on page size. */
4502 page_table_pages = dynamic_space_size/PAGE_BYTES;
4503 gc_assert(dynamic_space_size == npage_bytes(page_table_pages));
4505 page_table = calloc(page_table_pages, sizeof(struct page));
4506 gc_assert(page_table);
4509 scavtab[WEAK_POINTER_WIDETAG] = scav_weak_pointer;
4510 transother[SIMPLE_ARRAY_WIDETAG] = trans_boxed_large;
4512 #ifdef LUTEX_WIDETAG
4513 scavtab[LUTEX_WIDETAG] = scav_lutex;
4514 transother[LUTEX_WIDETAG] = trans_lutex;
4515 sizetab[LUTEX_WIDETAG] = size_lutex;
4518 heap_base = (void*)DYNAMIC_SPACE_START;
4520 /* Initialize each page structure. */
4521 for (i = 0; i < page_table_pages; i++) {
4522 /* Initialize all pages as free. */
4523 page_table[i].allocated = FREE_PAGE_FLAG;
4524 page_table[i].bytes_used = 0;
4526 /* Pages are not write-protected at startup. */
4527 page_table[i].write_protected = 0;
4530 bytes_allocated = 0;
4532 /* Initialize the generations.
4534 * FIXME: very similar to code in gc_free_heap(), should be shared */
4535 for (i = 0; i < NUM_GENERATIONS; i++) {
4536 generations[i].alloc_start_page = 0;
4537 generations[i].alloc_unboxed_start_page = 0;
4538 generations[i].alloc_large_start_page = 0;
4539 generations[i].alloc_large_unboxed_start_page = 0;
4540 generations[i].bytes_allocated = 0;
4541 generations[i].gc_trigger = 2000000;
4542 generations[i].num_gc = 0;
4543 generations[i].cum_sum_bytes_allocated = 0;
4544 /* the tune-able parameters */
4545 generations[i].bytes_consed_between_gc = 2000000;
4546 generations[i].trigger_age = 1;
4547 generations[i].min_av_mem_age = 0.75;
4548 generations[i].lutexes = NULL;
4551 /* Initialize gc_alloc. */
4552 gc_alloc_generation = 0;
4553 gc_set_region_empty(&boxed_region);
4554 gc_set_region_empty(&unboxed_region);
4559 /* Pick up the dynamic space from after a core load.
4561 * The ALLOCATION_POINTER points to the end of the dynamic space.
4565 gencgc_pickup_dynamic(void)
4567 page_index_t page = 0;
4568 void *alloc_ptr = (void *)get_alloc_pointer();
4569 lispobj *prev=(lispobj *)page_address(page);
4570 generation_index_t gen = PSEUDO_STATIC_GENERATION;
4573 lispobj *first,*ptr= (lispobj *)page_address(page);
4574 page_table[page].allocated = BOXED_PAGE_FLAG;
4575 page_table[page].gen = gen;
4576 page_table[page].bytes_used = PAGE_BYTES;
4577 page_table[page].large_object = 0;
4578 page_table[page].write_protected = 0;
4579 page_table[page].write_protected_cleared = 0;
4580 page_table[page].dont_move = 0;
4581 page_table[page].need_to_zero = 1;
4583 if (!gencgc_partial_pickup) {
4584 first=gc_search_space(prev,(ptr+2)-prev,ptr);
4585 if(ptr == first) prev=ptr;
4586 page_table[page].region_start_offset =
4587 page_address(page) - (void *)prev;
4590 } while (page_address(page) < alloc_ptr);
4592 #ifdef LUTEX_WIDETAG
4593 /* Lutexes have been registered in generation 0 by coreparse, and
4594 * need to be moved to the right one manually.
4596 move_lutexes(0, PSEUDO_STATIC_GENERATION);
4599 last_free_page = page;
4601 generations[gen].bytes_allocated = npage_bytes(page);
4602 bytes_allocated = npage_bytes(page);
4604 gc_alloc_update_all_page_tables();
4605 write_protect_generation_pages(gen);
4609 gc_initialize_pointers(void)
4611 gencgc_pickup_dynamic();
4617 /* alloc(..) is the external interface for memory allocation. It
4618 * allocates to generation 0. It is not called from within the garbage
4619 * collector as it is only external uses that need the check for heap
4620 * size (GC trigger) and to disable the interrupts (interrupts are
4621 * always disabled during a GC).
4623 * The vops that call alloc(..) assume that the returned space is zero-filled.
4624 * (E.g. the most significant word of a 2-word bignum in MOVE-FROM-UNSIGNED.)
4626 * The check for a GC trigger is only performed when the current
4627 * region is full, so in most cases it's not needed. */
4632 struct thread *thread=arch_os_get_current_thread();
4633 struct alloc_region *region=
4634 #ifdef LISP_FEATURE_SB_THREAD
4635 thread ? &(thread->alloc_region) : &boxed_region;
4639 #ifndef LISP_FEATURE_WIN32
4640 lispobj alloc_signal;
4643 void *new_free_pointer;
4645 gc_assert(nbytes>0);
4647 /* Check for alignment allocation problems. */
4648 gc_assert((((unsigned long)region->free_pointer & LOWTAG_MASK) == 0)
4649 && ((nbytes & LOWTAG_MASK) == 0));
4653 /* there are a few places in the C code that allocate data in the
4654 * heap before Lisp starts. This is before interrupts are enabled,
4655 * so we don't need to check for pseudo-atomic */
4656 #ifdef LISP_FEATURE_SB_THREAD
4657 if(!get_psuedo_atomic_atomic(th)) {
4659 fprintf(stderr, "fatal error in thread 0x%x, tid=%ld\n",
4661 __asm__("movl %fs,%0" : "=r" (fs) : );
4662 fprintf(stderr, "fs is %x, th->tls_cookie=%x \n",
4663 debug_get_fs(),th->tls_cookie);
4664 lose("If you see this message before 2004.01.31, mail details to sbcl-devel\n");
4667 gc_assert(get_pseudo_atomic_atomic(th));
4671 /* maybe we can do this quickly ... */
4672 new_free_pointer = region->free_pointer + nbytes;
4673 if (new_free_pointer <= region->end_addr) {
4674 new_obj = (void*)(region->free_pointer);
4675 region->free_pointer = new_free_pointer;
4676 return(new_obj); /* yup */
4679 /* we have to go the long way around, it seems. Check whether
4680 * we should GC in the near future
4682 if (auto_gc_trigger && bytes_allocated > auto_gc_trigger) {
4683 gc_assert(get_pseudo_atomic_atomic(thread));
4684 /* Don't flood the system with interrupts if the need to gc is
4685 * already noted. This can happen for example when SUB-GC
4686 * allocates or after a gc triggered in a WITHOUT-GCING. */
4687 if (SymbolValue(GC_PENDING,thread) == NIL) {
4688 /* set things up so that GC happens when we finish the PA
4690 SetSymbolValue(GC_PENDING,T,thread);
4691 if (SymbolValue(GC_INHIBIT,thread) == NIL)
4692 set_pseudo_atomic_interrupted(thread);
4695 new_obj = gc_alloc_with_region(nbytes,0,region,0);
4697 #ifndef LISP_FEATURE_WIN32
4698 alloc_signal = SymbolValue(ALLOC_SIGNAL,thread);
4699 if ((alloc_signal & FIXNUM_TAG_MASK) == 0) {
4700 if ((signed long) alloc_signal <= 0) {
4701 SetSymbolValue(ALLOC_SIGNAL, T, thread);
4702 #ifdef LISP_FEATURE_SB_THREAD
4703 kill_thread_safely(thread->os_thread, SIGPROF);
4708 SetSymbolValue(ALLOC_SIGNAL,
4709 alloc_signal - (1 << N_FIXNUM_TAG_BITS),
4719 * shared support for the OS-dependent signal handlers which
4720 * catch GENCGC-related write-protect violations
4723 void unhandled_sigmemoryfault(void* addr);
4725 /* Depending on which OS we're running under, different signals might
4726 * be raised for a violation of write protection in the heap. This
4727 * function factors out the common generational GC magic which needs
4728 * to invoked in this case, and should be called from whatever signal
4729 * handler is appropriate for the OS we're running under.
4731 * Return true if this signal is a normal generational GC thing that
4732 * we were able to handle, or false if it was abnormal and control
4733 * should fall through to the general SIGSEGV/SIGBUS/whatever logic. */
4736 gencgc_handle_wp_violation(void* fault_addr)
4738 page_index_t page_index = find_page_index(fault_addr);
4740 #ifdef QSHOW_SIGNALS
4741 FSHOW((stderr, "heap WP violation? fault_addr=%x, page_index=%d\n",
4742 fault_addr, page_index));
4745 /* Check whether the fault is within the dynamic space. */
4746 if (page_index == (-1)) {
4748 /* It can be helpful to be able to put a breakpoint on this
4749 * case to help diagnose low-level problems. */
4750 unhandled_sigmemoryfault(fault_addr);
4752 /* not within the dynamic space -- not our responsibility */
4756 if (page_table[page_index].write_protected) {
4757 /* Unprotect the page. */
4758 os_protect(page_address(page_index), PAGE_BYTES, OS_VM_PROT_ALL);
4759 page_table[page_index].write_protected_cleared = 1;
4760 page_table[page_index].write_protected = 0;
4762 /* The only acceptable reason for this signal on a heap
4763 * access is that GENCGC write-protected the page.
4764 * However, if two CPUs hit a wp page near-simultaneously,
4765 * we had better not have the second one lose here if it
4766 * does this test after the first one has already set wp=0
4768 if(page_table[page_index].write_protected_cleared != 1)
4769 lose("fault in heap page %d not marked as write-protected\nboxed_region.first_page: %d, boxed_region.last_page %d\n",
4770 page_index, boxed_region.first_page,
4771 boxed_region.last_page);
4773 /* Don't worry, we can handle it. */
4777 /* This is to be called when we catch a SIGSEGV/SIGBUS, determine that
4778 * it's not just a case of the program hitting the write barrier, and
4779 * are about to let Lisp deal with it. It's basically just a
4780 * convenient place to set a gdb breakpoint. */
4782 unhandled_sigmemoryfault(void *addr)
4785 void gc_alloc_update_all_page_tables(void)
4787 /* Flush the alloc regions updating the tables. */
4790 gc_alloc_update_page_tables(0, &th->alloc_region);
4791 gc_alloc_update_page_tables(1, &unboxed_region);
4792 gc_alloc_update_page_tables(0, &boxed_region);
4796 gc_set_region_empty(struct alloc_region *region)
4798 region->first_page = 0;
4799 region->last_page = -1;
4800 region->start_addr = page_address(0);
4801 region->free_pointer = page_address(0);
4802 region->end_addr = page_address(0);
4806 zero_all_free_pages()
4810 for (i = 0; i < last_free_page; i++) {
4811 if (page_table[i].allocated == FREE_PAGE_FLAG) {
4812 #ifdef READ_PROTECT_FREE_PAGES
4813 os_protect(page_address(i),
4822 /* Things to do before doing a final GC before saving a core (without
4825 * + Pages in large_object pages aren't moved by the GC, so we need to
4826 * unset that flag from all pages.
4827 * + The pseudo-static generation isn't normally collected, but it seems
4828 * reasonable to collect it at least when saving a core. So move the
4829 * pages to a normal generation.
4832 prepare_for_final_gc ()
4835 for (i = 0; i < last_free_page; i++) {
4836 page_table[i].large_object = 0;
4837 if (page_table[i].gen == PSEUDO_STATIC_GENERATION) {
4838 int used = page_table[i].bytes_used;
4839 page_table[i].gen = HIGHEST_NORMAL_GENERATION;
4840 generations[PSEUDO_STATIC_GENERATION].bytes_allocated -= used;
4841 generations[HIGHEST_NORMAL_GENERATION].bytes_allocated += used;
4847 /* Do a non-conservative GC, and then save a core with the initial
4848 * function being set to the value of the static symbol
4849 * SB!VM:RESTART-LISP-FUNCTION */
4851 gc_and_save(char *filename, boolean prepend_runtime,
4852 boolean save_runtime_options)
4855 void *runtime_bytes = NULL;
4856 size_t runtime_size;
4858 file = prepare_to_save(filename, prepend_runtime, &runtime_bytes,
4863 conservative_stack = 0;
4865 /* The filename might come from Lisp, and be moved by the now
4866 * non-conservative GC. */
4867 filename = strdup(filename);
4869 /* Collect twice: once into relatively high memory, and then back
4870 * into low memory. This compacts the retained data into the lower
4871 * pages, minimizing the size of the core file.
4873 prepare_for_final_gc();
4874 gencgc_alloc_start_page = last_free_page;
4875 collect_garbage(HIGHEST_NORMAL_GENERATION+1);
4877 prepare_for_final_gc();
4878 gencgc_alloc_start_page = -1;
4879 collect_garbage(HIGHEST_NORMAL_GENERATION+1);
4881 if (prepend_runtime)
4882 save_runtime_to_filehandle(file, runtime_bytes, runtime_size);
4884 /* The dumper doesn't know that pages need to be zeroed before use. */
4885 zero_all_free_pages();
4886 save_to_filehandle(file, filename, SymbolValue(RESTART_LISP_FUNCTION,0),
4887 prepend_runtime, save_runtime_options);
4888 /* Oops. Save still managed to fail. Since we've mangled the stack
4889 * beyond hope, there's not much we can do.
4890 * (beyond FUNCALLing RESTART_LISP_FUNCTION, but I suspect that's
4891 * going to be rather unsatisfactory too... */
4892 lose("Attempt to save core after non-conservative GC failed.\n");