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_accel_opts = {
305 .implied_opt_name = "accel",
306 .head = QTAILQ_HEAD_INITIALIZER(qemu_accel_opts.head),
311 .type = QEMU_OPT_STRING,
312 .help = "Select the type of accelerator",
316 .type = QEMU_OPT_STRING,
317 .help = "Enable/disable multi-threaded TCG",
319 { /* end of list */ }
323 static QemuOptsList qemu_boot_opts = {
325 .implied_opt_name = "order",
327 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
331 .type = QEMU_OPT_STRING,
334 .type = QEMU_OPT_STRING,
337 .type = QEMU_OPT_BOOL,
340 .type = QEMU_OPT_STRING,
342 .name = "splash-time",
343 .type = QEMU_OPT_STRING,
345 .name = "reboot-timeout",
346 .type = QEMU_OPT_STRING,
349 .type = QEMU_OPT_BOOL,
355 static QemuOptsList qemu_add_fd_opts = {
357 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
361 .type = QEMU_OPT_NUMBER,
362 .help = "file descriptor of which a duplicate is added to fd set",
365 .type = QEMU_OPT_NUMBER,
366 .help = "ID of the fd set to add fd to",
369 .type = QEMU_OPT_STRING,
370 .help = "free-form string used to describe fd",
372 { /* end of list */ }
376 static QemuOptsList qemu_object_opts = {
378 .implied_opt_name = "qom-type",
379 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
385 static QemuOptsList qemu_tpmdev_opts = {
387 .implied_opt_name = "type",
388 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
390 /* options are defined in the TPM backends */
391 { /* end of list */ }
395 static QemuOptsList qemu_realtime_opts = {
397 .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
401 .type = QEMU_OPT_BOOL,
403 { /* end of list */ }
407 static QemuOptsList qemu_msg_opts = {
409 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
413 .type = QEMU_OPT_BOOL,
415 { /* end of list */ }
419 static QemuOptsList qemu_name_opts = {
421 .implied_opt_name = "guest",
423 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
427 .type = QEMU_OPT_STRING,
428 .help = "Sets the name of the guest.\n"
429 "This name will be displayed in the SDL window caption.\n"
430 "The name will also be used for the VNC server",
433 .type = QEMU_OPT_STRING,
434 .help = "Sets the name of the QEMU process, as shown in top etc",
436 .name = "debug-threads",
437 .type = QEMU_OPT_BOOL,
438 .help = "When enabled, name the individual threads; defaults off.\n"
439 "NOTE: The thread names are for debugging and not a\n"
442 { /* End of list */ }
446 static QemuOptsList qemu_mem_opts = {
448 .implied_opt_name = "size",
449 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
454 .type = QEMU_OPT_SIZE,
458 .type = QEMU_OPT_NUMBER,
462 .type = QEMU_OPT_SIZE,
464 { /* end of list */ }
468 static QemuOptsList qemu_icount_opts = {
470 .implied_opt_name = "shift",
472 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
476 .type = QEMU_OPT_STRING,
479 .type = QEMU_OPT_BOOL,
482 .type = QEMU_OPT_BOOL,
485 .type = QEMU_OPT_STRING,
488 .type = QEMU_OPT_STRING,
490 .name = "rrsnapshot",
491 .type = QEMU_OPT_STRING,
493 { /* end of list */ }
497 static QemuOptsList qemu_semihosting_config_opts = {
498 .name = "semihosting-config",
499 .implied_opt_name = "enable",
500 .head = QTAILQ_HEAD_INITIALIZER(qemu_semihosting_config_opts.head),
504 .type = QEMU_OPT_BOOL,
507 .type = QEMU_OPT_STRING,
510 .type = QEMU_OPT_STRING,
512 { /* end of list */ }
516 static QemuOptsList qemu_fw_cfg_opts = {
518 .implied_opt_name = "name",
519 .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
523 .type = QEMU_OPT_STRING,
524 .help = "Sets the fw_cfg name of the blob to be inserted",
527 .type = QEMU_OPT_STRING,
528 .help = "Sets the name of the file from which\n"
529 "the fw_cfg blob will be loaded",
532 .type = QEMU_OPT_STRING,
533 .help = "Sets content of the blob to be inserted from a string",
535 { /* end of list */ }
540 * Get machine options
542 * Returns: machine options (never null).
544 QemuOpts *qemu_get_machine_opts(void)
546 return qemu_find_opts_singleton("machine");
549 const char *qemu_get_vm_name(void)
554 static void res_free(void)
556 g_free(boot_splash_filedata);
557 boot_splash_filedata = NULL;
560 static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
562 const char *driver = qemu_opt_get(opts, "driver");
567 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
568 if (strcmp(default_list[i].driver, driver) != 0)
570 *(default_list[i].flag) = 0;
575 /***********************************************************/
578 static RunState current_run_state = RUN_STATE_PRELAUNCH;
580 /* We use RUN_STATE__MAX but any invalid value will do */
581 static RunState vmstop_requested = RUN_STATE__MAX;
582 static QemuMutex vmstop_lock;
587 } RunStateTransition;
589 static const RunStateTransition runstate_transitions_def[] = {
591 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
592 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
593 { RUN_STATE_DEBUG, RUN_STATE_PRELAUNCH },
595 { RUN_STATE_INMIGRATE, RUN_STATE_INTERNAL_ERROR },
596 { RUN_STATE_INMIGRATE, RUN_STATE_IO_ERROR },
597 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
598 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
599 { RUN_STATE_INMIGRATE, RUN_STATE_SHUTDOWN },
600 { RUN_STATE_INMIGRATE, RUN_STATE_SUSPENDED },
601 { RUN_STATE_INMIGRATE, RUN_STATE_WATCHDOG },
602 { RUN_STATE_INMIGRATE, RUN_STATE_GUEST_PANICKED },
603 { RUN_STATE_INMIGRATE, RUN_STATE_FINISH_MIGRATE },
604 { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH },
605 { RUN_STATE_INMIGRATE, RUN_STATE_POSTMIGRATE },
606 { RUN_STATE_INMIGRATE, RUN_STATE_COLO },
608 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
609 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
610 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PRELAUNCH },
612 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
613 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
614 { RUN_STATE_IO_ERROR, RUN_STATE_PRELAUNCH },
616 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
617 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
618 { RUN_STATE_PAUSED, RUN_STATE_PRELAUNCH },
619 { RUN_STATE_PAUSED, RUN_STATE_COLO},
621 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
622 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
623 { RUN_STATE_POSTMIGRATE, RUN_STATE_PRELAUNCH },
625 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
626 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
627 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
629 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
630 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
631 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PRELAUNCH },
632 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_COLO},
634 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
635 { RUN_STATE_RESTORE_VM, RUN_STATE_PRELAUNCH },
637 { RUN_STATE_COLO, RUN_STATE_RUNNING },
639 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
640 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
641 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
642 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
643 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
644 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
645 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
646 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
647 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
648 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
649 { RUN_STATE_RUNNING, RUN_STATE_COLO},
651 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
653 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
654 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
655 { RUN_STATE_SHUTDOWN, RUN_STATE_PRELAUNCH },
657 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
658 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
659 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
660 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
661 { RUN_STATE_SUSPENDED, RUN_STATE_PRELAUNCH },
662 { RUN_STATE_SUSPENDED, RUN_STATE_COLO},
664 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
665 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
666 { RUN_STATE_WATCHDOG, RUN_STATE_PRELAUNCH },
667 { RUN_STATE_WATCHDOG, RUN_STATE_COLO},
669 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
670 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
671 { RUN_STATE_GUEST_PANICKED, RUN_STATE_PRELAUNCH },
673 { RUN_STATE__MAX, RUN_STATE__MAX },
676 static bool runstate_valid_transitions[RUN_STATE__MAX][RUN_STATE__MAX];
678 bool runstate_check(RunState state)
680 return current_run_state == state;
683 bool runstate_store(char *str, size_t size)
685 const char *state = RunState_lookup[current_run_state];
686 size_t len = strlen(state) + 1;
691 memcpy(str, state, len);
695 static void runstate_init(void)
697 const RunStateTransition *p;
699 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
700 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE__MAX; p++) {
701 runstate_valid_transitions[p->from][p->to] = true;
704 qemu_mutex_init(&vmstop_lock);
707 /* This function will abort() on invalid state transitions */
708 void runstate_set(RunState new_state)
710 assert(new_state < RUN_STATE__MAX);
712 if (current_run_state == new_state) {
716 if (!runstate_valid_transitions[current_run_state][new_state]) {
717 error_report("invalid runstate transition: '%s' -> '%s'",
718 RunState_lookup[current_run_state],
719 RunState_lookup[new_state]);
722 trace_runstate_set(new_state);
723 current_run_state = new_state;
726 int runstate_is_running(void)
728 return runstate_check(RUN_STATE_RUNNING);
731 bool runstate_needs_reset(void)
733 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
734 runstate_check(RUN_STATE_SHUTDOWN);
737 StatusInfo *qmp_query_status(Error **errp)
739 StatusInfo *info = g_malloc0(sizeof(*info));
741 info->running = runstate_is_running();
742 info->singlestep = singlestep;
743 info->status = current_run_state;
748 bool qemu_vmstop_requested(RunState *r)
750 qemu_mutex_lock(&vmstop_lock);
751 *r = vmstop_requested;
752 vmstop_requested = RUN_STATE__MAX;
753 qemu_mutex_unlock(&vmstop_lock);
754 return *r < RUN_STATE__MAX;
757 void qemu_system_vmstop_request_prepare(void)
759 qemu_mutex_lock(&vmstop_lock);
762 void qemu_system_vmstop_request(RunState state)
764 vmstop_requested = state;
765 qemu_mutex_unlock(&vmstop_lock);
769 /***********************************************************/
770 /* real time host monotonic timer */
772 static time_t qemu_time(void)
774 return qemu_clock_get_ms(QEMU_CLOCK_HOST) / 1000;
777 /***********************************************************/
778 /* host time/date access */
779 void qemu_get_timedate(struct tm *tm, int offset)
781 time_t ti = qemu_time();
784 if (rtc_date_offset == -1) {
788 localtime_r(&ti, tm);
790 ti -= rtc_date_offset;
795 int qemu_timedate_diff(struct tm *tm)
799 if (rtc_date_offset == -1)
801 seconds = mktimegm(tm);
804 tmp.tm_isdst = -1; /* use timezone to figure it out */
805 seconds = mktime(&tmp);
808 seconds = mktimegm(tm) + rtc_date_offset;
810 return seconds - qemu_time();
813 static void configure_rtc_date_offset(const char *startdate, int legacy)
815 time_t rtc_start_date;
818 if (!strcmp(startdate, "now") && legacy) {
819 rtc_date_offset = -1;
821 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
829 } else if (sscanf(startdate, "%d-%d-%d",
841 rtc_start_date = mktimegm(&tm);
842 if (rtc_start_date == -1) {
844 error_report("invalid date format");
845 error_printf("valid formats: "
846 "'2006-06-17T16:01:21' or '2006-06-17'\n");
849 rtc_date_offset = qemu_time() - rtc_start_date;
853 static void configure_rtc(QemuOpts *opts)
857 value = qemu_opt_get(opts, "base");
859 if (!strcmp(value, "utc")) {
861 } else if (!strcmp(value, "localtime")) {
862 Error *blocker = NULL;
864 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
865 "-rtc base=localtime");
866 replay_add_blocker(blocker);
868 configure_rtc_date_offset(value, 0);
871 value = qemu_opt_get(opts, "clock");
873 if (!strcmp(value, "host")) {
874 rtc_clock = QEMU_CLOCK_HOST;
875 } else if (!strcmp(value, "rt")) {
876 rtc_clock = QEMU_CLOCK_REALTIME;
877 } else if (!strcmp(value, "vm")) {
878 rtc_clock = QEMU_CLOCK_VIRTUAL;
880 error_report("invalid option value '%s'", value);
884 value = qemu_opt_get(opts, "driftfix");
886 if (!strcmp(value, "slew")) {
887 static GlobalProperty slew_lost_ticks = {
888 .driver = "mc146818rtc",
889 .property = "lost_tick_policy",
893 qdev_prop_register_global(&slew_lost_ticks);
894 } else if (!strcmp(value, "none")) {
895 /* discard is default */
897 error_report("invalid option value '%s'", value);
903 /***********************************************************/
904 /* Bluetooth support */
907 static struct HCIInfo *hci_table[MAX_NICS];
909 struct HCIInfo *qemu_next_hci(void)
911 if (cur_hci == nb_hcis)
914 return hci_table[cur_hci++];
917 static int bt_hci_parse(const char *str)
922 if (nb_hcis >= MAX_NICS) {
923 error_report("too many bluetooth HCIs (max %i)", MAX_NICS);
936 bdaddr.b[5] = 0x56 + nb_hcis;
937 hci->bdaddr_set(hci, bdaddr.b);
939 hci_table[nb_hcis++] = hci;
944 static void bt_vhci_add(int vlan_id)
946 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
949 error_report("warning: adding a VHCI to an empty scatternet %i",
952 bt_vhci_init(bt_new_hci(vlan));
955 static struct bt_device_s *bt_device_add(const char *opt)
957 struct bt_scatternet_s *vlan;
959 char *endp = strstr(opt, ",vlan=");
960 int len = (endp ? endp - opt : strlen(opt)) + 1;
963 pstrcpy(devname, MIN(sizeof(devname), len), opt);
966 vlan_id = strtol(endp + 6, &endp, 0);
968 error_report("unrecognised bluetooth vlan Id");
973 vlan = qemu_find_bt_vlan(vlan_id);
976 error_report("warning: adding a slave device to an empty scatternet %i",
979 if (!strcmp(devname, "keyboard"))
980 return bt_keyboard_init(vlan);
982 error_report("unsupported bluetooth device '%s'", devname);
986 static int bt_parse(const char *opt)
988 const char *endp, *p;
991 if (strstart(opt, "hci", &endp)) {
992 if (!*endp || *endp == ',') {
994 if (!strstart(endp, ",vlan=", 0))
997 return bt_hci_parse(opt);
999 } else if (strstart(opt, "vhci", &endp)) {
1000 if (!*endp || *endp == ',') {
1002 if (strstart(endp, ",vlan=", &p)) {
1003 vlan = strtol(p, (char **) &endp, 0);
1005 error_report("bad scatternet '%s'", p);
1009 error_report("bad parameter '%s'", endp + 1);
1018 } else if (strstart(opt, "device:", &endp))
1019 return !bt_device_add(endp);
1021 error_report("bad bluetooth parameter '%s'", opt);
1025 static int parse_sandbox(void *opaque, QemuOpts *opts, Error **errp)
1027 /* FIXME: change this to true for 1.3 */
1028 if (qemu_opt_get_bool(opts, "enable", false)) {
1029 #ifdef CONFIG_SECCOMP
1030 if (seccomp_start() < 0) {
1031 error_report("failed to install seccomp syscall filter "
1036 error_report("seccomp support is disabled");
1044 static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
1046 const char *proc_name;
1048 if (qemu_opt_get(opts, "debug-threads")) {
1049 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
1051 qemu_name = qemu_opt_get(opts, "guest");
1053 proc_name = qemu_opt_get(opts, "process");
1055 os_set_proc_name(proc_name);
1061 bool defaults_enabled(void)
1063 return has_defaults;
1067 static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1069 int fd, dupfd, flags;
1071 const char *fd_opaque = NULL;
1074 fd = qemu_opt_get_number(opts, "fd", -1);
1075 fdset_id = qemu_opt_get_number(opts, "set", -1);
1076 fd_opaque = qemu_opt_get(opts, "opaque");
1079 error_report("fd option is required and must be non-negative");
1083 if (fd <= STDERR_FILENO) {
1084 error_report("fd cannot be a standard I/O stream");
1089 * All fds inherited across exec() necessarily have FD_CLOEXEC
1090 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1092 flags = fcntl(fd, F_GETFD);
1093 if (flags == -1 || (flags & FD_CLOEXEC)) {
1094 error_report("fd is not valid or already in use");
1099 error_report("set option is required and must be non-negative");
1103 #ifdef F_DUPFD_CLOEXEC
1104 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1108 qemu_set_cloexec(dupfd);
1112 error_report("error duplicating fd: %s", strerror(errno));
1116 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1117 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
1124 static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1128 fd = qemu_opt_get_number(opts, "fd", -1);
1135 /***********************************************************/
1136 /* QEMU Block devices */
1138 #define HD_OPTS "media=disk"
1139 #define CDROM_OPTS "media=cdrom"
1141 #define PFLASH_OPTS ""
1145 static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
1147 BlockInterfaceType *block_default_type = opaque;
1149 return drive_new(opts, *block_default_type) == NULL;
1152 static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
1154 if (qemu_opt_get(opts, "snapshot") == NULL) {
1155 qemu_opt_set(opts, "snapshot", "on", &error_abort);
1160 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1161 int index, const char *optstr)
1166 if (!enable || drive_get_by_index(type, index)) {
1170 opts = drive_add(type, index, NULL, optstr);
1172 drive_enable_snapshot(NULL, opts, NULL);
1175 dinfo = drive_new(opts, type);
1179 dinfo->is_default = true;
1183 static QemuOptsList qemu_smp_opts = {
1185 .implied_opt_name = "cpus",
1186 .merge_lists = true,
1187 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
1191 .type = QEMU_OPT_NUMBER,
1194 .type = QEMU_OPT_NUMBER,
1197 .type = QEMU_OPT_NUMBER,
1200 .type = QEMU_OPT_NUMBER,
1203 .type = QEMU_OPT_NUMBER,
1205 { /*End of list */ }
1209 static void smp_parse(QemuOpts *opts)
1212 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
1213 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
1214 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
1215 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
1217 /* compute missing values, prefer sockets over cores over threads */
1218 if (cpus == 0 || sockets == 0) {
1219 sockets = sockets > 0 ? sockets : 1;
1220 cores = cores > 0 ? cores : 1;
1221 threads = threads > 0 ? threads : 1;
1223 cpus = cores * threads * sockets;
1225 } else if (cores == 0) {
1226 threads = threads > 0 ? threads : 1;
1227 cores = cpus / (sockets * threads);
1228 cores = cores > 0 ? cores : 1;
1229 } else if (threads == 0) {
1230 threads = cpus / (cores * sockets);
1231 threads = threads > 0 ? threads : 1;
1232 } else if (sockets * cores * threads < cpus) {
1233 error_report("cpu topology: "
1234 "sockets (%u) * cores (%u) * threads (%u) < "
1236 sockets, cores, threads, cpus);
1240 max_cpus = qemu_opt_get_number(opts, "maxcpus", cpus);
1242 if (max_cpus < cpus) {
1243 error_report("maxcpus must be equal to or greater than smp");
1247 if (sockets * cores * threads > max_cpus) {
1248 error_report("cpu topology: "
1249 "sockets (%u) * cores (%u) * threads (%u) > "
1251 sockets, cores, threads, max_cpus);
1257 smp_threads = threads;
1261 Error *blocker = NULL;
1262 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
1263 replay_add_blocker(blocker);
1267 static void realtime_init(void)
1270 if (os_mlock() < 0) {
1271 error_report("locking memory failed");
1278 static void configure_msg(QemuOpts *opts)
1280 enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
1283 /***********************************************************/
1286 typedef struct SemihostingConfig {
1288 SemihostingTarget target;
1291 const char *cmdline; /* concatenated argv */
1292 } SemihostingConfig;
1294 static SemihostingConfig semihosting;
1296 bool semihosting_enabled(void)
1298 return semihosting.enabled;
1301 SemihostingTarget semihosting_get_target(void)
1303 return semihosting.target;
1306 const char *semihosting_get_arg(int i)
1308 if (i >= semihosting.argc) {
1311 return semihosting.argv[i];
1314 int semihosting_get_argc(void)
1316 return semihosting.argc;
1319 const char *semihosting_get_cmdline(void)
1321 if (semihosting.cmdline == NULL && semihosting.argc > 0) {
1322 semihosting.cmdline = g_strjoinv(" ", (gchar **)semihosting.argv);
1324 return semihosting.cmdline;
1327 static int add_semihosting_arg(void *opaque,
1328 const char *name, const char *val,
1331 SemihostingConfig *s = opaque;
1332 if (strcmp(name, "arg") == 0) {
1334 /* one extra element as g_strjoinv() expects NULL-terminated array */
1335 s->argv = g_realloc(s->argv, (s->argc + 1) * sizeof(void *));
1336 s->argv[s->argc - 1] = val;
1337 s->argv[s->argc] = NULL;
1342 /* Use strings passed via -kernel/-append to initialize semihosting.argv[] */
1343 static inline void semihosting_arg_fallback(const char *file, const char *cmd)
1348 add_semihosting_arg(&semihosting, "arg", file, NULL);
1350 /* split -append and initialize argv[1..n] */
1351 cmd_token = strtok(g_strdup(cmd), " ");
1353 add_semihosting_arg(&semihosting, "arg", cmd_token, NULL);
1354 cmd_token = strtok(NULL, " ");
1358 /* Now we still need this for compatibility with XEN. */
1359 bool has_igd_gfx_passthru;
1360 static void igd_gfx_passthru(void)
1362 has_igd_gfx_passthru = current_machine->igd_gfx_passthru;
1365 /***********************************************************/
1368 static int usb_device_add(const char *devname)
1370 USBDevice *dev = NULL;
1371 #ifndef CONFIG_LINUX
1375 if (!machine_usb(current_machine)) {
1379 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1380 dev = usbdevice_create(devname);
1384 /* the other ones */
1385 #ifndef CONFIG_LINUX
1386 /* only the linux version is qdev-ified, usb-bsd still needs this */
1387 if (strstart(devname, "host:", &p)) {
1388 dev = usb_host_device_open(usb_bus_find(-1), p);
1398 static int usb_device_del(const char *devname)
1403 if (strstart(devname, "host:", &p)) {
1407 if (!machine_usb(current_machine)) {
1411 p = strchr(devname, '.');
1414 bus_num = strtoul(devname, NULL, 0);
1415 addr = strtoul(p + 1, NULL, 0);
1417 return usb_device_delete_addr(bus_num, addr);
1420 static int usb_parse(const char *cmdline)
1423 r = usb_device_add(cmdline);
1425 error_report("could not add USB device '%s'", cmdline);
1430 void hmp_usb_add(Monitor *mon, const QDict *qdict)
1432 const char *devname = qdict_get_str(qdict, "devname");
1433 if (usb_device_add(devname) < 0) {
1434 error_report("could not add USB device '%s'", devname);
1438 void hmp_usb_del(Monitor *mon, const QDict *qdict)
1440 const char *devname = qdict_get_str(qdict, "devname");
1441 if (usb_device_del(devname) < 0) {
1442 error_report("could not delete USB device '%s'", devname);
1446 /***********************************************************/
1447 /* machine registration */
1449 MachineState *current_machine;
1451 static MachineClass *find_machine(const char *name)
1453 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1454 MachineClass *mc = NULL;
1456 for (el = machines; el; el = el->next) {
1457 MachineClass *temp = el->data;
1459 if (!strcmp(temp->name, name)) {
1464 !strcmp(temp->alias, name)) {
1470 g_slist_free(machines);
1474 MachineClass *find_default_machine(void)
1476 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1477 MachineClass *mc = NULL;
1479 for (el = machines; el; el = el->next) {
1480 MachineClass *temp = el->data;
1482 if (temp->is_default) {
1488 g_slist_free(machines);
1492 MachineInfoList *qmp_query_machines(Error **errp)
1494 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1495 MachineInfoList *mach_list = NULL;
1497 for (el = machines; el; el = el->next) {
1498 MachineClass *mc = el->data;
1499 MachineInfoList *entry;
1502 info = g_malloc0(sizeof(*info));
1503 if (mc->is_default) {
1504 info->has_is_default = true;
1505 info->is_default = true;
1509 info->has_alias = true;
1510 info->alias = g_strdup(mc->alias);
1513 info->name = g_strdup(mc->name);
1514 info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
1515 info->hotpluggable_cpus = mc->has_hotpluggable_cpus;
1517 entry = g_malloc0(sizeof(*entry));
1518 entry->value = info;
1519 entry->next = mach_list;
1523 g_slist_free(machines);
1527 static int machine_help_func(QemuOpts *opts, MachineState *machine)
1529 ObjectProperty *prop;
1530 ObjectPropertyIterator iter;
1532 if (!qemu_opt_has_help_opt(opts)) {
1536 object_property_iter_init(&iter, OBJECT(machine));
1537 while ((prop = object_property_iter_next(&iter))) {
1542 error_printf("%s.%s=%s", MACHINE_GET_CLASS(machine)->name,
1543 prop->name, prop->type);
1544 if (prop->description) {
1545 error_printf(" (%s)\n", prop->description);
1554 /***********************************************************/
1555 /* main execution loop */
1557 struct vm_change_state_entry {
1558 VMChangeStateHandler *cb;
1560 QLIST_ENTRY (vm_change_state_entry) entries;
1563 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1565 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1568 VMChangeStateEntry *e;
1570 e = g_malloc0(sizeof (*e));
1574 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1578 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1580 QLIST_REMOVE (e, entries);
1584 void vm_state_notify(int running, RunState state)
1586 VMChangeStateEntry *e, *next;
1588 trace_vm_state_notify(running, state);
1590 QLIST_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
1591 e->cb(e->opaque, running, state);
1595 static int reset_requested;
1596 static int shutdown_requested, shutdown_signal = -1;
1597 static pid_t shutdown_pid;
1598 static int powerdown_requested;
1599 static int debug_requested;
1600 static int suspend_requested;
1601 static WakeupReason wakeup_reason;
1602 static NotifierList powerdown_notifiers =
1603 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
1604 static NotifierList suspend_notifiers =
1605 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1606 static NotifierList wakeup_notifiers =
1607 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1608 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
1610 int qemu_shutdown_requested_get(void)
1612 return shutdown_requested;
1615 int qemu_reset_requested_get(void)
1617 return reset_requested;
1620 static int qemu_shutdown_requested(void)
1622 return atomic_xchg(&shutdown_requested, 0);
1625 static void qemu_kill_report(void)
1627 if (!qtest_driver() && shutdown_signal != -1) {
1628 if (shutdown_pid == 0) {
1629 /* This happens for eg ^C at the terminal, so it's worth
1630 * avoiding printing an odd message in that case.
1632 error_report("terminating on signal %d", shutdown_signal);
1634 char *shutdown_cmd = qemu_get_pid_name(shutdown_pid);
1636 error_report("terminating on signal %d from pid " FMT_pid " (%s)",
1637 shutdown_signal, shutdown_pid,
1638 shutdown_cmd ? shutdown_cmd : "<unknown process>");
1639 g_free(shutdown_cmd);
1641 shutdown_signal = -1;
1645 static int qemu_reset_requested(void)
1647 int r = reset_requested;
1648 if (r && replay_checkpoint(CHECKPOINT_RESET_REQUESTED)) {
1649 reset_requested = 0;
1655 static int qemu_suspend_requested(void)
1657 int r = suspend_requested;
1658 if (r && replay_checkpoint(CHECKPOINT_SUSPEND_REQUESTED)) {
1659 suspend_requested = 0;
1665 static WakeupReason qemu_wakeup_requested(void)
1667 return wakeup_reason;
1670 static int qemu_powerdown_requested(void)
1672 int r = powerdown_requested;
1673 powerdown_requested = 0;
1677 static int qemu_debug_requested(void)
1679 int r = debug_requested;
1680 debug_requested = 0;
1684 void qemu_system_reset(bool report)
1688 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1690 cpu_synchronize_all_states();
1692 if (mc && mc->reset) {
1695 qemu_devices_reset();
1698 qapi_event_send_reset(&error_abort);
1700 cpu_synchronize_all_post_reset();
1703 void qemu_system_guest_panicked(GuestPanicInformation *info)
1705 qemu_log_mask(LOG_GUEST_ERROR, "Guest crashed\n");
1708 current_cpu->crash_occurred = true;
1710 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE,
1711 !!info, info, &error_abort);
1712 vm_stop(RUN_STATE_GUEST_PANICKED);
1714 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_POWEROFF,
1715 !!info, info, &error_abort);
1716 qemu_system_shutdown_request();
1720 if (info->type == GUEST_PANIC_INFORMATION_KIND_HYPER_V) {
1721 qemu_log_mask(LOG_GUEST_ERROR, "HV crash parameters: (%#"PRIx64
1722 " %#"PRIx64" %#"PRIx64" %#"PRIx64" %#"PRIx64")\n",
1723 info->u.hyper_v.data->arg1,
1724 info->u.hyper_v.data->arg2,
1725 info->u.hyper_v.data->arg3,
1726 info->u.hyper_v.data->arg4,
1727 info->u.hyper_v.data->arg5);
1729 qapi_free_GuestPanicInformation(info);
1733 void qemu_system_reset_request(void)
1736 shutdown_requested = 1;
1738 reset_requested = 1;
1741 qemu_notify_event();
1744 static void qemu_system_suspend(void)
1747 notifier_list_notify(&suspend_notifiers, NULL);
1748 runstate_set(RUN_STATE_SUSPENDED);
1749 qapi_event_send_suspend(&error_abort);
1752 void qemu_system_suspend_request(void)
1754 if (runstate_check(RUN_STATE_SUSPENDED)) {
1757 suspend_requested = 1;
1759 qemu_notify_event();
1762 void qemu_register_suspend_notifier(Notifier *notifier)
1764 notifier_list_add(&suspend_notifiers, notifier);
1767 void qemu_system_wakeup_request(WakeupReason reason)
1769 trace_system_wakeup_request(reason);
1771 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1774 if (!(wakeup_reason_mask & (1 << reason))) {
1777 runstate_set(RUN_STATE_RUNNING);
1778 wakeup_reason = reason;
1779 qemu_notify_event();
1782 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1785 wakeup_reason_mask |= (1 << reason);
1787 wakeup_reason_mask &= ~(1 << reason);
1791 void qemu_register_wakeup_notifier(Notifier *notifier)
1793 notifier_list_add(&wakeup_notifiers, notifier);
1796 void qemu_system_killed(int signal, pid_t pid)
1798 shutdown_signal = signal;
1802 /* Cannot call qemu_system_shutdown_request directly because
1803 * we are in a signal handler.
1805 shutdown_requested = 1;
1806 qemu_notify_event();
1809 void qemu_system_shutdown_request(void)
1811 trace_qemu_system_shutdown_request();
1812 replay_shutdown_request();
1813 shutdown_requested = 1;
1814 qemu_notify_event();
1817 static void qemu_system_powerdown(void)
1819 qapi_event_send_powerdown(&error_abort);
1820 notifier_list_notify(&powerdown_notifiers, NULL);
1823 void qemu_system_powerdown_request(void)
1825 trace_qemu_system_powerdown_request();
1826 powerdown_requested = 1;
1827 qemu_notify_event();
1830 void qemu_register_powerdown_notifier(Notifier *notifier)
1832 notifier_list_add(&powerdown_notifiers, notifier);
1835 void qemu_system_debug_request(void)
1837 debug_requested = 1;
1838 qemu_notify_event();
1841 static bool main_loop_should_exit(void)
1844 if (qemu_debug_requested()) {
1845 vm_stop(RUN_STATE_DEBUG);
1847 if (qemu_suspend_requested()) {
1848 qemu_system_suspend();
1850 if (qemu_shutdown_requested()) {
1852 qapi_event_send_shutdown(&error_abort);
1854 vm_stop(RUN_STATE_SHUTDOWN);
1859 if (qemu_reset_requested()) {
1861 qemu_system_reset(VMRESET_REPORT);
1863 if (!runstate_check(RUN_STATE_RUNNING) &&
1864 !runstate_check(RUN_STATE_INMIGRATE)) {
1865 runstate_set(RUN_STATE_PRELAUNCH);
1868 if (qemu_wakeup_requested()) {
1870 qemu_system_reset(VMRESET_SILENT);
1871 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
1872 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
1874 qapi_event_send_wakeup(&error_abort);
1876 if (qemu_powerdown_requested()) {
1877 qemu_system_powerdown();
1879 if (qemu_vmstop_requested(&r)) {
1885 static void main_loop(void)
1889 #ifdef CONFIG_PROFILER
1893 nonblocking = tcg_enabled() && last_io > 0;
1894 #ifdef CONFIG_PROFILER
1895 ti = profile_getclock();
1897 last_io = main_loop_wait(nonblocking);
1898 #ifdef CONFIG_PROFILER
1899 dev_time += profile_getclock() - ti;
1901 } while (!main_loop_should_exit());
1904 static void version(void)
1906 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION "\n"
1907 QEMU_COPYRIGHT "\n");
1910 static void help(int exitcode)
1913 printf("usage: %s [options] [disk_image]\n\n"
1914 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1915 error_get_progname());
1917 #define QEMU_OPTIONS_GENERATE_HELP
1918 #include "qemu-options-wrapper.h"
1920 printf("\nDuring emulation, the following keys are useful:\n"
1921 "ctrl-alt-f toggle full screen\n"
1922 "ctrl-alt-n switch to virtual console 'n'\n"
1923 "ctrl-alt toggle mouse and keyboard grab\n"
1925 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1930 #define HAS_ARG 0x0001
1932 typedef struct QEMUOption {
1939 static const QEMUOption qemu_options[] = {
1940 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1941 #define QEMU_OPTIONS_GENERATE_OPTIONS
1942 #include "qemu-options-wrapper.h"
1946 typedef struct VGAInterfaceInfo {
1947 const char *opt_name; /* option name */
1948 const char *name; /* human-readable name */
1949 /* Class names indicating that support is available.
1950 * If no class is specified, the interface is always available */
1951 const char *class_names[2];
1954 static VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
1960 .name = "standard VGA",
1961 .class_names = { "VGA", "isa-vga" },
1964 .opt_name = "cirrus",
1965 .name = "Cirrus VGA",
1966 .class_names = { "cirrus-vga", "isa-cirrus-vga" },
1969 .opt_name = "vmware",
1970 .name = "VMWare SVGA",
1971 .class_names = { "vmware-svga" },
1974 .opt_name = "virtio",
1975 .name = "Virtio VGA",
1976 .class_names = { "virtio-vga" },
1981 .class_names = { "qxl-vga" },
1985 .name = "TCX framebuffer",
1986 .class_names = { "SUNW,tcx" },
1990 .name = "CG3 framebuffer",
1991 .class_names = { "cgthree" },
1994 .opt_name = "xenfb",
1998 static bool vga_interface_available(VGAInterfaceType t)
2000 VGAInterfaceInfo *ti = &vga_interfaces[t];
2002 assert(t < VGA_TYPE_MAX);
2003 return !ti->class_names[0] ||
2004 object_class_by_name(ti->class_names[0]) ||
2005 object_class_by_name(ti->class_names[1]);
2008 static void select_vgahw(const char *p)
2013 assert(vga_interface_type == VGA_NONE);
2014 for (t = 0; t < VGA_TYPE_MAX; t++) {
2015 VGAInterfaceInfo *ti = &vga_interfaces[t];
2016 if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
2017 if (!vga_interface_available(t)) {
2018 error_report("%s not available", ti->name);
2021 vga_interface_type = t;
2025 if (t == VGA_TYPE_MAX) {
2027 error_report("unknown vga type: %s", p);
2031 const char *nextopt;
2033 if (strstart(opts, ",retrace=", &nextopt)) {
2035 if (strstart(opts, "dumb", &nextopt))
2036 vga_retrace_method = VGA_RETRACE_DUMB;
2037 else if (strstart(opts, "precise", &nextopt))
2038 vga_retrace_method = VGA_RETRACE_PRECISE;
2039 else goto invalid_vga;
2040 } else goto invalid_vga;
2045 typedef enum DisplayType {
2054 static DisplayType select_display(const char *p)
2057 DisplayType display = DT_DEFAULT;
2059 if (strstart(p, "sdl", &opts)) {
2063 const char *nextopt;
2065 if (strstart(opts, ",frame=", &nextopt)) {
2067 if (strstart(opts, "on", &nextopt)) {
2069 } else if (strstart(opts, "off", &nextopt)) {
2072 goto invalid_sdl_args;
2074 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
2076 if (strstart(opts, "on", &nextopt)) {
2078 } else if (strstart(opts, "off", &nextopt)) {
2081 goto invalid_sdl_args;
2083 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2085 if (strstart(opts, "on", &nextopt)) {
2087 } else if (strstart(opts, "off", &nextopt)) {
2090 goto invalid_sdl_args;
2092 } else if (strstart(opts, ",window_close=", &nextopt)) {
2094 if (strstart(opts, "on", &nextopt)) {
2096 } else if (strstart(opts, "off", &nextopt)) {
2099 goto invalid_sdl_args;
2101 } else if (strstart(opts, ",gl=", &nextopt)) {
2103 if (strstart(opts, "on", &nextopt)) {
2105 } else if (strstart(opts, "off", &nextopt)) {
2108 goto invalid_sdl_args;
2112 error_report("invalid SDL option string");
2118 error_report("SDL support is disabled");
2121 } else if (strstart(p, "vnc", &opts)) {
2123 vnc_parse(opts + 1, &error_fatal);
2125 error_report("VNC requires a display argument vnc=<display>");
2128 } else if (strstart(p, "curses", &opts)) {
2129 #ifdef CONFIG_CURSES
2130 display = DT_CURSES;
2132 error_report("curses support is disabled");
2135 } else if (strstart(p, "gtk", &opts)) {
2139 const char *nextopt;
2141 if (strstart(opts, ",grab_on_hover=", &nextopt)) {
2143 if (strstart(opts, "on", &nextopt)) {
2144 grab_on_hover = true;
2145 } else if (strstart(opts, "off", &nextopt)) {
2146 grab_on_hover = false;
2148 goto invalid_gtk_args;
2150 } else if (strstart(opts, ",gl=", &nextopt)) {
2152 if (strstart(opts, "on", &nextopt)) {
2154 } else if (strstart(opts, "off", &nextopt)) {
2157 goto invalid_gtk_args;
2161 error_report("invalid GTK option string");
2167 error_report("GTK support is disabled");
2170 } else if (strstart(p, "none", &opts)) {
2173 error_report("unknown display type");
2180 static int balloon_parse(const char *arg)
2184 if (strcmp(arg, "none") == 0) {
2188 if (!strncmp(arg, "virtio", 6)) {
2189 if (arg[6] == ',') {
2190 /* have params -> parse them */
2191 opts = qemu_opts_parse_noisily(qemu_find_opts("device"), arg + 7,
2196 /* create empty opts */
2197 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2200 qemu_opt_set(opts, "driver", "virtio-balloon", &error_abort);
2207 char *qemu_find_file(int type, const char *name)
2213 /* Try the name as a straight path first */
2214 if (access(name, R_OK) == 0) {
2215 trace_load_file(name, name);
2216 return g_strdup(name);
2220 case QEMU_FILE_TYPE_BIOS:
2223 case QEMU_FILE_TYPE_KEYMAP:
2224 subdir = "keymaps/";
2230 for (i = 0; i < data_dir_idx; i++) {
2231 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2232 if (access(buf, R_OK) == 0) {
2233 trace_load_file(name, buf);
2241 static inline bool nonempty_str(const char *str)
2246 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
2250 const char *name, *file, *str;
2251 FWCfgState *fw_cfg = (FWCfgState *) opaque;
2253 if (fw_cfg == NULL) {
2254 error_report("fw_cfg device not available");
2257 name = qemu_opt_get(opts, "name");
2258 file = qemu_opt_get(opts, "file");
2259 str = qemu_opt_get(opts, "string");
2261 /* we need name and either a file or the content string */
2262 if (!(nonempty_str(name) && (nonempty_str(file) || nonempty_str(str)))) {
2263 error_report("invalid argument(s)");
2266 if (nonempty_str(file) && nonempty_str(str)) {
2267 error_report("file and string are mutually exclusive");
2270 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
2271 error_report("name too long (max. %d char)", FW_CFG_MAX_FILE_PATH - 1);
2274 if (strncmp(name, "opt/", 4) != 0) {
2275 error_report("warning: externally provided fw_cfg item names "
2276 "should be prefixed with \"opt/\"");
2278 if (nonempty_str(str)) {
2279 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
2280 buf = g_memdup(str, size);
2282 if (!g_file_get_contents(file, &buf, &size, NULL)) {
2283 error_report("can't load %s", file);
2287 /* For legacy, keep user files in a specific global order. */
2288 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
2289 fw_cfg_add_file(fw_cfg, name, buf, size);
2290 fw_cfg_reset_order_override(fw_cfg);
2294 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
2296 return qdev_device_help(opts);
2299 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
2304 dev = qdev_device_add(opts, &err);
2306 error_report_err(err);
2309 object_unref(OBJECT(dev));
2313 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2315 Error *local_err = NULL;
2317 qemu_chr_new_from_opts(opts, &local_err);
2319 error_report_err(local_err);
2325 #ifdef CONFIG_VIRTFS
2326 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2328 return qemu_fsdev_add(opts);
2332 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
2335 const char *chardev;
2339 mode = qemu_opt_get(opts, "mode");
2343 if (strcmp(mode, "readline") == 0) {
2344 flags = MONITOR_USE_READLINE;
2345 } else if (strcmp(mode, "control") == 0) {
2346 flags = MONITOR_USE_CONTROL;
2348 error_report("unknown monitor mode \"%s\"", mode);
2352 if (qemu_opt_get_bool(opts, "pretty", 0))
2353 flags |= MONITOR_USE_PRETTY;
2355 if (qemu_opt_get_bool(opts, "default", 0)) {
2356 error_report("option 'default' does nothing and is deprecated");
2359 chardev = qemu_opt_get(opts, "chardev");
2360 chr = qemu_chr_find(chardev);
2362 error_report("chardev \"%s\" not found", chardev);
2366 monitor_init(chr, flags);
2370 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
2372 static int monitor_device_index = 0;
2377 if (strstart(optarg, "chardev:", &p)) {
2378 snprintf(label, sizeof(label), "%s", p);
2380 snprintf(label, sizeof(label), "compat_monitor%d",
2381 monitor_device_index);
2382 opts = qemu_chr_parse_compat(label, optarg);
2384 error_report("parse error: %s", optarg);
2389 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
2390 qemu_opt_set(opts, "mode", mode, &error_abort);
2391 qemu_opt_set(opts, "chardev", label, &error_abort);
2392 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
2393 monitor_device_index++;
2396 struct device_config {
2398 DEV_USB, /* -usbdevice */
2400 DEV_SERIAL, /* -serial */
2401 DEV_PARALLEL, /* -parallel */
2402 DEV_VIRTCON, /* -virtioconsole */
2403 DEV_DEBUGCON, /* -debugcon */
2404 DEV_GDB, /* -gdb, -s */
2405 DEV_SCLP, /* s390 sclp */
2407 const char *cmdline;
2409 QTAILQ_ENTRY(device_config) next;
2412 static QTAILQ_HEAD(, device_config) device_configs =
2413 QTAILQ_HEAD_INITIALIZER(device_configs);
2415 static void add_device_config(int type, const char *cmdline)
2417 struct device_config *conf;
2419 conf = g_malloc0(sizeof(*conf));
2421 conf->cmdline = cmdline;
2422 loc_save(&conf->loc);
2423 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2426 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2428 struct device_config *conf;
2431 QTAILQ_FOREACH(conf, &device_configs, next) {
2432 if (conf->type != type)
2434 loc_push_restore(&conf->loc);
2435 rc = func(conf->cmdline);
2436 loc_pop(&conf->loc);
2444 static int serial_parse(const char *devname)
2446 static int index = 0;
2449 if (strcmp(devname, "none") == 0)
2451 if (index == MAX_SERIAL_PORTS) {
2452 error_report("too many serial ports");
2455 snprintf(label, sizeof(label), "serial%d", index);
2456 serial_hds[index] = qemu_chr_new(label, devname);
2457 if (!serial_hds[index]) {
2458 error_report("could not connect serial device"
2459 " to character backend '%s'", devname);
2466 static int parallel_parse(const char *devname)
2468 static int index = 0;
2471 if (strcmp(devname, "none") == 0)
2473 if (index == MAX_PARALLEL_PORTS) {
2474 error_report("too many parallel ports");
2477 snprintf(label, sizeof(label), "parallel%d", index);
2478 parallel_hds[index] = qemu_chr_new(label, devname);
2479 if (!parallel_hds[index]) {
2480 error_report("could not connect parallel device"
2481 " to character backend '%s'", devname);
2488 static int virtcon_parse(const char *devname)
2490 QemuOptsList *device = qemu_find_opts("device");
2491 static int index = 0;
2493 QemuOpts *bus_opts, *dev_opts;
2495 if (strcmp(devname, "none") == 0)
2497 if (index == MAX_VIRTIO_CONSOLES) {
2498 error_report("too many virtio consoles");
2502 bus_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2503 qemu_opt_set(bus_opts, "driver", "virtio-serial", &error_abort);
2505 dev_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2506 qemu_opt_set(dev_opts, "driver", "virtconsole", &error_abort);
2508 snprintf(label, sizeof(label), "virtcon%d", index);
2509 virtcon_hds[index] = qemu_chr_new(label, devname);
2510 if (!virtcon_hds[index]) {
2511 error_report("could not connect virtio console"
2512 " to character backend '%s'", devname);
2515 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2521 static int sclp_parse(const char *devname)
2523 QemuOptsList *device = qemu_find_opts("device");
2524 static int index = 0;
2528 if (strcmp(devname, "none") == 0) {
2531 if (index == MAX_SCLP_CONSOLES) {
2532 error_report("too many sclp consoles");
2536 assert(arch_type == QEMU_ARCH_S390X);
2538 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2539 qemu_opt_set(dev_opts, "driver", "sclpconsole", &error_abort);
2541 snprintf(label, sizeof(label), "sclpcon%d", index);
2542 sclp_hds[index] = qemu_chr_new(label, devname);
2543 if (!sclp_hds[index]) {
2544 error_report("could not connect sclp console"
2545 " to character backend '%s'", devname);
2548 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2554 static int debugcon_parse(const char *devname)
2558 if (!qemu_chr_new("debugcon", devname)) {
2561 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2563 error_report("already have a debugcon device");
2566 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
2567 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
2571 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
2573 const MachineClass *mc1 = a, *mc2 = b;
2576 if (mc1->family == NULL) {
2577 if (mc2->family == NULL) {
2578 /* Compare standalone machine types against each other; they sort
2579 * in increasing order.
2581 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
2582 object_class_get_name(OBJECT_CLASS(mc2)));
2585 /* Standalone machine types sort after families. */
2589 if (mc2->family == NULL) {
2590 /* Families sort before standalone machine types. */
2594 /* Families sort between each other alphabetically increasingly. */
2595 res = strcmp(mc1->family, mc2->family);
2600 /* Within the same family, machine types sort in decreasing order. */
2601 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
2602 object_class_get_name(OBJECT_CLASS(mc1)));
2605 static MachineClass *machine_parse(const char *name)
2607 MachineClass *mc = NULL;
2608 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
2611 mc = find_machine(name);
2614 g_slist_free(machines);
2617 if (name && !is_help_option(name)) {
2618 error_report("unsupported machine type");
2619 error_printf("Use -machine help to list supported machines\n");
2621 printf("Supported machines are:\n");
2622 machines = g_slist_sort(machines, machine_class_cmp);
2623 for (el = machines; el; el = el->next) {
2624 MachineClass *mc = el->data;
2626 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
2628 printf("%-20s %s%s\n", mc->name, mc->desc,
2629 mc->is_default ? " (default)" : "");
2633 g_slist_free(machines);
2634 exit(!name || !is_help_option(name));
2637 void qemu_add_exit_notifier(Notifier *notify)
2639 notifier_list_add(&exit_notifiers, notify);
2642 void qemu_remove_exit_notifier(Notifier *notify)
2644 notifier_remove(notify);
2647 static void qemu_run_exit_notifiers(void)
2649 notifier_list_notify(&exit_notifiers, NULL);
2652 static bool machine_init_done;
2654 void qemu_add_machine_init_done_notifier(Notifier *notify)
2656 notifier_list_add(&machine_init_done_notifiers, notify);
2657 if (machine_init_done) {
2658 notify->notify(notify, NULL);
2662 void qemu_remove_machine_init_done_notifier(Notifier *notify)
2664 notifier_remove(notify);
2667 static void qemu_run_machine_init_done_notifiers(void)
2669 notifier_list_notify(&machine_init_done_notifiers, NULL);
2670 machine_init_done = true;
2673 static const QEMUOption *lookup_opt(int argc, char **argv,
2674 const char **poptarg, int *poptind)
2676 const QEMUOption *popt;
2677 int optind = *poptind;
2678 char *r = argv[optind];
2681 loc_set_cmdline(argv, optind, 1);
2683 /* Treat --foo the same as -foo. */
2686 popt = qemu_options;
2689 error_report("invalid option");
2692 if (!strcmp(popt->name, r + 1))
2696 if (popt->flags & HAS_ARG) {
2697 if (optind >= argc) {
2698 error_report("requires an argument");
2701 optarg = argv[optind++];
2702 loc_set_cmdline(argv, optind - 2, 2);
2713 static MachineClass *select_machine(void)
2715 MachineClass *machine_class = find_default_machine();
2720 loc_push_none(&loc);
2722 opts = qemu_get_machine_opts();
2723 qemu_opts_loc_restore(opts);
2725 optarg = qemu_opt_get(opts, "type");
2727 machine_class = machine_parse(optarg);
2730 if (!machine_class) {
2731 error_report("No machine specified, and there is no default");
2732 error_printf("Use -machine help to list supported machines\n");
2737 return machine_class;
2740 static int machine_set_property(void *opaque,
2741 const char *name, const char *value,
2744 Object *obj = OBJECT(opaque);
2745 Error *local_err = NULL;
2748 if (strcmp(name, "type") == 0) {
2752 qom_name = g_strdup(name);
2753 for (p = qom_name; *p; p++) {
2759 object_property_parse(obj, value, qom_name, &local_err);
2763 error_report_err(local_err);
2772 * Initial object creation happens before all other
2773 * QEMU data types are created. The majority of objects
2774 * can be created at this point. The rng-egd object
2775 * cannot be created here, as it depends on the chardev
2778 static bool object_create_initial(const char *type)
2780 if (g_str_equal(type, "rng-egd")) {
2785 * return false for concrete netfilters since
2786 * they depend on netdevs already existing
2788 if (g_str_equal(type, "filter-buffer") ||
2789 g_str_equal(type, "filter-dump") ||
2790 g_str_equal(type, "filter-mirror") ||
2791 g_str_equal(type, "filter-redirector") ||
2792 g_str_equal(type, "colo-compare") ||
2793 g_str_equal(type, "filter-rewriter") ||
2794 g_str_equal(type, "filter-replay")) {
2798 /* Memory allocation by backends needs to be done
2799 * after configure_accelerator() (due to the tcg_enabled()
2800 * checks at memory_region_init_*()).
2802 * Also, allocation of large amounts of memory may delay
2803 * chardev initialization for too long, and trigger timeouts
2804 * on software that waits for a monitor socket to be created
2807 if (g_str_has_prefix(type, "memory-backend-")) {
2816 * The remainder of object creation happens after the
2817 * creation of chardev, fsdev, net clients and device data types.
2819 static bool object_create_delayed(const char *type)
2821 return !object_create_initial(type);
2825 static void set_memory_options(uint64_t *ram_slots, ram_addr_t *maxram_size,
2829 const char *mem_str;
2830 const char *maxmem_str, *slots_str;
2831 const ram_addr_t default_ram_size = mc->default_ram_size;
2832 QemuOpts *opts = qemu_find_opts_singleton("memory");
2835 loc_push_none(&loc);
2836 qemu_opts_loc_restore(opts);
2839 mem_str = qemu_opt_get(opts, "size");
2842 error_report("missing 'size' option value");
2846 sz = qemu_opt_get_size(opts, "size", ram_size);
2848 /* Fix up legacy suffix-less format */
2849 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2850 uint64_t overflow_check = sz;
2853 if ((sz >> 20) != overflow_check) {
2854 error_report("too large 'size' option value");
2860 /* backward compatibility behaviour for case "-m 0" */
2862 sz = default_ram_size;
2865 sz = QEMU_ALIGN_UP(sz, 8192);
2867 if (ram_size != sz) {
2868 error_report("ram size too large");
2872 /* store value for the future use */
2873 qemu_opt_set_number(opts, "size", ram_size, &error_abort);
2874 *maxram_size = ram_size;
2876 maxmem_str = qemu_opt_get(opts, "maxmem");
2877 slots_str = qemu_opt_get(opts, "slots");
2878 if (maxmem_str && slots_str) {
2881 sz = qemu_opt_get_size(opts, "maxmem", 0);
2882 slots = qemu_opt_get_number(opts, "slots", 0);
2883 if (sz < ram_size) {
2884 error_report("invalid value of -m option maxmem: "
2885 "maximum memory size (0x%" PRIx64 ") must be at least "
2886 "the initial memory size (0x" RAM_ADDR_FMT ")",
2889 } else if (sz > ram_size) {
2891 error_report("invalid value of -m option: maxmem was "
2892 "specified, but no hotplug slots were specified");
2896 error_report("invalid value of -m option maxmem: "
2897 "memory slots were specified but maximum memory size "
2898 "(0x%" PRIx64 ") is equal to the initial memory size "
2899 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
2905 } else if ((!maxmem_str && slots_str) ||
2906 (maxmem_str && !slots_str)) {
2907 error_report("invalid -m option value: missing "
2908 "'%s' option", slots_str ? "maxmem" : "slots");
2915 static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
2919 g = g_malloc0(sizeof(*g));
2920 g->driver = qemu_opt_get(opts, "driver");
2921 g->property = qemu_opt_get(opts, "property");
2922 g->value = qemu_opt_get(opts, "value");
2923 g->user_provided = true;
2924 g->errp = &error_fatal;
2925 qdev_prop_register_global(g);
2929 static int qemu_read_default_config_file(void)
2933 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
2934 if (ret < 0 && ret != -ENOENT) {
2941 int main(int argc, char **argv, char **envp)
2944 int snapshot, linux_boot;
2945 const char *initrd_filename;
2946 const char *kernel_filename, *kernel_cmdline;
2947 const char *boot_order = NULL;
2948 const char *boot_once = NULL;
2950 int cyls, heads, secs, translation;
2951 QemuOpts *opts, *machine_opts;
2952 QemuOpts *hda_opts = NULL, *icount_opts = NULL, *accel_opts = NULL;
2953 QemuOptsList *olist;
2956 const char *loadvm = NULL;
2957 MachineClass *machine_class;
2958 const char *cpu_model;
2959 const char *vga_model = NULL;
2960 const char *qtest_chrdev = NULL;
2961 const char *qtest_log = NULL;
2962 const char *pid_file = NULL;
2963 const char *incoming = NULL;
2964 bool defconfig = true;
2965 bool userconfig = true;
2966 bool nographic = false;
2967 DisplayType display_type = DT_DEFAULT;
2968 int display_remote = 0;
2969 const char *log_mask = NULL;
2970 const char *log_file = NULL;
2971 char *trace_file = NULL;
2972 ram_addr_t maxram_size;
2973 uint64_t ram_slots = 0;
2974 FILE *vmstate_dump_file = NULL;
2975 Error *main_loop_err = NULL;
2977 bool list_data_dirs = false;
2979 module_call_init(MODULE_INIT_TRACE);
2981 qemu_init_cpu_list();
2982 qemu_init_cpu_loop();
2983 qemu_mutex_lock_iothread();
2985 atexit(qemu_run_exit_notifiers);
2986 error_set_progname(argv[0]);
2987 qemu_init_exec_dir(argv[0]);
2989 module_call_init(MODULE_INIT_QOM);
2990 module_call_init(MODULE_INIT_QAPI);
2992 qemu_add_opts(&qemu_drive_opts);
2993 qemu_add_drive_opts(&qemu_legacy_drive_opts);
2994 qemu_add_drive_opts(&qemu_common_drive_opts);
2995 qemu_add_drive_opts(&qemu_drive_opts);
2996 qemu_add_drive_opts(&bdrv_runtime_opts);
2997 qemu_add_opts(&qemu_chardev_opts);
2998 qemu_add_opts(&qemu_device_opts);
2999 qemu_add_opts(&qemu_netdev_opts);
3000 qemu_add_opts(&qemu_net_opts);
3001 qemu_add_opts(&qemu_rtc_opts);
3002 qemu_add_opts(&qemu_global_opts);
3003 qemu_add_opts(&qemu_mon_opts);
3004 qemu_add_opts(&qemu_trace_opts);
3005 qemu_add_opts(&qemu_option_rom_opts);
3006 qemu_add_opts(&qemu_machine_opts);
3007 qemu_add_opts(&qemu_accel_opts);
3008 qemu_add_opts(&qemu_mem_opts);
3009 qemu_add_opts(&qemu_smp_opts);
3010 qemu_add_opts(&qemu_boot_opts);
3011 qemu_add_opts(&qemu_sandbox_opts);
3012 qemu_add_opts(&qemu_add_fd_opts);
3013 qemu_add_opts(&qemu_object_opts);
3014 qemu_add_opts(&qemu_tpmdev_opts);
3015 qemu_add_opts(&qemu_realtime_opts);
3016 qemu_add_opts(&qemu_msg_opts);
3017 qemu_add_opts(&qemu_name_opts);
3018 qemu_add_opts(&qemu_numa_opts);
3019 qemu_add_opts(&qemu_icount_opts);
3020 qemu_add_opts(&qemu_semihosting_config_opts);
3021 qemu_add_opts(&qemu_fw_cfg_opts);
3022 module_call_init(MODULE_INIT_OPTS);
3026 if (qcrypto_init(&err) < 0) {
3027 error_reportf_err(err, "cannot initialize crypto: ");
3030 rtc_clock = QEMU_CLOCK_HOST;
3032 QLIST_INIT (&vm_change_state_head);
3033 os_setup_early_signal_handling();
3037 cyls = heads = secs = 0;
3038 translation = BIOS_ATA_TRANSLATION_AUTO;
3042 bdrv_init_with_whitelist();
3046 /* first pass of option parsing */
3048 while (optind < argc) {
3049 if (argv[optind][0] != '-') {
3053 const QEMUOption *popt;
3055 popt = lookup_opt(argc, argv, &optarg, &optind);
3056 switch (popt->index) {
3057 case QEMU_OPTION_nodefconfig:
3060 case QEMU_OPTION_nouserconfig:
3067 if (defconfig && userconfig) {
3068 if (qemu_read_default_config_file() < 0) {
3073 /* second pass of option parsing */
3078 if (argv[optind][0] != '-') {
3079 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
3081 const QEMUOption *popt;
3083 popt = lookup_opt(argc, argv, &optarg, &optind);
3084 if (!(popt->arch_mask & arch_type)) {
3085 error_report("Option not supported for this target");
3088 switch(popt->index) {
3089 case QEMU_OPTION_no_kvm_irqchip: {
3090 olist = qemu_find_opts("machine");
3091 qemu_opts_parse_noisily(olist, "kernel_irqchip=off", false);
3094 case QEMU_OPTION_cpu:
3095 /* hw initialization will check this */
3098 case QEMU_OPTION_hda:
3102 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
3104 snprintf(buf, sizeof(buf),
3105 "%s,cyls=%d,heads=%d,secs=%d%s",
3106 HD_OPTS , cyls, heads, secs,
3107 translation == BIOS_ATA_TRANSLATION_LBA ?
3109 translation == BIOS_ATA_TRANSLATION_NONE ?
3110 ",trans=none" : "");
3111 drive_add(IF_DEFAULT, 0, optarg, buf);
3114 case QEMU_OPTION_hdb:
3115 case QEMU_OPTION_hdc:
3116 case QEMU_OPTION_hdd:
3117 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
3120 case QEMU_OPTION_drive:
3121 if (drive_def(optarg) == NULL) {
3125 case QEMU_OPTION_set:
3126 if (qemu_set_option(optarg) != 0)
3129 case QEMU_OPTION_global:
3130 if (qemu_global_option(optarg) != 0)
3133 case QEMU_OPTION_mtdblock:
3134 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3136 case QEMU_OPTION_sd:
3137 drive_add(IF_SD, -1, optarg, SD_OPTS);
3139 case QEMU_OPTION_pflash:
3140 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
3142 case QEMU_OPTION_snapshot:
3145 case QEMU_OPTION_hdachs:
3149 cyls = strtol(p, (char **)&p, 0);
3150 if (cyls < 1 || cyls > 16383)
3155 heads = strtol(p, (char **)&p, 0);
3156 if (heads < 1 || heads > 16)
3161 secs = strtol(p, (char **)&p, 0);
3162 if (secs < 1 || secs > 63)
3166 if (!strcmp(p, "large")) {
3167 translation = BIOS_ATA_TRANSLATION_LARGE;
3168 } else if (!strcmp(p, "rechs")) {
3169 translation = BIOS_ATA_TRANSLATION_RECHS;
3170 } else if (!strcmp(p, "none")) {
3171 translation = BIOS_ATA_TRANSLATION_NONE;
3172 } else if (!strcmp(p, "lba")) {
3173 translation = BIOS_ATA_TRANSLATION_LBA;
3174 } else if (!strcmp(p, "auto")) {
3175 translation = BIOS_ATA_TRANSLATION_AUTO;
3179 } else if (*p != '\0') {
3181 error_report("invalid physical CHS format");
3184 if (hda_opts != NULL) {
3185 qemu_opt_set_number(hda_opts, "cyls", cyls,
3187 qemu_opt_set_number(hda_opts, "heads", heads,
3189 qemu_opt_set_number(hda_opts, "secs", secs,
3191 if (translation == BIOS_ATA_TRANSLATION_LARGE) {
3192 qemu_opt_set(hda_opts, "trans", "large",
3194 } else if (translation == BIOS_ATA_TRANSLATION_RECHS) {
3195 qemu_opt_set(hda_opts, "trans", "rechs",
3197 } else if (translation == BIOS_ATA_TRANSLATION_LBA) {
3198 qemu_opt_set(hda_opts, "trans", "lba",
3200 } else if (translation == BIOS_ATA_TRANSLATION_NONE) {
3201 qemu_opt_set(hda_opts, "trans", "none",
3207 case QEMU_OPTION_numa:
3208 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
3214 case QEMU_OPTION_display:
3215 display_type = select_display(optarg);
3217 case QEMU_OPTION_nographic:
3218 olist = qemu_find_opts("machine");
3219 qemu_opts_parse_noisily(olist, "graphics=off", false);
3221 display_type = DT_NONE;
3223 case QEMU_OPTION_curses:
3224 #ifdef CONFIG_CURSES
3225 display_type = DT_CURSES;
3227 error_report("curses support is disabled");
3231 case QEMU_OPTION_portrait:
3232 graphic_rotate = 90;
3234 case QEMU_OPTION_rotate:
3235 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3236 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3237 graphic_rotate != 180 && graphic_rotate != 270) {
3238 error_report("only 90, 180, 270 deg rotation is available");
3242 case QEMU_OPTION_kernel:
3243 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg,
3246 case QEMU_OPTION_initrd:
3247 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg,
3250 case QEMU_OPTION_append:
3251 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg,
3254 case QEMU_OPTION_dtb:
3255 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg,
3258 case QEMU_OPTION_cdrom:
3259 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3261 case QEMU_OPTION_boot:
3262 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
3268 case QEMU_OPTION_fda:
3269 case QEMU_OPTION_fdb:
3270 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3273 case QEMU_OPTION_no_fd_bootchk:
3276 case QEMU_OPTION_netdev:
3278 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3282 case QEMU_OPTION_net:
3284 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3288 #ifdef CONFIG_LIBISCSI
3289 case QEMU_OPTION_iscsi:
3290 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
3298 case QEMU_OPTION_tftp:
3299 error_report("The -tftp option is deprecated. "
3300 "Please use '-netdev user,tftp=...' instead.");
3301 legacy_tftp_prefix = optarg;
3303 case QEMU_OPTION_bootp:
3304 error_report("The -bootp option is deprecated. "
3305 "Please use '-netdev user,bootfile=...' instead.");
3306 legacy_bootp_filename = optarg;
3308 case QEMU_OPTION_redir:
3309 error_report("The -redir option is deprecated. "
3310 "Please use '-netdev user,hostfwd=...' instead.");
3311 if (net_slirp_redir(optarg) < 0)
3315 case QEMU_OPTION_bt:
3316 add_device_config(DEV_BT, optarg);
3318 case QEMU_OPTION_audio_help:
3322 case QEMU_OPTION_soundhw:
3323 select_soundhw (optarg);
3328 case QEMU_OPTION_version:
3333 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
3340 case QEMU_OPTION_tpmdev:
3341 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3346 case QEMU_OPTION_mempath:
3349 case QEMU_OPTION_mem_prealloc:
3358 case QEMU_OPTION_DFILTER:
3359 qemu_set_dfilter_ranges(optarg, &error_fatal);
3362 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3364 case QEMU_OPTION_gdb:
3365 add_device_config(DEV_GDB, optarg);
3368 if (is_help_option(optarg)) {
3369 list_data_dirs = true;
3370 } else if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3371 data_dir[data_dir_idx++] = optarg;
3374 case QEMU_OPTION_bios:
3375 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg,
3378 case QEMU_OPTION_singlestep:
3385 keyboard_layout = optarg;
3387 case QEMU_OPTION_localtime:
3390 case QEMU_OPTION_vga:
3399 w = strtol(p, (char **)&p, 10);
3402 error_report("invalid resolution or depth");
3408 h = strtol(p, (char **)&p, 10);
3413 depth = strtol(p, (char **)&p, 10);
3414 if (depth != 8 && depth != 15 && depth != 16 &&
3415 depth != 24 && depth != 32)
3417 } else if (*p == '\0') {
3418 depth = graphic_depth;
3425 graphic_depth = depth;
3428 case QEMU_OPTION_echr:
3431 term_escape_char = strtol(optarg, &r, 0);
3433 printf("Bad argument to echr\n");
3436 case QEMU_OPTION_monitor:
3437 default_monitor = 0;
3438 if (strncmp(optarg, "none", 4)) {
3439 monitor_parse(optarg, "readline", false);
3442 case QEMU_OPTION_qmp:
3443 monitor_parse(optarg, "control", false);
3444 default_monitor = 0;
3446 case QEMU_OPTION_qmp_pretty:
3447 monitor_parse(optarg, "control", true);
3448 default_monitor = 0;
3450 case QEMU_OPTION_mon:
3451 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3456 default_monitor = 0;
3458 case QEMU_OPTION_chardev:
3459 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3465 case QEMU_OPTION_fsdev:
3466 olist = qemu_find_opts("fsdev");
3468 error_report("fsdev support is disabled");
3471 opts = qemu_opts_parse_noisily(olist, optarg, true);
3476 case QEMU_OPTION_virtfs: {
3479 const char *writeout, *sock_fd, *socket;
3481 olist = qemu_find_opts("virtfs");
3483 error_report("virtfs support is disabled");
3486 opts = qemu_opts_parse_noisily(olist, optarg, true);
3491 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3492 qemu_opt_get(opts, "mount_tag") == NULL) {
3493 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3496 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3497 qemu_opt_get(opts, "mount_tag"),
3500 error_report("duplicate fsdev id: %s",
3501 qemu_opt_get(opts, "mount_tag"));
3505 writeout = qemu_opt_get(opts, "writeout");
3507 #ifdef CONFIG_SYNC_FILE_RANGE
3508 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3510 error_report("writeout=immediate not supported "
3511 "on this platform");
3515 qemu_opt_set(fsdev, "fsdriver",
3516 qemu_opt_get(opts, "fsdriver"), &error_abort);
3517 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"),
3519 qemu_opt_set(fsdev, "security_model",
3520 qemu_opt_get(opts, "security_model"),
3522 socket = qemu_opt_get(opts, "socket");
3524 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3526 sock_fd = qemu_opt_get(opts, "sock_fd");
3528 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3531 qemu_opt_set_bool(fsdev, "readonly",
3532 qemu_opt_get_bool(opts, "readonly", 0),
3534 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3536 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3537 qemu_opt_set(device, "fsdev",
3538 qemu_opt_get(opts, "mount_tag"), &error_abort);
3539 qemu_opt_set(device, "mount_tag",
3540 qemu_opt_get(opts, "mount_tag"), &error_abort);
3543 case QEMU_OPTION_virtfs_synth: {
3547 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3550 error_report("duplicate option: %s", "virtfs_synth");
3553 qemu_opt_set(fsdev, "fsdriver", "synth", &error_abort);
3555 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3557 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3558 qemu_opt_set(device, "fsdev", "v_synth", &error_abort);
3559 qemu_opt_set(device, "mount_tag", "v_synth", &error_abort);
3562 case QEMU_OPTION_serial:
3563 add_device_config(DEV_SERIAL, optarg);
3565 if (strncmp(optarg, "mon:", 4) == 0) {
3566 default_monitor = 0;
3569 case QEMU_OPTION_watchdog:
3571 error_report("only one watchdog option may be given");
3576 case QEMU_OPTION_watchdog_action:
3577 if (select_watchdog_action(optarg) == -1) {
3578 error_report("unknown -watchdog-action parameter");
3582 case QEMU_OPTION_virtiocon:
3583 add_device_config(DEV_VIRTCON, optarg);
3584 default_virtcon = 0;
3585 if (strncmp(optarg, "mon:", 4) == 0) {
3586 default_monitor = 0;
3589 case QEMU_OPTION_parallel:
3590 add_device_config(DEV_PARALLEL, optarg);
3591 default_parallel = 0;
3592 if (strncmp(optarg, "mon:", 4) == 0) {
3593 default_monitor = 0;
3596 case QEMU_OPTION_debugcon:
3597 add_device_config(DEV_DEBUGCON, optarg);
3599 case QEMU_OPTION_loadvm:
3602 case QEMU_OPTION_full_screen:
3605 case QEMU_OPTION_no_frame:
3608 case QEMU_OPTION_alt_grab:
3611 case QEMU_OPTION_ctrl_grab:
3614 case QEMU_OPTION_no_quit:
3617 case QEMU_OPTION_sdl:
3619 display_type = DT_SDL;
3622 error_report("SDL support is disabled");
3625 case QEMU_OPTION_pidfile:
3628 case QEMU_OPTION_win2k_hack:
3629 win2k_install_hack = 1;
3631 case QEMU_OPTION_rtc_td_hack: {
3632 static GlobalProperty slew_lost_ticks = {
3633 .driver = "mc146818rtc",
3634 .property = "lost_tick_policy",
3638 qdev_prop_register_global(&slew_lost_ticks);
3641 case QEMU_OPTION_acpitable:
3642 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3647 acpi_table_add(opts, &error_fatal);
3649 case QEMU_OPTION_smbios:
3650 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3655 smbios_entry_add(opts, &error_fatal);
3657 case QEMU_OPTION_fwcfg:
3658 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3664 case QEMU_OPTION_enable_kvm:
3665 olist = qemu_find_opts("machine");
3666 qemu_opts_parse_noisily(olist, "accel=kvm", false);
3668 case QEMU_OPTION_enable_hax:
3669 olist = qemu_find_opts("machine");
3670 qemu_opts_parse_noisily(olist, "accel=hax", false);
3673 case QEMU_OPTION_machine:
3674 olist = qemu_find_opts("machine");
3675 opts = qemu_opts_parse_noisily(olist, optarg, true);
3680 case QEMU_OPTION_no_kvm:
3681 olist = qemu_find_opts("machine");
3682 qemu_opts_parse_noisily(olist, "accel=tcg", false);
3684 case QEMU_OPTION_no_kvm_pit: {
3685 error_report("warning: ignoring deprecated option");
3688 case QEMU_OPTION_no_kvm_pit_reinjection: {
3689 static GlobalProperty kvm_pit_lost_tick_policy = {
3690 .driver = "kvm-pit",
3691 .property = "lost_tick_policy",
3695 error_report("warning: deprecated, replaced by "
3696 "-global kvm-pit.lost_tick_policy=discard");
3697 qdev_prop_register_global(&kvm_pit_lost_tick_policy);
3700 case QEMU_OPTION_accel:
3701 accel_opts = qemu_opts_parse_noisily(qemu_find_opts("accel"),
3703 optarg = qemu_opt_get(accel_opts, "accel");
3705 olist = qemu_find_opts("machine");
3706 if (strcmp("kvm", optarg) == 0) {
3707 qemu_opts_parse_noisily(olist, "accel=kvm", false);
3708 } else if (strcmp("xen", optarg) == 0) {
3709 qemu_opts_parse_noisily(olist, "accel=xen", false);
3710 } else if (strcmp("tcg", optarg) == 0) {
3711 qemu_opts_parse_noisily(olist, "accel=tcg", false);
3713 if (!is_help_option(optarg)) {
3714 error_printf("Unknown accelerator: %s", optarg);
3716 error_printf("Supported accelerators: kvm, xen, tcg\n");
3720 case QEMU_OPTION_usb:
3721 olist = qemu_find_opts("machine");
3722 qemu_opts_parse_noisily(olist, "usb=on", false);
3724 case QEMU_OPTION_usbdevice:
3725 olist = qemu_find_opts("machine");
3726 qemu_opts_parse_noisily(olist, "usb=on", false);
3727 add_device_config(DEV_USB, optarg);
3729 case QEMU_OPTION_device:
3730 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3735 case QEMU_OPTION_smp:
3736 if (!qemu_opts_parse_noisily(qemu_find_opts("smp-opts"),
3741 case QEMU_OPTION_vnc:
3742 vnc_parse(optarg, &error_fatal);
3744 case QEMU_OPTION_no_acpi:
3747 case QEMU_OPTION_no_hpet:
3750 case QEMU_OPTION_balloon:
3751 if (balloon_parse(optarg) < 0) {
3752 error_report("unknown -balloon argument %s", optarg);
3756 case QEMU_OPTION_no_reboot:
3759 case QEMU_OPTION_no_shutdown:
3762 case QEMU_OPTION_show_cursor:
3765 case QEMU_OPTION_uuid:
3766 if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
3767 error_report("failed to parse UUID string: wrong format");
3770 qemu_uuid_set = true;
3772 case QEMU_OPTION_option_rom:
3773 if (nb_option_roms >= MAX_OPTION_ROMS) {
3774 error_report("too many option ROMs");
3777 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3782 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3783 option_rom[nb_option_roms].bootindex =
3784 qemu_opt_get_number(opts, "bootindex", -1);
3785 if (!option_rom[nb_option_roms].name) {
3786 error_report("Option ROM file is not specified");
3791 case QEMU_OPTION_semihosting:
3792 semihosting.enabled = true;
3793 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3795 case QEMU_OPTION_semihosting_config:
3796 semihosting.enabled = true;
3797 opts = qemu_opts_parse_noisily(qemu_find_opts("semihosting-config"),
3800 semihosting.enabled = qemu_opt_get_bool(opts, "enable",
3802 const char *target = qemu_opt_get(opts, "target");
3803 if (target != NULL) {
3804 if (strcmp("native", target) == 0) {
3805 semihosting.target = SEMIHOSTING_TARGET_NATIVE;
3806 } else if (strcmp("gdb", target) == 0) {
3807 semihosting.target = SEMIHOSTING_TARGET_GDB;
3808 } else if (strcmp("auto", target) == 0) {
3809 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3811 error_report("unsupported semihosting-config %s",
3816 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3818 /* Set semihosting argument count and vector */
3819 qemu_opt_foreach(opts, add_semihosting_arg,
3820 &semihosting, NULL);
3822 error_report("unsupported semihosting-config %s", optarg);
3826 case QEMU_OPTION_tdf:
3827 error_report("warning: ignoring deprecated option");
3829 case QEMU_OPTION_name:
3830 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3836 case QEMU_OPTION_prom_env:
3837 if (nb_prom_envs >= MAX_PROM_ENVS) {
3838 error_report("too many prom variables");
3841 prom_envs[nb_prom_envs] = optarg;
3844 case QEMU_OPTION_old_param:
3847 case QEMU_OPTION_clock:
3848 /* Clock options no longer exist. Keep this option for
3849 * backward compatibility.
3852 case QEMU_OPTION_startdate:
3853 configure_rtc_date_offset(optarg, 1);
3855 case QEMU_OPTION_rtc:
3856 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3861 configure_rtc(opts);
3863 case QEMU_OPTION_tb_size:
3864 tcg_tb_size = strtol(optarg, NULL, 0);
3865 if (tcg_tb_size < 0) {
3869 case QEMU_OPTION_icount:
3870 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3876 case QEMU_OPTION_incoming:
3878 runstate_set(RUN_STATE_INMIGRATE);
3882 case QEMU_OPTION_only_migratable:
3883 only_migratable = 1;
3885 case QEMU_OPTION_nodefaults:
3888 case QEMU_OPTION_xen_domid:
3889 if (!(xen_available())) {
3890 error_report("Option not supported for this target");
3893 xen_domid = atoi(optarg);
3895 case QEMU_OPTION_xen_create:
3896 if (!(xen_available())) {
3897 error_report("Option not supported for this target");
3900 xen_mode = XEN_CREATE;
3902 case QEMU_OPTION_xen_attach:
3903 if (!(xen_available())) {
3904 error_report("Option not supported for this target");
3907 xen_mode = XEN_ATTACH;
3909 case QEMU_OPTION_trace:
3911 trace_file = trace_opt_parse(optarg);
3913 case QEMU_OPTION_readconfig:
3915 int ret = qemu_read_config_file(optarg);
3917 error_report("read config %s: %s", optarg,
3923 case QEMU_OPTION_spice:
3924 olist = qemu_find_opts("spice");
3926 error_report("spice support is disabled");
3929 opts = qemu_opts_parse_noisily(olist, optarg, false);
3935 case QEMU_OPTION_writeconfig:
3938 if (strcmp(optarg, "-") == 0) {
3941 fp = fopen(optarg, "w");
3943 error_report("open %s: %s", optarg,
3948 qemu_config_write(fp);
3954 case QEMU_OPTION_qtest:
3955 qtest_chrdev = optarg;
3957 case QEMU_OPTION_qtest_log:
3960 case QEMU_OPTION_sandbox:
3961 opts = qemu_opts_parse_noisily(qemu_find_opts("sandbox"),
3967 case QEMU_OPTION_add_fd:
3969 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
3975 error_report("File descriptor passing is disabled on this "
3980 case QEMU_OPTION_object:
3981 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
3987 case QEMU_OPTION_realtime:
3988 opts = qemu_opts_parse_noisily(qemu_find_opts("realtime"),
3993 enable_mlock = qemu_opt_get_bool(opts, "mlock", true);
3995 case QEMU_OPTION_msg:
3996 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
4001 configure_msg(opts);
4003 case QEMU_OPTION_dump_vmstate:
4004 if (vmstate_dump_file) {
4005 error_report("only one '-dump-vmstate' "
4006 "option may be given");
4009 vmstate_dump_file = fopen(optarg, "w");
4010 if (vmstate_dump_file == NULL) {
4011 error_report("open %s: %s", optarg, strerror(errno));
4016 os_parse_cmd_args(popt->index, optarg);
4021 * Clear error location left behind by the loop.
4022 * Best done right after the loop. Do not insert code here!
4026 replay_configure(icount_opts);
4028 qemu_tcg_configure(accel_opts, &error_fatal);
4030 machine_class = select_machine();
4032 set_memory_options(&ram_slots, &maxram_size, machine_class);
4036 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
4037 error_report("could not acquire pid file: %s", strerror(errno));
4041 if (qemu_init_main_loop(&main_loop_err)) {
4042 error_report_err(main_loop_err);
4046 if (qemu_opts_foreach(qemu_find_opts("sandbox"),
4047 parse_sandbox, NULL, NULL)) {
4051 if (qemu_opts_foreach(qemu_find_opts("name"),
4052 parse_name, NULL, NULL)) {
4057 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4058 parse_add_fd, NULL, NULL)) {
4062 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4063 cleanup_add_fd, NULL, NULL)) {
4068 current_machine = MACHINE(object_new(object_class_get_name(
4069 OBJECT_CLASS(machine_class))));
4070 if (machine_help_func(qemu_get_machine_opts(), current_machine)) {
4073 object_property_add_child(object_get_root(), "machine",
4074 OBJECT(current_machine), &error_abort);
4076 if (machine_class->minimum_page_bits) {
4077 if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
4078 /* This would be a board error: specifying a minimum smaller than
4079 * a target's compile-time fixed setting.
4081 g_assert_not_reached();
4085 cpu_exec_init_all();
4087 if (machine_class->hw_version) {
4088 qemu_set_hw_version(machine_class->hw_version);
4091 if (cpu_model && is_help_option(cpu_model)) {
4092 list_cpus(stdout, &fprintf, cpu_model);
4096 if (!trace_init_backends()) {
4099 trace_init_file(trace_file);
4101 /* Open the logfile at this point and set the log mask if necessary.
4104 qemu_set_log_filename(log_file, &error_fatal);
4109 mask = qemu_str_to_log_mask(log_mask);
4111 qemu_print_log_usage(stdout);
4119 /* If no data_dir is specified then try to find it relative to the
4121 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4122 data_dir[data_dir_idx] = os_find_datadir();
4123 if (data_dir[data_dir_idx] != NULL) {
4127 /* If all else fails use the install path specified when building. */
4128 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4129 data_dir[data_dir_idx++] = CONFIG_QEMU_DATADIR;
4132 /* -L help lists the data directories and exits. */
4133 if (list_data_dirs) {
4134 for (i = 0; i < data_dir_idx; i++) {
4135 printf("%s\n", data_dir[i]);
4140 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
4142 machine_class->max_cpus = machine_class->max_cpus ?: 1; /* Default to UP */
4143 if (max_cpus > machine_class->max_cpus) {
4144 error_report("Number of SMP CPUs requested (%d) exceeds max CPUs "
4145 "supported by machine '%s' (%d)", max_cpus,
4146 machine_class->name, machine_class->max_cpus);
4151 * Get the default machine options from the machine if it is not already
4152 * specified either by the configuration file or by the command line.
4154 if (machine_class->default_machine_opts) {
4155 qemu_opts_set_defaults(qemu_find_opts("machine"),
4156 machine_class->default_machine_opts, 0);
4159 qemu_opts_foreach(qemu_find_opts("device"),
4160 default_driver_check, NULL, NULL);
4161 qemu_opts_foreach(qemu_find_opts("global"),
4162 default_driver_check, NULL, NULL);
4164 if (!vga_model && !default_vga) {
4165 vga_interface_type = VGA_DEVICE;
4167 if (!has_defaults || machine_class->no_serial) {
4170 if (!has_defaults || machine_class->no_parallel) {
4171 default_parallel = 0;
4173 if (!has_defaults || !machine_class->use_virtcon) {
4174 default_virtcon = 0;
4176 if (!has_defaults || !machine_class->use_sclp) {
4179 if (!has_defaults || machine_class->no_floppy) {
4182 if (!has_defaults || machine_class->no_cdrom) {
4185 if (!has_defaults || machine_class->no_sdcard) {
4188 if (!has_defaults) {
4189 default_monitor = 0;
4194 if (is_daemonized()) {
4195 /* According to documentation and historically, -nographic redirects
4196 * serial port, parallel port and monitor to stdio, which does not work
4197 * with -daemonize. We can redirect these to null instead, but since
4198 * -nographic is legacy, let's just error out.
4199 * We disallow -nographic only if all other ports are not redirected
4200 * explicitly, to not break existing legacy setups which uses
4201 * -nographic _and_ redirects all ports explicitly - this is valid
4202 * usage, -nographic is just a no-op in this case.
4205 && (default_parallel || default_serial
4206 || default_monitor || default_virtcon)) {
4207 error_report("-nographic cannot be used with -daemonize");
4210 #ifdef CONFIG_CURSES
4211 if (display_type == DT_CURSES) {
4212 error_report("curses display cannot be used with -daemonize");
4219 if (default_parallel)
4220 add_device_config(DEV_PARALLEL, "null");
4221 if (default_serial && default_monitor) {
4222 add_device_config(DEV_SERIAL, "mon:stdio");
4223 } else if (default_virtcon && default_monitor) {
4224 add_device_config(DEV_VIRTCON, "mon:stdio");
4225 } else if (default_sclp && default_monitor) {
4226 add_device_config(DEV_SCLP, "mon:stdio");
4229 add_device_config(DEV_SERIAL, "stdio");
4230 if (default_virtcon)
4231 add_device_config(DEV_VIRTCON, "stdio");
4233 add_device_config(DEV_SCLP, "stdio");
4235 if (default_monitor)
4236 monitor_parse("stdio", "readline", false);
4240 add_device_config(DEV_SERIAL, "vc:80Cx24C");
4241 if (default_parallel)
4242 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
4243 if (default_monitor)
4244 monitor_parse("vc:80Cx24C", "readline", false);
4245 if (default_virtcon)
4246 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
4248 add_device_config(DEV_SCLP, "vc:80Cx24C");
4252 #if defined(CONFIG_VNC)
4253 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
4257 if (display_type == DT_DEFAULT && !display_remote) {
4258 #if defined(CONFIG_GTK)
4259 display_type = DT_GTK;
4260 #elif defined(CONFIG_SDL)
4261 display_type = DT_SDL;
4262 #elif defined(CONFIG_COCOA)
4263 display_type = DT_COCOA;
4264 #elif defined(CONFIG_VNC)
4265 vnc_parse("localhost:0,to=99,id=default", &error_abort);
4267 display_type = DT_NONE;
4271 if ((no_frame || alt_grab || ctrl_grab) && display_type != DT_SDL) {
4272 error_report("-no-frame, -alt-grab and -ctrl-grab are only valid "
4273 "for SDL, ignoring option");
4275 if (no_quit && (display_type != DT_GTK && display_type != DT_SDL)) {
4276 error_report("-no-quit is only valid for GTK and SDL, "
4280 if (display_type == DT_GTK) {
4281 early_gtk_display_init(request_opengl);
4284 if (display_type == DT_SDL) {
4285 sdl_display_early_init(request_opengl);
4288 qemu_console_early_init();
4290 if (request_opengl == 1 && display_opengl == 0) {
4291 #if defined(CONFIG_OPENGL)
4292 error_report("OpenGL is not supported by the display");
4294 error_report("OpenGL support is disabled");
4302 if (qemu_opts_foreach(qemu_find_opts("object"),
4303 user_creatable_add_opts_foreach,
4304 object_create_initial, NULL)) {
4308 if (qemu_opts_foreach(qemu_find_opts("chardev"),
4309 chardev_init_func, NULL, NULL)) {
4313 #ifdef CONFIG_VIRTFS
4314 if (qemu_opts_foreach(qemu_find_opts("fsdev"),
4315 fsdev_init_func, NULL, NULL)) {
4320 if (qemu_opts_foreach(qemu_find_opts("device"),
4321 device_help_func, NULL, NULL)) {
4325 machine_opts = qemu_get_machine_opts();
4326 if (qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
4328 object_unref(OBJECT(current_machine));
4332 configure_accelerator(current_machine);
4335 qtest_init(qtest_chrdev, qtest_log, &error_fatal);
4338 machine_opts = qemu_get_machine_opts();
4339 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4340 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4341 kernel_cmdline = qemu_opt_get(machine_opts, "append");
4342 bios_name = qemu_opt_get(machine_opts, "firmware");
4344 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4346 boot_order = qemu_opt_get(opts, "order");
4348 validate_bootdevices(boot_order, &error_fatal);
4351 boot_once = qemu_opt_get(opts, "once");
4353 validate_bootdevices(boot_once, &error_fatal);
4356 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
4357 boot_strict = qemu_opt_get_bool(opts, "strict", false);
4361 boot_order = machine_class->default_boot_order;
4364 if (!kernel_cmdline) {
4365 kernel_cmdline = "";
4366 current_machine->kernel_cmdline = (char *)kernel_cmdline;
4369 linux_boot = (kernel_filename != NULL);
4371 if (!linux_boot && *kernel_cmdline != '\0') {
4372 error_report("-append only allowed with -kernel option");
4376 if (!linux_boot && initrd_filename != NULL) {
4377 error_report("-initrd only allowed with -kernel option");
4381 if (semihosting_enabled() && !semihosting_get_argc() && kernel_filename) {
4382 /* fall back to the -kernel/-append */
4383 semihosting_arg_fallback(kernel_filename, kernel_cmdline);
4386 os_set_line_buffering();
4388 /* spice needs the timers to be initialized by this point */
4393 if (!tcg_enabled()) {
4394 error_report("-icount is not allowed with hardware virtualization");
4396 } else if (qemu_tcg_mttcg_enabled()) {
4397 error_report("-icount does not currently work with MTTCG");
4400 configure_icount(icount_opts, &error_abort);
4401 qemu_opts_del(icount_opts);
4405 QemuOptsList *net = qemu_find_opts("net");
4406 qemu_opts_set(net, NULL, "type", "nic", &error_abort);
4408 qemu_opts_set(net, NULL, "type", "user", &error_abort);
4414 if (net_init_clients() < 0) {
4418 if (qemu_opts_foreach(qemu_find_opts("object"),
4419 user_creatable_add_opts_foreach,
4420 object_create_delayed, NULL)) {
4425 if (tpm_init() < 0) {
4430 /* init the bluetooth world */
4431 if (foreach_device_config(DEV_BT, bt_parse))
4434 if (!xen_enabled()) {
4435 /* On 32-bit hosts, QEMU is limited by virtual address space */
4436 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4437 error_report("at most 2047 MB RAM can be simulated");
4445 /* If the currently selected machine wishes to override the units-per-bus
4446 * property of its default HBA interface type, do so now. */
4447 if (machine_class->units_per_default_bus) {
4448 override_max_devs(machine_class->block_default_type,
4449 machine_class->units_per_default_bus);
4452 /* open the virtual block devices */
4453 if (snapshot || replay_mode != REPLAY_MODE_NONE) {
4454 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
4457 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
4458 &machine_class->block_default_type, NULL)) {
4462 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
4464 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4465 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
4467 parse_numa_opts(machine_class);
4469 if (qemu_opts_foreach(qemu_find_opts("mon"),
4470 mon_init_func, NULL, NULL)) {
4474 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4476 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4478 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4480 if (foreach_device_config(DEV_SCLP, sclp_parse) < 0) {
4483 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4486 /* If no default VGA is requested, the default is "none". */
4488 if (machine_class->default_display) {
4489 vga_model = machine_class->default_display;
4490 } else if (vga_interface_available(VGA_CIRRUS)) {
4491 vga_model = "cirrus";
4492 } else if (vga_interface_available(VGA_STD)) {
4497 select_vgahw(vga_model);
4501 i = select_watchdog(watchdog);
4503 exit (i == 1 ? 1 : 0);
4506 machine_register_compat_props(current_machine);
4508 qemu_opts_foreach(qemu_find_opts("global"),
4509 global_init_func, NULL, NULL);
4511 /* This checkpoint is required by replay to separate prior clock
4512 reading from the other reads, because timer polling functions query
4513 clock values from the log. */
4514 replay_checkpoint(CHECKPOINT_INIT);
4515 qdev_machine_init();
4517 current_machine->ram_size = ram_size;
4518 current_machine->maxram_size = maxram_size;
4519 current_machine->ram_slots = ram_slots;
4520 current_machine->boot_order = boot_order;
4521 current_machine->cpu_model = cpu_model;
4523 machine_class->init(current_machine);
4529 if (hax_enabled()) {
4533 if (qemu_opts_foreach(qemu_find_opts("fw_cfg"),
4534 parse_fw_cfg, fw_cfg_find(), NULL) != 0) {
4538 /* init USB devices */
4539 if (machine_usb(current_machine)) {
4540 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4544 /* Check if IGD GFX passthrough. */
4547 /* init generic devices */
4548 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
4549 if (qemu_opts_foreach(qemu_find_opts("device"),
4550 device_init_func, NULL, NULL)) {
4554 cpu_synchronize_all_post_init();
4556 numa_post_machine_init();
4558 rom_reset_order_override();
4561 * Create frontends for -drive if=scsi leftovers.
4562 * Normally, frontends for -drive get created by machine
4563 * initialization for onboard SCSI HBAs. However, we create a few
4564 * more ever since SCSI qdevification, but this is pretty much an
4565 * implementation accident, and deprecated.
4567 scsi_legacy_handle_cmdline();
4569 /* Did we create any drives that we failed to create a device for? */
4570 drive_check_orphaned();
4572 /* Don't warn about the default network setup that you get if
4573 * no command line -net or -netdev options are specified. There
4574 * are two cases that we would otherwise complain about:
4575 * (1) board doesn't support a NIC but the implicit "-net nic"
4577 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
4578 * sets up a nic that isn't connected to anything.
4581 net_check_clients();
4586 qemu_boot_set(boot_once, &error_fatal);
4587 qemu_register_reset(restore_boot_order, g_strdup(boot_order));
4590 ds = init_displaystate();
4592 /* init local displays */
4593 switch (display_type) {
4595 curses_display_init(ds, full_screen);
4598 sdl_display_init(ds, full_screen, no_frame);
4601 cocoa_display_init(ds, full_screen);
4604 gtk_display_init(ds, full_screen, grab_on_hover);
4610 /* must be after terminal init, SDL library changes signal handlers */
4611 os_setup_signal_handling();
4613 /* init remote displays */
4615 qemu_opts_foreach(qemu_find_opts("vnc"),
4616 vnc_init_func, NULL, NULL);
4620 qemu_spice_display_init();
4623 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
4627 qdev_machine_creation_done();
4629 /* TODO: once all bus devices are qdevified, this should be done
4630 * when bus is created by qdev.c */
4631 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4632 qemu_run_machine_init_done_notifiers();
4634 if (rom_check_and_register_reset() != 0) {
4635 error_report("rom check and register reset failed");
4641 /* This checkpoint is required by replay to separate prior clock
4642 reading from the other reads, because timer polling functions query
4643 clock values from the log. */
4644 replay_checkpoint(CHECKPOINT_RESET);
4645 qemu_system_reset(VMRESET_SILENT);
4646 register_global_state();
4647 if (replay_mode != REPLAY_MODE_NONE) {
4648 replay_vmstate_init();
4649 } else if (loadvm) {
4650 if (load_vmstate(loadvm) < 0) {
4655 qdev_prop_check_globals();
4656 if (vmstate_dump_file) {
4658 dump_vmstate_json_to_file(vmstate_dump_file);
4663 Error *local_err = NULL;
4664 qemu_start_incoming_migration(incoming, &local_err);
4666 error_reportf_err(local_err, "-incoming %s: ", incoming);
4669 } else if (autostart) {
4676 replay_disable_events();
4677 iothread_stop_all();
4683 /* vhost-user must be cleaned up before chardevs. */