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>
36 #include "gc-internal.h"
39 #if defined(LISP_FEATURE_SB_THREAD) && defined(LISP_FEATURE_SB_LUTEX)
40 #include "genesis/sap.h"
41 #include "pthread-lutex.h"
45 unsigned char build_id[] =
46 #include "../../output/build-id.tmp"
50 open_binary(char *filename, int mode)
52 #ifdef LISP_FEATURE_WIN32
56 return open(filename, mode);
60 static struct runtime_options *
61 read_runtime_options(int fd)
63 size_t optarray[RUNTIME_OPTIONS_WORDS];
64 struct runtime_options *options = NULL;
66 if (read(fd, optarray, RUNTIME_OPTIONS_WORDS * sizeof(size_t)) !=
67 RUNTIME_OPTIONS_WORDS * sizeof(size_t)) {
71 if ((RUNTIME_OPTIONS_MAGIC != optarray[0]) || (0 == optarray[1])) {
75 options = successful_malloc(sizeof(struct runtime_options));
77 options->dynamic_space_size = optarray[2];
78 options->thread_control_stack_size = optarray[3];
84 maybe_initialize_runtime_options(int fd)
86 struct runtime_options *new_runtime_options;
87 off_t end_offset = sizeof(lispobj) +
88 sizeof(os_vm_offset_t) +
89 (RUNTIME_OPTIONS_WORDS * sizeof(size_t));
91 lseek(fd, -end_offset, SEEK_END);
93 if (new_runtime_options = read_runtime_options(fd)) {
94 runtime_options = new_runtime_options;
98 /* Search 'filename' for an embedded core. An SBCL core has, at the
99 * end of the file, a trailer containing optional saved runtime
100 * options, the start of the core (an os_vm_offset_t), and a final
101 * signature word (the lispobj CORE_MAGIC). If this trailer is found
102 * at the end of the file, the start of the core can be determined
103 * from the core size.
105 * If an embedded core is present, this returns the offset into the
106 * file to load the core from, or -1 if no core is present. */
108 search_for_embedded_core(char *filename)
111 os_vm_offset_t lispobj_size = sizeof(lispobj);
112 os_vm_offset_t trailer_size = lispobj_size + sizeof(os_vm_offset_t);
113 os_vm_offset_t core_start, pos;
116 if ((fd = open_binary(filename, O_RDONLY)) < 0)
119 if (read(fd, &header, (size_t)lispobj_size) < lispobj_size)
121 if (header == CORE_MAGIC) {
122 /* This file is a real core, not an embedded core. Return 0 to
123 * indicate where the core starts, and do not look for runtime
124 * options in this case. */
128 if (lseek(fd, -lispobj_size, SEEK_END) < 0)
130 if (read(fd, &header, (size_t)lispobj_size) < lispobj_size)
133 if (header == CORE_MAGIC) {
134 if (lseek(fd, -trailer_size, SEEK_END) < 0)
136 if (read(fd, &core_start, sizeof(os_vm_offset_t)) < 0)
139 if (lseek(fd, core_start, SEEK_SET) < 0)
141 pos = lseek(fd, 0, SEEK_CUR);
143 if (read(fd, &header, (size_t)lispobj_size) < lispobj_size)
146 if (header != CORE_MAGIC)
149 maybe_initialize_runtime_options(fd);
162 /* If more platforms doesn't support overlapping mmap rename this
163 * def to something like ifdef nommapoverlap */
164 /* currently hpux only */
165 #ifdef LISP_FEATURE_HPUX
166 os_vm_address_t copy_core_bytes(int fd, os_vm_offset_t offset,
167 os_vm_address_t addr, int len)
169 unsigned char buf[4096];
171 int old_fd = lseek(fd, 0, SEEK_CUR);
174 fprintf(stderr, "cant copy a slice of core because slice-length is not of page size(4096)\n");
178 fprintf(stderr, "cant perform lseek() on corefile\n");
180 lseek(fd, offset, SEEK_SET);
182 fprintf(stderr, "cant perform lseek(%u,%lu,SEEK_SET) on corefile\n", fd, offset);
184 for(x = 0; x < len; x += 4096){
185 c = read(fd, buf, 4096);
187 fprintf(stderr, "cant read memory area from corefile at position %lu, got %d\n", offset + x, c);
190 memcpy(addr+x, buf, 4096);
192 os_flush_icache(addr, len);
198 process_directory(int fd, lispobj *ptr, int count, os_vm_offset_t file_offset)
200 struct ndir_entry *entry;
202 FSHOW((stderr, "/process_directory(..), count=%d\n", count));
204 for (entry = (struct ndir_entry *) ptr; --count>= 0; ++entry) {
206 long id = entry->identifier;
207 long offset = os_vm_page_size * (1 + entry->data_page);
208 os_vm_address_t addr =
209 (os_vm_address_t) (os_vm_page_size * entry->address);
210 lispobj *free_pointer = (lispobj *) addr + entry->nwords;
211 unsigned long len = os_vm_page_size * entry->page_count;
213 os_vm_address_t real_addr;
214 FSHOW((stderr, "/mapping %ld(0x%lx) bytes at 0x%lx\n",
215 (long)len, (long)len, (unsigned long)addr));
216 #ifdef LISP_FEATURE_HPUX
217 real_addr = copy_core_bytes(fd, offset + file_offset, addr, len);
219 real_addr = os_map(fd, offset + file_offset, addr, len);
221 if (real_addr != addr) {
222 lose("file mapped in wrong place! "
223 "(0x%08x != 0x%08lx)\n",
229 FSHOW((stderr, "/space id = %ld, free pointer = 0x%lx\n",
230 id, (unsigned long)free_pointer));
233 case DYNAMIC_CORE_SPACE_ID:
234 if (len > dynamic_space_size) {
236 "dynamic space too small for core: %ldKiB required, %ldKiB available.\n",
238 (long)dynamic_space_size >> 10);
241 #ifdef LISP_FEATURE_GENCGC
242 if (addr != (os_vm_address_t)DYNAMIC_SPACE_START) {
243 fprintf(stderr, "in core: 0x%lx; in runtime: 0x%lx \n",
244 (long)addr, (long)DYNAMIC_SPACE_START);
245 lose("core/runtime address mismatch: DYNAMIC_SPACE_START\n");
248 if ((addr != (os_vm_address_t)DYNAMIC_0_SPACE_START) &&
249 (addr != (os_vm_address_t)DYNAMIC_1_SPACE_START)) {
250 fprintf(stderr, "in core: 0x%lx; in runtime: 0x%lx or 0x%lx\n",
252 (long)DYNAMIC_0_SPACE_START,
253 (long)DYNAMIC_1_SPACE_START);
254 lose("warning: core/runtime address mismatch: DYNAMIC_SPACE_START\n");
257 #if defined(ALLOCATION_POINTER)
258 SetSymbolValue(ALLOCATION_POINTER, (lispobj)free_pointer,0);
260 dynamic_space_free_pointer = free_pointer;
262 /* For stop-and-copy GC, this will be whatever the GC was
263 * using at the time. With GENCGC, this will always be
264 * space 0. (We checked above that for GENCGC,
265 * addr==DYNAMIC_SPACE_START.) */
266 current_dynamic_space = (lispobj *)addr;
268 case STATIC_CORE_SPACE_ID:
269 if (addr != (os_vm_address_t)STATIC_SPACE_START) {
270 fprintf(stderr, "in core: 0x%lx - in runtime: 0x%lx\n",
271 (long)addr, (long)STATIC_SPACE_START);
272 lose("core/runtime address mismatch: STATIC_SPACE_START\n");
275 case READ_ONLY_CORE_SPACE_ID:
276 if (addr != (os_vm_address_t)READ_ONLY_SPACE_START) {
277 fprintf(stderr, "in core: 0x%lx - in runtime: 0x%lx\n",
278 (long)addr, (long)READ_ONLY_SPACE_START);
279 lose("core/runtime address mismatch: READ_ONLY_SPACE_START\n");
283 lose("unknown space ID %ld addr 0x%lx\n", id, (long)addr);
289 load_core_file(char *file, os_vm_offset_t file_offset)
291 lispobj *header, val, len, *ptr, remaining_len;
292 int fd = open_binary(file, O_RDONLY);
295 lispobj initial_function = NIL;
296 FSHOW((stderr, "/entering load_core_file(%s)\n", file));
298 fprintf(stderr, "could not open file \"%s\"\n", file);
303 lseek(fd, file_offset, SEEK_SET);
304 header = calloc(os_vm_page_size / sizeof(u32), sizeof(u32));
306 count = read(fd, header, os_vm_page_size);
307 if (count < os_vm_page_size) {
308 lose("premature end of core file\n");
310 SHOW("successfully read first page of core");
315 if (val != CORE_MAGIC) {
316 lose("invalid magic number in core: 0x%lx should have been 0x%x.\n",
320 SHOW("found CORE_MAGIC");
322 while (val != END_CORE_ENTRY_TYPE_CODE) {
325 remaining_len = len - 2; /* (-2 to cancel the two ++ operations) */
326 FSHOW((stderr, "/val=0x%ld, remaining_len=0x%ld\n",
327 (long)val, (long)remaining_len));
331 case END_CORE_ENTRY_TYPE_CODE:
332 SHOW("END_CORE_ENTRY_TYPE_CODE case");
335 case VERSION_CORE_ENTRY_TYPE_CODE:
336 SHOW("VERSION_CORE_ENTRY_TYPE_CODE case");
337 if (*ptr != SBCL_CORE_VERSION_INTEGER) {
338 lose("core file version (%d) != runtime library version (%d)\n",
340 SBCL_CORE_VERSION_INTEGER);
344 case BUILD_ID_CORE_ENTRY_TYPE_CODE:
345 SHOW("BUILD_ID_CORE_ENTRY_TYPE_CODE case");
349 FSHOW((stderr, "build_id[]=\"%s\"\n", build_id));
350 FSHOW((stderr, "remaining_len = %d\n", remaining_len));
351 if (remaining_len != strlen((const char *)build_id))
352 goto losing_build_id;
353 for (i = 0; i < remaining_len; ++i) {
354 FSHOW((stderr, "ptr[%d] = char = %d, expected=%d\n",
355 i, ptr[i], build_id[i]));
356 if (ptr[i] != build_id[i])
357 goto losing_build_id;
361 /* .core files are not binary-compatible between
362 * builds because we can't easily detect whether the
363 * sources were patched between the time the
364 * dumping-the-.core runtime was built and the time
365 * that the loading-the-.core runtime was built.
367 * (We could easily detect whether version.lisp-expr
368 * was changed, but people experimenting with patches
369 * don't necessarily update version.lisp-expr.) */
371 lose("can't load .core for different runtime, sorry\n");
374 case NEW_DIRECTORY_CORE_ENTRY_TYPE_CODE:
375 SHOW("NEW_DIRECTORY_CORE_ENTRY_TYPE_CODE case");
376 process_directory(fd,
378 #ifndef LISP_FEATURE_ALPHA
379 remaining_len / (sizeof(struct ndir_entry) /
382 remaining_len / (sizeof(struct ndir_entry) /
388 case INITIAL_FUN_CORE_ENTRY_TYPE_CODE:
389 SHOW("INITIAL_FUN_CORE_ENTRY_TYPE_CODE case");
390 initial_function = (lispobj)*ptr;
393 #if defined(LISP_FEATURE_SB_THREAD) && defined(LISP_FEATURE_SB_LUTEX)
394 case LUTEX_TABLE_CORE_ENTRY_TYPE_CODE:
395 SHOW("LUTEX_TABLE_CORE_ENTRY_TYPE_CODE case");
397 size_t n_lutexes = *ptr;
398 size_t fdoffset = (*(ptr + 1) + 1) * (os_vm_page_size);
399 size_t data_length = n_lutexes * sizeof(struct sap *);
400 struct lutex **lutexes_to_resurrect = malloc(data_length);
403 lseek(fd, fdoffset + file_offset, SEEK_SET);
405 FSHOW((stderr, "attempting to read %ld lutexes from core\n", n_lutexes));
406 bytes_read = read(fd, lutexes_to_resurrect, data_length);
409 if (bytes_read != data_length) {
410 lose("Could not read the lutex table");
415 for (i=0; i<n_lutexes; ++i) {
416 struct lutex *lutex = lutexes_to_resurrect[i];
418 FSHOW((stderr, "re-init'ing lutex @ %p\n", lutex));
419 lutex_init((tagged_lutex_t) lutex);
422 free(lutexes_to_resurrect);
428 #ifdef LISP_FEATURE_GENCGC
429 case PAGE_TABLE_CORE_ENTRY_TYPE_CODE:
432 size_t fdoffset = (*(ptr+1) + 1) * (os_vm_page_size);
435 unsigned long data[4096];
437 lseek(fd, fdoffset + file_offset, SEEK_SET);
438 while ((bytes_read = read(fd, data, (size < 4096 ? size : 4096 )))
444 bytes_read -= sizeof(long);
445 /* Ignore all zeroes. The size of the page table
446 * core entry was rounded up to os_vm_page_size
447 * during the save, and might now have more
448 * elements than the page table.
450 * The low bits of each word are allocation flags.
453 page_table[offset].region_start_offset = word & ~0x03;
454 page_table[offset].allocated = word & 0x03;
461 gencgc_partial_pickup = 1;
466 lose("unknown core file entry: %ld\n", (long)val);
469 ptr += remaining_len;
470 FSHOW((stderr, "/new ptr=%lx\n", (unsigned long)ptr));
472 SHOW("about to free(header)");
474 SHOW("returning from load_core_file(..)");
475 return initial_function;