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"
38 #include "pseudo-atomic.h"
40 #include "genesis/static-symbols.h"
41 #include "genesis/symbol.h"
43 #ifdef LISP_FEATURE_SB_CORE_COMPRESSION
47 /* write_runtime_options uses a simple serialization scheme that
48 * consists of one word of magic, one word indicating whether options
49 * are actually saved, and one word per struct field. */
51 write_runtime_options(FILE *file
, struct runtime_options
*options
)
53 size_t optarray
[RUNTIME_OPTIONS_WORDS
];
55 memset(&optarray
, 0, sizeof(optarray
));
56 optarray
[0] = RUNTIME_OPTIONS_MAGIC
;
58 if (options
!= NULL
) {
59 /* optarray[1] is a flag indicating that options are present */
61 optarray
[2] = options
->dynamic_space_size
;
62 optarray
[3] = options
->thread_control_stack_size
;
65 if (RUNTIME_OPTIONS_WORDS
!=
66 fwrite(optarray
, sizeof(size_t), RUNTIME_OPTIONS_WORDS
, file
)) {
67 perror("Error writing runtime options to file");
72 write_lispobj(lispobj obj
, FILE *file
)
74 if (1 != fwrite(&obj
, sizeof(lispobj
), 1, file
)) {
75 perror("Error writing to file");
80 write_bytes_to_file(FILE * file
, char *addr
, long bytes
, int compression
)
82 if (compression
== COMPRESSION_LEVEL_NONE
) {
84 sword_t count
= fwrite(addr
, 1, bytes
, file
);
90 perror("error writing to core file");
91 lose("core file is incomplete or corrupt\n");
94 #ifdef LISP_FEATURE_SB_CORE_COMPRESSION
95 } else if ((compression
>= -1) && (compression
<= 9)) {
96 # define ZLIB_BUFFER_SIZE (1u<<16)
98 unsigned char* buf
= successful_malloc(ZLIB_BUFFER_SIZE
);
99 unsigned char * written
, * end
;
100 long total_written
= 0;
102 stream
.zalloc
= NULL
;
104 stream
.opaque
= NULL
;
105 stream
.avail_in
= bytes
;
106 stream
.next_in
= (void*)addr
;
107 ret
= deflateInit(&stream
, compression
);
109 lose("deflateInit: %i\n", ret
);
111 stream
.avail_out
= ZLIB_BUFFER_SIZE
;
112 stream
.next_out
= buf
;
113 ret
= deflate(&stream
, Z_FINISH
);
114 if (ret
< 0) lose("zlib deflate error: %i... exiting\n", ret
);
116 end
= buf
+ZLIB_BUFFER_SIZE
-stream
.avail_out
;
117 total_written
+= end
- written
;
118 while (written
< end
) {
119 long count
= fwrite(written
, 1, end
-written
, file
);
123 perror("error writing to core file");
124 lose("core file is incomplete or corrupt\n");
127 } while (stream
.avail_out
== 0);
130 printf("compressed %lu bytes into %lu at level %i\n",
131 bytes
, total_written
, compression
);
132 # undef ZLIB_BUFFER_SIZE
135 #ifdef LISP_FEATURE_SB_CORE_COMPRESSION
136 lose("Unknown core compression level %i, exiting\n", compression
);
138 lose("zlib-compressed core support not built in this runtime\n");
142 if (fflush(file
) != 0) {
143 perror("error writing to core file");
144 lose("core file is incomplete or corrupt\n");
150 write_and_compress_bytes(FILE *file
, char *addr
, long bytes
, os_vm_offset_t file_offset
,
155 bytes
= (bytes
+os_vm_page_size
-1)&~(os_vm_page_size
-1);
157 #ifdef LISP_FEATURE_WIN32
159 /* touch every single page in the space to force it to be mapped. */
160 for (count
= 0; count
< bytes
; count
+= 0x1000) {
161 volatile int temp
= addr
[count
];
167 fseek(file
, 0, SEEK_END
);
168 data
= (ftell(file
)+os_vm_page_size
-1)&~(os_vm_page_size
-1);
169 fseek(file
, data
, SEEK_SET
);
170 write_bytes_to_file(file
, addr
, bytes
, compression
);
171 fseek(file
, here
, SEEK_SET
);
172 return ((data
- file_offset
) / os_vm_page_size
) - 1;
175 static long __attribute__((__unused__
))
176 write_bytes(FILE *file
, char *addr
, long bytes
, os_vm_offset_t file_offset
)
178 return write_and_compress_bytes(file
, addr
, bytes
, file_offset
,
179 COMPRESSION_LEVEL_NONE
);
182 extern struct lisp_startup_options lisp_startup_options
;
185 output_space(FILE *file
, int id
, lispobj
*addr
, lispobj
*end
,
186 os_vm_offset_t file_offset
,
187 int core_compression_level
)
189 size_t words
, bytes
, data
, compressed_flag
;
190 static char *names
[] = {NULL
, "dynamic", "static", "read-only",
191 "immobile", "immobile"};
194 = ((core_compression_level
!= COMPRESSION_LEVEL_NONE
)
195 ? DEFLATED_CORE_SPACE_ID_FLAG
: 0);
197 write_lispobj(id
| compressed_flag
, file
);
199 write_lispobj(words
, file
);
201 bytes
= words
* sizeof(lispobj
);
203 if (!lisp_startup_options
.noinform
)
204 printf("writing %lu bytes from the %s space at %p\n",
205 (long unsigned)bytes
, names
[id
], addr
);
207 data
= write_and_compress_bytes(file
, (char *)addr
, bytes
, file_offset
,
208 core_compression_level
);
210 write_lispobj(data
, file
);
211 write_lispobj((uword_t
)addr
/ os_vm_page_size
, file
);
212 write_lispobj((bytes
+ os_vm_page_size
- 1) / os_vm_page_size
, file
);
216 open_core_for_saving(char *filename
)
218 /* Open the output file. We don't actually need the file yet, but
219 * the fopen() might fail for some reason, and we want to detect
220 * that and back out before we do anything irreversible. */
222 return fopen(filename
, "wb");
225 #ifdef LISP_FEATURE_IMMOBILE_SPACE
226 extern void prepare_immobile_space_for_save();
227 # define N_SPACES_TO_SAVE 5
228 # ifdef LISP_FEATURE_IMMOBILE_CODE
229 lispobj code_component_order
;
230 extern void defrag_immobile_space(lispobj
,boolean
);
233 # define N_SPACES_TO_SAVE 3
236 save_to_filehandle(FILE *file
, char *filename
, lispobj init_function
,
237 boolean make_executable
,
238 boolean save_runtime_options
,
239 int core_compression_level
)
242 os_vm_offset_t core_start_pos
;
243 boolean verbose
= !lisp_startup_options
.noinform
;
245 #ifdef LISP_FEATURE_X86_64
246 untune_asm_routines_for_microarch();
249 /* Smash the enclosing state. (Once we do this, there's no good
250 * way to go back, which is a sufficient reason that this ends up
251 * being SAVE-LISP-AND-DIE instead of SAVE-LISP-AND-GO-ON). */
253 printf("[undoing binding stack and other enclosing state... ");
256 for_each_thread(th
) { /* XXX really? */
257 unbind_to_here((lispobj
*)th
->binding_stack_start
,th
);
258 SetSymbolValue(CURRENT_CATCH_BLOCK
, 0,th
);
259 SetSymbolValue(CURRENT_UNWIND_PROTECT_BLOCK
, 0,th
);
261 if (verbose
) printf("done]\n");
262 #ifdef LISP_FEATURE_IMMOBILE_CODE
263 // It's better to wait to defrag until after the binding stack is undone,
264 // because we explicitly don't fixup code refs from stacks.
265 // i.e. if there *were* something on the binding stack that cared that code
266 // moved, it would be wrong. This way we can be sure we don't care.
267 if (code_component_order
) {
268 // Assert that defrag will not move the init_function
269 gc_assert(!immobile_space_p(init_function
));
271 printf("[defragmenting immobile space... ");
274 defrag_immobile_space(code_component_order
, verbose
);
275 if (verbose
) printf("done]\n");
279 /* (Now we can actually start copying ourselves into the output file.) */
282 printf("[saving current Lisp image into %s:\n", filename
);
286 core_start_pos
= ftell(file
);
287 write_lispobj(CORE_MAGIC
, file
);
289 write_lispobj(BUILD_ID_CORE_ENTRY_TYPE_CODE
, file
);
290 write_lispobj(/* (We're writing the word count of the entry here, and the 2
291 * term is one word for the leading BUILD_ID_CORE_ENTRY_TYPE_CODE
292 * word and one word where we store the count itself.) */
293 2 + strlen((const char *)build_id
),
297 for (p
= (unsigned char *)build_id
; *p
; ++p
)
298 write_lispobj(*p
, file
);
301 write_lispobj(NEW_DIRECTORY_CORE_ENTRY_TYPE_CODE
, file
);
302 write_lispobj(/* (word count = N spaces described by 5 words each, plus the
303 * entry type code, plus this count itself) */
304 (5*N_SPACES_TO_SAVE
)+2, file
);
306 READ_ONLY_CORE_SPACE_ID
,
307 (lispobj
*)READ_ONLY_SPACE_START
,
308 read_only_space_free_pointer
,
310 core_compression_level
);
312 STATIC_CORE_SPACE_ID
,
313 (lispobj
*)STATIC_SPACE_START
,
314 static_space_free_pointer
,
316 core_compression_level
);
317 #ifdef LISP_FEATURE_GENCGC
318 /* Flush the current_region, updating the tables. */
319 gc_alloc_update_all_page_tables(1);
320 update_dynamic_space_free_pointer();
322 #ifdef LISP_FEATURE_IMMOBILE_SPACE
323 prepare_immobile_space_for_save();
325 IMMOBILE_FIXEDOBJ_CORE_SPACE_ID
,
326 (lispobj
*)IMMOBILE_SPACE_START
,
327 immobile_fixedobj_free_pointer
,
329 core_compression_level
);
331 IMMOBILE_VARYOBJ_CORE_SPACE_ID
,
332 (lispobj
*)IMMOBILE_VARYOBJ_SUBSPACE_START
,
333 immobile_space_free_pointer
,
335 core_compression_level
);
338 DYNAMIC_CORE_SPACE_ID
,
339 current_dynamic_space
,
340 (lispobj
*)get_alloc_pointer(),
342 core_compression_level
);
344 write_lispobj(INITIAL_FUN_CORE_ENTRY_TYPE_CODE
, file
);
345 write_lispobj(3, file
);
346 write_lispobj(init_function
, file
);
348 #ifdef LISP_FEATURE_GENCGC
350 size_t true_size
= sizeof last_free_page
351 + (last_free_page
* sizeof(struct corefile_pte
));
352 size_t rounded_size
= CEILING(true_size
, os_vm_page_size
);
353 char* data
= successful_malloc(rounded_size
);
354 *(page_index_t
*)data
= last_free_page
;
355 struct corefile_pte
*ptes
= (struct corefile_pte
*)(data
+ sizeof(page_index_t
));
357 for (i
= 0; i
< last_free_page
; i
++) {
358 /* Thanks to alignment requirements, the two low bits
359 * are always zero, so we can use them to store the
360 * allocation type -- region is always closed, so only
361 * the two low bits of allocation flags matter. */
362 uword_t word
= page_scan_start_offset(i
);
363 gc_assert((word
& 0x03) == 0);
364 ptes
[i
].sso
= word
| (0x03 & page_table
[i
].allocated
);
365 ptes
[i
].bytes_used
= page_bytes_used(i
);
367 write_lispobj(PAGE_TABLE_CORE_ENTRY_TYPE_CODE
, file
);
368 write_lispobj(4, file
);
369 write_lispobj(rounded_size
, file
);
370 sword_t offset
= write_bytes(file
, data
, rounded_size
, core_start_pos
);
371 write_lispobj(offset
, file
);
375 write_lispobj(END_CORE_ENTRY_TYPE_CODE
, file
);
377 /* Write a trailing header, ignored when parsing the core normally.
378 * This is used to locate the start of the core when the runtime is
379 * prepended to it. */
380 fseek(file
, 0, SEEK_END
);
382 /* If NULL runtime options are passed to write_runtime_options,
383 * command-line processing is performed as normal in the SBCL
384 * executable. Otherwise, the saved runtime options are used and
385 * all command-line arguments are available to Lisp in
386 * SB-EXT:*POSIX-ARGV*. */
387 write_runtime_options(file
,
388 (save_runtime_options
? runtime_options
: NULL
));
390 if (1 != fwrite(&core_start_pos
, sizeof(os_vm_offset_t
), 1, file
)) {
391 perror("Error writing core starting position to file");
394 write_lispobj(CORE_MAGIC
, file
);
398 #ifndef LISP_FEATURE_WIN32
400 chmod (filename
, 0755);
403 if (verbose
) printf("done]\n");
406 #undef N_SPACES_TO_SAVE
408 /* Check if the build_id for the current runtime is present in a
411 check_runtime_build_id(void *buf
, size_t size
)
416 idlen
= strlen((const char*)build_id
) - 1;
417 while ((pos
= memchr(buf
, build_id
[0], size
)) != NULL
) {
418 size
-= (pos
+ 1) - (char *)buf
;
420 if (idlen
<= size
&& memcmp(buf
, build_id
+ 1, idlen
) == 0)
427 /* Slurp the executable portion of the runtime into a malloced buffer
428 * and return it. Places the size in bytes of the runtime into
429 * 'size_out'. Returns NULL if the runtime cannot be loaded from
432 load_runtime(char *runtime_path
, size_t *size_out
)
437 os_vm_offset_t core_offset
;
439 core_offset
= search_for_embedded_core (runtime_path
);
440 if ((input
= fopen(runtime_path
, "rb")) == NULL
) {
441 fprintf(stderr
, "Unable to open runtime: %s\n", runtime_path
);
445 fseek(input
, 0, SEEK_END
);
446 size
= (size_t) ftell(input
);
447 fseek(input
, 0, SEEK_SET
);
449 if (core_offset
!= -1 && size
> (size_t) core_offset
)
452 buf
= successful_malloc(size
);
453 if ((count
= fread(buf
, 1, size
, input
)) != size
) {
454 fprintf(stderr
, "Premature EOF while reading runtime.\n");
458 if (!check_runtime_build_id(buf
, size
)) {
459 fprintf(stderr
, "Failed to locate current build_id in runtime: %s\n",
477 save_runtime_to_filehandle(FILE *output
, void *runtime
, size_t runtime_size
,
478 int application_type
)
483 #ifdef LISP_FEATURE_WIN32
485 PIMAGE_DOS_HEADER dos_header
= (PIMAGE_DOS_HEADER
)runtime
;
486 PIMAGE_NT_HEADERS nt_header
= (PIMAGE_NT_HEADERS
)((char *)dos_header
+
487 dos_header
->e_lfanew
);
490 switch (application_type
) {
492 sub_system
= IMAGE_SUBSYSTEM_WINDOWS_CUI
;
495 sub_system
= IMAGE_SUBSYSTEM_WINDOWS_GUI
;
498 fprintf(stderr
, "Invalid application type %d\n", application_type
);
502 nt_header
->OptionalHeader
.Subsystem
= sub_system
;
506 if (runtime_size
!= fwrite(runtime
, 1, runtime_size
, output
)) {
507 perror("Error saving runtime");
511 padding
= (os_vm_page_size
- (runtime_size
% os_vm_page_size
)) & ~os_vm_page_size
;
513 padbytes
= successful_malloc(padding
);
514 memset(padbytes
, 0, padding
);
515 if (padding
!= fwrite(padbytes
, 1, padding
, output
)) {
516 perror("Error saving runtime");
527 prepare_to_save(char *filename
, boolean prepend_runtime
, void **runtime_bytes
,
528 size_t *runtime_size
)
533 if (prepend_runtime
) {
534 runtime_path
= os_get_runtime_executable_path(0);
536 if (runtime_path
== NULL
&& saved_runtime_path
== NULL
) {
537 fprintf(stderr
, "Unable to get default runtime path.\n");
541 if (runtime_path
== NULL
)
542 *runtime_bytes
= load_runtime(saved_runtime_path
, runtime_size
);
544 *runtime_bytes
= load_runtime(runtime_path
, runtime_size
);
548 if (*runtime_bytes
== NULL
)
552 file
= open_core_for_saving(filename
);
554 free(*runtime_bytes
);
562 #ifdef LISP_FEATURE_CHENEYGC
564 save(char *filename
, lispobj init_function
, boolean prepend_runtime
,
565 boolean save_runtime_options
, boolean compressed
, int compression_level
,
566 int application_type
)
569 void *runtime_bytes
= NULL
;
572 file
= prepare_to_save(filename
, prepend_runtime
, &runtime_bytes
, &runtime_size
);
577 save_runtime_to_filehandle(file
, runtime_bytes
, runtime_size
, application_type
);
579 return save_to_filehandle(file
, filename
, init_function
, prepend_runtime
,
580 save_runtime_options
,
581 compressed
? compressed
: COMPRESSION_LEVEL_NONE
);