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"
53 #include "genesis/lutex.h"
56 /* forward declarations */
57 page_index_t gc_find_freeish_pages(long *restart_page_ptr, long nbytes,
65 /* Generations 0-5 are normal collected generations, 6 is only used as
66 * scratch space by the collector, and should never get collected.
69 HIGHEST_NORMAL_GENERATION = 5,
70 PSEUDO_STATIC_GENERATION,
75 /* Should we use page protection to help avoid the scavenging of pages
76 * that don't have pointers to younger generations? */
77 boolean enable_page_protection = 1;
79 /* the minimum size (in bytes) for a large object*/
80 unsigned long large_object_size = 4 * PAGE_BYTES;
87 /* the verbosity level. All non-error messages are disabled at level 0;
88 * and only a few rare messages are printed at level 1. */
90 boolean gencgc_verbose = 1;
92 boolean gencgc_verbose = 0;
95 /* FIXME: At some point enable the various error-checking things below
96 * and see what they say. */
98 /* We hunt for pointers to old-space, when GCing generations >= verify_gen.
99 * Set verify_gens to HIGHEST_NORMAL_GENERATION + 1 to disable this kind of
101 generation_index_t verify_gens = HIGHEST_NORMAL_GENERATION + 1;
103 /* Should we do a pre-scan verify of generation 0 before it's GCed? */
104 boolean pre_verify_gen_0 = 0;
106 /* Should we check for bad pointers after gc_free_heap is called
107 * from Lisp PURIFY? */
108 boolean verify_after_free_heap = 0;
110 /* Should we print a note when code objects are found in the dynamic space
111 * during a heap verify? */
112 boolean verify_dynamic_code_check = 0;
114 /* Should we check code objects for fixup errors after they are transported? */
115 boolean check_code_fixups = 0;
117 /* Should we check that newly allocated regions are zero filled? */
118 boolean gencgc_zero_check = 0;
120 /* Should we check that the free space is zero filled? */
121 boolean gencgc_enable_verify_zero_fill = 0;
123 /* Should we check that free pages are zero filled during gc_free_heap
124 * called after Lisp PURIFY? */
125 boolean gencgc_zero_check_during_free_heap = 0;
127 /* When loading a core, don't do a full scan of the memory for the
128 * memory region boundaries. (Set to true by coreparse.c if the core
129 * contained a pagetable entry).
131 boolean gencgc_partial_pickup = 0;
133 /* If defined, free pages are read-protected to ensure that nothing
137 /* #define READ_PROTECT_FREE_PAGES */
141 * GC structures and variables
144 /* the total bytes allocated. These are seen by Lisp DYNAMIC-USAGE. */
145 unsigned long bytes_allocated = 0;
146 extern unsigned long bytes_consed_between_gcs; /* gc-common.c */
147 unsigned long auto_gc_trigger = 0;
149 /* the source and destination generations. These are set before a GC starts
151 generation_index_t from_space;
152 generation_index_t new_space;
154 /* should the GC be conservative on stack. If false (only right before
155 * saving a core), don't scan the stack / mark pages dont_move. */
156 static boolean conservative_stack = 1;
158 /* An array of page structures is statically allocated.
159 * This helps quickly map between an address its page structure.
160 * NUM_PAGES is set from the size of the dynamic space. */
161 struct page page_table[NUM_PAGES];
163 /* To map addresses to page structures the address of the first page
165 static void *heap_base = NULL;
167 #if N_WORD_BITS == 32
168 #define SIMPLE_ARRAY_WORD_WIDETAG SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG
169 #elif N_WORD_BITS == 64
170 #define SIMPLE_ARRAY_WORD_WIDETAG SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG
173 /* Calculate the start address for the given page number. */
175 page_address(page_index_t page_num)
177 return (heap_base + (page_num * PAGE_BYTES));
180 /* Find the page index within the page_table for the given
181 * address. Return -1 on failure. */
183 find_page_index(void *addr)
185 page_index_t index = addr-heap_base;
188 index = ((unsigned long)index)/PAGE_BYTES;
189 if (index < NUM_PAGES)
196 /* a structure to hold the state of a generation */
199 /* the first page that gc_alloc() checks on its next call */
200 page_index_t alloc_start_page;
202 /* the first page that gc_alloc_unboxed() checks on its next call */
203 page_index_t alloc_unboxed_start_page;
205 /* the first page that gc_alloc_large (boxed) considers on its next
206 * call. (Although it always allocates after the boxed_region.) */
207 page_index_t alloc_large_start_page;
209 /* the first page that gc_alloc_large (unboxed) considers on its
210 * next call. (Although it always allocates after the
211 * current_unboxed_region.) */
212 page_index_t alloc_large_unboxed_start_page;
214 /* the bytes allocated to this generation */
215 long bytes_allocated;
217 /* the number of bytes at which to trigger a GC */
220 /* to calculate a new level for gc_trigger */
221 long bytes_consed_between_gc;
223 /* the number of GCs since the last raise */
226 /* the average age after which a GC will raise objects to the
230 /* the cumulative sum of the bytes allocated to this generation. It is
231 * cleared after a GC on this generations, and update before new
232 * objects are added from a GC of a younger generation. Dividing by
233 * the bytes_allocated will give the average age of the memory in
234 * this generation since its last GC. */
235 long cum_sum_bytes_allocated;
237 /* a minimum average memory age before a GC will occur helps
238 * prevent a GC when a large number of new live objects have been
239 * added, in which case a GC could be a waste of time */
240 double min_av_mem_age;
242 /* A linked list of lutex structures in this generation, used for
243 * implementing lutex finalization. */
245 struct lutex *lutexes;
251 /* an array of generation structures. There needs to be one more
252 * generation structure than actual generations as the oldest
253 * generation is temporarily raised then lowered. */
254 struct generation generations[NUM_GENERATIONS];
256 /* the oldest generation that is will currently be GCed by default.
257 * Valid values are: 0, 1, ... HIGHEST_NORMAL_GENERATION
259 * The default of HIGHEST_NORMAL_GENERATION enables GC on all generations.
261 * Setting this to 0 effectively disables the generational nature of
262 * the GC. In some applications generational GC may not be useful
263 * because there are no long-lived objects.
265 * An intermediate value could be handy after moving long-lived data
266 * into an older generation so an unnecessary GC of this long-lived
267 * data can be avoided. */
268 generation_index_t gencgc_oldest_gen_to_gc = HIGHEST_NORMAL_GENERATION;
270 /* The maximum free page in the heap is maintained and used to update
271 * ALLOCATION_POINTER which is used by the room function to limit its
272 * search of the heap. XX Gencgc obviously needs to be better
273 * integrated with the Lisp code. */
274 page_index_t last_free_page;
276 /* This lock is to prevent multiple threads from simultaneously
277 * allocating new regions which overlap each other. Note that the
278 * majority of GC is single-threaded, but alloc() may be called from
279 * >1 thread at a time and must be thread-safe. This lock must be
280 * seized before all accesses to generations[] or to parts of
281 * page_table[] that other threads may want to see */
283 #ifdef LISP_FEATURE_SB_THREAD
284 static pthread_mutex_t free_pages_lock = PTHREAD_MUTEX_INITIALIZER;
289 * miscellaneous heap functions
292 /* Count the number of pages which are write-protected within the
293 * given generation. */
295 count_write_protect_generation_pages(generation_index_t generation)
300 for (i = 0; i < last_free_page; i++)
301 if ((page_table[i].allocated != FREE_PAGE_FLAG)
302 && (page_table[i].gen == generation)
303 && (page_table[i].write_protected == 1))
308 /* Count the number of pages within the given generation. */
310 count_generation_pages(generation_index_t generation)
315 for (i = 0; i < last_free_page; i++)
316 if ((page_table[i].allocated != 0)
317 && (page_table[i].gen == generation))
324 count_dont_move_pages(void)
328 for (i = 0; i < last_free_page; i++) {
329 if ((page_table[i].allocated != 0) && (page_table[i].dont_move != 0)) {
337 /* Work through the pages and add up the number of bytes used for the
338 * given generation. */
340 count_generation_bytes_allocated (generation_index_t gen)
344 for (i = 0; i < last_free_page; i++) {
345 if ((page_table[i].allocated != 0) && (page_table[i].gen == gen))
346 result += page_table[i].bytes_used;
351 /* Return the average age of the memory in a generation. */
353 gen_av_mem_age(generation_index_t gen)
355 if (generations[gen].bytes_allocated == 0)
359 ((double)generations[gen].cum_sum_bytes_allocated)
360 / ((double)generations[gen].bytes_allocated);
363 /* The verbose argument controls how much to print: 0 for normal
364 * level of detail; 1 for debugging. */
366 print_generation_stats(int verbose) /* FIXME: should take FILE argument */
368 generation_index_t i, gens;
370 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
371 #define FPU_STATE_SIZE 27
372 int fpu_state[FPU_STATE_SIZE];
373 #elif defined(LISP_FEATURE_PPC)
374 #define FPU_STATE_SIZE 32
375 long long fpu_state[FPU_STATE_SIZE];
378 /* This code uses the FP instructions which may be set up for Lisp
379 * so they need to be saved and reset for C. */
382 /* highest generation to print */
384 gens = SCRATCH_GENERATION;
386 gens = PSEUDO_STATIC_GENERATION;
388 /* Print the heap stats. */
390 " Gen StaPg UbSta LaSta LUbSt Boxed Unboxed LB LUB !move Alloc Waste Trig WP GCs Mem-age\n");
392 for (i = 0; i < gens; i++) {
395 long unboxed_cnt = 0;
396 long large_boxed_cnt = 0;
397 long large_unboxed_cnt = 0;
400 for (j = 0; j < last_free_page; j++)
401 if (page_table[j].gen == i) {
403 /* Count the number of boxed pages within the given
405 if (page_table[j].allocated & BOXED_PAGE_FLAG) {
406 if (page_table[j].large_object)
411 if(page_table[j].dont_move) pinned_cnt++;
412 /* Count the number of unboxed pages within the given
414 if (page_table[j].allocated & UNBOXED_PAGE_FLAG) {
415 if (page_table[j].large_object)
422 gc_assert(generations[i].bytes_allocated
423 == count_generation_bytes_allocated(i));
425 " %1d: %5ld %5ld %5ld %5ld %5ld %5ld %5ld %5ld %8ld %5ld %8ld %4ld %3d %7.4f\n",
427 generations[i].alloc_start_page,
428 generations[i].alloc_unboxed_start_page,
429 generations[i].alloc_large_start_page,
430 generations[i].alloc_large_unboxed_start_page,
431 boxed_cnt, unboxed_cnt, large_boxed_cnt, large_unboxed_cnt,
433 generations[i].bytes_allocated,
434 (count_generation_pages(i)*PAGE_BYTES
435 - generations[i].bytes_allocated),
436 generations[i].gc_trigger,
437 count_write_protect_generation_pages(i),
438 generations[i].num_gc,
441 fprintf(stderr," Total bytes allocated=%ld\n", bytes_allocated);
443 fpu_restore(fpu_state);
447 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
448 void fast_bzero(void*, size_t); /* in <arch>-assem.S */
451 /* Zero the pages from START to END (inclusive), but use mmap/munmap instead
452 * if zeroing it ourselves, i.e. in practice give the memory back to the
453 * OS. Generally done after a large GC.
455 void zero_pages_with_mmap(page_index_t start, page_index_t end) {
457 void *addr = (void *) page_address(start), *new_addr;
458 size_t length = PAGE_BYTES*(1+end-start);
463 os_invalidate(addr, length);
464 new_addr = os_validate(addr, length);
465 if (new_addr == NULL || new_addr != addr) {
466 lose("remap_free_pages: page moved, 0x%08x ==> 0x%08x", start, new_addr);
469 for (i = start; i <= end; i++) {
470 page_table[i].need_to_zero = 0;
474 /* Zero the pages from START to END (inclusive). Generally done just after
475 * a new region has been allocated.
478 zero_pages(page_index_t start, page_index_t end) {
482 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
483 fast_bzero(page_address(start), PAGE_BYTES*(1+end-start));
485 bzero(page_address(start), PAGE_BYTES*(1+end-start));
490 /* Zero the pages from START to END (inclusive), except for those
491 * pages that are known to already zeroed. Mark all pages in the
492 * ranges as non-zeroed.
495 zero_dirty_pages(page_index_t start, page_index_t end) {
498 for (i = start; i <= end; i++) {
499 if (page_table[i].need_to_zero == 1) {
500 zero_pages(start, end);
505 for (i = start; i <= end; i++) {
506 page_table[i].need_to_zero = 1;
512 * To support quick and inline allocation, regions of memory can be
513 * allocated and then allocated from with just a free pointer and a
514 * check against an end address.
516 * Since objects can be allocated to spaces with different properties
517 * e.g. boxed/unboxed, generation, ages; there may need to be many
518 * allocation regions.
520 * Each allocation region may start within a partly used page. Many
521 * features of memory use are noted on a page wise basis, e.g. the
522 * generation; so if a region starts within an existing allocated page
523 * it must be consistent with this page.
525 * During the scavenging of the newspace, objects will be transported
526 * into an allocation region, and pointers updated to point to this
527 * allocation region. It is possible that these pointers will be
528 * scavenged again before the allocation region is closed, e.g. due to
529 * trans_list which jumps all over the place to cleanup the list. It
530 * is important to be able to determine properties of all objects
531 * pointed to when scavenging, e.g to detect pointers to the oldspace.
532 * Thus it's important that the allocation regions have the correct
533 * properties set when allocated, and not just set when closed. The
534 * region allocation routines return regions with the specified
535 * properties, and grab all the pages, setting their properties
536 * appropriately, except that the amount used is not known.
538 * These regions are used to support quicker allocation using just a
539 * free pointer. The actual space used by the region is not reflected
540 * in the pages tables until it is closed. It can't be scavenged until
543 * When finished with the region it should be closed, which will
544 * update the page tables for the actual space used returning unused
545 * space. Further it may be noted in the new regions which is
546 * necessary when scavenging the newspace.
548 * Large objects may be allocated directly without an allocation
549 * region, the page tables are updated immediately.
551 * Unboxed objects don't contain pointers to other objects and so
552 * don't need scavenging. Further they can't contain pointers to
553 * younger generations so WP is not needed. By allocating pages to
554 * unboxed objects the whole page never needs scavenging or
555 * write-protecting. */
557 /* We are only using two regions at present. Both are for the current
558 * newspace generation. */
559 struct alloc_region boxed_region;
560 struct alloc_region unboxed_region;
562 /* The generation currently being allocated to. */
563 static generation_index_t gc_alloc_generation;
565 /* Find a new region with room for at least the given number of bytes.
567 * It starts looking at the current generation's alloc_start_page. So
568 * may pick up from the previous region if there is enough space. This
569 * keeps the allocation contiguous when scavenging the newspace.
571 * The alloc_region should have been closed by a call to
572 * gc_alloc_update_page_tables(), and will thus be in an empty state.
574 * To assist the scavenging functions write-protected pages are not
575 * used. Free pages should not be write-protected.
577 * It is critical to the conservative GC that the start of regions be
578 * known. To help achieve this only small regions are allocated at a
581 * During scavenging, pointers may be found to within the current
582 * region and the page generation must be set so that pointers to the
583 * from space can be recognized. Therefore the generation of pages in
584 * the region are set to gc_alloc_generation. To prevent another
585 * allocation call using the same pages, all the pages in the region
586 * are allocated, although they will initially be empty.
589 gc_alloc_new_region(long nbytes, int unboxed, struct alloc_region *alloc_region)
591 page_index_t first_page;
592 page_index_t last_page;
599 "/alloc_new_region for %d bytes from gen %d\n",
600 nbytes, gc_alloc_generation));
603 /* Check that the region is in a reset state. */
604 gc_assert((alloc_region->first_page == 0)
605 && (alloc_region->last_page == -1)
606 && (alloc_region->free_pointer == alloc_region->end_addr));
607 ret = thread_mutex_lock(&free_pages_lock);
611 generations[gc_alloc_generation].alloc_unboxed_start_page;
614 generations[gc_alloc_generation].alloc_start_page;
616 last_page=gc_find_freeish_pages(&first_page,nbytes,unboxed);
617 bytes_found=(PAGE_BYTES - page_table[first_page].bytes_used)
618 + PAGE_BYTES*(last_page-first_page);
620 /* Set up the alloc_region. */
621 alloc_region->first_page = first_page;
622 alloc_region->last_page = last_page;
623 alloc_region->start_addr = page_table[first_page].bytes_used
624 + page_address(first_page);
625 alloc_region->free_pointer = alloc_region->start_addr;
626 alloc_region->end_addr = alloc_region->start_addr + bytes_found;
628 /* Set up the pages. */
630 /* The first page may have already been in use. */
631 if (page_table[first_page].bytes_used == 0) {
633 page_table[first_page].allocated = UNBOXED_PAGE_FLAG;
635 page_table[first_page].allocated = BOXED_PAGE_FLAG;
636 page_table[first_page].gen = gc_alloc_generation;
637 page_table[first_page].large_object = 0;
638 page_table[first_page].first_object_offset = 0;
642 gc_assert(page_table[first_page].allocated == UNBOXED_PAGE_FLAG);
644 gc_assert(page_table[first_page].allocated == BOXED_PAGE_FLAG);
645 page_table[first_page].allocated |= OPEN_REGION_PAGE_FLAG;
647 gc_assert(page_table[first_page].gen == gc_alloc_generation);
648 gc_assert(page_table[first_page].large_object == 0);
650 for (i = first_page+1; i <= last_page; i++) {
652 page_table[i].allocated = UNBOXED_PAGE_FLAG;
654 page_table[i].allocated = BOXED_PAGE_FLAG;
655 page_table[i].gen = gc_alloc_generation;
656 page_table[i].large_object = 0;
657 /* This may not be necessary for unboxed regions (think it was
659 page_table[i].first_object_offset =
660 alloc_region->start_addr - page_address(i);
661 page_table[i].allocated |= OPEN_REGION_PAGE_FLAG ;
663 /* Bump up last_free_page. */
664 if (last_page+1 > last_free_page) {
665 last_free_page = last_page+1;
666 /* do we only want to call this on special occasions? like for boxed_region? */
667 set_alloc_pointer((lispobj)(((char *)heap_base) + last_free_page*PAGE_BYTES));
669 ret = thread_mutex_unlock(&free_pages_lock);
672 /* we can do this after releasing free_pages_lock */
673 if (gencgc_zero_check) {
675 for (p = (long *)alloc_region->start_addr;
676 p < (long *)alloc_region->end_addr; p++) {
678 /* KLUDGE: It would be nice to use %lx and explicit casts
679 * (long) in code like this, so that it is less likely to
680 * break randomly when running on a machine with different
681 * word sizes. -- WHN 19991129 */
682 lose("The new region at %x is not zero.\n", p);
687 #ifdef READ_PROTECT_FREE_PAGES
688 os_protect(page_address(first_page),
689 PAGE_BYTES*(1+last_page-first_page),
693 /* If the first page was only partial, don't check whether it's
694 * zeroed (it won't be) and don't zero it (since the parts that
695 * we're interested in are guaranteed to be zeroed).
697 if (page_table[first_page].bytes_used) {
701 zero_dirty_pages(first_page, last_page);
704 /* If the record_new_objects flag is 2 then all new regions created
707 * If it's 1 then then it is only recorded if the first page of the
708 * current region is <= new_areas_ignore_page. This helps avoid
709 * unnecessary recording when doing full scavenge pass.
711 * The new_object structure holds the page, byte offset, and size of
712 * new regions of objects. Each new area is placed in the array of
713 * these structures pointer to by new_areas. new_areas_index holds the
714 * offset into new_areas.
716 * If new_area overflows NUM_NEW_AREAS then it stops adding them. The
717 * later code must detect this and handle it, probably by doing a full
718 * scavenge of a generation. */
719 #define NUM_NEW_AREAS 512
720 static int record_new_objects = 0;
721 static page_index_t new_areas_ignore_page;
727 static struct new_area (*new_areas)[];
728 static long new_areas_index;
731 /* Add a new area to new_areas. */
733 add_new_area(page_index_t first_page, long offset, long size)
735 unsigned long new_area_start,c;
738 /* Ignore if full. */
739 if (new_areas_index >= NUM_NEW_AREAS)
742 switch (record_new_objects) {
746 if (first_page > new_areas_ignore_page)
755 new_area_start = PAGE_BYTES*first_page + offset;
757 /* Search backwards for a prior area that this follows from. If
758 found this will save adding a new area. */
759 for (i = new_areas_index-1, c = 0; (i >= 0) && (c < 8); i--, c++) {
760 unsigned long area_end =
761 PAGE_BYTES*((*new_areas)[i].page)
762 + (*new_areas)[i].offset
763 + (*new_areas)[i].size;
765 "/add_new_area S1 %d %d %d %d\n",
766 i, c, new_area_start, area_end));*/
767 if (new_area_start == area_end) {
769 "/adding to [%d] %d %d %d with %d %d %d:\n",
771 (*new_areas)[i].page,
772 (*new_areas)[i].offset,
773 (*new_areas)[i].size,
777 (*new_areas)[i].size += size;
782 (*new_areas)[new_areas_index].page = first_page;
783 (*new_areas)[new_areas_index].offset = offset;
784 (*new_areas)[new_areas_index].size = size;
786 "/new_area %d page %d offset %d size %d\n",
787 new_areas_index, first_page, offset, size));*/
790 /* Note the max new_areas used. */
791 if (new_areas_index > max_new_areas)
792 max_new_areas = new_areas_index;
795 /* Update the tables for the alloc_region. The region may be added to
798 * When done the alloc_region is set up so that the next quick alloc
799 * will fail safely and thus a new region will be allocated. Further
800 * it is safe to try to re-update the page table of this reset
803 gc_alloc_update_page_tables(int unboxed, struct alloc_region *alloc_region)
806 page_index_t first_page;
807 page_index_t next_page;
809 long orig_first_page_bytes_used;
815 first_page = alloc_region->first_page;
817 /* Catch an unused alloc_region. */
818 if ((first_page == 0) && (alloc_region->last_page == -1))
821 next_page = first_page+1;
823 ret = thread_mutex_lock(&free_pages_lock);
825 if (alloc_region->free_pointer != alloc_region->start_addr) {
826 /* some bytes were allocated in the region */
827 orig_first_page_bytes_used = page_table[first_page].bytes_used;
829 gc_assert(alloc_region->start_addr == (page_address(first_page) + page_table[first_page].bytes_used));
831 /* All the pages used need to be updated */
833 /* Update the first page. */
835 /* If the page was free then set up the gen, and
836 * first_object_offset. */
837 if (page_table[first_page].bytes_used == 0)
838 gc_assert(page_table[first_page].first_object_offset == 0);
839 page_table[first_page].allocated &= ~(OPEN_REGION_PAGE_FLAG);
842 gc_assert(page_table[first_page].allocated == UNBOXED_PAGE_FLAG);
844 gc_assert(page_table[first_page].allocated == BOXED_PAGE_FLAG);
845 gc_assert(page_table[first_page].gen == gc_alloc_generation);
846 gc_assert(page_table[first_page].large_object == 0);
850 /* Calculate the number of bytes used in this page. This is not
851 * always the number of new bytes, unless it was free. */
853 if ((bytes_used = (alloc_region->free_pointer - page_address(first_page)))>PAGE_BYTES) {
854 bytes_used = PAGE_BYTES;
857 page_table[first_page].bytes_used = bytes_used;
858 byte_cnt += bytes_used;
861 /* All the rest of the pages should be free. We need to set their
862 * first_object_offset pointer to the start of the region, and set
865 page_table[next_page].allocated &= ~(OPEN_REGION_PAGE_FLAG);
867 gc_assert(page_table[next_page].allocated==UNBOXED_PAGE_FLAG);
869 gc_assert(page_table[next_page].allocated == BOXED_PAGE_FLAG);
870 gc_assert(page_table[next_page].bytes_used == 0);
871 gc_assert(page_table[next_page].gen == gc_alloc_generation);
872 gc_assert(page_table[next_page].large_object == 0);
874 gc_assert(page_table[next_page].first_object_offset ==
875 alloc_region->start_addr - page_address(next_page));
877 /* Calculate the number of bytes used in this page. */
879 if ((bytes_used = (alloc_region->free_pointer
880 - page_address(next_page)))>PAGE_BYTES) {
881 bytes_used = PAGE_BYTES;
884 page_table[next_page].bytes_used = bytes_used;
885 byte_cnt += bytes_used;
890 region_size = alloc_region->free_pointer - alloc_region->start_addr;
891 bytes_allocated += region_size;
892 generations[gc_alloc_generation].bytes_allocated += region_size;
894 gc_assert((byte_cnt- orig_first_page_bytes_used) == region_size);
896 /* Set the generations alloc restart page to the last page of
899 generations[gc_alloc_generation].alloc_unboxed_start_page =
902 generations[gc_alloc_generation].alloc_start_page = next_page-1;
904 /* Add the region to the new_areas if requested. */
906 add_new_area(first_page,orig_first_page_bytes_used, region_size);
910 "/gc_alloc_update_page_tables update %d bytes to gen %d\n",
912 gc_alloc_generation));
915 /* There are no bytes allocated. Unallocate the first_page if
916 * there are 0 bytes_used. */
917 page_table[first_page].allocated &= ~(OPEN_REGION_PAGE_FLAG);
918 if (page_table[first_page].bytes_used == 0)
919 page_table[first_page].allocated = FREE_PAGE_FLAG;
922 /* Unallocate any unused pages. */
923 while (next_page <= alloc_region->last_page) {
924 gc_assert(page_table[next_page].bytes_used == 0);
925 page_table[next_page].allocated = FREE_PAGE_FLAG;
928 ret = thread_mutex_unlock(&free_pages_lock);
931 /* alloc_region is per-thread, we're ok to do this unlocked */
932 gc_set_region_empty(alloc_region);
935 static inline void *gc_quick_alloc(long nbytes);
937 /* Allocate a possibly large object. */
939 gc_alloc_large(long nbytes, int unboxed, struct alloc_region *alloc_region)
941 page_index_t first_page;
942 page_index_t last_page;
943 int orig_first_page_bytes_used;
947 page_index_t next_page;
950 ret = thread_mutex_lock(&free_pages_lock);
955 generations[gc_alloc_generation].alloc_large_unboxed_start_page;
957 first_page = generations[gc_alloc_generation].alloc_large_start_page;
959 if (first_page <= alloc_region->last_page) {
960 first_page = alloc_region->last_page+1;
963 last_page=gc_find_freeish_pages(&first_page,nbytes,unboxed);
965 gc_assert(first_page > alloc_region->last_page);
967 generations[gc_alloc_generation].alloc_large_unboxed_start_page =
970 generations[gc_alloc_generation].alloc_large_start_page = last_page;
972 /* Set up the pages. */
973 orig_first_page_bytes_used = page_table[first_page].bytes_used;
975 /* If the first page was free then set up the gen, and
976 * first_object_offset. */
977 if (page_table[first_page].bytes_used == 0) {
979 page_table[first_page].allocated = UNBOXED_PAGE_FLAG;
981 page_table[first_page].allocated = BOXED_PAGE_FLAG;
982 page_table[first_page].gen = gc_alloc_generation;
983 page_table[first_page].first_object_offset = 0;
984 page_table[first_page].large_object = 1;
988 gc_assert(page_table[first_page].allocated == UNBOXED_PAGE_FLAG);
990 gc_assert(page_table[first_page].allocated == BOXED_PAGE_FLAG);
991 gc_assert(page_table[first_page].gen == gc_alloc_generation);
992 gc_assert(page_table[first_page].large_object == 1);
996 /* Calc. the number of bytes used in this page. This is not
997 * always the number of new bytes, unless it was free. */
999 if ((bytes_used = nbytes+orig_first_page_bytes_used) > PAGE_BYTES) {
1000 bytes_used = PAGE_BYTES;
1003 page_table[first_page].bytes_used = bytes_used;
1004 byte_cnt += bytes_used;
1006 next_page = first_page+1;
1008 /* All the rest of the pages should be free. We need to set their
1009 * first_object_offset pointer to the start of the region, and
1010 * set the bytes_used. */
1012 gc_assert(page_table[next_page].allocated == FREE_PAGE_FLAG);
1013 gc_assert(page_table[next_page].bytes_used == 0);
1015 page_table[next_page].allocated = UNBOXED_PAGE_FLAG;
1017 page_table[next_page].allocated = BOXED_PAGE_FLAG;
1018 page_table[next_page].gen = gc_alloc_generation;
1019 page_table[next_page].large_object = 1;
1021 page_table[next_page].first_object_offset =
1022 orig_first_page_bytes_used - PAGE_BYTES*(next_page-first_page);
1024 /* Calculate the number of bytes used in this page. */
1026 if ((bytes_used=(nbytes+orig_first_page_bytes_used)-byte_cnt) > PAGE_BYTES) {
1027 bytes_used = PAGE_BYTES;
1030 page_table[next_page].bytes_used = bytes_used;
1031 page_table[next_page].write_protected=0;
1032 page_table[next_page].dont_move=0;
1033 byte_cnt += bytes_used;
1037 gc_assert((byte_cnt-orig_first_page_bytes_used) == nbytes);
1039 bytes_allocated += nbytes;
1040 generations[gc_alloc_generation].bytes_allocated += nbytes;
1042 /* Add the region to the new_areas if requested. */
1044 add_new_area(first_page,orig_first_page_bytes_used,nbytes);
1046 /* Bump up last_free_page */
1047 if (last_page+1 > last_free_page) {
1048 last_free_page = last_page+1;
1049 set_alloc_pointer((lispobj)(((char *)heap_base) + last_free_page*PAGE_BYTES));
1051 ret = thread_mutex_unlock(&free_pages_lock);
1052 gc_assert(ret == 0);
1054 #ifdef READ_PROTECT_FREE_PAGES
1055 os_protect(page_address(first_page),
1056 PAGE_BYTES*(1+last_page-first_page),
1060 zero_dirty_pages(first_page, last_page);
1062 return page_address(first_page);
1065 static page_index_t gencgc_alloc_start_page = -1;
1068 gc_find_freeish_pages(page_index_t *restart_page_ptr, long nbytes, int unboxed)
1070 page_index_t first_page;
1071 page_index_t last_page;
1073 page_index_t restart_page=*restart_page_ptr;
1076 int large_p=(nbytes>=large_object_size);
1077 /* FIXME: assert(free_pages_lock is held); */
1079 /* Search for a contiguous free space of at least nbytes. If it's
1080 * a large object then align it on a page boundary by searching
1081 * for a free page. */
1083 if (gencgc_alloc_start_page != -1) {
1084 restart_page = gencgc_alloc_start_page;
1088 first_page = restart_page;
1090 while ((first_page < NUM_PAGES)
1091 && (page_table[first_page].allocated != FREE_PAGE_FLAG))
1094 while (first_page < NUM_PAGES) {
1095 if(page_table[first_page].allocated == FREE_PAGE_FLAG)
1097 if((page_table[first_page].allocated ==
1098 (unboxed ? UNBOXED_PAGE_FLAG : BOXED_PAGE_FLAG)) &&
1099 (page_table[first_page].large_object == 0) &&
1100 (page_table[first_page].gen == gc_alloc_generation) &&
1101 (page_table[first_page].bytes_used < (PAGE_BYTES-32)) &&
1102 (page_table[first_page].write_protected == 0) &&
1103 (page_table[first_page].dont_move == 0)) {
1109 if (first_page >= NUM_PAGES) {
1111 "Argh! gc_find_free_space failed (first_page), nbytes=%ld.\n",
1113 print_generation_stats(1);
1117 gc_assert(page_table[first_page].write_protected == 0);
1119 last_page = first_page;
1120 bytes_found = PAGE_BYTES - page_table[first_page].bytes_used;
1122 while (((bytes_found < nbytes)
1123 || (!large_p && (num_pages < 2)))
1124 && (last_page < (NUM_PAGES-1))
1125 && (page_table[last_page+1].allocated == FREE_PAGE_FLAG)) {
1128 bytes_found += PAGE_BYTES;
1129 gc_assert(page_table[last_page].write_protected == 0);
1132 region_size = (PAGE_BYTES - page_table[first_page].bytes_used)
1133 + PAGE_BYTES*(last_page-first_page);
1135 gc_assert(bytes_found == region_size);
1136 restart_page = last_page + 1;
1137 } while ((restart_page < NUM_PAGES) && (bytes_found < nbytes));
1139 /* Check for a failure */
1140 if ((restart_page >= NUM_PAGES) && (bytes_found < nbytes)) {
1142 "Argh! gc_find_freeish_pages failed (restart_page), nbytes=%ld.\n",
1144 print_generation_stats(1);
1147 *restart_page_ptr=first_page;
1152 /* Allocate bytes. All the rest of the special-purpose allocation
1153 * functions will eventually call this */
1156 gc_alloc_with_region(long nbytes,int unboxed_p, struct alloc_region *my_region,
1159 void *new_free_pointer;
1161 if(nbytes>=large_object_size)
1162 return gc_alloc_large(nbytes,unboxed_p,my_region);
1164 /* Check whether there is room in the current alloc region. */
1165 new_free_pointer = my_region->free_pointer + nbytes;
1167 /* fprintf(stderr, "alloc %d bytes from %p to %p\n", nbytes,
1168 my_region->free_pointer, new_free_pointer); */
1170 if (new_free_pointer <= my_region->end_addr) {
1171 /* If so then allocate from the current alloc region. */
1172 void *new_obj = my_region->free_pointer;
1173 my_region->free_pointer = new_free_pointer;
1175 /* Unless a `quick' alloc was requested, check whether the
1176 alloc region is almost empty. */
1178 (my_region->end_addr - my_region->free_pointer) <= 32) {
1179 /* If so, finished with the current region. */
1180 gc_alloc_update_page_tables(unboxed_p, my_region);
1181 /* Set up a new region. */
1182 gc_alloc_new_region(32 /*bytes*/, unboxed_p, my_region);
1185 return((void *)new_obj);
1188 /* Else not enough free space in the current region: retry with a
1191 gc_alloc_update_page_tables(unboxed_p, my_region);
1192 gc_alloc_new_region(nbytes, unboxed_p, my_region);
1193 return gc_alloc_with_region(nbytes,unboxed_p,my_region,0);
1196 /* these are only used during GC: all allocation from the mutator calls
1197 * alloc() -> gc_alloc_with_region() with the appropriate per-thread
1201 gc_general_alloc(long nbytes,int unboxed_p,int quick_p)
1203 struct alloc_region *my_region =
1204 unboxed_p ? &unboxed_region : &boxed_region;
1205 return gc_alloc_with_region(nbytes,unboxed_p, my_region,quick_p);
1208 static inline void *
1209 gc_quick_alloc(long nbytes)
1211 return gc_general_alloc(nbytes,ALLOC_BOXED,ALLOC_QUICK);
1214 static inline void *
1215 gc_quick_alloc_large(long nbytes)
1217 return gc_general_alloc(nbytes,ALLOC_BOXED,ALLOC_QUICK);
1220 static inline void *
1221 gc_alloc_unboxed(long nbytes)
1223 return gc_general_alloc(nbytes,ALLOC_UNBOXED,0);
1226 static inline void *
1227 gc_quick_alloc_unboxed(long nbytes)
1229 return gc_general_alloc(nbytes,ALLOC_UNBOXED,ALLOC_QUICK);
1232 static inline void *
1233 gc_quick_alloc_large_unboxed(long nbytes)
1235 return gc_general_alloc(nbytes,ALLOC_UNBOXED,ALLOC_QUICK);
1239 * scavenging/transporting routines derived from gc.c in CMU CL ca. 18b
1242 extern long (*scavtab[256])(lispobj *where, lispobj object);
1243 extern lispobj (*transother[256])(lispobj object);
1244 extern long (*sizetab[256])(lispobj *where);
1246 /* Copy a large boxed object. If the object is in a large object
1247 * region then it is simply promoted, else it is copied. If it's large
1248 * enough then it's copied to a large object region.
1250 * Vectors may have shrunk. If the object is not copied the space
1251 * needs to be reclaimed, and the page_tables corrected. */
1253 copy_large_object(lispobj object, long nwords)
1257 page_index_t first_page;
1259 gc_assert(is_lisp_pointer(object));
1260 gc_assert(from_space_p(object));
1261 gc_assert((nwords & 0x01) == 0);
1264 /* Check whether it's in a large object region. */
1265 first_page = find_page_index((void *)object);
1266 gc_assert(first_page >= 0);
1268 if (page_table[first_page].large_object) {
1270 /* Promote the object. */
1272 long remaining_bytes;
1273 page_index_t next_page;
1275 long old_bytes_used;
1277 /* Note: Any page write-protection must be removed, else a
1278 * later scavenge_newspace may incorrectly not scavenge these
1279 * pages. This would not be necessary if they are added to the
1280 * new areas, but let's do it for them all (they'll probably
1281 * be written anyway?). */
1283 gc_assert(page_table[first_page].first_object_offset == 0);
1285 next_page = first_page;
1286 remaining_bytes = nwords*N_WORD_BYTES;
1287 while (remaining_bytes > PAGE_BYTES) {
1288 gc_assert(page_table[next_page].gen == from_space);
1289 gc_assert(page_table[next_page].allocated == BOXED_PAGE_FLAG);
1290 gc_assert(page_table[next_page].large_object);
1291 gc_assert(page_table[next_page].first_object_offset==
1292 -PAGE_BYTES*(next_page-first_page));
1293 gc_assert(page_table[next_page].bytes_used == PAGE_BYTES);
1295 page_table[next_page].gen = new_space;
1297 /* Remove any write-protection. We should be able to rely
1298 * on the write-protect flag to avoid redundant calls. */
1299 if (page_table[next_page].write_protected) {
1300 os_protect(page_address(next_page), PAGE_BYTES, OS_VM_PROT_ALL);
1301 page_table[next_page].write_protected = 0;
1303 remaining_bytes -= PAGE_BYTES;
1307 /* Now only one page remains, but the object may have shrunk
1308 * so there may be more unused pages which will be freed. */
1310 /* The object may have shrunk but shouldn't have grown. */
1311 gc_assert(page_table[next_page].bytes_used >= remaining_bytes);
1313 page_table[next_page].gen = new_space;
1314 gc_assert(page_table[next_page].allocated == BOXED_PAGE_FLAG);
1316 /* Adjust the bytes_used. */
1317 old_bytes_used = page_table[next_page].bytes_used;
1318 page_table[next_page].bytes_used = remaining_bytes;
1320 bytes_freed = old_bytes_used - remaining_bytes;
1322 /* Free any remaining pages; needs care. */
1324 while ((old_bytes_used == PAGE_BYTES) &&
1325 (page_table[next_page].gen == from_space) &&
1326 (page_table[next_page].allocated == BOXED_PAGE_FLAG) &&
1327 page_table[next_page].large_object &&
1328 (page_table[next_page].first_object_offset ==
1329 -(next_page - first_page)*PAGE_BYTES)) {
1330 /* Checks out OK, free the page. Don't need to bother zeroing
1331 * pages as this should have been done before shrinking the
1332 * object. These pages shouldn't be write-protected as they
1333 * should be zero filled. */
1334 gc_assert(page_table[next_page].write_protected == 0);
1336 old_bytes_used = page_table[next_page].bytes_used;
1337 page_table[next_page].allocated = FREE_PAGE_FLAG;
1338 page_table[next_page].bytes_used = 0;
1339 bytes_freed += old_bytes_used;
1343 generations[from_space].bytes_allocated -= N_WORD_BYTES*nwords +
1345 generations[new_space].bytes_allocated += N_WORD_BYTES*nwords;
1346 bytes_allocated -= bytes_freed;
1348 /* Add the region to the new_areas if requested. */
1349 add_new_area(first_page,0,nwords*N_WORD_BYTES);
1353 /* Get tag of object. */
1354 tag = lowtag_of(object);
1356 /* Allocate space. */
1357 new = gc_quick_alloc_large(nwords*N_WORD_BYTES);
1359 memcpy(new,native_pointer(object),nwords*N_WORD_BYTES);
1361 /* Return Lisp pointer of new object. */
1362 return ((lispobj) new) | tag;
1366 /* to copy unboxed objects */
1368 copy_unboxed_object(lispobj object, long nwords)
1373 gc_assert(is_lisp_pointer(object));
1374 gc_assert(from_space_p(object));
1375 gc_assert((nwords & 0x01) == 0);
1377 /* Get tag of object. */
1378 tag = lowtag_of(object);
1380 /* Allocate space. */
1381 new = gc_quick_alloc_unboxed(nwords*N_WORD_BYTES);
1383 memcpy(new,native_pointer(object),nwords*N_WORD_BYTES);
1385 /* Return Lisp pointer of new object. */
1386 return ((lispobj) new) | tag;
1389 /* to copy large unboxed objects
1391 * If the object is in a large object region then it is simply
1392 * promoted, else it is copied. If it's large enough then it's copied
1393 * to a large object region.
1395 * Bignums and vectors may have shrunk. If the object is not copied
1396 * the space needs to be reclaimed, and the page_tables corrected.
1398 * KLUDGE: There's a lot of cut-and-paste duplication between this
1399 * function and copy_large_object(..). -- WHN 20000619 */
1401 copy_large_unboxed_object(lispobj object, long nwords)
1405 page_index_t first_page;
1407 gc_assert(is_lisp_pointer(object));
1408 gc_assert(from_space_p(object));
1409 gc_assert((nwords & 0x01) == 0);
1411 if ((nwords > 1024*1024) && gencgc_verbose)
1412 FSHOW((stderr, "/copy_large_unboxed_object: %d bytes\n", nwords*N_WORD_BYTES));
1414 /* Check whether it's a large object. */
1415 first_page = find_page_index((void *)object);
1416 gc_assert(first_page >= 0);
1418 if (page_table[first_page].large_object) {
1419 /* Promote the object. Note: Unboxed objects may have been
1420 * allocated to a BOXED region so it may be necessary to
1421 * change the region to UNBOXED. */
1422 long remaining_bytes;
1423 page_index_t next_page;
1425 long old_bytes_used;
1427 gc_assert(page_table[first_page].first_object_offset == 0);
1429 next_page = first_page;
1430 remaining_bytes = nwords*N_WORD_BYTES;
1431 while (remaining_bytes > PAGE_BYTES) {
1432 gc_assert(page_table[next_page].gen == from_space);
1433 gc_assert((page_table[next_page].allocated == UNBOXED_PAGE_FLAG)
1434 || (page_table[next_page].allocated == BOXED_PAGE_FLAG));
1435 gc_assert(page_table[next_page].large_object);
1436 gc_assert(page_table[next_page].first_object_offset==
1437 -PAGE_BYTES*(next_page-first_page));
1438 gc_assert(page_table[next_page].bytes_used == PAGE_BYTES);
1440 page_table[next_page].gen = new_space;
1441 page_table[next_page].allocated = UNBOXED_PAGE_FLAG;
1442 remaining_bytes -= PAGE_BYTES;
1446 /* Now only one page remains, but the object may have shrunk so
1447 * there may be more unused pages which will be freed. */
1449 /* Object may have shrunk but shouldn't have grown - check. */
1450 gc_assert(page_table[next_page].bytes_used >= remaining_bytes);
1452 page_table[next_page].gen = new_space;
1453 page_table[next_page].allocated = UNBOXED_PAGE_FLAG;
1455 /* Adjust the bytes_used. */
1456 old_bytes_used = page_table[next_page].bytes_used;
1457 page_table[next_page].bytes_used = remaining_bytes;
1459 bytes_freed = old_bytes_used - remaining_bytes;
1461 /* Free any remaining pages; needs care. */
1463 while ((old_bytes_used == PAGE_BYTES) &&
1464 (page_table[next_page].gen == from_space) &&
1465 ((page_table[next_page].allocated == UNBOXED_PAGE_FLAG)
1466 || (page_table[next_page].allocated == BOXED_PAGE_FLAG)) &&
1467 page_table[next_page].large_object &&
1468 (page_table[next_page].first_object_offset ==
1469 -(next_page - first_page)*PAGE_BYTES)) {
1470 /* Checks out OK, free the page. Don't need to both zeroing
1471 * pages as this should have been done before shrinking the
1472 * object. These pages shouldn't be write-protected, even if
1473 * boxed they should be zero filled. */
1474 gc_assert(page_table[next_page].write_protected == 0);
1476 old_bytes_used = page_table[next_page].bytes_used;
1477 page_table[next_page].allocated = FREE_PAGE_FLAG;
1478 page_table[next_page].bytes_used = 0;
1479 bytes_freed += old_bytes_used;
1483 if ((bytes_freed > 0) && gencgc_verbose)
1485 "/copy_large_unboxed bytes_freed=%d\n",
1488 generations[from_space].bytes_allocated -= nwords*N_WORD_BYTES + bytes_freed;
1489 generations[new_space].bytes_allocated += nwords*N_WORD_BYTES;
1490 bytes_allocated -= bytes_freed;
1495 /* Get tag of object. */
1496 tag = lowtag_of(object);
1498 /* Allocate space. */
1499 new = gc_quick_alloc_large_unboxed(nwords*N_WORD_BYTES);
1501 /* Copy the object. */
1502 memcpy(new,native_pointer(object),nwords*N_WORD_BYTES);
1504 /* Return Lisp pointer of new object. */
1505 return ((lispobj) new) | tag;
1514 * code and code-related objects
1517 static lispobj trans_fun_header(lispobj object);
1518 static lispobj trans_boxed(lispobj object);
1521 /* Scan a x86 compiled code object, looking for possible fixups that
1522 * have been missed after a move.
1524 * Two types of fixups are needed:
1525 * 1. Absolute fixups to within the code object.
1526 * 2. Relative fixups to outside the code object.
1528 * Currently only absolute fixups to the constant vector, or to the
1529 * code area are checked. */
1531 sniff_code_object(struct code *code, unsigned long displacement)
1533 #ifdef LISP_FEATURE_X86
1534 long nheader_words, ncode_words, nwords;
1536 void *constants_start_addr = NULL, *constants_end_addr;
1537 void *code_start_addr, *code_end_addr;
1538 int fixup_found = 0;
1540 if (!check_code_fixups)
1543 ncode_words = fixnum_value(code->code_size);
1544 nheader_words = HeaderValue(*(lispobj *)code);
1545 nwords = ncode_words + nheader_words;
1547 constants_start_addr = (void *)code + 5*N_WORD_BYTES;
1548 constants_end_addr = (void *)code + nheader_words*N_WORD_BYTES;
1549 code_start_addr = (void *)code + nheader_words*N_WORD_BYTES;
1550 code_end_addr = (void *)code + nwords*N_WORD_BYTES;
1552 /* Work through the unboxed code. */
1553 for (p = code_start_addr; p < code_end_addr; p++) {
1554 void *data = *(void **)p;
1555 unsigned d1 = *((unsigned char *)p - 1);
1556 unsigned d2 = *((unsigned char *)p - 2);
1557 unsigned d3 = *((unsigned char *)p - 3);
1558 unsigned d4 = *((unsigned char *)p - 4);
1560 unsigned d5 = *((unsigned char *)p - 5);
1561 unsigned d6 = *((unsigned char *)p - 6);
1564 /* Check for code references. */
1565 /* Check for a 32 bit word that looks like an absolute
1566 reference to within the code adea of the code object. */
1567 if ((data >= (code_start_addr-displacement))
1568 && (data < (code_end_addr-displacement))) {
1569 /* function header */
1571 && (((unsigned)p - 4 - 4*HeaderValue(*((unsigned *)p-1))) == (unsigned)code)) {
1572 /* Skip the function header */
1576 /* the case of PUSH imm32 */
1580 "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1581 p, d6, d5, d4, d3, d2, d1, data));
1582 FSHOW((stderr, "/PUSH $0x%.8x\n", data));
1584 /* the case of MOV [reg-8],imm32 */
1586 && (d2==0x40 || d2==0x41 || d2==0x42 || d2==0x43
1587 || d2==0x45 || d2==0x46 || d2==0x47)
1591 "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1592 p, d6, d5, d4, d3, d2, d1, data));
1593 FSHOW((stderr, "/MOV [reg-8],$0x%.8x\n", data));
1595 /* the case of LEA reg,[disp32] */
1596 if ((d2 == 0x8d) && ((d1 & 0xc7) == 5)) {
1599 "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1600 p, d6, d5, d4, d3, d2, d1, data));
1601 FSHOW((stderr,"/LEA reg,[$0x%.8x]\n", data));
1605 /* Check for constant references. */
1606 /* Check for a 32 bit word that looks like an absolute
1607 reference to within the constant vector. Constant references
1609 if ((data >= (constants_start_addr-displacement))
1610 && (data < (constants_end_addr-displacement))
1611 && (((unsigned)data & 0x3) == 0)) {
1616 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1617 p, d6, d5, d4, d3, d2, d1, data));
1618 FSHOW((stderr,"/MOV eax,0x%.8x\n", data));
1621 /* the case of MOV m32,EAX */
1625 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1626 p, d6, d5, d4, d3, d2, d1, data));
1627 FSHOW((stderr, "/MOV 0x%.8x,eax\n", data));
1630 /* the case of CMP m32,imm32 */
1631 if ((d1 == 0x3d) && (d2 == 0x81)) {
1634 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1635 p, d6, d5, d4, d3, d2, d1, data));
1637 FSHOW((stderr, "/CMP 0x%.8x,immed32\n", data));
1640 /* Check for a mod=00, r/m=101 byte. */
1641 if ((d1 & 0xc7) == 5) {
1646 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1647 p, d6, d5, d4, d3, d2, d1, data));
1648 FSHOW((stderr,"/CMP 0x%.8x,reg\n", data));
1650 /* the case of CMP reg32,m32 */
1654 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1655 p, d6, d5, d4, d3, d2, d1, data));
1656 FSHOW((stderr, "/CMP reg32,0x%.8x\n", data));
1658 /* the case of MOV m32,reg32 */
1662 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1663 p, d6, d5, d4, d3, d2, d1, data));
1664 FSHOW((stderr, "/MOV 0x%.8x,reg32\n", data));
1666 /* the case of MOV reg32,m32 */
1670 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1671 p, d6, d5, d4, d3, d2, d1, data));
1672 FSHOW((stderr, "/MOV reg32,0x%.8x\n", data));
1674 /* the case of LEA reg32,m32 */
1678 "abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1679 p, d6, d5, d4, d3, d2, d1, data));
1680 FSHOW((stderr, "/LEA reg32,0x%.8x\n", data));
1686 /* If anything was found, print some information on the code
1690 "/compiled code object at %x: header words = %d, code words = %d\n",
1691 code, nheader_words, ncode_words));
1693 "/const start = %x, end = %x\n",
1694 constants_start_addr, constants_end_addr));
1696 "/code start = %x, end = %x\n",
1697 code_start_addr, code_end_addr));
1703 gencgc_apply_code_fixups(struct code *old_code, struct code *new_code)
1705 /* x86-64 uses pc-relative addressing instead of this kludge */
1706 #ifndef LISP_FEATURE_X86_64
1707 long nheader_words, ncode_words, nwords;
1708 void *constants_start_addr, *constants_end_addr;
1709 void *code_start_addr, *code_end_addr;
1710 lispobj fixups = NIL;
1711 unsigned long displacement = (unsigned long)new_code - (unsigned long)old_code;
1712 struct vector *fixups_vector;
1714 ncode_words = fixnum_value(new_code->code_size);
1715 nheader_words = HeaderValue(*(lispobj *)new_code);
1716 nwords = ncode_words + nheader_words;
1718 "/compiled code object at %x: header words = %d, code words = %d\n",
1719 new_code, nheader_words, ncode_words)); */
1720 constants_start_addr = (void *)new_code + 5*N_WORD_BYTES;
1721 constants_end_addr = (void *)new_code + nheader_words*N_WORD_BYTES;
1722 code_start_addr = (void *)new_code + nheader_words*N_WORD_BYTES;
1723 code_end_addr = (void *)new_code + nwords*N_WORD_BYTES;
1726 "/const start = %x, end = %x\n",
1727 constants_start_addr,constants_end_addr));
1729 "/code start = %x; end = %x\n",
1730 code_start_addr,code_end_addr));
1733 /* The first constant should be a pointer to the fixups for this
1734 code objects. Check. */
1735 fixups = new_code->constants[0];
1737 /* It will be 0 or the unbound-marker if there are no fixups (as
1738 * will be the case if the code object has been purified, for
1739 * example) and will be an other pointer if it is valid. */
1740 if ((fixups == 0) || (fixups == UNBOUND_MARKER_WIDETAG) ||
1741 !is_lisp_pointer(fixups)) {
1742 /* Check for possible errors. */
1743 if (check_code_fixups)
1744 sniff_code_object(new_code, displacement);
1749 fixups_vector = (struct vector *)native_pointer(fixups);
1751 /* Could be pointing to a forwarding pointer. */
1752 /* FIXME is this always in from_space? if so, could replace this code with
1753 * forwarding_pointer_p/forwarding_pointer_value */
1754 if (is_lisp_pointer(fixups) &&
1755 (find_page_index((void*)fixups_vector) != -1) &&
1756 (fixups_vector->header == 0x01)) {
1757 /* If so, then follow it. */
1758 /*SHOW("following pointer to a forwarding pointer");*/
1759 fixups_vector = (struct vector *)native_pointer((lispobj)fixups_vector->length);
1762 /*SHOW("got fixups");*/
1764 if (widetag_of(fixups_vector->header) == SIMPLE_ARRAY_WORD_WIDETAG) {
1765 /* Got the fixups for the code block. Now work through the vector,
1766 and apply a fixup at each address. */
1767 long length = fixnum_value(fixups_vector->length);
1769 for (i = 0; i < length; i++) {
1770 unsigned long offset = fixups_vector->data[i];
1771 /* Now check the current value of offset. */
1772 unsigned long old_value =
1773 *(unsigned long *)((unsigned long)code_start_addr + offset);
1775 /* If it's within the old_code object then it must be an
1776 * absolute fixup (relative ones are not saved) */
1777 if ((old_value >= (unsigned long)old_code)
1778 && (old_value < ((unsigned long)old_code + nwords*N_WORD_BYTES)))
1779 /* So add the dispacement. */
1780 *(unsigned long *)((unsigned long)code_start_addr + offset) =
1781 old_value + displacement;
1783 /* It is outside the old code object so it must be a
1784 * relative fixup (absolute fixups are not saved). So
1785 * subtract the displacement. */
1786 *(unsigned long *)((unsigned long)code_start_addr + offset) =
1787 old_value - displacement;
1790 fprintf(stderr, "widetag of fixup vector is %d\n", widetag_of(fixups_vector->header));
1793 /* Check for possible errors. */
1794 if (check_code_fixups) {
1795 sniff_code_object(new_code,displacement);
1802 trans_boxed_large(lispobj object)
1805 unsigned long length;
1807 gc_assert(is_lisp_pointer(object));
1809 header = *((lispobj *) native_pointer(object));
1810 length = HeaderValue(header) + 1;
1811 length = CEILING(length, 2);
1813 return copy_large_object(object, length);
1816 /* Doesn't seem to be used, delete it after the grace period. */
1819 trans_unboxed_large(lispobj object)
1822 unsigned long length;
1824 gc_assert(is_lisp_pointer(object));
1826 header = *((lispobj *) native_pointer(object));
1827 length = HeaderValue(header) + 1;
1828 length = CEILING(length, 2);
1830 return copy_large_unboxed_object(object, length);
1836 * vector-like objects
1840 /* FIXME: What does this mean? */
1841 int gencgc_hash = 1;
1843 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
1846 scav_vector(lispobj *where, lispobj object)
1848 unsigned long kv_length;
1850 unsigned long length = 0; /* (0 = dummy to stop GCC warning) */
1851 struct hash_table *hash_table;
1852 lispobj empty_symbol;
1853 unsigned long *index_vector = NULL; /* (NULL = dummy to stop GCC warning) */
1854 unsigned long *next_vector = NULL; /* (NULL = dummy to stop GCC warning) */
1855 unsigned long *hash_vector = NULL; /* (NULL = dummy to stop GCC warning) */
1857 unsigned long next_vector_length = 0;
1859 /* FIXME: A comment explaining this would be nice. It looks as
1860 * though SB-VM:VECTOR-VALID-HASHING-SUBTYPE is set for EQ-based
1861 * hash tables in the Lisp HASH-TABLE code, and nowhere else. */
1862 if (HeaderValue(object) != subtype_VectorValidHashing)
1866 /* This is set for backward compatibility. FIXME: Do we need
1869 (subtype_VectorMustRehash<<N_WIDETAG_BITS) | SIMPLE_VECTOR_WIDETAG;
1873 kv_length = fixnum_value(where[1]);
1874 kv_vector = where + 2; /* Skip the header and length. */
1875 /*FSHOW((stderr,"/kv_length = %d\n", kv_length));*/
1877 /* Scavenge element 0, which may be a hash-table structure. */
1878 scavenge(where+2, 1);
1879 if (!is_lisp_pointer(where[2])) {
1880 lose("no pointer at %x in hash table\n", where[2]);
1882 hash_table = (struct hash_table *)native_pointer(where[2]);
1883 /*FSHOW((stderr,"/hash_table = %x\n", hash_table));*/
1884 if (widetag_of(hash_table->header) != INSTANCE_HEADER_WIDETAG) {
1885 lose("hash table not instance (%x at %x)\n",
1890 /* Scavenge element 1, which should be some internal symbol that
1891 * the hash table code reserves for marking empty slots. */
1892 scavenge(where+3, 1);
1893 if (!is_lisp_pointer(where[3])) {
1894 lose("not empty-hash-table-slot symbol pointer: %x\n", where[3]);
1896 empty_symbol = where[3];
1897 /* fprintf(stderr,"* empty_symbol = %x\n", empty_symbol);*/
1898 if (widetag_of(*(lispobj *)native_pointer(empty_symbol)) !=
1899 SYMBOL_HEADER_WIDETAG) {
1900 lose("not a symbol where empty-hash-table-slot symbol expected: %x\n",
1901 *(lispobj *)native_pointer(empty_symbol));
1904 /* Scavenge hash table, which will fix the positions of the other
1905 * needed objects. */
1906 scavenge((lispobj *)hash_table,
1907 sizeof(struct hash_table) / sizeof(lispobj));
1909 /* Cross-check the kv_vector. */
1910 if (where != (lispobj *)native_pointer(hash_table->table)) {
1911 lose("hash_table table!=this table %x\n", hash_table->table);
1915 weak_p_obj = hash_table->weak_p;
1919 lispobj index_vector_obj = hash_table->index_vector;
1921 if (is_lisp_pointer(index_vector_obj) &&
1922 (widetag_of(*(lispobj *)native_pointer(index_vector_obj)) ==
1923 SIMPLE_ARRAY_WORD_WIDETAG)) {
1925 ((unsigned long *)native_pointer(index_vector_obj)) + 2;
1926 /*FSHOW((stderr, "/index_vector = %x\n",index_vector));*/
1927 length = fixnum_value(((lispobj *)native_pointer(index_vector_obj))[1]);
1928 /*FSHOW((stderr, "/length = %d\n", length));*/
1930 lose("invalid index_vector %x\n", index_vector_obj);
1936 lispobj next_vector_obj = hash_table->next_vector;
1938 if (is_lisp_pointer(next_vector_obj) &&
1939 (widetag_of(*(lispobj *)native_pointer(next_vector_obj)) ==
1940 SIMPLE_ARRAY_WORD_WIDETAG)) {
1941 next_vector = ((unsigned long *)native_pointer(next_vector_obj)) + 2;
1942 /*FSHOW((stderr, "/next_vector = %x\n", next_vector));*/
1943 next_vector_length = fixnum_value(((lispobj *)native_pointer(next_vector_obj))[1]);
1944 /*FSHOW((stderr, "/next_vector_length = %d\n", next_vector_length));*/
1946 lose("invalid next_vector %x\n", next_vector_obj);
1950 /* maybe hash vector */
1952 lispobj hash_vector_obj = hash_table->hash_vector;
1954 if (is_lisp_pointer(hash_vector_obj) &&
1955 (widetag_of(*(lispobj *)native_pointer(hash_vector_obj)) ==
1956 SIMPLE_ARRAY_WORD_WIDETAG)){
1958 ((unsigned long *)native_pointer(hash_vector_obj)) + 2;
1959 /*FSHOW((stderr, "/hash_vector = %x\n", hash_vector));*/
1960 gc_assert(fixnum_value(((lispobj *)native_pointer(hash_vector_obj))[1])
1961 == next_vector_length);
1964 /*FSHOW((stderr, "/no hash_vector: %x\n", hash_vector_obj));*/
1968 /* These lengths could be different as the index_vector can be a
1969 * different length from the others, a larger index_vector could help
1970 * reduce collisions. */
1971 gc_assert(next_vector_length*2 == kv_length);
1973 /* now all set up.. */
1975 /* Work through the KV vector. */
1978 for (i = 1; i < next_vector_length; i++) {
1979 lispobj old_key = kv_vector[2*i];
1981 #if N_WORD_BITS == 32
1982 unsigned long old_index = (old_key & 0x1fffffff)%length;
1983 #elif N_WORD_BITS == 64
1984 unsigned long old_index = (old_key & 0x1fffffffffffffff)%length;
1987 /* Scavenge the key and value. */
1988 scavenge(&kv_vector[2*i],2);
1990 /* Check whether the key has moved and is EQ based. */
1992 lispobj new_key = kv_vector[2*i];
1993 #if N_WORD_BITS == 32
1994 unsigned long new_index = (new_key & 0x1fffffff)%length;
1995 #elif N_WORD_BITS == 64
1996 unsigned long new_index = (new_key & 0x1fffffffffffffff)%length;
1999 if ((old_index != new_index) &&
2001 (hash_vector[i] == MAGIC_HASH_VECTOR_VALUE)) &&
2002 ((new_key != empty_symbol) ||
2003 (kv_vector[2*i] != empty_symbol))) {
2006 "* EQ key %d moved from %x to %x; index %d to %d\n",
2007 i, old_key, new_key, old_index, new_index));*/
2009 if (index_vector[old_index] != 0) {
2010 /*FSHOW((stderr, "/P1 %d\n", index_vector[old_index]));*/
2012 /* Unlink the key from the old_index chain. */
2013 if (index_vector[old_index] == i) {
2014 /*FSHOW((stderr, "/P2a %d\n", next_vector[i]));*/
2015 index_vector[old_index] = next_vector[i];
2016 /* Link it into the needing rehash chain. */
2017 next_vector[i] = fixnum_value(hash_table->needing_rehash);
2018 hash_table->needing_rehash = make_fixnum(i);
2021 unsigned long prior = index_vector[old_index];
2022 unsigned long next = next_vector[prior];
2024 /*FSHOW((stderr, "/P3a %d %d\n", prior, next));*/
2027 /*FSHOW((stderr, "/P3b %d %d\n", prior, next));*/
2030 next_vector[prior] = next_vector[next];
2031 /* Link it into the needing rehash
2034 fixnum_value(hash_table->needing_rehash);
2035 hash_table->needing_rehash = make_fixnum(next);
2040 next = next_vector[next];
2048 return (CEILING(kv_length + 2, 2));
2054 scav_vector(lispobj *where, lispobj object)
2056 if (HeaderValue(object) == subtype_VectorValidHashing) {
2058 (subtype_VectorMustRehash<<N_WIDETAG_BITS) | SIMPLE_VECTOR_WIDETAG;
2067 * Lutexes. Using the normal finalization machinery for finalizing
2068 * lutexes is tricky, since the finalization depends on working lutexes.
2069 * So we track the lutexes in the GC and finalize them manually.
2072 #if defined(LUTEX_WIDETAG)
2075 * Start tracking LUTEX in the GC, by adding it to the linked list of
2076 * lutexes in the nursery generation. The caller is responsible for
2077 * locking, and GCs must be inhibited until the registration is
2081 gencgc_register_lutex (struct lutex *lutex) {
2082 int index = find_page_index(lutex);
2083 generation_index_t gen;
2086 /* This lutex is in static space, so we don't need to worry about
2092 gen = page_table[index].gen;
2094 gc_assert(gen >= 0);
2095 gc_assert(gen < NUM_GENERATIONS);
2097 head = generations[gen].lutexes;
2104 generations[gen].lutexes = lutex;
2108 * Stop tracking LUTEX in the GC by removing it from the appropriate
2109 * linked lists. This will only be called during GC, so no locking is
2113 gencgc_unregister_lutex (struct lutex *lutex) {
2115 lutex->prev->next = lutex->next;
2117 generations[lutex->gen].lutexes = lutex->next;
2121 lutex->next->prev = lutex->prev;
2130 * Mark all lutexes in generation GEN as not live.
2133 unmark_lutexes (generation_index_t gen) {
2134 struct lutex *lutex = generations[gen].lutexes;
2138 lutex = lutex->next;
2143 * Finalize all lutexes in generation GEN that have not been marked live.
2146 reap_lutexes (generation_index_t gen) {
2147 struct lutex *lutex = generations[gen].lutexes;
2150 struct lutex *next = lutex->next;
2152 lutex_destroy(lutex);
2153 gencgc_unregister_lutex(lutex);
2160 * Mark LUTEX as live.
2163 mark_lutex (lispobj tagged_lutex) {
2164 struct lutex *lutex = (struct lutex*) native_pointer(tagged_lutex);
2170 * Move all lutexes in generation FROM to generation TO.
2173 move_lutexes (generation_index_t from, generation_index_t to) {
2174 struct lutex *tail = generations[from].lutexes;
2176 /* Nothing to move */
2180 /* Change the generation of the lutexes in FROM. */
2181 while (tail->next) {
2187 /* Link the last lutex in the FROM list to the start of the TO list */
2188 tail->next = generations[to].lutexes;
2190 /* And vice versa */
2191 if (generations[to].lutexes) {
2192 generations[to].lutexes->prev = tail;
2195 /* And update the generations structures to match this */
2196 generations[to].lutexes = generations[from].lutexes;
2197 generations[from].lutexes = NULL;
2201 scav_lutex(lispobj *where, lispobj object)
2203 mark_lutex((lispobj) where);
2205 return CEILING(sizeof(struct lutex)/sizeof(lispobj), 2);
2209 trans_lutex(lispobj object)
2211 struct lutex *lutex = native_pointer(object);
2213 size_t words = CEILING(sizeof(struct lutex)/sizeof(lispobj), 2);
2214 gc_assert(is_lisp_pointer(object));
2215 copied = copy_object(object, words);
2217 /* Update the links, since the lutex moved in memory. */
2219 lutex->next->prev = native_pointer(copied);
2223 lutex->prev->next = native_pointer(copied);
2225 generations[lutex->gen].lutexes = native_pointer(copied);
2232 size_lutex(lispobj *where)
2234 return CEILING(sizeof(struct lutex)/sizeof(lispobj), 2);
2236 #endif /* LUTEX_WIDETAG */
2243 /* XX This is a hack adapted from cgc.c. These don't work too
2244 * efficiently with the gencgc as a list of the weak pointers is
2245 * maintained within the objects which causes writes to the pages. A
2246 * limited attempt is made to avoid unnecessary writes, but this needs
2248 #define WEAK_POINTER_NWORDS \
2249 CEILING((sizeof(struct weak_pointer) / sizeof(lispobj)), 2)
2252 scav_weak_pointer(lispobj *where, lispobj object)
2254 struct weak_pointer *wp = weak_pointers;
2255 /* Push the weak pointer onto the list of weak pointers.
2256 * Do I have to watch for duplicates? Originally this was
2257 * part of trans_weak_pointer but that didn't work in the
2258 * case where the WP was in a promoted region.
2261 /* Check whether it's already in the list. */
2262 while (wp != NULL) {
2263 if (wp == (struct weak_pointer*)where) {
2269 /* Add it to the start of the list. */
2270 wp = (struct weak_pointer*)where;
2271 if (wp->next != weak_pointers) {
2272 wp->next = weak_pointers;
2274 /*SHOW("avoided write to weak pointer");*/
2279 /* Do not let GC scavenge the value slot of the weak pointer.
2280 * (That is why it is a weak pointer.) */
2282 return WEAK_POINTER_NWORDS;
2287 search_read_only_space(void *pointer)
2289 lispobj *start = (lispobj *) READ_ONLY_SPACE_START;
2290 lispobj *end = (lispobj *) SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0);
2291 if ((pointer < (void *)start) || (pointer >= (void *)end))
2293 return (gc_search_space(start,
2294 (((lispobj *)pointer)+2)-start,
2295 (lispobj *) pointer));
2299 search_static_space(void *pointer)
2301 lispobj *start = (lispobj *)STATIC_SPACE_START;
2302 lispobj *end = (lispobj *)SymbolValue(STATIC_SPACE_FREE_POINTER,0);
2303 if ((pointer < (void *)start) || (pointer >= (void *)end))
2305 return (gc_search_space(start,
2306 (((lispobj *)pointer)+2)-start,
2307 (lispobj *) pointer));
2310 /* a faster version for searching the dynamic space. This will work even
2311 * if the object is in a current allocation region. */
2313 search_dynamic_space(void *pointer)
2315 page_index_t page_index = find_page_index(pointer);
2318 /* The address may be invalid, so do some checks. */
2319 if ((page_index == -1) ||
2320 (page_table[page_index].allocated == FREE_PAGE_FLAG))
2322 start = (lispobj *)((void *)page_address(page_index)
2323 + page_table[page_index].first_object_offset);
2324 return (gc_search_space(start,
2325 (((lispobj *)pointer)+2)-start,
2326 (lispobj *)pointer));
2329 /* Is there any possibility that pointer is a valid Lisp object
2330 * reference, and/or something else (e.g. subroutine call return
2331 * address) which should prevent us from moving the referred-to thing?
2332 * This is called from preserve_pointers() */
2334 possibly_valid_dynamic_space_pointer(lispobj *pointer)
2336 lispobj *start_addr;
2338 /* Find the object start address. */
2339 if ((start_addr = search_dynamic_space(pointer)) == NULL) {
2343 /* We need to allow raw pointers into Code objects for return
2344 * addresses. This will also pick up pointers to functions in code
2346 if (widetag_of(*start_addr) == CODE_HEADER_WIDETAG) {
2347 /* XXX could do some further checks here */
2351 /* If it's not a return address then it needs to be a valid Lisp
2353 if (!is_lisp_pointer((lispobj)pointer)) {
2357 /* Check that the object pointed to is consistent with the pointer
2360 switch (lowtag_of((lispobj)pointer)) {
2361 case FUN_POINTER_LOWTAG:
2362 /* Start_addr should be the enclosing code object, or a closure
2364 switch (widetag_of(*start_addr)) {
2365 case CODE_HEADER_WIDETAG:
2366 /* This case is probably caught above. */
2368 case CLOSURE_HEADER_WIDETAG:
2369 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
2370 if ((unsigned long)pointer !=
2371 ((unsigned long)start_addr+FUN_POINTER_LOWTAG)) {
2375 pointer, start_addr, *start_addr));
2383 pointer, start_addr, *start_addr));
2387 case LIST_POINTER_LOWTAG:
2388 if ((unsigned long)pointer !=
2389 ((unsigned long)start_addr+LIST_POINTER_LOWTAG)) {
2393 pointer, start_addr, *start_addr));
2396 /* Is it plausible cons? */
2397 if ((is_lisp_pointer(start_addr[0])
2398 || (fixnump(start_addr[0]))
2399 || (widetag_of(start_addr[0]) == CHARACTER_WIDETAG)
2400 #if N_WORD_BITS == 64
2401 || (widetag_of(start_addr[0]) == SINGLE_FLOAT_WIDETAG)
2403 || (widetag_of(start_addr[0]) == UNBOUND_MARKER_WIDETAG))
2404 && (is_lisp_pointer(start_addr[1])
2405 || (fixnump(start_addr[1]))
2406 || (widetag_of(start_addr[1]) == CHARACTER_WIDETAG)
2407 #if N_WORD_BITS == 64
2408 || (widetag_of(start_addr[1]) == SINGLE_FLOAT_WIDETAG)
2410 || (widetag_of(start_addr[1]) == UNBOUND_MARKER_WIDETAG)))
2416 pointer, start_addr, *start_addr));
2419 case INSTANCE_POINTER_LOWTAG:
2420 if ((unsigned long)pointer !=
2421 ((unsigned long)start_addr+INSTANCE_POINTER_LOWTAG)) {
2425 pointer, start_addr, *start_addr));
2428 if (widetag_of(start_addr[0]) != INSTANCE_HEADER_WIDETAG) {
2432 pointer, start_addr, *start_addr));
2436 case OTHER_POINTER_LOWTAG:
2437 if ((unsigned long)pointer !=
2438 ((unsigned long)start_addr+OTHER_POINTER_LOWTAG)) {
2442 pointer, start_addr, *start_addr));
2445 /* Is it plausible? Not a cons. XXX should check the headers. */
2446 if (is_lisp_pointer(start_addr[0]) || ((start_addr[0] & 3) == 0)) {
2450 pointer, start_addr, *start_addr));
2453 switch (widetag_of(start_addr[0])) {
2454 case UNBOUND_MARKER_WIDETAG:
2455 case NO_TLS_VALUE_MARKER_WIDETAG:
2456 case CHARACTER_WIDETAG:
2457 #if N_WORD_BITS == 64
2458 case SINGLE_FLOAT_WIDETAG:
2463 pointer, start_addr, *start_addr));
2466 /* only pointed to by function pointers? */
2467 case CLOSURE_HEADER_WIDETAG:
2468 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
2472 pointer, start_addr, *start_addr));
2475 case INSTANCE_HEADER_WIDETAG:
2479 pointer, start_addr, *start_addr));
2482 /* the valid other immediate pointer objects */
2483 case SIMPLE_VECTOR_WIDETAG:
2485 case COMPLEX_WIDETAG:
2486 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
2487 case COMPLEX_SINGLE_FLOAT_WIDETAG:
2489 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
2490 case COMPLEX_DOUBLE_FLOAT_WIDETAG:
2492 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
2493 case COMPLEX_LONG_FLOAT_WIDETAG:
2495 case SIMPLE_ARRAY_WIDETAG:
2496 case COMPLEX_BASE_STRING_WIDETAG:
2497 #ifdef COMPLEX_CHARACTER_STRING_WIDETAG
2498 case COMPLEX_CHARACTER_STRING_WIDETAG:
2500 case COMPLEX_VECTOR_NIL_WIDETAG:
2501 case COMPLEX_BIT_VECTOR_WIDETAG:
2502 case COMPLEX_VECTOR_WIDETAG:
2503 case COMPLEX_ARRAY_WIDETAG:
2504 case VALUE_CELL_HEADER_WIDETAG:
2505 case SYMBOL_HEADER_WIDETAG:
2507 case CODE_HEADER_WIDETAG:
2508 case BIGNUM_WIDETAG:
2509 #if N_WORD_BITS != 64
2510 case SINGLE_FLOAT_WIDETAG:
2512 case DOUBLE_FLOAT_WIDETAG:
2513 #ifdef LONG_FLOAT_WIDETAG
2514 case LONG_FLOAT_WIDETAG:
2516 case SIMPLE_BASE_STRING_WIDETAG:
2517 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
2518 case SIMPLE_CHARACTER_STRING_WIDETAG:
2520 case SIMPLE_BIT_VECTOR_WIDETAG:
2521 case SIMPLE_ARRAY_NIL_WIDETAG:
2522 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
2523 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
2524 case SIMPLE_ARRAY_UNSIGNED_BYTE_7_WIDETAG:
2525 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
2526 case SIMPLE_ARRAY_UNSIGNED_BYTE_15_WIDETAG:
2527 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
2528 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_29_WIDETAG
2529 case SIMPLE_ARRAY_UNSIGNED_BYTE_29_WIDETAG:
2531 case SIMPLE_ARRAY_UNSIGNED_BYTE_31_WIDETAG:
2532 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
2533 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_60_WIDETAG
2534 case SIMPLE_ARRAY_UNSIGNED_BYTE_60_WIDETAG:
2536 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG
2537 case SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG:
2539 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG
2540 case SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG:
2542 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
2543 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
2545 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
2546 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
2548 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
2549 case SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG:
2551 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
2552 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
2554 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_61_WIDETAG
2555 case SIMPLE_ARRAY_SIGNED_BYTE_61_WIDETAG:
2557 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG
2558 case SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG:
2560 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
2561 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
2562 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
2563 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
2565 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
2566 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
2568 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
2569 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
2571 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
2572 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
2575 case WEAK_POINTER_WIDETAG:
2576 #ifdef LUTEX_WIDETAG
2585 pointer, start_addr, *start_addr));
2593 pointer, start_addr, *start_addr));
2601 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
2603 /* Adjust large bignum and vector objects. This will adjust the
2604 * allocated region if the size has shrunk, and move unboxed objects
2605 * into unboxed pages. The pages are not promoted here, and the
2606 * promoted region is not added to the new_regions; this is really
2607 * only designed to be called from preserve_pointer(). Shouldn't fail
2608 * if this is missed, just may delay the moving of objects to unboxed
2609 * pages, and the freeing of pages. */
2611 maybe_adjust_large_object(lispobj *where)
2613 page_index_t first_page;
2614 page_index_t next_page;
2617 long remaining_bytes;
2619 long old_bytes_used;
2623 /* Check whether it's a vector or bignum object. */
2624 switch (widetag_of(where[0])) {
2625 case SIMPLE_VECTOR_WIDETAG:
2626 boxed = BOXED_PAGE_FLAG;
2628 case BIGNUM_WIDETAG:
2629 case SIMPLE_BASE_STRING_WIDETAG:
2630 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
2631 case SIMPLE_CHARACTER_STRING_WIDETAG:
2633 case SIMPLE_BIT_VECTOR_WIDETAG:
2634 case SIMPLE_ARRAY_NIL_WIDETAG:
2635 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
2636 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
2637 case SIMPLE_ARRAY_UNSIGNED_BYTE_7_WIDETAG:
2638 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
2639 case SIMPLE_ARRAY_UNSIGNED_BYTE_15_WIDETAG:
2640 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
2641 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_29_WIDETAG
2642 case SIMPLE_ARRAY_UNSIGNED_BYTE_29_WIDETAG:
2644 case SIMPLE_ARRAY_UNSIGNED_BYTE_31_WIDETAG:
2645 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
2646 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_60_WIDETAG
2647 case SIMPLE_ARRAY_UNSIGNED_BYTE_60_WIDETAG:
2649 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG
2650 case SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG:
2652 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG
2653 case SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG:
2655 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
2656 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
2658 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
2659 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
2661 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
2662 case SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG:
2664 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
2665 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
2667 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_61_WIDETAG
2668 case SIMPLE_ARRAY_SIGNED_BYTE_61_WIDETAG:
2670 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG
2671 case SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG:
2673 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
2674 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
2675 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
2676 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
2678 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
2679 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
2681 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
2682 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
2684 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
2685 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
2687 boxed = UNBOXED_PAGE_FLAG;
2693 /* Find its current size. */
2694 nwords = (sizetab[widetag_of(where[0])])(where);
2696 first_page = find_page_index((void *)where);
2697 gc_assert(first_page >= 0);
2699 /* Note: Any page write-protection must be removed, else a later
2700 * scavenge_newspace may incorrectly not scavenge these pages.
2701 * This would not be necessary if they are added to the new areas,
2702 * but lets do it for them all (they'll probably be written
2705 gc_assert(page_table[first_page].first_object_offset == 0);
2707 next_page = first_page;
2708 remaining_bytes = nwords*N_WORD_BYTES;
2709 while (remaining_bytes > PAGE_BYTES) {
2710 gc_assert(page_table[next_page].gen == from_space);
2711 gc_assert((page_table[next_page].allocated == BOXED_PAGE_FLAG)
2712 || (page_table[next_page].allocated == UNBOXED_PAGE_FLAG));
2713 gc_assert(page_table[next_page].large_object);
2714 gc_assert(page_table[next_page].first_object_offset ==
2715 -PAGE_BYTES*(next_page-first_page));
2716 gc_assert(page_table[next_page].bytes_used == PAGE_BYTES);
2718 page_table[next_page].allocated = boxed;
2720 /* Shouldn't be write-protected at this stage. Essential that the
2722 gc_assert(!page_table[next_page].write_protected);
2723 remaining_bytes -= PAGE_BYTES;
2727 /* Now only one page remains, but the object may have shrunk so
2728 * there may be more unused pages which will be freed. */
2730 /* Object may have shrunk but shouldn't have grown - check. */
2731 gc_assert(page_table[next_page].bytes_used >= remaining_bytes);
2733 page_table[next_page].allocated = boxed;
2734 gc_assert(page_table[next_page].allocated ==
2735 page_table[first_page].allocated);
2737 /* Adjust the bytes_used. */
2738 old_bytes_used = page_table[next_page].bytes_used;
2739 page_table[next_page].bytes_used = remaining_bytes;
2741 bytes_freed = old_bytes_used - remaining_bytes;
2743 /* Free any remaining pages; needs care. */
2745 while ((old_bytes_used == PAGE_BYTES) &&
2746 (page_table[next_page].gen == from_space) &&
2747 ((page_table[next_page].allocated == UNBOXED_PAGE_FLAG)
2748 || (page_table[next_page].allocated == BOXED_PAGE_FLAG)) &&
2749 page_table[next_page].large_object &&
2750 (page_table[next_page].first_object_offset ==
2751 -(next_page - first_page)*PAGE_BYTES)) {
2752 /* It checks out OK, free the page. We don't need to both zeroing
2753 * pages as this should have been done before shrinking the
2754 * object. These pages shouldn't be write protected as they
2755 * should be zero filled. */
2756 gc_assert(page_table[next_page].write_protected == 0);
2758 old_bytes_used = page_table[next_page].bytes_used;
2759 page_table[next_page].allocated = FREE_PAGE_FLAG;
2760 page_table[next_page].bytes_used = 0;
2761 bytes_freed += old_bytes_used;
2765 if ((bytes_freed > 0) && gencgc_verbose) {
2767 "/maybe_adjust_large_object() freed %d\n",
2771 generations[from_space].bytes_allocated -= bytes_freed;
2772 bytes_allocated -= bytes_freed;
2779 /* Take a possible pointer to a Lisp object and mark its page in the
2780 * page_table so that it will not be relocated during a GC.
2782 * This involves locating the page it points to, then backing up to
2783 * the start of its region, then marking all pages dont_move from there
2784 * up to the first page that's not full or has a different generation
2786 * It is assumed that all the page static flags have been cleared at
2787 * the start of a GC.
2789 * It is also assumed that the current gc_alloc() region has been
2790 * flushed and the tables updated. */
2792 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
2795 preserve_pointer(void *addr)
2797 page_index_t addr_page_index = find_page_index(addr);
2798 page_index_t first_page;
2800 unsigned int region_allocation;
2802 /* quick check 1: Address is quite likely to have been invalid. */
2803 if ((addr_page_index == -1)
2804 || (page_table[addr_page_index].allocated == FREE_PAGE_FLAG)
2805 || (page_table[addr_page_index].bytes_used == 0)
2806 || (page_table[addr_page_index].gen != from_space)
2807 /* Skip if already marked dont_move. */
2808 || (page_table[addr_page_index].dont_move != 0))
2810 gc_assert(!(page_table[addr_page_index].allocated&OPEN_REGION_PAGE_FLAG));
2811 /* (Now that we know that addr_page_index is in range, it's
2812 * safe to index into page_table[] with it.) */
2813 region_allocation = page_table[addr_page_index].allocated;
2815 /* quick check 2: Check the offset within the page.
2818 if (((unsigned long)addr & (PAGE_BYTES - 1)) > page_table[addr_page_index].bytes_used)
2821 /* Filter out anything which can't be a pointer to a Lisp object
2822 * (or, as a special case which also requires dont_move, a return
2823 * address referring to something in a CodeObject). This is
2824 * expensive but important, since it vastly reduces the
2825 * probability that random garbage will be bogusly interpreted as
2826 * a pointer which prevents a page from moving. */
2827 if (!(possibly_valid_dynamic_space_pointer(addr)))
2830 /* Find the beginning of the region. Note that there may be
2831 * objects in the region preceding the one that we were passed a
2832 * pointer to: if this is the case, we will write-protect all the
2833 * previous objects' pages too. */
2836 /* I think this'd work just as well, but without the assertions.
2837 * -dan 2004.01.01 */
2839 find_page_index(page_address(addr_page_index)+
2840 page_table[addr_page_index].first_object_offset);
2842 first_page = addr_page_index;
2843 while (page_table[first_page].first_object_offset != 0) {
2845 /* Do some checks. */
2846 gc_assert(page_table[first_page].bytes_used == PAGE_BYTES);
2847 gc_assert(page_table[first_page].gen == from_space);
2848 gc_assert(page_table[first_page].allocated == region_allocation);
2852 /* Adjust any large objects before promotion as they won't be
2853 * copied after promotion. */
2854 if (page_table[first_page].large_object) {
2855 maybe_adjust_large_object(page_address(first_page));
2856 /* If a large object has shrunk then addr may now point to a
2857 * free area in which case it's ignored here. Note it gets
2858 * through the valid pointer test above because the tail looks
2860 if ((page_table[addr_page_index].allocated == FREE_PAGE_FLAG)
2861 || (page_table[addr_page_index].bytes_used == 0)
2862 /* Check the offset within the page. */
2863 || (((unsigned long)addr & (PAGE_BYTES - 1))
2864 > page_table[addr_page_index].bytes_used)) {
2866 "weird? ignore ptr 0x%x to freed area of large object\n",
2870 /* It may have moved to unboxed pages. */
2871 region_allocation = page_table[first_page].allocated;
2874 /* Now work forward until the end of this contiguous area is found,
2875 * marking all pages as dont_move. */
2876 for (i = first_page; ;i++) {
2877 gc_assert(page_table[i].allocated == region_allocation);
2879 /* Mark the page static. */
2880 page_table[i].dont_move = 1;
2882 /* Move the page to the new_space. XX I'd rather not do this
2883 * but the GC logic is not quite able to copy with the static
2884 * pages remaining in the from space. This also requires the
2885 * generation bytes_allocated counters be updated. */
2886 page_table[i].gen = new_space;
2887 generations[new_space].bytes_allocated += page_table[i].bytes_used;
2888 generations[from_space].bytes_allocated -= page_table[i].bytes_used;
2890 /* It is essential that the pages are not write protected as
2891 * they may have pointers into the old-space which need
2892 * scavenging. They shouldn't be write protected at this
2894 gc_assert(!page_table[i].write_protected);
2896 /* Check whether this is the last page in this contiguous block.. */
2897 if ((page_table[i].bytes_used < PAGE_BYTES)
2898 /* ..or it is PAGE_BYTES and is the last in the block */
2899 || (page_table[i+1].allocated == FREE_PAGE_FLAG)
2900 || (page_table[i+1].bytes_used == 0) /* next page free */
2901 || (page_table[i+1].gen != from_space) /* diff. gen */
2902 || (page_table[i+1].first_object_offset == 0))
2906 /* Check that the page is now static. */
2907 gc_assert(page_table[addr_page_index].dont_move != 0);
2913 /* If the given page is not write-protected, then scan it for pointers
2914 * to younger generations or the top temp. generation, if no
2915 * suspicious pointers are found then the page is write-protected.
2917 * Care is taken to check for pointers to the current gc_alloc()
2918 * region if it is a younger generation or the temp. generation. This
2919 * frees the caller from doing a gc_alloc_update_page_tables(). Actually
2920 * the gc_alloc_generation does not need to be checked as this is only
2921 * called from scavenge_generation() when the gc_alloc generation is
2922 * younger, so it just checks if there is a pointer to the current
2925 * We return 1 if the page was write-protected, else 0. */
2927 update_page_write_prot(page_index_t page)
2929 generation_index_t gen = page_table[page].gen;
2932 void **page_addr = (void **)page_address(page);
2933 long num_words = page_table[page].bytes_used / N_WORD_BYTES;
2935 /* Shouldn't be a free page. */
2936 gc_assert(page_table[page].allocated != FREE_PAGE_FLAG);
2937 gc_assert(page_table[page].bytes_used != 0);
2939 /* Skip if it's already write-protected, pinned, or unboxed */
2940 if (page_table[page].write_protected
2941 /* FIXME: What's the reason for not write-protecting pinned pages? */
2942 || page_table[page].dont_move
2943 || (page_table[page].allocated & UNBOXED_PAGE_FLAG))
2946 /* Scan the page for pointers to younger generations or the
2947 * top temp. generation. */
2949 for (j = 0; j < num_words; j++) {
2950 void *ptr = *(page_addr+j);
2951 page_index_t index = find_page_index(ptr);
2953 /* Check that it's in the dynamic space */
2955 if (/* Does it point to a younger or the temp. generation? */
2956 ((page_table[index].allocated != FREE_PAGE_FLAG)
2957 && (page_table[index].bytes_used != 0)
2958 && ((page_table[index].gen < gen)
2959 || (page_table[index].gen == SCRATCH_GENERATION)))
2961 /* Or does it point within a current gc_alloc() region? */
2962 || ((boxed_region.start_addr <= ptr)
2963 && (ptr <= boxed_region.free_pointer))
2964 || ((unboxed_region.start_addr <= ptr)
2965 && (ptr <= unboxed_region.free_pointer))) {
2972 /* Write-protect the page. */
2973 /*FSHOW((stderr, "/write-protecting page %d gen %d\n", page, gen));*/
2975 os_protect((void *)page_addr,
2977 OS_VM_PROT_READ|OS_VM_PROT_EXECUTE);
2979 /* Note the page as protected in the page tables. */
2980 page_table[page].write_protected = 1;
2986 /* Scavenge all generations from FROM to TO, inclusive, except for
2987 * new_space which needs special handling, as new objects may be
2988 * added which are not checked here - use scavenge_newspace generation.
2990 * Write-protected pages should not have any pointers to the
2991 * from_space so do need scavenging; thus write-protected pages are
2992 * not always scavenged. There is some code to check that these pages
2993 * are not written; but to check fully the write-protected pages need
2994 * to be scavenged by disabling the code to skip them.
2996 * Under the current scheme when a generation is GCed the younger
2997 * generations will be empty. So, when a generation is being GCed it
2998 * is only necessary to scavenge the older generations for pointers
2999 * not the younger. So a page that does not have pointers to younger
3000 * generations does not need to be scavenged.
3002 * The write-protection can be used to note pages that don't have
3003 * pointers to younger pages. But pages can be written without having
3004 * pointers to younger generations. After the pages are scavenged here
3005 * they can be scanned for pointers to younger generations and if
3006 * there are none the page can be write-protected.
3008 * One complication is when the newspace is the top temp. generation.
3010 * Enabling SC_GEN_CK scavenges the write-protected pages and checks
3011 * that none were written, which they shouldn't be as they should have
3012 * no pointers to younger generations. This breaks down for weak
3013 * pointers as the objects contain a link to the next and are written
3014 * if a weak pointer is scavenged. Still it's a useful check. */
3016 scavenge_generations(generation_index_t from, generation_index_t to)
3023 /* Clear the write_protected_cleared flags on all pages. */
3024 for (i = 0; i < NUM_PAGES; i++)
3025 page_table[i].write_protected_cleared = 0;
3028 for (i = 0; i < last_free_page; i++) {
3029 generation_index_t generation = page_table[i].gen;
3030 if ((page_table[i].allocated & BOXED_PAGE_FLAG)
3031 && (page_table[i].bytes_used != 0)
3032 && (generation != new_space)
3033 && (generation >= from)
3034 && (generation <= to)) {
3035 page_index_t last_page,j;
3036 int write_protected=1;
3038 /* This should be the start of a region */
3039 gc_assert(page_table[i].first_object_offset == 0);
3041 /* Now work forward until the end of the region */
3042 for (last_page = i; ; last_page++) {
3044 write_protected && page_table[last_page].write_protected;
3045 if ((page_table[last_page].bytes_used < PAGE_BYTES)
3046 /* Or it is PAGE_BYTES and is the last in the block */
3047 || (!(page_table[last_page+1].allocated & BOXED_PAGE_FLAG))
3048 || (page_table[last_page+1].bytes_used == 0)
3049 || (page_table[last_page+1].gen != generation)
3050 || (page_table[last_page+1].first_object_offset == 0))
3053 if (!write_protected) {
3054 scavenge(page_address(i),
3055 (page_table[last_page].bytes_used +
3056 (last_page-i)*PAGE_BYTES)/N_WORD_BYTES);
3058 /* Now scan the pages and write protect those that
3059 * don't have pointers to younger generations. */
3060 if (enable_page_protection) {
3061 for (j = i; j <= last_page; j++) {
3062 num_wp += update_page_write_prot(j);
3065 if ((gencgc_verbose > 1) && (num_wp != 0)) {
3067 "/write protected %d pages within generation %d\n",
3068 num_wp, generation));
3076 /* Check that none of the write_protected pages in this generation
3077 * have been written to. */
3078 for (i = 0; i < NUM_PAGES; i++) {
3079 if ((page_table[i].allocation != FREE_PAGE_FLAG)
3080 && (page_table[i].bytes_used != 0)
3081 && (page_table[i].gen == generation)
3082 && (page_table[i].write_protected_cleared != 0)) {
3083 FSHOW((stderr, "/scavenge_generation() %d\n", generation));
3085 "/page bytes_used=%d first_object_offset=%d dont_move=%d\n",
3086 page_table[i].bytes_used,
3087 page_table[i].first_object_offset,
3088 page_table[i].dont_move));
3089 lose("write to protected page %d in scavenge_generation()\n", i);
3096 /* Scavenge a newspace generation. As it is scavenged new objects may
3097 * be allocated to it; these will also need to be scavenged. This
3098 * repeats until there are no more objects unscavenged in the
3099 * newspace generation.
3101 * To help improve the efficiency, areas written are recorded by
3102 * gc_alloc() and only these scavenged. Sometimes a little more will be
3103 * scavenged, but this causes no harm. An easy check is done that the
3104 * scavenged bytes equals the number allocated in the previous
3107 * Write-protected pages are not scanned except if they are marked
3108 * dont_move in which case they may have been promoted and still have
3109 * pointers to the from space.
3111 * Write-protected pages could potentially be written by alloc however
3112 * to avoid having to handle re-scavenging of write-protected pages
3113 * gc_alloc() does not write to write-protected pages.
3115 * New areas of objects allocated are recorded alternatively in the two
3116 * new_areas arrays below. */
3117 static struct new_area new_areas_1[NUM_NEW_AREAS];
3118 static struct new_area new_areas_2[NUM_NEW_AREAS];
3120 /* Do one full scan of the new space generation. This is not enough to
3121 * complete the job as new objects may be added to the generation in
3122 * the process which are not scavenged. */
3124 scavenge_newspace_generation_one_scan(generation_index_t generation)
3129 "/starting one full scan of newspace generation %d\n",
3131 for (i = 0; i < last_free_page; i++) {
3132 /* Note that this skips over open regions when it encounters them. */
3133 if ((page_table[i].allocated & BOXED_PAGE_FLAG)
3134 && (page_table[i].bytes_used != 0)
3135 && (page_table[i].gen == generation)
3136 && ((page_table[i].write_protected == 0)
3137 /* (This may be redundant as write_protected is now
3138 * cleared before promotion.) */
3139 || (page_table[i].dont_move == 1))) {
3140 page_index_t last_page;
3143 /* The scavenge will start at the first_object_offset of page i.
3145 * We need to find the full extent of this contiguous
3146 * block in case objects span pages.
3148 * Now work forward until the end of this contiguous area
3149 * is found. A small area is preferred as there is a
3150 * better chance of its pages being write-protected. */
3151 for (last_page = i; ;last_page++) {
3152 /* If all pages are write-protected and movable,
3153 * then no need to scavenge */
3154 all_wp=all_wp && page_table[last_page].write_protected &&
3155 !page_table[last_page].dont_move;
3157 /* Check whether this is the last page in this
3158 * contiguous block */
3159 if ((page_table[last_page].bytes_used < PAGE_BYTES)
3160 /* Or it is PAGE_BYTES and is the last in the block */
3161 || (!(page_table[last_page+1].allocated & BOXED_PAGE_FLAG))
3162 || (page_table[last_page+1].bytes_used == 0)
3163 || (page_table[last_page+1].gen != generation)
3164 || (page_table[last_page+1].first_object_offset == 0))
3168 /* Do a limited check for write-protected pages. */
3172 size = (page_table[last_page].bytes_used
3173 + (last_page-i)*PAGE_BYTES
3174 - page_table[i].first_object_offset)/N_WORD_BYTES;
3175 new_areas_ignore_page = last_page;
3177 scavenge(page_address(i) +
3178 page_table[i].first_object_offset,
3186 "/done with one full scan of newspace generation %d\n",
3190 /* Do a complete scavenge of the newspace generation. */
3192 scavenge_newspace_generation(generation_index_t generation)
3196 /* the new_areas array currently being written to by gc_alloc() */
3197 struct new_area (*current_new_areas)[] = &new_areas_1;
3198 long current_new_areas_index;
3200 /* the new_areas created by the previous scavenge cycle */
3201 struct new_area (*previous_new_areas)[] = NULL;
3202 long previous_new_areas_index;
3204 /* Flush the current regions updating the tables. */
3205 gc_alloc_update_all_page_tables();
3207 /* Turn on the recording of new areas by gc_alloc(). */
3208 new_areas = current_new_areas;
3209 new_areas_index = 0;
3211 /* Don't need to record new areas that get scavenged anyway during
3212 * scavenge_newspace_generation_one_scan. */
3213 record_new_objects = 1;
3215 /* Start with a full scavenge. */
3216 scavenge_newspace_generation_one_scan(generation);
3218 /* Record all new areas now. */
3219 record_new_objects = 2;
3221 /* Flush the current regions updating the tables. */
3222 gc_alloc_update_all_page_tables();
3224 /* Grab new_areas_index. */
3225 current_new_areas_index = new_areas_index;
3228 "The first scan is finished; current_new_areas_index=%d.\n",
3229 current_new_areas_index));*/
3231 while (current_new_areas_index > 0) {
3232 /* Move the current to the previous new areas */
3233 previous_new_areas = current_new_areas;
3234 previous_new_areas_index = current_new_areas_index;
3236 /* Scavenge all the areas in previous new areas. Any new areas
3237 * allocated are saved in current_new_areas. */
3239 /* Allocate an array for current_new_areas; alternating between
3240 * new_areas_1 and 2 */
3241 if (previous_new_areas == &new_areas_1)
3242 current_new_areas = &new_areas_2;
3244 current_new_areas = &new_areas_1;
3246 /* Set up for gc_alloc(). */
3247 new_areas = current_new_areas;
3248 new_areas_index = 0;
3250 /* Check whether previous_new_areas had overflowed. */
3251 if (previous_new_areas_index >= NUM_NEW_AREAS) {
3253 /* New areas of objects allocated have been lost so need to do a
3254 * full scan to be sure! If this becomes a problem try
3255 * increasing NUM_NEW_AREAS. */
3257 SHOW("new_areas overflow, doing full scavenge");
3259 /* Don't need to record new areas that get scavenge anyway
3260 * during scavenge_newspace_generation_one_scan. */
3261 record_new_objects = 1;
3263 scavenge_newspace_generation_one_scan(generation);
3265 /* Record all new areas now. */
3266 record_new_objects = 2;
3268 /* Flush the current regions updating the tables. */
3269 gc_alloc_update_all_page_tables();
3273 /* Work through previous_new_areas. */
3274 for (i = 0; i < previous_new_areas_index; i++) {
3275 long page = (*previous_new_areas)[i].page;
3276 long offset = (*previous_new_areas)[i].offset;
3277 long size = (*previous_new_areas)[i].size / N_WORD_BYTES;
3278 gc_assert((*previous_new_areas)[i].size % N_WORD_BYTES == 0);
3279 scavenge(page_address(page)+offset, size);
3282 /* Flush the current regions updating the tables. */
3283 gc_alloc_update_all_page_tables();
3286 current_new_areas_index = new_areas_index;
3289 "The re-scan has finished; current_new_areas_index=%d.\n",
3290 current_new_areas_index));*/
3293 /* Turn off recording of areas allocated by gc_alloc(). */
3294 record_new_objects = 0;
3297 /* Check that none of the write_protected pages in this generation
3298 * have been written to. */
3299 for (i = 0; i < NUM_PAGES; i++) {
3300 if ((page_table[i].allocation != FREE_PAGE_FLAG)
3301 && (page_table[i].bytes_used != 0)
3302 && (page_table[i].gen == generation)
3303 && (page_table[i].write_protected_cleared != 0)
3304 && (page_table[i].dont_move == 0)) {
3305 lose("write protected page %d written to in scavenge_newspace_generation\ngeneration=%d dont_move=%d\n",
3306 i, generation, page_table[i].dont_move);
3312 /* Un-write-protect all the pages in from_space. This is done at the
3313 * start of a GC else there may be many page faults while scavenging
3314 * the newspace (I've seen drive the system time to 99%). These pages
3315 * would need to be unprotected anyway before unmapping in
3316 * free_oldspace; not sure what effect this has on paging.. */
3318 unprotect_oldspace(void)
3322 for (i = 0; i < last_free_page; i++) {
3323 if ((page_table[i].allocated != FREE_PAGE_FLAG)
3324 && (page_table[i].bytes_used != 0)
3325 && (page_table[i].gen == from_space)) {
3328 page_start = (void *)page_address(i);
3330 /* Remove any write-protection. We should be able to rely
3331 * on the write-protect flag to avoid redundant calls. */
3332 if (page_table[i].write_protected) {
3333 os_protect(page_start, PAGE_BYTES, OS_VM_PROT_ALL);
3334 page_table[i].write_protected = 0;
3340 /* Work through all the pages and free any in from_space. This
3341 * assumes that all objects have been copied or promoted to an older
3342 * generation. Bytes_allocated and the generation bytes_allocated
3343 * counter are updated. The number of bytes freed is returned. */
3347 long bytes_freed = 0;
3348 page_index_t first_page, last_page;
3353 /* Find a first page for the next region of pages. */
3354 while ((first_page < last_free_page)
3355 && ((page_table[first_page].allocated == FREE_PAGE_FLAG)
3356 || (page_table[first_page].bytes_used == 0)
3357 || (page_table[first_page].gen != from_space)))
3360 if (first_page >= last_free_page)
3363 /* Find the last page of this region. */
3364 last_page = first_page;
3367 /* Free the page. */
3368 bytes_freed += page_table[last_page].bytes_used;
3369 generations[page_table[last_page].gen].bytes_allocated -=
3370 page_table[last_page].bytes_used;
3371 page_table[last_page].allocated = FREE_PAGE_FLAG;
3372 page_table[last_page].bytes_used = 0;
3374 /* Remove any write-protection. We should be able to rely
3375 * on the write-protect flag to avoid redundant calls. */
3377 void *page_start = (void *)page_address(last_page);
3379 if (page_table[last_page].write_protected) {
3380 os_protect(page_start, PAGE_BYTES, OS_VM_PROT_ALL);
3381 page_table[last_page].write_protected = 0;
3386 while ((last_page < last_free_page)
3387 && (page_table[last_page].allocated != FREE_PAGE_FLAG)
3388 && (page_table[last_page].bytes_used != 0)
3389 && (page_table[last_page].gen == from_space));
3391 #ifdef READ_PROTECT_FREE_PAGES
3392 os_protect(page_address(first_page),
3393 PAGE_BYTES*(last_page-first_page),
3396 first_page = last_page;
3397 } while (first_page < last_free_page);
3399 bytes_allocated -= bytes_freed;
3404 /* Print some information about a pointer at the given address. */
3406 print_ptr(lispobj *addr)
3408 /* If addr is in the dynamic space then out the page information. */
3409 page_index_t pi1 = find_page_index((void*)addr);
3412 fprintf(stderr," %x: page %d alloc %d gen %d bytes_used %d offset %d dont_move %d\n",
3413 (unsigned long) addr,
3415 page_table[pi1].allocated,
3416 page_table[pi1].gen,
3417 page_table[pi1].bytes_used,
3418 page_table[pi1].first_object_offset,
3419 page_table[pi1].dont_move);
3420 fprintf(stderr," %x %x %x %x (%x) %x %x %x %x\n",
3433 #if defined(LISP_FEATURE_PPC)
3434 extern int closure_tramp;
3435 extern int undefined_tramp;
3437 extern int undefined_tramp;
3441 verify_space(lispobj *start, size_t words)
3443 int is_in_dynamic_space = (find_page_index((void*)start) != -1);
3444 int is_in_readonly_space =
3445 (READ_ONLY_SPACE_START <= (unsigned long)start &&
3446 (unsigned long)start < SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0));
3450 lispobj thing = *(lispobj*)start;
3452 if (is_lisp_pointer(thing)) {
3453 page_index_t page_index = find_page_index((void*)thing);
3454 long to_readonly_space =
3455 (READ_ONLY_SPACE_START <= thing &&
3456 thing < SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0));
3457 long to_static_space =
3458 (STATIC_SPACE_START <= thing &&
3459 thing < SymbolValue(STATIC_SPACE_FREE_POINTER,0));
3461 /* Does it point to the dynamic space? */
3462 if (page_index != -1) {
3463 /* If it's within the dynamic space it should point to a used
3464 * page. XX Could check the offset too. */
3465 if ((page_table[page_index].allocated != FREE_PAGE_FLAG)
3466 && (page_table[page_index].bytes_used == 0))
3467 lose ("Ptr %x @ %x sees free page.\n", thing, start);
3468 /* Check that it doesn't point to a forwarding pointer! */
3469 if (*((lispobj *)native_pointer(thing)) == 0x01) {
3470 lose("Ptr %x @ %x sees forwarding ptr.\n", thing, start);
3472 /* Check that its not in the RO space as it would then be a
3473 * pointer from the RO to the dynamic space. */
3474 if (is_in_readonly_space) {
3475 lose("ptr to dynamic space %x from RO space %x\n",
3478 /* Does it point to a plausible object? This check slows
3479 * it down a lot (so it's commented out).
3481 * "a lot" is serious: it ate 50 minutes cpu time on
3482 * my duron 950 before I came back from lunch and
3485 * FIXME: Add a variable to enable this
3488 if (!possibly_valid_dynamic_space_pointer((lispobj *)thing)) {
3489 lose("ptr %x to invalid object %x\n", thing, start);
3493 /* Verify that it points to another valid space. */
3494 if (!to_readonly_space && !to_static_space &&
3495 #if defined(LISP_FEATURE_PPC)
3496 !((thing == &closure_tramp) ||
3497 (thing == &undefined_tramp))
3499 thing != (unsigned long)&undefined_tramp
3502 lose("Ptr %x @ %x sees junk.\n", thing, start);
3506 if (!(fixnump(thing))) {
3508 switch(widetag_of(*start)) {
3511 case SIMPLE_VECTOR_WIDETAG:
3513 case COMPLEX_WIDETAG:
3514 case SIMPLE_ARRAY_WIDETAG:
3515 case COMPLEX_BASE_STRING_WIDETAG:
3516 #ifdef COMPLEX_CHARACTER_STRING_WIDETAG
3517 case COMPLEX_CHARACTER_STRING_WIDETAG:
3519 case COMPLEX_VECTOR_NIL_WIDETAG:
3520 case COMPLEX_BIT_VECTOR_WIDETAG:
3521 case COMPLEX_VECTOR_WIDETAG:
3522 case COMPLEX_ARRAY_WIDETAG:
3523 case CLOSURE_HEADER_WIDETAG:
3524 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
3525 case VALUE_CELL_HEADER_WIDETAG:
3526 case SYMBOL_HEADER_WIDETAG:
3527 case CHARACTER_WIDETAG:
3528 #if N_WORD_BITS == 64
3529 case SINGLE_FLOAT_WIDETAG:
3531 case UNBOUND_MARKER_WIDETAG:
3536 case INSTANCE_HEADER_WIDETAG:
3539 long ntotal = HeaderValue(thing);
3540 lispobj layout = ((struct instance *)start)->slots[0];
3545 nuntagged = ((struct layout *)native_pointer(layout))->n_untagged_slots;
3546 verify_space(start + 1, ntotal - fixnum_value(nuntagged));
3550 case CODE_HEADER_WIDETAG:
3552 lispobj object = *start;
3554 long nheader_words, ncode_words, nwords;
3556 struct simple_fun *fheaderp;
3558 code = (struct code *) start;
3560 /* Check that it's not in the dynamic space.
3561 * FIXME: Isn't is supposed to be OK for code
3562 * objects to be in the dynamic space these days? */
3563 if (is_in_dynamic_space
3564 /* It's ok if it's byte compiled code. The trace
3565 * table offset will be a fixnum if it's x86
3566 * compiled code - check.
3568 * FIXME: #^#@@! lack of abstraction here..
3569 * This line can probably go away now that
3570 * there's no byte compiler, but I've got
3571 * too much to worry about right now to try
3572 * to make sure. -- WHN 2001-10-06 */
3573 && fixnump(code->trace_table_offset)
3574 /* Only when enabled */
3575 && verify_dynamic_code_check) {
3577 "/code object at %x in the dynamic space\n",
3581 ncode_words = fixnum_value(code->code_size);
3582 nheader_words = HeaderValue(object);
3583 nwords = ncode_words + nheader_words;
3584 nwords = CEILING(nwords, 2);
3585 /* Scavenge the boxed section of the code data block */
3586 verify_space(start + 1, nheader_words - 1);
3588 /* Scavenge the boxed section of each function
3589 * object in the code data block. */
3590 fheaderl = code->entry_points;
3591 while (fheaderl != NIL) {
3593 (struct simple_fun *) native_pointer(fheaderl);
3594 gc_assert(widetag_of(fheaderp->header) == SIMPLE_FUN_HEADER_WIDETAG);
3595 verify_space(&fheaderp->name, 1);
3596 verify_space(&fheaderp->arglist, 1);
3597 verify_space(&fheaderp->type, 1);
3598 fheaderl = fheaderp->next;
3604 /* unboxed objects */
3605 case BIGNUM_WIDETAG:
3606 #if N_WORD_BITS != 64
3607 case SINGLE_FLOAT_WIDETAG:
3609 case DOUBLE_FLOAT_WIDETAG:
3610 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
3611 case LONG_FLOAT_WIDETAG:
3613 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
3614 case COMPLEX_SINGLE_FLOAT_WIDETAG:
3616 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
3617 case COMPLEX_DOUBLE_FLOAT_WIDETAG:
3619 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
3620 case COMPLEX_LONG_FLOAT_WIDETAG:
3622 case SIMPLE_BASE_STRING_WIDETAG:
3623 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
3624 case SIMPLE_CHARACTER_STRING_WIDETAG:
3626 case SIMPLE_BIT_VECTOR_WIDETAG:
3627 case SIMPLE_ARRAY_NIL_WIDETAG:
3628 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
3629 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
3630 case SIMPLE_ARRAY_UNSIGNED_BYTE_7_WIDETAG:
3631 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
3632 case SIMPLE_ARRAY_UNSIGNED_BYTE_15_WIDETAG:
3633 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
3634 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_29_WIDETAG
3635 case SIMPLE_ARRAY_UNSIGNED_BYTE_29_WIDETAG:
3637 case SIMPLE_ARRAY_UNSIGNED_BYTE_31_WIDETAG:
3638 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
3639 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_60_WIDETAG
3640 case SIMPLE_ARRAY_UNSIGNED_BYTE_60_WIDETAG:
3642 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG
3643 case SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG:
3645 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG
3646 case SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG:
3648 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
3649 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
3651 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
3652 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
3654 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
3655 case SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG:
3657 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
3658 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
3660 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_61_WIDETAG
3661 case SIMPLE_ARRAY_SIGNED_BYTE_61_WIDETAG:
3663 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG
3664 case SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG:
3666 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
3667 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
3668 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
3669 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
3671 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
3672 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
3674 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
3675 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
3677 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
3678 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
3681 case WEAK_POINTER_WIDETAG:
3682 #ifdef LUTEX_WIDETAG
3685 count = (sizetab[widetag_of(*start)])(start);
3690 "/Unhandled widetag 0x%x at 0x%x\n",
3691 widetag_of(*start), start));
3705 /* FIXME: It would be nice to make names consistent so that
3706 * foo_size meant size *in* *bytes* instead of size in some
3707 * arbitrary units. (Yes, this caused a bug, how did you guess?:-)
3708 * Some counts of lispobjs are called foo_count; it might be good
3709 * to grep for all foo_size and rename the appropriate ones to
3711 long read_only_space_size =
3712 (lispobj*)SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0)
3713 - (lispobj*)READ_ONLY_SPACE_START;
3714 long static_space_size =
3715 (lispobj*)SymbolValue(STATIC_SPACE_FREE_POINTER,0)
3716 - (lispobj*)STATIC_SPACE_START;
3718 for_each_thread(th) {
3719 long binding_stack_size =
3720 (lispobj*)get_binding_stack_pointer(th)
3721 - (lispobj*)th->binding_stack_start;
3722 verify_space(th->binding_stack_start, binding_stack_size);
3724 verify_space((lispobj*)READ_ONLY_SPACE_START, read_only_space_size);
3725 verify_space((lispobj*)STATIC_SPACE_START , static_space_size);
3729 verify_generation(generation_index_t generation)
3733 for (i = 0; i < last_free_page; i++) {
3734 if ((page_table[i].allocated != FREE_PAGE_FLAG)
3735 && (page_table[i].bytes_used != 0)
3736 && (page_table[i].gen == generation)) {
3737 page_index_t last_page;
3738 int region_allocation = page_table[i].allocated;
3740 /* This should be the start of a contiguous block */
3741 gc_assert(page_table[i].first_object_offset == 0);
3743 /* Need to find the full extent of this contiguous block in case
3744 objects span pages. */
3746 /* Now work forward until the end of this contiguous area is
3748 for (last_page = i; ;last_page++)
3749 /* Check whether this is the last page in this contiguous
3751 if ((page_table[last_page].bytes_used < PAGE_BYTES)
3752 /* Or it is PAGE_BYTES and is the last in the block */
3753 || (page_table[last_page+1].allocated != region_allocation)
3754 || (page_table[last_page+1].bytes_used == 0)
3755 || (page_table[last_page+1].gen != generation)
3756 || (page_table[last_page+1].first_object_offset == 0))
3759 verify_space(page_address(i), (page_table[last_page].bytes_used
3760 + (last_page-i)*PAGE_BYTES)/N_WORD_BYTES);
3766 /* Check that all the free space is zero filled. */
3768 verify_zero_fill(void)
3772 for (page = 0; page < last_free_page; page++) {
3773 if (page_table[page].allocated == FREE_PAGE_FLAG) {
3774 /* The whole page should be zero filled. */
3775 long *start_addr = (long *)page_address(page);
3778 for (i = 0; i < size; i++) {
3779 if (start_addr[i] != 0) {
3780 lose("free page not zero at %x\n", start_addr + i);
3784 long free_bytes = PAGE_BYTES - page_table[page].bytes_used;
3785 if (free_bytes > 0) {
3786 long *start_addr = (long *)((unsigned long)page_address(page)
3787 + page_table[page].bytes_used);
3788 long size = free_bytes / N_WORD_BYTES;
3790 for (i = 0; i < size; i++) {
3791 if (start_addr[i] != 0) {
3792 lose("free region not zero at %x\n", start_addr + i);
3800 /* External entry point for verify_zero_fill */
3802 gencgc_verify_zero_fill(void)
3804 /* Flush the alloc regions updating the tables. */
3805 gc_alloc_update_all_page_tables();
3806 SHOW("verifying zero fill");
3811 verify_dynamic_space(void)
3813 generation_index_t i;
3815 for (i = 0; i <= HIGHEST_NORMAL_GENERATION; i++)
3816 verify_generation(i);
3818 if (gencgc_enable_verify_zero_fill)
3822 /* Write-protect all the dynamic boxed pages in the given generation. */
3824 write_protect_generation_pages(generation_index_t generation)
3828 gc_assert(generation < SCRATCH_GENERATION);
3830 for (start = 0; start < last_free_page; start++) {
3831 if ((page_table[start].allocated == BOXED_PAGE_FLAG)
3832 && (page_table[start].bytes_used != 0)
3833 && !page_table[start].dont_move
3834 && (page_table[start].gen == generation)) {
3838 /* Note the page as protected in the page tables. */
3839 page_table[start].write_protected = 1;
3841 for (last = start + 1; last < last_free_page; last++) {
3842 if ((page_table[last].allocated != BOXED_PAGE_FLAG)
3843 || (page_table[last].bytes_used == 0)
3844 || page_table[last].dont_move
3845 || (page_table[last].gen != generation))
3847 page_table[last].write_protected = 1;
3850 page_start = (void *)page_address(start);
3852 os_protect(page_start,
3853 PAGE_BYTES * (last - start),
3854 OS_VM_PROT_READ | OS_VM_PROT_EXECUTE);
3860 if (gencgc_verbose > 1) {
3862 "/write protected %d of %d pages in generation %d\n",
3863 count_write_protect_generation_pages(generation),
3864 count_generation_pages(generation),
3870 scavenge_control_stack()
3872 unsigned long control_stack_size;
3874 /* This is going to be a big problem when we try to port threads
3876 struct thread *th = arch_os_get_current_thread();
3877 lispobj *control_stack =
3878 (lispobj *)(th->control_stack_start);
3880 control_stack_size = current_control_stack_pointer - control_stack;
3881 scavenge(control_stack, control_stack_size);
3884 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
3885 /* Scavenging Interrupt Contexts */
3887 static int boxed_registers[] = BOXED_REGISTERS;
3890 scavenge_interrupt_context(os_context_t * context)
3896 unsigned long lip_offset;
3897 int lip_register_pair;
3899 unsigned long pc_code_offset;
3901 #ifdef ARCH_HAS_LINK_REGISTER
3902 unsigned long lr_code_offset;
3904 #ifdef ARCH_HAS_NPC_REGISTER
3905 unsigned long npc_code_offset;
3909 /* Find the LIP's register pair and calculate it's offset */
3910 /* before we scavenge the context. */
3913 * I (RLT) think this is trying to find the boxed register that is
3914 * closest to the LIP address, without going past it. Usually, it's
3915 * reg_CODE or reg_LRA. But sometimes, nothing can be found.
3917 lip = *os_context_register_addr(context, reg_LIP);
3918 lip_offset = 0x7FFFFFFF;
3919 lip_register_pair = -1;
3920 for (i = 0; i < (sizeof(boxed_registers) / sizeof(int)); i++) {
3925 index = boxed_registers[i];
3926 reg = *os_context_register_addr(context, index);
3927 if ((reg & ~((1L<<N_LOWTAG_BITS)-1)) <= lip) {
3929 if (offset < lip_offset) {
3930 lip_offset = offset;
3931 lip_register_pair = index;
3935 #endif /* reg_LIP */
3937 /* Compute the PC's offset from the start of the CODE */
3939 pc_code_offset = *os_context_pc_addr(context) - *os_context_register_addr(context, reg_CODE);
3940 #ifdef ARCH_HAS_NPC_REGISTER
3941 npc_code_offset = *os_context_npc_addr(context) - *os_context_register_addr(context, reg_CODE);
3942 #endif /* ARCH_HAS_NPC_REGISTER */
3944 #ifdef ARCH_HAS_LINK_REGISTER
3946 *os_context_lr_addr(context) -
3947 *os_context_register_addr(context, reg_CODE);
3950 /* Scanvenge all boxed registers in the context. */
3951 for (i = 0; i < (sizeof(boxed_registers) / sizeof(int)); i++) {
3955 index = boxed_registers[i];
3956 foo = *os_context_register_addr(context, index);
3958 *os_context_register_addr(context, index) = foo;
3960 scavenge((lispobj*) &(*os_context_register_addr(context, index)), 1);
3967 * But what happens if lip_register_pair is -1? *os_context_register_addr on Solaris
3968 * (see solaris_register_address in solaris-os.c) will return
3969 * &context->uc_mcontext.gregs[2]. But gregs[2] is REG_nPC. Is
3970 * that what we really want? My guess is that that is not what we
3971 * want, so if lip_register_pair is -1, we don't touch reg_LIP at
3972 * all. But maybe it doesn't really matter if LIP is trashed?
3974 if (lip_register_pair >= 0) {
3975 *os_context_register_addr(context, reg_LIP) =
3976 *os_context_register_addr(context, lip_register_pair) + lip_offset;
3978 #endif /* reg_LIP */
3980 /* Fix the PC if it was in from space */
3981 if (from_space_p(*os_context_pc_addr(context)))
3982 *os_context_pc_addr(context) = *os_context_register_addr(context, reg_CODE) + pc_code_offset;
3984 #ifdef ARCH_HAS_LINK_REGISTER
3985 /* Fix the LR ditto; important if we're being called from
3986 * an assembly routine that expects to return using blr, otherwise
3988 if (from_space_p(*os_context_lr_addr(context)))
3989 *os_context_lr_addr(context) =
3990 *os_context_register_addr(context, reg_CODE) + lr_code_offset;
3993 #ifdef ARCH_HAS_NPC_REGISTER
3994 if (from_space_p(*os_context_npc_addr(context)))
3995 *os_context_npc_addr(context) = *os_context_register_addr(context, reg_CODE) + npc_code_offset;
3996 #endif /* ARCH_HAS_NPC_REGISTER */
4000 scavenge_interrupt_contexts(void)
4003 os_context_t *context;
4005 struct thread *th=arch_os_get_current_thread();
4007 index = fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,0));
4009 #if defined(DEBUG_PRINT_CONTEXT_INDEX)
4010 printf("Number of active contexts: %d\n", index);
4013 for (i = 0; i < index; i++) {
4014 context = th->interrupt_contexts[i];
4015 scavenge_interrupt_context(context);
4022 preserve_context_registers (os_context_t *c)
4025 /* On Darwin the signal context isn't a contiguous block of memory,
4026 * so just preserve_pointering its contents won't be sufficient.
4028 #if defined(LISP_FEATURE_DARWIN)
4029 #if defined LISP_FEATURE_X86
4030 preserve_pointer((void*)*os_context_register_addr(c,reg_EAX));
4031 preserve_pointer((void*)*os_context_register_addr(c,reg_ECX));
4032 preserve_pointer((void*)*os_context_register_addr(c,reg_EDX));
4033 preserve_pointer((void*)*os_context_register_addr(c,reg_EBX));
4034 preserve_pointer((void*)*os_context_register_addr(c,reg_ESI));
4035 preserve_pointer((void*)*os_context_register_addr(c,reg_EDI));
4036 preserve_pointer((void*)*os_context_pc_addr(c));
4038 #error "preserve_context_registers needs to be tweaked for non-x86 Darwin"
4041 for(ptr = (void **)(c+1); ptr>=(void **)c; ptr--) {
4042 preserve_pointer(*ptr);
4046 /* Garbage collect a generation. If raise is 0 then the remains of the
4047 * generation are not raised to the next generation. */
4049 garbage_collect_generation(generation_index_t generation, int raise)
4051 unsigned long bytes_freed;
4053 unsigned long static_space_size;
4055 gc_assert(generation <= HIGHEST_NORMAL_GENERATION);
4057 /* The oldest generation can't be raised. */
4058 gc_assert((generation != HIGHEST_NORMAL_GENERATION) || (raise == 0));
4060 /* Initialize the weak pointer list. */
4061 weak_pointers = NULL;
4063 #ifdef LUTEX_WIDETAG
4064 unmark_lutexes(generation);
4067 /* When a generation is not being raised it is transported to a
4068 * temporary generation (NUM_GENERATIONS), and lowered when
4069 * done. Set up this new generation. There should be no pages
4070 * allocated to it yet. */
4072 gc_assert(generations[SCRATCH_GENERATION].bytes_allocated == 0);
4075 /* Set the global src and dest. generations */
4076 from_space = generation;
4078 new_space = generation+1;
4080 new_space = SCRATCH_GENERATION;
4082 /* Change to a new space for allocation, resetting the alloc_start_page */
4083 gc_alloc_generation = new_space;
4084 generations[new_space].alloc_start_page = 0;
4085 generations[new_space].alloc_unboxed_start_page = 0;
4086 generations[new_space].alloc_large_start_page = 0;
4087 generations[new_space].alloc_large_unboxed_start_page = 0;
4089 /* Before any pointers are preserved, the dont_move flags on the
4090 * pages need to be cleared. */
4091 for (i = 0; i < last_free_page; i++)
4092 if(page_table[i].gen==from_space)
4093 page_table[i].dont_move = 0;
4095 /* Un-write-protect the old-space pages. This is essential for the
4096 * promoted pages as they may contain pointers into the old-space
4097 * which need to be scavenged. It also helps avoid unnecessary page
4098 * faults as forwarding pointers are written into them. They need to
4099 * be un-protected anyway before unmapping later. */
4100 unprotect_oldspace();
4102 /* Scavenge the stacks' conservative roots. */
4104 /* there are potentially two stacks for each thread: the main
4105 * stack, which may contain Lisp pointers, and the alternate stack.
4106 * We don't ever run Lisp code on the altstack, but it may
4107 * host a sigcontext with lisp objects in it */
4109 /* what we need to do: (1) find the stack pointer for the main
4110 * stack; scavenge it (2) find the interrupt context on the
4111 * alternate stack that might contain lisp values, and scavenge
4114 /* we assume that none of the preceding applies to the thread that
4115 * initiates GC. If you ever call GC from inside an altstack
4116 * handler, you will lose. */
4118 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
4119 /* And if we're saving a core, there's no point in being conservative. */
4120 if (conservative_stack) {
4121 for_each_thread(th) {
4123 void **esp=(void **)-1;
4124 #ifdef LISP_FEATURE_SB_THREAD
4126 if(th==arch_os_get_current_thread()) {
4127 /* Somebody is going to burn in hell for this, but casting
4128 * it in two steps shuts gcc up about strict aliasing. */
4129 esp = (void **)((void *)&raise);
4132 free=fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,th));
4133 for(i=free-1;i>=0;i--) {
4134 os_context_t *c=th->interrupt_contexts[i];
4135 esp1 = (void **) *os_context_register_addr(c,reg_SP);
4136 if (esp1>=(void **)th->control_stack_start &&
4137 esp1<(void **)th->control_stack_end) {
4138 if(esp1<esp) esp=esp1;
4139 preserve_context_registers(c);
4144 esp = (void **)((void *)&raise);
4146 for (ptr = (void **)th->control_stack_end; ptr > esp; ptr--) {
4147 preserve_pointer(*ptr);
4154 if (gencgc_verbose > 1) {
4155 long num_dont_move_pages = count_dont_move_pages();
4157 "/non-movable pages due to conservative pointers = %d (%d bytes)\n",
4158 num_dont_move_pages,
4159 num_dont_move_pages * PAGE_BYTES);
4163 /* Scavenge all the rest of the roots. */
4165 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
4167 * If not x86, we need to scavenge the interrupt context(s) and the
4170 scavenge_interrupt_contexts();
4171 scavenge_control_stack();
4174 /* Scavenge the Lisp functions of the interrupt handlers, taking
4175 * care to avoid SIG_DFL and SIG_IGN. */
4176 for (i = 0; i < NSIG; i++) {
4177 union interrupt_handler handler = interrupt_handlers[i];
4178 if (!ARE_SAME_HANDLER(handler.c, SIG_IGN) &&
4179 !ARE_SAME_HANDLER(handler.c, SIG_DFL)) {
4180 scavenge((lispobj *)(interrupt_handlers + i), 1);
4183 /* Scavenge the binding stacks. */
4186 for_each_thread(th) {
4187 long len= (lispobj *)get_binding_stack_pointer(th) -
4188 th->binding_stack_start;
4189 scavenge((lispobj *) th->binding_stack_start,len);
4190 #ifdef LISP_FEATURE_SB_THREAD
4191 /* do the tls as well */
4192 len=fixnum_value(SymbolValue(FREE_TLS_INDEX,0)) -
4193 (sizeof (struct thread))/(sizeof (lispobj));
4194 scavenge((lispobj *) (th+1),len);
4199 /* The original CMU CL code had scavenge-read-only-space code
4200 * controlled by the Lisp-level variable
4201 * *SCAVENGE-READ-ONLY-SPACE*. It was disabled by default, and it
4202 * wasn't documented under what circumstances it was useful or
4203 * safe to turn it on, so it's been turned off in SBCL. If you
4204 * want/need this functionality, and can test and document it,
4205 * please submit a patch. */
4207 if (SymbolValue(SCAVENGE_READ_ONLY_SPACE) != NIL) {
4208 unsigned long read_only_space_size =
4209 (lispobj*)SymbolValue(READ_ONLY_SPACE_FREE_POINTER) -
4210 (lispobj*)READ_ONLY_SPACE_START;
4212 "/scavenge read only space: %d bytes\n",
4213 read_only_space_size * sizeof(lispobj)));
4214 scavenge( (lispobj *) READ_ONLY_SPACE_START, read_only_space_size);
4218 /* Scavenge static space. */
4220 (lispobj *)SymbolValue(STATIC_SPACE_FREE_POINTER,0) -
4221 (lispobj *)STATIC_SPACE_START;
4222 if (gencgc_verbose > 1) {
4224 "/scavenge static space: %d bytes\n",
4225 static_space_size * sizeof(lispobj)));
4227 scavenge( (lispobj *) STATIC_SPACE_START, static_space_size);
4229 /* All generations but the generation being GCed need to be
4230 * scavenged. The new_space generation needs special handling as
4231 * objects may be moved in - it is handled separately below. */
4232 scavenge_generations(generation+1, PSEUDO_STATIC_GENERATION);
4234 /* Finally scavenge the new_space generation. Keep going until no
4235 * more objects are moved into the new generation */
4236 scavenge_newspace_generation(new_space);
4238 /* FIXME: I tried reenabling this check when debugging unrelated
4239 * GC weirdness ca. sbcl-0.6.12.45, and it failed immediately.
4240 * Since the current GC code seems to work well, I'm guessing that
4241 * this debugging code is just stale, but I haven't tried to
4242 * figure it out. It should be figured out and then either made to
4243 * work or just deleted. */
4244 #define RESCAN_CHECK 0
4246 /* As a check re-scavenge the newspace once; no new objects should
4249 long old_bytes_allocated = bytes_allocated;
4250 long bytes_allocated;
4252 /* Start with a full scavenge. */
4253 scavenge_newspace_generation_one_scan(new_space);
4255 /* Flush the current regions, updating the tables. */
4256 gc_alloc_update_all_page_tables();
4258 bytes_allocated = bytes_allocated - old_bytes_allocated;
4260 if (bytes_allocated != 0) {
4261 lose("Rescan of new_space allocated %d more bytes.\n",
4267 scan_weak_pointers();
4269 /* Flush the current regions, updating the tables. */
4270 gc_alloc_update_all_page_tables();
4272 /* Free the pages in oldspace, but not those marked dont_move. */
4273 bytes_freed = free_oldspace();
4275 /* If the GC is not raising the age then lower the generation back
4276 * to its normal generation number */
4278 for (i = 0; i < last_free_page; i++)
4279 if ((page_table[i].bytes_used != 0)
4280 && (page_table[i].gen == SCRATCH_GENERATION))
4281 page_table[i].gen = generation;
4282 gc_assert(generations[generation].bytes_allocated == 0);
4283 generations[generation].bytes_allocated =
4284 generations[SCRATCH_GENERATION].bytes_allocated;
4285 generations[SCRATCH_GENERATION].bytes_allocated = 0;
4288 /* Reset the alloc_start_page for generation. */
4289 generations[generation].alloc_start_page = 0;
4290 generations[generation].alloc_unboxed_start_page = 0;
4291 generations[generation].alloc_large_start_page = 0;
4292 generations[generation].alloc_large_unboxed_start_page = 0;
4294 if (generation >= verify_gens) {
4298 verify_dynamic_space();
4301 /* Set the new gc trigger for the GCed generation. */
4302 generations[generation].gc_trigger =
4303 generations[generation].bytes_allocated
4304 + generations[generation].bytes_consed_between_gc;
4307 generations[generation].num_gc = 0;
4309 ++generations[generation].num_gc;
4311 #ifdef LUTEX_WIDETAG
4312 reap_lutexes(generation);
4314 move_lutexes(generation, generation+1);
4318 /* Update last_free_page, then SymbolValue(ALLOCATION_POINTER). */
4320 update_dynamic_space_free_pointer(void)
4322 page_index_t last_page = -1, i;
4324 for (i = 0; i < last_free_page; i++)
4325 if ((page_table[i].allocated != FREE_PAGE_FLAG)
4326 && (page_table[i].bytes_used != 0))
4329 last_free_page = last_page+1;
4331 set_alloc_pointer((lispobj)(((char *)heap_base) + last_free_page*PAGE_BYTES));
4332 return 0; /* dummy value: return something ... */
4336 remap_free_pages (page_index_t from, page_index_t to)
4338 page_index_t first_page, last_page;
4340 for (first_page = from; first_page <= to; first_page++) {
4341 if (page_table[first_page].allocated != FREE_PAGE_FLAG ||
4342 page_table[first_page].need_to_zero == 0) {
4346 last_page = first_page + 1;
4347 while (page_table[last_page].allocated == FREE_PAGE_FLAG &&
4349 page_table[last_page].need_to_zero == 1) {
4353 /* There's a mysterious Solaris/x86 problem with using mmap
4354 * tricks for memory zeroing. See sbcl-devel thread
4355 * "Re: patch: standalone executable redux".
4357 #if defined(LISP_FEATURE_SUNOS)
4358 zero_pages(first_page, last_page-1);
4360 zero_pages_with_mmap(first_page, last_page-1);
4363 first_page = last_page;
4367 generation_index_t small_generation_limit = 1;
4369 /* GC all generations newer than last_gen, raising the objects in each
4370 * to the next older generation - we finish when all generations below
4371 * last_gen are empty. Then if last_gen is due for a GC, or if
4372 * last_gen==NUM_GENERATIONS (the scratch generation? eh?) we GC that
4373 * too. The valid range for last_gen is: 0,1,...,NUM_GENERATIONS.
4375 * We stop collecting at gencgc_oldest_gen_to_gc, even if this is less than
4376 * last_gen (oh, and note that by default it is NUM_GENERATIONS-1) */
4378 collect_garbage(generation_index_t last_gen)
4380 generation_index_t gen = 0, i;
4383 /* The largest value of last_free_page seen since the time
4384 * remap_free_pages was called. */
4385 static page_index_t high_water_mark = 0;
4387 FSHOW((stderr, "/entering collect_garbage(%d)\n", last_gen));
4389 if (last_gen > HIGHEST_NORMAL_GENERATION+1) {
4391 "/collect_garbage: last_gen = %d, doing a level 0 GC\n",
4396 /* Flush the alloc regions updating the tables. */
4397 gc_alloc_update_all_page_tables();
4399 /* Verify the new objects created by Lisp code. */
4400 if (pre_verify_gen_0) {
4401 FSHOW((stderr, "pre-checking generation 0\n"));
4402 verify_generation(0);
4405 if (gencgc_verbose > 1)
4406 print_generation_stats(0);
4409 /* Collect the generation. */
4411 if (gen >= gencgc_oldest_gen_to_gc) {
4412 /* Never raise the oldest generation. */
4417 || (generations[gen].num_gc >= generations[gen].trigger_age);
4420 if (gencgc_verbose > 1) {
4422 "starting GC of generation %d with raise=%d alloc=%d trig=%d GCs=%d\n",
4425 generations[gen].bytes_allocated,
4426 generations[gen].gc_trigger,
4427 generations[gen].num_gc));
4430 /* If an older generation is being filled, then update its
4433 generations[gen+1].cum_sum_bytes_allocated +=
4434 generations[gen+1].bytes_allocated;
4437 garbage_collect_generation(gen, raise);
4439 /* Reset the memory age cum_sum. */
4440 generations[gen].cum_sum_bytes_allocated = 0;
4442 if (gencgc_verbose > 1) {
4443 FSHOW((stderr, "GC of generation %d finished:\n", gen));
4444 print_generation_stats(0);
4448 } while ((gen <= gencgc_oldest_gen_to_gc)
4449 && ((gen < last_gen)
4450 || ((gen <= gencgc_oldest_gen_to_gc)
4452 && (generations[gen].bytes_allocated
4453 > generations[gen].gc_trigger)
4454 && (gen_av_mem_age(gen)
4455 > generations[gen].min_av_mem_age))));
4457 /* Now if gen-1 was raised all generations before gen are empty.
4458 * If it wasn't raised then all generations before gen-1 are empty.
4460 * Now objects within this gen's pages cannot point to younger
4461 * generations unless they are written to. This can be exploited
4462 * by write-protecting the pages of gen; then when younger
4463 * generations are GCed only the pages which have been written
4468 gen_to_wp = gen - 1;
4470 /* There's not much point in WPing pages in generation 0 as it is
4471 * never scavenged (except promoted pages). */
4472 if ((gen_to_wp > 0) && enable_page_protection) {
4473 /* Check that they are all empty. */
4474 for (i = 0; i < gen_to_wp; i++) {
4475 if (generations[i].bytes_allocated)
4476 lose("trying to write-protect gen. %d when gen. %d nonempty\n",
4479 write_protect_generation_pages(gen_to_wp);
4482 /* Set gc_alloc() back to generation 0. The current regions should
4483 * be flushed after the above GCs. */
4484 gc_assert((boxed_region.free_pointer - boxed_region.start_addr) == 0);
4485 gc_alloc_generation = 0;
4487 /* Save the high-water mark before updating last_free_page */
4488 if (last_free_page > high_water_mark)
4489 high_water_mark = last_free_page;
4491 update_dynamic_space_free_pointer();
4493 auto_gc_trigger = bytes_allocated + bytes_consed_between_gcs;
4495 fprintf(stderr,"Next gc when %ld bytes have been consed\n",
4498 /* If we did a big GC (arbitrarily defined as gen > 1), release memory
4501 if (gen > small_generation_limit) {
4502 if (last_free_page > high_water_mark)
4503 high_water_mark = last_free_page;
4504 remap_free_pages(0, high_water_mark);
4505 high_water_mark = 0;
4508 SHOW("returning from collect_garbage");
4511 /* This is called by Lisp PURIFY when it is finished. All live objects
4512 * will have been moved to the RO and Static heaps. The dynamic space
4513 * will need a full re-initialization. We don't bother having Lisp
4514 * PURIFY flush the current gc_alloc() region, as the page_tables are
4515 * re-initialized, and every page is zeroed to be sure. */
4521 if (gencgc_verbose > 1)
4522 SHOW("entering gc_free_heap");
4524 for (page = 0; page < NUM_PAGES; page++) {
4525 /* Skip free pages which should already be zero filled. */
4526 if (page_table[page].allocated != FREE_PAGE_FLAG) {
4527 void *page_start, *addr;
4529 /* Mark the page free. The other slots are assumed invalid
4530 * when it is a FREE_PAGE_FLAG and bytes_used is 0 and it
4531 * should not be write-protected -- except that the
4532 * generation is used for the current region but it sets
4534 page_table[page].allocated = FREE_PAGE_FLAG;
4535 page_table[page].bytes_used = 0;
4537 #ifndef LISP_FEATURE_WIN32 /* Pages already zeroed on win32? Not sure about this change. */
4538 /* Zero the page. */
4539 page_start = (void *)page_address(page);
4541 /* First, remove any write-protection. */
4542 os_protect(page_start, PAGE_BYTES, OS_VM_PROT_ALL);
4543 page_table[page].write_protected = 0;
4545 os_invalidate(page_start,PAGE_BYTES);
4546 addr = os_validate(page_start,PAGE_BYTES);
4547 if (addr == NULL || addr != page_start) {
4548 lose("gc_free_heap: page moved, 0x%08x ==> 0x%08x\n",
4553 page_table[page].write_protected = 0;
4555 } else if (gencgc_zero_check_during_free_heap) {
4556 /* Double-check that the page is zero filled. */
4559 gc_assert(page_table[page].allocated == FREE_PAGE_FLAG);
4560 gc_assert(page_table[page].bytes_used == 0);
4561 page_start = (long *)page_address(page);
4562 for (i=0; i<1024; i++) {
4563 if (page_start[i] != 0) {
4564 lose("free region not zero at %x\n", page_start + i);
4570 bytes_allocated = 0;
4572 /* Initialize the generations. */
4573 for (page = 0; page < NUM_GENERATIONS; page++) {
4574 generations[page].alloc_start_page = 0;
4575 generations[page].alloc_unboxed_start_page = 0;
4576 generations[page].alloc_large_start_page = 0;
4577 generations[page].alloc_large_unboxed_start_page = 0;
4578 generations[page].bytes_allocated = 0;
4579 generations[page].gc_trigger = 2000000;
4580 generations[page].num_gc = 0;
4581 generations[page].cum_sum_bytes_allocated = 0;
4582 generations[page].lutexes = NULL;
4585 if (gencgc_verbose > 1)
4586 print_generation_stats(0);
4588 /* Initialize gc_alloc(). */
4589 gc_alloc_generation = 0;
4591 gc_set_region_empty(&boxed_region);
4592 gc_set_region_empty(&unboxed_region);
4595 set_alloc_pointer((lispobj)((char *)heap_base));
4597 if (verify_after_free_heap) {
4598 /* Check whether purify has left any bad pointers. */
4600 SHOW("checking after free_heap\n");
4611 scavtab[SIMPLE_VECTOR_WIDETAG] = scav_vector;
4612 scavtab[WEAK_POINTER_WIDETAG] = scav_weak_pointer;
4613 transother[SIMPLE_ARRAY_WIDETAG] = trans_boxed_large;
4615 #ifdef LUTEX_WIDETAG
4616 scavtab[LUTEX_WIDETAG] = scav_lutex;
4617 transother[LUTEX_WIDETAG] = trans_lutex;
4618 sizetab[LUTEX_WIDETAG] = size_lutex;
4621 heap_base = (void*)DYNAMIC_SPACE_START;
4623 /* Initialize each page structure. */
4624 for (i = 0; i < NUM_PAGES; i++) {
4625 /* Initialize all pages as free. */
4626 page_table[i].allocated = FREE_PAGE_FLAG;
4627 page_table[i].bytes_used = 0;
4629 /* Pages are not write-protected at startup. */
4630 page_table[i].write_protected = 0;
4633 bytes_allocated = 0;
4635 /* Initialize the generations.
4637 * FIXME: very similar to code in gc_free_heap(), should be shared */
4638 for (i = 0; i < NUM_GENERATIONS; i++) {
4639 generations[i].alloc_start_page = 0;
4640 generations[i].alloc_unboxed_start_page = 0;
4641 generations[i].alloc_large_start_page = 0;
4642 generations[i].alloc_large_unboxed_start_page = 0;
4643 generations[i].bytes_allocated = 0;
4644 generations[i].gc_trigger = 2000000;
4645 generations[i].num_gc = 0;
4646 generations[i].cum_sum_bytes_allocated = 0;
4647 /* the tune-able parameters */
4648 generations[i].bytes_consed_between_gc = 2000000;
4649 generations[i].trigger_age = 1;
4650 generations[i].min_av_mem_age = 0.75;
4651 generations[i].lutexes = NULL;
4654 /* Initialize gc_alloc. */
4655 gc_alloc_generation = 0;
4656 gc_set_region_empty(&boxed_region);
4657 gc_set_region_empty(&unboxed_region);
4662 /* Pick up the dynamic space from after a core load.
4664 * The ALLOCATION_POINTER points to the end of the dynamic space.
4668 gencgc_pickup_dynamic(void)
4670 page_index_t page = 0;
4671 long alloc_ptr = get_alloc_pointer();
4672 lispobj *prev=(lispobj *)page_address(page);
4673 generation_index_t gen = PSEUDO_STATIC_GENERATION;
4676 lispobj *first,*ptr= (lispobj *)page_address(page);
4677 page_table[page].allocated = BOXED_PAGE_FLAG;
4678 page_table[page].gen = gen;
4679 page_table[page].bytes_used = PAGE_BYTES;
4680 page_table[page].large_object = 0;
4681 page_table[page].write_protected = 0;
4682 page_table[page].write_protected_cleared = 0;
4683 page_table[page].dont_move = 0;
4684 page_table[page].need_to_zero = 1;
4686 if (!gencgc_partial_pickup) {
4687 first=gc_search_space(prev,(ptr+2)-prev,ptr);
4688 if(ptr == first) prev=ptr;
4689 page_table[page].first_object_offset =
4690 (void *)prev - page_address(page);
4693 } while ((long)page_address(page) < alloc_ptr);
4695 #ifdef LUTEX_WIDETAG
4696 /* Lutexes have been registered in generation 0 by coreparse, and
4697 * need to be moved to the right one manually.
4699 move_lutexes(0, PSEUDO_STATIC_GENERATION);
4702 last_free_page = page;
4704 generations[gen].bytes_allocated = PAGE_BYTES*page;
4705 bytes_allocated = PAGE_BYTES*page;
4707 gc_alloc_update_all_page_tables();
4708 write_protect_generation_pages(gen);
4712 gc_initialize_pointers(void)
4714 gencgc_pickup_dynamic();
4720 /* alloc(..) is the external interface for memory allocation. It
4721 * allocates to generation 0. It is not called from within the garbage
4722 * collector as it is only external uses that need the check for heap
4723 * size (GC trigger) and to disable the interrupts (interrupts are
4724 * always disabled during a GC).
4726 * The vops that call alloc(..) assume that the returned space is zero-filled.
4727 * (E.g. the most significant word of a 2-word bignum in MOVE-FROM-UNSIGNED.)
4729 * The check for a GC trigger is only performed when the current
4730 * region is full, so in most cases it's not needed. */
4735 struct thread *thread=arch_os_get_current_thread();
4736 struct alloc_region *region=
4737 #ifdef LISP_FEATURE_SB_THREAD
4738 thread ? &(thread->alloc_region) : &boxed_region;
4743 void *new_free_pointer;
4744 gc_assert(nbytes>0);
4746 /* Check for alignment allocation problems. */
4747 gc_assert((((unsigned long)region->free_pointer & LOWTAG_MASK) == 0)
4748 && ((nbytes & LOWTAG_MASK) == 0));
4752 /* there are a few places in the C code that allocate data in the
4753 * heap before Lisp starts. This is before interrupts are enabled,
4754 * so we don't need to check for pseudo-atomic */
4755 #ifdef LISP_FEATURE_SB_THREAD
4756 if(!get_psuedo_atomic_atomic(th)) {
4758 fprintf(stderr, "fatal error in thread 0x%x, tid=%ld\n",
4760 __asm__("movl %fs,%0" : "=r" (fs) : );
4761 fprintf(stderr, "fs is %x, th->tls_cookie=%x \n",
4762 debug_get_fs(),th->tls_cookie);
4763 lose("If you see this message before 2004.01.31, mail details to sbcl-devel\n");
4766 gc_assert(get_pseudo_atomic_atomic(th));
4770 /* maybe we can do this quickly ... */
4771 new_free_pointer = region->free_pointer + nbytes;
4772 if (new_free_pointer <= region->end_addr) {
4773 new_obj = (void*)(region->free_pointer);
4774 region->free_pointer = new_free_pointer;
4775 return(new_obj); /* yup */
4778 /* we have to go the long way around, it seems. Check whether
4779 * we should GC in the near future
4781 if (auto_gc_trigger && bytes_allocated > auto_gc_trigger) {
4782 gc_assert(get_pseudo_atomic_atomic(thread));
4783 /* Don't flood the system with interrupts if the need to gc is
4784 * already noted. This can happen for example when SUB-GC
4785 * allocates or after a gc triggered in a WITHOUT-GCING. */
4786 if (SymbolValue(GC_PENDING,thread) == NIL) {
4787 /* set things up so that GC happens when we finish the PA
4789 SetSymbolValue(GC_PENDING,T,thread);
4790 if (SymbolValue(GC_INHIBIT,thread) == NIL)
4791 set_pseudo_atomic_interrupted(thread);
4794 new_obj = gc_alloc_with_region(nbytes,0,region,0);
4799 * shared support for the OS-dependent signal handlers which
4800 * catch GENCGC-related write-protect violations
4803 void unhandled_sigmemoryfault(void);
4805 /* Depending on which OS we're running under, different signals might
4806 * be raised for a violation of write protection in the heap. This
4807 * function factors out the common generational GC magic which needs
4808 * to invoked in this case, and should be called from whatever signal
4809 * handler is appropriate for the OS we're running under.
4811 * Return true if this signal is a normal generational GC thing that
4812 * we were able to handle, or false if it was abnormal and control
4813 * should fall through to the general SIGSEGV/SIGBUS/whatever logic. */
4816 gencgc_handle_wp_violation(void* fault_addr)
4818 page_index_t page_index = find_page_index(fault_addr);
4820 #ifdef QSHOW_SIGNALS
4821 FSHOW((stderr, "heap WP violation? fault_addr=%x, page_index=%d\n",
4822 fault_addr, page_index));
4825 /* Check whether the fault is within the dynamic space. */
4826 if (page_index == (-1)) {
4828 /* It can be helpful to be able to put a breakpoint on this
4829 * case to help diagnose low-level problems. */
4830 unhandled_sigmemoryfault();
4832 /* not within the dynamic space -- not our responsibility */
4836 if (page_table[page_index].write_protected) {
4837 /* Unprotect the page. */
4838 os_protect(page_address(page_index), PAGE_BYTES, OS_VM_PROT_ALL);
4839 page_table[page_index].write_protected_cleared = 1;
4840 page_table[page_index].write_protected = 0;
4842 /* The only acceptable reason for this signal on a heap
4843 * access is that GENCGC write-protected the page.
4844 * However, if two CPUs hit a wp page near-simultaneously,
4845 * we had better not have the second one lose here if it
4846 * does this test after the first one has already set wp=0
4848 if(page_table[page_index].write_protected_cleared != 1)
4849 lose("fault in heap page %d not marked as write-protected\nboxed_region.first_page: %d, boxed_region.last_page %d\n",
4850 page_index, boxed_region.first_page, boxed_region.last_page);
4852 /* Don't worry, we can handle it. */
4856 /* This is to be called when we catch a SIGSEGV/SIGBUS, determine that
4857 * it's not just a case of the program hitting the write barrier, and
4858 * are about to let Lisp deal with it. It's basically just a
4859 * convenient place to set a gdb breakpoint. */
4861 unhandled_sigmemoryfault()
4864 void gc_alloc_update_all_page_tables(void)
4866 /* Flush the alloc regions updating the tables. */
4869 gc_alloc_update_page_tables(0, &th->alloc_region);
4870 gc_alloc_update_page_tables(1, &unboxed_region);
4871 gc_alloc_update_page_tables(0, &boxed_region);
4875 gc_set_region_empty(struct alloc_region *region)
4877 region->first_page = 0;
4878 region->last_page = -1;
4879 region->start_addr = page_address(0);
4880 region->free_pointer = page_address(0);
4881 region->end_addr = page_address(0);
4885 zero_all_free_pages()
4889 for (i = 0; i < last_free_page; i++) {
4890 if (page_table[i].allocated == FREE_PAGE_FLAG) {
4891 #ifdef READ_PROTECT_FREE_PAGES
4892 os_protect(page_address(i),
4901 /* Things to do before doing a final GC before saving a core (without
4904 * + Pages in large_object pages aren't moved by the GC, so we need to
4905 * unset that flag from all pages.
4906 * + The pseudo-static generation isn't normally collected, but it seems
4907 * reasonable to collect it at least when saving a core. So move the
4908 * pages to a normal generation.
4911 prepare_for_final_gc ()
4914 for (i = 0; i < last_free_page; i++) {
4915 page_table[i].large_object = 0;
4916 if (page_table[i].gen == PSEUDO_STATIC_GENERATION) {
4917 int used = page_table[i].bytes_used;
4918 page_table[i].gen = HIGHEST_NORMAL_GENERATION;
4919 generations[PSEUDO_STATIC_GENERATION].bytes_allocated -= used;
4920 generations[HIGHEST_NORMAL_GENERATION].bytes_allocated += used;
4926 /* Do a non-conservative GC, and then save a core with the initial
4927 * function being set to the value of the static symbol
4928 * SB!VM:RESTART-LISP-FUNCTION */
4930 gc_and_save(char *filename, int prepend_runtime)
4933 void *runtime_bytes = NULL;
4934 size_t runtime_size;
4936 file = prepare_to_save(filename, prepend_runtime, &runtime_bytes, &runtime_size);
4940 conservative_stack = 0;
4942 /* The filename might come from Lisp, and be moved by the now
4943 * non-conservative GC. */
4944 filename = strdup(filename);
4946 /* Collect twice: once into relatively high memory, and then back
4947 * into low memory. This compacts the retained data into the lower
4948 * pages, minimizing the size of the core file.
4950 prepare_for_final_gc();
4951 gencgc_alloc_start_page = last_free_page;
4952 collect_garbage(HIGHEST_NORMAL_GENERATION+1);
4954 prepare_for_final_gc();
4955 gencgc_alloc_start_page = -1;
4956 collect_garbage(HIGHEST_NORMAL_GENERATION+1);
4958 if (prepend_runtime)
4959 save_runtime_to_filehandle(file, runtime_bytes, runtime_size);
4961 /* The dumper doesn't know that pages need to be zeroed before use. */
4962 zero_all_free_pages();
4963 save_to_filehandle(file, filename, SymbolValue(RESTART_LISP_FUNCTION,0),
4965 /* Oops. Save still managed to fail. Since we've mangled the stack
4966 * beyond hope, there's not much we can do.
4967 * (beyond FUNCALLing RESTART_LISP_FUNCTION, but I suspect that's
4968 * going to be rather unsatisfactory too... */
4969 lose("Attempt to save core after non-conservative GC failed.\n");