1.0.5.35: stack alignment on x86/Darwin, once more
[sbcl.git] / src / runtime / coreparse.c
blobe7dbdfd153a46f7e757045b5e9ecaa252a1c7f5d
1 /*
2 * A saved SBCL system is a .core file; the code here helps us accept
3 * such a file as input.
4 */
6 /*
7 * This software is part of the SBCL system. See the README file for
8 * more information.
10 * This software is derived from the CMU CL system, which was
11 * written at Carnegie Mellon University and released into the
12 * public domain. The software is in the public domain and is
13 * provided with absolutely no warranty. See the COPYING and CREDITS
14 * files for more information.
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <sys/file.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24 #include <unistd.h>
26 #include "sbcl.h"
27 #include "os.h"
28 #include "runtime.h"
29 #include "globals.h"
30 #include "core.h"
31 #include "arch.h"
32 #include "interr.h"
33 #include "thread.h"
35 #include "validate.h"
36 #include "gc-internal.h"
38 /* lutex stuff */
39 #if defined(LISP_FEATURE_SB_THREAD) && defined(LISP_FEATURE_SB_LUTEX)
40 #include "genesis/sap.h"
41 #endif
44 unsigned char build_id[] =
45 #include "../../output/build-id.tmp"
48 int
49 open_binary(char *filename, int mode)
51 #ifdef LISP_FEATURE_WIN32
52 mode |= O_BINARY;
53 #endif
55 return open(filename, mode);
58 /* Search 'filename' for an embedded core. An SBCL core has, at the
59 * end of the file, a trailer containing the size of the core (an
60 * os_vm_offset_t) and a final signature word (the lispobj
61 * CORE_MAGIC). If this trailer is found at the end of the file, the
62 * start of the core can be determined from the core size.
64 * If an embedded core is present, this returns the offset into the
65 * file to load the core from, or -1 if no core is present. */
66 os_vm_offset_t
67 search_for_embedded_core(char *filename)
69 lispobj header;
70 os_vm_offset_t lispobj_size = sizeof(lispobj);
71 os_vm_offset_t trailer_size = lispobj_size + sizeof(os_vm_offset_t);
72 os_vm_offset_t core_size, pos;
73 int fd = -1;
75 if ((fd = open_binary(filename, O_RDONLY)) < 0)
76 goto lose;
77 if (lseek(fd, -lispobj_size, SEEK_END) < 0)
78 goto lose;
79 if (read(fd, &header, (size_t)lispobj_size) < lispobj_size)
80 goto lose;
82 if (header == CORE_MAGIC) {
83 if (lseek(fd, -trailer_size, SEEK_END) < 0)
84 goto lose;
85 if (read(fd, &core_size, sizeof(os_vm_offset_t)) < 0)
86 goto lose;
88 if (lseek(fd, -(core_size + trailer_size), SEEK_END) < 0)
89 goto lose;
90 pos = lseek(fd, 0, SEEK_CUR);
92 if (read(fd, &header, (size_t)lispobj_size) < lispobj_size)
93 goto lose;
95 if (header != CORE_MAGIC)
96 goto lose;
98 close(fd);
99 return pos;
102 lose:
103 if (fd != -1)
104 close(fd);
106 return -1;
109 static void
110 process_directory(int fd, lispobj *ptr, int count, os_vm_offset_t file_offset)
112 struct ndir_entry *entry;
114 FSHOW((stderr, "/process_directory(..), count=%d\n", count));
116 for (entry = (struct ndir_entry *) ptr; --count>= 0; ++entry) {
118 long id = entry->identifier;
119 long offset = os_vm_page_size * (1 + entry->data_page);
120 os_vm_address_t addr =
121 (os_vm_address_t) (os_vm_page_size * entry->address);
122 lispobj *free_pointer = (lispobj *) addr + entry->nwords;
123 long len = os_vm_page_size * entry->page_count;
125 if (len != 0) {
126 os_vm_address_t real_addr;
127 FSHOW((stderr, "/mapping %ld(0x%lx) bytes at 0x%lx\n",
128 (long)len, (long)len, (unsigned long)addr));
129 real_addr = os_map(fd, offset + file_offset, addr, len);
130 if (real_addr != addr) {
131 lose("file mapped in wrong place! "
132 "(0x%08x != 0x%08lx)\n",
133 real_addr,
134 addr);
138 FSHOW((stderr, "/space id = %ld, free pointer = 0x%lx\n",
139 id, (unsigned long)free_pointer));
141 switch (id) {
142 case DYNAMIC_CORE_SPACE_ID:
143 if (len > dynamic_space_size) {
144 fprintf(stderr,
145 "dynamic space too small for core: %ldKiB required, %ldKiB available.\n",
146 len >> 10,
147 (long)dynamic_space_size >> 10);
148 exit(1);
150 #ifdef LISP_FEATURE_GENCGC
151 if (addr != (os_vm_address_t)DYNAMIC_SPACE_START) {
152 fprintf(stderr, "in core: 0x%lx; in runtime: 0x%lx \n",
153 (long)addr, (long)DYNAMIC_SPACE_START);
154 lose("core/runtime address mismatch: DYNAMIC_SPACE_START\n");
156 #else
157 if ((addr != (os_vm_address_t)DYNAMIC_0_SPACE_START) &&
158 (addr != (os_vm_address_t)DYNAMIC_1_SPACE_START)) {
159 fprintf(stderr, "in core: 0x%lx; in runtime: 0x%lx or 0x%lx\n",
160 (long)addr,
161 (long)DYNAMIC_0_SPACE_START,
162 (long)DYNAMIC_1_SPACE_START);
163 lose("warning: core/runtime address mismatch: DYNAMIC_SPACE_START\n");
165 #endif
166 #if defined(ALLOCATION_POINTER)
167 SetSymbolValue(ALLOCATION_POINTER, (lispobj)free_pointer,0);
168 #else
169 dynamic_space_free_pointer = free_pointer;
170 #endif
171 /* For stop-and-copy GC, this will be whatever the GC was
172 * using at the time. With GENCGC, this will always be
173 * space 0. (We checked above that for GENCGC,
174 * addr==DYNAMIC_SPACE_START.) */
175 current_dynamic_space = (lispobj *)addr;
176 break;
177 case STATIC_CORE_SPACE_ID:
178 if (addr != (os_vm_address_t)STATIC_SPACE_START) {
179 fprintf(stderr, "in core: 0x%lx - in runtime: 0x%lx\n",
180 (long)addr, (long)STATIC_SPACE_START);
181 lose("core/runtime address mismatch: STATIC_SPACE_START\n");
183 break;
184 case READ_ONLY_CORE_SPACE_ID:
185 if (addr != (os_vm_address_t)READ_ONLY_SPACE_START) {
186 fprintf(stderr, "in core: 0x%lx - in runtime: 0x%lx\n",
187 (long)addr, (long)READ_ONLY_SPACE_START);
188 lose("core/runtime address mismatch: READ_ONLY_SPACE_START\n");
190 break;
191 default:
192 lose("unknown space ID %ld addr 0x%lx\n", id, (long)addr);
197 lispobj
198 load_core_file(char *file, os_vm_offset_t file_offset)
200 lispobj *header, val, len, *ptr, remaining_len;
201 int fd = open_binary(file, O_RDONLY), count;
203 lispobj initial_function = NIL;
204 FSHOW((stderr, "/entering load_core_file(%s)\n", file));
205 if (fd < 0) {
206 fprintf(stderr, "could not open file \"%s\"\n", file);
207 perror("open");
208 exit(1);
211 lseek(fd, file_offset, SEEK_SET);
212 header = calloc(os_vm_page_size / sizeof(u32), sizeof(u32));
214 count = read(fd, header, os_vm_page_size);
215 if (count < os_vm_page_size) {
216 lose("premature end of core file\n");
218 SHOW("successfully read first page of core");
220 ptr = header;
221 val = *ptr++;
223 if (val != CORE_MAGIC) {
224 lose("invalid magic number in core: 0x%lx should have been 0x%x.\n",
225 val,
226 CORE_MAGIC);
228 SHOW("found CORE_MAGIC");
230 while (val != END_CORE_ENTRY_TYPE_CODE) {
231 val = *ptr++;
232 len = *ptr++;
233 remaining_len = len - 2; /* (-2 to cancel the two ++ operations) */
234 FSHOW((stderr, "/val=0x%ld, remaining_len=0x%ld\n",
235 (long)val, (long)remaining_len));
237 switch (val) {
239 case END_CORE_ENTRY_TYPE_CODE:
240 SHOW("END_CORE_ENTRY_TYPE_CODE case");
241 break;
243 case VERSION_CORE_ENTRY_TYPE_CODE:
244 SHOW("VERSION_CORE_ENTRY_TYPE_CODE case");
245 if (*ptr != SBCL_CORE_VERSION_INTEGER) {
246 lose("core file version (%d) != runtime library version (%d)\n",
247 *ptr,
248 SBCL_CORE_VERSION_INTEGER);
250 break;
252 case BUILD_ID_CORE_ENTRY_TYPE_CODE:
253 SHOW("BUILD_ID_CORE_ENTRY_TYPE_CODE case");
255 int i;
257 FSHOW((stderr, "build_id[]=\"%s\"\n", build_id));
258 FSHOW((stderr, "remaining_len = %d\n", remaining_len));
259 if (remaining_len != strlen((const char *)build_id))
260 goto losing_build_id;
261 for (i = 0; i < remaining_len; ++i) {
262 FSHOW((stderr, "ptr[%d] = char = %d, expected=%d\n",
263 i, ptr[i], build_id[i]));
264 if (ptr[i] != build_id[i])
265 goto losing_build_id;
267 break;
268 losing_build_id:
269 /* .core files are not binary-compatible between
270 * builds because we can't easily detect whether the
271 * sources were patched between the time the
272 * dumping-the-.core runtime was built and the time
273 * that the loading-the-.core runtime was built.
275 * (We could easily detect whether version.lisp-expr
276 * was changed, but people experimenting with patches
277 * don't necessarily update version.lisp-expr.) */
279 lose("can't load .core for different runtime, sorry\n");
282 case NEW_DIRECTORY_CORE_ENTRY_TYPE_CODE:
283 SHOW("NEW_DIRECTORY_CORE_ENTRY_TYPE_CODE case");
284 process_directory(fd,
285 ptr,
286 #ifndef LISP_FEATURE_ALPHA
287 remaining_len / (sizeof(struct ndir_entry) /
288 sizeof(long)),
289 #else
290 remaining_len / (sizeof(struct ndir_entry) /
291 sizeof(u32)),
292 #endif
293 file_offset);
294 break;
296 case INITIAL_FUN_CORE_ENTRY_TYPE_CODE:
297 SHOW("INITIAL_FUN_CORE_ENTRY_TYPE_CODE case");
298 initial_function = (lispobj)*ptr;
299 break;
301 #if defined(LISP_FEATURE_SB_THREAD) && defined(LISP_FEATURE_SB_LUTEX)
302 case LUTEX_TABLE_CORE_ENTRY_TYPE_CODE:
303 SHOW("LUTEX_TABLE_CORE_ENTRY_TYPE_CODE case");
305 size_t n_lutexes = *ptr;
306 size_t fdoffset = (*(ptr + 1) + 1) * (os_vm_page_size);
307 size_t data_length = n_lutexes * sizeof(struct sap *);
308 struct lutex **lutexes_to_resurrect = malloc(data_length);
309 long bytes_read;
311 lseek(fd, fdoffset + file_offset, SEEK_SET);
313 FSHOW((stderr, "attempting to read %ld lutexes from core\n", n_lutexes));
314 bytes_read = read(fd, lutexes_to_resurrect, data_length);
316 /* XXX */
317 if (bytes_read != data_length) {
318 lose("Could not read the lutex table");
320 else {
321 int i;
323 for (i=0; i<n_lutexes; ++i) {
324 struct lutex *lutex = lutexes_to_resurrect[i];
326 FSHOW((stderr, "re-init'ing lutex @ %p\n", lutex));
327 lutex_init(lutex);
330 free(lutexes_to_resurrect);
332 break;
334 #endif
336 #ifdef LISP_FEATURE_GENCGC
337 case PAGE_TABLE_CORE_ENTRY_TYPE_CODE:
339 size_t size = *ptr;
340 size_t fdoffset = (*(ptr+1) + 1) * (os_vm_page_size);
341 size_t offset = 0;
342 long bytes_read;
343 long data[4096];
344 lseek(fd, fdoffset + file_offset, SEEK_SET);
345 while ((bytes_read = read(fd, data, (size < 4096 ? size : 4096 )))
346 > 0)
348 int i = 0;
349 size -= bytes_read;
350 while (bytes_read) {
351 bytes_read -= sizeof(long);
352 /* Ignore all zeroes. The size of the page table
353 * core entry was rounded up to os_vm_page_size
354 * during the save, and might now have more
355 * elements than the page table.
357 if (data[i]) {
358 page_table[offset].first_object_offset = data[i];
360 i++;
361 offset++;
365 gencgc_partial_pickup = 1;
366 break;
368 #endif
369 default:
370 lose("unknown core file entry: %ld\n", (long)val);
373 ptr += remaining_len;
374 FSHOW((stderr, "/new ptr=%lx\n", (unsigned long)ptr));
376 SHOW("about to free(header)");
377 free(header);
378 SHOW("returning from load_core_file(..)");
379 return initial_function;