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)
118 if (lseek(fd, -lispobj_size, SEEK_END) < 0)
120 if (read(fd, &header, (size_t)lispobj_size) < lispobj_size)
123 if (header == CORE_MAGIC) {
124 if (lseek(fd, -trailer_size, SEEK_END) < 0)
126 if (read(fd, &core_start, sizeof(os_vm_offset_t)) < 0)
129 if (lseek(fd, core_start, SEEK_SET) < 0)
131 pos = lseek(fd, 0, SEEK_CUR);
133 if (read(fd, &header, (size_t)lispobj_size) < lispobj_size)
136 if (header != CORE_MAGIC)
139 maybe_initialize_runtime_options(fd);
152 /* If more platforms doesn't support overlapping mmap rename this
153 * def to something like ifdef nommapoverlap */
154 /* currently hpux only */
155 #ifdef LISP_FEATURE_HPUX
156 os_vm_address_t copy_core_bytes(int fd, os_vm_offset_t offset,
157 os_vm_address_t addr, int len)
159 unsigned char buf[4096];
161 int old_fd = lseek(fd, 0, SEEK_CUR);
164 fprintf(stderr, "cant copy a slice of core because slice-length is not of page size(4096)\n");
168 fprintf(stderr, "cant perform lseek() on corefile\n");
170 lseek(fd, offset, SEEK_SET);
172 fprintf(stderr, "cant perform lseek(%u,%lu,SEEK_SET) on corefile\n", fd, offset);
174 for(x = 0; x < len; x += 4096){
175 c = read(fd, buf, 4096);
177 fprintf(stderr, "cant read memory area from corefile at position %lu, got %d\n", offset + x, c);
180 memcpy(addr+x, buf, 4096);
182 os_flush_icache(addr, len);
188 process_directory(int fd, lispobj *ptr, int count, os_vm_offset_t file_offset)
190 struct ndir_entry *entry;
192 FSHOW((stderr, "/process_directory(..), count=%d\n", count));
194 for (entry = (struct ndir_entry *) ptr; --count>= 0; ++entry) {
196 long id = entry->identifier;
197 long offset = os_vm_page_size * (1 + entry->data_page);
198 os_vm_address_t addr =
199 (os_vm_address_t) (os_vm_page_size * entry->address);
200 lispobj *free_pointer = (lispobj *) addr + entry->nwords;
201 unsigned long len = os_vm_page_size * entry->page_count;
203 os_vm_address_t real_addr;
204 FSHOW((stderr, "/mapping %ld(0x%lx) bytes at 0x%lx\n",
205 (long)len, (long)len, (unsigned long)addr));
206 #ifdef LISP_FEATURE_HPUX
207 real_addr = copy_core_bytes(fd, offset + file_offset, addr, len);
209 real_addr = os_map(fd, offset + file_offset, addr, len);
211 if (real_addr != addr) {
212 lose("file mapped in wrong place! "
213 "(0x%08x != 0x%08lx)\n",
219 FSHOW((stderr, "/space id = %ld, free pointer = 0x%lx\n",
220 id, (unsigned long)free_pointer));
223 case DYNAMIC_CORE_SPACE_ID:
224 if (len > dynamic_space_size) {
226 "dynamic space too small for core: %ldKiB required, %ldKiB available.\n",
228 (long)dynamic_space_size >> 10);
231 #ifdef LISP_FEATURE_GENCGC
232 if (addr != (os_vm_address_t)DYNAMIC_SPACE_START) {
233 fprintf(stderr, "in core: 0x%lx; in runtime: 0x%lx \n",
234 (long)addr, (long)DYNAMIC_SPACE_START);
235 lose("core/runtime address mismatch: DYNAMIC_SPACE_START\n");
238 if ((addr != (os_vm_address_t)DYNAMIC_0_SPACE_START) &&
239 (addr != (os_vm_address_t)DYNAMIC_1_SPACE_START)) {
240 fprintf(stderr, "in core: 0x%lx; in runtime: 0x%lx or 0x%lx\n",
242 (long)DYNAMIC_0_SPACE_START,
243 (long)DYNAMIC_1_SPACE_START);
244 lose("warning: core/runtime address mismatch: DYNAMIC_SPACE_START\n");
247 #if defined(ALLOCATION_POINTER)
248 SetSymbolValue(ALLOCATION_POINTER, (lispobj)free_pointer,0);
250 dynamic_space_free_pointer = free_pointer;
252 /* For stop-and-copy GC, this will be whatever the GC was
253 * using at the time. With GENCGC, this will always be
254 * space 0. (We checked above that for GENCGC,
255 * addr==DYNAMIC_SPACE_START.) */
256 current_dynamic_space = (lispobj *)addr;
258 case STATIC_CORE_SPACE_ID:
259 if (addr != (os_vm_address_t)STATIC_SPACE_START) {
260 fprintf(stderr, "in core: 0x%lx - in runtime: 0x%lx\n",
261 (long)addr, (long)STATIC_SPACE_START);
262 lose("core/runtime address mismatch: STATIC_SPACE_START\n");
265 case READ_ONLY_CORE_SPACE_ID:
266 if (addr != (os_vm_address_t)READ_ONLY_SPACE_START) {
267 fprintf(stderr, "in core: 0x%lx - in runtime: 0x%lx\n",
268 (long)addr, (long)READ_ONLY_SPACE_START);
269 lose("core/runtime address mismatch: READ_ONLY_SPACE_START\n");
273 lose("unknown space ID %ld addr 0x%lx\n", id, (long)addr);
279 load_core_file(char *file, os_vm_offset_t file_offset)
281 lispobj *header, val, len, *ptr, remaining_len;
282 int fd = open_binary(file, O_RDONLY);
285 lispobj initial_function = NIL;
286 FSHOW((stderr, "/entering load_core_file(%s)\n", file));
288 fprintf(stderr, "could not open file \"%s\"\n", file);
293 lseek(fd, file_offset, SEEK_SET);
294 header = calloc(os_vm_page_size / sizeof(u32), sizeof(u32));
296 count = read(fd, header, os_vm_page_size);
297 if (count < os_vm_page_size) {
298 lose("premature end of core file\n");
300 SHOW("successfully read first page of core");
305 if (val != CORE_MAGIC) {
306 lose("invalid magic number in core: 0x%lx should have been 0x%x.\n",
310 SHOW("found CORE_MAGIC");
312 while (val != END_CORE_ENTRY_TYPE_CODE) {
315 remaining_len = len - 2; /* (-2 to cancel the two ++ operations) */
316 FSHOW((stderr, "/val=0x%ld, remaining_len=0x%ld\n",
317 (long)val, (long)remaining_len));
321 case END_CORE_ENTRY_TYPE_CODE:
322 SHOW("END_CORE_ENTRY_TYPE_CODE case");
325 case VERSION_CORE_ENTRY_TYPE_CODE:
326 SHOW("VERSION_CORE_ENTRY_TYPE_CODE case");
327 if (*ptr != SBCL_CORE_VERSION_INTEGER) {
328 lose("core file version (%d) != runtime library version (%d)\n",
330 SBCL_CORE_VERSION_INTEGER);
334 case BUILD_ID_CORE_ENTRY_TYPE_CODE:
335 SHOW("BUILD_ID_CORE_ENTRY_TYPE_CODE case");
339 FSHOW((stderr, "build_id[]=\"%s\"\n", build_id));
340 FSHOW((stderr, "remaining_len = %d\n", remaining_len));
341 if (remaining_len != strlen((const char *)build_id))
342 goto losing_build_id;
343 for (i = 0; i < remaining_len; ++i) {
344 FSHOW((stderr, "ptr[%d] = char = %d, expected=%d\n",
345 i, ptr[i], build_id[i]));
346 if (ptr[i] != build_id[i])
347 goto losing_build_id;
351 /* .core files are not binary-compatible between
352 * builds because we can't easily detect whether the
353 * sources were patched between the time the
354 * dumping-the-.core runtime was built and the time
355 * that the loading-the-.core runtime was built.
357 * (We could easily detect whether version.lisp-expr
358 * was changed, but people experimenting with patches
359 * don't necessarily update version.lisp-expr.) */
361 lose("can't load .core for different runtime, sorry\n");
364 case NEW_DIRECTORY_CORE_ENTRY_TYPE_CODE:
365 SHOW("NEW_DIRECTORY_CORE_ENTRY_TYPE_CODE case");
366 process_directory(fd,
368 #ifndef LISP_FEATURE_ALPHA
369 remaining_len / (sizeof(struct ndir_entry) /
372 remaining_len / (sizeof(struct ndir_entry) /
378 case INITIAL_FUN_CORE_ENTRY_TYPE_CODE:
379 SHOW("INITIAL_FUN_CORE_ENTRY_TYPE_CODE case");
380 initial_function = (lispobj)*ptr;
383 #if defined(LISP_FEATURE_SB_THREAD) && defined(LISP_FEATURE_SB_LUTEX)
384 case LUTEX_TABLE_CORE_ENTRY_TYPE_CODE:
385 SHOW("LUTEX_TABLE_CORE_ENTRY_TYPE_CODE case");
387 size_t n_lutexes = *ptr;
388 size_t fdoffset = (*(ptr + 1) + 1) * (os_vm_page_size);
389 size_t data_length = n_lutexes * sizeof(struct sap *);
390 struct lutex **lutexes_to_resurrect = malloc(data_length);
393 lseek(fd, fdoffset + file_offset, SEEK_SET);
395 FSHOW((stderr, "attempting to read %ld lutexes from core\n", n_lutexes));
396 bytes_read = read(fd, lutexes_to_resurrect, data_length);
399 if (bytes_read != data_length) {
400 lose("Could not read the lutex table");
405 for (i=0; i<n_lutexes; ++i) {
406 struct lutex *lutex = lutexes_to_resurrect[i];
408 FSHOW((stderr, "re-init'ing lutex @ %p\n", lutex));
409 lutex_init((tagged_lutex_t) lutex);
412 free(lutexes_to_resurrect);
418 #ifdef LISP_FEATURE_GENCGC
419 case PAGE_TABLE_CORE_ENTRY_TYPE_CODE:
422 size_t fdoffset = (*(ptr+1) + 1) * (os_vm_page_size);
425 unsigned long data[4096];
427 lseek(fd, fdoffset + file_offset, SEEK_SET);
428 while ((bytes_read = read(fd, data, (size < 4096 ? size : 4096 )))
434 bytes_read -= sizeof(long);
435 /* Ignore all zeroes. The size of the page table
436 * core entry was rounded up to os_vm_page_size
437 * during the save, and might now have more
438 * elements than the page table.
440 * The low bits of each word are allocation flags.
443 page_table[offset].region_start_offset = word & ~0x03;
444 page_table[offset].allocated = word & 0x03;
451 gencgc_partial_pickup = 1;
456 lose("unknown core file entry: %ld\n", (long)val);
459 ptr += remaining_len;
460 FSHOW((stderr, "/new ptr=%lx\n", (unsigned long)ptr));
462 SHOW("about to free(header)");
464 SHOW("returning from load_core_file(..)");
465 return initial_function;