2 * A saved SBCL system is a .core file; the code here helps us accept
3 * such a file as input.
7 * This software is part of the SBCL system. See the README file for
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.
21 #include <sys/types.h>
28 #ifndef LISP_FEATURE_WIN32
29 #ifdef LISP_FEATURE_LINUX
45 #include "gc-internal.h"
48 #if defined(LISP_FEATURE_SB_THREAD) && defined(LISP_FEATURE_SB_LUTEX)
49 #include "genesis/sap.h"
50 #include "pthread-lutex.h"
55 #ifdef LISP_FEATURE_SB_CORE_COMPRESSION
59 unsigned char build_id[] =
60 #include "../../output/build-id.tmp"
64 open_binary(char *filename, int mode)
66 #ifdef LISP_FEATURE_WIN32
70 return open(filename, mode);
74 static struct runtime_options *
75 read_runtime_options(int fd)
77 size_t optarray[RUNTIME_OPTIONS_WORDS];
78 struct runtime_options *options = NULL;
80 if (read(fd, optarray, RUNTIME_OPTIONS_WORDS * sizeof(size_t)) !=
81 RUNTIME_OPTIONS_WORDS * sizeof(size_t)) {
85 if ((RUNTIME_OPTIONS_MAGIC != optarray[0]) || (0 == optarray[1])) {
89 options = successful_malloc(sizeof(struct runtime_options));
91 options->dynamic_space_size = optarray[2];
92 options->thread_control_stack_size = optarray[3];
98 maybe_initialize_runtime_options(int fd)
100 struct runtime_options *new_runtime_options;
101 off_t end_offset = sizeof(lispobj) +
102 sizeof(os_vm_offset_t) +
103 (RUNTIME_OPTIONS_WORDS * sizeof(size_t));
105 lseek(fd, -end_offset, SEEK_END);
107 if ((new_runtime_options = read_runtime_options(fd))) {
108 runtime_options = new_runtime_options;
112 /* Search 'filename' for an embedded core. An SBCL core has, at the
113 * end of the file, a trailer containing optional saved runtime
114 * options, the start of the core (an os_vm_offset_t), and a final
115 * signature word (the lispobj CORE_MAGIC). If this trailer is found
116 * at the end of the file, the start of the core can be determined
117 * from the core size.
119 * If an embedded core is present, this returns the offset into the
120 * file to load the core from, or -1 if no core is present. */
122 search_for_embedded_core(char *filename)
125 os_vm_offset_t lispobj_size = sizeof(lispobj);
126 os_vm_offset_t trailer_size = lispobj_size + sizeof(os_vm_offset_t);
127 os_vm_offset_t core_start, pos;
130 if ((fd = open_binary(filename, O_RDONLY)) < 0)
133 if (read(fd, &header, (size_t)lispobj_size) < lispobj_size)
135 if (header == CORE_MAGIC) {
136 /* This file is a real core, not an embedded core. Return 0 to
137 * indicate where the core starts, and do not look for runtime
138 * options in this case. */
142 if (lseek(fd, -lispobj_size, SEEK_END) < 0)
144 if (read(fd, &header, (size_t)lispobj_size) < lispobj_size)
147 if (header == CORE_MAGIC) {
148 if (lseek(fd, -trailer_size, SEEK_END) < 0)
150 if (read(fd, &core_start, sizeof(os_vm_offset_t)) < 0)
153 if (lseek(fd, core_start, SEEK_SET) < 0)
155 pos = lseek(fd, 0, SEEK_CUR);
157 if (read(fd, &header, (size_t)lispobj_size) < lispobj_size)
160 if (header != CORE_MAGIC)
163 maybe_initialize_runtime_options(fd);
176 /* If more platforms doesn't support overlapping mmap rename this
177 * def to something like ifdef nommapoverlap */
178 /* currently hpux only */
179 #ifdef LISP_FEATURE_HPUX
180 os_vm_address_t copy_core_bytes(int fd, os_vm_offset_t offset,
181 os_vm_address_t addr, int len)
183 unsigned char buf[4096];
185 int old_fd = lseek(fd, 0, SEEK_CUR);
188 fprintf(stderr, "cant copy a slice of core because slice-length is not of page size(4096)\n");
192 fprintf(stderr, "cant perform lseek() on corefile\n");
194 lseek(fd, offset, SEEK_SET);
196 fprintf(stderr, "cant perform lseek(%u,%lu,SEEK_SET) on corefile\n", fd, offset);
198 for(x = 0; x < len; x += 4096){
199 c = read(fd, buf, 4096);
201 fprintf(stderr, "cant read memory area from corefile at position %lu, got %d\n", offset + x, c);
204 memcpy(addr+x, buf, 4096);
206 os_flush_icache(addr, len);
211 #ifdef LISP_FEATURE_SB_CORE_COMPRESSION
212 # define ZLIB_BUFFER_SIZE (1u<<16)
213 os_vm_address_t inflate_core_bytes(int fd, os_vm_offset_t offset,
214 os_vm_address_t addr, int len)
217 unsigned char buf[ZLIB_BUFFER_SIZE];
220 if (-1 == lseek(fd, offset, SEEK_SET)) {
221 lose("Unable to lseek() on corefile\n");
224 stream.zalloc = NULL;
226 stream.opaque = NULL;
228 stream.next_in = buf;
230 ret = inflateInit(&stream);
232 lose("zlib error %i\n", ret);
234 stream.next_out = (void*)addr;
235 stream.avail_out = len;
237 ssize_t count = read(fd, buf, sizeof(buf));
239 lose("unable to read core file (errno = %i)\n", errno);
240 stream.next_in = buf;
241 stream.avail_in = count;
242 if (count == 0) break;
243 ret = inflate(&stream, Z_NO_FLUSH);
248 if (stream.avail_out == 0)
249 lose("Runaway gzipped core directory... aborting\n");
250 if (stream.avail_in > 0)
251 lose("zlib inflate returned without fully"
252 "using up input buffer... aborting\n");
255 lose("zlib inflate error: %i\n", ret);
258 } while (ret != Z_STREAM_END);
260 if (stream.avail_out > 0) {
261 if (stream.avail_out >= os_vm_page_size)
262 fprintf(stderr, "Warning: gzipped core directory significantly"
263 "shorter than expected (%lu bytes)", (unsigned long)stream.avail_out);
264 /* Is this needed? */
265 memset(stream.next_out, 0, stream.avail_out);
271 # undef ZLIB_BUFFER_SIZE
274 int merge_core_pages = -1;
277 process_directory(int fd, lispobj *ptr, int count, os_vm_offset_t file_offset)
279 struct ndir_entry *entry;
282 FSHOW((stderr, "/process_directory(..), count=%d\n", count));
284 for (entry = (struct ndir_entry *) ptr; --count>= 0; ++entry) {
286 long id = entry->identifier;
287 if (id <= (MAX_CORE_SPACE_ID | DEFLATED_CORE_SPACE_ID_FLAG)) {
288 if (id & DEFLATED_CORE_SPACE_ID_FLAG)
290 id &= ~(DEFLATED_CORE_SPACE_ID_FLAG);
292 long offset = os_vm_page_size * (1 + entry->data_page);
293 os_vm_address_t addr =
294 (os_vm_address_t) (os_vm_page_size * entry->address);
295 lispobj *free_pointer = (lispobj *) addr + entry->nwords;
296 unsigned long len = os_vm_page_size * entry->page_count;
298 os_vm_address_t real_addr;
299 FSHOW((stderr, "/mapping %ld(0x%lx) bytes at 0x%lx\n",
300 (long)len, (long)len, (unsigned long)addr));
302 #ifdef LISP_FEATURE_SB_CORE_COMPRESSION
303 real_addr = inflate_core_bytes(fd, offset + file_offset, addr, len);
305 lose("This runtime was not built with zlib-compressed core support... aborting\n");
308 #ifdef LISP_FEATURE_HPUX
309 real_addr = copy_core_bytes(fd, offset + file_offset, addr, len);
311 real_addr = os_map(fd, offset + file_offset, addr, len);
314 if (real_addr != addr) {
315 lose("file mapped in wrong place! "
316 "(0x%08x != 0x%08lx)\n",
322 #ifdef MADV_MERGEABLE
323 if ((merge_core_pages == 1)
324 || ((merge_core_pages == -1) && compressed)) {
325 madvise(addr, len, MADV_MERGEABLE);
329 FSHOW((stderr, "/space id = %ld, free pointer = 0x%lx\n",
330 id, (unsigned long)free_pointer));
333 case DYNAMIC_CORE_SPACE_ID:
334 if (len > dynamic_space_size) {
336 "dynamic space too small for core: %ldKiB required, %ldKiB available.\n",
338 (long)dynamic_space_size >> 10);
341 #ifdef LISP_FEATURE_GENCGC
342 if (addr != (os_vm_address_t)DYNAMIC_SPACE_START) {
343 fprintf(stderr, "in core: 0x%lx; in runtime: 0x%lx \n",
344 (long)addr, (long)DYNAMIC_SPACE_START);
345 lose("core/runtime address mismatch: DYNAMIC_SPACE_START\n");
348 if ((addr != (os_vm_address_t)DYNAMIC_0_SPACE_START) &&
349 (addr != (os_vm_address_t)DYNAMIC_1_SPACE_START)) {
350 fprintf(stderr, "in core: 0x%lx; in runtime: 0x%lx or 0x%lx\n",
352 (long)DYNAMIC_0_SPACE_START,
353 (long)DYNAMIC_1_SPACE_START);
354 lose("warning: core/runtime address mismatch: DYNAMIC_SPACE_START\n");
357 #if defined(ALLOCATION_POINTER)
358 SetSymbolValue(ALLOCATION_POINTER, (lispobj)free_pointer,0);
360 dynamic_space_free_pointer = free_pointer;
362 /* For stop-and-copy GC, this will be whatever the GC was
363 * using at the time. With GENCGC, this will always be
364 * space 0. (We checked above that for GENCGC,
365 * addr==DYNAMIC_SPACE_START.) */
366 current_dynamic_space = (lispobj *)addr;
368 case STATIC_CORE_SPACE_ID:
369 if (addr != (os_vm_address_t)STATIC_SPACE_START) {
370 fprintf(stderr, "in core: 0x%lx - in runtime: 0x%lx\n",
371 (long)addr, (long)STATIC_SPACE_START);
372 lose("core/runtime address mismatch: STATIC_SPACE_START\n");
375 case READ_ONLY_CORE_SPACE_ID:
376 if (addr != (os_vm_address_t)READ_ONLY_SPACE_START) {
377 fprintf(stderr, "in core: 0x%lx - in runtime: 0x%lx\n",
378 (long)addr, (long)READ_ONLY_SPACE_START);
379 lose("core/runtime address mismatch: READ_ONLY_SPACE_START\n");
383 lose("unknown space ID %ld addr 0x%lx\n", id, (long)addr);
389 load_core_file(char *file, os_vm_offset_t file_offset)
391 lispobj *header, val, len, *ptr, remaining_len;
392 int fd = open_binary(file, O_RDONLY);
395 lispobj initial_function = NIL;
396 FSHOW((stderr, "/entering load_core_file(%s)\n", file));
398 fprintf(stderr, "could not open file \"%s\"\n", file);
403 lseek(fd, file_offset, SEEK_SET);
404 header = calloc(os_vm_page_size / sizeof(u32), sizeof(u32));
406 count = read(fd, header, os_vm_page_size);
407 if (count < os_vm_page_size) {
408 lose("premature end of core file\n");
410 SHOW("successfully read first page of core");
415 if (val != CORE_MAGIC) {
416 lose("invalid magic number in core: 0x%lx should have been 0x%x.\n",
420 SHOW("found CORE_MAGIC");
422 while (val != END_CORE_ENTRY_TYPE_CODE) {
425 remaining_len = len - 2; /* (-2 to cancel the two ++ operations) */
426 FSHOW((stderr, "/val=0x%ld, remaining_len=0x%ld\n",
427 (long)val, (long)remaining_len));
431 case END_CORE_ENTRY_TYPE_CODE:
432 SHOW("END_CORE_ENTRY_TYPE_CODE case");
435 case VERSION_CORE_ENTRY_TYPE_CODE:
436 SHOW("VERSION_CORE_ENTRY_TYPE_CODE case");
437 if (*ptr != SBCL_CORE_VERSION_INTEGER) {
438 lose("core file version (%d) != runtime library version (%d)\n",
440 SBCL_CORE_VERSION_INTEGER);
444 case BUILD_ID_CORE_ENTRY_TYPE_CODE:
445 SHOW("BUILD_ID_CORE_ENTRY_TYPE_CODE case");
449 FSHOW((stderr, "build_id[]=\"%s\"\n", build_id));
450 FSHOW((stderr, "remaining_len = %d\n", remaining_len));
451 if (remaining_len != strlen((const char *)build_id))
452 goto losing_build_id;
453 for (i = 0; i < remaining_len; ++i) {
454 FSHOW((stderr, "ptr[%d] = char = %d, expected=%d\n",
455 i, ptr[i], build_id[i]));
456 if (ptr[i] != build_id[i])
457 goto losing_build_id;
461 /* .core files are not binary-compatible between
462 * builds because we can't easily detect whether the
463 * sources were patched between the time the
464 * dumping-the-.core runtime was built and the time
465 * that the loading-the-.core runtime was built.
467 * (We could easily detect whether version.lisp-expr
468 * was changed, but people experimenting with patches
469 * don't necessarily update version.lisp-expr.) */
471 lose("can't load .core for different runtime, sorry\n");
474 case NEW_DIRECTORY_CORE_ENTRY_TYPE_CODE:
475 SHOW("NEW_DIRECTORY_CORE_ENTRY_TYPE_CODE case");
476 process_directory(fd,
478 #ifndef LISP_FEATURE_ALPHA
479 remaining_len / (sizeof(struct ndir_entry) /
482 remaining_len / (sizeof(struct ndir_entry) /
488 case INITIAL_FUN_CORE_ENTRY_TYPE_CODE:
489 SHOW("INITIAL_FUN_CORE_ENTRY_TYPE_CODE case");
490 initial_function = (lispobj)*ptr;
493 #if defined(LISP_FEATURE_SB_THREAD) && defined(LISP_FEATURE_SB_LUTEX)
494 case LUTEX_TABLE_CORE_ENTRY_TYPE_CODE:
495 SHOW("LUTEX_TABLE_CORE_ENTRY_TYPE_CODE case");
497 size_t n_lutexes = *ptr;
498 size_t fdoffset = (*(ptr + 1) + 1) * (os_vm_page_size);
499 size_t data_length = n_lutexes * sizeof(struct sap *);
500 struct lutex **lutexes_to_resurrect = malloc(data_length);
503 lseek(fd, fdoffset + file_offset, SEEK_SET);
505 FSHOW((stderr, "attempting to read %ld lutexes from core\n", n_lutexes));
506 bytes_read = read(fd, lutexes_to_resurrect, data_length);
509 if (bytes_read != data_length) {
510 lose("Could not read the lutex table");
515 for (i=0; i<n_lutexes; ++i) {
516 struct lutex *lutex = lutexes_to_resurrect[i];
518 FSHOW((stderr, "re-init'ing lutex @ %p\n", lutex));
519 lutex_init((tagged_lutex_t) lutex);
522 free(lutexes_to_resurrect);
528 #ifdef LISP_FEATURE_GENCGC
529 case PAGE_TABLE_CORE_ENTRY_TYPE_CODE:
532 size_t fdoffset = (*(ptr+1) + 1) * (os_vm_page_size);
535 unsigned long data[4096];
537 lseek(fd, fdoffset + file_offset, SEEK_SET);
538 while ((bytes_read = read(fd, data, (size < 4096 ? size : 4096 )))
544 bytes_read -= sizeof(long);
545 /* Ignore all zeroes. The size of the page table
546 * core entry was rounded up to os_vm_page_size
547 * during the save, and might now have more
548 * elements than the page table.
550 * The low bits of each word are allocation flags.
553 page_table[offset].region_start_offset = word & ~0x03;
554 page_table[offset].allocated = word & 0x03;
561 gencgc_partial_pickup = 1;
566 lose("unknown core file entry: %ld\n", (long)val);
569 ptr += remaining_len;
570 FSHOW((stderr, "/new ptr=%lx\n", (unsigned long)ptr));
572 SHOW("about to free(header)");
574 SHOW("returning from load_core_file(..)");
575 return initial_function;