7221bd082ede112bb8cefa67ceb70e6f0b230c4c
[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 = CORE_PAGESIZE * (1 + entry->data_page);
44         os_vm_address_t addr =
45             (os_vm_address_t) (CORE_PAGESIZE * entry->address);
46         lispobj *free_pointer = (lispobj *) addr + entry->nwords;
47         long len = CORE_PAGESIZE * 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             if (addr != (os_vm_address_t)DYNAMIC_SPACE_START) {
67                 lose("core/runtime address mismatch: DYNAMIC_SPACE_START");
68             }
69 #if defined(ibmrt) || defined(__i386__)
70             SetSymbolValue(ALLOCATION_POINTER, (lispobj)free_pointer);
71 #else
72             dynamic_space_free_pointer = free_pointer;
73 #endif
74             break;
75         case STATIC_SPACE_ID:
76             if (addr != (os_vm_address_t)STATIC_SPACE_START) {
77                 lose("core/runtime address mismatch: STATIC_SPACE_START");
78             }
79             break;
80         case READ_ONLY_SPACE_ID:
81             if (addr != (os_vm_address_t)READ_ONLY_SPACE_START) {
82                 lose("core/runtime address mismatch: READ_ONLY_SPACE_START");
83             }
84             break;
85         default:
86             lose("unknown space ID %ld", id);
87         }
88     }
89 }
90
91 lispobj load_core_file(char *file)
92 {
93     int fd = open(file, O_RDONLY), count;
94
95     /* KLUDGE: This kind of conditionalization everywhere that 32-bit
96      * ints are used is really nasty. It would be much nicer to define
97      * a typedef like addr_as_int once and for all in each
98      * architecture file, then use that everywhere. -- WHN 19990904 */
99 #ifndef alpha
100     long header[CORE_PAGESIZE / sizeof(long)], val, len, *ptr;
101     long remaining_len;
102 #else
103     u32 header[CORE_PAGESIZE / sizeof(u32)], val, len, *ptr;
104     u32 remaining_len;
105 #endif
106
107     lispobj initial_function = NIL;
108
109     if (fd < 0) {
110         fprintf(stderr, "could not open file \"%s\"\n", file);
111         perror("open");
112         exit(1);
113     }
114
115     count = read(fd, header, CORE_PAGESIZE);
116     if (count < CORE_PAGESIZE) {
117         lose("premature end of core file");
118     }
119
120     ptr = header;
121     val = *ptr++;
122
123     if (val != CORE_MAGIC) {
124         lose("invalid magic number in core: 0x%lx should have been 0x%x.",
125              val,
126              CORE_MAGIC);
127     }
128
129     while (val != CORE_END) {
130         val = *ptr++;
131         len = *ptr++;
132         remaining_len = len - 2; /* (-2 to cancel the two ++ operations) */
133
134         switch (val) {
135
136         case CORE_END:
137             break;
138
139         case CORE_VERSION:
140             if (*ptr != SBCL_CORE_VERSION_INTEGER) {
141                 lose("core file version (%d) != runtime library version (%d)",
142                      *ptr,
143                      SBCL_CORE_VERSION_INTEGER);
144             }
145             break;
146
147         case CORE_NDIRECTORY:
148             process_directory(fd,
149                               ptr,
150 #ifndef alpha
151                               remaining_len / (sizeof(struct ndir_entry) /
152                                                sizeof(long))
153 #else
154                               remaining_len / (sizeof(struct ndir_entry) /
155                                                sizeof(u32))
156 #endif
157                               );
158             break;
159
160         case CORE_INITIAL_FUNCTION:
161             initial_function = (lispobj)*ptr;
162             break;
163
164         default:
165             lose("unknown core file entry: %ld", val);
166         }
167
168         ptr += remaining_len;
169     }
170
171     return initial_function;
172 }