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
24 #include "qemu/osdep.h"
25 #include "qemu-version.h"
26 #include "qemu/cutils.h"
27 #include "qemu/help_option.h"
28 #include "qemu/uuid.h"
31 #include "sysemu/seccomp.h"
34 #if defined(CONFIG_VDE)
35 #include <libvdeplug.h>
39 #if defined(__APPLE__) || defined(main)
41 int qemu_main(int argc, char **argv, char **envp);
42 int main(int argc, char **argv)
44 return qemu_main(argc, argv, NULL);
47 #define main qemu_main
49 #endif /* CONFIG_SDL */
53 #define main qemu_main
54 #endif /* CONFIG_COCOA */
57 #include "qemu/error-report.h"
58 #include "qemu/sockets.h"
60 #include "hw/boards.h"
61 #include "sysemu/accel.h"
63 #include "hw/i386/pc.h"
64 #include "hw/isa/isa.h"
65 #include "hw/scsi/scsi.h"
67 #include "sysemu/watchdog.h"
68 #include "hw/smbios/smbios.h"
69 #include "hw/acpi/acpi.h"
70 #include "hw/xen/xen.h"
72 #include "hw/loader.h"
73 #include "monitor/qdev.h"
74 #include "sysemu/bt.h"
76 #include "net/slirp.h"
77 #include "monitor/monitor.h"
78 #include "ui/console.h"
80 #include "sysemu/sysemu.h"
81 #include "sysemu/numa.h"
82 #include "exec/gdbstub.h"
83 #include "qemu/timer.h"
84 #include "sysemu/char.h"
85 #include "qemu/bitmap.h"
87 #include "sysemu/blockdev.h"
88 #include "hw/block/block.h"
89 #include "migration/block.h"
90 #include "sysemu/tpm.h"
91 #include "sysemu/dma.h"
92 #include "audio/audio.h"
93 #include "migration/migration.h"
94 #include "sysemu/cpus.h"
95 #include "migration/colo.h"
96 #include "sysemu/kvm.h"
97 #include "sysemu/hax.h"
98 #include "qapi/qmp/qjson.h"
99 #include "qemu/option.h"
100 #include "qemu/config-file.h"
101 #include "qemu-options.h"
102 #include "qmp-commands.h"
103 #include "qemu/main-loop.h"
105 #include "fsdev/qemu-fsdev.h"
107 #include "sysemu/qtest.h"
109 #include "disas/disas.h"
112 #include "slirp/libslirp.h"
114 #include "trace-root.h"
115 #include "trace/control.h"
116 #include "qemu/queue.h"
117 #include "sysemu/arch_init.h"
119 #include "ui/qemu-spice.h"
120 #include "qapi/string-input-visitor.h"
121 #include "qapi/opts-visitor.h"
122 #include "qom/object_interfaces.h"
123 #include "qapi-event.h"
124 #include "exec/semihost.h"
125 #include "crypto/init.h"
126 #include "sysemu/replay.h"
127 #include "qapi/qmp/qerror.h"
128 #include "sysemu/iothread.h"
130 #define MAX_VIRTIO_CONSOLES 1
131 #define MAX_SCLP_CONSOLES 1
133 static const char *data_dir[16];
134 static int data_dir_idx;
135 const char *bios_name = NULL;
136 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
137 int request_opengl = -1;
139 const char* keyboard_layout = NULL;
141 const char *mem_path = NULL;
142 int mem_prealloc = 0; /* force preallocation of physical target memory */
143 bool enable_mlock = false;
145 NICInfo nd_table[MAX_NICS];
147 static int rtc_utc = 1;
148 static int rtc_date_offset = -1; /* -1 means no change */
149 QEMUClockType rtc_clock;
150 int vga_interface_type = VGA_NONE;
151 static int full_screen = 0;
152 static int no_frame = 0;
154 static bool grab_on_hover;
155 Chardev *serial_hds[MAX_SERIAL_PORTS];
156 Chardev *parallel_hds[MAX_PARALLEL_PORTS];
157 Chardev *virtcon_hds[MAX_VIRTIO_CONSOLES];
158 Chardev *sclp_hds[MAX_SCLP_CONSOLES];
159 int win2k_install_hack = 0;
165 int acpi_enabled = 1;
168 static int no_reboot;
171 int graphic_rotate = 0;
172 const char *watchdog;
173 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
176 const char *qemu_name;
179 unsigned int nb_prom_envs = 0;
180 const char *prom_envs[MAX_PROM_ENVS];
183 uint8_t *boot_splash_filedata;
184 size_t boot_splash_filedata_size;
185 uint8_t qemu_extra_params_fw[2];
186 int only_migratable; /* turn it off unless user states otherwise */
188 int icount_align_option;
190 /* The bytes in qemu_uuid are in the order specified by RFC4122, _not_ in the
191 * little-endian "wire format" described in the SMBIOS 2.6 specification.
196 static NotifierList exit_notifiers =
197 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
199 static NotifierList machine_init_done_notifiers =
200 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
204 enum xen_mode xen_mode = XEN_EMULATE;
206 static int has_defaults = 1;
207 static int default_serial = 1;
208 static int default_parallel = 1;
209 static int default_virtcon = 1;
210 static int default_sclp = 1;
211 static int default_monitor = 1;
212 static int default_floppy = 1;
213 static int default_cdrom = 1;
214 static int default_sdcard = 1;
215 static int default_vga = 1;
216 static int default_net = 1;
222 { .driver = "isa-serial", .flag = &default_serial },
223 { .driver = "isa-parallel", .flag = &default_parallel },
224 { .driver = "isa-fdc", .flag = &default_floppy },
225 { .driver = "floppy", .flag = &default_floppy },
226 { .driver = "ide-cd", .flag = &default_cdrom },
227 { .driver = "ide-hd", .flag = &default_cdrom },
228 { .driver = "ide-drive", .flag = &default_cdrom },
229 { .driver = "scsi-cd", .flag = &default_cdrom },
230 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
231 { .driver = "virtio-serial", .flag = &default_virtcon },
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 },
241 static QemuOptsList qemu_rtc_opts = {
243 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
247 .type = QEMU_OPT_STRING,
250 .type = QEMU_OPT_STRING,
253 .type = QEMU_OPT_STRING,
255 { /* end of list */ }
259 static QemuOptsList qemu_sandbox_opts = {
261 .implied_opt_name = "enable",
262 .head = QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts.head),
266 .type = QEMU_OPT_BOOL,
268 { /* end of list */ }
272 static QemuOptsList qemu_option_rom_opts = {
273 .name = "option-rom",
274 .implied_opt_name = "romfile",
275 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
279 .type = QEMU_OPT_NUMBER,
282 .type = QEMU_OPT_STRING,
284 { /* end of list */ }
288 static QemuOptsList qemu_machine_opts = {
290 .implied_opt_name = "type",
292 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
295 * no elements => accept any
296 * sanity checking will happen later
297 * when setting machine properties
303 static QemuOptsList qemu_boot_opts = {
305 .implied_opt_name = "order",
307 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
311 .type = QEMU_OPT_STRING,
314 .type = QEMU_OPT_STRING,
317 .type = QEMU_OPT_BOOL,
320 .type = QEMU_OPT_STRING,
322 .name = "splash-time",
323 .type = QEMU_OPT_STRING,
325 .name = "reboot-timeout",
326 .type = QEMU_OPT_STRING,
329 .type = QEMU_OPT_BOOL,
335 static QemuOptsList qemu_add_fd_opts = {
337 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
341 .type = QEMU_OPT_NUMBER,
342 .help = "file descriptor of which a duplicate is added to fd set",
345 .type = QEMU_OPT_NUMBER,
346 .help = "ID of the fd set to add fd to",
349 .type = QEMU_OPT_STRING,
350 .help = "free-form string used to describe fd",
352 { /* end of list */ }
356 static QemuOptsList qemu_object_opts = {
358 .implied_opt_name = "qom-type",
359 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
365 static QemuOptsList qemu_tpmdev_opts = {
367 .implied_opt_name = "type",
368 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
370 /* options are defined in the TPM backends */
371 { /* end of list */ }
375 static QemuOptsList qemu_realtime_opts = {
377 .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
381 .type = QEMU_OPT_BOOL,
383 { /* end of list */ }
387 static QemuOptsList qemu_msg_opts = {
389 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
393 .type = QEMU_OPT_BOOL,
395 { /* end of list */ }
399 static QemuOptsList qemu_name_opts = {
401 .implied_opt_name = "guest",
403 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
407 .type = QEMU_OPT_STRING,
408 .help = "Sets the name of the guest.\n"
409 "This name will be displayed in the SDL window caption.\n"
410 "The name will also be used for the VNC server",
413 .type = QEMU_OPT_STRING,
414 .help = "Sets the name of the QEMU process, as shown in top etc",
416 .name = "debug-threads",
417 .type = QEMU_OPT_BOOL,
418 .help = "When enabled, name the individual threads; defaults off.\n"
419 "NOTE: The thread names are for debugging and not a\n"
422 { /* End of list */ }
426 static QemuOptsList qemu_mem_opts = {
428 .implied_opt_name = "size",
429 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
434 .type = QEMU_OPT_SIZE,
438 .type = QEMU_OPT_NUMBER,
442 .type = QEMU_OPT_SIZE,
444 { /* end of list */ }
448 static QemuOptsList qemu_icount_opts = {
450 .implied_opt_name = "shift",
452 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
456 .type = QEMU_OPT_STRING,
459 .type = QEMU_OPT_BOOL,
462 .type = QEMU_OPT_BOOL,
465 .type = QEMU_OPT_STRING,
468 .type = QEMU_OPT_STRING,
470 .name = "rrsnapshot",
471 .type = QEMU_OPT_STRING,
473 { /* end of list */ }
477 static QemuOptsList qemu_semihosting_config_opts = {
478 .name = "semihosting-config",
479 .implied_opt_name = "enable",
480 .head = QTAILQ_HEAD_INITIALIZER(qemu_semihosting_config_opts.head),
484 .type = QEMU_OPT_BOOL,
487 .type = QEMU_OPT_STRING,
490 .type = QEMU_OPT_STRING,
492 { /* end of list */ }
496 static QemuOptsList qemu_fw_cfg_opts = {
498 .implied_opt_name = "name",
499 .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
503 .type = QEMU_OPT_STRING,
504 .help = "Sets the fw_cfg name of the blob to be inserted",
507 .type = QEMU_OPT_STRING,
508 .help = "Sets the name of the file from which\n"
509 "the fw_cfg blob will be loaded",
512 .type = QEMU_OPT_STRING,
513 .help = "Sets content of the blob to be inserted from a string",
515 { /* end of list */ }
520 * Get machine options
522 * Returns: machine options (never null).
524 QemuOpts *qemu_get_machine_opts(void)
526 return qemu_find_opts_singleton("machine");
529 const char *qemu_get_vm_name(void)
534 static void res_free(void)
536 g_free(boot_splash_filedata);
537 boot_splash_filedata = NULL;
540 static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
542 const char *driver = qemu_opt_get(opts, "driver");
547 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
548 if (strcmp(default_list[i].driver, driver) != 0)
550 *(default_list[i].flag) = 0;
555 /***********************************************************/
558 static RunState current_run_state = RUN_STATE_PRELAUNCH;
560 /* We use RUN_STATE__MAX but any invalid value will do */
561 static RunState vmstop_requested = RUN_STATE__MAX;
562 static QemuMutex vmstop_lock;
567 } RunStateTransition;
569 static const RunStateTransition runstate_transitions_def[] = {
571 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
572 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
573 { RUN_STATE_DEBUG, RUN_STATE_PRELAUNCH },
575 { RUN_STATE_INMIGRATE, RUN_STATE_INTERNAL_ERROR },
576 { RUN_STATE_INMIGRATE, RUN_STATE_IO_ERROR },
577 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
578 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
579 { RUN_STATE_INMIGRATE, RUN_STATE_SHUTDOWN },
580 { RUN_STATE_INMIGRATE, RUN_STATE_SUSPENDED },
581 { RUN_STATE_INMIGRATE, RUN_STATE_WATCHDOG },
582 { RUN_STATE_INMIGRATE, RUN_STATE_GUEST_PANICKED },
583 { RUN_STATE_INMIGRATE, RUN_STATE_FINISH_MIGRATE },
584 { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH },
585 { RUN_STATE_INMIGRATE, RUN_STATE_POSTMIGRATE },
586 { RUN_STATE_INMIGRATE, RUN_STATE_COLO },
588 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
589 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
590 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PRELAUNCH },
592 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
593 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
594 { RUN_STATE_IO_ERROR, RUN_STATE_PRELAUNCH },
596 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
597 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
598 { RUN_STATE_PAUSED, RUN_STATE_PRELAUNCH },
599 { RUN_STATE_PAUSED, RUN_STATE_COLO},
601 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
602 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
603 { RUN_STATE_POSTMIGRATE, RUN_STATE_PRELAUNCH },
605 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
606 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
607 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
609 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
610 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
611 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PRELAUNCH },
612 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_COLO},
614 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
615 { RUN_STATE_RESTORE_VM, RUN_STATE_PRELAUNCH },
617 { RUN_STATE_COLO, RUN_STATE_RUNNING },
619 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
620 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
621 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
622 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
623 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
624 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
625 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
626 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
627 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
628 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
629 { RUN_STATE_RUNNING, RUN_STATE_COLO},
631 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
633 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
634 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
635 { RUN_STATE_SHUTDOWN, RUN_STATE_PRELAUNCH },
637 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
638 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
639 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
640 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
641 { RUN_STATE_SUSPENDED, RUN_STATE_PRELAUNCH },
642 { RUN_STATE_SUSPENDED, RUN_STATE_COLO},
644 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
645 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
646 { RUN_STATE_WATCHDOG, RUN_STATE_PRELAUNCH },
647 { RUN_STATE_WATCHDOG, RUN_STATE_COLO},
649 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
650 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
651 { RUN_STATE_GUEST_PANICKED, RUN_STATE_PRELAUNCH },
653 { RUN_STATE__MAX, RUN_STATE__MAX },
656 static bool runstate_valid_transitions[RUN_STATE__MAX][RUN_STATE__MAX];
658 bool runstate_check(RunState state)
660 return current_run_state == state;
663 bool runstate_store(char *str, size_t size)
665 const char *state = RunState_lookup[current_run_state];
666 size_t len = strlen(state) + 1;
671 memcpy(str, state, len);
675 static void runstate_init(void)
677 const RunStateTransition *p;
679 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
680 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE__MAX; p++) {
681 runstate_valid_transitions[p->from][p->to] = true;
684 qemu_mutex_init(&vmstop_lock);
687 /* This function will abort() on invalid state transitions */
688 void runstate_set(RunState new_state)
690 assert(new_state < RUN_STATE__MAX);
692 if (current_run_state == new_state) {
696 if (!runstate_valid_transitions[current_run_state][new_state]) {
697 error_report("invalid runstate transition: '%s' -> '%s'",
698 RunState_lookup[current_run_state],
699 RunState_lookup[new_state]);
702 trace_runstate_set(new_state);
703 current_run_state = new_state;
706 int runstate_is_running(void)
708 return runstate_check(RUN_STATE_RUNNING);
711 bool runstate_needs_reset(void)
713 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
714 runstate_check(RUN_STATE_SHUTDOWN);
717 StatusInfo *qmp_query_status(Error **errp)
719 StatusInfo *info = g_malloc0(sizeof(*info));
721 info->running = runstate_is_running();
722 info->singlestep = singlestep;
723 info->status = current_run_state;
728 bool qemu_vmstop_requested(RunState *r)
730 qemu_mutex_lock(&vmstop_lock);
731 *r = vmstop_requested;
732 vmstop_requested = RUN_STATE__MAX;
733 qemu_mutex_unlock(&vmstop_lock);
734 return *r < RUN_STATE__MAX;
737 void qemu_system_vmstop_request_prepare(void)
739 qemu_mutex_lock(&vmstop_lock);
742 void qemu_system_vmstop_request(RunState state)
744 vmstop_requested = state;
745 qemu_mutex_unlock(&vmstop_lock);
749 /***********************************************************/
750 /* real time host monotonic timer */
752 static time_t qemu_time(void)
754 return qemu_clock_get_ms(QEMU_CLOCK_HOST) / 1000;
757 /***********************************************************/
758 /* host time/date access */
759 void qemu_get_timedate(struct tm *tm, int offset)
761 time_t ti = qemu_time();
764 if (rtc_date_offset == -1) {
768 localtime_r(&ti, tm);
770 ti -= rtc_date_offset;
775 int qemu_timedate_diff(struct tm *tm)
779 if (rtc_date_offset == -1)
781 seconds = mktimegm(tm);
784 tmp.tm_isdst = -1; /* use timezone to figure it out */
785 seconds = mktime(&tmp);
788 seconds = mktimegm(tm) + rtc_date_offset;
790 return seconds - qemu_time();
793 static void configure_rtc_date_offset(const char *startdate, int legacy)
795 time_t rtc_start_date;
798 if (!strcmp(startdate, "now") && legacy) {
799 rtc_date_offset = -1;
801 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
809 } else if (sscanf(startdate, "%d-%d-%d",
821 rtc_start_date = mktimegm(&tm);
822 if (rtc_start_date == -1) {
824 error_report("invalid date format");
825 error_printf("valid formats: "
826 "'2006-06-17T16:01:21' or '2006-06-17'\n");
829 rtc_date_offset = qemu_time() - rtc_start_date;
833 static void configure_rtc(QemuOpts *opts)
837 value = qemu_opt_get(opts, "base");
839 if (!strcmp(value, "utc")) {
841 } else if (!strcmp(value, "localtime")) {
842 Error *blocker = NULL;
844 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
845 "-rtc base=localtime");
846 replay_add_blocker(blocker);
848 configure_rtc_date_offset(value, 0);
851 value = qemu_opt_get(opts, "clock");
853 if (!strcmp(value, "host")) {
854 rtc_clock = QEMU_CLOCK_HOST;
855 } else if (!strcmp(value, "rt")) {
856 rtc_clock = QEMU_CLOCK_REALTIME;
857 } else if (!strcmp(value, "vm")) {
858 rtc_clock = QEMU_CLOCK_VIRTUAL;
860 error_report("invalid option value '%s'", value);
864 value = qemu_opt_get(opts, "driftfix");
866 if (!strcmp(value, "slew")) {
867 static GlobalProperty slew_lost_ticks = {
868 .driver = "mc146818rtc",
869 .property = "lost_tick_policy",
873 qdev_prop_register_global(&slew_lost_ticks);
874 } else if (!strcmp(value, "none")) {
875 /* discard is default */
877 error_report("invalid option value '%s'", value);
883 /***********************************************************/
884 /* Bluetooth support */
887 static struct HCIInfo *hci_table[MAX_NICS];
889 struct HCIInfo *qemu_next_hci(void)
891 if (cur_hci == nb_hcis)
894 return hci_table[cur_hci++];
897 static int bt_hci_parse(const char *str)
902 if (nb_hcis >= MAX_NICS) {
903 error_report("too many bluetooth HCIs (max %i)", MAX_NICS);
916 bdaddr.b[5] = 0x56 + nb_hcis;
917 hci->bdaddr_set(hci, bdaddr.b);
919 hci_table[nb_hcis++] = hci;
924 static void bt_vhci_add(int vlan_id)
926 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
929 error_report("warning: adding a VHCI to an empty scatternet %i",
932 bt_vhci_init(bt_new_hci(vlan));
935 static struct bt_device_s *bt_device_add(const char *opt)
937 struct bt_scatternet_s *vlan;
939 char *endp = strstr(opt, ",vlan=");
940 int len = (endp ? endp - opt : strlen(opt)) + 1;
943 pstrcpy(devname, MIN(sizeof(devname), len), opt);
946 vlan_id = strtol(endp + 6, &endp, 0);
948 error_report("unrecognised bluetooth vlan Id");
953 vlan = qemu_find_bt_vlan(vlan_id);
956 error_report("warning: adding a slave device to an empty scatternet %i",
959 if (!strcmp(devname, "keyboard"))
960 return bt_keyboard_init(vlan);
962 error_report("unsupported bluetooth device '%s'", devname);
966 static int bt_parse(const char *opt)
968 const char *endp, *p;
971 if (strstart(opt, "hci", &endp)) {
972 if (!*endp || *endp == ',') {
974 if (!strstart(endp, ",vlan=", 0))
977 return bt_hci_parse(opt);
979 } else if (strstart(opt, "vhci", &endp)) {
980 if (!*endp || *endp == ',') {
982 if (strstart(endp, ",vlan=", &p)) {
983 vlan = strtol(p, (char **) &endp, 0);
985 error_report("bad scatternet '%s'", p);
989 error_report("bad parameter '%s'", endp + 1);
998 } else if (strstart(opt, "device:", &endp))
999 return !bt_device_add(endp);
1001 error_report("bad bluetooth parameter '%s'", opt);
1005 static int parse_sandbox(void *opaque, QemuOpts *opts, Error **errp)
1007 /* FIXME: change this to true for 1.3 */
1008 if (qemu_opt_get_bool(opts, "enable", false)) {
1009 #ifdef CONFIG_SECCOMP
1010 if (seccomp_start() < 0) {
1011 error_report("failed to install seccomp syscall filter "
1016 error_report("seccomp support is disabled");
1024 static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
1026 const char *proc_name;
1028 if (qemu_opt_get(opts, "debug-threads")) {
1029 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
1031 qemu_name = qemu_opt_get(opts, "guest");
1033 proc_name = qemu_opt_get(opts, "process");
1035 os_set_proc_name(proc_name);
1041 bool defaults_enabled(void)
1043 return has_defaults;
1047 static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1049 int fd, dupfd, flags;
1051 const char *fd_opaque = NULL;
1054 fd = qemu_opt_get_number(opts, "fd", -1);
1055 fdset_id = qemu_opt_get_number(opts, "set", -1);
1056 fd_opaque = qemu_opt_get(opts, "opaque");
1059 error_report("fd option is required and must be non-negative");
1063 if (fd <= STDERR_FILENO) {
1064 error_report("fd cannot be a standard I/O stream");
1069 * All fds inherited across exec() necessarily have FD_CLOEXEC
1070 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1072 flags = fcntl(fd, F_GETFD);
1073 if (flags == -1 || (flags & FD_CLOEXEC)) {
1074 error_report("fd is not valid or already in use");
1079 error_report("set option is required and must be non-negative");
1083 #ifdef F_DUPFD_CLOEXEC
1084 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1088 qemu_set_cloexec(dupfd);
1092 error_report("error duplicating fd: %s", strerror(errno));
1096 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1097 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
1104 static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1108 fd = qemu_opt_get_number(opts, "fd", -1);
1115 /***********************************************************/
1116 /* QEMU Block devices */
1118 #define HD_OPTS "media=disk"
1119 #define CDROM_OPTS "media=cdrom"
1121 #define PFLASH_OPTS ""
1125 static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
1127 BlockInterfaceType *block_default_type = opaque;
1129 return drive_new(opts, *block_default_type) == NULL;
1132 static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
1134 if (qemu_opt_get(opts, "snapshot") == NULL) {
1135 qemu_opt_set(opts, "snapshot", "on", &error_abort);
1140 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1141 int index, const char *optstr)
1146 if (!enable || drive_get_by_index(type, index)) {
1150 opts = drive_add(type, index, NULL, optstr);
1152 drive_enable_snapshot(NULL, opts, NULL);
1155 dinfo = drive_new(opts, type);
1159 dinfo->is_default = true;
1163 static QemuOptsList qemu_smp_opts = {
1165 .implied_opt_name = "cpus",
1166 .merge_lists = true,
1167 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
1171 .type = QEMU_OPT_NUMBER,
1174 .type = QEMU_OPT_NUMBER,
1177 .type = QEMU_OPT_NUMBER,
1180 .type = QEMU_OPT_NUMBER,
1183 .type = QEMU_OPT_NUMBER,
1185 { /*End of list */ }
1189 static void smp_parse(QemuOpts *opts)
1192 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
1193 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
1194 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
1195 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
1197 /* compute missing values, prefer sockets over cores over threads */
1198 if (cpus == 0 || sockets == 0) {
1199 sockets = sockets > 0 ? sockets : 1;
1200 cores = cores > 0 ? cores : 1;
1201 threads = threads > 0 ? threads : 1;
1203 cpus = cores * threads * sockets;
1205 } else if (cores == 0) {
1206 threads = threads > 0 ? threads : 1;
1207 cores = cpus / (sockets * threads);
1208 cores = cores > 0 ? cores : 1;
1209 } else if (threads == 0) {
1210 threads = cpus / (cores * sockets);
1211 threads = threads > 0 ? threads : 1;
1212 } else if (sockets * cores * threads < cpus) {
1213 error_report("cpu topology: "
1214 "sockets (%u) * cores (%u) * threads (%u) < "
1216 sockets, cores, threads, cpus);
1220 max_cpus = qemu_opt_get_number(opts, "maxcpus", cpus);
1222 if (max_cpus < cpus) {
1223 error_report("maxcpus must be equal to or greater than smp");
1227 if (sockets * cores * threads > max_cpus) {
1228 error_report("cpu topology: "
1229 "sockets (%u) * cores (%u) * threads (%u) > "
1231 sockets, cores, threads, max_cpus);
1237 smp_threads = threads;
1241 Error *blocker = NULL;
1242 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
1243 replay_add_blocker(blocker);
1247 static void realtime_init(void)
1250 if (os_mlock() < 0) {
1251 error_report("locking memory failed");
1258 static void configure_msg(QemuOpts *opts)
1260 enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
1263 /***********************************************************/
1266 typedef struct SemihostingConfig {
1268 SemihostingTarget target;
1271 const char *cmdline; /* concatenated argv */
1272 } SemihostingConfig;
1274 static SemihostingConfig semihosting;
1276 bool semihosting_enabled(void)
1278 return semihosting.enabled;
1281 SemihostingTarget semihosting_get_target(void)
1283 return semihosting.target;
1286 const char *semihosting_get_arg(int i)
1288 if (i >= semihosting.argc) {
1291 return semihosting.argv[i];
1294 int semihosting_get_argc(void)
1296 return semihosting.argc;
1299 const char *semihosting_get_cmdline(void)
1301 if (semihosting.cmdline == NULL && semihosting.argc > 0) {
1302 semihosting.cmdline = g_strjoinv(" ", (gchar **)semihosting.argv);
1304 return semihosting.cmdline;
1307 static int add_semihosting_arg(void *opaque,
1308 const char *name, const char *val,
1311 SemihostingConfig *s = opaque;
1312 if (strcmp(name, "arg") == 0) {
1314 /* one extra element as g_strjoinv() expects NULL-terminated array */
1315 s->argv = g_realloc(s->argv, (s->argc + 1) * sizeof(void *));
1316 s->argv[s->argc - 1] = val;
1317 s->argv[s->argc] = NULL;
1322 /* Use strings passed via -kernel/-append to initialize semihosting.argv[] */
1323 static inline void semihosting_arg_fallback(const char *file, const char *cmd)
1328 add_semihosting_arg(&semihosting, "arg", file, NULL);
1330 /* split -append and initialize argv[1..n] */
1331 cmd_token = strtok(g_strdup(cmd), " ");
1333 add_semihosting_arg(&semihosting, "arg", cmd_token, NULL);
1334 cmd_token = strtok(NULL, " ");
1338 /* Now we still need this for compatibility with XEN. */
1339 bool has_igd_gfx_passthru;
1340 static void igd_gfx_passthru(void)
1342 has_igd_gfx_passthru = current_machine->igd_gfx_passthru;
1345 /***********************************************************/
1348 static int usb_device_add(const char *devname)
1350 USBDevice *dev = NULL;
1351 #ifndef CONFIG_LINUX
1355 if (!machine_usb(current_machine)) {
1359 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1360 dev = usbdevice_create(devname);
1364 /* the other ones */
1365 #ifndef CONFIG_LINUX
1366 /* only the linux version is qdev-ified, usb-bsd still needs this */
1367 if (strstart(devname, "host:", &p)) {
1368 dev = usb_host_device_open(usb_bus_find(-1), p);
1378 static int usb_device_del(const char *devname)
1383 if (strstart(devname, "host:", &p)) {
1387 if (!machine_usb(current_machine)) {
1391 p = strchr(devname, '.');
1394 bus_num = strtoul(devname, NULL, 0);
1395 addr = strtoul(p + 1, NULL, 0);
1397 return usb_device_delete_addr(bus_num, addr);
1400 static int usb_parse(const char *cmdline)
1403 r = usb_device_add(cmdline);
1405 error_report("could not add USB device '%s'", cmdline);
1410 void hmp_usb_add(Monitor *mon, const QDict *qdict)
1412 const char *devname = qdict_get_str(qdict, "devname");
1413 if (usb_device_add(devname) < 0) {
1414 error_report("could not add USB device '%s'", devname);
1418 void hmp_usb_del(Monitor *mon, const QDict *qdict)
1420 const char *devname = qdict_get_str(qdict, "devname");
1421 if (usb_device_del(devname) < 0) {
1422 error_report("could not delete USB device '%s'", devname);
1426 /***********************************************************/
1427 /* machine registration */
1429 MachineState *current_machine;
1431 static MachineClass *find_machine(const char *name)
1433 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1434 MachineClass *mc = NULL;
1436 for (el = machines; el; el = el->next) {
1437 MachineClass *temp = el->data;
1439 if (!strcmp(temp->name, name)) {
1444 !strcmp(temp->alias, name)) {
1450 g_slist_free(machines);
1454 MachineClass *find_default_machine(void)
1456 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1457 MachineClass *mc = NULL;
1459 for (el = machines; el; el = el->next) {
1460 MachineClass *temp = el->data;
1462 if (temp->is_default) {
1468 g_slist_free(machines);
1472 MachineInfoList *qmp_query_machines(Error **errp)
1474 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1475 MachineInfoList *mach_list = NULL;
1477 for (el = machines; el; el = el->next) {
1478 MachineClass *mc = el->data;
1479 MachineInfoList *entry;
1482 info = g_malloc0(sizeof(*info));
1483 if (mc->is_default) {
1484 info->has_is_default = true;
1485 info->is_default = true;
1489 info->has_alias = true;
1490 info->alias = g_strdup(mc->alias);
1493 info->name = g_strdup(mc->name);
1494 info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
1495 info->hotpluggable_cpus = !!mc->query_hotpluggable_cpus;
1497 entry = g_malloc0(sizeof(*entry));
1498 entry->value = info;
1499 entry->next = mach_list;
1503 g_slist_free(machines);
1507 static int machine_help_func(QemuOpts *opts, MachineState *machine)
1509 ObjectProperty *prop;
1510 ObjectPropertyIterator iter;
1512 if (!qemu_opt_has_help_opt(opts)) {
1516 object_property_iter_init(&iter, OBJECT(machine));
1517 while ((prop = object_property_iter_next(&iter))) {
1522 error_printf("%s.%s=%s", MACHINE_GET_CLASS(machine)->name,
1523 prop->name, prop->type);
1524 if (prop->description) {
1525 error_printf(" (%s)\n", prop->description);
1534 /***********************************************************/
1535 /* main execution loop */
1537 struct vm_change_state_entry {
1538 VMChangeStateHandler *cb;
1540 QLIST_ENTRY (vm_change_state_entry) entries;
1543 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1545 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1548 VMChangeStateEntry *e;
1550 e = g_malloc0(sizeof (*e));
1554 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1558 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1560 QLIST_REMOVE (e, entries);
1564 void vm_state_notify(int running, RunState state)
1566 VMChangeStateEntry *e, *next;
1568 trace_vm_state_notify(running, state);
1570 QLIST_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
1571 e->cb(e->opaque, running, state);
1575 static int reset_requested;
1576 static int shutdown_requested, shutdown_signal = -1;
1577 static pid_t shutdown_pid;
1578 static int powerdown_requested;
1579 static int debug_requested;
1580 static int suspend_requested;
1581 static WakeupReason wakeup_reason;
1582 static NotifierList powerdown_notifiers =
1583 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
1584 static NotifierList suspend_notifiers =
1585 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1586 static NotifierList wakeup_notifiers =
1587 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1588 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
1590 int qemu_shutdown_requested_get(void)
1592 return shutdown_requested;
1595 int qemu_reset_requested_get(void)
1597 return reset_requested;
1600 static int qemu_shutdown_requested(void)
1602 return atomic_xchg(&shutdown_requested, 0);
1605 static void qemu_kill_report(void)
1607 if (!qtest_driver() && shutdown_signal != -1) {
1608 if (shutdown_pid == 0) {
1609 /* This happens for eg ^C at the terminal, so it's worth
1610 * avoiding printing an odd message in that case.
1612 error_report("terminating on signal %d", shutdown_signal);
1614 char *shutdown_cmd = qemu_get_pid_name(shutdown_pid);
1616 error_report("terminating on signal %d from pid " FMT_pid " (%s)",
1617 shutdown_signal, shutdown_pid,
1618 shutdown_cmd ? shutdown_cmd : "<unknown process>");
1619 g_free(shutdown_cmd);
1621 shutdown_signal = -1;
1625 static int qemu_reset_requested(void)
1627 int r = reset_requested;
1628 if (r && replay_checkpoint(CHECKPOINT_RESET_REQUESTED)) {
1629 reset_requested = 0;
1635 static int qemu_suspend_requested(void)
1637 int r = suspend_requested;
1638 if (r && replay_checkpoint(CHECKPOINT_SUSPEND_REQUESTED)) {
1639 suspend_requested = 0;
1645 static WakeupReason qemu_wakeup_requested(void)
1647 return wakeup_reason;
1650 static int qemu_powerdown_requested(void)
1652 int r = powerdown_requested;
1653 powerdown_requested = 0;
1657 static int qemu_debug_requested(void)
1659 int r = debug_requested;
1660 debug_requested = 0;
1664 void qemu_system_reset(bool report)
1668 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1670 cpu_synchronize_all_states();
1672 if (mc && mc->reset) {
1675 qemu_devices_reset();
1678 qapi_event_send_reset(&error_abort);
1680 cpu_synchronize_all_post_reset();
1683 void qemu_system_guest_panicked(GuestPanicInformation *info)
1685 qemu_log_mask(LOG_GUEST_ERROR, "Guest crashed\n");
1688 current_cpu->crash_occurred = true;
1690 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE,
1691 !!info, info, &error_abort);
1692 vm_stop(RUN_STATE_GUEST_PANICKED);
1694 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_POWEROFF,
1695 !!info, info, &error_abort);
1696 qemu_system_shutdown_request();
1700 if (info->type == GUEST_PANIC_INFORMATION_KIND_HYPER_V) {
1701 qemu_log_mask(LOG_GUEST_ERROR, "HV crash parameters: (%#"PRIx64
1702 " %#"PRIx64" %#"PRIx64" %#"PRIx64" %#"PRIx64")\n",
1703 info->u.hyper_v.data->arg1,
1704 info->u.hyper_v.data->arg2,
1705 info->u.hyper_v.data->arg3,
1706 info->u.hyper_v.data->arg4,
1707 info->u.hyper_v.data->arg5);
1709 qapi_free_GuestPanicInformation(info);
1713 void qemu_system_reset_request(void)
1716 shutdown_requested = 1;
1718 reset_requested = 1;
1721 qemu_notify_event();
1724 static void qemu_system_suspend(void)
1727 notifier_list_notify(&suspend_notifiers, NULL);
1728 runstate_set(RUN_STATE_SUSPENDED);
1729 qapi_event_send_suspend(&error_abort);
1732 void qemu_system_suspend_request(void)
1734 if (runstate_check(RUN_STATE_SUSPENDED)) {
1737 suspend_requested = 1;
1739 qemu_notify_event();
1742 void qemu_register_suspend_notifier(Notifier *notifier)
1744 notifier_list_add(&suspend_notifiers, notifier);
1747 void qemu_system_wakeup_request(WakeupReason reason)
1749 trace_system_wakeup_request(reason);
1751 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1754 if (!(wakeup_reason_mask & (1 << reason))) {
1757 runstate_set(RUN_STATE_RUNNING);
1758 wakeup_reason = reason;
1759 qemu_notify_event();
1762 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1765 wakeup_reason_mask |= (1 << reason);
1767 wakeup_reason_mask &= ~(1 << reason);
1771 void qemu_register_wakeup_notifier(Notifier *notifier)
1773 notifier_list_add(&wakeup_notifiers, notifier);
1776 void qemu_system_killed(int signal, pid_t pid)
1778 shutdown_signal = signal;
1782 /* Cannot call qemu_system_shutdown_request directly because
1783 * we are in a signal handler.
1785 shutdown_requested = 1;
1786 qemu_notify_event();
1789 void qemu_system_shutdown_request(void)
1791 trace_qemu_system_shutdown_request();
1792 replay_shutdown_request();
1793 shutdown_requested = 1;
1794 qemu_notify_event();
1797 static void qemu_system_powerdown(void)
1799 qapi_event_send_powerdown(&error_abort);
1800 notifier_list_notify(&powerdown_notifiers, NULL);
1803 void qemu_system_powerdown_request(void)
1805 trace_qemu_system_powerdown_request();
1806 powerdown_requested = 1;
1807 qemu_notify_event();
1810 void qemu_register_powerdown_notifier(Notifier *notifier)
1812 notifier_list_add(&powerdown_notifiers, notifier);
1815 void qemu_system_debug_request(void)
1817 debug_requested = 1;
1818 qemu_notify_event();
1821 static bool main_loop_should_exit(void)
1824 if (qemu_debug_requested()) {
1825 vm_stop(RUN_STATE_DEBUG);
1827 if (qemu_suspend_requested()) {
1828 qemu_system_suspend();
1830 if (qemu_shutdown_requested()) {
1832 qapi_event_send_shutdown(&error_abort);
1834 vm_stop(RUN_STATE_SHUTDOWN);
1839 if (qemu_reset_requested()) {
1841 qemu_system_reset(VMRESET_REPORT);
1843 if (!runstate_check(RUN_STATE_RUNNING) &&
1844 !runstate_check(RUN_STATE_INMIGRATE)) {
1845 runstate_set(RUN_STATE_PRELAUNCH);
1848 if (qemu_wakeup_requested()) {
1850 qemu_system_reset(VMRESET_SILENT);
1851 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
1852 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
1854 qapi_event_send_wakeup(&error_abort);
1856 if (qemu_powerdown_requested()) {
1857 qemu_system_powerdown();
1859 if (qemu_vmstop_requested(&r)) {
1865 static void main_loop(void)
1869 #ifdef CONFIG_PROFILER
1873 nonblocking = tcg_enabled() && last_io > 0;
1874 #ifdef CONFIG_PROFILER
1875 ti = profile_getclock();
1877 last_io = main_loop_wait(nonblocking);
1878 #ifdef CONFIG_PROFILER
1879 dev_time += profile_getclock() - ti;
1881 } while (!main_loop_should_exit());
1884 static void version(void)
1886 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION "\n"
1887 QEMU_COPYRIGHT "\n");
1890 static void help(int exitcode)
1893 printf("usage: %s [options] [disk_image]\n\n"
1894 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1895 error_get_progname());
1897 #define QEMU_OPTIONS_GENERATE_HELP
1898 #include "qemu-options-wrapper.h"
1900 printf("\nDuring emulation, the following keys are useful:\n"
1901 "ctrl-alt-f toggle full screen\n"
1902 "ctrl-alt-n switch to virtual console 'n'\n"
1903 "ctrl-alt toggle mouse and keyboard grab\n"
1905 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1910 #define HAS_ARG 0x0001
1912 typedef struct QEMUOption {
1919 static const QEMUOption qemu_options[] = {
1920 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1921 #define QEMU_OPTIONS_GENERATE_OPTIONS
1922 #include "qemu-options-wrapper.h"
1926 typedef struct VGAInterfaceInfo {
1927 const char *opt_name; /* option name */
1928 const char *name; /* human-readable name */
1929 /* Class names indicating that support is available.
1930 * If no class is specified, the interface is always available */
1931 const char *class_names[2];
1934 static VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
1940 .name = "standard VGA",
1941 .class_names = { "VGA", "isa-vga" },
1944 .opt_name = "cirrus",
1945 .name = "Cirrus VGA",
1946 .class_names = { "cirrus-vga", "isa-cirrus-vga" },
1949 .opt_name = "vmware",
1950 .name = "VMWare SVGA",
1951 .class_names = { "vmware-svga" },
1954 .opt_name = "virtio",
1955 .name = "Virtio VGA",
1956 .class_names = { "virtio-vga" },
1961 .class_names = { "qxl-vga" },
1965 .name = "TCX framebuffer",
1966 .class_names = { "SUNW,tcx" },
1970 .name = "CG3 framebuffer",
1971 .class_names = { "cgthree" },
1974 .opt_name = "xenfb",
1978 static bool vga_interface_available(VGAInterfaceType t)
1980 VGAInterfaceInfo *ti = &vga_interfaces[t];
1982 assert(t < VGA_TYPE_MAX);
1983 return !ti->class_names[0] ||
1984 object_class_by_name(ti->class_names[0]) ||
1985 object_class_by_name(ti->class_names[1]);
1988 static void select_vgahw(const char *p)
1993 assert(vga_interface_type == VGA_NONE);
1994 for (t = 0; t < VGA_TYPE_MAX; t++) {
1995 VGAInterfaceInfo *ti = &vga_interfaces[t];
1996 if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
1997 if (!vga_interface_available(t)) {
1998 error_report("%s not available", ti->name);
2001 vga_interface_type = t;
2005 if (t == VGA_TYPE_MAX) {
2007 error_report("unknown vga type: %s", p);
2011 const char *nextopt;
2013 if (strstart(opts, ",retrace=", &nextopt)) {
2015 if (strstart(opts, "dumb", &nextopt))
2016 vga_retrace_method = VGA_RETRACE_DUMB;
2017 else if (strstart(opts, "precise", &nextopt))
2018 vga_retrace_method = VGA_RETRACE_PRECISE;
2019 else goto invalid_vga;
2020 } else goto invalid_vga;
2025 typedef enum DisplayType {
2034 static DisplayType select_display(const char *p)
2037 DisplayType display = DT_DEFAULT;
2039 if (strstart(p, "sdl", &opts)) {
2043 const char *nextopt;
2045 if (strstart(opts, ",frame=", &nextopt)) {
2047 if (strstart(opts, "on", &nextopt)) {
2049 } else if (strstart(opts, "off", &nextopt)) {
2052 goto invalid_sdl_args;
2054 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
2056 if (strstart(opts, "on", &nextopt)) {
2058 } else if (strstart(opts, "off", &nextopt)) {
2061 goto invalid_sdl_args;
2063 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2065 if (strstart(opts, "on", &nextopt)) {
2067 } else if (strstart(opts, "off", &nextopt)) {
2070 goto invalid_sdl_args;
2072 } else if (strstart(opts, ",window_close=", &nextopt)) {
2074 if (strstart(opts, "on", &nextopt)) {
2076 } else if (strstart(opts, "off", &nextopt)) {
2079 goto invalid_sdl_args;
2081 } else if (strstart(opts, ",gl=", &nextopt)) {
2083 if (strstart(opts, "on", &nextopt)) {
2085 } else if (strstart(opts, "off", &nextopt)) {
2088 goto invalid_sdl_args;
2092 error_report("invalid SDL option string");
2098 error_report("SDL support is disabled");
2101 } else if (strstart(p, "vnc", &opts)) {
2103 vnc_parse(opts + 1, &error_fatal);
2105 error_report("VNC requires a display argument vnc=<display>");
2108 } else if (strstart(p, "curses", &opts)) {
2109 #ifdef CONFIG_CURSES
2110 display = DT_CURSES;
2112 error_report("curses support is disabled");
2115 } else if (strstart(p, "gtk", &opts)) {
2119 const char *nextopt;
2121 if (strstart(opts, ",grab_on_hover=", &nextopt)) {
2123 if (strstart(opts, "on", &nextopt)) {
2124 grab_on_hover = true;
2125 } else if (strstart(opts, "off", &nextopt)) {
2126 grab_on_hover = false;
2128 goto invalid_gtk_args;
2130 } else if (strstart(opts, ",gl=", &nextopt)) {
2132 if (strstart(opts, "on", &nextopt)) {
2134 } else if (strstart(opts, "off", &nextopt)) {
2137 goto invalid_gtk_args;
2141 error_report("invalid GTK option string");
2147 error_report("GTK support is disabled");
2150 } else if (strstart(p, "none", &opts)) {
2153 error_report("unknown display type");
2160 static int balloon_parse(const char *arg)
2164 if (strcmp(arg, "none") == 0) {
2168 if (!strncmp(arg, "virtio", 6)) {
2169 if (arg[6] == ',') {
2170 /* have params -> parse them */
2171 opts = qemu_opts_parse_noisily(qemu_find_opts("device"), arg + 7,
2176 /* create empty opts */
2177 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2180 qemu_opt_set(opts, "driver", "virtio-balloon", &error_abort);
2187 char *qemu_find_file(int type, const char *name)
2193 /* Try the name as a straight path first */
2194 if (access(name, R_OK) == 0) {
2195 trace_load_file(name, name);
2196 return g_strdup(name);
2200 case QEMU_FILE_TYPE_BIOS:
2203 case QEMU_FILE_TYPE_KEYMAP:
2204 subdir = "keymaps/";
2210 for (i = 0; i < data_dir_idx; i++) {
2211 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2212 if (access(buf, R_OK) == 0) {
2213 trace_load_file(name, buf);
2221 static inline bool nonempty_str(const char *str)
2226 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
2230 const char *name, *file, *str;
2231 FWCfgState *fw_cfg = (FWCfgState *) opaque;
2233 if (fw_cfg == NULL) {
2234 error_report("fw_cfg device not available");
2237 name = qemu_opt_get(opts, "name");
2238 file = qemu_opt_get(opts, "file");
2239 str = qemu_opt_get(opts, "string");
2241 /* we need name and either a file or the content string */
2242 if (!(nonempty_str(name) && (nonempty_str(file) || nonempty_str(str)))) {
2243 error_report("invalid argument(s)");
2246 if (nonempty_str(file) && nonempty_str(str)) {
2247 error_report("file and string are mutually exclusive");
2250 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
2251 error_report("name too long (max. %d char)", FW_CFG_MAX_FILE_PATH - 1);
2254 if (strncmp(name, "opt/", 4) != 0) {
2255 error_report("warning: externally provided fw_cfg item names "
2256 "should be prefixed with \"opt/\"");
2258 if (nonempty_str(str)) {
2259 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
2260 buf = g_memdup(str, size);
2262 if (!g_file_get_contents(file, &buf, &size, NULL)) {
2263 error_report("can't load %s", file);
2267 /* For legacy, keep user files in a specific global order. */
2268 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
2269 fw_cfg_add_file(fw_cfg, name, buf, size);
2270 fw_cfg_reset_order_override(fw_cfg);
2274 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
2276 return qdev_device_help(opts);
2279 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
2284 dev = qdev_device_add(opts, &err);
2286 error_report_err(err);
2289 object_unref(OBJECT(dev));
2293 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2295 Error *local_err = NULL;
2297 qemu_chr_new_from_opts(opts, &local_err);
2299 error_report_err(local_err);
2305 #ifdef CONFIG_VIRTFS
2306 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2308 return qemu_fsdev_add(opts);
2312 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
2315 const char *chardev;
2319 mode = qemu_opt_get(opts, "mode");
2323 if (strcmp(mode, "readline") == 0) {
2324 flags = MONITOR_USE_READLINE;
2325 } else if (strcmp(mode, "control") == 0) {
2326 flags = MONITOR_USE_CONTROL;
2328 error_report("unknown monitor mode \"%s\"", mode);
2332 if (qemu_opt_get_bool(opts, "pretty", 0))
2333 flags |= MONITOR_USE_PRETTY;
2335 if (qemu_opt_get_bool(opts, "default", 0)) {
2336 error_report("option 'default' does nothing and is deprecated");
2339 chardev = qemu_opt_get(opts, "chardev");
2340 chr = qemu_chr_find(chardev);
2342 error_report("chardev \"%s\" not found", chardev);
2346 monitor_init(chr, flags);
2350 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
2352 static int monitor_device_index = 0;
2357 if (strstart(optarg, "chardev:", &p)) {
2358 snprintf(label, sizeof(label), "%s", p);
2360 snprintf(label, sizeof(label), "compat_monitor%d",
2361 monitor_device_index);
2362 opts = qemu_chr_parse_compat(label, optarg);
2364 error_report("parse error: %s", optarg);
2369 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
2370 qemu_opt_set(opts, "mode", mode, &error_abort);
2371 qemu_opt_set(opts, "chardev", label, &error_abort);
2372 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
2373 monitor_device_index++;
2376 struct device_config {
2378 DEV_USB, /* -usbdevice */
2380 DEV_SERIAL, /* -serial */
2381 DEV_PARALLEL, /* -parallel */
2382 DEV_VIRTCON, /* -virtioconsole */
2383 DEV_DEBUGCON, /* -debugcon */
2384 DEV_GDB, /* -gdb, -s */
2385 DEV_SCLP, /* s390 sclp */
2387 const char *cmdline;
2389 QTAILQ_ENTRY(device_config) next;
2392 static QTAILQ_HEAD(, device_config) device_configs =
2393 QTAILQ_HEAD_INITIALIZER(device_configs);
2395 static void add_device_config(int type, const char *cmdline)
2397 struct device_config *conf;
2399 conf = g_malloc0(sizeof(*conf));
2401 conf->cmdline = cmdline;
2402 loc_save(&conf->loc);
2403 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2406 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2408 struct device_config *conf;
2411 QTAILQ_FOREACH(conf, &device_configs, next) {
2412 if (conf->type != type)
2414 loc_push_restore(&conf->loc);
2415 rc = func(conf->cmdline);
2416 loc_pop(&conf->loc);
2424 static int serial_parse(const char *devname)
2426 static int index = 0;
2429 if (strcmp(devname, "none") == 0)
2431 if (index == MAX_SERIAL_PORTS) {
2432 error_report("too many serial ports");
2435 snprintf(label, sizeof(label), "serial%d", index);
2436 serial_hds[index] = qemu_chr_new(label, devname);
2437 if (!serial_hds[index]) {
2438 error_report("could not connect serial device"
2439 " to character backend '%s'", devname);
2446 static int parallel_parse(const char *devname)
2448 static int index = 0;
2451 if (strcmp(devname, "none") == 0)
2453 if (index == MAX_PARALLEL_PORTS) {
2454 error_report("too many parallel ports");
2457 snprintf(label, sizeof(label), "parallel%d", index);
2458 parallel_hds[index] = qemu_chr_new(label, devname);
2459 if (!parallel_hds[index]) {
2460 error_report("could not connect parallel device"
2461 " to character backend '%s'", devname);
2468 static int virtcon_parse(const char *devname)
2470 QemuOptsList *device = qemu_find_opts("device");
2471 static int index = 0;
2473 QemuOpts *bus_opts, *dev_opts;
2475 if (strcmp(devname, "none") == 0)
2477 if (index == MAX_VIRTIO_CONSOLES) {
2478 error_report("too many virtio consoles");
2482 bus_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2483 qemu_opt_set(bus_opts, "driver", "virtio-serial", &error_abort);
2485 dev_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2486 qemu_opt_set(dev_opts, "driver", "virtconsole", &error_abort);
2488 snprintf(label, sizeof(label), "virtcon%d", index);
2489 virtcon_hds[index] = qemu_chr_new(label, devname);
2490 if (!virtcon_hds[index]) {
2491 error_report("could not connect virtio console"
2492 " to character backend '%s'", devname);
2495 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2501 static int sclp_parse(const char *devname)
2503 QemuOptsList *device = qemu_find_opts("device");
2504 static int index = 0;
2508 if (strcmp(devname, "none") == 0) {
2511 if (index == MAX_SCLP_CONSOLES) {
2512 error_report("too many sclp consoles");
2516 assert(arch_type == QEMU_ARCH_S390X);
2518 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2519 qemu_opt_set(dev_opts, "driver", "sclpconsole", &error_abort);
2521 snprintf(label, sizeof(label), "sclpcon%d", index);
2522 sclp_hds[index] = qemu_chr_new(label, devname);
2523 if (!sclp_hds[index]) {
2524 error_report("could not connect sclp console"
2525 " to character backend '%s'", devname);
2528 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2534 static int debugcon_parse(const char *devname)
2538 if (!qemu_chr_new("debugcon", devname)) {
2541 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2543 error_report("already have a debugcon device");
2546 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
2547 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
2551 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
2553 const MachineClass *mc1 = a, *mc2 = b;
2556 if (mc1->family == NULL) {
2557 if (mc2->family == NULL) {
2558 /* Compare standalone machine types against each other; they sort
2559 * in increasing order.
2561 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
2562 object_class_get_name(OBJECT_CLASS(mc2)));
2565 /* Standalone machine types sort after families. */
2569 if (mc2->family == NULL) {
2570 /* Families sort before standalone machine types. */
2574 /* Families sort between each other alphabetically increasingly. */
2575 res = strcmp(mc1->family, mc2->family);
2580 /* Within the same family, machine types sort in decreasing order. */
2581 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
2582 object_class_get_name(OBJECT_CLASS(mc1)));
2585 static MachineClass *machine_parse(const char *name)
2587 MachineClass *mc = NULL;
2588 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
2591 mc = find_machine(name);
2594 g_slist_free(machines);
2597 if (name && !is_help_option(name)) {
2598 error_report("unsupported machine type");
2599 error_printf("Use -machine help to list supported machines\n");
2601 printf("Supported machines are:\n");
2602 machines = g_slist_sort(machines, machine_class_cmp);
2603 for (el = machines; el; el = el->next) {
2604 MachineClass *mc = el->data;
2606 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
2608 printf("%-20s %s%s\n", mc->name, mc->desc,
2609 mc->is_default ? " (default)" : "");
2613 g_slist_free(machines);
2614 exit(!name || !is_help_option(name));
2617 void qemu_add_exit_notifier(Notifier *notify)
2619 notifier_list_add(&exit_notifiers, notify);
2622 void qemu_remove_exit_notifier(Notifier *notify)
2624 notifier_remove(notify);
2627 static void qemu_run_exit_notifiers(void)
2629 notifier_list_notify(&exit_notifiers, NULL);
2632 static bool machine_init_done;
2634 void qemu_add_machine_init_done_notifier(Notifier *notify)
2636 notifier_list_add(&machine_init_done_notifiers, notify);
2637 if (machine_init_done) {
2638 notify->notify(notify, NULL);
2642 void qemu_remove_machine_init_done_notifier(Notifier *notify)
2644 notifier_remove(notify);
2647 static void qemu_run_machine_init_done_notifiers(void)
2649 notifier_list_notify(&machine_init_done_notifiers, NULL);
2650 machine_init_done = true;
2653 static const QEMUOption *lookup_opt(int argc, char **argv,
2654 const char **poptarg, int *poptind)
2656 const QEMUOption *popt;
2657 int optind = *poptind;
2658 char *r = argv[optind];
2661 loc_set_cmdline(argv, optind, 1);
2663 /* Treat --foo the same as -foo. */
2666 popt = qemu_options;
2669 error_report("invalid option");
2672 if (!strcmp(popt->name, r + 1))
2676 if (popt->flags & HAS_ARG) {
2677 if (optind >= argc) {
2678 error_report("requires an argument");
2681 optarg = argv[optind++];
2682 loc_set_cmdline(argv, optind - 2, 2);
2693 static MachineClass *select_machine(void)
2695 MachineClass *machine_class = find_default_machine();
2700 loc_push_none(&loc);
2702 opts = qemu_get_machine_opts();
2703 qemu_opts_loc_restore(opts);
2705 optarg = qemu_opt_get(opts, "type");
2707 machine_class = machine_parse(optarg);
2710 if (!machine_class) {
2711 error_report("No machine specified, and there is no default");
2712 error_printf("Use -machine help to list supported machines\n");
2717 return machine_class;
2720 static int machine_set_property(void *opaque,
2721 const char *name, const char *value,
2724 Object *obj = OBJECT(opaque);
2725 Error *local_err = NULL;
2728 if (strcmp(name, "type") == 0) {
2732 qom_name = g_strdup(name);
2733 for (p = qom_name; *p; p++) {
2739 object_property_parse(obj, value, qom_name, &local_err);
2743 error_report_err(local_err);
2752 * Initial object creation happens before all other
2753 * QEMU data types are created. The majority of objects
2754 * can be created at this point. The rng-egd object
2755 * cannot be created here, as it depends on the chardev
2758 static bool object_create_initial(const char *type)
2760 if (g_str_equal(type, "rng-egd")) {
2765 * return false for concrete netfilters since
2766 * they depend on netdevs already existing
2768 if (g_str_equal(type, "filter-buffer") ||
2769 g_str_equal(type, "filter-dump") ||
2770 g_str_equal(type, "filter-mirror") ||
2771 g_str_equal(type, "filter-redirector") ||
2772 g_str_equal(type, "colo-compare") ||
2773 g_str_equal(type, "filter-rewriter") ||
2774 g_str_equal(type, "filter-replay")) {
2778 /* Memory allocation by backends needs to be done
2779 * after configure_accelerator() (due to the tcg_enabled()
2780 * checks at memory_region_init_*()).
2782 * Also, allocation of large amounts of memory may delay
2783 * chardev initialization for too long, and trigger timeouts
2784 * on software that waits for a monitor socket to be created
2787 if (g_str_has_prefix(type, "memory-backend-")) {
2796 * The remainder of object creation happens after the
2797 * creation of chardev, fsdev, net clients and device data types.
2799 static bool object_create_delayed(const char *type)
2801 return !object_create_initial(type);
2805 static void set_memory_options(uint64_t *ram_slots, ram_addr_t *maxram_size,
2809 const char *mem_str;
2810 const char *maxmem_str, *slots_str;
2811 const ram_addr_t default_ram_size = mc->default_ram_size;
2812 QemuOpts *opts = qemu_find_opts_singleton("memory");
2815 loc_push_none(&loc);
2816 qemu_opts_loc_restore(opts);
2819 mem_str = qemu_opt_get(opts, "size");
2822 error_report("missing 'size' option value");
2826 sz = qemu_opt_get_size(opts, "size", ram_size);
2828 /* Fix up legacy suffix-less format */
2829 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2830 uint64_t overflow_check = sz;
2833 if ((sz >> 20) != overflow_check) {
2834 error_report("too large 'size' option value");
2840 /* backward compatibility behaviour for case "-m 0" */
2842 sz = default_ram_size;
2845 sz = QEMU_ALIGN_UP(sz, 8192);
2847 if (ram_size != sz) {
2848 error_report("ram size too large");
2852 /* store value for the future use */
2853 qemu_opt_set_number(opts, "size", ram_size, &error_abort);
2854 *maxram_size = ram_size;
2856 maxmem_str = qemu_opt_get(opts, "maxmem");
2857 slots_str = qemu_opt_get(opts, "slots");
2858 if (maxmem_str && slots_str) {
2861 sz = qemu_opt_get_size(opts, "maxmem", 0);
2862 slots = qemu_opt_get_number(opts, "slots", 0);
2863 if (sz < ram_size) {
2864 error_report("invalid value of -m option maxmem: "
2865 "maximum memory size (0x%" PRIx64 ") must be at least "
2866 "the initial memory size (0x" RAM_ADDR_FMT ")",
2869 } else if (sz > ram_size) {
2871 error_report("invalid value of -m option: maxmem was "
2872 "specified, but no hotplug slots were specified");
2876 error_report("invalid value of -m option maxmem: "
2877 "memory slots were specified but maximum memory size "
2878 "(0x%" PRIx64 ") is equal to the initial memory size "
2879 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
2885 } else if ((!maxmem_str && slots_str) ||
2886 (maxmem_str && !slots_str)) {
2887 error_report("invalid -m option value: missing "
2888 "'%s' option", slots_str ? "maxmem" : "slots");
2895 static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
2899 g = g_malloc0(sizeof(*g));
2900 g->driver = qemu_opt_get(opts, "driver");
2901 g->property = qemu_opt_get(opts, "property");
2902 g->value = qemu_opt_get(opts, "value");
2903 g->user_provided = true;
2904 g->errp = &error_fatal;
2905 qdev_prop_register_global(g);
2909 static int qemu_read_default_config_file(void)
2913 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
2914 if (ret < 0 && ret != -ENOENT) {
2921 int main(int argc, char **argv, char **envp)
2924 int snapshot, linux_boot;
2925 const char *initrd_filename;
2926 const char *kernel_filename, *kernel_cmdline;
2927 const char *boot_order = NULL;
2928 const char *boot_once = NULL;
2930 int cyls, heads, secs, translation;
2931 QemuOpts *hda_opts = NULL, *opts, *machine_opts, *icount_opts = NULL;
2932 QemuOptsList *olist;
2935 const char *loadvm = NULL;
2936 MachineClass *machine_class;
2937 const char *cpu_model;
2938 const char *vga_model = NULL;
2939 const char *qtest_chrdev = NULL;
2940 const char *qtest_log = NULL;
2941 const char *pid_file = NULL;
2942 const char *incoming = NULL;
2943 bool defconfig = true;
2944 bool userconfig = true;
2945 bool nographic = false;
2946 DisplayType display_type = DT_DEFAULT;
2947 int display_remote = 0;
2948 const char *log_mask = NULL;
2949 const char *log_file = NULL;
2950 char *trace_file = NULL;
2951 ram_addr_t maxram_size;
2952 uint64_t ram_slots = 0;
2953 FILE *vmstate_dump_file = NULL;
2954 Error *main_loop_err = NULL;
2956 bool list_data_dirs = false;
2958 module_call_init(MODULE_INIT_TRACE);
2960 qemu_init_cpu_list();
2961 qemu_init_cpu_loop();
2962 qemu_mutex_lock_iothread();
2964 atexit(qemu_run_exit_notifiers);
2965 error_set_progname(argv[0]);
2966 qemu_init_exec_dir(argv[0]);
2968 module_call_init(MODULE_INIT_QOM);
2969 module_call_init(MODULE_INIT_QAPI);
2971 qemu_add_opts(&qemu_drive_opts);
2972 qemu_add_drive_opts(&qemu_legacy_drive_opts);
2973 qemu_add_drive_opts(&qemu_common_drive_opts);
2974 qemu_add_drive_opts(&qemu_drive_opts);
2975 qemu_add_drive_opts(&bdrv_runtime_opts);
2976 qemu_add_opts(&qemu_chardev_opts);
2977 qemu_add_opts(&qemu_device_opts);
2978 qemu_add_opts(&qemu_netdev_opts);
2979 qemu_add_opts(&qemu_net_opts);
2980 qemu_add_opts(&qemu_rtc_opts);
2981 qemu_add_opts(&qemu_global_opts);
2982 qemu_add_opts(&qemu_mon_opts);
2983 qemu_add_opts(&qemu_trace_opts);
2984 qemu_add_opts(&qemu_option_rom_opts);
2985 qemu_add_opts(&qemu_machine_opts);
2986 qemu_add_opts(&qemu_mem_opts);
2987 qemu_add_opts(&qemu_smp_opts);
2988 qemu_add_opts(&qemu_boot_opts);
2989 qemu_add_opts(&qemu_sandbox_opts);
2990 qemu_add_opts(&qemu_add_fd_opts);
2991 qemu_add_opts(&qemu_object_opts);
2992 qemu_add_opts(&qemu_tpmdev_opts);
2993 qemu_add_opts(&qemu_realtime_opts);
2994 qemu_add_opts(&qemu_msg_opts);
2995 qemu_add_opts(&qemu_name_opts);
2996 qemu_add_opts(&qemu_numa_opts);
2997 qemu_add_opts(&qemu_icount_opts);
2998 qemu_add_opts(&qemu_semihosting_config_opts);
2999 qemu_add_opts(&qemu_fw_cfg_opts);
3000 module_call_init(MODULE_INIT_OPTS);
3004 if (qcrypto_init(&err) < 0) {
3005 error_reportf_err(err, "cannot initialize crypto: ");
3008 rtc_clock = QEMU_CLOCK_HOST;
3010 QLIST_INIT (&vm_change_state_head);
3011 os_setup_early_signal_handling();
3015 cyls = heads = secs = 0;
3016 translation = BIOS_ATA_TRANSLATION_AUTO;
3020 bdrv_init_with_whitelist();
3024 /* first pass of option parsing */
3026 while (optind < argc) {
3027 if (argv[optind][0] != '-') {
3031 const QEMUOption *popt;
3033 popt = lookup_opt(argc, argv, &optarg, &optind);
3034 switch (popt->index) {
3035 case QEMU_OPTION_nodefconfig:
3038 case QEMU_OPTION_nouserconfig:
3045 if (defconfig && userconfig) {
3046 if (qemu_read_default_config_file() < 0) {
3051 /* second pass of option parsing */
3056 if (argv[optind][0] != '-') {
3057 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
3059 const QEMUOption *popt;
3061 popt = lookup_opt(argc, argv, &optarg, &optind);
3062 if (!(popt->arch_mask & arch_type)) {
3063 error_report("Option not supported for this target");
3066 switch(popt->index) {
3067 case QEMU_OPTION_no_kvm_irqchip: {
3068 olist = qemu_find_opts("machine");
3069 qemu_opts_parse_noisily(olist, "kernel_irqchip=off", false);
3072 case QEMU_OPTION_cpu:
3073 /* hw initialization will check this */
3076 case QEMU_OPTION_hda:
3080 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
3082 snprintf(buf, sizeof(buf),
3083 "%s,cyls=%d,heads=%d,secs=%d%s",
3084 HD_OPTS , cyls, heads, secs,
3085 translation == BIOS_ATA_TRANSLATION_LBA ?
3087 translation == BIOS_ATA_TRANSLATION_NONE ?
3088 ",trans=none" : "");
3089 drive_add(IF_DEFAULT, 0, optarg, buf);
3092 case QEMU_OPTION_hdb:
3093 case QEMU_OPTION_hdc:
3094 case QEMU_OPTION_hdd:
3095 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
3098 case QEMU_OPTION_drive:
3099 if (drive_def(optarg) == NULL) {
3103 case QEMU_OPTION_set:
3104 if (qemu_set_option(optarg) != 0)
3107 case QEMU_OPTION_global:
3108 if (qemu_global_option(optarg) != 0)
3111 case QEMU_OPTION_mtdblock:
3112 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3114 case QEMU_OPTION_sd:
3115 drive_add(IF_SD, -1, optarg, SD_OPTS);
3117 case QEMU_OPTION_pflash:
3118 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
3120 case QEMU_OPTION_snapshot:
3123 case QEMU_OPTION_hdachs:
3127 cyls = strtol(p, (char **)&p, 0);
3128 if (cyls < 1 || cyls > 16383)
3133 heads = strtol(p, (char **)&p, 0);
3134 if (heads < 1 || heads > 16)
3139 secs = strtol(p, (char **)&p, 0);
3140 if (secs < 1 || secs > 63)
3144 if (!strcmp(p, "large")) {
3145 translation = BIOS_ATA_TRANSLATION_LARGE;
3146 } else if (!strcmp(p, "rechs")) {
3147 translation = BIOS_ATA_TRANSLATION_RECHS;
3148 } else if (!strcmp(p, "none")) {
3149 translation = BIOS_ATA_TRANSLATION_NONE;
3150 } else if (!strcmp(p, "lba")) {
3151 translation = BIOS_ATA_TRANSLATION_LBA;
3152 } else if (!strcmp(p, "auto")) {
3153 translation = BIOS_ATA_TRANSLATION_AUTO;
3157 } else if (*p != '\0') {
3159 error_report("invalid physical CHS format");
3162 if (hda_opts != NULL) {
3163 qemu_opt_set_number(hda_opts, "cyls", cyls,
3165 qemu_opt_set_number(hda_opts, "heads", heads,
3167 qemu_opt_set_number(hda_opts, "secs", secs,
3169 if (translation == BIOS_ATA_TRANSLATION_LARGE) {
3170 qemu_opt_set(hda_opts, "trans", "large",
3172 } else if (translation == BIOS_ATA_TRANSLATION_RECHS) {
3173 qemu_opt_set(hda_opts, "trans", "rechs",
3175 } else if (translation == BIOS_ATA_TRANSLATION_LBA) {
3176 qemu_opt_set(hda_opts, "trans", "lba",
3178 } else if (translation == BIOS_ATA_TRANSLATION_NONE) {
3179 qemu_opt_set(hda_opts, "trans", "none",
3185 case QEMU_OPTION_numa:
3186 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
3192 case QEMU_OPTION_display:
3193 display_type = select_display(optarg);
3195 case QEMU_OPTION_nographic:
3196 olist = qemu_find_opts("machine");
3197 qemu_opts_parse_noisily(olist, "graphics=off", false);
3199 display_type = DT_NONE;
3201 case QEMU_OPTION_curses:
3202 #ifdef CONFIG_CURSES
3203 display_type = DT_CURSES;
3205 error_report("curses support is disabled");
3209 case QEMU_OPTION_portrait:
3210 graphic_rotate = 90;
3212 case QEMU_OPTION_rotate:
3213 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3214 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3215 graphic_rotate != 180 && graphic_rotate != 270) {
3216 error_report("only 90, 180, 270 deg rotation is available");
3220 case QEMU_OPTION_kernel:
3221 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg,
3224 case QEMU_OPTION_initrd:
3225 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg,
3228 case QEMU_OPTION_append:
3229 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg,
3232 case QEMU_OPTION_dtb:
3233 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg,
3236 case QEMU_OPTION_cdrom:
3237 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3239 case QEMU_OPTION_boot:
3240 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
3246 case QEMU_OPTION_fda:
3247 case QEMU_OPTION_fdb:
3248 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3251 case QEMU_OPTION_no_fd_bootchk:
3254 case QEMU_OPTION_netdev:
3256 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3260 case QEMU_OPTION_net:
3262 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3266 #ifdef CONFIG_LIBISCSI
3267 case QEMU_OPTION_iscsi:
3268 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
3276 case QEMU_OPTION_tftp:
3277 error_report("The -tftp option is deprecated. "
3278 "Please use '-netdev user,tftp=...' instead.");
3279 legacy_tftp_prefix = optarg;
3281 case QEMU_OPTION_bootp:
3282 error_report("The -bootp option is deprecated. "
3283 "Please use '-netdev user,bootfile=...' instead.");
3284 legacy_bootp_filename = optarg;
3286 case QEMU_OPTION_redir:
3287 error_report("The -redir option is deprecated. "
3288 "Please use '-netdev user,hostfwd=...' instead.");
3289 if (net_slirp_redir(optarg) < 0)
3293 case QEMU_OPTION_bt:
3294 add_device_config(DEV_BT, optarg);
3296 case QEMU_OPTION_audio_help:
3300 case QEMU_OPTION_soundhw:
3301 select_soundhw (optarg);
3306 case QEMU_OPTION_version:
3311 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
3318 case QEMU_OPTION_tpmdev:
3319 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3324 case QEMU_OPTION_mempath:
3327 case QEMU_OPTION_mem_prealloc:
3336 case QEMU_OPTION_DFILTER:
3337 qemu_set_dfilter_ranges(optarg, &error_fatal);
3340 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3342 case QEMU_OPTION_gdb:
3343 add_device_config(DEV_GDB, optarg);
3346 if (is_help_option(optarg)) {
3347 list_data_dirs = true;
3348 } else if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3349 data_dir[data_dir_idx++] = optarg;
3352 case QEMU_OPTION_bios:
3353 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg,
3356 case QEMU_OPTION_singlestep:
3363 keyboard_layout = optarg;
3365 case QEMU_OPTION_localtime:
3368 case QEMU_OPTION_vga:
3377 w = strtol(p, (char **)&p, 10);
3380 error_report("invalid resolution or depth");
3386 h = strtol(p, (char **)&p, 10);
3391 depth = strtol(p, (char **)&p, 10);
3392 if (depth != 8 && depth != 15 && depth != 16 &&
3393 depth != 24 && depth != 32)
3395 } else if (*p == '\0') {
3396 depth = graphic_depth;
3403 graphic_depth = depth;
3406 case QEMU_OPTION_echr:
3409 term_escape_char = strtol(optarg, &r, 0);
3411 printf("Bad argument to echr\n");
3414 case QEMU_OPTION_monitor:
3415 default_monitor = 0;
3416 if (strncmp(optarg, "none", 4)) {
3417 monitor_parse(optarg, "readline", false);
3420 case QEMU_OPTION_qmp:
3421 monitor_parse(optarg, "control", false);
3422 default_monitor = 0;
3424 case QEMU_OPTION_qmp_pretty:
3425 monitor_parse(optarg, "control", true);
3426 default_monitor = 0;
3428 case QEMU_OPTION_mon:
3429 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3434 default_monitor = 0;
3436 case QEMU_OPTION_chardev:
3437 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3443 case QEMU_OPTION_fsdev:
3444 olist = qemu_find_opts("fsdev");
3446 error_report("fsdev support is disabled");
3449 opts = qemu_opts_parse_noisily(olist, optarg, true);
3454 case QEMU_OPTION_virtfs: {
3457 const char *writeout, *sock_fd, *socket;
3459 olist = qemu_find_opts("virtfs");
3461 error_report("virtfs support is disabled");
3464 opts = qemu_opts_parse_noisily(olist, optarg, true);
3469 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3470 qemu_opt_get(opts, "mount_tag") == NULL) {
3471 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3474 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3475 qemu_opt_get(opts, "mount_tag"),
3478 error_report("duplicate fsdev id: %s",
3479 qemu_opt_get(opts, "mount_tag"));
3483 writeout = qemu_opt_get(opts, "writeout");
3485 #ifdef CONFIG_SYNC_FILE_RANGE
3486 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3488 error_report("writeout=immediate not supported "
3489 "on this platform");
3493 qemu_opt_set(fsdev, "fsdriver",
3494 qemu_opt_get(opts, "fsdriver"), &error_abort);
3495 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"),
3497 qemu_opt_set(fsdev, "security_model",
3498 qemu_opt_get(opts, "security_model"),
3500 socket = qemu_opt_get(opts, "socket");
3502 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3504 sock_fd = qemu_opt_get(opts, "sock_fd");
3506 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3509 qemu_opt_set_bool(fsdev, "readonly",
3510 qemu_opt_get_bool(opts, "readonly", 0),
3512 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3514 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3515 qemu_opt_set(device, "fsdev",
3516 qemu_opt_get(opts, "mount_tag"), &error_abort);
3517 qemu_opt_set(device, "mount_tag",
3518 qemu_opt_get(opts, "mount_tag"), &error_abort);
3521 case QEMU_OPTION_virtfs_synth: {
3525 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3528 error_report("duplicate option: %s", "virtfs_synth");
3531 qemu_opt_set(fsdev, "fsdriver", "synth", &error_abort);
3533 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3535 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3536 qemu_opt_set(device, "fsdev", "v_synth", &error_abort);
3537 qemu_opt_set(device, "mount_tag", "v_synth", &error_abort);
3540 case QEMU_OPTION_serial:
3541 add_device_config(DEV_SERIAL, optarg);
3543 if (strncmp(optarg, "mon:", 4) == 0) {
3544 default_monitor = 0;
3547 case QEMU_OPTION_watchdog:
3549 error_report("only one watchdog option may be given");
3554 case QEMU_OPTION_watchdog_action:
3555 if (select_watchdog_action(optarg) == -1) {
3556 error_report("unknown -watchdog-action parameter");
3560 case QEMU_OPTION_virtiocon:
3561 add_device_config(DEV_VIRTCON, optarg);
3562 default_virtcon = 0;
3563 if (strncmp(optarg, "mon:", 4) == 0) {
3564 default_monitor = 0;
3567 case QEMU_OPTION_parallel:
3568 add_device_config(DEV_PARALLEL, optarg);
3569 default_parallel = 0;
3570 if (strncmp(optarg, "mon:", 4) == 0) {
3571 default_monitor = 0;
3574 case QEMU_OPTION_debugcon:
3575 add_device_config(DEV_DEBUGCON, optarg);
3577 case QEMU_OPTION_loadvm:
3580 case QEMU_OPTION_full_screen:
3583 case QEMU_OPTION_no_frame:
3586 case QEMU_OPTION_alt_grab:
3589 case QEMU_OPTION_ctrl_grab:
3592 case QEMU_OPTION_no_quit:
3595 case QEMU_OPTION_sdl:
3597 display_type = DT_SDL;
3600 error_report("SDL support is disabled");
3603 case QEMU_OPTION_pidfile:
3606 case QEMU_OPTION_win2k_hack:
3607 win2k_install_hack = 1;
3609 case QEMU_OPTION_rtc_td_hack: {
3610 static GlobalProperty slew_lost_ticks = {
3611 .driver = "mc146818rtc",
3612 .property = "lost_tick_policy",
3616 qdev_prop_register_global(&slew_lost_ticks);
3619 case QEMU_OPTION_acpitable:
3620 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3625 acpi_table_add(opts, &error_fatal);
3627 case QEMU_OPTION_smbios:
3628 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3633 smbios_entry_add(opts, &error_fatal);
3635 case QEMU_OPTION_fwcfg:
3636 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3642 case QEMU_OPTION_enable_kvm:
3643 olist = qemu_find_opts("machine");
3644 qemu_opts_parse_noisily(olist, "accel=kvm", false);
3646 case QEMU_OPTION_enable_hax:
3647 olist = qemu_find_opts("machine");
3648 qemu_opts_parse_noisily(olist, "accel=hax", false);
3651 case QEMU_OPTION_machine:
3652 olist = qemu_find_opts("machine");
3653 opts = qemu_opts_parse_noisily(olist, optarg, true);
3658 case QEMU_OPTION_no_kvm:
3659 olist = qemu_find_opts("machine");
3660 qemu_opts_parse_noisily(olist, "accel=tcg", false);
3662 case QEMU_OPTION_no_kvm_pit: {
3663 error_report("warning: ignoring deprecated option");
3666 case QEMU_OPTION_no_kvm_pit_reinjection: {
3667 static GlobalProperty kvm_pit_lost_tick_policy = {
3668 .driver = "kvm-pit",
3669 .property = "lost_tick_policy",
3673 error_report("warning: deprecated, replaced by "
3674 "-global kvm-pit.lost_tick_policy=discard");
3675 qdev_prop_register_global(&kvm_pit_lost_tick_policy);
3678 case QEMU_OPTION_usb:
3679 olist = qemu_find_opts("machine");
3680 qemu_opts_parse_noisily(olist, "usb=on", false);
3682 case QEMU_OPTION_usbdevice:
3683 olist = qemu_find_opts("machine");
3684 qemu_opts_parse_noisily(olist, "usb=on", false);
3685 add_device_config(DEV_USB, optarg);
3687 case QEMU_OPTION_device:
3688 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3693 case QEMU_OPTION_smp:
3694 if (!qemu_opts_parse_noisily(qemu_find_opts("smp-opts"),
3699 case QEMU_OPTION_vnc:
3700 vnc_parse(optarg, &error_fatal);
3702 case QEMU_OPTION_no_acpi:
3705 case QEMU_OPTION_no_hpet:
3708 case QEMU_OPTION_balloon:
3709 if (balloon_parse(optarg) < 0) {
3710 error_report("unknown -balloon argument %s", optarg);
3714 case QEMU_OPTION_no_reboot:
3717 case QEMU_OPTION_no_shutdown:
3720 case QEMU_OPTION_show_cursor:
3723 case QEMU_OPTION_uuid:
3724 if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
3725 error_report("failed to parse UUID string: wrong format");
3728 qemu_uuid_set = true;
3730 case QEMU_OPTION_option_rom:
3731 if (nb_option_roms >= MAX_OPTION_ROMS) {
3732 error_report("too many option ROMs");
3735 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3740 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3741 option_rom[nb_option_roms].bootindex =
3742 qemu_opt_get_number(opts, "bootindex", -1);
3743 if (!option_rom[nb_option_roms].name) {
3744 error_report("Option ROM file is not specified");
3749 case QEMU_OPTION_semihosting:
3750 semihosting.enabled = true;
3751 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3753 case QEMU_OPTION_semihosting_config:
3754 semihosting.enabled = true;
3755 opts = qemu_opts_parse_noisily(qemu_find_opts("semihosting-config"),
3758 semihosting.enabled = qemu_opt_get_bool(opts, "enable",
3760 const char *target = qemu_opt_get(opts, "target");
3761 if (target != NULL) {
3762 if (strcmp("native", target) == 0) {
3763 semihosting.target = SEMIHOSTING_TARGET_NATIVE;
3764 } else if (strcmp("gdb", target) == 0) {
3765 semihosting.target = SEMIHOSTING_TARGET_GDB;
3766 } else if (strcmp("auto", target) == 0) {
3767 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3769 error_report("unsupported semihosting-config %s",
3774 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3776 /* Set semihosting argument count and vector */
3777 qemu_opt_foreach(opts, add_semihosting_arg,
3778 &semihosting, NULL);
3780 error_report("unsupported semihosting-config %s", optarg);
3784 case QEMU_OPTION_tdf:
3785 error_report("warning: ignoring deprecated option");
3787 case QEMU_OPTION_name:
3788 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3794 case QEMU_OPTION_prom_env:
3795 if (nb_prom_envs >= MAX_PROM_ENVS) {
3796 error_report("too many prom variables");
3799 prom_envs[nb_prom_envs] = optarg;
3802 case QEMU_OPTION_old_param:
3805 case QEMU_OPTION_clock:
3806 /* Clock options no longer exist. Keep this option for
3807 * backward compatibility.
3810 case QEMU_OPTION_startdate:
3811 configure_rtc_date_offset(optarg, 1);
3813 case QEMU_OPTION_rtc:
3814 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3819 configure_rtc(opts);
3821 case QEMU_OPTION_tb_size:
3822 tcg_tb_size = strtol(optarg, NULL, 0);
3823 if (tcg_tb_size < 0) {
3827 case QEMU_OPTION_icount:
3828 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3834 case QEMU_OPTION_incoming:
3836 runstate_set(RUN_STATE_INMIGRATE);
3840 case QEMU_OPTION_only_migratable:
3841 only_migratable = 1;
3843 case QEMU_OPTION_nodefaults:
3846 case QEMU_OPTION_xen_domid:
3847 if (!(xen_available())) {
3848 error_report("Option not supported for this target");
3851 xen_domid = atoi(optarg);
3853 case QEMU_OPTION_xen_create:
3854 if (!(xen_available())) {
3855 error_report("Option not supported for this target");
3858 xen_mode = XEN_CREATE;
3860 case QEMU_OPTION_xen_attach:
3861 if (!(xen_available())) {
3862 error_report("Option not supported for this target");
3865 xen_mode = XEN_ATTACH;
3867 case QEMU_OPTION_trace:
3869 trace_file = trace_opt_parse(optarg);
3871 case QEMU_OPTION_readconfig:
3873 int ret = qemu_read_config_file(optarg);
3875 error_report("read config %s: %s", optarg,
3881 case QEMU_OPTION_spice:
3882 olist = qemu_find_opts("spice");
3884 error_report("spice support is disabled");
3887 opts = qemu_opts_parse_noisily(olist, optarg, false);
3893 case QEMU_OPTION_writeconfig:
3896 if (strcmp(optarg, "-") == 0) {
3899 fp = fopen(optarg, "w");
3901 error_report("open %s: %s", optarg,
3906 qemu_config_write(fp);
3912 case QEMU_OPTION_qtest:
3913 qtest_chrdev = optarg;
3915 case QEMU_OPTION_qtest_log:
3918 case QEMU_OPTION_sandbox:
3919 opts = qemu_opts_parse_noisily(qemu_find_opts("sandbox"),
3925 case QEMU_OPTION_add_fd:
3927 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
3933 error_report("File descriptor passing is disabled on this "
3938 case QEMU_OPTION_object:
3939 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
3945 case QEMU_OPTION_realtime:
3946 opts = qemu_opts_parse_noisily(qemu_find_opts("realtime"),
3951 enable_mlock = qemu_opt_get_bool(opts, "mlock", true);
3953 case QEMU_OPTION_msg:
3954 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
3959 configure_msg(opts);
3961 case QEMU_OPTION_dump_vmstate:
3962 if (vmstate_dump_file) {
3963 error_report("only one '-dump-vmstate' "
3964 "option may be given");
3967 vmstate_dump_file = fopen(optarg, "w");
3968 if (vmstate_dump_file == NULL) {
3969 error_report("open %s: %s", optarg, strerror(errno));
3974 os_parse_cmd_args(popt->index, optarg);
3979 * Clear error location left behind by the loop.
3980 * Best done right after the loop. Do not insert code here!
3984 replay_configure(icount_opts);
3986 machine_class = select_machine();
3988 set_memory_options(&ram_slots, &maxram_size, machine_class);
3992 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
3993 error_report("could not acquire pid file: %s", strerror(errno));
3997 if (qemu_init_main_loop(&main_loop_err)) {
3998 error_report_err(main_loop_err);
4002 if (qemu_opts_foreach(qemu_find_opts("sandbox"),
4003 parse_sandbox, NULL, NULL)) {
4007 if (qemu_opts_foreach(qemu_find_opts("name"),
4008 parse_name, NULL, NULL)) {
4013 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4014 parse_add_fd, NULL, NULL)) {
4018 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4019 cleanup_add_fd, NULL, NULL)) {
4024 current_machine = MACHINE(object_new(object_class_get_name(
4025 OBJECT_CLASS(machine_class))));
4026 if (machine_help_func(qemu_get_machine_opts(), current_machine)) {
4029 object_property_add_child(object_get_root(), "machine",
4030 OBJECT(current_machine), &error_abort);
4032 if (machine_class->minimum_page_bits) {
4033 if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
4034 /* This would be a board error: specifying a minimum smaller than
4035 * a target's compile-time fixed setting.
4037 g_assert_not_reached();
4041 cpu_exec_init_all();
4043 if (machine_class->hw_version) {
4044 qemu_set_hw_version(machine_class->hw_version);
4047 if (cpu_model && is_help_option(cpu_model)) {
4048 list_cpus(stdout, &fprintf, cpu_model);
4052 if (!trace_init_backends()) {
4055 trace_init_file(trace_file);
4057 /* Open the logfile at this point and set the log mask if necessary.
4060 qemu_set_log_filename(log_file, &error_fatal);
4065 mask = qemu_str_to_log_mask(log_mask);
4067 qemu_print_log_usage(stdout);
4075 /* If no data_dir is specified then try to find it relative to the
4077 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4078 data_dir[data_dir_idx] = os_find_datadir();
4079 if (data_dir[data_dir_idx] != NULL) {
4083 /* If all else fails use the install path specified when building. */
4084 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4085 data_dir[data_dir_idx++] = CONFIG_QEMU_DATADIR;
4088 /* -L help lists the data directories and exits. */
4089 if (list_data_dirs) {
4090 for (i = 0; i < data_dir_idx; i++) {
4091 printf("%s\n", data_dir[i]);
4096 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
4098 machine_class->max_cpus = machine_class->max_cpus ?: 1; /* Default to UP */
4099 if (max_cpus > machine_class->max_cpus) {
4100 error_report("Number of SMP CPUs requested (%d) exceeds max CPUs "
4101 "supported by machine '%s' (%d)", max_cpus,
4102 machine_class->name, machine_class->max_cpus);
4107 * Get the default machine options from the machine if it is not already
4108 * specified either by the configuration file or by the command line.
4110 if (machine_class->default_machine_opts) {
4111 qemu_opts_set_defaults(qemu_find_opts("machine"),
4112 machine_class->default_machine_opts, 0);
4115 qemu_opts_foreach(qemu_find_opts("device"),
4116 default_driver_check, NULL, NULL);
4117 qemu_opts_foreach(qemu_find_opts("global"),
4118 default_driver_check, NULL, NULL);
4120 if (!vga_model && !default_vga) {
4121 vga_interface_type = VGA_DEVICE;
4123 if (!has_defaults || machine_class->no_serial) {
4126 if (!has_defaults || machine_class->no_parallel) {
4127 default_parallel = 0;
4129 if (!has_defaults || !machine_class->use_virtcon) {
4130 default_virtcon = 0;
4132 if (!has_defaults || !machine_class->use_sclp) {
4135 if (!has_defaults || machine_class->no_floppy) {
4138 if (!has_defaults || machine_class->no_cdrom) {
4141 if (!has_defaults || machine_class->no_sdcard) {
4144 if (!has_defaults) {
4145 default_monitor = 0;
4150 if (is_daemonized()) {
4151 /* According to documentation and historically, -nographic redirects
4152 * serial port, parallel port and monitor to stdio, which does not work
4153 * with -daemonize. We can redirect these to null instead, but since
4154 * -nographic is legacy, let's just error out.
4155 * We disallow -nographic only if all other ports are not redirected
4156 * explicitly, to not break existing legacy setups which uses
4157 * -nographic _and_ redirects all ports explicitly - this is valid
4158 * usage, -nographic is just a no-op in this case.
4161 && (default_parallel || default_serial
4162 || default_monitor || default_virtcon)) {
4163 error_report("-nographic cannot be used with -daemonize");
4166 #ifdef CONFIG_CURSES
4167 if (display_type == DT_CURSES) {
4168 error_report("curses display cannot be used with -daemonize");
4175 if (default_parallel)
4176 add_device_config(DEV_PARALLEL, "null");
4177 if (default_serial && default_monitor) {
4178 add_device_config(DEV_SERIAL, "mon:stdio");
4179 } else if (default_virtcon && default_monitor) {
4180 add_device_config(DEV_VIRTCON, "mon:stdio");
4181 } else if (default_sclp && default_monitor) {
4182 add_device_config(DEV_SCLP, "mon:stdio");
4185 add_device_config(DEV_SERIAL, "stdio");
4186 if (default_virtcon)
4187 add_device_config(DEV_VIRTCON, "stdio");
4189 add_device_config(DEV_SCLP, "stdio");
4191 if (default_monitor)
4192 monitor_parse("stdio", "readline", false);
4196 add_device_config(DEV_SERIAL, "vc:80Cx24C");
4197 if (default_parallel)
4198 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
4199 if (default_monitor)
4200 monitor_parse("vc:80Cx24C", "readline", false);
4201 if (default_virtcon)
4202 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
4204 add_device_config(DEV_SCLP, "vc:80Cx24C");
4208 #if defined(CONFIG_VNC)
4209 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
4213 if (display_type == DT_DEFAULT && !display_remote) {
4214 #if defined(CONFIG_GTK)
4215 display_type = DT_GTK;
4216 #elif defined(CONFIG_SDL)
4217 display_type = DT_SDL;
4218 #elif defined(CONFIG_COCOA)
4219 display_type = DT_COCOA;
4220 #elif defined(CONFIG_VNC)
4221 vnc_parse("localhost:0,to=99,id=default", &error_abort);
4223 display_type = DT_NONE;
4227 if ((no_frame || alt_grab || ctrl_grab) && display_type != DT_SDL) {
4228 error_report("-no-frame, -alt-grab and -ctrl-grab are only valid "
4229 "for SDL, ignoring option");
4231 if (no_quit && (display_type != DT_GTK && display_type != DT_SDL)) {
4232 error_report("-no-quit is only valid for GTK and SDL, "
4236 if (display_type == DT_GTK) {
4237 early_gtk_display_init(request_opengl);
4240 if (display_type == DT_SDL) {
4241 sdl_display_early_init(request_opengl);
4244 qemu_console_early_init();
4246 if (request_opengl == 1 && display_opengl == 0) {
4247 #if defined(CONFIG_OPENGL)
4248 error_report("OpenGL is not supported by the display");
4250 error_report("OpenGL support is disabled");
4258 if (qemu_opts_foreach(qemu_find_opts("object"),
4259 user_creatable_add_opts_foreach,
4260 object_create_initial, NULL)) {
4264 if (qemu_opts_foreach(qemu_find_opts("chardev"),
4265 chardev_init_func, NULL, NULL)) {
4269 #ifdef CONFIG_VIRTFS
4270 if (qemu_opts_foreach(qemu_find_opts("fsdev"),
4271 fsdev_init_func, NULL, NULL)) {
4276 if (qemu_opts_foreach(qemu_find_opts("device"),
4277 device_help_func, NULL, NULL)) {
4281 machine_opts = qemu_get_machine_opts();
4282 if (qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
4284 object_unref(OBJECT(current_machine));
4288 configure_accelerator(current_machine);
4291 qtest_init(qtest_chrdev, qtest_log, &error_fatal);
4294 machine_opts = qemu_get_machine_opts();
4295 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4296 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4297 kernel_cmdline = qemu_opt_get(machine_opts, "append");
4298 bios_name = qemu_opt_get(machine_opts, "firmware");
4300 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4302 boot_order = qemu_opt_get(opts, "order");
4304 validate_bootdevices(boot_order, &error_fatal);
4307 boot_once = qemu_opt_get(opts, "once");
4309 validate_bootdevices(boot_once, &error_fatal);
4312 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
4313 boot_strict = qemu_opt_get_bool(opts, "strict", false);
4317 boot_order = machine_class->default_boot_order;
4320 if (!kernel_cmdline) {
4321 kernel_cmdline = "";
4322 current_machine->kernel_cmdline = (char *)kernel_cmdline;
4325 linux_boot = (kernel_filename != NULL);
4327 if (!linux_boot && *kernel_cmdline != '\0') {
4328 error_report("-append only allowed with -kernel option");
4332 if (!linux_boot && initrd_filename != NULL) {
4333 error_report("-initrd only allowed with -kernel option");
4337 if (semihosting_enabled() && !semihosting_get_argc() && kernel_filename) {
4338 /* fall back to the -kernel/-append */
4339 semihosting_arg_fallback(kernel_filename, kernel_cmdline);
4342 os_set_line_buffering();
4344 /* spice needs the timers to be initialized by this point */
4349 if (!tcg_enabled()) {
4350 error_report("-icount is not allowed with hardware virtualization");
4353 configure_icount(icount_opts, &error_abort);
4354 qemu_opts_del(icount_opts);
4358 QemuOptsList *net = qemu_find_opts("net");
4359 qemu_opts_set(net, NULL, "type", "nic", &error_abort);
4361 qemu_opts_set(net, NULL, "type", "user", &error_abort);
4367 if (net_init_clients() < 0) {
4371 if (qemu_opts_foreach(qemu_find_opts("object"),
4372 user_creatable_add_opts_foreach,
4373 object_create_delayed, NULL)) {
4378 if (tpm_init() < 0) {
4383 /* init the bluetooth world */
4384 if (foreach_device_config(DEV_BT, bt_parse))
4387 if (!xen_enabled()) {
4388 /* On 32-bit hosts, QEMU is limited by virtual address space */
4389 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4390 error_report("at most 2047 MB RAM can be simulated");
4398 /* If the currently selected machine wishes to override the units-per-bus
4399 * property of its default HBA interface type, do so now. */
4400 if (machine_class->units_per_default_bus) {
4401 override_max_devs(machine_class->block_default_type,
4402 machine_class->units_per_default_bus);
4405 /* open the virtual block devices */
4406 if (snapshot || replay_mode != REPLAY_MODE_NONE) {
4407 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
4410 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
4411 &machine_class->block_default_type, NULL)) {
4415 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
4417 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4418 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
4420 parse_numa_opts(machine_class);
4422 if (qemu_opts_foreach(qemu_find_opts("mon"),
4423 mon_init_func, NULL, NULL)) {
4427 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4429 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4431 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4433 if (foreach_device_config(DEV_SCLP, sclp_parse) < 0) {
4436 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4439 /* If no default VGA is requested, the default is "none". */
4441 if (machine_class->default_display) {
4442 vga_model = machine_class->default_display;
4443 } else if (vga_interface_available(VGA_CIRRUS)) {
4444 vga_model = "cirrus";
4445 } else if (vga_interface_available(VGA_STD)) {
4450 select_vgahw(vga_model);
4454 i = select_watchdog(watchdog);
4456 exit (i == 1 ? 1 : 0);
4459 machine_register_compat_props(current_machine);
4461 qemu_opts_foreach(qemu_find_opts("global"),
4462 global_init_func, NULL, NULL);
4464 /* This checkpoint is required by replay to separate prior clock
4465 reading from the other reads, because timer polling functions query
4466 clock values from the log. */
4467 replay_checkpoint(CHECKPOINT_INIT);
4468 qdev_machine_init();
4470 current_machine->ram_size = ram_size;
4471 current_machine->maxram_size = maxram_size;
4472 current_machine->ram_slots = ram_slots;
4473 current_machine->boot_order = boot_order;
4474 current_machine->cpu_model = cpu_model;
4476 machine_class->init(current_machine);
4482 if (hax_enabled()) {
4486 if (qemu_opts_foreach(qemu_find_opts("fw_cfg"),
4487 parse_fw_cfg, fw_cfg_find(), NULL) != 0) {
4491 /* init USB devices */
4492 if (machine_usb(current_machine)) {
4493 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4497 /* Check if IGD GFX passthrough. */
4500 /* init generic devices */
4501 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
4502 if (qemu_opts_foreach(qemu_find_opts("device"),
4503 device_init_func, NULL, NULL)) {
4507 cpu_synchronize_all_post_init();
4509 numa_post_machine_init();
4511 rom_reset_order_override();
4514 * Create frontends for -drive if=scsi leftovers.
4515 * Normally, frontends for -drive get created by machine
4516 * initialization for onboard SCSI HBAs. However, we create a few
4517 * more ever since SCSI qdevification, but this is pretty much an
4518 * implementation accident, and deprecated.
4520 scsi_legacy_handle_cmdline();
4522 /* Did we create any drives that we failed to create a device for? */
4523 drive_check_orphaned();
4525 /* Don't warn about the default network setup that you get if
4526 * no command line -net or -netdev options are specified. There
4527 * are two cases that we would otherwise complain about:
4528 * (1) board doesn't support a NIC but the implicit "-net nic"
4530 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
4531 * sets up a nic that isn't connected to anything.
4534 net_check_clients();
4539 qemu_boot_set(boot_once, &error_fatal);
4540 qemu_register_reset(restore_boot_order, g_strdup(boot_order));
4543 ds = init_displaystate();
4545 /* init local displays */
4546 switch (display_type) {
4548 curses_display_init(ds, full_screen);
4551 sdl_display_init(ds, full_screen, no_frame);
4554 cocoa_display_init(ds, full_screen);
4557 gtk_display_init(ds, full_screen, grab_on_hover);
4563 /* must be after terminal init, SDL library changes signal handlers */
4564 os_setup_signal_handling();
4566 /* init remote displays */
4568 qemu_opts_foreach(qemu_find_opts("vnc"),
4569 vnc_init_func, NULL, NULL);
4573 qemu_spice_display_init();
4576 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
4580 qdev_machine_creation_done();
4582 /* TODO: once all bus devices are qdevified, this should be done
4583 * when bus is created by qdev.c */
4584 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4585 qemu_run_machine_init_done_notifiers();
4587 if (rom_check_and_register_reset() != 0) {
4588 error_report("rom check and register reset failed");
4594 /* This checkpoint is required by replay to separate prior clock
4595 reading from the other reads, because timer polling functions query
4596 clock values from the log. */
4597 replay_checkpoint(CHECKPOINT_RESET);
4598 qemu_system_reset(VMRESET_SILENT);
4599 register_global_state();
4600 if (replay_mode != REPLAY_MODE_NONE) {
4601 replay_vmstate_init();
4602 } else if (loadvm) {
4603 if (load_vmstate(loadvm) < 0) {
4608 qdev_prop_check_globals();
4609 if (vmstate_dump_file) {
4611 dump_vmstate_json_to_file(vmstate_dump_file);
4616 Error *local_err = NULL;
4617 qemu_start_incoming_migration(incoming, &local_err);
4619 error_reportf_err(local_err, "-incoming %s: ", incoming);
4622 } else if (autostart) {
4629 replay_disable_events();
4630 iothread_stop_all();
4636 /* vhost-user must be cleaned up before chardevs. */