ffcce84291cb9a42c120ce4500478cdb30e03598
[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 <sys/types.h>
19 #include <sys/file.h>
20
21 #ifdef irix
22 #include <fcntl.h>
23 #include <stdlib.h>
24 #endif
25
26 #include "os.h"
27 #include "runtime.h"
28 #include "globals.h"
29 #include "core.h"
30 #include "arch.h"
31 #include "interr.h"
32 #include "sbcl.h"
33
34 static void process_directory(int fd, long *ptr, int count)
35 {
36     struct ndir_entry *entry;
37
38     FSHOW((stderr, "process_directory(..), count=%d\n", count));
39     
40     for (entry = (struct ndir_entry *) ptr; --count>= 0; ++entry) {
41
42         long id = entry->identifier;
43         long offset = os_vm_page_size * (1 + entry->data_page);
44         os_vm_address_t addr =
45             (os_vm_address_t) (os_vm_page_size * entry->address);
46         lispobj *free_pointer = (lispobj *) addr + entry->nwords;
47         long len = os_vm_page_size * entry->page_count;
48         
49         if (len != 0) {
50             os_vm_address_t real_addr;
51             FSHOW((stderr, "mapping %ld bytes at 0x%lx\n", len, addr));
52             real_addr = os_map(fd, offset, addr, len);
53             if (real_addr != addr) {
54                 lose("file mapped in wrong place! "
55                      "(0x%08x != 0x%08lx)",
56                      real_addr,
57                      addr);
58             }
59         }
60
61         FSHOW((stderr, "space id = %d, free pointer = 0x%08x\n",
62                id, free_pointer));
63
64         switch (id) {
65         case DYNAMIC_SPACE_ID:
66 #ifdef GENCGC     
67             if (addr != (os_vm_address_t)DYNAMIC_SPACE_START) {
68                 fprintf(stderr, "in core: 0x%x - in runtime: 0x%x \n",
69                         addr, (os_vm_address_t)DYNAMIC_SPACE_START);
70                 fprintf(stderr,"warning: core/runtime address mismatch: DYNAMIC_SPACE_START");
71             }
72 #else
73             if ((addr != (os_vm_address_t)DYNAMIC_0_SPACE_START) &&
74                 (addr != (os_vm_address_t)DYNAMIC_1_SPACE_START)) {
75                 fprintf(stderr, "in core: 0x%x - in runtime: 0x%x or 0x%x\n",
76                         addr, (os_vm_address_t)DYNAMIC_0_SPACE_START,
77                         (os_vm_address_t)DYNAMIC_1_SPACE_START);
78                 fprintf(stderr,"warning: core/runtime address mismatch: DYNAMIC_SPACE_START");
79             }
80 #endif
81 #if defined(ibmrt) || defined(__i386__)
82             SetSymbolValue(ALLOCATION_POINTER, (lispobj)free_pointer);
83 #else
84             dynamic_space_free_pointer = free_pointer;
85 #endif
86             /* on the x86, this will always be space 0 */
87             current_dynamic_space = (lispobj *)addr;
88             break;
89         case STATIC_SPACE_ID:
90             if (addr != (os_vm_address_t)STATIC_SPACE_START) {
91                 fprintf(stderr, "in core: 0x%p - in runtime: 0x%x\n",
92                         addr, (os_vm_address_t)STATIC_SPACE_START);
93                 lose("core/runtime address mismatch: STATIC_SPACE_START");
94             }
95             break;
96         case READ_ONLY_SPACE_ID:
97             if (addr != (os_vm_address_t)READ_ONLY_SPACE_START) {
98                 fprintf(stderr, "in core: 0x%x - in runtime: 0x%x\n",
99                         addr, (os_vm_address_t)READ_ONLY_SPACE_START);
100                 lose("core/runtime address mismatch: READ_ONLY_SPACE_START");
101             }
102             break;
103         default:
104             lose("unknown space ID %ld addr 0x%p", id);
105         }
106     }
107 }
108
109 lispobj load_core_file(char *file)
110 {
111     int fd = open(file, O_RDONLY), count;
112
113     /* KLUDGE: This kind of conditionalization everywhere that 32-bit
114      * ints are used is really nasty. It would be much nicer to define
115      * a typedef like addr_as_int once and for all in each
116      * architecture file, then use that everywhere. -- WHN 19990904 */
117 #ifndef alpha
118     long *header,  val, len, *ptr;
119     long remaining_len;
120 #else
121     u32 *header, val, len, *ptr;
122     u32 remaining_len;
123 #endif
124
125     lispobj initial_function = NIL;
126     if (fd < 0) {
127         fprintf(stderr, "could not open file \"%s\"\n", file);
128         perror("open");
129         exit(1);
130     }
131
132     header=calloc(os_vm_page_size / sizeof(u32),sizeof(u32));
133
134     count = read(fd, header, os_vm_page_size);
135     if (count < os_vm_page_size) {
136         lose("premature end of core file");
137     }
138
139     ptr = header;
140     val = *ptr++;
141
142     if (val != CORE_MAGIC) {
143         lose("invalid magic number in core: 0x%lx should have been 0x%x.",
144              val,
145              CORE_MAGIC);
146     }
147
148     while (val != CORE_END) {
149         val = *ptr++;
150         len = *ptr++;
151         remaining_len = len - 2; /* (-2 to cancel the two ++ operations) */
152
153         switch (val) {
154
155         case CORE_END:
156             break;
157
158         case CORE_VERSION:
159             if (*ptr != SBCL_CORE_VERSION_INTEGER) {
160                 lose("core file version (%d) != runtime library version (%d)",
161                      *ptr,
162                      SBCL_CORE_VERSION_INTEGER);
163             }
164             break;
165
166         case CORE_NDIRECTORY:
167             process_directory(fd,
168                               ptr,
169 #ifndef alpha
170                               remaining_len / (sizeof(struct ndir_entry) /
171                                                sizeof(long))
172 #else
173                               remaining_len / (sizeof(struct ndir_entry) /
174                                                sizeof(u32))
175 #endif
176                               );
177             break;
178
179         case CORE_INITIAL_FUNCTION:
180             initial_function = (lispobj)*ptr;
181             break;
182
183         default:
184             lose("unknown core file entry: %ld", val);
185         }
186
187         ptr += remaining_len;
188     }
189     free(header);
190     return initial_function;
191 }