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>
25 #ifdef LISP_FEATURE_LINUX
41 #include "gc-internal.h"
44 #if defined(LISP_FEATURE_SB_THREAD) && defined(LISP_FEATURE_SB_LUTEX)
45 #include "genesis/sap.h"
46 #include "pthread-lutex.h"
51 #ifdef LISP_FEATURE_SB_CORE_COMPRESSION
55 unsigned char build_id[] =
56 #include "../../output/build-id.tmp"
60 open_binary(char *filename, int mode)
62 #ifdef LISP_FEATURE_WIN32
66 return open(filename, mode);
70 static struct runtime_options *
71 read_runtime_options(int fd)
73 size_t optarray[RUNTIME_OPTIONS_WORDS];
74 struct runtime_options *options = NULL;
76 if (read(fd, optarray, RUNTIME_OPTIONS_WORDS * sizeof(size_t)) !=
77 RUNTIME_OPTIONS_WORDS * sizeof(size_t)) {
81 if ((RUNTIME_OPTIONS_MAGIC != optarray[0]) || (0 == optarray[1])) {
85 options = successful_malloc(sizeof(struct runtime_options));
87 options->dynamic_space_size = optarray[2];
88 options->thread_control_stack_size = optarray[3];
94 maybe_initialize_runtime_options(int fd)
96 struct runtime_options *new_runtime_options;
97 off_t end_offset = sizeof(lispobj) +
98 sizeof(os_vm_offset_t) +
99 (RUNTIME_OPTIONS_WORDS * sizeof(size_t));
101 lseek(fd, -end_offset, SEEK_END);
103 if ((new_runtime_options = read_runtime_options(fd))) {
104 runtime_options = new_runtime_options;
108 /* Search 'filename' for an embedded core. An SBCL core has, at the
109 * end of the file, a trailer containing optional saved runtime
110 * options, the start of the core (an os_vm_offset_t), and a final
111 * signature word (the lispobj CORE_MAGIC). If this trailer is found
112 * at the end of the file, the start of the core can be determined
113 * from the core size.
115 * If an embedded core is present, this returns the offset into the
116 * file to load the core from, or -1 if no core is present. */
118 search_for_embedded_core(char *filename)
121 os_vm_offset_t lispobj_size = sizeof(lispobj);
122 os_vm_offset_t trailer_size = lispobj_size + sizeof(os_vm_offset_t);
123 os_vm_offset_t core_start, pos;
126 if ((fd = open_binary(filename, O_RDONLY)) < 0)
129 if (read(fd, &header, (size_t)lispobj_size) < lispobj_size)
131 if (header == CORE_MAGIC) {
132 /* This file is a real core, not an embedded core. Return 0 to
133 * indicate where the core starts, and do not look for runtime
134 * options in this case. */
138 if (lseek(fd, -lispobj_size, SEEK_END) < 0)
140 if (read(fd, &header, (size_t)lispobj_size) < lispobj_size)
143 if (header == CORE_MAGIC) {
144 if (lseek(fd, -trailer_size, SEEK_END) < 0)
146 if (read(fd, &core_start, sizeof(os_vm_offset_t)) < 0)
149 if (lseek(fd, core_start, SEEK_SET) < 0)
151 pos = lseek(fd, 0, SEEK_CUR);
153 if (read(fd, &header, (size_t)lispobj_size) < lispobj_size)
156 if (header != CORE_MAGIC)
159 maybe_initialize_runtime_options(fd);
172 /* If more platforms doesn't support overlapping mmap rename this
173 * def to something like ifdef nommapoverlap */
174 /* currently hpux only */
175 #ifdef LISP_FEATURE_HPUX
176 os_vm_address_t copy_core_bytes(int fd, os_vm_offset_t offset,
177 os_vm_address_t addr, int len)
179 unsigned char buf[4096];
181 int old_fd = lseek(fd, 0, SEEK_CUR);
184 fprintf(stderr, "cant copy a slice of core because slice-length is not of page size(4096)\n");
188 fprintf(stderr, "cant perform lseek() on corefile\n");
190 lseek(fd, offset, SEEK_SET);
192 fprintf(stderr, "cant perform lseek(%u,%lu,SEEK_SET) on corefile\n", fd, offset);
194 for(x = 0; x < len; x += 4096){
195 c = read(fd, buf, 4096);
197 fprintf(stderr, "cant read memory area from corefile at position %lu, got %d\n", offset + x, c);
200 memcpy(addr+x, buf, 4096);
202 os_flush_icache(addr, len);
207 #ifdef LISP_FEATURE_SB_CORE_COMPRESSION
208 # define ZLIB_BUFFER_SIZE (1u<<16)
209 os_vm_address_t inflate_core_bytes(int fd, os_vm_offset_t offset,
210 os_vm_address_t addr, int len)
213 unsigned char buf[ZLIB_BUFFER_SIZE];
216 if (-1 == lseek(fd, offset, SEEK_SET)) {
217 lose("Unable to lseek() on corefile\n");
220 stream.zalloc = NULL;
222 stream.opaque = NULL;
224 stream.next_in = buf;
226 ret = inflateInit(&stream);
228 lose("zlib error %i\n", ret);
230 stream.next_out = (void*)addr;
231 stream.avail_out = len;
233 ssize_t count = read(fd, buf, sizeof(buf));
235 lose("unable to read core file (errno = %i)\n", errno);
236 stream.next_in = buf;
237 stream.avail_in = count;
238 if (count == 0) break;
239 ret = inflate(&stream, Z_NO_FLUSH);
244 if (stream.avail_out == 0)
245 lose("Runaway gzipped core directory... aborting\n");
246 if (stream.avail_in > 0)
247 lose("zlib inflate returned without fully"
248 "using up input buffer... aborting\n");
251 lose("zlib inflate error: %i\n", ret);
254 } while (ret != Z_STREAM_END);
256 if (stream.avail_out > 0) {
257 if (stream.avail_out >= os_vm_page_size)
258 fprintf(stderr, "Warning: gzipped core directory significantly"
259 "shorter than expected (%lu bytes)", (unsigned long)stream.avail_out);
260 /* Is this needed? */
261 memset(stream.next_out, 0, stream.avail_out);
267 # undef ZLIB_BUFFER_SIZE
270 int merge_core_pages = -1;
273 process_directory(int fd, lispobj *ptr, int count, os_vm_offset_t file_offset)
275 struct ndir_entry *entry;
278 FSHOW((stderr, "/process_directory(..), count=%d\n", count));
280 for (entry = (struct ndir_entry *) ptr; --count>= 0; ++entry) {
282 long id = entry->identifier;
283 if (id <= (MAX_CORE_SPACE_ID | DEFLATED_CORE_SPACE_ID_FLAG)) {
284 if (id & DEFLATED_CORE_SPACE_ID_FLAG)
286 id &= ~(DEFLATED_CORE_SPACE_ID_FLAG);
288 long offset = os_vm_page_size * (1 + entry->data_page);
289 os_vm_address_t addr =
290 (os_vm_address_t) (os_vm_page_size * entry->address);
291 lispobj *free_pointer = (lispobj *) addr + entry->nwords;
292 unsigned long len = os_vm_page_size * entry->page_count;
294 os_vm_address_t real_addr;
295 FSHOW((stderr, "/mapping %ld(0x%lx) bytes at 0x%lx\n",
296 (long)len, (long)len, (unsigned long)addr));
298 #ifdef LISP_FEATURE_SB_CORE_COMPRESSION
299 real_addr = inflate_core_bytes(fd, offset + file_offset, addr, len);
301 lose("This runtime was not built with zlib-compressed core support... aborting\n");
304 #ifdef LISP_FEATURE_HPUX
305 real_addr = copy_core_bytes(fd, offset + file_offset, addr, len);
307 real_addr = os_map(fd, offset + file_offset, addr, len);
310 if (real_addr != addr) {
311 lose("file mapped in wrong place! "
312 "(0x%08x != 0x%08lx)\n",
318 #ifdef MADV_MERGEABLE
319 if ((merge_core_pages == 1)
320 || ((merge_core_pages == -1) && compressed)) {
321 madvise(addr, len, MADV_MERGEABLE);
325 FSHOW((stderr, "/space id = %ld, free pointer = 0x%lx\n",
326 id, (unsigned long)free_pointer));
329 case DYNAMIC_CORE_SPACE_ID:
330 if (len > dynamic_space_size) {
332 "dynamic space too small for core: %ldKiB required, %ldKiB available.\n",
334 (long)dynamic_space_size >> 10);
337 #ifdef LISP_FEATURE_GENCGC
338 if (addr != (os_vm_address_t)DYNAMIC_SPACE_START) {
339 fprintf(stderr, "in core: 0x%lx; in runtime: 0x%lx \n",
340 (long)addr, (long)DYNAMIC_SPACE_START);
341 lose("core/runtime address mismatch: DYNAMIC_SPACE_START\n");
344 if ((addr != (os_vm_address_t)DYNAMIC_0_SPACE_START) &&
345 (addr != (os_vm_address_t)DYNAMIC_1_SPACE_START)) {
346 fprintf(stderr, "in core: 0x%lx; in runtime: 0x%lx or 0x%lx\n",
348 (long)DYNAMIC_0_SPACE_START,
349 (long)DYNAMIC_1_SPACE_START);
350 lose("warning: core/runtime address mismatch: DYNAMIC_SPACE_START\n");
353 #if defined(ALLOCATION_POINTER)
354 SetSymbolValue(ALLOCATION_POINTER, (lispobj)free_pointer,0);
356 dynamic_space_free_pointer = free_pointer;
358 /* For stop-and-copy GC, this will be whatever the GC was
359 * using at the time. With GENCGC, this will always be
360 * space 0. (We checked above that for GENCGC,
361 * addr==DYNAMIC_SPACE_START.) */
362 current_dynamic_space = (lispobj *)addr;
364 case STATIC_CORE_SPACE_ID:
365 if (addr != (os_vm_address_t)STATIC_SPACE_START) {
366 fprintf(stderr, "in core: 0x%lx - in runtime: 0x%lx\n",
367 (long)addr, (long)STATIC_SPACE_START);
368 lose("core/runtime address mismatch: STATIC_SPACE_START\n");
371 case READ_ONLY_CORE_SPACE_ID:
372 if (addr != (os_vm_address_t)READ_ONLY_SPACE_START) {
373 fprintf(stderr, "in core: 0x%lx - in runtime: 0x%lx\n",
374 (long)addr, (long)READ_ONLY_SPACE_START);
375 lose("core/runtime address mismatch: READ_ONLY_SPACE_START\n");
379 lose("unknown space ID %ld addr 0x%lx\n", id, (long)addr);
385 load_core_file(char *file, os_vm_offset_t file_offset)
387 lispobj *header, val, len, *ptr, remaining_len;
388 int fd = open_binary(file, O_RDONLY);
391 lispobj initial_function = NIL;
392 FSHOW((stderr, "/entering load_core_file(%s)\n", file));
394 fprintf(stderr, "could not open file \"%s\"\n", file);
399 lseek(fd, file_offset, SEEK_SET);
400 header = calloc(os_vm_page_size / sizeof(u32), sizeof(u32));
402 count = read(fd, header, os_vm_page_size);
403 if (count < os_vm_page_size) {
404 lose("premature end of core file\n");
406 SHOW("successfully read first page of core");
411 if (val != CORE_MAGIC) {
412 lose("invalid magic number in core: 0x%lx should have been 0x%x.\n",
416 SHOW("found CORE_MAGIC");
418 while (val != END_CORE_ENTRY_TYPE_CODE) {
421 remaining_len = len - 2; /* (-2 to cancel the two ++ operations) */
422 FSHOW((stderr, "/val=0x%ld, remaining_len=0x%ld\n",
423 (long)val, (long)remaining_len));
427 case END_CORE_ENTRY_TYPE_CODE:
428 SHOW("END_CORE_ENTRY_TYPE_CODE case");
431 case VERSION_CORE_ENTRY_TYPE_CODE:
432 SHOW("VERSION_CORE_ENTRY_TYPE_CODE case");
433 if (*ptr != SBCL_CORE_VERSION_INTEGER) {
434 lose("core file version (%d) != runtime library version (%d)\n",
436 SBCL_CORE_VERSION_INTEGER);
440 case BUILD_ID_CORE_ENTRY_TYPE_CODE:
441 SHOW("BUILD_ID_CORE_ENTRY_TYPE_CODE case");
445 FSHOW((stderr, "build_id[]=\"%s\"\n", build_id));
446 FSHOW((stderr, "remaining_len = %d\n", remaining_len));
447 if (remaining_len != strlen((const char *)build_id))
448 goto losing_build_id;
449 for (i = 0; i < remaining_len; ++i) {
450 FSHOW((stderr, "ptr[%d] = char = %d, expected=%d\n",
451 i, ptr[i], build_id[i]));
452 if (ptr[i] != build_id[i])
453 goto losing_build_id;
457 /* .core files are not binary-compatible between
458 * builds because we can't easily detect whether the
459 * sources were patched between the time the
460 * dumping-the-.core runtime was built and the time
461 * that the loading-the-.core runtime was built.
463 * (We could easily detect whether version.lisp-expr
464 * was changed, but people experimenting with patches
465 * don't necessarily update version.lisp-expr.) */
467 lose("can't load .core for different runtime, sorry\n");
470 case NEW_DIRECTORY_CORE_ENTRY_TYPE_CODE:
471 SHOW("NEW_DIRECTORY_CORE_ENTRY_TYPE_CODE case");
472 process_directory(fd,
474 #ifndef LISP_FEATURE_ALPHA
475 remaining_len / (sizeof(struct ndir_entry) /
478 remaining_len / (sizeof(struct ndir_entry) /
484 case INITIAL_FUN_CORE_ENTRY_TYPE_CODE:
485 SHOW("INITIAL_FUN_CORE_ENTRY_TYPE_CODE case");
486 initial_function = (lispobj)*ptr;
489 #if defined(LISP_FEATURE_SB_THREAD) && defined(LISP_FEATURE_SB_LUTEX)
490 case LUTEX_TABLE_CORE_ENTRY_TYPE_CODE:
491 SHOW("LUTEX_TABLE_CORE_ENTRY_TYPE_CODE case");
493 size_t n_lutexes = *ptr;
494 size_t fdoffset = (*(ptr + 1) + 1) * (os_vm_page_size);
495 size_t data_length = n_lutexes * sizeof(struct sap *);
496 struct lutex **lutexes_to_resurrect = malloc(data_length);
499 lseek(fd, fdoffset + file_offset, SEEK_SET);
501 FSHOW((stderr, "attempting to read %ld lutexes from core\n", n_lutexes));
502 bytes_read = read(fd, lutexes_to_resurrect, data_length);
505 if (bytes_read != data_length) {
506 lose("Could not read the lutex table");
511 for (i=0; i<n_lutexes; ++i) {
512 struct lutex *lutex = lutexes_to_resurrect[i];
514 FSHOW((stderr, "re-init'ing lutex @ %p\n", lutex));
515 lutex_init((tagged_lutex_t) lutex);
518 free(lutexes_to_resurrect);
524 #ifdef LISP_FEATURE_GENCGC
525 case PAGE_TABLE_CORE_ENTRY_TYPE_CODE:
528 size_t fdoffset = (*(ptr+1) + 1) * (os_vm_page_size);
531 unsigned long data[4096];
533 lseek(fd, fdoffset + file_offset, SEEK_SET);
534 while ((bytes_read = read(fd, data, (size < 4096 ? size : 4096 )))
540 bytes_read -= sizeof(long);
541 /* Ignore all zeroes. The size of the page table
542 * core entry was rounded up to os_vm_page_size
543 * during the save, and might now have more
544 * elements than the page table.
546 * The low bits of each word are allocation flags.
549 page_table[offset].region_start_offset = word & ~0x03;
550 page_table[offset].allocated = word & 0x03;
557 gencgc_partial_pickup = 1;
562 lose("unknown core file entry: %ld\n", (long)val);
565 ptr += remaining_len;
566 FSHOW((stderr, "/new ptr=%lx\n", (unsigned long)ptr));
568 SHOW("about to free(header)");
570 SHOW("returning from load_core_file(..)");
571 return initial_function;