Fix subtle bug in wipe_nonpinned_words()
[sbcl.git] / src / runtime / gencgc.c
blob814dd3ad8998bfef99b514086ec355067ed45252
1 /*
2 * GENerational Conservative Garbage Collector for SBCL
3 */
5 /*
6 * This software is part of the SBCL system. See the README file for
7 * more information.
9 * This software is derived from the CMU CL system, which was
10 * written at Carnegie Mellon University and released into the
11 * public domain. The software is in the public domain and is
12 * provided with absolutely no warranty. See the COPYING and CREDITS
13 * files for more information.
17 * For a review of garbage collection techniques (e.g. generational
18 * GC) and terminology (e.g. "scavenging") see Paul R. Wilson,
19 * "Uniprocessor Garbage Collection Techniques". As of 20000618, this
20 * had been accepted for _ACM Computing Surveys_ and was available
21 * as a PostScript preprint through
22 * <http://www.cs.utexas.edu/users/oops/papers.html>
23 * as
24 * <ftp://ftp.cs.utexas.edu/pub/garbage/bigsurv.ps>.
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <errno.h>
30 #include <string.h>
31 #include "sbcl.h"
32 #if defined(LISP_FEATURE_WIN32) && defined(LISP_FEATURE_SB_THREAD)
33 #include "pthreads_win32.h"
34 #else
35 #include <signal.h>
36 #endif
37 #include "runtime.h"
38 #include "os.h"
39 #include "interr.h"
40 #include "globals.h"
41 #include "interrupt.h"
42 #include "validate.h"
43 #include "lispregs.h"
44 #include "arch.h"
45 #include "gc.h"
46 #include "gc-internal.h"
47 #include "thread.h"
48 #include "pseudo-atomic.h"
49 #include "alloc.h"
50 #include "genesis/gc-tables.h"
51 #include "genesis/vector.h"
52 #include "genesis/weak-pointer.h"
53 #include "genesis/fdefn.h"
54 #include "genesis/simple-fun.h"
55 #include "save.h"
56 #include "genesis/hash-table.h"
57 #include "genesis/instance.h"
58 #include "genesis/layout.h"
59 #include "gencgc.h"
60 #include "hopscotch.h"
61 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
62 #include "genesis/cons.h"
63 #endif
64 #include "forwarding-ptr.h"
66 /* forward declarations */
67 page_index_t gc_find_freeish_pages(page_index_t *restart_page_ptr, sword_t nbytes,
68 int page_type_flag);
72 * GC parameters
75 /* As usually configured, generations 0-5 are normal collected generations,
76 6 is pseudo-static (the objects in which are never moved nor reclaimed),
77 and 7 is scratch space used when collecting a generation without promotion,
78 wherein it is moved to generation 7 and back again.
80 enum {
81 SCRATCH_GENERATION = PSEUDO_STATIC_GENERATION+1,
82 NUM_GENERATIONS
85 /* Should we use page protection to help avoid the scavenging of pages
86 * that don't have pointers to younger generations? */
87 boolean enable_page_protection = 1;
89 /* Largest allocation seen since last GC. */
90 os_vm_size_t large_allocation = 0;
94 * debugging
97 /* the verbosity level. All non-error messages are disabled at level 0;
98 * and only a few rare messages are printed at level 1. */
99 #if QSHOW == 2
100 boolean gencgc_verbose = 1;
101 #else
102 boolean gencgc_verbose = 0;
103 #endif
105 /* FIXME: At some point enable the various error-checking things below
106 * and see what they say. */
108 /* We hunt for pointers to old-space, when GCing generations >= verify_gen.
109 * Set verify_gens to HIGHEST_NORMAL_GENERATION + 1 to disable this kind of
110 * check. */
111 generation_index_t verify_gens = HIGHEST_NORMAL_GENERATION + 1;
113 /* Should we do a pre-scan verify of generation 0 before it's GCed? */
114 boolean pre_verify_gen_0 = 0;
116 /* Should we print a note when code objects are found in the dynamic space
117 * during a heap verify? */
118 boolean verify_dynamic_code_check = 0;
120 #ifdef LISP_FEATURE_X86
121 /* Should we check code objects for fixup errors after they are transported? */
122 boolean check_code_fixups = 0;
123 #endif
125 /* Should we check that newly allocated regions are zero filled? */
126 boolean gencgc_zero_check = 0;
128 /* Should we check that the free space is zero filled? */
129 boolean gencgc_enable_verify_zero_fill = 0;
131 /* When loading a core, don't do a full scan of the memory for the
132 * memory region boundaries. (Set to true by coreparse.c if the core
133 * contained a pagetable entry).
135 boolean gencgc_partial_pickup = 0;
137 /* If defined, free pages are read-protected to ensure that nothing
138 * accesses them.
141 /* #define READ_PROTECT_FREE_PAGES */
145 * GC structures and variables
148 /* the total bytes allocated. These are seen by Lisp DYNAMIC-USAGE. */
149 os_vm_size_t bytes_allocated = 0;
150 os_vm_size_t auto_gc_trigger = 0;
152 /* the source and destination generations. These are set before a GC starts
153 * scavenging. */
154 generation_index_t from_space;
155 generation_index_t new_space;
157 /* Set to 1 when in GC */
158 boolean gc_active_p = 0;
160 /* should the GC be conservative on stack. If false (only right before
161 * saving a core), don't scan the stack / mark pages dont_move. */
162 static boolean conservative_stack = 1;
164 /* An array of page structures is allocated on gc initialization.
165 * This helps to quickly map between an address and its page structure.
166 * page_table_pages is set from the size of the dynamic space. */
167 page_index_t page_table_pages;
168 struct page *page_table;
169 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
170 struct hopscotch_table pinned_objects;
171 #endif
173 /* In GC cards that have conservative pointers to them, should we wipe out
174 * dwords in there that are not used, so that they do not act as false
175 * root to other things in the heap from then on? This is a new feature
176 * but in testing it is both reliable and no noticeable slowdown. */
177 int do_wipe_p = 1;
179 /// Constants defined in gc-internal:
180 /// #define BOXED_PAGE_FLAG 1
181 /// #define UNBOXED_PAGE_FLAG 2
182 /// #define OPEN_REGION_PAGE_FLAG 4
184 static inline boolean page_no_region_p(page_index_t page) {
185 return !(page_table[page].allocated & OPEN_REGION_PAGE_FLAG);
188 /// Return true if 'allocated' bits are: {001, 010, 011}, false if 1zz or 000.
189 static inline boolean page_allocated_no_region_p(page_index_t page) {
190 return (page_table[page].allocated ^ OPEN_REGION_PAGE_FLAG) > OPEN_REGION_PAGE_FLAG;
193 static inline boolean page_free_p(page_index_t page) {
194 return (page_table[page].allocated == FREE_PAGE_FLAG);
197 static inline boolean page_boxed_p(page_index_t page) {
198 return (page_table[page].allocated & BOXED_PAGE_FLAG);
201 /// Return true if 'allocated' bits are: {001, 011}, false otherwise.
202 /// i.e. true of pages which could hold boxed or partially boxed objects.
203 static inline boolean page_boxed_no_region_p(page_index_t page) {
204 return (page_table[page].allocated & 5) == BOXED_PAGE_FLAG;
207 /// Return true if page MUST NOT hold boxed objects (including code).
208 static inline boolean page_unboxed_p(page_index_t page) {
209 /* Both flags set == boxed code page */
210 return (page_table[page].allocated & 3) == UNBOXED_PAGE_FLAG;
213 static inline boolean protect_page_p(page_index_t page, generation_index_t generation) {
214 return (page_boxed_no_region_p(page)
215 && (page_bytes_used(page) != 0)
216 && !page_table[page].dont_move
217 && (page_table[page].gen == generation));
220 /* Calculate the start address for the given page number. */
221 inline void *
222 page_address(page_index_t page_num)
224 return (void*)(DYNAMIC_SPACE_START + (page_num * GENCGC_CARD_BYTES));
227 /* Calculate the address where the allocation region associated with
228 * the page starts. */
229 static inline void *
230 page_scan_start(page_index_t page_index)
232 return page_address(page_index)-page_scan_start_offset(page_index);
235 /* True if the page starts a contiguous block. */
236 static inline boolean
237 page_starts_contiguous_block_p(page_index_t page_index)
239 // Don't use the preprocessor macro: 0 means 0.
240 return page_table[page_index].scan_start_offset_ == 0;
243 /* True if the page is the last page in a contiguous block. */
244 static inline boolean
245 page_ends_contiguous_block_p(page_index_t page_index, generation_index_t gen)
247 return (/* page doesn't fill block */
248 (page_bytes_used(page_index) < GENCGC_CARD_BYTES)
249 /* page is last allocated page */
250 || ((page_index + 1) >= last_free_page)
251 /* next page free */
252 || page_free_p(page_index + 1)
253 /* next page contains no data */
254 || (page_bytes_used(page_index + 1) == 0)
255 /* next page is in different generation */
256 || (page_table[page_index + 1].gen != gen)
257 /* next page starts its own contiguous block */
258 || (page_starts_contiguous_block_p(page_index + 1)));
261 /// External function for calling from Lisp.
262 page_index_t ext_find_page_index(void *addr) { return find_page_index(addr); }
264 static os_vm_size_t
265 npage_bytes(page_index_t npages)
267 gc_assert(npages>=0);
268 return ((os_vm_size_t)npages)*GENCGC_CARD_BYTES;
271 /* Check that X is a higher address than Y and return offset from Y to
272 * X in bytes. */
273 static inline os_vm_size_t
274 void_diff(void *x, void *y)
276 gc_assert(x >= y);
277 return (pointer_sized_uint_t)x - (pointer_sized_uint_t)y;
280 /* a structure to hold the state of a generation
282 * CAUTION: If you modify this, make sure to touch up the alien
283 * definition in src/code/gc.lisp accordingly. ...or better yes,
284 * deal with the FIXME there...
286 struct generation {
288 /* the first page that gc_alloc() checks on its next call */
289 page_index_t alloc_start_page;
291 /* the first page that gc_alloc_unboxed() checks on its next call */
292 page_index_t alloc_unboxed_start_page;
294 /* the first page that gc_alloc_large (boxed) considers on its next
295 * call. (Although it always allocates after the boxed_region.) */
296 page_index_t alloc_large_start_page;
298 /* the first page that gc_alloc_large (unboxed) considers on its
299 * next call. (Although it always allocates after the
300 * current_unboxed_region.) */
301 page_index_t alloc_large_unboxed_start_page;
303 /* the bytes allocated to this generation */
304 os_vm_size_t bytes_allocated;
306 /* the number of bytes at which to trigger a GC */
307 os_vm_size_t gc_trigger;
309 /* to calculate a new level for gc_trigger */
310 os_vm_size_t bytes_consed_between_gc;
312 /* the number of GCs since the last raise */
313 int num_gc;
315 /* the number of GCs to run on the generations before raising objects to the
316 * next generation */
317 int number_of_gcs_before_promotion;
319 /* the cumulative sum of the bytes allocated to this generation. It is
320 * cleared after a GC on this generations, and update before new
321 * objects are added from a GC of a younger generation. Dividing by
322 * the bytes_allocated will give the average age of the memory in
323 * this generation since its last GC. */
324 os_vm_size_t cum_sum_bytes_allocated;
326 /* a minimum average memory age before a GC will occur helps
327 * prevent a GC when a large number of new live objects have been
328 * added, in which case a GC could be a waste of time */
329 double minimum_age_before_gc;
332 /* an array of generation structures. There needs to be one more
333 * generation structure than actual generations as the oldest
334 * generation is temporarily raised then lowered. */
335 struct generation generations[NUM_GENERATIONS];
337 /* the oldest generation that is will currently be GCed by default.
338 * Valid values are: 0, 1, ... HIGHEST_NORMAL_GENERATION
340 * The default of HIGHEST_NORMAL_GENERATION enables GC on all generations.
342 * Setting this to 0 effectively disables the generational nature of
343 * the GC. In some applications generational GC may not be useful
344 * because there are no long-lived objects.
346 * An intermediate value could be handy after moving long-lived data
347 * into an older generation so an unnecessary GC of this long-lived
348 * data can be avoided. */
349 generation_index_t gencgc_oldest_gen_to_gc = HIGHEST_NORMAL_GENERATION;
351 /* META: Is nobody aside from me bothered by this especially misleading
352 * use of the word "last"? It could mean either "ultimate" or "prior",
353 * but in fact means neither. It is the *FIRST* page that should be grabbed
354 * for more space, so it is min free page, or 1+ the max used page. */
355 /* The maximum free page in the heap is maintained and used to update
356 * ALLOCATION_POINTER which is used by the room function to limit its
357 * search of the heap. XX Gencgc obviously needs to be better
358 * integrated with the Lisp code. */
360 page_index_t last_free_page;
362 #ifdef LISP_FEATURE_SB_THREAD
363 /* This lock is to prevent multiple threads from simultaneously
364 * allocating new regions which overlap each other. Note that the
365 * majority of GC is single-threaded, but alloc() may be called from
366 * >1 thread at a time and must be thread-safe. This lock must be
367 * seized before all accesses to generations[] or to parts of
368 * page_table[] that other threads may want to see */
369 static pthread_mutex_t free_pages_lock = PTHREAD_MUTEX_INITIALIZER;
370 /* This lock is used to protect non-thread-local allocation. */
371 static pthread_mutex_t allocation_lock = PTHREAD_MUTEX_INITIALIZER;
372 #endif
374 extern os_vm_size_t gencgc_release_granularity;
375 os_vm_size_t gencgc_release_granularity = GENCGC_RELEASE_GRANULARITY;
377 extern os_vm_size_t gencgc_alloc_granularity;
378 os_vm_size_t gencgc_alloc_granularity = GENCGC_ALLOC_GRANULARITY;
382 * miscellaneous heap functions
385 /* Count the number of pages which are write-protected within the
386 * given generation. */
387 static page_index_t
388 count_write_protect_generation_pages(generation_index_t generation)
390 page_index_t i, count = 0;
392 for (i = 0; i < last_free_page; i++)
393 if (!page_free_p(i)
394 && (page_table[i].gen == generation)
395 && (page_table[i].write_protected == 1))
396 count++;
397 return count;
400 /* Count the number of pages within the given generation. */
401 static page_index_t
402 count_generation_pages(generation_index_t generation)
404 page_index_t i;
405 page_index_t count = 0;
407 for (i = 0; i < last_free_page; i++)
408 if (!page_free_p(i)
409 && (page_table[i].gen == generation))
410 count++;
411 return count;
414 #if QSHOW
415 static page_index_t
416 count_dont_move_pages(void)
418 page_index_t i;
419 page_index_t count = 0;
420 for (i = 0; i < last_free_page; i++) {
421 if (!page_free_p(i)
422 && (page_table[i].dont_move != 0)) {
423 ++count;
426 return count;
428 #endif /* QSHOW */
430 /* Work through the pages and add up the number of bytes used for the
431 * given generation. */
432 static __attribute__((unused)) os_vm_size_t
433 count_generation_bytes_allocated (generation_index_t gen)
435 page_index_t i;
436 os_vm_size_t result = 0;
437 for (i = 0; i < last_free_page; i++) {
438 if (!page_free_p(i)
439 && (page_table[i].gen == gen))
440 result += page_bytes_used(i);
442 return result;
445 /* Return the average age of the memory in a generation. */
446 extern double
447 generation_average_age(generation_index_t gen)
449 if (generations[gen].bytes_allocated == 0)
450 return 0.0;
452 return
453 ((double)generations[gen].cum_sum_bytes_allocated)
454 / ((double)generations[gen].bytes_allocated);
457 #ifdef LISP_FEATURE_X86
458 extern void fpu_save(void *);
459 extern void fpu_restore(void *);
460 #endif
462 extern void
463 write_generation_stats(FILE *file)
465 generation_index_t i;
467 #ifdef LISP_FEATURE_X86
468 int fpu_state[27];
470 /* Can end up here after calling alloc_tramp which doesn't prepare
471 * the x87 state, and the C ABI uses a different mode */
472 fpu_save(fpu_state);
473 #endif
475 /* Print the heap stats. */
476 fprintf(file,
477 " Gen StaPg UbSta LaSta LUbSt Boxed Unboxed LB LUB !move Alloc Waste Trig WP GCs Mem-age\n");
479 for (i = 0; i <= SCRATCH_GENERATION; i++) {
480 page_index_t j;
481 page_index_t boxed_cnt = 0;
482 page_index_t unboxed_cnt = 0;
483 page_index_t large_boxed_cnt = 0;
484 page_index_t large_unboxed_cnt = 0;
485 page_index_t pinned_cnt=0;
487 for (j = 0; j < last_free_page; j++)
488 if (page_table[j].gen == i) {
490 /* Count the number of boxed pages within the given
491 * generation. */
492 if (page_boxed_p(j)) {
493 if (page_table[j].large_object)
494 large_boxed_cnt++;
495 else
496 boxed_cnt++;
498 if(page_table[j].dont_move) pinned_cnt++;
499 /* Count the number of unboxed pages within the given
500 * generation. */
501 if (page_unboxed_p(j)) {
502 if (page_table[j].large_object)
503 large_unboxed_cnt++;
504 else
505 unboxed_cnt++;
509 gc_assert(generations[i].bytes_allocated
510 == count_generation_bytes_allocated(i));
511 fprintf(file,
512 " %1d: %5ld %5ld %5ld %5ld",
514 (long)generations[i].alloc_start_page,
515 (long)generations[i].alloc_unboxed_start_page,
516 (long)generations[i].alloc_large_start_page,
517 (long)generations[i].alloc_large_unboxed_start_page);
518 fprintf(file,
519 " %5"PAGE_INDEX_FMT" %5"PAGE_INDEX_FMT" %5"PAGE_INDEX_FMT
520 " %5"PAGE_INDEX_FMT" %5"PAGE_INDEX_FMT,
521 boxed_cnt, unboxed_cnt, large_boxed_cnt,
522 large_unboxed_cnt, pinned_cnt);
523 fprintf(file,
524 " %8"OS_VM_SIZE_FMT
525 " %5"OS_VM_SIZE_FMT
526 " %8"OS_VM_SIZE_FMT
527 " %4"PAGE_INDEX_FMT" %3d %7.4f\n",
528 generations[i].bytes_allocated,
529 (npage_bytes(count_generation_pages(i)) - generations[i].bytes_allocated),
530 generations[i].gc_trigger,
531 count_write_protect_generation_pages(i),
532 generations[i].num_gc,
533 generation_average_age(i));
535 fprintf(file," Total bytes allocated = %"OS_VM_SIZE_FMT"\n", bytes_allocated);
536 fprintf(file," Dynamic-space-size bytes = %"OS_VM_SIZE_FMT"\n", dynamic_space_size);
538 #ifdef LISP_FEATURE_X86
539 fpu_restore(fpu_state);
540 #endif
543 extern void
544 write_heap_exhaustion_report(FILE *file, long available, long requested,
545 struct thread *thread)
547 fprintf(file,
548 "Heap exhausted during %s: %ld bytes available, %ld requested.\n",
549 gc_active_p ? "garbage collection" : "allocation",
550 available,
551 requested);
552 write_generation_stats(file);
553 fprintf(file, "GC control variables:\n");
554 fprintf(file, " *GC-INHIBIT* = %s\n *GC-PENDING* = %s\n",
555 SymbolValue(GC_INHIBIT,thread)==NIL ? "false" : "true",
556 (SymbolValue(GC_PENDING, thread) == T) ?
557 "true" : ((SymbolValue(GC_PENDING, thread) == NIL) ?
558 "false" : "in progress"));
559 #ifdef LISP_FEATURE_SB_THREAD
560 fprintf(file, " *STOP-FOR-GC-PENDING* = %s\n",
561 SymbolValue(STOP_FOR_GC_PENDING,thread)==NIL ? "false" : "true");
562 #endif
565 extern void
566 print_generation_stats(void)
568 write_generation_stats(stderr);
571 extern char* gc_logfile;
572 char * gc_logfile = NULL;
574 extern void
575 log_generation_stats(char *logfile, char *header)
577 if (logfile) {
578 FILE * log = fopen(logfile, "a");
579 if (log) {
580 fprintf(log, "%s\n", header);
581 write_generation_stats(log);
582 fclose(log);
583 } else {
584 fprintf(stderr, "Could not open gc logfile: %s\n", logfile);
585 fflush(stderr);
590 extern void
591 report_heap_exhaustion(long available, long requested, struct thread *th)
593 if (gc_logfile) {
594 FILE * log = fopen(gc_logfile, "a");
595 if (log) {
596 write_heap_exhaustion_report(log, available, requested, th);
597 fclose(log);
598 } else {
599 fprintf(stderr, "Could not open gc logfile: %s\n", gc_logfile);
600 fflush(stderr);
603 /* Always to stderr as well. */
604 write_heap_exhaustion_report(stderr, available, requested, th);
608 #if defined(LISP_FEATURE_X86)
609 void fast_bzero(void*, size_t); /* in <arch>-assem.S */
610 #endif
612 /* Zero the pages from START to END (inclusive), but use mmap/munmap instead
613 * if zeroing it ourselves, i.e. in practice give the memory back to the
614 * OS. Generally done after a large GC.
616 void zero_pages_with_mmap(page_index_t start, page_index_t end) {
617 page_index_t i;
618 void *addr = page_address(start), *new_addr;
619 os_vm_size_t length = npage_bytes(1+end-start);
621 if (start > end)
622 return;
624 gc_assert(length >= gencgc_release_granularity);
625 gc_assert((length % gencgc_release_granularity) == 0);
627 #ifdef LISP_FEATURE_LINUX
628 extern os_vm_address_t anon_dynamic_space_start;
629 // We use MADV_DONTNEED only on Linux due to differing semantics from BSD.
630 // Linux treats it as a demand that the memory be 0-filled, or refreshed
631 // from a file that backs the range. BSD takes it as a hint that you don't
632 // care if the memory has to brought in from swap when next accessed,
633 // i.e. it's not a request to make a user-visible alteration to memory.
634 // So in theory this can bring a page in from the core file, if we happen
635 // to hit a page that resides in the portion of memory mapped by coreparse.
636 // In practice this should not happen because objects from a core file can't
637 // become garbage. Except in save-lisp-and-die they can, and we must be
638 // cautious not to resurrect bytes that originally came from the file.
639 if ((os_vm_address_t)addr >= anon_dynamic_space_start) {
640 if (madvise(addr, length, MADV_DONTNEED) != 0)
641 lose("madvise failed\n");
642 } else
643 #endif
645 os_invalidate(addr, length);
646 new_addr = os_validate(addr, length);
647 if (new_addr == NULL || new_addr != addr) {
648 lose("remap_free_pages: page moved, 0x%08x ==> 0x%08x",
649 start, new_addr);
653 for (i = start; i <= end; i++)
654 set_page_need_to_zero(i, 0);
657 /* Zero the pages from START to END (inclusive). Generally done just after
658 * a new region has been allocated.
660 static void
661 zero_pages(page_index_t start, page_index_t end) {
662 if (start > end)
663 return;
665 #if defined(LISP_FEATURE_X86)
666 fast_bzero(page_address(start), npage_bytes(1+end-start));
667 #else
668 bzero(page_address(start), npage_bytes(1+end-start));
669 #endif
673 static void
674 zero_and_mark_pages(page_index_t start, page_index_t end) {
675 page_index_t i;
677 zero_pages(start, end);
678 for (i = start; i <= end; i++)
679 set_page_need_to_zero(i, 0);
682 /* Zero the pages from START to END (inclusive), except for those
683 * pages that are known to already zeroed. Mark all pages in the
684 * ranges as non-zeroed.
686 static void
687 zero_dirty_pages(page_index_t start, page_index_t end) {
688 page_index_t i, j;
690 for (i = start; i <= end; i++) {
691 if (!page_need_to_zero(i)) continue;
692 for (j = i+1; (j <= end) && page_need_to_zero(j) ; j++)
693 ; /* empty body */
694 zero_pages(i, j-1);
695 i = j;
698 for (i = start; i <= end; i++) {
699 set_page_need_to_zero(i, 1);
705 * To support quick and inline allocation, regions of memory can be
706 * allocated and then allocated from with just a free pointer and a
707 * check against an end address.
709 * Since objects can be allocated to spaces with different properties
710 * e.g. boxed/unboxed, generation, ages; there may need to be many
711 * allocation regions.
713 * Each allocation region may start within a partly used page. Many
714 * features of memory use are noted on a page wise basis, e.g. the
715 * generation; so if a region starts within an existing allocated page
716 * it must be consistent with this page.
718 * During the scavenging of the newspace, objects will be transported
719 * into an allocation region, and pointers updated to point to this
720 * allocation region. It is possible that these pointers will be
721 * scavenged again before the allocation region is closed, e.g. due to
722 * trans_list which jumps all over the place to cleanup the list. It
723 * is important to be able to determine properties of all objects
724 * pointed to when scavenging, e.g to detect pointers to the oldspace.
725 * Thus it's important that the allocation regions have the correct
726 * properties set when allocated, and not just set when closed. The
727 * region allocation routines return regions with the specified
728 * properties, and grab all the pages, setting their properties
729 * appropriately, except that the amount used is not known.
731 * These regions are used to support quicker allocation using just a
732 * free pointer. The actual space used by the region is not reflected
733 * in the pages tables until it is closed. It can't be scavenged until
734 * closed.
736 * When finished with the region it should be closed, which will
737 * update the page tables for the actual space used returning unused
738 * space. Further it may be noted in the new regions which is
739 * necessary when scavenging the newspace.
741 * Large objects may be allocated directly without an allocation
742 * region, the page tables are updated immediately.
744 * Unboxed objects don't contain pointers to other objects and so
745 * don't need scavenging. Further they can't contain pointers to
746 * younger generations so WP is not needed. By allocating pages to
747 * unboxed objects the whole page never needs scavenging or
748 * write-protecting. */
750 /* We are only using two regions at present. Both are for the current
751 * newspace generation. */
752 struct alloc_region boxed_region;
753 struct alloc_region unboxed_region;
755 /* The generation currently being allocated to. */
756 static generation_index_t gc_alloc_generation;
758 static inline page_index_t
759 generation_alloc_start_page(generation_index_t generation, int page_type_flag, int large)
761 if (large) {
762 if (UNBOXED_PAGE_FLAG == page_type_flag) {
763 return generations[generation].alloc_large_unboxed_start_page;
764 } else if (BOXED_PAGE_FLAG & page_type_flag) {
765 /* Both code and data. */
766 return generations[generation].alloc_large_start_page;
767 } else {
768 lose("bad page type flag: %d", page_type_flag);
770 } else {
771 if (UNBOXED_PAGE_FLAG == page_type_flag) {
772 return generations[generation].alloc_unboxed_start_page;
773 } else if (BOXED_PAGE_FLAG & page_type_flag) {
774 /* Both code and data. */
775 return generations[generation].alloc_start_page;
776 } else {
777 lose("bad page_type_flag: %d", page_type_flag);
782 static inline void
783 set_generation_alloc_start_page(generation_index_t generation, int page_type_flag, int large,
784 page_index_t page)
786 if (large) {
787 if (UNBOXED_PAGE_FLAG == page_type_flag) {
788 generations[generation].alloc_large_unboxed_start_page = page;
789 } else if (BOXED_PAGE_FLAG & page_type_flag) {
790 /* Both code and data. */
791 generations[generation].alloc_large_start_page = page;
792 } else {
793 lose("bad page type flag: %d", page_type_flag);
795 } else {
796 if (UNBOXED_PAGE_FLAG == page_type_flag) {
797 generations[generation].alloc_unboxed_start_page = page;
798 } else if (BOXED_PAGE_FLAG & page_type_flag) {
799 /* Both code and data. */
800 generations[generation].alloc_start_page = page;
801 } else {
802 lose("bad page type flag: %d", page_type_flag);
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 os_vm_size_t bytes_found;
836 page_index_t i;
837 int ret;
840 FSHOW((stderr,
841 "/alloc_new_region for %d bytes from gen %d\n",
842 nbytes, gc_alloc_generation));
845 /* Check that the region is in a reset state. */
846 gc_assert((alloc_region->first_page == 0)
847 && (alloc_region->last_page == -1)
848 && (alloc_region->free_pointer == alloc_region->end_addr));
849 ret = thread_mutex_lock(&free_pages_lock);
850 gc_assert(ret == 0);
851 first_page = generation_alloc_start_page(gc_alloc_generation, page_type_flag, 0);
852 last_page=gc_find_freeish_pages(&first_page, nbytes, page_type_flag);
853 bytes_found=(GENCGC_CARD_BYTES - page_bytes_used(first_page))
854 + npage_bytes(last_page-first_page);
856 /* Set up the alloc_region. */
857 alloc_region->first_page = first_page;
858 alloc_region->last_page = last_page;
859 alloc_region->start_addr = page_bytes_used(first_page)
860 + page_address(first_page);
861 alloc_region->free_pointer = alloc_region->start_addr;
862 alloc_region->end_addr = alloc_region->start_addr + bytes_found;
864 /* Set up the pages. */
866 /* The first page may have already been in use. */
867 if (page_bytes_used(first_page) == 0) {
868 page_table[first_page].allocated = page_type_flag;
869 page_table[first_page].gen = gc_alloc_generation;
870 page_table[first_page].large_object = 0;
871 set_page_scan_start_offset(first_page, 0);
874 gc_assert(page_table[first_page].allocated == page_type_flag);
875 page_table[first_page].allocated |= OPEN_REGION_PAGE_FLAG;
877 gc_assert(page_table[first_page].gen == gc_alloc_generation);
878 gc_assert(page_table[first_page].large_object == 0);
880 for (i = first_page+1; i <= last_page; i++) {
881 page_table[i].allocated = page_type_flag;
882 page_table[i].gen = gc_alloc_generation;
883 page_table[i].large_object = 0;
884 /* This may not be necessary for unboxed regions (think it was
885 * broken before!) */
886 set_page_scan_start_offset(i,
887 void_diff(page_address(i), alloc_region->start_addr));
888 page_table[i].allocated |= OPEN_REGION_PAGE_FLAG ;
890 /* Bump up last_free_page. */
891 if (last_page+1 > last_free_page) {
892 last_free_page = last_page+1;
893 /* do we only want to call this on special occasions? like for
894 * boxed_region? */
895 set_alloc_pointer((lispobj)page_address(last_free_page));
897 ret = thread_mutex_unlock(&free_pages_lock);
898 gc_assert(ret == 0);
900 #ifdef READ_PROTECT_FREE_PAGES
901 os_protect(page_address(first_page),
902 npage_bytes(1+last_page-first_page),
903 OS_VM_PROT_ALL);
904 #endif
906 /* If the first page was only partial, don't check whether it's
907 * zeroed (it won't be) and don't zero it (since the parts that
908 * we're interested in are guaranteed to be zeroed).
910 if (page_bytes_used(first_page)) {
911 first_page++;
914 zero_dirty_pages(first_page, last_page);
916 /* we can do this after releasing free_pages_lock */
917 if (gencgc_zero_check) {
918 word_t *p;
919 for (p = (word_t *)alloc_region->start_addr;
920 p < (word_t *)alloc_region->end_addr; p++) {
921 if (*p != 0) {
922 lose("The new region is not zero at %p (start=%p, end=%p).\n",
923 p, alloc_region->start_addr, alloc_region->end_addr);
929 /* If the record_new_objects flag is 2 then all new regions created
930 * are recorded.
932 * If it's 1 then then it is only recorded if the first page of the
933 * current region is <= new_areas_ignore_page. This helps avoid
934 * unnecessary recording when doing full scavenge pass.
936 * The new_object structure holds the page, byte offset, and size of
937 * new regions of objects. Each new area is placed in the array of
938 * these structures pointer to by new_areas. new_areas_index holds the
939 * offset into new_areas.
941 * If new_area overflows NUM_NEW_AREAS then it stops adding them. The
942 * later code must detect this and handle it, probably by doing a full
943 * scavenge of a generation. */
944 #define NUM_NEW_AREAS 512
945 static int record_new_objects = 0;
946 static page_index_t new_areas_ignore_page;
947 struct new_area {
948 page_index_t page;
949 size_t offset;
950 size_t size;
952 static struct new_area (*new_areas)[];
953 static size_t new_areas_index;
954 size_t max_new_areas;
956 /* Add a new area to new_areas. */
957 static void
958 add_new_area(page_index_t first_page, size_t offset, size_t size)
960 size_t new_area_start, c;
961 ssize_t i;
963 /* Ignore if full. */
964 if (new_areas_index >= NUM_NEW_AREAS)
965 return;
967 switch (record_new_objects) {
968 case 0:
969 return;
970 case 1:
971 if (first_page > new_areas_ignore_page)
972 return;
973 break;
974 case 2:
975 break;
976 default:
977 gc_abort();
980 new_area_start = npage_bytes(first_page) + offset;
982 /* Search backwards for a prior area that this follows from. If
983 found this will save adding a new area. */
984 for (i = new_areas_index-1, c = 0; (i >= 0) && (c < 8); i--, c++) {
985 size_t area_end =
986 npage_bytes((*new_areas)[i].page)
987 + (*new_areas)[i].offset
988 + (*new_areas)[i].size;
989 /*FSHOW((stderr,
990 "/add_new_area S1 %d %d %d %d\n",
991 i, c, new_area_start, area_end));*/
992 if (new_area_start == area_end) {
993 /*FSHOW((stderr,
994 "/adding to [%d] %d %d %d with %d %d %d:\n",
996 (*new_areas)[i].page,
997 (*new_areas)[i].offset,
998 (*new_areas)[i].size,
999 first_page,
1000 offset,
1001 size);*/
1002 (*new_areas)[i].size += size;
1003 return;
1007 (*new_areas)[new_areas_index].page = first_page;
1008 (*new_areas)[new_areas_index].offset = offset;
1009 (*new_areas)[new_areas_index].size = size;
1010 /*FSHOW((stderr,
1011 "/new_area %d page %d offset %d size %d\n",
1012 new_areas_index, first_page, offset, size));*/
1013 new_areas_index++;
1015 /* Note the max new_areas used. */
1016 if (new_areas_index > max_new_areas)
1017 max_new_areas = new_areas_index;
1020 /* Update the tables for the alloc_region. The region may be added to
1021 * the new_areas.
1023 * When done the alloc_region is set up so that the next quick alloc
1024 * will fail safely and thus a new region will be allocated. Further
1025 * it is safe to try to re-update the page table of this reset
1026 * alloc_region. */
1027 void
1028 gc_alloc_update_page_tables(int page_type_flag, struct alloc_region *alloc_region)
1030 boolean more;
1031 page_index_t first_page;
1032 page_index_t next_page;
1033 os_vm_size_t bytes_used;
1034 os_vm_size_t region_size;
1035 os_vm_size_t byte_cnt;
1036 page_bytes_t orig_first_page_bytes_used;
1037 int ret;
1040 first_page = alloc_region->first_page;
1042 /* Catch an unused alloc_region. */
1043 if ((first_page == 0) && (alloc_region->last_page == -1))
1044 return;
1046 next_page = first_page+1;
1048 ret = thread_mutex_lock(&free_pages_lock);
1049 gc_assert(ret == 0);
1050 if (alloc_region->free_pointer != alloc_region->start_addr) {
1051 /* some bytes were allocated in the region */
1052 orig_first_page_bytes_used = page_bytes_used(first_page);
1054 gc_assert(alloc_region->start_addr ==
1055 (page_address(first_page) + page_bytes_used(first_page)));
1057 /* All the pages used need to be updated */
1059 /* Update the first page. */
1061 /* If the page was free then set up the gen, and
1062 * scan_start_offset. */
1063 if (page_bytes_used(first_page) == 0)
1064 gc_assert(page_starts_contiguous_block_p(first_page));
1065 page_table[first_page].allocated &= ~(OPEN_REGION_PAGE_FLAG);
1067 gc_assert(page_table[first_page].allocated & page_type_flag);
1068 gc_assert(page_table[first_page].gen == gc_alloc_generation);
1069 gc_assert(page_table[first_page].large_object == 0);
1071 byte_cnt = 0;
1073 /* Calculate the number of bytes used in this page. This is not
1074 * always the number of new bytes, unless it was free. */
1075 more = 0;
1076 if ((bytes_used = void_diff(alloc_region->free_pointer,
1077 page_address(first_page)))
1078 >GENCGC_CARD_BYTES) {
1079 bytes_used = GENCGC_CARD_BYTES;
1080 more = 1;
1082 set_page_bytes_used(first_page, bytes_used);
1083 byte_cnt += bytes_used;
1086 /* All the rest of the pages should be free. We need to set
1087 * their scan_start_offset pointer to the start of the
1088 * region, and set the bytes_used. */
1089 while (more) {
1090 page_table[next_page].allocated &= ~(OPEN_REGION_PAGE_FLAG);
1091 gc_assert(page_table[next_page].allocated & page_type_flag);
1092 gc_assert(page_bytes_used(next_page) == 0);
1093 gc_assert(page_table[next_page].gen == gc_alloc_generation);
1094 gc_assert(page_table[next_page].large_object == 0);
1095 gc_assert(page_scan_start_offset(next_page) ==
1096 void_diff(page_address(next_page),
1097 alloc_region->start_addr));
1099 /* Calculate the number of bytes used in this page. */
1100 more = 0;
1101 if ((bytes_used = void_diff(alloc_region->free_pointer,
1102 page_address(next_page)))>GENCGC_CARD_BYTES) {
1103 bytes_used = GENCGC_CARD_BYTES;
1104 more = 1;
1106 set_page_bytes_used(next_page, bytes_used);
1107 byte_cnt += bytes_used;
1109 next_page++;
1112 region_size = void_diff(alloc_region->free_pointer,
1113 alloc_region->start_addr);
1114 bytes_allocated += region_size;
1115 generations[gc_alloc_generation].bytes_allocated += region_size;
1117 gc_assert((byte_cnt- orig_first_page_bytes_used) == region_size);
1119 /* Set the generations alloc restart page to the last page of
1120 * the region. */
1121 set_generation_alloc_start_page(gc_alloc_generation, page_type_flag, 0, next_page-1);
1123 /* Add the region to the new_areas if requested. */
1124 if (BOXED_PAGE_FLAG & page_type_flag)
1125 add_new_area(first_page,orig_first_page_bytes_used, region_size);
1128 FSHOW((stderr,
1129 "/gc_alloc_update_page_tables update %d bytes to gen %d\n",
1130 region_size,
1131 gc_alloc_generation));
1133 } else {
1134 /* There are no bytes allocated. Unallocate the first_page if
1135 * there are 0 bytes_used. */
1136 page_table[first_page].allocated &= ~(OPEN_REGION_PAGE_FLAG);
1137 if (page_bytes_used(first_page) == 0)
1138 page_table[first_page].allocated = FREE_PAGE_FLAG;
1141 /* Unallocate any unused pages. */
1142 while (next_page <= alloc_region->last_page) {
1143 gc_assert(page_bytes_used(next_page) == 0);
1144 page_table[next_page].allocated = FREE_PAGE_FLAG;
1145 next_page++;
1147 ret = thread_mutex_unlock(&free_pages_lock);
1148 gc_assert(ret == 0);
1150 /* alloc_region is per-thread, we're ok to do this unlocked */
1151 gc_set_region_empty(alloc_region);
1154 /* Allocate a possibly large object. */
1155 void *
1156 gc_alloc_large(sword_t nbytes, int page_type_flag, struct alloc_region *alloc_region)
1158 boolean more;
1159 page_index_t first_page, next_page, last_page;
1160 page_bytes_t orig_first_page_bytes_used;
1161 os_vm_size_t byte_cnt;
1162 os_vm_size_t bytes_used;
1163 int ret;
1165 ret = thread_mutex_lock(&free_pages_lock);
1166 gc_assert(ret == 0);
1168 first_page = generation_alloc_start_page(gc_alloc_generation, page_type_flag, 1);
1169 if (first_page <= alloc_region->last_page) {
1170 first_page = alloc_region->last_page+1;
1173 last_page=gc_find_freeish_pages(&first_page,nbytes, page_type_flag);
1175 gc_assert(first_page > alloc_region->last_page);
1177 set_generation_alloc_start_page(gc_alloc_generation, page_type_flag, 1, last_page);
1179 /* Set up the pages. */
1180 orig_first_page_bytes_used = page_bytes_used(first_page);
1182 /* If the first page was free then set up the gen, and
1183 * scan_start_offset. */
1184 if (page_bytes_used(first_page) == 0) {
1185 page_table[first_page].allocated = page_type_flag;
1186 page_table[first_page].gen = gc_alloc_generation;
1187 set_page_scan_start_offset(first_page, 0);
1188 page_table[first_page].large_object = 1;
1191 gc_assert(page_table[first_page].allocated == page_type_flag);
1192 gc_assert(page_table[first_page].gen == gc_alloc_generation);
1193 gc_assert(page_table[first_page].large_object == 1);
1195 byte_cnt = 0;
1197 /* Calc. the number of bytes used in this page. This is not
1198 * always the number of new bytes, unless it was free. */
1199 more = 0;
1200 if ((bytes_used = nbytes+orig_first_page_bytes_used) > GENCGC_CARD_BYTES) {
1201 bytes_used = GENCGC_CARD_BYTES;
1202 more = 1;
1204 set_page_bytes_used(first_page, bytes_used);
1205 byte_cnt += bytes_used;
1207 next_page = first_page+1;
1209 /* All the rest of the pages should be free. We need to set their
1210 * scan_start_offset pointer to the start of the region, and set
1211 * the bytes_used. */
1212 while (more) {
1213 gc_assert(page_free_p(next_page));
1214 gc_assert(page_bytes_used(next_page) == 0);
1215 page_table[next_page].allocated = page_type_flag;
1216 page_table[next_page].gen = gc_alloc_generation;
1217 page_table[next_page].large_object = 1;
1219 set_page_scan_start_offset(next_page,
1220 npage_bytes(next_page-first_page) - orig_first_page_bytes_used);
1222 /* Calculate the number of bytes used in this page. */
1223 more = 0;
1224 bytes_used=(nbytes+orig_first_page_bytes_used)-byte_cnt;
1225 if (bytes_used > GENCGC_CARD_BYTES) {
1226 bytes_used = GENCGC_CARD_BYTES;
1227 more = 1;
1229 set_page_bytes_used(next_page, bytes_used);
1230 page_table[next_page].write_protected=0;
1231 page_table[next_page].dont_move=0;
1232 byte_cnt += bytes_used;
1233 next_page++;
1236 gc_assert((byte_cnt-orig_first_page_bytes_used) == (size_t)nbytes);
1238 bytes_allocated += nbytes;
1239 generations[gc_alloc_generation].bytes_allocated += nbytes;
1241 /* Add the region to the new_areas if requested. */
1242 if (BOXED_PAGE_FLAG & page_type_flag)
1243 add_new_area(first_page,orig_first_page_bytes_used,nbytes);
1245 /* Bump up last_free_page */
1246 if (last_page+1 > last_free_page) {
1247 last_free_page = last_page+1;
1248 set_alloc_pointer((lispobj)(page_address(last_free_page)));
1250 ret = thread_mutex_unlock(&free_pages_lock);
1251 gc_assert(ret == 0);
1253 #ifdef READ_PROTECT_FREE_PAGES
1254 os_protect(page_address(first_page),
1255 npage_bytes(1+last_page-first_page),
1256 OS_VM_PROT_ALL);
1257 #endif
1259 zero_dirty_pages(first_page, last_page);
1261 return page_address(first_page);
1264 static page_index_t gencgc_alloc_start_page = -1;
1266 void
1267 gc_heap_exhausted_error_or_lose (sword_t available, sword_t requested)
1269 struct thread *thread = arch_os_get_current_thread();
1270 /* Write basic information before doing anything else: if we don't
1271 * call to lisp this is a must, and even if we do there is always
1272 * the danger that we bounce back here before the error has been
1273 * handled, or indeed even printed.
1275 report_heap_exhaustion(available, requested, thread);
1276 if (gc_active_p || (available == 0)) {
1277 /* If we are in GC, or totally out of memory there is no way
1278 * to sanely transfer control to the lisp-side of things.
1280 lose("Heap exhausted, game over.");
1282 else {
1283 /* FIXME: assert free_pages_lock held */
1284 (void)thread_mutex_unlock(&free_pages_lock);
1285 #if !(defined(LISP_FEATURE_WIN32) && defined(LISP_FEATURE_SB_THREAD))
1286 gc_assert(get_pseudo_atomic_atomic(thread));
1287 clear_pseudo_atomic_atomic(thread);
1288 if (get_pseudo_atomic_interrupted(thread))
1289 do_pending_interrupt();
1290 #endif
1291 /* Another issue is that signalling HEAP-EXHAUSTED error leads
1292 * to running user code at arbitrary places, even in a
1293 * WITHOUT-INTERRUPTS which may lead to a deadlock without
1294 * running out of the heap. So at this point all bets are
1295 * off. */
1296 if (SymbolValue(INTERRUPTS_ENABLED,thread) == NIL)
1297 corruption_warning_and_maybe_lose
1298 ("Signalling HEAP-EXHAUSTED in a WITHOUT-INTERRUPTS.");
1299 /* available and requested should be double word aligned, thus
1300 they can passed as fixnums and shifted later. */
1301 funcall2(StaticSymbolFunction(HEAP_EXHAUSTED_ERROR), available, requested);
1302 lose("HEAP-EXHAUSTED-ERROR fell through");
1306 page_index_t
1307 gc_find_freeish_pages(page_index_t *restart_page_ptr, sword_t bytes,
1308 int page_type_flag)
1310 page_index_t most_bytes_found_from = 0, most_bytes_found_to = 0;
1311 page_index_t first_page, last_page, restart_page = *restart_page_ptr;
1312 os_vm_size_t nbytes = bytes;
1313 os_vm_size_t nbytes_goal = nbytes;
1314 os_vm_size_t bytes_found = 0;
1315 os_vm_size_t most_bytes_found = 0;
1316 boolean small_object = nbytes < GENCGC_CARD_BYTES;
1317 /* FIXME: assert(free_pages_lock is held); */
1319 if (nbytes_goal < gencgc_alloc_granularity)
1320 nbytes_goal = gencgc_alloc_granularity;
1322 /* Toggled by gc_and_save for heap compaction, normally -1. */
1323 if (gencgc_alloc_start_page != -1) {
1324 restart_page = gencgc_alloc_start_page;
1327 /* FIXME: This is on bytes instead of nbytes pending cleanup of
1328 * long from the interface. */
1329 gc_assert(bytes>=0);
1330 /* Search for a page with at least nbytes of space. We prefer
1331 * not to split small objects on multiple pages, to reduce the
1332 * number of contiguous allocation regions spaning multiple
1333 * pages: this helps avoid excessive conservativism.
1335 * For other objects, we guarantee that they start on their own
1336 * page boundary.
1338 first_page = restart_page;
1339 while (first_page < page_table_pages) {
1340 bytes_found = 0;
1341 if (page_free_p(first_page)) {
1342 gc_assert(0 == page_bytes_used(first_page));
1343 bytes_found = GENCGC_CARD_BYTES;
1344 } else if (small_object &&
1345 (page_table[first_page].allocated == page_type_flag) &&
1346 (page_table[first_page].large_object == 0) &&
1347 (page_table[first_page].gen == gc_alloc_generation) &&
1348 (page_table[first_page].write_protected == 0) &&
1349 (page_table[first_page].dont_move == 0)) {
1350 bytes_found = GENCGC_CARD_BYTES - page_bytes_used(first_page);
1351 if (bytes_found < nbytes) {
1352 if (bytes_found > most_bytes_found)
1353 most_bytes_found = bytes_found;
1354 first_page++;
1355 continue;
1357 } else {
1358 first_page++;
1359 continue;
1362 gc_assert(page_table[first_page].write_protected == 0);
1363 for (last_page = first_page+1;
1364 ((last_page < page_table_pages) &&
1365 page_free_p(last_page) &&
1366 (bytes_found < nbytes_goal));
1367 last_page++) {
1368 bytes_found += GENCGC_CARD_BYTES;
1369 gc_assert(0 == page_bytes_used(last_page));
1370 gc_assert(0 == page_table[last_page].write_protected);
1373 if (bytes_found > most_bytes_found) {
1374 most_bytes_found = bytes_found;
1375 most_bytes_found_from = first_page;
1376 most_bytes_found_to = last_page;
1378 if (bytes_found >= nbytes_goal)
1379 break;
1381 first_page = last_page;
1384 bytes_found = most_bytes_found;
1385 restart_page = first_page + 1;
1387 /* Check for a failure */
1388 if (bytes_found < nbytes) {
1389 gc_assert(restart_page >= page_table_pages);
1390 gc_heap_exhausted_error_or_lose(most_bytes_found, nbytes);
1393 gc_assert(most_bytes_found_to);
1394 *restart_page_ptr = most_bytes_found_from;
1395 return most_bytes_found_to-1;
1398 /* Allocate bytes. All the rest of the special-purpose allocation
1399 * functions will eventually call this */
1401 void *
1402 gc_alloc_with_region(sword_t nbytes,int page_type_flag, struct alloc_region *my_region,
1403 int quick_p)
1405 void *new_free_pointer;
1407 if (nbytes>=LARGE_OBJECT_SIZE)
1408 return gc_alloc_large(nbytes, page_type_flag, my_region);
1410 /* Check whether there is room in the current alloc region. */
1411 new_free_pointer = my_region->free_pointer + nbytes;
1413 /* fprintf(stderr, "alloc %d bytes from %p to %p\n", nbytes,
1414 my_region->free_pointer, new_free_pointer); */
1416 if (new_free_pointer <= my_region->end_addr) {
1417 /* If so then allocate from the current alloc region. */
1418 void *new_obj = my_region->free_pointer;
1419 my_region->free_pointer = new_free_pointer;
1421 /* Unless a `quick' alloc was requested, check whether the
1422 alloc region is almost empty. */
1423 if (!quick_p &&
1424 void_diff(my_region->end_addr,my_region->free_pointer) <= 32) {
1425 /* If so, finished with the current region. */
1426 gc_alloc_update_page_tables(page_type_flag, my_region);
1427 /* Set up a new region. */
1428 gc_alloc_new_region(32 /*bytes*/, page_type_flag, my_region);
1431 return((void *)new_obj);
1434 /* Else not enough free space in the current region: retry with a
1435 * new region. */
1437 gc_alloc_update_page_tables(page_type_flag, my_region);
1438 gc_alloc_new_region(nbytes, page_type_flag, my_region);
1439 return gc_alloc_with_region(nbytes, page_type_flag, my_region,0);
1442 /* Copy a large object. If the object is in a large object region then
1443 * it is simply promoted, else it is copied. If it's large enough then
1444 * it's copied to a large object region.
1446 * Bignums and vectors may have shrunk. If the object is not copied
1447 * the space needs to be reclaimed, and the page_tables corrected. */
1448 static lispobj
1449 general_copy_large_object(lispobj object, word_t nwords, boolean boxedp)
1451 lispobj *new;
1452 page_index_t first_page;
1454 CHECK_COPY_PRECONDITIONS(object, nwords);
1456 if ((nwords > 1024*1024) && gencgc_verbose) {
1457 FSHOW((stderr, "/general_copy_large_object: %d bytes\n",
1458 nwords*N_WORD_BYTES));
1461 /* Check whether it's a large object. */
1462 first_page = find_page_index((void *)object);
1463 gc_assert(first_page >= 0);
1465 if (page_table[first_page].large_object) {
1466 /* Promote the object. Note: Unboxed objects may have been
1467 * allocated to a BOXED region so it may be necessary to
1468 * change the region to UNBOXED. */
1469 os_vm_size_t remaining_bytes;
1470 os_vm_size_t bytes_freed;
1471 page_index_t next_page;
1472 page_bytes_t old_bytes_used;
1474 /* FIXME: This comment is somewhat stale.
1476 * Note: Any page write-protection must be removed, else a
1477 * later scavenge_newspace may incorrectly not scavenge these
1478 * pages. This would not be necessary if they are added to the
1479 * new areas, but let's do it for them all (they'll probably
1480 * be written anyway?). */
1482 gc_assert(page_starts_contiguous_block_p(first_page));
1483 next_page = first_page;
1484 remaining_bytes = nwords*N_WORD_BYTES;
1486 while (remaining_bytes > GENCGC_CARD_BYTES) {
1487 gc_assert(page_table[next_page].gen == from_space);
1488 gc_assert(page_table[next_page].large_object);
1489 gc_assert(page_scan_start_offset(next_page) ==
1490 npage_bytes(next_page-first_page));
1491 gc_assert(page_bytes_used(next_page) == GENCGC_CARD_BYTES);
1492 /* Should have been unprotected by unprotect_oldspace()
1493 * for boxed objects, and after promotion unboxed ones
1494 * should not be on protected pages at all. */
1495 gc_assert(!page_table[next_page].write_protected);
1497 if (boxedp)
1498 gc_assert(page_boxed_p(next_page));
1499 else {
1500 gc_assert(page_allocated_no_region_p(next_page));
1501 page_table[next_page].allocated = UNBOXED_PAGE_FLAG;
1503 page_table[next_page].gen = new_space;
1505 remaining_bytes -= GENCGC_CARD_BYTES;
1506 next_page++;
1509 /* Now only one page remains, but the object may have shrunk so
1510 * there may be more unused pages which will be freed. */
1512 /* Object may have shrunk but shouldn't have grown - check. */
1513 gc_assert(page_bytes_used(next_page) >= remaining_bytes);
1515 page_table[next_page].gen = new_space;
1517 if (boxedp)
1518 gc_assert(page_boxed_p(next_page));
1519 else
1520 page_table[next_page].allocated = UNBOXED_PAGE_FLAG;
1522 /* Adjust the bytes_used. */
1523 old_bytes_used = page_bytes_used(next_page);
1524 set_page_bytes_used(next_page, remaining_bytes);
1526 bytes_freed = old_bytes_used - remaining_bytes;
1528 /* Free any remaining pages; needs care. */
1529 next_page++;
1530 while ((old_bytes_used == GENCGC_CARD_BYTES) &&
1531 (page_table[next_page].gen == from_space) &&
1532 /* FIXME: It is not obvious to me why this is necessary
1533 * as a loop condition: it seems to me that the
1534 * scan_start_offset test should be sufficient, but
1535 * experimentally that is not the case. --NS
1536 * 2011-11-28 */
1537 (boxedp ?
1538 page_boxed_p(next_page) :
1539 page_allocated_no_region_p(next_page)) &&
1540 page_table[next_page].large_object &&
1541 (page_scan_start_offset(next_page) ==
1542 npage_bytes(next_page - first_page))) {
1543 /* Checks out OK, free the page. Don't need to both zeroing
1544 * pages as this should have been done before shrinking the
1545 * object. These pages shouldn't be write-protected, even if
1546 * boxed they should be zero filled. */
1547 gc_assert(page_table[next_page].write_protected == 0);
1549 old_bytes_used = page_bytes_used(next_page);
1550 page_table[next_page].allocated = FREE_PAGE_FLAG;
1551 set_page_bytes_used(next_page, 0);
1552 bytes_freed += old_bytes_used;
1553 next_page++;
1556 if ((bytes_freed > 0) && gencgc_verbose) {
1557 FSHOW((stderr,
1558 "/general_copy_large_object bytes_freed=%"OS_VM_SIZE_FMT"\n",
1559 bytes_freed));
1562 generations[from_space].bytes_allocated -= nwords*N_WORD_BYTES
1563 + bytes_freed;
1564 generations[new_space].bytes_allocated += nwords*N_WORD_BYTES;
1565 bytes_allocated -= bytes_freed;
1567 /* Add the region to the new_areas if requested. */
1568 if (boxedp)
1569 add_new_area(first_page,0,nwords*N_WORD_BYTES);
1571 return(object);
1573 } else {
1574 /* Allocate space. */
1575 new = gc_general_alloc(nwords*N_WORD_BYTES,
1576 (boxedp ? BOXED_PAGE_FLAG : UNBOXED_PAGE_FLAG),
1577 ALLOC_QUICK);
1579 /* Copy the object. */
1580 memcpy(new,native_pointer(object),nwords*N_WORD_BYTES);
1582 /* Return Lisp pointer of new object. */
1583 return make_lispobj(new, lowtag_of(object));
1587 lispobj
1588 copy_large_object(lispobj object, sword_t nwords)
1590 return general_copy_large_object(object, nwords, 1);
1593 lispobj
1594 copy_large_unboxed_object(lispobj object, sword_t nwords)
1596 return general_copy_large_object(object, nwords, 0);
1599 /* to copy unboxed objects */
1600 lispobj
1601 copy_unboxed_object(lispobj object, sword_t nwords)
1603 return gc_general_copy_object(object, nwords, UNBOXED_PAGE_FLAG);
1608 * code and code-related objects
1611 static lispobj trans_fun_header(lispobj object);
1612 static lispobj trans_boxed(lispobj object);
1615 /* Scan a x86 compiled code object, looking for possible fixups that
1616 * have been missed after a move.
1618 * Two types of fixups are needed:
1619 * 1. Absolute fixups to within the code object.
1620 * 2. Relative fixups to outside the code object.
1622 * Currently only absolute fixups to the constant vector, or to the
1623 * code area are checked. */
1624 #ifdef LISP_FEATURE_X86
1625 void
1626 sniff_code_object(struct code *code, os_vm_size_t displacement)
1628 sword_t nheader_words, ncode_words, nwords;
1629 os_vm_address_t constants_start_addr = NULL, constants_end_addr, p;
1630 os_vm_address_t code_start_addr, code_end_addr;
1631 os_vm_address_t code_addr = (os_vm_address_t)code;
1632 int fixup_found = 0;
1634 if (!check_code_fixups)
1635 return;
1637 FSHOW((stderr, "/sniffing code: %p, %lu\n", code, displacement));
1639 ncode_words = code_instruction_words(code->code_size);
1640 nheader_words = code_header_words(*(lispobj *)code);
1641 nwords = ncode_words + nheader_words;
1643 constants_start_addr = code_addr + 5*N_WORD_BYTES;
1644 constants_end_addr = code_addr + nheader_words*N_WORD_BYTES;
1645 code_start_addr = code_addr + nheader_words*N_WORD_BYTES;
1646 code_end_addr = code_addr + nwords*N_WORD_BYTES;
1648 /* Work through the unboxed code. */
1649 for (p = code_start_addr; p < code_end_addr; p++) {
1650 void *data = *(void **)p;
1651 unsigned d1 = *((unsigned char *)p - 1);
1652 unsigned d2 = *((unsigned char *)p - 2);
1653 unsigned d3 = *((unsigned char *)p - 3);
1654 unsigned d4 = *((unsigned char *)p - 4);
1655 #if QSHOW
1656 unsigned d5 = *((unsigned char *)p - 5);
1657 unsigned d6 = *((unsigned char *)p - 6);
1658 #endif
1660 /* Check for code references. */
1661 /* Check for a 32 bit word that looks like an absolute
1662 reference to within the code adea of the code object. */
1663 if ((data >= (void*)(code_start_addr-displacement))
1664 && (data < (void*)(code_end_addr-displacement))) {
1665 /* function header */
1666 if ((d4 == 0x5e)
1667 && (((unsigned)p - 4 - 4*HeaderValue(*((unsigned *)p-1))) ==
1668 (unsigned)code)) {
1669 /* Skip the function header */
1670 p += 6*4 - 4 - 1;
1671 continue;
1673 /* the case of PUSH imm32 */
1674 if (d1 == 0x68) {
1675 fixup_found = 1;
1676 FSHOW((stderr,
1677 "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1678 p, d6, d5, d4, d3, d2, d1, data));
1679 FSHOW((stderr, "/PUSH $0x%.8x\n", data));
1681 /* the case of MOV [reg-8],imm32 */
1682 if ((d3 == 0xc7)
1683 && (d2==0x40 || d2==0x41 || d2==0x42 || d2==0x43
1684 || d2==0x45 || d2==0x46 || d2==0x47)
1685 && (d1 == 0xf8)) {
1686 fixup_found = 1;
1687 FSHOW((stderr,
1688 "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1689 p, d6, d5, d4, d3, d2, d1, data));
1690 FSHOW((stderr, "/MOV [reg-8],$0x%.8x\n", data));
1692 /* the case of LEA reg,[disp32] */
1693 if ((d2 == 0x8d) && ((d1 & 0xc7) == 5)) {
1694 fixup_found = 1;
1695 FSHOW((stderr,
1696 "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1697 p, d6, d5, d4, d3, d2, d1, data));
1698 FSHOW((stderr,"/LEA reg,[$0x%.8x]\n", data));
1702 /* Check for constant references. */
1703 /* Check for a 32 bit word that looks like an absolute
1704 reference to within the constant vector. Constant references
1705 will be aligned. */
1706 if ((data >= (void*)(constants_start_addr-displacement))
1707 && (data < (void*)(constants_end_addr-displacement))
1708 && (((unsigned)data & 0x3) == 0)) {
1709 /* Mov eax,m32 */
1710 if (d1 == 0xa1) {
1711 fixup_found = 1;
1712 FSHOW((stderr,
1713 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1714 p, d6, d5, d4, d3, d2, d1, data));
1715 FSHOW((stderr,"/MOV eax,0x%.8x\n", data));
1718 /* the case of MOV m32,EAX */
1719 if (d1 == 0xa3) {
1720 fixup_found = 1;
1721 FSHOW((stderr,
1722 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1723 p, d6, d5, d4, d3, d2, d1, data));
1724 FSHOW((stderr, "/MOV 0x%.8x,eax\n", data));
1727 /* the case of CMP m32,imm32 */
1728 if ((d1 == 0x3d) && (d2 == 0x81)) {
1729 fixup_found = 1;
1730 FSHOW((stderr,
1731 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1732 p, d6, d5, d4, d3, d2, d1, data));
1733 /* XX Check this */
1734 FSHOW((stderr, "/CMP 0x%.8x,immed32\n", data));
1737 /* Check for a mod=00, r/m=101 byte. */
1738 if ((d1 & 0xc7) == 5) {
1739 /* Cmp m32,reg */
1740 if (d2 == 0x39) {
1741 fixup_found = 1;
1742 FSHOW((stderr,
1743 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1744 p, d6, d5, d4, d3, d2, d1, data));
1745 FSHOW((stderr,"/CMP 0x%.8x,reg\n", data));
1747 /* the case of CMP reg32,m32 */
1748 if (d2 == 0x3b) {
1749 fixup_found = 1;
1750 FSHOW((stderr,
1751 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1752 p, d6, d5, d4, d3, d2, d1, data));
1753 FSHOW((stderr, "/CMP reg32,0x%.8x\n", data));
1755 /* the case of MOV m32,reg32 */
1756 if (d2 == 0x89) {
1757 fixup_found = 1;
1758 FSHOW((stderr,
1759 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1760 p, d6, d5, d4, d3, d2, d1, data));
1761 FSHOW((stderr, "/MOV 0x%.8x,reg32\n", data));
1763 /* the case of MOV reg32,m32 */
1764 if (d2 == 0x8b) {
1765 fixup_found = 1;
1766 FSHOW((stderr,
1767 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1768 p, d6, d5, d4, d3, d2, d1, data));
1769 FSHOW((stderr, "/MOV reg32,0x%.8x\n", data));
1771 /* the case of LEA reg32,m32 */
1772 if (d2 == 0x8d) {
1773 fixup_found = 1;
1774 FSHOW((stderr,
1775 "abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1776 p, d6, d5, d4, d3, d2, d1, data));
1777 FSHOW((stderr, "/LEA reg32,0x%.8x\n", data));
1783 /* If anything was found, print some information on the code
1784 * object. */
1785 if (fixup_found) {
1786 FSHOW((stderr,
1787 "/compiled code object at %x: header words = %d, code words = %d\n",
1788 code, nheader_words, ncode_words));
1789 FSHOW((stderr,
1790 "/const start = %x, end = %x\n",
1791 constants_start_addr, constants_end_addr));
1792 FSHOW((stderr,
1793 "/code start = %x, end = %x\n",
1794 code_start_addr, code_end_addr));
1797 #endif
1799 #ifdef LISP_FEATURE_X86
1800 void
1801 gencgc_apply_code_fixups(struct code *old_code, struct code *new_code)
1803 sword_t nheader_words, ncode_words, nwords;
1804 os_vm_address_t __attribute__((unused)) constants_start_addr, constants_end_addr;
1805 os_vm_address_t __attribute__((unused)) code_start_addr, code_end_addr;
1806 os_vm_address_t code_addr = (os_vm_address_t)new_code;
1807 os_vm_address_t old_addr = (os_vm_address_t)old_code;
1808 os_vm_size_t displacement = code_addr - old_addr;
1809 lispobj fixups = NIL;
1810 struct vector *fixups_vector;
1812 ncode_words = code_instruction_words(new_code->code_size);
1813 nheader_words = code_header_words(*(lispobj *)new_code);
1814 nwords = ncode_words + nheader_words;
1815 /* FSHOW((stderr,
1816 "/compiled code object at %x: header words = %d, code words = %d\n",
1817 new_code, nheader_words, ncode_words)); */
1818 constants_start_addr = code_addr + 5*N_WORD_BYTES;
1819 constants_end_addr = code_addr + nheader_words*N_WORD_BYTES;
1820 code_start_addr = code_addr + nheader_words*N_WORD_BYTES;
1821 code_end_addr = code_addr + nwords*N_WORD_BYTES;
1823 FSHOW((stderr,
1824 "/const start = %x, end = %x\n",
1825 constants_start_addr,constants_end_addr));
1826 FSHOW((stderr,
1827 "/code start = %x; end = %x\n",
1828 code_start_addr,code_end_addr));
1831 fixups = new_code->fixups;
1832 /* It will be a Lisp vector if valid, or 0 if there are no fixups */
1833 if (fixups == 0 || !is_lisp_pointer(fixups)) {
1834 /* Check for possible errors. */
1835 if (check_code_fixups)
1836 sniff_code_object(new_code, displacement);
1838 return;
1841 fixups_vector = (struct vector *)native_pointer(fixups);
1843 /* Could be pointing to a forwarding pointer. */
1844 /* This is extremely unlikely, because the only referent of the fixups
1845 is usually the code itself; so scavenging the vector won't occur
1846 until after the code object is known to be live. As we're just now
1847 enlivening the code, the fixups shouldn't have been forwarded.
1848 Maybe the vector is on the special binding stack though ... */
1849 if (is_lisp_pointer(fixups) &&
1850 (find_page_index((void*)fixups_vector) != -1) &&
1851 forwarding_pointer_p((lispobj*)fixups_vector)) {
1852 /* If so, then follow it. */
1853 /*SHOW("following pointer to a forwarding pointer");*/
1854 fixups_vector = (struct vector *)
1855 native_pointer(forwarding_pointer_value((lispobj*)fixups_vector));
1858 /*SHOW("got fixups");*/
1860 if (widetag_of(fixups_vector->header) == SIMPLE_ARRAY_WORD_WIDETAG) {
1861 /* Got the fixups for the code block. Now work through the vector,
1862 and apply a fixup at each address. */
1863 sword_t length = fixnum_value(fixups_vector->length);
1864 sword_t i;
1865 for (i = 0; i < length; i++) {
1866 long offset = fixups_vector->data[i];
1867 /* Now check the current value of offset. */
1868 os_vm_address_t old_value = *(os_vm_address_t *)(code_start_addr + offset);
1870 /* If it's within the old_code object then it must be an
1871 * absolute fixup (relative ones are not saved) */
1872 if ((old_value >= old_addr)
1873 && (old_value < (old_addr + nwords*N_WORD_BYTES)))
1874 /* So add the dispacement. */
1875 *(os_vm_address_t *)(code_start_addr + offset) =
1876 old_value + displacement;
1877 else
1878 /* It is outside the old code object so it must be a
1879 * relative fixup (absolute fixups are not saved). So
1880 * subtract the displacement. */
1881 *(os_vm_address_t *)(code_start_addr + offset) =
1882 old_value - displacement;
1884 } else {
1885 /* This used to just print a note to stderr, but a bogus fixup seems to
1886 * indicate real heap corruption, so a hard hailure is in order. */
1887 lose("fixup vector %p has a bad widetag: %d\n",
1888 fixups_vector, widetag_of(fixups_vector->header));
1891 /* Check for possible errors. */
1892 if (check_code_fixups) {
1893 sniff_code_object(new_code,displacement);
1896 #endif
1898 static lispobj
1899 trans_boxed_large(lispobj object)
1901 gc_assert(is_lisp_pointer(object));
1902 return copy_large_object(object,
1903 (HeaderValue(*native_pointer(object)) | 1) + 1);
1907 * weak pointers
1910 /* XX This is a hack adapted from cgc.c. These don't work too
1911 * efficiently with the gencgc as a list of the weak pointers is
1912 * maintained within the objects which causes writes to the pages. A
1913 * limited attempt is made to avoid unnecessary writes, but this needs
1914 * a re-think. */
1915 /* FIXME: now that we have non-Lisp hashtables in the GC, it might make sense
1916 * to stop chaining weak pointers through a slot in the object, as a remedy to
1917 * the above concern. It would also shorten the object by 2 words. */
1918 static sword_t
1919 scav_weak_pointer(lispobj *where, lispobj object)
1921 /* Since we overwrite the 'next' field, we have to make
1922 * sure not to do so for pointers already in the list.
1923 * Instead of searching the list of weak_pointers each
1924 * time, we ensure that next is always NULL when the weak
1925 * pointer isn't in the list, and not NULL otherwise.
1926 * Since we can't use NULL to denote end of list, we
1927 * use a pointer back to the same weak_pointer.
1929 struct weak_pointer * wp = (struct weak_pointer*)where;
1931 if (NULL == wp->next && weak_pointer_breakable_p(wp)) {
1932 wp->next = weak_pointers;
1933 weak_pointers = wp;
1934 if (NULL == wp->next)
1935 wp->next = wp;
1938 /* Do not let GC scavenge the value slot of the weak pointer.
1939 * (That is why it is a weak pointer.) */
1941 return WEAK_POINTER_NWORDS;
1945 lispobj *
1946 search_read_only_space(void *pointer)
1948 lispobj *start = (lispobj *) READ_ONLY_SPACE_START;
1949 lispobj *end = (lispobj *) SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0);
1950 if ((pointer < (void *)start) || (pointer >= (void *)end))
1951 return NULL;
1952 return gc_search_space(start, pointer);
1955 lispobj *
1956 search_static_space(void *pointer)
1958 lispobj *start = (lispobj *)STATIC_SPACE_START;
1959 lispobj *end = (lispobj *)SymbolValue(STATIC_SPACE_FREE_POINTER,0);
1960 if ((pointer < (void *)start) || (pointer >= (void *)end))
1961 return NULL;
1962 return gc_search_space(start, pointer);
1965 /* a faster version for searching the dynamic space. This will work even
1966 * if the object is in a current allocation region. */
1967 lispobj *
1968 search_dynamic_space(void *pointer)
1970 page_index_t page_index = find_page_index(pointer);
1971 lispobj *start;
1973 /* The address may be invalid, so do some checks. */
1974 if ((page_index == -1) || page_free_p(page_index))
1975 return NULL;
1976 start = (lispobj *)page_scan_start(page_index);
1977 return gc_search_space(start, pointer);
1980 // Return the starting address of the object containing 'addr'
1981 // if and only if the object is one which would be evacuated from 'from_space'
1982 // were it allowed to be either discarded as garbage or moved.
1983 // 'addr_page_index' is the page containing 'addr' and must not be -1.
1984 // Return 0 if there is no such object - that is, if addr is past the
1985 // end of the used bytes, or its pages are not in 'from_space' etc.
1986 static lispobj*
1987 conservative_root_p(void *addr, page_index_t addr_page_index)
1989 #ifdef GENCGC_IS_PRECISE
1990 /* If we're in precise gencgc (non-x86oid as of this writing) then
1991 * we are only called on valid object pointers in the first place,
1992 * so we just have to do a bounds-check against the heap, a
1993 * generation check, and the already-pinned check. */
1994 if ((page_table[addr_page_index].gen != from_space)
1995 || (page_table[addr_page_index].dont_move != 0))
1996 return 0;
1997 return (lispobj*)1;
1998 #else
1999 /* quick check 1: Address is quite likely to have been invalid. */
2000 if (page_free_p(addr_page_index)
2001 || (page_bytes_used(addr_page_index) == 0)
2002 || (page_table[addr_page_index].gen != from_space))
2003 return 0;
2004 gc_assert(!(page_table[addr_page_index].allocated&OPEN_REGION_PAGE_FLAG));
2006 /* quick check 2: Check the offset within the page.
2009 if (((uword_t)addr & (GENCGC_CARD_BYTES - 1)) > page_bytes_used(addr_page_index))
2010 return 0;
2012 /* Filter out anything which can't be a pointer to a Lisp object
2013 * (or, as a special case which also requires dont_move, a return
2014 * address referring to something in a CodeObject). This is
2015 * expensive but important, since it vastly reduces the
2016 * probability that random garbage will be bogusly interpreted as
2017 * a pointer which prevents a page from moving. */
2018 lispobj* object_start = search_dynamic_space(addr);
2019 if (!object_start) return 0;
2021 /* If the containing object is a code object and 'addr' points
2022 * anywhere beyond the boxed words,
2023 * presume it to be a valid unboxed return address. */
2024 if (instruction_ptr_p(addr, object_start))
2025 return object_start;
2027 /* Large object pages only contain ONE object, and it will never
2028 * be a CONS. However, arrays and bignums can be allocated larger
2029 * than necessary and then shrunk to fit, leaving what look like
2030 * (0 . 0) CONSes at the end. These appear valid to
2031 * properly_tagged_descriptor_p(), so pick them off here. */
2032 if (((lowtag_of((lispobj)addr) == LIST_POINTER_LOWTAG) &&
2033 page_table[addr_page_index].large_object)
2034 || !properly_tagged_descriptor_p(addr, object_start))
2035 return 0;
2037 return object_start;
2038 #endif
2041 /* Adjust large bignum and vector objects. This will adjust the
2042 * allocated region if the size has shrunk, and move unboxed objects
2043 * into unboxed pages. The pages are not promoted here, and the
2044 * promoted region is not added to the new_regions; this is really
2045 * only designed to be called from preserve_pointer(). Shouldn't fail
2046 * if this is missed, just may delay the moving of objects to unboxed
2047 * pages, and the freeing of pages. */
2048 static void
2049 maybe_adjust_large_object(lispobj *where)
2051 page_index_t first_page;
2052 page_index_t next_page;
2053 sword_t nwords;
2055 uword_t remaining_bytes;
2056 uword_t bytes_freed;
2057 uword_t old_bytes_used;
2059 int boxed;
2061 /* Check whether it's a vector or bignum object. */
2062 lispobj widetag = widetag_of(where[0]);
2063 if (widetag == SIMPLE_VECTOR_WIDETAG)
2064 boxed = BOXED_PAGE_FLAG;
2065 else if (specialized_vector_widetag_p(widetag) || widetag == BIGNUM_WIDETAG)
2066 boxed = UNBOXED_PAGE_FLAG;
2067 else
2068 return;
2070 /* Find its current size. */
2071 nwords = sizetab[widetag](where);
2073 first_page = find_page_index((void *)where);
2074 gc_assert(first_page >= 0);
2076 /* Note: Any page write-protection must be removed, else a later
2077 * scavenge_newspace may incorrectly not scavenge these pages.
2078 * This would not be necessary if they are added to the new areas,
2079 * but lets do it for them all (they'll probably be written
2080 * anyway?). */
2082 gc_assert(page_starts_contiguous_block_p(first_page));
2084 next_page = first_page;
2085 remaining_bytes = nwords*N_WORD_BYTES;
2086 while (remaining_bytes > GENCGC_CARD_BYTES) {
2087 gc_assert(page_table[next_page].gen == from_space);
2088 gc_assert(page_allocated_no_region_p(next_page));
2089 gc_assert(page_table[next_page].large_object);
2090 gc_assert(page_scan_start_offset(next_page) ==
2091 npage_bytes(next_page-first_page));
2092 gc_assert(page_bytes_used(next_page) == GENCGC_CARD_BYTES);
2094 page_table[next_page].allocated = boxed;
2096 /* Shouldn't be write-protected at this stage. Essential that the
2097 * pages aren't. */
2098 gc_assert(!page_table[next_page].write_protected);
2099 remaining_bytes -= GENCGC_CARD_BYTES;
2100 next_page++;
2103 /* Now only one page remains, but the object may have shrunk so
2104 * there may be more unused pages which will be freed. */
2106 /* Object may have shrunk but shouldn't have grown - check. */
2107 gc_assert(page_bytes_used(next_page) >= remaining_bytes);
2109 page_table[next_page].allocated = boxed;
2110 gc_assert(page_table[next_page].allocated ==
2111 page_table[first_page].allocated);
2113 /* Adjust the bytes_used. */
2114 old_bytes_used = page_bytes_used(next_page);
2115 set_page_bytes_used(next_page, remaining_bytes);
2117 bytes_freed = old_bytes_used - remaining_bytes;
2119 /* Free any remaining pages; needs care. */
2120 next_page++;
2121 while ((old_bytes_used == GENCGC_CARD_BYTES) &&
2122 (page_table[next_page].gen == from_space) &&
2123 page_allocated_no_region_p(next_page) &&
2124 page_table[next_page].large_object &&
2125 (page_scan_start_offset(next_page) ==
2126 npage_bytes(next_page - first_page))) {
2127 /* It checks out OK, free the page. We don't need to both zeroing
2128 * pages as this should have been done before shrinking the
2129 * object. These pages shouldn't be write protected as they
2130 * should be zero filled. */
2131 gc_assert(page_table[next_page].write_protected == 0);
2133 old_bytes_used = page_bytes_used(next_page);
2134 page_table[next_page].allocated = FREE_PAGE_FLAG;
2135 set_page_bytes_used(next_page, 0);
2136 bytes_freed += old_bytes_used;
2137 next_page++;
2140 if ((bytes_freed > 0) && gencgc_verbose) {
2141 FSHOW((stderr,
2142 "/maybe_adjust_large_object() freed %d\n",
2143 bytes_freed));
2146 generations[from_space].bytes_allocated -= bytes_freed;
2147 bytes_allocated -= bytes_freed;
2149 return;
2152 #if !(defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64))
2153 # define hopscotch_init()
2154 # define hopscotch_reset(a)
2155 # define scavenge_pinned_ranges()
2156 # define wipe_nonpinned_words()
2157 # define hopscotch_create(a,b,c,d)
2158 # define hopscotch_log_stats(a)
2159 /* After scavenging of the roots is done, we go back to the pinned objects
2160 * and look within them for pointers. While heap_scavenge() could certainly
2161 * do this, it would potentially lead to extra work, since we can't know
2162 * whether any given object has been examined at least once, since there is
2163 * no telltale forwarding-pointer. The easiest thing to do is defer all
2164 * pinned objects to a subsequent pass, as is done here.
2166 #else
2167 static void
2168 scavenge_pinned_ranges()
2170 int i;
2171 lispobj key;
2172 for_each_hopscotch_key(i, key, pinned_objects) {
2173 lispobj* obj = native_pointer(key);
2174 lispobj header = *obj;
2175 // Never invoke scavenger on a simple-fun, just code components.
2176 if (is_cons_half(header))
2177 scavenge(obj, 2);
2178 else if (widetag_of(header) != SIMPLE_FUN_HEADER_WIDETAG)
2179 scavtab[widetag_of(header)](obj, header);
2183 static int addrcmp(const void* a, const void* b) { // For qsort()
2184 sword_t diff = *(uword_t*)a - *(uword_t*)b;
2185 return diff < 0 ? -1 : (diff > 0 ? 1 : 0);
2188 /* Zero out the byte ranges on small object pages marked dont_move,
2189 * carefully skipping over objects in the pin hashtable.
2190 * TODO: by recording an additional bit per page indicating whether
2191 * there is more than one pinned object on it, we could avoid qsort()
2192 * except in the case where there is more than one. */
2193 static void
2194 wipe_nonpinned_words()
2196 // Loop over the keys in pinned_objects and pack them densely into
2197 // the same array - pinned_objects.keys[] - but skip any simple-funs.
2198 // Admittedly this is abstraction breakage.
2199 int limit = hopscotch_max_key_index(pinned_objects);
2200 int n_pins = 0, i;
2201 for (i = 0; i <= limit; ++i) {
2202 lispobj key = pinned_objects.keys[i];
2203 if (key) {
2204 lispobj* obj = native_pointer(key);
2205 // No need to check for is_cons_half() - it will be false
2206 // on a simple-fun header, and that's the correct answer.
2207 if (widetag_of(*obj) != SIMPLE_FUN_HEADER_WIDETAG)
2208 pinned_objects.keys[n_pins++] = (uword_t)obj;
2211 // Store a sentinel at the end. Even if n_pins = table capacity (unlikely),
2212 // it is safe to write one more word, because the hops[] array immediately
2213 // follows the keys[] array in memory. At worst, 2 elements of hops[]
2214 // are clobbered, which is irrelevant since the table has already been
2215 // rendered unusable by stealing its key array for a different purpose.
2216 pinned_objects.keys[n_pins] = 0;
2217 // Order by ascending address, stopping short of the sentinel.
2218 qsort(pinned_objects.keys, n_pins, sizeof (uword_t), addrcmp);
2219 #if 0
2220 printf("Sorted pin list:\n");
2221 for (i = 0; i < n_pins; ++i) {
2222 lispobj* obj = (lispobj*)pinned_objects.keys[i];
2223 if (!is_cons_half(*obj))
2224 printf("%p: %5d words\n", obj, (int)sizetab[widetag_of(*obj)](obj));
2225 else printf("%p: CONS\n", obj);
2227 #endif
2228 // Each entry in the pinned objects demarcates two ranges to be cleared:
2229 // - the range preceding it back to either the page start, or prior object.
2230 // - the range after it, up to the lesser of page bytes used or next object.
2231 uword_t preceding_object = 0;
2232 uword_t this_page_end = 0;
2233 #define page_base_address(x) (x&~(GENCGC_CARD_BYTES-1))
2234 for (i = 0; i < n_pins; ++i) {
2235 // Handle the preceding range. If this object is on the same page as
2236 // its predecessor, then intervening bytes were already zeroed.
2237 // If not, then start a new page and do some bookkeeping.
2238 lispobj* obj = (lispobj*)pinned_objects.keys[i];
2239 uword_t this_page_base = page_base_address((uword_t)obj);
2240 /* printf("i=%d obj=%p base=%p\n", i, obj, (void*)this_page_base); */
2241 if (this_page_base > page_base_address(preceding_object)) {
2242 bzero((void*)this_page_base, (uword_t)obj - this_page_base);
2243 // Move the page to newspace
2244 page_index_t page = find_page_index(obj);
2245 int used = page_bytes_used(page);
2246 this_page_end = this_page_base + used;
2247 /* printf(" Clearing %p .. %p (limit=%p)\n",
2248 (void*)this_page_base, obj, (void*)this_page_end); */
2249 generations[new_space].bytes_allocated += used;
2250 generations[page_table[page].gen].bytes_allocated -= used;
2251 page_table[page].gen = new_space;
2252 page_table[page].has_pins = 0;
2254 // Handle the following range.
2255 lispobj word = *obj;
2256 size_t nwords = is_cons_half(word) ? 2 : sizetab[widetag_of(word)](obj);
2257 uword_t range_start = (uword_t)(obj + nwords);
2258 uword_t range_end = this_page_end;
2259 // There is always an i+1'th key due to the sentinel value.
2260 if (page_base_address(pinned_objects.keys[i+1]) == this_page_base)
2261 range_end = pinned_objects.keys[i+1];
2262 /* printf(" Clearing %p .. %p\n", (void*)range_start, (void*)range_end); */
2263 bzero((void*)range_start, range_end - range_start);
2264 preceding_object = (uword_t)obj;
2268 /* Add 'object' to the hashtable, and if the object is a code component,
2269 * then also add all of the embedded simple-funs.
2270 * The rationale for the extra work on code components is that without it,
2271 * every test of pinned_p() on an object would have to check if the pointer
2272 * is to a simple-fun - entailing an extra read of the header - and mapping
2273 * to its code component if so. Since more calls to pinned_p occur than to
2274 * pin_object, the extra burden should be on this function.
2275 * Experimentation bears out that this is the better technique.
2276 * Also, we wouldn't often expect code components in the collected generation
2277 * so the extra work here is quite minimal, even if it can generally add to
2278 * the number of keys in the hashtable.
2280 static void
2281 pin_object(lispobj object)
2283 if (!hopscotch_containsp(&pinned_objects, object)) {
2284 hopscotch_put(&pinned_objects, object, 1);
2285 struct code* maybe_code = (struct code*)native_pointer(object);
2286 if (widetag_of(maybe_code->header) == CODE_HEADER_WIDETAG) {
2287 for_each_simple_fun(i, fun, maybe_code, 0, {
2288 hopscotch_put(&pinned_objects,
2289 make_lispobj(fun, FUN_POINTER_LOWTAG),
2295 #endif
2297 /* Take a possible pointer to a Lisp object and mark its page in the
2298 * page_table so that it will not be relocated during a GC.
2300 * This involves locating the page it points to, then backing up to
2301 * the start of its region, then marking all pages dont_move from there
2302 * up to the first page that's not full or has a different generation
2304 * It is assumed that all the page static flags have been cleared at
2305 * the start of a GC.
2307 * It is also assumed that the current gc_alloc() region has been
2308 * flushed and the tables updated. */
2310 // TODO: there's probably a way to be a little more efficient here.
2311 // As things are, we start by finding the object that encloses 'addr',
2312 // then we see if 'addr' was a "valid" Lisp pointer to that object
2313 // - meaning we expect the correct lowtag on the pointer - except
2314 // that for code objects we don't require a correct lowtag
2315 // and we allow a pointer to anywhere in the object.
2317 // It should be possible to avoid calling search_dynamic_space
2318 // more of the time. First, check if the page pointed to might hold code.
2319 // If it does, then we continue regardless of the pointer's lowtag
2320 // (because of the special allowance). If the page definitely does *not*
2321 // hold code, then we require up front that the lowtake make sense,
2322 // by doing the same checks that are in properly_tagged_descriptor_p.
2324 // Problem: when code is allocated from a per-thread region,
2325 // does it ensure that the occupied pages are flagged as having code?
2327 static void
2328 preserve_pointer(void *addr)
2330 #ifdef LISP_FEATURE_IMMOBILE_SPACE
2331 /* Immobile space MUST be lower than dynamic space,
2332 or else this test needs to be revised */
2333 if (addr < (void*)IMMOBILE_SPACE_END) {
2334 extern void immobile_space_preserve_pointer(void*);
2335 immobile_space_preserve_pointer(addr);
2336 return;
2338 #endif
2339 page_index_t addr_page_index = find_page_index(addr);
2340 lispobj *object_start;
2342 if (addr_page_index == -1
2343 || (object_start = conservative_root_p(addr, addr_page_index)) == 0)
2344 return;
2346 /* (Now that we know that addr_page_index is in range, it's
2347 * safe to index into page_table[] with it.) */
2348 unsigned int region_allocation = page_table[addr_page_index].allocated;
2350 /* Find the beginning of the region. Note that there may be
2351 * objects in the region preceding the one that we were passed a
2352 * pointer to: if this is the case, we will write-protect all the
2353 * previous objects' pages too. */
2355 #if 0
2356 /* I think this'd work just as well, but without the assertions.
2357 * -dan 2004.01.01 */
2358 page_index_t first_page = find_page_index(page_scan_start(addr_page_index))
2359 #else
2360 page_index_t first_page = addr_page_index;
2361 while (!page_starts_contiguous_block_p(first_page)) {
2362 --first_page;
2363 /* Do some checks. */
2364 gc_assert(page_bytes_used(first_page) == GENCGC_CARD_BYTES);
2365 gc_assert(page_table[first_page].gen == from_space);
2366 gc_assert(page_table[first_page].allocated == region_allocation);
2368 #endif
2370 /* Adjust any large objects before promotion as they won't be
2371 * copied after promotion. */
2372 if (page_table[first_page].large_object) {
2373 maybe_adjust_large_object(page_address(first_page));
2374 /* It may have moved to unboxed pages. */
2375 region_allocation = page_table[first_page].allocated;
2378 /* Now work forward until the end of this contiguous area is found,
2379 * marking all pages as dont_move. */
2380 page_index_t i;
2381 for (i = first_page; ;i++) {
2382 gc_assert(page_table[i].allocated == region_allocation);
2384 /* Mark the page static. */
2385 page_table[i].dont_move = 1;
2387 /* It is essential that the pages are not write protected as
2388 * they may have pointers into the old-space which need
2389 * scavenging. They shouldn't be write protected at this
2390 * stage. */
2391 gc_assert(!page_table[i].write_protected);
2393 /* Check whether this is the last page in this contiguous block.. */
2394 if (page_ends_contiguous_block_p(i, from_space))
2395 break;
2398 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
2399 /* Do not do this for multi-page objects. Those pages do not need
2400 * object wipeout anyway.
2402 if (do_wipe_p && i == first_page) { // single-page object
2403 lispobj word = *object_start;
2404 int lowtag = is_cons_half(word) ?
2405 LIST_POINTER_LOWTAG : lowtag_for_widetag[widetag_of(word)>>2];
2406 pin_object(make_lispobj(object_start, lowtag));
2407 page_table[i].has_pins = 1;
2409 #endif
2411 /* Check that the page is now static. */
2412 gc_assert(page_table[addr_page_index].dont_move != 0);
2415 /* If the given page is not write-protected, then scan it for pointers
2416 * to younger generations or the top temp. generation, if no
2417 * suspicious pointers are found then the page is write-protected.
2419 * Care is taken to check for pointers to the current gc_alloc()
2420 * region if it is a younger generation or the temp. generation. This
2421 * frees the caller from doing a gc_alloc_update_page_tables(). Actually
2422 * the gc_alloc_generation does not need to be checked as this is only
2423 * called from scavenge_generation() when the gc_alloc generation is
2424 * younger, so it just checks if there is a pointer to the current
2425 * region.
2427 * We return 1 if the page was write-protected, else 0. */
2428 static int
2429 update_page_write_prot(page_index_t page)
2431 generation_index_t gen = page_table[page].gen;
2432 sword_t j;
2433 int wp_it = 1;
2434 void **page_addr = (void **)page_address(page);
2435 sword_t num_words = page_bytes_used(page) / N_WORD_BYTES;
2437 /* Shouldn't be a free page. */
2438 gc_assert(!page_free_p(page));
2439 gc_assert(page_bytes_used(page) != 0);
2441 /* Skip if it's already write-protected, pinned, or unboxed */
2442 if (page_table[page].write_protected
2443 /* FIXME: What's the reason for not write-protecting pinned pages? */
2444 || page_table[page].dont_move
2445 || page_unboxed_p(page))
2446 return (0);
2448 /* Scan the page for pointers to younger generations or the
2449 * top temp. generation. */
2451 /* This is conservative: any word satisfying is_lisp_pointer() is
2452 * assumed to be a pointer. To do otherwise would require a family
2453 * of scavenge-like functions. */
2454 for (j = 0; j < num_words; j++) {
2455 void *ptr = *(page_addr+j);
2456 page_index_t index;
2457 lispobj __attribute__((unused)) header;
2459 if (!is_lisp_pointer((lispobj)ptr))
2460 continue;
2461 /* Check that it's in the dynamic space */
2462 if ((index = find_page_index(ptr)) != -1) {
2463 if (/* Does it point to a younger or the temp. generation? */
2464 (!page_free_p(index)
2465 && (page_bytes_used(index) != 0)
2466 && ((page_table[index].gen < gen)
2467 || (page_table[index].gen == SCRATCH_GENERATION)))
2469 /* Or does it point within a current gc_alloc() region? */
2470 || ((boxed_region.start_addr <= ptr)
2471 && (ptr <= boxed_region.free_pointer))
2472 || ((unboxed_region.start_addr <= ptr)
2473 && (ptr <= unboxed_region.free_pointer))) {
2474 wp_it = 0;
2475 break;
2478 #ifdef LISP_FEATURE_IMMOBILE_SPACE
2479 else if ((index = find_immobile_page_index(ptr)) >= 0 &&
2480 other_immediate_lowtag_p(header = *native_pointer((lispobj)ptr))) {
2481 // This is *possibly* a pointer to an object in immobile space,
2482 // given that above two conditions were satisfied.
2483 // But unlike in the dynamic space case, we need to read a byte
2484 // from the object to determine its generation, which requires care.
2485 // Consider an unboxed word that looks like a pointer to a word that
2486 // looks like fun-header-widetag. We can't naively back up to the
2487 // underlying code object since the alleged header might not be one.
2488 int obj_gen = gen; // Make comparison fail if we fall through
2489 if (lowtag_of((lispobj)ptr) != FUN_POINTER_LOWTAG) {
2490 obj_gen = __immobile_obj_generation(native_pointer((lispobj)ptr));
2491 } else if (widetag_of(header) == SIMPLE_FUN_HEADER_WIDETAG) {
2492 lispobj* code = fun_code_header((lispobj)ptr - FUN_POINTER_LOWTAG);
2493 // This is a heuristic, since we're not actually looking for
2494 // an object boundary. Precise scanning of 'page' would obviate
2495 // the guard conditions here.
2496 if ((lispobj)code >= IMMOBILE_VARYOBJ_SUBSPACE_START
2497 && widetag_of(*code) == CODE_HEADER_WIDETAG)
2498 obj_gen = __immobile_obj_generation(code);
2500 // A bogus generation number implies a not-really-pointer,
2501 // but it won't cause misbehavior.
2502 if (obj_gen < gen || obj_gen == SCRATCH_GENERATION) {
2503 wp_it = 0;
2504 break;
2507 #endif
2510 if (wp_it == 1) {
2511 /* Write-protect the page. */
2512 /*FSHOW((stderr, "/write-protecting page %d gen %d\n", page, gen));*/
2514 os_protect((void *)page_addr,
2515 GENCGC_CARD_BYTES,
2516 OS_VM_PROT_READ|OS_VM_PROT_EXECUTE);
2518 /* Note the page as protected in the page tables. */
2519 page_table[page].write_protected = 1;
2522 return (wp_it);
2525 /* Is this page holding a normal (non-hashtable) large-object
2526 * simple-vector? */
2527 static inline boolean large_simple_vector_p(page_index_t page) {
2528 if (!page_table[page].large_object)
2529 return 0;
2530 lispobj object = *(lispobj *)page_address(page);
2531 return widetag_of(object) == SIMPLE_VECTOR_WIDETAG &&
2532 (HeaderValue(object) & 0xFF) == subtype_VectorNormal;
2536 /* Scavenge all generations from FROM to TO, inclusive, except for
2537 * new_space which needs special handling, as new objects may be
2538 * added which are not checked here - use scavenge_newspace generation.
2540 * Write-protected pages should not have any pointers to the
2541 * from_space so do need scavenging; thus write-protected pages are
2542 * not always scavenged. There is some code to check that these pages
2543 * are not written; but to check fully the write-protected pages need
2544 * to be scavenged by disabling the code to skip them.
2546 * Under the current scheme when a generation is GCed the younger
2547 * generations will be empty. So, when a generation is being GCed it
2548 * is only necessary to scavenge the older generations for pointers
2549 * not the younger. So a page that does not have pointers to younger
2550 * generations does not need to be scavenged.
2552 * The write-protection can be used to note pages that don't have
2553 * pointers to younger pages. But pages can be written without having
2554 * pointers to younger generations. After the pages are scavenged here
2555 * they can be scanned for pointers to younger generations and if
2556 * there are none the page can be write-protected.
2558 * One complication is when the newspace is the top temp. generation.
2560 * Enabling SC_GEN_CK scavenges the write-protected pages and checks
2561 * that none were written, which they shouldn't be as they should have
2562 * no pointers to younger generations. This breaks down for weak
2563 * pointers as the objects contain a link to the next and are written
2564 * if a weak pointer is scavenged. Still it's a useful check. */
2565 static void
2566 scavenge_generations(generation_index_t from, generation_index_t to)
2568 page_index_t i;
2569 page_index_t num_wp = 0;
2571 #define SC_GEN_CK 0
2572 #if SC_GEN_CK
2573 /* Clear the write_protected_cleared flags on all pages. */
2574 for (i = 0; i < page_table_pages; i++)
2575 page_table[i].write_protected_cleared = 0;
2576 #endif
2578 for (i = 0; i < last_free_page; i++) {
2579 generation_index_t generation = page_table[i].gen;
2580 if (page_boxed_p(i)
2581 && (page_bytes_used(i) != 0)
2582 && (generation != new_space)
2583 && (generation >= from)
2584 && (generation <= to)) {
2585 page_index_t last_page,j;
2586 int write_protected=1;
2588 /* This should be the start of a region */
2589 gc_assert(page_starts_contiguous_block_p(i));
2591 if (large_simple_vector_p(i)) {
2592 /* Scavenge only the unprotected pages of a
2593 * large-object vector, other large objects could be
2594 * handled as well, but vectors are easier to deal
2595 * with and are more likely to grow to very large
2596 * sizes where avoiding scavenging the whole thing is
2597 * worthwile */
2598 if (!page_table[i].write_protected) {
2599 scavenge((lispobj*)page_address(i) + 2,
2600 GENCGC_CARD_BYTES / N_WORD_BYTES - 2);
2601 update_page_write_prot(i);
2603 for (last_page = i + 1; ; last_page++) {
2604 lispobj* start = page_address(last_page);
2605 write_protected = page_table[last_page].write_protected;
2606 if (page_ends_contiguous_block_p(last_page, generation)) {
2607 if (!write_protected) {
2608 scavenge(start, page_bytes_used(last_page) / N_WORD_BYTES);
2609 update_page_write_prot(last_page);
2611 break;
2613 if (!write_protected) {
2614 scavenge(start, GENCGC_CARD_BYTES / N_WORD_BYTES);
2615 update_page_write_prot(last_page);
2618 } else {
2619 /* Now work forward until the end of the region */
2620 for (last_page = i; ; last_page++) {
2621 write_protected =
2622 write_protected && page_table[last_page].write_protected;
2623 if (page_ends_contiguous_block_p(last_page, generation))
2624 break;
2626 if (!write_protected) {
2627 heap_scavenge(page_address(i),
2628 (lispobj*)((char*)page_address(last_page)
2629 + page_bytes_used(last_page)));
2631 /* Now scan the pages and write protect those that
2632 * don't have pointers to younger generations. */
2633 if (enable_page_protection) {
2634 for (j = i; j <= last_page; j++) {
2635 num_wp += update_page_write_prot(j);
2638 if ((gencgc_verbose > 1) && (num_wp != 0)) {
2639 FSHOW((stderr,
2640 "/write protected %d pages within generation %d\n",
2641 num_wp, generation));
2645 i = last_page;
2649 #if SC_GEN_CK
2650 /* Check that none of the write_protected pages in this generation
2651 * have been written to. */
2652 for (i = 0; i < page_table_pages; i++) {
2653 if (!page_free_p(i)
2654 && (page_bytes_used(i) != 0)
2655 && (page_table[i].gen == generation)
2656 && (page_table[i].write_protected_cleared != 0)) {
2657 FSHOW((stderr, "/scavenge_generation() %d\n", generation));
2658 FSHOW((stderr,
2659 "/page bytes_used=%d scan_start_offset=%lu dont_move=%d\n",
2660 page_bytes_used(i),
2661 scan_start_offset(page_table[i]),
2662 page_table[i].dont_move));
2663 lose("write to protected page %d in scavenge_generation()\n", i);
2666 #endif
2670 /* Scavenge a newspace generation. As it is scavenged new objects may
2671 * be allocated to it; these will also need to be scavenged. This
2672 * repeats until there are no more objects unscavenged in the
2673 * newspace generation.
2675 * To help improve the efficiency, areas written are recorded by
2676 * gc_alloc() and only these scavenged. Sometimes a little more will be
2677 * scavenged, but this causes no harm. An easy check is done that the
2678 * scavenged bytes equals the number allocated in the previous
2679 * scavenge.
2681 * Write-protected pages are not scanned except if they are marked
2682 * dont_move in which case they may have been promoted and still have
2683 * pointers to the from space.
2685 * Write-protected pages could potentially be written by alloc however
2686 * to avoid having to handle re-scavenging of write-protected pages
2687 * gc_alloc() does not write to write-protected pages.
2689 * New areas of objects allocated are recorded alternatively in the two
2690 * new_areas arrays below. */
2691 static struct new_area new_areas_1[NUM_NEW_AREAS];
2692 static struct new_area new_areas_2[NUM_NEW_AREAS];
2694 #ifdef LISP_FEATURE_IMMOBILE_SPACE
2695 extern unsigned int immobile_scav_queue_count;
2696 extern void
2697 gc_init_immobile(),
2698 update_immobile_nursery_bits(),
2699 scavenge_immobile_roots(generation_index_t,generation_index_t),
2700 scavenge_immobile_newspace(),
2701 sweep_immobile_space(int raise),
2702 write_protect_immobile_space();
2703 #else
2704 #define immobile_scav_queue_count 0
2705 #endif
2707 /* Do one full scan of the new space generation. This is not enough to
2708 * complete the job as new objects may be added to the generation in
2709 * the process which are not scavenged. */
2710 static void
2711 scavenge_newspace_generation_one_scan(generation_index_t generation)
2713 page_index_t i;
2715 FSHOW((stderr,
2716 "/starting one full scan of newspace generation %d\n",
2717 generation));
2718 for (i = 0; i < last_free_page; i++) {
2719 /* Note that this skips over open regions when it encounters them. */
2720 if (page_boxed_p(i)
2721 && (page_bytes_used(i) != 0)
2722 && (page_table[i].gen == generation)
2723 && ((page_table[i].write_protected == 0)
2724 /* (This may be redundant as write_protected is now
2725 * cleared before promotion.) */
2726 || (page_table[i].dont_move == 1))) {
2727 page_index_t last_page;
2728 int all_wp=1;
2730 /* The scavenge will start at the scan_start_offset of
2731 * page i.
2733 * We need to find the full extent of this contiguous
2734 * block in case objects span pages.
2736 * Now work forward until the end of this contiguous area
2737 * is found. A small area is preferred as there is a
2738 * better chance of its pages being write-protected. */
2739 for (last_page = i; ;last_page++) {
2740 /* If all pages are write-protected and movable,
2741 * then no need to scavenge */
2742 all_wp=all_wp && page_table[last_page].write_protected &&
2743 !page_table[last_page].dont_move;
2745 /* Check whether this is the last page in this
2746 * contiguous block */
2747 if (page_ends_contiguous_block_p(last_page, generation))
2748 break;
2751 /* Do a limited check for write-protected pages. */
2752 if (!all_wp) {
2753 new_areas_ignore_page = last_page;
2754 heap_scavenge(page_scan_start(i),
2755 (lispobj*)((char*)page_address(last_page)
2756 + page_bytes_used(last_page)));
2758 i = last_page;
2761 FSHOW((stderr,
2762 "/done with one full scan of newspace generation %d\n",
2763 generation));
2766 /* Do a complete scavenge of the newspace generation. */
2767 static void
2768 scavenge_newspace_generation(generation_index_t generation)
2770 size_t i;
2772 /* the new_areas array currently being written to by gc_alloc() */
2773 struct new_area (*current_new_areas)[] = &new_areas_1;
2774 size_t current_new_areas_index;
2776 /* the new_areas created by the previous scavenge cycle */
2777 struct new_area (*previous_new_areas)[] = NULL;
2778 size_t previous_new_areas_index;
2780 /* Flush the current regions updating the tables. */
2781 gc_alloc_update_all_page_tables(0);
2783 /* Turn on the recording of new areas by gc_alloc(). */
2784 new_areas = current_new_areas;
2785 new_areas_index = 0;
2787 /* Don't need to record new areas that get scavenged anyway during
2788 * scavenge_newspace_generation_one_scan. */
2789 record_new_objects = 1;
2791 /* Start with a full scavenge. */
2792 scavenge_newspace_generation_one_scan(generation);
2794 /* Record all new areas now. */
2795 record_new_objects = 2;
2797 /* Give a chance to weak hash tables to make other objects live.
2798 * FIXME: The algorithm implemented here for weak hash table gcing
2799 * is O(W^2+N) as Bruno Haible warns in
2800 * http://www.haible.de/bruno/papers/cs/weak/WeakDatastructures-writeup.html
2801 * see "Implementation 2". */
2802 scav_weak_hash_tables();
2804 /* Flush the current regions updating the tables. */
2805 gc_alloc_update_all_page_tables(0);
2807 /* Grab new_areas_index. */
2808 current_new_areas_index = new_areas_index;
2810 /*FSHOW((stderr,
2811 "The first scan is finished; current_new_areas_index=%d.\n",
2812 current_new_areas_index));*/
2814 while (current_new_areas_index > 0 || immobile_scav_queue_count) {
2815 /* Move the current to the previous new areas */
2816 previous_new_areas = current_new_areas;
2817 previous_new_areas_index = current_new_areas_index;
2819 /* Scavenge all the areas in previous new areas. Any new areas
2820 * allocated are saved in current_new_areas. */
2822 /* Allocate an array for current_new_areas; alternating between
2823 * new_areas_1 and 2 */
2824 if (previous_new_areas == &new_areas_1)
2825 current_new_areas = &new_areas_2;
2826 else
2827 current_new_areas = &new_areas_1;
2829 /* Set up for gc_alloc(). */
2830 new_areas = current_new_areas;
2831 new_areas_index = 0;
2833 #ifdef LISP_FEATURE_IMMOBILE_SPACE
2834 scavenge_immobile_newspace();
2835 #endif
2836 /* Check whether previous_new_areas had overflowed. */
2837 if (previous_new_areas_index >= NUM_NEW_AREAS) {
2839 /* New areas of objects allocated have been lost so need to do a
2840 * full scan to be sure! If this becomes a problem try
2841 * increasing NUM_NEW_AREAS. */
2842 if (gencgc_verbose) {
2843 SHOW("new_areas overflow, doing full scavenge");
2846 /* Don't need to record new areas that get scavenged
2847 * anyway during scavenge_newspace_generation_one_scan. */
2848 record_new_objects = 1;
2850 scavenge_newspace_generation_one_scan(generation);
2852 /* Record all new areas now. */
2853 record_new_objects = 2;
2855 scav_weak_hash_tables();
2857 /* Flush the current regions updating the tables. */
2858 gc_alloc_update_all_page_tables(0);
2860 } else {
2862 /* Work through previous_new_areas. */
2863 for (i = 0; i < previous_new_areas_index; i++) {
2864 page_index_t page = (*previous_new_areas)[i].page;
2865 size_t offset = (*previous_new_areas)[i].offset;
2866 size_t size = (*previous_new_areas)[i].size;
2867 gc_assert(size % N_WORD_BYTES == 0);
2868 lispobj *start = (lispobj*)((char*)page_address(page) + offset);
2869 heap_scavenge(start, (lispobj*)((char*)start + size));
2872 scav_weak_hash_tables();
2874 /* Flush the current regions updating the tables. */
2875 gc_alloc_update_all_page_tables(0);
2878 current_new_areas_index = new_areas_index;
2880 /*FSHOW((stderr,
2881 "The re-scan has finished; current_new_areas_index=%d.\n",
2882 current_new_areas_index));*/
2885 /* Turn off recording of areas allocated by gc_alloc(). */
2886 record_new_objects = 0;
2888 #if SC_NS_GEN_CK
2890 page_index_t i;
2891 /* Check that none of the write_protected pages in this generation
2892 * have been written to. */
2893 for (i = 0; i < page_table_pages; i++) {
2894 if (!page_free_p(i)
2895 && (page_bytes_used(i) != 0)
2896 && (page_table[i].gen == generation)
2897 && (page_table[i].write_protected_cleared != 0)
2898 && (page_table[i].dont_move == 0)) {
2899 lose("write protected page %d written to in scavenge_newspace_generation\ngeneration=%d dont_move=%d\n",
2900 i, generation, page_table[i].dont_move);
2904 #endif
2907 /* Un-write-protect all the pages in from_space. This is done at the
2908 * start of a GC else there may be many page faults while scavenging
2909 * the newspace (I've seen drive the system time to 99%). These pages
2910 * would need to be unprotected anyway before unmapping in
2911 * free_oldspace; not sure what effect this has on paging.. */
2912 static void
2913 unprotect_oldspace(void)
2915 page_index_t i;
2916 void *region_addr = 0;
2917 void *page_addr = 0;
2918 uword_t region_bytes = 0;
2920 for (i = 0; i < last_free_page; i++) {
2921 if (!page_free_p(i)
2922 && (page_bytes_used(i) != 0)
2923 && (page_table[i].gen == from_space)) {
2925 /* Remove any write-protection. We should be able to rely
2926 * on the write-protect flag to avoid redundant calls. */
2927 if (page_table[i].write_protected) {
2928 page_table[i].write_protected = 0;
2929 page_addr = page_address(i);
2930 if (!region_addr) {
2931 /* First region. */
2932 region_addr = page_addr;
2933 region_bytes = GENCGC_CARD_BYTES;
2934 } else if (region_addr + region_bytes == page_addr) {
2935 /* Region continue. */
2936 region_bytes += GENCGC_CARD_BYTES;
2937 } else {
2938 /* Unprotect previous region. */
2939 os_protect(region_addr, region_bytes, OS_VM_PROT_ALL);
2940 /* First page in new region. */
2941 region_addr = page_addr;
2942 region_bytes = GENCGC_CARD_BYTES;
2947 if (region_addr) {
2948 /* Unprotect last region. */
2949 os_protect(region_addr, region_bytes, OS_VM_PROT_ALL);
2953 /* Work through all the pages and free any in from_space. This
2954 * assumes that all objects have been copied or promoted to an older
2955 * generation. Bytes_allocated and the generation bytes_allocated
2956 * counter are updated. The number of bytes freed is returned. */
2957 static uword_t
2958 free_oldspace(void)
2960 uword_t bytes_freed = 0;
2961 page_index_t first_page, last_page;
2963 first_page = 0;
2965 do {
2966 /* Find a first page for the next region of pages. */
2967 while ((first_page < last_free_page)
2968 && (page_free_p(first_page)
2969 || (page_bytes_used(first_page) == 0)
2970 || (page_table[first_page].gen != from_space)))
2971 first_page++;
2973 if (first_page >= last_free_page)
2974 break;
2976 /* Find the last page of this region. */
2977 last_page = first_page;
2979 do {
2980 /* Free the page. */
2981 bytes_freed += page_bytes_used(last_page);
2982 generations[page_table[last_page].gen].bytes_allocated -=
2983 page_bytes_used(last_page);
2984 page_table[last_page].allocated = FREE_PAGE_FLAG;
2985 set_page_bytes_used(last_page, 0);
2986 /* Should already be unprotected by unprotect_oldspace(). */
2987 gc_assert(!page_table[last_page].write_protected);
2988 last_page++;
2990 while ((last_page < last_free_page)
2991 && !page_free_p(last_page)
2992 && (page_bytes_used(last_page) != 0)
2993 && (page_table[last_page].gen == from_space));
2995 #ifdef READ_PROTECT_FREE_PAGES
2996 os_protect(page_address(first_page),
2997 npage_bytes(last_page-first_page),
2998 OS_VM_PROT_NONE);
2999 #endif
3000 first_page = last_page;
3001 } while (first_page < last_free_page);
3003 bytes_allocated -= bytes_freed;
3004 return bytes_freed;
3007 #if 0
3008 /* Print some information about a pointer at the given address. */
3009 static void
3010 print_ptr(lispobj *addr)
3012 /* If addr is in the dynamic space then out the page information. */
3013 page_index_t pi1 = find_page_index((void*)addr);
3015 if (pi1 != -1)
3016 fprintf(stderr," %p: page %d alloc %d gen %d bytes_used %d offset %lu dont_move %d\n",
3017 addr,
3018 pi1,
3019 page_table[pi1].allocated,
3020 page_table[pi1].gen,
3021 page_bytes_used(pi1),
3022 scan_start_offset(page_table[pi1]),
3023 page_table[pi1].dont_move);
3024 fprintf(stderr," %x %x %x %x (%x) %x %x %x %x\n",
3025 *(addr-4),
3026 *(addr-3),
3027 *(addr-2),
3028 *(addr-1),
3029 *(addr-0),
3030 *(addr+1),
3031 *(addr+2),
3032 *(addr+3),
3033 *(addr+4));
3035 #endif
3037 static int
3038 is_in_stack_space(lispobj ptr)
3040 /* For space verification: Pointers can be valid if they point
3041 * to a thread stack space. This would be faster if the thread
3042 * structures had page-table entries as if they were part of
3043 * the heap space. */
3044 struct thread *th;
3045 for_each_thread(th) {
3046 if ((th->control_stack_start <= (lispobj *)ptr) &&
3047 (th->control_stack_end >= (lispobj *)ptr)) {
3048 return 1;
3051 return 0;
3054 // NOTE: This function can produces false failure indications,
3055 // usually related to dynamic space pointing to the stack of a
3056 // dead thread, but there may be other reasons as well.
3057 static void
3058 verify_space(lispobj *start, size_t words)
3060 extern int valid_lisp_pointer_p(lispobj);
3061 int is_in_dynamic_space = (find_page_index((void*)start) != -1);
3062 int is_in_readonly_space =
3063 (READ_ONLY_SPACE_START <= (uword_t)start &&
3064 (uword_t)start < SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0));
3065 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3066 int is_in_immobile_space =
3067 (IMMOBILE_SPACE_START <= (uword_t)start &&
3068 (uword_t)start < SymbolValue(IMMOBILE_SPACE_FREE_POINTER,0));
3069 #endif
3071 while (words > 0) {
3072 size_t count = 1;
3073 lispobj thing = *start;
3074 lispobj __attribute__((unused)) pointee;
3076 if (is_lisp_pointer(thing)) {
3077 page_index_t page_index = find_page_index((void*)thing);
3078 sword_t to_readonly_space =
3079 (READ_ONLY_SPACE_START <= thing &&
3080 thing < SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0));
3081 sword_t to_static_space =
3082 (STATIC_SPACE_START <= thing &&
3083 thing < SymbolValue(STATIC_SPACE_FREE_POINTER,0));
3084 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3085 sword_t to_immobile_space =
3086 (IMMOBILE_SPACE_START <= thing &&
3087 thing < SymbolValue(IMMOBILE_FIXEDOBJ_FREE_POINTER,0)) ||
3088 (IMMOBILE_VARYOBJ_SUBSPACE_START <= thing &&
3089 thing < SymbolValue(IMMOBILE_SPACE_FREE_POINTER,0));
3090 #endif
3092 /* Does it point to the dynamic space? */
3093 if (page_index != -1) {
3094 /* If it's within the dynamic space it should point to a used page. */
3095 if (page_free_p(page_index))
3096 lose ("Ptr %p @ %p sees free page.\n", thing, start);
3097 if ((thing & (GENCGC_CARD_BYTES-1)) >= page_bytes_used(page_index))
3098 lose ("Ptr %p @ %p sees unallocated space.\n", thing, start);
3099 /* Check that it doesn't point to a forwarding pointer! */
3100 if (*native_pointer(thing) == 0x01) {
3101 lose("Ptr %p @ %p sees forwarding ptr.\n", thing, start);
3103 /* Check that its not in the RO space as it would then be a
3104 * pointer from the RO to the dynamic space. */
3105 if (is_in_readonly_space) {
3106 lose("ptr to dynamic space %p from RO space %x\n",
3107 thing, start);
3109 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3110 // verify all immobile space -> dynamic space pointers
3111 if (is_in_immobile_space && !valid_lisp_pointer_p(thing)) {
3112 lose("Ptr %p @ %p sees junk.\n", thing, start);
3114 #endif
3115 /* Does it point to a plausible object? This check slows
3116 * it down a lot (so it's commented out).
3118 * "a lot" is serious: it ate 50 minutes cpu time on
3119 * my duron 950 before I came back from lunch and
3120 * killed it.
3122 * FIXME: Add a variable to enable this
3123 * dynamically. */
3125 if (!valid_lisp_pointer_p((lispobj *)thing) {
3126 lose("ptr %p to invalid object %p\n", thing, start);
3129 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3130 } else if (to_immobile_space) {
3131 // the object pointed to must not have been discarded as garbage
3132 if (!other_immediate_lowtag_p(*native_pointer(thing))
3133 || immobile_filler_p(native_pointer(thing)))
3134 lose("Ptr %p @ %p sees trashed object.\n", (void*)thing, start);
3135 // verify all pointers to immobile space
3136 if (!valid_lisp_pointer_p(thing))
3137 lose("Ptr %p @ %p sees junk.\n", thing, start);
3138 #endif
3139 } else {
3140 extern char __attribute__((unused)) funcallable_instance_tramp;
3141 /* Verify that it points to another valid space. */
3142 if (!to_readonly_space && !to_static_space
3143 && !is_in_stack_space(thing)) {
3144 lose("Ptr %p @ %p sees junk.\n", thing, start);
3147 } else {
3148 if (!(fixnump(thing))) {
3149 /* skip fixnums */
3150 switch(widetag_of(*start)) {
3152 /* boxed objects */
3153 case SIMPLE_VECTOR_WIDETAG:
3154 case RATIO_WIDETAG:
3155 case COMPLEX_WIDETAG:
3156 case SIMPLE_ARRAY_WIDETAG:
3157 case COMPLEX_BASE_STRING_WIDETAG:
3158 #ifdef COMPLEX_CHARACTER_STRING_WIDETAG
3159 case COMPLEX_CHARACTER_STRING_WIDETAG:
3160 #endif
3161 case COMPLEX_VECTOR_NIL_WIDETAG:
3162 case COMPLEX_BIT_VECTOR_WIDETAG:
3163 case COMPLEX_VECTOR_WIDETAG:
3164 case COMPLEX_ARRAY_WIDETAG:
3165 case CLOSURE_HEADER_WIDETAG:
3166 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
3167 case VALUE_CELL_HEADER_WIDETAG:
3168 case SYMBOL_HEADER_WIDETAG:
3169 case CHARACTER_WIDETAG:
3170 #if N_WORD_BITS == 64
3171 case SINGLE_FLOAT_WIDETAG:
3172 #endif
3173 case UNBOUND_MARKER_WIDETAG:
3174 break;
3175 case FDEFN_WIDETAG:
3176 #ifdef LISP_FEATURE_IMMOBILE_CODE
3177 verify_space(start + 1, 2);
3178 pointee = fdefn_raw_referent((struct fdefn*)start);
3179 verify_space(&pointee, 1);
3180 count = 4;
3181 #endif
3182 break;
3184 case INSTANCE_HEADER_WIDETAG:
3185 if (instance_layout(start)) {
3186 lispobj bitmap =
3187 ((struct layout*)
3188 native_pointer(instance_layout(start)))->bitmap;
3189 sword_t nslots = instance_length(thing) | 1;
3190 instance_scan(verify_space, start+1, nslots, bitmap);
3191 count = 1 + nslots;
3193 break;
3194 case CODE_HEADER_WIDETAG:
3196 /* Check that it's not in the dynamic space.
3197 * FIXME: Isn't is supposed to be OK for code
3198 * objects to be in the dynamic space these days? */
3199 /* It is for byte compiled code, but there's
3200 * no byte compilation in SBCL anymore. */
3201 if (is_in_dynamic_space
3202 /* Only when enabled */
3203 && verify_dynamic_code_check) {
3204 FSHOW((stderr,
3205 "/code object at %p in the dynamic space\n",
3206 start));
3209 struct code *code = (struct code *) start;
3210 sword_t nheader_words = code_header_words(code->header);
3211 /* Scavenge the boxed section of the code data block */
3212 verify_space(start + 1, nheader_words - 1);
3214 /* Scavenge the boxed section of each function
3215 * object in the code data block. */
3216 for_each_simple_fun(i, fheaderp, code, 1, {
3217 verify_space(SIMPLE_FUN_SCAV_START(fheaderp),
3218 SIMPLE_FUN_SCAV_NWORDS(fheaderp)); });
3219 count = nheader_words + code_instruction_words(code->code_size);
3220 break;
3223 /* unboxed objects */
3224 case BIGNUM_WIDETAG:
3225 #if N_WORD_BITS != 64
3226 case SINGLE_FLOAT_WIDETAG:
3227 #endif
3228 case DOUBLE_FLOAT_WIDETAG:
3229 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
3230 case LONG_FLOAT_WIDETAG:
3231 #endif
3232 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
3233 case COMPLEX_SINGLE_FLOAT_WIDETAG:
3234 #endif
3235 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
3236 case COMPLEX_DOUBLE_FLOAT_WIDETAG:
3237 #endif
3238 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
3239 case COMPLEX_LONG_FLOAT_WIDETAG:
3240 #endif
3241 #ifdef SIMD_PACK_WIDETAG
3242 case SIMD_PACK_WIDETAG:
3243 #endif
3244 #include "genesis/specialized-vectors.inc"
3245 case SAP_WIDETAG:
3246 case WEAK_POINTER_WIDETAG:
3247 #ifdef NO_TLS_VALUE_MARKER_WIDETAG
3248 case NO_TLS_VALUE_MARKER_WIDETAG:
3249 #endif
3250 count = (sizetab[widetag_of(*start)])(start);
3251 break;
3253 default:
3254 lose("Unhandled widetag %p at %p\n",
3255 widetag_of(*start), start);
3259 start += count;
3260 words -= count;
3264 static void verify_dynamic_space();
3266 static void
3267 verify_gc(void)
3269 /* FIXME: It would be nice to make names consistent so that
3270 * foo_size meant size *in* *bytes* instead of size in some
3271 * arbitrary units. (Yes, this caused a bug, how did you guess?:-)
3272 * Some counts of lispobjs are called foo_count; it might be good
3273 * to grep for all foo_size and rename the appropriate ones to
3274 * foo_count. */
3275 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3276 # ifdef __linux__
3277 // Try this verification if marknsweep was compiled with extra debugging.
3278 // But weak symbols don't work on macOS.
3279 extern void __attribute__((weak)) check_varyobj_pages();
3280 if (&check_varyobj_pages) check_varyobj_pages();
3281 # endif
3282 verify_space((lispobj*)IMMOBILE_SPACE_START,
3283 (lispobj*)SymbolValue(IMMOBILE_FIXEDOBJ_FREE_POINTER,0)
3284 - (lispobj*)IMMOBILE_SPACE_START);
3285 verify_space((lispobj*)IMMOBILE_VARYOBJ_SUBSPACE_START,
3286 (lispobj*)SymbolValue(IMMOBILE_SPACE_FREE_POINTER,0)
3287 - (lispobj*)IMMOBILE_VARYOBJ_SUBSPACE_START);
3288 #endif
3289 sword_t read_only_space_size =
3290 (lispobj*)SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0)
3291 - (lispobj*)READ_ONLY_SPACE_START;
3292 sword_t static_space_size =
3293 (lispobj*)SymbolValue(STATIC_SPACE_FREE_POINTER,0)
3294 - (lispobj*)STATIC_SPACE_START;
3295 struct thread *th;
3296 for_each_thread(th) {
3297 sword_t binding_stack_size =
3298 (lispobj*)get_binding_stack_pointer(th)
3299 - (lispobj*)th->binding_stack_start;
3300 verify_space(th->binding_stack_start, binding_stack_size);
3302 verify_space((lispobj*)READ_ONLY_SPACE_START, read_only_space_size);
3303 verify_space((lispobj*)STATIC_SPACE_START , static_space_size);
3304 verify_dynamic_space();
3307 void
3308 walk_generation(void (*proc)(lispobj*,size_t),
3309 generation_index_t generation)
3311 page_index_t i;
3312 int genmask = generation >= 0 ? 1 << generation : ~0;
3314 for (i = 0; i < last_free_page; i++) {
3315 if (!page_free_p(i)
3316 && (page_bytes_used(i) != 0)
3317 && ((1 << page_table[i].gen) & genmask)) {
3318 page_index_t last_page;
3320 /* This should be the start of a contiguous block */
3321 gc_assert(page_starts_contiguous_block_p(i));
3323 /* Need to find the full extent of this contiguous block in case
3324 objects span pages. */
3326 /* Now work forward until the end of this contiguous area is
3327 found. */
3328 for (last_page = i; ;last_page++)
3329 /* Check whether this is the last page in this contiguous
3330 * block. */
3331 if (page_ends_contiguous_block_p(last_page, page_table[i].gen))
3332 break;
3334 proc(page_address(i),
3335 ((uword_t)(page_bytes_used(last_page) + npage_bytes(last_page-i)))
3336 / N_WORD_BYTES);
3337 i = last_page;
3341 static void verify_generation(generation_index_t generation)
3343 walk_generation(verify_space, generation);
3346 /* Check that all the free space is zero filled. */
3347 static void
3348 verify_zero_fill(void)
3350 page_index_t page;
3352 for (page = 0; page < last_free_page; page++) {
3353 if (page_free_p(page)) {
3354 /* The whole page should be zero filled. */
3355 sword_t *start_addr = (sword_t *)page_address(page);
3356 sword_t i;
3357 for (i = 0; i < (sword_t)GENCGC_CARD_BYTES/N_WORD_BYTES; i++) {
3358 if (start_addr[i] != 0) {
3359 lose("free page not zero at %x\n", start_addr + i);
3362 } else {
3363 sword_t free_bytes = GENCGC_CARD_BYTES - page_bytes_used(page);
3364 if (free_bytes > 0) {
3365 sword_t *start_addr = (sword_t *)((uword_t)page_address(page)
3366 + page_bytes_used(page));
3367 sword_t size = free_bytes / N_WORD_BYTES;
3368 sword_t i;
3369 for (i = 0; i < size; i++) {
3370 if (start_addr[i] != 0) {
3371 lose("free region not zero at %x\n", start_addr + i);
3379 /* External entry point for verify_zero_fill */
3380 void
3381 gencgc_verify_zero_fill(void)
3383 /* Flush the alloc regions updating the tables. */
3384 gc_alloc_update_all_page_tables(1);
3385 SHOW("verifying zero fill");
3386 verify_zero_fill();
3389 static void
3390 verify_dynamic_space(void)
3392 verify_generation(-1);
3393 if (gencgc_enable_verify_zero_fill)
3394 verify_zero_fill();
3397 /* Write-protect all the dynamic boxed pages in the given generation. */
3398 static void
3399 write_protect_generation_pages(generation_index_t generation)
3401 page_index_t start;
3403 gc_assert(generation < SCRATCH_GENERATION);
3405 for (start = 0; start < last_free_page; start++) {
3406 if (protect_page_p(start, generation)) {
3407 void *page_start;
3408 page_index_t last;
3410 /* Note the page as protected in the page tables. */
3411 page_table[start].write_protected = 1;
3413 for (last = start + 1; last < last_free_page; last++) {
3414 if (!protect_page_p(last, generation))
3415 break;
3416 page_table[last].write_protected = 1;
3419 page_start = (void *)page_address(start);
3421 os_protect(page_start,
3422 npage_bytes(last - start),
3423 OS_VM_PROT_READ | OS_VM_PROT_EXECUTE);
3425 start = last;
3429 if (gencgc_verbose > 1) {
3430 FSHOW((stderr,
3431 "/write protected %d of %d pages in generation %d\n",
3432 count_write_protect_generation_pages(generation),
3433 count_generation_pages(generation),
3434 generation));
3438 #if defined(LISP_FEATURE_SB_THREAD) && (defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64))
3439 static void
3440 preserve_context_registers (os_context_t *c)
3442 void **ptr;
3443 /* On Darwin the signal context isn't a contiguous block of memory,
3444 * so just preserve_pointering its contents won't be sufficient.
3446 #if defined(LISP_FEATURE_DARWIN)||defined(LISP_FEATURE_WIN32)
3447 #if defined LISP_FEATURE_X86
3448 preserve_pointer((void*)*os_context_register_addr(c,reg_EAX));
3449 preserve_pointer((void*)*os_context_register_addr(c,reg_ECX));
3450 preserve_pointer((void*)*os_context_register_addr(c,reg_EDX));
3451 preserve_pointer((void*)*os_context_register_addr(c,reg_EBX));
3452 preserve_pointer((void*)*os_context_register_addr(c,reg_ESI));
3453 preserve_pointer((void*)*os_context_register_addr(c,reg_EDI));
3454 preserve_pointer((void*)*os_context_pc_addr(c));
3455 #elif defined LISP_FEATURE_X86_64
3456 preserve_pointer((void*)*os_context_register_addr(c,reg_RAX));
3457 preserve_pointer((void*)*os_context_register_addr(c,reg_RCX));
3458 preserve_pointer((void*)*os_context_register_addr(c,reg_RDX));
3459 preserve_pointer((void*)*os_context_register_addr(c,reg_RBX));
3460 preserve_pointer((void*)*os_context_register_addr(c,reg_RSI));
3461 preserve_pointer((void*)*os_context_register_addr(c,reg_RDI));
3462 preserve_pointer((void*)*os_context_register_addr(c,reg_R8));
3463 preserve_pointer((void*)*os_context_register_addr(c,reg_R9));
3464 preserve_pointer((void*)*os_context_register_addr(c,reg_R10));
3465 preserve_pointer((void*)*os_context_register_addr(c,reg_R11));
3466 preserve_pointer((void*)*os_context_register_addr(c,reg_R12));
3467 preserve_pointer((void*)*os_context_register_addr(c,reg_R13));
3468 preserve_pointer((void*)*os_context_register_addr(c,reg_R14));
3469 preserve_pointer((void*)*os_context_register_addr(c,reg_R15));
3470 preserve_pointer((void*)*os_context_pc_addr(c));
3471 #else
3472 #error "preserve_context_registers needs to be tweaked for non-x86 Darwin"
3473 #endif
3474 #endif
3475 #if !defined(LISP_FEATURE_WIN32)
3476 for(ptr = ((void **)(c+1))-1; ptr>=(void **)c; ptr--) {
3477 preserve_pointer(*ptr);
3479 #endif
3481 #endif
3483 static void
3484 move_pinned_pages_to_newspace()
3486 page_index_t i;
3488 /* scavenge() will evacuate all oldspace pages, but no newspace
3489 * pages. Pinned pages are precisely those pages which must not
3490 * be evacuated, so move them to newspace directly. */
3492 for (i = 0; i < last_free_page; i++) {
3493 if (page_table[i].dont_move &&
3494 /* dont_move is cleared lazily, so validate the space as well. */
3495 page_table[i].gen == from_space) {
3496 if (do_wipe_p && page_table[i].has_pins) {
3497 // do not move to newspace after all, this will be word-wiped
3498 continue;
3500 page_table[i].gen = new_space;
3501 /* And since we're moving the pages wholesale, also adjust
3502 * the generation allocation counters. */
3503 int used = page_bytes_used(i);
3504 generations[new_space].bytes_allocated += used;
3505 generations[from_space].bytes_allocated -= used;
3510 /* Garbage collect a generation. If raise is 0 then the remains of the
3511 * generation are not raised to the next generation. */
3512 static void
3513 garbage_collect_generation(generation_index_t generation, int raise)
3515 page_index_t i;
3516 struct thread *th;
3518 gc_assert(generation <= HIGHEST_NORMAL_GENERATION);
3520 /* The oldest generation can't be raised. */
3521 gc_assert((generation != HIGHEST_NORMAL_GENERATION) || (raise == 0));
3523 /* Check if weak hash tables were processed in the previous GC. */
3524 gc_assert(weak_hash_tables == NULL);
3526 /* Initialize the weak pointer list. */
3527 weak_pointers = NULL;
3529 /* When a generation is not being raised it is transported to a
3530 * temporary generation (NUM_GENERATIONS), and lowered when
3531 * done. Set up this new generation. There should be no pages
3532 * allocated to it yet. */
3533 if (!raise) {
3534 gc_assert(generations[SCRATCH_GENERATION].bytes_allocated == 0);
3537 /* Set the global src and dest. generations */
3538 from_space = generation;
3539 if (raise)
3540 new_space = generation+1;
3541 else
3542 new_space = SCRATCH_GENERATION;
3544 /* Change to a new space for allocation, resetting the alloc_start_page */
3545 gc_alloc_generation = new_space;
3546 generations[new_space].alloc_start_page = 0;
3547 generations[new_space].alloc_unboxed_start_page = 0;
3548 generations[new_space].alloc_large_start_page = 0;
3549 generations[new_space].alloc_large_unboxed_start_page = 0;
3551 hopscotch_reset(&pinned_objects);
3552 /* Before any pointers are preserved, the dont_move flags on the
3553 * pages need to be cleared. */
3554 /* FIXME: consider moving this bitmap into its own range of words,
3555 * out of the page table. Then we can just bzero() it.
3556 * This will also obviate the extra test at the comment
3557 * "dont_move is cleared lazily" in move_pinned_pages_to_newspace().
3559 for (i = 0; i < last_free_page; i++)
3560 if(page_table[i].gen==from_space) {
3561 page_table[i].dont_move = 0;
3564 /* Un-write-protect the old-space pages. This is essential for the
3565 * promoted pages as they may contain pointers into the old-space
3566 * which need to be scavenged. It also helps avoid unnecessary page
3567 * faults as forwarding pointers are written into them. They need to
3568 * be un-protected anyway before unmapping later. */
3569 unprotect_oldspace();
3571 /* Scavenge the stacks' conservative roots. */
3573 /* there are potentially two stacks for each thread: the main
3574 * stack, which may contain Lisp pointers, and the alternate stack.
3575 * We don't ever run Lisp code on the altstack, but it may
3576 * host a sigcontext with lisp objects in it */
3578 /* what we need to do: (1) find the stack pointer for the main
3579 * stack; scavenge it (2) find the interrupt context on the
3580 * alternate stack that might contain lisp values, and scavenge
3581 * that */
3583 /* we assume that none of the preceding applies to the thread that
3584 * initiates GC. If you ever call GC from inside an altstack
3585 * handler, you will lose. */
3587 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
3588 /* And if we're saving a core, there's no point in being conservative. */
3589 if (conservative_stack) {
3590 for_each_thread(th) {
3591 void **ptr;
3592 void **esp=(void **)-1;
3593 if (th->state == STATE_DEAD)
3594 continue;
3595 # if defined(LISP_FEATURE_SB_SAFEPOINT)
3596 /* Conservative collect_garbage is always invoked with a
3597 * foreign C call or an interrupt handler on top of every
3598 * existing thread, so the stored SP in each thread
3599 * structure is valid, no matter which thread we are looking
3600 * at. For threads that were running Lisp code, the pitstop
3601 * and edge functions maintain this value within the
3602 * interrupt or exception handler. */
3603 esp = os_get_csp(th);
3604 assert_on_stack(th, esp);
3606 /* In addition to pointers on the stack, also preserve the
3607 * return PC, the only value from the context that we need
3608 * in addition to the SP. The return PC gets saved by the
3609 * foreign call wrapper, and removed from the control stack
3610 * into a register. */
3611 preserve_pointer(th->pc_around_foreign_call);
3613 /* And on platforms with interrupts: scavenge ctx registers. */
3615 /* Disabled on Windows, because it does not have an explicit
3616 * stack of `interrupt_contexts'. The reported CSP has been
3617 * chosen so that the current context on the stack is
3618 * covered by the stack scan. See also set_csp_from_context(). */
3619 # ifndef LISP_FEATURE_WIN32
3620 if (th != arch_os_get_current_thread()) {
3621 long k = fixnum_value(
3622 SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,th));
3623 while (k > 0)
3624 preserve_context_registers(th->interrupt_contexts[--k]);
3626 # endif
3627 # elif defined(LISP_FEATURE_SB_THREAD)
3628 sword_t i,free;
3629 if(th==arch_os_get_current_thread()) {
3630 /* Somebody is going to burn in hell for this, but casting
3631 * it in two steps shuts gcc up about strict aliasing. */
3632 esp = (void **)((void *)&raise);
3633 } else {
3634 void **esp1;
3635 free=fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,th));
3636 for(i=free-1;i>=0;i--) {
3637 os_context_t *c=th->interrupt_contexts[i];
3638 esp1 = (void **) *os_context_register_addr(c,reg_SP);
3639 if (esp1>=(void **)th->control_stack_start &&
3640 esp1<(void **)th->control_stack_end) {
3641 if(esp1<esp) esp=esp1;
3642 preserve_context_registers(c);
3646 # else
3647 esp = (void **)((void *)&raise);
3648 # endif
3649 if (!esp || esp == (void*) -1)
3650 lose("garbage_collect: no SP known for thread %x (OS %x)",
3651 th, th->os_thread);
3652 for (ptr = ((void **)th->control_stack_end)-1; ptr >= esp; ptr--) {
3653 preserve_pointer(*ptr);
3657 #else
3658 /* Non-x86oid systems don't have "conservative roots" as such, but
3659 * the same mechanism is used for objects pinned for use by alien
3660 * code. */
3661 for_each_thread(th) {
3662 lispobj pin_list = SymbolTlValue(PINNED_OBJECTS,th);
3663 while (pin_list != NIL) {
3664 struct cons *list_entry =
3665 (struct cons *)native_pointer(pin_list);
3666 preserve_pointer((void*)list_entry->car);
3667 pin_list = list_entry->cdr;
3670 #endif
3672 #if QSHOW
3673 if (gencgc_verbose > 1) {
3674 sword_t num_dont_move_pages = count_dont_move_pages();
3675 fprintf(stderr,
3676 "/non-movable pages due to conservative pointers = %ld (%lu bytes)\n",
3677 num_dont_move_pages,
3678 npage_bytes(num_dont_move_pages));
3680 #endif
3682 /* Now that all of the pinned (dont_move) pages are known, and
3683 * before we start to scavenge (and thus relocate) objects,
3684 * relocate the pinned pages to newspace, so that the scavenger
3685 * will not attempt to relocate their contents. */
3686 move_pinned_pages_to_newspace();
3688 /* Scavenge all the rest of the roots. */
3690 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
3692 * If not x86, we need to scavenge the interrupt context(s) and the
3693 * control stack.
3696 struct thread *th;
3697 for_each_thread(th) {
3698 scavenge_interrupt_contexts(th);
3699 scavenge_control_stack(th);
3702 # ifdef LISP_FEATURE_SB_SAFEPOINT
3703 /* In this case, scrub all stacks right here from the GCing thread
3704 * instead of doing what the comment below says. Suboptimal, but
3705 * easier. */
3706 for_each_thread(th)
3707 scrub_thread_control_stack(th);
3708 # else
3709 /* Scrub the unscavenged control stack space, so that we can't run
3710 * into any stale pointers in a later GC (this is done by the
3711 * stop-for-gc handler in the other threads). */
3712 scrub_control_stack();
3713 # endif
3715 #endif
3717 /* Scavenge the Lisp functions of the interrupt handlers, taking
3718 * care to avoid SIG_DFL and SIG_IGN. */
3719 for (i = 0; i < NSIG; i++) {
3720 union interrupt_handler handler = interrupt_handlers[i];
3721 if (!ARE_SAME_HANDLER(handler.c, SIG_IGN) &&
3722 !ARE_SAME_HANDLER(handler.c, SIG_DFL)) {
3723 scavenge((lispobj *)(interrupt_handlers + i), 1);
3726 /* Scavenge the binding stacks. */
3728 struct thread *th;
3729 for_each_thread(th) {
3730 sword_t len= (lispobj *)get_binding_stack_pointer(th) -
3731 th->binding_stack_start;
3732 scavenge((lispobj *) th->binding_stack_start,len);
3733 #ifdef LISP_FEATURE_SB_THREAD
3734 /* do the tls as well */
3735 len=(SymbolValue(FREE_TLS_INDEX,0) >> WORD_SHIFT) -
3736 (sizeof (struct thread))/(sizeof (lispobj));
3737 scavenge((lispobj *) (th+1),len);
3738 #endif
3742 /* Scavenge static space. */
3743 if (gencgc_verbose > 1) {
3744 FSHOW((stderr,
3745 "/scavenge static space: %d bytes\n",
3746 SymbolValue(STATIC_SPACE_FREE_POINTER,0) - STATIC_SPACE_START));
3748 heap_scavenge((lispobj*)STATIC_SPACE_START,
3749 (lispobj*)SymbolValue(STATIC_SPACE_FREE_POINTER,0));
3751 /* All generations but the generation being GCed need to be
3752 * scavenged. The new_space generation needs special handling as
3753 * objects may be moved in - it is handled separately below. */
3754 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3755 scavenge_immobile_roots(generation+1, SCRATCH_GENERATION);
3756 #endif
3757 scavenge_generations(generation+1, PSEUDO_STATIC_GENERATION);
3759 scavenge_pinned_ranges();
3761 /* Finally scavenge the new_space generation. Keep going until no
3762 * more objects are moved into the new generation */
3763 scavenge_newspace_generation(new_space);
3765 /* FIXME: I tried reenabling this check when debugging unrelated
3766 * GC weirdness ca. sbcl-0.6.12.45, and it failed immediately.
3767 * Since the current GC code seems to work well, I'm guessing that
3768 * this debugging code is just stale, but I haven't tried to
3769 * figure it out. It should be figured out and then either made to
3770 * work or just deleted. */
3772 #define RESCAN_CHECK 0
3773 #if RESCAN_CHECK
3774 /* As a check re-scavenge the newspace once; no new objects should
3775 * be found. */
3777 os_vm_size_t old_bytes_allocated = bytes_allocated;
3778 os_vm_size_t bytes_allocated;
3780 /* Start with a full scavenge. */
3781 scavenge_newspace_generation_one_scan(new_space);
3783 /* Flush the current regions, updating the tables. */
3784 gc_alloc_update_all_page_tables(1);
3786 bytes_allocated = bytes_allocated - old_bytes_allocated;
3788 if (bytes_allocated != 0) {
3789 lose("Rescan of new_space allocated %d more bytes.\n",
3790 bytes_allocated);
3793 #endif
3795 scan_weak_hash_tables();
3796 scan_weak_pointers();
3797 wipe_nonpinned_words();
3798 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3799 // Do this last, because until wipe_nonpinned_words() happens,
3800 // not all page table entries have the 'gen' value updated,
3801 // which we need to correctly find all old->young pointers.
3802 sweep_immobile_space(raise);
3803 #endif
3805 /* Flush the current regions, updating the tables. */
3806 gc_alloc_update_all_page_tables(0);
3807 hopscotch_log_stats(&pinned_objects);
3809 /* Free the pages in oldspace, but not those marked dont_move. */
3810 free_oldspace();
3812 /* If the GC is not raising the age then lower the generation back
3813 * to its normal generation number */
3814 if (!raise) {
3815 for (i = 0; i < last_free_page; i++)
3816 if ((page_bytes_used(i) != 0)
3817 && (page_table[i].gen == SCRATCH_GENERATION))
3818 page_table[i].gen = generation;
3819 gc_assert(generations[generation].bytes_allocated == 0);
3820 generations[generation].bytes_allocated =
3821 generations[SCRATCH_GENERATION].bytes_allocated;
3822 generations[SCRATCH_GENERATION].bytes_allocated = 0;
3825 /* Reset the alloc_start_page for generation. */
3826 generations[generation].alloc_start_page = 0;
3827 generations[generation].alloc_unboxed_start_page = 0;
3828 generations[generation].alloc_large_start_page = 0;
3829 generations[generation].alloc_large_unboxed_start_page = 0;
3831 if (generation >= verify_gens) {
3832 if (gencgc_verbose) {
3833 SHOW("verifying");
3835 verify_gc();
3838 /* Set the new gc trigger for the GCed generation. */
3839 generations[generation].gc_trigger =
3840 generations[generation].bytes_allocated
3841 + generations[generation].bytes_consed_between_gc;
3843 if (raise)
3844 generations[generation].num_gc = 0;
3845 else
3846 ++generations[generation].num_gc;
3850 /* Update last_free_page, then SymbolValue(ALLOCATION_POINTER). */
3851 sword_t
3852 update_dynamic_space_free_pointer(void)
3854 page_index_t last_page = -1, i;
3856 for (i = 0; i < last_free_page; i++)
3857 if (!page_free_p(i) && (page_bytes_used(i) != 0))
3858 last_page = i;
3860 last_free_page = last_page+1;
3862 set_alloc_pointer((lispobj)(page_address(last_free_page)));
3863 return 0; /* dummy value: return something ... */
3866 static void
3867 remap_page_range (page_index_t from, page_index_t to)
3869 /* There's a mysterious Solaris/x86 problem with using mmap
3870 * tricks for memory zeroing. See sbcl-devel thread
3871 * "Re: patch: standalone executable redux".
3873 #if defined(LISP_FEATURE_SUNOS)
3874 zero_and_mark_pages(from, to);
3875 #else
3876 const page_index_t
3877 release_granularity = gencgc_release_granularity/GENCGC_CARD_BYTES,
3878 release_mask = release_granularity-1,
3879 end = to+1,
3880 aligned_from = (from+release_mask)&~release_mask,
3881 aligned_end = (end&~release_mask);
3883 if (aligned_from < aligned_end) {
3884 zero_pages_with_mmap(aligned_from, aligned_end-1);
3885 if (aligned_from != from)
3886 zero_and_mark_pages(from, aligned_from-1);
3887 if (aligned_end != end)
3888 zero_and_mark_pages(aligned_end, end-1);
3889 } else {
3890 zero_and_mark_pages(from, to);
3892 #endif
3895 static void
3896 remap_free_pages (page_index_t from, page_index_t to, int forcibly)
3898 page_index_t first_page, last_page;
3900 if (forcibly)
3901 return remap_page_range(from, to);
3903 for (first_page = from; first_page <= to; first_page++) {
3904 if (!page_free_p(first_page) || !page_need_to_zero(first_page))
3905 continue;
3907 last_page = first_page + 1;
3908 while (page_free_p(last_page) &&
3909 (last_page <= to) &&
3910 (page_need_to_zero(last_page)))
3911 last_page++;
3913 remap_page_range(first_page, last_page-1);
3915 first_page = last_page;
3919 generation_index_t small_generation_limit = 1;
3921 /* GC all generations newer than last_gen, raising the objects in each
3922 * to the next older generation - we finish when all generations below
3923 * last_gen are empty. Then if last_gen is due for a GC, or if
3924 * last_gen==NUM_GENERATIONS (the scratch generation? eh?) we GC that
3925 * too. The valid range for last_gen is: 0,1,...,NUM_GENERATIONS.
3927 * We stop collecting at gencgc_oldest_gen_to_gc, even if this is less than
3928 * last_gen (oh, and note that by default it is NUM_GENERATIONS-1) */
3929 void
3930 collect_garbage(generation_index_t last_gen)
3932 generation_index_t gen = 0, i;
3933 int raise, more = 0;
3934 int gen_to_wp;
3935 /* The largest value of last_free_page seen since the time
3936 * remap_free_pages was called. */
3937 static page_index_t high_water_mark = 0;
3939 FSHOW((stderr, "/entering collect_garbage(%d)\n", last_gen));
3940 log_generation_stats(gc_logfile, "=== GC Start ===");
3942 gc_active_p = 1;
3944 if (last_gen > HIGHEST_NORMAL_GENERATION+1) {
3945 FSHOW((stderr,
3946 "/collect_garbage: last_gen = %d, doing a level 0 GC\n",
3947 last_gen));
3948 last_gen = 0;
3951 /* Flush the alloc regions updating the tables. */
3952 gc_alloc_update_all_page_tables(1);
3954 /* Verify the new objects created by Lisp code. */
3955 if (pre_verify_gen_0) {
3956 FSHOW((stderr, "pre-checking generation 0\n"));
3957 verify_generation(0);
3960 if (gencgc_verbose > 1)
3961 print_generation_stats();
3963 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3964 /* Immobile space generation bits are lazily updated for gen0
3965 (not touched on every object allocation) so do it now */
3966 update_immobile_nursery_bits();
3967 #endif
3969 do {
3970 /* Collect the generation. */
3972 if (more || (gen >= gencgc_oldest_gen_to_gc)) {
3973 /* Never raise the oldest generation. Never raise the extra generation
3974 * collected due to more-flag. */
3975 raise = 0;
3976 more = 0;
3977 } else {
3978 raise =
3979 (gen < last_gen)
3980 || (generations[gen].num_gc >= generations[gen].number_of_gcs_before_promotion);
3981 /* If we would not normally raise this one, but we're
3982 * running low on space in comparison to the object-sizes
3983 * we've been seeing, raise it and collect the next one
3984 * too. */
3985 if (!raise && gen == last_gen) {
3986 more = (2*large_allocation) >= (dynamic_space_size - bytes_allocated);
3987 raise = more;
3991 if (gencgc_verbose > 1) {
3992 FSHOW((stderr,
3993 "starting GC of generation %d with raise=%d alloc=%d trig=%d GCs=%d\n",
3994 gen,
3995 raise,
3996 generations[gen].bytes_allocated,
3997 generations[gen].gc_trigger,
3998 generations[gen].num_gc));
4001 /* If an older generation is being filled, then update its
4002 * memory age. */
4003 if (raise == 1) {
4004 generations[gen+1].cum_sum_bytes_allocated +=
4005 generations[gen+1].bytes_allocated;
4008 garbage_collect_generation(gen, raise);
4010 /* Reset the memory age cum_sum. */
4011 generations[gen].cum_sum_bytes_allocated = 0;
4013 if (gencgc_verbose > 1) {
4014 FSHOW((stderr, "GC of generation %d finished:\n", gen));
4015 print_generation_stats();
4018 gen++;
4019 } while ((gen <= gencgc_oldest_gen_to_gc)
4020 && ((gen < last_gen)
4021 || more
4022 || (raise
4023 && (generations[gen].bytes_allocated
4024 > generations[gen].gc_trigger)
4025 && (generation_average_age(gen)
4026 > generations[gen].minimum_age_before_gc))));
4028 /* Now if gen-1 was raised all generations before gen are empty.
4029 * If it wasn't raised then all generations before gen-1 are empty.
4031 * Now objects within this gen's pages cannot point to younger
4032 * generations unless they are written to. This can be exploited
4033 * by write-protecting the pages of gen; then when younger
4034 * generations are GCed only the pages which have been written
4035 * need scanning. */
4036 if (raise)
4037 gen_to_wp = gen;
4038 else
4039 gen_to_wp = gen - 1;
4041 /* There's not much point in WPing pages in generation 0 as it is
4042 * never scavenged (except promoted pages). */
4043 if ((gen_to_wp > 0) && enable_page_protection) {
4044 /* Check that they are all empty. */
4045 for (i = 0; i < gen_to_wp; i++) {
4046 if (generations[i].bytes_allocated)
4047 lose("trying to write-protect gen. %d when gen. %d nonempty\n",
4048 gen_to_wp, i);
4050 write_protect_generation_pages(gen_to_wp);
4052 #ifdef LISP_FEATURE_IMMOBILE_SPACE
4053 write_protect_immobile_space();
4054 #endif
4056 /* Set gc_alloc() back to generation 0. The current regions should
4057 * be flushed after the above GCs. */
4058 gc_assert((boxed_region.free_pointer - boxed_region.start_addr) == 0);
4059 gc_alloc_generation = 0;
4061 /* Save the high-water mark before updating last_free_page */
4062 if (last_free_page > high_water_mark)
4063 high_water_mark = last_free_page;
4065 update_dynamic_space_free_pointer();
4067 /* Update auto_gc_trigger. Make sure we trigger the next GC before
4068 * running out of heap! */
4069 if (bytes_consed_between_gcs <= (dynamic_space_size - bytes_allocated))
4070 auto_gc_trigger = bytes_allocated + bytes_consed_between_gcs;
4071 else
4072 auto_gc_trigger = bytes_allocated + (dynamic_space_size - bytes_allocated)/2;
4074 if(gencgc_verbose)
4075 fprintf(stderr,"Next gc when %"OS_VM_SIZE_FMT" bytes have been consed\n",
4076 auto_gc_trigger);
4078 /* If we did a big GC (arbitrarily defined as gen > 1), release memory
4079 * back to the OS.
4081 if (gen > small_generation_limit) {
4082 if (last_free_page > high_water_mark)
4083 high_water_mark = last_free_page;
4084 remap_free_pages(0, high_water_mark, 0);
4085 high_water_mark = 0;
4088 gc_active_p = 0;
4089 large_allocation = 0;
4091 log_generation_stats(gc_logfile, "=== GC End ===");
4092 SHOW("returning from collect_garbage");
4095 void
4096 gc_init(void)
4098 page_index_t i;
4100 #if defined(LISP_FEATURE_SB_SAFEPOINT)
4101 alloc_gc_page();
4102 #endif
4104 /* Compute the number of pages needed for the dynamic space.
4105 * Dynamic space size should be aligned on page size. */
4106 page_table_pages = dynamic_space_size/GENCGC_CARD_BYTES;
4107 gc_assert(dynamic_space_size == npage_bytes(page_table_pages));
4109 /* Default nursery size to 5% of the total dynamic space size,
4110 * min 1Mb. */
4111 bytes_consed_between_gcs = dynamic_space_size/(os_vm_size_t)20;
4112 if (bytes_consed_between_gcs < (1024*1024))
4113 bytes_consed_between_gcs = 1024*1024;
4115 /* The page_table must be allocated using "calloc" to initialize
4116 * the page structures correctly. There used to be a separate
4117 * initialization loop (now commented out; see below) but that was
4118 * unnecessary and did hurt startup time. */
4119 page_table = calloc(page_table_pages, sizeof(struct page));
4120 gc_assert(page_table);
4121 #ifdef LISP_FEATURE_IMMOBILE_SPACE
4122 gc_init_immobile();
4123 #endif
4125 hopscotch_init();
4126 hopscotch_create(&pinned_objects, 0 /* no values */,
4127 32 /* logical bin count */, 0 /* default range */);
4129 scavtab[WEAK_POINTER_WIDETAG] = scav_weak_pointer;
4130 transother[SIMPLE_ARRAY_WIDETAG] = trans_boxed_large;
4132 /* The page structures are initialized implicitly when page_table
4133 * is allocated with "calloc" above. Formerly we had the following
4134 * explicit initialization here (comments converted to C99 style
4135 * for readability as C's block comments don't nest):
4137 * // Initialize each page structure.
4138 * for (i = 0; i < page_table_pages; i++) {
4139 * // Initialize all pages as free.
4140 * page_table[i].allocated = FREE_PAGE_FLAG;
4141 * page_table[i].bytes_used = 0;
4143 * // Pages are not write-protected at startup.
4144 * page_table[i].write_protected = 0;
4147 * Without this loop the image starts up much faster when dynamic
4148 * space is large -- which it is on 64-bit platforms already by
4149 * default -- and when "calloc" for large arrays is implemented
4150 * using copy-on-write of a page of zeroes -- which it is at least
4151 * on Linux. In this case the pages that page_table_pages is stored
4152 * in are mapped and cleared not before the corresponding part of
4153 * dynamic space is used. For example, this saves clearing 16 MB of
4154 * memory at startup if the page size is 4 KB and the size of
4155 * dynamic space is 4 GB.
4156 * FREE_PAGE_FLAG must be 0 for this to work correctly which is
4157 * asserted below: */
4159 /* Compile time assertion: If triggered, declares an array
4160 * of dimension -1 forcing a syntax error. The intent of the
4161 * assignment is to avoid an "unused variable" warning. */
4162 char assert_free_page_flag_0[(FREE_PAGE_FLAG) ? -1 : 1];
4163 assert_free_page_flag_0[0] = assert_free_page_flag_0[0];
4166 bytes_allocated = 0;
4168 /* Initialize the generations. */
4169 for (i = 0; i < NUM_GENERATIONS; i++) {
4170 generations[i].alloc_start_page = 0;
4171 generations[i].alloc_unboxed_start_page = 0;
4172 generations[i].alloc_large_start_page = 0;
4173 generations[i].alloc_large_unboxed_start_page = 0;
4174 generations[i].bytes_allocated = 0;
4175 generations[i].gc_trigger = 2000000;
4176 generations[i].num_gc = 0;
4177 generations[i].cum_sum_bytes_allocated = 0;
4178 /* the tune-able parameters */
4179 generations[i].bytes_consed_between_gc
4180 = bytes_consed_between_gcs/(os_vm_size_t)HIGHEST_NORMAL_GENERATION;
4181 generations[i].number_of_gcs_before_promotion = 1;
4182 generations[i].minimum_age_before_gc = 0.75;
4185 /* Initialize gc_alloc. */
4186 gc_alloc_generation = 0;
4187 gc_set_region_empty(&boxed_region);
4188 gc_set_region_empty(&unboxed_region);
4190 last_free_page = 0;
4193 /* Pick up the dynamic space from after a core load.
4195 * The ALLOCATION_POINTER points to the end of the dynamic space.
4198 static void
4199 gencgc_pickup_dynamic(void)
4201 page_index_t page = 0;
4202 void *alloc_ptr = (void *)get_alloc_pointer();
4203 lispobj *prev=(lispobj *)page_address(page);
4204 generation_index_t gen = PSEUDO_STATIC_GENERATION;
4206 bytes_allocated = 0;
4208 do {
4209 lispobj *first,*ptr= (lispobj *)page_address(page);
4211 if (!gencgc_partial_pickup || !page_free_p(page)) {
4212 /* It is possible, though rare, for the saved page table
4213 * to contain free pages below alloc_ptr. */
4214 page_table[page].gen = gen;
4215 set_page_bytes_used(page, GENCGC_CARD_BYTES);
4216 page_table[page].large_object = 0;
4217 page_table[page].write_protected = 0;
4218 page_table[page].write_protected_cleared = 0;
4219 page_table[page].dont_move = 0;
4220 set_page_need_to_zero(page, 1);
4222 bytes_allocated += GENCGC_CARD_BYTES;
4225 if (!gencgc_partial_pickup) {
4226 page_table[page].allocated = BOXED_PAGE_FLAG;
4227 first = gc_search_space3(ptr, prev, (ptr+2));
4228 if(ptr == first)
4229 prev=ptr;
4230 set_page_scan_start_offset(page,
4231 page_address(page) - (void *)prev);
4233 page++;
4234 } while (page_address(page) < alloc_ptr);
4236 last_free_page = page;
4238 generations[gen].bytes_allocated = bytes_allocated;
4240 gc_alloc_update_all_page_tables(1);
4241 write_protect_generation_pages(gen);
4244 void
4245 gc_initialize_pointers(void)
4247 gencgc_pickup_dynamic();
4251 /* alloc(..) is the external interface for memory allocation. It
4252 * allocates to generation 0. It is not called from within the garbage
4253 * collector as it is only external uses that need the check for heap
4254 * size (GC trigger) and to disable the interrupts (interrupts are
4255 * always disabled during a GC).
4257 * The vops that call alloc(..) assume that the returned space is zero-filled.
4258 * (E.g. the most significant word of a 2-word bignum in MOVE-FROM-UNSIGNED.)
4260 * The check for a GC trigger is only performed when the current
4261 * region is full, so in most cases it's not needed. */
4263 static inline lispobj *
4264 general_alloc_internal(sword_t nbytes, int page_type_flag, struct alloc_region *region,
4265 struct thread *thread)
4267 #ifndef LISP_FEATURE_WIN32
4268 lispobj alloc_signal;
4269 #endif
4270 void *new_obj;
4271 void *new_free_pointer;
4272 os_vm_size_t trigger_bytes = 0;
4274 gc_assert(nbytes > 0);
4276 /* Check for alignment allocation problems. */
4277 gc_assert((((uword_t)region->free_pointer & LOWTAG_MASK) == 0)
4278 && ((nbytes & LOWTAG_MASK) == 0));
4280 #if !(defined(LISP_FEATURE_WIN32) && defined(LISP_FEATURE_SB_THREAD))
4281 /* Must be inside a PA section. */
4282 gc_assert(get_pseudo_atomic_atomic(thread));
4283 #endif
4285 if ((os_vm_size_t) nbytes > large_allocation)
4286 large_allocation = nbytes;
4288 /* maybe we can do this quickly ... */
4289 new_free_pointer = region->free_pointer + nbytes;
4290 if (new_free_pointer <= region->end_addr) {
4291 new_obj = (void*)(region->free_pointer);
4292 region->free_pointer = new_free_pointer;
4293 return(new_obj); /* yup */
4296 /* We don't want to count nbytes against auto_gc_trigger unless we
4297 * have to: it speeds up the tenuring of objects and slows down
4298 * allocation. However, unless we do so when allocating _very_
4299 * large objects we are in danger of exhausting the heap without
4300 * running sufficient GCs.
4302 if ((os_vm_size_t) nbytes >= bytes_consed_between_gcs)
4303 trigger_bytes = nbytes;
4305 /* we have to go the long way around, it seems. Check whether we
4306 * should GC in the near future
4308 if (auto_gc_trigger && (bytes_allocated+trigger_bytes > auto_gc_trigger)) {
4309 /* Don't flood the system with interrupts if the need to gc is
4310 * already noted. This can happen for example when SUB-GC
4311 * allocates or after a gc triggered in a WITHOUT-GCING. */
4312 if (SymbolValue(GC_PENDING,thread) == NIL) {
4313 /* set things up so that GC happens when we finish the PA
4314 * section */
4315 SetSymbolValue(GC_PENDING,T,thread);
4316 if (SymbolValue(GC_INHIBIT,thread) == NIL) {
4317 #ifdef LISP_FEATURE_SB_SAFEPOINT
4318 thread_register_gc_trigger();
4319 #else
4320 set_pseudo_atomic_interrupted(thread);
4321 #ifdef GENCGC_IS_PRECISE
4322 /* PPC calls alloc() from a trap
4323 * look up the most context if it's from a trap. */
4325 os_context_t *context =
4326 thread->interrupt_data->allocation_trap_context;
4327 maybe_save_gc_mask_and_block_deferrables
4328 (context ? os_context_sigmask_addr(context) : NULL);
4330 #else
4331 maybe_save_gc_mask_and_block_deferrables(NULL);
4332 #endif
4333 #endif
4337 new_obj = gc_alloc_with_region(nbytes, page_type_flag, region, 0);
4339 #ifndef LISP_FEATURE_WIN32
4340 /* for sb-prof, and not supported on Windows yet */
4341 alloc_signal = SymbolValue(ALLOC_SIGNAL,thread);
4342 if ((alloc_signal & FIXNUM_TAG_MASK) == 0) {
4343 if ((sword_t) alloc_signal <= 0) {
4344 SetSymbolValue(ALLOC_SIGNAL, T, thread);
4345 raise(SIGPROF);
4346 } else {
4347 SetSymbolValue(ALLOC_SIGNAL,
4348 alloc_signal - (1 << N_FIXNUM_TAG_BITS),
4349 thread);
4352 #endif
4354 return (new_obj);
4357 lispobj *
4358 general_alloc(sword_t nbytes, int page_type_flag)
4360 struct thread *thread = arch_os_get_current_thread();
4361 /* Select correct region, and call general_alloc_internal with it.
4362 * For other then boxed allocation we must lock first, since the
4363 * region is shared. */
4364 if (BOXED_PAGE_FLAG & page_type_flag) {
4365 #ifdef LISP_FEATURE_SB_THREAD
4366 struct alloc_region *region = (thread ? &(thread->alloc_region) : &boxed_region);
4367 #else
4368 struct alloc_region *region = &boxed_region;
4369 #endif
4370 return general_alloc_internal(nbytes, page_type_flag, region, thread);
4371 } else if (UNBOXED_PAGE_FLAG == page_type_flag) {
4372 lispobj * obj;
4373 int result;
4374 result = thread_mutex_lock(&allocation_lock);
4375 gc_assert(!result);
4376 obj = general_alloc_internal(nbytes, page_type_flag, &unboxed_region, thread);
4377 result = thread_mutex_unlock(&allocation_lock);
4378 gc_assert(!result);
4379 return obj;
4380 } else {
4381 lose("bad page type flag: %d", page_type_flag);
4385 lispobj AMD64_SYSV_ABI *
4386 alloc(sword_t nbytes)
4388 #ifdef LISP_FEATURE_SB_SAFEPOINT_STRICTLY
4389 struct thread *self = arch_os_get_current_thread();
4390 int was_pseudo_atomic = get_pseudo_atomic_atomic(self);
4391 if (!was_pseudo_atomic)
4392 set_pseudo_atomic_atomic(self);
4393 #else
4394 gc_assert(get_pseudo_atomic_atomic(arch_os_get_current_thread()));
4395 #endif
4397 lispobj *result = general_alloc(nbytes, BOXED_PAGE_FLAG);
4399 #ifdef LISP_FEATURE_SB_SAFEPOINT_STRICTLY
4400 if (!was_pseudo_atomic)
4401 clear_pseudo_atomic_atomic(self);
4402 #endif
4404 return result;
4408 * shared support for the OS-dependent signal handlers which
4409 * catch GENCGC-related write-protect violations
4411 void unhandled_sigmemoryfault(void* addr);
4413 /* Depending on which OS we're running under, different signals might
4414 * be raised for a violation of write protection in the heap. This
4415 * function factors out the common generational GC magic which needs
4416 * to invoked in this case, and should be called from whatever signal
4417 * handler is appropriate for the OS we're running under.
4419 * Return true if this signal is a normal generational GC thing that
4420 * we were able to handle, or false if it was abnormal and control
4421 * should fall through to the general SIGSEGV/SIGBUS/whatever logic.
4423 * We have two control flags for this: one causes us to ignore faults
4424 * on unprotected pages completely, and the second complains to stderr
4425 * but allows us to continue without losing.
4427 extern boolean ignore_memoryfaults_on_unprotected_pages;
4428 boolean ignore_memoryfaults_on_unprotected_pages = 0;
4430 extern boolean continue_after_memoryfault_on_unprotected_pages;
4431 boolean continue_after_memoryfault_on_unprotected_pages = 0;
4434 gencgc_handle_wp_violation(void* fault_addr)
4436 page_index_t page_index = find_page_index(fault_addr);
4438 #if QSHOW_SIGNALS
4439 FSHOW((stderr,
4440 "heap WP violation? fault_addr=%p, page_index=%"PAGE_INDEX_FMT"\n",
4441 fault_addr, page_index));
4442 #endif
4444 /* Check whether the fault is within the dynamic space. */
4445 if (page_index == (-1)) {
4446 #ifdef LISP_FEATURE_IMMOBILE_SPACE
4447 extern int immobile_space_handle_wp_violation(void*);
4448 if (immobile_space_handle_wp_violation(fault_addr))
4449 return 1;
4450 #endif
4452 /* It can be helpful to be able to put a breakpoint on this
4453 * case to help diagnose low-level problems. */
4454 unhandled_sigmemoryfault(fault_addr);
4456 /* not within the dynamic space -- not our responsibility */
4457 return 0;
4459 } else {
4460 int ret;
4461 ret = thread_mutex_lock(&free_pages_lock);
4462 gc_assert(ret == 0);
4463 if (page_table[page_index].write_protected) {
4464 /* Unprotect the page. */
4465 os_protect(page_address(page_index), GENCGC_CARD_BYTES, OS_VM_PROT_ALL);
4466 page_table[page_index].write_protected_cleared = 1;
4467 page_table[page_index].write_protected = 0;
4468 } else if (!ignore_memoryfaults_on_unprotected_pages) {
4469 /* The only acceptable reason for this signal on a heap
4470 * access is that GENCGC write-protected the page.
4471 * However, if two CPUs hit a wp page near-simultaneously,
4472 * we had better not have the second one lose here if it
4473 * does this test after the first one has already set wp=0
4475 if(page_table[page_index].write_protected_cleared != 1) {
4476 void lisp_backtrace(int frames);
4477 lisp_backtrace(10);
4478 fprintf(stderr,
4479 "Fault @ %p, page %"PAGE_INDEX_FMT" not marked as write-protected:\n"
4480 " boxed_region.first_page: %"PAGE_INDEX_FMT","
4481 " boxed_region.last_page %"PAGE_INDEX_FMT"\n"
4482 " page.scan_start_offset: %"OS_VM_SIZE_FMT"\n"
4483 " page.bytes_used: %u\n"
4484 " page.allocated: %d\n"
4485 " page.write_protected: %d\n"
4486 " page.write_protected_cleared: %d\n"
4487 " page.generation: %d\n",
4488 fault_addr,
4489 page_index,
4490 boxed_region.first_page,
4491 boxed_region.last_page,
4492 page_scan_start_offset(page_index),
4493 page_bytes_used(page_index),
4494 page_table[page_index].allocated,
4495 page_table[page_index].write_protected,
4496 page_table[page_index].write_protected_cleared,
4497 page_table[page_index].gen);
4498 if (!continue_after_memoryfault_on_unprotected_pages)
4499 lose("Feh.\n");
4502 ret = thread_mutex_unlock(&free_pages_lock);
4503 gc_assert(ret == 0);
4504 /* Don't worry, we can handle it. */
4505 return 1;
4508 /* This is to be called when we catch a SIGSEGV/SIGBUS, determine that
4509 * it's not just a case of the program hitting the write barrier, and
4510 * are about to let Lisp deal with it. It's basically just a
4511 * convenient place to set a gdb breakpoint. */
4512 void
4513 unhandled_sigmemoryfault(void *addr)
4516 static void
4517 update_thread_page_tables(struct thread *th)
4519 gc_alloc_update_page_tables(BOXED_PAGE_FLAG, &th->alloc_region);
4520 #if defined(LISP_FEATURE_SB_SAFEPOINT_STRICTLY) && !defined(LISP_FEATURE_WIN32)
4521 gc_alloc_update_page_tables(BOXED_PAGE_FLAG, &th->sprof_alloc_region);
4522 #endif
4525 /* GC is single-threaded and all memory allocations during a
4526 collection happen in the GC thread, so it is sufficient to update
4527 all the the page tables once at the beginning of a collection and
4528 update only page tables of the GC thread during the collection. */
4529 void gc_alloc_update_all_page_tables(int for_all_threads)
4531 /* Flush the alloc regions updating the tables. */
4532 struct thread *th;
4533 if (for_all_threads) {
4534 for_each_thread(th) {
4535 update_thread_page_tables(th);
4538 else {
4539 th = arch_os_get_current_thread();
4540 if (th) {
4541 update_thread_page_tables(th);
4544 gc_alloc_update_page_tables(UNBOXED_PAGE_FLAG, &unboxed_region);
4545 gc_alloc_update_page_tables(BOXED_PAGE_FLAG, &boxed_region);
4548 void
4549 gc_set_region_empty(struct alloc_region *region)
4551 region->first_page = 0;
4552 region->last_page = -1;
4553 region->start_addr = page_address(0);
4554 region->free_pointer = page_address(0);
4555 region->end_addr = page_address(0);
4558 static void
4559 zero_all_free_pages()
4561 page_index_t i;
4563 for (i = 0; i < last_free_page; i++) {
4564 if (page_free_p(i)) {
4565 #ifdef READ_PROTECT_FREE_PAGES
4566 os_protect(page_address(i),
4567 GENCGC_CARD_BYTES,
4568 OS_VM_PROT_ALL);
4569 #endif
4570 zero_pages(i, i);
4575 /* Things to do before doing a final GC before saving a core (without
4576 * purify).
4578 * + Pages in large_object pages aren't moved by the GC, so we need to
4579 * unset that flag from all pages.
4580 * + The pseudo-static generation isn't normally collected, but it seems
4581 * reasonable to collect it at least when saving a core. So move the
4582 * pages to a normal generation.
4584 static void
4585 prepare_for_final_gc ()
4587 page_index_t i;
4589 #ifdef LISP_FEATURE_IMMOBILE_SPACE
4590 extern void prepare_immobile_space_for_final_gc();
4591 prepare_immobile_space_for_final_gc ();
4592 #endif
4593 do_wipe_p = 0;
4594 for (i = 0; i < last_free_page; i++) {
4595 page_table[i].large_object = 0;
4596 if (page_table[i].gen == PSEUDO_STATIC_GENERATION) {
4597 int used = page_bytes_used(i);
4598 page_table[i].gen = HIGHEST_NORMAL_GENERATION;
4599 generations[PSEUDO_STATIC_GENERATION].bytes_allocated -= used;
4600 generations[HIGHEST_NORMAL_GENERATION].bytes_allocated += used;
4606 /* Do a non-conservative GC, and then save a core with the initial
4607 * function being set to the value of the static symbol
4608 * SB!VM:RESTART-LISP-FUNCTION */
4609 void
4610 gc_and_save(char *filename, boolean prepend_runtime,
4611 boolean save_runtime_options, boolean compressed,
4612 int compression_level, int application_type)
4614 FILE *file;
4615 void *runtime_bytes = NULL;
4616 size_t runtime_size;
4618 file = prepare_to_save(filename, prepend_runtime, &runtime_bytes,
4619 &runtime_size);
4620 if (file == NULL)
4621 return;
4623 conservative_stack = 0;
4625 /* The filename might come from Lisp, and be moved by the now
4626 * non-conservative GC. */
4627 filename = strdup(filename);
4629 /* Collect twice: once into relatively high memory, and then back
4630 * into low memory. This compacts the retained data into the lower
4631 * pages, minimizing the size of the core file.
4633 prepare_for_final_gc();
4634 gencgc_alloc_start_page = last_free_page;
4635 collect_garbage(HIGHEST_NORMAL_GENERATION+1);
4637 prepare_for_final_gc();
4638 gencgc_alloc_start_page = -1;
4639 collect_garbage(HIGHEST_NORMAL_GENERATION+1);
4641 if (prepend_runtime)
4642 save_runtime_to_filehandle(file, runtime_bytes, runtime_size,
4643 application_type);
4645 /* The dumper doesn't know that pages need to be zeroed before use. */
4646 zero_all_free_pages();
4647 save_to_filehandle(file, filename, SymbolValue(RESTART_LISP_FUNCTION,0),
4648 prepend_runtime, save_runtime_options,
4649 compressed ? compression_level : COMPRESSION_LEVEL_NONE);
4650 /* Oops. Save still managed to fail. Since we've mangled the stack
4651 * beyond hope, there's not much we can do.
4652 * (beyond FUNCALLing RESTART_LISP_FUNCTION, but I suspect that's
4653 * going to be rather unsatisfactory too... */
4654 lose("Attempt to save core after non-conservative GC failed.\n");