Clean up RCS ids
[projects/cmucl/cmucl.git] / src / lisp / coreparse.c
1 /*
2
3  This code was written as part of the CMU Common Lisp project at
4  Carnegie Mellon University, and has been placed in the public domain.
5
6 */
7
8 #include <stdio.h>
9 #include <sys/types.h>
10 #include <sys/file.h>
11
12 #include <fcntl.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15
16 #include "os.h"
17 #include "lisp.h"
18 #include "globals.h"
19 #include "core.h"
20 #include "internals.h"
21
22 extern int version;
23
24 static void
25 process_directory(int fd, long *ptr, int count)
26 {
27     long id, offset, len;
28     lispobj *free_pointer;
29     os_vm_address_t addr;
30     struct ndir_entry *entry;
31
32     entry = (struct ndir_entry *) ptr;
33
34     while (count-- > 0) {
35         id = entry->identifier;
36         offset = CORE_PAGESIZE * (1 + entry->data_page);
37         addr = (os_vm_address_t) (CORE_PAGESIZE * entry->address);
38         free_pointer = (lispobj *) addr + entry->nwords;
39         len = CORE_PAGESIZE * entry->page_count;
40
41         if (len != 0) {
42             os_vm_address_t real_addr;
43
44 #ifdef PRINTNOISE
45             printf("Mapping %ld bytes at 0x%lx.\n", len, addr);
46 #endif
47             real_addr = os_map(fd, offset, addr, len);
48             if (real_addr != addr)
49                 fprintf(stderr,
50                         "process_directory: file mapped in wrong place! (0x%p != 0x%p)\n",
51                         (void *) real_addr, (void *) addr);
52         }
53 #if 0
54         printf("Space ID = %d, free pointer = 0x%08x.\n", id, free_pointer);
55 #endif
56
57         switch (id) {
58           case DYNAMIC_SPACE_ID:
59               if (addr != (os_vm_address_t) dynamic_0_space
60                   && addr != (os_vm_address_t) dynamic_1_space)
61                   printf("Strange ... dynamic space lossage.\n");
62               current_dynamic_space = (lispobj *) addr;
63 #if defined(ibmrt) || defined(i386) || defined(__x86_64)
64               SetSymbolValue(ALLOCATION_POINTER, (lispobj) free_pointer);
65 #else
66               current_dynamic_space_free_pointer = free_pointer;
67 #endif
68               break;
69           case STATIC_SPACE_ID:
70               static_space = (lispobj *) addr;
71               if (len >= static_space_size) {
72                   fprintf(stderr, "Error:  Static space size (%ld) exceeds allocated space (%ld)!\n",
73                           len, static_space_size);
74                   exit(1);
75               }
76               break;
77           case READ_ONLY_SPACE_ID:
78               /* Don't care about read only space */
79               if (len >= read_only_space_size) {
80                   fprintf(stderr, "Error:  Read only space size (%ld) exceeds allocated space (%lu)!\n",
81                           len, read_only_space_size);
82                   exit(1);
83               }
84               break;
85           default:
86               printf("Strange space ID: %ld; ignored.\n", id);
87               break;
88         }
89         entry++;
90     }
91 }
92
93 lispobj
94 load_core_file(const char *file, fpu_mode_t* fpu_type)
95 {
96     int fd = open(file, O_RDONLY), count;
97
98 #if !(defined(alpha) || defined(__x86_64))
99     long header[CORE_PAGESIZE / sizeof(long)], val, len, *ptr;
100 #else
101     u32 header[CORE_PAGESIZE / sizeof(u32)], val, len, *ptr;
102 #endif
103     lispobj initial_function = NIL;
104
105     if (fd < 0) {
106         fprintf(stderr, "Could not open file \"%s\".\n", file);
107         perror("open");
108         exit(1);
109     }
110
111     count = read(fd, header, CORE_PAGESIZE);
112     if (count < 0) {
113         perror("read");
114         exit(1);
115     }
116     if (count < CORE_PAGESIZE) {
117         fprintf(stderr, "Premature EOF.\n");
118         exit(1);
119     }
120
121     ptr = header;
122     val = *ptr++;
123
124     if (val != CORE_MAGIC) {
125         fprintf(stderr, "Invalid magic number: 0x%lx should have been 0x%x.\n",
126                 val, CORE_MAGIC);
127         exit(1);
128     }
129
130     while (val != CORE_END) {
131         val = *ptr++;
132         len = *ptr++;
133
134         switch (val) {
135           case CORE_END:
136               break;
137
138           case CORE_VERSION:
139               if (*ptr != version) {
140                   fprintf(stderr,
141                           "WARNING: startup-code version (%d) different from core version (%ld).\nYou may lose big.\n",
142                           version, *ptr);
143               }
144               if (len == 4) {
145                 *fpu_type = (fpu_mode_t) ptr[1];
146               } else {
147                   *fpu_type = AUTO;
148               }
149               break;
150
151           case CORE_VALIDATE:
152               fprintf(stderr, "Validation no longer supported; ignored.\n");
153               break;
154
155           case CORE_NDIRECTORY:
156               process_directory(fd, ptr,
157 #if !(defined(alpha) || defined(__x86_64))
158                                 (len - 2) / (sizeof(struct ndir_entry) / sizeof(long)));
159 #else
160                                 (len - 2) / (sizeof(struct ndir_entry) / sizeof(u32)));
161 #endif
162               break;
163
164           case CORE_INITIAL_FUNCTION:
165               initial_function = (lispobj) * ptr;
166               break;
167
168           case CORE_MACHINE_STATE:
169               fprintf(stderr, "Obsolete core file.\n");
170               exit(1);
171               break;
172
173           default:
174               printf("Unknown core file entry: %ld; skipping.\n", val);
175               break;
176         }
177
178         ptr += len - 2;
179     }
180
181     return initial_function;
182 }