qapi: Remove unwanted commas after #optional keyword
[qemu/ar7.git] / vl.c
blob68e8c003d1b34986cb76b39038608a2b726c0de7
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-version.h"
26 #include "qemu/cutils.h"
27 #include "qemu/help_option.h"
28 #include "qemu/uuid.h"
30 #ifdef CONFIG_SECCOMP
31 #include "sysemu/seccomp.h"
32 #endif
34 #if defined(CONFIG_VDE)
35 #include <libvdeplug.h>
36 #endif
38 #ifdef CONFIG_SDL
39 #if defined(__APPLE__) || defined(main)
40 #include <SDL.h>
41 int qemu_main(int argc, char **argv, char **envp);
42 int main(int argc, char **argv)
44 return qemu_main(argc, argv, NULL);
46 #undef main
47 #define main qemu_main
48 #endif
49 #endif /* CONFIG_SDL */
51 #ifdef CONFIG_COCOA
52 #undef main
53 #define main qemu_main
54 #endif /* CONFIG_COCOA */
57 #include "qemu/error-report.h"
58 #include "qemu/sockets.h"
59 #include "hw/hw.h"
60 #include "hw/boards.h"
61 #include "sysemu/accel.h"
62 #include "hw/usb.h"
63 #include "hw/i386/pc.h"
64 #include "hw/isa/isa.h"
65 #include "hw/bt.h"
66 #include "sysemu/watchdog.h"
67 #include "hw/smbios/smbios.h"
68 #include "hw/acpi/acpi.h"
69 #include "hw/xen/xen.h"
70 #include "hw/qdev.h"
71 #include "hw/loader.h"
72 #include "monitor/qdev.h"
73 #include "sysemu/bt.h"
74 #include "net/net.h"
75 #include "net/slirp.h"
76 #include "monitor/monitor.h"
77 #include "ui/console.h"
78 #include "ui/input.h"
79 #include "sysemu/sysemu.h"
80 #include "sysemu/numa.h"
81 #include "exec/gdbstub.h"
82 #include "qemu/timer.h"
83 #include "sysemu/char.h"
84 #include "qemu/bitmap.h"
85 #include "qemu/log.h"
86 #include "sysemu/blockdev.h"
87 #include "hw/block/block.h"
88 #include "migration/block.h"
89 #include "sysemu/tpm.h"
90 #include "sysemu/dma.h"
91 #include "audio/audio.h"
92 #include "migration/migration.h"
93 #include "sysemu/cpus.h"
94 #include "migration/colo.h"
95 #include "sysemu/kvm.h"
96 #include "sysemu/hax.h"
97 #include "qapi/qmp/qjson.h"
98 #include "qemu/option.h"
99 #include "qemu/config-file.h"
100 #include "qemu-options.h"
101 #include "qmp-commands.h"
102 #include "qemu/main-loop.h"
103 #ifdef CONFIG_VIRTFS
104 #include "fsdev/qemu-fsdev.h"
105 #endif
106 #include "sysemu/qtest.h"
108 #include "disas/disas.h"
111 #include "slirp/libslirp.h"
113 #include "trace.h"
114 #include "trace/control.h"
115 #include "qemu/queue.h"
116 #include "sysemu/arch_init.h"
118 #include "ui/qemu-spice.h"
119 #include "qapi/string-input-visitor.h"
120 #include "qapi/opts-visitor.h"
121 #include "qom/object_interfaces.h"
122 #include "qapi-event.h"
123 #include "exec/semihost.h"
124 #include "crypto/init.h"
125 #include "sysemu/replay.h"
126 #include "qapi/qmp/qerror.h"
127 #include "sysemu/iothread.h"
129 #define MAX_VIRTIO_CONSOLES 1
130 #define MAX_SCLP_CONSOLES 1
132 static const char *data_dir[16];
133 static int data_dir_idx;
134 const char *bios_name = NULL;
135 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
136 int request_opengl = -1;
137 int display_opengl;
138 const char* keyboard_layout = NULL;
139 ram_addr_t ram_size;
140 const char *mem_path = NULL;
141 int mem_prealloc = 0; /* force preallocation of physical target memory */
142 bool enable_mlock = false;
143 int nb_nics;
144 NICInfo nd_table[MAX_NICS];
145 int autostart;
146 static int rtc_utc = 1;
147 static int rtc_date_offset = -1; /* -1 means no change */
148 QEMUClockType rtc_clock;
149 int vga_interface_type = VGA_NONE;
150 static int full_screen = 0;
151 static int no_frame = 0;
152 int no_quit = 0;
153 static bool grab_on_hover;
154 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
155 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
156 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
157 CharDriverState *sclp_hds[MAX_SCLP_CONSOLES];
158 int win2k_install_hack = 0;
159 int singlestep = 0;
160 int smp_cpus = 1;
161 int max_cpus = 1;
162 int smp_cores = 1;
163 int smp_threads = 1;
164 int acpi_enabled = 1;
165 int no_hpet = 0;
166 int fd_bootchk = 1;
167 static int no_reboot;
168 int no_shutdown = 0;
169 int cursor_hide = 1;
170 int graphic_rotate = 0;
171 const char *watchdog;
172 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
173 int nb_option_roms;
174 int old_param = 0;
175 const char *qemu_name;
176 int alt_grab = 0;
177 int ctrl_grab = 0;
178 unsigned int nb_prom_envs = 0;
179 const char *prom_envs[MAX_PROM_ENVS];
180 int boot_menu;
181 bool boot_strict;
182 uint8_t *boot_splash_filedata;
183 size_t boot_splash_filedata_size;
184 uint8_t qemu_extra_params_fw[2];
185 int only_migratable; /* turn it off unless user states otherwise */
187 int icount_align_option;
189 /* The bytes in qemu_uuid are in the order specified by RFC4122, _not_ in the
190 * little-endian "wire format" described in the SMBIOS 2.6 specification.
192 QemuUUID qemu_uuid;
193 bool qemu_uuid_set;
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;
215 static int default_net = 1;
217 static struct {
218 const char *driver;
219 int *flag;
220 } default_list[] = {
221 { .driver = "isa-serial", .flag = &default_serial },
222 { .driver = "isa-parallel", .flag = &default_parallel },
223 { .driver = "isa-fdc", .flag = &default_floppy },
224 { .driver = "floppy", .flag = &default_floppy },
225 { .driver = "ide-cd", .flag = &default_cdrom },
226 { .driver = "ide-hd", .flag = &default_cdrom },
227 { .driver = "ide-drive", .flag = &default_cdrom },
228 { .driver = "scsi-cd", .flag = &default_cdrom },
229 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
230 { .driver = "virtio-serial", .flag = &default_virtcon },
231 { .driver = "VGA", .flag = &default_vga },
232 { .driver = "isa-vga", .flag = &default_vga },
233 { .driver = "cirrus-vga", .flag = &default_vga },
234 { .driver = "isa-cirrus-vga", .flag = &default_vga },
235 { .driver = "vmware-svga", .flag = &default_vga },
236 { .driver = "qxl-vga", .flag = &default_vga },
237 { .driver = "virtio-vga", .flag = &default_vga },
240 static QemuOptsList qemu_rtc_opts = {
241 .name = "rtc",
242 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
243 .desc = {
245 .name = "base",
246 .type = QEMU_OPT_STRING,
248 .name = "clock",
249 .type = QEMU_OPT_STRING,
251 .name = "driftfix",
252 .type = QEMU_OPT_STRING,
254 { /* end of list */ }
258 static QemuOptsList qemu_sandbox_opts = {
259 .name = "sandbox",
260 .implied_opt_name = "enable",
261 .head = QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts.head),
262 .desc = {
264 .name = "enable",
265 .type = QEMU_OPT_BOOL,
267 { /* end of list */ }
271 static QemuOptsList qemu_option_rom_opts = {
272 .name = "option-rom",
273 .implied_opt_name = "romfile",
274 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
275 .desc = {
277 .name = "bootindex",
278 .type = QEMU_OPT_NUMBER,
279 }, {
280 .name = "romfile",
281 .type = QEMU_OPT_STRING,
283 { /* end of list */ }
287 static QemuOptsList qemu_machine_opts = {
288 .name = "machine",
289 .implied_opt_name = "type",
290 .merge_lists = true,
291 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
292 .desc = {
294 * no elements => accept any
295 * sanity checking will happen later
296 * when setting machine properties
302 static QemuOptsList qemu_boot_opts = {
303 .name = "boot-opts",
304 .implied_opt_name = "order",
305 .merge_lists = true,
306 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
307 .desc = {
309 .name = "order",
310 .type = QEMU_OPT_STRING,
311 }, {
312 .name = "once",
313 .type = QEMU_OPT_STRING,
314 }, {
315 .name = "menu",
316 .type = QEMU_OPT_BOOL,
317 }, {
318 .name = "splash",
319 .type = QEMU_OPT_STRING,
320 }, {
321 .name = "splash-time",
322 .type = QEMU_OPT_STRING,
323 }, {
324 .name = "reboot-timeout",
325 .type = QEMU_OPT_STRING,
326 }, {
327 .name = "strict",
328 .type = QEMU_OPT_BOOL,
330 { /*End of list */ }
334 static QemuOptsList qemu_add_fd_opts = {
335 .name = "add-fd",
336 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
337 .desc = {
339 .name = "fd",
340 .type = QEMU_OPT_NUMBER,
341 .help = "file descriptor of which a duplicate is added to fd set",
343 .name = "set",
344 .type = QEMU_OPT_NUMBER,
345 .help = "ID of the fd set to add fd to",
347 .name = "opaque",
348 .type = QEMU_OPT_STRING,
349 .help = "free-form string used to describe fd",
351 { /* end of list */ }
355 static QemuOptsList qemu_object_opts = {
356 .name = "object",
357 .implied_opt_name = "qom-type",
358 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
359 .desc = {
364 static QemuOptsList qemu_tpmdev_opts = {
365 .name = "tpmdev",
366 .implied_opt_name = "type",
367 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
368 .desc = {
369 /* options are defined in the TPM backends */
370 { /* end of list */ }
374 static QemuOptsList qemu_realtime_opts = {
375 .name = "realtime",
376 .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
377 .desc = {
379 .name = "mlock",
380 .type = QEMU_OPT_BOOL,
382 { /* end of list */ }
386 static QemuOptsList qemu_msg_opts = {
387 .name = "msg",
388 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
389 .desc = {
391 .name = "timestamp",
392 .type = QEMU_OPT_BOOL,
394 { /* end of list */ }
398 static QemuOptsList qemu_name_opts = {
399 .name = "name",
400 .implied_opt_name = "guest",
401 .merge_lists = true,
402 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
403 .desc = {
405 .name = "guest",
406 .type = QEMU_OPT_STRING,
407 .help = "Sets the name of the guest.\n"
408 "This name will be displayed in the SDL window caption.\n"
409 "The name will also be used for the VNC server",
410 }, {
411 .name = "process",
412 .type = QEMU_OPT_STRING,
413 .help = "Sets the name of the QEMU process, as shown in top etc",
414 }, {
415 .name = "debug-threads",
416 .type = QEMU_OPT_BOOL,
417 .help = "When enabled, name the individual threads; defaults off.\n"
418 "NOTE: The thread names are for debugging and not a\n"
419 "stable API.",
421 { /* End of list */ }
425 static QemuOptsList qemu_mem_opts = {
426 .name = "memory",
427 .implied_opt_name = "size",
428 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
429 .merge_lists = true,
430 .desc = {
432 .name = "size",
433 .type = QEMU_OPT_SIZE,
436 .name = "slots",
437 .type = QEMU_OPT_NUMBER,
440 .name = "maxmem",
441 .type = QEMU_OPT_SIZE,
443 { /* end of list */ }
447 static QemuOptsList qemu_icount_opts = {
448 .name = "icount",
449 .implied_opt_name = "shift",
450 .merge_lists = true,
451 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
452 .desc = {
454 .name = "shift",
455 .type = QEMU_OPT_STRING,
456 }, {
457 .name = "align",
458 .type = QEMU_OPT_BOOL,
459 }, {
460 .name = "sleep",
461 .type = QEMU_OPT_BOOL,
462 }, {
463 .name = "rr",
464 .type = QEMU_OPT_STRING,
465 }, {
466 .name = "rrfile",
467 .type = QEMU_OPT_STRING,
469 { /* end of list */ }
473 static QemuOptsList qemu_semihosting_config_opts = {
474 .name = "semihosting-config",
475 .implied_opt_name = "enable",
476 .head = QTAILQ_HEAD_INITIALIZER(qemu_semihosting_config_opts.head),
477 .desc = {
479 .name = "enable",
480 .type = QEMU_OPT_BOOL,
481 }, {
482 .name = "target",
483 .type = QEMU_OPT_STRING,
484 }, {
485 .name = "arg",
486 .type = QEMU_OPT_STRING,
488 { /* end of list */ }
492 static QemuOptsList qemu_fw_cfg_opts = {
493 .name = "fw_cfg",
494 .implied_opt_name = "name",
495 .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
496 .desc = {
498 .name = "name",
499 .type = QEMU_OPT_STRING,
500 .help = "Sets the fw_cfg name of the blob to be inserted",
501 }, {
502 .name = "file",
503 .type = QEMU_OPT_STRING,
504 .help = "Sets the name of the file from which\n"
505 "the fw_cfg blob will be loaded",
506 }, {
507 .name = "string",
508 .type = QEMU_OPT_STRING,
509 .help = "Sets content of the blob to be inserted from a string",
511 { /* end of list */ }
515 #ifdef CONFIG_LIBISCSI
516 static QemuOptsList qemu_iscsi_opts = {
517 .name = "iscsi",
518 .head = QTAILQ_HEAD_INITIALIZER(qemu_iscsi_opts.head),
519 .desc = {
521 .name = "user",
522 .type = QEMU_OPT_STRING,
523 .help = "username for CHAP authentication to target",
525 .name = "password",
526 .type = QEMU_OPT_STRING,
527 .help = "password for CHAP authentication to target",
529 .name = "password-secret",
530 .type = QEMU_OPT_STRING,
531 .help = "ID of the secret providing password for CHAP "
532 "authentication to target",
534 .name = "header-digest",
535 .type = QEMU_OPT_STRING,
536 .help = "HeaderDigest setting. "
537 "{CRC32C|CRC32C-NONE|NONE-CRC32C|NONE}",
539 .name = "initiator-name",
540 .type = QEMU_OPT_STRING,
541 .help = "Initiator iqn name to use when connecting",
543 .name = "timeout",
544 .type = QEMU_OPT_NUMBER,
545 .help = "Request timeout in seconds (default 0 = no timeout)",
547 { /* end of list */ }
550 #endif
553 * Get machine options
555 * Returns: machine options (never null).
557 QemuOpts *qemu_get_machine_opts(void)
559 return qemu_find_opts_singleton("machine");
562 const char *qemu_get_vm_name(void)
564 return qemu_name;
567 static void res_free(void)
569 g_free(boot_splash_filedata);
570 boot_splash_filedata = NULL;
573 static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
575 const char *driver = qemu_opt_get(opts, "driver");
576 int i;
578 if (!driver)
579 return 0;
580 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
581 if (strcmp(default_list[i].driver, driver) != 0)
582 continue;
583 *(default_list[i].flag) = 0;
585 return 0;
588 /***********************************************************/
589 /* QEMU state */
591 static RunState current_run_state = RUN_STATE_PRELAUNCH;
593 /* We use RUN_STATE__MAX but any invalid value will do */
594 static RunState vmstop_requested = RUN_STATE__MAX;
595 static QemuMutex vmstop_lock;
597 typedef struct {
598 RunState from;
599 RunState to;
600 } RunStateTransition;
602 static const RunStateTransition runstate_transitions_def[] = {
603 /* from -> to */
604 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
605 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
606 { RUN_STATE_DEBUG, RUN_STATE_PRELAUNCH },
608 { RUN_STATE_INMIGRATE, RUN_STATE_INTERNAL_ERROR },
609 { RUN_STATE_INMIGRATE, RUN_STATE_IO_ERROR },
610 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
611 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
612 { RUN_STATE_INMIGRATE, RUN_STATE_SHUTDOWN },
613 { RUN_STATE_INMIGRATE, RUN_STATE_SUSPENDED },
614 { RUN_STATE_INMIGRATE, RUN_STATE_WATCHDOG },
615 { RUN_STATE_INMIGRATE, RUN_STATE_GUEST_PANICKED },
616 { RUN_STATE_INMIGRATE, RUN_STATE_FINISH_MIGRATE },
617 { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH },
618 { RUN_STATE_INMIGRATE, RUN_STATE_POSTMIGRATE },
619 { RUN_STATE_INMIGRATE, RUN_STATE_COLO },
621 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
622 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
623 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PRELAUNCH },
625 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
626 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
627 { RUN_STATE_IO_ERROR, RUN_STATE_PRELAUNCH },
629 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
630 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
631 { RUN_STATE_PAUSED, RUN_STATE_PRELAUNCH },
632 { RUN_STATE_PAUSED, RUN_STATE_COLO},
634 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
635 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
636 { RUN_STATE_POSTMIGRATE, RUN_STATE_PRELAUNCH },
638 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
639 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
640 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
642 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
643 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
644 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PRELAUNCH },
645 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_COLO},
647 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
648 { RUN_STATE_RESTORE_VM, RUN_STATE_PRELAUNCH },
650 { RUN_STATE_COLO, RUN_STATE_RUNNING },
652 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
653 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
654 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
655 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
656 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
657 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
658 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
659 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
660 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
661 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
662 { RUN_STATE_RUNNING, RUN_STATE_COLO},
664 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
666 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
667 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
668 { RUN_STATE_SHUTDOWN, RUN_STATE_PRELAUNCH },
670 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
671 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
672 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
673 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
674 { RUN_STATE_SUSPENDED, RUN_STATE_PRELAUNCH },
675 { RUN_STATE_SUSPENDED, RUN_STATE_COLO},
677 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
678 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
679 { RUN_STATE_WATCHDOG, RUN_STATE_PRELAUNCH },
680 { RUN_STATE_WATCHDOG, RUN_STATE_COLO},
682 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
683 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
684 { RUN_STATE_GUEST_PANICKED, RUN_STATE_PRELAUNCH },
686 { RUN_STATE__MAX, RUN_STATE__MAX },
689 static bool runstate_valid_transitions[RUN_STATE__MAX][RUN_STATE__MAX];
691 bool runstate_check(RunState state)
693 return current_run_state == state;
696 bool runstate_store(char *str, size_t size)
698 const char *state = RunState_lookup[current_run_state];
699 size_t len = strlen(state) + 1;
701 if (len > size) {
702 return false;
704 memcpy(str, state, len);
705 return true;
708 static void runstate_init(void)
710 const RunStateTransition *p;
712 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
713 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE__MAX; p++) {
714 runstate_valid_transitions[p->from][p->to] = true;
717 qemu_mutex_init(&vmstop_lock);
720 /* This function will abort() on invalid state transitions */
721 void runstate_set(RunState new_state)
723 assert(new_state < RUN_STATE__MAX);
725 if (current_run_state == new_state) {
726 return;
729 if (!runstate_valid_transitions[current_run_state][new_state]) {
730 error_report("invalid runstate transition: '%s' -> '%s'",
731 RunState_lookup[current_run_state],
732 RunState_lookup[new_state]);
733 abort();
735 trace_runstate_set(new_state);
736 current_run_state = new_state;
739 int runstate_is_running(void)
741 return runstate_check(RUN_STATE_RUNNING);
744 bool runstate_needs_reset(void)
746 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
747 runstate_check(RUN_STATE_SHUTDOWN);
750 StatusInfo *qmp_query_status(Error **errp)
752 StatusInfo *info = g_malloc0(sizeof(*info));
754 info->running = runstate_is_running();
755 info->singlestep = singlestep;
756 info->status = current_run_state;
758 return info;
761 static bool qemu_vmstop_requested(RunState *r)
763 qemu_mutex_lock(&vmstop_lock);
764 *r = vmstop_requested;
765 vmstop_requested = RUN_STATE__MAX;
766 qemu_mutex_unlock(&vmstop_lock);
767 return *r < RUN_STATE__MAX;
770 void qemu_system_vmstop_request_prepare(void)
772 qemu_mutex_lock(&vmstop_lock);
775 void qemu_system_vmstop_request(RunState state)
777 vmstop_requested = state;
778 qemu_mutex_unlock(&vmstop_lock);
779 qemu_notify_event();
782 void vm_start(void)
784 RunState requested;
786 qemu_vmstop_requested(&requested);
787 if (runstate_is_running() && requested == RUN_STATE__MAX) {
788 return;
791 /* Ensure that a STOP/RESUME pair of events is emitted if a
792 * vmstop request was pending. The BLOCK_IO_ERROR event, for
793 * example, according to documentation is always followed by
794 * the STOP event.
796 if (runstate_is_running()) {
797 qapi_event_send_stop(&error_abort);
798 } else {
799 replay_enable_events();
800 cpu_enable_ticks();
801 runstate_set(RUN_STATE_RUNNING);
802 vm_state_notify(1, RUN_STATE_RUNNING);
803 resume_all_vcpus();
806 qapi_event_send_resume(&error_abort);
810 /***********************************************************/
811 /* real time host monotonic timer */
813 static time_t qemu_time(void)
815 return qemu_clock_get_ms(QEMU_CLOCK_HOST) / 1000;
818 /***********************************************************/
819 /* host time/date access */
820 void qemu_get_timedate(struct tm *tm, int offset)
822 time_t ti = qemu_time();
824 ti += offset;
825 if (rtc_date_offset == -1) {
826 if (rtc_utc)
827 gmtime_r(&ti, tm);
828 else
829 localtime_r(&ti, tm);
830 } else {
831 ti -= rtc_date_offset;
832 gmtime_r(&ti, tm);
836 int qemu_timedate_diff(struct tm *tm)
838 time_t seconds;
840 if (rtc_date_offset == -1)
841 if (rtc_utc)
842 seconds = mktimegm(tm);
843 else {
844 struct tm tmp = *tm;
845 tmp.tm_isdst = -1; /* use timezone to figure it out */
846 seconds = mktime(&tmp);
848 else
849 seconds = mktimegm(tm) + rtc_date_offset;
851 return seconds - qemu_time();
854 static void configure_rtc_date_offset(const char *startdate, int legacy)
856 time_t rtc_start_date;
857 struct tm tm;
859 if (!strcmp(startdate, "now") && legacy) {
860 rtc_date_offset = -1;
861 } else {
862 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
863 &tm.tm_year,
864 &tm.tm_mon,
865 &tm.tm_mday,
866 &tm.tm_hour,
867 &tm.tm_min,
868 &tm.tm_sec) == 6) {
869 /* OK */
870 } else if (sscanf(startdate, "%d-%d-%d",
871 &tm.tm_year,
872 &tm.tm_mon,
873 &tm.tm_mday) == 3) {
874 tm.tm_hour = 0;
875 tm.tm_min = 0;
876 tm.tm_sec = 0;
877 } else {
878 goto date_fail;
880 tm.tm_year -= 1900;
881 tm.tm_mon--;
882 rtc_start_date = mktimegm(&tm);
883 if (rtc_start_date == -1) {
884 date_fail:
885 error_report("invalid date format");
886 error_printf("valid formats: "
887 "'2006-06-17T16:01:21' or '2006-06-17'\n");
888 exit(1);
890 rtc_date_offset = qemu_time() - rtc_start_date;
894 static void configure_rtc(QemuOpts *opts)
896 const char *value;
898 value = qemu_opt_get(opts, "base");
899 if (value) {
900 if (!strcmp(value, "utc")) {
901 rtc_utc = 1;
902 } else if (!strcmp(value, "localtime")) {
903 Error *blocker = NULL;
904 rtc_utc = 0;
905 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
906 "-rtc base=localtime");
907 replay_add_blocker(blocker);
908 } else {
909 configure_rtc_date_offset(value, 0);
912 value = qemu_opt_get(opts, "clock");
913 if (value) {
914 if (!strcmp(value, "host")) {
915 rtc_clock = QEMU_CLOCK_HOST;
916 } else if (!strcmp(value, "rt")) {
917 rtc_clock = QEMU_CLOCK_REALTIME;
918 } else if (!strcmp(value, "vm")) {
919 rtc_clock = QEMU_CLOCK_VIRTUAL;
920 } else {
921 error_report("invalid option value '%s'", value);
922 exit(1);
925 value = qemu_opt_get(opts, "driftfix");
926 if (value) {
927 if (!strcmp(value, "slew")) {
928 static GlobalProperty slew_lost_ticks = {
929 .driver = "mc146818rtc",
930 .property = "lost_tick_policy",
931 .value = "slew",
934 qdev_prop_register_global(&slew_lost_ticks);
935 } else if (!strcmp(value, "none")) {
936 /* discard is default */
937 } else {
938 error_report("invalid option value '%s'", value);
939 exit(1);
944 /***********************************************************/
945 /* Bluetooth support */
946 static int nb_hcis;
947 static int cur_hci;
948 static struct HCIInfo *hci_table[MAX_NICS];
950 struct HCIInfo *qemu_next_hci(void)
952 if (cur_hci == nb_hcis)
953 return &null_hci;
955 return hci_table[cur_hci++];
958 static int bt_hci_parse(const char *str)
960 struct HCIInfo *hci;
961 bdaddr_t bdaddr;
963 if (nb_hcis >= MAX_NICS) {
964 error_report("too many bluetooth HCIs (max %i)", MAX_NICS);
965 return -1;
968 hci = hci_init(str);
969 if (!hci)
970 return -1;
972 bdaddr.b[0] = 0x52;
973 bdaddr.b[1] = 0x54;
974 bdaddr.b[2] = 0x00;
975 bdaddr.b[3] = 0x12;
976 bdaddr.b[4] = 0x34;
977 bdaddr.b[5] = 0x56 + nb_hcis;
978 hci->bdaddr_set(hci, bdaddr.b);
980 hci_table[nb_hcis++] = hci;
982 return 0;
985 static void bt_vhci_add(int vlan_id)
987 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
989 if (!vlan->slave)
990 error_report("warning: adding a VHCI to an empty scatternet %i",
991 vlan_id);
993 bt_vhci_init(bt_new_hci(vlan));
996 static struct bt_device_s *bt_device_add(const char *opt)
998 struct bt_scatternet_s *vlan;
999 int vlan_id = 0;
1000 char *endp = strstr(opt, ",vlan=");
1001 int len = (endp ? endp - opt : strlen(opt)) + 1;
1002 char devname[10];
1004 pstrcpy(devname, MIN(sizeof(devname), len), opt);
1006 if (endp) {
1007 vlan_id = strtol(endp + 6, &endp, 0);
1008 if (*endp) {
1009 error_report("unrecognised bluetooth vlan Id");
1010 return 0;
1014 vlan = qemu_find_bt_vlan(vlan_id);
1016 if (!vlan->slave)
1017 error_report("warning: adding a slave device to an empty scatternet %i",
1018 vlan_id);
1020 if (!strcmp(devname, "keyboard"))
1021 return bt_keyboard_init(vlan);
1023 error_report("unsupported bluetooth device '%s'", devname);
1024 return 0;
1027 static int bt_parse(const char *opt)
1029 const char *endp, *p;
1030 int vlan;
1032 if (strstart(opt, "hci", &endp)) {
1033 if (!*endp || *endp == ',') {
1034 if (*endp)
1035 if (!strstart(endp, ",vlan=", 0))
1036 opt = endp + 1;
1038 return bt_hci_parse(opt);
1040 } else if (strstart(opt, "vhci", &endp)) {
1041 if (!*endp || *endp == ',') {
1042 if (*endp) {
1043 if (strstart(endp, ",vlan=", &p)) {
1044 vlan = strtol(p, (char **) &endp, 0);
1045 if (*endp) {
1046 error_report("bad scatternet '%s'", p);
1047 return 1;
1049 } else {
1050 error_report("bad parameter '%s'", endp + 1);
1051 return 1;
1053 } else
1054 vlan = 0;
1056 bt_vhci_add(vlan);
1057 return 0;
1059 } else if (strstart(opt, "device:", &endp))
1060 return !bt_device_add(endp);
1062 error_report("bad bluetooth parameter '%s'", opt);
1063 return 1;
1066 static int parse_sandbox(void *opaque, QemuOpts *opts, Error **errp)
1068 /* FIXME: change this to true for 1.3 */
1069 if (qemu_opt_get_bool(opts, "enable", false)) {
1070 #ifdef CONFIG_SECCOMP
1071 if (seccomp_start() < 0) {
1072 error_report("failed to install seccomp syscall filter "
1073 "in the kernel");
1074 return -1;
1076 #else
1077 error_report("seccomp support is disabled");
1078 return -1;
1079 #endif
1082 return 0;
1085 static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
1087 const char *proc_name;
1089 if (qemu_opt_get(opts, "debug-threads")) {
1090 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
1092 qemu_name = qemu_opt_get(opts, "guest");
1094 proc_name = qemu_opt_get(opts, "process");
1095 if (proc_name) {
1096 os_set_proc_name(proc_name);
1099 return 0;
1102 bool defaults_enabled(void)
1104 return has_defaults;
1107 #ifndef _WIN32
1108 static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1110 int fd, dupfd, flags;
1111 int64_t fdset_id;
1112 const char *fd_opaque = NULL;
1113 AddfdInfo *fdinfo;
1115 fd = qemu_opt_get_number(opts, "fd", -1);
1116 fdset_id = qemu_opt_get_number(opts, "set", -1);
1117 fd_opaque = qemu_opt_get(opts, "opaque");
1119 if (fd < 0) {
1120 error_report("fd option is required and must be non-negative");
1121 return -1;
1124 if (fd <= STDERR_FILENO) {
1125 error_report("fd cannot be a standard I/O stream");
1126 return -1;
1130 * All fds inherited across exec() necessarily have FD_CLOEXEC
1131 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1133 flags = fcntl(fd, F_GETFD);
1134 if (flags == -1 || (flags & FD_CLOEXEC)) {
1135 error_report("fd is not valid or already in use");
1136 return -1;
1139 if (fdset_id < 0) {
1140 error_report("set option is required and must be non-negative");
1141 return -1;
1144 #ifdef F_DUPFD_CLOEXEC
1145 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1146 #else
1147 dupfd = dup(fd);
1148 if (dupfd != -1) {
1149 qemu_set_cloexec(dupfd);
1151 #endif
1152 if (dupfd == -1) {
1153 error_report("error duplicating fd: %s", strerror(errno));
1154 return -1;
1157 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1158 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
1159 &error_abort);
1160 g_free(fdinfo);
1162 return 0;
1165 static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1167 int fd;
1169 fd = qemu_opt_get_number(opts, "fd", -1);
1170 close(fd);
1172 return 0;
1174 #endif
1176 /***********************************************************/
1177 /* QEMU Block devices */
1179 #define HD_OPTS "media=disk"
1180 #define CDROM_OPTS "media=cdrom"
1181 #define FD_OPTS ""
1182 #define PFLASH_OPTS ""
1183 #define MTD_OPTS ""
1184 #define SD_OPTS ""
1186 static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
1188 BlockInterfaceType *block_default_type = opaque;
1190 return drive_new(opts, *block_default_type) == NULL;
1193 static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
1195 if (qemu_opt_get(opts, "snapshot") == NULL) {
1196 qemu_opt_set(opts, "snapshot", "on", &error_abort);
1198 return 0;
1201 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1202 int index, const char *optstr)
1204 QemuOpts *opts;
1205 DriveInfo *dinfo;
1207 if (!enable || drive_get_by_index(type, index)) {
1208 return;
1211 opts = drive_add(type, index, NULL, optstr);
1212 if (snapshot) {
1213 drive_enable_snapshot(NULL, opts, NULL);
1216 dinfo = drive_new(opts, type);
1217 if (!dinfo) {
1218 exit(1);
1220 dinfo->is_default = true;
1224 static QemuOptsList qemu_smp_opts = {
1225 .name = "smp-opts",
1226 .implied_opt_name = "cpus",
1227 .merge_lists = true,
1228 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
1229 .desc = {
1231 .name = "cpus",
1232 .type = QEMU_OPT_NUMBER,
1233 }, {
1234 .name = "sockets",
1235 .type = QEMU_OPT_NUMBER,
1236 }, {
1237 .name = "cores",
1238 .type = QEMU_OPT_NUMBER,
1239 }, {
1240 .name = "threads",
1241 .type = QEMU_OPT_NUMBER,
1242 }, {
1243 .name = "maxcpus",
1244 .type = QEMU_OPT_NUMBER,
1246 { /*End of list */ }
1250 static void smp_parse(QemuOpts *opts)
1252 if (opts) {
1253 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
1254 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
1255 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
1256 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
1258 /* compute missing values, prefer sockets over cores over threads */
1259 if (cpus == 0 || sockets == 0) {
1260 sockets = sockets > 0 ? sockets : 1;
1261 cores = cores > 0 ? cores : 1;
1262 threads = threads > 0 ? threads : 1;
1263 if (cpus == 0) {
1264 cpus = cores * threads * sockets;
1266 } else if (cores == 0) {
1267 threads = threads > 0 ? threads : 1;
1268 cores = cpus / (sockets * threads);
1269 cores = cores > 0 ? cores : 1;
1270 } else if (threads == 0) {
1271 threads = cpus / (cores * sockets);
1272 threads = threads > 0 ? threads : 1;
1273 } else if (sockets * cores * threads < cpus) {
1274 error_report("cpu topology: "
1275 "sockets (%u) * cores (%u) * threads (%u) < "
1276 "smp_cpus (%u)",
1277 sockets, cores, threads, cpus);
1278 exit(1);
1281 max_cpus = qemu_opt_get_number(opts, "maxcpus", cpus);
1283 if (max_cpus < cpus) {
1284 error_report("maxcpus must be equal to or greater than smp");
1285 exit(1);
1288 if (sockets * cores * threads > max_cpus) {
1289 error_report("cpu topology: "
1290 "sockets (%u) * cores (%u) * threads (%u) > "
1291 "maxcpus (%u)",
1292 sockets, cores, threads, max_cpus);
1293 exit(1);
1296 smp_cpus = cpus;
1297 smp_cores = cores;
1298 smp_threads = threads;
1301 if (smp_cpus > 1) {
1302 Error *blocker = NULL;
1303 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
1304 replay_add_blocker(blocker);
1308 static void realtime_init(void)
1310 if (enable_mlock) {
1311 if (os_mlock() < 0) {
1312 error_report("locking memory failed");
1313 exit(1);
1319 static void configure_msg(QemuOpts *opts)
1321 enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
1324 /***********************************************************/
1325 /* Semihosting */
1327 typedef struct SemihostingConfig {
1328 bool enabled;
1329 SemihostingTarget target;
1330 const char **argv;
1331 int argc;
1332 const char *cmdline; /* concatenated argv */
1333 } SemihostingConfig;
1335 static SemihostingConfig semihosting;
1337 bool semihosting_enabled(void)
1339 return semihosting.enabled;
1342 SemihostingTarget semihosting_get_target(void)
1344 return semihosting.target;
1347 const char *semihosting_get_arg(int i)
1349 if (i >= semihosting.argc) {
1350 return NULL;
1352 return semihosting.argv[i];
1355 int semihosting_get_argc(void)
1357 return semihosting.argc;
1360 const char *semihosting_get_cmdline(void)
1362 if (semihosting.cmdline == NULL && semihosting.argc > 0) {
1363 semihosting.cmdline = g_strjoinv(" ", (gchar **)semihosting.argv);
1365 return semihosting.cmdline;
1368 static int add_semihosting_arg(void *opaque,
1369 const char *name, const char *val,
1370 Error **errp)
1372 SemihostingConfig *s = opaque;
1373 if (strcmp(name, "arg") == 0) {
1374 s->argc++;
1375 /* one extra element as g_strjoinv() expects NULL-terminated array */
1376 s->argv = g_realloc(s->argv, (s->argc + 1) * sizeof(void *));
1377 s->argv[s->argc - 1] = val;
1378 s->argv[s->argc] = NULL;
1380 return 0;
1383 /* Use strings passed via -kernel/-append to initialize semihosting.argv[] */
1384 static inline void semihosting_arg_fallback(const char *file, const char *cmd)
1386 char *cmd_token;
1388 /* argv[0] */
1389 add_semihosting_arg(&semihosting, "arg", file, NULL);
1391 /* split -append and initialize argv[1..n] */
1392 cmd_token = strtok(g_strdup(cmd), " ");
1393 while (cmd_token) {
1394 add_semihosting_arg(&semihosting, "arg", cmd_token, NULL);
1395 cmd_token = strtok(NULL, " ");
1399 /* Now we still need this for compatibility with XEN. */
1400 bool has_igd_gfx_passthru;
1401 static void igd_gfx_passthru(void)
1403 has_igd_gfx_passthru = current_machine->igd_gfx_passthru;
1406 /***********************************************************/
1407 /* USB devices */
1409 static int usb_device_add(const char *devname)
1411 USBDevice *dev = NULL;
1412 #ifndef CONFIG_LINUX
1413 const char *p;
1414 #endif
1416 if (!machine_usb(current_machine)) {
1417 return -1;
1420 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1421 dev = usbdevice_create(devname);
1422 if (dev)
1423 goto done;
1425 /* the other ones */
1426 #ifndef CONFIG_LINUX
1427 /* only the linux version is qdev-ified, usb-bsd still needs this */
1428 if (strstart(devname, "host:", &p)) {
1429 dev = usb_host_device_open(usb_bus_find(-1), p);
1431 #endif
1432 if (!dev)
1433 return -1;
1435 done:
1436 return 0;
1439 static int usb_device_del(const char *devname)
1441 int bus_num, addr;
1442 const char *p;
1444 if (strstart(devname, "host:", &p)) {
1445 return -1;
1448 if (!machine_usb(current_machine)) {
1449 return -1;
1452 p = strchr(devname, '.');
1453 if (!p)
1454 return -1;
1455 bus_num = strtoul(devname, NULL, 0);
1456 addr = strtoul(p + 1, NULL, 0);
1458 return usb_device_delete_addr(bus_num, addr);
1461 static int usb_parse(const char *cmdline)
1463 int r;
1464 r = usb_device_add(cmdline);
1465 if (r < 0) {
1466 error_report("could not add USB device '%s'", cmdline);
1468 return r;
1471 void hmp_usb_add(Monitor *mon, const QDict *qdict)
1473 const char *devname = qdict_get_str(qdict, "devname");
1474 if (usb_device_add(devname) < 0) {
1475 error_report("could not add USB device '%s'", devname);
1479 void hmp_usb_del(Monitor *mon, const QDict *qdict)
1481 const char *devname = qdict_get_str(qdict, "devname");
1482 if (usb_device_del(devname) < 0) {
1483 error_report("could not delete USB device '%s'", devname);
1487 /***********************************************************/
1488 /* machine registration */
1490 MachineState *current_machine;
1492 static MachineClass *find_machine(const char *name)
1494 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1495 MachineClass *mc = NULL;
1497 for (el = machines; el; el = el->next) {
1498 MachineClass *temp = el->data;
1500 if (!strcmp(temp->name, name)) {
1501 mc = temp;
1502 break;
1504 if (temp->alias &&
1505 !strcmp(temp->alias, name)) {
1506 mc = temp;
1507 break;
1511 g_slist_free(machines);
1512 return mc;
1515 MachineClass *find_default_machine(void)
1517 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1518 MachineClass *mc = NULL;
1520 for (el = machines; el; el = el->next) {
1521 MachineClass *temp = el->data;
1523 if (temp->is_default) {
1524 mc = temp;
1525 break;
1529 g_slist_free(machines);
1530 return mc;
1533 MachineInfoList *qmp_query_machines(Error **errp)
1535 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1536 MachineInfoList *mach_list = NULL;
1538 for (el = machines; el; el = el->next) {
1539 MachineClass *mc = el->data;
1540 MachineInfoList *entry;
1541 MachineInfo *info;
1543 info = g_malloc0(sizeof(*info));
1544 if (mc->is_default) {
1545 info->has_is_default = true;
1546 info->is_default = true;
1549 if (mc->alias) {
1550 info->has_alias = true;
1551 info->alias = g_strdup(mc->alias);
1554 info->name = g_strdup(mc->name);
1555 info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
1556 info->hotpluggable_cpus = !!mc->query_hotpluggable_cpus;
1558 entry = g_malloc0(sizeof(*entry));
1559 entry->value = info;
1560 entry->next = mach_list;
1561 mach_list = entry;
1564 g_slist_free(machines);
1565 return mach_list;
1568 static int machine_help_func(QemuOpts *opts, MachineState *machine)
1570 ObjectProperty *prop;
1571 ObjectPropertyIterator iter;
1573 if (!qemu_opt_has_help_opt(opts)) {
1574 return 0;
1577 object_property_iter_init(&iter, OBJECT(machine));
1578 while ((prop = object_property_iter_next(&iter))) {
1579 if (!prop->set) {
1580 continue;
1583 error_printf("%s.%s=%s", MACHINE_GET_CLASS(machine)->name,
1584 prop->name, prop->type);
1585 if (prop->description) {
1586 error_printf(" (%s)\n", prop->description);
1587 } else {
1588 error_printf("\n");
1592 return 1;
1595 /***********************************************************/
1596 /* main execution loop */
1598 struct vm_change_state_entry {
1599 VMChangeStateHandler *cb;
1600 void *opaque;
1601 QLIST_ENTRY (vm_change_state_entry) entries;
1604 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1606 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1607 void *opaque)
1609 VMChangeStateEntry *e;
1611 e = g_malloc0(sizeof (*e));
1613 e->cb = cb;
1614 e->opaque = opaque;
1615 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1616 return e;
1619 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1621 QLIST_REMOVE (e, entries);
1622 g_free (e);
1625 void vm_state_notify(int running, RunState state)
1627 VMChangeStateEntry *e, *next;
1629 trace_vm_state_notify(running, state);
1631 QLIST_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
1632 e->cb(e->opaque, running, state);
1636 static int reset_requested;
1637 static int shutdown_requested, shutdown_signal = -1;
1638 static pid_t shutdown_pid;
1639 static int powerdown_requested;
1640 static int debug_requested;
1641 static int suspend_requested;
1642 static WakeupReason wakeup_reason;
1643 static NotifierList powerdown_notifiers =
1644 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
1645 static NotifierList suspend_notifiers =
1646 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1647 static NotifierList wakeup_notifiers =
1648 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1649 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
1651 int qemu_shutdown_requested_get(void)
1653 return shutdown_requested;
1656 int qemu_reset_requested_get(void)
1658 return reset_requested;
1661 static int qemu_shutdown_requested(void)
1663 return atomic_xchg(&shutdown_requested, 0);
1666 static void qemu_kill_report(void)
1668 if (!qtest_driver() && shutdown_signal != -1) {
1669 if (shutdown_pid == 0) {
1670 /* This happens for eg ^C at the terminal, so it's worth
1671 * avoiding printing an odd message in that case.
1673 error_report("terminating on signal %d", shutdown_signal);
1674 } else {
1675 char *shutdown_cmd = qemu_get_pid_name(shutdown_pid);
1677 error_report("terminating on signal %d from pid " FMT_pid " (%s)",
1678 shutdown_signal, shutdown_pid,
1679 shutdown_cmd ? shutdown_cmd : "<unknown process>");
1680 g_free(shutdown_cmd);
1682 shutdown_signal = -1;
1686 static int qemu_reset_requested(void)
1688 int r = reset_requested;
1689 if (r && replay_checkpoint(CHECKPOINT_RESET_REQUESTED)) {
1690 reset_requested = 0;
1691 return r;
1693 return false;
1696 static int qemu_suspend_requested(void)
1698 int r = suspend_requested;
1699 if (r && replay_checkpoint(CHECKPOINT_SUSPEND_REQUESTED)) {
1700 suspend_requested = 0;
1701 return r;
1703 return false;
1706 static WakeupReason qemu_wakeup_requested(void)
1708 return wakeup_reason;
1711 static int qemu_powerdown_requested(void)
1713 int r = powerdown_requested;
1714 powerdown_requested = 0;
1715 return r;
1718 static int qemu_debug_requested(void)
1720 int r = debug_requested;
1721 debug_requested = 0;
1722 return r;
1725 void qemu_system_reset(bool report)
1727 MachineClass *mc;
1729 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1731 cpu_synchronize_all_states();
1733 if (mc && mc->reset) {
1734 mc->reset();
1735 } else {
1736 qemu_devices_reset();
1738 if (report) {
1739 qapi_event_send_reset(&error_abort);
1741 cpu_synchronize_all_post_reset();
1744 void qemu_system_guest_panicked(void)
1746 if (current_cpu) {
1747 current_cpu->crash_occurred = true;
1749 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE, &error_abort);
1750 vm_stop(RUN_STATE_GUEST_PANICKED);
1751 if (!no_shutdown) {
1752 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_POWEROFF,
1753 &error_abort);
1754 qemu_system_shutdown_request();
1758 void qemu_system_reset_request(void)
1760 if (no_reboot) {
1761 shutdown_requested = 1;
1762 } else {
1763 reset_requested = 1;
1765 cpu_stop_current();
1766 qemu_notify_event();
1769 static void qemu_system_suspend(void)
1771 pause_all_vcpus();
1772 notifier_list_notify(&suspend_notifiers, NULL);
1773 runstate_set(RUN_STATE_SUSPENDED);
1774 qapi_event_send_suspend(&error_abort);
1777 void qemu_system_suspend_request(void)
1779 if (runstate_check(RUN_STATE_SUSPENDED)) {
1780 return;
1782 suspend_requested = 1;
1783 cpu_stop_current();
1784 qemu_notify_event();
1787 void qemu_register_suspend_notifier(Notifier *notifier)
1789 notifier_list_add(&suspend_notifiers, notifier);
1792 void qemu_system_wakeup_request(WakeupReason reason)
1794 trace_system_wakeup_request(reason);
1796 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1797 return;
1799 if (!(wakeup_reason_mask & (1 << reason))) {
1800 return;
1802 runstate_set(RUN_STATE_RUNNING);
1803 wakeup_reason = reason;
1804 qemu_notify_event();
1807 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1809 if (enabled) {
1810 wakeup_reason_mask |= (1 << reason);
1811 } else {
1812 wakeup_reason_mask &= ~(1 << reason);
1816 void qemu_register_wakeup_notifier(Notifier *notifier)
1818 notifier_list_add(&wakeup_notifiers, notifier);
1821 void qemu_system_killed(int signal, pid_t pid)
1823 shutdown_signal = signal;
1824 shutdown_pid = pid;
1825 no_shutdown = 0;
1827 /* Cannot call qemu_system_shutdown_request directly because
1828 * we are in a signal handler.
1830 shutdown_requested = 1;
1831 qemu_notify_event();
1834 void qemu_system_shutdown_request(void)
1836 trace_qemu_system_shutdown_request();
1837 replay_shutdown_request();
1838 shutdown_requested = 1;
1839 qemu_notify_event();
1842 static void qemu_system_powerdown(void)
1844 qapi_event_send_powerdown(&error_abort);
1845 notifier_list_notify(&powerdown_notifiers, NULL);
1848 void qemu_system_powerdown_request(void)
1850 trace_qemu_system_powerdown_request();
1851 powerdown_requested = 1;
1852 qemu_notify_event();
1855 void qemu_register_powerdown_notifier(Notifier *notifier)
1857 notifier_list_add(&powerdown_notifiers, notifier);
1860 void qemu_system_debug_request(void)
1862 debug_requested = 1;
1863 qemu_notify_event();
1866 static bool main_loop_should_exit(void)
1868 RunState r;
1869 if (qemu_debug_requested()) {
1870 vm_stop(RUN_STATE_DEBUG);
1872 if (qemu_suspend_requested()) {
1873 qemu_system_suspend();
1875 if (qemu_shutdown_requested()) {
1876 qemu_kill_report();
1877 qapi_event_send_shutdown(&error_abort);
1878 if (no_shutdown) {
1879 vm_stop(RUN_STATE_SHUTDOWN);
1880 } else {
1881 return true;
1884 if (qemu_reset_requested()) {
1885 pause_all_vcpus();
1886 qemu_system_reset(VMRESET_REPORT);
1887 resume_all_vcpus();
1888 if (!runstate_check(RUN_STATE_RUNNING) &&
1889 !runstate_check(RUN_STATE_INMIGRATE)) {
1890 runstate_set(RUN_STATE_PRELAUNCH);
1893 if (qemu_wakeup_requested()) {
1894 pause_all_vcpus();
1895 qemu_system_reset(VMRESET_SILENT);
1896 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
1897 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
1898 resume_all_vcpus();
1899 qapi_event_send_wakeup(&error_abort);
1901 if (qemu_powerdown_requested()) {
1902 qemu_system_powerdown();
1904 if (qemu_vmstop_requested(&r)) {
1905 vm_stop(r);
1907 return false;
1910 static void main_loop(void)
1912 bool nonblocking;
1913 int last_io = 0;
1914 #ifdef CONFIG_PROFILER
1915 int64_t ti;
1916 #endif
1917 do {
1918 nonblocking = tcg_enabled() && last_io > 0;
1919 #ifdef CONFIG_PROFILER
1920 ti = profile_getclock();
1921 #endif
1922 last_io = main_loop_wait(nonblocking);
1923 #ifdef CONFIG_PROFILER
1924 dev_time += profile_getclock() - ti;
1925 #endif
1926 } while (!main_loop_should_exit());
1929 static void version(void)
1931 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION "\n"
1932 QEMU_COPYRIGHT "\n");
1935 static void help(int exitcode)
1937 version();
1938 printf("usage: %s [options] [disk_image]\n\n"
1939 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1940 error_get_progname());
1942 #define QEMU_OPTIONS_GENERATE_HELP
1943 #include "qemu-options-wrapper.h"
1945 printf("\nDuring emulation, the following keys are useful:\n"
1946 "ctrl-alt-f toggle full screen\n"
1947 "ctrl-alt-n switch to virtual console 'n'\n"
1948 "ctrl-alt toggle mouse and keyboard grab\n"
1949 "\n"
1950 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1952 exit(exitcode);
1955 #define HAS_ARG 0x0001
1957 typedef struct QEMUOption {
1958 const char *name;
1959 int flags;
1960 int index;
1961 uint32_t arch_mask;
1962 } QEMUOption;
1964 static const QEMUOption qemu_options[] = {
1965 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1966 #define QEMU_OPTIONS_GENERATE_OPTIONS
1967 #include "qemu-options-wrapper.h"
1968 { NULL },
1971 typedef struct VGAInterfaceInfo {
1972 const char *opt_name; /* option name */
1973 const char *name; /* human-readable name */
1974 /* Class names indicating that support is available.
1975 * If no class is specified, the interface is always available */
1976 const char *class_names[2];
1977 } VGAInterfaceInfo;
1979 static VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
1980 [VGA_NONE] = {
1981 .opt_name = "none",
1983 [VGA_STD] = {
1984 .opt_name = "std",
1985 .name = "standard VGA",
1986 .class_names = { "VGA", "isa-vga" },
1988 [VGA_CIRRUS] = {
1989 .opt_name = "cirrus",
1990 .name = "Cirrus VGA",
1991 .class_names = { "cirrus-vga", "isa-cirrus-vga" },
1993 [VGA_VMWARE] = {
1994 .opt_name = "vmware",
1995 .name = "VMWare SVGA",
1996 .class_names = { "vmware-svga" },
1998 [VGA_VIRTIO] = {
1999 .opt_name = "virtio",
2000 .name = "Virtio VGA",
2001 .class_names = { "virtio-vga" },
2003 [VGA_QXL] = {
2004 .opt_name = "qxl",
2005 .name = "QXL VGA",
2006 .class_names = { "qxl-vga" },
2008 [VGA_TCX] = {
2009 .opt_name = "tcx",
2010 .name = "TCX framebuffer",
2011 .class_names = { "SUNW,tcx" },
2013 [VGA_CG3] = {
2014 .opt_name = "cg3",
2015 .name = "CG3 framebuffer",
2016 .class_names = { "cgthree" },
2018 [VGA_XENFB] = {
2019 .opt_name = "xenfb",
2023 static bool vga_interface_available(VGAInterfaceType t)
2025 VGAInterfaceInfo *ti = &vga_interfaces[t];
2027 assert(t < VGA_TYPE_MAX);
2028 return !ti->class_names[0] ||
2029 object_class_by_name(ti->class_names[0]) ||
2030 object_class_by_name(ti->class_names[1]);
2033 static void select_vgahw(const char *p)
2035 const char *opts;
2036 int t;
2038 assert(vga_interface_type == VGA_NONE);
2039 for (t = 0; t < VGA_TYPE_MAX; t++) {
2040 VGAInterfaceInfo *ti = &vga_interfaces[t];
2041 if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
2042 if (!vga_interface_available(t)) {
2043 error_report("%s not available", ti->name);
2044 exit(1);
2046 vga_interface_type = t;
2047 break;
2050 if (t == VGA_TYPE_MAX) {
2051 invalid_vga:
2052 error_report("unknown vga type: %s", p);
2053 exit(1);
2055 while (*opts) {
2056 const char *nextopt;
2058 if (strstart(opts, ",retrace=", &nextopt)) {
2059 opts = nextopt;
2060 if (strstart(opts, "dumb", &nextopt))
2061 vga_retrace_method = VGA_RETRACE_DUMB;
2062 else if (strstart(opts, "precise", &nextopt))
2063 vga_retrace_method = VGA_RETRACE_PRECISE;
2064 else goto invalid_vga;
2065 } else goto invalid_vga;
2066 opts = nextopt;
2070 typedef enum DisplayType {
2071 DT_DEFAULT,
2072 DT_CURSES,
2073 DT_SDL,
2074 DT_COCOA,
2075 DT_GTK,
2076 DT_NONE,
2077 } DisplayType;
2079 static DisplayType select_display(const char *p)
2081 const char *opts;
2082 DisplayType display = DT_DEFAULT;
2084 if (strstart(p, "sdl", &opts)) {
2085 #ifdef CONFIG_SDL
2086 display = DT_SDL;
2087 while (*opts) {
2088 const char *nextopt;
2090 if (strstart(opts, ",frame=", &nextopt)) {
2091 opts = nextopt;
2092 if (strstart(opts, "on", &nextopt)) {
2093 no_frame = 0;
2094 } else if (strstart(opts, "off", &nextopt)) {
2095 no_frame = 1;
2096 } else {
2097 goto invalid_sdl_args;
2099 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
2100 opts = nextopt;
2101 if (strstart(opts, "on", &nextopt)) {
2102 alt_grab = 1;
2103 } else if (strstart(opts, "off", &nextopt)) {
2104 alt_grab = 0;
2105 } else {
2106 goto invalid_sdl_args;
2108 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2109 opts = nextopt;
2110 if (strstart(opts, "on", &nextopt)) {
2111 ctrl_grab = 1;
2112 } else if (strstart(opts, "off", &nextopt)) {
2113 ctrl_grab = 0;
2114 } else {
2115 goto invalid_sdl_args;
2117 } else if (strstart(opts, ",window_close=", &nextopt)) {
2118 opts = nextopt;
2119 if (strstart(opts, "on", &nextopt)) {
2120 no_quit = 0;
2121 } else if (strstart(opts, "off", &nextopt)) {
2122 no_quit = 1;
2123 } else {
2124 goto invalid_sdl_args;
2126 } else if (strstart(opts, ",gl=", &nextopt)) {
2127 opts = nextopt;
2128 if (strstart(opts, "on", &nextopt)) {
2129 request_opengl = 1;
2130 } else if (strstart(opts, "off", &nextopt)) {
2131 request_opengl = 0;
2132 } else {
2133 goto invalid_sdl_args;
2135 } else {
2136 invalid_sdl_args:
2137 error_report("invalid SDL option string");
2138 exit(1);
2140 opts = nextopt;
2142 #else
2143 error_report("SDL support is disabled");
2144 exit(1);
2145 #endif
2146 } else if (strstart(p, "vnc", &opts)) {
2147 if (*opts == '=') {
2148 vnc_parse(opts + 1, &error_fatal);
2149 } else {
2150 error_report("VNC requires a display argument vnc=<display>");
2151 exit(1);
2153 } else if (strstart(p, "curses", &opts)) {
2154 #ifdef CONFIG_CURSES
2155 display = DT_CURSES;
2156 #else
2157 error_report("curses support is disabled");
2158 exit(1);
2159 #endif
2160 } else if (strstart(p, "gtk", &opts)) {
2161 #ifdef CONFIG_GTK
2162 display = DT_GTK;
2163 while (*opts) {
2164 const char *nextopt;
2166 if (strstart(opts, ",grab_on_hover=", &nextopt)) {
2167 opts = nextopt;
2168 if (strstart(opts, "on", &nextopt)) {
2169 grab_on_hover = true;
2170 } else if (strstart(opts, "off", &nextopt)) {
2171 grab_on_hover = false;
2172 } else {
2173 goto invalid_gtk_args;
2175 } else if (strstart(opts, ",gl=", &nextopt)) {
2176 opts = nextopt;
2177 if (strstart(opts, "on", &nextopt)) {
2178 request_opengl = 1;
2179 } else if (strstart(opts, "off", &nextopt)) {
2180 request_opengl = 0;
2181 } else {
2182 goto invalid_gtk_args;
2184 } else {
2185 invalid_gtk_args:
2186 error_report("invalid GTK option string");
2187 exit(1);
2189 opts = nextopt;
2191 #else
2192 error_report("GTK support is disabled");
2193 exit(1);
2194 #endif
2195 } else if (strstart(p, "none", &opts)) {
2196 display = DT_NONE;
2197 } else {
2198 error_report("unknown display type");
2199 exit(1);
2202 return display;
2205 static int balloon_parse(const char *arg)
2207 QemuOpts *opts;
2209 if (strcmp(arg, "none") == 0) {
2210 return 0;
2213 if (!strncmp(arg, "virtio", 6)) {
2214 if (arg[6] == ',') {
2215 /* have params -> parse them */
2216 opts = qemu_opts_parse_noisily(qemu_find_opts("device"), arg + 7,
2217 false);
2218 if (!opts)
2219 return -1;
2220 } else {
2221 /* create empty opts */
2222 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2223 &error_abort);
2225 qemu_opt_set(opts, "driver", "virtio-balloon", &error_abort);
2226 return 0;
2229 return -1;
2232 char *qemu_find_file(int type, const char *name)
2234 int i;
2235 const char *subdir;
2236 char *buf;
2238 /* Try the name as a straight path first */
2239 if (access(name, R_OK) == 0) {
2240 trace_load_file(name, name);
2241 return g_strdup(name);
2244 switch (type) {
2245 case QEMU_FILE_TYPE_BIOS:
2246 subdir = "";
2247 break;
2248 case QEMU_FILE_TYPE_KEYMAP:
2249 subdir = "keymaps/";
2250 break;
2251 default:
2252 abort();
2255 for (i = 0; i < data_dir_idx; i++) {
2256 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2257 if (access(buf, R_OK) == 0) {
2258 trace_load_file(name, buf);
2259 return buf;
2261 g_free(buf);
2263 return NULL;
2266 static inline bool nonempty_str(const char *str)
2268 return str && *str;
2271 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
2273 gchar *buf;
2274 size_t size;
2275 const char *name, *file, *str;
2276 FWCfgState *fw_cfg = (FWCfgState *) opaque;
2278 if (fw_cfg == NULL) {
2279 error_report("fw_cfg device not available");
2280 return -1;
2282 name = qemu_opt_get(opts, "name");
2283 file = qemu_opt_get(opts, "file");
2284 str = qemu_opt_get(opts, "string");
2286 /* we need name and either a file or the content string */
2287 if (!(nonempty_str(name) && (nonempty_str(file) || nonempty_str(str)))) {
2288 error_report("invalid argument(s)");
2289 return -1;
2291 if (nonempty_str(file) && nonempty_str(str)) {
2292 error_report("file and string are mutually exclusive");
2293 return -1;
2295 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
2296 error_report("name too long (max. %d char)", FW_CFG_MAX_FILE_PATH - 1);
2297 return -1;
2299 if (strncmp(name, "opt/", 4) != 0) {
2300 error_report("warning: externally provided fw_cfg item names "
2301 "should be prefixed with \"opt/\"");
2303 if (nonempty_str(str)) {
2304 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
2305 buf = g_memdup(str, size);
2306 } else {
2307 if (!g_file_get_contents(file, &buf, &size, NULL)) {
2308 error_report("can't load %s", file);
2309 return -1;
2312 /* For legacy, keep user files in a specific global order. */
2313 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
2314 fw_cfg_add_file(fw_cfg, name, buf, size);
2315 fw_cfg_reset_order_override(fw_cfg);
2316 return 0;
2319 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
2321 return qdev_device_help(opts);
2324 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
2326 Error *err = NULL;
2327 DeviceState *dev;
2329 dev = qdev_device_add(opts, &err);
2330 if (!dev) {
2331 error_report_err(err);
2332 return -1;
2334 object_unref(OBJECT(dev));
2335 return 0;
2338 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2340 Error *local_err = NULL;
2342 qemu_chr_new_from_opts(opts, &local_err);
2343 if (local_err) {
2344 error_report_err(local_err);
2345 return -1;
2347 return 0;
2350 #ifdef CONFIG_VIRTFS
2351 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2353 return qemu_fsdev_add(opts);
2355 #endif
2357 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
2359 CharDriverState *chr;
2360 const char *chardev;
2361 const char *mode;
2362 int flags;
2364 mode = qemu_opt_get(opts, "mode");
2365 if (mode == NULL) {
2366 mode = "readline";
2368 if (strcmp(mode, "readline") == 0) {
2369 flags = MONITOR_USE_READLINE;
2370 } else if (strcmp(mode, "control") == 0) {
2371 flags = MONITOR_USE_CONTROL;
2372 } else {
2373 error_report("unknown monitor mode \"%s\"", mode);
2374 exit(1);
2377 if (qemu_opt_get_bool(opts, "pretty", 0))
2378 flags |= MONITOR_USE_PRETTY;
2380 if (qemu_opt_get_bool(opts, "default", 0)) {
2381 error_report("option 'default' does nothing and is deprecated");
2384 chardev = qemu_opt_get(opts, "chardev");
2385 chr = qemu_chr_find(chardev);
2386 if (chr == NULL) {
2387 error_report("chardev \"%s\" not found", chardev);
2388 exit(1);
2391 monitor_init(chr, flags);
2392 return 0;
2395 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
2397 static int monitor_device_index = 0;
2398 QemuOpts *opts;
2399 const char *p;
2400 char label[32];
2402 if (strstart(optarg, "chardev:", &p)) {
2403 snprintf(label, sizeof(label), "%s", p);
2404 } else {
2405 snprintf(label, sizeof(label), "compat_monitor%d",
2406 monitor_device_index);
2407 opts = qemu_chr_parse_compat(label, optarg);
2408 if (!opts) {
2409 error_report("parse error: %s", optarg);
2410 exit(1);
2414 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
2415 qemu_opt_set(opts, "mode", mode, &error_abort);
2416 qemu_opt_set(opts, "chardev", label, &error_abort);
2417 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
2418 monitor_device_index++;
2421 struct device_config {
2422 enum {
2423 DEV_USB, /* -usbdevice */
2424 DEV_BT, /* -bt */
2425 DEV_SERIAL, /* -serial */
2426 DEV_PARALLEL, /* -parallel */
2427 DEV_VIRTCON, /* -virtioconsole */
2428 DEV_DEBUGCON, /* -debugcon */
2429 DEV_GDB, /* -gdb, -s */
2430 DEV_SCLP, /* s390 sclp */
2431 } type;
2432 const char *cmdline;
2433 Location loc;
2434 QTAILQ_ENTRY(device_config) next;
2437 static QTAILQ_HEAD(, device_config) device_configs =
2438 QTAILQ_HEAD_INITIALIZER(device_configs);
2440 static void add_device_config(int type, const char *cmdline)
2442 struct device_config *conf;
2444 conf = g_malloc0(sizeof(*conf));
2445 conf->type = type;
2446 conf->cmdline = cmdline;
2447 loc_save(&conf->loc);
2448 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2451 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2453 struct device_config *conf;
2454 int rc;
2456 QTAILQ_FOREACH(conf, &device_configs, next) {
2457 if (conf->type != type)
2458 continue;
2459 loc_push_restore(&conf->loc);
2460 rc = func(conf->cmdline);
2461 loc_pop(&conf->loc);
2462 if (rc) {
2463 return rc;
2466 return 0;
2469 static int serial_parse(const char *devname)
2471 static int index = 0;
2472 char label[32];
2474 if (strcmp(devname, "none") == 0)
2475 return 0;
2476 if (index == MAX_SERIAL_PORTS) {
2477 error_report("too many serial ports");
2478 exit(1);
2480 snprintf(label, sizeof(label), "serial%d", index);
2481 serial_hds[index] = qemu_chr_new(label, devname);
2482 if (!serial_hds[index]) {
2483 error_report("could not connect serial device"
2484 " to character backend '%s'", devname);
2485 return -1;
2487 index++;
2488 return 0;
2491 static int parallel_parse(const char *devname)
2493 static int index = 0;
2494 char label[32];
2496 if (strcmp(devname, "none") == 0)
2497 return 0;
2498 if (index == MAX_PARALLEL_PORTS) {
2499 error_report("too many parallel ports");
2500 exit(1);
2502 snprintf(label, sizeof(label), "parallel%d", index);
2503 parallel_hds[index] = qemu_chr_new(label, devname);
2504 if (!parallel_hds[index]) {
2505 error_report("could not connect parallel device"
2506 " to character backend '%s'", devname);
2507 return -1;
2509 index++;
2510 return 0;
2513 static int virtcon_parse(const char *devname)
2515 QemuOptsList *device = qemu_find_opts("device");
2516 static int index = 0;
2517 char label[32];
2518 QemuOpts *bus_opts, *dev_opts;
2520 if (strcmp(devname, "none") == 0)
2521 return 0;
2522 if (index == MAX_VIRTIO_CONSOLES) {
2523 error_report("too many virtio consoles");
2524 exit(1);
2527 bus_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2528 qemu_opt_set(bus_opts, "driver", "virtio-serial", &error_abort);
2530 dev_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2531 qemu_opt_set(dev_opts, "driver", "virtconsole", &error_abort);
2533 snprintf(label, sizeof(label), "virtcon%d", index);
2534 virtcon_hds[index] = qemu_chr_new(label, devname);
2535 if (!virtcon_hds[index]) {
2536 error_report("could not connect virtio console"
2537 " to character backend '%s'", devname);
2538 return -1;
2540 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2542 index++;
2543 return 0;
2546 static int sclp_parse(const char *devname)
2548 QemuOptsList *device = qemu_find_opts("device");
2549 static int index = 0;
2550 char label[32];
2551 QemuOpts *dev_opts;
2553 if (strcmp(devname, "none") == 0) {
2554 return 0;
2556 if (index == MAX_SCLP_CONSOLES) {
2557 error_report("too many sclp consoles");
2558 exit(1);
2561 assert(arch_type == QEMU_ARCH_S390X);
2563 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2564 qemu_opt_set(dev_opts, "driver", "sclpconsole", &error_abort);
2566 snprintf(label, sizeof(label), "sclpcon%d", index);
2567 sclp_hds[index] = qemu_chr_new(label, devname);
2568 if (!sclp_hds[index]) {
2569 error_report("could not connect sclp console"
2570 " to character backend '%s'", devname);
2571 return -1;
2573 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2575 index++;
2576 return 0;
2579 static int debugcon_parse(const char *devname)
2581 QemuOpts *opts;
2583 if (!qemu_chr_new("debugcon", devname)) {
2584 exit(1);
2586 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2587 if (!opts) {
2588 error_report("already have a debugcon device");
2589 exit(1);
2591 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
2592 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
2593 return 0;
2596 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
2598 const MachineClass *mc1 = a, *mc2 = b;
2599 int res;
2601 if (mc1->family == NULL) {
2602 if (mc2->family == NULL) {
2603 /* Compare standalone machine types against each other; they sort
2604 * in increasing order.
2606 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
2607 object_class_get_name(OBJECT_CLASS(mc2)));
2610 /* Standalone machine types sort after families. */
2611 return 1;
2614 if (mc2->family == NULL) {
2615 /* Families sort before standalone machine types. */
2616 return -1;
2619 /* Families sort between each other alphabetically increasingly. */
2620 res = strcmp(mc1->family, mc2->family);
2621 if (res != 0) {
2622 return res;
2625 /* Within the same family, machine types sort in decreasing order. */
2626 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
2627 object_class_get_name(OBJECT_CLASS(mc1)));
2630 static MachineClass *machine_parse(const char *name)
2632 MachineClass *mc = NULL;
2633 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
2635 if (name) {
2636 mc = find_machine(name);
2638 if (mc) {
2639 g_slist_free(machines);
2640 return mc;
2642 if (name && !is_help_option(name)) {
2643 error_report("unsupported machine type");
2644 error_printf("Use -machine help to list supported machines\n");
2645 } else {
2646 printf("Supported machines are:\n");
2647 machines = g_slist_sort(machines, machine_class_cmp);
2648 for (el = machines; el; el = el->next) {
2649 MachineClass *mc = el->data;
2650 if (mc->alias) {
2651 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
2653 printf("%-20s %s%s\n", mc->name, mc->desc,
2654 mc->is_default ? " (default)" : "");
2658 g_slist_free(machines);
2659 exit(!name || !is_help_option(name));
2662 void qemu_add_exit_notifier(Notifier *notify)
2664 notifier_list_add(&exit_notifiers, notify);
2667 void qemu_remove_exit_notifier(Notifier *notify)
2669 notifier_remove(notify);
2672 static void qemu_run_exit_notifiers(void)
2674 notifier_list_notify(&exit_notifiers, NULL);
2677 static bool machine_init_done;
2679 void qemu_add_machine_init_done_notifier(Notifier *notify)
2681 notifier_list_add(&machine_init_done_notifiers, notify);
2682 if (machine_init_done) {
2683 notify->notify(notify, NULL);
2687 void qemu_remove_machine_init_done_notifier(Notifier *notify)
2689 notifier_remove(notify);
2692 static void qemu_run_machine_init_done_notifiers(void)
2694 notifier_list_notify(&machine_init_done_notifiers, NULL);
2695 machine_init_done = true;
2698 static const QEMUOption *lookup_opt(int argc, char **argv,
2699 const char **poptarg, int *poptind)
2701 const QEMUOption *popt;
2702 int optind = *poptind;
2703 char *r = argv[optind];
2704 const char *optarg;
2706 loc_set_cmdline(argv, optind, 1);
2707 optind++;
2708 /* Treat --foo the same as -foo. */
2709 if (r[1] == '-')
2710 r++;
2711 popt = qemu_options;
2712 for(;;) {
2713 if (!popt->name) {
2714 error_report("invalid option");
2715 exit(1);
2717 if (!strcmp(popt->name, r + 1))
2718 break;
2719 popt++;
2721 if (popt->flags & HAS_ARG) {
2722 if (optind >= argc) {
2723 error_report("requires an argument");
2724 exit(1);
2726 optarg = argv[optind++];
2727 loc_set_cmdline(argv, optind - 2, 2);
2728 } else {
2729 optarg = NULL;
2732 *poptarg = optarg;
2733 *poptind = optind;
2735 return popt;
2738 static MachineClass *select_machine(void)
2740 MachineClass *machine_class = find_default_machine();
2741 const char *optarg;
2742 QemuOpts *opts;
2743 Location loc;
2745 loc_push_none(&loc);
2747 opts = qemu_get_machine_opts();
2748 qemu_opts_loc_restore(opts);
2750 optarg = qemu_opt_get(opts, "type");
2751 if (optarg) {
2752 machine_class = machine_parse(optarg);
2755 if (!machine_class) {
2756 error_report("No machine specified, and there is no default");
2757 error_printf("Use -machine help to list supported machines\n");
2758 exit(1);
2761 loc_pop(&loc);
2762 return machine_class;
2765 static int machine_set_property(void *opaque,
2766 const char *name, const char *value,
2767 Error **errp)
2769 Object *obj = OBJECT(opaque);
2770 Error *local_err = NULL;
2771 char *p, *qom_name;
2773 if (strcmp(name, "type") == 0) {
2774 return 0;
2777 qom_name = g_strdup(name);
2778 for (p = qom_name; *p; p++) {
2779 if (*p == '_') {
2780 *p = '-';
2784 object_property_parse(obj, value, qom_name, &local_err);
2785 g_free(qom_name);
2787 if (local_err) {
2788 error_report_err(local_err);
2789 return -1;
2792 return 0;
2797 * Initial object creation happens before all other
2798 * QEMU data types are created. The majority of objects
2799 * can be created at this point. The rng-egd object
2800 * cannot be created here, as it depends on the chardev
2801 * already existing.
2803 static bool object_create_initial(const char *type)
2805 if (g_str_equal(type, "rng-egd")) {
2806 return false;
2810 * return false for concrete netfilters since
2811 * they depend on netdevs already existing
2813 if (g_str_equal(type, "filter-buffer") ||
2814 g_str_equal(type, "filter-dump") ||
2815 g_str_equal(type, "filter-mirror") ||
2816 g_str_equal(type, "filter-redirector") ||
2817 g_str_equal(type, "colo-compare") ||
2818 g_str_equal(type, "filter-rewriter") ||
2819 g_str_equal(type, "filter-replay")) {
2820 return false;
2823 /* Memory allocation by backends needs to be done
2824 * after configure_accelerator() (due to the tcg_enabled()
2825 * checks at memory_region_init_*()).
2827 * Also, allocation of large amounts of memory may delay
2828 * chardev initialization for too long, and trigger timeouts
2829 * on software that waits for a monitor socket to be created
2830 * (e.g. libvirt).
2832 if (g_str_has_prefix(type, "memory-backend-")) {
2833 return false;
2836 return true;
2841 * The remainder of object creation happens after the
2842 * creation of chardev, fsdev, net clients and device data types.
2844 static bool object_create_delayed(const char *type)
2846 return !object_create_initial(type);
2850 static void set_memory_options(uint64_t *ram_slots, ram_addr_t *maxram_size,
2851 MachineClass *mc)
2853 uint64_t sz;
2854 const char *mem_str;
2855 const char *maxmem_str, *slots_str;
2856 const ram_addr_t default_ram_size = mc->default_ram_size;
2857 QemuOpts *opts = qemu_find_opts_singleton("memory");
2858 Location loc;
2860 loc_push_none(&loc);
2861 qemu_opts_loc_restore(opts);
2863 sz = 0;
2864 mem_str = qemu_opt_get(opts, "size");
2865 if (mem_str) {
2866 if (!*mem_str) {
2867 error_report("missing 'size' option value");
2868 exit(EXIT_FAILURE);
2871 sz = qemu_opt_get_size(opts, "size", ram_size);
2873 /* Fix up legacy suffix-less format */
2874 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2875 uint64_t overflow_check = sz;
2877 sz <<= 20;
2878 if ((sz >> 20) != overflow_check) {
2879 error_report("too large 'size' option value");
2880 exit(EXIT_FAILURE);
2885 /* backward compatibility behaviour for case "-m 0" */
2886 if (sz == 0) {
2887 sz = default_ram_size;
2890 sz = QEMU_ALIGN_UP(sz, 8192);
2891 ram_size = sz;
2892 if (ram_size != sz) {
2893 error_report("ram size too large");
2894 exit(EXIT_FAILURE);
2897 /* store value for the future use */
2898 qemu_opt_set_number(opts, "size", ram_size, &error_abort);
2899 *maxram_size = ram_size;
2901 maxmem_str = qemu_opt_get(opts, "maxmem");
2902 slots_str = qemu_opt_get(opts, "slots");
2903 if (maxmem_str && slots_str) {
2904 uint64_t slots;
2906 sz = qemu_opt_get_size(opts, "maxmem", 0);
2907 slots = qemu_opt_get_number(opts, "slots", 0);
2908 if (sz < ram_size) {
2909 error_report("invalid value of -m option maxmem: "
2910 "maximum memory size (0x%" PRIx64 ") must be at least "
2911 "the initial memory size (0x" RAM_ADDR_FMT ")",
2912 sz, ram_size);
2913 exit(EXIT_FAILURE);
2914 } else if (sz > ram_size) {
2915 if (!slots) {
2916 error_report("invalid value of -m option: maxmem was "
2917 "specified, but no hotplug slots were specified");
2918 exit(EXIT_FAILURE);
2920 } else if (slots) {
2921 error_report("invalid value of -m option maxmem: "
2922 "memory slots were specified but maximum memory size "
2923 "(0x%" PRIx64 ") is equal to the initial memory size "
2924 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
2925 exit(EXIT_FAILURE);
2928 *maxram_size = sz;
2929 *ram_slots = slots;
2930 } else if ((!maxmem_str && slots_str) ||
2931 (maxmem_str && !slots_str)) {
2932 error_report("invalid -m option value: missing "
2933 "'%s' option", slots_str ? "maxmem" : "slots");
2934 exit(EXIT_FAILURE);
2937 loc_pop(&loc);
2940 static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
2942 GlobalProperty *g;
2944 g = g_malloc0(sizeof(*g));
2945 g->driver = qemu_opt_get(opts, "driver");
2946 g->property = qemu_opt_get(opts, "property");
2947 g->value = qemu_opt_get(opts, "value");
2948 g->user_provided = true;
2949 g->errp = &error_fatal;
2950 qdev_prop_register_global(g);
2951 return 0;
2954 static int qemu_read_default_config_file(void)
2956 int ret;
2958 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
2959 if (ret < 0 && ret != -ENOENT) {
2960 return ret;
2963 return 0;
2966 int main(int argc, char **argv, char **envp)
2968 int i;
2969 int snapshot, linux_boot;
2970 const char *initrd_filename;
2971 const char *kernel_filename, *kernel_cmdline;
2972 const char *boot_order = NULL;
2973 const char *boot_once = NULL;
2974 DisplayState *ds;
2975 int cyls, heads, secs, translation;
2976 QemuOpts *hda_opts = NULL, *opts, *machine_opts, *icount_opts = NULL;
2977 QemuOptsList *olist;
2978 int optind;
2979 const char *optarg;
2980 const char *loadvm = NULL;
2981 MachineClass *machine_class;
2982 const char *cpu_model;
2983 const char *vga_model = NULL;
2984 const char *qtest_chrdev = NULL;
2985 const char *qtest_log = NULL;
2986 const char *pid_file = NULL;
2987 const char *incoming = NULL;
2988 bool defconfig = true;
2989 bool userconfig = true;
2990 bool nographic = false;
2991 DisplayType display_type = DT_DEFAULT;
2992 int display_remote = 0;
2993 const char *log_mask = NULL;
2994 const char *log_file = NULL;
2995 char *trace_file = NULL;
2996 ram_addr_t maxram_size;
2997 uint64_t ram_slots = 0;
2998 FILE *vmstate_dump_file = NULL;
2999 Error *main_loop_err = NULL;
3000 Error *err = NULL;
3001 bool list_data_dirs = false;
3003 module_call_init(MODULE_INIT_TRACE);
3005 qemu_init_cpu_list();
3006 qemu_init_cpu_loop();
3007 qemu_mutex_lock_iothread();
3009 atexit(qemu_run_exit_notifiers);
3010 error_set_progname(argv[0]);
3011 qemu_init_exec_dir(argv[0]);
3013 module_call_init(MODULE_INIT_QOM);
3014 module_call_init(MODULE_INIT_QAPI);
3016 qemu_add_opts(&qemu_drive_opts);
3017 qemu_add_drive_opts(&qemu_legacy_drive_opts);
3018 qemu_add_drive_opts(&qemu_common_drive_opts);
3019 qemu_add_drive_opts(&qemu_drive_opts);
3020 qemu_add_drive_opts(&bdrv_runtime_opts);
3021 qemu_add_opts(&qemu_chardev_opts);
3022 qemu_add_opts(&qemu_device_opts);
3023 qemu_add_opts(&qemu_netdev_opts);
3024 qemu_add_opts(&qemu_net_opts);
3025 qemu_add_opts(&qemu_rtc_opts);
3026 qemu_add_opts(&qemu_global_opts);
3027 qemu_add_opts(&qemu_mon_opts);
3028 qemu_add_opts(&qemu_trace_opts);
3029 qemu_add_opts(&qemu_option_rom_opts);
3030 qemu_add_opts(&qemu_machine_opts);
3031 qemu_add_opts(&qemu_mem_opts);
3032 qemu_add_opts(&qemu_smp_opts);
3033 qemu_add_opts(&qemu_boot_opts);
3034 qemu_add_opts(&qemu_sandbox_opts);
3035 qemu_add_opts(&qemu_add_fd_opts);
3036 qemu_add_opts(&qemu_object_opts);
3037 qemu_add_opts(&qemu_tpmdev_opts);
3038 qemu_add_opts(&qemu_realtime_opts);
3039 qemu_add_opts(&qemu_msg_opts);
3040 qemu_add_opts(&qemu_name_opts);
3041 qemu_add_opts(&qemu_numa_opts);
3042 qemu_add_opts(&qemu_icount_opts);
3043 qemu_add_opts(&qemu_semihosting_config_opts);
3044 qemu_add_opts(&qemu_fw_cfg_opts);
3045 #ifdef CONFIG_LIBISCSI
3046 qemu_add_opts(&qemu_iscsi_opts);
3047 #endif
3048 module_call_init(MODULE_INIT_OPTS);
3050 runstate_init();
3052 if (qcrypto_init(&err) < 0) {
3053 error_reportf_err(err, "cannot initialize crypto: ");
3054 exit(1);
3056 rtc_clock = QEMU_CLOCK_HOST;
3058 QLIST_INIT (&vm_change_state_head);
3059 os_setup_early_signal_handling();
3061 cpu_model = NULL;
3062 snapshot = 0;
3063 cyls = heads = secs = 0;
3064 translation = BIOS_ATA_TRANSLATION_AUTO;
3066 nb_nics = 0;
3068 bdrv_init_with_whitelist();
3070 autostart = 1;
3072 /* first pass of option parsing */
3073 optind = 1;
3074 while (optind < argc) {
3075 if (argv[optind][0] != '-') {
3076 /* disk image */
3077 optind++;
3078 } else {
3079 const QEMUOption *popt;
3081 popt = lookup_opt(argc, argv, &optarg, &optind);
3082 switch (popt->index) {
3083 case QEMU_OPTION_nodefconfig:
3084 defconfig = false;
3085 break;
3086 case QEMU_OPTION_nouserconfig:
3087 userconfig = false;
3088 break;
3093 if (defconfig && userconfig) {
3094 if (qemu_read_default_config_file() < 0) {
3095 exit(1);
3099 /* second pass of option parsing */
3100 optind = 1;
3101 for(;;) {
3102 if (optind >= argc)
3103 break;
3104 if (argv[optind][0] != '-') {
3105 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
3106 } else {
3107 const QEMUOption *popt;
3109 popt = lookup_opt(argc, argv, &optarg, &optind);
3110 if (!(popt->arch_mask & arch_type)) {
3111 error_report("Option not supported for this target");
3112 exit(1);
3114 switch(popt->index) {
3115 case QEMU_OPTION_no_kvm_irqchip: {
3116 olist = qemu_find_opts("machine");
3117 qemu_opts_parse_noisily(olist, "kernel_irqchip=off", false);
3118 break;
3120 case QEMU_OPTION_cpu:
3121 /* hw initialization will check this */
3122 cpu_model = optarg;
3123 break;
3124 case QEMU_OPTION_hda:
3126 char buf[256];
3127 if (cyls == 0)
3128 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
3129 else
3130 snprintf(buf, sizeof(buf),
3131 "%s,cyls=%d,heads=%d,secs=%d%s",
3132 HD_OPTS , cyls, heads, secs,
3133 translation == BIOS_ATA_TRANSLATION_LBA ?
3134 ",trans=lba" :
3135 translation == BIOS_ATA_TRANSLATION_NONE ?
3136 ",trans=none" : "");
3137 drive_add(IF_DEFAULT, 0, optarg, buf);
3138 break;
3140 case QEMU_OPTION_hdb:
3141 case QEMU_OPTION_hdc:
3142 case QEMU_OPTION_hdd:
3143 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
3144 HD_OPTS);
3145 break;
3146 case QEMU_OPTION_drive:
3147 if (drive_def(optarg) == NULL) {
3148 exit(1);
3150 break;
3151 case QEMU_OPTION_set:
3152 if (qemu_set_option(optarg) != 0)
3153 exit(1);
3154 break;
3155 case QEMU_OPTION_global:
3156 if (qemu_global_option(optarg) != 0)
3157 exit(1);
3158 break;
3159 case QEMU_OPTION_mtdblock:
3160 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3161 break;
3162 case QEMU_OPTION_sd:
3163 drive_add(IF_SD, -1, optarg, SD_OPTS);
3164 break;
3165 case QEMU_OPTION_pflash:
3166 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
3167 break;
3168 case QEMU_OPTION_snapshot:
3169 snapshot = 1;
3170 break;
3171 case QEMU_OPTION_hdachs:
3173 const char *p;
3174 p = optarg;
3175 cyls = strtol(p, (char **)&p, 0);
3176 if (cyls < 1 || cyls > 16383)
3177 goto chs_fail;
3178 if (*p != ',')
3179 goto chs_fail;
3180 p++;
3181 heads = strtol(p, (char **)&p, 0);
3182 if (heads < 1 || heads > 16)
3183 goto chs_fail;
3184 if (*p != ',')
3185 goto chs_fail;
3186 p++;
3187 secs = strtol(p, (char **)&p, 0);
3188 if (secs < 1 || secs > 63)
3189 goto chs_fail;
3190 if (*p == ',') {
3191 p++;
3192 if (!strcmp(p, "large")) {
3193 translation = BIOS_ATA_TRANSLATION_LARGE;
3194 } else if (!strcmp(p, "rechs")) {
3195 translation = BIOS_ATA_TRANSLATION_RECHS;
3196 } else if (!strcmp(p, "none")) {
3197 translation = BIOS_ATA_TRANSLATION_NONE;
3198 } else if (!strcmp(p, "lba")) {
3199 translation = BIOS_ATA_TRANSLATION_LBA;
3200 } else if (!strcmp(p, "auto")) {
3201 translation = BIOS_ATA_TRANSLATION_AUTO;
3202 } else {
3203 goto chs_fail;
3205 } else if (*p != '\0') {
3206 chs_fail:
3207 error_report("invalid physical CHS format");
3208 exit(1);
3210 if (hda_opts != NULL) {
3211 qemu_opt_set_number(hda_opts, "cyls", cyls,
3212 &error_abort);
3213 qemu_opt_set_number(hda_opts, "heads", heads,
3214 &error_abort);
3215 qemu_opt_set_number(hda_opts, "secs", secs,
3216 &error_abort);
3217 if (translation == BIOS_ATA_TRANSLATION_LARGE) {
3218 qemu_opt_set(hda_opts, "trans", "large",
3219 &error_abort);
3220 } else if (translation == BIOS_ATA_TRANSLATION_RECHS) {
3221 qemu_opt_set(hda_opts, "trans", "rechs",
3222 &error_abort);
3223 } else if (translation == BIOS_ATA_TRANSLATION_LBA) {
3224 qemu_opt_set(hda_opts, "trans", "lba",
3225 &error_abort);
3226 } else if (translation == BIOS_ATA_TRANSLATION_NONE) {
3227 qemu_opt_set(hda_opts, "trans", "none",
3228 &error_abort);
3232 break;
3233 case QEMU_OPTION_numa:
3234 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
3235 optarg, true);
3236 if (!opts) {
3237 exit(1);
3239 break;
3240 case QEMU_OPTION_display:
3241 display_type = select_display(optarg);
3242 break;
3243 case QEMU_OPTION_nographic:
3244 olist = qemu_find_opts("machine");
3245 qemu_opts_parse_noisily(olist, "graphics=off", false);
3246 nographic = true;
3247 display_type = DT_NONE;
3248 break;
3249 case QEMU_OPTION_curses:
3250 #ifdef CONFIG_CURSES
3251 display_type = DT_CURSES;
3252 #else
3253 error_report("curses support is disabled");
3254 exit(1);
3255 #endif
3256 break;
3257 case QEMU_OPTION_portrait:
3258 graphic_rotate = 90;
3259 break;
3260 case QEMU_OPTION_rotate:
3261 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3262 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3263 graphic_rotate != 180 && graphic_rotate != 270) {
3264 error_report("only 90, 180, 270 deg rotation is available");
3265 exit(1);
3267 break;
3268 case QEMU_OPTION_kernel:
3269 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg,
3270 &error_abort);
3271 break;
3272 case QEMU_OPTION_initrd:
3273 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg,
3274 &error_abort);
3275 break;
3276 case QEMU_OPTION_append:
3277 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg,
3278 &error_abort);
3279 break;
3280 case QEMU_OPTION_dtb:
3281 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg,
3282 &error_abort);
3283 break;
3284 case QEMU_OPTION_cdrom:
3285 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3286 break;
3287 case QEMU_OPTION_boot:
3288 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
3289 optarg, true);
3290 if (!opts) {
3291 exit(1);
3293 break;
3294 case QEMU_OPTION_fda:
3295 case QEMU_OPTION_fdb:
3296 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3297 optarg, FD_OPTS);
3298 break;
3299 case QEMU_OPTION_no_fd_bootchk:
3300 fd_bootchk = 0;
3301 break;
3302 case QEMU_OPTION_netdev:
3303 default_net = 0;
3304 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3305 exit(1);
3307 break;
3308 case QEMU_OPTION_net:
3309 default_net = 0;
3310 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3311 exit(1);
3313 break;
3314 #ifdef CONFIG_LIBISCSI
3315 case QEMU_OPTION_iscsi:
3316 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
3317 optarg, false);
3318 if (!opts) {
3319 exit(1);
3321 break;
3322 #endif
3323 #ifdef CONFIG_SLIRP
3324 case QEMU_OPTION_tftp:
3325 error_report("The -tftp option is deprecated. "
3326 "Please use '-netdev user,tftp=...' instead.");
3327 legacy_tftp_prefix = optarg;
3328 break;
3329 case QEMU_OPTION_bootp:
3330 error_report("The -bootp option is deprecated. "
3331 "Please use '-netdev user,bootfile=...' instead.");
3332 legacy_bootp_filename = optarg;
3333 break;
3334 case QEMU_OPTION_redir:
3335 error_report("The -redir option is deprecated. "
3336 "Please use '-netdev user,hostfwd=...' instead.");
3337 if (net_slirp_redir(optarg) < 0)
3338 exit(1);
3339 break;
3340 #endif
3341 case QEMU_OPTION_bt:
3342 add_device_config(DEV_BT, optarg);
3343 break;
3344 case QEMU_OPTION_audio_help:
3345 AUD_help ();
3346 exit (0);
3347 break;
3348 case QEMU_OPTION_soundhw:
3349 select_soundhw (optarg);
3350 break;
3351 case QEMU_OPTION_h:
3352 help(0);
3353 break;
3354 case QEMU_OPTION_version:
3355 version();
3356 exit(0);
3357 break;
3358 case QEMU_OPTION_m:
3359 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
3360 optarg, true);
3361 if (!opts) {
3362 exit(EXIT_FAILURE);
3364 break;
3365 #ifdef CONFIG_TPM
3366 case QEMU_OPTION_tpmdev:
3367 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3368 exit(1);
3370 break;
3371 #endif
3372 case QEMU_OPTION_mempath:
3373 mem_path = optarg;
3374 break;
3375 case QEMU_OPTION_mem_prealloc:
3376 mem_prealloc = 1;
3377 break;
3378 case QEMU_OPTION_d:
3379 log_mask = optarg;
3380 break;
3381 case QEMU_OPTION_D:
3382 log_file = optarg;
3383 break;
3384 case QEMU_OPTION_DFILTER:
3385 qemu_set_dfilter_ranges(optarg, &error_fatal);
3386 break;
3387 case QEMU_OPTION_s:
3388 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3389 break;
3390 case QEMU_OPTION_gdb:
3391 add_device_config(DEV_GDB, optarg);
3392 break;
3393 case QEMU_OPTION_L:
3394 if (is_help_option(optarg)) {
3395 list_data_dirs = true;
3396 } else if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3397 data_dir[data_dir_idx++] = optarg;
3399 break;
3400 case QEMU_OPTION_bios:
3401 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg,
3402 &error_abort);
3403 break;
3404 case QEMU_OPTION_singlestep:
3405 singlestep = 1;
3406 break;
3407 case QEMU_OPTION_S:
3408 autostart = 0;
3409 break;
3410 case QEMU_OPTION_k:
3411 keyboard_layout = optarg;
3412 break;
3413 case QEMU_OPTION_localtime:
3414 rtc_utc = 0;
3415 break;
3416 case QEMU_OPTION_vga:
3417 vga_model = optarg;
3418 default_vga = 0;
3419 break;
3420 case QEMU_OPTION_g:
3422 const char *p;
3423 int w, h, depth;
3424 p = optarg;
3425 w = strtol(p, (char **)&p, 10);
3426 if (w <= 0) {
3427 graphic_error:
3428 error_report("invalid resolution or depth");
3429 exit(1);
3431 if (*p != 'x')
3432 goto graphic_error;
3433 p++;
3434 h = strtol(p, (char **)&p, 10);
3435 if (h <= 0)
3436 goto graphic_error;
3437 if (*p == 'x') {
3438 p++;
3439 depth = strtol(p, (char **)&p, 10);
3440 if (depth != 8 && depth != 15 && depth != 16 &&
3441 depth != 24 && depth != 32)
3442 goto graphic_error;
3443 } else if (*p == '\0') {
3444 depth = graphic_depth;
3445 } else {
3446 goto graphic_error;
3449 graphic_width = w;
3450 graphic_height = h;
3451 graphic_depth = depth;
3453 break;
3454 case QEMU_OPTION_echr:
3456 char *r;
3457 term_escape_char = strtol(optarg, &r, 0);
3458 if (r == optarg)
3459 printf("Bad argument to echr\n");
3460 break;
3462 case QEMU_OPTION_monitor:
3463 default_monitor = 0;
3464 if (strncmp(optarg, "none", 4)) {
3465 monitor_parse(optarg, "readline", false);
3467 break;
3468 case QEMU_OPTION_qmp:
3469 monitor_parse(optarg, "control", false);
3470 default_monitor = 0;
3471 break;
3472 case QEMU_OPTION_qmp_pretty:
3473 monitor_parse(optarg, "control", true);
3474 default_monitor = 0;
3475 break;
3476 case QEMU_OPTION_mon:
3477 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3478 true);
3479 if (!opts) {
3480 exit(1);
3482 default_monitor = 0;
3483 break;
3484 case QEMU_OPTION_chardev:
3485 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3486 optarg, true);
3487 if (!opts) {
3488 exit(1);
3490 break;
3491 case QEMU_OPTION_fsdev:
3492 olist = qemu_find_opts("fsdev");
3493 if (!olist) {
3494 error_report("fsdev support is disabled");
3495 exit(1);
3497 opts = qemu_opts_parse_noisily(olist, optarg, true);
3498 if (!opts) {
3499 exit(1);
3501 break;
3502 case QEMU_OPTION_virtfs: {
3503 QemuOpts *fsdev;
3504 QemuOpts *device;
3505 const char *writeout, *sock_fd, *socket;
3507 olist = qemu_find_opts("virtfs");
3508 if (!olist) {
3509 error_report("virtfs support is disabled");
3510 exit(1);
3512 opts = qemu_opts_parse_noisily(olist, optarg, true);
3513 if (!opts) {
3514 exit(1);
3517 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3518 qemu_opt_get(opts, "mount_tag") == NULL) {
3519 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3520 exit(1);
3522 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3523 qemu_opt_get(opts, "mount_tag"),
3524 1, NULL);
3525 if (!fsdev) {
3526 error_report("duplicate fsdev id: %s",
3527 qemu_opt_get(opts, "mount_tag"));
3528 exit(1);
3531 writeout = qemu_opt_get(opts, "writeout");
3532 if (writeout) {
3533 #ifdef CONFIG_SYNC_FILE_RANGE
3534 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3535 #else
3536 error_report("writeout=immediate not supported "
3537 "on this platform");
3538 exit(1);
3539 #endif
3541 qemu_opt_set(fsdev, "fsdriver",
3542 qemu_opt_get(opts, "fsdriver"), &error_abort);
3543 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"),
3544 &error_abort);
3545 qemu_opt_set(fsdev, "security_model",
3546 qemu_opt_get(opts, "security_model"),
3547 &error_abort);
3548 socket = qemu_opt_get(opts, "socket");
3549 if (socket) {
3550 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3552 sock_fd = qemu_opt_get(opts, "sock_fd");
3553 if (sock_fd) {
3554 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3557 qemu_opt_set_bool(fsdev, "readonly",
3558 qemu_opt_get_bool(opts, "readonly", 0),
3559 &error_abort);
3560 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3561 &error_abort);
3562 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3563 qemu_opt_set(device, "fsdev",
3564 qemu_opt_get(opts, "mount_tag"), &error_abort);
3565 qemu_opt_set(device, "mount_tag",
3566 qemu_opt_get(opts, "mount_tag"), &error_abort);
3567 break;
3569 case QEMU_OPTION_virtfs_synth: {
3570 QemuOpts *fsdev;
3571 QemuOpts *device;
3573 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3574 1, NULL);
3575 if (!fsdev) {
3576 error_report("duplicate option: %s", "virtfs_synth");
3577 exit(1);
3579 qemu_opt_set(fsdev, "fsdriver", "synth", &error_abort);
3581 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3582 &error_abort);
3583 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3584 qemu_opt_set(device, "fsdev", "v_synth", &error_abort);
3585 qemu_opt_set(device, "mount_tag", "v_synth", &error_abort);
3586 break;
3588 case QEMU_OPTION_serial:
3589 add_device_config(DEV_SERIAL, optarg);
3590 default_serial = 0;
3591 if (strncmp(optarg, "mon:", 4) == 0) {
3592 default_monitor = 0;
3594 break;
3595 case QEMU_OPTION_watchdog:
3596 if (watchdog) {
3597 error_report("only one watchdog option may be given");
3598 return 1;
3600 watchdog = optarg;
3601 break;
3602 case QEMU_OPTION_watchdog_action:
3603 if (select_watchdog_action(optarg) == -1) {
3604 error_report("unknown -watchdog-action parameter");
3605 exit(1);
3607 break;
3608 case QEMU_OPTION_virtiocon:
3609 add_device_config(DEV_VIRTCON, optarg);
3610 default_virtcon = 0;
3611 if (strncmp(optarg, "mon:", 4) == 0) {
3612 default_monitor = 0;
3614 break;
3615 case QEMU_OPTION_parallel:
3616 add_device_config(DEV_PARALLEL, optarg);
3617 default_parallel = 0;
3618 if (strncmp(optarg, "mon:", 4) == 0) {
3619 default_monitor = 0;
3621 break;
3622 case QEMU_OPTION_debugcon:
3623 add_device_config(DEV_DEBUGCON, optarg);
3624 break;
3625 case QEMU_OPTION_loadvm:
3626 loadvm = optarg;
3627 break;
3628 case QEMU_OPTION_full_screen:
3629 full_screen = 1;
3630 break;
3631 case QEMU_OPTION_no_frame:
3632 no_frame = 1;
3633 break;
3634 case QEMU_OPTION_alt_grab:
3635 alt_grab = 1;
3636 break;
3637 case QEMU_OPTION_ctrl_grab:
3638 ctrl_grab = 1;
3639 break;
3640 case QEMU_OPTION_no_quit:
3641 no_quit = 1;
3642 break;
3643 case QEMU_OPTION_sdl:
3644 #ifdef CONFIG_SDL
3645 display_type = DT_SDL;
3646 break;
3647 #else
3648 error_report("SDL support is disabled");
3649 exit(1);
3650 #endif
3651 case QEMU_OPTION_pidfile:
3652 pid_file = optarg;
3653 break;
3654 case QEMU_OPTION_win2k_hack:
3655 win2k_install_hack = 1;
3656 break;
3657 case QEMU_OPTION_rtc_td_hack: {
3658 static GlobalProperty slew_lost_ticks = {
3659 .driver = "mc146818rtc",
3660 .property = "lost_tick_policy",
3661 .value = "slew",
3664 qdev_prop_register_global(&slew_lost_ticks);
3665 break;
3667 case QEMU_OPTION_acpitable:
3668 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3669 optarg, true);
3670 if (!opts) {
3671 exit(1);
3673 acpi_table_add(opts, &error_fatal);
3674 break;
3675 case QEMU_OPTION_smbios:
3676 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3677 optarg, false);
3678 if (!opts) {
3679 exit(1);
3681 smbios_entry_add(opts, &error_fatal);
3682 break;
3683 case QEMU_OPTION_fwcfg:
3684 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3685 optarg, true);
3686 if (opts == NULL) {
3687 exit(1);
3689 break;
3690 case QEMU_OPTION_enable_kvm:
3691 olist = qemu_find_opts("machine");
3692 qemu_opts_parse_noisily(olist, "accel=kvm", false);
3693 break;
3694 case QEMU_OPTION_enable_hax:
3695 olist = qemu_find_opts("machine");
3696 qemu_opts_parse_noisily(olist, "accel=hax", false);
3697 break;
3698 case QEMU_OPTION_M:
3699 case QEMU_OPTION_machine:
3700 olist = qemu_find_opts("machine");
3701 opts = qemu_opts_parse_noisily(olist, optarg, true);
3702 if (!opts) {
3703 exit(1);
3705 break;
3706 case QEMU_OPTION_no_kvm:
3707 olist = qemu_find_opts("machine");
3708 qemu_opts_parse_noisily(olist, "accel=tcg", false);
3709 break;
3710 case QEMU_OPTION_no_kvm_pit: {
3711 error_report("warning: ignoring deprecated option");
3712 break;
3714 case QEMU_OPTION_no_kvm_pit_reinjection: {
3715 static GlobalProperty kvm_pit_lost_tick_policy = {
3716 .driver = "kvm-pit",
3717 .property = "lost_tick_policy",
3718 .value = "discard",
3721 error_report("warning: deprecated, replaced by "
3722 "-global kvm-pit.lost_tick_policy=discard");
3723 qdev_prop_register_global(&kvm_pit_lost_tick_policy);
3724 break;
3726 case QEMU_OPTION_usb:
3727 olist = qemu_find_opts("machine");
3728 qemu_opts_parse_noisily(olist, "usb=on", false);
3729 break;
3730 case QEMU_OPTION_usbdevice:
3731 olist = qemu_find_opts("machine");
3732 qemu_opts_parse_noisily(olist, "usb=on", false);
3733 add_device_config(DEV_USB, optarg);
3734 break;
3735 case QEMU_OPTION_device:
3736 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3737 optarg, true)) {
3738 exit(1);
3740 break;
3741 case QEMU_OPTION_smp:
3742 if (!qemu_opts_parse_noisily(qemu_find_opts("smp-opts"),
3743 optarg, true)) {
3744 exit(1);
3746 break;
3747 case QEMU_OPTION_vnc:
3748 vnc_parse(optarg, &error_fatal);
3749 break;
3750 case QEMU_OPTION_no_acpi:
3751 acpi_enabled = 0;
3752 break;
3753 case QEMU_OPTION_no_hpet:
3754 no_hpet = 1;
3755 break;
3756 case QEMU_OPTION_balloon:
3757 if (balloon_parse(optarg) < 0) {
3758 error_report("unknown -balloon argument %s", optarg);
3759 exit(1);
3761 break;
3762 case QEMU_OPTION_no_reboot:
3763 no_reboot = 1;
3764 break;
3765 case QEMU_OPTION_no_shutdown:
3766 no_shutdown = 1;
3767 break;
3768 case QEMU_OPTION_show_cursor:
3769 cursor_hide = 0;
3770 break;
3771 case QEMU_OPTION_uuid:
3772 if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
3773 error_report("failed to parse UUID string: wrong format");
3774 exit(1);
3776 qemu_uuid_set = true;
3777 break;
3778 case QEMU_OPTION_option_rom:
3779 if (nb_option_roms >= MAX_OPTION_ROMS) {
3780 error_report("too many option ROMs");
3781 exit(1);
3783 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3784 optarg, true);
3785 if (!opts) {
3786 exit(1);
3788 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3789 option_rom[nb_option_roms].bootindex =
3790 qemu_opt_get_number(opts, "bootindex", -1);
3791 if (!option_rom[nb_option_roms].name) {
3792 error_report("Option ROM file is not specified");
3793 exit(1);
3795 nb_option_roms++;
3796 break;
3797 case QEMU_OPTION_semihosting:
3798 semihosting.enabled = true;
3799 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3800 break;
3801 case QEMU_OPTION_semihosting_config:
3802 semihosting.enabled = true;
3803 opts = qemu_opts_parse_noisily(qemu_find_opts("semihosting-config"),
3804 optarg, false);
3805 if (opts != NULL) {
3806 semihosting.enabled = qemu_opt_get_bool(opts, "enable",
3807 true);
3808 const char *target = qemu_opt_get(opts, "target");
3809 if (target != NULL) {
3810 if (strcmp("native", target) == 0) {
3811 semihosting.target = SEMIHOSTING_TARGET_NATIVE;
3812 } else if (strcmp("gdb", target) == 0) {
3813 semihosting.target = SEMIHOSTING_TARGET_GDB;
3814 } else if (strcmp("auto", target) == 0) {
3815 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3816 } else {
3817 error_report("unsupported semihosting-config %s",
3818 optarg);
3819 exit(1);
3821 } else {
3822 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3824 /* Set semihosting argument count and vector */
3825 qemu_opt_foreach(opts, add_semihosting_arg,
3826 &semihosting, NULL);
3827 } else {
3828 error_report("unsupported semihosting-config %s", optarg);
3829 exit(1);
3831 break;
3832 case QEMU_OPTION_tdf:
3833 error_report("warning: ignoring deprecated option");
3834 break;
3835 case QEMU_OPTION_name:
3836 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3837 optarg, true);
3838 if (!opts) {
3839 exit(1);
3841 break;
3842 case QEMU_OPTION_prom_env:
3843 if (nb_prom_envs >= MAX_PROM_ENVS) {
3844 error_report("too many prom variables");
3845 exit(1);
3847 prom_envs[nb_prom_envs] = optarg;
3848 nb_prom_envs++;
3849 break;
3850 case QEMU_OPTION_old_param:
3851 old_param = 1;
3852 break;
3853 case QEMU_OPTION_clock:
3854 /* Clock options no longer exist. Keep this option for
3855 * backward compatibility.
3857 break;
3858 case QEMU_OPTION_startdate:
3859 configure_rtc_date_offset(optarg, 1);
3860 break;
3861 case QEMU_OPTION_rtc:
3862 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3863 false);
3864 if (!opts) {
3865 exit(1);
3867 configure_rtc(opts);
3868 break;
3869 case QEMU_OPTION_tb_size:
3870 tcg_tb_size = strtol(optarg, NULL, 0);
3871 if (tcg_tb_size < 0) {
3872 tcg_tb_size = 0;
3874 break;
3875 case QEMU_OPTION_icount:
3876 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3877 optarg, true);
3878 if (!icount_opts) {
3879 exit(1);
3881 break;
3882 case QEMU_OPTION_incoming:
3883 if (!incoming) {
3884 runstate_set(RUN_STATE_INMIGRATE);
3886 incoming = optarg;
3887 break;
3888 case QEMU_OPTION_only_migratable:
3889 only_migratable = 1;
3890 break;
3891 case QEMU_OPTION_nodefaults:
3892 has_defaults = 0;
3893 break;
3894 case QEMU_OPTION_xen_domid:
3895 if (!(xen_available())) {
3896 error_report("Option not supported for this target");
3897 exit(1);
3899 xen_domid = atoi(optarg);
3900 break;
3901 case QEMU_OPTION_xen_create:
3902 if (!(xen_available())) {
3903 error_report("Option not supported for this target");
3904 exit(1);
3906 xen_mode = XEN_CREATE;
3907 break;
3908 case QEMU_OPTION_xen_attach:
3909 if (!(xen_available())) {
3910 error_report("Option not supported for this target");
3911 exit(1);
3913 xen_mode = XEN_ATTACH;
3914 break;
3915 case QEMU_OPTION_trace:
3916 g_free(trace_file);
3917 trace_file = trace_opt_parse(optarg);
3918 break;
3919 case QEMU_OPTION_readconfig:
3921 int ret = qemu_read_config_file(optarg);
3922 if (ret < 0) {
3923 error_report("read config %s: %s", optarg,
3924 strerror(-ret));
3925 exit(1);
3927 break;
3929 case QEMU_OPTION_spice:
3930 olist = qemu_find_opts("spice");
3931 if (!olist) {
3932 error_report("spice support is disabled");
3933 exit(1);
3935 opts = qemu_opts_parse_noisily(olist, optarg, false);
3936 if (!opts) {
3937 exit(1);
3939 display_remote++;
3940 break;
3941 case QEMU_OPTION_writeconfig:
3943 FILE *fp;
3944 if (strcmp(optarg, "-") == 0) {
3945 fp = stdout;
3946 } else {
3947 fp = fopen(optarg, "w");
3948 if (fp == NULL) {
3949 error_report("open %s: %s", optarg,
3950 strerror(errno));
3951 exit(1);
3954 qemu_config_write(fp);
3955 if (fp != stdout) {
3956 fclose(fp);
3958 break;
3960 case QEMU_OPTION_qtest:
3961 qtest_chrdev = optarg;
3962 break;
3963 case QEMU_OPTION_qtest_log:
3964 qtest_log = optarg;
3965 break;
3966 case QEMU_OPTION_sandbox:
3967 opts = qemu_opts_parse_noisily(qemu_find_opts("sandbox"),
3968 optarg, true);
3969 if (!opts) {
3970 exit(1);
3972 break;
3973 case QEMU_OPTION_add_fd:
3974 #ifndef _WIN32
3975 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
3976 optarg, false);
3977 if (!opts) {
3978 exit(1);
3980 #else
3981 error_report("File descriptor passing is disabled on this "
3982 "platform");
3983 exit(1);
3984 #endif
3985 break;
3986 case QEMU_OPTION_object:
3987 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
3988 optarg, true);
3989 if (!opts) {
3990 exit(1);
3992 break;
3993 case QEMU_OPTION_realtime:
3994 opts = qemu_opts_parse_noisily(qemu_find_opts("realtime"),
3995 optarg, false);
3996 if (!opts) {
3997 exit(1);
3999 enable_mlock = qemu_opt_get_bool(opts, "mlock", true);
4000 break;
4001 case QEMU_OPTION_msg:
4002 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
4003 false);
4004 if (!opts) {
4005 exit(1);
4007 configure_msg(opts);
4008 break;
4009 case QEMU_OPTION_dump_vmstate:
4010 if (vmstate_dump_file) {
4011 error_report("only one '-dump-vmstate' "
4012 "option may be given");
4013 exit(1);
4015 vmstate_dump_file = fopen(optarg, "w");
4016 if (vmstate_dump_file == NULL) {
4017 error_report("open %s: %s", optarg, strerror(errno));
4018 exit(1);
4020 break;
4021 default:
4022 os_parse_cmd_args(popt->index, optarg);
4027 * Clear error location left behind by the loop.
4028 * Best done right after the loop. Do not insert code here!
4030 loc_set_none();
4032 replay_configure(icount_opts);
4034 machine_class = select_machine();
4036 set_memory_options(&ram_slots, &maxram_size, machine_class);
4038 os_daemonize();
4040 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
4041 error_report("could not acquire pid file: %s", strerror(errno));
4042 exit(1);
4045 if (qemu_init_main_loop(&main_loop_err)) {
4046 error_report_err(main_loop_err);
4047 exit(1);
4050 if (qemu_opts_foreach(qemu_find_opts("sandbox"),
4051 parse_sandbox, NULL, NULL)) {
4052 exit(1);
4055 if (qemu_opts_foreach(qemu_find_opts("name"),
4056 parse_name, NULL, NULL)) {
4057 exit(1);
4060 #ifndef _WIN32
4061 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4062 parse_add_fd, NULL, NULL)) {
4063 exit(1);
4066 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4067 cleanup_add_fd, NULL, NULL)) {
4068 exit(1);
4070 #endif
4072 current_machine = MACHINE(object_new(object_class_get_name(
4073 OBJECT_CLASS(machine_class))));
4074 if (machine_help_func(qemu_get_machine_opts(), current_machine)) {
4075 exit(0);
4077 object_property_add_child(object_get_root(), "machine",
4078 OBJECT(current_machine), &error_abort);
4080 if (machine_class->minimum_page_bits) {
4081 if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
4082 /* This would be a board error: specifying a minimum smaller than
4083 * a target's compile-time fixed setting.
4085 g_assert_not_reached();
4089 cpu_exec_init_all();
4091 if (machine_class->hw_version) {
4092 qemu_set_hw_version(machine_class->hw_version);
4095 if (cpu_model && is_help_option(cpu_model)) {
4096 list_cpus(stdout, &fprintf, cpu_model);
4097 exit(0);
4100 if (!trace_init_backends()) {
4101 exit(1);
4103 trace_init_file(trace_file);
4105 /* Open the logfile at this point and set the log mask if necessary.
4107 if (log_file) {
4108 qemu_set_log_filename(log_file, &error_fatal);
4111 if (log_mask) {
4112 int mask;
4113 mask = qemu_str_to_log_mask(log_mask);
4114 if (!mask) {
4115 qemu_print_log_usage(stdout);
4116 exit(1);
4118 qemu_set_log(mask);
4119 } else {
4120 qemu_set_log(0);
4123 /* If no data_dir is specified then try to find it relative to the
4124 executable path. */
4125 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4126 data_dir[data_dir_idx] = os_find_datadir();
4127 if (data_dir[data_dir_idx] != NULL) {
4128 data_dir_idx++;
4131 /* If all else fails use the install path specified when building. */
4132 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4133 data_dir[data_dir_idx++] = CONFIG_QEMU_DATADIR;
4136 /* -L help lists the data directories and exits. */
4137 if (list_data_dirs) {
4138 for (i = 0; i < data_dir_idx; i++) {
4139 printf("%s\n", data_dir[i]);
4141 exit(0);
4144 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
4146 machine_class->max_cpus = machine_class->max_cpus ?: 1; /* Default to UP */
4147 if (max_cpus > machine_class->max_cpus) {
4148 error_report("Number of SMP CPUs requested (%d) exceeds max CPUs "
4149 "supported by machine '%s' (%d)", max_cpus,
4150 machine_class->name, machine_class->max_cpus);
4151 exit(1);
4155 * Get the default machine options from the machine if it is not already
4156 * specified either by the configuration file or by the command line.
4158 if (machine_class->default_machine_opts) {
4159 qemu_opts_set_defaults(qemu_find_opts("machine"),
4160 machine_class->default_machine_opts, 0);
4163 qemu_opts_foreach(qemu_find_opts("device"),
4164 default_driver_check, NULL, NULL);
4165 qemu_opts_foreach(qemu_find_opts("global"),
4166 default_driver_check, NULL, NULL);
4168 if (!vga_model && !default_vga) {
4169 vga_interface_type = VGA_DEVICE;
4171 if (!has_defaults || machine_class->no_serial) {
4172 default_serial = 0;
4174 if (!has_defaults || machine_class->no_parallel) {
4175 default_parallel = 0;
4177 if (!has_defaults || !machine_class->use_virtcon) {
4178 default_virtcon = 0;
4180 if (!has_defaults || !machine_class->use_sclp) {
4181 default_sclp = 0;
4183 if (!has_defaults || machine_class->no_floppy) {
4184 default_floppy = 0;
4186 if (!has_defaults || machine_class->no_cdrom) {
4187 default_cdrom = 0;
4189 if (!has_defaults || machine_class->no_sdcard) {
4190 default_sdcard = 0;
4192 if (!has_defaults) {
4193 default_monitor = 0;
4194 default_net = 0;
4195 default_vga = 0;
4198 if (is_daemonized()) {
4199 /* According to documentation and historically, -nographic redirects
4200 * serial port, parallel port and monitor to stdio, which does not work
4201 * with -daemonize. We can redirect these to null instead, but since
4202 * -nographic is legacy, let's just error out.
4203 * We disallow -nographic only if all other ports are not redirected
4204 * explicitly, to not break existing legacy setups which uses
4205 * -nographic _and_ redirects all ports explicitly - this is valid
4206 * usage, -nographic is just a no-op in this case.
4208 if (nographic
4209 && (default_parallel || default_serial
4210 || default_monitor || default_virtcon)) {
4211 error_report("-nographic cannot be used with -daemonize");
4212 exit(1);
4214 #ifdef CONFIG_CURSES
4215 if (display_type == DT_CURSES) {
4216 error_report("curses display cannot be used with -daemonize");
4217 exit(1);
4219 #endif
4222 if (nographic) {
4223 if (default_parallel)
4224 add_device_config(DEV_PARALLEL, "null");
4225 if (default_serial && default_monitor) {
4226 add_device_config(DEV_SERIAL, "mon:stdio");
4227 } else if (default_virtcon && default_monitor) {
4228 add_device_config(DEV_VIRTCON, "mon:stdio");
4229 } else if (default_sclp && default_monitor) {
4230 add_device_config(DEV_SCLP, "mon:stdio");
4231 } else {
4232 if (default_serial)
4233 add_device_config(DEV_SERIAL, "stdio");
4234 if (default_virtcon)
4235 add_device_config(DEV_VIRTCON, "stdio");
4236 if (default_sclp) {
4237 add_device_config(DEV_SCLP, "stdio");
4239 if (default_monitor)
4240 monitor_parse("stdio", "readline", false);
4242 } else {
4243 if (default_serial)
4244 add_device_config(DEV_SERIAL, "vc:80Cx24C");
4245 if (default_parallel)
4246 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
4247 if (default_monitor)
4248 monitor_parse("vc:80Cx24C", "readline", false);
4249 if (default_virtcon)
4250 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
4251 if (default_sclp) {
4252 add_device_config(DEV_SCLP, "vc:80Cx24C");
4256 #if defined(CONFIG_VNC)
4257 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
4258 display_remote++;
4260 #endif
4261 if (display_type == DT_DEFAULT && !display_remote) {
4262 #if defined(CONFIG_GTK)
4263 display_type = DT_GTK;
4264 #elif defined(CONFIG_SDL)
4265 display_type = DT_SDL;
4266 #elif defined(CONFIG_COCOA)
4267 display_type = DT_COCOA;
4268 #elif defined(CONFIG_VNC)
4269 vnc_parse("localhost:0,to=99,id=default", &error_abort);
4270 #else
4271 display_type = DT_NONE;
4272 #endif
4275 if ((no_frame || alt_grab || ctrl_grab) && display_type != DT_SDL) {
4276 error_report("-no-frame, -alt-grab and -ctrl-grab are only valid "
4277 "for SDL, ignoring option");
4279 if (no_quit && (display_type != DT_GTK && display_type != DT_SDL)) {
4280 error_report("-no-quit is only valid for GTK and SDL, "
4281 "ignoring option");
4284 if (display_type == DT_GTK) {
4285 early_gtk_display_init(request_opengl);
4288 if (display_type == DT_SDL) {
4289 sdl_display_early_init(request_opengl);
4292 if (request_opengl == 1 && display_opengl == 0) {
4293 #if defined(CONFIG_OPENGL)
4294 error_report("OpenGL is not supported by the display");
4295 #else
4296 error_report("OpenGL support is disabled");
4297 #endif
4298 exit(1);
4301 page_size_init();
4302 socket_init();
4304 if (qemu_opts_foreach(qemu_find_opts("object"),
4305 user_creatable_add_opts_foreach,
4306 object_create_initial, NULL)) {
4307 exit(1);
4310 if (qemu_opts_foreach(qemu_find_opts("chardev"),
4311 chardev_init_func, NULL, NULL)) {
4312 exit(1);
4315 #ifdef CONFIG_VIRTFS
4316 if (qemu_opts_foreach(qemu_find_opts("fsdev"),
4317 fsdev_init_func, NULL, NULL)) {
4318 exit(1);
4320 #endif
4322 if (qemu_opts_foreach(qemu_find_opts("device"),
4323 device_help_func, NULL, NULL)) {
4324 exit(0);
4327 machine_opts = qemu_get_machine_opts();
4328 if (qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
4329 NULL)) {
4330 object_unref(OBJECT(current_machine));
4331 exit(1);
4334 configure_accelerator(current_machine);
4336 if (qtest_chrdev) {
4337 qtest_init(qtest_chrdev, qtest_log, &error_fatal);
4340 machine_opts = qemu_get_machine_opts();
4341 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4342 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4343 kernel_cmdline = qemu_opt_get(machine_opts, "append");
4344 bios_name = qemu_opt_get(machine_opts, "firmware");
4346 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4347 if (opts) {
4348 boot_order = qemu_opt_get(opts, "order");
4349 if (boot_order) {
4350 validate_bootdevices(boot_order, &error_fatal);
4353 boot_once = qemu_opt_get(opts, "once");
4354 if (boot_once) {
4355 validate_bootdevices(boot_once, &error_fatal);
4358 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
4359 boot_strict = qemu_opt_get_bool(opts, "strict", false);
4362 if (!boot_order) {
4363 boot_order = machine_class->default_boot_order;
4366 if (!kernel_cmdline) {
4367 kernel_cmdline = "";
4368 current_machine->kernel_cmdline = (char *)kernel_cmdline;
4371 linux_boot = (kernel_filename != NULL);
4373 if (!linux_boot && *kernel_cmdline != '\0') {
4374 error_report("-append only allowed with -kernel option");
4375 exit(1);
4378 if (!linux_boot && initrd_filename != NULL) {
4379 error_report("-initrd only allowed with -kernel option");
4380 exit(1);
4383 if (semihosting_enabled() && !semihosting_get_argc() && kernel_filename) {
4384 /* fall back to the -kernel/-append */
4385 semihosting_arg_fallback(kernel_filename, kernel_cmdline);
4388 os_set_line_buffering();
4390 /* spice needs the timers to be initialized by this point */
4391 qemu_spice_init();
4393 cpu_ticks_init();
4394 if (icount_opts) {
4395 if (!tcg_enabled()) {
4396 error_report("-icount is not allowed with hardware virtualization");
4397 exit(1);
4399 configure_icount(icount_opts, &error_abort);
4400 qemu_opts_del(icount_opts);
4403 if (default_net) {
4404 QemuOptsList *net = qemu_find_opts("net");
4405 qemu_opts_set(net, NULL, "type", "nic", &error_abort);
4406 #ifdef CONFIG_SLIRP
4407 qemu_opts_set(net, NULL, "type", "user", &error_abort);
4408 #endif
4411 colo_info_init();
4413 if (net_init_clients() < 0) {
4414 exit(1);
4417 if (qemu_opts_foreach(qemu_find_opts("object"),
4418 user_creatable_add_opts_foreach,
4419 object_create_delayed, NULL)) {
4420 exit(1);
4423 #ifdef CONFIG_TPM
4424 if (tpm_init() < 0) {
4425 exit(1);
4427 #endif
4429 /* init the bluetooth world */
4430 if (foreach_device_config(DEV_BT, bt_parse))
4431 exit(1);
4433 if (!xen_enabled()) {
4434 /* On 32-bit hosts, QEMU is limited by virtual address space */
4435 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4436 error_report("at most 2047 MB RAM can be simulated");
4437 exit(1);
4441 blk_mig_init();
4442 ram_mig_init();
4444 /* If the currently selected machine wishes to override the units-per-bus
4445 * property of its default HBA interface type, do so now. */
4446 if (machine_class->units_per_default_bus) {
4447 override_max_devs(machine_class->block_default_type,
4448 machine_class->units_per_default_bus);
4451 /* open the virtual block devices */
4452 if (snapshot || replay_mode != REPLAY_MODE_NONE) {
4453 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
4454 NULL, NULL);
4456 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
4457 &machine_class->block_default_type, NULL)) {
4458 exit(1);
4461 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
4462 CDROM_OPTS);
4463 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4464 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
4466 parse_numa_opts(machine_class);
4468 if (qemu_opts_foreach(qemu_find_opts("mon"),
4469 mon_init_func, NULL, NULL)) {
4470 exit(1);
4473 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4474 exit(1);
4475 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4476 exit(1);
4477 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4478 exit(1);
4479 if (foreach_device_config(DEV_SCLP, sclp_parse) < 0) {
4480 exit(1);
4482 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4483 exit(1);
4485 /* If no default VGA is requested, the default is "none". */
4486 if (default_vga) {
4487 if (machine_class->default_display) {
4488 vga_model = machine_class->default_display;
4489 } else if (vga_interface_available(VGA_CIRRUS)) {
4490 vga_model = "cirrus";
4491 } else if (vga_interface_available(VGA_STD)) {
4492 vga_model = "std";
4495 if (vga_model) {
4496 select_vgahw(vga_model);
4499 if (watchdog) {
4500 i = select_watchdog(watchdog);
4501 if (i > 0)
4502 exit (i == 1 ? 1 : 0);
4505 machine_register_compat_props(current_machine);
4507 qemu_opts_foreach(qemu_find_opts("global"),
4508 global_init_func, NULL, NULL);
4510 /* This checkpoint is required by replay to separate prior clock
4511 reading from the other reads, because timer polling functions query
4512 clock values from the log. */
4513 replay_checkpoint(CHECKPOINT_INIT);
4514 qdev_machine_init();
4516 current_machine->ram_size = ram_size;
4517 current_machine->maxram_size = maxram_size;
4518 current_machine->ram_slots = ram_slots;
4519 current_machine->boot_order = boot_order;
4520 current_machine->cpu_model = cpu_model;
4522 machine_class->init(current_machine);
4524 realtime_init();
4526 audio_init();
4528 cpu_synchronize_all_post_init();
4530 if (hax_enabled()) {
4531 hax_sync_vcpus();
4534 if (qemu_opts_foreach(qemu_find_opts("fw_cfg"),
4535 parse_fw_cfg, fw_cfg_find(), NULL) != 0) {
4536 exit(1);
4539 /* init USB devices */
4540 if (machine_usb(current_machine)) {
4541 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4542 exit(1);
4545 /* Check if IGD GFX passthrough. */
4546 igd_gfx_passthru();
4548 /* init generic devices */
4549 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
4550 if (qemu_opts_foreach(qemu_find_opts("device"),
4551 device_init_func, NULL, NULL)) {
4552 exit(1);
4555 numa_post_machine_init();
4557 rom_reset_order_override();
4559 /* Did we create any drives that we failed to create a device for? */
4560 drive_check_orphaned();
4562 /* Don't warn about the default network setup that you get if
4563 * no command line -net or -netdev options are specified. There
4564 * are two cases that we would otherwise complain about:
4565 * (1) board doesn't support a NIC but the implicit "-net nic"
4566 * requested one
4567 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
4568 * sets up a nic that isn't connected to anything.
4570 if (!default_net) {
4571 net_check_clients();
4575 if (boot_once) {
4576 qemu_boot_set(boot_once, &error_fatal);
4577 qemu_register_reset(restore_boot_order, g_strdup(boot_order));
4580 ds = init_displaystate();
4582 /* init local displays */
4583 switch (display_type) {
4584 case DT_CURSES:
4585 curses_display_init(ds, full_screen);
4586 break;
4587 case DT_SDL:
4588 sdl_display_init(ds, full_screen, no_frame);
4589 break;
4590 case DT_COCOA:
4591 cocoa_display_init(ds, full_screen);
4592 break;
4593 case DT_GTK:
4594 gtk_display_init(ds, full_screen, grab_on_hover);
4595 break;
4596 default:
4597 break;
4600 /* must be after terminal init, SDL library changes signal handlers */
4601 os_setup_signal_handling();
4603 /* init remote displays */
4604 #ifdef CONFIG_VNC
4605 qemu_opts_foreach(qemu_find_opts("vnc"),
4606 vnc_init_func, NULL, NULL);
4607 #endif
4609 if (using_spice) {
4610 qemu_spice_display_init();
4613 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
4614 exit(1);
4617 qdev_machine_creation_done();
4619 /* TODO: once all bus devices are qdevified, this should be done
4620 * when bus is created by qdev.c */
4621 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4622 qemu_run_machine_init_done_notifiers();
4624 if (rom_check_and_register_reset() != 0) {
4625 error_report("rom check and register reset failed");
4626 exit(1);
4629 replay_start();
4631 /* This checkpoint is required by replay to separate prior clock
4632 reading from the other reads, because timer polling functions query
4633 clock values from the log. */
4634 replay_checkpoint(CHECKPOINT_RESET);
4635 qemu_system_reset(VMRESET_SILENT);
4636 register_global_state();
4637 if (loadvm) {
4638 if (load_vmstate(loadvm) < 0) {
4639 autostart = 0;
4643 qdev_prop_check_globals();
4644 if (vmstate_dump_file) {
4645 /* dump and exit */
4646 dump_vmstate_json_to_file(vmstate_dump_file);
4647 return 0;
4650 if (incoming) {
4651 Error *local_err = NULL;
4652 qemu_start_incoming_migration(incoming, &local_err);
4653 if (local_err) {
4654 error_reportf_err(local_err, "-incoming %s: ", incoming);
4655 exit(1);
4657 } else if (autostart) {
4658 vm_start();
4661 os_setup_post();
4663 main_loop();
4664 replay_disable_events();
4665 iothread_stop_all();
4667 bdrv_close_all();
4668 pause_all_vcpus();
4669 res_free();
4671 /* vhost-user must be cleaned up before chardevs. */
4672 net_cleanup();
4673 audio_cleanup();
4674 monitor_cleanup();
4675 qemu_chr_cleanup();
4677 return 0;