2 * A saved SBCL system is a .core file; the code here helps us accept
3 * such a file as input.
7 * This software is part of the SBCL system. See the README file for
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.
21 #include <sys/types.h>
36 #include "gc-internal.h"
39 #if defined(LISP_FEATURE_SB_THREAD) && defined(LISP_FEATURE_SB_LUTEX)
40 #include "genesis/sap.h"
41 #include "pthread-lutex.h"
45 unsigned char build_id
[] =
46 #include "../../output/build-id.tmp"
50 open_binary(char *filename
, int mode
)
52 #ifdef LISP_FEATURE_WIN32
56 return open(filename
, mode
);
60 static struct runtime_options
*
61 read_runtime_options(int fd
)
63 size_t optarray
[RUNTIME_OPTIONS_WORDS
];
64 struct runtime_options
*options
= NULL
;
66 if (read(fd
, optarray
, RUNTIME_OPTIONS_WORDS
* sizeof(size_t)) !=
67 RUNTIME_OPTIONS_WORDS
* sizeof(size_t)) {
71 if ((RUNTIME_OPTIONS_MAGIC
!= optarray
[0]) || (0 == optarray
[1])) {
75 options
= successful_malloc(sizeof(struct runtime_options
));
77 options
->dynamic_space_size
= optarray
[2];
78 options
->thread_control_stack_size
= optarray
[3];
84 maybe_initialize_runtime_options(int fd
)
86 off_t end_offset
= sizeof(lispobj
) +
87 sizeof(os_vm_offset_t
) +
88 (RUNTIME_OPTIONS_WORDS
* sizeof(size_t));
90 lseek(fd
, -end_offset
, SEEK_END
);
91 runtime_options
= read_runtime_options(fd
);
94 /* Search 'filename' for an embedded core. An SBCL core has, at the
95 * end of the file, a trailer containing optional saved runtime
96 * options, the start of the core (an os_vm_offset_t), and a final
97 * signature word (the lispobj CORE_MAGIC). If this trailer is found
98 * at the end of the file, the start of the core can be determined
101 * If an embedded core is present, this returns the offset into the
102 * file to load the core from, or -1 if no core is present. */
104 search_for_embedded_core(char *filename
)
107 os_vm_offset_t lispobj_size
= sizeof(lispobj
);
108 os_vm_offset_t trailer_size
= lispobj_size
+ sizeof(os_vm_offset_t
);
109 os_vm_offset_t core_start
, pos
;
112 if ((fd
= open_binary(filename
, O_RDONLY
)) < 0)
114 if (lseek(fd
, -lispobj_size
, SEEK_END
) < 0)
116 if (read(fd
, &header
, (size_t)lispobj_size
) < lispobj_size
)
119 if (header
== CORE_MAGIC
) {
120 if (lseek(fd
, -trailer_size
, SEEK_END
) < 0)
122 if (read(fd
, &core_start
, sizeof(os_vm_offset_t
)) < 0)
125 if (lseek(fd
, core_start
, SEEK_SET
) < 0)
127 pos
= lseek(fd
, 0, SEEK_CUR
);
129 if (read(fd
, &header
, (size_t)lispobj_size
) < lispobj_size
)
132 if (header
!= CORE_MAGIC
)
135 maybe_initialize_runtime_options(fd
);
149 process_directory(int fd
, lispobj
*ptr
, int count
, os_vm_offset_t file_offset
)
151 struct ndir_entry
*entry
;
153 FSHOW((stderr
, "/process_directory(..), count=%d\n", count
));
155 for (entry
= (struct ndir_entry
*) ptr
; --count
>= 0; ++entry
) {
157 long id
= entry
->identifier
;
158 long offset
= os_vm_page_size
* (1 + entry
->data_page
);
159 os_vm_address_t addr
=
160 (os_vm_address_t
) (os_vm_page_size
* entry
->address
);
161 lispobj
*free_pointer
= (lispobj
*) addr
+ entry
->nwords
;
162 unsigned long len
= os_vm_page_size
* entry
->page_count
;
165 os_vm_address_t real_addr
;
166 FSHOW((stderr
, "/mapping %ld(0x%lx) bytes at 0x%lx\n",
167 (long)len
, (long)len
, (unsigned long)addr
));
168 real_addr
= os_map(fd
, offset
+ file_offset
, addr
, len
);
169 if (real_addr
!= addr
) {
170 lose("file mapped in wrong place! "
171 "(0x%08x != 0x%08lx)\n",
177 FSHOW((stderr
, "/space id = %ld, free pointer = 0x%lx\n",
178 id
, (unsigned long)free_pointer
));
181 case DYNAMIC_CORE_SPACE_ID
:
182 if (len
> dynamic_space_size
) {
184 "dynamic space too small for core: %ldKiB required, %ldKiB available.\n",
186 (long)dynamic_space_size
>> 10);
189 #ifdef LISP_FEATURE_GENCGC
190 if (addr
!= (os_vm_address_t
)DYNAMIC_SPACE_START
) {
191 fprintf(stderr
, "in core: 0x%lx; in runtime: 0x%lx \n",
192 (long)addr
, (long)DYNAMIC_SPACE_START
);
193 lose("core/runtime address mismatch: DYNAMIC_SPACE_START\n");
196 if ((addr
!= (os_vm_address_t
)DYNAMIC_0_SPACE_START
) &&
197 (addr
!= (os_vm_address_t
)DYNAMIC_1_SPACE_START
)) {
198 fprintf(stderr
, "in core: 0x%lx; in runtime: 0x%lx or 0x%lx\n",
200 (long)DYNAMIC_0_SPACE_START
,
201 (long)DYNAMIC_1_SPACE_START
);
202 lose("warning: core/runtime address mismatch: DYNAMIC_SPACE_START\n");
205 #if defined(ALLOCATION_POINTER)
206 SetSymbolValue(ALLOCATION_POINTER
, (lispobj
)free_pointer
,0);
208 dynamic_space_free_pointer
= free_pointer
;
210 /* For stop-and-copy GC, this will be whatever the GC was
211 * using at the time. With GENCGC, this will always be
212 * space 0. (We checked above that for GENCGC,
213 * addr==DYNAMIC_SPACE_START.) */
214 current_dynamic_space
= (lispobj
*)addr
;
216 case STATIC_CORE_SPACE_ID
:
217 if (addr
!= (os_vm_address_t
)STATIC_SPACE_START
) {
218 fprintf(stderr
, "in core: 0x%lx - in runtime: 0x%lx\n",
219 (long)addr
, (long)STATIC_SPACE_START
);
220 lose("core/runtime address mismatch: STATIC_SPACE_START\n");
223 case READ_ONLY_CORE_SPACE_ID
:
224 if (addr
!= (os_vm_address_t
)READ_ONLY_SPACE_START
) {
225 fprintf(stderr
, "in core: 0x%lx - in runtime: 0x%lx\n",
226 (long)addr
, (long)READ_ONLY_SPACE_START
);
227 lose("core/runtime address mismatch: READ_ONLY_SPACE_START\n");
231 lose("unknown space ID %ld addr 0x%lx\n", id
, (long)addr
);
237 load_core_file(char *file
, os_vm_offset_t file_offset
)
239 lispobj
*header
, val
, len
, *ptr
, remaining_len
;
240 int fd
= open_binary(file
, O_RDONLY
);
243 lispobj initial_function
= NIL
;
244 FSHOW((stderr
, "/entering load_core_file(%s)\n", file
));
246 fprintf(stderr
, "could not open file \"%s\"\n", file
);
251 lseek(fd
, file_offset
, SEEK_SET
);
252 header
= calloc(os_vm_page_size
/ sizeof(u32
), sizeof(u32
));
254 count
= read(fd
, header
, os_vm_page_size
);
255 if (count
< os_vm_page_size
) {
256 lose("premature end of core file\n");
258 SHOW("successfully read first page of core");
263 if (val
!= CORE_MAGIC
) {
264 lose("invalid magic number in core: 0x%lx should have been 0x%x.\n",
268 SHOW("found CORE_MAGIC");
270 while (val
!= END_CORE_ENTRY_TYPE_CODE
) {
273 remaining_len
= len
- 2; /* (-2 to cancel the two ++ operations) */
274 FSHOW((stderr
, "/val=0x%ld, remaining_len=0x%ld\n",
275 (long)val
, (long)remaining_len
));
279 case END_CORE_ENTRY_TYPE_CODE
:
280 SHOW("END_CORE_ENTRY_TYPE_CODE case");
283 case VERSION_CORE_ENTRY_TYPE_CODE
:
284 SHOW("VERSION_CORE_ENTRY_TYPE_CODE case");
285 if (*ptr
!= SBCL_CORE_VERSION_INTEGER
) {
286 lose("core file version (%d) != runtime library version (%d)\n",
288 SBCL_CORE_VERSION_INTEGER
);
292 case BUILD_ID_CORE_ENTRY_TYPE_CODE
:
293 SHOW("BUILD_ID_CORE_ENTRY_TYPE_CODE case");
297 FSHOW((stderr
, "build_id[]=\"%s\"\n", build_id
));
298 FSHOW((stderr
, "remaining_len = %d\n", remaining_len
));
299 if (remaining_len
!= strlen((const char *)build_id
))
300 goto losing_build_id
;
301 for (i
= 0; i
< remaining_len
; ++i
) {
302 FSHOW((stderr
, "ptr[%d] = char = %d, expected=%d\n",
303 i
, ptr
[i
], build_id
[i
]));
304 if (ptr
[i
] != build_id
[i
])
305 goto losing_build_id
;
309 /* .core files are not binary-compatible between
310 * builds because we can't easily detect whether the
311 * sources were patched between the time the
312 * dumping-the-.core runtime was built and the time
313 * that the loading-the-.core runtime was built.
315 * (We could easily detect whether version.lisp-expr
316 * was changed, but people experimenting with patches
317 * don't necessarily update version.lisp-expr.) */
319 lose("can't load .core for different runtime, sorry\n");
322 case NEW_DIRECTORY_CORE_ENTRY_TYPE_CODE
:
323 SHOW("NEW_DIRECTORY_CORE_ENTRY_TYPE_CODE case");
324 process_directory(fd
,
326 #ifndef LISP_FEATURE_ALPHA
327 remaining_len
/ (sizeof(struct ndir_entry
) /
330 remaining_len
/ (sizeof(struct ndir_entry
) /
336 case INITIAL_FUN_CORE_ENTRY_TYPE_CODE
:
337 SHOW("INITIAL_FUN_CORE_ENTRY_TYPE_CODE case");
338 initial_function
= (lispobj
)*ptr
;
341 #if defined(LISP_FEATURE_SB_THREAD) && defined(LISP_FEATURE_SB_LUTEX)
342 case LUTEX_TABLE_CORE_ENTRY_TYPE_CODE
:
343 SHOW("LUTEX_TABLE_CORE_ENTRY_TYPE_CODE case");
345 size_t n_lutexes
= *ptr
;
346 size_t fdoffset
= (*(ptr
+ 1) + 1) * (os_vm_page_size
);
347 size_t data_length
= n_lutexes
* sizeof(struct sap
*);
348 struct lutex
**lutexes_to_resurrect
= malloc(data_length
);
351 lseek(fd
, fdoffset
+ file_offset
, SEEK_SET
);
353 FSHOW((stderr
, "attempting to read %ld lutexes from core\n", n_lutexes
));
354 bytes_read
= read(fd
, lutexes_to_resurrect
, data_length
);
357 if (bytes_read
!= data_length
) {
358 lose("Could not read the lutex table");
363 for (i
=0; i
<n_lutexes
; ++i
) {
364 struct lutex
*lutex
= lutexes_to_resurrect
[i
];
366 FSHOW((stderr
, "re-init'ing lutex @ %p\n", lutex
));
367 lutex_init((tagged_lutex_t
) lutex
);
370 free(lutexes_to_resurrect
);
376 #ifdef LISP_FEATURE_GENCGC
377 case PAGE_TABLE_CORE_ENTRY_TYPE_CODE
:
380 size_t fdoffset
= (*(ptr
+1) + 1) * (os_vm_page_size
);
383 unsigned long data
[4096];
384 lseek(fd
, fdoffset
+ file_offset
, SEEK_SET
);
385 while ((bytes_read
= read(fd
, data
, (size
< 4096 ? size
: 4096 )))
391 bytes_read
-= sizeof(long);
392 /* Ignore all zeroes. The size of the page table
393 * core entry was rounded up to os_vm_page_size
394 * during the save, and might now have more
395 * elements than the page table.
398 page_table
[offset
].region_start_offset
= data
[i
];
405 gencgc_partial_pickup
= 1;
410 lose("unknown core file entry: %ld\n", (long)val
);
413 ptr
+= remaining_len
;
414 FSHOW((stderr
, "/new ptr=%lx\n", (unsigned long)ptr
));
416 SHOW("about to free(header)");
418 SHOW("returning from load_core_file(..)");
419 return initial_function
;