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