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