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