5 bool factor_arg(const vm_char* str, const vm_char* arg, cell* value) {
7 if (SSCANF(str, arg, &val) > 0) {
14 vm_parameters::vm_parameters() {
15 embedded_image = false;
18 datastack_size = 32 * sizeof(cell);
19 retainstack_size = 32 * sizeof(cell);
21 #if defined(FACTOR_PPC)
22 callstack_size = 256 * sizeof(cell);
24 callstack_size = 128 * sizeof(cell);
28 young_size = sizeof(cell) / 4;
29 aging_size = sizeof(cell) / 2;
30 tenured_size = 24 * sizeof(cell);
38 console = GetConsoleWindow() != NULL;
46 vm_parameters::~vm_parameters() {
47 free((vm_char *)image_path);
48 free((vm_char *)executable_path);
51 void vm_parameters::init_from_args(int argc, vm_char** argv) {
54 for (i = 1; i < argc; i++) {
55 vm_char* arg = argv[i];
56 if (STRCMP(arg, STRING_LITERAL("--")) == 0)
58 else if (factor_arg(arg, STRING_LITERAL("-datastack=%d"),
61 else if (factor_arg(arg, STRING_LITERAL("-retainstack=%d"),
64 else if (factor_arg(arg, STRING_LITERAL("-callstack=%d"),
67 else if (factor_arg(arg, STRING_LITERAL("-young=%d"), &young_size))
69 else if (factor_arg(arg, STRING_LITERAL("-aging=%d"), &aging_size))
71 else if (factor_arg(arg, STRING_LITERAL("-tenured=%d"), &tenured_size))
73 else if (factor_arg(arg, STRING_LITERAL("-codeheap=%d"), &code_size))
75 else if (factor_arg(arg, STRING_LITERAL("-pic=%d"), &max_pic_size))
77 else if (factor_arg(arg, STRING_LITERAL("-callbacks=%d"), &callback_size))
79 else if (STRNCMP(arg, STRING_LITERAL("-i="), 3) == 0) {
80 // In case you specify -i more than once.
82 free((vm_char *)image_path);
84 image_path = safe_strdup(arg + 3);
86 else if (STRCMP(arg, STRING_LITERAL("-fep")) == 0)
88 else if (STRCMP(arg, STRING_LITERAL("-nosignals")) == 0)
90 else if (STRCMP(arg, STRING_LITERAL("-console")) == 0)
95 void factor_vm::load_data_heap(FILE* file, image_header* h, vm_parameters* p) {
96 p->tenured_size = std::max((h->data_size * 3) / 2, p->tenured_size);
98 init_data_heap(p->young_size, p->aging_size, p->tenured_size);
101 raw_fread((void*)data->tenured->start, 1, h->data_size, file);
103 if ((cell)bytes_read != h->data_size) {
104 std::cout << "truncated image: " << bytes_read << " bytes read, ";
105 std::cout << h->data_size << " bytes expected\n";
106 fatal_error("load_data_heap failed", 0);
109 data->tenured->initial_free_list(h->data_size);
112 void factor_vm::load_code_heap(FILE* file, image_header* h, vm_parameters* p) {
113 if (h->code_size > p->code_size)
114 fatal_error("Code heap too small to fit image", h->code_size);
116 code = new code_heap(p->code_size);
118 if (h->code_size != 0) {
120 raw_fread((void*)code->allocator->start, 1, h->code_size, file);
121 if (bytes_read != h->code_size) {
122 std::cout << "truncated image: " << bytes_read << " bytes read, ";
123 std::cout << h->code_size << " bytes expected\n";
124 fatal_error("load_code_heap failed", 0);
128 code->allocator->initial_free_list(h->code_size);
129 code->initialize_all_blocks_set();
132 struct startup_fixup {
133 static const bool translated_code_block_map = true;
138 startup_fixup(cell data_offset, cell code_offset)
139 : data_offset(data_offset), code_offset(code_offset) {}
141 object* fixup_data(object* obj) {
142 return (object*)((cell)obj + data_offset);
145 code_block* fixup_code(code_block* obj) {
146 return (code_block*)((cell)obj + code_offset);
149 object* translate_data(const object* obj) { return fixup_data((object*)obj); }
151 code_block* translate_code(const code_block* compiled) {
152 return fixup_code((code_block*)compiled);
155 cell size(const object* obj) { return obj->size(*this); }
157 cell size(code_block* compiled) { return compiled->size(*this); }
160 void factor_vm::fixup_heaps(cell data_offset, cell code_offset) {
161 startup_fixup fixup(data_offset, code_offset);
162 slot_visitor<startup_fixup> visitor(this, fixup);
163 visitor.visit_all_roots();
165 auto start_object_updater = [&](object *obj, cell size) {
166 data->tenured->starts.record_object_start_offset(obj);
167 visitor.visit_slots(obj);
168 switch (obj->type()) {
170 alien* ptr = (alien*)obj;
171 if (to_boolean(ptr->base))
172 ptr->update_address();
174 ptr->expired = special_objects[OBJ_CANONICAL_TRUE];
178 ffi_dlopen((dll*)obj);
182 visitor.visit_object_code_block(obj);
187 data->tenured->iterate(start_object_updater, fixup);
189 auto updater = [&](code_block* compiled, cell size) {
190 visitor.visit_code_block_objects(compiled);
191 cell rel_base = compiled->entry_point() - fixup.code_offset;
192 visitor.visit_instruction_operands(compiled, rel_base);
194 code->allocator->iterate(updater, fixup);
197 bool factor_vm::read_embedded_image_footer(FILE* file,
198 embedded_image_footer* footer) {
199 safe_fseek(file, -(off_t)sizeof(embedded_image_footer), SEEK_END);
200 safe_fread(footer, (off_t)sizeof(embedded_image_footer), 1, file);
201 return footer->magic == image_magic;
204 char *threadsafe_strerror(int errnum) {
205 char *buf = (char *) malloc(STRERROR_BUFFER_SIZE);
207 fatal_error("Out of memory in threadsafe_strerror, errno", errnum);
209 THREADSAFE_STRERROR(errnum, buf, STRERROR_BUFFER_SIZE);
213 // Read an image file from disk, only done once during startup
214 // This function also initializes the data and code heaps
215 void factor_vm::load_image(vm_parameters* p) {
217 FILE* file = OPEN_READ(p->image_path);
219 std::cout << "Cannot open image file: " << p->image_path << std::endl;
220 char *msg = threadsafe_strerror(errno);
221 std::cout << "strerror:2: " << msg << std::endl;
225 if (p->embedded_image) {
226 embedded_image_footer footer;
227 if (!read_embedded_image_footer(file, &footer)) {
228 std::cout << "No embedded image" << std::endl;
231 safe_fseek(file, (off_t)footer.image_offset, SEEK_SET);
235 if (raw_fread(&h, sizeof(image_header), 1, file) != 1)
236 fatal_error("Cannot read image header", 0);
238 if (h.magic != image_magic)
239 fatal_error("Bad image: magic number check failed", h.magic);
241 if (h.version != image_version)
242 fatal_error("Bad image: version number check failed", h.version);
244 load_data_heap(file, &h, p);
245 load_code_heap(file, &h, p);
249 // Certain special objects in the image are known to the runtime
250 memcpy(special_objects, h.special_objects, sizeof(special_objects));
252 cell data_offset = data->tenured->start - h.data_relocation_base;
253 cell code_offset = code->allocator->start - h.code_relocation_base;
254 fixup_heaps(data_offset, code_offset);
257 // Save the current image to disk. We don't throw any exceptions here
258 // because if the 'then-die' argument is t it is not safe to do
259 // so. Instead we signal failure by returning false.
260 bool factor_vm::save_image(const vm_char* saving_filename,
261 const vm_char* filename) {
264 h.magic = image_magic;
265 h.version = image_version;
266 h.data_relocation_base = data->tenured->start;
267 h.data_size = data->tenured->occupied_space();
268 h.code_relocation_base = code->allocator->start;
269 h.code_size = code->allocator->occupied_space();
271 for (cell i = 0; i < special_object_count; i++)
272 h.special_objects[i] =
273 (save_special_p(i) ? special_objects[i] : false_object);
275 FILE* file = OPEN_WRITE(saving_filename);
278 if (safe_fwrite(&h, sizeof(image_header), 1, file) != 1)
280 if (h.data_size > 0 &&
281 safe_fwrite((void*)data->tenured->start, h.data_size, 1, file) != 1)
283 if (h.code_size > 0 &&
284 safe_fwrite((void*)code->allocator->start, h.code_size, 1, file) != 1)
286 if (raw_fclose(file) == -1)
288 if (!move_file(saving_filename, filename))
294 void factor_vm::primitive_save_image() {
295 // We unbox this before doing anything else. This is the only point
296 // where we might throw an error, so we have to throw an error here since
297 // later steps destroy the current image.
298 bool then_die = to_boolean(ctx->pop());
299 byte_array* path2 = untag_check<byte_array>(ctx->pop());
300 byte_array* path1 = untag_check<byte_array>(ctx->pop());
302 // Copy the paths to non-gc memory to avoid them hanging around in
304 vm_char* path1_saved = safe_strdup(path1->data<vm_char>());
305 vm_char* path2_saved = safe_strdup(path2->data<vm_char>());
308 // strip out special_objects data which is set on startup anyway
309 for (cell i = 0; i < special_object_count; i++)
310 if (!save_special_p(i))
311 special_objects[i] = false_object;
313 // dont trace objects only reachable from context stacks so we don't
314 // get volatile data saved in the image.
315 active_contexts.clear();
316 code->uninitialized_blocks.clear();
318 // I think clearing the callback heap should be fine too.
319 callbacks->allocator->initial_free_list(0);
322 // do a full GC to push everything remaining into tenured space
323 primitive_compact_gc();
326 bool ret = save_image(path1_saved, path2_saved);
334 general_error(ERROR_IO, tag_fixnum(errno), false_object);
338 bool factor_vm::embedded_image_p() {
339 const vm_char* vm_path = vm_executable_path();
340 FILE* file = OPEN_READ(vm_path);
342 free((vm_char *)vm_path);
345 embedded_image_footer footer;
346 bool embedded_p = read_embedded_image_footer(file, &footer);
348 free((vm_char *)vm_path);