0.9.8.7:
[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 static void
47 process_directory(int fd, u32 *ptr, int count)
48 {
49     struct ndir_entry *entry;
50
51     FSHOW((stderr, "/process_directory(..), count=%d\n", count));
52
53     for (entry = (struct ndir_entry *) ptr; --count>= 0; ++entry) {
54
55         long id = entry->identifier;
56         long offset = os_vm_page_size * (1 + entry->data_page);
57         os_vm_address_t addr =
58             (os_vm_address_t) (os_vm_page_size * entry->address);
59         lispobj *free_pointer = (lispobj *) addr + entry->nwords;
60         long len = os_vm_page_size * entry->page_count;
61
62         if (len != 0) {
63             os_vm_address_t real_addr;
64             FSHOW((stderr, "/mapping %ld(0x%lx) bytes at 0x%lx\n",
65                    (long)len, (long)len, (unsigned long)addr));
66             real_addr = os_map(fd, offset, addr, len);
67             if (real_addr != addr) {
68                 lose("file mapped in wrong place! "
69                      "(0x%08x != 0x%08lx)\n",
70                      real_addr,
71                      addr);
72             }
73         }
74
75         FSHOW((stderr, "/space id = %ld, free pointer = 0x%lx\n",
76                id, (unsigned long)free_pointer));
77
78         switch (id) {
79         case DYNAMIC_CORE_SPACE_ID:
80 #ifdef LISP_FEATURE_GENCGC
81             if (addr != (os_vm_address_t)DYNAMIC_SPACE_START) {
82                 fprintf(stderr, "in core: 0x%lx; in runtime: 0x%lx \n",
83                         (long)addr, (long)DYNAMIC_SPACE_START);
84                 lose("core/runtime address mismatch: DYNAMIC_SPACE_START\n");
85             }
86 #else
87             if ((addr != (os_vm_address_t)DYNAMIC_0_SPACE_START) &&
88                 (addr != (os_vm_address_t)DYNAMIC_1_SPACE_START)) {
89                 fprintf(stderr, "in core: 0x%lx; in runtime: 0x%lx or 0x%lx\n",
90                         (long)addr,
91                         (long)DYNAMIC_0_SPACE_START,
92                         (long)DYNAMIC_1_SPACE_START);
93                 lose("warning: core/runtime address mismatch: DYNAMIC_SPACE_START\n");
94             }
95 #endif
96 #if defined(ALLOCATION_POINTER)
97             SetSymbolValue(ALLOCATION_POINTER, (lispobj)free_pointer,0);
98 #else
99             dynamic_space_free_pointer = free_pointer;
100 #endif
101             /* For stop-and-copy GC, this will be whatever the GC was
102              * using at the time. With GENCGC, this will always be
103              * space 0. (We checked above that for GENCGC,
104              * addr==DYNAMIC_SPACE_START.) */
105             current_dynamic_space = (lispobj *)addr;
106             break;
107         case STATIC_CORE_SPACE_ID:
108             if (addr != (os_vm_address_t)STATIC_SPACE_START) {
109                 fprintf(stderr, "in core: 0x%lx - in runtime: 0x%lx\n",
110                         (long)addr, (long)STATIC_SPACE_START);
111                 lose("core/runtime address mismatch: STATIC_SPACE_START\n");
112             }
113             break;
114         case READ_ONLY_CORE_SPACE_ID:
115             if (addr != (os_vm_address_t)READ_ONLY_SPACE_START) {
116                 fprintf(stderr, "in core: 0x%lx - in runtime: 0x%lx\n",
117                         (long)addr, (long)READ_ONLY_SPACE_START);
118                 lose("core/runtime address mismatch: READ_ONLY_SPACE_START\n");
119             }
120             break;
121         default:
122             lose("unknown space ID %ld addr 0x%lx\n", id, (long)addr);
123         }
124     }
125 }
126
127 lispobj
128 load_core_file(char *file)
129 {
130     lispobj *header, val, len, *ptr, remaining_len;
131 #ifndef LISP_FEATURE_WIN32
132     int fd = open(file, O_RDONLY), count;
133 #else
134     int fd = open(file, O_RDONLY | O_BINARY), count;
135 #endif
136
137     lispobj initial_function = NIL;
138     FSHOW((stderr, "/entering load_core_file(%s)\n", file));
139     if (fd < 0) {
140         fprintf(stderr, "could not open file \"%s\"\n", file);
141         perror("open");
142         exit(1);
143     }
144
145     header = calloc(os_vm_page_size / sizeof(u32), sizeof(u32));
146
147     count = read(fd, header, os_vm_page_size);
148     if (count < os_vm_page_size) {
149         lose("premature end of core file\n");
150     }
151     SHOW("successfully read first page of core");
152
153     ptr = header;
154     val = *ptr++;
155
156     if (val != CORE_MAGIC) {
157         lose("invalid magic number in core: 0x%lx should have been 0x%x.\n",
158              val,
159              CORE_MAGIC);
160     }
161     SHOW("found CORE_MAGIC");
162
163     while (val != END_CORE_ENTRY_TYPE_CODE) {
164         val = *ptr++;
165         len = *ptr++;
166         remaining_len = len - 2; /* (-2 to cancel the two ++ operations) */
167         FSHOW((stderr, "/val=0x%ld, remaining_len=0x%ld\n",
168                (long)val, (long)remaining_len));
169
170         switch (val) {
171
172         case END_CORE_ENTRY_TYPE_CODE:
173             SHOW("END_CORE_ENTRY_TYPE_CODE case");
174             break;
175
176         case VERSION_CORE_ENTRY_TYPE_CODE:
177             SHOW("VERSION_CORE_ENTRY_TYPE_CODE case");
178             if (*ptr != SBCL_CORE_VERSION_INTEGER) {
179                 lose("core file version (%d) != runtime library version (%d)\n",
180                      *ptr,
181                      SBCL_CORE_VERSION_INTEGER);
182             }
183             break;
184
185         case BUILD_ID_CORE_ENTRY_TYPE_CODE:
186             SHOW("BUILD_ID_CORE_ENTRY_TYPE_CODE case");
187             {
188                 int i;
189
190                 FSHOW((stderr, "build_id[]=\"%s\"\n", build_id));
191                 FSHOW((stderr, "remaining_len = %d\n", remaining_len));
192                 if (remaining_len != strlen((const char *)build_id))
193                     goto losing_build_id;
194                 for (i = 0; i < remaining_len; ++i) {
195                     FSHOW((stderr, "ptr[%d] = char = %d, expected=%d\n",
196                            i, ptr[i], build_id[i]));
197                     if (ptr[i] != build_id[i])
198                         goto losing_build_id;
199                 }
200                 break;
201             losing_build_id:
202                 /* .core files are not binary-compatible between
203                  * builds because we can't easily detect whether the
204                  * sources were patched between the time the
205                  * dumping-the-.core runtime was built and the time
206                  * that the loading-the-.core runtime was built.
207                  *
208                  * (We could easily detect whether version.lisp-expr
209                  * was changed, but people experimenting with patches
210                  * don't necessarily update version.lisp-expr.) */
211
212                 lose("can't load .core for different runtime, sorry\n");
213             }
214
215         case NEW_DIRECTORY_CORE_ENTRY_TYPE_CODE:
216             SHOW("NEW_DIRECTORY_CORE_ENTRY_TYPE_CODE case");
217             process_directory(fd,
218                               ptr,
219 #ifndef LISP_FEATURE_ALPHA
220                               remaining_len / (sizeof(struct ndir_entry) /
221                                                sizeof(long))
222 #else
223                               remaining_len / (sizeof(struct ndir_entry) /
224                                                sizeof(u32))
225 #endif
226                               );
227             break;
228
229         case INITIAL_FUN_CORE_ENTRY_TYPE_CODE:
230             SHOW("INITIAL_FUN_CORE_ENTRY_TYPE_CODE case");
231             initial_function = (lispobj)*ptr;
232             break;
233
234 #ifdef LISP_FEATURE_GENCGC
235         case PAGE_TABLE_CORE_ENTRY_TYPE_CODE:
236         {
237             size_t size = *ptr;
238             size_t fdoffset = (*(ptr+1) + 1) * (os_vm_page_size);
239             size_t offset = 0;
240             long bytes_read;
241             long data[4096];
242             lseek(fd, fdoffset, SEEK_SET);
243             while ((bytes_read = read(fd, data, (size < 4096 ? size : 4096 )))
244                     > 0)
245             {
246                 int i = 0;
247                 size -= bytes_read;
248                 while (bytes_read) {
249                     bytes_read -= sizeof(long);
250                     page_table[offset++].first_object_offset = data[i++];
251                 }
252             }
253
254             gencgc_partial_pickup = 1;
255             break;
256         }
257 #endif
258         default:
259             lose("unknown core file entry: %ld\n", (long)val);
260         }
261
262         ptr += remaining_len;
263         FSHOW((stderr, "/new ptr=%lx\n", (unsigned long)ptr));
264     }
265     SHOW("about to free(header)");
266     free(header);
267     SHOW("returning from load_core_file(..)");
268     return initial_function;
269 }