Merge remote-tracking branch 'qemu/master'
[qemu/ar7.git] / vl.c
blob0061eff8f2c421b3bf420b0cb97842a995faf6bf
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
24 #include "qemu/osdep.h"
25 #include "qemu/cutils.h"
26 #include "qemu/help_option.h"
28 #ifdef CONFIG_SECCOMP
29 #include "sysemu/seccomp.h"
30 #endif
32 #if defined(CONFIG_VDE)
33 #include <libvdeplug.h>
34 #endif
36 #ifdef CONFIG_SDL
37 #if defined(__APPLE__) || defined(main)
38 #include <SDL.h>
39 int qemu_main(int argc, char **argv);
40 int main(int argc, char **argv)
42 return qemu_main(argc, argv);
44 #undef main
45 #define main qemu_main
46 #endif
47 #endif /* CONFIG_SDL */
49 #ifdef CONFIG_COCOA
50 #undef main
51 #define main qemu_main
52 #endif /* CONFIG_COCOA */
54 #include <glib.h>
56 #include "qemu/error-report.h"
57 #include "qemu/sockets.h"
58 #include "hw/hw.h"
59 #include "hw/boards.h"
60 #include "sysemu/accel.h"
61 #include "hw/usb.h"
62 #include "hw/i386/pc.h"
63 #include "hw/isa/isa.h"
64 #include "hw/bt.h"
65 #include "sysemu/watchdog.h"
66 #include "hw/smbios/smbios.h"
67 #include "hw/xen/xen.h"
68 #include "hw/qdev.h"
69 #include "hw/loader.h"
70 #include "monitor/qdev.h"
71 #include "sysemu/bt.h"
72 #include "net/net.h"
73 #include "net/slirp.h"
74 #include "monitor/monitor.h"
75 #include "ui/console.h"
76 #include "ui/input.h"
77 #include "sysemu/sysemu.h"
78 #include "sysemu/numa.h"
79 #include "exec/gdbstub.h"
80 #include "qemu/timer.h"
81 #include "sysemu/char.h"
82 #include "qemu/bitmap.h"
83 #include "sysemu/blockdev.h"
84 #include "hw/block/block.h"
85 #include "migration/block.h"
86 #include "sysemu/tpm.h"
87 #include "sysemu/dma.h"
88 #include "audio/audio.h"
89 #include "migration/migration.h"
90 #include "sysemu/kvm.h"
91 #include "qapi/qmp/qjson.h"
92 #include "qemu/option.h"
93 #include "qemu/config-file.h"
94 #include "qemu-options.h"
95 #include "qmp-commands.h"
96 #include "qemu/main-loop.h"
97 #ifdef CONFIG_VIRTFS
98 #include "fsdev/qemu-fsdev.h"
99 #endif
100 #include "sysemu/qtest.h"
102 #include "disas/disas.h"
105 #include "slirp/libslirp.h"
107 #include "trace.h"
108 #include "trace/control.h"
109 #include "qemu/queue.h"
110 #include "sysemu/cpus.h"
111 #include "sysemu/arch_init.h"
113 #include "ui/qemu-spice.h"
114 #include "qapi/string-input-visitor.h"
115 #include "qapi/opts-visitor.h"
116 #include "qom/object_interfaces.h"
117 #include "qapi-event.h"
118 #include "exec/semihost.h"
119 #include "crypto/init.h"
120 #include "sysemu/replay.h"
121 #include "qapi/qmp/qerror.h"
123 #define MAX_VIRTIO_CONSOLES 1
124 #define MAX_SCLP_CONSOLES 1
126 static const char *data_dir[16];
127 static int data_dir_idx;
128 const char *bios_name = NULL;
129 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
130 DisplayType display_type = DT_DEFAULT;
131 int request_opengl = -1;
132 int display_opengl;
133 static int display_remote;
134 const char* keyboard_layout = NULL;
135 ram_addr_t ram_size;
136 const char *mem_path = NULL;
137 int mem_prealloc = 0; /* force preallocation of physical target memory */
138 bool enable_mlock = false;
139 int nb_nics;
140 NICInfo nd_table[MAX_NICS];
141 int autostart;
142 static int rtc_utc = 1;
143 static int rtc_date_offset = -1; /* -1 means no change */
144 QEMUClockType rtc_clock;
145 int vga_interface_type = VGA_NONE;
146 static int full_screen = 0;
147 static int no_frame = 0;
148 int no_quit = 0;
149 #ifdef CONFIG_GTK
150 static bool grab_on_hover;
151 #endif
152 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
153 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
154 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
155 CharDriverState *sclp_hds[MAX_SCLP_CONSOLES];
156 int win2k_install_hack = 0;
157 int singlestep = 0;
158 int smp_cpus = 1;
159 int max_cpus = 0;
160 int smp_cores = 1;
161 int smp_threads = 1;
162 int acpi_enabled = 1;
163 int no_hpet = 0;
164 int fd_bootchk = 1;
165 static int no_reboot;
166 int no_shutdown = 0;
167 int cursor_hide = 1;
168 int graphic_rotate = 0;
169 const char *watchdog;
170 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
171 int nb_option_roms;
172 int old_param = 0;
173 const char *qemu_name;
174 int alt_grab = 0;
175 int ctrl_grab = 0;
176 unsigned int nb_prom_envs = 0;
177 const char *prom_envs[MAX_PROM_ENVS];
178 int boot_menu;
179 bool boot_strict;
180 uint8_t *boot_splash_filedata;
181 size_t boot_splash_filedata_size;
182 uint8_t qemu_extra_params_fw[2];
184 int icount_align_option;
186 /* The bytes in qemu_uuid[] are in the order specified by RFC4122, _not_ in the
187 * little-endian "wire format" described in the SMBIOS 2.6 specification.
189 uint8_t qemu_uuid[16];
190 bool qemu_uuid_set;
192 /* Trace unassigned memory or i/o accesses. */
193 bool trace_unassigned;
195 static NotifierList exit_notifiers =
196 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
198 static NotifierList machine_init_done_notifiers =
199 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
201 bool xen_allowed;
202 uint32_t xen_domid;
203 enum xen_mode xen_mode = XEN_EMULATE;
205 static int has_defaults = 1;
206 static int default_serial = 1;
207 static int default_parallel = 1;
208 static int default_virtcon = 1;
209 static int default_sclp = 1;
210 static int default_monitor = 1;
211 static int default_floppy = 1;
212 static int default_cdrom = 1;
213 static int default_sdcard = 1;
214 static int default_vga = 1;
216 static struct {
217 const char *driver;
218 int *flag;
219 } default_list[] = {
220 { .driver = "isa-serial", .flag = &default_serial },
221 { .driver = "isa-parallel", .flag = &default_parallel },
222 { .driver = "isa-fdc", .flag = &default_floppy },
223 { .driver = "ide-cd", .flag = &default_cdrom },
224 { .driver = "ide-hd", .flag = &default_cdrom },
225 { .driver = "ide-drive", .flag = &default_cdrom },
226 { .driver = "scsi-cd", .flag = &default_cdrom },
227 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
228 { .driver = "virtio-serial", .flag = &default_virtcon },
229 { .driver = "VGA", .flag = &default_vga },
230 { .driver = "isa-vga", .flag = &default_vga },
231 { .driver = "cirrus-vga", .flag = &default_vga },
232 { .driver = "isa-cirrus-vga", .flag = &default_vga },
233 { .driver = "vmware-svga", .flag = &default_vga },
234 { .driver = "qxl-vga", .flag = &default_vga },
235 { .driver = "virtio-vga", .flag = &default_vga },
238 static QemuOptsList qemu_rtc_opts = {
239 .name = "rtc",
240 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
241 .desc = {
243 .name = "base",
244 .type = QEMU_OPT_STRING,
246 .name = "clock",
247 .type = QEMU_OPT_STRING,
249 .name = "driftfix",
250 .type = QEMU_OPT_STRING,
252 { /* end of list */ }
256 static QemuOptsList qemu_sandbox_opts = {
257 .name = "sandbox",
258 .implied_opt_name = "enable",
259 .head = QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts.head),
260 .desc = {
262 .name = "enable",
263 .type = QEMU_OPT_BOOL,
265 { /* end of list */ }
269 static QemuOptsList qemu_trace_opts = {
270 .name = "trace",
271 .implied_opt_name = "enable",
272 .head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head),
273 .desc = {
275 .name = "enable",
276 .type = QEMU_OPT_STRING,
279 .name = "events",
280 .type = QEMU_OPT_STRING,
282 .name = "file",
283 .type = QEMU_OPT_STRING,
285 { /* end of list */ }
289 static QemuOptsList qemu_option_rom_opts = {
290 .name = "option-rom",
291 .implied_opt_name = "romfile",
292 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
293 .desc = {
295 .name = "bootindex",
296 .type = QEMU_OPT_NUMBER,
297 }, {
298 .name = "romfile",
299 .type = QEMU_OPT_STRING,
301 { /* end of list */ }
305 static QemuOptsList qemu_machine_opts = {
306 .name = "machine",
307 .implied_opt_name = "type",
308 .merge_lists = true,
309 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
310 .desc = {
312 * no elements => accept any
313 * sanity checking will happen later
314 * when setting machine properties
320 static QemuOptsList qemu_boot_opts = {
321 .name = "boot-opts",
322 .implied_opt_name = "order",
323 .merge_lists = true,
324 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
325 .desc = {
327 .name = "order",
328 .type = QEMU_OPT_STRING,
329 }, {
330 .name = "once",
331 .type = QEMU_OPT_STRING,
332 }, {
333 .name = "menu",
334 .type = QEMU_OPT_BOOL,
335 }, {
336 .name = "splash",
337 .type = QEMU_OPT_STRING,
338 }, {
339 .name = "splash-time",
340 .type = QEMU_OPT_STRING,
341 }, {
342 .name = "reboot-timeout",
343 .type = QEMU_OPT_STRING,
344 }, {
345 .name = "strict",
346 .type = QEMU_OPT_BOOL,
348 { /*End of list */ }
352 static QemuOptsList qemu_add_fd_opts = {
353 .name = "add-fd",
354 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
355 .desc = {
357 .name = "fd",
358 .type = QEMU_OPT_NUMBER,
359 .help = "file descriptor of which a duplicate is added to fd set",
361 .name = "set",
362 .type = QEMU_OPT_NUMBER,
363 .help = "ID of the fd set to add fd to",
365 .name = "opaque",
366 .type = QEMU_OPT_STRING,
367 .help = "free-form string used to describe fd",
369 { /* end of list */ }
373 static QemuOptsList qemu_object_opts = {
374 .name = "object",
375 .implied_opt_name = "qom-type",
376 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
377 .desc = {
382 static QemuOptsList qemu_tpmdev_opts = {
383 .name = "tpmdev",
384 .implied_opt_name = "type",
385 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
386 .desc = {
387 /* options are defined in the TPM backends */
388 { /* end of list */ }
392 static QemuOptsList qemu_realtime_opts = {
393 .name = "realtime",
394 .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
395 .desc = {
397 .name = "mlock",
398 .type = QEMU_OPT_BOOL,
400 { /* end of list */ }
404 static QemuOptsList qemu_msg_opts = {
405 .name = "msg",
406 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
407 .desc = {
409 .name = "timestamp",
410 .type = QEMU_OPT_BOOL,
412 { /* end of list */ }
416 static QemuOptsList qemu_name_opts = {
417 .name = "name",
418 .implied_opt_name = "guest",
419 .merge_lists = true,
420 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
421 .desc = {
423 .name = "guest",
424 .type = QEMU_OPT_STRING,
425 .help = "Sets the name of the guest.\n"
426 "This name will be displayed in the SDL window caption.\n"
427 "The name will also be used for the VNC server",
428 }, {
429 .name = "process",
430 .type = QEMU_OPT_STRING,
431 .help = "Sets the name of the QEMU process, as shown in top etc",
432 }, {
433 .name = "debug-threads",
434 .type = QEMU_OPT_BOOL,
435 .help = "When enabled, name the individual threads; defaults off.\n"
436 "NOTE: The thread names are for debugging and not a\n"
437 "stable API.",
439 { /* End of list */ }
443 static QemuOptsList qemu_mem_opts = {
444 .name = "memory",
445 .implied_opt_name = "size",
446 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
447 .merge_lists = true,
448 .desc = {
450 .name = "size",
451 .type = QEMU_OPT_SIZE,
454 .name = "slots",
455 .type = QEMU_OPT_NUMBER,
458 .name = "maxmem",
459 .type = QEMU_OPT_SIZE,
461 { /* end of list */ }
465 static QemuOptsList qemu_icount_opts = {
466 .name = "icount",
467 .implied_opt_name = "shift",
468 .merge_lists = true,
469 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
470 .desc = {
472 .name = "shift",
473 .type = QEMU_OPT_STRING,
474 }, {
475 .name = "align",
476 .type = QEMU_OPT_BOOL,
477 }, {
478 .name = "sleep",
479 .type = QEMU_OPT_BOOL,
480 }, {
481 .name = "rr",
482 .type = QEMU_OPT_STRING,
483 }, {
484 .name = "rrfile",
485 .type = QEMU_OPT_STRING,
487 { /* end of list */ }
491 static QemuOptsList qemu_semihosting_config_opts = {
492 .name = "semihosting-config",
493 .implied_opt_name = "enable",
494 .head = QTAILQ_HEAD_INITIALIZER(qemu_semihosting_config_opts.head),
495 .desc = {
497 .name = "enable",
498 .type = QEMU_OPT_BOOL,
499 }, {
500 .name = "target",
501 .type = QEMU_OPT_STRING,
502 }, {
503 .name = "arg",
504 .type = QEMU_OPT_STRING,
506 { /* end of list */ }
510 static QemuOptsList qemu_fw_cfg_opts = {
511 .name = "fw_cfg",
512 .implied_opt_name = "name",
513 .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
514 .desc = {
516 .name = "name",
517 .type = QEMU_OPT_STRING,
518 .help = "Sets the fw_cfg name of the blob to be inserted",
519 }, {
520 .name = "file",
521 .type = QEMU_OPT_STRING,
522 .help = "Sets the name of the file from which\n"
523 "the fw_cfg blob will be loaded",
524 }, {
525 .name = "string",
526 .type = QEMU_OPT_STRING,
527 .help = "Sets content of the blob to be inserted from a string",
529 { /* end of list */ }
534 * Get machine options
536 * Returns: machine options (never null).
538 QemuOpts *qemu_get_machine_opts(void)
540 return qemu_find_opts_singleton("machine");
543 const char *qemu_get_vm_name(void)
545 return qemu_name;
548 static void res_free(void)
550 g_free(boot_splash_filedata);
551 boot_splash_filedata = NULL;
554 static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
556 const char *driver = qemu_opt_get(opts, "driver");
557 int i;
559 if (!driver)
560 return 0;
561 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
562 if (strcmp(default_list[i].driver, driver) != 0)
563 continue;
564 *(default_list[i].flag) = 0;
566 return 0;
569 /***********************************************************/
570 /* QEMU state */
572 static RunState current_run_state = RUN_STATE_PRELAUNCH;
574 /* We use RUN_STATE__MAX but any invalid value will do */
575 static RunState vmstop_requested = RUN_STATE__MAX;
576 static QemuMutex vmstop_lock;
578 typedef struct {
579 RunState from;
580 RunState to;
581 } RunStateTransition;
583 static const RunStateTransition runstate_transitions_def[] = {
584 /* from -> to */
585 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
586 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
587 { RUN_STATE_DEBUG, RUN_STATE_PRELAUNCH },
589 { RUN_STATE_INMIGRATE, RUN_STATE_INTERNAL_ERROR },
590 { RUN_STATE_INMIGRATE, RUN_STATE_IO_ERROR },
591 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
592 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
593 { RUN_STATE_INMIGRATE, RUN_STATE_SHUTDOWN },
594 { RUN_STATE_INMIGRATE, RUN_STATE_SUSPENDED },
595 { RUN_STATE_INMIGRATE, RUN_STATE_WATCHDOG },
596 { RUN_STATE_INMIGRATE, RUN_STATE_GUEST_PANICKED },
597 { RUN_STATE_INMIGRATE, RUN_STATE_FINISH_MIGRATE },
598 { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH },
599 { RUN_STATE_INMIGRATE, RUN_STATE_POSTMIGRATE },
601 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
602 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
603 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PRELAUNCH },
605 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
606 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
607 { RUN_STATE_IO_ERROR, RUN_STATE_PRELAUNCH },
609 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
610 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
611 { RUN_STATE_PAUSED, RUN_STATE_PRELAUNCH },
613 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
614 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
615 { RUN_STATE_POSTMIGRATE, RUN_STATE_PRELAUNCH },
617 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
618 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
619 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
621 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
622 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
623 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PRELAUNCH },
625 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
626 { RUN_STATE_RESTORE_VM, RUN_STATE_PRELAUNCH },
628 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
629 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
630 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
631 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
632 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
633 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
634 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
635 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
636 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
637 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
639 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
641 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
642 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
643 { RUN_STATE_SHUTDOWN, RUN_STATE_PRELAUNCH },
645 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
646 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
647 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
648 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
649 { RUN_STATE_SUSPENDED, RUN_STATE_PRELAUNCH },
651 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
652 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
653 { RUN_STATE_WATCHDOG, RUN_STATE_PRELAUNCH },
655 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
656 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
657 { RUN_STATE_GUEST_PANICKED, RUN_STATE_PRELAUNCH },
659 { RUN_STATE__MAX, RUN_STATE__MAX },
662 static bool runstate_valid_transitions[RUN_STATE__MAX][RUN_STATE__MAX];
664 bool runstate_check(RunState state)
666 return current_run_state == state;
669 bool runstate_store(char *str, size_t size)
671 const char *state = RunState_lookup[current_run_state];
672 size_t len = strlen(state) + 1;
674 if (len > size) {
675 return false;
677 memcpy(str, state, len);
678 return true;
681 static void runstate_init(void)
683 const RunStateTransition *p;
685 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
686 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE__MAX; p++) {
687 runstate_valid_transitions[p->from][p->to] = true;
690 qemu_mutex_init(&vmstop_lock);
693 /* This function will abort() on invalid state transitions */
694 void runstate_set(RunState new_state)
696 assert(new_state < RUN_STATE__MAX);
698 if (!runstate_valid_transitions[current_run_state][new_state]) {
699 error_report("invalid runstate transition: '%s' -> '%s'",
700 RunState_lookup[current_run_state],
701 RunState_lookup[new_state]);
702 abort();
704 trace_runstate_set(new_state);
705 current_run_state = new_state;
708 int runstate_is_running(void)
710 return runstate_check(RUN_STATE_RUNNING);
713 bool runstate_needs_reset(void)
715 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
716 runstate_check(RUN_STATE_SHUTDOWN);
719 StatusInfo *qmp_query_status(Error **errp)
721 StatusInfo *info = g_malloc0(sizeof(*info));
723 info->running = runstate_is_running();
724 info->singlestep = singlestep;
725 info->status = current_run_state;
727 return info;
730 static bool qemu_vmstop_requested(RunState *r)
732 qemu_mutex_lock(&vmstop_lock);
733 *r = vmstop_requested;
734 vmstop_requested = RUN_STATE__MAX;
735 qemu_mutex_unlock(&vmstop_lock);
736 return *r < RUN_STATE__MAX;
739 void qemu_system_vmstop_request_prepare(void)
741 qemu_mutex_lock(&vmstop_lock);
744 void qemu_system_vmstop_request(RunState state)
746 vmstop_requested = state;
747 qemu_mutex_unlock(&vmstop_lock);
748 qemu_notify_event();
751 void vm_start(void)
753 RunState requested;
755 qemu_vmstop_requested(&requested);
756 if (runstate_is_running() && requested == RUN_STATE__MAX) {
757 return;
760 /* Ensure that a STOP/RESUME pair of events is emitted if a
761 * vmstop request was pending. The BLOCK_IO_ERROR event, for
762 * example, according to documentation is always followed by
763 * the STOP event.
765 if (runstate_is_running()) {
766 qapi_event_send_stop(&error_abort);
767 } else {
768 cpu_enable_ticks();
769 runstate_set(RUN_STATE_RUNNING);
770 vm_state_notify(1, RUN_STATE_RUNNING);
771 resume_all_vcpus();
774 qapi_event_send_resume(&error_abort);
778 /***********************************************************/
779 /* real time host monotonic timer */
781 static time_t qemu_time(void)
783 return qemu_clock_get_ms(QEMU_CLOCK_HOST) / 1000;
786 /***********************************************************/
787 /* host time/date access */
788 void qemu_get_timedate(struct tm *tm, int offset)
790 time_t ti = qemu_time();
792 ti += offset;
793 if (rtc_date_offset == -1) {
794 if (rtc_utc)
795 gmtime_r(&ti, tm);
796 else
797 localtime_r(&ti, tm);
798 } else {
799 ti -= rtc_date_offset;
800 gmtime_r(&ti, tm);
804 int qemu_timedate_diff(struct tm *tm)
806 time_t seconds;
808 if (rtc_date_offset == -1)
809 if (rtc_utc)
810 seconds = mktimegm(tm);
811 else {
812 struct tm tmp = *tm;
813 tmp.tm_isdst = -1; /* use timezone to figure it out */
814 seconds = mktime(&tmp);
816 else
817 seconds = mktimegm(tm) + rtc_date_offset;
819 return seconds - qemu_time();
822 static void configure_rtc_date_offset(const char *startdate, int legacy)
824 time_t rtc_start_date;
825 struct tm tm;
827 if (!strcmp(startdate, "now") && legacy) {
828 rtc_date_offset = -1;
829 } else {
830 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
831 &tm.tm_year,
832 &tm.tm_mon,
833 &tm.tm_mday,
834 &tm.tm_hour,
835 &tm.tm_min,
836 &tm.tm_sec) == 6) {
837 /* OK */
838 } else if (sscanf(startdate, "%d-%d-%d",
839 &tm.tm_year,
840 &tm.tm_mon,
841 &tm.tm_mday) == 3) {
842 tm.tm_hour = 0;
843 tm.tm_min = 0;
844 tm.tm_sec = 0;
845 } else {
846 goto date_fail;
848 tm.tm_year -= 1900;
849 tm.tm_mon--;
850 rtc_start_date = mktimegm(&tm);
851 if (rtc_start_date == -1) {
852 date_fail:
853 error_report("invalid date format");
854 error_printf("valid formats: "
855 "'2006-06-17T16:01:21' or '2006-06-17'\n");
856 exit(1);
858 rtc_date_offset = qemu_time() - rtc_start_date;
862 static void configure_rtc(QemuOpts *opts)
864 const char *value;
866 value = qemu_opt_get(opts, "base");
867 if (value) {
868 if (!strcmp(value, "utc")) {
869 rtc_utc = 1;
870 } else if (!strcmp(value, "localtime")) {
871 Error *blocker = NULL;
872 rtc_utc = 0;
873 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
874 "-rtc base=localtime");
875 replay_add_blocker(blocker);
876 } else {
877 configure_rtc_date_offset(value, 0);
880 value = qemu_opt_get(opts, "clock");
881 if (value) {
882 if (!strcmp(value, "host")) {
883 rtc_clock = QEMU_CLOCK_HOST;
884 } else if (!strcmp(value, "rt")) {
885 rtc_clock = QEMU_CLOCK_REALTIME;
886 } else if (!strcmp(value, "vm")) {
887 rtc_clock = QEMU_CLOCK_VIRTUAL;
888 } else {
889 error_report("invalid option value '%s'", value);
890 exit(1);
893 value = qemu_opt_get(opts, "driftfix");
894 if (value) {
895 if (!strcmp(value, "slew")) {
896 static GlobalProperty slew_lost_ticks[] = {
898 .driver = "mc146818rtc",
899 .property = "lost_tick_policy",
900 .value = "slew",
902 { /* end of list */ }
905 qdev_prop_register_global_list(slew_lost_ticks);
906 } else if (!strcmp(value, "none")) {
907 /* discard is default */
908 } else {
909 error_report("invalid option value '%s'", value);
910 exit(1);
915 /***********************************************************/
916 /* Bluetooth support */
917 static int nb_hcis;
918 static int cur_hci;
919 static struct HCIInfo *hci_table[MAX_NICS];
921 struct HCIInfo *qemu_next_hci(void)
923 if (cur_hci == nb_hcis)
924 return &null_hci;
926 return hci_table[cur_hci++];
929 static int bt_hci_parse(const char *str)
931 struct HCIInfo *hci;
932 bdaddr_t bdaddr;
934 if (nb_hcis >= MAX_NICS) {
935 error_report("too many bluetooth HCIs (max %i)", MAX_NICS);
936 return -1;
939 hci = hci_init(str);
940 if (!hci)
941 return -1;
943 bdaddr.b[0] = 0x52;
944 bdaddr.b[1] = 0x54;
945 bdaddr.b[2] = 0x00;
946 bdaddr.b[3] = 0x12;
947 bdaddr.b[4] = 0x34;
948 bdaddr.b[5] = 0x56 + nb_hcis;
949 hci->bdaddr_set(hci, bdaddr.b);
951 hci_table[nb_hcis++] = hci;
953 return 0;
956 static void bt_vhci_add(int vlan_id)
958 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
960 if (!vlan->slave)
961 error_report("warning: adding a VHCI to an empty scatternet %i",
962 vlan_id);
964 bt_vhci_init(bt_new_hci(vlan));
967 static struct bt_device_s *bt_device_add(const char *opt)
969 struct bt_scatternet_s *vlan;
970 int vlan_id = 0;
971 char *endp = strstr(opt, ",vlan=");
972 int len = (endp ? endp - opt : strlen(opt)) + 1;
973 char devname[10];
975 pstrcpy(devname, MIN(sizeof(devname), len), opt);
977 if (endp) {
978 vlan_id = strtol(endp + 6, &endp, 0);
979 if (*endp) {
980 error_report("unrecognised bluetooth vlan Id");
981 return 0;
985 vlan = qemu_find_bt_vlan(vlan_id);
987 if (!vlan->slave)
988 error_report("warning: adding a slave device to an empty scatternet %i",
989 vlan_id);
991 if (!strcmp(devname, "keyboard"))
992 return bt_keyboard_init(vlan);
994 error_report("unsupported bluetooth device '%s'", devname);
995 return 0;
998 static int bt_parse(const char *opt)
1000 const char *endp, *p;
1001 int vlan;
1003 if (strstart(opt, "hci", &endp)) {
1004 if (!*endp || *endp == ',') {
1005 if (*endp)
1006 if (!strstart(endp, ",vlan=", 0))
1007 opt = endp + 1;
1009 return bt_hci_parse(opt);
1011 } else if (strstart(opt, "vhci", &endp)) {
1012 if (!*endp || *endp == ',') {
1013 if (*endp) {
1014 if (strstart(endp, ",vlan=", &p)) {
1015 vlan = strtol(p, (char **) &endp, 0);
1016 if (*endp) {
1017 error_report("bad scatternet '%s'", p);
1018 return 1;
1020 } else {
1021 error_report("bad parameter '%s'", endp + 1);
1022 return 1;
1024 } else
1025 vlan = 0;
1027 bt_vhci_add(vlan);
1028 return 0;
1030 } else if (strstart(opt, "device:", &endp))
1031 return !bt_device_add(endp);
1033 error_report("bad bluetooth parameter '%s'", opt);
1034 return 1;
1037 static int parse_sandbox(void *opaque, QemuOpts *opts, Error **errp)
1039 /* FIXME: change this to true for 1.3 */
1040 if (qemu_opt_get_bool(opts, "enable", false)) {
1041 #ifdef CONFIG_SECCOMP
1042 if (seccomp_start() < 0) {
1043 error_report("failed to install seccomp syscall filter "
1044 "in the kernel");
1045 return -1;
1047 #else
1048 error_report("seccomp support is disabled");
1049 return -1;
1050 #endif
1053 return 0;
1056 static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
1058 const char *proc_name;
1060 if (qemu_opt_get(opts, "debug-threads")) {
1061 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
1063 qemu_name = qemu_opt_get(opts, "guest");
1065 proc_name = qemu_opt_get(opts, "process");
1066 if (proc_name) {
1067 os_set_proc_name(proc_name);
1070 return 0;
1073 bool defaults_enabled(void)
1075 return has_defaults;
1078 bool usb_enabled(void)
1080 return machine_usb(current_machine);
1083 #ifndef _WIN32
1084 static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1086 int fd, dupfd, flags;
1087 int64_t fdset_id;
1088 const char *fd_opaque = NULL;
1089 AddfdInfo *fdinfo;
1091 fd = qemu_opt_get_number(opts, "fd", -1);
1092 fdset_id = qemu_opt_get_number(opts, "set", -1);
1093 fd_opaque = qemu_opt_get(opts, "opaque");
1095 if (fd < 0) {
1096 error_report("fd option is required and must be non-negative");
1097 return -1;
1100 if (fd <= STDERR_FILENO) {
1101 error_report("fd cannot be a standard I/O stream");
1102 return -1;
1106 * All fds inherited across exec() necessarily have FD_CLOEXEC
1107 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1109 flags = fcntl(fd, F_GETFD);
1110 if (flags == -1 || (flags & FD_CLOEXEC)) {
1111 error_report("fd is not valid or already in use");
1112 return -1;
1115 if (fdset_id < 0) {
1116 error_report("set option is required and must be non-negative");
1117 return -1;
1120 #ifdef F_DUPFD_CLOEXEC
1121 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1122 #else
1123 dupfd = dup(fd);
1124 if (dupfd != -1) {
1125 qemu_set_cloexec(dupfd);
1127 #endif
1128 if (dupfd == -1) {
1129 error_report("error duplicating fd: %s", strerror(errno));
1130 return -1;
1133 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1134 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
1135 &error_abort);
1136 g_free(fdinfo);
1138 return 0;
1141 static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1143 int fd;
1145 fd = qemu_opt_get_number(opts, "fd", -1);
1146 close(fd);
1148 return 0;
1150 #endif
1152 /***********************************************************/
1153 /* QEMU Block devices */
1155 #define HD_OPTS "media=disk"
1156 #define CDROM_OPTS "media=cdrom"
1157 #define FD_OPTS ""
1158 #define PFLASH_OPTS ""
1159 #define MTD_OPTS ""
1160 #define SD_OPTS ""
1162 static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
1164 BlockInterfaceType *block_default_type = opaque;
1166 return drive_new(opts, *block_default_type) == NULL;
1169 static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
1171 if (qemu_opt_get(opts, "snapshot") == NULL) {
1172 qemu_opt_set(opts, "snapshot", "on", &error_abort);
1174 return 0;
1177 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1178 int index, const char *optstr)
1180 QemuOpts *opts;
1181 DriveInfo *dinfo;
1183 if (!enable || drive_get_by_index(type, index)) {
1184 return;
1187 opts = drive_add(type, index, NULL, optstr);
1188 if (snapshot) {
1189 drive_enable_snapshot(NULL, opts, NULL);
1192 dinfo = drive_new(opts, type);
1193 if (!dinfo) {
1194 exit(1);
1196 dinfo->is_default = true;
1200 static QemuOptsList qemu_smp_opts = {
1201 .name = "smp-opts",
1202 .implied_opt_name = "cpus",
1203 .merge_lists = true,
1204 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
1205 .desc = {
1207 .name = "cpus",
1208 .type = QEMU_OPT_NUMBER,
1209 }, {
1210 .name = "sockets",
1211 .type = QEMU_OPT_NUMBER,
1212 }, {
1213 .name = "cores",
1214 .type = QEMU_OPT_NUMBER,
1215 }, {
1216 .name = "threads",
1217 .type = QEMU_OPT_NUMBER,
1218 }, {
1219 .name = "maxcpus",
1220 .type = QEMU_OPT_NUMBER,
1222 { /*End of list */ }
1226 static void smp_parse(QemuOpts *opts)
1228 if (opts) {
1230 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
1231 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
1232 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
1233 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
1235 /* compute missing values, prefer sockets over cores over threads */
1236 if (cpus == 0 || sockets == 0) {
1237 sockets = sockets > 0 ? sockets : 1;
1238 cores = cores > 0 ? cores : 1;
1239 threads = threads > 0 ? threads : 1;
1240 if (cpus == 0) {
1241 cpus = cores * threads * sockets;
1243 } else if (cores == 0) {
1244 threads = threads > 0 ? threads : 1;
1245 cores = cpus / (sockets * threads);
1246 } else if (threads == 0) {
1247 threads = cpus / (cores * sockets);
1248 } else if (sockets * cores * threads < cpus) {
1249 error_report("cpu topology: "
1250 "sockets (%u) * cores (%u) * threads (%u) < "
1251 "smp_cpus (%u)",
1252 sockets, cores, threads, cpus);
1253 exit(1);
1256 max_cpus = qemu_opt_get_number(opts, "maxcpus", cpus);
1257 if (sockets * cores * threads > max_cpus) {
1258 error_report("cpu topology: "
1259 "sockets (%u) * cores (%u) * threads (%u) > "
1260 "maxcpus (%u)",
1261 sockets, cores, threads, max_cpus);
1262 exit(1);
1265 smp_cpus = cpus;
1266 smp_cores = cores > 0 ? cores : 1;
1267 smp_threads = threads > 0 ? threads : 1;
1271 if (max_cpus == 0) {
1272 max_cpus = smp_cpus;
1275 if (max_cpus > MAX_CPUMASK_BITS) {
1276 error_report("unsupported number of maxcpus");
1277 exit(1);
1279 if (max_cpus < smp_cpus) {
1280 error_report("maxcpus must be equal to or greater than smp");
1281 exit(1);
1284 if (smp_cpus > 1 || smp_cores > 1 || smp_threads > 1) {
1285 Error *blocker = NULL;
1286 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
1287 replay_add_blocker(blocker);
1291 static void realtime_init(void)
1293 if (enable_mlock) {
1294 if (os_mlock() < 0) {
1295 error_report("locking memory failed");
1296 exit(1);
1302 static void configure_msg(QemuOpts *opts)
1304 enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
1307 /***********************************************************/
1308 /* Semihosting */
1310 typedef struct SemihostingConfig {
1311 bool enabled;
1312 SemihostingTarget target;
1313 const char **argv;
1314 int argc;
1315 const char *cmdline; /* concatenated argv */
1316 } SemihostingConfig;
1318 static SemihostingConfig semihosting;
1320 bool semihosting_enabled(void)
1322 return semihosting.enabled;
1325 SemihostingTarget semihosting_get_target(void)
1327 return semihosting.target;
1330 const char *semihosting_get_arg(int i)
1332 if (i >= semihosting.argc) {
1333 return NULL;
1335 return semihosting.argv[i];
1338 int semihosting_get_argc(void)
1340 return semihosting.argc;
1343 const char *semihosting_get_cmdline(void)
1345 if (semihosting.cmdline == NULL && semihosting.argc > 0) {
1346 semihosting.cmdline = g_strjoinv(" ", (gchar **)semihosting.argv);
1348 return semihosting.cmdline;
1351 static int add_semihosting_arg(void *opaque,
1352 const char *name, const char *val,
1353 Error **errp)
1355 SemihostingConfig *s = opaque;
1356 if (strcmp(name, "arg") == 0) {
1357 s->argc++;
1358 /* one extra element as g_strjoinv() expects NULL-terminated array */
1359 s->argv = g_realloc(s->argv, (s->argc + 1) * sizeof(void *));
1360 s->argv[s->argc - 1] = val;
1361 s->argv[s->argc] = NULL;
1363 return 0;
1366 /* Use strings passed via -kernel/-append to initialize semihosting.argv[] */
1367 static inline void semihosting_arg_fallback(const char *file, const char *cmd)
1369 char *cmd_token;
1371 /* argv[0] */
1372 add_semihosting_arg(&semihosting, "arg", file, NULL);
1374 /* split -append and initialize argv[1..n] */
1375 cmd_token = strtok(g_strdup(cmd), " ");
1376 while (cmd_token) {
1377 add_semihosting_arg(&semihosting, "arg", cmd_token, NULL);
1378 cmd_token = strtok(NULL, " ");
1382 /* Now we still need this for compatibility with XEN. */
1383 bool has_igd_gfx_passthru;
1384 static void igd_gfx_passthru(void)
1386 has_igd_gfx_passthru = current_machine->igd_gfx_passthru;
1389 /***********************************************************/
1390 /* USB devices */
1392 static int usb_device_add(const char *devname)
1394 USBDevice *dev = NULL;
1395 #ifndef CONFIG_LINUX
1396 const char *p;
1397 #endif
1399 if (!usb_enabled()) {
1400 return -1;
1403 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1404 dev = usbdevice_create(devname);
1405 if (dev)
1406 goto done;
1408 /* the other ones */
1409 #ifndef CONFIG_LINUX
1410 /* only the linux version is qdev-ified, usb-bsd still needs this */
1411 if (strstart(devname, "host:", &p)) {
1412 dev = usb_host_device_open(usb_bus_find(-1), p);
1414 #endif
1415 if (!dev)
1416 return -1;
1418 done:
1419 return 0;
1422 static int usb_device_del(const char *devname)
1424 int bus_num, addr;
1425 const char *p;
1427 if (strstart(devname, "host:", &p)) {
1428 return -1;
1431 if (!usb_enabled()) {
1432 return -1;
1435 p = strchr(devname, '.');
1436 if (!p)
1437 return -1;
1438 bus_num = strtoul(devname, NULL, 0);
1439 addr = strtoul(p + 1, NULL, 0);
1441 return usb_device_delete_addr(bus_num, addr);
1444 static int usb_parse(const char *cmdline)
1446 int r;
1447 r = usb_device_add(cmdline);
1448 if (r < 0) {
1449 error_report("could not add USB device '%s'", cmdline);
1451 return r;
1454 void hmp_usb_add(Monitor *mon, const QDict *qdict)
1456 const char *devname = qdict_get_str(qdict, "devname");
1457 if (usb_device_add(devname) < 0) {
1458 error_report("could not add USB device '%s'", devname);
1462 void hmp_usb_del(Monitor *mon, const QDict *qdict)
1464 const char *devname = qdict_get_str(qdict, "devname");
1465 if (usb_device_del(devname) < 0) {
1466 error_report("could not delete USB device '%s'", devname);
1470 /***********************************************************/
1471 /* machine registration */
1473 MachineState *current_machine;
1475 static MachineClass *find_machine(const char *name)
1477 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1478 MachineClass *mc = NULL;
1480 for (el = machines; el; el = el->next) {
1481 MachineClass *temp = el->data;
1483 if (!strcmp(temp->name, name)) {
1484 mc = temp;
1485 break;
1487 if (temp->alias &&
1488 !strcmp(temp->alias, name)) {
1489 mc = temp;
1490 break;
1494 g_slist_free(machines);
1495 return mc;
1498 MachineClass *find_default_machine(void)
1500 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1501 MachineClass *mc = NULL;
1503 for (el = machines; el; el = el->next) {
1504 MachineClass *temp = el->data;
1506 if (temp->is_default) {
1507 mc = temp;
1508 break;
1512 g_slist_free(machines);
1513 return mc;
1516 MachineInfoList *qmp_query_machines(Error **errp)
1518 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1519 MachineInfoList *mach_list = NULL;
1521 for (el = machines; el; el = el->next) {
1522 MachineClass *mc = el->data;
1523 MachineInfoList *entry;
1524 MachineInfo *info;
1526 info = g_malloc0(sizeof(*info));
1527 if (mc->is_default) {
1528 info->has_is_default = true;
1529 info->is_default = true;
1532 if (mc->alias) {
1533 info->has_alias = true;
1534 info->alias = g_strdup(mc->alias);
1537 info->name = g_strdup(mc->name);
1538 info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
1540 entry = g_malloc0(sizeof(*entry));
1541 entry->value = info;
1542 entry->next = mach_list;
1543 mach_list = entry;
1546 g_slist_free(machines);
1547 return mach_list;
1550 static int machine_help_func(QemuOpts *opts, MachineState *machine)
1552 ObjectProperty *prop;
1553 ObjectPropertyIterator iter;
1555 if (!qemu_opt_has_help_opt(opts)) {
1556 return 0;
1559 object_property_iter_init(&iter, OBJECT(machine));
1560 while ((prop = object_property_iter_next(&iter))) {
1561 if (!prop->set) {
1562 continue;
1565 error_printf("%s.%s=%s", MACHINE_GET_CLASS(machine)->name,
1566 prop->name, prop->type);
1567 if (prop->description) {
1568 error_printf(" (%s)\n", prop->description);
1569 } else {
1570 error_printf("\n");
1574 return 1;
1577 /***********************************************************/
1578 /* main execution loop */
1580 struct vm_change_state_entry {
1581 VMChangeStateHandler *cb;
1582 void *opaque;
1583 QLIST_ENTRY (vm_change_state_entry) entries;
1586 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1588 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1589 void *opaque)
1591 VMChangeStateEntry *e;
1593 e = g_malloc0(sizeof (*e));
1595 e->cb = cb;
1596 e->opaque = opaque;
1597 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1598 return e;
1601 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1603 QLIST_REMOVE (e, entries);
1604 g_free (e);
1607 void vm_state_notify(int running, RunState state)
1609 VMChangeStateEntry *e, *next;
1611 trace_vm_state_notify(running, state);
1613 QLIST_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
1614 e->cb(e->opaque, running, state);
1618 /* reset/shutdown handler */
1620 typedef struct QEMUResetEntry {
1621 QTAILQ_ENTRY(QEMUResetEntry) entry;
1622 QEMUResetHandler *func;
1623 void *opaque;
1624 } QEMUResetEntry;
1626 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1627 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1628 static int reset_requested;
1629 static int shutdown_requested, shutdown_signal = -1;
1630 static pid_t shutdown_pid;
1631 static int powerdown_requested;
1632 static int debug_requested;
1633 static int suspend_requested;
1634 static WakeupReason wakeup_reason;
1635 static NotifierList powerdown_notifiers =
1636 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
1637 static NotifierList suspend_notifiers =
1638 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1639 static NotifierList wakeup_notifiers =
1640 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1641 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
1643 int qemu_shutdown_requested_get(void)
1645 return shutdown_requested;
1648 int qemu_reset_requested_get(void)
1650 return reset_requested;
1653 static int qemu_shutdown_requested(void)
1655 return atomic_xchg(&shutdown_requested, 0);
1658 static void qemu_kill_report(void)
1660 if (!qtest_driver() && shutdown_signal != -1) {
1661 if (shutdown_pid == 0) {
1662 /* This happens for eg ^C at the terminal, so it's worth
1663 * avoiding printing an odd message in that case.
1665 error_report("terminating on signal %d", shutdown_signal);
1666 } else {
1667 error_report("terminating on signal %d from pid " FMT_pid,
1668 shutdown_signal, shutdown_pid);
1670 shutdown_signal = -1;
1674 static int qemu_reset_requested(void)
1676 int r = reset_requested;
1677 if (r && replay_checkpoint(CHECKPOINT_RESET_REQUESTED)) {
1678 reset_requested = 0;
1679 return r;
1681 return false;
1684 static int qemu_suspend_requested(void)
1686 int r = suspend_requested;
1687 if (r && replay_checkpoint(CHECKPOINT_SUSPEND_REQUESTED)) {
1688 suspend_requested = 0;
1689 return r;
1691 return false;
1694 static WakeupReason qemu_wakeup_requested(void)
1696 return wakeup_reason;
1699 static int qemu_powerdown_requested(void)
1701 int r = powerdown_requested;
1702 powerdown_requested = 0;
1703 return r;
1706 static int qemu_debug_requested(void)
1708 int r = debug_requested;
1709 debug_requested = 0;
1710 return r;
1713 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1715 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
1717 re->func = func;
1718 re->opaque = opaque;
1719 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1722 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1724 QEMUResetEntry *re;
1726 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1727 if (re->func == func && re->opaque == opaque) {
1728 QTAILQ_REMOVE(&reset_handlers, re, entry);
1729 g_free(re);
1730 return;
1735 void qemu_devices_reset(void)
1737 QEMUResetEntry *re, *nre;
1739 /* reset all devices */
1740 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1741 re->func(re->opaque);
1745 void qemu_system_reset(bool report)
1747 MachineClass *mc;
1749 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1751 cpu_synchronize_all_states();
1753 if (mc && mc->reset) {
1754 mc->reset();
1755 } else {
1756 qemu_devices_reset();
1758 if (report) {
1759 qapi_event_send_reset(&error_abort);
1761 cpu_synchronize_all_post_reset();
1764 void qemu_system_guest_panicked(void)
1766 if (current_cpu) {
1767 current_cpu->crash_occurred = true;
1769 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE, &error_abort);
1770 vm_stop(RUN_STATE_GUEST_PANICKED);
1773 void qemu_system_reset_request(void)
1775 if (no_reboot) {
1776 shutdown_requested = 1;
1777 } else {
1778 reset_requested = 1;
1780 cpu_stop_current();
1781 qemu_notify_event();
1784 static void qemu_system_suspend(void)
1786 pause_all_vcpus();
1787 notifier_list_notify(&suspend_notifiers, NULL);
1788 runstate_set(RUN_STATE_SUSPENDED);
1789 qapi_event_send_suspend(&error_abort);
1792 void qemu_system_suspend_request(void)
1794 if (runstate_check(RUN_STATE_SUSPENDED)) {
1795 return;
1797 suspend_requested = 1;
1798 cpu_stop_current();
1799 qemu_notify_event();
1802 void qemu_register_suspend_notifier(Notifier *notifier)
1804 notifier_list_add(&suspend_notifiers, notifier);
1807 void qemu_system_wakeup_request(WakeupReason reason)
1809 trace_system_wakeup_request(reason);
1811 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1812 return;
1814 if (!(wakeup_reason_mask & (1 << reason))) {
1815 return;
1817 runstate_set(RUN_STATE_RUNNING);
1818 wakeup_reason = reason;
1819 qemu_notify_event();
1822 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1824 if (enabled) {
1825 wakeup_reason_mask |= (1 << reason);
1826 } else {
1827 wakeup_reason_mask &= ~(1 << reason);
1831 void qemu_register_wakeup_notifier(Notifier *notifier)
1833 notifier_list_add(&wakeup_notifiers, notifier);
1836 void qemu_system_killed(int signal, pid_t pid)
1838 shutdown_signal = signal;
1839 shutdown_pid = pid;
1840 no_shutdown = 0;
1842 /* Cannot call qemu_system_shutdown_request directly because
1843 * we are in a signal handler.
1845 shutdown_requested = 1;
1846 qemu_notify_event();
1849 void qemu_system_shutdown_request(void)
1851 trace_qemu_system_shutdown_request();
1852 replay_shutdown_request();
1853 shutdown_requested = 1;
1854 qemu_notify_event();
1857 static void qemu_system_powerdown(void)
1859 qapi_event_send_powerdown(&error_abort);
1860 notifier_list_notify(&powerdown_notifiers, NULL);
1863 void qemu_system_powerdown_request(void)
1865 trace_qemu_system_powerdown_request();
1866 powerdown_requested = 1;
1867 qemu_notify_event();
1870 void qemu_register_powerdown_notifier(Notifier *notifier)
1872 notifier_list_add(&powerdown_notifiers, notifier);
1875 void qemu_system_debug_request(void)
1877 debug_requested = 1;
1878 qemu_notify_event();
1881 static bool main_loop_should_exit(void)
1883 RunState r;
1884 if (qemu_debug_requested()) {
1885 vm_stop(RUN_STATE_DEBUG);
1887 if (qemu_suspend_requested()) {
1888 qemu_system_suspend();
1890 if (qemu_shutdown_requested()) {
1891 qemu_kill_report();
1892 qapi_event_send_shutdown(&error_abort);
1893 if (no_shutdown) {
1894 vm_stop(RUN_STATE_SHUTDOWN);
1895 } else {
1896 return true;
1899 if (qemu_reset_requested()) {
1900 pause_all_vcpus();
1901 qemu_system_reset(VMRESET_REPORT);
1902 resume_all_vcpus();
1903 if (!runstate_check(RUN_STATE_RUNNING) &&
1904 !runstate_check(RUN_STATE_INMIGRATE)) {
1905 runstate_set(RUN_STATE_PRELAUNCH);
1908 if (qemu_wakeup_requested()) {
1909 pause_all_vcpus();
1910 qemu_system_reset(VMRESET_SILENT);
1911 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
1912 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
1913 resume_all_vcpus();
1914 qapi_event_send_wakeup(&error_abort);
1916 if (qemu_powerdown_requested()) {
1917 qemu_system_powerdown();
1919 if (qemu_vmstop_requested(&r)) {
1920 vm_stop(r);
1922 return false;
1925 static void main_loop(void)
1927 bool nonblocking;
1928 int last_io = 0;
1929 #ifdef CONFIG_PROFILER
1930 int64_t ti;
1931 #endif
1932 do {
1933 nonblocking = !kvm_enabled() && !xen_enabled() && last_io > 0;
1934 #ifdef CONFIG_PROFILER
1935 ti = profile_getclock();
1936 #endif
1937 last_io = main_loop_wait(nonblocking);
1938 #ifdef CONFIG_PROFILER
1939 dev_time += profile_getclock() - ti;
1940 #endif
1941 } while (!main_loop_should_exit());
1944 static void version(void)
1946 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1949 static void QEMU_NORETURN help(int exitcode)
1951 version();
1952 printf("usage: %s [options] [disk_image]\n\n"
1953 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1954 error_get_progname());
1956 #define QEMU_OPTIONS_GENERATE_HELP
1957 #include "qemu-options-wrapper.h"
1959 printf("\nDuring emulation, the following keys are useful:\n"
1960 "ctrl-alt-f toggle full screen\n"
1961 "ctrl-alt-n switch to virtual console 'n'\n"
1962 "ctrl-alt toggle mouse and keyboard grab\n"
1963 "\n"
1964 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1966 exit(exitcode);
1969 #define HAS_ARG 0x0001
1971 typedef struct QEMUOption {
1972 const char *name;
1973 int flags;
1974 int index;
1975 uint32_t arch_mask;
1976 } QEMUOption;
1978 static const QEMUOption qemu_options[] = {
1979 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1980 #define QEMU_OPTIONS_GENERATE_OPTIONS
1981 #include "qemu-options-wrapper.h"
1982 { NULL },
1985 static bool vga_available(void)
1987 return object_class_by_name("VGA") || object_class_by_name("isa-vga");
1990 static bool cirrus_vga_available(void)
1992 return object_class_by_name("cirrus-vga")
1993 || object_class_by_name("isa-cirrus-vga");
1996 static bool vmware_vga_available(void)
1998 return object_class_by_name("vmware-svga");
2001 static bool qxl_vga_available(void)
2003 return object_class_by_name("qxl-vga");
2006 static bool tcx_vga_available(void)
2008 return object_class_by_name("SUNW,tcx");
2011 static bool cg3_vga_available(void)
2013 return object_class_by_name("cgthree");
2016 static bool virtio_vga_available(void)
2018 return object_class_by_name("virtio-vga");
2021 static void select_vgahw (const char *p)
2023 const char *opts;
2025 assert(vga_interface_type == VGA_NONE);
2026 if (strstart(p, "std", &opts)) {
2027 if (vga_available()) {
2028 vga_interface_type = VGA_STD;
2029 } else {
2030 error_report("standard VGA not available");
2031 exit(0);
2033 } else if (strstart(p, "cirrus", &opts)) {
2034 if (cirrus_vga_available()) {
2035 vga_interface_type = VGA_CIRRUS;
2036 } else {
2037 error_report("Cirrus VGA not available");
2038 exit(0);
2040 } else if (strstart(p, "vmware", &opts)) {
2041 if (vmware_vga_available()) {
2042 vga_interface_type = VGA_VMWARE;
2043 } else {
2044 error_report("VMWare SVGA not available");
2045 exit(0);
2047 } else if (strstart(p, "virtio", &opts)) {
2048 if (virtio_vga_available()) {
2049 vga_interface_type = VGA_VIRTIO;
2050 } else {
2051 error_report("Virtio VGA not available");
2052 exit(0);
2054 } else if (strstart(p, "xenfb", &opts)) {
2055 vga_interface_type = VGA_XENFB;
2056 } else if (strstart(p, "qxl", &opts)) {
2057 if (qxl_vga_available()) {
2058 vga_interface_type = VGA_QXL;
2059 } else {
2060 error_report("QXL VGA not available");
2061 exit(0);
2063 } else if (strstart(p, "tcx", &opts)) {
2064 if (tcx_vga_available()) {
2065 vga_interface_type = VGA_TCX;
2066 } else {
2067 error_report("TCX framebuffer not available");
2068 exit(0);
2070 } else if (strstart(p, "cg3", &opts)) {
2071 if (cg3_vga_available()) {
2072 vga_interface_type = VGA_CG3;
2073 } else {
2074 error_report("CG3 framebuffer not available");
2075 exit(0);
2077 } else if (!strstart(p, "none", &opts)) {
2078 invalid_vga:
2079 error_report("unknown vga type: %s", p);
2080 exit(1);
2082 while (*opts) {
2083 const char *nextopt;
2085 if (strstart(opts, ",retrace=", &nextopt)) {
2086 opts = nextopt;
2087 if (strstart(opts, "dumb", &nextopt))
2088 vga_retrace_method = VGA_RETRACE_DUMB;
2089 else if (strstart(opts, "precise", &nextopt))
2090 vga_retrace_method = VGA_RETRACE_PRECISE;
2091 else goto invalid_vga;
2092 } else goto invalid_vga;
2093 opts = nextopt;
2097 static DisplayType select_display(const char *p)
2099 const char *opts;
2100 DisplayType display = DT_DEFAULT;
2102 if (strstart(p, "sdl", &opts)) {
2103 #ifdef CONFIG_SDL
2104 display = DT_SDL;
2105 while (*opts) {
2106 const char *nextopt;
2108 if (strstart(opts, ",frame=", &nextopt)) {
2109 opts = nextopt;
2110 if (strstart(opts, "on", &nextopt)) {
2111 no_frame = 0;
2112 } else if (strstart(opts, "off", &nextopt)) {
2113 no_frame = 1;
2114 } else {
2115 goto invalid_sdl_args;
2117 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
2118 opts = nextopt;
2119 if (strstart(opts, "on", &nextopt)) {
2120 alt_grab = 1;
2121 } else if (strstart(opts, "off", &nextopt)) {
2122 alt_grab = 0;
2123 } else {
2124 goto invalid_sdl_args;
2126 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2127 opts = nextopt;
2128 if (strstart(opts, "on", &nextopt)) {
2129 ctrl_grab = 1;
2130 } else if (strstart(opts, "off", &nextopt)) {
2131 ctrl_grab = 0;
2132 } else {
2133 goto invalid_sdl_args;
2135 } else if (strstart(opts, ",window_close=", &nextopt)) {
2136 opts = nextopt;
2137 if (strstart(opts, "on", &nextopt)) {
2138 no_quit = 0;
2139 } else if (strstart(opts, "off", &nextopt)) {
2140 no_quit = 1;
2141 } else {
2142 goto invalid_sdl_args;
2144 } else if (strstart(opts, ",gl=", &nextopt)) {
2145 opts = nextopt;
2146 if (strstart(opts, "on", &nextopt)) {
2147 request_opengl = 1;
2148 } else if (strstart(opts, "off", &nextopt)) {
2149 request_opengl = 0;
2150 } else {
2151 goto invalid_sdl_args;
2153 } else {
2154 invalid_sdl_args:
2155 error_report("invalid SDL option string");
2156 exit(1);
2158 opts = nextopt;
2160 #else
2161 error_report("SDL support is disabled");
2162 exit(1);
2163 #endif
2164 } else if (strstart(p, "vnc", &opts)) {
2165 #ifdef CONFIG_VNC
2166 if (*opts == '=') {
2167 Error *err = NULL;
2168 if (vnc_parse(opts + 1, &err) == NULL) {
2169 error_report_err(err);
2170 exit(1);
2172 } else {
2173 error_report("VNC requires a display argument vnc=<display>");
2174 exit(1);
2176 #else
2177 error_report("VNC support is disabled");
2178 exit(1);
2179 #endif
2180 } else if (strstart(p, "curses", &opts)) {
2181 #ifdef CONFIG_CURSES
2182 display = DT_CURSES;
2183 #else
2184 error_report("curses support is disabled");
2185 exit(1);
2186 #endif
2187 } else if (strstart(p, "gtk", &opts)) {
2188 #ifdef CONFIG_GTK
2189 display = DT_GTK;
2190 while (*opts) {
2191 const char *nextopt;
2193 if (strstart(opts, ",grab_on_hover=", &nextopt)) {
2194 opts = nextopt;
2195 if (strstart(opts, "on", &nextopt)) {
2196 grab_on_hover = true;
2197 } else if (strstart(opts, "off", &nextopt)) {
2198 grab_on_hover = false;
2199 } else {
2200 goto invalid_gtk_args;
2202 } else if (strstart(opts, ",gl=", &nextopt)) {
2203 opts = nextopt;
2204 if (strstart(opts, "on", &nextopt)) {
2205 request_opengl = 1;
2206 } else if (strstart(opts, "off", &nextopt)) {
2207 request_opengl = 0;
2208 } else {
2209 goto invalid_gtk_args;
2211 } else {
2212 invalid_gtk_args:
2213 error_report("invalid GTK option string");
2214 exit(1);
2216 opts = nextopt;
2218 #else
2219 error_report("GTK support is disabled");
2220 exit(1);
2221 #endif
2222 } else if (strstart(p, "none", &opts)) {
2223 display = DT_NONE;
2224 } else {
2225 error_report("unknown display type");
2226 exit(1);
2229 return display;
2232 static int balloon_parse(const char *arg)
2234 QemuOpts *opts;
2236 if (strcmp(arg, "none") == 0) {
2237 return 0;
2240 if (!strncmp(arg, "virtio", 6)) {
2241 if (arg[6] == ',') {
2242 /* have params -> parse them */
2243 opts = qemu_opts_parse_noisily(qemu_find_opts("device"), arg + 7,
2244 false);
2245 if (!opts)
2246 return -1;
2247 } else {
2248 /* create empty opts */
2249 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2250 &error_abort);
2252 qemu_opt_set(opts, "driver", "virtio-balloon", &error_abort);
2253 return 0;
2256 return -1;
2259 #if defined(CONFIG_RUBY)
2260 /* Ruby interface for QEMU. See README.EXT for programming example. */
2261 #warning mit ruby
2262 #include <ruby.h>
2264 static VALUE qemu_open(int argc, VALUE *argv, VALUE klass)
2266 int i;
2267 for (i = 0; i < argc; i++) {
2268 argv[i] = StringValue(argv[i]);
2269 fprintf(stderr, "argv[%d] = %s\n", i, StringValuePtr(argv[i]));
2271 //~ if (rb_scan_args(argc, argv, "11", &file, &vmode) == 1) {
2272 //~ mode = 0666; /* default value */
2273 //~ }
2274 return INT2FIX(argc);
2277 void Init_qemu(void)
2279 printf("%s\n", __func__);
2280 VALUE cQEMU = rb_define_class("QEMU", rb_cObject);
2281 rb_define_singleton_method(cQEMU, "run", qemu_open, -1);
2282 #if 0
2283 rb_define_method();
2284 #endif
2286 #endif /* CONFIG_RUBY */
2288 #if defined(CONFIG_DLL)
2289 BOOL APIENTRY DllMain(HINSTANCE hModule, DWORD ul_reason_for_call, LPVOID data)
2291 return FALSE;
2293 #endif /* CONFIG_DLL */
2295 char *qemu_find_file(int type, const char *name)
2297 int i;
2298 const char *subdir;
2299 char *buf;
2301 /* Try the name as a straight path first */
2302 if (access(name, R_OK) == 0) {
2303 trace_load_file(name, name);
2304 return g_strdup(name);
2307 switch (type) {
2308 case QEMU_FILE_TYPE_BIOS:
2309 subdir = "";
2310 break;
2311 case QEMU_FILE_TYPE_KEYMAP:
2312 subdir = "keymaps/";
2313 break;
2314 default:
2315 abort();
2318 for (i = 0; i < data_dir_idx; i++) {
2319 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2320 if (access(buf, R_OK) == 0) {
2321 trace_load_file(name, buf);
2322 return buf;
2324 g_free(buf);
2326 return NULL;
2329 static inline bool nonempty_str(const char *str)
2331 return str && *str;
2334 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
2336 gchar *buf;
2337 size_t size;
2338 const char *name, *file, *str;
2339 FWCfgState *fw_cfg = (FWCfgState *) opaque;
2341 if (fw_cfg == NULL) {
2342 error_report("fw_cfg device not available");
2343 return -1;
2345 name = qemu_opt_get(opts, "name");
2346 file = qemu_opt_get(opts, "file");
2347 str = qemu_opt_get(opts, "string");
2349 /* we need name and either a file or the content string */
2350 if (!(nonempty_str(name) && (nonempty_str(file) || nonempty_str(str)))) {
2351 error_report("invalid argument(s)");
2352 return -1;
2354 if (nonempty_str(file) && nonempty_str(str)) {
2355 error_report("file and string are mutually exclusive");
2356 return -1;
2358 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
2359 error_report("name too long (max. %d char)", FW_CFG_MAX_FILE_PATH - 1);
2360 return -1;
2362 if (strncmp(name, "opt/", 4) != 0) {
2363 error_report("warning: externally provided fw_cfg item names "
2364 "should be prefixed with \"opt/\"");
2366 if (nonempty_str(str)) {
2367 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
2368 buf = g_memdup(str, size);
2369 } else {
2370 if (!g_file_get_contents(file, &buf, &size, NULL)) {
2371 error_report("can't load %s", file);
2372 return -1;
2375 /* For legacy, keep user files in a specific global order. */
2376 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
2377 fw_cfg_add_file(fw_cfg, name, buf, size);
2378 fw_cfg_reset_order_override(fw_cfg);
2379 return 0;
2382 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
2384 return qdev_device_help(opts);
2387 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
2389 Error *err = NULL;
2390 DeviceState *dev;
2392 dev = qdev_device_add(opts, &err);
2393 if (!dev) {
2394 error_report_err(err);
2395 return -1;
2397 object_unref(OBJECT(dev));
2398 return 0;
2401 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2403 Error *local_err = NULL;
2405 qemu_chr_new_from_opts(opts, NULL, &local_err);
2406 if (local_err) {
2407 error_report_err(local_err);
2408 return -1;
2410 return 0;
2413 #ifdef CONFIG_VIRTFS
2414 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2416 int ret;
2417 ret = qemu_fsdev_add(opts);
2419 return ret;
2421 #endif
2423 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
2425 CharDriverState *chr;
2426 const char *chardev;
2427 const char *mode;
2428 int flags;
2430 mode = qemu_opt_get(opts, "mode");
2431 if (mode == NULL) {
2432 mode = "readline";
2434 if (strcmp(mode, "readline") == 0) {
2435 flags = MONITOR_USE_READLINE;
2436 } else if (strcmp(mode, "control") == 0) {
2437 flags = MONITOR_USE_CONTROL;
2438 } else {
2439 error_report("unknown monitor mode \"%s\"", mode);
2440 exit(1);
2443 if (qemu_opt_get_bool(opts, "pretty", 0))
2444 flags |= MONITOR_USE_PRETTY;
2446 if (qemu_opt_get_bool(opts, "default", 0))
2447 flags |= MONITOR_IS_DEFAULT;
2449 chardev = qemu_opt_get(opts, "chardev");
2450 chr = qemu_chr_find(chardev);
2451 if (chr == NULL) {
2452 error_report("chardev \"%s\" not found", chardev);
2453 exit(1);
2456 qemu_chr_fe_claim_no_fail(chr);
2457 monitor_init(chr, flags);
2458 return 0;
2461 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
2463 static int monitor_device_index = 0;
2464 Error *local_err = NULL;
2465 QemuOpts *opts;
2466 const char *p;
2467 char label[32];
2468 int def = 0;
2470 if (strstart(optarg, "chardev:", &p)) {
2471 snprintf(label, sizeof(label), "%s", p);
2472 } else {
2473 snprintf(label, sizeof(label), "compat_monitor%d",
2474 monitor_device_index);
2475 if (monitor_device_index == 0) {
2476 def = 1;
2478 opts = qemu_chr_parse_compat(label, optarg);
2479 if (!opts) {
2480 error_report("parse error: %s", optarg);
2481 exit(1);
2485 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &local_err);
2486 if (!opts) {
2487 error_report_err(local_err);
2488 exit(1);
2490 qemu_opt_set(opts, "mode", mode, &error_abort);
2491 qemu_opt_set(opts, "chardev", label, &error_abort);
2492 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
2493 if (def)
2494 qemu_opt_set(opts, "default", "on", &error_abort);
2495 monitor_device_index++;
2498 struct device_config {
2499 enum {
2500 DEV_USB, /* -usbdevice */
2501 DEV_BT, /* -bt */
2502 DEV_SERIAL, /* -serial */
2503 DEV_PARALLEL, /* -parallel */
2504 DEV_VIRTCON, /* -virtioconsole */
2505 DEV_DEBUGCON, /* -debugcon */
2506 DEV_GDB, /* -gdb, -s */
2507 DEV_SCLP, /* s390 sclp */
2508 } type;
2509 const char *cmdline;
2510 Location loc;
2511 QTAILQ_ENTRY(device_config) next;
2514 static QTAILQ_HEAD(, device_config) device_configs =
2515 QTAILQ_HEAD_INITIALIZER(device_configs);
2517 static void add_device_config(int type, const char *cmdline)
2519 struct device_config *conf;
2521 conf = g_malloc0(sizeof(*conf));
2522 conf->type = type;
2523 conf->cmdline = cmdline;
2524 loc_save(&conf->loc);
2525 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2528 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2530 struct device_config *conf;
2531 int rc;
2533 QTAILQ_FOREACH(conf, &device_configs, next) {
2534 if (conf->type != type)
2535 continue;
2536 loc_push_restore(&conf->loc);
2537 rc = func(conf->cmdline);
2538 loc_pop(&conf->loc);
2539 if (rc) {
2540 return rc;
2543 return 0;
2546 static int serial_parse(const char *devname)
2548 static int index = 0;
2549 char label[32];
2551 if (strcmp(devname, "none") == 0)
2552 return 0;
2553 if (index == MAX_SERIAL_PORTS) {
2554 error_report("too many serial ports");
2555 exit(1);
2557 snprintf(label, sizeof(label), "serial%d", index);
2558 serial_hds[index] = qemu_chr_new(label, devname, NULL);
2559 if (!serial_hds[index]) {
2560 error_report("could not connect serial device"
2561 " to character backend '%s'", devname);
2562 return -1;
2564 index++;
2565 return 0;
2568 static int parallel_parse(const char *devname)
2570 static int index = 0;
2571 char label[32];
2573 if (strcmp(devname, "none") == 0)
2574 return 0;
2575 if (index == MAX_PARALLEL_PORTS) {
2576 error_report("too many parallel ports");
2577 exit(1);
2579 snprintf(label, sizeof(label), "parallel%d", index);
2580 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
2581 if (!parallel_hds[index]) {
2582 error_report("could not connect parallel device"
2583 " to character backend '%s'", devname);
2584 return -1;
2586 index++;
2587 return 0;
2590 static int virtcon_parse(const char *devname)
2592 QemuOptsList *device = qemu_find_opts("device");
2593 static int index = 0;
2594 char label[32];
2595 QemuOpts *bus_opts, *dev_opts;
2597 if (strcmp(devname, "none") == 0)
2598 return 0;
2599 if (index == MAX_VIRTIO_CONSOLES) {
2600 error_report("too many virtio consoles");
2601 exit(1);
2604 bus_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2605 qemu_opt_set(bus_opts, "driver", "virtio-serial", &error_abort);
2607 dev_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2608 qemu_opt_set(dev_opts, "driver", "virtconsole", &error_abort);
2610 snprintf(label, sizeof(label), "virtcon%d", index);
2611 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
2612 if (!virtcon_hds[index]) {
2613 error_report("could not connect virtio console"
2614 " to character backend '%s'", devname);
2615 return -1;
2617 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2619 index++;
2620 return 0;
2623 static int sclp_parse(const char *devname)
2625 QemuOptsList *device = qemu_find_opts("device");
2626 static int index = 0;
2627 char label[32];
2628 QemuOpts *dev_opts;
2630 if (strcmp(devname, "none") == 0) {
2631 return 0;
2633 if (index == MAX_SCLP_CONSOLES) {
2634 error_report("too many sclp consoles");
2635 exit(1);
2638 assert(arch_type == QEMU_ARCH_S390X);
2640 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2641 qemu_opt_set(dev_opts, "driver", "sclpconsole", &error_abort);
2643 snprintf(label, sizeof(label), "sclpcon%d", index);
2644 sclp_hds[index] = qemu_chr_new(label, devname, NULL);
2645 if (!sclp_hds[index]) {
2646 error_report("could not connect sclp console"
2647 " to character backend '%s'", devname);
2648 return -1;
2650 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2652 index++;
2653 return 0;
2656 static int debugcon_parse(const char *devname)
2658 QemuOpts *opts;
2660 if (!qemu_chr_new("debugcon", devname, NULL)) {
2661 exit(1);
2663 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2664 if (!opts) {
2665 error_report("already have a debugcon device");
2666 exit(1);
2668 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
2669 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
2670 return 0;
2673 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
2675 const MachineClass *mc1 = a, *mc2 = b;
2676 int res;
2678 if (mc1->family == NULL) {
2679 if (mc2->family == NULL) {
2680 /* Compare standalone machine types against each other; they sort
2681 * in increasing order.
2683 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
2684 object_class_get_name(OBJECT_CLASS(mc2)));
2687 /* Standalone machine types sort after families. */
2688 return 1;
2691 if (mc2->family == NULL) {
2692 /* Families sort before standalone machine types. */
2693 return -1;
2696 /* Families sort between each other alphabetically increasingly. */
2697 res = strcmp(mc1->family, mc2->family);
2698 if (res != 0) {
2699 return res;
2702 /* Within the same family, machine types sort in decreasing order. */
2703 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
2704 object_class_get_name(OBJECT_CLASS(mc1)));
2707 static MachineClass *machine_parse(const char *name)
2709 MachineClass *mc = NULL;
2710 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
2712 if (name) {
2713 mc = find_machine(name);
2715 if (mc) {
2716 g_slist_free(machines);
2717 return mc;
2719 if (name && !is_help_option(name)) {
2720 error_report("unsupported machine type");
2721 error_printf("Use -machine help to list supported machines\n");
2722 } else {
2723 printf("Supported machines are:\n");
2724 machines = g_slist_sort(machines, machine_class_cmp);
2725 for (el = machines; el; el = el->next) {
2726 MachineClass *mc = el->data;
2727 if (mc->alias) {
2728 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
2730 printf("%-20s %s%s\n", mc->name, mc->desc,
2731 mc->is_default ? " (default)" : "");
2735 g_slist_free(machines);
2736 exit(!name || !is_help_option(name));
2739 void qemu_add_exit_notifier(Notifier *notify)
2741 notifier_list_add(&exit_notifiers, notify);
2744 void qemu_remove_exit_notifier(Notifier *notify)
2746 notifier_remove(notify);
2749 static void qemu_run_exit_notifiers(void)
2751 notifier_list_notify(&exit_notifiers, NULL);
2754 static bool machine_init_done;
2756 void qemu_add_machine_init_done_notifier(Notifier *notify)
2758 notifier_list_add(&machine_init_done_notifiers, notify);
2759 if (machine_init_done) {
2760 notify->notify(notify, NULL);
2764 static void qemu_run_machine_init_done_notifiers(void)
2766 notifier_list_notify(&machine_init_done_notifiers, NULL);
2767 machine_init_done = true;
2770 static const QEMUOption *lookup_opt(int argc, char **argv,
2771 const char **poptarg, int *poptind)
2773 const QEMUOption *popt;
2774 int optind = *poptind;
2775 char *r = argv[optind];
2776 const char *optarg;
2778 loc_set_cmdline(argv, optind, 1);
2779 optind++;
2780 /* Treat --foo the same as -foo. */
2781 if (r[1] == '-')
2782 r++;
2783 popt = qemu_options;
2784 for(;;) {
2785 if (!popt->name) {
2786 error_report("invalid option");
2787 exit(1);
2789 if (!strcmp(popt->name, r + 1))
2790 break;
2791 popt++;
2793 if (popt->flags & HAS_ARG) {
2794 if (optind >= argc) {
2795 error_report("requires an argument");
2796 exit(1);
2798 optarg = argv[optind++];
2799 loc_set_cmdline(argv, optind - 2, 2);
2800 } else {
2801 optarg = NULL;
2804 *poptarg = optarg;
2805 *poptind = optind;
2807 return popt;
2810 static MachineClass *select_machine(void)
2812 MachineClass *machine_class = find_default_machine();
2813 const char *optarg;
2814 QemuOpts *opts;
2815 Location loc;
2817 loc_push_none(&loc);
2819 opts = qemu_get_machine_opts();
2820 qemu_opts_loc_restore(opts);
2822 optarg = qemu_opt_get(opts, "type");
2823 if (optarg) {
2824 machine_class = machine_parse(optarg);
2827 if (!machine_class) {
2828 error_report("No machine specified, and there is no default");
2829 error_printf("Use -machine help to list supported machines\n");
2830 exit(1);
2833 loc_pop(&loc);
2834 return machine_class;
2837 static int machine_set_property(void *opaque,
2838 const char *name, const char *value,
2839 Error **errp)
2841 Object *obj = OBJECT(opaque);
2842 Error *local_err = NULL;
2843 char *c, *qom_name;
2845 if (strcmp(name, "type") == 0) {
2846 return 0;
2849 qom_name = g_strdup(name);
2850 c = qom_name;
2851 while (*c++) {
2852 if (*c == '_') {
2853 *c = '-';
2857 object_property_parse(obj, value, qom_name, &local_err);
2858 g_free(qom_name);
2860 if (local_err) {
2861 error_report_err(local_err);
2862 return -1;
2865 return 0;
2870 * Initial object creation happens before all other
2871 * QEMU data types are created. The majority of objects
2872 * can be created at this point. The rng-egd object
2873 * cannot be created here, as it depends on the chardev
2874 * already existing.
2876 static bool object_create_initial(const char *type)
2878 if (g_str_equal(type, "rng-egd")) {
2879 return false;
2883 * return false for concrete netfilters since
2884 * they depend on netdevs already existing
2886 if (g_str_equal(type, "filter-buffer") ||
2887 g_str_equal(type, "filter-dump") ||
2888 g_str_equal(type, "filter-mirror") ||
2889 g_str_equal(type, "filter-redirector")) {
2890 return false;
2893 return true;
2898 * The remainder of object creation happens after the
2899 * creation of chardev, fsdev, net clients and device data types.
2901 static bool object_create_delayed(const char *type)
2903 return !object_create_initial(type);
2907 static void set_memory_options(uint64_t *ram_slots, ram_addr_t *maxram_size,
2908 MachineClass *mc)
2910 uint64_t sz;
2911 const char *mem_str;
2912 const char *maxmem_str, *slots_str;
2913 const ram_addr_t default_ram_size = mc->default_ram_size;
2914 QemuOpts *opts = qemu_find_opts_singleton("memory");
2915 Location loc;
2917 loc_push_none(&loc);
2918 qemu_opts_loc_restore(opts);
2920 sz = 0;
2921 mem_str = qemu_opt_get(opts, "size");
2922 if (mem_str) {
2923 if (!*mem_str) {
2924 error_report("missing 'size' option value");
2925 exit(EXIT_FAILURE);
2928 sz = qemu_opt_get_size(opts, "size", ram_size);
2930 /* Fix up legacy suffix-less format */
2931 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2932 uint64_t overflow_check = sz;
2934 sz <<= 20;
2935 if ((sz >> 20) != overflow_check) {
2936 error_report("too large 'size' option value");
2937 exit(EXIT_FAILURE);
2942 /* backward compatibility behaviour for case "-m 0" */
2943 if (sz == 0) {
2944 sz = default_ram_size;
2947 sz = QEMU_ALIGN_UP(sz, 8192);
2948 ram_size = sz;
2949 if (ram_size != sz) {
2950 error_report("ram size too large");
2951 exit(EXIT_FAILURE);
2954 /* store value for the future use */
2955 qemu_opt_set_number(opts, "size", ram_size, &error_abort);
2956 *maxram_size = ram_size;
2958 maxmem_str = qemu_opt_get(opts, "maxmem");
2959 slots_str = qemu_opt_get(opts, "slots");
2960 if (maxmem_str && slots_str) {
2961 uint64_t slots;
2963 sz = qemu_opt_get_size(opts, "maxmem", 0);
2964 slots = qemu_opt_get_number(opts, "slots", 0);
2965 if (sz < ram_size) {
2966 error_report("invalid value of -m option maxmem: "
2967 "maximum memory size (0x%" PRIx64 ") must be at least "
2968 "the initial memory size (0x" RAM_ADDR_FMT ")",
2969 sz, ram_size);
2970 exit(EXIT_FAILURE);
2971 } else if (sz > ram_size) {
2972 if (!slots) {
2973 error_report("invalid value of -m option: maxmem was "
2974 "specified, but no hotplug slots were specified");
2975 exit(EXIT_FAILURE);
2977 } else if (slots) {
2978 error_report("invalid value of -m option maxmem: "
2979 "memory slots were specified but maximum memory size "
2980 "(0x%" PRIx64 ") is equal to the initial memory size "
2981 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
2982 exit(EXIT_FAILURE);
2985 *maxram_size = sz;
2986 *ram_slots = slots;
2987 } else if ((!maxmem_str && slots_str) ||
2988 (maxmem_str && !slots_str)) {
2989 error_report("invalid -m option value: missing "
2990 "'%s' option", slots_str ? "maxmem" : "slots");
2991 exit(EXIT_FAILURE);
2994 loc_pop(&loc);
2997 int main(int argc, char **argv)
2999 int i;
3000 int snapshot, linux_boot;
3001 const char *initrd_filename;
3002 const char *kernel_filename, *kernel_cmdline;
3003 const char *boot_order = NULL;
3004 const char *boot_once = NULL;
3005 DisplayState *ds;
3006 int cyls, heads, secs, translation;
3007 QemuOpts *hda_opts = NULL, *opts, *machine_opts, *icount_opts = NULL;
3008 QemuOptsList *olist;
3009 int optind;
3010 const char *optarg;
3011 const char *loadvm = NULL;
3012 MachineClass *machine_class;
3013 const char *cpu_model;
3014 const char *vga_model = NULL;
3015 const char *qtest_chrdev = NULL;
3016 const char *qtest_log = NULL;
3017 const char *pid_file = NULL;
3018 const char *incoming = NULL;
3019 #ifdef CONFIG_VNC
3020 int show_vnc_port = 0;
3021 #endif
3022 bool defconfig = true;
3023 bool userconfig = true;
3024 const char *log_mask = NULL;
3025 const char *log_file = NULL;
3026 char *trace_file = NULL;
3027 ram_addr_t maxram_size;
3028 uint64_t ram_slots = 0;
3029 FILE *vmstate_dump_file = NULL;
3030 Error *main_loop_err = NULL;
3031 Error *err = NULL;
3033 qemu_init_cpu_loop();
3034 qemu_mutex_lock_iothread();
3036 atexit(qemu_run_exit_notifiers);
3037 error_set_progname(argv[0]);
3038 qemu_init_exec_dir(argv[0]);
3040 module_call_init(MODULE_INIT_QOM);
3042 qemu_add_opts(&qemu_drive_opts);
3043 qemu_add_drive_opts(&qemu_legacy_drive_opts);
3044 qemu_add_drive_opts(&qemu_common_drive_opts);
3045 qemu_add_drive_opts(&qemu_drive_opts);
3046 qemu_add_opts(&qemu_chardev_opts);
3047 qemu_add_opts(&qemu_device_opts);
3048 qemu_add_opts(&qemu_netdev_opts);
3049 qemu_add_opts(&qemu_net_opts);
3050 qemu_add_opts(&qemu_rtc_opts);
3051 qemu_add_opts(&qemu_global_opts);
3052 qemu_add_opts(&qemu_mon_opts);
3053 qemu_add_opts(&qemu_trace_opts);
3054 qemu_add_opts(&qemu_option_rom_opts);
3055 qemu_add_opts(&qemu_machine_opts);
3056 qemu_add_opts(&qemu_mem_opts);
3057 qemu_add_opts(&qemu_smp_opts);
3058 qemu_add_opts(&qemu_boot_opts);
3059 qemu_add_opts(&qemu_sandbox_opts);
3060 qemu_add_opts(&qemu_add_fd_opts);
3061 qemu_add_opts(&qemu_object_opts);
3062 qemu_add_opts(&qemu_tpmdev_opts);
3063 qemu_add_opts(&qemu_realtime_opts);
3064 qemu_add_opts(&qemu_msg_opts);
3065 qemu_add_opts(&qemu_name_opts);
3066 qemu_add_opts(&qemu_numa_opts);
3067 qemu_add_opts(&qemu_icount_opts);
3068 qemu_add_opts(&qemu_semihosting_config_opts);
3069 qemu_add_opts(&qemu_fw_cfg_opts);
3070 module_call_init(MODULE_INIT_OPTS);
3072 runstate_init();
3074 if (qcrypto_init(&err) < 0) {
3075 error_reportf_err(err, "cannot initialize crypto: ");
3076 exit(1);
3078 rtc_clock = QEMU_CLOCK_HOST;
3080 QLIST_INIT (&vm_change_state_head);
3081 os_setup_early_signal_handling();
3083 cpu_model = NULL;
3084 snapshot = 0;
3085 cyls = heads = secs = 0;
3086 translation = BIOS_ATA_TRANSLATION_AUTO;
3088 nb_nics = 0;
3090 bdrv_init_with_whitelist();
3092 autostart = 1;
3094 /* first pass of option parsing */
3095 optind = 1;
3096 while (optind < argc) {
3097 if (argv[optind][0] != '-') {
3098 /* disk image */
3099 optind++;
3100 } else {
3101 const QEMUOption *popt;
3103 popt = lookup_opt(argc, argv, &optarg, &optind);
3104 switch (popt->index) {
3105 case QEMU_OPTION_nodefconfig:
3106 defconfig = false;
3107 break;
3108 case QEMU_OPTION_nouserconfig:
3109 userconfig = false;
3110 break;
3115 if (defconfig) {
3116 int ret;
3117 ret = qemu_read_default_config_files(userconfig);
3118 if (ret < 0) {
3119 exit(1);
3123 /* second pass of option parsing */
3124 optind = 1;
3125 for(;;) {
3126 if (optind >= argc)
3127 break;
3128 if (argv[optind][0] != '-') {
3129 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
3130 } else {
3131 const QEMUOption *popt;
3133 popt = lookup_opt(argc, argv, &optarg, &optind);
3134 if (!(popt->arch_mask & arch_type)) {
3135 printf("Option %s not supported for this target\n", popt->name);
3136 exit(1);
3138 switch(popt->index) {
3139 case QEMU_OPTION_no_kvm_irqchip: {
3140 olist = qemu_find_opts("machine");
3141 qemu_opts_parse_noisily(olist, "kernel_irqchip=off", false);
3142 break;
3144 case QEMU_OPTION_cpu:
3145 /* hw initialization will check this */
3146 cpu_model = optarg;
3147 break;
3148 case QEMU_OPTION_hda:
3150 char buf[256];
3151 if (cyls == 0)
3152 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
3153 else
3154 snprintf(buf, sizeof(buf),
3155 "%s,cyls=%d,heads=%d,secs=%d%s",
3156 HD_OPTS , cyls, heads, secs,
3157 translation == BIOS_ATA_TRANSLATION_LBA ?
3158 ",trans=lba" :
3159 translation == BIOS_ATA_TRANSLATION_NONE ?
3160 ",trans=none" : "");
3161 drive_add(IF_DEFAULT, 0, optarg, buf);
3162 break;
3164 case QEMU_OPTION_hdb:
3165 case QEMU_OPTION_hdc:
3166 case QEMU_OPTION_hdd:
3167 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
3168 HD_OPTS);
3169 break;
3170 case QEMU_OPTION_drive:
3171 if (drive_def(optarg) == NULL) {
3172 exit(1);
3174 break;
3175 case QEMU_OPTION_set:
3176 if (qemu_set_option(optarg) != 0)
3177 exit(1);
3178 break;
3179 case QEMU_OPTION_global:
3180 if (qemu_global_option(optarg) != 0)
3181 exit(1);
3182 break;
3183 case QEMU_OPTION_mtdblock:
3184 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3185 break;
3186 case QEMU_OPTION_sd:
3187 drive_add(IF_SD, -1, optarg, SD_OPTS);
3188 break;
3189 case QEMU_OPTION_pflash:
3190 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
3191 break;
3192 case QEMU_OPTION_snapshot:
3193 snapshot = 1;
3194 break;
3195 case QEMU_OPTION_hdachs:
3197 const char *p;
3198 p = optarg;
3199 cyls = strtol(p, (char **)&p, 0);
3200 if (cyls < 1 || cyls > 16383)
3201 goto chs_fail;
3202 if (*p != ',')
3203 goto chs_fail;
3204 p++;
3205 heads = strtol(p, (char **)&p, 0);
3206 if (heads < 1 || heads > 16)
3207 goto chs_fail;
3208 if (*p != ',')
3209 goto chs_fail;
3210 p++;
3211 secs = strtol(p, (char **)&p, 0);
3212 if (secs < 1 || secs > 63)
3213 goto chs_fail;
3214 if (*p == ',') {
3215 p++;
3216 if (!strcmp(p, "large")) {
3217 translation = BIOS_ATA_TRANSLATION_LARGE;
3218 } else if (!strcmp(p, "rechs")) {
3219 translation = BIOS_ATA_TRANSLATION_RECHS;
3220 } else if (!strcmp(p, "none")) {
3221 translation = BIOS_ATA_TRANSLATION_NONE;
3222 } else if (!strcmp(p, "lba")) {
3223 translation = BIOS_ATA_TRANSLATION_LBA;
3224 } else if (!strcmp(p, "auto")) {
3225 translation = BIOS_ATA_TRANSLATION_AUTO;
3226 } else {
3227 goto chs_fail;
3229 } else if (*p != '\0') {
3230 chs_fail:
3231 error_report("invalid physical CHS format");
3232 exit(1);
3234 if (hda_opts != NULL) {
3235 qemu_opt_set_number(hda_opts, "cyls", cyls,
3236 &error_abort);
3237 qemu_opt_set_number(hda_opts, "heads", heads,
3238 &error_abort);
3239 qemu_opt_set_number(hda_opts, "secs", secs,
3240 &error_abort);
3241 if (translation == BIOS_ATA_TRANSLATION_LARGE) {
3242 qemu_opt_set(hda_opts, "trans", "large",
3243 &error_abort);
3244 } else if (translation == BIOS_ATA_TRANSLATION_RECHS) {
3245 qemu_opt_set(hda_opts, "trans", "rechs",
3246 &error_abort);
3247 } else if (translation == BIOS_ATA_TRANSLATION_LBA) {
3248 qemu_opt_set(hda_opts, "trans", "lba",
3249 &error_abort);
3250 } else if (translation == BIOS_ATA_TRANSLATION_NONE) {
3251 qemu_opt_set(hda_opts, "trans", "none",
3252 &error_abort);
3256 break;
3257 case QEMU_OPTION_numa:
3258 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
3259 optarg, true);
3260 if (!opts) {
3261 exit(1);
3263 break;
3264 case QEMU_OPTION_display:
3265 display_type = select_display(optarg);
3266 break;
3267 case QEMU_OPTION_nographic:
3268 display_type = DT_NOGRAPHIC;
3269 break;
3270 case QEMU_OPTION_curses:
3271 #ifdef CONFIG_CURSES
3272 display_type = DT_CURSES;
3273 #else
3274 error_report("curses support is disabled");
3275 exit(1);
3276 #endif
3277 break;
3278 case QEMU_OPTION_portrait:
3279 graphic_rotate = 90;
3280 break;
3281 case QEMU_OPTION_rotate:
3282 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3283 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3284 graphic_rotate != 180 && graphic_rotate != 270) {
3285 error_report("only 90, 180, 270 deg rotation is available");
3286 exit(1);
3288 break;
3289 case QEMU_OPTION_kernel:
3290 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg,
3291 &error_abort);
3292 break;
3293 case QEMU_OPTION_initrd:
3294 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg,
3295 &error_abort);
3296 break;
3297 case QEMU_OPTION_append:
3298 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg,
3299 &error_abort);
3300 break;
3301 case QEMU_OPTION_dtb:
3302 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg,
3303 &error_abort);
3304 break;
3305 case QEMU_OPTION_cdrom:
3306 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3307 break;
3308 case QEMU_OPTION_boot:
3309 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
3310 optarg, true);
3311 if (!opts) {
3312 exit(1);
3314 break;
3315 case QEMU_OPTION_fda:
3316 case QEMU_OPTION_fdb:
3317 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3318 optarg, FD_OPTS);
3319 break;
3320 case QEMU_OPTION_no_fd_bootchk:
3321 fd_bootchk = 0;
3322 break;
3323 case QEMU_OPTION_netdev:
3324 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3325 exit(1);
3327 break;
3328 case QEMU_OPTION_net:
3329 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3330 exit(1);
3332 break;
3333 #ifdef CONFIG_LIBISCSI
3334 case QEMU_OPTION_iscsi:
3335 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
3336 optarg, false);
3337 if (!opts) {
3338 exit(1);
3340 break;
3341 #endif
3342 #ifdef CONFIG_SLIRP
3343 case QEMU_OPTION_tftp:
3344 error_report("The -tftp option is deprecated. "
3345 "Please use '-netdev user,tftp=...' instead.");
3346 legacy_tftp_prefix = optarg;
3347 break;
3348 case QEMU_OPTION_bootp:
3349 error_report("The -bootp option is deprecated. "
3350 "Please use '-netdev user,bootfile=...' instead.");
3351 legacy_bootp_filename = optarg;
3352 break;
3353 case QEMU_OPTION_redir:
3354 error_report("The -redir option is deprecated. "
3355 "Please use '-netdev user,hostfwd=...' instead.");
3356 if (net_slirp_redir(optarg) < 0)
3357 exit(1);
3358 break;
3359 #endif
3360 case QEMU_OPTION_bt:
3361 add_device_config(DEV_BT, optarg);
3362 break;
3363 case QEMU_OPTION_audio_help:
3364 AUD_help ();
3365 exit (0);
3366 break;
3367 case QEMU_OPTION_soundhw:
3368 select_soundhw (optarg);
3369 break;
3370 case QEMU_OPTION_h:
3371 help(0);
3372 break;
3373 case QEMU_OPTION_version:
3374 version();
3375 exit(0);
3376 break;
3377 case QEMU_OPTION_m:
3378 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
3379 optarg, true);
3380 if (!opts) {
3381 exit(EXIT_FAILURE);
3383 break;
3384 #ifdef CONFIG_TPM
3385 case QEMU_OPTION_tpmdev:
3386 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3387 exit(1);
3389 break;
3390 #endif
3391 case QEMU_OPTION_mempath:
3392 mem_path = optarg;
3393 break;
3394 case QEMU_OPTION_mem_prealloc:
3395 mem_prealloc = 1;
3396 break;
3397 case QEMU_OPTION_d:
3398 log_mask = optarg;
3399 break;
3400 case QEMU_OPTION_D:
3401 log_file = optarg;
3402 break;
3403 case QEMU_OPTION_DFILTER:
3404 qemu_set_dfilter_ranges(optarg);
3405 break;
3406 case QEMU_OPTION_s:
3407 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3408 break;
3409 case QEMU_OPTION_gdb:
3410 add_device_config(DEV_GDB, optarg);
3411 break;
3412 case QEMU_OPTION_L:
3413 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3414 data_dir[data_dir_idx++] = optarg;
3416 break;
3417 case QEMU_OPTION_bios:
3418 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg,
3419 &error_abort);
3420 break;
3421 case QEMU_OPTION_singlestep:
3422 singlestep = 1;
3423 break;
3424 case QEMU_OPTION_S:
3425 autostart = 0;
3426 break;
3427 case QEMU_OPTION_k:
3428 keyboard_layout = optarg;
3429 break;
3430 case QEMU_OPTION_localtime:
3431 rtc_utc = 0;
3432 break;
3433 case QEMU_OPTION_vga:
3434 vga_model = optarg;
3435 default_vga = 0;
3436 break;
3437 case QEMU_OPTION_g:
3439 const char *p;
3440 int w, h, depth;
3441 p = optarg;
3442 w = strtol(p, (char **)&p, 10);
3443 if (w <= 0) {
3444 graphic_error:
3445 error_report("invalid resolution or depth");
3446 exit(1);
3448 if (*p != 'x')
3449 goto graphic_error;
3450 p++;
3451 h = strtol(p, (char **)&p, 10);
3452 if (h <= 0)
3453 goto graphic_error;
3454 if (*p == 'x') {
3455 p++;
3456 depth = strtol(p, (char **)&p, 10);
3457 if (depth != 8 && depth != 15 && depth != 16 &&
3458 depth != 24 && depth != 32)
3459 goto graphic_error;
3460 } else if (*p == '\0') {
3461 depth = graphic_depth;
3462 } else {
3463 goto graphic_error;
3466 graphic_width = w;
3467 graphic_height = h;
3468 graphic_depth = depth;
3470 break;
3471 case QEMU_OPTION_echr:
3473 char *r;
3474 term_escape_char = strtol(optarg, &r, 0);
3475 if (r == optarg)
3476 printf("Bad argument to echr\n");
3477 break;
3479 case QEMU_OPTION_monitor:
3480 default_monitor = 0;
3481 if (strncmp(optarg, "none", 4)) {
3482 monitor_parse(optarg, "readline", false);
3484 break;
3485 case QEMU_OPTION_qmp:
3486 monitor_parse(optarg, "control", false);
3487 default_monitor = 0;
3488 break;
3489 case QEMU_OPTION_qmp_pretty:
3490 monitor_parse(optarg, "control", true);
3491 default_monitor = 0;
3492 break;
3493 case QEMU_OPTION_mon:
3494 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3495 true);
3496 if (!opts) {
3497 exit(1);
3499 default_monitor = 0;
3500 break;
3501 case QEMU_OPTION_chardev:
3502 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3503 optarg, true);
3504 if (!opts) {
3505 exit(1);
3507 break;
3508 case QEMU_OPTION_fsdev:
3509 olist = qemu_find_opts("fsdev");
3510 if (!olist) {
3511 error_report("fsdev support is disabled");
3512 exit(1);
3514 opts = qemu_opts_parse_noisily(olist, optarg, true);
3515 if (!opts) {
3516 exit(1);
3518 break;
3519 case QEMU_OPTION_virtfs: {
3520 QemuOpts *fsdev;
3521 QemuOpts *device;
3522 const char *writeout, *sock_fd, *socket;
3524 olist = qemu_find_opts("virtfs");
3525 if (!olist) {
3526 error_report("virtfs support is disabled");
3527 exit(1);
3529 opts = qemu_opts_parse_noisily(olist, optarg, true);
3530 if (!opts) {
3531 exit(1);
3534 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3535 qemu_opt_get(opts, "mount_tag") == NULL) {
3536 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3537 exit(1);
3539 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3540 qemu_opt_get(opts, "mount_tag"),
3541 1, NULL);
3542 if (!fsdev) {
3543 error_report("duplicate fsdev id: %s",
3544 qemu_opt_get(opts, "mount_tag"));
3545 exit(1);
3548 writeout = qemu_opt_get(opts, "writeout");
3549 if (writeout) {
3550 #ifdef CONFIG_SYNC_FILE_RANGE
3551 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3552 #else
3553 error_report("writeout=immediate not supported "
3554 "on this platform");
3555 exit(1);
3556 #endif
3558 qemu_opt_set(fsdev, "fsdriver",
3559 qemu_opt_get(opts, "fsdriver"), &error_abort);
3560 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"),
3561 &error_abort);
3562 qemu_opt_set(fsdev, "security_model",
3563 qemu_opt_get(opts, "security_model"),
3564 &error_abort);
3565 socket = qemu_opt_get(opts, "socket");
3566 if (socket) {
3567 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3569 sock_fd = qemu_opt_get(opts, "sock_fd");
3570 if (sock_fd) {
3571 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3574 qemu_opt_set_bool(fsdev, "readonly",
3575 qemu_opt_get_bool(opts, "readonly", 0),
3576 &error_abort);
3577 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3578 &error_abort);
3579 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3580 qemu_opt_set(device, "fsdev",
3581 qemu_opt_get(opts, "mount_tag"), &error_abort);
3582 qemu_opt_set(device, "mount_tag",
3583 qemu_opt_get(opts, "mount_tag"), &error_abort);
3584 break;
3586 case QEMU_OPTION_virtfs_synth: {
3587 QemuOpts *fsdev;
3588 QemuOpts *device;
3590 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3591 1, NULL);
3592 if (!fsdev) {
3593 error_report("duplicate option: %s", "virtfs_synth");
3594 exit(1);
3596 qemu_opt_set(fsdev, "fsdriver", "synth", &error_abort);
3598 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3599 &error_abort);
3600 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3601 qemu_opt_set(device, "fsdev", "v_synth", &error_abort);
3602 qemu_opt_set(device, "mount_tag", "v_synth", &error_abort);
3603 break;
3605 case QEMU_OPTION_serial:
3606 add_device_config(DEV_SERIAL, optarg);
3607 default_serial = 0;
3608 if (strncmp(optarg, "mon:", 4) == 0) {
3609 default_monitor = 0;
3611 break;
3612 case QEMU_OPTION_watchdog:
3613 if (watchdog) {
3614 error_report("only one watchdog option may be given");
3615 return 1;
3617 watchdog = optarg;
3618 break;
3619 case QEMU_OPTION_watchdog_action:
3620 if (select_watchdog_action(optarg) == -1) {
3621 error_report("unknown -watchdog-action parameter");
3622 exit(1);
3624 break;
3625 case QEMU_OPTION_virtiocon:
3626 add_device_config(DEV_VIRTCON, optarg);
3627 default_virtcon = 0;
3628 if (strncmp(optarg, "mon:", 4) == 0) {
3629 default_monitor = 0;
3631 break;
3632 case QEMU_OPTION_parallel:
3633 add_device_config(DEV_PARALLEL, optarg);
3634 default_parallel = 0;
3635 if (strncmp(optarg, "mon:", 4) == 0) {
3636 default_monitor = 0;
3638 break;
3639 case QEMU_OPTION_debugcon:
3640 add_device_config(DEV_DEBUGCON, optarg);
3641 break;
3642 case QEMU_OPTION_loadvm:
3643 loadvm = optarg;
3644 break;
3645 case QEMU_OPTION_full_screen:
3646 full_screen = 1;
3647 break;
3648 case QEMU_OPTION_no_frame:
3649 no_frame = 1;
3650 break;
3651 case QEMU_OPTION_alt_grab:
3652 alt_grab = 1;
3653 break;
3654 case QEMU_OPTION_ctrl_grab:
3655 ctrl_grab = 1;
3656 break;
3657 case QEMU_OPTION_no_quit:
3658 no_quit = 1;
3659 break;
3660 case QEMU_OPTION_sdl:
3661 #ifdef CONFIG_SDL
3662 display_type = DT_SDL;
3663 break;
3664 #else
3665 error_report("SDL support is disabled");
3666 exit(1);
3667 #endif
3668 case QEMU_OPTION_pidfile:
3669 pid_file = optarg;
3670 break;
3671 case QEMU_OPTION_win2k_hack:
3672 win2k_install_hack = 1;
3673 break;
3674 case QEMU_OPTION_rtc_td_hack: {
3675 static GlobalProperty slew_lost_ticks[] = {
3677 .driver = "mc146818rtc",
3678 .property = "lost_tick_policy",
3679 .value = "slew",
3681 { /* end of list */ }
3684 qdev_prop_register_global_list(slew_lost_ticks);
3685 break;
3687 case QEMU_OPTION_acpitable:
3688 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3689 optarg, true);
3690 if (!opts) {
3691 exit(1);
3693 do_acpitable_option(opts);
3694 break;
3695 case QEMU_OPTION_smbios:
3696 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3697 optarg, false);
3698 if (!opts) {
3699 exit(1);
3701 do_smbios_option(opts);
3702 break;
3703 case QEMU_OPTION_fwcfg:
3704 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3705 optarg, true);
3706 if (opts == NULL) {
3707 exit(1);
3709 break;
3710 case QEMU_OPTION_enable_kvm:
3711 olist = qemu_find_opts("machine");
3712 qemu_opts_parse_noisily(olist, "accel=kvm", false);
3713 break;
3714 case QEMU_OPTION_M:
3715 case QEMU_OPTION_machine:
3716 olist = qemu_find_opts("machine");
3717 opts = qemu_opts_parse_noisily(olist, optarg, true);
3718 if (!opts) {
3719 exit(1);
3721 break;
3722 case QEMU_OPTION_no_kvm:
3723 olist = qemu_find_opts("machine");
3724 qemu_opts_parse_noisily(olist, "accel=tcg", false);
3725 break;
3726 case QEMU_OPTION_no_kvm_pit: {
3727 error_report("warning: ignoring deprecated option");
3728 break;
3730 case QEMU_OPTION_no_kvm_pit_reinjection: {
3731 static GlobalProperty kvm_pit_lost_tick_policy[] = {
3733 .driver = "kvm-pit",
3734 .property = "lost_tick_policy",
3735 .value = "discard",
3737 { /* end of list */ }
3740 error_report("warning: deprecated, replaced by "
3741 "-global kvm-pit.lost_tick_policy=discard");
3742 qdev_prop_register_global_list(kvm_pit_lost_tick_policy);
3743 break;
3745 case QEMU_OPTION_usb:
3746 olist = qemu_find_opts("machine");
3747 qemu_opts_parse_noisily(olist, "usb=on", false);
3748 break;
3749 case QEMU_OPTION_usbdevice:
3750 olist = qemu_find_opts("machine");
3751 qemu_opts_parse_noisily(olist, "usb=on", false);
3752 add_device_config(DEV_USB, optarg);
3753 break;
3754 case QEMU_OPTION_device:
3755 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3756 optarg, true)) {
3757 exit(1);
3759 break;
3760 case QEMU_OPTION_smp:
3761 if (!qemu_opts_parse_noisily(qemu_find_opts("smp-opts"),
3762 optarg, true)) {
3763 exit(1);
3765 break;
3766 case QEMU_OPTION_vnc:
3768 #ifdef CONFIG_VNC
3769 Error *local_err = NULL;
3771 if (vnc_parse(optarg, &local_err) == NULL) {
3772 error_report_err(local_err);
3773 exit(1);
3775 #else
3776 error_report("VNC support is disabled");
3777 exit(1);
3778 #endif
3779 break;
3781 case QEMU_OPTION_no_acpi:
3782 acpi_enabled = 0;
3783 break;
3784 case QEMU_OPTION_no_hpet:
3785 no_hpet = 1;
3786 break;
3787 case QEMU_OPTION_balloon:
3788 if (balloon_parse(optarg) < 0) {
3789 error_report("unknown -balloon argument %s", optarg);
3790 exit(1);
3792 break;
3793 case QEMU_OPTION_no_reboot:
3794 no_reboot = 1;
3795 break;
3796 case QEMU_OPTION_no_shutdown:
3797 no_shutdown = 1;
3798 break;
3799 case QEMU_OPTION_show_cursor:
3800 cursor_hide = 0;
3801 break;
3802 case QEMU_OPTION_uuid:
3803 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3804 error_report("failed to parse UUID string: wrong format");
3805 exit(1);
3807 qemu_uuid_set = true;
3808 break;
3809 case QEMU_OPTION_option_rom:
3810 if (nb_option_roms >= MAX_OPTION_ROMS) {
3811 error_report("too many option ROMs");
3812 exit(1);
3814 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3815 optarg, true);
3816 if (!opts) {
3817 exit(1);
3819 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3820 option_rom[nb_option_roms].bootindex =
3821 qemu_opt_get_number(opts, "bootindex", -1);
3822 if (!option_rom[nb_option_roms].name) {
3823 error_report("Option ROM file is not specified");
3824 exit(1);
3826 nb_option_roms++;
3827 break;
3828 case QEMU_OPTION_semihosting:
3829 semihosting.enabled = true;
3830 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3831 break;
3832 case QEMU_OPTION_semihosting_config:
3833 semihosting.enabled = true;
3834 opts = qemu_opts_parse_noisily(qemu_find_opts("semihosting-config"),
3835 optarg, false);
3836 if (opts != NULL) {
3837 semihosting.enabled = qemu_opt_get_bool(opts, "enable",
3838 true);
3839 const char *target = qemu_opt_get(opts, "target");
3840 if (target != NULL) {
3841 if (strcmp("native", target) == 0) {
3842 semihosting.target = SEMIHOSTING_TARGET_NATIVE;
3843 } else if (strcmp("gdb", target) == 0) {
3844 semihosting.target = SEMIHOSTING_TARGET_GDB;
3845 } else if (strcmp("auto", target) == 0) {
3846 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3847 } else {
3848 error_report("unsupported semihosting-config %s",
3849 optarg);
3850 exit(1);
3852 } else {
3853 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3855 /* Set semihosting argument count and vector */
3856 qemu_opt_foreach(opts, add_semihosting_arg,
3857 &semihosting, NULL);
3858 } else {
3859 error_report("unsupported semihosting-config %s", optarg);
3860 exit(1);
3862 break;
3863 case QEMU_OPTION_tdf:
3864 error_report("warning: ignoring deprecated option");
3865 break;
3866 case QEMU_OPTION_name:
3867 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3868 optarg, true);
3869 if (!opts) {
3870 exit(1);
3872 break;
3873 case QEMU_OPTION_prom_env:
3874 if (nb_prom_envs >= MAX_PROM_ENVS) {
3875 error_report("too many prom variables");
3876 exit(1);
3878 prom_envs[nb_prom_envs] = optarg;
3879 nb_prom_envs++;
3880 break;
3881 case QEMU_OPTION_old_param:
3882 old_param = 1;
3883 break;
3884 case QEMU_OPTION_clock:
3885 /* Clock options no longer exist. Keep this option for
3886 * backward compatibility.
3888 break;
3889 case QEMU_OPTION_startdate:
3890 configure_rtc_date_offset(optarg, 1);
3891 break;
3892 case QEMU_OPTION_rtc:
3893 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3894 false);
3895 if (!opts) {
3896 exit(1);
3898 configure_rtc(opts);
3899 break;
3900 case QEMU_OPTION_tb_size:
3901 tcg_tb_size = strtol(optarg, NULL, 0);
3902 if (tcg_tb_size < 0) {
3903 tcg_tb_size = 0;
3905 break;
3906 case QEMU_OPTION_icount:
3907 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3908 optarg, true);
3909 if (!icount_opts) {
3910 exit(1);
3912 break;
3913 case QEMU_OPTION_incoming:
3914 if (!incoming) {
3915 runstate_set(RUN_STATE_INMIGRATE);
3917 incoming = optarg;
3918 break;
3919 case QEMU_OPTION_nodefaults:
3920 has_defaults = 0;
3921 break;
3922 case QEMU_OPTION_xen_domid:
3923 if (!(xen_available())) {
3924 printf("Option %s not supported for this target\n", popt->name);
3925 exit(1);
3927 xen_domid = atoi(optarg);
3928 break;
3929 case QEMU_OPTION_xen_create:
3930 if (!(xen_available())) {
3931 printf("Option %s not supported for this target\n", popt->name);
3932 exit(1);
3934 xen_mode = XEN_CREATE;
3935 break;
3936 case QEMU_OPTION_xen_attach:
3937 if (!(xen_available())) {
3938 printf("Option %s not supported for this target\n", popt->name);
3939 exit(1);
3941 xen_mode = XEN_ATTACH;
3942 break;
3943 case QEMU_OPTION_trace:
3945 opts = qemu_opts_parse_noisily(qemu_find_opts("trace"),
3946 optarg, true);
3947 if (!opts) {
3948 exit(1);
3950 if (qemu_opt_get(opts, "enable")) {
3951 trace_enable_events(qemu_opt_get(opts, "enable"));
3953 trace_init_events(qemu_opt_get(opts, "events"));
3954 if (trace_file) {
3955 g_free(trace_file);
3957 trace_file = g_strdup(qemu_opt_get(opts, "file"));
3958 qemu_opts_del(opts);
3959 break;
3961 case QEMU_OPTION_trace_unassigned:
3962 trace_unassigned = true;
3963 break;
3964 case QEMU_OPTION_readconfig:
3966 int ret = qemu_read_config_file(optarg);
3967 if (ret < 0) {
3968 error_report("read config %s: %s", optarg,
3969 strerror(-ret));
3970 exit(1);
3972 break;
3974 case QEMU_OPTION_spice:
3975 olist = qemu_find_opts("spice");
3976 if (!olist) {
3977 error_report("spice support is disabled");
3978 exit(1);
3980 opts = qemu_opts_parse_noisily(olist, optarg, false);
3981 if (!opts) {
3982 exit(1);
3984 display_remote++;
3985 break;
3986 case QEMU_OPTION_writeconfig:
3988 FILE *fp;
3989 if (strcmp(optarg, "-") == 0) {
3990 fp = stdout;
3991 } else {
3992 fp = fopen(optarg, "w");
3993 if (fp == NULL) {
3994 error_report("open %s: %s", optarg,
3995 strerror(errno));
3996 exit(1);
3999 qemu_config_write(fp);
4000 if (fp != stdout) {
4001 fclose(fp);
4003 break;
4005 case QEMU_OPTION_qtest:
4006 qtest_chrdev = optarg;
4007 break;
4008 case QEMU_OPTION_qtest_log:
4009 qtest_log = optarg;
4010 break;
4011 case QEMU_OPTION_sandbox:
4012 opts = qemu_opts_parse_noisily(qemu_find_opts("sandbox"),
4013 optarg, true);
4014 if (!opts) {
4015 exit(1);
4017 break;
4018 case QEMU_OPTION_add_fd:
4019 #ifndef _WIN32
4020 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
4021 optarg, false);
4022 if (!opts) {
4023 exit(1);
4025 #else
4026 error_report("File descriptor passing is disabled on this "
4027 "platform");
4028 exit(1);
4029 #endif
4030 break;
4031 case QEMU_OPTION_object:
4032 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
4033 optarg, true);
4034 if (!opts) {
4035 exit(1);
4037 break;
4038 case QEMU_OPTION_realtime:
4039 opts = qemu_opts_parse_noisily(qemu_find_opts("realtime"),
4040 optarg, false);
4041 if (!opts) {
4042 exit(1);
4044 enable_mlock = qemu_opt_get_bool(opts, "mlock", true);
4045 break;
4046 case QEMU_OPTION_msg:
4047 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
4048 false);
4049 if (!opts) {
4050 exit(1);
4052 configure_msg(opts);
4053 break;
4054 case QEMU_OPTION_dump_vmstate:
4055 if (vmstate_dump_file) {
4056 error_report("only one '-dump-vmstate' "
4057 "option may be given");
4058 exit(1);
4060 vmstate_dump_file = fopen(optarg, "w");
4061 if (vmstate_dump_file == NULL) {
4062 error_report("open %s: %s", optarg, strerror(errno));
4063 exit(1);
4065 break;
4066 default:
4067 os_parse_cmd_args(popt->index, optarg);
4072 * Clear error location left behind by the loop.
4073 * Best done right after the loop. Do not insert code here!
4075 loc_set_none();
4077 replay_configure(icount_opts);
4079 machine_class = select_machine();
4081 set_memory_options(&ram_slots, &maxram_size, machine_class);
4083 os_daemonize();
4085 if (qemu_init_main_loop(&main_loop_err)) {
4086 error_report_err(main_loop_err);
4087 exit(1);
4090 if (qemu_opts_foreach(qemu_find_opts("sandbox"),
4091 parse_sandbox, NULL, NULL)) {
4092 exit(1);
4095 if (qemu_opts_foreach(qemu_find_opts("name"),
4096 parse_name, NULL, NULL)) {
4097 exit(1);
4100 #ifndef _WIN32
4101 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4102 parse_add_fd, NULL, NULL)) {
4103 exit(1);
4106 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4107 cleanup_add_fd, NULL, NULL)) {
4108 exit(1);
4110 #endif
4112 current_machine = MACHINE(object_new(object_class_get_name(
4113 OBJECT_CLASS(machine_class))));
4114 if (machine_help_func(qemu_get_machine_opts(), current_machine)) {
4115 exit(0);
4117 object_property_add_child(object_get_root(), "machine",
4118 OBJECT(current_machine), &error_abort);
4119 cpu_exec_init_all();
4121 if (machine_class->hw_version) {
4122 qemu_set_hw_version(machine_class->hw_version);
4125 /* Init CPU def lists, based on config
4126 * - Must be called after all the qemu_read_config_file() calls
4127 * - Must be called before list_cpus()
4128 * - Must be called before machine_class->init()
4130 cpudef_init();
4132 if (cpu_model && is_help_option(cpu_model)) {
4133 list_cpus(stdout, &fprintf, cpu_model);
4134 exit(0);
4137 if (!trace_init_backends()) {
4138 exit(1);
4140 trace_init_file(trace_file);
4142 /* Open the logfile at this point and set the log mask if necessary.
4144 if (log_file) {
4145 qemu_set_log_filename(log_file);
4148 if (log_mask) {
4149 int mask;
4150 mask = qemu_str_to_log_mask(log_mask);
4151 if (!mask) {
4152 qemu_print_log_usage(stdout);
4153 exit(1);
4155 qemu_set_log(mask);
4156 } else {
4157 qemu_set_log(0);
4160 /* If no data_dir is specified then try to find it relative to the
4161 executable path. */
4162 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4163 data_dir[data_dir_idx] = os_find_datadir();
4164 if (data_dir[data_dir_idx] != NULL) {
4165 data_dir_idx++;
4168 /* If all else fails use the install path specified when building. */
4169 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4170 data_dir[data_dir_idx++] = CONFIG_QEMU_DATADIR;
4173 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
4175 machine_class->max_cpus = machine_class->max_cpus ?: 1; /* Default to UP */
4176 if (max_cpus > machine_class->max_cpus) {
4177 error_report("Number of SMP CPUs requested (%d) exceeds max CPUs "
4178 "supported by machine '%s' (%d)", max_cpus,
4179 machine_class->name, machine_class->max_cpus);
4180 exit(1);
4184 * Get the default machine options from the machine if it is not already
4185 * specified either by the configuration file or by the command line.
4187 if (machine_class->default_machine_opts) {
4188 qemu_opts_set_defaults(qemu_find_opts("machine"),
4189 machine_class->default_machine_opts, 0);
4192 qemu_opts_foreach(qemu_find_opts("device"),
4193 default_driver_check, NULL, NULL);
4194 qemu_opts_foreach(qemu_find_opts("global"),
4195 default_driver_check, NULL, NULL);
4197 if (!vga_model && !default_vga) {
4198 vga_interface_type = VGA_DEVICE;
4200 if (!has_defaults || machine_class->no_serial) {
4201 default_serial = 0;
4203 if (!has_defaults || machine_class->no_parallel) {
4204 default_parallel = 0;
4206 if (!has_defaults || !machine_class->use_virtcon) {
4207 default_virtcon = 0;
4209 if (!has_defaults || !machine_class->use_sclp) {
4210 default_sclp = 0;
4212 if (!has_defaults || machine_class->no_floppy) {
4213 default_floppy = 0;
4215 if (!has_defaults || machine_class->no_cdrom) {
4216 default_cdrom = 0;
4218 if (!has_defaults || machine_class->no_sdcard) {
4219 default_sdcard = 0;
4221 if (!has_defaults) {
4222 default_monitor = 0;
4223 default_net = 0;
4224 default_vga = 0;
4227 if (is_daemonized()) {
4228 /* According to documentation and historically, -nographic redirects
4229 * serial port, parallel port and monitor to stdio, which does not work
4230 * with -daemonize. We can redirect these to null instead, but since
4231 * -nographic is legacy, let's just error out.
4232 * We disallow -nographic only if all other ports are not redirected
4233 * explicitly, to not break existing legacy setups which uses
4234 * -nographic _and_ redirects all ports explicitly - this is valid
4235 * usage, -nographic is just a no-op in this case.
4237 if (display_type == DT_NOGRAPHIC
4238 && (default_parallel || default_serial
4239 || default_monitor || default_virtcon)) {
4240 error_report("-nographic cannot be used with -daemonize");
4241 exit(1);
4243 #ifdef CONFIG_CURSES
4244 if (display_type == DT_CURSES) {
4245 error_report("curses display cannot be used with -daemonize");
4246 exit(1);
4248 #endif
4251 if (display_type == DT_NOGRAPHIC) {
4252 if (default_parallel)
4253 add_device_config(DEV_PARALLEL, "null");
4254 if (default_serial && default_monitor) {
4255 add_device_config(DEV_SERIAL, "mon:stdio");
4256 } else if (default_virtcon && default_monitor) {
4257 add_device_config(DEV_VIRTCON, "mon:stdio");
4258 } else if (default_sclp && default_monitor) {
4259 add_device_config(DEV_SCLP, "mon:stdio");
4260 } else {
4261 if (default_serial)
4262 add_device_config(DEV_SERIAL, "stdio");
4263 if (default_virtcon)
4264 add_device_config(DEV_VIRTCON, "stdio");
4265 if (default_sclp) {
4266 add_device_config(DEV_SCLP, "stdio");
4268 if (default_monitor)
4269 monitor_parse("stdio", "readline", false);
4271 } else {
4272 if (default_serial)
4273 add_device_config(DEV_SERIAL, "vc:80Cx24C");
4274 if (default_parallel)
4275 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
4276 if (default_monitor)
4277 monitor_parse("vc:80Cx24C", "readline", false);
4278 if (default_virtcon)
4279 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
4280 if (default_sclp) {
4281 add_device_config(DEV_SCLP, "vc:80Cx24C");
4285 #if defined(CONFIG_VNC)
4286 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
4287 display_remote++;
4289 #endif
4290 if (display_type == DT_DEFAULT && !display_remote) {
4291 #if defined(CONFIG_GTK)
4292 display_type = DT_GTK;
4293 #elif defined(CONFIG_SDL) || defined(CONFIG_COCOA)
4294 display_type = DT_SDL;
4295 #elif defined(CONFIG_VNC)
4296 vnc_parse("localhost:0,to=99,id=default", &error_abort);
4297 show_vnc_port = 1;
4298 #else
4299 display_type = DT_NONE;
4300 #endif
4303 if ((no_frame || alt_grab || ctrl_grab) && display_type != DT_SDL) {
4304 error_report("-no-frame, -alt-grab and -ctrl-grab are only valid "
4305 "for SDL, ignoring option");
4307 if (no_quit && (display_type != DT_GTK && display_type != DT_SDL)) {
4308 error_report("-no-quit is only valid for GTK and SDL, "
4309 "ignoring option");
4312 #if defined(CONFIG_GTK)
4313 if (display_type == DT_GTK) {
4314 early_gtk_display_init(request_opengl);
4316 #endif
4317 #if defined(CONFIG_SDL)
4318 if (display_type == DT_SDL) {
4319 sdl_display_early_init(request_opengl);
4321 #endif
4322 if (request_opengl == 1 && display_opengl == 0) {
4323 #if defined(CONFIG_OPENGL)
4324 error_report("OpenGL is not supported by the display");
4325 #else
4326 error_report("OpenGL support is disabled");
4327 #endif
4328 exit(1);
4331 page_size_init();
4332 socket_init();
4334 if (qemu_opts_foreach(qemu_find_opts("object"),
4335 user_creatable_add_opts_foreach,
4336 object_create_initial, NULL)) {
4337 exit(1);
4340 if (qemu_opts_foreach(qemu_find_opts("chardev"),
4341 chardev_init_func, NULL, NULL)) {
4342 exit(1);
4345 #ifdef CONFIG_VIRTFS
4346 if (qemu_opts_foreach(qemu_find_opts("fsdev"),
4347 fsdev_init_func, NULL, NULL)) {
4348 exit(1);
4350 #endif
4352 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
4353 error_report("could not acquire pid file: %s", strerror(errno));
4354 exit(1);
4357 if (qemu_opts_foreach(qemu_find_opts("device"),
4358 device_help_func, NULL, NULL)) {
4359 exit(0);
4362 machine_opts = qemu_get_machine_opts();
4363 if (qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
4364 NULL)) {
4365 object_unref(OBJECT(current_machine));
4366 exit(1);
4369 configure_accelerator(current_machine);
4371 if (qtest_chrdev) {
4372 qtest_init(qtest_chrdev, qtest_log, &error_fatal);
4375 machine_opts = qemu_get_machine_opts();
4376 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4377 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4378 kernel_cmdline = qemu_opt_get(machine_opts, "append");
4379 bios_name = qemu_opt_get(machine_opts, "firmware");
4381 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4382 if (opts) {
4383 boot_order = qemu_opt_get(opts, "order");
4384 if (boot_order) {
4385 validate_bootdevices(boot_order, &error_fatal);
4388 boot_once = qemu_opt_get(opts, "once");
4389 if (boot_once) {
4390 validate_bootdevices(boot_once, &error_fatal);
4393 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
4394 boot_strict = qemu_opt_get_bool(opts, "strict", false);
4397 if (!boot_order) {
4398 boot_order = machine_class->default_boot_order;
4401 if (!kernel_cmdline) {
4402 kernel_cmdline = "";
4403 current_machine->kernel_cmdline = (char *)kernel_cmdline;
4406 linux_boot = (kernel_filename != NULL);
4408 if (!linux_boot && *kernel_cmdline != '\0') {
4409 error_report("-append only allowed with -kernel option");
4410 exit(1);
4413 if (!linux_boot && initrd_filename != NULL) {
4414 error_report("-initrd only allowed with -kernel option");
4415 exit(1);
4418 if (!linux_boot && qemu_opt_get(machine_opts, "dtb")) {
4419 error_report("-dtb only allowed with -kernel option");
4420 exit(1);
4423 if (semihosting_enabled() && !semihosting_get_argc() && kernel_filename) {
4424 /* fall back to the -kernel/-append */
4425 semihosting_arg_fallback(kernel_filename, kernel_cmdline);
4428 os_set_line_buffering();
4430 #ifdef CONFIG_SPICE
4431 /* spice needs the timers to be initialized by this point */
4432 qemu_spice_init();
4433 #endif
4435 cpu_ticks_init();
4436 if (icount_opts) {
4437 if (kvm_enabled() || xen_enabled()) {
4438 error_report("-icount is not allowed with kvm or xen");
4439 exit(1);
4441 configure_icount(icount_opts, &error_abort);
4442 qemu_opts_del(icount_opts);
4445 /* clean up network at qemu process termination */
4446 atexit(&net_cleanup);
4448 if (net_init_clients() < 0) {
4449 exit(1);
4452 if (qemu_opts_foreach(qemu_find_opts("object"),
4453 user_creatable_add_opts_foreach,
4454 object_create_delayed, NULL)) {
4455 exit(1);
4458 #ifdef CONFIG_TPM
4459 if (tpm_init() < 0) {
4460 exit(1);
4462 #endif
4464 /* init the bluetooth world */
4465 if (foreach_device_config(DEV_BT, bt_parse))
4466 exit(1);
4468 if (!xen_enabled()) {
4469 /* On 32-bit hosts, QEMU is limited by virtual address space */
4470 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4471 error_report("at most 2047 MB RAM can be simulated");
4472 exit(1);
4476 blk_mig_init();
4477 ram_mig_init();
4479 /* If the currently selected machine wishes to override the units-per-bus
4480 * property of its default HBA interface type, do so now. */
4481 if (machine_class->units_per_default_bus) {
4482 override_max_devs(machine_class->block_default_type,
4483 machine_class->units_per_default_bus);
4486 /* open the virtual block devices */
4487 if (snapshot || replay_mode != REPLAY_MODE_NONE) {
4488 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
4489 NULL, NULL);
4491 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
4492 &machine_class->block_default_type, NULL)) {
4493 exit(1);
4496 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
4497 CDROM_OPTS);
4498 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4499 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
4501 parse_numa_opts(machine_class);
4503 if (qemu_opts_foreach(qemu_find_opts("mon"),
4504 mon_init_func, NULL, NULL)) {
4505 exit(1);
4508 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4509 exit(1);
4510 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4511 exit(1);
4512 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4513 exit(1);
4514 if (foreach_device_config(DEV_SCLP, sclp_parse) < 0) {
4515 exit(1);
4517 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4518 exit(1);
4520 /* If no default VGA is requested, the default is "none". */
4521 if (default_vga) {
4522 if (machine_class->default_display) {
4523 vga_model = machine_class->default_display;
4524 } else if (cirrus_vga_available()) {
4525 vga_model = "cirrus";
4526 } else if (vga_available()) {
4527 vga_model = "std";
4530 if (vga_model) {
4531 select_vgahw(vga_model);
4534 if (watchdog) {
4535 i = select_watchdog(watchdog);
4536 if (i > 0)
4537 exit (i == 1 ? 1 : 0);
4540 if (machine_class->compat_props) {
4541 qdev_prop_register_global_list(machine_class->compat_props);
4543 qemu_add_globals();
4545 /* This checkpoint is required by replay to separate prior clock
4546 reading from the other reads, because timer polling functions query
4547 clock values from the log. */
4548 replay_checkpoint(CHECKPOINT_INIT);
4549 qdev_machine_init();
4551 current_machine->ram_size = ram_size;
4552 current_machine->maxram_size = maxram_size;
4553 current_machine->ram_slots = ram_slots;
4554 current_machine->boot_order = boot_order;
4555 current_machine->cpu_model = cpu_model;
4557 machine_class->init(current_machine);
4559 realtime_init();
4561 audio_init();
4563 cpu_synchronize_all_post_init();
4565 numa_post_machine_init();
4567 if (qemu_opts_foreach(qemu_find_opts("fw_cfg"),
4568 parse_fw_cfg, fw_cfg_find(), NULL) != 0) {
4569 exit(1);
4572 /* init USB devices */
4573 if (usb_enabled()) {
4574 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4575 exit(1);
4578 /* Check if IGD GFX passthrough. */
4579 igd_gfx_passthru();
4581 /* init generic devices */
4582 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
4583 if (qemu_opts_foreach(qemu_find_opts("device"),
4584 device_init_func, NULL, NULL)) {
4585 exit(1);
4587 rom_reset_order_override();
4589 /* Did we create any drives that we failed to create a device for? */
4590 drive_check_orphaned();
4592 net_check_clients();
4594 if (boot_once) {
4595 qemu_boot_set(boot_once, &error_fatal);
4596 qemu_register_reset(restore_boot_order, g_strdup(boot_order));
4599 ds = init_displaystate();
4601 /* init local displays */
4602 switch (display_type) {
4603 case DT_NOGRAPHIC:
4604 (void)ds; /* avoid warning if no display is configured */
4605 break;
4606 #if defined(CONFIG_CURSES)
4607 case DT_CURSES:
4608 curses_display_init(ds, full_screen);
4609 break;
4610 #endif
4611 #if defined(CONFIG_SDL)
4612 case DT_SDL:
4613 sdl_display_init(ds, full_screen, no_frame);
4614 break;
4615 #elif defined(CONFIG_COCOA)
4616 case DT_SDL:
4617 cocoa_display_init(ds, full_screen);
4618 break;
4619 #endif
4620 #if defined(CONFIG_GTK)
4621 case DT_GTK:
4622 gtk_display_init(ds, full_screen, grab_on_hover);
4623 break;
4624 #endif
4625 default:
4626 break;
4629 /* must be after terminal init, SDL library changes signal handlers */
4630 os_setup_signal_handling();
4632 #ifdef CONFIG_VNC
4633 /* init remote displays */
4634 qemu_opts_foreach(qemu_find_opts("vnc"),
4635 vnc_init_func, NULL, NULL);
4636 if (show_vnc_port) {
4637 char *ret = vnc_display_local_addr("default");
4638 printf("VNC server running on '%s'\n", ret);
4639 g_free(ret);
4641 #endif
4642 #ifdef CONFIG_SPICE
4643 if (using_spice) {
4644 qemu_spice_display_init();
4646 #endif
4648 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
4649 exit(1);
4652 qdev_machine_creation_done();
4654 /* TODO: once all bus devices are qdevified, this should be done
4655 * when bus is created by qdev.c */
4656 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4657 qemu_run_machine_init_done_notifiers();
4659 if (rom_check_and_register_reset() != 0) {
4660 error_report("rom check and register reset failed");
4661 exit(1);
4664 replay_start();
4666 /* This checkpoint is required by replay to separate prior clock
4667 reading from the other reads, because timer polling functions query
4668 clock values from the log. */
4669 replay_checkpoint(CHECKPOINT_RESET);
4670 qemu_system_reset(VMRESET_SILENT);
4671 register_global_state();
4672 if (loadvm) {
4673 if (load_vmstate(loadvm) < 0) {
4674 autostart = 0;
4678 qdev_prop_check_globals();
4679 if (vmstate_dump_file) {
4680 /* dump and exit */
4681 dump_vmstate_json_to_file(vmstate_dump_file);
4682 return 0;
4685 if (incoming) {
4686 Error *local_err = NULL;
4687 qemu_start_incoming_migration(incoming, &local_err);
4688 if (local_err) {
4689 error_reportf_err(local_err, "-incoming %s: ", incoming);
4690 exit(1);
4692 } else if (autostart) {
4693 vm_start();
4696 os_setup_post();
4698 main_loop();
4699 replay_disable_events();
4701 bdrv_close_all();
4702 pause_all_vcpus();
4703 res_free();
4704 #ifdef CONFIG_TPM
4705 tpm_cleanup();
4706 #endif
4708 return 0;