Speed up wipe_nonpinned_words()
[sbcl.git] / src / runtime / gencgc.c
blob5d277118ad5d75579d038c8295f4e9aecc3b39ce
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 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
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 hopscotch_init()
2130 # define hopscotch_reset(a)
2131 # define scavenge_pinned_ranges()
2132 # define wipe_nonpinned_words()
2133 # define hopscotch_create(a,b,c,d,e)
2134 # define hopscotch_log_stats(a,b)
2135 /* After scavenging of the roots is done, we go back to the pinned objects
2136 * and look within them for pointers. While heap_scavenge() could certainly
2137 * do this, it would potentially lead to extra work, since we can't know
2138 * whether any given object has been examined at least once, since there is
2139 * no telltale forwarding-pointer. The easiest thing to do is defer all
2140 * pinned objects to a subsequent pass, as is done here.
2142 #else
2143 static void
2144 scavenge_pinned_ranges()
2146 int i;
2147 lispobj key;
2148 for_each_hopscotch_key(i, key, pinned_objects) {
2149 lispobj* obj = native_pointer(key);
2150 lispobj header = *obj;
2151 // Never invoke scavenger on a simple-fun, just code components.
2152 if (is_cons_half(header))
2153 scavenge(obj, 2);
2154 else if (widetag_of(header) != SIMPLE_FUN_WIDETAG)
2155 scavtab[widetag_of(header)](obj, header);
2159 /* Create an array of fixnum to consume the space between 'from' and 'to' */
2160 static void deposit_filler(uword_t from, uword_t to)
2162 if (to > from) {
2163 lispobj* where = (lispobj*)from;
2164 sword_t nwords = (to - from) >> WORD_SHIFT;
2165 where[0] = SIMPLE_ARRAY_FIXNUM_WIDETAG;
2166 where[1] = make_fixnum(nwords - 2);
2170 /* Zero out the byte ranges on small object pages marked dont_move,
2171 * carefully skipping over objects in the pin hashtable.
2172 * TODO: by recording an additional bit per page indicating whether
2173 * there is more than one pinned object on it, we could avoid qsort()
2174 * except in the case where there is more than one. */
2175 static void
2176 wipe_nonpinned_words()
2178 void gc_heapsort_uwords(uword_t*, int);
2179 // Loop over the keys in pinned_objects and pack them densely into
2180 // the same array - pinned_objects.keys[] - but skip any simple-funs.
2181 // Admittedly this is abstraction breakage.
2182 int limit = hopscotch_max_key_index(pinned_objects);
2183 int n_pins = 0, i;
2184 for (i = 0; i <= limit; ++i) {
2185 lispobj key = pinned_objects.keys[i];
2186 if (key) {
2187 lispobj* obj = native_pointer(key);
2188 // No need to check for is_cons_half() - it will be false
2189 // on a simple-fun header, and that's the correct answer.
2190 if (widetag_of(*obj) != SIMPLE_FUN_WIDETAG)
2191 pinned_objects.keys[n_pins++] = (uword_t)obj;
2194 // Store a sentinel at the end. Even if n_pins = table capacity (unlikely),
2195 // it is safe to write one more word, because the hops[] array immediately
2196 // follows the keys[] array in memory. At worst, 2 elements of hops[]
2197 // are clobbered, which is irrelevant since the table has already been
2198 // rendered unusable by stealing its key array for a different purpose.
2199 pinned_objects.keys[n_pins] = 0;
2200 // Don't touch pinned_objects.count in case the reset function uses it
2201 // to decide how to resize for next use (which it doesn't, but could).
2202 gc_n_stack_pins = n_pins;
2203 // Order by ascending address, stopping short of the sentinel.
2204 gc_heapsort_uwords(pinned_objects.keys, n_pins);
2205 #if 0
2206 printf("Sorted pin list:\n");
2207 for (i = 0; i < n_pins; ++i) {
2208 lispobj* obj = (lispobj*)pinned_objects.keys[i];
2209 if (!is_cons_half(*obj))
2210 printf("%p: %5d words\n", obj, (int)sizetab[widetag_of(*obj)](obj));
2211 else printf("%p: CONS\n", obj);
2213 #endif
2214 // Each entry in the pinned objects demarcates two ranges to be cleared:
2215 // - the range preceding it back to either the page start, or prior object.
2216 // - the range after it, up to the lesser of page bytes used or next object.
2217 uword_t preceding_object = 0;
2218 uword_t this_page_end = 0;
2219 #define page_base_address(x) (x&~(GENCGC_CARD_BYTES-1))
2220 for (i = 0; i < n_pins; ++i) {
2221 // Handle the preceding range. If this object is on the same page as
2222 // its predecessor, then intervening bytes were already zeroed.
2223 // If not, then start a new page and do some bookkeeping.
2224 lispobj* obj = (lispobj*)pinned_objects.keys[i];
2225 uword_t this_page_base = page_base_address((uword_t)obj);
2226 /* printf("i=%d obj=%p base=%p\n", i, obj, (void*)this_page_base); */
2227 if (this_page_base > page_base_address(preceding_object)) {
2228 deposit_filler(this_page_base, (lispobj)obj);
2229 // Move the page to newspace
2230 page_index_t page = find_page_index(obj);
2231 int used = page_bytes_used(page);
2232 this_page_end = this_page_base + used;
2233 /* printf(" Clearing %p .. %p (limit=%p)\n",
2234 (void*)this_page_base, obj, (void*)this_page_end); */
2235 generations[new_space].bytes_allocated += used;
2236 generations[page_table[page].gen].bytes_allocated -= used;
2237 page_table[page].gen = new_space;
2238 page_table[page].has_pins = 0;
2240 // Handle the following range.
2241 lispobj word = *obj;
2242 size_t nwords = is_cons_half(word) ? 2 : sizetab[widetag_of(word)](obj);
2243 uword_t range_start = (uword_t)(obj + nwords);
2244 uword_t range_end = this_page_end;
2245 // There is always an i+1'th key due to the sentinel value.
2246 if (page_base_address(pinned_objects.keys[i+1]) == this_page_base)
2247 range_end = pinned_objects.keys[i+1];
2248 /* printf(" Clearing %p .. %p\n", (void*)range_start, (void*)range_end); */
2249 deposit_filler(range_start, range_end);
2250 preceding_object = (uword_t)obj;
2254 /* Add 'object' to the hashtable, and if the object is a code component,
2255 * then also add all of the embedded simple-funs.
2256 * The rationale for the extra work on code components is that without it,
2257 * every test of pinned_p() on an object would have to check if the pointer
2258 * is to a simple-fun - entailing an extra read of the header - and mapping
2259 * to its code component if so. Since more calls to pinned_p occur than to
2260 * pin_object, the extra burden should be on this function.
2261 * Experimentation bears out that this is the better technique.
2262 * Also, we wouldn't often expect code components in the collected generation
2263 * so the extra work here is quite minimal, even if it can generally add to
2264 * the number of keys in the hashtable.
2266 static void
2267 pin_object(lispobj object)
2269 if (!hopscotch_containsp(&pinned_objects, object)) {
2270 hopscotch_insert(&pinned_objects, object, 1);
2271 struct code* maybe_code = (struct code*)native_pointer(object);
2272 if (widetag_of(maybe_code->header) == CODE_HEADER_WIDETAG) {
2273 for_each_simple_fun(i, fun, maybe_code, 0, {
2274 hopscotch_insert(&pinned_objects,
2275 make_lispobj(fun, FUN_POINTER_LOWTAG),
2281 #endif
2283 /* Take a possible pointer to a Lisp object and mark its page in the
2284 * page_table so that it will not be relocated during a GC.
2286 * This involves locating the page it points to, then backing up to
2287 * the start of its region, then marking all pages dont_move from there
2288 * up to the first page that's not full or has a different generation
2290 * It is assumed that all the page static flags have been cleared at
2291 * the start of a GC.
2293 * It is also assumed that the current gc_alloc() region has been
2294 * flushed and the tables updated. */
2296 // TODO: there's probably a way to be a little more efficient here.
2297 // As things are, we start by finding the object that encloses 'addr',
2298 // then we see if 'addr' was a "valid" Lisp pointer to that object
2299 // - meaning we expect the correct lowtag on the pointer - except
2300 // that for code objects we don't require a correct lowtag
2301 // and we allow a pointer to anywhere in the object.
2303 // It should be possible to avoid calling search_dynamic_space
2304 // more of the time. First, check if the page pointed to might hold code.
2305 // If it does, then we continue regardless of the pointer's lowtag
2306 // (because of the special allowance). If the page definitely does *not*
2307 // hold code, then we require up front that the lowtake make sense,
2308 // by doing the same checks that are in properly_tagged_descriptor_p.
2310 // Problem: when code is allocated from a per-thread region,
2311 // does it ensure that the occupied pages are flagged as having code?
2313 static void
2314 preserve_pointer(void *addr)
2316 #ifdef LISP_FEATURE_IMMOBILE_SPACE
2317 /* Immobile space MUST be lower than dynamic space,
2318 or else this test needs to be revised */
2319 if (addr < (void*)IMMOBILE_SPACE_END) {
2320 extern void immobile_space_preserve_pointer(void*);
2321 immobile_space_preserve_pointer(addr);
2322 return;
2324 #endif
2325 page_index_t addr_page_index = find_page_index(addr);
2327 #ifdef GENCGC_IS_PRECISE
2328 /* If we're in precise gencgc (non-x86oid as of this writing) then
2329 * we are only called on valid object pointers in the first place,
2330 * so we just have to do a bounds-check against the heap, a
2331 * generation check, and the already-pinned check. */
2332 if (addr_page_index == -1
2333 || (page_table[addr_page_index].gen != from_space)
2334 || page_table[addr_page_index].dont_move)
2335 return;
2336 #else
2337 lispobj *object_start;
2338 if (addr_page_index == -1
2339 || (object_start = conservative_root_p(addr, addr_page_index)) == 0)
2340 return;
2341 #endif
2343 /* (Now that we know that addr_page_index is in range, it's
2344 * safe to index into page_table[] with it.) */
2345 unsigned int region_allocation = page_table[addr_page_index].allocated;
2347 /* Find the beginning of the region. Note that there may be
2348 * objects in the region preceding the one that we were passed a
2349 * pointer to: if this is the case, we will write-protect all the
2350 * previous objects' pages too. */
2352 #if 0
2353 /* I think this'd work just as well, but without the assertions.
2354 * -dan 2004.01.01 */
2355 page_index_t first_page = find_page_index(page_scan_start(addr_page_index))
2356 #else
2357 page_index_t first_page = addr_page_index;
2358 while (!page_starts_contiguous_block_p(first_page)) {
2359 --first_page;
2360 /* Do some checks. */
2361 gc_assert(page_bytes_used(first_page) == GENCGC_CARD_BYTES);
2362 gc_assert(page_table[first_page].gen == from_space);
2363 gc_assert(page_table[first_page].allocated == region_allocation);
2365 #endif
2367 /* Adjust any large objects before promotion as they won't be
2368 * copied after promotion. */
2369 if (page_table[first_page].large_object) {
2370 maybe_adjust_large_object(first_page);
2371 /* It may have moved to unboxed pages. */
2372 region_allocation = page_table[first_page].allocated;
2375 /* Now work forward until the end of this contiguous area is found,
2376 * marking all pages as dont_move. */
2377 page_index_t i;
2378 for (i = first_page; ;i++) {
2379 gc_assert(page_table[i].allocated == region_allocation);
2381 /* Mark the page static. */
2382 page_table[i].dont_move = 1;
2384 /* It is essential that the pages are not write protected as
2385 * they may have pointers into the old-space which need
2386 * scavenging. They shouldn't be write protected at this
2387 * stage. */
2388 gc_assert(!page_table[i].write_protected);
2390 /* Check whether this is the last page in this contiguous block.. */
2391 if (page_ends_contiguous_block_p(i, from_space))
2392 break;
2395 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
2396 /* Do not do this for multi-page objects. Those pages do not need
2397 * object wipeout anyway.
2399 if (do_wipe_p && i == first_page) { // single-page object
2400 lispobj word = *object_start;
2401 int lowtag = is_cons_half(word) ?
2402 LIST_POINTER_LOWTAG : lowtag_for_widetag[widetag_of(word)>>2];
2403 pin_object(make_lispobj(object_start, lowtag));
2404 page_table[i].has_pins = 1;
2406 #endif
2408 /* Check that the page is now static. */
2409 gc_assert(page_table[addr_page_index].dont_move != 0);
2413 #define IN_REGION_P(a,kind) (kind##_region.start_addr<=a && a<=kind##_region.free_pointer)
2414 #ifdef LISP_FEATURE_SEGREGATED_CODE
2415 #define IN_BOXED_REGION_P(a) IN_REGION_P(a,boxed)||IN_REGION_P(a,code)
2416 #else
2417 #define IN_BOXED_REGION_P(a) IN_REGION_P(a,boxed)
2418 #endif
2420 /* If the given page is not write-protected, then scan it for pointers
2421 * to younger generations or the top temp. generation, if no
2422 * suspicious pointers are found then the page is write-protected.
2424 * Care is taken to check for pointers to the current gc_alloc()
2425 * region if it is a younger generation or the temp. generation. This
2426 * frees the caller from doing a gc_alloc_update_page_tables(). Actually
2427 * the gc_alloc_generation does not need to be checked as this is only
2428 * called from scavenge_generation() when the gc_alloc generation is
2429 * younger, so it just checks if there is a pointer to the current
2430 * region.
2432 * We return 1 if the page was write-protected, else 0. */
2433 static int
2434 update_page_write_prot(page_index_t page)
2436 generation_index_t gen = page_table[page].gen;
2437 sword_t j;
2438 int wp_it = 1;
2439 void **page_addr = (void **)page_address(page);
2440 sword_t num_words = page_bytes_used(page) / N_WORD_BYTES;
2442 /* Shouldn't be a free page. */
2443 gc_assert(!page_free_p(page));
2444 gc_assert(page_bytes_used(page) != 0);
2446 /* Skip if it's already write-protected, pinned, or unboxed */
2447 if (page_table[page].write_protected
2448 /* FIXME: What's the reason for not write-protecting pinned pages? */
2449 || page_table[page].dont_move
2450 || page_unboxed_p(page))
2451 return (0);
2453 /* Scan the page for pointers to younger generations or the
2454 * top temp. generation. */
2456 /* This is conservative: any word satisfying is_lisp_pointer() is
2457 * assumed to be a pointer. To do otherwise would require a family
2458 * of scavenge-like functions. */
2459 for (j = 0; j < num_words; j++) {
2460 void *ptr = *(page_addr+j);
2461 page_index_t index;
2462 lispobj __attribute__((unused)) header;
2464 if (!is_lisp_pointer((lispobj)ptr))
2465 continue;
2466 /* Check that it's in the dynamic space */
2467 if ((index = find_page_index(ptr)) != -1) {
2468 if (/* Does it point to a younger or the temp. generation? */
2469 (!page_free_p(index)
2470 && (page_bytes_used(index) != 0)
2471 && ((page_table[index].gen < gen)
2472 || (page_table[index].gen == SCRATCH_GENERATION)))
2474 /* Or does it point within a current gc_alloc() region? */
2475 || (IN_BOXED_REGION_P(ptr) || IN_REGION_P(ptr,unboxed))) {
2476 wp_it = 0;
2477 break;
2480 #ifdef LISP_FEATURE_IMMOBILE_SPACE
2481 else if ((index = find_immobile_page_index(ptr)) >= 0 &&
2482 other_immediate_lowtag_p(header = *native_pointer((lispobj)ptr))) {
2483 // This is *possibly* a pointer to an object in immobile space,
2484 // given that above two conditions were satisfied.
2485 // But unlike in the dynamic space case, we need to read a byte
2486 // from the object to determine its generation, which requires care.
2487 // Consider an unboxed word that looks like a pointer to a word that
2488 // looks like fun-header-widetag. We can't naively back up to the
2489 // underlying code object since the alleged header might not be one.
2490 int obj_gen = gen; // Make comparison fail if we fall through
2491 if (lowtag_of((lispobj)ptr) != FUN_POINTER_LOWTAG) {
2492 obj_gen = __immobile_obj_generation(native_pointer((lispobj)ptr));
2493 } else if (widetag_of(header) == SIMPLE_FUN_WIDETAG) {
2494 lispobj* code = fun_code_header((lispobj)ptr - FUN_POINTER_LOWTAG);
2495 // This is a heuristic, since we're not actually looking for
2496 // an object boundary. Precise scanning of 'page' would obviate
2497 // the guard conditions here.
2498 if ((lispobj)code >= IMMOBILE_VARYOBJ_SUBSPACE_START
2499 && widetag_of(*code) == CODE_HEADER_WIDETAG)
2500 obj_gen = __immobile_obj_generation(code);
2502 // A bogus generation number implies a not-really-pointer,
2503 // but it won't cause misbehavior.
2504 if (obj_gen < gen || obj_gen == SCRATCH_GENERATION) {
2505 wp_it = 0;
2506 break;
2509 #endif
2512 if (wp_it == 1) {
2513 /* Write-protect the page. */
2514 /*FSHOW((stderr, "/write-protecting page %d gen %d\n", page, gen));*/
2516 os_protect((void *)page_addr,
2517 GENCGC_CARD_BYTES,
2518 OS_VM_PROT_READ|OS_VM_PROT_EXECUTE);
2520 /* Note the page as protected in the page tables. */
2521 page_table[page].write_protected = 1;
2524 return (wp_it);
2527 /* Is this page holding a normal (non-hashtable) large-object
2528 * simple-vector? */
2529 static inline boolean large_simple_vector_p(page_index_t page) {
2530 if (!page_table[page].large_object)
2531 return 0;
2532 lispobj object = *(lispobj *)page_address(page);
2533 return widetag_of(object) == SIMPLE_VECTOR_WIDETAG &&
2534 (HeaderValue(object) & 0xFF) == subtype_VectorNormal;
2538 /* Scavenge all generations from FROM to TO, inclusive, except for
2539 * new_space which needs special handling, as new objects may be
2540 * added which are not checked here - use scavenge_newspace generation.
2542 * Write-protected pages should not have any pointers to the
2543 * from_space so do need scavenging; thus write-protected pages are
2544 * not always scavenged. There is some code to check that these pages
2545 * are not written; but to check fully the write-protected pages need
2546 * to be scavenged by disabling the code to skip them.
2548 * Under the current scheme when a generation is GCed the younger
2549 * generations will be empty. So, when a generation is being GCed it
2550 * is only necessary to scavenge the older generations for pointers
2551 * not the younger. So a page that does not have pointers to younger
2552 * generations does not need to be scavenged.
2554 * The write-protection can be used to note pages that don't have
2555 * pointers to younger pages. But pages can be written without having
2556 * pointers to younger generations. After the pages are scavenged here
2557 * they can be scanned for pointers to younger generations and if
2558 * there are none the page can be write-protected.
2560 * One complication is when the newspace is the top temp. generation.
2562 * Enabling SC_GEN_CK scavenges the write-protected pages and checks
2563 * that none were written, which they shouldn't be as they should have
2564 * no pointers to younger generations. This breaks down for weak
2565 * pointers as the objects contain a link to the next and are written
2566 * if a weak pointer is scavenged. Still it's a useful check. */
2567 static void
2568 scavenge_generations(generation_index_t from, generation_index_t to)
2570 page_index_t i;
2571 page_index_t num_wp = 0;
2573 #define SC_GEN_CK 0
2574 #if SC_GEN_CK
2575 /* Clear the write_protected_cleared flags on all pages. */
2576 for (i = 0; i < page_table_pages; i++)
2577 page_table[i].write_protected_cleared = 0;
2578 #endif
2580 for (i = 0; i < last_free_page; i++) {
2581 generation_index_t generation = page_table[i].gen;
2582 if (page_boxed_p(i)
2583 && (page_bytes_used(i) != 0)
2584 && (generation != new_space)
2585 && (generation >= from)
2586 && (generation <= to)) {
2587 page_index_t last_page,j;
2588 int write_protected=1;
2590 /* This should be the start of a region */
2591 gc_assert(page_starts_contiguous_block_p(i));
2593 if (large_simple_vector_p(i)) {
2594 /* Scavenge only the unprotected pages of a
2595 * large-object vector, other large objects could be
2596 * handled as well, but vectors are easier to deal
2597 * with and are more likely to grow to very large
2598 * sizes where avoiding scavenging the whole thing is
2599 * worthwile */
2600 if (!page_table[i].write_protected) {
2601 scavenge((lispobj*)page_address(i) + 2,
2602 GENCGC_CARD_BYTES / N_WORD_BYTES - 2);
2603 update_page_write_prot(i);
2605 for (last_page = i + 1; ; last_page++) {
2606 lispobj* start = (lispobj*)page_address(last_page);
2607 write_protected = page_table[last_page].write_protected;
2608 if (page_ends_contiguous_block_p(last_page, generation)) {
2609 if (!write_protected) {
2610 scavenge(start, page_bytes_used(last_page) / N_WORD_BYTES);
2611 update_page_write_prot(last_page);
2613 break;
2615 if (!write_protected) {
2616 scavenge(start, GENCGC_CARD_BYTES / N_WORD_BYTES);
2617 update_page_write_prot(last_page);
2620 } else {
2621 /* Now work forward until the end of the region */
2622 for (last_page = i; ; last_page++) {
2623 write_protected =
2624 write_protected && page_table[last_page].write_protected;
2625 if (page_ends_contiguous_block_p(last_page, generation))
2626 break;
2628 if (!write_protected) {
2629 heap_scavenge((lispobj*)page_address(i),
2630 (lispobj*)(page_address(last_page)
2631 + page_bytes_used(last_page)));
2633 /* Now scan the pages and write protect those that
2634 * don't have pointers to younger generations. */
2635 if (enable_page_protection) {
2636 for (j = i; j <= last_page; j++) {
2637 num_wp += update_page_write_prot(j);
2640 if ((gencgc_verbose > 1) && (num_wp != 0)) {
2641 FSHOW((stderr,
2642 "/write protected %d pages within generation %d\n",
2643 num_wp, generation));
2647 i = last_page;
2651 #if SC_GEN_CK
2652 /* Check that none of the write_protected pages in this generation
2653 * have been written to. */
2654 for (i = 0; i < page_table_pages; i++) {
2655 if (!page_free_p(i)
2656 && (page_bytes_used(i) != 0)
2657 && (page_table[i].gen == generation)
2658 && (page_table[i].write_protected_cleared != 0)) {
2659 FSHOW((stderr, "/scavenge_generation() %d\n", generation));
2660 FSHOW((stderr,
2661 "/page bytes_used=%d scan_start_offset=%lu dont_move=%d\n",
2662 page_bytes_used(i),
2663 scan_start_offset(page_table[i]),
2664 page_table[i].dont_move));
2665 lose("write to protected page %d in scavenge_generation()\n", i);
2668 #endif
2672 /* Scavenge a newspace generation. As it is scavenged new objects may
2673 * be allocated to it; these will also need to be scavenged. This
2674 * repeats until there are no more objects unscavenged in the
2675 * newspace generation.
2677 * To help improve the efficiency, areas written are recorded by
2678 * gc_alloc() and only these scavenged. Sometimes a little more will be
2679 * scavenged, but this causes no harm. An easy check is done that the
2680 * scavenged bytes equals the number allocated in the previous
2681 * scavenge.
2683 * Write-protected pages are not scanned except if they are marked
2684 * dont_move in which case they may have been promoted and still have
2685 * pointers to the from space.
2687 * Write-protected pages could potentially be written by alloc however
2688 * to avoid having to handle re-scavenging of write-protected pages
2689 * gc_alloc() does not write to write-protected pages.
2691 * New areas of objects allocated are recorded alternatively in the two
2692 * new_areas arrays below. */
2693 static struct new_area new_areas_1[NUM_NEW_AREAS];
2694 static struct new_area new_areas_2[NUM_NEW_AREAS];
2696 #ifdef LISP_FEATURE_IMMOBILE_SPACE
2697 extern unsigned int immobile_scav_queue_count;
2698 extern void
2699 gc_init_immobile(),
2700 update_immobile_nursery_bits(),
2701 scavenge_immobile_roots(generation_index_t,generation_index_t),
2702 scavenge_immobile_newspace(),
2703 sweep_immobile_space(int raise),
2704 write_protect_immobile_space();
2705 #else
2706 #define immobile_scav_queue_count 0
2707 #endif
2709 /* Do one full scan of the new space generation. This is not enough to
2710 * complete the job as new objects may be added to the generation in
2711 * the process which are not scavenged. */
2712 static void
2713 scavenge_newspace_generation_one_scan(generation_index_t generation)
2715 page_index_t i;
2717 FSHOW((stderr,
2718 "/starting one full scan of newspace generation %d\n",
2719 generation));
2720 for (i = 0; i < last_free_page; i++) {
2721 /* Note that this skips over open regions when it encounters them. */
2722 if (page_boxed_p(i)
2723 && (page_bytes_used(i) != 0)
2724 && (page_table[i].gen == generation)
2725 && ((page_table[i].write_protected == 0)
2726 /* (This may be redundant as write_protected is now
2727 * cleared before promotion.) */
2728 || (page_table[i].dont_move == 1))) {
2729 page_index_t last_page;
2730 int all_wp=1;
2732 /* The scavenge will start at the scan_start_offset of
2733 * page i.
2735 * We need to find the full extent of this contiguous
2736 * block in case objects span pages.
2738 * Now work forward until the end of this contiguous area
2739 * is found. A small area is preferred as there is a
2740 * better chance of its pages being write-protected. */
2741 for (last_page = i; ;last_page++) {
2742 /* If all pages are write-protected and movable,
2743 * then no need to scavenge */
2744 all_wp=all_wp && page_table[last_page].write_protected &&
2745 !page_table[last_page].dont_move;
2747 /* Check whether this is the last page in this
2748 * contiguous block */
2749 if (page_ends_contiguous_block_p(last_page, generation))
2750 break;
2753 /* Do a limited check for write-protected pages. */
2754 if (!all_wp) {
2755 new_areas_ignore_page = last_page;
2756 heap_scavenge(page_scan_start(i),
2757 (lispobj*)(page_address(last_page)
2758 + page_bytes_used(last_page)));
2760 i = last_page;
2763 FSHOW((stderr,
2764 "/done with one full scan of newspace generation %d\n",
2765 generation));
2768 /* Do a complete scavenge of the newspace generation. */
2769 static void
2770 scavenge_newspace_generation(generation_index_t generation)
2772 size_t i;
2774 /* the new_areas array currently being written to by gc_alloc() */
2775 struct new_area (*current_new_areas)[] = &new_areas_1;
2776 size_t current_new_areas_index;
2778 /* the new_areas created by the previous scavenge cycle */
2779 struct new_area (*previous_new_areas)[] = NULL;
2780 size_t previous_new_areas_index;
2782 /* Flush the current regions updating the tables. */
2783 gc_alloc_update_all_page_tables(0);
2785 /* Turn on the recording of new areas by gc_alloc(). */
2786 new_areas = current_new_areas;
2787 new_areas_index = 0;
2789 /* Don't need to record new areas that get scavenged anyway during
2790 * scavenge_newspace_generation_one_scan. */
2791 record_new_objects = 1;
2793 /* Start with a full scavenge. */
2794 scavenge_newspace_generation_one_scan(generation);
2796 /* Record all new areas now. */
2797 record_new_objects = 2;
2799 /* Give a chance to weak hash tables to make other objects live.
2800 * FIXME: The algorithm implemented here for weak hash table gcing
2801 * is O(W^2+N) as Bruno Haible warns in
2802 * http://www.haible.de/bruno/papers/cs/weak/WeakDatastructures-writeup.html
2803 * see "Implementation 2". */
2804 scav_weak_hash_tables();
2806 /* Flush the current regions updating the tables. */
2807 gc_alloc_update_all_page_tables(0);
2809 /* Grab new_areas_index. */
2810 current_new_areas_index = new_areas_index;
2812 /*FSHOW((stderr,
2813 "The first scan is finished; current_new_areas_index=%d.\n",
2814 current_new_areas_index));*/
2816 while (current_new_areas_index > 0 || immobile_scav_queue_count) {
2817 /* Move the current to the previous new areas */
2818 previous_new_areas = current_new_areas;
2819 previous_new_areas_index = current_new_areas_index;
2821 /* Scavenge all the areas in previous new areas. Any new areas
2822 * allocated are saved in current_new_areas. */
2824 /* Allocate an array for current_new_areas; alternating between
2825 * new_areas_1 and 2 */
2826 if (previous_new_areas == &new_areas_1)
2827 current_new_areas = &new_areas_2;
2828 else
2829 current_new_areas = &new_areas_1;
2831 /* Set up for gc_alloc(). */
2832 new_areas = current_new_areas;
2833 new_areas_index = 0;
2835 #ifdef LISP_FEATURE_IMMOBILE_SPACE
2836 scavenge_immobile_newspace();
2837 #endif
2838 /* Check whether previous_new_areas had overflowed. */
2839 if (previous_new_areas_index >= NUM_NEW_AREAS) {
2841 /* New areas of objects allocated have been lost so need to do a
2842 * full scan to be sure! If this becomes a problem try
2843 * increasing NUM_NEW_AREAS. */
2844 if (gencgc_verbose) {
2845 SHOW("new_areas overflow, doing full scavenge");
2848 /* Don't need to record new areas that get scavenged
2849 * anyway during scavenge_newspace_generation_one_scan. */
2850 record_new_objects = 1;
2852 scavenge_newspace_generation_one_scan(generation);
2854 /* Record all new areas now. */
2855 record_new_objects = 2;
2857 scav_weak_hash_tables();
2859 /* Flush the current regions updating the tables. */
2860 gc_alloc_update_all_page_tables(0);
2862 } else {
2864 /* Work through previous_new_areas. */
2865 for (i = 0; i < previous_new_areas_index; i++) {
2866 page_index_t page = (*previous_new_areas)[i].page;
2867 size_t offset = (*previous_new_areas)[i].offset;
2868 size_t size = (*previous_new_areas)[i].size;
2869 gc_assert(size % N_WORD_BYTES == 0);
2870 lispobj *start = (lispobj*)(page_address(page) + offset);
2871 heap_scavenge(start, (lispobj*)((char*)start + size));
2874 scav_weak_hash_tables();
2876 /* Flush the current regions updating the tables. */
2877 gc_alloc_update_all_page_tables(0);
2880 current_new_areas_index = new_areas_index;
2882 /*FSHOW((stderr,
2883 "The re-scan has finished; current_new_areas_index=%d.\n",
2884 current_new_areas_index));*/
2887 /* Turn off recording of areas allocated by gc_alloc(). */
2888 record_new_objects = 0;
2890 #if SC_NS_GEN_CK
2892 page_index_t i;
2893 /* Check that none of the write_protected pages in this generation
2894 * have been written to. */
2895 for (i = 0; i < page_table_pages; i++) {
2896 if (!page_free_p(i)
2897 && (page_bytes_used(i) != 0)
2898 && (page_table[i].gen == generation)
2899 && (page_table[i].write_protected_cleared != 0)
2900 && (page_table[i].dont_move == 0)) {
2901 lose("write protected page %d written to in scavenge_newspace_generation\ngeneration=%d dont_move=%d\n",
2902 i, generation, page_table[i].dont_move);
2906 #endif
2909 /* Un-write-protect all the pages in from_space. This is done at the
2910 * start of a GC else there may be many page faults while scavenging
2911 * the newspace (I've seen drive the system time to 99%). These pages
2912 * would need to be unprotected anyway before unmapping in
2913 * free_oldspace; not sure what effect this has on paging.. */
2914 static void
2915 unprotect_oldspace(void)
2917 page_index_t i;
2918 char *region_addr = 0;
2919 char *page_addr = 0;
2920 uword_t region_bytes = 0;
2922 for (i = 0; i < last_free_page; i++) {
2923 if (!page_free_p(i)
2924 && (page_bytes_used(i) != 0)
2925 && (page_table[i].gen == from_space)) {
2927 /* Remove any write-protection. We should be able to rely
2928 * on the write-protect flag to avoid redundant calls. */
2929 if (page_table[i].write_protected) {
2930 page_table[i].write_protected = 0;
2931 page_addr = page_address(i);
2932 if (!region_addr) {
2933 /* First region. */
2934 region_addr = page_addr;
2935 region_bytes = GENCGC_CARD_BYTES;
2936 } else if (region_addr + region_bytes == page_addr) {
2937 /* Region continue. */
2938 region_bytes += GENCGC_CARD_BYTES;
2939 } else {
2940 /* Unprotect previous region. */
2941 os_protect(region_addr, region_bytes, OS_VM_PROT_ALL);
2942 /* First page in new region. */
2943 region_addr = page_addr;
2944 region_bytes = GENCGC_CARD_BYTES;
2949 if (region_addr) {
2950 /* Unprotect last region. */
2951 os_protect(region_addr, region_bytes, OS_VM_PROT_ALL);
2955 /* Work through all the pages and free any in from_space. This
2956 * assumes that all objects have been copied or promoted to an older
2957 * generation. Bytes_allocated and the generation bytes_allocated
2958 * counter are updated. The number of bytes freed is returned. */
2959 static uword_t
2960 free_oldspace(void)
2962 uword_t bytes_freed = 0;
2963 page_index_t first_page, last_page;
2965 first_page = 0;
2967 do {
2968 /* Find a first page for the next region of pages. */
2969 while ((first_page < last_free_page)
2970 && (page_free_p(first_page)
2971 || (page_bytes_used(first_page) == 0)
2972 || (page_table[first_page].gen != from_space)))
2973 first_page++;
2975 if (first_page >= last_free_page)
2976 break;
2978 /* Find the last page of this region. */
2979 last_page = first_page;
2981 do {
2982 /* Free the page. */
2983 bytes_freed += page_bytes_used(last_page);
2984 generations[page_table[last_page].gen].bytes_allocated -=
2985 page_bytes_used(last_page);
2986 page_table[last_page].allocated = FREE_PAGE_FLAG;
2987 set_page_bytes_used(last_page, 0);
2988 /* Should already be unprotected by unprotect_oldspace(). */
2989 gc_assert(!page_table[last_page].write_protected);
2990 last_page++;
2992 while ((last_page < last_free_page)
2993 && !page_free_p(last_page)
2994 && (page_bytes_used(last_page) != 0)
2995 && (page_table[last_page].gen == from_space));
2997 #ifdef READ_PROTECT_FREE_PAGES
2998 os_protect(page_address(first_page),
2999 npage_bytes(last_page-first_page),
3000 OS_VM_PROT_NONE);
3001 #endif
3002 first_page = last_page;
3003 } while (first_page < last_free_page);
3005 bytes_allocated -= bytes_freed;
3006 return bytes_freed;
3009 #if 0
3010 /* Print some information about a pointer at the given address. */
3011 static void
3012 print_ptr(lispobj *addr)
3014 /* If addr is in the dynamic space then out the page information. */
3015 page_index_t pi1 = find_page_index((void*)addr);
3017 if (pi1 != -1)
3018 fprintf(stderr," %p: page %d alloc %d gen %d bytes_used %d offset %lu dont_move %d\n",
3019 addr,
3020 pi1,
3021 page_table[pi1].allocated,
3022 page_table[pi1].gen,
3023 page_bytes_used(pi1),
3024 scan_start_offset(page_table[pi1]),
3025 page_table[pi1].dont_move);
3026 fprintf(stderr," %x %x %x %x (%x) %x %x %x %x\n",
3027 *(addr-4),
3028 *(addr-3),
3029 *(addr-2),
3030 *(addr-1),
3031 *(addr-0),
3032 *(addr+1),
3033 *(addr+2),
3034 *(addr+3),
3035 *(addr+4));
3037 #endif
3039 static int
3040 is_in_stack_space(lispobj ptr)
3042 /* For space verification: Pointers can be valid if they point
3043 * to a thread stack space. This would be faster if the thread
3044 * structures had page-table entries as if they were part of
3045 * the heap space. */
3046 struct thread *th;
3047 for_each_thread(th) {
3048 if ((th->control_stack_start <= (lispobj *)ptr) &&
3049 (th->control_stack_end >= (lispobj *)ptr)) {
3050 return 1;
3053 return 0;
3056 // NOTE: This function can produces false failure indications,
3057 // usually related to dynamic space pointing to the stack of a
3058 // dead thread, but there may be other reasons as well.
3059 static void
3060 verify_range(lispobj *start, size_t words)
3062 extern int valid_lisp_pointer_p(lispobj);
3063 int is_in_readonly_space =
3064 (READ_ONLY_SPACE_START <= (uword_t)start &&
3065 (uword_t)start < SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0));
3066 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3067 int is_in_immobile_space =
3068 (IMMOBILE_SPACE_START <= (uword_t)start &&
3069 (uword_t)start < SymbolValue(IMMOBILE_SPACE_FREE_POINTER,0));
3070 #endif
3072 lispobj *end = start + words;
3073 size_t count;
3074 for ( ; start < end ; start += count) {
3075 count = 1;
3076 lispobj thing = *start;
3077 lispobj __attribute__((unused)) pointee;
3079 if (is_lisp_pointer(thing)) {
3080 page_index_t page_index = find_page_index((void*)thing);
3081 sword_t to_readonly_space =
3082 (READ_ONLY_SPACE_START <= thing &&
3083 thing < SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0));
3084 sword_t to_static_space =
3085 (STATIC_SPACE_START <= thing &&
3086 thing < SymbolValue(STATIC_SPACE_FREE_POINTER,0));
3087 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3088 sword_t to_immobile_space =
3089 (IMMOBILE_SPACE_START <= thing &&
3090 thing < SymbolValue(IMMOBILE_FIXEDOBJ_FREE_POINTER,0)) ||
3091 (IMMOBILE_VARYOBJ_SUBSPACE_START <= thing &&
3092 thing < SymbolValue(IMMOBILE_SPACE_FREE_POINTER,0));
3093 #endif
3095 /* Does it point to the dynamic space? */
3096 if (page_index != -1) {
3097 /* If it's within the dynamic space it should point to a used page. */
3098 if (page_free_p(page_index))
3099 lose ("Ptr %p @ %p sees free page.\n", thing, start);
3100 if ((thing & (GENCGC_CARD_BYTES-1)) >= page_bytes_used(page_index))
3101 lose ("Ptr %p @ %p sees unallocated space.\n", thing, start);
3102 /* Check that it doesn't point to a forwarding pointer! */
3103 if (*native_pointer(thing) == 0x01) {
3104 lose("Ptr %p @ %p sees forwarding ptr.\n", thing, start);
3106 /* Check that its not in the RO space as it would then be a
3107 * pointer from the RO to the dynamic space. */
3108 if (is_in_readonly_space) {
3109 lose("ptr to dynamic space %p from RO space %x\n",
3110 thing, start);
3112 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3113 // verify all immobile space -> dynamic space pointers
3114 if (is_in_immobile_space && !valid_lisp_pointer_p(thing)) {
3115 lose("Ptr %p @ %p sees junk.\n", thing, start);
3117 #endif
3118 /* Does it point to a plausible object? This check slows
3119 * it down a lot (so it's commented out).
3121 * "a lot" is serious: it ate 50 minutes cpu time on
3122 * my duron 950 before I came back from lunch and
3123 * killed it.
3125 * FIXME: Add a variable to enable this
3126 * dynamically. */
3128 if (!valid_lisp_pointer_p((lispobj *)thing) {
3129 lose("ptr %p to invalid object %p\n", thing, start);
3132 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3133 } else if (to_immobile_space) {
3134 // the object pointed to must not have been discarded as garbage
3135 if (!other_immediate_lowtag_p(*native_pointer(thing))
3136 || immobile_filler_p(native_pointer(thing)))
3137 lose("Ptr %p @ %p sees trashed object.\n", (void*)thing, start);
3138 // verify all pointers to immobile space
3139 if (!valid_lisp_pointer_p(thing))
3140 lose("Ptr %p @ %p sees junk.\n", thing, start);
3141 #endif
3142 } else {
3143 extern char __attribute__((unused)) funcallable_instance_tramp;
3144 /* Verify that it points to another valid space. */
3145 if (!to_readonly_space && !to_static_space
3146 && !is_in_stack_space(thing)) {
3147 lose("Ptr %p @ %p sees junk.\n", thing, start);
3150 continue;
3152 int widetag = widetag_of(thing);
3153 if (is_lisp_immediate(thing) || widetag == NO_TLS_VALUE_MARKER_WIDETAG) {
3154 /* skip immediates */
3155 } else if (!(other_immediate_lowtag_p(widetag)
3156 && lowtag_for_widetag[widetag>>2])) {
3157 lose("Unhandled widetag %p at %p\n", widetag, start);
3158 } else if (unboxed_obj_widetag_p(widetag)) {
3159 count = sizetab[widetag](start);
3160 } else switch(widetag) {
3161 /* boxed or partially boxed objects */
3162 // FIXME: x86-64 can have partially unboxed FINs. The raw words
3163 // are at the moment valid fixnums by blind luck.
3164 case INSTANCE_WIDETAG:
3165 if (instance_layout(start)) {
3166 sword_t nslots = instance_length(thing) | 1;
3167 instance_scan(verify_range, start+1, nslots,
3168 ((struct layout*)
3169 native_pointer(instance_layout(start)))->bitmap);
3170 count = 1 + nslots;
3172 break;
3173 case CODE_HEADER_WIDETAG:
3175 struct code *code = (struct code *) start;
3176 sword_t nheader_words = code_header_words(code->header);
3177 /* Scavenge the boxed section of the code data block */
3178 verify_range(start + 1, nheader_words - 1);
3180 /* Scavenge the boxed section of each function
3181 * object in the code data block. */
3182 for_each_simple_fun(i, fheaderp, code, 1, {
3183 verify_range(SIMPLE_FUN_SCAV_START(fheaderp),
3184 SIMPLE_FUN_SCAV_NWORDS(fheaderp)); });
3185 count = nheader_words + code_instruction_words(code->code_size);
3186 break;
3188 #ifdef LISP_FEATURE_IMMOBILE_CODE
3189 case FDEFN_WIDETAG:
3190 verify_range(start + 1, 2);
3191 pointee = fdefn_raw_referent((struct fdefn*)start);
3192 verify_range(&pointee, 1);
3193 count = CEILING(sizeof (struct fdefn)/sizeof(lispobj), 2);
3194 break;
3195 #endif
3199 static uword_t verify_space(lispobj start, lispobj end) {
3200 verify_range((lispobj*)start, (end-start)>>WORD_SHIFT);
3201 return 0;
3204 static void verify_dynamic_space();
3206 static void
3207 verify_gc(void)
3209 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3210 # ifdef __linux__
3211 // Try this verification if marknsweep was compiled with extra debugging.
3212 // But weak symbols don't work on macOS.
3213 extern void __attribute__((weak)) check_varyobj_pages();
3214 if (&check_varyobj_pages) check_varyobj_pages();
3215 # endif
3216 verify_space(IMMOBILE_SPACE_START,
3217 SymbolValue(IMMOBILE_FIXEDOBJ_FREE_POINTER,0));
3218 verify_space(IMMOBILE_VARYOBJ_SUBSPACE_START,
3219 SymbolValue(IMMOBILE_SPACE_FREE_POINTER,0));
3220 #endif
3221 struct thread *th;
3222 for_each_thread(th) {
3223 verify_space((lispobj)th->binding_stack_start,
3224 (lispobj)get_binding_stack_pointer(th));
3226 verify_space(READ_ONLY_SPACE_START,
3227 SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0));
3228 verify_space(STATIC_SPACE_START,
3229 SymbolValue(STATIC_SPACE_FREE_POINTER,0));
3230 verify_dynamic_space();
3233 /* Call 'proc' with pairs of addresses demarcating ranges in the
3234 * specified generation.
3235 * Stop if any invocation returns non-zero, and return that value */
3236 uword_t
3237 walk_generation(uword_t (*proc)(lispobj*,lispobj*,uword_t),
3238 generation_index_t generation, uword_t extra)
3240 page_index_t i;
3241 int genmask = generation >= 0 ? 1 << generation : ~0;
3243 for (i = 0; i < last_free_page; i++) {
3244 if (!page_free_p(i)
3245 && (page_bytes_used(i) != 0)
3246 && ((1 << page_table[i].gen) & genmask)) {
3247 page_index_t last_page;
3249 /* This should be the start of a contiguous block */
3250 gc_assert(page_starts_contiguous_block_p(i));
3252 /* Need to find the full extent of this contiguous block in case
3253 objects span pages. */
3255 /* Now work forward until the end of this contiguous area is
3256 found. */
3257 for (last_page = i; ;last_page++)
3258 /* Check whether this is the last page in this contiguous
3259 * block. */
3260 if (page_ends_contiguous_block_p(last_page, page_table[i].gen))
3261 break;
3263 uword_t result =
3264 proc((lispobj*)page_address(i),
3265 (lispobj*)(page_bytes_used(last_page) + page_address(last_page)),
3266 extra);
3267 if (result) return result;
3269 i = last_page;
3272 return 0;
3274 static void verify_generation(generation_index_t generation)
3276 walk_generation((uword_t(*)(lispobj*,lispobj*,uword_t))verify_space,
3277 generation, 0);
3280 /* Check that all the free space is zero filled. */
3281 static void
3282 verify_zero_fill(void)
3284 page_index_t page;
3286 for (page = 0; page < last_free_page; page++) {
3287 if (page_free_p(page)) {
3288 /* The whole page should be zero filled. */
3289 sword_t *start_addr = (sword_t *)page_address(page);
3290 sword_t i;
3291 for (i = 0; i < (sword_t)GENCGC_CARD_BYTES/N_WORD_BYTES; i++) {
3292 if (start_addr[i] != 0) {
3293 lose("free page not zero at %x\n", start_addr + i);
3296 } else {
3297 sword_t free_bytes = GENCGC_CARD_BYTES - page_bytes_used(page);
3298 if (free_bytes > 0) {
3299 sword_t *start_addr =
3300 (sword_t *)(page_address(page) + page_bytes_used(page));
3301 sword_t size = free_bytes / N_WORD_BYTES;
3302 sword_t i;
3303 for (i = 0; i < size; i++) {
3304 if (start_addr[i] != 0) {
3305 lose("free region not zero at %x\n", start_addr + i);
3313 /* External entry point for verify_zero_fill */
3314 void
3315 gencgc_verify_zero_fill(void)
3317 /* Flush the alloc regions updating the tables. */
3318 gc_alloc_update_all_page_tables(1);
3319 SHOW("verifying zero fill");
3320 verify_zero_fill();
3323 static void
3324 verify_dynamic_space(void)
3326 verify_generation(-1);
3327 if (gencgc_enable_verify_zero_fill)
3328 verify_zero_fill();
3331 /* Write-protect all the dynamic boxed pages in the given generation. */
3332 static void
3333 write_protect_generation_pages(generation_index_t generation)
3335 page_index_t start;
3337 gc_assert(generation < SCRATCH_GENERATION);
3339 for (start = 0; start < last_free_page; start++) {
3340 if (protect_page_p(start, generation)) {
3341 void *page_start;
3342 page_index_t last;
3344 /* Note the page as protected in the page tables. */
3345 page_table[start].write_protected = 1;
3347 for (last = start + 1; last < last_free_page; last++) {
3348 if (!protect_page_p(last, generation))
3349 break;
3350 page_table[last].write_protected = 1;
3353 page_start = page_address(start);
3355 os_protect(page_start,
3356 npage_bytes(last - start),
3357 OS_VM_PROT_READ | OS_VM_PROT_EXECUTE);
3359 start = last;
3363 if (gencgc_verbose > 1) {
3364 FSHOW((stderr,
3365 "/write protected %d of %d pages in generation %d\n",
3366 count_write_protect_generation_pages(generation),
3367 count_generation_pages(generation),
3368 generation));
3372 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
3373 static void
3374 preserve_context_registers (void (*proc)(os_context_register_t), os_context_t *c)
3376 #ifdef LISP_FEATURE_SB_THREAD
3377 void **ptr;
3378 /* On Darwin the signal context isn't a contiguous block of memory,
3379 * so just preserve_pointering its contents won't be sufficient.
3381 #if defined(LISP_FEATURE_DARWIN)||defined(LISP_FEATURE_WIN32)
3382 #if defined LISP_FEATURE_X86
3383 proc(*os_context_register_addr(c,reg_EAX));
3384 proc(*os_context_register_addr(c,reg_ECX));
3385 proc(*os_context_register_addr(c,reg_EDX));
3386 proc(*os_context_register_addr(c,reg_EBX));
3387 proc(*os_context_register_addr(c,reg_ESI));
3388 proc(*os_context_register_addr(c,reg_EDI));
3389 proc(*os_context_pc_addr(c));
3390 #elif defined LISP_FEATURE_X86_64
3391 proc(*os_context_register_addr(c,reg_RAX));
3392 proc(*os_context_register_addr(c,reg_RCX));
3393 proc(*os_context_register_addr(c,reg_RDX));
3394 proc(*os_context_register_addr(c,reg_RBX));
3395 proc(*os_context_register_addr(c,reg_RSI));
3396 proc(*os_context_register_addr(c,reg_RDI));
3397 proc(*os_context_register_addr(c,reg_R8));
3398 proc(*os_context_register_addr(c,reg_R9));
3399 proc(*os_context_register_addr(c,reg_R10));
3400 proc(*os_context_register_addr(c,reg_R11));
3401 proc(*os_context_register_addr(c,reg_R12));
3402 proc(*os_context_register_addr(c,reg_R13));
3403 proc(*os_context_register_addr(c,reg_R14));
3404 proc(*os_context_register_addr(c,reg_R15));
3405 proc(*os_context_pc_addr(c));
3406 #else
3407 #error "preserve_context_registers needs to be tweaked for non-x86 Darwin"
3408 #endif
3409 #endif
3410 #if !defined(LISP_FEATURE_WIN32)
3411 for(ptr = ((void **)(c+1))-1; ptr>=(void **)c; ptr--) {
3412 proc((os_context_register_t)*ptr);
3414 #endif
3415 #endif // LISP_FEATURE_SB_THREAD
3417 #endif
3419 static void
3420 move_pinned_pages_to_newspace()
3422 page_index_t i;
3424 /* scavenge() will evacuate all oldspace pages, but no newspace
3425 * pages. Pinned pages are precisely those pages which must not
3426 * be evacuated, so move them to newspace directly. */
3428 for (i = 0; i < last_free_page; i++) {
3429 if (page_table[i].dont_move &&
3430 /* dont_move is cleared lazily, so validate the space as well. */
3431 page_table[i].gen == from_space) {
3432 if (do_wipe_p && page_table[i].has_pins) {
3433 // do not move to newspace after all, this will be word-wiped
3434 continue;
3436 page_table[i].gen = new_space;
3437 /* And since we're moving the pages wholesale, also adjust
3438 * the generation allocation counters. */
3439 int used = page_bytes_used(i);
3440 generations[new_space].bytes_allocated += used;
3441 generations[from_space].bytes_allocated -= used;
3446 /* Garbage collect a generation. If raise is 0 then the remains of the
3447 * generation are not raised to the next generation. */
3448 static void
3449 garbage_collect_generation(generation_index_t generation, int raise)
3451 page_index_t i;
3452 struct thread *th;
3454 gc_assert(generation <= HIGHEST_NORMAL_GENERATION);
3456 /* The oldest generation can't be raised. */
3457 gc_assert((generation != HIGHEST_NORMAL_GENERATION) || (raise == 0));
3459 /* Check if weak hash tables were processed in the previous GC. */
3460 gc_assert(weak_hash_tables == NULL);
3462 /* Initialize the weak pointer list. */
3463 weak_pointers = NULL;
3465 /* When a generation is not being raised it is transported to a
3466 * temporary generation (NUM_GENERATIONS), and lowered when
3467 * done. Set up this new generation. There should be no pages
3468 * allocated to it yet. */
3469 if (!raise) {
3470 gc_assert(generations[SCRATCH_GENERATION].bytes_allocated == 0);
3473 /* Set the global src and dest. generations */
3474 from_space = generation;
3475 if (raise)
3476 new_space = generation+1;
3477 else
3478 new_space = SCRATCH_GENERATION;
3480 /* Change to a new space for allocation, resetting the alloc_start_page */
3481 gc_alloc_generation = new_space;
3482 #ifdef LISP_FEATURE_SEGREGATED_CODE
3483 bzero(generations[new_space].alloc_start_page_,
3484 sizeof generations[new_space].alloc_start_page_);
3485 #else
3486 generations[new_space].alloc_start_page = 0;
3487 generations[new_space].alloc_unboxed_start_page = 0;
3488 generations[new_space].alloc_large_start_page = 0;
3489 #endif
3491 hopscotch_reset(&pinned_objects);
3492 /* Before any pointers are preserved, the dont_move flags on the
3493 * pages need to be cleared. */
3494 /* FIXME: consider moving this bitmap into its own range of words,
3495 * out of the page table. Then we can just bzero() it.
3496 * This will also obviate the extra test at the comment
3497 * "dont_move is cleared lazily" in move_pinned_pages_to_newspace().
3499 for (i = 0; i < last_free_page; i++)
3500 if(page_table[i].gen==from_space) {
3501 page_table[i].dont_move = 0;
3504 /* Un-write-protect the old-space pages. This is essential for the
3505 * promoted pages as they may contain pointers into the old-space
3506 * which need to be scavenged. It also helps avoid unnecessary page
3507 * faults as forwarding pointers are written into them. They need to
3508 * be un-protected anyway before unmapping later. */
3509 unprotect_oldspace();
3511 /* Scavenge the stacks' conservative roots. */
3513 /* there are potentially two stacks for each thread: the main
3514 * stack, which may contain Lisp pointers, and the alternate stack.
3515 * We don't ever run Lisp code on the altstack, but it may
3516 * host a sigcontext with lisp objects in it */
3518 /* what we need to do: (1) find the stack pointer for the main
3519 * stack; scavenge it (2) find the interrupt context on the
3520 * alternate stack that might contain lisp values, and scavenge
3521 * that */
3523 /* we assume that none of the preceding applies to the thread that
3524 * initiates GC. If you ever call GC from inside an altstack
3525 * handler, you will lose. */
3527 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
3528 /* And if we're saving a core, there's no point in being conservative. */
3529 if (conservative_stack) {
3530 for_each_thread(th) {
3531 void **ptr;
3532 void **esp=(void **)-1;
3533 if (th->state == STATE_DEAD)
3534 continue;
3535 # if defined(LISP_FEATURE_SB_SAFEPOINT)
3536 /* Conservative collect_garbage is always invoked with a
3537 * foreign C call or an interrupt handler on top of every
3538 * existing thread, so the stored SP in each thread
3539 * structure is valid, no matter which thread we are looking
3540 * at. For threads that were running Lisp code, the pitstop
3541 * and edge functions maintain this value within the
3542 * interrupt or exception handler. */
3543 esp = os_get_csp(th);
3544 assert_on_stack(th, esp);
3546 /* In addition to pointers on the stack, also preserve the
3547 * return PC, the only value from the context that we need
3548 * in addition to the SP. The return PC gets saved by the
3549 * foreign call wrapper, and removed from the control stack
3550 * into a register. */
3551 preserve_pointer(th->pc_around_foreign_call);
3553 /* And on platforms with interrupts: scavenge ctx registers. */
3555 /* Disabled on Windows, because it does not have an explicit
3556 * stack of `interrupt_contexts'. The reported CSP has been
3557 * chosen so that the current context on the stack is
3558 * covered by the stack scan. See also set_csp_from_context(). */
3559 # ifndef LISP_FEATURE_WIN32
3560 if (th != arch_os_get_current_thread()) {
3561 long k = fixnum_value(
3562 SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,th));
3563 while (k > 0)
3564 preserve_context_registers((void(*)(os_context_register_t))preserve_pointer,
3565 th->interrupt_contexts[--k]);
3567 # endif
3568 # elif defined(LISP_FEATURE_SB_THREAD)
3569 sword_t i,free;
3570 if(th==arch_os_get_current_thread()) {
3571 /* Somebody is going to burn in hell for this, but casting
3572 * it in two steps shuts gcc up about strict aliasing. */
3573 esp = (void **)((void *)&raise);
3574 } else {
3575 void **esp1;
3576 free=fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,th));
3577 for(i=free-1;i>=0;i--) {
3578 os_context_t *c=th->interrupt_contexts[i];
3579 esp1 = (void **) *os_context_register_addr(c,reg_SP);
3580 if (esp1>=(void **)th->control_stack_start &&
3581 esp1<(void **)th->control_stack_end) {
3582 if(esp1<esp) esp=esp1;
3583 preserve_context_registers((void(*)(os_context_register_t))preserve_pointer,
3588 # else
3589 esp = (void **)((void *)&raise);
3590 # endif
3591 if (!esp || esp == (void*) -1)
3592 lose("garbage_collect: no SP known for thread %x (OS %x)",
3593 th, th->os_thread);
3594 for (ptr = ((void **)th->control_stack_end)-1; ptr >= esp; ptr--) {
3595 preserve_pointer(*ptr);
3599 #else
3600 /* Non-x86oid systems don't have "conservative roots" as such, but
3601 * the same mechanism is used for objects pinned for use by alien
3602 * code. */
3603 for_each_thread(th) {
3604 lispobj pin_list = SymbolTlValue(PINNED_OBJECTS,th);
3605 while (pin_list != NIL) {
3606 struct cons *list_entry =
3607 (struct cons *)native_pointer(pin_list);
3608 preserve_pointer((void*)list_entry->car);
3609 pin_list = list_entry->cdr;
3612 #endif
3614 #if QSHOW
3615 if (gencgc_verbose > 1) {
3616 sword_t num_dont_move_pages = count_dont_move_pages();
3617 fprintf(stderr,
3618 "/non-movable pages due to conservative pointers = %ld (%lu bytes)\n",
3619 num_dont_move_pages,
3620 npage_bytes(num_dont_move_pages));
3622 #endif
3624 /* Now that all of the pinned (dont_move) pages are known, and
3625 * before we start to scavenge (and thus relocate) objects,
3626 * relocate the pinned pages to newspace, so that the scavenger
3627 * will not attempt to relocate their contents. */
3628 move_pinned_pages_to_newspace();
3630 /* Scavenge all the rest of the roots. */
3632 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
3634 * If not x86, we need to scavenge the interrupt context(s) and the
3635 * control stack.
3638 struct thread *th;
3639 for_each_thread(th) {
3640 scavenge_interrupt_contexts(th);
3641 scavenge_control_stack(th);
3644 # ifdef LISP_FEATURE_SB_SAFEPOINT
3645 /* In this case, scrub all stacks right here from the GCing thread
3646 * instead of doing what the comment below says. Suboptimal, but
3647 * easier. */
3648 for_each_thread(th)
3649 scrub_thread_control_stack(th);
3650 # else
3651 /* Scrub the unscavenged control stack space, so that we can't run
3652 * into any stale pointers in a later GC (this is done by the
3653 * stop-for-gc handler in the other threads). */
3654 scrub_control_stack();
3655 # endif
3657 #endif
3659 /* Scavenge the Lisp functions of the interrupt handlers, taking
3660 * care to avoid SIG_DFL and SIG_IGN. */
3661 for (i = 0; i < NSIG; i++) {
3662 union interrupt_handler handler = interrupt_handlers[i];
3663 if (!ARE_SAME_HANDLER(handler.c, SIG_IGN) &&
3664 !ARE_SAME_HANDLER(handler.c, SIG_DFL)) {
3665 scavenge((lispobj *)(interrupt_handlers + i), 1);
3668 /* Scavenge the binding stacks. */
3670 struct thread *th;
3671 for_each_thread(th) {
3672 sword_t len= (lispobj *)get_binding_stack_pointer(th) -
3673 th->binding_stack_start;
3674 scavenge((lispobj *) th->binding_stack_start,len);
3675 #ifdef LISP_FEATURE_SB_THREAD
3676 /* do the tls as well */
3677 len=(SymbolValue(FREE_TLS_INDEX,0) >> WORD_SHIFT) -
3678 (sizeof (struct thread))/(sizeof (lispobj));
3679 scavenge((lispobj *) (th+1),len);
3680 #endif
3684 /* Scavenge static space. */
3685 if (gencgc_verbose > 1) {
3686 FSHOW((stderr,
3687 "/scavenge static space: %d bytes\n",
3688 SymbolValue(STATIC_SPACE_FREE_POINTER,0) - STATIC_SPACE_START));
3690 heap_scavenge((lispobj*)STATIC_SPACE_START,
3691 (lispobj*)SymbolValue(STATIC_SPACE_FREE_POINTER,0));
3693 /* All generations but the generation being GCed need to be
3694 * scavenged. The new_space generation needs special handling as
3695 * objects may be moved in - it is handled separately below. */
3696 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3697 scavenge_immobile_roots(generation+1, SCRATCH_GENERATION);
3698 #endif
3699 scavenge_generations(generation+1, PSEUDO_STATIC_GENERATION);
3701 #ifdef LISP_FEATURE_SB_TRACEROOT
3702 scavenge(&gc_object_watcher, 1);
3703 #endif
3704 scavenge_pinned_ranges();
3706 /* Finally scavenge the new_space generation. Keep going until no
3707 * more objects are moved into the new generation */
3708 scavenge_newspace_generation(new_space);
3710 /* FIXME: I tried reenabling this check when debugging unrelated
3711 * GC weirdness ca. sbcl-0.6.12.45, and it failed immediately.
3712 * Since the current GC code seems to work well, I'm guessing that
3713 * this debugging code is just stale, but I haven't tried to
3714 * figure it out. It should be figured out and then either made to
3715 * work or just deleted. */
3717 #define RESCAN_CHECK 0
3718 #if RESCAN_CHECK
3719 /* As a check re-scavenge the newspace once; no new objects should
3720 * be found. */
3722 os_vm_size_t old_bytes_allocated = bytes_allocated;
3723 os_vm_size_t bytes_allocated;
3725 /* Start with a full scavenge. */
3726 scavenge_newspace_generation_one_scan(new_space);
3728 /* Flush the current regions, updating the tables. */
3729 gc_alloc_update_all_page_tables(1);
3731 bytes_allocated = bytes_allocated - old_bytes_allocated;
3733 if (bytes_allocated != 0) {
3734 lose("Rescan of new_space allocated %d more bytes.\n",
3735 bytes_allocated);
3738 #endif
3740 scan_weak_hash_tables();
3741 scan_weak_pointers();
3742 wipe_nonpinned_words();
3743 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3744 // Do this last, because until wipe_nonpinned_words() happens,
3745 // not all page table entries have the 'gen' value updated,
3746 // which we need to correctly find all old->young pointers.
3747 sweep_immobile_space(raise);
3748 #endif
3750 /* Flush the current regions, updating the tables. */
3751 gc_alloc_update_all_page_tables(0);
3752 hopscotch_log_stats(&pinned_objects, "pins");
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 hopscotch_create(&pinned_objects, HOPSCOTCH_HASH_FUN_DEFAULT, 0 /* hashset */,
4085 32 /* logical bin count */, 0 /* default range */);
4087 scavtab[WEAK_POINTER_WIDETAG] = scav_weak_pointer;
4088 transother[SIMPLE_ARRAY_WIDETAG] = trans_boxed_large;
4090 /* The page structures are initialized implicitly when page_table
4091 * is allocated with "calloc" above. Formerly we had the following
4092 * explicit initialization here (comments converted to C99 style
4093 * for readability as C's block comments don't nest):
4095 * // Initialize each page structure.
4096 * for (i = 0; i < page_table_pages; i++) {
4097 * // Initialize all pages as free.
4098 * page_table[i].allocated = FREE_PAGE_FLAG;
4099 * page_table[i].bytes_used = 0;
4101 * // Pages are not write-protected at startup.
4102 * page_table[i].write_protected = 0;
4105 * Without this loop the image starts up much faster when dynamic
4106 * space is large -- which it is on 64-bit platforms already by
4107 * default -- and when "calloc" for large arrays is implemented
4108 * using copy-on-write of a page of zeroes -- which it is at least
4109 * on Linux. In this case the pages that page_table_pages is stored
4110 * in are mapped and cleared not before the corresponding part of
4111 * dynamic space is used. For example, this saves clearing 16 MB of
4112 * memory at startup if the page size is 4 KB and the size of
4113 * dynamic space is 4 GB.
4114 * FREE_PAGE_FLAG must be 0 for this to work correctly which is
4115 * asserted below: */
4117 /* Compile time assertion: If triggered, declares an array
4118 * of dimension -1 forcing a syntax error. The intent of the
4119 * assignment is to avoid an "unused variable" warning. */
4120 char assert_free_page_flag_0[(FREE_PAGE_FLAG) ? -1 : 1];
4121 assert_free_page_flag_0[0] = assert_free_page_flag_0[0];
4124 bytes_allocated = 0;
4126 /* Initialize the generations. */
4127 for (i = 0; i < NUM_GENERATIONS; i++) {
4128 generations[i].alloc_start_page = 0;
4129 generations[i].alloc_unboxed_start_page = 0;
4130 generations[i].alloc_large_start_page = 0;
4131 generations[i].bytes_allocated = 0;
4132 generations[i].gc_trigger = 2000000;
4133 generations[i].num_gc = 0;
4134 generations[i].cum_sum_bytes_allocated = 0;
4135 /* the tune-able parameters */
4136 generations[i].bytes_consed_between_gc
4137 = bytes_consed_between_gcs/(os_vm_size_t)HIGHEST_NORMAL_GENERATION;
4138 generations[i].number_of_gcs_before_promotion = 1;
4139 generations[i].minimum_age_before_gc = 0.75;
4142 /* Initialize gc_alloc. */
4143 gc_alloc_generation = 0;
4144 gc_set_region_empty(&boxed_region);
4145 gc_set_region_empty(&unboxed_region);
4146 #ifdef LISP_FEATURE_SEGREGATED_CODE
4147 gc_set_region_empty(&code_region);
4148 #endif
4150 last_free_page = 0;
4153 /* Pick up the dynamic space from after a core load.
4155 * The ALLOCATION_POINTER points to the end of the dynamic space.
4158 static void
4159 gencgc_pickup_dynamic(void)
4161 page_index_t page = 0;
4162 char *alloc_ptr = (char *)get_alloc_pointer();
4163 lispobj *prev=(lispobj *)page_address(page);
4164 generation_index_t gen = PSEUDO_STATIC_GENERATION;
4166 bytes_allocated = 0;
4168 do {
4169 lispobj *first,*ptr= (lispobj *)page_address(page);
4171 if (!gencgc_partial_pickup || !page_free_p(page)) {
4172 /* It is possible, though rare, for the saved page table
4173 * to contain free pages below alloc_ptr. */
4174 page_table[page].gen = gen;
4175 set_page_bytes_used(page, GENCGC_CARD_BYTES);
4176 page_table[page].large_object = 0;
4177 page_table[page].write_protected = 0;
4178 page_table[page].write_protected_cleared = 0;
4179 page_table[page].dont_move = 0;
4180 set_page_need_to_zero(page, 1);
4182 bytes_allocated += GENCGC_CARD_BYTES;
4185 if (!gencgc_partial_pickup) {
4186 #ifdef LISP_FEATURE_SEGREGATED_CODE
4187 // Make the most general assumption: any page *might* contain code.
4188 page_table[page].allocated = CODE_PAGE_FLAG;
4189 #else
4190 page_table[page].allocated = BOXED_PAGE_FLAG;
4191 #endif
4192 first = gc_search_space3(ptr, prev, (ptr+2));
4193 if(ptr == first)
4194 prev=ptr;
4195 set_page_scan_start_offset(page, page_address(page) - (char*)prev);
4197 page++;
4198 } while (page_address(page) < alloc_ptr);
4200 last_free_page = page;
4202 generations[gen].bytes_allocated = bytes_allocated;
4204 gc_alloc_update_all_page_tables(1);
4205 write_protect_generation_pages(gen);
4208 void
4209 gc_initialize_pointers(void)
4211 gencgc_pickup_dynamic();
4215 /* alloc(..) is the external interface for memory allocation. It
4216 * allocates to generation 0. It is not called from within the garbage
4217 * collector as it is only external uses that need the check for heap
4218 * size (GC trigger) and to disable the interrupts (interrupts are
4219 * always disabled during a GC).
4221 * The vops that call alloc(..) assume that the returned space is zero-filled.
4222 * (E.g. the most significant word of a 2-word bignum in MOVE-FROM-UNSIGNED.)
4224 * The check for a GC trigger is only performed when the current
4225 * region is full, so in most cases it's not needed. */
4227 static inline lispobj *
4228 general_alloc_internal(sword_t nbytes, int page_type_flag, struct alloc_region *region,
4229 struct thread *thread)
4231 #ifndef LISP_FEATURE_WIN32
4232 lispobj alloc_signal;
4233 #endif
4234 void *new_obj;
4235 void *new_free_pointer;
4236 os_vm_size_t trigger_bytes = 0;
4238 gc_assert(nbytes > 0);
4240 /* Check for alignment allocation problems. */
4241 gc_assert((((uword_t)region->free_pointer & LOWTAG_MASK) == 0)
4242 && ((nbytes & LOWTAG_MASK) == 0));
4244 #if !(defined(LISP_FEATURE_WIN32) && defined(LISP_FEATURE_SB_THREAD))
4245 /* Must be inside a PA section. */
4246 gc_assert(get_pseudo_atomic_atomic(thread));
4247 #endif
4249 if ((os_vm_size_t) nbytes > large_allocation)
4250 large_allocation = nbytes;
4252 /* maybe we can do this quickly ... */
4253 new_free_pointer = (char*)region->free_pointer + nbytes;
4254 if (new_free_pointer <= region->end_addr) {
4255 new_obj = (void*)(region->free_pointer);
4256 region->free_pointer = new_free_pointer;
4257 return(new_obj); /* yup */
4260 /* We don't want to count nbytes against auto_gc_trigger unless we
4261 * have to: it speeds up the tenuring of objects and slows down
4262 * allocation. However, unless we do so when allocating _very_
4263 * large objects we are in danger of exhausting the heap without
4264 * running sufficient GCs.
4266 if ((os_vm_size_t) nbytes >= bytes_consed_between_gcs)
4267 trigger_bytes = nbytes;
4269 /* we have to go the long way around, it seems. Check whether we
4270 * should GC in the near future
4272 if (auto_gc_trigger && (bytes_allocated+trigger_bytes > auto_gc_trigger)) {
4273 /* Don't flood the system with interrupts if the need to gc is
4274 * already noted. This can happen for example when SUB-GC
4275 * allocates or after a gc triggered in a WITHOUT-GCING. */
4276 if (SymbolValue(GC_PENDING,thread) == NIL) {
4277 /* set things up so that GC happens when we finish the PA
4278 * section */
4279 SetSymbolValue(GC_PENDING,T,thread);
4280 if (SymbolValue(GC_INHIBIT,thread) == NIL) {
4281 #ifdef LISP_FEATURE_SB_SAFEPOINT
4282 thread_register_gc_trigger();
4283 #else
4284 set_pseudo_atomic_interrupted(thread);
4285 #ifdef GENCGC_IS_PRECISE
4286 /* PPC calls alloc() from a trap
4287 * look up the most context if it's from a trap. */
4289 os_context_t *context =
4290 thread->interrupt_data->allocation_trap_context;
4291 maybe_save_gc_mask_and_block_deferrables
4292 (context ? os_context_sigmask_addr(context) : NULL);
4294 #else
4295 maybe_save_gc_mask_and_block_deferrables(NULL);
4296 #endif
4297 #endif
4301 new_obj = gc_alloc_with_region(nbytes, page_type_flag, region, 0);
4303 #ifndef LISP_FEATURE_WIN32
4304 /* for sb-prof, and not supported on Windows yet */
4305 alloc_signal = SymbolValue(ALLOC_SIGNAL,thread);
4306 if ((alloc_signal & FIXNUM_TAG_MASK) == 0) {
4307 if ((sword_t) alloc_signal <= 0) {
4308 SetSymbolValue(ALLOC_SIGNAL, T, thread);
4309 raise(SIGPROF);
4310 } else {
4311 SetSymbolValue(ALLOC_SIGNAL,
4312 alloc_signal - (1 << N_FIXNUM_TAG_BITS),
4313 thread);
4316 #endif
4318 return (new_obj);
4321 lispobj *
4322 general_alloc(sword_t nbytes, int page_type_flag)
4324 struct thread *thread = arch_os_get_current_thread();
4325 /* Select correct region, and call general_alloc_internal with it.
4326 * For other then boxed allocation we must lock first, since the
4327 * region is shared. */
4328 #ifdef LISP_FEATURE_SEGREGATED_CODE
4329 if (page_type_flag == BOXED_PAGE_FLAG) {
4330 #else
4331 if (BOXED_PAGE_FLAG & page_type_flag) {
4332 #endif
4333 #ifdef LISP_FEATURE_SB_THREAD
4334 struct alloc_region *region = (thread ? &(thread->alloc_region) : &boxed_region);
4335 #else
4336 struct alloc_region *region = &boxed_region;
4337 #endif
4338 return general_alloc_internal(nbytes, page_type_flag, region, thread);
4339 #ifdef LISP_FEATURE_SEGREGATED_CODE
4340 } else if (page_type_flag == UNBOXED_PAGE_FLAG ||
4341 page_type_flag == CODE_PAGE_FLAG) {
4342 struct alloc_region *region =
4343 page_type_flag == CODE_PAGE_FLAG ? &code_region : &unboxed_region;
4344 #else
4345 } else if (UNBOXED_PAGE_FLAG == page_type_flag) {
4346 struct alloc_region *region = &unboxed_region;
4347 #endif
4348 lispobj * obj;
4349 int result;
4350 result = thread_mutex_lock(&allocation_lock);
4351 gc_assert(!result);
4352 obj = general_alloc_internal(nbytes, page_type_flag, region, thread);
4353 result = thread_mutex_unlock(&allocation_lock);
4354 gc_assert(!result);
4355 return obj;
4356 } else {
4357 lose("bad page type flag: %d", page_type_flag);
4361 lispobj AMD64_SYSV_ABI *
4362 alloc(sword_t nbytes)
4364 #ifdef LISP_FEATURE_SB_SAFEPOINT_STRICTLY
4365 struct thread *self = arch_os_get_current_thread();
4366 int was_pseudo_atomic = get_pseudo_atomic_atomic(self);
4367 if (!was_pseudo_atomic)
4368 set_pseudo_atomic_atomic(self);
4369 #else
4370 gc_assert(get_pseudo_atomic_atomic(arch_os_get_current_thread()));
4371 #endif
4373 lispobj *result = general_alloc(nbytes, BOXED_PAGE_FLAG);
4375 #ifdef LISP_FEATURE_SB_SAFEPOINT_STRICTLY
4376 if (!was_pseudo_atomic)
4377 clear_pseudo_atomic_atomic(self);
4378 #endif
4380 return result;
4384 * shared support for the OS-dependent signal handlers which
4385 * catch GENCGC-related write-protect violations
4387 void unhandled_sigmemoryfault(void* addr);
4389 /* Depending on which OS we're running under, different signals might
4390 * be raised for a violation of write protection in the heap. This
4391 * function factors out the common generational GC magic which needs
4392 * to invoked in this case, and should be called from whatever signal
4393 * handler is appropriate for the OS we're running under.
4395 * Return true if this signal is a normal generational GC thing that
4396 * we were able to handle, or false if it was abnormal and control
4397 * should fall through to the general SIGSEGV/SIGBUS/whatever logic.
4399 * We have two control flags for this: one causes us to ignore faults
4400 * on unprotected pages completely, and the second complains to stderr
4401 * but allows us to continue without losing.
4403 extern boolean ignore_memoryfaults_on_unprotected_pages;
4404 boolean ignore_memoryfaults_on_unprotected_pages = 0;
4406 extern boolean continue_after_memoryfault_on_unprotected_pages;
4407 boolean continue_after_memoryfault_on_unprotected_pages = 0;
4410 gencgc_handle_wp_violation(void* fault_addr)
4412 page_index_t page_index = find_page_index(fault_addr);
4414 #if QSHOW_SIGNALS
4415 FSHOW((stderr,
4416 "heap WP violation? fault_addr=%p, page_index=%"PAGE_INDEX_FMT"\n",
4417 fault_addr, page_index));
4418 #endif
4420 /* Check whether the fault is within the dynamic space. */
4421 if (page_index == (-1)) {
4422 #ifdef LISP_FEATURE_IMMOBILE_SPACE
4423 extern int immobile_space_handle_wp_violation(void*);
4424 if (immobile_space_handle_wp_violation(fault_addr))
4425 return 1;
4426 #endif
4428 /* It can be helpful to be able to put a breakpoint on this
4429 * case to help diagnose low-level problems. */
4430 unhandled_sigmemoryfault(fault_addr);
4432 /* not within the dynamic space -- not our responsibility */
4433 return 0;
4435 } else {
4436 int ret;
4437 ret = thread_mutex_lock(&free_pages_lock);
4438 gc_assert(ret == 0);
4439 if (page_table[page_index].write_protected) {
4440 /* Unprotect the page. */
4441 os_protect(page_address(page_index), GENCGC_CARD_BYTES, OS_VM_PROT_ALL);
4442 page_table[page_index].write_protected_cleared = 1;
4443 page_table[page_index].write_protected = 0;
4444 } else if (!ignore_memoryfaults_on_unprotected_pages) {
4445 /* The only acceptable reason for this signal on a heap
4446 * access is that GENCGC write-protected the page.
4447 * However, if two CPUs hit a wp page near-simultaneously,
4448 * we had better not have the second one lose here if it
4449 * does this test after the first one has already set wp=0
4451 if(page_table[page_index].write_protected_cleared != 1) {
4452 void lisp_backtrace(int frames);
4453 lisp_backtrace(10);
4454 fprintf(stderr,
4455 "Fault @ %p, page %"PAGE_INDEX_FMT" not marked as write-protected:\n"
4456 " boxed_region.first_page: %"PAGE_INDEX_FMT","
4457 " boxed_region.last_page %"PAGE_INDEX_FMT"\n"
4458 " page.scan_start_offset: %"OS_VM_SIZE_FMT"\n"
4459 " page.bytes_used: %u\n"
4460 " page.allocated: %d\n"
4461 " page.write_protected: %d\n"
4462 " page.write_protected_cleared: %d\n"
4463 " page.generation: %d\n",
4464 fault_addr,
4465 page_index,
4466 boxed_region.first_page,
4467 boxed_region.last_page,
4468 page_scan_start_offset(page_index),
4469 page_bytes_used(page_index),
4470 page_table[page_index].allocated,
4471 page_table[page_index].write_protected,
4472 page_table[page_index].write_protected_cleared,
4473 page_table[page_index].gen);
4474 if (!continue_after_memoryfault_on_unprotected_pages)
4475 lose("Feh.\n");
4478 ret = thread_mutex_unlock(&free_pages_lock);
4479 gc_assert(ret == 0);
4480 /* Don't worry, we can handle it. */
4481 return 1;
4484 /* This is to be called when we catch a SIGSEGV/SIGBUS, determine that
4485 * it's not just a case of the program hitting the write barrier, and
4486 * are about to let Lisp deal with it. It's basically just a
4487 * convenient place to set a gdb breakpoint. */
4488 void
4489 unhandled_sigmemoryfault(void *addr)
4492 static void
4493 update_thread_page_tables(struct thread *th)
4495 gc_alloc_update_page_tables(BOXED_PAGE_FLAG, &th->alloc_region);
4496 #if defined(LISP_FEATURE_SB_SAFEPOINT_STRICTLY) && !defined(LISP_FEATURE_WIN32)
4497 gc_alloc_update_page_tables(BOXED_PAGE_FLAG, &th->sprof_alloc_region);
4498 #endif
4501 /* GC is single-threaded and all memory allocations during a
4502 collection happen in the GC thread, so it is sufficient to update
4503 all the the page tables once at the beginning of a collection and
4504 update only page tables of the GC thread during the collection. */
4505 void gc_alloc_update_all_page_tables(int for_all_threads)
4507 /* Flush the alloc regions updating the tables. */
4508 struct thread *th;
4509 if (for_all_threads) {
4510 for_each_thread(th) {
4511 update_thread_page_tables(th);
4514 else {
4515 th = arch_os_get_current_thread();
4516 if (th) {
4517 update_thread_page_tables(th);
4520 #ifdef LISP_FEATURE_SEGREGATED_CODE
4521 gc_alloc_update_page_tables(CODE_PAGE_FLAG, &code_region);
4522 #endif
4523 gc_alloc_update_page_tables(UNBOXED_PAGE_FLAG, &unboxed_region);
4524 gc_alloc_update_page_tables(BOXED_PAGE_FLAG, &boxed_region);
4527 void
4528 gc_set_region_empty(struct alloc_region *region)
4530 region->first_page = 0;
4531 region->last_page = -1;
4532 region->start_addr = page_address(0);
4533 region->free_pointer = page_address(0);
4534 region->end_addr = page_address(0);
4537 static void
4538 zero_all_free_pages()
4540 page_index_t i;
4542 for (i = 0; i < last_free_page; i++) {
4543 if (page_free_p(i)) {
4544 #ifdef READ_PROTECT_FREE_PAGES
4545 os_protect(page_address(i),
4546 GENCGC_CARD_BYTES,
4547 OS_VM_PROT_ALL);
4548 #endif
4549 zero_pages(i, i);
4554 /* Things to do before doing a final GC before saving a core (without
4555 * purify).
4557 * + Pages in large_object pages aren't moved by the GC, so we need to
4558 * unset that flag from all pages.
4559 * + The pseudo-static generation isn't normally collected, but it seems
4560 * reasonable to collect it at least when saving a core. So move the
4561 * pages to a normal generation.
4563 static void
4564 prepare_for_final_gc ()
4566 page_index_t i;
4568 #ifdef LISP_FEATURE_IMMOBILE_SPACE
4569 extern void prepare_immobile_space_for_final_gc();
4570 prepare_immobile_space_for_final_gc ();
4571 #endif
4572 do_wipe_p = 0;
4573 for (i = 0; i < last_free_page; i++) {
4574 page_table[i].large_object = 0;
4575 if (page_table[i].gen == PSEUDO_STATIC_GENERATION) {
4576 int used = page_bytes_used(i);
4577 page_table[i].gen = HIGHEST_NORMAL_GENERATION;
4578 generations[PSEUDO_STATIC_GENERATION].bytes_allocated -= used;
4579 generations[HIGHEST_NORMAL_GENERATION].bytes_allocated += used;
4585 /* Do a non-conservative GC, and then save a core with the initial
4586 * function being set to the value of the static symbol
4587 * SB!VM:RESTART-LISP-FUNCTION */
4588 void
4589 gc_and_save(char *filename, boolean prepend_runtime,
4590 boolean save_runtime_options, boolean compressed,
4591 int compression_level, int application_type)
4593 FILE *file;
4594 void *runtime_bytes = NULL;
4595 size_t runtime_size;
4597 file = prepare_to_save(filename, prepend_runtime, &runtime_bytes,
4598 &runtime_size);
4599 if (file == NULL)
4600 return;
4602 conservative_stack = 0;
4604 /* The filename might come from Lisp, and be moved by the now
4605 * non-conservative GC. */
4606 filename = strdup(filename);
4608 /* Collect twice: once into relatively high memory, and then back
4609 * into low memory. This compacts the retained data into the lower
4610 * pages, minimizing the size of the core file.
4612 prepare_for_final_gc();
4613 gencgc_alloc_start_page = last_free_page;
4614 collect_garbage(HIGHEST_NORMAL_GENERATION+1);
4616 prepare_for_final_gc();
4617 gencgc_alloc_start_page = -1;
4618 collect_garbage(HIGHEST_NORMAL_GENERATION+1);
4620 if (prepend_runtime)
4621 save_runtime_to_filehandle(file, runtime_bytes, runtime_size,
4622 application_type);
4624 /* The dumper doesn't know that pages need to be zeroed before use. */
4625 zero_all_free_pages();
4626 save_to_filehandle(file, filename, SymbolValue(RESTART_LISP_FUNCTION,0),
4627 prepend_runtime, save_runtime_options,
4628 compressed ? compression_level : COMPRESSION_LEVEL_NONE);
4629 /* Oops. Save still managed to fail. Since we've mangled the stack
4630 * beyond hope, there's not much we can do.
4631 * (beyond FUNCALLing RESTART_LISP_FUNCTION, but I suspect that's
4632 * going to be rather unsatisfactory too... */
4633 lose("Attempt to save core after non-conservative GC failed.\n");