56daffda349db04f04c0798e5455ca2dcf915094
[sbcl.git] / src / runtime / coreparse.c
1 /*
2  * A saved SBCL system is a .core file; the code here helps us accept
3  * such a file as input.
4  */
5
6 /*
7  * This software is part of the SBCL system. See the README file for
8  * more information.
9  *
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.
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <sys/file.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24 #include <unistd.h>
25
26 #include "sbcl.h"
27 #include "os.h"
28 #include "runtime.h"
29 #include "globals.h"
30 #include "core.h"
31 #include "arch.h"
32 #include "interr.h"
33 #include "thread.h"
34
35 #include "validate.h"
36 #include "gc-internal.h"
37
38 /* lutex stuff */
39 #if defined(LISP_FEATURE_SB_THREAD) && defined(LISP_FEATURE_SB_LUTEX)
40 #include "genesis/sap.h"
41 #include "pthread-lutex.h"
42 #endif
43
44
45 unsigned char build_id[] =
46 #include "../../output/build-id.tmp"
47 ;
48
49 int
50 open_binary(char *filename, int mode)
51 {
52 #ifdef LISP_FEATURE_WIN32
53     mode |= O_BINARY;
54 #endif
55
56     return open(filename, mode);
57 }
58
59
60 static struct runtime_options *
61 read_runtime_options(int fd)
62 {
63     size_t optarray[RUNTIME_OPTIONS_WORDS];
64     struct runtime_options *options = NULL;
65
66     if (read(fd, optarray, RUNTIME_OPTIONS_WORDS * sizeof(size_t)) !=
67         RUNTIME_OPTIONS_WORDS * sizeof(size_t)) {
68         return NULL;
69     }
70
71     if ((RUNTIME_OPTIONS_MAGIC != optarray[0]) || (0 == optarray[1])) {
72         return NULL;
73     }
74
75     options = successful_malloc(sizeof(struct runtime_options));
76
77     options->dynamic_space_size = optarray[2];
78     options->thread_control_stack_size = optarray[3];
79
80     return options;
81 }
82
83 void
84 maybe_initialize_runtime_options(int fd)
85 {
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));
90
91     lseek(fd, -end_offset, SEEK_END);
92
93     if (new_runtime_options = read_runtime_options(fd)) {
94         runtime_options = new_runtime_options;
95     }
96 }
97
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.
104  *
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. */
107 os_vm_offset_t
108 search_for_embedded_core(char *filename)
109 {
110     lispobj header;
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;
114     int fd = -1;
115
116     if ((fd = open_binary(filename, O_RDONLY)) < 0)
117         goto lose;
118
119     if (read(fd, &header, (size_t)lispobj_size) < lispobj_size)
120         goto lose;
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. */
125         return 0;
126     }
127
128     if (lseek(fd, -lispobj_size, SEEK_END) < 0)
129         goto lose;
130     if (read(fd, &header, (size_t)lispobj_size) < lispobj_size)
131         goto lose;
132
133     if (header == CORE_MAGIC) {
134         if (lseek(fd, -trailer_size, SEEK_END) < 0)
135             goto lose;
136         if (read(fd, &core_start, sizeof(os_vm_offset_t)) < 0)
137             goto lose;
138
139         if (lseek(fd, core_start, SEEK_SET) < 0)
140             goto lose;
141         pos = lseek(fd, 0, SEEK_CUR);
142
143         if (read(fd, &header, (size_t)lispobj_size) < lispobj_size)
144             goto lose;
145
146         if (header != CORE_MAGIC)
147             goto lose;
148
149         maybe_initialize_runtime_options(fd);
150
151         close(fd);
152         return pos;
153     }
154
155 lose:
156     if (fd != -1)
157         close(fd);
158
159     return -1;
160 }
161
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)
168 {
169   unsigned char buf[4096];
170   int c,x;
171   int old_fd = lseek(fd, 0, SEEK_CUR);
172
173   if(len & (4096-1)){
174     fprintf(stderr, "cant copy a slice of core because slice-length is not of page size(4096)\n");
175     exit(-1);
176   }
177   if(old_fd < 0){
178     fprintf(stderr, "cant perform lseek() on corefile\n");
179   }
180   lseek(fd, offset, SEEK_SET);
181   if(fd < 0){
182     fprintf(stderr, "cant perform lseek(%u,%lu,SEEK_SET) on corefile\n", fd, offset);
183   }
184   for(x = 0; x < len; x += 4096){
185     c = read(fd, buf, 4096);
186     if(c != 4096){
187       fprintf(stderr, "cant read memory area from corefile at position %lu, got %d\n", offset + x, c);
188       exit(-1);
189     }
190     memcpy(addr+x, buf, 4096);
191   }
192   os_flush_icache(addr, len);
193   return addr;
194 }
195 #endif
196
197 static void
198 process_directory(int fd, lispobj *ptr, int count, os_vm_offset_t file_offset)
199 {
200     struct ndir_entry *entry;
201
202     FSHOW((stderr, "/process_directory(..), count=%d\n", count));
203
204     for (entry = (struct ndir_entry *) ptr; --count>= 0; ++entry) {
205
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;
212         if (len != 0) {
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);
218 #else
219             real_addr = os_map(fd, offset + file_offset, addr, len);
220 #endif
221             if (real_addr != addr) {
222                 lose("file mapped in wrong place! "
223                      "(0x%08x != 0x%08lx)\n",
224                      real_addr,
225                      addr);
226             }
227         }
228
229         FSHOW((stderr, "/space id = %ld, free pointer = 0x%lx\n",
230                id, (unsigned long)free_pointer));
231
232         switch (id) {
233         case DYNAMIC_CORE_SPACE_ID:
234             if (len > dynamic_space_size) {
235                 fprintf(stderr,
236                         "dynamic space too small for core: %ldKiB required, %ldKiB available.\n",
237                         len >> 10,
238                         (long)dynamic_space_size >> 10);
239                 exit(1);
240             }
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");
246             }
247 #else
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",
251                         (long)addr,
252                         (long)DYNAMIC_0_SPACE_START,
253                         (long)DYNAMIC_1_SPACE_START);
254                 lose("warning: core/runtime address mismatch: DYNAMIC_SPACE_START\n");
255             }
256 #endif
257 #if defined(ALLOCATION_POINTER)
258             SetSymbolValue(ALLOCATION_POINTER, (lispobj)free_pointer,0);
259 #else
260             dynamic_space_free_pointer = free_pointer;
261 #endif
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;
267             break;
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");
273             }
274             break;
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");
280             }
281             break;
282         default:
283             lose("unknown space ID %ld addr 0x%lx\n", id, (long)addr);
284         }
285     }
286 }
287
288 lispobj
289 load_core_file(char *file, os_vm_offset_t file_offset)
290 {
291     lispobj *header, val, len, *ptr, remaining_len;
292     int fd = open_binary(file, O_RDONLY);
293     unsigned int count;
294
295     lispobj initial_function = NIL;
296     FSHOW((stderr, "/entering load_core_file(%s)\n", file));
297     if (fd < 0) {
298         fprintf(stderr, "could not open file \"%s\"\n", file);
299         perror("open");
300         exit(1);
301     }
302
303     lseek(fd, file_offset, SEEK_SET);
304     header = calloc(os_vm_page_size / sizeof(u32), sizeof(u32));
305
306     count = read(fd, header, os_vm_page_size);
307     if (count < os_vm_page_size) {
308         lose("premature end of core file\n");
309     }
310     SHOW("successfully read first page of core");
311
312     ptr = header;
313     val = *ptr++;
314
315     if (val != CORE_MAGIC) {
316         lose("invalid magic number in core: 0x%lx should have been 0x%x.\n",
317              val,
318              CORE_MAGIC);
319     }
320     SHOW("found CORE_MAGIC");
321
322     while (val != END_CORE_ENTRY_TYPE_CODE) {
323         val = *ptr++;
324         len = *ptr++;
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));
328
329         switch (val) {
330
331         case END_CORE_ENTRY_TYPE_CODE:
332             SHOW("END_CORE_ENTRY_TYPE_CODE case");
333             break;
334
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",
339                      *ptr,
340                      SBCL_CORE_VERSION_INTEGER);
341             }
342             break;
343
344         case BUILD_ID_CORE_ENTRY_TYPE_CODE:
345             SHOW("BUILD_ID_CORE_ENTRY_TYPE_CODE case");
346             {
347                 unsigned int i;
348
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;
358                 }
359                 break;
360             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.
366                  *
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.) */
370
371                 lose("can't load .core for different runtime, sorry\n");
372             }
373
374         case NEW_DIRECTORY_CORE_ENTRY_TYPE_CODE:
375             SHOW("NEW_DIRECTORY_CORE_ENTRY_TYPE_CODE case");
376             process_directory(fd,
377                               ptr,
378 #ifndef LISP_FEATURE_ALPHA
379                               remaining_len / (sizeof(struct ndir_entry) /
380                                                sizeof(long)),
381 #else
382                               remaining_len / (sizeof(struct ndir_entry) /
383                                                sizeof(u32)),
384 #endif
385                               file_offset);
386             break;
387
388         case INITIAL_FUN_CORE_ENTRY_TYPE_CODE:
389             SHOW("INITIAL_FUN_CORE_ENTRY_TYPE_CODE case");
390             initial_function = (lispobj)*ptr;
391             break;
392
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");
396             {
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);
401                 long bytes_read;
402
403                 lseek(fd, fdoffset + file_offset, SEEK_SET);
404
405                 FSHOW((stderr, "attempting to read %ld lutexes from core\n", n_lutexes));
406                 bytes_read = read(fd, lutexes_to_resurrect, data_length);
407
408                 /* XXX */
409                 if (bytes_read != data_length) {
410                     lose("Could not read the lutex table");
411                 }
412                 else {
413                     int i;
414
415                     for (i=0; i<n_lutexes; ++i) {
416                         struct lutex *lutex = lutexes_to_resurrect[i];
417
418                         FSHOW((stderr, "re-init'ing lutex @ %p\n", lutex));
419                         lutex_init((tagged_lutex_t) lutex);
420                     }
421
422                     free(lutexes_to_resurrect);
423                 }
424                 break;
425             }
426 #endif
427
428 #ifdef LISP_FEATURE_GENCGC
429         case PAGE_TABLE_CORE_ENTRY_TYPE_CODE:
430         {
431             size_t size = *ptr;
432             size_t fdoffset = (*(ptr+1) + 1) * (os_vm_page_size);
433             size_t offset = 0;
434             long bytes_read;
435             unsigned long data[4096];
436             unsigned long word;
437             lseek(fd, fdoffset + file_offset, SEEK_SET);
438             while ((bytes_read = read(fd, data, (size < 4096 ? size : 4096 )))
439                     > 0)
440             {
441                 int i = 0;
442                 size -= bytes_read;
443                 while (bytes_read) {
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.
449                      *
450                      * The low bits of each word are allocation flags.
451                      */
452                     if (word=data[i]) {
453                         page_table[offset].region_start_offset = word & ~0x03;
454                         page_table[offset].allocated = word & 0x03;
455                     }
456                     i++;
457                     offset++;
458                 }
459             }
460
461             gencgc_partial_pickup = 1;
462             break;
463         }
464 #endif
465         default:
466             lose("unknown core file entry: %ld\n", (long)val);
467         }
468
469         ptr += remaining_len;
470         FSHOW((stderr, "/new ptr=%lx\n", (unsigned long)ptr));
471     }
472     SHOW("about to free(header)");
473     free(header);
474     SHOW("returning from load_core_file(..)");
475     return initial_function;
476 }
477