0.6.12.7.flaky1.1:
[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             /* For stop-and-copy GC, this will be whatever the GC was
92              * using at the time. With GENCGC, this will always be
93              * space 0. (We checked above that for GENCGC,
94              * addr==DYNAMIC_SPACE_START.) */
95             current_dynamic_space = (lispobj *)addr;
96             break;
97         case STATIC_SPACE_ID:
98             if (addr != (os_vm_address_t)STATIC_SPACE_START) {
99                 fprintf(stderr, "in core: 0x%p - in runtime: 0x%x\n",
100                         addr, (os_vm_address_t)STATIC_SPACE_START);
101                 lose("core/runtime address mismatch: STATIC_SPACE_START");
102             }
103             break;
104         case READ_ONLY_SPACE_ID:
105             if (addr != (os_vm_address_t)READ_ONLY_SPACE_START) {
106                 fprintf(stderr, "in core: 0x%x - in runtime: 0x%x\n",
107                         addr, (os_vm_address_t)READ_ONLY_SPACE_START);
108                 lose("core/runtime address mismatch: READ_ONLY_SPACE_START");
109             }
110             break;
111         default:
112             lose("unknown space ID %ld addr 0x%p", id);
113         }
114     }
115 }
116
117 lispobj
118 load_core_file(char *file)
119 {
120     int fd = open(file, O_RDONLY), count;
121
122     /* KLUDGE: This kind of conditionalization everywhere that 32-bit
123      * ints are used is really nasty. It would be much nicer to define
124      * a typedef like addr_as_int once and for all in each
125      * architecture file, then use that everywhere. -- WHN 19990904 */
126 #ifndef alpha
127     long *header,  val, len, *ptr;
128     long remaining_len;
129 #else
130     u32 *header, val, len, *ptr;
131     u32 remaining_len;
132 #endif
133
134     lispobj initial_function = NIL;
135     FSHOW((stderr, "/entering load_core_file(%s)\n", file));
136     if (fd < 0) {
137         fprintf(stderr, "could not open file \"%s\"\n", file);
138         perror("open");
139         exit(1);
140     }
141
142     header = calloc(os_vm_page_size / sizeof(u32), sizeof(u32));
143
144     count = read(fd, header, os_vm_page_size);
145     if (count < os_vm_page_size) {
146         lose("premature end of core file");
147     }
148     SHOW("successfully read first page of core");
149
150     ptr = header;
151     val = *ptr++;
152
153     if (val != CORE_MAGIC) {
154         lose("invalid magic number in core: 0x%lx should have been 0x%x.",
155              val,
156              CORE_MAGIC);
157     }
158     SHOW("found CORE_MAGIC");
159
160     while (val != CORE_END) {
161         val = *ptr++;
162         len = *ptr++;
163         remaining_len = len - 2; /* (-2 to cancel the two ++ operations) */
164         FSHOW((stderr, "/val=0x%ld, remaining_len=0x%ld\n",
165                (long)val, (long)remaining_len));
166
167         switch (val) {
168
169         case CORE_END:
170             SHOW("CORE_END case");
171             break;
172
173         case CORE_VERSION:
174             SHOW("CORE_VERSION case");
175             if (*ptr != SBCL_CORE_VERSION_INTEGER) {
176                 lose("core file version (%d) != runtime library version (%d)",
177                      *ptr,
178                      SBCL_CORE_VERSION_INTEGER);
179             }
180             break;
181
182         case CORE_NDIRECTORY:
183             SHOW("CORE_NDIRECTORY case");
184             process_directory(fd,
185                               ptr,
186 #ifndef alpha
187                               remaining_len / (sizeof(struct ndir_entry) /
188                                                sizeof(long))
189 #else
190                               remaining_len / (sizeof(struct ndir_entry) /
191                                                sizeof(u32))
192 #endif
193                               );
194             break;
195
196         case CORE_INITIAL_FUNCTION:
197             SHOW("CORE_INITIAL_FUNCTION case");
198             initial_function = (lispobj)*ptr;
199             break;
200
201         default:
202             lose("unknown core file entry: %ld", (long)val);
203         }
204
205         ptr += remaining_len;
206         FSHOW((stderr, "/new ptr=%x\n", ptr));
207     }
208     SHOW("about to free(header)");
209     free(header);
210     SHOW("returning from load_core_file(..)");
211     return initial_function;
212 }