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
31 #include "config-host.h"
34 #include "sysemu/seccomp.h"
37 #if defined(CONFIG_VDE)
38 #include <libvdeplug.h>
42 #if defined(__APPLE__) || defined(main)
44 int qemu_main(int argc, char **argv, char **envp);
45 int main(int argc, char **argv)
47 return qemu_main(argc, argv, NULL);
50 #define main qemu_main
52 #endif /* CONFIG_SDL */
56 #define main qemu_main
57 #endif /* CONFIG_COCOA */
61 #include "qemu/sockets.h"
63 #include "hw/boards.h"
64 #include "sysemu/accel.h"
66 #include "hw/pcmcia.h"
67 #include "hw/i386/pc.h"
68 #include "hw/isa/isa.h"
70 #include "sysemu/watchdog.h"
71 #include "hw/i386/smbios.h"
72 #include "hw/xen/xen.h"
74 #include "hw/loader.h"
75 #include "monitor/qdev.h"
76 #include "sysemu/bt.h"
78 #include "net/slirp.h"
79 #include "monitor/monitor.h"
80 #include "ui/console.h"
81 #include "sysemu/sysemu.h"
82 #include "exec/gdbstub.h"
83 #include "qemu/timer.h"
84 #include "sysemu/char.h"
85 #include "qemu/bitmap.h"
86 #include "sysemu/blockdev.h"
87 #include "hw/block/block.h"
88 #include "migration/block.h"
89 #include "sysemu/tpm.h"
90 #include "sysemu/dma.h"
91 #include "audio/audio.h"
92 #include "migration/migration.h"
93 #include "sysemu/kvm.h"
94 #include "qapi/qmp/qjson.h"
95 #include "qemu/option.h"
96 #include "qemu/config-file.h"
97 #include "qemu-options.h"
98 #include "qmp-commands.h"
99 #include "qemu/main-loop.h"
101 #include "fsdev/qemu-fsdev.h"
103 #include "sysemu/qtest.h"
105 #include "disas/disas.h"
108 #include "slirp/libslirp.h"
111 #include "trace/control.h"
112 #include "qemu/queue.h"
113 #include "sysemu/cpus.h"
114 #include "sysemu/arch_init.h"
115 #include "qemu/osdep.h"
117 #include "ui/qemu-spice.h"
118 #include "qapi/string-input-visitor.h"
119 #include "qapi/opts-visitor.h"
120 #include "qom/object_interfaces.h"
121 #include "qapi-event.h"
123 #define DEFAULT_RAM_SIZE 128
125 #define MAX_VIRTIO_CONSOLES 1
126 #define MAX_SCLP_CONSOLES 1
128 static const char *data_dir[16];
129 static int data_dir_idx;
130 const char *bios_name = NULL;
131 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
132 DisplayType display_type = DT_DEFAULT;
133 static int display_remote;
134 const char* keyboard_layout = NULL;
136 const char *mem_path = NULL;
137 int mem_prealloc = 0; /* force preallocation of physical target memory */
138 bool enable_mlock = false;
140 NICInfo nd_table[MAX_NICS];
142 static int rtc_utc = 1;
143 static int rtc_date_offset = -1; /* -1 means no change */
144 QEMUClockType rtc_clock;
145 int vga_interface_type = VGA_NONE;
146 static int full_screen = 0;
147 static int no_frame = 0;
150 static bool grab_on_hover;
152 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
153 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
154 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
155 CharDriverState *sclp_hds[MAX_SCLP_CONSOLES];
156 int win2k_install_hack = 0;
163 const char *vnc_display;
165 int acpi_enabled = 1;
168 static int no_reboot;
171 int graphic_rotate = 0;
172 const char *watchdog;
173 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
175 int semihosting_enabled = 0;
177 const char *qemu_name;
180 unsigned int nb_prom_envs = 0;
181 const char *prom_envs[MAX_PROM_ENVS];
184 uint8_t *boot_splash_filedata;
185 size_t boot_splash_filedata_size;
186 uint8_t qemu_extra_params_fw[2];
188 int icount_align_option;
192 NodeInfo numa_info[MAX_NODES];
194 uint8_t qemu_uuid[16];
197 static QEMUBootSetHandler *boot_set_handler;
198 static void *boot_set_opaque;
200 static NotifierList exit_notifiers =
201 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
203 static NotifierList machine_init_done_notifiers =
204 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
208 enum xen_mode xen_mode = XEN_EMULATE;
210 static int has_defaults = 1;
211 static int default_serial = 1;
212 static int default_parallel = 1;
213 static int default_virtcon = 1;
214 static int default_sclp = 1;
215 static int default_monitor = 1;
216 static int default_floppy = 1;
217 static int default_cdrom = 1;
218 static int default_sdcard = 1;
219 static int default_vga = 1;
225 { .driver = "isa-serial", .flag = &default_serial },
226 { .driver = "isa-parallel", .flag = &default_parallel },
227 { .driver = "isa-fdc", .flag = &default_floppy },
228 { .driver = "ide-cd", .flag = &default_cdrom },
229 { .driver = "ide-hd", .flag = &default_cdrom },
230 { .driver = "ide-drive", .flag = &default_cdrom },
231 { .driver = "scsi-cd", .flag = &default_cdrom },
232 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
233 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
234 { .driver = "virtio-serial", .flag = &default_virtcon },
235 { .driver = "VGA", .flag = &default_vga },
236 { .driver = "isa-vga", .flag = &default_vga },
237 { .driver = "cirrus-vga", .flag = &default_vga },
238 { .driver = "isa-cirrus-vga", .flag = &default_vga },
239 { .driver = "vmware-svga", .flag = &default_vga },
240 { .driver = "qxl-vga", .flag = &default_vga },
243 static QemuOptsList qemu_rtc_opts = {
245 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
249 .type = QEMU_OPT_STRING,
252 .type = QEMU_OPT_STRING,
255 .type = QEMU_OPT_STRING,
257 { /* end of list */ }
261 static QemuOptsList qemu_sandbox_opts = {
263 .implied_opt_name = "enable",
264 .head = QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts.head),
268 .type = QEMU_OPT_BOOL,
270 { /* end of list */ }
274 static QemuOptsList qemu_trace_opts = {
276 .implied_opt_name = "trace",
277 .head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head),
281 .type = QEMU_OPT_STRING,
284 .type = QEMU_OPT_STRING,
286 { /* end of list */ }
290 static QemuOptsList qemu_option_rom_opts = {
291 .name = "option-rom",
292 .implied_opt_name = "romfile",
293 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
297 .type = QEMU_OPT_NUMBER,
300 .type = QEMU_OPT_STRING,
302 { /* end of list */ }
306 static QemuOptsList qemu_machine_opts = {
308 .implied_opt_name = "type",
310 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
314 .type = QEMU_OPT_STRING,
315 .help = "emulated machine"
318 .type = QEMU_OPT_STRING,
319 .help = "accelerator list",
321 .name = "kernel_irqchip",
322 .type = QEMU_OPT_BOOL,
323 .help = "use KVM in-kernel irqchip",
325 .name = "kvm_shadow_mem",
326 .type = QEMU_OPT_SIZE,
327 .help = "KVM shadow MMU size",
330 .type = QEMU_OPT_STRING,
331 .help = "Linux kernel image file",
334 .type = QEMU_OPT_STRING,
335 .help = "Linux initial ramdisk file",
338 .type = QEMU_OPT_STRING,
339 .help = "Linux kernel command line",
342 .type = QEMU_OPT_STRING,
343 .help = "Linux kernel device tree file",
346 .type = QEMU_OPT_STRING,
347 .help = "Dump current dtb to a file and quit",
349 .name = "phandle_start",
350 .type = QEMU_OPT_NUMBER,
351 .help = "The first phandle ID we may generate dynamically",
353 .name = "dt_compatible",
354 .type = QEMU_OPT_STRING,
355 .help = "Overrides the \"compatible\" property of the dt root node",
357 .name = "dump-guest-core",
358 .type = QEMU_OPT_BOOL,
359 .help = "Include guest memory in a core dump",
362 .type = QEMU_OPT_BOOL,
363 .help = "enable/disable memory merge support",
366 .type = QEMU_OPT_BOOL,
367 .help = "Set on/off to enable/disable usb",
370 .type = QEMU_OPT_STRING,
371 .help = "firmware image",
374 .type = QEMU_OPT_STRING,
375 .help = "Specifies the KVM virtualization mode (HV, PR)",
377 .name = PC_MACHINE_MAX_RAM_BELOW_4G,
378 .type = QEMU_OPT_SIZE,
379 .help = "maximum ram below the 4G boundary (32bit boundary)",
382 .type = QEMU_OPT_BOOL,
383 .help = "Set on/off to enable/disable Intel IOMMU (VT-d)",
385 { /* End of list */ }
389 static QemuOptsList qemu_boot_opts = {
391 .implied_opt_name = "order",
393 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
397 .type = QEMU_OPT_STRING,
400 .type = QEMU_OPT_STRING,
403 .type = QEMU_OPT_BOOL,
406 .type = QEMU_OPT_STRING,
408 .name = "splash-time",
409 .type = QEMU_OPT_STRING,
411 .name = "reboot-timeout",
412 .type = QEMU_OPT_STRING,
415 .type = QEMU_OPT_BOOL,
421 static QemuOptsList qemu_add_fd_opts = {
423 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
427 .type = QEMU_OPT_NUMBER,
428 .help = "file descriptor of which a duplicate is added to fd set",
431 .type = QEMU_OPT_NUMBER,
432 .help = "ID of the fd set to add fd to",
435 .type = QEMU_OPT_STRING,
436 .help = "free-form string used to describe fd",
438 { /* end of list */ }
442 static QemuOptsList qemu_object_opts = {
444 .implied_opt_name = "qom-type",
445 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
451 static QemuOptsList qemu_tpmdev_opts = {
453 .implied_opt_name = "type",
454 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
456 /* options are defined in the TPM backends */
457 { /* end of list */ }
461 static QemuOptsList qemu_realtime_opts = {
463 .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
467 .type = QEMU_OPT_BOOL,
469 { /* end of list */ }
473 static QemuOptsList qemu_msg_opts = {
475 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
479 .type = QEMU_OPT_BOOL,
481 { /* end of list */ }
485 static QemuOptsList qemu_name_opts = {
487 .implied_opt_name = "guest",
489 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
493 .type = QEMU_OPT_STRING,
494 .help = "Sets the name of the guest.\n"
495 "This name will be displayed in the SDL window caption.\n"
496 "The name will also be used for the VNC server",
499 .type = QEMU_OPT_STRING,
500 .help = "Sets the name of the QEMU process, as shown in top etc",
502 .name = "debug-threads",
503 .type = QEMU_OPT_BOOL,
504 .help = "When enabled, name the individual threads; defaults off.\n"
505 "NOTE: The thread names are for debugging and not a\n"
508 { /* End of list */ }
512 static QemuOptsList qemu_mem_opts = {
514 .implied_opt_name = "size",
515 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
520 .type = QEMU_OPT_SIZE,
524 .type = QEMU_OPT_NUMBER,
528 .type = QEMU_OPT_SIZE,
530 { /* end of list */ }
534 static QemuOptsList qemu_icount_opts = {
536 .implied_opt_name = "shift",
538 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
542 .type = QEMU_OPT_STRING,
545 .type = QEMU_OPT_BOOL,
547 { /* end of list */ }
552 * Get machine options
554 * Returns: machine options (never null).
556 QemuOpts *qemu_get_machine_opts(void)
558 return qemu_find_opts_singleton("machine");
561 const char *qemu_get_vm_name(void)
566 static void res_free(void)
568 if (boot_splash_filedata != NULL) {
569 g_free(boot_splash_filedata);
570 boot_splash_filedata = NULL;
574 static int default_driver_check(QemuOpts *opts, void *opaque)
576 const char *driver = qemu_opt_get(opts, "driver");
581 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
582 if (strcmp(default_list[i].driver, driver) != 0)
584 *(default_list[i].flag) = 0;
589 /***********************************************************/
592 static RunState current_run_state = RUN_STATE_PRELAUNCH;
594 /* We use RUN_STATE_MAX but any invalid value will do */
595 static RunState vmstop_requested = RUN_STATE_MAX;
596 static QemuMutex vmstop_lock;
601 } RunStateTransition;
603 static const RunStateTransition runstate_transitions_def[] = {
605 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
606 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
608 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
609 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
611 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
612 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
614 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
615 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
617 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
618 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
620 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
621 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
623 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
624 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
625 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
627 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
628 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
630 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
632 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
633 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
634 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
635 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
636 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
637 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
638 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
639 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
640 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
641 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
643 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
645 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
646 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
648 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
649 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
650 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
651 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
653 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
654 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
656 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
657 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
659 { RUN_STATE_MAX, RUN_STATE_MAX },
662 static bool runstate_valid_transitions[RUN_STATE_MAX][RUN_STATE_MAX];
664 bool runstate_check(RunState state)
666 return current_run_state == state;
669 static void runstate_init(void)
671 const RunStateTransition *p;
673 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
674 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE_MAX; p++) {
675 runstate_valid_transitions[p->from][p->to] = true;
678 qemu_mutex_init(&vmstop_lock);
681 /* This function will abort() on invalid state transitions */
682 void runstate_set(RunState new_state)
684 assert(new_state < RUN_STATE_MAX);
686 if (!runstate_valid_transitions[current_run_state][new_state]) {
687 fprintf(stderr, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
688 RunState_lookup[current_run_state],
689 RunState_lookup[new_state]);
692 trace_runstate_set(new_state);
693 current_run_state = new_state;
696 int runstate_is_running(void)
698 return runstate_check(RUN_STATE_RUNNING);
701 bool runstate_needs_reset(void)
703 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
704 runstate_check(RUN_STATE_SHUTDOWN);
707 StatusInfo *qmp_query_status(Error **errp)
709 StatusInfo *info = g_malloc0(sizeof(*info));
711 info->running = runstate_is_running();
712 info->singlestep = singlestep;
713 info->status = current_run_state;
718 static bool qemu_vmstop_requested(RunState *r)
720 qemu_mutex_lock(&vmstop_lock);
721 *r = vmstop_requested;
722 vmstop_requested = RUN_STATE_MAX;
723 qemu_mutex_unlock(&vmstop_lock);
724 return *r < RUN_STATE_MAX;
727 void qemu_system_vmstop_request_prepare(void)
729 qemu_mutex_lock(&vmstop_lock);
732 void qemu_system_vmstop_request(RunState state)
734 vmstop_requested = state;
735 qemu_mutex_unlock(&vmstop_lock);
743 qemu_vmstop_requested(&requested);
744 if (runstate_is_running() && requested == RUN_STATE_MAX) {
748 /* Ensure that a STOP/RESUME pair of events is emitted if a
749 * vmstop request was pending. The BLOCK_IO_ERROR event, for
750 * example, according to documentation is always followed by
753 if (runstate_is_running()) {
754 qapi_event_send_stop(&error_abort);
757 runstate_set(RUN_STATE_RUNNING);
758 vm_state_notify(1, RUN_STATE_RUNNING);
762 qapi_event_send_resume(&error_abort);
766 /***********************************************************/
767 /* real time host monotonic timer */
769 /***********************************************************/
770 /* host time/date access */
771 void qemu_get_timedate(struct tm *tm, int offset)
777 if (rtc_date_offset == -1) {
781 localtime_r(&ti, tm);
783 ti -= rtc_date_offset;
788 int qemu_timedate_diff(struct tm *tm)
792 if (rtc_date_offset == -1)
794 seconds = mktimegm(tm);
797 tmp.tm_isdst = -1; /* use timezone to figure it out */
798 seconds = mktime(&tmp);
801 seconds = mktimegm(tm) + rtc_date_offset;
803 return seconds - time(NULL);
806 static void configure_rtc_date_offset(const char *startdate, int legacy)
808 time_t rtc_start_date;
811 if (!strcmp(startdate, "now") && legacy) {
812 rtc_date_offset = -1;
814 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
822 } else if (sscanf(startdate, "%d-%d-%d",
834 rtc_start_date = mktimegm(&tm);
835 if (rtc_start_date == -1) {
837 fprintf(stderr, "Invalid date format. Valid formats are:\n"
838 "'2006-06-17T16:01:21' or '2006-06-17'\n");
841 rtc_date_offset = time(NULL) - rtc_start_date;
845 static void configure_rtc(QemuOpts *opts)
849 value = qemu_opt_get(opts, "base");
851 if (!strcmp(value, "utc")) {
853 } else if (!strcmp(value, "localtime")) {
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 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
872 value = qemu_opt_get(opts, "driftfix");
874 if (!strcmp(value, "slew")) {
875 static GlobalProperty slew_lost_ticks[] = {
877 .driver = "mc146818rtc",
878 .property = "lost_tick_policy",
881 { /* end of list */ }
884 qdev_prop_register_global_list(slew_lost_ticks);
885 } else if (!strcmp(value, "none")) {
886 /* discard is default */
888 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
894 /***********************************************************/
895 /* Bluetooth support */
898 static struct HCIInfo *hci_table[MAX_NICS];
900 struct HCIInfo *qemu_next_hci(void)
902 if (cur_hci == nb_hcis)
905 return hci_table[cur_hci++];
908 static int bt_hci_parse(const char *str)
913 if (nb_hcis >= MAX_NICS) {
914 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
927 bdaddr.b[5] = 0x56 + nb_hcis;
928 hci->bdaddr_set(hci, bdaddr.b);
930 hci_table[nb_hcis++] = hci;
935 static void bt_vhci_add(int vlan_id)
937 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
940 fprintf(stderr, "qemu: warning: adding a VHCI to "
941 "an empty scatternet %i\n", vlan_id);
943 bt_vhci_init(bt_new_hci(vlan));
946 static struct bt_device_s *bt_device_add(const char *opt)
948 struct bt_scatternet_s *vlan;
950 char *endp = strstr(opt, ",vlan=");
951 int len = (endp ? endp - opt : strlen(opt)) + 1;
954 pstrcpy(devname, MIN(sizeof(devname), len), opt);
957 vlan_id = strtol(endp + 6, &endp, 0);
959 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
964 vlan = qemu_find_bt_vlan(vlan_id);
967 fprintf(stderr, "qemu: warning: adding a slave device to "
968 "an empty scatternet %i\n", vlan_id);
970 if (!strcmp(devname, "keyboard"))
971 return bt_keyboard_init(vlan);
973 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
977 static int bt_parse(const char *opt)
979 const char *endp, *p;
982 if (strstart(opt, "hci", &endp)) {
983 if (!*endp || *endp == ',') {
985 if (!strstart(endp, ",vlan=", 0))
988 return bt_hci_parse(opt);
990 } else if (strstart(opt, "vhci", &endp)) {
991 if (!*endp || *endp == ',') {
993 if (strstart(endp, ",vlan=", &p)) {
994 vlan = strtol(p, (char **) &endp, 0);
996 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
1000 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
1009 } else if (strstart(opt, "device:", &endp))
1010 return !bt_device_add(endp);
1012 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
1016 static int parse_sandbox(QemuOpts *opts, void *opaque)
1018 /* FIXME: change this to true for 1.3 */
1019 if (qemu_opt_get_bool(opts, "enable", false)) {
1020 #ifdef CONFIG_SECCOMP
1021 if (seccomp_start() < 0) {
1022 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1023 "failed to install seccomp syscall filter in the kernel");
1027 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1028 "sandboxing request but seccomp is not compiled into this build");
1036 static int parse_name(QemuOpts *opts, void *opaque)
1038 const char *proc_name;
1040 if (qemu_opt_get(opts, "debug-threads")) {
1041 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
1043 qemu_name = qemu_opt_get(opts, "guest");
1045 proc_name = qemu_opt_get(opts, "process");
1047 os_set_proc_name(proc_name);
1053 bool usb_enabled(bool default_usb)
1055 return qemu_opt_get_bool(qemu_get_machine_opts(), "usb",
1056 has_defaults && default_usb);
1060 static int parse_add_fd(QemuOpts *opts, void *opaque)
1062 int fd, dupfd, flags;
1064 const char *fd_opaque = NULL;
1066 fd = qemu_opt_get_number(opts, "fd", -1);
1067 fdset_id = qemu_opt_get_number(opts, "set", -1);
1068 fd_opaque = qemu_opt_get(opts, "opaque");
1071 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1072 "fd option is required and must be non-negative");
1076 if (fd <= STDERR_FILENO) {
1077 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1078 "fd cannot be a standard I/O stream");
1083 * All fds inherited across exec() necessarily have FD_CLOEXEC
1084 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1086 flags = fcntl(fd, F_GETFD);
1087 if (flags == -1 || (flags & FD_CLOEXEC)) {
1088 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1089 "fd is not valid or already in use");
1094 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1095 "set option is required and must be non-negative");
1099 #ifdef F_DUPFD_CLOEXEC
1100 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1104 qemu_set_cloexec(dupfd);
1108 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1109 "Error duplicating fd: %s", strerror(errno));
1113 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1114 monitor_fdset_add_fd(dupfd, true, fdset_id, fd_opaque ? true : false,
1120 static int cleanup_add_fd(QemuOpts *opts, void *opaque)
1124 fd = qemu_opt_get_number(opts, "fd", -1);
1131 /***********************************************************/
1132 /* QEMU Block devices */
1134 #define HD_OPTS "media=disk"
1135 #define CDROM_OPTS "media=cdrom"
1137 #define PFLASH_OPTS ""
1141 static int drive_init_func(QemuOpts *opts, void *opaque)
1143 BlockInterfaceType *block_default_type = opaque;
1145 return drive_new(opts, *block_default_type) == NULL;
1148 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
1150 if (qemu_opt_get(opts, "snapshot") == NULL) {
1151 qemu_opt_set(opts, "snapshot", "on");
1156 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1157 int index, const char *optstr)
1162 if (!enable || drive_get_by_index(type, index)) {
1166 opts = drive_add(type, index, NULL, optstr);
1168 drive_enable_snapshot(opts, NULL);
1171 dinfo = drive_new(opts, type);
1175 dinfo->is_default = true;
1179 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1181 boot_set_handler = func;
1182 boot_set_opaque = opaque;
1185 int qemu_boot_set(const char *boot_order)
1187 if (!boot_set_handler) {
1190 return boot_set_handler(boot_set_opaque, boot_order);
1193 static void validate_bootdevices(const char *devices)
1195 /* We just do some generic consistency checks */
1199 for (p = devices; *p != '\0'; p++) {
1200 /* Allowed boot devices are:
1201 * a-b: floppy disk drives
1202 * c-f: IDE disk drives
1203 * g-m: machine implementation dependent drives
1204 * n-p: network devices
1205 * It's up to each machine implementation to check if the given boot
1206 * devices match the actual hardware implementation and firmware
1209 if (*p < 'a' || *p > 'p') {
1210 fprintf(stderr, "Invalid boot device '%c'\n", *p);
1213 if (bitmap & (1 << (*p - 'a'))) {
1214 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
1217 bitmap |= 1 << (*p - 'a');
1221 static void restore_boot_order(void *opaque)
1223 char *normal_boot_order = opaque;
1224 static int first = 1;
1226 /* Restore boot order and remove ourselves after the first boot */
1232 qemu_boot_set(normal_boot_order);
1234 qemu_unregister_reset(restore_boot_order, normal_boot_order);
1235 g_free(normal_boot_order);
1238 static QemuOptsList qemu_smp_opts = {
1240 .implied_opt_name = "cpus",
1241 .merge_lists = true,
1242 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
1246 .type = QEMU_OPT_NUMBER,
1249 .type = QEMU_OPT_NUMBER,
1252 .type = QEMU_OPT_NUMBER,
1255 .type = QEMU_OPT_NUMBER,
1258 .type = QEMU_OPT_NUMBER,
1260 { /*End of list */ }
1264 static void smp_parse(QemuOpts *opts)
1268 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
1269 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
1270 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
1271 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
1273 /* compute missing values, prefer sockets over cores over threads */
1274 if (cpus == 0 || sockets == 0) {
1275 sockets = sockets > 0 ? sockets : 1;
1276 cores = cores > 0 ? cores : 1;
1277 threads = threads > 0 ? threads : 1;
1279 cpus = cores * threads * sockets;
1283 threads = threads > 0 ? threads : 1;
1284 cores = cpus / (sockets * threads);
1286 threads = cpus / (cores * sockets);
1290 max_cpus = qemu_opt_get_number(opts, "maxcpus", 0);
1293 smp_cores = cores > 0 ? cores : 1;
1294 smp_threads = threads > 0 ? threads : 1;
1298 if (max_cpus == 0) {
1299 max_cpus = smp_cpus;
1302 if (max_cpus > MAX_CPUMASK_BITS) {
1303 fprintf(stderr, "Unsupported number of maxcpus\n");
1306 if (max_cpus < smp_cpus) {
1307 fprintf(stderr, "maxcpus must be equal to or greater than smp\n");
1313 static void realtime_init(void)
1316 if (os_mlock() < 0) {
1317 fprintf(stderr, "qemu: locking memory failed\n");
1324 static void configure_msg(QemuOpts *opts)
1326 enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
1329 /***********************************************************/
1332 static int usb_device_add(const char *devname)
1334 USBDevice *dev = NULL;
1335 #ifndef CONFIG_LINUX
1339 if (!usb_enabled(false)) {
1343 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1344 dev = usbdevice_create(devname);
1348 /* the other ones */
1349 #ifndef CONFIG_LINUX
1350 /* only the linux version is qdev-ified, usb-bsd still needs this */
1351 if (strstart(devname, "host:", &p)) {
1352 dev = usb_host_device_open(usb_bus_find(-1), p);
1362 static int usb_device_del(const char *devname)
1367 if (strstart(devname, "host:", &p)) {
1371 if (!usb_enabled(false)) {
1375 p = strchr(devname, '.');
1378 bus_num = strtoul(devname, NULL, 0);
1379 addr = strtoul(p + 1, NULL, 0);
1381 return usb_device_delete_addr(bus_num, addr);
1384 static int usb_parse(const char *cmdline)
1387 r = usb_device_add(cmdline);
1389 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1394 void do_usb_add(Monitor *mon, const QDict *qdict)
1396 const char *devname = qdict_get_str(qdict, "devname");
1397 if (usb_device_add(devname) < 0) {
1398 error_report("could not add USB device '%s'", devname);
1402 void do_usb_del(Monitor *mon, const QDict *qdict)
1404 const char *devname = qdict_get_str(qdict, "devname");
1405 if (usb_device_del(devname) < 0) {
1406 error_report("could not delete USB device '%s'", devname);
1410 /***********************************************************/
1411 /* PCMCIA/Cardbus */
1413 static struct pcmcia_socket_entry_s {
1414 PCMCIASocket *socket;
1415 struct pcmcia_socket_entry_s *next;
1416 } *pcmcia_sockets = 0;
1418 void pcmcia_socket_register(PCMCIASocket *socket)
1420 struct pcmcia_socket_entry_s *entry;
1422 entry = g_malloc(sizeof(struct pcmcia_socket_entry_s));
1423 entry->socket = socket;
1424 entry->next = pcmcia_sockets;
1425 pcmcia_sockets = entry;
1428 void pcmcia_socket_unregister(PCMCIASocket *socket)
1430 struct pcmcia_socket_entry_s *entry, **ptr;
1432 ptr = &pcmcia_sockets;
1433 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1434 if (entry->socket == socket) {
1440 void pcmcia_info(Monitor *mon, const QDict *qdict)
1442 struct pcmcia_socket_entry_s *iter;
1444 if (!pcmcia_sockets)
1445 monitor_printf(mon, "No PCMCIA sockets\n");
1447 for (iter = pcmcia_sockets; iter; iter = iter->next)
1448 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1449 iter->socket->attached ? iter->socket->card_string :
1453 /***********************************************************/
1454 /* machine registration */
1456 MachineState *current_machine;
1458 static void machine_class_init(ObjectClass *oc, void *data)
1460 MachineClass *mc = MACHINE_CLASS(oc);
1461 QEMUMachine *qm = data;
1463 mc->name = qm->name;
1464 mc->alias = qm->alias;
1465 mc->desc = qm->desc;
1466 mc->init = qm->init;
1467 mc->reset = qm->reset;
1468 mc->hot_add_cpu = qm->hot_add_cpu;
1469 mc->kvm_type = qm->kvm_type;
1470 mc->block_default_type = qm->block_default_type;
1471 mc->units_per_default_bus = qm->units_per_default_bus;
1472 mc->max_cpus = qm->max_cpus;
1473 mc->no_serial = qm->no_serial;
1474 mc->no_parallel = qm->no_parallel;
1475 mc->use_virtcon = qm->use_virtcon;
1476 mc->use_sclp = qm->use_sclp;
1477 mc->no_floppy = qm->no_floppy;
1478 mc->no_cdrom = qm->no_cdrom;
1479 mc->no_sdcard = qm->no_sdcard;
1480 mc->is_default = qm->is_default;
1481 mc->default_machine_opts = qm->default_machine_opts;
1482 mc->default_boot_order = qm->default_boot_order;
1483 mc->compat_props = qm->compat_props;
1484 mc->hw_version = qm->hw_version;
1487 int qemu_register_machine(QEMUMachine *m)
1489 char *name = g_strconcat(m->name, TYPE_MACHINE_SUFFIX, NULL);
1492 .parent = TYPE_MACHINE,
1493 .class_init = machine_class_init,
1494 .class_data = (void *)m,
1503 static MachineClass *find_machine(const char *name)
1505 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1506 MachineClass *mc = NULL;
1508 for (el = machines; el; el = el->next) {
1509 MachineClass *temp = el->data;
1511 if (!strcmp(temp->name, name)) {
1516 !strcmp(temp->alias, name)) {
1522 g_slist_free(machines);
1526 MachineClass *find_default_machine(void)
1528 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1529 MachineClass *mc = NULL;
1531 for (el = machines; el; el = el->next) {
1532 MachineClass *temp = el->data;
1534 if (temp->is_default) {
1540 g_slist_free(machines);
1544 MachineInfoList *qmp_query_machines(Error **errp)
1546 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1547 MachineInfoList *mach_list = NULL;
1549 for (el = machines; el; el = el->next) {
1550 MachineClass *mc = el->data;
1551 MachineInfoList *entry;
1554 info = g_malloc0(sizeof(*info));
1555 if (mc->is_default) {
1556 info->has_is_default = true;
1557 info->is_default = true;
1561 info->has_alias = true;
1562 info->alias = g_strdup(mc->alias);
1565 info->name = g_strdup(mc->name);
1566 info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
1568 entry = g_malloc0(sizeof(*entry));
1569 entry->value = info;
1570 entry->next = mach_list;
1574 g_slist_free(machines);
1578 /***********************************************************/
1579 /* main execution loop */
1581 struct vm_change_state_entry {
1582 VMChangeStateHandler *cb;
1584 QLIST_ENTRY (vm_change_state_entry) entries;
1587 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1589 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1592 VMChangeStateEntry *e;
1594 e = g_malloc0(sizeof (*e));
1598 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1602 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1604 QLIST_REMOVE (e, entries);
1608 void vm_state_notify(int running, RunState state)
1610 VMChangeStateEntry *e, *next;
1612 trace_vm_state_notify(running, state);
1614 QLIST_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
1615 e->cb(e->opaque, running, state);
1619 /* reset/shutdown handler */
1621 typedef struct QEMUResetEntry {
1622 QTAILQ_ENTRY(QEMUResetEntry) entry;
1623 QEMUResetHandler *func;
1627 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1628 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1629 static int reset_requested;
1630 static int shutdown_requested, shutdown_signal = -1;
1631 static pid_t shutdown_pid;
1632 static int powerdown_requested;
1633 static int debug_requested;
1634 static int suspend_requested;
1635 static WakeupReason wakeup_reason;
1636 static NotifierList powerdown_notifiers =
1637 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
1638 static NotifierList suspend_notifiers =
1639 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1640 static NotifierList wakeup_notifiers =
1641 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1642 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
1644 int qemu_shutdown_requested_get(void)
1646 return shutdown_requested;
1649 int qemu_reset_requested_get(void)
1651 return reset_requested;
1654 static int qemu_shutdown_requested(void)
1656 int r = shutdown_requested;
1657 shutdown_requested = 0;
1661 static void qemu_kill_report(void)
1663 if (!qtest_driver() && shutdown_signal != -1) {
1664 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1665 if (shutdown_pid == 0) {
1666 /* This happens for eg ^C at the terminal, so it's worth
1667 * avoiding printing an odd message in that case.
1669 fputc('\n', stderr);
1671 fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
1673 shutdown_signal = -1;
1677 static int qemu_reset_requested(void)
1679 int r = reset_requested;
1680 reset_requested = 0;
1684 static int qemu_suspend_requested(void)
1686 int r = suspend_requested;
1687 suspend_requested = 0;
1691 static WakeupReason qemu_wakeup_requested(void)
1693 return wakeup_reason;
1696 static int qemu_powerdown_requested(void)
1698 int r = powerdown_requested;
1699 powerdown_requested = 0;
1703 static int qemu_debug_requested(void)
1705 int r = debug_requested;
1706 debug_requested = 0;
1710 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1712 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
1715 re->opaque = opaque;
1716 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1719 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1723 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1724 if (re->func == func && re->opaque == opaque) {
1725 QTAILQ_REMOVE(&reset_handlers, re, entry);
1732 void qemu_devices_reset(void)
1734 QEMUResetEntry *re, *nre;
1736 /* reset all devices */
1737 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1738 re->func(re->opaque);
1742 void qemu_system_reset(bool report)
1746 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1748 if (mc && mc->reset) {
1751 qemu_devices_reset();
1754 qapi_event_send_reset(&error_abort);
1756 cpu_synchronize_all_post_reset();
1759 void qemu_system_reset_request(void)
1762 shutdown_requested = 1;
1764 reset_requested = 1;
1767 qemu_notify_event();
1770 static void qemu_system_suspend(void)
1773 notifier_list_notify(&suspend_notifiers, NULL);
1774 runstate_set(RUN_STATE_SUSPENDED);
1775 qapi_event_send_suspend(&error_abort);
1778 void qemu_system_suspend_request(void)
1780 if (runstate_check(RUN_STATE_SUSPENDED)) {
1783 suspend_requested = 1;
1785 qemu_notify_event();
1788 void qemu_register_suspend_notifier(Notifier *notifier)
1790 notifier_list_add(&suspend_notifiers, notifier);
1793 void qemu_system_wakeup_request(WakeupReason reason)
1795 trace_system_wakeup_request(reason);
1797 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1800 if (!(wakeup_reason_mask & (1 << reason))) {
1803 runstate_set(RUN_STATE_RUNNING);
1804 wakeup_reason = reason;
1805 qemu_notify_event();
1808 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1811 wakeup_reason_mask |= (1 << reason);
1813 wakeup_reason_mask &= ~(1 << reason);
1817 void qemu_register_wakeup_notifier(Notifier *notifier)
1819 notifier_list_add(&wakeup_notifiers, notifier);
1822 void qemu_system_killed(int signal, pid_t pid)
1824 shutdown_signal = signal;
1827 qemu_system_shutdown_request();
1830 void qemu_system_shutdown_request(void)
1832 trace_qemu_system_shutdown_request();
1833 shutdown_requested = 1;
1834 qemu_notify_event();
1837 static void qemu_system_powerdown(void)
1839 qapi_event_send_powerdown(&error_abort);
1840 notifier_list_notify(&powerdown_notifiers, NULL);
1843 void qemu_system_powerdown_request(void)
1845 trace_qemu_system_powerdown_request();
1846 powerdown_requested = 1;
1847 qemu_notify_event();
1850 void qemu_register_powerdown_notifier(Notifier *notifier)
1852 notifier_list_add(&powerdown_notifiers, notifier);
1855 void qemu_system_debug_request(void)
1857 debug_requested = 1;
1858 qemu_notify_event();
1861 static bool main_loop_should_exit(void)
1864 if (qemu_debug_requested()) {
1865 vm_stop(RUN_STATE_DEBUG);
1867 if (qemu_suspend_requested()) {
1868 qemu_system_suspend();
1870 if (qemu_shutdown_requested()) {
1872 qapi_event_send_shutdown(&error_abort);
1874 vm_stop(RUN_STATE_SHUTDOWN);
1879 if (qemu_reset_requested()) {
1881 cpu_synchronize_all_states();
1882 qemu_system_reset(VMRESET_REPORT);
1884 if (runstate_needs_reset()) {
1885 runstate_set(RUN_STATE_PAUSED);
1888 if (qemu_wakeup_requested()) {
1890 cpu_synchronize_all_states();
1891 qemu_system_reset(VMRESET_SILENT);
1892 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
1893 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
1895 qapi_event_send_wakeup(&error_abort);
1897 if (qemu_powerdown_requested()) {
1898 qemu_system_powerdown();
1900 if (qemu_vmstop_requested(&r)) {
1906 static void main_loop(void)
1910 #ifdef CONFIG_PROFILER
1914 nonblocking = !kvm_enabled() && !xen_enabled() && last_io > 0;
1915 #ifdef CONFIG_PROFILER
1916 ti = profile_getclock();
1918 last_io = main_loop_wait(nonblocking);
1919 #ifdef CONFIG_PROFILER
1920 dev_time += profile_getclock() - ti;
1922 } while (!main_loop_should_exit());
1925 static void version(void)
1927 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1930 static void help(int exitcode)
1933 printf("usage: %s [options] [disk_image]\n\n"
1934 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1935 error_get_progname());
1937 #define QEMU_OPTIONS_GENERATE_HELP
1938 #include "qemu-options-wrapper.h"
1940 printf("\nDuring emulation, the following keys are useful:\n"
1941 "ctrl-alt-f toggle full screen\n"
1942 "ctrl-alt-n switch to virtual console 'n'\n"
1943 "ctrl-alt toggle mouse and keyboard grab\n"
1945 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1950 #define HAS_ARG 0x0001
1952 typedef struct QEMUOption {
1959 static const QEMUOption qemu_options[] = {
1960 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1961 #define QEMU_OPTIONS_GENERATE_OPTIONS
1962 #include "qemu-options-wrapper.h"
1966 static bool vga_available(void)
1968 return object_class_by_name("VGA") || object_class_by_name("isa-vga");
1971 static bool cirrus_vga_available(void)
1973 return object_class_by_name("cirrus-vga")
1974 || object_class_by_name("isa-cirrus-vga");
1977 static bool vmware_vga_available(void)
1979 return object_class_by_name("vmware-svga");
1982 static bool qxl_vga_available(void)
1984 return object_class_by_name("qxl-vga");
1987 static bool tcx_vga_available(void)
1989 return object_class_by_name("SUNW,tcx");
1992 static bool cg3_vga_available(void)
1994 return object_class_by_name("cgthree");
1997 static void select_vgahw (const char *p)
2001 assert(vga_interface_type == VGA_NONE);
2002 if (strstart(p, "std", &opts)) {
2003 if (vga_available()) {
2004 vga_interface_type = VGA_STD;
2006 fprintf(stderr, "Error: standard VGA not available\n");
2009 } else if (strstart(p, "cirrus", &opts)) {
2010 if (cirrus_vga_available()) {
2011 vga_interface_type = VGA_CIRRUS;
2013 fprintf(stderr, "Error: Cirrus VGA not available\n");
2016 } else if (strstart(p, "vmware", &opts)) {
2017 if (vmware_vga_available()) {
2018 vga_interface_type = VGA_VMWARE;
2020 fprintf(stderr, "Error: VMWare SVGA not available\n");
2023 } else if (strstart(p, "xenfb", &opts)) {
2024 vga_interface_type = VGA_XENFB;
2025 } else if (strstart(p, "qxl", &opts)) {
2026 if (qxl_vga_available()) {
2027 vga_interface_type = VGA_QXL;
2029 fprintf(stderr, "Error: QXL VGA not available\n");
2032 } else if (strstart(p, "tcx", &opts)) {
2033 if (tcx_vga_available()) {
2034 vga_interface_type = VGA_TCX;
2036 fprintf(stderr, "Error: TCX framebuffer not available\n");
2039 } else if (strstart(p, "cg3", &opts)) {
2040 if (cg3_vga_available()) {
2041 vga_interface_type = VGA_CG3;
2043 fprintf(stderr, "Error: CG3 framebuffer not available\n");
2046 } else if (!strstart(p, "none", &opts)) {
2048 fprintf(stderr, "Unknown vga type: %s\n", p);
2052 const char *nextopt;
2054 if (strstart(opts, ",retrace=", &nextopt)) {
2056 if (strstart(opts, "dumb", &nextopt))
2057 vga_retrace_method = VGA_RETRACE_DUMB;
2058 else if (strstart(opts, "precise", &nextopt))
2059 vga_retrace_method = VGA_RETRACE_PRECISE;
2060 else goto invalid_vga;
2061 } else goto invalid_vga;
2066 static DisplayType select_display(const char *p)
2069 DisplayType display = DT_DEFAULT;
2071 if (strstart(p, "sdl", &opts)) {
2075 const char *nextopt;
2077 if (strstart(opts, ",frame=", &nextopt)) {
2079 if (strstart(opts, "on", &nextopt)) {
2081 } else if (strstart(opts, "off", &nextopt)) {
2084 goto invalid_sdl_args;
2086 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
2088 if (strstart(opts, "on", &nextopt)) {
2090 } else if (strstart(opts, "off", &nextopt)) {
2093 goto invalid_sdl_args;
2095 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2097 if (strstart(opts, "on", &nextopt)) {
2099 } else if (strstart(opts, "off", &nextopt)) {
2102 goto invalid_sdl_args;
2104 } else if (strstart(opts, ",window_close=", &nextopt)) {
2106 if (strstart(opts, "on", &nextopt)) {
2108 } else if (strstart(opts, "off", &nextopt)) {
2111 goto invalid_sdl_args;
2115 fprintf(stderr, "Invalid SDL option string: %s\n", p);
2121 fprintf(stderr, "SDL support is disabled\n");
2124 } else if (strstart(p, "vnc", &opts)) {
2129 const char *nextopt;
2131 if (strstart(opts, "=", &nextopt)) {
2132 vnc_display = nextopt;
2136 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
2140 fprintf(stderr, "VNC support is disabled\n");
2143 } else if (strstart(p, "curses", &opts)) {
2144 #ifdef CONFIG_CURSES
2145 display = DT_CURSES;
2147 fprintf(stderr, "Curses support is disabled\n");
2150 } else if (strstart(p, "gtk", &opts)) {
2154 const char *nextopt;
2156 if (strstart(opts, ",grab_on_hover=", &nextopt)) {
2158 if (strstart(opts, "on", &nextopt)) {
2159 grab_on_hover = true;
2160 } else if (strstart(opts, "off", &nextopt)) {
2161 grab_on_hover = false;
2163 goto invalid_gtk_args;
2167 fprintf(stderr, "Invalid GTK option string: %s\n", p);
2173 fprintf(stderr, "GTK support is disabled\n");
2176 } else if (strstart(p, "none", &opts)) {
2179 fprintf(stderr, "Unknown display type: %s\n", p);
2186 static int balloon_parse(const char *arg)
2190 if (strcmp(arg, "none") == 0) {
2194 if (!strncmp(arg, "virtio", 6)) {
2195 if (arg[6] == ',') {
2196 /* have params -> parse them */
2197 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
2201 /* create empty opts */
2202 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2205 qemu_opt_set(opts, "driver", "virtio-balloon");
2212 char *qemu_find_file(int type, const char *name)
2218 /* Try the name as a straight path first */
2219 if (access(name, R_OK) == 0) {
2220 trace_load_file(name, name);
2221 return g_strdup(name);
2225 case QEMU_FILE_TYPE_BIOS:
2228 case QEMU_FILE_TYPE_KEYMAP:
2229 subdir = "keymaps/";
2235 for (i = 0; i < data_dir_idx; i++) {
2236 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2237 if (access(buf, R_OK) == 0) {
2238 trace_load_file(name, buf);
2246 static int device_help_func(QemuOpts *opts, void *opaque)
2248 return qdev_device_help(opts);
2251 static int device_init_func(QemuOpts *opts, void *opaque)
2255 dev = qdev_device_add(opts);
2258 object_unref(OBJECT(dev));
2262 static int chardev_init_func(QemuOpts *opts, void *opaque)
2264 Error *local_err = NULL;
2266 qemu_chr_new_from_opts(opts, NULL, &local_err);
2268 error_report("%s", error_get_pretty(local_err));
2269 error_free(local_err);
2275 #ifdef CONFIG_VIRTFS
2276 static int fsdev_init_func(QemuOpts *opts, void *opaque)
2279 ret = qemu_fsdev_add(opts);
2285 static int mon_init_func(QemuOpts *opts, void *opaque)
2287 CharDriverState *chr;
2288 const char *chardev;
2292 mode = qemu_opt_get(opts, "mode");
2296 if (strcmp(mode, "readline") == 0) {
2297 flags = MONITOR_USE_READLINE;
2298 } else if (strcmp(mode, "control") == 0) {
2299 flags = MONITOR_USE_CONTROL;
2301 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
2305 if (qemu_opt_get_bool(opts, "pretty", 0))
2306 flags |= MONITOR_USE_PRETTY;
2308 if (qemu_opt_get_bool(opts, "default", 0))
2309 flags |= MONITOR_IS_DEFAULT;
2311 chardev = qemu_opt_get(opts, "chardev");
2312 chr = qemu_chr_find(chardev);
2314 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
2318 qemu_chr_fe_claim_no_fail(chr);
2319 monitor_init(chr, flags);
2323 static void monitor_parse(const char *optarg, const char *mode)
2325 static int monitor_device_index = 0;
2331 if (strstart(optarg, "chardev:", &p)) {
2332 snprintf(label, sizeof(label), "%s", p);
2334 snprintf(label, sizeof(label), "compat_monitor%d",
2335 monitor_device_index);
2336 if (monitor_device_index == 0) {
2339 opts = qemu_chr_parse_compat(label, optarg);
2341 fprintf(stderr, "parse error: %s\n", optarg);
2346 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, NULL);
2348 fprintf(stderr, "duplicate chardev: %s\n", label);
2351 qemu_opt_set(opts, "mode", mode);
2352 qemu_opt_set(opts, "chardev", label);
2354 qemu_opt_set(opts, "default", "on");
2355 monitor_device_index++;
2358 struct device_config {
2360 DEV_USB, /* -usbdevice */
2362 DEV_SERIAL, /* -serial */
2363 DEV_PARALLEL, /* -parallel */
2364 DEV_VIRTCON, /* -virtioconsole */
2365 DEV_DEBUGCON, /* -debugcon */
2366 DEV_GDB, /* -gdb, -s */
2367 DEV_SCLP, /* s390 sclp */
2369 const char *cmdline;
2371 QTAILQ_ENTRY(device_config) next;
2374 static QTAILQ_HEAD(, device_config) device_configs =
2375 QTAILQ_HEAD_INITIALIZER(device_configs);
2377 static void add_device_config(int type, const char *cmdline)
2379 struct device_config *conf;
2381 conf = g_malloc0(sizeof(*conf));
2383 conf->cmdline = cmdline;
2384 loc_save(&conf->loc);
2385 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2388 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2390 struct device_config *conf;
2393 QTAILQ_FOREACH(conf, &device_configs, next) {
2394 if (conf->type != type)
2396 loc_push_restore(&conf->loc);
2397 rc = func(conf->cmdline);
2398 loc_pop(&conf->loc);
2406 static int serial_parse(const char *devname)
2408 static int index = 0;
2411 if (strcmp(devname, "none") == 0)
2413 if (index == MAX_SERIAL_PORTS) {
2414 fprintf(stderr, "qemu: too many serial ports\n");
2417 snprintf(label, sizeof(label), "serial%d", index);
2418 serial_hds[index] = qemu_chr_new(label, devname, NULL);
2419 if (!serial_hds[index]) {
2420 fprintf(stderr, "qemu: could not connect serial device"
2421 " to character backend '%s'\n", devname);
2428 static int parallel_parse(const char *devname)
2430 static int index = 0;
2433 if (strcmp(devname, "none") == 0)
2435 if (index == MAX_PARALLEL_PORTS) {
2436 fprintf(stderr, "qemu: too many parallel ports\n");
2439 snprintf(label, sizeof(label), "parallel%d", index);
2440 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
2441 if (!parallel_hds[index]) {
2442 fprintf(stderr, "qemu: could not connect parallel device"
2443 " to character backend '%s'\n", devname);
2450 static int virtcon_parse(const char *devname)
2452 QemuOptsList *device = qemu_find_opts("device");
2453 static int index = 0;
2455 QemuOpts *bus_opts, *dev_opts;
2457 if (strcmp(devname, "none") == 0)
2459 if (index == MAX_VIRTIO_CONSOLES) {
2460 fprintf(stderr, "qemu: too many virtio consoles\n");
2464 bus_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2465 if (arch_type == QEMU_ARCH_S390X) {
2466 qemu_opt_set(bus_opts, "driver", "virtio-serial-s390");
2468 qemu_opt_set(bus_opts, "driver", "virtio-serial-pci");
2471 dev_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2472 qemu_opt_set(dev_opts, "driver", "virtconsole");
2474 snprintf(label, sizeof(label), "virtcon%d", index);
2475 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
2476 if (!virtcon_hds[index]) {
2477 fprintf(stderr, "qemu: could not connect virtio console"
2478 " to character backend '%s'\n", devname);
2481 qemu_opt_set(dev_opts, "chardev", label);
2487 static int sclp_parse(const char *devname)
2489 QemuOptsList *device = qemu_find_opts("device");
2490 static int index = 0;
2494 if (strcmp(devname, "none") == 0) {
2497 if (index == MAX_SCLP_CONSOLES) {
2498 fprintf(stderr, "qemu: too many sclp consoles\n");
2502 assert(arch_type == QEMU_ARCH_S390X);
2504 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2505 qemu_opt_set(dev_opts, "driver", "sclpconsole");
2507 snprintf(label, sizeof(label), "sclpcon%d", index);
2508 sclp_hds[index] = qemu_chr_new(label, devname, NULL);
2509 if (!sclp_hds[index]) {
2510 fprintf(stderr, "qemu: could not connect sclp console"
2511 " to character backend '%s'\n", devname);
2514 qemu_opt_set(dev_opts, "chardev", label);
2520 static int debugcon_parse(const char *devname)
2524 if (!qemu_chr_new("debugcon", devname, NULL)) {
2527 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2529 fprintf(stderr, "qemu: already have a debugcon device\n");
2532 qemu_opt_set(opts, "driver", "isa-debugcon");
2533 qemu_opt_set(opts, "chardev", "debugcon");
2537 static MachineClass *machine_parse(const char *name)
2539 MachineClass *mc = NULL;
2540 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
2543 mc = find_machine(name);
2548 if (name && !is_help_option(name)) {
2549 error_report("Unsupported machine type");
2550 error_printf("Use -machine help to list supported machines!\n");
2552 printf("Supported machines are:\n");
2553 for (el = machines; el; el = el->next) {
2554 MachineClass *mc = el->data;
2556 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
2558 printf("%-20s %s%s\n", mc->name, mc->desc,
2559 mc->is_default ? " (default)" : "");
2563 g_slist_free(machines);
2564 exit(!name || !is_help_option(name));
2567 void qemu_add_exit_notifier(Notifier *notify)
2569 notifier_list_add(&exit_notifiers, notify);
2572 void qemu_remove_exit_notifier(Notifier *notify)
2574 notifier_remove(notify);
2577 static void qemu_run_exit_notifiers(void)
2579 notifier_list_notify(&exit_notifiers, NULL);
2582 void qemu_add_machine_init_done_notifier(Notifier *notify)
2584 notifier_list_add(&machine_init_done_notifiers, notify);
2587 static void qemu_run_machine_init_done_notifiers(void)
2589 notifier_list_notify(&machine_init_done_notifiers, NULL);
2592 static const QEMUOption *lookup_opt(int argc, char **argv,
2593 const char **poptarg, int *poptind)
2595 const QEMUOption *popt;
2596 int optind = *poptind;
2597 char *r = argv[optind];
2600 loc_set_cmdline(argv, optind, 1);
2602 /* Treat --foo the same as -foo. */
2605 popt = qemu_options;
2608 error_report("invalid option");
2611 if (!strcmp(popt->name, r + 1))
2615 if (popt->flags & HAS_ARG) {
2616 if (optind >= argc) {
2617 error_report("requires an argument");
2620 optarg = argv[optind++];
2621 loc_set_cmdline(argv, optind - 2, 2);
2632 static gpointer malloc_and_trace(gsize n_bytes)
2634 void *ptr = malloc(n_bytes);
2635 trace_g_malloc(n_bytes, ptr);
2639 static gpointer realloc_and_trace(gpointer mem, gsize n_bytes)
2641 void *ptr = realloc(mem, n_bytes);
2642 trace_g_realloc(mem, n_bytes, ptr);
2646 static void free_and_trace(gpointer mem)
2652 static int machine_set_property(const char *name, const char *value,
2655 Object *obj = OBJECT(opaque);
2656 StringInputVisitor *siv;
2657 Error *local_err = NULL;
2660 if (strcmp(name, "type") == 0) {
2664 qom_name = g_strdup(name);
2672 siv = string_input_visitor_new(value);
2673 object_property_set(obj, string_input_get_visitor(siv), qom_name, &local_err);
2674 string_input_visitor_cleanup(siv);
2678 qerror_report_err(local_err);
2679 error_free(local_err);
2686 static int object_create(QemuOpts *opts, void *opaque)
2695 ov = opts_visitor_new(opts);
2696 pdict = qemu_opts_to_qdict(opts, NULL);
2698 visit_start_struct(opts_get_visitor(ov), &dummy, NULL, NULL, 0, &err);
2703 qdict_del(pdict, "qom-type");
2704 visit_type_str(opts_get_visitor(ov), &type, "qom-type", &err);
2709 qdict_del(pdict, "id");
2710 visit_type_str(opts_get_visitor(ov), &id, "id", &err);
2715 object_add(type, id, pdict, opts_get_visitor(ov), &err);
2719 visit_end_struct(opts_get_visitor(ov), &err);
2721 qmp_object_del(id, NULL);
2725 opts_visitor_cleanup(ov);
2732 qerror_report_err(err);
2739 int main(int argc, char **argv, char **envp)
2742 int snapshot, linux_boot;
2743 const char *initrd_filename;
2744 const char *kernel_filename, *kernel_cmdline;
2745 const char *boot_order;
2747 int cyls, heads, secs, translation;
2748 QemuOpts *hda_opts = NULL, *opts, *machine_opts, *icount_opts = NULL;
2749 QemuOptsList *olist;
2752 const char *loadvm = NULL;
2753 MachineClass *machine_class;
2754 const char *cpu_model;
2755 const char *vga_model = NULL;
2756 const char *qtest_chrdev = NULL;
2757 const char *qtest_log = NULL;
2758 const char *pid_file = NULL;
2759 const char *incoming = NULL;
2761 int show_vnc_port = 0;
2763 bool defconfig = true;
2764 bool userconfig = true;
2765 const char *log_mask = NULL;
2766 const char *log_file = NULL;
2767 GMemVTable mem_trace = {
2768 .malloc = malloc_and_trace,
2769 .realloc = realloc_and_trace,
2770 .free = free_and_trace,
2772 const char *trace_events = NULL;
2773 const char *trace_file = NULL;
2774 const ram_addr_t default_ram_size = (ram_addr_t)DEFAULT_RAM_SIZE *
2776 ram_addr_t maxram_size = default_ram_size;
2777 uint64_t ram_slots = 0;
2778 FILE *vmstate_dump_file = NULL;
2779 Error *main_loop_err = NULL;
2781 atexit(qemu_run_exit_notifiers);
2782 error_set_progname(argv[0]);
2783 qemu_init_exec_dir(argv[0]);
2785 g_mem_set_vtable(&mem_trace);
2787 module_call_init(MODULE_INIT_QOM);
2789 qemu_add_opts(&qemu_drive_opts);
2790 qemu_add_drive_opts(&qemu_legacy_drive_opts);
2791 qemu_add_drive_opts(&qemu_common_drive_opts);
2792 qemu_add_drive_opts(&qemu_drive_opts);
2793 qemu_add_opts(&qemu_chardev_opts);
2794 qemu_add_opts(&qemu_device_opts);
2795 qemu_add_opts(&qemu_netdev_opts);
2796 qemu_add_opts(&qemu_net_opts);
2797 qemu_add_opts(&qemu_rtc_opts);
2798 qemu_add_opts(&qemu_global_opts);
2799 qemu_add_opts(&qemu_mon_opts);
2800 qemu_add_opts(&qemu_trace_opts);
2801 qemu_add_opts(&qemu_option_rom_opts);
2802 qemu_add_opts(&qemu_machine_opts);
2803 qemu_add_opts(&qemu_mem_opts);
2804 qemu_add_opts(&qemu_smp_opts);
2805 qemu_add_opts(&qemu_boot_opts);
2806 qemu_add_opts(&qemu_sandbox_opts);
2807 qemu_add_opts(&qemu_add_fd_opts);
2808 qemu_add_opts(&qemu_object_opts);
2809 qemu_add_opts(&qemu_tpmdev_opts);
2810 qemu_add_opts(&qemu_realtime_opts);
2811 qemu_add_opts(&qemu_msg_opts);
2812 qemu_add_opts(&qemu_name_opts);
2813 qemu_add_opts(&qemu_numa_opts);
2814 qemu_add_opts(&qemu_icount_opts);
2818 rtc_clock = QEMU_CLOCK_HOST;
2820 QLIST_INIT (&vm_change_state_head);
2821 os_setup_early_signal_handling();
2823 module_call_init(MODULE_INIT_MACHINE);
2824 machine_class = find_default_machine();
2826 ram_size = default_ram_size;
2828 cyls = heads = secs = 0;
2829 translation = BIOS_ATA_TRANSLATION_AUTO;
2831 for (i = 0; i < MAX_NODES; i++) {
2832 numa_info[i].node_mem = 0;
2833 numa_info[i].present = false;
2834 bitmap_zero(numa_info[i].node_cpu, MAX_CPUMASK_BITS);
2838 max_numa_nodeid = 0;
2841 bdrv_init_with_whitelist();
2845 /* first pass of option parsing */
2847 while (optind < argc) {
2848 if (argv[optind][0] != '-') {
2852 const QEMUOption *popt;
2854 popt = lookup_opt(argc, argv, &optarg, &optind);
2855 switch (popt->index) {
2856 case QEMU_OPTION_nodefconfig:
2859 case QEMU_OPTION_nouserconfig:
2868 ret = qemu_read_default_config_files(userconfig);
2874 /* second pass of option parsing */
2879 if (argv[optind][0] != '-') {
2880 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2882 const QEMUOption *popt;
2884 popt = lookup_opt(argc, argv, &optarg, &optind);
2885 if (!(popt->arch_mask & arch_type)) {
2886 printf("Option %s not supported for this target\n", popt->name);
2889 switch(popt->index) {
2891 machine_class = machine_parse(optarg);
2893 case QEMU_OPTION_no_kvm_irqchip: {
2894 olist = qemu_find_opts("machine");
2895 qemu_opts_parse(olist, "kernel_irqchip=off", 0);
2898 case QEMU_OPTION_cpu:
2899 /* hw initialization will check this */
2902 case QEMU_OPTION_hda:
2906 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2908 snprintf(buf, sizeof(buf),
2909 "%s,cyls=%d,heads=%d,secs=%d%s",
2910 HD_OPTS , cyls, heads, secs,
2911 translation == BIOS_ATA_TRANSLATION_LBA ?
2913 translation == BIOS_ATA_TRANSLATION_NONE ?
2914 ",trans=none" : "");
2915 drive_add(IF_DEFAULT, 0, optarg, buf);
2918 case QEMU_OPTION_hdb:
2919 case QEMU_OPTION_hdc:
2920 case QEMU_OPTION_hdd:
2921 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2924 case QEMU_OPTION_drive:
2925 if (drive_def(optarg) == NULL) {
2929 case QEMU_OPTION_set:
2930 if (qemu_set_option(optarg) != 0)
2933 case QEMU_OPTION_global:
2934 if (qemu_global_option(optarg) != 0)
2937 case QEMU_OPTION_mtdblock:
2938 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2940 case QEMU_OPTION_sd:
2941 drive_add(IF_SD, -1, optarg, SD_OPTS);
2943 case QEMU_OPTION_pflash:
2944 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2946 case QEMU_OPTION_snapshot:
2949 case QEMU_OPTION_hdachs:
2953 cyls = strtol(p, (char **)&p, 0);
2954 if (cyls < 1 || cyls > 16383)
2959 heads = strtol(p, (char **)&p, 0);
2960 if (heads < 1 || heads > 16)
2965 secs = strtol(p, (char **)&p, 0);
2966 if (secs < 1 || secs > 63)
2970 if (!strcmp(p, "large")) {
2971 translation = BIOS_ATA_TRANSLATION_LARGE;
2972 } else if (!strcmp(p, "rechs")) {
2973 translation = BIOS_ATA_TRANSLATION_RECHS;
2974 } else if (!strcmp(p, "none")) {
2975 translation = BIOS_ATA_TRANSLATION_NONE;
2976 } else if (!strcmp(p, "lba")) {
2977 translation = BIOS_ATA_TRANSLATION_LBA;
2978 } else if (!strcmp(p, "auto")) {
2979 translation = BIOS_ATA_TRANSLATION_AUTO;
2983 } else if (*p != '\0') {
2985 fprintf(stderr, "qemu: invalid physical CHS format\n");
2988 if (hda_opts != NULL) {
2990 snprintf(num, sizeof(num), "%d", cyls);
2991 qemu_opt_set(hda_opts, "cyls", num);
2992 snprintf(num, sizeof(num), "%d", heads);
2993 qemu_opt_set(hda_opts, "heads", num);
2994 snprintf(num, sizeof(num), "%d", secs);
2995 qemu_opt_set(hda_opts, "secs", num);
2996 if (translation == BIOS_ATA_TRANSLATION_LARGE) {
2997 qemu_opt_set(hda_opts, "trans", "large");
2998 } else if (translation == BIOS_ATA_TRANSLATION_RECHS) {
2999 qemu_opt_set(hda_opts, "trans", "rechs");
3000 } else if (translation == BIOS_ATA_TRANSLATION_LBA) {
3001 qemu_opt_set(hda_opts, "trans", "lba");
3002 } else if (translation == BIOS_ATA_TRANSLATION_NONE) {
3003 qemu_opt_set(hda_opts, "trans", "none");
3008 case QEMU_OPTION_numa:
3009 opts = qemu_opts_parse(qemu_find_opts("numa"), optarg, 1);
3014 case QEMU_OPTION_display:
3015 display_type = select_display(optarg);
3017 case QEMU_OPTION_nographic:
3018 display_type = DT_NOGRAPHIC;
3020 case QEMU_OPTION_curses:
3021 #ifdef CONFIG_CURSES
3022 display_type = DT_CURSES;
3024 fprintf(stderr, "Curses support is disabled\n");
3028 case QEMU_OPTION_portrait:
3029 graphic_rotate = 90;
3031 case QEMU_OPTION_rotate:
3032 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3033 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3034 graphic_rotate != 180 && graphic_rotate != 270) {
3036 "qemu: only 90, 180, 270 deg rotation is available\n");
3040 case QEMU_OPTION_kernel:
3041 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg);
3043 case QEMU_OPTION_initrd:
3044 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg);
3046 case QEMU_OPTION_append:
3047 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg);
3049 case QEMU_OPTION_dtb:
3050 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg);
3052 case QEMU_OPTION_cdrom:
3053 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3055 case QEMU_OPTION_boot:
3056 opts = qemu_opts_parse(qemu_find_opts("boot-opts"), optarg, 1);
3061 case QEMU_OPTION_fda:
3062 case QEMU_OPTION_fdb:
3063 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3066 case QEMU_OPTION_no_fd_bootchk:
3069 case QEMU_OPTION_netdev:
3070 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3074 case QEMU_OPTION_net:
3075 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3079 #ifdef CONFIG_LIBISCSI
3080 case QEMU_OPTION_iscsi:
3081 opts = qemu_opts_parse(qemu_find_opts("iscsi"), optarg, 0);
3088 case QEMU_OPTION_tftp:
3089 legacy_tftp_prefix = optarg;
3091 case QEMU_OPTION_bootp:
3092 legacy_bootp_filename = optarg;
3094 case QEMU_OPTION_redir:
3095 if (net_slirp_redir(optarg) < 0)
3099 case QEMU_OPTION_bt:
3100 add_device_config(DEV_BT, optarg);
3102 case QEMU_OPTION_audio_help:
3106 case QEMU_OPTION_soundhw:
3107 select_soundhw (optarg);
3112 case QEMU_OPTION_version:
3116 case QEMU_OPTION_m: {
3118 const char *mem_str;
3119 const char *maxmem_str, *slots_str;
3121 opts = qemu_opts_parse(qemu_find_opts("memory"),
3127 mem_str = qemu_opt_get(opts, "size");
3129 error_report("invalid -m option, missing 'size' option");
3133 error_report("missing 'size' option value");
3137 sz = qemu_opt_get_size(opts, "size", ram_size);
3139 /* Fix up legacy suffix-less format */
3140 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
3141 uint64_t overflow_check = sz;
3144 if ((sz >> 20) != overflow_check) {
3145 error_report("too large 'size' option value");
3150 /* backward compatibility behaviour for case "-m 0" */
3152 sz = default_ram_size;
3155 sz = QEMU_ALIGN_UP(sz, 8192);
3157 if (ram_size != sz) {
3158 error_report("ram size too large");
3161 maxram_size = ram_size;
3163 maxmem_str = qemu_opt_get(opts, "maxmem");
3164 slots_str = qemu_opt_get(opts, "slots");
3165 if (maxmem_str && slots_str) {
3168 sz = qemu_opt_get_size(opts, "maxmem", 0);
3169 if (sz < ram_size) {
3170 error_report("invalid -m option value: maxmem "
3171 "(0x%" PRIx64 ") <= initial memory (0x"
3172 RAM_ADDR_FMT ")", sz, ram_size);
3176 slots = qemu_opt_get_number(opts, "slots", 0);
3177 if ((sz > ram_size) && !slots) {
3178 error_report("invalid -m option value: maxmem "
3179 "(0x%" PRIx64 ") more than initial memory (0x"
3180 RAM_ADDR_FMT ") but no hotplug slots where "
3181 "specified", sz, ram_size);
3185 if ((sz <= ram_size) && slots) {
3186 error_report("invalid -m option value: %"
3187 PRIu64 " hotplug slots where specified but "
3188 "maxmem (0x%" PRIx64 ") <= initial memory (0x"
3189 RAM_ADDR_FMT ")", slots, sz, ram_size);
3194 } else if ((!maxmem_str && slots_str) ||
3195 (maxmem_str && !slots_str)) {
3196 error_report("invalid -m option value: missing "
3197 "'%s' option", slots_str ? "maxmem" : "slots");
3203 case QEMU_OPTION_tpmdev:
3204 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3209 case QEMU_OPTION_mempath:
3212 case QEMU_OPTION_mem_prealloc:
3222 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3224 case QEMU_OPTION_gdb:
3225 add_device_config(DEV_GDB, optarg);
3228 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3229 data_dir[data_dir_idx++] = optarg;
3232 case QEMU_OPTION_bios:
3233 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg);
3235 case QEMU_OPTION_singlestep:
3242 keyboard_layout = optarg;
3244 case QEMU_OPTION_localtime:
3247 case QEMU_OPTION_vga:
3256 w = strtol(p, (char **)&p, 10);
3259 fprintf(stderr, "qemu: invalid resolution or depth\n");
3265 h = strtol(p, (char **)&p, 10);
3270 depth = strtol(p, (char **)&p, 10);
3271 if (depth != 8 && depth != 15 && depth != 16 &&
3272 depth != 24 && depth != 32)
3274 } else if (*p == '\0') {
3275 depth = graphic_depth;
3282 graphic_depth = depth;
3285 case QEMU_OPTION_echr:
3288 term_escape_char = strtol(optarg, &r, 0);
3290 printf("Bad argument to echr\n");
3293 case QEMU_OPTION_monitor:
3294 default_monitor = 0;
3295 if (strncmp(optarg, "none", 4)) {
3296 monitor_parse(optarg, "readline");
3299 case QEMU_OPTION_qmp:
3300 monitor_parse(optarg, "control");
3301 default_monitor = 0;
3303 case QEMU_OPTION_mon:
3304 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
3308 default_monitor = 0;
3310 case QEMU_OPTION_chardev:
3311 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
3316 case QEMU_OPTION_fsdev:
3317 olist = qemu_find_opts("fsdev");
3319 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
3322 opts = qemu_opts_parse(olist, optarg, 1);
3327 case QEMU_OPTION_virtfs: {
3330 const char *writeout, *sock_fd, *socket;
3332 olist = qemu_find_opts("virtfs");
3334 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
3337 opts = qemu_opts_parse(olist, optarg, 1);
3342 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3343 qemu_opt_get(opts, "mount_tag") == NULL) {
3344 fprintf(stderr, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
3347 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3348 qemu_opt_get(opts, "mount_tag"),
3351 fprintf(stderr, "duplicate fsdev id: %s\n",
3352 qemu_opt_get(opts, "mount_tag"));
3356 writeout = qemu_opt_get(opts, "writeout");
3358 #ifdef CONFIG_SYNC_FILE_RANGE
3359 qemu_opt_set(fsdev, "writeout", writeout);
3361 fprintf(stderr, "writeout=immediate not supported on "
3366 qemu_opt_set(fsdev, "fsdriver", qemu_opt_get(opts, "fsdriver"));
3367 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
3368 qemu_opt_set(fsdev, "security_model",
3369 qemu_opt_get(opts, "security_model"));
3370 socket = qemu_opt_get(opts, "socket");
3372 qemu_opt_set(fsdev, "socket", socket);
3374 sock_fd = qemu_opt_get(opts, "sock_fd");
3376 qemu_opt_set(fsdev, "sock_fd", sock_fd);
3379 qemu_opt_set_bool(fsdev, "readonly",
3380 qemu_opt_get_bool(opts, "readonly", 0));
3381 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3383 qemu_opt_set(device, "driver", "virtio-9p-pci");
3384 qemu_opt_set(device, "fsdev",
3385 qemu_opt_get(opts, "mount_tag"));
3386 qemu_opt_set(device, "mount_tag",
3387 qemu_opt_get(opts, "mount_tag"));
3390 case QEMU_OPTION_virtfs_synth: {
3394 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3397 fprintf(stderr, "duplicate option: %s\n", "virtfs_synth");
3400 qemu_opt_set(fsdev, "fsdriver", "synth");
3402 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3404 qemu_opt_set(device, "driver", "virtio-9p-pci");
3405 qemu_opt_set(device, "fsdev", "v_synth");
3406 qemu_opt_set(device, "mount_tag", "v_synth");
3409 case QEMU_OPTION_serial:
3410 add_device_config(DEV_SERIAL, optarg);
3412 if (strncmp(optarg, "mon:", 4) == 0) {
3413 default_monitor = 0;
3416 case QEMU_OPTION_watchdog:
3419 "qemu: only one watchdog option may be given\n");
3424 case QEMU_OPTION_watchdog_action:
3425 if (select_watchdog_action(optarg) == -1) {
3426 fprintf(stderr, "Unknown -watchdog-action parameter\n");
3430 case QEMU_OPTION_virtiocon:
3431 add_device_config(DEV_VIRTCON, optarg);
3432 default_virtcon = 0;
3433 if (strncmp(optarg, "mon:", 4) == 0) {
3434 default_monitor = 0;
3437 case QEMU_OPTION_parallel:
3438 add_device_config(DEV_PARALLEL, optarg);
3439 default_parallel = 0;
3440 if (strncmp(optarg, "mon:", 4) == 0) {
3441 default_monitor = 0;
3444 case QEMU_OPTION_debugcon:
3445 add_device_config(DEV_DEBUGCON, optarg);
3447 case QEMU_OPTION_loadvm:
3450 case QEMU_OPTION_full_screen:
3453 case QEMU_OPTION_no_frame:
3456 case QEMU_OPTION_alt_grab:
3459 case QEMU_OPTION_ctrl_grab:
3462 case QEMU_OPTION_no_quit:
3465 case QEMU_OPTION_sdl:
3467 display_type = DT_SDL;
3470 fprintf(stderr, "SDL support is disabled\n");
3473 case QEMU_OPTION_pidfile:
3476 case QEMU_OPTION_win2k_hack:
3477 win2k_install_hack = 1;
3479 case QEMU_OPTION_rtc_td_hack: {
3480 static GlobalProperty slew_lost_ticks[] = {
3482 .driver = "mc146818rtc",
3483 .property = "lost_tick_policy",
3486 { /* end of list */ }
3489 qdev_prop_register_global_list(slew_lost_ticks);
3492 case QEMU_OPTION_acpitable:
3493 opts = qemu_opts_parse(qemu_find_opts("acpi"), optarg, 1);
3497 do_acpitable_option(opts);
3499 case QEMU_OPTION_smbios:
3500 opts = qemu_opts_parse(qemu_find_opts("smbios"), optarg, 0);
3504 do_smbios_option(opts);
3506 case QEMU_OPTION_enable_kvm:
3507 olist = qemu_find_opts("machine");
3508 qemu_opts_parse(olist, "accel=kvm", 0);
3510 case QEMU_OPTION_machine:
3511 olist = qemu_find_opts("machine");
3512 opts = qemu_opts_parse(olist, optarg, 1);
3516 optarg = qemu_opt_get(opts, "type");
3518 machine_class = machine_parse(optarg);
3521 case QEMU_OPTION_no_kvm:
3522 olist = qemu_find_opts("machine");
3523 qemu_opts_parse(olist, "accel=tcg", 0);
3525 case QEMU_OPTION_no_kvm_pit: {
3526 fprintf(stderr, "Warning: KVM PIT can no longer be disabled "
3530 case QEMU_OPTION_no_kvm_pit_reinjection: {
3531 static GlobalProperty kvm_pit_lost_tick_policy[] = {
3533 .driver = "kvm-pit",
3534 .property = "lost_tick_policy",
3537 { /* end of list */ }
3540 fprintf(stderr, "Warning: option deprecated, use "
3541 "lost_tick_policy property of kvm-pit instead.\n");
3542 qdev_prop_register_global_list(kvm_pit_lost_tick_policy);
3545 case QEMU_OPTION_usb:
3546 olist = qemu_find_opts("machine");
3547 qemu_opts_parse(olist, "usb=on", 0);
3549 case QEMU_OPTION_usbdevice:
3550 olist = qemu_find_opts("machine");
3551 qemu_opts_parse(olist, "usb=on", 0);
3552 add_device_config(DEV_USB, optarg);
3554 case QEMU_OPTION_device:
3555 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
3559 case QEMU_OPTION_smp:
3560 if (!qemu_opts_parse(qemu_find_opts("smp-opts"), optarg, 1)) {
3564 case QEMU_OPTION_vnc:
3567 vnc_display = optarg;
3569 fprintf(stderr, "VNC support is disabled\n");
3573 case QEMU_OPTION_no_acpi:
3576 case QEMU_OPTION_no_hpet:
3579 case QEMU_OPTION_balloon:
3580 if (balloon_parse(optarg) < 0) {
3581 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
3585 case QEMU_OPTION_no_reboot:
3588 case QEMU_OPTION_no_shutdown:
3591 case QEMU_OPTION_show_cursor:
3594 case QEMU_OPTION_uuid:
3595 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3596 fprintf(stderr, "Fail to parse UUID string."
3597 " Wrong format.\n");
3600 qemu_uuid_set = true;
3602 case QEMU_OPTION_option_rom:
3603 if (nb_option_roms >= MAX_OPTION_ROMS) {
3604 fprintf(stderr, "Too many option ROMs\n");
3607 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
3611 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3612 option_rom[nb_option_roms].bootindex =
3613 qemu_opt_get_number(opts, "bootindex", -1);
3614 if (!option_rom[nb_option_roms].name) {
3615 fprintf(stderr, "Option ROM file is not specified\n");
3620 case QEMU_OPTION_semihosting:
3621 semihosting_enabled = 1;
3623 case QEMU_OPTION_tdf:
3624 fprintf(stderr, "Warning: user space PIT time drift fix "
3625 "is no longer supported.\n");
3627 case QEMU_OPTION_name:
3628 opts = qemu_opts_parse(qemu_find_opts("name"), optarg, 1);
3633 case QEMU_OPTION_prom_env:
3634 if (nb_prom_envs >= MAX_PROM_ENVS) {
3635 fprintf(stderr, "Too many prom variables\n");
3638 prom_envs[nb_prom_envs] = optarg;
3641 case QEMU_OPTION_old_param:
3644 case QEMU_OPTION_clock:
3645 /* Clock options no longer exist. Keep this option for
3646 * backward compatibility.
3649 case QEMU_OPTION_startdate:
3650 configure_rtc_date_offset(optarg, 1);
3652 case QEMU_OPTION_rtc:
3653 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
3657 configure_rtc(opts);
3659 case QEMU_OPTION_tb_size:
3660 tcg_tb_size = strtol(optarg, NULL, 0);
3661 if (tcg_tb_size < 0) {
3665 case QEMU_OPTION_icount:
3666 icount_opts = qemu_opts_parse(qemu_find_opts("icount"),
3672 case QEMU_OPTION_incoming:
3674 runstate_set(RUN_STATE_INMIGRATE);
3676 case QEMU_OPTION_nodefaults:
3679 case QEMU_OPTION_xen_domid:
3680 if (!(xen_available())) {
3681 printf("Option %s not supported for this target\n", popt->name);
3684 xen_domid = atoi(optarg);
3686 case QEMU_OPTION_xen_create:
3687 if (!(xen_available())) {
3688 printf("Option %s not supported for this target\n", popt->name);
3691 xen_mode = XEN_CREATE;
3693 case QEMU_OPTION_xen_attach:
3694 if (!(xen_available())) {
3695 printf("Option %s not supported for this target\n", popt->name);
3698 xen_mode = XEN_ATTACH;
3700 case QEMU_OPTION_trace:
3702 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
3706 trace_events = qemu_opt_get(opts, "events");
3707 trace_file = qemu_opt_get(opts, "file");
3710 case QEMU_OPTION_readconfig:
3712 int ret = qemu_read_config_file(optarg);
3714 fprintf(stderr, "read config %s: %s\n", optarg,
3720 case QEMU_OPTION_spice:
3721 olist = qemu_find_opts("spice");
3723 fprintf(stderr, "spice is not supported by this qemu build.\n");
3726 opts = qemu_opts_parse(olist, optarg, 0);
3732 case QEMU_OPTION_writeconfig:
3735 if (strcmp(optarg, "-") == 0) {
3738 fp = fopen(optarg, "w");
3740 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3744 qemu_config_write(fp);
3750 case QEMU_OPTION_qtest:
3751 qtest_chrdev = optarg;
3753 case QEMU_OPTION_qtest_log:
3756 case QEMU_OPTION_sandbox:
3757 opts = qemu_opts_parse(qemu_find_opts("sandbox"), optarg, 1);
3762 case QEMU_OPTION_add_fd:
3764 opts = qemu_opts_parse(qemu_find_opts("add-fd"), optarg, 0);
3769 error_report("File descriptor passing is disabled on this "
3774 case QEMU_OPTION_object:
3775 opts = qemu_opts_parse(qemu_find_opts("object"), optarg, 1);
3780 case QEMU_OPTION_realtime:
3781 opts = qemu_opts_parse(qemu_find_opts("realtime"), optarg, 0);
3785 enable_mlock = qemu_opt_get_bool(opts, "mlock", true);
3787 case QEMU_OPTION_msg:
3788 opts = qemu_opts_parse(qemu_find_opts("msg"), optarg, 0);
3792 configure_msg(opts);
3794 case QEMU_OPTION_dump_vmstate:
3795 vmstate_dump_file = fopen(optarg, "w");
3796 if (vmstate_dump_file == NULL) {
3797 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3802 os_parse_cmd_args(popt->index, optarg);
3810 if (qemu_init_main_loop(&main_loop_err)) {
3811 error_report("%s", error_get_pretty(main_loop_err));
3815 if (qemu_opts_foreach(qemu_find_opts("sandbox"), parse_sandbox, NULL, 0)) {
3819 if (qemu_opts_foreach(qemu_find_opts("name"), parse_name, NULL, 1)) {
3824 if (qemu_opts_foreach(qemu_find_opts("add-fd"), parse_add_fd, NULL, 1)) {
3828 if (qemu_opts_foreach(qemu_find_opts("add-fd"), cleanup_add_fd, NULL, 1)) {
3833 if (machine_class == NULL) {
3834 fprintf(stderr, "No machine specified, and there is no default.\n"
3835 "Use -machine help to list supported machines!\n");
3839 current_machine = MACHINE(object_new(object_class_get_name(
3840 OBJECT_CLASS(machine_class))));
3841 object_property_add_child(object_get_root(), "machine",
3842 OBJECT(current_machine), &error_abort);
3843 cpu_exec_init_all();
3845 if (machine_class->hw_version) {
3846 qemu_set_version(machine_class->hw_version);
3849 /* Init CPU def lists, based on config
3850 * - Must be called after all the qemu_read_config_file() calls
3851 * - Must be called before list_cpus()
3852 * - Must be called before machine->init()
3856 if (cpu_model && is_help_option(cpu_model)) {
3857 list_cpus(stdout, &fprintf, cpu_model);
3861 /* Open the logfile at this point, if necessary. We can't open the logfile
3862 * when encountering either of the logging options (-d or -D) because the
3863 * other one may be encountered later on the command line, changing the
3864 * location or level of logging.
3869 qemu_set_log_filename(log_file);
3872 mask = qemu_str_to_log_mask(log_mask);
3874 qemu_print_log_usage(stdout);
3880 if (!is_daemonized()) {
3881 if (!trace_init_backends(trace_events, trace_file)) {
3886 /* If no data_dir is specified then try to find it relative to the
3888 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3889 data_dir[data_dir_idx] = os_find_datadir();
3890 if (data_dir[data_dir_idx] != NULL) {
3894 /* If all else fails use the install path specified when building. */
3895 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3896 data_dir[data_dir_idx++] = CONFIG_QEMU_DATADIR;
3899 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
3901 machine_class->max_cpus = machine_class->max_cpus ?: 1; /* Default to UP */
3902 if (smp_cpus > machine_class->max_cpus) {
3903 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3904 "supported by machine `%s' (%d)\n", smp_cpus,
3905 machine_class->name, machine_class->max_cpus);
3910 * Get the default machine options from the machine if it is not already
3911 * specified either by the configuration file or by the command line.
3913 if (machine_class->default_machine_opts) {
3914 qemu_opts_set_defaults(qemu_find_opts("machine"),
3915 machine_class->default_machine_opts, 0);
3918 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
3919 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
3921 if (!vga_model && !default_vga) {
3922 vga_interface_type = VGA_DEVICE;
3924 if (!has_defaults || machine_class->no_serial) {
3927 if (!has_defaults || machine_class->no_parallel) {
3928 default_parallel = 0;
3930 if (!has_defaults || !machine_class->use_virtcon) {
3931 default_virtcon = 0;
3933 if (!has_defaults || !machine_class->use_sclp) {
3936 if (!has_defaults || machine_class->no_floppy) {
3939 if (!has_defaults || machine_class->no_cdrom) {
3942 if (!has_defaults || machine_class->no_sdcard) {
3945 if (!has_defaults) {
3946 default_monitor = 0;
3951 if (is_daemonized()) {
3952 /* According to documentation and historically, -nographic redirects
3953 * serial port, parallel port and monitor to stdio, which does not work
3954 * with -daemonize. We can redirect these to null instead, but since
3955 * -nographic is legacy, let's just error out.
3956 * We disallow -nographic only if all other ports are not redirected
3957 * explicitly, to not break existing legacy setups which uses
3958 * -nographic _and_ redirects all ports explicitly - this is valid
3959 * usage, -nographic is just a no-op in this case.
3961 if (display_type == DT_NOGRAPHIC
3962 && (default_parallel || default_serial
3963 || default_monitor || default_virtcon)) {
3964 fprintf(stderr, "-nographic can not be used with -daemonize\n");
3967 #ifdef CONFIG_CURSES
3968 if (display_type == DT_CURSES) {
3969 fprintf(stderr, "curses display can not be used with -daemonize\n");
3975 if (display_type == DT_NOGRAPHIC) {
3976 if (default_parallel)
3977 add_device_config(DEV_PARALLEL, "null");
3978 if (default_serial && default_monitor) {
3979 add_device_config(DEV_SERIAL, "mon:stdio");
3980 } else if (default_virtcon && default_monitor) {
3981 add_device_config(DEV_VIRTCON, "mon:stdio");
3982 } else if (default_sclp && default_monitor) {
3983 add_device_config(DEV_SCLP, "mon:stdio");
3986 add_device_config(DEV_SERIAL, "stdio");
3987 if (default_virtcon)
3988 add_device_config(DEV_VIRTCON, "stdio");
3990 add_device_config(DEV_SCLP, "stdio");
3992 if (default_monitor)
3993 monitor_parse("stdio", "readline");
3997 add_device_config(DEV_SERIAL, "vc:80Cx24C");
3998 if (default_parallel)
3999 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
4000 if (default_monitor)
4001 monitor_parse("vc:80Cx24C", "readline");
4002 if (default_virtcon)
4003 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
4005 add_device_config(DEV_SCLP, "vc:80Cx24C");
4009 if (display_type == DT_DEFAULT && !display_remote) {
4010 #if defined(CONFIG_GTK)
4011 display_type = DT_GTK;
4012 #elif defined(CONFIG_SDL) || defined(CONFIG_COCOA)
4013 display_type = DT_SDL;
4014 #elif defined(CONFIG_VNC)
4015 vnc_display = "localhost:0,to=99";
4018 display_type = DT_NONE;
4022 if ((no_frame || alt_grab || ctrl_grab) && display_type != DT_SDL) {
4023 fprintf(stderr, "-no-frame, -alt-grab and -ctrl-grab are only valid "
4024 "for SDL, ignoring option\n");
4026 if (no_quit && (display_type != DT_GTK && display_type != DT_SDL)) {
4027 fprintf(stderr, "-no-quit is only valid for GTK and SDL, "
4028 "ignoring option\n");
4031 #if defined(CONFIG_GTK)
4032 if (display_type == DT_GTK) {
4033 early_gtk_display_init();
4039 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
4041 #ifdef CONFIG_VIRTFS
4042 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
4047 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
4052 /* store value for the future use */
4053 qemu_opt_set_number(qemu_find_opts_singleton("memory"), "size", ram_size);
4055 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0)
4060 if (qemu_opts_foreach(qemu_find_opts("object"),
4061 object_create, NULL, 0) != 0) {
4065 machine_opts = qemu_get_machine_opts();
4066 if (qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
4068 object_unref(OBJECT(current_machine));
4072 configure_accelerator(current_machine);
4075 Error *local_err = NULL;
4076 qtest_init(qtest_chrdev, qtest_log, &local_err);
4078 error_report("%s", error_get_pretty(local_err));
4079 error_free(local_err);
4084 machine_opts = qemu_get_machine_opts();
4085 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4086 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4087 kernel_cmdline = qemu_opt_get(machine_opts, "append");
4088 bios_name = qemu_opt_get(machine_opts, "firmware");
4090 boot_order = machine_class->default_boot_order;
4091 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4093 char *normal_boot_order;
4094 const char *order, *once;
4096 order = qemu_opt_get(opts, "order");
4098 validate_bootdevices(order);
4102 once = qemu_opt_get(opts, "once");
4104 validate_bootdevices(once);
4105 normal_boot_order = g_strdup(boot_order);
4107 qemu_register_reset(restore_boot_order, normal_boot_order);
4110 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
4111 boot_strict = qemu_opt_get_bool(opts, "strict", false);
4114 if (!kernel_cmdline) {
4115 kernel_cmdline = "";
4116 current_machine->kernel_cmdline = (char *)kernel_cmdline;
4119 linux_boot = (kernel_filename != NULL);
4121 if (!linux_boot && *kernel_cmdline != '\0') {
4122 fprintf(stderr, "-append only allowed with -kernel option\n");
4126 if (!linux_boot && initrd_filename != NULL) {
4127 fprintf(stderr, "-initrd only allowed with -kernel option\n");
4131 if (!linux_boot && qemu_opt_get(machine_opts, "dtb")) {
4132 fprintf(stderr, "-dtb only allowed with -kernel option\n");
4136 os_set_line_buffering();
4138 qemu_init_cpu_loop();
4139 qemu_mutex_lock_iothread();
4142 /* spice needs the timers to be initialized by this point */
4148 if (kvm_enabled() || xen_enabled()) {
4149 fprintf(stderr, "-icount is not allowed with kvm or xen\n");
4152 configure_icount(icount_opts, &error_abort);
4153 qemu_opts_del(icount_opts);
4156 /* clean up network at qemu process termination */
4157 atexit(&net_cleanup);
4159 if (net_init_clients() < 0) {
4164 if (tpm_init() < 0) {
4169 /* init the bluetooth world */
4170 if (foreach_device_config(DEV_BT, bt_parse))
4173 if (!xen_enabled()) {
4174 /* On 32-bit hosts, QEMU is limited by virtual address space */
4175 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4176 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
4184 /* If the currently selected machine wishes to override the units-per-bus
4185 * property of its default HBA interface type, do so now. */
4186 if (machine_class->units_per_default_bus) {
4187 override_max_devs(machine_class->block_default_type,
4188 machine_class->units_per_default_bus);
4191 /* open the virtual block devices */
4193 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
4194 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
4195 &machine_class->block_default_type, 1) != 0) {
4199 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
4201 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4202 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
4204 if (qemu_opts_foreach(qemu_find_opts("numa"), numa_init_func,
4211 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
4215 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4217 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4219 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4221 if (foreach_device_config(DEV_SCLP, sclp_parse) < 0) {
4224 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4227 /* If no default VGA is requested, the default is "none". */
4229 if (cirrus_vga_available()) {
4230 vga_model = "cirrus";
4231 } else if (vga_available()) {
4236 select_vgahw(vga_model);
4240 i = select_watchdog(watchdog);
4242 exit (i == 1 ? 1 : 0);
4245 if (machine_class->compat_props) {
4246 qdev_prop_register_global_list(machine_class->compat_props);
4250 qdev_machine_init();
4252 current_machine->ram_size = ram_size;
4253 current_machine->maxram_size = maxram_size;
4254 current_machine->ram_slots = ram_slots;
4255 current_machine->boot_order = boot_order;
4256 current_machine->cpu_model = cpu_model;
4258 machine_class->init(current_machine);
4264 cpu_synchronize_all_post_init();
4268 /* init USB devices */
4269 if (usb_enabled(false)) {
4270 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4274 /* init generic devices */
4275 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
4278 /* Did we create any drives that we failed to create a device for? */
4279 drive_check_orphaned();
4281 net_check_clients();
4283 ds = init_displaystate();
4285 /* init local displays */
4286 switch (display_type) {
4288 (void)ds; /* avoid warning if no display is configured */
4290 #if defined(CONFIG_CURSES)
4292 curses_display_init(ds, full_screen);
4295 #if defined(CONFIG_SDL)
4297 sdl_display_init(ds, full_screen, no_frame);
4299 #elif defined(CONFIG_COCOA)
4301 cocoa_display_init(ds, full_screen);
4304 #if defined(CONFIG_GTK)
4306 gtk_display_init(ds, full_screen, grab_on_hover);
4313 /* must be after terminal init, SDL library changes signal handlers */
4314 os_setup_signal_handling();
4317 /* init remote displays */
4319 Error *local_err = NULL;
4320 vnc_display_init(ds);
4321 vnc_display_open(ds, vnc_display, &local_err);
4322 if (local_err != NULL) {
4323 error_report("Failed to start VNC server on `%s': %s",
4324 vnc_display, error_get_pretty(local_err));
4325 error_free(local_err);
4329 if (show_vnc_port) {
4330 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
4336 qemu_spice_display_init();
4340 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
4344 qdev_machine_creation_done();
4346 if (rom_load_all() != 0) {
4347 fprintf(stderr, "rom loading failed\n");
4351 /* TODO: once all bus devices are qdevified, this should be done
4352 * when bus is created by qdev.c */
4353 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4354 qemu_run_machine_init_done_notifiers();
4356 /* Done notifiers can load ROMs */
4359 qemu_system_reset(VMRESET_SILENT);
4361 if (load_vmstate(loadvm) < 0) {
4366 qdev_prop_check_globals();
4367 if (vmstate_dump_file) {
4369 dump_vmstate_json_to_file(vmstate_dump_file);
4374 Error *local_err = NULL;
4375 qemu_start_incoming_migration(incoming, &local_err);
4377 error_report("-incoming %s: %s", incoming,
4378 error_get_pretty(local_err));
4379 error_free(local_err);
4382 } else if (autostart) {
4388 if (is_daemonized()) {
4389 if (!trace_init_backends(trace_events, trace_file)) {