b9bcddb1fef8d5d6ea183e4b64e94f0db2823c56
[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 static void
38 process_directory(int fd, u32 *ptr, int count)
39 {
40     struct ndir_entry *entry;
41
42     FSHOW((stderr, "/process_directory(..), count=%d\n", count));
43     
44     for (entry = (struct ndir_entry *) ptr; --count>= 0; ++entry) {
45
46         long id = entry->identifier;
47         long offset = os_vm_page_size * (1 + entry->data_page);
48         os_vm_address_t addr =
49             (os_vm_address_t) (os_vm_page_size * entry->address);
50         lispobj *free_pointer = (lispobj *) addr + entry->nwords;
51         long len = os_vm_page_size * entry->page_count;
52         
53         if (len != 0) {
54             os_vm_address_t real_addr;
55             FSHOW((stderr, "/mapping %ld(0x%lx) bytes at 0x%lx\n",
56                    (long)len, (long)len, addr));
57             real_addr = os_map(fd, offset, addr, len);
58             if (real_addr != addr) {
59                 lose("file mapped in wrong place! "
60                      "(0x%08x != 0x%08lx)",
61                      real_addr,
62                      addr);
63             }
64         }
65
66         FSHOW((stderr, "/space id = %d, free pointer = 0x%08x\n",
67                id, (long)free_pointer));
68
69         switch (id) {
70         case DYNAMIC_CORE_SPACE_ID:
71 #ifdef LISP_FEATURE_GENCGC        
72             if (addr != (os_vm_address_t)DYNAMIC_SPACE_START) {
73                 fprintf(stderr, "in core: 0x%lx; in runtime: 0x%lx \n",
74                         (long)addr, (long)DYNAMIC_SPACE_START);
75                 lose("core/runtime address mismatch: DYNAMIC_SPACE_START");
76             }
77 #else
78             if ((addr != (os_vm_address_t)DYNAMIC_0_SPACE_START) &&
79                 (addr != (os_vm_address_t)DYNAMIC_1_SPACE_START)) {
80                 fprintf(stderr, "in core: 0x%lx; in runtime: 0x%lx or 0x%lx\n",
81                         (long)addr,
82                         (long)DYNAMIC_0_SPACE_START,
83                         (long)DYNAMIC_1_SPACE_START);
84                 lose("warning: core/runtime address mismatch: DYNAMIC_SPACE_START");
85             }
86 #endif
87 /* FIXME: Should the conditional here be reg_ALLOC instead of
88  *   defined(__i386__)
89  * ? */
90 #if defined(LISP_FEATURE_X86)
91             SetSymbolValue(ALLOCATION_POINTER, (lispobj)free_pointer);
92 #else
93             dynamic_space_free_pointer = free_pointer;
94 #endif
95             /* For stop-and-copy GC, this will be whatever the GC was
96              * using at the time. With GENCGC, this will always be
97              * space 0. (We checked above that for GENCGC,
98              * addr==DYNAMIC_SPACE_START.) */
99             current_dynamic_space = (lispobj *)addr;
100             break;
101         case STATIC_CORE_SPACE_ID:
102             if (addr != (os_vm_address_t)STATIC_SPACE_START) {
103                 fprintf(stderr, "in core: 0x%lx - in runtime: 0x%lx\n",
104                         (long)addr, (long)STATIC_SPACE_START);
105                 lose("core/runtime address mismatch: STATIC_SPACE_START");
106             }
107             break;
108         case READ_ONLY_CORE_SPACE_ID:
109             if (addr != (os_vm_address_t)READ_ONLY_SPACE_START) {
110                 fprintf(stderr, "in core: 0x%lx - in runtime: 0x%lx\n",
111                         (long)addr, (long)READ_ONLY_SPACE_START);
112                 lose("core/runtime address mismatch: READ_ONLY_SPACE_START");
113             }
114             break;
115         default:
116             lose("unknown space ID %ld addr 0x%p", id);
117         }
118     }
119 }
120
121 lispobj
122 load_core_file(char *file)
123 {
124     u32 *header, val, len, *ptr, remaining_len;
125     int fd = open(file, O_RDONLY), count;
126
127     lispobj initial_function = NIL;
128     FSHOW((stderr, "/entering load_core_file(%s)\n", file));
129     if (fd < 0) {
130         fprintf(stderr, "could not open file \"%s\"\n", file);
131         perror("open");
132         exit(1);
133     }
134
135     header = calloc(os_vm_page_size / sizeof(u32), sizeof(u32));
136
137     count = read(fd, header, os_vm_page_size);
138     if (count < os_vm_page_size) {
139         lose("premature end of core file");
140     }
141     SHOW("successfully read first page of core");
142
143     ptr = header;
144     val = *ptr++;
145
146     if (val != CORE_MAGIC) {
147         lose("invalid magic number in core: 0x%lx should have been 0x%x.",
148              val,
149              CORE_MAGIC);
150     }
151     SHOW("found CORE_MAGIC");
152
153     while (val != END_CORE_ENTRY_TYPE_CODE) {
154         val = *ptr++;
155         len = *ptr++;
156         remaining_len = len - 2; /* (-2 to cancel the two ++ operations) */
157         FSHOW((stderr, "/val=0x%ld, remaining_len=0x%ld\n",
158                (long)val, (long)remaining_len));
159
160         switch (val) {
161
162         case END_CORE_ENTRY_TYPE_CODE:
163             SHOW("END_CORE_ENTRY_TYPE_CODE case");
164             break;
165
166         case VERSION_CORE_ENTRY_TYPE_CODE:
167             SHOW("VERSION_CORE_ENTRY_TYPE_CODE case");
168             if (*ptr != SBCL_CORE_VERSION_INTEGER) {
169                 lose("core file version (%d) != runtime library version (%d)",
170                      *ptr,
171                      SBCL_CORE_VERSION_INTEGER);
172             }
173             break;
174
175         case NEW_DIRECTORY_CORE_ENTRY_TYPE_CODE:
176             SHOW("NEW_DIRECTORY_CORE_ENTRY_TYPE_CODE case");
177             process_directory(fd,
178                               ptr,
179 #ifndef alpha
180                               remaining_len / (sizeof(struct ndir_entry) /
181                                                sizeof(long))
182 #else
183                               remaining_len / (sizeof(struct ndir_entry) /
184                                                sizeof(u32))
185 #endif
186                               );
187             break;
188
189         case INITIAL_FUN_CORE_ENTRY_TYPE_CODE:
190             SHOW("INITIAL_FUN_CORE_ENTRY_TYPE_CODE case");
191             initial_function = (lispobj)*ptr;
192             break;
193
194         default:
195             lose("unknown core file entry: %ld", (long)val);
196         }
197
198         ptr += remaining_len;
199         FSHOW((stderr, "/new ptr=%x\n", ptr));
200     }
201     SHOW("about to free(header)");
202     free(header);
203     SHOW("returning from load_core_file(..)");
204     return initial_function;
205 }