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