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/qdev-properties.h"
29 #include "qapi/error.h"
30 #include "qemu-version.h"
31 #include "qemu/cutils.h"
32 #include "qemu/help_option.h"
33 #include "qemu/uuid.h"
34 #include "sysemu/reset.h"
35 #include "sysemu/runstate.h"
36 #include "sysemu/seccomp.h"
37 #include "sysemu/tcg.h"
40 #if defined(__APPLE__) || defined(main)
42 int qemu_main(int argc, char **argv, char **envp);
43 int main(int argc, char **argv)
45 return qemu_main(argc, argv, NULL);
48 #define main qemu_main
50 #endif /* CONFIG_SDL */
54 #define main qemu_main
55 #endif /* CONFIG_COCOA */
58 #include "qemu/error-report.h"
59 #include "qemu/sockets.h"
60 #include "sysemu/accel.h"
62 #include "hw/isa/isa.h"
63 #include "hw/scsi/scsi.h"
64 #include "hw/display/vga.h"
66 #include "sysemu/watchdog.h"
67 #include "hw/firmware/smbios.h"
68 #include "hw/acpi/acpi.h"
69 #include "hw/xen/xen.h"
70 #include "hw/loader.h"
71 #include "monitor/qdev.h"
72 #include "sysemu/bt.h"
74 #include "net/slirp.h"
75 #include "monitor/monitor.h"
76 #include "ui/console.h"
78 #include "sysemu/sysemu.h"
79 #include "sysemu/numa.h"
80 #include "exec/gdbstub.h"
81 #include "qemu/timer.h"
82 #include "chardev/char.h"
83 #include "qemu/bitmap.h"
85 #include "sysemu/blockdev.h"
86 #include "hw/block/block.h"
87 #include "migration/misc.h"
88 #include "migration/snapshot.h"
89 #include "migration/global_state.h"
90 #include "sysemu/tpm.h"
91 #include "sysemu/dma.h"
92 #include "hw/audio/soundhw.h"
93 #include "audio/audio.h"
94 #include "sysemu/cpus.h"
95 #include "migration/colo.h"
96 #include "migration/postcopy-ram.h"
97 #include "sysemu/kvm.h"
98 #include "sysemu/hax.h"
99 #include "qapi/qobject-input-visitor.h"
100 #include "qemu/option.h"
101 #include "qemu/config-file.h"
102 #include "qemu-options.h"
103 #include "qemu/main-loop.h"
105 #include "fsdev/qemu-fsdev.h"
107 #include "sysemu/qtest.h"
109 #include "disas/disas.h"
111 #include "trace-root.h"
112 #include "trace/control.h"
113 #include "qemu/plugin.h"
114 #include "qemu/queue.h"
115 #include "sysemu/arch_init.h"
117 #include "ui/qemu-spice.h"
118 #include "qapi/string-input-visitor.h"
119 #include "qapi/opts-visitor.h"
120 #include "qapi/clone-visitor.h"
121 #include "qom/object_interfaces.h"
122 #include "hw/semihosting/semihost.h"
123 #include "crypto/init.h"
124 #include "sysemu/replay.h"
125 #include "qapi/qapi-events-run-state.h"
126 #include "qapi/qapi-visit-block-core.h"
127 #include "qapi/qapi-visit-ui.h"
128 #include "qapi/qapi-commands-block-core.h"
129 #include "qapi/qapi-commands-run-state.h"
130 #include "qapi/qapi-commands-ui.h"
131 #include "qapi/qmp/qerror.h"
132 #include "sysemu/iothread.h"
133 #include "qemu/guest-random.h"
135 #define MAX_VIRTIO_CONSOLES 1
137 static const char *data_dir[16];
138 static int data_dir_idx;
139 const char *bios_name = NULL;
140 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
142 const char* keyboard_layout = NULL;
144 const char *mem_path = NULL;
145 int mem_prealloc = 0; /* force preallocation of physical target memory */
146 bool enable_mlock = false;
147 bool enable_cpu_pm = false;
149 NICInfo nd_table[MAX_NICS];
155 } rtc_base_type = RTC_BASE_UTC;
156 static time_t rtc_ref_start_datetime;
157 static int rtc_realtime_clock_offset; /* used only with QEMU_CLOCK_REALTIME */
158 static int rtc_host_datetime_offset = -1; /* valid & used only with
160 QEMUClockType rtc_clock;
161 int vga_interface_type = VGA_NONE;
162 static DisplayOptions dpy;
163 static int num_serial_hds;
164 static Chardev **serial_hds;
165 Chardev *parallel_hds[MAX_PARALLEL_PORTS];
166 int win2k_install_hack = 0;
168 int acpi_enabled = 1;
171 static int no_reboot;
174 int graphic_rotate = 0;
175 const char *watchdog;
176 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
179 const char *qemu_name;
182 unsigned int nb_prom_envs = 0;
183 const char *prom_envs[MAX_PROM_ENVS];
186 uint8_t *boot_splash_filedata;
187 int only_migratable; /* turn it off unless user states otherwise */
188 bool wakeup_suspend_enabled;
190 int icount_align_option;
192 /* The bytes in qemu_uuid are in the order specified by RFC4122, _not_ in the
193 * little-endian "wire format" described in the SMBIOS 2.6 specification.
198 static NotifierList exit_notifiers =
199 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
201 static NotifierList machine_init_done_notifiers =
202 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
206 enum xen_mode xen_mode = XEN_EMULATE;
207 bool xen_domid_restrict;
209 static int has_defaults = 1;
210 static int default_serial = 1;
211 static int default_parallel = 1;
212 static int default_monitor = 1;
213 static int default_floppy = 1;
214 static int default_cdrom = 1;
215 static int default_sdcard = 1;
216 static int default_vga = 1;
217 static int default_net = 1;
223 { .driver = "isa-serial", .flag = &default_serial },
224 { .driver = "isa-parallel", .flag = &default_parallel },
225 { .driver = "isa-fdc", .flag = &default_floppy },
226 { .driver = "floppy", .flag = &default_floppy },
227 { .driver = "ide-cd", .flag = &default_cdrom },
228 { .driver = "ide-hd", .flag = &default_cdrom },
229 { .driver = "ide-drive", .flag = &default_cdrom },
230 { .driver = "scsi-cd", .flag = &default_cdrom },
231 { .driver = "scsi-hd", .flag = &default_cdrom },
232 { .driver = "VGA", .flag = &default_vga },
233 { .driver = "isa-vga", .flag = &default_vga },
234 { .driver = "cirrus-vga", .flag = &default_vga },
235 { .driver = "isa-cirrus-vga", .flag = &default_vga },
236 { .driver = "vmware-svga", .flag = &default_vga },
237 { .driver = "qxl-vga", .flag = &default_vga },
238 { .driver = "virtio-vga", .flag = &default_vga },
239 { .driver = "ati-vga", .flag = &default_vga },
240 { .driver = "vhost-user-vga", .flag = &default_vga },
243 static QemuOptsList qemu_rtc_opts = {
245 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
250 .type = QEMU_OPT_STRING,
253 .type = QEMU_OPT_STRING,
256 .type = QEMU_OPT_STRING,
258 { /* end of list */ }
262 static QemuOptsList qemu_option_rom_opts = {
263 .name = "option-rom",
264 .implied_opt_name = "romfile",
265 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
269 .type = QEMU_OPT_NUMBER,
272 .type = QEMU_OPT_STRING,
274 { /* end of list */ }
278 static QemuOptsList qemu_machine_opts = {
280 .implied_opt_name = "type",
282 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
285 * no elements => accept any
286 * sanity checking will happen later
287 * when setting machine properties
293 static QemuOptsList qemu_accel_opts = {
295 .implied_opt_name = "accel",
296 .head = QTAILQ_HEAD_INITIALIZER(qemu_accel_opts.head),
301 .type = QEMU_OPT_STRING,
302 .help = "Select the type of accelerator",
306 .type = QEMU_OPT_STRING,
307 .help = "Enable/disable multi-threaded TCG",
309 { /* end of list */ }
313 static QemuOptsList qemu_boot_opts = {
315 .implied_opt_name = "order",
317 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
321 .type = QEMU_OPT_STRING,
324 .type = QEMU_OPT_STRING,
327 .type = QEMU_OPT_BOOL,
330 .type = QEMU_OPT_STRING,
332 .name = "splash-time",
333 .type = QEMU_OPT_NUMBER,
335 .name = "reboot-timeout",
336 .type = QEMU_OPT_NUMBER,
339 .type = QEMU_OPT_BOOL,
345 static QemuOptsList qemu_add_fd_opts = {
347 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
351 .type = QEMU_OPT_NUMBER,
352 .help = "file descriptor of which a duplicate is added to fd set",
355 .type = QEMU_OPT_NUMBER,
356 .help = "ID of the fd set to add fd to",
359 .type = QEMU_OPT_STRING,
360 .help = "free-form string used to describe fd",
362 { /* end of list */ }
366 static QemuOptsList qemu_object_opts = {
368 .implied_opt_name = "qom-type",
369 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
375 static QemuOptsList qemu_tpmdev_opts = {
377 .implied_opt_name = "type",
378 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
380 /* options are defined in the TPM backends */
381 { /* end of list */ }
385 static QemuOptsList qemu_realtime_opts = {
387 .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
391 .type = QEMU_OPT_BOOL,
393 { /* end of list */ }
397 static QemuOptsList qemu_overcommit_opts = {
398 .name = "overcommit",
399 .head = QTAILQ_HEAD_INITIALIZER(qemu_overcommit_opts.head),
403 .type = QEMU_OPT_BOOL,
407 .type = QEMU_OPT_BOOL,
409 { /* end of list */ }
413 static QemuOptsList qemu_msg_opts = {
415 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
419 .type = QEMU_OPT_BOOL,
421 { /* end of list */ }
425 static QemuOptsList qemu_name_opts = {
427 .implied_opt_name = "guest",
429 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
433 .type = QEMU_OPT_STRING,
434 .help = "Sets the name of the guest.\n"
435 "This name will be displayed in the SDL window caption.\n"
436 "The name will also be used for the VNC server",
439 .type = QEMU_OPT_STRING,
440 .help = "Sets the name of the QEMU process, as shown in top etc",
442 .name = "debug-threads",
443 .type = QEMU_OPT_BOOL,
444 .help = "When enabled, name the individual threads; defaults off.\n"
445 "NOTE: The thread names are for debugging and not a\n"
448 { /* End of list */ }
452 static QemuOptsList qemu_mem_opts = {
454 .implied_opt_name = "size",
455 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
460 .type = QEMU_OPT_SIZE,
464 .type = QEMU_OPT_NUMBER,
468 .type = QEMU_OPT_SIZE,
470 { /* end of list */ }
474 static QemuOptsList qemu_icount_opts = {
476 .implied_opt_name = "shift",
478 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
482 .type = QEMU_OPT_STRING,
485 .type = QEMU_OPT_BOOL,
488 .type = QEMU_OPT_BOOL,
491 .type = QEMU_OPT_STRING,
494 .type = QEMU_OPT_STRING,
496 .name = "rrsnapshot",
497 .type = QEMU_OPT_STRING,
499 { /* end of list */ }
503 static QemuOptsList qemu_fw_cfg_opts = {
505 .implied_opt_name = "name",
506 .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
510 .type = QEMU_OPT_STRING,
511 .help = "Sets the fw_cfg name of the blob to be inserted",
514 .type = QEMU_OPT_STRING,
515 .help = "Sets the name of the file from which "
516 "the fw_cfg blob will be loaded",
519 .type = QEMU_OPT_STRING,
520 .help = "Sets content of the blob to be inserted from a string",
522 { /* end of list */ }
527 * Get machine options
529 * Returns: machine options (never null).
531 QemuOpts *qemu_get_machine_opts(void)
533 return qemu_find_opts_singleton("machine");
536 const char *qemu_get_vm_name(void)
541 static void res_free(void)
543 g_free(boot_splash_filedata);
544 boot_splash_filedata = NULL;
547 static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
549 const char *driver = qemu_opt_get(opts, "driver");
554 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
555 if (strcmp(default_list[i].driver, driver) != 0)
557 *(default_list[i].flag) = 0;
562 /***********************************************************/
565 static RunState current_run_state = RUN_STATE_PRECONFIG;
567 /* We use RUN_STATE__MAX but any invalid value will do */
568 static RunState vmstop_requested = RUN_STATE__MAX;
569 static QemuMutex vmstop_lock;
574 } RunStateTransition;
576 static const RunStateTransition runstate_transitions_def[] = {
578 { RUN_STATE_PRECONFIG, RUN_STATE_PRELAUNCH },
579 /* Early switch to inmigrate state to allow -incoming CLI option work
580 * as it used to. TODO: delay actual switching to inmigrate state to
581 * the point after machine is built and remove this hack.
583 { RUN_STATE_PRECONFIG, RUN_STATE_INMIGRATE },
585 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
586 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
587 { RUN_STATE_DEBUG, RUN_STATE_PRELAUNCH },
589 { RUN_STATE_INMIGRATE, RUN_STATE_INTERNAL_ERROR },
590 { RUN_STATE_INMIGRATE, RUN_STATE_IO_ERROR },
591 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
592 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
593 { RUN_STATE_INMIGRATE, RUN_STATE_SHUTDOWN },
594 { RUN_STATE_INMIGRATE, RUN_STATE_SUSPENDED },
595 { RUN_STATE_INMIGRATE, RUN_STATE_WATCHDOG },
596 { RUN_STATE_INMIGRATE, RUN_STATE_GUEST_PANICKED },
597 { RUN_STATE_INMIGRATE, RUN_STATE_FINISH_MIGRATE },
598 { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH },
599 { RUN_STATE_INMIGRATE, RUN_STATE_POSTMIGRATE },
600 { RUN_STATE_INMIGRATE, RUN_STATE_COLO },
602 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
603 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
604 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PRELAUNCH },
606 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
607 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
608 { RUN_STATE_IO_ERROR, RUN_STATE_PRELAUNCH },
610 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
611 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
612 { RUN_STATE_PAUSED, RUN_STATE_POSTMIGRATE },
613 { RUN_STATE_PAUSED, RUN_STATE_PRELAUNCH },
614 { RUN_STATE_PAUSED, RUN_STATE_COLO},
616 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
617 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
618 { RUN_STATE_POSTMIGRATE, RUN_STATE_PRELAUNCH },
620 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
621 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
622 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
624 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
625 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PAUSED },
626 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
627 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PRELAUNCH },
628 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_COLO},
630 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
631 { RUN_STATE_RESTORE_VM, RUN_STATE_PRELAUNCH },
633 { RUN_STATE_COLO, RUN_STATE_RUNNING },
635 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
636 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
637 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
638 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
639 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
640 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
641 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
642 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
643 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
644 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
645 { RUN_STATE_RUNNING, RUN_STATE_COLO},
647 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
649 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
650 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
651 { RUN_STATE_SHUTDOWN, RUN_STATE_PRELAUNCH },
653 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
654 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
655 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
656 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
657 { RUN_STATE_SUSPENDED, RUN_STATE_PRELAUNCH },
658 { RUN_STATE_SUSPENDED, RUN_STATE_COLO},
660 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
661 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
662 { RUN_STATE_WATCHDOG, RUN_STATE_PRELAUNCH },
663 { RUN_STATE_WATCHDOG, RUN_STATE_COLO},
665 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
666 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
667 { RUN_STATE_GUEST_PANICKED, RUN_STATE_PRELAUNCH },
669 { RUN_STATE__MAX, RUN_STATE__MAX },
672 static bool runstate_valid_transitions[RUN_STATE__MAX][RUN_STATE__MAX];
674 bool runstate_check(RunState state)
676 return current_run_state == state;
679 bool runstate_store(char *str, size_t size)
681 const char *state = RunState_str(current_run_state);
682 size_t len = strlen(state) + 1;
687 memcpy(str, state, len);
691 static void runstate_init(void)
693 const RunStateTransition *p;
695 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
696 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE__MAX; p++) {
697 runstate_valid_transitions[p->from][p->to] = true;
700 qemu_mutex_init(&vmstop_lock);
703 /* This function will abort() on invalid state transitions */
704 void runstate_set(RunState new_state)
706 assert(new_state < RUN_STATE__MAX);
708 trace_runstate_set(current_run_state, RunState_str(current_run_state),
709 new_state, RunState_str(new_state));
711 if (current_run_state == new_state) {
715 if (!runstate_valid_transitions[current_run_state][new_state]) {
716 error_report("invalid runstate transition: '%s' -> '%s'",
717 RunState_str(current_run_state),
718 RunState_str(new_state));
722 current_run_state = new_state;
725 int runstate_is_running(void)
727 return runstate_check(RUN_STATE_RUNNING);
730 bool runstate_needs_reset(void)
732 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
733 runstate_check(RUN_STATE_SHUTDOWN);
736 StatusInfo *qmp_query_status(Error **errp)
738 StatusInfo *info = g_malloc0(sizeof(*info));
740 info->running = runstate_is_running();
741 info->singlestep = singlestep;
742 info->status = current_run_state;
747 bool qemu_vmstop_requested(RunState *r)
749 qemu_mutex_lock(&vmstop_lock);
750 *r = vmstop_requested;
751 vmstop_requested = RUN_STATE__MAX;
752 qemu_mutex_unlock(&vmstop_lock);
753 return *r < RUN_STATE__MAX;
756 void qemu_system_vmstop_request_prepare(void)
758 qemu_mutex_lock(&vmstop_lock);
761 void qemu_system_vmstop_request(RunState state)
763 vmstop_requested = state;
764 qemu_mutex_unlock(&vmstop_lock);
768 /***********************************************************/
769 /* RTC reference time/date access */
770 static time_t qemu_ref_timedate(QEMUClockType clock)
772 time_t value = qemu_clock_get_ms(clock) / 1000;
774 case QEMU_CLOCK_REALTIME:
775 value -= rtc_realtime_clock_offset;
777 case QEMU_CLOCK_VIRTUAL:
778 value += rtc_ref_start_datetime;
780 case QEMU_CLOCK_HOST:
781 if (rtc_base_type == RTC_BASE_DATETIME) {
782 value -= rtc_host_datetime_offset;
791 void qemu_get_timedate(struct tm *tm, int offset)
793 time_t ti = qemu_ref_timedate(rtc_clock);
797 switch (rtc_base_type) {
798 case RTC_BASE_DATETIME:
802 case RTC_BASE_LOCALTIME:
803 localtime_r(&ti, tm);
808 int qemu_timedate_diff(struct tm *tm)
812 switch (rtc_base_type) {
813 case RTC_BASE_DATETIME:
815 seconds = mktimegm(tm);
817 case RTC_BASE_LOCALTIME:
820 tmp.tm_isdst = -1; /* use timezone to figure it out */
821 seconds = mktime(&tmp);
828 return seconds - qemu_ref_timedate(QEMU_CLOCK_HOST);
831 static void configure_rtc_base_datetime(const char *startdate)
833 time_t rtc_start_datetime;
836 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d", &tm.tm_year, &tm.tm_mon,
837 &tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec) == 6) {
839 } else if (sscanf(startdate, "%d-%d-%d",
840 &tm.tm_year, &tm.tm_mon, &tm.tm_mday) == 3) {
849 rtc_start_datetime = mktimegm(&tm);
850 if (rtc_start_datetime == -1) {
852 error_report("invalid datetime format");
853 error_printf("valid formats: "
854 "'2006-06-17T16:01:21' or '2006-06-17'\n");
857 rtc_host_datetime_offset = rtc_ref_start_datetime - rtc_start_datetime;
858 rtc_ref_start_datetime = rtc_start_datetime;
861 static void configure_rtc(QemuOpts *opts)
866 rtc_clock = QEMU_CLOCK_HOST;
867 rtc_ref_start_datetime = qemu_clock_get_ms(QEMU_CLOCK_HOST) / 1000;
868 rtc_realtime_clock_offset = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) / 1000;
870 value = qemu_opt_get(opts, "base");
872 if (!strcmp(value, "utc")) {
873 rtc_base_type = RTC_BASE_UTC;
874 } else if (!strcmp(value, "localtime")) {
875 Error *blocker = NULL;
876 rtc_base_type = RTC_BASE_LOCALTIME;
877 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
878 "-rtc base=localtime");
879 replay_add_blocker(blocker);
881 rtc_base_type = RTC_BASE_DATETIME;
882 configure_rtc_base_datetime(value);
885 value = qemu_opt_get(opts, "clock");
887 if (!strcmp(value, "host")) {
888 rtc_clock = QEMU_CLOCK_HOST;
889 } else if (!strcmp(value, "rt")) {
890 rtc_clock = QEMU_CLOCK_REALTIME;
891 } else if (!strcmp(value, "vm")) {
892 rtc_clock = QEMU_CLOCK_VIRTUAL;
894 error_report("invalid option value '%s'", value);
898 value = qemu_opt_get(opts, "driftfix");
900 if (!strcmp(value, "slew")) {
901 static GlobalProperty slew_lost_ticks = {
902 .driver = "mc146818rtc",
903 .property = "lost_tick_policy",
907 qdev_prop_register_global(&slew_lost_ticks);
908 } else if (!strcmp(value, "none")) {
909 /* discard is default */
911 error_report("invalid option value '%s'", value);
917 /***********************************************************/
918 /* Bluetooth support */
921 static struct HCIInfo *hci_table[MAX_NICS];
923 struct HCIInfo *qemu_next_hci(void)
925 if (cur_hci == nb_hcis)
928 return hci_table[cur_hci++];
931 static int bt_hci_parse(const char *str)
936 if (nb_hcis >= MAX_NICS) {
937 error_report("too many bluetooth HCIs (max %i)", MAX_NICS);
950 bdaddr.b[5] = 0x56 + nb_hcis;
951 hci->bdaddr_set(hci, bdaddr.b);
953 hci_table[nb_hcis++] = hci;
958 static void bt_vhci_add(int vlan_id)
960 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
963 warn_report("adding a VHCI to an empty scatternet %i",
966 bt_vhci_init(bt_new_hci(vlan));
969 static struct bt_device_s *bt_device_add(const char *opt)
971 struct bt_scatternet_s *vlan;
973 char *endp = strstr(opt, ",vlan=");
974 int len = (endp ? endp - opt : strlen(opt)) + 1;
977 pstrcpy(devname, MIN(sizeof(devname), len), opt);
980 vlan_id = strtol(endp + 6, &endp, 0);
982 error_report("unrecognised bluetooth vlan Id");
987 vlan = qemu_find_bt_vlan(vlan_id);
990 warn_report("adding a slave device to an empty scatternet %i",
993 if (!strcmp(devname, "keyboard"))
994 return bt_keyboard_init(vlan);
996 error_report("unsupported bluetooth device '%s'", devname);
1000 static int bt_parse(const char *opt)
1002 const char *endp, *p;
1005 if (strstart(opt, "hci", &endp)) {
1006 if (!*endp || *endp == ',') {
1008 if (!strstart(endp, ",vlan=", 0))
1011 return bt_hci_parse(opt);
1013 } else if (strstart(opt, "vhci", &endp)) {
1014 if (!*endp || *endp == ',') {
1016 if (strstart(endp, ",vlan=", &p)) {
1017 vlan = strtol(p, (char **) &endp, 0);
1019 error_report("bad scatternet '%s'", p);
1023 error_report("bad parameter '%s'", endp + 1);
1032 } else if (strstart(opt, "device:", &endp))
1033 return !bt_device_add(endp);
1035 error_report("bad bluetooth parameter '%s'", opt);
1039 static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
1041 const char *proc_name;
1043 if (qemu_opt_get(opts, "debug-threads")) {
1044 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
1046 qemu_name = qemu_opt_get(opts, "guest");
1048 proc_name = qemu_opt_get(opts, "process");
1050 os_set_proc_name(proc_name);
1056 bool defaults_enabled(void)
1058 return has_defaults;
1062 static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1064 int fd, dupfd, flags;
1066 const char *fd_opaque = NULL;
1069 fd = qemu_opt_get_number(opts, "fd", -1);
1070 fdset_id = qemu_opt_get_number(opts, "set", -1);
1071 fd_opaque = qemu_opt_get(opts, "opaque");
1074 error_setg(errp, "fd option is required and must be non-negative");
1078 if (fd <= STDERR_FILENO) {
1079 error_setg(errp, "fd cannot be a standard I/O stream");
1084 * All fds inherited across exec() necessarily have FD_CLOEXEC
1085 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1087 flags = fcntl(fd, F_GETFD);
1088 if (flags == -1 || (flags & FD_CLOEXEC)) {
1089 error_setg(errp, "fd is not valid or already in use");
1094 error_setg(errp, "set option is required and must be non-negative");
1098 #ifdef F_DUPFD_CLOEXEC
1099 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1103 qemu_set_cloexec(dupfd);
1107 error_setg(errp, "error duplicating fd: %s", strerror(errno));
1111 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1112 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
1119 static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1123 fd = qemu_opt_get_number(opts, "fd", -1);
1130 /***********************************************************/
1131 /* QEMU Block devices */
1133 #define HD_OPTS "media=disk"
1134 #define CDROM_OPTS "media=cdrom"
1136 #define PFLASH_OPTS ""
1140 static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
1142 BlockInterfaceType *block_default_type = opaque;
1144 return drive_new(opts, *block_default_type, errp) == NULL;
1147 static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
1149 if (qemu_opt_get(opts, "snapshot") == NULL) {
1150 qemu_opt_set(opts, "snapshot", "on", &error_abort);
1155 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1156 int index, const char *optstr)
1161 if (!enable || drive_get_by_index(type, index)) {
1165 opts = drive_add(type, index, NULL, optstr);
1167 drive_enable_snapshot(NULL, opts, NULL);
1170 dinfo = drive_new(opts, type, &error_abort);
1171 dinfo->is_default = true;
1175 typedef struct BlockdevOptionsQueueEntry {
1176 BlockdevOptions *bdo;
1178 QSIMPLEQ_ENTRY(BlockdevOptionsQueueEntry) entry;
1179 } BlockdevOptionsQueueEntry;
1181 typedef QSIMPLEQ_HEAD(, BlockdevOptionsQueueEntry) BlockdevOptionsQueue;
1183 static void configure_blockdev(BlockdevOptionsQueue *bdo_queue,
1184 MachineClass *machine_class, int snapshot)
1187 * If the currently selected machine wishes to override the
1188 * units-per-bus property of its default HBA interface type, do so
1191 if (machine_class->units_per_default_bus) {
1192 override_max_devs(machine_class->block_default_type,
1193 machine_class->units_per_default_bus);
1196 /* open the virtual block devices */
1197 while (!QSIMPLEQ_EMPTY(bdo_queue)) {
1198 BlockdevOptionsQueueEntry *bdo = QSIMPLEQ_FIRST(bdo_queue);
1200 QSIMPLEQ_REMOVE_HEAD(bdo_queue, entry);
1201 loc_push_restore(&bdo->loc);
1202 qmp_blockdev_add(bdo->bdo, &error_fatal);
1204 qapi_free_BlockdevOptions(bdo->bdo);
1208 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
1211 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
1212 &machine_class->block_default_type, &error_fatal)) {
1213 /* We printed help */
1217 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
1219 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
1220 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
1224 static QemuOptsList qemu_smp_opts = {
1226 .implied_opt_name = "cpus",
1227 .merge_lists = true,
1228 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
1232 .type = QEMU_OPT_NUMBER,
1235 .type = QEMU_OPT_NUMBER,
1238 .type = QEMU_OPT_NUMBER,
1241 .type = QEMU_OPT_NUMBER,
1244 .type = QEMU_OPT_NUMBER,
1247 .type = QEMU_OPT_NUMBER,
1249 { /*End of list */ }
1253 static void realtime_init(void)
1256 if (os_mlock() < 0) {
1257 error_report("locking memory failed");
1264 static void configure_msg(QemuOpts *opts)
1266 enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
1270 /* Now we still need this for compatibility with XEN. */
1271 bool has_igd_gfx_passthru;
1272 static void igd_gfx_passthru(void)
1274 has_igd_gfx_passthru = current_machine->igd_gfx_passthru;
1277 /***********************************************************/
1280 static int usb_device_add(const char *devname)
1282 USBDevice *dev = NULL;
1284 if (!machine_usb(current_machine)) {
1288 dev = usbdevice_create(devname);
1295 static int usb_parse(const char *cmdline)
1298 r = usb_device_add(cmdline);
1300 error_report("could not add USB device '%s'", cmdline);
1305 /***********************************************************/
1306 /* machine registration */
1308 MachineState *current_machine;
1310 static MachineClass *find_machine(const char *name, GSList *machines)
1314 for (el = machines; el; el = el->next) {
1315 MachineClass *mc = el->data;
1317 if (!strcmp(mc->name, name) || !g_strcmp0(mc->alias, name)) {
1325 static MachineClass *find_default_machine(GSList *machines)
1329 for (el = machines; el; el = el->next) {
1330 MachineClass *mc = el->data;
1332 if (mc->is_default) {
1340 static int machine_help_func(QemuOpts *opts, MachineState *machine)
1342 ObjectProperty *prop;
1343 ObjectPropertyIterator iter;
1345 if (!qemu_opt_has_help_opt(opts)) {
1349 object_property_iter_init(&iter, OBJECT(machine));
1350 while ((prop = object_property_iter_next(&iter))) {
1355 printf("%s.%s=%s", MACHINE_GET_CLASS(machine)->name,
1356 prop->name, prop->type);
1357 if (prop->description) {
1358 printf(" (%s)\n", prop->description);
1367 struct VMChangeStateEntry {
1368 VMChangeStateHandler *cb;
1370 QTAILQ_ENTRY(VMChangeStateEntry) entries;
1374 static QTAILQ_HEAD(, VMChangeStateEntry) vm_change_state_head;
1377 * qemu_add_vm_change_state_handler_prio:
1378 * @cb: the callback to invoke
1379 * @opaque: user data passed to the callback
1380 * @priority: low priorities execute first when the vm runs and the reverse is
1381 * true when the vm stops
1383 * Register a callback function that is invoked when the vm starts or stops
1386 * Returns: an entry to be freed using qemu_del_vm_change_state_handler()
1388 VMChangeStateEntry *qemu_add_vm_change_state_handler_prio(
1389 VMChangeStateHandler *cb, void *opaque, int priority)
1391 VMChangeStateEntry *e;
1392 VMChangeStateEntry *other;
1394 e = g_malloc0(sizeof(*e));
1397 e->priority = priority;
1399 /* Keep list sorted in ascending priority order */
1400 QTAILQ_FOREACH(other, &vm_change_state_head, entries) {
1401 if (priority < other->priority) {
1402 QTAILQ_INSERT_BEFORE(other, e, entries);
1407 QTAILQ_INSERT_TAIL(&vm_change_state_head, e, entries);
1411 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1414 return qemu_add_vm_change_state_handler_prio(cb, opaque, 0);
1417 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1419 QTAILQ_REMOVE(&vm_change_state_head, e, entries);
1423 void vm_state_notify(int running, RunState state)
1425 VMChangeStateEntry *e, *next;
1427 trace_vm_state_notify(running, state, RunState_str(state));
1430 QTAILQ_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
1431 e->cb(e->opaque, running, state);
1434 QTAILQ_FOREACH_REVERSE_SAFE(e, &vm_change_state_head, entries, next) {
1435 e->cb(e->opaque, running, state);
1440 static ShutdownCause reset_requested;
1441 static ShutdownCause shutdown_requested;
1442 static int shutdown_signal;
1443 static pid_t shutdown_pid;
1444 static int powerdown_requested;
1445 static int debug_requested;
1446 static int suspend_requested;
1447 static bool preconfig_exit_requested = true;
1448 static WakeupReason wakeup_reason;
1449 static NotifierList powerdown_notifiers =
1450 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
1451 static NotifierList suspend_notifiers =
1452 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1453 static NotifierList wakeup_notifiers =
1454 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1455 static NotifierList shutdown_notifiers =
1456 NOTIFIER_LIST_INITIALIZER(shutdown_notifiers);
1457 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
1459 ShutdownCause qemu_shutdown_requested_get(void)
1461 return shutdown_requested;
1464 ShutdownCause qemu_reset_requested_get(void)
1466 return reset_requested;
1469 static int qemu_shutdown_requested(void)
1471 return atomic_xchg(&shutdown_requested, SHUTDOWN_CAUSE_NONE);
1474 static void qemu_kill_report(void)
1476 if (!qtest_driver() && shutdown_signal) {
1477 if (shutdown_pid == 0) {
1478 /* This happens for eg ^C at the terminal, so it's worth
1479 * avoiding printing an odd message in that case.
1481 error_report("terminating on signal %d", shutdown_signal);
1483 char *shutdown_cmd = qemu_get_pid_name(shutdown_pid);
1485 error_report("terminating on signal %d from pid " FMT_pid " (%s)",
1486 shutdown_signal, shutdown_pid,
1487 shutdown_cmd ? shutdown_cmd : "<unknown process>");
1488 g_free(shutdown_cmd);
1490 shutdown_signal = 0;
1494 static ShutdownCause qemu_reset_requested(void)
1496 ShutdownCause r = reset_requested;
1498 if (r && replay_checkpoint(CHECKPOINT_RESET_REQUESTED)) {
1499 reset_requested = SHUTDOWN_CAUSE_NONE;
1502 return SHUTDOWN_CAUSE_NONE;
1505 static int qemu_suspend_requested(void)
1507 int r = suspend_requested;
1508 if (r && replay_checkpoint(CHECKPOINT_SUSPEND_REQUESTED)) {
1509 suspend_requested = 0;
1515 static WakeupReason qemu_wakeup_requested(void)
1517 return wakeup_reason;
1520 static int qemu_powerdown_requested(void)
1522 int r = powerdown_requested;
1523 powerdown_requested = 0;
1527 static int qemu_debug_requested(void)
1529 int r = debug_requested;
1530 debug_requested = 0;
1534 void qemu_exit_preconfig_request(void)
1536 preconfig_exit_requested = true;
1540 * Reset the VM. Issue an event unless @reason is SHUTDOWN_CAUSE_NONE.
1542 void qemu_system_reset(ShutdownCause reason)
1546 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1548 cpu_synchronize_all_states();
1550 if (mc && mc->reset) {
1551 mc->reset(current_machine);
1553 qemu_devices_reset();
1555 if (reason && reason != SHUTDOWN_CAUSE_SUBSYSTEM_RESET) {
1556 qapi_event_send_reset(shutdown_caused_by_guest(reason), reason);
1558 cpu_synchronize_all_post_reset();
1562 * Wake the VM after suspend.
1564 static void qemu_system_wakeup(void)
1568 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1570 if (mc && mc->wakeup) {
1571 mc->wakeup(current_machine);
1575 void qemu_system_guest_panicked(GuestPanicInformation *info)
1577 qemu_log_mask(LOG_GUEST_ERROR, "Guest crashed");
1580 current_cpu->crash_occurred = true;
1582 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE,
1584 vm_stop(RUN_STATE_GUEST_PANICKED);
1586 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_POWEROFF,
1588 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_PANIC);
1592 if (info->type == GUEST_PANIC_INFORMATION_TYPE_HYPER_V) {
1593 qemu_log_mask(LOG_GUEST_ERROR, "\nHV crash parameters: (%#"PRIx64
1594 " %#"PRIx64" %#"PRIx64" %#"PRIx64" %#"PRIx64")\n",
1595 info->u.hyper_v.arg1,
1596 info->u.hyper_v.arg2,
1597 info->u.hyper_v.arg3,
1598 info->u.hyper_v.arg4,
1599 info->u.hyper_v.arg5);
1600 } else if (info->type == GUEST_PANIC_INFORMATION_TYPE_S390) {
1601 qemu_log_mask(LOG_GUEST_ERROR, " on cpu %d: %s\n"
1602 "PSW: 0x%016" PRIx64 " 0x%016" PRIx64"\n",
1604 S390CrashReason_str(info->u.s390.reason),
1605 info->u.s390.psw_mask,
1606 info->u.s390.psw_addr);
1608 qapi_free_GuestPanicInformation(info);
1612 void qemu_system_reset_request(ShutdownCause reason)
1614 if (no_reboot && reason != SHUTDOWN_CAUSE_SUBSYSTEM_RESET) {
1615 shutdown_requested = reason;
1617 reset_requested = reason;
1620 qemu_notify_event();
1623 static void qemu_system_suspend(void)
1626 notifier_list_notify(&suspend_notifiers, NULL);
1627 runstate_set(RUN_STATE_SUSPENDED);
1628 qapi_event_send_suspend();
1631 void qemu_system_suspend_request(void)
1633 if (runstate_check(RUN_STATE_SUSPENDED)) {
1636 suspend_requested = 1;
1638 qemu_notify_event();
1641 void qemu_register_suspend_notifier(Notifier *notifier)
1643 notifier_list_add(&suspend_notifiers, notifier);
1646 void qemu_system_wakeup_request(WakeupReason reason, Error **errp)
1648 trace_system_wakeup_request(reason);
1650 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1652 "Unable to wake up: guest is not in suspended state");
1655 if (!(wakeup_reason_mask & (1 << reason))) {
1658 runstate_set(RUN_STATE_RUNNING);
1659 wakeup_reason = reason;
1660 qemu_notify_event();
1663 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1666 wakeup_reason_mask |= (1 << reason);
1668 wakeup_reason_mask &= ~(1 << reason);
1672 void qemu_register_wakeup_notifier(Notifier *notifier)
1674 notifier_list_add(&wakeup_notifiers, notifier);
1677 void qemu_register_wakeup_support(void)
1679 wakeup_suspend_enabled = true;
1682 bool qemu_wakeup_suspend_enabled(void)
1684 return wakeup_suspend_enabled;
1687 void qemu_system_killed(int signal, pid_t pid)
1689 shutdown_signal = signal;
1693 /* Cannot call qemu_system_shutdown_request directly because
1694 * we are in a signal handler.
1696 shutdown_requested = SHUTDOWN_CAUSE_HOST_SIGNAL;
1697 qemu_notify_event();
1700 void qemu_system_shutdown_request(ShutdownCause reason)
1702 trace_qemu_system_shutdown_request(reason);
1703 replay_shutdown_request(reason);
1704 shutdown_requested = reason;
1705 qemu_notify_event();
1708 static void qemu_system_powerdown(void)
1710 qapi_event_send_powerdown();
1711 notifier_list_notify(&powerdown_notifiers, NULL);
1714 static void qemu_system_shutdown(ShutdownCause cause)
1716 qapi_event_send_shutdown(shutdown_caused_by_guest(cause), cause);
1717 notifier_list_notify(&shutdown_notifiers, &cause);
1720 void qemu_system_powerdown_request(void)
1722 trace_qemu_system_powerdown_request();
1723 powerdown_requested = 1;
1724 qemu_notify_event();
1727 void qemu_register_powerdown_notifier(Notifier *notifier)
1729 notifier_list_add(&powerdown_notifiers, notifier);
1732 void qemu_register_shutdown_notifier(Notifier *notifier)
1734 notifier_list_add(&shutdown_notifiers, notifier);
1737 void qemu_system_debug_request(void)
1739 debug_requested = 1;
1740 qemu_notify_event();
1743 static bool main_loop_should_exit(void)
1746 ShutdownCause request;
1748 if (runstate_check(RUN_STATE_FINISH_MIGRATE)) {
1751 if (preconfig_exit_requested) {
1752 if (runstate_check(RUN_STATE_PRECONFIG)) {
1753 runstate_set(RUN_STATE_PRELAUNCH);
1755 preconfig_exit_requested = false;
1758 if (qemu_debug_requested()) {
1759 vm_stop(RUN_STATE_DEBUG);
1761 if (qemu_suspend_requested()) {
1762 qemu_system_suspend();
1764 request = qemu_shutdown_requested();
1767 qemu_system_shutdown(request);
1769 vm_stop(RUN_STATE_SHUTDOWN);
1774 request = qemu_reset_requested();
1777 qemu_system_reset(request);
1779 if (!runstate_check(RUN_STATE_RUNNING) &&
1780 !runstate_check(RUN_STATE_INMIGRATE)) {
1781 runstate_set(RUN_STATE_PRELAUNCH);
1784 if (qemu_wakeup_requested()) {
1786 qemu_system_wakeup();
1787 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
1788 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
1790 qapi_event_send_wakeup();
1792 if (qemu_powerdown_requested()) {
1793 qemu_system_powerdown();
1795 if (qemu_vmstop_requested(&r)) {
1801 static void main_loop(void)
1803 #ifdef CONFIG_PROFILER
1806 while (!main_loop_should_exit()) {
1807 #ifdef CONFIG_PROFILER
1808 ti = profile_getclock();
1810 main_loop_wait(false);
1811 #ifdef CONFIG_PROFILER
1812 dev_time += profile_getclock() - ti;
1817 static void version(void)
1819 printf("QEMU emulator version " QEMU_FULL_VERSION "\n"
1820 QEMU_COPYRIGHT "\n");
1823 static void help(int exitcode)
1826 printf("usage: %s [options] [disk_image]\n\n"
1827 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1828 error_get_progname());
1830 #define QEMU_OPTIONS_GENERATE_HELP
1831 #include "qemu-options-wrapper.h"
1833 printf("\nDuring emulation, the following keys are useful:\n"
1834 "ctrl-alt-f toggle full screen\n"
1835 "ctrl-alt-n switch to virtual console 'n'\n"
1836 "ctrl-alt toggle mouse and keyboard grab\n"
1838 "When using -nographic, press 'ctrl-a h' to get some help.\n"
1840 QEMU_HELP_BOTTOM "\n");
1845 #define HAS_ARG 0x0001
1847 typedef struct QEMUOption {
1854 static const QEMUOption qemu_options[] = {
1855 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1856 #define QEMU_OPTIONS_GENERATE_OPTIONS
1857 #include "qemu-options-wrapper.h"
1861 typedef struct VGAInterfaceInfo {
1862 const char *opt_name; /* option name */
1863 const char *name; /* human-readable name */
1864 /* Class names indicating that support is available.
1865 * If no class is specified, the interface is always available */
1866 const char *class_names[2];
1869 static const VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
1872 .name = "no graphic card",
1876 .name = "standard VGA",
1877 .class_names = { "VGA", "isa-vga" },
1880 .opt_name = "cirrus",
1881 .name = "Cirrus VGA",
1882 .class_names = { "cirrus-vga", "isa-cirrus-vga" },
1885 .opt_name = "vmware",
1886 .name = "VMWare SVGA",
1887 .class_names = { "vmware-svga" },
1890 .opt_name = "virtio",
1891 .name = "Virtio VGA",
1892 .class_names = { "virtio-vga" },
1897 .class_names = { "qxl-vga" },
1901 .name = "TCX framebuffer",
1902 .class_names = { "SUNW,tcx" },
1906 .name = "CG3 framebuffer",
1907 .class_names = { "cgthree" },
1910 .opt_name = "xenfb",
1911 .name = "Xen paravirtualized framebuffer",
1915 static bool vga_interface_available(VGAInterfaceType t)
1917 const VGAInterfaceInfo *ti = &vga_interfaces[t];
1919 assert(t < VGA_TYPE_MAX);
1920 return !ti->class_names[0] ||
1921 object_class_by_name(ti->class_names[0]) ||
1922 object_class_by_name(ti->class_names[1]);
1926 get_default_vga_model(const MachineClass *machine_class)
1928 if (machine_class->default_display) {
1929 return machine_class->default_display;
1930 } else if (vga_interface_available(VGA_CIRRUS)) {
1932 } else if (vga_interface_available(VGA_STD)) {
1939 static void select_vgahw(const MachineClass *machine_class, const char *p)
1944 if (g_str_equal(p, "help")) {
1945 const char *def = get_default_vga_model(machine_class);
1947 for (t = 0; t < VGA_TYPE_MAX; t++) {
1948 const VGAInterfaceInfo *ti = &vga_interfaces[t];
1950 if (vga_interface_available(t) && ti->opt_name) {
1951 printf("%-20s %s%s\n", ti->opt_name, ti->name ?: "",
1952 g_str_equal(ti->opt_name, def) ? " (default)" : "");
1958 assert(vga_interface_type == VGA_NONE);
1959 for (t = 0; t < VGA_TYPE_MAX; t++) {
1960 const VGAInterfaceInfo *ti = &vga_interfaces[t];
1961 if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
1962 if (!vga_interface_available(t)) {
1963 error_report("%s not available", ti->name);
1966 vga_interface_type = t;
1970 if (t == VGA_TYPE_MAX) {
1972 error_report("unknown vga type: %s", p);
1976 const char *nextopt;
1978 if (strstart(opts, ",retrace=", &nextopt)) {
1980 if (strstart(opts, "dumb", &nextopt))
1981 vga_retrace_method = VGA_RETRACE_DUMB;
1982 else if (strstart(opts, "precise", &nextopt))
1983 vga_retrace_method = VGA_RETRACE_PRECISE;
1984 else goto invalid_vga;
1985 } else goto invalid_vga;
1990 static void parse_display_qapi(const char *optarg)
1992 DisplayOptions *opts;
1995 v = qobject_input_visitor_new_str(optarg, "type", &error_fatal);
1997 visit_type_DisplayOptions(v, NULL, &opts, &error_fatal);
1998 QAPI_CLONE_MEMBERS(DisplayOptions, &dpy, opts);
2000 qapi_free_DisplayOptions(opts);
2004 DisplayOptions *qmp_query_display_options(Error **errp)
2006 return QAPI_CLONE(DisplayOptions, &dpy);
2009 static void parse_display(const char *p)
2013 if (strstart(p, "sdl", &opts)) {
2015 * sdl DisplayType needs hand-crafted parser instead of
2016 * parse_display_qapi() due to some options not in
2017 * DisplayOptions, specifically:
2019 * Already deprecated.
2020 * - ctrl_grab + alt_grab
2021 * Not clear yet what happens to them long-term. Should
2022 * replaced by something better or deprecated and dropped.
2024 dpy.type = DISPLAY_TYPE_SDL;
2026 const char *nextopt;
2028 if (strstart(opts, ",alt_grab=", &nextopt)) {
2030 if (strstart(opts, "on", &nextopt)) {
2032 } else if (strstart(opts, "off", &nextopt)) {
2035 goto invalid_sdl_args;
2037 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2039 if (strstart(opts, "on", &nextopt)) {
2041 } else if (strstart(opts, "off", &nextopt)) {
2044 goto invalid_sdl_args;
2046 } else if (strstart(opts, ",window_close=", &nextopt)) {
2048 dpy.has_window_close = true;
2049 if (strstart(opts, "on", &nextopt)) {
2050 dpy.window_close = true;
2051 } else if (strstart(opts, "off", &nextopt)) {
2052 dpy.window_close = false;
2054 goto invalid_sdl_args;
2056 } else if (strstart(opts, ",gl=", &nextopt)) {
2059 if (strstart(opts, "on", &nextopt)) {
2060 dpy.gl = DISPLAYGL_MODE_ON;
2061 } else if (strstart(opts, "core", &nextopt)) {
2062 dpy.gl = DISPLAYGL_MODE_CORE;
2063 } else if (strstart(opts, "es", &nextopt)) {
2064 dpy.gl = DISPLAYGL_MODE_ES;
2065 } else if (strstart(opts, "off", &nextopt)) {
2066 dpy.gl = DISPLAYGL_MODE_OFF;
2068 goto invalid_sdl_args;
2072 error_report("invalid SDL option string");
2077 } else if (strstart(p, "vnc", &opts)) {
2079 * vnc isn't a (local) DisplayType but a protocol for remote
2083 vnc_parse(opts + 1, &error_fatal);
2085 error_report("VNC requires a display argument vnc=<display>");
2089 parse_display_qapi(p);
2093 char *qemu_find_file(int type, const char *name)
2099 /* Try the name as a straight path first */
2100 if (access(name, R_OK) == 0) {
2101 trace_load_file(name, name);
2102 return g_strdup(name);
2106 case QEMU_FILE_TYPE_BIOS:
2109 case QEMU_FILE_TYPE_KEYMAP:
2110 subdir = "keymaps/";
2116 for (i = 0; i < data_dir_idx; i++) {
2117 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2118 if (access(buf, R_OK) == 0) {
2119 trace_load_file(name, buf);
2127 static void qemu_add_data_dir(const char *path)
2134 if (data_dir_idx == ARRAY_SIZE(data_dir)) {
2137 for (i = 0; i < data_dir_idx; i++) {
2138 if (strcmp(data_dir[i], path) == 0) {
2139 return; /* duplicate */
2142 data_dir[data_dir_idx++] = g_strdup(path);
2145 static inline bool nonempty_str(const char *str)
2150 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
2154 const char *name, *file, *str;
2155 FWCfgState *fw_cfg = (FWCfgState *) opaque;
2157 if (fw_cfg == NULL) {
2158 error_setg(errp, "fw_cfg device not available");
2161 name = qemu_opt_get(opts, "name");
2162 file = qemu_opt_get(opts, "file");
2163 str = qemu_opt_get(opts, "string");
2165 /* we need name and either a file or the content string */
2166 if (!(nonempty_str(name) && (nonempty_str(file) || nonempty_str(str)))) {
2167 error_setg(errp, "invalid argument(s)");
2170 if (nonempty_str(file) && nonempty_str(str)) {
2171 error_setg(errp, "file and string are mutually exclusive");
2174 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
2175 error_setg(errp, "name too long (max. %d char)",
2176 FW_CFG_MAX_FILE_PATH - 1);
2179 if (strncmp(name, "opt/", 4) != 0) {
2180 warn_report("externally provided fw_cfg item names "
2181 "should be prefixed with \"opt/\"");
2183 if (nonempty_str(str)) {
2184 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
2185 buf = g_memdup(str, size);
2188 if (!g_file_get_contents(file, &buf, &size, &err)) {
2189 error_setg(errp, "can't load %s: %s", file, err->message);
2194 /* For legacy, keep user files in a specific global order. */
2195 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
2196 fw_cfg_add_file(fw_cfg, name, buf, size);
2197 fw_cfg_reset_order_override(fw_cfg);
2201 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
2203 return qdev_device_help(opts);
2206 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
2210 dev = qdev_device_add(opts, errp);
2211 if (!dev && *errp) {
2212 error_report_err(*errp);
2215 object_unref(OBJECT(dev));
2220 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2222 Error *local_err = NULL;
2224 if (!qemu_chr_new_from_opts(opts, NULL, &local_err)) {
2226 error_propagate(errp, local_err);
2234 #ifdef CONFIG_VIRTFS
2235 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2237 return qemu_fsdev_add(opts, errp);
2241 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
2245 bool pretty = false;
2246 const char *chardev;
2249 mode = qemu_opt_get(opts, "mode");
2253 if (strcmp(mode, "readline") == 0) {
2255 } else if (strcmp(mode, "control") == 0) {
2258 error_setg(errp, "unknown monitor mode \"%s\"", mode);
2262 if (!qmp && qemu_opt_get(opts, "pretty")) {
2263 warn_report("'pretty' is deprecated for HMP monitors, it has no effect "
2264 "and will be removed in future versions");
2266 if (qemu_opt_get_bool(opts, "pretty", 0)) {
2270 chardev = qemu_opt_get(opts, "chardev");
2272 error_report("chardev is required");
2275 chr = qemu_chr_find(chardev);
2277 error_setg(errp, "chardev \"%s\" not found", chardev);
2282 monitor_init_qmp(chr, pretty);
2284 monitor_init_hmp(chr, true);
2289 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
2291 static int monitor_device_index = 0;
2296 if (strstart(optarg, "chardev:", &p)) {
2297 snprintf(label, sizeof(label), "%s", p);
2299 snprintf(label, sizeof(label), "compat_monitor%d",
2300 monitor_device_index);
2301 opts = qemu_chr_parse_compat(label, optarg, true);
2303 error_report("parse error: %s", optarg);
2308 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
2309 qemu_opt_set(opts, "mode", mode, &error_abort);
2310 qemu_opt_set(opts, "chardev", label, &error_abort);
2311 if (!strcmp(mode, "control")) {
2312 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
2314 assert(pretty == false);
2316 monitor_device_index++;
2319 struct device_config {
2321 DEV_USB, /* -usbdevice */
2323 DEV_SERIAL, /* -serial */
2324 DEV_PARALLEL, /* -parallel */
2325 DEV_DEBUGCON, /* -debugcon */
2326 DEV_GDB, /* -gdb, -s */
2327 DEV_SCLP, /* s390 sclp */
2329 const char *cmdline;
2331 QTAILQ_ENTRY(device_config) next;
2334 static QTAILQ_HEAD(, device_config) device_configs =
2335 QTAILQ_HEAD_INITIALIZER(device_configs);
2337 static void add_device_config(int type, const char *cmdline)
2339 struct device_config *conf;
2341 conf = g_malloc0(sizeof(*conf));
2343 conf->cmdline = cmdline;
2344 loc_save(&conf->loc);
2345 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2348 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2350 struct device_config *conf;
2353 QTAILQ_FOREACH(conf, &device_configs, next) {
2354 if (conf->type != type)
2356 loc_push_restore(&conf->loc);
2357 rc = func(conf->cmdline);
2358 loc_pop(&conf->loc);
2366 static int serial_parse(const char *devname)
2368 int index = num_serial_hds;
2371 if (strcmp(devname, "none") == 0)
2373 snprintf(label, sizeof(label), "serial%d", index);
2374 serial_hds = g_renew(Chardev *, serial_hds, index + 1);
2376 serial_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
2377 if (!serial_hds[index]) {
2378 error_report("could not connect serial device"
2379 " to character backend '%s'", devname);
2386 Chardev *serial_hd(int i)
2389 if (i < num_serial_hds) {
2390 return serial_hds[i];
2395 int serial_max_hds(void)
2397 return num_serial_hds;
2400 static int parallel_parse(const char *devname)
2402 static int index = 0;
2405 if (strcmp(devname, "none") == 0)
2407 if (index == MAX_PARALLEL_PORTS) {
2408 error_report("too many parallel ports");
2411 snprintf(label, sizeof(label), "parallel%d", index);
2412 parallel_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
2413 if (!parallel_hds[index]) {
2414 error_report("could not connect parallel device"
2415 " to character backend '%s'", devname);
2422 static int debugcon_parse(const char *devname)
2426 if (!qemu_chr_new_mux_mon("debugcon", devname, NULL)) {
2427 error_report("invalid character backend '%s'", devname);
2430 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2432 error_report("already have a debugcon device");
2435 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
2436 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
2440 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
2442 const MachineClass *mc1 = a, *mc2 = b;
2445 if (mc1->family == NULL) {
2446 if (mc2->family == NULL) {
2447 /* Compare standalone machine types against each other; they sort
2448 * in increasing order.
2450 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
2451 object_class_get_name(OBJECT_CLASS(mc2)));
2454 /* Standalone machine types sort after families. */
2458 if (mc2->family == NULL) {
2459 /* Families sort before standalone machine types. */
2463 /* Families sort between each other alphabetically increasingly. */
2464 res = strcmp(mc1->family, mc2->family);
2469 /* Within the same family, machine types sort in decreasing order. */
2470 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
2471 object_class_get_name(OBJECT_CLASS(mc1)));
2474 static MachineClass *machine_parse(const char *name, GSList *machines)
2479 if (is_help_option(name)) {
2480 printf("Supported machines are:\n");
2481 machines = g_slist_sort(machines, machine_class_cmp);
2482 for (el = machines; el; el = el->next) {
2483 MachineClass *mc = el->data;
2485 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
2487 printf("%-20s %s%s%s\n", mc->name, mc->desc,
2488 mc->is_default ? " (default)" : "",
2489 mc->deprecation_reason ? " (deprecated)" : "");
2494 mc = find_machine(name, machines);
2496 error_report("unsupported machine type");
2497 error_printf("Use -machine help to list supported machines\n");
2503 void qemu_add_exit_notifier(Notifier *notify)
2505 notifier_list_add(&exit_notifiers, notify);
2508 void qemu_remove_exit_notifier(Notifier *notify)
2510 notifier_remove(notify);
2513 static void qemu_run_exit_notifiers(void)
2515 notifier_list_notify(&exit_notifiers, NULL);
2518 static const char *pid_file;
2519 static Notifier qemu_unlink_pidfile_notifier;
2521 static void qemu_unlink_pidfile(Notifier *n, void *data)
2528 bool machine_init_done;
2530 void qemu_add_machine_init_done_notifier(Notifier *notify)
2532 notifier_list_add(&machine_init_done_notifiers, notify);
2533 if (machine_init_done) {
2534 notify->notify(notify, NULL);
2538 void qemu_remove_machine_init_done_notifier(Notifier *notify)
2540 notifier_remove(notify);
2543 static void qemu_run_machine_init_done_notifiers(void)
2545 machine_init_done = true;
2546 notifier_list_notify(&machine_init_done_notifiers, NULL);
2549 static const QEMUOption *lookup_opt(int argc, char **argv,
2550 const char **poptarg, int *poptind)
2552 const QEMUOption *popt;
2553 int optind = *poptind;
2554 char *r = argv[optind];
2557 loc_set_cmdline(argv, optind, 1);
2559 /* Treat --foo the same as -foo. */
2562 popt = qemu_options;
2565 error_report("invalid option");
2568 if (!strcmp(popt->name, r + 1))
2572 if (popt->flags & HAS_ARG) {
2573 if (optind >= argc) {
2574 error_report("requires an argument");
2577 optarg = argv[optind++];
2578 loc_set_cmdline(argv, optind - 2, 2);
2589 static MachineClass *select_machine(void)
2591 GSList *machines = object_class_get_list(TYPE_MACHINE, false);
2592 MachineClass *machine_class = find_default_machine(machines);
2597 loc_push_none(&loc);
2599 opts = qemu_get_machine_opts();
2600 qemu_opts_loc_restore(opts);
2602 optarg = qemu_opt_get(opts, "type");
2604 machine_class = machine_parse(optarg, machines);
2607 if (!machine_class) {
2608 error_report("No machine specified, and there is no default");
2609 error_printf("Use -machine help to list supported machines\n");
2614 g_slist_free(machines);
2615 return machine_class;
2618 static int machine_set_property(void *opaque,
2619 const char *name, const char *value,
2622 Object *obj = OBJECT(opaque);
2623 Error *local_err = NULL;
2626 if (strcmp(name, "type") == 0) {
2630 qom_name = g_strdup(name);
2631 for (p = qom_name; *p; p++) {
2637 object_property_parse(obj, value, qom_name, &local_err);
2641 error_propagate(errp, local_err);
2650 * Initial object creation happens before all other
2651 * QEMU data types are created. The majority of objects
2652 * can be created at this point. The rng-egd object
2653 * cannot be created here, as it depends on the chardev
2656 static bool object_create_initial(const char *type, QemuOpts *opts)
2658 if (user_creatable_print_help(type, opts)) {
2663 * Objects should not be made "delayed" without a reason. If you
2664 * add one, state the reason in a comment!
2667 /* Reason: rng-egd property "chardev" */
2668 if (g_str_equal(type, "rng-egd")) {
2672 #if defined(CONFIG_VHOST_USER) && defined(CONFIG_LINUX)
2673 /* Reason: cryptodev-vhost-user property "chardev" */
2674 if (g_str_equal(type, "cryptodev-vhost-user")) {
2680 * Reason: filter-* property "netdev" etc.
2682 if (g_str_equal(type, "filter-buffer") ||
2683 g_str_equal(type, "filter-dump") ||
2684 g_str_equal(type, "filter-mirror") ||
2685 g_str_equal(type, "filter-redirector") ||
2686 g_str_equal(type, "colo-compare") ||
2687 g_str_equal(type, "filter-rewriter") ||
2688 g_str_equal(type, "filter-replay")) {
2692 /* Memory allocation by backends needs to be done
2693 * after configure_accelerator() (due to the tcg_enabled()
2694 * checks at memory_region_init_*()).
2696 * Also, allocation of large amounts of memory may delay
2697 * chardev initialization for too long, and trigger timeouts
2698 * on software that waits for a monitor socket to be created
2701 if (g_str_has_prefix(type, "memory-backend-")) {
2710 * The remainder of object creation happens after the
2711 * creation of chardev, fsdev, net clients and device data types.
2713 static bool object_create_delayed(const char *type, QemuOpts *opts)
2715 return !object_create_initial(type, opts);
2719 static void set_memory_options(uint64_t *ram_slots, ram_addr_t *maxram_size,
2723 const char *mem_str;
2724 const ram_addr_t default_ram_size = mc->default_ram_size;
2725 QemuOpts *opts = qemu_find_opts_singleton("memory");
2728 loc_push_none(&loc);
2729 qemu_opts_loc_restore(opts);
2732 mem_str = qemu_opt_get(opts, "size");
2735 error_report("missing 'size' option value");
2739 sz = qemu_opt_get_size(opts, "size", ram_size);
2741 /* Fix up legacy suffix-less format */
2742 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2743 uint64_t overflow_check = sz;
2746 if (sz / MiB != overflow_check) {
2747 error_report("too large 'size' option value");
2753 /* backward compatibility behaviour for case "-m 0" */
2755 sz = default_ram_size;
2758 sz = QEMU_ALIGN_UP(sz, 8192);
2760 if (ram_size != sz) {
2761 error_report("ram size too large");
2765 /* store value for the future use */
2766 qemu_opt_set_number(opts, "size", ram_size, &error_abort);
2767 *maxram_size = ram_size;
2769 if (qemu_opt_get(opts, "maxmem")) {
2772 sz = qemu_opt_get_size(opts, "maxmem", 0);
2773 slots = qemu_opt_get_number(opts, "slots", 0);
2774 if (sz < ram_size) {
2775 error_report("invalid value of -m option maxmem: "
2776 "maximum memory size (0x%" PRIx64 ") must be at least "
2777 "the initial memory size (0x" RAM_ADDR_FMT ")",
2780 } else if (slots && sz == ram_size) {
2781 error_report("invalid value of -m option maxmem: "
2782 "memory slots were specified but maximum memory size "
2783 "(0x%" PRIx64 ") is equal to the initial memory size "
2784 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
2790 } else if (qemu_opt_get(opts, "slots")) {
2791 error_report("invalid -m option value: missing 'maxmem' option");
2798 static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
2802 g = g_malloc0(sizeof(*g));
2803 g->driver = qemu_opt_get(opts, "driver");
2804 g->property = qemu_opt_get(opts, "property");
2805 g->value = qemu_opt_get(opts, "value");
2806 qdev_prop_register_global(g);
2810 static int qemu_read_default_config_file(void)
2814 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
2815 if (ret < 0 && ret != -ENOENT) {
2822 static void user_register_global_props(void)
2824 qemu_opts_foreach(qemu_find_opts("global"),
2825 global_init_func, NULL, NULL);
2828 int main(int argc, char **argv, char **envp)
2831 int snapshot, linux_boot;
2832 const char *initrd_filename;
2833 const char *kernel_filename, *kernel_cmdline;
2834 const char *boot_order = NULL;
2835 const char *boot_once = NULL;
2837 QemuOpts *opts, *machine_opts;
2838 QemuOpts *icount_opts = NULL, *accel_opts = NULL;
2839 QemuOptsList *olist;
2842 const char *loadvm = NULL;
2843 MachineClass *machine_class;
2844 const char *cpu_option;
2845 const char *vga_model = NULL;
2846 const char *qtest_chrdev = NULL;
2847 const char *qtest_log = NULL;
2848 const char *incoming = NULL;
2849 bool userconfig = true;
2850 bool nographic = false;
2851 int display_remote = 0;
2852 const char *log_mask = NULL;
2853 const char *log_file = NULL;
2854 char *trace_file = NULL;
2855 ram_addr_t maxram_size;
2856 uint64_t ram_slots = 0;
2857 FILE *vmstate_dump_file = NULL;
2858 Error *main_loop_err = NULL;
2860 bool list_data_dirs = false;
2862 BlockdevOptionsQueue bdo_queue = QSIMPLEQ_HEAD_INITIALIZER(bdo_queue);
2863 QemuPluginList plugin_list = QTAILQ_HEAD_INITIALIZER(plugin_list);
2865 os_set_line_buffering();
2867 error_init(argv[0]);
2868 module_call_init(MODULE_INIT_TRACE);
2870 qemu_init_cpu_list();
2871 qemu_init_cpu_loop();
2873 qemu_mutex_lock_iothread();
2875 atexit(qemu_run_exit_notifiers);
2876 qemu_init_exec_dir(argv[0]);
2878 module_call_init(MODULE_INIT_QOM);
2880 qemu_add_opts(&qemu_drive_opts);
2881 qemu_add_drive_opts(&qemu_legacy_drive_opts);
2882 qemu_add_drive_opts(&qemu_common_drive_opts);
2883 qemu_add_drive_opts(&qemu_drive_opts);
2884 qemu_add_drive_opts(&bdrv_runtime_opts);
2885 qemu_add_opts(&qemu_chardev_opts);
2886 qemu_add_opts(&qemu_device_opts);
2887 qemu_add_opts(&qemu_netdev_opts);
2888 qemu_add_opts(&qemu_nic_opts);
2889 qemu_add_opts(&qemu_net_opts);
2890 qemu_add_opts(&qemu_rtc_opts);
2891 qemu_add_opts(&qemu_global_opts);
2892 qemu_add_opts(&qemu_mon_opts);
2893 qemu_add_opts(&qemu_trace_opts);
2894 qemu_plugin_add_opts();
2895 qemu_add_opts(&qemu_option_rom_opts);
2896 qemu_add_opts(&qemu_machine_opts);
2897 qemu_add_opts(&qemu_accel_opts);
2898 qemu_add_opts(&qemu_mem_opts);
2899 qemu_add_opts(&qemu_smp_opts);
2900 qemu_add_opts(&qemu_boot_opts);
2901 qemu_add_opts(&qemu_add_fd_opts);
2902 qemu_add_opts(&qemu_object_opts);
2903 qemu_add_opts(&qemu_tpmdev_opts);
2904 qemu_add_opts(&qemu_realtime_opts);
2905 qemu_add_opts(&qemu_overcommit_opts);
2906 qemu_add_opts(&qemu_msg_opts);
2907 qemu_add_opts(&qemu_name_opts);
2908 qemu_add_opts(&qemu_numa_opts);
2909 qemu_add_opts(&qemu_icount_opts);
2910 qemu_add_opts(&qemu_semihosting_config_opts);
2911 qemu_add_opts(&qemu_fw_cfg_opts);
2912 module_call_init(MODULE_INIT_OPTS);
2915 precopy_infrastructure_init();
2916 postcopy_infrastructure_init();
2917 monitor_init_globals();
2919 if (qcrypto_init(&err) < 0) {
2920 error_reportf_err(err, "cannot initialize crypto: ");
2924 QTAILQ_INIT(&vm_change_state_head);
2925 os_setup_early_signal_handling();
2932 bdrv_init_with_whitelist();
2936 /* first pass of option parsing */
2938 while (optind < argc) {
2939 if (argv[optind][0] != '-') {
2943 const QEMUOption *popt;
2945 popt = lookup_opt(argc, argv, &optarg, &optind);
2946 switch (popt->index) {
2947 case QEMU_OPTION_nouserconfig:
2955 if (qemu_read_default_config_file() < 0) {
2960 /* second pass of option parsing */
2965 if (argv[optind][0] != '-') {
2966 loc_set_cmdline(argv, optind, 1);
2967 drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2969 const QEMUOption *popt;
2971 popt = lookup_opt(argc, argv, &optarg, &optind);
2972 if (!(popt->arch_mask & arch_type)) {
2973 error_report("Option not supported for this target");
2976 switch(popt->index) {
2977 case QEMU_OPTION_cpu:
2978 /* hw initialization will check this */
2979 cpu_option = optarg;
2981 case QEMU_OPTION_hda:
2982 case QEMU_OPTION_hdb:
2983 case QEMU_OPTION_hdc:
2984 case QEMU_OPTION_hdd:
2985 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2988 case QEMU_OPTION_blockdev:
2991 BlockdevOptionsQueueEntry *bdo;
2993 v = qobject_input_visitor_new_str(optarg, "driver",
2996 bdo = g_new(BlockdevOptionsQueueEntry, 1);
2997 visit_type_BlockdevOptions(v, NULL, &bdo->bdo,
3000 loc_save(&bdo->loc);
3001 QSIMPLEQ_INSERT_TAIL(&bdo_queue, bdo, entry);
3004 case QEMU_OPTION_drive:
3005 if (drive_def(optarg) == NULL) {
3009 case QEMU_OPTION_set:
3010 if (qemu_set_option(optarg) != 0)
3013 case QEMU_OPTION_global:
3014 if (qemu_global_option(optarg) != 0)
3017 case QEMU_OPTION_mtdblock:
3018 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3020 case QEMU_OPTION_sd:
3021 drive_add(IF_SD, -1, optarg, SD_OPTS);
3023 case QEMU_OPTION_pflash:
3024 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
3026 case QEMU_OPTION_snapshot:
3028 Error *blocker = NULL;
3030 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
3032 replay_add_blocker(blocker);
3035 case QEMU_OPTION_numa:
3036 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
3042 case QEMU_OPTION_display:
3043 parse_display(optarg);
3045 case QEMU_OPTION_nographic:
3046 olist = qemu_find_opts("machine");
3047 qemu_opts_parse_noisily(olist, "graphics=off", false);
3049 dpy.type = DISPLAY_TYPE_NONE;
3051 case QEMU_OPTION_curses:
3052 #ifdef CONFIG_CURSES
3053 dpy.type = DISPLAY_TYPE_CURSES;
3055 error_report("curses or iconv support is disabled");
3059 case QEMU_OPTION_portrait:
3060 graphic_rotate = 90;
3062 case QEMU_OPTION_rotate:
3063 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3064 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3065 graphic_rotate != 180 && graphic_rotate != 270) {
3066 error_report("only 90, 180, 270 deg rotation is available");
3070 case QEMU_OPTION_kernel:
3071 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg,
3074 case QEMU_OPTION_initrd:
3075 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg,
3078 case QEMU_OPTION_append:
3079 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg,
3082 case QEMU_OPTION_dtb:
3083 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg,
3086 case QEMU_OPTION_cdrom:
3087 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3089 case QEMU_OPTION_boot:
3090 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
3096 case QEMU_OPTION_fda:
3097 case QEMU_OPTION_fdb:
3098 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3101 case QEMU_OPTION_no_fd_bootchk:
3104 case QEMU_OPTION_netdev:
3106 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3110 case QEMU_OPTION_nic:
3112 if (net_client_parse(qemu_find_opts("nic"), optarg) == -1) {
3116 case QEMU_OPTION_net:
3118 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3122 #ifdef CONFIG_LIBISCSI
3123 case QEMU_OPTION_iscsi:
3124 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
3131 case QEMU_OPTION_bt:
3132 warn_report("The bluetooth subsystem is deprecated and will "
3133 "be removed soon. If the bluetooth subsystem is "
3134 "still useful for you, please send a mail to "
3136 add_device_config(DEV_BT, optarg);
3138 case QEMU_OPTION_audio_help:
3139 audio_legacy_help();
3142 case QEMU_OPTION_audiodev:
3143 audio_parse_option(optarg);
3145 case QEMU_OPTION_soundhw:
3146 select_soundhw (optarg);
3151 case QEMU_OPTION_version:
3156 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
3163 case QEMU_OPTION_tpmdev:
3164 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3169 case QEMU_OPTION_mempath:
3172 case QEMU_OPTION_mem_prealloc:
3181 case QEMU_OPTION_DFILTER:
3182 qemu_set_dfilter_ranges(optarg, &error_fatal);
3184 case QEMU_OPTION_seed:
3185 qemu_guest_random_seed_main(optarg, &error_fatal);
3188 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3190 case QEMU_OPTION_gdb:
3191 add_device_config(DEV_GDB, optarg);
3194 if (is_help_option(optarg)) {
3195 list_data_dirs = true;
3197 qemu_add_data_dir(optarg);
3200 case QEMU_OPTION_bios:
3201 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg,
3204 case QEMU_OPTION_singlestep:
3211 keyboard_layout = optarg;
3213 case QEMU_OPTION_vga:
3222 w = strtol(p, (char **)&p, 10);
3225 error_report("invalid resolution or depth");
3231 h = strtol(p, (char **)&p, 10);
3236 depth = strtol(p, (char **)&p, 10);
3237 if (depth != 1 && depth != 2 && depth != 4 &&
3238 depth != 8 && depth != 15 && depth != 16 &&
3239 depth != 24 && depth != 32)
3241 } else if (*p == '\0') {
3242 depth = graphic_depth;
3249 graphic_depth = depth;
3252 case QEMU_OPTION_echr:
3255 term_escape_char = strtol(optarg, &r, 0);
3257 printf("Bad argument to echr\n");
3260 case QEMU_OPTION_monitor:
3261 default_monitor = 0;
3262 if (strncmp(optarg, "none", 4)) {
3263 monitor_parse(optarg, "readline", false);
3266 case QEMU_OPTION_qmp:
3267 monitor_parse(optarg, "control", false);
3268 default_monitor = 0;
3270 case QEMU_OPTION_qmp_pretty:
3271 monitor_parse(optarg, "control", true);
3272 default_monitor = 0;
3274 case QEMU_OPTION_mon:
3275 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3280 default_monitor = 0;
3282 case QEMU_OPTION_chardev:
3283 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3289 case QEMU_OPTION_fsdev:
3290 olist = qemu_find_opts("fsdev");
3292 error_report("fsdev support is disabled");
3295 opts = qemu_opts_parse_noisily(olist, optarg, true);
3300 case QEMU_OPTION_virtfs: {
3303 const char *writeout, *sock_fd, *socket, *path, *security_model,
3306 olist = qemu_find_opts("virtfs");
3308 error_report("virtfs support is disabled");
3311 opts = qemu_opts_parse_noisily(olist, optarg, true);
3316 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3317 qemu_opt_get(opts, "mount_tag") == NULL) {
3318 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3321 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3322 qemu_opts_id(opts) ?:
3323 qemu_opt_get(opts, "mount_tag"),
3326 error_report("duplicate or invalid fsdev id: %s",
3327 qemu_opt_get(opts, "mount_tag"));
3331 writeout = qemu_opt_get(opts, "writeout");
3333 #ifdef CONFIG_SYNC_FILE_RANGE
3334 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3336 error_report("writeout=immediate not supported "
3337 "on this platform");
3341 qemu_opt_set(fsdev, "fsdriver",
3342 qemu_opt_get(opts, "fsdriver"), &error_abort);
3343 path = qemu_opt_get(opts, "path");
3345 qemu_opt_set(fsdev, "path", path, &error_abort);
3347 security_model = qemu_opt_get(opts, "security_model");
3348 if (security_model) {
3349 qemu_opt_set(fsdev, "security_model", security_model,
3352 socket = qemu_opt_get(opts, "socket");
3354 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3356 sock_fd = qemu_opt_get(opts, "sock_fd");
3358 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3361 qemu_opt_set_bool(fsdev, "readonly",
3362 qemu_opt_get_bool(opts, "readonly", 0),
3364 multidevs = qemu_opt_get(opts, "multidevs");
3366 qemu_opt_set(fsdev, "multidevs", multidevs, &error_abort);
3368 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3370 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3371 qemu_opt_set(device, "fsdev",
3372 qemu_opts_id(fsdev), &error_abort);
3373 qemu_opt_set(device, "mount_tag",
3374 qemu_opt_get(opts, "mount_tag"), &error_abort);
3377 case QEMU_OPTION_serial:
3378 add_device_config(DEV_SERIAL, optarg);
3380 if (strncmp(optarg, "mon:", 4) == 0) {
3381 default_monitor = 0;
3384 case QEMU_OPTION_watchdog:
3386 error_report("only one watchdog option may be given");
3391 case QEMU_OPTION_watchdog_action:
3392 if (select_watchdog_action(optarg) == -1) {
3393 error_report("unknown -watchdog-action parameter");
3397 case QEMU_OPTION_parallel:
3398 add_device_config(DEV_PARALLEL, optarg);
3399 default_parallel = 0;
3400 if (strncmp(optarg, "mon:", 4) == 0) {
3401 default_monitor = 0;
3404 case QEMU_OPTION_debugcon:
3405 add_device_config(DEV_DEBUGCON, optarg);
3407 case QEMU_OPTION_loadvm:
3410 case QEMU_OPTION_full_screen:
3411 dpy.has_full_screen = true;
3412 dpy.full_screen = true;
3414 case QEMU_OPTION_alt_grab:
3417 case QEMU_OPTION_ctrl_grab:
3420 case QEMU_OPTION_no_quit:
3421 dpy.has_window_close = true;
3422 dpy.window_close = false;
3424 case QEMU_OPTION_sdl:
3426 dpy.type = DISPLAY_TYPE_SDL;
3429 error_report("SDL support is disabled");
3432 case QEMU_OPTION_pidfile:
3435 case QEMU_OPTION_win2k_hack:
3436 win2k_install_hack = 1;
3438 case QEMU_OPTION_acpitable:
3439 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3444 acpi_table_add(opts, &error_fatal);
3446 case QEMU_OPTION_smbios:
3447 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3452 smbios_entry_add(opts, &error_fatal);
3454 case QEMU_OPTION_fwcfg:
3455 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3461 case QEMU_OPTION_preconfig:
3462 preconfig_exit_requested = false;
3464 case QEMU_OPTION_enable_kvm:
3465 olist = qemu_find_opts("machine");
3466 qemu_opts_parse_noisily(olist, "accel=kvm", false);
3469 case QEMU_OPTION_machine:
3470 olist = qemu_find_opts("machine");
3471 opts = qemu_opts_parse_noisily(olist, optarg, true);
3476 case QEMU_OPTION_no_kvm:
3477 olist = qemu_find_opts("machine");
3478 qemu_opts_parse_noisily(olist, "accel=tcg", false);
3480 case QEMU_OPTION_accel:
3481 accel_opts = qemu_opts_parse_noisily(qemu_find_opts("accel"),
3483 optarg = qemu_opt_get(accel_opts, "accel");
3484 if (!optarg || is_help_option(optarg)) {
3485 printf("Accelerators supported in QEMU binary:\n");
3486 GSList *el, *accel_list = object_class_get_list(TYPE_ACCEL,
3488 for (el = accel_list; el; el = el->next) {
3489 gchar *typename = g_strdup(object_class_get_name(
3490 OBJECT_CLASS(el->data)));
3491 /* omit qtest which is used for tests only */
3492 if (g_strcmp0(typename, ACCEL_CLASS_NAME("qtest")) &&
3493 g_str_has_suffix(typename, ACCEL_CLASS_SUFFIX)) {
3494 gchar **optname = g_strsplit(typename,
3495 ACCEL_CLASS_SUFFIX, 0);
3496 printf("%s\n", optname[0]);
3501 g_slist_free(accel_list);
3504 if (optarg && strchr(optarg, ':')) {
3505 error_report("Don't use ':' with -accel, "
3506 "use -M accel=... for now instead");
3509 opts = qemu_opts_create(qemu_find_opts("machine"), NULL,
3510 false, &error_abort);
3511 qemu_opt_set(opts, "accel", optarg, &error_abort);
3513 case QEMU_OPTION_usb:
3514 olist = qemu_find_opts("machine");
3515 qemu_opts_parse_noisily(olist, "usb=on", false);
3517 case QEMU_OPTION_usbdevice:
3518 error_report("'-usbdevice' is deprecated, please use "
3519 "'-device usb-...' instead");
3520 olist = qemu_find_opts("machine");
3521 qemu_opts_parse_noisily(olist, "usb=on", false);
3522 add_device_config(DEV_USB, optarg);
3524 case QEMU_OPTION_device:
3525 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3530 case QEMU_OPTION_smp:
3531 if (!qemu_opts_parse_noisily(qemu_find_opts("smp-opts"),
3536 case QEMU_OPTION_vnc:
3537 vnc_parse(optarg, &error_fatal);
3539 case QEMU_OPTION_no_acpi:
3542 case QEMU_OPTION_no_hpet:
3545 case QEMU_OPTION_no_reboot:
3548 case QEMU_OPTION_no_shutdown:
3551 case QEMU_OPTION_show_cursor:
3554 case QEMU_OPTION_uuid:
3555 if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
3556 error_report("failed to parse UUID string: wrong format");
3559 qemu_uuid_set = true;
3561 case QEMU_OPTION_option_rom:
3562 if (nb_option_roms >= MAX_OPTION_ROMS) {
3563 error_report("too many option ROMs");
3566 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3571 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3572 option_rom[nb_option_roms].bootindex =
3573 qemu_opt_get_number(opts, "bootindex", -1);
3574 if (!option_rom[nb_option_roms].name) {
3575 error_report("Option ROM file is not specified");
3580 case QEMU_OPTION_semihosting:
3581 qemu_semihosting_enable();
3583 case QEMU_OPTION_semihosting_config:
3584 if (qemu_semihosting_config_options(optarg) != 0) {
3588 case QEMU_OPTION_name:
3589 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3595 case QEMU_OPTION_prom_env:
3596 if (nb_prom_envs >= MAX_PROM_ENVS) {
3597 error_report("too many prom variables");
3600 prom_envs[nb_prom_envs] = optarg;
3603 case QEMU_OPTION_old_param:
3606 case QEMU_OPTION_rtc:
3607 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3613 case QEMU_OPTION_tb_size:
3615 error_report("TCG is disabled");
3618 if (qemu_strtoul(optarg, NULL, 0, &tcg_tb_size) < 0) {
3619 error_report("Invalid argument to -tb-size");
3623 case QEMU_OPTION_icount:
3624 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3630 case QEMU_OPTION_incoming:
3632 runstate_set(RUN_STATE_INMIGRATE);
3636 case QEMU_OPTION_only_migratable:
3637 only_migratable = 1;
3639 case QEMU_OPTION_nodefaults:
3642 case QEMU_OPTION_xen_domid:
3643 if (!(xen_available())) {
3644 error_report("Option not supported for this target");
3647 xen_domid = atoi(optarg);
3649 case QEMU_OPTION_xen_attach:
3650 if (!(xen_available())) {
3651 error_report("Option not supported for this target");
3654 xen_mode = XEN_ATTACH;
3656 case QEMU_OPTION_xen_domid_restrict:
3657 if (!(xen_available())) {
3658 error_report("Option not supported for this target");
3661 xen_domid_restrict = true;
3663 case QEMU_OPTION_trace:
3665 trace_file = trace_opt_parse(optarg);
3667 case QEMU_OPTION_plugin:
3668 qemu_plugin_opt_parse(optarg, &plugin_list);
3670 case QEMU_OPTION_readconfig:
3672 int ret = qemu_read_config_file(optarg);
3674 error_report("read config %s: %s", optarg,
3680 case QEMU_OPTION_spice:
3681 olist = qemu_find_opts("spice");
3683 error_report("spice support is disabled");
3686 opts = qemu_opts_parse_noisily(olist, optarg, false);
3692 case QEMU_OPTION_writeconfig:
3695 if (strcmp(optarg, "-") == 0) {
3698 fp = fopen(optarg, "w");
3700 error_report("open %s: %s", optarg,
3705 qemu_config_write(fp);
3711 case QEMU_OPTION_qtest:
3712 qtest_chrdev = optarg;
3714 case QEMU_OPTION_qtest_log:
3717 case QEMU_OPTION_sandbox:
3718 olist = qemu_find_opts("sandbox");
3720 #ifndef CONFIG_SECCOMP
3721 error_report("-sandbox support is not enabled "
3722 "in this QEMU binary");
3727 opts = qemu_opts_parse_noisily(olist, optarg, true);
3732 case QEMU_OPTION_add_fd:
3734 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
3740 error_report("File descriptor passing is disabled on this "
3745 case QEMU_OPTION_object:
3746 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
3752 case QEMU_OPTION_realtime:
3753 warn_report("'-realtime mlock=...' is deprecated, please use "
3754 "'-overcommit mem-lock=...' instead");
3755 opts = qemu_opts_parse_noisily(qemu_find_opts("realtime"),
3760 /* Don't override the -overcommit option if set */
3761 enable_mlock = enable_mlock ||
3762 qemu_opt_get_bool(opts, "mlock", true);
3764 case QEMU_OPTION_overcommit:
3765 opts = qemu_opts_parse_noisily(qemu_find_opts("overcommit"),
3770 /* Don't override the -realtime option if set */
3771 enable_mlock = enable_mlock ||
3772 qemu_opt_get_bool(opts, "mem-lock", false);
3773 enable_cpu_pm = qemu_opt_get_bool(opts, "cpu-pm", false);
3775 case QEMU_OPTION_msg:
3776 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
3781 configure_msg(opts);
3783 case QEMU_OPTION_dump_vmstate:
3784 if (vmstate_dump_file) {
3785 error_report("only one '-dump-vmstate' "
3786 "option may be given");
3789 vmstate_dump_file = fopen(optarg, "w");
3790 if (vmstate_dump_file == NULL) {
3791 error_report("open %s: %s", optarg, strerror(errno));
3795 case QEMU_OPTION_enable_sync_profile:
3798 case QEMU_OPTION_nouserconfig:
3799 /* Nothing to be parsed here. Especially, do not error out below. */
3802 if (os_parse_cmd_args(popt->index, optarg)) {
3803 error_report("Option not supported in this build");
3810 * Clear error location left behind by the loop.
3811 * Best done right after the loop. Do not insert code here!
3815 user_register_global_props();
3817 replay_configure(icount_opts);
3819 if (incoming && !preconfig_exit_requested) {
3820 error_report("'preconfig' and 'incoming' options are "
3821 "mutually exclusive");
3825 configure_rtc(qemu_find_opts_singleton("rtc"));
3827 machine_class = select_machine();
3828 object_set_machine_compat_props(machine_class->compat_props);
3830 set_memory_options(&ram_slots, &maxram_size, machine_class);
3833 rcu_disable_atfork();
3835 if (pid_file && !qemu_write_pidfile(pid_file, &err)) {
3836 error_reportf_err(err, "cannot create PID file: ");
3840 qemu_unlink_pidfile_notifier.notify = qemu_unlink_pidfile;
3841 qemu_add_exit_notifier(&qemu_unlink_pidfile_notifier);
3843 if (qemu_init_main_loop(&main_loop_err)) {
3844 error_report_err(main_loop_err);
3848 #ifdef CONFIG_SECCOMP
3849 olist = qemu_find_opts_err("sandbox", NULL);
3851 qemu_opts_foreach(olist, parse_sandbox, NULL, &error_fatal);
3855 qemu_opts_foreach(qemu_find_opts("name"),
3856 parse_name, NULL, &error_fatal);
3859 qemu_opts_foreach(qemu_find_opts("add-fd"),
3860 parse_add_fd, NULL, &error_fatal);
3862 qemu_opts_foreach(qemu_find_opts("add-fd"),
3863 cleanup_add_fd, NULL, &error_fatal);
3866 current_machine = MACHINE(object_new(object_class_get_name(
3867 OBJECT_CLASS(machine_class))));
3868 if (machine_help_func(qemu_get_machine_opts(), current_machine)) {
3871 object_property_add_child(object_get_root(), "machine",
3872 OBJECT(current_machine), &error_abort);
3873 object_property_add_child(container_get(OBJECT(current_machine),
3875 "sysbus", OBJECT(sysbus_get_default()),
3878 if (machine_class->minimum_page_bits) {
3879 if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
3880 /* This would be a board error: specifying a minimum smaller than
3881 * a target's compile-time fixed setting.
3883 g_assert_not_reached();
3887 cpu_exec_init_all();
3889 if (machine_class->hw_version) {
3890 qemu_set_hw_version(machine_class->hw_version);
3893 if (cpu_option && is_help_option(cpu_option)) {
3894 list_cpus(cpu_option);
3898 if (!trace_init_backends()) {
3901 trace_init_file(trace_file);
3903 /* Open the logfile at this point and set the log mask if necessary.
3906 qemu_set_log_filename(log_file, &error_fatal);
3911 mask = qemu_str_to_log_mask(log_mask);
3913 qemu_print_log_usage(stdout);
3921 /* add configured firmware directories */
3922 dirs = g_strsplit(CONFIG_QEMU_FIRMWAREPATH, G_SEARCHPATH_SEPARATOR_S, 0);
3923 for (i = 0; dirs[i] != NULL; i++) {
3924 qemu_add_data_dir(dirs[i]);
3928 /* try to find datadir relative to the executable path */
3929 dir = os_find_datadir();
3930 qemu_add_data_dir(dir);
3933 /* add the datadir specified when building */
3934 qemu_add_data_dir(CONFIG_QEMU_DATADIR);
3936 /* -L help lists the data directories and exits. */
3937 if (list_data_dirs) {
3938 for (i = 0; i < data_dir_idx; i++) {
3939 printf("%s\n", data_dir[i]);
3944 /* machine_class: default to UP */
3945 machine_class->max_cpus = machine_class->max_cpus ?: 1;
3946 machine_class->min_cpus = machine_class->min_cpus ?: 1;
3947 machine_class->default_cpus = machine_class->default_cpus ?: 1;
3949 /* default to machine_class->default_cpus */
3950 current_machine->smp.cpus = machine_class->default_cpus;
3951 current_machine->smp.max_cpus = machine_class->default_cpus;
3952 current_machine->smp.cores = 1;
3953 current_machine->smp.threads = 1;
3955 machine_class->smp_parse(current_machine,
3956 qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
3958 /* sanity-check smp_cpus and max_cpus against machine_class */
3959 if (current_machine->smp.cpus < machine_class->min_cpus) {
3960 error_report("Invalid SMP CPUs %d. The min CPUs "
3961 "supported by machine '%s' is %d",
3962 current_machine->smp.cpus,
3963 machine_class->name, machine_class->min_cpus);
3966 if (current_machine->smp.max_cpus > machine_class->max_cpus) {
3967 error_report("Invalid SMP CPUs %d. The max CPUs "
3968 "supported by machine '%s' is %d",
3969 current_machine->smp.max_cpus,
3970 machine_class->name, machine_class->max_cpus);
3975 * Get the default machine options from the machine if it is not already
3976 * specified either by the configuration file or by the command line.
3978 if (machine_class->default_machine_opts) {
3979 qemu_opts_set_defaults(qemu_find_opts("machine"),
3980 machine_class->default_machine_opts, 0);
3983 /* process plugin before CPUs are created, but once -smp has been parsed */
3984 if (qemu_plugin_load_list(&plugin_list)) {
3988 qemu_opts_foreach(qemu_find_opts("device"),
3989 default_driver_check, NULL, NULL);
3990 qemu_opts_foreach(qemu_find_opts("global"),
3991 default_driver_check, NULL, NULL);
3993 if (!vga_model && !default_vga) {
3994 vga_interface_type = VGA_DEVICE;
3996 if (!has_defaults || machine_class->no_serial) {
3999 if (!has_defaults || machine_class->no_parallel) {
4000 default_parallel = 0;
4002 if (!has_defaults || machine_class->no_floppy) {
4005 if (!has_defaults || machine_class->no_cdrom) {
4008 if (!has_defaults || machine_class->no_sdcard) {
4011 if (!has_defaults) {
4012 default_monitor = 0;
4017 if (is_daemonized()) {
4018 if (!preconfig_exit_requested) {
4019 error_report("'preconfig' and 'daemonize' options are "
4020 "mutually exclusive");
4024 /* According to documentation and historically, -nographic redirects
4025 * serial port, parallel port and monitor to stdio, which does not work
4026 * with -daemonize. We can redirect these to null instead, but since
4027 * -nographic is legacy, let's just error out.
4028 * We disallow -nographic only if all other ports are not redirected
4029 * explicitly, to not break existing legacy setups which uses
4030 * -nographic _and_ redirects all ports explicitly - this is valid
4031 * usage, -nographic is just a no-op in this case.
4034 && (default_parallel || default_serial || default_monitor)) {
4035 error_report("-nographic cannot be used with -daemonize");
4038 #ifdef CONFIG_CURSES
4039 if (dpy.type == DISPLAY_TYPE_CURSES) {
4040 error_report("curses display cannot be used with -daemonize");
4047 if (default_parallel)
4048 add_device_config(DEV_PARALLEL, "null");
4049 if (default_serial && default_monitor) {
4050 add_device_config(DEV_SERIAL, "mon:stdio");
4053 add_device_config(DEV_SERIAL, "stdio");
4054 if (default_monitor)
4055 monitor_parse("stdio", "readline", false);
4059 add_device_config(DEV_SERIAL, "vc:80Cx24C");
4060 if (default_parallel)
4061 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
4062 if (default_monitor)
4063 monitor_parse("vc:80Cx24C", "readline", false);
4066 #if defined(CONFIG_VNC)
4067 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
4071 if (dpy.type == DISPLAY_TYPE_DEFAULT && !display_remote) {
4072 if (!qemu_display_find_default(&dpy)) {
4073 dpy.type = DISPLAY_TYPE_NONE;
4074 #if defined(CONFIG_VNC)
4075 vnc_parse("localhost:0,to=99,id=default", &error_abort);
4079 if (dpy.type == DISPLAY_TYPE_DEFAULT) {
4080 dpy.type = DISPLAY_TYPE_NONE;
4083 if ((alt_grab || ctrl_grab) && dpy.type != DISPLAY_TYPE_SDL) {
4084 error_report("-alt-grab and -ctrl-grab are only valid "
4085 "for SDL, ignoring option");
4087 if (dpy.has_window_close &&
4088 (dpy.type != DISPLAY_TYPE_GTK && dpy.type != DISPLAY_TYPE_SDL)) {
4089 error_report("-no-quit is only valid for GTK and SDL, "
4093 qemu_display_early_init(&dpy);
4094 qemu_console_early_init();
4096 if (dpy.has_gl && dpy.gl != DISPLAYGL_MODE_OFF && display_opengl == 0) {
4097 #if defined(CONFIG_OPENGL)
4098 error_report("OpenGL is not supported by the display");
4100 error_report("OpenGL support is disabled");
4108 qemu_opts_foreach(qemu_find_opts("object"),
4109 user_creatable_add_opts_foreach,
4110 object_create_initial, &error_fatal);
4112 qemu_opts_foreach(qemu_find_opts("chardev"),
4113 chardev_init_func, NULL, &error_fatal);
4114 /* now chardevs have been created we may have semihosting to connect */
4115 qemu_semihosting_connect_chardevs();
4117 #ifdef CONFIG_VIRTFS
4118 qemu_opts_foreach(qemu_find_opts("fsdev"),
4119 fsdev_init_func, NULL, &error_fatal);
4122 if (qemu_opts_foreach(qemu_find_opts("device"),
4123 device_help_func, NULL, NULL)) {
4128 * Note: we need to create block backends before
4129 * machine_set_property(), so machine properties can refer to
4132 configure_blockdev(&bdo_queue, machine_class, snapshot);
4134 machine_opts = qemu_get_machine_opts();
4135 qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
4137 current_machine->ram_size = ram_size;
4138 current_machine->maxram_size = maxram_size;
4139 current_machine->ram_slots = ram_slots;
4142 * Note: uses machine properties such as kernel-irqchip, must run
4143 * after machine_set_property().
4145 configure_accelerator(current_machine, argv[0]);
4148 * Beware, QOM objects created before this point miss global and
4149 * compat properties.
4151 * Global properties get set up by qdev_prop_register_global(),
4152 * called from user_register_global_props(), and certain option
4153 * desugaring. Also in CPU feature desugaring (buried in
4154 * parse_cpu_option()), which happens below this point, but may
4155 * only target the CPU type, which can only be created after
4156 * parse_cpu_option() returned the type.
4158 * Machine compat properties: object_set_machine_compat_props().
4159 * Accelerator compat props: object_set_accelerator_compat_props(),
4160 * called from configure_accelerator().
4163 if (!qtest_enabled() && machine_class->deprecation_reason) {
4164 error_report("Machine type '%s' is deprecated: %s",
4165 machine_class->name, machine_class->deprecation_reason);
4169 * Note: creates a QOM object, must run only after global and
4170 * compat properties have been set up.
4172 migration_object_init();
4175 qtest_server_init(qtest_chrdev, qtest_log, &error_fatal);
4178 machine_opts = qemu_get_machine_opts();
4179 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4180 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4181 kernel_cmdline = qemu_opt_get(machine_opts, "append");
4182 bios_name = qemu_opt_get(machine_opts, "firmware");
4184 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4186 boot_order = qemu_opt_get(opts, "order");
4188 validate_bootdevices(boot_order, &error_fatal);
4191 boot_once = qemu_opt_get(opts, "once");
4193 validate_bootdevices(boot_once, &error_fatal);
4196 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
4197 boot_strict = qemu_opt_get_bool(opts, "strict", false);
4201 boot_order = machine_class->default_boot_order;
4204 if (!kernel_cmdline) {
4205 kernel_cmdline = "";
4206 current_machine->kernel_cmdline = (char *)kernel_cmdline;
4209 linux_boot = (kernel_filename != NULL);
4211 if (!linux_boot && *kernel_cmdline != '\0') {
4212 error_report("-append only allowed with -kernel option");
4216 if (!linux_boot && initrd_filename != NULL) {
4217 error_report("-initrd only allowed with -kernel option");
4221 if (semihosting_enabled() && !semihosting_get_argc() && kernel_filename) {
4222 /* fall back to the -kernel/-append */
4223 semihosting_arg_fallback(kernel_filename, kernel_cmdline);
4226 /* spice needs the timers to be initialized by this point */
4231 if (!tcg_enabled()) {
4232 error_report("-icount is not allowed with hardware virtualization");
4235 configure_icount(icount_opts, &error_abort);
4236 qemu_opts_del(icount_opts);
4239 if (tcg_enabled()) {
4240 qemu_tcg_configure(accel_opts, &error_fatal);
4244 QemuOptsList *net = qemu_find_opts("net");
4245 qemu_opts_set(net, NULL, "type", "nic", &error_abort);
4247 qemu_opts_set(net, NULL, "type", "user", &error_abort);
4251 if (net_init_clients(&err) < 0) {
4252 error_report_err(err);
4256 qemu_opts_foreach(qemu_find_opts("object"),
4257 user_creatable_add_opts_foreach,
4258 object_create_delayed, &error_fatal);
4262 /* init the bluetooth world */
4263 if (foreach_device_config(DEV_BT, bt_parse))
4266 if (!xen_enabled()) {
4267 /* On 32-bit hosts, QEMU is limited by virtual address space */
4268 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4269 error_report("at most 2047 MB RAM can be simulated");
4276 dirty_bitmap_mig_init();
4278 qemu_opts_foreach(qemu_find_opts("mon"),
4279 mon_init_func, NULL, &error_fatal);
4281 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4283 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4285 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4288 /* If no default VGA is requested, the default is "none". */
4290 vga_model = get_default_vga_model(machine_class);
4293 select_vgahw(machine_class, vga_model);
4297 i = select_watchdog(watchdog);
4299 exit (i == 1 ? 1 : 0);
4302 /* This checkpoint is required by replay to separate prior clock
4303 reading from the other reads, because timer polling functions query
4304 clock values from the log. */
4305 replay_checkpoint(CHECKPOINT_INIT);
4306 qdev_machine_init();
4308 current_machine->boot_order = boot_order;
4310 /* parse features once if machine provides default cpu_type */
4311 current_machine->cpu_type = machine_class->default_cpu_type;
4313 current_machine->cpu_type = parse_cpu_option(cpu_option);
4315 parse_numa_opts(current_machine);
4317 /* do monitor/qmp handling at preconfig state if requested */
4320 audio_init_audiodevs();
4322 /* from here on runstate is RUN_STATE_PRELAUNCH */
4323 machine_run_board_init(current_machine);
4329 if (hax_enabled()) {
4333 qemu_opts_foreach(qemu_find_opts("fw_cfg"),
4334 parse_fw_cfg, fw_cfg_find(), &error_fatal);
4336 /* init USB devices */
4337 if (machine_usb(current_machine)) {
4338 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4342 /* Check if IGD GFX passthrough. */
4345 /* init generic devices */
4346 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
4347 qemu_opts_foreach(qemu_find_opts("device"),
4348 device_init_func, NULL, &error_fatal);
4350 cpu_synchronize_all_post_init();
4352 rom_reset_order_override();
4354 /* Did we create any drives that we failed to create a device for? */
4355 drive_check_orphaned();
4357 /* Don't warn about the default network setup that you get if
4358 * no command line -net or -netdev options are specified. There
4359 * are two cases that we would otherwise complain about:
4360 * (1) board doesn't support a NIC but the implicit "-net nic"
4362 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
4363 * sets up a nic that isn't connected to anything.
4365 if (!default_net && (!qtest_enabled() || has_defaults)) {
4366 net_check_clients();
4370 qemu_boot_set(boot_once, &error_fatal);
4371 qemu_register_reset(restore_boot_order, g_strdup(boot_order));
4374 /* init local displays */
4375 ds = init_displaystate();
4376 qemu_display_init(ds, &dpy);
4378 /* must be after terminal init, SDL library changes signal handlers */
4379 os_setup_signal_handling();
4381 /* init remote displays */
4383 qemu_opts_foreach(qemu_find_opts("vnc"),
4384 vnc_init_func, NULL, &error_fatal);
4388 qemu_spice_display_init();
4391 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
4395 qdev_machine_creation_done();
4397 /* TODO: once all bus devices are qdevified, this should be done
4398 * when bus is created by qdev.c */
4399 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4400 qemu_run_machine_init_done_notifiers();
4402 if (rom_check_and_register_reset() != 0) {
4403 error_report("rom check and register reset failed");
4409 /* This checkpoint is required by replay to separate prior clock
4410 reading from the other reads, because timer polling functions query
4411 clock values from the log. */
4412 replay_checkpoint(CHECKPOINT_RESET);
4413 qemu_system_reset(SHUTDOWN_CAUSE_NONE);
4414 register_global_state();
4416 Error *local_err = NULL;
4417 if (load_snapshot(loadvm, &local_err) < 0) {
4418 error_report_err(local_err);
4423 if (replay_mode != REPLAY_MODE_NONE) {
4424 replay_vmstate_init();
4427 qdev_prop_check_globals();
4428 if (vmstate_dump_file) {
4430 dump_vmstate_json_to_file(vmstate_dump_file);
4435 Error *local_err = NULL;
4436 qemu_start_incoming_migration(incoming, &local_err);
4438 error_reportf_err(local_err, "-incoming %s: ", incoming);
4441 } else if (autostart) {
4445 accel_setup_post(current_machine);
4450 gdbserver_cleanup();
4453 * cleaning up the migration object cancels any existing migration
4454 * try to do this early so that it also stops using devices.
4456 migration_shutdown();
4459 * We must cancel all block jobs while the block layer is drained,
4460 * or cancelling will be affected by throttling and thus may block
4461 * for an extended period of time.
4462 * vm_shutdown() will bdrv_drain_all(), so we may as well include
4463 * it in the drained section.
4464 * We do not need to end this section, because we do not want any
4465 * requests happening from here on anyway.
4467 bdrv_drain_all_begin();
4469 /* No more vcpu or device emulation activity beyond this point */
4473 job_cancel_sync_all();
4478 /* vhost-user must be cleaned up before chardevs. */
4484 user_creatable_cleanup();
4485 /* TODO: unref root container, check all devices are ok */