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