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