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"
49 #ifdef LISP_FEATURE_SB_CORE_COMPRESSION
53 unsigned char build_id[] =
54 #include "../../output/build-id.tmp"
58 open_binary(char *filename, int mode)
60 #ifdef LISP_FEATURE_WIN32
64 return open(filename, mode);
68 static struct runtime_options *
69 read_runtime_options(int fd)
71 size_t optarray[RUNTIME_OPTIONS_WORDS];
72 struct runtime_options *options = NULL;
74 if (read(fd, optarray, RUNTIME_OPTIONS_WORDS * sizeof(size_t)) !=
75 RUNTIME_OPTIONS_WORDS * sizeof(size_t)) {
79 if ((RUNTIME_OPTIONS_MAGIC != optarray[0]) || (0 == optarray[1])) {
83 options = successful_malloc(sizeof(struct runtime_options));
85 options->dynamic_space_size = optarray[2];
86 options->thread_control_stack_size = optarray[3];
92 maybe_initialize_runtime_options(int fd)
94 struct runtime_options *new_runtime_options;
95 off_t end_offset = sizeof(lispobj) +
96 sizeof(os_vm_offset_t) +
97 (RUNTIME_OPTIONS_WORDS * sizeof(size_t));
99 lseek(fd, -end_offset, SEEK_END);
101 if ((new_runtime_options = read_runtime_options(fd))) {
102 runtime_options = new_runtime_options;
106 /* Search 'filename' for an embedded core. An SBCL core has, at the
107 * end of the file, a trailer containing optional saved runtime
108 * options, the start of the core (an os_vm_offset_t), and a final
109 * signature word (the lispobj CORE_MAGIC). If this trailer is found
110 * at the end of the file, the start of the core can be determined
111 * from the core size.
113 * If an embedded core is present, this returns the offset into the
114 * file to load the core from, or -1 if no core is present. */
116 search_for_embedded_core(char *filename)
119 os_vm_offset_t lispobj_size = sizeof(lispobj);
120 os_vm_offset_t trailer_size = lispobj_size + sizeof(os_vm_offset_t);
121 os_vm_offset_t core_start, pos;
124 if ((fd = open_binary(filename, O_RDONLY)) < 0)
127 if (read(fd, &header, (size_t)lispobj_size) < lispobj_size)
129 if (header == CORE_MAGIC) {
130 /* This file is a real core, not an embedded core. Return 0 to
131 * indicate where the core starts, and do not look for runtime
132 * options in this case. */
136 if (lseek(fd, -lispobj_size, SEEK_END) < 0)
138 if (read(fd, &header, (size_t)lispobj_size) < lispobj_size)
141 if (header == CORE_MAGIC) {
142 if (lseek(fd, -trailer_size, SEEK_END) < 0)
144 if (read(fd, &core_start, sizeof(os_vm_offset_t)) < 0)
147 if (lseek(fd, core_start, SEEK_SET) < 0)
149 pos = lseek(fd, 0, SEEK_CUR);
151 if (read(fd, &header, (size_t)lispobj_size) < lispobj_size)
154 if (header != CORE_MAGIC)
157 maybe_initialize_runtime_options(fd);
170 /* If more platforms doesn't support overlapping mmap rename this
171 * def to something like ifdef nommapoverlap */
172 /* currently hpux only */
173 #ifdef LISP_FEATURE_HPUX
174 os_vm_address_t copy_core_bytes(int fd, os_vm_offset_t offset,
175 os_vm_address_t addr, int len)
177 unsigned char buf[4096];
179 int old_fd = lseek(fd, 0, SEEK_CUR);
182 fprintf(stderr, "cant copy a slice of core because slice-length is not of page size(4096)\n");
186 fprintf(stderr, "cant perform lseek() on corefile\n");
188 lseek(fd, offset, SEEK_SET);
190 fprintf(stderr, "cant perform lseek(%u,%lu,SEEK_SET) on corefile\n", fd, offset);
192 for(x = 0; x < len; x += 4096){
193 c = read(fd, buf, 4096);
195 fprintf(stderr, "cant read memory area from corefile at position %lu, got %d\n", offset + x, c);
198 memcpy(addr+x, buf, 4096);
200 os_flush_icache(addr, len);
205 #ifdef LISP_FEATURE_SB_CORE_COMPRESSION
206 # define ZLIB_BUFFER_SIZE (1u<<16)
207 os_vm_address_t inflate_core_bytes(int fd, os_vm_offset_t offset,
208 os_vm_address_t addr, int len)
211 unsigned char buf[ZLIB_BUFFER_SIZE];
214 if (-1 == lseek(fd, offset, SEEK_SET)) {
215 lose("Unable to lseek() on corefile\n");
218 stream.zalloc = NULL;
220 stream.opaque = NULL;
222 stream.next_in = buf;
224 ret = inflateInit(&stream);
226 lose("zlib error %i\n", ret);
228 stream.next_out = (void*)addr;
229 stream.avail_out = len;
231 ssize_t count = read(fd, buf, sizeof(buf));
233 lose("unable to read core file (errno = %i)\n", errno);
234 stream.next_in = buf;
235 stream.avail_in = count;
236 if (count == 0) break;
237 ret = inflate(&stream, Z_NO_FLUSH);
242 if (stream.avail_out == 0)
243 lose("Runaway gzipped core directory... aborting\n");
244 if (stream.avail_in > 0)
245 lose("zlib inflate returned without fully"
246 "using up input buffer... aborting\n");
249 lose("zlib inflate error: %i\n", ret);
252 } while (ret != Z_STREAM_END);
254 if (stream.avail_out > 0) {
255 if (stream.avail_out >= os_vm_page_size)
256 fprintf(stderr, "Warning: gzipped core directory significantly"
257 "shorter than expected (%lu bytes)", (unsigned long)stream.avail_out);
258 /* Is this needed? */
259 memset(stream.next_out, 0, stream.avail_out);
265 # undef ZLIB_BUFFER_SIZE
268 int merge_core_pages = -1;
271 process_directory(int fd, lispobj *ptr, int count, os_vm_offset_t file_offset)
273 struct ndir_entry *entry;
276 FSHOW((stderr, "/process_directory(..), count=%d\n", count));
278 for (entry = (struct ndir_entry *) ptr; --count>= 0; ++entry) {
280 long id = entry->identifier;
281 if (id <= (MAX_CORE_SPACE_ID | DEFLATED_CORE_SPACE_ID_FLAG)) {
282 if (id & DEFLATED_CORE_SPACE_ID_FLAG)
284 id &= ~(DEFLATED_CORE_SPACE_ID_FLAG);
286 long offset = os_vm_page_size * (1 + entry->data_page);
287 os_vm_address_t addr =
288 (os_vm_address_t) (os_vm_page_size * entry->address);
289 lispobj *free_pointer = (lispobj *) addr + entry->nwords;
290 unsigned long len = os_vm_page_size * entry->page_count;
292 os_vm_address_t real_addr;
293 FSHOW((stderr, "/mapping %ld(0x%lx) bytes at 0x%lx\n",
294 (long)len, (long)len, (unsigned long)addr));
296 #ifdef LISP_FEATURE_SB_CORE_COMPRESSION
297 real_addr = inflate_core_bytes(fd, offset + file_offset, addr, len);
299 lose("This runtime was not built with zlib-compressed core support... aborting\n");
302 #ifdef LISP_FEATURE_HPUX
303 real_addr = copy_core_bytes(fd, offset + file_offset, addr, len);
305 real_addr = os_map(fd, offset + file_offset, addr, len);
308 if (real_addr != addr) {
309 lose("file mapped in wrong place! "
310 "(0x%08x != 0x%08lx)\n",
316 #ifdef MADV_MERGEABLE
317 if ((merge_core_pages == 1)
318 || ((merge_core_pages == -1) && compressed)) {
319 madvise(addr, len, MADV_MERGEABLE);
323 FSHOW((stderr, "/space id = %ld, free pointer = 0x%lx\n",
324 id, (unsigned long)free_pointer));
327 case DYNAMIC_CORE_SPACE_ID:
328 if (len > dynamic_space_size) {
330 "dynamic space too small for core: %ldKiB required, %ldKiB available.\n",
332 (long)dynamic_space_size >> 10);
335 #ifdef LISP_FEATURE_GENCGC
336 if (addr != (os_vm_address_t)DYNAMIC_SPACE_START) {
337 fprintf(stderr, "in core: 0x%lx; in runtime: 0x%lx \n",
338 (long)addr, (long)DYNAMIC_SPACE_START);
339 lose("core/runtime address mismatch: DYNAMIC_SPACE_START\n");
342 if ((addr != (os_vm_address_t)DYNAMIC_0_SPACE_START) &&
343 (addr != (os_vm_address_t)DYNAMIC_1_SPACE_START)) {
344 fprintf(stderr, "in core: 0x%lx; in runtime: 0x%lx or 0x%lx\n",
346 (long)DYNAMIC_0_SPACE_START,
347 (long)DYNAMIC_1_SPACE_START);
348 lose("warning: core/runtime address mismatch: DYNAMIC_SPACE_START\n");
351 #if defined(ALLOCATION_POINTER)
352 SetSymbolValue(ALLOCATION_POINTER, (lispobj)free_pointer,0);
354 dynamic_space_free_pointer = free_pointer;
356 /* For stop-and-copy GC, this will be whatever the GC was
357 * using at the time. With GENCGC, this will always be
358 * space 0. (We checked above that for GENCGC,
359 * addr==DYNAMIC_SPACE_START.) */
360 current_dynamic_space = (lispobj *)addr;
362 case STATIC_CORE_SPACE_ID:
363 if (addr != (os_vm_address_t)STATIC_SPACE_START) {
364 fprintf(stderr, "in core: 0x%lx - in runtime: 0x%lx\n",
365 (long)addr, (long)STATIC_SPACE_START);
366 lose("core/runtime address mismatch: STATIC_SPACE_START\n");
369 case READ_ONLY_CORE_SPACE_ID:
370 if (addr != (os_vm_address_t)READ_ONLY_SPACE_START) {
371 fprintf(stderr, "in core: 0x%lx - in runtime: 0x%lx\n",
372 (long)addr, (long)READ_ONLY_SPACE_START);
373 lose("core/runtime address mismatch: READ_ONLY_SPACE_START\n");
377 lose("unknown space ID %ld addr 0x%lx\n", id, (long)addr);
383 load_core_file(char *file, os_vm_offset_t file_offset)
385 lispobj *header, val, len, *ptr, remaining_len;
386 int fd = open_binary(file, O_RDONLY);
389 lispobj initial_function = NIL;
390 FSHOW((stderr, "/entering load_core_file(%s)\n", file));
392 fprintf(stderr, "could not open file \"%s\"\n", file);
397 lseek(fd, file_offset, SEEK_SET);
398 header = calloc(os_vm_page_size / sizeof(u32), sizeof(u32));
400 count = read(fd, header, os_vm_page_size);
401 if (count < os_vm_page_size) {
402 lose("premature end of core file\n");
404 SHOW("successfully read first page of core");
409 if (val != CORE_MAGIC) {
410 lose("invalid magic number in core: 0x%lx should have been 0x%x.\n",
414 SHOW("found CORE_MAGIC");
416 while (val != END_CORE_ENTRY_TYPE_CODE) {
419 remaining_len = len - 2; /* (-2 to cancel the two ++ operations) */
420 FSHOW((stderr, "/val=0x%ld, remaining_len=0x%ld\n",
421 (long)val, (long)remaining_len));
425 case END_CORE_ENTRY_TYPE_CODE:
426 SHOW("END_CORE_ENTRY_TYPE_CODE case");
429 case VERSION_CORE_ENTRY_TYPE_CODE:
430 SHOW("VERSION_CORE_ENTRY_TYPE_CODE case");
431 if (*ptr != SBCL_CORE_VERSION_INTEGER) {
432 lose("core file version (%d) != runtime library version (%d)\n",
434 SBCL_CORE_VERSION_INTEGER);
438 case BUILD_ID_CORE_ENTRY_TYPE_CODE:
439 SHOW("BUILD_ID_CORE_ENTRY_TYPE_CODE case");
443 FSHOW((stderr, "build_id[]=\"%s\"\n", build_id));
444 FSHOW((stderr, "remaining_len = %d\n", remaining_len));
445 if (remaining_len != strlen((const char *)build_id))
446 goto losing_build_id;
447 for (i = 0; i < remaining_len; ++i) {
448 FSHOW((stderr, "ptr[%d] = char = %d, expected=%d\n",
449 i, ptr[i], build_id[i]));
450 if (ptr[i] != build_id[i])
451 goto losing_build_id;
455 /* .core files are not binary-compatible between
456 * builds because we can't easily detect whether the
457 * sources were patched between the time the
458 * dumping-the-.core runtime was built and the time
459 * that the loading-the-.core runtime was built.
461 * (We could easily detect whether version.lisp-expr
462 * was changed, but people experimenting with patches
463 * don't necessarily update version.lisp-expr.) */
465 lose("can't load .core for different runtime, sorry\n");
468 case NEW_DIRECTORY_CORE_ENTRY_TYPE_CODE:
469 SHOW("NEW_DIRECTORY_CORE_ENTRY_TYPE_CODE case");
470 process_directory(fd,
472 #ifndef LISP_FEATURE_ALPHA
473 remaining_len / (sizeof(struct ndir_entry) /
476 remaining_len / (sizeof(struct ndir_entry) /
482 case INITIAL_FUN_CORE_ENTRY_TYPE_CODE:
483 SHOW("INITIAL_FUN_CORE_ENTRY_TYPE_CODE case");
484 initial_function = (lispobj)*ptr;
487 #ifdef LISP_FEATURE_GENCGC
488 case PAGE_TABLE_CORE_ENTRY_TYPE_CODE:
491 size_t fdoffset = (*(ptr+1) + 1) * (os_vm_page_size);
494 unsigned long data[4096];
496 lseek(fd, fdoffset + file_offset, SEEK_SET);
497 while ((bytes_read = read(fd, data, (size < 4096 ? size : 4096 )))
503 bytes_read -= sizeof(long);
504 /* Ignore all zeroes. The size of the page table
505 * core entry was rounded up to os_vm_page_size
506 * during the save, and might now have more
507 * elements than the page table.
509 * The low bits of each word are allocation flags.
512 page_table[offset].region_start_offset = word & ~0x03;
513 page_table[offset].allocated = word & 0x03;
520 gencgc_partial_pickup = 1;
525 lose("unknown core file entry: %ld\n", (long)val);
528 ptr += remaining_len;
529 FSHOW((stderr, "/new ptr=%lx\n", (unsigned long)ptr));
531 SHOW("about to free(header)");
533 SHOW("returning from load_core_file(..)");
534 return initial_function;