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
25 #include "qemu/osdep.h"
26 #include "qemu-common.h"
27 #include "qemu/units.h"
28 #include "hw/boards.h"
29 #include "hw/qdev-properties.h"
30 #include "qapi/error.h"
31 #include "qemu-version.h"
32 #include "qemu/cutils.h"
33 #include "qemu/help_option.h"
34 #include "qemu/uuid.h"
35 #include "sysemu/reset.h"
36 #include "sysemu/runstate.h"
37 #include "sysemu/seccomp.h"
38 #include "sysemu/tcg.h"
39 #include "sysemu/xen.h"
41 #include "qemu/error-report.h"
42 #include "qemu/sockets.h"
43 #include "sysemu/accel.h"
45 #include "hw/isa/isa.h"
46 #include "hw/scsi/scsi.h"
47 #include "hw/display/vga.h"
48 #include "sysemu/watchdog.h"
49 #include "hw/firmware/smbios.h"
50 #include "hw/acpi/acpi.h"
51 #include "hw/xen/xen.h"
52 #include "hw/loader.h"
53 #include "monitor/qdev.h"
55 #include "net/slirp.h"
56 #include "monitor/monitor.h"
57 #include "ui/console.h"
59 #include "sysemu/sysemu.h"
60 #include "sysemu/numa.h"
61 #include "sysemu/hostmem.h"
62 #include "exec/gdbstub.h"
63 #include "qemu/timer.h"
64 #include "chardev/char.h"
65 #include "qemu/bitmap.h"
67 #include "sysemu/blockdev.h"
68 #include "hw/block/block.h"
69 #include "migration/misc.h"
70 #include "migration/snapshot.h"
71 #include "migration/global_state.h"
72 #include "sysemu/tpm.h"
73 #include "sysemu/dma.h"
74 #include "hw/audio/soundhw.h"
75 #include "audio/audio.h"
76 #include "sysemu/cpus.h"
77 #include "sysemu/cpu-timers.h"
78 #include "migration/colo.h"
79 #include "migration/postcopy-ram.h"
80 #include "sysemu/kvm.h"
81 #include "sysemu/hax.h"
82 #include "qapi/qobject-input-visitor.h"
83 #include "qemu/option.h"
84 #include "qemu/config-file.h"
85 #include "qemu-options.h"
86 #include "qemu/main-loop.h"
88 #include "fsdev/qemu-fsdev.h"
90 #include "sysemu/qtest.h"
92 #include "disas/disas.h"
95 #include "trace/control.h"
96 #include "qemu/plugin.h"
97 #include "qemu/queue.h"
98 #include "sysemu/arch_init.h"
100 #include "ui/qemu-spice.h"
101 #include "qapi/string-input-visitor.h"
102 #include "qapi/opts-visitor.h"
103 #include "qapi/clone-visitor.h"
104 #include "qom/object_interfaces.h"
105 #include "hw/semihosting/semihost.h"
106 #include "crypto/init.h"
107 #include "sysemu/replay.h"
108 #include "qapi/qapi-events-run-state.h"
109 #include "qapi/qapi-visit-block-core.h"
110 #include "qapi/qapi-visit-ui.h"
111 #include "qapi/qapi-commands-block-core.h"
112 #include "qapi/qapi-commands-run-state.h"
113 #include "qapi/qapi-commands-ui.h"
114 #include "qapi/qmp/qerror.h"
115 #include "sysemu/iothread.h"
116 #include "qemu/guest-random.h"
118 #define MAX_VIRTIO_CONSOLES 1
120 static const char *cpu_option;
121 static const char *data_dir[16];
122 static int data_dir_idx;
123 static const char *mem_path;
124 static const char *boot_order;
125 static const char *boot_once;
126 static const char *incoming;
127 static const char *loadvm;
128 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
129 static int mem_prealloc; /* force preallocation of physical target memory */
131 const char* keyboard_layout = NULL;
132 static ram_addr_t ram_size;
133 bool enable_mlock = false;
134 bool enable_cpu_pm = false;
136 NICInfo nd_table[MAX_NICS];
142 } rtc_base_type = RTC_BASE_UTC;
143 static time_t rtc_ref_start_datetime;
144 static int rtc_realtime_clock_offset; /* used only with QEMU_CLOCK_REALTIME */
145 static int rtc_host_datetime_offset = -1; /* valid & used only with
147 QEMUClockType rtc_clock;
148 int vga_interface_type = VGA_NONE;
149 static DisplayOptions dpy;
150 static int num_serial_hds;
151 static Chardev **serial_hds;
152 static const char *log_mask;
153 static const char *log_file;
154 static bool list_data_dirs;
155 Chardev *parallel_hds[MAX_PARALLEL_PORTS];
156 int win2k_install_hack = 0;
159 static int no_reboot;
161 int graphic_rotate = 0;
162 static const char *watchdog;
163 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
166 const char *qemu_name;
169 unsigned int nb_prom_envs = 0;
170 const char *prom_envs[MAX_PROM_ENVS];
173 uint8_t *boot_splash_filedata;
174 int only_migratable; /* turn it off unless user states otherwise */
175 bool wakeup_suspend_enabled;
176 int icount_align_option;
177 static const char *qtest_chrdev;
178 static const char *qtest_log;
180 /* The bytes in qemu_uuid are in the order specified by RFC4122, _not_ in the
181 * little-endian "wire format" described in the SMBIOS 2.6 specification.
186 static NotifierList exit_notifiers =
187 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
189 static NotifierList machine_init_done_notifiers =
190 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
193 enum xen_mode xen_mode = XEN_EMULATE;
194 bool xen_domid_restrict;
196 static int has_defaults = 1;
197 static int default_serial = 1;
198 static int default_parallel = 1;
199 static int default_monitor = 1;
200 static int default_floppy = 1;
201 static int default_cdrom = 1;
202 static int default_sdcard = 1;
203 static int default_vga = 1;
204 static int default_net = 1;
210 { .driver = "isa-serial", .flag = &default_serial },
211 { .driver = "isa-parallel", .flag = &default_parallel },
212 { .driver = "isa-fdc", .flag = &default_floppy },
213 { .driver = "floppy", .flag = &default_floppy },
214 { .driver = "ide-cd", .flag = &default_cdrom },
215 { .driver = "ide-hd", .flag = &default_cdrom },
216 { .driver = "ide-drive", .flag = &default_cdrom },
217 { .driver = "scsi-cd", .flag = &default_cdrom },
218 { .driver = "scsi-hd", .flag = &default_cdrom },
219 { .driver = "VGA", .flag = &default_vga },
220 { .driver = "isa-vga", .flag = &default_vga },
221 { .driver = "cirrus-vga", .flag = &default_vga },
222 { .driver = "isa-cirrus-vga", .flag = &default_vga },
223 { .driver = "vmware-svga", .flag = &default_vga },
224 { .driver = "qxl-vga", .flag = &default_vga },
225 { .driver = "virtio-vga", .flag = &default_vga },
226 { .driver = "ati-vga", .flag = &default_vga },
227 { .driver = "vhost-user-vga", .flag = &default_vga },
230 static QemuOptsList qemu_rtc_opts = {
232 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
237 .type = QEMU_OPT_STRING,
240 .type = QEMU_OPT_STRING,
243 .type = QEMU_OPT_STRING,
245 { /* end of list */ }
249 static QemuOptsList qemu_option_rom_opts = {
250 .name = "option-rom",
251 .implied_opt_name = "romfile",
252 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
256 .type = QEMU_OPT_NUMBER,
259 .type = QEMU_OPT_STRING,
261 { /* end of list */ }
265 static QemuOptsList qemu_machine_opts = {
267 .implied_opt_name = "type",
269 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
272 * no elements => accept any
273 * sanity checking will happen later
274 * when setting machine properties
280 static QemuOptsList qemu_accel_opts = {
282 .implied_opt_name = "accel",
283 .head = QTAILQ_HEAD_INITIALIZER(qemu_accel_opts.head),
286 * no elements => accept any
287 * sanity checking will happen later
288 * when setting accelerator properties
294 static QemuOptsList qemu_boot_opts = {
296 .implied_opt_name = "order",
298 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
302 .type = QEMU_OPT_STRING,
305 .type = QEMU_OPT_STRING,
308 .type = QEMU_OPT_BOOL,
311 .type = QEMU_OPT_STRING,
313 .name = "splash-time",
314 .type = QEMU_OPT_NUMBER,
316 .name = "reboot-timeout",
317 .type = QEMU_OPT_NUMBER,
320 .type = QEMU_OPT_BOOL,
326 static QemuOptsList qemu_add_fd_opts = {
328 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
332 .type = QEMU_OPT_NUMBER,
333 .help = "file descriptor of which a duplicate is added to fd set",
336 .type = QEMU_OPT_NUMBER,
337 .help = "ID of the fd set to add fd to",
340 .type = QEMU_OPT_STRING,
341 .help = "free-form string used to describe fd",
343 { /* end of list */ }
347 static QemuOptsList qemu_object_opts = {
349 .implied_opt_name = "qom-type",
350 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
356 static QemuOptsList qemu_tpmdev_opts = {
358 .implied_opt_name = "type",
359 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
361 /* options are defined in the TPM backends */
362 { /* end of list */ }
366 static QemuOptsList qemu_realtime_opts = {
368 .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
372 .type = QEMU_OPT_BOOL,
374 { /* end of list */ }
378 static QemuOptsList qemu_overcommit_opts = {
379 .name = "overcommit",
380 .head = QTAILQ_HEAD_INITIALIZER(qemu_overcommit_opts.head),
384 .type = QEMU_OPT_BOOL,
388 .type = QEMU_OPT_BOOL,
390 { /* end of list */ }
394 static QemuOptsList qemu_msg_opts = {
396 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
400 .type = QEMU_OPT_BOOL,
403 .name = "guest-name",
404 .type = QEMU_OPT_BOOL,
405 .help = "Prepends guest name for error messages but only if "
406 "-name guest is set otherwise option is ignored\n",
408 { /* end of list */ }
412 static QemuOptsList qemu_name_opts = {
414 .implied_opt_name = "guest",
416 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
420 .type = QEMU_OPT_STRING,
421 .help = "Sets the name of the guest.\n"
422 "This name will be displayed in the SDL window caption.\n"
423 "The name will also be used for the VNC server",
426 .type = QEMU_OPT_STRING,
427 .help = "Sets the name of the QEMU process, as shown in top etc",
429 .name = "debug-threads",
430 .type = QEMU_OPT_BOOL,
431 .help = "When enabled, name the individual threads; defaults off.\n"
432 "NOTE: The thread names are for debugging and not a\n"
435 { /* End of list */ }
439 static QemuOptsList qemu_mem_opts = {
441 .implied_opt_name = "size",
442 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
447 .type = QEMU_OPT_SIZE,
451 .type = QEMU_OPT_NUMBER,
455 .type = QEMU_OPT_SIZE,
457 { /* end of list */ }
461 static QemuOptsList qemu_icount_opts = {
463 .implied_opt_name = "shift",
465 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
469 .type = QEMU_OPT_STRING,
472 .type = QEMU_OPT_BOOL,
475 .type = QEMU_OPT_BOOL,
478 .type = QEMU_OPT_STRING,
481 .type = QEMU_OPT_STRING,
483 .name = "rrsnapshot",
484 .type = QEMU_OPT_STRING,
486 { /* end of list */ }
490 static QemuOptsList qemu_fw_cfg_opts = {
492 .implied_opt_name = "name",
493 .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
497 .type = QEMU_OPT_STRING,
498 .help = "Sets the fw_cfg name of the blob to be inserted",
501 .type = QEMU_OPT_STRING,
502 .help = "Sets the name of the file from which "
503 "the fw_cfg blob will be loaded",
506 .type = QEMU_OPT_STRING,
507 .help = "Sets content of the blob to be inserted from a string",
510 .type = QEMU_OPT_STRING,
511 .help = "Sets id of the object generating the fw_cfg blob "
514 { /* end of list */ }
519 * Get machine options
521 * Returns: machine options (never null).
523 QemuOpts *qemu_get_machine_opts(void)
525 return qemu_find_opts_singleton("machine");
528 const char *qemu_get_vm_name(void)
533 static void res_free(void)
535 g_free(boot_splash_filedata);
536 boot_splash_filedata = NULL;
539 static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
541 const char *driver = qemu_opt_get(opts, "driver");
546 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
547 if (strcmp(default_list[i].driver, driver) != 0)
549 *(default_list[i].flag) = 0;
554 /***********************************************************/
557 static RunState current_run_state = RUN_STATE_PRECONFIG;
559 /* We use RUN_STATE__MAX but any invalid value will do */
560 static RunState vmstop_requested = RUN_STATE__MAX;
561 static QemuMutex vmstop_lock;
566 } RunStateTransition;
568 static const RunStateTransition runstate_transitions_def[] = {
570 { RUN_STATE_PRECONFIG, RUN_STATE_PRELAUNCH },
571 /* Early switch to inmigrate state to allow -incoming CLI option work
572 * as it used to. TODO: delay actual switching to inmigrate state to
573 * the point after machine is built and remove this hack.
575 { RUN_STATE_PRECONFIG, RUN_STATE_INMIGRATE },
577 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
578 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
579 { RUN_STATE_DEBUG, RUN_STATE_PRELAUNCH },
581 { RUN_STATE_INMIGRATE, RUN_STATE_INTERNAL_ERROR },
582 { RUN_STATE_INMIGRATE, RUN_STATE_IO_ERROR },
583 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
584 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
585 { RUN_STATE_INMIGRATE, RUN_STATE_SHUTDOWN },
586 { RUN_STATE_INMIGRATE, RUN_STATE_SUSPENDED },
587 { RUN_STATE_INMIGRATE, RUN_STATE_WATCHDOG },
588 { RUN_STATE_INMIGRATE, RUN_STATE_GUEST_PANICKED },
589 { RUN_STATE_INMIGRATE, RUN_STATE_FINISH_MIGRATE },
590 { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH },
591 { RUN_STATE_INMIGRATE, RUN_STATE_POSTMIGRATE },
592 { RUN_STATE_INMIGRATE, RUN_STATE_COLO },
594 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
595 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
596 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PRELAUNCH },
598 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
599 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
600 { RUN_STATE_IO_ERROR, RUN_STATE_PRELAUNCH },
602 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
603 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
604 { RUN_STATE_PAUSED, RUN_STATE_POSTMIGRATE },
605 { RUN_STATE_PAUSED, RUN_STATE_PRELAUNCH },
606 { RUN_STATE_PAUSED, RUN_STATE_COLO},
608 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
609 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
610 { RUN_STATE_POSTMIGRATE, RUN_STATE_PRELAUNCH },
612 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
613 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
614 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
616 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
617 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PAUSED },
618 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
619 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PRELAUNCH },
620 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_COLO},
622 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
623 { RUN_STATE_RESTORE_VM, RUN_STATE_PRELAUNCH },
625 { RUN_STATE_COLO, RUN_STATE_RUNNING },
627 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
628 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
629 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
630 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
631 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
632 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
633 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
634 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
635 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
636 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
637 { RUN_STATE_RUNNING, RUN_STATE_COLO},
639 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
641 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
642 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
643 { RUN_STATE_SHUTDOWN, RUN_STATE_PRELAUNCH },
644 { RUN_STATE_SHUTDOWN, RUN_STATE_COLO },
646 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
647 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
648 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
649 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
650 { RUN_STATE_SUSPENDED, RUN_STATE_PRELAUNCH },
651 { RUN_STATE_SUSPENDED, RUN_STATE_COLO},
653 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
654 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
655 { RUN_STATE_WATCHDOG, RUN_STATE_PRELAUNCH },
656 { RUN_STATE_WATCHDOG, RUN_STATE_COLO},
658 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
659 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
660 { RUN_STATE_GUEST_PANICKED, RUN_STATE_PRELAUNCH },
662 { RUN_STATE__MAX, RUN_STATE__MAX },
665 static bool runstate_valid_transitions[RUN_STATE__MAX][RUN_STATE__MAX];
667 bool runstate_check(RunState state)
669 return current_run_state == state;
672 bool runstate_store(char *str, size_t size)
674 const char *state = RunState_str(current_run_state);
675 size_t len = strlen(state) + 1;
680 memcpy(str, state, len);
684 static void runstate_init(void)
686 const RunStateTransition *p;
688 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
689 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE__MAX; p++) {
690 runstate_valid_transitions[p->from][p->to] = true;
693 qemu_mutex_init(&vmstop_lock);
696 /* This function will abort() on invalid state transitions */
697 void runstate_set(RunState new_state)
699 assert(new_state < RUN_STATE__MAX);
701 trace_runstate_set(current_run_state, RunState_str(current_run_state),
702 new_state, RunState_str(new_state));
704 if (current_run_state == new_state) {
708 if (!runstate_valid_transitions[current_run_state][new_state]) {
709 error_report("invalid runstate transition: '%s' -> '%s'",
710 RunState_str(current_run_state),
711 RunState_str(new_state));
715 current_run_state = new_state;
718 int runstate_is_running(void)
720 return runstate_check(RUN_STATE_RUNNING);
723 bool runstate_needs_reset(void)
725 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
726 runstate_check(RUN_STATE_SHUTDOWN);
729 StatusInfo *qmp_query_status(Error **errp)
731 StatusInfo *info = g_malloc0(sizeof(*info));
733 info->running = runstate_is_running();
734 info->singlestep = singlestep;
735 info->status = current_run_state;
740 bool qemu_vmstop_requested(RunState *r)
742 qemu_mutex_lock(&vmstop_lock);
743 *r = vmstop_requested;
744 vmstop_requested = RUN_STATE__MAX;
745 qemu_mutex_unlock(&vmstop_lock);
746 return *r < RUN_STATE__MAX;
749 void qemu_system_vmstop_request_prepare(void)
751 qemu_mutex_lock(&vmstop_lock);
754 void qemu_system_vmstop_request(RunState state)
756 vmstop_requested = state;
757 qemu_mutex_unlock(&vmstop_lock);
761 /***********************************************************/
762 /* RTC reference time/date access */
763 static time_t qemu_ref_timedate(QEMUClockType clock)
765 time_t value = qemu_clock_get_ms(clock) / 1000;
767 case QEMU_CLOCK_REALTIME:
768 value -= rtc_realtime_clock_offset;
770 case QEMU_CLOCK_VIRTUAL:
771 value += rtc_ref_start_datetime;
773 case QEMU_CLOCK_HOST:
774 if (rtc_base_type == RTC_BASE_DATETIME) {
775 value -= rtc_host_datetime_offset;
784 void qemu_get_timedate(struct tm *tm, int offset)
786 time_t ti = qemu_ref_timedate(rtc_clock);
790 switch (rtc_base_type) {
791 case RTC_BASE_DATETIME:
795 case RTC_BASE_LOCALTIME:
796 localtime_r(&ti, tm);
801 int qemu_timedate_diff(struct tm *tm)
805 switch (rtc_base_type) {
806 case RTC_BASE_DATETIME:
808 seconds = mktimegm(tm);
810 case RTC_BASE_LOCALTIME:
813 tmp.tm_isdst = -1; /* use timezone to figure it out */
814 seconds = mktime(&tmp);
821 return seconds - qemu_ref_timedate(QEMU_CLOCK_HOST);
824 static void configure_rtc_base_datetime(const char *startdate)
826 time_t rtc_start_datetime;
829 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d", &tm.tm_year, &tm.tm_mon,
830 &tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec) == 6) {
832 } else if (sscanf(startdate, "%d-%d-%d",
833 &tm.tm_year, &tm.tm_mon, &tm.tm_mday) == 3) {
842 rtc_start_datetime = mktimegm(&tm);
843 if (rtc_start_datetime == -1) {
845 error_report("invalid datetime format");
846 error_printf("valid formats: "
847 "'2006-06-17T16:01:21' or '2006-06-17'\n");
850 rtc_host_datetime_offset = rtc_ref_start_datetime - rtc_start_datetime;
851 rtc_ref_start_datetime = rtc_start_datetime;
854 static void configure_rtc(QemuOpts *opts)
859 rtc_clock = QEMU_CLOCK_HOST;
860 rtc_ref_start_datetime = qemu_clock_get_ms(QEMU_CLOCK_HOST) / 1000;
861 rtc_realtime_clock_offset = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) / 1000;
863 value = qemu_opt_get(opts, "base");
865 if (!strcmp(value, "utc")) {
866 rtc_base_type = RTC_BASE_UTC;
867 } else if (!strcmp(value, "localtime")) {
868 Error *blocker = NULL;
869 rtc_base_type = RTC_BASE_LOCALTIME;
870 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
871 "-rtc base=localtime");
872 replay_add_blocker(blocker);
874 rtc_base_type = RTC_BASE_DATETIME;
875 configure_rtc_base_datetime(value);
878 value = qemu_opt_get(opts, "clock");
880 if (!strcmp(value, "host")) {
881 rtc_clock = QEMU_CLOCK_HOST;
882 } else if (!strcmp(value, "rt")) {
883 rtc_clock = QEMU_CLOCK_REALTIME;
884 } else if (!strcmp(value, "vm")) {
885 rtc_clock = QEMU_CLOCK_VIRTUAL;
887 error_report("invalid option value '%s'", value);
891 value = qemu_opt_get(opts, "driftfix");
893 if (!strcmp(value, "slew")) {
894 object_register_sugar_prop("mc146818rtc",
897 } else if (!strcmp(value, "none")) {
898 /* discard is default */
900 error_report("invalid option value '%s'", value);
906 static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
908 const char *proc_name;
910 if (qemu_opt_get(opts, "debug-threads")) {
911 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
913 qemu_name = qemu_opt_get(opts, "guest");
915 proc_name = qemu_opt_get(opts, "process");
917 os_set_proc_name(proc_name);
923 bool defaults_enabled(void)
929 static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
931 int fd, dupfd, flags;
933 const char *fd_opaque = NULL;
936 fd = qemu_opt_get_number(opts, "fd", -1);
937 fdset_id = qemu_opt_get_number(opts, "set", -1);
938 fd_opaque = qemu_opt_get(opts, "opaque");
941 error_setg(errp, "fd option is required and must be non-negative");
945 if (fd <= STDERR_FILENO) {
946 error_setg(errp, "fd cannot be a standard I/O stream");
951 * All fds inherited across exec() necessarily have FD_CLOEXEC
952 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
954 flags = fcntl(fd, F_GETFD);
955 if (flags == -1 || (flags & FD_CLOEXEC)) {
956 error_setg(errp, "fd is not valid or already in use");
961 error_setg(errp, "set option is required and must be non-negative");
965 #ifdef F_DUPFD_CLOEXEC
966 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
970 qemu_set_cloexec(dupfd);
974 error_setg(errp, "error duplicating fd: %s", strerror(errno));
978 /* add the duplicate fd, and optionally the opaque string, to the fd set */
979 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
986 static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
990 fd = qemu_opt_get_number(opts, "fd", -1);
997 /***********************************************************/
998 /* QEMU Block devices */
1000 #define HD_OPTS "media=disk"
1001 #define CDROM_OPTS "media=cdrom"
1003 #define PFLASH_OPTS ""
1007 static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
1009 BlockInterfaceType *block_default_type = opaque;
1011 return drive_new(opts, *block_default_type, errp) == NULL;
1014 static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
1016 if (qemu_opt_get(opts, "snapshot") == NULL) {
1017 qemu_opt_set(opts, "snapshot", "on", &error_abort);
1022 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1023 int index, const char *optstr)
1028 if (!enable || drive_get_by_index(type, index)) {
1032 opts = drive_add(type, index, NULL, optstr);
1034 drive_enable_snapshot(NULL, opts, NULL);
1037 dinfo = drive_new(opts, type, &error_abort);
1038 dinfo->is_default = true;
1042 typedef struct BlockdevOptionsQueueEntry {
1043 BlockdevOptions *bdo;
1045 QSIMPLEQ_ENTRY(BlockdevOptionsQueueEntry) entry;
1046 } BlockdevOptionsQueueEntry;
1048 typedef QSIMPLEQ_HEAD(, BlockdevOptionsQueueEntry) BlockdevOptionsQueue;
1050 static void configure_blockdev(BlockdevOptionsQueue *bdo_queue,
1051 MachineClass *machine_class, int snapshot)
1054 * If the currently selected machine wishes to override the
1055 * units-per-bus property of its default HBA interface type, do so
1058 if (machine_class->units_per_default_bus) {
1059 override_max_devs(machine_class->block_default_type,
1060 machine_class->units_per_default_bus);
1063 /* open the virtual block devices */
1064 while (!QSIMPLEQ_EMPTY(bdo_queue)) {
1065 BlockdevOptionsQueueEntry *bdo = QSIMPLEQ_FIRST(bdo_queue);
1067 QSIMPLEQ_REMOVE_HEAD(bdo_queue, entry);
1068 loc_push_restore(&bdo->loc);
1069 qmp_blockdev_add(bdo->bdo, &error_fatal);
1071 qapi_free_BlockdevOptions(bdo->bdo);
1075 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
1078 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
1079 &machine_class->block_default_type, &error_fatal)) {
1080 /* We printed help */
1084 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
1086 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
1087 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
1091 static QemuOptsList qemu_smp_opts = {
1093 .implied_opt_name = "cpus",
1094 .merge_lists = true,
1095 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
1099 .type = QEMU_OPT_NUMBER,
1102 .type = QEMU_OPT_NUMBER,
1105 .type = QEMU_OPT_NUMBER,
1108 .type = QEMU_OPT_NUMBER,
1111 .type = QEMU_OPT_NUMBER,
1114 .type = QEMU_OPT_NUMBER,
1116 { /*End of list */ }
1120 static void realtime_init(void)
1123 if (os_mlock() < 0) {
1124 error_report("locking memory failed");
1131 static void configure_msg(QemuOpts *opts)
1133 error_with_timestamp = qemu_opt_get_bool(opts, "timestamp", false);
1134 error_with_guestname = qemu_opt_get_bool(opts, "guest-name", false);
1138 /***********************************************************/
1141 static int usb_device_add(const char *devname)
1143 USBDevice *dev = NULL;
1145 if (!machine_usb(current_machine)) {
1149 dev = usbdevice_create(devname);
1156 static int usb_parse(const char *cmdline)
1159 r = usb_device_add(cmdline);
1161 error_report("could not add USB device '%s'", cmdline);
1166 /***********************************************************/
1167 /* machine registration */
1169 MachineState *current_machine;
1171 static MachineClass *find_machine(const char *name, GSList *machines)
1175 for (el = machines; el; el = el->next) {
1176 MachineClass *mc = el->data;
1178 if (!strcmp(mc->name, name) || !g_strcmp0(mc->alias, name)) {
1186 static MachineClass *find_default_machine(GSList *machines)
1189 MachineClass *default_machineclass = NULL;
1191 for (el = machines; el; el = el->next) {
1192 MachineClass *mc = el->data;
1194 if (mc->is_default) {
1195 assert(default_machineclass == NULL && "Multiple default machines");
1196 default_machineclass = mc;
1200 return default_machineclass;
1203 static int machine_help_func(QemuOpts *opts, MachineState *machine)
1205 ObjectProperty *prop;
1206 ObjectPropertyIterator iter;
1208 if (!qemu_opt_has_help_opt(opts)) {
1212 object_property_iter_init(&iter, OBJECT(machine));
1213 while ((prop = object_property_iter_next(&iter))) {
1218 printf("%s.%s=%s", MACHINE_GET_CLASS(machine)->name,
1219 prop->name, prop->type);
1220 if (prop->description) {
1221 printf(" (%s)\n", prop->description);
1230 struct VMChangeStateEntry {
1231 VMChangeStateHandler *cb;
1233 QTAILQ_ENTRY(VMChangeStateEntry) entries;
1237 static QTAILQ_HEAD(, VMChangeStateEntry) vm_change_state_head =
1238 QTAILQ_HEAD_INITIALIZER(vm_change_state_head);
1241 * qemu_add_vm_change_state_handler_prio:
1242 * @cb: the callback to invoke
1243 * @opaque: user data passed to the callback
1244 * @priority: low priorities execute first when the vm runs and the reverse is
1245 * true when the vm stops
1247 * Register a callback function that is invoked when the vm starts or stops
1250 * Returns: an entry to be freed using qemu_del_vm_change_state_handler()
1252 VMChangeStateEntry *qemu_add_vm_change_state_handler_prio(
1253 VMChangeStateHandler *cb, void *opaque, int priority)
1255 VMChangeStateEntry *e;
1256 VMChangeStateEntry *other;
1258 e = g_malloc0(sizeof(*e));
1261 e->priority = priority;
1263 /* Keep list sorted in ascending priority order */
1264 QTAILQ_FOREACH(other, &vm_change_state_head, entries) {
1265 if (priority < other->priority) {
1266 QTAILQ_INSERT_BEFORE(other, e, entries);
1271 QTAILQ_INSERT_TAIL(&vm_change_state_head, e, entries);
1275 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1278 return qemu_add_vm_change_state_handler_prio(cb, opaque, 0);
1281 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1283 QTAILQ_REMOVE(&vm_change_state_head, e, entries);
1287 void vm_state_notify(int running, RunState state)
1289 VMChangeStateEntry *e, *next;
1291 trace_vm_state_notify(running, state, RunState_str(state));
1294 QTAILQ_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
1295 e->cb(e->opaque, running, state);
1298 QTAILQ_FOREACH_REVERSE_SAFE(e, &vm_change_state_head, entries, next) {
1299 e->cb(e->opaque, running, state);
1304 static ShutdownCause reset_requested;
1305 static ShutdownCause shutdown_requested;
1306 static int shutdown_signal;
1307 static pid_t shutdown_pid;
1308 static int powerdown_requested;
1309 static int debug_requested;
1310 static int suspend_requested;
1311 static bool preconfig_exit_requested = true;
1312 static WakeupReason wakeup_reason;
1313 static NotifierList powerdown_notifiers =
1314 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
1315 static NotifierList suspend_notifiers =
1316 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1317 static NotifierList wakeup_notifiers =
1318 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1319 static NotifierList shutdown_notifiers =
1320 NOTIFIER_LIST_INITIALIZER(shutdown_notifiers);
1321 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
1323 ShutdownCause qemu_shutdown_requested_get(void)
1325 return shutdown_requested;
1328 ShutdownCause qemu_reset_requested_get(void)
1330 return reset_requested;
1333 static int qemu_shutdown_requested(void)
1335 return qatomic_xchg(&shutdown_requested, SHUTDOWN_CAUSE_NONE);
1338 static void qemu_kill_report(void)
1340 if (!qtest_driver() && shutdown_signal) {
1341 if (shutdown_pid == 0) {
1342 /* This happens for eg ^C at the terminal, so it's worth
1343 * avoiding printing an odd message in that case.
1345 error_report("terminating on signal %d", shutdown_signal);
1347 char *shutdown_cmd = qemu_get_pid_name(shutdown_pid);
1349 error_report("terminating on signal %d from pid " FMT_pid " (%s)",
1350 shutdown_signal, shutdown_pid,
1351 shutdown_cmd ? shutdown_cmd : "<unknown process>");
1352 g_free(shutdown_cmd);
1354 shutdown_signal = 0;
1358 static ShutdownCause qemu_reset_requested(void)
1360 ShutdownCause r = reset_requested;
1362 if (r && replay_checkpoint(CHECKPOINT_RESET_REQUESTED)) {
1363 reset_requested = SHUTDOWN_CAUSE_NONE;
1366 return SHUTDOWN_CAUSE_NONE;
1369 static int qemu_suspend_requested(void)
1371 int r = suspend_requested;
1372 if (r && replay_checkpoint(CHECKPOINT_SUSPEND_REQUESTED)) {
1373 suspend_requested = 0;
1379 static WakeupReason qemu_wakeup_requested(void)
1381 return wakeup_reason;
1384 static int qemu_powerdown_requested(void)
1386 int r = powerdown_requested;
1387 powerdown_requested = 0;
1391 static int qemu_debug_requested(void)
1393 int r = debug_requested;
1394 debug_requested = 0;
1398 void qemu_exit_preconfig_request(void)
1400 preconfig_exit_requested = true;
1404 * Reset the VM. Issue an event unless @reason is SHUTDOWN_CAUSE_NONE.
1406 void qemu_system_reset(ShutdownCause reason)
1410 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1412 cpu_synchronize_all_states();
1414 if (mc && mc->reset) {
1415 mc->reset(current_machine);
1417 qemu_devices_reset();
1419 if (reason && reason != SHUTDOWN_CAUSE_SUBSYSTEM_RESET) {
1420 qapi_event_send_reset(shutdown_caused_by_guest(reason), reason);
1422 cpu_synchronize_all_post_reset();
1426 * Wake the VM after suspend.
1428 static void qemu_system_wakeup(void)
1432 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1434 if (mc && mc->wakeup) {
1435 mc->wakeup(current_machine);
1439 void qemu_system_guest_panicked(GuestPanicInformation *info)
1441 qemu_log_mask(LOG_GUEST_ERROR, "Guest crashed");
1444 current_cpu->crash_occurred = true;
1446 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE,
1448 vm_stop(RUN_STATE_GUEST_PANICKED);
1450 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_POWEROFF,
1452 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_PANIC);
1456 if (info->type == GUEST_PANIC_INFORMATION_TYPE_HYPER_V) {
1457 qemu_log_mask(LOG_GUEST_ERROR, "\nHV crash parameters: (%#"PRIx64
1458 " %#"PRIx64" %#"PRIx64" %#"PRIx64" %#"PRIx64")\n",
1459 info->u.hyper_v.arg1,
1460 info->u.hyper_v.arg2,
1461 info->u.hyper_v.arg3,
1462 info->u.hyper_v.arg4,
1463 info->u.hyper_v.arg5);
1464 } else if (info->type == GUEST_PANIC_INFORMATION_TYPE_S390) {
1465 qemu_log_mask(LOG_GUEST_ERROR, " on cpu %d: %s\n"
1466 "PSW: 0x%016" PRIx64 " 0x%016" PRIx64"\n",
1468 S390CrashReason_str(info->u.s390.reason),
1469 info->u.s390.psw_mask,
1470 info->u.s390.psw_addr);
1472 qapi_free_GuestPanicInformation(info);
1476 void qemu_system_guest_crashloaded(GuestPanicInformation *info)
1478 qemu_log_mask(LOG_GUEST_ERROR, "Guest crash loaded");
1480 qapi_event_send_guest_crashloaded(GUEST_PANIC_ACTION_RUN,
1484 qapi_free_GuestPanicInformation(info);
1488 void qemu_system_reset_request(ShutdownCause reason)
1490 if (no_reboot && reason != SHUTDOWN_CAUSE_SUBSYSTEM_RESET) {
1491 shutdown_requested = reason;
1493 reset_requested = reason;
1496 qemu_notify_event();
1499 static void qemu_system_suspend(void)
1502 notifier_list_notify(&suspend_notifiers, NULL);
1503 runstate_set(RUN_STATE_SUSPENDED);
1504 qapi_event_send_suspend();
1507 void qemu_system_suspend_request(void)
1509 if (runstate_check(RUN_STATE_SUSPENDED)) {
1512 suspend_requested = 1;
1514 qemu_notify_event();
1517 void qemu_register_suspend_notifier(Notifier *notifier)
1519 notifier_list_add(&suspend_notifiers, notifier);
1522 void qemu_system_wakeup_request(WakeupReason reason, Error **errp)
1524 trace_system_wakeup_request(reason);
1526 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1528 "Unable to wake up: guest is not in suspended state");
1531 if (!(wakeup_reason_mask & (1 << reason))) {
1534 runstate_set(RUN_STATE_RUNNING);
1535 wakeup_reason = reason;
1536 qemu_notify_event();
1539 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1542 wakeup_reason_mask |= (1 << reason);
1544 wakeup_reason_mask &= ~(1 << reason);
1548 void qemu_register_wakeup_notifier(Notifier *notifier)
1550 notifier_list_add(&wakeup_notifiers, notifier);
1553 void qemu_register_wakeup_support(void)
1555 wakeup_suspend_enabled = true;
1558 bool qemu_wakeup_suspend_enabled(void)
1560 return wakeup_suspend_enabled;
1563 void qemu_system_killed(int signal, pid_t pid)
1565 shutdown_signal = signal;
1569 /* Cannot call qemu_system_shutdown_request directly because
1570 * we are in a signal handler.
1572 shutdown_requested = SHUTDOWN_CAUSE_HOST_SIGNAL;
1573 qemu_notify_event();
1576 void qemu_system_shutdown_request(ShutdownCause reason)
1578 trace_qemu_system_shutdown_request(reason);
1579 replay_shutdown_request(reason);
1580 shutdown_requested = reason;
1581 qemu_notify_event();
1584 static void qemu_system_powerdown(void)
1586 qapi_event_send_powerdown();
1587 notifier_list_notify(&powerdown_notifiers, NULL);
1590 static void qemu_system_shutdown(ShutdownCause cause)
1592 qapi_event_send_shutdown(shutdown_caused_by_guest(cause), cause);
1593 notifier_list_notify(&shutdown_notifiers, &cause);
1596 void qemu_system_powerdown_request(void)
1598 trace_qemu_system_powerdown_request();
1599 powerdown_requested = 1;
1600 qemu_notify_event();
1603 void qemu_register_powerdown_notifier(Notifier *notifier)
1605 notifier_list_add(&powerdown_notifiers, notifier);
1608 void qemu_register_shutdown_notifier(Notifier *notifier)
1610 notifier_list_add(&shutdown_notifiers, notifier);
1613 void qemu_system_debug_request(void)
1615 debug_requested = 1;
1616 qemu_notify_event();
1619 static bool main_loop_should_exit(void)
1622 ShutdownCause request;
1624 if (preconfig_exit_requested) {
1625 if (runstate_check(RUN_STATE_PRECONFIG)) {
1626 runstate_set(RUN_STATE_PRELAUNCH);
1628 preconfig_exit_requested = false;
1631 if (qemu_debug_requested()) {
1632 vm_stop(RUN_STATE_DEBUG);
1634 if (qemu_suspend_requested()) {
1635 qemu_system_suspend();
1637 request = qemu_shutdown_requested();
1640 qemu_system_shutdown(request);
1642 vm_stop(RUN_STATE_SHUTDOWN);
1647 request = qemu_reset_requested();
1650 qemu_system_reset(request);
1653 * runstate can change in pause_all_vcpus()
1654 * as iothread mutex is unlocked
1656 if (!runstate_check(RUN_STATE_RUNNING) &&
1657 !runstate_check(RUN_STATE_INMIGRATE) &&
1658 !runstate_check(RUN_STATE_FINISH_MIGRATE)) {
1659 runstate_set(RUN_STATE_PRELAUNCH);
1662 if (qemu_wakeup_requested()) {
1664 qemu_system_wakeup();
1665 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
1666 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
1668 qapi_event_send_wakeup();
1670 if (qemu_powerdown_requested()) {
1671 qemu_system_powerdown();
1673 if (qemu_vmstop_requested(&r)) {
1679 void qemu_main_loop(void)
1681 #ifdef CONFIG_PROFILER
1684 while (!main_loop_should_exit()) {
1685 #ifdef CONFIG_PROFILER
1686 ti = profile_getclock();
1688 main_loop_wait(false);
1689 #ifdef CONFIG_PROFILER
1690 dev_time += profile_getclock() - ti;
1695 static void version(void)
1697 printf("QEMU emulator version " QEMU_FULL_VERSION "\n"
1698 QEMU_COPYRIGHT "\n");
1701 static void help(int exitcode)
1704 printf("usage: %s [options] [disk_image]\n\n"
1705 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1706 error_get_progname());
1708 #define QEMU_OPTIONS_GENERATE_HELP
1709 #include "qemu-options-wrapper.h"
1711 printf("\nDuring emulation, the following keys are useful:\n"
1712 "ctrl-alt-f toggle full screen\n"
1713 "ctrl-alt-n switch to virtual console 'n'\n"
1714 "ctrl-alt toggle mouse and keyboard grab\n"
1716 "When using -nographic, press 'ctrl-a h' to get some help.\n"
1718 QEMU_HELP_BOTTOM "\n");
1723 #define HAS_ARG 0x0001
1725 typedef struct QEMUOption {
1732 static const QEMUOption qemu_options[] = {
1733 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1734 #define QEMU_OPTIONS_GENERATE_OPTIONS
1735 #include "qemu-options-wrapper.h"
1739 typedef struct VGAInterfaceInfo {
1740 const char *opt_name; /* option name */
1741 const char *name; /* human-readable name */
1742 /* Class names indicating that support is available.
1743 * If no class is specified, the interface is always available */
1744 const char *class_names[2];
1747 static const VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
1750 .name = "no graphic card",
1754 .name = "standard VGA",
1755 .class_names = { "VGA", "isa-vga" },
1758 .opt_name = "cirrus",
1759 .name = "Cirrus VGA",
1760 .class_names = { "cirrus-vga", "isa-cirrus-vga" },
1763 .opt_name = "vmware",
1764 .name = "VMWare SVGA",
1765 .class_names = { "vmware-svga" },
1768 .opt_name = "virtio",
1769 .name = "Virtio VGA",
1770 .class_names = { "virtio-vga" },
1775 .class_names = { "qxl-vga" },
1779 .name = "TCX framebuffer",
1780 .class_names = { "SUNW,tcx" },
1784 .name = "CG3 framebuffer",
1785 .class_names = { "cgthree" },
1788 .opt_name = "xenfb",
1789 .name = "Xen paravirtualized framebuffer",
1793 static bool vga_interface_available(VGAInterfaceType t)
1795 const VGAInterfaceInfo *ti = &vga_interfaces[t];
1797 assert(t < VGA_TYPE_MAX);
1798 return !ti->class_names[0] ||
1799 module_object_class_by_name(ti->class_names[0]) ||
1800 module_object_class_by_name(ti->class_names[1]);
1804 get_default_vga_model(const MachineClass *machine_class)
1806 if (machine_class->default_display) {
1807 return machine_class->default_display;
1808 } else if (vga_interface_available(VGA_CIRRUS)) {
1810 } else if (vga_interface_available(VGA_STD)) {
1817 static void select_vgahw(const MachineClass *machine_class, const char *p)
1822 if (g_str_equal(p, "help")) {
1823 const char *def = get_default_vga_model(machine_class);
1825 for (t = 0; t < VGA_TYPE_MAX; t++) {
1826 const VGAInterfaceInfo *ti = &vga_interfaces[t];
1828 if (vga_interface_available(t) && ti->opt_name) {
1829 printf("%-20s %s%s\n", ti->opt_name, ti->name ?: "",
1830 g_str_equal(ti->opt_name, def) ? " (default)" : "");
1836 assert(vga_interface_type == VGA_NONE);
1837 for (t = 0; t < VGA_TYPE_MAX; t++) {
1838 const VGAInterfaceInfo *ti = &vga_interfaces[t];
1839 if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
1840 if (!vga_interface_available(t)) {
1841 error_report("%s not available", ti->name);
1844 vga_interface_type = t;
1848 if (t == VGA_TYPE_MAX) {
1850 error_report("unknown vga type: %s", p);
1854 const char *nextopt;
1856 if (strstart(opts, ",retrace=", &nextopt)) {
1858 if (strstart(opts, "dumb", &nextopt))
1859 vga_retrace_method = VGA_RETRACE_DUMB;
1860 else if (strstart(opts, "precise", &nextopt))
1861 vga_retrace_method = VGA_RETRACE_PRECISE;
1862 else goto invalid_vga;
1863 } else goto invalid_vga;
1868 static void parse_display_qapi(const char *optarg)
1870 DisplayOptions *opts;
1873 v = qobject_input_visitor_new_str(optarg, "type", &error_fatal);
1875 visit_type_DisplayOptions(v, NULL, &opts, &error_fatal);
1876 QAPI_CLONE_MEMBERS(DisplayOptions, &dpy, opts);
1878 qapi_free_DisplayOptions(opts);
1882 DisplayOptions *qmp_query_display_options(Error **errp)
1884 return QAPI_CLONE(DisplayOptions, &dpy);
1887 static void parse_display(const char *p)
1891 if (is_help_option(p)) {
1892 qemu_display_help();
1896 if (strstart(p, "sdl", &opts)) {
1898 * sdl DisplayType needs hand-crafted parser instead of
1899 * parse_display_qapi() due to some options not in
1900 * DisplayOptions, specifically:
1902 * Already deprecated.
1903 * - ctrl_grab + alt_grab
1904 * Not clear yet what happens to them long-term. Should
1905 * replaced by something better or deprecated and dropped.
1907 dpy.type = DISPLAY_TYPE_SDL;
1909 const char *nextopt;
1911 if (strstart(opts, ",alt_grab=", &nextopt)) {
1913 if (strstart(opts, "on", &nextopt)) {
1915 } else if (strstart(opts, "off", &nextopt)) {
1918 goto invalid_sdl_args;
1920 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1922 if (strstart(opts, "on", &nextopt)) {
1924 } else if (strstart(opts, "off", &nextopt)) {
1927 goto invalid_sdl_args;
1929 } else if (strstart(opts, ",window_close=", &nextopt)) {
1931 dpy.has_window_close = true;
1932 if (strstart(opts, "on", &nextopt)) {
1933 dpy.window_close = true;
1934 } else if (strstart(opts, "off", &nextopt)) {
1935 dpy.window_close = false;
1937 goto invalid_sdl_args;
1939 } else if (strstart(opts, ",show-cursor=", &nextopt)) {
1941 dpy.has_show_cursor = true;
1942 if (strstart(opts, "on", &nextopt)) {
1943 dpy.show_cursor = true;
1944 } else if (strstart(opts, "off", &nextopt)) {
1945 dpy.show_cursor = false;
1947 goto invalid_sdl_args;
1949 } else if (strstart(opts, ",gl=", &nextopt)) {
1952 if (strstart(opts, "on", &nextopt)) {
1953 dpy.gl = DISPLAYGL_MODE_ON;
1954 } else if (strstart(opts, "core", &nextopt)) {
1955 dpy.gl = DISPLAYGL_MODE_CORE;
1956 } else if (strstart(opts, "es", &nextopt)) {
1957 dpy.gl = DISPLAYGL_MODE_ES;
1958 } else if (strstart(opts, "off", &nextopt)) {
1959 dpy.gl = DISPLAYGL_MODE_OFF;
1961 goto invalid_sdl_args;
1965 error_report("invalid SDL option string");
1970 } else if (strstart(p, "vnc", &opts)) {
1972 * vnc isn't a (local) DisplayType but a protocol for remote
1976 vnc_parse(opts + 1, &error_fatal);
1978 error_report("VNC requires a display argument vnc=<display>");
1982 parse_display_qapi(p);
1986 char *qemu_find_file(int type, const char *name)
1992 /* Try the name as a straight path first */
1993 if (access(name, R_OK) == 0) {
1994 trace_load_file(name, name);
1995 return g_strdup(name);
1999 case QEMU_FILE_TYPE_BIOS:
2002 case QEMU_FILE_TYPE_KEYMAP:
2003 subdir = "keymaps/";
2009 for (i = 0; i < data_dir_idx; i++) {
2010 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2011 if (access(buf, R_OK) == 0) {
2012 trace_load_file(name, buf);
2020 void qemu_add_data_dir(char *path)
2027 if (data_dir_idx == ARRAY_SIZE(data_dir)) {
2030 for (i = 0; i < data_dir_idx; i++) {
2031 if (strcmp(data_dir[i], path) == 0) {
2032 g_free(path); /* duplicate */
2036 data_dir[data_dir_idx++] = path;
2039 static inline bool nonempty_str(const char *str)
2044 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
2048 const char *name, *file, *str, *gen_id;
2049 FWCfgState *fw_cfg = (FWCfgState *) opaque;
2051 if (fw_cfg == NULL) {
2052 error_setg(errp, "fw_cfg device not available");
2055 name = qemu_opt_get(opts, "name");
2056 file = qemu_opt_get(opts, "file");
2057 str = qemu_opt_get(opts, "string");
2058 gen_id = qemu_opt_get(opts, "gen_id");
2060 /* we need the name, and exactly one of: file, content string, gen_id */
2061 if (!nonempty_str(name) ||
2062 nonempty_str(file) + nonempty_str(str) + nonempty_str(gen_id) != 1) {
2063 error_setg(errp, "name, plus exactly one of file,"
2064 " string and gen_id, are needed");
2067 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
2068 error_setg(errp, "name too long (max. %d char)",
2069 FW_CFG_MAX_FILE_PATH - 1);
2072 if (nonempty_str(gen_id)) {
2074 * In this particular case where the content is populated
2075 * internally, the "etc/" namespace protection is relaxed,
2076 * so do not emit a warning.
2078 } else if (strncmp(name, "opt/", 4) != 0) {
2079 warn_report("externally provided fw_cfg item names "
2080 "should be prefixed with \"opt/\"");
2082 if (nonempty_str(str)) {
2083 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
2084 buf = g_memdup(str, size);
2085 } else if (nonempty_str(gen_id)) {
2086 if (!fw_cfg_add_from_generator(fw_cfg, name, gen_id, errp)) {
2092 if (!g_file_get_contents(file, &buf, &size, &err)) {
2093 error_setg(errp, "can't load %s: %s", file, err->message);
2098 /* For legacy, keep user files in a specific global order. */
2099 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
2100 fw_cfg_add_file(fw_cfg, name, buf, size);
2101 fw_cfg_reset_order_override(fw_cfg);
2105 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
2107 return qdev_device_help(opts);
2110 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
2114 dev = qdev_device_add(opts, errp);
2115 if (!dev && *errp) {
2116 error_report_err(*errp);
2119 object_unref(OBJECT(dev));
2124 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2126 Error *local_err = NULL;
2128 if (!qemu_chr_new_from_opts(opts, NULL, &local_err)) {
2130 error_propagate(errp, local_err);
2138 #ifdef CONFIG_VIRTFS
2139 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2141 return qemu_fsdev_add(opts, errp);
2145 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
2147 return monitor_init_opts(opts, errp);
2150 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
2152 static int monitor_device_index = 0;
2157 if (strstart(optarg, "chardev:", &p)) {
2158 snprintf(label, sizeof(label), "%s", p);
2160 snprintf(label, sizeof(label), "compat_monitor%d",
2161 monitor_device_index);
2162 opts = qemu_chr_parse_compat(label, optarg, true);
2164 error_report("parse error: %s", optarg);
2169 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
2170 qemu_opt_set(opts, "mode", mode, &error_abort);
2171 qemu_opt_set(opts, "chardev", label, &error_abort);
2172 if (!strcmp(mode, "control")) {
2173 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
2175 assert(pretty == false);
2177 monitor_device_index++;
2180 struct device_config {
2182 DEV_USB, /* -usbdevice */
2183 DEV_SERIAL, /* -serial */
2184 DEV_PARALLEL, /* -parallel */
2185 DEV_DEBUGCON, /* -debugcon */
2186 DEV_GDB, /* -gdb, -s */
2187 DEV_SCLP, /* s390 sclp */
2189 const char *cmdline;
2191 QTAILQ_ENTRY(device_config) next;
2194 static QTAILQ_HEAD(, device_config) device_configs =
2195 QTAILQ_HEAD_INITIALIZER(device_configs);
2197 static void add_device_config(int type, const char *cmdline)
2199 struct device_config *conf;
2201 conf = g_malloc0(sizeof(*conf));
2203 conf->cmdline = cmdline;
2204 loc_save(&conf->loc);
2205 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2208 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2210 struct device_config *conf;
2213 QTAILQ_FOREACH(conf, &device_configs, next) {
2214 if (conf->type != type)
2216 loc_push_restore(&conf->loc);
2217 rc = func(conf->cmdline);
2218 loc_pop(&conf->loc);
2226 static int serial_parse(const char *devname)
2228 int index = num_serial_hds;
2231 if (strcmp(devname, "none") == 0)
2233 snprintf(label, sizeof(label), "serial%d", index);
2234 serial_hds = g_renew(Chardev *, serial_hds, index + 1);
2236 serial_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
2237 if (!serial_hds[index]) {
2238 error_report("could not connect serial device"
2239 " to character backend '%s'", devname);
2246 Chardev *serial_hd(int i)
2249 if (i < num_serial_hds) {
2250 return serial_hds[i];
2255 int serial_max_hds(void)
2257 return num_serial_hds;
2260 static int parallel_parse(const char *devname)
2262 static int index = 0;
2265 if (strcmp(devname, "none") == 0)
2267 if (index == MAX_PARALLEL_PORTS) {
2268 error_report("too many parallel ports");
2271 snprintf(label, sizeof(label), "parallel%d", index);
2272 parallel_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
2273 if (!parallel_hds[index]) {
2274 error_report("could not connect parallel device"
2275 " to character backend '%s'", devname);
2282 static int debugcon_parse(const char *devname)
2286 if (!qemu_chr_new_mux_mon("debugcon", devname, NULL)) {
2287 error_report("invalid character backend '%s'", devname);
2290 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2292 error_report("already have a debugcon device");
2295 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
2296 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
2300 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
2302 const MachineClass *mc1 = a, *mc2 = b;
2305 if (mc1->family == NULL) {
2306 if (mc2->family == NULL) {
2307 /* Compare standalone machine types against each other; they sort
2308 * in increasing order.
2310 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
2311 object_class_get_name(OBJECT_CLASS(mc2)));
2314 /* Standalone machine types sort after families. */
2318 if (mc2->family == NULL) {
2319 /* Families sort before standalone machine types. */
2323 /* Families sort between each other alphabetically increasingly. */
2324 res = strcmp(mc1->family, mc2->family);
2329 /* Within the same family, machine types sort in decreasing order. */
2330 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
2331 object_class_get_name(OBJECT_CLASS(mc1)));
2334 static MachineClass *machine_parse(const char *name, GSList *machines)
2339 if (is_help_option(name)) {
2340 printf("Supported machines are:\n");
2341 machines = g_slist_sort(machines, machine_class_cmp);
2342 for (el = machines; el; el = el->next) {
2343 MachineClass *mc = el->data;
2345 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
2347 printf("%-20s %s%s%s\n", mc->name, mc->desc,
2348 mc->is_default ? " (default)" : "",
2349 mc->deprecation_reason ? " (deprecated)" : "");
2354 mc = find_machine(name, machines);
2356 error_report("unsupported machine type");
2357 error_printf("Use -machine help to list supported machines\n");
2363 void qemu_add_exit_notifier(Notifier *notify)
2365 notifier_list_add(&exit_notifiers, notify);
2368 void qemu_remove_exit_notifier(Notifier *notify)
2370 notifier_remove(notify);
2373 static void qemu_run_exit_notifiers(void)
2375 notifier_list_notify(&exit_notifiers, NULL);
2378 static const char *pid_file;
2379 static Notifier qemu_unlink_pidfile_notifier;
2381 static void qemu_unlink_pidfile(Notifier *n, void *data)
2388 bool machine_init_done;
2390 void qemu_add_machine_init_done_notifier(Notifier *notify)
2392 notifier_list_add(&machine_init_done_notifiers, notify);
2393 if (machine_init_done) {
2394 notify->notify(notify, NULL);
2398 void qemu_remove_machine_init_done_notifier(Notifier *notify)
2400 notifier_remove(notify);
2403 static void qemu_run_machine_init_done_notifiers(void)
2405 machine_init_done = true;
2406 notifier_list_notify(&machine_init_done_notifiers, NULL);
2409 static const QEMUOption *lookup_opt(int argc, char **argv,
2410 const char **poptarg, int *poptind)
2412 const QEMUOption *popt;
2413 int optind = *poptind;
2414 char *r = argv[optind];
2417 loc_set_cmdline(argv, optind, 1);
2419 /* Treat --foo the same as -foo. */
2422 popt = qemu_options;
2425 error_report("invalid option");
2428 if (!strcmp(popt->name, r + 1))
2432 if (popt->flags & HAS_ARG) {
2433 if (optind >= argc) {
2434 error_report("requires an argument");
2437 optarg = argv[optind++];
2438 loc_set_cmdline(argv, optind - 2, 2);
2449 static MachineClass *select_machine(void)
2451 GSList *machines = object_class_get_list(TYPE_MACHINE, false);
2452 MachineClass *machine_class = find_default_machine(machines);
2457 loc_push_none(&loc);
2459 opts = qemu_get_machine_opts();
2460 qemu_opts_loc_restore(opts);
2462 optarg = qemu_opt_get(opts, "type");
2464 machine_class = machine_parse(optarg, machines);
2467 if (!machine_class) {
2468 error_report("No machine specified, and there is no default");
2469 error_printf("Use -machine help to list supported machines\n");
2474 g_slist_free(machines);
2475 return machine_class;
2478 static int object_parse_property_opt(Object *obj,
2479 const char *name, const char *value,
2480 const char *skip, Error **errp)
2482 if (g_str_equal(name, skip)) {
2486 if (!object_property_parse(obj, name, value, errp)) {
2493 static int machine_set_property(void *opaque,
2494 const char *name, const char *value,
2497 g_autofree char *qom_name = g_strdup(name);
2500 for (p = qom_name; *p; p++) {
2506 /* Legacy options do not correspond to MachineState properties. */
2507 if (g_str_equal(qom_name, "accel")) {
2510 if (g_str_equal(qom_name, "igd-passthru")) {
2511 object_register_sugar_prop(ACCEL_CLASS_NAME("xen"), qom_name, value);
2514 if (g_str_equal(qom_name, "kvm-shadow-mem")) {
2515 object_register_sugar_prop(ACCEL_CLASS_NAME("kvm"), qom_name, value);
2518 if (g_str_equal(qom_name, "kernel-irqchip")) {
2519 object_register_sugar_prop(ACCEL_CLASS_NAME("kvm"), qom_name, value);
2520 object_register_sugar_prop(ACCEL_CLASS_NAME("whpx"), qom_name, value);
2524 return object_parse_property_opt(opaque, name, value, "type", errp);
2528 * Initial object creation happens before all other
2529 * QEMU data types are created. The majority of objects
2530 * can be created at this point. The rng-egd object
2531 * cannot be created here, as it depends on the chardev
2534 static bool object_create_initial(const char *type, QemuOpts *opts)
2536 if (user_creatable_print_help(type, opts)) {
2541 * Objects should not be made "delayed" without a reason. If you
2542 * add one, state the reason in a comment!
2545 /* Reason: rng-egd property "chardev" */
2546 if (g_str_equal(type, "rng-egd")) {
2550 #if defined(CONFIG_VHOST_USER) && defined(CONFIG_LINUX)
2551 /* Reason: cryptodev-vhost-user property "chardev" */
2552 if (g_str_equal(type, "cryptodev-vhost-user")) {
2557 /* Reason: vhost-user-blk-server property "node-name" */
2558 if (g_str_equal(type, "vhost-user-blk-server")) {
2562 * Reason: filter-* property "netdev" etc.
2564 if (g_str_equal(type, "filter-buffer") ||
2565 g_str_equal(type, "filter-dump") ||
2566 g_str_equal(type, "filter-mirror") ||
2567 g_str_equal(type, "filter-redirector") ||
2568 g_str_equal(type, "colo-compare") ||
2569 g_str_equal(type, "filter-rewriter") ||
2570 g_str_equal(type, "filter-replay")) {
2574 /* Memory allocation by backends needs to be done
2575 * after configure_accelerator() (due to the tcg_enabled()
2576 * checks at memory_region_init_*()).
2578 * Also, allocation of large amounts of memory may delay
2579 * chardev initialization for too long, and trigger timeouts
2580 * on software that waits for a monitor socket to be created
2583 if (g_str_has_prefix(type, "memory-backend-")) {
2592 * The remainder of object creation happens after the
2593 * creation of chardev, fsdev, net clients and device data types.
2595 static bool object_create_delayed(const char *type, QemuOpts *opts)
2597 return !object_create_initial(type, opts);
2601 static bool set_memory_options(uint64_t *ram_slots, ram_addr_t *maxram_size,
2605 const char *mem_str;
2606 const ram_addr_t default_ram_size = mc->default_ram_size;
2607 QemuOpts *opts = qemu_find_opts_singleton("memory");
2610 loc_push_none(&loc);
2611 qemu_opts_loc_restore(opts);
2614 mem_str = qemu_opt_get(opts, "size");
2617 error_report("missing 'size' option value");
2621 sz = qemu_opt_get_size(opts, "size", ram_size);
2623 /* Fix up legacy suffix-less format */
2624 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2625 uint64_t overflow_check = sz;
2628 if (sz / MiB != overflow_check) {
2629 error_report("too large 'size' option value");
2635 /* backward compatibility behaviour for case "-m 0" */
2637 sz = default_ram_size;
2640 sz = QEMU_ALIGN_UP(sz, 8192);
2641 if (mc->fixup_ram_size) {
2642 sz = mc->fixup_ram_size(sz);
2645 if (ram_size != sz) {
2646 error_report("ram size too large");
2650 /* store value for the future use */
2651 qemu_opt_set_number(opts, "size", ram_size, &error_abort);
2652 *maxram_size = ram_size;
2654 if (qemu_opt_get(opts, "maxmem")) {
2657 sz = qemu_opt_get_size(opts, "maxmem", 0);
2658 slots = qemu_opt_get_number(opts, "slots", 0);
2659 if (sz < ram_size) {
2660 error_report("invalid value of -m option maxmem: "
2661 "maximum memory size (0x%" PRIx64 ") must be at least "
2662 "the initial memory size (0x" RAM_ADDR_FMT ")",
2665 } else if (slots && sz == ram_size) {
2666 error_report("invalid value of -m option maxmem: "
2667 "memory slots were specified but maximum memory size "
2668 "(0x%" PRIx64 ") is equal to the initial memory size "
2669 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
2675 } else if (qemu_opt_get(opts, "slots")) {
2676 error_report("invalid -m option value: missing 'maxmem' option");
2684 static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
2688 g = g_malloc0(sizeof(*g));
2689 g->driver = qemu_opt_get(opts, "driver");
2690 g->property = qemu_opt_get(opts, "property");
2691 g->value = qemu_opt_get(opts, "value");
2692 qdev_prop_register_global(g);
2696 static int qemu_read_default_config_file(void)
2699 g_autofree char *file = get_relocated_path(CONFIG_QEMU_CONFDIR "/qemu.conf");
2701 ret = qemu_read_config_file(file);
2702 if (ret < 0 && ret != -ENOENT) {
2709 static void user_register_global_props(void)
2711 qemu_opts_foreach(qemu_find_opts("global"),
2712 global_init_func, NULL, NULL);
2715 static int do_configure_icount(void *opaque, QemuOpts *opts, Error **errp)
2717 icount_configure(opts, errp);
2721 static int accelerator_set_property(void *opaque,
2722 const char *name, const char *value,
2725 return object_parse_property_opt(opaque, name, value, "accel", errp);
2728 static int do_configure_accelerator(void *opaque, QemuOpts *opts, Error **errp)
2730 bool *p_init_failed = opaque;
2731 const char *acc = qemu_opt_get(opts, "accel");
2732 AccelClass *ac = accel_find(acc);
2735 bool qtest_with_kvm;
2737 qtest_with_kvm = g_str_equal(acc, "kvm") && qtest_chrdev != NULL;
2740 *p_init_failed = true;
2741 if (!qtest_with_kvm) {
2742 error_report("invalid accelerator %s", acc);
2746 accel = ACCEL(object_new_with_class(OBJECT_CLASS(ac)));
2747 object_apply_compat_props(OBJECT(accel));
2748 qemu_opt_foreach(opts, accelerator_set_property,
2752 ret = accel_init_machine(accel, current_machine);
2754 *p_init_failed = true;
2755 if (!qtest_with_kvm || ret != -ENOENT) {
2756 error_report("failed to initialize %s: %s", acc, strerror(-ret));
2764 static void configure_accelerators(const char *progname)
2767 bool init_failed = false;
2769 qemu_opts_foreach(qemu_find_opts("icount"),
2770 do_configure_icount, NULL, &error_fatal);
2772 accel = qemu_opt_get(qemu_get_machine_opts(), "accel");
2773 if (QTAILQ_EMPTY(&qemu_accel_opts.head)) {
2774 char **accel_list, **tmp;
2776 if (accel == NULL) {
2777 /* Select the default accelerator */
2778 bool have_tcg = accel_find("tcg");
2779 bool have_kvm = accel_find("kvm");
2781 if (have_tcg && have_kvm) {
2782 if (g_str_has_suffix(progname, "kvm")) {
2783 /* If the program name ends with "kvm", we prefer KVM */
2788 } else if (have_kvm) {
2790 } else if (have_tcg) {
2793 error_report("No accelerator selected and"
2794 " no default accelerator available");
2798 accel_list = g_strsplit(accel, ":", 0);
2800 for (tmp = accel_list; *tmp; tmp++) {
2802 * Filter invalid accelerators here, to prevent obscenities
2803 * such as "-machine accel=tcg,,thread=single".
2805 if (accel_find(*tmp)) {
2806 qemu_opts_parse_noisily(qemu_find_opts("accel"), *tmp, true);
2809 error_report("invalid accelerator %s", *tmp);
2812 g_strfreev(accel_list);
2814 if (accel != NULL) {
2815 error_report("The -accel and \"-machine accel=\" options are incompatible");
2820 if (!qemu_opts_foreach(qemu_find_opts("accel"),
2821 do_configure_accelerator, &init_failed, &error_fatal)) {
2823 error_report("no accelerator found");
2828 if (init_failed && !qtest_chrdev) {
2829 AccelClass *ac = ACCEL_GET_CLASS(current_accel());
2830 error_report("falling back to %s", ac->name);
2833 if (icount_enabled() && !tcg_enabled()) {
2834 error_report("-icount is not allowed with hardware virtualization");
2839 static void create_default_memdev(MachineState *ms, const char *path)
2842 MachineClass *mc = MACHINE_GET_CLASS(ms);
2844 obj = object_new(path ? TYPE_MEMORY_BACKEND_FILE : TYPE_MEMORY_BACKEND_RAM);
2846 object_property_set_str(obj, "mem-path", path, &error_fatal);
2848 object_property_set_int(obj, "size", ms->ram_size, &error_fatal);
2849 object_property_add_child(object_get_objects_root(), mc->default_ram_id,
2851 /* Ensure backend's memory region name is equal to mc->default_ram_id */
2852 object_property_set_bool(obj, "x-use-canonical-path-for-ramblock-id",
2853 false, &error_fatal);
2854 user_creatable_complete(USER_CREATABLE(obj), &error_fatal);
2856 object_property_set_str(OBJECT(ms), "memory-backend", mc->default_ram_id,
2861 * Find a likely location for support files using the location of the binary.
2862 * When running from the build tree this will be "$bindir/pc-bios".
2863 * Otherwise, this is CONFIG_QEMU_DATADIR (possibly relocated).
2865 * The caller must use g_free() to free the returned data when it is
2866 * no longer required.
2868 static char *find_datadir(void)
2870 g_autofree char *dir = NULL;
2872 dir = g_build_filename(qemu_get_exec_dir(), "pc-bios", NULL);
2873 if (g_file_test(dir, G_FILE_TEST_IS_DIR)) {
2874 return g_steal_pointer(&dir);
2877 return get_relocated_path(CONFIG_QEMU_DATADIR);
2880 static void qemu_validate_options(void)
2882 QemuOpts *machine_opts = qemu_get_machine_opts();
2883 const char *kernel_filename = qemu_opt_get(machine_opts, "kernel");
2884 const char *initrd_filename = qemu_opt_get(machine_opts, "initrd");
2885 const char *kernel_cmdline = qemu_opt_get(machine_opts, "append");
2887 if (kernel_filename == NULL) {
2888 if (kernel_cmdline != NULL) {
2889 error_report("-append only allowed with -kernel option");
2893 if (initrd_filename != NULL) {
2894 error_report("-initrd only allowed with -kernel option");
2899 if (loadvm && !preconfig_exit_requested) {
2900 error_report("'preconfig' and 'loadvm' options are "
2901 "mutually exclusive");
2904 if (incoming && !preconfig_exit_requested) {
2905 error_report("'preconfig' and 'incoming' options are "
2906 "mutually exclusive");
2910 #ifdef CONFIG_CURSES
2911 if (is_daemonized() && dpy.type == DISPLAY_TYPE_CURSES) {
2912 error_report("curses display cannot be used with -daemonize");
2918 static void qemu_process_sugar_options(void)
2923 val = g_strdup_printf("%d",
2924 (uint32_t) qemu_opt_get_number(qemu_find_opts_singleton("smp-opts"), "cpus", 1));
2925 object_register_sugar_prop("memory-backend", "prealloc-threads", val);
2927 object_register_sugar_prop("memory-backend", "prealloc", "on");
2931 int i = select_watchdog(watchdog);
2933 exit (i == 1 ? 1 : 0);
2937 static void qemu_process_early_options(void)
2942 #ifdef CONFIG_SECCOMP
2943 QemuOptsList *olist = qemu_find_opts_err("sandbox", NULL);
2945 qemu_opts_foreach(olist, parse_sandbox, NULL, &error_fatal);
2949 qemu_opts_foreach(qemu_find_opts("name"),
2950 parse_name, NULL, &error_fatal);
2953 qemu_opts_foreach(qemu_find_opts("add-fd"),
2954 parse_add_fd, NULL, &error_fatal);
2956 qemu_opts_foreach(qemu_find_opts("add-fd"),
2957 cleanup_add_fd, NULL, &error_fatal);
2960 if (!trace_init_backends()) {
2965 /* Open the logfile at this point and set the log mask if necessary. */
2966 qemu_set_log_filename(log_file, &error_fatal);
2969 mask = qemu_str_to_log_mask(log_mask);
2971 qemu_print_log_usage(stdout);
2979 /* add configured firmware directories */
2980 dirs = g_strsplit(CONFIG_QEMU_FIRMWAREPATH, G_SEARCHPATH_SEPARATOR_S, 0);
2981 for (i = 0; dirs[i] != NULL; i++) {
2982 qemu_add_data_dir(get_relocated_path(dirs[i]));
2986 /* try to find datadir relative to the executable path */
2987 qemu_add_data_dir(find_datadir());
2990 static void qemu_process_help_options(void)
2995 * Check for -cpu help and -device help before we call select_machine(),
2996 * which will return an error if the architecture has no default machine
2997 * type and the user did not specify one, so that the user doesn't need
2998 * to say '-cpu help -machine something'.
3000 if (cpu_option && is_help_option(cpu_option)) {
3001 list_cpus(cpu_option);
3005 if (qemu_opts_foreach(qemu_find_opts("device"),
3006 device_help_func, NULL, NULL)) {
3010 /* -L help lists the data directories and exits. */
3011 if (list_data_dirs) {
3012 for (i = 0; i < data_dir_idx; i++) {
3013 printf("%s\n", data_dir[i]);
3019 static void qemu_maybe_daemonize(const char *pid_file)
3024 rcu_disable_atfork();
3026 if (pid_file && !qemu_write_pidfile(pid_file, &err)) {
3027 error_reportf_err(err, "cannot create PID file: ");
3031 qemu_unlink_pidfile_notifier.notify = qemu_unlink_pidfile;
3032 qemu_add_exit_notifier(&qemu_unlink_pidfile_notifier);
3035 static void qemu_init_subsystems(void)
3039 os_set_line_buffering();
3041 module_call_init(MODULE_INIT_TRACE);
3043 qemu_init_cpu_list();
3044 qemu_init_cpu_loop();
3045 qemu_mutex_lock_iothread();
3047 atexit(qemu_run_exit_notifiers);
3049 module_call_init(MODULE_INIT_QOM);
3050 module_call_init(MODULE_INIT_MIGRATION);
3053 precopy_infrastructure_init();
3054 postcopy_infrastructure_init();
3055 monitor_init_globals();
3057 if (qcrypto_init(&err) < 0) {
3058 error_reportf_err(err, "cannot initialize crypto: ");
3062 os_setup_early_signal_handling();
3064 bdrv_init_with_whitelist();
3069 * Called after leaving preconfig state. From here on runstate is
3070 * RUN_STATE_PRELAUNCH or RUN_STATE_INMIGRATE.
3072 static void qemu_init_board(void)
3074 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
3076 if (machine_class->default_ram_id && current_machine->ram_size &&
3077 numa_uses_legacy_mem() && !current_machine->ram_memdev_id) {
3078 create_default_memdev(current_machine, mem_path);
3081 machine_run_board_init(current_machine);
3084 * TODO To drop support for deprecated bogus if=..., move
3085 * drive_check_orphaned() here, replacing this call. Also drop
3086 * its deprecation warning, along with DriveInfo member
3087 * @claimed_by_board.
3089 drive_mark_claimed_by_board();
3093 if (hax_enabled()) {
3094 /* FIXME: why isn't cpu_synchronize_all_post_init enough? */
3099 static void qemu_create_cli_devices(void)
3103 qemu_opts_foreach(qemu_find_opts("fw_cfg"),
3104 parse_fw_cfg, fw_cfg_find(), &error_fatal);
3106 /* init USB devices */
3107 if (machine_usb(current_machine)) {
3108 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3112 /* init generic devices */
3113 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
3114 qemu_opts_foreach(qemu_find_opts("device"),
3115 device_init_func, NULL, &error_fatal);
3116 rom_reset_order_override();
3119 static void qemu_machine_creation_done(void)
3123 cpu_synchronize_all_post_init();
3125 /* Did we create any drives that we failed to create a device for? */
3126 drive_check_orphaned();
3128 /* Don't warn about the default network setup that you get if
3129 * no command line -net or -netdev options are specified. There
3130 * are two cases that we would otherwise complain about:
3131 * (1) board doesn't support a NIC but the implicit "-net nic"
3133 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
3134 * sets up a nic that isn't connected to anything.
3136 if (!default_net && (!qtest_enabled() || has_defaults)) {
3137 net_check_clients();
3141 qemu_boot_set(boot_once, &error_fatal);
3142 qemu_register_reset(restore_boot_order, g_strdup(boot_order));
3145 /* init local displays */
3146 ds = init_displaystate();
3147 qemu_display_init(ds, &dpy);
3149 /* must be after terminal init, SDL library changes signal handlers */
3150 os_setup_signal_handling();
3152 /* init remote displays */
3154 qemu_opts_foreach(qemu_find_opts("vnc"),
3155 vnc_init_func, NULL, &error_fatal);
3159 qemu_spice.display_init();
3162 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
3166 qdev_machine_creation_done();
3168 /* TODO: once all bus devices are qdevified, this should be done
3169 * when bus is created by qdev.c */
3171 * TODO: If we had a main 'reset container' that the whole system
3172 * lived in, we could reset that using the multi-phase reset
3173 * APIs. For the moment, we just reset the sysbus, which will cause
3174 * all devices hanging off it (and all their child buses, recursively)
3175 * to be reset. Note that this will *not* reset any Device objects
3176 * which are not attached to some part of the qbus tree!
3178 qemu_register_reset(resettable_cold_reset_fn, sysbus_get_default());
3179 qemu_run_machine_init_done_notifiers();
3181 if (rom_check_and_register_reset() != 0) {
3182 error_report("rom check and register reset failed");
3188 /* This checkpoint is required by replay to separate prior clock
3189 reading from the other reads, because timer polling functions query
3190 clock values from the log. */
3191 replay_checkpoint(CHECKPOINT_RESET);
3192 qemu_system_reset(SHUTDOWN_CAUSE_NONE);
3193 register_global_state();
3196 void qemu_init(int argc, char **argv, char **envp)
3199 QemuOpts *opts, *machine_opts;
3200 QemuOpts *icount_opts = NULL, *accel_opts = NULL;
3201 QemuOptsList *olist;
3204 MachineClass *machine_class;
3205 const char *vga_model = NULL;
3206 bool userconfig = true;
3207 bool nographic = false;
3208 int display_remote = 0;
3209 ram_addr_t maxram_size;
3210 uint64_t ram_slots = 0;
3211 FILE *vmstate_dump_file = NULL;
3213 bool have_custom_ram_size;
3214 BlockdevOptionsQueue bdo_queue = QSIMPLEQ_HEAD_INITIALIZER(bdo_queue);
3215 QemuPluginList plugin_list = QTAILQ_HEAD_INITIALIZER(plugin_list);
3217 qemu_add_opts(&qemu_drive_opts);
3218 qemu_add_drive_opts(&qemu_legacy_drive_opts);
3219 qemu_add_drive_opts(&qemu_common_drive_opts);
3220 qemu_add_drive_opts(&qemu_drive_opts);
3221 qemu_add_drive_opts(&bdrv_runtime_opts);
3222 qemu_add_opts(&qemu_chardev_opts);
3223 qemu_add_opts(&qemu_device_opts);
3224 qemu_add_opts(&qemu_netdev_opts);
3225 qemu_add_opts(&qemu_nic_opts);
3226 qemu_add_opts(&qemu_net_opts);
3227 qemu_add_opts(&qemu_rtc_opts);
3228 qemu_add_opts(&qemu_global_opts);
3229 qemu_add_opts(&qemu_mon_opts);
3230 qemu_add_opts(&qemu_trace_opts);
3231 qemu_plugin_add_opts();
3232 qemu_add_opts(&qemu_option_rom_opts);
3233 qemu_add_opts(&qemu_machine_opts);
3234 qemu_add_opts(&qemu_accel_opts);
3235 qemu_add_opts(&qemu_mem_opts);
3236 qemu_add_opts(&qemu_smp_opts);
3237 qemu_add_opts(&qemu_boot_opts);
3238 qemu_add_opts(&qemu_add_fd_opts);
3239 qemu_add_opts(&qemu_object_opts);
3240 qemu_add_opts(&qemu_tpmdev_opts);
3241 qemu_add_opts(&qemu_realtime_opts);
3242 qemu_add_opts(&qemu_overcommit_opts);
3243 qemu_add_opts(&qemu_msg_opts);
3244 qemu_add_opts(&qemu_name_opts);
3245 qemu_add_opts(&qemu_numa_opts);
3246 qemu_add_opts(&qemu_icount_opts);
3247 qemu_add_opts(&qemu_semihosting_config_opts);
3248 qemu_add_opts(&qemu_fw_cfg_opts);
3249 module_call_init(MODULE_INIT_OPTS);
3251 error_init(argv[0]);
3252 qemu_init_exec_dir(argv[0]);
3254 qemu_init_subsystems();
3256 /* first pass of option parsing */
3258 while (optind < argc) {
3259 if (argv[optind][0] != '-') {
3263 const QEMUOption *popt;
3265 popt = lookup_opt(argc, argv, &optarg, &optind);
3266 switch (popt->index) {
3267 case QEMU_OPTION_nouserconfig:
3275 if (qemu_read_default_config_file() < 0) {
3280 /* second pass of option parsing */
3285 if (argv[optind][0] != '-') {
3286 loc_set_cmdline(argv, optind, 1);
3287 drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
3289 const QEMUOption *popt;
3291 popt = lookup_opt(argc, argv, &optarg, &optind);
3292 if (!(popt->arch_mask & arch_type)) {
3293 error_report("Option not supported for this target");
3296 switch(popt->index) {
3297 case QEMU_OPTION_cpu:
3298 /* hw initialization will check this */
3299 cpu_option = optarg;
3301 case QEMU_OPTION_hda:
3302 case QEMU_OPTION_hdb:
3303 case QEMU_OPTION_hdc:
3304 case QEMU_OPTION_hdd:
3305 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
3308 case QEMU_OPTION_blockdev:
3311 BlockdevOptionsQueueEntry *bdo;
3313 v = qobject_input_visitor_new_str(optarg, "driver",
3316 bdo = g_new(BlockdevOptionsQueueEntry, 1);
3317 visit_type_BlockdevOptions(v, NULL, &bdo->bdo,
3320 loc_save(&bdo->loc);
3321 QSIMPLEQ_INSERT_TAIL(&bdo_queue, bdo, entry);
3324 case QEMU_OPTION_drive:
3325 if (drive_def(optarg) == NULL) {
3329 case QEMU_OPTION_set:
3330 if (qemu_set_option(optarg) != 0)
3333 case QEMU_OPTION_global:
3334 if (qemu_global_option(optarg) != 0)
3337 case QEMU_OPTION_mtdblock:
3338 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3340 case QEMU_OPTION_sd:
3341 drive_add(IF_SD, -1, optarg, SD_OPTS);
3343 case QEMU_OPTION_pflash:
3344 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
3346 case QEMU_OPTION_snapshot:
3348 Error *blocker = NULL;
3350 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
3352 replay_add_blocker(blocker);
3355 case QEMU_OPTION_numa:
3356 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
3362 case QEMU_OPTION_display:
3363 parse_display(optarg);
3365 case QEMU_OPTION_nographic:
3366 olist = qemu_find_opts("machine");
3367 qemu_opts_parse_noisily(olist, "graphics=off", false);
3369 dpy.type = DISPLAY_TYPE_NONE;
3371 case QEMU_OPTION_curses:
3372 #ifdef CONFIG_CURSES
3373 dpy.type = DISPLAY_TYPE_CURSES;
3375 error_report("curses or iconv support is disabled");
3379 case QEMU_OPTION_portrait:
3380 graphic_rotate = 90;
3382 case QEMU_OPTION_rotate:
3383 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3384 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3385 graphic_rotate != 180 && graphic_rotate != 270) {
3386 error_report("only 90, 180, 270 deg rotation is available");
3390 case QEMU_OPTION_kernel:
3391 qemu_opts_set(qemu_find_opts("machine"), "kernel", optarg, &error_abort);
3393 case QEMU_OPTION_initrd:
3394 qemu_opts_set(qemu_find_opts("machine"), "initrd", optarg, &error_abort);
3396 case QEMU_OPTION_append:
3397 qemu_opts_set(qemu_find_opts("machine"), "append", optarg, &error_abort);
3399 case QEMU_OPTION_dtb:
3400 qemu_opts_set(qemu_find_opts("machine"), "dtb", optarg, &error_abort);
3402 case QEMU_OPTION_cdrom:
3403 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3405 case QEMU_OPTION_boot:
3406 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
3412 case QEMU_OPTION_fda:
3413 case QEMU_OPTION_fdb:
3414 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3417 case QEMU_OPTION_no_fd_bootchk:
3420 case QEMU_OPTION_netdev:
3422 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3426 case QEMU_OPTION_nic:
3428 if (net_client_parse(qemu_find_opts("nic"), optarg) == -1) {
3432 case QEMU_OPTION_net:
3434 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3438 #ifdef CONFIG_LIBISCSI
3439 case QEMU_OPTION_iscsi:
3440 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
3447 case QEMU_OPTION_audio_help:
3448 audio_legacy_help();
3451 case QEMU_OPTION_audiodev:
3452 audio_parse_option(optarg);
3454 case QEMU_OPTION_soundhw:
3455 select_soundhw (optarg);
3460 case QEMU_OPTION_version:
3465 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
3472 case QEMU_OPTION_tpmdev:
3473 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3478 case QEMU_OPTION_mempath:
3481 case QEMU_OPTION_mem_prealloc:
3490 case QEMU_OPTION_DFILTER:
3491 qemu_set_dfilter_ranges(optarg, &error_fatal);
3493 case QEMU_OPTION_seed:
3494 qemu_guest_random_seed_main(optarg, &error_fatal);
3497 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3499 case QEMU_OPTION_gdb:
3500 add_device_config(DEV_GDB, optarg);
3503 if (is_help_option(optarg)) {
3504 list_data_dirs = true;
3506 qemu_add_data_dir(g_strdup(optarg));
3509 case QEMU_OPTION_bios:
3510 qemu_opts_set(qemu_find_opts("machine"), "firmware", optarg, &error_abort);
3512 case QEMU_OPTION_singlestep:
3519 keyboard_layout = optarg;
3521 case QEMU_OPTION_vga:
3530 w = strtol(p, (char **)&p, 10);
3533 error_report("invalid resolution or depth");
3539 h = strtol(p, (char **)&p, 10);
3544 depth = strtol(p, (char **)&p, 10);
3545 if (depth != 1 && depth != 2 && depth != 4 &&
3546 depth != 8 && depth != 15 && depth != 16 &&
3547 depth != 24 && depth != 32)
3549 } else if (*p == '\0') {
3550 depth = graphic_depth;
3557 graphic_depth = depth;
3560 case QEMU_OPTION_echr:
3563 term_escape_char = strtol(optarg, &r, 0);
3565 printf("Bad argument to echr\n");
3568 case QEMU_OPTION_monitor:
3569 default_monitor = 0;
3570 if (strncmp(optarg, "none", 4)) {
3571 monitor_parse(optarg, "readline", false);
3574 case QEMU_OPTION_qmp:
3575 monitor_parse(optarg, "control", false);
3576 default_monitor = 0;
3578 case QEMU_OPTION_qmp_pretty:
3579 monitor_parse(optarg, "control", true);
3580 default_monitor = 0;
3582 case QEMU_OPTION_mon:
3583 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3588 default_monitor = 0;
3590 case QEMU_OPTION_chardev:
3591 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3597 case QEMU_OPTION_fsdev:
3598 olist = qemu_find_opts("fsdev");
3600 error_report("fsdev support is disabled");
3603 opts = qemu_opts_parse_noisily(olist, optarg, true);
3608 case QEMU_OPTION_virtfs: {
3611 const char *writeout, *sock_fd, *socket, *path, *security_model,
3614 olist = qemu_find_opts("virtfs");
3616 error_report("virtfs support is disabled");
3619 opts = qemu_opts_parse_noisily(olist, optarg, true);
3624 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3625 qemu_opt_get(opts, "mount_tag") == NULL) {
3626 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3629 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3630 qemu_opts_id(opts) ?:
3631 qemu_opt_get(opts, "mount_tag"),
3634 error_report("duplicate or invalid fsdev id: %s",
3635 qemu_opt_get(opts, "mount_tag"));
3639 writeout = qemu_opt_get(opts, "writeout");
3641 #ifdef CONFIG_SYNC_FILE_RANGE
3642 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3644 error_report("writeout=immediate not supported "
3645 "on this platform");
3649 qemu_opt_set(fsdev, "fsdriver",
3650 qemu_opt_get(opts, "fsdriver"), &error_abort);
3651 path = qemu_opt_get(opts, "path");
3653 qemu_opt_set(fsdev, "path", path, &error_abort);
3655 security_model = qemu_opt_get(opts, "security_model");
3656 if (security_model) {
3657 qemu_opt_set(fsdev, "security_model", security_model,
3660 socket = qemu_opt_get(opts, "socket");
3662 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3664 sock_fd = qemu_opt_get(opts, "sock_fd");
3666 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3669 qemu_opt_set_bool(fsdev, "readonly",
3670 qemu_opt_get_bool(opts, "readonly", 0),
3672 multidevs = qemu_opt_get(opts, "multidevs");
3674 qemu_opt_set(fsdev, "multidevs", multidevs, &error_abort);
3676 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3678 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3679 qemu_opt_set(device, "fsdev",
3680 qemu_opts_id(fsdev), &error_abort);
3681 qemu_opt_set(device, "mount_tag",
3682 qemu_opt_get(opts, "mount_tag"), &error_abort);
3685 case QEMU_OPTION_serial:
3686 add_device_config(DEV_SERIAL, optarg);
3688 if (strncmp(optarg, "mon:", 4) == 0) {
3689 default_monitor = 0;
3692 case QEMU_OPTION_watchdog:
3694 error_report("only one watchdog option may be given");
3699 case QEMU_OPTION_watchdog_action:
3700 if (select_watchdog_action(optarg) == -1) {
3701 error_report("unknown -watchdog-action parameter");
3705 case QEMU_OPTION_parallel:
3706 add_device_config(DEV_PARALLEL, optarg);
3707 default_parallel = 0;
3708 if (strncmp(optarg, "mon:", 4) == 0) {
3709 default_monitor = 0;
3712 case QEMU_OPTION_debugcon:
3713 add_device_config(DEV_DEBUGCON, optarg);
3715 case QEMU_OPTION_loadvm:
3718 case QEMU_OPTION_full_screen:
3719 dpy.has_full_screen = true;
3720 dpy.full_screen = true;
3722 case QEMU_OPTION_alt_grab:
3725 case QEMU_OPTION_ctrl_grab:
3728 case QEMU_OPTION_no_quit:
3729 dpy.has_window_close = true;
3730 dpy.window_close = false;
3732 case QEMU_OPTION_sdl:
3734 dpy.type = DISPLAY_TYPE_SDL;
3737 error_report("SDL support is disabled");
3740 case QEMU_OPTION_pidfile:
3743 case QEMU_OPTION_win2k_hack:
3744 win2k_install_hack = 1;
3746 case QEMU_OPTION_acpitable:
3747 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3752 acpi_table_add(opts, &error_fatal);
3754 case QEMU_OPTION_smbios:
3755 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3760 smbios_entry_add(opts, &error_fatal);
3762 case QEMU_OPTION_fwcfg:
3763 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3769 case QEMU_OPTION_preconfig:
3770 preconfig_exit_requested = false;
3772 case QEMU_OPTION_enable_kvm:
3773 olist = qemu_find_opts("machine");
3774 qemu_opts_parse_noisily(olist, "accel=kvm", false);
3777 case QEMU_OPTION_machine:
3778 olist = qemu_find_opts("machine");
3779 opts = qemu_opts_parse_noisily(olist, optarg, true);
3784 case QEMU_OPTION_accel:
3785 accel_opts = qemu_opts_parse_noisily(qemu_find_opts("accel"),
3787 optarg = qemu_opt_get(accel_opts, "accel");
3788 if (!optarg || is_help_option(optarg)) {
3789 printf("Accelerators supported in QEMU binary:\n");
3790 GSList *el, *accel_list = object_class_get_list(TYPE_ACCEL,
3792 for (el = accel_list; el; el = el->next) {
3793 gchar *typename = g_strdup(object_class_get_name(
3794 OBJECT_CLASS(el->data)));
3795 /* omit qtest which is used for tests only */
3796 if (g_strcmp0(typename, ACCEL_CLASS_NAME("qtest")) &&
3797 g_str_has_suffix(typename, ACCEL_CLASS_SUFFIX)) {
3798 gchar **optname = g_strsplit(typename,
3799 ACCEL_CLASS_SUFFIX, 0);
3800 printf("%s\n", optname[0]);
3801 g_strfreev(optname);
3805 g_slist_free(accel_list);
3809 case QEMU_OPTION_usb:
3810 olist = qemu_find_opts("machine");
3811 qemu_opts_parse_noisily(olist, "usb=on", false);
3813 case QEMU_OPTION_usbdevice:
3814 error_report("'-usbdevice' is deprecated, please use "
3815 "'-device usb-...' instead");
3816 olist = qemu_find_opts("machine");
3817 qemu_opts_parse_noisily(olist, "usb=on", false);
3818 add_device_config(DEV_USB, optarg);
3820 case QEMU_OPTION_device:
3821 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3826 case QEMU_OPTION_smp:
3827 if (!qemu_opts_parse_noisily(qemu_find_opts("smp-opts"),
3832 case QEMU_OPTION_vnc:
3833 vnc_parse(optarg, &error_fatal);
3835 case QEMU_OPTION_no_acpi:
3836 olist = qemu_find_opts("machine");
3837 qemu_opts_parse_noisily(olist, "acpi=off", false);
3839 case QEMU_OPTION_no_hpet:
3840 olist = qemu_find_opts("machine");
3841 qemu_opts_parse_noisily(olist, "hpet=off", false);
3843 case QEMU_OPTION_no_reboot:
3846 case QEMU_OPTION_no_shutdown:
3849 case QEMU_OPTION_show_cursor:
3850 warn_report("The -show-cursor option is deprecated. Please "
3851 "add show-cursor=on to your -display options.");
3852 warn_report("When using the default display you can use "
3853 "-display default,show-cursor=on");
3854 dpy.has_show_cursor = true;
3855 dpy.show_cursor = true;
3857 case QEMU_OPTION_uuid:
3858 if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
3859 error_report("failed to parse UUID string: wrong format");
3862 qemu_uuid_set = true;
3864 case QEMU_OPTION_option_rom:
3865 if (nb_option_roms >= MAX_OPTION_ROMS) {
3866 error_report("too many option ROMs");
3869 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3874 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3875 option_rom[nb_option_roms].bootindex =
3876 qemu_opt_get_number(opts, "bootindex", -1);
3877 if (!option_rom[nb_option_roms].name) {
3878 error_report("Option ROM file is not specified");
3883 case QEMU_OPTION_semihosting:
3884 qemu_semihosting_enable();
3886 case QEMU_OPTION_semihosting_config:
3887 if (qemu_semihosting_config_options(optarg) != 0) {
3891 case QEMU_OPTION_name:
3892 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3897 /* Capture guest name if -msg guest-name is used later */
3898 error_guest_name = qemu_opt_get(opts, "guest");
3900 case QEMU_OPTION_prom_env:
3901 if (nb_prom_envs >= MAX_PROM_ENVS) {
3902 error_report("too many prom variables");
3905 prom_envs[nb_prom_envs] = optarg;
3908 case QEMU_OPTION_old_param:
3911 case QEMU_OPTION_rtc:
3912 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3918 case QEMU_OPTION_tb_size:
3920 error_report("TCG is disabled");
3923 warn_report("The -tb-size option is deprecated, use -accel tcg,tb-size instead");
3924 object_register_sugar_prop(ACCEL_CLASS_NAME("tcg"), "tb-size", optarg);
3926 case QEMU_OPTION_icount:
3927 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3933 case QEMU_OPTION_incoming:
3935 runstate_set(RUN_STATE_INMIGRATE);
3939 case QEMU_OPTION_only_migratable:
3940 only_migratable = 1;
3942 case QEMU_OPTION_nodefaults:
3945 case QEMU_OPTION_xen_domid:
3946 if (!(xen_available())) {
3947 error_report("Option not supported for this target");
3950 xen_domid = atoi(optarg);
3952 case QEMU_OPTION_xen_attach:
3953 if (!(xen_available())) {
3954 error_report("Option not supported for this target");
3957 xen_mode = XEN_ATTACH;
3959 case QEMU_OPTION_xen_domid_restrict:
3960 if (!(xen_available())) {
3961 error_report("Option not supported for this target");
3964 xen_domid_restrict = true;
3966 case QEMU_OPTION_trace:
3967 trace_opt_parse(optarg);
3969 case QEMU_OPTION_plugin:
3970 qemu_plugin_opt_parse(optarg, &plugin_list);
3972 case QEMU_OPTION_readconfig:
3974 int ret = qemu_read_config_file(optarg);
3976 error_report("read config %s: %s", optarg,
3982 case QEMU_OPTION_spice:
3983 olist = qemu_find_opts_err("spice", NULL);
3985 ui_module_load_one("spice-core");
3986 olist = qemu_find_opts("spice");
3989 error_report("spice support is disabled");
3992 opts = qemu_opts_parse_noisily(olist, optarg, false);
3998 case QEMU_OPTION_writeconfig:
4001 if (strcmp(optarg, "-") == 0) {
4004 fp = fopen(optarg, "w");
4006 error_report("open %s: %s", optarg,
4011 qemu_config_write(fp);
4017 case QEMU_OPTION_qtest:
4018 qtest_chrdev = optarg;
4020 case QEMU_OPTION_qtest_log:
4023 case QEMU_OPTION_sandbox:
4024 olist = qemu_find_opts("sandbox");
4026 #ifndef CONFIG_SECCOMP
4027 error_report("-sandbox support is not enabled "
4028 "in this QEMU binary");
4033 opts = qemu_opts_parse_noisily(olist, optarg, true);
4038 case QEMU_OPTION_add_fd:
4040 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
4046 error_report("File descriptor passing is disabled on this "
4051 case QEMU_OPTION_object:
4052 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
4058 case QEMU_OPTION_realtime:
4059 warn_report("'-realtime mlock=...' is deprecated, please use "
4060 "'-overcommit mem-lock=...' instead");
4061 opts = qemu_opts_parse_noisily(qemu_find_opts("realtime"),
4066 /* Don't override the -overcommit option if set */
4067 enable_mlock = enable_mlock ||
4068 qemu_opt_get_bool(opts, "mlock", true);
4070 case QEMU_OPTION_overcommit:
4071 opts = qemu_opts_parse_noisily(qemu_find_opts("overcommit"),
4076 /* Don't override the -realtime option if set */
4077 enable_mlock = enable_mlock ||
4078 qemu_opt_get_bool(opts, "mem-lock", false);
4079 enable_cpu_pm = qemu_opt_get_bool(opts, "cpu-pm", false);
4081 case QEMU_OPTION_msg:
4082 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
4087 configure_msg(opts);
4089 case QEMU_OPTION_dump_vmstate:
4090 if (vmstate_dump_file) {
4091 error_report("only one '-dump-vmstate' "
4092 "option may be given");
4095 vmstate_dump_file = fopen(optarg, "w");
4096 if (vmstate_dump_file == NULL) {
4097 error_report("open %s: %s", optarg, strerror(errno));
4101 case QEMU_OPTION_enable_sync_profile:
4104 case QEMU_OPTION_nouserconfig:
4105 /* Nothing to be parsed here. Especially, do not error out below. */
4108 if (os_parse_cmd_args(popt->index, optarg)) {
4109 error_report("Option not supported in this build");
4116 * Clear error location left behind by the loop.
4117 * Best done right after the loop. Do not insert code here!
4121 qemu_validate_options();
4122 qemu_process_sugar_options();
4125 * These options affect everything else and should be processed
4126 * before daemonizing.
4128 qemu_process_early_options();
4130 qemu_process_help_options();
4131 qemu_maybe_daemonize(pid_file);
4133 qemu_init_main_loop(&error_fatal);
4136 user_register_global_props();
4137 replay_configure(icount_opts);
4139 configure_rtc(qemu_find_opts_singleton("rtc"));
4141 machine_class = select_machine();
4142 object_set_machine_compat_props(machine_class->compat_props);
4144 have_custom_ram_size = set_memory_options(&ram_slots, &maxram_size,
4147 current_machine = MACHINE(object_new_with_class(OBJECT_CLASS(machine_class)));
4148 if (machine_help_func(qemu_get_machine_opts(), current_machine)) {
4151 object_property_add_child(object_get_root(), "machine",
4152 OBJECT(current_machine));
4153 object_property_add_child(container_get(OBJECT(current_machine),
4155 "sysbus", OBJECT(sysbus_get_default()));
4157 if (machine_class->minimum_page_bits) {
4158 if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
4159 /* This would be a board error: specifying a minimum smaller than
4160 * a target's compile-time fixed setting.
4162 g_assert_not_reached();
4166 cpu_exec_init_all();
4169 if (machine_class->hw_version) {
4170 qemu_set_hw_version(machine_class->hw_version);
4173 machine_smp_parse(current_machine,
4174 qemu_opts_find(qemu_find_opts("smp-opts"), NULL), &error_fatal);
4177 * Get the default machine options from the machine if it is not already
4178 * specified either by the configuration file or by the command line.
4180 if (machine_class->default_machine_opts) {
4181 qemu_opts_set_defaults(qemu_find_opts("machine"),
4182 machine_class->default_machine_opts, 0);
4185 /* process plugin before CPUs are created, but once -smp has been parsed */
4186 if (qemu_plugin_load_list(&plugin_list)) {
4190 qemu_opts_foreach(qemu_find_opts("device"),
4191 default_driver_check, NULL, NULL);
4192 qemu_opts_foreach(qemu_find_opts("global"),
4193 default_driver_check, NULL, NULL);
4195 if (!vga_model && !default_vga) {
4196 vga_interface_type = VGA_DEVICE;
4198 if (!has_defaults || machine_class->no_serial) {
4201 if (!has_defaults || machine_class->no_parallel) {
4202 default_parallel = 0;
4204 if (!has_defaults || machine_class->no_floppy) {
4207 if (!has_defaults || machine_class->no_cdrom) {
4210 if (!has_defaults || machine_class->no_sdcard) {
4213 if (!has_defaults) {
4214 default_monitor = 0;
4219 if (is_daemonized()) {
4220 /* According to documentation and historically, -nographic redirects
4221 * serial port, parallel port and monitor to stdio, which does not work
4222 * with -daemonize. We can redirect these to null instead, but since
4223 * -nographic is legacy, let's just error out.
4224 * We disallow -nographic only if all other ports are not redirected
4225 * explicitly, to not break existing legacy setups which uses
4226 * -nographic _and_ redirects all ports explicitly - this is valid
4227 * usage, -nographic is just a no-op in this case.
4230 && (default_parallel || default_serial || default_monitor)) {
4231 error_report("-nographic cannot be used with -daemonize");
4237 if (default_parallel)
4238 add_device_config(DEV_PARALLEL, "null");
4239 if (default_serial && default_monitor) {
4240 add_device_config(DEV_SERIAL, "mon:stdio");
4243 add_device_config(DEV_SERIAL, "stdio");
4244 if (default_monitor)
4245 monitor_parse("stdio", "readline", false);
4249 add_device_config(DEV_SERIAL, "vc:80Cx24C");
4250 if (default_parallel)
4251 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
4252 if (default_monitor)
4253 monitor_parse("vc:80Cx24C", "readline", false);
4257 QemuOptsList *net = qemu_find_opts("net");
4258 qemu_opts_parse(net, "nic", true, &error_abort);
4260 qemu_opts_parse(net, "user", true, &error_abort);
4264 #if defined(CONFIG_VNC)
4265 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
4269 if (dpy.type == DISPLAY_TYPE_DEFAULT && !display_remote) {
4270 if (!qemu_display_find_default(&dpy)) {
4271 dpy.type = DISPLAY_TYPE_NONE;
4272 #if defined(CONFIG_VNC)
4273 vnc_parse("localhost:0,to=99,id=default", &error_abort);
4277 if (dpy.type == DISPLAY_TYPE_DEFAULT) {
4278 dpy.type = DISPLAY_TYPE_NONE;
4281 if ((alt_grab || ctrl_grab) && dpy.type != DISPLAY_TYPE_SDL) {
4282 error_report("-alt-grab and -ctrl-grab are only valid "
4283 "for SDL, ignoring option");
4285 if (dpy.has_window_close &&
4286 (dpy.type != DISPLAY_TYPE_GTK && dpy.type != DISPLAY_TYPE_SDL)) {
4287 error_report("-no-quit is only valid for GTK and SDL, "
4291 qemu_display_early_init(&dpy);
4292 qemu_console_early_init();
4294 if (dpy.has_gl && dpy.gl != DISPLAYGL_MODE_OFF && display_opengl == 0) {
4295 #if defined(CONFIG_OPENGL)
4296 error_report("OpenGL is not supported by the display");
4298 error_report("OpenGL support is disabled");
4303 qemu_opts_foreach(qemu_find_opts("object"),
4304 user_creatable_add_opts_foreach,
4305 object_create_initial, &error_fatal);
4307 /* spice needs the timers to be initialized by this point */
4308 /* spice must initialize before audio as it changes the default auiodev */
4309 /* spice must initialize before chardevs (for spicevmc and spiceport) */
4312 qemu_opts_foreach(qemu_find_opts("chardev"),
4313 chardev_init_func, NULL, &error_fatal);
4315 #ifdef CONFIG_VIRTFS
4316 qemu_opts_foreach(qemu_find_opts("fsdev"),
4317 fsdev_init_func, NULL, &error_fatal);
4321 * Note: we need to create audio and block backends before
4322 * machine_set_property(), so machine properties can refer to
4325 configure_blockdev(&bdo_queue, machine_class, snapshot);
4326 audio_init_audiodevs();
4328 machine_opts = qemu_get_machine_opts();
4329 qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
4331 current_machine->ram_size = ram_size;
4332 current_machine->maxram_size = maxram_size;
4333 current_machine->ram_slots = ram_slots;
4336 * Note: uses machine properties such as kernel-irqchip, must run
4337 * after machine_set_property().
4339 configure_accelerators(argv[0]);
4342 * Beware, QOM objects created before this point miss global and
4343 * compat properties.
4345 * Global properties get set up by qdev_prop_register_global(),
4346 * called from user_register_global_props(), and certain option
4347 * desugaring. Also in CPU feature desugaring (buried in
4348 * parse_cpu_option()), which happens below this point, but may
4349 * only target the CPU type, which can only be created after
4350 * parse_cpu_option() returned the type.
4352 * Machine compat properties: object_set_machine_compat_props().
4353 * Accelerator compat props: object_set_accelerator_compat_props(),
4354 * called from configure_accelerator().
4357 if (!qtest_enabled() && machine_class->deprecation_reason) {
4358 error_report("Machine type '%s' is deprecated: %s",
4359 machine_class->name, machine_class->deprecation_reason);
4363 * Note: creates a QOM object, must run only after global and
4364 * compat properties have been set up.
4366 migration_object_init();
4369 qtest_server_init(qtest_chrdev, qtest_log, &error_fatal);
4372 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4374 boot_order = qemu_opt_get(opts, "order");
4376 validate_bootdevices(boot_order, &error_fatal);
4379 boot_once = qemu_opt_get(opts, "once");
4381 validate_bootdevices(boot_once, &error_fatal);
4384 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
4385 boot_strict = qemu_opt_get_bool(opts, "strict", false);
4389 boot_order = machine_class->default_boot_order;
4392 if (semihosting_enabled() && !semihosting_get_argc()) {
4393 const char *kernel_filename = qemu_opt_get(machine_opts, "kernel");
4394 const char *kernel_cmdline = qemu_opt_get(machine_opts, "append");
4395 /* fall back to the -kernel/-append */
4396 semihosting_arg_fallback(kernel_filename, kernel_cmdline);
4399 if (net_init_clients(&err) < 0) {
4400 error_report_err(err);
4404 qemu_opts_foreach(qemu_find_opts("object"),
4405 user_creatable_add_opts_foreach,
4406 object_create_delayed, &error_fatal);
4408 if (tpm_init() < 0) {
4412 qemu_opts_foreach(qemu_find_opts("mon"),
4413 mon_init_func, NULL, &error_fatal);
4415 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4417 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4419 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4422 /* now chardevs have been created we may have semihosting to connect */
4423 qemu_semihosting_connect_chardevs();
4424 qemu_semihosting_console_init();
4426 /* If no default VGA is requested, the default is "none". */
4428 vga_model = get_default_vga_model(machine_class);
4431 select_vgahw(machine_class, vga_model);
4434 /* This checkpoint is required by replay to separate prior clock
4435 reading from the other reads, because timer polling functions query
4436 clock values from the log. */
4437 replay_checkpoint(CHECKPOINT_INIT);
4439 current_machine->boot_order = boot_order;
4441 /* parse features once if machine provides default cpu_type */
4442 current_machine->cpu_type = machine_class->default_cpu_type;
4444 current_machine->cpu_type = parse_cpu_option(cpu_option);
4447 if (current_machine->ram_memdev_id) {
4449 ram_addr_t backend_size;
4451 backend = object_resolve_path_type(current_machine->ram_memdev_id,
4452 TYPE_MEMORY_BACKEND, NULL);
4454 error_report("Memory backend '%s' not found",
4455 current_machine->ram_memdev_id);
4458 backend_size = object_property_get_uint(backend, "size", &error_abort);
4459 if (have_custom_ram_size && backend_size != ram_size) {
4460 error_report("Size specified by -m option must match size of "
4461 "explicitly specified 'memory-backend' property");
4465 error_report("'-mem-path' can't be used together with"
4466 "'-machine memory-backend'");
4469 ram_size = backend_size;
4472 if (!xen_enabled()) {
4473 /* On 32-bit hosts, QEMU is limited by virtual address space */
4474 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4475 error_report("at most 2047 MB RAM can be simulated");
4480 parse_numa_opts(current_machine);
4482 /* do monitor/qmp handling at preconfig state if requested */
4486 qemu_create_cli_devices();
4487 qemu_machine_creation_done();
4490 Error *local_err = NULL;
4491 if (load_snapshot(loadvm, &local_err) < 0) {
4492 error_report_err(local_err);
4497 if (replay_mode != REPLAY_MODE_NONE) {
4498 replay_vmstate_init();
4501 qdev_prop_check_globals();
4502 if (vmstate_dump_file) {
4504 dump_vmstate_json_to_file(vmstate_dump_file);
4508 Error *local_err = NULL;
4509 qemu_start_incoming_migration(incoming, &local_err);
4511 error_reportf_err(local_err, "-incoming %s: ", incoming);
4514 } else if (autostart) {
4518 accel_setup_post(current_machine);
4524 void qemu_cleanup(void)
4526 gdbserver_cleanup();
4529 * cleaning up the migration object cancels any existing migration
4530 * try to do this early so that it also stops using devices.
4532 migration_shutdown();
4535 * We must cancel all block jobs while the block layer is drained,
4536 * or cancelling will be affected by throttling and thus may block
4537 * for an extended period of time.
4538 * vm_shutdown() will bdrv_drain_all(), so we may as well include
4539 * it in the drained section.
4540 * We do not need to end this section, because we do not want any
4541 * requests happening from here on anyway.
4543 bdrv_drain_all_begin();
4545 /* No more vcpu or device emulation activity beyond this point */
4549 job_cancel_sync_all();
4554 /* vhost-user must be cleaned up before chardevs. */
4560 user_creatable_cleanup();
4561 /* TODO: unref root container, check all devices are ok */