1.0.29.44: Complex float improvements
[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 /* If more platforms doesn't support overlapping mmap rename this
149  * def to something like ifdef nommapoverlap */
150 /* currently hpux only */
151 #ifdef LISP_FEATURE_HPUX
152 os_vm_address_t copy_core_bytes(int fd, os_vm_offset_t offset,
153                                 os_vm_address_t addr, int len)
154 {
155   unsigned char buf[4096];
156   int c,x;
157   int old_fd = lseek(fd, 0, SEEK_CUR);
158
159   if(len & (4096-1)){
160     fprintf(stderr, "cant copy a slice of core because slice-length is not of page size(4096)\n");
161     exit(-1);
162   }
163   if(old_fd < 0){
164     fprintf(stderr, "cant perform lseek() on corefile\n");
165   }
166   lseek(fd, offset, SEEK_SET);
167   if(fd < 0){
168     fprintf(stderr, "cant perform lseek(%u,%lu,SEEK_SET) on corefile\n", fd, offset);
169   }
170   for(x = 0; x < len; x += 4096){
171     c = read(fd, buf, 4096);
172     if(c != 4096){
173       fprintf(stderr, "cant read memory area from corefile at position %lu, got %d\n", offset + x, c);
174       exit(-1);
175     }
176     memcpy(addr+x, buf, 4096);
177   }
178   os_flush_icache(addr, len);
179   return addr;
180 }
181 #endif
182
183 static void
184 process_directory(int fd, lispobj *ptr, int count, os_vm_offset_t file_offset)
185 {
186     struct ndir_entry *entry;
187
188     FSHOW((stderr, "/process_directory(..), count=%d\n", count));
189
190     for (entry = (struct ndir_entry *) ptr; --count>= 0; ++entry) {
191
192         long id = entry->identifier;
193         long offset = os_vm_page_size * (1 + entry->data_page);
194         os_vm_address_t addr =
195             (os_vm_address_t) (os_vm_page_size * entry->address);
196         lispobj *free_pointer = (lispobj *) addr + entry->nwords;
197         unsigned long len = os_vm_page_size * entry->page_count;
198         if (len != 0) {
199             os_vm_address_t real_addr;
200             FSHOW((stderr, "/mapping %ld(0x%lx) bytes at 0x%lx\n",
201                    (long)len, (long)len, (unsigned long)addr));
202 #ifdef LISP_FEATURE_HPUX
203             real_addr = copy_core_bytes(fd, offset + file_offset, addr, len);
204 #else
205             real_addr = os_map(fd, offset + file_offset, addr, len);
206 #endif
207             if (real_addr != addr) {
208                 lose("file mapped in wrong place! "
209                      "(0x%08x != 0x%08lx)\n",
210                      real_addr,
211                      addr);
212             }
213         }
214
215         FSHOW((stderr, "/space id = %ld, free pointer = 0x%lx\n",
216                id, (unsigned long)free_pointer));
217
218         switch (id) {
219         case DYNAMIC_CORE_SPACE_ID:
220             if (len > dynamic_space_size) {
221                 fprintf(stderr,
222                         "dynamic space too small for core: %ldKiB required, %ldKiB available.\n",
223                         len >> 10,
224                         (long)dynamic_space_size >> 10);
225                 exit(1);
226             }
227 #ifdef LISP_FEATURE_GENCGC
228             if (addr != (os_vm_address_t)DYNAMIC_SPACE_START) {
229                 fprintf(stderr, "in core: 0x%lx; in runtime: 0x%lx \n",
230                         (long)addr, (long)DYNAMIC_SPACE_START);
231                 lose("core/runtime address mismatch: DYNAMIC_SPACE_START\n");
232             }
233 #else
234             if ((addr != (os_vm_address_t)DYNAMIC_0_SPACE_START) &&
235                 (addr != (os_vm_address_t)DYNAMIC_1_SPACE_START)) {
236                 fprintf(stderr, "in core: 0x%lx; in runtime: 0x%lx or 0x%lx\n",
237                         (long)addr,
238                         (long)DYNAMIC_0_SPACE_START,
239                         (long)DYNAMIC_1_SPACE_START);
240                 lose("warning: core/runtime address mismatch: DYNAMIC_SPACE_START\n");
241             }
242 #endif
243 #if defined(ALLOCATION_POINTER)
244             SetSymbolValue(ALLOCATION_POINTER, (lispobj)free_pointer,0);
245 #else
246             dynamic_space_free_pointer = free_pointer;
247 #endif
248             /* For stop-and-copy GC, this will be whatever the GC was
249              * using at the time. With GENCGC, this will always be
250              * space 0. (We checked above that for GENCGC,
251              * addr==DYNAMIC_SPACE_START.) */
252             current_dynamic_space = (lispobj *)addr;
253             break;
254         case STATIC_CORE_SPACE_ID:
255             if (addr != (os_vm_address_t)STATIC_SPACE_START) {
256                 fprintf(stderr, "in core: 0x%lx - in runtime: 0x%lx\n",
257                         (long)addr, (long)STATIC_SPACE_START);
258                 lose("core/runtime address mismatch: STATIC_SPACE_START\n");
259             }
260             break;
261         case READ_ONLY_CORE_SPACE_ID:
262             if (addr != (os_vm_address_t)READ_ONLY_SPACE_START) {
263                 fprintf(stderr, "in core: 0x%lx - in runtime: 0x%lx\n",
264                         (long)addr, (long)READ_ONLY_SPACE_START);
265                 lose("core/runtime address mismatch: READ_ONLY_SPACE_START\n");
266             }
267             break;
268         default:
269             lose("unknown space ID %ld addr 0x%lx\n", id, (long)addr);
270         }
271     }
272 }
273
274 lispobj
275 load_core_file(char *file, os_vm_offset_t file_offset)
276 {
277     lispobj *header, val, len, *ptr, remaining_len;
278     int fd = open_binary(file, O_RDONLY);
279     unsigned int count;
280
281     lispobj initial_function = NIL;
282     FSHOW((stderr, "/entering load_core_file(%s)\n", file));
283     if (fd < 0) {
284         fprintf(stderr, "could not open file \"%s\"\n", file);
285         perror("open");
286         exit(1);
287     }
288
289     lseek(fd, file_offset, SEEK_SET);
290     header = calloc(os_vm_page_size / sizeof(u32), sizeof(u32));
291
292     count = read(fd, header, os_vm_page_size);
293     if (count < os_vm_page_size) {
294         lose("premature end of core file\n");
295     }
296     SHOW("successfully read first page of core");
297
298     ptr = header;
299     val = *ptr++;
300
301     if (val != CORE_MAGIC) {
302         lose("invalid magic number in core: 0x%lx should have been 0x%x.\n",
303              val,
304              CORE_MAGIC);
305     }
306     SHOW("found CORE_MAGIC");
307
308     while (val != END_CORE_ENTRY_TYPE_CODE) {
309         val = *ptr++;
310         len = *ptr++;
311         remaining_len = len - 2; /* (-2 to cancel the two ++ operations) */
312         FSHOW((stderr, "/val=0x%ld, remaining_len=0x%ld\n",
313                (long)val, (long)remaining_len));
314
315         switch (val) {
316
317         case END_CORE_ENTRY_TYPE_CODE:
318             SHOW("END_CORE_ENTRY_TYPE_CODE case");
319             break;
320
321         case VERSION_CORE_ENTRY_TYPE_CODE:
322             SHOW("VERSION_CORE_ENTRY_TYPE_CODE case");
323             if (*ptr != SBCL_CORE_VERSION_INTEGER) {
324                 lose("core file version (%d) != runtime library version (%d)\n",
325                      *ptr,
326                      SBCL_CORE_VERSION_INTEGER);
327             }
328             break;
329
330         case BUILD_ID_CORE_ENTRY_TYPE_CODE:
331             SHOW("BUILD_ID_CORE_ENTRY_TYPE_CODE case");
332             {
333                 unsigned int i;
334
335                 FSHOW((stderr, "build_id[]=\"%s\"\n", build_id));
336                 FSHOW((stderr, "remaining_len = %d\n", remaining_len));
337                 if (remaining_len != strlen((const char *)build_id))
338                     goto losing_build_id;
339                 for (i = 0; i < remaining_len; ++i) {
340                     FSHOW((stderr, "ptr[%d] = char = %d, expected=%d\n",
341                            i, ptr[i], build_id[i]));
342                     if (ptr[i] != build_id[i])
343                         goto losing_build_id;
344                 }
345                 break;
346             losing_build_id:
347                 /* .core files are not binary-compatible between
348                  * builds because we can't easily detect whether the
349                  * sources were patched between the time the
350                  * dumping-the-.core runtime was built and the time
351                  * that the loading-the-.core runtime was built.
352                  *
353                  * (We could easily detect whether version.lisp-expr
354                  * was changed, but people experimenting with patches
355                  * don't necessarily update version.lisp-expr.) */
356
357                 lose("can't load .core for different runtime, sorry\n");
358             }
359
360         case NEW_DIRECTORY_CORE_ENTRY_TYPE_CODE:
361             SHOW("NEW_DIRECTORY_CORE_ENTRY_TYPE_CODE case");
362             process_directory(fd,
363                               ptr,
364 #ifndef LISP_FEATURE_ALPHA
365                               remaining_len / (sizeof(struct ndir_entry) /
366                                                sizeof(long)),
367 #else
368                               remaining_len / (sizeof(struct ndir_entry) /
369                                                sizeof(u32)),
370 #endif
371                               file_offset);
372             break;
373
374         case INITIAL_FUN_CORE_ENTRY_TYPE_CODE:
375             SHOW("INITIAL_FUN_CORE_ENTRY_TYPE_CODE case");
376             initial_function = (lispobj)*ptr;
377             break;
378
379 #if defined(LISP_FEATURE_SB_THREAD) && defined(LISP_FEATURE_SB_LUTEX)
380         case LUTEX_TABLE_CORE_ENTRY_TYPE_CODE:
381             SHOW("LUTEX_TABLE_CORE_ENTRY_TYPE_CODE case");
382             {
383                 size_t n_lutexes = *ptr;
384                 size_t fdoffset = (*(ptr + 1) + 1) * (os_vm_page_size);
385                 size_t data_length = n_lutexes * sizeof(struct sap *);
386                 struct lutex **lutexes_to_resurrect = malloc(data_length);
387                 long bytes_read;
388
389                 lseek(fd, fdoffset + file_offset, SEEK_SET);
390
391                 FSHOW((stderr, "attempting to read %ld lutexes from core\n", n_lutexes));
392                 bytes_read = read(fd, lutexes_to_resurrect, data_length);
393
394                 /* XXX */
395                 if (bytes_read != data_length) {
396                     lose("Could not read the lutex table");
397                 }
398                 else {
399                     int i;
400
401                     for (i=0; i<n_lutexes; ++i) {
402                         struct lutex *lutex = lutexes_to_resurrect[i];
403
404                         FSHOW((stderr, "re-init'ing lutex @ %p\n", lutex));
405                         lutex_init((tagged_lutex_t) lutex);
406                     }
407
408                     free(lutexes_to_resurrect);
409                 }
410                 break;
411             }
412 #endif
413
414 #ifdef LISP_FEATURE_GENCGC
415         case PAGE_TABLE_CORE_ENTRY_TYPE_CODE:
416         {
417             size_t size = *ptr;
418             size_t fdoffset = (*(ptr+1) + 1) * (os_vm_page_size);
419             size_t offset = 0;
420             long bytes_read;
421             unsigned long data[4096];
422             lseek(fd, fdoffset + file_offset, SEEK_SET);
423             while ((bytes_read = read(fd, data, (size < 4096 ? size : 4096 )))
424                     > 0)
425             {
426                 int i = 0;
427                 size -= bytes_read;
428                 while (bytes_read) {
429                     bytes_read -= sizeof(long);
430                     /* Ignore all zeroes. The size of the page table
431                      * core entry was rounded up to os_vm_page_size
432                      * during the save, and might now have more
433                      * elements than the page table.
434                      */
435                     if (data[i]) {
436                         page_table[offset].region_start_offset = data[i];
437                     }
438                     i++;
439                     offset++;
440                 }
441             }
442
443             gencgc_partial_pickup = 1;
444             break;
445         }
446 #endif
447         default:
448             lose("unknown core file entry: %ld\n", (long)val);
449         }
450
451         ptr += remaining_len;
452         FSHOW((stderr, "/new ptr=%lx\n", (unsigned long)ptr));
453     }
454     SHOW("about to free(header)");
455     free(header);
456     SHOW("returning from load_core_file(..)");
457     return initial_function;
458 }
459