a016ced85347c9f7078c3d1f4308011eafdc4640
[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     u32 *header, val, len, *ptr, remaining_len;
121     int fd = open(file, O_RDONLY), count;
122
123     lispobj initial_function = NIL;
124     FSHOW((stderr, "/entering load_core_file(%s)\n", file));
125     if (fd < 0) {
126         fprintf(stderr, "could not open file \"%s\"\n", file);
127         perror("open");
128         exit(1);
129     }
130
131     header = calloc(os_vm_page_size / sizeof(u32), sizeof(u32));
132
133     count = read(fd, header, os_vm_page_size);
134     if (count < os_vm_page_size) {
135         lose("premature end of core file");
136     }
137     SHOW("successfully read first page of core");
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     SHOW("found CORE_MAGIC");
148
149     while (val != CORE_END) {
150         val = *ptr++;
151         len = *ptr++;
152         remaining_len = len - 2; /* (-2 to cancel the two ++ operations) */
153         FSHOW((stderr, "/val=0x%ld, remaining_len=0x%ld\n",
154                (long)val, (long)remaining_len));
155
156         switch (val) {
157
158         case CORE_END:
159             SHOW("CORE_END case");
160             break;
161
162         case CORE_VERSION:
163             SHOW("CORE_VERSION case");
164             if (*ptr != SBCL_CORE_VERSION_INTEGER) {
165                 lose("core file version (%d) != runtime library version (%d)",
166                      *ptr,
167                      SBCL_CORE_VERSION_INTEGER);
168             }
169             break;
170
171         case CORE_NDIRECTORY:
172             SHOW("CORE_NDIRECTORY case");
173             process_directory(fd,
174                               ptr,
175 #ifndef alpha
176                               remaining_len / (sizeof(struct ndir_entry) /
177                                                sizeof(long))
178 #else
179                               remaining_len / (sizeof(struct ndir_entry) /
180                                                sizeof(u32))
181 #endif
182                               );
183             break;
184
185         case CORE_INITIAL_FUNCTION:
186             SHOW("CORE_INITIAL_FUNCTION case");
187             initial_function = (lispobj)*ptr;
188             break;
189
190         default:
191             lose("unknown core file entry: %ld", (long)val);
192         }
193
194         ptr += remaining_len;
195         FSHOW((stderr, "/new ptr=%x\n", ptr));
196     }
197     SHOW("about to free(header)");
198     free(header);
199     SHOW("returning from load_core_file(..)");
200     return initial_function;
201 }