3 This code was written as part of the CMU Common Lisp project at
4 Carnegie Mellon University, and has been placed in the public domain.
16 #include "internals.h"
28 #ifdef FEATURE_EXECUTABLE
30 #if !(defined(DARWIN) && defined(__ppc__))
38 write_bytes(FILE * file, char *addr, long bytes)
40 long count, here, data;
42 bytes = (bytes + CORE_PAGESIZE - 1) & ~(CORE_PAGESIZE - 1);
47 data = (ftell(file) + CORE_PAGESIZE - 1) & ~(CORE_PAGESIZE - 1);
51 count = fwrite(addr, 1, bytes, file);
56 perror("Error writing to save file");
62 return data / CORE_PAGESIZE - 1;
66 output_space(FILE * file, int id, lispobj * addr, lispobj * end)
68 int words, bytes, data;
69 static char *names[] = { NULL, "Dynamic", "Static", "Read-Only" };
75 bytes = words * sizeof(lispobj);
77 printf("Writing %d bytes from the %s space at 0x%08lX.\n",
78 bytes, names[id], (unsigned long) addr);
80 data = write_bytes(file, (char *) addr, bytes);
83 putw((long) addr / CORE_PAGESIZE, file);
84 putw((bytes + CORE_PAGESIZE - 1) / CORE_PAGESIZE, file);
89 dump_region(struct alloc_region *alloc_region)
91 fprintf(stderr, "free_pointer = %p\n", alloc_region->free_pointer);
92 fprintf(stderr, "end_addr = %p\n", alloc_region->end_addr);
93 fprintf(stderr, "first_page = %d\n", alloc_region->first_page);
94 fprintf(stderr, "last_page = %d\n", alloc_region->last_page);
95 fprintf(stderr, "start_addr = %p\n", alloc_region->start_addr);
97 fprintf(stderr, " page_table[%d]\n", alloc_region->first_page);
98 fprintf(stderr, " flags = %x\n", page_table[alloc_region->first_page].flags);
99 fprintf(stderr, " offset = %x\n", page_table[alloc_region->first_page].first_object_offset);
100 fprintf(stderr, " used = %x\n", page_table[alloc_region->first_page].bytes_used);
105 save(char *filename, lispobj init_function, int sse2_mode)
110 volatile lispobj *func_ptr = &init_function;
113 strcpy(sbuf, filename);
115 /* Get rid of remnant stuff. This is a MUST so that
116 * the memory manager can get started correctly when
117 * we restart after this save. Purify is going to
118 * maybe move the args so we need to consider them volatile,
119 * especially if the gcc optimizer is working!!
123 init_function = *func_ptr;
124 /* Set dynamic space pointer to base value so we don't write out
125 * MBs of just cleared heap.
127 if (SymbolValue(X86_CGC_ACTIVE_P) != NIL)
128 SetSymbolValue(ALLOCATION_POINTER, DYNAMIC_0_SPACE_START);
132 file = fopen(filename, "w");
137 printf("[Undoing binding stack... ");
139 unbind_to_here((lispobj *) BINDING_STACK_START);
140 SetSymbolValue(CURRENT_CATCH_BLOCK, 0);
141 SetSymbolValue(CURRENT_UNWIND_PROTECT_BLOCK, 0);
142 SetSymbolValue(EVAL_STACK_TOP, 0);
144 #if defined WANT_CGC && defined X86_CGC_ACTIVE_P
145 SetSymbolValue(X86_CGC_ACTIVE_P, T);
147 printf("[Saving current lisp image into %s:\n", filename);
149 putw(CORE_MAGIC, file);
151 putw(CORE_VERSION, file);
152 #if defined(i386) && defined(FEATURE_SSE2)
159 #if defined(i386) && defined(FEATURE_SSE2)
167 putw(CORE_NDIRECTORY, file);
168 putw((5 * 3) + 2, file);
170 output_space(file, READ_ONLY_SPACE_ID, read_only_space,
171 (lispobj *) SymbolValue(READ_ONLY_SPACE_FREE_POINTER));
172 output_space(file, STATIC_SPACE_ID, static_space,
173 (lispobj *) SymbolValue(STATIC_SPACE_FREE_POINTER));
175 /* Flush the current_region updating the tables. */
176 #ifdef DEBUG_BAD_HEAP
177 fprintf(stderr, "before ALLOC_POINTER = %p\n", (lispobj *) SymbolValue(ALLOCATION_POINTER));
178 dump_region(&boxed_region);
180 gc_alloc_update_page_tables(0, &boxed_region);
181 gc_alloc_update_page_tables(1, &unboxed_region);
182 #ifdef DEBUG_BAD_HEAP
183 fprintf(stderr, "boxed_region after update\n");
184 dump_region(&boxed_region);
186 print_ptr((lispobj*) 0x2805a184);
189 #ifdef DEBUG_BAD_HEAP
191 * For some reason x86 has a heap corruption problem. I (rtoy)
192 * have not been able to figure out how that occurs, but what is
193 * happening is that when a core is loaded, there is some static
194 * object pointing to an object that is on a free page. In normal
195 * usage, at startup there should be 4 objects in static space
196 * pointing to a free page, because these are newly allocated
197 * objects created by the C runtime. However, there is an
200 * I do not know what this object should be or how it got there,
201 * but it will often cause CMUCL to fail to save a new core file.
203 * Disabling this call to update_dynamic_space_free_pointer is a
204 * work around. What is happening is that u_d_s_f_p is resetting
205 * ALLOCATION_POINTER, but that weird object is in the current
206 * region, but after resetting the pointer, that object isn't
207 * saved to the core file. By not resetting the pointer, the
208 * object (or at least enough of it) gets saved in the core file
209 * that we don't have problems when reloading.
211 * Note that on sparc and ppc, u_d_s_f_p doesn't actually do
212 * anything because the call to reset ALLOCATION_POINTER is a nop
213 * on sparc and ppc. And sparc and ppc dont' have the heap
214 * corruption issue. That's not conclusive evidence, though.
216 * This needs more work and investigation.
218 update_dynamic_space_free_pointer();
221 #ifdef DEBUG_BAD_HEAP
222 fprintf(stderr, "after ALLOC_POINTER = %p\n", (lispobj *) SymbolValue(ALLOCATION_POINTER));
227 output_space(file, DYNAMIC_SPACE_ID, current_dynamic_space,
228 current_dynamic_space_free_pointer);
230 output_space(file, DYNAMIC_SPACE_ID, current_dynamic_space,
231 (lispobj *) SymbolValue(ALLOCATION_POINTER));
234 putw(CORE_INITIAL_FUNCTION, file);
236 putw(init_function, file);
238 putw(CORE_END, file);
247 #ifdef FEATURE_EXECUTABLE
249 save_executable(char *filename, lispobj init_function)
254 volatile lispobj *func_ptr = &init_function;
257 strcpy(sbuf, filename);
259 /* Get rid of remnant stuff. This is a MUST so that
260 * the memory manager can get started correctly when
261 * we restart after this save. Purify is going to
262 * maybe move the args so we need to consider them volatile,
263 * especially if the gcc optimizer is working!!
267 init_function = *func_ptr;
268 /* Set dynamic space pointer to base value so we don't write out
269 * MBs of just cleared heap.
271 if(SymbolValue(X86_CGC_ACTIVE_P) != NIL)
272 SetSymbolValue(ALLOCATION_POINTER, DYNAMIC_0_SPACE_START);
274 dir_name = dirname(strdup(filename));
276 printf("[Undoing binding stack... ");
278 unbind_to_here((lispobj *)BINDING_STACK_START);
279 SetSymbolValue(CURRENT_CATCH_BLOCK, 0);
280 SetSymbolValue(CURRENT_UNWIND_PROTECT_BLOCK, 0);
281 SetSymbolValue(EVAL_STACK_TOP, 0);
283 #if defined WANT_CGC && defined X86_CGC_ACTIVE_P
284 SetSymbolValue(X86_CGC_ACTIVE_P, T);
286 printf("[Saving current lisp image as executable into \"%s\":\n", filename);
288 printf("\t[Writing core objects\n");
290 write_space_object(dir_name, READ_ONLY_SPACE_ID, (os_vm_address_t)read_only_space,
291 (os_vm_address_t)SymbolValue(READ_ONLY_SPACE_FREE_POINTER));
292 write_space_object(dir_name, STATIC_SPACE_ID, (os_vm_address_t)static_space,
293 (os_vm_address_t)SymbolValue(STATIC_SPACE_FREE_POINTER));
295 /* Flush the current_region updating the tables. */
296 #ifdef DEBUG_BAD_HEAP
297 fprintf(stderr, "before ALLOC_POINTER = %p\n", (lispobj *) SymbolValue(ALLOCATION_POINTER));
298 dump_region(&boxed_region);
300 gc_alloc_update_page_tables(0,&boxed_region);
301 gc_alloc_update_page_tables(1,&unboxed_region);
302 #ifdef DEBUG_BAD_HEAP
303 fprintf(stderr, "boxed_region after update\n");
304 dump_region(&boxed_region);
306 print_ptr((lispobj*) 0x2805a184);
308 #ifdef DEBUG_BAD_HEAP
310 * For some reason x86 has a heap corruption problem. I (rtoy)
311 * have not been able to figure out how that occurs, but what is
312 * happening is that when a core is loaded, there is some static
313 * object pointing to an object that is on a free page. In normal
314 * usage, at startup there should be 4 objects in static space
315 * pointing to a free page, because these are newly allocated
316 * objects created by the C runtime. However, there is an
319 * I do not know what this object should be or how it got there,
320 * but it will often cause CMUCL to fail to save a new core file.
322 * Disabling this call to update_dynamic_space_free_pointer is a
323 * work around. What is happening is that u_d_s_f_p is resetting
324 * ALLOCATION_POINTER, but that weird object is in the current
325 * region, but after resetting the pointer, that object isn't
326 * saved to the core file. By not resetting the pointer, the
327 * object (or at least enough of it) gets saved in the core file
328 * that we don't have problems when reloading.
330 * Note that on sparc and ppc, u_d_s_f_p doesn't actually do
331 * anything because the call to reset ALLOCATION_POINTER is a nop
332 * on sparc and ppc. And sparc and ppc dont' have the heap
333 * corruption issue. That's not conclusive evidence, though.
335 * This needs more work and investigation.
337 update_dynamic_space_free_pointer();
340 #ifdef DEBUG_BAD_HEAP
341 fprintf(stderr, "after ALLOC_POINTER = %p\n", (lispobj *) SymbolValue(ALLOCATION_POINTER));
346 write_space_object(dir_name, DYNAMIC_SPACE_ID, (os_vm_address_t)current_dynamic_space,
347 (os_vm_address_t)current_dynamic_space_free_pointer);
349 write_space_object(dir_name, DYNAMIC_SPACE_ID, (os_vm_address_t)current_dynamic_space,
350 (os_vm_address_t)SymbolValue(ALLOCATION_POINTER));
356 printf("Linking executable...\n");
358 obj_run_linker(init_function, filename);