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