2 * GENerational Conservative Garbage Collector for SBCL
6 * This software is part of the SBCL system. See the README file for
9 * This software is derived from the CMU CL system, which was
10 * written at Carnegie Mellon University and released into the
11 * public domain. The software is in the public domain and is
12 * provided with absolutely no warranty. See the COPYING and CREDITS
13 * files for more information.
17 * For a review of garbage collection techniques (e.g. generational
18 * GC) and terminology (e.g. "scavenging") see Paul R. Wilson,
19 * "Uniprocessor Garbage Collection Techniques". As of 20000618, this
20 * had been accepted for _ACM Computing Surveys_ and was available
21 * as a PostScript preprint through
22 * <http://www.cs.utexas.edu/users/oops/papers.html>
24 * <ftp://ftp.cs.utexas.edu/pub/garbage/bigsurv.ps>.
37 #include "interrupt.h"
42 #include "gc-internal.h"
45 #include "genesis/vector.h"
46 #include "genesis/weak-pointer.h"
47 #include "genesis/fdefn.h"
48 #include "genesis/simple-fun.h"
50 #include "genesis/hash-table.h"
51 #include "genesis/instance.h"
52 #include "genesis/layout.h"
54 #if defined(LUTEX_WIDETAG)
55 #include "pthread-lutex.h"
58 /* forward declarations */
59 page_index_t gc_find_freeish_pages(long *restart_page_ptr, long nbytes,
67 /* Generations 0-5 are normal collected generations, 6 is only used as
68 * scratch space by the collector, and should never get collected.
71 HIGHEST_NORMAL_GENERATION = 5,
72 PSEUDO_STATIC_GENERATION,
77 /* Should we use page protection to help avoid the scavenging of pages
78 * that don't have pointers to younger generations? */
79 boolean enable_page_protection = 1;
81 /* the minimum size (in bytes) for a large object*/
82 long large_object_size = 4 * PAGE_BYTES;
89 /* the verbosity level. All non-error messages are disabled at level 0;
90 * and only a few rare messages are printed at level 1. */
92 boolean gencgc_verbose = 1;
94 boolean gencgc_verbose = 0;
97 /* FIXME: At some point enable the various error-checking things below
98 * and see what they say. */
100 /* We hunt for pointers to old-space, when GCing generations >= verify_gen.
101 * Set verify_gens to HIGHEST_NORMAL_GENERATION + 1 to disable this kind of
103 generation_index_t verify_gens = HIGHEST_NORMAL_GENERATION + 1;
105 /* Should we do a pre-scan verify of generation 0 before it's GCed? */
106 boolean pre_verify_gen_0 = 0;
108 /* Should we check for bad pointers after gc_free_heap is called
109 * from Lisp PURIFY? */
110 boolean verify_after_free_heap = 0;
112 /* Should we print a note when code objects are found in the dynamic space
113 * during a heap verify? */
114 boolean verify_dynamic_code_check = 0;
116 /* Should we check code objects for fixup errors after they are transported? */
117 boolean check_code_fixups = 0;
119 /* Should we check that newly allocated regions are zero filled? */
120 boolean gencgc_zero_check = 0;
122 /* Should we check that the free space is zero filled? */
123 boolean gencgc_enable_verify_zero_fill = 0;
125 /* Should we check that free pages are zero filled during gc_free_heap
126 * called after Lisp PURIFY? */
127 boolean gencgc_zero_check_during_free_heap = 0;
129 /* When loading a core, don't do a full scan of the memory for the
130 * memory region boundaries. (Set to true by coreparse.c if the core
131 * contained a pagetable entry).
133 boolean gencgc_partial_pickup = 0;
135 /* If defined, free pages are read-protected to ensure that nothing
139 /* #define READ_PROTECT_FREE_PAGES */
143 * GC structures and variables
146 /* the total bytes allocated. These are seen by Lisp DYNAMIC-USAGE. */
147 unsigned long bytes_allocated = 0;
148 unsigned long auto_gc_trigger = 0;
150 /* the source and destination generations. These are set before a GC starts
152 generation_index_t from_space;
153 generation_index_t new_space;
155 /* Set to 1 when in GC */
156 boolean gc_active_p = 0;
158 /* should the GC be conservative on stack. If false (only right before
159 * saving a core), don't scan the stack / mark pages dont_move. */
160 static boolean conservative_stack = 1;
162 /* An array of page structures is allocated on gc initialization.
163 * This helps quickly map between an address its page structure.
164 * page_table_pages is set from the size of the dynamic space. */
165 page_index_t page_table_pages;
166 struct page *page_table;
168 /* To map addresses to page structures the address of the first page
170 static void *heap_base = NULL;
172 /* Calculate the start address for the given page number. */
174 page_address(page_index_t page_num)
176 return (heap_base + (page_num * PAGE_BYTES));
179 /* Calculate the address where the allocation region associated with
180 * the page starts. */
182 page_region_start(page_index_t page_index)
184 return page_address(page_index)+page_table[page_index].first_object_offset;
187 /* Find the page index within the page_table for the given
188 * address. Return -1 on failure. */
190 find_page_index(void *addr)
192 page_index_t index = addr-heap_base;
195 index = ((unsigned long)index)/PAGE_BYTES;
196 if (index < page_table_pages)
203 /* a structure to hold the state of a generation */
206 /* the first page that gc_alloc() checks on its next call */
207 page_index_t alloc_start_page;
209 /* the first page that gc_alloc_unboxed() checks on its next call */
210 page_index_t alloc_unboxed_start_page;
212 /* the first page that gc_alloc_large (boxed) considers on its next
213 * call. (Although it always allocates after the boxed_region.) */
214 page_index_t alloc_large_start_page;
216 /* the first page that gc_alloc_large (unboxed) considers on its
217 * next call. (Although it always allocates after the
218 * current_unboxed_region.) */
219 page_index_t alloc_large_unboxed_start_page;
221 /* the bytes allocated to this generation */
222 long bytes_allocated;
224 /* the number of bytes at which to trigger a GC */
227 /* to calculate a new level for gc_trigger */
228 long bytes_consed_between_gc;
230 /* the number of GCs since the last raise */
233 /* the average age after which a GC will raise objects to the
237 /* the cumulative sum of the bytes allocated to this generation. It is
238 * cleared after a GC on this generations, and update before new
239 * objects are added from a GC of a younger generation. Dividing by
240 * the bytes_allocated will give the average age of the memory in
241 * this generation since its last GC. */
242 long cum_sum_bytes_allocated;
244 /* a minimum average memory age before a GC will occur helps
245 * prevent a GC when a large number of new live objects have been
246 * added, in which case a GC could be a waste of time */
247 double min_av_mem_age;
249 /* A linked list of lutex structures in this generation, used for
250 * implementing lutex finalization. */
252 struct lutex *lutexes;
258 /* an array of generation structures. There needs to be one more
259 * generation structure than actual generations as the oldest
260 * generation is temporarily raised then lowered. */
261 struct generation generations[NUM_GENERATIONS];
263 /* the oldest generation that is will currently be GCed by default.
264 * Valid values are: 0, 1, ... HIGHEST_NORMAL_GENERATION
266 * The default of HIGHEST_NORMAL_GENERATION enables GC on all generations.
268 * Setting this to 0 effectively disables the generational nature of
269 * the GC. In some applications generational GC may not be useful
270 * because there are no long-lived objects.
272 * An intermediate value could be handy after moving long-lived data
273 * into an older generation so an unnecessary GC of this long-lived
274 * data can be avoided. */
275 generation_index_t gencgc_oldest_gen_to_gc = HIGHEST_NORMAL_GENERATION;
277 /* The maximum free page in the heap is maintained and used to update
278 * ALLOCATION_POINTER which is used by the room function to limit its
279 * search of the heap. XX Gencgc obviously needs to be better
280 * integrated with the Lisp code. */
281 page_index_t last_free_page;
283 /* This lock is to prevent multiple threads from simultaneously
284 * allocating new regions which overlap each other. Note that the
285 * majority of GC is single-threaded, but alloc() may be called from
286 * >1 thread at a time and must be thread-safe. This lock must be
287 * seized before all accesses to generations[] or to parts of
288 * page_table[] that other threads may want to see */
290 #ifdef LISP_FEATURE_SB_THREAD
291 static pthread_mutex_t free_pages_lock = PTHREAD_MUTEX_INITIALIZER;
296 * miscellaneous heap functions
299 /* Count the number of pages which are write-protected within the
300 * given generation. */
302 count_write_protect_generation_pages(generation_index_t generation)
307 for (i = 0; i < last_free_page; i++)
308 if ((page_table[i].allocated != FREE_PAGE_FLAG)
309 && (page_table[i].gen == generation)
310 && (page_table[i].write_protected == 1))
315 /* Count the number of pages within the given generation. */
317 count_generation_pages(generation_index_t generation)
322 for (i = 0; i < last_free_page; i++)
323 if ((page_table[i].allocated != FREE_PAGE_FLAG)
324 && (page_table[i].gen == generation))
331 count_dont_move_pages(void)
335 for (i = 0; i < last_free_page; i++) {
336 if ((page_table[i].allocated != FREE_PAGE_FLAG)
337 && (page_table[i].dont_move != 0)) {
345 /* Work through the pages and add up the number of bytes used for the
346 * given generation. */
348 count_generation_bytes_allocated (generation_index_t gen)
352 for (i = 0; i < last_free_page; i++) {
353 if ((page_table[i].allocated != FREE_PAGE_FLAG)
354 && (page_table[i].gen == gen))
355 result += page_table[i].bytes_used;
360 /* Return the average age of the memory in a generation. */
362 gen_av_mem_age(generation_index_t gen)
364 if (generations[gen].bytes_allocated == 0)
368 ((double)generations[gen].cum_sum_bytes_allocated)
369 / ((double)generations[gen].bytes_allocated);
372 /* The verbose argument controls how much to print: 0 for normal
373 * level of detail; 1 for debugging. */
375 print_generation_stats(int verbose) /* FIXME: should take FILE argument */
377 generation_index_t i, gens;
379 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
380 #define FPU_STATE_SIZE 27
381 int fpu_state[FPU_STATE_SIZE];
382 #elif defined(LISP_FEATURE_PPC)
383 #define FPU_STATE_SIZE 32
384 long long fpu_state[FPU_STATE_SIZE];
387 /* This code uses the FP instructions which may be set up for Lisp
388 * so they need to be saved and reset for C. */
391 /* highest generation to print */
393 gens = SCRATCH_GENERATION;
395 gens = PSEUDO_STATIC_GENERATION;
397 /* Print the heap stats. */
399 " Gen StaPg UbSta LaSta LUbSt Boxed Unboxed LB LUB !move Alloc Waste Trig WP GCs Mem-age\n");
401 for (i = 0; i < gens; i++) {
404 long unboxed_cnt = 0;
405 long large_boxed_cnt = 0;
406 long large_unboxed_cnt = 0;
409 for (j = 0; j < last_free_page; j++)
410 if (page_table[j].gen == i) {
412 /* Count the number of boxed pages within the given
414 if (page_table[j].allocated & BOXED_PAGE_FLAG) {
415 if (page_table[j].large_object)
420 if(page_table[j].dont_move) pinned_cnt++;
421 /* Count the number of unboxed pages within the given
423 if (page_table[j].allocated & UNBOXED_PAGE_FLAG) {
424 if (page_table[j].large_object)
431 gc_assert(generations[i].bytes_allocated
432 == count_generation_bytes_allocated(i));
434 " %1d: %5ld %5ld %5ld %5ld %5ld %5ld %5ld %5ld %5ld %8ld %5ld %8ld %4ld %3d %7.4f\n",
436 generations[i].alloc_start_page,
437 generations[i].alloc_unboxed_start_page,
438 generations[i].alloc_large_start_page,
439 generations[i].alloc_large_unboxed_start_page,
445 generations[i].bytes_allocated,
446 (count_generation_pages(i)*PAGE_BYTES
447 - generations[i].bytes_allocated),
448 generations[i].gc_trigger,
449 count_write_protect_generation_pages(i),
450 generations[i].num_gc,
453 fprintf(stderr," Total bytes allocated=%ld\n", bytes_allocated);
455 fpu_restore(fpu_state);
459 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
460 void fast_bzero(void*, size_t); /* in <arch>-assem.S */
463 /* Zero the pages from START to END (inclusive), but use mmap/munmap instead
464 * if zeroing it ourselves, i.e. in practice give the memory back to the
465 * OS. Generally done after a large GC.
467 void zero_pages_with_mmap(page_index_t start, page_index_t end) {
469 void *addr = (void *) page_address(start), *new_addr;
470 size_t length = PAGE_BYTES*(1+end-start);
475 os_invalidate(addr, length);
476 new_addr = os_validate(addr, length);
477 if (new_addr == NULL || new_addr != addr) {
478 lose("remap_free_pages: page moved, 0x%08x ==> 0x%08x",
482 for (i = start; i <= end; i++) {
483 page_table[i].need_to_zero = 0;
487 /* Zero the pages from START to END (inclusive). Generally done just after
488 * a new region has been allocated.
491 zero_pages(page_index_t start, page_index_t end) {
495 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
496 fast_bzero(page_address(start), PAGE_BYTES*(1+end-start));
498 bzero(page_address(start), PAGE_BYTES*(1+end-start));
503 /* Zero the pages from START to END (inclusive), except for those
504 * pages that are known to already zeroed. Mark all pages in the
505 * ranges as non-zeroed.
508 zero_dirty_pages(page_index_t start, page_index_t end) {
511 for (i = start; i <= end; i++) {
512 if (page_table[i].need_to_zero == 1) {
513 zero_pages(start, end);
518 for (i = start; i <= end; i++) {
519 page_table[i].need_to_zero = 1;
525 * To support quick and inline allocation, regions of memory can be
526 * allocated and then allocated from with just a free pointer and a
527 * check against an end address.
529 * Since objects can be allocated to spaces with different properties
530 * e.g. boxed/unboxed, generation, ages; there may need to be many
531 * allocation regions.
533 * Each allocation region may start within a partly used page. Many
534 * features of memory use are noted on a page wise basis, e.g. the
535 * generation; so if a region starts within an existing allocated page
536 * it must be consistent with this page.
538 * During the scavenging of the newspace, objects will be transported
539 * into an allocation region, and pointers updated to point to this
540 * allocation region. It is possible that these pointers will be
541 * scavenged again before the allocation region is closed, e.g. due to
542 * trans_list which jumps all over the place to cleanup the list. It
543 * is important to be able to determine properties of all objects
544 * pointed to when scavenging, e.g to detect pointers to the oldspace.
545 * Thus it's important that the allocation regions have the correct
546 * properties set when allocated, and not just set when closed. The
547 * region allocation routines return regions with the specified
548 * properties, and grab all the pages, setting their properties
549 * appropriately, except that the amount used is not known.
551 * These regions are used to support quicker allocation using just a
552 * free pointer. The actual space used by the region is not reflected
553 * in the pages tables until it is closed. It can't be scavenged until
556 * When finished with the region it should be closed, which will
557 * update the page tables for the actual space used returning unused
558 * space. Further it may be noted in the new regions which is
559 * necessary when scavenging the newspace.
561 * Large objects may be allocated directly without an allocation
562 * region, the page tables are updated immediately.
564 * Unboxed objects don't contain pointers to other objects and so
565 * don't need scavenging. Further they can't contain pointers to
566 * younger generations so WP is not needed. By allocating pages to
567 * unboxed objects the whole page never needs scavenging or
568 * write-protecting. */
570 /* We are only using two regions at present. Both are for the current
571 * newspace generation. */
572 struct alloc_region boxed_region;
573 struct alloc_region unboxed_region;
575 /* The generation currently being allocated to. */
576 static generation_index_t gc_alloc_generation;
578 /* Find a new region with room for at least the given number of bytes.
580 * It starts looking at the current generation's alloc_start_page. So
581 * may pick up from the previous region if there is enough space. This
582 * keeps the allocation contiguous when scavenging the newspace.
584 * The alloc_region should have been closed by a call to
585 * gc_alloc_update_page_tables(), and will thus be in an empty state.
587 * To assist the scavenging functions write-protected pages are not
588 * used. Free pages should not be write-protected.
590 * It is critical to the conservative GC that the start of regions be
591 * known. To help achieve this only small regions are allocated at a
594 * During scavenging, pointers may be found to within the current
595 * region and the page generation must be set so that pointers to the
596 * from space can be recognized. Therefore the generation of pages in
597 * the region are set to gc_alloc_generation. To prevent another
598 * allocation call using the same pages, all the pages in the region
599 * are allocated, although they will initially be empty.
602 gc_alloc_new_region(long nbytes, int unboxed, struct alloc_region *alloc_region)
604 page_index_t first_page;
605 page_index_t last_page;
612 "/alloc_new_region for %d bytes from gen %d\n",
613 nbytes, gc_alloc_generation));
616 /* Check that the region is in a reset state. */
617 gc_assert((alloc_region->first_page == 0)
618 && (alloc_region->last_page == -1)
619 && (alloc_region->free_pointer == alloc_region->end_addr));
620 ret = thread_mutex_lock(&free_pages_lock);
624 generations[gc_alloc_generation].alloc_unboxed_start_page;
627 generations[gc_alloc_generation].alloc_start_page;
629 last_page=gc_find_freeish_pages(&first_page,nbytes,unboxed);
630 bytes_found=(PAGE_BYTES - page_table[first_page].bytes_used)
631 + PAGE_BYTES*(last_page-first_page);
633 /* Set up the alloc_region. */
634 alloc_region->first_page = first_page;
635 alloc_region->last_page = last_page;
636 alloc_region->start_addr = page_table[first_page].bytes_used
637 + page_address(first_page);
638 alloc_region->free_pointer = alloc_region->start_addr;
639 alloc_region->end_addr = alloc_region->start_addr + bytes_found;
641 /* Set up the pages. */
643 /* The first page may have already been in use. */
644 if (page_table[first_page].bytes_used == 0) {
646 page_table[first_page].allocated = UNBOXED_PAGE_FLAG;
648 page_table[first_page].allocated = BOXED_PAGE_FLAG;
649 page_table[first_page].gen = gc_alloc_generation;
650 page_table[first_page].large_object = 0;
651 page_table[first_page].first_object_offset = 0;
655 gc_assert(page_table[first_page].allocated == UNBOXED_PAGE_FLAG);
657 gc_assert(page_table[first_page].allocated == BOXED_PAGE_FLAG);
658 page_table[first_page].allocated |= OPEN_REGION_PAGE_FLAG;
660 gc_assert(page_table[first_page].gen == gc_alloc_generation);
661 gc_assert(page_table[first_page].large_object == 0);
663 for (i = first_page+1; i <= last_page; i++) {
665 page_table[i].allocated = UNBOXED_PAGE_FLAG;
667 page_table[i].allocated = BOXED_PAGE_FLAG;
668 page_table[i].gen = gc_alloc_generation;
669 page_table[i].large_object = 0;
670 /* This may not be necessary for unboxed regions (think it was
672 page_table[i].first_object_offset =
673 alloc_region->start_addr - page_address(i);
674 page_table[i].allocated |= OPEN_REGION_PAGE_FLAG ;
676 /* Bump up last_free_page. */
677 if (last_page+1 > last_free_page) {
678 last_free_page = last_page+1;
679 /* do we only want to call this on special occasions? like for
681 set_alloc_pointer((lispobj)(((char *)heap_base)
682 + last_free_page*PAGE_BYTES));
684 ret = thread_mutex_unlock(&free_pages_lock);
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);
703 /* we can do this after releasing free_pages_lock */
704 if (gencgc_zero_check) {
706 for (p = (long *)alloc_region->start_addr;
707 p < (long *)alloc_region->end_addr; p++) {
709 /* KLUDGE: It would be nice to use %lx and explicit casts
710 * (long) in code like this, so that it is less likely to
711 * break randomly when running on a machine with different
712 * word sizes. -- WHN 19991129 */
713 lose("The new region at %x is not zero (start=%p, end=%p).\n",
714 p, alloc_region->start_addr, alloc_region->end_addr);
720 /* If the record_new_objects flag is 2 then all new regions created
723 * If it's 1 then then it is only recorded if the first page of the
724 * current region is <= new_areas_ignore_page. This helps avoid
725 * unnecessary recording when doing full scavenge pass.
727 * The new_object structure holds the page, byte offset, and size of
728 * new regions of objects. Each new area is placed in the array of
729 * these structures pointer to by new_areas. new_areas_index holds the
730 * offset into new_areas.
732 * If new_area overflows NUM_NEW_AREAS then it stops adding them. The
733 * later code must detect this and handle it, probably by doing a full
734 * scavenge of a generation. */
735 #define NUM_NEW_AREAS 512
736 static int record_new_objects = 0;
737 static page_index_t new_areas_ignore_page;
743 static struct new_area (*new_areas)[];
744 static long new_areas_index;
747 /* Add a new area to new_areas. */
749 add_new_area(page_index_t first_page, long offset, long size)
751 unsigned long new_area_start,c;
754 /* Ignore if full. */
755 if (new_areas_index >= NUM_NEW_AREAS)
758 switch (record_new_objects) {
762 if (first_page > new_areas_ignore_page)
771 new_area_start = PAGE_BYTES*first_page + offset;
773 /* Search backwards for a prior area that this follows from. If
774 found this will save adding a new area. */
775 for (i = new_areas_index-1, c = 0; (i >= 0) && (c < 8); i--, c++) {
776 unsigned long area_end =
777 PAGE_BYTES*((*new_areas)[i].page)
778 + (*new_areas)[i].offset
779 + (*new_areas)[i].size;
781 "/add_new_area S1 %d %d %d %d\n",
782 i, c, new_area_start, area_end));*/
783 if (new_area_start == area_end) {
785 "/adding to [%d] %d %d %d with %d %d %d:\n",
787 (*new_areas)[i].page,
788 (*new_areas)[i].offset,
789 (*new_areas)[i].size,
793 (*new_areas)[i].size += size;
798 (*new_areas)[new_areas_index].page = first_page;
799 (*new_areas)[new_areas_index].offset = offset;
800 (*new_areas)[new_areas_index].size = size;
802 "/new_area %d page %d offset %d size %d\n",
803 new_areas_index, first_page, offset, size));*/
806 /* Note the max new_areas used. */
807 if (new_areas_index > max_new_areas)
808 max_new_areas = new_areas_index;
811 /* Update the tables for the alloc_region. The region may be added to
814 * When done the alloc_region is set up so that the next quick alloc
815 * will fail safely and thus a new region will be allocated. Further
816 * it is safe to try to re-update the page table of this reset
819 gc_alloc_update_page_tables(int unboxed, struct alloc_region *alloc_region)
822 page_index_t first_page;
823 page_index_t next_page;
825 long orig_first_page_bytes_used;
831 first_page = alloc_region->first_page;
833 /* Catch an unused alloc_region. */
834 if ((first_page == 0) && (alloc_region->last_page == -1))
837 next_page = first_page+1;
839 ret = thread_mutex_lock(&free_pages_lock);
841 if (alloc_region->free_pointer != alloc_region->start_addr) {
842 /* some bytes were allocated in the region */
843 orig_first_page_bytes_used = page_table[first_page].bytes_used;
845 gc_assert(alloc_region->start_addr ==
846 (page_address(first_page)
847 + page_table[first_page].bytes_used));
849 /* All the pages used need to be updated */
851 /* Update the first page. */
853 /* If the page was free then set up the gen, and
854 * first_object_offset. */
855 if (page_table[first_page].bytes_used == 0)
856 gc_assert(page_table[first_page].first_object_offset == 0);
857 page_table[first_page].allocated &= ~(OPEN_REGION_PAGE_FLAG);
860 gc_assert(page_table[first_page].allocated == UNBOXED_PAGE_FLAG);
862 gc_assert(page_table[first_page].allocated == BOXED_PAGE_FLAG);
863 gc_assert(page_table[first_page].gen == gc_alloc_generation);
864 gc_assert(page_table[first_page].large_object == 0);
868 /* Calculate the number of bytes used in this page. This is not
869 * always the number of new bytes, unless it was free. */
871 if ((bytes_used = (alloc_region->free_pointer
872 - page_address(first_page)))>PAGE_BYTES) {
873 bytes_used = PAGE_BYTES;
876 page_table[first_page].bytes_used = bytes_used;
877 byte_cnt += bytes_used;
880 /* All the rest of the pages should be free. We need to set their
881 * first_object_offset pointer to the start of the region, and set
884 page_table[next_page].allocated &= ~(OPEN_REGION_PAGE_FLAG);
886 gc_assert(page_table[next_page].allocated==UNBOXED_PAGE_FLAG);
888 gc_assert(page_table[next_page].allocated == BOXED_PAGE_FLAG);
889 gc_assert(page_table[next_page].bytes_used == 0);
890 gc_assert(page_table[next_page].gen == gc_alloc_generation);
891 gc_assert(page_table[next_page].large_object == 0);
893 gc_assert(page_table[next_page].first_object_offset ==
894 alloc_region->start_addr - page_address(next_page));
896 /* Calculate the number of bytes used in this page. */
898 if ((bytes_used = (alloc_region->free_pointer
899 - page_address(next_page)))>PAGE_BYTES) {
900 bytes_used = PAGE_BYTES;
903 page_table[next_page].bytes_used = bytes_used;
904 byte_cnt += bytes_used;
909 region_size = alloc_region->free_pointer - alloc_region->start_addr;
910 bytes_allocated += region_size;
911 generations[gc_alloc_generation].bytes_allocated += region_size;
913 gc_assert((byte_cnt- orig_first_page_bytes_used) == region_size);
915 /* Set the generations alloc restart page to the last page of
918 generations[gc_alloc_generation].alloc_unboxed_start_page =
921 generations[gc_alloc_generation].alloc_start_page = next_page-1;
923 /* Add the region to the new_areas if requested. */
925 add_new_area(first_page,orig_first_page_bytes_used, region_size);
929 "/gc_alloc_update_page_tables update %d bytes to gen %d\n",
931 gc_alloc_generation));
934 /* There are no bytes allocated. Unallocate the first_page if
935 * there are 0 bytes_used. */
936 page_table[first_page].allocated &= ~(OPEN_REGION_PAGE_FLAG);
937 if (page_table[first_page].bytes_used == 0)
938 page_table[first_page].allocated = FREE_PAGE_FLAG;
941 /* Unallocate any unused pages. */
942 while (next_page <= alloc_region->last_page) {
943 gc_assert(page_table[next_page].bytes_used == 0);
944 page_table[next_page].allocated = FREE_PAGE_FLAG;
947 ret = thread_mutex_unlock(&free_pages_lock);
950 /* alloc_region is per-thread, we're ok to do this unlocked */
951 gc_set_region_empty(alloc_region);
954 static inline void *gc_quick_alloc(long nbytes);
956 /* Allocate a possibly large object. */
958 gc_alloc_large(long nbytes, int unboxed, struct alloc_region *alloc_region)
960 page_index_t first_page;
961 page_index_t last_page;
962 int orig_first_page_bytes_used;
966 page_index_t next_page;
969 ret = thread_mutex_lock(&free_pages_lock);
974 generations[gc_alloc_generation].alloc_large_unboxed_start_page;
976 first_page = generations[gc_alloc_generation].alloc_large_start_page;
978 if (first_page <= alloc_region->last_page) {
979 first_page = alloc_region->last_page+1;
982 last_page=gc_find_freeish_pages(&first_page,nbytes,unboxed);
984 gc_assert(first_page > alloc_region->last_page);
986 generations[gc_alloc_generation].alloc_large_unboxed_start_page =
989 generations[gc_alloc_generation].alloc_large_start_page = last_page;
991 /* Set up the pages. */
992 orig_first_page_bytes_used = page_table[first_page].bytes_used;
994 /* If the first page was free then set up the gen, and
995 * first_object_offset. */
996 if (page_table[first_page].bytes_used == 0) {
998 page_table[first_page].allocated = UNBOXED_PAGE_FLAG;
1000 page_table[first_page].allocated = BOXED_PAGE_FLAG;
1001 page_table[first_page].gen = gc_alloc_generation;
1002 page_table[first_page].first_object_offset = 0;
1003 page_table[first_page].large_object = 1;
1007 gc_assert(page_table[first_page].allocated == UNBOXED_PAGE_FLAG);
1009 gc_assert(page_table[first_page].allocated == BOXED_PAGE_FLAG);
1010 gc_assert(page_table[first_page].gen == gc_alloc_generation);
1011 gc_assert(page_table[first_page].large_object == 1);
1015 /* Calc. the number of bytes used in this page. This is not
1016 * always the number of new bytes, unless it was free. */
1018 if ((bytes_used = nbytes+orig_first_page_bytes_used) > PAGE_BYTES) {
1019 bytes_used = PAGE_BYTES;
1022 page_table[first_page].bytes_used = bytes_used;
1023 byte_cnt += bytes_used;
1025 next_page = first_page+1;
1027 /* All the rest of the pages should be free. We need to set their
1028 * first_object_offset pointer to the start of the region, and
1029 * set the bytes_used. */
1031 gc_assert(page_table[next_page].allocated == FREE_PAGE_FLAG);
1032 gc_assert(page_table[next_page].bytes_used == 0);
1034 page_table[next_page].allocated = UNBOXED_PAGE_FLAG;
1036 page_table[next_page].allocated = BOXED_PAGE_FLAG;
1037 page_table[next_page].gen = gc_alloc_generation;
1038 page_table[next_page].large_object = 1;
1040 page_table[next_page].first_object_offset =
1041 orig_first_page_bytes_used - PAGE_BYTES*(next_page-first_page);
1043 /* Calculate the number of bytes used in this page. */
1045 bytes_used=(nbytes+orig_first_page_bytes_used)-byte_cnt;
1046 if (bytes_used > PAGE_BYTES) {
1047 bytes_used = PAGE_BYTES;
1050 page_table[next_page].bytes_used = bytes_used;
1051 page_table[next_page].write_protected=0;
1052 page_table[next_page].dont_move=0;
1053 byte_cnt += bytes_used;
1057 gc_assert((byte_cnt-orig_first_page_bytes_used) == nbytes);
1059 bytes_allocated += nbytes;
1060 generations[gc_alloc_generation].bytes_allocated += nbytes;
1062 /* Add the region to the new_areas if requested. */
1064 add_new_area(first_page,orig_first_page_bytes_used,nbytes);
1066 /* Bump up last_free_page */
1067 if (last_page+1 > last_free_page) {
1068 last_free_page = last_page+1;
1069 set_alloc_pointer((lispobj)(((char *)heap_base)
1070 + last_free_page*PAGE_BYTES));
1072 ret = thread_mutex_unlock(&free_pages_lock);
1073 gc_assert(ret == 0);
1075 #ifdef READ_PROTECT_FREE_PAGES
1076 os_protect(page_address(first_page),
1077 PAGE_BYTES*(1+last_page-first_page),
1081 zero_dirty_pages(first_page, last_page);
1083 return page_address(first_page);
1086 static page_index_t gencgc_alloc_start_page = -1;
1089 gc_heap_exhausted_error_or_lose (long available, long requested)
1091 /* Write basic information before doing anything else: if we don't
1092 * call to lisp this is a must, and even if we do there is always
1093 * the danger that we bounce back here before the error has been
1094 * handled, or indeed even printed.
1096 fprintf(stderr, "Heap exhausted during %s: %ld bytes available, %ld requested.\n",
1097 gc_active_p ? "garbage collection" : "allocation",
1098 available, requested);
1099 if (gc_active_p || (available == 0)) {
1100 /* If we are in GC, or totally out of memory there is no way
1101 * to sanely transfer control to the lisp-side of things.
1103 struct thread *thread = arch_os_get_current_thread();
1104 print_generation_stats(1);
1105 fprintf(stderr, "GC control variables:\n");
1106 fprintf(stderr, " *GC-INHIBIT* = %s\n *GC-PENDING* = %s\n",
1107 SymbolValue(GC_INHIBIT,thread)==NIL ? "false" : "true",
1108 SymbolValue(GC_PENDING,thread)==NIL ? "false" : "true");
1109 #ifdef LISP_FEATURE_SB_THREAD
1110 fprintf(stderr, " *STOP-FOR-GC-PENDING* = %s\n",
1111 SymbolValue(STOP_FOR_GC_PENDING,thread)==NIL ? "false" : "true");
1113 lose("Heap exhausted, game over.");
1116 /* FIXME: assert free_pages_lock held */
1117 (void)thread_mutex_unlock(&free_pages_lock);
1118 funcall2(StaticSymbolFunction(HEAP_EXHAUSTED_ERROR),
1119 alloc_number(available), alloc_number(requested));
1120 lose("HEAP-EXHAUSTED-ERROR fell through");
1125 gc_find_freeish_pages(page_index_t *restart_page_ptr, long nbytes, int unboxed)
1127 page_index_t first_page, last_page;
1128 page_index_t restart_page = *restart_page_ptr;
1129 long bytes_found = 0;
1130 long most_bytes_found = 0;
1131 /* FIXME: assert(free_pages_lock is held); */
1133 /* Toggled by gc_and_save for heap compaction, normally -1. */
1134 if (gencgc_alloc_start_page != -1) {
1135 restart_page = gencgc_alloc_start_page;
1138 if (nbytes>=PAGE_BYTES) {
1139 /* Search for a contiguous free space of at least nbytes,
1140 * aligned on a page boundary. The page-alignment is strictly
1141 * speaking needed only for objects at least large_object_size
1144 first_page = restart_page;
1145 while ((first_page < page_table_pages) &&
1146 (page_table[first_page].allocated != FREE_PAGE_FLAG))
1149 last_page = first_page;
1150 bytes_found = PAGE_BYTES;
1151 while ((bytes_found < nbytes) &&
1152 (last_page < (page_table_pages-1)) &&
1153 (page_table[last_page+1].allocated == FREE_PAGE_FLAG)) {
1155 bytes_found += PAGE_BYTES;
1156 gc_assert(page_table[last_page].write_protected == 0);
1158 if (bytes_found > most_bytes_found)
1159 most_bytes_found = bytes_found;
1160 restart_page = last_page + 1;
1161 } while ((restart_page < page_table_pages) && (bytes_found < nbytes));
1164 /* Search for a page with at least nbytes of space. We prefer
1165 * not to split small objects on multiple pages, to reduce the
1166 * number of contiguous allocation regions spaning multiple
1167 * pages: this helps avoid excessive conservativism. */
1168 first_page = restart_page;
1169 while (first_page < page_table_pages) {
1170 if (page_table[first_page].allocated == FREE_PAGE_FLAG)
1172 bytes_found = PAGE_BYTES;
1175 else if ((page_table[first_page].allocated ==
1176 (unboxed ? UNBOXED_PAGE_FLAG : BOXED_PAGE_FLAG)) &&
1177 (page_table[first_page].large_object == 0) &&
1178 (page_table[first_page].gen == gc_alloc_generation) &&
1179 (page_table[first_page].write_protected == 0) &&
1180 (page_table[first_page].dont_move == 0))
1182 bytes_found = PAGE_BYTES
1183 - page_table[first_page].bytes_used;
1184 if (bytes_found > most_bytes_found)
1185 most_bytes_found = bytes_found;
1186 if (bytes_found >= nbytes)
1191 last_page = first_page;
1192 restart_page = first_page + 1;
1195 /* Check for a failure */
1196 if (bytes_found < nbytes) {
1197 gc_assert(restart_page >= page_table_pages);
1198 gc_heap_exhausted_error_or_lose(most_bytes_found, nbytes);
1201 gc_assert(page_table[first_page].write_protected == 0);
1203 *restart_page_ptr = first_page;
1207 /* Allocate bytes. All the rest of the special-purpose allocation
1208 * functions will eventually call this */
1211 gc_alloc_with_region(long nbytes,int unboxed_p, struct alloc_region *my_region,
1214 void *new_free_pointer;
1216 if (nbytes>=large_object_size)
1217 return gc_alloc_large(nbytes,unboxed_p,my_region);
1219 /* Check whether there is room in the current alloc region. */
1220 new_free_pointer = my_region->free_pointer + nbytes;
1222 /* fprintf(stderr, "alloc %d bytes from %p to %p\n", nbytes,
1223 my_region->free_pointer, new_free_pointer); */
1225 if (new_free_pointer <= my_region->end_addr) {
1226 /* If so then allocate from the current alloc region. */
1227 void *new_obj = my_region->free_pointer;
1228 my_region->free_pointer = new_free_pointer;
1230 /* Unless a `quick' alloc was requested, check whether the
1231 alloc region is almost empty. */
1233 (my_region->end_addr - my_region->free_pointer) <= 32) {
1234 /* If so, finished with the current region. */
1235 gc_alloc_update_page_tables(unboxed_p, my_region);
1236 /* Set up a new region. */
1237 gc_alloc_new_region(32 /*bytes*/, unboxed_p, my_region);
1240 return((void *)new_obj);
1243 /* Else not enough free space in the current region: retry with a
1246 gc_alloc_update_page_tables(unboxed_p, my_region);
1247 gc_alloc_new_region(nbytes, unboxed_p, my_region);
1248 return gc_alloc_with_region(nbytes,unboxed_p,my_region,0);
1251 /* these are only used during GC: all allocation from the mutator calls
1252 * alloc() -> gc_alloc_with_region() with the appropriate per-thread
1256 gc_general_alloc(long nbytes,int unboxed_p,int quick_p)
1258 struct alloc_region *my_region =
1259 unboxed_p ? &unboxed_region : &boxed_region;
1260 return gc_alloc_with_region(nbytes,unboxed_p, my_region,quick_p);
1263 static inline void *
1264 gc_quick_alloc(long nbytes)
1266 return gc_general_alloc(nbytes,ALLOC_BOXED,ALLOC_QUICK);
1269 static inline void *
1270 gc_quick_alloc_large(long nbytes)
1272 return gc_general_alloc(nbytes,ALLOC_BOXED,ALLOC_QUICK);
1275 static inline void *
1276 gc_alloc_unboxed(long nbytes)
1278 return gc_general_alloc(nbytes,ALLOC_UNBOXED,0);
1281 static inline void *
1282 gc_quick_alloc_unboxed(long nbytes)
1284 return gc_general_alloc(nbytes,ALLOC_UNBOXED,ALLOC_QUICK);
1287 static inline void *
1288 gc_quick_alloc_large_unboxed(long nbytes)
1290 return gc_general_alloc(nbytes,ALLOC_UNBOXED,ALLOC_QUICK);
1294 /* Copy a large boxed object. If the object is in a large object
1295 * region then it is simply promoted, else it is copied. If it's large
1296 * enough then it's copied to a large object region.
1298 * Vectors may have shrunk. If the object is not copied the space
1299 * needs to be reclaimed, and the page_tables corrected. */
1301 copy_large_object(lispobj object, long nwords)
1305 page_index_t first_page;
1307 gc_assert(is_lisp_pointer(object));
1308 gc_assert(from_space_p(object));
1309 gc_assert((nwords & 0x01) == 0);
1312 /* Check whether it's in a large object region. */
1313 first_page = find_page_index((void *)object);
1314 gc_assert(first_page >= 0);
1316 if (page_table[first_page].large_object) {
1318 /* Promote the object. */
1320 long remaining_bytes;
1321 page_index_t next_page;
1323 long old_bytes_used;
1325 /* Note: Any page write-protection must be removed, else a
1326 * later scavenge_newspace may incorrectly not scavenge these
1327 * pages. This would not be necessary if they are added to the
1328 * new areas, but let's do it for them all (they'll probably
1329 * be written anyway?). */
1331 gc_assert(page_table[first_page].first_object_offset == 0);
1333 next_page = first_page;
1334 remaining_bytes = nwords*N_WORD_BYTES;
1335 while (remaining_bytes > PAGE_BYTES) {
1336 gc_assert(page_table[next_page].gen == from_space);
1337 gc_assert(page_table[next_page].allocated == BOXED_PAGE_FLAG);
1338 gc_assert(page_table[next_page].large_object);
1339 gc_assert(page_table[next_page].first_object_offset==
1340 -PAGE_BYTES*(next_page-first_page));
1341 gc_assert(page_table[next_page].bytes_used == PAGE_BYTES);
1343 page_table[next_page].gen = new_space;
1345 /* Remove any write-protection. We should be able to rely
1346 * on the write-protect flag to avoid redundant calls. */
1347 if (page_table[next_page].write_protected) {
1348 os_protect(page_address(next_page), PAGE_BYTES, OS_VM_PROT_ALL);
1349 page_table[next_page].write_protected = 0;
1351 remaining_bytes -= PAGE_BYTES;
1355 /* Now only one page remains, but the object may have shrunk
1356 * so there may be more unused pages which will be freed. */
1358 /* The object may have shrunk but shouldn't have grown. */
1359 gc_assert(page_table[next_page].bytes_used >= remaining_bytes);
1361 page_table[next_page].gen = new_space;
1362 gc_assert(page_table[next_page].allocated == BOXED_PAGE_FLAG);
1364 /* Adjust the bytes_used. */
1365 old_bytes_used = page_table[next_page].bytes_used;
1366 page_table[next_page].bytes_used = remaining_bytes;
1368 bytes_freed = old_bytes_used - remaining_bytes;
1370 /* Free any remaining pages; needs care. */
1372 while ((old_bytes_used == PAGE_BYTES) &&
1373 (page_table[next_page].gen == from_space) &&
1374 (page_table[next_page].allocated == BOXED_PAGE_FLAG) &&
1375 page_table[next_page].large_object &&
1376 (page_table[next_page].first_object_offset ==
1377 -(next_page - first_page)*PAGE_BYTES)) {
1378 /* Checks out OK, free the page. Don't need to bother zeroing
1379 * pages as this should have been done before shrinking the
1380 * object. These pages shouldn't be write-protected as they
1381 * should be zero filled. */
1382 gc_assert(page_table[next_page].write_protected == 0);
1384 old_bytes_used = page_table[next_page].bytes_used;
1385 page_table[next_page].allocated = FREE_PAGE_FLAG;
1386 page_table[next_page].bytes_used = 0;
1387 bytes_freed += old_bytes_used;
1391 generations[from_space].bytes_allocated -= N_WORD_BYTES*nwords
1393 generations[new_space].bytes_allocated += N_WORD_BYTES*nwords;
1394 bytes_allocated -= bytes_freed;
1396 /* Add the region to the new_areas if requested. */
1397 add_new_area(first_page,0,nwords*N_WORD_BYTES);
1401 /* Get tag of object. */
1402 tag = lowtag_of(object);
1404 /* Allocate space. */
1405 new = gc_quick_alloc_large(nwords*N_WORD_BYTES);
1407 memcpy(new,native_pointer(object),nwords*N_WORD_BYTES);
1409 /* Return Lisp pointer of new object. */
1410 return ((lispobj) new) | tag;
1414 /* to copy unboxed objects */
1416 copy_unboxed_object(lispobj object, long nwords)
1421 gc_assert(is_lisp_pointer(object));
1422 gc_assert(from_space_p(object));
1423 gc_assert((nwords & 0x01) == 0);
1425 /* Get tag of object. */
1426 tag = lowtag_of(object);
1428 /* Allocate space. */
1429 new = gc_quick_alloc_unboxed(nwords*N_WORD_BYTES);
1431 memcpy(new,native_pointer(object),nwords*N_WORD_BYTES);
1433 /* Return Lisp pointer of new object. */
1434 return ((lispobj) new) | tag;
1437 /* to copy large unboxed objects
1439 * If the object is in a large object region then it is simply
1440 * promoted, else it is copied. If it's large enough then it's copied
1441 * to a large object region.
1443 * Bignums and vectors may have shrunk. If the object is not copied
1444 * the space needs to be reclaimed, and the page_tables corrected.
1446 * KLUDGE: There's a lot of cut-and-paste duplication between this
1447 * function and copy_large_object(..). -- WHN 20000619 */
1449 copy_large_unboxed_object(lispobj object, long nwords)
1453 page_index_t first_page;
1455 gc_assert(is_lisp_pointer(object));
1456 gc_assert(from_space_p(object));
1457 gc_assert((nwords & 0x01) == 0);
1459 if ((nwords > 1024*1024) && gencgc_verbose)
1460 FSHOW((stderr, "/copy_large_unboxed_object: %d bytes\n",
1461 nwords*N_WORD_BYTES));
1463 /* Check whether it's a large object. */
1464 first_page = find_page_index((void *)object);
1465 gc_assert(first_page >= 0);
1467 if (page_table[first_page].large_object) {
1468 /* Promote the object. Note: Unboxed objects may have been
1469 * allocated to a BOXED region so it may be necessary to
1470 * change the region to UNBOXED. */
1471 long remaining_bytes;
1472 page_index_t next_page;
1474 long old_bytes_used;
1476 gc_assert(page_table[first_page].first_object_offset == 0);
1478 next_page = first_page;
1479 remaining_bytes = nwords*N_WORD_BYTES;
1480 while (remaining_bytes > PAGE_BYTES) {
1481 gc_assert(page_table[next_page].gen == from_space);
1482 gc_assert((page_table[next_page].allocated == UNBOXED_PAGE_FLAG)
1483 || (page_table[next_page].allocated == BOXED_PAGE_FLAG));
1484 gc_assert(page_table[next_page].large_object);
1485 gc_assert(page_table[next_page].first_object_offset==
1486 -PAGE_BYTES*(next_page-first_page));
1487 gc_assert(page_table[next_page].bytes_used == PAGE_BYTES);
1489 page_table[next_page].gen = new_space;
1490 page_table[next_page].allocated = UNBOXED_PAGE_FLAG;
1491 remaining_bytes -= PAGE_BYTES;
1495 /* Now only one page remains, but the object may have shrunk so
1496 * there may be more unused pages which will be freed. */
1498 /* Object may have shrunk but shouldn't have grown - check. */
1499 gc_assert(page_table[next_page].bytes_used >= remaining_bytes);
1501 page_table[next_page].gen = new_space;
1502 page_table[next_page].allocated = UNBOXED_PAGE_FLAG;
1504 /* Adjust the bytes_used. */
1505 old_bytes_used = page_table[next_page].bytes_used;
1506 page_table[next_page].bytes_used = remaining_bytes;
1508 bytes_freed = old_bytes_used - remaining_bytes;
1510 /* Free any remaining pages; needs care. */
1512 while ((old_bytes_used == PAGE_BYTES) &&
1513 (page_table[next_page].gen == from_space) &&
1514 ((page_table[next_page].allocated == UNBOXED_PAGE_FLAG)
1515 || (page_table[next_page].allocated == BOXED_PAGE_FLAG)) &&
1516 page_table[next_page].large_object &&
1517 (page_table[next_page].first_object_offset ==
1518 -(next_page - first_page)*PAGE_BYTES)) {
1519 /* Checks out OK, free the page. Don't need to both zeroing
1520 * pages as this should have been done before shrinking the
1521 * object. These pages shouldn't be write-protected, even if
1522 * boxed they should be zero filled. */
1523 gc_assert(page_table[next_page].write_protected == 0);
1525 old_bytes_used = page_table[next_page].bytes_used;
1526 page_table[next_page].allocated = FREE_PAGE_FLAG;
1527 page_table[next_page].bytes_used = 0;
1528 bytes_freed += old_bytes_used;
1532 if ((bytes_freed > 0) && gencgc_verbose)
1534 "/copy_large_unboxed bytes_freed=%d\n",
1537 generations[from_space].bytes_allocated -=
1538 nwords*N_WORD_BYTES + bytes_freed;
1539 generations[new_space].bytes_allocated += nwords*N_WORD_BYTES;
1540 bytes_allocated -= bytes_freed;
1545 /* Get tag of object. */
1546 tag = lowtag_of(object);
1548 /* Allocate space. */
1549 new = gc_quick_alloc_large_unboxed(nwords*N_WORD_BYTES);
1551 /* Copy the object. */
1552 memcpy(new,native_pointer(object),nwords*N_WORD_BYTES);
1554 /* Return Lisp pointer of new object. */
1555 return ((lispobj) new) | tag;
1564 * code and code-related objects
1567 static lispobj trans_fun_header(lispobj object);
1568 static lispobj trans_boxed(lispobj object);
1571 /* Scan a x86 compiled code object, looking for possible fixups that
1572 * have been missed after a move.
1574 * Two types of fixups are needed:
1575 * 1. Absolute fixups to within the code object.
1576 * 2. Relative fixups to outside the code object.
1578 * Currently only absolute fixups to the constant vector, or to the
1579 * code area are checked. */
1581 sniff_code_object(struct code *code, unsigned long displacement)
1583 #ifdef LISP_FEATURE_X86
1584 long nheader_words, ncode_words, nwords;
1586 void *constants_start_addr = NULL, *constants_end_addr;
1587 void *code_start_addr, *code_end_addr;
1588 int fixup_found = 0;
1590 if (!check_code_fixups)
1593 FSHOW((stderr, "/sniffing code: %p, %lu\n", code, displacement));
1595 ncode_words = fixnum_value(code->code_size);
1596 nheader_words = HeaderValue(*(lispobj *)code);
1597 nwords = ncode_words + nheader_words;
1599 constants_start_addr = (void *)code + 5*N_WORD_BYTES;
1600 constants_end_addr = (void *)code + nheader_words*N_WORD_BYTES;
1601 code_start_addr = (void *)code + nheader_words*N_WORD_BYTES;
1602 code_end_addr = (void *)code + nwords*N_WORD_BYTES;
1604 /* Work through the unboxed code. */
1605 for (p = code_start_addr; p < code_end_addr; p++) {
1606 void *data = *(void **)p;
1607 unsigned d1 = *((unsigned char *)p - 1);
1608 unsigned d2 = *((unsigned char *)p - 2);
1609 unsigned d3 = *((unsigned char *)p - 3);
1610 unsigned d4 = *((unsigned char *)p - 4);
1612 unsigned d5 = *((unsigned char *)p - 5);
1613 unsigned d6 = *((unsigned char *)p - 6);
1616 /* Check for code references. */
1617 /* Check for a 32 bit word that looks like an absolute
1618 reference to within the code adea of the code object. */
1619 if ((data >= (code_start_addr-displacement))
1620 && (data < (code_end_addr-displacement))) {
1621 /* function header */
1623 && (((unsigned)p - 4 - 4*HeaderValue(*((unsigned *)p-1))) ==
1625 /* Skip the function header */
1629 /* the case of PUSH imm32 */
1633 "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1634 p, d6, d5, d4, d3, d2, d1, data));
1635 FSHOW((stderr, "/PUSH $0x%.8x\n", data));
1637 /* the case of MOV [reg-8],imm32 */
1639 && (d2==0x40 || d2==0x41 || d2==0x42 || d2==0x43
1640 || d2==0x45 || d2==0x46 || d2==0x47)
1644 "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1645 p, d6, d5, d4, d3, d2, d1, data));
1646 FSHOW((stderr, "/MOV [reg-8],$0x%.8x\n", data));
1648 /* the case of LEA reg,[disp32] */
1649 if ((d2 == 0x8d) && ((d1 & 0xc7) == 5)) {
1652 "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1653 p, d6, d5, d4, d3, d2, d1, data));
1654 FSHOW((stderr,"/LEA reg,[$0x%.8x]\n", data));
1658 /* Check for constant references. */
1659 /* Check for a 32 bit word that looks like an absolute
1660 reference to within the constant vector. Constant references
1662 if ((data >= (constants_start_addr-displacement))
1663 && (data < (constants_end_addr-displacement))
1664 && (((unsigned)data & 0x3) == 0)) {
1669 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1670 p, d6, d5, d4, d3, d2, d1, data));
1671 FSHOW((stderr,"/MOV eax,0x%.8x\n", data));
1674 /* the case of MOV m32,EAX */
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, "/MOV 0x%.8x,eax\n", data));
1683 /* the case of CMP m32,imm32 */
1684 if ((d1 == 0x3d) && (d2 == 0x81)) {
1687 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1688 p, d6, d5, d4, d3, d2, d1, data));
1690 FSHOW((stderr, "/CMP 0x%.8x,immed32\n", data));
1693 /* Check for a mod=00, r/m=101 byte. */
1694 if ((d1 & 0xc7) == 5) {
1699 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1700 p, d6, d5, d4, d3, d2, d1, data));
1701 FSHOW((stderr,"/CMP 0x%.8x,reg\n", data));
1703 /* the case of CMP reg32,m32 */
1707 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1708 p, d6, d5, d4, d3, d2, d1, data));
1709 FSHOW((stderr, "/CMP reg32,0x%.8x\n", data));
1711 /* the case of MOV m32,reg32 */
1715 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1716 p, d6, d5, d4, d3, d2, d1, data));
1717 FSHOW((stderr, "/MOV 0x%.8x,reg32\n", data));
1719 /* the case of MOV reg32,m32 */
1723 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1724 p, d6, d5, d4, d3, d2, d1, data));
1725 FSHOW((stderr, "/MOV reg32,0x%.8x\n", data));
1727 /* the case of LEA reg32,m32 */
1731 "abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1732 p, d6, d5, d4, d3, d2, d1, data));
1733 FSHOW((stderr, "/LEA reg32,0x%.8x\n", data));
1739 /* If anything was found, print some information on the code
1743 "/compiled code object at %x: header words = %d, code words = %d\n",
1744 code, nheader_words, ncode_words));
1746 "/const start = %x, end = %x\n",
1747 constants_start_addr, constants_end_addr));
1749 "/code start = %x, end = %x\n",
1750 code_start_addr, code_end_addr));
1756 gencgc_apply_code_fixups(struct code *old_code, struct code *new_code)
1758 /* x86-64 uses pc-relative addressing instead of this kludge */
1759 #ifndef LISP_FEATURE_X86_64
1760 long nheader_words, ncode_words, nwords;
1761 void *constants_start_addr, *constants_end_addr;
1762 void *code_start_addr, *code_end_addr;
1763 lispobj fixups = NIL;
1764 unsigned long displacement =
1765 (unsigned long)new_code - (unsigned long)old_code;
1766 struct vector *fixups_vector;
1768 ncode_words = fixnum_value(new_code->code_size);
1769 nheader_words = HeaderValue(*(lispobj *)new_code);
1770 nwords = ncode_words + nheader_words;
1772 "/compiled code object at %x: header words = %d, code words = %d\n",
1773 new_code, nheader_words, ncode_words)); */
1774 constants_start_addr = (void *)new_code + 5*N_WORD_BYTES;
1775 constants_end_addr = (void *)new_code + nheader_words*N_WORD_BYTES;
1776 code_start_addr = (void *)new_code + nheader_words*N_WORD_BYTES;
1777 code_end_addr = (void *)new_code + nwords*N_WORD_BYTES;
1780 "/const start = %x, end = %x\n",
1781 constants_start_addr,constants_end_addr));
1783 "/code start = %x; end = %x\n",
1784 code_start_addr,code_end_addr));
1787 /* The first constant should be a pointer to the fixups for this
1788 code objects. Check. */
1789 fixups = new_code->constants[0];
1791 /* It will be 0 or the unbound-marker if there are no fixups (as
1792 * will be the case if the code object has been purified, for
1793 * example) and will be an other pointer if it is valid. */
1794 if ((fixups == 0) || (fixups == UNBOUND_MARKER_WIDETAG) ||
1795 !is_lisp_pointer(fixups)) {
1796 /* Check for possible errors. */
1797 if (check_code_fixups)
1798 sniff_code_object(new_code, displacement);
1803 fixups_vector = (struct vector *)native_pointer(fixups);
1805 /* Could be pointing to a forwarding pointer. */
1806 /* FIXME is this always in from_space? if so, could replace this code with
1807 * forwarding_pointer_p/forwarding_pointer_value */
1808 if (is_lisp_pointer(fixups) &&
1809 (find_page_index((void*)fixups_vector) != -1) &&
1810 (fixups_vector->header == 0x01)) {
1811 /* If so, then follow it. */
1812 /*SHOW("following pointer to a forwarding pointer");*/
1814 (struct vector *)native_pointer((lispobj)fixups_vector->length);
1817 /*SHOW("got fixups");*/
1819 if (widetag_of(fixups_vector->header) == SIMPLE_ARRAY_WORD_WIDETAG) {
1820 /* Got the fixups for the code block. Now work through the vector,
1821 and apply a fixup at each address. */
1822 long length = fixnum_value(fixups_vector->length);
1824 for (i = 0; i < length; i++) {
1825 unsigned long offset = fixups_vector->data[i];
1826 /* Now check the current value of offset. */
1827 unsigned long old_value =
1828 *(unsigned long *)((unsigned long)code_start_addr + offset);
1830 /* If it's within the old_code object then it must be an
1831 * absolute fixup (relative ones are not saved) */
1832 if ((old_value >= (unsigned long)old_code)
1833 && (old_value < ((unsigned long)old_code
1834 + nwords*N_WORD_BYTES)))
1835 /* So add the dispacement. */
1836 *(unsigned long *)((unsigned long)code_start_addr + offset) =
1837 old_value + displacement;
1839 /* It is outside the old code object so it must be a
1840 * relative fixup (absolute fixups are not saved). So
1841 * subtract the displacement. */
1842 *(unsigned long *)((unsigned long)code_start_addr + offset) =
1843 old_value - displacement;
1846 /* This used to just print a note to stderr, but a bogus fixup seems to
1847 * indicate real heap corruption, so a hard hailure is in order. */
1848 lose("fixup vector %p has a bad widetag: %d\n",
1849 fixups_vector, widetag_of(fixups_vector->header));
1852 /* Check for possible errors. */
1853 if (check_code_fixups) {
1854 sniff_code_object(new_code,displacement);
1861 trans_boxed_large(lispobj object)
1864 unsigned long length;
1866 gc_assert(is_lisp_pointer(object));
1868 header = *((lispobj *) native_pointer(object));
1869 length = HeaderValue(header) + 1;
1870 length = CEILING(length, 2);
1872 return copy_large_object(object, length);
1875 /* Doesn't seem to be used, delete it after the grace period. */
1878 trans_unboxed_large(lispobj object)
1881 unsigned long length;
1883 gc_assert(is_lisp_pointer(object));
1885 header = *((lispobj *) native_pointer(object));
1886 length = HeaderValue(header) + 1;
1887 length = CEILING(length, 2);
1889 return copy_large_unboxed_object(object, length);
1895 * Lutexes. Using the normal finalization machinery for finalizing
1896 * lutexes is tricky, since the finalization depends on working lutexes.
1897 * So we track the lutexes in the GC and finalize them manually.
1900 #if defined(LUTEX_WIDETAG)
1903 * Start tracking LUTEX in the GC, by adding it to the linked list of
1904 * lutexes in the nursery generation. The caller is responsible for
1905 * locking, and GCs must be inhibited until the registration is
1909 gencgc_register_lutex (struct lutex *lutex) {
1910 int index = find_page_index(lutex);
1911 generation_index_t gen;
1914 /* This lutex is in static space, so we don't need to worry about
1920 gen = page_table[index].gen;
1922 gc_assert(gen >= 0);
1923 gc_assert(gen < NUM_GENERATIONS);
1925 head = generations[gen].lutexes;
1932 generations[gen].lutexes = lutex;
1936 * Stop tracking LUTEX in the GC by removing it from the appropriate
1937 * linked lists. This will only be called during GC, so no locking is
1941 gencgc_unregister_lutex (struct lutex *lutex) {
1943 lutex->prev->next = lutex->next;
1945 generations[lutex->gen].lutexes = lutex->next;
1949 lutex->next->prev = lutex->prev;
1958 * Mark all lutexes in generation GEN as not live.
1961 unmark_lutexes (generation_index_t gen) {
1962 struct lutex *lutex = generations[gen].lutexes;
1966 lutex = lutex->next;
1971 * Finalize all lutexes in generation GEN that have not been marked live.
1974 reap_lutexes (generation_index_t gen) {
1975 struct lutex *lutex = generations[gen].lutexes;
1978 struct lutex *next = lutex->next;
1980 lutex_destroy((tagged_lutex_t) lutex);
1981 gencgc_unregister_lutex(lutex);
1988 * Mark LUTEX as live.
1991 mark_lutex (lispobj tagged_lutex) {
1992 struct lutex *lutex = (struct lutex*) native_pointer(tagged_lutex);
1998 * Move all lutexes in generation FROM to generation TO.
2001 move_lutexes (generation_index_t from, generation_index_t to) {
2002 struct lutex *tail = generations[from].lutexes;
2004 /* Nothing to move */
2008 /* Change the generation of the lutexes in FROM. */
2009 while (tail->next) {
2015 /* Link the last lutex in the FROM list to the start of the TO list */
2016 tail->next = generations[to].lutexes;
2018 /* And vice versa */
2019 if (generations[to].lutexes) {
2020 generations[to].lutexes->prev = tail;
2023 /* And update the generations structures to match this */
2024 generations[to].lutexes = generations[from].lutexes;
2025 generations[from].lutexes = NULL;
2029 scav_lutex(lispobj *where, lispobj object)
2031 mark_lutex((lispobj) where);
2033 return CEILING(sizeof(struct lutex)/sizeof(lispobj), 2);
2037 trans_lutex(lispobj object)
2039 struct lutex *lutex = (struct lutex *) native_pointer(object);
2041 size_t words = CEILING(sizeof(struct lutex)/sizeof(lispobj), 2);
2042 gc_assert(is_lisp_pointer(object));
2043 copied = copy_object(object, words);
2045 /* Update the links, since the lutex moved in memory. */
2047 lutex->next->prev = (struct lutex *) native_pointer(copied);
2051 lutex->prev->next = (struct lutex *) native_pointer(copied);
2053 generations[lutex->gen].lutexes =
2054 (struct lutex *) native_pointer(copied);
2061 size_lutex(lispobj *where)
2063 return CEILING(sizeof(struct lutex)/sizeof(lispobj), 2);
2065 #endif /* LUTEX_WIDETAG */
2072 /* XX This is a hack adapted from cgc.c. These don't work too
2073 * efficiently with the gencgc as a list of the weak pointers is
2074 * maintained within the objects which causes writes to the pages. A
2075 * limited attempt is made to avoid unnecessary writes, but this needs
2077 #define WEAK_POINTER_NWORDS \
2078 CEILING((sizeof(struct weak_pointer) / sizeof(lispobj)), 2)
2081 scav_weak_pointer(lispobj *where, lispobj object)
2083 /* Since we overwrite the 'next' field, we have to make
2084 * sure not to do so for pointers already in the list.
2085 * Instead of searching the list of weak_pointers each
2086 * time, we ensure that next is always NULL when the weak
2087 * pointer isn't in the list, and not NULL otherwise.
2088 * Since we can't use NULL to denote end of list, we
2089 * use a pointer back to the same weak_pointer.
2091 struct weak_pointer * wp = (struct weak_pointer*)where;
2093 if (NULL == wp->next) {
2094 wp->next = weak_pointers;
2096 if (NULL == wp->next)
2100 /* Do not let GC scavenge the value slot of the weak pointer.
2101 * (That is why it is a weak pointer.) */
2103 return WEAK_POINTER_NWORDS;
2108 search_read_only_space(void *pointer)
2110 lispobj *start = (lispobj *) READ_ONLY_SPACE_START;
2111 lispobj *end = (lispobj *) SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0);
2112 if ((pointer < (void *)start) || (pointer >= (void *)end))
2114 return (gc_search_space(start,
2115 (((lispobj *)pointer)+2)-start,
2116 (lispobj *) pointer));
2120 search_static_space(void *pointer)
2122 lispobj *start = (lispobj *)STATIC_SPACE_START;
2123 lispobj *end = (lispobj *)SymbolValue(STATIC_SPACE_FREE_POINTER,0);
2124 if ((pointer < (void *)start) || (pointer >= (void *)end))
2126 return (gc_search_space(start,
2127 (((lispobj *)pointer)+2)-start,
2128 (lispobj *) pointer));
2131 /* a faster version for searching the dynamic space. This will work even
2132 * if the object is in a current allocation region. */
2134 search_dynamic_space(void *pointer)
2136 page_index_t page_index = find_page_index(pointer);
2139 /* The address may be invalid, so do some checks. */
2140 if ((page_index == -1) ||
2141 (page_table[page_index].allocated == FREE_PAGE_FLAG))
2143 start = (lispobj *)page_region_start(page_index);
2144 return (gc_search_space(start,
2145 (((lispobj *)pointer)+2)-start,
2146 (lispobj *)pointer));
2149 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
2151 /* Helper for valid_lisp_pointer_p and
2152 * possibly_valid_dynamic_space_pointer.
2154 * pointer is the pointer to validate, and start_addr is the address
2155 * of the enclosing object.
2158 looks_like_valid_lisp_pointer_p(lispobj *pointer, lispobj *start_addr)
2160 /* We need to allow raw pointers into Code objects for return
2161 * addresses. This will also pick up pointers to functions in code
2163 if (widetag_of(*start_addr) == CODE_HEADER_WIDETAG)
2164 /* XXX could do some further checks here */
2167 if (!is_lisp_pointer((lispobj)pointer)) {
2171 /* Check that the object pointed to is consistent with the pointer
2173 switch (lowtag_of((lispobj)pointer)) {
2174 case FUN_POINTER_LOWTAG:
2175 /* Start_addr should be the enclosing code object, or a closure
2177 switch (widetag_of(*start_addr)) {
2178 case CODE_HEADER_WIDETAG:
2179 /* This case is probably caught above. */
2181 case CLOSURE_HEADER_WIDETAG:
2182 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
2183 if ((unsigned long)pointer !=
2184 ((unsigned long)start_addr+FUN_POINTER_LOWTAG)) {
2188 pointer, start_addr, *start_addr));
2196 pointer, start_addr, *start_addr));
2200 case LIST_POINTER_LOWTAG:
2201 if ((unsigned long)pointer !=
2202 ((unsigned long)start_addr+LIST_POINTER_LOWTAG)) {
2206 pointer, start_addr, *start_addr));
2209 /* Is it plausible cons? */
2210 if ((is_lisp_pointer(start_addr[0]) ||
2211 is_lisp_immediate(start_addr[0])) &&
2212 (is_lisp_pointer(start_addr[1]) ||
2213 is_lisp_immediate(start_addr[1])))
2219 pointer, start_addr, *start_addr));
2222 case INSTANCE_POINTER_LOWTAG:
2223 if ((unsigned long)pointer !=
2224 ((unsigned long)start_addr+INSTANCE_POINTER_LOWTAG)) {
2228 pointer, start_addr, *start_addr));
2231 if (widetag_of(start_addr[0]) != INSTANCE_HEADER_WIDETAG) {
2235 pointer, start_addr, *start_addr));
2239 case OTHER_POINTER_LOWTAG:
2240 if ((unsigned long)pointer !=
2241 ((unsigned long)start_addr+OTHER_POINTER_LOWTAG)) {
2245 pointer, start_addr, *start_addr));
2248 /* Is it plausible? Not a cons. XXX should check the headers. */
2249 if (is_lisp_pointer(start_addr[0]) || ((start_addr[0] & 3) == 0)) {
2253 pointer, start_addr, *start_addr));
2256 switch (widetag_of(start_addr[0])) {
2257 case UNBOUND_MARKER_WIDETAG:
2258 case NO_TLS_VALUE_MARKER_WIDETAG:
2259 case CHARACTER_WIDETAG:
2260 #if N_WORD_BITS == 64
2261 case SINGLE_FLOAT_WIDETAG:
2266 pointer, start_addr, *start_addr));
2269 /* only pointed to by function pointers? */
2270 case CLOSURE_HEADER_WIDETAG:
2271 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
2275 pointer, start_addr, *start_addr));
2278 case INSTANCE_HEADER_WIDETAG:
2282 pointer, start_addr, *start_addr));
2285 /* the valid other immediate pointer objects */
2286 case SIMPLE_VECTOR_WIDETAG:
2288 case COMPLEX_WIDETAG:
2289 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
2290 case COMPLEX_SINGLE_FLOAT_WIDETAG:
2292 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
2293 case COMPLEX_DOUBLE_FLOAT_WIDETAG:
2295 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
2296 case COMPLEX_LONG_FLOAT_WIDETAG:
2298 case SIMPLE_ARRAY_WIDETAG:
2299 case COMPLEX_BASE_STRING_WIDETAG:
2300 #ifdef COMPLEX_CHARACTER_STRING_WIDETAG
2301 case COMPLEX_CHARACTER_STRING_WIDETAG:
2303 case COMPLEX_VECTOR_NIL_WIDETAG:
2304 case COMPLEX_BIT_VECTOR_WIDETAG:
2305 case COMPLEX_VECTOR_WIDETAG:
2306 case COMPLEX_ARRAY_WIDETAG:
2307 case VALUE_CELL_HEADER_WIDETAG:
2308 case SYMBOL_HEADER_WIDETAG:
2310 case CODE_HEADER_WIDETAG:
2311 case BIGNUM_WIDETAG:
2312 #if N_WORD_BITS != 64
2313 case SINGLE_FLOAT_WIDETAG:
2315 case DOUBLE_FLOAT_WIDETAG:
2316 #ifdef LONG_FLOAT_WIDETAG
2317 case LONG_FLOAT_WIDETAG:
2319 case SIMPLE_BASE_STRING_WIDETAG:
2320 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
2321 case SIMPLE_CHARACTER_STRING_WIDETAG:
2323 case SIMPLE_BIT_VECTOR_WIDETAG:
2324 case SIMPLE_ARRAY_NIL_WIDETAG:
2325 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
2326 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
2327 case SIMPLE_ARRAY_UNSIGNED_BYTE_7_WIDETAG:
2328 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
2329 case SIMPLE_ARRAY_UNSIGNED_BYTE_15_WIDETAG:
2330 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
2331 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_29_WIDETAG
2332 case SIMPLE_ARRAY_UNSIGNED_BYTE_29_WIDETAG:
2334 case SIMPLE_ARRAY_UNSIGNED_BYTE_31_WIDETAG:
2335 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
2336 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_60_WIDETAG
2337 case SIMPLE_ARRAY_UNSIGNED_BYTE_60_WIDETAG:
2339 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG
2340 case SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG:
2342 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG
2343 case SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG:
2345 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
2346 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
2348 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
2349 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
2351 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
2352 case SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG:
2354 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
2355 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
2357 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_61_WIDETAG
2358 case SIMPLE_ARRAY_SIGNED_BYTE_61_WIDETAG:
2360 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG
2361 case SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG:
2363 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
2364 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
2365 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
2366 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
2368 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
2369 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
2371 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
2372 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
2374 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
2375 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
2378 case WEAK_POINTER_WIDETAG:
2379 #ifdef LUTEX_WIDETAG
2388 pointer, start_addr, *start_addr));
2396 pointer, start_addr, *start_addr));
2404 /* Used by the debugger to validate possibly bogus pointers before
2405 * calling MAKE-LISP-OBJ on them.
2407 * FIXME: We would like to make this perfect, because if the debugger
2408 * constructs a reference to a bugs lisp object, and it ends up in a
2409 * location scavenged by the GC all hell breaks loose.
2411 * Whereas possibly_valid_dynamic_space_pointer has to be conservative
2412 * and return true for all valid pointers, this could actually be eager
2413 * and lie about a few pointers without bad results... but that should
2414 * be reflected in the name.
2417 valid_lisp_pointer_p(lispobj *pointer)
2420 if (((start=search_dynamic_space(pointer))!=NULL) ||
2421 ((start=search_static_space(pointer))!=NULL) ||
2422 ((start=search_read_only_space(pointer))!=NULL))
2423 return looks_like_valid_lisp_pointer_p(pointer, start);
2428 /* Is there any possibility that pointer is a valid Lisp object
2429 * reference, and/or something else (e.g. subroutine call return
2430 * address) which should prevent us from moving the referred-to thing?
2431 * This is called from preserve_pointers() */
2433 possibly_valid_dynamic_space_pointer(lispobj *pointer)
2435 lispobj *start_addr;
2437 /* Find the object start address. */
2438 if ((start_addr = search_dynamic_space(pointer)) == NULL) {
2442 return looks_like_valid_lisp_pointer_p(pointer, start_addr);
2445 /* Adjust large bignum and vector objects. This will adjust the
2446 * allocated region if the size has shrunk, and move unboxed objects
2447 * into unboxed pages. The pages are not promoted here, and the
2448 * promoted region is not added to the new_regions; this is really
2449 * only designed to be called from preserve_pointer(). Shouldn't fail
2450 * if this is missed, just may delay the moving of objects to unboxed
2451 * pages, and the freeing of pages. */
2453 maybe_adjust_large_object(lispobj *where)
2455 page_index_t first_page;
2456 page_index_t next_page;
2459 long remaining_bytes;
2461 long old_bytes_used;
2465 /* Check whether it's a vector or bignum object. */
2466 switch (widetag_of(where[0])) {
2467 case SIMPLE_VECTOR_WIDETAG:
2468 boxed = BOXED_PAGE_FLAG;
2470 case BIGNUM_WIDETAG:
2471 case SIMPLE_BASE_STRING_WIDETAG:
2472 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
2473 case SIMPLE_CHARACTER_STRING_WIDETAG:
2475 case SIMPLE_BIT_VECTOR_WIDETAG:
2476 case SIMPLE_ARRAY_NIL_WIDETAG:
2477 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
2478 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
2479 case SIMPLE_ARRAY_UNSIGNED_BYTE_7_WIDETAG:
2480 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
2481 case SIMPLE_ARRAY_UNSIGNED_BYTE_15_WIDETAG:
2482 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
2483 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_29_WIDETAG
2484 case SIMPLE_ARRAY_UNSIGNED_BYTE_29_WIDETAG:
2486 case SIMPLE_ARRAY_UNSIGNED_BYTE_31_WIDETAG:
2487 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
2488 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_60_WIDETAG
2489 case SIMPLE_ARRAY_UNSIGNED_BYTE_60_WIDETAG:
2491 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG
2492 case SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG:
2494 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG
2495 case SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG:
2497 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
2498 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
2500 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
2501 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
2503 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
2504 case SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG:
2506 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
2507 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
2509 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_61_WIDETAG
2510 case SIMPLE_ARRAY_SIGNED_BYTE_61_WIDETAG:
2512 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG
2513 case SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG:
2515 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
2516 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
2517 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
2518 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
2520 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
2521 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
2523 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
2524 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
2526 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
2527 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
2529 boxed = UNBOXED_PAGE_FLAG;
2535 /* Find its current size. */
2536 nwords = (sizetab[widetag_of(where[0])])(where);
2538 first_page = find_page_index((void *)where);
2539 gc_assert(first_page >= 0);
2541 /* Note: Any page write-protection must be removed, else a later
2542 * scavenge_newspace may incorrectly not scavenge these pages.
2543 * This would not be necessary if they are added to the new areas,
2544 * but lets do it for them all (they'll probably be written
2547 gc_assert(page_table[first_page].first_object_offset == 0);
2549 next_page = first_page;
2550 remaining_bytes = nwords*N_WORD_BYTES;
2551 while (remaining_bytes > PAGE_BYTES) {
2552 gc_assert(page_table[next_page].gen == from_space);
2553 gc_assert((page_table[next_page].allocated == BOXED_PAGE_FLAG)
2554 || (page_table[next_page].allocated == UNBOXED_PAGE_FLAG));
2555 gc_assert(page_table[next_page].large_object);
2556 gc_assert(page_table[next_page].first_object_offset ==
2557 -PAGE_BYTES*(next_page-first_page));
2558 gc_assert(page_table[next_page].bytes_used == PAGE_BYTES);
2560 page_table[next_page].allocated = boxed;
2562 /* Shouldn't be write-protected at this stage. Essential that the
2564 gc_assert(!page_table[next_page].write_protected);
2565 remaining_bytes -= PAGE_BYTES;
2569 /* Now only one page remains, but the object may have shrunk so
2570 * there may be more unused pages which will be freed. */
2572 /* Object may have shrunk but shouldn't have grown - check. */
2573 gc_assert(page_table[next_page].bytes_used >= remaining_bytes);
2575 page_table[next_page].allocated = boxed;
2576 gc_assert(page_table[next_page].allocated ==
2577 page_table[first_page].allocated);
2579 /* Adjust the bytes_used. */
2580 old_bytes_used = page_table[next_page].bytes_used;
2581 page_table[next_page].bytes_used = remaining_bytes;
2583 bytes_freed = old_bytes_used - remaining_bytes;
2585 /* Free any remaining pages; needs care. */
2587 while ((old_bytes_used == PAGE_BYTES) &&
2588 (page_table[next_page].gen == from_space) &&
2589 ((page_table[next_page].allocated == UNBOXED_PAGE_FLAG)
2590 || (page_table[next_page].allocated == BOXED_PAGE_FLAG)) &&
2591 page_table[next_page].large_object &&
2592 (page_table[next_page].first_object_offset ==
2593 -(next_page - first_page)*PAGE_BYTES)) {
2594 /* It checks out OK, free the page. We don't need to both zeroing
2595 * pages as this should have been done before shrinking the
2596 * object. These pages shouldn't be write protected as they
2597 * should be zero filled. */
2598 gc_assert(page_table[next_page].write_protected == 0);
2600 old_bytes_used = page_table[next_page].bytes_used;
2601 page_table[next_page].allocated = FREE_PAGE_FLAG;
2602 page_table[next_page].bytes_used = 0;
2603 bytes_freed += old_bytes_used;
2607 if ((bytes_freed > 0) && gencgc_verbose) {
2609 "/maybe_adjust_large_object() freed %d\n",
2613 generations[from_space].bytes_allocated -= bytes_freed;
2614 bytes_allocated -= bytes_freed;
2619 /* Take a possible pointer to a Lisp object and mark its page in the
2620 * page_table so that it will not be relocated during a GC.
2622 * This involves locating the page it points to, then backing up to
2623 * the start of its region, then marking all pages dont_move from there
2624 * up to the first page that's not full or has a different generation
2626 * It is assumed that all the page static flags have been cleared at
2627 * the start of a GC.
2629 * It is also assumed that the current gc_alloc() region has been
2630 * flushed and the tables updated. */
2633 preserve_pointer(void *addr)
2635 page_index_t addr_page_index = find_page_index(addr);
2636 page_index_t first_page;
2638 unsigned int region_allocation;
2640 /* quick check 1: Address is quite likely to have been invalid. */
2641 if ((addr_page_index == -1)
2642 || (page_table[addr_page_index].allocated == FREE_PAGE_FLAG)
2643 || (page_table[addr_page_index].bytes_used == 0)
2644 || (page_table[addr_page_index].gen != from_space)
2645 /* Skip if already marked dont_move. */
2646 || (page_table[addr_page_index].dont_move != 0))
2648 gc_assert(!(page_table[addr_page_index].allocated&OPEN_REGION_PAGE_FLAG));
2649 /* (Now that we know that addr_page_index is in range, it's
2650 * safe to index into page_table[] with it.) */
2651 region_allocation = page_table[addr_page_index].allocated;
2653 /* quick check 2: Check the offset within the page.
2656 if (((unsigned long)addr & (PAGE_BYTES - 1)) >
2657 page_table[addr_page_index].bytes_used)
2660 /* Filter out anything which can't be a pointer to a Lisp object
2661 * (or, as a special case which also requires dont_move, a return
2662 * address referring to something in a CodeObject). This is
2663 * expensive but important, since it vastly reduces the
2664 * probability that random garbage will be bogusly interpreted as
2665 * a pointer which prevents a page from moving. */
2666 if (!(possibly_valid_dynamic_space_pointer(addr)))
2669 /* Find the beginning of the region. Note that there may be
2670 * objects in the region preceding the one that we were passed a
2671 * pointer to: if this is the case, we will write-protect all the
2672 * previous objects' pages too. */
2675 /* I think this'd work just as well, but without the assertions.
2676 * -dan 2004.01.01 */
2677 first_page = find_page_index(page_region_start(addr_page_index))
2679 first_page = addr_page_index;
2680 while (page_table[first_page].first_object_offset != 0) {
2682 /* Do some checks. */
2683 gc_assert(page_table[first_page].bytes_used == PAGE_BYTES);
2684 gc_assert(page_table[first_page].gen == from_space);
2685 gc_assert(page_table[first_page].allocated == region_allocation);
2689 /* Adjust any large objects before promotion as they won't be
2690 * copied after promotion. */
2691 if (page_table[first_page].large_object) {
2692 maybe_adjust_large_object(page_address(first_page));
2693 /* If a large object has shrunk then addr may now point to a
2694 * free area in which case it's ignored here. Note it gets
2695 * through the valid pointer test above because the tail looks
2697 if ((page_table[addr_page_index].allocated == FREE_PAGE_FLAG)
2698 || (page_table[addr_page_index].bytes_used == 0)
2699 /* Check the offset within the page. */
2700 || (((unsigned long)addr & (PAGE_BYTES - 1))
2701 > page_table[addr_page_index].bytes_used)) {
2703 "weird? ignore ptr 0x%x to freed area of large object\n",
2707 /* It may have moved to unboxed pages. */
2708 region_allocation = page_table[first_page].allocated;
2711 /* Now work forward until the end of this contiguous area is found,
2712 * marking all pages as dont_move. */
2713 for (i = first_page; ;i++) {
2714 gc_assert(page_table[i].allocated == region_allocation);
2716 /* Mark the page static. */
2717 page_table[i].dont_move = 1;
2719 /* Move the page to the new_space. XX I'd rather not do this
2720 * but the GC logic is not quite able to copy with the static
2721 * pages remaining in the from space. This also requires the
2722 * generation bytes_allocated counters be updated. */
2723 page_table[i].gen = new_space;
2724 generations[new_space].bytes_allocated += page_table[i].bytes_used;
2725 generations[from_space].bytes_allocated -= page_table[i].bytes_used;
2727 /* It is essential that the pages are not write protected as
2728 * they may have pointers into the old-space which need
2729 * scavenging. They shouldn't be write protected at this
2731 gc_assert(!page_table[i].write_protected);
2733 /* Check whether this is the last page in this contiguous block.. */
2734 if ((page_table[i].bytes_used < PAGE_BYTES)
2735 /* ..or it is PAGE_BYTES and is the last in the block */
2736 || (page_table[i+1].allocated == FREE_PAGE_FLAG)
2737 || (page_table[i+1].bytes_used == 0) /* next page free */
2738 || (page_table[i+1].gen != from_space) /* diff. gen */
2739 || (page_table[i+1].first_object_offset == 0))
2743 /* Check that the page is now static. */
2744 gc_assert(page_table[addr_page_index].dont_move != 0);
2747 #endif // defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
2750 /* If the given page is not write-protected, then scan it for pointers
2751 * to younger generations or the top temp. generation, if no
2752 * suspicious pointers are found then the page is write-protected.
2754 * Care is taken to check for pointers to the current gc_alloc()
2755 * region if it is a younger generation or the temp. generation. This
2756 * frees the caller from doing a gc_alloc_update_page_tables(). Actually
2757 * the gc_alloc_generation does not need to be checked as this is only
2758 * called from scavenge_generation() when the gc_alloc generation is
2759 * younger, so it just checks if there is a pointer to the current
2762 * We return 1 if the page was write-protected, else 0. */
2764 update_page_write_prot(page_index_t page)
2766 generation_index_t gen = page_table[page].gen;
2769 void **page_addr = (void **)page_address(page);
2770 long num_words = page_table[page].bytes_used / N_WORD_BYTES;
2772 /* Shouldn't be a free page. */
2773 gc_assert(page_table[page].allocated != FREE_PAGE_FLAG);
2774 gc_assert(page_table[page].bytes_used != 0);
2776 /* Skip if it's already write-protected, pinned, or unboxed */
2777 if (page_table[page].write_protected
2778 /* FIXME: What's the reason for not write-protecting pinned pages? */
2779 || page_table[page].dont_move
2780 || (page_table[page].allocated & UNBOXED_PAGE_FLAG))
2783 /* Scan the page for pointers to younger generations or the
2784 * top temp. generation. */
2786 for (j = 0; j < num_words; j++) {
2787 void *ptr = *(page_addr+j);
2788 page_index_t index = find_page_index(ptr);
2790 /* Check that it's in the dynamic space */
2792 if (/* Does it point to a younger or the temp. generation? */
2793 ((page_table[index].allocated != FREE_PAGE_FLAG)
2794 && (page_table[index].bytes_used != 0)
2795 && ((page_table[index].gen < gen)
2796 || (page_table[index].gen == SCRATCH_GENERATION)))
2798 /* Or does it point within a current gc_alloc() region? */
2799 || ((boxed_region.start_addr <= ptr)
2800 && (ptr <= boxed_region.free_pointer))
2801 || ((unboxed_region.start_addr <= ptr)
2802 && (ptr <= unboxed_region.free_pointer))) {
2809 /* Write-protect the page. */
2810 /*FSHOW((stderr, "/write-protecting page %d gen %d\n", page, gen));*/
2812 os_protect((void *)page_addr,
2814 OS_VM_PROT_READ|OS_VM_PROT_EXECUTE);
2816 /* Note the page as protected in the page tables. */
2817 page_table[page].write_protected = 1;
2823 /* Scavenge all generations from FROM to TO, inclusive, except for
2824 * new_space which needs special handling, as new objects may be
2825 * added which are not checked here - use scavenge_newspace generation.
2827 * Write-protected pages should not have any pointers to the
2828 * from_space so do need scavenging; thus write-protected pages are
2829 * not always scavenged. There is some code to check that these pages
2830 * are not written; but to check fully the write-protected pages need
2831 * to be scavenged by disabling the code to skip them.
2833 * Under the current scheme when a generation is GCed the younger
2834 * generations will be empty. So, when a generation is being GCed it
2835 * is only necessary to scavenge the older generations for pointers
2836 * not the younger. So a page that does not have pointers to younger
2837 * generations does not need to be scavenged.
2839 * The write-protection can be used to note pages that don't have
2840 * pointers to younger pages. But pages can be written without having
2841 * pointers to younger generations. After the pages are scavenged here
2842 * they can be scanned for pointers to younger generations and if
2843 * there are none the page can be write-protected.
2845 * One complication is when the newspace is the top temp. generation.
2847 * Enabling SC_GEN_CK scavenges the write-protected pages and checks
2848 * that none were written, which they shouldn't be as they should have
2849 * no pointers to younger generations. This breaks down for weak
2850 * pointers as the objects contain a link to the next and are written
2851 * if a weak pointer is scavenged. Still it's a useful check. */
2853 scavenge_generations(generation_index_t from, generation_index_t to)
2860 /* Clear the write_protected_cleared flags on all pages. */
2861 for (i = 0; i < page_table_pages; i++)
2862 page_table[i].write_protected_cleared = 0;
2865 for (i = 0; i < last_free_page; i++) {
2866 generation_index_t generation = page_table[i].gen;
2867 if ((page_table[i].allocated & BOXED_PAGE_FLAG)
2868 && (page_table[i].bytes_used != 0)
2869 && (generation != new_space)
2870 && (generation >= from)
2871 && (generation <= to)) {
2872 page_index_t last_page,j;
2873 int write_protected=1;
2875 /* This should be the start of a region */
2876 gc_assert(page_table[i].first_object_offset == 0);
2878 /* Now work forward until the end of the region */
2879 for (last_page = i; ; last_page++) {
2881 write_protected && page_table[last_page].write_protected;
2882 if ((page_table[last_page].bytes_used < PAGE_BYTES)
2883 /* Or it is PAGE_BYTES and is the last in the block */
2884 || (!(page_table[last_page+1].allocated & BOXED_PAGE_FLAG))
2885 || (page_table[last_page+1].bytes_used == 0)
2886 || (page_table[last_page+1].gen != generation)
2887 || (page_table[last_page+1].first_object_offset == 0))
2890 if (!write_protected) {
2891 scavenge(page_address(i),
2892 (page_table[last_page].bytes_used
2893 + (last_page-i)*PAGE_BYTES)/N_WORD_BYTES);
2895 /* Now scan the pages and write protect those that
2896 * don't have pointers to younger generations. */
2897 if (enable_page_protection) {
2898 for (j = i; j <= last_page; j++) {
2899 num_wp += update_page_write_prot(j);
2902 if ((gencgc_verbose > 1) && (num_wp != 0)) {
2904 "/write protected %d pages within generation %d\n",
2905 num_wp, generation));
2913 /* Check that none of the write_protected pages in this generation
2914 * have been written to. */
2915 for (i = 0; i < page_table_pages; i++) {
2916 if ((page_table[i].allocation != FREE_PAGE_FLAG)
2917 && (page_table[i].bytes_used != 0)
2918 && (page_table[i].gen == generation)
2919 && (page_table[i].write_protected_cleared != 0)) {
2920 FSHOW((stderr, "/scavenge_generation() %d\n", generation));
2922 "/page bytes_used=%d first_object_offset=%d dont_move=%d\n",
2923 page_table[i].bytes_used,
2924 page_table[i].first_object_offset,
2925 page_table[i].dont_move));
2926 lose("write to protected page %d in scavenge_generation()\n", i);
2933 /* Scavenge a newspace generation. As it is scavenged new objects may
2934 * be allocated to it; these will also need to be scavenged. This
2935 * repeats until there are no more objects unscavenged in the
2936 * newspace generation.
2938 * To help improve the efficiency, areas written are recorded by
2939 * gc_alloc() and only these scavenged. Sometimes a little more will be
2940 * scavenged, but this causes no harm. An easy check is done that the
2941 * scavenged bytes equals the number allocated in the previous
2944 * Write-protected pages are not scanned except if they are marked
2945 * dont_move in which case they may have been promoted and still have
2946 * pointers to the from space.
2948 * Write-protected pages could potentially be written by alloc however
2949 * to avoid having to handle re-scavenging of write-protected pages
2950 * gc_alloc() does not write to write-protected pages.
2952 * New areas of objects allocated are recorded alternatively in the two
2953 * new_areas arrays below. */
2954 static struct new_area new_areas_1[NUM_NEW_AREAS];
2955 static struct new_area new_areas_2[NUM_NEW_AREAS];
2957 /* Do one full scan of the new space generation. This is not enough to
2958 * complete the job as new objects may be added to the generation in
2959 * the process which are not scavenged. */
2961 scavenge_newspace_generation_one_scan(generation_index_t generation)
2966 "/starting one full scan of newspace generation %d\n",
2968 for (i = 0; i < last_free_page; i++) {
2969 /* Note that this skips over open regions when it encounters them. */
2970 if ((page_table[i].allocated & BOXED_PAGE_FLAG)
2971 && (page_table[i].bytes_used != 0)
2972 && (page_table[i].gen == generation)
2973 && ((page_table[i].write_protected == 0)
2974 /* (This may be redundant as write_protected is now
2975 * cleared before promotion.) */
2976 || (page_table[i].dont_move == 1))) {
2977 page_index_t last_page;
2980 /* The scavenge will start at the first_object_offset of page i.
2982 * We need to find the full extent of this contiguous
2983 * block in case objects span pages.
2985 * Now work forward until the end of this contiguous area
2986 * is found. A small area is preferred as there is a
2987 * better chance of its pages being write-protected. */
2988 for (last_page = i; ;last_page++) {
2989 /* If all pages are write-protected and movable,
2990 * then no need to scavenge */
2991 all_wp=all_wp && page_table[last_page].write_protected &&
2992 !page_table[last_page].dont_move;
2994 /* Check whether this is the last page in this
2995 * contiguous block */
2996 if ((page_table[last_page].bytes_used < PAGE_BYTES)
2997 /* Or it is PAGE_BYTES and is the last in the block */
2998 || (!(page_table[last_page+1].allocated & BOXED_PAGE_FLAG))
2999 || (page_table[last_page+1].bytes_used == 0)
3000 || (page_table[last_page+1].gen != generation)
3001 || (page_table[last_page+1].first_object_offset == 0))
3005 /* Do a limited check for write-protected pages. */
3009 size = (page_table[last_page].bytes_used
3010 + (last_page-i)*PAGE_BYTES
3011 - page_table[i].first_object_offset)/N_WORD_BYTES;
3012 new_areas_ignore_page = last_page;
3014 scavenge(page_region_start(i), size);
3021 "/done with one full scan of newspace generation %d\n",
3025 /* Do a complete scavenge of the newspace generation. */
3027 scavenge_newspace_generation(generation_index_t generation)
3031 /* the new_areas array currently being written to by gc_alloc() */
3032 struct new_area (*current_new_areas)[] = &new_areas_1;
3033 long current_new_areas_index;
3035 /* the new_areas created by the previous scavenge cycle */
3036 struct new_area (*previous_new_areas)[] = NULL;
3037 long previous_new_areas_index;
3039 /* Flush the current regions updating the tables. */
3040 gc_alloc_update_all_page_tables();
3042 /* Turn on the recording of new areas by gc_alloc(). */
3043 new_areas = current_new_areas;
3044 new_areas_index = 0;
3046 /* Don't need to record new areas that get scavenged anyway during
3047 * scavenge_newspace_generation_one_scan. */
3048 record_new_objects = 1;
3050 /* Start with a full scavenge. */
3051 scavenge_newspace_generation_one_scan(generation);
3053 /* Record all new areas now. */
3054 record_new_objects = 2;
3056 /* Give a chance to weak hash tables to make other objects live.
3057 * FIXME: The algorithm implemented here for weak hash table gcing
3058 * is O(W^2+N) as Bruno Haible warns in
3059 * http://www.haible.de/bruno/papers/cs/weak/WeakDatastructures-writeup.html
3060 * see "Implementation 2". */
3061 scav_weak_hash_tables();
3063 /* Flush the current regions updating the tables. */
3064 gc_alloc_update_all_page_tables();
3066 /* Grab new_areas_index. */
3067 current_new_areas_index = new_areas_index;
3070 "The first scan is finished; current_new_areas_index=%d.\n",
3071 current_new_areas_index));*/
3073 while (current_new_areas_index > 0) {
3074 /* Move the current to the previous new areas */
3075 previous_new_areas = current_new_areas;
3076 previous_new_areas_index = current_new_areas_index;
3078 /* Scavenge all the areas in previous new areas. Any new areas
3079 * allocated are saved in current_new_areas. */
3081 /* Allocate an array for current_new_areas; alternating between
3082 * new_areas_1 and 2 */
3083 if (previous_new_areas == &new_areas_1)
3084 current_new_areas = &new_areas_2;
3086 current_new_areas = &new_areas_1;
3088 /* Set up for gc_alloc(). */
3089 new_areas = current_new_areas;
3090 new_areas_index = 0;
3092 /* Check whether previous_new_areas had overflowed. */
3093 if (previous_new_areas_index >= NUM_NEW_AREAS) {
3095 /* New areas of objects allocated have been lost so need to do a
3096 * full scan to be sure! If this becomes a problem try
3097 * increasing NUM_NEW_AREAS. */
3099 SHOW("new_areas overflow, doing full scavenge");
3101 /* Don't need to record new areas that get scavenged
3102 * anyway during scavenge_newspace_generation_one_scan. */
3103 record_new_objects = 1;
3105 scavenge_newspace_generation_one_scan(generation);
3107 /* Record all new areas now. */
3108 record_new_objects = 2;
3110 scav_weak_hash_tables();
3112 /* Flush the current regions updating the tables. */
3113 gc_alloc_update_all_page_tables();
3117 /* Work through previous_new_areas. */
3118 for (i = 0; i < previous_new_areas_index; i++) {
3119 long page = (*previous_new_areas)[i].page;
3120 long offset = (*previous_new_areas)[i].offset;
3121 long size = (*previous_new_areas)[i].size / N_WORD_BYTES;
3122 gc_assert((*previous_new_areas)[i].size % N_WORD_BYTES == 0);
3123 scavenge(page_address(page)+offset, size);
3126 scav_weak_hash_tables();
3128 /* Flush the current regions updating the tables. */
3129 gc_alloc_update_all_page_tables();
3132 current_new_areas_index = new_areas_index;
3135 "The re-scan has finished; current_new_areas_index=%d.\n",
3136 current_new_areas_index));*/
3139 /* Turn off recording of areas allocated by gc_alloc(). */
3140 record_new_objects = 0;
3143 /* Check that none of the write_protected pages in this generation
3144 * have been written to. */
3145 for (i = 0; i < page_table_pages; i++) {
3146 if ((page_table[i].allocation != FREE_PAGE_FLAG)
3147 && (page_table[i].bytes_used != 0)
3148 && (page_table[i].gen == generation)
3149 && (page_table[i].write_protected_cleared != 0)
3150 && (page_table[i].dont_move == 0)) {
3151 lose("write protected page %d written to in scavenge_newspace_generation\ngeneration=%d dont_move=%d\n",
3152 i, generation, page_table[i].dont_move);
3158 /* Un-write-protect all the pages in from_space. This is done at the
3159 * start of a GC else there may be many page faults while scavenging
3160 * the newspace (I've seen drive the system time to 99%). These pages
3161 * would need to be unprotected anyway before unmapping in
3162 * free_oldspace; not sure what effect this has on paging.. */
3164 unprotect_oldspace(void)
3168 for (i = 0; i < last_free_page; i++) {
3169 if ((page_table[i].allocated != FREE_PAGE_FLAG)
3170 && (page_table[i].bytes_used != 0)
3171 && (page_table[i].gen == from_space)) {
3174 page_start = (void *)page_address(i);
3176 /* Remove any write-protection. We should be able to rely
3177 * on the write-protect flag to avoid redundant calls. */
3178 if (page_table[i].write_protected) {
3179 os_protect(page_start, PAGE_BYTES, OS_VM_PROT_ALL);
3180 page_table[i].write_protected = 0;
3186 /* Work through all the pages and free any in from_space. This
3187 * assumes that all objects have been copied or promoted to an older
3188 * generation. Bytes_allocated and the generation bytes_allocated
3189 * counter are updated. The number of bytes freed is returned. */
3193 long bytes_freed = 0;
3194 page_index_t first_page, last_page;
3199 /* Find a first page for the next region of pages. */
3200 while ((first_page < last_free_page)
3201 && ((page_table[first_page].allocated == FREE_PAGE_FLAG)
3202 || (page_table[first_page].bytes_used == 0)
3203 || (page_table[first_page].gen != from_space)))
3206 if (first_page >= last_free_page)
3209 /* Find the last page of this region. */
3210 last_page = first_page;
3213 /* Free the page. */
3214 bytes_freed += page_table[last_page].bytes_used;
3215 generations[page_table[last_page].gen].bytes_allocated -=
3216 page_table[last_page].bytes_used;
3217 page_table[last_page].allocated = FREE_PAGE_FLAG;
3218 page_table[last_page].bytes_used = 0;
3220 /* Remove any write-protection. We should be able to rely
3221 * on the write-protect flag to avoid redundant calls. */
3223 void *page_start = (void *)page_address(last_page);
3225 if (page_table[last_page].write_protected) {
3226 os_protect(page_start, PAGE_BYTES, OS_VM_PROT_ALL);
3227 page_table[last_page].write_protected = 0;
3232 while ((last_page < last_free_page)
3233 && (page_table[last_page].allocated != FREE_PAGE_FLAG)
3234 && (page_table[last_page].bytes_used != 0)
3235 && (page_table[last_page].gen == from_space));
3237 #ifdef READ_PROTECT_FREE_PAGES
3238 os_protect(page_address(first_page),
3239 PAGE_BYTES*(last_page-first_page),
3242 first_page = last_page;
3243 } while (first_page < last_free_page);
3245 bytes_allocated -= bytes_freed;
3250 /* Print some information about a pointer at the given address. */
3252 print_ptr(lispobj *addr)
3254 /* If addr is in the dynamic space then out the page information. */
3255 page_index_t pi1 = find_page_index((void*)addr);
3258 fprintf(stderr," %x: page %d alloc %d gen %d bytes_used %d offset %d dont_move %d\n",
3259 (unsigned long) addr,
3261 page_table[pi1].allocated,
3262 page_table[pi1].gen,
3263 page_table[pi1].bytes_used,
3264 page_table[pi1].first_object_offset,
3265 page_table[pi1].dont_move);
3266 fprintf(stderr," %x %x %x %x (%x) %x %x %x %x\n",
3280 verify_space(lispobj *start, size_t words)
3282 int is_in_dynamic_space = (find_page_index((void*)start) != -1);
3283 int is_in_readonly_space =
3284 (READ_ONLY_SPACE_START <= (unsigned long)start &&
3285 (unsigned long)start < SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0));
3289 lispobj thing = *(lispobj*)start;
3291 if (is_lisp_pointer(thing)) {
3292 page_index_t page_index = find_page_index((void*)thing);
3293 long to_readonly_space =
3294 (READ_ONLY_SPACE_START <= thing &&
3295 thing < SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0));
3296 long to_static_space =
3297 (STATIC_SPACE_START <= thing &&
3298 thing < SymbolValue(STATIC_SPACE_FREE_POINTER,0));
3300 /* Does it point to the dynamic space? */
3301 if (page_index != -1) {
3302 /* If it's within the dynamic space it should point to a used
3303 * page. XX Could check the offset too. */
3304 if ((page_table[page_index].allocated != FREE_PAGE_FLAG)
3305 && (page_table[page_index].bytes_used == 0))
3306 lose ("Ptr %x @ %x sees free page.\n", thing, start);
3307 /* Check that it doesn't point to a forwarding pointer! */
3308 if (*((lispobj *)native_pointer(thing)) == 0x01) {
3309 lose("Ptr %x @ %x sees forwarding ptr.\n", thing, start);
3311 /* Check that its not in the RO space as it would then be a
3312 * pointer from the RO to the dynamic space. */
3313 if (is_in_readonly_space) {
3314 lose("ptr to dynamic space %x from RO space %x\n",
3317 /* Does it point to a plausible object? This check slows
3318 * it down a lot (so it's commented out).
3320 * "a lot" is serious: it ate 50 minutes cpu time on
3321 * my duron 950 before I came back from lunch and
3324 * FIXME: Add a variable to enable this
3327 if (!possibly_valid_dynamic_space_pointer((lispobj *)thing)) {
3328 lose("ptr %x to invalid object %x\n", thing, start);
3332 /* Verify that it points to another valid space. */
3333 if (!to_readonly_space && !to_static_space) {
3334 lose("Ptr %x @ %x sees junk.\n", thing, start);
3338 if (!(fixnump(thing))) {
3340 switch(widetag_of(*start)) {
3343 case SIMPLE_VECTOR_WIDETAG:
3345 case COMPLEX_WIDETAG:
3346 case SIMPLE_ARRAY_WIDETAG:
3347 case COMPLEX_BASE_STRING_WIDETAG:
3348 #ifdef COMPLEX_CHARACTER_STRING_WIDETAG
3349 case COMPLEX_CHARACTER_STRING_WIDETAG:
3351 case COMPLEX_VECTOR_NIL_WIDETAG:
3352 case COMPLEX_BIT_VECTOR_WIDETAG:
3353 case COMPLEX_VECTOR_WIDETAG:
3354 case COMPLEX_ARRAY_WIDETAG:
3355 case CLOSURE_HEADER_WIDETAG:
3356 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
3357 case VALUE_CELL_HEADER_WIDETAG:
3358 case SYMBOL_HEADER_WIDETAG:
3359 case CHARACTER_WIDETAG:
3360 #if N_WORD_BITS == 64
3361 case SINGLE_FLOAT_WIDETAG:
3363 case UNBOUND_MARKER_WIDETAG:
3368 case INSTANCE_HEADER_WIDETAG:
3371 long ntotal = HeaderValue(thing);
3372 lispobj layout = ((struct instance *)start)->slots[0];
3377 nuntagged = ((struct layout *)
3378 native_pointer(layout))->n_untagged_slots;
3379 verify_space(start + 1,
3380 ntotal - fixnum_value(nuntagged));
3384 case CODE_HEADER_WIDETAG:
3386 lispobj object = *start;
3388 long nheader_words, ncode_words, nwords;
3390 struct simple_fun *fheaderp;
3392 code = (struct code *) start;
3394 /* Check that it's not in the dynamic space.
3395 * FIXME: Isn't is supposed to be OK for code
3396 * objects to be in the dynamic space these days? */
3397 if (is_in_dynamic_space
3398 /* It's ok if it's byte compiled code. The trace
3399 * table offset will be a fixnum if it's x86
3400 * compiled code - check.
3402 * FIXME: #^#@@! lack of abstraction here..
3403 * This line can probably go away now that
3404 * there's no byte compiler, but I've got
3405 * too much to worry about right now to try
3406 * to make sure. -- WHN 2001-10-06 */
3407 && fixnump(code->trace_table_offset)
3408 /* Only when enabled */
3409 && verify_dynamic_code_check) {
3411 "/code object at %x in the dynamic space\n",
3415 ncode_words = fixnum_value(code->code_size);
3416 nheader_words = HeaderValue(object);
3417 nwords = ncode_words + nheader_words;
3418 nwords = CEILING(nwords, 2);
3419 /* Scavenge the boxed section of the code data block */
3420 verify_space(start + 1, nheader_words - 1);
3422 /* Scavenge the boxed section of each function
3423 * object in the code data block. */
3424 fheaderl = code->entry_points;
3425 while (fheaderl != NIL) {
3427 (struct simple_fun *) native_pointer(fheaderl);
3428 gc_assert(widetag_of(fheaderp->header) ==
3429 SIMPLE_FUN_HEADER_WIDETAG);
3430 verify_space(&fheaderp->name, 1);
3431 verify_space(&fheaderp->arglist, 1);
3432 verify_space(&fheaderp->type, 1);
3433 fheaderl = fheaderp->next;
3439 /* unboxed objects */
3440 case BIGNUM_WIDETAG:
3441 #if N_WORD_BITS != 64
3442 case SINGLE_FLOAT_WIDETAG:
3444 case DOUBLE_FLOAT_WIDETAG:
3445 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
3446 case LONG_FLOAT_WIDETAG:
3448 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
3449 case COMPLEX_SINGLE_FLOAT_WIDETAG:
3451 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
3452 case COMPLEX_DOUBLE_FLOAT_WIDETAG:
3454 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
3455 case COMPLEX_LONG_FLOAT_WIDETAG:
3457 case SIMPLE_BASE_STRING_WIDETAG:
3458 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
3459 case SIMPLE_CHARACTER_STRING_WIDETAG:
3461 case SIMPLE_BIT_VECTOR_WIDETAG:
3462 case SIMPLE_ARRAY_NIL_WIDETAG:
3463 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
3464 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
3465 case SIMPLE_ARRAY_UNSIGNED_BYTE_7_WIDETAG:
3466 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
3467 case SIMPLE_ARRAY_UNSIGNED_BYTE_15_WIDETAG:
3468 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
3469 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_29_WIDETAG
3470 case SIMPLE_ARRAY_UNSIGNED_BYTE_29_WIDETAG:
3472 case SIMPLE_ARRAY_UNSIGNED_BYTE_31_WIDETAG:
3473 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
3474 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_60_WIDETAG
3475 case SIMPLE_ARRAY_UNSIGNED_BYTE_60_WIDETAG:
3477 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG
3478 case SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG:
3480 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG
3481 case SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG:
3483 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
3484 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
3486 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
3487 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
3489 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
3490 case SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG:
3492 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
3493 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
3495 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_61_WIDETAG
3496 case SIMPLE_ARRAY_SIGNED_BYTE_61_WIDETAG:
3498 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG
3499 case SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG:
3501 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
3502 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
3503 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
3504 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
3506 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
3507 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
3509 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
3510 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
3512 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
3513 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
3516 case WEAK_POINTER_WIDETAG:
3517 #ifdef LUTEX_WIDETAG
3520 #ifdef NO_TLS_VALUE_MARKER_WIDETAG
3521 case NO_TLS_VALUE_MARKER_WIDETAG:
3523 count = (sizetab[widetag_of(*start)])(start);
3527 lose("Unhandled widetag 0x%x at 0x%x\n",
3528 widetag_of(*start), start);
3540 /* FIXME: It would be nice to make names consistent so that
3541 * foo_size meant size *in* *bytes* instead of size in some
3542 * arbitrary units. (Yes, this caused a bug, how did you guess?:-)
3543 * Some counts of lispobjs are called foo_count; it might be good
3544 * to grep for all foo_size and rename the appropriate ones to
3546 long read_only_space_size =
3547 (lispobj*)SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0)
3548 - (lispobj*)READ_ONLY_SPACE_START;
3549 long static_space_size =
3550 (lispobj*)SymbolValue(STATIC_SPACE_FREE_POINTER,0)
3551 - (lispobj*)STATIC_SPACE_START;
3553 for_each_thread(th) {
3554 long binding_stack_size =
3555 (lispobj*)get_binding_stack_pointer(th)
3556 - (lispobj*)th->binding_stack_start;
3557 verify_space(th->binding_stack_start, binding_stack_size);
3559 verify_space((lispobj*)READ_ONLY_SPACE_START, read_only_space_size);
3560 verify_space((lispobj*)STATIC_SPACE_START , static_space_size);
3564 verify_generation(generation_index_t generation)
3568 for (i = 0; i < last_free_page; i++) {
3569 if ((page_table[i].allocated != FREE_PAGE_FLAG)
3570 && (page_table[i].bytes_used != 0)
3571 && (page_table[i].gen == generation)) {
3572 page_index_t last_page;
3573 int region_allocation = page_table[i].allocated;
3575 /* This should be the start of a contiguous block */
3576 gc_assert(page_table[i].first_object_offset == 0);
3578 /* Need to find the full extent of this contiguous block in case
3579 objects span pages. */
3581 /* Now work forward until the end of this contiguous area is
3583 for (last_page = i; ;last_page++)
3584 /* Check whether this is the last page in this contiguous
3586 if ((page_table[last_page].bytes_used < PAGE_BYTES)
3587 /* Or it is PAGE_BYTES and is the last in the block */
3588 || (page_table[last_page+1].allocated != region_allocation)
3589 || (page_table[last_page+1].bytes_used == 0)
3590 || (page_table[last_page+1].gen != generation)
3591 || (page_table[last_page+1].first_object_offset == 0))
3594 verify_space(page_address(i),
3595 (page_table[last_page].bytes_used
3596 + (last_page-i)*PAGE_BYTES)/N_WORD_BYTES);
3602 /* Check that all the free space is zero filled. */
3604 verify_zero_fill(void)
3608 for (page = 0; page < last_free_page; page++) {
3609 if (page_table[page].allocated == FREE_PAGE_FLAG) {
3610 /* The whole page should be zero filled. */
3611 long *start_addr = (long *)page_address(page);
3614 for (i = 0; i < size; i++) {
3615 if (start_addr[i] != 0) {
3616 lose("free page not zero at %x\n", start_addr + i);
3620 long free_bytes = PAGE_BYTES - page_table[page].bytes_used;
3621 if (free_bytes > 0) {
3622 long *start_addr = (long *)((unsigned long)page_address(page)
3623 + page_table[page].bytes_used);
3624 long size = free_bytes / N_WORD_BYTES;
3626 for (i = 0; i < size; i++) {
3627 if (start_addr[i] != 0) {
3628 lose("free region not zero at %x\n", start_addr + i);
3636 /* External entry point for verify_zero_fill */
3638 gencgc_verify_zero_fill(void)
3640 /* Flush the alloc regions updating the tables. */
3641 gc_alloc_update_all_page_tables();
3642 SHOW("verifying zero fill");
3647 verify_dynamic_space(void)
3649 generation_index_t i;
3651 for (i = 0; i <= HIGHEST_NORMAL_GENERATION; i++)
3652 verify_generation(i);
3654 if (gencgc_enable_verify_zero_fill)
3658 /* Write-protect all the dynamic boxed pages in the given generation. */
3660 write_protect_generation_pages(generation_index_t generation)
3664 gc_assert(generation < SCRATCH_GENERATION);
3666 for (start = 0; start < last_free_page; start++) {
3667 if ((page_table[start].allocated == BOXED_PAGE_FLAG)
3668 && (page_table[start].bytes_used != 0)
3669 && !page_table[start].dont_move
3670 && (page_table[start].gen == generation)) {
3674 /* Note the page as protected in the page tables. */
3675 page_table[start].write_protected = 1;
3677 for (last = start + 1; last < last_free_page; last++) {
3678 if ((page_table[last].allocated != BOXED_PAGE_FLAG)
3679 || (page_table[last].bytes_used == 0)
3680 || page_table[last].dont_move
3681 || (page_table[last].gen != generation))
3683 page_table[last].write_protected = 1;
3686 page_start = (void *)page_address(start);
3688 os_protect(page_start,
3689 PAGE_BYTES * (last - start),
3690 OS_VM_PROT_READ | OS_VM_PROT_EXECUTE);
3696 if (gencgc_verbose > 1) {
3698 "/write protected %d of %d pages in generation %d\n",
3699 count_write_protect_generation_pages(generation),
3700 count_generation_pages(generation),
3705 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
3708 scavenge_control_stack()
3710 unsigned long control_stack_size;
3712 /* This is going to be a big problem when we try to port threads
3714 struct thread *th = arch_os_get_current_thread();
3715 lispobj *control_stack =
3716 (lispobj *)(th->control_stack_start);
3718 control_stack_size = current_control_stack_pointer - control_stack;
3719 scavenge(control_stack, control_stack_size);
3722 /* Scavenging Interrupt Contexts */
3724 static int boxed_registers[] = BOXED_REGISTERS;
3727 scavenge_interrupt_context(os_context_t * context)
3733 unsigned long lip_offset;
3734 int lip_register_pair;
3736 unsigned long pc_code_offset;
3738 #ifdef ARCH_HAS_LINK_REGISTER
3739 unsigned long lr_code_offset;
3741 #ifdef ARCH_HAS_NPC_REGISTER
3742 unsigned long npc_code_offset;
3746 /* Find the LIP's register pair and calculate it's offset */
3747 /* before we scavenge the context. */
3750 * I (RLT) think this is trying to find the boxed register that is
3751 * closest to the LIP address, without going past it. Usually, it's
3752 * reg_CODE or reg_LRA. But sometimes, nothing can be found.
3754 lip = *os_context_register_addr(context, reg_LIP);
3755 lip_offset = 0x7FFFFFFF;
3756 lip_register_pair = -1;
3757 for (i = 0; i < (sizeof(boxed_registers) / sizeof(int)); i++) {
3762 index = boxed_registers[i];
3763 reg = *os_context_register_addr(context, index);
3764 if ((reg & ~((1L<<N_LOWTAG_BITS)-1)) <= lip) {
3766 if (offset < lip_offset) {
3767 lip_offset = offset;
3768 lip_register_pair = index;
3772 #endif /* reg_LIP */
3774 /* Compute the PC's offset from the start of the CODE */
3776 pc_code_offset = *os_context_pc_addr(context)
3777 - *os_context_register_addr(context, reg_CODE);
3778 #ifdef ARCH_HAS_NPC_REGISTER
3779 npc_code_offset = *os_context_npc_addr(context)
3780 - *os_context_register_addr(context, reg_CODE);
3781 #endif /* ARCH_HAS_NPC_REGISTER */
3783 #ifdef ARCH_HAS_LINK_REGISTER
3785 *os_context_lr_addr(context) -
3786 *os_context_register_addr(context, reg_CODE);
3789 /* Scanvenge all boxed registers in the context. */
3790 for (i = 0; i < (sizeof(boxed_registers) / sizeof(int)); i++) {
3794 index = boxed_registers[i];
3795 foo = *os_context_register_addr(context, index);
3797 *os_context_register_addr(context, index) = foo;
3799 scavenge((lispobj*) &(*os_context_register_addr(context, index)), 1);
3806 * But what happens if lip_register_pair is -1?
3807 * *os_context_register_addr on Solaris (see
3808 * solaris_register_address in solaris-os.c) will return
3809 * &context->uc_mcontext.gregs[2]. But gregs[2] is REG_nPC. Is
3810 * that what we really want? My guess is that that is not what we
3811 * want, so if lip_register_pair is -1, we don't touch reg_LIP at
3812 * all. But maybe it doesn't really matter if LIP is trashed?
3814 if (lip_register_pair >= 0) {
3815 *os_context_register_addr(context, reg_LIP) =
3816 *os_context_register_addr(context, lip_register_pair)
3819 #endif /* reg_LIP */
3821 /* Fix the PC if it was in from space */
3822 if (from_space_p(*os_context_pc_addr(context)))
3823 *os_context_pc_addr(context) =
3824 *os_context_register_addr(context, reg_CODE) + pc_code_offset;
3826 #ifdef ARCH_HAS_LINK_REGISTER
3827 /* Fix the LR ditto; important if we're being called from
3828 * an assembly routine that expects to return using blr, otherwise
3830 if (from_space_p(*os_context_lr_addr(context)))
3831 *os_context_lr_addr(context) =
3832 *os_context_register_addr(context, reg_CODE) + lr_code_offset;
3835 #ifdef ARCH_HAS_NPC_REGISTER
3836 if (from_space_p(*os_context_npc_addr(context)))
3837 *os_context_npc_addr(context) =
3838 *os_context_register_addr(context, reg_CODE) + npc_code_offset;
3839 #endif /* ARCH_HAS_NPC_REGISTER */
3843 scavenge_interrupt_contexts(void)
3846 os_context_t *context;
3848 struct thread *th=arch_os_get_current_thread();
3850 index = fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,0));
3852 #if defined(DEBUG_PRINT_CONTEXT_INDEX)
3853 printf("Number of active contexts: %d\n", index);
3856 for (i = 0; i < index; i++) {
3857 context = th->interrupt_contexts[i];
3858 scavenge_interrupt_context(context);
3864 #if defined(LISP_FEATURE_SB_THREAD)
3866 preserve_context_registers (os_context_t *c)
3869 /* On Darwin the signal context isn't a contiguous block of memory,
3870 * so just preserve_pointering its contents won't be sufficient.
3872 #if defined(LISP_FEATURE_DARWIN)
3873 #if defined LISP_FEATURE_X86
3874 preserve_pointer((void*)*os_context_register_addr(c,reg_EAX));
3875 preserve_pointer((void*)*os_context_register_addr(c,reg_ECX));
3876 preserve_pointer((void*)*os_context_register_addr(c,reg_EDX));
3877 preserve_pointer((void*)*os_context_register_addr(c,reg_EBX));
3878 preserve_pointer((void*)*os_context_register_addr(c,reg_ESI));
3879 preserve_pointer((void*)*os_context_register_addr(c,reg_EDI));
3880 preserve_pointer((void*)*os_context_pc_addr(c));
3881 #elif defined LISP_FEATURE_X86_64
3882 preserve_pointer((void*)*os_context_register_addr(c,reg_RAX));
3883 preserve_pointer((void*)*os_context_register_addr(c,reg_RCX));
3884 preserve_pointer((void*)*os_context_register_addr(c,reg_RDX));
3885 preserve_pointer((void*)*os_context_register_addr(c,reg_RBX));
3886 preserve_pointer((void*)*os_context_register_addr(c,reg_RSI));
3887 preserve_pointer((void*)*os_context_register_addr(c,reg_RDI));
3888 preserve_pointer((void*)*os_context_register_addr(c,reg_R8));
3889 preserve_pointer((void*)*os_context_register_addr(c,reg_R9));
3890 preserve_pointer((void*)*os_context_register_addr(c,reg_R10));
3891 preserve_pointer((void*)*os_context_register_addr(c,reg_R11));
3892 preserve_pointer((void*)*os_context_register_addr(c,reg_R12));
3893 preserve_pointer((void*)*os_context_register_addr(c,reg_R13));
3894 preserve_pointer((void*)*os_context_register_addr(c,reg_R14));
3895 preserve_pointer((void*)*os_context_register_addr(c,reg_R15));
3896 preserve_pointer((void*)*os_context_pc_addr(c));
3898 #error "preserve_context_registers needs to be tweaked for non-x86 Darwin"
3901 for(ptr = ((void **)(c+1))-1; ptr>=(void **)c; ptr--) {
3902 preserve_pointer(*ptr);
3907 /* Garbage collect a generation. If raise is 0 then the remains of the
3908 * generation are not raised to the next generation. */
3910 garbage_collect_generation(generation_index_t generation, int raise)
3912 unsigned long bytes_freed;
3914 unsigned long static_space_size;
3915 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
3918 gc_assert(generation <= HIGHEST_NORMAL_GENERATION);
3920 /* The oldest generation can't be raised. */
3921 gc_assert((generation != HIGHEST_NORMAL_GENERATION) || (raise == 0));
3923 /* Check if weak hash tables were processed in the previous GC. */
3924 gc_assert(weak_hash_tables == NULL);
3926 /* Initialize the weak pointer list. */
3927 weak_pointers = NULL;
3929 #ifdef LUTEX_WIDETAG
3930 unmark_lutexes(generation);
3933 /* When a generation is not being raised it is transported to a
3934 * temporary generation (NUM_GENERATIONS), and lowered when
3935 * done. Set up this new generation. There should be no pages
3936 * allocated to it yet. */
3938 gc_assert(generations[SCRATCH_GENERATION].bytes_allocated == 0);
3941 /* Set the global src and dest. generations */
3942 from_space = generation;
3944 new_space = generation+1;
3946 new_space = SCRATCH_GENERATION;
3948 /* Change to a new space for allocation, resetting the alloc_start_page */
3949 gc_alloc_generation = new_space;
3950 generations[new_space].alloc_start_page = 0;
3951 generations[new_space].alloc_unboxed_start_page = 0;
3952 generations[new_space].alloc_large_start_page = 0;
3953 generations[new_space].alloc_large_unboxed_start_page = 0;
3955 /* Before any pointers are preserved, the dont_move flags on the
3956 * pages need to be cleared. */
3957 for (i = 0; i < last_free_page; i++)
3958 if(page_table[i].gen==from_space)
3959 page_table[i].dont_move = 0;
3961 /* Un-write-protect the old-space pages. This is essential for the
3962 * promoted pages as they may contain pointers into the old-space
3963 * which need to be scavenged. It also helps avoid unnecessary page
3964 * faults as forwarding pointers are written into them. They need to
3965 * be un-protected anyway before unmapping later. */
3966 unprotect_oldspace();
3968 /* Scavenge the stacks' conservative roots. */
3970 /* there are potentially two stacks for each thread: the main
3971 * stack, which may contain Lisp pointers, and the alternate stack.
3972 * We don't ever run Lisp code on the altstack, but it may
3973 * host a sigcontext with lisp objects in it */
3975 /* what we need to do: (1) find the stack pointer for the main
3976 * stack; scavenge it (2) find the interrupt context on the
3977 * alternate stack that might contain lisp values, and scavenge
3980 /* we assume that none of the preceding applies to the thread that
3981 * initiates GC. If you ever call GC from inside an altstack
3982 * handler, you will lose. */
3984 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
3985 /* And if we're saving a core, there's no point in being conservative. */
3986 if (conservative_stack) {
3987 for_each_thread(th) {
3989 void **esp=(void **)-1;
3990 #ifdef LISP_FEATURE_SB_THREAD
3992 if(th==arch_os_get_current_thread()) {
3993 /* Somebody is going to burn in hell for this, but casting
3994 * it in two steps shuts gcc up about strict aliasing. */
3995 esp = (void **)((void *)&raise);
3998 free=fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,th));
3999 for(i=free-1;i>=0;i--) {
4000 os_context_t *c=th->interrupt_contexts[i];
4001 esp1 = (void **) *os_context_register_addr(c,reg_SP);
4002 if (esp1>=(void **)th->control_stack_start &&
4003 esp1<(void **)th->control_stack_end) {
4004 if(esp1<esp) esp=esp1;
4005 preserve_context_registers(c);
4010 esp = (void **)((void *)&raise);
4012 for (ptr = ((void **)th->control_stack_end)-1; ptr >= esp; ptr--) {
4013 preserve_pointer(*ptr);
4020 if (gencgc_verbose > 1) {
4021 long num_dont_move_pages = count_dont_move_pages();
4023 "/non-movable pages due to conservative pointers = %d (%d bytes)\n",
4024 num_dont_move_pages,
4025 num_dont_move_pages * PAGE_BYTES);
4029 /* Scavenge all the rest of the roots. */
4031 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
4033 * If not x86, we need to scavenge the interrupt context(s) and the
4036 scavenge_interrupt_contexts();
4037 scavenge_control_stack();
4040 /* Scavenge the Lisp functions of the interrupt handlers, taking
4041 * care to avoid SIG_DFL and SIG_IGN. */
4042 for (i = 0; i < NSIG; i++) {
4043 union interrupt_handler handler = interrupt_handlers[i];
4044 if (!ARE_SAME_HANDLER(handler.c, SIG_IGN) &&
4045 !ARE_SAME_HANDLER(handler.c, SIG_DFL)) {
4046 scavenge((lispobj *)(interrupt_handlers + i), 1);
4049 /* Scavenge the binding stacks. */
4052 for_each_thread(th) {
4053 long len= (lispobj *)get_binding_stack_pointer(th) -
4054 th->binding_stack_start;
4055 scavenge((lispobj *) th->binding_stack_start,len);
4056 #ifdef LISP_FEATURE_SB_THREAD
4057 /* do the tls as well */
4058 len=fixnum_value(SymbolValue(FREE_TLS_INDEX,0)) -
4059 (sizeof (struct thread))/(sizeof (lispobj));
4060 scavenge((lispobj *) (th+1),len);
4065 /* The original CMU CL code had scavenge-read-only-space code
4066 * controlled by the Lisp-level variable
4067 * *SCAVENGE-READ-ONLY-SPACE*. It was disabled by default, and it
4068 * wasn't documented under what circumstances it was useful or
4069 * safe to turn it on, so it's been turned off in SBCL. If you
4070 * want/need this functionality, and can test and document it,
4071 * please submit a patch. */
4073 if (SymbolValue(SCAVENGE_READ_ONLY_SPACE) != NIL) {
4074 unsigned long read_only_space_size =
4075 (lispobj*)SymbolValue(READ_ONLY_SPACE_FREE_POINTER) -
4076 (lispobj*)READ_ONLY_SPACE_START;
4078 "/scavenge read only space: %d bytes\n",
4079 read_only_space_size * sizeof(lispobj)));
4080 scavenge( (lispobj *) READ_ONLY_SPACE_START, read_only_space_size);
4084 /* Scavenge static space. */
4086 (lispobj *)SymbolValue(STATIC_SPACE_FREE_POINTER,0) -
4087 (lispobj *)STATIC_SPACE_START;
4088 if (gencgc_verbose > 1) {
4090 "/scavenge static space: %d bytes\n",
4091 static_space_size * sizeof(lispobj)));
4093 scavenge( (lispobj *) STATIC_SPACE_START, static_space_size);
4095 /* All generations but the generation being GCed need to be
4096 * scavenged. The new_space generation needs special handling as
4097 * objects may be moved in - it is handled separately below. */
4098 scavenge_generations(generation+1, PSEUDO_STATIC_GENERATION);
4100 /* Finally scavenge the new_space generation. Keep going until no
4101 * more objects are moved into the new generation */
4102 scavenge_newspace_generation(new_space);
4104 /* FIXME: I tried reenabling this check when debugging unrelated
4105 * GC weirdness ca. sbcl-0.6.12.45, and it failed immediately.
4106 * Since the current GC code seems to work well, I'm guessing that
4107 * this debugging code is just stale, but I haven't tried to
4108 * figure it out. It should be figured out and then either made to
4109 * work or just deleted. */
4110 #define RESCAN_CHECK 0
4112 /* As a check re-scavenge the newspace once; no new objects should
4115 long old_bytes_allocated = bytes_allocated;
4116 long bytes_allocated;
4118 /* Start with a full scavenge. */
4119 scavenge_newspace_generation_one_scan(new_space);
4121 /* Flush the current regions, updating the tables. */
4122 gc_alloc_update_all_page_tables();
4124 bytes_allocated = bytes_allocated - old_bytes_allocated;
4126 if (bytes_allocated != 0) {
4127 lose("Rescan of new_space allocated %d more bytes.\n",
4133 scan_weak_hash_tables();
4134 scan_weak_pointers();
4136 /* Flush the current regions, updating the tables. */
4137 gc_alloc_update_all_page_tables();
4139 /* Free the pages in oldspace, but not those marked dont_move. */
4140 bytes_freed = free_oldspace();
4142 /* If the GC is not raising the age then lower the generation back
4143 * to its normal generation number */
4145 for (i = 0; i < last_free_page; i++)
4146 if ((page_table[i].bytes_used != 0)
4147 && (page_table[i].gen == SCRATCH_GENERATION))
4148 page_table[i].gen = generation;
4149 gc_assert(generations[generation].bytes_allocated == 0);
4150 generations[generation].bytes_allocated =
4151 generations[SCRATCH_GENERATION].bytes_allocated;
4152 generations[SCRATCH_GENERATION].bytes_allocated = 0;
4155 /* Reset the alloc_start_page for generation. */
4156 generations[generation].alloc_start_page = 0;
4157 generations[generation].alloc_unboxed_start_page = 0;
4158 generations[generation].alloc_large_start_page = 0;
4159 generations[generation].alloc_large_unboxed_start_page = 0;
4161 if (generation >= verify_gens) {
4165 verify_dynamic_space();
4168 /* Set the new gc trigger for the GCed generation. */
4169 generations[generation].gc_trigger =
4170 generations[generation].bytes_allocated
4171 + generations[generation].bytes_consed_between_gc;
4174 generations[generation].num_gc = 0;
4176 ++generations[generation].num_gc;
4178 #ifdef LUTEX_WIDETAG
4179 reap_lutexes(generation);
4181 move_lutexes(generation, generation+1);
4185 /* Update last_free_page, then SymbolValue(ALLOCATION_POINTER). */
4187 update_dynamic_space_free_pointer(void)
4189 page_index_t last_page = -1, i;
4191 for (i = 0; i < last_free_page; i++)
4192 if ((page_table[i].allocated != FREE_PAGE_FLAG)
4193 && (page_table[i].bytes_used != 0))
4196 last_free_page = last_page+1;
4198 set_alloc_pointer((lispobj)(((char *)heap_base)
4199 + last_free_page*PAGE_BYTES));
4200 return 0; /* dummy value: return something ... */
4204 remap_free_pages (page_index_t from, page_index_t to)
4206 page_index_t first_page, last_page;
4208 for (first_page = from; first_page <= to; first_page++) {
4209 if (page_table[first_page].allocated != FREE_PAGE_FLAG ||
4210 page_table[first_page].need_to_zero == 0) {
4214 last_page = first_page + 1;
4215 while (page_table[last_page].allocated == FREE_PAGE_FLAG &&
4217 page_table[last_page].need_to_zero == 1) {
4221 /* There's a mysterious Solaris/x86 problem with using mmap
4222 * tricks for memory zeroing. See sbcl-devel thread
4223 * "Re: patch: standalone executable redux".
4225 #if defined(LISP_FEATURE_SUNOS)
4226 zero_pages(first_page, last_page-1);
4228 zero_pages_with_mmap(first_page, last_page-1);
4231 first_page = last_page;
4235 generation_index_t small_generation_limit = 1;
4237 /* GC all generations newer than last_gen, raising the objects in each
4238 * to the next older generation - we finish when all generations below
4239 * last_gen are empty. Then if last_gen is due for a GC, or if
4240 * last_gen==NUM_GENERATIONS (the scratch generation? eh?) we GC that
4241 * too. The valid range for last_gen is: 0,1,...,NUM_GENERATIONS.
4243 * We stop collecting at gencgc_oldest_gen_to_gc, even if this is less than
4244 * last_gen (oh, and note that by default it is NUM_GENERATIONS-1) */
4246 collect_garbage(generation_index_t last_gen)
4248 generation_index_t gen = 0, i;
4251 /* The largest value of last_free_page seen since the time
4252 * remap_free_pages was called. */
4253 static page_index_t high_water_mark = 0;
4255 FSHOW((stderr, "/entering collect_garbage(%d)\n", last_gen));
4259 if (last_gen > HIGHEST_NORMAL_GENERATION+1) {
4261 "/collect_garbage: last_gen = %d, doing a level 0 GC\n",
4266 /* Flush the alloc regions updating the tables. */
4267 gc_alloc_update_all_page_tables();
4269 /* Verify the new objects created by Lisp code. */
4270 if (pre_verify_gen_0) {
4271 FSHOW((stderr, "pre-checking generation 0\n"));
4272 verify_generation(0);
4275 if (gencgc_verbose > 1)
4276 print_generation_stats(0);
4279 /* Collect the generation. */
4281 if (gen >= gencgc_oldest_gen_to_gc) {
4282 /* Never raise the oldest generation. */
4287 || (generations[gen].num_gc >= generations[gen].trigger_age);
4290 if (gencgc_verbose > 1) {
4292 "starting GC of generation %d with raise=%d alloc=%d trig=%d GCs=%d\n",
4295 generations[gen].bytes_allocated,
4296 generations[gen].gc_trigger,
4297 generations[gen].num_gc));
4300 /* If an older generation is being filled, then update its
4303 generations[gen+1].cum_sum_bytes_allocated +=
4304 generations[gen+1].bytes_allocated;
4307 garbage_collect_generation(gen, raise);
4309 /* Reset the memory age cum_sum. */
4310 generations[gen].cum_sum_bytes_allocated = 0;
4312 if (gencgc_verbose > 1) {
4313 FSHOW((stderr, "GC of generation %d finished:\n", gen));
4314 print_generation_stats(0);
4318 } while ((gen <= gencgc_oldest_gen_to_gc)
4319 && ((gen < last_gen)
4320 || ((gen <= gencgc_oldest_gen_to_gc)
4322 && (generations[gen].bytes_allocated
4323 > generations[gen].gc_trigger)
4324 && (gen_av_mem_age(gen)
4325 > generations[gen].min_av_mem_age))));
4327 /* Now if gen-1 was raised all generations before gen are empty.
4328 * If it wasn't raised then all generations before gen-1 are empty.
4330 * Now objects within this gen's pages cannot point to younger
4331 * generations unless they are written to. This can be exploited
4332 * by write-protecting the pages of gen; then when younger
4333 * generations are GCed only the pages which have been written
4338 gen_to_wp = gen - 1;
4340 /* There's not much point in WPing pages in generation 0 as it is
4341 * never scavenged (except promoted pages). */
4342 if ((gen_to_wp > 0) && enable_page_protection) {
4343 /* Check that they are all empty. */
4344 for (i = 0; i < gen_to_wp; i++) {
4345 if (generations[i].bytes_allocated)
4346 lose("trying to write-protect gen. %d when gen. %d nonempty\n",
4349 write_protect_generation_pages(gen_to_wp);
4352 /* Set gc_alloc() back to generation 0. The current regions should
4353 * be flushed after the above GCs. */
4354 gc_assert((boxed_region.free_pointer - boxed_region.start_addr) == 0);
4355 gc_alloc_generation = 0;
4357 /* Save the high-water mark before updating last_free_page */
4358 if (last_free_page > high_water_mark)
4359 high_water_mark = last_free_page;
4361 update_dynamic_space_free_pointer();
4363 auto_gc_trigger = bytes_allocated + bytes_consed_between_gcs;
4365 fprintf(stderr,"Next gc when %ld bytes have been consed\n",
4368 /* If we did a big GC (arbitrarily defined as gen > 1), release memory
4371 if (gen > small_generation_limit) {
4372 if (last_free_page > high_water_mark)
4373 high_water_mark = last_free_page;
4374 remap_free_pages(0, high_water_mark);
4375 high_water_mark = 0;
4380 SHOW("returning from collect_garbage");
4383 /* This is called by Lisp PURIFY when it is finished. All live objects
4384 * will have been moved to the RO and Static heaps. The dynamic space
4385 * will need a full re-initialization. We don't bother having Lisp
4386 * PURIFY flush the current gc_alloc() region, as the page_tables are
4387 * re-initialized, and every page is zeroed to be sure. */
4393 if (gencgc_verbose > 1)
4394 SHOW("entering gc_free_heap");
4396 for (page = 0; page < page_table_pages; page++) {
4397 /* Skip free pages which should already be zero filled. */
4398 if (page_table[page].allocated != FREE_PAGE_FLAG) {
4399 void *page_start, *addr;
4401 /* Mark the page free. The other slots are assumed invalid
4402 * when it is a FREE_PAGE_FLAG and bytes_used is 0 and it
4403 * should not be write-protected -- except that the
4404 * generation is used for the current region but it sets
4406 page_table[page].allocated = FREE_PAGE_FLAG;
4407 page_table[page].bytes_used = 0;
4409 #ifndef LISP_FEATURE_WIN32 /* Pages already zeroed on win32? Not sure
4410 * about this change. */
4411 /* Zero the page. */
4412 page_start = (void *)page_address(page);
4414 /* First, remove any write-protection. */
4415 os_protect(page_start, PAGE_BYTES, OS_VM_PROT_ALL);
4416 page_table[page].write_protected = 0;
4418 os_invalidate(page_start,PAGE_BYTES);
4419 addr = os_validate(page_start,PAGE_BYTES);
4420 if (addr == NULL || addr != page_start) {
4421 lose("gc_free_heap: page moved, 0x%08x ==> 0x%08x\n",
4426 page_table[page].write_protected = 0;
4428 } else if (gencgc_zero_check_during_free_heap) {
4429 /* Double-check that the page is zero filled. */
4432 gc_assert(page_table[page].allocated == FREE_PAGE_FLAG);
4433 gc_assert(page_table[page].bytes_used == 0);
4434 page_start = (long *)page_address(page);
4435 for (i=0; i<1024; i++) {
4436 if (page_start[i] != 0) {
4437 lose("free region not zero at %x\n", page_start + i);
4443 bytes_allocated = 0;
4445 /* Initialize the generations. */
4446 for (page = 0; page < NUM_GENERATIONS; page++) {
4447 generations[page].alloc_start_page = 0;
4448 generations[page].alloc_unboxed_start_page = 0;
4449 generations[page].alloc_large_start_page = 0;
4450 generations[page].alloc_large_unboxed_start_page = 0;
4451 generations[page].bytes_allocated = 0;
4452 generations[page].gc_trigger = 2000000;
4453 generations[page].num_gc = 0;
4454 generations[page].cum_sum_bytes_allocated = 0;
4455 generations[page].lutexes = NULL;
4458 if (gencgc_verbose > 1)
4459 print_generation_stats(0);
4461 /* Initialize gc_alloc(). */
4462 gc_alloc_generation = 0;
4464 gc_set_region_empty(&boxed_region);
4465 gc_set_region_empty(&unboxed_region);
4468 set_alloc_pointer((lispobj)((char *)heap_base));
4470 if (verify_after_free_heap) {
4471 /* Check whether purify has left any bad pointers. */
4472 FSHOW((stderr, "checking after free_heap\n"));
4482 /* Compute the number of pages needed for the dynamic space.
4483 * Dynamic space size should be aligned on page size. */
4484 page_table_pages = dynamic_space_size/PAGE_BYTES;
4485 gc_assert(dynamic_space_size == (size_t) page_table_pages*PAGE_BYTES);
4487 page_table = calloc(page_table_pages, sizeof(struct page));
4488 gc_assert(page_table);
4491 scavtab[WEAK_POINTER_WIDETAG] = scav_weak_pointer;
4492 transother[SIMPLE_ARRAY_WIDETAG] = trans_boxed_large;
4494 #ifdef LUTEX_WIDETAG
4495 scavtab[LUTEX_WIDETAG] = scav_lutex;
4496 transother[LUTEX_WIDETAG] = trans_lutex;
4497 sizetab[LUTEX_WIDETAG] = size_lutex;
4500 heap_base = (void*)DYNAMIC_SPACE_START;
4502 /* Initialize each page structure. */
4503 for (i = 0; i < page_table_pages; i++) {
4504 /* Initialize all pages as free. */
4505 page_table[i].allocated = FREE_PAGE_FLAG;
4506 page_table[i].bytes_used = 0;
4508 /* Pages are not write-protected at startup. */
4509 page_table[i].write_protected = 0;
4512 bytes_allocated = 0;
4514 /* Initialize the generations.
4516 * FIXME: very similar to code in gc_free_heap(), should be shared */
4517 for (i = 0; i < NUM_GENERATIONS; i++) {
4518 generations[i].alloc_start_page = 0;
4519 generations[i].alloc_unboxed_start_page = 0;
4520 generations[i].alloc_large_start_page = 0;
4521 generations[i].alloc_large_unboxed_start_page = 0;
4522 generations[i].bytes_allocated = 0;
4523 generations[i].gc_trigger = 2000000;
4524 generations[i].num_gc = 0;
4525 generations[i].cum_sum_bytes_allocated = 0;
4526 /* the tune-able parameters */
4527 generations[i].bytes_consed_between_gc = 2000000;
4528 generations[i].trigger_age = 1;
4529 generations[i].min_av_mem_age = 0.75;
4530 generations[i].lutexes = NULL;
4533 /* Initialize gc_alloc. */
4534 gc_alloc_generation = 0;
4535 gc_set_region_empty(&boxed_region);
4536 gc_set_region_empty(&unboxed_region);
4541 /* Pick up the dynamic space from after a core load.
4543 * The ALLOCATION_POINTER points to the end of the dynamic space.
4547 gencgc_pickup_dynamic(void)
4549 page_index_t page = 0;
4550 long alloc_ptr = get_alloc_pointer();
4551 lispobj *prev=(lispobj *)page_address(page);
4552 generation_index_t gen = PSEUDO_STATIC_GENERATION;
4555 lispobj *first,*ptr= (lispobj *)page_address(page);
4556 page_table[page].allocated = BOXED_PAGE_FLAG;
4557 page_table[page].gen = gen;
4558 page_table[page].bytes_used = PAGE_BYTES;
4559 page_table[page].large_object = 0;
4560 page_table[page].write_protected = 0;
4561 page_table[page].write_protected_cleared = 0;
4562 page_table[page].dont_move = 0;
4563 page_table[page].need_to_zero = 1;
4565 if (!gencgc_partial_pickup) {
4566 first=gc_search_space(prev,(ptr+2)-prev,ptr);
4567 if(ptr == first) prev=ptr;
4568 page_table[page].first_object_offset =
4569 (void *)prev - page_address(page);
4572 } while ((long)page_address(page) < alloc_ptr);
4574 #ifdef LUTEX_WIDETAG
4575 /* Lutexes have been registered in generation 0 by coreparse, and
4576 * need to be moved to the right one manually.
4578 move_lutexes(0, PSEUDO_STATIC_GENERATION);
4581 last_free_page = page;
4583 generations[gen].bytes_allocated = PAGE_BYTES*page;
4584 bytes_allocated = PAGE_BYTES*page;
4586 gc_alloc_update_all_page_tables();
4587 write_protect_generation_pages(gen);
4591 gc_initialize_pointers(void)
4593 gencgc_pickup_dynamic();
4599 /* alloc(..) is the external interface for memory allocation. It
4600 * allocates to generation 0. It is not called from within the garbage
4601 * collector as it is only external uses that need the check for heap
4602 * size (GC trigger) and to disable the interrupts (interrupts are
4603 * always disabled during a GC).
4605 * The vops that call alloc(..) assume that the returned space is zero-filled.
4606 * (E.g. the most significant word of a 2-word bignum in MOVE-FROM-UNSIGNED.)
4608 * The check for a GC trigger is only performed when the current
4609 * region is full, so in most cases it's not needed. */
4614 struct thread *thread=arch_os_get_current_thread();
4615 struct alloc_region *region=
4616 #ifdef LISP_FEATURE_SB_THREAD
4617 thread ? &(thread->alloc_region) : &boxed_region;
4621 #ifndef LISP_FEATURE_WIN32
4622 lispobj alloc_signal;
4625 void *new_free_pointer;
4627 gc_assert(nbytes>0);
4629 /* Check for alignment allocation problems. */
4630 gc_assert((((unsigned long)region->free_pointer & LOWTAG_MASK) == 0)
4631 && ((nbytes & LOWTAG_MASK) == 0));
4635 /* there are a few places in the C code that allocate data in the
4636 * heap before Lisp starts. This is before interrupts are enabled,
4637 * so we don't need to check for pseudo-atomic */
4638 #ifdef LISP_FEATURE_SB_THREAD
4639 if(!get_psuedo_atomic_atomic(th)) {
4641 fprintf(stderr, "fatal error in thread 0x%x, tid=%ld\n",
4643 __asm__("movl %fs,%0" : "=r" (fs) : );
4644 fprintf(stderr, "fs is %x, th->tls_cookie=%x \n",
4645 debug_get_fs(),th->tls_cookie);
4646 lose("If you see this message before 2004.01.31, mail details to sbcl-devel\n");
4649 gc_assert(get_pseudo_atomic_atomic(th));
4653 /* maybe we can do this quickly ... */
4654 new_free_pointer = region->free_pointer + nbytes;
4655 if (new_free_pointer <= region->end_addr) {
4656 new_obj = (void*)(region->free_pointer);
4657 region->free_pointer = new_free_pointer;
4658 return(new_obj); /* yup */
4661 /* we have to go the long way around, it seems. Check whether
4662 * we should GC in the near future
4664 if (auto_gc_trigger && bytes_allocated > auto_gc_trigger) {
4665 gc_assert(get_pseudo_atomic_atomic(thread));
4666 /* Don't flood the system with interrupts if the need to gc is
4667 * already noted. This can happen for example when SUB-GC
4668 * allocates or after a gc triggered in a WITHOUT-GCING. */
4669 if (SymbolValue(GC_PENDING,thread) == NIL) {
4670 /* set things up so that GC happens when we finish the PA
4672 SetSymbolValue(GC_PENDING,T,thread);
4673 if (SymbolValue(GC_INHIBIT,thread) == NIL)
4674 set_pseudo_atomic_interrupted(thread);
4677 new_obj = gc_alloc_with_region(nbytes,0,region,0);
4679 #ifndef LISP_FEATURE_WIN32
4680 alloc_signal = SymbolValue(ALLOC_SIGNAL,thread);
4681 if ((alloc_signal & FIXNUM_TAG_MASK) == 0) {
4682 if ((signed long) alloc_signal <= 0) {
4683 SetSymbolValue(ALLOC_SIGNAL, T, thread);
4684 #ifdef LISP_FEATURE_SB_THREAD
4685 kill_thread_safely(thread->os_thread, SIGPROF);
4690 SetSymbolValue(ALLOC_SIGNAL,
4691 alloc_signal - (1 << N_FIXNUM_TAG_BITS),
4701 * shared support for the OS-dependent signal handlers which
4702 * catch GENCGC-related write-protect violations
4705 void unhandled_sigmemoryfault(void* addr);
4707 /* Depending on which OS we're running under, different signals might
4708 * be raised for a violation of write protection in the heap. This
4709 * function factors out the common generational GC magic which needs
4710 * to invoked in this case, and should be called from whatever signal
4711 * handler is appropriate for the OS we're running under.
4713 * Return true if this signal is a normal generational GC thing that
4714 * we were able to handle, or false if it was abnormal and control
4715 * should fall through to the general SIGSEGV/SIGBUS/whatever logic. */
4718 gencgc_handle_wp_violation(void* fault_addr)
4720 page_index_t page_index = find_page_index(fault_addr);
4722 #ifdef QSHOW_SIGNALS
4723 FSHOW((stderr, "heap WP violation? fault_addr=%x, page_index=%d\n",
4724 fault_addr, page_index));
4727 /* Check whether the fault is within the dynamic space. */
4728 if (page_index == (-1)) {
4730 /* It can be helpful to be able to put a breakpoint on this
4731 * case to help diagnose low-level problems. */
4732 unhandled_sigmemoryfault(fault_addr);
4734 /* not within the dynamic space -- not our responsibility */
4738 if (page_table[page_index].write_protected) {
4739 /* Unprotect the page. */
4740 os_protect(page_address(page_index), PAGE_BYTES, OS_VM_PROT_ALL);
4741 page_table[page_index].write_protected_cleared = 1;
4742 page_table[page_index].write_protected = 0;
4744 /* The only acceptable reason for this signal on a heap
4745 * access is that GENCGC write-protected the page.
4746 * However, if two CPUs hit a wp page near-simultaneously,
4747 * we had better not have the second one lose here if it
4748 * does this test after the first one has already set wp=0
4750 if(page_table[page_index].write_protected_cleared != 1)
4751 lose("fault in heap page %d not marked as write-protected\nboxed_region.first_page: %d, boxed_region.last_page %d\n",
4752 page_index, boxed_region.first_page,
4753 boxed_region.last_page);
4755 /* Don't worry, we can handle it. */
4759 /* This is to be called when we catch a SIGSEGV/SIGBUS, determine that
4760 * it's not just a case of the program hitting the write barrier, and
4761 * are about to let Lisp deal with it. It's basically just a
4762 * convenient place to set a gdb breakpoint. */
4764 unhandled_sigmemoryfault(void *addr)
4767 void gc_alloc_update_all_page_tables(void)
4769 /* Flush the alloc regions updating the tables. */
4772 gc_alloc_update_page_tables(0, &th->alloc_region);
4773 gc_alloc_update_page_tables(1, &unboxed_region);
4774 gc_alloc_update_page_tables(0, &boxed_region);
4778 gc_set_region_empty(struct alloc_region *region)
4780 region->first_page = 0;
4781 region->last_page = -1;
4782 region->start_addr = page_address(0);
4783 region->free_pointer = page_address(0);
4784 region->end_addr = page_address(0);
4788 zero_all_free_pages()
4792 for (i = 0; i < last_free_page; i++) {
4793 if (page_table[i].allocated == FREE_PAGE_FLAG) {
4794 #ifdef READ_PROTECT_FREE_PAGES
4795 os_protect(page_address(i),
4804 /* Things to do before doing a final GC before saving a core (without
4807 * + Pages in large_object pages aren't moved by the GC, so we need to
4808 * unset that flag from all pages.
4809 * + The pseudo-static generation isn't normally collected, but it seems
4810 * reasonable to collect it at least when saving a core. So move the
4811 * pages to a normal generation.
4814 prepare_for_final_gc ()
4817 for (i = 0; i < last_free_page; i++) {
4818 page_table[i].large_object = 0;
4819 if (page_table[i].gen == PSEUDO_STATIC_GENERATION) {
4820 int used = page_table[i].bytes_used;
4821 page_table[i].gen = HIGHEST_NORMAL_GENERATION;
4822 generations[PSEUDO_STATIC_GENERATION].bytes_allocated -= used;
4823 generations[HIGHEST_NORMAL_GENERATION].bytes_allocated += used;
4829 /* Do a non-conservative GC, and then save a core with the initial
4830 * function being set to the value of the static symbol
4831 * SB!VM:RESTART-LISP-FUNCTION */
4833 gc_and_save(char *filename, int prepend_runtime)
4836 void *runtime_bytes = NULL;
4837 size_t runtime_size;
4839 file = prepare_to_save(filename, prepend_runtime, &runtime_bytes,
4844 conservative_stack = 0;
4846 /* The filename might come from Lisp, and be moved by the now
4847 * non-conservative GC. */
4848 filename = strdup(filename);
4850 /* Collect twice: once into relatively high memory, and then back
4851 * into low memory. This compacts the retained data into the lower
4852 * pages, minimizing the size of the core file.
4854 prepare_for_final_gc();
4855 gencgc_alloc_start_page = last_free_page;
4856 collect_garbage(HIGHEST_NORMAL_GENERATION+1);
4858 prepare_for_final_gc();
4859 gencgc_alloc_start_page = -1;
4860 collect_garbage(HIGHEST_NORMAL_GENERATION+1);
4862 if (prepend_runtime)
4863 save_runtime_to_filehandle(file, runtime_bytes, runtime_size);
4865 /* The dumper doesn't know that pages need to be zeroed before use. */
4866 zero_all_free_pages();
4867 save_to_filehandle(file, filename, SymbolValue(RESTART_LISP_FUNCTION,0),
4869 /* Oops. Save still managed to fail. Since we've mangled the stack
4870 * beyond hope, there's not much we can do.
4871 * (beyond FUNCALLing RESTART_LISP_FUNCTION, but I suspect that's
4872 * going to be rather unsatisfactory too... */
4873 lose("Attempt to save core after non-conservative GC failed.\n");