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.
19 #ifndef LISP_FEATURE_WIN32
20 #ifdef LISP_FEATURE_LINUX
34 #include <sys/types.h>
48 #include "gc-internal.h"
49 #include "runtime-options.h"
53 #ifdef LISP_FEATURE_SB_CORE_COMPRESSION
57 unsigned char build_id
[] =
58 #include "../../output/build-id.tmp"
62 open_binary(char *filename
, int mode
)
64 #ifdef LISP_FEATURE_WIN32
68 return open(filename
, mode
);
72 static struct runtime_options
*
73 read_runtime_options(int fd
)
75 os_vm_size_t optarray
[RUNTIME_OPTIONS_WORDS
];
76 struct runtime_options
*options
= NULL
;
78 if (read(fd
, optarray
, RUNTIME_OPTIONS_WORDS
* sizeof(os_vm_size_t
)) !=
79 RUNTIME_OPTIONS_WORDS
* sizeof(size_t)) {
83 if ((RUNTIME_OPTIONS_MAGIC
!= optarray
[0]) || (0 == optarray
[1])) {
87 options
= successful_malloc(sizeof(struct runtime_options
));
89 options
->dynamic_space_size
= optarray
[2];
90 options
->thread_control_stack_size
= optarray
[3];
96 maybe_initialize_runtime_options(int fd
)
98 struct runtime_options
*new_runtime_options
;
99 off_t end_offset
= sizeof(lispobj
) +
100 sizeof(os_vm_offset_t
) +
101 (RUNTIME_OPTIONS_WORDS
* sizeof(size_t));
103 lseek(fd
, -end_offset
, SEEK_END
);
105 if ((new_runtime_options
= read_runtime_options(fd
))) {
106 runtime_options
= new_runtime_options
;
110 /* Search 'filename' for an embedded core. An SBCL core has, at the
111 * end of the file, a trailer containing optional saved runtime
112 * options, the start of the core (an os_vm_offset_t), and a final
113 * signature word (the lispobj CORE_MAGIC). If this trailer is found
114 * at the end of the file, the start of the core can be determined
115 * from the core size.
117 * If an embedded core is present, this returns the offset into the
118 * file to load the core from, or -1 if no core is present. */
120 search_for_embedded_core(char *filename
)
123 os_vm_offset_t lispobj_size
= sizeof(lispobj
);
124 os_vm_offset_t trailer_size
= lispobj_size
+ sizeof(os_vm_offset_t
);
125 os_vm_offset_t core_start
, pos
;
128 if ((fd
= open_binary(filename
, O_RDONLY
)) < 0)
131 if (read(fd
, &header
, (size_t)lispobj_size
) < lispobj_size
)
133 if (header
== CORE_MAGIC
) {
134 /* This file is a real core, not an embedded core. Return 0 to
135 * indicate where the core starts, and do not look for runtime
136 * options in this case. */
140 if (lseek(fd
, -lispobj_size
, SEEK_END
) < 0)
142 if (read(fd
, &header
, (size_t)lispobj_size
) < lispobj_size
)
145 if (header
== CORE_MAGIC
) {
146 if (lseek(fd
, -trailer_size
, SEEK_END
) < 0)
148 if (read(fd
, &core_start
, sizeof(os_vm_offset_t
)) < 0)
151 if (lseek(fd
, core_start
, SEEK_SET
) < 0)
153 pos
= lseek(fd
, 0, SEEK_CUR
);
155 if (read(fd
, &header
, (size_t)lispobj_size
) < lispobj_size
)
158 if (header
!= CORE_MAGIC
)
161 maybe_initialize_runtime_options(fd
);
174 /* If more platforms doesn't support overlapping mmap rename this
175 * def to something like ifdef nommapoverlap */
176 /* currently hpux only */
177 #ifdef LISP_FEATURE_HPUX
178 void copy_core_bytes(int fd
, os_vm_offset_t offset
,
179 os_vm_address_t addr
, int len
)
181 unsigned char buf
[4096];
183 int old_fd
= lseek(fd
, 0, SEEK_CUR
);
186 fprintf(stderr
, "cant copy a slice of core because slice-length is not of page size(4096)\n");
190 fprintf(stderr
, "cant perform lseek() on corefile\n");
192 lseek(fd
, offset
, SEEK_SET
);
194 fprintf(stderr
, "cant perform lseek(%u,%lu,SEEK_SET) on corefile\n", fd
, offset
);
196 for(x
= 0; x
< len
; x
+= 4096){
197 c
= read(fd
, buf
, 4096);
199 fprintf(stderr
, "cant read memory area from corefile at position %lu, got %d\n", offset
+ x
, c
);
202 memcpy(addr
+x
, buf
, 4096);
204 os_flush_icache(addr
, len
);
208 #ifdef LISP_FEATURE_SB_CORE_COMPRESSION
209 # define ZLIB_BUFFER_SIZE (1u<<16)
210 void inflate_core_bytes(int fd
, os_vm_offset_t offset
,
211 os_vm_address_t addr
, int len
)
214 unsigned char buf
[ZLIB_BUFFER_SIZE
];
217 # ifdef LISP_FEATURE_WIN32
218 /* Ensure the memory is committed so zlib doesn't segfault trying to
220 os_validate_recommit(addr
, len
);
223 if (-1 == lseek(fd
, offset
, SEEK_SET
)) {
224 lose("Unable to lseek() on corefile\n");
227 stream
.zalloc
= NULL
;
229 stream
.opaque
= NULL
;
231 stream
.next_in
= buf
;
233 ret
= inflateInit(&stream
);
235 lose("zlib error %i\n", ret
);
237 stream
.next_out
= (void*)addr
;
238 stream
.avail_out
= len
;
240 ssize_t count
= read(fd
, buf
, sizeof(buf
));
242 lose("unable to read core file (errno = %i)\n", errno
);
243 stream
.next_in
= buf
;
244 stream
.avail_in
= count
;
245 if (count
== 0) break;
246 ret
= inflate(&stream
, Z_NO_FLUSH
);
251 if (stream
.avail_out
== 0)
252 lose("Runaway gzipped core directory... aborting\n");
253 if (stream
.avail_in
> 0)
254 lose("zlib inflate returned without fully"
255 "using up input buffer... aborting\n");
258 lose("zlib inflate error: %i\n", ret
);
261 } while (ret
!= Z_STREAM_END
);
263 if (stream
.avail_out
> 0) {
264 if (stream
.avail_out
>= os_vm_page_size
)
265 fprintf(stderr
, "Warning: gzipped core directory significantly"
266 "shorter than expected (%lu bytes)", (unsigned long)stream
.avail_out
);
267 /* Is this needed? */
268 memset(stream
.next_out
, 0, stream
.avail_out
);
273 # undef ZLIB_BUFFER_SIZE
276 int merge_core_pages
= -1;
278 #ifdef LISP_FEATURE_LINUX
279 os_vm_address_t anon_dynamic_space_start
;
283 process_directory(int fd
, lispobj
*ptr
, int count
, os_vm_offset_t file_offset
)
285 extern void immobile_space_coreparse(uword_t
,uword_t
);
286 struct ndir_entry
*entry
;
289 FSHOW((stderr
, "/process_directory(..), count=%d\n", count
));
291 for (entry
= (struct ndir_entry
*) ptr
; --count
>= 0; ++entry
) {
294 sword_t id
= entry
->identifier
;
295 if (id
<= (MAX_CORE_SPACE_ID
| DEFLATED_CORE_SPACE_ID_FLAG
)) {
296 if (id
& DEFLATED_CORE_SPACE_ID_FLAG
)
298 id
&= ~(DEFLATED_CORE_SPACE_ID_FLAG
);
300 sword_t offset
= os_vm_page_size
* (1 + entry
->data_page
);
301 os_vm_address_t addr
=
302 (os_vm_address_t
) (os_vm_page_size
* entry
->address
);
303 lispobj
*free_pointer
= (lispobj
*) addr
+ entry
->nwords
;
304 uword_t len
= os_vm_page_size
* entry
->page_count
;
306 FSHOW((stderr
, "/mapping %ld(0x%lx) bytes at 0x%lx\n",
307 len
, len
, (uword_t
)addr
));
309 #ifdef LISP_FEATURE_SB_CORE_COMPRESSION
310 inflate_core_bytes(fd
, offset
+ file_offset
, addr
, len
);
312 lose("This runtime was not built with zlib-compressed core support... aborting\n");
315 #ifdef LISP_FEATURE_HPUX
316 copy_core_bytes(fd
, offset
+ file_offset
, addr
, len
);
318 os_map(fd
, offset
+ file_offset
, addr
, len
);
323 #ifdef MADV_MERGEABLE
324 if ((merge_core_pages
== 1)
325 || ((merge_core_pages
== -1) && compressed
)) {
326 madvise(addr
, len
, MADV_MERGEABLE
);
329 FSHOW((stderr
, "/space id = %ld, free pointer = %p\n",
330 id
, (uword_t
)free_pointer
));
333 case DYNAMIC_CORE_SPACE_ID
:
334 if (len
> dynamic_space_size
) {
336 "dynamic space too small for core: %luKiB required, %luKiB available.\n",
337 (unsigned long)len
>> 10,
338 (unsigned long)dynamic_space_size
>> 10);
341 #ifdef LISP_FEATURE_GENCGC
342 if (addr
!= (os_vm_address_t
)DYNAMIC_SPACE_START
) {
343 fprintf(stderr
, "in core: %p; in runtime: %p \n",
344 (void*)addr
, (void*)DYNAMIC_SPACE_START
);
345 lose("core/runtime address mismatch: DYNAMIC_SPACE_START\n");
348 if ((addr
!= (os_vm_address_t
)DYNAMIC_0_SPACE_START
) &&
349 (addr
!= (os_vm_address_t
)DYNAMIC_1_SPACE_START
)) {
350 fprintf(stderr
, "in core: %p; in runtime: %p or %p\n",
352 (void*)DYNAMIC_0_SPACE_START
,
353 (void*)DYNAMIC_1_SPACE_START
);
354 lose("warning: core/runtime address mismatch: DYNAMIC_SPACE_START\n");
357 #if defined(ALLOCATION_POINTER)
358 SetSymbolValue(ALLOCATION_POINTER
, (lispobj
)free_pointer
,0);
360 dynamic_space_free_pointer
= free_pointer
;
362 /* For stop-and-copy GC, this will be whatever the GC was
363 * using at the time. With GENCGC, this will always be
364 * space 0. (We checked above that for GENCGC,
365 * addr==DYNAMIC_SPACE_START.) */
366 current_dynamic_space
= (lispobj
*)addr
;
367 #ifdef LISP_FEATURE_LINUX
368 anon_dynamic_space_start
= addr
+ len
;
369 // This assertion is here because of the test in zero_pages_with_mmap()
370 // which trusts that if addr > anon_dynamic_space_start
371 // then addr did not come from any file mapping.
372 gc_assert((lispobj
)anon_dynamic_space_start
> STATIC_SPACE_END
);
375 case STATIC_CORE_SPACE_ID
:
376 if (addr
!= (os_vm_address_t
)STATIC_SPACE_START
) {
377 fprintf(stderr
, "in core: %p - in runtime: %p\n",
378 (void*)addr
, (void*)STATIC_SPACE_START
);
379 lose("core/runtime address mismatch: STATIC_SPACE_START\n");
382 case READ_ONLY_CORE_SPACE_ID
:
383 if (addr
!= (os_vm_address_t
)READ_ONLY_SPACE_START
) {
384 fprintf(stderr
, "in core: %p - in runtime: %p\n",
385 (void*)addr
, (void*)READ_ONLY_SPACE_START
);
386 lose("core/runtime address mismatch: READ_ONLY_SPACE_START\n");
389 #ifdef LISP_FEATURE_IMMOBILE_SPACE
390 // Immobile space is subdivided into fixed-size and variable-size.
391 // There is no margin between the two, though for efficiency
392 // they are written separately to eliminate waste in the core file.
393 case IMMOBILE_FIXEDOBJ_CORE_SPACE_ID
:
394 if (addr
!= (os_vm_address_t
)IMMOBILE_SPACE_START
) {
395 fprintf(stderr
, "in core: %p - in runtime: %p\n",
396 (void*)addr
, (void*)IMMOBILE_SPACE_START
);
397 lose("core/runtime address mismatch: IMMOBILE_SPACE_START\n");
399 immobile_space_coreparse(IMMOBILE_SPACE_START
, len
);
401 case IMMOBILE_VARYOBJ_CORE_SPACE_ID
:
402 if (addr
!= (os_vm_address_t
)IMMOBILE_VARYOBJ_SUBSPACE_START
) {
403 fprintf(stderr
, "in core: %p - in runtime: %p\n",
404 (void*)addr
, (void*)IMMOBILE_VARYOBJ_SUBSPACE_START
);
405 lose("core/runtime address mismatch: IMMOBILE_VARYOBJ_SUBSPACE_START\n");
407 immobile_space_coreparse(IMMOBILE_VARYOBJ_SUBSPACE_START
, len
);
411 lose("unknown space ID %ld addr %p\n", id
, addr
);
417 load_core_file(char *file
, os_vm_offset_t file_offset
)
420 #ifndef LISP_FEATURE_ALPHA
425 os_vm_size_t len
, remaining_len
;
426 int fd
= open_binary(file
, O_RDONLY
);
428 lispobj initial_function
= NIL
;
430 FSHOW((stderr
, "/entering load_core_file(%s)\n", file
));
432 fprintf(stderr
, "could not open file \"%s\"\n", file
);
437 lseek(fd
, file_offset
, SEEK_SET
);
438 header
= calloc(os_vm_page_size
, 1);
440 count
= read(fd
, header
, os_vm_page_size
);
441 if (count
< (ssize_t
) os_vm_page_size
) {
442 lose("premature end of core file\n");
444 SHOW("successfully read first page of core");
449 if (val
!= CORE_MAGIC
) {
450 lose("invalid magic number in core: 0x%lx should have been 0x%x.\n",
454 SHOW("found CORE_MAGIC");
456 while (val
!= END_CORE_ENTRY_TYPE_CODE
) {
459 remaining_len
= len
- 2; /* (-2 to cancel the two ++ operations) */
460 FSHOW((stderr
, "/val=0x%"WORD_FMTX
", remaining_len=0x%"WORD_FMTX
"\n",
461 val
, remaining_len
));
465 case END_CORE_ENTRY_TYPE_CODE
:
466 SHOW("END_CORE_ENTRY_TYPE_CODE case");
469 case BUILD_ID_CORE_ENTRY_TYPE_CODE
:
470 SHOW("BUILD_ID_CORE_ENTRY_TYPE_CODE case");
474 FSHOW((stderr
, "build_id[]=\"%s\"\n", build_id
));
475 FSHOW((stderr
, "remaining_len = %d\n", remaining_len
));
476 if (remaining_len
!= strlen((const char *)build_id
))
477 goto losing_build_id
;
478 for (i
= 0; i
< remaining_len
; ++i
) {
479 FSHOW((stderr
, "ptr[%d] = char = %d, expected=%d\n",
480 i
, ptr
[i
], build_id
[i
]));
481 if (ptr
[i
] != build_id
[i
])
482 goto losing_build_id
;
486 /* .core files are not binary-compatible between
487 * builds because we can't easily detect whether the
488 * sources were patched between the time the
489 * dumping-the-.core runtime was built and the time
490 * that the loading-the-.core runtime was built.
492 * (We could easily detect whether version.lisp-expr
493 * was changed, but people experimenting with patches
494 * don't necessarily update version.lisp-expr.) */
496 lose("can't load .core for different runtime, sorry\n");
499 case NEW_DIRECTORY_CORE_ENTRY_TYPE_CODE
:
500 SHOW("NEW_DIRECTORY_CORE_ENTRY_TYPE_CODE case");
501 process_directory(fd
,
503 #ifndef LISP_FEATURE_ALPHA
504 remaining_len
/ (sizeof(struct ndir_entry
) /
507 remaining_len
/ (sizeof(struct ndir_entry
) /
513 case INITIAL_FUN_CORE_ENTRY_TYPE_CODE
:
514 SHOW("INITIAL_FUN_CORE_ENTRY_TYPE_CODE case");
515 initial_function
= (lispobj
)*ptr
;
518 #ifdef LISP_FEATURE_GENCGC
519 case PAGE_TABLE_CORE_ENTRY_TYPE_CODE
:
521 os_vm_size_t size
= *ptr
;
522 os_vm_size_t fdoffset
= (*(ptr
+1) + 1) * (os_vm_page_size
);
523 page_index_t offset
= 0;
527 lseek(fd
, fdoffset
+ file_offset
, SEEK_SET
);
528 while ((bytes_read
= read(fd
, data
, (size
< 4096 ? size
: 4096 )))
534 bytes_read
-= sizeof(word_t
);
535 /* Ignore all zeroes. The size of the page table
536 * core entry was rounded up to os_vm_page_size
537 * during the save, and might now have more
538 * elements than the page table.
540 * The low bits of each word are allocation flags.
542 if ((word
=data
[i
])) {
543 set_page_scan_start_offset(offset
, word
& ~0x03);
544 page_table
[offset
].allocated
= word
& 0x03;
551 gencgc_partial_pickup
= 1;
556 lose("unknown core file entry: 0x%"WORD_FMTX
"\n", val
);
559 ptr
+= remaining_len
;
560 FSHOW((stderr
, "/new ptr=0x%"WORD_FMTX
"\n", ptr
));
562 SHOW("about to free(header)");
565 SHOW("returning from load_core_file(..)");
566 return initial_function
;
569 #include "genesis/hash-table.h"
570 #include "genesis/vector.h"
571 os_vm_address_t
get_asm_routine_by_name(const char* name
)
573 lispobj routines
= SYMBOL(ASSEMBLER_ROUTINES
)->value
;
574 if (lowtag_of(routines
) == INSTANCE_POINTER_LOWTAG
) {
575 struct hash_table
* ht
= (struct hash_table
*)native_pointer(routines
);
576 struct vector
* table
= (struct vector
*)native_pointer(ht
->table
);
579 for (i
=2 ; i
< fixnum_value(table
->length
) ; i
+= 2) {
580 sym
= table
->data
[i
];
581 if (lowtag_of(sym
) == OTHER_POINTER_LOWTAG
582 && widetag_of(SYMBOL(sym
)->header
) == SYMBOL_WIDETAG
584 (char*)((struct vector
*)
585 native_pointer(SYMBOL(sym
)->name
))->data
))
586 return (os_vm_address_t
)fixnum_value(table
->data
[i
+1]);
588 // Something is wrong if we have a hashtable but find nothing.
589 fprintf(stderr
, "WARNING: get_asm_routine_by_name(%s) failed\n",
595 void asm_routine_poke(const char* routine
, int offset
, char byte
)
597 char *address
= (char *)get_asm_routine_by_name(routine
);
599 address
[offset
] = byte
;