2 * This software is part of the SBCL system. See the README file for
5 * This software is derived from the CMU CL system, which was
6 * written at Carnegie Mellon University and released into the
7 * public domain. The software is in the public domain and is
8 * provided with absolutely no warranty. See the COPYING and CREDITS
9 * files for more information.
12 #ifndef LISP_FEATURE_WIN32
13 #include <sys/types.h>
22 #if defined(LISP_FEATURE_WIN32) && defined(LISP_FEATURE_SB_THREAD)
23 #include "pthreads_win32.h"
35 #include "gc-internal.h"
39 #include "genesis/static-symbols.h"
40 #include "genesis/symbol.h"
42 #ifdef LISP_FEATURE_SB_CORE_COMPRESSION
46 /* write_runtime_options uses a simple serialization scheme that
47 * consists of one word of magic, one word indicating whether options
48 * are actually saved, and one word per struct field. */
50 write_runtime_options(FILE *file
, struct runtime_options
*options
)
52 size_t optarray
[RUNTIME_OPTIONS_WORDS
];
54 memset(&optarray
, 0, sizeof(optarray
));
55 optarray
[0] = RUNTIME_OPTIONS_MAGIC
;
57 if (options
!= NULL
) {
58 /* optarray[1] is a flag indicating that options are present */
60 optarray
[2] = options
->dynamic_space_size
;
61 optarray
[3] = options
->thread_control_stack_size
;
64 if (RUNTIME_OPTIONS_WORDS
!=
65 fwrite(optarray
, sizeof(size_t), RUNTIME_OPTIONS_WORDS
, file
)) {
66 perror("Error writing runtime options to file");
71 write_lispobj(lispobj obj
, FILE *file
)
73 if (1 != fwrite(&obj
, sizeof(lispobj
), 1, file
)) {
74 perror("Error writing to file");
79 write_bytes_to_file(FILE * file
, char *addr
, long bytes
, int compression
)
81 if (compression
== COMPRESSION_LEVEL_NONE
) {
83 sword_t count
= fwrite(addr
, 1, bytes
, file
);
89 perror("error writing to core file");
90 lose("core file is incomplete or corrupt\n");
93 #ifdef LISP_FEATURE_SB_CORE_COMPRESSION
94 } else if ((compression
>= -1) && (compression
<= 9)) {
95 # define ZLIB_BUFFER_SIZE (1u<<16)
97 unsigned char* buf
= successful_malloc(ZLIB_BUFFER_SIZE
);
98 unsigned char * written
, * end
;
99 long total_written
= 0;
101 stream
.zalloc
= NULL
;
103 stream
.opaque
= NULL
;
104 stream
.avail_in
= bytes
;
105 stream
.next_in
= (void*)addr
;
106 ret
= deflateInit(&stream
, compression
);
108 lose("deflateInit: %i\n", ret
);
110 stream
.avail_out
= ZLIB_BUFFER_SIZE
;
111 stream
.next_out
= buf
;
112 ret
= deflate(&stream
, Z_FINISH
);
113 if (ret
< 0) lose("zlib deflate error: %i... exiting\n", ret
);
115 end
= buf
+ZLIB_BUFFER_SIZE
-stream
.avail_out
;
116 total_written
+= end
- written
;
117 while (written
< end
) {
118 long count
= fwrite(written
, 1, end
-written
, file
);
122 perror("error writing to core file");
123 lose("core file is incomplete or corrupt\n");
126 } while (stream
.avail_out
== 0);
129 printf("compressed %lu bytes into %lu at level %i\n",
130 bytes
, total_written
, compression
);
131 # undef ZLIB_BUFFER_SIZE
134 #ifdef LISP_FEATURE_SB_CORE_COMPRESSION
135 lose("Unknown core compression level %i, exiting\n", compression
);
137 lose("zlib-compressed core support not built in this runtime\n");
141 if (fflush(file
) != 0) {
142 perror("error writing to core file");
143 lose("core file is incomplete or corrupt\n");
149 write_and_compress_bytes(FILE *file
, char *addr
, long bytes
, os_vm_offset_t file_offset
,
154 bytes
= (bytes
+os_vm_page_size
-1)&~(os_vm_page_size
-1);
156 #ifdef LISP_FEATURE_WIN32
158 /* touch every single page in the space to force it to be mapped. */
159 for (count
= 0; count
< bytes
; count
+= 0x1000) {
160 volatile int temp
= addr
[count
];
166 fseek(file
, 0, SEEK_END
);
167 data
= (ftell(file
)+os_vm_page_size
-1)&~(os_vm_page_size
-1);
168 fseek(file
, data
, SEEK_SET
);
169 write_bytes_to_file(file
, addr
, bytes
, compression
);
170 fseek(file
, here
, SEEK_SET
);
171 return ((data
- file_offset
) / os_vm_page_size
) - 1;
174 static long __attribute__((__unused__
))
175 write_bytes(FILE *file
, char *addr
, long bytes
, os_vm_offset_t file_offset
)
177 return write_and_compress_bytes(file
, addr
, bytes
, file_offset
,
178 COMPRESSION_LEVEL_NONE
);
181 extern struct lisp_startup_options lisp_startup_options
;
184 output_space(FILE *file
, int id
, lispobj
*addr
, lispobj
*end
,
185 os_vm_offset_t file_offset
,
186 int core_compression_level
)
188 size_t words
, bytes
, data
, compressed_flag
;
189 static char *names
[] = {NULL
, "dynamic", "static", "read-only",
190 "immobile", "immobile"};
193 = ((core_compression_level
!= COMPRESSION_LEVEL_NONE
)
194 ? DEFLATED_CORE_SPACE_ID_FLAG
: 0);
196 write_lispobj(id
| compressed_flag
, file
);
198 write_lispobj(words
, file
);
200 bytes
= words
* sizeof(lispobj
);
202 if (!lisp_startup_options
.noinform
)
203 printf("writing %lu bytes from the %s space at %p\n",
204 (long unsigned)bytes
, names
[id
], addr
);
206 data
= write_and_compress_bytes(file
, (char *)addr
, bytes
, file_offset
,
207 core_compression_level
);
209 write_lispobj(data
, file
);
210 write_lispobj((uword_t
)addr
/ os_vm_page_size
, file
);
211 write_lispobj((bytes
+ os_vm_page_size
- 1) / os_vm_page_size
, file
);
215 open_core_for_saving(char *filename
)
217 /* Open the output file. We don't actually need the file yet, but
218 * the fopen() might fail for some reason, and we want to detect
219 * that and back out before we do anything irreversible. */
221 return fopen(filename
, "wb");
224 #ifdef LISP_FEATURE_IMMOBILE_SPACE
225 extern void prepare_immobile_space_for_save();
226 # define N_SPACES_TO_SAVE 5
227 # ifdef LISP_FEATURE_IMMOBILE_CODE
228 lispobj code_component_order
;
229 extern void defrag_immobile_space(lispobj
,boolean
);
232 # define N_SPACES_TO_SAVE 3
235 save_to_filehandle(FILE *file
, char *filename
, lispobj init_function
,
236 boolean make_executable
,
237 boolean save_runtime_options
,
238 int core_compression_level
)
241 os_vm_offset_t core_start_pos
;
242 boolean verbose
= !lisp_startup_options
.noinform
;
244 #ifdef LISP_FEATURE_X86_64
245 untune_asm_routines_for_microarch();
248 /* Smash the enclosing state. (Once we do this, there's no good
249 * way to go back, which is a sufficient reason that this ends up
250 * being SAVE-LISP-AND-DIE instead of SAVE-LISP-AND-GO-ON). */
252 printf("[undoing binding stack and other enclosing state... ");
255 for_each_thread(th
) { /* XXX really? */
256 unbind_to_here((lispobj
*)th
->binding_stack_start
,th
);
257 SetSymbolValue(CURRENT_CATCH_BLOCK
, 0,th
);
258 SetSymbolValue(CURRENT_UNWIND_PROTECT_BLOCK
, 0,th
);
260 if (verbose
) printf("done]\n");
261 #ifdef LISP_FEATURE_IMMOBILE_CODE
262 // It's better to wait to defrag until after the binding stack is undone,
263 // because we explicitly don't fixup code refs from stacks.
264 // i.e. if there *were* something on the binding stack that cared that code
265 // moved, it would be wrong. This way we can be sure we don't care.
266 if (code_component_order
) {
267 // Assert that defrag will not move the init_function
268 gc_assert(!immobile_space_p(init_function
));
270 printf("[defragmenting immobile space... ");
273 defrag_immobile_space(code_component_order
, verbose
);
274 if (verbose
) printf("done]\n");
278 /* (Now we can actually start copying ourselves into the output file.) */
281 printf("[saving current Lisp image into %s:\n", filename
);
285 core_start_pos
= ftell(file
);
286 write_lispobj(CORE_MAGIC
, file
);
288 write_lispobj(BUILD_ID_CORE_ENTRY_TYPE_CODE
, file
);
289 write_lispobj(/* (We're writing the word count of the entry here, and the 2
290 * term is one word for the leading BUILD_ID_CORE_ENTRY_TYPE_CODE
291 * word and one word where we store the count itself.) */
292 2 + strlen((const char *)build_id
),
296 for (p
= (unsigned char *)build_id
; *p
; ++p
)
297 write_lispobj(*p
, file
);
300 write_lispobj(NEW_DIRECTORY_CORE_ENTRY_TYPE_CODE
, file
);
301 write_lispobj(/* (word count = N spaces described by 5 words each, plus the
302 * entry type code, plus this count itself) */
303 (5*N_SPACES_TO_SAVE
)+2, file
);
305 READ_ONLY_CORE_SPACE_ID
,
306 (lispobj
*)READ_ONLY_SPACE_START
,
307 read_only_space_free_pointer
,
309 core_compression_level
);
311 STATIC_CORE_SPACE_ID
,
312 (lispobj
*)STATIC_SPACE_START
,
313 static_space_free_pointer
,
315 core_compression_level
);
316 #ifdef LISP_FEATURE_GENCGC
317 /* Flush the current_region, updating the tables. */
318 gc_alloc_update_all_page_tables(1);
319 update_dynamic_space_free_pointer();
321 #ifdef LISP_FEATURE_IMMOBILE_SPACE
322 prepare_immobile_space_for_save();
324 IMMOBILE_FIXEDOBJ_CORE_SPACE_ID
,
325 (lispobj
*)IMMOBILE_SPACE_START
,
326 immobile_fixedobj_free_pointer
,
328 core_compression_level
);
330 IMMOBILE_VARYOBJ_CORE_SPACE_ID
,
331 (lispobj
*)IMMOBILE_VARYOBJ_SUBSPACE_START
,
332 immobile_space_free_pointer
,
334 core_compression_level
);
337 #ifdef LISP_FEATURE_GENCGC
339 DYNAMIC_CORE_SPACE_ID
,
340 (lispobj
*)DYNAMIC_SPACE_START
,
341 dynamic_space_free_pointer
,
343 core_compression_level
);
346 DYNAMIC_CORE_SPACE_ID
,
347 (lispobj
*)current_dynamic_space
,
348 dynamic_space_free_pointer
,
350 core_compression_level
);
354 DYNAMIC_CORE_SPACE_ID
,
355 (lispobj
*)DYNAMIC_SPACE_START
,
356 (lispobj
*)SymbolValue(ALLOCATION_POINTER
,0),
358 core_compression_level
);
361 write_lispobj(INITIAL_FUN_CORE_ENTRY_TYPE_CODE
, file
);
362 write_lispobj(3, file
);
363 write_lispobj(init_function
, file
);
365 #ifdef LISP_FEATURE_GENCGC
367 size_t true_size
= sizeof last_free_page
368 + (last_free_page
* sizeof(struct corefile_pte
));
369 size_t rounded_size
= CEILING(true_size
, os_vm_page_size
);
370 char* data
= successful_malloc(rounded_size
);
371 *(page_index_t
*)data
= last_free_page
;
372 struct corefile_pte
*ptes
= (struct corefile_pte
*)(data
+ sizeof(page_index_t
));
374 for (i
= 0; i
< last_free_page
; i
++) {
375 /* Thanks to alignment requirements, the two low bits
376 * are always zero, so we can use them to store the
377 * allocation type -- region is always closed, so only
378 * the two low bits of allocation flags matter. */
379 uword_t word
= page_scan_start_offset(i
);
380 gc_assert((word
& 0x03) == 0);
381 ptes
[i
].sso
= word
| (0x03 & page_table
[i
].allocated
);
382 ptes
[i
].bytes_used
= page_bytes_used(i
);
384 write_lispobj(PAGE_TABLE_CORE_ENTRY_TYPE_CODE
, file
);
385 write_lispobj(4, file
);
386 write_lispobj(rounded_size
, file
);
387 sword_t offset
= write_bytes(file
, data
, rounded_size
, core_start_pos
);
388 write_lispobj(offset
, file
);
392 write_lispobj(END_CORE_ENTRY_TYPE_CODE
, file
);
394 /* Write a trailing header, ignored when parsing the core normally.
395 * This is used to locate the start of the core when the runtime is
396 * prepended to it. */
397 fseek(file
, 0, SEEK_END
);
399 /* If NULL runtime options are passed to write_runtime_options,
400 * command-line processing is performed as normal in the SBCL
401 * executable. Otherwise, the saved runtime options are used and
402 * all command-line arguments are available to Lisp in
403 * SB-EXT:*POSIX-ARGV*. */
404 write_runtime_options(file
,
405 (save_runtime_options
? runtime_options
: NULL
));
407 if (1 != fwrite(&core_start_pos
, sizeof(os_vm_offset_t
), 1, file
)) {
408 perror("Error writing core starting position to file");
411 write_lispobj(CORE_MAGIC
, file
);
415 #ifndef LISP_FEATURE_WIN32
417 chmod (filename
, 0755);
420 if (verbose
) printf("done]\n");
423 #undef N_SPACES_TO_SAVE
425 /* Check if the build_id for the current runtime is present in a
428 check_runtime_build_id(void *buf
, size_t size
)
433 idlen
= strlen((const char*)build_id
) - 1;
434 while ((pos
= memchr(buf
, build_id
[0], size
)) != NULL
) {
435 size
-= (pos
+ 1) - (char *)buf
;
437 if (idlen
<= size
&& memcmp(buf
, build_id
+ 1, idlen
) == 0)
444 /* Slurp the executable portion of the runtime into a malloced buffer
445 * and return it. Places the size in bytes of the runtime into
446 * 'size_out'. Returns NULL if the runtime cannot be loaded from
449 load_runtime(char *runtime_path
, size_t *size_out
)
454 os_vm_offset_t core_offset
;
456 core_offset
= search_for_embedded_core (runtime_path
);
457 if ((input
= fopen(runtime_path
, "rb")) == NULL
) {
458 fprintf(stderr
, "Unable to open runtime: %s\n", runtime_path
);
462 fseek(input
, 0, SEEK_END
);
463 size
= (size_t) ftell(input
);
464 fseek(input
, 0, SEEK_SET
);
466 if (core_offset
!= -1 && size
> (size_t) core_offset
)
469 buf
= successful_malloc(size
);
470 if ((count
= fread(buf
, 1, size
, input
)) != size
) {
471 fprintf(stderr
, "Premature EOF while reading runtime.\n");
475 if (!check_runtime_build_id(buf
, size
)) {
476 fprintf(stderr
, "Failed to locate current build_id in runtime: %s\n",
494 save_runtime_to_filehandle(FILE *output
, void *runtime
, size_t runtime_size
,
495 int application_type
)
500 #ifdef LISP_FEATURE_WIN32
502 PIMAGE_DOS_HEADER dos_header
= (PIMAGE_DOS_HEADER
)runtime
;
503 PIMAGE_NT_HEADERS nt_header
= (PIMAGE_NT_HEADERS
)((char *)dos_header
+
504 dos_header
->e_lfanew
);
507 switch (application_type
) {
509 sub_system
= IMAGE_SUBSYSTEM_WINDOWS_CUI
;
512 sub_system
= IMAGE_SUBSYSTEM_WINDOWS_GUI
;
515 fprintf(stderr
, "Invalid application type %d\n", application_type
);
519 nt_header
->OptionalHeader
.Subsystem
= sub_system
;
523 if (runtime_size
!= fwrite(runtime
, 1, runtime_size
, output
)) {
524 perror("Error saving runtime");
528 padding
= (os_vm_page_size
- (runtime_size
% os_vm_page_size
)) & ~os_vm_page_size
;
530 padbytes
= successful_malloc(padding
);
531 memset(padbytes
, 0, padding
);
532 if (padding
!= fwrite(padbytes
, 1, padding
, output
)) {
533 perror("Error saving runtime");
544 prepare_to_save(char *filename
, boolean prepend_runtime
, void **runtime_bytes
,
545 size_t *runtime_size
)
550 if (prepend_runtime
) {
551 runtime_path
= os_get_runtime_executable_path(0);
553 if (runtime_path
== NULL
&& saved_runtime_path
== NULL
) {
554 fprintf(stderr
, "Unable to get default runtime path.\n");
558 if (runtime_path
== NULL
)
559 *runtime_bytes
= load_runtime(saved_runtime_path
, runtime_size
);
561 *runtime_bytes
= load_runtime(runtime_path
, runtime_size
);
565 if (*runtime_bytes
== NULL
)
569 file
= open_core_for_saving(filename
);
571 free(*runtime_bytes
);
579 #ifdef LISP_FEATURE_CHENEYGC
581 save(char *filename
, lispobj init_function
, boolean prepend_runtime
,
582 boolean save_runtime_options
, boolean compressed
, int compression_level
,
583 int application_type
)
586 void *runtime_bytes
= NULL
;
589 file
= prepare_to_save(filename
, prepend_runtime
, &runtime_bytes
, &runtime_size
);
594 save_runtime_to_filehandle(file
, runtime_bytes
, runtime_size
, application_type
);
596 return save_to_filehandle(file
, filename
, init_function
, prepend_runtime
,
597 save_runtime_options
,
598 compressed
? compressed
: COMPRESSION_LEVEL_NONE
);