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