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 "genesis/static-symbols.h"
39 #include "genesis/symbol.h"
41 #ifdef LISP_FEATURE_SB_CORE_COMPRESSION
45 /* write_runtime_options uses a simple serialization scheme that
46 * consists of one word of magic, one word indicating whether options
47 * are actually saved, and one word per struct field. */
49 write_runtime_options(FILE *file
, struct runtime_options
*options
)
51 size_t optarray
[RUNTIME_OPTIONS_WORDS
];
53 memset(&optarray
, 0, sizeof(optarray
));
54 optarray
[0] = RUNTIME_OPTIONS_MAGIC
;
56 if (options
!= NULL
) {
57 /* optarray[1] is a flag indicating that options are present */
59 optarray
[2] = options
->dynamic_space_size
;
60 optarray
[3] = options
->thread_control_stack_size
;
63 if (RUNTIME_OPTIONS_WORDS
!=
64 fwrite(optarray
, sizeof(size_t), RUNTIME_OPTIONS_WORDS
, file
)) {
65 perror("Error writing runtime options to file");
70 write_lispobj(lispobj obj
, FILE *file
)
72 if (1 != fwrite(&obj
, sizeof(lispobj
), 1, file
)) {
73 perror("Error writing to file");
78 write_bytes_to_file(FILE * file
, char *addr
, long bytes
, int compression
)
80 if (compression
== COMPRESSION_LEVEL_NONE
) {
82 sword_t count
= fwrite(addr
, 1, bytes
, file
);
88 perror("error writing to save file");
92 #ifdef LISP_FEATURE_SB_CORE_COMPRESSION
93 } else if ((compression
>= -1) && (compression
<= 9)) {
94 # define ZLIB_BUFFER_SIZE (1u<<16)
96 unsigned char buf
[ZLIB_BUFFER_SIZE
];
97 unsigned char * written
, * end
;
98 long total_written
= 0;
100 stream
.zalloc
= NULL
;
102 stream
.opaque
= NULL
;
103 stream
.avail_in
= bytes
;
104 stream
.next_in
= (void*)addr
;
105 ret
= deflateInit(&stream
, compression
);
107 lose("deflateInit: %i\n", ret
);
109 stream
.avail_out
= sizeof(buf
);
110 stream
.next_out
= buf
;
111 ret
= deflate(&stream
, Z_FINISH
);
112 if (ret
< 0) lose("zlib deflate error: %i... exiting\n", ret
);
114 end
= buf
+sizeof(buf
)-stream
.avail_out
;
115 total_written
+= end
- written
;
116 while (written
< end
) {
117 long count
= fwrite(written
, 1, end
-written
, file
);
121 lose("unable to write to core file\n");
124 } while (stream
.avail_out
== 0);
126 printf("compressed %lu bytes into %lu at level %i\n",
127 bytes
, total_written
, compression
);
128 # undef ZLIB_BUFFER_SIZE
131 #ifdef LISP_FEATURE_SB_CORE_COMPRESSION
132 lose("Unknown core compression level %i, exiting\n", compression
);
134 lose("zlib-compressed core support not built in this runtime\n");
143 write_and_compress_bytes(FILE *file
, char *addr
, long bytes
, os_vm_offset_t file_offset
,
148 bytes
= (bytes
+os_vm_page_size
-1)&~(os_vm_page_size
-1);
150 #ifdef LISP_FEATURE_WIN32
152 /* touch every single page in the space to force it to be mapped. */
153 for (count
= 0; count
< bytes
; count
+= 0x1000) {
154 volatile int temp
= addr
[count
];
160 fseek(file
, 0, SEEK_END
);
161 data
= (ftell(file
)+os_vm_page_size
-1)&~(os_vm_page_size
-1);
162 fseek(file
, data
, SEEK_SET
);
163 write_bytes_to_file(file
, addr
, bytes
, compression
);
164 fseek(file
, here
, SEEK_SET
);
165 return ((data
- file_offset
) / os_vm_page_size
) - 1;
169 write_bytes(FILE *file
, char *addr
, long bytes
, os_vm_offset_t file_offset
)
171 return write_and_compress_bytes(file
, addr
, bytes
, file_offset
,
172 COMPRESSION_LEVEL_NONE
);
176 output_space(FILE *file
, int id
, lispobj
*addr
, lispobj
*end
,
177 os_vm_offset_t file_offset
,
178 int core_compression_level
)
180 size_t words
, bytes
, data
, compressed_flag
;
181 static char *names
[] = {NULL
, "dynamic", "static", "read-only"};
184 = ((core_compression_level
!= COMPRESSION_LEVEL_NONE
)
185 ? DEFLATED_CORE_SPACE_ID_FLAG
: 0);
187 write_lispobj(id
| compressed_flag
, file
);
189 write_lispobj(words
, file
);
191 bytes
= words
* sizeof(lispobj
);
193 printf("writing %lu bytes from the %s space at %p\n",
194 bytes
, names
[id
], addr
);
196 data
= write_and_compress_bytes(file
, (char *)addr
, bytes
, file_offset
,
197 core_compression_level
);
199 write_lispobj(data
, file
);
200 write_lispobj((uword_t
)addr
/ os_vm_page_size
, file
);
201 write_lispobj((bytes
+ os_vm_page_size
- 1) / os_vm_page_size
, file
);
205 open_core_for_saving(char *filename
)
207 /* Open the output file. We don't actually need the file yet, but
208 * the fopen() might fail for some reason, and we want to detect
209 * that and back out before we do anything irreversible. */
211 return fopen(filename
, "wb");
215 save_to_filehandle(FILE *file
, char *filename
, lispobj init_function
,
216 boolean make_executable
,
217 boolean save_runtime_options
,
218 int core_compression_level
)
221 os_vm_offset_t core_start_pos
;
223 /* Smash the enclosing state. (Once we do this, there's no good
224 * way to go back, which is a sufficient reason that this ends up
225 * being SAVE-LISP-AND-DIE instead of SAVE-LISP-AND-GO-ON). */
226 printf("[undoing binding stack and other enclosing state... ");
228 for_each_thread(th
) { /* XXX really? */
229 unbind_to_here((lispobj
*)th
->binding_stack_start
,th
);
230 SetSymbolValue(CURRENT_CATCH_BLOCK
, 0,th
);
231 SetSymbolValue(CURRENT_UNWIND_PROTECT_BLOCK
, 0,th
);
236 /* (Now we can actually start copying ourselves into the output file.) */
238 printf("[saving current Lisp image into %s:\n", filename
);
241 core_start_pos
= ftell(file
);
242 write_lispobj(CORE_MAGIC
, file
);
244 write_lispobj(BUILD_ID_CORE_ENTRY_TYPE_CODE
, file
);
245 write_lispobj(/* (We're writing the word count of the entry here, and the 2
246 * term is one word for the leading BUILD_ID_CORE_ENTRY_TYPE_CODE
247 * word and one word where we store the count itself.) */
248 2 + strlen((const char *)build_id
),
252 for (p
= (unsigned char *)build_id
; *p
; ++p
)
253 write_lispobj(*p
, file
);
256 write_lispobj(NEW_DIRECTORY_CORE_ENTRY_TYPE_CODE
, file
);
257 write_lispobj(/* (word count = 3 spaces described by 5 words each, plus the
258 * entry type code, plus this count itself) */
261 READ_ONLY_CORE_SPACE_ID
,
262 (lispobj
*)READ_ONLY_SPACE_START
,
263 (lispobj
*)SymbolValue(READ_ONLY_SPACE_FREE_POINTER
,0),
265 core_compression_level
);
267 STATIC_CORE_SPACE_ID
,
268 (lispobj
*)STATIC_SPACE_START
,
269 (lispobj
*)SymbolValue(STATIC_SPACE_FREE_POINTER
,0),
271 core_compression_level
);
272 #ifdef LISP_FEATURE_GENCGC
273 /* Flush the current_region, updating the tables. */
274 gc_alloc_update_all_page_tables();
275 update_dynamic_space_free_pointer();
278 #ifdef LISP_FEATURE_GENCGC
280 DYNAMIC_CORE_SPACE_ID
,
281 (lispobj
*)DYNAMIC_SPACE_START
,
282 dynamic_space_free_pointer
,
284 core_compression_level
);
287 DYNAMIC_CORE_SPACE_ID
,
288 (lispobj
*)current_dynamic_space
,
289 dynamic_space_free_pointer
,
291 core_compression_level
);
295 DYNAMIC_CORE_SPACE_ID
,
296 (lispobj
*)DYNAMIC_SPACE_START
,
297 (lispobj
*)SymbolValue(ALLOCATION_POINTER
,0),
299 core_compression_level
);
302 write_lispobj(INITIAL_FUN_CORE_ENTRY_TYPE_CODE
, file
);
303 write_lispobj(3, file
);
304 write_lispobj(init_function
, file
);
306 #ifdef LISP_FEATURE_GENCGC
308 size_t size
= (last_free_page
*sizeof(sword_t
)+os_vm_page_size
-1)
309 &~(os_vm_page_size
-1);
310 uword_t
*data
= calloc(size
, 1);
315 for (i
= 0; i
< last_free_page
; i
++) {
316 /* Thanks to alignment requirements, the two low bits
317 * are always zero, so we can use them to store the
318 * allocation type -- region is always closed, so only
319 * the two low bits of allocation flags matter. */
320 word
= page_table
[i
].scan_start_offset
;
321 gc_assert((word
& 0x03) == 0);
322 data
[i
] = word
| (0x03 & page_table
[i
].allocated
);
324 write_lispobj(PAGE_TABLE_CORE_ENTRY_TYPE_CODE
, file
);
325 write_lispobj(4, file
);
326 write_lispobj(size
, file
);
327 offset
= write_bytes(file
, (char *)data
, size
, core_start_pos
);
328 write_lispobj(offset
, file
);
333 write_lispobj(END_CORE_ENTRY_TYPE_CODE
, file
);
335 /* Write a trailing header, ignored when parsing the core normally.
336 * This is used to locate the start of the core when the runtime is
337 * prepended to it. */
338 fseek(file
, 0, SEEK_END
);
340 /* If NULL runtime options are passed to write_runtime_options,
341 * command-line processing is performed as normal in the SBCL
342 * executable. Otherwise, the saved runtime options are used and
343 * all command-line arguments are available to Lisp in
344 * SB-EXT:*POSIX-ARGV*. */
345 write_runtime_options(file
,
346 (save_runtime_options
? runtime_options
: NULL
));
348 if (1 != fwrite(&core_start_pos
, sizeof(os_vm_offset_t
), 1, file
)) {
349 perror("Error writing core starting position to file");
352 write_lispobj(CORE_MAGIC
, file
);
356 #ifndef LISP_FEATURE_WIN32
358 chmod (filename
, 0755);
365 /* Check if the build_id for the current runtime is present in a
368 check_runtime_build_id(void *buf
, size_t size
)
373 idlen
= strlen((const char*)build_id
) - 1;
374 while ((pos
= memchr(buf
, build_id
[0], size
)) != NULL
) {
375 size
-= (pos
+ 1) - (char *)buf
;
377 if (idlen
<= size
&& memcmp(buf
, build_id
+ 1, idlen
) == 0)
384 /* Slurp the executable portion of the runtime into a malloced buffer
385 * and return it. Places the size in bytes of the runtime into
386 * 'size_out'. Returns NULL if the runtime cannot be loaded from
389 load_runtime(char *runtime_path
, size_t *size_out
)
394 os_vm_offset_t core_offset
;
396 core_offset
= search_for_embedded_core (runtime_path
);
397 if ((input
= fopen(runtime_path
, "rb")) == NULL
) {
398 fprintf(stderr
, "Unable to open runtime: %s\n", runtime_path
);
402 fseek(input
, 0, SEEK_END
);
403 size
= (size_t) ftell(input
);
404 fseek(input
, 0, SEEK_SET
);
406 if (core_offset
!= -1 && size
> core_offset
)
409 buf
= successful_malloc(size
);
410 if ((count
= fread(buf
, 1, size
, input
)) != size
) {
411 fprintf(stderr
, "Premature EOF while reading runtime.\n");
415 if (!check_runtime_build_id(buf
, size
)) {
416 fprintf(stderr
, "Failed to locate current build_id in runtime: %s\n",
434 save_runtime_to_filehandle(FILE *output
, void *runtime
, size_t runtime_size
,
435 int application_type
)
440 #ifdef LISP_FEATURE_WIN32
442 PIMAGE_DOS_HEADER dos_header
= (PIMAGE_DOS_HEADER
)runtime
;
443 PIMAGE_NT_HEADERS nt_header
= (PIMAGE_NT_HEADERS
)((char *)dos_header
+
444 dos_header
->e_lfanew
);
447 switch (application_type
) {
449 sub_system
= IMAGE_SUBSYSTEM_WINDOWS_CUI
;
452 sub_system
= IMAGE_SUBSYSTEM_WINDOWS_GUI
;
455 fprintf(stderr
, "Invalid application type %d\n", application_type
);
459 nt_header
->OptionalHeader
.Subsystem
= sub_system
;
463 if (runtime_size
!= fwrite(runtime
, 1, runtime_size
, output
)) {
464 perror("Error saving runtime");
468 padding
= (os_vm_page_size
- (runtime_size
% os_vm_page_size
)) & ~os_vm_page_size
;
470 padbytes
= successful_malloc(padding
);
471 memset(padbytes
, 0, padding
);
472 if (padding
!= fwrite(padbytes
, 1, padding
, output
)) {
473 perror("Error saving runtime");
484 prepare_to_save(char *filename
, boolean prepend_runtime
, void **runtime_bytes
,
485 size_t *runtime_size
)
490 if (prepend_runtime
) {
491 runtime_path
= os_get_runtime_executable_path(0);
493 if (runtime_path
== NULL
&& saved_runtime_path
== NULL
) {
494 fprintf(stderr
, "Unable to get default runtime path.\n");
498 if (runtime_path
== NULL
)
499 *runtime_bytes
= load_runtime(saved_runtime_path
, runtime_size
);
501 *runtime_bytes
= load_runtime(runtime_path
, runtime_size
);
505 if (*runtime_bytes
== NULL
)
509 file
= open_core_for_saving(filename
);
511 free(*runtime_bytes
);
520 save(char *filename
, lispobj init_function
, boolean prepend_runtime
,
521 boolean save_runtime_options
, boolean compressed
, int compression_level
,
522 int application_type
)
525 void *runtime_bytes
= NULL
;
528 file
= prepare_to_save(filename
, prepend_runtime
, &runtime_bytes
, &runtime_size
);
533 save_runtime_to_filehandle(file
, runtime_bytes
, runtime_size
, application_type
);
535 return save_to_filehandle(file
, filename
, init_function
, prepend_runtime
,
536 save_runtime_options
,
537 compressed
? compressed
: COMPRESSION_LEVEL_NONE
);