2 * C-level stuff to implement Lisp-level PURIFY
6 * This software is part of the SBCL system. See the README file for
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 #include <sys/types.h>
26 #include "interrupt.h"
30 #include "gc-internal.h"
31 #include "primitive-objects.h"
36 /* again, what's so special about the x86 that this is differently
37 * visible there than on other platforms? -dan 20010125
39 static lispobj *dynamic_space_free_pointer;
43 lose("GC invariant lost, file \"%s\", line %d", __FILE__, __LINE__)
46 #define gc_assert(ex) do { \
47 if (!(ex)) gc_abort(); \
54 /* These hold the original end of the read_only and static spaces so
55 * we can tell what are forwarding pointers. */
57 static lispobj *read_only_end, *static_end;
59 static lispobj *read_only_free, *static_free;
61 static lispobj *pscav(lispobj *addr, int nwords, boolean constant);
63 #define LATERBLOCKSIZE 1020
64 #define LATERMAXCOUNT 10
73 } *later_blocks = NULL;
74 static int later_count = 0;
76 #define CEILING(x,y) (((x) + ((y) - 1)) & (~((y) - 1)))
77 #define NWORDS(x,y) (CEILING((x),(y)) / (y))
79 /* FIXME: Shouldn't this be defined in sbcl.h? See also notes in
83 #define FUN_RAW_ADDR_OFFSET 0
85 #define FUN_RAW_ADDR_OFFSET (6*sizeof(lispobj) - FUN_POINTER_LOWTAG)
89 forwarding_pointer_p(lispobj obj)
91 lispobj *ptr = native_pointer(obj);
93 return ((static_end <= ptr && ptr <= static_free) ||
94 (read_only_end <= ptr && ptr <= read_only_free));
98 dynamic_pointer_p(lispobj ptr)
101 return (ptr >= (lispobj)current_dynamic_space
103 ptr < (lispobj)dynamic_space_free_pointer);
105 /* Be more conservative, and remember, this is a maybe. */
106 return (ptr >= (lispobj)DYNAMIC_SPACE_START
108 ptr < (lispobj)dynamic_space_free_pointer);
115 #ifdef LISP_FEATURE_GENCGC
117 * enhanced x86/GENCGC stack scavenging by Douglas Crosher
119 * Scavenging the stack on the i386 is problematic due to conservative
120 * roots and raw return addresses. Here it is handled in two passes:
121 * the first pass runs before any objects are moved and tries to
122 * identify valid pointers and return address on the stack, the second
123 * pass scavenges these.
126 static unsigned pointer_filter_verbose = 0;
128 /* FIXME: This is substantially the same code as in gencgc.c. (There
129 * are some differences, at least (1) the gencgc.c code needs to worry
130 * about return addresses on the stack pinning code objects, (2) the
131 * gencgc.c code needs to worry about the GC maybe happening in an
132 * interrupt service routine when the main thread of control was
133 * interrupted just as it had allocated memory and before it
134 * initialized it, while PURIFY needn't worry about that, and (3) the
135 * gencgc.c code has mutated more under maintenance since the fork
136 * from CMU CL than the code here has.) The two versions should be
137 * made to explicitly share common code, instead of just two different
138 * cut-and-pasted versions. */
140 valid_dynamic_space_pointer(lispobj *pointer, lispobj *start_addr)
142 /* If it's not a return address then it needs to be a valid Lisp
144 if (!is_lisp_pointer((lispobj)pointer))
147 /* Check that the object pointed to is consistent with the pointer
149 switch (lowtag_of((lispobj)pointer)) {
150 case FUN_POINTER_LOWTAG:
151 /* Start_addr should be the enclosing code object, or a closure
153 switch (widetag_of(*start_addr)) {
154 case CODE_HEADER_WIDETAG:
155 /* This case is probably caught above. */
157 case CLOSURE_HEADER_WIDETAG:
158 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
159 if ((int)pointer != ((int)start_addr+FUN_POINTER_LOWTAG)) {
160 if (pointer_filter_verbose) {
161 fprintf(stderr,"*Wf2: %x %x %x\n", (unsigned int) pointer,
162 (unsigned int) start_addr, *start_addr);
168 if (pointer_filter_verbose) {
169 fprintf(stderr,"*Wf3: %x %x %x\n", (unsigned int) pointer,
170 (unsigned int) start_addr, *start_addr);
175 case LIST_POINTER_LOWTAG:
176 if ((int)pointer != ((int)start_addr+LIST_POINTER_LOWTAG)) {
177 if (pointer_filter_verbose)
178 fprintf(stderr,"*Wl1: %x %x %x\n", (unsigned int) pointer,
179 (unsigned int) start_addr, *start_addr);
182 /* Is it plausible cons? */
183 if ((is_lisp_pointer(start_addr[0])
184 || ((start_addr[0] & 3) == 0) /* fixnum */
185 || (widetag_of(start_addr[0]) == BASE_CHAR_WIDETAG)
186 || (widetag_of(start_addr[0]) == UNBOUND_MARKER_WIDETAG))
187 && (is_lisp_pointer(start_addr[1])
188 || ((start_addr[1] & 3) == 0) /* fixnum */
189 || (widetag_of(start_addr[1]) == BASE_CHAR_WIDETAG)
190 || (widetag_of(start_addr[1]) == UNBOUND_MARKER_WIDETAG))) {
193 if (pointer_filter_verbose) {
194 fprintf(stderr,"*Wl2: %x %x %x\n", (unsigned int) pointer,
195 (unsigned int) start_addr, *start_addr);
199 case INSTANCE_POINTER_LOWTAG:
200 if ((int)pointer != ((int)start_addr+INSTANCE_POINTER_LOWTAG)) {
201 if (pointer_filter_verbose) {
202 fprintf(stderr,"*Wi1: %x %x %x\n", (unsigned int) pointer,
203 (unsigned int) start_addr, *start_addr);
207 if (widetag_of(start_addr[0]) != INSTANCE_HEADER_WIDETAG) {
208 if (pointer_filter_verbose) {
209 fprintf(stderr,"*Wi2: %x %x %x\n", (unsigned int) pointer,
210 (unsigned int) start_addr, *start_addr);
215 case OTHER_POINTER_LOWTAG:
216 if ((int)pointer != ((int)start_addr+OTHER_POINTER_LOWTAG)) {
217 if (pointer_filter_verbose) {
218 fprintf(stderr,"*Wo1: %x %x %x\n", (unsigned int) pointer,
219 (unsigned int) start_addr, *start_addr);
223 /* Is it plausible? Not a cons. XXX should check the headers. */
224 if (is_lisp_pointer(start_addr[0]) || ((start_addr[0] & 3) == 0)) {
225 if (pointer_filter_verbose) {
226 fprintf(stderr,"*Wo2: %x %x %x\n", (unsigned int) pointer,
227 (unsigned int) start_addr, *start_addr);
231 switch (widetag_of(start_addr[0])) {
232 case UNBOUND_MARKER_WIDETAG:
233 case BASE_CHAR_WIDETAG:
234 if (pointer_filter_verbose) {
235 fprintf(stderr,"*Wo3: %x %x %x\n", (unsigned int) pointer,
236 (unsigned int) start_addr, *start_addr);
240 /* only pointed to by function pointers? */
241 case CLOSURE_HEADER_WIDETAG:
242 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
243 if (pointer_filter_verbose) {
244 fprintf(stderr,"*Wo4: %x %x %x\n", (unsigned int) pointer,
245 (unsigned int) start_addr, *start_addr);
249 case INSTANCE_HEADER_WIDETAG:
250 if (pointer_filter_verbose) {
251 fprintf(stderr,"*Wo5: %x %x %x\n", (unsigned int) pointer,
252 (unsigned int) start_addr, *start_addr);
256 /* the valid other immediate pointer objects */
257 case SIMPLE_VECTOR_WIDETAG:
259 case COMPLEX_WIDETAG:
260 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
261 case COMPLEX_SINGLE_FLOAT_WIDETAG:
263 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
264 case COMPLEX_DOUBLE_FLOAT_WIDETAG:
266 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
267 case COMPLEX_LONG_FLOAT_WIDETAG:
269 case SIMPLE_ARRAY_WIDETAG:
270 case COMPLEX_STRING_WIDETAG:
271 case COMPLEX_BIT_VECTOR_WIDETAG:
272 case COMPLEX_VECTOR_WIDETAG:
273 case COMPLEX_ARRAY_WIDETAG:
274 case VALUE_CELL_HEADER_WIDETAG:
275 case SYMBOL_HEADER_WIDETAG:
277 case CODE_HEADER_WIDETAG:
279 case SINGLE_FLOAT_WIDETAG:
280 case DOUBLE_FLOAT_WIDETAG:
281 #ifdef LONG_FLOAT_WIDETAG
282 case LONG_FLOAT_WIDETAG:
284 case SIMPLE_STRING_WIDETAG:
285 case SIMPLE_BIT_VECTOR_WIDETAG:
286 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
287 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
288 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
289 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
290 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
291 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
292 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
294 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
295 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
297 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
298 case SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG:
300 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
301 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
303 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
304 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
305 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
306 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
308 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
309 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
311 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
312 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
314 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
315 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
318 case WEAK_POINTER_WIDETAG:
322 if (pointer_filter_verbose) {
323 fprintf(stderr,"*Wo6: %x %x %x\n", (unsigned int) pointer,
324 (unsigned int) start_addr, *start_addr);
330 if (pointer_filter_verbose) {
331 fprintf(stderr,"*W?: %x %x %x\n", (unsigned int) pointer,
332 (unsigned int) start_addr, *start_addr);
341 #define MAX_STACK_POINTERS 256
342 lispobj *valid_stack_locations[MAX_STACK_POINTERS];
343 unsigned int num_valid_stack_locations;
345 #define MAX_STACK_RETURN_ADDRESSES 128
346 lispobj *valid_stack_ra_locations[MAX_STACK_RETURN_ADDRESSES];
347 lispobj *valid_stack_ra_code_objects[MAX_STACK_RETURN_ADDRESSES];
348 unsigned int num_valid_stack_ra_locations;
350 /* Identify valid stack slots. */
352 setup_i386_stack_scav(lispobj *lowaddr, lispobj *base)
354 lispobj *sp = lowaddr;
355 num_valid_stack_locations = 0;
356 num_valid_stack_ra_locations = 0;
357 for (sp = lowaddr; sp < base; sp++) {
359 /* Find the object start address */
360 lispobj *start_addr = search_dynamic_space((void *)thing);
362 /* We need to allow raw pointers into Code objects for
363 * return addresses. This will also pick up pointers to
364 * functions in code objects. */
365 if (widetag_of(*start_addr) == CODE_HEADER_WIDETAG) {
366 /* FIXME asserting here is a really dumb thing to do.
367 * If we've overflowed some arbitrary static limit, we
368 * should just refuse to purify, instead of killing
369 * the whole lisp session
371 gc_assert(num_valid_stack_ra_locations <
372 MAX_STACK_RETURN_ADDRESSES);
373 valid_stack_ra_locations[num_valid_stack_ra_locations] = sp;
374 valid_stack_ra_code_objects[num_valid_stack_ra_locations++] =
375 (lispobj *)((int)start_addr + OTHER_POINTER_LOWTAG);
377 if (valid_dynamic_space_pointer((void *)thing, start_addr)) {
378 gc_assert(num_valid_stack_locations < MAX_STACK_POINTERS);
379 valid_stack_locations[num_valid_stack_locations++] = sp;
384 if (pointer_filter_verbose) {
385 fprintf(stderr, "number of valid stack pointers = %d\n",
386 num_valid_stack_locations);
387 fprintf(stderr, "number of stack return addresses = %d\n",
388 num_valid_stack_ra_locations);
393 pscav_i386_stack(void)
397 for (i = 0; i < num_valid_stack_locations; i++)
398 pscav(valid_stack_locations[i], 1, 0);
400 for (i = 0; i < num_valid_stack_ra_locations; i++) {
401 lispobj code_obj = (lispobj)valid_stack_ra_code_objects[i];
402 pscav(&code_obj, 1, 0);
403 if (pointer_filter_verbose) {
404 fprintf(stderr,"*C moved RA %x to %x; for code object %x to %x\n",
405 *valid_stack_ra_locations[i],
406 (int)(*valid_stack_ra_locations[i])
407 - ((int)valid_stack_ra_code_objects[i] - (int)code_obj),
408 (unsigned int) valid_stack_ra_code_objects[i], code_obj);
410 *valid_stack_ra_locations[i] =
411 ((int)(*valid_stack_ra_locations[i])
412 - ((int)valid_stack_ra_code_objects[i] - (int)code_obj));
420 pscav_later(lispobj *where, int count)
424 if (count > LATERMAXCOUNT) {
425 while (count > LATERMAXCOUNT) {
426 pscav_later(where, LATERMAXCOUNT);
427 count -= LATERMAXCOUNT;
428 where += LATERMAXCOUNT;
432 if (later_blocks == NULL || later_count == LATERBLOCKSIZE ||
433 (later_count == LATERBLOCKSIZE-1 && count > 1)) {
434 new = (struct later *)malloc(sizeof(struct later));
435 new->next = later_blocks;
436 if (later_blocks && later_count < LATERBLOCKSIZE)
437 later_blocks->u[later_count].ptr = NULL;
443 later_blocks->u[later_count++].count = count;
444 later_blocks->u[later_count++].ptr = where;
449 ptrans_boxed(lispobj thing, lispobj header, boolean constant)
452 lispobj result, *new, *old;
454 nwords = 1 + HeaderValue(header);
457 old = (lispobj *)native_pointer(thing);
459 new = read_only_free;
460 read_only_free += CEILING(nwords, 2);
464 static_free += CEILING(nwords, 2);
468 bcopy(old, new, nwords * sizeof(lispobj));
470 /* Deposit forwarding pointer. */
471 result = make_lispobj(new, lowtag_of(thing));
475 pscav(new, nwords, constant);
480 /* We need to look at the layout to see whether it is a pure structure
481 * class, and only then can we transport as constant. If it is pure,
482 * we can ALWAYS transport as a constant. */
484 ptrans_instance(lispobj thing, lispobj header, boolean constant)
486 lispobj layout = ((struct instance *)native_pointer(thing))->slots[0];
487 lispobj pure = ((struct instance *)native_pointer(layout))->slots[15];
491 return (ptrans_boxed(thing, header, 1));
493 return (ptrans_boxed(thing, header, 0));
496 /* Substructure: special case for the COMPACT-INFO-ENVs,
497 * where the instance may have a point to the dynamic
498 * space placed into it (e.g. the cache-name slot), but
499 * the lists and arrays at the time of a purify can be
500 * moved to the RO space. */
502 lispobj result, *new, *old;
504 nwords = 1 + HeaderValue(header);
507 old = (lispobj *)native_pointer(thing);
509 static_free += CEILING(nwords, 2);
512 bcopy(old, new, nwords * sizeof(lispobj));
514 /* Deposit forwarding pointer. */
515 result = make_lispobj(new, lowtag_of(thing));
519 pscav(new, nwords, 1);
525 return NIL; /* dummy value: return something ... */
530 ptrans_fdefn(lispobj thing, lispobj header)
533 lispobj result, *new, *old, oldfn;
536 nwords = 1 + HeaderValue(header);
539 old = (lispobj *)native_pointer(thing);
541 static_free += CEILING(nwords, 2);
544 bcopy(old, new, nwords * sizeof(lispobj));
546 /* Deposit forwarding pointer. */
547 result = make_lispobj(new, lowtag_of(thing));
550 /* Scavenge the function. */
551 fdefn = (struct fdefn *)new;
553 pscav(&fdefn->fun, 1, 0);
554 if ((char *)oldfn + FUN_RAW_ADDR_OFFSET == fdefn->raw_addr)
555 fdefn->raw_addr = (char *)fdefn->fun + FUN_RAW_ADDR_OFFSET;
561 ptrans_unboxed(lispobj thing, lispobj header)
564 lispobj result, *new, *old;
566 nwords = 1 + HeaderValue(header);
569 old = (lispobj *)native_pointer(thing);
570 new = read_only_free;
571 read_only_free += CEILING(nwords, 2);
574 bcopy(old, new, nwords * sizeof(lispobj));
576 /* Deposit forwarding pointer. */
577 result = make_lispobj(new , lowtag_of(thing));
584 ptrans_vector(lispobj thing, int bits, int extra,
585 boolean boxed, boolean constant)
587 struct vector *vector;
589 lispobj result, *new;
591 vector = (struct vector *)native_pointer(thing);
592 nwords = 2 + (CEILING((fixnum_value(vector->length)+extra)*bits,32)>>5);
594 if (boxed && !constant) {
596 static_free += CEILING(nwords, 2);
599 new = read_only_free;
600 read_only_free += CEILING(nwords, 2);
603 bcopy(vector, new, nwords * sizeof(lispobj));
605 result = make_lispobj(new, lowtag_of(thing));
606 vector->header = result;
609 pscav(new, nwords, constant);
616 apply_code_fixups_during_purify(struct code *old_code, struct code *new_code)
618 int nheader_words, ncode_words, nwords;
619 void *constants_start_addr, *constants_end_addr;
620 void *code_start_addr, *code_end_addr;
621 lispobj fixups = NIL;
622 unsigned displacement = (unsigned)new_code - (unsigned)old_code;
623 struct vector *fixups_vector;
625 ncode_words = fixnum_value(new_code->code_size);
626 nheader_words = HeaderValue(*(lispobj *)new_code);
627 nwords = ncode_words + nheader_words;
629 constants_start_addr = (void *)new_code + 5*4;
630 constants_end_addr = (void *)new_code + nheader_words*4;
631 code_start_addr = (void *)new_code + nheader_words*4;
632 code_end_addr = (void *)new_code + nwords*4;
634 /* The first constant should be a pointer to the fixups for this
635 * code objects. Check. */
636 fixups = new_code->constants[0];
638 /* It will be 0 or the unbound-marker if there are no fixups, and
639 * will be an other-pointer to a vector if it is valid. */
641 (fixups==UNBOUND_MARKER_WIDETAG) ||
642 !is_lisp_pointer(fixups)) {
643 #ifdef LISP_FEATURE_GENCGC
644 /* Check for a possible errors. */
645 sniff_code_object(new_code,displacement);
650 fixups_vector = (struct vector *)native_pointer(fixups);
652 /* Could be pointing to a forwarding pointer. */
653 if (is_lisp_pointer(fixups) && (dynamic_pointer_p(fixups))
654 && forwarding_pointer_p(*(lispobj *)fixups_vector)) {
655 /* If so then follow it. */
657 (struct vector *)native_pointer(*(lispobj *)fixups_vector);
660 if (widetag_of(fixups_vector->header) ==
661 SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG) {
662 /* We got the fixups for the code block. Now work through the
663 * vector, and apply a fixup at each address. */
664 int length = fixnum_value(fixups_vector->length);
666 for (i=0; i<length; i++) {
667 unsigned offset = fixups_vector->data[i];
668 /* Now check the current value of offset. */
670 *(unsigned *)((unsigned)code_start_addr + offset);
672 /* If it's within the old_code object then it must be an
673 * absolute fixup (relative ones are not saved) */
674 if ((old_value>=(unsigned)old_code)
675 && (old_value<((unsigned)old_code + nwords*4)))
676 /* So add the dispacement. */
677 *(unsigned *)((unsigned)code_start_addr + offset) = old_value
680 /* It is outside the old code object so it must be a relative
681 * fixup (absolute fixups are not saved). So subtract the
683 *(unsigned *)((unsigned)code_start_addr + offset) = old_value
688 /* No longer need the fixups. */
689 new_code->constants[0] = 0;
691 #ifdef LISP_FEATURE_GENCGC
692 /* Check for possible errors. */
693 sniff_code_object(new_code,displacement);
699 ptrans_code(lispobj thing)
701 struct code *code, *new;
703 lispobj func, result;
705 code = (struct code *)native_pointer(thing);
706 nwords = HeaderValue(code->header) + fixnum_value(code->code_size);
708 new = (struct code *)read_only_free;
709 read_only_free += CEILING(nwords, 2);
711 bcopy(code, new, nwords * sizeof(lispobj));
713 #ifdef LISP_FEATURE_X86
714 apply_code_fixups_during_purify(code,new);
717 result = make_lispobj(new, OTHER_POINTER_LOWTAG);
719 /* Stick in a forwarding pointer for the code object. */
720 *(lispobj *)code = result;
722 /* Put in forwarding pointers for all the functions. */
723 for (func = code->entry_points;
725 func = ((struct simple_fun *)native_pointer(func))->next) {
727 gc_assert(lowtag_of(func) == FUN_POINTER_LOWTAG);
729 *(lispobj *)native_pointer(func) = result + (func - thing);
732 /* Arrange to scavenge the debug info later. */
733 pscav_later(&new->debug_info, 1);
735 if (new->trace_table_offset & 0x3)
737 pscav(&new->trace_table_offset, 1, 0);
739 new->trace_table_offset = NIL; /* limit lifetime */
742 /* Scavenge the constants. */
743 pscav(new->constants, HeaderValue(new->header)-5, 1);
745 /* Scavenge all the functions. */
746 pscav(&new->entry_points, 1, 1);
747 for (func = new->entry_points;
749 func = ((struct simple_fun *)native_pointer(func))->next) {
750 gc_assert(lowtag_of(func) == FUN_POINTER_LOWTAG);
751 gc_assert(!dynamic_pointer_p(func));
754 /* Temporarly convert the self pointer to a real function pointer. */
755 ((struct simple_fun *)native_pointer(func))->self
756 -= FUN_RAW_ADDR_OFFSET;
758 pscav(&((struct simple_fun *)native_pointer(func))->self, 2, 1);
760 ((struct simple_fun *)native_pointer(func))->self
761 += FUN_RAW_ADDR_OFFSET;
763 pscav_later(&((struct simple_fun *)native_pointer(func))->name, 3);
770 ptrans_func(lispobj thing, lispobj header)
773 lispobj code, *new, *old, result;
774 struct simple_fun *function;
776 /* Thing can either be a function header, a closure function
777 * header, a closure, or a funcallable-instance. If it's a closure
778 * or a funcallable-instance, we do the same as ptrans_boxed.
779 * Otherwise we have to do something strange, 'cause it is buried
780 * inside a code object. */
782 if (widetag_of(header) == SIMPLE_FUN_HEADER_WIDETAG ||
783 widetag_of(header) == CLOSURE_FUN_HEADER_WIDETAG) {
785 /* We can only end up here if the code object has not been
786 * scavenged, because if it had been scavenged, forwarding pointers
787 * would have been left behind for all the entry points. */
789 function = (struct simple_fun *)native_pointer(thing);
792 ((native_pointer(thing) -
793 (HeaderValue(function->header))), OTHER_POINTER_LOWTAG);
795 /* This will cause the function's header to be replaced with a
796 * forwarding pointer. */
800 /* So we can just return that. */
801 return function->header;
804 /* It's some kind of closure-like thing. */
805 nwords = 1 + HeaderValue(header);
806 old = (lispobj *)native_pointer(thing);
808 /* Allocate the new one. */
809 if (widetag_of(header) == FUNCALLABLE_INSTANCE_HEADER_WIDETAG) {
810 /* FINs *must* not go in read_only space. */
812 static_free += CEILING(nwords, 2);
815 /* Closures can always go in read-only space, 'cause they
818 new = read_only_free;
819 read_only_free += CEILING(nwords, 2);
822 bcopy(old, new, nwords * sizeof(lispobj));
824 /* Deposit forwarding pointer. */
825 result = make_lispobj(new, lowtag_of(thing));
829 pscav(new, nwords, 0);
836 ptrans_returnpc(lispobj thing, lispobj header)
840 /* Find the corresponding code object. */
841 code = thing - HeaderValue(header)*sizeof(lispobj);
843 /* Make sure it's been transported. */
844 new = *(lispobj *)native_pointer(code);
845 if (!forwarding_pointer_p(new))
846 new = ptrans_code(code);
848 /* Maintain the offset: */
849 return new + (thing - code);
852 #define WORDS_PER_CONS CEILING(sizeof(struct cons) / sizeof(lispobj), 2)
855 ptrans_list(lispobj thing, boolean constant)
857 struct cons *old, *new, *orig;
861 orig = (struct cons *)read_only_free;
863 orig = (struct cons *)static_free;
867 /* Allocate a new cons cell. */
868 old = (struct cons *)native_pointer(thing);
870 new = (struct cons *)read_only_free;
871 read_only_free += WORDS_PER_CONS;
874 new = (struct cons *)static_free;
875 static_free += WORDS_PER_CONS;
878 /* Copy the cons cell and keep a pointer to the cdr. */
880 thing = new->cdr = old->cdr;
882 /* Set up the forwarding pointer. */
883 *(lispobj *)old = make_lispobj(new, LIST_POINTER_LOWTAG);
885 /* And count this cell. */
887 } while (lowtag_of(thing) == LIST_POINTER_LOWTAG &&
888 dynamic_pointer_p(thing) &&
889 !(forwarding_pointer_p(*(lispobj *)native_pointer(thing))));
891 /* Scavenge the list we just copied. */
892 pscav((lispobj *)orig, length * WORDS_PER_CONS, constant);
894 return make_lispobj(orig, LIST_POINTER_LOWTAG);
898 ptrans_otherptr(lispobj thing, lispobj header, boolean constant)
900 switch (widetag_of(header)) {
902 case SINGLE_FLOAT_WIDETAG:
903 case DOUBLE_FLOAT_WIDETAG:
904 #ifdef LONG_FLOAT_WIDETAG
905 case LONG_FLOAT_WIDETAG:
907 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
908 case COMPLEX_SINGLE_FLOAT_WIDETAG:
910 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
911 case COMPLEX_DOUBLE_FLOAT_WIDETAG:
913 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
914 case COMPLEX_LONG_FLOAT_WIDETAG:
917 return ptrans_unboxed(thing, header);
920 case COMPLEX_WIDETAG:
921 case SIMPLE_ARRAY_WIDETAG:
922 case COMPLEX_STRING_WIDETAG:
923 case COMPLEX_VECTOR_WIDETAG:
924 case COMPLEX_ARRAY_WIDETAG:
925 return ptrans_boxed(thing, header, constant);
927 case VALUE_CELL_HEADER_WIDETAG:
928 case WEAK_POINTER_WIDETAG:
929 return ptrans_boxed(thing, header, 0);
931 case SYMBOL_HEADER_WIDETAG:
932 return ptrans_boxed(thing, header, 0);
934 case SIMPLE_STRING_WIDETAG:
935 return ptrans_vector(thing, 8, 1, 0, constant);
937 case SIMPLE_BIT_VECTOR_WIDETAG:
938 return ptrans_vector(thing, 1, 0, 0, constant);
940 case SIMPLE_VECTOR_WIDETAG:
941 return ptrans_vector(thing, 32, 0, 1, constant);
943 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
944 return ptrans_vector(thing, 2, 0, 0, constant);
946 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
947 return ptrans_vector(thing, 4, 0, 0, constant);
949 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
950 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
951 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
953 return ptrans_vector(thing, 8, 0, 0, constant);
955 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
956 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
957 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
959 return ptrans_vector(thing, 16, 0, 0, constant);
961 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
962 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
963 case SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG:
965 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
966 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
968 return ptrans_vector(thing, 32, 0, 0, constant);
970 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
971 return ptrans_vector(thing, 32, 0, 0, constant);
973 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
974 return ptrans_vector(thing, 64, 0, 0, constant);
976 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
977 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
979 return ptrans_vector(thing, 96, 0, 0, constant);
982 return ptrans_vector(thing, 128, 0, 0, constant);
986 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
987 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
988 return ptrans_vector(thing, 64, 0, 0, constant);
991 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
992 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
993 return ptrans_vector(thing, 128, 0, 0, constant);
996 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
997 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
999 return ptrans_vector(thing, 192, 0, 0, constant);
1002 return ptrans_vector(thing, 256, 0, 0, constant);
1006 case CODE_HEADER_WIDETAG:
1007 return ptrans_code(thing);
1009 case RETURN_PC_HEADER_WIDETAG:
1010 return ptrans_returnpc(thing, header);
1013 return ptrans_fdefn(thing, header);
1016 /* Should only come across other pointers to the above stuff. */
1023 pscav_fdefn(struct fdefn *fdefn)
1027 fix_func = ((char *)(fdefn->fun+FUN_RAW_ADDR_OFFSET) == fdefn->raw_addr);
1028 pscav(&fdefn->name, 1, 1);
1029 pscav(&fdefn->fun, 1, 0);
1031 fdefn->raw_addr = (char *)(fdefn->fun + FUN_RAW_ADDR_OFFSET);
1032 return sizeof(struct fdefn) / sizeof(lispobj);
1036 /* now putting code objects in static space */
1038 pscav_code(struct code*code)
1042 nwords = HeaderValue(code->header) + fixnum_value(code->code_size);
1044 /* Arrange to scavenge the debug info later. */
1045 pscav_later(&code->debug_info, 1);
1047 /* Scavenge the constants. */
1048 pscav(code->constants, HeaderValue(code->header)-5, 1);
1050 /* Scavenge all the functions. */
1051 pscav(&code->entry_points, 1, 1);
1052 for (func = code->entry_points;
1054 func = ((struct simple_fun *)native_pointer(func))->next) {
1055 gc_assert(lowtag_of(func) == FUN_POINTER_LOWTAG);
1056 gc_assert(!dynamic_pointer_p(func));
1059 /* Temporarly convert the self pointer to a real function
1061 ((struct simple_fun *)native_pointer(func))->self
1062 -= FUN_RAW_ADDR_OFFSET;
1064 pscav(&((struct simple_fun *)native_pointer(func))->self, 2, 1);
1066 ((struct simple_fun *)native_pointer(func))->self
1067 += FUN_RAW_ADDR_OFFSET;
1069 pscav_later(&((struct simple_fun *)native_pointer(func))->name, 3);
1072 return CEILING(nwords,2);
1077 pscav(lispobj *addr, int nwords, boolean constant)
1079 lispobj thing, *thingp, header;
1080 int count = 0; /* (0 = dummy init value to stop GCC warning) */
1081 struct vector *vector;
1083 while (nwords > 0) {
1085 if (is_lisp_pointer(thing)) {
1086 /* It's a pointer. Is it something we might have to move? */
1087 if (dynamic_pointer_p(thing)) {
1088 /* Maybe. Have we already moved it? */
1089 thingp = (lispobj *)native_pointer(thing);
1091 if (is_lisp_pointer(header) && forwarding_pointer_p(header))
1092 /* Yep, so just copy the forwarding pointer. */
1095 /* Nope, copy the object. */
1096 switch (lowtag_of(thing)) {
1097 case FUN_POINTER_LOWTAG:
1098 thing = ptrans_func(thing, header);
1101 case LIST_POINTER_LOWTAG:
1102 thing = ptrans_list(thing, constant);
1105 case INSTANCE_POINTER_LOWTAG:
1106 thing = ptrans_instance(thing, header, constant);
1109 case OTHER_POINTER_LOWTAG:
1110 thing = ptrans_otherptr(thing, header, constant);
1114 /* It was a pointer, but not one of them? */
1122 else if (thing & 3) {
1123 /* It's an other immediate. Maybe the header for an unboxed */
1125 switch (widetag_of(thing)) {
1126 case BIGNUM_WIDETAG:
1127 case SINGLE_FLOAT_WIDETAG:
1128 case DOUBLE_FLOAT_WIDETAG:
1129 #ifdef LONG_FLOAT_WIDETAG
1130 case LONG_FLOAT_WIDETAG:
1133 /* It's an unboxed simple object. */
1134 count = HeaderValue(thing)+1;
1137 case SIMPLE_VECTOR_WIDETAG:
1138 if (HeaderValue(thing) == subtype_VectorValidHashing) {
1139 *addr = (subtype_VectorMustRehash << N_WIDETAG_BITS) |
1140 SIMPLE_VECTOR_WIDETAG;
1145 case SIMPLE_STRING_WIDETAG:
1146 vector = (struct vector *)addr;
1147 count = CEILING(NWORDS(fixnum_value(vector->length)+1,4)+2,2);
1150 case SIMPLE_BIT_VECTOR_WIDETAG:
1151 vector = (struct vector *)addr;
1152 count = CEILING(NWORDS(fixnum_value(vector->length),32)+2,2);
1155 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
1156 vector = (struct vector *)addr;
1157 count = CEILING(NWORDS(fixnum_value(vector->length),16)+2,2);
1160 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
1161 vector = (struct vector *)addr;
1162 count = CEILING(NWORDS(fixnum_value(vector->length),8)+2,2);
1165 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
1166 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
1167 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
1169 vector = (struct vector *)addr;
1170 count = CEILING(NWORDS(fixnum_value(vector->length),4)+2,2);
1173 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
1174 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
1175 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
1177 vector = (struct vector *)addr;
1178 count = CEILING(NWORDS(fixnum_value(vector->length),2)+2,2);
1181 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
1182 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
1183 case SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG:
1185 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
1186 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
1188 vector = (struct vector *)addr;
1189 count = CEILING(fixnum_value(vector->length)+2,2);
1192 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
1193 vector = (struct vector *)addr;
1194 count = CEILING(fixnum_value(vector->length)+2,2);
1197 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
1198 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
1199 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
1201 vector = (struct vector *)addr;
1202 count = fixnum_value(vector->length)*2+2;
1205 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
1206 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
1207 vector = (struct vector *)addr;
1209 count = fixnum_value(vector->length)*3+2;
1212 count = fixnum_value(vector->length)*4+2;
1217 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
1218 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
1219 vector = (struct vector *)addr;
1220 count = fixnum_value(vector->length)*4+2;
1224 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
1225 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
1226 vector = (struct vector *)addr;
1228 count = fixnum_value(vector->length)*6+2;
1231 count = fixnum_value(vector->length)*8+2;
1236 case CODE_HEADER_WIDETAG:
1238 gc_abort(); /* no code headers in static space */
1240 count = pscav_code((struct code*)addr);
1244 case SIMPLE_FUN_HEADER_WIDETAG:
1245 case CLOSURE_FUN_HEADER_WIDETAG:
1246 case RETURN_PC_HEADER_WIDETAG:
1247 /* We should never hit any of these, 'cause they occur
1248 * buried in the middle of code objects. */
1253 case CLOSURE_HEADER_WIDETAG:
1254 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
1255 /* The function self pointer needs special care on the
1256 * x86 because it is the real entry point. */
1258 lispobj fun = ((struct closure *)addr)->fun
1259 - FUN_RAW_ADDR_OFFSET;
1260 pscav(&fun, 1, constant);
1261 ((struct closure *)addr)->fun = fun + FUN_RAW_ADDR_OFFSET;
1267 case WEAK_POINTER_WIDETAG:
1268 /* Weak pointers get preserved during purify, 'cause I
1269 * don't feel like figuring out how to break them. */
1270 pscav(addr+1, 2, constant);
1275 /* We have to handle fdefn objects specially, so we
1276 * can fix up the raw function address. */
1277 count = pscav_fdefn((struct fdefn *)addr);
1286 /* It's a fixnum. */
1298 purify(lispobj static_roots, lispobj read_only_roots)
1302 struct later *laters, *next;
1306 printf("[doing purification:");
1309 #ifdef LISP_FEATURE_GENCGC
1310 gc_alloc_update_all_page_tables();
1312 if (fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX)) != 0) {
1313 /* FIXME: 1. What does this mean? 2. It shouldn't be reporting
1314 * its error simply by a. printing a string b. to stdout instead
1316 printf(" Ack! Can't purify interrupt contexts. ");
1321 #if defined(__i386__)
1322 dynamic_space_free_pointer =
1323 (lispobj*)SymbolValue(ALLOCATION_POINTER);
1326 read_only_end = read_only_free =
1327 (lispobj *)SymbolValue(READ_ONLY_SPACE_FREE_POINTER);
1328 static_end = static_free =
1329 (lispobj *)SymbolValue(STATIC_SPACE_FREE_POINTER);
1336 #if (defined(LISP_FEATURE_GENCGC) && defined(LISP_FEATURE_X86))
1337 gc_assert((lispobj *)CONTROL_STACK_END > ((&read_only_roots)+1));
1338 setup_i386_stack_scav(((&static_roots)-2), (lispobj *)CONTROL_STACK_END);
1341 pscav(&static_roots, 1, 0);
1342 pscav(&read_only_roots, 1, 1);
1345 printf(" handlers");
1348 pscav((lispobj *) interrupt_handlers,
1349 sizeof(interrupt_handlers) / sizeof(lispobj),
1357 pscav((lispobj *)CONTROL_STACK_START,
1358 current_control_stack_pointer - (lispobj *)CONTROL_STACK_START,
1361 #ifdef LISP_FEATURE_GENCGC
1367 printf(" bindings");
1370 #if !defined(__i386__)
1371 pscav( (lispobj *)BINDING_STACK_START,
1372 (lispobj *)current_binding_stack_pointer - (lispobj *)BINDING_STACK_START,
1375 pscav( (lispobj *)BINDING_STACK_START,
1376 (lispobj *)SymbolValue(BINDING_STACK_POINTER) -
1377 (lispobj *)BINDING_STACK_START,
1381 /* The original CMU CL code had scavenge-read-only-space code
1382 * controlled by the Lisp-level variable
1383 * *SCAVENGE-READ-ONLY-SPACE*. It was disabled by default, and it
1384 * wasn't documented under what circumstances it was useful or
1385 * safe to turn it on, so it's been turned off in SBCL. If you
1386 * want/need this functionality, and can test and document it,
1387 * please submit a patch. */
1389 if (SymbolValue(SCAVENGE_READ_ONLY_SPACE) != UNBOUND_MARKER_WIDETAG
1390 && SymbolValue(SCAVENGE_READ_ONLY_SPACE) != NIL) {
1391 unsigned read_only_space_size =
1392 (lispobj *)SymbolValue(READ_ONLY_SPACE_FREE_POINTER) -
1393 (lispobj *)READ_ONLY_SPACE_START;
1395 "scavenging read only space: %d bytes\n",
1396 read_only_space_size * sizeof(lispobj));
1397 pscav( (lispobj *)READ_ONLY_SPACE_START, read_only_space_size, 0);
1405 clean = (lispobj *)STATIC_SPACE_START;
1407 while (clean != static_free)
1408 clean = pscav(clean, static_free - clean, 0);
1409 laters = later_blocks;
1410 count = later_count;
1411 later_blocks = NULL;
1413 while (laters != NULL) {
1414 for (i = 0; i < count; i++) {
1415 if (laters->u[i].count == 0) {
1417 } else if (laters->u[i].count <= LATERMAXCOUNT) {
1418 pscav(laters->u[i+1].ptr, laters->u[i].count, 1);
1421 pscav(laters->u[i].ptr, 1, 1);
1424 next = laters->next;
1427 count = LATERBLOCKSIZE;
1429 } while (clean != static_free || later_blocks != NULL);
1436 os_zero((os_vm_address_t) current_dynamic_space,
1437 (os_vm_size_t) DYNAMIC_SPACE_SIZE);
1439 /* Zero the stack. Note that the stack is also zeroed by SUB-GC
1440 * calling SCRUB-CONTROL-STACK - this zeros the stack on the x86. */
1442 os_zero((os_vm_address_t) current_control_stack_pointer,
1443 (os_vm_size_t) (CONTROL_STACK_SIZE -
1444 ((current_control_stack_pointer -
1445 (lispobj *)CONTROL_STACK_START) *
1449 /* It helps to update the heap free pointers so that free_heap can
1450 * verify after it's done. */
1451 SetSymbolValue(READ_ONLY_SPACE_FREE_POINTER, (lispobj)read_only_free);
1452 SetSymbolValue(STATIC_SPACE_FREE_POINTER, (lispobj)static_free);
1454 #if !defined(__i386__)
1455 dynamic_space_free_pointer = current_dynamic_space;
1457 #if defined LISP_FEATURE_GENCGC
1460 #error unsupported case /* in CMU CL, was "ibmrt using GC" */