Kill a ton of implicit cast warnings
[sbcl.git] / src / runtime / gencgc.c
blob21a6855b8740de9a531f8fca726494d6ccbc6ee1
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 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
61 #include "genesis/cons.h"
62 #endif
63 #ifdef LISP_FEATURE_X86
64 #include "forwarding-ptr.h"
65 #endif
67 /* forward declarations */
68 page_index_t gc_find_freeish_pages(page_index_t *restart_page_ptr, sword_t nbytes,
69 int page_type_flag);
73 * GC parameters
76 /* As usually configured, generations 0-5 are normal collected generations,
77 6 is pseudo-static (the objects in which are never moved nor reclaimed),
78 and 7 is scratch space used when collecting a generation without promotion,
79 wherein it is moved to generation 7 and back again.
81 enum {
82 SCRATCH_GENERATION = PSEUDO_STATIC_GENERATION+1,
83 NUM_GENERATIONS
86 /* Should we use page protection to help avoid the scavenging of pages
87 * that don't have pointers to younger generations? */
88 boolean enable_page_protection = 1;
90 /* Largest allocation seen since last GC. */
91 os_vm_size_t large_allocation = 0;
95 * debugging
98 /* the verbosity level. All non-error messages are disabled at level 0;
99 * and only a few rare messages are printed at level 1. */
100 #if QSHOW == 2
101 boolean gencgc_verbose = 1;
102 #else
103 boolean gencgc_verbose = 0;
104 #endif
106 /* FIXME: At some point enable the various error-checking things below
107 * and see what they say. */
109 /* We hunt for pointers to old-space, when GCing generations >= verify_gen.
110 * Set verify_gens to HIGHEST_NORMAL_GENERATION + 1 to disable this kind of
111 * check. */
112 generation_index_t verify_gens = HIGHEST_NORMAL_GENERATION + 1;
114 /* Should we do a pre-scan verify of generation 0 before it's GCed? */
115 boolean pre_verify_gen_0 = 0;
117 /* Should we print a note when code objects are found in the dynamic space
118 * during a heap verify? */
119 boolean verify_dynamic_code_check = 0;
121 #ifdef LISP_FEATURE_X86
122 /* Should we check code objects for fixup errors after they are transported? */
123 boolean check_code_fixups = 0;
124 #endif
126 /* Should we check that newly allocated regions are zero filled? */
127 boolean gencgc_zero_check = 0;
129 /* Should we check that the free space is zero filled? */
130 boolean gencgc_enable_verify_zero_fill = 0;
132 /* When loading a core, don't do a full scan of the memory for the
133 * memory region boundaries. (Set to true by coreparse.c if the core
134 * contained a pagetable entry).
136 boolean gencgc_partial_pickup = 0;
138 /* If defined, free pages are read-protected to ensure that nothing
139 * accesses them.
142 /* #define READ_PROTECT_FREE_PAGES */
146 * GC structures and variables
149 /* the total bytes allocated. These are seen by Lisp DYNAMIC-USAGE. */
150 os_vm_size_t bytes_allocated = 0;
151 os_vm_size_t auto_gc_trigger = 0;
153 /* the source and destination generations. These are set before a GC starts
154 * scavenging. */
155 generation_index_t from_space;
156 generation_index_t new_space;
158 /* Set to 1 when in GC */
159 boolean gc_active_p = 0;
161 /* should the GC be conservative on stack. If false (only right before
162 * saving a core), don't scan the stack / mark pages dont_move. */
163 static boolean conservative_stack = 1;
165 /* An array of page structures is allocated on gc initialization.
166 * This helps to quickly map between an address and its page structure.
167 * page_table_pages is set from the size of the dynamic space. */
168 page_index_t page_table_pages;
169 struct page *page_table;
171 in_use_marker_t *page_table_pinned_dwords;
172 size_t pins_map_size_in_bytes;
174 /* In GC cards that have conservative pointers to them, should we wipe out
175 * dwords in there that are not used, so that they do not act as false
176 * root to other things in the heap from then on? This is a new feature
177 * but in testing it is both reliable and no noticeable slowdown. */
178 int do_wipe_p = 1;
180 static inline boolean page_allocated_p(page_index_t page) {
181 return (page_table[page].allocated != FREE_PAGE_FLAG);
184 static inline boolean page_no_region_p(page_index_t page) {
185 return !(page_table[page].allocated & OPEN_REGION_PAGE_FLAG);
188 static inline boolean page_allocated_no_region_p(page_index_t page) {
189 return ((page_table[page].allocated & (UNBOXED_PAGE_FLAG | BOXED_PAGE_FLAG))
190 && page_no_region_p(page));
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 static inline boolean page_boxed_no_region_p(page_index_t page) {
202 return page_boxed_p(page) && page_no_region_p(page);
205 static inline boolean page_unboxed_p(page_index_t page) {
206 /* Both flags set == boxed code page */
207 return ((page_table[page].allocated & UNBOXED_PAGE_FLAG)
208 && !page_boxed_p(page));
211 static inline boolean protect_page_p(page_index_t page, generation_index_t generation) {
212 return (page_boxed_no_region_p(page)
213 && (page_bytes_used(page) != 0)
214 && !page_table[page].dont_move
215 && (page_table[page].gen == generation));
218 /* Calculate the start address for the given page number. */
219 inline void *
220 page_address(page_index_t page_num)
222 return (void*)(DYNAMIC_SPACE_START + (page_num * GENCGC_CARD_BYTES));
225 /* Calculate the address where the allocation region associated with
226 * the page starts. */
227 static inline void *
228 page_scan_start(page_index_t page_index)
230 return page_address(page_index)-page_scan_start_offset(page_index);
233 /* True if the page starts a contiguous block. */
234 static inline boolean
235 page_starts_contiguous_block_p(page_index_t page_index)
237 return page_scan_start_offset(page_index) == 0;
240 /* True if the page is the last page in a contiguous block. */
241 static inline boolean
242 page_ends_contiguous_block_p(page_index_t page_index, generation_index_t gen)
244 return (/* page doesn't fill block */
245 (page_bytes_used(page_index) < GENCGC_CARD_BYTES)
246 /* page is last allocated page */
247 || ((page_index + 1) >= last_free_page)
248 /* next page free */
249 || page_free_p(page_index + 1)
250 /* next page contains no data */
251 || (page_bytes_used(page_index + 1) == 0)
252 /* next page is in different generation */
253 || (page_table[page_index + 1].gen != gen)
254 /* next page starts its own contiguous block */
255 || (page_starts_contiguous_block_p(page_index + 1)));
258 /// External function for calling from Lisp.
259 page_index_t ext_find_page_index(void *addr) { return find_page_index(addr); }
261 static os_vm_size_t
262 npage_bytes(page_index_t npages)
264 gc_assert(npages>=0);
265 return ((os_vm_size_t)npages)*GENCGC_CARD_BYTES;
268 /* Check that X is a higher address than Y and return offset from Y to
269 * X in bytes. */
270 static inline os_vm_size_t
271 void_diff(void *x, void *y)
273 gc_assert(x >= y);
274 return (pointer_sized_uint_t)x - (pointer_sized_uint_t)y;
277 /* a structure to hold the state of a generation
279 * CAUTION: If you modify this, make sure to touch up the alien
280 * definition in src/code/gc.lisp accordingly. ...or better yes,
281 * deal with the FIXME there...
283 struct generation {
285 /* the first page that gc_alloc() checks on its next call */
286 page_index_t alloc_start_page;
288 /* the first page that gc_alloc_unboxed() checks on its next call */
289 page_index_t alloc_unboxed_start_page;
291 /* the first page that gc_alloc_large (boxed) considers on its next
292 * call. (Although it always allocates after the boxed_region.) */
293 page_index_t alloc_large_start_page;
295 /* the first page that gc_alloc_large (unboxed) considers on its
296 * next call. (Although it always allocates after the
297 * current_unboxed_region.) */
298 page_index_t alloc_large_unboxed_start_page;
300 /* the bytes allocated to this generation */
301 os_vm_size_t bytes_allocated;
303 /* the number of bytes at which to trigger a GC */
304 os_vm_size_t gc_trigger;
306 /* to calculate a new level for gc_trigger */
307 os_vm_size_t bytes_consed_between_gc;
309 /* the number of GCs since the last raise */
310 int num_gc;
312 /* the number of GCs to run on the generations before raising objects to the
313 * next generation */
314 int number_of_gcs_before_promotion;
316 /* the cumulative sum of the bytes allocated to this generation. It is
317 * cleared after a GC on this generations, and update before new
318 * objects are added from a GC of a younger generation. Dividing by
319 * the bytes_allocated will give the average age of the memory in
320 * this generation since its last GC. */
321 os_vm_size_t cum_sum_bytes_allocated;
323 /* a minimum average memory age before a GC will occur helps
324 * prevent a GC when a large number of new live objects have been
325 * added, in which case a GC could be a waste of time */
326 double minimum_age_before_gc;
329 /* an array of generation structures. There needs to be one more
330 * generation structure than actual generations as the oldest
331 * generation is temporarily raised then lowered. */
332 struct generation generations[NUM_GENERATIONS];
334 /* the oldest generation that is will currently be GCed by default.
335 * Valid values are: 0, 1, ... HIGHEST_NORMAL_GENERATION
337 * The default of HIGHEST_NORMAL_GENERATION enables GC on all generations.
339 * Setting this to 0 effectively disables the generational nature of
340 * the GC. In some applications generational GC may not be useful
341 * because there are no long-lived objects.
343 * An intermediate value could be handy after moving long-lived data
344 * into an older generation so an unnecessary GC of this long-lived
345 * data can be avoided. */
346 generation_index_t gencgc_oldest_gen_to_gc = HIGHEST_NORMAL_GENERATION;
348 /* META: Is nobody aside from me bothered by this especially misleading
349 * use of the word "last"? It could mean either "ultimate" or "prior",
350 * but in fact means neither. It is the *FIRST* page that should be grabbed
351 * for more space, so it is min free page, or 1+ the max used page. */
352 /* The maximum free page in the heap is maintained and used to update
353 * ALLOCATION_POINTER which is used by the room function to limit its
354 * search of the heap. XX Gencgc obviously needs to be better
355 * integrated with the Lisp code. */
357 page_index_t last_free_page;
359 #ifdef LISP_FEATURE_SB_THREAD
360 /* This lock is to prevent multiple threads from simultaneously
361 * allocating new regions which overlap each other. Note that the
362 * majority of GC is single-threaded, but alloc() may be called from
363 * >1 thread at a time and must be thread-safe. This lock must be
364 * seized before all accesses to generations[] or to parts of
365 * page_table[] that other threads may want to see */
366 static pthread_mutex_t free_pages_lock = PTHREAD_MUTEX_INITIALIZER;
367 /* This lock is used to protect non-thread-local allocation. */
368 static pthread_mutex_t allocation_lock = PTHREAD_MUTEX_INITIALIZER;
369 #endif
371 extern os_vm_size_t gencgc_release_granularity;
372 os_vm_size_t gencgc_release_granularity = GENCGC_RELEASE_GRANULARITY;
374 extern os_vm_size_t gencgc_alloc_granularity;
375 os_vm_size_t gencgc_alloc_granularity = GENCGC_ALLOC_GRANULARITY;
379 * miscellaneous heap functions
382 /* Count the number of pages which are write-protected within the
383 * given generation. */
384 static page_index_t
385 count_write_protect_generation_pages(generation_index_t generation)
387 page_index_t i, count = 0;
389 for (i = 0; i < last_free_page; i++)
390 if (page_allocated_p(i)
391 && (page_table[i].gen == generation)
392 && (page_table[i].write_protected == 1))
393 count++;
394 return count;
397 /* Count the number of pages within the given generation. */
398 static page_index_t
399 count_generation_pages(generation_index_t generation)
401 page_index_t i;
402 page_index_t count = 0;
404 for (i = 0; i < last_free_page; i++)
405 if (page_allocated_p(i)
406 && (page_table[i].gen == generation))
407 count++;
408 return count;
411 #if QSHOW
412 static page_index_t
413 count_dont_move_pages(void)
415 page_index_t i;
416 page_index_t count = 0;
417 for (i = 0; i < last_free_page; i++) {
418 if (page_allocated_p(i)
419 && (page_table[i].dont_move != 0)) {
420 ++count;
423 return count;
425 #endif /* QSHOW */
427 /* Work through the pages and add up the number of bytes used for the
428 * given generation. */
429 static __attribute__((unused)) os_vm_size_t
430 count_generation_bytes_allocated (generation_index_t gen)
432 page_index_t i;
433 os_vm_size_t result = 0;
434 for (i = 0; i < last_free_page; i++) {
435 if (page_allocated_p(i)
436 && (page_table[i].gen == gen))
437 result += page_bytes_used(i);
439 return result;
442 /* Return the average age of the memory in a generation. */
443 extern double
444 generation_average_age(generation_index_t gen)
446 if (generations[gen].bytes_allocated == 0)
447 return 0.0;
449 return
450 ((double)generations[gen].cum_sum_bytes_allocated)
451 / ((double)generations[gen].bytes_allocated);
454 #ifdef LISP_FEATURE_X86
455 extern void fpu_save(void *);
456 extern void fpu_restore(void *);
457 #endif
459 extern void
460 write_generation_stats(FILE *file)
462 generation_index_t i;
464 #ifdef LISP_FEATURE_X86
465 int fpu_state[27];
467 /* Can end up here after calling alloc_tramp which doesn't prepare
468 * the x87 state, and the C ABI uses a different mode */
469 fpu_save(fpu_state);
470 #endif
472 /* Print the heap stats. */
473 fprintf(file,
474 " Gen StaPg UbSta LaSta LUbSt Boxed Unboxed LB LUB !move Alloc Waste Trig WP GCs Mem-age\n");
476 for (i = 0; i < SCRATCH_GENERATION; i++) {
477 page_index_t j;
478 page_index_t boxed_cnt = 0;
479 page_index_t unboxed_cnt = 0;
480 page_index_t large_boxed_cnt = 0;
481 page_index_t large_unboxed_cnt = 0;
482 page_index_t pinned_cnt=0;
484 for (j = 0; j < last_free_page; j++)
485 if (page_table[j].gen == i) {
487 /* Count the number of boxed pages within the given
488 * generation. */
489 if (page_boxed_p(j)) {
490 if (page_table[j].large_object)
491 large_boxed_cnt++;
492 else
493 boxed_cnt++;
495 if(page_table[j].dont_move) pinned_cnt++;
496 /* Count the number of unboxed pages within the given
497 * generation. */
498 if (page_unboxed_p(j)) {
499 if (page_table[j].large_object)
500 large_unboxed_cnt++;
501 else
502 unboxed_cnt++;
506 gc_assert(generations[i].bytes_allocated
507 == count_generation_bytes_allocated(i));
508 fprintf(file,
509 " %1d: %5ld %5ld %5ld %5ld",
511 (long)generations[i].alloc_start_page,
512 (long)generations[i].alloc_unboxed_start_page,
513 (long)generations[i].alloc_large_start_page,
514 (long)generations[i].alloc_large_unboxed_start_page);
515 fprintf(file,
516 " %5"PAGE_INDEX_FMT" %5"PAGE_INDEX_FMT" %5"PAGE_INDEX_FMT
517 " %5"PAGE_INDEX_FMT" %5"PAGE_INDEX_FMT,
518 boxed_cnt, unboxed_cnt, large_boxed_cnt,
519 large_unboxed_cnt, pinned_cnt);
520 fprintf(file,
521 " %8"OS_VM_SIZE_FMT
522 " %5"OS_VM_SIZE_FMT
523 " %8"OS_VM_SIZE_FMT
524 " %4"PAGE_INDEX_FMT" %3d %7.4f\n",
525 generations[i].bytes_allocated,
526 (npage_bytes(count_generation_pages(i)) - generations[i].bytes_allocated),
527 generations[i].gc_trigger,
528 count_write_protect_generation_pages(i),
529 generations[i].num_gc,
530 generation_average_age(i));
532 fprintf(file," Total bytes allocated = %"OS_VM_SIZE_FMT"\n", bytes_allocated);
533 fprintf(file," Dynamic-space-size bytes = %"OS_VM_SIZE_FMT"\n", dynamic_space_size);
535 #ifdef LISP_FEATURE_X86
536 fpu_restore(fpu_state);
537 #endif
540 extern void
541 write_heap_exhaustion_report(FILE *file, long available, long requested,
542 struct thread *thread)
544 fprintf(file,
545 "Heap exhausted during %s: %ld bytes available, %ld requested.\n",
546 gc_active_p ? "garbage collection" : "allocation",
547 available,
548 requested);
549 write_generation_stats(file);
550 fprintf(file, "GC control variables:\n");
551 fprintf(file, " *GC-INHIBIT* = %s\n *GC-PENDING* = %s\n",
552 SymbolValue(GC_INHIBIT,thread)==NIL ? "false" : "true",
553 (SymbolValue(GC_PENDING, thread) == T) ?
554 "true" : ((SymbolValue(GC_PENDING, thread) == NIL) ?
555 "false" : "in progress"));
556 #ifdef LISP_FEATURE_SB_THREAD
557 fprintf(file, " *STOP-FOR-GC-PENDING* = %s\n",
558 SymbolValue(STOP_FOR_GC_PENDING,thread)==NIL ? "false" : "true");
559 #endif
562 extern void
563 print_generation_stats(void)
565 write_generation_stats(stderr);
568 extern char* gc_logfile;
569 char * gc_logfile = NULL;
571 extern void
572 log_generation_stats(char *logfile, char *header)
574 if (logfile) {
575 FILE * log = fopen(logfile, "a");
576 if (log) {
577 fprintf(log, "%s\n", header);
578 write_generation_stats(log);
579 fclose(log);
580 } else {
581 fprintf(stderr, "Could not open gc logfile: %s\n", logfile);
582 fflush(stderr);
587 extern void
588 report_heap_exhaustion(long available, long requested, struct thread *th)
590 if (gc_logfile) {
591 FILE * log = fopen(gc_logfile, "a");
592 if (log) {
593 write_heap_exhaustion_report(log, available, requested, th);
594 fclose(log);
595 } else {
596 fprintf(stderr, "Could not open gc logfile: %s\n", gc_logfile);
597 fflush(stderr);
600 /* Always to stderr as well. */
601 write_heap_exhaustion_report(stderr, available, requested, th);
605 #if defined(LISP_FEATURE_X86)
606 void fast_bzero(void*, size_t); /* in <arch>-assem.S */
607 #endif
609 /* Zero the pages from START to END (inclusive), but use mmap/munmap instead
610 * if zeroing it ourselves, i.e. in practice give the memory back to the
611 * OS. Generally done after a large GC.
613 void zero_pages_with_mmap(page_index_t start, page_index_t end) {
614 page_index_t i;
615 void *addr = page_address(start), *new_addr;
616 os_vm_size_t length = npage_bytes(1+end-start);
618 if (start > end)
619 return;
621 gc_assert(length >= gencgc_release_granularity);
622 gc_assert((length % gencgc_release_granularity) == 0);
624 #ifdef LISP_FEATURE_LINUX
625 extern os_vm_address_t anon_dynamic_space_start;
626 // We use MADV_DONTNEED only on Linux due to differing semantics from BSD.
627 // Linux treats it as a demand that the memory be 0-filled, or refreshed
628 // from a file that backs the range. BSD takes it as a hint that you don't
629 // care if the memory has to brought in from swap when next accessed,
630 // i.e. it's not a request to make a user-visible alteration to memory.
631 // So in theory this can bring a page in from the core file, if we happen
632 // to hit a page that resides in the portion of memory mapped by coreparse.
633 // In practice this should not happen because objects from a core file can't
634 // become garbage. Except in save-lisp-and-die they can, and we must be
635 // cautious not to resurrect bytes that originally came from the file.
636 if ((os_vm_address_t)addr >= anon_dynamic_space_start) {
637 if (madvise(addr, length, MADV_DONTNEED) != 0)
638 lose("madvise failed\n");
639 } else
640 #endif
642 os_invalidate(addr, length);
643 new_addr = os_validate(addr, length);
644 if (new_addr == NULL || new_addr != addr) {
645 lose("remap_free_pages: page moved, 0x%08x ==> 0x%08x",
646 start, new_addr);
650 for (i = start; i <= end; i++)
651 set_page_need_to_zero(i, 0);
654 /* Zero the pages from START to END (inclusive). Generally done just after
655 * a new region has been allocated.
657 static void
658 zero_pages(page_index_t start, page_index_t end) {
659 if (start > end)
660 return;
662 #if defined(LISP_FEATURE_X86)
663 fast_bzero(page_address(start), npage_bytes(1+end-start));
664 #else
665 bzero(page_address(start), npage_bytes(1+end-start));
666 #endif
670 static void
671 zero_and_mark_pages(page_index_t start, page_index_t end) {
672 page_index_t i;
674 zero_pages(start, end);
675 for (i = start; i <= end; i++)
676 set_page_need_to_zero(i, 0);
679 /* Zero the pages from START to END (inclusive), except for those
680 * pages that are known to already zeroed. Mark all pages in the
681 * ranges as non-zeroed.
683 static void
684 zero_dirty_pages(page_index_t start, page_index_t end) {
685 page_index_t i, j;
687 for (i = start; i <= end; i++) {
688 if (!page_need_to_zero(i)) continue;
689 for (j = i+1; (j <= end) && page_need_to_zero(j) ; j++)
690 ; /* empty body */
691 zero_pages(i, j-1);
692 i = j;
695 for (i = start; i <= end; i++) {
696 set_page_need_to_zero(i, 1);
702 * To support quick and inline allocation, regions of memory can be
703 * allocated and then allocated from with just a free pointer and a
704 * check against an end address.
706 * Since objects can be allocated to spaces with different properties
707 * e.g. boxed/unboxed, generation, ages; there may need to be many
708 * allocation regions.
710 * Each allocation region may start within a partly used page. Many
711 * features of memory use are noted on a page wise basis, e.g. the
712 * generation; so if a region starts within an existing allocated page
713 * it must be consistent with this page.
715 * During the scavenging of the newspace, objects will be transported
716 * into an allocation region, and pointers updated to point to this
717 * allocation region. It is possible that these pointers will be
718 * scavenged again before the allocation region is closed, e.g. due to
719 * trans_list which jumps all over the place to cleanup the list. It
720 * is important to be able to determine properties of all objects
721 * pointed to when scavenging, e.g to detect pointers to the oldspace.
722 * Thus it's important that the allocation regions have the correct
723 * properties set when allocated, and not just set when closed. The
724 * region allocation routines return regions with the specified
725 * properties, and grab all the pages, setting their properties
726 * appropriately, except that the amount used is not known.
728 * These regions are used to support quicker allocation using just a
729 * free pointer. The actual space used by the region is not reflected
730 * in the pages tables until it is closed. It can't be scavenged until
731 * closed.
733 * When finished with the region it should be closed, which will
734 * update the page tables for the actual space used returning unused
735 * space. Further it may be noted in the new regions which is
736 * necessary when scavenging the newspace.
738 * Large objects may be allocated directly without an allocation
739 * region, the page tables are updated immediately.
741 * Unboxed objects don't contain pointers to other objects and so
742 * don't need scavenging. Further they can't contain pointers to
743 * younger generations so WP is not needed. By allocating pages to
744 * unboxed objects the whole page never needs scavenging or
745 * write-protecting. */
747 /* We are only using two regions at present. Both are for the current
748 * newspace generation. */
749 struct alloc_region boxed_region;
750 struct alloc_region unboxed_region;
752 /* The generation currently being allocated to. */
753 static generation_index_t gc_alloc_generation;
755 static inline page_index_t
756 generation_alloc_start_page(generation_index_t generation, int page_type_flag, int large)
758 if (large) {
759 if (UNBOXED_PAGE_FLAG == page_type_flag) {
760 return generations[generation].alloc_large_unboxed_start_page;
761 } else if (BOXED_PAGE_FLAG & page_type_flag) {
762 /* Both code and data. */
763 return generations[generation].alloc_large_start_page;
764 } else {
765 lose("bad page type flag: %d", page_type_flag);
767 } else {
768 if (UNBOXED_PAGE_FLAG == page_type_flag) {
769 return generations[generation].alloc_unboxed_start_page;
770 } else if (BOXED_PAGE_FLAG & page_type_flag) {
771 /* Both code and data. */
772 return generations[generation].alloc_start_page;
773 } else {
774 lose("bad page_type_flag: %d", page_type_flag);
779 static inline void
780 set_generation_alloc_start_page(generation_index_t generation, int page_type_flag, int large,
781 page_index_t page)
783 if (large) {
784 if (UNBOXED_PAGE_FLAG == page_type_flag) {
785 generations[generation].alloc_large_unboxed_start_page = page;
786 } else if (BOXED_PAGE_FLAG & page_type_flag) {
787 /* Both code and data. */
788 generations[generation].alloc_large_start_page = page;
789 } else {
790 lose("bad page type flag: %d", page_type_flag);
792 } else {
793 if (UNBOXED_PAGE_FLAG == page_type_flag) {
794 generations[generation].alloc_unboxed_start_page = page;
795 } else if (BOXED_PAGE_FLAG & page_type_flag) {
796 /* Both code and data. */
797 generations[generation].alloc_start_page = page;
798 } else {
799 lose("bad page type flag: %d", page_type_flag);
804 const int n_dwords_in_card = GENCGC_CARD_BYTES / N_WORD_BYTES / 2;
805 in_use_marker_t *
806 pinned_dwords(page_index_t page)
808 if (page_table[page].has_pin_map)
809 return &page_table_pinned_dwords[page * (n_dwords_in_card/N_WORD_BITS)];
810 return NULL;
813 /* Find a new region with room for at least the given number of bytes.
815 * It starts looking at the current generation's alloc_start_page. So
816 * may pick up from the previous region if there is enough space. This
817 * keeps the allocation contiguous when scavenging the newspace.
819 * The alloc_region should have been closed by a call to
820 * gc_alloc_update_page_tables(), and will thus be in an empty state.
822 * To assist the scavenging functions write-protected pages are not
823 * used. Free pages should not be write-protected.
825 * It is critical to the conservative GC that the start of regions be
826 * known. To help achieve this only small regions are allocated at a
827 * time.
829 * During scavenging, pointers may be found to within the current
830 * region and the page generation must be set so that pointers to the
831 * from space can be recognized. Therefore the generation of pages in
832 * the region are set to gc_alloc_generation. To prevent another
833 * allocation call using the same pages, all the pages in the region
834 * are allocated, although they will initially be empty.
836 static void
837 gc_alloc_new_region(sword_t nbytes, int page_type_flag, struct alloc_region *alloc_region)
839 page_index_t first_page;
840 page_index_t last_page;
841 os_vm_size_t bytes_found;
842 page_index_t i;
843 int ret;
846 FSHOW((stderr,
847 "/alloc_new_region for %d bytes from gen %d\n",
848 nbytes, gc_alloc_generation));
851 /* Check that the region is in a reset state. */
852 gc_assert((alloc_region->first_page == 0)
853 && (alloc_region->last_page == -1)
854 && (alloc_region->free_pointer == alloc_region->end_addr));
855 ret = thread_mutex_lock(&free_pages_lock);
856 gc_assert(ret == 0);
857 first_page = generation_alloc_start_page(gc_alloc_generation, page_type_flag, 0);
858 last_page=gc_find_freeish_pages(&first_page, nbytes, page_type_flag);
859 bytes_found=(GENCGC_CARD_BYTES - page_bytes_used(first_page))
860 + npage_bytes(last_page-first_page);
862 /* Set up the alloc_region. */
863 alloc_region->first_page = first_page;
864 alloc_region->last_page = last_page;
865 alloc_region->start_addr = page_bytes_used(first_page)
866 + page_address(first_page);
867 alloc_region->free_pointer = alloc_region->start_addr;
868 alloc_region->end_addr = alloc_region->start_addr + bytes_found;
870 /* Set up the pages. */
872 /* The first page may have already been in use. */
873 if (page_bytes_used(first_page) == 0) {
874 page_table[first_page].allocated = page_type_flag;
875 page_table[first_page].gen = gc_alloc_generation;
876 page_table[first_page].large_object = 0;
877 set_page_scan_start_offset(first_page, 0);
878 // wiping should have free()ed and :=NULL
879 gc_assert(pinned_dwords(first_page) == NULL);
882 gc_assert(page_table[first_page].allocated == page_type_flag);
883 page_table[first_page].allocated |= OPEN_REGION_PAGE_FLAG;
885 gc_assert(page_table[first_page].gen == gc_alloc_generation);
886 gc_assert(page_table[first_page].large_object == 0);
888 for (i = first_page+1; i <= last_page; i++) {
889 page_table[i].allocated = page_type_flag;
890 page_table[i].gen = gc_alloc_generation;
891 page_table[i].large_object = 0;
892 /* This may not be necessary for unboxed regions (think it was
893 * broken before!) */
894 set_page_scan_start_offset(i,
895 void_diff(page_address(i), alloc_region->start_addr));
896 page_table[i].allocated |= OPEN_REGION_PAGE_FLAG ;
898 /* Bump up last_free_page. */
899 if (last_page+1 > last_free_page) {
900 last_free_page = last_page+1;
901 /* do we only want to call this on special occasions? like for
902 * boxed_region? */
903 set_alloc_pointer((lispobj)page_address(last_free_page));
905 ret = thread_mutex_unlock(&free_pages_lock);
906 gc_assert(ret == 0);
908 #ifdef READ_PROTECT_FREE_PAGES
909 os_protect(page_address(first_page),
910 npage_bytes(1+last_page-first_page),
911 OS_VM_PROT_ALL);
912 #endif
914 /* If the first page was only partial, don't check whether it's
915 * zeroed (it won't be) and don't zero it (since the parts that
916 * we're interested in are guaranteed to be zeroed).
918 if (page_bytes_used(first_page)) {
919 first_page++;
922 zero_dirty_pages(first_page, last_page);
924 /* we can do this after releasing free_pages_lock */
925 if (gencgc_zero_check) {
926 word_t *p;
927 for (p = (word_t *)alloc_region->start_addr;
928 p < (word_t *)alloc_region->end_addr; p++) {
929 if (*p != 0) {
930 lose("The new region is not zero at %p (start=%p, end=%p).\n",
931 p, alloc_region->start_addr, alloc_region->end_addr);
937 /* If the record_new_objects flag is 2 then all new regions created
938 * are recorded.
940 * If it's 1 then then it is only recorded if the first page of the
941 * current region is <= new_areas_ignore_page. This helps avoid
942 * unnecessary recording when doing full scavenge pass.
944 * The new_object structure holds the page, byte offset, and size of
945 * new regions of objects. Each new area is placed in the array of
946 * these structures pointer to by new_areas. new_areas_index holds the
947 * offset into new_areas.
949 * If new_area overflows NUM_NEW_AREAS then it stops adding them. The
950 * later code must detect this and handle it, probably by doing a full
951 * scavenge of a generation. */
952 #define NUM_NEW_AREAS 512
953 static int record_new_objects = 0;
954 static page_index_t new_areas_ignore_page;
955 struct new_area {
956 page_index_t page;
957 size_t offset;
958 size_t size;
960 static struct new_area (*new_areas)[];
961 static size_t new_areas_index;
962 size_t max_new_areas;
964 /* Add a new area to new_areas. */
965 static void
966 add_new_area(page_index_t first_page, size_t offset, size_t size)
968 size_t new_area_start, c;
969 ssize_t i;
971 /* Ignore if full. */
972 if (new_areas_index >= NUM_NEW_AREAS)
973 return;
975 switch (record_new_objects) {
976 case 0:
977 return;
978 case 1:
979 if (first_page > new_areas_ignore_page)
980 return;
981 break;
982 case 2:
983 break;
984 default:
985 gc_abort();
988 new_area_start = npage_bytes(first_page) + offset;
990 /* Search backwards for a prior area that this follows from. If
991 found this will save adding a new area. */
992 for (i = new_areas_index-1, c = 0; (i >= 0) && (c < 8); i--, c++) {
993 size_t area_end =
994 npage_bytes((*new_areas)[i].page)
995 + (*new_areas)[i].offset
996 + (*new_areas)[i].size;
997 /*FSHOW((stderr,
998 "/add_new_area S1 %d %d %d %d\n",
999 i, c, new_area_start, area_end));*/
1000 if (new_area_start == area_end) {
1001 /*FSHOW((stderr,
1002 "/adding to [%d] %d %d %d with %d %d %d:\n",
1004 (*new_areas)[i].page,
1005 (*new_areas)[i].offset,
1006 (*new_areas)[i].size,
1007 first_page,
1008 offset,
1009 size);*/
1010 (*new_areas)[i].size += size;
1011 return;
1015 (*new_areas)[new_areas_index].page = first_page;
1016 (*new_areas)[new_areas_index].offset = offset;
1017 (*new_areas)[new_areas_index].size = size;
1018 /*FSHOW((stderr,
1019 "/new_area %d page %d offset %d size %d\n",
1020 new_areas_index, first_page, offset, size));*/
1021 new_areas_index++;
1023 /* Note the max new_areas used. */
1024 if (new_areas_index > max_new_areas)
1025 max_new_areas = new_areas_index;
1028 /* Update the tables for the alloc_region. The region may be added to
1029 * the new_areas.
1031 * When done the alloc_region is set up so that the next quick alloc
1032 * will fail safely and thus a new region will be allocated. Further
1033 * it is safe to try to re-update the page table of this reset
1034 * alloc_region. */
1035 void
1036 gc_alloc_update_page_tables(int page_type_flag, struct alloc_region *alloc_region)
1038 boolean more;
1039 page_index_t first_page;
1040 page_index_t next_page;
1041 os_vm_size_t bytes_used;
1042 os_vm_size_t region_size;
1043 os_vm_size_t byte_cnt;
1044 page_bytes_t orig_first_page_bytes_used;
1045 int ret;
1048 first_page = alloc_region->first_page;
1050 /* Catch an unused alloc_region. */
1051 if ((first_page == 0) && (alloc_region->last_page == -1))
1052 return;
1054 next_page = first_page+1;
1056 ret = thread_mutex_lock(&free_pages_lock);
1057 gc_assert(ret == 0);
1058 if (alloc_region->free_pointer != alloc_region->start_addr) {
1059 /* some bytes were allocated in the region */
1060 orig_first_page_bytes_used = page_bytes_used(first_page);
1062 gc_assert(alloc_region->start_addr ==
1063 (page_address(first_page) + page_bytes_used(first_page)));
1065 /* All the pages used need to be updated */
1067 /* Update the first page. */
1069 /* If the page was free then set up the gen, and
1070 * scan_start_offset. */
1071 if (page_bytes_used(first_page) == 0)
1072 gc_assert(page_starts_contiguous_block_p(first_page));
1073 page_table[first_page].allocated &= ~(OPEN_REGION_PAGE_FLAG);
1075 gc_assert(page_table[first_page].allocated & page_type_flag);
1076 gc_assert(page_table[first_page].gen == gc_alloc_generation);
1077 gc_assert(page_table[first_page].large_object == 0);
1079 byte_cnt = 0;
1081 /* Calculate the number of bytes used in this page. This is not
1082 * always the number of new bytes, unless it was free. */
1083 more = 0;
1084 if ((bytes_used = void_diff(alloc_region->free_pointer,
1085 page_address(first_page)))
1086 >GENCGC_CARD_BYTES) {
1087 bytes_used = GENCGC_CARD_BYTES;
1088 more = 1;
1090 set_page_bytes_used(first_page, bytes_used);
1091 byte_cnt += bytes_used;
1094 /* All the rest of the pages should be free. We need to set
1095 * their scan_start_offset pointer to the start of the
1096 * region, and set the bytes_used. */
1097 while (more) {
1098 page_table[next_page].allocated &= ~(OPEN_REGION_PAGE_FLAG);
1099 gc_assert(page_table[next_page].allocated & page_type_flag);
1100 gc_assert(page_bytes_used(next_page) == 0);
1101 gc_assert(page_table[next_page].gen == gc_alloc_generation);
1102 gc_assert(page_table[next_page].large_object == 0);
1103 gc_assert(page_scan_start_offset(next_page) ==
1104 void_diff(page_address(next_page),
1105 alloc_region->start_addr));
1107 /* Calculate the number of bytes used in this page. */
1108 more = 0;
1109 if ((bytes_used = void_diff(alloc_region->free_pointer,
1110 page_address(next_page)))>GENCGC_CARD_BYTES) {
1111 bytes_used = GENCGC_CARD_BYTES;
1112 more = 1;
1114 set_page_bytes_used(next_page, bytes_used);
1115 byte_cnt += bytes_used;
1117 next_page++;
1120 region_size = void_diff(alloc_region->free_pointer,
1121 alloc_region->start_addr);
1122 bytes_allocated += region_size;
1123 generations[gc_alloc_generation].bytes_allocated += region_size;
1125 gc_assert((byte_cnt- orig_first_page_bytes_used) == region_size);
1127 /* Set the generations alloc restart page to the last page of
1128 * the region. */
1129 set_generation_alloc_start_page(gc_alloc_generation, page_type_flag, 0, next_page-1);
1131 /* Add the region to the new_areas if requested. */
1132 if (BOXED_PAGE_FLAG & page_type_flag)
1133 add_new_area(first_page,orig_first_page_bytes_used, region_size);
1136 FSHOW((stderr,
1137 "/gc_alloc_update_page_tables update %d bytes to gen %d\n",
1138 region_size,
1139 gc_alloc_generation));
1141 } else {
1142 /* There are no bytes allocated. Unallocate the first_page if
1143 * there are 0 bytes_used. */
1144 page_table[first_page].allocated &= ~(OPEN_REGION_PAGE_FLAG);
1145 if (page_bytes_used(first_page) == 0)
1146 page_table[first_page].allocated = FREE_PAGE_FLAG;
1149 /* Unallocate any unused pages. */
1150 while (next_page <= alloc_region->last_page) {
1151 gc_assert(page_bytes_used(next_page) == 0);
1152 page_table[next_page].allocated = FREE_PAGE_FLAG;
1153 next_page++;
1155 ret = thread_mutex_unlock(&free_pages_lock);
1156 gc_assert(ret == 0);
1158 /* alloc_region is per-thread, we're ok to do this unlocked */
1159 gc_set_region_empty(alloc_region);
1162 /* Allocate a possibly large object. */
1163 void *
1164 gc_alloc_large(sword_t nbytes, int page_type_flag, struct alloc_region *alloc_region)
1166 boolean more;
1167 page_index_t first_page, next_page, last_page;
1168 page_bytes_t orig_first_page_bytes_used;
1169 os_vm_size_t byte_cnt;
1170 os_vm_size_t bytes_used;
1171 int ret;
1173 ret = thread_mutex_lock(&free_pages_lock);
1174 gc_assert(ret == 0);
1176 first_page = generation_alloc_start_page(gc_alloc_generation, page_type_flag, 1);
1177 if (first_page <= alloc_region->last_page) {
1178 first_page = alloc_region->last_page+1;
1181 last_page=gc_find_freeish_pages(&first_page,nbytes, page_type_flag);
1183 gc_assert(first_page > alloc_region->last_page);
1185 set_generation_alloc_start_page(gc_alloc_generation, page_type_flag, 1, last_page);
1187 /* Set up the pages. */
1188 orig_first_page_bytes_used = page_bytes_used(first_page);
1190 /* If the first page was free then set up the gen, and
1191 * scan_start_offset. */
1192 if (page_bytes_used(first_page) == 0) {
1193 page_table[first_page].allocated = page_type_flag;
1194 page_table[first_page].gen = gc_alloc_generation;
1195 set_page_scan_start_offset(first_page, 0);
1196 page_table[first_page].large_object = 1;
1199 gc_assert(page_table[first_page].allocated == page_type_flag);
1200 gc_assert(page_table[first_page].gen == gc_alloc_generation);
1201 gc_assert(page_table[first_page].large_object == 1);
1203 byte_cnt = 0;
1205 /* Calc. the number of bytes used in this page. This is not
1206 * always the number of new bytes, unless it was free. */
1207 more = 0;
1208 if ((bytes_used = nbytes+orig_first_page_bytes_used) > GENCGC_CARD_BYTES) {
1209 bytes_used = GENCGC_CARD_BYTES;
1210 more = 1;
1212 set_page_bytes_used(first_page, bytes_used);
1213 byte_cnt += bytes_used;
1215 next_page = first_page+1;
1217 /* All the rest of the pages should be free. We need to set their
1218 * scan_start_offset pointer to the start of the region, and set
1219 * the bytes_used. */
1220 while (more) {
1221 gc_assert(page_free_p(next_page));
1222 gc_assert(page_bytes_used(next_page) == 0);
1223 page_table[next_page].allocated = page_type_flag;
1224 page_table[next_page].gen = gc_alloc_generation;
1225 page_table[next_page].large_object = 1;
1227 set_page_scan_start_offset(next_page,
1228 npage_bytes(next_page-first_page) - orig_first_page_bytes_used);
1230 /* Calculate the number of bytes used in this page. */
1231 more = 0;
1232 bytes_used=(nbytes+orig_first_page_bytes_used)-byte_cnt;
1233 if (bytes_used > GENCGC_CARD_BYTES) {
1234 bytes_used = GENCGC_CARD_BYTES;
1235 more = 1;
1237 set_page_bytes_used(next_page, bytes_used);
1238 page_table[next_page].write_protected=0;
1239 page_table[next_page].dont_move=0;
1240 byte_cnt += bytes_used;
1241 next_page++;
1244 gc_assert((byte_cnt-orig_first_page_bytes_used) == (size_t)nbytes);
1246 bytes_allocated += nbytes;
1247 generations[gc_alloc_generation].bytes_allocated += nbytes;
1249 /* Add the region to the new_areas if requested. */
1250 if (BOXED_PAGE_FLAG & page_type_flag)
1251 add_new_area(first_page,orig_first_page_bytes_used,nbytes);
1253 /* Bump up last_free_page */
1254 if (last_page+1 > last_free_page) {
1255 last_free_page = last_page+1;
1256 set_alloc_pointer((lispobj)(page_address(last_free_page)));
1258 ret = thread_mutex_unlock(&free_pages_lock);
1259 gc_assert(ret == 0);
1261 #ifdef READ_PROTECT_FREE_PAGES
1262 os_protect(page_address(first_page),
1263 npage_bytes(1+last_page-first_page),
1264 OS_VM_PROT_ALL);
1265 #endif
1267 zero_dirty_pages(first_page, last_page);
1269 return page_address(first_page);
1272 static page_index_t gencgc_alloc_start_page = -1;
1274 void
1275 gc_heap_exhausted_error_or_lose (sword_t available, sword_t requested)
1277 struct thread *thread = arch_os_get_current_thread();
1278 /* Write basic information before doing anything else: if we don't
1279 * call to lisp this is a must, and even if we do there is always
1280 * the danger that we bounce back here before the error has been
1281 * handled, or indeed even printed.
1283 report_heap_exhaustion(available, requested, thread);
1284 if (gc_active_p || (available == 0)) {
1285 /* If we are in GC, or totally out of memory there is no way
1286 * to sanely transfer control to the lisp-side of things.
1288 lose("Heap exhausted, game over.");
1290 else {
1291 /* FIXME: assert free_pages_lock held */
1292 (void)thread_mutex_unlock(&free_pages_lock);
1293 #if !(defined(LISP_FEATURE_WIN32) && defined(LISP_FEATURE_SB_THREAD))
1294 gc_assert(get_pseudo_atomic_atomic(thread));
1295 clear_pseudo_atomic_atomic(thread);
1296 if (get_pseudo_atomic_interrupted(thread))
1297 do_pending_interrupt();
1298 #endif
1299 /* Another issue is that signalling HEAP-EXHAUSTED error leads
1300 * to running user code at arbitrary places, even in a
1301 * WITHOUT-INTERRUPTS which may lead to a deadlock without
1302 * running out of the heap. So at this point all bets are
1303 * off. */
1304 if (SymbolValue(INTERRUPTS_ENABLED,thread) == NIL)
1305 corruption_warning_and_maybe_lose
1306 ("Signalling HEAP-EXHAUSTED in a WITHOUT-INTERRUPTS.");
1307 /* available and requested should be double word aligned, thus
1308 they can passed as fixnums and shifted later. */
1309 funcall2(StaticSymbolFunction(HEAP_EXHAUSTED_ERROR), available, requested);
1310 lose("HEAP-EXHAUSTED-ERROR fell through");
1314 page_index_t
1315 gc_find_freeish_pages(page_index_t *restart_page_ptr, sword_t bytes,
1316 int page_type_flag)
1318 page_index_t most_bytes_found_from = 0, most_bytes_found_to = 0;
1319 page_index_t first_page, last_page, restart_page = *restart_page_ptr;
1320 os_vm_size_t nbytes = bytes;
1321 os_vm_size_t nbytes_goal = nbytes;
1322 os_vm_size_t bytes_found = 0;
1323 os_vm_size_t most_bytes_found = 0;
1324 boolean small_object = nbytes < GENCGC_CARD_BYTES;
1325 /* FIXME: assert(free_pages_lock is held); */
1327 if (nbytes_goal < gencgc_alloc_granularity)
1328 nbytes_goal = gencgc_alloc_granularity;
1330 /* Toggled by gc_and_save for heap compaction, normally -1. */
1331 if (gencgc_alloc_start_page != -1) {
1332 restart_page = gencgc_alloc_start_page;
1335 /* FIXME: This is on bytes instead of nbytes pending cleanup of
1336 * long from the interface. */
1337 gc_assert(bytes>=0);
1338 /* Search for a page with at least nbytes of space. We prefer
1339 * not to split small objects on multiple pages, to reduce the
1340 * number of contiguous allocation regions spaning multiple
1341 * pages: this helps avoid excessive conservativism.
1343 * For other objects, we guarantee that they start on their own
1344 * page boundary.
1346 first_page = restart_page;
1347 while (first_page < page_table_pages) {
1348 bytes_found = 0;
1349 if (page_free_p(first_page)) {
1350 gc_assert(0 == page_bytes_used(first_page));
1351 bytes_found = GENCGC_CARD_BYTES;
1352 } else if (small_object &&
1353 (page_table[first_page].allocated == page_type_flag) &&
1354 (page_table[first_page].large_object == 0) &&
1355 (page_table[first_page].gen == gc_alloc_generation) &&
1356 (page_table[first_page].write_protected == 0) &&
1357 (page_table[first_page].dont_move == 0)) {
1358 bytes_found = GENCGC_CARD_BYTES - page_bytes_used(first_page);
1359 if (bytes_found < nbytes) {
1360 if (bytes_found > most_bytes_found)
1361 most_bytes_found = bytes_found;
1362 first_page++;
1363 continue;
1365 } else {
1366 first_page++;
1367 continue;
1370 gc_assert(page_table[first_page].write_protected == 0);
1371 for (last_page = first_page+1;
1372 ((last_page < page_table_pages) &&
1373 page_free_p(last_page) &&
1374 (bytes_found < nbytes_goal));
1375 last_page++) {
1376 bytes_found += GENCGC_CARD_BYTES;
1377 gc_assert(0 == page_bytes_used(last_page));
1378 gc_assert(0 == page_table[last_page].write_protected);
1381 if (bytes_found > most_bytes_found) {
1382 most_bytes_found = bytes_found;
1383 most_bytes_found_from = first_page;
1384 most_bytes_found_to = last_page;
1386 if (bytes_found >= nbytes_goal)
1387 break;
1389 first_page = last_page;
1392 bytes_found = most_bytes_found;
1393 restart_page = first_page + 1;
1395 /* Check for a failure */
1396 if (bytes_found < nbytes) {
1397 gc_assert(restart_page >= page_table_pages);
1398 gc_heap_exhausted_error_or_lose(most_bytes_found, nbytes);
1401 gc_assert(most_bytes_found_to);
1402 *restart_page_ptr = most_bytes_found_from;
1403 return most_bytes_found_to-1;
1406 /* Allocate bytes. All the rest of the special-purpose allocation
1407 * functions will eventually call this */
1409 void *
1410 gc_alloc_with_region(sword_t nbytes,int page_type_flag, struct alloc_region *my_region,
1411 int quick_p)
1413 void *new_free_pointer;
1415 if (nbytes>=LARGE_OBJECT_SIZE)
1416 return gc_alloc_large(nbytes, page_type_flag, my_region);
1418 /* Check whether there is room in the current alloc region. */
1419 new_free_pointer = my_region->free_pointer + nbytes;
1421 /* fprintf(stderr, "alloc %d bytes from %p to %p\n", nbytes,
1422 my_region->free_pointer, new_free_pointer); */
1424 if (new_free_pointer <= my_region->end_addr) {
1425 /* If so then allocate from the current alloc region. */
1426 void *new_obj = my_region->free_pointer;
1427 my_region->free_pointer = new_free_pointer;
1429 /* Unless a `quick' alloc was requested, check whether the
1430 alloc region is almost empty. */
1431 if (!quick_p &&
1432 void_diff(my_region->end_addr,my_region->free_pointer) <= 32) {
1433 /* If so, finished with the current region. */
1434 gc_alloc_update_page_tables(page_type_flag, my_region);
1435 /* Set up a new region. */
1436 gc_alloc_new_region(32 /*bytes*/, page_type_flag, my_region);
1439 return((void *)new_obj);
1442 /* Else not enough free space in the current region: retry with a
1443 * new region. */
1445 gc_alloc_update_page_tables(page_type_flag, my_region);
1446 gc_alloc_new_region(nbytes, page_type_flag, my_region);
1447 return gc_alloc_with_region(nbytes, page_type_flag, my_region,0);
1450 /* Copy a large object. If the object is in a large object region then
1451 * it is simply promoted, else it is copied. If it's large enough then
1452 * it's copied to a large object region.
1454 * Bignums and vectors may have shrunk. If the object is not copied
1455 * the space needs to be reclaimed, and the page_tables corrected. */
1456 static lispobj
1457 general_copy_large_object(lispobj object, word_t nwords, boolean boxedp)
1459 lispobj *new;
1460 page_index_t first_page;
1462 CHECK_COPY_PRECONDITIONS(object, nwords);
1464 if ((nwords > 1024*1024) && gencgc_verbose) {
1465 FSHOW((stderr, "/general_copy_large_object: %d bytes\n",
1466 nwords*N_WORD_BYTES));
1469 /* Check whether it's a large object. */
1470 first_page = find_page_index((void *)object);
1471 gc_assert(first_page >= 0);
1473 if (page_table[first_page].large_object) {
1474 /* Promote the object. Note: Unboxed objects may have been
1475 * allocated to a BOXED region so it may be necessary to
1476 * change the region to UNBOXED. */
1477 os_vm_size_t remaining_bytes;
1478 os_vm_size_t bytes_freed;
1479 page_index_t next_page;
1480 page_bytes_t old_bytes_used;
1482 /* FIXME: This comment is somewhat stale.
1484 * Note: Any page write-protection must be removed, else a
1485 * later scavenge_newspace may incorrectly not scavenge these
1486 * pages. This would not be necessary if they are added to the
1487 * new areas, but let's do it for them all (they'll probably
1488 * be written anyway?). */
1490 gc_assert(page_starts_contiguous_block_p(first_page));
1491 next_page = first_page;
1492 remaining_bytes = nwords*N_WORD_BYTES;
1494 while (remaining_bytes > GENCGC_CARD_BYTES) {
1495 gc_assert(page_table[next_page].gen == from_space);
1496 gc_assert(page_table[next_page].large_object);
1497 gc_assert(page_scan_start_offset(next_page) ==
1498 npage_bytes(next_page-first_page));
1499 gc_assert(page_bytes_used(next_page) == GENCGC_CARD_BYTES);
1500 /* Should have been unprotected by unprotect_oldspace()
1501 * for boxed objects, and after promotion unboxed ones
1502 * should not be on protected pages at all. */
1503 gc_assert(!page_table[next_page].write_protected);
1505 if (boxedp)
1506 gc_assert(page_boxed_p(next_page));
1507 else {
1508 gc_assert(page_allocated_no_region_p(next_page));
1509 page_table[next_page].allocated = UNBOXED_PAGE_FLAG;
1511 page_table[next_page].gen = new_space;
1513 remaining_bytes -= GENCGC_CARD_BYTES;
1514 next_page++;
1517 /* Now only one page remains, but the object may have shrunk so
1518 * there may be more unused pages which will be freed. */
1520 /* Object may have shrunk but shouldn't have grown - check. */
1521 gc_assert(page_bytes_used(next_page) >= remaining_bytes);
1523 page_table[next_page].gen = new_space;
1525 if (boxedp)
1526 gc_assert(page_boxed_p(next_page));
1527 else
1528 page_table[next_page].allocated = UNBOXED_PAGE_FLAG;
1530 /* Adjust the bytes_used. */
1531 old_bytes_used = page_bytes_used(next_page);
1532 set_page_bytes_used(next_page, remaining_bytes);
1534 bytes_freed = old_bytes_used - remaining_bytes;
1536 /* Free any remaining pages; needs care. */
1537 next_page++;
1538 while ((old_bytes_used == GENCGC_CARD_BYTES) &&
1539 (page_table[next_page].gen == from_space) &&
1540 /* FIXME: It is not obvious to me why this is necessary
1541 * as a loop condition: it seems to me that the
1542 * scan_start_offset test should be sufficient, but
1543 * experimentally that is not the case. --NS
1544 * 2011-11-28 */
1545 (boxedp ?
1546 page_boxed_p(next_page) :
1547 page_allocated_no_region_p(next_page)) &&
1548 page_table[next_page].large_object &&
1549 (page_scan_start_offset(next_page) ==
1550 npage_bytes(next_page - first_page))) {
1551 /* Checks out OK, free the page. Don't need to both zeroing
1552 * pages as this should have been done before shrinking the
1553 * object. These pages shouldn't be write-protected, even if
1554 * boxed they should be zero filled. */
1555 gc_assert(page_table[next_page].write_protected == 0);
1557 old_bytes_used = page_bytes_used(next_page);
1558 page_table[next_page].allocated = FREE_PAGE_FLAG;
1559 set_page_bytes_used(next_page, 0);
1560 bytes_freed += old_bytes_used;
1561 next_page++;
1564 if ((bytes_freed > 0) && gencgc_verbose) {
1565 FSHOW((stderr,
1566 "/general_copy_large_object bytes_freed=%"OS_VM_SIZE_FMT"\n",
1567 bytes_freed));
1570 generations[from_space].bytes_allocated -= nwords*N_WORD_BYTES
1571 + bytes_freed;
1572 generations[new_space].bytes_allocated += nwords*N_WORD_BYTES;
1573 bytes_allocated -= bytes_freed;
1575 /* Add the region to the new_areas if requested. */
1576 if (boxedp)
1577 add_new_area(first_page,0,nwords*N_WORD_BYTES);
1579 return(object);
1581 } else {
1582 /* Allocate space. */
1583 new = gc_general_alloc(nwords*N_WORD_BYTES,
1584 (boxedp ? BOXED_PAGE_FLAG : UNBOXED_PAGE_FLAG),
1585 ALLOC_QUICK);
1587 /* Copy the object. */
1588 memcpy(new,native_pointer(object),nwords*N_WORD_BYTES);
1590 /* Return Lisp pointer of new object. */
1591 return make_lispobj(new, lowtag_of(object));
1595 lispobj
1596 copy_large_object(lispobj object, sword_t nwords)
1598 return general_copy_large_object(object, nwords, 1);
1601 lispobj
1602 copy_large_unboxed_object(lispobj object, sword_t nwords)
1604 return general_copy_large_object(object, nwords, 0);
1607 /* to copy unboxed objects */
1608 lispobj
1609 copy_unboxed_object(lispobj object, sword_t nwords)
1611 return gc_general_copy_object(object, nwords, UNBOXED_PAGE_FLAG);
1616 * code and code-related objects
1619 static lispobj trans_fun_header(lispobj object);
1620 static lispobj trans_boxed(lispobj object);
1623 /* Scan a x86 compiled code object, looking for possible fixups that
1624 * have been missed after a move.
1626 * Two types of fixups are needed:
1627 * 1. Absolute fixups to within the code object.
1628 * 2. Relative fixups to outside the code object.
1630 * Currently only absolute fixups to the constant vector, or to the
1631 * code area are checked. */
1632 #ifdef LISP_FEATURE_X86
1633 void
1634 sniff_code_object(struct code *code, os_vm_size_t displacement)
1636 sword_t nheader_words, ncode_words, nwords;
1637 os_vm_address_t constants_start_addr = NULL, constants_end_addr, p;
1638 os_vm_address_t code_start_addr, code_end_addr;
1639 os_vm_address_t code_addr = (os_vm_address_t)code;
1640 int fixup_found = 0;
1642 if (!check_code_fixups)
1643 return;
1645 FSHOW((stderr, "/sniffing code: %p, %lu\n", code, displacement));
1647 ncode_words = code_instruction_words(code->code_size);
1648 nheader_words = code_header_words(*(lispobj *)code);
1649 nwords = ncode_words + nheader_words;
1651 constants_start_addr = code_addr + 5*N_WORD_BYTES;
1652 constants_end_addr = code_addr + nheader_words*N_WORD_BYTES;
1653 code_start_addr = code_addr + nheader_words*N_WORD_BYTES;
1654 code_end_addr = code_addr + nwords*N_WORD_BYTES;
1656 /* Work through the unboxed code. */
1657 for (p = code_start_addr; p < code_end_addr; p++) {
1658 void *data = *(void **)p;
1659 unsigned d1 = *((unsigned char *)p - 1);
1660 unsigned d2 = *((unsigned char *)p - 2);
1661 unsigned d3 = *((unsigned char *)p - 3);
1662 unsigned d4 = *((unsigned char *)p - 4);
1663 #if QSHOW
1664 unsigned d5 = *((unsigned char *)p - 5);
1665 unsigned d6 = *((unsigned char *)p - 6);
1666 #endif
1668 /* Check for code references. */
1669 /* Check for a 32 bit word that looks like an absolute
1670 reference to within the code adea of the code object. */
1671 if ((data >= (void*)(code_start_addr-displacement))
1672 && (data < (void*)(code_end_addr-displacement))) {
1673 /* function header */
1674 if ((d4 == 0x5e)
1675 && (((unsigned)p - 4 - 4*HeaderValue(*((unsigned *)p-1))) ==
1676 (unsigned)code)) {
1677 /* Skip the function header */
1678 p += 6*4 - 4 - 1;
1679 continue;
1681 /* the case of PUSH imm32 */
1682 if (d1 == 0x68) {
1683 fixup_found = 1;
1684 FSHOW((stderr,
1685 "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1686 p, d6, d5, d4, d3, d2, d1, data));
1687 FSHOW((stderr, "/PUSH $0x%.8x\n", data));
1689 /* the case of MOV [reg-8],imm32 */
1690 if ((d3 == 0xc7)
1691 && (d2==0x40 || d2==0x41 || d2==0x42 || d2==0x43
1692 || d2==0x45 || d2==0x46 || d2==0x47)
1693 && (d1 == 0xf8)) {
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, "/MOV [reg-8],$0x%.8x\n", data));
1700 /* the case of LEA reg,[disp32] */
1701 if ((d2 == 0x8d) && ((d1 & 0xc7) == 5)) {
1702 fixup_found = 1;
1703 FSHOW((stderr,
1704 "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1705 p, d6, d5, d4, d3, d2, d1, data));
1706 FSHOW((stderr,"/LEA reg,[$0x%.8x]\n", data));
1710 /* Check for constant references. */
1711 /* Check for a 32 bit word that looks like an absolute
1712 reference to within the constant vector. Constant references
1713 will be aligned. */
1714 if ((data >= (void*)(constants_start_addr-displacement))
1715 && (data < (void*)(constants_end_addr-displacement))
1716 && (((unsigned)data & 0x3) == 0)) {
1717 /* Mov eax,m32 */
1718 if (d1 == 0xa1) {
1719 fixup_found = 1;
1720 FSHOW((stderr,
1721 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1722 p, d6, d5, d4, d3, d2, d1, data));
1723 FSHOW((stderr,"/MOV eax,0x%.8x\n", data));
1726 /* the case of MOV m32,EAX */
1727 if (d1 == 0xa3) {
1728 fixup_found = 1;
1729 FSHOW((stderr,
1730 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1731 p, d6, d5, d4, d3, d2, d1, data));
1732 FSHOW((stderr, "/MOV 0x%.8x,eax\n", data));
1735 /* the case of CMP m32,imm32 */
1736 if ((d1 == 0x3d) && (d2 == 0x81)) {
1737 fixup_found = 1;
1738 FSHOW((stderr,
1739 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1740 p, d6, d5, d4, d3, d2, d1, data));
1741 /* XX Check this */
1742 FSHOW((stderr, "/CMP 0x%.8x,immed32\n", data));
1745 /* Check for a mod=00, r/m=101 byte. */
1746 if ((d1 & 0xc7) == 5) {
1747 /* Cmp m32,reg */
1748 if (d2 == 0x39) {
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 0x%.8x,reg\n", data));
1755 /* the case of CMP reg32,m32 */
1756 if (d2 == 0x3b) {
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, "/CMP reg32,0x%.8x\n", data));
1763 /* the case of MOV m32,reg32 */
1764 if (d2 == 0x89) {
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 0x%.8x,reg32\n", data));
1771 /* the case of MOV reg32,m32 */
1772 if (d2 == 0x8b) {
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, "/MOV reg32,0x%.8x\n", data));
1779 /* the case of LEA reg32,m32 */
1780 if (d2 == 0x8d) {
1781 fixup_found = 1;
1782 FSHOW((stderr,
1783 "abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1784 p, d6, d5, d4, d3, d2, d1, data));
1785 FSHOW((stderr, "/LEA reg32,0x%.8x\n", data));
1791 /* If anything was found, print some information on the code
1792 * object. */
1793 if (fixup_found) {
1794 FSHOW((stderr,
1795 "/compiled code object at %x: header words = %d, code words = %d\n",
1796 code, nheader_words, ncode_words));
1797 FSHOW((stderr,
1798 "/const start = %x, end = %x\n",
1799 constants_start_addr, constants_end_addr));
1800 FSHOW((stderr,
1801 "/code start = %x, end = %x\n",
1802 code_start_addr, code_end_addr));
1805 #endif
1807 #ifdef LISP_FEATURE_X86
1808 void
1809 gencgc_apply_code_fixups(struct code *old_code, struct code *new_code)
1811 sword_t nheader_words, ncode_words, nwords;
1812 os_vm_address_t __attribute__((unused)) constants_start_addr, constants_end_addr;
1813 os_vm_address_t __attribute__((unused)) code_start_addr, code_end_addr;
1814 os_vm_address_t code_addr = (os_vm_address_t)new_code;
1815 os_vm_address_t old_addr = (os_vm_address_t)old_code;
1816 os_vm_size_t displacement = code_addr - old_addr;
1817 lispobj fixups = NIL;
1818 struct vector *fixups_vector;
1820 ncode_words = code_instruction_words(new_code->code_size);
1821 nheader_words = code_header_words(*(lispobj *)new_code);
1822 nwords = ncode_words + nheader_words;
1823 /* FSHOW((stderr,
1824 "/compiled code object at %x: header words = %d, code words = %d\n",
1825 new_code, nheader_words, ncode_words)); */
1826 constants_start_addr = code_addr + 5*N_WORD_BYTES;
1827 constants_end_addr = code_addr + nheader_words*N_WORD_BYTES;
1828 code_start_addr = code_addr + nheader_words*N_WORD_BYTES;
1829 code_end_addr = code_addr + nwords*N_WORD_BYTES;
1831 FSHOW((stderr,
1832 "/const start = %x, end = %x\n",
1833 constants_start_addr,constants_end_addr));
1834 FSHOW((stderr,
1835 "/code start = %x; end = %x\n",
1836 code_start_addr,code_end_addr));
1839 fixups = new_code->fixups;
1840 /* It will be a Lisp vector if valid, or 0 if there are no fixups */
1841 if (fixups == 0 || !is_lisp_pointer(fixups)) {
1842 /* Check for possible errors. */
1843 if (check_code_fixups)
1844 sniff_code_object(new_code, displacement);
1846 return;
1849 fixups_vector = (struct vector *)native_pointer(fixups);
1851 /* Could be pointing to a forwarding pointer. */
1852 /* This is extremely unlikely, because the only referent of the fixups
1853 is usually the code itself; so scavenging the vector won't occur
1854 until after the code object is known to be live. As we're just now
1855 enlivening the code, the fixups shouldn't have been forwarded.
1856 Maybe the vector is on the special binding stack though ... */
1857 if (is_lisp_pointer(fixups) &&
1858 (find_page_index((void*)fixups_vector) != -1) &&
1859 forwarding_pointer_p((lispobj*)fixups_vector)) {
1860 /* If so, then follow it. */
1861 /*SHOW("following pointer to a forwarding pointer");*/
1862 fixups_vector = (struct vector *)
1863 native_pointer(forwarding_pointer_value((lispobj*)fixups_vector));
1866 /*SHOW("got fixups");*/
1868 if (widetag_of(fixups_vector->header) == SIMPLE_ARRAY_WORD_WIDETAG) {
1869 /* Got the fixups for the code block. Now work through the vector,
1870 and apply a fixup at each address. */
1871 sword_t length = fixnum_value(fixups_vector->length);
1872 sword_t i;
1873 for (i = 0; i < length; i++) {
1874 long offset = fixups_vector->data[i];
1875 /* Now check the current value of offset. */
1876 os_vm_address_t old_value = *(os_vm_address_t *)(code_start_addr + offset);
1878 /* If it's within the old_code object then it must be an
1879 * absolute fixup (relative ones are not saved) */
1880 if ((old_value >= old_addr)
1881 && (old_value < (old_addr + nwords*N_WORD_BYTES)))
1882 /* So add the dispacement. */
1883 *(os_vm_address_t *)(code_start_addr + offset) =
1884 old_value + displacement;
1885 else
1886 /* It is outside the old code object so it must be a
1887 * relative fixup (absolute fixups are not saved). So
1888 * subtract the displacement. */
1889 *(os_vm_address_t *)(code_start_addr + offset) =
1890 old_value - displacement;
1892 } else {
1893 /* This used to just print a note to stderr, but a bogus fixup seems to
1894 * indicate real heap corruption, so a hard hailure is in order. */
1895 lose("fixup vector %p has a bad widetag: %d\n",
1896 fixups_vector, widetag_of(fixups_vector->header));
1899 /* Check for possible errors. */
1900 if (check_code_fixups) {
1901 sniff_code_object(new_code,displacement);
1904 #endif
1906 static lispobj
1907 trans_boxed_large(lispobj object)
1909 gc_assert(is_lisp_pointer(object));
1910 return copy_large_object(object,
1911 (HeaderValue(*native_pointer(object)) | 1) + 1);
1915 * weak pointers
1918 /* XX This is a hack adapted from cgc.c. These don't work too
1919 * efficiently with the gencgc as a list of the weak pointers is
1920 * maintained within the objects which causes writes to the pages. A
1921 * limited attempt is made to avoid unnecessary writes, but this needs
1922 * a re-think. */
1923 static sword_t
1924 scav_weak_pointer(lispobj *where, lispobj object)
1926 /* Since we overwrite the 'next' field, we have to make
1927 * sure not to do so for pointers already in the list.
1928 * Instead of searching the list of weak_pointers each
1929 * time, we ensure that next is always NULL when the weak
1930 * pointer isn't in the list, and not NULL otherwise.
1931 * Since we can't use NULL to denote end of list, we
1932 * use a pointer back to the same weak_pointer.
1934 struct weak_pointer * wp = (struct weak_pointer*)where;
1936 if (NULL == wp->next && weak_pointer_breakable_p(wp)) {
1937 wp->next = weak_pointers;
1938 weak_pointers = wp;
1939 if (NULL == wp->next)
1940 wp->next = wp;
1943 /* Do not let GC scavenge the value slot of the weak pointer.
1944 * (That is why it is a weak pointer.) */
1946 return WEAK_POINTER_NWORDS;
1950 lispobj *
1951 search_read_only_space(void *pointer)
1953 lispobj *start = (lispobj *) READ_ONLY_SPACE_START;
1954 lispobj *end = (lispobj *) SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0);
1955 if ((pointer < (void *)start) || (pointer >= (void *)end))
1956 return NULL;
1957 return gc_search_space(start, pointer);
1960 lispobj *
1961 search_static_space(void *pointer)
1963 lispobj *start = (lispobj *)STATIC_SPACE_START;
1964 lispobj *end = (lispobj *)SymbolValue(STATIC_SPACE_FREE_POINTER,0);
1965 if ((pointer < (void *)start) || (pointer >= (void *)end))
1966 return NULL;
1967 return gc_search_space(start, pointer);
1970 /* a faster version for searching the dynamic space. This will work even
1971 * if the object is in a current allocation region. */
1972 lispobj *
1973 search_dynamic_space(void *pointer)
1975 page_index_t page_index = find_page_index(pointer);
1976 lispobj *start;
1978 /* The address may be invalid, so do some checks. */
1979 if ((page_index == -1) || page_free_p(page_index))
1980 return NULL;
1981 start = (lispobj *)page_scan_start(page_index);
1982 return gc_search_space(start, pointer);
1985 // Return the starting address of the object containing 'addr'
1986 // if and only if the object is one which would be evacuated from 'from_space'
1987 // were it allowed to be either discarded as garbage or moved.
1988 // 'addr_page_index' is the page containing 'addr' and must not be -1.
1989 // Return 0 if there is no such object - that is, if addr is past the
1990 // end of the used bytes, or its pages are not in 'from_space' etc.
1991 static lispobj*
1992 conservative_root_p(void *addr, page_index_t addr_page_index)
1994 #ifdef GENCGC_IS_PRECISE
1995 /* If we're in precise gencgc (non-x86oid as of this writing) then
1996 * we are only called on valid object pointers in the first place,
1997 * so we just have to do a bounds-check against the heap, a
1998 * generation check, and the already-pinned check. */
1999 if ((page_table[addr_page_index].gen != from_space)
2000 || (page_table[addr_page_index].dont_move != 0))
2001 return 0;
2002 return (lispobj*)1;
2003 #else
2004 /* quick check 1: Address is quite likely to have been invalid. */
2005 if (page_free_p(addr_page_index)
2006 || (page_bytes_used(addr_page_index) == 0)
2007 || (page_table[addr_page_index].gen != from_space))
2008 return 0;
2009 gc_assert(!(page_table[addr_page_index].allocated&OPEN_REGION_PAGE_FLAG));
2011 /* quick check 2: Check the offset within the page.
2014 if (((uword_t)addr & (GENCGC_CARD_BYTES - 1)) > page_bytes_used(addr_page_index))
2015 return 0;
2017 /* Filter out anything which can't be a pointer to a Lisp object
2018 * (or, as a special case which also requires dont_move, a return
2019 * address referring to something in a CodeObject). This is
2020 * expensive but important, since it vastly reduces the
2021 * probability that random garbage will be bogusly interpreted as
2022 * a pointer which prevents a page from moving. */
2023 lispobj* object_start = search_dynamic_space(addr);
2024 if (!object_start) return 0;
2026 /* If the containing object is a code object and 'addr' points
2027 * anywhere beyond the boxed words,
2028 * presume it to be a valid unboxed return address. */
2029 if (instruction_ptr_p(addr, object_start))
2030 return object_start;
2032 /* Large object pages only contain ONE object, and it will never
2033 * be a CONS. However, arrays and bignums can be allocated larger
2034 * than necessary and then shrunk to fit, leaving what look like
2035 * (0 . 0) CONSes at the end. These appear valid to
2036 * properly_tagged_descriptor_p(), so pick them off here. */
2037 if (((lowtag_of((lispobj)addr) == LIST_POINTER_LOWTAG) &&
2038 page_table[addr_page_index].large_object)
2039 || !properly_tagged_descriptor_p(addr, object_start))
2040 return 0;
2042 return object_start;
2043 #endif
2046 /* Adjust large bignum and vector objects. This will adjust the
2047 * allocated region if the size has shrunk, and move unboxed objects
2048 * into unboxed pages. The pages are not promoted here, and the
2049 * promoted region is not added to the new_regions; this is really
2050 * only designed to be called from preserve_pointer(). Shouldn't fail
2051 * if this is missed, just may delay the moving of objects to unboxed
2052 * pages, and the freeing of pages. */
2053 static void
2054 maybe_adjust_large_object(lispobj *where)
2056 page_index_t first_page;
2057 page_index_t next_page;
2058 sword_t nwords;
2060 uword_t remaining_bytes;
2061 uword_t bytes_freed;
2062 uword_t old_bytes_used;
2064 int boxed;
2066 /* Check whether it's a vector or bignum object. */
2067 lispobj widetag = widetag_of(where[0]);
2068 if (widetag == SIMPLE_VECTOR_WIDETAG)
2069 boxed = BOXED_PAGE_FLAG;
2070 else if (specialized_vector_widetag_p(widetag) || widetag == BIGNUM_WIDETAG)
2071 boxed = UNBOXED_PAGE_FLAG;
2072 else
2073 return;
2075 /* Find its current size. */
2076 nwords = sizetab[widetag](where);
2078 first_page = find_page_index((void *)where);
2079 gc_assert(first_page >= 0);
2081 /* Note: Any page write-protection must be removed, else a later
2082 * scavenge_newspace may incorrectly not scavenge these pages.
2083 * This would not be necessary if they are added to the new areas,
2084 * but lets do it for them all (they'll probably be written
2085 * anyway?). */
2087 gc_assert(page_starts_contiguous_block_p(first_page));
2089 next_page = first_page;
2090 remaining_bytes = nwords*N_WORD_BYTES;
2091 while (remaining_bytes > GENCGC_CARD_BYTES) {
2092 gc_assert(page_table[next_page].gen == from_space);
2093 gc_assert(page_allocated_no_region_p(next_page));
2094 gc_assert(page_table[next_page].large_object);
2095 gc_assert(page_scan_start_offset(next_page) ==
2096 npage_bytes(next_page-first_page));
2097 gc_assert(page_bytes_used(next_page) == GENCGC_CARD_BYTES);
2099 page_table[next_page].allocated = boxed;
2101 /* Shouldn't be write-protected at this stage. Essential that the
2102 * pages aren't. */
2103 gc_assert(!page_table[next_page].write_protected);
2104 remaining_bytes -= GENCGC_CARD_BYTES;
2105 next_page++;
2108 /* Now only one page remains, but the object may have shrunk so
2109 * there may be more unused pages which will be freed. */
2111 /* Object may have shrunk but shouldn't have grown - check. */
2112 gc_assert(page_bytes_used(next_page) >= remaining_bytes);
2114 page_table[next_page].allocated = boxed;
2115 gc_assert(page_table[next_page].allocated ==
2116 page_table[first_page].allocated);
2118 /* Adjust the bytes_used. */
2119 old_bytes_used = page_bytes_used(next_page);
2120 set_page_bytes_used(next_page, remaining_bytes);
2122 bytes_freed = old_bytes_used - remaining_bytes;
2124 /* Free any remaining pages; needs care. */
2125 next_page++;
2126 while ((old_bytes_used == GENCGC_CARD_BYTES) &&
2127 (page_table[next_page].gen == from_space) &&
2128 page_allocated_no_region_p(next_page) &&
2129 page_table[next_page].large_object &&
2130 (page_scan_start_offset(next_page) ==
2131 npage_bytes(next_page - first_page))) {
2132 /* It checks out OK, free the page. We don't need to both zeroing
2133 * pages as this should have been done before shrinking the
2134 * object. These pages shouldn't be write protected as they
2135 * should be zero filled. */
2136 gc_assert(page_table[next_page].write_protected == 0);
2138 old_bytes_used = page_bytes_used(next_page);
2139 page_table[next_page].allocated = FREE_PAGE_FLAG;
2140 set_page_bytes_used(next_page, 0);
2141 bytes_freed += old_bytes_used;
2142 next_page++;
2145 if ((bytes_freed > 0) && gencgc_verbose) {
2146 FSHOW((stderr,
2147 "/maybe_adjust_large_object() freed %d\n",
2148 bytes_freed));
2151 generations[from_space].bytes_allocated -= bytes_freed;
2152 bytes_allocated -= bytes_freed;
2154 return;
2158 * Why is this restricted to protected objects only?
2159 * Because the rest of the page has been scavenged already,
2160 * and since that leaves forwarding pointers in the unprotected
2161 * areas you cannot scavenge it again until those are gone.
2163 static void
2164 scavenge_pinned_range(void* page_base, int start, int count)
2166 // 'start' and 'count' are expressed in units of dwords
2167 lispobj *where = (lispobj*)page_base + 2*start;
2168 heap_scavenge(where, where + 2*count);
2171 static void
2172 scavenge_pinned_ranges()
2174 page_index_t page;
2175 for (page = 0; page < last_free_page; page++) {
2176 in_use_marker_t* bitmap = pinned_dwords(page);
2177 if (bitmap)
2178 bitmap_scan(bitmap,
2179 GENCGC_CARD_BYTES / (2*N_WORD_BYTES) / N_WORD_BITS,
2180 0, scavenge_pinned_range, page_address(page));
2184 static void wipe_range(void* page_base, int start, int count)
2186 bzero((lispobj*)page_base + 2*start, count*2*N_WORD_BYTES);
2189 static void
2190 wipe_nonpinned_words()
2192 page_index_t i;
2193 in_use_marker_t* bitmap;
2195 for (i = 0; i < last_free_page; i++) {
2196 if (page_table[i].dont_move && (bitmap = pinned_dwords(i)) != 0) {
2197 bitmap_scan(bitmap,
2198 GENCGC_CARD_BYTES / (2*N_WORD_BYTES) / N_WORD_BITS,
2199 BIT_SCAN_INVERT | BIT_SCAN_CLEAR,
2200 wipe_range, page_address(i));
2201 page_table[i].has_pin_map = 0;
2202 // move the page to newspace
2203 int used = page_bytes_used(i);
2204 generations[new_space].bytes_allocated += used;
2205 generations[page_table[i].gen].bytes_allocated -= used;
2206 page_table[i].gen = new_space;
2209 #ifndef LISP_FEATURE_WIN32
2210 madvise(page_table_pinned_dwords, pins_map_size_in_bytes, MADV_DONTNEED);
2211 #endif
2214 static void __attribute__((unused))
2215 pin_words(page_index_t pageindex, lispobj *mark_which_pointer)
2217 gc_assert(mark_which_pointer);
2218 if (!page_table[pageindex].has_pin_map) {
2219 page_table[pageindex].has_pin_map = 1;
2220 #ifdef DEBUG
2222 int i;
2223 in_use_marker_t* map = pinned_dwords(pageindex);
2224 for (i=0; i<n_dwords_in_card/N_WORD_BITS; ++i)
2225 gc_assert(map[i] == 0);
2227 #endif
2229 lispobj *page_base = page_address(pageindex);
2230 unsigned int begin_dword_index = (mark_which_pointer - page_base) / 2;
2231 in_use_marker_t *bitmap = pinned_dwords(pageindex);
2232 if (bitmap[begin_dword_index/N_WORD_BITS]
2233 & ((uword_t)1 << (begin_dword_index % N_WORD_BITS)))
2234 return; // already seen this object
2236 lispobj header = *mark_which_pointer;
2237 int size = 2;
2238 // Don't bother calling a sizing function for cons cells.
2239 if (!is_cons_half(header))
2240 size = (sizetab[widetag_of(header)])(mark_which_pointer);
2241 gc_assert(size % 2 == 0);
2242 unsigned int end_dword_index = begin_dword_index + size / 2;
2243 unsigned int index;
2244 for (index = begin_dword_index; index < end_dword_index; index++)
2245 bitmap[index/N_WORD_BITS] |= (uword_t)1 << (index % N_WORD_BITS);
2248 /* Take a possible pointer to a Lisp object and mark its page in the
2249 * page_table so that it will not be relocated during a GC.
2251 * This involves locating the page it points to, then backing up to
2252 * the start of its region, then marking all pages dont_move from there
2253 * up to the first page that's not full or has a different generation
2255 * It is assumed that all the page static flags have been cleared at
2256 * the start of a GC.
2258 * It is also assumed that the current gc_alloc() region has been
2259 * flushed and the tables updated. */
2261 // TODO: there's probably a way to be a little more efficient here.
2262 // As things are, we start by finding the object that encloses 'addr',
2263 // then we see if 'addr' was a "valid" Lisp pointer to that object
2264 // - meaning we expect the correct lowtag on the pointer - except
2265 // that for code objects we don't require a correct lowtag
2266 // and we allow a pointer to anywhere in the object.
2268 // It should be possible to avoid calling search_dynamic_space
2269 // more of the time. First, check if the page pointed to might hold code.
2270 // If it does, then we continue regardless of the pointer's lowtag
2271 // (because of the special allowance). If the page definitely does *not*
2272 // hold code, then we require up front that the lowtake make sense,
2273 // by doing the same checks that are in properly_tagged_descriptor_p.
2275 // Problem: when code is allocated from a per-thread region,
2276 // does it ensure that the occupied pages are flagged as having code?
2278 static void
2279 preserve_pointer(void *addr)
2281 #ifdef LISP_FEATURE_IMMOBILE_SPACE
2282 /* Immobile space MUST be lower than dynamic space,
2283 or else this test needs to be revised */
2284 if (addr < (void*)IMMOBILE_SPACE_END) {
2285 extern void immobile_space_preserve_pointer(void*);
2286 immobile_space_preserve_pointer(addr);
2287 return;
2289 #endif
2290 page_index_t addr_page_index = find_page_index(addr);
2291 lispobj *object_start;
2293 if (addr_page_index == -1
2294 || (object_start = conservative_root_p(addr, addr_page_index)) == 0)
2295 return;
2297 /* (Now that we know that addr_page_index is in range, it's
2298 * safe to index into page_table[] with it.) */
2299 unsigned int region_allocation = page_table[addr_page_index].allocated;
2301 /* Find the beginning of the region. Note that there may be
2302 * objects in the region preceding the one that we were passed a
2303 * pointer to: if this is the case, we will write-protect all the
2304 * previous objects' pages too. */
2306 #if 0
2307 /* I think this'd work just as well, but without the assertions.
2308 * -dan 2004.01.01 */
2309 page_index_t first_page = find_page_index(page_scan_start(addr_page_index))
2310 #else
2311 page_index_t first_page = addr_page_index;
2312 while (!page_starts_contiguous_block_p(first_page)) {
2313 --first_page;
2314 /* Do some checks. */
2315 gc_assert(page_bytes_used(first_page) == GENCGC_CARD_BYTES);
2316 gc_assert(page_table[first_page].gen == from_space);
2317 gc_assert(page_table[first_page].allocated == region_allocation);
2319 #endif
2321 /* Adjust any large objects before promotion as they won't be
2322 * copied after promotion. */
2323 if (page_table[first_page].large_object) {
2324 maybe_adjust_large_object(page_address(first_page));
2325 /* It may have moved to unboxed pages. */
2326 region_allocation = page_table[first_page].allocated;
2329 /* Now work forward until the end of this contiguous area is found,
2330 * marking all pages as dont_move. */
2331 page_index_t i;
2332 for (i = first_page; ;i++) {
2333 gc_assert(page_table[i].allocated == region_allocation);
2335 /* Mark the page static. */
2336 page_table[i].dont_move = 1;
2338 /* It is essential that the pages are not write protected as
2339 * they may have pointers into the old-space which need
2340 * scavenging. They shouldn't be write protected at this
2341 * stage. */
2342 gc_assert(!page_table[i].write_protected);
2344 /* Check whether this is the last page in this contiguous block.. */
2345 if (page_ends_contiguous_block_p(i, from_space))
2346 break;
2349 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
2350 /* Do not do this for multi-page objects. Those pages do not need
2351 * object wipeout anyway.
2353 if (do_wipe_p && i == first_page) // single-page object
2354 pin_words(first_page, object_start);
2355 #endif
2357 /* Check that the page is now static. */
2358 gc_assert(page_table[addr_page_index].dont_move != 0);
2361 /* If the given page is not write-protected, then scan it for pointers
2362 * to younger generations or the top temp. generation, if no
2363 * suspicious pointers are found then the page is write-protected.
2365 * Care is taken to check for pointers to the current gc_alloc()
2366 * region if it is a younger generation or the temp. generation. This
2367 * frees the caller from doing a gc_alloc_update_page_tables(). Actually
2368 * the gc_alloc_generation does not need to be checked as this is only
2369 * called from scavenge_generation() when the gc_alloc generation is
2370 * younger, so it just checks if there is a pointer to the current
2371 * region.
2373 * We return 1 if the page was write-protected, else 0. */
2374 static int
2375 update_page_write_prot(page_index_t page)
2377 generation_index_t gen = page_table[page].gen;
2378 sword_t j;
2379 int wp_it = 1;
2380 void **page_addr = (void **)page_address(page);
2381 sword_t num_words = page_bytes_used(page) / N_WORD_BYTES;
2383 /* Shouldn't be a free page. */
2384 gc_assert(page_allocated_p(page));
2385 gc_assert(page_bytes_used(page) != 0);
2387 /* Skip if it's already write-protected, pinned, or unboxed */
2388 if (page_table[page].write_protected
2389 /* FIXME: What's the reason for not write-protecting pinned pages? */
2390 || page_table[page].dont_move
2391 || page_unboxed_p(page))
2392 return (0);
2394 /* Scan the page for pointers to younger generations or the
2395 * top temp. generation. */
2397 /* This is conservative: any word satisfying is_lisp_pointer() is
2398 * assumed to be a pointer. To do otherwise would require a family
2399 * of scavenge-like functions. */
2400 for (j = 0; j < num_words; j++) {
2401 void *ptr = *(page_addr+j);
2402 page_index_t index;
2403 lispobj __attribute__((unused)) header;
2405 if (!is_lisp_pointer((lispobj)ptr))
2406 continue;
2407 /* Check that it's in the dynamic space */
2408 if ((index = find_page_index(ptr)) != -1) {
2409 if (/* Does it point to a younger or the temp. generation? */
2410 (page_allocated_p(index)
2411 && (page_bytes_used(index) != 0)
2412 && ((page_table[index].gen < gen)
2413 || (page_table[index].gen == SCRATCH_GENERATION)))
2415 /* Or does it point within a current gc_alloc() region? */
2416 || ((boxed_region.start_addr <= ptr)
2417 && (ptr <= boxed_region.free_pointer))
2418 || ((unboxed_region.start_addr <= ptr)
2419 && (ptr <= unboxed_region.free_pointer))) {
2420 wp_it = 0;
2421 break;
2424 #ifdef LISP_FEATURE_IMMOBILE_SPACE
2425 else if ((index = find_immobile_page_index(ptr)) >= 0 &&
2426 other_immediate_lowtag_p(header = *native_pointer((lispobj)ptr))) {
2427 // This is *possibly* a pointer to an object in immobile space,
2428 // given that above two conditions were satisfied.
2429 // But unlike in the dynamic space case, we need to read a byte
2430 // from the object to determine its generation, which requires care.
2431 // Consider an unboxed word that looks like a pointer to a word that
2432 // looks like fun-header-widetag. We can't naively back up to the
2433 // underlying code object since the alleged header might not be one.
2434 int obj_gen = gen; // Make comparison fail if we fall through
2435 if (lowtag_of((lispobj)ptr) != FUN_POINTER_LOWTAG) {
2436 obj_gen = __immobile_obj_generation(native_pointer((lispobj)ptr));
2437 } else if (widetag_of(header) == SIMPLE_FUN_HEADER_WIDETAG) {
2438 struct code* code =
2439 code_obj_from_simple_fun((struct simple_fun *)
2440 ((lispobj)ptr - FUN_POINTER_LOWTAG));
2441 // This is a heuristic, since we're not actually looking for
2442 // an object boundary. Precise scanning of 'page' would obviate
2443 // the guard conditions here.
2444 if ((lispobj)code >= IMMOBILE_VARYOBJ_SUBSPACE_START
2445 && widetag_of(code->header) == CODE_HEADER_WIDETAG)
2446 obj_gen = __immobile_obj_generation((lispobj*)code);
2448 // A bogus generation number implies a not-really-pointer,
2449 // but it won't cause misbehavior.
2450 if (obj_gen < gen || obj_gen == SCRATCH_GENERATION) {
2451 wp_it = 0;
2452 break;
2455 #endif
2458 if (wp_it == 1) {
2459 /* Write-protect the page. */
2460 /*FSHOW((stderr, "/write-protecting page %d gen %d\n", page, gen));*/
2462 os_protect((void *)page_addr,
2463 GENCGC_CARD_BYTES,
2464 OS_VM_PROT_READ|OS_VM_PROT_EXECUTE);
2466 /* Note the page as protected in the page tables. */
2467 page_table[page].write_protected = 1;
2470 return (wp_it);
2473 /* Scavenge all generations from FROM to TO, inclusive, except for
2474 * new_space which needs special handling, as new objects may be
2475 * added which are not checked here - use scavenge_newspace generation.
2477 * Write-protected pages should not have any pointers to the
2478 * from_space so do need scavenging; thus write-protected pages are
2479 * not always scavenged. There is some code to check that these pages
2480 * are not written; but to check fully the write-protected pages need
2481 * to be scavenged by disabling the code to skip them.
2483 * Under the current scheme when a generation is GCed the younger
2484 * generations will be empty. So, when a generation is being GCed it
2485 * is only necessary to scavenge the older generations for pointers
2486 * not the younger. So a page that does not have pointers to younger
2487 * generations does not need to be scavenged.
2489 * The write-protection can be used to note pages that don't have
2490 * pointers to younger pages. But pages can be written without having
2491 * pointers to younger generations. After the pages are scavenged here
2492 * they can be scanned for pointers to younger generations and if
2493 * there are none the page can be write-protected.
2495 * One complication is when the newspace is the top temp. generation.
2497 * Enabling SC_GEN_CK scavenges the write-protected pages and checks
2498 * that none were written, which they shouldn't be as they should have
2499 * no pointers to younger generations. This breaks down for weak
2500 * pointers as the objects contain a link to the next and are written
2501 * if a weak pointer is scavenged. Still it's a useful check. */
2502 static void
2503 scavenge_generations(generation_index_t from, generation_index_t to)
2505 page_index_t i;
2506 page_index_t num_wp = 0;
2508 #define SC_GEN_CK 0
2509 #if SC_GEN_CK
2510 /* Clear the write_protected_cleared flags on all pages. */
2511 for (i = 0; i < page_table_pages; i++)
2512 page_table[i].write_protected_cleared = 0;
2513 #endif
2515 for (i = 0; i < last_free_page; i++) {
2516 generation_index_t generation = page_table[i].gen;
2517 if (page_boxed_p(i)
2518 && (page_bytes_used(i) != 0)
2519 && (generation != new_space)
2520 && (generation >= from)
2521 && (generation <= to)) {
2522 page_index_t last_page,j;
2523 int write_protected=1;
2525 /* This should be the start of a region */
2526 gc_assert(page_starts_contiguous_block_p(i));
2528 /* Now work forward until the end of the region */
2529 for (last_page = i; ; last_page++) {
2530 write_protected =
2531 write_protected && page_table[last_page].write_protected;
2532 if (page_ends_contiguous_block_p(last_page, generation))
2533 break;
2535 if (!write_protected) {
2536 heap_scavenge(page_address(i),
2537 (lispobj*)((char*)page_address(last_page)
2538 + page_bytes_used(last_page)));
2540 /* Now scan the pages and write protect those that
2541 * don't have pointers to younger generations. */
2542 if (enable_page_protection) {
2543 for (j = i; j <= last_page; j++) {
2544 num_wp += update_page_write_prot(j);
2547 if ((gencgc_verbose > 1) && (num_wp != 0)) {
2548 FSHOW((stderr,
2549 "/write protected %d pages within generation %d\n",
2550 num_wp, generation));
2553 i = last_page;
2557 #if SC_GEN_CK
2558 /* Check that none of the write_protected pages in this generation
2559 * have been written to. */
2560 for (i = 0; i < page_table_pages; i++) {
2561 if (page_allocated_p(i)
2562 && (page_bytes_used(i) != 0)
2563 && (page_table[i].gen == generation)
2564 && (page_table[i].write_protected_cleared != 0)) {
2565 FSHOW((stderr, "/scavenge_generation() %d\n", generation));
2566 FSHOW((stderr,
2567 "/page bytes_used=%d scan_start_offset=%lu dont_move=%d\n",
2568 page_bytes_used(i),
2569 scan_start_offset(page_table[i]),
2570 page_table[i].dont_move));
2571 lose("write to protected page %d in scavenge_generation()\n", i);
2574 #endif
2578 /* Scavenge a newspace generation. As it is scavenged new objects may
2579 * be allocated to it; these will also need to be scavenged. This
2580 * repeats until there are no more objects unscavenged in the
2581 * newspace generation.
2583 * To help improve the efficiency, areas written are recorded by
2584 * gc_alloc() and only these scavenged. Sometimes a little more will be
2585 * scavenged, but this causes no harm. An easy check is done that the
2586 * scavenged bytes equals the number allocated in the previous
2587 * scavenge.
2589 * Write-protected pages are not scanned except if they are marked
2590 * dont_move in which case they may have been promoted and still have
2591 * pointers to the from space.
2593 * Write-protected pages could potentially be written by alloc however
2594 * to avoid having to handle re-scavenging of write-protected pages
2595 * gc_alloc() does not write to write-protected pages.
2597 * New areas of objects allocated are recorded alternatively in the two
2598 * new_areas arrays below. */
2599 static struct new_area new_areas_1[NUM_NEW_AREAS];
2600 static struct new_area new_areas_2[NUM_NEW_AREAS];
2602 #ifdef LISP_FEATURE_IMMOBILE_SPACE
2603 extern unsigned int immobile_scav_queue_count;
2604 extern void
2605 gc_init_immobile(),
2606 update_immobile_nursery_bits(),
2607 scavenge_immobile_roots(generation_index_t,generation_index_t),
2608 scavenge_immobile_newspace(),
2609 sweep_immobile_space(int raise),
2610 write_protect_immobile_space();
2611 #else
2612 #define immobile_scav_queue_count 0
2613 #endif
2615 /* Do one full scan of the new space generation. This is not enough to
2616 * complete the job as new objects may be added to the generation in
2617 * the process which are not scavenged. */
2618 static void
2619 scavenge_newspace_generation_one_scan(generation_index_t generation)
2621 page_index_t i;
2623 FSHOW((stderr,
2624 "/starting one full scan of newspace generation %d\n",
2625 generation));
2626 for (i = 0; i < last_free_page; i++) {
2627 /* Note that this skips over open regions when it encounters them. */
2628 if (page_boxed_p(i)
2629 && (page_bytes_used(i) != 0)
2630 && (page_table[i].gen == generation)
2631 && ((page_table[i].write_protected == 0)
2632 /* (This may be redundant as write_protected is now
2633 * cleared before promotion.) */
2634 || (page_table[i].dont_move == 1))) {
2635 page_index_t last_page;
2636 int all_wp=1;
2638 /* The scavenge will start at the scan_start_offset of
2639 * page i.
2641 * We need to find the full extent of this contiguous
2642 * block in case objects span pages.
2644 * Now work forward until the end of this contiguous area
2645 * is found. A small area is preferred as there is a
2646 * better chance of its pages being write-protected. */
2647 for (last_page = i; ;last_page++) {
2648 /* If all pages are write-protected and movable,
2649 * then no need to scavenge */
2650 all_wp=all_wp && page_table[last_page].write_protected &&
2651 !page_table[last_page].dont_move;
2653 /* Check whether this is the last page in this
2654 * contiguous block */
2655 if (page_ends_contiguous_block_p(last_page, generation))
2656 break;
2659 /* Do a limited check for write-protected pages. */
2660 if (!all_wp) {
2661 new_areas_ignore_page = last_page;
2662 heap_scavenge(page_scan_start(i),
2663 (lispobj*)((char*)page_address(last_page)
2664 + page_bytes_used(last_page)));
2666 i = last_page;
2669 FSHOW((stderr,
2670 "/done with one full scan of newspace generation %d\n",
2671 generation));
2674 /* Do a complete scavenge of the newspace generation. */
2675 static void
2676 scavenge_newspace_generation(generation_index_t generation)
2678 size_t i;
2680 /* the new_areas array currently being written to by gc_alloc() */
2681 struct new_area (*current_new_areas)[] = &new_areas_1;
2682 size_t current_new_areas_index;
2684 /* the new_areas created by the previous scavenge cycle */
2685 struct new_area (*previous_new_areas)[] = NULL;
2686 size_t previous_new_areas_index;
2688 /* Flush the current regions updating the tables. */
2689 gc_alloc_update_all_page_tables(0);
2691 /* Turn on the recording of new areas by gc_alloc(). */
2692 new_areas = current_new_areas;
2693 new_areas_index = 0;
2695 /* Don't need to record new areas that get scavenged anyway during
2696 * scavenge_newspace_generation_one_scan. */
2697 record_new_objects = 1;
2699 /* Start with a full scavenge. */
2700 scavenge_newspace_generation_one_scan(generation);
2702 /* Record all new areas now. */
2703 record_new_objects = 2;
2705 /* Give a chance to weak hash tables to make other objects live.
2706 * FIXME: The algorithm implemented here for weak hash table gcing
2707 * is O(W^2+N) as Bruno Haible warns in
2708 * http://www.haible.de/bruno/papers/cs/weak/WeakDatastructures-writeup.html
2709 * see "Implementation 2". */
2710 scav_weak_hash_tables();
2712 /* Flush the current regions updating the tables. */
2713 gc_alloc_update_all_page_tables(0);
2715 /* Grab new_areas_index. */
2716 current_new_areas_index = new_areas_index;
2718 /*FSHOW((stderr,
2719 "The first scan is finished; current_new_areas_index=%d.\n",
2720 current_new_areas_index));*/
2722 while (current_new_areas_index > 0 || immobile_scav_queue_count) {
2723 /* Move the current to the previous new areas */
2724 previous_new_areas = current_new_areas;
2725 previous_new_areas_index = current_new_areas_index;
2727 /* Scavenge all the areas in previous new areas. Any new areas
2728 * allocated are saved in current_new_areas. */
2730 /* Allocate an array for current_new_areas; alternating between
2731 * new_areas_1 and 2 */
2732 if (previous_new_areas == &new_areas_1)
2733 current_new_areas = &new_areas_2;
2734 else
2735 current_new_areas = &new_areas_1;
2737 /* Set up for gc_alloc(). */
2738 new_areas = current_new_areas;
2739 new_areas_index = 0;
2741 #ifdef LISP_FEATURE_IMMOBILE_SPACE
2742 scavenge_immobile_newspace();
2743 #endif
2744 /* Check whether previous_new_areas had overflowed. */
2745 if (previous_new_areas_index >= NUM_NEW_AREAS) {
2747 /* New areas of objects allocated have been lost so need to do a
2748 * full scan to be sure! If this becomes a problem try
2749 * increasing NUM_NEW_AREAS. */
2750 if (gencgc_verbose) {
2751 SHOW("new_areas overflow, doing full scavenge");
2754 /* Don't need to record new areas that get scavenged
2755 * anyway during scavenge_newspace_generation_one_scan. */
2756 record_new_objects = 1;
2758 scavenge_newspace_generation_one_scan(generation);
2760 /* Record all new areas now. */
2761 record_new_objects = 2;
2763 scav_weak_hash_tables();
2765 /* Flush the current regions updating the tables. */
2766 gc_alloc_update_all_page_tables(0);
2768 } else {
2770 /* Work through previous_new_areas. */
2771 for (i = 0; i < previous_new_areas_index; i++) {
2772 page_index_t page = (*previous_new_areas)[i].page;
2773 size_t offset = (*previous_new_areas)[i].offset;
2774 size_t size = (*previous_new_areas)[i].size;
2775 gc_assert(size % N_WORD_BYTES == 0);
2776 lispobj *start = (lispobj*)((char*)page_address(page) + offset);
2777 heap_scavenge(start, (lispobj*)((char*)start + size));
2780 scav_weak_hash_tables();
2782 /* Flush the current regions updating the tables. */
2783 gc_alloc_update_all_page_tables(0);
2786 current_new_areas_index = new_areas_index;
2788 /*FSHOW((stderr,
2789 "The re-scan has finished; current_new_areas_index=%d.\n",
2790 current_new_areas_index));*/
2793 /* Turn off recording of areas allocated by gc_alloc(). */
2794 record_new_objects = 0;
2796 #if SC_NS_GEN_CK
2798 page_index_t i;
2799 /* Check that none of the write_protected pages in this generation
2800 * have been written to. */
2801 for (i = 0; i < page_table_pages; i++) {
2802 if (page_allocated_p(i)
2803 && (page_bytes_used(i) != 0)
2804 && (page_table[i].gen == generation)
2805 && (page_table[i].write_protected_cleared != 0)
2806 && (page_table[i].dont_move == 0)) {
2807 lose("write protected page %d written to in scavenge_newspace_generation\ngeneration=%d dont_move=%d\n",
2808 i, generation, page_table[i].dont_move);
2812 #endif
2815 /* Un-write-protect all the pages in from_space. This is done at the
2816 * start of a GC else there may be many page faults while scavenging
2817 * the newspace (I've seen drive the system time to 99%). These pages
2818 * would need to be unprotected anyway before unmapping in
2819 * free_oldspace; not sure what effect this has on paging.. */
2820 static void
2821 unprotect_oldspace(void)
2823 page_index_t i;
2824 void *region_addr = 0;
2825 void *page_addr = 0;
2826 uword_t region_bytes = 0;
2828 for (i = 0; i < last_free_page; i++) {
2829 if (page_allocated_p(i)
2830 && (page_bytes_used(i) != 0)
2831 && (page_table[i].gen == from_space)) {
2833 /* Remove any write-protection. We should be able to rely
2834 * on the write-protect flag to avoid redundant calls. */
2835 if (page_table[i].write_protected) {
2836 page_table[i].write_protected = 0;
2837 page_addr = page_address(i);
2838 if (!region_addr) {
2839 /* First region. */
2840 region_addr = page_addr;
2841 region_bytes = GENCGC_CARD_BYTES;
2842 } else if (region_addr + region_bytes == page_addr) {
2843 /* Region continue. */
2844 region_bytes += GENCGC_CARD_BYTES;
2845 } else {
2846 /* Unprotect previous region. */
2847 os_protect(region_addr, region_bytes, OS_VM_PROT_ALL);
2848 /* First page in new region. */
2849 region_addr = page_addr;
2850 region_bytes = GENCGC_CARD_BYTES;
2855 if (region_addr) {
2856 /* Unprotect last region. */
2857 os_protect(region_addr, region_bytes, OS_VM_PROT_ALL);
2861 /* Work through all the pages and free any in from_space. This
2862 * assumes that all objects have been copied or promoted to an older
2863 * generation. Bytes_allocated and the generation bytes_allocated
2864 * counter are updated. The number of bytes freed is returned. */
2865 static uword_t
2866 free_oldspace(void)
2868 uword_t bytes_freed = 0;
2869 page_index_t first_page, last_page;
2871 first_page = 0;
2873 do {
2874 /* Find a first page for the next region of pages. */
2875 while ((first_page < last_free_page)
2876 && (page_free_p(first_page)
2877 || (page_bytes_used(first_page) == 0)
2878 || (page_table[first_page].gen != from_space)))
2879 first_page++;
2881 if (first_page >= last_free_page)
2882 break;
2884 /* Find the last page of this region. */
2885 last_page = first_page;
2887 do {
2888 /* Free the page. */
2889 bytes_freed += page_bytes_used(last_page);
2890 generations[page_table[last_page].gen].bytes_allocated -=
2891 page_bytes_used(last_page);
2892 page_table[last_page].allocated = FREE_PAGE_FLAG;
2893 set_page_bytes_used(last_page, 0);
2894 /* Should already be unprotected by unprotect_oldspace(). */
2895 gc_assert(!page_table[last_page].write_protected);
2896 last_page++;
2898 while ((last_page < last_free_page)
2899 && page_allocated_p(last_page)
2900 && (page_bytes_used(last_page) != 0)
2901 && (page_table[last_page].gen == from_space));
2903 #ifdef READ_PROTECT_FREE_PAGES
2904 os_protect(page_address(first_page),
2905 npage_bytes(last_page-first_page),
2906 OS_VM_PROT_NONE);
2907 #endif
2908 first_page = last_page;
2909 } while (first_page < last_free_page);
2911 bytes_allocated -= bytes_freed;
2912 return bytes_freed;
2915 #if 0
2916 /* Print some information about a pointer at the given address. */
2917 static void
2918 print_ptr(lispobj *addr)
2920 /* If addr is in the dynamic space then out the page information. */
2921 page_index_t pi1 = find_page_index((void*)addr);
2923 if (pi1 != -1)
2924 fprintf(stderr," %p: page %d alloc %d gen %d bytes_used %d offset %lu dont_move %d\n",
2925 addr,
2926 pi1,
2927 page_table[pi1].allocated,
2928 page_table[pi1].gen,
2929 page_bytes_used(pi1),
2930 scan_start_offset(page_table[pi1]),
2931 page_table[pi1].dont_move);
2932 fprintf(stderr," %x %x %x %x (%x) %x %x %x %x\n",
2933 *(addr-4),
2934 *(addr-3),
2935 *(addr-2),
2936 *(addr-1),
2937 *(addr-0),
2938 *(addr+1),
2939 *(addr+2),
2940 *(addr+3),
2941 *(addr+4));
2943 #endif
2945 static int
2946 is_in_stack_space(lispobj ptr)
2948 /* For space verification: Pointers can be valid if they point
2949 * to a thread stack space. This would be faster if the thread
2950 * structures had page-table entries as if they were part of
2951 * the heap space. */
2952 struct thread *th;
2953 for_each_thread(th) {
2954 if ((th->control_stack_start <= (lispobj *)ptr) &&
2955 (th->control_stack_end >= (lispobj *)ptr)) {
2956 return 1;
2959 return 0;
2962 // NOTE: This function can produces false failure indications,
2963 // usually related to dynamic space pointing to the stack of a
2964 // dead thread, but there may be other reasons as well.
2965 static void
2966 verify_space(lispobj *start, size_t words)
2968 extern int valid_lisp_pointer_p(lispobj);
2969 int is_in_dynamic_space = (find_page_index((void*)start) != -1);
2970 int is_in_readonly_space =
2971 (READ_ONLY_SPACE_START <= (uword_t)start &&
2972 (uword_t)start < SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0));
2973 #ifdef LISP_FEATURE_IMMOBILE_SPACE
2974 int is_in_immobile_space =
2975 (IMMOBILE_SPACE_START <= (uword_t)start &&
2976 (uword_t)start < SymbolValue(IMMOBILE_SPACE_FREE_POINTER,0));
2977 #endif
2979 while (words > 0) {
2980 size_t count = 1;
2981 lispobj thing = *start;
2982 lispobj __attribute__((unused)) pointee;
2984 if (is_lisp_pointer(thing)) {
2985 page_index_t page_index = find_page_index((void*)thing);
2986 sword_t to_readonly_space =
2987 (READ_ONLY_SPACE_START <= thing &&
2988 thing < SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0));
2989 sword_t to_static_space =
2990 (STATIC_SPACE_START <= thing &&
2991 thing < SymbolValue(STATIC_SPACE_FREE_POINTER,0));
2992 #ifdef LISP_FEATURE_IMMOBILE_SPACE
2993 sword_t to_immobile_space =
2994 (IMMOBILE_SPACE_START <= thing &&
2995 thing < SymbolValue(IMMOBILE_FIXEDOBJ_FREE_POINTER,0)) ||
2996 (IMMOBILE_VARYOBJ_SUBSPACE_START <= thing &&
2997 thing < SymbolValue(IMMOBILE_SPACE_FREE_POINTER,0));
2998 #endif
3000 /* Does it point to the dynamic space? */
3001 if (page_index != -1) {
3002 /* If it's within the dynamic space it should point to a used page. */
3003 if (!page_allocated_p(page_index))
3004 lose ("Ptr %p @ %p sees free page.\n", thing, start);
3005 if ((thing & (GENCGC_CARD_BYTES-1)) >= page_bytes_used(page_index))
3006 lose ("Ptr %p @ %p sees unallocated space.\n", thing, start);
3007 /* Check that it doesn't point to a forwarding pointer! */
3008 if (*native_pointer(thing) == 0x01) {
3009 lose("Ptr %p @ %p sees forwarding ptr.\n", thing, start);
3011 /* Check that its not in the RO space as it would then be a
3012 * pointer from the RO to the dynamic space. */
3013 if (is_in_readonly_space) {
3014 lose("ptr to dynamic space %p from RO space %x\n",
3015 thing, start);
3017 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3018 // verify all immobile space -> dynamic space pointers
3019 if (is_in_immobile_space && !valid_lisp_pointer_p(thing)) {
3020 lose("Ptr %p @ %p sees junk.\n", thing, start);
3022 #endif
3023 /* Does it point to a plausible object? This check slows
3024 * it down a lot (so it's commented out).
3026 * "a lot" is serious: it ate 50 minutes cpu time on
3027 * my duron 950 before I came back from lunch and
3028 * killed it.
3030 * FIXME: Add a variable to enable this
3031 * dynamically. */
3033 if (!valid_lisp_pointer_p((lispobj *)thing) {
3034 lose("ptr %p to invalid object %p\n", thing, start);
3037 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3038 } else if (to_immobile_space) {
3039 // the object pointed to must not have been discarded as garbage
3040 if (!other_immediate_lowtag_p(*native_pointer(thing))
3041 || immobile_filler_p(native_pointer(thing)))
3042 lose("Ptr %p @ %p sees trashed object.\n", (void*)thing, start);
3043 // verify all pointers to immobile space
3044 if (!valid_lisp_pointer_p(thing))
3045 lose("Ptr %p @ %p sees junk.\n", thing, start);
3046 #endif
3047 } else {
3048 extern char __attribute__((unused)) funcallable_instance_tramp;
3049 /* Verify that it points to another valid space. */
3050 if (!to_readonly_space && !to_static_space
3051 && !is_in_stack_space(thing)) {
3052 lose("Ptr %p @ %p sees junk.\n", thing, start);
3055 } else {
3056 if (!(fixnump(thing))) {
3057 /* skip fixnums */
3058 switch(widetag_of(*start)) {
3060 /* boxed objects */
3061 case SIMPLE_VECTOR_WIDETAG:
3062 case RATIO_WIDETAG:
3063 case COMPLEX_WIDETAG:
3064 case SIMPLE_ARRAY_WIDETAG:
3065 case COMPLEX_BASE_STRING_WIDETAG:
3066 #ifdef COMPLEX_CHARACTER_STRING_WIDETAG
3067 case COMPLEX_CHARACTER_STRING_WIDETAG:
3068 #endif
3069 case COMPLEX_VECTOR_NIL_WIDETAG:
3070 case COMPLEX_BIT_VECTOR_WIDETAG:
3071 case COMPLEX_VECTOR_WIDETAG:
3072 case COMPLEX_ARRAY_WIDETAG:
3073 case CLOSURE_HEADER_WIDETAG:
3074 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
3075 case VALUE_CELL_HEADER_WIDETAG:
3076 case SYMBOL_HEADER_WIDETAG:
3077 case CHARACTER_WIDETAG:
3078 #if N_WORD_BITS == 64
3079 case SINGLE_FLOAT_WIDETAG:
3080 #endif
3081 case UNBOUND_MARKER_WIDETAG:
3082 break;
3083 case FDEFN_WIDETAG:
3084 #ifdef LISP_FEATURE_IMMOBILE_CODE
3085 verify_space(start + 1, 2);
3086 pointee = fdefn_raw_referent((struct fdefn*)start);
3087 verify_space(&pointee, 1);
3088 count = 4;
3089 #endif
3090 break;
3092 case INSTANCE_HEADER_WIDETAG:
3093 if (instance_layout(start)) {
3094 lispobj bitmap =
3095 ((struct layout*)
3096 native_pointer(instance_layout(start)))->bitmap;
3097 sword_t nslots = instance_length(thing) | 1;
3098 instance_scan(verify_space, start+1, nslots, bitmap);
3099 count = 1 + nslots;
3101 break;
3102 case CODE_HEADER_WIDETAG:
3104 /* Check that it's not in the dynamic space.
3105 * FIXME: Isn't is supposed to be OK for code
3106 * objects to be in the dynamic space these days? */
3107 /* It is for byte compiled code, but there's
3108 * no byte compilation in SBCL anymore. */
3109 if (is_in_dynamic_space
3110 /* Only when enabled */
3111 && verify_dynamic_code_check) {
3112 FSHOW((stderr,
3113 "/code object at %p in the dynamic space\n",
3114 start));
3117 struct code *code = (struct code *) start;
3118 sword_t nheader_words = code_header_words(code->header);
3119 /* Scavenge the boxed section of the code data block */
3120 verify_space(start + 1, nheader_words - 1);
3122 /* Scavenge the boxed section of each function
3123 * object in the code data block. */
3124 for_each_simple_fun(i, fheaderp, code, 1, {
3125 verify_space(SIMPLE_FUN_SCAV_START(fheaderp),
3126 SIMPLE_FUN_SCAV_NWORDS(fheaderp)); });
3127 count = nheader_words + code_instruction_words(code->code_size);
3128 break;
3131 /* unboxed objects */
3132 case BIGNUM_WIDETAG:
3133 #if N_WORD_BITS != 64
3134 case SINGLE_FLOAT_WIDETAG:
3135 #endif
3136 case DOUBLE_FLOAT_WIDETAG:
3137 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
3138 case LONG_FLOAT_WIDETAG:
3139 #endif
3140 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
3141 case COMPLEX_SINGLE_FLOAT_WIDETAG:
3142 #endif
3143 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
3144 case COMPLEX_DOUBLE_FLOAT_WIDETAG:
3145 #endif
3146 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
3147 case COMPLEX_LONG_FLOAT_WIDETAG:
3148 #endif
3149 #ifdef SIMD_PACK_WIDETAG
3150 case SIMD_PACK_WIDETAG:
3151 #endif
3152 #include "genesis/specialized-vectors.inc"
3153 case SAP_WIDETAG:
3154 case WEAK_POINTER_WIDETAG:
3155 #ifdef NO_TLS_VALUE_MARKER_WIDETAG
3156 case NO_TLS_VALUE_MARKER_WIDETAG:
3157 #endif
3158 count = (sizetab[widetag_of(*start)])(start);
3159 break;
3161 default:
3162 lose("Unhandled widetag %p at %p\n",
3163 widetag_of(*start), start);
3167 start += count;
3168 words -= count;
3172 static void verify_dynamic_space();
3174 static void
3175 verify_gc(void)
3177 /* FIXME: It would be nice to make names consistent so that
3178 * foo_size meant size *in* *bytes* instead of size in some
3179 * arbitrary units. (Yes, this caused a bug, how did you guess?:-)
3180 * Some counts of lispobjs are called foo_count; it might be good
3181 * to grep for all foo_size and rename the appropriate ones to
3182 * foo_count. */
3183 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3184 # ifdef __linux__
3185 // Try this verification if marknsweep was compiled with extra debugging.
3186 // But weak symbols don't work on macOS.
3187 extern void __attribute__((weak)) check_varyobj_pages();
3188 if (&check_varyobj_pages) check_varyobj_pages();
3189 # endif
3190 verify_space((lispobj*)IMMOBILE_SPACE_START,
3191 (lispobj*)SymbolValue(IMMOBILE_FIXEDOBJ_FREE_POINTER,0)
3192 - (lispobj*)IMMOBILE_SPACE_START);
3193 verify_space((lispobj*)IMMOBILE_VARYOBJ_SUBSPACE_START,
3194 (lispobj*)SymbolValue(IMMOBILE_SPACE_FREE_POINTER,0)
3195 - (lispobj*)IMMOBILE_VARYOBJ_SUBSPACE_START);
3196 #endif
3197 sword_t read_only_space_size =
3198 (lispobj*)SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0)
3199 - (lispobj*)READ_ONLY_SPACE_START;
3200 sword_t static_space_size =
3201 (lispobj*)SymbolValue(STATIC_SPACE_FREE_POINTER,0)
3202 - (lispobj*)STATIC_SPACE_START;
3203 struct thread *th;
3204 for_each_thread(th) {
3205 sword_t binding_stack_size =
3206 (lispobj*)get_binding_stack_pointer(th)
3207 - (lispobj*)th->binding_stack_start;
3208 verify_space(th->binding_stack_start, binding_stack_size);
3210 verify_space((lispobj*)READ_ONLY_SPACE_START, read_only_space_size);
3211 verify_space((lispobj*)STATIC_SPACE_START , static_space_size);
3212 verify_dynamic_space();
3215 void
3216 walk_generation(void (*proc)(lispobj*,size_t),
3217 generation_index_t generation)
3219 page_index_t i;
3220 int genmask = generation >= 0 ? 1 << generation : ~0;
3222 for (i = 0; i < last_free_page; i++) {
3223 if (page_allocated_p(i)
3224 && (page_bytes_used(i) != 0)
3225 && ((1 << page_table[i].gen) & genmask)) {
3226 page_index_t last_page;
3228 /* This should be the start of a contiguous block */
3229 gc_assert(page_starts_contiguous_block_p(i));
3231 /* Need to find the full extent of this contiguous block in case
3232 objects span pages. */
3234 /* Now work forward until the end of this contiguous area is
3235 found. */
3236 for (last_page = i; ;last_page++)
3237 /* Check whether this is the last page in this contiguous
3238 * block. */
3239 if (page_ends_contiguous_block_p(last_page, page_table[i].gen))
3240 break;
3242 proc(page_address(i),
3243 ((uword_t)(page_bytes_used(last_page) + npage_bytes(last_page-i)))
3244 / N_WORD_BYTES);
3245 i = last_page;
3249 static void verify_generation(generation_index_t generation)
3251 walk_generation(verify_space, generation);
3254 /* Check that all the free space is zero filled. */
3255 static void
3256 verify_zero_fill(void)
3258 page_index_t page;
3260 for (page = 0; page < last_free_page; page++) {
3261 if (page_free_p(page)) {
3262 /* The whole page should be zero filled. */
3263 sword_t *start_addr = (sword_t *)page_address(page);
3264 sword_t i;
3265 for (i = 0; i < (sword_t)GENCGC_CARD_BYTES/N_WORD_BYTES; i++) {
3266 if (start_addr[i] != 0) {
3267 lose("free page not zero at %x\n", start_addr + i);
3270 } else {
3271 sword_t free_bytes = GENCGC_CARD_BYTES - page_bytes_used(page);
3272 if (free_bytes > 0) {
3273 sword_t *start_addr = (sword_t *)((uword_t)page_address(page)
3274 + page_bytes_used(page));
3275 sword_t size = free_bytes / N_WORD_BYTES;
3276 sword_t i;
3277 for (i = 0; i < size; i++) {
3278 if (start_addr[i] != 0) {
3279 lose("free region not zero at %x\n", start_addr + i);
3287 /* External entry point for verify_zero_fill */
3288 void
3289 gencgc_verify_zero_fill(void)
3291 /* Flush the alloc regions updating the tables. */
3292 gc_alloc_update_all_page_tables(1);
3293 SHOW("verifying zero fill");
3294 verify_zero_fill();
3297 static void
3298 verify_dynamic_space(void)
3300 verify_generation(-1);
3301 if (gencgc_enable_verify_zero_fill)
3302 verify_zero_fill();
3305 /* Write-protect all the dynamic boxed pages in the given generation. */
3306 static void
3307 write_protect_generation_pages(generation_index_t generation)
3309 page_index_t start;
3311 gc_assert(generation < SCRATCH_GENERATION);
3313 for (start = 0; start < last_free_page; start++) {
3314 if (protect_page_p(start, generation)) {
3315 void *page_start;
3316 page_index_t last;
3318 /* Note the page as protected in the page tables. */
3319 page_table[start].write_protected = 1;
3321 for (last = start + 1; last < last_free_page; last++) {
3322 if (!protect_page_p(last, generation))
3323 break;
3324 page_table[last].write_protected = 1;
3327 page_start = (void *)page_address(start);
3329 os_protect(page_start,
3330 npage_bytes(last - start),
3331 OS_VM_PROT_READ | OS_VM_PROT_EXECUTE);
3333 start = last;
3337 if (gencgc_verbose > 1) {
3338 FSHOW((stderr,
3339 "/write protected %d of %d pages in generation %d\n",
3340 count_write_protect_generation_pages(generation),
3341 count_generation_pages(generation),
3342 generation));
3346 #if defined(LISP_FEATURE_SB_THREAD) && (defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64))
3347 static void
3348 preserve_context_registers (os_context_t *c)
3350 void **ptr;
3351 /* On Darwin the signal context isn't a contiguous block of memory,
3352 * so just preserve_pointering its contents won't be sufficient.
3354 #if defined(LISP_FEATURE_DARWIN)||defined(LISP_FEATURE_WIN32)
3355 #if defined LISP_FEATURE_X86
3356 preserve_pointer((void*)*os_context_register_addr(c,reg_EAX));
3357 preserve_pointer((void*)*os_context_register_addr(c,reg_ECX));
3358 preserve_pointer((void*)*os_context_register_addr(c,reg_EDX));
3359 preserve_pointer((void*)*os_context_register_addr(c,reg_EBX));
3360 preserve_pointer((void*)*os_context_register_addr(c,reg_ESI));
3361 preserve_pointer((void*)*os_context_register_addr(c,reg_EDI));
3362 preserve_pointer((void*)*os_context_pc_addr(c));
3363 #elif defined LISP_FEATURE_X86_64
3364 preserve_pointer((void*)*os_context_register_addr(c,reg_RAX));
3365 preserve_pointer((void*)*os_context_register_addr(c,reg_RCX));
3366 preserve_pointer((void*)*os_context_register_addr(c,reg_RDX));
3367 preserve_pointer((void*)*os_context_register_addr(c,reg_RBX));
3368 preserve_pointer((void*)*os_context_register_addr(c,reg_RSI));
3369 preserve_pointer((void*)*os_context_register_addr(c,reg_RDI));
3370 preserve_pointer((void*)*os_context_register_addr(c,reg_R8));
3371 preserve_pointer((void*)*os_context_register_addr(c,reg_R9));
3372 preserve_pointer((void*)*os_context_register_addr(c,reg_R10));
3373 preserve_pointer((void*)*os_context_register_addr(c,reg_R11));
3374 preserve_pointer((void*)*os_context_register_addr(c,reg_R12));
3375 preserve_pointer((void*)*os_context_register_addr(c,reg_R13));
3376 preserve_pointer((void*)*os_context_register_addr(c,reg_R14));
3377 preserve_pointer((void*)*os_context_register_addr(c,reg_R15));
3378 preserve_pointer((void*)*os_context_pc_addr(c));
3379 #else
3380 #error "preserve_context_registers needs to be tweaked for non-x86 Darwin"
3381 #endif
3382 #endif
3383 #if !defined(LISP_FEATURE_WIN32)
3384 for(ptr = ((void **)(c+1))-1; ptr>=(void **)c; ptr--) {
3385 preserve_pointer(*ptr);
3387 #endif
3389 #endif
3391 static void
3392 move_pinned_pages_to_newspace()
3394 page_index_t i;
3396 /* scavenge() will evacuate all oldspace pages, but no newspace
3397 * pages. Pinned pages are precisely those pages which must not
3398 * be evacuated, so move them to newspace directly. */
3400 for (i = 0; i < last_free_page; i++) {
3401 if (page_table[i].dont_move &&
3402 /* dont_move is cleared lazily, so validate the space as well. */
3403 page_table[i].gen == from_space) {
3404 if (pinned_dwords(i) && do_wipe_p) {
3405 // do not move to newspace after all, this will be word-wiped
3406 continue;
3408 page_table[i].gen = new_space;
3409 /* And since we're moving the pages wholesale, also adjust
3410 * the generation allocation counters. */
3411 int used = page_bytes_used(i);
3412 generations[new_space].bytes_allocated += used;
3413 generations[from_space].bytes_allocated -= used;
3418 /* Garbage collect a generation. If raise is 0 then the remains of the
3419 * generation are not raised to the next generation. */
3420 static void
3421 garbage_collect_generation(generation_index_t generation, int raise)
3423 page_index_t i;
3424 struct thread *th;
3426 gc_assert(generation <= HIGHEST_NORMAL_GENERATION);
3428 /* The oldest generation can't be raised. */
3429 gc_assert((generation != HIGHEST_NORMAL_GENERATION) || (raise == 0));
3431 /* Check if weak hash tables were processed in the previous GC. */
3432 gc_assert(weak_hash_tables == NULL);
3434 /* Initialize the weak pointer list. */
3435 weak_pointers = NULL;
3437 /* When a generation is not being raised it is transported to a
3438 * temporary generation (NUM_GENERATIONS), and lowered when
3439 * done. Set up this new generation. There should be no pages
3440 * allocated to it yet. */
3441 if (!raise) {
3442 gc_assert(generations[SCRATCH_GENERATION].bytes_allocated == 0);
3445 /* Set the global src and dest. generations */
3446 from_space = generation;
3447 if (raise)
3448 new_space = generation+1;
3449 else
3450 new_space = SCRATCH_GENERATION;
3452 /* Change to a new space for allocation, resetting the alloc_start_page */
3453 gc_alloc_generation = new_space;
3454 generations[new_space].alloc_start_page = 0;
3455 generations[new_space].alloc_unboxed_start_page = 0;
3456 generations[new_space].alloc_large_start_page = 0;
3457 generations[new_space].alloc_large_unboxed_start_page = 0;
3459 /* Before any pointers are preserved, the dont_move flags on the
3460 * pages need to be cleared. */
3461 for (i = 0; i < last_free_page; i++)
3462 if(page_table[i].gen==from_space) {
3463 page_table[i].dont_move = 0;
3464 gc_assert(pinned_dwords(i) == NULL);
3467 /* Un-write-protect the old-space pages. This is essential for the
3468 * promoted pages as they may contain pointers into the old-space
3469 * which need to be scavenged. It also helps avoid unnecessary page
3470 * faults as forwarding pointers are written into them. They need to
3471 * be un-protected anyway before unmapping later. */
3472 unprotect_oldspace();
3474 /* Scavenge the stacks' conservative roots. */
3476 /* there are potentially two stacks for each thread: the main
3477 * stack, which may contain Lisp pointers, and the alternate stack.
3478 * We don't ever run Lisp code on the altstack, but it may
3479 * host a sigcontext with lisp objects in it */
3481 /* what we need to do: (1) find the stack pointer for the main
3482 * stack; scavenge it (2) find the interrupt context on the
3483 * alternate stack that might contain lisp values, and scavenge
3484 * that */
3486 /* we assume that none of the preceding applies to the thread that
3487 * initiates GC. If you ever call GC from inside an altstack
3488 * handler, you will lose. */
3490 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
3491 /* And if we're saving a core, there's no point in being conservative. */
3492 if (conservative_stack) {
3493 for_each_thread(th) {
3494 void **ptr;
3495 void **esp=(void **)-1;
3496 if (th->state == STATE_DEAD)
3497 continue;
3498 # if defined(LISP_FEATURE_SB_SAFEPOINT)
3499 /* Conservative collect_garbage is always invoked with a
3500 * foreign C call or an interrupt handler on top of every
3501 * existing thread, so the stored SP in each thread
3502 * structure is valid, no matter which thread we are looking
3503 * at. For threads that were running Lisp code, the pitstop
3504 * and edge functions maintain this value within the
3505 * interrupt or exception handler. */
3506 esp = os_get_csp(th);
3507 assert_on_stack(th, esp);
3509 /* In addition to pointers on the stack, also preserve the
3510 * return PC, the only value from the context that we need
3511 * in addition to the SP. The return PC gets saved by the
3512 * foreign call wrapper, and removed from the control stack
3513 * into a register. */
3514 preserve_pointer(th->pc_around_foreign_call);
3516 /* And on platforms with interrupts: scavenge ctx registers. */
3518 /* Disabled on Windows, because it does not have an explicit
3519 * stack of `interrupt_contexts'. The reported CSP has been
3520 * chosen so that the current context on the stack is
3521 * covered by the stack scan. See also set_csp_from_context(). */
3522 # ifndef LISP_FEATURE_WIN32
3523 if (th != arch_os_get_current_thread()) {
3524 long k = fixnum_value(
3525 SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,th));
3526 while (k > 0)
3527 preserve_context_registers(th->interrupt_contexts[--k]);
3529 # endif
3530 # elif defined(LISP_FEATURE_SB_THREAD)
3531 sword_t i,free;
3532 if(th==arch_os_get_current_thread()) {
3533 /* Somebody is going to burn in hell for this, but casting
3534 * it in two steps shuts gcc up about strict aliasing. */
3535 esp = (void **)((void *)&raise);
3536 } else {
3537 void **esp1;
3538 free=fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,th));
3539 for(i=free-1;i>=0;i--) {
3540 os_context_t *c=th->interrupt_contexts[i];
3541 esp1 = (void **) *os_context_register_addr(c,reg_SP);
3542 if (esp1>=(void **)th->control_stack_start &&
3543 esp1<(void **)th->control_stack_end) {
3544 if(esp1<esp) esp=esp1;
3545 preserve_context_registers(c);
3549 # else
3550 esp = (void **)((void *)&raise);
3551 # endif
3552 if (!esp || esp == (void*) -1)
3553 lose("garbage_collect: no SP known for thread %x (OS %x)",
3554 th, th->os_thread);
3555 for (ptr = ((void **)th->control_stack_end)-1; ptr >= esp; ptr--) {
3556 preserve_pointer(*ptr);
3560 #else
3561 /* Non-x86oid systems don't have "conservative roots" as such, but
3562 * the same mechanism is used for objects pinned for use by alien
3563 * code. */
3564 for_each_thread(th) {
3565 lispobj pin_list = SymbolTlValue(PINNED_OBJECTS,th);
3566 while (pin_list != NIL) {
3567 struct cons *list_entry =
3568 (struct cons *)native_pointer(pin_list);
3569 preserve_pointer((void*)list_entry->car);
3570 pin_list = list_entry->cdr;
3573 #endif
3575 #if QSHOW
3576 if (gencgc_verbose > 1) {
3577 sword_t num_dont_move_pages = count_dont_move_pages();
3578 fprintf(stderr,
3579 "/non-movable pages due to conservative pointers = %ld (%lu bytes)\n",
3580 num_dont_move_pages,
3581 npage_bytes(num_dont_move_pages));
3583 #endif
3585 /* Now that all of the pinned (dont_move) pages are known, and
3586 * before we start to scavenge (and thus relocate) objects,
3587 * relocate the pinned pages to newspace, so that the scavenger
3588 * will not attempt to relocate their contents. */
3589 move_pinned_pages_to_newspace();
3591 /* Scavenge all the rest of the roots. */
3593 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
3595 * If not x86, we need to scavenge the interrupt context(s) and the
3596 * control stack.
3599 struct thread *th;
3600 for_each_thread(th) {
3601 scavenge_interrupt_contexts(th);
3602 scavenge_control_stack(th);
3605 # ifdef LISP_FEATURE_SB_SAFEPOINT
3606 /* In this case, scrub all stacks right here from the GCing thread
3607 * instead of doing what the comment below says. Suboptimal, but
3608 * easier. */
3609 for_each_thread(th)
3610 scrub_thread_control_stack(th);
3611 # else
3612 /* Scrub the unscavenged control stack space, so that we can't run
3613 * into any stale pointers in a later GC (this is done by the
3614 * stop-for-gc handler in the other threads). */
3615 scrub_control_stack();
3616 # endif
3618 #endif
3620 /* Scavenge the Lisp functions of the interrupt handlers, taking
3621 * care to avoid SIG_DFL and SIG_IGN. */
3622 for (i = 0; i < NSIG; i++) {
3623 union interrupt_handler handler = interrupt_handlers[i];
3624 if (!ARE_SAME_HANDLER(handler.c, SIG_IGN) &&
3625 !ARE_SAME_HANDLER(handler.c, SIG_DFL)) {
3626 scavenge((lispobj *)(interrupt_handlers + i), 1);
3629 /* Scavenge the binding stacks. */
3631 struct thread *th;
3632 for_each_thread(th) {
3633 sword_t len= (lispobj *)get_binding_stack_pointer(th) -
3634 th->binding_stack_start;
3635 scavenge((lispobj *) th->binding_stack_start,len);
3636 #ifdef LISP_FEATURE_SB_THREAD
3637 /* do the tls as well */
3638 len=(SymbolValue(FREE_TLS_INDEX,0) >> WORD_SHIFT) -
3639 (sizeof (struct thread))/(sizeof (lispobj));
3640 scavenge((lispobj *) (th+1),len);
3641 #endif
3645 /* Scavenge static space. */
3646 if (gencgc_verbose > 1) {
3647 FSHOW((stderr,
3648 "/scavenge static space: %d bytes\n",
3649 SymbolValue(STATIC_SPACE_FREE_POINTER,0) - STATIC_SPACE_START));
3651 heap_scavenge((lispobj*)STATIC_SPACE_START,
3652 (lispobj*)SymbolValue(STATIC_SPACE_FREE_POINTER,0));
3654 /* All generations but the generation being GCed need to be
3655 * scavenged. The new_space generation needs special handling as
3656 * objects may be moved in - it is handled separately below. */
3657 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3658 scavenge_immobile_roots(generation+1, SCRATCH_GENERATION);
3659 #endif
3660 scavenge_generations(generation+1, PSEUDO_STATIC_GENERATION);
3662 scavenge_pinned_ranges();
3664 /* Finally scavenge the new_space generation. Keep going until no
3665 * more objects are moved into the new generation */
3666 scavenge_newspace_generation(new_space);
3668 /* FIXME: I tried reenabling this check when debugging unrelated
3669 * GC weirdness ca. sbcl-0.6.12.45, and it failed immediately.
3670 * Since the current GC code seems to work well, I'm guessing that
3671 * this debugging code is just stale, but I haven't tried to
3672 * figure it out. It should be figured out and then either made to
3673 * work or just deleted. */
3675 #define RESCAN_CHECK 0
3676 #if RESCAN_CHECK
3677 /* As a check re-scavenge the newspace once; no new objects should
3678 * be found. */
3680 os_vm_size_t old_bytes_allocated = bytes_allocated;
3681 os_vm_size_t bytes_allocated;
3683 /* Start with a full scavenge. */
3684 scavenge_newspace_generation_one_scan(new_space);
3686 /* Flush the current regions, updating the tables. */
3687 gc_alloc_update_all_page_tables(1);
3689 bytes_allocated = bytes_allocated - old_bytes_allocated;
3691 if (bytes_allocated != 0) {
3692 lose("Rescan of new_space allocated %d more bytes.\n",
3693 bytes_allocated);
3696 #endif
3698 scan_weak_hash_tables();
3699 scan_weak_pointers();
3700 wipe_nonpinned_words();
3701 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3702 // Do this last, because until wipe_nonpinned_words() happens,
3703 // not all page table entries have the 'gen' value updated,
3704 // which we need to correctly find all old->young pointers.
3705 sweep_immobile_space(raise);
3706 #endif
3708 /* Flush the current regions, updating the tables. */
3709 gc_alloc_update_all_page_tables(0);
3711 /* Free the pages in oldspace, but not those marked dont_move. */
3712 free_oldspace();
3714 /* If the GC is not raising the age then lower the generation back
3715 * to its normal generation number */
3716 if (!raise) {
3717 for (i = 0; i < last_free_page; i++)
3718 if ((page_bytes_used(i) != 0)
3719 && (page_table[i].gen == SCRATCH_GENERATION))
3720 page_table[i].gen = generation;
3721 gc_assert(generations[generation].bytes_allocated == 0);
3722 generations[generation].bytes_allocated =
3723 generations[SCRATCH_GENERATION].bytes_allocated;
3724 generations[SCRATCH_GENERATION].bytes_allocated = 0;
3727 /* Reset the alloc_start_page for generation. */
3728 generations[generation].alloc_start_page = 0;
3729 generations[generation].alloc_unboxed_start_page = 0;
3730 generations[generation].alloc_large_start_page = 0;
3731 generations[generation].alloc_large_unboxed_start_page = 0;
3733 if (generation >= verify_gens) {
3734 if (gencgc_verbose) {
3735 SHOW("verifying");
3737 verify_gc();
3740 /* Set the new gc trigger for the GCed generation. */
3741 generations[generation].gc_trigger =
3742 generations[generation].bytes_allocated
3743 + generations[generation].bytes_consed_between_gc;
3745 if (raise)
3746 generations[generation].num_gc = 0;
3747 else
3748 ++generations[generation].num_gc;
3752 /* Update last_free_page, then SymbolValue(ALLOCATION_POINTER). */
3753 sword_t
3754 update_dynamic_space_free_pointer(void)
3756 page_index_t last_page = -1, i;
3758 for (i = 0; i < last_free_page; i++)
3759 if (page_allocated_p(i) && (page_bytes_used(i) != 0))
3760 last_page = i;
3762 last_free_page = last_page+1;
3764 set_alloc_pointer((lispobj)(page_address(last_free_page)));
3765 return 0; /* dummy value: return something ... */
3768 static void
3769 remap_page_range (page_index_t from, page_index_t to)
3771 /* There's a mysterious Solaris/x86 problem with using mmap
3772 * tricks for memory zeroing. See sbcl-devel thread
3773 * "Re: patch: standalone executable redux".
3775 #if defined(LISP_FEATURE_SUNOS)
3776 zero_and_mark_pages(from, to);
3777 #else
3778 const page_index_t
3779 release_granularity = gencgc_release_granularity/GENCGC_CARD_BYTES,
3780 release_mask = release_granularity-1,
3781 end = to+1,
3782 aligned_from = (from+release_mask)&~release_mask,
3783 aligned_end = (end&~release_mask);
3785 if (aligned_from < aligned_end) {
3786 zero_pages_with_mmap(aligned_from, aligned_end-1);
3787 if (aligned_from != from)
3788 zero_and_mark_pages(from, aligned_from-1);
3789 if (aligned_end != end)
3790 zero_and_mark_pages(aligned_end, end-1);
3791 } else {
3792 zero_and_mark_pages(from, to);
3794 #endif
3797 static void
3798 remap_free_pages (page_index_t from, page_index_t to, int forcibly)
3800 page_index_t first_page, last_page;
3802 if (forcibly)
3803 return remap_page_range(from, to);
3805 for (first_page = from; first_page <= to; first_page++) {
3806 if (page_allocated_p(first_page) || !page_need_to_zero(first_page))
3807 continue;
3809 last_page = first_page + 1;
3810 while (page_free_p(last_page) &&
3811 (last_page <= to) &&
3812 (page_need_to_zero(last_page)))
3813 last_page++;
3815 remap_page_range(first_page, last_page-1);
3817 first_page = last_page;
3821 generation_index_t small_generation_limit = 1;
3823 /* GC all generations newer than last_gen, raising the objects in each
3824 * to the next older generation - we finish when all generations below
3825 * last_gen are empty. Then if last_gen is due for a GC, or if
3826 * last_gen==NUM_GENERATIONS (the scratch generation? eh?) we GC that
3827 * too. The valid range for last_gen is: 0,1,...,NUM_GENERATIONS.
3829 * We stop collecting at gencgc_oldest_gen_to_gc, even if this is less than
3830 * last_gen (oh, and note that by default it is NUM_GENERATIONS-1) */
3831 void
3832 collect_garbage(generation_index_t last_gen)
3834 generation_index_t gen = 0, i;
3835 int raise, more = 0;
3836 int gen_to_wp;
3837 /* The largest value of last_free_page seen since the time
3838 * remap_free_pages was called. */
3839 static page_index_t high_water_mark = 0;
3841 FSHOW((stderr, "/entering collect_garbage(%d)\n", last_gen));
3842 log_generation_stats(gc_logfile, "=== GC Start ===");
3844 gc_active_p = 1;
3846 if (last_gen > HIGHEST_NORMAL_GENERATION+1) {
3847 FSHOW((stderr,
3848 "/collect_garbage: last_gen = %d, doing a level 0 GC\n",
3849 last_gen));
3850 last_gen = 0;
3853 /* Flush the alloc regions updating the tables. */
3854 gc_alloc_update_all_page_tables(1);
3856 /* Verify the new objects created by Lisp code. */
3857 if (pre_verify_gen_0) {
3858 FSHOW((stderr, "pre-checking generation 0\n"));
3859 verify_generation(0);
3862 if (gencgc_verbose > 1)
3863 print_generation_stats();
3865 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3866 /* Immobile space generation bits are lazily updated for gen0
3867 (not touched on every object allocation) so do it now */
3868 update_immobile_nursery_bits();
3869 #endif
3871 do {
3872 /* Collect the generation. */
3874 if (more || (gen >= gencgc_oldest_gen_to_gc)) {
3875 /* Never raise the oldest generation. Never raise the extra generation
3876 * collected due to more-flag. */
3877 raise = 0;
3878 more = 0;
3879 } else {
3880 raise =
3881 (gen < last_gen)
3882 || (generations[gen].num_gc >= generations[gen].number_of_gcs_before_promotion);
3883 /* If we would not normally raise this one, but we're
3884 * running low on space in comparison to the object-sizes
3885 * we've been seeing, raise it and collect the next one
3886 * too. */
3887 if (!raise && gen == last_gen) {
3888 more = (2*large_allocation) >= (dynamic_space_size - bytes_allocated);
3889 raise = more;
3893 if (gencgc_verbose > 1) {
3894 FSHOW((stderr,
3895 "starting GC of generation %d with raise=%d alloc=%d trig=%d GCs=%d\n",
3896 gen,
3897 raise,
3898 generations[gen].bytes_allocated,
3899 generations[gen].gc_trigger,
3900 generations[gen].num_gc));
3903 /* If an older generation is being filled, then update its
3904 * memory age. */
3905 if (raise == 1) {
3906 generations[gen+1].cum_sum_bytes_allocated +=
3907 generations[gen+1].bytes_allocated;
3910 garbage_collect_generation(gen, raise);
3912 /* Reset the memory age cum_sum. */
3913 generations[gen].cum_sum_bytes_allocated = 0;
3915 if (gencgc_verbose > 1) {
3916 FSHOW((stderr, "GC of generation %d finished:\n", gen));
3917 print_generation_stats();
3920 gen++;
3921 } while ((gen <= gencgc_oldest_gen_to_gc)
3922 && ((gen < last_gen)
3923 || more
3924 || (raise
3925 && (generations[gen].bytes_allocated
3926 > generations[gen].gc_trigger)
3927 && (generation_average_age(gen)
3928 > generations[gen].minimum_age_before_gc))));
3930 /* Now if gen-1 was raised all generations before gen are empty.
3931 * If it wasn't raised then all generations before gen-1 are empty.
3933 * Now objects within this gen's pages cannot point to younger
3934 * generations unless they are written to. This can be exploited
3935 * by write-protecting the pages of gen; then when younger
3936 * generations are GCed only the pages which have been written
3937 * need scanning. */
3938 if (raise)
3939 gen_to_wp = gen;
3940 else
3941 gen_to_wp = gen - 1;
3943 /* There's not much point in WPing pages in generation 0 as it is
3944 * never scavenged (except promoted pages). */
3945 if ((gen_to_wp > 0) && enable_page_protection) {
3946 /* Check that they are all empty. */
3947 for (i = 0; i < gen_to_wp; i++) {
3948 if (generations[i].bytes_allocated)
3949 lose("trying to write-protect gen. %d when gen. %d nonempty\n",
3950 gen_to_wp, i);
3952 write_protect_generation_pages(gen_to_wp);
3954 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3955 write_protect_immobile_space();
3956 #endif
3958 /* Set gc_alloc() back to generation 0. The current regions should
3959 * be flushed after the above GCs. */
3960 gc_assert((boxed_region.free_pointer - boxed_region.start_addr) == 0);
3961 gc_alloc_generation = 0;
3963 /* Save the high-water mark before updating last_free_page */
3964 if (last_free_page > high_water_mark)
3965 high_water_mark = last_free_page;
3967 update_dynamic_space_free_pointer();
3969 /* Update auto_gc_trigger. Make sure we trigger the next GC before
3970 * running out of heap! */
3971 if (bytes_consed_between_gcs <= (dynamic_space_size - bytes_allocated))
3972 auto_gc_trigger = bytes_allocated + bytes_consed_between_gcs;
3973 else
3974 auto_gc_trigger = bytes_allocated + (dynamic_space_size - bytes_allocated)/2;
3976 if(gencgc_verbose)
3977 fprintf(stderr,"Next gc when %"OS_VM_SIZE_FMT" bytes have been consed\n",
3978 auto_gc_trigger);
3980 /* If we did a big GC (arbitrarily defined as gen > 1), release memory
3981 * back to the OS.
3983 if (gen > small_generation_limit) {
3984 if (last_free_page > high_water_mark)
3985 high_water_mark = last_free_page;
3986 remap_free_pages(0, high_water_mark, 0);
3987 high_water_mark = 0;
3990 gc_active_p = 0;
3991 large_allocation = 0;
3993 log_generation_stats(gc_logfile, "=== GC End ===");
3994 SHOW("returning from collect_garbage");
3997 void
3998 gc_init(void)
4000 page_index_t i;
4002 #if defined(LISP_FEATURE_SB_SAFEPOINT)
4003 alloc_gc_page();
4004 #endif
4006 /* Compute the number of pages needed for the dynamic space.
4007 * Dynamic space size should be aligned on page size. */
4008 page_table_pages = dynamic_space_size/GENCGC_CARD_BYTES;
4009 gc_assert(dynamic_space_size == npage_bytes(page_table_pages));
4011 /* Default nursery size to 5% of the total dynamic space size,
4012 * min 1Mb. */
4013 bytes_consed_between_gcs = dynamic_space_size/(os_vm_size_t)20;
4014 if (bytes_consed_between_gcs < (1024*1024))
4015 bytes_consed_between_gcs = 1024*1024;
4017 /* The page_table must be allocated using "calloc" to initialize
4018 * the page structures correctly. There used to be a separate
4019 * initialization loop (now commented out; see below) but that was
4020 * unnecessary and did hurt startup time. */
4021 page_table = calloc(page_table_pages, sizeof(struct page));
4022 gc_assert(page_table);
4023 #ifdef LISP_FEATURE_IMMOBILE_SPACE
4024 gc_init_immobile();
4025 #endif
4027 size_t pins_map_size_in_bytes =
4028 (n_dwords_in_card / N_WORD_BITS) * sizeof (uword_t) * page_table_pages;
4029 /* We use mmap directly here so that we can use a minimum of
4030 system calls per page during GC.
4031 All we need here now is a madvise(DONTNEED) at the end of GC. */
4032 page_table_pinned_dwords
4033 = (in_use_marker_t*)os_validate(NULL, pins_map_size_in_bytes);
4034 /* We do not need to zero */
4035 gc_assert(page_table_pinned_dwords);
4037 scavtab[WEAK_POINTER_WIDETAG] = scav_weak_pointer;
4038 transother[SIMPLE_ARRAY_WIDETAG] = trans_boxed_large;
4040 /* The page structures are initialized implicitly when page_table
4041 * is allocated with "calloc" above. Formerly we had the following
4042 * explicit initialization here (comments converted to C99 style
4043 * for readability as C's block comments don't nest):
4045 * // Initialize each page structure.
4046 * for (i = 0; i < page_table_pages; i++) {
4047 * // Initialize all pages as free.
4048 * page_table[i].allocated = FREE_PAGE_FLAG;
4049 * page_table[i].bytes_used = 0;
4051 * // Pages are not write-protected at startup.
4052 * page_table[i].write_protected = 0;
4055 * Without this loop the image starts up much faster when dynamic
4056 * space is large -- which it is on 64-bit platforms already by
4057 * default -- and when "calloc" for large arrays is implemented
4058 * using copy-on-write of a page of zeroes -- which it is at least
4059 * on Linux. In this case the pages that page_table_pages is stored
4060 * in are mapped and cleared not before the corresponding part of
4061 * dynamic space is used. For example, this saves clearing 16 MB of
4062 * memory at startup if the page size is 4 KB and the size of
4063 * dynamic space is 4 GB.
4064 * FREE_PAGE_FLAG must be 0 for this to work correctly which is
4065 * asserted below: */
4067 /* Compile time assertion: If triggered, declares an array
4068 * of dimension -1 forcing a syntax error. The intent of the
4069 * assignment is to avoid an "unused variable" warning. */
4070 char assert_free_page_flag_0[(FREE_PAGE_FLAG) ? -1 : 1];
4071 assert_free_page_flag_0[0] = assert_free_page_flag_0[0];
4074 bytes_allocated = 0;
4076 /* Initialize the generations. */
4077 for (i = 0; i < NUM_GENERATIONS; i++) {
4078 generations[i].alloc_start_page = 0;
4079 generations[i].alloc_unboxed_start_page = 0;
4080 generations[i].alloc_large_start_page = 0;
4081 generations[i].alloc_large_unboxed_start_page = 0;
4082 generations[i].bytes_allocated = 0;
4083 generations[i].gc_trigger = 2000000;
4084 generations[i].num_gc = 0;
4085 generations[i].cum_sum_bytes_allocated = 0;
4086 /* the tune-able parameters */
4087 generations[i].bytes_consed_between_gc
4088 = bytes_consed_between_gcs/(os_vm_size_t)HIGHEST_NORMAL_GENERATION;
4089 generations[i].number_of_gcs_before_promotion = 1;
4090 generations[i].minimum_age_before_gc = 0.75;
4093 /* Initialize gc_alloc. */
4094 gc_alloc_generation = 0;
4095 gc_set_region_empty(&boxed_region);
4096 gc_set_region_empty(&unboxed_region);
4098 last_free_page = 0;
4101 /* Pick up the dynamic space from after a core load.
4103 * The ALLOCATION_POINTER points to the end of the dynamic space.
4106 static void
4107 gencgc_pickup_dynamic(void)
4109 page_index_t page = 0;
4110 void *alloc_ptr = (void *)get_alloc_pointer();
4111 lispobj *prev=(lispobj *)page_address(page);
4112 generation_index_t gen = PSEUDO_STATIC_GENERATION;
4114 bytes_allocated = 0;
4116 do {
4117 lispobj *first,*ptr= (lispobj *)page_address(page);
4119 if (!gencgc_partial_pickup || page_allocated_p(page)) {
4120 /* It is possible, though rare, for the saved page table
4121 * to contain free pages below alloc_ptr. */
4122 page_table[page].gen = gen;
4123 set_page_bytes_used(page, GENCGC_CARD_BYTES);
4124 page_table[page].large_object = 0;
4125 page_table[page].write_protected = 0;
4126 page_table[page].write_protected_cleared = 0;
4127 page_table[page].dont_move = 0;
4128 set_page_need_to_zero(page, 1);
4130 bytes_allocated += GENCGC_CARD_BYTES;
4133 if (!gencgc_partial_pickup) {
4134 page_table[page].allocated = BOXED_PAGE_FLAG;
4135 first = gc_search_space3(ptr, prev, (ptr+2));
4136 if(ptr == first)
4137 prev=ptr;
4138 set_page_scan_start_offset(page,
4139 page_address(page) - (void *)prev);
4141 page++;
4142 } while (page_address(page) < alloc_ptr);
4144 last_free_page = page;
4146 generations[gen].bytes_allocated = bytes_allocated;
4148 gc_alloc_update_all_page_tables(1);
4149 write_protect_generation_pages(gen);
4152 void
4153 gc_initialize_pointers(void)
4155 gencgc_pickup_dynamic();
4159 /* alloc(..) is the external interface for memory allocation. It
4160 * allocates to generation 0. It is not called from within the garbage
4161 * collector as it is only external uses that need the check for heap
4162 * size (GC trigger) and to disable the interrupts (interrupts are
4163 * always disabled during a GC).
4165 * The vops that call alloc(..) assume that the returned space is zero-filled.
4166 * (E.g. the most significant word of a 2-word bignum in MOVE-FROM-UNSIGNED.)
4168 * The check for a GC trigger is only performed when the current
4169 * region is full, so in most cases it's not needed. */
4171 static inline lispobj *
4172 general_alloc_internal(sword_t nbytes, int page_type_flag, struct alloc_region *region,
4173 struct thread *thread)
4175 #ifndef LISP_FEATURE_WIN32
4176 lispobj alloc_signal;
4177 #endif
4178 void *new_obj;
4179 void *new_free_pointer;
4180 os_vm_size_t trigger_bytes = 0;
4182 gc_assert(nbytes > 0);
4184 /* Check for alignment allocation problems. */
4185 gc_assert((((uword_t)region->free_pointer & LOWTAG_MASK) == 0)
4186 && ((nbytes & LOWTAG_MASK) == 0));
4188 #if !(defined(LISP_FEATURE_WIN32) && defined(LISP_FEATURE_SB_THREAD))
4189 /* Must be inside a PA section. */
4190 gc_assert(get_pseudo_atomic_atomic(thread));
4191 #endif
4193 if ((os_vm_size_t) nbytes > large_allocation)
4194 large_allocation = nbytes;
4196 /* maybe we can do this quickly ... */
4197 new_free_pointer = region->free_pointer + nbytes;
4198 if (new_free_pointer <= region->end_addr) {
4199 new_obj = (void*)(region->free_pointer);
4200 region->free_pointer = new_free_pointer;
4201 return(new_obj); /* yup */
4204 /* We don't want to count nbytes against auto_gc_trigger unless we
4205 * have to: it speeds up the tenuring of objects and slows down
4206 * allocation. However, unless we do so when allocating _very_
4207 * large objects we are in danger of exhausting the heap without
4208 * running sufficient GCs.
4210 if ((os_vm_size_t) nbytes >= bytes_consed_between_gcs)
4211 trigger_bytes = nbytes;
4213 /* we have to go the long way around, it seems. Check whether we
4214 * should GC in the near future
4216 if (auto_gc_trigger && (bytes_allocated+trigger_bytes > auto_gc_trigger)) {
4217 /* Don't flood the system with interrupts if the need to gc is
4218 * already noted. This can happen for example when SUB-GC
4219 * allocates or after a gc triggered in a WITHOUT-GCING. */
4220 if (SymbolValue(GC_PENDING,thread) == NIL) {
4221 /* set things up so that GC happens when we finish the PA
4222 * section */
4223 SetSymbolValue(GC_PENDING,T,thread);
4224 if (SymbolValue(GC_INHIBIT,thread) == NIL) {
4225 #ifdef LISP_FEATURE_SB_SAFEPOINT
4226 thread_register_gc_trigger();
4227 #else
4228 set_pseudo_atomic_interrupted(thread);
4229 #ifdef GENCGC_IS_PRECISE
4230 /* PPC calls alloc() from a trap
4231 * look up the most context if it's from a trap. */
4233 os_context_t *context =
4234 thread->interrupt_data->allocation_trap_context;
4235 maybe_save_gc_mask_and_block_deferrables
4236 (context ? os_context_sigmask_addr(context) : NULL);
4238 #else
4239 maybe_save_gc_mask_and_block_deferrables(NULL);
4240 #endif
4241 #endif
4245 new_obj = gc_alloc_with_region(nbytes, page_type_flag, region, 0);
4247 #ifndef LISP_FEATURE_WIN32
4248 /* for sb-prof, and not supported on Windows yet */
4249 alloc_signal = SymbolValue(ALLOC_SIGNAL,thread);
4250 if ((alloc_signal & FIXNUM_TAG_MASK) == 0) {
4251 if ((sword_t) alloc_signal <= 0) {
4252 SetSymbolValue(ALLOC_SIGNAL, T, thread);
4253 raise(SIGPROF);
4254 } else {
4255 SetSymbolValue(ALLOC_SIGNAL,
4256 alloc_signal - (1 << N_FIXNUM_TAG_BITS),
4257 thread);
4260 #endif
4262 return (new_obj);
4265 lispobj *
4266 general_alloc(sword_t nbytes, int page_type_flag)
4268 struct thread *thread = arch_os_get_current_thread();
4269 /* Select correct region, and call general_alloc_internal with it.
4270 * For other then boxed allocation we must lock first, since the
4271 * region is shared. */
4272 if (BOXED_PAGE_FLAG & page_type_flag) {
4273 #ifdef LISP_FEATURE_SB_THREAD
4274 struct alloc_region *region = (thread ? &(thread->alloc_region) : &boxed_region);
4275 #else
4276 struct alloc_region *region = &boxed_region;
4277 #endif
4278 return general_alloc_internal(nbytes, page_type_flag, region, thread);
4279 } else if (UNBOXED_PAGE_FLAG == page_type_flag) {
4280 lispobj * obj;
4281 int result;
4282 result = thread_mutex_lock(&allocation_lock);
4283 gc_assert(!result);
4284 obj = general_alloc_internal(nbytes, page_type_flag, &unboxed_region, thread);
4285 result = thread_mutex_unlock(&allocation_lock);
4286 gc_assert(!result);
4287 return obj;
4288 } else {
4289 lose("bad page type flag: %d", page_type_flag);
4293 lispobj AMD64_SYSV_ABI *
4294 alloc(sword_t nbytes)
4296 #ifdef LISP_FEATURE_SB_SAFEPOINT_STRICTLY
4297 struct thread *self = arch_os_get_current_thread();
4298 int was_pseudo_atomic = get_pseudo_atomic_atomic(self);
4299 if (!was_pseudo_atomic)
4300 set_pseudo_atomic_atomic(self);
4301 #else
4302 gc_assert(get_pseudo_atomic_atomic(arch_os_get_current_thread()));
4303 #endif
4305 lispobj *result = general_alloc(nbytes, BOXED_PAGE_FLAG);
4307 #ifdef LISP_FEATURE_SB_SAFEPOINT_STRICTLY
4308 if (!was_pseudo_atomic)
4309 clear_pseudo_atomic_atomic(self);
4310 #endif
4312 return result;
4316 * shared support for the OS-dependent signal handlers which
4317 * catch GENCGC-related write-protect violations
4319 void unhandled_sigmemoryfault(void* addr);
4321 /* Depending on which OS we're running under, different signals might
4322 * be raised for a violation of write protection in the heap. This
4323 * function factors out the common generational GC magic which needs
4324 * to invoked in this case, and should be called from whatever signal
4325 * handler is appropriate for the OS we're running under.
4327 * Return true if this signal is a normal generational GC thing that
4328 * we were able to handle, or false if it was abnormal and control
4329 * should fall through to the general SIGSEGV/SIGBUS/whatever logic.
4331 * We have two control flags for this: one causes us to ignore faults
4332 * on unprotected pages completely, and the second complains to stderr
4333 * but allows us to continue without losing.
4335 extern boolean ignore_memoryfaults_on_unprotected_pages;
4336 boolean ignore_memoryfaults_on_unprotected_pages = 0;
4338 extern boolean continue_after_memoryfault_on_unprotected_pages;
4339 boolean continue_after_memoryfault_on_unprotected_pages = 0;
4342 gencgc_handle_wp_violation(void* fault_addr)
4344 page_index_t page_index = find_page_index(fault_addr);
4346 #if QSHOW_SIGNALS
4347 FSHOW((stderr,
4348 "heap WP violation? fault_addr=%p, page_index=%"PAGE_INDEX_FMT"\n",
4349 fault_addr, page_index));
4350 #endif
4352 /* Check whether the fault is within the dynamic space. */
4353 if (page_index == (-1)) {
4354 #ifdef LISP_FEATURE_IMMOBILE_SPACE
4355 extern int immobile_space_handle_wp_violation(void*);
4356 if (immobile_space_handle_wp_violation(fault_addr))
4357 return 1;
4358 #endif
4360 /* It can be helpful to be able to put a breakpoint on this
4361 * case to help diagnose low-level problems. */
4362 unhandled_sigmemoryfault(fault_addr);
4364 /* not within the dynamic space -- not our responsibility */
4365 return 0;
4367 } else {
4368 int ret;
4369 ret = thread_mutex_lock(&free_pages_lock);
4370 gc_assert(ret == 0);
4371 if (page_table[page_index].write_protected) {
4372 /* Unprotect the page. */
4373 os_protect(page_address(page_index), GENCGC_CARD_BYTES, OS_VM_PROT_ALL);
4374 page_table[page_index].write_protected_cleared = 1;
4375 page_table[page_index].write_protected = 0;
4376 } else if (!ignore_memoryfaults_on_unprotected_pages) {
4377 /* The only acceptable reason for this signal on a heap
4378 * access is that GENCGC write-protected the page.
4379 * However, if two CPUs hit a wp page near-simultaneously,
4380 * we had better not have the second one lose here if it
4381 * does this test after the first one has already set wp=0
4383 if(page_table[page_index].write_protected_cleared != 1) {
4384 void lisp_backtrace(int frames);
4385 lisp_backtrace(10);
4386 fprintf(stderr,
4387 "Fault @ %p, page %"PAGE_INDEX_FMT" not marked as write-protected:\n"
4388 " boxed_region.first_page: %"PAGE_INDEX_FMT","
4389 " boxed_region.last_page %"PAGE_INDEX_FMT"\n"
4390 " page.scan_start_offset: %"OS_VM_SIZE_FMT"\n"
4391 " page.bytes_used: %u\n"
4392 " page.allocated: %d\n"
4393 " page.write_protected: %d\n"
4394 " page.write_protected_cleared: %d\n"
4395 " page.generation: %d\n",
4396 fault_addr,
4397 page_index,
4398 boxed_region.first_page,
4399 boxed_region.last_page,
4400 page_scan_start_offset(page_index),
4401 page_bytes_used(page_index),
4402 page_table[page_index].allocated,
4403 page_table[page_index].write_protected,
4404 page_table[page_index].write_protected_cleared,
4405 page_table[page_index].gen);
4406 if (!continue_after_memoryfault_on_unprotected_pages)
4407 lose("Feh.\n");
4410 ret = thread_mutex_unlock(&free_pages_lock);
4411 gc_assert(ret == 0);
4412 /* Don't worry, we can handle it. */
4413 return 1;
4416 /* This is to be called when we catch a SIGSEGV/SIGBUS, determine that
4417 * it's not just a case of the program hitting the write barrier, and
4418 * are about to let Lisp deal with it. It's basically just a
4419 * convenient place to set a gdb breakpoint. */
4420 void
4421 unhandled_sigmemoryfault(void *addr)
4424 static void
4425 update_thread_page_tables(struct thread *th)
4427 gc_alloc_update_page_tables(BOXED_PAGE_FLAG, &th->alloc_region);
4428 #if defined(LISP_FEATURE_SB_SAFEPOINT_STRICTLY) && !defined(LISP_FEATURE_WIN32)
4429 gc_alloc_update_page_tables(BOXED_PAGE_FLAG, &th->sprof_alloc_region);
4430 #endif
4433 /* GC is single-threaded and all memory allocations during a
4434 collection happen in the GC thread, so it is sufficient to update
4435 all the the page tables once at the beginning of a collection and
4436 update only page tables of the GC thread during the collection. */
4437 void gc_alloc_update_all_page_tables(int for_all_threads)
4439 /* Flush the alloc regions updating the tables. */
4440 struct thread *th;
4441 if (for_all_threads) {
4442 for_each_thread(th) {
4443 update_thread_page_tables(th);
4446 else {
4447 th = arch_os_get_current_thread();
4448 if (th) {
4449 update_thread_page_tables(th);
4452 gc_alloc_update_page_tables(UNBOXED_PAGE_FLAG, &unboxed_region);
4453 gc_alloc_update_page_tables(BOXED_PAGE_FLAG, &boxed_region);
4456 void
4457 gc_set_region_empty(struct alloc_region *region)
4459 region->first_page = 0;
4460 region->last_page = -1;
4461 region->start_addr = page_address(0);
4462 region->free_pointer = page_address(0);
4463 region->end_addr = page_address(0);
4466 static void
4467 zero_all_free_pages()
4469 page_index_t i;
4471 for (i = 0; i < last_free_page; i++) {
4472 if (page_free_p(i)) {
4473 #ifdef READ_PROTECT_FREE_PAGES
4474 os_protect(page_address(i),
4475 GENCGC_CARD_BYTES,
4476 OS_VM_PROT_ALL);
4477 #endif
4478 zero_pages(i, i);
4483 /* Things to do before doing a final GC before saving a core (without
4484 * purify).
4486 * + Pages in large_object pages aren't moved by the GC, so we need to
4487 * unset that flag from all pages.
4488 * + The pseudo-static generation isn't normally collected, but it seems
4489 * reasonable to collect it at least when saving a core. So move the
4490 * pages to a normal generation.
4492 static void
4493 prepare_for_final_gc ()
4495 page_index_t i;
4497 #ifdef LISP_FEATURE_IMMOBILE_SPACE
4498 extern void prepare_immobile_space_for_final_gc();
4499 prepare_immobile_space_for_final_gc ();
4500 #endif
4501 do_wipe_p = 0;
4502 for (i = 0; i < last_free_page; i++) {
4503 page_table[i].large_object = 0;
4504 if (page_table[i].gen == PSEUDO_STATIC_GENERATION) {
4505 int used = page_bytes_used(i);
4506 page_table[i].gen = HIGHEST_NORMAL_GENERATION;
4507 generations[PSEUDO_STATIC_GENERATION].bytes_allocated -= used;
4508 generations[HIGHEST_NORMAL_GENERATION].bytes_allocated += used;
4514 /* Do a non-conservative GC, and then save a core with the initial
4515 * function being set to the value of the static symbol
4516 * SB!VM:RESTART-LISP-FUNCTION */
4517 void
4518 gc_and_save(char *filename, boolean prepend_runtime,
4519 boolean save_runtime_options, boolean compressed,
4520 int compression_level, int application_type)
4522 FILE *file;
4523 void *runtime_bytes = NULL;
4524 size_t runtime_size;
4526 file = prepare_to_save(filename, prepend_runtime, &runtime_bytes,
4527 &runtime_size);
4528 if (file == NULL)
4529 return;
4531 conservative_stack = 0;
4533 /* The filename might come from Lisp, and be moved by the now
4534 * non-conservative GC. */
4535 filename = strdup(filename);
4537 /* Collect twice: once into relatively high memory, and then back
4538 * into low memory. This compacts the retained data into the lower
4539 * pages, minimizing the size of the core file.
4541 prepare_for_final_gc();
4542 gencgc_alloc_start_page = last_free_page;
4543 collect_garbage(HIGHEST_NORMAL_GENERATION+1);
4545 prepare_for_final_gc();
4546 gencgc_alloc_start_page = -1;
4547 collect_garbage(HIGHEST_NORMAL_GENERATION+1);
4549 if (prepend_runtime)
4550 save_runtime_to_filehandle(file, runtime_bytes, runtime_size,
4551 application_type);
4553 /* The dumper doesn't know that pages need to be zeroed before use. */
4554 zero_all_free_pages();
4555 save_to_filehandle(file, filename, SymbolValue(RESTART_LISP_FUNCTION,0),
4556 prepend_runtime, save_runtime_options,
4557 compressed ? compression_level : COMPRESSION_LEVEL_NONE);
4558 /* Oops. Save still managed to fail. Since we've mangled the stack
4559 * beyond hope, there's not much we can do.
4560 * (beyond FUNCALLing RESTART_LISP_FUNCTION, but I suspect that's
4561 * going to be rather unsatisfactory too... */
4562 lose("Attempt to save core after non-conservative GC failed.\n");