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"
45 #include "genesis/vector.h"
46 #include "genesis/weak-pointer.h"
47 #include "genesis/fdefn.h"
48 #include "genesis/simple-fun.h"
50 #include "genesis/hash-table.h"
51 #include "genesis/instance.h"
52 #include "genesis/layout.h"
54 #if defined(LUTEX_WIDETAG)
55 #include "pthread-lutex.h"
58 /* forward declarations */
59 page_index_t gc_find_freeish_pages(long *restart_page_ptr, long nbytes,
67 /* Generations 0-5 are normal collected generations, 6 is only used as
68 * scratch space by the collector, and should never get collected.
71 HIGHEST_NORMAL_GENERATION = 5,
72 PSEUDO_STATIC_GENERATION,
77 /* Should we use page protection to help avoid the scavenging of pages
78 * that don't have pointers to younger generations? */
79 boolean enable_page_protection = 1;
81 /* the minimum size (in bytes) for a large object*/
82 unsigned long large_object_size = 4 * PAGE_BYTES;
89 /* the verbosity level. All non-error messages are disabled at level 0;
90 * and only a few rare messages are printed at level 1. */
92 boolean gencgc_verbose = 1;
94 boolean gencgc_verbose = 0;
97 /* FIXME: At some point enable the various error-checking things below
98 * and see what they say. */
100 /* We hunt for pointers to old-space, when GCing generations >= verify_gen.
101 * Set verify_gens to HIGHEST_NORMAL_GENERATION + 1 to disable this kind of
103 generation_index_t verify_gens = HIGHEST_NORMAL_GENERATION + 1;
105 /* Should we do a pre-scan verify of generation 0 before it's GCed? */
106 boolean pre_verify_gen_0 = 0;
108 /* Should we check for bad pointers after gc_free_heap is called
109 * from Lisp PURIFY? */
110 boolean verify_after_free_heap = 0;
112 /* Should we print a note when code objects are found in the dynamic space
113 * during a heap verify? */
114 boolean verify_dynamic_code_check = 0;
116 /* Should we check code objects for fixup errors after they are transported? */
117 boolean check_code_fixups = 0;
119 /* Should we check that newly allocated regions are zero filled? */
120 boolean gencgc_zero_check = 0;
122 /* Should we check that the free space is zero filled? */
123 boolean gencgc_enable_verify_zero_fill = 0;
125 /* Should we check that free pages are zero filled during gc_free_heap
126 * called after Lisp PURIFY? */
127 boolean gencgc_zero_check_during_free_heap = 0;
129 /* When loading a core, don't do a full scan of the memory for the
130 * memory region boundaries. (Set to true by coreparse.c if the core
131 * contained a pagetable entry).
133 boolean gencgc_partial_pickup = 0;
135 /* If defined, free pages are read-protected to ensure that nothing
139 /* #define READ_PROTECT_FREE_PAGES */
143 * GC structures and variables
146 /* the total bytes allocated. These are seen by Lisp DYNAMIC-USAGE. */
147 unsigned long bytes_allocated = 0;
148 unsigned long auto_gc_trigger = 0;
150 /* the source and destination generations. These are set before a GC starts
152 generation_index_t from_space;
153 generation_index_t new_space;
155 /* Set to 1 when in GC */
156 boolean gc_active_p = 0;
158 /* should the GC be conservative on stack. If false (only right before
159 * saving a core), don't scan the stack / mark pages dont_move. */
160 static boolean conservative_stack = 1;
162 /* An array of page structures is allocated on gc initialization.
163 * This helps quickly map between an address its page structure.
164 * page_table_pages is set from the size of the dynamic space. */
165 unsigned page_table_pages;
166 struct page *page_table;
168 /* To map addresses to page structures the address of the first page
170 static void *heap_base = NULL;
172 /* Calculate the start address for the given page number. */
174 page_address(page_index_t page_num)
176 return (heap_base + (page_num * PAGE_BYTES));
179 /* Find the page index within the page_table for the given
180 * address. Return -1 on failure. */
182 find_page_index(void *addr)
184 page_index_t index = addr-heap_base;
187 index = ((unsigned long)index)/PAGE_BYTES;
188 if (index < page_table_pages)
195 /* a structure to hold the state of a generation */
198 /* the first page that gc_alloc() checks on its next call */
199 page_index_t alloc_start_page;
201 /* the first page that gc_alloc_unboxed() checks on its next call */
202 page_index_t alloc_unboxed_start_page;
204 /* the first page that gc_alloc_large (boxed) considers on its next
205 * call. (Although it always allocates after the boxed_region.) */
206 page_index_t alloc_large_start_page;
208 /* the first page that gc_alloc_large (unboxed) considers on its
209 * next call. (Although it always allocates after the
210 * current_unboxed_region.) */
211 page_index_t alloc_large_unboxed_start_page;
213 /* the bytes allocated to this generation */
214 long bytes_allocated;
216 /* the number of bytes at which to trigger a GC */
219 /* to calculate a new level for gc_trigger */
220 long bytes_consed_between_gc;
222 /* the number of GCs since the last raise */
225 /* the average age after which a GC will raise objects to the
229 /* the cumulative sum of the bytes allocated to this generation. It is
230 * cleared after a GC on this generations, and update before new
231 * objects are added from a GC of a younger generation. Dividing by
232 * the bytes_allocated will give the average age of the memory in
233 * this generation since its last GC. */
234 long cum_sum_bytes_allocated;
236 /* a minimum average memory age before a GC will occur helps
237 * prevent a GC when a large number of new live objects have been
238 * added, in which case a GC could be a waste of time */
239 double min_av_mem_age;
241 /* A linked list of lutex structures in this generation, used for
242 * implementing lutex finalization. */
244 struct lutex *lutexes;
250 /* an array of generation structures. There needs to be one more
251 * generation structure than actual generations as the oldest
252 * generation is temporarily raised then lowered. */
253 struct generation generations[NUM_GENERATIONS];
255 /* the oldest generation that is will currently be GCed by default.
256 * Valid values are: 0, 1, ... HIGHEST_NORMAL_GENERATION
258 * The default of HIGHEST_NORMAL_GENERATION enables GC on all generations.
260 * Setting this to 0 effectively disables the generational nature of
261 * the GC. In some applications generational GC may not be useful
262 * because there are no long-lived objects.
264 * An intermediate value could be handy after moving long-lived data
265 * into an older generation so an unnecessary GC of this long-lived
266 * data can be avoided. */
267 generation_index_t gencgc_oldest_gen_to_gc = HIGHEST_NORMAL_GENERATION;
269 /* The maximum free page in the heap is maintained and used to update
270 * ALLOCATION_POINTER which is used by the room function to limit its
271 * search of the heap. XX Gencgc obviously needs to be better
272 * integrated with the Lisp code. */
273 page_index_t last_free_page;
275 /* This lock is to prevent multiple threads from simultaneously
276 * allocating new regions which overlap each other. Note that the
277 * majority of GC is single-threaded, but alloc() may be called from
278 * >1 thread at a time and must be thread-safe. This lock must be
279 * seized before all accesses to generations[] or to parts of
280 * page_table[] that other threads may want to see */
282 #ifdef LISP_FEATURE_SB_THREAD
283 static pthread_mutex_t free_pages_lock = PTHREAD_MUTEX_INITIALIZER;
288 * miscellaneous heap functions
291 /* Count the number of pages which are write-protected within the
292 * given generation. */
294 count_write_protect_generation_pages(generation_index_t generation)
299 for (i = 0; i < last_free_page; i++)
300 if ((page_table[i].allocated != FREE_PAGE_FLAG)
301 && (page_table[i].gen == generation)
302 && (page_table[i].write_protected == 1))
307 /* Count the number of pages within the given generation. */
309 count_generation_pages(generation_index_t generation)
314 for (i = 0; i < last_free_page; i++)
315 if ((page_table[i].allocated != FREE_PAGE_FLAG)
316 && (page_table[i].gen == generation))
323 count_dont_move_pages(void)
327 for (i = 0; i < last_free_page; i++) {
328 if ((page_table[i].allocated != FREE_PAGE_FLAG)
329 && (page_table[i].dont_move != 0)) {
337 /* Work through the pages and add up the number of bytes used for the
338 * given generation. */
340 count_generation_bytes_allocated (generation_index_t gen)
344 for (i = 0; i < last_free_page; i++) {
345 if ((page_table[i].allocated != FREE_PAGE_FLAG)
346 && (page_table[i].gen == gen))
347 result += page_table[i].bytes_used;
352 /* Return the average age of the memory in a generation. */
354 gen_av_mem_age(generation_index_t gen)
356 if (generations[gen].bytes_allocated == 0)
360 ((double)generations[gen].cum_sum_bytes_allocated)
361 / ((double)generations[gen].bytes_allocated);
364 /* The verbose argument controls how much to print: 0 for normal
365 * level of detail; 1 for debugging. */
367 print_generation_stats(int verbose) /* FIXME: should take FILE argument */
369 generation_index_t i, gens;
371 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
372 #define FPU_STATE_SIZE 27
373 int fpu_state[FPU_STATE_SIZE];
374 #elif defined(LISP_FEATURE_PPC)
375 #define FPU_STATE_SIZE 32
376 long long fpu_state[FPU_STATE_SIZE];
379 /* This code uses the FP instructions which may be set up for Lisp
380 * so they need to be saved and reset for C. */
383 /* highest generation to print */
385 gens = SCRATCH_GENERATION;
387 gens = PSEUDO_STATIC_GENERATION;
389 /* Print the heap stats. */
391 " Gen StaPg UbSta LaSta LUbSt Boxed Unboxed LB LUB !move Alloc Waste Trig WP GCs Mem-age\n");
393 for (i = 0; i < gens; i++) {
396 long unboxed_cnt = 0;
397 long large_boxed_cnt = 0;
398 long large_unboxed_cnt = 0;
401 for (j = 0; j < last_free_page; j++)
402 if (page_table[j].gen == i) {
404 /* Count the number of boxed pages within the given
406 if (page_table[j].allocated & BOXED_PAGE_FLAG) {
407 if (page_table[j].large_object)
412 if(page_table[j].dont_move) pinned_cnt++;
413 /* Count the number of unboxed pages within the given
415 if (page_table[j].allocated & UNBOXED_PAGE_FLAG) {
416 if (page_table[j].large_object)
423 gc_assert(generations[i].bytes_allocated
424 == count_generation_bytes_allocated(i));
426 " %1d: %5ld %5ld %5ld %5ld %5ld %5ld %5ld %5ld %5ld %8ld %5ld %8ld %4ld %3d %7.4f\n",
428 generations[i].alloc_start_page,
429 generations[i].alloc_unboxed_start_page,
430 generations[i].alloc_large_start_page,
431 generations[i].alloc_large_unboxed_start_page,
437 generations[i].bytes_allocated,
438 (count_generation_pages(i)*PAGE_BYTES - generations[i].bytes_allocated),
439 generations[i].gc_trigger,
440 count_write_protect_generation_pages(i),
441 generations[i].num_gc,
444 fprintf(stderr," Total bytes allocated=%ld\n", bytes_allocated);
446 fpu_restore(fpu_state);
450 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
451 void fast_bzero(void*, size_t); /* in <arch>-assem.S */
454 /* Zero the pages from START to END (inclusive), but use mmap/munmap instead
455 * if zeroing it ourselves, i.e. in practice give the memory back to the
456 * OS. Generally done after a large GC.
458 void zero_pages_with_mmap(page_index_t start, page_index_t end) {
460 void *addr = (void *) page_address(start), *new_addr;
461 size_t length = PAGE_BYTES*(1+end-start);
466 os_invalidate(addr, length);
467 new_addr = os_validate(addr, length);
468 if (new_addr == NULL || new_addr != addr) {
469 lose("remap_free_pages: page moved, 0x%08x ==> 0x%08x", start, new_addr);
472 for (i = start; i <= end; i++) {
473 page_table[i].need_to_zero = 0;
477 /* Zero the pages from START to END (inclusive). Generally done just after
478 * a new region has been allocated.
481 zero_pages(page_index_t start, page_index_t end) {
485 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
486 fast_bzero(page_address(start), PAGE_BYTES*(1+end-start));
488 bzero(page_address(start), PAGE_BYTES*(1+end-start));
493 /* Zero the pages from START to END (inclusive), except for those
494 * pages that are known to already zeroed. Mark all pages in the
495 * ranges as non-zeroed.
498 zero_dirty_pages(page_index_t start, page_index_t end) {
501 for (i = start; i <= end; i++) {
502 if (page_table[i].need_to_zero == 1) {
503 zero_pages(start, end);
508 for (i = start; i <= end; i++) {
509 page_table[i].need_to_zero = 1;
515 * To support quick and inline allocation, regions of memory can be
516 * allocated and then allocated from with just a free pointer and a
517 * check against an end address.
519 * Since objects can be allocated to spaces with different properties
520 * e.g. boxed/unboxed, generation, ages; there may need to be many
521 * allocation regions.
523 * Each allocation region may start within a partly used page. Many
524 * features of memory use are noted on a page wise basis, e.g. the
525 * generation; so if a region starts within an existing allocated page
526 * it must be consistent with this page.
528 * During the scavenging of the newspace, objects will be transported
529 * into an allocation region, and pointers updated to point to this
530 * allocation region. It is possible that these pointers will be
531 * scavenged again before the allocation region is closed, e.g. due to
532 * trans_list which jumps all over the place to cleanup the list. It
533 * is important to be able to determine properties of all objects
534 * pointed to when scavenging, e.g to detect pointers to the oldspace.
535 * Thus it's important that the allocation regions have the correct
536 * properties set when allocated, and not just set when closed. The
537 * region allocation routines return regions with the specified
538 * properties, and grab all the pages, setting their properties
539 * appropriately, except that the amount used is not known.
541 * These regions are used to support quicker allocation using just a
542 * free pointer. The actual space used by the region is not reflected
543 * in the pages tables until it is closed. It can't be scavenged until
546 * When finished with the region it should be closed, which will
547 * update the page tables for the actual space used returning unused
548 * space. Further it may be noted in the new regions which is
549 * necessary when scavenging the newspace.
551 * Large objects may be allocated directly without an allocation
552 * region, the page tables are updated immediately.
554 * Unboxed objects don't contain pointers to other objects and so
555 * don't need scavenging. Further they can't contain pointers to
556 * younger generations so WP is not needed. By allocating pages to
557 * unboxed objects the whole page never needs scavenging or
558 * write-protecting. */
560 /* We are only using two regions at present. Both are for the current
561 * newspace generation. */
562 struct alloc_region boxed_region;
563 struct alloc_region unboxed_region;
565 /* The generation currently being allocated to. */
566 static generation_index_t gc_alloc_generation;
568 /* Find a new region with room for at least the given number of bytes.
570 * It starts looking at the current generation's alloc_start_page. So
571 * may pick up from the previous region if there is enough space. This
572 * keeps the allocation contiguous when scavenging the newspace.
574 * The alloc_region should have been closed by a call to
575 * gc_alloc_update_page_tables(), and will thus be in an empty state.
577 * To assist the scavenging functions write-protected pages are not
578 * used. Free pages should not be write-protected.
580 * It is critical to the conservative GC that the start of regions be
581 * known. To help achieve this only small regions are allocated at a
584 * During scavenging, pointers may be found to within the current
585 * region and the page generation must be set so that pointers to the
586 * from space can be recognized. Therefore the generation of pages in
587 * the region are set to gc_alloc_generation. To prevent another
588 * allocation call using the same pages, all the pages in the region
589 * are allocated, although they will initially be empty.
592 gc_alloc_new_region(long nbytes, int unboxed, struct alloc_region *alloc_region)
594 page_index_t first_page;
595 page_index_t last_page;
602 "/alloc_new_region for %d bytes from gen %d\n",
603 nbytes, gc_alloc_generation));
606 /* Check that the region is in a reset state. */
607 gc_assert((alloc_region->first_page == 0)
608 && (alloc_region->last_page == -1)
609 && (alloc_region->free_pointer == alloc_region->end_addr));
610 ret = thread_mutex_lock(&free_pages_lock);
614 generations[gc_alloc_generation].alloc_unboxed_start_page;
617 generations[gc_alloc_generation].alloc_start_page;
619 last_page=gc_find_freeish_pages(&first_page,nbytes,unboxed);
620 bytes_found=(PAGE_BYTES - page_table[first_page].bytes_used)
621 + PAGE_BYTES*(last_page-first_page);
623 /* Set up the alloc_region. */
624 alloc_region->first_page = first_page;
625 alloc_region->last_page = last_page;
626 alloc_region->start_addr = page_table[first_page].bytes_used
627 + page_address(first_page);
628 alloc_region->free_pointer = alloc_region->start_addr;
629 alloc_region->end_addr = alloc_region->start_addr + bytes_found;
631 /* Set up the pages. */
633 /* The first page may have already been in use. */
634 if (page_table[first_page].bytes_used == 0) {
636 page_table[first_page].allocated = UNBOXED_PAGE_FLAG;
638 page_table[first_page].allocated = BOXED_PAGE_FLAG;
639 page_table[first_page].gen = gc_alloc_generation;
640 page_table[first_page].large_object = 0;
641 page_table[first_page].first_object_offset = 0;
645 gc_assert(page_table[first_page].allocated == UNBOXED_PAGE_FLAG);
647 gc_assert(page_table[first_page].allocated == BOXED_PAGE_FLAG);
648 page_table[first_page].allocated |= OPEN_REGION_PAGE_FLAG;
650 gc_assert(page_table[first_page].gen == gc_alloc_generation);
651 gc_assert(page_table[first_page].large_object == 0);
653 for (i = first_page+1; i <= last_page; i++) {
655 page_table[i].allocated = UNBOXED_PAGE_FLAG;
657 page_table[i].allocated = BOXED_PAGE_FLAG;
658 page_table[i].gen = gc_alloc_generation;
659 page_table[i].large_object = 0;
660 /* This may not be necessary for unboxed regions (think it was
662 page_table[i].first_object_offset =
663 alloc_region->start_addr - page_address(i);
664 page_table[i].allocated |= OPEN_REGION_PAGE_FLAG ;
666 /* Bump up last_free_page. */
667 if (last_page+1 > last_free_page) {
668 last_free_page = last_page+1;
669 /* do we only want to call this on special occasions? like for boxed_region? */
670 set_alloc_pointer((lispobj)(((char *)heap_base) + last_free_page*PAGE_BYTES));
672 ret = thread_mutex_unlock(&free_pages_lock);
675 /* we can do this after releasing free_pages_lock */
676 if (gencgc_zero_check) {
678 for (p = (long *)alloc_region->start_addr;
679 p < (long *)alloc_region->end_addr; p++) {
681 /* KLUDGE: It would be nice to use %lx and explicit casts
682 * (long) in code like this, so that it is less likely to
683 * break randomly when running on a machine with different
684 * word sizes. -- WHN 19991129 */
685 lose("The new region at %x is not zero.\n", p);
690 #ifdef READ_PROTECT_FREE_PAGES
691 os_protect(page_address(first_page),
692 PAGE_BYTES*(1+last_page-first_page),
696 /* If the first page was only partial, don't check whether it's
697 * zeroed (it won't be) and don't zero it (since the parts that
698 * we're interested in are guaranteed to be zeroed).
700 if (page_table[first_page].bytes_used) {
704 zero_dirty_pages(first_page, last_page);
707 /* If the record_new_objects flag is 2 then all new regions created
710 * If it's 1 then then it is only recorded if the first page of the
711 * current region is <= new_areas_ignore_page. This helps avoid
712 * unnecessary recording when doing full scavenge pass.
714 * The new_object structure holds the page, byte offset, and size of
715 * new regions of objects. Each new area is placed in the array of
716 * these structures pointer to by new_areas. new_areas_index holds the
717 * offset into new_areas.
719 * If new_area overflows NUM_NEW_AREAS then it stops adding them. The
720 * later code must detect this and handle it, probably by doing a full
721 * scavenge of a generation. */
722 #define NUM_NEW_AREAS 512
723 static int record_new_objects = 0;
724 static page_index_t new_areas_ignore_page;
730 static struct new_area (*new_areas)[];
731 static long new_areas_index;
734 /* Add a new area to new_areas. */
736 add_new_area(page_index_t first_page, long offset, long size)
738 unsigned long new_area_start,c;
741 /* Ignore if full. */
742 if (new_areas_index >= NUM_NEW_AREAS)
745 switch (record_new_objects) {
749 if (first_page > new_areas_ignore_page)
758 new_area_start = PAGE_BYTES*first_page + offset;
760 /* Search backwards for a prior area that this follows from. If
761 found this will save adding a new area. */
762 for (i = new_areas_index-1, c = 0; (i >= 0) && (c < 8); i--, c++) {
763 unsigned long area_end =
764 PAGE_BYTES*((*new_areas)[i].page)
765 + (*new_areas)[i].offset
766 + (*new_areas)[i].size;
768 "/add_new_area S1 %d %d %d %d\n",
769 i, c, new_area_start, area_end));*/
770 if (new_area_start == area_end) {
772 "/adding to [%d] %d %d %d with %d %d %d:\n",
774 (*new_areas)[i].page,
775 (*new_areas)[i].offset,
776 (*new_areas)[i].size,
780 (*new_areas)[i].size += size;
785 (*new_areas)[new_areas_index].page = first_page;
786 (*new_areas)[new_areas_index].offset = offset;
787 (*new_areas)[new_areas_index].size = size;
789 "/new_area %d page %d offset %d size %d\n",
790 new_areas_index, first_page, offset, size));*/
793 /* Note the max new_areas used. */
794 if (new_areas_index > max_new_areas)
795 max_new_areas = new_areas_index;
798 /* Update the tables for the alloc_region. The region may be added to
801 * When done the alloc_region is set up so that the next quick alloc
802 * will fail safely and thus a new region will be allocated. Further
803 * it is safe to try to re-update the page table of this reset
806 gc_alloc_update_page_tables(int unboxed, struct alloc_region *alloc_region)
809 page_index_t first_page;
810 page_index_t next_page;
812 long orig_first_page_bytes_used;
818 first_page = alloc_region->first_page;
820 /* Catch an unused alloc_region. */
821 if ((first_page == 0) && (alloc_region->last_page == -1))
824 next_page = first_page+1;
826 ret = thread_mutex_lock(&free_pages_lock);
828 if (alloc_region->free_pointer != alloc_region->start_addr) {
829 /* some bytes were allocated in the region */
830 orig_first_page_bytes_used = page_table[first_page].bytes_used;
832 gc_assert(alloc_region->start_addr == (page_address(first_page) + page_table[first_page].bytes_used));
834 /* All the pages used need to be updated */
836 /* Update the first page. */
838 /* If the page was free then set up the gen, and
839 * first_object_offset. */
840 if (page_table[first_page].bytes_used == 0)
841 gc_assert(page_table[first_page].first_object_offset == 0);
842 page_table[first_page].allocated &= ~(OPEN_REGION_PAGE_FLAG);
845 gc_assert(page_table[first_page].allocated == UNBOXED_PAGE_FLAG);
847 gc_assert(page_table[first_page].allocated == BOXED_PAGE_FLAG);
848 gc_assert(page_table[first_page].gen == gc_alloc_generation);
849 gc_assert(page_table[first_page].large_object == 0);
853 /* Calculate the number of bytes used in this page. This is not
854 * always the number of new bytes, unless it was free. */
856 if ((bytes_used = (alloc_region->free_pointer - page_address(first_page)))>PAGE_BYTES) {
857 bytes_used = PAGE_BYTES;
860 page_table[first_page].bytes_used = bytes_used;
861 byte_cnt += bytes_used;
864 /* All the rest of the pages should be free. We need to set their
865 * first_object_offset pointer to the start of the region, and set
868 page_table[next_page].allocated &= ~(OPEN_REGION_PAGE_FLAG);
870 gc_assert(page_table[next_page].allocated==UNBOXED_PAGE_FLAG);
872 gc_assert(page_table[next_page].allocated == BOXED_PAGE_FLAG);
873 gc_assert(page_table[next_page].bytes_used == 0);
874 gc_assert(page_table[next_page].gen == gc_alloc_generation);
875 gc_assert(page_table[next_page].large_object == 0);
877 gc_assert(page_table[next_page].first_object_offset ==
878 alloc_region->start_addr - page_address(next_page));
880 /* Calculate the number of bytes used in this page. */
882 if ((bytes_used = (alloc_region->free_pointer
883 - page_address(next_page)))>PAGE_BYTES) {
884 bytes_used = PAGE_BYTES;
887 page_table[next_page].bytes_used = bytes_used;
888 byte_cnt += bytes_used;
893 region_size = alloc_region->free_pointer - alloc_region->start_addr;
894 bytes_allocated += region_size;
895 generations[gc_alloc_generation].bytes_allocated += region_size;
897 gc_assert((byte_cnt- orig_first_page_bytes_used) == region_size);
899 /* Set the generations alloc restart page to the last page of
902 generations[gc_alloc_generation].alloc_unboxed_start_page =
905 generations[gc_alloc_generation].alloc_start_page = next_page-1;
907 /* Add the region to the new_areas if requested. */
909 add_new_area(first_page,orig_first_page_bytes_used, region_size);
913 "/gc_alloc_update_page_tables update %d bytes to gen %d\n",
915 gc_alloc_generation));
918 /* There are no bytes allocated. Unallocate the first_page if
919 * there are 0 bytes_used. */
920 page_table[first_page].allocated &= ~(OPEN_REGION_PAGE_FLAG);
921 if (page_table[first_page].bytes_used == 0)
922 page_table[first_page].allocated = FREE_PAGE_FLAG;
925 /* Unallocate any unused pages. */
926 while (next_page <= alloc_region->last_page) {
927 gc_assert(page_table[next_page].bytes_used == 0);
928 page_table[next_page].allocated = FREE_PAGE_FLAG;
931 ret = thread_mutex_unlock(&free_pages_lock);
934 /* alloc_region is per-thread, we're ok to do this unlocked */
935 gc_set_region_empty(alloc_region);
938 static inline void *gc_quick_alloc(long nbytes);
940 /* Allocate a possibly large object. */
942 gc_alloc_large(long nbytes, int unboxed, struct alloc_region *alloc_region)
944 page_index_t first_page;
945 page_index_t last_page;
946 int orig_first_page_bytes_used;
950 page_index_t next_page;
953 ret = thread_mutex_lock(&free_pages_lock);
958 generations[gc_alloc_generation].alloc_large_unboxed_start_page;
960 first_page = generations[gc_alloc_generation].alloc_large_start_page;
962 if (first_page <= alloc_region->last_page) {
963 first_page = alloc_region->last_page+1;
966 last_page=gc_find_freeish_pages(&first_page,nbytes,unboxed);
968 gc_assert(first_page > alloc_region->last_page);
970 generations[gc_alloc_generation].alloc_large_unboxed_start_page =
973 generations[gc_alloc_generation].alloc_large_start_page = last_page;
975 /* Set up the pages. */
976 orig_first_page_bytes_used = page_table[first_page].bytes_used;
978 /* If the first page was free then set up the gen, and
979 * first_object_offset. */
980 if (page_table[first_page].bytes_used == 0) {
982 page_table[first_page].allocated = UNBOXED_PAGE_FLAG;
984 page_table[first_page].allocated = BOXED_PAGE_FLAG;
985 page_table[first_page].gen = gc_alloc_generation;
986 page_table[first_page].first_object_offset = 0;
987 page_table[first_page].large_object = 1;
991 gc_assert(page_table[first_page].allocated == UNBOXED_PAGE_FLAG);
993 gc_assert(page_table[first_page].allocated == BOXED_PAGE_FLAG);
994 gc_assert(page_table[first_page].gen == gc_alloc_generation);
995 gc_assert(page_table[first_page].large_object == 1);
999 /* Calc. the number of bytes used in this page. This is not
1000 * always the number of new bytes, unless it was free. */
1002 if ((bytes_used = nbytes+orig_first_page_bytes_used) > PAGE_BYTES) {
1003 bytes_used = PAGE_BYTES;
1006 page_table[first_page].bytes_used = bytes_used;
1007 byte_cnt += bytes_used;
1009 next_page = first_page+1;
1011 /* All the rest of the pages should be free. We need to set their
1012 * first_object_offset pointer to the start of the region, and
1013 * set the bytes_used. */
1015 gc_assert(page_table[next_page].allocated == FREE_PAGE_FLAG);
1016 gc_assert(page_table[next_page].bytes_used == 0);
1018 page_table[next_page].allocated = UNBOXED_PAGE_FLAG;
1020 page_table[next_page].allocated = BOXED_PAGE_FLAG;
1021 page_table[next_page].gen = gc_alloc_generation;
1022 page_table[next_page].large_object = 1;
1024 page_table[next_page].first_object_offset =
1025 orig_first_page_bytes_used - PAGE_BYTES*(next_page-first_page);
1027 /* Calculate the number of bytes used in this page. */
1029 if ((bytes_used=(nbytes+orig_first_page_bytes_used)-byte_cnt) > PAGE_BYTES) {
1030 bytes_used = PAGE_BYTES;
1033 page_table[next_page].bytes_used = bytes_used;
1034 page_table[next_page].write_protected=0;
1035 page_table[next_page].dont_move=0;
1036 byte_cnt += bytes_used;
1040 gc_assert((byte_cnt-orig_first_page_bytes_used) == nbytes);
1042 bytes_allocated += nbytes;
1043 generations[gc_alloc_generation].bytes_allocated += nbytes;
1045 /* Add the region to the new_areas if requested. */
1047 add_new_area(first_page,orig_first_page_bytes_used,nbytes);
1049 /* Bump up last_free_page */
1050 if (last_page+1 > last_free_page) {
1051 last_free_page = last_page+1;
1052 set_alloc_pointer((lispobj)(((char *)heap_base) + last_free_page*PAGE_BYTES));
1054 ret = thread_mutex_unlock(&free_pages_lock);
1055 gc_assert(ret == 0);
1057 #ifdef READ_PROTECT_FREE_PAGES
1058 os_protect(page_address(first_page),
1059 PAGE_BYTES*(1+last_page-first_page),
1063 zero_dirty_pages(first_page, last_page);
1065 return page_address(first_page);
1068 static page_index_t gencgc_alloc_start_page = -1;
1071 gc_heap_exhausted_error_or_lose (long available, long requested)
1073 /* Write basic information before doing anything else: if we don't
1074 * call to lisp this is a must, and even if we do there is always
1075 * the danger that we bounce back here before the error has been
1076 * handled, or indeed even printed.
1078 fprintf(stderr, "Heap exhausted during %s: %ld bytes available, %ld requested.\n",
1079 gc_active_p ? "garbage collection" : "allocation", available, requested);
1080 if (gc_active_p || (available == 0)) {
1081 /* If we are in GC, or totally out of memory there is no way
1082 * to sanely transfer control to the lisp-side of things.
1084 struct thread *thread = arch_os_get_current_thread();
1085 print_generation_stats(1);
1086 fprintf(stderr, "GC control variables:\n");
1087 fprintf(stderr, " *GC-INHIBIT* = %s\n *GC-PENDING* = %s\n",
1088 SymbolValue(GC_INHIBIT,thread)==NIL ? "false" : "true",
1089 SymbolValue(GC_PENDING,thread)==NIL ? "false" : "true");
1090 #ifdef LISP_FEATURE_SB_THREAD
1091 fprintf(stderr, " *STOP-FOR-GC-PENDING* = %s\n",
1092 SymbolValue(STOP_FOR_GC_PENDING,thread)==NIL ? "false" : "true");
1094 lose("Heap exhausted, game over.");
1097 /* FIXME: assert free_pages_lock held */
1098 thread_mutex_unlock(&free_pages_lock);
1099 funcall2(SymbolFunction(HEAP_EXHAUSTED_ERROR),
1100 alloc_number(available), alloc_number(requested));
1101 lose("HEAP-EXHAUSTED-ERROR fell through");
1106 gc_find_freeish_pages(page_index_t *restart_page_ptr, long nbytes, int unboxed)
1108 page_index_t first_page;
1109 page_index_t last_page;
1111 page_index_t restart_page=*restart_page_ptr;
1114 int large_p=(nbytes>=large_object_size);
1115 /* FIXME: assert(free_pages_lock is held); */
1117 /* Search for a contiguous free space of at least nbytes. If it's
1118 * a large object then align it on a page boundary by searching
1119 * for a free page. */
1121 if (gencgc_alloc_start_page != -1) {
1122 restart_page = gencgc_alloc_start_page;
1126 first_page = restart_page;
1128 while ((first_page < page_table_pages)
1129 && (page_table[first_page].allocated != FREE_PAGE_FLAG))
1132 while (first_page < page_table_pages) {
1133 if(page_table[first_page].allocated == FREE_PAGE_FLAG)
1135 if((page_table[first_page].allocated ==
1136 (unboxed ? UNBOXED_PAGE_FLAG : BOXED_PAGE_FLAG)) &&
1137 (page_table[first_page].large_object == 0) &&
1138 (page_table[first_page].gen == gc_alloc_generation) &&
1139 (page_table[first_page].bytes_used < (PAGE_BYTES-32)) &&
1140 (page_table[first_page].write_protected == 0) &&
1141 (page_table[first_page].dont_move == 0)) {
1147 if (first_page >= page_table_pages)
1148 gc_heap_exhausted_error_or_lose(0, nbytes);
1150 gc_assert(page_table[first_page].write_protected == 0);
1152 last_page = first_page;
1153 bytes_found = PAGE_BYTES - page_table[first_page].bytes_used;
1155 while (((bytes_found < nbytes)
1156 || (!large_p && (num_pages < 2)))
1157 && (last_page < (page_table_pages-1))
1158 && (page_table[last_page+1].allocated == FREE_PAGE_FLAG)) {
1161 bytes_found += PAGE_BYTES;
1162 gc_assert(page_table[last_page].write_protected == 0);
1165 region_size = (PAGE_BYTES - page_table[first_page].bytes_used)
1166 + PAGE_BYTES*(last_page-first_page);
1168 gc_assert(bytes_found == region_size);
1169 restart_page = last_page + 1;
1170 } while ((restart_page < page_table_pages) && (bytes_found < nbytes));
1172 /* Check for a failure */
1173 if ((restart_page >= page_table_pages) && (bytes_found < nbytes))
1174 gc_heap_exhausted_error_or_lose(bytes_found, nbytes);
1176 *restart_page_ptr=first_page;
1181 /* Allocate bytes. All the rest of the special-purpose allocation
1182 * functions will eventually call this */
1185 gc_alloc_with_region(long nbytes,int unboxed_p, struct alloc_region *my_region,
1188 void *new_free_pointer;
1190 if(nbytes>=large_object_size)
1191 return gc_alloc_large(nbytes,unboxed_p,my_region);
1193 /* Check whether there is room in the current alloc region. */
1194 new_free_pointer = my_region->free_pointer + nbytes;
1196 /* fprintf(stderr, "alloc %d bytes from %p to %p\n", nbytes,
1197 my_region->free_pointer, new_free_pointer); */
1199 if (new_free_pointer <= my_region->end_addr) {
1200 /* If so then allocate from the current alloc region. */
1201 void *new_obj = my_region->free_pointer;
1202 my_region->free_pointer = new_free_pointer;
1204 /* Unless a `quick' alloc was requested, check whether the
1205 alloc region is almost empty. */
1207 (my_region->end_addr - my_region->free_pointer) <= 32) {
1208 /* If so, finished with the current region. */
1209 gc_alloc_update_page_tables(unboxed_p, my_region);
1210 /* Set up a new region. */
1211 gc_alloc_new_region(32 /*bytes*/, unboxed_p, my_region);
1214 return((void *)new_obj);
1217 /* Else not enough free space in the current region: retry with a
1220 gc_alloc_update_page_tables(unboxed_p, my_region);
1221 gc_alloc_new_region(nbytes, unboxed_p, my_region);
1222 return gc_alloc_with_region(nbytes,unboxed_p,my_region,0);
1225 /* these are only used during GC: all allocation from the mutator calls
1226 * alloc() -> gc_alloc_with_region() with the appropriate per-thread
1230 gc_general_alloc(long nbytes,int unboxed_p,int quick_p)
1232 struct alloc_region *my_region =
1233 unboxed_p ? &unboxed_region : &boxed_region;
1234 return gc_alloc_with_region(nbytes,unboxed_p, my_region,quick_p);
1237 static inline void *
1238 gc_quick_alloc(long nbytes)
1240 return gc_general_alloc(nbytes,ALLOC_BOXED,ALLOC_QUICK);
1243 static inline void *
1244 gc_quick_alloc_large(long nbytes)
1246 return gc_general_alloc(nbytes,ALLOC_BOXED,ALLOC_QUICK);
1249 static inline void *
1250 gc_alloc_unboxed(long nbytes)
1252 return gc_general_alloc(nbytes,ALLOC_UNBOXED,0);
1255 static inline void *
1256 gc_quick_alloc_unboxed(long nbytes)
1258 return gc_general_alloc(nbytes,ALLOC_UNBOXED,ALLOC_QUICK);
1261 static inline void *
1262 gc_quick_alloc_large_unboxed(long nbytes)
1264 return gc_general_alloc(nbytes,ALLOC_UNBOXED,ALLOC_QUICK);
1268 * scavenging/transporting routines derived from gc.c in CMU CL ca. 18b
1271 extern long (*scavtab[256])(lispobj *where, lispobj object);
1272 extern lispobj (*transother[256])(lispobj object);
1273 extern long (*sizetab[256])(lispobj *where);
1275 /* Copy a large boxed object. If the object is in a large object
1276 * region then it is simply promoted, else it is copied. If it's large
1277 * enough then it's copied to a large object region.
1279 * Vectors may have shrunk. If the object is not copied the space
1280 * needs to be reclaimed, and the page_tables corrected. */
1282 copy_large_object(lispobj object, long nwords)
1286 page_index_t first_page;
1288 gc_assert(is_lisp_pointer(object));
1289 gc_assert(from_space_p(object));
1290 gc_assert((nwords & 0x01) == 0);
1293 /* Check whether it's in a large object region. */
1294 first_page = find_page_index((void *)object);
1295 gc_assert(first_page >= 0);
1297 if (page_table[first_page].large_object) {
1299 /* Promote the object. */
1301 long remaining_bytes;
1302 page_index_t next_page;
1304 long old_bytes_used;
1306 /* Note: Any page write-protection must be removed, else a
1307 * later scavenge_newspace may incorrectly not scavenge these
1308 * pages. This would not be necessary if they are added to the
1309 * new areas, but let's do it for them all (they'll probably
1310 * be written anyway?). */
1312 gc_assert(page_table[first_page].first_object_offset == 0);
1314 next_page = first_page;
1315 remaining_bytes = nwords*N_WORD_BYTES;
1316 while (remaining_bytes > PAGE_BYTES) {
1317 gc_assert(page_table[next_page].gen == from_space);
1318 gc_assert(page_table[next_page].allocated == BOXED_PAGE_FLAG);
1319 gc_assert(page_table[next_page].large_object);
1320 gc_assert(page_table[next_page].first_object_offset==
1321 -PAGE_BYTES*(next_page-first_page));
1322 gc_assert(page_table[next_page].bytes_used == PAGE_BYTES);
1324 page_table[next_page].gen = new_space;
1326 /* Remove any write-protection. We should be able to rely
1327 * on the write-protect flag to avoid redundant calls. */
1328 if (page_table[next_page].write_protected) {
1329 os_protect(page_address(next_page), PAGE_BYTES, OS_VM_PROT_ALL);
1330 page_table[next_page].write_protected = 0;
1332 remaining_bytes -= PAGE_BYTES;
1336 /* Now only one page remains, but the object may have shrunk
1337 * so there may be more unused pages which will be freed. */
1339 /* The object may have shrunk but shouldn't have grown. */
1340 gc_assert(page_table[next_page].bytes_used >= remaining_bytes);
1342 page_table[next_page].gen = new_space;
1343 gc_assert(page_table[next_page].allocated == BOXED_PAGE_FLAG);
1345 /* Adjust the bytes_used. */
1346 old_bytes_used = page_table[next_page].bytes_used;
1347 page_table[next_page].bytes_used = remaining_bytes;
1349 bytes_freed = old_bytes_used - remaining_bytes;
1351 /* Free any remaining pages; needs care. */
1353 while ((old_bytes_used == PAGE_BYTES) &&
1354 (page_table[next_page].gen == from_space) &&
1355 (page_table[next_page].allocated == BOXED_PAGE_FLAG) &&
1356 page_table[next_page].large_object &&
1357 (page_table[next_page].first_object_offset ==
1358 -(next_page - first_page)*PAGE_BYTES)) {
1359 /* Checks out OK, free the page. Don't need to bother zeroing
1360 * pages as this should have been done before shrinking the
1361 * object. These pages shouldn't be write-protected as they
1362 * should be zero filled. */
1363 gc_assert(page_table[next_page].write_protected == 0);
1365 old_bytes_used = page_table[next_page].bytes_used;
1366 page_table[next_page].allocated = FREE_PAGE_FLAG;
1367 page_table[next_page].bytes_used = 0;
1368 bytes_freed += old_bytes_used;
1372 generations[from_space].bytes_allocated -= N_WORD_BYTES*nwords +
1374 generations[new_space].bytes_allocated += N_WORD_BYTES*nwords;
1375 bytes_allocated -= bytes_freed;
1377 /* Add the region to the new_areas if requested. */
1378 add_new_area(first_page,0,nwords*N_WORD_BYTES);
1382 /* Get tag of object. */
1383 tag = lowtag_of(object);
1385 /* Allocate space. */
1386 new = gc_quick_alloc_large(nwords*N_WORD_BYTES);
1388 memcpy(new,native_pointer(object),nwords*N_WORD_BYTES);
1390 /* Return Lisp pointer of new object. */
1391 return ((lispobj) new) | tag;
1395 /* to copy unboxed objects */
1397 copy_unboxed_object(lispobj object, long nwords)
1402 gc_assert(is_lisp_pointer(object));
1403 gc_assert(from_space_p(object));
1404 gc_assert((nwords & 0x01) == 0);
1406 /* Get tag of object. */
1407 tag = lowtag_of(object);
1409 /* Allocate space. */
1410 new = gc_quick_alloc_unboxed(nwords*N_WORD_BYTES);
1412 memcpy(new,native_pointer(object),nwords*N_WORD_BYTES);
1414 /* Return Lisp pointer of new object. */
1415 return ((lispobj) new) | tag;
1418 /* to copy large unboxed objects
1420 * If the object is in a large object region then it is simply
1421 * promoted, else it is copied. If it's large enough then it's copied
1422 * to a large object region.
1424 * Bignums and vectors may have shrunk. If the object is not copied
1425 * the space needs to be reclaimed, and the page_tables corrected.
1427 * KLUDGE: There's a lot of cut-and-paste duplication between this
1428 * function and copy_large_object(..). -- WHN 20000619 */
1430 copy_large_unboxed_object(lispobj object, long nwords)
1434 page_index_t first_page;
1436 gc_assert(is_lisp_pointer(object));
1437 gc_assert(from_space_p(object));
1438 gc_assert((nwords & 0x01) == 0);
1440 if ((nwords > 1024*1024) && gencgc_verbose)
1441 FSHOW((stderr, "/copy_large_unboxed_object: %d bytes\n", nwords*N_WORD_BYTES));
1443 /* Check whether it's a large object. */
1444 first_page = find_page_index((void *)object);
1445 gc_assert(first_page >= 0);
1447 if (page_table[first_page].large_object) {
1448 /* Promote the object. Note: Unboxed objects may have been
1449 * allocated to a BOXED region so it may be necessary to
1450 * change the region to UNBOXED. */
1451 long remaining_bytes;
1452 page_index_t next_page;
1454 long old_bytes_used;
1456 gc_assert(page_table[first_page].first_object_offset == 0);
1458 next_page = first_page;
1459 remaining_bytes = nwords*N_WORD_BYTES;
1460 while (remaining_bytes > PAGE_BYTES) {
1461 gc_assert(page_table[next_page].gen == from_space);
1462 gc_assert((page_table[next_page].allocated == UNBOXED_PAGE_FLAG)
1463 || (page_table[next_page].allocated == BOXED_PAGE_FLAG));
1464 gc_assert(page_table[next_page].large_object);
1465 gc_assert(page_table[next_page].first_object_offset==
1466 -PAGE_BYTES*(next_page-first_page));
1467 gc_assert(page_table[next_page].bytes_used == PAGE_BYTES);
1469 page_table[next_page].gen = new_space;
1470 page_table[next_page].allocated = UNBOXED_PAGE_FLAG;
1471 remaining_bytes -= PAGE_BYTES;
1475 /* Now only one page remains, but the object may have shrunk so
1476 * there may be more unused pages which will be freed. */
1478 /* Object may have shrunk but shouldn't have grown - check. */
1479 gc_assert(page_table[next_page].bytes_used >= remaining_bytes);
1481 page_table[next_page].gen = new_space;
1482 page_table[next_page].allocated = UNBOXED_PAGE_FLAG;
1484 /* Adjust the bytes_used. */
1485 old_bytes_used = page_table[next_page].bytes_used;
1486 page_table[next_page].bytes_used = remaining_bytes;
1488 bytes_freed = old_bytes_used - remaining_bytes;
1490 /* Free any remaining pages; needs care. */
1492 while ((old_bytes_used == PAGE_BYTES) &&
1493 (page_table[next_page].gen == from_space) &&
1494 ((page_table[next_page].allocated == UNBOXED_PAGE_FLAG)
1495 || (page_table[next_page].allocated == BOXED_PAGE_FLAG)) &&
1496 page_table[next_page].large_object &&
1497 (page_table[next_page].first_object_offset ==
1498 -(next_page - first_page)*PAGE_BYTES)) {
1499 /* Checks out OK, free the page. Don't need to both zeroing
1500 * pages as this should have been done before shrinking the
1501 * object. These pages shouldn't be write-protected, even if
1502 * boxed they should be zero filled. */
1503 gc_assert(page_table[next_page].write_protected == 0);
1505 old_bytes_used = page_table[next_page].bytes_used;
1506 page_table[next_page].allocated = FREE_PAGE_FLAG;
1507 page_table[next_page].bytes_used = 0;
1508 bytes_freed += old_bytes_used;
1512 if ((bytes_freed > 0) && gencgc_verbose)
1514 "/copy_large_unboxed bytes_freed=%d\n",
1517 generations[from_space].bytes_allocated -= nwords*N_WORD_BYTES + bytes_freed;
1518 generations[new_space].bytes_allocated += nwords*N_WORD_BYTES;
1519 bytes_allocated -= bytes_freed;
1524 /* Get tag of object. */
1525 tag = lowtag_of(object);
1527 /* Allocate space. */
1528 new = gc_quick_alloc_large_unboxed(nwords*N_WORD_BYTES);
1530 /* Copy the object. */
1531 memcpy(new,native_pointer(object),nwords*N_WORD_BYTES);
1533 /* Return Lisp pointer of new object. */
1534 return ((lispobj) new) | tag;
1543 * code and code-related objects
1546 static lispobj trans_fun_header(lispobj object);
1547 static lispobj trans_boxed(lispobj object);
1550 /* Scan a x86 compiled code object, looking for possible fixups that
1551 * have been missed after a move.
1553 * Two types of fixups are needed:
1554 * 1. Absolute fixups to within the code object.
1555 * 2. Relative fixups to outside the code object.
1557 * Currently only absolute fixups to the constant vector, or to the
1558 * code area are checked. */
1560 sniff_code_object(struct code *code, unsigned long displacement)
1562 #ifdef LISP_FEATURE_X86
1563 long nheader_words, ncode_words, nwords;
1565 void *constants_start_addr = NULL, *constants_end_addr;
1566 void *code_start_addr, *code_end_addr;
1567 int fixup_found = 0;
1569 if (!check_code_fixups)
1572 ncode_words = fixnum_value(code->code_size);
1573 nheader_words = HeaderValue(*(lispobj *)code);
1574 nwords = ncode_words + nheader_words;
1576 constants_start_addr = (void *)code + 5*N_WORD_BYTES;
1577 constants_end_addr = (void *)code + nheader_words*N_WORD_BYTES;
1578 code_start_addr = (void *)code + nheader_words*N_WORD_BYTES;
1579 code_end_addr = (void *)code + nwords*N_WORD_BYTES;
1581 /* Work through the unboxed code. */
1582 for (p = code_start_addr; p < code_end_addr; p++) {
1583 void *data = *(void **)p;
1584 unsigned d1 = *((unsigned char *)p - 1);
1585 unsigned d2 = *((unsigned char *)p - 2);
1586 unsigned d3 = *((unsigned char *)p - 3);
1587 unsigned d4 = *((unsigned char *)p - 4);
1589 unsigned d5 = *((unsigned char *)p - 5);
1590 unsigned d6 = *((unsigned char *)p - 6);
1593 /* Check for code references. */
1594 /* Check for a 32 bit word that looks like an absolute
1595 reference to within the code adea of the code object. */
1596 if ((data >= (code_start_addr-displacement))
1597 && (data < (code_end_addr-displacement))) {
1598 /* function header */
1600 && (((unsigned)p - 4 - 4*HeaderValue(*((unsigned *)p-1))) == (unsigned)code)) {
1601 /* Skip the function header */
1605 /* the case of PUSH imm32 */
1609 "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1610 p, d6, d5, d4, d3, d2, d1, data));
1611 FSHOW((stderr, "/PUSH $0x%.8x\n", data));
1613 /* the case of MOV [reg-8],imm32 */
1615 && (d2==0x40 || d2==0x41 || d2==0x42 || d2==0x43
1616 || d2==0x45 || d2==0x46 || d2==0x47)
1620 "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1621 p, d6, d5, d4, d3, d2, d1, data));
1622 FSHOW((stderr, "/MOV [reg-8],$0x%.8x\n", data));
1624 /* the case of LEA reg,[disp32] */
1625 if ((d2 == 0x8d) && ((d1 & 0xc7) == 5)) {
1628 "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1629 p, d6, d5, d4, d3, d2, d1, data));
1630 FSHOW((stderr,"/LEA reg,[$0x%.8x]\n", data));
1634 /* Check for constant references. */
1635 /* Check for a 32 bit word that looks like an absolute
1636 reference to within the constant vector. Constant references
1638 if ((data >= (constants_start_addr-displacement))
1639 && (data < (constants_end_addr-displacement))
1640 && (((unsigned)data & 0x3) == 0)) {
1645 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1646 p, d6, d5, d4, d3, d2, d1, data));
1647 FSHOW((stderr,"/MOV eax,0x%.8x\n", data));
1650 /* the case of MOV m32,EAX */
1654 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1655 p, d6, d5, d4, d3, d2, d1, data));
1656 FSHOW((stderr, "/MOV 0x%.8x,eax\n", data));
1659 /* the case of CMP m32,imm32 */
1660 if ((d1 == 0x3d) && (d2 == 0x81)) {
1663 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1664 p, d6, d5, d4, d3, d2, d1, data));
1666 FSHOW((stderr, "/CMP 0x%.8x,immed32\n", data));
1669 /* Check for a mod=00, r/m=101 byte. */
1670 if ((d1 & 0xc7) == 5) {
1675 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1676 p, d6, d5, d4, d3, d2, d1, data));
1677 FSHOW((stderr,"/CMP 0x%.8x,reg\n", data));
1679 /* the case of CMP reg32,m32 */
1683 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1684 p, d6, d5, d4, d3, d2, d1, data));
1685 FSHOW((stderr, "/CMP reg32,0x%.8x\n", data));
1687 /* the case of MOV m32,reg32 */
1691 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1692 p, d6, d5, d4, d3, d2, d1, data));
1693 FSHOW((stderr, "/MOV 0x%.8x,reg32\n", data));
1695 /* the case of MOV reg32,m32 */
1699 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1700 p, d6, d5, d4, d3, d2, d1, data));
1701 FSHOW((stderr, "/MOV reg32,0x%.8x\n", data));
1703 /* the case of LEA reg32,m32 */
1707 "abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1708 p, d6, d5, d4, d3, d2, d1, data));
1709 FSHOW((stderr, "/LEA reg32,0x%.8x\n", data));
1715 /* If anything was found, print some information on the code
1719 "/compiled code object at %x: header words = %d, code words = %d\n",
1720 code, nheader_words, ncode_words));
1722 "/const start = %x, end = %x\n",
1723 constants_start_addr, constants_end_addr));
1725 "/code start = %x, end = %x\n",
1726 code_start_addr, code_end_addr));
1732 gencgc_apply_code_fixups(struct code *old_code, struct code *new_code)
1734 /* x86-64 uses pc-relative addressing instead of this kludge */
1735 #ifndef LISP_FEATURE_X86_64
1736 long nheader_words, ncode_words, nwords;
1737 void *constants_start_addr, *constants_end_addr;
1738 void *code_start_addr, *code_end_addr;
1739 lispobj fixups = NIL;
1740 unsigned long displacement = (unsigned long)new_code - (unsigned long)old_code;
1741 struct vector *fixups_vector;
1743 ncode_words = fixnum_value(new_code->code_size);
1744 nheader_words = HeaderValue(*(lispobj *)new_code);
1745 nwords = ncode_words + nheader_words;
1747 "/compiled code object at %x: header words = %d, code words = %d\n",
1748 new_code, nheader_words, ncode_words)); */
1749 constants_start_addr = (void *)new_code + 5*N_WORD_BYTES;
1750 constants_end_addr = (void *)new_code + nheader_words*N_WORD_BYTES;
1751 code_start_addr = (void *)new_code + nheader_words*N_WORD_BYTES;
1752 code_end_addr = (void *)new_code + nwords*N_WORD_BYTES;
1755 "/const start = %x, end = %x\n",
1756 constants_start_addr,constants_end_addr));
1758 "/code start = %x; end = %x\n",
1759 code_start_addr,code_end_addr));
1762 /* The first constant should be a pointer to the fixups for this
1763 code objects. Check. */
1764 fixups = new_code->constants[0];
1766 /* It will be 0 or the unbound-marker if there are no fixups (as
1767 * will be the case if the code object has been purified, for
1768 * example) and will be an other pointer if it is valid. */
1769 if ((fixups == 0) || (fixups == UNBOUND_MARKER_WIDETAG) ||
1770 !is_lisp_pointer(fixups)) {
1771 /* Check for possible errors. */
1772 if (check_code_fixups)
1773 sniff_code_object(new_code, displacement);
1778 fixups_vector = (struct vector *)native_pointer(fixups);
1780 /* Could be pointing to a forwarding pointer. */
1781 /* FIXME is this always in from_space? if so, could replace this code with
1782 * forwarding_pointer_p/forwarding_pointer_value */
1783 if (is_lisp_pointer(fixups) &&
1784 (find_page_index((void*)fixups_vector) != -1) &&
1785 (fixups_vector->header == 0x01)) {
1786 /* If so, then follow it. */
1787 /*SHOW("following pointer to a forwarding pointer");*/
1788 fixups_vector = (struct vector *)native_pointer((lispobj)fixups_vector->length);
1791 /*SHOW("got fixups");*/
1793 if (widetag_of(fixups_vector->header) == SIMPLE_ARRAY_WORD_WIDETAG) {
1794 /* Got the fixups for the code block. Now work through the vector,
1795 and apply a fixup at each address. */
1796 long length = fixnum_value(fixups_vector->length);
1798 for (i = 0; i < length; i++) {
1799 unsigned long offset = fixups_vector->data[i];
1800 /* Now check the current value of offset. */
1801 unsigned long old_value =
1802 *(unsigned long *)((unsigned long)code_start_addr + offset);
1804 /* If it's within the old_code object then it must be an
1805 * absolute fixup (relative ones are not saved) */
1806 if ((old_value >= (unsigned long)old_code)
1807 && (old_value < ((unsigned long)old_code + nwords*N_WORD_BYTES)))
1808 /* So add the dispacement. */
1809 *(unsigned long *)((unsigned long)code_start_addr + offset) =
1810 old_value + displacement;
1812 /* It is outside the old code object so it must be a
1813 * relative fixup (absolute fixups are not saved). So
1814 * subtract the displacement. */
1815 *(unsigned long *)((unsigned long)code_start_addr + offset) =
1816 old_value - displacement;
1819 fprintf(stderr, "widetag of fixup vector is %d\n", widetag_of(fixups_vector->header));
1822 /* Check for possible errors. */
1823 if (check_code_fixups) {
1824 sniff_code_object(new_code,displacement);
1831 trans_boxed_large(lispobj object)
1834 unsigned long length;
1836 gc_assert(is_lisp_pointer(object));
1838 header = *((lispobj *) native_pointer(object));
1839 length = HeaderValue(header) + 1;
1840 length = CEILING(length, 2);
1842 return copy_large_object(object, length);
1845 /* Doesn't seem to be used, delete it after the grace period. */
1848 trans_unboxed_large(lispobj object)
1851 unsigned long length;
1853 gc_assert(is_lisp_pointer(object));
1855 header = *((lispobj *) native_pointer(object));
1856 length = HeaderValue(header) + 1;
1857 length = CEILING(length, 2);
1859 return copy_large_unboxed_object(object, length);
1865 * Lutexes. Using the normal finalization machinery for finalizing
1866 * lutexes is tricky, since the finalization depends on working lutexes.
1867 * So we track the lutexes in the GC and finalize them manually.
1870 #if defined(LUTEX_WIDETAG)
1873 * Start tracking LUTEX in the GC, by adding it to the linked list of
1874 * lutexes in the nursery generation. The caller is responsible for
1875 * locking, and GCs must be inhibited until the registration is
1879 gencgc_register_lutex (struct lutex *lutex) {
1880 int index = find_page_index(lutex);
1881 generation_index_t gen;
1884 /* This lutex is in static space, so we don't need to worry about
1890 gen = page_table[index].gen;
1892 gc_assert(gen >= 0);
1893 gc_assert(gen < NUM_GENERATIONS);
1895 head = generations[gen].lutexes;
1902 generations[gen].lutexes = lutex;
1906 * Stop tracking LUTEX in the GC by removing it from the appropriate
1907 * linked lists. This will only be called during GC, so no locking is
1911 gencgc_unregister_lutex (struct lutex *lutex) {
1913 lutex->prev->next = lutex->next;
1915 generations[lutex->gen].lutexes = lutex->next;
1919 lutex->next->prev = lutex->prev;
1928 * Mark all lutexes in generation GEN as not live.
1931 unmark_lutexes (generation_index_t gen) {
1932 struct lutex *lutex = generations[gen].lutexes;
1936 lutex = lutex->next;
1941 * Finalize all lutexes in generation GEN that have not been marked live.
1944 reap_lutexes (generation_index_t gen) {
1945 struct lutex *lutex = generations[gen].lutexes;
1948 struct lutex *next = lutex->next;
1950 lutex_destroy((tagged_lutex_t) lutex);
1951 gencgc_unregister_lutex(lutex);
1958 * Mark LUTEX as live.
1961 mark_lutex (lispobj tagged_lutex) {
1962 struct lutex *lutex = (struct lutex*) native_pointer(tagged_lutex);
1968 * Move all lutexes in generation FROM to generation TO.
1971 move_lutexes (generation_index_t from, generation_index_t to) {
1972 struct lutex *tail = generations[from].lutexes;
1974 /* Nothing to move */
1978 /* Change the generation of the lutexes in FROM. */
1979 while (tail->next) {
1985 /* Link the last lutex in the FROM list to the start of the TO list */
1986 tail->next = generations[to].lutexes;
1988 /* And vice versa */
1989 if (generations[to].lutexes) {
1990 generations[to].lutexes->prev = tail;
1993 /* And update the generations structures to match this */
1994 generations[to].lutexes = generations[from].lutexes;
1995 generations[from].lutexes = NULL;
1999 scav_lutex(lispobj *where, lispobj object)
2001 mark_lutex((lispobj) where);
2003 return CEILING(sizeof(struct lutex)/sizeof(lispobj), 2);
2007 trans_lutex(lispobj object)
2009 struct lutex *lutex = (struct lutex *) native_pointer(object);
2011 size_t words = CEILING(sizeof(struct lutex)/sizeof(lispobj), 2);
2012 gc_assert(is_lisp_pointer(object));
2013 copied = copy_object(object, words);
2015 /* Update the links, since the lutex moved in memory. */
2017 lutex->next->prev = (struct lutex *) native_pointer(copied);
2021 lutex->prev->next = (struct lutex *) native_pointer(copied);
2023 generations[lutex->gen].lutexes =
2024 (struct lutex *) native_pointer(copied);
2031 size_lutex(lispobj *where)
2033 return CEILING(sizeof(struct lutex)/sizeof(lispobj), 2);
2035 #endif /* LUTEX_WIDETAG */
2042 /* XX This is a hack adapted from cgc.c. These don't work too
2043 * efficiently with the gencgc as a list of the weak pointers is
2044 * maintained within the objects which causes writes to the pages. A
2045 * limited attempt is made to avoid unnecessary writes, but this needs
2047 #define WEAK_POINTER_NWORDS \
2048 CEILING((sizeof(struct weak_pointer) / sizeof(lispobj)), 2)
2051 scav_weak_pointer(lispobj *where, lispobj object)
2053 struct weak_pointer *wp = weak_pointers;
2054 /* Push the weak pointer onto the list of weak pointers.
2055 * Do I have to watch for duplicates? Originally this was
2056 * part of trans_weak_pointer but that didn't work in the
2057 * case where the WP was in a promoted region.
2060 /* Check whether it's already in the list. */
2061 while (wp != NULL) {
2062 if (wp == (struct weak_pointer*)where) {
2068 /* Add it to the start of the list. */
2069 wp = (struct weak_pointer*)where;
2070 if (wp->next != weak_pointers) {
2071 wp->next = weak_pointers;
2073 /*SHOW("avoided write to weak pointer");*/
2078 /* Do not let GC scavenge the value slot of the weak pointer.
2079 * (That is why it is a weak pointer.) */
2081 return WEAK_POINTER_NWORDS;
2086 search_read_only_space(void *pointer)
2088 lispobj *start = (lispobj *) READ_ONLY_SPACE_START;
2089 lispobj *end = (lispobj *) SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0);
2090 if ((pointer < (void *)start) || (pointer >= (void *)end))
2092 return (gc_search_space(start,
2093 (((lispobj *)pointer)+2)-start,
2094 (lispobj *) pointer));
2098 search_static_space(void *pointer)
2100 lispobj *start = (lispobj *)STATIC_SPACE_START;
2101 lispobj *end = (lispobj *)SymbolValue(STATIC_SPACE_FREE_POINTER,0);
2102 if ((pointer < (void *)start) || (pointer >= (void *)end))
2104 return (gc_search_space(start,
2105 (((lispobj *)pointer)+2)-start,
2106 (lispobj *) pointer));
2109 /* a faster version for searching the dynamic space. This will work even
2110 * if the object is in a current allocation region. */
2112 search_dynamic_space(void *pointer)
2114 page_index_t page_index = find_page_index(pointer);
2117 /* The address may be invalid, so do some checks. */
2118 if ((page_index == -1) ||
2119 (page_table[page_index].allocated == FREE_PAGE_FLAG))
2121 start = (lispobj *)((void *)page_address(page_index)
2122 + page_table[page_index].first_object_offset);
2123 return (gc_search_space(start,
2124 (((lispobj *)pointer)+2)-start,
2125 (lispobj *)pointer));
2128 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
2130 /* Helper for valid_lisp_pointer_p and
2131 * possibly_valid_dynamic_space_pointer.
2133 * pointer is the pointer to validate, and start_addr is the address
2134 * of the enclosing object.
2137 looks_like_valid_lisp_pointer_p(lispobj *pointer, lispobj *start_addr)
2139 /* We need to allow raw pointers into Code objects for return
2140 * addresses. This will also pick up pointers to functions in code
2142 if (widetag_of(*start_addr) == CODE_HEADER_WIDETAG)
2143 /* XXX could do some further checks here */
2146 if (!is_lisp_pointer((lispobj)pointer)) {
2150 /* Check that the object pointed to is consistent with the pointer
2152 switch (lowtag_of((lispobj)pointer)) {
2153 case FUN_POINTER_LOWTAG:
2154 /* Start_addr should be the enclosing code object, or a closure
2156 switch (widetag_of(*start_addr)) {
2157 case CODE_HEADER_WIDETAG:
2158 /* This case is probably caught above. */
2160 case CLOSURE_HEADER_WIDETAG:
2161 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
2162 if ((unsigned long)pointer !=
2163 ((unsigned long)start_addr+FUN_POINTER_LOWTAG)) {
2167 pointer, start_addr, *start_addr));
2175 pointer, start_addr, *start_addr));
2179 case LIST_POINTER_LOWTAG:
2180 if ((unsigned long)pointer !=
2181 ((unsigned long)start_addr+LIST_POINTER_LOWTAG)) {
2185 pointer, start_addr, *start_addr));
2188 /* Is it plausible cons? */
2189 if ((is_lisp_pointer(start_addr[0])
2190 || (fixnump(start_addr[0]))
2191 || (widetag_of(start_addr[0]) == CHARACTER_WIDETAG)
2192 #if N_WORD_BITS == 64
2193 || (widetag_of(start_addr[0]) == SINGLE_FLOAT_WIDETAG)
2195 || (widetag_of(start_addr[0]) == UNBOUND_MARKER_WIDETAG))
2196 && (is_lisp_pointer(start_addr[1])
2197 || (fixnump(start_addr[1]))
2198 || (widetag_of(start_addr[1]) == CHARACTER_WIDETAG)
2199 #if N_WORD_BITS == 64
2200 || (widetag_of(start_addr[1]) == SINGLE_FLOAT_WIDETAG)
2202 || (widetag_of(start_addr[1]) == UNBOUND_MARKER_WIDETAG)))
2208 pointer, start_addr, *start_addr));
2211 case INSTANCE_POINTER_LOWTAG:
2212 if ((unsigned long)pointer !=
2213 ((unsigned long)start_addr+INSTANCE_POINTER_LOWTAG)) {
2217 pointer, start_addr, *start_addr));
2220 if (widetag_of(start_addr[0]) != INSTANCE_HEADER_WIDETAG) {
2224 pointer, start_addr, *start_addr));
2228 case OTHER_POINTER_LOWTAG:
2229 if ((unsigned long)pointer !=
2230 ((unsigned long)start_addr+OTHER_POINTER_LOWTAG)) {
2234 pointer, start_addr, *start_addr));
2237 /* Is it plausible? Not a cons. XXX should check the headers. */
2238 if (is_lisp_pointer(start_addr[0]) || ((start_addr[0] & 3) == 0)) {
2242 pointer, start_addr, *start_addr));
2245 switch (widetag_of(start_addr[0])) {
2246 case UNBOUND_MARKER_WIDETAG:
2247 case NO_TLS_VALUE_MARKER_WIDETAG:
2248 case CHARACTER_WIDETAG:
2249 #if N_WORD_BITS == 64
2250 case SINGLE_FLOAT_WIDETAG:
2255 pointer, start_addr, *start_addr));
2258 /* only pointed to by function pointers? */
2259 case CLOSURE_HEADER_WIDETAG:
2260 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
2264 pointer, start_addr, *start_addr));
2267 case INSTANCE_HEADER_WIDETAG:
2271 pointer, start_addr, *start_addr));
2274 /* the valid other immediate pointer objects */
2275 case SIMPLE_VECTOR_WIDETAG:
2277 case COMPLEX_WIDETAG:
2278 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
2279 case COMPLEX_SINGLE_FLOAT_WIDETAG:
2281 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
2282 case COMPLEX_DOUBLE_FLOAT_WIDETAG:
2284 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
2285 case COMPLEX_LONG_FLOAT_WIDETAG:
2287 case SIMPLE_ARRAY_WIDETAG:
2288 case COMPLEX_BASE_STRING_WIDETAG:
2289 #ifdef COMPLEX_CHARACTER_STRING_WIDETAG
2290 case COMPLEX_CHARACTER_STRING_WIDETAG:
2292 case COMPLEX_VECTOR_NIL_WIDETAG:
2293 case COMPLEX_BIT_VECTOR_WIDETAG:
2294 case COMPLEX_VECTOR_WIDETAG:
2295 case COMPLEX_ARRAY_WIDETAG:
2296 case VALUE_CELL_HEADER_WIDETAG:
2297 case SYMBOL_HEADER_WIDETAG:
2299 case CODE_HEADER_WIDETAG:
2300 case BIGNUM_WIDETAG:
2301 #if N_WORD_BITS != 64
2302 case SINGLE_FLOAT_WIDETAG:
2304 case DOUBLE_FLOAT_WIDETAG:
2305 #ifdef LONG_FLOAT_WIDETAG
2306 case LONG_FLOAT_WIDETAG:
2308 case SIMPLE_BASE_STRING_WIDETAG:
2309 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
2310 case SIMPLE_CHARACTER_STRING_WIDETAG:
2312 case SIMPLE_BIT_VECTOR_WIDETAG:
2313 case SIMPLE_ARRAY_NIL_WIDETAG:
2314 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
2315 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
2316 case SIMPLE_ARRAY_UNSIGNED_BYTE_7_WIDETAG:
2317 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
2318 case SIMPLE_ARRAY_UNSIGNED_BYTE_15_WIDETAG:
2319 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
2320 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_29_WIDETAG
2321 case SIMPLE_ARRAY_UNSIGNED_BYTE_29_WIDETAG:
2323 case SIMPLE_ARRAY_UNSIGNED_BYTE_31_WIDETAG:
2324 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
2325 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_60_WIDETAG
2326 case SIMPLE_ARRAY_UNSIGNED_BYTE_60_WIDETAG:
2328 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG
2329 case SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG:
2331 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG
2332 case SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG:
2334 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
2335 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
2337 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
2338 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
2340 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
2341 case SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG:
2343 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
2344 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
2346 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_61_WIDETAG
2347 case SIMPLE_ARRAY_SIGNED_BYTE_61_WIDETAG:
2349 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG
2350 case SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG:
2352 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
2353 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
2354 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
2355 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
2357 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
2358 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
2360 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
2361 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
2363 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
2364 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
2367 case WEAK_POINTER_WIDETAG:
2368 #ifdef LUTEX_WIDETAG
2377 pointer, start_addr, *start_addr));
2385 pointer, start_addr, *start_addr));
2393 /* Used by the debugger to validate possibly bogus pointers before
2394 * calling MAKE-LISP-OBJ on them.
2396 * FIXME: We would like to make this perfect, because if the debugger
2397 * constructs a reference to a bugs lisp object, and it ends up in a
2398 * location scavenged by the GC all hell breaks loose.
2400 * Whereas possibly_valid_dynamic_space_pointer has to be conservative
2401 * and return true for all valid pointers, this could actually be eager
2402 * and lie about a few pointers without bad results... but that should
2403 * be reflected in the name.
2406 valid_lisp_pointer_p(lispobj *pointer)
2409 if (((start=search_dynamic_space(pointer))!=NULL) ||
2410 ((start=search_static_space(pointer))!=NULL) ||
2411 ((start=search_read_only_space(pointer))!=NULL))
2412 return looks_like_valid_lisp_pointer_p(pointer, start);
2417 /* Is there any possibility that pointer is a valid Lisp object
2418 * reference, and/or something else (e.g. subroutine call return
2419 * address) which should prevent us from moving the referred-to thing?
2420 * This is called from preserve_pointers() */
2422 possibly_valid_dynamic_space_pointer(lispobj *pointer)
2424 lispobj *start_addr;
2426 /* Find the object start address. */
2427 if ((start_addr = search_dynamic_space(pointer)) == NULL) {
2431 return looks_like_valid_lisp_pointer_p(pointer, start_addr);
2434 /* Adjust large bignum and vector objects. This will adjust the
2435 * allocated region if the size has shrunk, and move unboxed objects
2436 * into unboxed pages. The pages are not promoted here, and the
2437 * promoted region is not added to the new_regions; this is really
2438 * only designed to be called from preserve_pointer(). Shouldn't fail
2439 * if this is missed, just may delay the moving of objects to unboxed
2440 * pages, and the freeing of pages. */
2442 maybe_adjust_large_object(lispobj *where)
2444 page_index_t first_page;
2445 page_index_t next_page;
2448 long remaining_bytes;
2450 long old_bytes_used;
2454 /* Check whether it's a vector or bignum object. */
2455 switch (widetag_of(where[0])) {
2456 case SIMPLE_VECTOR_WIDETAG:
2457 boxed = BOXED_PAGE_FLAG;
2459 case BIGNUM_WIDETAG:
2460 case SIMPLE_BASE_STRING_WIDETAG:
2461 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
2462 case SIMPLE_CHARACTER_STRING_WIDETAG:
2464 case SIMPLE_BIT_VECTOR_WIDETAG:
2465 case SIMPLE_ARRAY_NIL_WIDETAG:
2466 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
2467 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
2468 case SIMPLE_ARRAY_UNSIGNED_BYTE_7_WIDETAG:
2469 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
2470 case SIMPLE_ARRAY_UNSIGNED_BYTE_15_WIDETAG:
2471 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
2472 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_29_WIDETAG
2473 case SIMPLE_ARRAY_UNSIGNED_BYTE_29_WIDETAG:
2475 case SIMPLE_ARRAY_UNSIGNED_BYTE_31_WIDETAG:
2476 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
2477 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_60_WIDETAG
2478 case SIMPLE_ARRAY_UNSIGNED_BYTE_60_WIDETAG:
2480 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG
2481 case SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG:
2483 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG
2484 case SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG:
2486 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
2487 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
2489 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
2490 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
2492 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
2493 case SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG:
2495 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
2496 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
2498 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_61_WIDETAG
2499 case SIMPLE_ARRAY_SIGNED_BYTE_61_WIDETAG:
2501 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG
2502 case SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG:
2504 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
2505 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
2506 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
2507 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
2509 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
2510 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
2512 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
2513 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
2515 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
2516 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
2518 boxed = UNBOXED_PAGE_FLAG;
2524 /* Find its current size. */
2525 nwords = (sizetab[widetag_of(where[0])])(where);
2527 first_page = find_page_index((void *)where);
2528 gc_assert(first_page >= 0);
2530 /* Note: Any page write-protection must be removed, else a later
2531 * scavenge_newspace may incorrectly not scavenge these pages.
2532 * This would not be necessary if they are added to the new areas,
2533 * but lets do it for them all (they'll probably be written
2536 gc_assert(page_table[first_page].first_object_offset == 0);
2538 next_page = first_page;
2539 remaining_bytes = nwords*N_WORD_BYTES;
2540 while (remaining_bytes > PAGE_BYTES) {
2541 gc_assert(page_table[next_page].gen == from_space);
2542 gc_assert((page_table[next_page].allocated == BOXED_PAGE_FLAG)
2543 || (page_table[next_page].allocated == UNBOXED_PAGE_FLAG));
2544 gc_assert(page_table[next_page].large_object);
2545 gc_assert(page_table[next_page].first_object_offset ==
2546 -PAGE_BYTES*(next_page-first_page));
2547 gc_assert(page_table[next_page].bytes_used == PAGE_BYTES);
2549 page_table[next_page].allocated = boxed;
2551 /* Shouldn't be write-protected at this stage. Essential that the
2553 gc_assert(!page_table[next_page].write_protected);
2554 remaining_bytes -= PAGE_BYTES;
2558 /* Now only one page remains, but the object may have shrunk so
2559 * there may be more unused pages which will be freed. */
2561 /* Object may have shrunk but shouldn't have grown - check. */
2562 gc_assert(page_table[next_page].bytes_used >= remaining_bytes);
2564 page_table[next_page].allocated = boxed;
2565 gc_assert(page_table[next_page].allocated ==
2566 page_table[first_page].allocated);
2568 /* Adjust the bytes_used. */
2569 old_bytes_used = page_table[next_page].bytes_used;
2570 page_table[next_page].bytes_used = remaining_bytes;
2572 bytes_freed = old_bytes_used - remaining_bytes;
2574 /* Free any remaining pages; needs care. */
2576 while ((old_bytes_used == PAGE_BYTES) &&
2577 (page_table[next_page].gen == from_space) &&
2578 ((page_table[next_page].allocated == UNBOXED_PAGE_FLAG)
2579 || (page_table[next_page].allocated == BOXED_PAGE_FLAG)) &&
2580 page_table[next_page].large_object &&
2581 (page_table[next_page].first_object_offset ==
2582 -(next_page - first_page)*PAGE_BYTES)) {
2583 /* It checks out OK, free the page. We don't need to both zeroing
2584 * pages as this should have been done before shrinking the
2585 * object. These pages shouldn't be write protected as they
2586 * should be zero filled. */
2587 gc_assert(page_table[next_page].write_protected == 0);
2589 old_bytes_used = page_table[next_page].bytes_used;
2590 page_table[next_page].allocated = FREE_PAGE_FLAG;
2591 page_table[next_page].bytes_used = 0;
2592 bytes_freed += old_bytes_used;
2596 if ((bytes_freed > 0) && gencgc_verbose) {
2598 "/maybe_adjust_large_object() freed %d\n",
2602 generations[from_space].bytes_allocated -= bytes_freed;
2603 bytes_allocated -= bytes_freed;
2608 /* Take a possible pointer to a Lisp object and mark its page in the
2609 * page_table so that it will not be relocated during a GC.
2611 * This involves locating the page it points to, then backing up to
2612 * the start of its region, then marking all pages dont_move from there
2613 * up to the first page that's not full or has a different generation
2615 * It is assumed that all the page static flags have been cleared at
2616 * the start of a GC.
2618 * It is also assumed that the current gc_alloc() region has been
2619 * flushed and the tables updated. */
2622 preserve_pointer(void *addr)
2624 page_index_t addr_page_index = find_page_index(addr);
2625 page_index_t first_page;
2627 unsigned int region_allocation;
2629 /* quick check 1: Address is quite likely to have been invalid. */
2630 if ((addr_page_index == -1)
2631 || (page_table[addr_page_index].allocated == FREE_PAGE_FLAG)
2632 || (page_table[addr_page_index].bytes_used == 0)
2633 || (page_table[addr_page_index].gen != from_space)
2634 /* Skip if already marked dont_move. */
2635 || (page_table[addr_page_index].dont_move != 0))
2637 gc_assert(!(page_table[addr_page_index].allocated&OPEN_REGION_PAGE_FLAG));
2638 /* (Now that we know that addr_page_index is in range, it's
2639 * safe to index into page_table[] with it.) */
2640 region_allocation = page_table[addr_page_index].allocated;
2642 /* quick check 2: Check the offset within the page.
2645 if (((unsigned long)addr & (PAGE_BYTES - 1)) > page_table[addr_page_index].bytes_used)
2648 /* Filter out anything which can't be a pointer to a Lisp object
2649 * (or, as a special case which also requires dont_move, a return
2650 * address referring to something in a CodeObject). This is
2651 * expensive but important, since it vastly reduces the
2652 * probability that random garbage will be bogusly interpreted as
2653 * a pointer which prevents a page from moving. */
2654 if (!(possibly_valid_dynamic_space_pointer(addr)))
2657 /* Find the beginning of the region. Note that there may be
2658 * objects in the region preceding the one that we were passed a
2659 * pointer to: if this is the case, we will write-protect all the
2660 * previous objects' pages too. */
2663 /* I think this'd work just as well, but without the assertions.
2664 * -dan 2004.01.01 */
2666 find_page_index(page_address(addr_page_index)+
2667 page_table[addr_page_index].first_object_offset);
2669 first_page = addr_page_index;
2670 while (page_table[first_page].first_object_offset != 0) {
2672 /* Do some checks. */
2673 gc_assert(page_table[first_page].bytes_used == PAGE_BYTES);
2674 gc_assert(page_table[first_page].gen == from_space);
2675 gc_assert(page_table[first_page].allocated == region_allocation);
2679 /* Adjust any large objects before promotion as they won't be
2680 * copied after promotion. */
2681 if (page_table[first_page].large_object) {
2682 maybe_adjust_large_object(page_address(first_page));
2683 /* If a large object has shrunk then addr may now point to a
2684 * free area in which case it's ignored here. Note it gets
2685 * through the valid pointer test above because the tail looks
2687 if ((page_table[addr_page_index].allocated == FREE_PAGE_FLAG)
2688 || (page_table[addr_page_index].bytes_used == 0)
2689 /* Check the offset within the page. */
2690 || (((unsigned long)addr & (PAGE_BYTES - 1))
2691 > page_table[addr_page_index].bytes_used)) {
2693 "weird? ignore ptr 0x%x to freed area of large object\n",
2697 /* It may have moved to unboxed pages. */
2698 region_allocation = page_table[first_page].allocated;
2701 /* Now work forward until the end of this contiguous area is found,
2702 * marking all pages as dont_move. */
2703 for (i = first_page; ;i++) {
2704 gc_assert(page_table[i].allocated == region_allocation);
2706 /* Mark the page static. */
2707 page_table[i].dont_move = 1;
2709 /* Move the page to the new_space. XX I'd rather not do this
2710 * but the GC logic is not quite able to copy with the static
2711 * pages remaining in the from space. This also requires the
2712 * generation bytes_allocated counters be updated. */
2713 page_table[i].gen = new_space;
2714 generations[new_space].bytes_allocated += page_table[i].bytes_used;
2715 generations[from_space].bytes_allocated -= page_table[i].bytes_used;
2717 /* It is essential that the pages are not write protected as
2718 * they may have pointers into the old-space which need
2719 * scavenging. They shouldn't be write protected at this
2721 gc_assert(!page_table[i].write_protected);
2723 /* Check whether this is the last page in this contiguous block.. */
2724 if ((page_table[i].bytes_used < PAGE_BYTES)
2725 /* ..or it is PAGE_BYTES and is the last in the block */
2726 || (page_table[i+1].allocated == FREE_PAGE_FLAG)
2727 || (page_table[i+1].bytes_used == 0) /* next page free */
2728 || (page_table[i+1].gen != from_space) /* diff. gen */
2729 || (page_table[i+1].first_object_offset == 0))
2733 /* Check that the page is now static. */
2734 gc_assert(page_table[addr_page_index].dont_move != 0);
2737 #endif // defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
2740 /* If the given page is not write-protected, then scan it for pointers
2741 * to younger generations or the top temp. generation, if no
2742 * suspicious pointers are found then the page is write-protected.
2744 * Care is taken to check for pointers to the current gc_alloc()
2745 * region if it is a younger generation or the temp. generation. This
2746 * frees the caller from doing a gc_alloc_update_page_tables(). Actually
2747 * the gc_alloc_generation does not need to be checked as this is only
2748 * called from scavenge_generation() when the gc_alloc generation is
2749 * younger, so it just checks if there is a pointer to the current
2752 * We return 1 if the page was write-protected, else 0. */
2754 update_page_write_prot(page_index_t page)
2756 generation_index_t gen = page_table[page].gen;
2759 void **page_addr = (void **)page_address(page);
2760 long num_words = page_table[page].bytes_used / N_WORD_BYTES;
2762 /* Shouldn't be a free page. */
2763 gc_assert(page_table[page].allocated != FREE_PAGE_FLAG);
2764 gc_assert(page_table[page].bytes_used != 0);
2766 /* Skip if it's already write-protected, pinned, or unboxed */
2767 if (page_table[page].write_protected
2768 /* FIXME: What's the reason for not write-protecting pinned pages? */
2769 || page_table[page].dont_move
2770 || (page_table[page].allocated & UNBOXED_PAGE_FLAG))
2773 /* Scan the page for pointers to younger generations or the
2774 * top temp. generation. */
2776 for (j = 0; j < num_words; j++) {
2777 void *ptr = *(page_addr+j);
2778 page_index_t index = find_page_index(ptr);
2780 /* Check that it's in the dynamic space */
2782 if (/* Does it point to a younger or the temp. generation? */
2783 ((page_table[index].allocated != FREE_PAGE_FLAG)
2784 && (page_table[index].bytes_used != 0)
2785 && ((page_table[index].gen < gen)
2786 || (page_table[index].gen == SCRATCH_GENERATION)))
2788 /* Or does it point within a current gc_alloc() region? */
2789 || ((boxed_region.start_addr <= ptr)
2790 && (ptr <= boxed_region.free_pointer))
2791 || ((unboxed_region.start_addr <= ptr)
2792 && (ptr <= unboxed_region.free_pointer))) {
2799 /* Write-protect the page. */
2800 /*FSHOW((stderr, "/write-protecting page %d gen %d\n", page, gen));*/
2802 os_protect((void *)page_addr,
2804 OS_VM_PROT_READ|OS_VM_PROT_EXECUTE);
2806 /* Note the page as protected in the page tables. */
2807 page_table[page].write_protected = 1;
2813 /* Scavenge all generations from FROM to TO, inclusive, except for
2814 * new_space which needs special handling, as new objects may be
2815 * added which are not checked here - use scavenge_newspace generation.
2817 * Write-protected pages should not have any pointers to the
2818 * from_space so do need scavenging; thus write-protected pages are
2819 * not always scavenged. There is some code to check that these pages
2820 * are not written; but to check fully the write-protected pages need
2821 * to be scavenged by disabling the code to skip them.
2823 * Under the current scheme when a generation is GCed the younger
2824 * generations will be empty. So, when a generation is being GCed it
2825 * is only necessary to scavenge the older generations for pointers
2826 * not the younger. So a page that does not have pointers to younger
2827 * generations does not need to be scavenged.
2829 * The write-protection can be used to note pages that don't have
2830 * pointers to younger pages. But pages can be written without having
2831 * pointers to younger generations. After the pages are scavenged here
2832 * they can be scanned for pointers to younger generations and if
2833 * there are none the page can be write-protected.
2835 * One complication is when the newspace is the top temp. generation.
2837 * Enabling SC_GEN_CK scavenges the write-protected pages and checks
2838 * that none were written, which they shouldn't be as they should have
2839 * no pointers to younger generations. This breaks down for weak
2840 * pointers as the objects contain a link to the next and are written
2841 * if a weak pointer is scavenged. Still it's a useful check. */
2843 scavenge_generations(generation_index_t from, generation_index_t to)
2850 /* Clear the write_protected_cleared flags on all pages. */
2851 for (i = 0; i < page_table_pages; i++)
2852 page_table[i].write_protected_cleared = 0;
2855 for (i = 0; i < last_free_page; i++) {
2856 generation_index_t generation = page_table[i].gen;
2857 if ((page_table[i].allocated & BOXED_PAGE_FLAG)
2858 && (page_table[i].bytes_used != 0)
2859 && (generation != new_space)
2860 && (generation >= from)
2861 && (generation <= to)) {
2862 page_index_t last_page,j;
2863 int write_protected=1;
2865 /* This should be the start of a region */
2866 gc_assert(page_table[i].first_object_offset == 0);
2868 /* Now work forward until the end of the region */
2869 for (last_page = i; ; last_page++) {
2871 write_protected && page_table[last_page].write_protected;
2872 if ((page_table[last_page].bytes_used < PAGE_BYTES)
2873 /* Or it is PAGE_BYTES and is the last in the block */
2874 || (!(page_table[last_page+1].allocated & BOXED_PAGE_FLAG))
2875 || (page_table[last_page+1].bytes_used == 0)
2876 || (page_table[last_page+1].gen != generation)
2877 || (page_table[last_page+1].first_object_offset == 0))
2880 if (!write_protected) {
2881 scavenge(page_address(i),
2882 (page_table[last_page].bytes_used +
2883 (last_page-i)*PAGE_BYTES)/N_WORD_BYTES);
2885 /* Now scan the pages and write protect those that
2886 * don't have pointers to younger generations. */
2887 if (enable_page_protection) {
2888 for (j = i; j <= last_page; j++) {
2889 num_wp += update_page_write_prot(j);
2892 if ((gencgc_verbose > 1) && (num_wp != 0)) {
2894 "/write protected %d pages within generation %d\n",
2895 num_wp, generation));
2903 /* Check that none of the write_protected pages in this generation
2904 * have been written to. */
2905 for (i = 0; i < page_table_pages; i++) {
2906 if ((page_table[i].allocation != FREE_PAGE_FLAG)
2907 && (page_table[i].bytes_used != 0)
2908 && (page_table[i].gen == generation)
2909 && (page_table[i].write_protected_cleared != 0)) {
2910 FSHOW((stderr, "/scavenge_generation() %d\n", generation));
2912 "/page bytes_used=%d first_object_offset=%d dont_move=%d\n",
2913 page_table[i].bytes_used,
2914 page_table[i].first_object_offset,
2915 page_table[i].dont_move));
2916 lose("write to protected page %d in scavenge_generation()\n", i);
2923 /* Scavenge a newspace generation. As it is scavenged new objects may
2924 * be allocated to it; these will also need to be scavenged. This
2925 * repeats until there are no more objects unscavenged in the
2926 * newspace generation.
2928 * To help improve the efficiency, areas written are recorded by
2929 * gc_alloc() and only these scavenged. Sometimes a little more will be
2930 * scavenged, but this causes no harm. An easy check is done that the
2931 * scavenged bytes equals the number allocated in the previous
2934 * Write-protected pages are not scanned except if they are marked
2935 * dont_move in which case they may have been promoted and still have
2936 * pointers to the from space.
2938 * Write-protected pages could potentially be written by alloc however
2939 * to avoid having to handle re-scavenging of write-protected pages
2940 * gc_alloc() does not write to write-protected pages.
2942 * New areas of objects allocated are recorded alternatively in the two
2943 * new_areas arrays below. */
2944 static struct new_area new_areas_1[NUM_NEW_AREAS];
2945 static struct new_area new_areas_2[NUM_NEW_AREAS];
2947 /* Do one full scan of the new space generation. This is not enough to
2948 * complete the job as new objects may be added to the generation in
2949 * the process which are not scavenged. */
2951 scavenge_newspace_generation_one_scan(generation_index_t generation)
2956 "/starting one full scan of newspace generation %d\n",
2958 for (i = 0; i < last_free_page; i++) {
2959 /* Note that this skips over open regions when it encounters them. */
2960 if ((page_table[i].allocated & BOXED_PAGE_FLAG)
2961 && (page_table[i].bytes_used != 0)
2962 && (page_table[i].gen == generation)
2963 && ((page_table[i].write_protected == 0)
2964 /* (This may be redundant as write_protected is now
2965 * cleared before promotion.) */
2966 || (page_table[i].dont_move == 1))) {
2967 page_index_t last_page;
2970 /* The scavenge will start at the first_object_offset of page i.
2972 * We need to find the full extent of this contiguous
2973 * block in case objects span pages.
2975 * Now work forward until the end of this contiguous area
2976 * is found. A small area is preferred as there is a
2977 * better chance of its pages being write-protected. */
2978 for (last_page = i; ;last_page++) {
2979 /* If all pages are write-protected and movable,
2980 * then no need to scavenge */
2981 all_wp=all_wp && page_table[last_page].write_protected &&
2982 !page_table[last_page].dont_move;
2984 /* Check whether this is the last page in this
2985 * contiguous block */
2986 if ((page_table[last_page].bytes_used < PAGE_BYTES)
2987 /* Or it is PAGE_BYTES and is the last in the block */
2988 || (!(page_table[last_page+1].allocated & BOXED_PAGE_FLAG))
2989 || (page_table[last_page+1].bytes_used == 0)
2990 || (page_table[last_page+1].gen != generation)
2991 || (page_table[last_page+1].first_object_offset == 0))
2995 /* Do a limited check for write-protected pages. */
2999 size = (page_table[last_page].bytes_used
3000 + (last_page-i)*PAGE_BYTES
3001 - page_table[i].first_object_offset)/N_WORD_BYTES;
3002 new_areas_ignore_page = last_page;
3004 scavenge(page_address(i) +
3005 page_table[i].first_object_offset,
3013 "/done with one full scan of newspace generation %d\n",
3017 /* Do a complete scavenge of the newspace generation. */
3019 scavenge_newspace_generation(generation_index_t generation)
3023 /* the new_areas array currently being written to by gc_alloc() */
3024 struct new_area (*current_new_areas)[] = &new_areas_1;
3025 long current_new_areas_index;
3027 /* the new_areas created by the previous scavenge cycle */
3028 struct new_area (*previous_new_areas)[] = NULL;
3029 long previous_new_areas_index;
3031 /* Flush the current regions updating the tables. */
3032 gc_alloc_update_all_page_tables();
3034 /* Turn on the recording of new areas by gc_alloc(). */
3035 new_areas = current_new_areas;
3036 new_areas_index = 0;
3038 /* Don't need to record new areas that get scavenged anyway during
3039 * scavenge_newspace_generation_one_scan. */
3040 record_new_objects = 1;
3042 /* Start with a full scavenge. */
3043 scavenge_newspace_generation_one_scan(generation);
3045 /* Record all new areas now. */
3046 record_new_objects = 2;
3048 /* Give a chance to weak hash tables to make other objects live.
3049 * FIXME: The algorithm implemented here for weak hash table gcing
3050 * is O(W^2+N) as Bruno Haible warns in
3051 * http://www.haible.de/bruno/papers/cs/weak/WeakDatastructures-writeup.html
3052 * see "Implementation 2". */
3053 scav_weak_hash_tables();
3055 /* Flush the current regions updating the tables. */
3056 gc_alloc_update_all_page_tables();
3058 /* Grab new_areas_index. */
3059 current_new_areas_index = new_areas_index;
3062 "The first scan is finished; current_new_areas_index=%d.\n",
3063 current_new_areas_index));*/
3065 while (current_new_areas_index > 0) {
3066 /* Move the current to the previous new areas */
3067 previous_new_areas = current_new_areas;
3068 previous_new_areas_index = current_new_areas_index;
3070 /* Scavenge all the areas in previous new areas. Any new areas
3071 * allocated are saved in current_new_areas. */
3073 /* Allocate an array for current_new_areas; alternating between
3074 * new_areas_1 and 2 */
3075 if (previous_new_areas == &new_areas_1)
3076 current_new_areas = &new_areas_2;
3078 current_new_areas = &new_areas_1;
3080 /* Set up for gc_alloc(). */
3081 new_areas = current_new_areas;
3082 new_areas_index = 0;
3084 /* Check whether previous_new_areas had overflowed. */
3085 if (previous_new_areas_index >= NUM_NEW_AREAS) {
3087 /* New areas of objects allocated have been lost so need to do a
3088 * full scan to be sure! If this becomes a problem try
3089 * increasing NUM_NEW_AREAS. */
3091 SHOW("new_areas overflow, doing full scavenge");
3093 /* Don't need to record new areas that get scavenged
3094 * anyway during scavenge_newspace_generation_one_scan. */
3095 record_new_objects = 1;
3097 scavenge_newspace_generation_one_scan(generation);
3099 /* Record all new areas now. */
3100 record_new_objects = 2;
3102 scav_weak_hash_tables();
3104 /* Flush the current regions updating the tables. */
3105 gc_alloc_update_all_page_tables();
3109 /* Work through previous_new_areas. */
3110 for (i = 0; i < previous_new_areas_index; i++) {
3111 long page = (*previous_new_areas)[i].page;
3112 long offset = (*previous_new_areas)[i].offset;
3113 long size = (*previous_new_areas)[i].size / N_WORD_BYTES;
3114 gc_assert((*previous_new_areas)[i].size % N_WORD_BYTES == 0);
3115 scavenge(page_address(page)+offset, size);
3118 scav_weak_hash_tables();
3120 /* Flush the current regions updating the tables. */
3121 gc_alloc_update_all_page_tables();
3124 current_new_areas_index = new_areas_index;
3127 "The re-scan has finished; current_new_areas_index=%d.\n",
3128 current_new_areas_index));*/
3131 /* Turn off recording of areas allocated by gc_alloc(). */
3132 record_new_objects = 0;
3135 /* Check that none of the write_protected pages in this generation
3136 * have been written to. */
3137 for (i = 0; i < page_table_pages; i++) {
3138 if ((page_table[i].allocation != FREE_PAGE_FLAG)
3139 && (page_table[i].bytes_used != 0)
3140 && (page_table[i].gen == generation)
3141 && (page_table[i].write_protected_cleared != 0)
3142 && (page_table[i].dont_move == 0)) {
3143 lose("write protected page %d written to in scavenge_newspace_generation\ngeneration=%d dont_move=%d\n",
3144 i, generation, page_table[i].dont_move);
3150 /* Un-write-protect all the pages in from_space. This is done at the
3151 * start of a GC else there may be many page faults while scavenging
3152 * the newspace (I've seen drive the system time to 99%). These pages
3153 * would need to be unprotected anyway before unmapping in
3154 * free_oldspace; not sure what effect this has on paging.. */
3156 unprotect_oldspace(void)
3160 for (i = 0; i < last_free_page; i++) {
3161 if ((page_table[i].allocated != FREE_PAGE_FLAG)
3162 && (page_table[i].bytes_used != 0)
3163 && (page_table[i].gen == from_space)) {
3166 page_start = (void *)page_address(i);
3168 /* Remove any write-protection. We should be able to rely
3169 * on the write-protect flag to avoid redundant calls. */
3170 if (page_table[i].write_protected) {
3171 os_protect(page_start, PAGE_BYTES, OS_VM_PROT_ALL);
3172 page_table[i].write_protected = 0;
3178 /* Work through all the pages and free any in from_space. This
3179 * assumes that all objects have been copied or promoted to an older
3180 * generation. Bytes_allocated and the generation bytes_allocated
3181 * counter are updated. The number of bytes freed is returned. */
3185 long bytes_freed = 0;
3186 page_index_t first_page, last_page;
3191 /* Find a first page for the next region of pages. */
3192 while ((first_page < last_free_page)
3193 && ((page_table[first_page].allocated == FREE_PAGE_FLAG)
3194 || (page_table[first_page].bytes_used == 0)
3195 || (page_table[first_page].gen != from_space)))
3198 if (first_page >= last_free_page)
3201 /* Find the last page of this region. */
3202 last_page = first_page;
3205 /* Free the page. */
3206 bytes_freed += page_table[last_page].bytes_used;
3207 generations[page_table[last_page].gen].bytes_allocated -=
3208 page_table[last_page].bytes_used;
3209 page_table[last_page].allocated = FREE_PAGE_FLAG;
3210 page_table[last_page].bytes_used = 0;
3212 /* Remove any write-protection. We should be able to rely
3213 * on the write-protect flag to avoid redundant calls. */
3215 void *page_start = (void *)page_address(last_page);
3217 if (page_table[last_page].write_protected) {
3218 os_protect(page_start, PAGE_BYTES, OS_VM_PROT_ALL);
3219 page_table[last_page].write_protected = 0;
3224 while ((last_page < last_free_page)
3225 && (page_table[last_page].allocated != FREE_PAGE_FLAG)
3226 && (page_table[last_page].bytes_used != 0)
3227 && (page_table[last_page].gen == from_space));
3229 #ifdef READ_PROTECT_FREE_PAGES
3230 os_protect(page_address(first_page),
3231 PAGE_BYTES*(last_page-first_page),
3234 first_page = last_page;
3235 } while (first_page < last_free_page);
3237 bytes_allocated -= bytes_freed;
3242 /* Print some information about a pointer at the given address. */
3244 print_ptr(lispobj *addr)
3246 /* If addr is in the dynamic space then out the page information. */
3247 page_index_t pi1 = find_page_index((void*)addr);
3250 fprintf(stderr," %x: page %d alloc %d gen %d bytes_used %d offset %d dont_move %d\n",
3251 (unsigned long) addr,
3253 page_table[pi1].allocated,
3254 page_table[pi1].gen,
3255 page_table[pi1].bytes_used,
3256 page_table[pi1].first_object_offset,
3257 page_table[pi1].dont_move);
3258 fprintf(stderr," %x %x %x %x (%x) %x %x %x %x\n",
3272 verify_space(lispobj *start, size_t words)
3274 int is_in_dynamic_space = (find_page_index((void*)start) != -1);
3275 int is_in_readonly_space =
3276 (READ_ONLY_SPACE_START <= (unsigned long)start &&
3277 (unsigned long)start < SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0));
3281 lispobj thing = *(lispobj*)start;
3283 if (is_lisp_pointer(thing)) {
3284 page_index_t page_index = find_page_index((void*)thing);
3285 long to_readonly_space =
3286 (READ_ONLY_SPACE_START <= thing &&
3287 thing < SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0));
3288 long to_static_space =
3289 (STATIC_SPACE_START <= thing &&
3290 thing < SymbolValue(STATIC_SPACE_FREE_POINTER,0));
3292 /* Does it point to the dynamic space? */
3293 if (page_index != -1) {
3294 /* If it's within the dynamic space it should point to a used
3295 * page. XX Could check the offset too. */
3296 if ((page_table[page_index].allocated != FREE_PAGE_FLAG)
3297 && (page_table[page_index].bytes_used == 0))
3298 lose ("Ptr %x @ %x sees free page.\n", thing, start);
3299 /* Check that it doesn't point to a forwarding pointer! */
3300 if (*((lispobj *)native_pointer(thing)) == 0x01) {
3301 lose("Ptr %x @ %x sees forwarding ptr.\n", thing, start);
3303 /* Check that its not in the RO space as it would then be a
3304 * pointer from the RO to the dynamic space. */
3305 if (is_in_readonly_space) {
3306 lose("ptr to dynamic space %x from RO space %x\n",
3309 /* Does it point to a plausible object? This check slows
3310 * it down a lot (so it's commented out).
3312 * "a lot" is serious: it ate 50 minutes cpu time on
3313 * my duron 950 before I came back from lunch and
3316 * FIXME: Add a variable to enable this
3319 if (!possibly_valid_dynamic_space_pointer((lispobj *)thing)) {
3320 lose("ptr %x to invalid object %x\n", thing, start);
3324 /* Verify that it points to another valid space. */
3325 if (!to_readonly_space && !to_static_space) {
3326 lose("Ptr %x @ %x sees junk.\n", thing, start);
3330 if (!(fixnump(thing))) {
3332 switch(widetag_of(*start)) {
3335 case SIMPLE_VECTOR_WIDETAG:
3337 case COMPLEX_WIDETAG:
3338 case SIMPLE_ARRAY_WIDETAG:
3339 case COMPLEX_BASE_STRING_WIDETAG:
3340 #ifdef COMPLEX_CHARACTER_STRING_WIDETAG
3341 case COMPLEX_CHARACTER_STRING_WIDETAG:
3343 case COMPLEX_VECTOR_NIL_WIDETAG:
3344 case COMPLEX_BIT_VECTOR_WIDETAG:
3345 case COMPLEX_VECTOR_WIDETAG:
3346 case COMPLEX_ARRAY_WIDETAG:
3347 case CLOSURE_HEADER_WIDETAG:
3348 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
3349 case VALUE_CELL_HEADER_WIDETAG:
3350 case SYMBOL_HEADER_WIDETAG:
3351 case CHARACTER_WIDETAG:
3352 #if N_WORD_BITS == 64
3353 case SINGLE_FLOAT_WIDETAG:
3355 case UNBOUND_MARKER_WIDETAG:
3360 case INSTANCE_HEADER_WIDETAG:
3363 long ntotal = HeaderValue(thing);
3364 lispobj layout = ((struct instance *)start)->slots[0];
3369 nuntagged = ((struct layout *)native_pointer(layout))->n_untagged_slots;
3370 verify_space(start + 1, ntotal - fixnum_value(nuntagged));
3374 case CODE_HEADER_WIDETAG:
3376 lispobj object = *start;
3378 long nheader_words, ncode_words, nwords;
3380 struct simple_fun *fheaderp;
3382 code = (struct code *) start;
3384 /* Check that it's not in the dynamic space.
3385 * FIXME: Isn't is supposed to be OK for code
3386 * objects to be in the dynamic space these days? */
3387 if (is_in_dynamic_space
3388 /* It's ok if it's byte compiled code. The trace
3389 * table offset will be a fixnum if it's x86
3390 * compiled code - check.
3392 * FIXME: #^#@@! lack of abstraction here..
3393 * This line can probably go away now that
3394 * there's no byte compiler, but I've got
3395 * too much to worry about right now to try
3396 * to make sure. -- WHN 2001-10-06 */
3397 && fixnump(code->trace_table_offset)
3398 /* Only when enabled */
3399 && verify_dynamic_code_check) {
3401 "/code object at %x in the dynamic space\n",
3405 ncode_words = fixnum_value(code->code_size);
3406 nheader_words = HeaderValue(object);
3407 nwords = ncode_words + nheader_words;
3408 nwords = CEILING(nwords, 2);
3409 /* Scavenge the boxed section of the code data block */
3410 verify_space(start + 1, nheader_words - 1);
3412 /* Scavenge the boxed section of each function
3413 * object in the code data block. */
3414 fheaderl = code->entry_points;
3415 while (fheaderl != NIL) {
3417 (struct simple_fun *) native_pointer(fheaderl);
3418 gc_assert(widetag_of(fheaderp->header) == SIMPLE_FUN_HEADER_WIDETAG);
3419 verify_space(&fheaderp->name, 1);
3420 verify_space(&fheaderp->arglist, 1);
3421 verify_space(&fheaderp->type, 1);
3422 fheaderl = fheaderp->next;
3428 /* unboxed objects */
3429 case BIGNUM_WIDETAG:
3430 #if N_WORD_BITS != 64
3431 case SINGLE_FLOAT_WIDETAG:
3433 case DOUBLE_FLOAT_WIDETAG:
3434 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
3435 case LONG_FLOAT_WIDETAG:
3437 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
3438 case COMPLEX_SINGLE_FLOAT_WIDETAG:
3440 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
3441 case COMPLEX_DOUBLE_FLOAT_WIDETAG:
3443 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
3444 case COMPLEX_LONG_FLOAT_WIDETAG:
3446 case SIMPLE_BASE_STRING_WIDETAG:
3447 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
3448 case SIMPLE_CHARACTER_STRING_WIDETAG:
3450 case SIMPLE_BIT_VECTOR_WIDETAG:
3451 case SIMPLE_ARRAY_NIL_WIDETAG:
3452 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
3453 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
3454 case SIMPLE_ARRAY_UNSIGNED_BYTE_7_WIDETAG:
3455 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
3456 case SIMPLE_ARRAY_UNSIGNED_BYTE_15_WIDETAG:
3457 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
3458 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_29_WIDETAG
3459 case SIMPLE_ARRAY_UNSIGNED_BYTE_29_WIDETAG:
3461 case SIMPLE_ARRAY_UNSIGNED_BYTE_31_WIDETAG:
3462 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
3463 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_60_WIDETAG
3464 case SIMPLE_ARRAY_UNSIGNED_BYTE_60_WIDETAG:
3466 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG
3467 case SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG:
3469 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG
3470 case SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG:
3472 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
3473 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
3475 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
3476 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
3478 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
3479 case SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG:
3481 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
3482 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
3484 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_61_WIDETAG
3485 case SIMPLE_ARRAY_SIGNED_BYTE_61_WIDETAG:
3487 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG
3488 case SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG:
3490 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
3491 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
3492 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
3493 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
3495 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
3496 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
3498 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
3499 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
3501 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
3502 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
3505 case WEAK_POINTER_WIDETAG:
3506 #ifdef LUTEX_WIDETAG
3509 count = (sizetab[widetag_of(*start)])(start);
3514 "/Unhandled widetag 0x%x at 0x%x\n",
3515 widetag_of(*start), start));
3529 /* FIXME: It would be nice to make names consistent so that
3530 * foo_size meant size *in* *bytes* instead of size in some
3531 * arbitrary units. (Yes, this caused a bug, how did you guess?:-)
3532 * Some counts of lispobjs are called foo_count; it might be good
3533 * to grep for all foo_size and rename the appropriate ones to
3535 long read_only_space_size =
3536 (lispobj*)SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0)
3537 - (lispobj*)READ_ONLY_SPACE_START;
3538 long static_space_size =
3539 (lispobj*)SymbolValue(STATIC_SPACE_FREE_POINTER,0)
3540 - (lispobj*)STATIC_SPACE_START;
3542 for_each_thread(th) {
3543 long binding_stack_size =
3544 (lispobj*)get_binding_stack_pointer(th)
3545 - (lispobj*)th->binding_stack_start;
3546 verify_space(th->binding_stack_start, binding_stack_size);
3548 verify_space((lispobj*)READ_ONLY_SPACE_START, read_only_space_size);
3549 verify_space((lispobj*)STATIC_SPACE_START , static_space_size);
3553 verify_generation(generation_index_t generation)
3557 for (i = 0; i < last_free_page; i++) {
3558 if ((page_table[i].allocated != FREE_PAGE_FLAG)
3559 && (page_table[i].bytes_used != 0)
3560 && (page_table[i].gen == generation)) {
3561 page_index_t last_page;
3562 int region_allocation = page_table[i].allocated;
3564 /* This should be the start of a contiguous block */
3565 gc_assert(page_table[i].first_object_offset == 0);
3567 /* Need to find the full extent of this contiguous block in case
3568 objects span pages. */
3570 /* Now work forward until the end of this contiguous area is
3572 for (last_page = i; ;last_page++)
3573 /* Check whether this is the last page in this contiguous
3575 if ((page_table[last_page].bytes_used < PAGE_BYTES)
3576 /* Or it is PAGE_BYTES and is the last in the block */
3577 || (page_table[last_page+1].allocated != region_allocation)
3578 || (page_table[last_page+1].bytes_used == 0)
3579 || (page_table[last_page+1].gen != generation)
3580 || (page_table[last_page+1].first_object_offset == 0))
3583 verify_space(page_address(i), (page_table[last_page].bytes_used
3584 + (last_page-i)*PAGE_BYTES)/N_WORD_BYTES);
3590 /* Check that all the free space is zero filled. */
3592 verify_zero_fill(void)
3596 for (page = 0; page < last_free_page; page++) {
3597 if (page_table[page].allocated == FREE_PAGE_FLAG) {
3598 /* The whole page should be zero filled. */
3599 long *start_addr = (long *)page_address(page);
3602 for (i = 0; i < size; i++) {
3603 if (start_addr[i] != 0) {
3604 lose("free page not zero at %x\n", start_addr + i);
3608 long free_bytes = PAGE_BYTES - page_table[page].bytes_used;
3609 if (free_bytes > 0) {
3610 long *start_addr = (long *)((unsigned long)page_address(page)
3611 + page_table[page].bytes_used);
3612 long size = free_bytes / N_WORD_BYTES;
3614 for (i = 0; i < size; i++) {
3615 if (start_addr[i] != 0) {
3616 lose("free region not zero at %x\n", start_addr + i);
3624 /* External entry point for verify_zero_fill */
3626 gencgc_verify_zero_fill(void)
3628 /* Flush the alloc regions updating the tables. */
3629 gc_alloc_update_all_page_tables();
3630 SHOW("verifying zero fill");
3635 verify_dynamic_space(void)
3637 generation_index_t i;
3639 for (i = 0; i <= HIGHEST_NORMAL_GENERATION; i++)
3640 verify_generation(i);
3642 if (gencgc_enable_verify_zero_fill)
3646 /* Write-protect all the dynamic boxed pages in the given generation. */
3648 write_protect_generation_pages(generation_index_t generation)
3652 gc_assert(generation < SCRATCH_GENERATION);
3654 for (start = 0; start < last_free_page; start++) {
3655 if ((page_table[start].allocated == BOXED_PAGE_FLAG)
3656 && (page_table[start].bytes_used != 0)
3657 && !page_table[start].dont_move
3658 && (page_table[start].gen == generation)) {
3662 /* Note the page as protected in the page tables. */
3663 page_table[start].write_protected = 1;
3665 for (last = start + 1; last < last_free_page; last++) {
3666 if ((page_table[last].allocated != BOXED_PAGE_FLAG)
3667 || (page_table[last].bytes_used == 0)
3668 || page_table[last].dont_move
3669 || (page_table[last].gen != generation))
3671 page_table[last].write_protected = 1;
3674 page_start = (void *)page_address(start);
3676 os_protect(page_start,
3677 PAGE_BYTES * (last - start),
3678 OS_VM_PROT_READ | OS_VM_PROT_EXECUTE);
3684 if (gencgc_verbose > 1) {
3686 "/write protected %d of %d pages in generation %d\n",
3687 count_write_protect_generation_pages(generation),
3688 count_generation_pages(generation),
3693 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
3696 scavenge_control_stack()
3698 unsigned long control_stack_size;
3700 /* This is going to be a big problem when we try to port threads
3702 struct thread *th = arch_os_get_current_thread();
3703 lispobj *control_stack =
3704 (lispobj *)(th->control_stack_start);
3706 control_stack_size = current_control_stack_pointer - control_stack;
3707 scavenge(control_stack, control_stack_size);
3710 /* Scavenging Interrupt Contexts */
3712 static int boxed_registers[] = BOXED_REGISTERS;
3715 scavenge_interrupt_context(os_context_t * context)
3721 unsigned long lip_offset;
3722 int lip_register_pair;
3724 unsigned long pc_code_offset;
3726 #ifdef ARCH_HAS_LINK_REGISTER
3727 unsigned long lr_code_offset;
3729 #ifdef ARCH_HAS_NPC_REGISTER
3730 unsigned long npc_code_offset;
3734 /* Find the LIP's register pair and calculate it's offset */
3735 /* before we scavenge the context. */
3738 * I (RLT) think this is trying to find the boxed register that is
3739 * closest to the LIP address, without going past it. Usually, it's
3740 * reg_CODE or reg_LRA. But sometimes, nothing can be found.
3742 lip = *os_context_register_addr(context, reg_LIP);
3743 lip_offset = 0x7FFFFFFF;
3744 lip_register_pair = -1;
3745 for (i = 0; i < (sizeof(boxed_registers) / sizeof(int)); i++) {
3750 index = boxed_registers[i];
3751 reg = *os_context_register_addr(context, index);
3752 if ((reg & ~((1L<<N_LOWTAG_BITS)-1)) <= lip) {
3754 if (offset < lip_offset) {
3755 lip_offset = offset;
3756 lip_register_pair = index;
3760 #endif /* reg_LIP */
3762 /* Compute the PC's offset from the start of the CODE */
3764 pc_code_offset = *os_context_pc_addr(context) - *os_context_register_addr(context, reg_CODE);
3765 #ifdef ARCH_HAS_NPC_REGISTER
3766 npc_code_offset = *os_context_npc_addr(context) - *os_context_register_addr(context, reg_CODE);
3767 #endif /* ARCH_HAS_NPC_REGISTER */
3769 #ifdef ARCH_HAS_LINK_REGISTER
3771 *os_context_lr_addr(context) -
3772 *os_context_register_addr(context, reg_CODE);
3775 /* Scanvenge all boxed registers in the context. */
3776 for (i = 0; i < (sizeof(boxed_registers) / sizeof(int)); i++) {
3780 index = boxed_registers[i];
3781 foo = *os_context_register_addr(context, index);
3783 *os_context_register_addr(context, index) = foo;
3785 scavenge((lispobj*) &(*os_context_register_addr(context, index)), 1);
3792 * But what happens if lip_register_pair is -1? *os_context_register_addr on Solaris
3793 * (see solaris_register_address in solaris-os.c) will return
3794 * &context->uc_mcontext.gregs[2]. But gregs[2] is REG_nPC. Is
3795 * that what we really want? My guess is that that is not what we
3796 * want, so if lip_register_pair is -1, we don't touch reg_LIP at
3797 * all. But maybe it doesn't really matter if LIP is trashed?
3799 if (lip_register_pair >= 0) {
3800 *os_context_register_addr(context, reg_LIP) =
3801 *os_context_register_addr(context, lip_register_pair) + lip_offset;
3803 #endif /* reg_LIP */
3805 /* Fix the PC if it was in from space */
3806 if (from_space_p(*os_context_pc_addr(context)))
3807 *os_context_pc_addr(context) = *os_context_register_addr(context, reg_CODE) + pc_code_offset;
3809 #ifdef ARCH_HAS_LINK_REGISTER
3810 /* Fix the LR ditto; important if we're being called from
3811 * an assembly routine that expects to return using blr, otherwise
3813 if (from_space_p(*os_context_lr_addr(context)))
3814 *os_context_lr_addr(context) =
3815 *os_context_register_addr(context, reg_CODE) + lr_code_offset;
3818 #ifdef ARCH_HAS_NPC_REGISTER
3819 if (from_space_p(*os_context_npc_addr(context)))
3820 *os_context_npc_addr(context) = *os_context_register_addr(context, reg_CODE) + npc_code_offset;
3821 #endif /* ARCH_HAS_NPC_REGISTER */
3825 scavenge_interrupt_contexts(void)
3828 os_context_t *context;
3830 struct thread *th=arch_os_get_current_thread();
3832 index = fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,0));
3834 #if defined(DEBUG_PRINT_CONTEXT_INDEX)
3835 printf("Number of active contexts: %d\n", index);
3838 for (i = 0; i < index; i++) {
3839 context = th->interrupt_contexts[i];
3840 scavenge_interrupt_context(context);
3846 #if defined(LISP_FEATURE_SB_THREAD)
3848 preserve_context_registers (os_context_t *c)
3851 /* On Darwin the signal context isn't a contiguous block of memory,
3852 * so just preserve_pointering its contents won't be sufficient.
3854 #if defined(LISP_FEATURE_DARWIN)
3855 #if defined LISP_FEATURE_X86
3856 preserve_pointer((void*)*os_context_register_addr(c,reg_EAX));
3857 preserve_pointer((void*)*os_context_register_addr(c,reg_ECX));
3858 preserve_pointer((void*)*os_context_register_addr(c,reg_EDX));
3859 preserve_pointer((void*)*os_context_register_addr(c,reg_EBX));
3860 preserve_pointer((void*)*os_context_register_addr(c,reg_ESI));
3861 preserve_pointer((void*)*os_context_register_addr(c,reg_EDI));
3862 preserve_pointer((void*)*os_context_pc_addr(c));
3863 #elif defined LISP_FEATURE_X86_64
3864 preserve_pointer((void*)*os_context_register_addr(c,reg_RAX));
3865 preserve_pointer((void*)*os_context_register_addr(c,reg_RCX));
3866 preserve_pointer((void*)*os_context_register_addr(c,reg_RDX));
3867 preserve_pointer((void*)*os_context_register_addr(c,reg_RBX));
3868 preserve_pointer((void*)*os_context_register_addr(c,reg_RSI));
3869 preserve_pointer((void*)*os_context_register_addr(c,reg_RDI));
3870 preserve_pointer((void*)*os_context_register_addr(c,reg_R8));
3871 preserve_pointer((void*)*os_context_register_addr(c,reg_R9));
3872 preserve_pointer((void*)*os_context_register_addr(c,reg_R10));
3873 preserve_pointer((void*)*os_context_register_addr(c,reg_R11));
3874 preserve_pointer((void*)*os_context_register_addr(c,reg_R12));
3875 preserve_pointer((void*)*os_context_register_addr(c,reg_R13));
3876 preserve_pointer((void*)*os_context_register_addr(c,reg_R14));
3877 preserve_pointer((void*)*os_context_register_addr(c,reg_R15));
3878 preserve_pointer((void*)*os_context_pc_addr(c));
3880 #error "preserve_context_registers needs to be tweaked for non-x86 Darwin"
3883 for(ptr = ((void **)(c+1))-1; ptr>=(void **)c; ptr--) {
3884 preserve_pointer(*ptr);
3889 /* Garbage collect a generation. If raise is 0 then the remains of the
3890 * generation are not raised to the next generation. */
3892 garbage_collect_generation(generation_index_t generation, int raise)
3894 unsigned long bytes_freed;
3896 unsigned long static_space_size;
3897 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
3900 gc_assert(generation <= HIGHEST_NORMAL_GENERATION);
3902 /* The oldest generation can't be raised. */
3903 gc_assert((generation != HIGHEST_NORMAL_GENERATION) || (raise == 0));
3905 /* Check if weak hash tables were processed in the previous GC. */
3906 gc_assert(weak_hash_tables == NULL);
3908 /* Initialize the weak pointer list. */
3909 weak_pointers = NULL;
3911 #ifdef LUTEX_WIDETAG
3912 unmark_lutexes(generation);
3915 /* When a generation is not being raised it is transported to a
3916 * temporary generation (NUM_GENERATIONS), and lowered when
3917 * done. Set up this new generation. There should be no pages
3918 * allocated to it yet. */
3920 gc_assert(generations[SCRATCH_GENERATION].bytes_allocated == 0);
3923 /* Set the global src and dest. generations */
3924 from_space = generation;
3926 new_space = generation+1;
3928 new_space = SCRATCH_GENERATION;
3930 /* Change to a new space for allocation, resetting the alloc_start_page */
3931 gc_alloc_generation = new_space;
3932 generations[new_space].alloc_start_page = 0;
3933 generations[new_space].alloc_unboxed_start_page = 0;
3934 generations[new_space].alloc_large_start_page = 0;
3935 generations[new_space].alloc_large_unboxed_start_page = 0;
3937 /* Before any pointers are preserved, the dont_move flags on the
3938 * pages need to be cleared. */
3939 for (i = 0; i < last_free_page; i++)
3940 if(page_table[i].gen==from_space)
3941 page_table[i].dont_move = 0;
3943 /* Un-write-protect the old-space pages. This is essential for the
3944 * promoted pages as they may contain pointers into the old-space
3945 * which need to be scavenged. It also helps avoid unnecessary page
3946 * faults as forwarding pointers are written into them. They need to
3947 * be un-protected anyway before unmapping later. */
3948 unprotect_oldspace();
3950 /* Scavenge the stacks' conservative roots. */
3952 /* there are potentially two stacks for each thread: the main
3953 * stack, which may contain Lisp pointers, and the alternate stack.
3954 * We don't ever run Lisp code on the altstack, but it may
3955 * host a sigcontext with lisp objects in it */
3957 /* what we need to do: (1) find the stack pointer for the main
3958 * stack; scavenge it (2) find the interrupt context on the
3959 * alternate stack that might contain lisp values, and scavenge
3962 /* we assume that none of the preceding applies to the thread that
3963 * initiates GC. If you ever call GC from inside an altstack
3964 * handler, you will lose. */
3966 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
3967 /* And if we're saving a core, there's no point in being conservative. */
3968 if (conservative_stack) {
3969 for_each_thread(th) {
3971 void **esp=(void **)-1;
3972 #ifdef LISP_FEATURE_SB_THREAD
3974 if(th==arch_os_get_current_thread()) {
3975 /* Somebody is going to burn in hell for this, but casting
3976 * it in two steps shuts gcc up about strict aliasing. */
3977 esp = (void **)((void *)&raise);
3980 free=fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,th));
3981 for(i=free-1;i>=0;i--) {
3982 os_context_t *c=th->interrupt_contexts[i];
3983 esp1 = (void **) *os_context_register_addr(c,reg_SP);
3984 if (esp1>=(void **)th->control_stack_start &&
3985 esp1<(void **)th->control_stack_end) {
3986 if(esp1<esp) esp=esp1;
3987 preserve_context_registers(c);
3992 esp = (void **)((void *)&raise);
3994 for (ptr = ((void **)th->control_stack_end)-1; ptr >= esp; ptr--) {
3995 preserve_pointer(*ptr);
4002 if (gencgc_verbose > 1) {
4003 long num_dont_move_pages = count_dont_move_pages();
4005 "/non-movable pages due to conservative pointers = %d (%d bytes)\n",
4006 num_dont_move_pages,
4007 num_dont_move_pages * PAGE_BYTES);
4011 /* Scavenge all the rest of the roots. */
4013 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
4015 * If not x86, we need to scavenge the interrupt context(s) and the
4018 scavenge_interrupt_contexts();
4019 scavenge_control_stack();
4022 /* Scavenge the Lisp functions of the interrupt handlers, taking
4023 * care to avoid SIG_DFL and SIG_IGN. */
4024 for (i = 0; i < NSIG; i++) {
4025 union interrupt_handler handler = interrupt_handlers[i];
4026 if (!ARE_SAME_HANDLER(handler.c, SIG_IGN) &&
4027 !ARE_SAME_HANDLER(handler.c, SIG_DFL)) {
4028 scavenge((lispobj *)(interrupt_handlers + i), 1);
4031 /* Scavenge the binding stacks. */
4034 for_each_thread(th) {
4035 long len= (lispobj *)get_binding_stack_pointer(th) -
4036 th->binding_stack_start;
4037 scavenge((lispobj *) th->binding_stack_start,len);
4038 #ifdef LISP_FEATURE_SB_THREAD
4039 /* do the tls as well */
4040 len=fixnum_value(SymbolValue(FREE_TLS_INDEX,0)) -
4041 (sizeof (struct thread))/(sizeof (lispobj));
4042 scavenge((lispobj *) (th+1),len);
4047 /* The original CMU CL code had scavenge-read-only-space code
4048 * controlled by the Lisp-level variable
4049 * *SCAVENGE-READ-ONLY-SPACE*. It was disabled by default, and it
4050 * wasn't documented under what circumstances it was useful or
4051 * safe to turn it on, so it's been turned off in SBCL. If you
4052 * want/need this functionality, and can test and document it,
4053 * please submit a patch. */
4055 if (SymbolValue(SCAVENGE_READ_ONLY_SPACE) != NIL) {
4056 unsigned long read_only_space_size =
4057 (lispobj*)SymbolValue(READ_ONLY_SPACE_FREE_POINTER) -
4058 (lispobj*)READ_ONLY_SPACE_START;
4060 "/scavenge read only space: %d bytes\n",
4061 read_only_space_size * sizeof(lispobj)));
4062 scavenge( (lispobj *) READ_ONLY_SPACE_START, read_only_space_size);
4066 /* Scavenge static space. */
4068 (lispobj *)SymbolValue(STATIC_SPACE_FREE_POINTER,0) -
4069 (lispobj *)STATIC_SPACE_START;
4070 if (gencgc_verbose > 1) {
4072 "/scavenge static space: %d bytes\n",
4073 static_space_size * sizeof(lispobj)));
4075 scavenge( (lispobj *) STATIC_SPACE_START, static_space_size);
4077 /* All generations but the generation being GCed need to be
4078 * scavenged. The new_space generation needs special handling as
4079 * objects may be moved in - it is handled separately below. */
4080 scavenge_generations(generation+1, PSEUDO_STATIC_GENERATION);
4082 /* Finally scavenge the new_space generation. Keep going until no
4083 * more objects are moved into the new generation */
4084 scavenge_newspace_generation(new_space);
4086 /* FIXME: I tried reenabling this check when debugging unrelated
4087 * GC weirdness ca. sbcl-0.6.12.45, and it failed immediately.
4088 * Since the current GC code seems to work well, I'm guessing that
4089 * this debugging code is just stale, but I haven't tried to
4090 * figure it out. It should be figured out and then either made to
4091 * work or just deleted. */
4092 #define RESCAN_CHECK 0
4094 /* As a check re-scavenge the newspace once; no new objects should
4097 long old_bytes_allocated = bytes_allocated;
4098 long bytes_allocated;
4100 /* Start with a full scavenge. */
4101 scavenge_newspace_generation_one_scan(new_space);
4103 /* Flush the current regions, updating the tables. */
4104 gc_alloc_update_all_page_tables();
4106 bytes_allocated = bytes_allocated - old_bytes_allocated;
4108 if (bytes_allocated != 0) {
4109 lose("Rescan of new_space allocated %d more bytes.\n",
4115 scan_weak_hash_tables();
4116 scan_weak_pointers();
4118 /* Flush the current regions, updating the tables. */
4119 gc_alloc_update_all_page_tables();
4121 /* Free the pages in oldspace, but not those marked dont_move. */
4122 bytes_freed = free_oldspace();
4124 /* If the GC is not raising the age then lower the generation back
4125 * to its normal generation number */
4127 for (i = 0; i < last_free_page; i++)
4128 if ((page_table[i].bytes_used != 0)
4129 && (page_table[i].gen == SCRATCH_GENERATION))
4130 page_table[i].gen = generation;
4131 gc_assert(generations[generation].bytes_allocated == 0);
4132 generations[generation].bytes_allocated =
4133 generations[SCRATCH_GENERATION].bytes_allocated;
4134 generations[SCRATCH_GENERATION].bytes_allocated = 0;
4137 /* Reset the alloc_start_page for generation. */
4138 generations[generation].alloc_start_page = 0;
4139 generations[generation].alloc_unboxed_start_page = 0;
4140 generations[generation].alloc_large_start_page = 0;
4141 generations[generation].alloc_large_unboxed_start_page = 0;
4143 if (generation >= verify_gens) {
4147 verify_dynamic_space();
4150 /* Set the new gc trigger for the GCed generation. */
4151 generations[generation].gc_trigger =
4152 generations[generation].bytes_allocated
4153 + generations[generation].bytes_consed_between_gc;
4156 generations[generation].num_gc = 0;
4158 ++generations[generation].num_gc;
4160 #ifdef LUTEX_WIDETAG
4161 reap_lutexes(generation);
4163 move_lutexes(generation, generation+1);
4167 /* Update last_free_page, then SymbolValue(ALLOCATION_POINTER). */
4169 update_dynamic_space_free_pointer(void)
4171 page_index_t last_page = -1, i;
4173 for (i = 0; i < last_free_page; i++)
4174 if ((page_table[i].allocated != FREE_PAGE_FLAG)
4175 && (page_table[i].bytes_used != 0))
4178 last_free_page = last_page+1;
4180 set_alloc_pointer((lispobj)(((char *)heap_base) + last_free_page*PAGE_BYTES));
4181 return 0; /* dummy value: return something ... */
4185 remap_free_pages (page_index_t from, page_index_t to)
4187 page_index_t first_page, last_page;
4189 for (first_page = from; first_page <= to; first_page++) {
4190 if (page_table[first_page].allocated != FREE_PAGE_FLAG ||
4191 page_table[first_page].need_to_zero == 0) {
4195 last_page = first_page + 1;
4196 while (page_table[last_page].allocated == FREE_PAGE_FLAG &&
4198 page_table[last_page].need_to_zero == 1) {
4202 /* There's a mysterious Solaris/x86 problem with using mmap
4203 * tricks for memory zeroing. See sbcl-devel thread
4204 * "Re: patch: standalone executable redux".
4206 #if defined(LISP_FEATURE_SUNOS)
4207 zero_pages(first_page, last_page-1);
4209 zero_pages_with_mmap(first_page, last_page-1);
4212 first_page = last_page;
4216 generation_index_t small_generation_limit = 1;
4218 /* GC all generations newer than last_gen, raising the objects in each
4219 * to the next older generation - we finish when all generations below
4220 * last_gen are empty. Then if last_gen is due for a GC, or if
4221 * last_gen==NUM_GENERATIONS (the scratch generation? eh?) we GC that
4222 * too. The valid range for last_gen is: 0,1,...,NUM_GENERATIONS.
4224 * We stop collecting at gencgc_oldest_gen_to_gc, even if this is less than
4225 * last_gen (oh, and note that by default it is NUM_GENERATIONS-1) */
4227 collect_garbage(generation_index_t last_gen)
4229 generation_index_t gen = 0, i;
4232 /* The largest value of last_free_page seen since the time
4233 * remap_free_pages was called. */
4234 static page_index_t high_water_mark = 0;
4236 FSHOW((stderr, "/entering collect_garbage(%d)\n", last_gen));
4240 if (last_gen > HIGHEST_NORMAL_GENERATION+1) {
4242 "/collect_garbage: last_gen = %d, doing a level 0 GC\n",
4247 /* Flush the alloc regions updating the tables. */
4248 gc_alloc_update_all_page_tables();
4250 /* Verify the new objects created by Lisp code. */
4251 if (pre_verify_gen_0) {
4252 FSHOW((stderr, "pre-checking generation 0\n"));
4253 verify_generation(0);
4256 if (gencgc_verbose > 1)
4257 print_generation_stats(0);
4260 /* Collect the generation. */
4262 if (gen >= gencgc_oldest_gen_to_gc) {
4263 /* Never raise the oldest generation. */
4268 || (generations[gen].num_gc >= generations[gen].trigger_age);
4271 if (gencgc_verbose > 1) {
4273 "starting GC of generation %d with raise=%d alloc=%d trig=%d GCs=%d\n",
4276 generations[gen].bytes_allocated,
4277 generations[gen].gc_trigger,
4278 generations[gen].num_gc));
4281 /* If an older generation is being filled, then update its
4284 generations[gen+1].cum_sum_bytes_allocated +=
4285 generations[gen+1].bytes_allocated;
4288 garbage_collect_generation(gen, raise);
4290 /* Reset the memory age cum_sum. */
4291 generations[gen].cum_sum_bytes_allocated = 0;
4293 if (gencgc_verbose > 1) {
4294 FSHOW((stderr, "GC of generation %d finished:\n", gen));
4295 print_generation_stats(0);
4299 } while ((gen <= gencgc_oldest_gen_to_gc)
4300 && ((gen < last_gen)
4301 || ((gen <= gencgc_oldest_gen_to_gc)
4303 && (generations[gen].bytes_allocated
4304 > generations[gen].gc_trigger)
4305 && (gen_av_mem_age(gen)
4306 > generations[gen].min_av_mem_age))));
4308 /* Now if gen-1 was raised all generations before gen are empty.
4309 * If it wasn't raised then all generations before gen-1 are empty.
4311 * Now objects within this gen's pages cannot point to younger
4312 * generations unless they are written to. This can be exploited
4313 * by write-protecting the pages of gen; then when younger
4314 * generations are GCed only the pages which have been written
4319 gen_to_wp = gen - 1;
4321 /* There's not much point in WPing pages in generation 0 as it is
4322 * never scavenged (except promoted pages). */
4323 if ((gen_to_wp > 0) && enable_page_protection) {
4324 /* Check that they are all empty. */
4325 for (i = 0; i < gen_to_wp; i++) {
4326 if (generations[i].bytes_allocated)
4327 lose("trying to write-protect gen. %d when gen. %d nonempty\n",
4330 write_protect_generation_pages(gen_to_wp);
4333 /* Set gc_alloc() back to generation 0. The current regions should
4334 * be flushed after the above GCs. */
4335 gc_assert((boxed_region.free_pointer - boxed_region.start_addr) == 0);
4336 gc_alloc_generation = 0;
4338 /* Save the high-water mark before updating last_free_page */
4339 if (last_free_page > high_water_mark)
4340 high_water_mark = last_free_page;
4342 update_dynamic_space_free_pointer();
4344 auto_gc_trigger = bytes_allocated + bytes_consed_between_gcs;
4346 fprintf(stderr,"Next gc when %ld bytes have been consed\n",
4349 /* If we did a big GC (arbitrarily defined as gen > 1), release memory
4352 if (gen > small_generation_limit) {
4353 if (last_free_page > high_water_mark)
4354 high_water_mark = last_free_page;
4355 remap_free_pages(0, high_water_mark);
4356 high_water_mark = 0;
4361 SHOW("returning from collect_garbage");
4364 /* This is called by Lisp PURIFY when it is finished. All live objects
4365 * will have been moved to the RO and Static heaps. The dynamic space
4366 * will need a full re-initialization. We don't bother having Lisp
4367 * PURIFY flush the current gc_alloc() region, as the page_tables are
4368 * re-initialized, and every page is zeroed to be sure. */
4374 if (gencgc_verbose > 1)
4375 SHOW("entering gc_free_heap");
4377 for (page = 0; page < page_table_pages; page++) {
4378 /* Skip free pages which should already be zero filled. */
4379 if (page_table[page].allocated != FREE_PAGE_FLAG) {
4380 void *page_start, *addr;
4382 /* Mark the page free. The other slots are assumed invalid
4383 * when it is a FREE_PAGE_FLAG and bytes_used is 0 and it
4384 * should not be write-protected -- except that the
4385 * generation is used for the current region but it sets
4387 page_table[page].allocated = FREE_PAGE_FLAG;
4388 page_table[page].bytes_used = 0;
4390 #ifndef LISP_FEATURE_WIN32 /* Pages already zeroed on win32? Not sure about this change. */
4391 /* Zero the page. */
4392 page_start = (void *)page_address(page);
4394 /* First, remove any write-protection. */
4395 os_protect(page_start, PAGE_BYTES, OS_VM_PROT_ALL);
4396 page_table[page].write_protected = 0;
4398 os_invalidate(page_start,PAGE_BYTES);
4399 addr = os_validate(page_start,PAGE_BYTES);
4400 if (addr == NULL || addr != page_start) {
4401 lose("gc_free_heap: page moved, 0x%08x ==> 0x%08x\n",
4406 page_table[page].write_protected = 0;
4408 } else if (gencgc_zero_check_during_free_heap) {
4409 /* Double-check that the page is zero filled. */
4412 gc_assert(page_table[page].allocated == FREE_PAGE_FLAG);
4413 gc_assert(page_table[page].bytes_used == 0);
4414 page_start = (long *)page_address(page);
4415 for (i=0; i<1024; i++) {
4416 if (page_start[i] != 0) {
4417 lose("free region not zero at %x\n", page_start + i);
4423 bytes_allocated = 0;
4425 /* Initialize the generations. */
4426 for (page = 0; page < NUM_GENERATIONS; page++) {
4427 generations[page].alloc_start_page = 0;
4428 generations[page].alloc_unboxed_start_page = 0;
4429 generations[page].alloc_large_start_page = 0;
4430 generations[page].alloc_large_unboxed_start_page = 0;
4431 generations[page].bytes_allocated = 0;
4432 generations[page].gc_trigger = 2000000;
4433 generations[page].num_gc = 0;
4434 generations[page].cum_sum_bytes_allocated = 0;
4435 generations[page].lutexes = NULL;
4438 if (gencgc_verbose > 1)
4439 print_generation_stats(0);
4441 /* Initialize gc_alloc(). */
4442 gc_alloc_generation = 0;
4444 gc_set_region_empty(&boxed_region);
4445 gc_set_region_empty(&unboxed_region);
4448 set_alloc_pointer((lispobj)((char *)heap_base));
4450 if (verify_after_free_heap) {
4451 /* Check whether purify has left any bad pointers. */
4453 SHOW("checking after free_heap\n");
4463 /* Compute the number of pages needed for the dynamic space.
4464 * Dynamic space size should be aligned on page size. */
4465 page_table_pages = dynamic_space_size/PAGE_BYTES;
4466 gc_assert(dynamic_space_size == (size_t) page_table_pages*PAGE_BYTES);
4468 page_table = calloc(page_table_pages, sizeof(struct page));
4469 gc_assert(page_table);
4472 scavtab[WEAK_POINTER_WIDETAG] = scav_weak_pointer;
4473 transother[SIMPLE_ARRAY_WIDETAG] = trans_boxed_large;
4475 #ifdef LUTEX_WIDETAG
4476 scavtab[LUTEX_WIDETAG] = scav_lutex;
4477 transother[LUTEX_WIDETAG] = trans_lutex;
4478 sizetab[LUTEX_WIDETAG] = size_lutex;
4481 heap_base = (void*)DYNAMIC_SPACE_START;
4483 /* Initialize each page structure. */
4484 for (i = 0; i < page_table_pages; i++) {
4485 /* Initialize all pages as free. */
4486 page_table[i].allocated = FREE_PAGE_FLAG;
4487 page_table[i].bytes_used = 0;
4489 /* Pages are not write-protected at startup. */
4490 page_table[i].write_protected = 0;
4493 bytes_allocated = 0;
4495 /* Initialize the generations.
4497 * FIXME: very similar to code in gc_free_heap(), should be shared */
4498 for (i = 0; i < NUM_GENERATIONS; i++) {
4499 generations[i].alloc_start_page = 0;
4500 generations[i].alloc_unboxed_start_page = 0;
4501 generations[i].alloc_large_start_page = 0;
4502 generations[i].alloc_large_unboxed_start_page = 0;
4503 generations[i].bytes_allocated = 0;
4504 generations[i].gc_trigger = 2000000;
4505 generations[i].num_gc = 0;
4506 generations[i].cum_sum_bytes_allocated = 0;
4507 /* the tune-able parameters */
4508 generations[i].bytes_consed_between_gc = 2000000;
4509 generations[i].trigger_age = 1;
4510 generations[i].min_av_mem_age = 0.75;
4511 generations[i].lutexes = NULL;
4514 /* Initialize gc_alloc. */
4515 gc_alloc_generation = 0;
4516 gc_set_region_empty(&boxed_region);
4517 gc_set_region_empty(&unboxed_region);
4522 /* Pick up the dynamic space from after a core load.
4524 * The ALLOCATION_POINTER points to the end of the dynamic space.
4528 gencgc_pickup_dynamic(void)
4530 page_index_t page = 0;
4531 long alloc_ptr = get_alloc_pointer();
4532 lispobj *prev=(lispobj *)page_address(page);
4533 generation_index_t gen = PSEUDO_STATIC_GENERATION;
4536 lispobj *first,*ptr= (lispobj *)page_address(page);
4537 page_table[page].allocated = BOXED_PAGE_FLAG;
4538 page_table[page].gen = gen;
4539 page_table[page].bytes_used = PAGE_BYTES;
4540 page_table[page].large_object = 0;
4541 page_table[page].write_protected = 0;
4542 page_table[page].write_protected_cleared = 0;
4543 page_table[page].dont_move = 0;
4544 page_table[page].need_to_zero = 1;
4546 if (!gencgc_partial_pickup) {
4547 first=gc_search_space(prev,(ptr+2)-prev,ptr);
4548 if(ptr == first) prev=ptr;
4549 page_table[page].first_object_offset =
4550 (void *)prev - page_address(page);
4553 } while ((long)page_address(page) < alloc_ptr);
4555 #ifdef LUTEX_WIDETAG
4556 /* Lutexes have been registered in generation 0 by coreparse, and
4557 * need to be moved to the right one manually.
4559 move_lutexes(0, PSEUDO_STATIC_GENERATION);
4562 last_free_page = page;
4564 generations[gen].bytes_allocated = PAGE_BYTES*page;
4565 bytes_allocated = PAGE_BYTES*page;
4567 gc_alloc_update_all_page_tables();
4568 write_protect_generation_pages(gen);
4572 gc_initialize_pointers(void)
4574 gencgc_pickup_dynamic();
4580 /* alloc(..) is the external interface for memory allocation. It
4581 * allocates to generation 0. It is not called from within the garbage
4582 * collector as it is only external uses that need the check for heap
4583 * size (GC trigger) and to disable the interrupts (interrupts are
4584 * always disabled during a GC).
4586 * The vops that call alloc(..) assume that the returned space is zero-filled.
4587 * (E.g. the most significant word of a 2-word bignum in MOVE-FROM-UNSIGNED.)
4589 * The check for a GC trigger is only performed when the current
4590 * region is full, so in most cases it's not needed. */
4595 struct thread *thread=arch_os_get_current_thread();
4596 struct alloc_region *region=
4597 #ifdef LISP_FEATURE_SB_THREAD
4598 thread ? &(thread->alloc_region) : &boxed_region;
4602 #ifndef LISP_FEATURE_WIN32
4603 lispobj alloc_signal;
4606 void *new_free_pointer;
4608 gc_assert(nbytes>0);
4610 /* Check for alignment allocation problems. */
4611 gc_assert((((unsigned long)region->free_pointer & LOWTAG_MASK) == 0)
4612 && ((nbytes & LOWTAG_MASK) == 0));
4616 /* there are a few places in the C code that allocate data in the
4617 * heap before Lisp starts. This is before interrupts are enabled,
4618 * so we don't need to check for pseudo-atomic */
4619 #ifdef LISP_FEATURE_SB_THREAD
4620 if(!get_psuedo_atomic_atomic(th)) {
4622 fprintf(stderr, "fatal error in thread 0x%x, tid=%ld\n",
4624 __asm__("movl %fs,%0" : "=r" (fs) : );
4625 fprintf(stderr, "fs is %x, th->tls_cookie=%x \n",
4626 debug_get_fs(),th->tls_cookie);
4627 lose("If you see this message before 2004.01.31, mail details to sbcl-devel\n");
4630 gc_assert(get_pseudo_atomic_atomic(th));
4634 /* maybe we can do this quickly ... */
4635 new_free_pointer = region->free_pointer + nbytes;
4636 if (new_free_pointer <= region->end_addr) {
4637 new_obj = (void*)(region->free_pointer);
4638 region->free_pointer = new_free_pointer;
4639 return(new_obj); /* yup */
4642 /* we have to go the long way around, it seems. Check whether
4643 * we should GC in the near future
4645 if (auto_gc_trigger && bytes_allocated > auto_gc_trigger) {
4646 gc_assert(get_pseudo_atomic_atomic(thread));
4647 /* Don't flood the system with interrupts if the need to gc is
4648 * already noted. This can happen for example when SUB-GC
4649 * allocates or after a gc triggered in a WITHOUT-GCING. */
4650 if (SymbolValue(GC_PENDING,thread) == NIL) {
4651 /* set things up so that GC happens when we finish the PA
4653 SetSymbolValue(GC_PENDING,T,thread);
4654 if (SymbolValue(GC_INHIBIT,thread) == NIL)
4655 set_pseudo_atomic_interrupted(thread);
4658 new_obj = gc_alloc_with_region(nbytes,0,region,0);
4660 #ifndef LISP_FEATURE_WIN32
4661 alloc_signal = SymbolValue(ALLOC_SIGNAL,thread);
4662 if ((alloc_signal & FIXNUM_TAG_MASK) == 0) {
4663 if ((signed long) alloc_signal <= 0) {
4664 #ifdef LISP_FEATURE_SB_THREAD
4665 kill_thread_safely(thread->os_thread, SIGPROF);
4670 SetSymbolValue(ALLOC_SIGNAL,
4671 alloc_signal - (1 << N_FIXNUM_TAG_BITS),
4681 * shared support for the OS-dependent signal handlers which
4682 * catch GENCGC-related write-protect violations
4685 void unhandled_sigmemoryfault(void* addr);
4687 /* Depending on which OS we're running under, different signals might
4688 * be raised for a violation of write protection in the heap. This
4689 * function factors out the common generational GC magic which needs
4690 * to invoked in this case, and should be called from whatever signal
4691 * handler is appropriate for the OS we're running under.
4693 * Return true if this signal is a normal generational GC thing that
4694 * we were able to handle, or false if it was abnormal and control
4695 * should fall through to the general SIGSEGV/SIGBUS/whatever logic. */
4698 gencgc_handle_wp_violation(void* fault_addr)
4700 page_index_t page_index = find_page_index(fault_addr);
4702 #ifdef QSHOW_SIGNALS
4703 FSHOW((stderr, "heap WP violation? fault_addr=%x, page_index=%d\n",
4704 fault_addr, page_index));
4707 /* Check whether the fault is within the dynamic space. */
4708 if (page_index == (-1)) {
4710 /* It can be helpful to be able to put a breakpoint on this
4711 * case to help diagnose low-level problems. */
4712 unhandled_sigmemoryfault(fault_addr);
4714 /* not within the dynamic space -- not our responsibility */
4718 if (page_table[page_index].write_protected) {
4719 /* Unprotect the page. */
4720 os_protect(page_address(page_index), PAGE_BYTES, OS_VM_PROT_ALL);
4721 page_table[page_index].write_protected_cleared = 1;
4722 page_table[page_index].write_protected = 0;
4724 /* The only acceptable reason for this signal on a heap
4725 * access is that GENCGC write-protected the page.
4726 * However, if two CPUs hit a wp page near-simultaneously,
4727 * we had better not have the second one lose here if it
4728 * does this test after the first one has already set wp=0
4730 if(page_table[page_index].write_protected_cleared != 1)
4731 lose("fault in heap page %d not marked as write-protected\nboxed_region.first_page: %d, boxed_region.last_page %d\n",
4732 page_index, boxed_region.first_page, boxed_region.last_page);
4734 /* Don't worry, we can handle it. */
4738 /* This is to be called when we catch a SIGSEGV/SIGBUS, determine that
4739 * it's not just a case of the program hitting the write barrier, and
4740 * are about to let Lisp deal with it. It's basically just a
4741 * convenient place to set a gdb breakpoint. */
4743 unhandled_sigmemoryfault(void *addr)
4746 void gc_alloc_update_all_page_tables(void)
4748 /* Flush the alloc regions updating the tables. */
4751 gc_alloc_update_page_tables(0, &th->alloc_region);
4752 gc_alloc_update_page_tables(1, &unboxed_region);
4753 gc_alloc_update_page_tables(0, &boxed_region);
4757 gc_set_region_empty(struct alloc_region *region)
4759 region->first_page = 0;
4760 region->last_page = -1;
4761 region->start_addr = page_address(0);
4762 region->free_pointer = page_address(0);
4763 region->end_addr = page_address(0);
4767 zero_all_free_pages()
4771 for (i = 0; i < last_free_page; i++) {
4772 if (page_table[i].allocated == FREE_PAGE_FLAG) {
4773 #ifdef READ_PROTECT_FREE_PAGES
4774 os_protect(page_address(i),
4783 /* Things to do before doing a final GC before saving a core (without
4786 * + Pages in large_object pages aren't moved by the GC, so we need to
4787 * unset that flag from all pages.
4788 * + The pseudo-static generation isn't normally collected, but it seems
4789 * reasonable to collect it at least when saving a core. So move the
4790 * pages to a normal generation.
4793 prepare_for_final_gc ()
4796 for (i = 0; i < last_free_page; i++) {
4797 page_table[i].large_object = 0;
4798 if (page_table[i].gen == PSEUDO_STATIC_GENERATION) {
4799 int used = page_table[i].bytes_used;
4800 page_table[i].gen = HIGHEST_NORMAL_GENERATION;
4801 generations[PSEUDO_STATIC_GENERATION].bytes_allocated -= used;
4802 generations[HIGHEST_NORMAL_GENERATION].bytes_allocated += used;
4808 /* Do a non-conservative GC, and then save a core with the initial
4809 * function being set to the value of the static symbol
4810 * SB!VM:RESTART-LISP-FUNCTION */
4812 gc_and_save(char *filename, int prepend_runtime)
4815 void *runtime_bytes = NULL;
4816 size_t runtime_size;
4818 file = prepare_to_save(filename, prepend_runtime, &runtime_bytes,
4823 conservative_stack = 0;
4825 /* The filename might come from Lisp, and be moved by the now
4826 * non-conservative GC. */
4827 filename = strdup(filename);
4829 /* Collect twice: once into relatively high memory, and then back
4830 * into low memory. This compacts the retained data into the lower
4831 * pages, minimizing the size of the core file.
4833 prepare_for_final_gc();
4834 gencgc_alloc_start_page = last_free_page;
4835 collect_garbage(HIGHEST_NORMAL_GENERATION+1);
4837 prepare_for_final_gc();
4838 gencgc_alloc_start_page = -1;
4839 collect_garbage(HIGHEST_NORMAL_GENERATION+1);
4841 if (prepend_runtime)
4842 save_runtime_to_filehandle(file, runtime_bytes, runtime_size);
4844 /* The dumper doesn't know that pages need to be zeroed before use. */
4845 zero_all_free_pages();
4846 save_to_filehandle(file, filename, SymbolValue(RESTART_LISP_FUNCTION,0),
4848 /* Oops. Save still managed to fail. Since we've mangled the stack
4849 * beyond hope, there's not much we can do.
4850 * (beyond FUNCALLing RESTART_LISP_FUNCTION, but I suspect that's
4851 * going to be rather unsatisfactory too... */
4852 lose("Attempt to save core after non-conservative GC failed.\n");