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>
26 #ifndef LISP_FEATURE_WIN32
27 #ifdef LISP_FEATURE_LINUX
44 #include "gc-internal.h"
47 #if defined(LISP_FEATURE_SB_THREAD) && defined(LISP_FEATURE_SB_LUTEX)
48 #include "genesis/sap.h"
49 #include "pthread-lutex.h"
54 #ifdef LISP_FEATURE_SB_CORE_COMPRESSION
58 unsigned char build_id[] =
59 #include "../../output/build-id.tmp"
63 open_binary(char *filename, int mode)
65 #ifdef LISP_FEATURE_WIN32
69 return open(filename, mode);
73 static struct runtime_options *
74 read_runtime_options(int fd)
76 size_t optarray[RUNTIME_OPTIONS_WORDS];
77 struct runtime_options *options = NULL;
79 if (read(fd, optarray, RUNTIME_OPTIONS_WORDS * sizeof(size_t)) !=
80 RUNTIME_OPTIONS_WORDS * sizeof(size_t)) {
84 if ((RUNTIME_OPTIONS_MAGIC != optarray[0]) || (0 == optarray[1])) {
88 options = successful_malloc(sizeof(struct runtime_options));
90 options->dynamic_space_size = optarray[2];
91 options->thread_control_stack_size = optarray[3];
97 maybe_initialize_runtime_options(int fd)
99 struct runtime_options *new_runtime_options;
100 off_t end_offset = sizeof(lispobj) +
101 sizeof(os_vm_offset_t) +
102 (RUNTIME_OPTIONS_WORDS * sizeof(size_t));
104 lseek(fd, -end_offset, SEEK_END);
106 if ((new_runtime_options = read_runtime_options(fd))) {
107 runtime_options = new_runtime_options;
111 /* Search 'filename' for an embedded core. An SBCL core has, at the
112 * end of the file, a trailer containing optional saved runtime
113 * options, the start of the core (an os_vm_offset_t), and a final
114 * signature word (the lispobj CORE_MAGIC). If this trailer is found
115 * at the end of the file, the start of the core can be determined
116 * from the core size.
118 * If an embedded core is present, this returns the offset into the
119 * file to load the core from, or -1 if no core is present. */
121 search_for_embedded_core(char *filename)
124 os_vm_offset_t lispobj_size = sizeof(lispobj);
125 os_vm_offset_t trailer_size = lispobj_size + sizeof(os_vm_offset_t);
126 os_vm_offset_t core_start, pos;
129 if ((fd = open_binary(filename, O_RDONLY)) < 0)
132 if (read(fd, &header, (size_t)lispobj_size) < lispobj_size)
134 if (header == CORE_MAGIC) {
135 /* This file is a real core, not an embedded core. Return 0 to
136 * indicate where the core starts, and do not look for runtime
137 * options in this case. */
141 if (lseek(fd, -lispobj_size, SEEK_END) < 0)
143 if (read(fd, &header, (size_t)lispobj_size) < lispobj_size)
146 if (header == CORE_MAGIC) {
147 if (lseek(fd, -trailer_size, SEEK_END) < 0)
149 if (read(fd, &core_start, sizeof(os_vm_offset_t)) < 0)
152 if (lseek(fd, core_start, SEEK_SET) < 0)
154 pos = lseek(fd, 0, SEEK_CUR);
156 if (read(fd, &header, (size_t)lispobj_size) < lispobj_size)
159 if (header != CORE_MAGIC)
162 maybe_initialize_runtime_options(fd);
175 /* If more platforms doesn't support overlapping mmap rename this
176 * def to something like ifdef nommapoverlap */
177 /* currently hpux only */
178 #ifdef LISP_FEATURE_HPUX
179 os_vm_address_t copy_core_bytes(int fd, os_vm_offset_t offset,
180 os_vm_address_t addr, int len)
182 unsigned char buf[4096];
184 int old_fd = lseek(fd, 0, SEEK_CUR);
187 fprintf(stderr, "cant copy a slice of core because slice-length is not of page size(4096)\n");
191 fprintf(stderr, "cant perform lseek() on corefile\n");
193 lseek(fd, offset, SEEK_SET);
195 fprintf(stderr, "cant perform lseek(%u,%lu,SEEK_SET) on corefile\n", fd, offset);
197 for(x = 0; x < len; x += 4096){
198 c = read(fd, buf, 4096);
200 fprintf(stderr, "cant read memory area from corefile at position %lu, got %d\n", offset + x, c);
203 memcpy(addr+x, buf, 4096);
205 os_flush_icache(addr, len);
210 #ifdef LISP_FEATURE_SB_CORE_COMPRESSION
211 # define ZLIB_BUFFER_SIZE (1u<<16)
212 os_vm_address_t inflate_core_bytes(int fd, os_vm_offset_t offset,
213 os_vm_address_t addr, int len)
216 unsigned char buf[ZLIB_BUFFER_SIZE];
219 if (-1 == lseek(fd, offset, SEEK_SET)) {
220 lose("Unable to lseek() on corefile\n");
223 stream.zalloc = NULL;
225 stream.opaque = NULL;
227 stream.next_in = buf;
229 ret = inflateInit(&stream);
231 lose("zlib error %i\n", ret);
233 stream.next_out = (void*)addr;
234 stream.avail_out = len;
236 ssize_t count = read(fd, buf, sizeof(buf));
238 lose("unable to read core file (errno = %i)\n", errno);
239 stream.next_in = buf;
240 stream.avail_in = count;
241 if (count == 0) break;
242 ret = inflate(&stream, Z_NO_FLUSH);
247 if (stream.avail_out == 0)
248 lose("Runaway gzipped core directory... aborting\n");
249 if (stream.avail_in > 0)
250 lose("zlib inflate returned without fully"
251 "using up input buffer... aborting\n");
254 lose("zlib inflate error: %i\n", ret);
257 } while (ret != Z_STREAM_END);
259 if (stream.avail_out > 0) {
260 if (stream.avail_out >= os_vm_page_size)
261 fprintf(stderr, "Warning: gzipped core directory significantly"
262 "shorter than expected (%lu bytes)", (unsigned long)stream.avail_out);
263 /* Is this needed? */
264 memset(stream.next_out, 0, stream.avail_out);
270 # undef ZLIB_BUFFER_SIZE
273 int merge_core_pages = -1;
276 process_directory(int fd, lispobj *ptr, int count, os_vm_offset_t file_offset)
278 struct ndir_entry *entry;
281 FSHOW((stderr, "/process_directory(..), count=%d\n", count));
283 for (entry = (struct ndir_entry *) ptr; --count>= 0; ++entry) {
285 long id = entry->identifier;
286 if (id <= (MAX_CORE_SPACE_ID | DEFLATED_CORE_SPACE_ID_FLAG)) {
287 if (id & DEFLATED_CORE_SPACE_ID_FLAG)
289 id &= ~(DEFLATED_CORE_SPACE_ID_FLAG);
291 long offset = os_vm_page_size * (1 + entry->data_page);
292 os_vm_address_t addr =
293 (os_vm_address_t) (os_vm_page_size * entry->address);
294 lispobj *free_pointer = (lispobj *) addr + entry->nwords;
295 unsigned long len = os_vm_page_size * entry->page_count;
297 os_vm_address_t real_addr;
298 FSHOW((stderr, "/mapping %ld(0x%lx) bytes at 0x%lx\n",
299 (long)len, (long)len, (unsigned long)addr));
301 #ifdef LISP_FEATURE_SB_CORE_COMPRESSION
302 real_addr = inflate_core_bytes(fd, offset + file_offset, addr, len);
304 lose("This runtime was not built with zlib-compressed core support... aborting\n");
307 #ifdef LISP_FEATURE_HPUX
308 real_addr = copy_core_bytes(fd, offset + file_offset, addr, len);
310 real_addr = os_map(fd, offset + file_offset, addr, len);
313 if (real_addr != addr) {
314 lose("file mapped in wrong place! "
315 "(0x%08x != 0x%08lx)\n",
321 #ifdef MADV_MERGEABLE
322 if ((merge_core_pages == 1)
323 || ((merge_core_pages == -1) && compressed)) {
324 madvise(addr, len, MADV_MERGEABLE);
328 FSHOW((stderr, "/space id = %ld, free pointer = 0x%lx\n",
329 id, (unsigned long)free_pointer));
332 case DYNAMIC_CORE_SPACE_ID:
333 if (len > dynamic_space_size) {
335 "dynamic space too small for core: %ldKiB required, %ldKiB available.\n",
337 (long)dynamic_space_size >> 10);
340 #ifdef LISP_FEATURE_GENCGC
341 if (addr != (os_vm_address_t)DYNAMIC_SPACE_START) {
342 fprintf(stderr, "in core: 0x%lx; in runtime: 0x%lx \n",
343 (long)addr, (long)DYNAMIC_SPACE_START);
344 lose("core/runtime address mismatch: DYNAMIC_SPACE_START\n");
347 if ((addr != (os_vm_address_t)DYNAMIC_0_SPACE_START) &&
348 (addr != (os_vm_address_t)DYNAMIC_1_SPACE_START)) {
349 fprintf(stderr, "in core: 0x%lx; in runtime: 0x%lx or 0x%lx\n",
351 (long)DYNAMIC_0_SPACE_START,
352 (long)DYNAMIC_1_SPACE_START);
353 lose("warning: core/runtime address mismatch: DYNAMIC_SPACE_START\n");
356 #if defined(ALLOCATION_POINTER)
357 SetSymbolValue(ALLOCATION_POINTER, (lispobj)free_pointer,0);
359 dynamic_space_free_pointer = free_pointer;
361 /* For stop-and-copy GC, this will be whatever the GC was
362 * using at the time. With GENCGC, this will always be
363 * space 0. (We checked above that for GENCGC,
364 * addr==DYNAMIC_SPACE_START.) */
365 current_dynamic_space = (lispobj *)addr;
367 case STATIC_CORE_SPACE_ID:
368 if (addr != (os_vm_address_t)STATIC_SPACE_START) {
369 fprintf(stderr, "in core: 0x%lx - in runtime: 0x%lx\n",
370 (long)addr, (long)STATIC_SPACE_START);
371 lose("core/runtime address mismatch: STATIC_SPACE_START\n");
374 case READ_ONLY_CORE_SPACE_ID:
375 if (addr != (os_vm_address_t)READ_ONLY_SPACE_START) {
376 fprintf(stderr, "in core: 0x%lx - in runtime: 0x%lx\n",
377 (long)addr, (long)READ_ONLY_SPACE_START);
378 lose("core/runtime address mismatch: READ_ONLY_SPACE_START\n");
382 lose("unknown space ID %ld addr 0x%lx\n", id, (long)addr);
388 load_core_file(char *file, os_vm_offset_t file_offset)
390 lispobj *header, val, len, *ptr, remaining_len;
391 int fd = open_binary(file, O_RDONLY);
394 lispobj initial_function = NIL;
395 FSHOW((stderr, "/entering load_core_file(%s)\n", file));
397 fprintf(stderr, "could not open file \"%s\"\n", file);
402 lseek(fd, file_offset, SEEK_SET);
403 header = calloc(os_vm_page_size / sizeof(u32), sizeof(u32));
405 count = read(fd, header, os_vm_page_size);
406 if (count < os_vm_page_size) {
407 lose("premature end of core file\n");
409 SHOW("successfully read first page of core");
414 if (val != CORE_MAGIC) {
415 lose("invalid magic number in core: 0x%lx should have been 0x%x.\n",
419 SHOW("found CORE_MAGIC");
421 while (val != END_CORE_ENTRY_TYPE_CODE) {
424 remaining_len = len - 2; /* (-2 to cancel the two ++ operations) */
425 FSHOW((stderr, "/val=0x%ld, remaining_len=0x%ld\n",
426 (long)val, (long)remaining_len));
430 case END_CORE_ENTRY_TYPE_CODE:
431 SHOW("END_CORE_ENTRY_TYPE_CODE case");
434 case VERSION_CORE_ENTRY_TYPE_CODE:
435 SHOW("VERSION_CORE_ENTRY_TYPE_CODE case");
436 if (*ptr != SBCL_CORE_VERSION_INTEGER) {
437 lose("core file version (%d) != runtime library version (%d)\n",
439 SBCL_CORE_VERSION_INTEGER);
443 case BUILD_ID_CORE_ENTRY_TYPE_CODE:
444 SHOW("BUILD_ID_CORE_ENTRY_TYPE_CODE case");
448 FSHOW((stderr, "build_id[]=\"%s\"\n", build_id));
449 FSHOW((stderr, "remaining_len = %d\n", remaining_len));
450 if (remaining_len != strlen((const char *)build_id))
451 goto losing_build_id;
452 for (i = 0; i < remaining_len; ++i) {
453 FSHOW((stderr, "ptr[%d] = char = %d, expected=%d\n",
454 i, ptr[i], build_id[i]));
455 if (ptr[i] != build_id[i])
456 goto losing_build_id;
460 /* .core files are not binary-compatible between
461 * builds because we can't easily detect whether the
462 * sources were patched between the time the
463 * dumping-the-.core runtime was built and the time
464 * that the loading-the-.core runtime was built.
466 * (We could easily detect whether version.lisp-expr
467 * was changed, but people experimenting with patches
468 * don't necessarily update version.lisp-expr.) */
470 lose("can't load .core for different runtime, sorry\n");
473 case NEW_DIRECTORY_CORE_ENTRY_TYPE_CODE:
474 SHOW("NEW_DIRECTORY_CORE_ENTRY_TYPE_CODE case");
475 process_directory(fd,
477 #ifndef LISP_FEATURE_ALPHA
478 remaining_len / (sizeof(struct ndir_entry) /
481 remaining_len / (sizeof(struct ndir_entry) /
487 case INITIAL_FUN_CORE_ENTRY_TYPE_CODE:
488 SHOW("INITIAL_FUN_CORE_ENTRY_TYPE_CODE case");
489 initial_function = (lispobj)*ptr;
492 #if defined(LISP_FEATURE_SB_THREAD) && defined(LISP_FEATURE_SB_LUTEX)
493 case LUTEX_TABLE_CORE_ENTRY_TYPE_CODE:
494 SHOW("LUTEX_TABLE_CORE_ENTRY_TYPE_CODE case");
496 size_t n_lutexes = *ptr;
497 size_t fdoffset = (*(ptr + 1) + 1) * (os_vm_page_size);
498 size_t data_length = n_lutexes * sizeof(struct sap *);
499 struct lutex **lutexes_to_resurrect = malloc(data_length);
502 lseek(fd, fdoffset + file_offset, SEEK_SET);
504 FSHOW((stderr, "attempting to read %ld lutexes from core\n", n_lutexes));
505 bytes_read = read(fd, lutexes_to_resurrect, data_length);
508 if (bytes_read != data_length) {
509 lose("Could not read the lutex table");
514 for (i=0; i<n_lutexes; ++i) {
515 struct lutex *lutex = lutexes_to_resurrect[i];
517 FSHOW((stderr, "re-init'ing lutex @ %p\n", lutex));
518 lutex_init((tagged_lutex_t) lutex);
521 free(lutexes_to_resurrect);
527 #ifdef LISP_FEATURE_GENCGC
528 case PAGE_TABLE_CORE_ENTRY_TYPE_CODE:
531 size_t fdoffset = (*(ptr+1) + 1) * (os_vm_page_size);
534 unsigned long data[4096];
536 lseek(fd, fdoffset + file_offset, SEEK_SET);
537 while ((bytes_read = read(fd, data, (size < 4096 ? size : 4096 )))
543 bytes_read -= sizeof(long);
544 /* Ignore all zeroes. The size of the page table
545 * core entry was rounded up to os_vm_page_size
546 * during the save, and might now have more
547 * elements than the page table.
549 * The low bits of each word are allocation flags.
552 page_table[offset].region_start_offset = word & ~0x03;
553 page_table[offset].allocated = word & 0x03;
560 gencgc_partial_pickup = 1;
565 lose("unknown core file entry: %ld\n", (long)val);
568 ptr += remaining_len;
569 FSHOW((stderr, "/new ptr=%lx\n", (unsigned long)ptr));
571 SHOW("about to free(header)");
573 SHOW("returning from load_core_file(..)");
574 return initial_function;