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"
46 #include "runtime-options.h"
50 #ifdef LISP_FEATURE_SB_CORE_COMPRESSION
54 unsigned char build_id[] =
55 #include "../../output/build-id.tmp"
59 open_binary(char *filename, int mode)
61 #ifdef LISP_FEATURE_WIN32
65 return open(filename, mode);
69 static struct runtime_options *
70 read_runtime_options(int fd)
72 os_vm_size_t optarray[RUNTIME_OPTIONS_WORDS];
73 struct runtime_options *options = NULL;
75 if (read(fd, optarray, RUNTIME_OPTIONS_WORDS * sizeof(os_vm_size_t)) !=
76 RUNTIME_OPTIONS_WORDS * sizeof(size_t)) {
80 if ((RUNTIME_OPTIONS_MAGIC != optarray[0]) || (0 == optarray[1])) {
84 options = successful_malloc(sizeof(struct runtime_options));
86 options->dynamic_space_size = optarray[2];
87 options->thread_control_stack_size = optarray[3];
93 maybe_initialize_runtime_options(int fd)
95 struct runtime_options *new_runtime_options;
96 off_t end_offset = sizeof(lispobj) +
97 sizeof(os_vm_offset_t) +
98 (RUNTIME_OPTIONS_WORDS * sizeof(size_t));
100 lseek(fd, -end_offset, SEEK_END);
102 if ((new_runtime_options = read_runtime_options(fd))) {
103 runtime_options = new_runtime_options;
107 /* Search 'filename' for an embedded core. An SBCL core has, at the
108 * end of the file, a trailer containing optional saved runtime
109 * options, the start of the core (an os_vm_offset_t), and a final
110 * signature word (the lispobj CORE_MAGIC). If this trailer is found
111 * at the end of the file, the start of the core can be determined
112 * from the core size.
114 * If an embedded core is present, this returns the offset into the
115 * file to load the core from, or -1 if no core is present. */
117 search_for_embedded_core(char *filename)
120 os_vm_offset_t lispobj_size = sizeof(lispobj);
121 os_vm_offset_t trailer_size = lispobj_size + sizeof(os_vm_offset_t);
122 os_vm_offset_t core_start, pos;
125 if ((fd = open_binary(filename, O_RDONLY)) < 0)
128 if (read(fd, &header, (size_t)lispobj_size) < lispobj_size)
130 if (header == CORE_MAGIC) {
131 /* This file is a real core, not an embedded core. Return 0 to
132 * indicate where the core starts, and do not look for runtime
133 * options in this case. */
137 if (lseek(fd, -lispobj_size, SEEK_END) < 0)
139 if (read(fd, &header, (size_t)lispobj_size) < lispobj_size)
142 if (header == CORE_MAGIC) {
143 if (lseek(fd, -trailer_size, SEEK_END) < 0)
145 if (read(fd, &core_start, sizeof(os_vm_offset_t)) < 0)
148 if (lseek(fd, core_start, SEEK_SET) < 0)
150 pos = lseek(fd, 0, SEEK_CUR);
152 if (read(fd, &header, (size_t)lispobj_size) < lispobj_size)
155 if (header != CORE_MAGIC)
158 maybe_initialize_runtime_options(fd);
171 /* If more platforms doesn't support overlapping mmap rename this
172 * def to something like ifdef nommapoverlap */
173 /* currently hpux only */
174 #ifdef LISP_FEATURE_HPUX
175 os_vm_address_t copy_core_bytes(int fd, os_vm_offset_t offset,
176 os_vm_address_t addr, int len)
178 unsigned char buf[4096];
180 int old_fd = lseek(fd, 0, SEEK_CUR);
183 fprintf(stderr, "cant copy a slice of core because slice-length is not of page size(4096)\n");
187 fprintf(stderr, "cant perform lseek() on corefile\n");
189 lseek(fd, offset, SEEK_SET);
191 fprintf(stderr, "cant perform lseek(%u,%lu,SEEK_SET) on corefile\n", fd, offset);
193 for(x = 0; x < len; x += 4096){
194 c = read(fd, buf, 4096);
196 fprintf(stderr, "cant read memory area from corefile at position %lu, got %d\n", offset + x, c);
199 memcpy(addr+x, buf, 4096);
201 os_flush_icache(addr, len);
206 #ifdef LISP_FEATURE_SB_CORE_COMPRESSION
207 # define ZLIB_BUFFER_SIZE (1u<<16)
208 os_vm_address_t inflate_core_bytes(int fd, os_vm_offset_t offset,
209 os_vm_address_t addr, int len)
212 unsigned char buf[ZLIB_BUFFER_SIZE];
215 if (-1 == lseek(fd, offset, SEEK_SET)) {
216 lose("Unable to lseek() on corefile\n");
219 stream.zalloc = NULL;
221 stream.opaque = NULL;
223 stream.next_in = buf;
225 ret = inflateInit(&stream);
227 lose("zlib error %i\n", ret);
229 stream.next_out = (void*)addr;
230 stream.avail_out = len;
232 ssize_t count = read(fd, buf, sizeof(buf));
234 lose("unable to read core file (errno = %i)\n", errno);
235 stream.next_in = buf;
236 stream.avail_in = count;
237 if (count == 0) break;
238 ret = inflate(&stream, Z_NO_FLUSH);
243 if (stream.avail_out == 0)
244 lose("Runaway gzipped core directory... aborting\n");
245 if (stream.avail_in > 0)
246 lose("zlib inflate returned without fully"
247 "using up input buffer... aborting\n");
250 lose("zlib inflate error: %i\n", ret);
253 } while (ret != Z_STREAM_END);
255 if (stream.avail_out > 0) {
256 if (stream.avail_out >= os_vm_page_size)
257 fprintf(stderr, "Warning: gzipped core directory significantly"
258 "shorter than expected (%lu bytes)", (unsigned long)stream.avail_out);
259 /* Is this needed? */
260 memset(stream.next_out, 0, stream.avail_out);
266 # undef ZLIB_BUFFER_SIZE
269 int merge_core_pages = -1;
272 process_directory(int fd, lispobj *ptr, int count, os_vm_offset_t file_offset)
274 struct ndir_entry *entry;
277 FSHOW((stderr, "/process_directory(..), count=%d\n", count));
279 for (entry = (struct ndir_entry *) ptr; --count>= 0; ++entry) {
281 long id = entry->identifier;
282 if (id <= (MAX_CORE_SPACE_ID | DEFLATED_CORE_SPACE_ID_FLAG)) {
283 if (id & DEFLATED_CORE_SPACE_ID_FLAG)
285 id &= ~(DEFLATED_CORE_SPACE_ID_FLAG);
287 long offset = os_vm_page_size * (1 + entry->data_page);
288 os_vm_address_t addr =
289 (os_vm_address_t) (os_vm_page_size * entry->address);
290 lispobj *free_pointer = (lispobj *) addr + entry->nwords;
291 unsigned long len = os_vm_page_size * entry->page_count;
293 os_vm_address_t real_addr;
294 FSHOW((stderr, "/mapping %ld(0x%lx) bytes at 0x%lx\n",
295 (long)len, (long)len, (unsigned long)addr));
297 #ifdef LISP_FEATURE_SB_CORE_COMPRESSION
298 real_addr = inflate_core_bytes(fd, offset + file_offset, addr, len);
300 lose("This runtime was not built with zlib-compressed core support... aborting\n");
303 #ifdef LISP_FEATURE_HPUX
304 real_addr = copy_core_bytes(fd, offset + file_offset, addr, len);
306 real_addr = os_map(fd, offset + file_offset, addr, len);
309 if (real_addr != addr) {
310 lose("file mapped in wrong place! "
311 "(0x%08x != 0x%08lx)\n",
317 #ifdef MADV_MERGEABLE
318 if ((merge_core_pages == 1)
319 || ((merge_core_pages == -1) && compressed)) {
320 madvise(addr, len, MADV_MERGEABLE);
324 FSHOW((stderr, "/space id = %ld, free pointer = 0x%lx\n",
325 id, (unsigned long)free_pointer));
328 case DYNAMIC_CORE_SPACE_ID:
329 if (len > dynamic_space_size) {
331 "dynamic space too small for core: %ldKiB required, %ldKiB available.\n",
333 (long)dynamic_space_size >> 10);
336 #ifdef LISP_FEATURE_GENCGC
337 if (addr != (os_vm_address_t)DYNAMIC_SPACE_START) {
338 fprintf(stderr, "in core: 0x%lx; in runtime: 0x%lx \n",
339 (long)addr, (long)DYNAMIC_SPACE_START);
340 lose("core/runtime address mismatch: DYNAMIC_SPACE_START\n");
343 if ((addr != (os_vm_address_t)DYNAMIC_0_SPACE_START) &&
344 (addr != (os_vm_address_t)DYNAMIC_1_SPACE_START)) {
345 fprintf(stderr, "in core: 0x%lx; in runtime: 0x%lx or 0x%lx\n",
347 (long)DYNAMIC_0_SPACE_START,
348 (long)DYNAMIC_1_SPACE_START);
349 lose("warning: core/runtime address mismatch: DYNAMIC_SPACE_START\n");
352 #if defined(ALLOCATION_POINTER)
353 SetSymbolValue(ALLOCATION_POINTER, (lispobj)free_pointer,0);
355 dynamic_space_free_pointer = free_pointer;
357 /* For stop-and-copy GC, this will be whatever the GC was
358 * using at the time. With GENCGC, this will always be
359 * space 0. (We checked above that for GENCGC,
360 * addr==DYNAMIC_SPACE_START.) */
361 current_dynamic_space = (lispobj *)addr;
363 case STATIC_CORE_SPACE_ID:
364 if (addr != (os_vm_address_t)STATIC_SPACE_START) {
365 fprintf(stderr, "in core: 0x%lx - in runtime: 0x%lx\n",
366 (long)addr, (long)STATIC_SPACE_START);
367 lose("core/runtime address mismatch: STATIC_SPACE_START\n");
370 case READ_ONLY_CORE_SPACE_ID:
371 if (addr != (os_vm_address_t)READ_ONLY_SPACE_START) {
372 fprintf(stderr, "in core: 0x%lx - in runtime: 0x%lx\n",
373 (long)addr, (long)READ_ONLY_SPACE_START);
374 lose("core/runtime address mismatch: READ_ONLY_SPACE_START\n");
378 lose("unknown space ID %ld addr 0x%lx\n", id, (long)addr);
384 load_core_file(char *file, os_vm_offset_t file_offset)
388 os_vm_size_t len, remaining_len;
389 int fd = open_binary(file, O_RDONLY);
391 lispobj initial_function = NIL;
393 FSHOW((stderr, "/entering load_core_file(%s)\n", file));
395 fprintf(stderr, "could not open file \"%s\"\n", file);
400 lseek(fd, file_offset, SEEK_SET);
401 header = calloc(os_vm_page_size, 1);
403 count = read(fd, header, os_vm_page_size);
404 if (count < os_vm_page_size) {
405 lose("premature end of core file\n");
407 SHOW("successfully read first page of core");
412 if (val != CORE_MAGIC) {
413 lose("invalid magic number in core: 0x%lx should have been 0x%x.\n",
417 SHOW("found CORE_MAGIC");
419 while (val != END_CORE_ENTRY_TYPE_CODE) {
422 remaining_len = len - 2; /* (-2 to cancel the two ++ operations) */
423 FSHOW((stderr, "/val=0x%"WORD_FMTX", remaining_len=0x%"WORD_FMTX"\n",
424 val, remaining_len));
428 case END_CORE_ENTRY_TYPE_CODE:
429 SHOW("END_CORE_ENTRY_TYPE_CODE case");
432 case VERSION_CORE_ENTRY_TYPE_CODE:
433 SHOW("VERSION_CORE_ENTRY_TYPE_CODE case");
434 if (*ptr != SBCL_CORE_VERSION_INTEGER) {
435 lose("core file version (%d) != runtime library version (%d)\n",
437 SBCL_CORE_VERSION_INTEGER);
441 case BUILD_ID_CORE_ENTRY_TYPE_CODE:
442 SHOW("BUILD_ID_CORE_ENTRY_TYPE_CODE case");
446 FSHOW((stderr, "build_id[]=\"%s\"\n", build_id));
447 FSHOW((stderr, "remaining_len = %d\n", remaining_len));
448 if (remaining_len != strlen((const char *)build_id))
449 goto losing_build_id;
450 for (i = 0; i < remaining_len; ++i) {
451 FSHOW((stderr, "ptr[%d] = char = %d, expected=%d\n",
452 i, ptr[i], build_id[i]));
453 if (ptr[i] != build_id[i])
454 goto losing_build_id;
458 /* .core files are not binary-compatible between
459 * builds because we can't easily detect whether the
460 * sources were patched between the time the
461 * dumping-the-.core runtime was built and the time
462 * that the loading-the-.core runtime was built.
464 * (We could easily detect whether version.lisp-expr
465 * was changed, but people experimenting with patches
466 * don't necessarily update version.lisp-expr.) */
468 lose("can't load .core for different runtime, sorry\n");
471 case NEW_DIRECTORY_CORE_ENTRY_TYPE_CODE:
472 SHOW("NEW_DIRECTORY_CORE_ENTRY_TYPE_CODE case");
473 process_directory(fd,
475 #ifndef LISP_FEATURE_ALPHA
476 remaining_len / (sizeof(struct ndir_entry) /
479 remaining_len / (sizeof(struct ndir_entry) /
485 case INITIAL_FUN_CORE_ENTRY_TYPE_CODE:
486 SHOW("INITIAL_FUN_CORE_ENTRY_TYPE_CODE case");
487 initial_function = (lispobj)*ptr;
490 #ifdef LISP_FEATURE_GENCGC
491 case PAGE_TABLE_CORE_ENTRY_TYPE_CODE:
493 os_vm_size_t size = *ptr;
494 os_vm_size_t fdoffset = (*(ptr+1) + 1) * (os_vm_page_size);
495 page_index_t offset = 0;
499 lseek(fd, fdoffset + file_offset, SEEK_SET);
500 while ((bytes_read = read(fd, data, (size < 4096 ? size : 4096 )))
506 bytes_read -= sizeof(word_t);
507 /* Ignore all zeroes. The size of the page table
508 * core entry was rounded up to os_vm_page_size
509 * during the save, and might now have more
510 * elements than the page table.
512 * The low bits of each word are allocation flags.
514 if ((word=data[i])) {
515 page_table[offset].region_start_offset = word & ~0x03;
516 page_table[offset].allocated = word & 0x03;
523 gencgc_partial_pickup = 1;
528 lose("unknown core file entry: 0x%"WORD_FMTX"\n", val);
531 ptr += remaining_len;
532 FSHOW((stderr, "/new ptr=0x%"WORD_FTMX"\n", ptr));
534 SHOW("about to free(header)");
536 SHOW("returning from load_core_file(..)");
537 return initial_function;