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