Don't use a mutex in page fault handler
[sbcl.git] / src / runtime / gencgc.c
blobe6cef4ea6691a0458765e32ac15e487f44229668
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" available at
20 * <https://www.cs.rice.edu/~javaplt/311/Readings/wilson92uniprocessor.pdf>
21 * or
22 * <ftp://ftp.cs.utexas.edu/pub/garbage/bigsurv.ps>.
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <errno.h>
28 #include <string.h>
29 #include <inttypes.h>
30 #include "sbcl.h"
31 #if defined(LISP_FEATURE_WIN32) && defined(LISP_FEATURE_SB_THREAD)
32 #include "pthreads_win32.h"
33 #else
34 #include <signal.h>
35 #endif
36 #include "runtime.h"
37 #include "os.h"
38 #include "interr.h"
39 #include "globals.h"
40 #include "interrupt.h"
41 #include "validate.h"
42 #include "lispregs.h"
43 #include "arch.h"
44 #include "gc.h"
45 #include "gc-internal.h"
46 #include "gc-private.h"
47 #include "gencgc-private.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 #include "genesis/cons.h"
63 #include "forwarding-ptr.h"
65 /* forward declarations */
66 page_index_t gc_find_freeish_pages(page_index_t *restart_page_ptr, sword_t nbytes,
67 int page_type_flag);
71 * GC parameters
74 /* As usually configured, generations 0-5 are normal collected generations,
75 6 is pseudo-static (the objects in which are never moved nor reclaimed),
76 and 7 is scratch space used when collecting a generation without promotion,
77 wherein it is moved to generation 7 and back again.
79 enum {
80 SCRATCH_GENERATION = PSEUDO_STATIC_GENERATION+1,
81 NUM_GENERATIONS
84 /* Largest allocation seen since last GC. */
85 os_vm_size_t large_allocation = 0;
89 * debugging
92 /* the verbosity level. All non-error messages are disabled at level 0;
93 * and only a few rare messages are printed at level 1. */
94 #if QSHOW == 2
95 boolean gencgc_verbose = 1;
96 #else
97 boolean gencgc_verbose = 0;
98 #endif
100 /* FIXME: At some point enable the various error-checking things below
101 * and see what they say. */
103 /* We hunt for pointers to old-space, when GCing generations >= verify_gen.
104 * Set verify_gens to HIGHEST_NORMAL_GENERATION + 2 to disable this kind of
105 * check. */
106 generation_index_t verify_gens = HIGHEST_NORMAL_GENERATION + 2;
108 /* Should we do a pre-scan verify of generation 0 before it's GCed? */
109 boolean pre_verify_gen_0 = 0;
111 /* Should we check that newly allocated regions are zero filled? */
112 boolean gencgc_zero_check = 0;
114 /* Should we check that the free space is zero filled? */
115 /* Don't use this - you'll get more mileage out of READ_PROTECT_FREE_PAGES,
116 * because we zero-fill lazily. This switch should probably be removed. */
117 boolean gencgc_enable_verify_zero_fill = 0;
119 /* When loading a core, don't do a full scan of the memory for the
120 * memory region boundaries. (Set to true by coreparse.c if the core
121 * contained a pagetable entry).
123 boolean gencgc_partial_pickup = 0;
125 /* If defined, free pages are read-protected to ensure that nothing
126 * accesses them.
129 /* #define READ_PROTECT_FREE_PAGES */
133 * GC structures and variables
136 /* the total bytes allocated. These are seen by Lisp DYNAMIC-USAGE. */
137 os_vm_size_t bytes_allocated = 0;
138 os_vm_size_t auto_gc_trigger = 0;
140 /* the source and destination generations. These are set before a GC starts
141 * scavenging. */
142 generation_index_t from_space;
143 generation_index_t new_space;
145 /* Set to 1 when in GC */
146 boolean gc_active_p = 0;
148 /* should the GC be conservative on stack. If false (only right before
149 * saving a core), don't scan the stack / mark pages dont_move. */
150 static boolean conservative_stack = 1;
152 /* An array of page structures is allocated on gc initialization.
153 * This helps to quickly map between an address and its page structure.
154 * page_table_pages is set from the size of the dynamic space. */
155 page_index_t page_table_pages;
156 struct page *page_table;
157 lispobj gc_object_watcher;
158 int gc_traceroot_criterion;
159 #ifdef PIN_GRANULARITY_LISPOBJ
160 int gc_n_stack_pins;
161 struct hopscotch_table pinned_objects;
162 #endif
164 /* This is always 0 except during gc_and_save() */
165 lispobj lisp_init_function;
167 /// Constants defined in gc-internal:
168 /// #define BOXED_PAGE_FLAG 1
169 /// #define UNBOXED_PAGE_FLAG 2
170 /// #define OPEN_REGION_PAGE_FLAG 4
172 /// Return true if 'allocated' bits are: {001, 010, 011}, false if 1zz or 000.
173 static inline boolean page_allocated_no_region_p(page_index_t page) {
174 return (page_table[page].allocated ^ OPEN_REGION_PAGE_FLAG) > OPEN_REGION_PAGE_FLAG;
177 static inline boolean page_free_p(page_index_t page) {
178 return (page_table[page].allocated == FREE_PAGE_FLAG);
181 static inline boolean page_boxed_p(page_index_t page) {
182 return (page_table[page].allocated & BOXED_PAGE_FLAG);
185 /// Return true if 'allocated' bits are: {001, 011}, false otherwise.
186 /// i.e. true of pages which could hold boxed or partially boxed objects.
187 static inline boolean page_boxed_no_region_p(page_index_t page) {
188 return (page_table[page].allocated & 5) == BOXED_PAGE_FLAG;
191 /// Return true if page MUST NOT hold boxed objects (including code).
192 static inline boolean page_unboxed_p(page_index_t page) {
193 /* Both flags set == boxed code page */
194 return (page_table[page].allocated & 3) == UNBOXED_PAGE_FLAG;
197 static inline boolean protect_page_p(page_index_t page, generation_index_t generation) {
198 return (page_boxed_no_region_p(page)
199 && (page_bytes_used(page) != 0)
200 && !page_table[page].dont_move
201 && (page_table[page].gen == generation));
204 /* Calculate the start address for the given page number. */
205 inline char *
206 page_address(page_index_t page_num)
208 return (void*)(DYNAMIC_SPACE_START + (page_num * GENCGC_CARD_BYTES));
211 /* Calculate the address where the allocation region associated with
212 * the page starts. */
213 static inline void *
214 page_scan_start(page_index_t page_index)
216 return page_address(page_index)-page_scan_start_offset(page_index);
219 /* True if the page starts a contiguous block. */
220 static inline boolean
221 page_starts_contiguous_block_p(page_index_t page_index)
223 // Don't use the preprocessor macro: 0 means 0.
224 return page_table[page_index].scan_start_offset_ == 0;
227 /* True if the page is the last page in a contiguous block. */
228 static inline boolean
229 page_ends_contiguous_block_p(page_index_t page_index, generation_index_t gen)
231 // There is *always* a next page in the page table.
232 boolean answer = page_bytes_used(page_index) < GENCGC_CARD_BYTES
233 || page_starts_contiguous_block_p(page_index+1);
234 #ifdef DEBUG
235 boolean safe_answer =
236 (/* page doesn't fill block */
237 (page_bytes_used(page_index) < GENCGC_CARD_BYTES)
238 /* page is last allocated page */
239 || ((page_index + 1) >= last_free_page)
240 /* next page contains no data */
241 || !page_bytes_used(page_index + 1)
242 /* next page is in different generation */
243 || (page_table[page_index + 1].gen != gen)
244 /* next page starts its own contiguous block */
245 || (page_starts_contiguous_block_p(page_index + 1)));
246 gc_assert(answer == safe_answer);
247 #endif
248 return answer;
251 /* We maintain the invariant that pages with FREE_PAGE_FLAG have
252 * scan_start of zero, to optimize page_ends_contiguous_block_p().
253 * Clear all other flags as well, since they don't mean anything,
254 * and a store is simpler than a bitwise operation */
255 static inline void reset_page_flags(page_index_t page) {
256 page_table[page].scan_start_offset_ = 0;
257 // Any C compiler worth its salt should merge these into one store
258 page_table[page].allocated = page_table[page].write_protected
259 = page_table[page].write_protected_cleared
260 = page_table[page].dont_move = page_table[page].has_pins
261 = page_table[page].large_object = 0;
264 /// External function for calling from Lisp.
265 page_index_t ext_find_page_index(void *addr) { return find_page_index(addr); }
267 static os_vm_size_t
268 npage_bytes(page_index_t npages)
270 gc_assert(npages>=0);
271 return ((os_vm_size_t)npages)*GENCGC_CARD_BYTES;
274 /* Check that X is a higher address than Y and return offset from Y to
275 * X in bytes. */
276 static inline os_vm_size_t
277 addr_diff(void *x, void *y)
279 gc_assert(x >= y);
280 return (uintptr_t)x - (uintptr_t)y;
283 /* a structure to hold the state of a generation
285 * CAUTION: If you modify this, make sure to touch up the alien
286 * definition in src/code/gc.lisp accordingly. ...or better yes,
287 * deal with the FIXME there...
289 struct generation {
291 #if SEGREGATED_CODE
292 // A distinct start page per nonzero value of 'page_type_flag'.
293 // The zeroth index is the large object start page.
294 page_index_t alloc_start_page_[4];
295 #define alloc_large_start_page alloc_start_page_[0]
296 #define alloc_start_page alloc_start_page_[BOXED_PAGE_FLAG]
297 #define alloc_unboxed_start_page alloc_start_page_[UNBOXED_PAGE_FLAG]
298 #else
299 /* the first page that gc_alloc_large (boxed) considers on its next
300 * call. (Although it always allocates after the boxed_region.) */
301 page_index_t alloc_large_start_page;
303 /* the first page that gc_alloc() checks on its next call */
304 page_index_t alloc_start_page;
306 /* the first page that gc_alloc_unboxed() checks on its next call */
307 page_index_t alloc_unboxed_start_page;
308 #endif
310 /* the bytes allocated to this generation */
311 os_vm_size_t bytes_allocated;
313 /* the number of bytes at which to trigger a GC */
314 os_vm_size_t gc_trigger;
316 /* to calculate a new level for gc_trigger */
317 os_vm_size_t bytes_consed_between_gc;
319 /* the number of GCs since the last raise */
320 int num_gc;
322 /* the number of GCs to run on the generations before raising objects to the
323 * next generation */
324 int number_of_gcs_before_promotion;
326 /* the cumulative sum of the bytes allocated to this generation. It is
327 * cleared after a GC on this generations, and update before new
328 * objects are added from a GC of a younger generation. Dividing by
329 * the bytes_allocated will give the average age of the memory in
330 * this generation since its last GC. */
331 os_vm_size_t cum_sum_bytes_allocated;
333 /* a minimum average memory age before a GC will occur helps
334 * prevent a GC when a large number of new live objects have been
335 * added, in which case a GC could be a waste of time */
336 double minimum_age_before_gc;
339 /* an array of generation structures. There needs to be one more
340 * generation structure than actual generations as the oldest
341 * generation is temporarily raised then lowered. */
342 struct generation generations[NUM_GENERATIONS];
344 /* the oldest generation that is will currently be GCed by default.
345 * Valid values are: 0, 1, ... HIGHEST_NORMAL_GENERATION
347 * The default of HIGHEST_NORMAL_GENERATION enables GC on all generations.
349 * Setting this to 0 effectively disables the generational nature of
350 * the GC. In some applications generational GC may not be useful
351 * because there are no long-lived objects.
353 * An intermediate value could be handy after moving long-lived data
354 * into an older generation so an unnecessary GC of this long-lived
355 * data can be avoided. */
356 generation_index_t gencgc_oldest_gen_to_gc = HIGHEST_NORMAL_GENERATION;
358 /* META: Is nobody aside from me bothered by this especially misleading
359 * use of the word "last"? It could mean either "ultimate" or "prior",
360 * but in fact means neither. It is the *FIRST* page that should be grabbed
361 * for more space, so it is min free page, or 1+ the max used page. */
362 /* The maximum free page in the heap is maintained and used to update
363 * ALLOCATION_POINTER which is used by the room function to limit its
364 * search of the heap. XX Gencgc obviously needs to be better
365 * integrated with the Lisp code. */
367 page_index_t last_free_page;
369 #ifdef LISP_FEATURE_SB_THREAD
370 /* This lock is to prevent multiple threads from simultaneously
371 * allocating new regions which overlap each other. Note that the
372 * majority of GC is single-threaded, but alloc() may be called from
373 * >1 thread at a time and must be thread-safe. This lock must be
374 * seized before all accesses to generations[] or to parts of
375 * page_table[] that other threads may want to see */
376 static pthread_mutex_t free_pages_lock = PTHREAD_MUTEX_INITIALIZER;
377 /* This lock is used to protect non-thread-local allocation. */
378 static pthread_mutex_t allocation_lock = PTHREAD_MUTEX_INITIALIZER;
379 #endif
381 extern os_vm_size_t gencgc_release_granularity;
382 os_vm_size_t gencgc_release_granularity = GENCGC_RELEASE_GRANULARITY;
384 extern os_vm_size_t gencgc_alloc_granularity;
385 os_vm_size_t gencgc_alloc_granularity = GENCGC_ALLOC_GRANULARITY;
389 * miscellaneous heap functions
392 /* Count the number of pages which are write-protected within the
393 * given generation. */
394 static page_index_t
395 count_write_protect_generation_pages(generation_index_t generation)
397 page_index_t i, count = 0;
399 for (i = 0; i < last_free_page; i++)
400 if (!page_free_p(i)
401 && (page_table[i].gen == generation)
402 && page_table[i].write_protected)
403 count++;
404 return count;
407 /* Count the number of pages within the given generation. */
408 static page_index_t
409 count_generation_pages(generation_index_t generation)
411 page_index_t i;
412 page_index_t count = 0;
414 for (i = 0; i < last_free_page; i++)
415 if (!page_free_p(i) && page_table[i].gen == generation)
416 count++;
417 return count;
420 #if QSHOW
421 static page_index_t
422 count_dont_move_pages(void)
424 page_index_t i;
425 page_index_t count = 0;
426 for (i = 0; i < last_free_page; i++) {
427 if (!page_free_p(i) && page_table[i].dont_move) {
428 ++count;
431 return count;
433 #endif /* QSHOW */
435 /* Work through the pages and add up the number of bytes used for the
436 * given generation. */
437 static __attribute__((unused)) os_vm_size_t
438 count_generation_bytes_allocated (generation_index_t gen)
440 page_index_t i;
441 os_vm_size_t result = 0;
442 for (i = 0; i < last_free_page; i++) {
443 if (!page_free_p(i) && page_table[i].gen == gen)
444 result += page_bytes_used(i);
446 return result;
449 /* Return the average age of the memory in a generation. */
450 extern double
451 generation_average_age(generation_index_t gen)
453 if (generations[gen].bytes_allocated == 0)
454 return 0.0;
456 return
457 ((double)generations[gen].cum_sum_bytes_allocated)
458 / ((double)generations[gen].bytes_allocated);
461 #ifdef LISP_FEATURE_X86
462 extern void fpu_save(void *);
463 extern void fpu_restore(void *);
464 #endif
466 #define PAGE_INDEX_FMT PRIdPTR
468 extern void
469 write_generation_stats(FILE *file)
471 generation_index_t i;
473 #ifdef LISP_FEATURE_X86
474 int fpu_state[27];
476 /* Can end up here after calling alloc_tramp which doesn't prepare
477 * the x87 state, and the C ABI uses a different mode */
478 fpu_save(fpu_state);
479 #endif
481 /* Print the heap stats. */
482 fprintf(file,
483 " Gen StaPg UbSta LaSta Boxed Unbox LB LUB !move Alloc Waste Trig WP GCs Mem-age\n");
485 for (i = 0; i <= SCRATCH_GENERATION; i++) {
486 page_index_t j;
487 page_index_t boxed_cnt = 0;
488 page_index_t unboxed_cnt = 0;
489 page_index_t large_boxed_cnt = 0;
490 page_index_t large_unboxed_cnt = 0;
491 page_index_t pinned_cnt=0;
493 for (j = 0; j < last_free_page; j++)
494 if (page_table[j].gen == i) {
496 /* Count the number of boxed pages within the given
497 * generation. */
498 if (page_boxed_p(j)) {
499 if (page_table[j].large_object)
500 large_boxed_cnt++;
501 else
502 boxed_cnt++;
504 if(page_table[j].dont_move) pinned_cnt++;
505 /* Count the number of unboxed pages within the given
506 * generation. */
507 if (page_unboxed_p(j)) {
508 if (page_table[j].large_object)
509 large_unboxed_cnt++;
510 else
511 unboxed_cnt++;
515 gc_assert(generations[i].bytes_allocated
516 == count_generation_bytes_allocated(i));
517 fprintf(file,
518 " %1d: %5ld %5ld %5ld",
520 (long)generations[i].alloc_start_page,
521 (long)generations[i].alloc_unboxed_start_page,
522 (long)generations[i].alloc_large_start_page);
523 fprintf(file,
524 " %5"PAGE_INDEX_FMT" %5"PAGE_INDEX_FMT" %5"PAGE_INDEX_FMT
525 " %5"PAGE_INDEX_FMT" %5"PAGE_INDEX_FMT,
526 boxed_cnt, unboxed_cnt, large_boxed_cnt,
527 large_unboxed_cnt, pinned_cnt);
528 fprintf(file,
529 " %8"OS_VM_SIZE_FMT
530 " %6"OS_VM_SIZE_FMT
531 " %8"OS_VM_SIZE_FMT
532 " %4"PAGE_INDEX_FMT" %3d %7.4f\n",
533 generations[i].bytes_allocated,
534 (npage_bytes(count_generation_pages(i)) - generations[i].bytes_allocated),
535 generations[i].gc_trigger,
536 count_write_protect_generation_pages(i),
537 generations[i].num_gc,
538 generation_average_age(i));
540 fprintf(file," Total bytes allocated = %"OS_VM_SIZE_FMT"\n", bytes_allocated);
541 fprintf(file," Dynamic-space-size bytes = %"OS_VM_SIZE_FMT"\n", dynamic_space_size);
543 #ifdef LISP_FEATURE_X86
544 fpu_restore(fpu_state);
545 #endif
548 extern void
549 write_heap_exhaustion_report(FILE *file, long available, long requested,
550 struct thread *thread)
552 fprintf(file,
553 "Heap exhausted during %s: %ld bytes available, %ld requested.\n",
554 gc_active_p ? "garbage collection" : "allocation",
555 available,
556 requested);
557 write_generation_stats(file);
558 fprintf(file, "GC control variables:\n");
559 fprintf(file, " *GC-INHIBIT* = %s\n *GC-PENDING* = %s\n",
560 read_TLS(GC_INHIBIT,thread)==NIL ? "false" : "true",
561 (read_TLS(GC_PENDING, thread) == T) ?
562 "true" : ((read_TLS(GC_PENDING, thread) == NIL) ?
563 "false" : "in progress"));
564 #ifdef LISP_FEATURE_SB_THREAD
565 fprintf(file, " *STOP-FOR-GC-PENDING* = %s\n",
566 read_TLS(STOP_FOR_GC_PENDING,thread)==NIL ? "false" : "true");
567 #endif
570 extern void
571 print_generation_stats(void)
573 write_generation_stats(stderr);
576 extern char* gc_logfile;
577 char * gc_logfile = NULL;
579 extern void
580 log_generation_stats(char *logfile, char *header)
582 if (logfile) {
583 FILE * log = fopen(logfile, "a");
584 if (log) {
585 fprintf(log, "%s\n", header);
586 write_generation_stats(log);
587 fclose(log);
588 } else {
589 fprintf(stderr, "Could not open gc logfile: %s\n", logfile);
590 fflush(stderr);
595 extern void
596 report_heap_exhaustion(long available, long requested, struct thread *th)
598 if (gc_logfile) {
599 FILE * log = fopen(gc_logfile, "a");
600 if (log) {
601 write_heap_exhaustion_report(log, available, requested, th);
602 fclose(log);
603 } else {
604 fprintf(stderr, "Could not open gc logfile: %s\n", gc_logfile);
605 fflush(stderr);
608 /* Always to stderr as well. */
609 write_heap_exhaustion_report(stderr, available, requested, th);
613 #if defined(LISP_FEATURE_X86)
614 void fast_bzero(void*, size_t); /* in <arch>-assem.S */
615 #endif
617 /* Zero the pages from START to END (inclusive), but use mmap/munmap instead
618 * if zeroing it ourselves, i.e. in practice give the memory back to the
619 * OS. Generally done after a large GC.
621 void zero_pages_with_mmap(page_index_t start, page_index_t end) {
622 page_index_t i;
623 void *addr = page_address(start), *new_addr;
624 os_vm_size_t length = npage_bytes(1+end-start);
626 if (start > end)
627 return;
629 gc_assert(length >= gencgc_release_granularity);
630 gc_assert((length % gencgc_release_granularity) == 0);
632 #ifdef LISP_FEATURE_LINUX
633 // We use MADV_DONTNEED only on Linux due to differing semantics from BSD.
634 // Linux treats it as a demand that the memory be 0-filled, or refreshed
635 // from a file that backs the range. BSD takes it as a hint that you don't
636 // care if the memory has to brought in from swap when next accessed,
637 // i.e. it's not a request to make a user-visible alteration to memory.
638 // So in theory this can bring a page in from the core file, if we happen
639 // to hit a page that resides in the portion of memory mapped by coreparse.
640 // In practice this should not happen because objects from a core file can't
641 // become garbage. Except in save-lisp-and-die they can, and we must be
642 // cautious not to resurrect bytes that originally came from the file.
643 if ((os_vm_address_t)addr >= anon_dynamic_space_start) {
644 if (madvise(addr, length, MADV_DONTNEED) != 0)
645 lose("madvise failed\n");
646 } else
647 #endif
649 os_invalidate(addr, length);
650 new_addr = os_validate(NOT_MOVABLE, addr, length);
651 if (new_addr == NULL || new_addr != addr) {
652 lose("remap_free_pages: page moved, 0x%08x ==> 0x%08x",
653 start, new_addr);
657 for (i = start; i <= end; i++)
658 set_page_need_to_zero(i, 0);
661 /* Zero the pages from START to END (inclusive). Generally done just after
662 * a new region has been allocated.
664 static void
665 zero_pages(page_index_t start, page_index_t end) {
666 if (start > end)
667 return;
669 #if defined(LISP_FEATURE_X86)
670 fast_bzero(page_address(start), npage_bytes(1+end-start));
671 #else
672 bzero(page_address(start), npage_bytes(1+end-start));
673 #endif
677 static void
678 zero_and_mark_pages(page_index_t start, page_index_t end) {
679 page_index_t i;
681 zero_pages(start, end);
682 for (i = start; i <= end; i++)
683 set_page_need_to_zero(i, 0);
686 /* Zero the pages from START to END (inclusive), except for those
687 * pages that are known to already zeroed. Mark all pages in the
688 * ranges as non-zeroed.
690 static void
691 zero_dirty_pages(page_index_t start, page_index_t end) {
692 page_index_t i, j;
694 #ifdef READ_PROTECT_FREE_PAGES
695 os_protect(page_address(start), npage_bytes(1+end-start), OS_VM_PROT_ALL);
696 #endif
697 for (i = start; i <= end; i++) {
698 if (!page_need_to_zero(i)) continue;
699 for (j = i+1; (j <= end) && page_need_to_zero(j) ; j++)
700 ; /* empty body */
701 zero_pages(i, j-1);
702 i = j;
705 for (i = start; i <= end; i++) {
706 set_page_need_to_zero(i, 1);
712 * To support quick and inline allocation, regions of memory can be
713 * allocated and then allocated from with just a free pointer and a
714 * check against an end address.
716 * Since objects can be allocated to spaces with different properties
717 * e.g. boxed/unboxed, generation, ages; there may need to be many
718 * allocation regions.
720 * Each allocation region may start within a partly used page. Many
721 * features of memory use are noted on a page wise basis, e.g. the
722 * generation; so if a region starts within an existing allocated page
723 * it must be consistent with this page.
725 * During the scavenging of the newspace, objects will be transported
726 * into an allocation region, and pointers updated to point to this
727 * allocation region. It is possible that these pointers will be
728 * scavenged again before the allocation region is closed, e.g. due to
729 * trans_list which jumps all over the place to cleanup the list. It
730 * is important to be able to determine properties of all objects
731 * pointed to when scavenging, e.g to detect pointers to the oldspace.
732 * Thus it's important that the allocation regions have the correct
733 * properties set when allocated, and not just set when closed. The
734 * region allocation routines return regions with the specified
735 * properties, and grab all the pages, setting their properties
736 * appropriately, except that the amount used is not known.
738 * These regions are used to support quicker allocation using just a
739 * free pointer. The actual space used by the region is not reflected
740 * in the pages tables until it is closed. It can't be scavenged until
741 * closed.
743 * When finished with the region it should be closed, which will
744 * update the page tables for the actual space used returning unused
745 * space. Further it may be noted in the new regions which is
746 * necessary when scavenging the newspace.
748 * Large objects may be allocated directly without an allocation
749 * region, the page tables are updated immediately.
751 * Unboxed objects don't contain pointers to other objects and so
752 * don't need scavenging. Further they can't contain pointers to
753 * younger generations so WP is not needed. By allocating pages to
754 * unboxed objects the whole page never needs scavenging or
755 * write-protecting. */
757 /* We use either two or three regions for the current newspace generation. */
758 #if SEGREGATED_CODE
759 struct alloc_region gc_alloc_region[3];
760 #define boxed_region gc_alloc_region[BOXED_PAGE_FLAG-1]
761 #define unboxed_region gc_alloc_region[UNBOXED_PAGE_FLAG-1]
762 #define code_region gc_alloc_region[CODE_PAGE_FLAG-1]
763 #else
764 struct alloc_region boxed_region;
765 struct alloc_region unboxed_region;
766 #endif
768 /* The generation currently being allocated to. */
769 static generation_index_t gc_alloc_generation;
771 static inline page_index_t
772 generation_alloc_start_page(generation_index_t generation, int page_type_flag, int large)
774 if (!(page_type_flag >= 1 && page_type_flag <= 3))
775 lose("bad page_type_flag: %d", page_type_flag);
776 if (large)
777 return generations[generation].alloc_large_start_page;
778 #if SEGREGATED_CODE
779 return generations[generation].alloc_start_page_[page_type_flag];
780 #else
781 if (UNBOXED_PAGE_FLAG == page_type_flag)
782 return generations[generation].alloc_unboxed_start_page;
783 /* Both code and data. */
784 return generations[generation].alloc_start_page;
785 #endif
788 static inline void
789 set_generation_alloc_start_page(generation_index_t generation, int page_type_flag, int large,
790 page_index_t page)
792 if (!(page_type_flag >= 1 && page_type_flag <= 3))
793 lose("bad page_type_flag: %d", page_type_flag);
794 if (large)
795 generations[generation].alloc_large_start_page = page;
796 #if SEGREGATED_CODE
797 else
798 generations[generation].alloc_start_page_[page_type_flag] = page;
799 #else
800 else if (UNBOXED_PAGE_FLAG == page_type_flag)
801 generations[generation].alloc_unboxed_start_page = page;
802 else /* Both code and data. */
803 generations[generation].alloc_start_page = page;
804 #endif
807 /* Find a new region with room for at least the given number of bytes.
809 * It starts looking at the current generation's alloc_start_page. So
810 * may pick up from the previous region if there is enough space. This
811 * keeps the allocation contiguous when scavenging the newspace.
813 * The alloc_region should have been closed by a call to
814 * gc_alloc_update_page_tables(), and will thus be in an empty state.
816 * To assist the scavenging functions write-protected pages are not
817 * used. Free pages should not be write-protected.
819 * It is critical to the conservative GC that the start of regions be
820 * known. To help achieve this only small regions are allocated at a
821 * time.
823 * During scavenging, pointers may be found to within the current
824 * region and the page generation must be set so that pointers to the
825 * from space can be recognized. Therefore the generation of pages in
826 * the region are set to gc_alloc_generation. To prevent another
827 * allocation call using the same pages, all the pages in the region
828 * are allocated, although they will initially be empty.
830 static void
831 gc_alloc_new_region(sword_t nbytes, int page_type_flag, struct alloc_region *alloc_region)
833 page_index_t first_page;
834 page_index_t last_page;
835 page_index_t i;
836 int ret;
839 FSHOW((stderr,
840 "/alloc_new_region for %d bytes from gen %d\n",
841 nbytes, gc_alloc_generation));
844 /* Check that the region is in a reset state. */
845 gc_assert((alloc_region->first_page == 0)
846 && (alloc_region->last_page == -1)
847 && (alloc_region->free_pointer == alloc_region->end_addr));
848 ret = thread_mutex_lock(&free_pages_lock);
849 gc_assert(ret == 0);
850 first_page = generation_alloc_start_page(gc_alloc_generation, page_type_flag, 0);
851 last_page=gc_find_freeish_pages(&first_page, nbytes, page_type_flag);
853 /* Set up the alloc_region. */
854 alloc_region->first_page = first_page;
855 alloc_region->last_page = last_page;
856 alloc_region->start_addr = page_address(first_page) + page_bytes_used(first_page);
857 alloc_region->free_pointer = alloc_region->start_addr;
858 alloc_region->end_addr = page_address(last_page+1);
860 /* Set up the pages. */
862 /* The first page may have already been in use. */
863 /* If so, just assert that it's consistent, otherwise, set it up. */
864 if (page_bytes_used(first_page)) {
865 gc_assert(page_table[first_page].allocated == page_type_flag);
866 gc_assert(page_table[first_page].gen == gc_alloc_generation);
867 gc_dcheck(page_table[first_page].large_object == 0);
868 } else {
869 page_table[first_page].allocated = page_type_flag;
870 page_table[first_page].gen = gc_alloc_generation;
872 page_table[first_page].allocated |= OPEN_REGION_PAGE_FLAG;
874 for (i = first_page+1; i <= last_page; i++) {
875 page_table[i].allocated = page_type_flag;
876 page_table[i].gen = gc_alloc_generation;
877 set_page_scan_start_offset(i,
878 addr_diff(page_address(i), alloc_region->start_addr));
879 page_table[i].allocated |= OPEN_REGION_PAGE_FLAG;
881 /* Bump up last_free_page. */
882 if (last_page+1 > last_free_page) {
883 last_free_page = last_page+1;
884 /* do we only want to call this on special occasions? like for
885 * boxed_region? */
886 set_alloc_pointer((lispobj)page_address(last_free_page));
888 ret = thread_mutex_unlock(&free_pages_lock);
889 gc_assert(ret == 0);
891 /* If the first page was only partial, don't check whether it's
892 * zeroed (it won't be) and don't zero it (since the parts that
893 * we're interested in are guaranteed to be zeroed).
895 if (page_bytes_used(first_page)) {
896 first_page++;
899 zero_dirty_pages(first_page, last_page);
901 /* we can do this after releasing free_pages_lock */
902 if (gencgc_zero_check) {
903 lispobj *p;
904 for (p = alloc_region->start_addr;
905 (void*)p < alloc_region->end_addr; p++) {
906 if (*p != 0) {
907 lose("The new region is not zero at %p (start=%p, end=%p).\n",
908 p, alloc_region->start_addr, alloc_region->end_addr);
914 /* If the record_new_objects flag is 2 then all new regions created
915 * are recorded.
917 * If it's 1 then then it is only recorded if the first page of the
918 * current region is <= new_areas_ignore_page. This helps avoid
919 * unnecessary recording when doing full scavenge pass.
921 * The new_object structure holds the page, byte offset, and size of
922 * new regions of objects. Each new area is placed in the array of
923 * these structures pointer to by new_areas. new_areas_index holds the
924 * offset into new_areas.
926 * If new_area overflows NUM_NEW_AREAS then it stops adding them. The
927 * later code must detect this and handle it, probably by doing a full
928 * scavenge of a generation. */
929 #define NUM_NEW_AREAS 512
930 static int record_new_objects = 0;
931 static page_index_t new_areas_ignore_page;
932 struct new_area {
933 page_index_t page;
934 size_t offset;
935 size_t size;
937 static struct new_area (*new_areas)[];
938 static size_t new_areas_index;
939 size_t max_new_areas;
941 /* Add a new area to new_areas. */
942 static void
943 add_new_area(page_index_t first_page, size_t offset, size_t size)
945 size_t new_area_start, c;
946 ssize_t i;
948 /* Ignore if full. */
949 if (new_areas_index >= NUM_NEW_AREAS)
950 return;
952 switch (record_new_objects) {
953 case 0:
954 return;
955 case 1:
956 if (first_page > new_areas_ignore_page)
957 return;
958 break;
959 case 2:
960 break;
961 default:
962 gc_abort();
965 new_area_start = npage_bytes(first_page) + offset;
967 /* Search backwards for a prior area that this follows from. If
968 found this will save adding a new area. */
969 for (i = new_areas_index-1, c = 0; (i >= 0) && (c < 8); i--, c++) {
970 size_t area_end =
971 npage_bytes((*new_areas)[i].page)
972 + (*new_areas)[i].offset
973 + (*new_areas)[i].size;
974 /*FSHOW((stderr,
975 "/add_new_area S1 %d %d %d %d\n",
976 i, c, new_area_start, area_end));*/
977 if (new_area_start == area_end) {
978 /*FSHOW((stderr,
979 "/adding to [%d] %d %d %d with %d %d %d:\n",
981 (*new_areas)[i].page,
982 (*new_areas)[i].offset,
983 (*new_areas)[i].size,
984 first_page,
985 offset,
986 size);*/
987 (*new_areas)[i].size += size;
988 return;
992 (*new_areas)[new_areas_index].page = first_page;
993 (*new_areas)[new_areas_index].offset = offset;
994 (*new_areas)[new_areas_index].size = size;
995 /*FSHOW((stderr,
996 "/new_area %d page %d offset %d size %d\n",
997 new_areas_index, first_page, offset, size));*/
998 new_areas_index++;
1000 /* Note the max new_areas used. */
1001 if (new_areas_index > max_new_areas)
1002 max_new_areas = new_areas_index;
1005 /* Update the tables for the alloc_region. The region may be added to
1006 * the new_areas.
1008 * When done the alloc_region is set up so that the next quick alloc
1009 * will fail safely and thus a new region will be allocated. Further
1010 * it is safe to try to re-update the page table of this reset
1011 * alloc_region. */
1012 void
1013 gc_alloc_update_page_tables(int page_type_flag, struct alloc_region *alloc_region)
1015 /* Catch an unused alloc_region. */
1016 if (alloc_region->last_page == -1)
1017 return;
1019 page_index_t first_page = alloc_region->first_page;
1020 page_index_t next_page = first_page+1;
1021 char *page_base = page_address(first_page);
1022 char *free_pointer = alloc_region->free_pointer;
1024 // page_bytes_used() can be done without holding a lock. Nothing else
1025 // affects the usage on the first page of a region owned by this thread.
1026 page_bytes_t orig_first_page_bytes_used = page_bytes_used(first_page);
1027 gc_assert(alloc_region->start_addr == page_base + orig_first_page_bytes_used);
1029 int ret = thread_mutex_lock(&free_pages_lock);
1030 gc_assert(ret == 0);
1032 // Mark the region as closed on its first page.
1033 page_table[first_page].allocated &= ~(OPEN_REGION_PAGE_FLAG);
1035 if (free_pointer != alloc_region->start_addr) {
1036 /* some bytes were allocated in the region */
1038 /* All the pages used need to be updated */
1040 /* Update the first page. */
1041 if (!orig_first_page_bytes_used)
1042 gc_assert(page_starts_contiguous_block_p(first_page));
1043 page_table[first_page].allocated &= ~(OPEN_REGION_PAGE_FLAG);
1045 #if SEGREGATED_CODE
1046 gc_assert(page_table[first_page].allocated == page_type_flag);
1047 #else
1048 gc_assert(page_table[first_page].allocated & page_type_flag);
1049 #endif
1050 gc_assert(page_table[first_page].gen == gc_alloc_generation);
1051 gc_assert(page_table[first_page].large_object == 0);
1053 /* Calculate the number of bytes used in this page. This is not
1054 * always the number of new bytes, unless it was free. */
1055 os_vm_size_t bytes_used = addr_diff(free_pointer, page_base);
1056 boolean more;
1057 if ((more = (bytes_used > GENCGC_CARD_BYTES)))
1058 bytes_used = GENCGC_CARD_BYTES;
1059 set_page_bytes_used(first_page, bytes_used);
1061 /* 'region_size' will be the sum of new bytes consumed by the region,
1062 * EXCLUDING any part of the first page already in use,
1063 * and any unused part of the final used page */
1064 os_vm_size_t region_size = bytes_used - orig_first_page_bytes_used;
1066 /* All the rest of the pages should be accounted for. */
1067 while (more) {
1068 page_table[next_page].allocated &= ~(OPEN_REGION_PAGE_FLAG);
1069 #if SEGREGATED_CODE
1070 gc_assert(page_table[next_page].allocated == page_type_flag);
1071 #else
1072 gc_assert(page_table[next_page].allocated & page_type_flag);
1073 #endif
1074 gc_assert(page_bytes_used(next_page) == 0);
1075 gc_assert(page_table[next_page].gen == gc_alloc_generation);
1076 gc_assert(page_table[next_page].large_object == 0);
1077 page_base += GENCGC_CARD_BYTES;
1078 gc_assert(page_scan_start_offset(next_page) ==
1079 addr_diff(page_base, alloc_region->start_addr));
1081 /* Calculate the number of bytes used in this page. */
1082 bytes_used = addr_diff(free_pointer, page_base);
1083 if ((more = (bytes_used > GENCGC_CARD_BYTES)))
1084 bytes_used = GENCGC_CARD_BYTES;
1085 set_page_bytes_used(next_page, bytes_used);
1086 region_size += bytes_used;
1088 next_page++;
1091 // Now 'next_page' is 1 page beyond those fully accounted for.
1092 gc_assert(addr_diff(free_pointer, alloc_region->start_addr) == region_size);
1093 // Update the global totals
1094 bytes_allocated += region_size;
1095 generations[gc_alloc_generation].bytes_allocated += region_size;
1097 /* Set the generations alloc restart page to the last page of
1098 * the region. */
1099 set_generation_alloc_start_page(gc_alloc_generation, page_type_flag, 0, next_page-1);
1101 /* Add the region to the new_areas if requested. */
1102 if (BOXED_PAGE_FLAG & page_type_flag)
1103 add_new_area(first_page,orig_first_page_bytes_used, region_size);
1105 } else if (!orig_first_page_bytes_used) {
1106 /* The first page is completely unused. Unallocate it */
1107 reset_page_flags(first_page);
1110 /* Unallocate any unused pages. */
1111 while (next_page <= alloc_region->last_page) {
1112 gc_assert(page_bytes_used(next_page) == 0);
1113 reset_page_flags(next_page);
1114 next_page++;
1116 ret = thread_mutex_unlock(&free_pages_lock);
1117 gc_assert(ret == 0);
1119 /* alloc_region is per-thread, we're ok to do this unlocked */
1120 gc_set_region_empty(alloc_region);
1123 /* Allocate a possibly large object. */
1124 void *
1125 gc_alloc_large(sword_t nbytes, int page_type_flag, struct alloc_region *alloc_region)
1127 boolean more;
1128 page_index_t first_page, next_page, last_page;
1129 os_vm_size_t byte_cnt;
1130 os_vm_size_t bytes_used;
1131 int ret;
1133 ret = thread_mutex_lock(&free_pages_lock);
1134 gc_assert(ret == 0);
1136 first_page = generation_alloc_start_page(gc_alloc_generation, page_type_flag, 1);
1137 // FIXME: really we want to try looking for space following the highest of
1138 // the last page of all other small object regions. That's impossible - there's
1139 // not enough information. At best we can skip some work in only the case where
1140 // the supplied region was the one most recently created. To do this right
1141 // would entail a malloc-like allocator at the page granularity.
1142 if (first_page <= alloc_region->last_page) {
1143 first_page = alloc_region->last_page+1;
1146 last_page=gc_find_freeish_pages(&first_page,nbytes, page_type_flag);
1148 gc_assert(first_page > alloc_region->last_page);
1150 set_generation_alloc_start_page(gc_alloc_generation, page_type_flag, 1, last_page);
1152 /* Large objects don't share pages with other objects. */
1153 gc_assert(page_bytes_used(first_page) == 0);
1155 /* Set up the pages. */
1156 page_table[first_page].allocated = page_type_flag;
1157 page_table[first_page].gen = gc_alloc_generation;
1158 page_table[first_page].large_object = 1;
1160 byte_cnt = 0;
1162 /* Calc. the number of bytes used in this page. This is not
1163 * always the number of new bytes, unless it was free. */
1164 more = 0;
1165 if ((bytes_used = nbytes) > GENCGC_CARD_BYTES) {
1166 bytes_used = GENCGC_CARD_BYTES;
1167 more = 1;
1169 set_page_bytes_used(first_page, bytes_used);
1170 byte_cnt += bytes_used;
1172 next_page = first_page+1;
1174 /* All the rest of the pages should be free. We need to set their
1175 * scan_start_offset pointer to the start of the region, and set
1176 * the bytes_used. */
1177 while (more) {
1178 gc_assert(page_free_p(next_page));
1179 gc_assert(page_bytes_used(next_page) == 0);
1180 page_table[next_page].allocated = page_type_flag;
1181 page_table[next_page].gen = gc_alloc_generation;
1182 page_table[next_page].large_object = 1;
1184 set_page_scan_start_offset(next_page, npage_bytes(next_page-first_page));
1186 /* Calculate the number of bytes used in this page. */
1187 more = 0;
1188 bytes_used = nbytes - byte_cnt;
1189 if (bytes_used > GENCGC_CARD_BYTES) {
1190 bytes_used = GENCGC_CARD_BYTES;
1191 more = 1;
1193 set_page_bytes_used(next_page, bytes_used);
1194 byte_cnt += bytes_used;
1195 next_page++;
1198 gc_assert(byte_cnt == (size_t)nbytes);
1200 bytes_allocated += nbytes;
1201 generations[gc_alloc_generation].bytes_allocated += nbytes;
1203 /* Add the region to the new_areas if requested. */
1204 if (BOXED_PAGE_FLAG & page_type_flag)
1205 add_new_area(first_page, 0, nbytes);
1207 /* Bump up last_free_page */
1208 if (last_page+1 > last_free_page) {
1209 last_free_page = last_page+1;
1210 set_alloc_pointer((lispobj)(page_address(last_free_page)));
1212 ret = thread_mutex_unlock(&free_pages_lock);
1213 gc_assert(ret == 0);
1215 zero_dirty_pages(first_page, last_page);
1217 return page_address(first_page);
1220 static page_index_t gencgc_alloc_start_page = -1;
1222 void
1223 gc_heap_exhausted_error_or_lose (sword_t available, sword_t requested)
1225 struct thread *thread = arch_os_get_current_thread();
1226 /* Write basic information before doing anything else: if we don't
1227 * call to lisp this is a must, and even if we do there is always
1228 * the danger that we bounce back here before the error has been
1229 * handled, or indeed even printed.
1231 report_heap_exhaustion(available, requested, thread);
1232 if (gc_active_p || (available == 0)) {
1233 /* If we are in GC, or totally out of memory there is no way
1234 * to sanely transfer control to the lisp-side of things.
1236 lose("Heap exhausted, game over.");
1238 else {
1239 /* FIXME: assert free_pages_lock held */
1240 (void)thread_mutex_unlock(&free_pages_lock);
1241 #if !(defined(LISP_FEATURE_WIN32) && defined(LISP_FEATURE_SB_THREAD))
1242 gc_assert(get_pseudo_atomic_atomic(thread));
1243 clear_pseudo_atomic_atomic(thread);
1244 if (get_pseudo_atomic_interrupted(thread))
1245 do_pending_interrupt();
1246 #endif
1247 /* Another issue is that signalling HEAP-EXHAUSTED error leads
1248 * to running user code at arbitrary places, even in a
1249 * WITHOUT-INTERRUPTS which may lead to a deadlock without
1250 * running out of the heap. So at this point all bets are
1251 * off. */
1252 if (read_TLS(INTERRUPTS_ENABLED,thread) == NIL)
1253 corruption_warning_and_maybe_lose
1254 ("Signalling HEAP-EXHAUSTED in a WITHOUT-INTERRUPTS.");
1255 /* available and requested should be double word aligned, thus
1256 they can passed as fixnums and shifted later. */
1257 funcall2(StaticSymbolFunction(HEAP_EXHAUSTED_ERROR), available, requested);
1258 lose("HEAP-EXHAUSTED-ERROR fell through");
1262 /* Test whether page 'index' can continue a non-large-object region
1263 * having specified 'gen' and 'allocated' values. */
1264 static inline boolean
1265 page_extensible_p(page_index_t index, generation_index_t gen, int allocated) {
1266 #ifdef LISP_FEATURE_BIG_ENDIAN /* TODO: implement the simpler test */
1267 /* Counterintuitively, gcc prefers to see sequential tests of the bitfields,
1268 * versus one test "!(p.large_object | p.write_protected | p.dont_move)".
1269 * When expressed as separate tests, it figures out that this can be optimized
1270 * as an AND. On the other hand, by attempting to *force* it to do that,
1271 * it shifts each field to the right to line them all up at bit index 0 to
1272 * test that 1 bit, which is a literal rendering of the user-written code.
1274 boolean result =
1275 page_table[index].allocated == allocated
1276 && page_table[index].gen == gen
1277 && !page_table[index].large_object
1278 && !page_table[index].write_protected
1279 && !page_table[index].dont_move;
1280 return result;
1281 #else
1282 /* Test all 5 conditions above as a single comparison against a mask.
1283 * (The C compiler doesn't understand how to do that)
1284 * Any bit that has a 1 in this mask must match the desired input.
1285 * The two 0 bits are for "has_pins" and "write_protected_cleared".
1286 * has_pins is irrelevant- it won't be 1 except during gc.
1287 * wp_cleared is probably 0, but needs to be masked out to be sure.
1288 * All other flag bits must be zero to pass the test.
1290 * large -\ /-- WP
1291 * v v
1292 * #b11111111_10101111
1293 * ^ ^^^
1294 * !move / \ allocated
1296 * The flags reside at 1 byte prior to 'gen' in the page structure.
1298 return (*(int16_t*)(&page_table[index].gen-1) & 0xFFAF) == ((gen<<8)|allocated);
1299 #endif
1302 /* Search for at least nbytes of space, possibly picking up any
1303 * remaining space on the tail of a page that was not fully used.
1305 * Non-small allocations are guaranteed to be page-aligned.
1307 page_index_t
1308 gc_find_freeish_pages(page_index_t *restart_page_ptr, sword_t bytes,
1309 int page_type_flag)
1311 page_index_t most_bytes_found_from = 0, most_bytes_found_to = 0;
1312 page_index_t first_page, last_page, restart_page = *restart_page_ptr;
1313 os_vm_size_t nbytes = bytes;
1314 os_vm_size_t nbytes_goal = nbytes;
1315 os_vm_size_t bytes_found = 0;
1316 os_vm_size_t most_bytes_found = 0;
1317 /* Note that this definition of "small" is not the complement
1318 * of "large" as used in gc_alloc_large(). That's fine.
1319 * The constraint we must respect is that a large object
1320 * MUST NOT share any of its pages with another object.
1321 * It should also be page-aligned, though that's not a restriction
1322 * per se, but a fairly obvious consequence of not sharing.
1324 boolean small_object = nbytes < GENCGC_CARD_BYTES;
1325 /* FIXME: assert(free_pages_lock is held); */
1327 if (nbytes_goal < gencgc_alloc_granularity)
1328 nbytes_goal = gencgc_alloc_granularity;
1329 #if !defined(LISP_FEATURE_64_BIT) && SEGREGATED_CODE
1330 // Increase the region size to avoid excessive fragmentation
1331 if (page_type_flag == CODE_PAGE_FLAG && nbytes_goal < 65536)
1332 nbytes_goal = 65536;
1333 #endif
1335 /* Toggled by gc_and_save for heap compaction, normally -1. */
1336 if (gencgc_alloc_start_page != -1) {
1337 restart_page = gencgc_alloc_start_page;
1340 /* FIXME: This is on bytes instead of nbytes pending cleanup of
1341 * long from the interface. */
1342 gc_assert(bytes>=0);
1343 first_page = restart_page;
1344 while (first_page < page_table_pages) {
1345 bytes_found = 0;
1346 if (page_free_p(first_page)) {
1347 gc_dcheck(!page_bytes_used(first_page));
1348 bytes_found = GENCGC_CARD_BYTES;
1349 } else if (small_object &&
1350 page_extensible_p(first_page, gc_alloc_generation, page_type_flag)) {
1351 bytes_found = GENCGC_CARD_BYTES - page_bytes_used(first_page);
1352 // XXX: Prefer to start non-code on new pages.
1353 // This is temporary until scavenging of small-object pages
1354 // is made a little more intelligent (work in progress).
1355 if (bytes_found < nbytes && page_type_flag != CODE_PAGE_FLAG) {
1356 if (bytes_found > most_bytes_found)
1357 most_bytes_found = bytes_found;
1358 first_page++;
1359 continue;
1361 } else {
1362 first_page++;
1363 continue;
1366 gc_dcheck(!page_table[first_page].write_protected);
1367 /* page_free_p() can legally be used at index 'page_table_pages'
1368 * because the array dimension is 1+page_table_pages */
1369 for (last_page = first_page+1;
1370 bytes_found < nbytes_goal &&
1371 page_free_p(last_page) && last_page < page_table_pages;
1372 last_page++) {
1373 /* page_free_p() implies 0 bytes used, thus GENCGC_CARD_BYTES available.
1374 * It also implies !write_protected, and if the OS's conception were
1375 * otherwise, lossage would routinely occur in the fault handler) */
1376 bytes_found += GENCGC_CARD_BYTES;
1377 gc_dcheck(0 == page_bytes_used(last_page));
1378 gc_dcheck(!page_table[last_page].write_protected);
1381 if (bytes_found > most_bytes_found) {
1382 most_bytes_found = bytes_found;
1383 most_bytes_found_from = first_page;
1384 most_bytes_found_to = last_page;
1386 if (bytes_found >= nbytes_goal)
1387 break;
1389 first_page = last_page;
1392 bytes_found = most_bytes_found;
1393 restart_page = first_page + 1;
1395 /* Check for a failure */
1396 if (bytes_found < nbytes) {
1397 gc_assert(restart_page >= page_table_pages);
1398 gc_heap_exhausted_error_or_lose(most_bytes_found, nbytes);
1401 gc_assert(most_bytes_found_to);
1402 *restart_page_ptr = most_bytes_found_from;
1403 return most_bytes_found_to-1;
1406 /* Allocate bytes. All the rest of the special-purpose allocation
1407 * functions will eventually call this */
1409 void *
1410 gc_alloc_with_region(sword_t nbytes,int page_type_flag, struct alloc_region *my_region,
1411 int quick_p)
1413 void *new_free_pointer;
1415 if (nbytes>=LARGE_OBJECT_SIZE)
1416 return gc_alloc_large(nbytes, page_type_flag, my_region);
1418 /* Check whether there is room in the current alloc region. */
1419 new_free_pointer = (char*)my_region->free_pointer + nbytes;
1421 /* fprintf(stderr, "alloc %d bytes from %p to %p\n", nbytes,
1422 my_region->free_pointer, new_free_pointer); */
1424 if (new_free_pointer <= my_region->end_addr) {
1425 /* If so then allocate from the current alloc region. */
1426 void *new_obj = my_region->free_pointer;
1427 my_region->free_pointer = new_free_pointer;
1429 /* Unless a `quick' alloc was requested, check whether the
1430 alloc region is almost empty. */
1431 if (!quick_p &&
1432 addr_diff(my_region->end_addr,my_region->free_pointer) <= 32) {
1433 /* If so, finished with the current region. */
1434 gc_alloc_update_page_tables(page_type_flag, my_region);
1435 /* Set up a new region. */
1436 gc_alloc_new_region(32 /*bytes*/, page_type_flag, my_region);
1439 return((void *)new_obj);
1442 /* Else not enough free space in the current region: retry with a
1443 * new region. */
1445 gc_alloc_update_page_tables(page_type_flag, my_region);
1446 gc_alloc_new_region(nbytes, page_type_flag, my_region);
1447 return gc_alloc_with_region(nbytes, page_type_flag, my_region,0);
1450 /* Copy a large object. If the object is on a large object page then
1451 * it is simply promoted, else it is copied.
1453 * Bignums and vectors may have shrunk. If the object is not copied
1454 * the space needs to be reclaimed, and the page_tables corrected.
1456 * Code objects can't shrink, but it's not worth adding an extra test
1457 * for large code just to avoid the loop that performs adjustment, so
1458 * go through the adjustment motions even though nothing happens.
1460 * An object that is on non-large object pages will never move
1461 * to large object pages, thus ensuring that the assignment of
1462 * '.large_object = 0' in prepare_for_final_gc() is meaningful.
1463 * The saved core should have no large object pages.
1465 lispobj
1466 copy_large_object(lispobj object, sword_t nwords, int page_type_flag)
1468 lispobj *new;
1469 page_index_t first_page;
1470 boolean boxedp = page_type_flag != UNBOXED_PAGE_FLAG;
1472 CHECK_COPY_PRECONDITIONS(object, nwords);
1474 if ((nwords > 1024*1024) && gencgc_verbose) {
1475 FSHOW((stderr, "/general_copy_large_object: %d bytes\n",
1476 nwords*N_WORD_BYTES));
1479 /* Check whether it's a large object. */
1480 first_page = find_page_index((void *)object);
1481 gc_assert(first_page >= 0);
1483 // An objects that shrank but was allocated on a large-object page
1484 // is a candidate for copying if its current size is non-large.
1485 if (page_table[first_page].large_object
1486 && nwords >= LARGE_OBJECT_SIZE / N_WORD_BYTES) {
1487 /* Promote the object. Note: Unboxed objects may have been
1488 * allocated to a BOXED region so it may be necessary to
1489 * change the region to UNBOXED. */
1490 os_vm_size_t remaining_bytes;
1491 os_vm_size_t bytes_freed;
1492 page_index_t next_page;
1493 page_bytes_t old_bytes_used;
1495 /* FIXME: This comment is somewhat stale.
1497 * Note: Any page write-protection must be removed, else a
1498 * later scavenge_newspace may incorrectly not scavenge these
1499 * pages. This would not be necessary if they are added to the
1500 * new areas, but let's do it for them all (they'll probably
1501 * be written anyway?). */
1503 gc_assert(page_starts_contiguous_block_p(first_page));
1504 next_page = first_page;
1505 remaining_bytes = nwords*N_WORD_BYTES;
1507 /* FIXME: can we share code with maybe_adjust_large_object ? */
1508 while (remaining_bytes > GENCGC_CARD_BYTES) {
1509 gc_assert(page_table[next_page].gen == from_space);
1510 gc_assert(page_table[next_page].large_object);
1511 gc_assert(page_scan_start_offset(next_page) ==
1512 npage_bytes(next_page-first_page));
1513 gc_assert(page_bytes_used(next_page) == GENCGC_CARD_BYTES);
1514 /* Should have been unprotected by unprotect_oldspace()
1515 * for boxed objects, and after promotion unboxed ones
1516 * should not be on protected pages at all. */
1517 gc_assert(!page_table[next_page].write_protected);
1519 if (boxedp)
1520 gc_assert(page_boxed_p(next_page));
1521 else {
1522 gc_assert(page_allocated_no_region_p(next_page));
1523 page_table[next_page].allocated = UNBOXED_PAGE_FLAG;
1525 page_table[next_page].gen = new_space;
1527 remaining_bytes -= GENCGC_CARD_BYTES;
1528 next_page++;
1531 /* Now only one page remains, but the object may have shrunk so
1532 * there may be more unused pages which will be freed. */
1534 /* Object may have shrunk but shouldn't have grown - check. */
1535 gc_assert(page_bytes_used(next_page) >= remaining_bytes);
1537 page_table[next_page].gen = new_space;
1539 if (boxedp)
1540 gc_assert(page_boxed_p(next_page));
1541 else
1542 page_table[next_page].allocated = UNBOXED_PAGE_FLAG;
1544 /* Adjust the bytes_used. */
1545 old_bytes_used = page_bytes_used(next_page);
1546 set_page_bytes_used(next_page, remaining_bytes);
1548 bytes_freed = old_bytes_used - remaining_bytes;
1550 /* Free any remaining pages; needs care. */
1551 next_page++;
1552 while ((old_bytes_used == GENCGC_CARD_BYTES) &&
1553 (page_table[next_page].gen == from_space) &&
1554 /* FIXME: It is not obvious to me why this is necessary
1555 * as a loop condition: it seems to me that the
1556 * scan_start_offset test should be sufficient, but
1557 * experimentally that is not the case. --NS
1558 * 2011-11-28 */
1559 (boxedp ?
1560 page_boxed_p(next_page) :
1561 page_allocated_no_region_p(next_page)) &&
1562 page_table[next_page].large_object &&
1563 (page_scan_start_offset(next_page) ==
1564 npage_bytes(next_page - first_page))) {
1565 /* Checks out OK, free the page. Don't need to both zeroing
1566 * pages as this should have been done before shrinking the
1567 * object. These pages shouldn't be write-protected, even if
1568 * boxed they should be zero filled. */
1569 gc_assert(!page_table[next_page].write_protected);
1571 old_bytes_used = page_bytes_used(next_page);
1572 reset_page_flags(next_page);
1573 set_page_bytes_used(next_page, 0);
1574 bytes_freed += old_bytes_used;
1575 next_page++;
1578 if ((bytes_freed > 0) && gencgc_verbose) {
1579 FSHOW((stderr,
1580 "/general_copy_large_object bytes_freed=%"OS_VM_SIZE_FMT"\n",
1581 bytes_freed));
1584 generations[from_space].bytes_allocated -= nwords*N_WORD_BYTES
1585 + bytes_freed;
1586 generations[new_space].bytes_allocated += nwords*N_WORD_BYTES;
1587 bytes_allocated -= bytes_freed;
1589 /* Add the region to the new_areas if requested. */
1590 if (boxedp)
1591 add_new_area(first_page,0,nwords*N_WORD_BYTES);
1593 return(object);
1595 } else {
1596 /* Allocate space. */
1597 new = gc_general_alloc(nwords*N_WORD_BYTES, page_type_flag, ALLOC_QUICK);
1599 /* Copy the object. */
1600 memcpy(new,native_pointer(object),nwords*N_WORD_BYTES);
1602 /* Return Lisp pointer of new object. */
1603 return make_lispobj(new, lowtag_of(object));
1607 /* to copy unboxed objects */
1608 lispobj
1609 copy_unboxed_object(lispobj object, sword_t nwords)
1611 return gc_general_copy_object(object, nwords, UNBOXED_PAGE_FLAG);
1615 * weak pointers
1618 static sword_t
1619 scav_weak_pointer(lispobj *where, lispobj object)
1621 struct weak_pointer * wp = (struct weak_pointer*)where;
1623 if (!wp->next && weak_pointer_breakable_p(wp)) {
1624 /* All weak pointers refer to objects at least as old as themselves,
1625 * because there is no slot setter for WEAK-POINTER-VALUE.
1626 * (i.e. You can't reference an object that didn't already exist,
1627 * assuming that users don't stuff a new value in via low-level hacks)
1628 * A weak pointer is breakable only if it points to an object in the
1629 * condemned generation, which must be as young as, or younger than
1630 * the weak pointer itself. Per the initial claim, it can't be younger.
1631 * So it must be in the same generation. Therefore, if the pointee
1632 * is condemned, the pointer itself must be condemned. Hence it must
1633 * not be on a write-protected page. Assert this, to be sure.
1634 * (This assertion is compiled out in a normal build,
1635 * so even if incorrect, it should be relatively harmless)
1637 gc_dcheck(!page_table[find_page_index(wp)].write_protected);
1638 add_to_weak_pointer_list(wp);
1641 /* Do not let GC scavenge the value slot of the weak pointer.
1642 * (That is why it is a weak pointer.) */
1644 return WEAK_POINTER_NWORDS;
1647 /* a faster version for searching the dynamic space. This will work even
1648 * if the object is in a current allocation region. */
1649 lispobj *
1650 search_dynamic_space(void *pointer)
1652 page_index_t page_index = find_page_index(pointer);
1653 lispobj *start;
1655 /* The address may be invalid, so do some checks. */
1656 if ((page_index == -1) || page_free_p(page_index))
1657 return NULL;
1658 start = (lispobj *)page_scan_start(page_index);
1659 return gc_search_space(start, pointer);
1662 #if !GENCGC_IS_PRECISE
1663 // Return the starting address of the object containing 'addr'
1664 // if and only if the object is one which would be evacuated from 'from_space'
1665 // were it allowed to be either discarded as garbage or moved.
1666 // 'addr_page_index' is the page containing 'addr' and must not be -1.
1667 // Return 0 if there is no such object - that is, if addr is past the
1668 // end of the used bytes, or its pages are not in 'from_space' etc.
1669 static lispobj*
1670 conservative_root_p(lispobj addr, page_index_t addr_page_index)
1672 /* quick check 1: Address is quite likely to have been invalid. */
1673 struct page* page = &page_table[addr_page_index];
1674 if ((addr & (GENCGC_CARD_BYTES - 1)) >= page_bytes_used(addr_page_index) ||
1675 #if SEGREGATED_CODE
1676 (!is_lisp_pointer(addr) && page->allocated != CODE_PAGE_FLAG) ||
1677 #endif
1678 (compacting_p() && (page->gen != from_space ||
1679 (page->large_object && page->dont_move))))
1680 return 0;
1681 gc_assert(!(page->allocated & OPEN_REGION_PAGE_FLAG));
1683 #if SEGREGATED_CODE
1684 /* quick check 2: Unless the page can hold code, the pointer's lowtag must
1685 * correspond to the widetag of the object. The object header can safely
1686 * be read even if it turns out that the pointer is not valid,
1687 * because the pointer was in bounds for the page.
1688 * Note that this can falsely pass if looking at the interior of an unboxed
1689 * array that masquerades as a Lisp object header by pure luck.
1690 * But if this doesn't pass, there's no point in proceeding to the
1691 * definitive test which involves searching for the containing object. */
1693 if (page->allocated != CODE_PAGE_FLAG) {
1694 lispobj* obj = native_pointer(addr);
1695 if (lowtag_of(addr) == LIST_POINTER_LOWTAG) {
1696 if (!is_cons_half(obj[0]) || !is_cons_half(obj[1]))
1697 return 0;
1698 } else {
1699 unsigned char widetag = widetag_of(*obj);
1700 if (!other_immediate_lowtag_p(widetag) ||
1701 lowtag_of(addr) != lowtag_for_widetag[widetag>>2])
1702 return 0;
1704 /* Don't gc_search_space() more than once for any object.
1705 * Doesn't apply to code since the base address is unknown */
1706 if (pinned_p(addr, addr_page_index)) return 0;
1708 #endif
1710 /* Filter out anything which can't be a pointer to a Lisp object
1711 * (or, as a special case which also requires dont_move, a return
1712 * address referring to something in a CodeObject). This is
1713 * expensive but important, since it vastly reduces the
1714 * probability that random garbage will be bogusly interpreted as
1715 * a pointer which prevents a page from moving. */
1716 lispobj* object_start = search_dynamic_space((void*)addr);
1717 if (!object_start) return 0;
1719 /* If the containing object is a code object and 'addr' points
1720 * anywhere beyond the boxed words,
1721 * presume it to be a valid unboxed return address. */
1722 if (instruction_ptr_p((void*)addr, object_start))
1723 return object_start;
1725 /* Large object pages only contain ONE object, and it will never
1726 * be a CONS. However, arrays and bignums can be allocated larger
1727 * than necessary and then shrunk to fit, leaving what look like
1728 * (0 . 0) CONSes at the end. These appear valid to
1729 * properly_tagged_descriptor_p(), so pick them off here. */
1730 if (((lowtag_of(addr) == LIST_POINTER_LOWTAG) &&
1731 page_table[addr_page_index].large_object)
1732 || !properly_tagged_descriptor_p((void*)addr, object_start))
1733 return 0;
1735 return object_start;
1737 #endif
1739 /* Adjust large bignum and vector objects. This will adjust the
1740 * allocated region if the size has shrunk, and change boxed pages
1741 * into unboxed pages. The pages are not promoted here, and the
1742 * object is not added to the new_regions; this is really
1743 * only designed to be called from preserve_pointer(). Shouldn't fail
1744 * if this is missed, just may delay the moving of objects to unboxed
1745 * pages, and the freeing of pages. */
1746 static void
1747 maybe_adjust_large_object(page_index_t first_page, sword_t nwords)
1749 lispobj* where = (lispobj*)page_address(first_page);
1750 page_index_t next_page;
1752 uword_t remaining_bytes;
1753 uword_t bytes_freed;
1754 uword_t old_bytes_used;
1756 int page_type_flag;
1758 /* Check whether it's a vector or bignum object. */
1759 lispobj widetag = widetag_of(where[0]);
1760 if (widetag == SIMPLE_VECTOR_WIDETAG)
1761 page_type_flag = BOXED_PAGE_FLAG;
1762 else if (specialized_vector_widetag_p(widetag) || widetag == BIGNUM_WIDETAG)
1763 page_type_flag = UNBOXED_PAGE_FLAG;
1764 else
1765 return;
1767 /* Note: Any page write-protection must be removed, else a later
1768 * scavenge_newspace may incorrectly not scavenge these pages.
1769 * This would not be necessary if they are added to the new areas,
1770 * but lets do it for them all (they'll probably be written
1771 * anyway?). */
1773 gc_assert(page_starts_contiguous_block_p(first_page));
1775 next_page = first_page;
1776 remaining_bytes = nwords*N_WORD_BYTES;
1777 while (remaining_bytes > GENCGC_CARD_BYTES) {
1778 gc_assert(page_table[next_page].gen == from_space);
1779 // We can't assert that page_table[next_page].allocated is correct,
1780 // because unboxed objects are initially allocated on boxed pages.
1781 gc_assert(page_allocated_no_region_p(next_page));
1782 gc_assert(page_table[next_page].large_object);
1783 gc_assert(page_scan_start_offset(next_page) ==
1784 npage_bytes(next_page-first_page));
1785 gc_assert(page_bytes_used(next_page) == GENCGC_CARD_BYTES);
1787 // This affects only one object, since large objects don't share pages.
1788 page_table[next_page].allocated = page_type_flag;
1790 /* Shouldn't be write-protected at this stage. Essential that the
1791 * pages aren't. */
1792 gc_assert(!page_table[next_page].write_protected);
1793 remaining_bytes -= GENCGC_CARD_BYTES;
1794 next_page++;
1797 /* Now only one page remains, but the object may have shrunk so
1798 * there may be more unused pages which will be freed. */
1800 /* Object may have shrunk but shouldn't have grown - check. */
1801 gc_assert(page_bytes_used(next_page) >= remaining_bytes);
1803 page_table[next_page].allocated = page_type_flag;
1805 /* Adjust the bytes_used. */
1806 old_bytes_used = page_bytes_used(next_page);
1807 set_page_bytes_used(next_page, remaining_bytes);
1809 bytes_freed = old_bytes_used - remaining_bytes;
1811 /* Free any remaining pages; needs care. */
1812 next_page++;
1813 while ((old_bytes_used == GENCGC_CARD_BYTES) &&
1814 (page_table[next_page].gen == from_space) &&
1815 page_allocated_no_region_p(next_page) &&
1816 page_table[next_page].large_object &&
1817 (page_scan_start_offset(next_page) ==
1818 npage_bytes(next_page - first_page))) {
1819 /* It checks out OK, free the page. We don't need to bother zeroing
1820 * pages as this should have been done before shrinking the
1821 * object. These pages shouldn't be write protected as they
1822 * should be zero filled. */
1823 gc_assert(!page_table[next_page].write_protected);
1825 old_bytes_used = page_bytes_used(next_page);
1826 reset_page_flags(next_page);
1827 set_page_bytes_used(next_page, 0);
1828 bytes_freed += old_bytes_used;
1829 next_page++;
1832 if ((bytes_freed > 0) && gencgc_verbose) {
1833 FSHOW((stderr,
1834 "/maybe_adjust_large_object() freed %d\n",
1835 bytes_freed));
1838 generations[from_space].bytes_allocated -= bytes_freed;
1839 bytes_allocated -= bytes_freed;
1841 return;
1844 #ifdef PIN_GRANULARITY_LISPOBJ
1845 /* After scavenging of the roots is done, we go back to the pinned objects
1846 * and look within them for pointers. While heap_scavenge() could certainly
1847 * do this, it would potentially lead to extra work, since we can't know
1848 * whether any given object has been examined at least once, since there is
1849 * no telltale forwarding-pointer. The easiest thing to do is defer all
1850 * pinned objects to a subsequent pass, as is done here.
1852 static void
1853 scavenge_pinned_ranges()
1855 int i;
1856 lispobj key;
1857 for_each_hopscotch_key(i, key, pinned_objects) {
1858 lispobj* obj = native_pointer(key);
1859 lispobj header = *obj;
1860 // Never invoke scavenger on a simple-fun, just code components.
1861 if (is_cons_half(header))
1862 scavenge(obj, 2);
1863 else if (widetag_of(header) != SIMPLE_FUN_WIDETAG)
1864 scavtab[widetag_of(header)](obj, header);
1868 /* Deposit filler objects on small object pinned pages
1869 * from the page start to the first pinned object and in between pairs
1870 * of pinned objects. Zero-fill bytes following the last pinned object.
1871 * Also ensure that no scan_start_offset points to a page in
1872 * oldspace that will be freed.
1874 static void
1875 wipe_nonpinned_words()
1877 void gc_heapsort_uwords(uword_t*, int);
1879 if (!pinned_objects.count)
1880 return;
1882 // Loop over the keys in pinned_objects and pack them densely into
1883 // the same array - pinned_objects.keys[] - but skip any simple-funs.
1884 // Admittedly this is abstraction breakage.
1885 int limit = hopscotch_max_key_index(pinned_objects);
1886 int n_pins = 0, i;
1887 for (i = 0; i <= limit; ++i) {
1888 lispobj key = pinned_objects.keys[i];
1889 if (key) {
1890 lispobj* obj = native_pointer(key);
1891 // No need to check for is_cons_half() - it will be false
1892 // on a simple-fun header, and that's the correct answer.
1893 if (widetag_of(*obj) != SIMPLE_FUN_WIDETAG)
1894 pinned_objects.keys[n_pins++] = (uword_t)obj;
1897 // Don't touch pinned_objects.count in case the reset function uses it
1898 // to decide how to resize for next use (which it doesn't, but could).
1899 gc_n_stack_pins = n_pins;
1900 // Order by ascending address, stopping short of the sentinel.
1901 gc_heapsort_uwords(pinned_objects.keys, n_pins);
1902 #if 0
1903 fprintf(stderr, "Sorted pin list:\n");
1904 for (i = 0; i < n_pins; ++i) {
1905 lispobj* obj = (lispobj*)pinned_objects.keys[i];
1906 lispobj word = *obj;
1907 int widetag = widetag_of(word);
1908 if (is_cons_half(word))
1909 fprintf(stderr, "%p: (cons)\n", obj);
1910 else
1911 fprintf(stderr, "%p: %d words (%s)\n", obj,
1912 (int)sizetab[widetag](obj), widetag_names[widetag>>2]);
1914 #endif
1916 #define page_base(x) ALIGN_DOWN(x, GENCGC_CARD_BYTES)
1917 // This macro asserts that space accounting happens exactly
1918 // once per affected page (a page with any pins, no matter how many)
1919 #define adjust_gen_usage(i) \
1920 gc_assert(page_table[i].has_pins); \
1921 page_table[i].has_pins = 0; \
1922 bytes_moved += page_bytes_used(i); \
1923 page_table[i].gen = new_space
1925 // Store a sentinel at the end. Even if n_pins = table capacity (unlikely),
1926 // it is safe to write one more word, because the hops[] array immediately
1927 // follows the keys[] array in memory. At worst, 2 elements of hops[]
1928 // are clobbered, which is irrelevant since the table has already been
1929 // rendered unusable by stealing its key array for a different purpose.
1930 pinned_objects.keys[n_pins] = ~(uword_t)0;
1932 // Each pinned object begets two ranges of bytes to be turned into filler:
1933 // - the range preceding it back to its page start or predecessor object
1934 // - the range after it, up to the lesser of page bytes used or successor object
1936 // Prime the loop
1937 uword_t fill_from = page_base(pinned_objects.keys[0]);
1938 os_vm_size_t bytes_moved = 0; // i.e. virtually moved
1939 os_vm_size_t bytes_freed = 0; // bytes after last pinned object per page
1941 for (i = 0; i < n_pins; ++i) {
1942 lispobj* obj = (lispobj*)pinned_objects.keys[i];
1943 page_index_t begin_page_index = find_page_index(obj);
1944 // Create a filler object occupying space from 'fill_from' up to but
1945 // excluding 'obj'. If obj directly abuts its predecessor then don't.
1946 if ((uword_t)obj > fill_from) {
1947 lispobj* filler = (lispobj*)fill_from;
1948 int nwords = obj - filler;
1949 if (page_table[begin_page_index].allocated != CODE_PAGE_FLAG) {
1950 // On pages holding non-code, the filler is an array
1951 filler[0] = SIMPLE_ARRAY_WORD_WIDETAG;
1952 filler[1] = make_fixnum(nwords - 2);
1953 } else if (nwords > 2) {
1954 // Otherwise try to keep a strict code/non-code distinction
1955 filler[0] = 2<<N_WIDETAG_BITS | CODE_HEADER_WIDETAG;
1956 filler[1] = make_fixnum((nwords - 2) * N_WORD_BYTES);
1957 filler[2] = 0;
1958 filler[3] = 0;
1959 } else {
1960 // But as an exception, use a NIL array for tiny code filler
1961 // (If the ENSURE-CODE/DATA-SEPARATION test fails again,
1962 // it may need to ignore these objects. Hasn't happened yet)
1963 filler[0] = SIMPLE_ARRAY_NIL_WIDETAG;
1964 filler[1] = make_fixnum(0xDEAD);
1967 if (fill_from == page_base((uword_t)obj)) {
1968 adjust_gen_usage(begin_page_index);
1969 // This pinned object started a new page of pins.
1970 // scan_start must not see any page prior to this page,
1971 // as those might be in oldspace and about to be marked free.
1972 set_page_scan_start_offset(begin_page_index, 0);
1974 // If 'obj' spans pages, move its successive page(s) to newspace and
1975 // ensure that those pages' scan_starts point at the same address
1976 // that this page's scan start does, which could be this page or earlier.
1977 size_t nwords = OBJECT_SIZE(*obj, obj);
1978 lispobj* obj_end = obj + nwords; // non-inclusive address bound
1979 page_index_t end_page_index = find_page_index(obj_end - 1); // inclusive bound
1981 if (end_page_index > begin_page_index) {
1982 char *scan_start = page_scan_start(begin_page_index);
1983 page_index_t index;
1984 for (index = begin_page_index + 1; index <= end_page_index; ++index) {
1985 set_page_scan_start_offset(index,
1986 addr_diff(page_address(index), scan_start));
1987 adjust_gen_usage(index);
1990 // Compute page base address of last page touched by this obj.
1991 uword_t obj_end_pageaddr = page_base((uword_t)obj_end - 1);
1992 // See if there's another pinned object on this page.
1993 // There is always a next object, due to the sentinel.
1994 if (pinned_objects.keys[i+1] < obj_end_pageaddr + GENCGC_CARD_BYTES) {
1995 // Next object starts within the same page.
1996 fill_from = (uword_t)obj_end;
1997 } else {
1998 // Next pinned object does not start on the same page this obj ends on.
1999 // Any bytes following 'obj' up to its page end are garbage.
2000 uword_t page_end = obj_end_pageaddr + page_bytes_used(end_page_index);
2001 long nbytes = page_end - (uword_t)obj_end;
2002 gc_assert(nbytes >= 0);
2003 if (nbytes) {
2004 // Bytes beyond a page's highest used byte must be zero.
2005 memset(obj_end, 0, nbytes);
2006 bytes_freed += nbytes;
2007 set_page_bytes_used(end_page_index,
2008 (uword_t)obj_end - obj_end_pageaddr);
2010 fill_from = page_base(pinned_objects.keys[i+1]);
2013 generations[from_space].bytes_allocated -= bytes_moved;
2014 generations[new_space].bytes_allocated += bytes_moved - bytes_freed;
2015 bytes_allocated -= bytes_freed;
2016 #undef adjust_gen_usage
2017 #undef page_base
2020 /* Add 'object' to the hashtable, and if the object is a code component,
2021 * then also add all of the embedded simple-funs.
2022 * The rationale for the extra work on code components is that without it,
2023 * every test of pinned_p() on an object would have to check if the pointer
2024 * is to a simple-fun - entailing an extra read of the header - and mapping
2025 * to its code component if so. Since more calls to pinned_p occur than to
2026 * pin_object, the extra burden should be on this function.
2027 * Experimentation bears out that this is the better technique.
2028 * Also, we wouldn't often expect code components in the collected generation
2029 * so the extra work here is quite minimal, even if it can generally add to
2030 * the number of keys in the hashtable.
2032 static void
2033 pin_object(lispobj* base_addr)
2035 lispobj object = compute_lispobj(base_addr);
2036 if (!hopscotch_containsp(&pinned_objects, object)) {
2037 hopscotch_insert(&pinned_objects, object, 1);
2038 struct code* maybe_code = (struct code*)native_pointer(object);
2039 if (widetag_of(maybe_code->header) == CODE_HEADER_WIDETAG) {
2040 for_each_simple_fun(i, fun, maybe_code, 0, {
2041 hopscotch_insert(&pinned_objects,
2042 make_lispobj(fun, FUN_POINTER_LOWTAG),
2048 #else
2049 # define scavenge_pinned_ranges()
2050 # define wipe_nonpinned_words()
2051 #endif
2053 /* Take a possible pointer to a Lisp object and mark its page in the
2054 * page_table so that it will not be relocated during a GC.
2056 * This involves locating the page it points to, then backing up to
2057 * the start of its region, then marking all pages dont_move from there
2058 * up to the first page that's not full or has a different generation
2060 * It is assumed that all the page static flags have been cleared at
2061 * the start of a GC.
2063 * It is also assumed that the current gc_alloc() region has been
2064 * flushed and the tables updated. */
2066 // TODO: there's probably a way to be a little more efficient here.
2067 // As things are, we start by finding the object that encloses 'addr',
2068 // then we see if 'addr' was a "valid" Lisp pointer to that object
2069 // - meaning we expect the correct lowtag on the pointer - except
2070 // that for code objects we don't require a correct lowtag
2071 // and we allow a pointer to anywhere in the object.
2073 // It should be possible to avoid calling search_dynamic_space
2074 // more of the time. First, check if the page pointed to might hold code.
2075 // If it does, then we continue regardless of the pointer's lowtag
2076 // (because of the special allowance). If the page definitely does *not*
2077 // hold code, then we require up front that the lowtake make sense,
2078 // by doing the same checks that are in properly_tagged_descriptor_p.
2080 // Problem: when code is allocated from a per-thread region,
2081 // does it ensure that the occupied pages are flagged as having code?
2083 #if defined(__GNUC__) && defined(MEMORY_SANITIZER)
2084 #define NO_SANITIZE_MEMORY __attribute__((no_sanitize_memory))
2085 #else
2086 #define NO_SANITIZE_MEMORY
2087 #endif
2089 static void NO_SANITIZE_MEMORY
2090 preserve_pointer(void *addr)
2092 #ifdef LISP_FEATURE_IMMOBILE_SPACE
2093 /* Immobile space MUST be lower than dynamic space,
2094 or else this test needs to be revised */
2095 if (addr < (void*)DYNAMIC_SPACE_START) {
2096 extern void immobile_space_preserve_pointer(void*);
2097 immobile_space_preserve_pointer(addr);
2098 return;
2100 #endif
2101 page_index_t page = find_page_index(addr);
2102 lispobj *object_start;
2104 #if GENCGC_IS_PRECISE
2105 /* If we're in precise gencgc (non-x86oid as of this writing) then
2106 * we are only called on valid object pointers in the first place,
2107 * so we just have to do a bounds-check against the heap, a
2108 * generation check, and the already-pinned check. */
2109 if (page < 0 ||
2110 (compacting_p() && (page_table[page].gen != from_space ||
2111 (page_table[page].large_object &&
2112 page_table[page].dont_move))))
2113 return;
2114 object_start = native_pointer((lispobj)addr);
2115 switch (widetag_of(*object_start)) {
2116 case SIMPLE_FUN_WIDETAG:
2117 #ifdef RETURN_PC_WIDETAG
2118 case RETURN_PC_WIDETAG:
2119 #endif
2120 object_start = fun_code_header(object_start);
2122 #else
2123 if (page < 0 || (object_start = conservative_root_p((lispobj)addr, page)) == NULL)
2124 return;
2125 #endif
2127 if (!compacting_p()) {
2128 /* Just mark it. No distinction between large and small objects. */
2129 gc_mark_obj(compute_lispobj(object_start));
2130 return;
2133 page_index_t first_page = find_page_index(object_start);
2134 size_t nwords = OBJECT_SIZE(*object_start, object_start);
2135 page_index_t last_page = find_page_index(object_start + nwords - 1);
2137 for (page = first_page; page <= last_page; ++page) {
2138 /* Oldspace pages were unprotected at start of GC.
2139 * Assert this here, because the previous logic used to,
2140 * and page protection bugs are scary */
2141 gc_assert(!page_table[page].write_protected);
2143 /* Mark the page static. */
2144 page_table[page].dont_move = 1;
2145 page_table[page].has_pins = !page_table[page].large_object;
2148 if (page_table[first_page].large_object)
2149 maybe_adjust_large_object(first_page, nwords);
2150 else
2151 pin_object(object_start);
2155 #define IN_REGION_P(a,kind) (kind##_region.start_addr<=a && a<=kind##_region.free_pointer)
2156 #if SEGREGATED_CODE
2157 #define IN_BOXED_REGION_P(a) IN_REGION_P(a,boxed)||IN_REGION_P(a,code)
2158 #else
2159 #define IN_BOXED_REGION_P(a) IN_REGION_P(a,boxed)
2160 #endif
2162 /* If the given page is not write-protected, then scan it for pointers
2163 * to younger generations or the top temp. generation, if no
2164 * suspicious pointers are found then the page is write-protected.
2166 * Care is taken to check for pointers to the current gc_alloc()
2167 * region if it is a younger generation or the temp. generation. This
2168 * frees the caller from doing a gc_alloc_update_page_tables(). Actually
2169 * the gc_alloc_generation does not need to be checked as this is only
2170 * called from scavenge_generation() when the gc_alloc generation is
2171 * younger, so it just checks if there is a pointer to the current
2172 * region.
2174 * We return 1 if the page was write-protected, else 0.
2176 * Note that because of the existence of some words which have fixnum lowtag
2177 * but are actually pointers, you might think it would be possible for this
2178 * function to go wrong, protecting a page that contains old->young pointers.
2179 * Well, it seems fine mostly. Why: two of the guilty parties are CLOSURE-FUN
2180 * and FDEFN-RAW-ADDR. Closure-fun is a fixnum (on x86) which when treated
2181 * as a pointer indicates the entry point to call. Its function can never
2182 * be an object younger than itself. (An invariant of any immutable object)
2183 * fdefn-raw-address is more subtle. In set-fdefn-fun we first store 'fun'
2184 * and then 'raw-addr', where a stop-for-GC could occur in between.
2185 * So if the fdefn was, before the first store:
2186 * fun -> younger object
2187 * raw-addr -> younger object
2188 * and then after the first store:
2189 * fun -> older object | <- interrupt occurred after this store
2190 * raw-addr -> younger object
2191 * then we have a page that may look like it has no traceable pointers
2192 * to younger objects (the raw-addr is untraceable by the algorithm below).
2193 * But because the fdefn is in a register, it is pinned, therefore it is live,
2194 * therefore all its slots will be traced on this GC.
2195 * In fact update_page_write_prot() won't even be called on the fdefn's page.
2196 * The final problem is compact-instance-header layouts. Conditions and
2197 * structures can't point to younger layouts, so that much is easy.
2198 * Standard-objects can. I think those layouts are kept live by the
2199 * voluminous amount of metadata that CLOS insists on maintaining,
2200 * though I'm not 100% sure, and would not be surprised if there is a bug
2201 * related to GC of those layouts.
2203 static int
2204 update_page_write_prot(page_index_t page)
2206 generation_index_t gen = page_table[page].gen;
2207 sword_t j;
2208 int wp_it = 1;
2209 void **page_addr = (void **)page_address(page);
2210 sword_t num_words = page_bytes_used(page) / N_WORD_BYTES;
2212 /* Shouldn't be a free page. */
2213 gc_dcheck(!page_free_p(page)); // Implied by the next assertion
2214 gc_assert(page_bytes_used(page) != 0);
2216 if (!ENABLE_PAGE_PROTECTION) return 0;
2218 /* Skip if it's already write-protected, pinned, or unboxed */
2219 if (page_table[page].write_protected
2220 /* FIXME: What's the reason for not write-protecting pinned pages? */
2221 || page_table[page].dont_move
2222 || page_unboxed_p(page))
2223 return (0);
2225 /* Scan the page for pointers to younger generations or the
2226 * top temp. generation. */
2228 /* This is conservative: any word satisfying is_lisp_pointer() is
2229 * assumed to be a pointer. To do otherwise would require a family
2230 * of scavenge-like functions. */
2231 for (j = 0; j < num_words; j++) {
2232 void *ptr = *(page_addr+j);
2233 page_index_t index;
2234 lispobj __attribute__((unused)) header;
2236 if (!is_lisp_pointer((lispobj)ptr))
2237 continue;
2238 /* Check that it's in the dynamic space */
2239 if ((index = find_page_index(ptr)) != -1) {
2240 if (/* Does it point to a younger or the temp. generation? */
2241 ((page_bytes_used(index) != 0)
2242 && ((page_table[index].gen < gen)
2243 || (page_table[index].gen == SCRATCH_GENERATION)))
2245 /* Or does it point within a current gc_alloc() region? */
2246 || (IN_BOXED_REGION_P(ptr) || IN_REGION_P(ptr,unboxed))) {
2247 wp_it = 0;
2248 break;
2251 #ifdef LISP_FEATURE_IMMOBILE_SPACE
2252 else if (immobile_space_p((lispobj)ptr) &&
2253 other_immediate_lowtag_p(header = *native_pointer((lispobj)ptr))) {
2254 // This is *possibly* a pointer to an object in immobile space,
2255 // given that above two conditions were satisfied.
2256 // But unlike in the dynamic space case, we need to read a byte
2257 // from the object to determine its generation, which requires care.
2258 // Consider an unboxed word that looks like a pointer to a word that
2259 // looks like simple-fun-widetag. We can't naively back up to the
2260 // underlying code object since the alleged header might not be one.
2261 int obj_gen = gen; // Make comparison fail if we fall through
2262 if (lowtag_of((lispobj)ptr) == FUN_POINTER_LOWTAG &&
2263 widetag_of(header) == SIMPLE_FUN_WIDETAG) {
2264 lispobj* code = fun_code_header((lispobj)ptr - FUN_POINTER_LOWTAG);
2265 // This is a heuristic, since we're not actually looking for
2266 // an object boundary. Precise scanning of 'page' would obviate
2267 // the guard conditions here.
2268 if (immobile_space_p((lispobj)code)
2269 && widetag_of(*code) == CODE_HEADER_WIDETAG)
2270 obj_gen = __immobile_obj_generation(code);
2271 } else {
2272 obj_gen = __immobile_obj_generation(native_pointer((lispobj)ptr));
2274 // A bogus generation number implies a not-really-pointer,
2275 // but it won't cause misbehavior.
2276 if (obj_gen < gen || obj_gen == SCRATCH_GENERATION) {
2277 wp_it = 0;
2278 break;
2281 #endif
2284 if (wp_it == 1)
2285 protect_page(page_addr, page);
2287 return (wp_it);
2290 /* Is this page holding a normal (non-hashtable) large-object
2291 * simple-vector? */
2292 static inline boolean large_simple_vector_p(page_index_t page) {
2293 if (!page_table[page].large_object)
2294 return 0;
2295 lispobj header = *(lispobj *)page_address(page);
2296 return widetag_of(header) == SIMPLE_VECTOR_WIDETAG &&
2297 is_vector_subtype(header, VectorNormal);
2301 /* Scavenge all generations from FROM to TO, inclusive, except for
2302 * new_space which needs special handling, as new objects may be
2303 * added which are not checked here - use scavenge_newspace generation.
2305 * Write-protected pages should not have any pointers to the
2306 * from_space so do need scavenging; thus write-protected pages are
2307 * not always scavenged. There is some code to check that these pages
2308 * are not written; but to check fully the write-protected pages need
2309 * to be scavenged by disabling the code to skip them.
2311 * Under the current scheme when a generation is GCed the younger
2312 * generations will be empty. So, when a generation is being GCed it
2313 * is only necessary to scavenge the older generations for pointers
2314 * not the younger. So a page that does not have pointers to younger
2315 * generations does not need to be scavenged.
2317 * The write-protection can be used to note pages that don't have
2318 * pointers to younger pages. But pages can be written without having
2319 * pointers to younger generations. After the pages are scavenged here
2320 * they can be scanned for pointers to younger generations and if
2321 * there are none the page can be write-protected.
2323 * One complication is when the newspace is the top temp. generation.
2325 * Enabling SC_GEN_CK scavenges the write-protected pages and checks
2326 * that none were written, which they shouldn't be as they should have
2327 * no pointers to younger generations. This breaks down for weak
2328 * pointers as the objects contain a link to the next and are written
2329 * if a weak pointer is scavenged. Still it's a useful check. */
2330 static void
2331 scavenge_generations(generation_index_t from, generation_index_t to)
2333 page_index_t i;
2334 page_index_t num_wp = 0;
2336 #define SC_GEN_CK 0
2337 #if SC_GEN_CK
2338 /* Clear the write_protected_cleared flags on all pages. */
2339 for (i = 0; i < page_table_pages; i++)
2340 page_table[i].write_protected_cleared = 0;
2341 #endif
2343 for (i = 0; i < last_free_page; i++) {
2344 generation_index_t generation = page_table[i].gen;
2345 if (page_boxed_p(i)
2346 && (page_bytes_used(i) != 0)
2347 && (generation != new_space)
2348 && (generation >= from)
2349 && (generation <= to)) {
2351 /* This should be the start of a region */
2352 gc_assert(page_starts_contiguous_block_p(i));
2354 if (large_simple_vector_p(i)) {
2355 /* Scavenge only the unprotected pages of a
2356 * large-object vector, other large objects could be
2357 * handled as well, but vectors are easier to deal
2358 * with and are more likely to grow to very large
2359 * sizes where avoiding scavenging the whole thing is
2360 * worthwile */
2361 if (!page_table[i].write_protected) {
2362 scavenge((lispobj*)page_address(i) + 2,
2363 GENCGC_CARD_BYTES / N_WORD_BYTES - 2);
2364 update_page_write_prot(i);
2366 while (!page_ends_contiguous_block_p(i, generation)) {
2367 ++i;
2368 if (!page_table[i].write_protected) {
2369 scavenge((lispobj*)page_address(i),
2370 page_bytes_used(i) / N_WORD_BYTES);
2371 update_page_write_prot(i);
2374 } else {
2375 page_index_t last_page, j;
2376 boolean write_protected = 1;
2377 /* Now work forward until the end of the region */
2378 for (last_page = i; ; last_page++) {
2379 write_protected =
2380 write_protected && page_table[last_page].write_protected;
2381 if (page_ends_contiguous_block_p(last_page, generation))
2382 break;
2384 if (!write_protected) {
2385 heap_scavenge((lispobj*)page_address(i),
2386 (lispobj*)(page_address(last_page)
2387 + page_bytes_used(last_page)));
2389 /* Now scan the pages and write protect those that
2390 * don't have pointers to younger generations. */
2391 for (j = i; j <= last_page; j++)
2392 num_wp += update_page_write_prot(j);
2394 if ((gencgc_verbose > 1) && (num_wp != 0)) {
2395 FSHOW((stderr,
2396 "/write protected %d pages within generation %d\n",
2397 num_wp, generation));
2400 i = last_page;
2405 #if SC_GEN_CK
2406 /* Check that none of the write_protected pages in this generation
2407 * have been written to. */
2408 for (i = 0; i < page_table_pages; i++) {
2409 if ((page_bytes_used(i) != 0)
2410 && (page_table[i].gen == generation)
2411 && (page_table[i].write_protected_cleared != 0)) {
2412 FSHOW((stderr, "/scavenge_generation() %d\n", generation));
2413 FSHOW((stderr,
2414 "/page bytes_used=%d scan_start_offset=%lu dont_move=%d\n",
2415 page_bytes_used(i),
2416 scan_start_offset(page_table[i]),
2417 page_table[i].dont_move));
2418 lose("write to protected page %d in scavenge_generation()\n", i);
2421 #endif
2425 /* Scavenge a newspace generation. As it is scavenged new objects may
2426 * be allocated to it; these will also need to be scavenged. This
2427 * repeats until there are no more objects unscavenged in the
2428 * newspace generation.
2430 * To help improve the efficiency, areas written are recorded by
2431 * gc_alloc() and only these scavenged. Sometimes a little more will be
2432 * scavenged, but this causes no harm. An easy check is done that the
2433 * scavenged bytes equals the number allocated in the previous
2434 * scavenge.
2436 * Write-protected pages are not scanned except if they are marked
2437 * dont_move in which case they may have been promoted and still have
2438 * pointers to the from space.
2440 * Write-protected pages could potentially be written by alloc however
2441 * to avoid having to handle re-scavenging of write-protected pages
2442 * gc_alloc() does not write to write-protected pages.
2444 * New areas of objects allocated are recorded alternatively in the two
2445 * new_areas arrays below. */
2446 static struct new_area new_areas_1[NUM_NEW_AREAS];
2447 static struct new_area new_areas_2[NUM_NEW_AREAS];
2449 #ifdef LISP_FEATURE_IMMOBILE_SPACE
2450 extern unsigned int immobile_scav_queue_count;
2451 extern void
2452 update_immobile_nursery_bits(),
2453 scavenge_immobile_roots(generation_index_t,generation_index_t),
2454 scavenge_immobile_newspace(),
2455 sweep_immobile_space(int raise),
2456 write_protect_immobile_space();
2457 #else
2458 #define immobile_scav_queue_count 0
2459 #endif
2461 /* Do one full scan of the new space generation. This is not enough to
2462 * complete the job as new objects may be added to the generation in
2463 * the process which are not scavenged. */
2464 static void
2465 scavenge_newspace_generation_one_scan(generation_index_t generation)
2467 page_index_t i;
2469 FSHOW((stderr,
2470 "/starting one full scan of newspace generation %d\n",
2471 generation));
2472 for (i = 0; i < last_free_page; i++) {
2473 /* Note that this skips over open regions when it encounters them. */
2474 if (page_boxed_p(i)
2475 && (page_bytes_used(i) != 0)
2476 && (page_table[i].gen == generation)
2477 && (!page_table[i].write_protected
2478 /* (This may be redundant as write_protected is now
2479 * cleared before promotion.) */
2480 || page_table[i].dont_move)) {
2481 page_index_t last_page;
2482 int all_wp=1;
2484 /* The scavenge will start at the scan_start_offset of
2485 * page i.
2487 * We need to find the full extent of this contiguous
2488 * block in case objects span pages.
2490 * Now work forward until the end of this contiguous area
2491 * is found. A small area is preferred as there is a
2492 * better chance of its pages being write-protected. */
2493 for (last_page = i; ;last_page++) {
2494 /* If all pages are write-protected and movable,
2495 * then no need to scavenge */
2496 all_wp=all_wp && page_table[last_page].write_protected &&
2497 !page_table[last_page].dont_move;
2499 /* Check whether this is the last page in this
2500 * contiguous block */
2501 if (page_ends_contiguous_block_p(last_page, generation))
2502 break;
2505 /* Do a limited check for write-protected pages. */
2506 if (!all_wp) {
2507 new_areas_ignore_page = last_page;
2508 heap_scavenge(page_scan_start(i),
2509 (lispobj*)(page_address(last_page)
2510 + page_bytes_used(last_page)));
2512 i = last_page;
2515 FSHOW((stderr,
2516 "/done with one full scan of newspace generation %d\n",
2517 generation));
2520 /* Do a complete scavenge of the newspace generation. */
2521 static void
2522 scavenge_newspace_generation(generation_index_t generation)
2524 size_t i;
2526 /* the new_areas array currently being written to by gc_alloc() */
2527 struct new_area (*current_new_areas)[] = &new_areas_1;
2528 size_t current_new_areas_index;
2530 /* the new_areas created by the previous scavenge cycle */
2531 struct new_area (*previous_new_areas)[] = NULL;
2532 size_t previous_new_areas_index;
2534 /* Flush the current regions updating the tables. */
2535 gc_alloc_update_all_page_tables(0);
2537 /* Turn on the recording of new areas by gc_alloc(). */
2538 new_areas = current_new_areas;
2539 new_areas_index = 0;
2541 /* Don't need to record new areas that get scavenged anyway during
2542 * scavenge_newspace_generation_one_scan. */
2543 record_new_objects = 1;
2545 /* Start with a full scavenge. */
2546 scavenge_newspace_generation_one_scan(generation);
2548 /* Record all new areas now. */
2549 record_new_objects = 2;
2551 /* Give a chance to weak hash tables to make other objects live.
2552 * FIXME: The algorithm implemented here for weak hash table gcing
2553 * is O(W^2+N) as Bruno Haible warns in
2554 * http://www.haible.de/bruno/papers/cs/weak/WeakDatastructures-writeup.html
2555 * see "Implementation 2". */
2556 scav_weak_hash_tables(weak_ht_alivep_funs, gc_scav_pair);
2558 /* Flush the current regions updating the tables. */
2559 gc_alloc_update_all_page_tables(0);
2561 /* Grab new_areas_index. */
2562 current_new_areas_index = new_areas_index;
2564 /*FSHOW((stderr,
2565 "The first scan is finished; current_new_areas_index=%d.\n",
2566 current_new_areas_index));*/
2568 while (current_new_areas_index > 0 || immobile_scav_queue_count) {
2569 /* Move the current to the previous new areas */
2570 previous_new_areas = current_new_areas;
2571 previous_new_areas_index = current_new_areas_index;
2573 /* Scavenge all the areas in previous new areas. Any new areas
2574 * allocated are saved in current_new_areas. */
2576 /* Allocate an array for current_new_areas; alternating between
2577 * new_areas_1 and 2 */
2578 if (previous_new_areas == &new_areas_1)
2579 current_new_areas = &new_areas_2;
2580 else
2581 current_new_areas = &new_areas_1;
2583 /* Set up for gc_alloc(). */
2584 new_areas = current_new_areas;
2585 new_areas_index = 0;
2587 #ifdef LISP_FEATURE_IMMOBILE_SPACE
2588 scavenge_immobile_newspace();
2589 #endif
2590 /* Check whether previous_new_areas had overflowed. */
2591 if (previous_new_areas_index >= NUM_NEW_AREAS) {
2593 /* New areas of objects allocated have been lost so need to do a
2594 * full scan to be sure! If this becomes a problem try
2595 * increasing NUM_NEW_AREAS. */
2596 if (gencgc_verbose) {
2597 SHOW("new_areas overflow, doing full scavenge");
2600 /* Don't need to record new areas that get scavenged
2601 * anyway during scavenge_newspace_generation_one_scan. */
2602 record_new_objects = 1;
2604 scavenge_newspace_generation_one_scan(generation);
2606 /* Record all new areas now. */
2607 record_new_objects = 2;
2609 } else {
2611 /* Work through previous_new_areas. */
2612 for (i = 0; i < previous_new_areas_index; i++) {
2613 page_index_t page = (*previous_new_areas)[i].page;
2614 size_t offset = (*previous_new_areas)[i].offset;
2615 size_t size = (*previous_new_areas)[i].size;
2616 gc_assert(size % (2*N_WORD_BYTES) == 0);
2617 lispobj *start = (lispobj*)(page_address(page) + offset);
2618 heap_scavenge(start, (lispobj*)((char*)start + size));
2623 scav_weak_hash_tables(weak_ht_alivep_funs, gc_scav_pair);
2625 /* Flush the current regions updating the tables. */
2626 gc_alloc_update_all_page_tables(0);
2628 current_new_areas_index = new_areas_index;
2630 /*FSHOW((stderr,
2631 "The re-scan has finished; current_new_areas_index=%d.\n",
2632 current_new_areas_index));*/
2635 /* Turn off recording of areas allocated by gc_alloc(). */
2636 record_new_objects = 0;
2638 #ifdef SC_NS_GEN_CK
2640 page_index_t i;
2641 /* Check that none of the write_protected pages in this generation
2642 * have been written to. */
2643 for (i = 0; i < page_table_pages; i++) {
2644 if ((page_bytes_used(i) != 0)
2645 && (page_table[i].gen == generation)
2646 && (page_table[i].write_protected_cleared != 0)
2647 && (page_table[i].dont_move == 0)) {
2648 lose("write protected page %d written to in scavenge_newspace_generation\ngeneration=%d dont_move=%d\n",
2649 i, generation, page_table[i].dont_move);
2653 #endif
2656 /* Un-write-protect all the pages in from_space. This is done at the
2657 * start of a GC else there may be many page faults while scavenging
2658 * the newspace (I've seen drive the system time to 99%). These pages
2659 * would need to be unprotected anyway before unmapping in
2660 * free_oldspace; not sure what effect this has on paging.. */
2661 static void
2662 unprotect_oldspace(void)
2664 page_index_t i;
2665 char *region_addr = 0;
2666 char *page_addr = 0;
2667 uword_t region_bytes = 0;
2669 for (i = 0; i < last_free_page; i++) {
2670 if ((page_bytes_used(i) != 0)
2671 && (page_table[i].gen == from_space)) {
2673 /* Remove any write-protection. We should be able to rely
2674 * on the write-protect flag to avoid redundant calls. */
2675 if (page_table[i].write_protected) {
2676 page_table[i].write_protected = 0;
2677 page_addr = page_address(i);
2678 if (!region_addr) {
2679 /* First region. */
2680 region_addr = page_addr;
2681 region_bytes = GENCGC_CARD_BYTES;
2682 } else if (region_addr + region_bytes == page_addr) {
2683 /* Region continue. */
2684 region_bytes += GENCGC_CARD_BYTES;
2685 } else {
2686 /* Unprotect previous region. */
2687 os_protect(region_addr, region_bytes, OS_VM_PROT_ALL);
2688 /* First page in new region. */
2689 region_addr = page_addr;
2690 region_bytes = GENCGC_CARD_BYTES;
2695 if (region_addr) {
2696 /* Unprotect last region. */
2697 os_protect(region_addr, region_bytes, OS_VM_PROT_ALL);
2701 /* Work through all the pages and free any in from_space. This
2702 * assumes that all objects have been copied or promoted to an older
2703 * generation. Bytes_allocated and the generation bytes_allocated
2704 * counter are updated. The number of bytes freed is returned. */
2705 static uword_t
2706 free_oldspace(void)
2708 uword_t bytes_freed = 0;
2709 page_index_t first_page, last_page;
2711 first_page = 0;
2713 do {
2714 /* Find a first page for the next region of pages. */
2715 while ((first_page < last_free_page)
2716 && ((page_bytes_used(first_page) == 0)
2717 || (page_table[first_page].gen != from_space)))
2718 first_page++;
2720 if (first_page >= last_free_page)
2721 break;
2723 /* Find the last page of this region. */
2724 last_page = first_page;
2726 do {
2727 /* Free the page. */
2728 bytes_freed += page_bytes_used(last_page);
2729 generations[page_table[last_page].gen].bytes_allocated -=
2730 page_bytes_used(last_page);
2731 reset_page_flags(last_page);
2732 set_page_bytes_used(last_page, 0);
2733 /* Should already be unprotected by unprotect_oldspace(). */
2734 gc_assert(!page_table[last_page].write_protected);
2735 last_page++;
2737 while ((last_page < last_free_page)
2738 && (page_bytes_used(last_page) != 0)
2739 && (page_table[last_page].gen == from_space));
2741 #ifdef TRAVERSE_FREED_OBJECTS
2742 /* At this point we could attempt to recycle unused TLS indices
2743 * as follows: For each now-garbage symbol that had a nonzero index,
2744 * return that index to a "free TLS index" pool, perhaps a linked list
2745 * or bitmap. Then either always try the free pool first (for better
2746 * locality) or if ALLOC-TLS-INDEX detects exhaustion (for speed). */
2748 lispobj* where = (lispobj*)page_address(first_page);
2749 lispobj* end = (lispobj*)page_address(last_page);
2750 while (where < end) {
2751 lispobj word = *where;
2752 if (forwarding_pointer_p(where)) {
2753 word = *native_pointer(forwarding_pointer_value(where));
2754 where += OBJECT_SIZE(word,
2755 native_pointer(forwarding_pointer_value(where)));
2756 } else if (is_cons_half(word)) {
2757 // Print something maybe
2758 where += 2;
2759 } else {
2760 // Print something maybe
2761 where += sizetab[widetag_of(word)](where);
2765 #endif
2767 #ifdef READ_PROTECT_FREE_PAGES
2768 os_protect(page_address(first_page),
2769 npage_bytes(last_page-first_page),
2770 OS_VM_PROT_NONE);
2771 #endif
2772 first_page = last_page;
2773 } while (first_page < last_free_page);
2775 bytes_allocated -= bytes_freed;
2776 return bytes_freed;
2779 #if 0
2780 /* Print some information about a pointer at the given address. */
2781 static void
2782 print_ptr(lispobj *addr)
2784 /* If addr is in the dynamic space then out the page information. */
2785 page_index_t pi1 = find_page_index((void*)addr);
2787 if (pi1 != -1)
2788 fprintf(stderr," %p: page %d alloc %d gen %d bytes_used %d offset %lu dont_move %d\n",
2789 addr,
2790 pi1,
2791 page_table[pi1].allocated,
2792 page_table[pi1].gen,
2793 page_bytes_used(pi1),
2794 scan_start_offset(page_table[pi1]),
2795 page_table[pi1].dont_move);
2796 fprintf(stderr," %x %x %x %x (%x) %x %x %x %x\n",
2797 *(addr-4),
2798 *(addr-3),
2799 *(addr-2),
2800 *(addr-1),
2801 *(addr-0),
2802 *(addr+1),
2803 *(addr+2),
2804 *(addr+3),
2805 *(addr+4));
2807 #endif
2809 static int
2810 is_in_stack_space(lispobj ptr)
2812 /* For space verification: Pointers can be valid if they point
2813 * to a thread stack space. This would be faster if the thread
2814 * structures had page-table entries as if they were part of
2815 * the heap space. */
2816 /* Actually, no, how would that be faster?
2817 * If you have to examine thread structures, you have to examine
2818 * them all. This demands something like a binary search tree */
2819 struct thread *th;
2820 for_each_thread(th) {
2821 if ((th->control_stack_start <= (lispobj *)ptr) &&
2822 (th->control_stack_end >= (lispobj *)ptr)) {
2823 return 1;
2826 return 0;
2829 struct verify_state {
2830 lispobj *object_start, *object_end;
2831 lispobj *virtual_where;
2832 uword_t flags;
2833 int errors;
2834 generation_index_t object_gen;
2837 #define VERIFY_VERBOSE 1
2838 /* AGGRESSIVE = always call valid_lisp_pointer_p() on pointers.
2839 * Otherwise, do only a quick check that widetag/lowtag correspond */
2840 #define VERIFY_AGGRESSIVE 2
2841 /* VERIFYING_foo indicates internal state, not a caller's option */
2842 #define VERIFYING_HEAP_OBJECTS 8
2844 // NOTE: This function can produces false failure indications,
2845 // usually related to dynamic space pointing to the stack of a
2846 // dead thread, but there may be other reasons as well.
2847 static void
2848 verify_range(lispobj *where, sword_t nwords, struct verify_state *state)
2850 extern int valid_lisp_pointer_p(lispobj);
2851 boolean is_in_readonly_space =
2852 (READ_ONLY_SPACE_START <= (uword_t)where &&
2853 where < read_only_space_free_pointer);
2854 boolean is_in_immobile_space = immobile_space_p((lispobj)where);
2856 lispobj *end = where + nwords;
2857 size_t count;
2858 for ( ; where < end ; where += count) {
2859 // Keep track of object boundaries, unless verifying a non-heap space.
2860 if (where > state->object_end && (state->flags & VERIFYING_HEAP_OBJECTS)) {
2861 state->object_start = where;
2862 state->object_end = where + OBJECT_SIZE(*where, where) - 1;
2864 count = 1;
2865 lispobj thing = *where;
2866 lispobj callee;
2868 if (is_lisp_pointer(thing)) {
2869 page_index_t page_index = find_page_index((void*)thing);
2870 boolean to_immobile_space = immobile_space_p(thing);
2872 /* unlike lose(), fprintf detects format mismatch, hence the casts */
2873 #define FAIL_IF(what, why) if (what) { \
2874 if (++state->errors > 25) lose("Too many errors"); \
2875 else fprintf(stderr, "Ptr %p @ %"OBJ_FMTX" sees %s\n", \
2876 (void*)(uintptr_t)thing, \
2877 (lispobj)(state->virtual_where ? state->virtual_where : where), \
2878 why); }
2880 /* Does it point to the dynamic space? */
2881 if (page_index != -1) {
2882 /* If it's within the dynamic space it should point to a used page. */
2883 FAIL_IF(page_free_p(page_index), "free page");
2884 FAIL_IF(!(page_table[page_index].allocated & OPEN_REGION_PAGE_FLAG)
2885 && (thing & (GENCGC_CARD_BYTES-1)) >= page_bytes_used(page_index),
2886 "unallocated space");
2887 /* Check that it doesn't point to a forwarding pointer! */
2888 FAIL_IF(*native_pointer(thing) == 0x01, "forwarding ptr");
2889 /* Check that its not in the RO space as it would then be a
2890 * pointer from the RO to the dynamic space. */
2891 FAIL_IF(is_in_readonly_space, "dynamic space from RO space");
2892 } else if (to_immobile_space) {
2893 // the object pointed to must not have been discarded as garbage
2894 FAIL_IF(!other_immediate_lowtag_p(*native_pointer(thing)) ||
2895 filler_obj_p(native_pointer(thing)),
2896 "trashed object");
2898 /* Any pointer that points to non-static space is examined further.
2899 * You might think this should scan stacks first as a quick out,
2900 * but that would take time proportional to the number of threads. */
2901 if (page_index >= 0 || to_immobile_space) {
2902 int valid;
2903 /* If aggressive, or to/from immobile space, do a full search
2904 * (as entailed by valid_lisp_pointer_p) */
2905 if ((state->flags & VERIFY_AGGRESSIVE)
2906 || (is_in_immobile_space || to_immobile_space))
2907 valid = valid_lisp_pointer_p(thing);
2908 else {
2909 /* Efficiently decide whether 'thing' is plausible.
2910 * This MUST NOT use properly_tagged_descriptor_p() which
2911 * assumes a known good object base address, and would
2912 * "dangerously" scan a code component for embedded funs. */
2913 int lowtag = lowtag_of(thing);
2914 if (lowtag == LIST_POINTER_LOWTAG)
2915 valid = is_cons_half(CONS(thing)->car)
2916 && is_cons_half(CONS(thing)->cdr);
2917 else {
2918 lispobj word = *native_pointer(thing);
2919 valid = other_immediate_lowtag_p(word) &&
2920 lowtag_for_widetag[widetag_of(word)>>2] == lowtag;
2923 /* If 'thing' points to a stack, we can only hope that the frame
2924 * not clobbered, or the object at 'where' is unreachable. */
2925 FAIL_IF(!valid && !is_in_stack_space(thing), "junk");
2927 continue;
2929 int widetag = widetag_of(thing);
2930 if (is_lisp_immediate(thing) || widetag == NO_TLS_VALUE_MARKER_WIDETAG) {
2931 /* skip immediates */
2932 } else if (!(other_immediate_lowtag_p(widetag)
2933 && lowtag_for_widetag[widetag>>2])) {
2934 lose("Unhandled widetag %p at %p\n", widetag, where);
2935 } else if (unboxed_obj_widetag_p(widetag)) {
2936 count = sizetab[widetag](where);
2937 } else switch(widetag) {
2938 /* boxed or partially boxed objects */
2939 // FIXME: x86-64 can have partially unboxed FINs. The raw words
2940 // are at the moment valid fixnums by blind luck.
2941 case INSTANCE_WIDETAG:
2942 if (instance_layout(where)) {
2943 sword_t nslots = instance_length(thing) | 1;
2944 lispobj bitmap = LAYOUT(instance_layout(where))->bitmap;
2945 gc_assert(fixnump(bitmap)
2946 || widetag_of(*native_pointer(bitmap))==BIGNUM_WIDETAG);
2947 instance_scan((void (*)(lispobj*, sword_t, uword_t))verify_range,
2948 where+1, nslots, bitmap, (uintptr_t)state);
2949 count = 1 + nslots;
2951 break;
2952 case CODE_HEADER_WIDETAG:
2954 struct code *code = (struct code *) where;
2955 sword_t nheader_words = code_header_words(code->header);
2956 /* Scavenge the boxed section of the code data block */
2957 verify_range(where + 1, nheader_words - 1, state);
2959 /* Scavenge the boxed section of each function
2960 * object in the code data block. */
2961 for_each_simple_fun(i, fheaderp, code, 1, {
2962 #if defined(LISP_FEATURE_COMPACT_INSTANCE_HEADER)
2963 lispobj __attribute__((unused)) layout =
2964 function_layout((lispobj*)fheaderp);
2965 gc_assert(!layout || layout == SYMBOL(FUNCTION_LAYOUT)->value >> 32);
2966 #endif
2967 verify_range(SIMPLE_FUN_SCAV_START(fheaderp),
2968 SIMPLE_FUN_SCAV_NWORDS(fheaderp),
2969 state); });
2970 count = nheader_words + code_instruction_words(code->code_size);
2971 break;
2973 case FDEFN_WIDETAG:
2974 verify_range(where + 1, 2, state);
2975 callee = fdefn_callee_lispobj((struct fdefn*)where);
2976 /* For a more intelligible error, don't say that the word that
2977 * contains an errant pointer is in stack space if it isn't. */
2978 state->virtual_where = where + 3;
2979 verify_range(&callee, 1, state);
2980 state->virtual_where = 0;
2981 count = ALIGN_UP(sizeof (struct fdefn)/sizeof(lispobj), 2);
2982 break;
2986 static uword_t verify_space(lispobj start, lispobj* end, uword_t flags) {
2987 struct verify_state state;
2988 memset(&state, 0, sizeof state);
2989 state.flags = flags;
2990 verify_range((lispobj*)start, end-(lispobj*)start, &state);
2991 if (state.errors) lose("verify failed: %d error(s)", state.errors);
2992 return 0;
2994 static uword_t verify_gen_aux(lispobj start, lispobj* end, struct verify_state* state)
2996 verify_range((lispobj*)start, end-(lispobj*)start, state);
2997 return 0;
2999 static void verify_generation(generation_index_t generation, uword_t flags)
3001 struct verify_state state;
3002 memset(&state, 0, sizeof state);
3003 state.flags = flags;
3004 walk_generation((uword_t(*)(lispobj*,lispobj*,uword_t))verify_gen_aux,
3005 generation, (uword_t)&state);
3006 if (state.errors) lose("verify failed: %d error(s)", state.errors);
3009 void verify_gc(uword_t flags)
3011 int verbose = flags & VERIFY_VERBOSE;
3013 flags |= VERIFYING_HEAP_OBJECTS;
3015 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3016 # ifdef __linux__
3017 // Try this verification if immobile-space was compiled with extra debugging.
3018 // But weak symbols don't work on macOS.
3019 extern void __attribute__((weak)) check_varyobj_pages();
3020 if (&check_varyobj_pages) check_varyobj_pages();
3021 # endif
3022 if (verbose)
3023 printf("Verifying immobile space\n");
3024 verify_space(FIXEDOBJ_SPACE_START, fixedobj_free_pointer, flags);
3025 verify_space(VARYOBJ_SPACE_START, varyobj_free_pointer, flags);
3026 #endif
3027 struct thread *th;
3028 if (verbose)
3029 printf("Verifying binding stacks\n");
3030 for_each_thread(th) {
3031 verify_space((lispobj)th->binding_stack_start,
3032 (lispobj*)get_binding_stack_pointer(th),
3033 flags ^ VERIFYING_HEAP_OBJECTS);
3034 #ifdef LISP_FEATURE_SB_THREAD
3035 verify_space((lispobj)(th+1),
3036 (lispobj*)(SymbolValue(FREE_TLS_INDEX,0)
3037 + (char*)((union per_thread_data*)th)->dynamic_values),
3038 flags ^ VERIFYING_HEAP_OBJECTS);
3039 #endif
3041 if (verbose)
3042 printf("Verifying RO space\n");
3043 verify_space(READ_ONLY_SPACE_START, read_only_space_free_pointer, flags);
3044 if (verbose)
3045 printf("Verifying static space\n");
3046 verify_space(STATIC_SPACE_START, static_space_free_pointer, flags);
3047 if (verbose)
3048 printf("Verifying dynamic space\n");
3049 verify_generation(-1, flags);
3052 /* Call 'proc' with pairs of addresses demarcating ranges in the
3053 * specified generation.
3054 * Stop if any invocation returns non-zero, and return that value */
3055 uword_t
3056 walk_generation(uword_t (*proc)(lispobj*,lispobj*,uword_t),
3057 generation_index_t generation, uword_t extra)
3059 page_index_t i;
3060 int genmask = generation >= 0 ? 1 << generation : ~0;
3062 for (i = 0; i < last_free_page; i++) {
3063 if ((page_bytes_used(i) != 0) && ((1 << page_table[i].gen) & genmask)) {
3064 page_index_t last_page;
3066 /* This should be the start of a contiguous block */
3067 gc_assert(page_starts_contiguous_block_p(i));
3069 /* Need to find the full extent of this contiguous block in case
3070 objects span pages. */
3072 /* Now work forward until the end of this contiguous area is
3073 found. */
3074 for (last_page = i; ;last_page++)
3075 /* Check whether this is the last page in this contiguous
3076 * block. */
3077 if (page_ends_contiguous_block_p(last_page, page_table[i].gen))
3078 break;
3080 uword_t result =
3081 proc((lispobj*)page_address(i),
3082 (lispobj*)(page_bytes_used(last_page) + page_address(last_page)),
3083 extra);
3084 if (result) return result;
3086 i = last_page;
3089 return 0;
3092 /* Check that all the free space is zero filled. */
3093 static void
3094 verify_zero_fill(void)
3096 page_index_t page;
3098 for (page = 0; page < last_free_page; page++) {
3099 if (page_free_p(page)) {
3100 /* The whole page should be zero filled. */
3101 sword_t *start_addr = (sword_t *)page_address(page);
3102 sword_t i;
3103 for (i = 0; i < (sword_t)GENCGC_CARD_BYTES/N_WORD_BYTES; i++) {
3104 if (start_addr[i] != 0) {
3105 lose("free page not zero at %p\n", start_addr + i);
3108 } else {
3109 sword_t free_bytes = GENCGC_CARD_BYTES - page_bytes_used(page);
3110 if (free_bytes > 0) {
3111 sword_t *start_addr =
3112 (sword_t *)(page_address(page) + page_bytes_used(page));
3113 sword_t size = free_bytes / N_WORD_BYTES;
3114 sword_t i;
3115 for (i = 0; i < size; i++) {
3116 if (start_addr[i] != 0) {
3117 lose("free region not zero at %p\n", start_addr + i);
3125 /* External entry point for verify_zero_fill */
3126 void
3127 gencgc_verify_zero_fill(void)
3129 /* Flush the alloc regions updating the tables. */
3130 gc_alloc_update_all_page_tables(1);
3131 SHOW("verifying zero fill");
3132 verify_zero_fill();
3135 /* Write-protect all the dynamic boxed pages in the given generation. */
3136 static void
3137 write_protect_generation_pages(generation_index_t generation)
3139 page_index_t start;
3141 gc_assert(generation < SCRATCH_GENERATION);
3143 for (start = 0; start < last_free_page; start++) {
3144 if (protect_page_p(start, generation)) {
3145 void *page_start;
3146 page_index_t last;
3148 /* Note the page as protected in the page tables. */
3149 page_table[start].write_protected = 1;
3151 for (last = start + 1; last < last_free_page; last++) {
3152 if (!protect_page_p(last, generation))
3153 break;
3154 page_table[last].write_protected = 1;
3157 page_start = page_address(start);
3159 os_protect(page_start,
3160 npage_bytes(last - start),
3161 OS_VM_PROT_READ | OS_VM_PROT_EXECUTE);
3163 start = last;
3167 if (gencgc_verbose > 1) {
3168 FSHOW((stderr,
3169 "/write protected %d of %d pages in generation %d\n",
3170 count_write_protect_generation_pages(generation),
3171 count_generation_pages(generation),
3172 generation));
3176 #if !GENCGC_IS_PRECISE
3177 static void
3178 preserve_context_registers (void (*proc)(os_context_register_t), os_context_t *c)
3180 #ifdef LISP_FEATURE_SB_THREAD
3181 void **ptr;
3182 /* On Darwin the signal context isn't a contiguous block of memory,
3183 * so just preserve_pointering its contents won't be sufficient.
3185 #if defined(LISP_FEATURE_DARWIN)||defined(LISP_FEATURE_WIN32)
3186 #if defined LISP_FEATURE_X86
3187 proc(*os_context_register_addr(c,reg_EAX));
3188 proc(*os_context_register_addr(c,reg_ECX));
3189 proc(*os_context_register_addr(c,reg_EDX));
3190 proc(*os_context_register_addr(c,reg_EBX));
3191 proc(*os_context_register_addr(c,reg_ESI));
3192 proc(*os_context_register_addr(c,reg_EDI));
3193 proc(*os_context_pc_addr(c));
3194 #elif defined LISP_FEATURE_X86_64
3195 proc(*os_context_register_addr(c,reg_RAX));
3196 proc(*os_context_register_addr(c,reg_RCX));
3197 proc(*os_context_register_addr(c,reg_RDX));
3198 proc(*os_context_register_addr(c,reg_RBX));
3199 proc(*os_context_register_addr(c,reg_RSI));
3200 proc(*os_context_register_addr(c,reg_RDI));
3201 proc(*os_context_register_addr(c,reg_R8));
3202 proc(*os_context_register_addr(c,reg_R9));
3203 proc(*os_context_register_addr(c,reg_R10));
3204 proc(*os_context_register_addr(c,reg_R11));
3205 proc(*os_context_register_addr(c,reg_R12));
3206 proc(*os_context_register_addr(c,reg_R13));
3207 proc(*os_context_register_addr(c,reg_R14));
3208 proc(*os_context_register_addr(c,reg_R15));
3209 proc(*os_context_pc_addr(c));
3210 #else
3211 #error "preserve_context_registers needs to be tweaked for non-x86 Darwin"
3212 #endif
3213 #endif
3214 #if !defined(LISP_FEATURE_WIN32)
3215 for(ptr = ((void **)(c+1))-1; ptr>=(void **)c; ptr--) {
3216 proc((os_context_register_t)*ptr);
3218 #endif
3219 #endif // LISP_FEATURE_SB_THREAD
3221 #endif
3223 static void
3224 move_pinned_pages_to_newspace()
3226 page_index_t i;
3228 /* scavenge() will evacuate all oldspace pages, but no newspace
3229 * pages. Pinned pages are precisely those pages which must not
3230 * be evacuated, so move them to newspace directly. */
3232 for (i = 0; i < last_free_page; i++) {
3233 if (page_table[i].dont_move &&
3234 /* dont_move is cleared lazily, so test the 'gen' field as well. */
3235 page_table[i].gen == from_space) {
3236 if (page_table[i].has_pins) {
3237 // do not move to newspace after all, this will be word-wiped
3238 continue;
3240 page_table[i].gen = new_space;
3241 /* And since we're moving the pages wholesale, also adjust
3242 * the generation allocation counters. */
3243 int used = page_bytes_used(i);
3244 generations[new_space].bytes_allocated += used;
3245 generations[from_space].bytes_allocated -= used;
3250 #if defined(__GNUC__) && defined(ADDRESS_SANITIZER)
3251 #define NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address))
3252 #else
3253 #define NO_SANITIZE_ADDRESS
3254 #endif
3256 /* Garbage collect a generation. If raise is 0 then the remains of the
3257 * generation are not raised to the next generation. */
3258 static void NO_SANITIZE_ADDRESS
3259 garbage_collect_generation(generation_index_t generation, int raise)
3261 page_index_t i;
3262 struct thread *th;
3264 gc_assert(generation <= PSEUDO_STATIC_GENERATION);
3266 /* The oldest generation can't be raised. */
3267 gc_assert(!raise || generation < HIGHEST_NORMAL_GENERATION);
3269 /* Check that weak hash tables were processed in the previous GC. */
3270 gc_assert(weak_hash_tables == NULL);
3271 gc_assert(weak_AND_hash_tables == NULL);
3273 /* Initialize the weak pointer list. */
3274 weak_pointers = NULL;
3276 /* When a generation is not being raised it is transported to a
3277 * temporary generation (NUM_GENERATIONS), and lowered when
3278 * done. Set up this new generation. There should be no pages
3279 * allocated to it yet. */
3280 if (!raise) {
3281 gc_assert(generations[SCRATCH_GENERATION].bytes_allocated == 0);
3284 /* Set the global src and dest. generations */
3285 if (generation < PSEUDO_STATIC_GENERATION) {
3287 from_space = generation;
3288 if (raise)
3289 new_space = generation+1;
3290 else
3291 new_space = SCRATCH_GENERATION;
3293 /* Change to a new space for allocation, resetting the alloc_start_page */
3294 gc_alloc_generation = new_space;
3295 #if SEGREGATED_CODE
3296 bzero(generations[new_space].alloc_start_page_,
3297 sizeof generations[new_space].alloc_start_page_);
3298 #else
3299 generations[new_space].alloc_start_page = 0;
3300 generations[new_space].alloc_unboxed_start_page = 0;
3301 generations[new_space].alloc_large_start_page = 0;
3302 #endif
3304 #ifdef PIN_GRANULARITY_LISPOBJ
3305 hopscotch_reset(&pinned_objects);
3306 #endif
3307 /* Before any pointers are preserved, the dont_move flags on the
3308 * pages need to be cleared. */
3309 /* FIXME: consider moving this bitmap into its own range of words,
3310 * out of the page table. Then we can just bzero() it.
3311 * This will also obviate the extra test at the comment
3312 * "dont_move is cleared lazily" in move_pinned_pages_to_newspace().
3314 for (i = 0; i < last_free_page; i++)
3315 if(page_table[i].gen==from_space)
3316 page_table[i].dont_move = 0;
3318 /* Un-write-protect the old-space pages. This is essential for the
3319 * promoted pages as they may contain pointers into the old-space
3320 * which need to be scavenged. It also helps avoid unnecessary page
3321 * faults as forwarding pointers are written into them. They need to
3322 * be un-protected anyway before unmapping later. */
3323 if (ENABLE_PAGE_PROTECTION)
3324 unprotect_oldspace();
3326 } else { // "full" [sic] GC
3328 /* This is a full mark-and-sweep of all generations without compacting
3329 * and without returning free space to the allocator. The intent is to
3330 * break chains of objects causing accidental reachability.
3331 * Subsequent GC cycles will compact and reclaims space as usual. */
3332 from_space = new_space = -1;
3334 // Unprotect the dynamic space but leave page_table bits alone
3335 if (ENABLE_PAGE_PROTECTION)
3336 os_protect(page_address(0), npage_bytes(last_free_page),
3337 OS_VM_PROT_ALL);
3339 // Allocate pages from dynamic space for the work queue.
3340 extern void prepare_for_full_mark_phase();
3341 prepare_for_full_mark_phase();
3345 /* Scavenge the stacks' conservative roots. */
3347 /* there are potentially two stacks for each thread: the main
3348 * stack, which may contain Lisp pointers, and the alternate stack.
3349 * We don't ever run Lisp code on the altstack, but it may
3350 * host a sigcontext with lisp objects in it */
3352 /* what we need to do: (1) find the stack pointer for the main
3353 * stack; scavenge it (2) find the interrupt context on the
3354 * alternate stack that might contain lisp values, and scavenge
3355 * that */
3357 /* we assume that none of the preceding applies to the thread that
3358 * initiates GC. If you ever call GC from inside an altstack
3359 * handler, you will lose. */
3361 #if !GENCGC_IS_PRECISE
3362 /* And if we're saving a core, there's no point in being conservative. */
3363 if (conservative_stack) {
3364 for_each_thread(th) {
3365 void **ptr;
3366 void **esp=(void **)-1;
3367 if (th->state == STATE_DEAD)
3368 continue;
3369 # if defined(LISP_FEATURE_SB_SAFEPOINT)
3370 /* Conservative collect_garbage is always invoked with a
3371 * foreign C call or an interrupt handler on top of every
3372 * existing thread, so the stored SP in each thread
3373 * structure is valid, no matter which thread we are looking
3374 * at. For threads that were running Lisp code, the pitstop
3375 * and edge functions maintain this value within the
3376 * interrupt or exception handler. */
3377 esp = os_get_csp(th);
3378 assert_on_stack(th, esp);
3380 /* In addition to pointers on the stack, also preserve the
3381 * return PC, the only value from the context that we need
3382 * in addition to the SP. The return PC gets saved by the
3383 * foreign call wrapper, and removed from the control stack
3384 * into a register. */
3385 preserve_pointer(th->pc_around_foreign_call);
3387 /* And on platforms with interrupts: scavenge ctx registers. */
3389 /* Disabled on Windows, because it does not have an explicit
3390 * stack of `interrupt_contexts'. The reported CSP has been
3391 * chosen so that the current context on the stack is
3392 * covered by the stack scan. See also set_csp_from_context(). */
3393 # ifndef LISP_FEATURE_WIN32
3394 if (th != arch_os_get_current_thread()) {
3395 long k = fixnum_value(
3396 read_TLS(FREE_INTERRUPT_CONTEXT_INDEX,th));
3397 while (k > 0)
3398 preserve_context_registers((void(*)(os_context_register_t))preserve_pointer,
3399 th->interrupt_contexts[--k]);
3401 # endif
3402 # elif defined(LISP_FEATURE_SB_THREAD)
3403 sword_t i,free;
3404 if(th==arch_os_get_current_thread()) {
3405 /* Somebody is going to burn in hell for this, but casting
3406 * it in two steps shuts gcc up about strict aliasing. */
3407 esp = (void **)((void *)&raise);
3408 } else {
3409 void **esp1;
3410 free=fixnum_value(read_TLS(FREE_INTERRUPT_CONTEXT_INDEX,th));
3411 for(i=free-1;i>=0;i--) {
3412 os_context_t *c=th->interrupt_contexts[i];
3413 esp1 = (void **) *os_context_register_addr(c,reg_SP);
3414 if (esp1>=(void **)th->control_stack_start &&
3415 esp1<(void **)th->control_stack_end) {
3416 if(esp1<esp) esp=esp1;
3417 preserve_context_registers((void(*)(os_context_register_t))preserve_pointer,
3422 # else
3423 esp = (void **)((void *)&raise);
3424 # endif
3425 if (!esp || esp == (void*) -1)
3426 lose("garbage_collect: no SP known for thread %x (OS %x)",
3427 th, th->os_thread);
3428 for (ptr = ((void **)th->control_stack_end)-1; ptr >= esp; ptr--) {
3429 preserve_pointer(*ptr);
3433 #else
3434 /* Non-x86oid systems don't have "conservative roots" as such, but
3435 * the same mechanism is used for objects pinned for use by alien
3436 * code. */
3437 for_each_thread(th) {
3438 lispobj pin_list = read_TLS(PINNED_OBJECTS,th);
3439 while (pin_list != NIL) {
3440 preserve_pointer((void*)(CONS(pin_list)->car));
3441 pin_list = CONS(pin_list)->cdr;
3444 #endif
3446 #if QSHOW
3447 if (gencgc_verbose > 1) {
3448 sword_t num_dont_move_pages = count_dont_move_pages();
3449 fprintf(stderr,
3450 "/non-movable pages due to conservative pointers = %ld (%lu bytes)\n",
3451 num_dont_move_pages,
3452 npage_bytes(num_dont_move_pages));
3454 #endif
3456 /* Now that all of the pinned (dont_move) pages are known, and
3457 * before we start to scavenge (and thus relocate) objects,
3458 * relocate the pinned pages to newspace, so that the scavenger
3459 * will not attempt to relocate their contents. */
3460 if (compacting_p())
3461 move_pinned_pages_to_newspace();
3463 /* Scavenge all the rest of the roots. */
3465 #if GENCGC_IS_PRECISE
3467 * If not x86, we need to scavenge the interrupt context(s) and the
3468 * control stack.
3471 struct thread *th;
3472 for_each_thread(th) {
3473 scavenge_interrupt_contexts(th);
3474 scavenge_control_stack(th);
3477 # ifdef LISP_FEATURE_SB_SAFEPOINT
3478 /* In this case, scrub all stacks right here from the GCing thread
3479 * instead of doing what the comment below says. Suboptimal, but
3480 * easier. */
3481 for_each_thread(th)
3482 scrub_thread_control_stack(th);
3483 # else
3484 /* Scrub the unscavenged control stack space, so that we can't run
3485 * into any stale pointers in a later GC (this is done by the
3486 * stop-for-gc handler in the other threads). */
3487 scrub_control_stack();
3488 # endif
3490 #endif
3492 /* Scavenge the Lisp functions of the interrupt handlers, taking
3493 * care to avoid SIG_DFL and SIG_IGN. */
3494 for (i = 0; i < NSIG; i++) {
3495 union interrupt_handler handler = interrupt_handlers[i];
3496 if (!ARE_SAME_HANDLER(handler.c, SIG_IGN) &&
3497 !ARE_SAME_HANDLER(handler.c, SIG_DFL) &&
3498 is_lisp_pointer(handler.lisp)) {
3499 if (compacting_p())
3500 scavenge((lispobj *)(interrupt_handlers + i), 1);
3501 else
3502 gc_mark_obj(handler.lisp);
3505 /* Scavenge the binding stacks. */
3507 struct thread *th;
3508 for_each_thread(th) {
3509 scav_binding_stack((lispobj*)th->binding_stack_start,
3510 (lispobj*)get_binding_stack_pointer(th),
3511 compacting_p() ? 0 : gc_mark_obj);
3512 #ifdef LISP_FEATURE_SB_THREAD
3513 /* do the tls as well */
3514 sword_t len;
3515 len=(SymbolValue(FREE_TLS_INDEX,0) >> WORD_SHIFT) -
3516 (sizeof (struct thread))/(sizeof (lispobj));
3517 if (compacting_p())
3518 scavenge((lispobj *) (th+1), len);
3519 else
3520 gc_mark_range((lispobj *) (th+1), len);
3521 #endif
3525 if (!compacting_p()) {
3526 extern void execute_full_mark_phase();
3527 extern void execute_full_sweep_phase();
3528 execute_full_mark_phase();
3529 execute_full_sweep_phase();
3530 goto maybe_verify;
3533 /* Scavenge static space. */
3534 if (gencgc_verbose > 1) {
3535 FSHOW((stderr,
3536 "/scavenge static space: %d bytes\n",
3537 (uword_t)static_space_free_pointer - STATIC_SPACE_START));
3539 heap_scavenge((lispobj*)STATIC_SPACE_START, static_space_free_pointer);
3541 /* All generations but the generation being GCed need to be
3542 * scavenged. The new_space generation needs special handling as
3543 * objects may be moved in - it is handled separately below. */
3544 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3545 // SCRATCH_GENERATION is scavenged in immobile space
3546 // because pinned objects will already have had their generation
3547 // number reassigned to that generation if applicable.
3548 scavenge_immobile_roots(generation+1, SCRATCH_GENERATION);
3549 #endif
3550 scavenge_generations(generation+1, PSEUDO_STATIC_GENERATION);
3552 if (gc_object_watcher) scavenge(&gc_object_watcher, 1);
3553 scavenge_pinned_ranges();
3554 /* The Lisp start function is stored in the core header, not a static
3555 * symbol. It is passed to gc_and_save() in this C variable */
3556 if (lisp_init_function) scavenge(&lisp_init_function, 1);
3558 /* Finally scavenge the new_space generation. Keep going until no
3559 * more objects are moved into the new generation */
3560 scavenge_newspace_generation(new_space);
3562 /* FIXME: I tried reenabling this check when debugging unrelated
3563 * GC weirdness ca. sbcl-0.6.12.45, and it failed immediately.
3564 * Since the current GC code seems to work well, I'm guessing that
3565 * this debugging code is just stale, but I haven't tried to
3566 * figure it out. It should be figured out and then either made to
3567 * work or just deleted. */
3569 #define RESCAN_CHECK 0
3570 #if RESCAN_CHECK
3571 /* As a check re-scavenge the newspace once; no new objects should
3572 * be found. */
3574 os_vm_size_t old_bytes_allocated = bytes_allocated;
3575 os_vm_size_t bytes_allocated;
3577 /* Start with a full scavenge. */
3578 scavenge_newspace_generation_one_scan(new_space);
3580 /* Flush the current regions, updating the tables. */
3581 gc_alloc_update_all_page_tables(1);
3583 bytes_allocated = bytes_allocated - old_bytes_allocated;
3585 if (bytes_allocated != 0) {
3586 lose("Rescan of new_space allocated %d more bytes.\n",
3587 bytes_allocated);
3590 #endif
3592 scan_binding_stack();
3593 scan_weak_hash_tables(weak_ht_alivep_funs);
3594 scan_weak_pointers();
3595 wipe_nonpinned_words();
3596 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3597 // Do this last, because until wipe_nonpinned_words() happens,
3598 // not all page table entries have the 'gen' value updated,
3599 // which we need to correctly find all old->young pointers.
3600 sweep_immobile_space(raise);
3601 #endif
3603 gc_assert(boxed_region.last_page < 0);
3604 gc_assert(unboxed_region.last_page < 0);
3605 #if SEGREGATED_CODE
3606 gc_assert(gc_alloc_region[2].last_page < 0);
3607 #endif
3608 #ifdef PIN_GRANULARITY_LISPOBJ
3609 hopscotch_log_stats(&pinned_objects, "pins");
3610 #endif
3612 /* Free the pages in oldspace, but not those marked dont_move. */
3613 free_oldspace();
3615 /* If the GC is not raising the age then lower the generation back
3616 * to its normal generation number */
3617 if (!raise) {
3618 for (i = 0; i < last_free_page; i++)
3619 if ((page_bytes_used(i) != 0)
3620 && (page_table[i].gen == SCRATCH_GENERATION))
3621 page_table[i].gen = generation;
3622 gc_assert(generations[generation].bytes_allocated == 0);
3623 generations[generation].bytes_allocated =
3624 generations[SCRATCH_GENERATION].bytes_allocated;
3625 generations[SCRATCH_GENERATION].bytes_allocated = 0;
3628 /* Reset the alloc_start_page for generation. */
3629 #if SEGREGATED_CODE
3630 bzero(generations[generation].alloc_start_page_,
3631 sizeof generations[generation].alloc_start_page_);
3632 #else
3633 generations[generation].alloc_start_page = 0;
3634 generations[generation].alloc_unboxed_start_page = 0;
3635 generations[generation].alloc_large_start_page = 0;
3636 #endif
3638 /* Set the new gc trigger for the GCed generation. */
3639 generations[generation].gc_trigger =
3640 generations[generation].bytes_allocated
3641 + generations[generation].bytes_consed_between_gc;
3643 if (raise)
3644 generations[generation].num_gc = 0;
3645 else
3646 ++generations[generation].num_gc;
3648 maybe_verify:
3649 if (generation >= verify_gens) {
3650 if (gencgc_verbose) {
3651 SHOW("verifying");
3653 verify_gc(0);
3657 page_index_t
3658 find_last_free_page(void)
3660 page_index_t last_page = -1, i;
3662 for (i = 0; i < last_free_page; i++)
3663 if (page_bytes_used(i) != 0)
3664 last_page = i;
3666 /* The last free page is actually the first available page */
3667 return last_page + 1;
3670 void
3671 update_dynamic_space_free_pointer(void)
3673 set_alloc_pointer((lispobj)(page_address(find_last_free_page())));
3676 static void
3677 remap_page_range (page_index_t from, page_index_t to)
3679 /* There's a mysterious Solaris/x86 problem with using mmap
3680 * tricks for memory zeroing. See sbcl-devel thread
3681 * "Re: patch: standalone executable redux".
3683 #if defined(LISP_FEATURE_SUNOS)
3684 zero_and_mark_pages(from, to);
3685 #else
3686 const page_index_t
3687 release_granularity = gencgc_release_granularity/GENCGC_CARD_BYTES,
3688 release_mask = release_granularity-1,
3689 end = to+1,
3690 aligned_from = (from+release_mask)&~release_mask,
3691 aligned_end = (end&~release_mask);
3693 if (aligned_from < aligned_end) {
3694 zero_pages_with_mmap(aligned_from, aligned_end-1);
3695 if (aligned_from != from)
3696 zero_and_mark_pages(from, aligned_from-1);
3697 if (aligned_end != end)
3698 zero_and_mark_pages(aligned_end, end-1);
3699 } else {
3700 zero_and_mark_pages(from, to);
3702 #endif
3705 static void
3706 remap_free_pages (page_index_t from, page_index_t to)
3708 page_index_t first_page, last_page;
3710 for (first_page = from; first_page <= to; first_page++) {
3711 if (!page_free_p(first_page) || !page_need_to_zero(first_page))
3712 continue;
3714 last_page = first_page + 1;
3715 while (page_free_p(last_page) &&
3716 (last_page <= to) &&
3717 (page_need_to_zero(last_page)))
3718 last_page++;
3720 remap_page_range(first_page, last_page-1);
3722 first_page = last_page;
3726 generation_index_t small_generation_limit = 1;
3728 /* GC all generations newer than last_gen, raising the objects in each
3729 * to the next older generation - we finish when all generations below
3730 * last_gen are empty. Then if last_gen is due for a GC, or if
3731 * last_gen==NUM_GENERATIONS (the scratch generation? eh?) we GC that
3732 * too. The valid range for last_gen is: 0,1,...,NUM_GENERATIONS.
3734 * We stop collecting at gencgc_oldest_gen_to_gc, even if this is less than
3735 * last_gen (oh, and note that by default it is NUM_GENERATIONS-1) */
3736 void
3737 collect_garbage(generation_index_t last_gen)
3739 generation_index_t gen = 0, i;
3740 boolean gc_mark_only = 0;
3741 int raise, more = 0;
3742 int gen_to_wp;
3743 /* The largest value of last_free_page seen since the time
3744 * remap_free_pages was called. */
3745 static page_index_t high_water_mark = 0;
3747 FSHOW((stderr, "/entering collect_garbage(%d)\n", last_gen));
3748 log_generation_stats(gc_logfile, "=== GC Start ===");
3750 gc_active_p = 1;
3752 if (last_gen == 1+PSEUDO_STATIC_GENERATION) {
3753 // Pseudostatic space undergoes a non-moving collection
3754 last_gen = PSEUDO_STATIC_GENERATION;
3755 gc_mark_only = 1;
3756 } else if (last_gen > 1+PSEUDO_STATIC_GENERATION) {
3757 // This is a completely non-obvious thing to do, but whatever...
3758 FSHOW((stderr,
3759 "/collect_garbage: last_gen = %d, doing a level 0 GC\n",
3760 last_gen));
3761 last_gen = 0;
3764 /* Flush the alloc regions updating the tables. */
3765 gc_alloc_update_all_page_tables(1);
3767 /* Verify the new objects created by Lisp code. */
3768 if (pre_verify_gen_0) {
3769 FSHOW((stderr, "pre-checking generation 0\n"));
3770 verify_generation(0, 0);
3773 if (gencgc_verbose > 1)
3774 print_generation_stats();
3776 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3777 /* Immobile space generation bits are lazily updated for gen0
3778 (not touched on every object allocation) so do it now */
3779 update_immobile_nursery_bits();
3780 #endif
3782 if (gc_mark_only) {
3783 garbage_collect_generation(PSEUDO_STATIC_GENERATION, 0);
3784 goto finish;
3787 do {
3788 /* Collect the generation. */
3790 if (more || (gen >= gencgc_oldest_gen_to_gc)) {
3791 /* Never raise the oldest generation. Never raise the extra generation
3792 * collected due to more-flag. */
3793 raise = 0;
3794 more = 0;
3795 } else {
3796 raise =
3797 (gen < last_gen)
3798 || (generations[gen].num_gc >= generations[gen].number_of_gcs_before_promotion);
3799 /* If we would not normally raise this one, but we're
3800 * running low on space in comparison to the object-sizes
3801 * we've been seeing, raise it and collect the next one
3802 * too. */
3803 if (!raise && gen == last_gen) {
3804 more = (2*large_allocation) >= (dynamic_space_size - bytes_allocated);
3805 raise = more;
3809 if (gencgc_verbose > 1) {
3810 FSHOW((stderr,
3811 "starting GC of generation %d with raise=%d alloc=%d trig=%d GCs=%d\n",
3812 gen,
3813 raise,
3814 generations[gen].bytes_allocated,
3815 generations[gen].gc_trigger,
3816 generations[gen].num_gc));
3819 /* If an older generation is being filled, then update its
3820 * memory age. */
3821 if (raise == 1) {
3822 generations[gen+1].cum_sum_bytes_allocated +=
3823 generations[gen+1].bytes_allocated;
3826 garbage_collect_generation(gen, raise);
3828 /* Reset the memory age cum_sum. */
3829 generations[gen].cum_sum_bytes_allocated = 0;
3831 if (gencgc_verbose > 1) {
3832 FSHOW((stderr, "GC of generation %d finished:\n", gen));
3833 print_generation_stats();
3836 gen++;
3837 } while ((gen <= gencgc_oldest_gen_to_gc)
3838 && ((gen < last_gen)
3839 || more
3840 || (raise
3841 && (generations[gen].bytes_allocated
3842 > generations[gen].gc_trigger)
3843 && (generation_average_age(gen)
3844 > generations[gen].minimum_age_before_gc))));
3846 /* Now if gen-1 was raised all generations before gen are empty.
3847 * If it wasn't raised then all generations before gen-1 are empty.
3849 * Now objects within this gen's pages cannot point to younger
3850 * generations unless they are written to. This can be exploited
3851 * by write-protecting the pages of gen; then when younger
3852 * generations are GCed only the pages which have been written
3853 * need scanning. */
3854 if (raise)
3855 gen_to_wp = gen;
3856 else
3857 gen_to_wp = gen - 1;
3859 /* There's not much point in WPing pages in generation 0 as it is
3860 * never scavenged (except promoted pages). */
3861 if ((gen_to_wp > 0) && ENABLE_PAGE_PROTECTION) {
3862 /* Check that they are all empty. */
3863 for (i = 0; i < gen_to_wp; i++) {
3864 if (generations[i].bytes_allocated)
3865 lose("trying to write-protect gen. %d when gen. %d nonempty\n",
3866 gen_to_wp, i);
3868 write_protect_generation_pages(gen_to_wp);
3871 /* Set gc_alloc() back to generation 0. The current regions should
3872 * be flushed after the above GCs. */
3873 gc_assert(boxed_region.free_pointer == boxed_region.start_addr);
3874 gc_alloc_generation = 0;
3876 /* Save the high-water mark before updating last_free_page */
3877 if (last_free_page > high_water_mark)
3878 high_water_mark = last_free_page;
3880 update_dynamic_space_free_pointer();
3882 /* Update auto_gc_trigger. Make sure we trigger the next GC before
3883 * running out of heap! */
3884 if (bytes_consed_between_gcs <= (dynamic_space_size - bytes_allocated))
3885 auto_gc_trigger = bytes_allocated + bytes_consed_between_gcs;
3886 else
3887 auto_gc_trigger = bytes_allocated + (dynamic_space_size - bytes_allocated)/2;
3889 if(gencgc_verbose) {
3890 #define MESSAGE ("Next gc when %"OS_VM_SIZE_FMT" bytes have been consed\n")
3891 char buf[64];
3892 int n;
3893 // fprintf() can - and does - cause deadlock here.
3894 // snprintf() seems to work fine.
3895 n = snprintf(buf, sizeof buf, MESSAGE, auto_gc_trigger);
3896 ignore_value(write(2, buf, n));
3897 #undef MESSAGE
3900 /* If we did a big GC (arbitrarily defined as gen > 1), release memory
3901 * back to the OS.
3903 if (gen > small_generation_limit) {
3904 if (last_free_page > high_water_mark)
3905 high_water_mark = last_free_page;
3906 remap_free_pages(0, high_water_mark);
3907 high_water_mark = 0;
3910 large_allocation = 0;
3911 finish:
3912 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3913 write_protect_immobile_space();
3914 #endif
3915 gc_active_p = 0;
3917 if (gc_object_watcher) {
3918 extern void gc_prove_liveness(void(*)(), lispobj, int, uword_t*, int);
3919 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
3920 gc_prove_liveness(preserve_context_registers,
3921 gc_object_watcher,
3922 gc_n_stack_pins, pinned_objects.keys,
3923 gc_traceroot_criterion);
3924 #else
3925 gc_prove_liveness(0, gc_object_watcher, 0, 0, gc_traceroot_criterion);
3926 #endif
3929 log_generation_stats(gc_logfile, "=== GC End ===");
3930 SHOW("returning from collect_garbage");
3933 /* Initialization of gencgc metadata is split into three steps:
3934 * 1. gc_init() - allocation of a fixed-address space via mmap(),
3935 * failing which there's no reason to go on. (safepoint only)
3936 * 2. gc_allocate_ptes() - page table entries
3937 * 3. gencgc_pickup_dynamic() - calculation of scan start offsets
3938 * Steps (2) and (3) are combined in self-build because there is
3939 * no PAGE_TABLE_CORE_ENTRY_TYPE_CODE core entry. */
3940 void
3941 gc_init(void)
3943 #if defined(LISP_FEATURE_SB_SAFEPOINT)
3944 alloc_gc_page();
3945 #endif
3946 // Verify that foo_BIT constants agree with the C compiler's bit packing
3947 // and that we can compute the correct adddress of the bitfields.
3948 // These tests can be optimized out of the emitted code by a good compiler.
3949 struct page test;
3950 unsigned char *pflagbits = (unsigned char*)&test.gen - 1;
3951 memset(&test, 0, sizeof test);
3952 *pflagbits = WRITE_PROTECTED_BIT;
3953 gc_assert(test.write_protected);
3954 *pflagbits = WP_CLEARED_BIT;
3955 gc_assert(test.write_protected_cleared);
3958 void gc_allocate_ptes()
3960 page_index_t i;
3962 /* Compute the number of pages needed for the dynamic space.
3963 * Dynamic space size should be aligned on page size. */
3964 page_table_pages = dynamic_space_size/GENCGC_CARD_BYTES;
3965 gc_assert(dynamic_space_size == npage_bytes(page_table_pages));
3967 /* Default nursery size to 5% of the total dynamic space size,
3968 * min 1Mb. */
3969 bytes_consed_between_gcs = dynamic_space_size/(os_vm_size_t)20;
3970 if (bytes_consed_between_gcs < (1024*1024))
3971 bytes_consed_between_gcs = 1024*1024;
3973 /* The page_table is allocated using "calloc" to zero-initialize it.
3974 * The C library typically implements this efficiently with mmap() if the
3975 * size is large enough. To further avoid touching each page structure
3976 * until first use, FREE_PAGE_FLAG must be 0, statically asserted here:
3979 /* Compile time assertion: If triggered, declares an array
3980 * of dimension -1 forcing a syntax error. The intent of the
3981 * assignment is to avoid an "unused variable" warning. */
3982 char __attribute__((unused)) assert_free_page_flag_0[(FREE_PAGE_FLAG) ? -1 : 1];
3984 /* An extra struct exists as the end as a sentinel. Its 'scan_start_offset'
3985 * and 'bytes_used' must be zero.
3986 * Doing so avoids testing in page_ends_contiguous_block_p() whether the
3987 * next page_index is within bounds, and whether that page contains data.
3989 page_table = calloc(1+page_table_pages, sizeof(struct page));
3990 gc_assert(page_table);
3992 hopscotch_init();
3993 #ifdef PIN_GRANULARITY_LISPOBJ
3994 hopscotch_create(&pinned_objects, HOPSCOTCH_HASH_FUN_DEFAULT, 0 /* hashset */,
3995 32 /* logical bin count */, 0 /* default range */);
3996 #endif
3998 scavtab[WEAK_POINTER_WIDETAG] = scav_weak_pointer;
4000 bytes_allocated = 0;
4002 /* Initialize the generations. */
4003 for (i = 0; i < NUM_GENERATIONS; i++) {
4004 generations[i].alloc_start_page = 0;
4005 generations[i].alloc_unboxed_start_page = 0;
4006 generations[i].alloc_large_start_page = 0;
4007 generations[i].bytes_allocated = 0;
4008 generations[i].gc_trigger = 2000000;
4009 generations[i].num_gc = 0;
4010 generations[i].cum_sum_bytes_allocated = 0;
4011 /* the tune-able parameters */
4012 generations[i].bytes_consed_between_gc
4013 = bytes_consed_between_gcs/(os_vm_size_t)HIGHEST_NORMAL_GENERATION;
4014 generations[i].number_of_gcs_before_promotion = 1;
4015 generations[i].minimum_age_before_gc = 0.75;
4018 /* Initialize gc_alloc. */
4019 gc_alloc_generation = 0;
4020 gc_set_region_empty(&boxed_region);
4021 gc_set_region_empty(&unboxed_region);
4022 #if SEGREGATED_CODE
4023 gc_set_region_empty(&code_region);
4024 #endif
4026 last_free_page = 0;
4029 /* Pick up the dynamic space from after a core load.
4031 * The ALLOCATION_POINTER points to the end of the dynamic space.
4034 static void
4035 gencgc_pickup_dynamic(void)
4037 page_index_t page = 0;
4038 char *alloc_ptr = (char *)get_alloc_pointer();
4039 lispobj *prev=(lispobj *)page_address(page);
4040 generation_index_t gen = PSEUDO_STATIC_GENERATION;
4042 bytes_allocated = 0;
4044 do {
4045 lispobj *first,*ptr= (lispobj *)page_address(page);
4047 if (!gencgc_partial_pickup || !page_free_p(page)) {
4048 page_bytes_t bytes_used = GENCGC_CARD_BYTES;
4049 /* It is possible, though rare, for the saved page table
4050 * to contain free pages below alloc_ptr. */
4051 page_table[page].gen = gen;
4052 if (gencgc_partial_pickup)
4053 bytes_used = page_bytes_used(page);
4054 else
4055 set_page_bytes_used(page, GENCGC_CARD_BYTES);
4056 page_table[page].large_object = 0;
4057 page_table[page].write_protected = 0;
4058 page_table[page].write_protected_cleared = 0;
4059 page_table[page].dont_move = 0;
4060 set_page_need_to_zero(page, 1);
4062 bytes_allocated += bytes_used;
4065 if (!gencgc_partial_pickup) {
4066 #if SEGREGATED_CODE
4067 // Make the most general assumption: any page *might* contain code.
4068 page_table[page].allocated = CODE_PAGE_FLAG;
4069 #else
4070 page_table[page].allocated = BOXED_PAGE_FLAG;
4071 #endif
4072 first = gc_search_space3(ptr, prev, (ptr+2));
4073 if(ptr == first)
4074 prev=ptr;
4075 set_page_scan_start_offset(page, page_address(page) - (char*)prev);
4077 page++;
4078 } while (page_address(page) < alloc_ptr);
4080 last_free_page = page;
4082 generations[gen].bytes_allocated = bytes_allocated;
4084 gc_alloc_update_all_page_tables(1);
4085 if (ENABLE_PAGE_PROTECTION)
4086 write_protect_generation_pages(gen);
4089 void
4090 gc_initialize_pointers(void)
4092 /* !page_table_pages happens once only in self-build and not again */
4093 if (!page_table_pages)
4094 gc_allocate_ptes();
4095 gencgc_pickup_dynamic();
4099 /* alloc(..) is the external interface for memory allocation. It
4100 * allocates to generation 0. It is not called from within the garbage
4101 * collector as it is only external uses that need the check for heap
4102 * size (GC trigger) and to disable the interrupts (interrupts are
4103 * always disabled during a GC).
4105 * The vops that call alloc(..) assume that the returned space is zero-filled.
4106 * (E.g. the most significant word of a 2-word bignum in MOVE-FROM-UNSIGNED.)
4108 * The check for a GC trigger is only performed when the current
4109 * region is full, so in most cases it's not needed. */
4111 static inline lispobj *
4112 general_alloc_internal(sword_t nbytes, int page_type_flag, struct alloc_region *region,
4113 struct thread *thread)
4115 #ifndef LISP_FEATURE_WIN32
4116 lispobj alloc_signal;
4117 #endif
4118 void *new_obj;
4119 void *new_free_pointer;
4120 os_vm_size_t trigger_bytes = 0;
4122 gc_assert(nbytes > 0);
4124 /* Check for alignment allocation problems. */
4125 gc_assert((((uword_t)region->free_pointer & LOWTAG_MASK) == 0)
4126 && ((nbytes & LOWTAG_MASK) == 0));
4128 #if !(defined(LISP_FEATURE_WIN32) && defined(LISP_FEATURE_SB_THREAD))
4129 /* Must be inside a PA section. */
4130 gc_assert(get_pseudo_atomic_atomic(thread));
4131 #endif
4133 if ((os_vm_size_t) nbytes > large_allocation)
4134 large_allocation = nbytes;
4136 /* maybe we can do this quickly ... */
4137 new_free_pointer = (char*)region->free_pointer + nbytes;
4138 if (new_free_pointer <= region->end_addr) {
4139 new_obj = (void*)(region->free_pointer);
4140 region->free_pointer = new_free_pointer;
4141 return(new_obj); /* yup */
4144 /* We don't want to count nbytes against auto_gc_trigger unless we
4145 * have to: it speeds up the tenuring of objects and slows down
4146 * allocation. However, unless we do so when allocating _very_
4147 * large objects we are in danger of exhausting the heap without
4148 * running sufficient GCs.
4150 if ((os_vm_size_t) nbytes >= bytes_consed_between_gcs)
4151 trigger_bytes = nbytes;
4153 /* we have to go the long way around, it seems. Check whether we
4154 * should GC in the near future
4156 if (auto_gc_trigger && (bytes_allocated+trigger_bytes > auto_gc_trigger)) {
4157 /* Don't flood the system with interrupts if the need to gc is
4158 * already noted. This can happen for example when SUB-GC
4159 * allocates or after a gc triggered in a WITHOUT-GCING. */
4160 if (read_TLS(GC_PENDING,thread) == NIL) {
4161 /* set things up so that GC happens when we finish the PA
4162 * section */
4163 write_TLS(GC_PENDING,T,thread);
4164 if (read_TLS(GC_INHIBIT,thread) == NIL) {
4165 #ifdef LISP_FEATURE_SB_SAFEPOINT
4166 thread_register_gc_trigger();
4167 #else
4168 set_pseudo_atomic_interrupted(thread);
4169 #if GENCGC_IS_PRECISE
4170 /* PPC calls alloc() from a trap
4171 * look up the most context if it's from a trap. */
4173 os_context_t *context =
4174 thread->interrupt_data->allocation_trap_context;
4175 maybe_save_gc_mask_and_block_deferrables
4176 (context ? os_context_sigmask_addr(context) : NULL);
4178 #else
4179 maybe_save_gc_mask_and_block_deferrables(NULL);
4180 #endif
4181 #endif
4185 new_obj = gc_alloc_with_region(nbytes, page_type_flag, region, 0);
4187 #ifndef LISP_FEATURE_WIN32
4188 /* for sb-prof, and not supported on Windows yet */
4189 alloc_signal = read_TLS(ALLOC_SIGNAL,thread);
4190 if ((alloc_signal & FIXNUM_TAG_MASK) == 0) {
4191 if ((sword_t) alloc_signal <= 0) {
4192 write_TLS(ALLOC_SIGNAL, T, thread);
4193 raise(SIGPROF);
4194 } else {
4195 write_TLS(ALLOC_SIGNAL,
4196 alloc_signal - (1 << N_FIXNUM_TAG_BITS),
4197 thread);
4200 #endif
4202 return (new_obj);
4205 lispobj *
4206 general_alloc(sword_t nbytes, int page_type_flag)
4208 struct thread *thread = arch_os_get_current_thread();
4209 /* Select correct region, and call general_alloc_internal with it.
4210 * For other then boxed allocation we must lock first, since the
4211 * region is shared. */
4212 #if SEGREGATED_CODE
4213 if (page_type_flag == BOXED_PAGE_FLAG) {
4214 #else
4215 if (BOXED_PAGE_FLAG & page_type_flag) {
4216 #endif
4217 #ifdef LISP_FEATURE_SB_THREAD
4218 struct alloc_region *region = (thread ? &(thread->alloc_region) : &boxed_region);
4219 #else
4220 struct alloc_region *region = &boxed_region;
4221 #endif
4222 return general_alloc_internal(nbytes, page_type_flag, region, thread);
4223 #if SEGREGATED_CODE
4224 } else if (page_type_flag == UNBOXED_PAGE_FLAG ||
4225 page_type_flag == CODE_PAGE_FLAG) {
4226 struct alloc_region *region =
4227 page_type_flag == CODE_PAGE_FLAG ? &code_region : &unboxed_region;
4228 #else
4229 } else if (UNBOXED_PAGE_FLAG == page_type_flag) {
4230 struct alloc_region *region = &unboxed_region;
4231 #endif
4232 lispobj * obj;
4233 int result;
4234 result = thread_mutex_lock(&allocation_lock);
4235 gc_assert(!result);
4236 obj = general_alloc_internal(nbytes, page_type_flag, region, thread);
4237 result = thread_mutex_unlock(&allocation_lock);
4238 gc_assert(!result);
4239 return obj;
4240 } else {
4241 lose("bad page type flag: %d", page_type_flag);
4245 lispobj AMD64_SYSV_ABI *
4246 alloc(sword_t nbytes)
4248 #ifdef LISP_FEATURE_SB_SAFEPOINT_STRICTLY
4249 struct thread *self = arch_os_get_current_thread();
4250 int was_pseudo_atomic = get_pseudo_atomic_atomic(self);
4251 if (!was_pseudo_atomic)
4252 set_pseudo_atomic_atomic(self);
4253 #else
4254 gc_assert(get_pseudo_atomic_atomic(arch_os_get_current_thread()));
4255 #endif
4257 lispobj *result = general_alloc(nbytes, BOXED_PAGE_FLAG);
4259 #ifdef LISP_FEATURE_SB_SAFEPOINT_STRICTLY
4260 if (!was_pseudo_atomic)
4261 clear_pseudo_atomic_atomic(self);
4262 #endif
4264 return result;
4268 * shared support for the OS-dependent signal handlers which
4269 * catch GENCGC-related write-protect violations
4271 void unhandled_sigmemoryfault(void* addr);
4273 /* Depending on which OS we're running under, different signals might
4274 * be raised for a violation of write protection in the heap. This
4275 * function factors out the common generational GC magic which needs
4276 * to invoked in this case, and should be called from whatever signal
4277 * handler is appropriate for the OS we're running under.
4279 * Return true if this signal is a normal generational GC thing that
4280 * we were able to handle, or false if it was abnormal and control
4281 * should fall through to the general SIGSEGV/SIGBUS/whatever logic.
4283 * We have two control flags for this: one causes us to ignore faults
4284 * on unprotected pages completely, and the second complains to stderr
4285 * but allows us to continue without losing.
4287 extern boolean ignore_memoryfaults_on_unprotected_pages;
4288 boolean ignore_memoryfaults_on_unprotected_pages = 0;
4290 extern boolean continue_after_memoryfault_on_unprotected_pages;
4291 boolean continue_after_memoryfault_on_unprotected_pages = 0;
4294 gencgc_handle_wp_violation(void* fault_addr)
4296 page_index_t page_index = find_page_index(fault_addr);
4298 #if QSHOW_SIGNALS
4299 FSHOW((stderr,
4300 "heap WP violation? fault_addr=%p, page_index=%"PAGE_INDEX_FMT"\n",
4301 fault_addr, page_index));
4302 #endif
4304 /* Check whether the fault is within the dynamic space. */
4305 if (page_index == (-1)) {
4306 #ifdef LISP_FEATURE_IMMOBILE_SPACE
4307 extern int immobile_space_handle_wp_violation(void*);
4308 if (immobile_space_handle_wp_violation(fault_addr))
4309 return 1;
4310 #endif
4312 /* It can be helpful to be able to put a breakpoint on this
4313 * case to help diagnose low-level problems. */
4314 unhandled_sigmemoryfault(fault_addr);
4316 /* not within the dynamic space -- not our responsibility */
4317 return 0;
4319 } else {
4320 unsigned char *pflagbits = (unsigned char*)&page_table[page_index].gen - 1;
4321 unsigned char flagbits = __sync_fetch_and_add(pflagbits, 0);
4322 if (flagbits & WRITE_PROTECTED_BIT) {
4323 unprotect_page_index(page_index);
4324 } else if (!ignore_memoryfaults_on_unprotected_pages) {
4325 /* The only acceptable reason for this signal on a heap
4326 * access is that GENCGC write-protected the page.
4327 * However, if two CPUs hit a wp page near-simultaneously,
4328 * we had better not have the second one lose here if it
4329 * does this test after the first one has already set wp=0
4331 if (!(flagbits & WP_CLEARED_BIT)) {
4332 void lisp_backtrace(int frames);
4333 lisp_backtrace(10);
4334 fprintf(stderr,
4335 "Fault @ %p, page %"PAGE_INDEX_FMT" not marked as write-protected:\n"
4336 " boxed_region.first_page: %"PAGE_INDEX_FMT","
4337 " boxed_region.last_page %"PAGE_INDEX_FMT"\n"
4338 " page.scan_start_offset: %"OS_VM_SIZE_FMT"\n"
4339 " page.bytes_used: %u\n"
4340 " page.allocated: %d\n"
4341 " page.write_protected: %d\n"
4342 " page.write_protected_cleared: %d\n"
4343 " page.generation: %d\n",
4344 fault_addr,
4345 page_index,
4346 boxed_region.first_page,
4347 boxed_region.last_page,
4348 page_scan_start_offset(page_index),
4349 page_bytes_used(page_index),
4350 page_table[page_index].allocated,
4351 page_table[page_index].write_protected,
4352 page_table[page_index].write_protected_cleared,
4353 page_table[page_index].gen);
4354 if (!continue_after_memoryfault_on_unprotected_pages)
4355 lose("Feh.\n");
4358 /* Don't worry, we can handle it. */
4359 return 1;
4362 /* This is to be called when we catch a SIGSEGV/SIGBUS, determine that
4363 * it's not just a case of the program hitting the write barrier, and
4364 * are about to let Lisp deal with it. It's basically just a
4365 * convenient place to set a gdb breakpoint. */
4366 void
4367 unhandled_sigmemoryfault(void *addr)
4370 static void
4371 update_thread_page_tables(struct thread *th)
4373 gc_alloc_update_page_tables(BOXED_PAGE_FLAG, &th->alloc_region);
4374 #if defined(LISP_FEATURE_SB_SAFEPOINT_STRICTLY) && !defined(LISP_FEATURE_WIN32)
4375 gc_alloc_update_page_tables(BOXED_PAGE_FLAG, &th->sprof_alloc_region);
4376 #endif
4379 /* GC is single-threaded and all memory allocations during a
4380 collection happen in the GC thread, so it is sufficient to update
4381 all the the page tables once at the beginning of a collection and
4382 update only page tables of the GC thread during the collection. */
4383 void gc_alloc_update_all_page_tables(int for_all_threads)
4385 /* Flush the alloc regions updating the tables. */
4386 struct thread *th;
4387 if (for_all_threads) {
4388 for_each_thread(th) {
4389 update_thread_page_tables(th);
4392 else {
4393 th = arch_os_get_current_thread();
4394 if (th) {
4395 update_thread_page_tables(th);
4398 #if SEGREGATED_CODE
4399 gc_alloc_update_page_tables(CODE_PAGE_FLAG, &code_region);
4400 #endif
4401 gc_alloc_update_page_tables(UNBOXED_PAGE_FLAG, &unboxed_region);
4402 gc_alloc_update_page_tables(BOXED_PAGE_FLAG, &boxed_region);
4405 void
4406 gc_set_region_empty(struct alloc_region *region)
4408 region->first_page = 0;
4409 region->last_page = -1;
4410 region->start_addr = page_address(0);
4411 region->free_pointer = page_address(0);
4412 region->end_addr = page_address(0);
4415 static void
4416 zero_all_free_pages() /* called only by gc_and_save() */
4418 page_index_t i;
4420 for (i = 0; i < last_free_page; i++) {
4421 if (page_free_p(i)) {
4422 #ifdef READ_PROTECT_FREE_PAGES
4423 os_protect(page_address(i), GENCGC_CARD_BYTES, OS_VM_PROT_ALL);
4424 #endif
4425 zero_pages(i, i);
4430 /* Things to do before doing a final GC before saving a core (without
4431 * purify).
4433 * + Pages in large_object pages aren't moved by the GC, so we need to
4434 * unset that flag from all pages.
4435 * + The pseudo-static generation isn't normally collected, but it seems
4436 * reasonable to collect it at least when saving a core. So move the
4437 * pages to a normal generation.
4439 static void
4440 prepare_for_final_gc ()
4442 page_index_t i;
4444 #ifdef LISP_FEATURE_IMMOBILE_SPACE
4445 extern void prepare_immobile_space_for_final_gc();
4446 prepare_immobile_space_for_final_gc ();
4447 #endif
4448 for (i = 0; i < last_free_page; i++) {
4449 page_table[i].large_object = 0;
4450 if (page_table[i].gen == PSEUDO_STATIC_GENERATION) {
4451 int used = page_bytes_used(i);
4452 page_table[i].gen = HIGHEST_NORMAL_GENERATION;
4453 generations[PSEUDO_STATIC_GENERATION].bytes_allocated -= used;
4454 generations[HIGHEST_NORMAL_GENERATION].bytes_allocated += used;
4457 #ifdef PINNED_OBJECTS
4458 struct thread *th;
4459 for_each_thread(th) {
4460 write_TLS(PINNED_OBJECTS, NIL, th);
4462 #endif
4465 /* Set this switch to 1 for coalescing of strings dumped to fasl,
4466 * or 2 for coalescing of those,
4467 * plus literal strings in code compiled to memory. */
4468 char gc_coalesce_string_literals = 0;
4470 /* Do a non-conservative GC, and then save a core with the initial
4471 * function being set to the value of 'lisp_init_function' */
4472 void
4473 gc_and_save(char *filename, boolean prepend_runtime,
4474 boolean save_runtime_options, boolean compressed,
4475 int compression_level, int application_type)
4477 FILE *file;
4478 void *runtime_bytes = NULL;
4479 size_t runtime_size;
4480 extern void coalesce_similar_objects();
4481 extern struct lisp_startup_options lisp_startup_options;
4482 boolean verbose = !lisp_startup_options.noinform;
4484 file = prepare_to_save(filename, prepend_runtime, &runtime_bytes,
4485 &runtime_size);
4486 if (file == NULL)
4487 return;
4489 conservative_stack = 0;
4491 /* The filename might come from Lisp, and be moved by the now
4492 * non-conservative GC. */
4493 filename = strdup(filename);
4495 /* We're committed to process death at this point, and interrupts can not
4496 * possibly be handled in Lisp. Let the installed handler closures become
4497 * garbage, since new ones will be made by ENABLE-INTERRUPT on restart */
4498 #ifndef LISP_FEATURE_WIN32
4500 int i;
4501 for (i=0; i<NSIG; ++i)
4502 if (lowtag_of(interrupt_handlers[i].lisp) == FUN_POINTER_LOWTAG)
4503 interrupt_handlers[i].lisp = 0;
4505 #endif
4507 /* Collect twice: once into relatively high memory, and then back
4508 * into low memory. This compacts the retained data into the lower
4509 * pages, minimizing the size of the core file.
4511 prepare_for_final_gc();
4512 gencgc_alloc_start_page = last_free_page;
4513 collect_garbage(HIGHEST_NORMAL_GENERATION+1);
4515 // We always coalesce copyable numbers. Addional coalescing is done
4516 // only on request, in which case a message is shown (unless verbose=0).
4517 if (gc_coalesce_string_literals && verbose) {
4518 printf("[coalescing similar vectors... ");
4519 fflush(stdout);
4521 coalesce_similar_objects();
4522 if (gc_coalesce_string_literals && verbose)
4523 printf("done]\n");
4525 /* FIXME: now that relocate_heap() works, can we just memmove() everything
4526 * down and perform a relocation instead of a collection? */
4527 prepare_for_final_gc();
4528 gencgc_alloc_start_page = -1;
4529 collect_garbage(HIGHEST_NORMAL_GENERATION+1);
4531 if (prepend_runtime)
4532 save_runtime_to_filehandle(file, runtime_bytes, runtime_size,
4533 application_type);
4535 /* The dumper doesn't know that pages need to be zeroed before use. */
4536 zero_all_free_pages();
4537 do_destructive_cleanup_before_save(lisp_init_function);
4539 save_to_filehandle(file, filename, lisp_init_function,
4540 prepend_runtime, save_runtime_options,
4541 compressed ? compression_level : COMPRESSION_LEVEL_NONE);
4542 /* Oops. Save still managed to fail. Since we've mangled the stack
4543 * beyond hope, there's not much we can do.
4544 * (beyond FUNCALLing lisp_init_function, but I suspect that's
4545 * going to be rather unsatisfactory too... */
4546 lose("Attempt to save core after non-conservative GC failed.\n");
4549 /* Convert corefile ptes to corresponding 'struct page' */
4550 boolean gc_load_corefile_ptes(char data[], ssize_t bytes_read,
4551 page_index_t npages, page_index_t* ppage)
4553 page_index_t page = *ppage;
4554 int i = 0;
4555 struct corefile_pte pte;
4557 while (bytes_read) {
4558 bytes_read -= sizeof(struct corefile_pte);
4559 memcpy(&pte, data+i*sizeof (struct corefile_pte), sizeof pte);
4560 set_page_bytes_used(page, pte.bytes_used);
4561 // Low 2 bits of the corefile_pte hold the 'allocated' flag.
4562 // The other bits become the scan_start_offset
4563 set_page_scan_start_offset(page, pte.sso & ~0x03);
4564 page_table[page].allocated = pte.sso & 0x03;
4565 if (++page == npages)
4566 return 0; // No more to go
4567 ++i;
4569 *ppage = page;
4570 return 1; // More to go
4573 /* Prepare the array of corefile_ptes for save */
4574 void gc_store_corefile_ptes(struct corefile_pte *ptes)
4576 page_index_t i;
4577 for (i = 0; i < last_free_page; i++) {
4578 /* Thanks to alignment requirements, the two low bits
4579 * are always zero, so we can use them to store the
4580 * allocation type -- region is always closed, so only
4581 * the two low bits of allocation flags matter. */
4582 uword_t word = page_scan_start_offset(i);
4583 gc_assert((word & 0x03) == 0);
4584 ptes[i].sso = word | (0x03 & page_table[i].allocated);
4585 ptes[i].bytes_used = page_bytes_used(i);