87891dc52cc86f9944dad0aa5973df157e878494
[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 /* Search 'filename' for an embedded core.  An SBCL core has, at the
60  * end of the file, a trailer containing the size of the core (an
61  * os_vm_offset_t) and a final signature word (the lispobj
62  * CORE_MAGIC).  If this trailer is found at the end of the file, the
63  * start of the core can be determined from the core size.
64  *
65  * If an embedded core is present, this returns the offset into the
66  * file to load the core from, or -1 if no core is present. */
67 os_vm_offset_t
68 search_for_embedded_core(char *filename)
69 {
70     lispobj header;
71     os_vm_offset_t lispobj_size = sizeof(lispobj);
72     os_vm_offset_t trailer_size = lispobj_size + sizeof(os_vm_offset_t);
73     os_vm_offset_t core_size, pos;
74     int fd = -1;
75
76     if ((fd = open_binary(filename, O_RDONLY)) < 0)
77         goto lose;
78     if (lseek(fd, -lispobj_size, SEEK_END) < 0)
79         goto lose;
80     if (read(fd, &header, (size_t)lispobj_size) < lispobj_size)
81         goto lose;
82
83     if (header == CORE_MAGIC) {
84         if (lseek(fd, -trailer_size, SEEK_END) < 0)
85             goto lose;
86         if (read(fd, &core_size, sizeof(os_vm_offset_t)) < 0)
87             goto lose;
88
89         if (lseek(fd, -(core_size + trailer_size), SEEK_END) < 0)
90             goto lose;
91         pos = lseek(fd, 0, SEEK_CUR);
92
93         if (read(fd, &header, (size_t)lispobj_size) < lispobj_size)
94             goto lose;
95
96         if (header != CORE_MAGIC)
97             goto lose;
98
99         close(fd);
100         return pos;
101     }
102
103 lose:
104     if (fd != -1)
105         close(fd);
106
107     return -1;
108 }
109
110 static void
111 process_directory(int fd, lispobj *ptr, int count, os_vm_offset_t file_offset)
112 {
113     struct ndir_entry *entry;
114
115     FSHOW((stderr, "/process_directory(..), count=%d\n", count));
116
117     for (entry = (struct ndir_entry *) ptr; --count>= 0; ++entry) {
118
119         long id = entry->identifier;
120         long offset = os_vm_page_size * (1 + entry->data_page);
121         os_vm_address_t addr =
122             (os_vm_address_t) (os_vm_page_size * entry->address);
123         lispobj *free_pointer = (lispobj *) addr + entry->nwords;
124         unsigned long len = os_vm_page_size * entry->page_count;
125
126         if (len != 0) {
127             os_vm_address_t real_addr;
128             FSHOW((stderr, "/mapping %ld(0x%lx) bytes at 0x%lx\n",
129                    (long)len, (long)len, (unsigned long)addr));
130             real_addr = os_map(fd, offset + file_offset, addr, len);
131             if (real_addr != addr) {
132                 lose("file mapped in wrong place! "
133                      "(0x%08x != 0x%08lx)\n",
134                      real_addr,
135                      addr);
136             }
137         }
138
139         FSHOW((stderr, "/space id = %ld, free pointer = 0x%lx\n",
140                id, (unsigned long)free_pointer));
141
142         switch (id) {
143         case DYNAMIC_CORE_SPACE_ID:
144             if (len > dynamic_space_size) {
145                 fprintf(stderr,
146                         "dynamic space too small for core: %ldKiB required, %ldKiB available.\n",
147                         len >> 10,
148                         (long)dynamic_space_size >> 10);
149                 exit(1);
150             }
151 #ifdef LISP_FEATURE_GENCGC
152             if (addr != (os_vm_address_t)DYNAMIC_SPACE_START) {
153                 fprintf(stderr, "in core: 0x%lx; in runtime: 0x%lx \n",
154                         (long)addr, (long)DYNAMIC_SPACE_START);
155                 lose("core/runtime address mismatch: DYNAMIC_SPACE_START\n");
156             }
157 #else
158             if ((addr != (os_vm_address_t)DYNAMIC_0_SPACE_START) &&
159                 (addr != (os_vm_address_t)DYNAMIC_1_SPACE_START)) {
160                 fprintf(stderr, "in core: 0x%lx; in runtime: 0x%lx or 0x%lx\n",
161                         (long)addr,
162                         (long)DYNAMIC_0_SPACE_START,
163                         (long)DYNAMIC_1_SPACE_START);
164                 lose("warning: core/runtime address mismatch: DYNAMIC_SPACE_START\n");
165             }
166 #endif
167 #if defined(ALLOCATION_POINTER)
168             SetSymbolValue(ALLOCATION_POINTER, (lispobj)free_pointer,0);
169 #else
170             dynamic_space_free_pointer = free_pointer;
171 #endif
172             /* For stop-and-copy GC, this will be whatever the GC was
173              * using at the time. With GENCGC, this will always be
174              * space 0. (We checked above that for GENCGC,
175              * addr==DYNAMIC_SPACE_START.) */
176             current_dynamic_space = (lispobj *)addr;
177             break;
178         case STATIC_CORE_SPACE_ID:
179             if (addr != (os_vm_address_t)STATIC_SPACE_START) {
180                 fprintf(stderr, "in core: 0x%lx - in runtime: 0x%lx\n",
181                         (long)addr, (long)STATIC_SPACE_START);
182                 lose("core/runtime address mismatch: STATIC_SPACE_START\n");
183             }
184             break;
185         case READ_ONLY_CORE_SPACE_ID:
186             if (addr != (os_vm_address_t)READ_ONLY_SPACE_START) {
187                 fprintf(stderr, "in core: 0x%lx - in runtime: 0x%lx\n",
188                         (long)addr, (long)READ_ONLY_SPACE_START);
189                 lose("core/runtime address mismatch: READ_ONLY_SPACE_START\n");
190             }
191             break;
192         default:
193             lose("unknown space ID %ld addr 0x%lx\n", id, (long)addr);
194         }
195     }
196 }
197
198 lispobj
199 load_core_file(char *file, os_vm_offset_t file_offset)
200 {
201     lispobj *header, val, len, *ptr, remaining_len;
202     int fd = open_binary(file, O_RDONLY);
203     unsigned int count;
204
205     lispobj initial_function = NIL;
206     FSHOW((stderr, "/entering load_core_file(%s)\n", file));
207     if (fd < 0) {
208         fprintf(stderr, "could not open file \"%s\"\n", file);
209         perror("open");
210         exit(1);
211     }
212
213     lseek(fd, file_offset, SEEK_SET);
214     header = calloc(os_vm_page_size / sizeof(u32), sizeof(u32));
215
216     count = read(fd, header, os_vm_page_size);
217     if (count < os_vm_page_size) {
218         lose("premature end of core file\n");
219     }
220     SHOW("successfully read first page of core");
221
222     ptr = header;
223     val = *ptr++;
224
225     if (val != CORE_MAGIC) {
226         lose("invalid magic number in core: 0x%lx should have been 0x%x.\n",
227              val,
228              CORE_MAGIC);
229     }
230     SHOW("found CORE_MAGIC");
231
232     while (val != END_CORE_ENTRY_TYPE_CODE) {
233         val = *ptr++;
234         len = *ptr++;
235         remaining_len = len - 2; /* (-2 to cancel the two ++ operations) */
236         FSHOW((stderr, "/val=0x%ld, remaining_len=0x%ld\n",
237                (long)val, (long)remaining_len));
238
239         switch (val) {
240
241         case END_CORE_ENTRY_TYPE_CODE:
242             SHOW("END_CORE_ENTRY_TYPE_CODE case");
243             break;
244
245         case VERSION_CORE_ENTRY_TYPE_CODE:
246             SHOW("VERSION_CORE_ENTRY_TYPE_CODE case");
247             if (*ptr != SBCL_CORE_VERSION_INTEGER) {
248                 lose("core file version (%d) != runtime library version (%d)\n",
249                      *ptr,
250                      SBCL_CORE_VERSION_INTEGER);
251             }
252             break;
253
254         case BUILD_ID_CORE_ENTRY_TYPE_CODE:
255             SHOW("BUILD_ID_CORE_ENTRY_TYPE_CODE case");
256             {
257                 unsigned int i;
258
259                 FSHOW((stderr, "build_id[]=\"%s\"\n", build_id));
260                 FSHOW((stderr, "remaining_len = %d\n", remaining_len));
261                 if (remaining_len != strlen((const char *)build_id))
262                     goto losing_build_id;
263                 for (i = 0; i < remaining_len; ++i) {
264                     FSHOW((stderr, "ptr[%d] = char = %d, expected=%d\n",
265                            i, ptr[i], build_id[i]));
266                     if (ptr[i] != build_id[i])
267                         goto losing_build_id;
268                 }
269                 break;
270             losing_build_id:
271                 /* .core files are not binary-compatible between
272                  * builds because we can't easily detect whether the
273                  * sources were patched between the time the
274                  * dumping-the-.core runtime was built and the time
275                  * that the loading-the-.core runtime was built.
276                  *
277                  * (We could easily detect whether version.lisp-expr
278                  * was changed, but people experimenting with patches
279                  * don't necessarily update version.lisp-expr.) */
280
281                 lose("can't load .core for different runtime, sorry\n");
282             }
283
284         case NEW_DIRECTORY_CORE_ENTRY_TYPE_CODE:
285             SHOW("NEW_DIRECTORY_CORE_ENTRY_TYPE_CODE case");
286             process_directory(fd,
287                               ptr,
288 #ifndef LISP_FEATURE_ALPHA
289                               remaining_len / (sizeof(struct ndir_entry) /
290                                                sizeof(long)),
291 #else
292                               remaining_len / (sizeof(struct ndir_entry) /
293                                                sizeof(u32)),
294 #endif
295                               file_offset);
296             break;
297
298         case INITIAL_FUN_CORE_ENTRY_TYPE_CODE:
299             SHOW("INITIAL_FUN_CORE_ENTRY_TYPE_CODE case");
300             initial_function = (lispobj)*ptr;
301             break;
302
303 #if defined(LISP_FEATURE_SB_THREAD) && defined(LISP_FEATURE_SB_LUTEX)
304         case LUTEX_TABLE_CORE_ENTRY_TYPE_CODE:
305             SHOW("LUTEX_TABLE_CORE_ENTRY_TYPE_CODE case");
306             {
307                 size_t n_lutexes = *ptr;
308                 size_t fdoffset = (*(ptr + 1) + 1) * (os_vm_page_size);
309                 size_t data_length = n_lutexes * sizeof(struct sap *);
310                 struct lutex **lutexes_to_resurrect = malloc(data_length);
311                 long bytes_read;
312
313                 lseek(fd, fdoffset + file_offset, SEEK_SET);
314
315                 FSHOW((stderr, "attempting to read %ld lutexes from core\n", n_lutexes));
316                 bytes_read = read(fd, lutexes_to_resurrect, data_length);
317
318                 /* XXX */
319                 if (bytes_read != data_length) {
320                     lose("Could not read the lutex table");
321                 }
322                 else {
323                     int i;
324
325                     for (i=0; i<n_lutexes; ++i) {
326                         struct lutex *lutex = lutexes_to_resurrect[i];
327
328                         FSHOW((stderr, "re-init'ing lutex @ %p\n", lutex));
329                         lutex_init((tagged_lutex_t) lutex);
330                     }
331
332                     free(lutexes_to_resurrect);
333                 }
334                 break;
335             }
336 #endif
337
338 #ifdef LISP_FEATURE_GENCGC
339         case PAGE_TABLE_CORE_ENTRY_TYPE_CODE:
340         {
341             size_t size = *ptr;
342             size_t fdoffset = (*(ptr+1) + 1) * (os_vm_page_size);
343             size_t offset = 0;
344             long bytes_read;
345             unsigned long data[4096];
346             lseek(fd, fdoffset + file_offset, SEEK_SET);
347             while ((bytes_read = read(fd, data, (size < 4096 ? size : 4096 )))
348                     > 0)
349             {
350                 int i = 0;
351                 size -= bytes_read;
352                 while (bytes_read) {
353                     bytes_read -= sizeof(long);
354                     /* Ignore all zeroes. The size of the page table
355                      * core entry was rounded up to os_vm_page_size
356                      * during the save, and might now have more
357                      * elements than the page table.
358                      */
359                     if (data[i]) {
360                         page_table[offset].region_start_offset = data[i];
361                     }
362                     i++;
363                     offset++;
364                 }
365             }
366
367             gencgc_partial_pickup = 1;
368             break;
369         }
370 #endif
371         default:
372             lose("unknown core file entry: %ld\n", (long)val);
373         }
374
375         ptr += remaining_len;
376         FSHOW((stderr, "/new ptr=%lx\n", (unsigned long)ptr));
377     }
378     SHOW("about to free(header)");
379     free(header);
380     SHOW("returning from load_core_file(..)");
381     return initial_function;
382 }
383