qemu-common: Move tcg_enabled() etc. to sysemu/tcg.h
[qemu/ar7.git] / vl.c
blobefdfea26ee9f031225d7c98860cfea62e3364642
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.
25 #include "qemu/osdep.h"
26 #include "qemu/units.h"
27 #include "qapi/error.h"
28 #include "qemu-version.h"
29 #include "qemu/cutils.h"
30 #include "qemu/help_option.h"
31 #include "qemu/uuid.h"
32 #include "sysemu/seccomp.h"
33 #include "sysemu/tcg.h"
35 #ifdef CONFIG_SDL
36 #if defined(__APPLE__) || defined(main)
37 #include <SDL.h>
38 int qemu_main(int argc, char **argv, char **envp);
39 int main(int argc, char **argv)
41 return qemu_main(argc, argv, NULL);
43 #undef main
44 #define main qemu_main
45 #endif
46 #endif /* CONFIG_SDL */
48 #ifdef CONFIG_COCOA
49 #undef main
50 #define main qemu_main
51 #endif /* CONFIG_COCOA */
54 #include "qemu/error-report.h"
55 #include "qemu/sockets.h"
56 #include "hw/hw.h"
57 #include "hw/boards.h"
58 #include "sysemu/accel.h"
59 #include "hw/usb.h"
60 #include "hw/isa/isa.h"
61 #include "hw/scsi/scsi.h"
62 #include "hw/display/vga.h"
63 #include "hw/bt.h"
64 #include "sysemu/watchdog.h"
65 #include "hw/firmware/smbios.h"
66 #include "hw/acpi/acpi.h"
67 #include "hw/xen/xen.h"
68 #include "hw/qdev.h"
69 #include "hw/loader.h"
70 #include "monitor/qdev.h"
71 #include "sysemu/bt.h"
72 #include "net/net.h"
73 #include "net/slirp.h"
74 #include "monitor/monitor.h"
75 #include "ui/console.h"
76 #include "ui/input.h"
77 #include "sysemu/sysemu.h"
78 #include "sysemu/numa.h"
79 #include "exec/gdbstub.h"
80 #include "qemu/timer.h"
81 #include "chardev/char.h"
82 #include "qemu/bitmap.h"
83 #include "qemu/log.h"
84 #include "sysemu/blockdev.h"
85 #include "hw/block/block.h"
86 #include "migration/misc.h"
87 #include "migration/snapshot.h"
88 #include "migration/global_state.h"
89 #include "sysemu/tpm.h"
90 #include "sysemu/dma.h"
91 #include "hw/audio/soundhw.h"
92 #include "audio/audio.h"
93 #include "sysemu/cpus.h"
94 #include "migration/colo.h"
95 #include "migration/postcopy-ram.h"
96 #include "sysemu/kvm.h"
97 #include "sysemu/hax.h"
98 #include "qapi/qobject-input-visitor.h"
99 #include "qemu/option.h"
100 #include "qemu/config-file.h"
101 #include "qemu-options.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"
110 #include "trace-root.h"
111 #include "trace/control.h"
112 #include "qemu/queue.h"
113 #include "sysemu/arch_init.h"
115 #include "ui/qemu-spice.h"
116 #include "qapi/string-input-visitor.h"
117 #include "qapi/opts-visitor.h"
118 #include "qapi/clone-visitor.h"
119 #include "qom/object_interfaces.h"
120 #include "hw/semihosting/semihost.h"
121 #include "crypto/init.h"
122 #include "sysemu/replay.h"
123 #include "qapi/qapi-events-run-state.h"
124 #include "qapi/qapi-visit-block-core.h"
125 #include "qapi/qapi-visit-ui.h"
126 #include "qapi/qapi-commands-block-core.h"
127 #include "qapi/qapi-commands-misc.h"
128 #include "qapi/qapi-commands-run-state.h"
129 #include "qapi/qapi-commands-ui.h"
130 #include "qapi/qmp/qerror.h"
131 #include "sysemu/iothread.h"
132 #include "qemu/guest-random.h"
134 #define MAX_VIRTIO_CONSOLES 1
136 static const char *data_dir[16];
137 static int data_dir_idx;
138 const char *bios_name = NULL;
139 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
140 int display_opengl;
141 const char* keyboard_layout = NULL;
142 ram_addr_t ram_size;
143 const char *mem_path = NULL;
144 int mem_prealloc = 0; /* force preallocation of physical target memory */
145 bool enable_mlock = false;
146 bool enable_cpu_pm = false;
147 int nb_nics;
148 NICInfo nd_table[MAX_NICS];
149 int autostart;
150 static enum {
151 RTC_BASE_UTC,
152 RTC_BASE_LOCALTIME,
153 RTC_BASE_DATETIME,
154 } rtc_base_type = RTC_BASE_UTC;
155 static time_t rtc_ref_start_datetime;
156 static int rtc_realtime_clock_offset; /* used only with QEMU_CLOCK_REALTIME */
157 static int rtc_host_datetime_offset = -1; /* valid & used only with
158 RTC_BASE_DATETIME */
159 QEMUClockType rtc_clock;
160 int vga_interface_type = VGA_NONE;
161 static DisplayOptions dpy;
162 static int num_serial_hds;
163 static Chardev **serial_hds;
164 Chardev *parallel_hds[MAX_PARALLEL_PORTS];
165 int win2k_install_hack = 0;
166 int singlestep = 0;
167 int smp_cpus;
168 unsigned int max_cpus;
169 int smp_cores = 1;
170 int smp_threads = 1;
171 int acpi_enabled = 1;
172 int no_hpet = 0;
173 int fd_bootchk = 1;
174 static int no_reboot;
175 int no_shutdown = 0;
176 int cursor_hide = 1;
177 int graphic_rotate = 0;
178 const char *watchdog;
179 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
180 int nb_option_roms;
181 int old_param = 0;
182 const char *qemu_name;
183 int alt_grab = 0;
184 int ctrl_grab = 0;
185 unsigned int nb_prom_envs = 0;
186 const char *prom_envs[MAX_PROM_ENVS];
187 int boot_menu;
188 bool boot_strict;
189 uint8_t *boot_splash_filedata;
190 int only_migratable; /* turn it off unless user states otherwise */
191 bool wakeup_suspend_enabled;
193 int icount_align_option;
195 /* The bytes in qemu_uuid are in the order specified by RFC4122, _not_ in the
196 * little-endian "wire format" described in the SMBIOS 2.6 specification.
198 QemuUUID qemu_uuid;
199 bool qemu_uuid_set;
201 static NotifierList exit_notifiers =
202 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
204 static NotifierList machine_init_done_notifiers =
205 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
207 bool xen_allowed;
208 uint32_t xen_domid;
209 enum xen_mode xen_mode = XEN_EMULATE;
210 bool xen_domid_restrict;
212 static int has_defaults = 1;
213 static int default_serial = 1;
214 static int default_parallel = 1;
215 static int default_monitor = 1;
216 static int default_floppy = 1;
217 static int default_cdrom = 1;
218 static int default_sdcard = 1;
219 static int default_vga = 1;
220 static int default_net = 1;
222 static struct {
223 const char *driver;
224 int *flag;
225 } default_list[] = {
226 { .driver = "isa-serial", .flag = &default_serial },
227 { .driver = "isa-parallel", .flag = &default_parallel },
228 { .driver = "isa-fdc", .flag = &default_floppy },
229 { .driver = "floppy", .flag = &default_floppy },
230 { .driver = "ide-cd", .flag = &default_cdrom },
231 { .driver = "ide-hd", .flag = &default_cdrom },
232 { .driver = "ide-drive", .flag = &default_cdrom },
233 { .driver = "scsi-cd", .flag = &default_cdrom },
234 { .driver = "scsi-hd", .flag = &default_cdrom },
235 { .driver = "VGA", .flag = &default_vga },
236 { .driver = "isa-vga", .flag = &default_vga },
237 { .driver = "cirrus-vga", .flag = &default_vga },
238 { .driver = "isa-cirrus-vga", .flag = &default_vga },
239 { .driver = "vmware-svga", .flag = &default_vga },
240 { .driver = "qxl-vga", .flag = &default_vga },
241 { .driver = "virtio-vga", .flag = &default_vga },
242 { .driver = "ati-vga", .flag = &default_vga },
243 { .driver = "vhost-user-vga", .flag = &default_vga },
246 static QemuOptsList qemu_rtc_opts = {
247 .name = "rtc",
248 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
249 .merge_lists = true,
250 .desc = {
252 .name = "base",
253 .type = QEMU_OPT_STRING,
255 .name = "clock",
256 .type = QEMU_OPT_STRING,
258 .name = "driftfix",
259 .type = QEMU_OPT_STRING,
261 { /* end of list */ }
265 static QemuOptsList qemu_option_rom_opts = {
266 .name = "option-rom",
267 .implied_opt_name = "romfile",
268 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
269 .desc = {
271 .name = "bootindex",
272 .type = QEMU_OPT_NUMBER,
273 }, {
274 .name = "romfile",
275 .type = QEMU_OPT_STRING,
277 { /* end of list */ }
281 static QemuOptsList qemu_machine_opts = {
282 .name = "machine",
283 .implied_opt_name = "type",
284 .merge_lists = true,
285 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
286 .desc = {
288 * no elements => accept any
289 * sanity checking will happen later
290 * when setting machine properties
296 static QemuOptsList qemu_accel_opts = {
297 .name = "accel",
298 .implied_opt_name = "accel",
299 .head = QTAILQ_HEAD_INITIALIZER(qemu_accel_opts.head),
300 .merge_lists = true,
301 .desc = {
303 .name = "accel",
304 .type = QEMU_OPT_STRING,
305 .help = "Select the type of accelerator",
308 .name = "thread",
309 .type = QEMU_OPT_STRING,
310 .help = "Enable/disable multi-threaded TCG",
312 { /* end of list */ }
316 static QemuOptsList qemu_boot_opts = {
317 .name = "boot-opts",
318 .implied_opt_name = "order",
319 .merge_lists = true,
320 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
321 .desc = {
323 .name = "order",
324 .type = QEMU_OPT_STRING,
325 }, {
326 .name = "once",
327 .type = QEMU_OPT_STRING,
328 }, {
329 .name = "menu",
330 .type = QEMU_OPT_BOOL,
331 }, {
332 .name = "splash",
333 .type = QEMU_OPT_STRING,
334 }, {
335 .name = "splash-time",
336 .type = QEMU_OPT_NUMBER,
337 }, {
338 .name = "reboot-timeout",
339 .type = QEMU_OPT_NUMBER,
340 }, {
341 .name = "strict",
342 .type = QEMU_OPT_BOOL,
344 { /*End of list */ }
348 static QemuOptsList qemu_add_fd_opts = {
349 .name = "add-fd",
350 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
351 .desc = {
353 .name = "fd",
354 .type = QEMU_OPT_NUMBER,
355 .help = "file descriptor of which a duplicate is added to fd set",
357 .name = "set",
358 .type = QEMU_OPT_NUMBER,
359 .help = "ID of the fd set to add fd to",
361 .name = "opaque",
362 .type = QEMU_OPT_STRING,
363 .help = "free-form string used to describe fd",
365 { /* end of list */ }
369 static QemuOptsList qemu_object_opts = {
370 .name = "object",
371 .implied_opt_name = "qom-type",
372 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
373 .desc = {
378 static QemuOptsList qemu_tpmdev_opts = {
379 .name = "tpmdev",
380 .implied_opt_name = "type",
381 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
382 .desc = {
383 /* options are defined in the TPM backends */
384 { /* end of list */ }
388 static QemuOptsList qemu_realtime_opts = {
389 .name = "realtime",
390 .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
391 .desc = {
393 .name = "mlock",
394 .type = QEMU_OPT_BOOL,
396 { /* end of list */ }
400 static QemuOptsList qemu_overcommit_opts = {
401 .name = "overcommit",
402 .head = QTAILQ_HEAD_INITIALIZER(qemu_overcommit_opts.head),
403 .desc = {
405 .name = "mem-lock",
406 .type = QEMU_OPT_BOOL,
409 .name = "cpu-pm",
410 .type = QEMU_OPT_BOOL,
412 { /* end of list */ }
416 static QemuOptsList qemu_msg_opts = {
417 .name = "msg",
418 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
419 .desc = {
421 .name = "timestamp",
422 .type = QEMU_OPT_BOOL,
424 { /* end of list */ }
428 static QemuOptsList qemu_name_opts = {
429 .name = "name",
430 .implied_opt_name = "guest",
431 .merge_lists = true,
432 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
433 .desc = {
435 .name = "guest",
436 .type = QEMU_OPT_STRING,
437 .help = "Sets the name of the guest.\n"
438 "This name will be displayed in the SDL window caption.\n"
439 "The name will also be used for the VNC server",
440 }, {
441 .name = "process",
442 .type = QEMU_OPT_STRING,
443 .help = "Sets the name of the QEMU process, as shown in top etc",
444 }, {
445 .name = "debug-threads",
446 .type = QEMU_OPT_BOOL,
447 .help = "When enabled, name the individual threads; defaults off.\n"
448 "NOTE: The thread names are for debugging and not a\n"
449 "stable API.",
451 { /* End of list */ }
455 static QemuOptsList qemu_mem_opts = {
456 .name = "memory",
457 .implied_opt_name = "size",
458 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
459 .merge_lists = true,
460 .desc = {
462 .name = "size",
463 .type = QEMU_OPT_SIZE,
466 .name = "slots",
467 .type = QEMU_OPT_NUMBER,
470 .name = "maxmem",
471 .type = QEMU_OPT_SIZE,
473 { /* end of list */ }
477 static QemuOptsList qemu_icount_opts = {
478 .name = "icount",
479 .implied_opt_name = "shift",
480 .merge_lists = true,
481 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
482 .desc = {
484 .name = "shift",
485 .type = QEMU_OPT_STRING,
486 }, {
487 .name = "align",
488 .type = QEMU_OPT_BOOL,
489 }, {
490 .name = "sleep",
491 .type = QEMU_OPT_BOOL,
492 }, {
493 .name = "rr",
494 .type = QEMU_OPT_STRING,
495 }, {
496 .name = "rrfile",
497 .type = QEMU_OPT_STRING,
498 }, {
499 .name = "rrsnapshot",
500 .type = QEMU_OPT_STRING,
502 { /* end of list */ }
506 static QemuOptsList qemu_fw_cfg_opts = {
507 .name = "fw_cfg",
508 .implied_opt_name = "name",
509 .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
510 .desc = {
512 .name = "name",
513 .type = QEMU_OPT_STRING,
514 .help = "Sets the fw_cfg name of the blob to be inserted",
515 }, {
516 .name = "file",
517 .type = QEMU_OPT_STRING,
518 .help = "Sets the name of the file from which "
519 "the fw_cfg blob will be loaded",
520 }, {
521 .name = "string",
522 .type = QEMU_OPT_STRING,
523 .help = "Sets content of the blob to be inserted from a string",
525 { /* end of list */ }
530 * Get machine options
532 * Returns: machine options (never null).
534 QemuOpts *qemu_get_machine_opts(void)
536 return qemu_find_opts_singleton("machine");
539 const char *qemu_get_vm_name(void)
541 return qemu_name;
544 static void res_free(void)
546 g_free(boot_splash_filedata);
547 boot_splash_filedata = NULL;
550 static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
552 const char *driver = qemu_opt_get(opts, "driver");
553 int i;
555 if (!driver)
556 return 0;
557 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
558 if (strcmp(default_list[i].driver, driver) != 0)
559 continue;
560 *(default_list[i].flag) = 0;
562 return 0;
565 /***********************************************************/
566 /* QEMU state */
568 static RunState current_run_state = RUN_STATE_PRECONFIG;
570 /* We use RUN_STATE__MAX but any invalid value will do */
571 static RunState vmstop_requested = RUN_STATE__MAX;
572 static QemuMutex vmstop_lock;
574 typedef struct {
575 RunState from;
576 RunState to;
577 } RunStateTransition;
579 static const RunStateTransition runstate_transitions_def[] = {
580 /* from -> to */
581 { RUN_STATE_PRECONFIG, RUN_STATE_PRELAUNCH },
582 /* Early switch to inmigrate state to allow -incoming CLI option work
583 * as it used to. TODO: delay actual switching to inmigrate state to
584 * the point after machine is built and remove this hack.
586 { RUN_STATE_PRECONFIG, RUN_STATE_INMIGRATE },
588 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
589 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
590 { RUN_STATE_DEBUG, RUN_STATE_PRELAUNCH },
592 { RUN_STATE_INMIGRATE, RUN_STATE_INTERNAL_ERROR },
593 { RUN_STATE_INMIGRATE, RUN_STATE_IO_ERROR },
594 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
595 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
596 { RUN_STATE_INMIGRATE, RUN_STATE_SHUTDOWN },
597 { RUN_STATE_INMIGRATE, RUN_STATE_SUSPENDED },
598 { RUN_STATE_INMIGRATE, RUN_STATE_WATCHDOG },
599 { RUN_STATE_INMIGRATE, RUN_STATE_GUEST_PANICKED },
600 { RUN_STATE_INMIGRATE, RUN_STATE_FINISH_MIGRATE },
601 { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH },
602 { RUN_STATE_INMIGRATE, RUN_STATE_POSTMIGRATE },
603 { RUN_STATE_INMIGRATE, RUN_STATE_COLO },
605 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
606 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
607 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PRELAUNCH },
609 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
610 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
611 { RUN_STATE_IO_ERROR, RUN_STATE_PRELAUNCH },
613 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
614 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
615 { RUN_STATE_PAUSED, RUN_STATE_POSTMIGRATE },
616 { RUN_STATE_PAUSED, RUN_STATE_PRELAUNCH },
617 { RUN_STATE_PAUSED, RUN_STATE_COLO},
619 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
620 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
621 { RUN_STATE_POSTMIGRATE, RUN_STATE_PRELAUNCH },
623 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
624 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
625 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
627 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
628 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PAUSED },
629 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
630 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PRELAUNCH },
631 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_COLO},
633 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
634 { RUN_STATE_RESTORE_VM, RUN_STATE_PRELAUNCH },
636 { RUN_STATE_COLO, RUN_STATE_RUNNING },
638 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
639 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
640 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
641 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
642 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
643 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
644 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
645 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
646 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
647 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
648 { RUN_STATE_RUNNING, RUN_STATE_COLO},
650 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
652 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
653 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
654 { RUN_STATE_SHUTDOWN, RUN_STATE_PRELAUNCH },
656 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
657 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
658 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
659 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
660 { RUN_STATE_SUSPENDED, RUN_STATE_PRELAUNCH },
661 { RUN_STATE_SUSPENDED, RUN_STATE_COLO},
663 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
664 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
665 { RUN_STATE_WATCHDOG, RUN_STATE_PRELAUNCH },
666 { RUN_STATE_WATCHDOG, RUN_STATE_COLO},
668 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
669 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
670 { RUN_STATE_GUEST_PANICKED, RUN_STATE_PRELAUNCH },
672 { RUN_STATE__MAX, RUN_STATE__MAX },
675 static bool runstate_valid_transitions[RUN_STATE__MAX][RUN_STATE__MAX];
677 bool runstate_check(RunState state)
679 return current_run_state == state;
682 bool runstate_store(char *str, size_t size)
684 const char *state = RunState_str(current_run_state);
685 size_t len = strlen(state) + 1;
687 if (len > size) {
688 return false;
690 memcpy(str, state, len);
691 return true;
694 static void runstate_init(void)
696 const RunStateTransition *p;
698 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
699 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE__MAX; p++) {
700 runstate_valid_transitions[p->from][p->to] = true;
703 qemu_mutex_init(&vmstop_lock);
706 /* This function will abort() on invalid state transitions */
707 void runstate_set(RunState new_state)
709 assert(new_state < RUN_STATE__MAX);
711 trace_runstate_set(current_run_state, RunState_str(current_run_state),
712 new_state, RunState_str(new_state));
714 if (current_run_state == new_state) {
715 return;
718 if (!runstate_valid_transitions[current_run_state][new_state]) {
719 error_report("invalid runstate transition: '%s' -> '%s'",
720 RunState_str(current_run_state),
721 RunState_str(new_state));
722 abort();
725 current_run_state = new_state;
728 int runstate_is_running(void)
730 return runstate_check(RUN_STATE_RUNNING);
733 bool runstate_needs_reset(void)
735 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
736 runstate_check(RUN_STATE_SHUTDOWN);
739 StatusInfo *qmp_query_status(Error **errp)
741 StatusInfo *info = g_malloc0(sizeof(*info));
743 info->running = runstate_is_running();
744 info->singlestep = singlestep;
745 info->status = current_run_state;
747 return info;
750 bool qemu_vmstop_requested(RunState *r)
752 qemu_mutex_lock(&vmstop_lock);
753 *r = vmstop_requested;
754 vmstop_requested = RUN_STATE__MAX;
755 qemu_mutex_unlock(&vmstop_lock);
756 return *r < RUN_STATE__MAX;
759 void qemu_system_vmstop_request_prepare(void)
761 qemu_mutex_lock(&vmstop_lock);
764 void qemu_system_vmstop_request(RunState state)
766 vmstop_requested = state;
767 qemu_mutex_unlock(&vmstop_lock);
768 qemu_notify_event();
771 /***********************************************************/
772 /* RTC reference time/date access */
773 static time_t qemu_ref_timedate(QEMUClockType clock)
775 time_t value = qemu_clock_get_ms(clock) / 1000;
776 switch (clock) {
777 case QEMU_CLOCK_REALTIME:
778 value -= rtc_realtime_clock_offset;
779 /* no break */
780 case QEMU_CLOCK_VIRTUAL:
781 value += rtc_ref_start_datetime;
782 break;
783 case QEMU_CLOCK_HOST:
784 if (rtc_base_type == RTC_BASE_DATETIME) {
785 value -= rtc_host_datetime_offset;
787 break;
788 default:
789 assert(0);
791 return value;
794 void qemu_get_timedate(struct tm *tm, int offset)
796 time_t ti = qemu_ref_timedate(rtc_clock);
798 ti += offset;
800 switch (rtc_base_type) {
801 case RTC_BASE_DATETIME:
802 case RTC_BASE_UTC:
803 gmtime_r(&ti, tm);
804 break;
805 case RTC_BASE_LOCALTIME:
806 localtime_r(&ti, tm);
807 break;
811 int qemu_timedate_diff(struct tm *tm)
813 time_t seconds;
815 switch (rtc_base_type) {
816 case RTC_BASE_DATETIME:
817 case RTC_BASE_UTC:
818 seconds = mktimegm(tm);
819 break;
820 case RTC_BASE_LOCALTIME:
822 struct tm tmp = *tm;
823 tmp.tm_isdst = -1; /* use timezone to figure it out */
824 seconds = mktime(&tmp);
825 break;
827 default:
828 abort();
831 return seconds - qemu_ref_timedate(QEMU_CLOCK_HOST);
834 static void configure_rtc_base_datetime(const char *startdate)
836 time_t rtc_start_datetime;
837 struct tm tm;
839 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d", &tm.tm_year, &tm.tm_mon,
840 &tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec) == 6) {
841 /* OK */
842 } else if (sscanf(startdate, "%d-%d-%d",
843 &tm.tm_year, &tm.tm_mon, &tm.tm_mday) == 3) {
844 tm.tm_hour = 0;
845 tm.tm_min = 0;
846 tm.tm_sec = 0;
847 } else {
848 goto date_fail;
850 tm.tm_year -= 1900;
851 tm.tm_mon--;
852 rtc_start_datetime = mktimegm(&tm);
853 if (rtc_start_datetime == -1) {
854 date_fail:
855 error_report("invalid datetime format");
856 error_printf("valid formats: "
857 "'2006-06-17T16:01:21' or '2006-06-17'\n");
858 exit(1);
860 rtc_host_datetime_offset = rtc_ref_start_datetime - rtc_start_datetime;
861 rtc_ref_start_datetime = rtc_start_datetime;
864 static void configure_rtc(QemuOpts *opts)
866 const char *value;
868 /* Set defaults */
869 rtc_clock = QEMU_CLOCK_HOST;
870 rtc_ref_start_datetime = qemu_clock_get_ms(QEMU_CLOCK_HOST) / 1000;
871 rtc_realtime_clock_offset = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) / 1000;
873 value = qemu_opt_get(opts, "base");
874 if (value) {
875 if (!strcmp(value, "utc")) {
876 rtc_base_type = RTC_BASE_UTC;
877 } else if (!strcmp(value, "localtime")) {
878 Error *blocker = NULL;
879 rtc_base_type = RTC_BASE_LOCALTIME;
880 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
881 "-rtc base=localtime");
882 replay_add_blocker(blocker);
883 } else {
884 rtc_base_type = RTC_BASE_DATETIME;
885 configure_rtc_base_datetime(value);
888 value = qemu_opt_get(opts, "clock");
889 if (value) {
890 if (!strcmp(value, "host")) {
891 rtc_clock = QEMU_CLOCK_HOST;
892 } else if (!strcmp(value, "rt")) {
893 rtc_clock = QEMU_CLOCK_REALTIME;
894 } else if (!strcmp(value, "vm")) {
895 rtc_clock = QEMU_CLOCK_VIRTUAL;
896 } else {
897 error_report("invalid option value '%s'", value);
898 exit(1);
901 value = qemu_opt_get(opts, "driftfix");
902 if (value) {
903 if (!strcmp(value, "slew")) {
904 static GlobalProperty slew_lost_ticks = {
905 .driver = "mc146818rtc",
906 .property = "lost_tick_policy",
907 .value = "slew",
910 qdev_prop_register_global(&slew_lost_ticks);
911 } else if (!strcmp(value, "none")) {
912 /* discard is default */
913 } else {
914 error_report("invalid option value '%s'", value);
915 exit(1);
920 /***********************************************************/
921 /* Bluetooth support */
922 static int nb_hcis;
923 static int cur_hci;
924 static struct HCIInfo *hci_table[MAX_NICS];
926 struct HCIInfo *qemu_next_hci(void)
928 if (cur_hci == nb_hcis)
929 return &null_hci;
931 return hci_table[cur_hci++];
934 static int bt_hci_parse(const char *str)
936 struct HCIInfo *hci;
937 bdaddr_t bdaddr;
939 if (nb_hcis >= MAX_NICS) {
940 error_report("too many bluetooth HCIs (max %i)", MAX_NICS);
941 return -1;
944 hci = hci_init(str);
945 if (!hci)
946 return -1;
948 bdaddr.b[0] = 0x52;
949 bdaddr.b[1] = 0x54;
950 bdaddr.b[2] = 0x00;
951 bdaddr.b[3] = 0x12;
952 bdaddr.b[4] = 0x34;
953 bdaddr.b[5] = 0x56 + nb_hcis;
954 hci->bdaddr_set(hci, bdaddr.b);
956 hci_table[nb_hcis++] = hci;
958 return 0;
961 static void bt_vhci_add(int vlan_id)
963 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
965 if (!vlan->slave)
966 warn_report("adding a VHCI to an empty scatternet %i",
967 vlan_id);
969 bt_vhci_init(bt_new_hci(vlan));
972 static struct bt_device_s *bt_device_add(const char *opt)
974 struct bt_scatternet_s *vlan;
975 int vlan_id = 0;
976 char *endp = strstr(opt, ",vlan=");
977 int len = (endp ? endp - opt : strlen(opt)) + 1;
978 char devname[10];
980 pstrcpy(devname, MIN(sizeof(devname), len), opt);
982 if (endp) {
983 vlan_id = strtol(endp + 6, &endp, 0);
984 if (*endp) {
985 error_report("unrecognised bluetooth vlan Id");
986 return 0;
990 vlan = qemu_find_bt_vlan(vlan_id);
992 if (!vlan->slave)
993 warn_report("adding a slave device to an empty scatternet %i",
994 vlan_id);
996 if (!strcmp(devname, "keyboard"))
997 return bt_keyboard_init(vlan);
999 error_report("unsupported bluetooth device '%s'", devname);
1000 return 0;
1003 static int bt_parse(const char *opt)
1005 const char *endp, *p;
1006 int vlan;
1008 if (strstart(opt, "hci", &endp)) {
1009 if (!*endp || *endp == ',') {
1010 if (*endp)
1011 if (!strstart(endp, ",vlan=", 0))
1012 opt = endp + 1;
1014 return bt_hci_parse(opt);
1016 } else if (strstart(opt, "vhci", &endp)) {
1017 if (!*endp || *endp == ',') {
1018 if (*endp) {
1019 if (strstart(endp, ",vlan=", &p)) {
1020 vlan = strtol(p, (char **) &endp, 0);
1021 if (*endp) {
1022 error_report("bad scatternet '%s'", p);
1023 return 1;
1025 } else {
1026 error_report("bad parameter '%s'", endp + 1);
1027 return 1;
1029 } else
1030 vlan = 0;
1032 bt_vhci_add(vlan);
1033 return 0;
1035 } else if (strstart(opt, "device:", &endp))
1036 return !bt_device_add(endp);
1038 error_report("bad bluetooth parameter '%s'", opt);
1039 return 1;
1042 static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
1044 const char *proc_name;
1046 if (qemu_opt_get(opts, "debug-threads")) {
1047 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
1049 qemu_name = qemu_opt_get(opts, "guest");
1051 proc_name = qemu_opt_get(opts, "process");
1052 if (proc_name) {
1053 os_set_proc_name(proc_name);
1056 return 0;
1059 bool defaults_enabled(void)
1061 return has_defaults;
1064 #ifndef _WIN32
1065 static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1067 int fd, dupfd, flags;
1068 int64_t fdset_id;
1069 const char *fd_opaque = NULL;
1070 AddfdInfo *fdinfo;
1072 fd = qemu_opt_get_number(opts, "fd", -1);
1073 fdset_id = qemu_opt_get_number(opts, "set", -1);
1074 fd_opaque = qemu_opt_get(opts, "opaque");
1076 if (fd < 0) {
1077 error_setg(errp, "fd option is required and must be non-negative");
1078 return -1;
1081 if (fd <= STDERR_FILENO) {
1082 error_setg(errp, "fd cannot be a standard I/O stream");
1083 return -1;
1087 * All fds inherited across exec() necessarily have FD_CLOEXEC
1088 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1090 flags = fcntl(fd, F_GETFD);
1091 if (flags == -1 || (flags & FD_CLOEXEC)) {
1092 error_setg(errp, "fd is not valid or already in use");
1093 return -1;
1096 if (fdset_id < 0) {
1097 error_setg(errp, "set option is required and must be non-negative");
1098 return -1;
1101 #ifdef F_DUPFD_CLOEXEC
1102 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1103 #else
1104 dupfd = dup(fd);
1105 if (dupfd != -1) {
1106 qemu_set_cloexec(dupfd);
1108 #endif
1109 if (dupfd == -1) {
1110 error_setg(errp, "error duplicating fd: %s", strerror(errno));
1111 return -1;
1114 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1115 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
1116 &error_abort);
1117 g_free(fdinfo);
1119 return 0;
1122 static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1124 int fd;
1126 fd = qemu_opt_get_number(opts, "fd", -1);
1127 close(fd);
1129 return 0;
1131 #endif
1133 /***********************************************************/
1134 /* QEMU Block devices */
1136 #define HD_OPTS "media=disk"
1137 #define CDROM_OPTS "media=cdrom"
1138 #define FD_OPTS ""
1139 #define PFLASH_OPTS ""
1140 #define MTD_OPTS ""
1141 #define SD_OPTS ""
1143 static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
1145 BlockInterfaceType *block_default_type = opaque;
1147 return drive_new(opts, *block_default_type, errp) == NULL;
1150 static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
1152 if (qemu_opt_get(opts, "snapshot") == NULL) {
1153 qemu_opt_set(opts, "snapshot", "on", &error_abort);
1155 return 0;
1158 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1159 int index, const char *optstr)
1161 QemuOpts *opts;
1162 DriveInfo *dinfo;
1164 if (!enable || drive_get_by_index(type, index)) {
1165 return;
1168 opts = drive_add(type, index, NULL, optstr);
1169 if (snapshot) {
1170 drive_enable_snapshot(NULL, opts, NULL);
1173 dinfo = drive_new(opts, type, &error_abort);
1174 dinfo->is_default = true;
1178 typedef struct BlockdevOptionsQueueEntry {
1179 BlockdevOptions *bdo;
1180 Location loc;
1181 QSIMPLEQ_ENTRY(BlockdevOptionsQueueEntry) entry;
1182 } BlockdevOptionsQueueEntry;
1184 typedef QSIMPLEQ_HEAD(, BlockdevOptionsQueueEntry) BlockdevOptionsQueue;
1186 static void configure_blockdev(BlockdevOptionsQueue *bdo_queue,
1187 MachineClass *machine_class, int snapshot)
1190 * If the currently selected machine wishes to override the
1191 * units-per-bus property of its default HBA interface type, do so
1192 * now.
1194 if (machine_class->units_per_default_bus) {
1195 override_max_devs(machine_class->block_default_type,
1196 machine_class->units_per_default_bus);
1199 /* open the virtual block devices */
1200 while (!QSIMPLEQ_EMPTY(bdo_queue)) {
1201 BlockdevOptionsQueueEntry *bdo = QSIMPLEQ_FIRST(bdo_queue);
1203 QSIMPLEQ_REMOVE_HEAD(bdo_queue, entry);
1204 loc_push_restore(&bdo->loc);
1205 qmp_blockdev_add(bdo->bdo, &error_fatal);
1206 loc_pop(&bdo->loc);
1207 qapi_free_BlockdevOptions(bdo->bdo);
1208 g_free(bdo);
1210 if (snapshot || replay_mode != REPLAY_MODE_NONE) {
1211 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
1212 NULL, NULL);
1214 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
1215 &machine_class->block_default_type, &error_fatal)) {
1216 /* We printed help */
1217 exit(0);
1220 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
1221 CDROM_OPTS);
1222 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
1223 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
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 cores = cores > 0 ? cores : 1;
1264 threads = threads > 0 ? threads : 1;
1265 if (cpus == 0) {
1266 sockets = sockets > 0 ? sockets : 1;
1267 cpus = cores * threads * sockets;
1268 } else {
1269 max_cpus = qemu_opt_get_number(opts, "maxcpus", cpus);
1270 sockets = max_cpus / (cores * threads);
1272 } else if (cores == 0) {
1273 threads = threads > 0 ? threads : 1;
1274 cores = cpus / (sockets * threads);
1275 cores = cores > 0 ? cores : 1;
1276 } else if (threads == 0) {
1277 threads = cpus / (cores * sockets);
1278 threads = threads > 0 ? threads : 1;
1279 } else if (sockets * cores * threads < cpus) {
1280 error_report("cpu topology: "
1281 "sockets (%u) * cores (%u) * threads (%u) < "
1282 "smp_cpus (%u)",
1283 sockets, cores, threads, cpus);
1284 exit(1);
1287 max_cpus = qemu_opt_get_number(opts, "maxcpus", cpus);
1289 if (max_cpus < cpus) {
1290 error_report("maxcpus must be equal to or greater than smp");
1291 exit(1);
1294 if (sockets * cores * threads > max_cpus) {
1295 error_report("cpu topology: "
1296 "sockets (%u) * cores (%u) * threads (%u) > "
1297 "maxcpus (%u)",
1298 sockets, cores, threads, max_cpus);
1299 exit(1);
1302 if (sockets * cores * threads != max_cpus) {
1303 warn_report("Invalid CPU topology deprecated: "
1304 "sockets (%u) * cores (%u) * threads (%u) "
1305 "!= maxcpus (%u)",
1306 sockets, cores, threads, max_cpus);
1309 smp_cpus = cpus;
1310 smp_cores = cores;
1311 smp_threads = threads;
1314 if (smp_cpus > 1) {
1315 Error *blocker = NULL;
1316 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
1317 replay_add_blocker(blocker);
1321 static void realtime_init(void)
1323 if (enable_mlock) {
1324 if (os_mlock() < 0) {
1325 error_report("locking memory failed");
1326 exit(1);
1332 static void configure_msg(QemuOpts *opts)
1334 enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
1338 /* Now we still need this for compatibility with XEN. */
1339 bool has_igd_gfx_passthru;
1340 static void igd_gfx_passthru(void)
1342 has_igd_gfx_passthru = current_machine->igd_gfx_passthru;
1345 /***********************************************************/
1346 /* USB devices */
1348 static int usb_device_add(const char *devname)
1350 USBDevice *dev = NULL;
1352 if (!machine_usb(current_machine)) {
1353 return -1;
1356 dev = usbdevice_create(devname);
1357 if (!dev)
1358 return -1;
1360 return 0;
1363 static int usb_parse(const char *cmdline)
1365 int r;
1366 r = usb_device_add(cmdline);
1367 if (r < 0) {
1368 error_report("could not add USB device '%s'", cmdline);
1370 return r;
1373 /***********************************************************/
1374 /* machine registration */
1376 MachineState *current_machine;
1378 static MachineClass *find_machine(const char *name, GSList *machines)
1380 GSList *el;
1382 for (el = machines; el; el = el->next) {
1383 MachineClass *mc = el->data;
1385 if (!strcmp(mc->name, name) || !g_strcmp0(mc->alias, name)) {
1386 return mc;
1390 return NULL;
1393 static MachineClass *find_default_machine(GSList *machines)
1395 GSList *el;
1397 for (el = machines; el; el = el->next) {
1398 MachineClass *mc = el->data;
1400 if (mc->is_default) {
1401 return mc;
1405 return NULL;
1408 MachineInfoList *qmp_query_machines(Error **errp)
1410 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1411 MachineInfoList *mach_list = NULL;
1413 for (el = machines; el; el = el->next) {
1414 MachineClass *mc = el->data;
1415 MachineInfoList *entry;
1416 MachineInfo *info;
1418 info = g_malloc0(sizeof(*info));
1419 if (mc->is_default) {
1420 info->has_is_default = true;
1421 info->is_default = true;
1424 if (mc->alias) {
1425 info->has_alias = true;
1426 info->alias = g_strdup(mc->alias);
1429 info->name = g_strdup(mc->name);
1430 info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
1431 info->hotpluggable_cpus = mc->has_hotpluggable_cpus;
1433 entry = g_malloc0(sizeof(*entry));
1434 entry->value = info;
1435 entry->next = mach_list;
1436 mach_list = entry;
1439 g_slist_free(machines);
1440 return mach_list;
1443 static int machine_help_func(QemuOpts *opts, MachineState *machine)
1445 ObjectProperty *prop;
1446 ObjectPropertyIterator iter;
1448 if (!qemu_opt_has_help_opt(opts)) {
1449 return 0;
1452 object_property_iter_init(&iter, OBJECT(machine));
1453 while ((prop = object_property_iter_next(&iter))) {
1454 if (!prop->set) {
1455 continue;
1458 printf("%s.%s=%s", MACHINE_GET_CLASS(machine)->name,
1459 prop->name, prop->type);
1460 if (prop->description) {
1461 printf(" (%s)\n", prop->description);
1462 } else {
1463 printf("\n");
1467 return 1;
1470 struct vm_change_state_entry {
1471 VMChangeStateHandler *cb;
1472 void *opaque;
1473 QLIST_ENTRY (vm_change_state_entry) entries;
1476 static QLIST_HEAD(, vm_change_state_entry) vm_change_state_head;
1478 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1479 void *opaque)
1481 VMChangeStateEntry *e;
1483 e = g_malloc0(sizeof (*e));
1485 e->cb = cb;
1486 e->opaque = opaque;
1487 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1488 return e;
1491 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1493 QLIST_REMOVE (e, entries);
1494 g_free (e);
1497 void vm_state_notify(int running, RunState state)
1499 VMChangeStateEntry *e, *next;
1501 trace_vm_state_notify(running, state, RunState_str(state));
1503 QLIST_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
1504 e->cb(e->opaque, running, state);
1508 static ShutdownCause reset_requested;
1509 static ShutdownCause shutdown_requested;
1510 static int shutdown_signal;
1511 static pid_t shutdown_pid;
1512 static int powerdown_requested;
1513 static int debug_requested;
1514 static int suspend_requested;
1515 static bool preconfig_exit_requested = true;
1516 static WakeupReason wakeup_reason;
1517 static NotifierList powerdown_notifiers =
1518 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
1519 static NotifierList suspend_notifiers =
1520 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1521 static NotifierList wakeup_notifiers =
1522 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1523 static NotifierList shutdown_notifiers =
1524 NOTIFIER_LIST_INITIALIZER(shutdown_notifiers);
1525 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
1527 ShutdownCause qemu_shutdown_requested_get(void)
1529 return shutdown_requested;
1532 ShutdownCause qemu_reset_requested_get(void)
1534 return reset_requested;
1537 static int qemu_shutdown_requested(void)
1539 return atomic_xchg(&shutdown_requested, SHUTDOWN_CAUSE_NONE);
1542 static void qemu_kill_report(void)
1544 if (!qtest_driver() && shutdown_signal) {
1545 if (shutdown_pid == 0) {
1546 /* This happens for eg ^C at the terminal, so it's worth
1547 * avoiding printing an odd message in that case.
1549 error_report("terminating on signal %d", shutdown_signal);
1550 } else {
1551 char *shutdown_cmd = qemu_get_pid_name(shutdown_pid);
1553 error_report("terminating on signal %d from pid " FMT_pid " (%s)",
1554 shutdown_signal, shutdown_pid,
1555 shutdown_cmd ? shutdown_cmd : "<unknown process>");
1556 g_free(shutdown_cmd);
1558 shutdown_signal = 0;
1562 static ShutdownCause qemu_reset_requested(void)
1564 ShutdownCause r = reset_requested;
1566 if (r && replay_checkpoint(CHECKPOINT_RESET_REQUESTED)) {
1567 reset_requested = SHUTDOWN_CAUSE_NONE;
1568 return r;
1570 return SHUTDOWN_CAUSE_NONE;
1573 static int qemu_suspend_requested(void)
1575 int r = suspend_requested;
1576 if (r && replay_checkpoint(CHECKPOINT_SUSPEND_REQUESTED)) {
1577 suspend_requested = 0;
1578 return r;
1580 return false;
1583 static WakeupReason qemu_wakeup_requested(void)
1585 return wakeup_reason;
1588 static int qemu_powerdown_requested(void)
1590 int r = powerdown_requested;
1591 powerdown_requested = 0;
1592 return r;
1595 static int qemu_debug_requested(void)
1597 int r = debug_requested;
1598 debug_requested = 0;
1599 return r;
1602 void qemu_exit_preconfig_request(void)
1604 preconfig_exit_requested = true;
1608 * Reset the VM. Issue an event unless @reason is SHUTDOWN_CAUSE_NONE.
1610 void qemu_system_reset(ShutdownCause reason)
1612 MachineClass *mc;
1614 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1616 cpu_synchronize_all_states();
1618 if (mc && mc->reset) {
1619 mc->reset();
1620 } else {
1621 qemu_devices_reset();
1623 if (reason != SHUTDOWN_CAUSE_SUBSYSTEM_RESET) {
1624 qapi_event_send_reset(shutdown_caused_by_guest(reason), reason);
1626 cpu_synchronize_all_post_reset();
1629 void qemu_system_guest_panicked(GuestPanicInformation *info)
1631 qemu_log_mask(LOG_GUEST_ERROR, "Guest crashed");
1633 if (current_cpu) {
1634 current_cpu->crash_occurred = true;
1636 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE,
1637 !!info, info);
1638 vm_stop(RUN_STATE_GUEST_PANICKED);
1639 if (!no_shutdown) {
1640 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_POWEROFF,
1641 !!info, info);
1642 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_PANIC);
1645 if (info) {
1646 if (info->type == GUEST_PANIC_INFORMATION_TYPE_HYPER_V) {
1647 qemu_log_mask(LOG_GUEST_ERROR, "\nHV crash parameters: (%#"PRIx64
1648 " %#"PRIx64" %#"PRIx64" %#"PRIx64" %#"PRIx64")\n",
1649 info->u.hyper_v.arg1,
1650 info->u.hyper_v.arg2,
1651 info->u.hyper_v.arg3,
1652 info->u.hyper_v.arg4,
1653 info->u.hyper_v.arg5);
1654 } else if (info->type == GUEST_PANIC_INFORMATION_TYPE_S390) {
1655 qemu_log_mask(LOG_GUEST_ERROR, " on cpu %d: %s\n"
1656 "PSW: 0x%016" PRIx64 " 0x%016" PRIx64"\n",
1657 info->u.s390.core,
1658 S390CrashReason_str(info->u.s390.reason),
1659 info->u.s390.psw_mask,
1660 info->u.s390.psw_addr);
1662 qapi_free_GuestPanicInformation(info);
1666 void qemu_system_reset_request(ShutdownCause reason)
1668 if (no_reboot && reason != SHUTDOWN_CAUSE_SUBSYSTEM_RESET) {
1669 shutdown_requested = reason;
1670 } else {
1671 reset_requested = reason;
1673 cpu_stop_current();
1674 qemu_notify_event();
1677 static void qemu_system_suspend(void)
1679 pause_all_vcpus();
1680 notifier_list_notify(&suspend_notifiers, NULL);
1681 runstate_set(RUN_STATE_SUSPENDED);
1682 qapi_event_send_suspend();
1685 void qemu_system_suspend_request(void)
1687 if (runstate_check(RUN_STATE_SUSPENDED)) {
1688 return;
1690 suspend_requested = 1;
1691 cpu_stop_current();
1692 qemu_notify_event();
1695 void qemu_register_suspend_notifier(Notifier *notifier)
1697 notifier_list_add(&suspend_notifiers, notifier);
1700 void qemu_system_wakeup_request(WakeupReason reason, Error **errp)
1702 trace_system_wakeup_request(reason);
1704 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1705 error_setg(errp,
1706 "Unable to wake up: guest is not in suspended state");
1707 return;
1709 if (!(wakeup_reason_mask & (1 << reason))) {
1710 return;
1712 runstate_set(RUN_STATE_RUNNING);
1713 wakeup_reason = reason;
1714 qemu_notify_event();
1717 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1719 if (enabled) {
1720 wakeup_reason_mask |= (1 << reason);
1721 } else {
1722 wakeup_reason_mask &= ~(1 << reason);
1726 void qemu_register_wakeup_notifier(Notifier *notifier)
1728 notifier_list_add(&wakeup_notifiers, notifier);
1731 void qemu_register_wakeup_support(void)
1733 wakeup_suspend_enabled = true;
1736 bool qemu_wakeup_suspend_enabled(void)
1738 return wakeup_suspend_enabled;
1741 CurrentMachineParams *qmp_query_current_machine(Error **errp)
1743 CurrentMachineParams *params = g_malloc0(sizeof(*params));
1744 params->wakeup_suspend_support = qemu_wakeup_suspend_enabled();
1746 return params;
1749 void qemu_system_killed(int signal, pid_t pid)
1751 shutdown_signal = signal;
1752 shutdown_pid = pid;
1753 no_shutdown = 0;
1755 /* Cannot call qemu_system_shutdown_request directly because
1756 * we are in a signal handler.
1758 shutdown_requested = SHUTDOWN_CAUSE_HOST_SIGNAL;
1759 qemu_notify_event();
1762 void qemu_system_shutdown_request(ShutdownCause reason)
1764 trace_qemu_system_shutdown_request(reason);
1765 replay_shutdown_request(reason);
1766 shutdown_requested = reason;
1767 qemu_notify_event();
1770 static void qemu_system_powerdown(void)
1772 qapi_event_send_powerdown();
1773 notifier_list_notify(&powerdown_notifiers, NULL);
1776 static void qemu_system_shutdown(ShutdownCause cause)
1778 qapi_event_send_shutdown(shutdown_caused_by_guest(cause), cause);
1779 notifier_list_notify(&shutdown_notifiers, &cause);
1782 void qemu_system_powerdown_request(void)
1784 trace_qemu_system_powerdown_request();
1785 powerdown_requested = 1;
1786 qemu_notify_event();
1789 void qemu_register_powerdown_notifier(Notifier *notifier)
1791 notifier_list_add(&powerdown_notifiers, notifier);
1794 void qemu_register_shutdown_notifier(Notifier *notifier)
1796 notifier_list_add(&shutdown_notifiers, notifier);
1799 void qemu_system_debug_request(void)
1801 debug_requested = 1;
1802 qemu_notify_event();
1805 static bool main_loop_should_exit(void)
1807 RunState r;
1808 ShutdownCause request;
1810 if (preconfig_exit_requested) {
1811 if (runstate_check(RUN_STATE_PRECONFIG)) {
1812 runstate_set(RUN_STATE_PRELAUNCH);
1814 preconfig_exit_requested = false;
1815 return true;
1817 if (qemu_debug_requested()) {
1818 vm_stop(RUN_STATE_DEBUG);
1820 if (qemu_suspend_requested()) {
1821 qemu_system_suspend();
1823 request = qemu_shutdown_requested();
1824 if (request) {
1825 qemu_kill_report();
1826 qemu_system_shutdown(request);
1827 if (no_shutdown) {
1828 vm_stop(RUN_STATE_SHUTDOWN);
1829 } else {
1830 return true;
1833 request = qemu_reset_requested();
1834 if (request) {
1835 pause_all_vcpus();
1836 qemu_system_reset(request);
1837 resume_all_vcpus();
1838 if (!runstate_check(RUN_STATE_RUNNING) &&
1839 !runstate_check(RUN_STATE_INMIGRATE)) {
1840 runstate_set(RUN_STATE_PRELAUNCH);
1843 if (qemu_wakeup_requested()) {
1844 pause_all_vcpus();
1845 qemu_system_reset(SHUTDOWN_CAUSE_NONE);
1846 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
1847 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
1848 resume_all_vcpus();
1849 qapi_event_send_wakeup();
1851 if (qemu_powerdown_requested()) {
1852 qemu_system_powerdown();
1854 if (qemu_vmstop_requested(&r)) {
1855 vm_stop(r);
1857 return false;
1860 static void main_loop(void)
1862 #ifdef CONFIG_PROFILER
1863 int64_t ti;
1864 #endif
1865 while (!main_loop_should_exit()) {
1866 #ifdef CONFIG_PROFILER
1867 ti = profile_getclock();
1868 #endif
1869 main_loop_wait(false);
1870 #ifdef CONFIG_PROFILER
1871 dev_time += profile_getclock() - ti;
1872 #endif
1876 static void version(void)
1878 printf("QEMU emulator version " QEMU_FULL_VERSION "\n"
1879 QEMU_COPYRIGHT "\n");
1882 static void help(int exitcode)
1884 version();
1885 printf("usage: %s [options] [disk_image]\n\n"
1886 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1887 error_get_progname());
1889 #define QEMU_OPTIONS_GENERATE_HELP
1890 #include "qemu-options-wrapper.h"
1892 printf("\nDuring emulation, the following keys are useful:\n"
1893 "ctrl-alt-f toggle full screen\n"
1894 "ctrl-alt-n switch to virtual console 'n'\n"
1895 "ctrl-alt toggle mouse and keyboard grab\n"
1896 "\n"
1897 "When using -nographic, press 'ctrl-a h' to get some help.\n"
1898 "\n"
1899 QEMU_HELP_BOTTOM "\n");
1901 exit(exitcode);
1904 #define HAS_ARG 0x0001
1906 typedef struct QEMUOption {
1907 const char *name;
1908 int flags;
1909 int index;
1910 uint32_t arch_mask;
1911 } QEMUOption;
1913 static const QEMUOption qemu_options[] = {
1914 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1915 #define QEMU_OPTIONS_GENERATE_OPTIONS
1916 #include "qemu-options-wrapper.h"
1917 { NULL },
1920 typedef struct VGAInterfaceInfo {
1921 const char *opt_name; /* option name */
1922 const char *name; /* human-readable name */
1923 /* Class names indicating that support is available.
1924 * If no class is specified, the interface is always available */
1925 const char *class_names[2];
1926 } VGAInterfaceInfo;
1928 static const VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
1929 [VGA_NONE] = {
1930 .opt_name = "none",
1931 .name = "no graphic card",
1933 [VGA_STD] = {
1934 .opt_name = "std",
1935 .name = "standard VGA",
1936 .class_names = { "VGA", "isa-vga" },
1938 [VGA_CIRRUS] = {
1939 .opt_name = "cirrus",
1940 .name = "Cirrus VGA",
1941 .class_names = { "cirrus-vga", "isa-cirrus-vga" },
1943 [VGA_VMWARE] = {
1944 .opt_name = "vmware",
1945 .name = "VMWare SVGA",
1946 .class_names = { "vmware-svga" },
1948 [VGA_VIRTIO] = {
1949 .opt_name = "virtio",
1950 .name = "Virtio VGA",
1951 .class_names = { "virtio-vga" },
1953 [VGA_QXL] = {
1954 .opt_name = "qxl",
1955 .name = "QXL VGA",
1956 .class_names = { "qxl-vga" },
1958 [VGA_TCX] = {
1959 .opt_name = "tcx",
1960 .name = "TCX framebuffer",
1961 .class_names = { "SUNW,tcx" },
1963 [VGA_CG3] = {
1964 .opt_name = "cg3",
1965 .name = "CG3 framebuffer",
1966 .class_names = { "cgthree" },
1968 [VGA_XENFB] = {
1969 .opt_name = "xenfb",
1970 .name = "Xen paravirtualized framebuffer",
1974 static bool vga_interface_available(VGAInterfaceType t)
1976 const VGAInterfaceInfo *ti = &vga_interfaces[t];
1978 assert(t < VGA_TYPE_MAX);
1979 return !ti->class_names[0] ||
1980 object_class_by_name(ti->class_names[0]) ||
1981 object_class_by_name(ti->class_names[1]);
1984 static const char *
1985 get_default_vga_model(const MachineClass *machine_class)
1987 if (machine_class->default_display) {
1988 return machine_class->default_display;
1989 } else if (vga_interface_available(VGA_CIRRUS)) {
1990 return "cirrus";
1991 } else if (vga_interface_available(VGA_STD)) {
1992 return "std";
1995 return NULL;
1998 static void select_vgahw(const MachineClass *machine_class, const char *p)
2000 const char *opts;
2001 int t;
2003 if (g_str_equal(p, "help")) {
2004 const char *def = get_default_vga_model(machine_class);
2006 for (t = 0; t < VGA_TYPE_MAX; t++) {
2007 const VGAInterfaceInfo *ti = &vga_interfaces[t];
2009 if (vga_interface_available(t) && ti->opt_name) {
2010 printf("%-20s %s%s\n", ti->opt_name, ti->name ?: "",
2011 g_str_equal(ti->opt_name, def) ? " (default)" : "");
2014 exit(0);
2017 assert(vga_interface_type == VGA_NONE);
2018 for (t = 0; t < VGA_TYPE_MAX; t++) {
2019 const VGAInterfaceInfo *ti = &vga_interfaces[t];
2020 if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
2021 if (!vga_interface_available(t)) {
2022 error_report("%s not available", ti->name);
2023 exit(1);
2025 vga_interface_type = t;
2026 break;
2029 if (t == VGA_TYPE_MAX) {
2030 invalid_vga:
2031 error_report("unknown vga type: %s", p);
2032 exit(1);
2034 while (*opts) {
2035 const char *nextopt;
2037 if (strstart(opts, ",retrace=", &nextopt)) {
2038 opts = nextopt;
2039 if (strstart(opts, "dumb", &nextopt))
2040 vga_retrace_method = VGA_RETRACE_DUMB;
2041 else if (strstart(opts, "precise", &nextopt))
2042 vga_retrace_method = VGA_RETRACE_PRECISE;
2043 else goto invalid_vga;
2044 } else goto invalid_vga;
2045 opts = nextopt;
2049 static void parse_display_qapi(const char *optarg)
2051 DisplayOptions *opts;
2052 Visitor *v;
2054 v = qobject_input_visitor_new_str(optarg, "type", &error_fatal);
2056 visit_type_DisplayOptions(v, NULL, &opts, &error_fatal);
2057 QAPI_CLONE_MEMBERS(DisplayOptions, &dpy, opts);
2059 qapi_free_DisplayOptions(opts);
2060 visit_free(v);
2063 DisplayOptions *qmp_query_display_options(Error **errp)
2065 return QAPI_CLONE(DisplayOptions, &dpy);
2068 static void parse_display(const char *p)
2070 const char *opts;
2072 if (strstart(p, "sdl", &opts)) {
2074 * sdl DisplayType needs hand-crafted parser instead of
2075 * parse_display_qapi() due to some options not in
2076 * DisplayOptions, specifically:
2077 * - frame
2078 * Already deprecated.
2079 * - ctrl_grab + alt_grab
2080 * Not clear yet what happens to them long-term. Should
2081 * replaced by something better or deprecated and dropped.
2083 dpy.type = DISPLAY_TYPE_SDL;
2084 while (*opts) {
2085 const char *nextopt;
2087 if (strstart(opts, ",alt_grab=", &nextopt)) {
2088 opts = nextopt;
2089 if (strstart(opts, "on", &nextopt)) {
2090 alt_grab = 1;
2091 } else if (strstart(opts, "off", &nextopt)) {
2092 alt_grab = 0;
2093 } else {
2094 goto invalid_sdl_args;
2096 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2097 opts = nextopt;
2098 if (strstart(opts, "on", &nextopt)) {
2099 ctrl_grab = 1;
2100 } else if (strstart(opts, "off", &nextopt)) {
2101 ctrl_grab = 0;
2102 } else {
2103 goto invalid_sdl_args;
2105 } else if (strstart(opts, ",window_close=", &nextopt)) {
2106 opts = nextopt;
2107 dpy.has_window_close = true;
2108 if (strstart(opts, "on", &nextopt)) {
2109 dpy.window_close = true;
2110 } else if (strstart(opts, "off", &nextopt)) {
2111 dpy.window_close = false;
2112 } else {
2113 goto invalid_sdl_args;
2115 } else if (strstart(opts, ",gl=", &nextopt)) {
2116 opts = nextopt;
2117 dpy.has_gl = true;
2118 if (strstart(opts, "on", &nextopt)) {
2119 dpy.gl = DISPLAYGL_MODE_ON;
2120 } else if (strstart(opts, "core", &nextopt)) {
2121 dpy.gl = DISPLAYGL_MODE_CORE;
2122 } else if (strstart(opts, "es", &nextopt)) {
2123 dpy.gl = DISPLAYGL_MODE_ES;
2124 } else if (strstart(opts, "off", &nextopt)) {
2125 dpy.gl = DISPLAYGL_MODE_OFF;
2126 } else {
2127 goto invalid_sdl_args;
2129 } else {
2130 invalid_sdl_args:
2131 error_report("invalid SDL option string");
2132 exit(1);
2134 opts = nextopt;
2136 } else if (strstart(p, "vnc", &opts)) {
2138 * vnc isn't a (local) DisplayType but a protocol for remote
2139 * display access.
2141 if (*opts == '=') {
2142 vnc_parse(opts + 1, &error_fatal);
2143 } else {
2144 error_report("VNC requires a display argument vnc=<display>");
2145 exit(1);
2147 } else {
2148 parse_display_qapi(p);
2152 char *qemu_find_file(int type, const char *name)
2154 int i;
2155 const char *subdir;
2156 char *buf;
2158 /* Try the name as a straight path first */
2159 if (access(name, R_OK) == 0) {
2160 trace_load_file(name, name);
2161 return g_strdup(name);
2164 switch (type) {
2165 case QEMU_FILE_TYPE_BIOS:
2166 subdir = "";
2167 break;
2168 case QEMU_FILE_TYPE_KEYMAP:
2169 subdir = "keymaps/";
2170 break;
2171 default:
2172 abort();
2175 for (i = 0; i < data_dir_idx; i++) {
2176 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2177 if (access(buf, R_OK) == 0) {
2178 trace_load_file(name, buf);
2179 return buf;
2181 g_free(buf);
2183 return NULL;
2186 static void qemu_add_data_dir(const char *path)
2188 int i;
2190 if (path == NULL) {
2191 return;
2193 if (data_dir_idx == ARRAY_SIZE(data_dir)) {
2194 return;
2196 for (i = 0; i < data_dir_idx; i++) {
2197 if (strcmp(data_dir[i], path) == 0) {
2198 return; /* duplicate */
2201 data_dir[data_dir_idx++] = g_strdup(path);
2204 static inline bool nonempty_str(const char *str)
2206 return str && *str;
2209 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
2211 gchar *buf;
2212 size_t size;
2213 const char *name, *file, *str;
2214 FWCfgState *fw_cfg = (FWCfgState *) opaque;
2216 if (fw_cfg == NULL) {
2217 error_setg(errp, "fw_cfg device not available");
2218 return -1;
2220 name = qemu_opt_get(opts, "name");
2221 file = qemu_opt_get(opts, "file");
2222 str = qemu_opt_get(opts, "string");
2224 /* we need name and either a file or the content string */
2225 if (!(nonempty_str(name) && (nonempty_str(file) || nonempty_str(str)))) {
2226 error_setg(errp, "invalid argument(s)");
2227 return -1;
2229 if (nonempty_str(file) && nonempty_str(str)) {
2230 error_setg(errp, "file and string are mutually exclusive");
2231 return -1;
2233 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
2234 error_setg(errp, "name too long (max. %d char)",
2235 FW_CFG_MAX_FILE_PATH - 1);
2236 return -1;
2238 if (strncmp(name, "opt/", 4) != 0) {
2239 warn_report("externally provided fw_cfg item names "
2240 "should be prefixed with \"opt/\"");
2242 if (nonempty_str(str)) {
2243 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
2244 buf = g_memdup(str, size);
2245 } else {
2246 GError *err = NULL;
2247 if (!g_file_get_contents(file, &buf, &size, &err)) {
2248 error_setg(errp, "can't load %s: %s", file, err->message);
2249 g_error_free(err);
2250 return -1;
2253 /* For legacy, keep user files in a specific global order. */
2254 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
2255 fw_cfg_add_file(fw_cfg, name, buf, size);
2256 fw_cfg_reset_order_override(fw_cfg);
2257 return 0;
2260 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
2262 return qdev_device_help(opts);
2265 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
2267 DeviceState *dev;
2269 dev = qdev_device_add(opts, errp);
2270 if (!dev) {
2271 return -1;
2273 object_unref(OBJECT(dev));
2274 return 0;
2277 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2279 Error *local_err = NULL;
2281 if (!qemu_chr_new_from_opts(opts, NULL, &local_err)) {
2282 if (local_err) {
2283 error_propagate(errp, local_err);
2284 return -1;
2286 exit(0);
2288 return 0;
2291 #ifdef CONFIG_VIRTFS
2292 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2294 return qemu_fsdev_add(opts, errp);
2296 #endif
2298 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
2300 Chardev *chr;
2301 const char *chardev;
2302 const char *mode;
2303 int flags;
2305 mode = qemu_opt_get(opts, "mode");
2306 if (mode == NULL) {
2307 mode = "readline";
2309 if (strcmp(mode, "readline") == 0) {
2310 flags = MONITOR_USE_READLINE;
2311 } else if (strcmp(mode, "control") == 0) {
2312 flags = MONITOR_USE_CONTROL;
2313 } else {
2314 error_setg(errp, "unknown monitor mode \"%s\"", mode);
2315 return -1;
2318 if (qemu_opt_get_bool(opts, "pretty", 0))
2319 flags |= MONITOR_USE_PRETTY;
2321 chardev = qemu_opt_get(opts, "chardev");
2322 if (!chardev) {
2323 error_report("chardev is required");
2324 exit(1);
2326 chr = qemu_chr_find(chardev);
2327 if (chr == NULL) {
2328 error_setg(errp, "chardev \"%s\" not found", chardev);
2329 return -1;
2332 monitor_init(chr, flags);
2333 return 0;
2336 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
2338 static int monitor_device_index = 0;
2339 QemuOpts *opts;
2340 const char *p;
2341 char label[32];
2343 if (strstart(optarg, "chardev:", &p)) {
2344 snprintf(label, sizeof(label), "%s", p);
2345 } else {
2346 snprintf(label, sizeof(label), "compat_monitor%d",
2347 monitor_device_index);
2348 opts = qemu_chr_parse_compat(label, optarg, true);
2349 if (!opts) {
2350 error_report("parse error: %s", optarg);
2351 exit(1);
2355 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
2356 qemu_opt_set(opts, "mode", mode, &error_abort);
2357 qemu_opt_set(opts, "chardev", label, &error_abort);
2358 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
2359 monitor_device_index++;
2362 struct device_config {
2363 enum {
2364 DEV_USB, /* -usbdevice */
2365 DEV_BT, /* -bt */
2366 DEV_SERIAL, /* -serial */
2367 DEV_PARALLEL, /* -parallel */
2368 DEV_DEBUGCON, /* -debugcon */
2369 DEV_GDB, /* -gdb, -s */
2370 DEV_SCLP, /* s390 sclp */
2371 } type;
2372 const char *cmdline;
2373 Location loc;
2374 QTAILQ_ENTRY(device_config) next;
2377 static QTAILQ_HEAD(, device_config) device_configs =
2378 QTAILQ_HEAD_INITIALIZER(device_configs);
2380 static void add_device_config(int type, const char *cmdline)
2382 struct device_config *conf;
2384 conf = g_malloc0(sizeof(*conf));
2385 conf->type = type;
2386 conf->cmdline = cmdline;
2387 loc_save(&conf->loc);
2388 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2391 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2393 struct device_config *conf;
2394 int rc;
2396 QTAILQ_FOREACH(conf, &device_configs, next) {
2397 if (conf->type != type)
2398 continue;
2399 loc_push_restore(&conf->loc);
2400 rc = func(conf->cmdline);
2401 loc_pop(&conf->loc);
2402 if (rc) {
2403 return rc;
2406 return 0;
2409 static int serial_parse(const char *devname)
2411 int index = num_serial_hds;
2412 char label[32];
2414 if (strcmp(devname, "none") == 0)
2415 return 0;
2416 snprintf(label, sizeof(label), "serial%d", index);
2417 serial_hds = g_renew(Chardev *, serial_hds, index + 1);
2419 serial_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
2420 if (!serial_hds[index]) {
2421 error_report("could not connect serial device"
2422 " to character backend '%s'", devname);
2423 return -1;
2425 num_serial_hds++;
2426 return 0;
2429 Chardev *serial_hd(int i)
2431 assert(i >= 0);
2432 if (i < num_serial_hds) {
2433 return serial_hds[i];
2435 return NULL;
2438 int serial_max_hds(void)
2440 return num_serial_hds;
2443 static int parallel_parse(const char *devname)
2445 static int index = 0;
2446 char label[32];
2448 if (strcmp(devname, "none") == 0)
2449 return 0;
2450 if (index == MAX_PARALLEL_PORTS) {
2451 error_report("too many parallel ports");
2452 exit(1);
2454 snprintf(label, sizeof(label), "parallel%d", index);
2455 parallel_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
2456 if (!parallel_hds[index]) {
2457 error_report("could not connect parallel device"
2458 " to character backend '%s'", devname);
2459 return -1;
2461 index++;
2462 return 0;
2465 static int debugcon_parse(const char *devname)
2467 QemuOpts *opts;
2469 if (!qemu_chr_new_mux_mon("debugcon", devname, NULL)) {
2470 error_report("invalid character backend '%s'", devname);
2471 exit(1);
2473 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2474 if (!opts) {
2475 error_report("already have a debugcon device");
2476 exit(1);
2478 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
2479 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
2480 return 0;
2483 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
2485 const MachineClass *mc1 = a, *mc2 = b;
2486 int res;
2488 if (mc1->family == NULL) {
2489 if (mc2->family == NULL) {
2490 /* Compare standalone machine types against each other; they sort
2491 * in increasing order.
2493 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
2494 object_class_get_name(OBJECT_CLASS(mc2)));
2497 /* Standalone machine types sort after families. */
2498 return 1;
2501 if (mc2->family == NULL) {
2502 /* Families sort before standalone machine types. */
2503 return -1;
2506 /* Families sort between each other alphabetically increasingly. */
2507 res = strcmp(mc1->family, mc2->family);
2508 if (res != 0) {
2509 return res;
2512 /* Within the same family, machine types sort in decreasing order. */
2513 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
2514 object_class_get_name(OBJECT_CLASS(mc1)));
2517 static MachineClass *machine_parse(const char *name, GSList *machines)
2519 MachineClass *mc;
2520 GSList *el;
2522 if (is_help_option(name)) {
2523 printf("Supported machines are:\n");
2524 machines = g_slist_sort(machines, machine_class_cmp);
2525 for (el = machines; el; el = el->next) {
2526 MachineClass *mc = el->data;
2527 if (mc->alias) {
2528 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
2530 printf("%-20s %s%s%s\n", mc->name, mc->desc,
2531 mc->is_default ? " (default)" : "",
2532 mc->deprecation_reason ? " (deprecated)" : "");
2534 exit(0);
2537 mc = find_machine(name, machines);
2538 if (!mc) {
2539 error_report("unsupported machine type");
2540 error_printf("Use -machine help to list supported machines\n");
2541 exit(1);
2543 return mc;
2546 void qemu_add_exit_notifier(Notifier *notify)
2548 notifier_list_add(&exit_notifiers, notify);
2551 void qemu_remove_exit_notifier(Notifier *notify)
2553 notifier_remove(notify);
2556 static void qemu_run_exit_notifiers(void)
2558 notifier_list_notify(&exit_notifiers, NULL);
2561 static const char *pid_file;
2562 static Notifier qemu_unlink_pidfile_notifier;
2564 static void qemu_unlink_pidfile(Notifier *n, void *data)
2566 if (pid_file) {
2567 unlink(pid_file);
2571 bool machine_init_done;
2573 void qemu_add_machine_init_done_notifier(Notifier *notify)
2575 notifier_list_add(&machine_init_done_notifiers, notify);
2576 if (machine_init_done) {
2577 notify->notify(notify, NULL);
2581 void qemu_remove_machine_init_done_notifier(Notifier *notify)
2583 notifier_remove(notify);
2586 static void qemu_run_machine_init_done_notifiers(void)
2588 machine_init_done = true;
2589 notifier_list_notify(&machine_init_done_notifiers, NULL);
2592 static const QEMUOption *lookup_opt(int argc, char **argv,
2593 const char **poptarg, int *poptind)
2595 const QEMUOption *popt;
2596 int optind = *poptind;
2597 char *r = argv[optind];
2598 const char *optarg;
2600 loc_set_cmdline(argv, optind, 1);
2601 optind++;
2602 /* Treat --foo the same as -foo. */
2603 if (r[1] == '-')
2604 r++;
2605 popt = qemu_options;
2606 for(;;) {
2607 if (!popt->name) {
2608 error_report("invalid option");
2609 exit(1);
2611 if (!strcmp(popt->name, r + 1))
2612 break;
2613 popt++;
2615 if (popt->flags & HAS_ARG) {
2616 if (optind >= argc) {
2617 error_report("requires an argument");
2618 exit(1);
2620 optarg = argv[optind++];
2621 loc_set_cmdline(argv, optind - 2, 2);
2622 } else {
2623 optarg = NULL;
2626 *poptarg = optarg;
2627 *poptind = optind;
2629 return popt;
2632 static MachineClass *select_machine(void)
2634 GSList *machines = object_class_get_list(TYPE_MACHINE, false);
2635 MachineClass *machine_class = find_default_machine(machines);
2636 const char *optarg;
2637 QemuOpts *opts;
2638 Location loc;
2640 loc_push_none(&loc);
2642 opts = qemu_get_machine_opts();
2643 qemu_opts_loc_restore(opts);
2645 optarg = qemu_opt_get(opts, "type");
2646 if (optarg) {
2647 machine_class = machine_parse(optarg, machines);
2650 if (!machine_class) {
2651 error_report("No machine specified, and there is no default");
2652 error_printf("Use -machine help to list supported machines\n");
2653 exit(1);
2656 loc_pop(&loc);
2657 g_slist_free(machines);
2658 return machine_class;
2661 static int machine_set_property(void *opaque,
2662 const char *name, const char *value,
2663 Error **errp)
2665 Object *obj = OBJECT(opaque);
2666 Error *local_err = NULL;
2667 char *p, *qom_name;
2669 if (strcmp(name, "type") == 0) {
2670 return 0;
2673 qom_name = g_strdup(name);
2674 for (p = qom_name; *p; p++) {
2675 if (*p == '_') {
2676 *p = '-';
2680 object_property_parse(obj, value, qom_name, &local_err);
2681 g_free(qom_name);
2683 if (local_err) {
2684 error_propagate(errp, local_err);
2685 return -1;
2688 return 0;
2693 * Initial object creation happens before all other
2694 * QEMU data types are created. The majority of objects
2695 * can be created at this point. The rng-egd object
2696 * cannot be created here, as it depends on the chardev
2697 * already existing.
2699 static bool object_create_initial(const char *type, QemuOpts *opts)
2701 ObjectClass *klass;
2703 if (is_help_option(type)) {
2704 GSList *l, *list;
2706 printf("List of user creatable objects:\n");
2707 list = object_class_get_list_sorted(TYPE_USER_CREATABLE, false);
2708 for (l = list; l != NULL; l = l->next) {
2709 ObjectClass *oc = OBJECT_CLASS(l->data);
2710 printf(" %s\n", object_class_get_name(oc));
2712 g_slist_free(list);
2713 exit(0);
2716 klass = object_class_by_name(type);
2717 if (klass && qemu_opt_has_help_opt(opts)) {
2718 ObjectPropertyIterator iter;
2719 ObjectProperty *prop;
2720 GPtrArray *array = g_ptr_array_new();
2721 int i;
2723 object_class_property_iter_init(&iter, klass);
2724 while ((prop = object_property_iter_next(&iter))) {
2725 GString *str;
2727 if (!prop->set) {
2728 continue;
2731 str = g_string_new(NULL);
2732 g_string_append_printf(str, " %s=<%s>", prop->name, prop->type);
2733 if (prop->description) {
2734 if (str->len < 24) {
2735 g_string_append_printf(str, "%*s", 24 - (int)str->len, "");
2737 g_string_append_printf(str, " - %s", prop->description);
2739 g_ptr_array_add(array, g_string_free(str, false));
2741 g_ptr_array_sort(array, (GCompareFunc)qemu_pstrcmp0);
2742 if (array->len > 0) {
2743 printf("%s options:\n", type);
2744 } else {
2745 printf("There are no options for %s.\n", type);
2747 for (i = 0; i < array->len; i++) {
2748 printf("%s\n", (char *)array->pdata[i]);
2750 g_ptr_array_set_free_func(array, g_free);
2751 g_ptr_array_free(array, true);
2752 exit(0);
2756 * Objects should not be made "delayed" without a reason. If you
2757 * add one, state the reason in a comment!
2760 /* Reason: rng-egd property "chardev" */
2761 if (g_str_equal(type, "rng-egd")) {
2762 return false;
2765 #if defined(CONFIG_VHOST_USER) && defined(CONFIG_LINUX)
2766 /* Reason: cryptodev-vhost-user property "chardev" */
2767 if (g_str_equal(type, "cryptodev-vhost-user")) {
2768 return false;
2770 #endif
2773 * Reason: filter-* property "netdev" etc.
2775 if (g_str_equal(type, "filter-buffer") ||
2776 g_str_equal(type, "filter-dump") ||
2777 g_str_equal(type, "filter-mirror") ||
2778 g_str_equal(type, "filter-redirector") ||
2779 g_str_equal(type, "colo-compare") ||
2780 g_str_equal(type, "filter-rewriter") ||
2781 g_str_equal(type, "filter-replay")) {
2782 return false;
2785 /* Memory allocation by backends needs to be done
2786 * after configure_accelerator() (due to the tcg_enabled()
2787 * checks at memory_region_init_*()).
2789 * Also, allocation of large amounts of memory may delay
2790 * chardev initialization for too long, and trigger timeouts
2791 * on software that waits for a monitor socket to be created
2792 * (e.g. libvirt).
2794 if (g_str_has_prefix(type, "memory-backend-")) {
2795 return false;
2798 return true;
2803 * The remainder of object creation happens after the
2804 * creation of chardev, fsdev, net clients and device data types.
2806 static bool object_create_delayed(const char *type, QemuOpts *opts)
2808 return !object_create_initial(type, opts);
2812 static void set_memory_options(uint64_t *ram_slots, ram_addr_t *maxram_size,
2813 MachineClass *mc)
2815 uint64_t sz;
2816 const char *mem_str;
2817 const ram_addr_t default_ram_size = mc->default_ram_size;
2818 QemuOpts *opts = qemu_find_opts_singleton("memory");
2819 Location loc;
2821 loc_push_none(&loc);
2822 qemu_opts_loc_restore(opts);
2824 sz = 0;
2825 mem_str = qemu_opt_get(opts, "size");
2826 if (mem_str) {
2827 if (!*mem_str) {
2828 error_report("missing 'size' option value");
2829 exit(EXIT_FAILURE);
2832 sz = qemu_opt_get_size(opts, "size", ram_size);
2834 /* Fix up legacy suffix-less format */
2835 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2836 uint64_t overflow_check = sz;
2838 sz *= MiB;
2839 if (sz / MiB != overflow_check) {
2840 error_report("too large 'size' option value");
2841 exit(EXIT_FAILURE);
2846 /* backward compatibility behaviour for case "-m 0" */
2847 if (sz == 0) {
2848 sz = default_ram_size;
2851 sz = QEMU_ALIGN_UP(sz, 8192);
2852 ram_size = sz;
2853 if (ram_size != sz) {
2854 error_report("ram size too large");
2855 exit(EXIT_FAILURE);
2858 /* store value for the future use */
2859 qemu_opt_set_number(opts, "size", ram_size, &error_abort);
2860 *maxram_size = ram_size;
2862 if (qemu_opt_get(opts, "maxmem")) {
2863 uint64_t slots;
2865 sz = qemu_opt_get_size(opts, "maxmem", 0);
2866 slots = qemu_opt_get_number(opts, "slots", 0);
2867 if (sz < ram_size) {
2868 error_report("invalid value of -m option maxmem: "
2869 "maximum memory size (0x%" PRIx64 ") must be at least "
2870 "the initial memory size (0x" RAM_ADDR_FMT ")",
2871 sz, ram_size);
2872 exit(EXIT_FAILURE);
2873 } else if (slots && sz == ram_size) {
2874 error_report("invalid value of -m option maxmem: "
2875 "memory slots were specified but maximum memory size "
2876 "(0x%" PRIx64 ") is equal to the initial memory size "
2877 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
2878 exit(EXIT_FAILURE);
2881 *maxram_size = sz;
2882 *ram_slots = slots;
2883 } else if (qemu_opt_get(opts, "slots")) {
2884 error_report("invalid -m option value: missing 'maxmem' option");
2885 exit(EXIT_FAILURE);
2888 loc_pop(&loc);
2891 static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
2893 GlobalProperty *g;
2895 g = g_malloc0(sizeof(*g));
2896 g->driver = qemu_opt_get(opts, "driver");
2897 g->property = qemu_opt_get(opts, "property");
2898 g->value = qemu_opt_get(opts, "value");
2899 qdev_prop_register_global(g);
2900 return 0;
2903 static int qemu_read_default_config_file(void)
2905 int ret;
2907 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
2908 if (ret < 0 && ret != -ENOENT) {
2909 return ret;
2912 return 0;
2915 static void user_register_global_props(void)
2917 qemu_opts_foreach(qemu_find_opts("global"),
2918 global_init_func, NULL, NULL);
2921 int main(int argc, char **argv, char **envp)
2923 int i;
2924 int snapshot, linux_boot;
2925 const char *initrd_filename;
2926 const char *kernel_filename, *kernel_cmdline;
2927 const char *boot_order = NULL;
2928 const char *boot_once = NULL;
2929 DisplayState *ds;
2930 QemuOpts *opts, *machine_opts;
2931 QemuOpts *icount_opts = NULL, *accel_opts = NULL;
2932 QemuOptsList *olist;
2933 int optind;
2934 const char *optarg;
2935 const char *loadvm = NULL;
2936 MachineClass *machine_class;
2937 const char *cpu_option;
2938 const char *vga_model = NULL;
2939 const char *qtest_chrdev = NULL;
2940 const char *qtest_log = NULL;
2941 const char *incoming = NULL;
2942 bool userconfig = true;
2943 bool nographic = false;
2944 int display_remote = 0;
2945 const char *log_mask = NULL;
2946 const char *log_file = NULL;
2947 char *trace_file = NULL;
2948 ram_addr_t maxram_size;
2949 uint64_t ram_slots = 0;
2950 FILE *vmstate_dump_file = NULL;
2951 Error *main_loop_err = NULL;
2952 Error *err = NULL;
2953 bool list_data_dirs = false;
2954 char *dir, **dirs;
2955 BlockdevOptionsQueue bdo_queue = QSIMPLEQ_HEAD_INITIALIZER(bdo_queue);
2957 error_init(argv[0]);
2958 module_call_init(MODULE_INIT_TRACE);
2960 qemu_init_cpu_list();
2961 qemu_init_cpu_loop();
2963 qemu_mutex_lock_iothread();
2965 atexit(qemu_run_exit_notifiers);
2966 qemu_init_exec_dir(argv[0]);
2968 module_call_init(MODULE_INIT_QOM);
2970 qemu_add_opts(&qemu_drive_opts);
2971 qemu_add_drive_opts(&qemu_legacy_drive_opts);
2972 qemu_add_drive_opts(&qemu_common_drive_opts);
2973 qemu_add_drive_opts(&qemu_drive_opts);
2974 qemu_add_drive_opts(&bdrv_runtime_opts);
2975 qemu_add_opts(&qemu_chardev_opts);
2976 qemu_add_opts(&qemu_device_opts);
2977 qemu_add_opts(&qemu_netdev_opts);
2978 qemu_add_opts(&qemu_nic_opts);
2979 qemu_add_opts(&qemu_net_opts);
2980 qemu_add_opts(&qemu_rtc_opts);
2981 qemu_add_opts(&qemu_global_opts);
2982 qemu_add_opts(&qemu_mon_opts);
2983 qemu_add_opts(&qemu_trace_opts);
2984 qemu_add_opts(&qemu_option_rom_opts);
2985 qemu_add_opts(&qemu_machine_opts);
2986 qemu_add_opts(&qemu_accel_opts);
2987 qemu_add_opts(&qemu_mem_opts);
2988 qemu_add_opts(&qemu_smp_opts);
2989 qemu_add_opts(&qemu_boot_opts);
2990 qemu_add_opts(&qemu_add_fd_opts);
2991 qemu_add_opts(&qemu_object_opts);
2992 qemu_add_opts(&qemu_tpmdev_opts);
2993 qemu_add_opts(&qemu_realtime_opts);
2994 qemu_add_opts(&qemu_overcommit_opts);
2995 qemu_add_opts(&qemu_msg_opts);
2996 qemu_add_opts(&qemu_name_opts);
2997 qemu_add_opts(&qemu_numa_opts);
2998 qemu_add_opts(&qemu_icount_opts);
2999 qemu_add_opts(&qemu_semihosting_config_opts);
3000 qemu_add_opts(&qemu_fw_cfg_opts);
3001 module_call_init(MODULE_INIT_OPTS);
3003 runstate_init();
3004 precopy_infrastructure_init();
3005 postcopy_infrastructure_init();
3006 monitor_init_globals();
3008 if (qcrypto_init(&err) < 0) {
3009 error_reportf_err(err, "cannot initialize crypto: ");
3010 exit(1);
3013 QLIST_INIT (&vm_change_state_head);
3014 os_setup_early_signal_handling();
3016 cpu_option = NULL;
3017 snapshot = 0;
3019 nb_nics = 0;
3021 bdrv_init_with_whitelist();
3023 autostart = 1;
3025 /* first pass of option parsing */
3026 optind = 1;
3027 while (optind < argc) {
3028 if (argv[optind][0] != '-') {
3029 /* disk image */
3030 optind++;
3031 } else {
3032 const QEMUOption *popt;
3034 popt = lookup_opt(argc, argv, &optarg, &optind);
3035 switch (popt->index) {
3036 case QEMU_OPTION_nouserconfig:
3037 userconfig = false;
3038 break;
3043 if (userconfig) {
3044 if (qemu_read_default_config_file() < 0) {
3045 exit(1);
3049 /* second pass of option parsing */
3050 optind = 1;
3051 for(;;) {
3052 if (optind >= argc)
3053 break;
3054 if (argv[optind][0] != '-') {
3055 loc_set_cmdline(argv, optind, 1);
3056 drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
3057 } else {
3058 const QEMUOption *popt;
3060 popt = lookup_opt(argc, argv, &optarg, &optind);
3061 if (!(popt->arch_mask & arch_type)) {
3062 error_report("Option not supported for this target");
3063 exit(1);
3065 switch(popt->index) {
3066 case QEMU_OPTION_cpu:
3067 /* hw initialization will check this */
3068 cpu_option = optarg;
3069 break;
3070 case QEMU_OPTION_hda:
3071 case QEMU_OPTION_hdb:
3072 case QEMU_OPTION_hdc:
3073 case QEMU_OPTION_hdd:
3074 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
3075 HD_OPTS);
3076 break;
3077 case QEMU_OPTION_blockdev:
3079 Visitor *v;
3080 BlockdevOptionsQueueEntry *bdo;
3082 v = qobject_input_visitor_new_str(optarg, "driver",
3083 &error_fatal);
3085 bdo = g_new(BlockdevOptionsQueueEntry, 1);
3086 visit_type_BlockdevOptions(v, NULL, &bdo->bdo,
3087 &error_fatal);
3088 visit_free(v);
3089 loc_save(&bdo->loc);
3090 QSIMPLEQ_INSERT_TAIL(&bdo_queue, bdo, entry);
3091 break;
3093 case QEMU_OPTION_drive:
3094 if (drive_def(optarg) == NULL) {
3095 exit(1);
3097 break;
3098 case QEMU_OPTION_set:
3099 if (qemu_set_option(optarg) != 0)
3100 exit(1);
3101 break;
3102 case QEMU_OPTION_global:
3103 if (qemu_global_option(optarg) != 0)
3104 exit(1);
3105 break;
3106 case QEMU_OPTION_mtdblock:
3107 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3108 break;
3109 case QEMU_OPTION_sd:
3110 drive_add(IF_SD, -1, optarg, SD_OPTS);
3111 break;
3112 case QEMU_OPTION_pflash:
3113 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
3114 break;
3115 case QEMU_OPTION_snapshot:
3116 snapshot = 1;
3117 break;
3118 case QEMU_OPTION_numa:
3119 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
3120 optarg, true);
3121 if (!opts) {
3122 exit(1);
3124 break;
3125 case QEMU_OPTION_display:
3126 parse_display(optarg);
3127 break;
3128 case QEMU_OPTION_nographic:
3129 olist = qemu_find_opts("machine");
3130 qemu_opts_parse_noisily(olist, "graphics=off", false);
3131 nographic = true;
3132 dpy.type = DISPLAY_TYPE_NONE;
3133 break;
3134 case QEMU_OPTION_curses:
3135 #ifdef CONFIG_CURSES
3136 dpy.type = DISPLAY_TYPE_CURSES;
3137 #else
3138 error_report("curses or iconv support is disabled");
3139 exit(1);
3140 #endif
3141 break;
3142 case QEMU_OPTION_portrait:
3143 graphic_rotate = 90;
3144 break;
3145 case QEMU_OPTION_rotate:
3146 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3147 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3148 graphic_rotate != 180 && graphic_rotate != 270) {
3149 error_report("only 90, 180, 270 deg rotation is available");
3150 exit(1);
3152 break;
3153 case QEMU_OPTION_kernel:
3154 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg,
3155 &error_abort);
3156 break;
3157 case QEMU_OPTION_initrd:
3158 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg,
3159 &error_abort);
3160 break;
3161 case QEMU_OPTION_append:
3162 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg,
3163 &error_abort);
3164 break;
3165 case QEMU_OPTION_dtb:
3166 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg,
3167 &error_abort);
3168 break;
3169 case QEMU_OPTION_cdrom:
3170 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3171 break;
3172 case QEMU_OPTION_boot:
3173 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
3174 optarg, true);
3175 if (!opts) {
3176 exit(1);
3178 break;
3179 case QEMU_OPTION_fda:
3180 case QEMU_OPTION_fdb:
3181 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3182 optarg, FD_OPTS);
3183 break;
3184 case QEMU_OPTION_no_fd_bootchk:
3185 fd_bootchk = 0;
3186 break;
3187 case QEMU_OPTION_netdev:
3188 default_net = 0;
3189 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3190 exit(1);
3192 break;
3193 case QEMU_OPTION_nic:
3194 default_net = 0;
3195 if (net_client_parse(qemu_find_opts("nic"), optarg) == -1) {
3196 exit(1);
3198 break;
3199 case QEMU_OPTION_net:
3200 default_net = 0;
3201 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3202 exit(1);
3204 break;
3205 #ifdef CONFIG_LIBISCSI
3206 case QEMU_OPTION_iscsi:
3207 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
3208 optarg, false);
3209 if (!opts) {
3210 exit(1);
3212 break;
3213 #endif
3214 case QEMU_OPTION_bt:
3215 warn_report("The bluetooth subsystem is deprecated and will "
3216 "be removed soon. If the bluetooth subsystem is "
3217 "still useful for you, please send a mail to "
3218 "qemu-devel@nongnu.org with your usecase.");
3219 add_device_config(DEV_BT, optarg);
3220 break;
3221 case QEMU_OPTION_audio_help:
3222 audio_legacy_help();
3223 exit (0);
3224 break;
3225 case QEMU_OPTION_audiodev:
3226 audio_parse_option(optarg);
3227 break;
3228 case QEMU_OPTION_soundhw:
3229 select_soundhw (optarg);
3230 break;
3231 case QEMU_OPTION_h:
3232 help(0);
3233 break;
3234 case QEMU_OPTION_version:
3235 version();
3236 exit(0);
3237 break;
3238 case QEMU_OPTION_m:
3239 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
3240 optarg, true);
3241 if (!opts) {
3242 exit(EXIT_FAILURE);
3244 break;
3245 #ifdef CONFIG_TPM
3246 case QEMU_OPTION_tpmdev:
3247 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3248 exit(1);
3250 break;
3251 #endif
3252 case QEMU_OPTION_mempath:
3253 mem_path = optarg;
3254 break;
3255 case QEMU_OPTION_mem_prealloc:
3256 mem_prealloc = 1;
3257 break;
3258 case QEMU_OPTION_d:
3259 log_mask = optarg;
3260 break;
3261 case QEMU_OPTION_D:
3262 log_file = optarg;
3263 break;
3264 case QEMU_OPTION_DFILTER:
3265 qemu_set_dfilter_ranges(optarg, &error_fatal);
3266 break;
3267 case QEMU_OPTION_seed:
3268 qemu_guest_random_seed_main(optarg, &error_fatal);
3269 break;
3270 case QEMU_OPTION_s:
3271 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3272 break;
3273 case QEMU_OPTION_gdb:
3274 add_device_config(DEV_GDB, optarg);
3275 break;
3276 case QEMU_OPTION_L:
3277 if (is_help_option(optarg)) {
3278 list_data_dirs = true;
3279 } else {
3280 qemu_add_data_dir(optarg);
3282 break;
3283 case QEMU_OPTION_bios:
3284 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg,
3285 &error_abort);
3286 break;
3287 case QEMU_OPTION_singlestep:
3288 singlestep = 1;
3289 break;
3290 case QEMU_OPTION_S:
3291 autostart = 0;
3292 break;
3293 case QEMU_OPTION_k:
3294 keyboard_layout = optarg;
3295 break;
3296 case QEMU_OPTION_vga:
3297 vga_model = optarg;
3298 default_vga = 0;
3299 break;
3300 case QEMU_OPTION_g:
3302 const char *p;
3303 int w, h, depth;
3304 p = optarg;
3305 w = strtol(p, (char **)&p, 10);
3306 if (w <= 0) {
3307 graphic_error:
3308 error_report("invalid resolution or depth");
3309 exit(1);
3311 if (*p != 'x')
3312 goto graphic_error;
3313 p++;
3314 h = strtol(p, (char **)&p, 10);
3315 if (h <= 0)
3316 goto graphic_error;
3317 if (*p == 'x') {
3318 p++;
3319 depth = strtol(p, (char **)&p, 10);
3320 if (depth != 8 && depth != 15 && depth != 16 &&
3321 depth != 24 && depth != 32)
3322 goto graphic_error;
3323 } else if (*p == '\0') {
3324 depth = graphic_depth;
3325 } else {
3326 goto graphic_error;
3329 graphic_width = w;
3330 graphic_height = h;
3331 graphic_depth = depth;
3333 break;
3334 case QEMU_OPTION_echr:
3336 char *r;
3337 term_escape_char = strtol(optarg, &r, 0);
3338 if (r == optarg)
3339 printf("Bad argument to echr\n");
3340 break;
3342 case QEMU_OPTION_monitor:
3343 default_monitor = 0;
3344 if (strncmp(optarg, "none", 4)) {
3345 monitor_parse(optarg, "readline", false);
3347 break;
3348 case QEMU_OPTION_qmp:
3349 monitor_parse(optarg, "control", false);
3350 default_monitor = 0;
3351 break;
3352 case QEMU_OPTION_qmp_pretty:
3353 monitor_parse(optarg, "control", true);
3354 default_monitor = 0;
3355 break;
3356 case QEMU_OPTION_mon:
3357 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3358 true);
3359 if (!opts) {
3360 exit(1);
3362 default_monitor = 0;
3363 break;
3364 case QEMU_OPTION_chardev:
3365 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3366 optarg, true);
3367 if (!opts) {
3368 exit(1);
3370 break;
3371 case QEMU_OPTION_fsdev:
3372 olist = qemu_find_opts("fsdev");
3373 if (!olist) {
3374 error_report("fsdev support is disabled");
3375 exit(1);
3377 opts = qemu_opts_parse_noisily(olist, optarg, true);
3378 if (!opts) {
3379 exit(1);
3381 break;
3382 case QEMU_OPTION_virtfs: {
3383 QemuOpts *fsdev;
3384 QemuOpts *device;
3385 const char *writeout, *sock_fd, *socket, *path, *security_model;
3387 olist = qemu_find_opts("virtfs");
3388 if (!olist) {
3389 error_report("virtfs support is disabled");
3390 exit(1);
3392 opts = qemu_opts_parse_noisily(olist, optarg, true);
3393 if (!opts) {
3394 exit(1);
3397 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3398 qemu_opt_get(opts, "mount_tag") == NULL) {
3399 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3400 exit(1);
3402 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3403 qemu_opts_id(opts) ?:
3404 qemu_opt_get(opts, "mount_tag"),
3405 1, NULL);
3406 if (!fsdev) {
3407 error_report("duplicate or invalid fsdev id: %s",
3408 qemu_opt_get(opts, "mount_tag"));
3409 exit(1);
3412 writeout = qemu_opt_get(opts, "writeout");
3413 if (writeout) {
3414 #ifdef CONFIG_SYNC_FILE_RANGE
3415 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3416 #else
3417 error_report("writeout=immediate not supported "
3418 "on this platform");
3419 exit(1);
3420 #endif
3422 qemu_opt_set(fsdev, "fsdriver",
3423 qemu_opt_get(opts, "fsdriver"), &error_abort);
3424 path = qemu_opt_get(opts, "path");
3425 if (path) {
3426 qemu_opt_set(fsdev, "path", path, &error_abort);
3428 security_model = qemu_opt_get(opts, "security_model");
3429 if (security_model) {
3430 qemu_opt_set(fsdev, "security_model", security_model,
3431 &error_abort);
3433 socket = qemu_opt_get(opts, "socket");
3434 if (socket) {
3435 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3437 sock_fd = qemu_opt_get(opts, "sock_fd");
3438 if (sock_fd) {
3439 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3442 qemu_opt_set_bool(fsdev, "readonly",
3443 qemu_opt_get_bool(opts, "readonly", 0),
3444 &error_abort);
3445 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3446 &error_abort);
3447 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3448 qemu_opt_set(device, "fsdev",
3449 qemu_opts_id(fsdev), &error_abort);
3450 qemu_opt_set(device, "mount_tag",
3451 qemu_opt_get(opts, "mount_tag"), &error_abort);
3452 break;
3454 case QEMU_OPTION_virtfs_synth: {
3455 QemuOpts *fsdev;
3456 QemuOpts *device;
3458 warn_report("'-virtfs_synth' is deprecated, please use "
3459 "'-fsdev synth' and '-device virtio-9p-...' "
3460 "instead");
3462 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3463 1, NULL);
3464 if (!fsdev) {
3465 error_report("duplicate option: %s", "virtfs_synth");
3466 exit(1);
3468 qemu_opt_set(fsdev, "fsdriver", "synth", &error_abort);
3470 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3471 &error_abort);
3472 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3473 qemu_opt_set(device, "fsdev", "v_synth", &error_abort);
3474 qemu_opt_set(device, "mount_tag", "v_synth", &error_abort);
3475 break;
3477 case QEMU_OPTION_serial:
3478 add_device_config(DEV_SERIAL, optarg);
3479 default_serial = 0;
3480 if (strncmp(optarg, "mon:", 4) == 0) {
3481 default_monitor = 0;
3483 break;
3484 case QEMU_OPTION_watchdog:
3485 if (watchdog) {
3486 error_report("only one watchdog option may be given");
3487 return 1;
3489 watchdog = optarg;
3490 break;
3491 case QEMU_OPTION_watchdog_action:
3492 if (select_watchdog_action(optarg) == -1) {
3493 error_report("unknown -watchdog-action parameter");
3494 exit(1);
3496 break;
3497 case QEMU_OPTION_parallel:
3498 add_device_config(DEV_PARALLEL, optarg);
3499 default_parallel = 0;
3500 if (strncmp(optarg, "mon:", 4) == 0) {
3501 default_monitor = 0;
3503 break;
3504 case QEMU_OPTION_debugcon:
3505 add_device_config(DEV_DEBUGCON, optarg);
3506 break;
3507 case QEMU_OPTION_loadvm:
3508 loadvm = optarg;
3509 break;
3510 case QEMU_OPTION_full_screen:
3511 dpy.has_full_screen = true;
3512 dpy.full_screen = true;
3513 break;
3514 case QEMU_OPTION_alt_grab:
3515 alt_grab = 1;
3516 break;
3517 case QEMU_OPTION_ctrl_grab:
3518 ctrl_grab = 1;
3519 break;
3520 case QEMU_OPTION_no_quit:
3521 dpy.has_window_close = true;
3522 dpy.window_close = false;
3523 break;
3524 case QEMU_OPTION_sdl:
3525 #ifdef CONFIG_SDL
3526 dpy.type = DISPLAY_TYPE_SDL;
3527 break;
3528 #else
3529 error_report("SDL support is disabled");
3530 exit(1);
3531 #endif
3532 case QEMU_OPTION_pidfile:
3533 pid_file = optarg;
3534 break;
3535 case QEMU_OPTION_win2k_hack:
3536 win2k_install_hack = 1;
3537 break;
3538 case QEMU_OPTION_acpitable:
3539 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3540 optarg, true);
3541 if (!opts) {
3542 exit(1);
3544 acpi_table_add(opts, &error_fatal);
3545 break;
3546 case QEMU_OPTION_smbios:
3547 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3548 optarg, false);
3549 if (!opts) {
3550 exit(1);
3552 smbios_entry_add(opts, &error_fatal);
3553 break;
3554 case QEMU_OPTION_fwcfg:
3555 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3556 optarg, true);
3557 if (opts == NULL) {
3558 exit(1);
3560 break;
3561 case QEMU_OPTION_preconfig:
3562 preconfig_exit_requested = false;
3563 break;
3564 case QEMU_OPTION_enable_kvm:
3565 olist = qemu_find_opts("machine");
3566 qemu_opts_parse_noisily(olist, "accel=kvm", false);
3567 break;
3568 case QEMU_OPTION_M:
3569 case QEMU_OPTION_machine:
3570 olist = qemu_find_opts("machine");
3571 opts = qemu_opts_parse_noisily(olist, optarg, true);
3572 if (!opts) {
3573 exit(1);
3575 break;
3576 case QEMU_OPTION_no_kvm:
3577 olist = qemu_find_opts("machine");
3578 qemu_opts_parse_noisily(olist, "accel=tcg", false);
3579 break;
3580 case QEMU_OPTION_accel:
3581 accel_opts = qemu_opts_parse_noisily(qemu_find_opts("accel"),
3582 optarg, true);
3583 optarg = qemu_opt_get(accel_opts, "accel");
3584 if (!optarg || is_help_option(optarg)) {
3585 printf("Accelerators supported in QEMU binary:\n");
3586 GSList *el, *accel_list = object_class_get_list(TYPE_ACCEL,
3587 false);
3588 for (el = accel_list; el; el = el->next) {
3589 gchar *typename = g_strdup(object_class_get_name(
3590 OBJECT_CLASS(el->data)));
3591 /* omit qtest which is used for tests only */
3592 if (g_strcmp0(typename, ACCEL_CLASS_NAME("qtest")) &&
3593 g_str_has_suffix(typename, ACCEL_CLASS_SUFFIX)) {
3594 gchar **optname = g_strsplit(typename,
3595 ACCEL_CLASS_SUFFIX, 0);
3596 printf("%s\n", optname[0]);
3597 g_free(optname);
3599 g_free(typename);
3601 g_slist_free(accel_list);
3602 exit(0);
3604 opts = qemu_opts_create(qemu_find_opts("machine"), NULL,
3605 false, &error_abort);
3606 qemu_opt_set(opts, "accel", optarg, &error_abort);
3607 break;
3608 case QEMU_OPTION_usb:
3609 olist = qemu_find_opts("machine");
3610 qemu_opts_parse_noisily(olist, "usb=on", false);
3611 break;
3612 case QEMU_OPTION_usbdevice:
3613 error_report("'-usbdevice' is deprecated, please use "
3614 "'-device usb-...' instead");
3615 olist = qemu_find_opts("machine");
3616 qemu_opts_parse_noisily(olist, "usb=on", false);
3617 add_device_config(DEV_USB, optarg);
3618 break;
3619 case QEMU_OPTION_device:
3620 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3621 optarg, true)) {
3622 exit(1);
3624 break;
3625 case QEMU_OPTION_smp:
3626 if (!qemu_opts_parse_noisily(qemu_find_opts("smp-opts"),
3627 optarg, true)) {
3628 exit(1);
3630 break;
3631 case QEMU_OPTION_vnc:
3632 vnc_parse(optarg, &error_fatal);
3633 break;
3634 case QEMU_OPTION_no_acpi:
3635 acpi_enabled = 0;
3636 break;
3637 case QEMU_OPTION_no_hpet:
3638 no_hpet = 1;
3639 break;
3640 case QEMU_OPTION_no_reboot:
3641 no_reboot = 1;
3642 break;
3643 case QEMU_OPTION_no_shutdown:
3644 no_shutdown = 1;
3645 break;
3646 case QEMU_OPTION_show_cursor:
3647 cursor_hide = 0;
3648 break;
3649 case QEMU_OPTION_uuid:
3650 if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
3651 error_report("failed to parse UUID string: wrong format");
3652 exit(1);
3654 qemu_uuid_set = true;
3655 break;
3656 case QEMU_OPTION_option_rom:
3657 if (nb_option_roms >= MAX_OPTION_ROMS) {
3658 error_report("too many option ROMs");
3659 exit(1);
3661 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3662 optarg, true);
3663 if (!opts) {
3664 exit(1);
3666 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3667 option_rom[nb_option_roms].bootindex =
3668 qemu_opt_get_number(opts, "bootindex", -1);
3669 if (!option_rom[nb_option_roms].name) {
3670 error_report("Option ROM file is not specified");
3671 exit(1);
3673 nb_option_roms++;
3674 break;
3675 case QEMU_OPTION_semihosting:
3676 qemu_semihosting_enable();
3677 break;
3678 case QEMU_OPTION_semihosting_config:
3679 if (qemu_semihosting_config_options(optarg) != 0) {
3680 exit(1);
3682 break;
3683 case QEMU_OPTION_name:
3684 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3685 optarg, true);
3686 if (!opts) {
3687 exit(1);
3689 break;
3690 case QEMU_OPTION_prom_env:
3691 if (nb_prom_envs >= MAX_PROM_ENVS) {
3692 error_report("too many prom variables");
3693 exit(1);
3695 prom_envs[nb_prom_envs] = optarg;
3696 nb_prom_envs++;
3697 break;
3698 case QEMU_OPTION_old_param:
3699 old_param = 1;
3700 break;
3701 case QEMU_OPTION_rtc:
3702 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3703 false);
3704 if (!opts) {
3705 exit(1);
3707 break;
3708 case QEMU_OPTION_tb_size:
3709 #ifndef CONFIG_TCG
3710 error_report("TCG is disabled");
3711 exit(1);
3712 #endif
3713 if (qemu_strtoul(optarg, NULL, 0, &tcg_tb_size) < 0) {
3714 error_report("Invalid argument to -tb-size");
3715 exit(1);
3717 break;
3718 case QEMU_OPTION_icount:
3719 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3720 optarg, true);
3721 if (!icount_opts) {
3722 exit(1);
3724 break;
3725 case QEMU_OPTION_incoming:
3726 if (!incoming) {
3727 runstate_set(RUN_STATE_INMIGRATE);
3729 incoming = optarg;
3730 break;
3731 case QEMU_OPTION_only_migratable:
3732 only_migratable = 1;
3733 break;
3734 case QEMU_OPTION_nodefaults:
3735 has_defaults = 0;
3736 break;
3737 case QEMU_OPTION_xen_domid:
3738 if (!(xen_available())) {
3739 error_report("Option not supported for this target");
3740 exit(1);
3742 xen_domid = atoi(optarg);
3743 break;
3744 case QEMU_OPTION_xen_attach:
3745 if (!(xen_available())) {
3746 error_report("Option not supported for this target");
3747 exit(1);
3749 xen_mode = XEN_ATTACH;
3750 break;
3751 case QEMU_OPTION_xen_domid_restrict:
3752 if (!(xen_available())) {
3753 error_report("Option not supported for this target");
3754 exit(1);
3756 xen_domid_restrict = true;
3757 break;
3758 case QEMU_OPTION_trace:
3759 g_free(trace_file);
3760 trace_file = trace_opt_parse(optarg);
3761 break;
3762 case QEMU_OPTION_readconfig:
3764 int ret = qemu_read_config_file(optarg);
3765 if (ret < 0) {
3766 error_report("read config %s: %s", optarg,
3767 strerror(-ret));
3768 exit(1);
3770 break;
3772 case QEMU_OPTION_spice:
3773 olist = qemu_find_opts("spice");
3774 if (!olist) {
3775 error_report("spice support is disabled");
3776 exit(1);
3778 opts = qemu_opts_parse_noisily(olist, optarg, false);
3779 if (!opts) {
3780 exit(1);
3782 display_remote++;
3783 break;
3784 case QEMU_OPTION_writeconfig:
3786 FILE *fp;
3787 if (strcmp(optarg, "-") == 0) {
3788 fp = stdout;
3789 } else {
3790 fp = fopen(optarg, "w");
3791 if (fp == NULL) {
3792 error_report("open %s: %s", optarg,
3793 strerror(errno));
3794 exit(1);
3797 qemu_config_write(fp);
3798 if (fp != stdout) {
3799 fclose(fp);
3801 break;
3803 case QEMU_OPTION_qtest:
3804 qtest_chrdev = optarg;
3805 break;
3806 case QEMU_OPTION_qtest_log:
3807 qtest_log = optarg;
3808 break;
3809 case QEMU_OPTION_sandbox:
3810 olist = qemu_find_opts("sandbox");
3811 if (!olist) {
3812 #ifndef CONFIG_SECCOMP
3813 error_report("-sandbox support is not enabled "
3814 "in this QEMU binary");
3815 #endif
3816 exit(1);
3819 opts = qemu_opts_parse_noisily(olist, optarg, true);
3820 if (!opts) {
3821 exit(1);
3823 break;
3824 case QEMU_OPTION_add_fd:
3825 #ifndef _WIN32
3826 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
3827 optarg, false);
3828 if (!opts) {
3829 exit(1);
3831 #else
3832 error_report("File descriptor passing is disabled on this "
3833 "platform");
3834 exit(1);
3835 #endif
3836 break;
3837 case QEMU_OPTION_object:
3838 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
3839 optarg, true);
3840 if (!opts) {
3841 exit(1);
3843 break;
3844 case QEMU_OPTION_realtime:
3845 warn_report("'-realtime mlock=...' is deprecated, please use "
3846 "'-overcommit mem-lock=...' instead");
3847 opts = qemu_opts_parse_noisily(qemu_find_opts("realtime"),
3848 optarg, false);
3849 if (!opts) {
3850 exit(1);
3852 /* Don't override the -overcommit option if set */
3853 enable_mlock = enable_mlock ||
3854 qemu_opt_get_bool(opts, "mlock", true);
3855 break;
3856 case QEMU_OPTION_overcommit:
3857 opts = qemu_opts_parse_noisily(qemu_find_opts("overcommit"),
3858 optarg, false);
3859 if (!opts) {
3860 exit(1);
3862 /* Don't override the -realtime option if set */
3863 enable_mlock = enable_mlock ||
3864 qemu_opt_get_bool(opts, "mem-lock", false);
3865 enable_cpu_pm = qemu_opt_get_bool(opts, "cpu-pm", false);
3866 break;
3867 case QEMU_OPTION_msg:
3868 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
3869 false);
3870 if (!opts) {
3871 exit(1);
3873 configure_msg(opts);
3874 break;
3875 case QEMU_OPTION_dump_vmstate:
3876 if (vmstate_dump_file) {
3877 error_report("only one '-dump-vmstate' "
3878 "option may be given");
3879 exit(1);
3881 vmstate_dump_file = fopen(optarg, "w");
3882 if (vmstate_dump_file == NULL) {
3883 error_report("open %s: %s", optarg, strerror(errno));
3884 exit(1);
3886 break;
3887 case QEMU_OPTION_enable_sync_profile:
3888 qsp_enable();
3889 break;
3890 case QEMU_OPTION_nouserconfig:
3891 /* Nothing to be parsed here. Especially, do not error out below. */
3892 break;
3893 default:
3894 if (os_parse_cmd_args(popt->index, optarg)) {
3895 error_report("Option not supported in this build");
3896 exit(1);
3902 * Clear error location left behind by the loop.
3903 * Best done right after the loop. Do not insert code here!
3905 loc_set_none();
3907 user_register_global_props();
3909 replay_configure(icount_opts);
3911 if (incoming && !preconfig_exit_requested) {
3912 error_report("'preconfig' and 'incoming' options are "
3913 "mutually exclusive");
3914 exit(EXIT_FAILURE);
3917 configure_rtc(qemu_find_opts_singleton("rtc"));
3919 machine_class = select_machine();
3920 object_set_machine_compat_props(machine_class->compat_props);
3922 set_memory_options(&ram_slots, &maxram_size, machine_class);
3924 os_daemonize();
3925 rcu_disable_atfork();
3927 if (pid_file && !qemu_write_pidfile(pid_file, &err)) {
3928 error_reportf_err(err, "cannot create PID file: ");
3929 exit(1);
3932 qemu_unlink_pidfile_notifier.notify = qemu_unlink_pidfile;
3933 qemu_add_exit_notifier(&qemu_unlink_pidfile_notifier);
3935 if (qemu_init_main_loop(&main_loop_err)) {
3936 error_report_err(main_loop_err);
3937 exit(1);
3940 #ifdef CONFIG_SECCOMP
3941 olist = qemu_find_opts_err("sandbox", NULL);
3942 if (olist) {
3943 qemu_opts_foreach(olist, parse_sandbox, NULL, &error_fatal);
3945 #endif
3947 qemu_opts_foreach(qemu_find_opts("name"),
3948 parse_name, NULL, &error_fatal);
3950 #ifndef _WIN32
3951 qemu_opts_foreach(qemu_find_opts("add-fd"),
3952 parse_add_fd, NULL, &error_fatal);
3954 qemu_opts_foreach(qemu_find_opts("add-fd"),
3955 cleanup_add_fd, NULL, &error_fatal);
3956 #endif
3958 current_machine = MACHINE(object_new(object_class_get_name(
3959 OBJECT_CLASS(machine_class))));
3960 if (machine_help_func(qemu_get_machine_opts(), current_machine)) {
3961 exit(0);
3963 object_property_add_child(object_get_root(), "machine",
3964 OBJECT(current_machine), &error_abort);
3965 object_property_add_child(container_get(OBJECT(current_machine),
3966 "/unattached"),
3967 "sysbus", OBJECT(sysbus_get_default()),
3968 NULL);
3970 if (machine_class->minimum_page_bits) {
3971 if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
3972 /* This would be a board error: specifying a minimum smaller than
3973 * a target's compile-time fixed setting.
3975 g_assert_not_reached();
3979 cpu_exec_init_all();
3981 if (machine_class->hw_version) {
3982 qemu_set_hw_version(machine_class->hw_version);
3985 if (cpu_option && is_help_option(cpu_option)) {
3986 list_cpus(cpu_option);
3987 exit(0);
3990 if (!trace_init_backends()) {
3991 exit(1);
3993 trace_init_file(trace_file);
3995 /* Open the logfile at this point and set the log mask if necessary.
3997 if (log_file) {
3998 qemu_set_log_filename(log_file, &error_fatal);
4001 if (log_mask) {
4002 int mask;
4003 mask = qemu_str_to_log_mask(log_mask);
4004 if (!mask) {
4005 qemu_print_log_usage(stdout);
4006 exit(1);
4008 qemu_set_log(mask);
4009 } else {
4010 qemu_set_log(0);
4013 /* add configured firmware directories */
4014 dirs = g_strsplit(CONFIG_QEMU_FIRMWAREPATH, G_SEARCHPATH_SEPARATOR_S, 0);
4015 for (i = 0; dirs[i] != NULL; i++) {
4016 qemu_add_data_dir(dirs[i]);
4018 g_strfreev(dirs);
4020 /* try to find datadir relative to the executable path */
4021 dir = os_find_datadir();
4022 qemu_add_data_dir(dir);
4023 g_free(dir);
4025 /* add the datadir specified when building */
4026 qemu_add_data_dir(CONFIG_QEMU_DATADIR);
4028 /* -L help lists the data directories and exits. */
4029 if (list_data_dirs) {
4030 for (i = 0; i < data_dir_idx; i++) {
4031 printf("%s\n", data_dir[i]);
4033 exit(0);
4036 /* machine_class: default to UP */
4037 machine_class->max_cpus = machine_class->max_cpus ?: 1;
4038 machine_class->min_cpus = machine_class->min_cpus ?: 1;
4039 machine_class->default_cpus = machine_class->default_cpus ?: 1;
4041 /* default to machine_class->default_cpus */
4042 smp_cpus = machine_class->default_cpus;
4043 max_cpus = machine_class->default_cpus;
4045 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
4047 /* sanity-check smp_cpus and max_cpus against machine_class */
4048 if (smp_cpus < machine_class->min_cpus) {
4049 error_report("Invalid SMP CPUs %d. The min CPUs "
4050 "supported by machine '%s' is %d", smp_cpus,
4051 machine_class->name, machine_class->min_cpus);
4052 exit(1);
4054 if (max_cpus > machine_class->max_cpus) {
4055 error_report("Invalid SMP CPUs %d. The max CPUs "
4056 "supported by machine '%s' is %d", max_cpus,
4057 machine_class->name, machine_class->max_cpus);
4058 exit(1);
4062 * Get the default machine options from the machine if it is not already
4063 * specified either by the configuration file or by the command line.
4065 if (machine_class->default_machine_opts) {
4066 qemu_opts_set_defaults(qemu_find_opts("machine"),
4067 machine_class->default_machine_opts, 0);
4070 qemu_opts_foreach(qemu_find_opts("device"),
4071 default_driver_check, NULL, NULL);
4072 qemu_opts_foreach(qemu_find_opts("global"),
4073 default_driver_check, NULL, NULL);
4075 if (!vga_model && !default_vga) {
4076 vga_interface_type = VGA_DEVICE;
4078 if (!has_defaults || machine_class->no_serial) {
4079 default_serial = 0;
4081 if (!has_defaults || machine_class->no_parallel) {
4082 default_parallel = 0;
4084 if (!has_defaults || machine_class->no_floppy) {
4085 default_floppy = 0;
4087 if (!has_defaults || machine_class->no_cdrom) {
4088 default_cdrom = 0;
4090 if (!has_defaults || machine_class->no_sdcard) {
4091 default_sdcard = 0;
4093 if (!has_defaults) {
4094 default_monitor = 0;
4095 default_net = 0;
4096 default_vga = 0;
4099 if (is_daemonized()) {
4100 if (!preconfig_exit_requested) {
4101 error_report("'preconfig' and 'daemonize' options are "
4102 "mutually exclusive");
4103 exit(EXIT_FAILURE);
4106 /* According to documentation and historically, -nographic redirects
4107 * serial port, parallel port and monitor to stdio, which does not work
4108 * with -daemonize. We can redirect these to null instead, but since
4109 * -nographic is legacy, let's just error out.
4110 * We disallow -nographic only if all other ports are not redirected
4111 * explicitly, to not break existing legacy setups which uses
4112 * -nographic _and_ redirects all ports explicitly - this is valid
4113 * usage, -nographic is just a no-op in this case.
4115 if (nographic
4116 && (default_parallel || default_serial || default_monitor)) {
4117 error_report("-nographic cannot be used with -daemonize");
4118 exit(1);
4120 #ifdef CONFIG_CURSES
4121 if (dpy.type == DISPLAY_TYPE_CURSES) {
4122 error_report("curses display cannot be used with -daemonize");
4123 exit(1);
4125 #endif
4128 if (nographic) {
4129 if (default_parallel)
4130 add_device_config(DEV_PARALLEL, "null");
4131 if (default_serial && default_monitor) {
4132 add_device_config(DEV_SERIAL, "mon:stdio");
4133 } else {
4134 if (default_serial)
4135 add_device_config(DEV_SERIAL, "stdio");
4136 if (default_monitor)
4137 monitor_parse("stdio", "readline", false);
4139 } else {
4140 if (default_serial)
4141 add_device_config(DEV_SERIAL, "vc:80Cx24C");
4142 if (default_parallel)
4143 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
4144 if (default_monitor)
4145 monitor_parse("vc:80Cx24C", "readline", false);
4148 #if defined(CONFIG_VNC)
4149 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
4150 display_remote++;
4152 #endif
4153 if (dpy.type == DISPLAY_TYPE_DEFAULT && !display_remote) {
4154 if (!qemu_display_find_default(&dpy)) {
4155 dpy.type = DISPLAY_TYPE_NONE;
4156 #if defined(CONFIG_VNC)
4157 vnc_parse("localhost:0,to=99,id=default", &error_abort);
4158 #endif
4161 if (dpy.type == DISPLAY_TYPE_DEFAULT) {
4162 dpy.type = DISPLAY_TYPE_NONE;
4165 if ((alt_grab || ctrl_grab) && dpy.type != DISPLAY_TYPE_SDL) {
4166 error_report("-alt-grab and -ctrl-grab are only valid "
4167 "for SDL, ignoring option");
4169 if (dpy.has_window_close &&
4170 (dpy.type != DISPLAY_TYPE_GTK && dpy.type != DISPLAY_TYPE_SDL)) {
4171 error_report("-no-quit is only valid for GTK and SDL, "
4172 "ignoring option");
4175 qemu_display_early_init(&dpy);
4176 qemu_console_early_init();
4178 if (dpy.has_gl && dpy.gl != DISPLAYGL_MODE_OFF && display_opengl == 0) {
4179 #if defined(CONFIG_OPENGL)
4180 error_report("OpenGL is not supported by the display");
4181 #else
4182 error_report("OpenGL support is disabled");
4183 #endif
4184 exit(1);
4187 page_size_init();
4188 socket_init();
4190 qemu_opts_foreach(qemu_find_opts("object"),
4191 user_creatable_add_opts_foreach,
4192 object_create_initial, &error_fatal);
4194 qemu_opts_foreach(qemu_find_opts("chardev"),
4195 chardev_init_func, NULL, &error_fatal);
4196 /* now chardevs have been created we may have semihosting to connect */
4197 qemu_semihosting_connect_chardevs();
4199 #ifdef CONFIG_VIRTFS
4200 qemu_opts_foreach(qemu_find_opts("fsdev"),
4201 fsdev_init_func, NULL, &error_fatal);
4202 #endif
4204 if (qemu_opts_foreach(qemu_find_opts("device"),
4205 device_help_func, NULL, NULL)) {
4206 exit(0);
4210 * Note: we need to create block backends before
4211 * machine_set_property(), so machine properties can refer to
4212 * them.
4214 configure_blockdev(&bdo_queue, machine_class, snapshot);
4216 machine_opts = qemu_get_machine_opts();
4217 qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
4218 &error_fatal);
4219 current_machine->ram_size = ram_size;
4220 current_machine->maxram_size = maxram_size;
4221 current_machine->ram_slots = ram_slots;
4224 * Note: uses machine properties such as kernel-irqchip, must run
4225 * after machine_set_property().
4227 configure_accelerator(current_machine, argv[0]);
4230 * Beware, QOM objects created before this point miss global and
4231 * compat properties.
4233 * Global properties get set up by qdev_prop_register_global(),
4234 * called from user_register_global_props(), and certain option
4235 * desugaring. Also in CPU feature desugaring (buried in
4236 * parse_cpu_option()), which happens below this point, but may
4237 * only target the CPU type, which can only be created after
4238 * parse_cpu_option() returned the type.
4240 * Machine compat properties: object_set_machine_compat_props().
4241 * Accelerator compat props: object_set_accelerator_compat_props(),
4242 * called from configure_accelerator().
4245 if (!qtest_enabled() && machine_class->deprecation_reason) {
4246 error_report("Machine type '%s' is deprecated: %s",
4247 machine_class->name, machine_class->deprecation_reason);
4251 * Note: creates a QOM object, must run only after global and
4252 * compat properties have been set up.
4254 migration_object_init();
4256 if (qtest_chrdev) {
4257 qtest_init(qtest_chrdev, qtest_log, &error_fatal);
4260 machine_opts = qemu_get_machine_opts();
4261 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4262 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4263 kernel_cmdline = qemu_opt_get(machine_opts, "append");
4264 bios_name = qemu_opt_get(machine_opts, "firmware");
4266 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4267 if (opts) {
4268 boot_order = qemu_opt_get(opts, "order");
4269 if (boot_order) {
4270 validate_bootdevices(boot_order, &error_fatal);
4273 boot_once = qemu_opt_get(opts, "once");
4274 if (boot_once) {
4275 validate_bootdevices(boot_once, &error_fatal);
4278 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
4279 boot_strict = qemu_opt_get_bool(opts, "strict", false);
4282 if (!boot_order) {
4283 boot_order = machine_class->default_boot_order;
4286 if (!kernel_cmdline) {
4287 kernel_cmdline = "";
4288 current_machine->kernel_cmdline = (char *)kernel_cmdline;
4291 linux_boot = (kernel_filename != NULL);
4293 if (!linux_boot && *kernel_cmdline != '\0') {
4294 error_report("-append only allowed with -kernel option");
4295 exit(1);
4298 if (!linux_boot && initrd_filename != NULL) {
4299 error_report("-initrd only allowed with -kernel option");
4300 exit(1);
4303 if (semihosting_enabled() && !semihosting_get_argc() && kernel_filename) {
4304 /* fall back to the -kernel/-append */
4305 semihosting_arg_fallback(kernel_filename, kernel_cmdline);
4308 os_set_line_buffering();
4310 /* spice needs the timers to be initialized by this point */
4311 qemu_spice_init();
4313 cpu_ticks_init();
4314 if (icount_opts) {
4315 if (!tcg_enabled()) {
4316 error_report("-icount is not allowed with hardware virtualization");
4317 exit(1);
4319 configure_icount(icount_opts, &error_abort);
4320 qemu_opts_del(icount_opts);
4323 if (tcg_enabled()) {
4324 qemu_tcg_configure(accel_opts, &error_fatal);
4327 if (default_net) {
4328 QemuOptsList *net = qemu_find_opts("net");
4329 qemu_opts_set(net, NULL, "type", "nic", &error_abort);
4330 #ifdef CONFIG_SLIRP
4331 qemu_opts_set(net, NULL, "type", "user", &error_abort);
4332 #endif
4335 if (net_init_clients(&err) < 0) {
4336 error_report_err(err);
4337 exit(1);
4340 qemu_opts_foreach(qemu_find_opts("object"),
4341 user_creatable_add_opts_foreach,
4342 object_create_delayed, &error_fatal);
4344 tpm_init();
4346 /* init the bluetooth world */
4347 if (foreach_device_config(DEV_BT, bt_parse))
4348 exit(1);
4350 if (!xen_enabled()) {
4351 /* On 32-bit hosts, QEMU is limited by virtual address space */
4352 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4353 error_report("at most 2047 MB RAM can be simulated");
4354 exit(1);
4358 blk_mig_init();
4359 ram_mig_init();
4360 dirty_bitmap_mig_init();
4362 qemu_opts_foreach(qemu_find_opts("mon"),
4363 mon_init_func, NULL, &error_fatal);
4365 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4366 exit(1);
4367 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4368 exit(1);
4369 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4370 exit(1);
4372 /* If no default VGA is requested, the default is "none". */
4373 if (default_vga) {
4374 vga_model = get_default_vga_model(machine_class);
4376 if (vga_model) {
4377 select_vgahw(machine_class, vga_model);
4380 if (watchdog) {
4381 i = select_watchdog(watchdog);
4382 if (i > 0)
4383 exit (i == 1 ? 1 : 0);
4386 /* This checkpoint is required by replay to separate prior clock
4387 reading from the other reads, because timer polling functions query
4388 clock values from the log. */
4389 replay_checkpoint(CHECKPOINT_INIT);
4390 qdev_machine_init();
4392 current_machine->boot_order = boot_order;
4394 /* parse features once if machine provides default cpu_type */
4395 current_machine->cpu_type = machine_class->default_cpu_type;
4396 if (cpu_option) {
4397 current_machine->cpu_type = parse_cpu_option(cpu_option);
4399 parse_numa_opts(current_machine);
4401 /* do monitor/qmp handling at preconfig state if requested */
4402 main_loop();
4404 audio_init_audiodevs();
4406 /* from here on runstate is RUN_STATE_PRELAUNCH */
4407 machine_run_board_init(current_machine);
4409 realtime_init();
4411 soundhw_init();
4413 if (hax_enabled()) {
4414 hax_sync_vcpus();
4417 qemu_opts_foreach(qemu_find_opts("fw_cfg"),
4418 parse_fw_cfg, fw_cfg_find(), &error_fatal);
4420 /* init USB devices */
4421 if (machine_usb(current_machine)) {
4422 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4423 exit(1);
4426 /* Check if IGD GFX passthrough. */
4427 igd_gfx_passthru();
4429 /* init generic devices */
4430 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
4431 qemu_opts_foreach(qemu_find_opts("device"),
4432 device_init_func, NULL, &error_fatal);
4434 cpu_synchronize_all_post_init();
4436 rom_reset_order_override();
4438 /* Did we create any drives that we failed to create a device for? */
4439 drive_check_orphaned();
4441 /* Don't warn about the default network setup that you get if
4442 * no command line -net or -netdev options are specified. There
4443 * are two cases that we would otherwise complain about:
4444 * (1) board doesn't support a NIC but the implicit "-net nic"
4445 * requested one
4446 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
4447 * sets up a nic that isn't connected to anything.
4449 if (!default_net && (!qtest_enabled() || has_defaults)) {
4450 net_check_clients();
4453 if (boot_once) {
4454 qemu_boot_set(boot_once, &error_fatal);
4455 qemu_register_reset(restore_boot_order, g_strdup(boot_order));
4458 /* init local displays */
4459 ds = init_displaystate();
4460 qemu_display_init(ds, &dpy);
4462 /* must be after terminal init, SDL library changes signal handlers */
4463 os_setup_signal_handling();
4465 /* init remote displays */
4466 #ifdef CONFIG_VNC
4467 qemu_opts_foreach(qemu_find_opts("vnc"),
4468 vnc_init_func, NULL, &error_fatal);
4469 #endif
4471 if (using_spice) {
4472 qemu_spice_display_init();
4475 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
4476 exit(1);
4479 qdev_machine_creation_done();
4481 /* TODO: once all bus devices are qdevified, this should be done
4482 * when bus is created by qdev.c */
4483 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4484 qemu_run_machine_init_done_notifiers();
4486 if (rom_check_and_register_reset() != 0) {
4487 error_report("rom check and register reset failed");
4488 exit(1);
4491 replay_start();
4493 /* This checkpoint is required by replay to separate prior clock
4494 reading from the other reads, because timer polling functions query
4495 clock values from the log. */
4496 replay_checkpoint(CHECKPOINT_RESET);
4497 qemu_system_reset(SHUTDOWN_CAUSE_NONE);
4498 register_global_state();
4499 if (loadvm) {
4500 Error *local_err = NULL;
4501 if (load_snapshot(loadvm, &local_err) < 0) {
4502 error_report_err(local_err);
4503 autostart = 0;
4504 exit(1);
4507 if (replay_mode != REPLAY_MODE_NONE) {
4508 replay_vmstate_init();
4511 qdev_prop_check_globals();
4512 if (vmstate_dump_file) {
4513 /* dump and exit */
4514 dump_vmstate_json_to_file(vmstate_dump_file);
4515 return 0;
4518 if (incoming) {
4519 Error *local_err = NULL;
4520 qemu_start_incoming_migration(incoming, &local_err);
4521 if (local_err) {
4522 error_reportf_err(local_err, "-incoming %s: ", incoming);
4523 exit(1);
4525 } else if (autostart) {
4526 vm_start();
4529 accel_setup_post(current_machine);
4530 os_setup_post();
4532 main_loop();
4534 gdbserver_cleanup();
4537 * cleaning up the migration object cancels any existing migration
4538 * try to do this early so that it also stops using devices.
4540 migration_shutdown();
4542 /* No more vcpu or device emulation activity beyond this point */
4543 vm_shutdown();
4545 job_cancel_sync_all();
4546 bdrv_close_all();
4548 res_free();
4550 /* vhost-user must be cleaned up before chardevs. */
4551 tpm_cleanup();
4552 net_cleanup();
4553 audio_cleanup();
4554 monitor_cleanup();
4555 qemu_chr_cleanup();
4556 user_creatable_cleanup();
4557 /* TODO: unref root container, check all devices are ok */
4559 return 0;