Add hopscotch.c to $(COMMON_SRC)
[sbcl.git] / src / runtime / gencgc.c
blobb10a62c98946a4db7d821ef4a2ff81bf1e89c64b
1 /*
2 * GENerational Conservative Garbage Collector for SBCL
3 */
5 /*
6 * This software is part of the SBCL system. See the README file for
7 * more information.
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>
23 * as
24 * <ftp://ftp.cs.utexas.edu/pub/garbage/bigsurv.ps>.
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <errno.h>
30 #include <string.h>
31 #include <inttypes.h>
32 #include "sbcl.h"
33 #if defined(LISP_FEATURE_WIN32) && defined(LISP_FEATURE_SB_THREAD)
34 #include "pthreads_win32.h"
35 #else
36 #include <signal.h>
37 #endif
38 #include "runtime.h"
39 #include "os.h"
40 #include "interr.h"
41 #include "globals.h"
42 #include "interrupt.h"
43 #include "validate.h"
44 #include "lispregs.h"
45 #include "arch.h"
46 #include "gc.h"
47 #include "gc-internal.h"
48 #include "thread.h"
49 #include "pseudo-atomic.h"
50 #include "alloc.h"
51 #include "genesis/gc-tables.h"
52 #include "genesis/vector.h"
53 #include "genesis/weak-pointer.h"
54 #include "genesis/fdefn.h"
55 #include "genesis/simple-fun.h"
56 #include "save.h"
57 #include "genesis/hash-table.h"
58 #include "genesis/instance.h"
59 #include "genesis/layout.h"
60 #include "gencgc.h"
61 #include "hopscotch.h"
62 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
63 #include "genesis/cons.h"
64 #endif
65 #include "forwarding-ptr.h"
67 /* forward declarations */
68 page_index_t gc_find_freeish_pages(page_index_t *restart_page_ptr, sword_t nbytes,
69 int page_type_flag);
73 * GC parameters
76 /* As usually configured, generations 0-5 are normal collected generations,
77 6 is pseudo-static (the objects in which are never moved nor reclaimed),
78 and 7 is scratch space used when collecting a generation without promotion,
79 wherein it is moved to generation 7 and back again.
81 enum {
82 SCRATCH_GENERATION = PSEUDO_STATIC_GENERATION+1,
83 NUM_GENERATIONS
86 /* Should we use page protection to help avoid the scavenging of pages
87 * that don't have pointers to younger generations? */
88 boolean enable_page_protection = 1;
90 /* Largest allocation seen since last GC. */
91 os_vm_size_t large_allocation = 0;
95 * debugging
98 /* the verbosity level. All non-error messages are disabled at level 0;
99 * and only a few rare messages are printed at level 1. */
100 #if QSHOW == 2
101 boolean gencgc_verbose = 1;
102 #else
103 boolean gencgc_verbose = 0;
104 #endif
106 /* FIXME: At some point enable the various error-checking things below
107 * and see what they say. */
109 /* We hunt for pointers to old-space, when GCing generations >= verify_gen.
110 * Set verify_gens to HIGHEST_NORMAL_GENERATION + 1 to disable this kind of
111 * check. */
112 generation_index_t verify_gens = HIGHEST_NORMAL_GENERATION + 1;
114 /* Should we do a pre-scan verify of generation 0 before it's GCed? */
115 boolean pre_verify_gen_0 = 0;
117 #ifdef LISP_FEATURE_X86
118 /* Should we check code objects for fixup errors after they are transported? */
119 boolean check_code_fixups = 0;
120 #endif
122 /* Should we check that newly allocated regions are zero filled? */
123 boolean gencgc_zero_check = 0;
125 /* Should we check that the free space is zero filled? */
126 boolean gencgc_enable_verify_zero_fill = 0;
128 /* When loading a core, don't do a full scan of the memory for the
129 * memory region boundaries. (Set to true by coreparse.c if the core
130 * contained a pagetable entry).
132 boolean gencgc_partial_pickup = 0;
134 /* If defined, free pages are read-protected to ensure that nothing
135 * accesses them.
138 /* #define READ_PROTECT_FREE_PAGES */
142 * GC structures and variables
145 /* the total bytes allocated. These are seen by Lisp DYNAMIC-USAGE. */
146 os_vm_size_t bytes_allocated = 0;
147 os_vm_size_t auto_gc_trigger = 0;
149 /* the source and destination generations. These are set before a GC starts
150 * scavenging. */
151 generation_index_t from_space;
152 generation_index_t new_space;
154 /* Set to 1 when in GC */
155 boolean gc_active_p = 0;
157 /* should the GC be conservative on stack. If false (only right before
158 * saving a core), don't scan the stack / mark pages dont_move. */
159 static boolean conservative_stack = 1;
161 /* An array of page structures is allocated on gc initialization.
162 * This helps to quickly map between an address and its page structure.
163 * page_table_pages is set from the size of the dynamic space. */
164 page_index_t page_table_pages;
165 struct page *page_table;
166 #ifndef GENCGC_IS_PRECISE
167 struct hopscotch_table pinned_objects;
168 lispobj gc_object_watcher;
169 int gc_n_stack_pins;
170 #endif
172 /* In GC cards that have conservative pointers to them, should we wipe out
173 * dwords in there that are not used, so that they do not act as false
174 * root to other things in the heap from then on? This is a new feature
175 * but in testing it is both reliable and no noticeable slowdown. */
176 int do_wipe_p = 1;
178 /// Constants defined in gc-internal:
179 /// #define BOXED_PAGE_FLAG 1
180 /// #define UNBOXED_PAGE_FLAG 2
181 /// #define OPEN_REGION_PAGE_FLAG 4
183 /// Return true if 'allocated' bits are: {001, 010, 011}, false if 1zz or 000.
184 static inline boolean page_allocated_no_region_p(page_index_t page) {
185 return (page_table[page].allocated ^ OPEN_REGION_PAGE_FLAG) > OPEN_REGION_PAGE_FLAG;
188 static inline boolean page_free_p(page_index_t page) {
189 return (page_table[page].allocated == FREE_PAGE_FLAG);
192 static inline boolean page_boxed_p(page_index_t page) {
193 return (page_table[page].allocated & BOXED_PAGE_FLAG);
196 /// Return true if 'allocated' bits are: {001, 011}, false otherwise.
197 /// i.e. true of pages which could hold boxed or partially boxed objects.
198 static inline boolean page_boxed_no_region_p(page_index_t page) {
199 return (page_table[page].allocated & 5) == BOXED_PAGE_FLAG;
202 /// Return true if page MUST NOT hold boxed objects (including code).
203 static inline boolean page_unboxed_p(page_index_t page) {
204 /* Both flags set == boxed code page */
205 return (page_table[page].allocated & 3) == UNBOXED_PAGE_FLAG;
208 static inline boolean protect_page_p(page_index_t page, generation_index_t generation) {
209 return (page_boxed_no_region_p(page)
210 && (page_bytes_used(page) != 0)
211 && !page_table[page].dont_move
212 && (page_table[page].gen == generation));
215 /* Calculate the start address for the given page number. */
216 inline char *
217 page_address(page_index_t page_num)
219 return (void*)(DYNAMIC_SPACE_START + (page_num * GENCGC_CARD_BYTES));
222 /* Calculate the address where the allocation region associated with
223 * the page starts. */
224 static inline void *
225 page_scan_start(page_index_t page_index)
227 return page_address(page_index)-page_scan_start_offset(page_index);
230 /* True if the page starts a contiguous block. */
231 static inline boolean
232 page_starts_contiguous_block_p(page_index_t page_index)
234 // Don't use the preprocessor macro: 0 means 0.
235 return page_table[page_index].scan_start_offset_ == 0;
238 /* True if the page is the last page in a contiguous block. */
239 static inline boolean
240 page_ends_contiguous_block_p(page_index_t page_index, generation_index_t gen)
242 return (/* page doesn't fill block */
243 (page_bytes_used(page_index) < GENCGC_CARD_BYTES)
244 /* page is last allocated page */
245 || ((page_index + 1) >= last_free_page)
246 /* next page free */
247 || page_free_p(page_index + 1)
248 /* next page contains no data */
249 || (page_bytes_used(page_index + 1) == 0)
250 /* next page is in different generation */
251 || (page_table[page_index + 1].gen != gen)
252 /* next page starts its own contiguous block */
253 || (page_starts_contiguous_block_p(page_index + 1)));
256 /// External function for calling from Lisp.
257 page_index_t ext_find_page_index(void *addr) { return find_page_index(addr); }
259 static os_vm_size_t
260 npage_bytes(page_index_t npages)
262 gc_assert(npages>=0);
263 return ((os_vm_size_t)npages)*GENCGC_CARD_BYTES;
266 /* Check that X is a higher address than Y and return offset from Y to
267 * X in bytes. */
268 static inline os_vm_size_t
269 addr_diff(void *x, void *y)
271 gc_assert(x >= y);
272 return (uintptr_t)x - (uintptr_t)y;
275 /* a structure to hold the state of a generation
277 * CAUTION: If you modify this, make sure to touch up the alien
278 * definition in src/code/gc.lisp accordingly. ...or better yes,
279 * deal with the FIXME there...
281 struct generation {
283 #ifdef LISP_FEATURE_SEGREGATED_CODE
284 // A distinct start page per nonzero value of 'page_type_flag'.
285 // The zeroth index is the large object start page.
286 page_index_t alloc_start_page_[4];
287 #define alloc_large_start_page alloc_start_page_[0]
288 #define alloc_start_page alloc_start_page_[BOXED_PAGE_FLAG]
289 #define alloc_unboxed_start_page alloc_start_page_[UNBOXED_PAGE_FLAG]
290 #else
291 /* the first page that gc_alloc() checks on its next call */
292 page_index_t alloc_start_page;
294 /* the first page that gc_alloc_unboxed() checks on its next call */
295 page_index_t alloc_unboxed_start_page;
297 /* the first page that gc_alloc_large (boxed) considers on its next
298 * call. (Although it always allocates after the boxed_region.) */
299 page_index_t alloc_large_start_page;
300 #endif
302 /* the bytes allocated to this generation */
303 os_vm_size_t bytes_allocated;
305 /* the number of bytes at which to trigger a GC */
306 os_vm_size_t gc_trigger;
308 /* to calculate a new level for gc_trigger */
309 os_vm_size_t bytes_consed_between_gc;
311 /* the number of GCs since the last raise */
312 int num_gc;
314 /* the number of GCs to run on the generations before raising objects to the
315 * next generation */
316 int number_of_gcs_before_promotion;
318 /* the cumulative sum of the bytes allocated to this generation. It is
319 * cleared after a GC on this generations, and update before new
320 * objects are added from a GC of a younger generation. Dividing by
321 * the bytes_allocated will give the average age of the memory in
322 * this generation since its last GC. */
323 os_vm_size_t cum_sum_bytes_allocated;
325 /* a minimum average memory age before a GC will occur helps
326 * prevent a GC when a large number of new live objects have been
327 * added, in which case a GC could be a waste of time */
328 double minimum_age_before_gc;
331 /* an array of generation structures. There needs to be one more
332 * generation structure than actual generations as the oldest
333 * generation is temporarily raised then lowered. */
334 struct generation generations[NUM_GENERATIONS];
336 /* the oldest generation that is will currently be GCed by default.
337 * Valid values are: 0, 1, ... HIGHEST_NORMAL_GENERATION
339 * The default of HIGHEST_NORMAL_GENERATION enables GC on all generations.
341 * Setting this to 0 effectively disables the generational nature of
342 * the GC. In some applications generational GC may not be useful
343 * because there are no long-lived objects.
345 * An intermediate value could be handy after moving long-lived data
346 * into an older generation so an unnecessary GC of this long-lived
347 * data can be avoided. */
348 generation_index_t gencgc_oldest_gen_to_gc = HIGHEST_NORMAL_GENERATION;
350 /* META: Is nobody aside from me bothered by this especially misleading
351 * use of the word "last"? It could mean either "ultimate" or "prior",
352 * but in fact means neither. It is the *FIRST* page that should be grabbed
353 * for more space, so it is min free page, or 1+ the max used page. */
354 /* The maximum free page in the heap is maintained and used to update
355 * ALLOCATION_POINTER which is used by the room function to limit its
356 * search of the heap. XX Gencgc obviously needs to be better
357 * integrated with the Lisp code. */
359 page_index_t last_free_page;
361 #ifdef LISP_FEATURE_SB_THREAD
362 /* This lock is to prevent multiple threads from simultaneously
363 * allocating new regions which overlap each other. Note that the
364 * majority of GC is single-threaded, but alloc() may be called from
365 * >1 thread at a time and must be thread-safe. This lock must be
366 * seized before all accesses to generations[] or to parts of
367 * page_table[] that other threads may want to see */
368 static pthread_mutex_t free_pages_lock = PTHREAD_MUTEX_INITIALIZER;
369 /* This lock is used to protect non-thread-local allocation. */
370 static pthread_mutex_t allocation_lock = PTHREAD_MUTEX_INITIALIZER;
371 #endif
373 extern os_vm_size_t gencgc_release_granularity;
374 os_vm_size_t gencgc_release_granularity = GENCGC_RELEASE_GRANULARITY;
376 extern os_vm_size_t gencgc_alloc_granularity;
377 os_vm_size_t gencgc_alloc_granularity = GENCGC_ALLOC_GRANULARITY;
381 * miscellaneous heap functions
384 /* Count the number of pages which are write-protected within the
385 * given generation. */
386 static page_index_t
387 count_write_protect_generation_pages(generation_index_t generation)
389 page_index_t i, count = 0;
391 for (i = 0; i < last_free_page; i++)
392 if (!page_free_p(i)
393 && (page_table[i].gen == generation)
394 && (page_table[i].write_protected == 1))
395 count++;
396 return count;
399 /* Count the number of pages within the given generation. */
400 static page_index_t
401 count_generation_pages(generation_index_t generation)
403 page_index_t i;
404 page_index_t count = 0;
406 for (i = 0; i < last_free_page; i++)
407 if (!page_free_p(i)
408 && (page_table[i].gen == generation))
409 count++;
410 return count;
413 #if QSHOW
414 static page_index_t
415 count_dont_move_pages(void)
417 page_index_t i;
418 page_index_t count = 0;
419 for (i = 0; i < last_free_page; i++) {
420 if (!page_free_p(i)
421 && (page_table[i].dont_move != 0)) {
422 ++count;
425 return count;
427 #endif /* QSHOW */
429 /* Work through the pages and add up the number of bytes used for the
430 * given generation. */
431 static __attribute__((unused)) os_vm_size_t
432 count_generation_bytes_allocated (generation_index_t gen)
434 page_index_t i;
435 os_vm_size_t result = 0;
436 for (i = 0; i < last_free_page; i++) {
437 if (!page_free_p(i)
438 && (page_table[i].gen == gen))
439 result += page_bytes_used(i);
441 return result;
444 /* Return the average age of the memory in a generation. */
445 extern double
446 generation_average_age(generation_index_t gen)
448 if (generations[gen].bytes_allocated == 0)
449 return 0.0;
451 return
452 ((double)generations[gen].cum_sum_bytes_allocated)
453 / ((double)generations[gen].bytes_allocated);
456 #ifdef LISP_FEATURE_X86
457 extern void fpu_save(void *);
458 extern void fpu_restore(void *);
459 #endif
461 #define PAGE_INDEX_FMT PRIdPTR
463 extern void
464 write_generation_stats(FILE *file)
466 generation_index_t i;
468 #ifdef LISP_FEATURE_X86
469 int fpu_state[27];
471 /* Can end up here after calling alloc_tramp which doesn't prepare
472 * the x87 state, and the C ABI uses a different mode */
473 fpu_save(fpu_state);
474 #endif
476 /* Print the heap stats. */
477 fprintf(file,
478 " Gen StaPg UbSta LaSta Boxed Unbox LB LUB !move Alloc Waste Trig WP GCs Mem-age\n");
480 for (i = 0; i <= SCRATCH_GENERATION; i++) {
481 page_index_t j;
482 page_index_t boxed_cnt = 0;
483 page_index_t unboxed_cnt = 0;
484 page_index_t large_boxed_cnt = 0;
485 page_index_t large_unboxed_cnt = 0;
486 page_index_t pinned_cnt=0;
488 for (j = 0; j < last_free_page; j++)
489 if (page_table[j].gen == i) {
491 /* Count the number of boxed pages within the given
492 * generation. */
493 if (page_boxed_p(j)) {
494 if (page_table[j].large_object)
495 large_boxed_cnt++;
496 else
497 boxed_cnt++;
499 if(page_table[j].dont_move) pinned_cnt++;
500 /* Count the number of unboxed pages within the given
501 * generation. */
502 if (page_unboxed_p(j)) {
503 if (page_table[j].large_object)
504 large_unboxed_cnt++;
505 else
506 unboxed_cnt++;
510 gc_assert(generations[i].bytes_allocated
511 == count_generation_bytes_allocated(i));
512 fprintf(file,
513 " %1d: %5ld %5ld %5ld",
515 (long)generations[i].alloc_start_page,
516 (long)generations[i].alloc_unboxed_start_page,
517 (long)generations[i].alloc_large_start_page);
518 fprintf(file,
519 " %5"PAGE_INDEX_FMT" %5"PAGE_INDEX_FMT" %5"PAGE_INDEX_FMT
520 " %5"PAGE_INDEX_FMT" %5"PAGE_INDEX_FMT,
521 boxed_cnt, unboxed_cnt, large_boxed_cnt,
522 large_unboxed_cnt, pinned_cnt);
523 fprintf(file,
524 " %8"OS_VM_SIZE_FMT
525 " %6"OS_VM_SIZE_FMT
526 " %8"OS_VM_SIZE_FMT
527 " %4"PAGE_INDEX_FMT" %3d %7.4f\n",
528 generations[i].bytes_allocated,
529 (npage_bytes(count_generation_pages(i)) - generations[i].bytes_allocated),
530 generations[i].gc_trigger,
531 count_write_protect_generation_pages(i),
532 generations[i].num_gc,
533 generation_average_age(i));
535 fprintf(file," Total bytes allocated = %"OS_VM_SIZE_FMT"\n", bytes_allocated);
536 fprintf(file," Dynamic-space-size bytes = %"OS_VM_SIZE_FMT"\n", dynamic_space_size);
538 #ifdef LISP_FEATURE_X86
539 fpu_restore(fpu_state);
540 #endif
543 extern void
544 write_heap_exhaustion_report(FILE *file, long available, long requested,
545 struct thread *thread)
547 fprintf(file,
548 "Heap exhausted during %s: %ld bytes available, %ld requested.\n",
549 gc_active_p ? "garbage collection" : "allocation",
550 available,
551 requested);
552 write_generation_stats(file);
553 fprintf(file, "GC control variables:\n");
554 fprintf(file, " *GC-INHIBIT* = %s\n *GC-PENDING* = %s\n",
555 SymbolValue(GC_INHIBIT,thread)==NIL ? "false" : "true",
556 (SymbolValue(GC_PENDING, thread) == T) ?
557 "true" : ((SymbolValue(GC_PENDING, thread) == NIL) ?
558 "false" : "in progress"));
559 #ifdef LISP_FEATURE_SB_THREAD
560 fprintf(file, " *STOP-FOR-GC-PENDING* = %s\n",
561 SymbolValue(STOP_FOR_GC_PENDING,thread)==NIL ? "false" : "true");
562 #endif
565 extern void
566 print_generation_stats(void)
568 write_generation_stats(stderr);
571 extern char* gc_logfile;
572 char * gc_logfile = NULL;
574 extern void
575 log_generation_stats(char *logfile, char *header)
577 if (logfile) {
578 FILE * log = fopen(logfile, "a");
579 if (log) {
580 fprintf(log, "%s\n", header);
581 write_generation_stats(log);
582 fclose(log);
583 } else {
584 fprintf(stderr, "Could not open gc logfile: %s\n", logfile);
585 fflush(stderr);
590 extern void
591 report_heap_exhaustion(long available, long requested, struct thread *th)
593 if (gc_logfile) {
594 FILE * log = fopen(gc_logfile, "a");
595 if (log) {
596 write_heap_exhaustion_report(log, available, requested, th);
597 fclose(log);
598 } else {
599 fprintf(stderr, "Could not open gc logfile: %s\n", gc_logfile);
600 fflush(stderr);
603 /* Always to stderr as well. */
604 write_heap_exhaustion_report(stderr, available, requested, th);
608 #if defined(LISP_FEATURE_X86)
609 void fast_bzero(void*, size_t); /* in <arch>-assem.S */
610 #endif
612 /* Zero the pages from START to END (inclusive), but use mmap/munmap instead
613 * if zeroing it ourselves, i.e. in practice give the memory back to the
614 * OS. Generally done after a large GC.
616 void zero_pages_with_mmap(page_index_t start, page_index_t end) {
617 page_index_t i;
618 void *addr = page_address(start), *new_addr;
619 os_vm_size_t length = npage_bytes(1+end-start);
621 if (start > end)
622 return;
624 gc_assert(length >= gencgc_release_granularity);
625 gc_assert((length % gencgc_release_granularity) == 0);
627 #ifdef LISP_FEATURE_LINUX
628 extern os_vm_address_t anon_dynamic_space_start;
629 // We use MADV_DONTNEED only on Linux due to differing semantics from BSD.
630 // Linux treats it as a demand that the memory be 0-filled, or refreshed
631 // from a file that backs the range. BSD takes it as a hint that you don't
632 // care if the memory has to brought in from swap when next accessed,
633 // i.e. it's not a request to make a user-visible alteration to memory.
634 // So in theory this can bring a page in from the core file, if we happen
635 // to hit a page that resides in the portion of memory mapped by coreparse.
636 // In practice this should not happen because objects from a core file can't
637 // become garbage. Except in save-lisp-and-die they can, and we must be
638 // cautious not to resurrect bytes that originally came from the file.
639 if ((os_vm_address_t)addr >= anon_dynamic_space_start) {
640 if (madvise(addr, length, MADV_DONTNEED) != 0)
641 lose("madvise failed\n");
642 } else
643 #endif
645 os_invalidate(addr, length);
646 new_addr = os_validate(addr, length);
647 if (new_addr == NULL || new_addr != addr) {
648 lose("remap_free_pages: page moved, 0x%08x ==> 0x%08x",
649 start, new_addr);
653 for (i = start; i <= end; i++)
654 set_page_need_to_zero(i, 0);
657 /* Zero the pages from START to END (inclusive). Generally done just after
658 * a new region has been allocated.
660 static void
661 zero_pages(page_index_t start, page_index_t end) {
662 if (start > end)
663 return;
665 #if defined(LISP_FEATURE_X86)
666 fast_bzero(page_address(start), npage_bytes(1+end-start));
667 #else
668 bzero(page_address(start), npage_bytes(1+end-start));
669 #endif
673 static void
674 zero_and_mark_pages(page_index_t start, page_index_t end) {
675 page_index_t i;
677 zero_pages(start, end);
678 for (i = start; i <= end; i++)
679 set_page_need_to_zero(i, 0);
682 /* Zero the pages from START to END (inclusive), except for those
683 * pages that are known to already zeroed. Mark all pages in the
684 * ranges as non-zeroed.
686 static void
687 zero_dirty_pages(page_index_t start, page_index_t end) {
688 page_index_t i, j;
690 for (i = start; i <= end; i++) {
691 if (!page_need_to_zero(i)) continue;
692 for (j = i+1; (j <= end) && page_need_to_zero(j) ; j++)
693 ; /* empty body */
694 zero_pages(i, j-1);
695 i = j;
698 for (i = start; i <= end; i++) {
699 set_page_need_to_zero(i, 1);
705 * To support quick and inline allocation, regions of memory can be
706 * allocated and then allocated from with just a free pointer and a
707 * check against an end address.
709 * Since objects can be allocated to spaces with different properties
710 * e.g. boxed/unboxed, generation, ages; there may need to be many
711 * allocation regions.
713 * Each allocation region may start within a partly used page. Many
714 * features of memory use are noted on a page wise basis, e.g. the
715 * generation; so if a region starts within an existing allocated page
716 * it must be consistent with this page.
718 * During the scavenging of the newspace, objects will be transported
719 * into an allocation region, and pointers updated to point to this
720 * allocation region. It is possible that these pointers will be
721 * scavenged again before the allocation region is closed, e.g. due to
722 * trans_list which jumps all over the place to cleanup the list. It
723 * is important to be able to determine properties of all objects
724 * pointed to when scavenging, e.g to detect pointers to the oldspace.
725 * Thus it's important that the allocation regions have the correct
726 * properties set when allocated, and not just set when closed. The
727 * region allocation routines return regions with the specified
728 * properties, and grab all the pages, setting their properties
729 * appropriately, except that the amount used is not known.
731 * These regions are used to support quicker allocation using just a
732 * free pointer. The actual space used by the region is not reflected
733 * in the pages tables until it is closed. It can't be scavenged until
734 * closed.
736 * When finished with the region it should be closed, which will
737 * update the page tables for the actual space used returning unused
738 * space. Further it may be noted in the new regions which is
739 * necessary when scavenging the newspace.
741 * Large objects may be allocated directly without an allocation
742 * region, the page tables are updated immediately.
744 * Unboxed objects don't contain pointers to other objects and so
745 * don't need scavenging. Further they can't contain pointers to
746 * younger generations so WP is not needed. By allocating pages to
747 * unboxed objects the whole page never needs scavenging or
748 * write-protecting. */
750 /* We use either two or three regions for the current newspace generation. */
751 #ifdef LISP_FEATURE_SEGREGATED_CODE
752 struct alloc_region gc_alloc_regions[3];
753 #define boxed_region gc_alloc_regions[BOXED_PAGE_FLAG-1]
754 #define unboxed_region gc_alloc_regions[UNBOXED_PAGE_FLAG-1]
755 #define code_region gc_alloc_regions[CODE_PAGE_FLAG-1]
756 #else
757 struct alloc_region boxed_region;
758 struct alloc_region unboxed_region;
759 #endif
761 /* The generation currently being allocated to. */
762 static generation_index_t gc_alloc_generation;
764 static inline page_index_t
765 generation_alloc_start_page(generation_index_t generation, int page_type_flag, int large)
767 if (!(page_type_flag >= 1 && page_type_flag <= 3))
768 lose("bad page_type_flag: %d", page_type_flag);
769 if (large)
770 return generations[generation].alloc_large_start_page;
771 #ifdef LISP_FEATURE_SEGREGATED_CODE
772 return generations[generation].alloc_start_page_[page_type_flag];
773 #else
774 if (UNBOXED_PAGE_FLAG == page_type_flag)
775 return generations[generation].alloc_unboxed_start_page;
776 /* Both code and data. */
777 return generations[generation].alloc_start_page;
778 #endif
781 static inline void
782 set_generation_alloc_start_page(generation_index_t generation, int page_type_flag, int large,
783 page_index_t page)
785 if (!(page_type_flag >= 1 && page_type_flag <= 3))
786 lose("bad page_type_flag: %d", page_type_flag);
787 if (large)
788 generations[generation].alloc_large_start_page = page;
789 #ifdef LISP_FEATURE_SEGREGATED_CODE
790 else
791 generations[generation].alloc_start_page_[page_type_flag] = page;
792 #else
793 else if (UNBOXED_PAGE_FLAG == page_type_flag)
794 generations[generation].alloc_unboxed_start_page = page;
795 else /* Both code and data. */
796 generations[generation].alloc_start_page = page;
797 #endif
800 /* Find a new region with room for at least the given number of bytes.
802 * It starts looking at the current generation's alloc_start_page. So
803 * may pick up from the previous region if there is enough space. This
804 * keeps the allocation contiguous when scavenging the newspace.
806 * The alloc_region should have been closed by a call to
807 * gc_alloc_update_page_tables(), and will thus be in an empty state.
809 * To assist the scavenging functions write-protected pages are not
810 * used. Free pages should not be write-protected.
812 * It is critical to the conservative GC that the start of regions be
813 * known. To help achieve this only small regions are allocated at a
814 * time.
816 * During scavenging, pointers may be found to within the current
817 * region and the page generation must be set so that pointers to the
818 * from space can be recognized. Therefore the generation of pages in
819 * the region are set to gc_alloc_generation. To prevent another
820 * allocation call using the same pages, all the pages in the region
821 * are allocated, although they will initially be empty.
823 static void
824 gc_alloc_new_region(sword_t nbytes, int page_type_flag, struct alloc_region *alloc_region)
826 page_index_t first_page;
827 page_index_t last_page;
828 page_index_t i;
829 int ret;
832 FSHOW((stderr,
833 "/alloc_new_region for %d bytes from gen %d\n",
834 nbytes, gc_alloc_generation));
837 /* Check that the region is in a reset state. */
838 gc_assert((alloc_region->first_page == 0)
839 && (alloc_region->last_page == -1)
840 && (alloc_region->free_pointer == alloc_region->end_addr));
841 ret = thread_mutex_lock(&free_pages_lock);
842 gc_assert(ret == 0);
843 first_page = generation_alloc_start_page(gc_alloc_generation, page_type_flag, 0);
844 last_page=gc_find_freeish_pages(&first_page, nbytes, page_type_flag);
846 /* Set up the alloc_region. */
847 alloc_region->first_page = first_page;
848 alloc_region->last_page = last_page;
849 alloc_region->start_addr = page_address(first_page) + page_bytes_used(first_page);
850 alloc_region->free_pointer = alloc_region->start_addr;
851 alloc_region->end_addr = page_address(last_page+1);
853 /* Set up the pages. */
855 /* The first page may have already been in use. */
856 /* If so, just assert that it's consistent, otherwise, set it up. */
857 if (page_bytes_used(first_page)) {
858 gc_assert(page_table[first_page].allocated == page_type_flag);
859 gc_assert(page_table[first_page].gen == gc_alloc_generation);
860 gc_assert(page_table[first_page].large_object == 0);
861 } else {
862 page_table[first_page].allocated = page_type_flag;
863 page_table[first_page].gen = gc_alloc_generation;
864 page_table[first_page].large_object = 0;
865 set_page_scan_start_offset(first_page, 0);
867 page_table[first_page].allocated |= OPEN_REGION_PAGE_FLAG;
869 for (i = first_page+1; i <= last_page; i++) {
870 page_table[i].allocated = page_type_flag;
871 page_table[i].gen = gc_alloc_generation;
872 page_table[i].large_object = 0;
873 /* This may not be necessary for unboxed regions (think it was
874 * broken before!) */
875 set_page_scan_start_offset(i,
876 addr_diff(page_address(i), alloc_region->start_addr));
877 page_table[i].allocated |= OPEN_REGION_PAGE_FLAG;
879 /* Bump up last_free_page. */
880 if (last_page+1 > last_free_page) {
881 last_free_page = last_page+1;
882 /* do we only want to call this on special occasions? like for
883 * boxed_region? */
884 set_alloc_pointer((lispobj)page_address(last_free_page));
886 ret = thread_mutex_unlock(&free_pages_lock);
887 gc_assert(ret == 0);
889 #ifdef READ_PROTECT_FREE_PAGES
890 os_protect(page_address(first_page),
891 npage_bytes(1+last_page-first_page),
892 OS_VM_PROT_ALL);
893 #endif
895 /* If the first page was only partial, don't check whether it's
896 * zeroed (it won't be) and don't zero it (since the parts that
897 * we're interested in are guaranteed to be zeroed).
899 if (page_bytes_used(first_page)) {
900 first_page++;
903 zero_dirty_pages(first_page, last_page);
905 /* we can do this after releasing free_pages_lock */
906 if (gencgc_zero_check) {
907 word_t *p;
908 for (p = (word_t *)alloc_region->start_addr;
909 p < (word_t *)alloc_region->end_addr; p++) {
910 if (*p != 0) {
911 lose("The new region is not zero at %p (start=%p, end=%p).\n",
912 p, alloc_region->start_addr, alloc_region->end_addr);
918 /* If the record_new_objects flag is 2 then all new regions created
919 * are recorded.
921 * If it's 1 then then it is only recorded if the first page of the
922 * current region is <= new_areas_ignore_page. This helps avoid
923 * unnecessary recording when doing full scavenge pass.
925 * The new_object structure holds the page, byte offset, and size of
926 * new regions of objects. Each new area is placed in the array of
927 * these structures pointer to by new_areas. new_areas_index holds the
928 * offset into new_areas.
930 * If new_area overflows NUM_NEW_AREAS then it stops adding them. The
931 * later code must detect this and handle it, probably by doing a full
932 * scavenge of a generation. */
933 #define NUM_NEW_AREAS 512
934 static int record_new_objects = 0;
935 static page_index_t new_areas_ignore_page;
936 struct new_area {
937 page_index_t page;
938 size_t offset;
939 size_t size;
941 static struct new_area (*new_areas)[];
942 static size_t new_areas_index;
943 size_t max_new_areas;
945 /* Add a new area to new_areas. */
946 static void
947 add_new_area(page_index_t first_page, size_t offset, size_t size)
949 size_t new_area_start, c;
950 ssize_t i;
952 /* Ignore if full. */
953 if (new_areas_index >= NUM_NEW_AREAS)
954 return;
956 switch (record_new_objects) {
957 case 0:
958 return;
959 case 1:
960 if (first_page > new_areas_ignore_page)
961 return;
962 break;
963 case 2:
964 break;
965 default:
966 gc_abort();
969 new_area_start = npage_bytes(first_page) + offset;
971 /* Search backwards for a prior area that this follows from. If
972 found this will save adding a new area. */
973 for (i = new_areas_index-1, c = 0; (i >= 0) && (c < 8); i--, c++) {
974 size_t area_end =
975 npage_bytes((*new_areas)[i].page)
976 + (*new_areas)[i].offset
977 + (*new_areas)[i].size;
978 /*FSHOW((stderr,
979 "/add_new_area S1 %d %d %d %d\n",
980 i, c, new_area_start, area_end));*/
981 if (new_area_start == area_end) {
982 /*FSHOW((stderr,
983 "/adding to [%d] %d %d %d with %d %d %d:\n",
985 (*new_areas)[i].page,
986 (*new_areas)[i].offset,
987 (*new_areas)[i].size,
988 first_page,
989 offset,
990 size);*/
991 (*new_areas)[i].size += size;
992 return;
996 (*new_areas)[new_areas_index].page = first_page;
997 (*new_areas)[new_areas_index].offset = offset;
998 (*new_areas)[new_areas_index].size = size;
999 /*FSHOW((stderr,
1000 "/new_area %d page %d offset %d size %d\n",
1001 new_areas_index, first_page, offset, size));*/
1002 new_areas_index++;
1004 /* Note the max new_areas used. */
1005 if (new_areas_index > max_new_areas)
1006 max_new_areas = new_areas_index;
1009 /* Update the tables for the alloc_region. The region may be added to
1010 * the new_areas.
1012 * When done the alloc_region is set up so that the next quick alloc
1013 * will fail safely and thus a new region will be allocated. Further
1014 * it is safe to try to re-update the page table of this reset
1015 * alloc_region. */
1016 void
1017 gc_alloc_update_page_tables(int page_type_flag, struct alloc_region *alloc_region)
1019 boolean more;
1020 page_index_t first_page;
1021 page_index_t next_page;
1022 os_vm_size_t bytes_used;
1023 os_vm_size_t region_size;
1024 os_vm_size_t byte_cnt;
1025 page_bytes_t orig_first_page_bytes_used;
1026 int ret;
1029 first_page = alloc_region->first_page;
1031 /* Catch an unused alloc_region. */
1032 if ((first_page == 0) && (alloc_region->last_page == -1))
1033 return;
1035 next_page = first_page+1;
1037 ret = thread_mutex_lock(&free_pages_lock);
1038 gc_assert(ret == 0);
1039 if (alloc_region->free_pointer != alloc_region->start_addr) {
1040 /* some bytes were allocated in the region */
1041 orig_first_page_bytes_used = page_bytes_used(first_page);
1043 gc_assert(alloc_region->start_addr ==
1044 (page_address(first_page) + page_bytes_used(first_page)));
1046 /* All the pages used need to be updated */
1048 /* Update the first page. */
1050 /* If the page was free then set up the gen, and
1051 * scan_start_offset. */
1052 if (page_bytes_used(first_page) == 0)
1053 gc_assert(page_starts_contiguous_block_p(first_page));
1054 page_table[first_page].allocated &= ~(OPEN_REGION_PAGE_FLAG);
1056 #ifdef LISP_FEATURE_SEGREGATED_CODE
1057 gc_assert(page_table[first_page].allocated == page_type_flag);
1058 #else
1059 gc_assert(page_table[first_page].allocated & page_type_flag);
1060 #endif
1061 gc_assert(page_table[first_page].gen == gc_alloc_generation);
1062 gc_assert(page_table[first_page].large_object == 0);
1064 byte_cnt = 0;
1066 /* Calculate the number of bytes used in this page. This is not
1067 * always the number of new bytes, unless it was free. */
1068 more = 0;
1069 if ((bytes_used = addr_diff(alloc_region->free_pointer,
1070 page_address(first_page)))
1071 >GENCGC_CARD_BYTES) {
1072 bytes_used = GENCGC_CARD_BYTES;
1073 more = 1;
1075 set_page_bytes_used(first_page, bytes_used);
1076 byte_cnt += bytes_used;
1079 /* All the rest of the pages should be free. We need to set
1080 * their scan_start_offset pointer to the start of the
1081 * region, and set the bytes_used. */
1082 while (more) {
1083 page_table[next_page].allocated &= ~(OPEN_REGION_PAGE_FLAG);
1084 #ifdef LISP_FEATURE_SEGREGATED_CODE
1085 gc_assert(page_table[next_page].allocated == page_type_flag);
1086 #else
1087 gc_assert(page_table[next_page].allocated & page_type_flag);
1088 #endif
1089 gc_assert(page_bytes_used(next_page) == 0);
1090 gc_assert(page_table[next_page].gen == gc_alloc_generation);
1091 gc_assert(page_table[next_page].large_object == 0);
1092 gc_assert(page_scan_start_offset(next_page) ==
1093 addr_diff(page_address(next_page),
1094 alloc_region->start_addr));
1096 /* Calculate the number of bytes used in this page. */
1097 more = 0;
1098 if ((bytes_used = addr_diff(alloc_region->free_pointer,
1099 page_address(next_page)))>GENCGC_CARD_BYTES) {
1100 bytes_used = GENCGC_CARD_BYTES;
1101 more = 1;
1103 set_page_bytes_used(next_page, bytes_used);
1104 byte_cnt += bytes_used;
1106 next_page++;
1109 region_size = addr_diff(alloc_region->free_pointer,
1110 alloc_region->start_addr);
1111 bytes_allocated += region_size;
1112 generations[gc_alloc_generation].bytes_allocated += region_size;
1114 gc_assert((byte_cnt- orig_first_page_bytes_used) == region_size);
1116 /* Set the generations alloc restart page to the last page of
1117 * the region. */
1118 set_generation_alloc_start_page(gc_alloc_generation, page_type_flag, 0, next_page-1);
1120 /* Add the region to the new_areas if requested. */
1121 if (BOXED_PAGE_FLAG & page_type_flag)
1122 add_new_area(first_page,orig_first_page_bytes_used, region_size);
1125 FSHOW((stderr,
1126 "/gc_alloc_update_page_tables update %d bytes to gen %d\n",
1127 region_size,
1128 gc_alloc_generation));
1130 } else {
1131 /* There are no bytes allocated. Unallocate the first_page if
1132 * there are 0 bytes_used. */
1133 page_table[first_page].allocated &= ~(OPEN_REGION_PAGE_FLAG);
1134 if (page_bytes_used(first_page) == 0)
1135 page_table[first_page].allocated = FREE_PAGE_FLAG;
1138 /* Unallocate any unused pages. */
1139 while (next_page <= alloc_region->last_page) {
1140 gc_assert(page_bytes_used(next_page) == 0);
1141 page_table[next_page].allocated = FREE_PAGE_FLAG;
1142 next_page++;
1144 ret = thread_mutex_unlock(&free_pages_lock);
1145 gc_assert(ret == 0);
1147 /* alloc_region is per-thread, we're ok to do this unlocked */
1148 gc_set_region_empty(alloc_region);
1151 /* Allocate a possibly large object. */
1152 void *
1153 gc_alloc_large(sword_t nbytes, int page_type_flag, struct alloc_region *alloc_region)
1155 boolean more;
1156 page_index_t first_page, next_page, last_page;
1157 os_vm_size_t byte_cnt;
1158 os_vm_size_t bytes_used;
1159 int ret;
1161 ret = thread_mutex_lock(&free_pages_lock);
1162 gc_assert(ret == 0);
1164 first_page = generation_alloc_start_page(gc_alloc_generation, page_type_flag, 1);
1165 // FIXME: really we want to try looking for space following the highest of
1166 // the last page of all other small object regions. That's impossible - there's
1167 // not enough information. At best we can skip some work in only the case where
1168 // the supplied region was the one most recently created. To do this right
1169 // would entail a malloc-like allocator at the page granularity.
1170 if (first_page <= alloc_region->last_page) {
1171 first_page = alloc_region->last_page+1;
1174 last_page=gc_find_freeish_pages(&first_page,nbytes, page_type_flag);
1176 gc_assert(first_page > alloc_region->last_page);
1178 set_generation_alloc_start_page(gc_alloc_generation, page_type_flag, 1, last_page);
1180 /* Large objects don't share pages with other objects. */
1181 gc_assert(page_bytes_used(first_page) == 0);
1183 /* Set up the pages. */
1184 page_table[first_page].allocated = page_type_flag;
1185 page_table[first_page].gen = gc_alloc_generation;
1186 page_table[first_page].large_object = 1;
1187 set_page_scan_start_offset(first_page, 0);
1189 byte_cnt = 0;
1191 /* Calc. the number of bytes used in this page. This is not
1192 * always the number of new bytes, unless it was free. */
1193 more = 0;
1194 if ((bytes_used = nbytes) > GENCGC_CARD_BYTES) {
1195 bytes_used = GENCGC_CARD_BYTES;
1196 more = 1;
1198 set_page_bytes_used(first_page, bytes_used);
1199 byte_cnt += bytes_used;
1201 next_page = first_page+1;
1203 /* All the rest of the pages should be free. We need to set their
1204 * scan_start_offset pointer to the start of the region, and set
1205 * the bytes_used. */
1206 while (more) {
1207 gc_assert(page_free_p(next_page));
1208 gc_assert(page_bytes_used(next_page) == 0);
1209 page_table[next_page].allocated = page_type_flag;
1210 page_table[next_page].gen = gc_alloc_generation;
1211 page_table[next_page].large_object = 1;
1213 set_page_scan_start_offset(next_page, npage_bytes(next_page-first_page));
1215 /* Calculate the number of bytes used in this page. */
1216 more = 0;
1217 bytes_used = nbytes - byte_cnt;
1218 if (bytes_used > GENCGC_CARD_BYTES) {
1219 bytes_used = GENCGC_CARD_BYTES;
1220 more = 1;
1222 set_page_bytes_used(next_page, bytes_used);
1223 page_table[next_page].write_protected=0;
1224 page_table[next_page].dont_move=0;
1225 byte_cnt += bytes_used;
1226 next_page++;
1229 gc_assert(byte_cnt == (size_t)nbytes);
1231 bytes_allocated += nbytes;
1232 generations[gc_alloc_generation].bytes_allocated += nbytes;
1234 /* Add the region to the new_areas if requested. */
1235 if (BOXED_PAGE_FLAG & page_type_flag)
1236 add_new_area(first_page, 0, nbytes);
1238 /* Bump up last_free_page */
1239 if (last_page+1 > last_free_page) {
1240 last_free_page = last_page+1;
1241 set_alloc_pointer((lispobj)(page_address(last_free_page)));
1243 ret = thread_mutex_unlock(&free_pages_lock);
1244 gc_assert(ret == 0);
1246 #ifdef READ_PROTECT_FREE_PAGES
1247 os_protect(page_address(first_page),
1248 npage_bytes(1+last_page-first_page),
1249 OS_VM_PROT_ALL);
1250 #endif
1252 zero_dirty_pages(first_page, last_page);
1254 return page_address(first_page);
1257 static page_index_t gencgc_alloc_start_page = -1;
1259 void
1260 gc_heap_exhausted_error_or_lose (sword_t available, sword_t requested)
1262 struct thread *thread = arch_os_get_current_thread();
1263 /* Write basic information before doing anything else: if we don't
1264 * call to lisp this is a must, and even if we do there is always
1265 * the danger that we bounce back here before the error has been
1266 * handled, or indeed even printed.
1268 report_heap_exhaustion(available, requested, thread);
1269 if (gc_active_p || (available == 0)) {
1270 /* If we are in GC, or totally out of memory there is no way
1271 * to sanely transfer control to the lisp-side of things.
1273 lose("Heap exhausted, game over.");
1275 else {
1276 /* FIXME: assert free_pages_lock held */
1277 (void)thread_mutex_unlock(&free_pages_lock);
1278 #if !(defined(LISP_FEATURE_WIN32) && defined(LISP_FEATURE_SB_THREAD))
1279 gc_assert(get_pseudo_atomic_atomic(thread));
1280 clear_pseudo_atomic_atomic(thread);
1281 if (get_pseudo_atomic_interrupted(thread))
1282 do_pending_interrupt();
1283 #endif
1284 /* Another issue is that signalling HEAP-EXHAUSTED error leads
1285 * to running user code at arbitrary places, even in a
1286 * WITHOUT-INTERRUPTS which may lead to a deadlock without
1287 * running out of the heap. So at this point all bets are
1288 * off. */
1289 if (SymbolValue(INTERRUPTS_ENABLED,thread) == NIL)
1290 corruption_warning_and_maybe_lose
1291 ("Signalling HEAP-EXHAUSTED in a WITHOUT-INTERRUPTS.");
1292 /* available and requested should be double word aligned, thus
1293 they can passed as fixnums and shifted later. */
1294 funcall2(StaticSymbolFunction(HEAP_EXHAUSTED_ERROR), available, requested);
1295 lose("HEAP-EXHAUSTED-ERROR fell through");
1299 page_index_t
1300 gc_find_freeish_pages(page_index_t *restart_page_ptr, sword_t bytes,
1301 int page_type_flag)
1303 page_index_t most_bytes_found_from = 0, most_bytes_found_to = 0;
1304 page_index_t first_page, last_page, restart_page = *restart_page_ptr;
1305 os_vm_size_t nbytes = bytes;
1306 os_vm_size_t nbytes_goal = nbytes;
1307 os_vm_size_t bytes_found = 0;
1308 os_vm_size_t most_bytes_found = 0;
1309 boolean small_object = nbytes < GENCGC_CARD_BYTES;
1310 /* FIXME: assert(free_pages_lock is held); */
1312 if (nbytes_goal < gencgc_alloc_granularity)
1313 nbytes_goal = gencgc_alloc_granularity;
1315 /* Toggled by gc_and_save for heap compaction, normally -1. */
1316 if (gencgc_alloc_start_page != -1) {
1317 restart_page = gencgc_alloc_start_page;
1320 /* FIXME: This is on bytes instead of nbytes pending cleanup of
1321 * long from the interface. */
1322 gc_assert(bytes>=0);
1323 /* Search for a page with at least nbytes of space. We prefer
1324 * not to split small objects on multiple pages, to reduce the
1325 * number of contiguous allocation regions spaning multiple
1326 * pages: this helps avoid excessive conservativism.
1328 * For other objects, we guarantee that they start on their own
1329 * page boundary.
1331 first_page = restart_page;
1332 while (first_page < page_table_pages) {
1333 bytes_found = 0;
1334 if (page_free_p(first_page)) {
1335 gc_assert(0 == page_bytes_used(first_page));
1336 bytes_found = GENCGC_CARD_BYTES;
1337 } else if (small_object &&
1338 (page_table[first_page].allocated == page_type_flag) &&
1339 (page_table[first_page].large_object == 0) &&
1340 (page_table[first_page].gen == gc_alloc_generation) &&
1341 (page_table[first_page].write_protected == 0) &&
1342 (page_table[first_page].dont_move == 0)) {
1343 bytes_found = GENCGC_CARD_BYTES - page_bytes_used(first_page);
1344 if (bytes_found < nbytes) {
1345 if (bytes_found > most_bytes_found)
1346 most_bytes_found = bytes_found;
1347 first_page++;
1348 continue;
1350 } else {
1351 first_page++;
1352 continue;
1355 gc_assert(page_table[first_page].write_protected == 0);
1356 for (last_page = first_page+1;
1357 ((last_page < page_table_pages) &&
1358 page_free_p(last_page) &&
1359 (bytes_found < nbytes_goal));
1360 last_page++) {
1361 bytes_found += GENCGC_CARD_BYTES;
1362 gc_assert(0 == page_bytes_used(last_page));
1363 gc_assert(0 == page_table[last_page].write_protected);
1366 if (bytes_found > most_bytes_found) {
1367 most_bytes_found = bytes_found;
1368 most_bytes_found_from = first_page;
1369 most_bytes_found_to = last_page;
1371 if (bytes_found >= nbytes_goal)
1372 break;
1374 first_page = last_page;
1377 bytes_found = most_bytes_found;
1378 restart_page = first_page + 1;
1380 /* Check for a failure */
1381 if (bytes_found < nbytes) {
1382 gc_assert(restart_page >= page_table_pages);
1383 gc_heap_exhausted_error_or_lose(most_bytes_found, nbytes);
1386 gc_assert(most_bytes_found_to);
1387 *restart_page_ptr = most_bytes_found_from;
1388 return most_bytes_found_to-1;
1391 /* Allocate bytes. All the rest of the special-purpose allocation
1392 * functions will eventually call this */
1394 void *
1395 gc_alloc_with_region(sword_t nbytes,int page_type_flag, struct alloc_region *my_region,
1396 int quick_p)
1398 void *new_free_pointer;
1400 if (nbytes>=LARGE_OBJECT_SIZE)
1401 return gc_alloc_large(nbytes, page_type_flag, my_region);
1403 /* Check whether there is room in the current alloc region. */
1404 new_free_pointer = (char*)my_region->free_pointer + nbytes;
1406 /* fprintf(stderr, "alloc %d bytes from %p to %p\n", nbytes,
1407 my_region->free_pointer, new_free_pointer); */
1409 if (new_free_pointer <= my_region->end_addr) {
1410 /* If so then allocate from the current alloc region. */
1411 void *new_obj = my_region->free_pointer;
1412 my_region->free_pointer = new_free_pointer;
1414 /* Unless a `quick' alloc was requested, check whether the
1415 alloc region is almost empty. */
1416 if (!quick_p &&
1417 addr_diff(my_region->end_addr,my_region->free_pointer) <= 32) {
1418 /* If so, finished with the current region. */
1419 gc_alloc_update_page_tables(page_type_flag, my_region);
1420 /* Set up a new region. */
1421 gc_alloc_new_region(32 /*bytes*/, page_type_flag, my_region);
1424 return((void *)new_obj);
1427 /* Else not enough free space in the current region: retry with a
1428 * new region. */
1430 gc_alloc_update_page_tables(page_type_flag, my_region);
1431 gc_alloc_new_region(nbytes, page_type_flag, my_region);
1432 return gc_alloc_with_region(nbytes, page_type_flag, my_region,0);
1435 /* Copy a large object. If the object is in a large object region then
1436 * it is simply promoted, else it is copied. If it's large enough then
1437 * it's copied to a large object region.
1439 * Bignums and vectors may have shrunk. If the object is not copied
1440 * the space needs to be reclaimed, and the page_tables corrected. */
1441 static lispobj
1442 general_copy_large_object(lispobj object, word_t nwords, boolean boxedp)
1444 lispobj *new;
1445 page_index_t first_page;
1447 CHECK_COPY_PRECONDITIONS(object, nwords);
1449 if ((nwords > 1024*1024) && gencgc_verbose) {
1450 FSHOW((stderr, "/general_copy_large_object: %d bytes\n",
1451 nwords*N_WORD_BYTES));
1454 /* Check whether it's a large object. */
1455 first_page = find_page_index((void *)object);
1456 gc_assert(first_page >= 0);
1458 if (page_table[first_page].large_object) {
1459 /* Promote the object. Note: Unboxed objects may have been
1460 * allocated to a BOXED region so it may be necessary to
1461 * change the region to UNBOXED. */
1462 os_vm_size_t remaining_bytes;
1463 os_vm_size_t bytes_freed;
1464 page_index_t next_page;
1465 page_bytes_t old_bytes_used;
1467 /* FIXME: This comment is somewhat stale.
1469 * Note: Any page write-protection must be removed, else a
1470 * later scavenge_newspace may incorrectly not scavenge these
1471 * pages. This would not be necessary if they are added to the
1472 * new areas, but let's do it for them all (they'll probably
1473 * be written anyway?). */
1475 gc_assert(page_starts_contiguous_block_p(first_page));
1476 next_page = first_page;
1477 remaining_bytes = nwords*N_WORD_BYTES;
1479 while (remaining_bytes > GENCGC_CARD_BYTES) {
1480 gc_assert(page_table[next_page].gen == from_space);
1481 gc_assert(page_table[next_page].large_object);
1482 gc_assert(page_scan_start_offset(next_page) ==
1483 npage_bytes(next_page-first_page));
1484 gc_assert(page_bytes_used(next_page) == GENCGC_CARD_BYTES);
1485 /* Should have been unprotected by unprotect_oldspace()
1486 * for boxed objects, and after promotion unboxed ones
1487 * should not be on protected pages at all. */
1488 gc_assert(!page_table[next_page].write_protected);
1490 if (boxedp)
1491 gc_assert(page_boxed_p(next_page));
1492 else {
1493 gc_assert(page_allocated_no_region_p(next_page));
1494 page_table[next_page].allocated = UNBOXED_PAGE_FLAG;
1496 page_table[next_page].gen = new_space;
1498 remaining_bytes -= GENCGC_CARD_BYTES;
1499 next_page++;
1502 /* Now only one page remains, but the object may have shrunk so
1503 * there may be more unused pages which will be freed. */
1505 /* Object may have shrunk but shouldn't have grown - check. */
1506 gc_assert(page_bytes_used(next_page) >= remaining_bytes);
1508 page_table[next_page].gen = new_space;
1510 if (boxedp)
1511 gc_assert(page_boxed_p(next_page));
1512 else
1513 page_table[next_page].allocated = UNBOXED_PAGE_FLAG;
1515 /* Adjust the bytes_used. */
1516 old_bytes_used = page_bytes_used(next_page);
1517 set_page_bytes_used(next_page, remaining_bytes);
1519 bytes_freed = old_bytes_used - remaining_bytes;
1521 /* Free any remaining pages; needs care. */
1522 next_page++;
1523 while ((old_bytes_used == GENCGC_CARD_BYTES) &&
1524 (page_table[next_page].gen == from_space) &&
1525 /* FIXME: It is not obvious to me why this is necessary
1526 * as a loop condition: it seems to me that the
1527 * scan_start_offset test should be sufficient, but
1528 * experimentally that is not the case. --NS
1529 * 2011-11-28 */
1530 (boxedp ?
1531 page_boxed_p(next_page) :
1532 page_allocated_no_region_p(next_page)) &&
1533 page_table[next_page].large_object &&
1534 (page_scan_start_offset(next_page) ==
1535 npage_bytes(next_page - first_page))) {
1536 /* Checks out OK, free the page. Don't need to both zeroing
1537 * pages as this should have been done before shrinking the
1538 * object. These pages shouldn't be write-protected, even if
1539 * boxed they should be zero filled. */
1540 gc_assert(page_table[next_page].write_protected == 0);
1542 old_bytes_used = page_bytes_used(next_page);
1543 page_table[next_page].allocated = FREE_PAGE_FLAG;
1544 set_page_bytes_used(next_page, 0);
1545 bytes_freed += old_bytes_used;
1546 next_page++;
1549 if ((bytes_freed > 0) && gencgc_verbose) {
1550 FSHOW((stderr,
1551 "/general_copy_large_object bytes_freed=%"OS_VM_SIZE_FMT"\n",
1552 bytes_freed));
1555 generations[from_space].bytes_allocated -= nwords*N_WORD_BYTES
1556 + bytes_freed;
1557 generations[new_space].bytes_allocated += nwords*N_WORD_BYTES;
1558 bytes_allocated -= bytes_freed;
1560 /* Add the region to the new_areas if requested. */
1561 if (boxedp)
1562 add_new_area(first_page,0,nwords*N_WORD_BYTES);
1564 return(object);
1566 } else {
1567 /* Allocate space. */
1568 new = gc_general_alloc(nwords*N_WORD_BYTES,
1569 (boxedp ? BOXED_PAGE_FLAG : UNBOXED_PAGE_FLAG),
1570 ALLOC_QUICK);
1572 /* Copy the object. */
1573 memcpy(new,native_pointer(object),nwords*N_WORD_BYTES);
1575 /* Return Lisp pointer of new object. */
1576 return make_lispobj(new, lowtag_of(object));
1580 lispobj
1581 copy_large_object(lispobj object, sword_t nwords)
1583 return general_copy_large_object(object, nwords, 1);
1586 lispobj
1587 copy_large_unboxed_object(lispobj object, sword_t nwords)
1589 return general_copy_large_object(object, nwords, 0);
1592 /* to copy unboxed objects */
1593 lispobj
1594 copy_unboxed_object(lispobj object, sword_t nwords)
1596 return gc_general_copy_object(object, nwords, UNBOXED_PAGE_FLAG);
1601 * code and code-related objects
1604 static lispobj trans_fun_header(lispobj object);
1605 static lispobj trans_boxed(lispobj object);
1608 /* Scan a x86 compiled code object, looking for possible fixups that
1609 * have been missed after a move.
1611 * Two types of fixups are needed:
1612 * 1. Absolute fixups to within the code object.
1613 * 2. Relative fixups to outside the code object.
1615 * Currently only absolute fixups to the constant vector, or to the
1616 * code area are checked. */
1617 #ifdef LISP_FEATURE_X86
1618 void
1619 sniff_code_object(struct code *code, os_vm_size_t displacement)
1621 sword_t nheader_words, ncode_words, nwords;
1622 os_vm_address_t constants_start_addr = NULL, constants_end_addr, p;
1623 os_vm_address_t code_start_addr, code_end_addr;
1624 os_vm_address_t code_addr = (os_vm_address_t)code;
1625 int fixup_found = 0;
1627 if (!check_code_fixups)
1628 return;
1630 FSHOW((stderr, "/sniffing code: %p, %lu\n", code, displacement));
1632 ncode_words = code_instruction_words(code->code_size);
1633 nheader_words = code_header_words(*(lispobj *)code);
1634 nwords = ncode_words + nheader_words;
1636 constants_start_addr = code_addr + 5*N_WORD_BYTES;
1637 constants_end_addr = code_addr + nheader_words*N_WORD_BYTES;
1638 code_start_addr = code_addr + nheader_words*N_WORD_BYTES;
1639 code_end_addr = code_addr + nwords*N_WORD_BYTES;
1641 /* Work through the unboxed code. */
1642 for (p = code_start_addr; p < code_end_addr; p++) {
1643 void *data = *(void **)p;
1644 unsigned d1 = *((unsigned char *)p - 1);
1645 unsigned d2 = *((unsigned char *)p - 2);
1646 unsigned d3 = *((unsigned char *)p - 3);
1647 unsigned d4 = *((unsigned char *)p - 4);
1648 #if QSHOW
1649 unsigned d5 = *((unsigned char *)p - 5);
1650 unsigned d6 = *((unsigned char *)p - 6);
1651 #endif
1653 /* Check for code references. */
1654 /* Check for a 32 bit word that looks like an absolute
1655 reference to within the code adea of the code object. */
1656 if ((data >= (void*)(code_start_addr-displacement))
1657 && (data < (void*)(code_end_addr-displacement))) {
1658 /* function header */
1659 if ((d4 == 0x5e)
1660 && (((unsigned)p - 4 - 4*HeaderValue(*((unsigned *)p-1))) ==
1661 (unsigned)code)) {
1662 /* Skip the function header */
1663 p += 6*4 - 4 - 1;
1664 continue;
1666 /* the case of PUSH imm32 */
1667 if (d1 == 0x68) {
1668 fixup_found = 1;
1669 FSHOW((stderr,
1670 "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1671 p, d6, d5, d4, d3, d2, d1, data));
1672 FSHOW((stderr, "/PUSH $0x%.8x\n", data));
1674 /* the case of MOV [reg-8],imm32 */
1675 if ((d3 == 0xc7)
1676 && (d2==0x40 || d2==0x41 || d2==0x42 || d2==0x43
1677 || d2==0x45 || d2==0x46 || d2==0x47)
1678 && (d1 == 0xf8)) {
1679 fixup_found = 1;
1680 FSHOW((stderr,
1681 "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1682 p, d6, d5, d4, d3, d2, d1, data));
1683 FSHOW((stderr, "/MOV [reg-8],$0x%.8x\n", data));
1685 /* the case of LEA reg,[disp32] */
1686 if ((d2 == 0x8d) && ((d1 & 0xc7) == 5)) {
1687 fixup_found = 1;
1688 FSHOW((stderr,
1689 "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1690 p, d6, d5, d4, d3, d2, d1, data));
1691 FSHOW((stderr,"/LEA reg,[$0x%.8x]\n", data));
1695 /* Check for constant references. */
1696 /* Check for a 32 bit word that looks like an absolute
1697 reference to within the constant vector. Constant references
1698 will be aligned. */
1699 if ((data >= (void*)(constants_start_addr-displacement))
1700 && (data < (void*)(constants_end_addr-displacement))
1701 && (((unsigned)data & 0x3) == 0)) {
1702 /* Mov eax,m32 */
1703 if (d1 == 0xa1) {
1704 fixup_found = 1;
1705 FSHOW((stderr,
1706 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1707 p, d6, d5, d4, d3, d2, d1, data));
1708 FSHOW((stderr,"/MOV eax,0x%.8x\n", data));
1711 /* the case of MOV m32,EAX */
1712 if (d1 == 0xa3) {
1713 fixup_found = 1;
1714 FSHOW((stderr,
1715 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1716 p, d6, d5, d4, d3, d2, d1, data));
1717 FSHOW((stderr, "/MOV 0x%.8x,eax\n", data));
1720 /* the case of CMP m32,imm32 */
1721 if ((d1 == 0x3d) && (d2 == 0x81)) {
1722 fixup_found = 1;
1723 FSHOW((stderr,
1724 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1725 p, d6, d5, d4, d3, d2, d1, data));
1726 /* XX Check this */
1727 FSHOW((stderr, "/CMP 0x%.8x,immed32\n", data));
1730 /* Check for a mod=00, r/m=101 byte. */
1731 if ((d1 & 0xc7) == 5) {
1732 /* Cmp m32,reg */
1733 if (d2 == 0x39) {
1734 fixup_found = 1;
1735 FSHOW((stderr,
1736 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1737 p, d6, d5, d4, d3, d2, d1, data));
1738 FSHOW((stderr,"/CMP 0x%.8x,reg\n", data));
1740 /* the case of CMP reg32,m32 */
1741 if (d2 == 0x3b) {
1742 fixup_found = 1;
1743 FSHOW((stderr,
1744 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1745 p, d6, d5, d4, d3, d2, d1, data));
1746 FSHOW((stderr, "/CMP reg32,0x%.8x\n", data));
1748 /* the case of MOV m32,reg32 */
1749 if (d2 == 0x89) {
1750 fixup_found = 1;
1751 FSHOW((stderr,
1752 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1753 p, d6, d5, d4, d3, d2, d1, data));
1754 FSHOW((stderr, "/MOV 0x%.8x,reg32\n", data));
1756 /* the case of MOV reg32,m32 */
1757 if (d2 == 0x8b) {
1758 fixup_found = 1;
1759 FSHOW((stderr,
1760 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1761 p, d6, d5, d4, d3, d2, d1, data));
1762 FSHOW((stderr, "/MOV reg32,0x%.8x\n", data));
1764 /* the case of LEA reg32,m32 */
1765 if (d2 == 0x8d) {
1766 fixup_found = 1;
1767 FSHOW((stderr,
1768 "abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1769 p, d6, d5, d4, d3, d2, d1, data));
1770 FSHOW((stderr, "/LEA reg32,0x%.8x\n", data));
1776 /* If anything was found, print some information on the code
1777 * object. */
1778 if (fixup_found) {
1779 FSHOW((stderr,
1780 "/compiled code object at %x: header words = %d, code words = %d\n",
1781 code, nheader_words, ncode_words));
1782 FSHOW((stderr,
1783 "/const start = %x, end = %x\n",
1784 constants_start_addr, constants_end_addr));
1785 FSHOW((stderr,
1786 "/code start = %x, end = %x\n",
1787 code_start_addr, code_end_addr));
1790 #endif
1792 #ifdef LISP_FEATURE_X86
1793 void
1794 gencgc_apply_code_fixups(struct code *old_code, struct code *new_code)
1796 sword_t nheader_words, ncode_words, nwords;
1797 os_vm_address_t __attribute__((unused)) constants_start_addr, constants_end_addr;
1798 os_vm_address_t __attribute__((unused)) code_start_addr, code_end_addr;
1799 os_vm_address_t code_addr = (os_vm_address_t)new_code;
1800 os_vm_address_t old_addr = (os_vm_address_t)old_code;
1801 os_vm_size_t displacement = code_addr - old_addr;
1802 lispobj fixups = NIL;
1803 struct vector *fixups_vector;
1805 ncode_words = code_instruction_words(new_code->code_size);
1806 nheader_words = code_header_words(*(lispobj *)new_code);
1807 nwords = ncode_words + nheader_words;
1808 /* FSHOW((stderr,
1809 "/compiled code object at %x: header words = %d, code words = %d\n",
1810 new_code, nheader_words, ncode_words)); */
1811 constants_start_addr = code_addr + 5*N_WORD_BYTES;
1812 constants_end_addr = code_addr + nheader_words*N_WORD_BYTES;
1813 code_start_addr = code_addr + nheader_words*N_WORD_BYTES;
1814 code_end_addr = code_addr + nwords*N_WORD_BYTES;
1816 FSHOW((stderr,
1817 "/const start = %x, end = %x\n",
1818 constants_start_addr,constants_end_addr));
1819 FSHOW((stderr,
1820 "/code start = %x; end = %x\n",
1821 code_start_addr,code_end_addr));
1824 fixups = new_code->fixups;
1825 /* It will be a Lisp vector if valid, or 0 if there are no fixups */
1826 if (fixups == 0 || !is_lisp_pointer(fixups)) {
1827 /* Check for possible errors. */
1828 if (check_code_fixups)
1829 sniff_code_object(new_code, displacement);
1831 return;
1834 fixups_vector = (struct vector *)native_pointer(fixups);
1836 /* Could be pointing to a forwarding pointer. */
1837 /* This is extremely unlikely, because the only referent of the fixups
1838 is usually the code itself; so scavenging the vector won't occur
1839 until after the code object is known to be live. As we're just now
1840 enlivening the code, the fixups shouldn't have been forwarded.
1841 Maybe the vector is on the special binding stack though ... */
1842 if (is_lisp_pointer(fixups) &&
1843 (find_page_index((void*)fixups_vector) != -1) &&
1844 forwarding_pointer_p((lispobj*)fixups_vector)) {
1845 /* If so, then follow it. */
1846 /*SHOW("following pointer to a forwarding pointer");*/
1847 fixups_vector = (struct vector *)
1848 native_pointer(forwarding_pointer_value((lispobj*)fixups_vector));
1851 /*SHOW("got fixups");*/
1853 if (widetag_of(fixups_vector->header) == SIMPLE_ARRAY_WORD_WIDETAG) {
1854 /* Got the fixups for the code block. Now work through the vector,
1855 and apply a fixup at each address. */
1856 sword_t length = fixnum_value(fixups_vector->length);
1857 sword_t i;
1858 for (i = 0; i < length; i++) {
1859 long offset = fixups_vector->data[i];
1860 /* Now check the current value of offset. */
1861 os_vm_address_t old_value = *(os_vm_address_t *)(code_start_addr + offset);
1863 /* If it's within the old_code object then it must be an
1864 * absolute fixup (relative ones are not saved) */
1865 if ((old_value >= old_addr)
1866 && (old_value < (old_addr + nwords*N_WORD_BYTES)))
1867 /* So add the dispacement. */
1868 *(os_vm_address_t *)(code_start_addr + offset) =
1869 old_value + displacement;
1870 else
1871 /* It is outside the old code object so it must be a
1872 * relative fixup (absolute fixups are not saved). So
1873 * subtract the displacement. */
1874 *(os_vm_address_t *)(code_start_addr + offset) =
1875 old_value - displacement;
1877 } else {
1878 /* This used to just print a note to stderr, but a bogus fixup seems to
1879 * indicate real heap corruption, so a hard hailure is in order. */
1880 lose("fixup vector %p has a bad widetag: %d\n",
1881 fixups_vector, widetag_of(fixups_vector->header));
1884 /* Check for possible errors. */
1885 if (check_code_fixups) {
1886 sniff_code_object(new_code,displacement);
1889 #endif
1891 static lispobj
1892 trans_boxed_large(lispobj object)
1894 gc_assert(is_lisp_pointer(object));
1895 return copy_large_object(object,
1896 (HeaderValue(*native_pointer(object)) | 1) + 1);
1900 * weak pointers
1903 /* XX This is a hack adapted from cgc.c. These don't work too
1904 * efficiently with the gencgc as a list of the weak pointers is
1905 * maintained within the objects which causes writes to the pages. A
1906 * limited attempt is made to avoid unnecessary writes, but this needs
1907 * a re-think. */
1908 /* FIXME: now that we have non-Lisp hashtables in the GC, it might make sense
1909 * to stop chaining weak pointers through a slot in the object, as a remedy to
1910 * the above concern. It would also shorten the object by 2 words. */
1911 static sword_t
1912 scav_weak_pointer(lispobj *where, lispobj object)
1914 /* Since we overwrite the 'next' field, we have to make
1915 * sure not to do so for pointers already in the list.
1916 * Instead of searching the list of weak_pointers each
1917 * time, we ensure that next is always NULL when the weak
1918 * pointer isn't in the list, and not NULL otherwise.
1919 * Since we can't use NULL to denote end of list, we
1920 * use a pointer back to the same weak_pointer.
1922 struct weak_pointer * wp = (struct weak_pointer*)where;
1924 if (NULL == wp->next && weak_pointer_breakable_p(wp)) {
1925 wp->next = weak_pointers;
1926 weak_pointers = wp;
1927 if (NULL == wp->next)
1928 wp->next = wp;
1931 /* Do not let GC scavenge the value slot of the weak pointer.
1932 * (That is why it is a weak pointer.) */
1934 return WEAK_POINTER_NWORDS;
1938 lispobj *
1939 search_read_only_space(void *pointer)
1941 lispobj *start = (lispobj *) READ_ONLY_SPACE_START;
1942 lispobj *end = (lispobj *) SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0);
1943 if ((pointer < (void *)start) || (pointer >= (void *)end))
1944 return NULL;
1945 return gc_search_space(start, pointer);
1948 lispobj *
1949 search_static_space(void *pointer)
1951 lispobj *start = (lispobj *)STATIC_SPACE_START;
1952 lispobj *end = (lispobj *)SymbolValue(STATIC_SPACE_FREE_POINTER,0);
1953 if ((pointer < (void *)start) || (pointer >= (void *)end))
1954 return NULL;
1955 return gc_search_space(start, pointer);
1958 /* a faster version for searching the dynamic space. This will work even
1959 * if the object is in a current allocation region. */
1960 lispobj *
1961 search_dynamic_space(void *pointer)
1963 page_index_t page_index = find_page_index(pointer);
1964 lispobj *start;
1966 /* The address may be invalid, so do some checks. */
1967 if ((page_index == -1) || page_free_p(page_index))
1968 return NULL;
1969 start = (lispobj *)page_scan_start(page_index);
1970 return gc_search_space(start, pointer);
1973 #ifndef GENCGC_IS_PRECISE
1974 // Return the starting address of the object containing 'addr'
1975 // if and only if the object is one which would be evacuated from 'from_space'
1976 // were it allowed to be either discarded as garbage or moved.
1977 // 'addr_page_index' is the page containing 'addr' and must not be -1.
1978 // Return 0 if there is no such object - that is, if addr is past the
1979 // end of the used bytes, or its pages are not in 'from_space' etc.
1980 static lispobj*
1981 conservative_root_p(void *addr, page_index_t addr_page_index)
1983 /* quick check 1: Address is quite likely to have been invalid. */
1984 struct page* page = &page_table[addr_page_index];
1985 if (page->gen != from_space ||
1986 ((uword_t)addr & (GENCGC_CARD_BYTES - 1)) > page_bytes_used(addr_page_index) ||
1987 (page->large_object && page->dont_move))
1988 return 0;
1989 gc_assert(!(page->allocated & OPEN_REGION_PAGE_FLAG));
1991 /* Filter out anything which can't be a pointer to a Lisp object
1992 * (or, as a special case which also requires dont_move, a return
1993 * address referring to something in a CodeObject). This is
1994 * expensive but important, since it vastly reduces the
1995 * probability that random garbage will be bogusly interpreted as
1996 * a pointer which prevents a page from moving. */
1997 lispobj* object_start = search_dynamic_space(addr);
1998 if (!object_start) return 0;
2000 /* If the containing object is a code object and 'addr' points
2001 * anywhere beyond the boxed words,
2002 * presume it to be a valid unboxed return address. */
2003 if (instruction_ptr_p(addr, object_start))
2004 return object_start;
2006 /* Large object pages only contain ONE object, and it will never
2007 * be a CONS. However, arrays and bignums can be allocated larger
2008 * than necessary and then shrunk to fit, leaving what look like
2009 * (0 . 0) CONSes at the end. These appear valid to
2010 * properly_tagged_descriptor_p(), so pick them off here. */
2011 if (((lowtag_of((lispobj)addr) == LIST_POINTER_LOWTAG) &&
2012 page_table[addr_page_index].large_object)
2013 || !properly_tagged_descriptor_p(addr, object_start))
2014 return 0;
2016 return object_start;
2018 #endif
2020 /* Adjust large bignum and vector objects. This will adjust the
2021 * allocated region if the size has shrunk, and move unboxed objects
2022 * into unboxed pages. The pages are not promoted here, and the
2023 * promoted region is not added to the new_regions; this is really
2024 * only designed to be called from preserve_pointer(). Shouldn't fail
2025 * if this is missed, just may delay the moving of objects to unboxed
2026 * pages, and the freeing of pages. */
2027 static void
2028 maybe_adjust_large_object(page_index_t first_page)
2030 lispobj* where = (lispobj*)page_address(first_page);
2031 page_index_t next_page;
2033 uword_t remaining_bytes;
2034 uword_t bytes_freed;
2035 uword_t old_bytes_used;
2037 int page_type_flag;
2039 /* Check whether it's a vector or bignum object. */
2040 lispobj widetag = widetag_of(where[0]);
2041 if (widetag == SIMPLE_VECTOR_WIDETAG)
2042 page_type_flag = BOXED_PAGE_FLAG;
2043 else if (specialized_vector_widetag_p(widetag) || widetag == BIGNUM_WIDETAG)
2044 page_type_flag = UNBOXED_PAGE_FLAG;
2045 else
2046 return;
2048 /* Find its current size. */
2049 sword_t nwords = sizetab[widetag](where);
2051 /* Note: Any page write-protection must be removed, else a later
2052 * scavenge_newspace may incorrectly not scavenge these pages.
2053 * This would not be necessary if they are added to the new areas,
2054 * but lets do it for them all (they'll probably be written
2055 * anyway?). */
2057 gc_assert(page_starts_contiguous_block_p(first_page));
2059 next_page = first_page;
2060 remaining_bytes = nwords*N_WORD_BYTES;
2061 while (remaining_bytes > GENCGC_CARD_BYTES) {
2062 gc_assert(page_table[next_page].gen == from_space);
2063 // We can't assert that page_table[next_page].allocated is correct,
2064 // because unboxed objects are initially allocated on boxed pages.
2065 gc_assert(page_allocated_no_region_p(next_page));
2066 gc_assert(page_table[next_page].large_object);
2067 gc_assert(page_scan_start_offset(next_page) ==
2068 npage_bytes(next_page-first_page));
2069 gc_assert(page_bytes_used(next_page) == GENCGC_CARD_BYTES);
2071 // This affects only one object, since large objects don't share pages.
2072 page_table[next_page].allocated = page_type_flag;
2074 /* Shouldn't be write-protected at this stage. Essential that the
2075 * pages aren't. */
2076 gc_assert(!page_table[next_page].write_protected);
2077 remaining_bytes -= GENCGC_CARD_BYTES;
2078 next_page++;
2081 /* Now only one page remains, but the object may have shrunk so
2082 * there may be more unused pages which will be freed. */
2084 /* Object may have shrunk but shouldn't have grown - check. */
2085 gc_assert(page_bytes_used(next_page) >= remaining_bytes);
2087 page_table[next_page].allocated = page_type_flag;
2089 /* Adjust the bytes_used. */
2090 old_bytes_used = page_bytes_used(next_page);
2091 set_page_bytes_used(next_page, remaining_bytes);
2093 bytes_freed = old_bytes_used - remaining_bytes;
2095 /* Free any remaining pages; needs care. */
2096 next_page++;
2097 while ((old_bytes_used == GENCGC_CARD_BYTES) &&
2098 (page_table[next_page].gen == from_space) &&
2099 page_allocated_no_region_p(next_page) &&
2100 page_table[next_page].large_object &&
2101 (page_scan_start_offset(next_page) ==
2102 npage_bytes(next_page - first_page))) {
2103 /* It checks out OK, free the page. We don't need to bother zeroing
2104 * pages as this should have been done before shrinking the
2105 * object. These pages shouldn't be write protected as they
2106 * should be zero filled. */
2107 gc_assert(page_table[next_page].write_protected == 0);
2109 old_bytes_used = page_bytes_used(next_page);
2110 page_table[next_page].allocated = FREE_PAGE_FLAG;
2111 set_page_bytes_used(next_page, 0);
2112 bytes_freed += old_bytes_used;
2113 next_page++;
2116 if ((bytes_freed > 0) && gencgc_verbose) {
2117 FSHOW((stderr,
2118 "/maybe_adjust_large_object() freed %d\n",
2119 bytes_freed));
2122 generations[from_space].bytes_allocated -= bytes_freed;
2123 bytes_allocated -= bytes_freed;
2125 return;
2128 #if !(defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64))
2129 # define scavenge_pinned_ranges()
2130 # define wipe_nonpinned_words()
2131 /* After scavenging of the roots is done, we go back to the pinned objects
2132 * and look within them for pointers. While heap_scavenge() could certainly
2133 * do this, it would potentially lead to extra work, since we can't know
2134 * whether any given object has been examined at least once, since there is
2135 * no telltale forwarding-pointer. The easiest thing to do is defer all
2136 * pinned objects to a subsequent pass, as is done here.
2138 #else
2139 static void
2140 scavenge_pinned_ranges()
2142 int i;
2143 lispobj key;
2144 for_each_hopscotch_key(i, key, pinned_objects) {
2145 lispobj* obj = native_pointer(key);
2146 lispobj header = *obj;
2147 // Never invoke scavenger on a simple-fun, just code components.
2148 if (is_cons_half(header))
2149 scavenge(obj, 2);
2150 else if (widetag_of(header) != SIMPLE_FUN_WIDETAG)
2151 scavtab[widetag_of(header)](obj, header);
2155 /* Create an array of fixnum to consume the space between 'from' and 'to' */
2156 static void deposit_filler(uword_t from, uword_t to)
2158 if (to > from) {
2159 lispobj* where = (lispobj*)from;
2160 sword_t nwords = (to - from) >> WORD_SHIFT;
2161 where[0] = SIMPLE_ARRAY_WORD_WIDETAG;
2162 where[1] = make_fixnum(nwords - 2);
2166 /* Zero out the byte ranges on small object pages marked dont_move,
2167 * carefully skipping over objects in the pin hashtable.
2168 * TODO: by recording an additional bit per page indicating whether
2169 * there is more than one pinned object on it, we could avoid qsort()
2170 * except in the case where there is more than one. */
2171 static void
2172 wipe_nonpinned_words()
2174 void gc_heapsort_uwords(uword_t*, int);
2175 // Loop over the keys in pinned_objects and pack them densely into
2176 // the same array - pinned_objects.keys[] - but skip any simple-funs.
2177 // Admittedly this is abstraction breakage.
2178 int limit = hopscotch_max_key_index(pinned_objects);
2179 int n_pins = 0, i;
2180 for (i = 0; i <= limit; ++i) {
2181 lispobj key = pinned_objects.keys[i];
2182 if (key) {
2183 lispobj* obj = native_pointer(key);
2184 // No need to check for is_cons_half() - it will be false
2185 // on a simple-fun header, and that's the correct answer.
2186 if (widetag_of(*obj) != SIMPLE_FUN_WIDETAG)
2187 pinned_objects.keys[n_pins++] = (uword_t)obj;
2190 // Store a sentinel at the end. Even if n_pins = table capacity (unlikely),
2191 // it is safe to write one more word, because the hops[] array immediately
2192 // follows the keys[] array in memory. At worst, 2 elements of hops[]
2193 // are clobbered, which is irrelevant since the table has already been
2194 // rendered unusable by stealing its key array for a different purpose.
2195 pinned_objects.keys[n_pins] = 0;
2196 // Don't touch pinned_objects.count in case the reset function uses it
2197 // to decide how to resize for next use (which it doesn't, but could).
2198 gc_n_stack_pins = n_pins;
2199 // Order by ascending address, stopping short of the sentinel.
2200 gc_heapsort_uwords(pinned_objects.keys, n_pins);
2201 #if 0
2202 printf("Sorted pin list:\n");
2203 for (i = 0; i < n_pins; ++i) {
2204 lispobj* obj = (lispobj*)pinned_objects.keys[i];
2205 if (!is_cons_half(*obj))
2206 printf("%p: %5d words\n", obj, (int)sizetab[widetag_of(*obj)](obj));
2207 else printf("%p: CONS\n", obj);
2209 #endif
2210 // Each entry in the pinned objects demarcates two ranges to be cleared:
2211 // - the range preceding it back to either the page start, or prior object.
2212 // - the range after it, up to the lesser of page bytes used or next object.
2213 uword_t preceding_object = 0;
2214 uword_t this_page_end = 0;
2215 #define page_base_address(x) (x&~(GENCGC_CARD_BYTES-1))
2216 for (i = 0; i < n_pins; ++i) {
2217 // Handle the preceding range. If this object is on the same page as
2218 // its predecessor, then intervening bytes were already zeroed.
2219 // If not, then start a new page and do some bookkeeping.
2220 lispobj* obj = (lispobj*)pinned_objects.keys[i];
2221 uword_t this_page_base = page_base_address((uword_t)obj);
2222 /* printf("i=%d obj=%p base=%p\n", i, obj, (void*)this_page_base); */
2223 if (this_page_base > page_base_address(preceding_object)) {
2224 deposit_filler(this_page_base, (lispobj)obj);
2225 // Move the page to newspace
2226 page_index_t page = find_page_index(obj);
2227 int used = page_bytes_used(page);
2228 this_page_end = this_page_base + used;
2229 /* printf(" Clearing %p .. %p (limit=%p)\n",
2230 (void*)this_page_base, obj, (void*)this_page_end); */
2231 generations[new_space].bytes_allocated += used;
2232 generations[page_table[page].gen].bytes_allocated -= used;
2233 page_table[page].gen = new_space;
2234 page_table[page].has_pins = 0;
2236 // Handle the following range.
2237 lispobj word = *obj;
2238 size_t nwords = is_cons_half(word) ? 2 : sizetab[widetag_of(word)](obj);
2239 uword_t range_start = (uword_t)(obj + nwords);
2240 uword_t range_end = this_page_end;
2241 // There is always an i+1'th key due to the sentinel value.
2242 if (page_base_address(pinned_objects.keys[i+1]) == this_page_base)
2243 range_end = pinned_objects.keys[i+1];
2244 /* printf(" Clearing %p .. %p\n", (void*)range_start, (void*)range_end); */
2245 deposit_filler(range_start, range_end);
2246 preceding_object = (uword_t)obj;
2250 /* Add 'object' to the hashtable, and if the object is a code component,
2251 * then also add all of the embedded simple-funs.
2252 * The rationale for the extra work on code components is that without it,
2253 * every test of pinned_p() on an object would have to check if the pointer
2254 * is to a simple-fun - entailing an extra read of the header - and mapping
2255 * to its code component if so. Since more calls to pinned_p occur than to
2256 * pin_object, the extra burden should be on this function.
2257 * Experimentation bears out that this is the better technique.
2258 * Also, we wouldn't often expect code components in the collected generation
2259 * so the extra work here is quite minimal, even if it can generally add to
2260 * the number of keys in the hashtable.
2262 static void
2263 pin_object(lispobj object)
2265 if (!hopscotch_containsp(&pinned_objects, object)) {
2266 hopscotch_insert(&pinned_objects, object, 1);
2267 struct code* maybe_code = (struct code*)native_pointer(object);
2268 if (widetag_of(maybe_code->header) == CODE_HEADER_WIDETAG) {
2269 for_each_simple_fun(i, fun, maybe_code, 0, {
2270 hopscotch_insert(&pinned_objects,
2271 make_lispobj(fun, FUN_POINTER_LOWTAG),
2277 #endif
2279 /* Take a possible pointer to a Lisp object and mark its page in the
2280 * page_table so that it will not be relocated during a GC.
2282 * This involves locating the page it points to, then backing up to
2283 * the start of its region, then marking all pages dont_move from there
2284 * up to the first page that's not full or has a different generation
2286 * It is assumed that all the page static flags have been cleared at
2287 * the start of a GC.
2289 * It is also assumed that the current gc_alloc() region has been
2290 * flushed and the tables updated. */
2292 // TODO: there's probably a way to be a little more efficient here.
2293 // As things are, we start by finding the object that encloses 'addr',
2294 // then we see if 'addr' was a "valid" Lisp pointer to that object
2295 // - meaning we expect the correct lowtag on the pointer - except
2296 // that for code objects we don't require a correct lowtag
2297 // and we allow a pointer to anywhere in the object.
2299 // It should be possible to avoid calling search_dynamic_space
2300 // more of the time. First, check if the page pointed to might hold code.
2301 // If it does, then we continue regardless of the pointer's lowtag
2302 // (because of the special allowance). If the page definitely does *not*
2303 // hold code, then we require up front that the lowtake make sense,
2304 // by doing the same checks that are in properly_tagged_descriptor_p.
2306 // Problem: when code is allocated from a per-thread region,
2307 // does it ensure that the occupied pages are flagged as having code?
2309 static void
2310 preserve_pointer(void *addr)
2312 #ifdef LISP_FEATURE_IMMOBILE_SPACE
2313 /* Immobile space MUST be lower than dynamic space,
2314 or else this test needs to be revised */
2315 if (addr < (void*)IMMOBILE_SPACE_END) {
2316 extern void immobile_space_preserve_pointer(void*);
2317 immobile_space_preserve_pointer(addr);
2318 return;
2320 #endif
2321 page_index_t addr_page_index = find_page_index(addr);
2323 #ifdef GENCGC_IS_PRECISE
2324 /* If we're in precise gencgc (non-x86oid as of this writing) then
2325 * we are only called on valid object pointers in the first place,
2326 * so we just have to do a bounds-check against the heap, a
2327 * generation check, and the already-pinned check. */
2328 if (addr_page_index == -1
2329 || (page_table[addr_page_index].gen != from_space)
2330 || page_table[addr_page_index].dont_move)
2331 return;
2332 #else
2333 lispobj *object_start;
2334 if (addr_page_index == -1
2335 || (object_start = conservative_root_p(addr, addr_page_index)) == 0)
2336 return;
2337 #endif
2339 /* (Now that we know that addr_page_index is in range, it's
2340 * safe to index into page_table[] with it.) */
2341 unsigned int region_allocation = page_table[addr_page_index].allocated;
2343 /* Find the beginning of the region. Note that there may be
2344 * objects in the region preceding the one that we were passed a
2345 * pointer to: if this is the case, we will write-protect all the
2346 * previous objects' pages too. */
2348 #if 0
2349 /* I think this'd work just as well, but without the assertions.
2350 * -dan 2004.01.01 */
2351 page_index_t first_page = find_page_index(page_scan_start(addr_page_index))
2352 #else
2353 page_index_t first_page = addr_page_index;
2354 while (!page_starts_contiguous_block_p(first_page)) {
2355 --first_page;
2356 /* Do some checks. */
2357 gc_assert(page_bytes_used(first_page) == GENCGC_CARD_BYTES);
2358 gc_assert(page_table[first_page].gen == from_space);
2359 gc_assert(page_table[first_page].allocated == region_allocation);
2361 #endif
2363 /* Adjust any large objects before promotion as they won't be
2364 * copied after promotion. */
2365 if (page_table[first_page].large_object) {
2366 maybe_adjust_large_object(first_page);
2367 /* It may have moved to unboxed pages. */
2368 region_allocation = page_table[first_page].allocated;
2371 /* Now work forward until the end of this contiguous area is found,
2372 * marking all pages as dont_move. */
2373 page_index_t i;
2374 for (i = first_page; ;i++) {
2375 gc_assert(page_table[i].allocated == region_allocation);
2377 /* Mark the page static. */
2378 page_table[i].dont_move = 1;
2380 /* It is essential that the pages are not write protected as
2381 * they may have pointers into the old-space which need
2382 * scavenging. They shouldn't be write protected at this
2383 * stage. */
2384 gc_assert(!page_table[i].write_protected);
2386 /* Check whether this is the last page in this contiguous block.. */
2387 if (page_ends_contiguous_block_p(i, from_space))
2388 break;
2391 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
2392 /* Do not do this for multi-page objects. Those pages do not need
2393 * object wipeout anyway.
2395 if (do_wipe_p && i == first_page) { // single-page object
2396 lispobj word = *object_start;
2397 int lowtag = is_cons_half(word) ?
2398 LIST_POINTER_LOWTAG : lowtag_for_widetag[widetag_of(word)>>2];
2399 pin_object(make_lispobj(object_start, lowtag));
2400 page_table[i].has_pins = 1;
2402 #endif
2404 /* Check that the page is now static. */
2405 gc_assert(page_table[addr_page_index].dont_move != 0);
2409 #define IN_REGION_P(a,kind) (kind##_region.start_addr<=a && a<=kind##_region.free_pointer)
2410 #ifdef LISP_FEATURE_SEGREGATED_CODE
2411 #define IN_BOXED_REGION_P(a) IN_REGION_P(a,boxed)||IN_REGION_P(a,code)
2412 #else
2413 #define IN_BOXED_REGION_P(a) IN_REGION_P(a,boxed)
2414 #endif
2416 /* If the given page is not write-protected, then scan it for pointers
2417 * to younger generations or the top temp. generation, if no
2418 * suspicious pointers are found then the page is write-protected.
2420 * Care is taken to check for pointers to the current gc_alloc()
2421 * region if it is a younger generation or the temp. generation. This
2422 * frees the caller from doing a gc_alloc_update_page_tables(). Actually
2423 * the gc_alloc_generation does not need to be checked as this is only
2424 * called from scavenge_generation() when the gc_alloc generation is
2425 * younger, so it just checks if there is a pointer to the current
2426 * region.
2428 * We return 1 if the page was write-protected, else 0. */
2429 static int
2430 update_page_write_prot(page_index_t page)
2432 generation_index_t gen = page_table[page].gen;
2433 sword_t j;
2434 int wp_it = 1;
2435 void **page_addr = (void **)page_address(page);
2436 sword_t num_words = page_bytes_used(page) / N_WORD_BYTES;
2438 /* Shouldn't be a free page. */
2439 gc_assert(!page_free_p(page));
2440 gc_assert(page_bytes_used(page) != 0);
2442 /* Skip if it's already write-protected, pinned, or unboxed */
2443 if (page_table[page].write_protected
2444 /* FIXME: What's the reason for not write-protecting pinned pages? */
2445 || page_table[page].dont_move
2446 || page_unboxed_p(page))
2447 return (0);
2449 /* Scan the page for pointers to younger generations or the
2450 * top temp. generation. */
2452 /* This is conservative: any word satisfying is_lisp_pointer() is
2453 * assumed to be a pointer. To do otherwise would require a family
2454 * of scavenge-like functions. */
2455 for (j = 0; j < num_words; j++) {
2456 void *ptr = *(page_addr+j);
2457 page_index_t index;
2458 lispobj __attribute__((unused)) header;
2460 if (!is_lisp_pointer((lispobj)ptr))
2461 continue;
2462 /* Check that it's in the dynamic space */
2463 if ((index = find_page_index(ptr)) != -1) {
2464 if (/* Does it point to a younger or the temp. generation? */
2465 (!page_free_p(index)
2466 && (page_bytes_used(index) != 0)
2467 && ((page_table[index].gen < gen)
2468 || (page_table[index].gen == SCRATCH_GENERATION)))
2470 /* Or does it point within a current gc_alloc() region? */
2471 || (IN_BOXED_REGION_P(ptr) || IN_REGION_P(ptr,unboxed))) {
2472 wp_it = 0;
2473 break;
2476 #ifdef LISP_FEATURE_IMMOBILE_SPACE
2477 else if ((index = find_immobile_page_index(ptr)) >= 0 &&
2478 other_immediate_lowtag_p(header = *native_pointer((lispobj)ptr))) {
2479 // This is *possibly* a pointer to an object in immobile space,
2480 // given that above two conditions were satisfied.
2481 // But unlike in the dynamic space case, we need to read a byte
2482 // from the object to determine its generation, which requires care.
2483 // Consider an unboxed word that looks like a pointer to a word that
2484 // looks like fun-header-widetag. We can't naively back up to the
2485 // underlying code object since the alleged header might not be one.
2486 int obj_gen = gen; // Make comparison fail if we fall through
2487 if (lowtag_of((lispobj)ptr) != FUN_POINTER_LOWTAG) {
2488 obj_gen = __immobile_obj_generation(native_pointer((lispobj)ptr));
2489 } else if (widetag_of(header) == SIMPLE_FUN_WIDETAG) {
2490 lispobj* code = fun_code_header((lispobj)ptr - FUN_POINTER_LOWTAG);
2491 // This is a heuristic, since we're not actually looking for
2492 // an object boundary. Precise scanning of 'page' would obviate
2493 // the guard conditions here.
2494 if ((lispobj)code >= IMMOBILE_VARYOBJ_SUBSPACE_START
2495 && widetag_of(*code) == CODE_HEADER_WIDETAG)
2496 obj_gen = __immobile_obj_generation(code);
2498 // A bogus generation number implies a not-really-pointer,
2499 // but it won't cause misbehavior.
2500 if (obj_gen < gen || obj_gen == SCRATCH_GENERATION) {
2501 wp_it = 0;
2502 break;
2505 #endif
2508 if (wp_it == 1) {
2509 /* Write-protect the page. */
2510 /*FSHOW((stderr, "/write-protecting page %d gen %d\n", page, gen));*/
2512 os_protect((void *)page_addr,
2513 GENCGC_CARD_BYTES,
2514 OS_VM_PROT_READ|OS_VM_PROT_EXECUTE);
2516 /* Note the page as protected in the page tables. */
2517 page_table[page].write_protected = 1;
2520 return (wp_it);
2523 /* Is this page holding a normal (non-hashtable) large-object
2524 * simple-vector? */
2525 static inline boolean large_simple_vector_p(page_index_t page) {
2526 if (!page_table[page].large_object)
2527 return 0;
2528 lispobj object = *(lispobj *)page_address(page);
2529 return widetag_of(object) == SIMPLE_VECTOR_WIDETAG &&
2530 (HeaderValue(object) & 0xFF) == subtype_VectorNormal;
2534 /* Scavenge all generations from FROM to TO, inclusive, except for
2535 * new_space which needs special handling, as new objects may be
2536 * added which are not checked here - use scavenge_newspace generation.
2538 * Write-protected pages should not have any pointers to the
2539 * from_space so do need scavenging; thus write-protected pages are
2540 * not always scavenged. There is some code to check that these pages
2541 * are not written; but to check fully the write-protected pages need
2542 * to be scavenged by disabling the code to skip them.
2544 * Under the current scheme when a generation is GCed the younger
2545 * generations will be empty. So, when a generation is being GCed it
2546 * is only necessary to scavenge the older generations for pointers
2547 * not the younger. So a page that does not have pointers to younger
2548 * generations does not need to be scavenged.
2550 * The write-protection can be used to note pages that don't have
2551 * pointers to younger pages. But pages can be written without having
2552 * pointers to younger generations. After the pages are scavenged here
2553 * they can be scanned for pointers to younger generations and if
2554 * there are none the page can be write-protected.
2556 * One complication is when the newspace is the top temp. generation.
2558 * Enabling SC_GEN_CK scavenges the write-protected pages and checks
2559 * that none were written, which they shouldn't be as they should have
2560 * no pointers to younger generations. This breaks down for weak
2561 * pointers as the objects contain a link to the next and are written
2562 * if a weak pointer is scavenged. Still it's a useful check. */
2563 static void
2564 scavenge_generations(generation_index_t from, generation_index_t to)
2566 page_index_t i;
2567 page_index_t num_wp = 0;
2569 #define SC_GEN_CK 0
2570 #if SC_GEN_CK
2571 /* Clear the write_protected_cleared flags on all pages. */
2572 for (i = 0; i < page_table_pages; i++)
2573 page_table[i].write_protected_cleared = 0;
2574 #endif
2576 for (i = 0; i < last_free_page; i++) {
2577 generation_index_t generation = page_table[i].gen;
2578 if (page_boxed_p(i)
2579 && (page_bytes_used(i) != 0)
2580 && (generation != new_space)
2581 && (generation >= from)
2582 && (generation <= to)) {
2583 page_index_t last_page,j;
2584 int write_protected=1;
2586 /* This should be the start of a region */
2587 gc_assert(page_starts_contiguous_block_p(i));
2589 if (large_simple_vector_p(i)) {
2590 /* Scavenge only the unprotected pages of a
2591 * large-object vector, other large objects could be
2592 * handled as well, but vectors are easier to deal
2593 * with and are more likely to grow to very large
2594 * sizes where avoiding scavenging the whole thing is
2595 * worthwile */
2596 if (!page_table[i].write_protected) {
2597 scavenge((lispobj*)page_address(i) + 2,
2598 GENCGC_CARD_BYTES / N_WORD_BYTES - 2);
2599 update_page_write_prot(i);
2601 for (last_page = i + 1; ; last_page++) {
2602 lispobj* start = (lispobj*)page_address(last_page);
2603 write_protected = page_table[last_page].write_protected;
2604 if (page_ends_contiguous_block_p(last_page, generation)) {
2605 if (!write_protected) {
2606 scavenge(start, page_bytes_used(last_page) / N_WORD_BYTES);
2607 update_page_write_prot(last_page);
2609 break;
2611 if (!write_protected) {
2612 scavenge(start, GENCGC_CARD_BYTES / N_WORD_BYTES);
2613 update_page_write_prot(last_page);
2616 } else {
2617 /* Now work forward until the end of the region */
2618 for (last_page = i; ; last_page++) {
2619 write_protected =
2620 write_protected && page_table[last_page].write_protected;
2621 if (page_ends_contiguous_block_p(last_page, generation))
2622 break;
2624 if (!write_protected) {
2625 heap_scavenge((lispobj*)page_address(i),
2626 (lispobj*)(page_address(last_page)
2627 + page_bytes_used(last_page)));
2629 /* Now scan the pages and write protect those that
2630 * don't have pointers to younger generations. */
2631 if (enable_page_protection) {
2632 for (j = i; j <= last_page; j++) {
2633 num_wp += update_page_write_prot(j);
2636 if ((gencgc_verbose > 1) && (num_wp != 0)) {
2637 FSHOW((stderr,
2638 "/write protected %d pages within generation %d\n",
2639 num_wp, generation));
2643 i = last_page;
2647 #if SC_GEN_CK
2648 /* Check that none of the write_protected pages in this generation
2649 * have been written to. */
2650 for (i = 0; i < page_table_pages; i++) {
2651 if (!page_free_p(i)
2652 && (page_bytes_used(i) != 0)
2653 && (page_table[i].gen == generation)
2654 && (page_table[i].write_protected_cleared != 0)) {
2655 FSHOW((stderr, "/scavenge_generation() %d\n", generation));
2656 FSHOW((stderr,
2657 "/page bytes_used=%d scan_start_offset=%lu dont_move=%d\n",
2658 page_bytes_used(i),
2659 scan_start_offset(page_table[i]),
2660 page_table[i].dont_move));
2661 lose("write to protected page %d in scavenge_generation()\n", i);
2664 #endif
2668 /* Scavenge a newspace generation. As it is scavenged new objects may
2669 * be allocated to it; these will also need to be scavenged. This
2670 * repeats until there are no more objects unscavenged in the
2671 * newspace generation.
2673 * To help improve the efficiency, areas written are recorded by
2674 * gc_alloc() and only these scavenged. Sometimes a little more will be
2675 * scavenged, but this causes no harm. An easy check is done that the
2676 * scavenged bytes equals the number allocated in the previous
2677 * scavenge.
2679 * Write-protected pages are not scanned except if they are marked
2680 * dont_move in which case they may have been promoted and still have
2681 * pointers to the from space.
2683 * Write-protected pages could potentially be written by alloc however
2684 * to avoid having to handle re-scavenging of write-protected pages
2685 * gc_alloc() does not write to write-protected pages.
2687 * New areas of objects allocated are recorded alternatively in the two
2688 * new_areas arrays below. */
2689 static struct new_area new_areas_1[NUM_NEW_AREAS];
2690 static struct new_area new_areas_2[NUM_NEW_AREAS];
2692 #ifdef LISP_FEATURE_IMMOBILE_SPACE
2693 extern unsigned int immobile_scav_queue_count;
2694 extern void
2695 gc_init_immobile(),
2696 update_immobile_nursery_bits(),
2697 scavenge_immobile_roots(generation_index_t,generation_index_t),
2698 scavenge_immobile_newspace(),
2699 sweep_immobile_space(int raise),
2700 write_protect_immobile_space();
2701 #else
2702 #define immobile_scav_queue_count 0
2703 #endif
2705 /* Do one full scan of the new space generation. This is not enough to
2706 * complete the job as new objects may be added to the generation in
2707 * the process which are not scavenged. */
2708 static void
2709 scavenge_newspace_generation_one_scan(generation_index_t generation)
2711 page_index_t i;
2713 FSHOW((stderr,
2714 "/starting one full scan of newspace generation %d\n",
2715 generation));
2716 for (i = 0; i < last_free_page; i++) {
2717 /* Note that this skips over open regions when it encounters them. */
2718 if (page_boxed_p(i)
2719 && (page_bytes_used(i) != 0)
2720 && (page_table[i].gen == generation)
2721 && ((page_table[i].write_protected == 0)
2722 /* (This may be redundant as write_protected is now
2723 * cleared before promotion.) */
2724 || (page_table[i].dont_move == 1))) {
2725 page_index_t last_page;
2726 int all_wp=1;
2728 /* The scavenge will start at the scan_start_offset of
2729 * page i.
2731 * We need to find the full extent of this contiguous
2732 * block in case objects span pages.
2734 * Now work forward until the end of this contiguous area
2735 * is found. A small area is preferred as there is a
2736 * better chance of its pages being write-protected. */
2737 for (last_page = i; ;last_page++) {
2738 /* If all pages are write-protected and movable,
2739 * then no need to scavenge */
2740 all_wp=all_wp && page_table[last_page].write_protected &&
2741 !page_table[last_page].dont_move;
2743 /* Check whether this is the last page in this
2744 * contiguous block */
2745 if (page_ends_contiguous_block_p(last_page, generation))
2746 break;
2749 /* Do a limited check for write-protected pages. */
2750 if (!all_wp) {
2751 new_areas_ignore_page = last_page;
2752 heap_scavenge(page_scan_start(i),
2753 (lispobj*)(page_address(last_page)
2754 + page_bytes_used(last_page)));
2756 i = last_page;
2759 FSHOW((stderr,
2760 "/done with one full scan of newspace generation %d\n",
2761 generation));
2764 /* Do a complete scavenge of the newspace generation. */
2765 static void
2766 scavenge_newspace_generation(generation_index_t generation)
2768 size_t i;
2770 /* the new_areas array currently being written to by gc_alloc() */
2771 struct new_area (*current_new_areas)[] = &new_areas_1;
2772 size_t current_new_areas_index;
2774 /* the new_areas created by the previous scavenge cycle */
2775 struct new_area (*previous_new_areas)[] = NULL;
2776 size_t previous_new_areas_index;
2778 /* Flush the current regions updating the tables. */
2779 gc_alloc_update_all_page_tables(0);
2781 /* Turn on the recording of new areas by gc_alloc(). */
2782 new_areas = current_new_areas;
2783 new_areas_index = 0;
2785 /* Don't need to record new areas that get scavenged anyway during
2786 * scavenge_newspace_generation_one_scan. */
2787 record_new_objects = 1;
2789 /* Start with a full scavenge. */
2790 scavenge_newspace_generation_one_scan(generation);
2792 /* Record all new areas now. */
2793 record_new_objects = 2;
2795 /* Give a chance to weak hash tables to make other objects live.
2796 * FIXME: The algorithm implemented here for weak hash table gcing
2797 * is O(W^2+N) as Bruno Haible warns in
2798 * http://www.haible.de/bruno/papers/cs/weak/WeakDatastructures-writeup.html
2799 * see "Implementation 2". */
2800 scav_weak_hash_tables();
2802 /* Flush the current regions updating the tables. */
2803 gc_alloc_update_all_page_tables(0);
2805 /* Grab new_areas_index. */
2806 current_new_areas_index = new_areas_index;
2808 /*FSHOW((stderr,
2809 "The first scan is finished; current_new_areas_index=%d.\n",
2810 current_new_areas_index));*/
2812 while (current_new_areas_index > 0 || immobile_scav_queue_count) {
2813 /* Move the current to the previous new areas */
2814 previous_new_areas = current_new_areas;
2815 previous_new_areas_index = current_new_areas_index;
2817 /* Scavenge all the areas in previous new areas. Any new areas
2818 * allocated are saved in current_new_areas. */
2820 /* Allocate an array for current_new_areas; alternating between
2821 * new_areas_1 and 2 */
2822 if (previous_new_areas == &new_areas_1)
2823 current_new_areas = &new_areas_2;
2824 else
2825 current_new_areas = &new_areas_1;
2827 /* Set up for gc_alloc(). */
2828 new_areas = current_new_areas;
2829 new_areas_index = 0;
2831 #ifdef LISP_FEATURE_IMMOBILE_SPACE
2832 scavenge_immobile_newspace();
2833 #endif
2834 /* Check whether previous_new_areas had overflowed. */
2835 if (previous_new_areas_index >= NUM_NEW_AREAS) {
2837 /* New areas of objects allocated have been lost so need to do a
2838 * full scan to be sure! If this becomes a problem try
2839 * increasing NUM_NEW_AREAS. */
2840 if (gencgc_verbose) {
2841 SHOW("new_areas overflow, doing full scavenge");
2844 /* Don't need to record new areas that get scavenged
2845 * anyway during scavenge_newspace_generation_one_scan. */
2846 record_new_objects = 1;
2848 scavenge_newspace_generation_one_scan(generation);
2850 /* Record all new areas now. */
2851 record_new_objects = 2;
2853 scav_weak_hash_tables();
2855 /* Flush the current regions updating the tables. */
2856 gc_alloc_update_all_page_tables(0);
2858 } else {
2860 /* Work through previous_new_areas. */
2861 for (i = 0; i < previous_new_areas_index; i++) {
2862 page_index_t page = (*previous_new_areas)[i].page;
2863 size_t offset = (*previous_new_areas)[i].offset;
2864 size_t size = (*previous_new_areas)[i].size;
2865 gc_assert(size % N_WORD_BYTES == 0);
2866 lispobj *start = (lispobj*)(page_address(page) + offset);
2867 heap_scavenge(start, (lispobj*)((char*)start + size));
2870 scav_weak_hash_tables();
2872 /* Flush the current regions updating the tables. */
2873 gc_alloc_update_all_page_tables(0);
2876 current_new_areas_index = new_areas_index;
2878 /*FSHOW((stderr,
2879 "The re-scan has finished; current_new_areas_index=%d.\n",
2880 current_new_areas_index));*/
2883 /* Turn off recording of areas allocated by gc_alloc(). */
2884 record_new_objects = 0;
2886 #if SC_NS_GEN_CK
2888 page_index_t i;
2889 /* Check that none of the write_protected pages in this generation
2890 * have been written to. */
2891 for (i = 0; i < page_table_pages; i++) {
2892 if (!page_free_p(i)
2893 && (page_bytes_used(i) != 0)
2894 && (page_table[i].gen == generation)
2895 && (page_table[i].write_protected_cleared != 0)
2896 && (page_table[i].dont_move == 0)) {
2897 lose("write protected page %d written to in scavenge_newspace_generation\ngeneration=%d dont_move=%d\n",
2898 i, generation, page_table[i].dont_move);
2902 #endif
2905 /* Un-write-protect all the pages in from_space. This is done at the
2906 * start of a GC else there may be many page faults while scavenging
2907 * the newspace (I've seen drive the system time to 99%). These pages
2908 * would need to be unprotected anyway before unmapping in
2909 * free_oldspace; not sure what effect this has on paging.. */
2910 static void
2911 unprotect_oldspace(void)
2913 page_index_t i;
2914 char *region_addr = 0;
2915 char *page_addr = 0;
2916 uword_t region_bytes = 0;
2918 for (i = 0; i < last_free_page; i++) {
2919 if (!page_free_p(i)
2920 && (page_bytes_used(i) != 0)
2921 && (page_table[i].gen == from_space)) {
2923 /* Remove any write-protection. We should be able to rely
2924 * on the write-protect flag to avoid redundant calls. */
2925 if (page_table[i].write_protected) {
2926 page_table[i].write_protected = 0;
2927 page_addr = page_address(i);
2928 if (!region_addr) {
2929 /* First region. */
2930 region_addr = page_addr;
2931 region_bytes = GENCGC_CARD_BYTES;
2932 } else if (region_addr + region_bytes == page_addr) {
2933 /* Region continue. */
2934 region_bytes += GENCGC_CARD_BYTES;
2935 } else {
2936 /* Unprotect previous region. */
2937 os_protect(region_addr, region_bytes, OS_VM_PROT_ALL);
2938 /* First page in new region. */
2939 region_addr = page_addr;
2940 region_bytes = GENCGC_CARD_BYTES;
2945 if (region_addr) {
2946 /* Unprotect last region. */
2947 os_protect(region_addr, region_bytes, OS_VM_PROT_ALL);
2951 /* Work through all the pages and free any in from_space. This
2952 * assumes that all objects have been copied or promoted to an older
2953 * generation. Bytes_allocated and the generation bytes_allocated
2954 * counter are updated. The number of bytes freed is returned. */
2955 static uword_t
2956 free_oldspace(void)
2958 uword_t bytes_freed = 0;
2959 page_index_t first_page, last_page;
2961 first_page = 0;
2963 do {
2964 /* Find a first page for the next region of pages. */
2965 while ((first_page < last_free_page)
2966 && (page_free_p(first_page)
2967 || (page_bytes_used(first_page) == 0)
2968 || (page_table[first_page].gen != from_space)))
2969 first_page++;
2971 if (first_page >= last_free_page)
2972 break;
2974 /* Find the last page of this region. */
2975 last_page = first_page;
2977 do {
2978 /* Free the page. */
2979 bytes_freed += page_bytes_used(last_page);
2980 generations[page_table[last_page].gen].bytes_allocated -=
2981 page_bytes_used(last_page);
2982 page_table[last_page].allocated = FREE_PAGE_FLAG;
2983 set_page_bytes_used(last_page, 0);
2984 /* Should already be unprotected by unprotect_oldspace(). */
2985 gc_assert(!page_table[last_page].write_protected);
2986 last_page++;
2988 while ((last_page < last_free_page)
2989 && !page_free_p(last_page)
2990 && (page_bytes_used(last_page) != 0)
2991 && (page_table[last_page].gen == from_space));
2993 #ifdef READ_PROTECT_FREE_PAGES
2994 os_protect(page_address(first_page),
2995 npage_bytes(last_page-first_page),
2996 OS_VM_PROT_NONE);
2997 #endif
2998 first_page = last_page;
2999 } while (first_page < last_free_page);
3001 bytes_allocated -= bytes_freed;
3002 return bytes_freed;
3005 #if 0
3006 /* Print some information about a pointer at the given address. */
3007 static void
3008 print_ptr(lispobj *addr)
3010 /* If addr is in the dynamic space then out the page information. */
3011 page_index_t pi1 = find_page_index((void*)addr);
3013 if (pi1 != -1)
3014 fprintf(stderr," %p: page %d alloc %d gen %d bytes_used %d offset %lu dont_move %d\n",
3015 addr,
3016 pi1,
3017 page_table[pi1].allocated,
3018 page_table[pi1].gen,
3019 page_bytes_used(pi1),
3020 scan_start_offset(page_table[pi1]),
3021 page_table[pi1].dont_move);
3022 fprintf(stderr," %x %x %x %x (%x) %x %x %x %x\n",
3023 *(addr-4),
3024 *(addr-3),
3025 *(addr-2),
3026 *(addr-1),
3027 *(addr-0),
3028 *(addr+1),
3029 *(addr+2),
3030 *(addr+3),
3031 *(addr+4));
3033 #endif
3035 static int
3036 is_in_stack_space(lispobj ptr)
3038 /* For space verification: Pointers can be valid if they point
3039 * to a thread stack space. This would be faster if the thread
3040 * structures had page-table entries as if they were part of
3041 * the heap space. */
3042 struct thread *th;
3043 for_each_thread(th) {
3044 if ((th->control_stack_start <= (lispobj *)ptr) &&
3045 (th->control_stack_end >= (lispobj *)ptr)) {
3046 return 1;
3049 return 0;
3052 // NOTE: This function can produces false failure indications,
3053 // usually related to dynamic space pointing to the stack of a
3054 // dead thread, but there may be other reasons as well.
3055 static void
3056 verify_range(lispobj *start, size_t words)
3058 extern int valid_lisp_pointer_p(lispobj);
3059 int is_in_readonly_space =
3060 (READ_ONLY_SPACE_START <= (uword_t)start &&
3061 (uword_t)start < SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0));
3062 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3063 int is_in_immobile_space =
3064 (IMMOBILE_SPACE_START <= (uword_t)start &&
3065 (uword_t)start < SymbolValue(IMMOBILE_SPACE_FREE_POINTER,0));
3066 #endif
3068 lispobj *end = start + words;
3069 size_t count;
3070 for ( ; start < end ; start += count) {
3071 count = 1;
3072 lispobj thing = *start;
3073 lispobj __attribute__((unused)) pointee;
3075 if (is_lisp_pointer(thing)) {
3076 page_index_t page_index = find_page_index((void*)thing);
3077 sword_t to_readonly_space =
3078 (READ_ONLY_SPACE_START <= thing &&
3079 thing < SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0));
3080 sword_t to_static_space =
3081 (STATIC_SPACE_START <= thing &&
3082 thing < SymbolValue(STATIC_SPACE_FREE_POINTER,0));
3083 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3084 sword_t to_immobile_space =
3085 (IMMOBILE_SPACE_START <= thing &&
3086 thing < SymbolValue(IMMOBILE_FIXEDOBJ_FREE_POINTER,0)) ||
3087 (IMMOBILE_VARYOBJ_SUBSPACE_START <= thing &&
3088 thing < SymbolValue(IMMOBILE_SPACE_FREE_POINTER,0));
3089 #endif
3091 /* Does it point to the dynamic space? */
3092 if (page_index != -1) {
3093 /* If it's within the dynamic space it should point to a used page. */
3094 if (page_free_p(page_index))
3095 lose ("Ptr %p @ %p sees free page.\n", thing, start);
3096 if ((thing & (GENCGC_CARD_BYTES-1)) >= page_bytes_used(page_index))
3097 lose ("Ptr %p @ %p sees unallocated space.\n", thing, start);
3098 /* Check that it doesn't point to a forwarding pointer! */
3099 if (*native_pointer(thing) == 0x01) {
3100 lose("Ptr %p @ %p sees forwarding ptr.\n", thing, start);
3102 /* Check that its not in the RO space as it would then be a
3103 * pointer from the RO to the dynamic space. */
3104 if (is_in_readonly_space) {
3105 lose("ptr to dynamic space %p from RO space %x\n",
3106 thing, start);
3108 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3109 // verify all immobile space -> dynamic space pointers
3110 if (is_in_immobile_space && !valid_lisp_pointer_p(thing)) {
3111 lose("Ptr %p @ %p sees junk.\n", thing, start);
3113 #endif
3114 /* Does it point to a plausible object? This check slows
3115 * it down a lot (so it's commented out).
3117 * "a lot" is serious: it ate 50 minutes cpu time on
3118 * my duron 950 before I came back from lunch and
3119 * killed it.
3121 * FIXME: Add a variable to enable this
3122 * dynamically. */
3124 if (!valid_lisp_pointer_p((lispobj *)thing) {
3125 lose("ptr %p to invalid object %p\n", thing, start);
3128 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3129 } else if (to_immobile_space) {
3130 // the object pointed to must not have been discarded as garbage
3131 if (!other_immediate_lowtag_p(*native_pointer(thing))
3132 || immobile_filler_p(native_pointer(thing)))
3133 lose("Ptr %p @ %p sees trashed object.\n", (void*)thing, start);
3134 // verify all pointers to immobile space
3135 if (!valid_lisp_pointer_p(thing))
3136 lose("Ptr %p @ %p sees junk.\n", thing, start);
3137 #endif
3138 } else {
3139 extern char __attribute__((unused)) funcallable_instance_tramp;
3140 /* Verify that it points to another valid space. */
3141 if (!to_readonly_space && !to_static_space
3142 && !is_in_stack_space(thing)) {
3143 lose("Ptr %p @ %p sees junk.\n", thing, start);
3146 continue;
3148 int widetag = widetag_of(thing);
3149 if (is_lisp_immediate(thing) || widetag == NO_TLS_VALUE_MARKER_WIDETAG) {
3150 /* skip immediates */
3151 } else if (!(other_immediate_lowtag_p(widetag)
3152 && lowtag_for_widetag[widetag>>2])) {
3153 lose("Unhandled widetag %p at %p\n", widetag, start);
3154 } else if (unboxed_obj_widetag_p(widetag)) {
3155 count = sizetab[widetag](start);
3156 } else switch(widetag) {
3157 /* boxed or partially boxed objects */
3158 // FIXME: x86-64 can have partially unboxed FINs. The raw words
3159 // are at the moment valid fixnums by blind luck.
3160 case INSTANCE_WIDETAG:
3161 if (instance_layout(start)) {
3162 sword_t nslots = instance_length(thing) | 1;
3163 instance_scan(verify_range, start+1, nslots,
3164 ((struct layout*)
3165 native_pointer(instance_layout(start)))->bitmap);
3166 count = 1 + nslots;
3168 break;
3169 case CODE_HEADER_WIDETAG:
3171 struct code *code = (struct code *) start;
3172 sword_t nheader_words = code_header_words(code->header);
3173 /* Scavenge the boxed section of the code data block */
3174 verify_range(start + 1, nheader_words - 1);
3176 /* Scavenge the boxed section of each function
3177 * object in the code data block. */
3178 for_each_simple_fun(i, fheaderp, code, 1, {
3179 verify_range(SIMPLE_FUN_SCAV_START(fheaderp),
3180 SIMPLE_FUN_SCAV_NWORDS(fheaderp)); });
3181 count = nheader_words + code_instruction_words(code->code_size);
3182 break;
3184 #ifdef LISP_FEATURE_IMMOBILE_CODE
3185 case FDEFN_WIDETAG:
3186 verify_range(start + 1, 2);
3187 pointee = fdefn_raw_referent((struct fdefn*)start);
3188 verify_range(&pointee, 1);
3189 count = CEILING(sizeof (struct fdefn)/sizeof(lispobj), 2);
3190 break;
3191 #endif
3195 static uword_t verify_space(lispobj start, lispobj end) {
3196 verify_range((lispobj*)start, (end-start)>>WORD_SHIFT);
3197 return 0;
3200 static void verify_dynamic_space();
3202 static void
3203 verify_gc(void)
3205 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3206 # ifdef __linux__
3207 // Try this verification if marknsweep was compiled with extra debugging.
3208 // But weak symbols don't work on macOS.
3209 extern void __attribute__((weak)) check_varyobj_pages();
3210 if (&check_varyobj_pages) check_varyobj_pages();
3211 # endif
3212 verify_space(IMMOBILE_SPACE_START,
3213 SymbolValue(IMMOBILE_FIXEDOBJ_FREE_POINTER,0));
3214 verify_space(IMMOBILE_VARYOBJ_SUBSPACE_START,
3215 SymbolValue(IMMOBILE_SPACE_FREE_POINTER,0));
3216 #endif
3217 struct thread *th;
3218 for_each_thread(th) {
3219 verify_space((lispobj)th->binding_stack_start,
3220 (lispobj)get_binding_stack_pointer(th));
3222 verify_space(READ_ONLY_SPACE_START,
3223 SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0));
3224 verify_space(STATIC_SPACE_START,
3225 SymbolValue(STATIC_SPACE_FREE_POINTER,0));
3226 verify_dynamic_space();
3229 /* Call 'proc' with pairs of addresses demarcating ranges in the
3230 * specified generation.
3231 * Stop if any invocation returns non-zero, and return that value */
3232 uword_t
3233 walk_generation(uword_t (*proc)(lispobj*,lispobj*,uword_t),
3234 generation_index_t generation, uword_t extra)
3236 page_index_t i;
3237 int genmask = generation >= 0 ? 1 << generation : ~0;
3239 for (i = 0; i < last_free_page; i++) {
3240 if (!page_free_p(i)
3241 && (page_bytes_used(i) != 0)
3242 && ((1 << page_table[i].gen) & genmask)) {
3243 page_index_t last_page;
3245 /* This should be the start of a contiguous block */
3246 gc_assert(page_starts_contiguous_block_p(i));
3248 /* Need to find the full extent of this contiguous block in case
3249 objects span pages. */
3251 /* Now work forward until the end of this contiguous area is
3252 found. */
3253 for (last_page = i; ;last_page++)
3254 /* Check whether this is the last page in this contiguous
3255 * block. */
3256 if (page_ends_contiguous_block_p(last_page, page_table[i].gen))
3257 break;
3259 uword_t result =
3260 proc((lispobj*)page_address(i),
3261 (lispobj*)(page_bytes_used(last_page) + page_address(last_page)),
3262 extra);
3263 if (result) return result;
3265 i = last_page;
3268 return 0;
3270 static void verify_generation(generation_index_t generation)
3272 walk_generation((uword_t(*)(lispobj*,lispobj*,uword_t))verify_space,
3273 generation, 0);
3276 /* Check that all the free space is zero filled. */
3277 static void
3278 verify_zero_fill(void)
3280 page_index_t page;
3282 for (page = 0; page < last_free_page; page++) {
3283 if (page_free_p(page)) {
3284 /* The whole page should be zero filled. */
3285 sword_t *start_addr = (sword_t *)page_address(page);
3286 sword_t i;
3287 for (i = 0; i < (sword_t)GENCGC_CARD_BYTES/N_WORD_BYTES; i++) {
3288 if (start_addr[i] != 0) {
3289 lose("free page not zero at %x\n", start_addr + i);
3292 } else {
3293 sword_t free_bytes = GENCGC_CARD_BYTES - page_bytes_used(page);
3294 if (free_bytes > 0) {
3295 sword_t *start_addr =
3296 (sword_t *)(page_address(page) + page_bytes_used(page));
3297 sword_t size = free_bytes / N_WORD_BYTES;
3298 sword_t i;
3299 for (i = 0; i < size; i++) {
3300 if (start_addr[i] != 0) {
3301 lose("free region not zero at %x\n", start_addr + i);
3309 /* External entry point for verify_zero_fill */
3310 void
3311 gencgc_verify_zero_fill(void)
3313 /* Flush the alloc regions updating the tables. */
3314 gc_alloc_update_all_page_tables(1);
3315 SHOW("verifying zero fill");
3316 verify_zero_fill();
3319 static void
3320 verify_dynamic_space(void)
3322 verify_generation(-1);
3323 if (gencgc_enable_verify_zero_fill)
3324 verify_zero_fill();
3327 /* Write-protect all the dynamic boxed pages in the given generation. */
3328 static void
3329 write_protect_generation_pages(generation_index_t generation)
3331 page_index_t start;
3333 gc_assert(generation < SCRATCH_GENERATION);
3335 for (start = 0; start < last_free_page; start++) {
3336 if (protect_page_p(start, generation)) {
3337 void *page_start;
3338 page_index_t last;
3340 /* Note the page as protected in the page tables. */
3341 page_table[start].write_protected = 1;
3343 for (last = start + 1; last < last_free_page; last++) {
3344 if (!protect_page_p(last, generation))
3345 break;
3346 page_table[last].write_protected = 1;
3349 page_start = page_address(start);
3351 os_protect(page_start,
3352 npage_bytes(last - start),
3353 OS_VM_PROT_READ | OS_VM_PROT_EXECUTE);
3355 start = last;
3359 if (gencgc_verbose > 1) {
3360 FSHOW((stderr,
3361 "/write protected %d of %d pages in generation %d\n",
3362 count_write_protect_generation_pages(generation),
3363 count_generation_pages(generation),
3364 generation));
3368 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
3369 static void
3370 preserve_context_registers (void (*proc)(os_context_register_t), os_context_t *c)
3372 #ifdef LISP_FEATURE_SB_THREAD
3373 void **ptr;
3374 /* On Darwin the signal context isn't a contiguous block of memory,
3375 * so just preserve_pointering its contents won't be sufficient.
3377 #if defined(LISP_FEATURE_DARWIN)||defined(LISP_FEATURE_WIN32)
3378 #if defined LISP_FEATURE_X86
3379 proc(*os_context_register_addr(c,reg_EAX));
3380 proc(*os_context_register_addr(c,reg_ECX));
3381 proc(*os_context_register_addr(c,reg_EDX));
3382 proc(*os_context_register_addr(c,reg_EBX));
3383 proc(*os_context_register_addr(c,reg_ESI));
3384 proc(*os_context_register_addr(c,reg_EDI));
3385 proc(*os_context_pc_addr(c));
3386 #elif defined LISP_FEATURE_X86_64
3387 proc(*os_context_register_addr(c,reg_RAX));
3388 proc(*os_context_register_addr(c,reg_RCX));
3389 proc(*os_context_register_addr(c,reg_RDX));
3390 proc(*os_context_register_addr(c,reg_RBX));
3391 proc(*os_context_register_addr(c,reg_RSI));
3392 proc(*os_context_register_addr(c,reg_RDI));
3393 proc(*os_context_register_addr(c,reg_R8));
3394 proc(*os_context_register_addr(c,reg_R9));
3395 proc(*os_context_register_addr(c,reg_R10));
3396 proc(*os_context_register_addr(c,reg_R11));
3397 proc(*os_context_register_addr(c,reg_R12));
3398 proc(*os_context_register_addr(c,reg_R13));
3399 proc(*os_context_register_addr(c,reg_R14));
3400 proc(*os_context_register_addr(c,reg_R15));
3401 proc(*os_context_pc_addr(c));
3402 #else
3403 #error "preserve_context_registers needs to be tweaked for non-x86 Darwin"
3404 #endif
3405 #endif
3406 #if !defined(LISP_FEATURE_WIN32)
3407 for(ptr = ((void **)(c+1))-1; ptr>=(void **)c; ptr--) {
3408 proc((os_context_register_t)*ptr);
3410 #endif
3411 #endif // LISP_FEATURE_SB_THREAD
3413 #endif
3415 static void
3416 move_pinned_pages_to_newspace()
3418 page_index_t i;
3420 /* scavenge() will evacuate all oldspace pages, but no newspace
3421 * pages. Pinned pages are precisely those pages which must not
3422 * be evacuated, so move them to newspace directly. */
3424 for (i = 0; i < last_free_page; i++) {
3425 if (page_table[i].dont_move &&
3426 /* dont_move is cleared lazily, so validate the space as well. */
3427 page_table[i].gen == from_space) {
3428 if (do_wipe_p && page_table[i].has_pins) {
3429 // do not move to newspace after all, this will be word-wiped
3430 continue;
3432 page_table[i].gen = new_space;
3433 /* And since we're moving the pages wholesale, also adjust
3434 * the generation allocation counters. */
3435 int used = page_bytes_used(i);
3436 generations[new_space].bytes_allocated += used;
3437 generations[from_space].bytes_allocated -= used;
3442 /* Garbage collect a generation. If raise is 0 then the remains of the
3443 * generation are not raised to the next generation. */
3444 static void
3445 garbage_collect_generation(generation_index_t generation, int raise)
3447 page_index_t i;
3448 struct thread *th;
3450 gc_assert(generation <= HIGHEST_NORMAL_GENERATION);
3452 /* The oldest generation can't be raised. */
3453 gc_assert((generation != HIGHEST_NORMAL_GENERATION) || (raise == 0));
3455 /* Check if weak hash tables were processed in the previous GC. */
3456 gc_assert(weak_hash_tables == NULL);
3458 /* Initialize the weak pointer list. */
3459 weak_pointers = NULL;
3461 /* When a generation is not being raised it is transported to a
3462 * temporary generation (NUM_GENERATIONS), and lowered when
3463 * done. Set up this new generation. There should be no pages
3464 * allocated to it yet. */
3465 if (!raise) {
3466 gc_assert(generations[SCRATCH_GENERATION].bytes_allocated == 0);
3469 /* Set the global src and dest. generations */
3470 from_space = generation;
3471 if (raise)
3472 new_space = generation+1;
3473 else
3474 new_space = SCRATCH_GENERATION;
3476 /* Change to a new space for allocation, resetting the alloc_start_page */
3477 gc_alloc_generation = new_space;
3478 #ifdef LISP_FEATURE_SEGREGATED_CODE
3479 bzero(generations[new_space].alloc_start_page_,
3480 sizeof generations[new_space].alloc_start_page_);
3481 #else
3482 generations[new_space].alloc_start_page = 0;
3483 generations[new_space].alloc_unboxed_start_page = 0;
3484 generations[new_space].alloc_large_start_page = 0;
3485 #endif
3487 #ifndef GENCGC_IS_PRECISE
3488 hopscotch_reset(&pinned_objects);
3489 #endif
3490 /* Before any pointers are preserved, the dont_move flags on the
3491 * pages need to be cleared. */
3492 /* FIXME: consider moving this bitmap into its own range of words,
3493 * out of the page table. Then we can just bzero() it.
3494 * This will also obviate the extra test at the comment
3495 * "dont_move is cleared lazily" in move_pinned_pages_to_newspace().
3497 for (i = 0; i < last_free_page; i++)
3498 if(page_table[i].gen==from_space) {
3499 page_table[i].dont_move = 0;
3502 /* Un-write-protect the old-space pages. This is essential for the
3503 * promoted pages as they may contain pointers into the old-space
3504 * which need to be scavenged. It also helps avoid unnecessary page
3505 * faults as forwarding pointers are written into them. They need to
3506 * be un-protected anyway before unmapping later. */
3507 unprotect_oldspace();
3509 /* Scavenge the stacks' conservative roots. */
3511 /* there are potentially two stacks for each thread: the main
3512 * stack, which may contain Lisp pointers, and the alternate stack.
3513 * We don't ever run Lisp code on the altstack, but it may
3514 * host a sigcontext with lisp objects in it */
3516 /* what we need to do: (1) find the stack pointer for the main
3517 * stack; scavenge it (2) find the interrupt context on the
3518 * alternate stack that might contain lisp values, and scavenge
3519 * that */
3521 /* we assume that none of the preceding applies to the thread that
3522 * initiates GC. If you ever call GC from inside an altstack
3523 * handler, you will lose. */
3525 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
3526 /* And if we're saving a core, there's no point in being conservative. */
3527 if (conservative_stack) {
3528 for_each_thread(th) {
3529 void **ptr;
3530 void **esp=(void **)-1;
3531 if (th->state == STATE_DEAD)
3532 continue;
3533 # if defined(LISP_FEATURE_SB_SAFEPOINT)
3534 /* Conservative collect_garbage is always invoked with a
3535 * foreign C call or an interrupt handler on top of every
3536 * existing thread, so the stored SP in each thread
3537 * structure is valid, no matter which thread we are looking
3538 * at. For threads that were running Lisp code, the pitstop
3539 * and edge functions maintain this value within the
3540 * interrupt or exception handler. */
3541 esp = os_get_csp(th);
3542 assert_on_stack(th, esp);
3544 /* In addition to pointers on the stack, also preserve the
3545 * return PC, the only value from the context that we need
3546 * in addition to the SP. The return PC gets saved by the
3547 * foreign call wrapper, and removed from the control stack
3548 * into a register. */
3549 preserve_pointer(th->pc_around_foreign_call);
3551 /* And on platforms with interrupts: scavenge ctx registers. */
3553 /* Disabled on Windows, because it does not have an explicit
3554 * stack of `interrupt_contexts'. The reported CSP has been
3555 * chosen so that the current context on the stack is
3556 * covered by the stack scan. See also set_csp_from_context(). */
3557 # ifndef LISP_FEATURE_WIN32
3558 if (th != arch_os_get_current_thread()) {
3559 long k = fixnum_value(
3560 SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,th));
3561 while (k > 0)
3562 preserve_context_registers((void(*)(os_context_register_t))preserve_pointer,
3563 th->interrupt_contexts[--k]);
3565 # endif
3566 # elif defined(LISP_FEATURE_SB_THREAD)
3567 sword_t i,free;
3568 if(th==arch_os_get_current_thread()) {
3569 /* Somebody is going to burn in hell for this, but casting
3570 * it in two steps shuts gcc up about strict aliasing. */
3571 esp = (void **)((void *)&raise);
3572 } else {
3573 void **esp1;
3574 free=fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,th));
3575 for(i=free-1;i>=0;i--) {
3576 os_context_t *c=th->interrupt_contexts[i];
3577 esp1 = (void **) *os_context_register_addr(c,reg_SP);
3578 if (esp1>=(void **)th->control_stack_start &&
3579 esp1<(void **)th->control_stack_end) {
3580 if(esp1<esp) esp=esp1;
3581 preserve_context_registers((void(*)(os_context_register_t))preserve_pointer,
3586 # else
3587 esp = (void **)((void *)&raise);
3588 # endif
3589 if (!esp || esp == (void*) -1)
3590 lose("garbage_collect: no SP known for thread %x (OS %x)",
3591 th, th->os_thread);
3592 for (ptr = ((void **)th->control_stack_end)-1; ptr >= esp; ptr--) {
3593 preserve_pointer(*ptr);
3597 #else
3598 /* Non-x86oid systems don't have "conservative roots" as such, but
3599 * the same mechanism is used for objects pinned for use by alien
3600 * code. */
3601 for_each_thread(th) {
3602 lispobj pin_list = SymbolTlValue(PINNED_OBJECTS,th);
3603 while (pin_list != NIL) {
3604 struct cons *list_entry =
3605 (struct cons *)native_pointer(pin_list);
3606 preserve_pointer((void*)list_entry->car);
3607 pin_list = list_entry->cdr;
3610 #endif
3612 #if QSHOW
3613 if (gencgc_verbose > 1) {
3614 sword_t num_dont_move_pages = count_dont_move_pages();
3615 fprintf(stderr,
3616 "/non-movable pages due to conservative pointers = %ld (%lu bytes)\n",
3617 num_dont_move_pages,
3618 npage_bytes(num_dont_move_pages));
3620 #endif
3622 /* Now that all of the pinned (dont_move) pages are known, and
3623 * before we start to scavenge (and thus relocate) objects,
3624 * relocate the pinned pages to newspace, so that the scavenger
3625 * will not attempt to relocate their contents. */
3626 move_pinned_pages_to_newspace();
3628 /* Scavenge all the rest of the roots. */
3630 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
3632 * If not x86, we need to scavenge the interrupt context(s) and the
3633 * control stack.
3636 struct thread *th;
3637 for_each_thread(th) {
3638 scavenge_interrupt_contexts(th);
3639 scavenge_control_stack(th);
3642 # ifdef LISP_FEATURE_SB_SAFEPOINT
3643 /* In this case, scrub all stacks right here from the GCing thread
3644 * instead of doing what the comment below says. Suboptimal, but
3645 * easier. */
3646 for_each_thread(th)
3647 scrub_thread_control_stack(th);
3648 # else
3649 /* Scrub the unscavenged control stack space, so that we can't run
3650 * into any stale pointers in a later GC (this is done by the
3651 * stop-for-gc handler in the other threads). */
3652 scrub_control_stack();
3653 # endif
3655 #endif
3657 /* Scavenge the Lisp functions of the interrupt handlers, taking
3658 * care to avoid SIG_DFL and SIG_IGN. */
3659 for (i = 0; i < NSIG; i++) {
3660 union interrupt_handler handler = interrupt_handlers[i];
3661 if (!ARE_SAME_HANDLER(handler.c, SIG_IGN) &&
3662 !ARE_SAME_HANDLER(handler.c, SIG_DFL)) {
3663 scavenge((lispobj *)(interrupt_handlers + i), 1);
3666 /* Scavenge the binding stacks. */
3668 struct thread *th;
3669 for_each_thread(th) {
3670 sword_t len= (lispobj *)get_binding_stack_pointer(th) -
3671 th->binding_stack_start;
3672 scavenge((lispobj *) th->binding_stack_start,len);
3673 #ifdef LISP_FEATURE_SB_THREAD
3674 /* do the tls as well */
3675 len=(SymbolValue(FREE_TLS_INDEX,0) >> WORD_SHIFT) -
3676 (sizeof (struct thread))/(sizeof (lispobj));
3677 scavenge((lispobj *) (th+1),len);
3678 #endif
3682 /* Scavenge static space. */
3683 if (gencgc_verbose > 1) {
3684 FSHOW((stderr,
3685 "/scavenge static space: %d bytes\n",
3686 SymbolValue(STATIC_SPACE_FREE_POINTER,0) - STATIC_SPACE_START));
3688 heap_scavenge((lispobj*)STATIC_SPACE_START,
3689 (lispobj*)SymbolValue(STATIC_SPACE_FREE_POINTER,0));
3691 /* All generations but the generation being GCed need to be
3692 * scavenged. The new_space generation needs special handling as
3693 * objects may be moved in - it is handled separately below. */
3694 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3695 scavenge_immobile_roots(generation+1, SCRATCH_GENERATION);
3696 #endif
3697 scavenge_generations(generation+1, PSEUDO_STATIC_GENERATION);
3699 #ifdef LISP_FEATURE_SB_TRACEROOT
3700 scavenge(&gc_object_watcher, 1);
3701 #endif
3702 scavenge_pinned_ranges();
3704 /* Finally scavenge the new_space generation. Keep going until no
3705 * more objects are moved into the new generation */
3706 scavenge_newspace_generation(new_space);
3708 /* FIXME: I tried reenabling this check when debugging unrelated
3709 * GC weirdness ca. sbcl-0.6.12.45, and it failed immediately.
3710 * Since the current GC code seems to work well, I'm guessing that
3711 * this debugging code is just stale, but I haven't tried to
3712 * figure it out. It should be figured out and then either made to
3713 * work or just deleted. */
3715 #define RESCAN_CHECK 0
3716 #if RESCAN_CHECK
3717 /* As a check re-scavenge the newspace once; no new objects should
3718 * be found. */
3720 os_vm_size_t old_bytes_allocated = bytes_allocated;
3721 os_vm_size_t bytes_allocated;
3723 /* Start with a full scavenge. */
3724 scavenge_newspace_generation_one_scan(new_space);
3726 /* Flush the current regions, updating the tables. */
3727 gc_alloc_update_all_page_tables(1);
3729 bytes_allocated = bytes_allocated - old_bytes_allocated;
3731 if (bytes_allocated != 0) {
3732 lose("Rescan of new_space allocated %d more bytes.\n",
3733 bytes_allocated);
3736 #endif
3738 scan_weak_hash_tables();
3739 scan_weak_pointers();
3740 wipe_nonpinned_words();
3741 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3742 // Do this last, because until wipe_nonpinned_words() happens,
3743 // not all page table entries have the 'gen' value updated,
3744 // which we need to correctly find all old->young pointers.
3745 sweep_immobile_space(raise);
3746 #endif
3748 /* Flush the current regions, updating the tables. */
3749 gc_alloc_update_all_page_tables(0);
3750 #ifndef GENCGC_IS_PRECISE
3751 hopscotch_log_stats(&pinned_objects, "pins");
3752 #endif
3754 /* Free the pages in oldspace, but not those marked dont_move. */
3755 free_oldspace();
3757 /* If the GC is not raising the age then lower the generation back
3758 * to its normal generation number */
3759 if (!raise) {
3760 for (i = 0; i < last_free_page; i++)
3761 if ((page_bytes_used(i) != 0)
3762 && (page_table[i].gen == SCRATCH_GENERATION))
3763 page_table[i].gen = generation;
3764 gc_assert(generations[generation].bytes_allocated == 0);
3765 generations[generation].bytes_allocated =
3766 generations[SCRATCH_GENERATION].bytes_allocated;
3767 generations[SCRATCH_GENERATION].bytes_allocated = 0;
3770 /* Reset the alloc_start_page for generation. */
3771 #ifdef LISP_FEATURE_SEGREGATED_CODE
3772 bzero(generations[generation].alloc_start_page_,
3773 sizeof generations[generation].alloc_start_page_);
3774 #else
3775 generations[generation].alloc_start_page = 0;
3776 generations[generation].alloc_unboxed_start_page = 0;
3777 generations[generation].alloc_large_start_page = 0;
3778 #endif
3780 if (generation >= verify_gens) {
3781 if (gencgc_verbose) {
3782 SHOW("verifying");
3784 verify_gc();
3787 /* Set the new gc trigger for the GCed generation. */
3788 generations[generation].gc_trigger =
3789 generations[generation].bytes_allocated
3790 + generations[generation].bytes_consed_between_gc;
3792 if (raise)
3793 generations[generation].num_gc = 0;
3794 else
3795 ++generations[generation].num_gc;
3799 /* Update last_free_page, then SymbolValue(ALLOCATION_POINTER). */
3800 sword_t
3801 update_dynamic_space_free_pointer(void)
3803 page_index_t last_page = -1, i;
3805 for (i = 0; i < last_free_page; i++)
3806 if (!page_free_p(i) && (page_bytes_used(i) != 0))
3807 last_page = i;
3809 last_free_page = last_page+1;
3811 set_alloc_pointer((lispobj)(page_address(last_free_page)));
3812 return 0; /* dummy value: return something ... */
3815 static void
3816 remap_page_range (page_index_t from, page_index_t to)
3818 /* There's a mysterious Solaris/x86 problem with using mmap
3819 * tricks for memory zeroing. See sbcl-devel thread
3820 * "Re: patch: standalone executable redux".
3822 #if defined(LISP_FEATURE_SUNOS)
3823 zero_and_mark_pages(from, to);
3824 #else
3825 const page_index_t
3826 release_granularity = gencgc_release_granularity/GENCGC_CARD_BYTES,
3827 release_mask = release_granularity-1,
3828 end = to+1,
3829 aligned_from = (from+release_mask)&~release_mask,
3830 aligned_end = (end&~release_mask);
3832 if (aligned_from < aligned_end) {
3833 zero_pages_with_mmap(aligned_from, aligned_end-1);
3834 if (aligned_from != from)
3835 zero_and_mark_pages(from, aligned_from-1);
3836 if (aligned_end != end)
3837 zero_and_mark_pages(aligned_end, end-1);
3838 } else {
3839 zero_and_mark_pages(from, to);
3841 #endif
3844 static void
3845 remap_free_pages (page_index_t from, page_index_t to, int forcibly)
3847 page_index_t first_page, last_page;
3849 if (forcibly)
3850 return remap_page_range(from, to);
3852 for (first_page = from; first_page <= to; first_page++) {
3853 if (!page_free_p(first_page) || !page_need_to_zero(first_page))
3854 continue;
3856 last_page = first_page + 1;
3857 while (page_free_p(last_page) &&
3858 (last_page <= to) &&
3859 (page_need_to_zero(last_page)))
3860 last_page++;
3862 remap_page_range(first_page, last_page-1);
3864 first_page = last_page;
3868 generation_index_t small_generation_limit = 1;
3870 /* GC all generations newer than last_gen, raising the objects in each
3871 * to the next older generation - we finish when all generations below
3872 * last_gen are empty. Then if last_gen is due for a GC, or if
3873 * last_gen==NUM_GENERATIONS (the scratch generation? eh?) we GC that
3874 * too. The valid range for last_gen is: 0,1,...,NUM_GENERATIONS.
3876 * We stop collecting at gencgc_oldest_gen_to_gc, even if this is less than
3877 * last_gen (oh, and note that by default it is NUM_GENERATIONS-1) */
3878 void
3879 collect_garbage(generation_index_t last_gen)
3881 generation_index_t gen = 0, i;
3882 int raise, more = 0;
3883 int gen_to_wp;
3884 /* The largest value of last_free_page seen since the time
3885 * remap_free_pages was called. */
3886 static page_index_t high_water_mark = 0;
3888 FSHOW((stderr, "/entering collect_garbage(%d)\n", last_gen));
3889 log_generation_stats(gc_logfile, "=== GC Start ===");
3891 gc_active_p = 1;
3893 if (last_gen > HIGHEST_NORMAL_GENERATION+1) {
3894 FSHOW((stderr,
3895 "/collect_garbage: last_gen = %d, doing a level 0 GC\n",
3896 last_gen));
3897 last_gen = 0;
3900 /* Flush the alloc regions updating the tables. */
3901 gc_alloc_update_all_page_tables(1);
3903 /* Verify the new objects created by Lisp code. */
3904 if (pre_verify_gen_0) {
3905 FSHOW((stderr, "pre-checking generation 0\n"));
3906 verify_generation(0);
3909 if (gencgc_verbose > 1)
3910 print_generation_stats();
3912 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3913 /* Immobile space generation bits are lazily updated for gen0
3914 (not touched on every object allocation) so do it now */
3915 update_immobile_nursery_bits();
3916 #endif
3918 do {
3919 /* Collect the generation. */
3921 if (more || (gen >= gencgc_oldest_gen_to_gc)) {
3922 /* Never raise the oldest generation. Never raise the extra generation
3923 * collected due to more-flag. */
3924 raise = 0;
3925 more = 0;
3926 } else {
3927 raise =
3928 (gen < last_gen)
3929 || (generations[gen].num_gc >= generations[gen].number_of_gcs_before_promotion);
3930 /* If we would not normally raise this one, but we're
3931 * running low on space in comparison to the object-sizes
3932 * we've been seeing, raise it and collect the next one
3933 * too. */
3934 if (!raise && gen == last_gen) {
3935 more = (2*large_allocation) >= (dynamic_space_size - bytes_allocated);
3936 raise = more;
3940 if (gencgc_verbose > 1) {
3941 FSHOW((stderr,
3942 "starting GC of generation %d with raise=%d alloc=%d trig=%d GCs=%d\n",
3943 gen,
3944 raise,
3945 generations[gen].bytes_allocated,
3946 generations[gen].gc_trigger,
3947 generations[gen].num_gc));
3950 /* If an older generation is being filled, then update its
3951 * memory age. */
3952 if (raise == 1) {
3953 generations[gen+1].cum_sum_bytes_allocated +=
3954 generations[gen+1].bytes_allocated;
3957 garbage_collect_generation(gen, raise);
3959 /* Reset the memory age cum_sum. */
3960 generations[gen].cum_sum_bytes_allocated = 0;
3962 if (gencgc_verbose > 1) {
3963 FSHOW((stderr, "GC of generation %d finished:\n", gen));
3964 print_generation_stats();
3967 gen++;
3968 } while ((gen <= gencgc_oldest_gen_to_gc)
3969 && ((gen < last_gen)
3970 || more
3971 || (raise
3972 && (generations[gen].bytes_allocated
3973 > generations[gen].gc_trigger)
3974 && (generation_average_age(gen)
3975 > generations[gen].minimum_age_before_gc))));
3977 /* Now if gen-1 was raised all generations before gen are empty.
3978 * If it wasn't raised then all generations before gen-1 are empty.
3980 * Now objects within this gen's pages cannot point to younger
3981 * generations unless they are written to. This can be exploited
3982 * by write-protecting the pages of gen; then when younger
3983 * generations are GCed only the pages which have been written
3984 * need scanning. */
3985 if (raise)
3986 gen_to_wp = gen;
3987 else
3988 gen_to_wp = gen - 1;
3990 /* There's not much point in WPing pages in generation 0 as it is
3991 * never scavenged (except promoted pages). */
3992 if ((gen_to_wp > 0) && enable_page_protection) {
3993 /* Check that they are all empty. */
3994 for (i = 0; i < gen_to_wp; i++) {
3995 if (generations[i].bytes_allocated)
3996 lose("trying to write-protect gen. %d when gen. %d nonempty\n",
3997 gen_to_wp, i);
3999 write_protect_generation_pages(gen_to_wp);
4001 #ifdef LISP_FEATURE_IMMOBILE_SPACE
4002 write_protect_immobile_space();
4003 #endif
4005 /* Set gc_alloc() back to generation 0. The current regions should
4006 * be flushed after the above GCs. */
4007 gc_assert(boxed_region.free_pointer == boxed_region.start_addr);
4008 gc_alloc_generation = 0;
4010 /* Save the high-water mark before updating last_free_page */
4011 if (last_free_page > high_water_mark)
4012 high_water_mark = last_free_page;
4014 update_dynamic_space_free_pointer();
4016 /* Update auto_gc_trigger. Make sure we trigger the next GC before
4017 * running out of heap! */
4018 if (bytes_consed_between_gcs <= (dynamic_space_size - bytes_allocated))
4019 auto_gc_trigger = bytes_allocated + bytes_consed_between_gcs;
4020 else
4021 auto_gc_trigger = bytes_allocated + (dynamic_space_size - bytes_allocated)/2;
4023 if(gencgc_verbose)
4024 fprintf(stderr,"Next gc when %"OS_VM_SIZE_FMT" bytes have been consed\n",
4025 auto_gc_trigger);
4027 /* If we did a big GC (arbitrarily defined as gen > 1), release memory
4028 * back to the OS.
4030 if (gen > small_generation_limit) {
4031 if (last_free_page > high_water_mark)
4032 high_water_mark = last_free_page;
4033 remap_free_pages(0, high_water_mark, 0);
4034 high_water_mark = 0;
4037 gc_active_p = 0;
4038 large_allocation = 0;
4040 #ifdef LISP_FEATURE_SB_TRACEROOT
4041 if (gc_object_watcher) {
4042 extern void gc_prove_liveness(void(*)(), lispobj, int, uword_t*);
4043 gc_prove_liveness(preserve_context_registers,
4044 gc_object_watcher,
4045 gc_n_stack_pins, pinned_objects.keys);
4047 #endif
4049 log_generation_stats(gc_logfile, "=== GC End ===");
4050 SHOW("returning from collect_garbage");
4053 void
4054 gc_init(void)
4056 page_index_t i;
4058 #if defined(LISP_FEATURE_SB_SAFEPOINT)
4059 alloc_gc_page();
4060 #endif
4062 /* Compute the number of pages needed for the dynamic space.
4063 * Dynamic space size should be aligned on page size. */
4064 page_table_pages = dynamic_space_size/GENCGC_CARD_BYTES;
4065 gc_assert(dynamic_space_size == npage_bytes(page_table_pages));
4067 /* Default nursery size to 5% of the total dynamic space size,
4068 * min 1Mb. */
4069 bytes_consed_between_gcs = dynamic_space_size/(os_vm_size_t)20;
4070 if (bytes_consed_between_gcs < (1024*1024))
4071 bytes_consed_between_gcs = 1024*1024;
4073 /* The page_table must be allocated using "calloc" to initialize
4074 * the page structures correctly. There used to be a separate
4075 * initialization loop (now commented out; see below) but that was
4076 * unnecessary and did hurt startup time. */
4077 page_table = calloc(page_table_pages, sizeof(struct page));
4078 gc_assert(page_table);
4079 #ifdef LISP_FEATURE_IMMOBILE_SPACE
4080 gc_init_immobile();
4081 #endif
4083 hopscotch_init();
4084 #ifndef GENCGC_IS_PRECISE
4085 hopscotch_create(&pinned_objects, HOPSCOTCH_HASH_FUN_DEFAULT, 0 /* hashset */,
4086 32 /* logical bin count */, 0 /* default range */);
4087 #endif
4089 scavtab[WEAK_POINTER_WIDETAG] = scav_weak_pointer;
4090 transother[SIMPLE_ARRAY_WIDETAG] = trans_boxed_large;
4092 /* The page structures are initialized implicitly when page_table
4093 * is allocated with "calloc" above. Formerly we had the following
4094 * explicit initialization here (comments converted to C99 style
4095 * for readability as C's block comments don't nest):
4097 * // Initialize each page structure.
4098 * for (i = 0; i < page_table_pages; i++) {
4099 * // Initialize all pages as free.
4100 * page_table[i].allocated = FREE_PAGE_FLAG;
4101 * page_table[i].bytes_used = 0;
4103 * // Pages are not write-protected at startup.
4104 * page_table[i].write_protected = 0;
4107 * Without this loop the image starts up much faster when dynamic
4108 * space is large -- which it is on 64-bit platforms already by
4109 * default -- and when "calloc" for large arrays is implemented
4110 * using copy-on-write of a page of zeroes -- which it is at least
4111 * on Linux. In this case the pages that page_table_pages is stored
4112 * in are mapped and cleared not before the corresponding part of
4113 * dynamic space is used. For example, this saves clearing 16 MB of
4114 * memory at startup if the page size is 4 KB and the size of
4115 * dynamic space is 4 GB.
4116 * FREE_PAGE_FLAG must be 0 for this to work correctly which is
4117 * asserted below: */
4119 /* Compile time assertion: If triggered, declares an array
4120 * of dimension -1 forcing a syntax error. The intent of the
4121 * assignment is to avoid an "unused variable" warning. */
4122 char assert_free_page_flag_0[(FREE_PAGE_FLAG) ? -1 : 1];
4123 assert_free_page_flag_0[0] = assert_free_page_flag_0[0];
4126 bytes_allocated = 0;
4128 /* Initialize the generations. */
4129 for (i = 0; i < NUM_GENERATIONS; i++) {
4130 generations[i].alloc_start_page = 0;
4131 generations[i].alloc_unboxed_start_page = 0;
4132 generations[i].alloc_large_start_page = 0;
4133 generations[i].bytes_allocated = 0;
4134 generations[i].gc_trigger = 2000000;
4135 generations[i].num_gc = 0;
4136 generations[i].cum_sum_bytes_allocated = 0;
4137 /* the tune-able parameters */
4138 generations[i].bytes_consed_between_gc
4139 = bytes_consed_between_gcs/(os_vm_size_t)HIGHEST_NORMAL_GENERATION;
4140 generations[i].number_of_gcs_before_promotion = 1;
4141 generations[i].minimum_age_before_gc = 0.75;
4144 /* Initialize gc_alloc. */
4145 gc_alloc_generation = 0;
4146 gc_set_region_empty(&boxed_region);
4147 gc_set_region_empty(&unboxed_region);
4148 #ifdef LISP_FEATURE_SEGREGATED_CODE
4149 gc_set_region_empty(&code_region);
4150 #endif
4152 last_free_page = 0;
4155 /* Pick up the dynamic space from after a core load.
4157 * The ALLOCATION_POINTER points to the end of the dynamic space.
4160 static void
4161 gencgc_pickup_dynamic(void)
4163 page_index_t page = 0;
4164 char *alloc_ptr = (char *)get_alloc_pointer();
4165 lispobj *prev=(lispobj *)page_address(page);
4166 generation_index_t gen = PSEUDO_STATIC_GENERATION;
4168 bytes_allocated = 0;
4170 do {
4171 lispobj *first,*ptr= (lispobj *)page_address(page);
4173 if (!gencgc_partial_pickup || !page_free_p(page)) {
4174 /* It is possible, though rare, for the saved page table
4175 * to contain free pages below alloc_ptr. */
4176 page_table[page].gen = gen;
4177 set_page_bytes_used(page, GENCGC_CARD_BYTES);
4178 page_table[page].large_object = 0;
4179 page_table[page].write_protected = 0;
4180 page_table[page].write_protected_cleared = 0;
4181 page_table[page].dont_move = 0;
4182 set_page_need_to_zero(page, 1);
4184 bytes_allocated += GENCGC_CARD_BYTES;
4187 if (!gencgc_partial_pickup) {
4188 #ifdef LISP_FEATURE_SEGREGATED_CODE
4189 // Make the most general assumption: any page *might* contain code.
4190 page_table[page].allocated = CODE_PAGE_FLAG;
4191 #else
4192 page_table[page].allocated = BOXED_PAGE_FLAG;
4193 #endif
4194 first = gc_search_space3(ptr, prev, (ptr+2));
4195 if(ptr == first)
4196 prev=ptr;
4197 set_page_scan_start_offset(page, page_address(page) - (char*)prev);
4199 page++;
4200 } while (page_address(page) < alloc_ptr);
4202 last_free_page = page;
4204 generations[gen].bytes_allocated = bytes_allocated;
4206 gc_alloc_update_all_page_tables(1);
4207 write_protect_generation_pages(gen);
4210 void
4211 gc_initialize_pointers(void)
4213 gencgc_pickup_dynamic();
4217 /* alloc(..) is the external interface for memory allocation. It
4218 * allocates to generation 0. It is not called from within the garbage
4219 * collector as it is only external uses that need the check for heap
4220 * size (GC trigger) and to disable the interrupts (interrupts are
4221 * always disabled during a GC).
4223 * The vops that call alloc(..) assume that the returned space is zero-filled.
4224 * (E.g. the most significant word of a 2-word bignum in MOVE-FROM-UNSIGNED.)
4226 * The check for a GC trigger is only performed when the current
4227 * region is full, so in most cases it's not needed. */
4229 static inline lispobj *
4230 general_alloc_internal(sword_t nbytes, int page_type_flag, struct alloc_region *region,
4231 struct thread *thread)
4233 #ifndef LISP_FEATURE_WIN32
4234 lispobj alloc_signal;
4235 #endif
4236 void *new_obj;
4237 void *new_free_pointer;
4238 os_vm_size_t trigger_bytes = 0;
4240 gc_assert(nbytes > 0);
4242 /* Check for alignment allocation problems. */
4243 gc_assert((((uword_t)region->free_pointer & LOWTAG_MASK) == 0)
4244 && ((nbytes & LOWTAG_MASK) == 0));
4246 #if !(defined(LISP_FEATURE_WIN32) && defined(LISP_FEATURE_SB_THREAD))
4247 /* Must be inside a PA section. */
4248 gc_assert(get_pseudo_atomic_atomic(thread));
4249 #endif
4251 if ((os_vm_size_t) nbytes > large_allocation)
4252 large_allocation = nbytes;
4254 /* maybe we can do this quickly ... */
4255 new_free_pointer = (char*)region->free_pointer + nbytes;
4256 if (new_free_pointer <= region->end_addr) {
4257 new_obj = (void*)(region->free_pointer);
4258 region->free_pointer = new_free_pointer;
4259 return(new_obj); /* yup */
4262 /* We don't want to count nbytes against auto_gc_trigger unless we
4263 * have to: it speeds up the tenuring of objects and slows down
4264 * allocation. However, unless we do so when allocating _very_
4265 * large objects we are in danger of exhausting the heap without
4266 * running sufficient GCs.
4268 if ((os_vm_size_t) nbytes >= bytes_consed_between_gcs)
4269 trigger_bytes = nbytes;
4271 /* we have to go the long way around, it seems. Check whether we
4272 * should GC in the near future
4274 if (auto_gc_trigger && (bytes_allocated+trigger_bytes > auto_gc_trigger)) {
4275 /* Don't flood the system with interrupts if the need to gc is
4276 * already noted. This can happen for example when SUB-GC
4277 * allocates or after a gc triggered in a WITHOUT-GCING. */
4278 if (SymbolValue(GC_PENDING,thread) == NIL) {
4279 /* set things up so that GC happens when we finish the PA
4280 * section */
4281 SetSymbolValue(GC_PENDING,T,thread);
4282 if (SymbolValue(GC_INHIBIT,thread) == NIL) {
4283 #ifdef LISP_FEATURE_SB_SAFEPOINT
4284 thread_register_gc_trigger();
4285 #else
4286 set_pseudo_atomic_interrupted(thread);
4287 #ifdef GENCGC_IS_PRECISE
4288 /* PPC calls alloc() from a trap
4289 * look up the most context if it's from a trap. */
4291 os_context_t *context =
4292 thread->interrupt_data->allocation_trap_context;
4293 maybe_save_gc_mask_and_block_deferrables
4294 (context ? os_context_sigmask_addr(context) : NULL);
4296 #else
4297 maybe_save_gc_mask_and_block_deferrables(NULL);
4298 #endif
4299 #endif
4303 new_obj = gc_alloc_with_region(nbytes, page_type_flag, region, 0);
4305 #ifndef LISP_FEATURE_WIN32
4306 /* for sb-prof, and not supported on Windows yet */
4307 alloc_signal = SymbolValue(ALLOC_SIGNAL,thread);
4308 if ((alloc_signal & FIXNUM_TAG_MASK) == 0) {
4309 if ((sword_t) alloc_signal <= 0) {
4310 SetSymbolValue(ALLOC_SIGNAL, T, thread);
4311 raise(SIGPROF);
4312 } else {
4313 SetSymbolValue(ALLOC_SIGNAL,
4314 alloc_signal - (1 << N_FIXNUM_TAG_BITS),
4315 thread);
4318 #endif
4320 return (new_obj);
4323 lispobj *
4324 general_alloc(sword_t nbytes, int page_type_flag)
4326 struct thread *thread = arch_os_get_current_thread();
4327 /* Select correct region, and call general_alloc_internal with it.
4328 * For other then boxed allocation we must lock first, since the
4329 * region is shared. */
4330 #ifdef LISP_FEATURE_SEGREGATED_CODE
4331 if (page_type_flag == BOXED_PAGE_FLAG) {
4332 #else
4333 if (BOXED_PAGE_FLAG & page_type_flag) {
4334 #endif
4335 #ifdef LISP_FEATURE_SB_THREAD
4336 struct alloc_region *region = (thread ? &(thread->alloc_region) : &boxed_region);
4337 #else
4338 struct alloc_region *region = &boxed_region;
4339 #endif
4340 return general_alloc_internal(nbytes, page_type_flag, region, thread);
4341 #ifdef LISP_FEATURE_SEGREGATED_CODE
4342 } else if (page_type_flag == UNBOXED_PAGE_FLAG ||
4343 page_type_flag == CODE_PAGE_FLAG) {
4344 struct alloc_region *region =
4345 page_type_flag == CODE_PAGE_FLAG ? &code_region : &unboxed_region;
4346 #else
4347 } else if (UNBOXED_PAGE_FLAG == page_type_flag) {
4348 struct alloc_region *region = &unboxed_region;
4349 #endif
4350 lispobj * obj;
4351 int result;
4352 result = thread_mutex_lock(&allocation_lock);
4353 gc_assert(!result);
4354 obj = general_alloc_internal(nbytes, page_type_flag, region, thread);
4355 result = thread_mutex_unlock(&allocation_lock);
4356 gc_assert(!result);
4357 return obj;
4358 } else {
4359 lose("bad page type flag: %d", page_type_flag);
4363 lispobj AMD64_SYSV_ABI *
4364 alloc(sword_t nbytes)
4366 #ifdef LISP_FEATURE_SB_SAFEPOINT_STRICTLY
4367 struct thread *self = arch_os_get_current_thread();
4368 int was_pseudo_atomic = get_pseudo_atomic_atomic(self);
4369 if (!was_pseudo_atomic)
4370 set_pseudo_atomic_atomic(self);
4371 #else
4372 gc_assert(get_pseudo_atomic_atomic(arch_os_get_current_thread()));
4373 #endif
4375 lispobj *result = general_alloc(nbytes, BOXED_PAGE_FLAG);
4377 #ifdef LISP_FEATURE_SB_SAFEPOINT_STRICTLY
4378 if (!was_pseudo_atomic)
4379 clear_pseudo_atomic_atomic(self);
4380 #endif
4382 return result;
4386 * shared support for the OS-dependent signal handlers which
4387 * catch GENCGC-related write-protect violations
4389 void unhandled_sigmemoryfault(void* addr);
4391 /* Depending on which OS we're running under, different signals might
4392 * be raised for a violation of write protection in the heap. This
4393 * function factors out the common generational GC magic which needs
4394 * to invoked in this case, and should be called from whatever signal
4395 * handler is appropriate for the OS we're running under.
4397 * Return true if this signal is a normal generational GC thing that
4398 * we were able to handle, or false if it was abnormal and control
4399 * should fall through to the general SIGSEGV/SIGBUS/whatever logic.
4401 * We have two control flags for this: one causes us to ignore faults
4402 * on unprotected pages completely, and the second complains to stderr
4403 * but allows us to continue without losing.
4405 extern boolean ignore_memoryfaults_on_unprotected_pages;
4406 boolean ignore_memoryfaults_on_unprotected_pages = 0;
4408 extern boolean continue_after_memoryfault_on_unprotected_pages;
4409 boolean continue_after_memoryfault_on_unprotected_pages = 0;
4412 gencgc_handle_wp_violation(void* fault_addr)
4414 page_index_t page_index = find_page_index(fault_addr);
4416 #if QSHOW_SIGNALS
4417 FSHOW((stderr,
4418 "heap WP violation? fault_addr=%p, page_index=%"PAGE_INDEX_FMT"\n",
4419 fault_addr, page_index));
4420 #endif
4422 /* Check whether the fault is within the dynamic space. */
4423 if (page_index == (-1)) {
4424 #ifdef LISP_FEATURE_IMMOBILE_SPACE
4425 extern int immobile_space_handle_wp_violation(void*);
4426 if (immobile_space_handle_wp_violation(fault_addr))
4427 return 1;
4428 #endif
4430 /* It can be helpful to be able to put a breakpoint on this
4431 * case to help diagnose low-level problems. */
4432 unhandled_sigmemoryfault(fault_addr);
4434 /* not within the dynamic space -- not our responsibility */
4435 return 0;
4437 } else {
4438 int ret;
4439 ret = thread_mutex_lock(&free_pages_lock);
4440 gc_assert(ret == 0);
4441 if (page_table[page_index].write_protected) {
4442 /* Unprotect the page. */
4443 os_protect(page_address(page_index), GENCGC_CARD_BYTES, OS_VM_PROT_ALL);
4444 page_table[page_index].write_protected_cleared = 1;
4445 page_table[page_index].write_protected = 0;
4446 } else if (!ignore_memoryfaults_on_unprotected_pages) {
4447 /* The only acceptable reason for this signal on a heap
4448 * access is that GENCGC write-protected the page.
4449 * However, if two CPUs hit a wp page near-simultaneously,
4450 * we had better not have the second one lose here if it
4451 * does this test after the first one has already set wp=0
4453 if(page_table[page_index].write_protected_cleared != 1) {
4454 void lisp_backtrace(int frames);
4455 lisp_backtrace(10);
4456 fprintf(stderr,
4457 "Fault @ %p, page %"PAGE_INDEX_FMT" not marked as write-protected:\n"
4458 " boxed_region.first_page: %"PAGE_INDEX_FMT","
4459 " boxed_region.last_page %"PAGE_INDEX_FMT"\n"
4460 " page.scan_start_offset: %"OS_VM_SIZE_FMT"\n"
4461 " page.bytes_used: %u\n"
4462 " page.allocated: %d\n"
4463 " page.write_protected: %d\n"
4464 " page.write_protected_cleared: %d\n"
4465 " page.generation: %d\n",
4466 fault_addr,
4467 page_index,
4468 boxed_region.first_page,
4469 boxed_region.last_page,
4470 page_scan_start_offset(page_index),
4471 page_bytes_used(page_index),
4472 page_table[page_index].allocated,
4473 page_table[page_index].write_protected,
4474 page_table[page_index].write_protected_cleared,
4475 page_table[page_index].gen);
4476 if (!continue_after_memoryfault_on_unprotected_pages)
4477 lose("Feh.\n");
4480 ret = thread_mutex_unlock(&free_pages_lock);
4481 gc_assert(ret == 0);
4482 /* Don't worry, we can handle it. */
4483 return 1;
4486 /* This is to be called when we catch a SIGSEGV/SIGBUS, determine that
4487 * it's not just a case of the program hitting the write barrier, and
4488 * are about to let Lisp deal with it. It's basically just a
4489 * convenient place to set a gdb breakpoint. */
4490 void
4491 unhandled_sigmemoryfault(void *addr)
4494 static void
4495 update_thread_page_tables(struct thread *th)
4497 gc_alloc_update_page_tables(BOXED_PAGE_FLAG, &th->alloc_region);
4498 #if defined(LISP_FEATURE_SB_SAFEPOINT_STRICTLY) && !defined(LISP_FEATURE_WIN32)
4499 gc_alloc_update_page_tables(BOXED_PAGE_FLAG, &th->sprof_alloc_region);
4500 #endif
4503 /* GC is single-threaded and all memory allocations during a
4504 collection happen in the GC thread, so it is sufficient to update
4505 all the the page tables once at the beginning of a collection and
4506 update only page tables of the GC thread during the collection. */
4507 void gc_alloc_update_all_page_tables(int for_all_threads)
4509 /* Flush the alloc regions updating the tables. */
4510 struct thread *th;
4511 if (for_all_threads) {
4512 for_each_thread(th) {
4513 update_thread_page_tables(th);
4516 else {
4517 th = arch_os_get_current_thread();
4518 if (th) {
4519 update_thread_page_tables(th);
4522 #ifdef LISP_FEATURE_SEGREGATED_CODE
4523 gc_alloc_update_page_tables(CODE_PAGE_FLAG, &code_region);
4524 #endif
4525 gc_alloc_update_page_tables(UNBOXED_PAGE_FLAG, &unboxed_region);
4526 gc_alloc_update_page_tables(BOXED_PAGE_FLAG, &boxed_region);
4529 void
4530 gc_set_region_empty(struct alloc_region *region)
4532 region->first_page = 0;
4533 region->last_page = -1;
4534 region->start_addr = page_address(0);
4535 region->free_pointer = page_address(0);
4536 region->end_addr = page_address(0);
4539 static void
4540 zero_all_free_pages()
4542 page_index_t i;
4544 for (i = 0; i < last_free_page; i++) {
4545 if (page_free_p(i)) {
4546 #ifdef READ_PROTECT_FREE_PAGES
4547 os_protect(page_address(i),
4548 GENCGC_CARD_BYTES,
4549 OS_VM_PROT_ALL);
4550 #endif
4551 zero_pages(i, i);
4556 /* Things to do before doing a final GC before saving a core (without
4557 * purify).
4559 * + Pages in large_object pages aren't moved by the GC, so we need to
4560 * unset that flag from all pages.
4561 * + The pseudo-static generation isn't normally collected, but it seems
4562 * reasonable to collect it at least when saving a core. So move the
4563 * pages to a normal generation.
4565 static void
4566 prepare_for_final_gc ()
4568 page_index_t i;
4570 #ifdef LISP_FEATURE_IMMOBILE_SPACE
4571 extern void prepare_immobile_space_for_final_gc();
4572 prepare_immobile_space_for_final_gc ();
4573 #endif
4574 do_wipe_p = 0;
4575 for (i = 0; i < last_free_page; i++) {
4576 page_table[i].large_object = 0;
4577 if (page_table[i].gen == PSEUDO_STATIC_GENERATION) {
4578 int used = page_bytes_used(i);
4579 page_table[i].gen = HIGHEST_NORMAL_GENERATION;
4580 generations[PSEUDO_STATIC_GENERATION].bytes_allocated -= used;
4581 generations[HIGHEST_NORMAL_GENERATION].bytes_allocated += used;
4586 char gc_coalesce_string_literals = 0;
4588 /* Do a non-conservative GC, and then save a core with the initial
4589 * function being set to the value of the static symbol
4590 * SB!VM:RESTART-LISP-FUNCTION */
4591 void
4592 gc_and_save(char *filename, boolean prepend_runtime,
4593 boolean save_runtime_options, boolean compressed,
4594 int compression_level, int application_type)
4596 FILE *file;
4597 void *runtime_bytes = NULL;
4598 size_t runtime_size;
4600 file = prepare_to_save(filename, prepend_runtime, &runtime_bytes,
4601 &runtime_size);
4602 if (file == NULL)
4603 return;
4605 conservative_stack = 0;
4607 /* The filename might come from Lisp, and be moved by the now
4608 * non-conservative GC. */
4609 filename = strdup(filename);
4611 /* Collect twice: once into relatively high memory, and then back
4612 * into low memory. This compacts the retained data into the lower
4613 * pages, minimizing the size of the core file.
4615 prepare_for_final_gc();
4616 gencgc_alloc_start_page = last_free_page;
4617 collect_garbage(HIGHEST_NORMAL_GENERATION+1);
4619 if (gc_coalesce_string_literals) {
4620 extern struct lisp_startup_options lisp_startup_options;
4621 extern void coalesce_strings();
4622 boolean verbose = !lisp_startup_options.noinform;
4623 if (verbose) {
4624 printf("[coalescing similar strings... ");
4625 fflush(stdout);
4627 coalesce_strings();
4628 if (verbose)
4629 printf("done]\n");
4632 prepare_for_final_gc();
4633 gencgc_alloc_start_page = -1;
4634 collect_garbage(HIGHEST_NORMAL_GENERATION+1);
4636 if (prepend_runtime)
4637 save_runtime_to_filehandle(file, runtime_bytes, runtime_size,
4638 application_type);
4640 /* The dumper doesn't know that pages need to be zeroed before use. */
4641 zero_all_free_pages();
4642 save_to_filehandle(file, filename, SymbolValue(RESTART_LISP_FUNCTION,0),
4643 prepend_runtime, save_runtime_options,
4644 compressed ? compression_level : COMPRESSION_LEVEL_NONE);
4645 /* Oops. Save still managed to fail. Since we've mangled the stack
4646 * beyond hope, there's not much we can do.
4647 * (beyond FUNCALLing RESTART_LISP_FUNCTION, but I suspect that's
4648 * going to be rather unsatisfactory too... */
4649 lose("Attempt to save core after non-conservative GC failed.\n");