Halve the number of syscalls in zero_pages_with_mmap on Linux
[sbcl.git] / src / runtime / coreparse.c
blobee29cd9c93c10d1b1364cd45de6b5ae450c31a63
1 /*
2 * A saved SBCL system is a .core file; the code here helps us accept
3 * such a file as input.
4 */
6 /*
7 * This software is part of the SBCL system. See the README file for
8 * more information.
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.
17 #include "sbcl.h"
19 #ifndef LISP_FEATURE_WIN32
20 #ifdef LISP_FEATURE_LINUX
21 /* For madvise */
22 #define _BSD_SOURCE
23 #include <sys/mman.h>
24 #undef _BSD_SOURCE
25 #else
26 #include <sys/mman.h>
27 #endif
28 #endif
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <sys/file.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <fcntl.h>
37 #include <unistd.h>
39 #include "os.h"
40 #include "runtime.h"
41 #include "globals.h"
42 #include "core.h"
43 #include "arch.h"
44 #include "interr.h"
45 #include "thread.h"
47 #include "validate.h"
48 #include "gc-internal.h"
49 #include "runtime-options.h"
51 #include <errno.h>
53 #ifdef LISP_FEATURE_SB_CORE_COMPRESSION
54 # include <zlib.h>
55 #endif
57 unsigned char build_id[] =
58 #include "../../output/build-id.tmp"
61 int
62 open_binary(char *filename, int mode)
64 #ifdef LISP_FEATURE_WIN32
65 mode |= O_BINARY;
66 #endif
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)) {
80 return NULL;
83 if ((RUNTIME_OPTIONS_MAGIC != optarray[0]) || (0 == optarray[1])) {
84 return NULL;
87 options = successful_malloc(sizeof(struct runtime_options));
89 options->dynamic_space_size = optarray[2];
90 options->thread_control_stack_size = optarray[3];
92 return options;
95 void
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. */
119 os_vm_offset_t
120 search_for_embedded_core(char *filename)
122 lispobj header;
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;
126 int fd = -1;
128 if ((fd = open_binary(filename, O_RDONLY)) < 0)
129 goto lose;
131 if (read(fd, &header, (size_t)lispobj_size) < lispobj_size)
132 goto lose;
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. */
137 return 0;
140 if (lseek(fd, -lispobj_size, SEEK_END) < 0)
141 goto lose;
142 if (read(fd, &header, (size_t)lispobj_size) < lispobj_size)
143 goto lose;
145 if (header == CORE_MAGIC) {
146 if (lseek(fd, -trailer_size, SEEK_END) < 0)
147 goto lose;
148 if (read(fd, &core_start, sizeof(os_vm_offset_t)) < 0)
149 goto lose;
151 if (lseek(fd, core_start, SEEK_SET) < 0)
152 goto lose;
153 pos = lseek(fd, 0, SEEK_CUR);
155 if (read(fd, &header, (size_t)lispobj_size) < lispobj_size)
156 goto lose;
158 if (header != CORE_MAGIC)
159 goto lose;
161 maybe_initialize_runtime_options(fd);
163 close(fd);
164 return pos;
167 lose:
168 if (fd != -1)
169 close(fd);
171 return -1;
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 os_vm_address_t copy_core_bytes(int fd, os_vm_offset_t offset,
179 os_vm_address_t addr, int len)
181 unsigned char buf[4096];
182 int c,x;
183 int old_fd = lseek(fd, 0, SEEK_CUR);
185 if(len & (4096-1)){
186 fprintf(stderr, "cant copy a slice of core because slice-length is not of page size(4096)\n");
187 exit(-1);
189 if(old_fd < 0){
190 fprintf(stderr, "cant perform lseek() on corefile\n");
192 lseek(fd, offset, SEEK_SET);
193 if(fd < 0){
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);
198 if(c != 4096){
199 fprintf(stderr, "cant read memory area from corefile at position %lu, got %d\n", offset + x, c);
200 exit(-1);
202 memcpy(addr+x, buf, 4096);
204 os_flush_icache(addr, len);
205 return addr;
207 #endif
209 #ifdef LISP_FEATURE_SB_CORE_COMPRESSION
210 # define ZLIB_BUFFER_SIZE (1u<<16)
211 os_vm_address_t inflate_core_bytes(int fd, os_vm_offset_t offset,
212 os_vm_address_t addr, int len)
214 z_stream stream;
215 unsigned char buf[ZLIB_BUFFER_SIZE];
216 int ret;
218 # ifdef LISP_FEATURE_WIN32
219 /* Ensure the memory is committed so zlib doesn't segfault trying to
220 inflate. */
221 os_validate_recommit(addr, len);
222 # endif
224 if (-1 == lseek(fd, offset, SEEK_SET)) {
225 lose("Unable to lseek() on corefile\n");
228 stream.zalloc = NULL;
229 stream.zfree = NULL;
230 stream.opaque = NULL;
231 stream.avail_in = 0;
232 stream.next_in = buf;
234 ret = inflateInit(&stream);
235 if (ret != Z_OK)
236 lose("zlib error %i\n", ret);
238 stream.next_out = (void*)addr;
239 stream.avail_out = len;
240 do {
241 ssize_t count = read(fd, buf, sizeof(buf));
242 if (count < 0)
243 lose("unable to read core file (errno = %i)\n", errno);
244 stream.next_in = buf;
245 stream.avail_in = count;
246 if (count == 0) break;
247 ret = inflate(&stream, Z_NO_FLUSH);
248 switch (ret) {
249 case Z_STREAM_END:
250 break;
251 case Z_OK:
252 if (stream.avail_out == 0)
253 lose("Runaway gzipped core directory... aborting\n");
254 if (stream.avail_in > 0)
255 lose("zlib inflate returned without fully"
256 "using up input buffer... aborting\n");
257 break;
258 default:
259 lose("zlib inflate error: %i\n", ret);
260 break;
262 } while (ret != Z_STREAM_END);
264 if (stream.avail_out > 0) {
265 if (stream.avail_out >= os_vm_page_size)
266 fprintf(stderr, "Warning: gzipped core directory significantly"
267 "shorter than expected (%lu bytes)", (unsigned long)stream.avail_out);
268 /* Is this needed? */
269 memset(stream.next_out, 0, stream.avail_out);
272 inflateEnd(&stream);
273 return addr;
275 # undef ZLIB_BUFFER_SIZE
276 #endif
278 int merge_core_pages = -1;
280 #ifdef LISP_FEATURE_LINUX
281 os_vm_address_t anon_dynamic_space_start;
282 #endif
284 static void
285 process_directory(int fd, lispobj *ptr, int count, os_vm_offset_t file_offset)
287 extern void immobile_space_coreparse(uword_t,uword_t);
288 struct ndir_entry *entry;
289 int compressed;
291 FSHOW((stderr, "/process_directory(..), count=%d\n", count));
293 for (entry = (struct ndir_entry *) ptr; --count>= 0; ++entry) {
295 compressed = 0;
296 sword_t id = entry->identifier;
297 if (id <= (MAX_CORE_SPACE_ID | DEFLATED_CORE_SPACE_ID_FLAG)) {
298 if (id & DEFLATED_CORE_SPACE_ID_FLAG)
299 compressed = 1;
300 id &= ~(DEFLATED_CORE_SPACE_ID_FLAG);
302 sword_t offset = os_vm_page_size * (1 + entry->data_page);
303 os_vm_address_t addr =
304 (os_vm_address_t) (os_vm_page_size * entry->address);
305 lispobj *free_pointer = (lispobj *) addr + entry->nwords;
306 uword_t len = os_vm_page_size * entry->page_count;
307 if (len != 0) {
308 os_vm_address_t real_addr;
309 FSHOW((stderr, "/mapping %ld(0x%lx) bytes at 0x%lx\n",
310 len, len, (uword_t)addr));
311 if (compressed) {
312 #ifdef LISP_FEATURE_SB_CORE_COMPRESSION
313 real_addr = inflate_core_bytes(fd, offset + file_offset, addr, len);
314 #else
315 lose("This runtime was not built with zlib-compressed core support... aborting\n");
316 #endif
317 } else {
318 #ifdef LISP_FEATURE_HPUX
319 real_addr = copy_core_bytes(fd, offset + file_offset, addr, len);
320 #else
321 real_addr = os_map(fd, offset + file_offset, addr, len);
322 #endif
324 if (real_addr != addr) {
325 lose("file mapped in wrong place! "
326 "(0x%08x != 0x%08lx)\n",
327 real_addr,
328 addr);
332 #ifdef MADV_MERGEABLE
333 if ((merge_core_pages == 1)
334 || ((merge_core_pages == -1) && compressed)) {
335 madvise(addr, len, MADV_MERGEABLE);
337 #endif
338 FSHOW((stderr, "/space id = %ld, free pointer = %p\n",
339 id, (uword_t)free_pointer));
341 switch (id) {
342 case DYNAMIC_CORE_SPACE_ID:
343 if (len > dynamic_space_size) {
344 fprintf(stderr,
345 "dynamic space too small for core: %luKiB required, %luKiB available.\n",
346 (unsigned long)len >> 10,
347 (unsigned long)dynamic_space_size >> 10);
348 exit(1);
350 #ifdef LISP_FEATURE_GENCGC
351 if (addr != (os_vm_address_t)DYNAMIC_SPACE_START) {
352 fprintf(stderr, "in core: %p; in runtime: %p \n",
353 (void*)addr, (void*)DYNAMIC_SPACE_START);
354 lose("core/runtime address mismatch: DYNAMIC_SPACE_START\n");
356 #else
357 if ((addr != (os_vm_address_t)DYNAMIC_0_SPACE_START) &&
358 (addr != (os_vm_address_t)DYNAMIC_1_SPACE_START)) {
359 fprintf(stderr, "in core: %p; in runtime: %p or %p\n",
360 (void*)addr,
361 (void*)DYNAMIC_0_SPACE_START,
362 (void*)DYNAMIC_1_SPACE_START);
363 lose("warning: core/runtime address mismatch: DYNAMIC_SPACE_START\n");
365 #endif
366 #if defined(ALLOCATION_POINTER)
367 SetSymbolValue(ALLOCATION_POINTER, (lispobj)free_pointer,0);
368 #else
369 dynamic_space_free_pointer = free_pointer;
370 #endif
371 /* For stop-and-copy GC, this will be whatever the GC was
372 * using at the time. With GENCGC, this will always be
373 * space 0. (We checked above that for GENCGC,
374 * addr==DYNAMIC_SPACE_START.) */
375 current_dynamic_space = (lispobj *)addr;
376 #ifdef LISP_FEATURE_LINUX
377 anon_dynamic_space_start = addr + len;
378 // This assertion is here because of the test in zero_pages_with_mmap()
379 // which trusts that if addr > anon_dynamic_space_start
380 // then addr did not come from any file mapping.
381 gc_assert((lispobj)anon_dynamic_space_start > STATIC_SPACE_END);
382 #endif
383 break;
384 case STATIC_CORE_SPACE_ID:
385 if (addr != (os_vm_address_t)STATIC_SPACE_START) {
386 fprintf(stderr, "in core: %p - in runtime: %p\n",
387 (void*)addr, (void*)STATIC_SPACE_START);
388 lose("core/runtime address mismatch: STATIC_SPACE_START\n");
390 break;
391 case READ_ONLY_CORE_SPACE_ID:
392 if (addr != (os_vm_address_t)READ_ONLY_SPACE_START) {
393 fprintf(stderr, "in core: %p - in runtime: %p\n",
394 (void*)addr, (void*)READ_ONLY_SPACE_START);
395 lose("core/runtime address mismatch: READ_ONLY_SPACE_START\n");
397 break;
398 #ifdef LISP_FEATURE_IMMOBILE_SPACE
399 // Immobile space is subdivided into fixed-size and variable-size.
400 // There is no margin between the two, though for efficiency
401 // they are written separately to eliminate waste in the core file.
402 case IMMOBILE_FIXEDOBJ_CORE_SPACE_ID:
403 if (addr != (os_vm_address_t)IMMOBILE_SPACE_START) {
404 fprintf(stderr, "in core: %p - in runtime: %p\n",
405 (void*)addr, (void*)IMMOBILE_SPACE_START);
406 lose("core/runtime address mismatch: IMMOBILE_SPACE_START\n");
408 immobile_space_coreparse(IMMOBILE_SPACE_START, len);
409 break;
410 case IMMOBILE_VARYOBJ_CORE_SPACE_ID:
411 if (addr != (os_vm_address_t)IMMOBILE_VARYOBJ_SUBSPACE_START) {
412 fprintf(stderr, "in core: %p - in runtime: %p\n",
413 (void*)addr, (void*)IMMOBILE_VARYOBJ_SUBSPACE_START);
414 lose("core/runtime address mismatch: IMMOBILE_VARYOBJ_SUBSPACE_START\n");
416 immobile_space_coreparse(IMMOBILE_VARYOBJ_SUBSPACE_START, len);
417 break;
418 #endif
419 default:
420 lose("unknown space ID %ld addr %p\n", id, addr);
425 lispobj
426 load_core_file(char *file, os_vm_offset_t file_offset)
428 void *header;
429 #ifndef LISP_FEATURE_ALPHA
430 word_t val, *ptr;
431 #else
432 u32 val, *ptr;
433 #endif
434 os_vm_size_t len, remaining_len;
435 int fd = open_binary(file, O_RDONLY);
436 ssize_t count;
437 lispobj initial_function = NIL;
439 FSHOW((stderr, "/entering load_core_file(%s)\n", file));
440 if (fd < 0) {
441 fprintf(stderr, "could not open file \"%s\"\n", file);
442 perror("open");
443 exit(1);
446 lseek(fd, file_offset, SEEK_SET);
447 header = calloc(os_vm_page_size, 1);
449 count = read(fd, header, os_vm_page_size);
450 if (count < (ssize_t) os_vm_page_size) {
451 lose("premature end of core file\n");
453 SHOW("successfully read first page of core");
455 ptr = header;
456 val = *ptr++;
458 if (val != CORE_MAGIC) {
459 lose("invalid magic number in core: 0x%lx should have been 0x%x.\n",
460 val,
461 CORE_MAGIC);
463 SHOW("found CORE_MAGIC");
465 while (val != END_CORE_ENTRY_TYPE_CODE) {
466 val = *ptr++;
467 len = *ptr++;
468 remaining_len = len - 2; /* (-2 to cancel the two ++ operations) */
469 FSHOW((stderr, "/val=0x%"WORD_FMTX", remaining_len=0x%"WORD_FMTX"\n",
470 val, remaining_len));
472 switch (val) {
474 case END_CORE_ENTRY_TYPE_CODE:
475 SHOW("END_CORE_ENTRY_TYPE_CODE case");
476 break;
478 case BUILD_ID_CORE_ENTRY_TYPE_CODE:
479 SHOW("BUILD_ID_CORE_ENTRY_TYPE_CODE case");
481 os_vm_size_t i;
483 FSHOW((stderr, "build_id[]=\"%s\"\n", build_id));
484 FSHOW((stderr, "remaining_len = %d\n", remaining_len));
485 if (remaining_len != strlen((const char *)build_id))
486 goto losing_build_id;
487 for (i = 0; i < remaining_len; ++i) {
488 FSHOW((stderr, "ptr[%d] = char = %d, expected=%d\n",
489 i, ptr[i], build_id[i]));
490 if (ptr[i] != build_id[i])
491 goto losing_build_id;
493 break;
494 losing_build_id:
495 /* .core files are not binary-compatible between
496 * builds because we can't easily detect whether the
497 * sources were patched between the time the
498 * dumping-the-.core runtime was built and the time
499 * that the loading-the-.core runtime was built.
501 * (We could easily detect whether version.lisp-expr
502 * was changed, but people experimenting with patches
503 * don't necessarily update version.lisp-expr.) */
505 lose("can't load .core for different runtime, sorry\n");
508 case NEW_DIRECTORY_CORE_ENTRY_TYPE_CODE:
509 SHOW("NEW_DIRECTORY_CORE_ENTRY_TYPE_CODE case");
510 process_directory(fd,
511 ptr,
512 #ifndef LISP_FEATURE_ALPHA
513 remaining_len / (sizeof(struct ndir_entry) /
514 sizeof(lispobj)),
515 #else
516 remaining_len / (sizeof(struct ndir_entry) /
517 sizeof(u32)),
518 #endif
519 file_offset);
520 break;
522 case INITIAL_FUN_CORE_ENTRY_TYPE_CODE:
523 SHOW("INITIAL_FUN_CORE_ENTRY_TYPE_CODE case");
524 initial_function = (lispobj)*ptr;
525 break;
527 #ifdef LISP_FEATURE_GENCGC
528 case PAGE_TABLE_CORE_ENTRY_TYPE_CODE:
530 os_vm_size_t size = *ptr;
531 os_vm_size_t fdoffset = (*(ptr+1) + 1) * (os_vm_page_size);
532 page_index_t offset = 0;
533 ssize_t bytes_read;
534 word_t data[4096];
535 word_t word;
536 lseek(fd, fdoffset + file_offset, SEEK_SET);
537 while ((bytes_read = read(fd, data, (size < 4096 ? size : 4096 )))
538 > 0)
540 int i = 0;
541 size -= bytes_read;
542 while (bytes_read) {
543 bytes_read -= sizeof(word_t);
544 /* Ignore all zeroes. The size of the page table
545 * core entry was rounded up to os_vm_page_size
546 * during the save, and might now have more
547 * elements than the page table.
549 * The low bits of each word are allocation flags.
551 if ((word=data[i])) {
552 page_table[offset].scan_start_offset = word & ~0x03;
553 page_table[offset].allocated = word & 0x03;
555 i++;
556 offset++;
560 gencgc_partial_pickup = 1;
561 break;
563 #endif
564 default:
565 lose("unknown core file entry: 0x%"WORD_FMTX"\n", val);
568 ptr += remaining_len;
569 FSHOW((stderr, "/new ptr=0x%"WORD_FMTX"\n", ptr));
571 SHOW("about to free(header)");
572 free(header);
573 close(fd);
574 SHOW("returning from load_core_file(..)");
575 return initial_function;
578 #include "genesis/hash-table.h"
579 #include "genesis/vector.h"
580 os_vm_address_t get_asm_routine_by_name(const char* name)
582 lispobj routines = SYMBOL(ASSEMBLER_ROUTINES)->value;
583 if (lowtag_of(routines) == INSTANCE_POINTER_LOWTAG) {
584 struct hash_table* ht = (struct hash_table*)native_pointer(routines);
585 struct vector* table = (struct vector*)native_pointer(ht->table);
586 lispobj sym;
587 int i;
588 for (i=2 ; i < fixnum_value(table->length) ; i += 2) {
589 sym = table->data[i];
590 if (lowtag_of(sym) == OTHER_POINTER_LOWTAG
591 && widetag_of(SYMBOL(sym)->header) == SYMBOL_HEADER_WIDETAG
592 && !strcmp(name,
593 (char*)((struct vector*)
594 native_pointer(SYMBOL(sym)->name))->data))
595 return (os_vm_address_t)fixnum_value(table->data[i+1]);
597 // Something is wrong if we have a hashtable but find nothing.
598 fprintf(stderr, "WARNING: get_asm_routine_by_name(%s) failed\n",
599 name);
601 return NULL;
604 void asm_routine_poke(const char* routine, int offset, char byte)
606 char *address = (char *)get_asm_routine_by_name(routine);
607 if (address)
608 address[offset] = byte;