improve the SB-EXT:GC docstring(s)
[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     void *header;
387     word_t val, *ptr;
388     os_vm_size_t len, remaining_len;
389     int fd = open_binary(file, O_RDONLY);
390     ssize_t count;
391     lispobj initial_function = NIL;
392
393     FSHOW((stderr, "/entering load_core_file(%s)\n", file));
394     if (fd < 0) {
395         fprintf(stderr, "could not open file \"%s\"\n", file);
396         perror("open");
397         exit(1);
398     }
399
400     lseek(fd, file_offset, SEEK_SET);
401     header = calloc(os_vm_page_size, 1);
402
403     count = read(fd, header, os_vm_page_size);
404     if (count < os_vm_page_size) {
405         lose("premature end of core file\n");
406     }
407     SHOW("successfully read first page of core");
408
409     ptr = header;
410     val = *ptr++;
411
412     if (val != CORE_MAGIC) {
413         lose("invalid magic number in core: 0x%lx should have been 0x%x.\n",
414              val,
415              CORE_MAGIC);
416     }
417     SHOW("found CORE_MAGIC");
418
419     while (val != END_CORE_ENTRY_TYPE_CODE) {
420         val = *ptr++;
421         len = *ptr++;
422         remaining_len = len - 2; /* (-2 to cancel the two ++ operations) */
423         FSHOW((stderr, "/val=0x%"WORD_FMTX", remaining_len=0x%"WORD_FMTX"\n",
424                val, remaining_len));
425
426         switch (val) {
427
428         case END_CORE_ENTRY_TYPE_CODE:
429             SHOW("END_CORE_ENTRY_TYPE_CODE case");
430             break;
431
432         case VERSION_CORE_ENTRY_TYPE_CODE:
433             SHOW("VERSION_CORE_ENTRY_TYPE_CODE case");
434             if (*ptr != SBCL_CORE_VERSION_INTEGER) {
435                 lose("core file version (%d) != runtime library version (%d)\n",
436                      *ptr,
437                      SBCL_CORE_VERSION_INTEGER);
438             }
439             break;
440
441         case BUILD_ID_CORE_ENTRY_TYPE_CODE:
442             SHOW("BUILD_ID_CORE_ENTRY_TYPE_CODE case");
443             {
444                 os_vm_size_t i;
445
446                 FSHOW((stderr, "build_id[]=\"%s\"\n", build_id));
447                 FSHOW((stderr, "remaining_len = %d\n", remaining_len));
448                 if (remaining_len != strlen((const char *)build_id))
449                     goto losing_build_id;
450                 for (i = 0; i < remaining_len; ++i) {
451                     FSHOW((stderr, "ptr[%d] = char = %d, expected=%d\n",
452                            i, ptr[i], build_id[i]));
453                     if (ptr[i] != build_id[i])
454                         goto losing_build_id;
455                 }
456                 break;
457             losing_build_id:
458                 /* .core files are not binary-compatible between
459                  * builds because we can't easily detect whether the
460                  * sources were patched between the time the
461                  * dumping-the-.core runtime was built and the time
462                  * that the loading-the-.core runtime was built.
463                  *
464                  * (We could easily detect whether version.lisp-expr
465                  * was changed, but people experimenting with patches
466                  * don't necessarily update version.lisp-expr.) */
467
468                 lose("can't load .core for different runtime, sorry\n");
469             }
470
471         case NEW_DIRECTORY_CORE_ENTRY_TYPE_CODE:
472             SHOW("NEW_DIRECTORY_CORE_ENTRY_TYPE_CODE case");
473             process_directory(fd,
474                               ptr,
475 #ifndef LISP_FEATURE_ALPHA
476                               remaining_len / (sizeof(struct ndir_entry) /
477                                                sizeof(long)),
478 #else
479                               remaining_len / (sizeof(struct ndir_entry) /
480                                                sizeof(u32)),
481 #endif
482                               file_offset);
483             break;
484
485         case INITIAL_FUN_CORE_ENTRY_TYPE_CODE:
486             SHOW("INITIAL_FUN_CORE_ENTRY_TYPE_CODE case");
487             initial_function = (lispobj)*ptr;
488             break;
489
490 #ifdef LISP_FEATURE_GENCGC
491         case PAGE_TABLE_CORE_ENTRY_TYPE_CODE:
492         {
493             os_vm_size_t size = *ptr;
494             os_vm_size_t fdoffset = (*(ptr+1) + 1) * (os_vm_page_size);
495             page_index_t offset = 0;
496             ssize_t bytes_read;
497             word_t data[4096];
498             word_t word;
499             lseek(fd, fdoffset + file_offset, SEEK_SET);
500             while ((bytes_read = read(fd, data, (size < 4096 ? size : 4096 )))
501                     > 0)
502             {
503                 int i = 0;
504                 size -= bytes_read;
505                 while (bytes_read) {
506                     bytes_read -= sizeof(word_t);
507                     /* Ignore all zeroes. The size of the page table
508                      * core entry was rounded up to os_vm_page_size
509                      * during the save, and might now have more
510                      * elements than the page table.
511                      *
512                      * The low bits of each word are allocation flags.
513                      */
514                     if ((word=data[i])) {
515                         page_table[offset].region_start_offset = word & ~0x03;
516                         page_table[offset].allocated = word & 0x03;
517                     }
518                     i++;
519                     offset++;
520                 }
521             }
522
523             gencgc_partial_pickup = 1;
524             break;
525         }
526 #endif
527         default:
528             lose("unknown core file entry: 0x%"WORD_FMTX"\n", val);
529         }
530
531         ptr += remaining_len;
532         FSHOW((stderr, "/new ptr=0x%"WORD_FTMX"\n", ptr));
533     }
534     SHOW("about to free(header)");
535     free(header);
536     SHOW("returning from load_core_file(..)");
537     return initial_function;
538 }
539