2 * GENerational Conservative Garbage Collector for SBCL x86
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>.
36 #include "interrupt.h"
42 #include "gc-internal.h"
44 #include "genesis/vector.h"
45 #include "genesis/weak-pointer.h"
46 #include "genesis/simple-fun.h"
48 #include "genesis/hash-table.h"
49 #include "genesis/instance.h"
50 #include "genesis/layout.h"
52 /* forward declarations */
53 page_index_t gc_find_freeish_pages(long *restart_page_ptr, long nbytes,
61 /* Generations 0-5 are normal collected generations, 6 is only used as
62 * scratch space by the collector, and should never get collected.
65 HIGHEST_NORMAL_GENERATION = 5,
66 PSEUDO_STATIC_GENERATION,
71 /* Should we use page protection to help avoid the scavenging of pages
72 * that don't have pointers to younger generations? */
73 boolean enable_page_protection = 1;
75 /* the minimum size (in bytes) for a large object*/
76 unsigned long large_object_size = 4 * PAGE_BYTES;
83 /* the verbosity level. All non-error messages are disabled at level 0;
84 * and only a few rare messages are printed at level 1. */
86 boolean gencgc_verbose = 1;
88 boolean gencgc_verbose = 0;
91 /* FIXME: At some point enable the various error-checking things below
92 * and see what they say. */
94 /* We hunt for pointers to old-space, when GCing generations >= verify_gen.
95 * Set verify_gens to HIGHEST_NORMAL_GENERATION + 1 to disable this kind of
97 generation_index_t verify_gens = HIGHEST_NORMAL_GENERATION + 1;
99 /* Should we do a pre-scan verify of generation 0 before it's GCed? */
100 boolean pre_verify_gen_0 = 0;
102 /* Should we check for bad pointers after gc_free_heap is called
103 * from Lisp PURIFY? */
104 boolean verify_after_free_heap = 0;
106 /* Should we print a note when code objects are found in the dynamic space
107 * during a heap verify? */
108 boolean verify_dynamic_code_check = 0;
110 /* Should we check code objects for fixup errors after they are transported? */
111 boolean check_code_fixups = 0;
113 /* Should we check that newly allocated regions are zero filled? */
114 boolean gencgc_zero_check = 0;
116 /* Should we check that the free space is zero filled? */
117 boolean gencgc_enable_verify_zero_fill = 0;
119 /* Should we check that free pages are zero filled during gc_free_heap
120 * called after Lisp PURIFY? */
121 boolean gencgc_zero_check_during_free_heap = 0;
123 /* When loading a core, don't do a full scan of the memory for the
124 * memory region boundaries. (Set to true by coreparse.c if the core
125 * contained a pagetable entry).
127 boolean gencgc_partial_pickup = 0;
129 /* If defined, free pages are read-protected to ensure that nothing
133 /* #define READ_PROTECT_FREE_PAGES */
137 * GC structures and variables
140 /* the total bytes allocated. These are seen by Lisp DYNAMIC-USAGE. */
141 unsigned long bytes_allocated = 0;
142 extern unsigned long bytes_consed_between_gcs; /* gc-common.c */
143 unsigned long auto_gc_trigger = 0;
145 /* the source and destination generations. These are set before a GC starts
147 generation_index_t from_space;
148 generation_index_t new_space;
150 /* should the GC be conservative on stack. If false (only right before
151 * saving a core), don't scan the stack / mark pages dont_move. */
152 static boolean conservative_stack = 1;
154 /* An array of page structures is statically allocated.
155 * This helps quickly map between an address its page structure.
156 * NUM_PAGES is set from the size of the dynamic space. */
157 struct page page_table[NUM_PAGES];
159 /* To map addresses to page structures the address of the first page
161 static void *heap_base = NULL;
163 #if N_WORD_BITS == 32
164 #define SIMPLE_ARRAY_WORD_WIDETAG SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG
165 #elif N_WORD_BITS == 64
166 #define SIMPLE_ARRAY_WORD_WIDETAG SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG
169 /* Calculate the start address for the given page number. */
171 page_address(page_index_t page_num)
173 return (heap_base + (page_num * PAGE_BYTES));
176 /* Find the page index within the page_table for the given
177 * address. Return -1 on failure. */
179 find_page_index(void *addr)
181 page_index_t index = addr-heap_base;
184 index = ((unsigned long)index)/PAGE_BYTES;
185 if (index < NUM_PAGES)
192 /* a structure to hold the state of a generation */
195 /* the first page that gc_alloc() checks on its next call */
196 page_index_t alloc_start_page;
198 /* the first page that gc_alloc_unboxed() checks on its next call */
199 page_index_t alloc_unboxed_start_page;
201 /* the first page that gc_alloc_large (boxed) considers on its next
202 * call. (Although it always allocates after the boxed_region.) */
203 page_index_t alloc_large_start_page;
205 /* the first page that gc_alloc_large (unboxed) considers on its
206 * next call. (Although it always allocates after the
207 * current_unboxed_region.) */
208 page_index_t alloc_large_unboxed_start_page;
210 /* the bytes allocated to this generation */
211 long bytes_allocated;
213 /* the number of bytes at which to trigger a GC */
216 /* to calculate a new level for gc_trigger */
217 long bytes_consed_between_gc;
219 /* the number of GCs since the last raise */
222 /* the average age after which a GC will raise objects to the
226 /* the cumulative sum of the bytes allocated to this generation. It is
227 * cleared after a GC on this generations, and update before new
228 * objects are added from a GC of a younger generation. Dividing by
229 * the bytes_allocated will give the average age of the memory in
230 * this generation since its last GC. */
231 long cum_sum_bytes_allocated;
233 /* a minimum average memory age before a GC will occur helps
234 * prevent a GC when a large number of new live objects have been
235 * added, in which case a GC could be a waste of time */
236 double min_av_mem_age;
239 /* an array of generation structures. There needs to be one more
240 * generation structure than actual generations as the oldest
241 * generation is temporarily raised then lowered. */
242 struct generation generations[NUM_GENERATIONS];
244 /* the oldest generation that is will currently be GCed by default.
245 * Valid values are: 0, 1, ... HIGHEST_NORMAL_GENERATION
247 * The default of HIGHEST_NORMAL_GENERATION enables GC on all generations.
249 * Setting this to 0 effectively disables the generational nature of
250 * the GC. In some applications generational GC may not be useful
251 * because there are no long-lived objects.
253 * An intermediate value could be handy after moving long-lived data
254 * into an older generation so an unnecessary GC of this long-lived
255 * data can be avoided. */
256 generation_index_t gencgc_oldest_gen_to_gc = HIGHEST_NORMAL_GENERATION;
258 /* The maximum free page in the heap is maintained and used to update
259 * ALLOCATION_POINTER which is used by the room function to limit its
260 * search of the heap. XX Gencgc obviously needs to be better
261 * integrated with the Lisp code. */
262 page_index_t last_free_page;
264 /* This lock is to prevent multiple threads from simultaneously
265 * allocating new regions which overlap each other. Note that the
266 * majority of GC is single-threaded, but alloc() may be called from
267 * >1 thread at a time and must be thread-safe. This lock must be
268 * seized before all accesses to generations[] or to parts of
269 * page_table[] that other threads may want to see */
271 #ifdef LISP_FEATURE_SB_THREAD
272 static pthread_mutex_t free_pages_lock = PTHREAD_MUTEX_INITIALIZER;
277 * miscellaneous heap functions
280 /* Count the number of pages which are write-protected within the
281 * given generation. */
283 count_write_protect_generation_pages(generation_index_t generation)
288 for (i = 0; i < last_free_page; i++)
289 if ((page_table[i].allocated != FREE_PAGE_FLAG)
290 && (page_table[i].gen == generation)
291 && (page_table[i].write_protected == 1))
296 /* Count the number of pages within the given generation. */
298 count_generation_pages(generation_index_t generation)
303 for (i = 0; i < last_free_page; i++)
304 if ((page_table[i].allocated != 0)
305 && (page_table[i].gen == generation))
312 count_dont_move_pages(void)
316 for (i = 0; i < last_free_page; i++) {
317 if ((page_table[i].allocated != 0) && (page_table[i].dont_move != 0)) {
325 /* Work through the pages and add up the number of bytes used for the
326 * given generation. */
328 count_generation_bytes_allocated (generation_index_t gen)
332 for (i = 0; i < last_free_page; i++) {
333 if ((page_table[i].allocated != 0) && (page_table[i].gen == gen))
334 result += page_table[i].bytes_used;
339 /* Return the average age of the memory in a generation. */
341 gen_av_mem_age(generation_index_t gen)
343 if (generations[gen].bytes_allocated == 0)
347 ((double)generations[gen].cum_sum_bytes_allocated)
348 / ((double)generations[gen].bytes_allocated);
351 void fpu_save(int *); /* defined in x86-assem.S */
352 void fpu_restore(int *); /* defined in x86-assem.S */
353 /* The verbose argument controls how much to print: 0 for normal
354 * level of detail; 1 for debugging. */
356 print_generation_stats(int verbose) /* FIXME: should take FILE argument */
358 generation_index_t i, gens;
361 /* This code uses the FP instructions which may be set up for Lisp
362 * so they need to be saved and reset for C. */
365 /* highest generation to print */
367 gens = SCRATCH_GENERATION;
369 gens = PSEUDO_STATIC_GENERATION;
371 /* Print the heap stats. */
373 " Gen Boxed Unboxed LB LUB !move Alloc Waste Trig WP GCs Mem-age\n");
375 for (i = 0; i < gens; i++) {
378 long unboxed_cnt = 0;
379 long large_boxed_cnt = 0;
380 long large_unboxed_cnt = 0;
383 for (j = 0; j < last_free_page; j++)
384 if (page_table[j].gen == i) {
386 /* Count the number of boxed pages within the given
388 if (page_table[j].allocated & BOXED_PAGE_FLAG) {
389 if (page_table[j].large_object)
394 if(page_table[j].dont_move) pinned_cnt++;
395 /* Count the number of unboxed pages within the given
397 if (page_table[j].allocated & UNBOXED_PAGE_FLAG) {
398 if (page_table[j].large_object)
405 gc_assert(generations[i].bytes_allocated
406 == count_generation_bytes_allocated(i));
408 " %1d: %5ld %5ld %5ld %5ld %5ld %8ld %5ld %8ld %4ld %3d %7.4f\n",
410 boxed_cnt, unboxed_cnt, large_boxed_cnt, large_unboxed_cnt,
412 generations[i].bytes_allocated,
413 (count_generation_pages(i)*PAGE_BYTES
414 - generations[i].bytes_allocated),
415 generations[i].gc_trigger,
416 count_write_protect_generation_pages(i),
417 generations[i].num_gc,
420 fprintf(stderr," Total bytes allocated=%ld\n", bytes_allocated);
422 fpu_restore(fpu_state);
426 void fast_bzero(void*, size_t); /* in <arch>-assem.S */
428 /* Zero the pages from START to END (inclusive), but use mmap/munmap instead
429 * if zeroing it ourselves, i.e. in practice give the memory back to the
430 * OS. Generally done after a large GC.
432 void zero_pages_with_mmap(page_index_t start, page_index_t end) {
434 void *addr = (void *) page_address(start), *new_addr;
435 size_t length = PAGE_BYTES*(1+end-start);
440 os_invalidate(addr, length);
441 new_addr = os_validate(addr, length);
442 if (new_addr == NULL || new_addr != addr) {
443 lose("remap_free_pages: page moved, 0x%08x ==> 0x%08x", start, new_addr);
446 for (i = start; i <= end; i++) {
447 page_table[i].need_to_zero = 0;
451 /* Zero the pages from START to END (inclusive). Generally done just after
452 * a new region has been allocated.
455 zero_pages(page_index_t start, page_index_t end) {
459 fast_bzero(page_address(start), PAGE_BYTES*(1+end-start));
462 /* Zero the pages from START to END (inclusive), except for those
463 * pages that are known to already zeroed. Mark all pages in the
464 * ranges as non-zeroed.
467 zero_dirty_pages(page_index_t start, page_index_t end) {
470 for (i = start; i <= end; i++) {
471 if (page_table[i].need_to_zero == 1) {
472 zero_pages(start, end);
477 for (i = start; i <= end; i++) {
478 page_table[i].need_to_zero = 1;
484 * To support quick and inline allocation, regions of memory can be
485 * allocated and then allocated from with just a free pointer and a
486 * check against an end address.
488 * Since objects can be allocated to spaces with different properties
489 * e.g. boxed/unboxed, generation, ages; there may need to be many
490 * allocation regions.
492 * Each allocation region may start within a partly used page. Many
493 * features of memory use are noted on a page wise basis, e.g. the
494 * generation; so if a region starts within an existing allocated page
495 * it must be consistent with this page.
497 * During the scavenging of the newspace, objects will be transported
498 * into an allocation region, and pointers updated to point to this
499 * allocation region. It is possible that these pointers will be
500 * scavenged again before the allocation region is closed, e.g. due to
501 * trans_list which jumps all over the place to cleanup the list. It
502 * is important to be able to determine properties of all objects
503 * pointed to when scavenging, e.g to detect pointers to the oldspace.
504 * Thus it's important that the allocation regions have the correct
505 * properties set when allocated, and not just set when closed. The
506 * region allocation routines return regions with the specified
507 * properties, and grab all the pages, setting their properties
508 * appropriately, except that the amount used is not known.
510 * These regions are used to support quicker allocation using just a
511 * free pointer. The actual space used by the region is not reflected
512 * in the pages tables until it is closed. It can't be scavenged until
515 * When finished with the region it should be closed, which will
516 * update the page tables for the actual space used returning unused
517 * space. Further it may be noted in the new regions which is
518 * necessary when scavenging the newspace.
520 * Large objects may be allocated directly without an allocation
521 * region, the page tables are updated immediately.
523 * Unboxed objects don't contain pointers to other objects and so
524 * don't need scavenging. Further they can't contain pointers to
525 * younger generations so WP is not needed. By allocating pages to
526 * unboxed objects the whole page never needs scavenging or
527 * write-protecting. */
529 /* We are only using two regions at present. Both are for the current
530 * newspace generation. */
531 struct alloc_region boxed_region;
532 struct alloc_region unboxed_region;
534 /* The generation currently being allocated to. */
535 static generation_index_t gc_alloc_generation;
537 /* Find a new region with room for at least the given number of bytes.
539 * It starts looking at the current generation's alloc_start_page. So
540 * may pick up from the previous region if there is enough space. This
541 * keeps the allocation contiguous when scavenging the newspace.
543 * The alloc_region should have been closed by a call to
544 * gc_alloc_update_page_tables(), and will thus be in an empty state.
546 * To assist the scavenging functions write-protected pages are not
547 * used. Free pages should not be write-protected.
549 * It is critical to the conservative GC that the start of regions be
550 * known. To help achieve this only small regions are allocated at a
553 * During scavenging, pointers may be found to within the current
554 * region and the page generation must be set so that pointers to the
555 * from space can be recognized. Therefore the generation of pages in
556 * the region are set to gc_alloc_generation. To prevent another
557 * allocation call using the same pages, all the pages in the region
558 * are allocated, although they will initially be empty.
561 gc_alloc_new_region(long nbytes, int unboxed, struct alloc_region *alloc_region)
563 page_index_t first_page;
564 page_index_t last_page;
570 "/alloc_new_region for %d bytes from gen %d\n",
571 nbytes, gc_alloc_generation));
574 /* Check that the region is in a reset state. */
575 gc_assert((alloc_region->first_page == 0)
576 && (alloc_region->last_page == -1)
577 && (alloc_region->free_pointer == alloc_region->end_addr));
578 thread_mutex_lock(&free_pages_lock);
581 generations[gc_alloc_generation].alloc_unboxed_start_page;
584 generations[gc_alloc_generation].alloc_start_page;
586 last_page=gc_find_freeish_pages(&first_page,nbytes,unboxed);
587 bytes_found=(PAGE_BYTES - page_table[first_page].bytes_used)
588 + PAGE_BYTES*(last_page-first_page);
590 /* Set up the alloc_region. */
591 alloc_region->first_page = first_page;
592 alloc_region->last_page = last_page;
593 alloc_region->start_addr = page_table[first_page].bytes_used
594 + page_address(first_page);
595 alloc_region->free_pointer = alloc_region->start_addr;
596 alloc_region->end_addr = alloc_region->start_addr + bytes_found;
598 /* Set up the pages. */
600 /* The first page may have already been in use. */
601 if (page_table[first_page].bytes_used == 0) {
603 page_table[first_page].allocated = UNBOXED_PAGE_FLAG;
605 page_table[first_page].allocated = BOXED_PAGE_FLAG;
606 page_table[first_page].gen = gc_alloc_generation;
607 page_table[first_page].large_object = 0;
608 page_table[first_page].first_object_offset = 0;
612 gc_assert(page_table[first_page].allocated == UNBOXED_PAGE_FLAG);
614 gc_assert(page_table[first_page].allocated == BOXED_PAGE_FLAG);
615 page_table[first_page].allocated |= OPEN_REGION_PAGE_FLAG;
617 gc_assert(page_table[first_page].gen == gc_alloc_generation);
618 gc_assert(page_table[first_page].large_object == 0);
620 for (i = first_page+1; i <= last_page; i++) {
622 page_table[i].allocated = UNBOXED_PAGE_FLAG;
624 page_table[i].allocated = BOXED_PAGE_FLAG;
625 page_table[i].gen = gc_alloc_generation;
626 page_table[i].large_object = 0;
627 /* This may not be necessary for unboxed regions (think it was
629 page_table[i].first_object_offset =
630 alloc_region->start_addr - page_address(i);
631 page_table[i].allocated |= OPEN_REGION_PAGE_FLAG ;
633 /* Bump up last_free_page. */
634 if (last_page+1 > last_free_page) {
635 last_free_page = last_page+1;
636 SetSymbolValue(ALLOCATION_POINTER,
637 (lispobj)(((char *)heap_base) + last_free_page*PAGE_BYTES),
640 thread_mutex_unlock(&free_pages_lock);
642 /* we can do this after releasing free_pages_lock */
643 if (gencgc_zero_check) {
645 for (p = (long *)alloc_region->start_addr;
646 p < (long *)alloc_region->end_addr; p++) {
648 /* KLUDGE: It would be nice to use %lx and explicit casts
649 * (long) in code like this, so that it is less likely to
650 * break randomly when running on a machine with different
651 * word sizes. -- WHN 19991129 */
652 lose("The new region at %x is not zero.\n", p);
657 #ifdef READ_PROTECT_FREE_PAGES
658 os_protect(page_address(first_page),
659 PAGE_BYTES*(1+last_page-first_page),
663 /* If the first page was only partial, don't check whether it's
664 * zeroed (it won't be) and don't zero it (since the parts that
665 * we're interested in are guaranteed to be zeroed).
667 if (page_table[first_page].bytes_used) {
671 zero_dirty_pages(first_page, last_page);
674 /* If the record_new_objects flag is 2 then all new regions created
677 * If it's 1 then then it is only recorded if the first page of the
678 * current region is <= new_areas_ignore_page. This helps avoid
679 * unnecessary recording when doing full scavenge pass.
681 * The new_object structure holds the page, byte offset, and size of
682 * new regions of objects. Each new area is placed in the array of
683 * these structures pointer to by new_areas. new_areas_index holds the
684 * offset into new_areas.
686 * If new_area overflows NUM_NEW_AREAS then it stops adding them. The
687 * later code must detect this and handle it, probably by doing a full
688 * scavenge of a generation. */
689 #define NUM_NEW_AREAS 512
690 static int record_new_objects = 0;
691 static page_index_t new_areas_ignore_page;
697 static struct new_area (*new_areas)[];
698 static long new_areas_index;
701 /* Add a new area to new_areas. */
703 add_new_area(page_index_t first_page, long offset, long size)
705 unsigned long new_area_start,c;
708 /* Ignore if full. */
709 if (new_areas_index >= NUM_NEW_AREAS)
712 switch (record_new_objects) {
716 if (first_page > new_areas_ignore_page)
725 new_area_start = PAGE_BYTES*first_page + offset;
727 /* Search backwards for a prior area that this follows from. If
728 found this will save adding a new area. */
729 for (i = new_areas_index-1, c = 0; (i >= 0) && (c < 8); i--, c++) {
730 unsigned long area_end =
731 PAGE_BYTES*((*new_areas)[i].page)
732 + (*new_areas)[i].offset
733 + (*new_areas)[i].size;
735 "/add_new_area S1 %d %d %d %d\n",
736 i, c, new_area_start, area_end));*/
737 if (new_area_start == area_end) {
739 "/adding to [%d] %d %d %d with %d %d %d:\n",
741 (*new_areas)[i].page,
742 (*new_areas)[i].offset,
743 (*new_areas)[i].size,
747 (*new_areas)[i].size += size;
752 (*new_areas)[new_areas_index].page = first_page;
753 (*new_areas)[new_areas_index].offset = offset;
754 (*new_areas)[new_areas_index].size = size;
756 "/new_area %d page %d offset %d size %d\n",
757 new_areas_index, first_page, offset, size));*/
760 /* Note the max new_areas used. */
761 if (new_areas_index > max_new_areas)
762 max_new_areas = new_areas_index;
765 /* Update the tables for the alloc_region. The region may be added to
768 * When done the alloc_region is set up so that the next quick alloc
769 * will fail safely and thus a new region will be allocated. Further
770 * it is safe to try to re-update the page table of this reset
773 gc_alloc_update_page_tables(int unboxed, struct alloc_region *alloc_region)
776 page_index_t first_page;
777 page_index_t next_page;
779 long orig_first_page_bytes_used;
784 first_page = alloc_region->first_page;
786 /* Catch an unused alloc_region. */
787 if ((first_page == 0) && (alloc_region->last_page == -1))
790 next_page = first_page+1;
792 thread_mutex_lock(&free_pages_lock);
793 if (alloc_region->free_pointer != alloc_region->start_addr) {
794 /* some bytes were allocated in the region */
795 orig_first_page_bytes_used = page_table[first_page].bytes_used;
797 gc_assert(alloc_region->start_addr == (page_address(first_page) + page_table[first_page].bytes_used));
799 /* All the pages used need to be updated */
801 /* Update the first page. */
803 /* If the page was free then set up the gen, and
804 * first_object_offset. */
805 if (page_table[first_page].bytes_used == 0)
806 gc_assert(page_table[first_page].first_object_offset == 0);
807 page_table[first_page].allocated &= ~(OPEN_REGION_PAGE_FLAG);
810 gc_assert(page_table[first_page].allocated == UNBOXED_PAGE_FLAG);
812 gc_assert(page_table[first_page].allocated == BOXED_PAGE_FLAG);
813 gc_assert(page_table[first_page].gen == gc_alloc_generation);
814 gc_assert(page_table[first_page].large_object == 0);
818 /* Calculate the number of bytes used in this page. This is not
819 * always the number of new bytes, unless it was free. */
821 if ((bytes_used = (alloc_region->free_pointer - page_address(first_page)))>PAGE_BYTES) {
822 bytes_used = PAGE_BYTES;
825 page_table[first_page].bytes_used = bytes_used;
826 byte_cnt += bytes_used;
829 /* All the rest of the pages should be free. We need to set their
830 * first_object_offset pointer to the start of the region, and set
833 page_table[next_page].allocated &= ~(OPEN_REGION_PAGE_FLAG);
835 gc_assert(page_table[next_page].allocated==UNBOXED_PAGE_FLAG);
837 gc_assert(page_table[next_page].allocated == BOXED_PAGE_FLAG);
838 gc_assert(page_table[next_page].bytes_used == 0);
839 gc_assert(page_table[next_page].gen == gc_alloc_generation);
840 gc_assert(page_table[next_page].large_object == 0);
842 gc_assert(page_table[next_page].first_object_offset ==
843 alloc_region->start_addr - page_address(next_page));
845 /* Calculate the number of bytes used in this page. */
847 if ((bytes_used = (alloc_region->free_pointer
848 - page_address(next_page)))>PAGE_BYTES) {
849 bytes_used = PAGE_BYTES;
852 page_table[next_page].bytes_used = bytes_used;
853 byte_cnt += bytes_used;
858 region_size = alloc_region->free_pointer - alloc_region->start_addr;
859 bytes_allocated += region_size;
860 generations[gc_alloc_generation].bytes_allocated += region_size;
862 gc_assert((byte_cnt- orig_first_page_bytes_used) == region_size);
864 /* Set the generations alloc restart page to the last page of
867 generations[gc_alloc_generation].alloc_unboxed_start_page =
870 generations[gc_alloc_generation].alloc_start_page = next_page-1;
872 /* Add the region to the new_areas if requested. */
874 add_new_area(first_page,orig_first_page_bytes_used, region_size);
878 "/gc_alloc_update_page_tables update %d bytes to gen %d\n",
880 gc_alloc_generation));
883 /* There are no bytes allocated. Unallocate the first_page if
884 * there are 0 bytes_used. */
885 page_table[first_page].allocated &= ~(OPEN_REGION_PAGE_FLAG);
886 if (page_table[first_page].bytes_used == 0)
887 page_table[first_page].allocated = FREE_PAGE_FLAG;
890 /* Unallocate any unused pages. */
891 while (next_page <= alloc_region->last_page) {
892 gc_assert(page_table[next_page].bytes_used == 0);
893 page_table[next_page].allocated = FREE_PAGE_FLAG;
896 thread_mutex_unlock(&free_pages_lock);
897 /* alloc_region is per-thread, we're ok to do this unlocked */
898 gc_set_region_empty(alloc_region);
901 static inline void *gc_quick_alloc(long nbytes);
903 /* Allocate a possibly large object. */
905 gc_alloc_large(long nbytes, int unboxed, struct alloc_region *alloc_region)
907 page_index_t first_page;
908 page_index_t last_page;
909 int orig_first_page_bytes_used;
913 page_index_t next_page;
915 thread_mutex_lock(&free_pages_lock);
919 generations[gc_alloc_generation].alloc_large_unboxed_start_page;
921 first_page = generations[gc_alloc_generation].alloc_large_start_page;
923 if (first_page <= alloc_region->last_page) {
924 first_page = alloc_region->last_page+1;
927 last_page=gc_find_freeish_pages(&first_page,nbytes,unboxed);
929 gc_assert(first_page > alloc_region->last_page);
931 generations[gc_alloc_generation].alloc_large_unboxed_start_page =
934 generations[gc_alloc_generation].alloc_large_start_page = last_page;
936 /* Set up the pages. */
937 orig_first_page_bytes_used = page_table[first_page].bytes_used;
939 /* If the first page was free then set up the gen, and
940 * first_object_offset. */
941 if (page_table[first_page].bytes_used == 0) {
943 page_table[first_page].allocated = UNBOXED_PAGE_FLAG;
945 page_table[first_page].allocated = BOXED_PAGE_FLAG;
946 page_table[first_page].gen = gc_alloc_generation;
947 page_table[first_page].first_object_offset = 0;
948 page_table[first_page].large_object = 1;
952 gc_assert(page_table[first_page].allocated == UNBOXED_PAGE_FLAG);
954 gc_assert(page_table[first_page].allocated == BOXED_PAGE_FLAG);
955 gc_assert(page_table[first_page].gen == gc_alloc_generation);
956 gc_assert(page_table[first_page].large_object == 1);
960 /* Calc. the number of bytes used in this page. This is not
961 * always the number of new bytes, unless it was free. */
963 if ((bytes_used = nbytes+orig_first_page_bytes_used) > PAGE_BYTES) {
964 bytes_used = PAGE_BYTES;
967 page_table[first_page].bytes_used = bytes_used;
968 byte_cnt += bytes_used;
970 next_page = first_page+1;
972 /* All the rest of the pages should be free. We need to set their
973 * first_object_offset pointer to the start of the region, and
974 * set the bytes_used. */
976 gc_assert(page_table[next_page].allocated == FREE_PAGE_FLAG);
977 gc_assert(page_table[next_page].bytes_used == 0);
979 page_table[next_page].allocated = UNBOXED_PAGE_FLAG;
981 page_table[next_page].allocated = BOXED_PAGE_FLAG;
982 page_table[next_page].gen = gc_alloc_generation;
983 page_table[next_page].large_object = 1;
985 page_table[next_page].first_object_offset =
986 orig_first_page_bytes_used - PAGE_BYTES*(next_page-first_page);
988 /* Calculate the number of bytes used in this page. */
990 if ((bytes_used=(nbytes+orig_first_page_bytes_used)-byte_cnt) > PAGE_BYTES) {
991 bytes_used = PAGE_BYTES;
994 page_table[next_page].bytes_used = bytes_used;
995 page_table[next_page].write_protected=0;
996 page_table[next_page].dont_move=0;
997 byte_cnt += bytes_used;
1001 gc_assert((byte_cnt-orig_first_page_bytes_used) == nbytes);
1003 bytes_allocated += nbytes;
1004 generations[gc_alloc_generation].bytes_allocated += nbytes;
1006 /* Add the region to the new_areas if requested. */
1008 add_new_area(first_page,orig_first_page_bytes_used,nbytes);
1010 /* Bump up last_free_page */
1011 if (last_page+1 > last_free_page) {
1012 last_free_page = last_page+1;
1013 SetSymbolValue(ALLOCATION_POINTER,
1014 (lispobj)(((char *)heap_base) + last_free_page*PAGE_BYTES),0);
1016 thread_mutex_unlock(&free_pages_lock);
1018 #ifdef READ_PROTECT_FREE_PAGES
1019 os_protect(page_address(first_page),
1020 PAGE_BYTES*(1+last_page-first_page),
1024 zero_dirty_pages(first_page, last_page);
1026 return page_address(first_page);
1029 static page_index_t gencgc_alloc_start_page = -1;
1032 gc_find_freeish_pages(page_index_t *restart_page_ptr, long nbytes, int unboxed)
1034 page_index_t first_page;
1035 page_index_t last_page;
1037 page_index_t restart_page=*restart_page_ptr;
1040 int large_p=(nbytes>=large_object_size);
1041 /* FIXME: assert(free_pages_lock is held); */
1043 /* Search for a contiguous free space of at least nbytes. If it's
1044 * a large object then align it on a page boundary by searching
1045 * for a free page. */
1047 if (gencgc_alloc_start_page != -1) {
1048 restart_page = gencgc_alloc_start_page;
1052 first_page = restart_page;
1054 while ((first_page < NUM_PAGES)
1055 && (page_table[first_page].allocated != FREE_PAGE_FLAG))
1058 while (first_page < NUM_PAGES) {
1059 if(page_table[first_page].allocated == FREE_PAGE_FLAG)
1061 if((page_table[first_page].allocated ==
1062 (unboxed ? UNBOXED_PAGE_FLAG : BOXED_PAGE_FLAG)) &&
1063 (page_table[first_page].large_object == 0) &&
1064 (page_table[first_page].gen == gc_alloc_generation) &&
1065 (page_table[first_page].bytes_used < (PAGE_BYTES-32)) &&
1066 (page_table[first_page].write_protected == 0) &&
1067 (page_table[first_page].dont_move == 0)) {
1073 if (first_page >= NUM_PAGES) {
1075 "Argh! gc_find_free_space failed (first_page), nbytes=%ld.\n",
1077 print_generation_stats(1);
1081 gc_assert(page_table[first_page].write_protected == 0);
1083 last_page = first_page;
1084 bytes_found = PAGE_BYTES - page_table[first_page].bytes_used;
1086 while (((bytes_found < nbytes)
1087 || (!large_p && (num_pages < 2)))
1088 && (last_page < (NUM_PAGES-1))
1089 && (page_table[last_page+1].allocated == FREE_PAGE_FLAG)) {
1092 bytes_found += PAGE_BYTES;
1093 gc_assert(page_table[last_page].write_protected == 0);
1096 region_size = (PAGE_BYTES - page_table[first_page].bytes_used)
1097 + PAGE_BYTES*(last_page-first_page);
1099 gc_assert(bytes_found == region_size);
1100 restart_page = last_page + 1;
1101 } while ((restart_page < NUM_PAGES) && (bytes_found < nbytes));
1103 /* Check for a failure */
1104 if ((restart_page >= NUM_PAGES) && (bytes_found < nbytes)) {
1106 "Argh! gc_find_freeish_pages failed (restart_page), nbytes=%ld.\n",
1108 print_generation_stats(1);
1111 *restart_page_ptr=first_page;
1116 /* Allocate bytes. All the rest of the special-purpose allocation
1117 * functions will eventually call this */
1120 gc_alloc_with_region(long nbytes,int unboxed_p, struct alloc_region *my_region,
1123 void *new_free_pointer;
1125 if(nbytes>=large_object_size)
1126 return gc_alloc_large(nbytes,unboxed_p,my_region);
1128 /* Check whether there is room in the current alloc region. */
1129 new_free_pointer = my_region->free_pointer + nbytes;
1131 /* fprintf(stderr, "alloc %d bytes from %p to %p\n", nbytes,
1132 my_region->free_pointer, new_free_pointer); */
1134 if (new_free_pointer <= my_region->end_addr) {
1135 /* If so then allocate from the current alloc region. */
1136 void *new_obj = my_region->free_pointer;
1137 my_region->free_pointer = new_free_pointer;
1139 /* Unless a `quick' alloc was requested, check whether the
1140 alloc region is almost empty. */
1142 (my_region->end_addr - my_region->free_pointer) <= 32) {
1143 /* If so, finished with the current region. */
1144 gc_alloc_update_page_tables(unboxed_p, my_region);
1145 /* Set up a new region. */
1146 gc_alloc_new_region(32 /*bytes*/, unboxed_p, my_region);
1149 return((void *)new_obj);
1152 /* Else not enough free space in the current region: retry with a
1155 gc_alloc_update_page_tables(unboxed_p, my_region);
1156 gc_alloc_new_region(nbytes, unboxed_p, my_region);
1157 return gc_alloc_with_region(nbytes,unboxed_p,my_region,0);
1160 /* these are only used during GC: all allocation from the mutator calls
1161 * alloc() -> gc_alloc_with_region() with the appropriate per-thread
1165 gc_general_alloc(long nbytes,int unboxed_p,int quick_p)
1167 struct alloc_region *my_region =
1168 unboxed_p ? &unboxed_region : &boxed_region;
1169 return gc_alloc_with_region(nbytes,unboxed_p, my_region,quick_p);
1172 static inline void *
1173 gc_quick_alloc(long nbytes)
1175 return gc_general_alloc(nbytes,ALLOC_BOXED,ALLOC_QUICK);
1178 static inline void *
1179 gc_quick_alloc_large(long nbytes)
1181 return gc_general_alloc(nbytes,ALLOC_BOXED,ALLOC_QUICK);
1184 static inline void *
1185 gc_alloc_unboxed(long nbytes)
1187 return gc_general_alloc(nbytes,ALLOC_UNBOXED,0);
1190 static inline void *
1191 gc_quick_alloc_unboxed(long nbytes)
1193 return gc_general_alloc(nbytes,ALLOC_UNBOXED,ALLOC_QUICK);
1196 static inline void *
1197 gc_quick_alloc_large_unboxed(long nbytes)
1199 return gc_general_alloc(nbytes,ALLOC_UNBOXED,ALLOC_QUICK);
1203 * scavenging/transporting routines derived from gc.c in CMU CL ca. 18b
1206 extern long (*scavtab[256])(lispobj *where, lispobj object);
1207 extern lispobj (*transother[256])(lispobj object);
1208 extern long (*sizetab[256])(lispobj *where);
1210 /* Copy a large boxed object. If the object is in a large object
1211 * region then it is simply promoted, else it is copied. If it's large
1212 * enough then it's copied to a large object region.
1214 * Vectors may have shrunk. If the object is not copied the space
1215 * needs to be reclaimed, and the page_tables corrected. */
1217 copy_large_object(lispobj object, long nwords)
1221 page_index_t first_page;
1223 gc_assert(is_lisp_pointer(object));
1224 gc_assert(from_space_p(object));
1225 gc_assert((nwords & 0x01) == 0);
1228 /* Check whether it's in a large object region. */
1229 first_page = find_page_index((void *)object);
1230 gc_assert(first_page >= 0);
1232 if (page_table[first_page].large_object) {
1234 /* Promote the object. */
1236 long remaining_bytes;
1237 page_index_t next_page;
1239 long old_bytes_used;
1241 /* Note: Any page write-protection must be removed, else a
1242 * later scavenge_newspace may incorrectly not scavenge these
1243 * pages. This would not be necessary if they are added to the
1244 * new areas, but let's do it for them all (they'll probably
1245 * be written anyway?). */
1247 gc_assert(page_table[first_page].first_object_offset == 0);
1249 next_page = first_page;
1250 remaining_bytes = nwords*N_WORD_BYTES;
1251 while (remaining_bytes > PAGE_BYTES) {
1252 gc_assert(page_table[next_page].gen == from_space);
1253 gc_assert(page_table[next_page].allocated == BOXED_PAGE_FLAG);
1254 gc_assert(page_table[next_page].large_object);
1255 gc_assert(page_table[next_page].first_object_offset==
1256 -PAGE_BYTES*(next_page-first_page));
1257 gc_assert(page_table[next_page].bytes_used == PAGE_BYTES);
1259 page_table[next_page].gen = new_space;
1261 /* Remove any write-protection. We should be able to rely
1262 * on the write-protect flag to avoid redundant calls. */
1263 if (page_table[next_page].write_protected) {
1264 os_protect(page_address(next_page), PAGE_BYTES, OS_VM_PROT_ALL);
1265 page_table[next_page].write_protected = 0;
1267 remaining_bytes -= PAGE_BYTES;
1271 /* Now only one page remains, but the object may have shrunk
1272 * so there may be more unused pages which will be freed. */
1274 /* The object may have shrunk but shouldn't have grown. */
1275 gc_assert(page_table[next_page].bytes_used >= remaining_bytes);
1277 page_table[next_page].gen = new_space;
1278 gc_assert(page_table[next_page].allocated == BOXED_PAGE_FLAG);
1280 /* Adjust the bytes_used. */
1281 old_bytes_used = page_table[next_page].bytes_used;
1282 page_table[next_page].bytes_used = remaining_bytes;
1284 bytes_freed = old_bytes_used - remaining_bytes;
1286 /* Free any remaining pages; needs care. */
1288 while ((old_bytes_used == PAGE_BYTES) &&
1289 (page_table[next_page].gen == from_space) &&
1290 (page_table[next_page].allocated == BOXED_PAGE_FLAG) &&
1291 page_table[next_page].large_object &&
1292 (page_table[next_page].first_object_offset ==
1293 -(next_page - first_page)*PAGE_BYTES)) {
1294 /* Checks out OK, free the page. Don't need to bother zeroing
1295 * pages as this should have been done before shrinking the
1296 * object. These pages shouldn't be write-protected as they
1297 * should be zero filled. */
1298 gc_assert(page_table[next_page].write_protected == 0);
1300 old_bytes_used = page_table[next_page].bytes_used;
1301 page_table[next_page].allocated = FREE_PAGE_FLAG;
1302 page_table[next_page].bytes_used = 0;
1303 bytes_freed += old_bytes_used;
1307 generations[from_space].bytes_allocated -= N_WORD_BYTES*nwords +
1309 generations[new_space].bytes_allocated += N_WORD_BYTES*nwords;
1310 bytes_allocated -= bytes_freed;
1312 /* Add the region to the new_areas if requested. */
1313 add_new_area(first_page,0,nwords*N_WORD_BYTES);
1317 /* Get tag of object. */
1318 tag = lowtag_of(object);
1320 /* Allocate space. */
1321 new = gc_quick_alloc_large(nwords*N_WORD_BYTES);
1323 memcpy(new,native_pointer(object),nwords*N_WORD_BYTES);
1325 /* Return Lisp pointer of new object. */
1326 return ((lispobj) new) | tag;
1330 /* to copy unboxed objects */
1332 copy_unboxed_object(lispobj object, long nwords)
1337 gc_assert(is_lisp_pointer(object));
1338 gc_assert(from_space_p(object));
1339 gc_assert((nwords & 0x01) == 0);
1341 /* Get tag of object. */
1342 tag = lowtag_of(object);
1344 /* Allocate space. */
1345 new = gc_quick_alloc_unboxed(nwords*N_WORD_BYTES);
1347 memcpy(new,native_pointer(object),nwords*N_WORD_BYTES);
1349 /* Return Lisp pointer of new object. */
1350 return ((lispobj) new) | tag;
1353 /* to copy large unboxed objects
1355 * If the object is in a large object region then it is simply
1356 * promoted, else it is copied. If it's large enough then it's copied
1357 * to a large object region.
1359 * Bignums and vectors may have shrunk. If the object is not copied
1360 * the space needs to be reclaimed, and the page_tables corrected.
1362 * KLUDGE: There's a lot of cut-and-paste duplication between this
1363 * function and copy_large_object(..). -- WHN 20000619 */
1365 copy_large_unboxed_object(lispobj object, long nwords)
1369 page_index_t first_page;
1371 gc_assert(is_lisp_pointer(object));
1372 gc_assert(from_space_p(object));
1373 gc_assert((nwords & 0x01) == 0);
1375 if ((nwords > 1024*1024) && gencgc_verbose)
1376 FSHOW((stderr, "/copy_large_unboxed_object: %d bytes\n", nwords*N_WORD_BYTES));
1378 /* Check whether it's a large object. */
1379 first_page = find_page_index((void *)object);
1380 gc_assert(first_page >= 0);
1382 if (page_table[first_page].large_object) {
1383 /* Promote the object. Note: Unboxed objects may have been
1384 * allocated to a BOXED region so it may be necessary to
1385 * change the region to UNBOXED. */
1386 long remaining_bytes;
1387 page_index_t next_page;
1389 long old_bytes_used;
1391 gc_assert(page_table[first_page].first_object_offset == 0);
1393 next_page = first_page;
1394 remaining_bytes = nwords*N_WORD_BYTES;
1395 while (remaining_bytes > PAGE_BYTES) {
1396 gc_assert(page_table[next_page].gen == from_space);
1397 gc_assert((page_table[next_page].allocated == UNBOXED_PAGE_FLAG)
1398 || (page_table[next_page].allocated == BOXED_PAGE_FLAG));
1399 gc_assert(page_table[next_page].large_object);
1400 gc_assert(page_table[next_page].first_object_offset==
1401 -PAGE_BYTES*(next_page-first_page));
1402 gc_assert(page_table[next_page].bytes_used == PAGE_BYTES);
1404 page_table[next_page].gen = new_space;
1405 page_table[next_page].allocated = UNBOXED_PAGE_FLAG;
1406 remaining_bytes -= PAGE_BYTES;
1410 /* Now only one page remains, but the object may have shrunk so
1411 * there may be more unused pages which will be freed. */
1413 /* Object may have shrunk but shouldn't have grown - check. */
1414 gc_assert(page_table[next_page].bytes_used >= remaining_bytes);
1416 page_table[next_page].gen = new_space;
1417 page_table[next_page].allocated = UNBOXED_PAGE_FLAG;
1419 /* Adjust the bytes_used. */
1420 old_bytes_used = page_table[next_page].bytes_used;
1421 page_table[next_page].bytes_used = remaining_bytes;
1423 bytes_freed = old_bytes_used - remaining_bytes;
1425 /* Free any remaining pages; needs care. */
1427 while ((old_bytes_used == PAGE_BYTES) &&
1428 (page_table[next_page].gen == from_space) &&
1429 ((page_table[next_page].allocated == UNBOXED_PAGE_FLAG)
1430 || (page_table[next_page].allocated == BOXED_PAGE_FLAG)) &&
1431 page_table[next_page].large_object &&
1432 (page_table[next_page].first_object_offset ==
1433 -(next_page - first_page)*PAGE_BYTES)) {
1434 /* Checks out OK, free the page. Don't need to both zeroing
1435 * pages as this should have been done before shrinking the
1436 * object. These pages shouldn't be write-protected, even if
1437 * boxed they should be zero filled. */
1438 gc_assert(page_table[next_page].write_protected == 0);
1440 old_bytes_used = page_table[next_page].bytes_used;
1441 page_table[next_page].allocated = FREE_PAGE_FLAG;
1442 page_table[next_page].bytes_used = 0;
1443 bytes_freed += old_bytes_used;
1447 if ((bytes_freed > 0) && gencgc_verbose)
1449 "/copy_large_unboxed bytes_freed=%d\n",
1452 generations[from_space].bytes_allocated -= nwords*N_WORD_BYTES + bytes_freed;
1453 generations[new_space].bytes_allocated += nwords*N_WORD_BYTES;
1454 bytes_allocated -= bytes_freed;
1459 /* Get tag of object. */
1460 tag = lowtag_of(object);
1462 /* Allocate space. */
1463 new = gc_quick_alloc_large_unboxed(nwords*N_WORD_BYTES);
1465 /* Copy the object. */
1466 memcpy(new,native_pointer(object),nwords*N_WORD_BYTES);
1468 /* Return Lisp pointer of new object. */
1469 return ((lispobj) new) | tag;
1478 * code and code-related objects
1481 static lispobj trans_fun_header(lispobj object);
1482 static lispobj trans_boxed(lispobj object);
1485 /* Scan a x86 compiled code object, looking for possible fixups that
1486 * have been missed after a move.
1488 * Two types of fixups are needed:
1489 * 1. Absolute fixups to within the code object.
1490 * 2. Relative fixups to outside the code object.
1492 * Currently only absolute fixups to the constant vector, or to the
1493 * code area are checked. */
1495 sniff_code_object(struct code *code, unsigned long displacement)
1497 #ifdef LISP_FEATURE_X86
1498 long nheader_words, ncode_words, nwords;
1500 void *constants_start_addr = NULL, *constants_end_addr;
1501 void *code_start_addr, *code_end_addr;
1502 int fixup_found = 0;
1504 if (!check_code_fixups)
1507 ncode_words = fixnum_value(code->code_size);
1508 nheader_words = HeaderValue(*(lispobj *)code);
1509 nwords = ncode_words + nheader_words;
1511 constants_start_addr = (void *)code + 5*N_WORD_BYTES;
1512 constants_end_addr = (void *)code + nheader_words*N_WORD_BYTES;
1513 code_start_addr = (void *)code + nheader_words*N_WORD_BYTES;
1514 code_end_addr = (void *)code + nwords*N_WORD_BYTES;
1516 /* Work through the unboxed code. */
1517 for (p = code_start_addr; p < code_end_addr; p++) {
1518 void *data = *(void **)p;
1519 unsigned d1 = *((unsigned char *)p - 1);
1520 unsigned d2 = *((unsigned char *)p - 2);
1521 unsigned d3 = *((unsigned char *)p - 3);
1522 unsigned d4 = *((unsigned char *)p - 4);
1524 unsigned d5 = *((unsigned char *)p - 5);
1525 unsigned d6 = *((unsigned char *)p - 6);
1528 /* Check for code references. */
1529 /* Check for a 32 bit word that looks like an absolute
1530 reference to within the code adea of the code object. */
1531 if ((data >= (code_start_addr-displacement))
1532 && (data < (code_end_addr-displacement))) {
1533 /* function header */
1535 && (((unsigned)p - 4 - 4*HeaderValue(*((unsigned *)p-1))) == (unsigned)code)) {
1536 /* Skip the function header */
1540 /* the case of PUSH imm32 */
1544 "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1545 p, d6, d5, d4, d3, d2, d1, data));
1546 FSHOW((stderr, "/PUSH $0x%.8x\n", data));
1548 /* the case of MOV [reg-8],imm32 */
1550 && (d2==0x40 || d2==0x41 || d2==0x42 || d2==0x43
1551 || d2==0x45 || d2==0x46 || d2==0x47)
1555 "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1556 p, d6, d5, d4, d3, d2, d1, data));
1557 FSHOW((stderr, "/MOV [reg-8],$0x%.8x\n", data));
1559 /* the case of LEA reg,[disp32] */
1560 if ((d2 == 0x8d) && ((d1 & 0xc7) == 5)) {
1563 "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1564 p, d6, d5, d4, d3, d2, d1, data));
1565 FSHOW((stderr,"/LEA reg,[$0x%.8x]\n", data));
1569 /* Check for constant references. */
1570 /* Check for a 32 bit word that looks like an absolute
1571 reference to within the constant vector. Constant references
1573 if ((data >= (constants_start_addr-displacement))
1574 && (data < (constants_end_addr-displacement))
1575 && (((unsigned)data & 0x3) == 0)) {
1580 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1581 p, d6, d5, d4, d3, d2, d1, data));
1582 FSHOW((stderr,"/MOV eax,0x%.8x\n", data));
1585 /* the case of MOV m32,EAX */
1589 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1590 p, d6, d5, d4, d3, d2, d1, data));
1591 FSHOW((stderr, "/MOV 0x%.8x,eax\n", data));
1594 /* the case of CMP m32,imm32 */
1595 if ((d1 == 0x3d) && (d2 == 0x81)) {
1598 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1599 p, d6, d5, d4, d3, d2, d1, data));
1601 FSHOW((stderr, "/CMP 0x%.8x,immed32\n", data));
1604 /* Check for a mod=00, r/m=101 byte. */
1605 if ((d1 & 0xc7) == 5) {
1610 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1611 p, d6, d5, d4, d3, d2, d1, data));
1612 FSHOW((stderr,"/CMP 0x%.8x,reg\n", data));
1614 /* the case of CMP reg32,m32 */
1618 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1619 p, d6, d5, d4, d3, d2, d1, data));
1620 FSHOW((stderr, "/CMP reg32,0x%.8x\n", data));
1622 /* the case of MOV m32,reg32 */
1626 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1627 p, d6, d5, d4, d3, d2, d1, data));
1628 FSHOW((stderr, "/MOV 0x%.8x,reg32\n", data));
1630 /* the case of MOV reg32,m32 */
1634 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1635 p, d6, d5, d4, d3, d2, d1, data));
1636 FSHOW((stderr, "/MOV reg32,0x%.8x\n", data));
1638 /* the case of LEA reg32,m32 */
1642 "abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1643 p, d6, d5, d4, d3, d2, d1, data));
1644 FSHOW((stderr, "/LEA reg32,0x%.8x\n", data));
1650 /* If anything was found, print some information on the code
1654 "/compiled code object at %x: header words = %d, code words = %d\n",
1655 code, nheader_words, ncode_words));
1657 "/const start = %x, end = %x\n",
1658 constants_start_addr, constants_end_addr));
1660 "/code start = %x, end = %x\n",
1661 code_start_addr, code_end_addr));
1667 gencgc_apply_code_fixups(struct code *old_code, struct code *new_code)
1669 /* x86-64 uses pc-relative addressing instead of this kludge */
1670 #ifndef LISP_FEATURE_X86_64
1671 long nheader_words, ncode_words, nwords;
1672 void *constants_start_addr, *constants_end_addr;
1673 void *code_start_addr, *code_end_addr;
1674 lispobj fixups = NIL;
1675 unsigned long displacement = (unsigned long)new_code - (unsigned long)old_code;
1676 struct vector *fixups_vector;
1678 ncode_words = fixnum_value(new_code->code_size);
1679 nheader_words = HeaderValue(*(lispobj *)new_code);
1680 nwords = ncode_words + nheader_words;
1682 "/compiled code object at %x: header words = %d, code words = %d\n",
1683 new_code, nheader_words, ncode_words)); */
1684 constants_start_addr = (void *)new_code + 5*N_WORD_BYTES;
1685 constants_end_addr = (void *)new_code + nheader_words*N_WORD_BYTES;
1686 code_start_addr = (void *)new_code + nheader_words*N_WORD_BYTES;
1687 code_end_addr = (void *)new_code + nwords*N_WORD_BYTES;
1690 "/const start = %x, end = %x\n",
1691 constants_start_addr,constants_end_addr));
1693 "/code start = %x; end = %x\n",
1694 code_start_addr,code_end_addr));
1697 /* The first constant should be a pointer to the fixups for this
1698 code objects. Check. */
1699 fixups = new_code->constants[0];
1701 /* It will be 0 or the unbound-marker if there are no fixups (as
1702 * will be the case if the code object has been purified, for
1703 * example) and will be an other pointer if it is valid. */
1704 if ((fixups == 0) || (fixups == UNBOUND_MARKER_WIDETAG) ||
1705 !is_lisp_pointer(fixups)) {
1706 /* Check for possible errors. */
1707 if (check_code_fixups)
1708 sniff_code_object(new_code, displacement);
1713 fixups_vector = (struct vector *)native_pointer(fixups);
1715 /* Could be pointing to a forwarding pointer. */
1716 /* FIXME is this always in from_space? if so, could replace this code with
1717 * forwarding_pointer_p/forwarding_pointer_value */
1718 if (is_lisp_pointer(fixups) &&
1719 (find_page_index((void*)fixups_vector) != -1) &&
1720 (fixups_vector->header == 0x01)) {
1721 /* If so, then follow it. */
1722 /*SHOW("following pointer to a forwarding pointer");*/
1723 fixups_vector = (struct vector *)native_pointer((lispobj)fixups_vector->length);
1726 /*SHOW("got fixups");*/
1728 if (widetag_of(fixups_vector->header) == SIMPLE_ARRAY_WORD_WIDETAG) {
1729 /* Got the fixups for the code block. Now work through the vector,
1730 and apply a fixup at each address. */
1731 long length = fixnum_value(fixups_vector->length);
1733 for (i = 0; i < length; i++) {
1734 unsigned long offset = fixups_vector->data[i];
1735 /* Now check the current value of offset. */
1736 unsigned long old_value =
1737 *(unsigned long *)((unsigned long)code_start_addr + offset);
1739 /* If it's within the old_code object then it must be an
1740 * absolute fixup (relative ones are not saved) */
1741 if ((old_value >= (unsigned long)old_code)
1742 && (old_value < ((unsigned long)old_code + nwords*N_WORD_BYTES)))
1743 /* So add the dispacement. */
1744 *(unsigned long *)((unsigned long)code_start_addr + offset) =
1745 old_value + displacement;
1747 /* It is outside the old code object so it must be a
1748 * relative fixup (absolute fixups are not saved). So
1749 * subtract the displacement. */
1750 *(unsigned long *)((unsigned long)code_start_addr + offset) =
1751 old_value - displacement;
1754 fprintf(stderr, "widetag of fixup vector is %d\n", widetag_of(fixups_vector->header));
1757 /* Check for possible errors. */
1758 if (check_code_fixups) {
1759 sniff_code_object(new_code,displacement);
1766 trans_boxed_large(lispobj object)
1769 unsigned long length;
1771 gc_assert(is_lisp_pointer(object));
1773 header = *((lispobj *) native_pointer(object));
1774 length = HeaderValue(header) + 1;
1775 length = CEILING(length, 2);
1777 return copy_large_object(object, length);
1780 /* Doesn't seem to be used, delete it after the grace period. */
1783 trans_unboxed_large(lispobj object)
1786 unsigned long length;
1788 gc_assert(is_lisp_pointer(object));
1790 header = *((lispobj *) native_pointer(object));
1791 length = HeaderValue(header) + 1;
1792 length = CEILING(length, 2);
1794 return copy_large_unboxed_object(object, length);
1800 * vector-like objects
1804 /* FIXME: What does this mean? */
1805 int gencgc_hash = 1;
1808 scav_vector(lispobj *where, lispobj object)
1810 unsigned long kv_length;
1812 unsigned long length = 0; /* (0 = dummy to stop GCC warning) */
1813 struct hash_table *hash_table;
1814 lispobj empty_symbol;
1815 unsigned long *index_vector = NULL; /* (NULL = dummy to stop GCC warning) */
1816 unsigned long *next_vector = NULL; /* (NULL = dummy to stop GCC warning) */
1817 unsigned long *hash_vector = NULL; /* (NULL = dummy to stop GCC warning) */
1819 unsigned long next_vector_length = 0;
1821 /* FIXME: A comment explaining this would be nice. It looks as
1822 * though SB-VM:VECTOR-VALID-HASHING-SUBTYPE is set for EQ-based
1823 * hash tables in the Lisp HASH-TABLE code, and nowhere else. */
1824 if (HeaderValue(object) != subtype_VectorValidHashing)
1828 /* This is set for backward compatibility. FIXME: Do we need
1831 (subtype_VectorMustRehash<<N_WIDETAG_BITS) | SIMPLE_VECTOR_WIDETAG;
1835 kv_length = fixnum_value(where[1]);
1836 kv_vector = where + 2; /* Skip the header and length. */
1837 /*FSHOW((stderr,"/kv_length = %d\n", kv_length));*/
1839 /* Scavenge element 0, which may be a hash-table structure. */
1840 scavenge(where+2, 1);
1841 if (!is_lisp_pointer(where[2])) {
1842 lose("no pointer at %x in hash table\n", where[2]);
1844 hash_table = (struct hash_table *)native_pointer(where[2]);
1845 /*FSHOW((stderr,"/hash_table = %x\n", hash_table));*/
1846 if (widetag_of(hash_table->header) != INSTANCE_HEADER_WIDETAG) {
1847 lose("hash table not instance (%x at %x)\n",
1852 /* Scavenge element 1, which should be some internal symbol that
1853 * the hash table code reserves for marking empty slots. */
1854 scavenge(where+3, 1);
1855 if (!is_lisp_pointer(where[3])) {
1856 lose("not empty-hash-table-slot symbol pointer: %x\n", where[3]);
1858 empty_symbol = where[3];
1859 /* fprintf(stderr,"* empty_symbol = %x\n", empty_symbol);*/
1860 if (widetag_of(*(lispobj *)native_pointer(empty_symbol)) !=
1861 SYMBOL_HEADER_WIDETAG) {
1862 lose("not a symbol where empty-hash-table-slot symbol expected: %x\n",
1863 *(lispobj *)native_pointer(empty_symbol));
1866 /* Scavenge hash table, which will fix the positions of the other
1867 * needed objects. */
1868 scavenge((lispobj *)hash_table,
1869 sizeof(struct hash_table) / sizeof(lispobj));
1871 /* Cross-check the kv_vector. */
1872 if (where != (lispobj *)native_pointer(hash_table->table)) {
1873 lose("hash_table table!=this table %x\n", hash_table->table);
1877 weak_p_obj = hash_table->weak_p;
1881 lispobj index_vector_obj = hash_table->index_vector;
1883 if (is_lisp_pointer(index_vector_obj) &&
1884 (widetag_of(*(lispobj *)native_pointer(index_vector_obj)) ==
1885 SIMPLE_ARRAY_WORD_WIDETAG)) {
1887 ((unsigned long *)native_pointer(index_vector_obj)) + 2;
1888 /*FSHOW((stderr, "/index_vector = %x\n",index_vector));*/
1889 length = fixnum_value(((lispobj *)native_pointer(index_vector_obj))[1]);
1890 /*FSHOW((stderr, "/length = %d\n", length));*/
1892 lose("invalid index_vector %x\n", index_vector_obj);
1898 lispobj next_vector_obj = hash_table->next_vector;
1900 if (is_lisp_pointer(next_vector_obj) &&
1901 (widetag_of(*(lispobj *)native_pointer(next_vector_obj)) ==
1902 SIMPLE_ARRAY_WORD_WIDETAG)) {
1903 next_vector = ((unsigned long *)native_pointer(next_vector_obj)) + 2;
1904 /*FSHOW((stderr, "/next_vector = %x\n", next_vector));*/
1905 next_vector_length = fixnum_value(((lispobj *)native_pointer(next_vector_obj))[1]);
1906 /*FSHOW((stderr, "/next_vector_length = %d\n", next_vector_length));*/
1908 lose("invalid next_vector %x\n", next_vector_obj);
1912 /* maybe hash vector */
1914 lispobj hash_vector_obj = hash_table->hash_vector;
1916 if (is_lisp_pointer(hash_vector_obj) &&
1917 (widetag_of(*(lispobj *)native_pointer(hash_vector_obj)) ==
1918 SIMPLE_ARRAY_WORD_WIDETAG)){
1920 ((unsigned long *)native_pointer(hash_vector_obj)) + 2;
1921 /*FSHOW((stderr, "/hash_vector = %x\n", hash_vector));*/
1922 gc_assert(fixnum_value(((lispobj *)native_pointer(hash_vector_obj))[1])
1923 == next_vector_length);
1926 /*FSHOW((stderr, "/no hash_vector: %x\n", hash_vector_obj));*/
1930 /* These lengths could be different as the index_vector can be a
1931 * different length from the others, a larger index_vector could help
1932 * reduce collisions. */
1933 gc_assert(next_vector_length*2 == kv_length);
1935 /* now all set up.. */
1937 /* Work through the KV vector. */
1940 for (i = 1; i < next_vector_length; i++) {
1941 lispobj old_key = kv_vector[2*i];
1943 #if N_WORD_BITS == 32
1944 unsigned long old_index = (old_key & 0x1fffffff)%length;
1945 #elif N_WORD_BITS == 64
1946 unsigned long old_index = (old_key & 0x1fffffffffffffff)%length;
1949 /* Scavenge the key and value. */
1950 scavenge(&kv_vector[2*i],2);
1952 /* Check whether the key has moved and is EQ based. */
1954 lispobj new_key = kv_vector[2*i];
1955 #if N_WORD_BITS == 32
1956 unsigned long new_index = (new_key & 0x1fffffff)%length;
1957 #elif N_WORD_BITS == 64
1958 unsigned long new_index = (new_key & 0x1fffffffffffffff)%length;
1961 if ((old_index != new_index) &&
1963 (hash_vector[i] == MAGIC_HASH_VECTOR_VALUE)) &&
1964 ((new_key != empty_symbol) ||
1965 (kv_vector[2*i] != empty_symbol))) {
1968 "* EQ key %d moved from %x to %x; index %d to %d\n",
1969 i, old_key, new_key, old_index, new_index));*/
1971 if (index_vector[old_index] != 0) {
1972 /*FSHOW((stderr, "/P1 %d\n", index_vector[old_index]));*/
1974 /* Unlink the key from the old_index chain. */
1975 if (index_vector[old_index] == i) {
1976 /*FSHOW((stderr, "/P2a %d\n", next_vector[i]));*/
1977 index_vector[old_index] = next_vector[i];
1978 /* Link it into the needing rehash chain. */
1979 next_vector[i] = fixnum_value(hash_table->needing_rehash);
1980 hash_table->needing_rehash = make_fixnum(i);
1983 unsigned long prior = index_vector[old_index];
1984 unsigned long next = next_vector[prior];
1986 /*FSHOW((stderr, "/P3a %d %d\n", prior, next));*/
1989 /*FSHOW((stderr, "/P3b %d %d\n", prior, next));*/
1992 next_vector[prior] = next_vector[next];
1993 /* Link it into the needing rehash
1996 fixnum_value(hash_table->needing_rehash);
1997 hash_table->needing_rehash = make_fixnum(next);
2002 next = next_vector[next];
2010 return (CEILING(kv_length + 2, 2));
2019 /* XX This is a hack adapted from cgc.c. These don't work too
2020 * efficiently with the gencgc as a list of the weak pointers is
2021 * maintained within the objects which causes writes to the pages. A
2022 * limited attempt is made to avoid unnecessary writes, but this needs
2024 #define WEAK_POINTER_NWORDS \
2025 CEILING((sizeof(struct weak_pointer) / sizeof(lispobj)), 2)
2028 scav_weak_pointer(lispobj *where, lispobj object)
2030 struct weak_pointer *wp = weak_pointers;
2031 /* Push the weak pointer onto the list of weak pointers.
2032 * Do I have to watch for duplicates? Originally this was
2033 * part of trans_weak_pointer but that didn't work in the
2034 * case where the WP was in a promoted region.
2037 /* Check whether it's already in the list. */
2038 while (wp != NULL) {
2039 if (wp == (struct weak_pointer*)where) {
2045 /* Add it to the start of the list. */
2046 wp = (struct weak_pointer*)where;
2047 if (wp->next != weak_pointers) {
2048 wp->next = weak_pointers;
2050 /*SHOW("avoided write to weak pointer");*/
2055 /* Do not let GC scavenge the value slot of the weak pointer.
2056 * (That is why it is a weak pointer.) */
2058 return WEAK_POINTER_NWORDS;
2063 search_read_only_space(void *pointer)
2065 lispobj *start = (lispobj *) READ_ONLY_SPACE_START;
2066 lispobj *end = (lispobj *) SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0);
2067 if ((pointer < (void *)start) || (pointer >= (void *)end))
2069 return (gc_search_space(start,
2070 (((lispobj *)pointer)+2)-start,
2071 (lispobj *) pointer));
2075 search_static_space(void *pointer)
2077 lispobj *start = (lispobj *)STATIC_SPACE_START;
2078 lispobj *end = (lispobj *)SymbolValue(STATIC_SPACE_FREE_POINTER,0);
2079 if ((pointer < (void *)start) || (pointer >= (void *)end))
2081 return (gc_search_space(start,
2082 (((lispobj *)pointer)+2)-start,
2083 (lispobj *) pointer));
2086 /* a faster version for searching the dynamic space. This will work even
2087 * if the object is in a current allocation region. */
2089 search_dynamic_space(void *pointer)
2091 page_index_t page_index = find_page_index(pointer);
2094 /* The address may be invalid, so do some checks. */
2095 if ((page_index == -1) ||
2096 (page_table[page_index].allocated == FREE_PAGE_FLAG))
2098 start = (lispobj *)((void *)page_address(page_index)
2099 + page_table[page_index].first_object_offset);
2100 return (gc_search_space(start,
2101 (((lispobj *)pointer)+2)-start,
2102 (lispobj *)pointer));
2105 /* Is there any possibility that pointer is a valid Lisp object
2106 * reference, and/or something else (e.g. subroutine call return
2107 * address) which should prevent us from moving the referred-to thing?
2108 * This is called from preserve_pointers() */
2110 possibly_valid_dynamic_space_pointer(lispobj *pointer)
2112 lispobj *start_addr;
2114 /* Find the object start address. */
2115 if ((start_addr = search_dynamic_space(pointer)) == NULL) {
2119 /* We need to allow raw pointers into Code objects for return
2120 * addresses. This will also pick up pointers to functions in code
2122 if (widetag_of(*start_addr) == CODE_HEADER_WIDETAG) {
2123 /* XXX could do some further checks here */
2127 /* If it's not a return address then it needs to be a valid Lisp
2129 if (!is_lisp_pointer((lispobj)pointer)) {
2133 /* Check that the object pointed to is consistent with the pointer
2136 switch (lowtag_of((lispobj)pointer)) {
2137 case FUN_POINTER_LOWTAG:
2138 /* Start_addr should be the enclosing code object, or a closure
2140 switch (widetag_of(*start_addr)) {
2141 case CODE_HEADER_WIDETAG:
2142 /* This case is probably caught above. */
2144 case CLOSURE_HEADER_WIDETAG:
2145 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
2146 if ((unsigned long)pointer !=
2147 ((unsigned long)start_addr+FUN_POINTER_LOWTAG)) {
2151 pointer, start_addr, *start_addr));
2159 pointer, start_addr, *start_addr));
2163 case LIST_POINTER_LOWTAG:
2164 if ((unsigned long)pointer !=
2165 ((unsigned long)start_addr+LIST_POINTER_LOWTAG)) {
2169 pointer, start_addr, *start_addr));
2172 /* Is it plausible cons? */
2173 if ((is_lisp_pointer(start_addr[0])
2174 || (fixnump(start_addr[0]))
2175 || (widetag_of(start_addr[0]) == CHARACTER_WIDETAG)
2176 #if N_WORD_BITS == 64
2177 || (widetag_of(start_addr[0]) == SINGLE_FLOAT_WIDETAG)
2179 || (widetag_of(start_addr[0]) == UNBOUND_MARKER_WIDETAG))
2180 && (is_lisp_pointer(start_addr[1])
2181 || (fixnump(start_addr[1]))
2182 || (widetag_of(start_addr[1]) == CHARACTER_WIDETAG)
2183 #if N_WORD_BITS == 64
2184 || (widetag_of(start_addr[1]) == SINGLE_FLOAT_WIDETAG)
2186 || (widetag_of(start_addr[1]) == UNBOUND_MARKER_WIDETAG)))
2192 pointer, start_addr, *start_addr));
2195 case INSTANCE_POINTER_LOWTAG:
2196 if ((unsigned long)pointer !=
2197 ((unsigned long)start_addr+INSTANCE_POINTER_LOWTAG)) {
2201 pointer, start_addr, *start_addr));
2204 if (widetag_of(start_addr[0]) != INSTANCE_HEADER_WIDETAG) {
2208 pointer, start_addr, *start_addr));
2212 case OTHER_POINTER_LOWTAG:
2213 if ((unsigned long)pointer !=
2214 ((unsigned long)start_addr+OTHER_POINTER_LOWTAG)) {
2218 pointer, start_addr, *start_addr));
2221 /* Is it plausible? Not a cons. XXX should check the headers. */
2222 if (is_lisp_pointer(start_addr[0]) || ((start_addr[0] & 3) == 0)) {
2226 pointer, start_addr, *start_addr));
2229 switch (widetag_of(start_addr[0])) {
2230 case UNBOUND_MARKER_WIDETAG:
2231 case NO_TLS_VALUE_MARKER_WIDETAG:
2232 case CHARACTER_WIDETAG:
2233 #if N_WORD_BITS == 64
2234 case SINGLE_FLOAT_WIDETAG:
2239 pointer, start_addr, *start_addr));
2242 /* only pointed to by function pointers? */
2243 case CLOSURE_HEADER_WIDETAG:
2244 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
2248 pointer, start_addr, *start_addr));
2251 case INSTANCE_HEADER_WIDETAG:
2255 pointer, start_addr, *start_addr));
2258 /* the valid other immediate pointer objects */
2259 case SIMPLE_VECTOR_WIDETAG:
2261 case COMPLEX_WIDETAG:
2262 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
2263 case COMPLEX_SINGLE_FLOAT_WIDETAG:
2265 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
2266 case COMPLEX_DOUBLE_FLOAT_WIDETAG:
2268 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
2269 case COMPLEX_LONG_FLOAT_WIDETAG:
2271 case SIMPLE_ARRAY_WIDETAG:
2272 case COMPLEX_BASE_STRING_WIDETAG:
2273 #ifdef COMPLEX_CHARACTER_STRING_WIDETAG
2274 case COMPLEX_CHARACTER_STRING_WIDETAG:
2276 case COMPLEX_VECTOR_NIL_WIDETAG:
2277 case COMPLEX_BIT_VECTOR_WIDETAG:
2278 case COMPLEX_VECTOR_WIDETAG:
2279 case COMPLEX_ARRAY_WIDETAG:
2280 case VALUE_CELL_HEADER_WIDETAG:
2281 case SYMBOL_HEADER_WIDETAG:
2283 case CODE_HEADER_WIDETAG:
2284 case BIGNUM_WIDETAG:
2285 #if N_WORD_BITS != 64
2286 case SINGLE_FLOAT_WIDETAG:
2288 case DOUBLE_FLOAT_WIDETAG:
2289 #ifdef LONG_FLOAT_WIDETAG
2290 case LONG_FLOAT_WIDETAG:
2292 case SIMPLE_BASE_STRING_WIDETAG:
2293 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
2294 case SIMPLE_CHARACTER_STRING_WIDETAG:
2296 case SIMPLE_BIT_VECTOR_WIDETAG:
2297 case SIMPLE_ARRAY_NIL_WIDETAG:
2298 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
2299 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
2300 case SIMPLE_ARRAY_UNSIGNED_BYTE_7_WIDETAG:
2301 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
2302 case SIMPLE_ARRAY_UNSIGNED_BYTE_15_WIDETAG:
2303 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
2304 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_29_WIDETAG
2305 case SIMPLE_ARRAY_UNSIGNED_BYTE_29_WIDETAG:
2307 case SIMPLE_ARRAY_UNSIGNED_BYTE_31_WIDETAG:
2308 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
2309 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_60_WIDETAG
2310 case SIMPLE_ARRAY_UNSIGNED_BYTE_60_WIDETAG:
2312 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG
2313 case SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG:
2315 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG
2316 case SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG:
2318 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
2319 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
2321 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
2322 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
2324 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
2325 case SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG:
2327 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
2328 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
2330 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_61_WIDETAG
2331 case SIMPLE_ARRAY_SIGNED_BYTE_61_WIDETAG:
2333 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG
2334 case SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG:
2336 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
2337 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
2338 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
2339 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
2341 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
2342 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
2344 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
2345 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
2347 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
2348 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
2351 case WEAK_POINTER_WIDETAG:
2358 pointer, start_addr, *start_addr));
2366 pointer, start_addr, *start_addr));
2374 /* Adjust large bignum and vector objects. This will adjust the
2375 * allocated region if the size has shrunk, and move unboxed objects
2376 * into unboxed pages. The pages are not promoted here, and the
2377 * promoted region is not added to the new_regions; this is really
2378 * only designed to be called from preserve_pointer(). Shouldn't fail
2379 * if this is missed, just may delay the moving of objects to unboxed
2380 * pages, and the freeing of pages. */
2382 maybe_adjust_large_object(lispobj *where)
2384 page_index_t first_page;
2385 page_index_t next_page;
2388 long remaining_bytes;
2390 long old_bytes_used;
2394 /* Check whether it's a vector or bignum object. */
2395 switch (widetag_of(where[0])) {
2396 case SIMPLE_VECTOR_WIDETAG:
2397 boxed = BOXED_PAGE_FLAG;
2399 case BIGNUM_WIDETAG:
2400 case SIMPLE_BASE_STRING_WIDETAG:
2401 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
2402 case SIMPLE_CHARACTER_STRING_WIDETAG:
2404 case SIMPLE_BIT_VECTOR_WIDETAG:
2405 case SIMPLE_ARRAY_NIL_WIDETAG:
2406 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
2407 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
2408 case SIMPLE_ARRAY_UNSIGNED_BYTE_7_WIDETAG:
2409 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
2410 case SIMPLE_ARRAY_UNSIGNED_BYTE_15_WIDETAG:
2411 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
2412 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_29_WIDETAG
2413 case SIMPLE_ARRAY_UNSIGNED_BYTE_29_WIDETAG:
2415 case SIMPLE_ARRAY_UNSIGNED_BYTE_31_WIDETAG:
2416 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
2417 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_60_WIDETAG
2418 case SIMPLE_ARRAY_UNSIGNED_BYTE_60_WIDETAG:
2420 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG
2421 case SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG:
2423 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG
2424 case SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG:
2426 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
2427 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
2429 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
2430 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
2432 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
2433 case SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG:
2435 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
2436 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
2438 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_61_WIDETAG
2439 case SIMPLE_ARRAY_SIGNED_BYTE_61_WIDETAG:
2441 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG
2442 case SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG:
2444 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
2445 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
2446 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
2447 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
2449 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
2450 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
2452 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
2453 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
2455 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
2456 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
2458 boxed = UNBOXED_PAGE_FLAG;
2464 /* Find its current size. */
2465 nwords = (sizetab[widetag_of(where[0])])(where);
2467 first_page = find_page_index((void *)where);
2468 gc_assert(first_page >= 0);
2470 /* Note: Any page write-protection must be removed, else a later
2471 * scavenge_newspace may incorrectly not scavenge these pages.
2472 * This would not be necessary if they are added to the new areas,
2473 * but lets do it for them all (they'll probably be written
2476 gc_assert(page_table[first_page].first_object_offset == 0);
2478 next_page = first_page;
2479 remaining_bytes = nwords*N_WORD_BYTES;
2480 while (remaining_bytes > PAGE_BYTES) {
2481 gc_assert(page_table[next_page].gen == from_space);
2482 gc_assert((page_table[next_page].allocated == BOXED_PAGE_FLAG)
2483 || (page_table[next_page].allocated == UNBOXED_PAGE_FLAG));
2484 gc_assert(page_table[next_page].large_object);
2485 gc_assert(page_table[next_page].first_object_offset ==
2486 -PAGE_BYTES*(next_page-first_page));
2487 gc_assert(page_table[next_page].bytes_used == PAGE_BYTES);
2489 page_table[next_page].allocated = boxed;
2491 /* Shouldn't be write-protected at this stage. Essential that the
2493 gc_assert(!page_table[next_page].write_protected);
2494 remaining_bytes -= PAGE_BYTES;
2498 /* Now only one page remains, but the object may have shrunk so
2499 * there may be more unused pages which will be freed. */
2501 /* Object may have shrunk but shouldn't have grown - check. */
2502 gc_assert(page_table[next_page].bytes_used >= remaining_bytes);
2504 page_table[next_page].allocated = boxed;
2505 gc_assert(page_table[next_page].allocated ==
2506 page_table[first_page].allocated);
2508 /* Adjust the bytes_used. */
2509 old_bytes_used = page_table[next_page].bytes_used;
2510 page_table[next_page].bytes_used = remaining_bytes;
2512 bytes_freed = old_bytes_used - remaining_bytes;
2514 /* Free any remaining pages; needs care. */
2516 while ((old_bytes_used == PAGE_BYTES) &&
2517 (page_table[next_page].gen == from_space) &&
2518 ((page_table[next_page].allocated == UNBOXED_PAGE_FLAG)
2519 || (page_table[next_page].allocated == BOXED_PAGE_FLAG)) &&
2520 page_table[next_page].large_object &&
2521 (page_table[next_page].first_object_offset ==
2522 -(next_page - first_page)*PAGE_BYTES)) {
2523 /* It checks out OK, free the page. We don't need to both zeroing
2524 * pages as this should have been done before shrinking the
2525 * object. These pages shouldn't be write protected as they
2526 * should be zero filled. */
2527 gc_assert(page_table[next_page].write_protected == 0);
2529 old_bytes_used = page_table[next_page].bytes_used;
2530 page_table[next_page].allocated = FREE_PAGE_FLAG;
2531 page_table[next_page].bytes_used = 0;
2532 bytes_freed += old_bytes_used;
2536 if ((bytes_freed > 0) && gencgc_verbose) {
2538 "/maybe_adjust_large_object() freed %d\n",
2542 generations[from_space].bytes_allocated -= bytes_freed;
2543 bytes_allocated -= bytes_freed;
2548 /* Take a possible pointer to a Lisp object and mark its page in the
2549 * page_table so that it will not be relocated during a GC.
2551 * This involves locating the page it points to, then backing up to
2552 * the start of its region, then marking all pages dont_move from there
2553 * up to the first page that's not full or has a different generation
2555 * It is assumed that all the page static flags have been cleared at
2556 * the start of a GC.
2558 * It is also assumed that the current gc_alloc() region has been
2559 * flushed and the tables updated. */
2561 preserve_pointer(void *addr)
2563 page_index_t addr_page_index = find_page_index(addr);
2564 page_index_t first_page;
2566 unsigned int region_allocation;
2568 /* quick check 1: Address is quite likely to have been invalid. */
2569 if ((addr_page_index == -1)
2570 || (page_table[addr_page_index].allocated == FREE_PAGE_FLAG)
2571 || (page_table[addr_page_index].bytes_used == 0)
2572 || (page_table[addr_page_index].gen != from_space)
2573 /* Skip if already marked dont_move. */
2574 || (page_table[addr_page_index].dont_move != 0))
2576 gc_assert(!(page_table[addr_page_index].allocated&OPEN_REGION_PAGE_FLAG));
2577 /* (Now that we know that addr_page_index is in range, it's
2578 * safe to index into page_table[] with it.) */
2579 region_allocation = page_table[addr_page_index].allocated;
2581 /* quick check 2: Check the offset within the page.
2584 if (((unsigned long)addr & (PAGE_BYTES - 1)) > page_table[addr_page_index].bytes_used)
2587 /* Filter out anything which can't be a pointer to a Lisp object
2588 * (or, as a special case which also requires dont_move, a return
2589 * address referring to something in a CodeObject). This is
2590 * expensive but important, since it vastly reduces the
2591 * probability that random garbage will be bogusly interpreted as
2592 * a pointer which prevents a page from moving. */
2593 if (!(possibly_valid_dynamic_space_pointer(addr)))
2596 /* Find the beginning of the region. Note that there may be
2597 * objects in the region preceding the one that we were passed a
2598 * pointer to: if this is the case, we will write-protect all the
2599 * previous objects' pages too. */
2602 /* I think this'd work just as well, but without the assertions.
2603 * -dan 2004.01.01 */
2605 find_page_index(page_address(addr_page_index)+
2606 page_table[addr_page_index].first_object_offset);
2608 first_page = addr_page_index;
2609 while (page_table[first_page].first_object_offset != 0) {
2611 /* Do some checks. */
2612 gc_assert(page_table[first_page].bytes_used == PAGE_BYTES);
2613 gc_assert(page_table[first_page].gen == from_space);
2614 gc_assert(page_table[first_page].allocated == region_allocation);
2618 /* Adjust any large objects before promotion as they won't be
2619 * copied after promotion. */
2620 if (page_table[first_page].large_object) {
2621 maybe_adjust_large_object(page_address(first_page));
2622 /* If a large object has shrunk then addr may now point to a
2623 * free area in which case it's ignored here. Note it gets
2624 * through the valid pointer test above because the tail looks
2626 if ((page_table[addr_page_index].allocated == FREE_PAGE_FLAG)
2627 || (page_table[addr_page_index].bytes_used == 0)
2628 /* Check the offset within the page. */
2629 || (((unsigned long)addr & (PAGE_BYTES - 1))
2630 > page_table[addr_page_index].bytes_used)) {
2632 "weird? ignore ptr 0x%x to freed area of large object\n",
2636 /* It may have moved to unboxed pages. */
2637 region_allocation = page_table[first_page].allocated;
2640 /* Now work forward until the end of this contiguous area is found,
2641 * marking all pages as dont_move. */
2642 for (i = first_page; ;i++) {
2643 gc_assert(page_table[i].allocated == region_allocation);
2645 /* Mark the page static. */
2646 page_table[i].dont_move = 1;
2648 /* Move the page to the new_space. XX I'd rather not do this
2649 * but the GC logic is not quite able to copy with the static
2650 * pages remaining in the from space. This also requires the
2651 * generation bytes_allocated counters be updated. */
2652 page_table[i].gen = new_space;
2653 generations[new_space].bytes_allocated += page_table[i].bytes_used;
2654 generations[from_space].bytes_allocated -= page_table[i].bytes_used;
2656 /* It is essential that the pages are not write protected as
2657 * they may have pointers into the old-space which need
2658 * scavenging. They shouldn't be write protected at this
2660 gc_assert(!page_table[i].write_protected);
2662 /* Check whether this is the last page in this contiguous block.. */
2663 if ((page_table[i].bytes_used < PAGE_BYTES)
2664 /* ..or it is PAGE_BYTES and is the last in the block */
2665 || (page_table[i+1].allocated == FREE_PAGE_FLAG)
2666 || (page_table[i+1].bytes_used == 0) /* next page free */
2667 || (page_table[i+1].gen != from_space) /* diff. gen */
2668 || (page_table[i+1].first_object_offset == 0))
2672 /* Check that the page is now static. */
2673 gc_assert(page_table[addr_page_index].dont_move != 0);
2676 /* If the given page is not write-protected, then scan it for pointers
2677 * to younger generations or the top temp. generation, if no
2678 * suspicious pointers are found then the page is write-protected.
2680 * Care is taken to check for pointers to the current gc_alloc()
2681 * region if it is a younger generation or the temp. generation. This
2682 * frees the caller from doing a gc_alloc_update_page_tables(). Actually
2683 * the gc_alloc_generation does not need to be checked as this is only
2684 * called from scavenge_generation() when the gc_alloc generation is
2685 * younger, so it just checks if there is a pointer to the current
2688 * We return 1 if the page was write-protected, else 0. */
2690 update_page_write_prot(page_index_t page)
2692 generation_index_t gen = page_table[page].gen;
2695 void **page_addr = (void **)page_address(page);
2696 long num_words = page_table[page].bytes_used / N_WORD_BYTES;
2698 /* Shouldn't be a free page. */
2699 gc_assert(page_table[page].allocated != FREE_PAGE_FLAG);
2700 gc_assert(page_table[page].bytes_used != 0);
2702 /* Skip if it's already write-protected, pinned, or unboxed */
2703 if (page_table[page].write_protected
2704 /* FIXME: What's the reason for not write-protecting pinned pages? */
2705 || page_table[page].dont_move
2706 || (page_table[page].allocated & UNBOXED_PAGE_FLAG))
2709 /* Scan the page for pointers to younger generations or the
2710 * top temp. generation. */
2712 for (j = 0; j < num_words; j++) {
2713 void *ptr = *(page_addr+j);
2714 page_index_t index = find_page_index(ptr);
2716 /* Check that it's in the dynamic space */
2718 if (/* Does it point to a younger or the temp. generation? */
2719 ((page_table[index].allocated != FREE_PAGE_FLAG)
2720 && (page_table[index].bytes_used != 0)
2721 && ((page_table[index].gen < gen)
2722 || (page_table[index].gen == SCRATCH_GENERATION)))
2724 /* Or does it point within a current gc_alloc() region? */
2725 || ((boxed_region.start_addr <= ptr)
2726 && (ptr <= boxed_region.free_pointer))
2727 || ((unboxed_region.start_addr <= ptr)
2728 && (ptr <= unboxed_region.free_pointer))) {
2735 /* Write-protect the page. */
2736 /*FSHOW((stderr, "/write-protecting page %d gen %d\n", page, gen));*/
2738 os_protect((void *)page_addr,
2740 OS_VM_PROT_READ|OS_VM_PROT_EXECUTE);
2742 /* Note the page as protected in the page tables. */
2743 page_table[page].write_protected = 1;
2749 /* Scavenge all generations from FROM to TO, inclusive, except for
2750 * new_space which needs special handling, as new objects may be
2751 * added which are not checked here - use scavenge_newspace generation.
2753 * Write-protected pages should not have any pointers to the
2754 * from_space so do need scavenging; thus write-protected pages are
2755 * not always scavenged. There is some code to check that these pages
2756 * are not written; but to check fully the write-protected pages need
2757 * to be scavenged by disabling the code to skip them.
2759 * Under the current scheme when a generation is GCed the younger
2760 * generations will be empty. So, when a generation is being GCed it
2761 * is only necessary to scavenge the older generations for pointers
2762 * not the younger. So a page that does not have pointers to younger
2763 * generations does not need to be scavenged.
2765 * The write-protection can be used to note pages that don't have
2766 * pointers to younger pages. But pages can be written without having
2767 * pointers to younger generations. After the pages are scavenged here
2768 * they can be scanned for pointers to younger generations and if
2769 * there are none the page can be write-protected.
2771 * One complication is when the newspace is the top temp. generation.
2773 * Enabling SC_GEN_CK scavenges the write-protected pages and checks
2774 * that none were written, which they shouldn't be as they should have
2775 * no pointers to younger generations. This breaks down for weak
2776 * pointers as the objects contain a link to the next and are written
2777 * if a weak pointer is scavenged. Still it's a useful check. */
2779 scavenge_generations(generation_index_t from, generation_index_t to)
2786 /* Clear the write_protected_cleared flags on all pages. */
2787 for (i = 0; i < NUM_PAGES; i++)
2788 page_table[i].write_protected_cleared = 0;
2791 for (i = 0; i < last_free_page; i++) {
2792 generation_index_t generation = page_table[i].gen;
2793 if ((page_table[i].allocated & BOXED_PAGE_FLAG)
2794 && (page_table[i].bytes_used != 0)
2795 && (generation != new_space)
2796 && (generation >= from)
2797 && (generation <= to)) {
2798 page_index_t last_page,j;
2799 int write_protected=1;
2801 /* This should be the start of a region */
2802 gc_assert(page_table[i].first_object_offset == 0);
2804 /* Now work forward until the end of the region */
2805 for (last_page = i; ; last_page++) {
2807 write_protected && page_table[last_page].write_protected;
2808 if ((page_table[last_page].bytes_used < PAGE_BYTES)
2809 /* Or it is PAGE_BYTES and is the last in the block */
2810 || (!(page_table[last_page+1].allocated & BOXED_PAGE_FLAG))
2811 || (page_table[last_page+1].bytes_used == 0)
2812 || (page_table[last_page+1].gen != generation)
2813 || (page_table[last_page+1].first_object_offset == 0))
2816 if (!write_protected) {
2817 scavenge(page_address(i),
2818 (page_table[last_page].bytes_used +
2819 (last_page-i)*PAGE_BYTES)/N_WORD_BYTES);
2821 /* Now scan the pages and write protect those that
2822 * don't have pointers to younger generations. */
2823 if (enable_page_protection) {
2824 for (j = i; j <= last_page; j++) {
2825 num_wp += update_page_write_prot(j);
2828 if ((gencgc_verbose > 1) && (num_wp != 0)) {
2830 "/write protected %d pages within generation %d\n",
2831 num_wp, generation));
2839 /* Check that none of the write_protected pages in this generation
2840 * have been written to. */
2841 for (i = 0; i < NUM_PAGES; i++) {
2842 if ((page_table[i].allocation != FREE_PAGE_FLAG)
2843 && (page_table[i].bytes_used != 0)
2844 && (page_table[i].gen == generation)
2845 && (page_table[i].write_protected_cleared != 0)) {
2846 FSHOW((stderr, "/scavenge_generation() %d\n", generation));
2848 "/page bytes_used=%d first_object_offset=%d dont_move=%d\n",
2849 page_table[i].bytes_used,
2850 page_table[i].first_object_offset,
2851 page_table[i].dont_move));
2852 lose("write to protected page %d in scavenge_generation()\n", i);
2859 /* Scavenge a newspace generation. As it is scavenged new objects may
2860 * be allocated to it; these will also need to be scavenged. This
2861 * repeats until there are no more objects unscavenged in the
2862 * newspace generation.
2864 * To help improve the efficiency, areas written are recorded by
2865 * gc_alloc() and only these scavenged. Sometimes a little more will be
2866 * scavenged, but this causes no harm. An easy check is done that the
2867 * scavenged bytes equals the number allocated in the previous
2870 * Write-protected pages are not scanned except if they are marked
2871 * dont_move in which case they may have been promoted and still have
2872 * pointers to the from space.
2874 * Write-protected pages could potentially be written by alloc however
2875 * to avoid having to handle re-scavenging of write-protected pages
2876 * gc_alloc() does not write to write-protected pages.
2878 * New areas of objects allocated are recorded alternatively in the two
2879 * new_areas arrays below. */
2880 static struct new_area new_areas_1[NUM_NEW_AREAS];
2881 static struct new_area new_areas_2[NUM_NEW_AREAS];
2883 /* Do one full scan of the new space generation. This is not enough to
2884 * complete the job as new objects may be added to the generation in
2885 * the process which are not scavenged. */
2887 scavenge_newspace_generation_one_scan(generation_index_t generation)
2892 "/starting one full scan of newspace generation %d\n",
2894 for (i = 0; i < last_free_page; i++) {
2895 /* Note that this skips over open regions when it encounters them. */
2896 if ((page_table[i].allocated & BOXED_PAGE_FLAG)
2897 && (page_table[i].bytes_used != 0)
2898 && (page_table[i].gen == generation)
2899 && ((page_table[i].write_protected == 0)
2900 /* (This may be redundant as write_protected is now
2901 * cleared before promotion.) */
2902 || (page_table[i].dont_move == 1))) {
2903 page_index_t last_page;
2906 /* The scavenge will start at the first_object_offset of page i.
2908 * We need to find the full extent of this contiguous
2909 * block in case objects span pages.
2911 * Now work forward until the end of this contiguous area
2912 * is found. A small area is preferred as there is a
2913 * better chance of its pages being write-protected. */
2914 for (last_page = i; ;last_page++) {
2915 /* If all pages are write-protected and movable,
2916 * then no need to scavenge */
2917 all_wp=all_wp && page_table[last_page].write_protected &&
2918 !page_table[last_page].dont_move;
2920 /* Check whether this is the last page in this
2921 * contiguous block */
2922 if ((page_table[last_page].bytes_used < PAGE_BYTES)
2923 /* Or it is PAGE_BYTES and is the last in the block */
2924 || (!(page_table[last_page+1].allocated & BOXED_PAGE_FLAG))
2925 || (page_table[last_page+1].bytes_used == 0)
2926 || (page_table[last_page+1].gen != generation)
2927 || (page_table[last_page+1].first_object_offset == 0))
2931 /* Do a limited check for write-protected pages. */
2935 size = (page_table[last_page].bytes_used
2936 + (last_page-i)*PAGE_BYTES
2937 - page_table[i].first_object_offset)/N_WORD_BYTES;
2938 new_areas_ignore_page = last_page;
2940 scavenge(page_address(i) +
2941 page_table[i].first_object_offset,
2949 "/done with one full scan of newspace generation %d\n",
2953 /* Do a complete scavenge of the newspace generation. */
2955 scavenge_newspace_generation(generation_index_t generation)
2959 /* the new_areas array currently being written to by gc_alloc() */
2960 struct new_area (*current_new_areas)[] = &new_areas_1;
2961 long current_new_areas_index;
2963 /* the new_areas created by the previous scavenge cycle */
2964 struct new_area (*previous_new_areas)[] = NULL;
2965 long previous_new_areas_index;
2967 /* Flush the current regions updating the tables. */
2968 gc_alloc_update_all_page_tables();
2970 /* Turn on the recording of new areas by gc_alloc(). */
2971 new_areas = current_new_areas;
2972 new_areas_index = 0;
2974 /* Don't need to record new areas that get scavenged anyway during
2975 * scavenge_newspace_generation_one_scan. */
2976 record_new_objects = 1;
2978 /* Start with a full scavenge. */
2979 scavenge_newspace_generation_one_scan(generation);
2981 /* Record all new areas now. */
2982 record_new_objects = 2;
2984 /* Flush the current regions updating the tables. */
2985 gc_alloc_update_all_page_tables();
2987 /* Grab new_areas_index. */
2988 current_new_areas_index = new_areas_index;
2991 "The first scan is finished; current_new_areas_index=%d.\n",
2992 current_new_areas_index));*/
2994 while (current_new_areas_index > 0) {
2995 /* Move the current to the previous new areas */
2996 previous_new_areas = current_new_areas;
2997 previous_new_areas_index = current_new_areas_index;
2999 /* Scavenge all the areas in previous new areas. Any new areas
3000 * allocated are saved in current_new_areas. */
3002 /* Allocate an array for current_new_areas; alternating between
3003 * new_areas_1 and 2 */
3004 if (previous_new_areas == &new_areas_1)
3005 current_new_areas = &new_areas_2;
3007 current_new_areas = &new_areas_1;
3009 /* Set up for gc_alloc(). */
3010 new_areas = current_new_areas;
3011 new_areas_index = 0;
3013 /* Check whether previous_new_areas had overflowed. */
3014 if (previous_new_areas_index >= NUM_NEW_AREAS) {
3016 /* New areas of objects allocated have been lost so need to do a
3017 * full scan to be sure! If this becomes a problem try
3018 * increasing NUM_NEW_AREAS. */
3020 SHOW("new_areas overflow, doing full scavenge");
3022 /* Don't need to record new areas that get scavenge anyway
3023 * during scavenge_newspace_generation_one_scan. */
3024 record_new_objects = 1;
3026 scavenge_newspace_generation_one_scan(generation);
3028 /* Record all new areas now. */
3029 record_new_objects = 2;
3031 /* Flush the current regions updating the tables. */
3032 gc_alloc_update_all_page_tables();
3036 /* Work through previous_new_areas. */
3037 for (i = 0; i < previous_new_areas_index; i++) {
3038 long page = (*previous_new_areas)[i].page;
3039 long offset = (*previous_new_areas)[i].offset;
3040 long size = (*previous_new_areas)[i].size / N_WORD_BYTES;
3041 gc_assert((*previous_new_areas)[i].size % N_WORD_BYTES == 0);
3042 scavenge(page_address(page)+offset, size);
3045 /* Flush the current regions updating the tables. */
3046 gc_alloc_update_all_page_tables();
3049 current_new_areas_index = new_areas_index;
3052 "The re-scan has finished; current_new_areas_index=%d.\n",
3053 current_new_areas_index));*/
3056 /* Turn off recording of areas allocated by gc_alloc(). */
3057 record_new_objects = 0;
3060 /* Check that none of the write_protected pages in this generation
3061 * have been written to. */
3062 for (i = 0; i < NUM_PAGES; i++) {
3063 if ((page_table[i].allocation != FREE_PAGE_FLAG)
3064 && (page_table[i].bytes_used != 0)
3065 && (page_table[i].gen == generation)
3066 && (page_table[i].write_protected_cleared != 0)
3067 && (page_table[i].dont_move == 0)) {
3068 lose("write protected page %d written to in scavenge_newspace_generation\ngeneration=%d dont_move=%d\n",
3069 i, generation, page_table[i].dont_move);
3075 /* Un-write-protect all the pages in from_space. This is done at the
3076 * start of a GC else there may be many page faults while scavenging
3077 * the newspace (I've seen drive the system time to 99%). These pages
3078 * would need to be unprotected anyway before unmapping in
3079 * free_oldspace; not sure what effect this has on paging.. */
3081 unprotect_oldspace(void)
3085 for (i = 0; i < last_free_page; i++) {
3086 if ((page_table[i].allocated != FREE_PAGE_FLAG)
3087 && (page_table[i].bytes_used != 0)
3088 && (page_table[i].gen == from_space)) {
3091 page_start = (void *)page_address(i);
3093 /* Remove any write-protection. We should be able to rely
3094 * on the write-protect flag to avoid redundant calls. */
3095 if (page_table[i].write_protected) {
3096 os_protect(page_start, PAGE_BYTES, OS_VM_PROT_ALL);
3097 page_table[i].write_protected = 0;
3103 /* Work through all the pages and free any in from_space. This
3104 * assumes that all objects have been copied or promoted to an older
3105 * generation. Bytes_allocated and the generation bytes_allocated
3106 * counter are updated. The number of bytes freed is returned. */
3110 long bytes_freed = 0;
3111 page_index_t first_page, last_page;
3116 /* Find a first page for the next region of pages. */
3117 while ((first_page < last_free_page)
3118 && ((page_table[first_page].allocated == FREE_PAGE_FLAG)
3119 || (page_table[first_page].bytes_used == 0)
3120 || (page_table[first_page].gen != from_space)))
3123 if (first_page >= last_free_page)
3126 /* Find the last page of this region. */
3127 last_page = first_page;
3130 /* Free the page. */
3131 bytes_freed += page_table[last_page].bytes_used;
3132 generations[page_table[last_page].gen].bytes_allocated -=
3133 page_table[last_page].bytes_used;
3134 page_table[last_page].allocated = FREE_PAGE_FLAG;
3135 page_table[last_page].bytes_used = 0;
3137 /* Remove any write-protection. We should be able to rely
3138 * on the write-protect flag to avoid redundant calls. */
3140 void *page_start = (void *)page_address(last_page);
3142 if (page_table[last_page].write_protected) {
3143 os_protect(page_start, PAGE_BYTES, OS_VM_PROT_ALL);
3144 page_table[last_page].write_protected = 0;
3149 while ((last_page < last_free_page)
3150 && (page_table[last_page].allocated != FREE_PAGE_FLAG)
3151 && (page_table[last_page].bytes_used != 0)
3152 && (page_table[last_page].gen == from_space));
3154 #ifdef READ_PROTECT_FREE_PAGES
3155 os_protect(page_address(first_page),
3156 PAGE_BYTES*(last_page-first_page),
3159 first_page = last_page;
3160 } while (first_page < last_free_page);
3162 bytes_allocated -= bytes_freed;
3167 /* Print some information about a pointer at the given address. */
3169 print_ptr(lispobj *addr)
3171 /* If addr is in the dynamic space then out the page information. */
3172 page_index_t pi1 = find_page_index((void*)addr);
3175 fprintf(stderr," %x: page %d alloc %d gen %d bytes_used %d offset %d dont_move %d\n",
3176 (unsigned long) addr,
3178 page_table[pi1].allocated,
3179 page_table[pi1].gen,
3180 page_table[pi1].bytes_used,
3181 page_table[pi1].first_object_offset,
3182 page_table[pi1].dont_move);
3183 fprintf(stderr," %x %x %x %x (%x) %x %x %x %x\n",
3196 extern long undefined_tramp;
3199 verify_space(lispobj *start, size_t words)
3201 int is_in_dynamic_space = (find_page_index((void*)start) != -1);
3202 int is_in_readonly_space =
3203 (READ_ONLY_SPACE_START <= (unsigned long)start &&
3204 (unsigned long)start < SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0));
3208 lispobj thing = *(lispobj*)start;
3210 if (is_lisp_pointer(thing)) {
3211 page_index_t page_index = find_page_index((void*)thing);
3212 long to_readonly_space =
3213 (READ_ONLY_SPACE_START <= thing &&
3214 thing < SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0));
3215 long to_static_space =
3216 (STATIC_SPACE_START <= thing &&
3217 thing < SymbolValue(STATIC_SPACE_FREE_POINTER,0));
3219 /* Does it point to the dynamic space? */
3220 if (page_index != -1) {
3221 /* If it's within the dynamic space it should point to a used
3222 * page. XX Could check the offset too. */
3223 if ((page_table[page_index].allocated != FREE_PAGE_FLAG)
3224 && (page_table[page_index].bytes_used == 0))
3225 lose ("Ptr %x @ %x sees free page.\n", thing, start);
3226 /* Check that it doesn't point to a forwarding pointer! */
3227 if (*((lispobj *)native_pointer(thing)) == 0x01) {
3228 lose("Ptr %x @ %x sees forwarding ptr.\n", thing, start);
3230 /* Check that its not in the RO space as it would then be a
3231 * pointer from the RO to the dynamic space. */
3232 if (is_in_readonly_space) {
3233 lose("ptr to dynamic space %x from RO space %x\n",
3236 /* Does it point to a plausible object? This check slows
3237 * it down a lot (so it's commented out).
3239 * "a lot" is serious: it ate 50 minutes cpu time on
3240 * my duron 950 before I came back from lunch and
3243 * FIXME: Add a variable to enable this
3246 if (!possibly_valid_dynamic_space_pointer((lispobj *)thing)) {
3247 lose("ptr %x to invalid object %x\n", thing, start);
3251 /* Verify that it points to another valid space. */
3252 if (!to_readonly_space && !to_static_space
3253 && (thing != (unsigned long)&undefined_tramp)) {
3254 lose("Ptr %x @ %x sees junk.\n", thing, start);
3258 if (!(fixnump(thing))) {
3260 switch(widetag_of(*start)) {
3263 case SIMPLE_VECTOR_WIDETAG:
3265 case COMPLEX_WIDETAG:
3266 case SIMPLE_ARRAY_WIDETAG:
3267 case COMPLEX_BASE_STRING_WIDETAG:
3268 #ifdef COMPLEX_CHARACTER_STRING_WIDETAG
3269 case COMPLEX_CHARACTER_STRING_WIDETAG:
3271 case COMPLEX_VECTOR_NIL_WIDETAG:
3272 case COMPLEX_BIT_VECTOR_WIDETAG:
3273 case COMPLEX_VECTOR_WIDETAG:
3274 case COMPLEX_ARRAY_WIDETAG:
3275 case CLOSURE_HEADER_WIDETAG:
3276 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
3277 case VALUE_CELL_HEADER_WIDETAG:
3278 case SYMBOL_HEADER_WIDETAG:
3279 case CHARACTER_WIDETAG:
3280 #if N_WORD_BITS == 64
3281 case SINGLE_FLOAT_WIDETAG:
3283 case UNBOUND_MARKER_WIDETAG:
3288 case INSTANCE_HEADER_WIDETAG:
3291 long ntotal = HeaderValue(thing);
3292 lispobj layout = ((struct instance *)start)->slots[0];
3297 nuntagged = ((struct layout *)native_pointer(layout))->n_untagged_slots;
3298 verify_space(start + 1, ntotal - fixnum_value(nuntagged));
3302 case CODE_HEADER_WIDETAG:
3304 lispobj object = *start;
3306 long nheader_words, ncode_words, nwords;
3308 struct simple_fun *fheaderp;
3310 code = (struct code *) start;
3312 /* Check that it's not in the dynamic space.
3313 * FIXME: Isn't is supposed to be OK for code
3314 * objects to be in the dynamic space these days? */
3315 if (is_in_dynamic_space
3316 /* It's ok if it's byte compiled code. The trace
3317 * table offset will be a fixnum if it's x86
3318 * compiled code - check.
3320 * FIXME: #^#@@! lack of abstraction here..
3321 * This line can probably go away now that
3322 * there's no byte compiler, but I've got
3323 * too much to worry about right now to try
3324 * to make sure. -- WHN 2001-10-06 */
3325 && fixnump(code->trace_table_offset)
3326 /* Only when enabled */
3327 && verify_dynamic_code_check) {
3329 "/code object at %x in the dynamic space\n",
3333 ncode_words = fixnum_value(code->code_size);
3334 nheader_words = HeaderValue(object);
3335 nwords = ncode_words + nheader_words;
3336 nwords = CEILING(nwords, 2);
3337 /* Scavenge the boxed section of the code data block */
3338 verify_space(start + 1, nheader_words - 1);
3340 /* Scavenge the boxed section of each function
3341 * object in the code data block. */
3342 fheaderl = code->entry_points;
3343 while (fheaderl != NIL) {
3345 (struct simple_fun *) native_pointer(fheaderl);
3346 gc_assert(widetag_of(fheaderp->header) == SIMPLE_FUN_HEADER_WIDETAG);
3347 verify_space(&fheaderp->name, 1);
3348 verify_space(&fheaderp->arglist, 1);
3349 verify_space(&fheaderp->type, 1);
3350 fheaderl = fheaderp->next;
3356 /* unboxed objects */
3357 case BIGNUM_WIDETAG:
3358 #if N_WORD_BITS != 64
3359 case SINGLE_FLOAT_WIDETAG:
3361 case DOUBLE_FLOAT_WIDETAG:
3362 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
3363 case LONG_FLOAT_WIDETAG:
3365 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
3366 case COMPLEX_SINGLE_FLOAT_WIDETAG:
3368 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
3369 case COMPLEX_DOUBLE_FLOAT_WIDETAG:
3371 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
3372 case COMPLEX_LONG_FLOAT_WIDETAG:
3374 case SIMPLE_BASE_STRING_WIDETAG:
3375 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
3376 case SIMPLE_CHARACTER_STRING_WIDETAG:
3378 case SIMPLE_BIT_VECTOR_WIDETAG:
3379 case SIMPLE_ARRAY_NIL_WIDETAG:
3380 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
3381 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
3382 case SIMPLE_ARRAY_UNSIGNED_BYTE_7_WIDETAG:
3383 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
3384 case SIMPLE_ARRAY_UNSIGNED_BYTE_15_WIDETAG:
3385 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
3386 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_29_WIDETAG
3387 case SIMPLE_ARRAY_UNSIGNED_BYTE_29_WIDETAG:
3389 case SIMPLE_ARRAY_UNSIGNED_BYTE_31_WIDETAG:
3390 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
3391 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_60_WIDETAG
3392 case SIMPLE_ARRAY_UNSIGNED_BYTE_60_WIDETAG:
3394 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG
3395 case SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG:
3397 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG
3398 case SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG:
3400 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
3401 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
3403 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
3404 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
3406 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
3407 case SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG:
3409 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
3410 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
3412 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_61_WIDETAG
3413 case SIMPLE_ARRAY_SIGNED_BYTE_61_WIDETAG:
3415 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG
3416 case SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG:
3418 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
3419 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
3420 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
3421 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
3423 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
3424 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
3426 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
3427 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
3429 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
3430 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
3433 case WEAK_POINTER_WIDETAG:
3434 count = (sizetab[widetag_of(*start)])(start);
3450 /* FIXME: It would be nice to make names consistent so that
3451 * foo_size meant size *in* *bytes* instead of size in some
3452 * arbitrary units. (Yes, this caused a bug, how did you guess?:-)
3453 * Some counts of lispobjs are called foo_count; it might be good
3454 * to grep for all foo_size and rename the appropriate ones to
3456 long read_only_space_size =
3457 (lispobj*)SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0)
3458 - (lispobj*)READ_ONLY_SPACE_START;
3459 long static_space_size =
3460 (lispobj*)SymbolValue(STATIC_SPACE_FREE_POINTER,0)
3461 - (lispobj*)STATIC_SPACE_START;
3463 for_each_thread(th) {
3464 long binding_stack_size =
3465 (lispobj*)SymbolValue(BINDING_STACK_POINTER,th)
3466 - (lispobj*)th->binding_stack_start;
3467 verify_space(th->binding_stack_start, binding_stack_size);
3469 verify_space((lispobj*)READ_ONLY_SPACE_START, read_only_space_size);
3470 verify_space((lispobj*)STATIC_SPACE_START , static_space_size);
3474 verify_generation(generation_index_t generation)
3478 for (i = 0; i < last_free_page; i++) {
3479 if ((page_table[i].allocated != FREE_PAGE_FLAG)
3480 && (page_table[i].bytes_used != 0)
3481 && (page_table[i].gen == generation)) {
3482 page_index_t last_page;
3483 int region_allocation = page_table[i].allocated;
3485 /* This should be the start of a contiguous block */
3486 gc_assert(page_table[i].first_object_offset == 0);
3488 /* Need to find the full extent of this contiguous block in case
3489 objects span pages. */
3491 /* Now work forward until the end of this contiguous area is
3493 for (last_page = i; ;last_page++)
3494 /* Check whether this is the last page in this contiguous
3496 if ((page_table[last_page].bytes_used < PAGE_BYTES)
3497 /* Or it is PAGE_BYTES and is the last in the block */
3498 || (page_table[last_page+1].allocated != region_allocation)
3499 || (page_table[last_page+1].bytes_used == 0)
3500 || (page_table[last_page+1].gen != generation)
3501 || (page_table[last_page+1].first_object_offset == 0))
3504 verify_space(page_address(i), (page_table[last_page].bytes_used
3505 + (last_page-i)*PAGE_BYTES)/N_WORD_BYTES);
3511 /* Check that all the free space is zero filled. */
3513 verify_zero_fill(void)
3517 for (page = 0; page < last_free_page; page++) {
3518 if (page_table[page].allocated == FREE_PAGE_FLAG) {
3519 /* The whole page should be zero filled. */
3520 long *start_addr = (long *)page_address(page);
3523 for (i = 0; i < size; i++) {
3524 if (start_addr[i] != 0) {
3525 lose("free page not zero at %x\n", start_addr + i);
3529 long free_bytes = PAGE_BYTES - page_table[page].bytes_used;
3530 if (free_bytes > 0) {
3531 long *start_addr = (long *)((unsigned long)page_address(page)
3532 + page_table[page].bytes_used);
3533 long size = free_bytes / N_WORD_BYTES;
3535 for (i = 0; i < size; i++) {
3536 if (start_addr[i] != 0) {
3537 lose("free region not zero at %x\n", start_addr + i);
3545 /* External entry point for verify_zero_fill */
3547 gencgc_verify_zero_fill(void)
3549 /* Flush the alloc regions updating the tables. */
3550 gc_alloc_update_all_page_tables();
3551 SHOW("verifying zero fill");
3556 verify_dynamic_space(void)
3558 generation_index_t i;
3560 for (i = 0; i <= HIGHEST_NORMAL_GENERATION; i++)
3561 verify_generation(i);
3563 if (gencgc_enable_verify_zero_fill)
3567 /* Write-protect all the dynamic boxed pages in the given generation. */
3569 write_protect_generation_pages(generation_index_t generation)
3573 gc_assert(generation < SCRATCH_GENERATION);
3575 for (start = 0; start < last_free_page; start++) {
3576 if ((page_table[start].allocated == BOXED_PAGE_FLAG)
3577 && (page_table[start].bytes_used != 0)
3578 && !page_table[start].dont_move
3579 && (page_table[start].gen == generation)) {
3583 /* Note the page as protected in the page tables. */
3584 page_table[start].write_protected = 1;
3586 for (last = start + 1; last < last_free_page; last++) {
3587 if ((page_table[last].allocated != BOXED_PAGE_FLAG)
3588 || (page_table[last].bytes_used == 0)
3589 || page_table[last].dont_move
3590 || (page_table[last].gen != generation))
3592 page_table[last].write_protected = 1;
3595 page_start = (void *)page_address(start);
3597 os_protect(page_start,
3598 PAGE_BYTES * (last - start),
3599 OS_VM_PROT_READ | OS_VM_PROT_EXECUTE);
3605 if (gencgc_verbose > 1) {
3607 "/write protected %d of %d pages in generation %d\n",
3608 count_write_protect_generation_pages(generation),
3609 count_generation_pages(generation),
3614 /* Garbage collect a generation. If raise is 0 then the remains of the
3615 * generation are not raised to the next generation. */
3617 garbage_collect_generation(generation_index_t generation, int raise)
3619 unsigned long bytes_freed;
3621 unsigned long static_space_size;
3623 gc_assert(generation <= HIGHEST_NORMAL_GENERATION);
3625 /* The oldest generation can't be raised. */
3626 gc_assert((generation != HIGHEST_NORMAL_GENERATION) || (raise == 0));
3628 /* Initialize the weak pointer list. */
3629 weak_pointers = NULL;
3631 /* When a generation is not being raised it is transported to a
3632 * temporary generation (NUM_GENERATIONS), and lowered when
3633 * done. Set up this new generation. There should be no pages
3634 * allocated to it yet. */
3636 gc_assert(generations[SCRATCH_GENERATION].bytes_allocated == 0);
3639 /* Set the global src and dest. generations */
3640 from_space = generation;
3642 new_space = generation+1;
3644 new_space = SCRATCH_GENERATION;
3646 /* Change to a new space for allocation, resetting the alloc_start_page */
3647 gc_alloc_generation = new_space;
3648 generations[new_space].alloc_start_page = 0;
3649 generations[new_space].alloc_unboxed_start_page = 0;
3650 generations[new_space].alloc_large_start_page = 0;
3651 generations[new_space].alloc_large_unboxed_start_page = 0;
3653 /* Before any pointers are preserved, the dont_move flags on the
3654 * pages need to be cleared. */
3655 for (i = 0; i < last_free_page; i++)
3656 if(page_table[i].gen==from_space)
3657 page_table[i].dont_move = 0;
3659 /* Un-write-protect the old-space pages. This is essential for the
3660 * promoted pages as they may contain pointers into the old-space
3661 * which need to be scavenged. It also helps avoid unnecessary page
3662 * faults as forwarding pointers are written into them. They need to
3663 * be un-protected anyway before unmapping later. */
3664 unprotect_oldspace();
3666 /* Scavenge the stacks' conservative roots. */
3668 /* there are potentially two stacks for each thread: the main
3669 * stack, which may contain Lisp pointers, and the alternate stack.
3670 * We don't ever run Lisp code on the altstack, but it may
3671 * host a sigcontext with lisp objects in it */
3673 /* what we need to do: (1) find the stack pointer for the main
3674 * stack; scavenge it (2) find the interrupt context on the
3675 * alternate stack that might contain lisp values, and scavenge
3678 /* we assume that none of the preceding applies to the thread that
3679 * initiates GC. If you ever call GC from inside an altstack
3680 * handler, you will lose. */
3682 /* And if we're saving a core, there's no point in being conservative. */
3683 if (conservative_stack) {
3684 for_each_thread(th) {
3686 void **esp=(void **)-1;
3687 #ifdef LISP_FEATURE_SB_THREAD
3689 if(th==arch_os_get_current_thread()) {
3690 /* Somebody is going to burn in hell for this, but casting
3691 * it in two steps shuts gcc up about strict aliasing. */
3692 esp = (void **)((void *)&raise);
3695 free=fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,th));
3696 for(i=free-1;i>=0;i--) {
3697 os_context_t *c=th->interrupt_contexts[i];
3698 esp1 = (void **) *os_context_register_addr(c,reg_SP);
3699 if (esp1>=(void **)th->control_stack_start &&
3700 esp1<(void **)th->control_stack_end) {
3701 if(esp1<esp) esp=esp1;
3702 for(ptr = (void **)(c+1); ptr>=(void **)c; ptr--) {
3703 preserve_pointer(*ptr);
3709 esp = (void **)((void *)&raise);
3711 for (ptr = (void **)th->control_stack_end; ptr > esp; ptr--) {
3712 preserve_pointer(*ptr);
3717 if (gencgc_verbose > 1) {
3718 long num_dont_move_pages = count_dont_move_pages();
3720 "/non-movable pages due to conservative pointers = %d (%d bytes)\n",
3721 num_dont_move_pages,
3722 num_dont_move_pages * PAGE_BYTES);
3726 /* Scavenge all the rest of the roots. */
3728 /* Scavenge the Lisp functions of the interrupt handlers, taking
3729 * care to avoid SIG_DFL and SIG_IGN. */
3730 for (i = 0; i < NSIG; i++) {
3731 union interrupt_handler handler = interrupt_handlers[i];
3732 if (!ARE_SAME_HANDLER(handler.c, SIG_IGN) &&
3733 !ARE_SAME_HANDLER(handler.c, SIG_DFL)) {
3734 scavenge((lispobj *)(interrupt_handlers + i), 1);
3737 /* Scavenge the binding stacks. */
3740 for_each_thread(th) {
3741 long len= (lispobj *)SymbolValue(BINDING_STACK_POINTER,th) -
3742 th->binding_stack_start;
3743 scavenge((lispobj *) th->binding_stack_start,len);
3744 #ifdef LISP_FEATURE_SB_THREAD
3745 /* do the tls as well */
3746 len=fixnum_value(SymbolValue(FREE_TLS_INDEX,0)) -
3747 (sizeof (struct thread))/(sizeof (lispobj));
3748 scavenge((lispobj *) (th+1),len);
3753 /* The original CMU CL code had scavenge-read-only-space code
3754 * controlled by the Lisp-level variable
3755 * *SCAVENGE-READ-ONLY-SPACE*. It was disabled by default, and it
3756 * wasn't documented under what circumstances it was useful or
3757 * safe to turn it on, so it's been turned off in SBCL. If you
3758 * want/need this functionality, and can test and document it,
3759 * please submit a patch. */
3761 if (SymbolValue(SCAVENGE_READ_ONLY_SPACE) != NIL) {
3762 unsigned long read_only_space_size =
3763 (lispobj*)SymbolValue(READ_ONLY_SPACE_FREE_POINTER) -
3764 (lispobj*)READ_ONLY_SPACE_START;
3766 "/scavenge read only space: %d bytes\n",
3767 read_only_space_size * sizeof(lispobj)));
3768 scavenge( (lispobj *) READ_ONLY_SPACE_START, read_only_space_size);
3772 /* Scavenge static space. */
3774 (lispobj *)SymbolValue(STATIC_SPACE_FREE_POINTER,0) -
3775 (lispobj *)STATIC_SPACE_START;
3776 if (gencgc_verbose > 1) {
3778 "/scavenge static space: %d bytes\n",
3779 static_space_size * sizeof(lispobj)));
3781 scavenge( (lispobj *) STATIC_SPACE_START, static_space_size);
3783 /* All generations but the generation being GCed need to be
3784 * scavenged. The new_space generation needs special handling as
3785 * objects may be moved in - it is handled separately below. */
3786 scavenge_generations(generation+1, PSEUDO_STATIC_GENERATION);
3788 /* Finally scavenge the new_space generation. Keep going until no
3789 * more objects are moved into the new generation */
3790 scavenge_newspace_generation(new_space);
3792 /* FIXME: I tried reenabling this check when debugging unrelated
3793 * GC weirdness ca. sbcl-0.6.12.45, and it failed immediately.
3794 * Since the current GC code seems to work well, I'm guessing that
3795 * this debugging code is just stale, but I haven't tried to
3796 * figure it out. It should be figured out and then either made to
3797 * work or just deleted. */
3798 #define RESCAN_CHECK 0
3800 /* As a check re-scavenge the newspace once; no new objects should
3803 long old_bytes_allocated = bytes_allocated;
3804 long bytes_allocated;
3806 /* Start with a full scavenge. */
3807 scavenge_newspace_generation_one_scan(new_space);
3809 /* Flush the current regions, updating the tables. */
3810 gc_alloc_update_all_page_tables();
3812 bytes_allocated = bytes_allocated - old_bytes_allocated;
3814 if (bytes_allocated != 0) {
3815 lose("Rescan of new_space allocated %d more bytes.\n",
3821 scan_weak_pointers();
3823 /* Flush the current regions, updating the tables. */
3824 gc_alloc_update_all_page_tables();
3826 /* Free the pages in oldspace, but not those marked dont_move. */
3827 bytes_freed = free_oldspace();
3829 /* If the GC is not raising the age then lower the generation back
3830 * to its normal generation number */
3832 for (i = 0; i < last_free_page; i++)
3833 if ((page_table[i].bytes_used != 0)
3834 && (page_table[i].gen == SCRATCH_GENERATION))
3835 page_table[i].gen = generation;
3836 gc_assert(generations[generation].bytes_allocated == 0);
3837 generations[generation].bytes_allocated =
3838 generations[SCRATCH_GENERATION].bytes_allocated;
3839 generations[SCRATCH_GENERATION].bytes_allocated = 0;
3842 /* Reset the alloc_start_page for generation. */
3843 generations[generation].alloc_start_page = 0;
3844 generations[generation].alloc_unboxed_start_page = 0;
3845 generations[generation].alloc_large_start_page = 0;
3846 generations[generation].alloc_large_unboxed_start_page = 0;
3848 if (generation >= verify_gens) {
3852 verify_dynamic_space();
3855 /* Set the new gc trigger for the GCed generation. */
3856 generations[generation].gc_trigger =
3857 generations[generation].bytes_allocated
3858 + generations[generation].bytes_consed_between_gc;
3861 generations[generation].num_gc = 0;
3863 ++generations[generation].num_gc;
3866 /* Update last_free_page, then SymbolValue(ALLOCATION_POINTER). */
3868 update_dynamic_space_free_pointer(void)
3870 page_index_t last_page = -1, i;
3872 for (i = 0; i < last_free_page; i++)
3873 if ((page_table[i].allocated != FREE_PAGE_FLAG)
3874 && (page_table[i].bytes_used != 0))
3877 last_free_page = last_page+1;
3879 SetSymbolValue(ALLOCATION_POINTER,
3880 (lispobj)(((char *)heap_base) + last_free_page*PAGE_BYTES),0);
3881 return 0; /* dummy value: return something ... */
3885 remap_free_pages (page_index_t from, page_index_t to)
3887 page_index_t first_page, last_page;
3889 for (first_page = from; first_page <= to; first_page++) {
3890 if (page_table[first_page].allocated != FREE_PAGE_FLAG ||
3891 page_table[first_page].need_to_zero == 0) {
3895 last_page = first_page + 1;
3896 while (page_table[last_page].allocated == FREE_PAGE_FLAG &&
3898 page_table[last_page].need_to_zero == 1) {
3902 zero_pages_with_mmap(first_page, last_page-1);
3904 first_page = last_page;
3908 generation_index_t small_generation_limit = 1;
3910 /* GC all generations newer than last_gen, raising the objects in each
3911 * to the next older generation - we finish when all generations below
3912 * last_gen are empty. Then if last_gen is due for a GC, or if
3913 * last_gen==NUM_GENERATIONS (the scratch generation? eh?) we GC that
3914 * too. The valid range for last_gen is: 0,1,...,NUM_GENERATIONS.
3916 * We stop collecting at gencgc_oldest_gen_to_gc, even if this is less than
3917 * last_gen (oh, and note that by default it is NUM_GENERATIONS-1) */
3919 collect_garbage(generation_index_t last_gen)
3921 generation_index_t gen = 0, i;
3924 /* The largest value of last_free_page seen since the time
3925 * remap_free_pages was called. */
3926 static page_index_t high_water_mark = 0;
3928 FSHOW((stderr, "/entering collect_garbage(%d)\n", last_gen));
3930 if (last_gen > HIGHEST_NORMAL_GENERATION+1) {
3932 "/collect_garbage: last_gen = %d, doing a level 0 GC\n",
3937 /* Flush the alloc regions updating the tables. */
3938 gc_alloc_update_all_page_tables();
3940 /* Verify the new objects created by Lisp code. */
3941 if (pre_verify_gen_0) {
3942 FSHOW((stderr, "pre-checking generation 0\n"));
3943 verify_generation(0);
3946 if (gencgc_verbose > 1)
3947 print_generation_stats(0);
3950 /* Collect the generation. */
3952 if (gen >= gencgc_oldest_gen_to_gc) {
3953 /* Never raise the oldest generation. */
3958 || (generations[gen].num_gc >= generations[gen].trigger_age);
3961 if (gencgc_verbose > 1) {
3963 "starting GC of generation %d with raise=%d alloc=%d trig=%d GCs=%d\n",
3966 generations[gen].bytes_allocated,
3967 generations[gen].gc_trigger,
3968 generations[gen].num_gc));
3971 /* If an older generation is being filled, then update its
3974 generations[gen+1].cum_sum_bytes_allocated +=
3975 generations[gen+1].bytes_allocated;
3978 garbage_collect_generation(gen, raise);
3980 /* Reset the memory age cum_sum. */
3981 generations[gen].cum_sum_bytes_allocated = 0;
3983 if (gencgc_verbose > 1) {
3984 FSHOW((stderr, "GC of generation %d finished:\n", gen));
3985 print_generation_stats(0);
3989 } while ((gen <= gencgc_oldest_gen_to_gc)
3990 && ((gen < last_gen)
3991 || ((gen <= gencgc_oldest_gen_to_gc)
3993 && (generations[gen].bytes_allocated
3994 > generations[gen].gc_trigger)
3995 && (gen_av_mem_age(gen)
3996 > generations[gen].min_av_mem_age))));
3998 /* Now if gen-1 was raised all generations before gen are empty.
3999 * If it wasn't raised then all generations before gen-1 are empty.
4001 * Now objects within this gen's pages cannot point to younger
4002 * generations unless they are written to. This can be exploited
4003 * by write-protecting the pages of gen; then when younger
4004 * generations are GCed only the pages which have been written
4009 gen_to_wp = gen - 1;
4011 /* There's not much point in WPing pages in generation 0 as it is
4012 * never scavenged (except promoted pages). */
4013 if ((gen_to_wp > 0) && enable_page_protection) {
4014 /* Check that they are all empty. */
4015 for (i = 0; i < gen_to_wp; i++) {
4016 if (generations[i].bytes_allocated)
4017 lose("trying to write-protect gen. %d when gen. %d nonempty\n",
4020 write_protect_generation_pages(gen_to_wp);
4023 /* Set gc_alloc() back to generation 0. The current regions should
4024 * be flushed after the above GCs. */
4025 gc_assert((boxed_region.free_pointer - boxed_region.start_addr) == 0);
4026 gc_alloc_generation = 0;
4028 /* Save the high-water mark before updating last_free_page */
4029 if (last_free_page > high_water_mark)
4030 high_water_mark = last_free_page;
4031 update_dynamic_space_free_pointer();
4032 auto_gc_trigger = bytes_allocated + bytes_consed_between_gcs;
4034 fprintf(stderr,"Next gc when %ld bytes have been consed\n",
4037 /* If we did a big GC (arbitrarily defined as gen > 1), release memory
4040 if (gen > small_generation_limit) {
4041 if (last_free_page > high_water_mark)
4042 high_water_mark = last_free_page;
4043 remap_free_pages(0, high_water_mark);
4044 high_water_mark = 0;
4047 SHOW("returning from collect_garbage");
4050 /* This is called by Lisp PURIFY when it is finished. All live objects
4051 * will have been moved to the RO and Static heaps. The dynamic space
4052 * will need a full re-initialization. We don't bother having Lisp
4053 * PURIFY flush the current gc_alloc() region, as the page_tables are
4054 * re-initialized, and every page is zeroed to be sure. */
4060 if (gencgc_verbose > 1)
4061 SHOW("entering gc_free_heap");
4063 for (page = 0; page < NUM_PAGES; page++) {
4064 /* Skip free pages which should already be zero filled. */
4065 if (page_table[page].allocated != FREE_PAGE_FLAG) {
4066 void *page_start, *addr;
4068 /* Mark the page free. The other slots are assumed invalid
4069 * when it is a FREE_PAGE_FLAG and bytes_used is 0 and it
4070 * should not be write-protected -- except that the
4071 * generation is used for the current region but it sets
4073 page_table[page].allocated = FREE_PAGE_FLAG;
4074 page_table[page].bytes_used = 0;
4076 #ifndef LISP_FEATURE_WIN32 /* Pages already zeroed on win32? Not sure about this change. */
4077 /* Zero the page. */
4078 page_start = (void *)page_address(page);
4080 /* First, remove any write-protection. */
4081 os_protect(page_start, PAGE_BYTES, OS_VM_PROT_ALL);
4082 page_table[page].write_protected = 0;
4084 os_invalidate(page_start,PAGE_BYTES);
4085 addr = os_validate(page_start,PAGE_BYTES);
4086 if (addr == NULL || addr != page_start) {
4087 lose("gc_free_heap: page moved, 0x%08x ==> 0x%08x\n",
4092 page_table[page].write_protected = 0;
4094 } else if (gencgc_zero_check_during_free_heap) {
4095 /* Double-check that the page is zero filled. */
4098 gc_assert(page_table[page].allocated == FREE_PAGE_FLAG);
4099 gc_assert(page_table[page].bytes_used == 0);
4100 page_start = (long *)page_address(page);
4101 for (i=0; i<1024; i++) {
4102 if (page_start[i] != 0) {
4103 lose("free region not zero at %x\n", page_start + i);
4109 bytes_allocated = 0;
4111 /* Initialize the generations. */
4112 for (page = 0; page < NUM_GENERATIONS; page++) {
4113 generations[page].alloc_start_page = 0;
4114 generations[page].alloc_unboxed_start_page = 0;
4115 generations[page].alloc_large_start_page = 0;
4116 generations[page].alloc_large_unboxed_start_page = 0;
4117 generations[page].bytes_allocated = 0;
4118 generations[page].gc_trigger = 2000000;
4119 generations[page].num_gc = 0;
4120 generations[page].cum_sum_bytes_allocated = 0;
4123 if (gencgc_verbose > 1)
4124 print_generation_stats(0);
4126 /* Initialize gc_alloc(). */
4127 gc_alloc_generation = 0;
4129 gc_set_region_empty(&boxed_region);
4130 gc_set_region_empty(&unboxed_region);
4133 SetSymbolValue(ALLOCATION_POINTER, (lispobj)((char *)heap_base),0);
4135 if (verify_after_free_heap) {
4136 /* Check whether purify has left any bad pointers. */
4138 SHOW("checking after free_heap\n");
4149 scavtab[SIMPLE_VECTOR_WIDETAG] = scav_vector;
4150 scavtab[WEAK_POINTER_WIDETAG] = scav_weak_pointer;
4151 transother[SIMPLE_ARRAY_WIDETAG] = trans_boxed_large;
4153 heap_base = (void*)DYNAMIC_SPACE_START;
4155 /* Initialize each page structure. */
4156 for (i = 0; i < NUM_PAGES; i++) {
4157 /* Initialize all pages as free. */
4158 page_table[i].allocated = FREE_PAGE_FLAG;
4159 page_table[i].bytes_used = 0;
4161 /* Pages are not write-protected at startup. */
4162 page_table[i].write_protected = 0;
4165 bytes_allocated = 0;
4167 /* Initialize the generations.
4169 * FIXME: very similar to code in gc_free_heap(), should be shared */
4170 for (i = 0; i < NUM_GENERATIONS; i++) {
4171 generations[i].alloc_start_page = 0;
4172 generations[i].alloc_unboxed_start_page = 0;
4173 generations[i].alloc_large_start_page = 0;
4174 generations[i].alloc_large_unboxed_start_page = 0;
4175 generations[i].bytes_allocated = 0;
4176 generations[i].gc_trigger = 2000000;
4177 generations[i].num_gc = 0;
4178 generations[i].cum_sum_bytes_allocated = 0;
4179 /* the tune-able parameters */
4180 generations[i].bytes_consed_between_gc = 2000000;
4181 generations[i].trigger_age = 1;
4182 generations[i].min_av_mem_age = 0.75;
4185 /* Initialize gc_alloc. */
4186 gc_alloc_generation = 0;
4187 gc_set_region_empty(&boxed_region);
4188 gc_set_region_empty(&unboxed_region);
4193 /* Pick up the dynamic space from after a core load.
4195 * The ALLOCATION_POINTER points to the end of the dynamic space.
4199 gencgc_pickup_dynamic(void)
4201 page_index_t page = 0;
4202 long alloc_ptr = SymbolValue(ALLOCATION_POINTER,0);
4203 lispobj *prev=(lispobj *)page_address(page);
4204 generation_index_t gen = PSEUDO_STATIC_GENERATION;
4207 lispobj *first,*ptr= (lispobj *)page_address(page);
4208 page_table[page].allocated = BOXED_PAGE_FLAG;
4209 page_table[page].gen = gen;
4210 page_table[page].bytes_used = PAGE_BYTES;
4211 page_table[page].large_object = 0;
4212 page_table[page].write_protected = 0;
4213 page_table[page].write_protected_cleared = 0;
4214 page_table[page].dont_move = 0;
4215 page_table[page].need_to_zero = 1;
4217 if (!gencgc_partial_pickup) {
4218 first=gc_search_space(prev,(ptr+2)-prev,ptr);
4219 if(ptr == first) prev=ptr;
4220 page_table[page].first_object_offset =
4221 (void *)prev - page_address(page);
4224 } while ((long)page_address(page) < alloc_ptr);
4226 last_free_page = page;
4228 generations[gen].bytes_allocated = PAGE_BYTES*page;
4229 bytes_allocated = PAGE_BYTES*page;
4231 gc_alloc_update_all_page_tables();
4232 write_protect_generation_pages(gen);
4236 gc_initialize_pointers(void)
4238 gencgc_pickup_dynamic();
4244 /* alloc(..) is the external interface for memory allocation. It
4245 * allocates to generation 0. It is not called from within the garbage
4246 * collector as it is only external uses that need the check for heap
4247 * size (GC trigger) and to disable the interrupts (interrupts are
4248 * always disabled during a GC).
4250 * The vops that call alloc(..) assume that the returned space is zero-filled.
4251 * (E.g. the most significant word of a 2-word bignum in MOVE-FROM-UNSIGNED.)
4253 * The check for a GC trigger is only performed when the current
4254 * region is full, so in most cases it's not needed. */
4259 struct thread *thread=arch_os_get_current_thread();
4260 struct alloc_region *region=
4261 #ifdef LISP_FEATURE_SB_THREAD
4262 thread ? &(thread->alloc_region) : &boxed_region;
4267 void *new_free_pointer;
4268 gc_assert(nbytes>0);
4269 /* Check for alignment allocation problems. */
4270 gc_assert((((unsigned long)region->free_pointer & LOWTAG_MASK) == 0)
4271 && ((nbytes & LOWTAG_MASK) == 0));
4274 /* there are a few places in the C code that allocate data in the
4275 * heap before Lisp starts. This is before interrupts are enabled,
4276 * so we don't need to check for pseudo-atomic */
4277 #ifdef LISP_FEATURE_SB_THREAD
4278 if(!SymbolValue(PSEUDO_ATOMIC_ATOMIC,th)) {
4280 fprintf(stderr, "fatal error in thread 0x%x, tid=%ld\n",
4282 __asm__("movl %fs,%0" : "=r" (fs) : );
4283 fprintf(stderr, "fs is %x, th->tls_cookie=%x \n",
4284 debug_get_fs(),th->tls_cookie);
4285 lose("If you see this message before 2004.01.31, mail details to sbcl-devel\n");
4288 gc_assert(SymbolValue(PSEUDO_ATOMIC_ATOMIC,th));
4292 /* maybe we can do this quickly ... */
4293 new_free_pointer = region->free_pointer + nbytes;
4294 if (new_free_pointer <= region->end_addr) {
4295 new_obj = (void*)(region->free_pointer);
4296 region->free_pointer = new_free_pointer;
4297 return(new_obj); /* yup */
4300 /* we have to go the long way around, it seems. Check whether
4301 * we should GC in the near future
4303 if (auto_gc_trigger && bytes_allocated > auto_gc_trigger) {
4304 gc_assert(fixnum_value(SymbolValue(PSEUDO_ATOMIC_ATOMIC,thread)));
4305 /* Don't flood the system with interrupts if the need to gc is
4306 * already noted. This can happen for example when SUB-GC
4307 * allocates or after a gc triggered in a WITHOUT-GCING. */
4308 if (SymbolValue(GC_PENDING,thread) == NIL) {
4309 /* set things up so that GC happens when we finish the PA
4311 SetSymbolValue(GC_PENDING,T,thread);
4312 if (SymbolValue(GC_INHIBIT,thread) == NIL)
4313 arch_set_pseudo_atomic_interrupted(0);
4316 new_obj = gc_alloc_with_region(nbytes,0,region,0);
4321 * shared support for the OS-dependent signal handlers which
4322 * catch GENCGC-related write-protect violations
4325 void unhandled_sigmemoryfault(void);
4327 /* Depending on which OS we're running under, different signals might
4328 * be raised for a violation of write protection in the heap. This
4329 * function factors out the common generational GC magic which needs
4330 * to invoked in this case, and should be called from whatever signal
4331 * handler is appropriate for the OS we're running under.
4333 * Return true if this signal is a normal generational GC thing that
4334 * we were able to handle, or false if it was abnormal and control
4335 * should fall through to the general SIGSEGV/SIGBUS/whatever logic. */
4338 gencgc_handle_wp_violation(void* fault_addr)
4340 page_index_t page_index = find_page_index(fault_addr);
4342 #ifdef QSHOW_SIGNALS
4343 FSHOW((stderr, "heap WP violation? fault_addr=%x, page_index=%d\n",
4344 fault_addr, page_index));
4347 /* Check whether the fault is within the dynamic space. */
4348 if (page_index == (-1)) {
4350 /* It can be helpful to be able to put a breakpoint on this
4351 * case to help diagnose low-level problems. */
4352 unhandled_sigmemoryfault();
4354 /* not within the dynamic space -- not our responsibility */
4358 if (page_table[page_index].write_protected) {
4359 /* Unprotect the page. */
4360 os_protect(page_address(page_index), PAGE_BYTES, OS_VM_PROT_ALL);
4361 page_table[page_index].write_protected_cleared = 1;
4362 page_table[page_index].write_protected = 0;
4364 /* The only acceptable reason for this signal on a heap
4365 * access is that GENCGC write-protected the page.
4366 * However, if two CPUs hit a wp page near-simultaneously,
4367 * we had better not have the second one lose here if it
4368 * does this test after the first one has already set wp=0
4370 if(page_table[page_index].write_protected_cleared != 1)
4371 lose("fault in heap page not marked as write-protected\n");
4373 /* Don't worry, we can handle it. */
4377 /* This is to be called when we catch a SIGSEGV/SIGBUS, determine that
4378 * it's not just a case of the program hitting the write barrier, and
4379 * are about to let Lisp deal with it. It's basically just a
4380 * convenient place to set a gdb breakpoint. */
4382 unhandled_sigmemoryfault()
4385 void gc_alloc_update_all_page_tables(void)
4387 /* Flush the alloc regions updating the tables. */
4390 gc_alloc_update_page_tables(0, &th->alloc_region);
4391 gc_alloc_update_page_tables(1, &unboxed_region);
4392 gc_alloc_update_page_tables(0, &boxed_region);
4396 gc_set_region_empty(struct alloc_region *region)
4398 region->first_page = 0;
4399 region->last_page = -1;
4400 region->start_addr = page_address(0);
4401 region->free_pointer = page_address(0);
4402 region->end_addr = page_address(0);
4406 zero_all_free_pages()
4410 for (i = 0; i < last_free_page; i++) {
4411 if (page_table[i].allocated == FREE_PAGE_FLAG) {
4412 #ifdef READ_PROTECT_FREE_PAGES
4413 os_protect(page_address(i),
4422 /* Things to do before doing a final GC before saving a core (without
4425 * + Pages in large_object pages aren't moved by the GC, so we need to
4426 * unset that flag from all pages.
4427 * + The pseudo-static generation isn't normally collected, but it seems
4428 * reasonable to collect it at least when saving a core. So move the
4429 * pages to a normal generation.
4432 prepare_for_final_gc ()
4435 for (i = 0; i < last_free_page; i++) {
4436 page_table[i].large_object = 0;
4437 if (page_table[i].gen == PSEUDO_STATIC_GENERATION) {
4438 int used = page_table[i].bytes_used;
4439 page_table[i].gen = HIGHEST_NORMAL_GENERATION;
4440 generations[PSEUDO_STATIC_GENERATION].bytes_allocated -= used;
4441 generations[HIGHEST_NORMAL_GENERATION].bytes_allocated += used;
4447 /* Do a non-conservative GC, and then save a core with the initial
4448 * function being set to the value of the static symbol
4449 * SB!VM:RESTART-LISP-FUNCTION */
4451 gc_and_save(char *filename, int prepend_runtime)
4454 void *runtime_bytes = NULL;
4455 size_t runtime_size;
4457 file = prepare_to_save(filename, prepend_runtime, &runtime_bytes, &runtime_size);
4461 conservative_stack = 0;
4463 /* The filename might come from Lisp, and be moved by the now
4464 * non-conservative GC. */
4465 filename = strdup(filename);
4467 /* Collect twice: once into relatively high memory, and then back
4468 * into low memory. This compacts the retained data into the lower
4469 * pages, minimizing the size of the core file.
4471 prepare_for_final_gc();
4472 gencgc_alloc_start_page = last_free_page;
4473 collect_garbage(HIGHEST_NORMAL_GENERATION+1);
4475 prepare_for_final_gc();
4476 gencgc_alloc_start_page = -1;
4477 collect_garbage(HIGHEST_NORMAL_GENERATION+1);
4479 if (prepend_runtime)
4480 save_runtime_to_filehandle(file, runtime_bytes, runtime_size);
4482 /* The dumper doesn't know that pages need to be zeroed before use. */
4483 zero_all_free_pages();
4484 save_to_filehandle(file, filename, SymbolValue(RESTART_LISP_FUNCTION,0),
4486 /* Oops. Save still managed to fail. Since we've mangled the stack
4487 * beyond hope, there's not much we can do.
4488 * (beyond FUNCALLing RESTART_LISP_FUNCTION, but I suspect that's
4489 * going to be rather unsatisfactory too... */
4490 lose("Attempt to save core after non-conservative GC failed.\n");