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 off_t end_offset = sizeof(lispobj) +
87 sizeof(os_vm_offset_t) +
88 (RUNTIME_OPTIONS_WORDS * sizeof(size_t));
90 lseek(fd, -end_offset, SEEK_END);
91 runtime_options = read_runtime_options(fd);
94 /* Search 'filename' for an embedded core. An SBCL core has, at the
95 * end of the file, a trailer containing optional saved runtime
96 * options, the start of the core (an os_vm_offset_t), and a final
97 * signature word (the lispobj CORE_MAGIC). If this trailer is found
98 * at the end of the file, the start of the core can be determined
101 * If an embedded core is present, this returns the offset into the
102 * file to load the core from, or -1 if no core is present. */
104 search_for_embedded_core(char *filename)
107 os_vm_offset_t lispobj_size = sizeof(lispobj);
108 os_vm_offset_t trailer_size = lispobj_size + sizeof(os_vm_offset_t);
109 os_vm_offset_t core_start, pos;
112 if ((fd = open_binary(filename, O_RDONLY)) < 0)
114 if (lseek(fd, -lispobj_size, SEEK_END) < 0)
116 if (read(fd, &header, (size_t)lispobj_size) < lispobj_size)
119 if (header == CORE_MAGIC) {
120 if (lseek(fd, -trailer_size, SEEK_END) < 0)
122 if (read(fd, &core_start, sizeof(os_vm_offset_t)) < 0)
125 if (lseek(fd, core_start, SEEK_SET) < 0)
127 pos = lseek(fd, 0, SEEK_CUR);
129 if (read(fd, &header, (size_t)lispobj_size) < lispobj_size)
132 if (header != CORE_MAGIC)
135 maybe_initialize_runtime_options(fd);
148 /* If more platforms doesn't support overlapping mmap rename this
149 * def to something like ifdef nommapoverlap */
150 /* currently hpux only */
151 #ifdef LISP_FEATURE_HPUX
152 os_vm_address_t copy_core_bytes(int fd, os_vm_offset_t offset,
153 os_vm_address_t addr, int len)
155 unsigned char buf[4096];
157 int old_fd = lseek(fd, 0, SEEK_CUR);
160 fprintf(stderr, "cant copy a slice of core because slice-length is not of page size(4096)\n");
164 fprintf(stderr, "cant perform lseek() on corefile\n");
166 lseek(fd, offset, SEEK_SET);
168 fprintf(stderr, "cant perform lseek(%u,%lu,SEEK_SET) on corefile\n", fd, offset);
170 for(x = 0; x < len; x += 4096){
171 c = read(fd, buf, 4096);
173 fprintf(stderr, "cant read memory area from corefile at position %lu, got %d\n", offset + x, c);
176 memcpy(addr+x, buf, 4096);
178 os_flush_icache(addr, len);
184 process_directory(int fd, lispobj *ptr, int count, os_vm_offset_t file_offset)
186 struct ndir_entry *entry;
188 FSHOW((stderr, "/process_directory(..), count=%d\n", count));
190 for (entry = (struct ndir_entry *) ptr; --count>= 0; ++entry) {
192 long id = entry->identifier;
193 long offset = os_vm_page_size * (1 + entry->data_page);
194 os_vm_address_t addr =
195 (os_vm_address_t) (os_vm_page_size * entry->address);
196 lispobj *free_pointer = (lispobj *) addr + entry->nwords;
197 unsigned long len = os_vm_page_size * entry->page_count;
199 os_vm_address_t real_addr;
200 FSHOW((stderr, "/mapping %ld(0x%lx) bytes at 0x%lx\n",
201 (long)len, (long)len, (unsigned long)addr));
202 #ifdef LISP_FEATURE_HPUX
203 real_addr = copy_core_bytes(fd, offset + file_offset, addr, len);
205 real_addr = os_map(fd, offset + file_offset, addr, len);
207 if (real_addr != addr) {
208 lose("file mapped in wrong place! "
209 "(0x%08x != 0x%08lx)\n",
215 FSHOW((stderr, "/space id = %ld, free pointer = 0x%lx\n",
216 id, (unsigned long)free_pointer));
219 case DYNAMIC_CORE_SPACE_ID:
220 if (len > dynamic_space_size) {
222 "dynamic space too small for core: %ldKiB required, %ldKiB available.\n",
224 (long)dynamic_space_size >> 10);
227 #ifdef LISP_FEATURE_GENCGC
228 if (addr != (os_vm_address_t)DYNAMIC_SPACE_START) {
229 fprintf(stderr, "in core: 0x%lx; in runtime: 0x%lx \n",
230 (long)addr, (long)DYNAMIC_SPACE_START);
231 lose("core/runtime address mismatch: DYNAMIC_SPACE_START\n");
234 if ((addr != (os_vm_address_t)DYNAMIC_0_SPACE_START) &&
235 (addr != (os_vm_address_t)DYNAMIC_1_SPACE_START)) {
236 fprintf(stderr, "in core: 0x%lx; in runtime: 0x%lx or 0x%lx\n",
238 (long)DYNAMIC_0_SPACE_START,
239 (long)DYNAMIC_1_SPACE_START);
240 lose("warning: core/runtime address mismatch: DYNAMIC_SPACE_START\n");
243 #if defined(ALLOCATION_POINTER)
244 SetSymbolValue(ALLOCATION_POINTER, (lispobj)free_pointer,0);
246 dynamic_space_free_pointer = free_pointer;
248 /* For stop-and-copy GC, this will be whatever the GC was
249 * using at the time. With GENCGC, this will always be
250 * space 0. (We checked above that for GENCGC,
251 * addr==DYNAMIC_SPACE_START.) */
252 current_dynamic_space = (lispobj *)addr;
254 case STATIC_CORE_SPACE_ID:
255 if (addr != (os_vm_address_t)STATIC_SPACE_START) {
256 fprintf(stderr, "in core: 0x%lx - in runtime: 0x%lx\n",
257 (long)addr, (long)STATIC_SPACE_START);
258 lose("core/runtime address mismatch: STATIC_SPACE_START\n");
261 case READ_ONLY_CORE_SPACE_ID:
262 if (addr != (os_vm_address_t)READ_ONLY_SPACE_START) {
263 fprintf(stderr, "in core: 0x%lx - in runtime: 0x%lx\n",
264 (long)addr, (long)READ_ONLY_SPACE_START);
265 lose("core/runtime address mismatch: READ_ONLY_SPACE_START\n");
269 lose("unknown space ID %ld addr 0x%lx\n", id, (long)addr);
275 load_core_file(char *file, os_vm_offset_t file_offset)
277 lispobj *header, val, len, *ptr, remaining_len;
278 int fd = open_binary(file, O_RDONLY);
281 lispobj initial_function = NIL;
282 FSHOW((stderr, "/entering load_core_file(%s)\n", file));
284 fprintf(stderr, "could not open file \"%s\"\n", file);
289 lseek(fd, file_offset, SEEK_SET);
290 header = calloc(os_vm_page_size / sizeof(u32), sizeof(u32));
292 count = read(fd, header, os_vm_page_size);
293 if (count < os_vm_page_size) {
294 lose("premature end of core file\n");
296 SHOW("successfully read first page of core");
301 if (val != CORE_MAGIC) {
302 lose("invalid magic number in core: 0x%lx should have been 0x%x.\n",
306 SHOW("found CORE_MAGIC");
308 while (val != END_CORE_ENTRY_TYPE_CODE) {
311 remaining_len = len - 2; /* (-2 to cancel the two ++ operations) */
312 FSHOW((stderr, "/val=0x%ld, remaining_len=0x%ld\n",
313 (long)val, (long)remaining_len));
317 case END_CORE_ENTRY_TYPE_CODE:
318 SHOW("END_CORE_ENTRY_TYPE_CODE case");
321 case VERSION_CORE_ENTRY_TYPE_CODE:
322 SHOW("VERSION_CORE_ENTRY_TYPE_CODE case");
323 if (*ptr != SBCL_CORE_VERSION_INTEGER) {
324 lose("core file version (%d) != runtime library version (%d)\n",
326 SBCL_CORE_VERSION_INTEGER);
330 case BUILD_ID_CORE_ENTRY_TYPE_CODE:
331 SHOW("BUILD_ID_CORE_ENTRY_TYPE_CODE case");
335 FSHOW((stderr, "build_id[]=\"%s\"\n", build_id));
336 FSHOW((stderr, "remaining_len = %d\n", remaining_len));
337 if (remaining_len != strlen((const char *)build_id))
338 goto losing_build_id;
339 for (i = 0; i < remaining_len; ++i) {
340 FSHOW((stderr, "ptr[%d] = char = %d, expected=%d\n",
341 i, ptr[i], build_id[i]));
342 if (ptr[i] != build_id[i])
343 goto losing_build_id;
347 /* .core files are not binary-compatible between
348 * builds because we can't easily detect whether the
349 * sources were patched between the time the
350 * dumping-the-.core runtime was built and the time
351 * that the loading-the-.core runtime was built.
353 * (We could easily detect whether version.lisp-expr
354 * was changed, but people experimenting with patches
355 * don't necessarily update version.lisp-expr.) */
357 lose("can't load .core for different runtime, sorry\n");
360 case NEW_DIRECTORY_CORE_ENTRY_TYPE_CODE:
361 SHOW("NEW_DIRECTORY_CORE_ENTRY_TYPE_CODE case");
362 process_directory(fd,
364 #ifndef LISP_FEATURE_ALPHA
365 remaining_len / (sizeof(struct ndir_entry) /
368 remaining_len / (sizeof(struct ndir_entry) /
374 case INITIAL_FUN_CORE_ENTRY_TYPE_CODE:
375 SHOW("INITIAL_FUN_CORE_ENTRY_TYPE_CODE case");
376 initial_function = (lispobj)*ptr;
379 #if defined(LISP_FEATURE_SB_THREAD) && defined(LISP_FEATURE_SB_LUTEX)
380 case LUTEX_TABLE_CORE_ENTRY_TYPE_CODE:
381 SHOW("LUTEX_TABLE_CORE_ENTRY_TYPE_CODE case");
383 size_t n_lutexes = *ptr;
384 size_t fdoffset = (*(ptr + 1) + 1) * (os_vm_page_size);
385 size_t data_length = n_lutexes * sizeof(struct sap *);
386 struct lutex **lutexes_to_resurrect = malloc(data_length);
389 lseek(fd, fdoffset + file_offset, SEEK_SET);
391 FSHOW((stderr, "attempting to read %ld lutexes from core\n", n_lutexes));
392 bytes_read = read(fd, lutexes_to_resurrect, data_length);
395 if (bytes_read != data_length) {
396 lose("Could not read the lutex table");
401 for (i=0; i<n_lutexes; ++i) {
402 struct lutex *lutex = lutexes_to_resurrect[i];
404 FSHOW((stderr, "re-init'ing lutex @ %p\n", lutex));
405 lutex_init((tagged_lutex_t) lutex);
408 free(lutexes_to_resurrect);
414 #ifdef LISP_FEATURE_GENCGC
415 case PAGE_TABLE_CORE_ENTRY_TYPE_CODE:
418 size_t fdoffset = (*(ptr+1) + 1) * (os_vm_page_size);
421 unsigned long data[4096];
422 lseek(fd, fdoffset + file_offset, SEEK_SET);
423 while ((bytes_read = read(fd, data, (size < 4096 ? size : 4096 )))
429 bytes_read -= sizeof(long);
430 /* Ignore all zeroes. The size of the page table
431 * core entry was rounded up to os_vm_page_size
432 * during the save, and might now have more
433 * elements than the page table.
436 page_table[offset].region_start_offset = data[i];
443 gencgc_partial_pickup = 1;
448 lose("unknown core file entry: %ld\n", (long)val);
451 ptr += remaining_len;
452 FSHOW((stderr, "/new ptr=%lx\n", (unsigned long)ptr));
454 SHOW("about to free(header)");
456 SHOW("returning from load_core_file(..)");
457 return initial_function;