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"
43 #include "gc-internal.h"
46 #include "genesis/vector.h"
47 #include "genesis/weak-pointer.h"
48 #include "genesis/fdefn.h"
49 #include "genesis/simple-fun.h"
51 #include "genesis/hash-table.h"
52 #include "genesis/instance.h"
53 #include "genesis/layout.h"
55 #if defined(LUTEX_WIDETAG)
56 #include "pthread-lutex.h"
59 /* forward declarations */
60 page_index_t gc_find_freeish_pages(long *restart_page_ptr, long nbytes,
68 /* Generations 0-5 are normal collected generations, 6 is only used as
69 * scratch space by the collector, and should never get collected.
72 HIGHEST_NORMAL_GENERATION = 5,
73 PSEUDO_STATIC_GENERATION,
78 /* Should we use page protection to help avoid the scavenging of pages
79 * that don't have pointers to younger generations? */
80 boolean enable_page_protection = 1;
82 /* the minimum size (in bytes) for a large object*/
83 long large_object_size = 4 * PAGE_BYTES;
90 /* the verbosity level. All non-error messages are disabled at level 0;
91 * and only a few rare messages are printed at level 1. */
93 boolean gencgc_verbose = 1;
95 boolean gencgc_verbose = 0;
98 /* FIXME: At some point enable the various error-checking things below
99 * and see what they say. */
101 /* We hunt for pointers to old-space, when GCing generations >= verify_gen.
102 * Set verify_gens to HIGHEST_NORMAL_GENERATION + 1 to disable this kind of
104 generation_index_t verify_gens = HIGHEST_NORMAL_GENERATION + 1;
106 /* Should we do a pre-scan verify of generation 0 before it's GCed? */
107 boolean pre_verify_gen_0 = 0;
109 /* Should we check for bad pointers after gc_free_heap is called
110 * from Lisp PURIFY? */
111 boolean verify_after_free_heap = 0;
113 /* Should we print a note when code objects are found in the dynamic space
114 * during a heap verify? */
115 boolean verify_dynamic_code_check = 0;
117 /* Should we check code objects for fixup errors after they are transported? */
118 boolean check_code_fixups = 0;
120 /* Should we check that newly allocated regions are zero filled? */
121 boolean gencgc_zero_check = 0;
123 /* Should we check that the free space is zero filled? */
124 boolean gencgc_enable_verify_zero_fill = 0;
126 /* Should we check that free pages are zero filled during gc_free_heap
127 * called after Lisp PURIFY? */
128 boolean gencgc_zero_check_during_free_heap = 0;
130 /* When loading a core, don't do a full scan of the memory for the
131 * memory region boundaries. (Set to true by coreparse.c if the core
132 * contained a pagetable entry).
134 boolean gencgc_partial_pickup = 0;
136 /* If defined, free pages are read-protected to ensure that nothing
140 /* #define READ_PROTECT_FREE_PAGES */
144 * GC structures and variables
147 /* the total bytes allocated. These are seen by Lisp DYNAMIC-USAGE. */
148 unsigned long bytes_allocated = 0;
149 unsigned long auto_gc_trigger = 0;
151 /* the source and destination generations. These are set before a GC starts
153 generation_index_t from_space;
154 generation_index_t new_space;
156 /* Set to 1 when in GC */
157 boolean gc_active_p = 0;
159 /* should the GC be conservative on stack. If false (only right before
160 * saving a core), don't scan the stack / mark pages dont_move. */
161 static boolean conservative_stack = 1;
163 /* An array of page structures is allocated on gc initialization.
164 * This helps quickly map between an address its page structure.
165 * page_table_pages is set from the size of the dynamic space. */
166 page_index_t page_table_pages;
167 struct page *page_table;
169 /* To map addresses to page structures the address of the first page
171 static void *heap_base = NULL;
173 /* Calculate the start address for the given page number. */
175 page_address(page_index_t page_num)
177 return (heap_base + (page_num * PAGE_BYTES));
180 /* Find the page index within the page_table for the given
181 * address. Return -1 on failure. */
183 find_page_index(void *addr)
185 page_index_t index = addr-heap_base;
188 index = ((unsigned long)index)/PAGE_BYTES;
189 if (index < page_table_pages)
196 /* a structure to hold the state of a generation */
199 /* the first page that gc_alloc() checks on its next call */
200 page_index_t alloc_start_page;
202 /* the first page that gc_alloc_unboxed() checks on its next call */
203 page_index_t alloc_unboxed_start_page;
205 /* the first page that gc_alloc_large (boxed) considers on its next
206 * call. (Although it always allocates after the boxed_region.) */
207 page_index_t alloc_large_start_page;
209 /* the first page that gc_alloc_large (unboxed) considers on its
210 * next call. (Although it always allocates after the
211 * current_unboxed_region.) */
212 page_index_t alloc_large_unboxed_start_page;
214 /* the bytes allocated to this generation */
215 long bytes_allocated;
217 /* the number of bytes at which to trigger a GC */
220 /* to calculate a new level for gc_trigger */
221 long bytes_consed_between_gc;
223 /* the number of GCs since the last raise */
226 /* the average age after which a GC will raise objects to the
230 /* the cumulative sum of the bytes allocated to this generation. It is
231 * cleared after a GC on this generations, and update before new
232 * objects are added from a GC of a younger generation. Dividing by
233 * the bytes_allocated will give the average age of the memory in
234 * this generation since its last GC. */
235 long cum_sum_bytes_allocated;
237 /* a minimum average memory age before a GC will occur helps
238 * prevent a GC when a large number of new live objects have been
239 * added, in which case a GC could be a waste of time */
240 double min_av_mem_age;
242 /* A linked list of lutex structures in this generation, used for
243 * implementing lutex finalization. */
245 struct lutex *lutexes;
251 /* an array of generation structures. There needs to be one more
252 * generation structure than actual generations as the oldest
253 * generation is temporarily raised then lowered. */
254 struct generation generations[NUM_GENERATIONS];
256 /* the oldest generation that is will currently be GCed by default.
257 * Valid values are: 0, 1, ... HIGHEST_NORMAL_GENERATION
259 * The default of HIGHEST_NORMAL_GENERATION enables GC on all generations.
261 * Setting this to 0 effectively disables the generational nature of
262 * the GC. In some applications generational GC may not be useful
263 * because there are no long-lived objects.
265 * An intermediate value could be handy after moving long-lived data
266 * into an older generation so an unnecessary GC of this long-lived
267 * data can be avoided. */
268 generation_index_t gencgc_oldest_gen_to_gc = HIGHEST_NORMAL_GENERATION;
270 /* The maximum free page in the heap is maintained and used to update
271 * ALLOCATION_POINTER which is used by the room function to limit its
272 * search of the heap. XX Gencgc obviously needs to be better
273 * integrated with the Lisp code. */
274 page_index_t last_free_page;
276 /* This lock is to prevent multiple threads from simultaneously
277 * allocating new regions which overlap each other. Note that the
278 * majority of GC is single-threaded, but alloc() may be called from
279 * >1 thread at a time and must be thread-safe. This lock must be
280 * seized before all accesses to generations[] or to parts of
281 * page_table[] that other threads may want to see */
283 #ifdef LISP_FEATURE_SB_THREAD
284 static pthread_mutex_t free_pages_lock = PTHREAD_MUTEX_INITIALIZER;
289 * miscellaneous heap functions
292 /* Count the number of pages which are write-protected within the
293 * given generation. */
295 count_write_protect_generation_pages(generation_index_t generation)
300 for (i = 0; i < last_free_page; i++)
301 if ((page_table[i].allocated != FREE_PAGE_FLAG)
302 && (page_table[i].gen == generation)
303 && (page_table[i].write_protected == 1))
308 /* Count the number of pages within the given generation. */
310 count_generation_pages(generation_index_t generation)
315 for (i = 0; i < last_free_page; i++)
316 if ((page_table[i].allocated != FREE_PAGE_FLAG)
317 && (page_table[i].gen == generation))
324 count_dont_move_pages(void)
328 for (i = 0; i < last_free_page; i++) {
329 if ((page_table[i].allocated != FREE_PAGE_FLAG)
330 && (page_table[i].dont_move != 0)) {
338 /* Work through the pages and add up the number of bytes used for the
339 * given generation. */
341 count_generation_bytes_allocated (generation_index_t gen)
345 for (i = 0; i < last_free_page; i++) {
346 if ((page_table[i].allocated != FREE_PAGE_FLAG)
347 && (page_table[i].gen == gen))
348 result += page_table[i].bytes_used;
353 /* Return the average age of the memory in a generation. */
355 gen_av_mem_age(generation_index_t gen)
357 if (generations[gen].bytes_allocated == 0)
361 ((double)generations[gen].cum_sum_bytes_allocated)
362 / ((double)generations[gen].bytes_allocated);
365 /* The verbose argument controls how much to print: 0 for normal
366 * level of detail; 1 for debugging. */
368 print_generation_stats(int verbose) /* FIXME: should take FILE argument */
370 generation_index_t i, gens;
372 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
373 #define FPU_STATE_SIZE 27
374 int fpu_state[FPU_STATE_SIZE];
375 #elif defined(LISP_FEATURE_PPC)
376 #define FPU_STATE_SIZE 32
377 long long fpu_state[FPU_STATE_SIZE];
380 /* This code uses the FP instructions which may be set up for Lisp
381 * so they need to be saved and reset for C. */
384 /* highest generation to print */
386 gens = SCRATCH_GENERATION;
388 gens = PSEUDO_STATIC_GENERATION;
390 /* Print the heap stats. */
392 " Gen StaPg UbSta LaSta LUbSt Boxed Unboxed LB LUB !move Alloc Waste Trig WP GCs Mem-age\n");
394 for (i = 0; i < gens; i++) {
397 long unboxed_cnt = 0;
398 long large_boxed_cnt = 0;
399 long large_unboxed_cnt = 0;
402 for (j = 0; j < last_free_page; j++)
403 if (page_table[j].gen == i) {
405 /* Count the number of boxed pages within the given
407 if (page_table[j].allocated & BOXED_PAGE_FLAG) {
408 if (page_table[j].large_object)
413 if(page_table[j].dont_move) pinned_cnt++;
414 /* Count the number of unboxed pages within the given
416 if (page_table[j].allocated & UNBOXED_PAGE_FLAG) {
417 if (page_table[j].large_object)
424 gc_assert(generations[i].bytes_allocated
425 == count_generation_bytes_allocated(i));
427 " %1d: %5ld %5ld %5ld %5ld %5ld %5ld %5ld %5ld %5ld %8ld %5ld %8ld %4ld %3d %7.4f\n",
429 generations[i].alloc_start_page,
430 generations[i].alloc_unboxed_start_page,
431 generations[i].alloc_large_start_page,
432 generations[i].alloc_large_unboxed_start_page,
438 generations[i].bytes_allocated,
439 (count_generation_pages(i)*PAGE_BYTES - generations[i].bytes_allocated),
440 generations[i].gc_trigger,
441 count_write_protect_generation_pages(i),
442 generations[i].num_gc,
445 fprintf(stderr," Total bytes allocated=%ld\n", bytes_allocated);
447 fpu_restore(fpu_state);
451 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
452 void fast_bzero(void*, size_t); /* in <arch>-assem.S */
455 /* Zero the pages from START to END (inclusive), but use mmap/munmap instead
456 * if zeroing it ourselves, i.e. in practice give the memory back to the
457 * OS. Generally done after a large GC.
459 void zero_pages_with_mmap(page_index_t start, page_index_t end) {
461 void *addr = (void *) page_address(start), *new_addr;
462 size_t length = PAGE_BYTES*(1+end-start);
467 os_invalidate(addr, length);
468 new_addr = os_validate(addr, length);
469 if (new_addr == NULL || new_addr != addr) {
470 lose("remap_free_pages: page moved, 0x%08x ==> 0x%08x", start, new_addr);
473 for (i = start; i <= end; i++) {
474 page_table[i].need_to_zero = 0;
478 /* Zero the pages from START to END (inclusive). Generally done just after
479 * a new region has been allocated.
482 zero_pages(page_index_t start, page_index_t end) {
486 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
487 fast_bzero(page_address(start), PAGE_BYTES*(1+end-start));
489 bzero(page_address(start), PAGE_BYTES*(1+end-start));
494 /* Zero the pages from START to END (inclusive), except for those
495 * pages that are known to already zeroed. Mark all pages in the
496 * ranges as non-zeroed.
499 zero_dirty_pages(page_index_t start, page_index_t end) {
502 for (i = start; i <= end; i++) {
503 if (page_table[i].need_to_zero == 1) {
504 zero_pages(start, end);
509 for (i = start; i <= end; i++) {
510 page_table[i].need_to_zero = 1;
516 * To support quick and inline allocation, regions of memory can be
517 * allocated and then allocated from with just a free pointer and a
518 * check against an end address.
520 * Since objects can be allocated to spaces with different properties
521 * e.g. boxed/unboxed, generation, ages; there may need to be many
522 * allocation regions.
524 * Each allocation region may start within a partly used page. Many
525 * features of memory use are noted on a page wise basis, e.g. the
526 * generation; so if a region starts within an existing allocated page
527 * it must be consistent with this page.
529 * During the scavenging of the newspace, objects will be transported
530 * into an allocation region, and pointers updated to point to this
531 * allocation region. It is possible that these pointers will be
532 * scavenged again before the allocation region is closed, e.g. due to
533 * trans_list which jumps all over the place to cleanup the list. It
534 * is important to be able to determine properties of all objects
535 * pointed to when scavenging, e.g to detect pointers to the oldspace.
536 * Thus it's important that the allocation regions have the correct
537 * properties set when allocated, and not just set when closed. The
538 * region allocation routines return regions with the specified
539 * properties, and grab all the pages, setting their properties
540 * appropriately, except that the amount used is not known.
542 * These regions are used to support quicker allocation using just a
543 * free pointer. The actual space used by the region is not reflected
544 * in the pages tables until it is closed. It can't be scavenged until
547 * When finished with the region it should be closed, which will
548 * update the page tables for the actual space used returning unused
549 * space. Further it may be noted in the new regions which is
550 * necessary when scavenging the newspace.
552 * Large objects may be allocated directly without an allocation
553 * region, the page tables are updated immediately.
555 * Unboxed objects don't contain pointers to other objects and so
556 * don't need scavenging. Further they can't contain pointers to
557 * younger generations so WP is not needed. By allocating pages to
558 * unboxed objects the whole page never needs scavenging or
559 * write-protecting. */
561 /* We are only using two regions at present. Both are for the current
562 * newspace generation. */
563 struct alloc_region boxed_region;
564 struct alloc_region unboxed_region;
566 /* The generation currently being allocated to. */
567 static generation_index_t gc_alloc_generation;
569 /* Find a new region with room for at least the given number of bytes.
571 * It starts looking at the current generation's alloc_start_page. So
572 * may pick up from the previous region if there is enough space. This
573 * keeps the allocation contiguous when scavenging the newspace.
575 * The alloc_region should have been closed by a call to
576 * gc_alloc_update_page_tables(), and will thus be in an empty state.
578 * To assist the scavenging functions write-protected pages are not
579 * used. Free pages should not be write-protected.
581 * It is critical to the conservative GC that the start of regions be
582 * known. To help achieve this only small regions are allocated at a
585 * During scavenging, pointers may be found to within the current
586 * region and the page generation must be set so that pointers to the
587 * from space can be recognized. Therefore the generation of pages in
588 * the region are set to gc_alloc_generation. To prevent another
589 * allocation call using the same pages, all the pages in the region
590 * are allocated, although they will initially be empty.
593 gc_alloc_new_region(long nbytes, int unboxed, struct alloc_region *alloc_region)
595 page_index_t first_page;
596 page_index_t last_page;
603 "/alloc_new_region for %d bytes from gen %d\n",
604 nbytes, gc_alloc_generation));
607 /* Check that the region is in a reset state. */
608 gc_assert((alloc_region->first_page == 0)
609 && (alloc_region->last_page == -1)
610 && (alloc_region->free_pointer == alloc_region->end_addr));
611 ret = thread_mutex_lock(&free_pages_lock);
615 generations[gc_alloc_generation].alloc_unboxed_start_page;
618 generations[gc_alloc_generation].alloc_start_page;
620 last_page=gc_find_freeish_pages(&first_page,nbytes,unboxed);
621 bytes_found=(PAGE_BYTES - page_table[first_page].bytes_used)
622 + PAGE_BYTES*(last_page-first_page);
624 /* Set up the alloc_region. */
625 alloc_region->first_page = first_page;
626 alloc_region->last_page = last_page;
627 alloc_region->start_addr = page_table[first_page].bytes_used
628 + page_address(first_page);
629 alloc_region->free_pointer = alloc_region->start_addr;
630 alloc_region->end_addr = alloc_region->start_addr + bytes_found;
632 /* Set up the pages. */
634 /* The first page may have already been in use. */
635 if (page_table[first_page].bytes_used == 0) {
637 page_table[first_page].allocated = UNBOXED_PAGE_FLAG;
639 page_table[first_page].allocated = BOXED_PAGE_FLAG;
640 page_table[first_page].gen = gc_alloc_generation;
641 page_table[first_page].large_object = 0;
642 page_table[first_page].first_object_offset = 0;
646 gc_assert(page_table[first_page].allocated == UNBOXED_PAGE_FLAG);
648 gc_assert(page_table[first_page].allocated == BOXED_PAGE_FLAG);
649 page_table[first_page].allocated |= OPEN_REGION_PAGE_FLAG;
651 gc_assert(page_table[first_page].gen == gc_alloc_generation);
652 gc_assert(page_table[first_page].large_object == 0);
654 for (i = first_page+1; i <= last_page; i++) {
656 page_table[i].allocated = UNBOXED_PAGE_FLAG;
658 page_table[i].allocated = BOXED_PAGE_FLAG;
659 page_table[i].gen = gc_alloc_generation;
660 page_table[i].large_object = 0;
661 /* This may not be necessary for unboxed regions (think it was
663 page_table[i].first_object_offset =
664 alloc_region->start_addr - page_address(i);
665 page_table[i].allocated |= OPEN_REGION_PAGE_FLAG ;
667 /* Bump up last_free_page. */
668 if (last_page+1 > last_free_page) {
669 last_free_page = last_page+1;
670 /* do we only want to call this on special occasions? like for boxed_region? */
671 set_alloc_pointer((lispobj)(((char *)heap_base) + last_free_page*PAGE_BYTES));
673 ret = thread_mutex_unlock(&free_pages_lock);
676 #ifdef READ_PROTECT_FREE_PAGES
677 os_protect(page_address(first_page),
678 PAGE_BYTES*(1+last_page-first_page),
682 /* If the first page was only partial, don't check whether it's
683 * zeroed (it won't be) and don't zero it (since the parts that
684 * we're interested in are guaranteed to be zeroed).
686 if (page_table[first_page].bytes_used) {
690 zero_dirty_pages(first_page, last_page);
692 /* we can do this after releasing free_pages_lock */
693 if (gencgc_zero_check) {
695 for (p = (long *)alloc_region->start_addr;
696 p < (long *)alloc_region->end_addr; p++) {
698 /* KLUDGE: It would be nice to use %lx and explicit casts
699 * (long) in code like this, so that it is less likely to
700 * break randomly when running on a machine with different
701 * word sizes. -- WHN 19991129 */
702 lose("The new region at %x is not zero (start=%p, end=%p).\n",
703 p, alloc_region->start_addr, alloc_region->end_addr);
709 /* If the record_new_objects flag is 2 then all new regions created
712 * If it's 1 then then it is only recorded if the first page of the
713 * current region is <= new_areas_ignore_page. This helps avoid
714 * unnecessary recording when doing full scavenge pass.
716 * The new_object structure holds the page, byte offset, and size of
717 * new regions of objects. Each new area is placed in the array of
718 * these structures pointer to by new_areas. new_areas_index holds the
719 * offset into new_areas.
721 * If new_area overflows NUM_NEW_AREAS then it stops adding them. The
722 * later code must detect this and handle it, probably by doing a full
723 * scavenge of a generation. */
724 #define NUM_NEW_AREAS 512
725 static int record_new_objects = 0;
726 static page_index_t new_areas_ignore_page;
732 static struct new_area (*new_areas)[];
733 static long new_areas_index;
736 /* Add a new area to new_areas. */
738 add_new_area(page_index_t first_page, long offset, long size)
740 unsigned long new_area_start,c;
743 /* Ignore if full. */
744 if (new_areas_index >= NUM_NEW_AREAS)
747 switch (record_new_objects) {
751 if (first_page > new_areas_ignore_page)
760 new_area_start = PAGE_BYTES*first_page + offset;
762 /* Search backwards for a prior area that this follows from. If
763 found this will save adding a new area. */
764 for (i = new_areas_index-1, c = 0; (i >= 0) && (c < 8); i--, c++) {
765 unsigned long area_end =
766 PAGE_BYTES*((*new_areas)[i].page)
767 + (*new_areas)[i].offset
768 + (*new_areas)[i].size;
770 "/add_new_area S1 %d %d %d %d\n",
771 i, c, new_area_start, area_end));*/
772 if (new_area_start == area_end) {
774 "/adding to [%d] %d %d %d with %d %d %d:\n",
776 (*new_areas)[i].page,
777 (*new_areas)[i].offset,
778 (*new_areas)[i].size,
782 (*new_areas)[i].size += size;
787 (*new_areas)[new_areas_index].page = first_page;
788 (*new_areas)[new_areas_index].offset = offset;
789 (*new_areas)[new_areas_index].size = size;
791 "/new_area %d page %d offset %d size %d\n",
792 new_areas_index, first_page, offset, size));*/
795 /* Note the max new_areas used. */
796 if (new_areas_index > max_new_areas)
797 max_new_areas = new_areas_index;
800 /* Update the tables for the alloc_region. The region may be added to
803 * When done the alloc_region is set up so that the next quick alloc
804 * will fail safely and thus a new region will be allocated. Further
805 * it is safe to try to re-update the page table of this reset
808 gc_alloc_update_page_tables(int unboxed, struct alloc_region *alloc_region)
811 page_index_t first_page;
812 page_index_t next_page;
814 long orig_first_page_bytes_used;
820 first_page = alloc_region->first_page;
822 /* Catch an unused alloc_region. */
823 if ((first_page == 0) && (alloc_region->last_page == -1))
826 next_page = first_page+1;
828 ret = thread_mutex_lock(&free_pages_lock);
830 if (alloc_region->free_pointer != alloc_region->start_addr) {
831 /* some bytes were allocated in the region */
832 orig_first_page_bytes_used = page_table[first_page].bytes_used;
834 gc_assert(alloc_region->start_addr == (page_address(first_page) + page_table[first_page].bytes_used));
836 /* All the pages used need to be updated */
838 /* Update the first page. */
840 /* If the page was free then set up the gen, and
841 * first_object_offset. */
842 if (page_table[first_page].bytes_used == 0)
843 gc_assert(page_table[first_page].first_object_offset == 0);
844 page_table[first_page].allocated &= ~(OPEN_REGION_PAGE_FLAG);
847 gc_assert(page_table[first_page].allocated == UNBOXED_PAGE_FLAG);
849 gc_assert(page_table[first_page].allocated == BOXED_PAGE_FLAG);
850 gc_assert(page_table[first_page].gen == gc_alloc_generation);
851 gc_assert(page_table[first_page].large_object == 0);
855 /* Calculate the number of bytes used in this page. This is not
856 * always the number of new bytes, unless it was free. */
858 if ((bytes_used = (alloc_region->free_pointer - page_address(first_page)))>PAGE_BYTES) {
859 bytes_used = PAGE_BYTES;
862 page_table[first_page].bytes_used = bytes_used;
863 byte_cnt += bytes_used;
866 /* All the rest of the pages should be free. We need to set their
867 * first_object_offset pointer to the start of the region, and set
870 page_table[next_page].allocated &= ~(OPEN_REGION_PAGE_FLAG);
872 gc_assert(page_table[next_page].allocated==UNBOXED_PAGE_FLAG);
874 gc_assert(page_table[next_page].allocated == BOXED_PAGE_FLAG);
875 gc_assert(page_table[next_page].bytes_used == 0);
876 gc_assert(page_table[next_page].gen == gc_alloc_generation);
877 gc_assert(page_table[next_page].large_object == 0);
879 gc_assert(page_table[next_page].first_object_offset ==
880 alloc_region->start_addr - page_address(next_page));
882 /* Calculate the number of bytes used in this page. */
884 if ((bytes_used = (alloc_region->free_pointer
885 - page_address(next_page)))>PAGE_BYTES) {
886 bytes_used = PAGE_BYTES;
889 page_table[next_page].bytes_used = bytes_used;
890 byte_cnt += bytes_used;
895 region_size = alloc_region->free_pointer - alloc_region->start_addr;
896 bytes_allocated += region_size;
897 generations[gc_alloc_generation].bytes_allocated += region_size;
899 gc_assert((byte_cnt- orig_first_page_bytes_used) == region_size);
901 /* Set the generations alloc restart page to the last page of
904 generations[gc_alloc_generation].alloc_unboxed_start_page =
907 generations[gc_alloc_generation].alloc_start_page = next_page-1;
909 /* Add the region to the new_areas if requested. */
911 add_new_area(first_page,orig_first_page_bytes_used, region_size);
915 "/gc_alloc_update_page_tables update %d bytes to gen %d\n",
917 gc_alloc_generation));
920 /* There are no bytes allocated. Unallocate the first_page if
921 * there are 0 bytes_used. */
922 page_table[first_page].allocated &= ~(OPEN_REGION_PAGE_FLAG);
923 if (page_table[first_page].bytes_used == 0)
924 page_table[first_page].allocated = FREE_PAGE_FLAG;
927 /* Unallocate any unused pages. */
928 while (next_page <= alloc_region->last_page) {
929 gc_assert(page_table[next_page].bytes_used == 0);
930 page_table[next_page].allocated = FREE_PAGE_FLAG;
933 ret = thread_mutex_unlock(&free_pages_lock);
936 /* alloc_region is per-thread, we're ok to do this unlocked */
937 gc_set_region_empty(alloc_region);
940 static inline void *gc_quick_alloc(long nbytes);
942 /* Allocate a possibly large object. */
944 gc_alloc_large(long nbytes, int unboxed, struct alloc_region *alloc_region)
946 page_index_t first_page;
947 page_index_t last_page;
948 int orig_first_page_bytes_used;
952 page_index_t next_page;
955 ret = thread_mutex_lock(&free_pages_lock);
960 generations[gc_alloc_generation].alloc_large_unboxed_start_page;
962 first_page = generations[gc_alloc_generation].alloc_large_start_page;
964 if (first_page <= alloc_region->last_page) {
965 first_page = alloc_region->last_page+1;
968 last_page=gc_find_freeish_pages(&first_page,nbytes,unboxed);
970 gc_assert(first_page > alloc_region->last_page);
972 generations[gc_alloc_generation].alloc_large_unboxed_start_page =
975 generations[gc_alloc_generation].alloc_large_start_page = last_page;
977 /* Set up the pages. */
978 orig_first_page_bytes_used = page_table[first_page].bytes_used;
980 /* If the first page was free then set up the gen, and
981 * first_object_offset. */
982 if (page_table[first_page].bytes_used == 0) {
984 page_table[first_page].allocated = UNBOXED_PAGE_FLAG;
986 page_table[first_page].allocated = BOXED_PAGE_FLAG;
987 page_table[first_page].gen = gc_alloc_generation;
988 page_table[first_page].first_object_offset = 0;
989 page_table[first_page].large_object = 1;
993 gc_assert(page_table[first_page].allocated == UNBOXED_PAGE_FLAG);
995 gc_assert(page_table[first_page].allocated == BOXED_PAGE_FLAG);
996 gc_assert(page_table[first_page].gen == gc_alloc_generation);
997 gc_assert(page_table[first_page].large_object == 1);
1001 /* Calc. the number of bytes used in this page. This is not
1002 * always the number of new bytes, unless it was free. */
1004 if ((bytes_used = nbytes+orig_first_page_bytes_used) > PAGE_BYTES) {
1005 bytes_used = PAGE_BYTES;
1008 page_table[first_page].bytes_used = bytes_used;
1009 byte_cnt += bytes_used;
1011 next_page = first_page+1;
1013 /* All the rest of the pages should be free. We need to set their
1014 * first_object_offset pointer to the start of the region, and
1015 * set the bytes_used. */
1017 gc_assert(page_table[next_page].allocated == FREE_PAGE_FLAG);
1018 gc_assert(page_table[next_page].bytes_used == 0);
1020 page_table[next_page].allocated = UNBOXED_PAGE_FLAG;
1022 page_table[next_page].allocated = BOXED_PAGE_FLAG;
1023 page_table[next_page].gen = gc_alloc_generation;
1024 page_table[next_page].large_object = 1;
1026 page_table[next_page].first_object_offset =
1027 orig_first_page_bytes_used - PAGE_BYTES*(next_page-first_page);
1029 /* Calculate the number of bytes used in this page. */
1031 if ((bytes_used=(nbytes+orig_first_page_bytes_used)-byte_cnt) > PAGE_BYTES) {
1032 bytes_used = PAGE_BYTES;
1035 page_table[next_page].bytes_used = bytes_used;
1036 page_table[next_page].write_protected=0;
1037 page_table[next_page].dont_move=0;
1038 byte_cnt += bytes_used;
1042 gc_assert((byte_cnt-orig_first_page_bytes_used) == nbytes);
1044 bytes_allocated += nbytes;
1045 generations[gc_alloc_generation].bytes_allocated += nbytes;
1047 /* Add the region to the new_areas if requested. */
1049 add_new_area(first_page,orig_first_page_bytes_used,nbytes);
1051 /* Bump up last_free_page */
1052 if (last_page+1 > last_free_page) {
1053 last_free_page = last_page+1;
1054 set_alloc_pointer((lispobj)(((char *)heap_base) + last_free_page*PAGE_BYTES));
1056 ret = thread_mutex_unlock(&free_pages_lock);
1057 gc_assert(ret == 0);
1059 #ifdef READ_PROTECT_FREE_PAGES
1060 os_protect(page_address(first_page),
1061 PAGE_BYTES*(1+last_page-first_page),
1065 zero_dirty_pages(first_page, last_page);
1067 return page_address(first_page);
1070 static page_index_t gencgc_alloc_start_page = -1;
1073 gc_heap_exhausted_error_or_lose (long available, long requested)
1075 /* Write basic information before doing anything else: if we don't
1076 * call to lisp this is a must, and even if we do there is always
1077 * the danger that we bounce back here before the error has been
1078 * handled, or indeed even printed.
1080 fprintf(stderr, "Heap exhausted during %s: %ld bytes available, %ld requested.\n",
1081 gc_active_p ? "garbage collection" : "allocation", available, requested);
1082 if (gc_active_p || (available == 0)) {
1083 /* If we are in GC, or totally out of memory there is no way
1084 * to sanely transfer control to the lisp-side of things.
1086 struct thread *thread = arch_os_get_current_thread();
1087 print_generation_stats(1);
1088 fprintf(stderr, "GC control variables:\n");
1089 fprintf(stderr, " *GC-INHIBIT* = %s\n *GC-PENDING* = %s\n",
1090 SymbolValue(GC_INHIBIT,thread)==NIL ? "false" : "true",
1091 SymbolValue(GC_PENDING,thread)==NIL ? "false" : "true");
1092 #ifdef LISP_FEATURE_SB_THREAD
1093 fprintf(stderr, " *STOP-FOR-GC-PENDING* = %s\n",
1094 SymbolValue(STOP_FOR_GC_PENDING,thread)==NIL ? "false" : "true");
1096 lose("Heap exhausted, game over.");
1099 /* FIXME: assert free_pages_lock held */
1100 (void)thread_mutex_unlock(&free_pages_lock);
1101 funcall2(StaticSymbolFunction(HEAP_EXHAUSTED_ERROR),
1102 alloc_number(available), alloc_number(requested));
1103 lose("HEAP-EXHAUSTED-ERROR fell through");
1108 gc_find_freeish_pages(page_index_t *restart_page_ptr, long nbytes, int unboxed)
1110 page_index_t first_page;
1111 page_index_t last_page;
1113 page_index_t restart_page=*restart_page_ptr;
1116 int large_p=(nbytes>=large_object_size);
1117 /* FIXME: assert(free_pages_lock is held); */
1119 /* Search for a contiguous free space of at least nbytes. If it's
1120 * a large object then align it on a page boundary by searching
1121 * for a free page. */
1123 if (gencgc_alloc_start_page != -1) {
1124 restart_page = gencgc_alloc_start_page;
1128 first_page = restart_page;
1130 while ((first_page < page_table_pages)
1131 && (page_table[first_page].allocated != FREE_PAGE_FLAG))
1134 while (first_page < page_table_pages) {
1135 if(page_table[first_page].allocated == FREE_PAGE_FLAG)
1137 if((page_table[first_page].allocated ==
1138 (unboxed ? UNBOXED_PAGE_FLAG : BOXED_PAGE_FLAG)) &&
1139 (page_table[first_page].large_object == 0) &&
1140 (page_table[first_page].gen == gc_alloc_generation) &&
1141 (page_table[first_page].bytes_used < (PAGE_BYTES-32)) &&
1142 (page_table[first_page].write_protected == 0) &&
1143 (page_table[first_page].dont_move == 0)) {
1149 if (first_page >= page_table_pages)
1150 gc_heap_exhausted_error_or_lose(0, nbytes);
1152 gc_assert(page_table[first_page].write_protected == 0);
1154 last_page = first_page;
1155 bytes_found = PAGE_BYTES - page_table[first_page].bytes_used;
1157 while (((bytes_found < nbytes)
1158 || (!large_p && (num_pages < 2)))
1159 && (last_page < (page_table_pages-1))
1160 && (page_table[last_page+1].allocated == FREE_PAGE_FLAG)) {
1163 bytes_found += PAGE_BYTES;
1164 gc_assert(page_table[last_page].write_protected == 0);
1167 region_size = (PAGE_BYTES - page_table[first_page].bytes_used)
1168 + PAGE_BYTES*(last_page-first_page);
1170 gc_assert(bytes_found == region_size);
1171 restart_page = last_page + 1;
1172 } while ((restart_page < page_table_pages) && (bytes_found < nbytes));
1174 /* Check for a failure */
1175 if ((restart_page >= page_table_pages) && (bytes_found < nbytes))
1176 gc_heap_exhausted_error_or_lose(bytes_found, nbytes);
1178 *restart_page_ptr=first_page;
1183 /* Allocate bytes. All the rest of the special-purpose allocation
1184 * functions will eventually call this */
1187 gc_alloc_with_region(long nbytes,int unboxed_p, struct alloc_region *my_region,
1190 void *new_free_pointer;
1192 if (nbytes>=large_object_size)
1193 return gc_alloc_large(nbytes,unboxed_p,my_region);
1195 /* Check whether there is room in the current alloc region. */
1196 new_free_pointer = my_region->free_pointer + nbytes;
1198 /* fprintf(stderr, "alloc %d bytes from %p to %p\n", nbytes,
1199 my_region->free_pointer, new_free_pointer); */
1201 if (new_free_pointer <= my_region->end_addr) {
1202 /* If so then allocate from the current alloc region. */
1203 void *new_obj = my_region->free_pointer;
1204 my_region->free_pointer = new_free_pointer;
1206 /* Unless a `quick' alloc was requested, check whether the
1207 alloc region is almost empty. */
1209 (my_region->end_addr - my_region->free_pointer) <= 32) {
1210 /* If so, finished with the current region. */
1211 gc_alloc_update_page_tables(unboxed_p, my_region);
1212 /* Set up a new region. */
1213 gc_alloc_new_region(32 /*bytes*/, unboxed_p, my_region);
1216 return((void *)new_obj);
1219 /* Else not enough free space in the current region: retry with a
1222 gc_alloc_update_page_tables(unboxed_p, my_region);
1223 gc_alloc_new_region(nbytes, unboxed_p, my_region);
1224 return gc_alloc_with_region(nbytes,unboxed_p,my_region,0);
1227 /* these are only used during GC: all allocation from the mutator calls
1228 * alloc() -> gc_alloc_with_region() with the appropriate per-thread
1232 gc_general_alloc(long nbytes,int unboxed_p,int quick_p)
1234 struct alloc_region *my_region =
1235 unboxed_p ? &unboxed_region : &boxed_region;
1236 return gc_alloc_with_region(nbytes,unboxed_p, my_region,quick_p);
1239 static inline void *
1240 gc_quick_alloc(long nbytes)
1242 return gc_general_alloc(nbytes,ALLOC_BOXED,ALLOC_QUICK);
1245 static inline void *
1246 gc_quick_alloc_large(long nbytes)
1248 return gc_general_alloc(nbytes,ALLOC_BOXED,ALLOC_QUICK);
1251 static inline void *
1252 gc_alloc_unboxed(long nbytes)
1254 return gc_general_alloc(nbytes,ALLOC_UNBOXED,0);
1257 static inline void *
1258 gc_quick_alloc_unboxed(long nbytes)
1260 return gc_general_alloc(nbytes,ALLOC_UNBOXED,ALLOC_QUICK);
1263 static inline void *
1264 gc_quick_alloc_large_unboxed(long nbytes)
1266 return gc_general_alloc(nbytes,ALLOC_UNBOXED,ALLOC_QUICK);
1270 * scavenging/transporting routines derived from gc.c in CMU CL ca. 18b
1273 extern long (*scavtab[256])(lispobj *where, lispobj object);
1274 extern lispobj (*transother[256])(lispobj object);
1275 extern long (*sizetab[256])(lispobj *where);
1277 /* Copy a large boxed object. If the object is in a large object
1278 * region then it is simply promoted, else it is copied. If it's large
1279 * enough then it's copied to a large object region.
1281 * Vectors may have shrunk. If the object is not copied the space
1282 * needs to be reclaimed, and the page_tables corrected. */
1284 copy_large_object(lispobj object, long nwords)
1288 page_index_t first_page;
1290 gc_assert(is_lisp_pointer(object));
1291 gc_assert(from_space_p(object));
1292 gc_assert((nwords & 0x01) == 0);
1295 /* Check whether it's in a large object region. */
1296 first_page = find_page_index((void *)object);
1297 gc_assert(first_page >= 0);
1299 if (page_table[first_page].large_object) {
1301 /* Promote the object. */
1303 long remaining_bytes;
1304 page_index_t next_page;
1306 long old_bytes_used;
1308 /* Note: Any page write-protection must be removed, else a
1309 * later scavenge_newspace may incorrectly not scavenge these
1310 * pages. This would not be necessary if they are added to the
1311 * new areas, but let's do it for them all (they'll probably
1312 * be written anyway?). */
1314 gc_assert(page_table[first_page].first_object_offset == 0);
1316 next_page = first_page;
1317 remaining_bytes = nwords*N_WORD_BYTES;
1318 while (remaining_bytes > PAGE_BYTES) {
1319 gc_assert(page_table[next_page].gen == from_space);
1320 gc_assert(page_table[next_page].allocated == BOXED_PAGE_FLAG);
1321 gc_assert(page_table[next_page].large_object);
1322 gc_assert(page_table[next_page].first_object_offset==
1323 -PAGE_BYTES*(next_page-first_page));
1324 gc_assert(page_table[next_page].bytes_used == PAGE_BYTES);
1326 page_table[next_page].gen = new_space;
1328 /* Remove any write-protection. We should be able to rely
1329 * on the write-protect flag to avoid redundant calls. */
1330 if (page_table[next_page].write_protected) {
1331 os_protect(page_address(next_page), PAGE_BYTES, OS_VM_PROT_ALL);
1332 page_table[next_page].write_protected = 0;
1334 remaining_bytes -= PAGE_BYTES;
1338 /* Now only one page remains, but the object may have shrunk
1339 * so there may be more unused pages which will be freed. */
1341 /* The object may have shrunk but shouldn't have grown. */
1342 gc_assert(page_table[next_page].bytes_used >= remaining_bytes);
1344 page_table[next_page].gen = new_space;
1345 gc_assert(page_table[next_page].allocated == BOXED_PAGE_FLAG);
1347 /* Adjust the bytes_used. */
1348 old_bytes_used = page_table[next_page].bytes_used;
1349 page_table[next_page].bytes_used = remaining_bytes;
1351 bytes_freed = old_bytes_used - remaining_bytes;
1353 /* Free any remaining pages; needs care. */
1355 while ((old_bytes_used == PAGE_BYTES) &&
1356 (page_table[next_page].gen == from_space) &&
1357 (page_table[next_page].allocated == BOXED_PAGE_FLAG) &&
1358 page_table[next_page].large_object &&
1359 (page_table[next_page].first_object_offset ==
1360 -(next_page - first_page)*PAGE_BYTES)) {
1361 /* Checks out OK, free the page. Don't need to bother zeroing
1362 * pages as this should have been done before shrinking the
1363 * object. These pages shouldn't be write-protected as they
1364 * should be zero filled. */
1365 gc_assert(page_table[next_page].write_protected == 0);
1367 old_bytes_used = page_table[next_page].bytes_used;
1368 page_table[next_page].allocated = FREE_PAGE_FLAG;
1369 page_table[next_page].bytes_used = 0;
1370 bytes_freed += old_bytes_used;
1374 generations[from_space].bytes_allocated -= N_WORD_BYTES*nwords +
1376 generations[new_space].bytes_allocated += N_WORD_BYTES*nwords;
1377 bytes_allocated -= bytes_freed;
1379 /* Add the region to the new_areas if requested. */
1380 add_new_area(first_page,0,nwords*N_WORD_BYTES);
1384 /* Get tag of object. */
1385 tag = lowtag_of(object);
1387 /* Allocate space. */
1388 new = gc_quick_alloc_large(nwords*N_WORD_BYTES);
1390 memcpy(new,native_pointer(object),nwords*N_WORD_BYTES);
1392 /* Return Lisp pointer of new object. */
1393 return ((lispobj) new) | tag;
1397 /* to copy unboxed objects */
1399 copy_unboxed_object(lispobj object, long nwords)
1404 gc_assert(is_lisp_pointer(object));
1405 gc_assert(from_space_p(object));
1406 gc_assert((nwords & 0x01) == 0);
1408 /* Get tag of object. */
1409 tag = lowtag_of(object);
1411 /* Allocate space. */
1412 new = gc_quick_alloc_unboxed(nwords*N_WORD_BYTES);
1414 memcpy(new,native_pointer(object),nwords*N_WORD_BYTES);
1416 /* Return Lisp pointer of new object. */
1417 return ((lispobj) new) | tag;
1420 /* to copy large unboxed objects
1422 * If the object is in a large object region then it is simply
1423 * promoted, else it is copied. If it's large enough then it's copied
1424 * to a large object region.
1426 * Bignums and vectors may have shrunk. If the object is not copied
1427 * the space needs to be reclaimed, and the page_tables corrected.
1429 * KLUDGE: There's a lot of cut-and-paste duplication between this
1430 * function and copy_large_object(..). -- WHN 20000619 */
1432 copy_large_unboxed_object(lispobj object, long nwords)
1436 page_index_t first_page;
1438 gc_assert(is_lisp_pointer(object));
1439 gc_assert(from_space_p(object));
1440 gc_assert((nwords & 0x01) == 0);
1442 if ((nwords > 1024*1024) && gencgc_verbose)
1443 FSHOW((stderr, "/copy_large_unboxed_object: %d bytes\n", nwords*N_WORD_BYTES));
1445 /* Check whether it's a large object. */
1446 first_page = find_page_index((void *)object);
1447 gc_assert(first_page >= 0);
1449 if (page_table[first_page].large_object) {
1450 /* Promote the object. Note: Unboxed objects may have been
1451 * allocated to a BOXED region so it may be necessary to
1452 * change the region to UNBOXED. */
1453 long remaining_bytes;
1454 page_index_t next_page;
1456 long old_bytes_used;
1458 gc_assert(page_table[first_page].first_object_offset == 0);
1460 next_page = first_page;
1461 remaining_bytes = nwords*N_WORD_BYTES;
1462 while (remaining_bytes > PAGE_BYTES) {
1463 gc_assert(page_table[next_page].gen == from_space);
1464 gc_assert((page_table[next_page].allocated == UNBOXED_PAGE_FLAG)
1465 || (page_table[next_page].allocated == BOXED_PAGE_FLAG));
1466 gc_assert(page_table[next_page].large_object);
1467 gc_assert(page_table[next_page].first_object_offset==
1468 -PAGE_BYTES*(next_page-first_page));
1469 gc_assert(page_table[next_page].bytes_used == PAGE_BYTES);
1471 page_table[next_page].gen = new_space;
1472 page_table[next_page].allocated = UNBOXED_PAGE_FLAG;
1473 remaining_bytes -= PAGE_BYTES;
1477 /* Now only one page remains, but the object may have shrunk so
1478 * there may be more unused pages which will be freed. */
1480 /* Object may have shrunk but shouldn't have grown - check. */
1481 gc_assert(page_table[next_page].bytes_used >= remaining_bytes);
1483 page_table[next_page].gen = new_space;
1484 page_table[next_page].allocated = UNBOXED_PAGE_FLAG;
1486 /* Adjust the bytes_used. */
1487 old_bytes_used = page_table[next_page].bytes_used;
1488 page_table[next_page].bytes_used = remaining_bytes;
1490 bytes_freed = old_bytes_used - remaining_bytes;
1492 /* Free any remaining pages; needs care. */
1494 while ((old_bytes_used == PAGE_BYTES) &&
1495 (page_table[next_page].gen == from_space) &&
1496 ((page_table[next_page].allocated == UNBOXED_PAGE_FLAG)
1497 || (page_table[next_page].allocated == BOXED_PAGE_FLAG)) &&
1498 page_table[next_page].large_object &&
1499 (page_table[next_page].first_object_offset ==
1500 -(next_page - first_page)*PAGE_BYTES)) {
1501 /* Checks out OK, free the page. Don't need to both zeroing
1502 * pages as this should have been done before shrinking the
1503 * object. These pages shouldn't be write-protected, even if
1504 * boxed they should be zero filled. */
1505 gc_assert(page_table[next_page].write_protected == 0);
1507 old_bytes_used = page_table[next_page].bytes_used;
1508 page_table[next_page].allocated = FREE_PAGE_FLAG;
1509 page_table[next_page].bytes_used = 0;
1510 bytes_freed += old_bytes_used;
1514 if ((bytes_freed > 0) && gencgc_verbose)
1516 "/copy_large_unboxed bytes_freed=%d\n",
1519 generations[from_space].bytes_allocated -= nwords*N_WORD_BYTES + bytes_freed;
1520 generations[new_space].bytes_allocated += nwords*N_WORD_BYTES;
1521 bytes_allocated -= bytes_freed;
1526 /* Get tag of object. */
1527 tag = lowtag_of(object);
1529 /* Allocate space. */
1530 new = gc_quick_alloc_large_unboxed(nwords*N_WORD_BYTES);
1532 /* Copy the object. */
1533 memcpy(new,native_pointer(object),nwords*N_WORD_BYTES);
1535 /* Return Lisp pointer of new object. */
1536 return ((lispobj) new) | tag;
1545 * code and code-related objects
1548 static lispobj trans_fun_header(lispobj object);
1549 static lispobj trans_boxed(lispobj object);
1552 /* Scan a x86 compiled code object, looking for possible fixups that
1553 * have been missed after a move.
1555 * Two types of fixups are needed:
1556 * 1. Absolute fixups to within the code object.
1557 * 2. Relative fixups to outside the code object.
1559 * Currently only absolute fixups to the constant vector, or to the
1560 * code area are checked. */
1562 sniff_code_object(struct code *code, unsigned long displacement)
1564 #ifdef LISP_FEATURE_X86
1565 long nheader_words, ncode_words, nwords;
1567 void *constants_start_addr = NULL, *constants_end_addr;
1568 void *code_start_addr, *code_end_addr;
1569 int fixup_found = 0;
1571 if (!check_code_fixups)
1574 FSHOW((stderr, "/sniffing code: %p, %lu\n", code, displacement));
1576 ncode_words = fixnum_value(code->code_size);
1577 nheader_words = HeaderValue(*(lispobj *)code);
1578 nwords = ncode_words + nheader_words;
1580 constants_start_addr = (void *)code + 5*N_WORD_BYTES;
1581 constants_end_addr = (void *)code + nheader_words*N_WORD_BYTES;
1582 code_start_addr = (void *)code + nheader_words*N_WORD_BYTES;
1583 code_end_addr = (void *)code + nwords*N_WORD_BYTES;
1585 /* Work through the unboxed code. */
1586 for (p = code_start_addr; p < code_end_addr; p++) {
1587 void *data = *(void **)p;
1588 unsigned d1 = *((unsigned char *)p - 1);
1589 unsigned d2 = *((unsigned char *)p - 2);
1590 unsigned d3 = *((unsigned char *)p - 3);
1591 unsigned d4 = *((unsigned char *)p - 4);
1593 unsigned d5 = *((unsigned char *)p - 5);
1594 unsigned d6 = *((unsigned char *)p - 6);
1597 /* Check for code references. */
1598 /* Check for a 32 bit word that looks like an absolute
1599 reference to within the code adea of the code object. */
1600 if ((data >= (code_start_addr-displacement))
1601 && (data < (code_end_addr-displacement))) {
1602 /* function header */
1604 && (((unsigned)p - 4 - 4*HeaderValue(*((unsigned *)p-1))) == (unsigned)code)) {
1605 /* Skip the function header */
1609 /* the case of PUSH imm32 */
1613 "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1614 p, d6, d5, d4, d3, d2, d1, data));
1615 FSHOW((stderr, "/PUSH $0x%.8x\n", data));
1617 /* the case of MOV [reg-8],imm32 */
1619 && (d2==0x40 || d2==0x41 || d2==0x42 || d2==0x43
1620 || d2==0x45 || d2==0x46 || d2==0x47)
1624 "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1625 p, d6, d5, d4, d3, d2, d1, data));
1626 FSHOW((stderr, "/MOV [reg-8],$0x%.8x\n", data));
1628 /* the case of LEA reg,[disp32] */
1629 if ((d2 == 0x8d) && ((d1 & 0xc7) == 5)) {
1632 "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1633 p, d6, d5, d4, d3, d2, d1, data));
1634 FSHOW((stderr,"/LEA reg,[$0x%.8x]\n", data));
1638 /* Check for constant references. */
1639 /* Check for a 32 bit word that looks like an absolute
1640 reference to within the constant vector. Constant references
1642 if ((data >= (constants_start_addr-displacement))
1643 && (data < (constants_end_addr-displacement))
1644 && (((unsigned)data & 0x3) == 0)) {
1649 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1650 p, d6, d5, d4, d3, d2, d1, data));
1651 FSHOW((stderr,"/MOV eax,0x%.8x\n", data));
1654 /* the case of MOV m32,EAX */
1658 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1659 p, d6, d5, d4, d3, d2, d1, data));
1660 FSHOW((stderr, "/MOV 0x%.8x,eax\n", data));
1663 /* the case of CMP m32,imm32 */
1664 if ((d1 == 0x3d) && (d2 == 0x81)) {
1667 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1668 p, d6, d5, d4, d3, d2, d1, data));
1670 FSHOW((stderr, "/CMP 0x%.8x,immed32\n", data));
1673 /* Check for a mod=00, r/m=101 byte. */
1674 if ((d1 & 0xc7) == 5) {
1679 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1680 p, d6, d5, d4, d3, d2, d1, data));
1681 FSHOW((stderr,"/CMP 0x%.8x,reg\n", data));
1683 /* the case of CMP reg32,m32 */
1687 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1688 p, d6, d5, d4, d3, d2, d1, data));
1689 FSHOW((stderr, "/CMP reg32,0x%.8x\n", data));
1691 /* the case of MOV m32,reg32 */
1695 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1696 p, d6, d5, d4, d3, d2, d1, data));
1697 FSHOW((stderr, "/MOV 0x%.8x,reg32\n", data));
1699 /* the case of MOV reg32,m32 */
1703 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1704 p, d6, d5, d4, d3, d2, d1, data));
1705 FSHOW((stderr, "/MOV reg32,0x%.8x\n", data));
1707 /* the case of LEA reg32,m32 */
1711 "abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1712 p, d6, d5, d4, d3, d2, d1, data));
1713 FSHOW((stderr, "/LEA reg32,0x%.8x\n", data));
1719 /* If anything was found, print some information on the code
1723 "/compiled code object at %x: header words = %d, code words = %d\n",
1724 code, nheader_words, ncode_words));
1726 "/const start = %x, end = %x\n",
1727 constants_start_addr, constants_end_addr));
1729 "/code start = %x, end = %x\n",
1730 code_start_addr, code_end_addr));
1736 gencgc_apply_code_fixups(struct code *old_code, struct code *new_code)
1738 /* x86-64 uses pc-relative addressing instead of this kludge */
1739 #ifndef LISP_FEATURE_X86_64
1740 long nheader_words, ncode_words, nwords;
1741 void *constants_start_addr, *constants_end_addr;
1742 void *code_start_addr, *code_end_addr;
1743 lispobj fixups = NIL;
1744 unsigned long displacement = (unsigned long)new_code - (unsigned long)old_code;
1745 struct vector *fixups_vector;
1747 ncode_words = fixnum_value(new_code->code_size);
1748 nheader_words = HeaderValue(*(lispobj *)new_code);
1749 nwords = ncode_words + nheader_words;
1751 "/compiled code object at %x: header words = %d, code words = %d\n",
1752 new_code, nheader_words, ncode_words)); */
1753 constants_start_addr = (void *)new_code + 5*N_WORD_BYTES;
1754 constants_end_addr = (void *)new_code + nheader_words*N_WORD_BYTES;
1755 code_start_addr = (void *)new_code + nheader_words*N_WORD_BYTES;
1756 code_end_addr = (void *)new_code + nwords*N_WORD_BYTES;
1759 "/const start = %x, end = %x\n",
1760 constants_start_addr,constants_end_addr));
1762 "/code start = %x; end = %x\n",
1763 code_start_addr,code_end_addr));
1766 /* The first constant should be a pointer to the fixups for this
1767 code objects. Check. */
1768 fixups = new_code->constants[0];
1770 /* It will be 0 or the unbound-marker if there are no fixups (as
1771 * will be the case if the code object has been purified, for
1772 * example) and will be an other pointer if it is valid. */
1773 if ((fixups == 0) || (fixups == UNBOUND_MARKER_WIDETAG) ||
1774 !is_lisp_pointer(fixups)) {
1775 /* Check for possible errors. */
1776 if (check_code_fixups)
1777 sniff_code_object(new_code, displacement);
1782 fixups_vector = (struct vector *)native_pointer(fixups);
1784 /* Could be pointing to a forwarding pointer. */
1785 /* FIXME is this always in from_space? if so, could replace this code with
1786 * forwarding_pointer_p/forwarding_pointer_value */
1787 if (is_lisp_pointer(fixups) &&
1788 (find_page_index((void*)fixups_vector) != -1) &&
1789 (fixups_vector->header == 0x01)) {
1790 /* If so, then follow it. */
1791 /*SHOW("following pointer to a forwarding pointer");*/
1792 fixups_vector = (struct vector *)native_pointer((lispobj)fixups_vector->length);
1795 /*SHOW("got fixups");*/
1797 if (widetag_of(fixups_vector->header) == SIMPLE_ARRAY_WORD_WIDETAG) {
1798 /* Got the fixups for the code block. Now work through the vector,
1799 and apply a fixup at each address. */
1800 long length = fixnum_value(fixups_vector->length);
1802 for (i = 0; i < length; i++) {
1803 unsigned long offset = fixups_vector->data[i];
1804 /* Now check the current value of offset. */
1805 unsigned long old_value =
1806 *(unsigned long *)((unsigned long)code_start_addr + offset);
1808 /* If it's within the old_code object then it must be an
1809 * absolute fixup (relative ones are not saved) */
1810 if ((old_value >= (unsigned long)old_code)
1811 && (old_value < ((unsigned long)old_code + nwords*N_WORD_BYTES)))
1812 /* So add the dispacement. */
1813 *(unsigned long *)((unsigned long)code_start_addr + offset) =
1814 old_value + displacement;
1816 /* It is outside the old code object so it must be a
1817 * relative fixup (absolute fixups are not saved). So
1818 * subtract the displacement. */
1819 *(unsigned long *)((unsigned long)code_start_addr + offset) =
1820 old_value - displacement;
1823 /* This used to just print a note to stderr, but a bogus fixup seems to
1824 * indicate real heap corruption, so a hard hailure is in order. */
1825 lose("fixup vector %p has a bad widetag: %d\n", fixups_vector, widetag_of(fixups_vector->header));
1828 /* Check for possible errors. */
1829 if (check_code_fixups) {
1830 sniff_code_object(new_code,displacement);
1837 trans_boxed_large(lispobj object)
1840 unsigned long length;
1842 gc_assert(is_lisp_pointer(object));
1844 header = *((lispobj *) native_pointer(object));
1845 length = HeaderValue(header) + 1;
1846 length = CEILING(length, 2);
1848 return copy_large_object(object, length);
1851 /* Doesn't seem to be used, delete it after the grace period. */
1854 trans_unboxed_large(lispobj object)
1857 unsigned long length;
1859 gc_assert(is_lisp_pointer(object));
1861 header = *((lispobj *) native_pointer(object));
1862 length = HeaderValue(header) + 1;
1863 length = CEILING(length, 2);
1865 return copy_large_unboxed_object(object, length);
1871 * Lutexes. Using the normal finalization machinery for finalizing
1872 * lutexes is tricky, since the finalization depends on working lutexes.
1873 * So we track the lutexes in the GC and finalize them manually.
1876 #if defined(LUTEX_WIDETAG)
1879 * Start tracking LUTEX in the GC, by adding it to the linked list of
1880 * lutexes in the nursery generation. The caller is responsible for
1881 * locking, and GCs must be inhibited until the registration is
1885 gencgc_register_lutex (struct lutex *lutex) {
1886 int index = find_page_index(lutex);
1887 generation_index_t gen;
1890 /* This lutex is in static space, so we don't need to worry about
1896 gen = page_table[index].gen;
1898 gc_assert(gen >= 0);
1899 gc_assert(gen < NUM_GENERATIONS);
1901 head = generations[gen].lutexes;
1908 generations[gen].lutexes = lutex;
1912 * Stop tracking LUTEX in the GC by removing it from the appropriate
1913 * linked lists. This will only be called during GC, so no locking is
1917 gencgc_unregister_lutex (struct lutex *lutex) {
1919 lutex->prev->next = lutex->next;
1921 generations[lutex->gen].lutexes = lutex->next;
1925 lutex->next->prev = lutex->prev;
1934 * Mark all lutexes in generation GEN as not live.
1937 unmark_lutexes (generation_index_t gen) {
1938 struct lutex *lutex = generations[gen].lutexes;
1942 lutex = lutex->next;
1947 * Finalize all lutexes in generation GEN that have not been marked live.
1950 reap_lutexes (generation_index_t gen) {
1951 struct lutex *lutex = generations[gen].lutexes;
1954 struct lutex *next = lutex->next;
1956 lutex_destroy((tagged_lutex_t) lutex);
1957 gencgc_unregister_lutex(lutex);
1964 * Mark LUTEX as live.
1967 mark_lutex (lispobj tagged_lutex) {
1968 struct lutex *lutex = (struct lutex*) native_pointer(tagged_lutex);
1974 * Move all lutexes in generation FROM to generation TO.
1977 move_lutexes (generation_index_t from, generation_index_t to) {
1978 struct lutex *tail = generations[from].lutexes;
1980 /* Nothing to move */
1984 /* Change the generation of the lutexes in FROM. */
1985 while (tail->next) {
1991 /* Link the last lutex in the FROM list to the start of the TO list */
1992 tail->next = generations[to].lutexes;
1994 /* And vice versa */
1995 if (generations[to].lutexes) {
1996 generations[to].lutexes->prev = tail;
1999 /* And update the generations structures to match this */
2000 generations[to].lutexes = generations[from].lutexes;
2001 generations[from].lutexes = NULL;
2005 scav_lutex(lispobj *where, lispobj object)
2007 mark_lutex((lispobj) where);
2009 return CEILING(sizeof(struct lutex)/sizeof(lispobj), 2);
2013 trans_lutex(lispobj object)
2015 struct lutex *lutex = (struct lutex *) native_pointer(object);
2017 size_t words = CEILING(sizeof(struct lutex)/sizeof(lispobj), 2);
2018 gc_assert(is_lisp_pointer(object));
2019 copied = copy_object(object, words);
2021 /* Update the links, since the lutex moved in memory. */
2023 lutex->next->prev = (struct lutex *) native_pointer(copied);
2027 lutex->prev->next = (struct lutex *) native_pointer(copied);
2029 generations[lutex->gen].lutexes =
2030 (struct lutex *) native_pointer(copied);
2037 size_lutex(lispobj *where)
2039 return CEILING(sizeof(struct lutex)/sizeof(lispobj), 2);
2041 #endif /* LUTEX_WIDETAG */
2048 /* XX This is a hack adapted from cgc.c. These don't work too
2049 * efficiently with the gencgc as a list of the weak pointers is
2050 * maintained within the objects which causes writes to the pages. A
2051 * limited attempt is made to avoid unnecessary writes, but this needs
2053 #define WEAK_POINTER_NWORDS \
2054 CEILING((sizeof(struct weak_pointer) / sizeof(lispobj)), 2)
2057 scav_weak_pointer(lispobj *where, lispobj object)
2059 /* Since we overwrite the 'next' field, we have to make
2060 * sure not to do so for pointers already in the list.
2061 * Instead of searching the list of weak_pointers each
2062 * time, we ensure that next is always NULL when the weak
2063 * pointer isn't in the list, and not NULL otherwise.
2064 * Since we can't use NULL to denote end of list, we
2065 * use a pointer back to the same weak_pointer.
2067 struct weak_pointer * wp = (struct weak_pointer*)where;
2069 if (NULL == wp->next) {
2070 wp->next = weak_pointers;
2072 if (NULL == wp->next)
2076 /* Do not let GC scavenge the value slot of the weak pointer.
2077 * (That is why it is a weak pointer.) */
2079 return WEAK_POINTER_NWORDS;
2084 search_read_only_space(void *pointer)
2086 lispobj *start = (lispobj *) READ_ONLY_SPACE_START;
2087 lispobj *end = (lispobj *) SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0);
2088 if ((pointer < (void *)start) || (pointer >= (void *)end))
2090 return (gc_search_space(start,
2091 (((lispobj *)pointer)+2)-start,
2092 (lispobj *) pointer));
2096 search_static_space(void *pointer)
2098 lispobj *start = (lispobj *)STATIC_SPACE_START;
2099 lispobj *end = (lispobj *)SymbolValue(STATIC_SPACE_FREE_POINTER,0);
2100 if ((pointer < (void *)start) || (pointer >= (void *)end))
2102 return (gc_search_space(start,
2103 (((lispobj *)pointer)+2)-start,
2104 (lispobj *) pointer));
2107 /* a faster version for searching the dynamic space. This will work even
2108 * if the object is in a current allocation region. */
2110 search_dynamic_space(void *pointer)
2112 page_index_t page_index = find_page_index(pointer);
2115 /* The address may be invalid, so do some checks. */
2116 if ((page_index == -1) ||
2117 (page_table[page_index].allocated == FREE_PAGE_FLAG))
2119 start = (lispobj *)((void *)page_address(page_index)
2120 + page_table[page_index].first_object_offset);
2121 return (gc_search_space(start,
2122 (((lispobj *)pointer)+2)-start,
2123 (lispobj *)pointer));
2126 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
2128 /* Helper for valid_lisp_pointer_p and
2129 * possibly_valid_dynamic_space_pointer.
2131 * pointer is the pointer to validate, and start_addr is the address
2132 * of the enclosing object.
2135 looks_like_valid_lisp_pointer_p(lispobj *pointer, lispobj *start_addr)
2137 /* We need to allow raw pointers into Code objects for return
2138 * addresses. This will also pick up pointers to functions in code
2140 if (widetag_of(*start_addr) == CODE_HEADER_WIDETAG)
2141 /* XXX could do some further checks here */
2144 if (!is_lisp_pointer((lispobj)pointer)) {
2148 /* Check that the object pointed to is consistent with the pointer
2150 switch (lowtag_of((lispobj)pointer)) {
2151 case FUN_POINTER_LOWTAG:
2152 /* Start_addr should be the enclosing code object, or a closure
2154 switch (widetag_of(*start_addr)) {
2155 case CODE_HEADER_WIDETAG:
2156 /* This case is probably caught above. */
2158 case CLOSURE_HEADER_WIDETAG:
2159 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
2160 if ((unsigned long)pointer !=
2161 ((unsigned long)start_addr+FUN_POINTER_LOWTAG)) {
2165 pointer, start_addr, *start_addr));
2173 pointer, start_addr, *start_addr));
2177 case LIST_POINTER_LOWTAG:
2178 if ((unsigned long)pointer !=
2179 ((unsigned long)start_addr+LIST_POINTER_LOWTAG)) {
2183 pointer, start_addr, *start_addr));
2186 /* Is it plausible cons? */
2187 if ((is_lisp_pointer(start_addr[0])
2188 || (fixnump(start_addr[0]))
2189 || (widetag_of(start_addr[0]) == CHARACTER_WIDETAG)
2190 #if N_WORD_BITS == 64
2191 || (widetag_of(start_addr[0]) == SINGLE_FLOAT_WIDETAG)
2193 || (widetag_of(start_addr[0]) == UNBOUND_MARKER_WIDETAG))
2194 && (is_lisp_pointer(start_addr[1])
2195 || (fixnump(start_addr[1]))
2196 || (widetag_of(start_addr[1]) == CHARACTER_WIDETAG)
2197 #if N_WORD_BITS == 64
2198 || (widetag_of(start_addr[1]) == SINGLE_FLOAT_WIDETAG)
2200 || (widetag_of(start_addr[1]) == UNBOUND_MARKER_WIDETAG)))
2206 pointer, start_addr, *start_addr));
2209 case INSTANCE_POINTER_LOWTAG:
2210 if ((unsigned long)pointer !=
2211 ((unsigned long)start_addr+INSTANCE_POINTER_LOWTAG)) {
2215 pointer, start_addr, *start_addr));
2218 if (widetag_of(start_addr[0]) != INSTANCE_HEADER_WIDETAG) {
2222 pointer, start_addr, *start_addr));
2226 case OTHER_POINTER_LOWTAG:
2227 if ((unsigned long)pointer !=
2228 ((unsigned long)start_addr+OTHER_POINTER_LOWTAG)) {
2232 pointer, start_addr, *start_addr));
2235 /* Is it plausible? Not a cons. XXX should check the headers. */
2236 if (is_lisp_pointer(start_addr[0]) || ((start_addr[0] & 3) == 0)) {
2240 pointer, start_addr, *start_addr));
2243 switch (widetag_of(start_addr[0])) {
2244 case UNBOUND_MARKER_WIDETAG:
2245 case NO_TLS_VALUE_MARKER_WIDETAG:
2246 case CHARACTER_WIDETAG:
2247 #if N_WORD_BITS == 64
2248 case SINGLE_FLOAT_WIDETAG:
2253 pointer, start_addr, *start_addr));
2256 /* only pointed to by function pointers? */
2257 case CLOSURE_HEADER_WIDETAG:
2258 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
2262 pointer, start_addr, *start_addr));
2265 case INSTANCE_HEADER_WIDETAG:
2269 pointer, start_addr, *start_addr));
2272 /* the valid other immediate pointer objects */
2273 case SIMPLE_VECTOR_WIDETAG:
2275 case COMPLEX_WIDETAG:
2276 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
2277 case COMPLEX_SINGLE_FLOAT_WIDETAG:
2279 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
2280 case COMPLEX_DOUBLE_FLOAT_WIDETAG:
2282 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
2283 case COMPLEX_LONG_FLOAT_WIDETAG:
2285 case SIMPLE_ARRAY_WIDETAG:
2286 case COMPLEX_BASE_STRING_WIDETAG:
2287 #ifdef COMPLEX_CHARACTER_STRING_WIDETAG
2288 case COMPLEX_CHARACTER_STRING_WIDETAG:
2290 case COMPLEX_VECTOR_NIL_WIDETAG:
2291 case COMPLEX_BIT_VECTOR_WIDETAG:
2292 case COMPLEX_VECTOR_WIDETAG:
2293 case COMPLEX_ARRAY_WIDETAG:
2294 case VALUE_CELL_HEADER_WIDETAG:
2295 case SYMBOL_HEADER_WIDETAG:
2297 case CODE_HEADER_WIDETAG:
2298 case BIGNUM_WIDETAG:
2299 #if N_WORD_BITS != 64
2300 case SINGLE_FLOAT_WIDETAG:
2302 case DOUBLE_FLOAT_WIDETAG:
2303 #ifdef LONG_FLOAT_WIDETAG
2304 case LONG_FLOAT_WIDETAG:
2306 case SIMPLE_BASE_STRING_WIDETAG:
2307 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
2308 case SIMPLE_CHARACTER_STRING_WIDETAG:
2310 case SIMPLE_BIT_VECTOR_WIDETAG:
2311 case SIMPLE_ARRAY_NIL_WIDETAG:
2312 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
2313 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
2314 case SIMPLE_ARRAY_UNSIGNED_BYTE_7_WIDETAG:
2315 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
2316 case SIMPLE_ARRAY_UNSIGNED_BYTE_15_WIDETAG:
2317 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
2318 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_29_WIDETAG
2319 case SIMPLE_ARRAY_UNSIGNED_BYTE_29_WIDETAG:
2321 case SIMPLE_ARRAY_UNSIGNED_BYTE_31_WIDETAG:
2322 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
2323 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_60_WIDETAG
2324 case SIMPLE_ARRAY_UNSIGNED_BYTE_60_WIDETAG:
2326 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG
2327 case SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG:
2329 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG
2330 case SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG:
2332 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
2333 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
2335 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
2336 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
2338 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
2339 case SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG:
2341 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
2342 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
2344 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_61_WIDETAG
2345 case SIMPLE_ARRAY_SIGNED_BYTE_61_WIDETAG:
2347 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG
2348 case SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG:
2350 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
2351 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
2352 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
2353 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
2355 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
2356 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
2358 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
2359 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
2361 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
2362 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
2365 case WEAK_POINTER_WIDETAG:
2366 #ifdef LUTEX_WIDETAG
2375 pointer, start_addr, *start_addr));
2383 pointer, start_addr, *start_addr));
2391 /* Used by the debugger to validate possibly bogus pointers before
2392 * calling MAKE-LISP-OBJ on them.
2394 * FIXME: We would like to make this perfect, because if the debugger
2395 * constructs a reference to a bugs lisp object, and it ends up in a
2396 * location scavenged by the GC all hell breaks loose.
2398 * Whereas possibly_valid_dynamic_space_pointer has to be conservative
2399 * and return true for all valid pointers, this could actually be eager
2400 * and lie about a few pointers without bad results... but that should
2401 * be reflected in the name.
2404 valid_lisp_pointer_p(lispobj *pointer)
2407 if (((start=search_dynamic_space(pointer))!=NULL) ||
2408 ((start=search_static_space(pointer))!=NULL) ||
2409 ((start=search_read_only_space(pointer))!=NULL))
2410 return looks_like_valid_lisp_pointer_p(pointer, start);
2415 /* Is there any possibility that pointer is a valid Lisp object
2416 * reference, and/or something else (e.g. subroutine call return
2417 * address) which should prevent us from moving the referred-to thing?
2418 * This is called from preserve_pointers() */
2420 possibly_valid_dynamic_space_pointer(lispobj *pointer)
2422 lispobj *start_addr;
2424 /* Find the object start address. */
2425 if ((start_addr = search_dynamic_space(pointer)) == NULL) {
2429 return looks_like_valid_lisp_pointer_p(pointer, start_addr);
2432 /* Adjust large bignum and vector objects. This will adjust the
2433 * allocated region if the size has shrunk, and move unboxed objects
2434 * into unboxed pages. The pages are not promoted here, and the
2435 * promoted region is not added to the new_regions; this is really
2436 * only designed to be called from preserve_pointer(). Shouldn't fail
2437 * if this is missed, just may delay the moving of objects to unboxed
2438 * pages, and the freeing of pages. */
2440 maybe_adjust_large_object(lispobj *where)
2442 page_index_t first_page;
2443 page_index_t next_page;
2446 long remaining_bytes;
2448 long old_bytes_used;
2452 /* Check whether it's a vector or bignum object. */
2453 switch (widetag_of(where[0])) {
2454 case SIMPLE_VECTOR_WIDETAG:
2455 boxed = BOXED_PAGE_FLAG;
2457 case BIGNUM_WIDETAG:
2458 case SIMPLE_BASE_STRING_WIDETAG:
2459 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
2460 case SIMPLE_CHARACTER_STRING_WIDETAG:
2462 case SIMPLE_BIT_VECTOR_WIDETAG:
2463 case SIMPLE_ARRAY_NIL_WIDETAG:
2464 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
2465 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
2466 case SIMPLE_ARRAY_UNSIGNED_BYTE_7_WIDETAG:
2467 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
2468 case SIMPLE_ARRAY_UNSIGNED_BYTE_15_WIDETAG:
2469 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
2470 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_29_WIDETAG
2471 case SIMPLE_ARRAY_UNSIGNED_BYTE_29_WIDETAG:
2473 case SIMPLE_ARRAY_UNSIGNED_BYTE_31_WIDETAG:
2474 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
2475 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_60_WIDETAG
2476 case SIMPLE_ARRAY_UNSIGNED_BYTE_60_WIDETAG:
2478 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG
2479 case SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG:
2481 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG
2482 case SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG:
2484 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
2485 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
2487 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
2488 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
2490 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
2491 case SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG:
2493 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
2494 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
2496 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_61_WIDETAG
2497 case SIMPLE_ARRAY_SIGNED_BYTE_61_WIDETAG:
2499 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG
2500 case SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG:
2502 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
2503 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
2504 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
2505 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
2507 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
2508 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
2510 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
2511 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
2513 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
2514 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
2516 boxed = UNBOXED_PAGE_FLAG;
2522 /* Find its current size. */
2523 nwords = (sizetab[widetag_of(where[0])])(where);
2525 first_page = find_page_index((void *)where);
2526 gc_assert(first_page >= 0);
2528 /* Note: Any page write-protection must be removed, else a later
2529 * scavenge_newspace may incorrectly not scavenge these pages.
2530 * This would not be necessary if they are added to the new areas,
2531 * but lets do it for them all (they'll probably be written
2534 gc_assert(page_table[first_page].first_object_offset == 0);
2536 next_page = first_page;
2537 remaining_bytes = nwords*N_WORD_BYTES;
2538 while (remaining_bytes > PAGE_BYTES) {
2539 gc_assert(page_table[next_page].gen == from_space);
2540 gc_assert((page_table[next_page].allocated == BOXED_PAGE_FLAG)
2541 || (page_table[next_page].allocated == UNBOXED_PAGE_FLAG));
2542 gc_assert(page_table[next_page].large_object);
2543 gc_assert(page_table[next_page].first_object_offset ==
2544 -PAGE_BYTES*(next_page-first_page));
2545 gc_assert(page_table[next_page].bytes_used == PAGE_BYTES);
2547 page_table[next_page].allocated = boxed;
2549 /* Shouldn't be write-protected at this stage. Essential that the
2551 gc_assert(!page_table[next_page].write_protected);
2552 remaining_bytes -= PAGE_BYTES;
2556 /* Now only one page remains, but the object may have shrunk so
2557 * there may be more unused pages which will be freed. */
2559 /* Object may have shrunk but shouldn't have grown - check. */
2560 gc_assert(page_table[next_page].bytes_used >= remaining_bytes);
2562 page_table[next_page].allocated = boxed;
2563 gc_assert(page_table[next_page].allocated ==
2564 page_table[first_page].allocated);
2566 /* Adjust the bytes_used. */
2567 old_bytes_used = page_table[next_page].bytes_used;
2568 page_table[next_page].bytes_used = remaining_bytes;
2570 bytes_freed = old_bytes_used - remaining_bytes;
2572 /* Free any remaining pages; needs care. */
2574 while ((old_bytes_used == PAGE_BYTES) &&
2575 (page_table[next_page].gen == from_space) &&
2576 ((page_table[next_page].allocated == UNBOXED_PAGE_FLAG)
2577 || (page_table[next_page].allocated == BOXED_PAGE_FLAG)) &&
2578 page_table[next_page].large_object &&
2579 (page_table[next_page].first_object_offset ==
2580 -(next_page - first_page)*PAGE_BYTES)) {
2581 /* It checks out OK, free the page. We don't need to both zeroing
2582 * pages as this should have been done before shrinking the
2583 * object. These pages shouldn't be write protected as they
2584 * should be zero filled. */
2585 gc_assert(page_table[next_page].write_protected == 0);
2587 old_bytes_used = page_table[next_page].bytes_used;
2588 page_table[next_page].allocated = FREE_PAGE_FLAG;
2589 page_table[next_page].bytes_used = 0;
2590 bytes_freed += old_bytes_used;
2594 if ((bytes_freed > 0) && gencgc_verbose) {
2596 "/maybe_adjust_large_object() freed %d\n",
2600 generations[from_space].bytes_allocated -= bytes_freed;
2601 bytes_allocated -= bytes_freed;
2606 /* Take a possible pointer to a Lisp object and mark its page in the
2607 * page_table so that it will not be relocated during a GC.
2609 * This involves locating the page it points to, then backing up to
2610 * the start of its region, then marking all pages dont_move from there
2611 * up to the first page that's not full or has a different generation
2613 * It is assumed that all the page static flags have been cleared at
2614 * the start of a GC.
2616 * It is also assumed that the current gc_alloc() region has been
2617 * flushed and the tables updated. */
2620 preserve_pointer(void *addr)
2622 page_index_t addr_page_index = find_page_index(addr);
2623 page_index_t first_page;
2625 unsigned int region_allocation;
2627 /* quick check 1: Address is quite likely to have been invalid. */
2628 if ((addr_page_index == -1)
2629 || (page_table[addr_page_index].allocated == FREE_PAGE_FLAG)
2630 || (page_table[addr_page_index].bytes_used == 0)
2631 || (page_table[addr_page_index].gen != from_space)
2632 /* Skip if already marked dont_move. */
2633 || (page_table[addr_page_index].dont_move != 0))
2635 gc_assert(!(page_table[addr_page_index].allocated&OPEN_REGION_PAGE_FLAG));
2636 /* (Now that we know that addr_page_index is in range, it's
2637 * safe to index into page_table[] with it.) */
2638 region_allocation = page_table[addr_page_index].allocated;
2640 /* quick check 2: Check the offset within the page.
2643 if (((unsigned long)addr & (PAGE_BYTES - 1)) > page_table[addr_page_index].bytes_used)
2646 /* Filter out anything which can't be a pointer to a Lisp object
2647 * (or, as a special case which also requires dont_move, a return
2648 * address referring to something in a CodeObject). This is
2649 * expensive but important, since it vastly reduces the
2650 * probability that random garbage will be bogusly interpreted as
2651 * a pointer which prevents a page from moving. */
2652 if (!(possibly_valid_dynamic_space_pointer(addr)))
2655 /* Find the beginning of the region. Note that there may be
2656 * objects in the region preceding the one that we were passed a
2657 * pointer to: if this is the case, we will write-protect all the
2658 * previous objects' pages too. */
2661 /* I think this'd work just as well, but without the assertions.
2662 * -dan 2004.01.01 */
2664 find_page_index(page_address(addr_page_index)+
2665 page_table[addr_page_index].first_object_offset);
2667 first_page = addr_page_index;
2668 while (page_table[first_page].first_object_offset != 0) {
2670 /* Do some checks. */
2671 gc_assert(page_table[first_page].bytes_used == PAGE_BYTES);
2672 gc_assert(page_table[first_page].gen == from_space);
2673 gc_assert(page_table[first_page].allocated == region_allocation);
2677 /* Adjust any large objects before promotion as they won't be
2678 * copied after promotion. */
2679 if (page_table[first_page].large_object) {
2680 maybe_adjust_large_object(page_address(first_page));
2681 /* If a large object has shrunk then addr may now point to a
2682 * free area in which case it's ignored here. Note it gets
2683 * through the valid pointer test above because the tail looks
2685 if ((page_table[addr_page_index].allocated == FREE_PAGE_FLAG)
2686 || (page_table[addr_page_index].bytes_used == 0)
2687 /* Check the offset within the page. */
2688 || (((unsigned long)addr & (PAGE_BYTES - 1))
2689 > page_table[addr_page_index].bytes_used)) {
2691 "weird? ignore ptr 0x%x to freed area of large object\n",
2695 /* It may have moved to unboxed pages. */
2696 region_allocation = page_table[first_page].allocated;
2699 /* Now work forward until the end of this contiguous area is found,
2700 * marking all pages as dont_move. */
2701 for (i = first_page; ;i++) {
2702 gc_assert(page_table[i].allocated == region_allocation);
2704 /* Mark the page static. */
2705 page_table[i].dont_move = 1;
2707 /* Move the page to the new_space. XX I'd rather not do this
2708 * but the GC logic is not quite able to copy with the static
2709 * pages remaining in the from space. This also requires the
2710 * generation bytes_allocated counters be updated. */
2711 page_table[i].gen = new_space;
2712 generations[new_space].bytes_allocated += page_table[i].bytes_used;
2713 generations[from_space].bytes_allocated -= page_table[i].bytes_used;
2715 /* It is essential that the pages are not write protected as
2716 * they may have pointers into the old-space which need
2717 * scavenging. They shouldn't be write protected at this
2719 gc_assert(!page_table[i].write_protected);
2721 /* Check whether this is the last page in this contiguous block.. */
2722 if ((page_table[i].bytes_used < PAGE_BYTES)
2723 /* ..or it is PAGE_BYTES and is the last in the block */
2724 || (page_table[i+1].allocated == FREE_PAGE_FLAG)
2725 || (page_table[i+1].bytes_used == 0) /* next page free */
2726 || (page_table[i+1].gen != from_space) /* diff. gen */
2727 || (page_table[i+1].first_object_offset == 0))
2731 /* Check that the page is now static. */
2732 gc_assert(page_table[addr_page_index].dont_move != 0);
2735 #endif // defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
2738 /* If the given page is not write-protected, then scan it for pointers
2739 * to younger generations or the top temp. generation, if no
2740 * suspicious pointers are found then the page is write-protected.
2742 * Care is taken to check for pointers to the current gc_alloc()
2743 * region if it is a younger generation or the temp. generation. This
2744 * frees the caller from doing a gc_alloc_update_page_tables(). Actually
2745 * the gc_alloc_generation does not need to be checked as this is only
2746 * called from scavenge_generation() when the gc_alloc generation is
2747 * younger, so it just checks if there is a pointer to the current
2750 * We return 1 if the page was write-protected, else 0. */
2752 update_page_write_prot(page_index_t page)
2754 generation_index_t gen = page_table[page].gen;
2757 void **page_addr = (void **)page_address(page);
2758 long num_words = page_table[page].bytes_used / N_WORD_BYTES;
2760 /* Shouldn't be a free page. */
2761 gc_assert(page_table[page].allocated != FREE_PAGE_FLAG);
2762 gc_assert(page_table[page].bytes_used != 0);
2764 /* Skip if it's already write-protected, pinned, or unboxed */
2765 if (page_table[page].write_protected
2766 /* FIXME: What's the reason for not write-protecting pinned pages? */
2767 || page_table[page].dont_move
2768 || (page_table[page].allocated & UNBOXED_PAGE_FLAG))
2771 /* Scan the page for pointers to younger generations or the
2772 * top temp. generation. */
2774 for (j = 0; j < num_words; j++) {
2775 void *ptr = *(page_addr+j);
2776 page_index_t index = find_page_index(ptr);
2778 /* Check that it's in the dynamic space */
2780 if (/* Does it point to a younger or the temp. generation? */
2781 ((page_table[index].allocated != FREE_PAGE_FLAG)
2782 && (page_table[index].bytes_used != 0)
2783 && ((page_table[index].gen < gen)
2784 || (page_table[index].gen == SCRATCH_GENERATION)))
2786 /* Or does it point within a current gc_alloc() region? */
2787 || ((boxed_region.start_addr <= ptr)
2788 && (ptr <= boxed_region.free_pointer))
2789 || ((unboxed_region.start_addr <= ptr)
2790 && (ptr <= unboxed_region.free_pointer))) {
2797 /* Write-protect the page. */
2798 /*FSHOW((stderr, "/write-protecting page %d gen %d\n", page, gen));*/
2800 os_protect((void *)page_addr,
2802 OS_VM_PROT_READ|OS_VM_PROT_EXECUTE);
2804 /* Note the page as protected in the page tables. */
2805 page_table[page].write_protected = 1;
2811 /* Scavenge all generations from FROM to TO, inclusive, except for
2812 * new_space which needs special handling, as new objects may be
2813 * added which are not checked here - use scavenge_newspace generation.
2815 * Write-protected pages should not have any pointers to the
2816 * from_space so do need scavenging; thus write-protected pages are
2817 * not always scavenged. There is some code to check that these pages
2818 * are not written; but to check fully the write-protected pages need
2819 * to be scavenged by disabling the code to skip them.
2821 * Under the current scheme when a generation is GCed the younger
2822 * generations will be empty. So, when a generation is being GCed it
2823 * is only necessary to scavenge the older generations for pointers
2824 * not the younger. So a page that does not have pointers to younger
2825 * generations does not need to be scavenged.
2827 * The write-protection can be used to note pages that don't have
2828 * pointers to younger pages. But pages can be written without having
2829 * pointers to younger generations. After the pages are scavenged here
2830 * they can be scanned for pointers to younger generations and if
2831 * there are none the page can be write-protected.
2833 * One complication is when the newspace is the top temp. generation.
2835 * Enabling SC_GEN_CK scavenges the write-protected pages and checks
2836 * that none were written, which they shouldn't be as they should have
2837 * no pointers to younger generations. This breaks down for weak
2838 * pointers as the objects contain a link to the next and are written
2839 * if a weak pointer is scavenged. Still it's a useful check. */
2841 scavenge_generations(generation_index_t from, generation_index_t to)
2848 /* Clear the write_protected_cleared flags on all pages. */
2849 for (i = 0; i < page_table_pages; i++)
2850 page_table[i].write_protected_cleared = 0;
2853 for (i = 0; i < last_free_page; i++) {
2854 generation_index_t generation = page_table[i].gen;
2855 if ((page_table[i].allocated & BOXED_PAGE_FLAG)
2856 && (page_table[i].bytes_used != 0)
2857 && (generation != new_space)
2858 && (generation >= from)
2859 && (generation <= to)) {
2860 page_index_t last_page,j;
2861 int write_protected=1;
2863 /* This should be the start of a region */
2864 gc_assert(page_table[i].first_object_offset == 0);
2866 /* Now work forward until the end of the region */
2867 for (last_page = i; ; last_page++) {
2869 write_protected && page_table[last_page].write_protected;
2870 if ((page_table[last_page].bytes_used < PAGE_BYTES)
2871 /* Or it is PAGE_BYTES and is the last in the block */
2872 || (!(page_table[last_page+1].allocated & BOXED_PAGE_FLAG))
2873 || (page_table[last_page+1].bytes_used == 0)
2874 || (page_table[last_page+1].gen != generation)
2875 || (page_table[last_page+1].first_object_offset == 0))
2878 if (!write_protected) {
2879 scavenge(page_address(i),
2880 (page_table[last_page].bytes_used +
2881 (last_page-i)*PAGE_BYTES)/N_WORD_BYTES);
2883 /* Now scan the pages and write protect those that
2884 * don't have pointers to younger generations. */
2885 if (enable_page_protection) {
2886 for (j = i; j <= last_page; j++) {
2887 num_wp += update_page_write_prot(j);
2890 if ((gencgc_verbose > 1) && (num_wp != 0)) {
2892 "/write protected %d pages within generation %d\n",
2893 num_wp, generation));
2901 /* Check that none of the write_protected pages in this generation
2902 * have been written to. */
2903 for (i = 0; i < page_table_pages; i++) {
2904 if ((page_table[i].allocation != FREE_PAGE_FLAG)
2905 && (page_table[i].bytes_used != 0)
2906 && (page_table[i].gen == generation)
2907 && (page_table[i].write_protected_cleared != 0)) {
2908 FSHOW((stderr, "/scavenge_generation() %d\n", generation));
2910 "/page bytes_used=%d first_object_offset=%d dont_move=%d\n",
2911 page_table[i].bytes_used,
2912 page_table[i].first_object_offset,
2913 page_table[i].dont_move));
2914 lose("write to protected page %d in scavenge_generation()\n", i);
2921 /* Scavenge a newspace generation. As it is scavenged new objects may
2922 * be allocated to it; these will also need to be scavenged. This
2923 * repeats until there are no more objects unscavenged in the
2924 * newspace generation.
2926 * To help improve the efficiency, areas written are recorded by
2927 * gc_alloc() and only these scavenged. Sometimes a little more will be
2928 * scavenged, but this causes no harm. An easy check is done that the
2929 * scavenged bytes equals the number allocated in the previous
2932 * Write-protected pages are not scanned except if they are marked
2933 * dont_move in which case they may have been promoted and still have
2934 * pointers to the from space.
2936 * Write-protected pages could potentially be written by alloc however
2937 * to avoid having to handle re-scavenging of write-protected pages
2938 * gc_alloc() does not write to write-protected pages.
2940 * New areas of objects allocated are recorded alternatively in the two
2941 * new_areas arrays below. */
2942 static struct new_area new_areas_1[NUM_NEW_AREAS];
2943 static struct new_area new_areas_2[NUM_NEW_AREAS];
2945 /* Do one full scan of the new space generation. This is not enough to
2946 * complete the job as new objects may be added to the generation in
2947 * the process which are not scavenged. */
2949 scavenge_newspace_generation_one_scan(generation_index_t generation)
2954 "/starting one full scan of newspace generation %d\n",
2956 for (i = 0; i < last_free_page; i++) {
2957 /* Note that this skips over open regions when it encounters them. */
2958 if ((page_table[i].allocated & BOXED_PAGE_FLAG)
2959 && (page_table[i].bytes_used != 0)
2960 && (page_table[i].gen == generation)
2961 && ((page_table[i].write_protected == 0)
2962 /* (This may be redundant as write_protected is now
2963 * cleared before promotion.) */
2964 || (page_table[i].dont_move == 1))) {
2965 page_index_t last_page;
2968 /* The scavenge will start at the first_object_offset of page i.
2970 * We need to find the full extent of this contiguous
2971 * block in case objects span pages.
2973 * Now work forward until the end of this contiguous area
2974 * is found. A small area is preferred as there is a
2975 * better chance of its pages being write-protected. */
2976 for (last_page = i; ;last_page++) {
2977 /* If all pages are write-protected and movable,
2978 * then no need to scavenge */
2979 all_wp=all_wp && page_table[last_page].write_protected &&
2980 !page_table[last_page].dont_move;
2982 /* Check whether this is the last page in this
2983 * contiguous block */
2984 if ((page_table[last_page].bytes_used < PAGE_BYTES)
2985 /* Or it is PAGE_BYTES and is the last in the block */
2986 || (!(page_table[last_page+1].allocated & BOXED_PAGE_FLAG))
2987 || (page_table[last_page+1].bytes_used == 0)
2988 || (page_table[last_page+1].gen != generation)
2989 || (page_table[last_page+1].first_object_offset == 0))
2993 /* Do a limited check for write-protected pages. */
2997 size = (page_table[last_page].bytes_used
2998 + (last_page-i)*PAGE_BYTES
2999 - page_table[i].first_object_offset)/N_WORD_BYTES;
3000 new_areas_ignore_page = last_page;
3002 scavenge(page_address(i) +
3003 page_table[i].first_object_offset,
3011 "/done with one full scan of newspace generation %d\n",
3015 /* Do a complete scavenge of the newspace generation. */
3017 scavenge_newspace_generation(generation_index_t generation)
3021 /* the new_areas array currently being written to by gc_alloc() */
3022 struct new_area (*current_new_areas)[] = &new_areas_1;
3023 long current_new_areas_index;
3025 /* the new_areas created by the previous scavenge cycle */
3026 struct new_area (*previous_new_areas)[] = NULL;
3027 long previous_new_areas_index;
3029 /* Flush the current regions updating the tables. */
3030 gc_alloc_update_all_page_tables();
3032 /* Turn on the recording of new areas by gc_alloc(). */
3033 new_areas = current_new_areas;
3034 new_areas_index = 0;
3036 /* Don't need to record new areas that get scavenged anyway during
3037 * scavenge_newspace_generation_one_scan. */
3038 record_new_objects = 1;
3040 /* Start with a full scavenge. */
3041 scavenge_newspace_generation_one_scan(generation);
3043 /* Record all new areas now. */
3044 record_new_objects = 2;
3046 /* Give a chance to weak hash tables to make other objects live.
3047 * FIXME: The algorithm implemented here for weak hash table gcing
3048 * is O(W^2+N) as Bruno Haible warns in
3049 * http://www.haible.de/bruno/papers/cs/weak/WeakDatastructures-writeup.html
3050 * see "Implementation 2". */
3051 scav_weak_hash_tables();
3053 /* Flush the current regions updating the tables. */
3054 gc_alloc_update_all_page_tables();
3056 /* Grab new_areas_index. */
3057 current_new_areas_index = new_areas_index;
3060 "The first scan is finished; current_new_areas_index=%d.\n",
3061 current_new_areas_index));*/
3063 while (current_new_areas_index > 0) {
3064 /* Move the current to the previous new areas */
3065 previous_new_areas = current_new_areas;
3066 previous_new_areas_index = current_new_areas_index;
3068 /* Scavenge all the areas in previous new areas. Any new areas
3069 * allocated are saved in current_new_areas. */
3071 /* Allocate an array for current_new_areas; alternating between
3072 * new_areas_1 and 2 */
3073 if (previous_new_areas == &new_areas_1)
3074 current_new_areas = &new_areas_2;
3076 current_new_areas = &new_areas_1;
3078 /* Set up for gc_alloc(). */
3079 new_areas = current_new_areas;
3080 new_areas_index = 0;
3082 /* Check whether previous_new_areas had overflowed. */
3083 if (previous_new_areas_index >= NUM_NEW_AREAS) {
3085 /* New areas of objects allocated have been lost so need to do a
3086 * full scan to be sure! If this becomes a problem try
3087 * increasing NUM_NEW_AREAS. */
3089 SHOW("new_areas overflow, doing full scavenge");
3091 /* Don't need to record new areas that get scavenged
3092 * anyway during scavenge_newspace_generation_one_scan. */
3093 record_new_objects = 1;
3095 scavenge_newspace_generation_one_scan(generation);
3097 /* Record all new areas now. */
3098 record_new_objects = 2;
3100 scav_weak_hash_tables();
3102 /* Flush the current regions updating the tables. */
3103 gc_alloc_update_all_page_tables();
3107 /* Work through previous_new_areas. */
3108 for (i = 0; i < previous_new_areas_index; i++) {
3109 long page = (*previous_new_areas)[i].page;
3110 long offset = (*previous_new_areas)[i].offset;
3111 long size = (*previous_new_areas)[i].size / N_WORD_BYTES;
3112 gc_assert((*previous_new_areas)[i].size % N_WORD_BYTES == 0);
3113 scavenge(page_address(page)+offset, size);
3116 scav_weak_hash_tables();
3118 /* Flush the current regions updating the tables. */
3119 gc_alloc_update_all_page_tables();
3122 current_new_areas_index = new_areas_index;
3125 "The re-scan has finished; current_new_areas_index=%d.\n",
3126 current_new_areas_index));*/
3129 /* Turn off recording of areas allocated by gc_alloc(). */
3130 record_new_objects = 0;
3133 /* Check that none of the write_protected pages in this generation
3134 * have been written to. */
3135 for (i = 0; i < page_table_pages; i++) {
3136 if ((page_table[i].allocation != FREE_PAGE_FLAG)
3137 && (page_table[i].bytes_used != 0)
3138 && (page_table[i].gen == generation)
3139 && (page_table[i].write_protected_cleared != 0)
3140 && (page_table[i].dont_move == 0)) {
3141 lose("write protected page %d written to in scavenge_newspace_generation\ngeneration=%d dont_move=%d\n",
3142 i, generation, page_table[i].dont_move);
3148 /* Un-write-protect all the pages in from_space. This is done at the
3149 * start of a GC else there may be many page faults while scavenging
3150 * the newspace (I've seen drive the system time to 99%). These pages
3151 * would need to be unprotected anyway before unmapping in
3152 * free_oldspace; not sure what effect this has on paging.. */
3154 unprotect_oldspace(void)
3158 for (i = 0; i < last_free_page; i++) {
3159 if ((page_table[i].allocated != FREE_PAGE_FLAG)
3160 && (page_table[i].bytes_used != 0)
3161 && (page_table[i].gen == from_space)) {
3164 page_start = (void *)page_address(i);
3166 /* Remove any write-protection. We should be able to rely
3167 * on the write-protect flag to avoid redundant calls. */
3168 if (page_table[i].write_protected) {
3169 os_protect(page_start, PAGE_BYTES, OS_VM_PROT_ALL);
3170 page_table[i].write_protected = 0;
3176 /* Work through all the pages and free any in from_space. This
3177 * assumes that all objects have been copied or promoted to an older
3178 * generation. Bytes_allocated and the generation bytes_allocated
3179 * counter are updated. The number of bytes freed is returned. */
3183 long bytes_freed = 0;
3184 page_index_t first_page, last_page;
3189 /* Find a first page for the next region of pages. */
3190 while ((first_page < last_free_page)
3191 && ((page_table[first_page].allocated == FREE_PAGE_FLAG)
3192 || (page_table[first_page].bytes_used == 0)
3193 || (page_table[first_page].gen != from_space)))
3196 if (first_page >= last_free_page)
3199 /* Find the last page of this region. */
3200 last_page = first_page;
3203 /* Free the page. */
3204 bytes_freed += page_table[last_page].bytes_used;
3205 generations[page_table[last_page].gen].bytes_allocated -=
3206 page_table[last_page].bytes_used;
3207 page_table[last_page].allocated = FREE_PAGE_FLAG;
3208 page_table[last_page].bytes_used = 0;
3210 /* Remove any write-protection. We should be able to rely
3211 * on the write-protect flag to avoid redundant calls. */
3213 void *page_start = (void *)page_address(last_page);
3215 if (page_table[last_page].write_protected) {
3216 os_protect(page_start, PAGE_BYTES, OS_VM_PROT_ALL);
3217 page_table[last_page].write_protected = 0;
3222 while ((last_page < last_free_page)
3223 && (page_table[last_page].allocated != FREE_PAGE_FLAG)
3224 && (page_table[last_page].bytes_used != 0)
3225 && (page_table[last_page].gen == from_space));
3227 #ifdef READ_PROTECT_FREE_PAGES
3228 os_protect(page_address(first_page),
3229 PAGE_BYTES*(last_page-first_page),
3232 first_page = last_page;
3233 } while (first_page < last_free_page);
3235 bytes_allocated -= bytes_freed;
3240 /* Print some information about a pointer at the given address. */
3242 print_ptr(lispobj *addr)
3244 /* If addr is in the dynamic space then out the page information. */
3245 page_index_t pi1 = find_page_index((void*)addr);
3248 fprintf(stderr," %x: page %d alloc %d gen %d bytes_used %d offset %d dont_move %d\n",
3249 (unsigned long) addr,
3251 page_table[pi1].allocated,
3252 page_table[pi1].gen,
3253 page_table[pi1].bytes_used,
3254 page_table[pi1].first_object_offset,
3255 page_table[pi1].dont_move);
3256 fprintf(stderr," %x %x %x %x (%x) %x %x %x %x\n",
3270 verify_space(lispobj *start, size_t words)
3272 int is_in_dynamic_space = (find_page_index((void*)start) != -1);
3273 int is_in_readonly_space =
3274 (READ_ONLY_SPACE_START <= (unsigned long)start &&
3275 (unsigned long)start < SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0));
3279 lispobj thing = *(lispobj*)start;
3281 if (is_lisp_pointer(thing)) {
3282 page_index_t page_index = find_page_index((void*)thing);
3283 long to_readonly_space =
3284 (READ_ONLY_SPACE_START <= thing &&
3285 thing < SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0));
3286 long to_static_space =
3287 (STATIC_SPACE_START <= thing &&
3288 thing < SymbolValue(STATIC_SPACE_FREE_POINTER,0));
3290 /* Does it point to the dynamic space? */
3291 if (page_index != -1) {
3292 /* If it's within the dynamic space it should point to a used
3293 * page. XX Could check the offset too. */
3294 if ((page_table[page_index].allocated != FREE_PAGE_FLAG)
3295 && (page_table[page_index].bytes_used == 0))
3296 lose ("Ptr %x @ %x sees free page.\n", thing, start);
3297 /* Check that it doesn't point to a forwarding pointer! */
3298 if (*((lispobj *)native_pointer(thing)) == 0x01) {
3299 lose("Ptr %x @ %x sees forwarding ptr.\n", thing, start);
3301 /* Check that its not in the RO space as it would then be a
3302 * pointer from the RO to the dynamic space. */
3303 if (is_in_readonly_space) {
3304 lose("ptr to dynamic space %x from RO space %x\n",
3307 /* Does it point to a plausible object? This check slows
3308 * it down a lot (so it's commented out).
3310 * "a lot" is serious: it ate 50 minutes cpu time on
3311 * my duron 950 before I came back from lunch and
3314 * FIXME: Add a variable to enable this
3317 if (!possibly_valid_dynamic_space_pointer((lispobj *)thing)) {
3318 lose("ptr %x to invalid object %x\n", thing, start);
3322 /* Verify that it points to another valid space. */
3323 if (!to_readonly_space && !to_static_space) {
3324 lose("Ptr %x @ %x sees junk.\n", thing, start);
3328 if (!(fixnump(thing))) {
3330 switch(widetag_of(*start)) {
3333 case SIMPLE_VECTOR_WIDETAG:
3335 case COMPLEX_WIDETAG:
3336 case SIMPLE_ARRAY_WIDETAG:
3337 case COMPLEX_BASE_STRING_WIDETAG:
3338 #ifdef COMPLEX_CHARACTER_STRING_WIDETAG
3339 case COMPLEX_CHARACTER_STRING_WIDETAG:
3341 case COMPLEX_VECTOR_NIL_WIDETAG:
3342 case COMPLEX_BIT_VECTOR_WIDETAG:
3343 case COMPLEX_VECTOR_WIDETAG:
3344 case COMPLEX_ARRAY_WIDETAG:
3345 case CLOSURE_HEADER_WIDETAG:
3346 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
3347 case VALUE_CELL_HEADER_WIDETAG:
3348 case SYMBOL_HEADER_WIDETAG:
3349 case CHARACTER_WIDETAG:
3350 #if N_WORD_BITS == 64
3351 case SINGLE_FLOAT_WIDETAG:
3353 case UNBOUND_MARKER_WIDETAG:
3358 case INSTANCE_HEADER_WIDETAG:
3361 long ntotal = HeaderValue(thing);
3362 lispobj layout = ((struct instance *)start)->slots[0];
3367 nuntagged = ((struct layout *)native_pointer(layout))->n_untagged_slots;
3368 verify_space(start + 1, ntotal - fixnum_value(nuntagged));
3372 case CODE_HEADER_WIDETAG:
3374 lispobj object = *start;
3376 long nheader_words, ncode_words, nwords;
3378 struct simple_fun *fheaderp;
3380 code = (struct code *) start;
3382 /* Check that it's not in the dynamic space.
3383 * FIXME: Isn't is supposed to be OK for code
3384 * objects to be in the dynamic space these days? */
3385 if (is_in_dynamic_space
3386 /* It's ok if it's byte compiled code. The trace
3387 * table offset will be a fixnum if it's x86
3388 * compiled code - check.
3390 * FIXME: #^#@@! lack of abstraction here..
3391 * This line can probably go away now that
3392 * there's no byte compiler, but I've got
3393 * too much to worry about right now to try
3394 * to make sure. -- WHN 2001-10-06 */
3395 && fixnump(code->trace_table_offset)
3396 /* Only when enabled */
3397 && verify_dynamic_code_check) {
3399 "/code object at %x in the dynamic space\n",
3403 ncode_words = fixnum_value(code->code_size);
3404 nheader_words = HeaderValue(object);
3405 nwords = ncode_words + nheader_words;
3406 nwords = CEILING(nwords, 2);
3407 /* Scavenge the boxed section of the code data block */
3408 verify_space(start + 1, nheader_words - 1);
3410 /* Scavenge the boxed section of each function
3411 * object in the code data block. */
3412 fheaderl = code->entry_points;
3413 while (fheaderl != NIL) {
3415 (struct simple_fun *) native_pointer(fheaderl);
3416 gc_assert(widetag_of(fheaderp->header) == SIMPLE_FUN_HEADER_WIDETAG);
3417 verify_space(&fheaderp->name, 1);
3418 verify_space(&fheaderp->arglist, 1);
3419 verify_space(&fheaderp->type, 1);
3420 fheaderl = fheaderp->next;
3426 /* unboxed objects */
3427 case BIGNUM_WIDETAG:
3428 #if N_WORD_BITS != 64
3429 case SINGLE_FLOAT_WIDETAG:
3431 case DOUBLE_FLOAT_WIDETAG:
3432 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
3433 case LONG_FLOAT_WIDETAG:
3435 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
3436 case COMPLEX_SINGLE_FLOAT_WIDETAG:
3438 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
3439 case COMPLEX_DOUBLE_FLOAT_WIDETAG:
3441 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
3442 case COMPLEX_LONG_FLOAT_WIDETAG:
3444 case SIMPLE_BASE_STRING_WIDETAG:
3445 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
3446 case SIMPLE_CHARACTER_STRING_WIDETAG:
3448 case SIMPLE_BIT_VECTOR_WIDETAG:
3449 case SIMPLE_ARRAY_NIL_WIDETAG:
3450 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
3451 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
3452 case SIMPLE_ARRAY_UNSIGNED_BYTE_7_WIDETAG:
3453 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
3454 case SIMPLE_ARRAY_UNSIGNED_BYTE_15_WIDETAG:
3455 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
3456 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_29_WIDETAG
3457 case SIMPLE_ARRAY_UNSIGNED_BYTE_29_WIDETAG:
3459 case SIMPLE_ARRAY_UNSIGNED_BYTE_31_WIDETAG:
3460 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
3461 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_60_WIDETAG
3462 case SIMPLE_ARRAY_UNSIGNED_BYTE_60_WIDETAG:
3464 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG
3465 case SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG:
3467 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG
3468 case SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG:
3470 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
3471 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
3473 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
3474 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
3476 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
3477 case SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG:
3479 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
3480 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
3482 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_61_WIDETAG
3483 case SIMPLE_ARRAY_SIGNED_BYTE_61_WIDETAG:
3485 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG
3486 case SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG:
3488 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
3489 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
3490 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
3491 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
3493 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
3494 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
3496 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
3497 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
3499 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
3500 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
3503 case WEAK_POINTER_WIDETAG:
3504 #ifdef LUTEX_WIDETAG
3507 #ifdef NO_TLS_VALUE_MARKER_WIDETAG
3508 case NO_TLS_VALUE_MARKER_WIDETAG:
3510 count = (sizetab[widetag_of(*start)])(start);
3514 lose("Unhandled widetag 0x%x at 0x%x\n", widetag_of(*start), start);
3526 /* FIXME: It would be nice to make names consistent so that
3527 * foo_size meant size *in* *bytes* instead of size in some
3528 * arbitrary units. (Yes, this caused a bug, how did you guess?:-)
3529 * Some counts of lispobjs are called foo_count; it might be good
3530 * to grep for all foo_size and rename the appropriate ones to
3532 long read_only_space_size =
3533 (lispobj*)SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0)
3534 - (lispobj*)READ_ONLY_SPACE_START;
3535 long static_space_size =
3536 (lispobj*)SymbolValue(STATIC_SPACE_FREE_POINTER,0)
3537 - (lispobj*)STATIC_SPACE_START;
3539 for_each_thread(th) {
3540 long binding_stack_size =
3541 (lispobj*)get_binding_stack_pointer(th)
3542 - (lispobj*)th->binding_stack_start;
3543 verify_space(th->binding_stack_start, binding_stack_size);
3545 verify_space((lispobj*)READ_ONLY_SPACE_START, read_only_space_size);
3546 verify_space((lispobj*)STATIC_SPACE_START , static_space_size);
3550 verify_generation(generation_index_t generation)
3554 for (i = 0; i < last_free_page; i++) {
3555 if ((page_table[i].allocated != FREE_PAGE_FLAG)
3556 && (page_table[i].bytes_used != 0)
3557 && (page_table[i].gen == generation)) {
3558 page_index_t last_page;
3559 int region_allocation = page_table[i].allocated;
3561 /* This should be the start of a contiguous block */
3562 gc_assert(page_table[i].first_object_offset == 0);
3564 /* Need to find the full extent of this contiguous block in case
3565 objects span pages. */
3567 /* Now work forward until the end of this contiguous area is
3569 for (last_page = i; ;last_page++)
3570 /* Check whether this is the last page in this contiguous
3572 if ((page_table[last_page].bytes_used < PAGE_BYTES)
3573 /* Or it is PAGE_BYTES and is the last in the block */
3574 || (page_table[last_page+1].allocated != region_allocation)
3575 || (page_table[last_page+1].bytes_used == 0)
3576 || (page_table[last_page+1].gen != generation)
3577 || (page_table[last_page+1].first_object_offset == 0))
3580 verify_space(page_address(i), (page_table[last_page].bytes_used
3581 + (last_page-i)*PAGE_BYTES)/N_WORD_BYTES);
3587 /* Check that all the free space is zero filled. */
3589 verify_zero_fill(void)
3593 for (page = 0; page < last_free_page; page++) {
3594 if (page_table[page].allocated == FREE_PAGE_FLAG) {
3595 /* The whole page should be zero filled. */
3596 long *start_addr = (long *)page_address(page);
3599 for (i = 0; i < size; i++) {
3600 if (start_addr[i] != 0) {
3601 lose("free page not zero at %x\n", start_addr + i);
3605 long free_bytes = PAGE_BYTES - page_table[page].bytes_used;
3606 if (free_bytes > 0) {
3607 long *start_addr = (long *)((unsigned long)page_address(page)
3608 + page_table[page].bytes_used);
3609 long size = free_bytes / N_WORD_BYTES;
3611 for (i = 0; i < size; i++) {
3612 if (start_addr[i] != 0) {
3613 lose("free region not zero at %x\n", start_addr + i);
3621 /* External entry point for verify_zero_fill */
3623 gencgc_verify_zero_fill(void)
3625 /* Flush the alloc regions updating the tables. */
3626 gc_alloc_update_all_page_tables();
3627 SHOW("verifying zero fill");
3632 verify_dynamic_space(void)
3634 generation_index_t i;
3636 for (i = 0; i <= HIGHEST_NORMAL_GENERATION; i++)
3637 verify_generation(i);
3639 if (gencgc_enable_verify_zero_fill)
3643 /* Write-protect all the dynamic boxed pages in the given generation. */
3645 write_protect_generation_pages(generation_index_t generation)
3649 gc_assert(generation < SCRATCH_GENERATION);
3651 for (start = 0; start < last_free_page; start++) {
3652 if ((page_table[start].allocated == BOXED_PAGE_FLAG)
3653 && (page_table[start].bytes_used != 0)
3654 && !page_table[start].dont_move
3655 && (page_table[start].gen == generation)) {
3659 /* Note the page as protected in the page tables. */
3660 page_table[start].write_protected = 1;
3662 for (last = start + 1; last < last_free_page; last++) {
3663 if ((page_table[last].allocated != BOXED_PAGE_FLAG)
3664 || (page_table[last].bytes_used == 0)
3665 || page_table[last].dont_move
3666 || (page_table[last].gen != generation))
3668 page_table[last].write_protected = 1;
3671 page_start = (void *)page_address(start);
3673 os_protect(page_start,
3674 PAGE_BYTES * (last - start),
3675 OS_VM_PROT_READ | OS_VM_PROT_EXECUTE);
3681 if (gencgc_verbose > 1) {
3683 "/write protected %d of %d pages in generation %d\n",
3684 count_write_protect_generation_pages(generation),
3685 count_generation_pages(generation),
3690 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
3693 scavenge_control_stack()
3695 unsigned long control_stack_size;
3697 /* This is going to be a big problem when we try to port threads
3699 struct thread *th = arch_os_get_current_thread();
3700 lispobj *control_stack =
3701 (lispobj *)(th->control_stack_start);
3703 control_stack_size = current_control_stack_pointer - control_stack;
3704 scavenge(control_stack, control_stack_size);
3707 /* Scavenging Interrupt Contexts */
3709 static int boxed_registers[] = BOXED_REGISTERS;
3712 scavenge_interrupt_context(os_context_t * context)
3718 unsigned long lip_offset;
3719 int lip_register_pair;
3721 unsigned long pc_code_offset;
3723 #ifdef ARCH_HAS_LINK_REGISTER
3724 unsigned long lr_code_offset;
3726 #ifdef ARCH_HAS_NPC_REGISTER
3727 unsigned long npc_code_offset;
3731 /* Find the LIP's register pair and calculate it's offset */
3732 /* before we scavenge the context. */
3735 * I (RLT) think this is trying to find the boxed register that is
3736 * closest to the LIP address, without going past it. Usually, it's
3737 * reg_CODE or reg_LRA. But sometimes, nothing can be found.
3739 lip = *os_context_register_addr(context, reg_LIP);
3740 lip_offset = 0x7FFFFFFF;
3741 lip_register_pair = -1;
3742 for (i = 0; i < (sizeof(boxed_registers) / sizeof(int)); i++) {
3747 index = boxed_registers[i];
3748 reg = *os_context_register_addr(context, index);
3749 if ((reg & ~((1L<<N_LOWTAG_BITS)-1)) <= lip) {
3751 if (offset < lip_offset) {
3752 lip_offset = offset;
3753 lip_register_pair = index;
3757 #endif /* reg_LIP */
3759 /* Compute the PC's offset from the start of the CODE */
3761 pc_code_offset = *os_context_pc_addr(context) - *os_context_register_addr(context, reg_CODE);
3762 #ifdef ARCH_HAS_NPC_REGISTER
3763 npc_code_offset = *os_context_npc_addr(context) - *os_context_register_addr(context, reg_CODE);
3764 #endif /* ARCH_HAS_NPC_REGISTER */
3766 #ifdef ARCH_HAS_LINK_REGISTER
3768 *os_context_lr_addr(context) -
3769 *os_context_register_addr(context, reg_CODE);
3772 /* Scanvenge all boxed registers in the context. */
3773 for (i = 0; i < (sizeof(boxed_registers) / sizeof(int)); i++) {
3777 index = boxed_registers[i];
3778 foo = *os_context_register_addr(context, index);
3780 *os_context_register_addr(context, index) = foo;
3782 scavenge((lispobj*) &(*os_context_register_addr(context, index)), 1);
3789 * But what happens if lip_register_pair is -1? *os_context_register_addr on Solaris
3790 * (see solaris_register_address in solaris-os.c) will return
3791 * &context->uc_mcontext.gregs[2]. But gregs[2] is REG_nPC. Is
3792 * that what we really want? My guess is that that is not what we
3793 * want, so if lip_register_pair is -1, we don't touch reg_LIP at
3794 * all. But maybe it doesn't really matter if LIP is trashed?
3796 if (lip_register_pair >= 0) {
3797 *os_context_register_addr(context, reg_LIP) =
3798 *os_context_register_addr(context, lip_register_pair) + lip_offset;
3800 #endif /* reg_LIP */
3802 /* Fix the PC if it was in from space */
3803 if (from_space_p(*os_context_pc_addr(context)))
3804 *os_context_pc_addr(context) = *os_context_register_addr(context, reg_CODE) + pc_code_offset;
3806 #ifdef ARCH_HAS_LINK_REGISTER
3807 /* Fix the LR ditto; important if we're being called from
3808 * an assembly routine that expects to return using blr, otherwise
3810 if (from_space_p(*os_context_lr_addr(context)))
3811 *os_context_lr_addr(context) =
3812 *os_context_register_addr(context, reg_CODE) + lr_code_offset;
3815 #ifdef ARCH_HAS_NPC_REGISTER
3816 if (from_space_p(*os_context_npc_addr(context)))
3817 *os_context_npc_addr(context) = *os_context_register_addr(context, reg_CODE) + npc_code_offset;
3818 #endif /* ARCH_HAS_NPC_REGISTER */
3822 scavenge_interrupt_contexts(void)
3825 os_context_t *context;
3827 struct thread *th=arch_os_get_current_thread();
3829 index = fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,0));
3831 #if defined(DEBUG_PRINT_CONTEXT_INDEX)
3832 printf("Number of active contexts: %d\n", index);
3835 for (i = 0; i < index; i++) {
3836 context = th->interrupt_contexts[i];
3837 scavenge_interrupt_context(context);
3843 #if defined(LISP_FEATURE_SB_THREAD)
3845 preserve_context_registers (os_context_t *c)
3848 /* On Darwin the signal context isn't a contiguous block of memory,
3849 * so just preserve_pointering its contents won't be sufficient.
3851 #if defined(LISP_FEATURE_DARWIN)
3852 #if defined LISP_FEATURE_X86
3853 preserve_pointer((void*)*os_context_register_addr(c,reg_EAX));
3854 preserve_pointer((void*)*os_context_register_addr(c,reg_ECX));
3855 preserve_pointer((void*)*os_context_register_addr(c,reg_EDX));
3856 preserve_pointer((void*)*os_context_register_addr(c,reg_EBX));
3857 preserve_pointer((void*)*os_context_register_addr(c,reg_ESI));
3858 preserve_pointer((void*)*os_context_register_addr(c,reg_EDI));
3859 preserve_pointer((void*)*os_context_pc_addr(c));
3860 #elif defined LISP_FEATURE_X86_64
3861 preserve_pointer((void*)*os_context_register_addr(c,reg_RAX));
3862 preserve_pointer((void*)*os_context_register_addr(c,reg_RCX));
3863 preserve_pointer((void*)*os_context_register_addr(c,reg_RDX));
3864 preserve_pointer((void*)*os_context_register_addr(c,reg_RBX));
3865 preserve_pointer((void*)*os_context_register_addr(c,reg_RSI));
3866 preserve_pointer((void*)*os_context_register_addr(c,reg_RDI));
3867 preserve_pointer((void*)*os_context_register_addr(c,reg_R8));
3868 preserve_pointer((void*)*os_context_register_addr(c,reg_R9));
3869 preserve_pointer((void*)*os_context_register_addr(c,reg_R10));
3870 preserve_pointer((void*)*os_context_register_addr(c,reg_R11));
3871 preserve_pointer((void*)*os_context_register_addr(c,reg_R12));
3872 preserve_pointer((void*)*os_context_register_addr(c,reg_R13));
3873 preserve_pointer((void*)*os_context_register_addr(c,reg_R14));
3874 preserve_pointer((void*)*os_context_register_addr(c,reg_R15));
3875 preserve_pointer((void*)*os_context_pc_addr(c));
3877 #error "preserve_context_registers needs to be tweaked for non-x86 Darwin"
3880 for(ptr = ((void **)(c+1))-1; ptr>=(void **)c; ptr--) {
3881 preserve_pointer(*ptr);
3886 /* Garbage collect a generation. If raise is 0 then the remains of the
3887 * generation are not raised to the next generation. */
3889 garbage_collect_generation(generation_index_t generation, int raise)
3891 unsigned long bytes_freed;
3893 unsigned long static_space_size;
3894 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
3897 gc_assert(generation <= HIGHEST_NORMAL_GENERATION);
3899 /* The oldest generation can't be raised. */
3900 gc_assert((generation != HIGHEST_NORMAL_GENERATION) || (raise == 0));
3902 /* Check if weak hash tables were processed in the previous GC. */
3903 gc_assert(weak_hash_tables == NULL);
3905 /* Initialize the weak pointer list. */
3906 weak_pointers = NULL;
3908 #ifdef LUTEX_WIDETAG
3909 unmark_lutexes(generation);
3912 /* When a generation is not being raised it is transported to a
3913 * temporary generation (NUM_GENERATIONS), and lowered when
3914 * done. Set up this new generation. There should be no pages
3915 * allocated to it yet. */
3917 gc_assert(generations[SCRATCH_GENERATION].bytes_allocated == 0);
3920 /* Set the global src and dest. generations */
3921 from_space = generation;
3923 new_space = generation+1;
3925 new_space = SCRATCH_GENERATION;
3927 /* Change to a new space for allocation, resetting the alloc_start_page */
3928 gc_alloc_generation = new_space;
3929 generations[new_space].alloc_start_page = 0;
3930 generations[new_space].alloc_unboxed_start_page = 0;
3931 generations[new_space].alloc_large_start_page = 0;
3932 generations[new_space].alloc_large_unboxed_start_page = 0;
3934 /* Before any pointers are preserved, the dont_move flags on the
3935 * pages need to be cleared. */
3936 for (i = 0; i < last_free_page; i++)
3937 if(page_table[i].gen==from_space)
3938 page_table[i].dont_move = 0;
3940 /* Un-write-protect the old-space pages. This is essential for the
3941 * promoted pages as they may contain pointers into the old-space
3942 * which need to be scavenged. It also helps avoid unnecessary page
3943 * faults as forwarding pointers are written into them. They need to
3944 * be un-protected anyway before unmapping later. */
3945 unprotect_oldspace();
3947 /* Scavenge the stacks' conservative roots. */
3949 /* there are potentially two stacks for each thread: the main
3950 * stack, which may contain Lisp pointers, and the alternate stack.
3951 * We don't ever run Lisp code on the altstack, but it may
3952 * host a sigcontext with lisp objects in it */
3954 /* what we need to do: (1) find the stack pointer for the main
3955 * stack; scavenge it (2) find the interrupt context on the
3956 * alternate stack that might contain lisp values, and scavenge
3959 /* we assume that none of the preceding applies to the thread that
3960 * initiates GC. If you ever call GC from inside an altstack
3961 * handler, you will lose. */
3963 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
3964 /* And if we're saving a core, there's no point in being conservative. */
3965 if (conservative_stack) {
3966 for_each_thread(th) {
3968 void **esp=(void **)-1;
3969 #ifdef LISP_FEATURE_SB_THREAD
3971 if(th==arch_os_get_current_thread()) {
3972 /* Somebody is going to burn in hell for this, but casting
3973 * it in two steps shuts gcc up about strict aliasing. */
3974 esp = (void **)((void *)&raise);
3977 free=fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,th));
3978 for(i=free-1;i>=0;i--) {
3979 os_context_t *c=th->interrupt_contexts[i];
3980 esp1 = (void **) *os_context_register_addr(c,reg_SP);
3981 if (esp1>=(void **)th->control_stack_start &&
3982 esp1<(void **)th->control_stack_end) {
3983 if(esp1<esp) esp=esp1;
3984 preserve_context_registers(c);
3989 esp = (void **)((void *)&raise);
3991 for (ptr = ((void **)th->control_stack_end)-1; ptr >= esp; ptr--) {
3992 preserve_pointer(*ptr);
3999 if (gencgc_verbose > 1) {
4000 long num_dont_move_pages = count_dont_move_pages();
4002 "/non-movable pages due to conservative pointers = %d (%d bytes)\n",
4003 num_dont_move_pages,
4004 num_dont_move_pages * PAGE_BYTES);
4008 /* Scavenge all the rest of the roots. */
4010 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
4012 * If not x86, we need to scavenge the interrupt context(s) and the
4015 scavenge_interrupt_contexts();
4016 scavenge_control_stack();
4019 /* Scavenge the Lisp functions of the interrupt handlers, taking
4020 * care to avoid SIG_DFL and SIG_IGN. */
4021 for (i = 0; i < NSIG; i++) {
4022 union interrupt_handler handler = interrupt_handlers[i];
4023 if (!ARE_SAME_HANDLER(handler.c, SIG_IGN) &&
4024 !ARE_SAME_HANDLER(handler.c, SIG_DFL)) {
4025 scavenge((lispobj *)(interrupt_handlers + i), 1);
4028 /* Scavenge the binding stacks. */
4031 for_each_thread(th) {
4032 long len= (lispobj *)get_binding_stack_pointer(th) -
4033 th->binding_stack_start;
4034 scavenge((lispobj *) th->binding_stack_start,len);
4035 #ifdef LISP_FEATURE_SB_THREAD
4036 /* do the tls as well */
4037 len=fixnum_value(SymbolValue(FREE_TLS_INDEX,0)) -
4038 (sizeof (struct thread))/(sizeof (lispobj));
4039 scavenge((lispobj *) (th+1),len);
4044 /* The original CMU CL code had scavenge-read-only-space code
4045 * controlled by the Lisp-level variable
4046 * *SCAVENGE-READ-ONLY-SPACE*. It was disabled by default, and it
4047 * wasn't documented under what circumstances it was useful or
4048 * safe to turn it on, so it's been turned off in SBCL. If you
4049 * want/need this functionality, and can test and document it,
4050 * please submit a patch. */
4052 if (SymbolValue(SCAVENGE_READ_ONLY_SPACE) != NIL) {
4053 unsigned long read_only_space_size =
4054 (lispobj*)SymbolValue(READ_ONLY_SPACE_FREE_POINTER) -
4055 (lispobj*)READ_ONLY_SPACE_START;
4057 "/scavenge read only space: %d bytes\n",
4058 read_only_space_size * sizeof(lispobj)));
4059 scavenge( (lispobj *) READ_ONLY_SPACE_START, read_only_space_size);
4063 /* Scavenge static space. */
4065 (lispobj *)SymbolValue(STATIC_SPACE_FREE_POINTER,0) -
4066 (lispobj *)STATIC_SPACE_START;
4067 if (gencgc_verbose > 1) {
4069 "/scavenge static space: %d bytes\n",
4070 static_space_size * sizeof(lispobj)));
4072 scavenge( (lispobj *) STATIC_SPACE_START, static_space_size);
4074 /* All generations but the generation being GCed need to be
4075 * scavenged. The new_space generation needs special handling as
4076 * objects may be moved in - it is handled separately below. */
4077 scavenge_generations(generation+1, PSEUDO_STATIC_GENERATION);
4079 /* Finally scavenge the new_space generation. Keep going until no
4080 * more objects are moved into the new generation */
4081 scavenge_newspace_generation(new_space);
4083 /* FIXME: I tried reenabling this check when debugging unrelated
4084 * GC weirdness ca. sbcl-0.6.12.45, and it failed immediately.
4085 * Since the current GC code seems to work well, I'm guessing that
4086 * this debugging code is just stale, but I haven't tried to
4087 * figure it out. It should be figured out and then either made to
4088 * work or just deleted. */
4089 #define RESCAN_CHECK 0
4091 /* As a check re-scavenge the newspace once; no new objects should
4094 long old_bytes_allocated = bytes_allocated;
4095 long bytes_allocated;
4097 /* Start with a full scavenge. */
4098 scavenge_newspace_generation_one_scan(new_space);
4100 /* Flush the current regions, updating the tables. */
4101 gc_alloc_update_all_page_tables();
4103 bytes_allocated = bytes_allocated - old_bytes_allocated;
4105 if (bytes_allocated != 0) {
4106 lose("Rescan of new_space allocated %d more bytes.\n",
4112 scan_weak_hash_tables();
4113 scan_weak_pointers();
4115 /* Flush the current regions, updating the tables. */
4116 gc_alloc_update_all_page_tables();
4118 /* Free the pages in oldspace, but not those marked dont_move. */
4119 bytes_freed = free_oldspace();
4121 /* If the GC is not raising the age then lower the generation back
4122 * to its normal generation number */
4124 for (i = 0; i < last_free_page; i++)
4125 if ((page_table[i].bytes_used != 0)
4126 && (page_table[i].gen == SCRATCH_GENERATION))
4127 page_table[i].gen = generation;
4128 gc_assert(generations[generation].bytes_allocated == 0);
4129 generations[generation].bytes_allocated =
4130 generations[SCRATCH_GENERATION].bytes_allocated;
4131 generations[SCRATCH_GENERATION].bytes_allocated = 0;
4134 /* Reset the alloc_start_page for generation. */
4135 generations[generation].alloc_start_page = 0;
4136 generations[generation].alloc_unboxed_start_page = 0;
4137 generations[generation].alloc_large_start_page = 0;
4138 generations[generation].alloc_large_unboxed_start_page = 0;
4140 if (generation >= verify_gens) {
4144 verify_dynamic_space();
4147 /* Set the new gc trigger for the GCed generation. */
4148 generations[generation].gc_trigger =
4149 generations[generation].bytes_allocated
4150 + generations[generation].bytes_consed_between_gc;
4153 generations[generation].num_gc = 0;
4155 ++generations[generation].num_gc;
4157 #ifdef LUTEX_WIDETAG
4158 reap_lutexes(generation);
4160 move_lutexes(generation, generation+1);
4164 /* Update last_free_page, then SymbolValue(ALLOCATION_POINTER). */
4166 update_dynamic_space_free_pointer(void)
4168 page_index_t last_page = -1, i;
4170 for (i = 0; i < last_free_page; i++)
4171 if ((page_table[i].allocated != FREE_PAGE_FLAG)
4172 && (page_table[i].bytes_used != 0))
4175 last_free_page = last_page+1;
4177 set_alloc_pointer((lispobj)(((char *)heap_base) + last_free_page*PAGE_BYTES));
4178 return 0; /* dummy value: return something ... */
4182 remap_free_pages (page_index_t from, page_index_t to)
4184 page_index_t first_page, last_page;
4186 for (first_page = from; first_page <= to; first_page++) {
4187 if (page_table[first_page].allocated != FREE_PAGE_FLAG ||
4188 page_table[first_page].need_to_zero == 0) {
4192 last_page = first_page + 1;
4193 while (page_table[last_page].allocated == FREE_PAGE_FLAG &&
4195 page_table[last_page].need_to_zero == 1) {
4199 /* There's a mysterious Solaris/x86 problem with using mmap
4200 * tricks for memory zeroing. See sbcl-devel thread
4201 * "Re: patch: standalone executable redux".
4203 #if defined(LISP_FEATURE_SUNOS)
4204 zero_pages(first_page, last_page-1);
4206 zero_pages_with_mmap(first_page, last_page-1);
4209 first_page = last_page;
4213 generation_index_t small_generation_limit = 1;
4215 /* GC all generations newer than last_gen, raising the objects in each
4216 * to the next older generation - we finish when all generations below
4217 * last_gen are empty. Then if last_gen is due for a GC, or if
4218 * last_gen==NUM_GENERATIONS (the scratch generation? eh?) we GC that
4219 * too. The valid range for last_gen is: 0,1,...,NUM_GENERATIONS.
4221 * We stop collecting at gencgc_oldest_gen_to_gc, even if this is less than
4222 * last_gen (oh, and note that by default it is NUM_GENERATIONS-1) */
4224 collect_garbage(generation_index_t last_gen)
4226 generation_index_t gen = 0, i;
4229 /* The largest value of last_free_page seen since the time
4230 * remap_free_pages was called. */
4231 static page_index_t high_water_mark = 0;
4233 FSHOW((stderr, "/entering collect_garbage(%d)\n", last_gen));
4237 if (last_gen > HIGHEST_NORMAL_GENERATION+1) {
4239 "/collect_garbage: last_gen = %d, doing a level 0 GC\n",
4244 /* Flush the alloc regions updating the tables. */
4245 gc_alloc_update_all_page_tables();
4247 /* Verify the new objects created by Lisp code. */
4248 if (pre_verify_gen_0) {
4249 FSHOW((stderr, "pre-checking generation 0\n"));
4250 verify_generation(0);
4253 if (gencgc_verbose > 1)
4254 print_generation_stats(0);
4257 /* Collect the generation. */
4259 if (gen >= gencgc_oldest_gen_to_gc) {
4260 /* Never raise the oldest generation. */
4265 || (generations[gen].num_gc >= generations[gen].trigger_age);
4268 if (gencgc_verbose > 1) {
4270 "starting GC of generation %d with raise=%d alloc=%d trig=%d GCs=%d\n",
4273 generations[gen].bytes_allocated,
4274 generations[gen].gc_trigger,
4275 generations[gen].num_gc));
4278 /* If an older generation is being filled, then update its
4281 generations[gen+1].cum_sum_bytes_allocated +=
4282 generations[gen+1].bytes_allocated;
4285 garbage_collect_generation(gen, raise);
4287 /* Reset the memory age cum_sum. */
4288 generations[gen].cum_sum_bytes_allocated = 0;
4290 if (gencgc_verbose > 1) {
4291 FSHOW((stderr, "GC of generation %d finished:\n", gen));
4292 print_generation_stats(0);
4296 } while ((gen <= gencgc_oldest_gen_to_gc)
4297 && ((gen < last_gen)
4298 || ((gen <= gencgc_oldest_gen_to_gc)
4300 && (generations[gen].bytes_allocated
4301 > generations[gen].gc_trigger)
4302 && (gen_av_mem_age(gen)
4303 > generations[gen].min_av_mem_age))));
4305 /* Now if gen-1 was raised all generations before gen are empty.
4306 * If it wasn't raised then all generations before gen-1 are empty.
4308 * Now objects within this gen's pages cannot point to younger
4309 * generations unless they are written to. This can be exploited
4310 * by write-protecting the pages of gen; then when younger
4311 * generations are GCed only the pages which have been written
4316 gen_to_wp = gen - 1;
4318 /* There's not much point in WPing pages in generation 0 as it is
4319 * never scavenged (except promoted pages). */
4320 if ((gen_to_wp > 0) && enable_page_protection) {
4321 /* Check that they are all empty. */
4322 for (i = 0; i < gen_to_wp; i++) {
4323 if (generations[i].bytes_allocated)
4324 lose("trying to write-protect gen. %d when gen. %d nonempty\n",
4327 write_protect_generation_pages(gen_to_wp);
4330 /* Set gc_alloc() back to generation 0. The current regions should
4331 * be flushed after the above GCs. */
4332 gc_assert((boxed_region.free_pointer - boxed_region.start_addr) == 0);
4333 gc_alloc_generation = 0;
4335 /* Save the high-water mark before updating last_free_page */
4336 if (last_free_page > high_water_mark)
4337 high_water_mark = last_free_page;
4339 update_dynamic_space_free_pointer();
4341 auto_gc_trigger = bytes_allocated + bytes_consed_between_gcs;
4343 fprintf(stderr,"Next gc when %ld bytes have been consed\n",
4346 /* If we did a big GC (arbitrarily defined as gen > 1), release memory
4349 if (gen > small_generation_limit) {
4350 if (last_free_page > high_water_mark)
4351 high_water_mark = last_free_page;
4352 remap_free_pages(0, high_water_mark);
4353 high_water_mark = 0;
4358 SHOW("returning from collect_garbage");
4361 /* This is called by Lisp PURIFY when it is finished. All live objects
4362 * will have been moved to the RO and Static heaps. The dynamic space
4363 * will need a full re-initialization. We don't bother having Lisp
4364 * PURIFY flush the current gc_alloc() region, as the page_tables are
4365 * re-initialized, and every page is zeroed to be sure. */
4371 if (gencgc_verbose > 1)
4372 SHOW("entering gc_free_heap");
4374 for (page = 0; page < page_table_pages; page++) {
4375 /* Skip free pages which should already be zero filled. */
4376 if (page_table[page].allocated != FREE_PAGE_FLAG) {
4377 void *page_start, *addr;
4379 /* Mark the page free. The other slots are assumed invalid
4380 * when it is a FREE_PAGE_FLAG and bytes_used is 0 and it
4381 * should not be write-protected -- except that the
4382 * generation is used for the current region but it sets
4384 page_table[page].allocated = FREE_PAGE_FLAG;
4385 page_table[page].bytes_used = 0;
4387 #ifndef LISP_FEATURE_WIN32 /* Pages already zeroed on win32? Not sure about this change. */
4388 /* Zero the page. */
4389 page_start = (void *)page_address(page);
4391 /* First, remove any write-protection. */
4392 os_protect(page_start, PAGE_BYTES, OS_VM_PROT_ALL);
4393 page_table[page].write_protected = 0;
4395 os_invalidate(page_start,PAGE_BYTES);
4396 addr = os_validate(page_start,PAGE_BYTES);
4397 if (addr == NULL || addr != page_start) {
4398 lose("gc_free_heap: page moved, 0x%08x ==> 0x%08x\n",
4403 page_table[page].write_protected = 0;
4405 } else if (gencgc_zero_check_during_free_heap) {
4406 /* Double-check that the page is zero filled. */
4409 gc_assert(page_table[page].allocated == FREE_PAGE_FLAG);
4410 gc_assert(page_table[page].bytes_used == 0);
4411 page_start = (long *)page_address(page);
4412 for (i=0; i<1024; i++) {
4413 if (page_start[i] != 0) {
4414 lose("free region not zero at %x\n", page_start + i);
4420 bytes_allocated = 0;
4422 /* Initialize the generations. */
4423 for (page = 0; page < NUM_GENERATIONS; page++) {
4424 generations[page].alloc_start_page = 0;
4425 generations[page].alloc_unboxed_start_page = 0;
4426 generations[page].alloc_large_start_page = 0;
4427 generations[page].alloc_large_unboxed_start_page = 0;
4428 generations[page].bytes_allocated = 0;
4429 generations[page].gc_trigger = 2000000;
4430 generations[page].num_gc = 0;
4431 generations[page].cum_sum_bytes_allocated = 0;
4432 generations[page].lutexes = NULL;
4435 if (gencgc_verbose > 1)
4436 print_generation_stats(0);
4438 /* Initialize gc_alloc(). */
4439 gc_alloc_generation = 0;
4441 gc_set_region_empty(&boxed_region);
4442 gc_set_region_empty(&unboxed_region);
4445 set_alloc_pointer((lispobj)((char *)heap_base));
4447 if (verify_after_free_heap) {
4448 /* Check whether purify has left any bad pointers. */
4449 FSHOW((stderr, "checking after free_heap\n"));
4459 /* Compute the number of pages needed for the dynamic space.
4460 * Dynamic space size should be aligned on page size. */
4461 page_table_pages = dynamic_space_size/PAGE_BYTES;
4462 gc_assert(dynamic_space_size == (size_t) page_table_pages*PAGE_BYTES);
4464 page_table = calloc(page_table_pages, sizeof(struct page));
4465 gc_assert(page_table);
4468 scavtab[WEAK_POINTER_WIDETAG] = scav_weak_pointer;
4469 transother[SIMPLE_ARRAY_WIDETAG] = trans_boxed_large;
4471 #ifdef LUTEX_WIDETAG
4472 scavtab[LUTEX_WIDETAG] = scav_lutex;
4473 transother[LUTEX_WIDETAG] = trans_lutex;
4474 sizetab[LUTEX_WIDETAG] = size_lutex;
4477 heap_base = (void*)DYNAMIC_SPACE_START;
4479 /* Initialize each page structure. */
4480 for (i = 0; i < page_table_pages; i++) {
4481 /* Initialize all pages as free. */
4482 page_table[i].allocated = FREE_PAGE_FLAG;
4483 page_table[i].bytes_used = 0;
4485 /* Pages are not write-protected at startup. */
4486 page_table[i].write_protected = 0;
4489 bytes_allocated = 0;
4491 /* Initialize the generations.
4493 * FIXME: very similar to code in gc_free_heap(), should be shared */
4494 for (i = 0; i < NUM_GENERATIONS; i++) {
4495 generations[i].alloc_start_page = 0;
4496 generations[i].alloc_unboxed_start_page = 0;
4497 generations[i].alloc_large_start_page = 0;
4498 generations[i].alloc_large_unboxed_start_page = 0;
4499 generations[i].bytes_allocated = 0;
4500 generations[i].gc_trigger = 2000000;
4501 generations[i].num_gc = 0;
4502 generations[i].cum_sum_bytes_allocated = 0;
4503 /* the tune-able parameters */
4504 generations[i].bytes_consed_between_gc = 2000000;
4505 generations[i].trigger_age = 1;
4506 generations[i].min_av_mem_age = 0.75;
4507 generations[i].lutexes = NULL;
4510 /* Initialize gc_alloc. */
4511 gc_alloc_generation = 0;
4512 gc_set_region_empty(&boxed_region);
4513 gc_set_region_empty(&unboxed_region);
4518 /* Pick up the dynamic space from after a core load.
4520 * The ALLOCATION_POINTER points to the end of the dynamic space.
4524 gencgc_pickup_dynamic(void)
4526 page_index_t page = 0;
4527 long alloc_ptr = get_alloc_pointer();
4528 lispobj *prev=(lispobj *)page_address(page);
4529 generation_index_t gen = PSEUDO_STATIC_GENERATION;
4532 lispobj *first,*ptr= (lispobj *)page_address(page);
4533 page_table[page].allocated = BOXED_PAGE_FLAG;
4534 page_table[page].gen = gen;
4535 page_table[page].bytes_used = PAGE_BYTES;
4536 page_table[page].large_object = 0;
4537 page_table[page].write_protected = 0;
4538 page_table[page].write_protected_cleared = 0;
4539 page_table[page].dont_move = 0;
4540 page_table[page].need_to_zero = 1;
4542 if (!gencgc_partial_pickup) {
4543 first=gc_search_space(prev,(ptr+2)-prev,ptr);
4544 if(ptr == first) prev=ptr;
4545 page_table[page].first_object_offset =
4546 (void *)prev - page_address(page);
4549 } while ((long)page_address(page) < alloc_ptr);
4551 #ifdef LUTEX_WIDETAG
4552 /* Lutexes have been registered in generation 0 by coreparse, and
4553 * need to be moved to the right one manually.
4555 move_lutexes(0, PSEUDO_STATIC_GENERATION);
4558 last_free_page = page;
4560 generations[gen].bytes_allocated = PAGE_BYTES*page;
4561 bytes_allocated = PAGE_BYTES*page;
4563 gc_alloc_update_all_page_tables();
4564 write_protect_generation_pages(gen);
4568 gc_initialize_pointers(void)
4570 gencgc_pickup_dynamic();
4576 /* alloc(..) is the external interface for memory allocation. It
4577 * allocates to generation 0. It is not called from within the garbage
4578 * collector as it is only external uses that need the check for heap
4579 * size (GC trigger) and to disable the interrupts (interrupts are
4580 * always disabled during a GC).
4582 * The vops that call alloc(..) assume that the returned space is zero-filled.
4583 * (E.g. the most significant word of a 2-word bignum in MOVE-FROM-UNSIGNED.)
4585 * The check for a GC trigger is only performed when the current
4586 * region is full, so in most cases it's not needed. */
4591 struct thread *thread=arch_os_get_current_thread();
4592 struct alloc_region *region=
4593 #ifdef LISP_FEATURE_SB_THREAD
4594 thread ? &(thread->alloc_region) : &boxed_region;
4598 #ifndef LISP_FEATURE_WIN32
4599 lispobj alloc_signal;
4602 void *new_free_pointer;
4604 gc_assert(nbytes>0);
4606 /* Check for alignment allocation problems. */
4607 gc_assert((((unsigned long)region->free_pointer & LOWTAG_MASK) == 0)
4608 && ((nbytes & LOWTAG_MASK) == 0));
4612 /* there are a few places in the C code that allocate data in the
4613 * heap before Lisp starts. This is before interrupts are enabled,
4614 * so we don't need to check for pseudo-atomic */
4615 #ifdef LISP_FEATURE_SB_THREAD
4616 if(!get_psuedo_atomic_atomic(th)) {
4618 fprintf(stderr, "fatal error in thread 0x%x, tid=%ld\n",
4620 __asm__("movl %fs,%0" : "=r" (fs) : );
4621 fprintf(stderr, "fs is %x, th->tls_cookie=%x \n",
4622 debug_get_fs(),th->tls_cookie);
4623 lose("If you see this message before 2004.01.31, mail details to sbcl-devel\n");
4626 gc_assert(get_pseudo_atomic_atomic(th));
4630 /* maybe we can do this quickly ... */
4631 new_free_pointer = region->free_pointer + nbytes;
4632 if (new_free_pointer <= region->end_addr) {
4633 new_obj = (void*)(region->free_pointer);
4634 region->free_pointer = new_free_pointer;
4635 return(new_obj); /* yup */
4638 /* we have to go the long way around, it seems. Check whether
4639 * we should GC in the near future
4641 if (auto_gc_trigger && bytes_allocated > auto_gc_trigger) {
4642 gc_assert(get_pseudo_atomic_atomic(thread));
4643 /* Don't flood the system with interrupts if the need to gc is
4644 * already noted. This can happen for example when SUB-GC
4645 * allocates or after a gc triggered in a WITHOUT-GCING. */
4646 if (SymbolValue(GC_PENDING,thread) == NIL) {
4647 /* set things up so that GC happens when we finish the PA
4649 SetSymbolValue(GC_PENDING,T,thread);
4650 if (SymbolValue(GC_INHIBIT,thread) == NIL)
4651 set_pseudo_atomic_interrupted(thread);
4654 new_obj = gc_alloc_with_region(nbytes,0,region,0);
4656 #ifndef LISP_FEATURE_WIN32
4657 alloc_signal = SymbolValue(ALLOC_SIGNAL,thread);
4658 if ((alloc_signal & FIXNUM_TAG_MASK) == 0) {
4659 if ((signed long) alloc_signal <= 0) {
4660 #ifdef LISP_FEATURE_SB_THREAD
4661 kill_thread_safely(thread->os_thread, SIGPROF);
4666 SetSymbolValue(ALLOC_SIGNAL,
4667 alloc_signal - (1 << N_FIXNUM_TAG_BITS),
4677 * shared support for the OS-dependent signal handlers which
4678 * catch GENCGC-related write-protect violations
4681 void unhandled_sigmemoryfault(void* addr);
4683 /* Depending on which OS we're running under, different signals might
4684 * be raised for a violation of write protection in the heap. This
4685 * function factors out the common generational GC magic which needs
4686 * to invoked in this case, and should be called from whatever signal
4687 * handler is appropriate for the OS we're running under.
4689 * Return true if this signal is a normal generational GC thing that
4690 * we were able to handle, or false if it was abnormal and control
4691 * should fall through to the general SIGSEGV/SIGBUS/whatever logic. */
4694 gencgc_handle_wp_violation(void* fault_addr)
4696 page_index_t page_index = find_page_index(fault_addr);
4698 #ifdef QSHOW_SIGNALS
4699 FSHOW((stderr, "heap WP violation? fault_addr=%x, page_index=%d\n",
4700 fault_addr, page_index));
4703 /* Check whether the fault is within the dynamic space. */
4704 if (page_index == (-1)) {
4706 /* It can be helpful to be able to put a breakpoint on this
4707 * case to help diagnose low-level problems. */
4708 unhandled_sigmemoryfault(fault_addr);
4710 /* not within the dynamic space -- not our responsibility */
4714 if (page_table[page_index].write_protected) {
4715 /* Unprotect the page. */
4716 os_protect(page_address(page_index), PAGE_BYTES, OS_VM_PROT_ALL);
4717 page_table[page_index].write_protected_cleared = 1;
4718 page_table[page_index].write_protected = 0;
4720 /* The only acceptable reason for this signal on a heap
4721 * access is that GENCGC write-protected the page.
4722 * However, if two CPUs hit a wp page near-simultaneously,
4723 * we had better not have the second one lose here if it
4724 * does this test after the first one has already set wp=0
4726 if(page_table[page_index].write_protected_cleared != 1)
4727 lose("fault in heap page %d not marked as write-protected\nboxed_region.first_page: %d, boxed_region.last_page %d\n",
4728 page_index, boxed_region.first_page, boxed_region.last_page);
4730 /* Don't worry, we can handle it. */
4734 /* This is to be called when we catch a SIGSEGV/SIGBUS, determine that
4735 * it's not just a case of the program hitting the write barrier, and
4736 * are about to let Lisp deal with it. It's basically just a
4737 * convenient place to set a gdb breakpoint. */
4739 unhandled_sigmemoryfault(void *addr)
4742 void gc_alloc_update_all_page_tables(void)
4744 /* Flush the alloc regions updating the tables. */
4747 gc_alloc_update_page_tables(0, &th->alloc_region);
4748 gc_alloc_update_page_tables(1, &unboxed_region);
4749 gc_alloc_update_page_tables(0, &boxed_region);
4753 gc_set_region_empty(struct alloc_region *region)
4755 region->first_page = 0;
4756 region->last_page = -1;
4757 region->start_addr = page_address(0);
4758 region->free_pointer = page_address(0);
4759 region->end_addr = page_address(0);
4763 zero_all_free_pages()
4767 for (i = 0; i < last_free_page; i++) {
4768 if (page_table[i].allocated == FREE_PAGE_FLAG) {
4769 #ifdef READ_PROTECT_FREE_PAGES
4770 os_protect(page_address(i),
4779 /* Things to do before doing a final GC before saving a core (without
4782 * + Pages in large_object pages aren't moved by the GC, so we need to
4783 * unset that flag from all pages.
4784 * + The pseudo-static generation isn't normally collected, but it seems
4785 * reasonable to collect it at least when saving a core. So move the
4786 * pages to a normal generation.
4789 prepare_for_final_gc ()
4792 for (i = 0; i < last_free_page; i++) {
4793 page_table[i].large_object = 0;
4794 if (page_table[i].gen == PSEUDO_STATIC_GENERATION) {
4795 int used = page_table[i].bytes_used;
4796 page_table[i].gen = HIGHEST_NORMAL_GENERATION;
4797 generations[PSEUDO_STATIC_GENERATION].bytes_allocated -= used;
4798 generations[HIGHEST_NORMAL_GENERATION].bytes_allocated += used;
4804 /* Do a non-conservative GC, and then save a core with the initial
4805 * function being set to the value of the static symbol
4806 * SB!VM:RESTART-LISP-FUNCTION */
4808 gc_and_save(char *filename, int prepend_runtime)
4811 void *runtime_bytes = NULL;
4812 size_t runtime_size;
4814 file = prepare_to_save(filename, prepend_runtime, &runtime_bytes,
4819 conservative_stack = 0;
4821 /* The filename might come from Lisp, and be moved by the now
4822 * non-conservative GC. */
4823 filename = strdup(filename);
4825 /* Collect twice: once into relatively high memory, and then back
4826 * into low memory. This compacts the retained data into the lower
4827 * pages, minimizing the size of the core file.
4829 prepare_for_final_gc();
4830 gencgc_alloc_start_page = last_free_page;
4831 collect_garbage(HIGHEST_NORMAL_GENERATION+1);
4833 prepare_for_final_gc();
4834 gencgc_alloc_start_page = -1;
4835 collect_garbage(HIGHEST_NORMAL_GENERATION+1);
4837 if (prepend_runtime)
4838 save_runtime_to_filehandle(file, runtime_bytes, runtime_size);
4840 /* The dumper doesn't know that pages need to be zeroed before use. */
4841 zero_all_free_pages();
4842 save_to_filehandle(file, filename, SymbolValue(RESTART_LISP_FUNCTION,0),
4844 /* Oops. Save still managed to fail. Since we've mangled the stack
4845 * beyond hope, there's not much we can do.
4846 * (beyond FUNCALLing RESTART_LISP_FUNCTION, but I suspect that's
4847 * going to be rather unsatisfactory too... */
4848 lose("Attempt to save core after non-conservative GC failed.\n");