cleanup: page_index_t canonicalization
[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
28 #ifndef LISP_FEATURE_WIN32
29 #ifdef LISP_FEATURE_LINUX
30 /* For madvise */
31 # define _BSD_SOURCE
32 #endif
33 #include <sys/mman.h>
34 #endif
35
36 #include "os.h"
37 #include "runtime.h"
38 #include "globals.h"
39 #include "core.h"
40 #include "arch.h"
41 #include "interr.h"
42 #include "thread.h"
43
44 #include "validate.h"
45 #include "gc-internal.h"
46 #include "runtime-options.h"
47
48 #include <errno.h>
49
50 #ifdef LISP_FEATURE_SB_CORE_COMPRESSION
51 # include <zlib.h>
52 #endif
53
54 unsigned char build_id[] =
55 #include "../../output/build-id.tmp"
56 ;
57
58 int
59 open_binary(char *filename, int mode)
60 {
61 #ifdef LISP_FEATURE_WIN32
62     mode |= O_BINARY;
63 #endif
64
65     return open(filename, mode);
66 }
67
68
69 static struct runtime_options *
70 read_runtime_options(int fd)
71 {
72     os_vm_size_t optarray[RUNTIME_OPTIONS_WORDS];
73     struct runtime_options *options = NULL;
74
75     if (read(fd, optarray, RUNTIME_OPTIONS_WORDS * sizeof(os_vm_size_t)) !=
76         RUNTIME_OPTIONS_WORDS * sizeof(size_t)) {
77         return NULL;
78     }
79
80     if ((RUNTIME_OPTIONS_MAGIC != optarray[0]) || (0 == optarray[1])) {
81         return NULL;
82     }
83
84     options = successful_malloc(sizeof(struct runtime_options));
85
86     options->dynamic_space_size = optarray[2];
87     options->thread_control_stack_size = optarray[3];
88
89     return options;
90 }
91
92 void
93 maybe_initialize_runtime_options(int fd)
94 {
95     struct runtime_options *new_runtime_options;
96     off_t end_offset = sizeof(lispobj) +
97         sizeof(os_vm_offset_t) +
98         (RUNTIME_OPTIONS_WORDS * sizeof(size_t));
99
100     lseek(fd, -end_offset, SEEK_END);
101
102     if ((new_runtime_options = read_runtime_options(fd))) {
103         runtime_options = new_runtime_options;
104     }
105 }
106
107 /* Search 'filename' for an embedded core.  An SBCL core has, at the
108  * end of the file, a trailer containing optional saved runtime
109  * options, the start of the core (an os_vm_offset_t), and a final
110  * signature word (the lispobj CORE_MAGIC).  If this trailer is found
111  * at the end of the file, the start of the core can be determined
112  * from the core size.
113  *
114  * If an embedded core is present, this returns the offset into the
115  * file to load the core from, or -1 if no core is present. */
116 os_vm_offset_t
117 search_for_embedded_core(char *filename)
118 {
119     lispobj header;
120     os_vm_offset_t lispobj_size = sizeof(lispobj);
121     os_vm_offset_t trailer_size = lispobj_size + sizeof(os_vm_offset_t);
122     os_vm_offset_t core_start, pos;
123     int fd = -1;
124
125     if ((fd = open_binary(filename, O_RDONLY)) < 0)
126         goto lose;
127
128     if (read(fd, &header, (size_t)lispobj_size) < lispobj_size)
129         goto lose;
130     if (header == CORE_MAGIC) {
131         /* This file is a real core, not an embedded core.  Return 0 to
132          * indicate where the core starts, and do not look for runtime
133          * options in this case. */
134         return 0;
135     }
136
137     if (lseek(fd, -lispobj_size, SEEK_END) < 0)
138         goto lose;
139     if (read(fd, &header, (size_t)lispobj_size) < lispobj_size)
140         goto lose;
141
142     if (header == CORE_MAGIC) {
143         if (lseek(fd, -trailer_size, SEEK_END) < 0)
144             goto lose;
145         if (read(fd, &core_start, sizeof(os_vm_offset_t)) < 0)
146             goto lose;
147
148         if (lseek(fd, core_start, SEEK_SET) < 0)
149             goto lose;
150         pos = lseek(fd, 0, SEEK_CUR);
151
152         if (read(fd, &header, (size_t)lispobj_size) < lispobj_size)
153             goto lose;
154
155         if (header != CORE_MAGIC)
156             goto lose;
157
158         maybe_initialize_runtime_options(fd);
159
160         close(fd);
161         return pos;
162     }
163
164 lose:
165     if (fd != -1)
166         close(fd);
167
168     return -1;
169 }
170
171 /* If more platforms doesn't support overlapping mmap rename this
172  * def to something like ifdef nommapoverlap */
173 /* currently hpux only */
174 #ifdef LISP_FEATURE_HPUX
175 os_vm_address_t copy_core_bytes(int fd, os_vm_offset_t offset,
176                                 os_vm_address_t addr, int len)
177 {
178   unsigned char buf[4096];
179   int c,x;
180   int old_fd = lseek(fd, 0, SEEK_CUR);
181
182   if(len & (4096-1)){
183     fprintf(stderr, "cant copy a slice of core because slice-length is not of page size(4096)\n");
184     exit(-1);
185   }
186   if(old_fd < 0){
187     fprintf(stderr, "cant perform lseek() on corefile\n");
188   }
189   lseek(fd, offset, SEEK_SET);
190   if(fd < 0){
191     fprintf(stderr, "cant perform lseek(%u,%lu,SEEK_SET) on corefile\n", fd, offset);
192   }
193   for(x = 0; x < len; x += 4096){
194     c = read(fd, buf, 4096);
195     if(c != 4096){
196       fprintf(stderr, "cant read memory area from corefile at position %lu, got %d\n", offset + x, c);
197       exit(-1);
198     }
199     memcpy(addr+x, buf, 4096);
200   }
201   os_flush_icache(addr, len);
202   return addr;
203 }
204 #endif
205
206 #ifdef LISP_FEATURE_SB_CORE_COMPRESSION
207 # define ZLIB_BUFFER_SIZE (1u<<16)
208 os_vm_address_t inflate_core_bytes(int fd, os_vm_offset_t offset,
209                                    os_vm_address_t addr, int len)
210 {
211     z_stream stream;
212     unsigned char buf[ZLIB_BUFFER_SIZE];
213     int ret;
214
215     if (-1 == lseek(fd, offset, SEEK_SET)) {
216         lose("Unable to lseek() on corefile\n");
217     }
218
219     stream.zalloc = NULL;
220     stream.zfree = NULL;
221     stream.opaque = NULL;
222     stream.avail_in = 0;
223     stream.next_in = buf;
224
225     ret = inflateInit(&stream);
226     if (ret != Z_OK)
227         lose("zlib error %i\n", ret);
228
229     stream.next_out  = (void*)addr;
230     stream.avail_out = len;
231     do {
232         ssize_t count = read(fd, buf, sizeof(buf));
233         if (count < 0)
234             lose("unable to read core file (errno = %i)\n", errno);
235         stream.next_in = buf;
236         stream.avail_in = count;
237         if (count == 0) break;
238         ret = inflate(&stream, Z_NO_FLUSH);
239         switch (ret) {
240         case Z_STREAM_END:
241             break;
242         case Z_OK:
243             if (stream.avail_out == 0)
244                 lose("Runaway gzipped core directory... aborting\n");
245             if (stream.avail_in > 0)
246                 lose("zlib inflate returned without fully"
247                      "using up input buffer... aborting\n");
248             break;
249         default:
250             lose("zlib inflate error: %i\n", ret);
251             break;
252         }
253     } while (ret != Z_STREAM_END);
254
255     if (stream.avail_out > 0) {
256         if (stream.avail_out >= os_vm_page_size)
257             fprintf(stderr, "Warning: gzipped core directory significantly"
258                     "shorter than expected (%lu bytes)", (unsigned long)stream.avail_out);
259         /* Is this needed? */
260         memset(stream.next_out, 0, stream.avail_out);
261     }
262
263     inflateEnd(&stream);
264     return addr;
265 }
266 # undef ZLIB_BUFFER_SIZE
267 #endif
268
269 int merge_core_pages = -1;
270
271 static void
272 process_directory(int fd, lispobj *ptr, int count, os_vm_offset_t file_offset)
273 {
274     struct ndir_entry *entry;
275     int compressed;
276
277     FSHOW((stderr, "/process_directory(..), count=%d\n", count));
278
279     for (entry = (struct ndir_entry *) ptr; --count>= 0; ++entry) {
280         compressed = 0;
281         long id = entry->identifier;
282         if (id <= (MAX_CORE_SPACE_ID | DEFLATED_CORE_SPACE_ID_FLAG)) {
283             if (id & DEFLATED_CORE_SPACE_ID_FLAG)
284                 compressed = 1;
285             id &= ~(DEFLATED_CORE_SPACE_ID_FLAG);
286         }
287         long offset = os_vm_page_size * (1 + entry->data_page);
288         os_vm_address_t addr =
289             (os_vm_address_t) (os_vm_page_size * entry->address);
290         lispobj *free_pointer = (lispobj *) addr + entry->nwords;
291         unsigned long len = os_vm_page_size * entry->page_count;
292         if (len != 0) {
293             os_vm_address_t real_addr;
294             FSHOW((stderr, "/mapping %ld(0x%lx) bytes at 0x%lx\n",
295                    (long)len, (long)len, (unsigned long)addr));
296             if (compressed) {
297 #ifdef LISP_FEATURE_SB_CORE_COMPRESSION
298                 real_addr = inflate_core_bytes(fd, offset + file_offset, addr, len);
299 #else
300                 lose("This runtime was not built with zlib-compressed core support... aborting\n");
301 #endif
302             } else {
303 #ifdef LISP_FEATURE_HPUX
304                 real_addr = copy_core_bytes(fd, offset + file_offset, addr, len);
305 #else
306                 real_addr = os_map(fd, offset + file_offset, addr, len);
307 #endif
308             }
309             if (real_addr != addr) {
310                 lose("file mapped in wrong place! "
311                      "(0x%08x != 0x%08lx)\n",
312                      real_addr,
313                      addr);
314             }
315         }
316
317 #ifdef MADV_MERGEABLE
318         if ((merge_core_pages == 1)
319             || ((merge_core_pages == -1) && compressed)) {
320                 madvise(addr, len, MADV_MERGEABLE);
321         }
322 #endif
323
324         FSHOW((stderr, "/space id = %ld, free pointer = 0x%lx\n",
325                id, (unsigned long)free_pointer));
326
327         switch (id) {
328         case DYNAMIC_CORE_SPACE_ID:
329             if (len > dynamic_space_size) {
330                 fprintf(stderr,
331                         "dynamic space too small for core: %ldKiB required, %ldKiB available.\n",
332                         len >> 10,
333                         (long)dynamic_space_size >> 10);
334                 exit(1);
335             }
336 #ifdef LISP_FEATURE_GENCGC
337             if (addr != (os_vm_address_t)DYNAMIC_SPACE_START) {
338                 fprintf(stderr, "in core: 0x%lx; in runtime: 0x%lx \n",
339                         (long)addr, (long)DYNAMIC_SPACE_START);
340                 lose("core/runtime address mismatch: DYNAMIC_SPACE_START\n");
341             }
342 #else
343             if ((addr != (os_vm_address_t)DYNAMIC_0_SPACE_START) &&
344                 (addr != (os_vm_address_t)DYNAMIC_1_SPACE_START)) {
345                 fprintf(stderr, "in core: 0x%lx; in runtime: 0x%lx or 0x%lx\n",
346                         (long)addr,
347                         (long)DYNAMIC_0_SPACE_START,
348                         (long)DYNAMIC_1_SPACE_START);
349                 lose("warning: core/runtime address mismatch: DYNAMIC_SPACE_START\n");
350             }
351 #endif
352 #if defined(ALLOCATION_POINTER)
353             SetSymbolValue(ALLOCATION_POINTER, (lispobj)free_pointer,0);
354 #else
355             dynamic_space_free_pointer = free_pointer;
356 #endif
357             /* For stop-and-copy GC, this will be whatever the GC was
358              * using at the time. With GENCGC, this will always be
359              * space 0. (We checked above that for GENCGC,
360              * addr==DYNAMIC_SPACE_START.) */
361             current_dynamic_space = (lispobj *)addr;
362             break;
363         case STATIC_CORE_SPACE_ID:
364             if (addr != (os_vm_address_t)STATIC_SPACE_START) {
365                 fprintf(stderr, "in core: 0x%lx - in runtime: 0x%lx\n",
366                         (long)addr, (long)STATIC_SPACE_START);
367                 lose("core/runtime address mismatch: STATIC_SPACE_START\n");
368             }
369             break;
370         case READ_ONLY_CORE_SPACE_ID:
371             if (addr != (os_vm_address_t)READ_ONLY_SPACE_START) {
372                 fprintf(stderr, "in core: 0x%lx - in runtime: 0x%lx\n",
373                         (long)addr, (long)READ_ONLY_SPACE_START);
374                 lose("core/runtime address mismatch: READ_ONLY_SPACE_START\n");
375             }
376             break;
377         default:
378             lose("unknown space ID %ld addr 0x%lx\n", id, (long)addr);
379         }
380     }
381 }
382
383 lispobj
384 load_core_file(char *file, os_vm_offset_t file_offset)
385 {
386     lispobj *header, val, len, *ptr, remaining_len;
387     int fd = open_binary(file, O_RDONLY);
388     unsigned int count;
389
390     lispobj initial_function = NIL;
391     FSHOW((stderr, "/entering load_core_file(%s)\n", file));
392     if (fd < 0) {
393         fprintf(stderr, "could not open file \"%s\"\n", file);
394         perror("open");
395         exit(1);
396     }
397
398     lseek(fd, file_offset, SEEK_SET);
399     header = calloc(os_vm_page_size / sizeof(u32), sizeof(u32));
400
401     count = read(fd, header, os_vm_page_size);
402     if (count < os_vm_page_size) {
403         lose("premature end of core file\n");
404     }
405     SHOW("successfully read first page of core");
406
407     ptr = header;
408     val = *ptr++;
409
410     if (val != CORE_MAGIC) {
411         lose("invalid magic number in core: 0x%lx should have been 0x%x.\n",
412              val,
413              CORE_MAGIC);
414     }
415     SHOW("found CORE_MAGIC");
416
417     while (val != END_CORE_ENTRY_TYPE_CODE) {
418         val = *ptr++;
419         len = *ptr++;
420         remaining_len = len - 2; /* (-2 to cancel the two ++ operations) */
421         FSHOW((stderr, "/val=0x%ld, remaining_len=0x%ld\n",
422                (long)val, (long)remaining_len));
423
424         switch (val) {
425
426         case END_CORE_ENTRY_TYPE_CODE:
427             SHOW("END_CORE_ENTRY_TYPE_CODE case");
428             break;
429
430         case VERSION_CORE_ENTRY_TYPE_CODE:
431             SHOW("VERSION_CORE_ENTRY_TYPE_CODE case");
432             if (*ptr != SBCL_CORE_VERSION_INTEGER) {
433                 lose("core file version (%d) != runtime library version (%d)\n",
434                      *ptr,
435                      SBCL_CORE_VERSION_INTEGER);
436             }
437             break;
438
439         case BUILD_ID_CORE_ENTRY_TYPE_CODE:
440             SHOW("BUILD_ID_CORE_ENTRY_TYPE_CODE case");
441             {
442                 unsigned int i;
443
444                 FSHOW((stderr, "build_id[]=\"%s\"\n", build_id));
445                 FSHOW((stderr, "remaining_len = %d\n", remaining_len));
446                 if (remaining_len != strlen((const char *)build_id))
447                     goto losing_build_id;
448                 for (i = 0; i < remaining_len; ++i) {
449                     FSHOW((stderr, "ptr[%d] = char = %d, expected=%d\n",
450                            i, ptr[i], build_id[i]));
451                     if (ptr[i] != build_id[i])
452                         goto losing_build_id;
453                 }
454                 break;
455             losing_build_id:
456                 /* .core files are not binary-compatible between
457                  * builds because we can't easily detect whether the
458                  * sources were patched between the time the
459                  * dumping-the-.core runtime was built and the time
460                  * that the loading-the-.core runtime was built.
461                  *
462                  * (We could easily detect whether version.lisp-expr
463                  * was changed, but people experimenting with patches
464                  * don't necessarily update version.lisp-expr.) */
465
466                 lose("can't load .core for different runtime, sorry\n");
467             }
468
469         case NEW_DIRECTORY_CORE_ENTRY_TYPE_CODE:
470             SHOW("NEW_DIRECTORY_CORE_ENTRY_TYPE_CODE case");
471             process_directory(fd,
472                               ptr,
473 #ifndef LISP_FEATURE_ALPHA
474                               remaining_len / (sizeof(struct ndir_entry) /
475                                                sizeof(long)),
476 #else
477                               remaining_len / (sizeof(struct ndir_entry) /
478                                                sizeof(u32)),
479 #endif
480                               file_offset);
481             break;
482
483         case INITIAL_FUN_CORE_ENTRY_TYPE_CODE:
484             SHOW("INITIAL_FUN_CORE_ENTRY_TYPE_CODE case");
485             initial_function = (lispobj)*ptr;
486             break;
487
488 #ifdef LISP_FEATURE_GENCGC
489         case PAGE_TABLE_CORE_ENTRY_TYPE_CODE:
490         {
491             size_t size = *ptr;
492             size_t fdoffset = (*(ptr+1) + 1) * (os_vm_page_size);
493             page_index_t offset = 0;
494             long bytes_read;
495             unsigned long data[4096];
496             unsigned long word;
497             lseek(fd, fdoffset + file_offset, SEEK_SET);
498             while ((bytes_read = read(fd, data, (size < 4096 ? size : 4096 )))
499                     > 0)
500             {
501                 int i = 0;
502                 size -= bytes_read;
503                 while (bytes_read) {
504                     bytes_read -= sizeof(long);
505                     /* Ignore all zeroes. The size of the page table
506                      * core entry was rounded up to os_vm_page_size
507                      * during the save, and might now have more
508                      * elements than the page table.
509                      *
510                      * The low bits of each word are allocation flags.
511                      */
512                     if (word=data[i]) {
513                         page_table[offset].region_start_offset = word & ~0x03;
514                         page_table[offset].allocated = word & 0x03;
515                     }
516                     i++;
517                     offset++;
518                 }
519             }
520
521             gencgc_partial_pickup = 1;
522             break;
523         }
524 #endif
525         default:
526             lose("unknown core file entry: %ld\n", (long)val);
527         }
528
529         ptr += remaining_len;
530         FSHOW((stderr, "/new ptr=%lx\n", (unsigned long)ptr));
531     }
532     SHOW("about to free(header)");
533     free(header);
534     SHOW("returning from load_core_file(..)");
535     return initial_function;
536 }
537