4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "qemu/osdep.h"
26 #include "qapi/error.h"
27 #include "qemu-version.h"
28 #include "qemu/cutils.h"
29 #include "qemu/help_option.h"
30 #include "qemu/uuid.h"
31 #include "sysemu/seccomp.h"
34 #if defined(__APPLE__) || defined(main)
36 int qemu_main(int argc, char **argv, char **envp);
37 int main(int argc, char **argv)
39 return qemu_main(argc, argv, NULL);
42 #define main qemu_main
44 #endif /* CONFIG_SDL */
48 #define main qemu_main
49 #endif /* CONFIG_COCOA */
52 #include "qemu/error-report.h"
53 #include "qemu/sockets.h"
55 #include "hw/boards.h"
56 #include "sysemu/accel.h"
58 #include "hw/isa/isa.h"
59 #include "hw/scsi/scsi.h"
60 #include "hw/display/vga.h"
62 #include "sysemu/watchdog.h"
63 #include "hw/smbios/smbios.h"
64 #include "hw/acpi/acpi.h"
65 #include "hw/xen/xen.h"
67 #include "hw/loader.h"
68 #include "monitor/qdev.h"
69 #include "sysemu/bt.h"
71 #include "net/slirp.h"
72 #include "monitor/monitor.h"
73 #include "ui/console.h"
75 #include "sysemu/sysemu.h"
76 #include "sysemu/numa.h"
77 #include "exec/gdbstub.h"
78 #include "qemu/timer.h"
79 #include "chardev/char.h"
80 #include "qemu/bitmap.h"
82 #include "sysemu/blockdev.h"
83 #include "hw/block/block.h"
84 #include "migration/misc.h"
85 #include "migration/snapshot.h"
86 #include "migration/global_state.h"
87 #include "sysemu/tpm.h"
88 #include "sysemu/dma.h"
89 #include "hw/audio/soundhw.h"
90 #include "audio/audio.h"
91 #include "sysemu/cpus.h"
92 #include "migration/colo.h"
93 #include "migration/postcopy-ram.h"
94 #include "sysemu/kvm.h"
95 #include "sysemu/hax.h"
96 #include "qapi/qobject-input-visitor.h"
97 #include "qemu/option.h"
98 #include "qemu/config-file.h"
99 #include "qemu-options.h"
100 #include "qemu/main-loop.h"
102 #include "fsdev/qemu-fsdev.h"
104 #include "sysemu/qtest.h"
106 #include "disas/disas.h"
109 #include "slirp/libslirp.h"
111 #include "trace-root.h"
112 #include "trace/control.h"
113 #include "qemu/queue.h"
114 #include "sysemu/arch_init.h"
116 #include "ui/qemu-spice.h"
117 #include "qapi/string-input-visitor.h"
118 #include "qapi/opts-visitor.h"
119 #include "qapi/clone-visitor.h"
120 #include "qom/object_interfaces.h"
121 #include "exec/semihost.h"
122 #include "crypto/init.h"
123 #include "sysemu/replay.h"
124 #include "qapi/qapi-events-run-state.h"
125 #include "qapi/qapi-visit-block-core.h"
126 #include "qapi/qapi-visit-ui.h"
127 #include "qapi/qapi-commands-block-core.h"
128 #include "qapi/qapi-commands-misc.h"
129 #include "qapi/qapi-commands-run-state.h"
130 #include "qapi/qmp/qerror.h"
131 #include "sysemu/iothread.h"
133 #define MAX_VIRTIO_CONSOLES 1
135 static const char *data_dir[16];
136 static int data_dir_idx;
137 const char *bios_name = NULL;
138 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
140 const char* keyboard_layout = NULL;
142 const char *mem_path = NULL;
143 int mem_prealloc = 0; /* force preallocation of physical target memory */
144 bool enable_mlock = false;
146 NICInfo nd_table[MAX_NICS];
148 static int rtc_utc = 1;
149 static int rtc_date_offset = -1; /* -1 means no change */
150 QEMUClockType rtc_clock;
151 int vga_interface_type = VGA_NONE;
152 static DisplayOptions dpy;
154 static int num_serial_hds = 0;
155 static Chardev **serial_hds = NULL;
156 Chardev *parallel_hds[MAX_PARALLEL_PORTS];
157 Chardev *virtcon_hds[MAX_VIRTIO_CONSOLES];
158 int win2k_install_hack = 0;
161 unsigned int max_cpus;
164 int acpi_enabled = 1;
167 static int no_reboot;
170 int graphic_rotate = 0;
171 const char *watchdog;
172 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
175 const char *qemu_name;
178 unsigned int nb_prom_envs = 0;
179 const char *prom_envs[MAX_PROM_ENVS];
182 uint8_t *boot_splash_filedata;
183 size_t boot_splash_filedata_size;
184 uint8_t qemu_extra_params_fw[2];
186 int icount_align_option;
188 /* The bytes in qemu_uuid are in the order specified by RFC4122, _not_ in the
189 * little-endian "wire format" described in the SMBIOS 2.6 specification.
194 static NotifierList exit_notifiers =
195 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
197 static NotifierList machine_init_done_notifiers =
198 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
202 enum xen_mode xen_mode = XEN_EMULATE;
203 bool xen_domid_restrict;
205 static int has_defaults = 1;
206 static int default_serial = 1;
207 static int default_parallel = 1;
208 static int default_virtcon = 1;
209 static int default_monitor = 1;
210 static int default_floppy = 1;
211 static int default_cdrom = 1;
212 static int default_sdcard = 1;
213 static int default_vga = 1;
214 static int default_net = 1;
220 { .driver = "isa-serial", .flag = &default_serial },
221 { .driver = "isa-parallel", .flag = &default_parallel },
222 { .driver = "isa-fdc", .flag = &default_floppy },
223 { .driver = "floppy", .flag = &default_floppy },
224 { .driver = "ide-cd", .flag = &default_cdrom },
225 { .driver = "ide-hd", .flag = &default_cdrom },
226 { .driver = "ide-drive", .flag = &default_cdrom },
227 { .driver = "scsi-cd", .flag = &default_cdrom },
228 { .driver = "scsi-hd", .flag = &default_cdrom },
229 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
230 { .driver = "virtio-serial", .flag = &default_virtcon },
231 { .driver = "VGA", .flag = &default_vga },
232 { .driver = "isa-vga", .flag = &default_vga },
233 { .driver = "cirrus-vga", .flag = &default_vga },
234 { .driver = "isa-cirrus-vga", .flag = &default_vga },
235 { .driver = "vmware-svga", .flag = &default_vga },
236 { .driver = "qxl-vga", .flag = &default_vga },
237 { .driver = "virtio-vga", .flag = &default_vga },
240 static QemuOptsList qemu_rtc_opts = {
242 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
246 .type = QEMU_OPT_STRING,
249 .type = QEMU_OPT_STRING,
252 .type = QEMU_OPT_STRING,
254 { /* end of list */ }
258 static QemuOptsList qemu_option_rom_opts = {
259 .name = "option-rom",
260 .implied_opt_name = "romfile",
261 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
265 .type = QEMU_OPT_NUMBER,
268 .type = QEMU_OPT_STRING,
270 { /* end of list */ }
274 static QemuOptsList qemu_machine_opts = {
276 .implied_opt_name = "type",
278 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
281 * no elements => accept any
282 * sanity checking will happen later
283 * when setting machine properties
289 static QemuOptsList qemu_accel_opts = {
291 .implied_opt_name = "accel",
292 .head = QTAILQ_HEAD_INITIALIZER(qemu_accel_opts.head),
297 .type = QEMU_OPT_STRING,
298 .help = "Select the type of accelerator",
302 .type = QEMU_OPT_STRING,
303 .help = "Enable/disable multi-threaded TCG",
305 { /* end of list */ }
309 static QemuOptsList qemu_boot_opts = {
311 .implied_opt_name = "order",
313 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
317 .type = QEMU_OPT_STRING,
320 .type = QEMU_OPT_STRING,
323 .type = QEMU_OPT_BOOL,
326 .type = QEMU_OPT_STRING,
328 .name = "splash-time",
329 .type = QEMU_OPT_STRING,
331 .name = "reboot-timeout",
332 .type = QEMU_OPT_STRING,
335 .type = QEMU_OPT_BOOL,
341 static QemuOptsList qemu_add_fd_opts = {
343 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
347 .type = QEMU_OPT_NUMBER,
348 .help = "file descriptor of which a duplicate is added to fd set",
351 .type = QEMU_OPT_NUMBER,
352 .help = "ID of the fd set to add fd to",
355 .type = QEMU_OPT_STRING,
356 .help = "free-form string used to describe fd",
358 { /* end of list */ }
362 static QemuOptsList qemu_object_opts = {
364 .implied_opt_name = "qom-type",
365 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
371 static QemuOptsList qemu_tpmdev_opts = {
373 .implied_opt_name = "type",
374 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
376 /* options are defined in the TPM backends */
377 { /* end of list */ }
381 static QemuOptsList qemu_realtime_opts = {
383 .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
387 .type = QEMU_OPT_BOOL,
389 { /* end of list */ }
393 static QemuOptsList qemu_msg_opts = {
395 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
399 .type = QEMU_OPT_BOOL,
401 { /* end of list */ }
405 static QemuOptsList qemu_name_opts = {
407 .implied_opt_name = "guest",
409 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
413 .type = QEMU_OPT_STRING,
414 .help = "Sets the name of the guest.\n"
415 "This name will be displayed in the SDL window caption.\n"
416 "The name will also be used for the VNC server",
419 .type = QEMU_OPT_STRING,
420 .help = "Sets the name of the QEMU process, as shown in top etc",
422 .name = "debug-threads",
423 .type = QEMU_OPT_BOOL,
424 .help = "When enabled, name the individual threads; defaults off.\n"
425 "NOTE: The thread names are for debugging and not a\n"
428 { /* End of list */ }
432 static QemuOptsList qemu_mem_opts = {
434 .implied_opt_name = "size",
435 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
440 .type = QEMU_OPT_SIZE,
444 .type = QEMU_OPT_NUMBER,
448 .type = QEMU_OPT_SIZE,
450 { /* end of list */ }
454 static QemuOptsList qemu_icount_opts = {
456 .implied_opt_name = "shift",
458 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
462 .type = QEMU_OPT_STRING,
465 .type = QEMU_OPT_BOOL,
468 .type = QEMU_OPT_BOOL,
471 .type = QEMU_OPT_STRING,
474 .type = QEMU_OPT_STRING,
476 .name = "rrsnapshot",
477 .type = QEMU_OPT_STRING,
479 { /* end of list */ }
483 static QemuOptsList qemu_semihosting_config_opts = {
484 .name = "semihosting-config",
485 .implied_opt_name = "enable",
486 .head = QTAILQ_HEAD_INITIALIZER(qemu_semihosting_config_opts.head),
490 .type = QEMU_OPT_BOOL,
493 .type = QEMU_OPT_STRING,
496 .type = QEMU_OPT_STRING,
498 { /* end of list */ }
502 static QemuOptsList qemu_fw_cfg_opts = {
504 .implied_opt_name = "name",
505 .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
509 .type = QEMU_OPT_STRING,
510 .help = "Sets the fw_cfg name of the blob to be inserted",
513 .type = QEMU_OPT_STRING,
514 .help = "Sets the name of the file from which\n"
515 "the fw_cfg blob will be loaded",
518 .type = QEMU_OPT_STRING,
519 .help = "Sets content of the blob to be inserted from a string",
521 { /* end of list */ }
526 * Get machine options
528 * Returns: machine options (never null).
530 QemuOpts *qemu_get_machine_opts(void)
532 return qemu_find_opts_singleton("machine");
535 const char *qemu_get_vm_name(void)
540 static void res_free(void)
542 g_free(boot_splash_filedata);
543 boot_splash_filedata = NULL;
546 static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
548 const char *driver = qemu_opt_get(opts, "driver");
553 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
554 if (strcmp(default_list[i].driver, driver) != 0)
556 *(default_list[i].flag) = 0;
561 /***********************************************************/
564 static RunState current_run_state = RUN_STATE_PRELAUNCH;
566 /* We use RUN_STATE__MAX but any invalid value will do */
567 static RunState vmstop_requested = RUN_STATE__MAX;
568 static QemuMutex vmstop_lock;
573 } RunStateTransition;
575 static const RunStateTransition runstate_transitions_def[] = {
577 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
578 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
579 { RUN_STATE_DEBUG, RUN_STATE_PRELAUNCH },
581 { RUN_STATE_INMIGRATE, RUN_STATE_INTERNAL_ERROR },
582 { RUN_STATE_INMIGRATE, RUN_STATE_IO_ERROR },
583 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
584 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
585 { RUN_STATE_INMIGRATE, RUN_STATE_SHUTDOWN },
586 { RUN_STATE_INMIGRATE, RUN_STATE_SUSPENDED },
587 { RUN_STATE_INMIGRATE, RUN_STATE_WATCHDOG },
588 { RUN_STATE_INMIGRATE, RUN_STATE_GUEST_PANICKED },
589 { RUN_STATE_INMIGRATE, RUN_STATE_FINISH_MIGRATE },
590 { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH },
591 { RUN_STATE_INMIGRATE, RUN_STATE_POSTMIGRATE },
592 { RUN_STATE_INMIGRATE, RUN_STATE_COLO },
594 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
595 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
596 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PRELAUNCH },
598 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
599 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
600 { RUN_STATE_IO_ERROR, RUN_STATE_PRELAUNCH },
602 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
603 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
604 { RUN_STATE_PAUSED, RUN_STATE_POSTMIGRATE },
605 { RUN_STATE_PAUSED, RUN_STATE_PRELAUNCH },
606 { RUN_STATE_PAUSED, RUN_STATE_COLO},
608 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
609 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
610 { RUN_STATE_POSTMIGRATE, RUN_STATE_PRELAUNCH },
612 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
613 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
614 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
616 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
617 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PAUSED },
618 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
619 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PRELAUNCH },
620 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_COLO},
622 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
623 { RUN_STATE_RESTORE_VM, RUN_STATE_PRELAUNCH },
625 { RUN_STATE_COLO, RUN_STATE_RUNNING },
627 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
628 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
629 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
630 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
631 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
632 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
633 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
634 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
635 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
636 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
637 { RUN_STATE_RUNNING, RUN_STATE_COLO},
639 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
641 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
642 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
643 { RUN_STATE_SHUTDOWN, RUN_STATE_PRELAUNCH },
645 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
646 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
647 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
648 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
649 { RUN_STATE_SUSPENDED, RUN_STATE_PRELAUNCH },
650 { RUN_STATE_SUSPENDED, RUN_STATE_COLO},
652 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
653 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
654 { RUN_STATE_WATCHDOG, RUN_STATE_PRELAUNCH },
655 { RUN_STATE_WATCHDOG, RUN_STATE_COLO},
657 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
658 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
659 { RUN_STATE_GUEST_PANICKED, RUN_STATE_PRELAUNCH },
661 { RUN_STATE__MAX, RUN_STATE__MAX },
664 static bool runstate_valid_transitions[RUN_STATE__MAX][RUN_STATE__MAX];
666 bool runstate_check(RunState state)
668 return current_run_state == state;
671 bool runstate_store(char *str, size_t size)
673 const char *state = RunState_str(current_run_state);
674 size_t len = strlen(state) + 1;
679 memcpy(str, state, len);
683 static void runstate_init(void)
685 const RunStateTransition *p;
687 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
688 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE__MAX; p++) {
689 runstate_valid_transitions[p->from][p->to] = true;
692 qemu_mutex_init(&vmstop_lock);
695 /* This function will abort() on invalid state transitions */
696 void runstate_set(RunState new_state)
698 assert(new_state < RUN_STATE__MAX);
700 if (current_run_state == new_state) {
704 if (!runstate_valid_transitions[current_run_state][new_state]) {
705 error_report("invalid runstate transition: '%s' -> '%s'",
706 RunState_str(current_run_state),
707 RunState_str(new_state));
710 trace_runstate_set(new_state);
711 current_run_state = new_state;
714 int runstate_is_running(void)
716 return runstate_check(RUN_STATE_RUNNING);
719 bool runstate_needs_reset(void)
721 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
722 runstate_check(RUN_STATE_SHUTDOWN);
725 StatusInfo *qmp_query_status(Error **errp)
727 StatusInfo *info = g_malloc0(sizeof(*info));
729 info->running = runstate_is_running();
730 info->singlestep = singlestep;
731 info->status = current_run_state;
736 bool qemu_vmstop_requested(RunState *r)
738 qemu_mutex_lock(&vmstop_lock);
739 *r = vmstop_requested;
740 vmstop_requested = RUN_STATE__MAX;
741 qemu_mutex_unlock(&vmstop_lock);
742 return *r < RUN_STATE__MAX;
745 void qemu_system_vmstop_request_prepare(void)
747 qemu_mutex_lock(&vmstop_lock);
750 void qemu_system_vmstop_request(RunState state)
752 vmstop_requested = state;
753 qemu_mutex_unlock(&vmstop_lock);
757 /***********************************************************/
758 /* real time host monotonic timer */
760 static time_t qemu_time(void)
762 return qemu_clock_get_ms(QEMU_CLOCK_HOST) / 1000;
765 /***********************************************************/
766 /* host time/date access */
767 void qemu_get_timedate(struct tm *tm, int offset)
769 time_t ti = qemu_time();
772 if (rtc_date_offset == -1) {
776 localtime_r(&ti, tm);
778 ti -= rtc_date_offset;
783 int qemu_timedate_diff(struct tm *tm)
787 if (rtc_date_offset == -1)
789 seconds = mktimegm(tm);
792 tmp.tm_isdst = -1; /* use timezone to figure it out */
793 seconds = mktime(&tmp);
796 seconds = mktimegm(tm) + rtc_date_offset;
798 return seconds - qemu_time();
801 static void configure_rtc_date_offset(const char *startdate, int legacy)
803 time_t rtc_start_date;
806 if (!strcmp(startdate, "now") && legacy) {
807 rtc_date_offset = -1;
809 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
817 } else if (sscanf(startdate, "%d-%d-%d",
829 rtc_start_date = mktimegm(&tm);
830 if (rtc_start_date == -1) {
832 error_report("invalid date format");
833 error_printf("valid formats: "
834 "'2006-06-17T16:01:21' or '2006-06-17'\n");
837 rtc_date_offset = qemu_time() - rtc_start_date;
841 static void configure_rtc(QemuOpts *opts)
845 value = qemu_opt_get(opts, "base");
847 if (!strcmp(value, "utc")) {
849 } else if (!strcmp(value, "localtime")) {
850 Error *blocker = NULL;
852 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
853 "-rtc base=localtime");
854 replay_add_blocker(blocker);
856 configure_rtc_date_offset(value, 0);
859 value = qemu_opt_get(opts, "clock");
861 if (!strcmp(value, "host")) {
862 rtc_clock = QEMU_CLOCK_HOST;
863 } else if (!strcmp(value, "rt")) {
864 rtc_clock = QEMU_CLOCK_REALTIME;
865 } else if (!strcmp(value, "vm")) {
866 rtc_clock = QEMU_CLOCK_VIRTUAL;
868 error_report("invalid option value '%s'", value);
872 value = qemu_opt_get(opts, "driftfix");
874 if (!strcmp(value, "slew")) {
875 static GlobalProperty slew_lost_ticks = {
876 .driver = "mc146818rtc",
877 .property = "lost_tick_policy",
881 qdev_prop_register_global(&slew_lost_ticks);
882 } else if (!strcmp(value, "none")) {
883 /* discard is default */
885 error_report("invalid option value '%s'", value);
891 /***********************************************************/
892 /* Bluetooth support */
895 static struct HCIInfo *hci_table[MAX_NICS];
897 struct HCIInfo *qemu_next_hci(void)
899 if (cur_hci == nb_hcis)
902 return hci_table[cur_hci++];
905 static int bt_hci_parse(const char *str)
910 if (nb_hcis >= MAX_NICS) {
911 error_report("too many bluetooth HCIs (max %i)", MAX_NICS);
924 bdaddr.b[5] = 0x56 + nb_hcis;
925 hci->bdaddr_set(hci, bdaddr.b);
927 hci_table[nb_hcis++] = hci;
932 static void bt_vhci_add(int vlan_id)
934 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
937 warn_report("adding a VHCI to an empty scatternet %i",
940 bt_vhci_init(bt_new_hci(vlan));
943 static struct bt_device_s *bt_device_add(const char *opt)
945 struct bt_scatternet_s *vlan;
947 char *endp = strstr(opt, ",vlan=");
948 int len = (endp ? endp - opt : strlen(opt)) + 1;
951 pstrcpy(devname, MIN(sizeof(devname), len), opt);
954 vlan_id = strtol(endp + 6, &endp, 0);
956 error_report("unrecognised bluetooth vlan Id");
961 vlan = qemu_find_bt_vlan(vlan_id);
964 warn_report("adding a slave device to an empty scatternet %i",
967 if (!strcmp(devname, "keyboard"))
968 return bt_keyboard_init(vlan);
970 error_report("unsupported bluetooth device '%s'", devname);
974 static int bt_parse(const char *opt)
976 const char *endp, *p;
979 if (strstart(opt, "hci", &endp)) {
980 if (!*endp || *endp == ',') {
982 if (!strstart(endp, ",vlan=", 0))
985 return bt_hci_parse(opt);
987 } else if (strstart(opt, "vhci", &endp)) {
988 if (!*endp || *endp == ',') {
990 if (strstart(endp, ",vlan=", &p)) {
991 vlan = strtol(p, (char **) &endp, 0);
993 error_report("bad scatternet '%s'", p);
997 error_report("bad parameter '%s'", endp + 1);
1006 } else if (strstart(opt, "device:", &endp))
1007 return !bt_device_add(endp);
1009 error_report("bad bluetooth parameter '%s'", opt);
1013 static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
1015 const char *proc_name;
1017 if (qemu_opt_get(opts, "debug-threads")) {
1018 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
1020 qemu_name = qemu_opt_get(opts, "guest");
1022 proc_name = qemu_opt_get(opts, "process");
1024 os_set_proc_name(proc_name);
1030 bool defaults_enabled(void)
1032 return has_defaults;
1036 static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1038 int fd, dupfd, flags;
1040 const char *fd_opaque = NULL;
1043 fd = qemu_opt_get_number(opts, "fd", -1);
1044 fdset_id = qemu_opt_get_number(opts, "set", -1);
1045 fd_opaque = qemu_opt_get(opts, "opaque");
1048 error_report("fd option is required and must be non-negative");
1052 if (fd <= STDERR_FILENO) {
1053 error_report("fd cannot be a standard I/O stream");
1058 * All fds inherited across exec() necessarily have FD_CLOEXEC
1059 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1061 flags = fcntl(fd, F_GETFD);
1062 if (flags == -1 || (flags & FD_CLOEXEC)) {
1063 error_report("fd is not valid or already in use");
1068 error_report("set option is required and must be non-negative");
1072 #ifdef F_DUPFD_CLOEXEC
1073 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1077 qemu_set_cloexec(dupfd);
1081 error_report("error duplicating fd: %s", strerror(errno));
1085 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1086 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
1093 static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1097 fd = qemu_opt_get_number(opts, "fd", -1);
1104 /***********************************************************/
1105 /* QEMU Block devices */
1107 #define HD_OPTS "media=disk"
1108 #define CDROM_OPTS "media=cdrom"
1110 #define PFLASH_OPTS ""
1114 static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
1116 BlockInterfaceType *block_default_type = opaque;
1118 return drive_new(opts, *block_default_type) == NULL;
1121 static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
1123 if (qemu_opt_get(opts, "snapshot") == NULL) {
1124 qemu_opt_set(opts, "snapshot", "on", &error_abort);
1129 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1130 int index, const char *optstr)
1135 if (!enable || drive_get_by_index(type, index)) {
1139 opts = drive_add(type, index, NULL, optstr);
1141 drive_enable_snapshot(NULL, opts, NULL);
1144 dinfo = drive_new(opts, type);
1148 dinfo->is_default = true;
1152 static QemuOptsList qemu_smp_opts = {
1154 .implied_opt_name = "cpus",
1155 .merge_lists = true,
1156 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
1160 .type = QEMU_OPT_NUMBER,
1163 .type = QEMU_OPT_NUMBER,
1166 .type = QEMU_OPT_NUMBER,
1169 .type = QEMU_OPT_NUMBER,
1172 .type = QEMU_OPT_NUMBER,
1174 { /*End of list */ }
1178 static void smp_parse(QemuOpts *opts)
1181 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
1182 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
1183 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
1184 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
1186 /* compute missing values, prefer sockets over cores over threads */
1187 if (cpus == 0 || sockets == 0) {
1188 sockets = sockets > 0 ? sockets : 1;
1189 cores = cores > 0 ? cores : 1;
1190 threads = threads > 0 ? threads : 1;
1192 cpus = cores * threads * sockets;
1194 } else if (cores == 0) {
1195 threads = threads > 0 ? threads : 1;
1196 cores = cpus / (sockets * threads);
1197 cores = cores > 0 ? cores : 1;
1198 } else if (threads == 0) {
1199 threads = cpus / (cores * sockets);
1200 threads = threads > 0 ? threads : 1;
1201 } else if (sockets * cores * threads < cpus) {
1202 error_report("cpu topology: "
1203 "sockets (%u) * cores (%u) * threads (%u) < "
1205 sockets, cores, threads, cpus);
1209 max_cpus = qemu_opt_get_number(opts, "maxcpus", cpus);
1211 if (max_cpus < cpus) {
1212 error_report("maxcpus must be equal to or greater than smp");
1216 if (sockets * cores * threads > max_cpus) {
1217 error_report("cpu topology: "
1218 "sockets (%u) * cores (%u) * threads (%u) > "
1220 sockets, cores, threads, max_cpus);
1226 smp_threads = threads;
1230 Error *blocker = NULL;
1231 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
1232 replay_add_blocker(blocker);
1236 static void realtime_init(void)
1239 if (os_mlock() < 0) {
1240 error_report("locking memory failed");
1247 static void configure_msg(QemuOpts *opts)
1249 enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
1252 /***********************************************************/
1255 typedef struct SemihostingConfig {
1257 SemihostingTarget target;
1260 const char *cmdline; /* concatenated argv */
1261 } SemihostingConfig;
1263 static SemihostingConfig semihosting;
1265 bool semihosting_enabled(void)
1267 return semihosting.enabled;
1270 SemihostingTarget semihosting_get_target(void)
1272 return semihosting.target;
1275 const char *semihosting_get_arg(int i)
1277 if (i >= semihosting.argc) {
1280 return semihosting.argv[i];
1283 int semihosting_get_argc(void)
1285 return semihosting.argc;
1288 const char *semihosting_get_cmdline(void)
1290 if (semihosting.cmdline == NULL && semihosting.argc > 0) {
1291 semihosting.cmdline = g_strjoinv(" ", (gchar **)semihosting.argv);
1293 return semihosting.cmdline;
1296 static int add_semihosting_arg(void *opaque,
1297 const char *name, const char *val,
1300 SemihostingConfig *s = opaque;
1301 if (strcmp(name, "arg") == 0) {
1303 /* one extra element as g_strjoinv() expects NULL-terminated array */
1304 s->argv = g_realloc(s->argv, (s->argc + 1) * sizeof(void *));
1305 s->argv[s->argc - 1] = val;
1306 s->argv[s->argc] = NULL;
1311 /* Use strings passed via -kernel/-append to initialize semihosting.argv[] */
1312 static inline void semihosting_arg_fallback(const char *file, const char *cmd)
1317 add_semihosting_arg(&semihosting, "arg", file, NULL);
1319 /* split -append and initialize argv[1..n] */
1320 cmd_token = strtok(g_strdup(cmd), " ");
1322 add_semihosting_arg(&semihosting, "arg", cmd_token, NULL);
1323 cmd_token = strtok(NULL, " ");
1327 /* Now we still need this for compatibility with XEN. */
1328 bool has_igd_gfx_passthru;
1329 static void igd_gfx_passthru(void)
1331 has_igd_gfx_passthru = current_machine->igd_gfx_passthru;
1334 /***********************************************************/
1337 static int usb_device_add(const char *devname)
1339 USBDevice *dev = NULL;
1341 if (!machine_usb(current_machine)) {
1345 dev = usbdevice_create(devname);
1352 static int usb_parse(const char *cmdline)
1355 r = usb_device_add(cmdline);
1357 error_report("could not add USB device '%s'", cmdline);
1362 /***********************************************************/
1363 /* machine registration */
1365 MachineState *current_machine;
1367 static MachineClass *find_machine(const char *name)
1369 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1370 MachineClass *mc = NULL;
1372 for (el = machines; el; el = el->next) {
1373 MachineClass *temp = el->data;
1375 if (!strcmp(temp->name, name)) {
1380 !strcmp(temp->alias, name)) {
1386 g_slist_free(machines);
1390 MachineClass *find_default_machine(void)
1392 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1393 MachineClass *mc = NULL;
1395 for (el = machines; el; el = el->next) {
1396 MachineClass *temp = el->data;
1398 if (temp->is_default) {
1404 g_slist_free(machines);
1408 MachineInfoList *qmp_query_machines(Error **errp)
1410 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1411 MachineInfoList *mach_list = NULL;
1413 for (el = machines; el; el = el->next) {
1414 MachineClass *mc = el->data;
1415 MachineInfoList *entry;
1418 info = g_malloc0(sizeof(*info));
1419 if (mc->is_default) {
1420 info->has_is_default = true;
1421 info->is_default = true;
1425 info->has_alias = true;
1426 info->alias = g_strdup(mc->alias);
1429 info->name = g_strdup(mc->name);
1430 info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
1431 info->hotpluggable_cpus = mc->has_hotpluggable_cpus;
1433 entry = g_malloc0(sizeof(*entry));
1434 entry->value = info;
1435 entry->next = mach_list;
1439 g_slist_free(machines);
1443 static int machine_help_func(QemuOpts *opts, MachineState *machine)
1445 ObjectProperty *prop;
1446 ObjectPropertyIterator iter;
1448 if (!qemu_opt_has_help_opt(opts)) {
1452 object_property_iter_init(&iter, OBJECT(machine));
1453 while ((prop = object_property_iter_next(&iter))) {
1458 error_printf("%s.%s=%s", MACHINE_GET_CLASS(machine)->name,
1459 prop->name, prop->type);
1460 if (prop->description) {
1461 error_printf(" (%s)\n", prop->description);
1470 /***********************************************************/
1471 /* main execution loop */
1473 struct vm_change_state_entry {
1474 VMChangeStateHandler *cb;
1476 QLIST_ENTRY (vm_change_state_entry) entries;
1479 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1481 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1484 VMChangeStateEntry *e;
1486 e = g_malloc0(sizeof (*e));
1490 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1494 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1496 QLIST_REMOVE (e, entries);
1500 void vm_state_notify(int running, RunState state)
1502 VMChangeStateEntry *e, *next;
1504 trace_vm_state_notify(running, state);
1506 QLIST_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
1507 e->cb(e->opaque, running, state);
1511 static ShutdownCause reset_requested;
1512 static ShutdownCause shutdown_requested;
1513 static int shutdown_signal;
1514 static pid_t shutdown_pid;
1515 static int powerdown_requested;
1516 static int debug_requested;
1517 static int suspend_requested;
1518 static WakeupReason wakeup_reason;
1519 static NotifierList powerdown_notifiers =
1520 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
1521 static NotifierList suspend_notifiers =
1522 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1523 static NotifierList wakeup_notifiers =
1524 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1525 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
1527 ShutdownCause qemu_shutdown_requested_get(void)
1529 return shutdown_requested;
1532 ShutdownCause qemu_reset_requested_get(void)
1534 return reset_requested;
1537 static int qemu_shutdown_requested(void)
1539 return atomic_xchg(&shutdown_requested, SHUTDOWN_CAUSE_NONE);
1542 static void qemu_kill_report(void)
1544 if (!qtest_driver() && shutdown_signal) {
1545 if (shutdown_pid == 0) {
1546 /* This happens for eg ^C at the terminal, so it's worth
1547 * avoiding printing an odd message in that case.
1549 error_report("terminating on signal %d", shutdown_signal);
1551 char *shutdown_cmd = qemu_get_pid_name(shutdown_pid);
1553 error_report("terminating on signal %d from pid " FMT_pid " (%s)",
1554 shutdown_signal, shutdown_pid,
1555 shutdown_cmd ? shutdown_cmd : "<unknown process>");
1556 g_free(shutdown_cmd);
1558 shutdown_signal = 0;
1562 static ShutdownCause qemu_reset_requested(void)
1564 ShutdownCause r = reset_requested;
1566 if (r && replay_checkpoint(CHECKPOINT_RESET_REQUESTED)) {
1567 reset_requested = SHUTDOWN_CAUSE_NONE;
1570 return SHUTDOWN_CAUSE_NONE;
1573 static int qemu_suspend_requested(void)
1575 int r = suspend_requested;
1576 if (r && replay_checkpoint(CHECKPOINT_SUSPEND_REQUESTED)) {
1577 suspend_requested = 0;
1583 static WakeupReason qemu_wakeup_requested(void)
1585 return wakeup_reason;
1588 static int qemu_powerdown_requested(void)
1590 int r = powerdown_requested;
1591 powerdown_requested = 0;
1595 static int qemu_debug_requested(void)
1597 int r = debug_requested;
1598 debug_requested = 0;
1603 * Reset the VM. Issue an event unless @reason is SHUTDOWN_CAUSE_NONE.
1605 void qemu_system_reset(ShutdownCause reason)
1609 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1611 cpu_synchronize_all_states();
1613 if (mc && mc->reset) {
1616 qemu_devices_reset();
1619 qapi_event_send_reset(shutdown_caused_by_guest(reason),
1622 cpu_synchronize_all_post_reset();
1625 void qemu_system_guest_panicked(GuestPanicInformation *info)
1627 qemu_log_mask(LOG_GUEST_ERROR, "Guest crashed");
1630 current_cpu->crash_occurred = true;
1632 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE,
1633 !!info, info, &error_abort);
1634 vm_stop(RUN_STATE_GUEST_PANICKED);
1636 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_POWEROFF,
1637 !!info, info, &error_abort);
1638 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_PANIC);
1642 if (info->type == GUEST_PANIC_INFORMATION_TYPE_HYPER_V) {
1643 qemu_log_mask(LOG_GUEST_ERROR, "\nHV crash parameters: (%#"PRIx64
1644 " %#"PRIx64" %#"PRIx64" %#"PRIx64" %#"PRIx64")\n",
1645 info->u.hyper_v.arg1,
1646 info->u.hyper_v.arg2,
1647 info->u.hyper_v.arg3,
1648 info->u.hyper_v.arg4,
1649 info->u.hyper_v.arg5);
1650 } else if (info->type == GUEST_PANIC_INFORMATION_TYPE_S390) {
1651 qemu_log_mask(LOG_GUEST_ERROR, " on cpu %d: %s\n"
1652 "PSW: 0x%016" PRIx64 " 0x%016" PRIx64"\n",
1654 S390CrashReason_str(info->u.s390.reason),
1655 info->u.s390.psw_mask,
1656 info->u.s390.psw_addr);
1658 qapi_free_GuestPanicInformation(info);
1662 void qemu_system_reset_request(ShutdownCause reason)
1665 shutdown_requested = reason;
1667 reset_requested = reason;
1670 qemu_notify_event();
1673 static void qemu_system_suspend(void)
1676 notifier_list_notify(&suspend_notifiers, NULL);
1677 runstate_set(RUN_STATE_SUSPENDED);
1678 qapi_event_send_suspend(&error_abort);
1681 void qemu_system_suspend_request(void)
1683 if (runstate_check(RUN_STATE_SUSPENDED)) {
1686 suspend_requested = 1;
1688 qemu_notify_event();
1691 void qemu_register_suspend_notifier(Notifier *notifier)
1693 notifier_list_add(&suspend_notifiers, notifier);
1696 void qemu_system_wakeup_request(WakeupReason reason)
1698 trace_system_wakeup_request(reason);
1700 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1703 if (!(wakeup_reason_mask & (1 << reason))) {
1706 runstate_set(RUN_STATE_RUNNING);
1707 wakeup_reason = reason;
1708 qemu_notify_event();
1711 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1714 wakeup_reason_mask |= (1 << reason);
1716 wakeup_reason_mask &= ~(1 << reason);
1720 void qemu_register_wakeup_notifier(Notifier *notifier)
1722 notifier_list_add(&wakeup_notifiers, notifier);
1725 void qemu_system_killed(int signal, pid_t pid)
1727 shutdown_signal = signal;
1731 /* Cannot call qemu_system_shutdown_request directly because
1732 * we are in a signal handler.
1734 shutdown_requested = SHUTDOWN_CAUSE_HOST_SIGNAL;
1735 qemu_notify_event();
1738 void qemu_system_shutdown_request(ShutdownCause reason)
1740 trace_qemu_system_shutdown_request(reason);
1741 replay_shutdown_request(reason);
1742 shutdown_requested = reason;
1743 qemu_notify_event();
1746 static void qemu_system_powerdown(void)
1748 qapi_event_send_powerdown(&error_abort);
1749 notifier_list_notify(&powerdown_notifiers, NULL);
1752 void qemu_system_powerdown_request(void)
1754 trace_qemu_system_powerdown_request();
1755 powerdown_requested = 1;
1756 qemu_notify_event();
1759 void qemu_register_powerdown_notifier(Notifier *notifier)
1761 notifier_list_add(&powerdown_notifiers, notifier);
1764 void qemu_system_debug_request(void)
1766 debug_requested = 1;
1767 qemu_notify_event();
1770 static bool main_loop_should_exit(void)
1773 ShutdownCause request;
1775 if (qemu_debug_requested()) {
1776 vm_stop(RUN_STATE_DEBUG);
1778 if (qemu_suspend_requested()) {
1779 qemu_system_suspend();
1781 request = qemu_shutdown_requested();
1784 qapi_event_send_shutdown(shutdown_caused_by_guest(request),
1787 vm_stop(RUN_STATE_SHUTDOWN);
1792 request = qemu_reset_requested();
1795 qemu_system_reset(request);
1797 if (!runstate_check(RUN_STATE_RUNNING) &&
1798 !runstate_check(RUN_STATE_INMIGRATE)) {
1799 runstate_set(RUN_STATE_PRELAUNCH);
1802 if (qemu_wakeup_requested()) {
1804 qemu_system_reset(SHUTDOWN_CAUSE_NONE);
1805 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
1806 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
1808 qapi_event_send_wakeup(&error_abort);
1810 if (qemu_powerdown_requested()) {
1811 qemu_system_powerdown();
1813 if (qemu_vmstop_requested(&r)) {
1819 static void main_loop(void)
1821 #ifdef CONFIG_PROFILER
1825 #ifdef CONFIG_PROFILER
1826 ti = profile_getclock();
1828 main_loop_wait(false);
1829 #ifdef CONFIG_PROFILER
1830 dev_time += profile_getclock() - ti;
1832 } while (!main_loop_should_exit());
1835 static void version(void)
1837 printf("QEMU emulator version " QEMU_FULL_VERSION "\n"
1838 QEMU_COPYRIGHT "\n");
1841 static void help(int exitcode)
1844 printf("usage: %s [options] [disk_image]\n\n"
1845 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1846 error_get_progname());
1848 #define QEMU_OPTIONS_GENERATE_HELP
1849 #include "qemu-options-wrapper.h"
1851 printf("\nDuring emulation, the following keys are useful:\n"
1852 "ctrl-alt-f toggle full screen\n"
1853 "ctrl-alt-n switch to virtual console 'n'\n"
1854 "ctrl-alt toggle mouse and keyboard grab\n"
1856 "When using -nographic, press 'ctrl-a h' to get some help.\n"
1858 QEMU_HELP_BOTTOM "\n");
1863 #define HAS_ARG 0x0001
1865 typedef struct QEMUOption {
1872 static const QEMUOption qemu_options[] = {
1873 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1874 #define QEMU_OPTIONS_GENERATE_OPTIONS
1875 #include "qemu-options-wrapper.h"
1879 typedef struct VGAInterfaceInfo {
1880 const char *opt_name; /* option name */
1881 const char *name; /* human-readable name */
1882 /* Class names indicating that support is available.
1883 * If no class is specified, the interface is always available */
1884 const char *class_names[2];
1887 static VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
1893 .name = "standard VGA",
1894 .class_names = { "VGA", "isa-vga" },
1897 .opt_name = "cirrus",
1898 .name = "Cirrus VGA",
1899 .class_names = { "cirrus-vga", "isa-cirrus-vga" },
1902 .opt_name = "vmware",
1903 .name = "VMWare SVGA",
1904 .class_names = { "vmware-svga" },
1907 .opt_name = "virtio",
1908 .name = "Virtio VGA",
1909 .class_names = { "virtio-vga" },
1914 .class_names = { "qxl-vga" },
1918 .name = "TCX framebuffer",
1919 .class_names = { "SUNW,tcx" },
1923 .name = "CG3 framebuffer",
1924 .class_names = { "cgthree" },
1927 .opt_name = "xenfb",
1931 static bool vga_interface_available(VGAInterfaceType t)
1933 VGAInterfaceInfo *ti = &vga_interfaces[t];
1935 assert(t < VGA_TYPE_MAX);
1936 return !ti->class_names[0] ||
1937 object_class_by_name(ti->class_names[0]) ||
1938 object_class_by_name(ti->class_names[1]);
1941 static void select_vgahw(const char *p)
1946 assert(vga_interface_type == VGA_NONE);
1947 for (t = 0; t < VGA_TYPE_MAX; t++) {
1948 VGAInterfaceInfo *ti = &vga_interfaces[t];
1949 if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
1950 if (!vga_interface_available(t)) {
1951 error_report("%s not available", ti->name);
1954 vga_interface_type = t;
1958 if (t == VGA_TYPE_MAX) {
1960 error_report("unknown vga type: %s", p);
1964 const char *nextopt;
1966 if (strstart(opts, ",retrace=", &nextopt)) {
1968 if (strstart(opts, "dumb", &nextopt))
1969 vga_retrace_method = VGA_RETRACE_DUMB;
1970 else if (strstart(opts, "precise", &nextopt))
1971 vga_retrace_method = VGA_RETRACE_PRECISE;
1972 else goto invalid_vga;
1973 } else goto invalid_vga;
1978 static void parse_display_qapi(const char *optarg)
1981 DisplayOptions *opts;
1984 v = qobject_input_visitor_new_str(optarg, "type", &err);
1986 error_report_err(err);
1990 visit_type_DisplayOptions(v, NULL, &opts, &error_fatal);
1991 QAPI_CLONE_MEMBERS(DisplayOptions, &dpy, opts);
1993 qapi_free_DisplayOptions(opts);
1997 static void parse_display(const char *p)
2001 if (strstart(p, "sdl", &opts)) {
2003 * sdl DisplayType needs hand-crafted parser instead of
2004 * parse_display_qapi() due to some options not in
2005 * DisplayOptions, specifically:
2007 * Already deprecated.
2008 * - ctrl_grab + alt_grab
2009 * Not clear yet what happens to them long-term. Should
2010 * replaced by something better or deprecated and dropped.
2012 dpy.type = DISPLAY_TYPE_SDL;
2014 const char *nextopt;
2016 if (strstart(opts, ",frame=", &nextopt)) {
2017 g_printerr("The frame= sdl option is deprecated, and will be\n"
2018 "removed in a future release.\n");
2020 if (strstart(opts, "on", &nextopt)) {
2022 } else if (strstart(opts, "off", &nextopt)) {
2025 goto invalid_sdl_args;
2027 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
2029 if (strstart(opts, "on", &nextopt)) {
2031 } else if (strstart(opts, "off", &nextopt)) {
2034 goto invalid_sdl_args;
2036 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2038 if (strstart(opts, "on", &nextopt)) {
2040 } else if (strstart(opts, "off", &nextopt)) {
2043 goto invalid_sdl_args;
2045 } else if (strstart(opts, ",window_close=", &nextopt)) {
2047 dpy.has_window_close = true;
2048 if (strstart(opts, "on", &nextopt)) {
2049 dpy.window_close = true;
2050 } else if (strstart(opts, "off", &nextopt)) {
2051 dpy.window_close = false;
2053 goto invalid_sdl_args;
2055 } else if (strstart(opts, ",gl=", &nextopt)) {
2058 if (strstart(opts, "on", &nextopt)) {
2059 dpy.gl = DISPLAYGL_MODE_ON;
2060 } else if (strstart(opts, "core", &nextopt)) {
2061 dpy.gl = DISPLAYGL_MODE_CORE;
2062 } else if (strstart(opts, "es", &nextopt)) {
2063 dpy.gl = DISPLAYGL_MODE_ES;
2064 } else if (strstart(opts, "off", &nextopt)) {
2065 dpy.gl = DISPLAYGL_MODE_OFF;
2067 goto invalid_sdl_args;
2071 error_report("invalid SDL option string");
2076 } else if (strstart(p, "vnc", &opts)) {
2078 * vnc isn't a (local) DisplayType but a protocol for remote
2082 vnc_parse(opts + 1, &error_fatal);
2084 error_report("VNC requires a display argument vnc=<display>");
2088 parse_display_qapi(p);
2092 static int balloon_parse(const char *arg)
2096 warn_report("This option is deprecated. "
2097 "Use '--device virtio-balloon' to enable the balloon device.");
2099 if (strcmp(arg, "none") == 0) {
2103 if (!strncmp(arg, "virtio", 6)) {
2104 if (arg[6] == ',') {
2105 /* have params -> parse them */
2106 opts = qemu_opts_parse_noisily(qemu_find_opts("device"), arg + 7,
2111 /* create empty opts */
2112 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2115 qemu_opt_set(opts, "driver", "virtio-balloon", &error_abort);
2122 char *qemu_find_file(int type, const char *name)
2128 /* Try the name as a straight path first */
2129 if (access(name, R_OK) == 0) {
2130 trace_load_file(name, name);
2131 return g_strdup(name);
2135 case QEMU_FILE_TYPE_BIOS:
2138 case QEMU_FILE_TYPE_KEYMAP:
2139 subdir = "keymaps/";
2145 for (i = 0; i < data_dir_idx; i++) {
2146 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2147 if (access(buf, R_OK) == 0) {
2148 trace_load_file(name, buf);
2156 static void qemu_add_data_dir(const char *path)
2163 if (data_dir_idx == ARRAY_SIZE(data_dir)) {
2166 for (i = 0; i < data_dir_idx; i++) {
2167 if (strcmp(data_dir[i], path) == 0) {
2168 return; /* duplicate */
2171 data_dir[data_dir_idx++] = g_strdup(path);
2174 static inline bool nonempty_str(const char *str)
2179 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
2183 const char *name, *file, *str;
2184 FWCfgState *fw_cfg = (FWCfgState *) opaque;
2186 if (fw_cfg == NULL) {
2187 error_report("fw_cfg device not available");
2190 name = qemu_opt_get(opts, "name");
2191 file = qemu_opt_get(opts, "file");
2192 str = qemu_opt_get(opts, "string");
2194 /* we need name and either a file or the content string */
2195 if (!(nonempty_str(name) && (nonempty_str(file) || nonempty_str(str)))) {
2196 error_report("invalid argument(s)");
2199 if (nonempty_str(file) && nonempty_str(str)) {
2200 error_report("file and string are mutually exclusive");
2203 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
2204 error_report("name too long (max. %d char)", FW_CFG_MAX_FILE_PATH - 1);
2207 if (strncmp(name, "opt/", 4) != 0) {
2208 warn_report("externally provided fw_cfg item names "
2209 "should be prefixed with \"opt/\"");
2211 if (nonempty_str(str)) {
2212 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
2213 buf = g_memdup(str, size);
2215 if (!g_file_get_contents(file, &buf, &size, NULL)) {
2216 error_report("can't load %s", file);
2220 /* For legacy, keep user files in a specific global order. */
2221 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
2222 fw_cfg_add_file(fw_cfg, name, buf, size);
2223 fw_cfg_reset_order_override(fw_cfg);
2227 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
2229 return qdev_device_help(opts);
2232 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
2237 dev = qdev_device_add(opts, &err);
2239 error_report_err(err);
2242 object_unref(OBJECT(dev));
2246 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2248 Error *local_err = NULL;
2250 if (!qemu_chr_new_from_opts(opts, &local_err)) {
2252 error_report_err(local_err);
2260 #ifdef CONFIG_VIRTFS
2261 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2263 return qemu_fsdev_add(opts);
2267 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
2270 const char *chardev;
2274 mode = qemu_opt_get(opts, "mode");
2278 if (strcmp(mode, "readline") == 0) {
2279 flags = MONITOR_USE_READLINE;
2280 } else if (strcmp(mode, "control") == 0) {
2281 flags = MONITOR_USE_CONTROL;
2283 error_report("unknown monitor mode \"%s\"", mode);
2287 if (qemu_opt_get_bool(opts, "pretty", 0))
2288 flags |= MONITOR_USE_PRETTY;
2290 /* OOB is off by default */
2291 if (qemu_opt_get_bool(opts, "x-oob", 0)) {
2292 flags |= MONITOR_USE_OOB;
2295 chardev = qemu_opt_get(opts, "chardev");
2296 chr = qemu_chr_find(chardev);
2298 error_report("chardev \"%s\" not found", chardev);
2302 monitor_init(chr, flags);
2306 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
2308 static int monitor_device_index = 0;
2313 if (strstart(optarg, "chardev:", &p)) {
2314 snprintf(label, sizeof(label), "%s", p);
2316 snprintf(label, sizeof(label), "compat_monitor%d",
2317 monitor_device_index);
2318 opts = qemu_chr_parse_compat(label, optarg);
2320 error_report("parse error: %s", optarg);
2325 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
2326 qemu_opt_set(opts, "mode", mode, &error_abort);
2327 qemu_opt_set(opts, "chardev", label, &error_abort);
2328 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
2329 monitor_device_index++;
2332 struct device_config {
2334 DEV_USB, /* -usbdevice */
2336 DEV_SERIAL, /* -serial */
2337 DEV_PARALLEL, /* -parallel */
2338 DEV_VIRTCON, /* -virtioconsole */
2339 DEV_DEBUGCON, /* -debugcon */
2340 DEV_GDB, /* -gdb, -s */
2341 DEV_SCLP, /* s390 sclp */
2343 const char *cmdline;
2345 QTAILQ_ENTRY(device_config) next;
2348 static QTAILQ_HEAD(, device_config) device_configs =
2349 QTAILQ_HEAD_INITIALIZER(device_configs);
2351 static void add_device_config(int type, const char *cmdline)
2353 struct device_config *conf;
2355 conf = g_malloc0(sizeof(*conf));
2357 conf->cmdline = cmdline;
2358 loc_save(&conf->loc);
2359 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2362 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2364 struct device_config *conf;
2367 QTAILQ_FOREACH(conf, &device_configs, next) {
2368 if (conf->type != type)
2370 loc_push_restore(&conf->loc);
2371 rc = func(conf->cmdline);
2372 loc_pop(&conf->loc);
2380 static int serial_parse(const char *devname)
2382 int index = num_serial_hds;
2385 if (strcmp(devname, "none") == 0)
2387 snprintf(label, sizeof(label), "serial%d", index);
2388 serial_hds = g_renew(Chardev *, serial_hds, index + 1);
2390 serial_hds[index] = qemu_chr_new(label, devname);
2391 if (!serial_hds[index]) {
2392 error_report("could not connect serial device"
2393 " to character backend '%s'", devname);
2400 Chardev *serial_hd(int i)
2403 if (i < num_serial_hds) {
2404 return serial_hds[i];
2409 int serial_max_hds(void)
2411 return num_serial_hds;
2414 static int parallel_parse(const char *devname)
2416 static int index = 0;
2419 if (strcmp(devname, "none") == 0)
2421 if (index == MAX_PARALLEL_PORTS) {
2422 error_report("too many parallel ports");
2425 snprintf(label, sizeof(label), "parallel%d", index);
2426 parallel_hds[index] = qemu_chr_new(label, devname);
2427 if (!parallel_hds[index]) {
2428 error_report("could not connect parallel device"
2429 " to character backend '%s'", devname);
2436 static int virtcon_parse(const char *devname)
2438 QemuOptsList *device = qemu_find_opts("device");
2439 static int index = 0;
2441 QemuOpts *bus_opts, *dev_opts;
2443 if (strcmp(devname, "none") == 0)
2445 if (index == MAX_VIRTIO_CONSOLES) {
2446 error_report("too many virtio consoles");
2450 bus_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2451 qemu_opt_set(bus_opts, "driver", "virtio-serial", &error_abort);
2453 dev_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2454 qemu_opt_set(dev_opts, "driver", "virtconsole", &error_abort);
2456 snprintf(label, sizeof(label), "virtcon%d", index);
2457 virtcon_hds[index] = qemu_chr_new(label, devname);
2458 if (!virtcon_hds[index]) {
2459 error_report("could not connect virtio console"
2460 " to character backend '%s'", devname);
2463 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2469 static int debugcon_parse(const char *devname)
2473 if (!qemu_chr_new("debugcon", devname)) {
2476 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2478 error_report("already have a debugcon device");
2481 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
2482 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
2486 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
2488 const MachineClass *mc1 = a, *mc2 = b;
2491 if (mc1->family == NULL) {
2492 if (mc2->family == NULL) {
2493 /* Compare standalone machine types against each other; they sort
2494 * in increasing order.
2496 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
2497 object_class_get_name(OBJECT_CLASS(mc2)));
2500 /* Standalone machine types sort after families. */
2504 if (mc2->family == NULL) {
2505 /* Families sort before standalone machine types. */
2509 /* Families sort between each other alphabetically increasingly. */
2510 res = strcmp(mc1->family, mc2->family);
2515 /* Within the same family, machine types sort in decreasing order. */
2516 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
2517 object_class_get_name(OBJECT_CLASS(mc1)));
2520 static MachineClass *machine_parse(const char *name)
2522 MachineClass *mc = NULL;
2523 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
2526 mc = find_machine(name);
2529 g_slist_free(machines);
2532 if (name && !is_help_option(name)) {
2533 error_report("unsupported machine type");
2534 error_printf("Use -machine help to list supported machines\n");
2536 printf("Supported machines are:\n");
2537 machines = g_slist_sort(machines, machine_class_cmp);
2538 for (el = machines; el; el = el->next) {
2539 MachineClass *mc = el->data;
2541 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
2543 printf("%-20s %s%s\n", mc->name, mc->desc,
2544 mc->is_default ? " (default)" : "");
2548 g_slist_free(machines);
2549 exit(!name || !is_help_option(name));
2552 void qemu_add_exit_notifier(Notifier *notify)
2554 notifier_list_add(&exit_notifiers, notify);
2557 void qemu_remove_exit_notifier(Notifier *notify)
2559 notifier_remove(notify);
2562 static void qemu_run_exit_notifiers(void)
2564 notifier_list_notify(&exit_notifiers, NULL);
2567 bool machine_init_done;
2569 void qemu_add_machine_init_done_notifier(Notifier *notify)
2571 notifier_list_add(&machine_init_done_notifiers, notify);
2572 if (machine_init_done) {
2573 notify->notify(notify, NULL);
2577 void qemu_remove_machine_init_done_notifier(Notifier *notify)
2579 notifier_remove(notify);
2582 static void qemu_run_machine_init_done_notifiers(void)
2584 machine_init_done = true;
2585 notifier_list_notify(&machine_init_done_notifiers, NULL);
2588 static const QEMUOption *lookup_opt(int argc, char **argv,
2589 const char **poptarg, int *poptind)
2591 const QEMUOption *popt;
2592 int optind = *poptind;
2593 char *r = argv[optind];
2596 loc_set_cmdline(argv, optind, 1);
2598 /* Treat --foo the same as -foo. */
2601 popt = qemu_options;
2604 error_report("invalid option");
2607 if (!strcmp(popt->name, r + 1))
2611 if (popt->flags & HAS_ARG) {
2612 if (optind >= argc) {
2613 error_report("requires an argument");
2616 optarg = argv[optind++];
2617 loc_set_cmdline(argv, optind - 2, 2);
2628 static MachineClass *select_machine(void)
2630 MachineClass *machine_class = find_default_machine();
2635 loc_push_none(&loc);
2637 opts = qemu_get_machine_opts();
2638 qemu_opts_loc_restore(opts);
2640 optarg = qemu_opt_get(opts, "type");
2642 machine_class = machine_parse(optarg);
2645 if (!machine_class) {
2646 error_report("No machine specified, and there is no default");
2647 error_printf("Use -machine help to list supported machines\n");
2652 return machine_class;
2655 static int machine_set_property(void *opaque,
2656 const char *name, const char *value,
2659 Object *obj = OBJECT(opaque);
2660 Error *local_err = NULL;
2663 if (strcmp(name, "type") == 0) {
2667 qom_name = g_strdup(name);
2668 for (p = qom_name; *p; p++) {
2674 object_property_parse(obj, value, qom_name, &local_err);
2678 error_report_err(local_err);
2687 * Initial object creation happens before all other
2688 * QEMU data types are created. The majority of objects
2689 * can be created at this point. The rng-egd object
2690 * cannot be created here, as it depends on the chardev
2693 static bool object_create_initial(const char *type)
2695 if (g_str_equal(type, "rng-egd") ||
2696 g_str_has_prefix(type, "pr-manager-")) {
2700 #if defined(CONFIG_VHOST_USER) && defined(CONFIG_LINUX)
2701 if (g_str_equal(type, "cryptodev-vhost-user")) {
2707 * return false for concrete netfilters since
2708 * they depend on netdevs already existing
2710 if (g_str_equal(type, "filter-buffer") ||
2711 g_str_equal(type, "filter-dump") ||
2712 g_str_equal(type, "filter-mirror") ||
2713 g_str_equal(type, "filter-redirector") ||
2714 g_str_equal(type, "colo-compare") ||
2715 g_str_equal(type, "filter-rewriter") ||
2716 g_str_equal(type, "filter-replay")) {
2720 /* Memory allocation by backends needs to be done
2721 * after configure_accelerator() (due to the tcg_enabled()
2722 * checks at memory_region_init_*()).
2724 * Also, allocation of large amounts of memory may delay
2725 * chardev initialization for too long, and trigger timeouts
2726 * on software that waits for a monitor socket to be created
2729 if (g_str_has_prefix(type, "memory-backend-")) {
2738 * The remainder of object creation happens after the
2739 * creation of chardev, fsdev, net clients and device data types.
2741 static bool object_create_delayed(const char *type)
2743 return !object_create_initial(type);
2747 static void set_memory_options(uint64_t *ram_slots, ram_addr_t *maxram_size,
2751 const char *mem_str;
2752 const ram_addr_t default_ram_size = mc->default_ram_size;
2753 QemuOpts *opts = qemu_find_opts_singleton("memory");
2756 loc_push_none(&loc);
2757 qemu_opts_loc_restore(opts);
2760 mem_str = qemu_opt_get(opts, "size");
2763 error_report("missing 'size' option value");
2767 sz = qemu_opt_get_size(opts, "size", ram_size);
2769 /* Fix up legacy suffix-less format */
2770 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2771 uint64_t overflow_check = sz;
2774 if ((sz >> 20) != overflow_check) {
2775 error_report("too large 'size' option value");
2781 /* backward compatibility behaviour for case "-m 0" */
2783 sz = default_ram_size;
2786 sz = QEMU_ALIGN_UP(sz, 8192);
2788 if (ram_size != sz) {
2789 error_report("ram size too large");
2793 /* store value for the future use */
2794 qemu_opt_set_number(opts, "size", ram_size, &error_abort);
2795 *maxram_size = ram_size;
2797 if (qemu_opt_get(opts, "maxmem")) {
2800 sz = qemu_opt_get_size(opts, "maxmem", 0);
2801 slots = qemu_opt_get_number(opts, "slots", 0);
2802 if (sz < ram_size) {
2803 error_report("invalid value of -m option maxmem: "
2804 "maximum memory size (0x%" PRIx64 ") must be at least "
2805 "the initial memory size (0x" RAM_ADDR_FMT ")",
2808 } else if (slots && sz == ram_size) {
2809 error_report("invalid value of -m option maxmem: "
2810 "memory slots were specified but maximum memory size "
2811 "(0x%" PRIx64 ") is equal to the initial memory size "
2812 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
2818 } else if (qemu_opt_get(opts, "slots")) {
2819 error_report("invalid -m option value: missing 'maxmem' option");
2826 static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
2830 g = g_malloc0(sizeof(*g));
2831 g->driver = qemu_opt_get(opts, "driver");
2832 g->property = qemu_opt_get(opts, "property");
2833 g->value = qemu_opt_get(opts, "value");
2834 g->user_provided = true;
2835 g->errp = &error_fatal;
2836 qdev_prop_register_global(g);
2840 static int qemu_read_default_config_file(void)
2844 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
2845 if (ret < 0 && ret != -ENOENT) {
2852 static void user_register_global_props(void)
2854 qemu_opts_foreach(qemu_find_opts("global"),
2855 global_init_func, NULL, NULL);
2859 * Note: we should see that these properties are actually having a
2860 * priority: accel < machine < user. This means e.g. when user
2861 * specifies something in "-global", it'll always be used with highest
2862 * priority than either machine/accelerator compat properties.
2864 static void register_global_properties(MachineState *ms)
2866 accel_register_compat_props(ms->accelerator);
2867 machine_register_compat_props(ms);
2868 user_register_global_props();
2871 int main(int argc, char **argv, char **envp)
2874 int snapshot, linux_boot;
2875 const char *initrd_filename;
2876 const char *kernel_filename, *kernel_cmdline;
2877 const char *boot_order = NULL;
2878 const char *boot_once = NULL;
2880 QemuOpts *opts, *machine_opts;
2881 QemuOpts *icount_opts = NULL, *accel_opts = NULL;
2882 QemuOptsList *olist;
2885 const char *loadvm = NULL;
2886 MachineClass *machine_class;
2887 const char *cpu_model;
2888 const char *vga_model = NULL;
2889 const char *qtest_chrdev = NULL;
2890 const char *qtest_log = NULL;
2891 const char *pid_file = NULL;
2892 const char *incoming = NULL;
2893 bool userconfig = true;
2894 bool nographic = false;
2895 int display_remote = 0;
2896 const char *log_mask = NULL;
2897 const char *log_file = NULL;
2898 char *trace_file = NULL;
2899 ram_addr_t maxram_size;
2900 uint64_t ram_slots = 0;
2901 FILE *vmstate_dump_file = NULL;
2902 Error *main_loop_err = NULL;
2904 bool list_data_dirs = false;
2906 typedef struct BlockdevOptions_queue {
2907 BlockdevOptions *bdo;
2909 QSIMPLEQ_ENTRY(BlockdevOptions_queue) entry;
2910 } BlockdevOptions_queue;
2911 QSIMPLEQ_HEAD(, BlockdevOptions_queue) bdo_queue
2912 = QSIMPLEQ_HEAD_INITIALIZER(bdo_queue);
2914 module_call_init(MODULE_INIT_TRACE);
2916 qemu_init_cpu_list();
2917 qemu_init_cpu_loop();
2919 qemu_mutex_lock_iothread();
2921 atexit(qemu_run_exit_notifiers);
2922 error_set_progname(argv[0]);
2923 qemu_init_exec_dir(argv[0]);
2925 module_call_init(MODULE_INIT_QOM);
2927 qemu_add_opts(&qemu_drive_opts);
2928 qemu_add_drive_opts(&qemu_legacy_drive_opts);
2929 qemu_add_drive_opts(&qemu_common_drive_opts);
2930 qemu_add_drive_opts(&qemu_drive_opts);
2931 qemu_add_drive_opts(&bdrv_runtime_opts);
2932 qemu_add_opts(&qemu_chardev_opts);
2933 qemu_add_opts(&qemu_device_opts);
2934 qemu_add_opts(&qemu_netdev_opts);
2935 qemu_add_opts(&qemu_nic_opts);
2936 qemu_add_opts(&qemu_net_opts);
2937 qemu_add_opts(&qemu_rtc_opts);
2938 qemu_add_opts(&qemu_global_opts);
2939 qemu_add_opts(&qemu_mon_opts);
2940 qemu_add_opts(&qemu_trace_opts);
2941 qemu_add_opts(&qemu_option_rom_opts);
2942 qemu_add_opts(&qemu_machine_opts);
2943 qemu_add_opts(&qemu_accel_opts);
2944 qemu_add_opts(&qemu_mem_opts);
2945 qemu_add_opts(&qemu_smp_opts);
2946 qemu_add_opts(&qemu_boot_opts);
2947 qemu_add_opts(&qemu_add_fd_opts);
2948 qemu_add_opts(&qemu_object_opts);
2949 qemu_add_opts(&qemu_tpmdev_opts);
2950 qemu_add_opts(&qemu_realtime_opts);
2951 qemu_add_opts(&qemu_msg_opts);
2952 qemu_add_opts(&qemu_name_opts);
2953 qemu_add_opts(&qemu_numa_opts);
2954 qemu_add_opts(&qemu_icount_opts);
2955 qemu_add_opts(&qemu_semihosting_config_opts);
2956 qemu_add_opts(&qemu_fw_cfg_opts);
2957 module_call_init(MODULE_INIT_OPTS);
2960 postcopy_infrastructure_init();
2962 if (qcrypto_init(&err) < 0) {
2963 error_reportf_err(err, "cannot initialize crypto: ");
2966 rtc_clock = QEMU_CLOCK_HOST;
2968 QLIST_INIT (&vm_change_state_head);
2969 os_setup_early_signal_handling();
2976 bdrv_init_with_whitelist();
2980 /* first pass of option parsing */
2982 while (optind < argc) {
2983 if (argv[optind][0] != '-') {
2987 const QEMUOption *popt;
2989 popt = lookup_opt(argc, argv, &optarg, &optind);
2990 switch (popt->index) {
2991 case QEMU_OPTION_nodefconfig:
2992 case QEMU_OPTION_nouserconfig:
3000 if (qemu_read_default_config_file() < 0) {
3005 /* second pass of option parsing */
3010 if (argv[optind][0] != '-') {
3011 drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
3013 const QEMUOption *popt;
3015 popt = lookup_opt(argc, argv, &optarg, &optind);
3016 if (!(popt->arch_mask & arch_type)) {
3017 error_report("Option not supported for this target");
3020 switch(popt->index) {
3021 case QEMU_OPTION_cpu:
3022 /* hw initialization will check this */
3025 case QEMU_OPTION_hda:
3026 case QEMU_OPTION_hdb:
3027 case QEMU_OPTION_hdc:
3028 case QEMU_OPTION_hdd:
3029 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
3032 case QEMU_OPTION_blockdev:
3035 BlockdevOptions_queue *bdo;
3037 v = qobject_input_visitor_new_str(optarg, "driver", &err);
3039 error_report_err(err);
3043 bdo = g_new(BlockdevOptions_queue, 1);
3044 visit_type_BlockdevOptions(v, NULL, &bdo->bdo,
3047 loc_save(&bdo->loc);
3048 QSIMPLEQ_INSERT_TAIL(&bdo_queue, bdo, entry);
3051 case QEMU_OPTION_drive:
3052 if (drive_def(optarg) == NULL) {
3056 case QEMU_OPTION_set:
3057 if (qemu_set_option(optarg) != 0)
3060 case QEMU_OPTION_global:
3061 if (qemu_global_option(optarg) != 0)
3064 case QEMU_OPTION_mtdblock:
3065 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3067 case QEMU_OPTION_sd:
3068 drive_add(IF_SD, -1, optarg, SD_OPTS);
3070 case QEMU_OPTION_pflash:
3071 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
3073 case QEMU_OPTION_snapshot:
3076 case QEMU_OPTION_numa:
3077 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
3083 case QEMU_OPTION_display:
3084 parse_display(optarg);
3086 case QEMU_OPTION_nographic:
3087 olist = qemu_find_opts("machine");
3088 qemu_opts_parse_noisily(olist, "graphics=off", false);
3090 dpy.type = DISPLAY_TYPE_NONE;
3092 case QEMU_OPTION_curses:
3093 #ifdef CONFIG_CURSES
3094 dpy.type = DISPLAY_TYPE_CURSES;
3096 error_report("curses support is disabled");
3100 case QEMU_OPTION_portrait:
3101 graphic_rotate = 90;
3103 case QEMU_OPTION_rotate:
3104 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3105 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3106 graphic_rotate != 180 && graphic_rotate != 270) {
3107 error_report("only 90, 180, 270 deg rotation is available");
3111 case QEMU_OPTION_kernel:
3112 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg,
3115 case QEMU_OPTION_initrd:
3116 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg,
3119 case QEMU_OPTION_append:
3120 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg,
3123 case QEMU_OPTION_dtb:
3124 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg,
3127 case QEMU_OPTION_cdrom:
3128 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3130 case QEMU_OPTION_boot:
3131 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
3137 case QEMU_OPTION_fda:
3138 case QEMU_OPTION_fdb:
3139 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3142 case QEMU_OPTION_no_fd_bootchk:
3145 case QEMU_OPTION_netdev:
3147 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3151 case QEMU_OPTION_nic:
3153 if (net_client_parse(qemu_find_opts("nic"), optarg) == -1) {
3157 case QEMU_OPTION_net:
3159 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3163 #ifdef CONFIG_LIBISCSI
3164 case QEMU_OPTION_iscsi:
3165 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
3173 case QEMU_OPTION_tftp:
3174 error_report("The -tftp option is deprecated. "
3175 "Please use '-netdev user,tftp=...' instead.");
3176 legacy_tftp_prefix = optarg;
3178 case QEMU_OPTION_bootp:
3179 error_report("The -bootp option is deprecated. "
3180 "Please use '-netdev user,bootfile=...' instead.");
3181 legacy_bootp_filename = optarg;
3183 case QEMU_OPTION_redir:
3184 error_report("The -redir option is deprecated. "
3185 "Please use '-netdev user,hostfwd=...' instead.");
3186 if (net_slirp_redir(optarg) < 0)
3190 case QEMU_OPTION_bt:
3191 add_device_config(DEV_BT, optarg);
3193 case QEMU_OPTION_audio_help:
3197 case QEMU_OPTION_soundhw:
3198 select_soundhw (optarg);
3203 case QEMU_OPTION_version:
3208 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
3215 case QEMU_OPTION_tpmdev:
3216 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3221 case QEMU_OPTION_mempath:
3224 case QEMU_OPTION_mem_prealloc:
3233 case QEMU_OPTION_DFILTER:
3234 qemu_set_dfilter_ranges(optarg, &error_fatal);
3237 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3239 case QEMU_OPTION_gdb:
3240 add_device_config(DEV_GDB, optarg);
3243 if (is_help_option(optarg)) {
3244 list_data_dirs = true;
3246 qemu_add_data_dir(optarg);
3249 case QEMU_OPTION_bios:
3250 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg,
3253 case QEMU_OPTION_singlestep:
3260 keyboard_layout = optarg;
3262 case QEMU_OPTION_localtime:
3264 warn_report("This option is deprecated, "
3265 "use '-rtc base=localtime' instead.");
3267 case QEMU_OPTION_vga:
3276 w = strtol(p, (char **)&p, 10);
3279 error_report("invalid resolution or depth");
3285 h = strtol(p, (char **)&p, 10);
3290 depth = strtol(p, (char **)&p, 10);
3291 if (depth != 8 && depth != 15 && depth != 16 &&
3292 depth != 24 && depth != 32)
3294 } else if (*p == '\0') {
3295 depth = graphic_depth;
3302 graphic_depth = depth;
3305 case QEMU_OPTION_echr:
3308 term_escape_char = strtol(optarg, &r, 0);
3310 printf("Bad argument to echr\n");
3313 case QEMU_OPTION_monitor:
3314 default_monitor = 0;
3315 if (strncmp(optarg, "none", 4)) {
3316 monitor_parse(optarg, "readline", false);
3319 case QEMU_OPTION_qmp:
3320 monitor_parse(optarg, "control", false);
3321 default_monitor = 0;
3323 case QEMU_OPTION_qmp_pretty:
3324 monitor_parse(optarg, "control", true);
3325 default_monitor = 0;
3327 case QEMU_OPTION_mon:
3328 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3333 default_monitor = 0;
3335 case QEMU_OPTION_chardev:
3336 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3342 case QEMU_OPTION_fsdev:
3343 olist = qemu_find_opts("fsdev");
3345 error_report("fsdev support is disabled");
3348 opts = qemu_opts_parse_noisily(olist, optarg, true);
3353 case QEMU_OPTION_virtfs: {
3356 const char *writeout, *sock_fd, *socket, *path, *security_model;
3358 olist = qemu_find_opts("virtfs");
3360 error_report("virtfs support is disabled");
3363 opts = qemu_opts_parse_noisily(olist, optarg, true);
3368 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3369 qemu_opt_get(opts, "mount_tag") == NULL) {
3370 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3373 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3374 qemu_opts_id(opts) ?:
3375 qemu_opt_get(opts, "mount_tag"),
3378 error_report("duplicate or invalid fsdev id: %s",
3379 qemu_opt_get(opts, "mount_tag"));
3383 writeout = qemu_opt_get(opts, "writeout");
3385 #ifdef CONFIG_SYNC_FILE_RANGE
3386 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3388 error_report("writeout=immediate not supported "
3389 "on this platform");
3393 qemu_opt_set(fsdev, "fsdriver",
3394 qemu_opt_get(opts, "fsdriver"), &error_abort);
3395 path = qemu_opt_get(opts, "path");
3397 qemu_opt_set(fsdev, "path", path, &error_abort);
3399 security_model = qemu_opt_get(opts, "security_model");
3400 if (security_model) {
3401 qemu_opt_set(fsdev, "security_model", security_model,
3404 socket = qemu_opt_get(opts, "socket");
3406 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3408 sock_fd = qemu_opt_get(opts, "sock_fd");
3410 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3413 qemu_opt_set_bool(fsdev, "readonly",
3414 qemu_opt_get_bool(opts, "readonly", 0),
3416 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3418 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3419 qemu_opt_set(device, "fsdev",
3420 qemu_opts_id(fsdev), &error_abort);
3421 qemu_opt_set(device, "mount_tag",
3422 qemu_opt_get(opts, "mount_tag"), &error_abort);
3425 case QEMU_OPTION_virtfs_synth: {
3429 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3432 error_report("duplicate option: %s", "virtfs_synth");
3435 qemu_opt_set(fsdev, "fsdriver", "synth", &error_abort);
3437 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3439 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3440 qemu_opt_set(device, "fsdev", "v_synth", &error_abort);
3441 qemu_opt_set(device, "mount_tag", "v_synth", &error_abort);
3444 case QEMU_OPTION_serial:
3445 add_device_config(DEV_SERIAL, optarg);
3447 if (strncmp(optarg, "mon:", 4) == 0) {
3448 default_monitor = 0;
3451 case QEMU_OPTION_watchdog:
3453 error_report("only one watchdog option may be given");
3458 case QEMU_OPTION_watchdog_action:
3459 if (select_watchdog_action(optarg) == -1) {
3460 error_report("unknown -watchdog-action parameter");
3464 case QEMU_OPTION_virtiocon:
3465 warn_report("This option is deprecated, "
3466 "use '-device virtconsole' instead");
3467 add_device_config(DEV_VIRTCON, optarg);
3468 default_virtcon = 0;
3469 if (strncmp(optarg, "mon:", 4) == 0) {
3470 default_monitor = 0;
3473 case QEMU_OPTION_parallel:
3474 add_device_config(DEV_PARALLEL, optarg);
3475 default_parallel = 0;
3476 if (strncmp(optarg, "mon:", 4) == 0) {
3477 default_monitor = 0;
3480 case QEMU_OPTION_debugcon:
3481 add_device_config(DEV_DEBUGCON, optarg);
3483 case QEMU_OPTION_loadvm:
3486 case QEMU_OPTION_full_screen:
3487 dpy.has_full_screen = true;
3488 dpy.full_screen = true;
3490 case QEMU_OPTION_no_frame:
3491 g_printerr("The -no-frame switch is deprecated, and will be\n"
3492 "removed in a future release.\n");
3495 case QEMU_OPTION_alt_grab:
3498 case QEMU_OPTION_ctrl_grab:
3501 case QEMU_OPTION_no_quit:
3502 dpy.has_window_close = true;
3503 dpy.window_close = false;
3505 case QEMU_OPTION_sdl:
3507 dpy.type = DISPLAY_TYPE_SDL;
3510 error_report("SDL support is disabled");
3513 case QEMU_OPTION_pidfile:
3516 case QEMU_OPTION_win2k_hack:
3517 win2k_install_hack = 1;
3519 case QEMU_OPTION_rtc_td_hack: {
3520 static GlobalProperty slew_lost_ticks = {
3521 .driver = "mc146818rtc",
3522 .property = "lost_tick_policy",
3526 qdev_prop_register_global(&slew_lost_ticks);
3527 warn_report("This option is deprecated, "
3528 "use '-rtc driftfix=slew' instead.");
3531 case QEMU_OPTION_acpitable:
3532 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3537 acpi_table_add(opts, &error_fatal);
3539 case QEMU_OPTION_smbios:
3540 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3545 smbios_entry_add(opts, &error_fatal);
3547 case QEMU_OPTION_fwcfg:
3548 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3554 case QEMU_OPTION_enable_kvm:
3555 olist = qemu_find_opts("machine");
3556 qemu_opts_parse_noisily(olist, "accel=kvm", false);
3558 case QEMU_OPTION_enable_hax:
3559 olist = qemu_find_opts("machine");
3560 qemu_opts_parse_noisily(olist, "accel=hax", false);
3563 case QEMU_OPTION_machine:
3564 olist = qemu_find_opts("machine");
3565 opts = qemu_opts_parse_noisily(olist, optarg, true);
3570 case QEMU_OPTION_no_kvm:
3571 olist = qemu_find_opts("machine");
3572 qemu_opts_parse_noisily(olist, "accel=tcg", false);
3574 case QEMU_OPTION_accel:
3575 accel_opts = qemu_opts_parse_noisily(qemu_find_opts("accel"),
3577 optarg = qemu_opt_get(accel_opts, "accel");
3578 if (!optarg || is_help_option(optarg)) {
3579 error_printf("Possible accelerators: kvm, xen, hax, tcg\n");
3582 opts = qemu_opts_create(qemu_find_opts("machine"), NULL,
3583 false, &error_abort);
3584 qemu_opt_set(opts, "accel", optarg, &error_abort);
3586 case QEMU_OPTION_usb:
3587 olist = qemu_find_opts("machine");
3588 qemu_opts_parse_noisily(olist, "usb=on", false);
3590 case QEMU_OPTION_usbdevice:
3591 error_report("'-usbdevice' is deprecated, please use "
3592 "'-device usb-...' instead");
3593 olist = qemu_find_opts("machine");
3594 qemu_opts_parse_noisily(olist, "usb=on", false);
3595 add_device_config(DEV_USB, optarg);
3597 case QEMU_OPTION_device:
3598 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3603 case QEMU_OPTION_smp:
3604 if (!qemu_opts_parse_noisily(qemu_find_opts("smp-opts"),
3609 case QEMU_OPTION_vnc:
3610 vnc_parse(optarg, &error_fatal);
3612 case QEMU_OPTION_no_acpi:
3615 case QEMU_OPTION_no_hpet:
3618 case QEMU_OPTION_balloon:
3619 if (balloon_parse(optarg) < 0) {
3620 error_report("unknown -balloon argument %s", optarg);
3624 case QEMU_OPTION_no_reboot:
3627 case QEMU_OPTION_no_shutdown:
3630 case QEMU_OPTION_show_cursor:
3633 case QEMU_OPTION_uuid:
3634 if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
3635 error_report("failed to parse UUID string: wrong format");
3638 qemu_uuid_set = true;
3640 case QEMU_OPTION_option_rom:
3641 if (nb_option_roms >= MAX_OPTION_ROMS) {
3642 error_report("too many option ROMs");
3645 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3650 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3651 option_rom[nb_option_roms].bootindex =
3652 qemu_opt_get_number(opts, "bootindex", -1);
3653 if (!option_rom[nb_option_roms].name) {
3654 error_report("Option ROM file is not specified");
3659 case QEMU_OPTION_semihosting:
3660 semihosting.enabled = true;
3661 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3663 case QEMU_OPTION_semihosting_config:
3664 semihosting.enabled = true;
3665 opts = qemu_opts_parse_noisily(qemu_find_opts("semihosting-config"),
3668 semihosting.enabled = qemu_opt_get_bool(opts, "enable",
3670 const char *target = qemu_opt_get(opts, "target");
3671 if (target != NULL) {
3672 if (strcmp("native", target) == 0) {
3673 semihosting.target = SEMIHOSTING_TARGET_NATIVE;
3674 } else if (strcmp("gdb", target) == 0) {
3675 semihosting.target = SEMIHOSTING_TARGET_GDB;
3676 } else if (strcmp("auto", target) == 0) {
3677 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3679 error_report("unsupported semihosting-config %s",
3684 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3686 /* Set semihosting argument count and vector */
3687 qemu_opt_foreach(opts, add_semihosting_arg,
3688 &semihosting, NULL);
3690 error_report("unsupported semihosting-config %s", optarg);
3694 case QEMU_OPTION_name:
3695 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3701 case QEMU_OPTION_prom_env:
3702 if (nb_prom_envs >= MAX_PROM_ENVS) {
3703 error_report("too many prom variables");
3706 prom_envs[nb_prom_envs] = optarg;
3709 case QEMU_OPTION_old_param:
3712 case QEMU_OPTION_clock:
3713 /* Clock options no longer exist. Keep this option for
3714 * backward compatibility.
3716 warn_report("This option is ignored and will be removed soon");
3718 case QEMU_OPTION_startdate:
3719 warn_report("This option is deprecated, use '-rtc base=' instead.");
3720 configure_rtc_date_offset(optarg, 1);
3722 case QEMU_OPTION_rtc:
3723 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3728 configure_rtc(opts);
3730 case QEMU_OPTION_tb_size:
3732 error_report("TCG is disabled");
3735 if (qemu_strtoul(optarg, NULL, 0, &tcg_tb_size) < 0) {
3736 error_report("Invalid argument to -tb-size");
3740 case QEMU_OPTION_icount:
3741 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3747 case QEMU_OPTION_incoming:
3749 runstate_set(RUN_STATE_INMIGRATE);
3753 case QEMU_OPTION_only_migratable:
3755 * TODO: we can remove this option one day, and we
3758 * "-global migration.only-migratable=true"
3760 qemu_global_option("migration.only-migratable=true");
3762 case QEMU_OPTION_nodefaults:
3765 case QEMU_OPTION_xen_domid:
3766 if (!(xen_available())) {
3767 error_report("Option not supported for this target");
3770 xen_domid = atoi(optarg);
3772 case QEMU_OPTION_xen_create:
3773 if (!(xen_available())) {
3774 error_report("Option not supported for this target");
3777 xen_mode = XEN_CREATE;
3779 case QEMU_OPTION_xen_attach:
3780 if (!(xen_available())) {
3781 error_report("Option not supported for this target");
3784 xen_mode = XEN_ATTACH;
3786 case QEMU_OPTION_xen_domid_restrict:
3787 if (!(xen_available())) {
3788 error_report("Option not supported for this target");
3791 xen_domid_restrict = true;
3793 case QEMU_OPTION_trace:
3795 trace_file = trace_opt_parse(optarg);
3797 case QEMU_OPTION_readconfig:
3799 int ret = qemu_read_config_file(optarg);
3801 error_report("read config %s: %s", optarg,
3807 case QEMU_OPTION_spice:
3808 olist = qemu_find_opts("spice");
3810 error_report("spice support is disabled");
3813 opts = qemu_opts_parse_noisily(olist, optarg, false);
3819 case QEMU_OPTION_writeconfig:
3822 if (strcmp(optarg, "-") == 0) {
3825 fp = fopen(optarg, "w");
3827 error_report("open %s: %s", optarg,
3832 qemu_config_write(fp);
3838 case QEMU_OPTION_qtest:
3839 qtest_chrdev = optarg;
3841 case QEMU_OPTION_qtest_log:
3844 case QEMU_OPTION_sandbox:
3845 #ifdef CONFIG_SECCOMP
3846 opts = qemu_opts_parse_noisily(qemu_find_opts("sandbox"),
3852 error_report("-sandbox support is not enabled "
3853 "in this QEMU binary");
3857 case QEMU_OPTION_add_fd:
3859 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
3865 error_report("File descriptor passing is disabled on this "
3870 case QEMU_OPTION_object:
3871 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
3877 case QEMU_OPTION_realtime:
3878 opts = qemu_opts_parse_noisily(qemu_find_opts("realtime"),
3883 enable_mlock = qemu_opt_get_bool(opts, "mlock", true);
3885 case QEMU_OPTION_msg:
3886 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
3891 configure_msg(opts);
3893 case QEMU_OPTION_dump_vmstate:
3894 if (vmstate_dump_file) {
3895 error_report("only one '-dump-vmstate' "
3896 "option may be given");
3899 vmstate_dump_file = fopen(optarg, "w");
3900 if (vmstate_dump_file == NULL) {
3901 error_report("open %s: %s", optarg, strerror(errno));
3905 case QEMU_OPTION_nodefconfig:
3906 case QEMU_OPTION_nouserconfig:
3907 /* Nothing to be parsed here. Especially, do not error out below. */
3910 if (os_parse_cmd_args(popt->index, optarg)) {
3911 error_report("Option not supported in this build");
3918 * Clear error location left behind by the loop.
3919 * Best done right after the loop. Do not insert code here!
3923 replay_configure(icount_opts);
3925 machine_class = select_machine();
3927 set_memory_options(&ram_slots, &maxram_size, machine_class);
3930 rcu_disable_atfork();
3932 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
3933 error_report("could not acquire pid file: %s", strerror(errno));
3937 if (qemu_init_main_loop(&main_loop_err)) {
3938 error_report_err(main_loop_err);
3942 #ifdef CONFIG_SECCOMP
3943 if (qemu_opts_foreach(qemu_find_opts("sandbox"),
3944 parse_sandbox, NULL, NULL)) {
3949 if (qemu_opts_foreach(qemu_find_opts("name"),
3950 parse_name, NULL, NULL)) {
3955 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
3956 parse_add_fd, NULL, NULL)) {
3960 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
3961 cleanup_add_fd, NULL, NULL)) {
3966 current_machine = MACHINE(object_new(object_class_get_name(
3967 OBJECT_CLASS(machine_class))));
3968 if (machine_help_func(qemu_get_machine_opts(), current_machine)) {
3971 object_property_add_child(object_get_root(), "machine",
3972 OBJECT(current_machine), &error_abort);
3974 if (machine_class->minimum_page_bits) {
3975 if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
3976 /* This would be a board error: specifying a minimum smaller than
3977 * a target's compile-time fixed setting.
3979 g_assert_not_reached();
3983 cpu_exec_init_all();
3985 if (machine_class->hw_version) {
3986 qemu_set_hw_version(machine_class->hw_version);
3989 if (cpu_model && is_help_option(cpu_model)) {
3990 list_cpus(stdout, &fprintf, cpu_model);
3994 if (!trace_init_backends()) {
3997 trace_init_file(trace_file);
3999 /* Open the logfile at this point and set the log mask if necessary.
4002 qemu_set_log_filename(log_file, &error_fatal);
4007 mask = qemu_str_to_log_mask(log_mask);
4009 qemu_print_log_usage(stdout);
4017 /* add configured firmware directories */
4018 dirs = g_strsplit(CONFIG_QEMU_FIRMWAREPATH, G_SEARCHPATH_SEPARATOR_S, 0);
4019 for (i = 0; dirs[i] != NULL; i++) {
4020 qemu_add_data_dir(dirs[i]);
4024 /* try to find datadir relative to the executable path */
4025 dir = os_find_datadir();
4026 qemu_add_data_dir(dir);
4029 /* add the datadir specified when building */
4030 qemu_add_data_dir(CONFIG_QEMU_DATADIR);
4032 /* -L help lists the data directories and exits. */
4033 if (list_data_dirs) {
4034 for (i = 0; i < data_dir_idx; i++) {
4035 printf("%s\n", data_dir[i]);
4040 /* machine_class: default to UP */
4041 machine_class->max_cpus = machine_class->max_cpus ?: 1;
4042 machine_class->min_cpus = machine_class->min_cpus ?: 1;
4043 machine_class->default_cpus = machine_class->default_cpus ?: 1;
4045 /* default to machine_class->default_cpus */
4046 smp_cpus = machine_class->default_cpus;
4047 max_cpus = machine_class->default_cpus;
4049 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
4051 /* sanity-check smp_cpus and max_cpus against machine_class */
4052 if (smp_cpus < machine_class->min_cpus) {
4053 error_report("Invalid SMP CPUs %d. The min CPUs "
4054 "supported by machine '%s' is %d", smp_cpus,
4055 machine_class->name, machine_class->min_cpus);
4058 if (max_cpus > machine_class->max_cpus) {
4059 error_report("Invalid SMP CPUs %d. The max CPUs "
4060 "supported by machine '%s' is %d", max_cpus,
4061 machine_class->name, machine_class->max_cpus);
4066 * Get the default machine options from the machine if it is not already
4067 * specified either by the configuration file or by the command line.
4069 if (machine_class->default_machine_opts) {
4070 qemu_opts_set_defaults(qemu_find_opts("machine"),
4071 machine_class->default_machine_opts, 0);
4074 qemu_opts_foreach(qemu_find_opts("device"),
4075 default_driver_check, NULL, NULL);
4076 qemu_opts_foreach(qemu_find_opts("global"),
4077 default_driver_check, NULL, NULL);
4079 if (!vga_model && !default_vga) {
4080 vga_interface_type = VGA_DEVICE;
4082 if (!has_defaults || machine_class->no_serial) {
4085 if (!has_defaults || machine_class->no_parallel) {
4086 default_parallel = 0;
4088 if (!has_defaults || !machine_class->use_virtcon) {
4089 default_virtcon = 0;
4091 if (!has_defaults || machine_class->no_floppy) {
4094 if (!has_defaults || machine_class->no_cdrom) {
4097 if (!has_defaults || machine_class->no_sdcard) {
4100 if (!has_defaults) {
4101 default_monitor = 0;
4106 if (is_daemonized()) {
4107 /* According to documentation and historically, -nographic redirects
4108 * serial port, parallel port and monitor to stdio, which does not work
4109 * with -daemonize. We can redirect these to null instead, but since
4110 * -nographic is legacy, let's just error out.
4111 * We disallow -nographic only if all other ports are not redirected
4112 * explicitly, to not break existing legacy setups which uses
4113 * -nographic _and_ redirects all ports explicitly - this is valid
4114 * usage, -nographic is just a no-op in this case.
4117 && (default_parallel || default_serial
4118 || default_monitor || default_virtcon)) {
4119 error_report("-nographic cannot be used with -daemonize");
4122 #ifdef CONFIG_CURSES
4123 if (dpy.type == DISPLAY_TYPE_CURSES) {
4124 error_report("curses display cannot be used with -daemonize");
4131 if (default_parallel)
4132 add_device_config(DEV_PARALLEL, "null");
4133 if (default_serial && default_monitor) {
4134 add_device_config(DEV_SERIAL, "mon:stdio");
4135 } else if (default_virtcon && default_monitor) {
4136 add_device_config(DEV_VIRTCON, "mon:stdio");
4139 add_device_config(DEV_SERIAL, "stdio");
4140 if (default_virtcon)
4141 add_device_config(DEV_VIRTCON, "stdio");
4142 if (default_monitor)
4143 monitor_parse("stdio", "readline", false);
4147 add_device_config(DEV_SERIAL, "vc:80Cx24C");
4148 if (default_parallel)
4149 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
4150 if (default_monitor)
4151 monitor_parse("vc:80Cx24C", "readline", false);
4152 if (default_virtcon)
4153 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
4156 #if defined(CONFIG_VNC)
4157 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
4161 if (dpy.type == DISPLAY_TYPE_DEFAULT && !display_remote) {
4162 if (!qemu_display_find_default(&dpy)) {
4163 dpy.type = DISPLAY_TYPE_NONE;
4164 #if defined(CONFIG_VNC)
4165 vnc_parse("localhost:0,to=99,id=default", &error_abort);
4169 if (dpy.type == DISPLAY_TYPE_DEFAULT) {
4170 dpy.type = DISPLAY_TYPE_NONE;
4173 if ((no_frame || alt_grab || ctrl_grab) && dpy.type != DISPLAY_TYPE_SDL) {
4174 error_report("-no-frame, -alt-grab and -ctrl-grab are only valid "
4175 "for SDL, ignoring option");
4177 if (dpy.has_window_close &&
4178 (dpy.type != DISPLAY_TYPE_GTK && dpy.type != DISPLAY_TYPE_SDL)) {
4179 error_report("-no-quit is only valid for GTK and SDL, "
4183 qemu_display_early_init(&dpy);
4184 qemu_console_early_init();
4186 if (dpy.has_gl && dpy.gl != DISPLAYGL_MODE_OFF && display_opengl == 0) {
4187 #if defined(CONFIG_OPENGL)
4188 error_report("OpenGL is not supported by the display");
4190 error_report("OpenGL support is disabled");
4198 if (qemu_opts_foreach(qemu_find_opts("object"),
4199 user_creatable_add_opts_foreach,
4200 object_create_initial, NULL)) {
4204 if (qemu_opts_foreach(qemu_find_opts("chardev"),
4205 chardev_init_func, NULL, NULL)) {
4209 #ifdef CONFIG_VIRTFS
4210 if (qemu_opts_foreach(qemu_find_opts("fsdev"),
4211 fsdev_init_func, NULL, NULL)) {
4216 if (qemu_opts_foreach(qemu_find_opts("device"),
4217 device_help_func, NULL, NULL)) {
4221 machine_opts = qemu_get_machine_opts();
4222 if (qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
4224 object_unref(OBJECT(current_machine));
4228 configure_accelerator(current_machine);
4231 * Register all the global properties, including accel properties,
4232 * machine properties, and user-specified ones.
4234 register_global_properties(current_machine);
4237 * Migration object can only be created after global properties
4238 * are applied correctly.
4240 migration_object_init();
4243 qtest_init(qtest_chrdev, qtest_log, &error_fatal);
4246 machine_opts = qemu_get_machine_opts();
4247 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4248 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4249 kernel_cmdline = qemu_opt_get(machine_opts, "append");
4250 bios_name = qemu_opt_get(machine_opts, "firmware");
4252 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4254 boot_order = qemu_opt_get(opts, "order");
4256 validate_bootdevices(boot_order, &error_fatal);
4259 boot_once = qemu_opt_get(opts, "once");
4261 validate_bootdevices(boot_once, &error_fatal);
4264 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
4265 boot_strict = qemu_opt_get_bool(opts, "strict", false);
4269 boot_order = machine_class->default_boot_order;
4272 if (!kernel_cmdline) {
4273 kernel_cmdline = "";
4274 current_machine->kernel_cmdline = (char *)kernel_cmdline;
4277 linux_boot = (kernel_filename != NULL);
4279 if (!linux_boot && *kernel_cmdline != '\0') {
4280 error_report("-append only allowed with -kernel option");
4284 if (!linux_boot && initrd_filename != NULL) {
4285 error_report("-initrd only allowed with -kernel option");
4289 if (semihosting_enabled() && !semihosting_get_argc() && kernel_filename) {
4290 /* fall back to the -kernel/-append */
4291 semihosting_arg_fallback(kernel_filename, kernel_cmdline);
4294 os_set_line_buffering();
4296 /* spice needs the timers to be initialized by this point */
4301 if (!tcg_enabled()) {
4302 error_report("-icount is not allowed with hardware virtualization");
4305 configure_icount(icount_opts, &error_abort);
4306 qemu_opts_del(icount_opts);
4309 if (tcg_enabled()) {
4310 qemu_tcg_configure(accel_opts, &error_fatal);
4314 QemuOptsList *net = qemu_find_opts("net");
4315 qemu_opts_set(net, NULL, "type", "nic", &error_abort);
4317 qemu_opts_set(net, NULL, "type", "user", &error_abort);
4323 if (net_init_clients(&err) < 0) {
4324 error_report_err(err);
4328 if (qemu_opts_foreach(qemu_find_opts("object"),
4329 user_creatable_add_opts_foreach,
4330 object_create_delayed, NULL)) {
4334 if (tpm_init() < 0) {
4338 /* init the bluetooth world */
4339 if (foreach_device_config(DEV_BT, bt_parse))
4342 if (!xen_enabled()) {
4343 /* On 32-bit hosts, QEMU is limited by virtual address space */
4344 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4345 error_report("at most 2047 MB RAM can be simulated");
4352 dirty_bitmap_mig_init();
4354 /* If the currently selected machine wishes to override the units-per-bus
4355 * property of its default HBA interface type, do so now. */
4356 if (machine_class->units_per_default_bus) {
4357 override_max_devs(machine_class->block_default_type,
4358 machine_class->units_per_default_bus);
4361 /* open the virtual block devices */
4362 while (!QSIMPLEQ_EMPTY(&bdo_queue)) {
4363 BlockdevOptions_queue *bdo = QSIMPLEQ_FIRST(&bdo_queue);
4365 QSIMPLEQ_REMOVE_HEAD(&bdo_queue, entry);
4366 loc_push_restore(&bdo->loc);
4367 qmp_blockdev_add(bdo->bdo, &error_fatal);
4369 qapi_free_BlockdevOptions(bdo->bdo);
4372 if (snapshot || replay_mode != REPLAY_MODE_NONE) {
4373 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
4376 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
4377 &machine_class->block_default_type, NULL)) {
4381 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
4383 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4384 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
4387 * Note: qtest_enabled() (which is used in monitor_qapi_event_init())
4388 * depends on configure_accelerator() above.
4390 monitor_init_globals();
4392 if (qemu_opts_foreach(qemu_find_opts("mon"),
4393 mon_init_func, NULL, NULL)) {
4397 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4399 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4401 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4403 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4406 /* If no default VGA is requested, the default is "none". */
4408 if (machine_class->default_display) {
4409 vga_model = machine_class->default_display;
4410 } else if (vga_interface_available(VGA_CIRRUS)) {
4411 vga_model = "cirrus";
4412 } else if (vga_interface_available(VGA_STD)) {
4417 select_vgahw(vga_model);
4421 i = select_watchdog(watchdog);
4423 exit (i == 1 ? 1 : 0);
4426 /* This checkpoint is required by replay to separate prior clock
4427 reading from the other reads, because timer polling functions query
4428 clock values from the log. */
4429 replay_checkpoint(CHECKPOINT_INIT);
4430 qdev_machine_init();
4432 current_machine->ram_size = ram_size;
4433 current_machine->maxram_size = maxram_size;
4434 current_machine->ram_slots = ram_slots;
4435 current_machine->boot_order = boot_order;
4437 /* parse features once if machine provides default cpu_type */
4438 current_machine->cpu_type = machine_class->default_cpu_type;
4440 current_machine->cpu_type = parse_cpu_model(cpu_model);
4442 parse_numa_opts(current_machine);
4444 machine_run_board_init(current_machine);
4450 if (hax_enabled()) {
4454 if (qemu_opts_foreach(qemu_find_opts("fw_cfg"),
4455 parse_fw_cfg, fw_cfg_find(), NULL) != 0) {
4459 /* init USB devices */
4460 if (machine_usb(current_machine)) {
4461 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4465 /* Check if IGD GFX passthrough. */
4468 /* init generic devices */
4469 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
4470 if (qemu_opts_foreach(qemu_find_opts("device"),
4471 device_init_func, NULL, NULL)) {
4475 cpu_synchronize_all_post_init();
4477 rom_reset_order_override();
4479 /* Did we create any drives that we failed to create a device for? */
4480 drive_check_orphaned();
4482 /* Don't warn about the default network setup that you get if
4483 * no command line -net or -netdev options are specified. There
4484 * are two cases that we would otherwise complain about:
4485 * (1) board doesn't support a NIC but the implicit "-net nic"
4487 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
4488 * sets up a nic that isn't connected to anything.
4491 net_check_clients();
4496 qemu_boot_set(boot_once, &error_fatal);
4497 qemu_register_reset(restore_boot_order, g_strdup(boot_order));
4500 /* init local displays */
4501 ds = init_displaystate();
4502 qemu_display_init(ds, &dpy);
4504 /* must be after terminal init, SDL library changes signal handlers */
4505 os_setup_signal_handling();
4507 /* init remote displays */
4509 qemu_opts_foreach(qemu_find_opts("vnc"),
4510 vnc_init_func, NULL, NULL);
4514 qemu_spice_display_init();
4517 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
4521 qdev_machine_creation_done();
4523 /* TODO: once all bus devices are qdevified, this should be done
4524 * when bus is created by qdev.c */
4525 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4526 qemu_run_machine_init_done_notifiers();
4528 if (rom_check_and_register_reset() != 0) {
4529 error_report("rom check and register reset failed");
4535 /* This checkpoint is required by replay to separate prior clock
4536 reading from the other reads, because timer polling functions query
4537 clock values from the log. */
4538 replay_checkpoint(CHECKPOINT_RESET);
4539 qemu_system_reset(SHUTDOWN_CAUSE_NONE);
4540 register_global_state();
4541 if (replay_mode != REPLAY_MODE_NONE) {
4542 replay_vmstate_init();
4543 } else if (loadvm) {
4544 Error *local_err = NULL;
4545 if (load_snapshot(loadvm, &local_err) < 0) {
4546 error_report_err(local_err);
4551 qdev_prop_check_globals();
4552 if (vmstate_dump_file) {
4554 dump_vmstate_json_to_file(vmstate_dump_file);
4559 Error *local_err = NULL;
4560 qemu_start_incoming_migration(incoming, &local_err);
4562 error_reportf_err(local_err, "-incoming %s: ", incoming);
4565 } else if (autostart) {
4569 accel_setup_post(current_machine);
4574 gdbserver_cleanup();
4576 /* No more vcpu or device emulation activity beyond this point */
4579 job_cancel_sync_all();
4584 /* vhost-user must be cleaned up before chardevs. */
4590 user_creatable_cleanup();
4591 migration_object_finalize();
4592 /* TODO: unref root container, check all devices are ok */