4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "qemu/osdep.h"
26 #include "qemu-common.h"
27 #include "qemu/datadir.h"
28 #include "qemu/units.h"
29 #include "exec/cpu-common.h"
30 #include "hw/qdev-properties.h"
31 #include "qapi/compat-policy.h"
32 #include "qapi/error.h"
33 #include "qapi/qmp/qdict.h"
34 #include "qapi/qmp/qstring.h"
35 #include "qapi/qmp/qjson.h"
36 #include "qemu-version.h"
37 #include "qemu/cutils.h"
38 #include "qemu/help_option.h"
39 #include "qemu/uuid.h"
40 #include "sysemu/reset.h"
41 #include "sysemu/runstate.h"
42 #include "sysemu/runstate-action.h"
43 #include "sysemu/seccomp.h"
44 #include "sysemu/tcg.h"
45 #include "sysemu/xen.h"
47 #include "qemu/error-report.h"
48 #include "qemu/sockets.h"
49 #include "qemu/accel.h"
51 #include "hw/isa/isa.h"
52 #include "hw/scsi/scsi.h"
53 #include "hw/display/vga.h"
54 #include "sysemu/watchdog.h"
55 #include "hw/firmware/smbios.h"
56 #include "hw/acpi/acpi.h"
57 #include "hw/xen/xen.h"
58 #include "hw/loader.h"
59 #include "monitor/qdev.h"
61 #include "net/slirp.h"
62 #include "monitor/monitor.h"
63 #include "ui/console.h"
65 #include "sysemu/sysemu.h"
66 #include "sysemu/numa.h"
67 #include "sysemu/hostmem.h"
68 #include "exec/gdbstub.h"
69 #include "qemu/timer.h"
70 #include "chardev/char.h"
71 #include "qemu/bitmap.h"
73 #include "sysemu/blockdev.h"
74 #include "hw/block/block.h"
75 #include "hw/i386/x86.h"
76 #include "hw/i386/pc.h"
77 #include "migration/misc.h"
78 #include "migration/snapshot.h"
79 #include "sysemu/tpm.h"
80 #include "sysemu/dma.h"
81 #include "hw/audio/soundhw.h"
82 #include "audio/audio.h"
83 #include "sysemu/cpus.h"
84 #include "sysemu/cpu-timers.h"
85 #include "migration/colo.h"
86 #include "migration/postcopy-ram.h"
87 #include "sysemu/kvm.h"
88 #include "sysemu/hax.h"
89 #include "qapi/qobject-input-visitor.h"
90 #include "qemu/option.h"
91 #include "qemu/config-file.h"
92 #include "qemu/qemu-options.h"
93 #include "qemu/main-loop.h"
95 #include "fsdev/qemu-fsdev.h"
97 #include "sysemu/qtest.h"
99 #include "disas/disas.h"
102 #include "trace/control.h"
103 #include "qemu/plugin.h"
104 #include "qemu/queue.h"
105 #include "sysemu/arch_init.h"
106 #include "exec/confidential-guest-support.h"
108 #include "ui/qemu-spice.h"
109 #include "qapi/string-input-visitor.h"
110 #include "qapi/opts-visitor.h"
111 #include "qapi/clone-visitor.h"
112 #include "qom/object_interfaces.h"
113 #include "semihosting/semihost.h"
114 #include "crypto/init.h"
115 #include "sysemu/replay.h"
116 #include "qapi/qapi-events-run-state.h"
117 #include "qapi/qapi-visit-block-core.h"
118 #include "qapi/qapi-visit-compat.h"
119 #include "qapi/qapi-visit-ui.h"
120 #include "qapi/qapi-commands-block-core.h"
121 #include "qapi/qapi-commands-migration.h"
122 #include "qapi/qapi-commands-misc.h"
123 #include "qapi/qapi-visit-qom.h"
124 #include "qapi/qapi-commands-ui.h"
125 #include "qapi/qmp/qdict.h"
126 #include "qapi/qmp/qerror.h"
127 #include "sysemu/iothread.h"
128 #include "qemu/guest-random.h"
130 #include "config-host.h"
132 #define MAX_VIRTIO_CONSOLES 1
134 typedef struct BlockdevOptionsQueueEntry {
135 BlockdevOptions *bdo;
137 QSIMPLEQ_ENTRY(BlockdevOptionsQueueEntry) entry;
138 } BlockdevOptionsQueueEntry;
140 typedef QSIMPLEQ_HEAD(, BlockdevOptionsQueueEntry) BlockdevOptionsQueue;
142 typedef struct ObjectOption {
144 QTAILQ_ENTRY(ObjectOption) next;
147 static const char *cpu_option;
148 static const char *mem_path;
149 static const char *incoming;
150 static const char *loadvm;
151 static const char *accelerators;
152 static QDict *machine_opts_dict;
153 static QTAILQ_HEAD(, ObjectOption) object_opts = QTAILQ_HEAD_INITIALIZER(object_opts);
154 static ram_addr_t maxram_size;
155 static uint64_t ram_slots;
156 static int display_remote;
158 static bool preconfig_requested;
159 static QemuPluginList plugin_list = QTAILQ_HEAD_INITIALIZER(plugin_list);
160 static BlockdevOptionsQueue bdo_queue = QSIMPLEQ_HEAD_INITIALIZER(bdo_queue);
161 static bool nographic = false;
162 static int mem_prealloc; /* force preallocation of physical target memory */
163 static ram_addr_t ram_size;
164 static const char *vga_model = NULL;
165 static DisplayOptions dpy;
166 static int num_serial_hds;
167 static Chardev **serial_hds;
168 static const char *log_mask;
169 static const char *log_file;
170 static bool list_data_dirs;
171 static const char *watchdog;
172 static const char *qtest_chrdev;
173 static const char *qtest_log;
175 static int has_defaults = 1;
176 static int default_serial = 1;
177 static int default_parallel = 1;
178 static int default_monitor = 1;
179 static int default_floppy = 1;
180 static int default_cdrom = 1;
181 static int default_sdcard = 1;
182 static int default_vga = 1;
183 static int default_net = 1;
189 { .driver = "isa-serial", .flag = &default_serial },
190 { .driver = "isa-parallel", .flag = &default_parallel },
191 { .driver = "isa-fdc", .flag = &default_floppy },
192 { .driver = "floppy", .flag = &default_floppy },
193 { .driver = "ide-cd", .flag = &default_cdrom },
194 { .driver = "ide-hd", .flag = &default_cdrom },
195 { .driver = "scsi-cd", .flag = &default_cdrom },
196 { .driver = "scsi-hd", .flag = &default_cdrom },
197 { .driver = "VGA", .flag = &default_vga },
198 { .driver = "isa-vga", .flag = &default_vga },
199 { .driver = "cirrus-vga", .flag = &default_vga },
200 { .driver = "isa-cirrus-vga", .flag = &default_vga },
201 { .driver = "vmware-svga", .flag = &default_vga },
202 { .driver = "qxl-vga", .flag = &default_vga },
203 { .driver = "virtio-vga", .flag = &default_vga },
204 { .driver = "ati-vga", .flag = &default_vga },
205 { .driver = "vhost-user-vga", .flag = &default_vga },
206 { .driver = "virtio-vga-gl", .flag = &default_vga },
209 static QemuOptsList qemu_rtc_opts = {
211 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
216 .type = QEMU_OPT_STRING,
219 .type = QEMU_OPT_STRING,
222 .type = QEMU_OPT_STRING,
224 { /* end of list */ }
228 static QemuOptsList qemu_option_rom_opts = {
229 .name = "option-rom",
230 .implied_opt_name = "romfile",
231 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
235 .type = QEMU_OPT_NUMBER,
238 .type = QEMU_OPT_STRING,
240 { /* end of list */ }
244 static QemuOptsList qemu_accel_opts = {
246 .implied_opt_name = "accel",
247 .head = QTAILQ_HEAD_INITIALIZER(qemu_accel_opts.head),
250 * no elements => accept any
251 * sanity checking will happen later
252 * when setting accelerator properties
258 static QemuOptsList qemu_boot_opts = {
260 .implied_opt_name = "order",
262 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
266 .type = QEMU_OPT_STRING,
269 .type = QEMU_OPT_STRING,
272 .type = QEMU_OPT_BOOL,
275 .type = QEMU_OPT_STRING,
277 .name = "splash-time",
278 .type = QEMU_OPT_NUMBER,
280 .name = "reboot-timeout",
281 .type = QEMU_OPT_NUMBER,
284 .type = QEMU_OPT_BOOL,
290 static QemuOptsList qemu_add_fd_opts = {
292 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
296 .type = QEMU_OPT_NUMBER,
297 .help = "file descriptor of which a duplicate is added to fd set",
300 .type = QEMU_OPT_NUMBER,
301 .help = "ID of the fd set to add fd to",
304 .type = QEMU_OPT_STRING,
305 .help = "free-form string used to describe fd",
307 { /* end of list */ }
311 static QemuOptsList qemu_object_opts = {
313 .implied_opt_name = "qom-type",
314 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
320 static QemuOptsList qemu_tpmdev_opts = {
322 .implied_opt_name = "type",
323 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
325 /* options are defined in the TPM backends */
326 { /* end of list */ }
330 static QemuOptsList qemu_overcommit_opts = {
331 .name = "overcommit",
332 .head = QTAILQ_HEAD_INITIALIZER(qemu_overcommit_opts.head),
336 .type = QEMU_OPT_BOOL,
340 .type = QEMU_OPT_BOOL,
342 { /* end of list */ }
346 static QemuOptsList qemu_msg_opts = {
348 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
352 .type = QEMU_OPT_BOOL,
355 .name = "guest-name",
356 .type = QEMU_OPT_BOOL,
357 .help = "Prepends guest name for error messages but only if "
358 "-name guest is set otherwise option is ignored\n",
360 { /* end of list */ }
364 static QemuOptsList qemu_name_opts = {
366 .implied_opt_name = "guest",
368 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
372 .type = QEMU_OPT_STRING,
373 .help = "Sets the name of the guest.\n"
374 "This name will be displayed in the SDL window caption.\n"
375 "The name will also be used for the VNC server",
378 .type = QEMU_OPT_STRING,
379 .help = "Sets the name of the QEMU process, as shown in top etc",
381 .name = "debug-threads",
382 .type = QEMU_OPT_BOOL,
383 .help = "When enabled, name the individual threads; defaults off.\n"
384 "NOTE: The thread names are for debugging and not a\n"
387 { /* End of list */ }
391 static QemuOptsList qemu_mem_opts = {
393 .implied_opt_name = "size",
394 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
399 .type = QEMU_OPT_SIZE,
403 .type = QEMU_OPT_NUMBER,
407 .type = QEMU_OPT_SIZE,
409 { /* end of list */ }
413 static QemuOptsList qemu_icount_opts = {
415 .implied_opt_name = "shift",
417 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
421 .type = QEMU_OPT_STRING,
424 .type = QEMU_OPT_BOOL,
427 .type = QEMU_OPT_BOOL,
430 .type = QEMU_OPT_STRING,
433 .type = QEMU_OPT_STRING,
435 .name = "rrsnapshot",
436 .type = QEMU_OPT_STRING,
438 { /* end of list */ }
442 static QemuOptsList qemu_fw_cfg_opts = {
444 .implied_opt_name = "name",
445 .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
449 .type = QEMU_OPT_STRING,
450 .help = "Sets the fw_cfg name of the blob to be inserted",
453 .type = QEMU_OPT_STRING,
454 .help = "Sets the name of the file from which "
455 "the fw_cfg blob will be loaded",
458 .type = QEMU_OPT_STRING,
459 .help = "Sets content of the blob to be inserted from a string",
462 .type = QEMU_OPT_STRING,
463 .help = "Sets id of the object generating the fw_cfg blob "
466 { /* end of list */ }
470 static QemuOptsList qemu_action_opts = {
473 .head = QTAILQ_HEAD_INITIALIZER(qemu_action_opts.head),
477 .type = QEMU_OPT_STRING,
480 .type = QEMU_OPT_STRING,
483 .type = QEMU_OPT_STRING,
486 .type = QEMU_OPT_STRING,
488 { /* end of list */ }
492 const char *qemu_get_vm_name(void)
497 static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
499 const char *driver = qemu_opt_get(opts, "driver");
504 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
505 if (strcmp(default_list[i].driver, driver) != 0)
507 *(default_list[i].flag) = 0;
512 static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
514 const char *proc_name;
516 if (qemu_opt_get(opts, "debug-threads")) {
517 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
519 qemu_name = qemu_opt_get(opts, "guest");
521 proc_name = qemu_opt_get(opts, "process");
523 os_set_proc_name(proc_name);
529 bool defaults_enabled(void)
535 static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
537 int fd, dupfd, flags;
539 const char *fd_opaque = NULL;
542 fd = qemu_opt_get_number(opts, "fd", -1);
543 fdset_id = qemu_opt_get_number(opts, "set", -1);
544 fd_opaque = qemu_opt_get(opts, "opaque");
547 error_setg(errp, "fd option is required and must be non-negative");
551 if (fd <= STDERR_FILENO) {
552 error_setg(errp, "fd cannot be a standard I/O stream");
557 * All fds inherited across exec() necessarily have FD_CLOEXEC
558 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
560 flags = fcntl(fd, F_GETFD);
561 if (flags == -1 || (flags & FD_CLOEXEC)) {
562 error_setg(errp, "fd is not valid or already in use");
567 error_setg(errp, "set option is required and must be non-negative");
571 #ifdef F_DUPFD_CLOEXEC
572 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
576 qemu_set_cloexec(dupfd);
580 error_setg(errp, "error duplicating fd: %s", strerror(errno));
584 /* add the duplicate fd, and optionally the opaque string, to the fd set */
585 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
592 static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
596 fd = qemu_opt_get_number(opts, "fd", -1);
603 /***********************************************************/
604 /* QEMU Block devices */
606 #define HD_OPTS "media=disk"
607 #define CDROM_OPTS "media=cdrom"
609 #define PFLASH_OPTS ""
613 static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
615 BlockInterfaceType *block_default_type = opaque;
617 return drive_new(opts, *block_default_type, errp) == NULL;
620 static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
622 if (qemu_opt_get(opts, "snapshot") == NULL) {
623 qemu_opt_set(opts, "snapshot", "on", &error_abort);
628 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
629 int index, const char *optstr)
634 if (!enable || drive_get_by_index(type, index)) {
638 opts = drive_add(type, index, NULL, optstr);
640 drive_enable_snapshot(NULL, opts, NULL);
643 dinfo = drive_new(opts, type, &error_abort);
644 dinfo->is_default = true;
648 static void configure_blockdev(BlockdevOptionsQueue *bdo_queue,
649 MachineClass *machine_class, int snapshot)
652 * If the currently selected machine wishes to override the
653 * units-per-bus property of its default HBA interface type, do so
656 if (machine_class->units_per_default_bus) {
657 override_max_devs(machine_class->block_default_type,
658 machine_class->units_per_default_bus);
661 /* open the virtual block devices */
662 while (!QSIMPLEQ_EMPTY(bdo_queue)) {
663 BlockdevOptionsQueueEntry *bdo = QSIMPLEQ_FIRST(bdo_queue);
665 QSIMPLEQ_REMOVE_HEAD(bdo_queue, entry);
666 loc_push_restore(&bdo->loc);
667 qmp_blockdev_add(bdo->bdo, &error_fatal);
669 qapi_free_BlockdevOptions(bdo->bdo);
673 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
676 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
677 &machine_class->block_default_type, &error_fatal)) {
678 /* We printed help */
682 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
684 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
685 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
689 static QemuOptsList qemu_smp_opts = {
691 .implied_opt_name = "cpus",
693 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
697 .type = QEMU_OPT_NUMBER,
700 .type = QEMU_OPT_NUMBER,
703 .type = QEMU_OPT_NUMBER,
706 .type = QEMU_OPT_NUMBER,
709 .type = QEMU_OPT_NUMBER,
712 .type = QEMU_OPT_NUMBER,
718 static void realtime_init(void)
721 if (os_mlock() < 0) {
722 error_report("locking memory failed");
729 static void configure_msg(QemuOpts *opts)
731 message_with_timestamp = qemu_opt_get_bool(opts, "timestamp", false);
732 error_with_guestname = qemu_opt_get_bool(opts, "guest-name", false);
736 /***********************************************************/
739 static int usb_device_add(const char *devname)
741 USBDevice *dev = NULL;
743 if (!machine_usb(current_machine)) {
747 dev = usbdevice_create(devname);
754 static int usb_parse(const char *cmdline)
757 r = usb_device_add(cmdline);
759 error_report("could not add USB device '%s'", cmdline);
764 /***********************************************************/
765 /* machine registration */
767 static MachineClass *find_machine(const char *name, GSList *machines)
771 for (el = machines; el; el = el->next) {
772 MachineClass *mc = el->data;
774 if (!strcmp(mc->name, name) || !g_strcmp0(mc->alias, name)) {
782 static MachineClass *find_default_machine(GSList *machines)
785 MachineClass *default_machineclass = NULL;
787 for (el = machines; el; el = el->next) {
788 MachineClass *mc = el->data;
790 if (mc->is_default) {
791 assert(default_machineclass == NULL && "Multiple default machines");
792 default_machineclass = mc;
796 return default_machineclass;
799 static void version(void)
801 printf("QEMU emulator version " QEMU_FULL_VERSION "\n"
802 QEMU_COPYRIGHT "\n");
805 static void help(int exitcode)
808 printf("usage: %s [options] [disk_image]\n\n"
809 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
810 error_get_progname());
812 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
813 if ((arch_mask) & arch_type) \
814 fputs(opt_help, stdout);
816 #define ARCHHEADING(text, arch_mask) \
817 if ((arch_mask) & arch_type) \
818 puts(stringify(text));
820 #define DEFHEADING(text) ARCHHEADING(text, QEMU_ARCH_ALL)
822 #include "qemu-options.def"
824 printf("\nDuring emulation, the following keys are useful:\n"
825 "ctrl-alt-f toggle full screen\n"
826 "ctrl-alt-n switch to virtual console 'n'\n"
827 "ctrl-alt toggle mouse and keyboard grab\n"
829 "When using -nographic, press 'ctrl-a h' to get some help.\n"
831 QEMU_HELP_BOTTOM "\n");
836 #define HAS_ARG 0x0001
838 typedef struct QEMUOption {
845 static const QEMUOption qemu_options[] = {
846 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
848 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
849 { option, opt_arg, opt_enum, arch_mask },
850 #define DEFHEADING(text)
851 #define ARCHHEADING(text, arch_mask)
853 #include "qemu-options.def"
857 typedef struct VGAInterfaceInfo {
858 const char *opt_name; /* option name */
859 const char *name; /* human-readable name */
860 /* Class names indicating that support is available.
861 * If no class is specified, the interface is always available */
862 const char *class_names[2];
865 static const VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
868 .name = "no graphic card",
872 .name = "standard VGA",
873 .class_names = { "VGA", "isa-vga" },
876 .opt_name = "cirrus",
877 .name = "Cirrus VGA",
878 .class_names = { "cirrus-vga", "isa-cirrus-vga" },
881 .opt_name = "vmware",
882 .name = "VMWare SVGA",
883 .class_names = { "vmware-svga" },
886 .opt_name = "virtio",
887 .name = "Virtio VGA",
888 .class_names = { "virtio-vga" },
893 .class_names = { "qxl-vga" },
897 .name = "TCX framebuffer",
898 .class_names = { "sun-tcx" },
902 .name = "CG3 framebuffer",
903 .class_names = { "cgthree" },
907 .name = "Xen paravirtualized framebuffer",
911 static bool vga_interface_available(VGAInterfaceType t)
913 const VGAInterfaceInfo *ti = &vga_interfaces[t];
915 assert(t < VGA_TYPE_MAX);
916 return !ti->class_names[0] ||
917 module_object_class_by_name(ti->class_names[0]) ||
918 module_object_class_by_name(ti->class_names[1]);
922 get_default_vga_model(const MachineClass *machine_class)
924 if (machine_class->default_display) {
925 return machine_class->default_display;
926 } else if (vga_interface_available(VGA_CIRRUS)) {
928 } else if (vga_interface_available(VGA_STD)) {
935 static void select_vgahw(const MachineClass *machine_class, const char *p)
940 if (g_str_equal(p, "help")) {
941 const char *def = get_default_vga_model(machine_class);
943 for (t = 0; t < VGA_TYPE_MAX; t++) {
944 const VGAInterfaceInfo *ti = &vga_interfaces[t];
946 if (vga_interface_available(t) && ti->opt_name) {
947 printf("%-20s %s%s\n", ti->opt_name, ti->name ?: "",
948 g_str_equal(ti->opt_name, def) ? " (default)" : "");
954 assert(vga_interface_type == VGA_NONE);
955 for (t = 0; t < VGA_TYPE_MAX; t++) {
956 const VGAInterfaceInfo *ti = &vga_interfaces[t];
957 if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
958 if (!vga_interface_available(t)) {
959 error_report("%s not available", ti->name);
962 vga_interface_type = t;
966 if (t == VGA_TYPE_MAX) {
968 error_report("unknown vga type: %s", p);
974 if (strstart(opts, ",retrace=", &nextopt)) {
976 if (strstart(opts, "dumb", &nextopt))
977 vga_retrace_method = VGA_RETRACE_DUMB;
978 else if (strstart(opts, "precise", &nextopt))
979 vga_retrace_method = VGA_RETRACE_PRECISE;
980 else goto invalid_vga;
981 } else goto invalid_vga;
986 static void parse_display_qapi(const char *optarg)
988 DisplayOptions *opts;
991 v = qobject_input_visitor_new_str(optarg, "type", &error_fatal);
993 visit_type_DisplayOptions(v, NULL, &opts, &error_fatal);
994 QAPI_CLONE_MEMBERS(DisplayOptions, &dpy, opts);
996 qapi_free_DisplayOptions(opts);
1000 DisplayOptions *qmp_query_display_options(Error **errp)
1002 return QAPI_CLONE(DisplayOptions, &dpy);
1005 static void parse_display(const char *p)
1009 if (is_help_option(p)) {
1010 qemu_display_help();
1014 if (strstart(p, "sdl", &opts)) {
1016 * sdl DisplayType needs hand-crafted parser instead of
1017 * parse_display_qapi() due to some options not in
1018 * DisplayOptions, specifically:
1019 * - ctrl_grab + alt_grab
1020 * Not clear yet what happens to them long-term. Should
1021 * replaced by something better or deprecated and dropped.
1023 #if defined(CONFIG_SDL)
1024 dpy.type = DISPLAY_TYPE_SDL;
1026 const char *nextopt;
1028 if (strstart(opts, ",alt_grab=", &nextopt)) {
1030 if (strstart(opts, "on", &nextopt)) {
1032 } else if (strstart(opts, "off", &nextopt)) {
1035 goto invalid_sdl_args;
1037 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1039 if (strstart(opts, "on", &nextopt)) {
1041 } else if (strstart(opts, "off", &nextopt)) {
1044 goto invalid_sdl_args;
1046 } else if (strstart(opts, ",window_close=", &nextopt) ||
1047 strstart(opts, ",window-close=", &nextopt)) {
1048 if (strstart(opts, ",window_close=", NULL)) {
1049 warn_report("window_close with an underscore is deprecated,"
1050 " please use window-close instead.");
1053 dpy.has_window_close = true;
1054 if (strstart(opts, "on", &nextopt)) {
1055 dpy.window_close = true;
1056 } else if (strstart(opts, "off", &nextopt)) {
1057 dpy.window_close = false;
1059 goto invalid_sdl_args;
1061 } else if (strstart(opts, ",show-cursor=", &nextopt)) {
1063 dpy.has_show_cursor = true;
1064 if (strstart(opts, "on", &nextopt)) {
1065 dpy.show_cursor = true;
1066 } else if (strstart(opts, "off", &nextopt)) {
1067 dpy.show_cursor = false;
1069 goto invalid_sdl_args;
1071 } else if (strstart(opts, ",gl=", &nextopt)) {
1074 if (strstart(opts, "on", &nextopt)) {
1075 dpy.gl = DISPLAYGL_MODE_ON;
1076 } else if (strstart(opts, "core", &nextopt)) {
1077 dpy.gl = DISPLAYGL_MODE_CORE;
1078 } else if (strstart(opts, "es", &nextopt)) {
1079 dpy.gl = DISPLAYGL_MODE_ES;
1080 } else if (strstart(opts, "off", &nextopt)) {
1081 dpy.gl = DISPLAYGL_MODE_OFF;
1083 goto invalid_sdl_args;
1087 error_report("invalid SDL option string");
1093 error_report("SDL display supported is not available in this binary");
1096 } else if (strstart(p, "vnc", &opts)) {
1098 * vnc isn't a (local) DisplayType but a protocol for remote
1102 vnc_parse(opts + 1);
1104 error_report("VNC requires a display argument vnc=<display>");
1108 parse_display_qapi(p);
1112 static inline bool nonempty_str(const char *str)
1117 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
1121 const char *name, *file, *str, *gen_id;
1122 FWCfgState *fw_cfg = (FWCfgState *) opaque;
1124 if (fw_cfg == NULL) {
1125 error_setg(errp, "fw_cfg device not available");
1128 name = qemu_opt_get(opts, "name");
1129 file = qemu_opt_get(opts, "file");
1130 str = qemu_opt_get(opts, "string");
1131 gen_id = qemu_opt_get(opts, "gen_id");
1133 /* we need the name, and exactly one of: file, content string, gen_id */
1134 if (!nonempty_str(name) ||
1135 nonempty_str(file) + nonempty_str(str) + nonempty_str(gen_id) != 1) {
1136 error_setg(errp, "name, plus exactly one of file,"
1137 " string and gen_id, are needed");
1140 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
1141 error_setg(errp, "name too long (max. %d char)",
1142 FW_CFG_MAX_FILE_PATH - 1);
1145 if (nonempty_str(gen_id)) {
1147 * In this particular case where the content is populated
1148 * internally, the "etc/" namespace protection is relaxed,
1149 * so do not emit a warning.
1151 } else if (strncmp(name, "opt/", 4) != 0) {
1152 warn_report("externally provided fw_cfg item names "
1153 "should be prefixed with \"opt/\"");
1155 if (nonempty_str(str)) {
1156 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
1157 buf = g_memdup(str, size);
1158 } else if (nonempty_str(gen_id)) {
1159 if (!fw_cfg_add_from_generator(fw_cfg, name, gen_id, errp)) {
1165 if (!g_file_get_contents(file, &buf, &size, &err)) {
1166 error_setg(errp, "can't load %s: %s", file, err->message);
1171 /* For legacy, keep user files in a specific global order. */
1172 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
1173 fw_cfg_add_file(fw_cfg, name, buf, size);
1174 fw_cfg_reset_order_override(fw_cfg);
1178 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
1180 return qdev_device_help(opts);
1183 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
1187 dev = qdev_device_add(opts, errp);
1188 if (!dev && *errp) {
1189 error_report_err(*errp);
1192 object_unref(OBJECT(dev));
1197 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
1199 Error *local_err = NULL;
1201 if (!qemu_chr_new_from_opts(opts, NULL, &local_err)) {
1203 error_propagate(errp, local_err);
1211 #ifdef CONFIG_VIRTFS
1212 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
1214 return qemu_fsdev_add(opts, errp);
1218 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
1220 return monitor_init_opts(opts, errp);
1223 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
1225 static int monitor_device_index = 0;
1230 if (strstart(optarg, "chardev:", &p)) {
1231 snprintf(label, sizeof(label), "%s", p);
1233 snprintf(label, sizeof(label), "compat_monitor%d",
1234 monitor_device_index);
1235 opts = qemu_chr_parse_compat(label, optarg, true);
1237 error_report("parse error: %s", optarg);
1242 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
1243 qemu_opt_set(opts, "mode", mode, &error_abort);
1244 qemu_opt_set(opts, "chardev", label, &error_abort);
1245 if (!strcmp(mode, "control")) {
1246 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
1248 assert(pretty == false);
1250 monitor_device_index++;
1253 struct device_config {
1255 DEV_USB, /* -usbdevice */
1256 DEV_SERIAL, /* -serial */
1257 DEV_PARALLEL, /* -parallel */
1258 DEV_DEBUGCON, /* -debugcon */
1259 DEV_GDB, /* -gdb, -s */
1260 DEV_SCLP, /* s390 sclp */
1262 const char *cmdline;
1264 QTAILQ_ENTRY(device_config) next;
1267 static QTAILQ_HEAD(, device_config) device_configs =
1268 QTAILQ_HEAD_INITIALIZER(device_configs);
1270 static void add_device_config(int type, const char *cmdline)
1272 struct device_config *conf;
1274 conf = g_malloc0(sizeof(*conf));
1276 conf->cmdline = cmdline;
1277 loc_save(&conf->loc);
1278 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1281 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1283 struct device_config *conf;
1286 QTAILQ_FOREACH(conf, &device_configs, next) {
1287 if (conf->type != type)
1289 loc_push_restore(&conf->loc);
1290 rc = func(conf->cmdline);
1291 loc_pop(&conf->loc);
1299 static void qemu_disable_default_devices(void)
1301 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1303 qemu_opts_foreach(qemu_find_opts("device"),
1304 default_driver_check, NULL, NULL);
1305 qemu_opts_foreach(qemu_find_opts("global"),
1306 default_driver_check, NULL, NULL);
1308 if (!vga_model && !default_vga) {
1309 vga_interface_type = VGA_DEVICE;
1311 if (!has_defaults || machine_class->no_serial) {
1314 if (!has_defaults || machine_class->no_parallel) {
1315 default_parallel = 0;
1317 if (!has_defaults || machine_class->no_floppy) {
1320 if (!has_defaults || machine_class->no_cdrom) {
1323 if (!has_defaults || machine_class->no_sdcard) {
1326 if (!has_defaults) {
1327 default_monitor = 0;
1333 static void qemu_create_default_devices(void)
1335 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1337 if (is_daemonized()) {
1338 /* According to documentation and historically, -nographic redirects
1339 * serial port, parallel port and monitor to stdio, which does not work
1340 * with -daemonize. We can redirect these to null instead, but since
1341 * -nographic is legacy, let's just error out.
1342 * We disallow -nographic only if all other ports are not redirected
1343 * explicitly, to not break existing legacy setups which uses
1344 * -nographic _and_ redirects all ports explicitly - this is valid
1345 * usage, -nographic is just a no-op in this case.
1348 && (default_parallel || default_serial || default_monitor)) {
1349 error_report("-nographic cannot be used with -daemonize");
1355 if (default_parallel)
1356 add_device_config(DEV_PARALLEL, "null");
1357 if (default_serial && default_monitor) {
1358 add_device_config(DEV_SERIAL, "mon:stdio");
1361 add_device_config(DEV_SERIAL, "stdio");
1362 if (default_monitor)
1363 monitor_parse("stdio", "readline", false);
1367 add_device_config(DEV_SERIAL, "vc:80Cx24C");
1368 if (default_parallel)
1369 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
1370 if (default_monitor)
1371 monitor_parse("vc:80Cx24C", "readline", false);
1375 QemuOptsList *net = qemu_find_opts("net");
1376 qemu_opts_parse(net, "nic", true, &error_abort);
1378 qemu_opts_parse(net, "user", true, &error_abort);
1382 #if defined(CONFIG_VNC)
1383 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
1387 if (dpy.type == DISPLAY_TYPE_DEFAULT && !display_remote) {
1388 if (!qemu_display_find_default(&dpy)) {
1389 dpy.type = DISPLAY_TYPE_NONE;
1390 #if defined(CONFIG_VNC)
1391 vnc_parse("localhost:0,to=99,id=default");
1395 if (dpy.type == DISPLAY_TYPE_DEFAULT) {
1396 dpy.type = DISPLAY_TYPE_NONE;
1399 /* If no default VGA is requested, the default is "none". */
1401 vga_model = get_default_vga_model(machine_class);
1404 select_vgahw(machine_class, vga_model);
1408 static int serial_parse(const char *devname)
1410 int index = num_serial_hds;
1413 if (strcmp(devname, "none") == 0)
1415 snprintf(label, sizeof(label), "serial%d", index);
1416 serial_hds = g_renew(Chardev *, serial_hds, index + 1);
1418 serial_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
1419 if (!serial_hds[index]) {
1420 error_report("could not connect serial device"
1421 " to character backend '%s'", devname);
1428 Chardev *serial_hd(int i)
1431 if (i < num_serial_hds) {
1432 return serial_hds[i];
1437 static int parallel_parse(const char *devname)
1439 static int index = 0;
1442 if (strcmp(devname, "none") == 0)
1444 if (index == MAX_PARALLEL_PORTS) {
1445 error_report("too many parallel ports");
1448 snprintf(label, sizeof(label), "parallel%d", index);
1449 parallel_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
1450 if (!parallel_hds[index]) {
1451 error_report("could not connect parallel device"
1452 " to character backend '%s'", devname);
1459 static int debugcon_parse(const char *devname)
1463 if (!qemu_chr_new_mux_mon("debugcon", devname, NULL)) {
1464 error_report("invalid character backend '%s'", devname);
1467 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
1469 error_report("already have a debugcon device");
1472 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
1473 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
1477 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
1479 const MachineClass *mc1 = a, *mc2 = b;
1482 if (mc1->family == NULL) {
1483 if (mc2->family == NULL) {
1484 /* Compare standalone machine types against each other; they sort
1485 * in increasing order.
1487 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
1488 object_class_get_name(OBJECT_CLASS(mc2)));
1491 /* Standalone machine types sort after families. */
1495 if (mc2->family == NULL) {
1496 /* Families sort before standalone machine types. */
1500 /* Families sort between each other alphabetically increasingly. */
1501 res = strcmp(mc1->family, mc2->family);
1506 /* Within the same family, machine types sort in decreasing order. */
1507 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
1508 object_class_get_name(OBJECT_CLASS(mc1)));
1511 static void machine_help_func(const QDict *qdict)
1513 GSList *machines, *el;
1514 const char *type = qdict_get_try_str(qdict, "type");
1516 machines = object_class_get_list(TYPE_MACHINE, false);
1518 ObjectClass *machine_class = OBJECT_CLASS(find_machine(type, machines));
1519 if (machine_class) {
1520 type_print_class_properties(object_class_get_name(machine_class));
1525 printf("Supported machines are:\n");
1526 machines = g_slist_sort(machines, machine_class_cmp);
1527 for (el = machines; el; el = el->next) {
1528 MachineClass *mc = el->data;
1530 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
1532 printf("%-20s %s%s%s\n", mc->name, mc->desc,
1533 mc->is_default ? " (default)" : "",
1534 mc->deprecation_reason ? " (deprecated)" : "");
1539 machine_merge_property(const char *propname, QDict *prop, Error **errp)
1544 /* Preserve the caller's reference to prop. */
1546 qdict_put(opts, propname, prop);
1547 keyval_merge(machine_opts_dict, opts, errp);
1548 qobject_unref(opts);
1552 machine_parse_property_opt(QemuOptsList *opts_list, const char *propname,
1553 const char *arg, Error **errp)
1558 prop = keyval_parse(arg, opts_list->implied_opt_name, &help, errp);
1560 qemu_opts_print_help(opts_list, true);
1566 machine_merge_property(propname, prop, errp);
1567 qobject_unref(prop);
1570 static const char *pid_file;
1571 static Notifier qemu_unlink_pidfile_notifier;
1573 static void qemu_unlink_pidfile(Notifier *n, void *data)
1580 static const QEMUOption *lookup_opt(int argc, char **argv,
1581 const char **poptarg, int *poptind)
1583 const QEMUOption *popt;
1584 int optind = *poptind;
1585 char *r = argv[optind];
1588 loc_set_cmdline(argv, optind, 1);
1590 /* Treat --foo the same as -foo. */
1593 popt = qemu_options;
1596 error_report("invalid option");
1599 if (!strcmp(popt->name, r + 1))
1603 if (popt->flags & HAS_ARG) {
1604 if (optind >= argc) {
1605 error_report("requires an argument");
1608 optarg = argv[optind++];
1609 loc_set_cmdline(argv, optind - 2, 2);
1620 static MachineClass *select_machine(QDict *qdict, Error **errp)
1622 const char *optarg = qdict_get_try_str(qdict, "type");
1623 GSList *machines = object_class_get_list(TYPE_MACHINE, false);
1624 MachineClass *machine_class;
1625 Error *local_err = NULL;
1628 machine_class = find_machine(optarg, machines);
1629 qdict_del(qdict, "type");
1630 if (!machine_class) {
1631 error_setg(&local_err, "unsupported machine type");
1634 machine_class = find_default_machine(machines);
1635 if (!machine_class) {
1636 error_setg(&local_err, "No machine specified, and there is no default");
1640 g_slist_free(machines);
1642 error_append_hint(&local_err, "Use -machine help to list supported machines\n");
1643 error_propagate(errp, local_err);
1645 return machine_class;
1648 static int object_parse_property_opt(Object *obj,
1649 const char *name, const char *value,
1650 const char *skip, Error **errp)
1652 if (g_str_equal(name, skip)) {
1656 if (!object_property_parse(obj, name, value, errp)) {
1663 /* *Non*recursively replace underscores with dashes in QDict keys. */
1664 static void keyval_dashify(QDict *qdict, Error **errp)
1666 const QDictEntry *ent, *next;
1669 for (ent = qdict_first(qdict); ent; ent = next) {
1670 g_autofree char *new_key = NULL;
1672 next = qdict_next(qdict, ent);
1673 if (!strchr(ent->key, '_')) {
1676 new_key = g_strdup(ent->key);
1677 for (p = new_key; *p; p++) {
1682 if (qdict_haskey(qdict, new_key)) {
1683 error_setg(errp, "Conflict between '%s' and '%s'", ent->key, new_key);
1686 qobject_ref(ent->value);
1687 qdict_put_obj(qdict, new_key, ent->value);
1688 qdict_del(qdict, ent->key);
1692 static void qemu_apply_legacy_machine_options(QDict *qdict)
1696 keyval_dashify(qdict, &error_fatal);
1698 /* Legacy options do not correspond to MachineState properties. */
1699 value = qdict_get_try_str(qdict, "accel");
1701 accelerators = g_strdup(value);
1702 qdict_del(qdict, "accel");
1705 value = qdict_get_try_str(qdict, "igd-passthru");
1707 object_register_sugar_prop(ACCEL_CLASS_NAME("xen"), "igd-passthru", value,
1709 qdict_del(qdict, "igd-passthru");
1712 value = qdict_get_try_str(qdict, "kvm-shadow-mem");
1714 object_register_sugar_prop(ACCEL_CLASS_NAME("kvm"), "kvm-shadow-mem", value,
1716 qdict_del(qdict, "kvm-shadow-mem");
1719 value = qdict_get_try_str(qdict, "kernel-irqchip");
1721 object_register_sugar_prop(ACCEL_CLASS_NAME("kvm"), "kernel-irqchip", value,
1723 object_register_sugar_prop(ACCEL_CLASS_NAME("whpx"), "kernel-irqchip", value,
1725 qdict_del(qdict, "kernel-irqchip");
1729 static void object_option_foreach_add(bool (*type_opt_predicate)(const char *))
1731 ObjectOption *opt, *next;
1733 QTAILQ_FOREACH_SAFE(opt, &object_opts, next, next) {
1734 const char *type = ObjectType_str(opt->opts->qom_type);
1735 if (type_opt_predicate(type)) {
1736 user_creatable_add_qapi(opt->opts, &error_fatal);
1737 qapi_free_ObjectOptions(opt->opts);
1738 QTAILQ_REMOVE(&object_opts, opt, next);
1744 static void object_option_add_visitor(Visitor *v)
1746 ObjectOption *opt = g_new0(ObjectOption, 1);
1747 visit_type_ObjectOptions(v, NULL, &opt->opts, &error_fatal);
1748 QTAILQ_INSERT_TAIL(&object_opts, opt, next);
1751 static void object_option_parse(const char *optarg)
1757 if (optarg[0] == '{') {
1758 QObject *obj = qobject_from_json(optarg, &error_fatal);
1760 v = qobject_input_visitor_new(obj);
1763 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
1769 type = qemu_opt_get(opts, "qom-type");
1771 error_setg(&error_fatal, QERR_MISSING_PARAMETER, "qom-type");
1773 if (user_creatable_print_help(type, opts)) {
1777 v = opts_visitor_new(opts);
1780 object_option_add_visitor(v);
1785 * Initial object creation happens before all other
1786 * QEMU data types are created. The majority of objects
1787 * can be created at this point. The rng-egd object
1788 * cannot be created here, as it depends on the chardev
1791 static bool object_create_early(const char *type)
1794 * Objects should not be made "delayed" without a reason. If you
1795 * add one, state the reason in a comment!
1798 /* Reason: property "chardev" */
1799 if (g_str_equal(type, "rng-egd") ||
1800 g_str_equal(type, "qtest")) {
1804 #if defined(CONFIG_VHOST_USER) && defined(CONFIG_LINUX)
1805 /* Reason: cryptodev-vhost-user property "chardev" */
1806 if (g_str_equal(type, "cryptodev-vhost-user")) {
1811 /* Reason: vhost-user-blk-server property "node-name" */
1812 if (g_str_equal(type, "vhost-user-blk-server")) {
1816 * Reason: filter-* property "netdev" etc.
1818 if (g_str_equal(type, "filter-buffer") ||
1819 g_str_equal(type, "filter-dump") ||
1820 g_str_equal(type, "filter-mirror") ||
1821 g_str_equal(type, "filter-redirector") ||
1822 g_str_equal(type, "colo-compare") ||
1823 g_str_equal(type, "filter-rewriter") ||
1824 g_str_equal(type, "filter-replay")) {
1829 * Allocation of large amounts of memory may delay
1830 * chardev initialization for too long, and trigger timeouts
1831 * on software that waits for a monitor socket to be created
1834 if (g_str_has_prefix(type, "memory-backend-")) {
1841 static void qemu_apply_machine_options(QDict *qdict)
1843 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1844 const char *boot_order = NULL;
1845 const char *boot_once = NULL;
1848 object_set_properties_from_keyval(OBJECT(current_machine), qdict, false, &error_fatal);
1849 current_machine->ram_size = ram_size;
1850 current_machine->maxram_size = maxram_size;
1851 current_machine->ram_slots = ram_slots;
1853 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
1855 boot_order = qemu_opt_get(opts, "order");
1857 validate_bootdevices(boot_order, &error_fatal);
1860 boot_once = qemu_opt_get(opts, "once");
1862 validate_bootdevices(boot_once, &error_fatal);
1865 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
1866 boot_strict = qemu_opt_get_bool(opts, "strict", false);
1870 boot_order = machine_class->default_boot_order;
1873 current_machine->boot_order = boot_order;
1874 current_machine->boot_once = boot_once;
1876 if (semihosting_enabled() && !semihosting_get_argc()) {
1877 /* fall back to the -kernel/-append */
1878 semihosting_arg_fallback(current_machine->kernel_filename, current_machine->kernel_cmdline);
1881 if (current_machine->smp.cpus > 1) {
1882 Error *blocker = NULL;
1883 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
1884 replay_add_blocker(blocker);
1888 static void qemu_create_early_backends(void)
1890 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1891 #if defined(CONFIG_SDL)
1892 const bool use_sdl = (dpy.type == DISPLAY_TYPE_SDL);
1894 const bool use_sdl = false;
1896 #if defined(CONFIG_GTK)
1897 const bool use_gtk = (dpy.type == DISPLAY_TYPE_GTK);
1899 const bool use_gtk = false;
1902 if ((alt_grab || ctrl_grab) && !use_sdl) {
1903 error_report("-alt-grab and -ctrl-grab are only valid "
1904 "for SDL, ignoring option");
1906 if (dpy.has_window_close && !use_gtk && !use_sdl) {
1907 error_report("-no-quit is only valid for GTK and SDL, "
1911 qemu_display_early_init(&dpy);
1912 qemu_console_early_init();
1914 if (dpy.has_gl && dpy.gl != DISPLAYGL_MODE_OFF && display_opengl == 0) {
1915 #if defined(CONFIG_OPENGL)
1916 error_report("OpenGL is not supported by the display");
1918 error_report("OpenGL support is disabled");
1923 object_option_foreach_add(object_create_early);
1925 /* spice needs the timers to be initialized by this point */
1926 /* spice must initialize before audio as it changes the default auiodev */
1927 /* spice must initialize before chardevs (for spicevmc and spiceport) */
1930 qemu_opts_foreach(qemu_find_opts("chardev"),
1931 chardev_init_func, NULL, &error_fatal);
1933 #ifdef CONFIG_VIRTFS
1934 qemu_opts_foreach(qemu_find_opts("fsdev"),
1935 fsdev_init_func, NULL, &error_fatal);
1939 * Note: we need to create audio and block backends before
1940 * setting machine properties, so they can be referred to.
1942 configure_blockdev(&bdo_queue, machine_class, snapshot);
1943 audio_init_audiodevs();
1948 * The remainder of object creation happens after the
1949 * creation of chardev, fsdev, net clients and device data types.
1951 static bool object_create_late(const char *type)
1953 return !object_create_early(type);
1956 static void qemu_create_late_backends(void)
1959 qtest_server_init(qtest_chrdev, qtest_log, &error_fatal);
1962 net_init_clients(&error_fatal);
1964 object_option_foreach_add(object_create_late);
1966 if (tpm_init() < 0) {
1970 qemu_opts_foreach(qemu_find_opts("mon"),
1971 mon_init_func, NULL, &error_fatal);
1973 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
1975 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
1977 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
1980 /* now chardevs have been created we may have semihosting to connect */
1981 qemu_semihosting_connect_chardevs();
1982 qemu_semihosting_console_init();
1985 static bool have_custom_ram_size(void)
1987 QemuOpts *opts = qemu_find_opts_singleton("memory");
1988 return !!qemu_opt_get_size(opts, "size", 0);
1991 static void qemu_resolve_machine_memdev(void)
1993 if (current_machine->ram_memdev_id) {
1995 ram_addr_t backend_size;
1997 backend = object_resolve_path_type(current_machine->ram_memdev_id,
1998 TYPE_MEMORY_BACKEND, NULL);
2000 error_report("Memory backend '%s' not found",
2001 current_machine->ram_memdev_id);
2004 backend_size = object_property_get_uint(backend, "size", &error_abort);
2005 if (have_custom_ram_size() && backend_size != ram_size) {
2006 error_report("Size specified by -m option must match size of "
2007 "explicitly specified 'memory-backend' property");
2011 error_report("'-mem-path' can't be used together with"
2012 "'-machine memory-backend'");
2015 ram_size = backend_size;
2018 if (!xen_enabled()) {
2019 /* On 32-bit hosts, QEMU is limited by virtual address space */
2020 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
2021 error_report("at most 2047 MB RAM can be simulated");
2027 static void set_memory_options(MachineClass *mc)
2030 const char *mem_str;
2031 const ram_addr_t default_ram_size = mc->default_ram_size;
2032 QemuOpts *opts = qemu_find_opts_singleton("memory");
2035 loc_push_none(&loc);
2036 qemu_opts_loc_restore(opts);
2039 mem_str = qemu_opt_get(opts, "size");
2042 error_report("missing 'size' option value");
2046 sz = qemu_opt_get_size(opts, "size", ram_size);
2048 /* Fix up legacy suffix-less format */
2049 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2050 uint64_t overflow_check = sz;
2053 if (sz / MiB != overflow_check) {
2054 error_report("too large 'size' option value");
2060 /* backward compatibility behaviour for case "-m 0" */
2062 sz = default_ram_size;
2065 sz = QEMU_ALIGN_UP(sz, 8192);
2066 if (mc->fixup_ram_size) {
2067 sz = mc->fixup_ram_size(sz);
2070 if (ram_size != sz) {
2071 error_report("ram size too large");
2075 maxram_size = ram_size;
2077 if (qemu_opt_get(opts, "maxmem")) {
2080 sz = qemu_opt_get_size(opts, "maxmem", 0);
2081 slots = qemu_opt_get_number(opts, "slots", 0);
2082 if (sz < ram_size) {
2083 error_report("invalid value of -m option maxmem: "
2084 "maximum memory size (0x%" PRIx64 ") must be at least "
2085 "the initial memory size (0x" RAM_ADDR_FMT ")",
2088 } else if (slots && sz == ram_size) {
2089 error_report("invalid value of -m option maxmem: "
2090 "memory slots were specified but maximum memory size "
2091 "(0x%" PRIx64 ") is equal to the initial memory size "
2092 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
2098 } else if (qemu_opt_get(opts, "slots")) {
2099 error_report("invalid -m option value: missing 'maxmem' option");
2106 static void qemu_create_machine(QDict *qdict)
2108 MachineClass *machine_class = select_machine(qdict, &error_fatal);
2109 object_set_machine_compat_props(machine_class->compat_props);
2111 set_memory_options(machine_class);
2113 current_machine = MACHINE(object_new_with_class(OBJECT_CLASS(machine_class)));
2114 object_property_add_child(object_get_root(), "machine",
2115 OBJECT(current_machine));
2116 object_property_add_child(container_get(OBJECT(current_machine),
2118 "sysbus", OBJECT(sysbus_get_default()));
2120 if (machine_class->minimum_page_bits) {
2121 if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
2122 /* This would be a board error: specifying a minimum smaller than
2123 * a target's compile-time fixed setting.
2125 g_assert_not_reached();
2129 cpu_exec_init_all();
2132 if (machine_class->hw_version) {
2133 qemu_set_hw_version(machine_class->hw_version);
2137 * Get the default machine options from the machine if it is not already
2138 * specified either by the configuration file or by the command line.
2140 if (machine_class->default_machine_opts) {
2141 QDict *default_opts =
2142 keyval_parse(machine_class->default_machine_opts, NULL, NULL,
2144 qemu_apply_legacy_machine_options(default_opts);
2145 object_set_properties_from_keyval(OBJECT(current_machine), default_opts,
2146 false, &error_abort);
2147 qobject_unref(default_opts);
2151 static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
2155 g = g_malloc0(sizeof(*g));
2156 g->driver = qemu_opt_get(opts, "driver");
2157 g->property = qemu_opt_get(opts, "property");
2158 g->value = qemu_opt_get(opts, "value");
2159 qdev_prop_register_global(g);
2164 * Return whether configuration group @group is stored in QemuOpts, or
2165 * recorded as one or more QDicts by qemu_record_config_group.
2167 static bool is_qemuopts_group(const char *group)
2169 if (g_str_equal(group, "object") ||
2170 g_str_equal(group, "machine") ||
2171 g_str_equal(group, "smp-opts")) {
2177 static void qemu_record_config_group(const char *group, QDict *dict,
2178 bool from_json, Error **errp)
2180 if (g_str_equal(group, "object")) {
2181 Visitor *v = qobject_input_visitor_new_keyval(QOBJECT(dict));
2182 object_option_add_visitor(v);
2184 } else if (g_str_equal(group, "machine")) {
2186 * Cannot merge string-valued and type-safe dictionaries, so JSON
2187 * is not accepted yet for -M.
2190 keyval_merge(machine_opts_dict, dict, errp);
2191 } else if (g_str_equal(group, "smp-opts")) {
2192 machine_merge_property("smp", dict, &error_fatal);
2199 * Parse non-QemuOpts config file groups, pass the rest to
2200 * qemu_config_do_parse.
2202 static void qemu_parse_config_group(const char *group, QDict *qdict,
2203 void *opaque, Error **errp)
2206 if (is_qemuopts_group(group)) {
2207 qemu_config_do_parse(group, qdict, opaque, errp);
2211 crumpled = qdict_crumple(qdict, errp);
2215 switch (qobject_type(crumpled)) {
2217 qemu_record_config_group(group, qobject_to(QDict, crumpled), false, errp);
2220 error_setg(errp, "Lists cannot be at top level of a configuration section");
2223 g_assert_not_reached();
2225 qobject_unref(crumpled);
2228 static void qemu_read_default_config_file(Error **errp)
2232 g_autofree char *file = get_relocated_path(CONFIG_QEMU_CONFDIR "/qemu.conf");
2234 ret = qemu_read_config_file(file, qemu_parse_config_group, errp);
2236 if (ret == -ENOENT) {
2243 static void qemu_set_option(const char *str, Error **errp)
2245 char group[64], id[64], arg[64];
2250 rc = sscanf(str, "%63[^.].%63[^.].%63[^=]%n", group, id, arg, &offset);
2251 if (rc < 3 || str[offset] != '=') {
2252 error_setg(errp, "can't parse: \"%s\"", str);
2256 if (!is_qemuopts_group(group)) {
2257 error_setg(errp, "-set is not supported with %s", group);
2259 list = qemu_find_opts_err(group, errp);
2261 opts = qemu_opts_find(list, id);
2263 error_setg(errp, "there is no %s \"%s\" defined", group, id);
2266 qemu_opt_set(opts, arg, str + offset + 1, errp);
2271 static void user_register_global_props(void)
2273 qemu_opts_foreach(qemu_find_opts("global"),
2274 global_init_func, NULL, NULL);
2277 static int do_configure_icount(void *opaque, QemuOpts *opts, Error **errp)
2279 icount_configure(opts, errp);
2283 static int accelerator_set_property(void *opaque,
2284 const char *name, const char *value,
2287 return object_parse_property_opt(opaque, name, value, "accel", errp);
2290 static int do_configure_accelerator(void *opaque, QemuOpts *opts, Error **errp)
2292 bool *p_init_failed = opaque;
2293 const char *acc = qemu_opt_get(opts, "accel");
2294 AccelClass *ac = accel_find(acc);
2297 bool qtest_with_kvm;
2299 qtest_with_kvm = g_str_equal(acc, "kvm") && qtest_chrdev != NULL;
2302 *p_init_failed = true;
2303 if (!qtest_with_kvm) {
2304 error_report("invalid accelerator %s", acc);
2308 accel = ACCEL(object_new_with_class(OBJECT_CLASS(ac)));
2309 object_apply_compat_props(OBJECT(accel));
2310 qemu_opt_foreach(opts, accelerator_set_property,
2314 ret = accel_init_machine(accel, current_machine);
2316 *p_init_failed = true;
2317 if (!qtest_with_kvm || ret != -ENOENT) {
2318 error_report("failed to initialize %s: %s", acc, strerror(-ret));
2326 static void configure_accelerators(const char *progname)
2328 bool init_failed = false;
2330 qemu_opts_foreach(qemu_find_opts("icount"),
2331 do_configure_icount, NULL, &error_fatal);
2333 if (QTAILQ_EMPTY(&qemu_accel_opts.head)) {
2334 char **accel_list, **tmp;
2336 if (accelerators == NULL) {
2337 /* Select the default accelerator */
2338 bool have_tcg = accel_find("tcg");
2339 bool have_kvm = accel_find("kvm");
2341 if (have_tcg && have_kvm) {
2342 if (g_str_has_suffix(progname, "kvm")) {
2343 /* If the program name ends with "kvm", we prefer KVM */
2344 accelerators = "kvm:tcg";
2346 accelerators = "tcg:kvm";
2348 } else if (have_kvm) {
2349 accelerators = "kvm";
2350 } else if (have_tcg) {
2351 accelerators = "tcg";
2353 error_report("No accelerator selected and"
2354 " no default accelerator available");
2358 accel_list = g_strsplit(accelerators, ":", 0);
2360 for (tmp = accel_list; *tmp; tmp++) {
2362 * Filter invalid accelerators here, to prevent obscenities
2363 * such as "-machine accel=tcg,,thread=single".
2365 if (accel_find(*tmp)) {
2366 qemu_opts_parse_noisily(qemu_find_opts("accel"), *tmp, true);
2369 error_report("invalid accelerator %s", *tmp);
2372 g_strfreev(accel_list);
2374 if (accelerators != NULL) {
2375 error_report("The -accel and \"-machine accel=\" options are incompatible");
2380 if (!qemu_opts_foreach(qemu_find_opts("accel"),
2381 do_configure_accelerator, &init_failed, &error_fatal)) {
2383 error_report("no accelerator found");
2388 if (init_failed && !qtest_chrdev) {
2389 AccelClass *ac = ACCEL_GET_CLASS(current_accel());
2390 error_report("falling back to %s", ac->name);
2393 if (icount_enabled() && !tcg_enabled()) {
2394 error_report("-icount is not allowed with hardware virtualization");
2399 static void create_default_memdev(MachineState *ms, const char *path)
2402 MachineClass *mc = MACHINE_GET_CLASS(ms);
2404 obj = object_new(path ? TYPE_MEMORY_BACKEND_FILE : TYPE_MEMORY_BACKEND_RAM);
2406 object_property_set_str(obj, "mem-path", path, &error_fatal);
2408 object_property_set_int(obj, "size", ms->ram_size, &error_fatal);
2409 object_property_add_child(object_get_objects_root(), mc->default_ram_id,
2411 /* Ensure backend's memory region name is equal to mc->default_ram_id */
2412 object_property_set_bool(obj, "x-use-canonical-path-for-ramblock-id",
2413 false, &error_fatal);
2414 user_creatable_complete(USER_CREATABLE(obj), &error_fatal);
2416 object_property_set_str(OBJECT(ms), "memory-backend", mc->default_ram_id,
2420 static void qemu_validate_options(const QDict *machine_opts)
2422 const char *kernel_filename = qdict_get_try_str(machine_opts, "kernel");
2423 const char *initrd_filename = qdict_get_try_str(machine_opts, "initrd");
2424 const char *kernel_cmdline = qdict_get_try_str(machine_opts, "append");
2426 if (kernel_filename == NULL) {
2427 if (kernel_cmdline != NULL) {
2428 error_report("-append only allowed with -kernel option");
2432 if (initrd_filename != NULL) {
2433 error_report("-initrd only allowed with -kernel option");
2438 if (loadvm && preconfig_requested) {
2439 error_report("'preconfig' and 'loadvm' options are "
2440 "mutually exclusive");
2443 if (incoming && preconfig_requested && strcmp(incoming, "defer") != 0) {
2444 error_report("'preconfig' supports '-incoming defer' only");
2448 #ifdef CONFIG_CURSES
2449 if (is_daemonized() && dpy.type == DISPLAY_TYPE_CURSES) {
2450 error_report("curses display cannot be used with -daemonize");
2456 static void qemu_process_sugar_options(void)
2459 QObject *smp = qdict_get(machine_opts_dict, "smp");
2460 if (smp && qobject_type(smp) == QTYPE_QDICT) {
2461 QObject *cpus = qdict_get(qobject_to(QDict, smp), "cpus");
2462 if (cpus && qobject_type(cpus) == QTYPE_QSTRING) {
2463 const char *val = qstring_get_str(qobject_to(QString, cpus));
2464 object_register_sugar_prop("memory-backend", "prealloc-threads",
2468 object_register_sugar_prop("memory-backend", "prealloc", "on", false);
2472 int i = select_watchdog(watchdog);
2474 exit (i == 1 ? 1 : 0);
2478 /* -action processing */
2481 * Process all the -action parameters parsed from cmdline.
2483 static int process_runstate_actions(void *opaque, QemuOpts *opts, Error **errp)
2485 Error *local_err = NULL;
2486 QDict *qdict = qemu_opts_to_qdict(opts, NULL);
2487 QObject *ret = NULL;
2488 qmp_marshal_set_action(qdict, &ret, &local_err);
2490 qobject_unref(qdict);
2492 error_propagate(errp, local_err);
2498 static void qemu_process_early_options(void)
2500 #ifdef CONFIG_SECCOMP
2501 QemuOptsList *olist = qemu_find_opts_err("sandbox", NULL);
2503 qemu_opts_foreach(olist, parse_sandbox, NULL, &error_fatal);
2507 qemu_opts_foreach(qemu_find_opts("name"),
2508 parse_name, NULL, &error_fatal);
2510 if (qemu_opts_foreach(qemu_find_opts("action"),
2511 process_runstate_actions, NULL, &error_fatal)) {
2516 qemu_opts_foreach(qemu_find_opts("add-fd"),
2517 parse_add_fd, NULL, &error_fatal);
2519 qemu_opts_foreach(qemu_find_opts("add-fd"),
2520 cleanup_add_fd, NULL, &error_fatal);
2523 /* Open the logfile at this point and set the log mask if necessary. */
2525 qemu_set_log_filename(log_file, &error_fatal);
2529 mask = qemu_str_to_log_mask(log_mask);
2531 qemu_print_log_usage(stdout);
2539 qemu_add_default_firmwarepath();
2542 static void qemu_process_help_options(void)
2545 * Check for -cpu help and -device help before we call select_machine(),
2546 * which will return an error if the architecture has no default machine
2547 * type and the user did not specify one, so that the user doesn't need
2548 * to say '-cpu help -machine something'.
2550 if (cpu_option && is_help_option(cpu_option)) {
2551 list_cpus(cpu_option);
2555 if (qemu_opts_foreach(qemu_find_opts("device"),
2556 device_help_func, NULL, NULL)) {
2560 /* -L help lists the data directories and exits. */
2561 if (list_data_dirs) {
2562 qemu_list_data_dirs();
2567 static void qemu_maybe_daemonize(const char *pid_file)
2572 rcu_disable_atfork();
2574 if (pid_file && !qemu_write_pidfile(pid_file, &err)) {
2575 error_reportf_err(err, "cannot create PID file: ");
2579 qemu_unlink_pidfile_notifier.notify = qemu_unlink_pidfile;
2580 qemu_add_exit_notifier(&qemu_unlink_pidfile_notifier);
2583 static void qemu_init_displays(void)
2587 /* init local displays */
2588 ds = init_displaystate();
2589 qemu_display_init(ds, &dpy);
2591 /* must be after terminal init, SDL library changes signal handlers */
2592 os_setup_signal_handling();
2594 /* init remote displays */
2596 qemu_opts_foreach(qemu_find_opts("vnc"),
2597 vnc_init_func, NULL, &error_fatal);
2601 qemu_spice.display_init();
2605 static void qemu_init_board(void)
2607 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
2609 if (machine_class->default_ram_id && current_machine->ram_size &&
2610 numa_uses_legacy_mem() && !current_machine->ram_memdev_id) {
2611 create_default_memdev(current_machine, mem_path);
2614 /* process plugin before CPUs are created, but once -smp has been parsed */
2615 qemu_plugin_load_list(&plugin_list, &error_fatal);
2617 /* From here on we enter MACHINE_PHASE_INITIALIZED. */
2618 machine_run_board_init(current_machine);
2620 drive_check_orphaned();
2624 if (hax_enabled()) {
2625 /* FIXME: why isn't cpu_synchronize_all_post_init enough? */
2630 static void qemu_create_cli_devices(void)
2634 qemu_opts_foreach(qemu_find_opts("fw_cfg"),
2635 parse_fw_cfg, fw_cfg_find(), &error_fatal);
2637 /* init USB devices */
2638 if (machine_usb(current_machine)) {
2639 if (foreach_device_config(DEV_USB, usb_parse) < 0)
2643 /* init generic devices */
2644 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
2645 qemu_opts_foreach(qemu_find_opts("device"),
2646 device_init_func, NULL, &error_fatal);
2647 rom_reset_order_override();
2650 static void qemu_machine_creation_done(void)
2652 MachineState *machine = MACHINE(qdev_get_machine());
2654 /* Did we create any drives that we failed to create a device for? */
2655 drive_check_orphaned();
2657 /* Don't warn about the default network setup that you get if
2658 * no command line -net or -netdev options are specified. There
2659 * are two cases that we would otherwise complain about:
2660 * (1) board doesn't support a NIC but the implicit "-net nic"
2662 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
2663 * sets up a nic that isn't connected to anything.
2665 if (!default_net && (!qtest_enabled() || has_defaults)) {
2666 net_check_clients();
2669 qdev_prop_check_globals();
2671 qdev_machine_creation_done();
2675 * Verify that Confidential Guest Support has actually been initialized
2677 assert(machine->cgs->ready);
2680 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
2685 void qmp_x_exit_preconfig(Error **errp)
2687 if (phase_check(PHASE_MACHINE_INITIALIZED)) {
2688 error_setg(errp, "The command is permitted only before machine initialization");
2693 qemu_create_cli_devices();
2694 qemu_machine_creation_done();
2697 Error *local_err = NULL;
2698 if (!load_snapshot(loadvm, NULL, false, NULL, &local_err)) {
2699 error_report_err(local_err);
2704 if (replay_mode != REPLAY_MODE_NONE) {
2705 replay_vmstate_init();
2709 Error *local_err = NULL;
2710 if (strcmp(incoming, "defer") != 0) {
2711 qmp_migrate_incoming(incoming, &local_err);
2713 error_reportf_err(local_err, "-incoming %s: ", incoming);
2717 } else if (autostart) {
2722 void qemu_init(int argc, char **argv, char **envp)
2725 QemuOpts *icount_opts = NULL, *accel_opts = NULL;
2726 QemuOptsList *olist;
2729 MachineClass *machine_class;
2730 bool userconfig = true;
2731 FILE *vmstate_dump_file = NULL;
2733 qemu_add_opts(&qemu_drive_opts);
2734 qemu_add_drive_opts(&qemu_legacy_drive_opts);
2735 qemu_add_drive_opts(&qemu_common_drive_opts);
2736 qemu_add_drive_opts(&qemu_drive_opts);
2737 qemu_add_drive_opts(&bdrv_runtime_opts);
2738 qemu_add_opts(&qemu_chardev_opts);
2739 qemu_add_opts(&qemu_device_opts);
2740 qemu_add_opts(&qemu_netdev_opts);
2741 qemu_add_opts(&qemu_nic_opts);
2742 qemu_add_opts(&qemu_net_opts);
2743 qemu_add_opts(&qemu_rtc_opts);
2744 qemu_add_opts(&qemu_global_opts);
2745 qemu_add_opts(&qemu_mon_opts);
2746 qemu_add_opts(&qemu_trace_opts);
2747 qemu_plugin_add_opts();
2748 qemu_add_opts(&qemu_option_rom_opts);
2749 qemu_add_opts(&qemu_accel_opts);
2750 qemu_add_opts(&qemu_mem_opts);
2751 qemu_add_opts(&qemu_smp_opts);
2752 qemu_add_opts(&qemu_boot_opts);
2753 qemu_add_opts(&qemu_add_fd_opts);
2754 qemu_add_opts(&qemu_object_opts);
2755 qemu_add_opts(&qemu_tpmdev_opts);
2756 qemu_add_opts(&qemu_overcommit_opts);
2757 qemu_add_opts(&qemu_msg_opts);
2758 qemu_add_opts(&qemu_name_opts);
2759 qemu_add_opts(&qemu_numa_opts);
2760 qemu_add_opts(&qemu_icount_opts);
2761 qemu_add_opts(&qemu_semihosting_config_opts);
2762 qemu_add_opts(&qemu_fw_cfg_opts);
2763 qemu_add_opts(&qemu_action_opts);
2764 module_call_init(MODULE_INIT_OPTS);
2766 error_init(argv[0]);
2767 qemu_init_exec_dir(argv[0]);
2769 #ifdef CONFIG_MODULES
2770 module_init_info(qemu_modinfo);
2771 module_allow_arch(TARGET_NAME);
2774 qemu_init_subsystems();
2776 /* first pass of option parsing */
2778 while (optind < argc) {
2779 if (argv[optind][0] != '-') {
2783 const QEMUOption *popt;
2785 popt = lookup_opt(argc, argv, &optarg, &optind);
2786 switch (popt->index) {
2787 case QEMU_OPTION_nouserconfig:
2794 machine_opts_dict = qdict_new();
2796 qemu_read_default_config_file(&error_fatal);
2799 /* second pass of option parsing */
2804 if (argv[optind][0] != '-') {
2805 loc_set_cmdline(argv, optind, 1);
2806 drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2808 const QEMUOption *popt;
2810 popt = lookup_opt(argc, argv, &optarg, &optind);
2811 if (!(popt->arch_mask & arch_type)) {
2812 error_report("Option not supported for this target");
2815 switch(popt->index) {
2816 case QEMU_OPTION_cpu:
2817 /* hw initialization will check this */
2818 cpu_option = optarg;
2820 case QEMU_OPTION_hda:
2821 case QEMU_OPTION_hdb:
2822 case QEMU_OPTION_hdc:
2823 case QEMU_OPTION_hdd:
2824 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2827 case QEMU_OPTION_blockdev:
2830 BlockdevOptionsQueueEntry *bdo;
2832 v = qobject_input_visitor_new_str(optarg, "driver",
2835 bdo = g_new(BlockdevOptionsQueueEntry, 1);
2836 visit_type_BlockdevOptions(v, NULL, &bdo->bdo,
2839 loc_save(&bdo->loc);
2840 QSIMPLEQ_INSERT_TAIL(&bdo_queue, bdo, entry);
2843 case QEMU_OPTION_drive:
2844 if (drive_def(optarg) == NULL) {
2848 case QEMU_OPTION_set:
2849 qemu_set_option(optarg, &error_fatal);
2851 case QEMU_OPTION_global:
2852 if (qemu_global_option(optarg) != 0)
2855 case QEMU_OPTION_mtdblock:
2856 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2858 case QEMU_OPTION_sd:
2859 drive_add(IF_SD, -1, optarg, SD_OPTS);
2861 case QEMU_OPTION_pflash:
2862 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2864 case QEMU_OPTION_snapshot:
2866 Error *blocker = NULL;
2868 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
2870 replay_add_blocker(blocker);
2873 case QEMU_OPTION_numa:
2874 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
2880 case QEMU_OPTION_display:
2881 parse_display(optarg);
2883 case QEMU_OPTION_nographic:
2884 qdict_put_str(machine_opts_dict, "graphics", "off");
2886 dpy.type = DISPLAY_TYPE_NONE;
2888 case QEMU_OPTION_curses:
2889 #ifdef CONFIG_CURSES
2890 dpy.type = DISPLAY_TYPE_CURSES;
2892 error_report("curses or iconv support is disabled");
2896 case QEMU_OPTION_portrait:
2897 graphic_rotate = 90;
2899 case QEMU_OPTION_rotate:
2900 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2901 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2902 graphic_rotate != 180 && graphic_rotate != 270) {
2903 error_report("only 90, 180, 270 deg rotation is available");
2907 case QEMU_OPTION_kernel:
2908 qdict_put_str(machine_opts_dict, "kernel", optarg);
2910 case QEMU_OPTION_initrd:
2911 qdict_put_str(machine_opts_dict, "initrd", optarg);
2913 case QEMU_OPTION_append:
2914 qdict_put_str(machine_opts_dict, "append", optarg);
2916 case QEMU_OPTION_dtb:
2917 qdict_put_str(machine_opts_dict, "dtb", optarg);
2919 case QEMU_OPTION_cdrom:
2920 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
2922 case QEMU_OPTION_boot:
2923 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
2929 case QEMU_OPTION_fda:
2930 case QEMU_OPTION_fdb:
2931 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2934 case QEMU_OPTION_no_fd_bootchk:
2937 case QEMU_OPTION_netdev:
2939 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2943 case QEMU_OPTION_nic:
2945 if (net_client_parse(qemu_find_opts("nic"), optarg) == -1) {
2949 case QEMU_OPTION_net:
2951 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2955 #ifdef CONFIG_LIBISCSI
2956 case QEMU_OPTION_iscsi:
2957 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
2964 case QEMU_OPTION_audio_help:
2965 audio_legacy_help();
2968 case QEMU_OPTION_audiodev:
2969 audio_parse_option(optarg);
2971 case QEMU_OPTION_soundhw:
2972 select_soundhw (optarg);
2977 case QEMU_OPTION_version:
2982 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
2989 case QEMU_OPTION_tpmdev:
2990 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
2995 case QEMU_OPTION_mempath:
2998 case QEMU_OPTION_mem_prealloc:
3007 case QEMU_OPTION_DFILTER:
3008 qemu_set_dfilter_ranges(optarg, &error_fatal);
3010 case QEMU_OPTION_seed:
3011 qemu_guest_random_seed_main(optarg, &error_fatal);
3014 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3016 case QEMU_OPTION_gdb:
3017 add_device_config(DEV_GDB, optarg);
3020 if (is_help_option(optarg)) {
3021 list_data_dirs = true;
3023 qemu_add_data_dir(g_strdup(optarg));
3026 case QEMU_OPTION_bios:
3027 qdict_put_str(machine_opts_dict, "firmware", optarg);
3029 case QEMU_OPTION_singlestep:
3036 keyboard_layout = optarg;
3038 case QEMU_OPTION_vga:
3047 w = strtol(p, (char **)&p, 10);
3050 error_report("invalid resolution or depth");
3056 h = strtol(p, (char **)&p, 10);
3061 depth = strtol(p, (char **)&p, 10);
3062 if (depth != 1 && depth != 2 && depth != 4 &&
3063 depth != 8 && depth != 15 && depth != 16 &&
3064 depth != 24 && depth != 32)
3066 } else if (*p == '\0') {
3067 depth = graphic_depth;
3074 graphic_depth = depth;
3077 case QEMU_OPTION_echr:
3080 term_escape_char = strtol(optarg, &r, 0);
3082 printf("Bad argument to echr\n");
3085 case QEMU_OPTION_monitor:
3086 default_monitor = 0;
3087 if (strncmp(optarg, "none", 4)) {
3088 monitor_parse(optarg, "readline", false);
3091 case QEMU_OPTION_qmp:
3092 monitor_parse(optarg, "control", false);
3093 default_monitor = 0;
3095 case QEMU_OPTION_qmp_pretty:
3096 monitor_parse(optarg, "control", true);
3097 default_monitor = 0;
3099 case QEMU_OPTION_mon:
3100 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3105 default_monitor = 0;
3107 case QEMU_OPTION_chardev:
3108 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3114 case QEMU_OPTION_fsdev:
3115 olist = qemu_find_opts("fsdev");
3117 error_report("fsdev support is disabled");
3120 opts = qemu_opts_parse_noisily(olist, optarg, true);
3125 case QEMU_OPTION_virtfs: {
3128 const char *writeout, *sock_fd, *socket, *path, *security_model,
3131 olist = qemu_find_opts("virtfs");
3133 error_report("virtfs support is disabled");
3136 opts = qemu_opts_parse_noisily(olist, optarg, true);
3141 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3142 qemu_opt_get(opts, "mount_tag") == NULL) {
3143 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3146 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3147 qemu_opts_id(opts) ?:
3148 qemu_opt_get(opts, "mount_tag"),
3151 error_report("duplicate or invalid fsdev id: %s",
3152 qemu_opt_get(opts, "mount_tag"));
3156 writeout = qemu_opt_get(opts, "writeout");
3158 #ifdef CONFIG_SYNC_FILE_RANGE
3159 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3161 error_report("writeout=immediate not supported "
3162 "on this platform");
3166 qemu_opt_set(fsdev, "fsdriver",
3167 qemu_opt_get(opts, "fsdriver"), &error_abort);
3168 path = qemu_opt_get(opts, "path");
3170 qemu_opt_set(fsdev, "path", path, &error_abort);
3172 security_model = qemu_opt_get(opts, "security_model");
3173 if (security_model) {
3174 qemu_opt_set(fsdev, "security_model", security_model,
3177 socket = qemu_opt_get(opts, "socket");
3179 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3181 sock_fd = qemu_opt_get(opts, "sock_fd");
3183 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3186 qemu_opt_set_bool(fsdev, "readonly",
3187 qemu_opt_get_bool(opts, "readonly", 0),
3189 multidevs = qemu_opt_get(opts, "multidevs");
3191 qemu_opt_set(fsdev, "multidevs", multidevs, &error_abort);
3193 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3195 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3196 qemu_opt_set(device, "fsdev",
3197 qemu_opts_id(fsdev), &error_abort);
3198 qemu_opt_set(device, "mount_tag",
3199 qemu_opt_get(opts, "mount_tag"), &error_abort);
3202 case QEMU_OPTION_serial:
3203 add_device_config(DEV_SERIAL, optarg);
3205 if (strncmp(optarg, "mon:", 4) == 0) {
3206 default_monitor = 0;
3209 case QEMU_OPTION_watchdog:
3211 error_report("only one watchdog option may be given");
3216 case QEMU_OPTION_action:
3217 olist = qemu_find_opts("action");
3218 if (!qemu_opts_parse_noisily(olist, optarg, false)) {
3222 case QEMU_OPTION_watchdog_action:
3223 if (select_watchdog_action(optarg) == -1) {
3224 error_report("unknown -watchdog-action parameter");
3228 case QEMU_OPTION_parallel:
3229 add_device_config(DEV_PARALLEL, optarg);
3230 default_parallel = 0;
3231 if (strncmp(optarg, "mon:", 4) == 0) {
3232 default_monitor = 0;
3235 case QEMU_OPTION_debugcon:
3236 add_device_config(DEV_DEBUGCON, optarg);
3238 case QEMU_OPTION_loadvm:
3241 case QEMU_OPTION_full_screen:
3242 dpy.has_full_screen = true;
3243 dpy.full_screen = true;
3245 case QEMU_OPTION_alt_grab:
3248 case QEMU_OPTION_ctrl_grab:
3251 case QEMU_OPTION_no_quit:
3252 dpy.has_window_close = true;
3253 dpy.window_close = false;
3254 warn_report("-no-quit is deprecated, please use "
3255 "-display ...,window-close=off instead.");
3257 case QEMU_OPTION_sdl:
3259 dpy.type = DISPLAY_TYPE_SDL;
3262 error_report("SDL support is disabled");
3265 case QEMU_OPTION_pidfile:
3268 case QEMU_OPTION_win2k_hack:
3269 win2k_install_hack = 1;
3271 case QEMU_OPTION_acpitable:
3272 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3277 acpi_table_add(opts, &error_fatal);
3279 case QEMU_OPTION_smbios:
3280 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3285 smbios_entry_add(opts, &error_fatal);
3287 case QEMU_OPTION_fwcfg:
3288 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3294 case QEMU_OPTION_preconfig:
3295 preconfig_requested = true;
3297 case QEMU_OPTION_enable_kvm:
3298 qdict_put_str(machine_opts_dict, "accel", "kvm");
3301 case QEMU_OPTION_machine:
3305 keyval_parse_into(machine_opts_dict, optarg, "type", &help, &error_fatal);
3307 machine_help_func(machine_opts_dict);
3312 case QEMU_OPTION_accel:
3313 accel_opts = qemu_opts_parse_noisily(qemu_find_opts("accel"),
3315 optarg = qemu_opt_get(accel_opts, "accel");
3316 if (!optarg || is_help_option(optarg)) {
3317 printf("Accelerators supported in QEMU binary:\n");
3318 GSList *el, *accel_list = object_class_get_list(TYPE_ACCEL,
3320 for (el = accel_list; el; el = el->next) {
3321 gchar *typename = g_strdup(object_class_get_name(
3322 OBJECT_CLASS(el->data)));
3323 /* omit qtest which is used for tests only */
3324 if (g_strcmp0(typename, ACCEL_CLASS_NAME("qtest")) &&
3325 g_str_has_suffix(typename, ACCEL_CLASS_SUFFIX)) {
3326 gchar **optname = g_strsplit(typename,
3327 ACCEL_CLASS_SUFFIX, 0);
3328 printf("%s\n", optname[0]);
3329 g_strfreev(optname);
3333 g_slist_free(accel_list);
3337 case QEMU_OPTION_usb:
3338 qdict_put_str(machine_opts_dict, "usb", "on");
3340 case QEMU_OPTION_usbdevice:
3341 qdict_put_str(machine_opts_dict, "usb", "on");
3342 add_device_config(DEV_USB, optarg);
3344 case QEMU_OPTION_device:
3345 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3350 case QEMU_OPTION_smp:
3351 machine_parse_property_opt(qemu_find_opts("smp-opts"), "smp", optarg, &error_fatal);
3353 case QEMU_OPTION_vnc:
3356 case QEMU_OPTION_no_acpi:
3357 qdict_put_str(machine_opts_dict, "acpi", "off");
3359 case QEMU_OPTION_no_hpet:
3360 qdict_put_str(machine_opts_dict, "hpet", "off");
3362 case QEMU_OPTION_no_reboot:
3363 olist = qemu_find_opts("action");
3364 qemu_opts_parse_noisily(olist, "reboot=shutdown", false);
3366 case QEMU_OPTION_no_shutdown:
3367 olist = qemu_find_opts("action");
3368 qemu_opts_parse_noisily(olist, "shutdown=pause", false);
3370 case QEMU_OPTION_uuid:
3371 if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
3372 error_report("failed to parse UUID string: wrong format");
3375 qemu_uuid_set = true;
3377 case QEMU_OPTION_option_rom:
3378 if (nb_option_roms >= MAX_OPTION_ROMS) {
3379 error_report("too many option ROMs");
3382 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3387 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3388 option_rom[nb_option_roms].bootindex =
3389 qemu_opt_get_number(opts, "bootindex", -1);
3390 if (!option_rom[nb_option_roms].name) {
3391 error_report("Option ROM file is not specified");
3396 case QEMU_OPTION_semihosting:
3397 qemu_semihosting_enable();
3399 case QEMU_OPTION_semihosting_config:
3400 if (qemu_semihosting_config_options(optarg) != 0) {
3404 case QEMU_OPTION_name:
3405 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3410 /* Capture guest name if -msg guest-name is used later */
3411 error_guest_name = qemu_opt_get(opts, "guest");
3413 case QEMU_OPTION_prom_env:
3414 if (nb_prom_envs >= MAX_PROM_ENVS) {
3415 error_report("too many prom variables");
3418 prom_envs[nb_prom_envs] = optarg;
3421 case QEMU_OPTION_old_param:
3424 case QEMU_OPTION_rtc:
3425 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3431 case QEMU_OPTION_icount:
3432 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3438 case QEMU_OPTION_incoming:
3440 runstate_set(RUN_STATE_INMIGRATE);
3444 case QEMU_OPTION_only_migratable:
3445 only_migratable = 1;
3447 case QEMU_OPTION_nodefaults:
3450 case QEMU_OPTION_xen_domid:
3451 if (!(accel_find("xen"))) {
3452 error_report("Option not supported for this target");
3455 xen_domid = atoi(optarg);
3457 case QEMU_OPTION_xen_attach:
3458 if (!(accel_find("xen"))) {
3459 error_report("Option not supported for this target");
3462 xen_mode = XEN_ATTACH;
3464 case QEMU_OPTION_xen_domid_restrict:
3465 if (!(accel_find("xen"))) {
3466 error_report("Option not supported for this target");
3469 xen_domid_restrict = true;
3471 case QEMU_OPTION_trace:
3472 trace_opt_parse(optarg);
3474 case QEMU_OPTION_plugin:
3475 qemu_plugin_opt_parse(optarg, &plugin_list);
3477 case QEMU_OPTION_readconfig:
3478 qemu_read_config_file(optarg, qemu_parse_config_group, &error_fatal);
3480 case QEMU_OPTION_spice:
3481 olist = qemu_find_opts_err("spice", NULL);
3483 error_report("spice support is disabled");
3486 opts = qemu_opts_parse_noisily(olist, optarg, false);
3492 case QEMU_OPTION_writeconfig:
3495 warn_report("-writeconfig is deprecated and will go away without a replacement");
3496 if (strcmp(optarg, "-") == 0) {
3499 fp = fopen(optarg, "w");
3501 error_report("open %s: %s", optarg,
3506 qemu_config_write(fp);
3512 case QEMU_OPTION_qtest:
3513 qtest_chrdev = optarg;
3515 case QEMU_OPTION_qtest_log:
3518 case QEMU_OPTION_sandbox:
3519 olist = qemu_find_opts("sandbox");
3521 #ifndef CONFIG_SECCOMP
3522 error_report("-sandbox support is not enabled "
3523 "in this QEMU binary");
3528 opts = qemu_opts_parse_noisily(olist, optarg, true);
3533 case QEMU_OPTION_add_fd:
3535 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
3541 error_report("File descriptor passing is disabled on this "
3546 case QEMU_OPTION_object:
3547 object_option_parse(optarg);
3549 case QEMU_OPTION_overcommit:
3550 opts = qemu_opts_parse_noisily(qemu_find_opts("overcommit"),
3555 enable_mlock = qemu_opt_get_bool(opts, "mem-lock", false);
3556 enable_cpu_pm = qemu_opt_get_bool(opts, "cpu-pm", false);
3558 case QEMU_OPTION_compat:
3563 v = qobject_input_visitor_new_str(optarg, NULL,
3566 visit_type_CompatPolicy(v, NULL, &opts, &error_fatal);
3567 QAPI_CLONE_MEMBERS(CompatPolicy, &compat_policy, opts);
3569 qapi_free_CompatPolicy(opts);
3573 case QEMU_OPTION_msg:
3574 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
3579 configure_msg(opts);
3581 case QEMU_OPTION_dump_vmstate:
3582 if (vmstate_dump_file) {
3583 error_report("only one '-dump-vmstate' "
3584 "option may be given");
3587 vmstate_dump_file = fopen(optarg, "w");
3588 if (vmstate_dump_file == NULL) {
3589 error_report("open %s: %s", optarg, strerror(errno));
3593 case QEMU_OPTION_enable_sync_profile:
3596 case QEMU_OPTION_nouserconfig:
3597 /* Nothing to be parsed here. Especially, do not error out below. */
3600 if (os_parse_cmd_args(popt->index, optarg)) {
3601 error_report("Option not supported in this build");
3608 * Clear error location left behind by the loop.
3609 * Best done right after the loop. Do not insert code here!
3613 qemu_validate_options(machine_opts_dict);
3614 qemu_process_sugar_options();
3617 * These options affect everything else and should be processed
3618 * before daemonizing.
3620 qemu_process_early_options();
3622 qemu_process_help_options();
3623 qemu_maybe_daemonize(pid_file);
3626 * The trace backend must be initialized after daemonizing.
3627 * trace_init_backends() will call st_init(), which will create the
3628 * trace thread in the parent, and also register st_flush_trace_buffer()
3629 * in atexit(). This function will force the parent to wait for the
3630 * writeout thread to finish, which will not occur, and the parent
3631 * process will be left in the host.
3633 if (!trace_init_backends()) {
3638 qemu_init_main_loop(&error_fatal);
3641 user_register_global_props();
3642 replay_configure(icount_opts);
3644 configure_rtc(qemu_find_opts_singleton("rtc"));
3646 qemu_create_machine(machine_opts_dict);
3650 qemu_disable_default_devices();
3651 qemu_create_default_devices();
3652 qemu_create_early_backends();
3654 qemu_apply_legacy_machine_options(machine_opts_dict);
3655 qemu_apply_machine_options(machine_opts_dict);
3656 qobject_unref(machine_opts_dict);
3657 phase_advance(PHASE_MACHINE_CREATED);
3660 * Note: uses machine properties such as kernel-irqchip, must run
3661 * after qemu_apply_machine_options.
3663 configure_accelerators(argv[0]);
3664 phase_advance(PHASE_ACCEL_CREATED);
3667 * Beware, QOM objects created before this point miss global and
3668 * compat properties.
3670 * Global properties get set up by qdev_prop_register_global(),
3671 * called from user_register_global_props(), and certain option
3672 * desugaring. Also in CPU feature desugaring (buried in
3673 * parse_cpu_option()), which happens below this point, but may
3674 * only target the CPU type, which can only be created after
3675 * parse_cpu_option() returned the type.
3677 * Machine compat properties: object_set_machine_compat_props().
3678 * Accelerator compat props: object_set_accelerator_compat_props(),
3679 * called from do_configure_accelerator().
3682 machine_class = MACHINE_GET_CLASS(current_machine);
3683 if (!qtest_enabled() && machine_class->deprecation_reason) {
3684 error_report("Machine type '%s' is deprecated: %s",
3685 machine_class->name, machine_class->deprecation_reason);
3689 * Note: creates a QOM object, must run only after global and
3690 * compat properties have been set up.
3692 migration_object_init();
3694 qemu_create_late_backends();
3696 /* parse features once if machine provides default cpu_type */
3697 current_machine->cpu_type = machine_class->default_cpu_type;
3699 current_machine->cpu_type = parse_cpu_option(cpu_option);
3701 /* NB: for machine none cpu_type could STILL be NULL here! */
3703 qemu_resolve_machine_memdev();
3704 parse_numa_opts(current_machine);
3706 if (vmstate_dump_file) {
3708 dump_vmstate_json_to_file(vmstate_dump_file);
3712 if (!preconfig_requested) {
3713 qmp_x_exit_preconfig(&error_fatal);
3715 qemu_init_displays();
3716 accel_setup_post(current_machine);