2 * This software is part of the SBCL system. See the README file for
5 * This software is derived from the CMU CL system, which was
6 * written at Carnegie Mellon University and released into the
7 * public domain. The software is in the public domain and is
8 * provided with absolutely no warranty. See the COPYING and CREDITS
9 * files for more information.
26 #include "gc-internal.h"
29 #include "genesis/static-symbols.h"
30 #include "genesis/symbol.h"
33 write_bytes(FILE *file, char *addr, long bytes)
35 long count, here, data;
37 bytes = (bytes+os_vm_page_size-1)&~(os_vm_page_size-1);
42 data = (ftell(file)+os_vm_page_size-1)&~(os_vm_page_size-1);
46 count = fwrite(addr, 1, bytes, file);
52 perror("error writing to save file");
58 return data/os_vm_page_size - 1;
62 output_space(FILE *file, int id, lispobj *addr, lispobj *end)
64 int words, bytes, data;
65 static char *names[] = {NULL, "dynamic", "static", "read-only"};
71 bytes = words * sizeof(lispobj);
73 printf("writing %d bytes from the %s space at 0x%08lx\n",
74 bytes, names[id], (unsigned long)addr);
76 data = write_bytes(file, (char *)addr, bytes);
79 putw((long)addr / os_vm_page_size, file);
80 putw((bytes + os_vm_page_size - 1) / os_vm_page_size, file);
84 save(char *filename, lispobj init_function)
89 /* Open the output file. We don't actually need the file yet, but
90 * the fopen() might fail for some reason, and we want to detect
91 * that and back out before we do anything irreversible. */
93 file = fopen(filename, "w");
99 /* Smash the enclosing state. (Once we do this, there's no good
100 * way to go back, which is a sufficient reason that this ends up
101 * being SAVE-LISP-AND-DIE instead of SAVE-LISP-AND-GO-ON). */
102 printf("[undoing binding stack and other enclosing state... ");
104 for_each_thread(th) { /* XXX really? */
105 unbind_to_here((lispobj *)th->binding_stack_start,th);
106 SetSymbolValue(CURRENT_CATCH_BLOCK, 0,th);
107 SetSymbolValue(CURRENT_UNWIND_PROTECT_BLOCK, 0,th);
112 /* (Now we can actually start copying ourselves into the output file.) */
114 printf("[saving current Lisp image into %s:\n", filename);
117 putw(CORE_MAGIC, file);
119 putw(VERSION_CORE_ENTRY_TYPE_CODE, file);
121 putw(SBCL_CORE_VERSION_INTEGER, file);
123 putw(BUILD_ID_CORE_ENTRY_TYPE_CODE, file);
124 putw(/* (We're writing the word count of the entry here, and the 2
125 * term is one word for the leading BUILD_ID_CORE_ENTRY_TYPE_CODE
126 * word and one word where we store the count itself.) */
127 2 + strlen(build_id),
131 for (p = build_id; *p; ++p)
135 putw(NEW_DIRECTORY_CORE_ENTRY_TYPE_CODE, file);
136 putw(/* (word count = 3 spaces described by 5 words each, plus the
137 * entry type code, plus this count itself) */
140 READ_ONLY_CORE_SPACE_ID,
141 (lispobj *)READ_ONLY_SPACE_START,
142 (lispobj *)SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0));
144 STATIC_CORE_SPACE_ID,
145 (lispobj *)STATIC_SPACE_START,
146 (lispobj *)SymbolValue(STATIC_SPACE_FREE_POINTER,0));
149 DYNAMIC_CORE_SPACE_ID,
150 (lispobj *)current_dynamic_space,
151 dynamic_space_free_pointer);
153 #ifdef LISP_FEATURE_GENCGC
154 /* Flush the current_region, updating the tables. */
155 gc_alloc_update_all_page_tables();
156 update_x86_dynamic_space_free_pointer();
159 DYNAMIC_CORE_SPACE_ID,
160 (lispobj *)DYNAMIC_SPACE_START,
161 (lispobj *)SymbolValue(ALLOCATION_POINTER,0));
164 putw(INITIAL_FUN_CORE_ENTRY_TYPE_CODE, file);
166 putw(init_function, file);
168 putw(END_CORE_ENTRY_TYPE_CODE, file);