hw/isa/lpc_ich9: add SMI feature negotiation via fw_cfg
[qemu/kevin.git] / vl.c
blob9b71ed586ab5b7d5d00c02dc4a719772328c935a
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,
468 }, {
469 .name = "rrsnapshot",
470 .type = QEMU_OPT_STRING,
472 { /* end of list */ }
476 static QemuOptsList qemu_semihosting_config_opts = {
477 .name = "semihosting-config",
478 .implied_opt_name = "enable",
479 .head = QTAILQ_HEAD_INITIALIZER(qemu_semihosting_config_opts.head),
480 .desc = {
482 .name = "enable",
483 .type = QEMU_OPT_BOOL,
484 }, {
485 .name = "target",
486 .type = QEMU_OPT_STRING,
487 }, {
488 .name = "arg",
489 .type = QEMU_OPT_STRING,
491 { /* end of list */ }
495 static QemuOptsList qemu_fw_cfg_opts = {
496 .name = "fw_cfg",
497 .implied_opt_name = "name",
498 .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
499 .desc = {
501 .name = "name",
502 .type = QEMU_OPT_STRING,
503 .help = "Sets the fw_cfg name of the blob to be inserted",
504 }, {
505 .name = "file",
506 .type = QEMU_OPT_STRING,
507 .help = "Sets the name of the file from which\n"
508 "the fw_cfg blob will be loaded",
509 }, {
510 .name = "string",
511 .type = QEMU_OPT_STRING,
512 .help = "Sets content of the blob to be inserted from a string",
514 { /* end of list */ }
518 #ifdef CONFIG_LIBISCSI
519 static QemuOptsList qemu_iscsi_opts = {
520 .name = "iscsi",
521 .head = QTAILQ_HEAD_INITIALIZER(qemu_iscsi_opts.head),
522 .desc = {
524 .name = "user",
525 .type = QEMU_OPT_STRING,
526 .help = "username for CHAP authentication to target",
528 .name = "password",
529 .type = QEMU_OPT_STRING,
530 .help = "password for CHAP authentication to target",
532 .name = "password-secret",
533 .type = QEMU_OPT_STRING,
534 .help = "ID of the secret providing password for CHAP "
535 "authentication to target",
537 .name = "header-digest",
538 .type = QEMU_OPT_STRING,
539 .help = "HeaderDigest setting. "
540 "{CRC32C|CRC32C-NONE|NONE-CRC32C|NONE}",
542 .name = "initiator-name",
543 .type = QEMU_OPT_STRING,
544 .help = "Initiator iqn name to use when connecting",
546 .name = "timeout",
547 .type = QEMU_OPT_NUMBER,
548 .help = "Request timeout in seconds (default 0 = no timeout)",
550 { /* end of list */ }
553 #endif
556 * Get machine options
558 * Returns: machine options (never null).
560 QemuOpts *qemu_get_machine_opts(void)
562 return qemu_find_opts_singleton("machine");
565 const char *qemu_get_vm_name(void)
567 return qemu_name;
570 static void res_free(void)
572 g_free(boot_splash_filedata);
573 boot_splash_filedata = NULL;
576 static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
578 const char *driver = qemu_opt_get(opts, "driver");
579 int i;
581 if (!driver)
582 return 0;
583 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
584 if (strcmp(default_list[i].driver, driver) != 0)
585 continue;
586 *(default_list[i].flag) = 0;
588 return 0;
591 /***********************************************************/
592 /* QEMU state */
594 static RunState current_run_state = RUN_STATE_PRELAUNCH;
596 /* We use RUN_STATE__MAX but any invalid value will do */
597 static RunState vmstop_requested = RUN_STATE__MAX;
598 static QemuMutex vmstop_lock;
600 typedef struct {
601 RunState from;
602 RunState to;
603 } RunStateTransition;
605 static const RunStateTransition runstate_transitions_def[] = {
606 /* from -> to */
607 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
608 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
609 { RUN_STATE_DEBUG, RUN_STATE_PRELAUNCH },
611 { RUN_STATE_INMIGRATE, RUN_STATE_INTERNAL_ERROR },
612 { RUN_STATE_INMIGRATE, RUN_STATE_IO_ERROR },
613 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
614 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
615 { RUN_STATE_INMIGRATE, RUN_STATE_SHUTDOWN },
616 { RUN_STATE_INMIGRATE, RUN_STATE_SUSPENDED },
617 { RUN_STATE_INMIGRATE, RUN_STATE_WATCHDOG },
618 { RUN_STATE_INMIGRATE, RUN_STATE_GUEST_PANICKED },
619 { RUN_STATE_INMIGRATE, RUN_STATE_FINISH_MIGRATE },
620 { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH },
621 { RUN_STATE_INMIGRATE, RUN_STATE_POSTMIGRATE },
622 { RUN_STATE_INMIGRATE, RUN_STATE_COLO },
624 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
625 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
626 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PRELAUNCH },
628 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
629 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
630 { RUN_STATE_IO_ERROR, RUN_STATE_PRELAUNCH },
632 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
633 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
634 { RUN_STATE_PAUSED, RUN_STATE_PRELAUNCH },
635 { RUN_STATE_PAUSED, RUN_STATE_COLO},
637 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
638 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
639 { RUN_STATE_POSTMIGRATE, RUN_STATE_PRELAUNCH },
641 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
642 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
643 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
645 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
646 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
647 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PRELAUNCH },
648 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_COLO},
650 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
651 { RUN_STATE_RESTORE_VM, RUN_STATE_PRELAUNCH },
653 { RUN_STATE_COLO, RUN_STATE_RUNNING },
655 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
656 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
657 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
658 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
659 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
660 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
661 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
662 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
663 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
664 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
665 { RUN_STATE_RUNNING, RUN_STATE_COLO},
667 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
669 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
670 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
671 { RUN_STATE_SHUTDOWN, RUN_STATE_PRELAUNCH },
673 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
674 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
675 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
676 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
677 { RUN_STATE_SUSPENDED, RUN_STATE_PRELAUNCH },
678 { RUN_STATE_SUSPENDED, RUN_STATE_COLO},
680 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
681 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
682 { RUN_STATE_WATCHDOG, RUN_STATE_PRELAUNCH },
683 { RUN_STATE_WATCHDOG, RUN_STATE_COLO},
685 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
686 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
687 { RUN_STATE_GUEST_PANICKED, RUN_STATE_PRELAUNCH },
689 { RUN_STATE__MAX, RUN_STATE__MAX },
692 static bool runstate_valid_transitions[RUN_STATE__MAX][RUN_STATE__MAX];
694 bool runstate_check(RunState state)
696 return current_run_state == state;
699 bool runstate_store(char *str, size_t size)
701 const char *state = RunState_lookup[current_run_state];
702 size_t len = strlen(state) + 1;
704 if (len > size) {
705 return false;
707 memcpy(str, state, len);
708 return true;
711 static void runstate_init(void)
713 const RunStateTransition *p;
715 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
716 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE__MAX; p++) {
717 runstate_valid_transitions[p->from][p->to] = true;
720 qemu_mutex_init(&vmstop_lock);
723 /* This function will abort() on invalid state transitions */
724 void runstate_set(RunState new_state)
726 assert(new_state < RUN_STATE__MAX);
728 if (current_run_state == new_state) {
729 return;
732 if (!runstate_valid_transitions[current_run_state][new_state]) {
733 error_report("invalid runstate transition: '%s' -> '%s'",
734 RunState_lookup[current_run_state],
735 RunState_lookup[new_state]);
736 abort();
738 trace_runstate_set(new_state);
739 current_run_state = new_state;
742 int runstate_is_running(void)
744 return runstate_check(RUN_STATE_RUNNING);
747 bool runstate_needs_reset(void)
749 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
750 runstate_check(RUN_STATE_SHUTDOWN);
753 StatusInfo *qmp_query_status(Error **errp)
755 StatusInfo *info = g_malloc0(sizeof(*info));
757 info->running = runstate_is_running();
758 info->singlestep = singlestep;
759 info->status = current_run_state;
761 return info;
764 static bool qemu_vmstop_requested(RunState *r)
766 qemu_mutex_lock(&vmstop_lock);
767 *r = vmstop_requested;
768 vmstop_requested = RUN_STATE__MAX;
769 qemu_mutex_unlock(&vmstop_lock);
770 return *r < RUN_STATE__MAX;
773 void qemu_system_vmstop_request_prepare(void)
775 qemu_mutex_lock(&vmstop_lock);
778 void qemu_system_vmstop_request(RunState state)
780 vmstop_requested = state;
781 qemu_mutex_unlock(&vmstop_lock);
782 qemu_notify_event();
785 void vm_start(void)
787 RunState requested;
789 qemu_vmstop_requested(&requested);
790 if (runstate_is_running() && requested == RUN_STATE__MAX) {
791 return;
794 /* Ensure that a STOP/RESUME pair of events is emitted if a
795 * vmstop request was pending. The BLOCK_IO_ERROR event, for
796 * example, according to documentation is always followed by
797 * the STOP event.
799 if (runstate_is_running()) {
800 qapi_event_send_stop(&error_abort);
801 } else {
802 replay_enable_events();
803 cpu_enable_ticks();
804 runstate_set(RUN_STATE_RUNNING);
805 vm_state_notify(1, RUN_STATE_RUNNING);
806 resume_all_vcpus();
809 qapi_event_send_resume(&error_abort);
813 /***********************************************************/
814 /* real time host monotonic timer */
816 static time_t qemu_time(void)
818 return qemu_clock_get_ms(QEMU_CLOCK_HOST) / 1000;
821 /***********************************************************/
822 /* host time/date access */
823 void qemu_get_timedate(struct tm *tm, int offset)
825 time_t ti = qemu_time();
827 ti += offset;
828 if (rtc_date_offset == -1) {
829 if (rtc_utc)
830 gmtime_r(&ti, tm);
831 else
832 localtime_r(&ti, tm);
833 } else {
834 ti -= rtc_date_offset;
835 gmtime_r(&ti, tm);
839 int qemu_timedate_diff(struct tm *tm)
841 time_t seconds;
843 if (rtc_date_offset == -1)
844 if (rtc_utc)
845 seconds = mktimegm(tm);
846 else {
847 struct tm tmp = *tm;
848 tmp.tm_isdst = -1; /* use timezone to figure it out */
849 seconds = mktime(&tmp);
851 else
852 seconds = mktimegm(tm) + rtc_date_offset;
854 return seconds - qemu_time();
857 static void configure_rtc_date_offset(const char *startdate, int legacy)
859 time_t rtc_start_date;
860 struct tm tm;
862 if (!strcmp(startdate, "now") && legacy) {
863 rtc_date_offset = -1;
864 } else {
865 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
866 &tm.tm_year,
867 &tm.tm_mon,
868 &tm.tm_mday,
869 &tm.tm_hour,
870 &tm.tm_min,
871 &tm.tm_sec) == 6) {
872 /* OK */
873 } else if (sscanf(startdate, "%d-%d-%d",
874 &tm.tm_year,
875 &tm.tm_mon,
876 &tm.tm_mday) == 3) {
877 tm.tm_hour = 0;
878 tm.tm_min = 0;
879 tm.tm_sec = 0;
880 } else {
881 goto date_fail;
883 tm.tm_year -= 1900;
884 tm.tm_mon--;
885 rtc_start_date = mktimegm(&tm);
886 if (rtc_start_date == -1) {
887 date_fail:
888 error_report("invalid date format");
889 error_printf("valid formats: "
890 "'2006-06-17T16:01:21' or '2006-06-17'\n");
891 exit(1);
893 rtc_date_offset = qemu_time() - rtc_start_date;
897 static void configure_rtc(QemuOpts *opts)
899 const char *value;
901 value = qemu_opt_get(opts, "base");
902 if (value) {
903 if (!strcmp(value, "utc")) {
904 rtc_utc = 1;
905 } else if (!strcmp(value, "localtime")) {
906 Error *blocker = NULL;
907 rtc_utc = 0;
908 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
909 "-rtc base=localtime");
910 replay_add_blocker(blocker);
911 } else {
912 configure_rtc_date_offset(value, 0);
915 value = qemu_opt_get(opts, "clock");
916 if (value) {
917 if (!strcmp(value, "host")) {
918 rtc_clock = QEMU_CLOCK_HOST;
919 } else if (!strcmp(value, "rt")) {
920 rtc_clock = QEMU_CLOCK_REALTIME;
921 } else if (!strcmp(value, "vm")) {
922 rtc_clock = QEMU_CLOCK_VIRTUAL;
923 } else {
924 error_report("invalid option value '%s'", value);
925 exit(1);
928 value = qemu_opt_get(opts, "driftfix");
929 if (value) {
930 if (!strcmp(value, "slew")) {
931 static GlobalProperty slew_lost_ticks = {
932 .driver = "mc146818rtc",
933 .property = "lost_tick_policy",
934 .value = "slew",
937 qdev_prop_register_global(&slew_lost_ticks);
938 } else if (!strcmp(value, "none")) {
939 /* discard is default */
940 } else {
941 error_report("invalid option value '%s'", value);
942 exit(1);
947 /***********************************************************/
948 /* Bluetooth support */
949 static int nb_hcis;
950 static int cur_hci;
951 static struct HCIInfo *hci_table[MAX_NICS];
953 struct HCIInfo *qemu_next_hci(void)
955 if (cur_hci == nb_hcis)
956 return &null_hci;
958 return hci_table[cur_hci++];
961 static int bt_hci_parse(const char *str)
963 struct HCIInfo *hci;
964 bdaddr_t bdaddr;
966 if (nb_hcis >= MAX_NICS) {
967 error_report("too many bluetooth HCIs (max %i)", MAX_NICS);
968 return -1;
971 hci = hci_init(str);
972 if (!hci)
973 return -1;
975 bdaddr.b[0] = 0x52;
976 bdaddr.b[1] = 0x54;
977 bdaddr.b[2] = 0x00;
978 bdaddr.b[3] = 0x12;
979 bdaddr.b[4] = 0x34;
980 bdaddr.b[5] = 0x56 + nb_hcis;
981 hci->bdaddr_set(hci, bdaddr.b);
983 hci_table[nb_hcis++] = hci;
985 return 0;
988 static void bt_vhci_add(int vlan_id)
990 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
992 if (!vlan->slave)
993 error_report("warning: adding a VHCI to an empty scatternet %i",
994 vlan_id);
996 bt_vhci_init(bt_new_hci(vlan));
999 static struct bt_device_s *bt_device_add(const char *opt)
1001 struct bt_scatternet_s *vlan;
1002 int vlan_id = 0;
1003 char *endp = strstr(opt, ",vlan=");
1004 int len = (endp ? endp - opt : strlen(opt)) + 1;
1005 char devname[10];
1007 pstrcpy(devname, MIN(sizeof(devname), len), opt);
1009 if (endp) {
1010 vlan_id = strtol(endp + 6, &endp, 0);
1011 if (*endp) {
1012 error_report("unrecognised bluetooth vlan Id");
1013 return 0;
1017 vlan = qemu_find_bt_vlan(vlan_id);
1019 if (!vlan->slave)
1020 error_report("warning: adding a slave device to an empty scatternet %i",
1021 vlan_id);
1023 if (!strcmp(devname, "keyboard"))
1024 return bt_keyboard_init(vlan);
1026 error_report("unsupported bluetooth device '%s'", devname);
1027 return 0;
1030 static int bt_parse(const char *opt)
1032 const char *endp, *p;
1033 int vlan;
1035 if (strstart(opt, "hci", &endp)) {
1036 if (!*endp || *endp == ',') {
1037 if (*endp)
1038 if (!strstart(endp, ",vlan=", 0))
1039 opt = endp + 1;
1041 return bt_hci_parse(opt);
1043 } else if (strstart(opt, "vhci", &endp)) {
1044 if (!*endp || *endp == ',') {
1045 if (*endp) {
1046 if (strstart(endp, ",vlan=", &p)) {
1047 vlan = strtol(p, (char **) &endp, 0);
1048 if (*endp) {
1049 error_report("bad scatternet '%s'", p);
1050 return 1;
1052 } else {
1053 error_report("bad parameter '%s'", endp + 1);
1054 return 1;
1056 } else
1057 vlan = 0;
1059 bt_vhci_add(vlan);
1060 return 0;
1062 } else if (strstart(opt, "device:", &endp))
1063 return !bt_device_add(endp);
1065 error_report("bad bluetooth parameter '%s'", opt);
1066 return 1;
1069 static int parse_sandbox(void *opaque, QemuOpts *opts, Error **errp)
1071 /* FIXME: change this to true for 1.3 */
1072 if (qemu_opt_get_bool(opts, "enable", false)) {
1073 #ifdef CONFIG_SECCOMP
1074 if (seccomp_start() < 0) {
1075 error_report("failed to install seccomp syscall filter "
1076 "in the kernel");
1077 return -1;
1079 #else
1080 error_report("seccomp support is disabled");
1081 return -1;
1082 #endif
1085 return 0;
1088 static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
1090 const char *proc_name;
1092 if (qemu_opt_get(opts, "debug-threads")) {
1093 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
1095 qemu_name = qemu_opt_get(opts, "guest");
1097 proc_name = qemu_opt_get(opts, "process");
1098 if (proc_name) {
1099 os_set_proc_name(proc_name);
1102 return 0;
1105 bool defaults_enabled(void)
1107 return has_defaults;
1110 #ifndef _WIN32
1111 static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1113 int fd, dupfd, flags;
1114 int64_t fdset_id;
1115 const char *fd_opaque = NULL;
1116 AddfdInfo *fdinfo;
1118 fd = qemu_opt_get_number(opts, "fd", -1);
1119 fdset_id = qemu_opt_get_number(opts, "set", -1);
1120 fd_opaque = qemu_opt_get(opts, "opaque");
1122 if (fd < 0) {
1123 error_report("fd option is required and must be non-negative");
1124 return -1;
1127 if (fd <= STDERR_FILENO) {
1128 error_report("fd cannot be a standard I/O stream");
1129 return -1;
1133 * All fds inherited across exec() necessarily have FD_CLOEXEC
1134 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1136 flags = fcntl(fd, F_GETFD);
1137 if (flags == -1 || (flags & FD_CLOEXEC)) {
1138 error_report("fd is not valid or already in use");
1139 return -1;
1142 if (fdset_id < 0) {
1143 error_report("set option is required and must be non-negative");
1144 return -1;
1147 #ifdef F_DUPFD_CLOEXEC
1148 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1149 #else
1150 dupfd = dup(fd);
1151 if (dupfd != -1) {
1152 qemu_set_cloexec(dupfd);
1154 #endif
1155 if (dupfd == -1) {
1156 error_report("error duplicating fd: %s", strerror(errno));
1157 return -1;
1160 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1161 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
1162 &error_abort);
1163 g_free(fdinfo);
1165 return 0;
1168 static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1170 int fd;
1172 fd = qemu_opt_get_number(opts, "fd", -1);
1173 close(fd);
1175 return 0;
1177 #endif
1179 /***********************************************************/
1180 /* QEMU Block devices */
1182 #define HD_OPTS "media=disk"
1183 #define CDROM_OPTS "media=cdrom"
1184 #define FD_OPTS ""
1185 #define PFLASH_OPTS ""
1186 #define MTD_OPTS ""
1187 #define SD_OPTS ""
1189 static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
1191 BlockInterfaceType *block_default_type = opaque;
1193 return drive_new(opts, *block_default_type) == NULL;
1196 static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
1198 if (qemu_opt_get(opts, "snapshot") == NULL) {
1199 qemu_opt_set(opts, "snapshot", "on", &error_abort);
1201 return 0;
1204 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1205 int index, const char *optstr)
1207 QemuOpts *opts;
1208 DriveInfo *dinfo;
1210 if (!enable || drive_get_by_index(type, index)) {
1211 return;
1214 opts = drive_add(type, index, NULL, optstr);
1215 if (snapshot) {
1216 drive_enable_snapshot(NULL, opts, NULL);
1219 dinfo = drive_new(opts, type);
1220 if (!dinfo) {
1221 exit(1);
1223 dinfo->is_default = true;
1227 static QemuOptsList qemu_smp_opts = {
1228 .name = "smp-opts",
1229 .implied_opt_name = "cpus",
1230 .merge_lists = true,
1231 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
1232 .desc = {
1234 .name = "cpus",
1235 .type = QEMU_OPT_NUMBER,
1236 }, {
1237 .name = "sockets",
1238 .type = QEMU_OPT_NUMBER,
1239 }, {
1240 .name = "cores",
1241 .type = QEMU_OPT_NUMBER,
1242 }, {
1243 .name = "threads",
1244 .type = QEMU_OPT_NUMBER,
1245 }, {
1246 .name = "maxcpus",
1247 .type = QEMU_OPT_NUMBER,
1249 { /*End of list */ }
1253 static void smp_parse(QemuOpts *opts)
1255 if (opts) {
1256 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
1257 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
1258 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
1259 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
1261 /* compute missing values, prefer sockets over cores over threads */
1262 if (cpus == 0 || sockets == 0) {
1263 sockets = sockets > 0 ? sockets : 1;
1264 cores = cores > 0 ? cores : 1;
1265 threads = threads > 0 ? threads : 1;
1266 if (cpus == 0) {
1267 cpus = cores * threads * sockets;
1269 } else if (cores == 0) {
1270 threads = threads > 0 ? threads : 1;
1271 cores = cpus / (sockets * threads);
1272 cores = cores > 0 ? cores : 1;
1273 } else if (threads == 0) {
1274 threads = cpus / (cores * sockets);
1275 threads = threads > 0 ? threads : 1;
1276 } else if (sockets * cores * threads < cpus) {
1277 error_report("cpu topology: "
1278 "sockets (%u) * cores (%u) * threads (%u) < "
1279 "smp_cpus (%u)",
1280 sockets, cores, threads, cpus);
1281 exit(1);
1284 max_cpus = qemu_opt_get_number(opts, "maxcpus", cpus);
1286 if (max_cpus < cpus) {
1287 error_report("maxcpus must be equal to or greater than smp");
1288 exit(1);
1291 if (sockets * cores * threads > max_cpus) {
1292 error_report("cpu topology: "
1293 "sockets (%u) * cores (%u) * threads (%u) > "
1294 "maxcpus (%u)",
1295 sockets, cores, threads, max_cpus);
1296 exit(1);
1299 smp_cpus = cpus;
1300 smp_cores = cores;
1301 smp_threads = threads;
1304 if (smp_cpus > 1) {
1305 Error *blocker = NULL;
1306 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
1307 replay_add_blocker(blocker);
1311 static void realtime_init(void)
1313 if (enable_mlock) {
1314 if (os_mlock() < 0) {
1315 error_report("locking memory failed");
1316 exit(1);
1322 static void configure_msg(QemuOpts *opts)
1324 enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
1327 /***********************************************************/
1328 /* Semihosting */
1330 typedef struct SemihostingConfig {
1331 bool enabled;
1332 SemihostingTarget target;
1333 const char **argv;
1334 int argc;
1335 const char *cmdline; /* concatenated argv */
1336 } SemihostingConfig;
1338 static SemihostingConfig semihosting;
1340 bool semihosting_enabled(void)
1342 return semihosting.enabled;
1345 SemihostingTarget semihosting_get_target(void)
1347 return semihosting.target;
1350 const char *semihosting_get_arg(int i)
1352 if (i >= semihosting.argc) {
1353 return NULL;
1355 return semihosting.argv[i];
1358 int semihosting_get_argc(void)
1360 return semihosting.argc;
1363 const char *semihosting_get_cmdline(void)
1365 if (semihosting.cmdline == NULL && semihosting.argc > 0) {
1366 semihosting.cmdline = g_strjoinv(" ", (gchar **)semihosting.argv);
1368 return semihosting.cmdline;
1371 static int add_semihosting_arg(void *opaque,
1372 const char *name, const char *val,
1373 Error **errp)
1375 SemihostingConfig *s = opaque;
1376 if (strcmp(name, "arg") == 0) {
1377 s->argc++;
1378 /* one extra element as g_strjoinv() expects NULL-terminated array */
1379 s->argv = g_realloc(s->argv, (s->argc + 1) * sizeof(void *));
1380 s->argv[s->argc - 1] = val;
1381 s->argv[s->argc] = NULL;
1383 return 0;
1386 /* Use strings passed via -kernel/-append to initialize semihosting.argv[] */
1387 static inline void semihosting_arg_fallback(const char *file, const char *cmd)
1389 char *cmd_token;
1391 /* argv[0] */
1392 add_semihosting_arg(&semihosting, "arg", file, NULL);
1394 /* split -append and initialize argv[1..n] */
1395 cmd_token = strtok(g_strdup(cmd), " ");
1396 while (cmd_token) {
1397 add_semihosting_arg(&semihosting, "arg", cmd_token, NULL);
1398 cmd_token = strtok(NULL, " ");
1402 /* Now we still need this for compatibility with XEN. */
1403 bool has_igd_gfx_passthru;
1404 static void igd_gfx_passthru(void)
1406 has_igd_gfx_passthru = current_machine->igd_gfx_passthru;
1409 /***********************************************************/
1410 /* USB devices */
1412 static int usb_device_add(const char *devname)
1414 USBDevice *dev = NULL;
1415 #ifndef CONFIG_LINUX
1416 const char *p;
1417 #endif
1419 if (!machine_usb(current_machine)) {
1420 return -1;
1423 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1424 dev = usbdevice_create(devname);
1425 if (dev)
1426 goto done;
1428 /* the other ones */
1429 #ifndef CONFIG_LINUX
1430 /* only the linux version is qdev-ified, usb-bsd still needs this */
1431 if (strstart(devname, "host:", &p)) {
1432 dev = usb_host_device_open(usb_bus_find(-1), p);
1434 #endif
1435 if (!dev)
1436 return -1;
1438 done:
1439 return 0;
1442 static int usb_device_del(const char *devname)
1444 int bus_num, addr;
1445 const char *p;
1447 if (strstart(devname, "host:", &p)) {
1448 return -1;
1451 if (!machine_usb(current_machine)) {
1452 return -1;
1455 p = strchr(devname, '.');
1456 if (!p)
1457 return -1;
1458 bus_num = strtoul(devname, NULL, 0);
1459 addr = strtoul(p + 1, NULL, 0);
1461 return usb_device_delete_addr(bus_num, addr);
1464 static int usb_parse(const char *cmdline)
1466 int r;
1467 r = usb_device_add(cmdline);
1468 if (r < 0) {
1469 error_report("could not add USB device '%s'", cmdline);
1471 return r;
1474 void hmp_usb_add(Monitor *mon, const QDict *qdict)
1476 const char *devname = qdict_get_str(qdict, "devname");
1477 if (usb_device_add(devname) < 0) {
1478 error_report("could not add USB device '%s'", devname);
1482 void hmp_usb_del(Monitor *mon, const QDict *qdict)
1484 const char *devname = qdict_get_str(qdict, "devname");
1485 if (usb_device_del(devname) < 0) {
1486 error_report("could not delete USB device '%s'", devname);
1490 /***********************************************************/
1491 /* machine registration */
1493 MachineState *current_machine;
1495 static MachineClass *find_machine(const char *name)
1497 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1498 MachineClass *mc = NULL;
1500 for (el = machines; el; el = el->next) {
1501 MachineClass *temp = el->data;
1503 if (!strcmp(temp->name, name)) {
1504 mc = temp;
1505 break;
1507 if (temp->alias &&
1508 !strcmp(temp->alias, name)) {
1509 mc = temp;
1510 break;
1514 g_slist_free(machines);
1515 return mc;
1518 MachineClass *find_default_machine(void)
1520 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1521 MachineClass *mc = NULL;
1523 for (el = machines; el; el = el->next) {
1524 MachineClass *temp = el->data;
1526 if (temp->is_default) {
1527 mc = temp;
1528 break;
1532 g_slist_free(machines);
1533 return mc;
1536 MachineInfoList *qmp_query_machines(Error **errp)
1538 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1539 MachineInfoList *mach_list = NULL;
1541 for (el = machines; el; el = el->next) {
1542 MachineClass *mc = el->data;
1543 MachineInfoList *entry;
1544 MachineInfo *info;
1546 info = g_malloc0(sizeof(*info));
1547 if (mc->is_default) {
1548 info->has_is_default = true;
1549 info->is_default = true;
1552 if (mc->alias) {
1553 info->has_alias = true;
1554 info->alias = g_strdup(mc->alias);
1557 info->name = g_strdup(mc->name);
1558 info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
1559 info->hotpluggable_cpus = !!mc->query_hotpluggable_cpus;
1561 entry = g_malloc0(sizeof(*entry));
1562 entry->value = info;
1563 entry->next = mach_list;
1564 mach_list = entry;
1567 g_slist_free(machines);
1568 return mach_list;
1571 static int machine_help_func(QemuOpts *opts, MachineState *machine)
1573 ObjectProperty *prop;
1574 ObjectPropertyIterator iter;
1576 if (!qemu_opt_has_help_opt(opts)) {
1577 return 0;
1580 object_property_iter_init(&iter, OBJECT(machine));
1581 while ((prop = object_property_iter_next(&iter))) {
1582 if (!prop->set) {
1583 continue;
1586 error_printf("%s.%s=%s", MACHINE_GET_CLASS(machine)->name,
1587 prop->name, prop->type);
1588 if (prop->description) {
1589 error_printf(" (%s)\n", prop->description);
1590 } else {
1591 error_printf("\n");
1595 return 1;
1598 /***********************************************************/
1599 /* main execution loop */
1601 struct vm_change_state_entry {
1602 VMChangeStateHandler *cb;
1603 void *opaque;
1604 QLIST_ENTRY (vm_change_state_entry) entries;
1607 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1609 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1610 void *opaque)
1612 VMChangeStateEntry *e;
1614 e = g_malloc0(sizeof (*e));
1616 e->cb = cb;
1617 e->opaque = opaque;
1618 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1619 return e;
1622 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1624 QLIST_REMOVE (e, entries);
1625 g_free (e);
1628 void vm_state_notify(int running, RunState state)
1630 VMChangeStateEntry *e, *next;
1632 trace_vm_state_notify(running, state);
1634 QLIST_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
1635 e->cb(e->opaque, running, state);
1639 static int reset_requested;
1640 static int shutdown_requested, shutdown_signal = -1;
1641 static pid_t shutdown_pid;
1642 static int powerdown_requested;
1643 static int debug_requested;
1644 static int suspend_requested;
1645 static WakeupReason wakeup_reason;
1646 static NotifierList powerdown_notifiers =
1647 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
1648 static NotifierList suspend_notifiers =
1649 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1650 static NotifierList wakeup_notifiers =
1651 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1652 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
1654 int qemu_shutdown_requested_get(void)
1656 return shutdown_requested;
1659 int qemu_reset_requested_get(void)
1661 return reset_requested;
1664 static int qemu_shutdown_requested(void)
1666 return atomic_xchg(&shutdown_requested, 0);
1669 static void qemu_kill_report(void)
1671 if (!qtest_driver() && shutdown_signal != -1) {
1672 if (shutdown_pid == 0) {
1673 /* This happens for eg ^C at the terminal, so it's worth
1674 * avoiding printing an odd message in that case.
1676 error_report("terminating on signal %d", shutdown_signal);
1677 } else {
1678 char *shutdown_cmd = qemu_get_pid_name(shutdown_pid);
1680 error_report("terminating on signal %d from pid " FMT_pid " (%s)",
1681 shutdown_signal, shutdown_pid,
1682 shutdown_cmd ? shutdown_cmd : "<unknown process>");
1683 g_free(shutdown_cmd);
1685 shutdown_signal = -1;
1689 static int qemu_reset_requested(void)
1691 int r = reset_requested;
1692 if (r && replay_checkpoint(CHECKPOINT_RESET_REQUESTED)) {
1693 reset_requested = 0;
1694 return r;
1696 return false;
1699 static int qemu_suspend_requested(void)
1701 int r = suspend_requested;
1702 if (r && replay_checkpoint(CHECKPOINT_SUSPEND_REQUESTED)) {
1703 suspend_requested = 0;
1704 return r;
1706 return false;
1709 static WakeupReason qemu_wakeup_requested(void)
1711 return wakeup_reason;
1714 static int qemu_powerdown_requested(void)
1716 int r = powerdown_requested;
1717 powerdown_requested = 0;
1718 return r;
1721 static int qemu_debug_requested(void)
1723 int r = debug_requested;
1724 debug_requested = 0;
1725 return r;
1728 void qemu_system_reset(bool report)
1730 MachineClass *mc;
1732 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1734 cpu_synchronize_all_states();
1736 if (mc && mc->reset) {
1737 mc->reset();
1738 } else {
1739 qemu_devices_reset();
1741 if (report) {
1742 qapi_event_send_reset(&error_abort);
1744 cpu_synchronize_all_post_reset();
1747 void qemu_system_guest_panicked(void)
1749 if (current_cpu) {
1750 current_cpu->crash_occurred = true;
1752 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE, &error_abort);
1753 vm_stop(RUN_STATE_GUEST_PANICKED);
1754 if (!no_shutdown) {
1755 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_POWEROFF,
1756 &error_abort);
1757 qemu_system_shutdown_request();
1761 void qemu_system_reset_request(void)
1763 if (no_reboot) {
1764 shutdown_requested = 1;
1765 } else {
1766 reset_requested = 1;
1768 cpu_stop_current();
1769 qemu_notify_event();
1772 static void qemu_system_suspend(void)
1774 pause_all_vcpus();
1775 notifier_list_notify(&suspend_notifiers, NULL);
1776 runstate_set(RUN_STATE_SUSPENDED);
1777 qapi_event_send_suspend(&error_abort);
1780 void qemu_system_suspend_request(void)
1782 if (runstate_check(RUN_STATE_SUSPENDED)) {
1783 return;
1785 suspend_requested = 1;
1786 cpu_stop_current();
1787 qemu_notify_event();
1790 void qemu_register_suspend_notifier(Notifier *notifier)
1792 notifier_list_add(&suspend_notifiers, notifier);
1795 void qemu_system_wakeup_request(WakeupReason reason)
1797 trace_system_wakeup_request(reason);
1799 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1800 return;
1802 if (!(wakeup_reason_mask & (1 << reason))) {
1803 return;
1805 runstate_set(RUN_STATE_RUNNING);
1806 wakeup_reason = reason;
1807 qemu_notify_event();
1810 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1812 if (enabled) {
1813 wakeup_reason_mask |= (1 << reason);
1814 } else {
1815 wakeup_reason_mask &= ~(1 << reason);
1819 void qemu_register_wakeup_notifier(Notifier *notifier)
1821 notifier_list_add(&wakeup_notifiers, notifier);
1824 void qemu_system_killed(int signal, pid_t pid)
1826 shutdown_signal = signal;
1827 shutdown_pid = pid;
1828 no_shutdown = 0;
1830 /* Cannot call qemu_system_shutdown_request directly because
1831 * we are in a signal handler.
1833 shutdown_requested = 1;
1834 qemu_notify_event();
1837 void qemu_system_shutdown_request(void)
1839 trace_qemu_system_shutdown_request();
1840 replay_shutdown_request();
1841 shutdown_requested = 1;
1842 qemu_notify_event();
1845 static void qemu_system_powerdown(void)
1847 qapi_event_send_powerdown(&error_abort);
1848 notifier_list_notify(&powerdown_notifiers, NULL);
1851 void qemu_system_powerdown_request(void)
1853 trace_qemu_system_powerdown_request();
1854 powerdown_requested = 1;
1855 qemu_notify_event();
1858 void qemu_register_powerdown_notifier(Notifier *notifier)
1860 notifier_list_add(&powerdown_notifiers, notifier);
1863 void qemu_system_debug_request(void)
1865 debug_requested = 1;
1866 qemu_notify_event();
1869 static bool main_loop_should_exit(void)
1871 RunState r;
1872 if (qemu_debug_requested()) {
1873 vm_stop(RUN_STATE_DEBUG);
1875 if (qemu_suspend_requested()) {
1876 qemu_system_suspend();
1878 if (qemu_shutdown_requested()) {
1879 qemu_kill_report();
1880 qapi_event_send_shutdown(&error_abort);
1881 if (no_shutdown) {
1882 vm_stop(RUN_STATE_SHUTDOWN);
1883 } else {
1884 return true;
1887 if (qemu_reset_requested()) {
1888 pause_all_vcpus();
1889 qemu_system_reset(VMRESET_REPORT);
1890 resume_all_vcpus();
1891 if (!runstate_check(RUN_STATE_RUNNING) &&
1892 !runstate_check(RUN_STATE_INMIGRATE)) {
1893 runstate_set(RUN_STATE_PRELAUNCH);
1896 if (qemu_wakeup_requested()) {
1897 pause_all_vcpus();
1898 qemu_system_reset(VMRESET_SILENT);
1899 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
1900 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
1901 resume_all_vcpus();
1902 qapi_event_send_wakeup(&error_abort);
1904 if (qemu_powerdown_requested()) {
1905 qemu_system_powerdown();
1907 if (qemu_vmstop_requested(&r)) {
1908 vm_stop(r);
1910 return false;
1913 static void main_loop(void)
1915 bool nonblocking;
1916 int last_io = 0;
1917 #ifdef CONFIG_PROFILER
1918 int64_t ti;
1919 #endif
1920 do {
1921 nonblocking = tcg_enabled() && last_io > 0;
1922 #ifdef CONFIG_PROFILER
1923 ti = profile_getclock();
1924 #endif
1925 last_io = main_loop_wait(nonblocking);
1926 #ifdef CONFIG_PROFILER
1927 dev_time += profile_getclock() - ti;
1928 #endif
1929 } while (!main_loop_should_exit());
1932 static void version(void)
1934 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION "\n"
1935 QEMU_COPYRIGHT "\n");
1938 static void help(int exitcode)
1940 version();
1941 printf("usage: %s [options] [disk_image]\n\n"
1942 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1943 error_get_progname());
1945 #define QEMU_OPTIONS_GENERATE_HELP
1946 #include "qemu-options-wrapper.h"
1948 printf("\nDuring emulation, the following keys are useful:\n"
1949 "ctrl-alt-f toggle full screen\n"
1950 "ctrl-alt-n switch to virtual console 'n'\n"
1951 "ctrl-alt toggle mouse and keyboard grab\n"
1952 "\n"
1953 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1955 exit(exitcode);
1958 #define HAS_ARG 0x0001
1960 typedef struct QEMUOption {
1961 const char *name;
1962 int flags;
1963 int index;
1964 uint32_t arch_mask;
1965 } QEMUOption;
1967 static const QEMUOption qemu_options[] = {
1968 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1969 #define QEMU_OPTIONS_GENERATE_OPTIONS
1970 #include "qemu-options-wrapper.h"
1971 { NULL },
1974 typedef struct VGAInterfaceInfo {
1975 const char *opt_name; /* option name */
1976 const char *name; /* human-readable name */
1977 /* Class names indicating that support is available.
1978 * If no class is specified, the interface is always available */
1979 const char *class_names[2];
1980 } VGAInterfaceInfo;
1982 static VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
1983 [VGA_NONE] = {
1984 .opt_name = "none",
1986 [VGA_STD] = {
1987 .opt_name = "std",
1988 .name = "standard VGA",
1989 .class_names = { "VGA", "isa-vga" },
1991 [VGA_CIRRUS] = {
1992 .opt_name = "cirrus",
1993 .name = "Cirrus VGA",
1994 .class_names = { "cirrus-vga", "isa-cirrus-vga" },
1996 [VGA_VMWARE] = {
1997 .opt_name = "vmware",
1998 .name = "VMWare SVGA",
1999 .class_names = { "vmware-svga" },
2001 [VGA_VIRTIO] = {
2002 .opt_name = "virtio",
2003 .name = "Virtio VGA",
2004 .class_names = { "virtio-vga" },
2006 [VGA_QXL] = {
2007 .opt_name = "qxl",
2008 .name = "QXL VGA",
2009 .class_names = { "qxl-vga" },
2011 [VGA_TCX] = {
2012 .opt_name = "tcx",
2013 .name = "TCX framebuffer",
2014 .class_names = { "SUNW,tcx" },
2016 [VGA_CG3] = {
2017 .opt_name = "cg3",
2018 .name = "CG3 framebuffer",
2019 .class_names = { "cgthree" },
2021 [VGA_XENFB] = {
2022 .opt_name = "xenfb",
2026 static bool vga_interface_available(VGAInterfaceType t)
2028 VGAInterfaceInfo *ti = &vga_interfaces[t];
2030 assert(t < VGA_TYPE_MAX);
2031 return !ti->class_names[0] ||
2032 object_class_by_name(ti->class_names[0]) ||
2033 object_class_by_name(ti->class_names[1]);
2036 static void select_vgahw(const char *p)
2038 const char *opts;
2039 int t;
2041 assert(vga_interface_type == VGA_NONE);
2042 for (t = 0; t < VGA_TYPE_MAX; t++) {
2043 VGAInterfaceInfo *ti = &vga_interfaces[t];
2044 if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
2045 if (!vga_interface_available(t)) {
2046 error_report("%s not available", ti->name);
2047 exit(1);
2049 vga_interface_type = t;
2050 break;
2053 if (t == VGA_TYPE_MAX) {
2054 invalid_vga:
2055 error_report("unknown vga type: %s", p);
2056 exit(1);
2058 while (*opts) {
2059 const char *nextopt;
2061 if (strstart(opts, ",retrace=", &nextopt)) {
2062 opts = nextopt;
2063 if (strstart(opts, "dumb", &nextopt))
2064 vga_retrace_method = VGA_RETRACE_DUMB;
2065 else if (strstart(opts, "precise", &nextopt))
2066 vga_retrace_method = VGA_RETRACE_PRECISE;
2067 else goto invalid_vga;
2068 } else goto invalid_vga;
2069 opts = nextopt;
2073 typedef enum DisplayType {
2074 DT_DEFAULT,
2075 DT_CURSES,
2076 DT_SDL,
2077 DT_COCOA,
2078 DT_GTK,
2079 DT_NONE,
2080 } DisplayType;
2082 static DisplayType select_display(const char *p)
2084 const char *opts;
2085 DisplayType display = DT_DEFAULT;
2087 if (strstart(p, "sdl", &opts)) {
2088 #ifdef CONFIG_SDL
2089 display = DT_SDL;
2090 while (*opts) {
2091 const char *nextopt;
2093 if (strstart(opts, ",frame=", &nextopt)) {
2094 opts = nextopt;
2095 if (strstart(opts, "on", &nextopt)) {
2096 no_frame = 0;
2097 } else if (strstart(opts, "off", &nextopt)) {
2098 no_frame = 1;
2099 } else {
2100 goto invalid_sdl_args;
2102 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
2103 opts = nextopt;
2104 if (strstart(opts, "on", &nextopt)) {
2105 alt_grab = 1;
2106 } else if (strstart(opts, "off", &nextopt)) {
2107 alt_grab = 0;
2108 } else {
2109 goto invalid_sdl_args;
2111 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2112 opts = nextopt;
2113 if (strstart(opts, "on", &nextopt)) {
2114 ctrl_grab = 1;
2115 } else if (strstart(opts, "off", &nextopt)) {
2116 ctrl_grab = 0;
2117 } else {
2118 goto invalid_sdl_args;
2120 } else if (strstart(opts, ",window_close=", &nextopt)) {
2121 opts = nextopt;
2122 if (strstart(opts, "on", &nextopt)) {
2123 no_quit = 0;
2124 } else if (strstart(opts, "off", &nextopt)) {
2125 no_quit = 1;
2126 } else {
2127 goto invalid_sdl_args;
2129 } else if (strstart(opts, ",gl=", &nextopt)) {
2130 opts = nextopt;
2131 if (strstart(opts, "on", &nextopt)) {
2132 request_opengl = 1;
2133 } else if (strstart(opts, "off", &nextopt)) {
2134 request_opengl = 0;
2135 } else {
2136 goto invalid_sdl_args;
2138 } else {
2139 invalid_sdl_args:
2140 error_report("invalid SDL option string");
2141 exit(1);
2143 opts = nextopt;
2145 #else
2146 error_report("SDL support is disabled");
2147 exit(1);
2148 #endif
2149 } else if (strstart(p, "vnc", &opts)) {
2150 if (*opts == '=') {
2151 vnc_parse(opts + 1, &error_fatal);
2152 } else {
2153 error_report("VNC requires a display argument vnc=<display>");
2154 exit(1);
2156 } else if (strstart(p, "curses", &opts)) {
2157 #ifdef CONFIG_CURSES
2158 display = DT_CURSES;
2159 #else
2160 error_report("curses support is disabled");
2161 exit(1);
2162 #endif
2163 } else if (strstart(p, "gtk", &opts)) {
2164 #ifdef CONFIG_GTK
2165 display = DT_GTK;
2166 while (*opts) {
2167 const char *nextopt;
2169 if (strstart(opts, ",grab_on_hover=", &nextopt)) {
2170 opts = nextopt;
2171 if (strstart(opts, "on", &nextopt)) {
2172 grab_on_hover = true;
2173 } else if (strstart(opts, "off", &nextopt)) {
2174 grab_on_hover = false;
2175 } else {
2176 goto invalid_gtk_args;
2178 } else if (strstart(opts, ",gl=", &nextopt)) {
2179 opts = nextopt;
2180 if (strstart(opts, "on", &nextopt)) {
2181 request_opengl = 1;
2182 } else if (strstart(opts, "off", &nextopt)) {
2183 request_opengl = 0;
2184 } else {
2185 goto invalid_gtk_args;
2187 } else {
2188 invalid_gtk_args:
2189 error_report("invalid GTK option string");
2190 exit(1);
2192 opts = nextopt;
2194 #else
2195 error_report("GTK support is disabled");
2196 exit(1);
2197 #endif
2198 } else if (strstart(p, "none", &opts)) {
2199 display = DT_NONE;
2200 } else {
2201 error_report("unknown display type");
2202 exit(1);
2205 return display;
2208 static int balloon_parse(const char *arg)
2210 QemuOpts *opts;
2212 if (strcmp(arg, "none") == 0) {
2213 return 0;
2216 if (!strncmp(arg, "virtio", 6)) {
2217 if (arg[6] == ',') {
2218 /* have params -> parse them */
2219 opts = qemu_opts_parse_noisily(qemu_find_opts("device"), arg + 7,
2220 false);
2221 if (!opts)
2222 return -1;
2223 } else {
2224 /* create empty opts */
2225 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2226 &error_abort);
2228 qemu_opt_set(opts, "driver", "virtio-balloon", &error_abort);
2229 return 0;
2232 return -1;
2235 char *qemu_find_file(int type, const char *name)
2237 int i;
2238 const char *subdir;
2239 char *buf;
2241 /* Try the name as a straight path first */
2242 if (access(name, R_OK) == 0) {
2243 trace_load_file(name, name);
2244 return g_strdup(name);
2247 switch (type) {
2248 case QEMU_FILE_TYPE_BIOS:
2249 subdir = "";
2250 break;
2251 case QEMU_FILE_TYPE_KEYMAP:
2252 subdir = "keymaps/";
2253 break;
2254 default:
2255 abort();
2258 for (i = 0; i < data_dir_idx; i++) {
2259 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2260 if (access(buf, R_OK) == 0) {
2261 trace_load_file(name, buf);
2262 return buf;
2264 g_free(buf);
2266 return NULL;
2269 static inline bool nonempty_str(const char *str)
2271 return str && *str;
2274 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
2276 gchar *buf;
2277 size_t size;
2278 const char *name, *file, *str;
2279 FWCfgState *fw_cfg = (FWCfgState *) opaque;
2281 if (fw_cfg == NULL) {
2282 error_report("fw_cfg device not available");
2283 return -1;
2285 name = qemu_opt_get(opts, "name");
2286 file = qemu_opt_get(opts, "file");
2287 str = qemu_opt_get(opts, "string");
2289 /* we need name and either a file or the content string */
2290 if (!(nonempty_str(name) && (nonempty_str(file) || nonempty_str(str)))) {
2291 error_report("invalid argument(s)");
2292 return -1;
2294 if (nonempty_str(file) && nonempty_str(str)) {
2295 error_report("file and string are mutually exclusive");
2296 return -1;
2298 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
2299 error_report("name too long (max. %d char)", FW_CFG_MAX_FILE_PATH - 1);
2300 return -1;
2302 if (strncmp(name, "opt/", 4) != 0) {
2303 error_report("warning: externally provided fw_cfg item names "
2304 "should be prefixed with \"opt/\"");
2306 if (nonempty_str(str)) {
2307 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
2308 buf = g_memdup(str, size);
2309 } else {
2310 if (!g_file_get_contents(file, &buf, &size, NULL)) {
2311 error_report("can't load %s", file);
2312 return -1;
2315 /* For legacy, keep user files in a specific global order. */
2316 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
2317 fw_cfg_add_file(fw_cfg, name, buf, size);
2318 fw_cfg_reset_order_override(fw_cfg);
2319 return 0;
2322 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
2324 return qdev_device_help(opts);
2327 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
2329 Error *err = NULL;
2330 DeviceState *dev;
2332 dev = qdev_device_add(opts, &err);
2333 if (!dev) {
2334 error_report_err(err);
2335 return -1;
2337 object_unref(OBJECT(dev));
2338 return 0;
2341 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2343 Error *local_err = NULL;
2345 qemu_chr_new_from_opts(opts, &local_err);
2346 if (local_err) {
2347 error_report_err(local_err);
2348 return -1;
2350 return 0;
2353 #ifdef CONFIG_VIRTFS
2354 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2356 return qemu_fsdev_add(opts);
2358 #endif
2360 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
2362 CharDriverState *chr;
2363 const char *chardev;
2364 const char *mode;
2365 int flags;
2367 mode = qemu_opt_get(opts, "mode");
2368 if (mode == NULL) {
2369 mode = "readline";
2371 if (strcmp(mode, "readline") == 0) {
2372 flags = MONITOR_USE_READLINE;
2373 } else if (strcmp(mode, "control") == 0) {
2374 flags = MONITOR_USE_CONTROL;
2375 } else {
2376 error_report("unknown monitor mode \"%s\"", mode);
2377 exit(1);
2380 if (qemu_opt_get_bool(opts, "pretty", 0))
2381 flags |= MONITOR_USE_PRETTY;
2383 if (qemu_opt_get_bool(opts, "default", 0)) {
2384 error_report("option 'default' does nothing and is deprecated");
2387 chardev = qemu_opt_get(opts, "chardev");
2388 chr = qemu_chr_find(chardev);
2389 if (chr == NULL) {
2390 error_report("chardev \"%s\" not found", chardev);
2391 exit(1);
2394 monitor_init(chr, flags);
2395 return 0;
2398 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
2400 static int monitor_device_index = 0;
2401 QemuOpts *opts;
2402 const char *p;
2403 char label[32];
2405 if (strstart(optarg, "chardev:", &p)) {
2406 snprintf(label, sizeof(label), "%s", p);
2407 } else {
2408 snprintf(label, sizeof(label), "compat_monitor%d",
2409 monitor_device_index);
2410 opts = qemu_chr_parse_compat(label, optarg);
2411 if (!opts) {
2412 error_report("parse error: %s", optarg);
2413 exit(1);
2417 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
2418 qemu_opt_set(opts, "mode", mode, &error_abort);
2419 qemu_opt_set(opts, "chardev", label, &error_abort);
2420 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
2421 monitor_device_index++;
2424 struct device_config {
2425 enum {
2426 DEV_USB, /* -usbdevice */
2427 DEV_BT, /* -bt */
2428 DEV_SERIAL, /* -serial */
2429 DEV_PARALLEL, /* -parallel */
2430 DEV_VIRTCON, /* -virtioconsole */
2431 DEV_DEBUGCON, /* -debugcon */
2432 DEV_GDB, /* -gdb, -s */
2433 DEV_SCLP, /* s390 sclp */
2434 } type;
2435 const char *cmdline;
2436 Location loc;
2437 QTAILQ_ENTRY(device_config) next;
2440 static QTAILQ_HEAD(, device_config) device_configs =
2441 QTAILQ_HEAD_INITIALIZER(device_configs);
2443 static void add_device_config(int type, const char *cmdline)
2445 struct device_config *conf;
2447 conf = g_malloc0(sizeof(*conf));
2448 conf->type = type;
2449 conf->cmdline = cmdline;
2450 loc_save(&conf->loc);
2451 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2454 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2456 struct device_config *conf;
2457 int rc;
2459 QTAILQ_FOREACH(conf, &device_configs, next) {
2460 if (conf->type != type)
2461 continue;
2462 loc_push_restore(&conf->loc);
2463 rc = func(conf->cmdline);
2464 loc_pop(&conf->loc);
2465 if (rc) {
2466 return rc;
2469 return 0;
2472 static int serial_parse(const char *devname)
2474 static int index = 0;
2475 char label[32];
2477 if (strcmp(devname, "none") == 0)
2478 return 0;
2479 if (index == MAX_SERIAL_PORTS) {
2480 error_report("too many serial ports");
2481 exit(1);
2483 snprintf(label, sizeof(label), "serial%d", index);
2484 serial_hds[index] = qemu_chr_new(label, devname);
2485 if (!serial_hds[index]) {
2486 error_report("could not connect serial device"
2487 " to character backend '%s'", devname);
2488 return -1;
2490 index++;
2491 return 0;
2494 static int parallel_parse(const char *devname)
2496 static int index = 0;
2497 char label[32];
2499 if (strcmp(devname, "none") == 0)
2500 return 0;
2501 if (index == MAX_PARALLEL_PORTS) {
2502 error_report("too many parallel ports");
2503 exit(1);
2505 snprintf(label, sizeof(label), "parallel%d", index);
2506 parallel_hds[index] = qemu_chr_new(label, devname);
2507 if (!parallel_hds[index]) {
2508 error_report("could not connect parallel device"
2509 " to character backend '%s'", devname);
2510 return -1;
2512 index++;
2513 return 0;
2516 static int virtcon_parse(const char *devname)
2518 QemuOptsList *device = qemu_find_opts("device");
2519 static int index = 0;
2520 char label[32];
2521 QemuOpts *bus_opts, *dev_opts;
2523 if (strcmp(devname, "none") == 0)
2524 return 0;
2525 if (index == MAX_VIRTIO_CONSOLES) {
2526 error_report("too many virtio consoles");
2527 exit(1);
2530 bus_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2531 qemu_opt_set(bus_opts, "driver", "virtio-serial", &error_abort);
2533 dev_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2534 qemu_opt_set(dev_opts, "driver", "virtconsole", &error_abort);
2536 snprintf(label, sizeof(label), "virtcon%d", index);
2537 virtcon_hds[index] = qemu_chr_new(label, devname);
2538 if (!virtcon_hds[index]) {
2539 error_report("could not connect virtio console"
2540 " to character backend '%s'", devname);
2541 return -1;
2543 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2545 index++;
2546 return 0;
2549 static int sclp_parse(const char *devname)
2551 QemuOptsList *device = qemu_find_opts("device");
2552 static int index = 0;
2553 char label[32];
2554 QemuOpts *dev_opts;
2556 if (strcmp(devname, "none") == 0) {
2557 return 0;
2559 if (index == MAX_SCLP_CONSOLES) {
2560 error_report("too many sclp consoles");
2561 exit(1);
2564 assert(arch_type == QEMU_ARCH_S390X);
2566 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2567 qemu_opt_set(dev_opts, "driver", "sclpconsole", &error_abort);
2569 snprintf(label, sizeof(label), "sclpcon%d", index);
2570 sclp_hds[index] = qemu_chr_new(label, devname);
2571 if (!sclp_hds[index]) {
2572 error_report("could not connect sclp console"
2573 " to character backend '%s'", devname);
2574 return -1;
2576 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2578 index++;
2579 return 0;
2582 static int debugcon_parse(const char *devname)
2584 QemuOpts *opts;
2586 if (!qemu_chr_new("debugcon", devname)) {
2587 exit(1);
2589 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2590 if (!opts) {
2591 error_report("already have a debugcon device");
2592 exit(1);
2594 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
2595 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
2596 return 0;
2599 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
2601 const MachineClass *mc1 = a, *mc2 = b;
2602 int res;
2604 if (mc1->family == NULL) {
2605 if (mc2->family == NULL) {
2606 /* Compare standalone machine types against each other; they sort
2607 * in increasing order.
2609 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
2610 object_class_get_name(OBJECT_CLASS(mc2)));
2613 /* Standalone machine types sort after families. */
2614 return 1;
2617 if (mc2->family == NULL) {
2618 /* Families sort before standalone machine types. */
2619 return -1;
2622 /* Families sort between each other alphabetically increasingly. */
2623 res = strcmp(mc1->family, mc2->family);
2624 if (res != 0) {
2625 return res;
2628 /* Within the same family, machine types sort in decreasing order. */
2629 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
2630 object_class_get_name(OBJECT_CLASS(mc1)));
2633 static MachineClass *machine_parse(const char *name)
2635 MachineClass *mc = NULL;
2636 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
2638 if (name) {
2639 mc = find_machine(name);
2641 if (mc) {
2642 g_slist_free(machines);
2643 return mc;
2645 if (name && !is_help_option(name)) {
2646 error_report("unsupported machine type");
2647 error_printf("Use -machine help to list supported machines\n");
2648 } else {
2649 printf("Supported machines are:\n");
2650 machines = g_slist_sort(machines, machine_class_cmp);
2651 for (el = machines; el; el = el->next) {
2652 MachineClass *mc = el->data;
2653 if (mc->alias) {
2654 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
2656 printf("%-20s %s%s\n", mc->name, mc->desc,
2657 mc->is_default ? " (default)" : "");
2661 g_slist_free(machines);
2662 exit(!name || !is_help_option(name));
2665 void qemu_add_exit_notifier(Notifier *notify)
2667 notifier_list_add(&exit_notifiers, notify);
2670 void qemu_remove_exit_notifier(Notifier *notify)
2672 notifier_remove(notify);
2675 static void qemu_run_exit_notifiers(void)
2677 notifier_list_notify(&exit_notifiers, NULL);
2680 static bool machine_init_done;
2682 void qemu_add_machine_init_done_notifier(Notifier *notify)
2684 notifier_list_add(&machine_init_done_notifiers, notify);
2685 if (machine_init_done) {
2686 notify->notify(notify, NULL);
2690 void qemu_remove_machine_init_done_notifier(Notifier *notify)
2692 notifier_remove(notify);
2695 static void qemu_run_machine_init_done_notifiers(void)
2697 notifier_list_notify(&machine_init_done_notifiers, NULL);
2698 machine_init_done = true;
2701 static const QEMUOption *lookup_opt(int argc, char **argv,
2702 const char **poptarg, int *poptind)
2704 const QEMUOption *popt;
2705 int optind = *poptind;
2706 char *r = argv[optind];
2707 const char *optarg;
2709 loc_set_cmdline(argv, optind, 1);
2710 optind++;
2711 /* Treat --foo the same as -foo. */
2712 if (r[1] == '-')
2713 r++;
2714 popt = qemu_options;
2715 for(;;) {
2716 if (!popt->name) {
2717 error_report("invalid option");
2718 exit(1);
2720 if (!strcmp(popt->name, r + 1))
2721 break;
2722 popt++;
2724 if (popt->flags & HAS_ARG) {
2725 if (optind >= argc) {
2726 error_report("requires an argument");
2727 exit(1);
2729 optarg = argv[optind++];
2730 loc_set_cmdline(argv, optind - 2, 2);
2731 } else {
2732 optarg = NULL;
2735 *poptarg = optarg;
2736 *poptind = optind;
2738 return popt;
2741 static MachineClass *select_machine(void)
2743 MachineClass *machine_class = find_default_machine();
2744 const char *optarg;
2745 QemuOpts *opts;
2746 Location loc;
2748 loc_push_none(&loc);
2750 opts = qemu_get_machine_opts();
2751 qemu_opts_loc_restore(opts);
2753 optarg = qemu_opt_get(opts, "type");
2754 if (optarg) {
2755 machine_class = machine_parse(optarg);
2758 if (!machine_class) {
2759 error_report("No machine specified, and there is no default");
2760 error_printf("Use -machine help to list supported machines\n");
2761 exit(1);
2764 loc_pop(&loc);
2765 return machine_class;
2768 static int machine_set_property(void *opaque,
2769 const char *name, const char *value,
2770 Error **errp)
2772 Object *obj = OBJECT(opaque);
2773 Error *local_err = NULL;
2774 char *p, *qom_name;
2776 if (strcmp(name, "type") == 0) {
2777 return 0;
2780 qom_name = g_strdup(name);
2781 for (p = qom_name; *p; p++) {
2782 if (*p == '_') {
2783 *p = '-';
2787 object_property_parse(obj, value, qom_name, &local_err);
2788 g_free(qom_name);
2790 if (local_err) {
2791 error_report_err(local_err);
2792 return -1;
2795 return 0;
2800 * Initial object creation happens before all other
2801 * QEMU data types are created. The majority of objects
2802 * can be created at this point. The rng-egd object
2803 * cannot be created here, as it depends on the chardev
2804 * already existing.
2806 static bool object_create_initial(const char *type)
2808 if (g_str_equal(type, "rng-egd")) {
2809 return false;
2813 * return false for concrete netfilters since
2814 * they depend on netdevs already existing
2816 if (g_str_equal(type, "filter-buffer") ||
2817 g_str_equal(type, "filter-dump") ||
2818 g_str_equal(type, "filter-mirror") ||
2819 g_str_equal(type, "filter-redirector") ||
2820 g_str_equal(type, "colo-compare") ||
2821 g_str_equal(type, "filter-rewriter") ||
2822 g_str_equal(type, "filter-replay")) {
2823 return false;
2826 /* Memory allocation by backends needs to be done
2827 * after configure_accelerator() (due to the tcg_enabled()
2828 * checks at memory_region_init_*()).
2830 * Also, allocation of large amounts of memory may delay
2831 * chardev initialization for too long, and trigger timeouts
2832 * on software that waits for a monitor socket to be created
2833 * (e.g. libvirt).
2835 if (g_str_has_prefix(type, "memory-backend-")) {
2836 return false;
2839 return true;
2844 * The remainder of object creation happens after the
2845 * creation of chardev, fsdev, net clients and device data types.
2847 static bool object_create_delayed(const char *type)
2849 return !object_create_initial(type);
2853 static void set_memory_options(uint64_t *ram_slots, ram_addr_t *maxram_size,
2854 MachineClass *mc)
2856 uint64_t sz;
2857 const char *mem_str;
2858 const char *maxmem_str, *slots_str;
2859 const ram_addr_t default_ram_size = mc->default_ram_size;
2860 QemuOpts *opts = qemu_find_opts_singleton("memory");
2861 Location loc;
2863 loc_push_none(&loc);
2864 qemu_opts_loc_restore(opts);
2866 sz = 0;
2867 mem_str = qemu_opt_get(opts, "size");
2868 if (mem_str) {
2869 if (!*mem_str) {
2870 error_report("missing 'size' option value");
2871 exit(EXIT_FAILURE);
2874 sz = qemu_opt_get_size(opts, "size", ram_size);
2876 /* Fix up legacy suffix-less format */
2877 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2878 uint64_t overflow_check = sz;
2880 sz <<= 20;
2881 if ((sz >> 20) != overflow_check) {
2882 error_report("too large 'size' option value");
2883 exit(EXIT_FAILURE);
2888 /* backward compatibility behaviour for case "-m 0" */
2889 if (sz == 0) {
2890 sz = default_ram_size;
2893 sz = QEMU_ALIGN_UP(sz, 8192);
2894 ram_size = sz;
2895 if (ram_size != sz) {
2896 error_report("ram size too large");
2897 exit(EXIT_FAILURE);
2900 /* store value for the future use */
2901 qemu_opt_set_number(opts, "size", ram_size, &error_abort);
2902 *maxram_size = ram_size;
2904 maxmem_str = qemu_opt_get(opts, "maxmem");
2905 slots_str = qemu_opt_get(opts, "slots");
2906 if (maxmem_str && slots_str) {
2907 uint64_t slots;
2909 sz = qemu_opt_get_size(opts, "maxmem", 0);
2910 slots = qemu_opt_get_number(opts, "slots", 0);
2911 if (sz < ram_size) {
2912 error_report("invalid value of -m option maxmem: "
2913 "maximum memory size (0x%" PRIx64 ") must be at least "
2914 "the initial memory size (0x" RAM_ADDR_FMT ")",
2915 sz, ram_size);
2916 exit(EXIT_FAILURE);
2917 } else if (sz > ram_size) {
2918 if (!slots) {
2919 error_report("invalid value of -m option: maxmem was "
2920 "specified, but no hotplug slots were specified");
2921 exit(EXIT_FAILURE);
2923 } else if (slots) {
2924 error_report("invalid value of -m option maxmem: "
2925 "memory slots were specified but maximum memory size "
2926 "(0x%" PRIx64 ") is equal to the initial memory size "
2927 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
2928 exit(EXIT_FAILURE);
2931 *maxram_size = sz;
2932 *ram_slots = slots;
2933 } else if ((!maxmem_str && slots_str) ||
2934 (maxmem_str && !slots_str)) {
2935 error_report("invalid -m option value: missing "
2936 "'%s' option", slots_str ? "maxmem" : "slots");
2937 exit(EXIT_FAILURE);
2940 loc_pop(&loc);
2943 static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
2945 GlobalProperty *g;
2947 g = g_malloc0(sizeof(*g));
2948 g->driver = qemu_opt_get(opts, "driver");
2949 g->property = qemu_opt_get(opts, "property");
2950 g->value = qemu_opt_get(opts, "value");
2951 g->user_provided = true;
2952 g->errp = &error_fatal;
2953 qdev_prop_register_global(g);
2954 return 0;
2957 static int qemu_read_default_config_file(void)
2959 int ret;
2961 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
2962 if (ret < 0 && ret != -ENOENT) {
2963 return ret;
2966 return 0;
2969 int main(int argc, char **argv, char **envp)
2971 int i;
2972 int snapshot, linux_boot;
2973 const char *initrd_filename;
2974 const char *kernel_filename, *kernel_cmdline;
2975 const char *boot_order = NULL;
2976 const char *boot_once = NULL;
2977 DisplayState *ds;
2978 int cyls, heads, secs, translation;
2979 QemuOpts *hda_opts = NULL, *opts, *machine_opts, *icount_opts = NULL;
2980 QemuOptsList *olist;
2981 int optind;
2982 const char *optarg;
2983 const char *loadvm = NULL;
2984 MachineClass *machine_class;
2985 const char *cpu_model;
2986 const char *vga_model = NULL;
2987 const char *qtest_chrdev = NULL;
2988 const char *qtest_log = NULL;
2989 const char *pid_file = NULL;
2990 const char *incoming = NULL;
2991 bool defconfig = true;
2992 bool userconfig = true;
2993 bool nographic = false;
2994 DisplayType display_type = DT_DEFAULT;
2995 int display_remote = 0;
2996 const char *log_mask = NULL;
2997 const char *log_file = NULL;
2998 char *trace_file = NULL;
2999 ram_addr_t maxram_size;
3000 uint64_t ram_slots = 0;
3001 FILE *vmstate_dump_file = NULL;
3002 Error *main_loop_err = NULL;
3003 Error *err = NULL;
3004 bool list_data_dirs = false;
3006 module_call_init(MODULE_INIT_TRACE);
3008 qemu_init_cpu_list();
3009 qemu_init_cpu_loop();
3010 qemu_mutex_lock_iothread();
3012 atexit(qemu_run_exit_notifiers);
3013 error_set_progname(argv[0]);
3014 qemu_init_exec_dir(argv[0]);
3016 module_call_init(MODULE_INIT_QOM);
3017 module_call_init(MODULE_INIT_QAPI);
3019 qemu_add_opts(&qemu_drive_opts);
3020 qemu_add_drive_opts(&qemu_legacy_drive_opts);
3021 qemu_add_drive_opts(&qemu_common_drive_opts);
3022 qemu_add_drive_opts(&qemu_drive_opts);
3023 qemu_add_drive_opts(&bdrv_runtime_opts);
3024 qemu_add_opts(&qemu_chardev_opts);
3025 qemu_add_opts(&qemu_device_opts);
3026 qemu_add_opts(&qemu_netdev_opts);
3027 qemu_add_opts(&qemu_net_opts);
3028 qemu_add_opts(&qemu_rtc_opts);
3029 qemu_add_opts(&qemu_global_opts);
3030 qemu_add_opts(&qemu_mon_opts);
3031 qemu_add_opts(&qemu_trace_opts);
3032 qemu_add_opts(&qemu_option_rom_opts);
3033 qemu_add_opts(&qemu_machine_opts);
3034 qemu_add_opts(&qemu_mem_opts);
3035 qemu_add_opts(&qemu_smp_opts);
3036 qemu_add_opts(&qemu_boot_opts);
3037 qemu_add_opts(&qemu_sandbox_opts);
3038 qemu_add_opts(&qemu_add_fd_opts);
3039 qemu_add_opts(&qemu_object_opts);
3040 qemu_add_opts(&qemu_tpmdev_opts);
3041 qemu_add_opts(&qemu_realtime_opts);
3042 qemu_add_opts(&qemu_msg_opts);
3043 qemu_add_opts(&qemu_name_opts);
3044 qemu_add_opts(&qemu_numa_opts);
3045 qemu_add_opts(&qemu_icount_opts);
3046 qemu_add_opts(&qemu_semihosting_config_opts);
3047 qemu_add_opts(&qemu_fw_cfg_opts);
3048 #ifdef CONFIG_LIBISCSI
3049 qemu_add_opts(&qemu_iscsi_opts);
3050 #endif
3051 module_call_init(MODULE_INIT_OPTS);
3053 runstate_init();
3055 if (qcrypto_init(&err) < 0) {
3056 error_reportf_err(err, "cannot initialize crypto: ");
3057 exit(1);
3059 rtc_clock = QEMU_CLOCK_HOST;
3061 QLIST_INIT (&vm_change_state_head);
3062 os_setup_early_signal_handling();
3064 cpu_model = NULL;
3065 snapshot = 0;
3066 cyls = heads = secs = 0;
3067 translation = BIOS_ATA_TRANSLATION_AUTO;
3069 nb_nics = 0;
3071 bdrv_init_with_whitelist();
3073 autostart = 1;
3075 /* first pass of option parsing */
3076 optind = 1;
3077 while (optind < argc) {
3078 if (argv[optind][0] != '-') {
3079 /* disk image */
3080 optind++;
3081 } else {
3082 const QEMUOption *popt;
3084 popt = lookup_opt(argc, argv, &optarg, &optind);
3085 switch (popt->index) {
3086 case QEMU_OPTION_nodefconfig:
3087 defconfig = false;
3088 break;
3089 case QEMU_OPTION_nouserconfig:
3090 userconfig = false;
3091 break;
3096 if (defconfig && userconfig) {
3097 if (qemu_read_default_config_file() < 0) {
3098 exit(1);
3102 /* second pass of option parsing */
3103 optind = 1;
3104 for(;;) {
3105 if (optind >= argc)
3106 break;
3107 if (argv[optind][0] != '-') {
3108 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
3109 } else {
3110 const QEMUOption *popt;
3112 popt = lookup_opt(argc, argv, &optarg, &optind);
3113 if (!(popt->arch_mask & arch_type)) {
3114 error_report("Option not supported for this target");
3115 exit(1);
3117 switch(popt->index) {
3118 case QEMU_OPTION_no_kvm_irqchip: {
3119 olist = qemu_find_opts("machine");
3120 qemu_opts_parse_noisily(olist, "kernel_irqchip=off", false);
3121 break;
3123 case QEMU_OPTION_cpu:
3124 /* hw initialization will check this */
3125 cpu_model = optarg;
3126 break;
3127 case QEMU_OPTION_hda:
3129 char buf[256];
3130 if (cyls == 0)
3131 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
3132 else
3133 snprintf(buf, sizeof(buf),
3134 "%s,cyls=%d,heads=%d,secs=%d%s",
3135 HD_OPTS , cyls, heads, secs,
3136 translation == BIOS_ATA_TRANSLATION_LBA ?
3137 ",trans=lba" :
3138 translation == BIOS_ATA_TRANSLATION_NONE ?
3139 ",trans=none" : "");
3140 drive_add(IF_DEFAULT, 0, optarg, buf);
3141 break;
3143 case QEMU_OPTION_hdb:
3144 case QEMU_OPTION_hdc:
3145 case QEMU_OPTION_hdd:
3146 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
3147 HD_OPTS);
3148 break;
3149 case QEMU_OPTION_drive:
3150 if (drive_def(optarg) == NULL) {
3151 exit(1);
3153 break;
3154 case QEMU_OPTION_set:
3155 if (qemu_set_option(optarg) != 0)
3156 exit(1);
3157 break;
3158 case QEMU_OPTION_global:
3159 if (qemu_global_option(optarg) != 0)
3160 exit(1);
3161 break;
3162 case QEMU_OPTION_mtdblock:
3163 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3164 break;
3165 case QEMU_OPTION_sd:
3166 drive_add(IF_SD, -1, optarg, SD_OPTS);
3167 break;
3168 case QEMU_OPTION_pflash:
3169 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
3170 break;
3171 case QEMU_OPTION_snapshot:
3172 snapshot = 1;
3173 break;
3174 case QEMU_OPTION_hdachs:
3176 const char *p;
3177 p = optarg;
3178 cyls = strtol(p, (char **)&p, 0);
3179 if (cyls < 1 || cyls > 16383)
3180 goto chs_fail;
3181 if (*p != ',')
3182 goto chs_fail;
3183 p++;
3184 heads = strtol(p, (char **)&p, 0);
3185 if (heads < 1 || heads > 16)
3186 goto chs_fail;
3187 if (*p != ',')
3188 goto chs_fail;
3189 p++;
3190 secs = strtol(p, (char **)&p, 0);
3191 if (secs < 1 || secs > 63)
3192 goto chs_fail;
3193 if (*p == ',') {
3194 p++;
3195 if (!strcmp(p, "large")) {
3196 translation = BIOS_ATA_TRANSLATION_LARGE;
3197 } else if (!strcmp(p, "rechs")) {
3198 translation = BIOS_ATA_TRANSLATION_RECHS;
3199 } else if (!strcmp(p, "none")) {
3200 translation = BIOS_ATA_TRANSLATION_NONE;
3201 } else if (!strcmp(p, "lba")) {
3202 translation = BIOS_ATA_TRANSLATION_LBA;
3203 } else if (!strcmp(p, "auto")) {
3204 translation = BIOS_ATA_TRANSLATION_AUTO;
3205 } else {
3206 goto chs_fail;
3208 } else if (*p != '\0') {
3209 chs_fail:
3210 error_report("invalid physical CHS format");
3211 exit(1);
3213 if (hda_opts != NULL) {
3214 qemu_opt_set_number(hda_opts, "cyls", cyls,
3215 &error_abort);
3216 qemu_opt_set_number(hda_opts, "heads", heads,
3217 &error_abort);
3218 qemu_opt_set_number(hda_opts, "secs", secs,
3219 &error_abort);
3220 if (translation == BIOS_ATA_TRANSLATION_LARGE) {
3221 qemu_opt_set(hda_opts, "trans", "large",
3222 &error_abort);
3223 } else if (translation == BIOS_ATA_TRANSLATION_RECHS) {
3224 qemu_opt_set(hda_opts, "trans", "rechs",
3225 &error_abort);
3226 } else if (translation == BIOS_ATA_TRANSLATION_LBA) {
3227 qemu_opt_set(hda_opts, "trans", "lba",
3228 &error_abort);
3229 } else if (translation == BIOS_ATA_TRANSLATION_NONE) {
3230 qemu_opt_set(hda_opts, "trans", "none",
3231 &error_abort);
3235 break;
3236 case QEMU_OPTION_numa:
3237 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
3238 optarg, true);
3239 if (!opts) {
3240 exit(1);
3242 break;
3243 case QEMU_OPTION_display:
3244 display_type = select_display(optarg);
3245 break;
3246 case QEMU_OPTION_nographic:
3247 olist = qemu_find_opts("machine");
3248 qemu_opts_parse_noisily(olist, "graphics=off", false);
3249 nographic = true;
3250 display_type = DT_NONE;
3251 break;
3252 case QEMU_OPTION_curses:
3253 #ifdef CONFIG_CURSES
3254 display_type = DT_CURSES;
3255 #else
3256 error_report("curses support is disabled");
3257 exit(1);
3258 #endif
3259 break;
3260 case QEMU_OPTION_portrait:
3261 graphic_rotate = 90;
3262 break;
3263 case QEMU_OPTION_rotate:
3264 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3265 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3266 graphic_rotate != 180 && graphic_rotate != 270) {
3267 error_report("only 90, 180, 270 deg rotation is available");
3268 exit(1);
3270 break;
3271 case QEMU_OPTION_kernel:
3272 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg,
3273 &error_abort);
3274 break;
3275 case QEMU_OPTION_initrd:
3276 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg,
3277 &error_abort);
3278 break;
3279 case QEMU_OPTION_append:
3280 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg,
3281 &error_abort);
3282 break;
3283 case QEMU_OPTION_dtb:
3284 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg,
3285 &error_abort);
3286 break;
3287 case QEMU_OPTION_cdrom:
3288 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3289 break;
3290 case QEMU_OPTION_boot:
3291 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
3292 optarg, true);
3293 if (!opts) {
3294 exit(1);
3296 break;
3297 case QEMU_OPTION_fda:
3298 case QEMU_OPTION_fdb:
3299 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3300 optarg, FD_OPTS);
3301 break;
3302 case QEMU_OPTION_no_fd_bootchk:
3303 fd_bootchk = 0;
3304 break;
3305 case QEMU_OPTION_netdev:
3306 default_net = 0;
3307 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3308 exit(1);
3310 break;
3311 case QEMU_OPTION_net:
3312 default_net = 0;
3313 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3314 exit(1);
3316 break;
3317 #ifdef CONFIG_LIBISCSI
3318 case QEMU_OPTION_iscsi:
3319 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
3320 optarg, false);
3321 if (!opts) {
3322 exit(1);
3324 break;
3325 #endif
3326 #ifdef CONFIG_SLIRP
3327 case QEMU_OPTION_tftp:
3328 error_report("The -tftp option is deprecated. "
3329 "Please use '-netdev user,tftp=...' instead.");
3330 legacy_tftp_prefix = optarg;
3331 break;
3332 case QEMU_OPTION_bootp:
3333 error_report("The -bootp option is deprecated. "
3334 "Please use '-netdev user,bootfile=...' instead.");
3335 legacy_bootp_filename = optarg;
3336 break;
3337 case QEMU_OPTION_redir:
3338 error_report("The -redir option is deprecated. "
3339 "Please use '-netdev user,hostfwd=...' instead.");
3340 if (net_slirp_redir(optarg) < 0)
3341 exit(1);
3342 break;
3343 #endif
3344 case QEMU_OPTION_bt:
3345 add_device_config(DEV_BT, optarg);
3346 break;
3347 case QEMU_OPTION_audio_help:
3348 AUD_help ();
3349 exit (0);
3350 break;
3351 case QEMU_OPTION_soundhw:
3352 select_soundhw (optarg);
3353 break;
3354 case QEMU_OPTION_h:
3355 help(0);
3356 break;
3357 case QEMU_OPTION_version:
3358 version();
3359 exit(0);
3360 break;
3361 case QEMU_OPTION_m:
3362 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
3363 optarg, true);
3364 if (!opts) {
3365 exit(EXIT_FAILURE);
3367 break;
3368 #ifdef CONFIG_TPM
3369 case QEMU_OPTION_tpmdev:
3370 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3371 exit(1);
3373 break;
3374 #endif
3375 case QEMU_OPTION_mempath:
3376 mem_path = optarg;
3377 break;
3378 case QEMU_OPTION_mem_prealloc:
3379 mem_prealloc = 1;
3380 break;
3381 case QEMU_OPTION_d:
3382 log_mask = optarg;
3383 break;
3384 case QEMU_OPTION_D:
3385 log_file = optarg;
3386 break;
3387 case QEMU_OPTION_DFILTER:
3388 qemu_set_dfilter_ranges(optarg, &error_fatal);
3389 break;
3390 case QEMU_OPTION_s:
3391 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3392 break;
3393 case QEMU_OPTION_gdb:
3394 add_device_config(DEV_GDB, optarg);
3395 break;
3396 case QEMU_OPTION_L:
3397 if (is_help_option(optarg)) {
3398 list_data_dirs = true;
3399 } else if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3400 data_dir[data_dir_idx++] = optarg;
3402 break;
3403 case QEMU_OPTION_bios:
3404 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg,
3405 &error_abort);
3406 break;
3407 case QEMU_OPTION_singlestep:
3408 singlestep = 1;
3409 break;
3410 case QEMU_OPTION_S:
3411 autostart = 0;
3412 break;
3413 case QEMU_OPTION_k:
3414 keyboard_layout = optarg;
3415 break;
3416 case QEMU_OPTION_localtime:
3417 rtc_utc = 0;
3418 break;
3419 case QEMU_OPTION_vga:
3420 vga_model = optarg;
3421 default_vga = 0;
3422 break;
3423 case QEMU_OPTION_g:
3425 const char *p;
3426 int w, h, depth;
3427 p = optarg;
3428 w = strtol(p, (char **)&p, 10);
3429 if (w <= 0) {
3430 graphic_error:
3431 error_report("invalid resolution or depth");
3432 exit(1);
3434 if (*p != 'x')
3435 goto graphic_error;
3436 p++;
3437 h = strtol(p, (char **)&p, 10);
3438 if (h <= 0)
3439 goto graphic_error;
3440 if (*p == 'x') {
3441 p++;
3442 depth = strtol(p, (char **)&p, 10);
3443 if (depth != 8 && depth != 15 && depth != 16 &&
3444 depth != 24 && depth != 32)
3445 goto graphic_error;
3446 } else if (*p == '\0') {
3447 depth = graphic_depth;
3448 } else {
3449 goto graphic_error;
3452 graphic_width = w;
3453 graphic_height = h;
3454 graphic_depth = depth;
3456 break;
3457 case QEMU_OPTION_echr:
3459 char *r;
3460 term_escape_char = strtol(optarg, &r, 0);
3461 if (r == optarg)
3462 printf("Bad argument to echr\n");
3463 break;
3465 case QEMU_OPTION_monitor:
3466 default_monitor = 0;
3467 if (strncmp(optarg, "none", 4)) {
3468 monitor_parse(optarg, "readline", false);
3470 break;
3471 case QEMU_OPTION_qmp:
3472 monitor_parse(optarg, "control", false);
3473 default_monitor = 0;
3474 break;
3475 case QEMU_OPTION_qmp_pretty:
3476 monitor_parse(optarg, "control", true);
3477 default_monitor = 0;
3478 break;
3479 case QEMU_OPTION_mon:
3480 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3481 true);
3482 if (!opts) {
3483 exit(1);
3485 default_monitor = 0;
3486 break;
3487 case QEMU_OPTION_chardev:
3488 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3489 optarg, true);
3490 if (!opts) {
3491 exit(1);
3493 break;
3494 case QEMU_OPTION_fsdev:
3495 olist = qemu_find_opts("fsdev");
3496 if (!olist) {
3497 error_report("fsdev support is disabled");
3498 exit(1);
3500 opts = qemu_opts_parse_noisily(olist, optarg, true);
3501 if (!opts) {
3502 exit(1);
3504 break;
3505 case QEMU_OPTION_virtfs: {
3506 QemuOpts *fsdev;
3507 QemuOpts *device;
3508 const char *writeout, *sock_fd, *socket;
3510 olist = qemu_find_opts("virtfs");
3511 if (!olist) {
3512 error_report("virtfs support is disabled");
3513 exit(1);
3515 opts = qemu_opts_parse_noisily(olist, optarg, true);
3516 if (!opts) {
3517 exit(1);
3520 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3521 qemu_opt_get(opts, "mount_tag") == NULL) {
3522 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3523 exit(1);
3525 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3526 qemu_opt_get(opts, "mount_tag"),
3527 1, NULL);
3528 if (!fsdev) {
3529 error_report("duplicate fsdev id: %s",
3530 qemu_opt_get(opts, "mount_tag"));
3531 exit(1);
3534 writeout = qemu_opt_get(opts, "writeout");
3535 if (writeout) {
3536 #ifdef CONFIG_SYNC_FILE_RANGE
3537 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3538 #else
3539 error_report("writeout=immediate not supported "
3540 "on this platform");
3541 exit(1);
3542 #endif
3544 qemu_opt_set(fsdev, "fsdriver",
3545 qemu_opt_get(opts, "fsdriver"), &error_abort);
3546 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"),
3547 &error_abort);
3548 qemu_opt_set(fsdev, "security_model",
3549 qemu_opt_get(opts, "security_model"),
3550 &error_abort);
3551 socket = qemu_opt_get(opts, "socket");
3552 if (socket) {
3553 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3555 sock_fd = qemu_opt_get(opts, "sock_fd");
3556 if (sock_fd) {
3557 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3560 qemu_opt_set_bool(fsdev, "readonly",
3561 qemu_opt_get_bool(opts, "readonly", 0),
3562 &error_abort);
3563 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3564 &error_abort);
3565 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3566 qemu_opt_set(device, "fsdev",
3567 qemu_opt_get(opts, "mount_tag"), &error_abort);
3568 qemu_opt_set(device, "mount_tag",
3569 qemu_opt_get(opts, "mount_tag"), &error_abort);
3570 break;
3572 case QEMU_OPTION_virtfs_synth: {
3573 QemuOpts *fsdev;
3574 QemuOpts *device;
3576 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3577 1, NULL);
3578 if (!fsdev) {
3579 error_report("duplicate option: %s", "virtfs_synth");
3580 exit(1);
3582 qemu_opt_set(fsdev, "fsdriver", "synth", &error_abort);
3584 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3585 &error_abort);
3586 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3587 qemu_opt_set(device, "fsdev", "v_synth", &error_abort);
3588 qemu_opt_set(device, "mount_tag", "v_synth", &error_abort);
3589 break;
3591 case QEMU_OPTION_serial:
3592 add_device_config(DEV_SERIAL, optarg);
3593 default_serial = 0;
3594 if (strncmp(optarg, "mon:", 4) == 0) {
3595 default_monitor = 0;
3597 break;
3598 case QEMU_OPTION_watchdog:
3599 if (watchdog) {
3600 error_report("only one watchdog option may be given");
3601 return 1;
3603 watchdog = optarg;
3604 break;
3605 case QEMU_OPTION_watchdog_action:
3606 if (select_watchdog_action(optarg) == -1) {
3607 error_report("unknown -watchdog-action parameter");
3608 exit(1);
3610 break;
3611 case QEMU_OPTION_virtiocon:
3612 add_device_config(DEV_VIRTCON, optarg);
3613 default_virtcon = 0;
3614 if (strncmp(optarg, "mon:", 4) == 0) {
3615 default_monitor = 0;
3617 break;
3618 case QEMU_OPTION_parallel:
3619 add_device_config(DEV_PARALLEL, optarg);
3620 default_parallel = 0;
3621 if (strncmp(optarg, "mon:", 4) == 0) {
3622 default_monitor = 0;
3624 break;
3625 case QEMU_OPTION_debugcon:
3626 add_device_config(DEV_DEBUGCON, optarg);
3627 break;
3628 case QEMU_OPTION_loadvm:
3629 loadvm = optarg;
3630 break;
3631 case QEMU_OPTION_full_screen:
3632 full_screen = 1;
3633 break;
3634 case QEMU_OPTION_no_frame:
3635 no_frame = 1;
3636 break;
3637 case QEMU_OPTION_alt_grab:
3638 alt_grab = 1;
3639 break;
3640 case QEMU_OPTION_ctrl_grab:
3641 ctrl_grab = 1;
3642 break;
3643 case QEMU_OPTION_no_quit:
3644 no_quit = 1;
3645 break;
3646 case QEMU_OPTION_sdl:
3647 #ifdef CONFIG_SDL
3648 display_type = DT_SDL;
3649 break;
3650 #else
3651 error_report("SDL support is disabled");
3652 exit(1);
3653 #endif
3654 case QEMU_OPTION_pidfile:
3655 pid_file = optarg;
3656 break;
3657 case QEMU_OPTION_win2k_hack:
3658 win2k_install_hack = 1;
3659 break;
3660 case QEMU_OPTION_rtc_td_hack: {
3661 static GlobalProperty slew_lost_ticks = {
3662 .driver = "mc146818rtc",
3663 .property = "lost_tick_policy",
3664 .value = "slew",
3667 qdev_prop_register_global(&slew_lost_ticks);
3668 break;
3670 case QEMU_OPTION_acpitable:
3671 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3672 optarg, true);
3673 if (!opts) {
3674 exit(1);
3676 acpi_table_add(opts, &error_fatal);
3677 break;
3678 case QEMU_OPTION_smbios:
3679 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3680 optarg, false);
3681 if (!opts) {
3682 exit(1);
3684 smbios_entry_add(opts, &error_fatal);
3685 break;
3686 case QEMU_OPTION_fwcfg:
3687 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3688 optarg, true);
3689 if (opts == NULL) {
3690 exit(1);
3692 break;
3693 case QEMU_OPTION_enable_kvm:
3694 olist = qemu_find_opts("machine");
3695 qemu_opts_parse_noisily(olist, "accel=kvm", false);
3696 break;
3697 case QEMU_OPTION_enable_hax:
3698 olist = qemu_find_opts("machine");
3699 qemu_opts_parse_noisily(olist, "accel=hax", false);
3700 break;
3701 case QEMU_OPTION_M:
3702 case QEMU_OPTION_machine:
3703 olist = qemu_find_opts("machine");
3704 opts = qemu_opts_parse_noisily(olist, optarg, true);
3705 if (!opts) {
3706 exit(1);
3708 break;
3709 case QEMU_OPTION_no_kvm:
3710 olist = qemu_find_opts("machine");
3711 qemu_opts_parse_noisily(olist, "accel=tcg", false);
3712 break;
3713 case QEMU_OPTION_no_kvm_pit: {
3714 error_report("warning: ignoring deprecated option");
3715 break;
3717 case QEMU_OPTION_no_kvm_pit_reinjection: {
3718 static GlobalProperty kvm_pit_lost_tick_policy = {
3719 .driver = "kvm-pit",
3720 .property = "lost_tick_policy",
3721 .value = "discard",
3724 error_report("warning: deprecated, replaced by "
3725 "-global kvm-pit.lost_tick_policy=discard");
3726 qdev_prop_register_global(&kvm_pit_lost_tick_policy);
3727 break;
3729 case QEMU_OPTION_usb:
3730 olist = qemu_find_opts("machine");
3731 qemu_opts_parse_noisily(olist, "usb=on", false);
3732 break;
3733 case QEMU_OPTION_usbdevice:
3734 olist = qemu_find_opts("machine");
3735 qemu_opts_parse_noisily(olist, "usb=on", false);
3736 add_device_config(DEV_USB, optarg);
3737 break;
3738 case QEMU_OPTION_device:
3739 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3740 optarg, true)) {
3741 exit(1);
3743 break;
3744 case QEMU_OPTION_smp:
3745 if (!qemu_opts_parse_noisily(qemu_find_opts("smp-opts"),
3746 optarg, true)) {
3747 exit(1);
3749 break;
3750 case QEMU_OPTION_vnc:
3751 vnc_parse(optarg, &error_fatal);
3752 break;
3753 case QEMU_OPTION_no_acpi:
3754 acpi_enabled = 0;
3755 break;
3756 case QEMU_OPTION_no_hpet:
3757 no_hpet = 1;
3758 break;
3759 case QEMU_OPTION_balloon:
3760 if (balloon_parse(optarg) < 0) {
3761 error_report("unknown -balloon argument %s", optarg);
3762 exit(1);
3764 break;
3765 case QEMU_OPTION_no_reboot:
3766 no_reboot = 1;
3767 break;
3768 case QEMU_OPTION_no_shutdown:
3769 no_shutdown = 1;
3770 break;
3771 case QEMU_OPTION_show_cursor:
3772 cursor_hide = 0;
3773 break;
3774 case QEMU_OPTION_uuid:
3775 if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
3776 error_report("failed to parse UUID string: wrong format");
3777 exit(1);
3779 qemu_uuid_set = true;
3780 break;
3781 case QEMU_OPTION_option_rom:
3782 if (nb_option_roms >= MAX_OPTION_ROMS) {
3783 error_report("too many option ROMs");
3784 exit(1);
3786 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3787 optarg, true);
3788 if (!opts) {
3789 exit(1);
3791 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3792 option_rom[nb_option_roms].bootindex =
3793 qemu_opt_get_number(opts, "bootindex", -1);
3794 if (!option_rom[nb_option_roms].name) {
3795 error_report("Option ROM file is not specified");
3796 exit(1);
3798 nb_option_roms++;
3799 break;
3800 case QEMU_OPTION_semihosting:
3801 semihosting.enabled = true;
3802 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3803 break;
3804 case QEMU_OPTION_semihosting_config:
3805 semihosting.enabled = true;
3806 opts = qemu_opts_parse_noisily(qemu_find_opts("semihosting-config"),
3807 optarg, false);
3808 if (opts != NULL) {
3809 semihosting.enabled = qemu_opt_get_bool(opts, "enable",
3810 true);
3811 const char *target = qemu_opt_get(opts, "target");
3812 if (target != NULL) {
3813 if (strcmp("native", target) == 0) {
3814 semihosting.target = SEMIHOSTING_TARGET_NATIVE;
3815 } else if (strcmp("gdb", target) == 0) {
3816 semihosting.target = SEMIHOSTING_TARGET_GDB;
3817 } else if (strcmp("auto", target) == 0) {
3818 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3819 } else {
3820 error_report("unsupported semihosting-config %s",
3821 optarg);
3822 exit(1);
3824 } else {
3825 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3827 /* Set semihosting argument count and vector */
3828 qemu_opt_foreach(opts, add_semihosting_arg,
3829 &semihosting, NULL);
3830 } else {
3831 error_report("unsupported semihosting-config %s", optarg);
3832 exit(1);
3834 break;
3835 case QEMU_OPTION_tdf:
3836 error_report("warning: ignoring deprecated option");
3837 break;
3838 case QEMU_OPTION_name:
3839 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3840 optarg, true);
3841 if (!opts) {
3842 exit(1);
3844 break;
3845 case QEMU_OPTION_prom_env:
3846 if (nb_prom_envs >= MAX_PROM_ENVS) {
3847 error_report("too many prom variables");
3848 exit(1);
3850 prom_envs[nb_prom_envs] = optarg;
3851 nb_prom_envs++;
3852 break;
3853 case QEMU_OPTION_old_param:
3854 old_param = 1;
3855 break;
3856 case QEMU_OPTION_clock:
3857 /* Clock options no longer exist. Keep this option for
3858 * backward compatibility.
3860 break;
3861 case QEMU_OPTION_startdate:
3862 configure_rtc_date_offset(optarg, 1);
3863 break;
3864 case QEMU_OPTION_rtc:
3865 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3866 false);
3867 if (!opts) {
3868 exit(1);
3870 configure_rtc(opts);
3871 break;
3872 case QEMU_OPTION_tb_size:
3873 tcg_tb_size = strtol(optarg, NULL, 0);
3874 if (tcg_tb_size < 0) {
3875 tcg_tb_size = 0;
3877 break;
3878 case QEMU_OPTION_icount:
3879 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3880 optarg, true);
3881 if (!icount_opts) {
3882 exit(1);
3884 break;
3885 case QEMU_OPTION_incoming:
3886 if (!incoming) {
3887 runstate_set(RUN_STATE_INMIGRATE);
3889 incoming = optarg;
3890 break;
3891 case QEMU_OPTION_only_migratable:
3892 only_migratable = 1;
3893 break;
3894 case QEMU_OPTION_nodefaults:
3895 has_defaults = 0;
3896 break;
3897 case QEMU_OPTION_xen_domid:
3898 if (!(xen_available())) {
3899 error_report("Option not supported for this target");
3900 exit(1);
3902 xen_domid = atoi(optarg);
3903 break;
3904 case QEMU_OPTION_xen_create:
3905 if (!(xen_available())) {
3906 error_report("Option not supported for this target");
3907 exit(1);
3909 xen_mode = XEN_CREATE;
3910 break;
3911 case QEMU_OPTION_xen_attach:
3912 if (!(xen_available())) {
3913 error_report("Option not supported for this target");
3914 exit(1);
3916 xen_mode = XEN_ATTACH;
3917 break;
3918 case QEMU_OPTION_trace:
3919 g_free(trace_file);
3920 trace_file = trace_opt_parse(optarg);
3921 break;
3922 case QEMU_OPTION_readconfig:
3924 int ret = qemu_read_config_file(optarg);
3925 if (ret < 0) {
3926 error_report("read config %s: %s", optarg,
3927 strerror(-ret));
3928 exit(1);
3930 break;
3932 case QEMU_OPTION_spice:
3933 olist = qemu_find_opts("spice");
3934 if (!olist) {
3935 error_report("spice support is disabled");
3936 exit(1);
3938 opts = qemu_opts_parse_noisily(olist, optarg, false);
3939 if (!opts) {
3940 exit(1);
3942 display_remote++;
3943 break;
3944 case QEMU_OPTION_writeconfig:
3946 FILE *fp;
3947 if (strcmp(optarg, "-") == 0) {
3948 fp = stdout;
3949 } else {
3950 fp = fopen(optarg, "w");
3951 if (fp == NULL) {
3952 error_report("open %s: %s", optarg,
3953 strerror(errno));
3954 exit(1);
3957 qemu_config_write(fp);
3958 if (fp != stdout) {
3959 fclose(fp);
3961 break;
3963 case QEMU_OPTION_qtest:
3964 qtest_chrdev = optarg;
3965 break;
3966 case QEMU_OPTION_qtest_log:
3967 qtest_log = optarg;
3968 break;
3969 case QEMU_OPTION_sandbox:
3970 opts = qemu_opts_parse_noisily(qemu_find_opts("sandbox"),
3971 optarg, true);
3972 if (!opts) {
3973 exit(1);
3975 break;
3976 case QEMU_OPTION_add_fd:
3977 #ifndef _WIN32
3978 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
3979 optarg, false);
3980 if (!opts) {
3981 exit(1);
3983 #else
3984 error_report("File descriptor passing is disabled on this "
3985 "platform");
3986 exit(1);
3987 #endif
3988 break;
3989 case QEMU_OPTION_object:
3990 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
3991 optarg, true);
3992 if (!opts) {
3993 exit(1);
3995 break;
3996 case QEMU_OPTION_realtime:
3997 opts = qemu_opts_parse_noisily(qemu_find_opts("realtime"),
3998 optarg, false);
3999 if (!opts) {
4000 exit(1);
4002 enable_mlock = qemu_opt_get_bool(opts, "mlock", true);
4003 break;
4004 case QEMU_OPTION_msg:
4005 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
4006 false);
4007 if (!opts) {
4008 exit(1);
4010 configure_msg(opts);
4011 break;
4012 case QEMU_OPTION_dump_vmstate:
4013 if (vmstate_dump_file) {
4014 error_report("only one '-dump-vmstate' "
4015 "option may be given");
4016 exit(1);
4018 vmstate_dump_file = fopen(optarg, "w");
4019 if (vmstate_dump_file == NULL) {
4020 error_report("open %s: %s", optarg, strerror(errno));
4021 exit(1);
4023 break;
4024 default:
4025 os_parse_cmd_args(popt->index, optarg);
4030 * Clear error location left behind by the loop.
4031 * Best done right after the loop. Do not insert code here!
4033 loc_set_none();
4035 replay_configure(icount_opts);
4037 machine_class = select_machine();
4039 set_memory_options(&ram_slots, &maxram_size, machine_class);
4041 os_daemonize();
4043 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
4044 error_report("could not acquire pid file: %s", strerror(errno));
4045 exit(1);
4048 if (qemu_init_main_loop(&main_loop_err)) {
4049 error_report_err(main_loop_err);
4050 exit(1);
4053 if (qemu_opts_foreach(qemu_find_opts("sandbox"),
4054 parse_sandbox, NULL, NULL)) {
4055 exit(1);
4058 if (qemu_opts_foreach(qemu_find_opts("name"),
4059 parse_name, NULL, NULL)) {
4060 exit(1);
4063 #ifndef _WIN32
4064 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4065 parse_add_fd, NULL, NULL)) {
4066 exit(1);
4069 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4070 cleanup_add_fd, NULL, NULL)) {
4071 exit(1);
4073 #endif
4075 current_machine = MACHINE(object_new(object_class_get_name(
4076 OBJECT_CLASS(machine_class))));
4077 if (machine_help_func(qemu_get_machine_opts(), current_machine)) {
4078 exit(0);
4080 object_property_add_child(object_get_root(), "machine",
4081 OBJECT(current_machine), &error_abort);
4083 if (machine_class->minimum_page_bits) {
4084 if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
4085 /* This would be a board error: specifying a minimum smaller than
4086 * a target's compile-time fixed setting.
4088 g_assert_not_reached();
4092 cpu_exec_init_all();
4094 if (machine_class->hw_version) {
4095 qemu_set_hw_version(machine_class->hw_version);
4098 if (cpu_model && is_help_option(cpu_model)) {
4099 list_cpus(stdout, &fprintf, cpu_model);
4100 exit(0);
4103 if (!trace_init_backends()) {
4104 exit(1);
4106 trace_init_file(trace_file);
4108 /* Open the logfile at this point and set the log mask if necessary.
4110 if (log_file) {
4111 qemu_set_log_filename(log_file, &error_fatal);
4114 if (log_mask) {
4115 int mask;
4116 mask = qemu_str_to_log_mask(log_mask);
4117 if (!mask) {
4118 qemu_print_log_usage(stdout);
4119 exit(1);
4121 qemu_set_log(mask);
4122 } else {
4123 qemu_set_log(0);
4126 /* If no data_dir is specified then try to find it relative to the
4127 executable path. */
4128 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4129 data_dir[data_dir_idx] = os_find_datadir();
4130 if (data_dir[data_dir_idx] != NULL) {
4131 data_dir_idx++;
4134 /* If all else fails use the install path specified when building. */
4135 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4136 data_dir[data_dir_idx++] = CONFIG_QEMU_DATADIR;
4139 /* -L help lists the data directories and exits. */
4140 if (list_data_dirs) {
4141 for (i = 0; i < data_dir_idx; i++) {
4142 printf("%s\n", data_dir[i]);
4144 exit(0);
4147 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
4149 machine_class->max_cpus = machine_class->max_cpus ?: 1; /* Default to UP */
4150 if (max_cpus > machine_class->max_cpus) {
4151 error_report("Number of SMP CPUs requested (%d) exceeds max CPUs "
4152 "supported by machine '%s' (%d)", max_cpus,
4153 machine_class->name, machine_class->max_cpus);
4154 exit(1);
4158 * Get the default machine options from the machine if it is not already
4159 * specified either by the configuration file or by the command line.
4161 if (machine_class->default_machine_opts) {
4162 qemu_opts_set_defaults(qemu_find_opts("machine"),
4163 machine_class->default_machine_opts, 0);
4166 qemu_opts_foreach(qemu_find_opts("device"),
4167 default_driver_check, NULL, NULL);
4168 qemu_opts_foreach(qemu_find_opts("global"),
4169 default_driver_check, NULL, NULL);
4171 if (!vga_model && !default_vga) {
4172 vga_interface_type = VGA_DEVICE;
4174 if (!has_defaults || machine_class->no_serial) {
4175 default_serial = 0;
4177 if (!has_defaults || machine_class->no_parallel) {
4178 default_parallel = 0;
4180 if (!has_defaults || !machine_class->use_virtcon) {
4181 default_virtcon = 0;
4183 if (!has_defaults || !machine_class->use_sclp) {
4184 default_sclp = 0;
4186 if (!has_defaults || machine_class->no_floppy) {
4187 default_floppy = 0;
4189 if (!has_defaults || machine_class->no_cdrom) {
4190 default_cdrom = 0;
4192 if (!has_defaults || machine_class->no_sdcard) {
4193 default_sdcard = 0;
4195 if (!has_defaults) {
4196 default_monitor = 0;
4197 default_net = 0;
4198 default_vga = 0;
4201 if (is_daemonized()) {
4202 /* According to documentation and historically, -nographic redirects
4203 * serial port, parallel port and monitor to stdio, which does not work
4204 * with -daemonize. We can redirect these to null instead, but since
4205 * -nographic is legacy, let's just error out.
4206 * We disallow -nographic only if all other ports are not redirected
4207 * explicitly, to not break existing legacy setups which uses
4208 * -nographic _and_ redirects all ports explicitly - this is valid
4209 * usage, -nographic is just a no-op in this case.
4211 if (nographic
4212 && (default_parallel || default_serial
4213 || default_monitor || default_virtcon)) {
4214 error_report("-nographic cannot be used with -daemonize");
4215 exit(1);
4217 #ifdef CONFIG_CURSES
4218 if (display_type == DT_CURSES) {
4219 error_report("curses display cannot be used with -daemonize");
4220 exit(1);
4222 #endif
4225 if (nographic) {
4226 if (default_parallel)
4227 add_device_config(DEV_PARALLEL, "null");
4228 if (default_serial && default_monitor) {
4229 add_device_config(DEV_SERIAL, "mon:stdio");
4230 } else if (default_virtcon && default_monitor) {
4231 add_device_config(DEV_VIRTCON, "mon:stdio");
4232 } else if (default_sclp && default_monitor) {
4233 add_device_config(DEV_SCLP, "mon:stdio");
4234 } else {
4235 if (default_serial)
4236 add_device_config(DEV_SERIAL, "stdio");
4237 if (default_virtcon)
4238 add_device_config(DEV_VIRTCON, "stdio");
4239 if (default_sclp) {
4240 add_device_config(DEV_SCLP, "stdio");
4242 if (default_monitor)
4243 monitor_parse("stdio", "readline", false);
4245 } else {
4246 if (default_serial)
4247 add_device_config(DEV_SERIAL, "vc:80Cx24C");
4248 if (default_parallel)
4249 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
4250 if (default_monitor)
4251 monitor_parse("vc:80Cx24C", "readline", false);
4252 if (default_virtcon)
4253 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
4254 if (default_sclp) {
4255 add_device_config(DEV_SCLP, "vc:80Cx24C");
4259 #if defined(CONFIG_VNC)
4260 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
4261 display_remote++;
4263 #endif
4264 if (display_type == DT_DEFAULT && !display_remote) {
4265 #if defined(CONFIG_GTK)
4266 display_type = DT_GTK;
4267 #elif defined(CONFIG_SDL)
4268 display_type = DT_SDL;
4269 #elif defined(CONFIG_COCOA)
4270 display_type = DT_COCOA;
4271 #elif defined(CONFIG_VNC)
4272 vnc_parse("localhost:0,to=99,id=default", &error_abort);
4273 #else
4274 display_type = DT_NONE;
4275 #endif
4278 if ((no_frame || alt_grab || ctrl_grab) && display_type != DT_SDL) {
4279 error_report("-no-frame, -alt-grab and -ctrl-grab are only valid "
4280 "for SDL, ignoring option");
4282 if (no_quit && (display_type != DT_GTK && display_type != DT_SDL)) {
4283 error_report("-no-quit is only valid for GTK and SDL, "
4284 "ignoring option");
4287 if (display_type == DT_GTK) {
4288 early_gtk_display_init(request_opengl);
4291 if (display_type == DT_SDL) {
4292 sdl_display_early_init(request_opengl);
4295 if (request_opengl == 1 && display_opengl == 0) {
4296 #if defined(CONFIG_OPENGL)
4297 error_report("OpenGL is not supported by the display");
4298 #else
4299 error_report("OpenGL support is disabled");
4300 #endif
4301 exit(1);
4304 page_size_init();
4305 socket_init();
4307 if (qemu_opts_foreach(qemu_find_opts("object"),
4308 user_creatable_add_opts_foreach,
4309 object_create_initial, NULL)) {
4310 exit(1);
4313 if (qemu_opts_foreach(qemu_find_opts("chardev"),
4314 chardev_init_func, NULL, NULL)) {
4315 exit(1);
4318 #ifdef CONFIG_VIRTFS
4319 if (qemu_opts_foreach(qemu_find_opts("fsdev"),
4320 fsdev_init_func, NULL, NULL)) {
4321 exit(1);
4323 #endif
4325 if (qemu_opts_foreach(qemu_find_opts("device"),
4326 device_help_func, NULL, NULL)) {
4327 exit(0);
4330 machine_opts = qemu_get_machine_opts();
4331 if (qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
4332 NULL)) {
4333 object_unref(OBJECT(current_machine));
4334 exit(1);
4337 configure_accelerator(current_machine);
4339 if (qtest_chrdev) {
4340 qtest_init(qtest_chrdev, qtest_log, &error_fatal);
4343 machine_opts = qemu_get_machine_opts();
4344 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4345 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4346 kernel_cmdline = qemu_opt_get(machine_opts, "append");
4347 bios_name = qemu_opt_get(machine_opts, "firmware");
4349 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4350 if (opts) {
4351 boot_order = qemu_opt_get(opts, "order");
4352 if (boot_order) {
4353 validate_bootdevices(boot_order, &error_fatal);
4356 boot_once = qemu_opt_get(opts, "once");
4357 if (boot_once) {
4358 validate_bootdevices(boot_once, &error_fatal);
4361 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
4362 boot_strict = qemu_opt_get_bool(opts, "strict", false);
4365 if (!boot_order) {
4366 boot_order = machine_class->default_boot_order;
4369 if (!kernel_cmdline) {
4370 kernel_cmdline = "";
4371 current_machine->kernel_cmdline = (char *)kernel_cmdline;
4374 linux_boot = (kernel_filename != NULL);
4376 if (!linux_boot && *kernel_cmdline != '\0') {
4377 error_report("-append only allowed with -kernel option");
4378 exit(1);
4381 if (!linux_boot && initrd_filename != NULL) {
4382 error_report("-initrd only allowed with -kernel option");
4383 exit(1);
4386 if (semihosting_enabled() && !semihosting_get_argc() && kernel_filename) {
4387 /* fall back to the -kernel/-append */
4388 semihosting_arg_fallback(kernel_filename, kernel_cmdline);
4391 os_set_line_buffering();
4393 /* spice needs the timers to be initialized by this point */
4394 qemu_spice_init();
4396 cpu_ticks_init();
4397 if (icount_opts) {
4398 if (!tcg_enabled()) {
4399 error_report("-icount is not allowed with hardware virtualization");
4400 exit(1);
4402 configure_icount(icount_opts, &error_abort);
4403 qemu_opts_del(icount_opts);
4406 if (default_net) {
4407 QemuOptsList *net = qemu_find_opts("net");
4408 qemu_opts_set(net, NULL, "type", "nic", &error_abort);
4409 #ifdef CONFIG_SLIRP
4410 qemu_opts_set(net, NULL, "type", "user", &error_abort);
4411 #endif
4414 colo_info_init();
4416 if (net_init_clients() < 0) {
4417 exit(1);
4420 if (qemu_opts_foreach(qemu_find_opts("object"),
4421 user_creatable_add_opts_foreach,
4422 object_create_delayed, NULL)) {
4423 exit(1);
4426 #ifdef CONFIG_TPM
4427 if (tpm_init() < 0) {
4428 exit(1);
4430 #endif
4432 /* init the bluetooth world */
4433 if (foreach_device_config(DEV_BT, bt_parse))
4434 exit(1);
4436 if (!xen_enabled()) {
4437 /* On 32-bit hosts, QEMU is limited by virtual address space */
4438 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4439 error_report("at most 2047 MB RAM can be simulated");
4440 exit(1);
4444 blk_mig_init();
4445 ram_mig_init();
4447 /* If the currently selected machine wishes to override the units-per-bus
4448 * property of its default HBA interface type, do so now. */
4449 if (machine_class->units_per_default_bus) {
4450 override_max_devs(machine_class->block_default_type,
4451 machine_class->units_per_default_bus);
4454 /* open the virtual block devices */
4455 if (snapshot || replay_mode != REPLAY_MODE_NONE) {
4456 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
4457 NULL, NULL);
4459 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
4460 &machine_class->block_default_type, NULL)) {
4461 exit(1);
4464 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
4465 CDROM_OPTS);
4466 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4467 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
4469 parse_numa_opts(machine_class);
4471 if (qemu_opts_foreach(qemu_find_opts("mon"),
4472 mon_init_func, NULL, NULL)) {
4473 exit(1);
4476 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4477 exit(1);
4478 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4479 exit(1);
4480 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4481 exit(1);
4482 if (foreach_device_config(DEV_SCLP, sclp_parse) < 0) {
4483 exit(1);
4485 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4486 exit(1);
4488 /* If no default VGA is requested, the default is "none". */
4489 if (default_vga) {
4490 if (machine_class->default_display) {
4491 vga_model = machine_class->default_display;
4492 } else if (vga_interface_available(VGA_CIRRUS)) {
4493 vga_model = "cirrus";
4494 } else if (vga_interface_available(VGA_STD)) {
4495 vga_model = "std";
4498 if (vga_model) {
4499 select_vgahw(vga_model);
4502 if (watchdog) {
4503 i = select_watchdog(watchdog);
4504 if (i > 0)
4505 exit (i == 1 ? 1 : 0);
4508 machine_register_compat_props(current_machine);
4510 qemu_opts_foreach(qemu_find_opts("global"),
4511 global_init_func, NULL, NULL);
4513 /* This checkpoint is required by replay to separate prior clock
4514 reading from the other reads, because timer polling functions query
4515 clock values from the log. */
4516 replay_checkpoint(CHECKPOINT_INIT);
4517 qdev_machine_init();
4519 current_machine->ram_size = ram_size;
4520 current_machine->maxram_size = maxram_size;
4521 current_machine->ram_slots = ram_slots;
4522 current_machine->boot_order = boot_order;
4523 current_machine->cpu_model = cpu_model;
4525 machine_class->init(current_machine);
4527 realtime_init();
4529 audio_init();
4531 cpu_synchronize_all_post_init();
4533 if (hax_enabled()) {
4534 hax_sync_vcpus();
4537 if (qemu_opts_foreach(qemu_find_opts("fw_cfg"),
4538 parse_fw_cfg, fw_cfg_find(), NULL) != 0) {
4539 exit(1);
4542 /* init USB devices */
4543 if (machine_usb(current_machine)) {
4544 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4545 exit(1);
4548 /* Check if IGD GFX passthrough. */
4549 igd_gfx_passthru();
4551 /* init generic devices */
4552 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
4553 if (qemu_opts_foreach(qemu_find_opts("device"),
4554 device_init_func, NULL, NULL)) {
4555 exit(1);
4558 numa_post_machine_init();
4560 rom_reset_order_override();
4562 /* Did we create any drives that we failed to create a device for? */
4563 drive_check_orphaned();
4565 /* Don't warn about the default network setup that you get if
4566 * no command line -net or -netdev options are specified. There
4567 * are two cases that we would otherwise complain about:
4568 * (1) board doesn't support a NIC but the implicit "-net nic"
4569 * requested one
4570 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
4571 * sets up a nic that isn't connected to anything.
4573 if (!default_net) {
4574 net_check_clients();
4578 if (boot_once) {
4579 qemu_boot_set(boot_once, &error_fatal);
4580 qemu_register_reset(restore_boot_order, g_strdup(boot_order));
4583 ds = init_displaystate();
4585 /* init local displays */
4586 switch (display_type) {
4587 case DT_CURSES:
4588 curses_display_init(ds, full_screen);
4589 break;
4590 case DT_SDL:
4591 sdl_display_init(ds, full_screen, no_frame);
4592 break;
4593 case DT_COCOA:
4594 cocoa_display_init(ds, full_screen);
4595 break;
4596 case DT_GTK:
4597 gtk_display_init(ds, full_screen, grab_on_hover);
4598 break;
4599 default:
4600 break;
4603 /* must be after terminal init, SDL library changes signal handlers */
4604 os_setup_signal_handling();
4606 /* init remote displays */
4607 #ifdef CONFIG_VNC
4608 qemu_opts_foreach(qemu_find_opts("vnc"),
4609 vnc_init_func, NULL, NULL);
4610 #endif
4612 if (using_spice) {
4613 qemu_spice_display_init();
4616 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
4617 exit(1);
4620 qdev_machine_creation_done();
4622 /* TODO: once all bus devices are qdevified, this should be done
4623 * when bus is created by qdev.c */
4624 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4625 qemu_run_machine_init_done_notifiers();
4627 if (rom_check_and_register_reset() != 0) {
4628 error_report("rom check and register reset failed");
4629 exit(1);
4632 replay_start();
4634 /* This checkpoint is required by replay to separate prior clock
4635 reading from the other reads, because timer polling functions query
4636 clock values from the log. */
4637 replay_checkpoint(CHECKPOINT_RESET);
4638 qemu_system_reset(VMRESET_SILENT);
4639 register_global_state();
4640 if (replay_mode != REPLAY_MODE_NONE) {
4641 replay_vmstate_init();
4642 } else if (loadvm) {
4643 if (load_vmstate(loadvm) < 0) {
4644 autostart = 0;
4648 qdev_prop_check_globals();
4649 if (vmstate_dump_file) {
4650 /* dump and exit */
4651 dump_vmstate_json_to_file(vmstate_dump_file);
4652 return 0;
4655 if (incoming) {
4656 Error *local_err = NULL;
4657 qemu_start_incoming_migration(incoming, &local_err);
4658 if (local_err) {
4659 error_reportf_err(local_err, "-incoming %s: ", incoming);
4660 exit(1);
4662 } else if (autostart) {
4663 vm_start();
4666 os_setup_post();
4668 main_loop();
4669 replay_disable_events();
4670 iothread_stop_all();
4672 bdrv_close_all();
4673 pause_all_vcpus();
4674 res_free();
4676 /* vhost-user must be cleaned up before chardevs. */
4677 net_cleanup();
4678 audio_cleanup();
4679 monitor_cleanup();
4680 qemu_chr_cleanup();
4682 return 0;