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/boards.h"
31 #include "hw/qdev-properties.h"
32 #include "qapi/error.h"
33 #include "qemu-version.h"
34 #include "qemu/cutils.h"
35 #include "qemu/help_option.h"
36 #include "qemu/uuid.h"
37 #include "sysemu/reset.h"
38 #include "sysemu/runstate.h"
39 #include "sysemu/seccomp.h"
40 #include "sysemu/tcg.h"
41 #include "sysemu/xen.h"
43 #include "qemu/error-report.h"
44 #include "qemu/sockets.h"
45 #include "sysemu/accel.h"
47 #include "hw/isa/isa.h"
48 #include "hw/scsi/scsi.h"
49 #include "hw/display/vga.h"
50 #include "sysemu/watchdog.h"
51 #include "hw/firmware/smbios.h"
52 #include "hw/acpi/acpi.h"
53 #include "hw/xen/xen.h"
54 #include "hw/loader.h"
55 #include "monitor/qdev.h"
57 #include "net/slirp.h"
58 #include "monitor/monitor.h"
59 #include "ui/console.h"
61 #include "sysemu/sysemu.h"
62 #include "sysemu/numa.h"
63 #include "sysemu/hostmem.h"
64 #include "exec/gdbstub.h"
65 #include "qemu/timer.h"
66 #include "chardev/char.h"
67 #include "qemu/bitmap.h"
69 #include "sysemu/blockdev.h"
70 #include "hw/block/block.h"
71 #include "hw/i386/x86.h"
72 #include "hw/i386/pc.h"
73 #include "migration/misc.h"
74 #include "migration/snapshot.h"
75 #include "sysemu/tpm.h"
76 #include "sysemu/dma.h"
77 #include "hw/audio/soundhw.h"
78 #include "audio/audio.h"
79 #include "sysemu/cpus.h"
80 #include "sysemu/cpu-timers.h"
81 #include "migration/colo.h"
82 #include "migration/postcopy-ram.h"
83 #include "sysemu/kvm.h"
84 #include "sysemu/hax.h"
85 #include "qapi/qobject-input-visitor.h"
86 #include "qemu/option.h"
87 #include "qemu/config-file.h"
88 #include "qemu-options.h"
89 #include "qemu/main-loop.h"
91 #include "fsdev/qemu-fsdev.h"
93 #include "sysemu/qtest.h"
95 #include "disas/disas.h"
98 #include "trace/control.h"
99 #include "qemu/plugin.h"
100 #include "qemu/queue.h"
101 #include "sysemu/arch_init.h"
103 #include "ui/qemu-spice.h"
104 #include "qapi/string-input-visitor.h"
105 #include "qapi/opts-visitor.h"
106 #include "qapi/clone-visitor.h"
107 #include "qom/object_interfaces.h"
108 #include "hw/semihosting/semihost.h"
109 #include "crypto/init.h"
110 #include "sysemu/replay.h"
111 #include "qapi/qapi-events-run-state.h"
112 #include "qapi/qapi-visit-block-core.h"
113 #include "qapi/qapi-visit-ui.h"
114 #include "qapi/qapi-commands-block-core.h"
115 #include "qapi/qapi-commands-migration.h"
116 #include "qapi/qapi-commands-misc.h"
117 #include "qapi/qapi-commands-ui.h"
118 #include "qapi/qmp/qerror.h"
119 #include "sysemu/iothread.h"
120 #include "qemu/guest-random.h"
122 #define MAX_VIRTIO_CONSOLES 1
124 typedef struct BlockdevOptionsQueueEntry {
125 BlockdevOptions *bdo;
127 QSIMPLEQ_ENTRY(BlockdevOptionsQueueEntry) entry;
128 } BlockdevOptionsQueueEntry;
130 typedef QSIMPLEQ_HEAD(, BlockdevOptionsQueueEntry) BlockdevOptionsQueue;
132 static const char *cpu_option;
133 static const char *mem_path;
134 static const char *incoming;
135 static const char *loadvm;
136 static ram_addr_t maxram_size;
137 static uint64_t ram_slots;
138 static int display_remote;
140 static bool preconfig_requested;
141 static QemuPluginList plugin_list = QTAILQ_HEAD_INITIALIZER(plugin_list);
142 static BlockdevOptionsQueue bdo_queue = QSIMPLEQ_HEAD_INITIALIZER(bdo_queue);
143 static bool nographic = false;
144 static int mem_prealloc; /* force preallocation of physical target memory */
145 static ram_addr_t ram_size;
146 static const char *vga_model = NULL;
147 static DisplayOptions dpy;
148 static int num_serial_hds;
149 static Chardev **serial_hds;
150 static const char *log_mask;
151 static const char *log_file;
152 static bool list_data_dirs;
153 static const char *watchdog;
154 static const char *qtest_chrdev;
155 static const char *qtest_log;
157 static int has_defaults = 1;
158 static int default_serial = 1;
159 static int default_parallel = 1;
160 static int default_monitor = 1;
161 static int default_floppy = 1;
162 static int default_cdrom = 1;
163 static int default_sdcard = 1;
164 static int default_vga = 1;
165 static int default_net = 1;
171 { .driver = "isa-serial", .flag = &default_serial },
172 { .driver = "isa-parallel", .flag = &default_parallel },
173 { .driver = "isa-fdc", .flag = &default_floppy },
174 { .driver = "floppy", .flag = &default_floppy },
175 { .driver = "ide-cd", .flag = &default_cdrom },
176 { .driver = "ide-hd", .flag = &default_cdrom },
177 { .driver = "ide-drive", .flag = &default_cdrom },
178 { .driver = "scsi-cd", .flag = &default_cdrom },
179 { .driver = "scsi-hd", .flag = &default_cdrom },
180 { .driver = "VGA", .flag = &default_vga },
181 { .driver = "isa-vga", .flag = &default_vga },
182 { .driver = "cirrus-vga", .flag = &default_vga },
183 { .driver = "isa-cirrus-vga", .flag = &default_vga },
184 { .driver = "vmware-svga", .flag = &default_vga },
185 { .driver = "qxl-vga", .flag = &default_vga },
186 { .driver = "virtio-vga", .flag = &default_vga },
187 { .driver = "ati-vga", .flag = &default_vga },
188 { .driver = "vhost-user-vga", .flag = &default_vga },
191 static QemuOptsList qemu_rtc_opts = {
193 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
198 .type = QEMU_OPT_STRING,
201 .type = QEMU_OPT_STRING,
204 .type = QEMU_OPT_STRING,
206 { /* end of list */ }
210 static QemuOptsList qemu_option_rom_opts = {
211 .name = "option-rom",
212 .implied_opt_name = "romfile",
213 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
217 .type = QEMU_OPT_NUMBER,
220 .type = QEMU_OPT_STRING,
222 { /* end of list */ }
226 static QemuOptsList qemu_machine_opts = {
228 .implied_opt_name = "type",
230 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
233 * no elements => accept any
234 * sanity checking will happen later
235 * when setting machine properties
241 static QemuOptsList qemu_accel_opts = {
243 .implied_opt_name = "accel",
244 .head = QTAILQ_HEAD_INITIALIZER(qemu_accel_opts.head),
247 * no elements => accept any
248 * sanity checking will happen later
249 * when setting accelerator properties
255 static QemuOptsList qemu_boot_opts = {
257 .implied_opt_name = "order",
259 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
263 .type = QEMU_OPT_STRING,
266 .type = QEMU_OPT_STRING,
269 .type = QEMU_OPT_BOOL,
272 .type = QEMU_OPT_STRING,
274 .name = "splash-time",
275 .type = QEMU_OPT_NUMBER,
277 .name = "reboot-timeout",
278 .type = QEMU_OPT_NUMBER,
281 .type = QEMU_OPT_BOOL,
287 static QemuOptsList qemu_add_fd_opts = {
289 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
293 .type = QEMU_OPT_NUMBER,
294 .help = "file descriptor of which a duplicate is added to fd set",
297 .type = QEMU_OPT_NUMBER,
298 .help = "ID of the fd set to add fd to",
301 .type = QEMU_OPT_STRING,
302 .help = "free-form string used to describe fd",
304 { /* end of list */ }
308 static QemuOptsList qemu_object_opts = {
310 .implied_opt_name = "qom-type",
311 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
317 static QemuOptsList qemu_tpmdev_opts = {
319 .implied_opt_name = "type",
320 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
322 /* options are defined in the TPM backends */
323 { /* end of list */ }
327 static QemuOptsList qemu_realtime_opts = {
329 .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
333 .type = QEMU_OPT_BOOL,
335 { /* end of list */ }
339 static QemuOptsList qemu_overcommit_opts = {
340 .name = "overcommit",
341 .head = QTAILQ_HEAD_INITIALIZER(qemu_overcommit_opts.head),
345 .type = QEMU_OPT_BOOL,
349 .type = QEMU_OPT_BOOL,
351 { /* end of list */ }
355 static QemuOptsList qemu_msg_opts = {
357 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
361 .type = QEMU_OPT_BOOL,
364 .name = "guest-name",
365 .type = QEMU_OPT_BOOL,
366 .help = "Prepends guest name for error messages but only if "
367 "-name guest is set otherwise option is ignored\n",
369 { /* end of list */ }
373 static QemuOptsList qemu_name_opts = {
375 .implied_opt_name = "guest",
377 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
381 .type = QEMU_OPT_STRING,
382 .help = "Sets the name of the guest.\n"
383 "This name will be displayed in the SDL window caption.\n"
384 "The name will also be used for the VNC server",
387 .type = QEMU_OPT_STRING,
388 .help = "Sets the name of the QEMU process, as shown in top etc",
390 .name = "debug-threads",
391 .type = QEMU_OPT_BOOL,
392 .help = "When enabled, name the individual threads; defaults off.\n"
393 "NOTE: The thread names are for debugging and not a\n"
396 { /* End of list */ }
400 static QemuOptsList qemu_mem_opts = {
402 .implied_opt_name = "size",
403 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
408 .type = QEMU_OPT_SIZE,
412 .type = QEMU_OPT_NUMBER,
416 .type = QEMU_OPT_SIZE,
418 { /* end of list */ }
422 static QemuOptsList qemu_icount_opts = {
424 .implied_opt_name = "shift",
426 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
430 .type = QEMU_OPT_STRING,
433 .type = QEMU_OPT_BOOL,
436 .type = QEMU_OPT_BOOL,
439 .type = QEMU_OPT_STRING,
442 .type = QEMU_OPT_STRING,
444 .name = "rrsnapshot",
445 .type = QEMU_OPT_STRING,
447 { /* end of list */ }
451 static QemuOptsList qemu_fw_cfg_opts = {
453 .implied_opt_name = "name",
454 .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
458 .type = QEMU_OPT_STRING,
459 .help = "Sets the fw_cfg name of the blob to be inserted",
462 .type = QEMU_OPT_STRING,
463 .help = "Sets the name of the file from which "
464 "the fw_cfg blob will be loaded",
467 .type = QEMU_OPT_STRING,
468 .help = "Sets content of the blob to be inserted from a string",
471 .type = QEMU_OPT_STRING,
472 .help = "Sets id of the object generating the fw_cfg blob "
475 { /* end of list */ }
480 * Get machine options
482 * Returns: machine options (never null).
484 static QemuOpts *qemu_get_machine_opts(void)
486 return qemu_find_opts_singleton("machine");
489 const char *qemu_get_vm_name(void)
494 static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
496 const char *driver = qemu_opt_get(opts, "driver");
501 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
502 if (strcmp(default_list[i].driver, driver) != 0)
504 *(default_list[i].flag) = 0;
509 static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
511 const char *proc_name;
513 if (qemu_opt_get(opts, "debug-threads")) {
514 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
516 qemu_name = qemu_opt_get(opts, "guest");
518 proc_name = qemu_opt_get(opts, "process");
520 os_set_proc_name(proc_name);
526 bool defaults_enabled(void)
532 static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
534 int fd, dupfd, flags;
536 const char *fd_opaque = NULL;
539 fd = qemu_opt_get_number(opts, "fd", -1);
540 fdset_id = qemu_opt_get_number(opts, "set", -1);
541 fd_opaque = qemu_opt_get(opts, "opaque");
544 error_setg(errp, "fd option is required and must be non-negative");
548 if (fd <= STDERR_FILENO) {
549 error_setg(errp, "fd cannot be a standard I/O stream");
554 * All fds inherited across exec() necessarily have FD_CLOEXEC
555 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
557 flags = fcntl(fd, F_GETFD);
558 if (flags == -1 || (flags & FD_CLOEXEC)) {
559 error_setg(errp, "fd is not valid or already in use");
564 error_setg(errp, "set option is required and must be non-negative");
568 #ifdef F_DUPFD_CLOEXEC
569 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
573 qemu_set_cloexec(dupfd);
577 error_setg(errp, "error duplicating fd: %s", strerror(errno));
581 /* add the duplicate fd, and optionally the opaque string, to the fd set */
582 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
589 static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
593 fd = qemu_opt_get_number(opts, "fd", -1);
600 /***********************************************************/
601 /* QEMU Block devices */
603 #define HD_OPTS "media=disk"
604 #define CDROM_OPTS "media=cdrom"
606 #define PFLASH_OPTS ""
610 static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
612 BlockInterfaceType *block_default_type = opaque;
614 return drive_new(opts, *block_default_type, errp) == NULL;
617 static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
619 if (qemu_opt_get(opts, "snapshot") == NULL) {
620 qemu_opt_set(opts, "snapshot", "on", &error_abort);
625 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
626 int index, const char *optstr)
631 if (!enable || drive_get_by_index(type, index)) {
635 opts = drive_add(type, index, NULL, optstr);
637 drive_enable_snapshot(NULL, opts, NULL);
640 dinfo = drive_new(opts, type, &error_abort);
641 dinfo->is_default = true;
645 static void configure_blockdev(BlockdevOptionsQueue *bdo_queue,
646 MachineClass *machine_class, int snapshot)
649 * If the currently selected machine wishes to override the
650 * units-per-bus property of its default HBA interface type, do so
653 if (machine_class->units_per_default_bus) {
654 override_max_devs(machine_class->block_default_type,
655 machine_class->units_per_default_bus);
658 /* open the virtual block devices */
659 while (!QSIMPLEQ_EMPTY(bdo_queue)) {
660 BlockdevOptionsQueueEntry *bdo = QSIMPLEQ_FIRST(bdo_queue);
662 QSIMPLEQ_REMOVE_HEAD(bdo_queue, entry);
663 loc_push_restore(&bdo->loc);
664 qmp_blockdev_add(bdo->bdo, &error_fatal);
666 qapi_free_BlockdevOptions(bdo->bdo);
670 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
673 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
674 &machine_class->block_default_type, &error_fatal)) {
675 /* We printed help */
679 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
681 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
682 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
686 static QemuOptsList qemu_smp_opts = {
688 .implied_opt_name = "cpus",
690 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
694 .type = QEMU_OPT_NUMBER,
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,
715 static void realtime_init(void)
718 if (os_mlock() < 0) {
719 error_report("locking memory failed");
726 static void configure_msg(QemuOpts *opts)
728 error_with_timestamp = qemu_opt_get_bool(opts, "timestamp", false);
729 error_with_guestname = qemu_opt_get_bool(opts, "guest-name", false);
733 /***********************************************************/
736 static int usb_device_add(const char *devname)
738 USBDevice *dev = NULL;
740 if (!machine_usb(current_machine)) {
744 dev = usbdevice_create(devname);
751 static int usb_parse(const char *cmdline)
754 r = usb_device_add(cmdline);
756 error_report("could not add USB device '%s'", cmdline);
761 /***********************************************************/
762 /* machine registration */
764 static MachineClass *find_machine(const char *name, GSList *machines)
768 for (el = machines; el; el = el->next) {
769 MachineClass *mc = el->data;
771 if (!strcmp(mc->name, name) || !g_strcmp0(mc->alias, name)) {
779 static MachineClass *find_default_machine(GSList *machines)
782 MachineClass *default_machineclass = NULL;
784 for (el = machines; el; el = el->next) {
785 MachineClass *mc = el->data;
787 if (mc->is_default) {
788 assert(default_machineclass == NULL && "Multiple default machines");
789 default_machineclass = mc;
793 return default_machineclass;
796 static int machine_help_func(QemuOpts *opts, MachineState *machine)
798 ObjectProperty *prop;
799 ObjectPropertyIterator iter;
801 if (!qemu_opt_has_help_opt(opts)) {
805 object_property_iter_init(&iter, OBJECT(machine));
806 while ((prop = object_property_iter_next(&iter))) {
811 printf("%s.%s=%s", MACHINE_GET_CLASS(machine)->name,
812 prop->name, prop->type);
813 if (prop->description) {
814 printf(" (%s)\n", prop->description);
823 static void version(void)
825 printf("QEMU emulator version " QEMU_FULL_VERSION "\n"
826 QEMU_COPYRIGHT "\n");
829 static void help(int exitcode)
832 printf("usage: %s [options] [disk_image]\n\n"
833 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
834 error_get_progname());
836 #define QEMU_OPTIONS_GENERATE_HELP
837 #include "qemu-options-wrapper.h"
839 printf("\nDuring emulation, the following keys are useful:\n"
840 "ctrl-alt-f toggle full screen\n"
841 "ctrl-alt-n switch to virtual console 'n'\n"
842 "ctrl-alt toggle mouse and keyboard grab\n"
844 "When using -nographic, press 'ctrl-a h' to get some help.\n"
846 QEMU_HELP_BOTTOM "\n");
851 #define HAS_ARG 0x0001
853 typedef struct QEMUOption {
860 static const QEMUOption qemu_options[] = {
861 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
862 #define QEMU_OPTIONS_GENERATE_OPTIONS
863 #include "qemu-options-wrapper.h"
867 typedef struct VGAInterfaceInfo {
868 const char *opt_name; /* option name */
869 const char *name; /* human-readable name */
870 /* Class names indicating that support is available.
871 * If no class is specified, the interface is always available */
872 const char *class_names[2];
875 static const VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
878 .name = "no graphic card",
882 .name = "standard VGA",
883 .class_names = { "VGA", "isa-vga" },
886 .opt_name = "cirrus",
887 .name = "Cirrus VGA",
888 .class_names = { "cirrus-vga", "isa-cirrus-vga" },
891 .opt_name = "vmware",
892 .name = "VMWare SVGA",
893 .class_names = { "vmware-svga" },
896 .opt_name = "virtio",
897 .name = "Virtio VGA",
898 .class_names = { "virtio-vga" },
903 .class_names = { "qxl-vga" },
907 .name = "TCX framebuffer",
908 .class_names = { "SUNW,tcx" },
912 .name = "CG3 framebuffer",
913 .class_names = { "cgthree" },
917 .name = "Xen paravirtualized framebuffer",
921 static bool vga_interface_available(VGAInterfaceType t)
923 const VGAInterfaceInfo *ti = &vga_interfaces[t];
925 assert(t < VGA_TYPE_MAX);
926 return !ti->class_names[0] ||
927 module_object_class_by_name(ti->class_names[0]) ||
928 module_object_class_by_name(ti->class_names[1]);
932 get_default_vga_model(const MachineClass *machine_class)
934 if (machine_class->default_display) {
935 return machine_class->default_display;
936 } else if (vga_interface_available(VGA_CIRRUS)) {
938 } else if (vga_interface_available(VGA_STD)) {
945 static void select_vgahw(const MachineClass *machine_class, const char *p)
950 if (g_str_equal(p, "help")) {
951 const char *def = get_default_vga_model(machine_class);
953 for (t = 0; t < VGA_TYPE_MAX; t++) {
954 const VGAInterfaceInfo *ti = &vga_interfaces[t];
956 if (vga_interface_available(t) && ti->opt_name) {
957 printf("%-20s %s%s\n", ti->opt_name, ti->name ?: "",
958 g_str_equal(ti->opt_name, def) ? " (default)" : "");
964 assert(vga_interface_type == VGA_NONE);
965 for (t = 0; t < VGA_TYPE_MAX; t++) {
966 const VGAInterfaceInfo *ti = &vga_interfaces[t];
967 if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
968 if (!vga_interface_available(t)) {
969 error_report("%s not available", ti->name);
972 vga_interface_type = t;
976 if (t == VGA_TYPE_MAX) {
978 error_report("unknown vga type: %s", p);
984 if (strstart(opts, ",retrace=", &nextopt)) {
986 if (strstart(opts, "dumb", &nextopt))
987 vga_retrace_method = VGA_RETRACE_DUMB;
988 else if (strstart(opts, "precise", &nextopt))
989 vga_retrace_method = VGA_RETRACE_PRECISE;
990 else goto invalid_vga;
991 } else goto invalid_vga;
996 static void parse_display_qapi(const char *optarg)
998 DisplayOptions *opts;
1001 v = qobject_input_visitor_new_str(optarg, "type", &error_fatal);
1003 visit_type_DisplayOptions(v, NULL, &opts, &error_fatal);
1004 QAPI_CLONE_MEMBERS(DisplayOptions, &dpy, opts);
1006 qapi_free_DisplayOptions(opts);
1010 DisplayOptions *qmp_query_display_options(Error **errp)
1012 return QAPI_CLONE(DisplayOptions, &dpy);
1015 static void parse_display(const char *p)
1019 if (is_help_option(p)) {
1020 qemu_display_help();
1024 if (strstart(p, "sdl", &opts)) {
1026 * sdl DisplayType needs hand-crafted parser instead of
1027 * parse_display_qapi() due to some options not in
1028 * DisplayOptions, specifically:
1030 * Already deprecated.
1031 * - ctrl_grab + alt_grab
1032 * Not clear yet what happens to them long-term. Should
1033 * replaced by something better or deprecated and dropped.
1035 dpy.type = DISPLAY_TYPE_SDL;
1037 const char *nextopt;
1039 if (strstart(opts, ",alt_grab=", &nextopt)) {
1041 if (strstart(opts, "on", &nextopt)) {
1043 } else if (strstart(opts, "off", &nextopt)) {
1046 goto invalid_sdl_args;
1048 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1050 if (strstart(opts, "on", &nextopt)) {
1052 } else if (strstart(opts, "off", &nextopt)) {
1055 goto invalid_sdl_args;
1057 } else if (strstart(opts, ",window_close=", &nextopt)) {
1059 dpy.has_window_close = true;
1060 if (strstart(opts, "on", &nextopt)) {
1061 dpy.window_close = true;
1062 } else if (strstart(opts, "off", &nextopt)) {
1063 dpy.window_close = false;
1065 goto invalid_sdl_args;
1067 } else if (strstart(opts, ",show-cursor=", &nextopt)) {
1069 dpy.has_show_cursor = true;
1070 if (strstart(opts, "on", &nextopt)) {
1071 dpy.show_cursor = true;
1072 } else if (strstart(opts, "off", &nextopt)) {
1073 dpy.show_cursor = false;
1075 goto invalid_sdl_args;
1077 } else if (strstart(opts, ",gl=", &nextopt)) {
1080 if (strstart(opts, "on", &nextopt)) {
1081 dpy.gl = DISPLAYGL_MODE_ON;
1082 } else if (strstart(opts, "core", &nextopt)) {
1083 dpy.gl = DISPLAYGL_MODE_CORE;
1084 } else if (strstart(opts, "es", &nextopt)) {
1085 dpy.gl = DISPLAYGL_MODE_ES;
1086 } else if (strstart(opts, "off", &nextopt)) {
1087 dpy.gl = DISPLAYGL_MODE_OFF;
1089 goto invalid_sdl_args;
1093 error_report("invalid SDL option string");
1098 } else if (strstart(p, "vnc", &opts)) {
1100 * vnc isn't a (local) DisplayType but a protocol for remote
1104 vnc_parse(opts + 1, &error_fatal);
1106 error_report("VNC requires a display argument vnc=<display>");
1110 parse_display_qapi(p);
1114 static inline bool nonempty_str(const char *str)
1119 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
1123 const char *name, *file, *str, *gen_id;
1124 FWCfgState *fw_cfg = (FWCfgState *) opaque;
1126 if (fw_cfg == NULL) {
1127 error_setg(errp, "fw_cfg device not available");
1130 name = qemu_opt_get(opts, "name");
1131 file = qemu_opt_get(opts, "file");
1132 str = qemu_opt_get(opts, "string");
1133 gen_id = qemu_opt_get(opts, "gen_id");
1135 /* we need the name, and exactly one of: file, content string, gen_id */
1136 if (!nonempty_str(name) ||
1137 nonempty_str(file) + nonempty_str(str) + nonempty_str(gen_id) != 1) {
1138 error_setg(errp, "name, plus exactly one of file,"
1139 " string and gen_id, are needed");
1142 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
1143 error_setg(errp, "name too long (max. %d char)",
1144 FW_CFG_MAX_FILE_PATH - 1);
1147 if (nonempty_str(gen_id)) {
1149 * In this particular case where the content is populated
1150 * internally, the "etc/" namespace protection is relaxed,
1151 * so do not emit a warning.
1153 } else if (strncmp(name, "opt/", 4) != 0) {
1154 warn_report("externally provided fw_cfg item names "
1155 "should be prefixed with \"opt/\"");
1157 if (nonempty_str(str)) {
1158 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
1159 buf = g_memdup(str, size);
1160 } else if (nonempty_str(gen_id)) {
1161 if (!fw_cfg_add_from_generator(fw_cfg, name, gen_id, errp)) {
1167 if (!g_file_get_contents(file, &buf, &size, &err)) {
1168 error_setg(errp, "can't load %s: %s", file, err->message);
1173 /* For legacy, keep user files in a specific global order. */
1174 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
1175 fw_cfg_add_file(fw_cfg, name, buf, size);
1176 fw_cfg_reset_order_override(fw_cfg);
1180 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
1182 return qdev_device_help(opts);
1185 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
1189 dev = qdev_device_add(opts, errp);
1190 if (!dev && *errp) {
1191 error_report_err(*errp);
1194 object_unref(OBJECT(dev));
1199 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
1201 Error *local_err = NULL;
1203 if (!qemu_chr_new_from_opts(opts, NULL, &local_err)) {
1205 error_propagate(errp, local_err);
1213 #ifdef CONFIG_VIRTFS
1214 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
1216 return qemu_fsdev_add(opts, errp);
1220 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
1222 return monitor_init_opts(opts, errp);
1225 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
1227 static int monitor_device_index = 0;
1232 if (strstart(optarg, "chardev:", &p)) {
1233 snprintf(label, sizeof(label), "%s", p);
1235 snprintf(label, sizeof(label), "compat_monitor%d",
1236 monitor_device_index);
1237 opts = qemu_chr_parse_compat(label, optarg, true);
1239 error_report("parse error: %s", optarg);
1244 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
1245 qemu_opt_set(opts, "mode", mode, &error_abort);
1246 qemu_opt_set(opts, "chardev", label, &error_abort);
1247 if (!strcmp(mode, "control")) {
1248 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
1250 assert(pretty == false);
1252 monitor_device_index++;
1255 struct device_config {
1257 DEV_USB, /* -usbdevice */
1258 DEV_SERIAL, /* -serial */
1259 DEV_PARALLEL, /* -parallel */
1260 DEV_DEBUGCON, /* -debugcon */
1261 DEV_GDB, /* -gdb, -s */
1262 DEV_SCLP, /* s390 sclp */
1264 const char *cmdline;
1266 QTAILQ_ENTRY(device_config) next;
1269 static QTAILQ_HEAD(, device_config) device_configs =
1270 QTAILQ_HEAD_INITIALIZER(device_configs);
1272 static void add_device_config(int type, const char *cmdline)
1274 struct device_config *conf;
1276 conf = g_malloc0(sizeof(*conf));
1278 conf->cmdline = cmdline;
1279 loc_save(&conf->loc);
1280 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1283 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1285 struct device_config *conf;
1288 QTAILQ_FOREACH(conf, &device_configs, next) {
1289 if (conf->type != type)
1291 loc_push_restore(&conf->loc);
1292 rc = func(conf->cmdline);
1293 loc_pop(&conf->loc);
1301 static void qemu_disable_default_devices(void)
1303 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1305 qemu_opts_foreach(qemu_find_opts("device"),
1306 default_driver_check, NULL, NULL);
1307 qemu_opts_foreach(qemu_find_opts("global"),
1308 default_driver_check, NULL, NULL);
1310 if (!vga_model && !default_vga) {
1311 vga_interface_type = VGA_DEVICE;
1313 if (!has_defaults || machine_class->no_serial) {
1316 if (!has_defaults || machine_class->no_parallel) {
1317 default_parallel = 0;
1319 if (!has_defaults || machine_class->no_floppy) {
1322 if (!has_defaults || machine_class->no_cdrom) {
1325 if (!has_defaults || machine_class->no_sdcard) {
1328 if (!has_defaults) {
1329 default_monitor = 0;
1335 static void qemu_create_default_devices(void)
1337 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1339 if (is_daemonized()) {
1340 /* According to documentation and historically, -nographic redirects
1341 * serial port, parallel port and monitor to stdio, which does not work
1342 * with -daemonize. We can redirect these to null instead, but since
1343 * -nographic is legacy, let's just error out.
1344 * We disallow -nographic only if all other ports are not redirected
1345 * explicitly, to not break existing legacy setups which uses
1346 * -nographic _and_ redirects all ports explicitly - this is valid
1347 * usage, -nographic is just a no-op in this case.
1350 && (default_parallel || default_serial || default_monitor)) {
1351 error_report("-nographic cannot be used with -daemonize");
1357 if (default_parallel)
1358 add_device_config(DEV_PARALLEL, "null");
1359 if (default_serial && default_monitor) {
1360 add_device_config(DEV_SERIAL, "mon:stdio");
1363 add_device_config(DEV_SERIAL, "stdio");
1364 if (default_monitor)
1365 monitor_parse("stdio", "readline", false);
1369 add_device_config(DEV_SERIAL, "vc:80Cx24C");
1370 if (default_parallel)
1371 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
1372 if (default_monitor)
1373 monitor_parse("vc:80Cx24C", "readline", false);
1377 QemuOptsList *net = qemu_find_opts("net");
1378 qemu_opts_parse(net, "nic", true, &error_abort);
1380 qemu_opts_parse(net, "user", true, &error_abort);
1384 #if defined(CONFIG_VNC)
1385 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
1389 if (dpy.type == DISPLAY_TYPE_DEFAULT && !display_remote) {
1390 if (!qemu_display_find_default(&dpy)) {
1391 dpy.type = DISPLAY_TYPE_NONE;
1392 #if defined(CONFIG_VNC)
1393 vnc_parse("localhost:0,to=99,id=default", &error_abort);
1397 if (dpy.type == DISPLAY_TYPE_DEFAULT) {
1398 dpy.type = DISPLAY_TYPE_NONE;
1401 /* If no default VGA is requested, the default is "none". */
1403 vga_model = get_default_vga_model(machine_class);
1406 select_vgahw(machine_class, vga_model);
1410 static int serial_parse(const char *devname)
1412 int index = num_serial_hds;
1415 if (strcmp(devname, "none") == 0)
1417 snprintf(label, sizeof(label), "serial%d", index);
1418 serial_hds = g_renew(Chardev *, serial_hds, index + 1);
1420 serial_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
1421 if (!serial_hds[index]) {
1422 error_report("could not connect serial device"
1423 " to character backend '%s'", devname);
1430 Chardev *serial_hd(int i)
1433 if (i < num_serial_hds) {
1434 return serial_hds[i];
1439 static int parallel_parse(const char *devname)
1441 static int index = 0;
1444 if (strcmp(devname, "none") == 0)
1446 if (index == MAX_PARALLEL_PORTS) {
1447 error_report("too many parallel ports");
1450 snprintf(label, sizeof(label), "parallel%d", index);
1451 parallel_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
1452 if (!parallel_hds[index]) {
1453 error_report("could not connect parallel device"
1454 " to character backend '%s'", devname);
1461 static int debugcon_parse(const char *devname)
1465 if (!qemu_chr_new_mux_mon("debugcon", devname, NULL)) {
1466 error_report("invalid character backend '%s'", devname);
1469 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
1471 error_report("already have a debugcon device");
1474 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
1475 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
1479 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
1481 const MachineClass *mc1 = a, *mc2 = b;
1484 if (mc1->family == NULL) {
1485 if (mc2->family == NULL) {
1486 /* Compare standalone machine types against each other; they sort
1487 * in increasing order.
1489 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
1490 object_class_get_name(OBJECT_CLASS(mc2)));
1493 /* Standalone machine types sort after families. */
1497 if (mc2->family == NULL) {
1498 /* Families sort before standalone machine types. */
1502 /* Families sort between each other alphabetically increasingly. */
1503 res = strcmp(mc1->family, mc2->family);
1508 /* Within the same family, machine types sort in decreasing order. */
1509 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
1510 object_class_get_name(OBJECT_CLASS(mc1)));
1513 static MachineClass *machine_parse(const char *name, GSList *machines)
1518 if (is_help_option(name)) {
1519 printf("Supported machines are:\n");
1520 machines = g_slist_sort(machines, machine_class_cmp);
1521 for (el = machines; el; el = el->next) {
1522 MachineClass *mc = el->data;
1524 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
1526 printf("%-20s %s%s%s\n", mc->name, mc->desc,
1527 mc->is_default ? " (default)" : "",
1528 mc->deprecation_reason ? " (deprecated)" : "");
1533 mc = find_machine(name, machines);
1535 error_report("unsupported machine type");
1536 error_printf("Use -machine help to list supported machines\n");
1542 static const char *pid_file;
1543 static Notifier qemu_unlink_pidfile_notifier;
1545 static void qemu_unlink_pidfile(Notifier *n, void *data)
1552 static const QEMUOption *lookup_opt(int argc, char **argv,
1553 const char **poptarg, int *poptind)
1555 const QEMUOption *popt;
1556 int optind = *poptind;
1557 char *r = argv[optind];
1560 loc_set_cmdline(argv, optind, 1);
1562 /* Treat --foo the same as -foo. */
1565 popt = qemu_options;
1568 error_report("invalid option");
1571 if (!strcmp(popt->name, r + 1))
1575 if (popt->flags & HAS_ARG) {
1576 if (optind >= argc) {
1577 error_report("requires an argument");
1580 optarg = argv[optind++];
1581 loc_set_cmdline(argv, optind - 2, 2);
1592 static MachineClass *select_machine(void)
1594 GSList *machines = object_class_get_list(TYPE_MACHINE, false);
1595 MachineClass *machine_class = find_default_machine(machines);
1600 loc_push_none(&loc);
1602 opts = qemu_get_machine_opts();
1603 qemu_opts_loc_restore(opts);
1605 optarg = qemu_opt_get(opts, "type");
1607 machine_class = machine_parse(optarg, machines);
1610 if (!machine_class) {
1611 error_report("No machine specified, and there is no default");
1612 error_printf("Use -machine help to list supported machines\n");
1617 g_slist_free(machines);
1618 return machine_class;
1621 static int object_parse_property_opt(Object *obj,
1622 const char *name, const char *value,
1623 const char *skip, Error **errp)
1625 if (g_str_equal(name, skip)) {
1629 if (!object_property_parse(obj, name, value, errp)) {
1636 static int machine_set_property(void *opaque,
1637 const char *name, const char *value,
1640 g_autofree char *qom_name = g_strdup(name);
1643 for (p = qom_name; *p; p++) {
1649 /* Legacy options do not correspond to MachineState properties. */
1650 if (g_str_equal(qom_name, "accel")) {
1653 if (g_str_equal(qom_name, "igd-passthru")) {
1654 object_register_sugar_prop(ACCEL_CLASS_NAME("xen"), qom_name, value);
1657 if (g_str_equal(qom_name, "kvm-shadow-mem")) {
1658 object_register_sugar_prop(ACCEL_CLASS_NAME("kvm"), qom_name, value);
1661 if (g_str_equal(qom_name, "kernel-irqchip")) {
1662 object_register_sugar_prop(ACCEL_CLASS_NAME("kvm"), qom_name, value);
1663 object_register_sugar_prop(ACCEL_CLASS_NAME("whpx"), qom_name, value);
1667 return object_parse_property_opt(opaque, name, value, "type", errp);
1671 * Initial object creation happens before all other
1672 * QEMU data types are created. The majority of objects
1673 * can be created at this point. The rng-egd object
1674 * cannot be created here, as it depends on the chardev
1677 static bool object_create_early(const char *type, QemuOpts *opts)
1679 if (user_creatable_print_help(type, opts)) {
1684 * Objects should not be made "delayed" without a reason. If you
1685 * add one, state the reason in a comment!
1688 /* Reason: rng-egd property "chardev" */
1689 if (g_str_equal(type, "rng-egd")) {
1693 #if defined(CONFIG_VHOST_USER) && defined(CONFIG_LINUX)
1694 /* Reason: cryptodev-vhost-user property "chardev" */
1695 if (g_str_equal(type, "cryptodev-vhost-user")) {
1700 /* Reason: vhost-user-blk-server property "node-name" */
1701 if (g_str_equal(type, "vhost-user-blk-server")) {
1705 * Reason: filter-* property "netdev" etc.
1707 if (g_str_equal(type, "filter-buffer") ||
1708 g_str_equal(type, "filter-dump") ||
1709 g_str_equal(type, "filter-mirror") ||
1710 g_str_equal(type, "filter-redirector") ||
1711 g_str_equal(type, "colo-compare") ||
1712 g_str_equal(type, "filter-rewriter") ||
1713 g_str_equal(type, "filter-replay")) {
1717 /* Memory allocation by backends needs to be done
1718 * after configure_accelerator() (due to the tcg_enabled()
1719 * checks at memory_region_init_*()).
1721 * Also, allocation of large amounts of memory may delay
1722 * chardev initialization for too long, and trigger timeouts
1723 * on software that waits for a monitor socket to be created
1726 if (g_str_has_prefix(type, "memory-backend-")) {
1733 static void qemu_apply_machine_options(void)
1735 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1736 QemuOpts *machine_opts = qemu_get_machine_opts();
1737 const char *boot_order = NULL;
1738 const char *boot_once = NULL;
1741 qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
1743 current_machine->ram_size = ram_size;
1744 current_machine->maxram_size = maxram_size;
1745 current_machine->ram_slots = ram_slots;
1747 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
1749 boot_order = qemu_opt_get(opts, "order");
1751 validate_bootdevices(boot_order, &error_fatal);
1754 boot_once = qemu_opt_get(opts, "once");
1756 validate_bootdevices(boot_once, &error_fatal);
1759 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
1760 boot_strict = qemu_opt_get_bool(opts, "strict", false);
1764 boot_order = machine_class->default_boot_order;
1767 current_machine->boot_order = boot_order;
1768 current_machine->boot_once = boot_once;
1770 if (semihosting_enabled() && !semihosting_get_argc()) {
1771 const char *kernel_filename = qemu_opt_get(machine_opts, "kernel");
1772 const char *kernel_cmdline = qemu_opt_get(machine_opts, "append") ?: "";
1773 /* fall back to the -kernel/-append */
1774 semihosting_arg_fallback(kernel_filename, kernel_cmdline);
1778 static void qemu_create_early_backends(void)
1780 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1782 if ((alt_grab || ctrl_grab) && dpy.type != DISPLAY_TYPE_SDL) {
1783 error_report("-alt-grab and -ctrl-grab are only valid "
1784 "for SDL, ignoring option");
1786 if (dpy.has_window_close &&
1787 (dpy.type != DISPLAY_TYPE_GTK && dpy.type != DISPLAY_TYPE_SDL)) {
1788 error_report("-no-quit is only valid for GTK and SDL, "
1792 qemu_display_early_init(&dpy);
1793 qemu_console_early_init();
1795 if (dpy.has_gl && dpy.gl != DISPLAYGL_MODE_OFF && display_opengl == 0) {
1796 #if defined(CONFIG_OPENGL)
1797 error_report("OpenGL is not supported by the display");
1799 error_report("OpenGL support is disabled");
1804 qemu_opts_foreach(qemu_find_opts("object"),
1805 user_creatable_add_opts_foreach,
1806 object_create_early, &error_fatal);
1808 /* spice needs the timers to be initialized by this point */
1809 /* spice must initialize before audio as it changes the default auiodev */
1810 /* spice must initialize before chardevs (for spicevmc and spiceport) */
1813 qemu_opts_foreach(qemu_find_opts("chardev"),
1814 chardev_init_func, NULL, &error_fatal);
1816 #ifdef CONFIG_VIRTFS
1817 qemu_opts_foreach(qemu_find_opts("fsdev"),
1818 fsdev_init_func, NULL, &error_fatal);
1822 * Note: we need to create audio and block backends before
1823 * machine_set_property(), so machine properties can refer to
1826 configure_blockdev(&bdo_queue, machine_class, snapshot);
1827 audio_init_audiodevs();
1832 * The remainder of object creation happens after the
1833 * creation of chardev, fsdev, net clients and device data types.
1835 static bool object_create_late(const char *type, QemuOpts *opts)
1837 return !object_create_early(type, opts);
1840 static void qemu_create_late_backends(void)
1843 qtest_server_init(qtest_chrdev, qtest_log, &error_fatal);
1846 net_init_clients(&error_fatal);
1848 qemu_opts_foreach(qemu_find_opts("object"),
1849 user_creatable_add_opts_foreach,
1850 object_create_late, &error_fatal);
1852 if (tpm_init() < 0) {
1856 qemu_opts_foreach(qemu_find_opts("mon"),
1857 mon_init_func, NULL, &error_fatal);
1859 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
1861 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
1863 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
1866 /* now chardevs have been created we may have semihosting to connect */
1867 qemu_semihosting_connect_chardevs();
1868 qemu_semihosting_console_init();
1871 static bool have_custom_ram_size(void)
1873 QemuOpts *opts = qemu_find_opts_singleton("memory");
1874 return !!qemu_opt_get_size(opts, "size", 0);
1877 static void qemu_resolve_machine_memdev(void)
1879 if (current_machine->ram_memdev_id) {
1881 ram_addr_t backend_size;
1883 backend = object_resolve_path_type(current_machine->ram_memdev_id,
1884 TYPE_MEMORY_BACKEND, NULL);
1886 error_report("Memory backend '%s' not found",
1887 current_machine->ram_memdev_id);
1890 backend_size = object_property_get_uint(backend, "size", &error_abort);
1891 if (have_custom_ram_size() && backend_size != ram_size) {
1892 error_report("Size specified by -m option must match size of "
1893 "explicitly specified 'memory-backend' property");
1897 error_report("'-mem-path' can't be used together with"
1898 "'-machine memory-backend'");
1901 ram_size = backend_size;
1904 if (!xen_enabled()) {
1905 /* On 32-bit hosts, QEMU is limited by virtual address space */
1906 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
1907 error_report("at most 2047 MB RAM can be simulated");
1913 static void set_memory_options(MachineClass *mc)
1916 const char *mem_str;
1917 const ram_addr_t default_ram_size = mc->default_ram_size;
1918 QemuOpts *opts = qemu_find_opts_singleton("memory");
1921 loc_push_none(&loc);
1922 qemu_opts_loc_restore(opts);
1925 mem_str = qemu_opt_get(opts, "size");
1928 error_report("missing 'size' option value");
1932 sz = qemu_opt_get_size(opts, "size", ram_size);
1934 /* Fix up legacy suffix-less format */
1935 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
1936 uint64_t overflow_check = sz;
1939 if (sz / MiB != overflow_check) {
1940 error_report("too large 'size' option value");
1946 /* backward compatibility behaviour for case "-m 0" */
1948 sz = default_ram_size;
1951 sz = QEMU_ALIGN_UP(sz, 8192);
1952 if (mc->fixup_ram_size) {
1953 sz = mc->fixup_ram_size(sz);
1956 if (ram_size != sz) {
1957 error_report("ram size too large");
1961 /* store value for the future use */
1962 qemu_opt_set_number(opts, "size", ram_size, &error_abort);
1963 maxram_size = ram_size;
1965 if (qemu_opt_get(opts, "maxmem")) {
1968 sz = qemu_opt_get_size(opts, "maxmem", 0);
1969 slots = qemu_opt_get_number(opts, "slots", 0);
1970 if (sz < ram_size) {
1971 error_report("invalid value of -m option maxmem: "
1972 "maximum memory size (0x%" PRIx64 ") must be at least "
1973 "the initial memory size (0x" RAM_ADDR_FMT ")",
1976 } else if (slots && sz == ram_size) {
1977 error_report("invalid value of -m option maxmem: "
1978 "memory slots were specified but maximum memory size "
1979 "(0x%" PRIx64 ") is equal to the initial memory size "
1980 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
1986 } else if (qemu_opt_get(opts, "slots")) {
1987 error_report("invalid -m option value: missing 'maxmem' option");
1994 static void qemu_create_machine(MachineClass *machine_class)
1996 object_set_machine_compat_props(machine_class->compat_props);
1998 set_memory_options(machine_class);
2000 current_machine = MACHINE(object_new_with_class(OBJECT_CLASS(machine_class)));
2001 if (machine_help_func(qemu_get_machine_opts(), current_machine)) {
2004 object_property_add_child(object_get_root(), "machine",
2005 OBJECT(current_machine));
2006 object_property_add_child(container_get(OBJECT(current_machine),
2008 "sysbus", OBJECT(sysbus_get_default()));
2010 if (machine_class->minimum_page_bits) {
2011 if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
2012 /* This would be a board error: specifying a minimum smaller than
2013 * a target's compile-time fixed setting.
2015 g_assert_not_reached();
2019 cpu_exec_init_all();
2022 if (machine_class->hw_version) {
2023 qemu_set_hw_version(machine_class->hw_version);
2026 machine_smp_parse(current_machine,
2027 qemu_opts_find(qemu_find_opts("smp-opts"), NULL), &error_fatal);
2030 * Get the default machine options from the machine if it is not already
2031 * specified either by the configuration file or by the command line.
2033 if (machine_class->default_machine_opts) {
2034 qemu_opts_set_defaults(qemu_find_opts("machine"),
2035 machine_class->default_machine_opts, 0);
2039 static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
2043 g = g_malloc0(sizeof(*g));
2044 g->driver = qemu_opt_get(opts, "driver");
2045 g->property = qemu_opt_get(opts, "property");
2046 g->value = qemu_opt_get(opts, "value");
2047 qdev_prop_register_global(g);
2051 static int qemu_read_default_config_file(void)
2054 g_autofree char *file = get_relocated_path(CONFIG_QEMU_CONFDIR "/qemu.conf");
2056 ret = qemu_read_config_file(file);
2057 if (ret < 0 && ret != -ENOENT) {
2064 static int qemu_set_option(const char *str)
2066 Error *local_err = NULL;
2067 char group[64], id[64], arg[64];
2072 rc = sscanf(str, "%63[^.].%63[^.].%63[^=]%n", group, id, arg, &offset);
2073 if (rc < 3 || str[offset] != '=') {
2074 error_report("can't parse: \"%s\"", str);
2078 list = qemu_find_opts(group);
2083 opts = qemu_opts_find(list, id);
2085 error_report("there is no %s \"%s\" defined",
2090 if (!qemu_opt_set(opts, arg, str + offset + 1, &local_err)) {
2091 error_report_err(local_err);
2097 static void user_register_global_props(void)
2099 qemu_opts_foreach(qemu_find_opts("global"),
2100 global_init_func, NULL, NULL);
2103 static int do_configure_icount(void *opaque, QemuOpts *opts, Error **errp)
2105 icount_configure(opts, errp);
2109 static int accelerator_set_property(void *opaque,
2110 const char *name, const char *value,
2113 return object_parse_property_opt(opaque, name, value, "accel", errp);
2116 static int do_configure_accelerator(void *opaque, QemuOpts *opts, Error **errp)
2118 bool *p_init_failed = opaque;
2119 const char *acc = qemu_opt_get(opts, "accel");
2120 AccelClass *ac = accel_find(acc);
2123 bool qtest_with_kvm;
2125 qtest_with_kvm = g_str_equal(acc, "kvm") && qtest_chrdev != NULL;
2128 *p_init_failed = true;
2129 if (!qtest_with_kvm) {
2130 error_report("invalid accelerator %s", acc);
2134 accel = ACCEL(object_new_with_class(OBJECT_CLASS(ac)));
2135 object_apply_compat_props(OBJECT(accel));
2136 qemu_opt_foreach(opts, accelerator_set_property,
2140 ret = accel_init_machine(accel, current_machine);
2142 *p_init_failed = true;
2143 if (!qtest_with_kvm || ret != -ENOENT) {
2144 error_report("failed to initialize %s: %s", acc, strerror(-ret));
2152 static void configure_accelerators(const char *progname)
2155 bool init_failed = false;
2157 qemu_opts_foreach(qemu_find_opts("icount"),
2158 do_configure_icount, NULL, &error_fatal);
2160 accel = qemu_opt_get(qemu_get_machine_opts(), "accel");
2161 if (QTAILQ_EMPTY(&qemu_accel_opts.head)) {
2162 char **accel_list, **tmp;
2164 if (accel == NULL) {
2165 /* Select the default accelerator */
2166 bool have_tcg = accel_find("tcg");
2167 bool have_kvm = accel_find("kvm");
2169 if (have_tcg && have_kvm) {
2170 if (g_str_has_suffix(progname, "kvm")) {
2171 /* If the program name ends with "kvm", we prefer KVM */
2176 } else if (have_kvm) {
2178 } else if (have_tcg) {
2181 error_report("No accelerator selected and"
2182 " no default accelerator available");
2186 accel_list = g_strsplit(accel, ":", 0);
2188 for (tmp = accel_list; *tmp; tmp++) {
2190 * Filter invalid accelerators here, to prevent obscenities
2191 * such as "-machine accel=tcg,,thread=single".
2193 if (accel_find(*tmp)) {
2194 qemu_opts_parse_noisily(qemu_find_opts("accel"), *tmp, true);
2197 error_report("invalid accelerator %s", *tmp);
2200 g_strfreev(accel_list);
2202 if (accel != NULL) {
2203 error_report("The -accel and \"-machine accel=\" options are incompatible");
2208 if (!qemu_opts_foreach(qemu_find_opts("accel"),
2209 do_configure_accelerator, &init_failed, &error_fatal)) {
2211 error_report("no accelerator found");
2216 if (init_failed && !qtest_chrdev) {
2217 AccelClass *ac = ACCEL_GET_CLASS(current_accel());
2218 error_report("falling back to %s", ac->name);
2221 if (icount_enabled() && !tcg_enabled()) {
2222 error_report("-icount is not allowed with hardware virtualization");
2227 static void create_default_memdev(MachineState *ms, const char *path)
2230 MachineClass *mc = MACHINE_GET_CLASS(ms);
2232 obj = object_new(path ? TYPE_MEMORY_BACKEND_FILE : TYPE_MEMORY_BACKEND_RAM);
2234 object_property_set_str(obj, "mem-path", path, &error_fatal);
2236 object_property_set_int(obj, "size", ms->ram_size, &error_fatal);
2237 object_property_add_child(object_get_objects_root(), mc->default_ram_id,
2239 /* Ensure backend's memory region name is equal to mc->default_ram_id */
2240 object_property_set_bool(obj, "x-use-canonical-path-for-ramblock-id",
2241 false, &error_fatal);
2242 user_creatable_complete(USER_CREATABLE(obj), &error_fatal);
2244 object_property_set_str(OBJECT(ms), "memory-backend", mc->default_ram_id,
2248 static void qemu_validate_options(void)
2250 QemuOpts *machine_opts = qemu_get_machine_opts();
2251 const char *kernel_filename = qemu_opt_get(machine_opts, "kernel");
2252 const char *initrd_filename = qemu_opt_get(machine_opts, "initrd");
2253 const char *kernel_cmdline = qemu_opt_get(machine_opts, "append");
2255 if (kernel_filename == NULL) {
2256 if (kernel_cmdline != NULL) {
2257 error_report("-append only allowed with -kernel option");
2261 if (initrd_filename != NULL) {
2262 error_report("-initrd only allowed with -kernel option");
2267 if (loadvm && preconfig_requested) {
2268 error_report("'preconfig' and 'loadvm' options are "
2269 "mutually exclusive");
2272 if (incoming && preconfig_requested && strcmp(incoming, "defer") != 0) {
2273 error_report("'preconfig' supports '-incoming defer' only");
2277 #ifdef CONFIG_CURSES
2278 if (is_daemonized() && dpy.type == DISPLAY_TYPE_CURSES) {
2279 error_report("curses display cannot be used with -daemonize");
2285 static void qemu_process_sugar_options(void)
2290 val = g_strdup_printf("%d",
2291 (uint32_t) qemu_opt_get_number(qemu_find_opts_singleton("smp-opts"), "cpus", 1));
2292 object_register_sugar_prop("memory-backend", "prealloc-threads", val);
2294 object_register_sugar_prop("memory-backend", "prealloc", "on");
2298 int i = select_watchdog(watchdog);
2300 exit (i == 1 ? 1 : 0);
2304 static void qemu_process_early_options(void)
2306 #ifdef CONFIG_SECCOMP
2307 QemuOptsList *olist = qemu_find_opts_err("sandbox", NULL);
2309 qemu_opts_foreach(olist, parse_sandbox, NULL, &error_fatal);
2313 qemu_opts_foreach(qemu_find_opts("name"),
2314 parse_name, NULL, &error_fatal);
2317 qemu_opts_foreach(qemu_find_opts("add-fd"),
2318 parse_add_fd, NULL, &error_fatal);
2320 qemu_opts_foreach(qemu_find_opts("add-fd"),
2321 cleanup_add_fd, NULL, &error_fatal);
2324 if (!trace_init_backends()) {
2329 /* Open the logfile at this point and set the log mask if necessary. */
2330 qemu_set_log_filename(log_file, &error_fatal);
2333 mask = qemu_str_to_log_mask(log_mask);
2335 qemu_print_log_usage(stdout);
2343 qemu_add_default_firmwarepath();
2346 static void qemu_process_help_options(void)
2349 * Check for -cpu help and -device help before we call select_machine(),
2350 * which will return an error if the architecture has no default machine
2351 * type and the user did not specify one, so that the user doesn't need
2352 * to say '-cpu help -machine something'.
2354 if (cpu_option && is_help_option(cpu_option)) {
2355 list_cpus(cpu_option);
2359 if (qemu_opts_foreach(qemu_find_opts("device"),
2360 device_help_func, NULL, NULL)) {
2364 /* -L help lists the data directories and exits. */
2365 if (list_data_dirs) {
2366 qemu_list_data_dirs();
2371 static void qemu_maybe_daemonize(const char *pid_file)
2376 rcu_disable_atfork();
2378 if (pid_file && !qemu_write_pidfile(pid_file, &err)) {
2379 error_reportf_err(err, "cannot create PID file: ");
2383 qemu_unlink_pidfile_notifier.notify = qemu_unlink_pidfile;
2384 qemu_add_exit_notifier(&qemu_unlink_pidfile_notifier);
2387 static void qemu_init_displays(void)
2391 /* init local displays */
2392 ds = init_displaystate();
2393 qemu_display_init(ds, &dpy);
2395 /* must be after terminal init, SDL library changes signal handlers */
2396 os_setup_signal_handling();
2398 /* init remote displays */
2400 qemu_opts_foreach(qemu_find_opts("vnc"),
2401 vnc_init_func, NULL, &error_fatal);
2405 qemu_spice.display_init();
2409 static void qemu_init_board(void)
2411 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
2413 if (machine_class->default_ram_id && current_machine->ram_size &&
2414 numa_uses_legacy_mem() && !current_machine->ram_memdev_id) {
2415 create_default_memdev(current_machine, mem_path);
2418 /* process plugin before CPUs are created, but once -smp has been parsed */
2419 if (qemu_plugin_load_list(&plugin_list)) {
2423 /* From here on we enter MACHINE_PHASE_INITIALIZED. */
2424 machine_run_board_init(current_machine);
2427 * TODO To drop support for deprecated bogus if=..., move
2428 * drive_check_orphaned() here, replacing this call. Also drop
2429 * its deprecation warning, along with DriveInfo member
2430 * @claimed_by_board.
2432 drive_mark_claimed_by_board();
2436 if (hax_enabled()) {
2437 /* FIXME: why isn't cpu_synchronize_all_post_init enough? */
2442 static void qemu_create_cli_devices(void)
2446 qemu_opts_foreach(qemu_find_opts("fw_cfg"),
2447 parse_fw_cfg, fw_cfg_find(), &error_fatal);
2449 /* init USB devices */
2450 if (machine_usb(current_machine)) {
2451 if (foreach_device_config(DEV_USB, usb_parse) < 0)
2455 /* init generic devices */
2456 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
2457 qemu_opts_foreach(qemu_find_opts("device"),
2458 device_init_func, NULL, &error_fatal);
2459 rom_reset_order_override();
2462 static void qemu_machine_creation_done(void)
2464 /* Did we create any drives that we failed to create a device for? */
2465 drive_check_orphaned();
2467 /* Don't warn about the default network setup that you get if
2468 * no command line -net or -netdev options are specified. There
2469 * are two cases that we would otherwise complain about:
2470 * (1) board doesn't support a NIC but the implicit "-net nic"
2472 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
2473 * sets up a nic that isn't connected to anything.
2475 if (!default_net && (!qtest_enabled() || has_defaults)) {
2476 net_check_clients();
2479 qdev_prop_check_globals();
2481 qdev_machine_creation_done();
2483 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
2488 void qmp_x_exit_preconfig(Error **errp)
2490 if (phase_check(PHASE_MACHINE_INITIALIZED)) {
2491 error_setg(errp, "The command is permitted only before machine initialization");
2496 qemu_create_cli_devices();
2497 qemu_machine_creation_done();
2500 Error *local_err = NULL;
2501 if (load_snapshot(loadvm, &local_err) < 0) {
2502 error_report_err(local_err);
2507 if (replay_mode != REPLAY_MODE_NONE) {
2508 replay_vmstate_init();
2512 Error *local_err = NULL;
2513 if (strcmp(incoming, "defer") != 0) {
2514 qmp_migrate_incoming(incoming, &local_err);
2516 error_reportf_err(local_err, "-incoming %s: ", incoming);
2520 } else if (autostart) {
2525 void qemu_init(int argc, char **argv, char **envp)
2528 QemuOpts *icount_opts = NULL, *accel_opts = NULL;
2529 QemuOptsList *olist;
2532 MachineClass *machine_class;
2533 bool userconfig = true;
2534 FILE *vmstate_dump_file = NULL;
2536 qemu_add_opts(&qemu_drive_opts);
2537 qemu_add_drive_opts(&qemu_legacy_drive_opts);
2538 qemu_add_drive_opts(&qemu_common_drive_opts);
2539 qemu_add_drive_opts(&qemu_drive_opts);
2540 qemu_add_drive_opts(&bdrv_runtime_opts);
2541 qemu_add_opts(&qemu_chardev_opts);
2542 qemu_add_opts(&qemu_device_opts);
2543 qemu_add_opts(&qemu_netdev_opts);
2544 qemu_add_opts(&qemu_nic_opts);
2545 qemu_add_opts(&qemu_net_opts);
2546 qemu_add_opts(&qemu_rtc_opts);
2547 qemu_add_opts(&qemu_global_opts);
2548 qemu_add_opts(&qemu_mon_opts);
2549 qemu_add_opts(&qemu_trace_opts);
2550 qemu_plugin_add_opts();
2551 qemu_add_opts(&qemu_option_rom_opts);
2552 qemu_add_opts(&qemu_machine_opts);
2553 qemu_add_opts(&qemu_accel_opts);
2554 qemu_add_opts(&qemu_mem_opts);
2555 qemu_add_opts(&qemu_smp_opts);
2556 qemu_add_opts(&qemu_boot_opts);
2557 qemu_add_opts(&qemu_add_fd_opts);
2558 qemu_add_opts(&qemu_object_opts);
2559 qemu_add_opts(&qemu_tpmdev_opts);
2560 qemu_add_opts(&qemu_realtime_opts);
2561 qemu_add_opts(&qemu_overcommit_opts);
2562 qemu_add_opts(&qemu_msg_opts);
2563 qemu_add_opts(&qemu_name_opts);
2564 qemu_add_opts(&qemu_numa_opts);
2565 qemu_add_opts(&qemu_icount_opts);
2566 qemu_add_opts(&qemu_semihosting_config_opts);
2567 qemu_add_opts(&qemu_fw_cfg_opts);
2568 module_call_init(MODULE_INIT_OPTS);
2570 error_init(argv[0]);
2571 qemu_init_exec_dir(argv[0]);
2573 qemu_init_subsystems();
2575 /* first pass of option parsing */
2577 while (optind < argc) {
2578 if (argv[optind][0] != '-') {
2582 const QEMUOption *popt;
2584 popt = lookup_opt(argc, argv, &optarg, &optind);
2585 switch (popt->index) {
2586 case QEMU_OPTION_nouserconfig:
2594 if (qemu_read_default_config_file() < 0) {
2599 /* second pass of option parsing */
2604 if (argv[optind][0] != '-') {
2605 loc_set_cmdline(argv, optind, 1);
2606 drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2608 const QEMUOption *popt;
2610 popt = lookup_opt(argc, argv, &optarg, &optind);
2611 if (!(popt->arch_mask & arch_type)) {
2612 error_report("Option not supported for this target");
2615 switch(popt->index) {
2616 case QEMU_OPTION_cpu:
2617 /* hw initialization will check this */
2618 cpu_option = optarg;
2620 case QEMU_OPTION_hda:
2621 case QEMU_OPTION_hdb:
2622 case QEMU_OPTION_hdc:
2623 case QEMU_OPTION_hdd:
2624 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2627 case QEMU_OPTION_blockdev:
2630 BlockdevOptionsQueueEntry *bdo;
2632 v = qobject_input_visitor_new_str(optarg, "driver",
2635 bdo = g_new(BlockdevOptionsQueueEntry, 1);
2636 visit_type_BlockdevOptions(v, NULL, &bdo->bdo,
2639 loc_save(&bdo->loc);
2640 QSIMPLEQ_INSERT_TAIL(&bdo_queue, bdo, entry);
2643 case QEMU_OPTION_drive:
2644 if (drive_def(optarg) == NULL) {
2648 case QEMU_OPTION_set:
2649 if (qemu_set_option(optarg) != 0)
2652 case QEMU_OPTION_global:
2653 if (qemu_global_option(optarg) != 0)
2656 case QEMU_OPTION_mtdblock:
2657 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2659 case QEMU_OPTION_sd:
2660 drive_add(IF_SD, -1, optarg, SD_OPTS);
2662 case QEMU_OPTION_pflash:
2663 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2665 case QEMU_OPTION_snapshot:
2667 Error *blocker = NULL;
2669 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
2671 replay_add_blocker(blocker);
2674 case QEMU_OPTION_numa:
2675 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
2681 case QEMU_OPTION_display:
2682 parse_display(optarg);
2684 case QEMU_OPTION_nographic:
2685 olist = qemu_find_opts("machine");
2686 qemu_opts_parse_noisily(olist, "graphics=off", false);
2688 dpy.type = DISPLAY_TYPE_NONE;
2690 case QEMU_OPTION_curses:
2691 #ifdef CONFIG_CURSES
2692 dpy.type = DISPLAY_TYPE_CURSES;
2694 error_report("curses or iconv support is disabled");
2698 case QEMU_OPTION_portrait:
2699 graphic_rotate = 90;
2701 case QEMU_OPTION_rotate:
2702 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2703 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2704 graphic_rotate != 180 && graphic_rotate != 270) {
2705 error_report("only 90, 180, 270 deg rotation is available");
2709 case QEMU_OPTION_kernel:
2710 qemu_opts_set(qemu_find_opts("machine"), "kernel", optarg, &error_abort);
2712 case QEMU_OPTION_initrd:
2713 qemu_opts_set(qemu_find_opts("machine"), "initrd", optarg, &error_abort);
2715 case QEMU_OPTION_append:
2716 qemu_opts_set(qemu_find_opts("machine"), "append", optarg, &error_abort);
2718 case QEMU_OPTION_dtb:
2719 qemu_opts_set(qemu_find_opts("machine"), "dtb", optarg, &error_abort);
2721 case QEMU_OPTION_cdrom:
2722 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
2724 case QEMU_OPTION_boot:
2725 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
2731 case QEMU_OPTION_fda:
2732 case QEMU_OPTION_fdb:
2733 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2736 case QEMU_OPTION_no_fd_bootchk:
2739 case QEMU_OPTION_netdev:
2741 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2745 case QEMU_OPTION_nic:
2747 if (net_client_parse(qemu_find_opts("nic"), optarg) == -1) {
2751 case QEMU_OPTION_net:
2753 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2757 #ifdef CONFIG_LIBISCSI
2758 case QEMU_OPTION_iscsi:
2759 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
2766 case QEMU_OPTION_audio_help:
2767 audio_legacy_help();
2770 case QEMU_OPTION_audiodev:
2771 audio_parse_option(optarg);
2773 case QEMU_OPTION_soundhw:
2774 select_soundhw (optarg);
2779 case QEMU_OPTION_version:
2784 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
2791 case QEMU_OPTION_tpmdev:
2792 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
2797 case QEMU_OPTION_mempath:
2800 case QEMU_OPTION_mem_prealloc:
2809 case QEMU_OPTION_DFILTER:
2810 qemu_set_dfilter_ranges(optarg, &error_fatal);
2812 case QEMU_OPTION_seed:
2813 qemu_guest_random_seed_main(optarg, &error_fatal);
2816 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
2818 case QEMU_OPTION_gdb:
2819 add_device_config(DEV_GDB, optarg);
2822 if (is_help_option(optarg)) {
2823 list_data_dirs = true;
2825 qemu_add_data_dir(g_strdup(optarg));
2828 case QEMU_OPTION_bios:
2829 qemu_opts_set(qemu_find_opts("machine"), "firmware", optarg, &error_abort);
2831 case QEMU_OPTION_singlestep:
2838 keyboard_layout = optarg;
2840 case QEMU_OPTION_vga:
2849 w = strtol(p, (char **)&p, 10);
2852 error_report("invalid resolution or depth");
2858 h = strtol(p, (char **)&p, 10);
2863 depth = strtol(p, (char **)&p, 10);
2864 if (depth != 1 && depth != 2 && depth != 4 &&
2865 depth != 8 && depth != 15 && depth != 16 &&
2866 depth != 24 && depth != 32)
2868 } else if (*p == '\0') {
2869 depth = graphic_depth;
2876 graphic_depth = depth;
2879 case QEMU_OPTION_echr:
2882 term_escape_char = strtol(optarg, &r, 0);
2884 printf("Bad argument to echr\n");
2887 case QEMU_OPTION_monitor:
2888 default_monitor = 0;
2889 if (strncmp(optarg, "none", 4)) {
2890 monitor_parse(optarg, "readline", false);
2893 case QEMU_OPTION_qmp:
2894 monitor_parse(optarg, "control", false);
2895 default_monitor = 0;
2897 case QEMU_OPTION_qmp_pretty:
2898 monitor_parse(optarg, "control", true);
2899 default_monitor = 0;
2901 case QEMU_OPTION_mon:
2902 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
2907 default_monitor = 0;
2909 case QEMU_OPTION_chardev:
2910 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
2916 case QEMU_OPTION_fsdev:
2917 olist = qemu_find_opts("fsdev");
2919 error_report("fsdev support is disabled");
2922 opts = qemu_opts_parse_noisily(olist, optarg, true);
2927 case QEMU_OPTION_virtfs: {
2930 const char *writeout, *sock_fd, *socket, *path, *security_model,
2933 olist = qemu_find_opts("virtfs");
2935 error_report("virtfs support is disabled");
2938 opts = qemu_opts_parse_noisily(olist, optarg, true);
2943 if (qemu_opt_get(opts, "fsdriver") == NULL ||
2944 qemu_opt_get(opts, "mount_tag") == NULL) {
2945 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
2948 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
2949 qemu_opts_id(opts) ?:
2950 qemu_opt_get(opts, "mount_tag"),
2953 error_report("duplicate or invalid fsdev id: %s",
2954 qemu_opt_get(opts, "mount_tag"));
2958 writeout = qemu_opt_get(opts, "writeout");
2960 #ifdef CONFIG_SYNC_FILE_RANGE
2961 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
2963 error_report("writeout=immediate not supported "
2964 "on this platform");
2968 qemu_opt_set(fsdev, "fsdriver",
2969 qemu_opt_get(opts, "fsdriver"), &error_abort);
2970 path = qemu_opt_get(opts, "path");
2972 qemu_opt_set(fsdev, "path", path, &error_abort);
2974 security_model = qemu_opt_get(opts, "security_model");
2975 if (security_model) {
2976 qemu_opt_set(fsdev, "security_model", security_model,
2979 socket = qemu_opt_get(opts, "socket");
2981 qemu_opt_set(fsdev, "socket", socket, &error_abort);
2983 sock_fd = qemu_opt_get(opts, "sock_fd");
2985 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
2988 qemu_opt_set_bool(fsdev, "readonly",
2989 qemu_opt_get_bool(opts, "readonly", 0),
2991 multidevs = qemu_opt_get(opts, "multidevs");
2993 qemu_opt_set(fsdev, "multidevs", multidevs, &error_abort);
2995 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2997 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
2998 qemu_opt_set(device, "fsdev",
2999 qemu_opts_id(fsdev), &error_abort);
3000 qemu_opt_set(device, "mount_tag",
3001 qemu_opt_get(opts, "mount_tag"), &error_abort);
3004 case QEMU_OPTION_serial:
3005 add_device_config(DEV_SERIAL, optarg);
3007 if (strncmp(optarg, "mon:", 4) == 0) {
3008 default_monitor = 0;
3011 case QEMU_OPTION_watchdog:
3013 error_report("only one watchdog option may be given");
3018 case QEMU_OPTION_watchdog_action:
3019 if (select_watchdog_action(optarg) == -1) {
3020 error_report("unknown -watchdog-action parameter");
3024 case QEMU_OPTION_parallel:
3025 add_device_config(DEV_PARALLEL, optarg);
3026 default_parallel = 0;
3027 if (strncmp(optarg, "mon:", 4) == 0) {
3028 default_monitor = 0;
3031 case QEMU_OPTION_debugcon:
3032 add_device_config(DEV_DEBUGCON, optarg);
3034 case QEMU_OPTION_loadvm:
3037 case QEMU_OPTION_full_screen:
3038 dpy.has_full_screen = true;
3039 dpy.full_screen = true;
3041 case QEMU_OPTION_alt_grab:
3044 case QEMU_OPTION_ctrl_grab:
3047 case QEMU_OPTION_no_quit:
3048 dpy.has_window_close = true;
3049 dpy.window_close = false;
3051 case QEMU_OPTION_sdl:
3053 dpy.type = DISPLAY_TYPE_SDL;
3056 error_report("SDL support is disabled");
3059 case QEMU_OPTION_pidfile:
3062 case QEMU_OPTION_win2k_hack:
3063 win2k_install_hack = 1;
3065 case QEMU_OPTION_acpitable:
3066 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3071 acpi_table_add(opts, &error_fatal);
3073 case QEMU_OPTION_smbios:
3074 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3079 smbios_entry_add(opts, &error_fatal);
3081 case QEMU_OPTION_fwcfg:
3082 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3088 case QEMU_OPTION_preconfig:
3089 preconfig_requested = true;
3091 case QEMU_OPTION_enable_kvm:
3092 olist = qemu_find_opts("machine");
3093 qemu_opts_parse_noisily(olist, "accel=kvm", false);
3096 case QEMU_OPTION_machine:
3097 olist = qemu_find_opts("machine");
3098 opts = qemu_opts_parse_noisily(olist, optarg, true);
3103 case QEMU_OPTION_accel:
3104 accel_opts = qemu_opts_parse_noisily(qemu_find_opts("accel"),
3106 optarg = qemu_opt_get(accel_opts, "accel");
3107 if (!optarg || is_help_option(optarg)) {
3108 printf("Accelerators supported in QEMU binary:\n");
3109 GSList *el, *accel_list = object_class_get_list(TYPE_ACCEL,
3111 for (el = accel_list; el; el = el->next) {
3112 gchar *typename = g_strdup(object_class_get_name(
3113 OBJECT_CLASS(el->data)));
3114 /* omit qtest which is used for tests only */
3115 if (g_strcmp0(typename, ACCEL_CLASS_NAME("qtest")) &&
3116 g_str_has_suffix(typename, ACCEL_CLASS_SUFFIX)) {
3117 gchar **optname = g_strsplit(typename,
3118 ACCEL_CLASS_SUFFIX, 0);
3119 printf("%s\n", optname[0]);
3120 g_strfreev(optname);
3124 g_slist_free(accel_list);
3128 case QEMU_OPTION_usb:
3129 olist = qemu_find_opts("machine");
3130 qemu_opts_parse_noisily(olist, "usb=on", false);
3132 case QEMU_OPTION_usbdevice:
3133 error_report("'-usbdevice' is deprecated, please use "
3134 "'-device usb-...' instead");
3135 olist = qemu_find_opts("machine");
3136 qemu_opts_parse_noisily(olist, "usb=on", false);
3137 add_device_config(DEV_USB, optarg);
3139 case QEMU_OPTION_device:
3140 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3145 case QEMU_OPTION_smp:
3146 if (!qemu_opts_parse_noisily(qemu_find_opts("smp-opts"),
3151 case QEMU_OPTION_vnc:
3152 vnc_parse(optarg, &error_fatal);
3154 case QEMU_OPTION_no_acpi:
3155 olist = qemu_find_opts("machine");
3156 qemu_opts_parse_noisily(olist, "acpi=off", false);
3158 case QEMU_OPTION_no_hpet:
3159 olist = qemu_find_opts("machine");
3160 qemu_opts_parse_noisily(olist, "hpet=off", false);
3162 case QEMU_OPTION_no_reboot:
3165 case QEMU_OPTION_no_shutdown:
3168 case QEMU_OPTION_show_cursor:
3169 warn_report("The -show-cursor option is deprecated. Please "
3170 "add show-cursor=on to your -display options.");
3171 warn_report("When using the default display you can use "
3172 "-display default,show-cursor=on");
3173 dpy.has_show_cursor = true;
3174 dpy.show_cursor = true;
3176 case QEMU_OPTION_uuid:
3177 if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
3178 error_report("failed to parse UUID string: wrong format");
3181 qemu_uuid_set = true;
3183 case QEMU_OPTION_option_rom:
3184 if (nb_option_roms >= MAX_OPTION_ROMS) {
3185 error_report("too many option ROMs");
3188 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3193 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3194 option_rom[nb_option_roms].bootindex =
3195 qemu_opt_get_number(opts, "bootindex", -1);
3196 if (!option_rom[nb_option_roms].name) {
3197 error_report("Option ROM file is not specified");
3202 case QEMU_OPTION_semihosting:
3203 qemu_semihosting_enable();
3205 case QEMU_OPTION_semihosting_config:
3206 if (qemu_semihosting_config_options(optarg) != 0) {
3210 case QEMU_OPTION_name:
3211 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3216 /* Capture guest name if -msg guest-name is used later */
3217 error_guest_name = qemu_opt_get(opts, "guest");
3219 case QEMU_OPTION_prom_env:
3220 if (nb_prom_envs >= MAX_PROM_ENVS) {
3221 error_report("too many prom variables");
3224 prom_envs[nb_prom_envs] = optarg;
3227 case QEMU_OPTION_old_param:
3230 case QEMU_OPTION_rtc:
3231 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3237 case QEMU_OPTION_tb_size:
3239 error_report("TCG is disabled");
3242 warn_report("The -tb-size option is deprecated, use -accel tcg,tb-size instead");
3243 object_register_sugar_prop(ACCEL_CLASS_NAME("tcg"), "tb-size", optarg);
3245 case QEMU_OPTION_icount:
3246 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3252 case QEMU_OPTION_incoming:
3254 runstate_set(RUN_STATE_INMIGRATE);
3258 case QEMU_OPTION_only_migratable:
3259 only_migratable = 1;
3261 case QEMU_OPTION_nodefaults:
3264 case QEMU_OPTION_xen_domid:
3265 if (!(xen_available())) {
3266 error_report("Option not supported for this target");
3269 xen_domid = atoi(optarg);
3271 case QEMU_OPTION_xen_attach:
3272 if (!(xen_available())) {
3273 error_report("Option not supported for this target");
3276 xen_mode = XEN_ATTACH;
3278 case QEMU_OPTION_xen_domid_restrict:
3279 if (!(xen_available())) {
3280 error_report("Option not supported for this target");
3283 xen_domid_restrict = true;
3285 case QEMU_OPTION_trace:
3286 trace_opt_parse(optarg);
3288 case QEMU_OPTION_plugin:
3289 qemu_plugin_opt_parse(optarg, &plugin_list);
3291 case QEMU_OPTION_readconfig:
3293 int ret = qemu_read_config_file(optarg);
3295 error_report("read config %s: %s", optarg,
3301 case QEMU_OPTION_spice:
3302 olist = qemu_find_opts_err("spice", NULL);
3304 ui_module_load_one("spice-core");
3305 olist = qemu_find_opts("spice");
3308 error_report("spice support is disabled");
3311 opts = qemu_opts_parse_noisily(olist, optarg, false);
3317 case QEMU_OPTION_writeconfig:
3320 if (strcmp(optarg, "-") == 0) {
3323 fp = fopen(optarg, "w");
3325 error_report("open %s: %s", optarg,
3330 qemu_config_write(fp);
3336 case QEMU_OPTION_qtest:
3337 qtest_chrdev = optarg;
3339 case QEMU_OPTION_qtest_log:
3342 case QEMU_OPTION_sandbox:
3343 olist = qemu_find_opts("sandbox");
3345 #ifndef CONFIG_SECCOMP
3346 error_report("-sandbox support is not enabled "
3347 "in this QEMU binary");
3352 opts = qemu_opts_parse_noisily(olist, optarg, true);
3357 case QEMU_OPTION_add_fd:
3359 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
3365 error_report("File descriptor passing is disabled on this "
3370 case QEMU_OPTION_object:
3371 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
3377 case QEMU_OPTION_realtime:
3378 warn_report("'-realtime mlock=...' is deprecated, please use "
3379 "'-overcommit mem-lock=...' instead");
3380 opts = qemu_opts_parse_noisily(qemu_find_opts("realtime"),
3385 /* Don't override the -overcommit option if set */
3386 enable_mlock = enable_mlock ||
3387 qemu_opt_get_bool(opts, "mlock", true);
3389 case QEMU_OPTION_overcommit:
3390 opts = qemu_opts_parse_noisily(qemu_find_opts("overcommit"),
3395 /* Don't override the -realtime option if set */
3396 enable_mlock = enable_mlock ||
3397 qemu_opt_get_bool(opts, "mem-lock", false);
3398 enable_cpu_pm = qemu_opt_get_bool(opts, "cpu-pm", false);
3400 case QEMU_OPTION_msg:
3401 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
3406 configure_msg(opts);
3408 case QEMU_OPTION_dump_vmstate:
3409 if (vmstate_dump_file) {
3410 error_report("only one '-dump-vmstate' "
3411 "option may be given");
3414 vmstate_dump_file = fopen(optarg, "w");
3415 if (vmstate_dump_file == NULL) {
3416 error_report("open %s: %s", optarg, strerror(errno));
3420 case QEMU_OPTION_enable_sync_profile:
3423 case QEMU_OPTION_nouserconfig:
3424 /* Nothing to be parsed here. Especially, do not error out below. */
3427 if (os_parse_cmd_args(popt->index, optarg)) {
3428 error_report("Option not supported in this build");
3435 * Clear error location left behind by the loop.
3436 * Best done right after the loop. Do not insert code here!
3440 qemu_validate_options();
3441 qemu_process_sugar_options();
3444 * These options affect everything else and should be processed
3445 * before daemonizing.
3447 qemu_process_early_options();
3449 qemu_process_help_options();
3450 qemu_maybe_daemonize(pid_file);
3452 qemu_init_main_loop(&error_fatal);
3455 user_register_global_props();
3456 replay_configure(icount_opts);
3458 configure_rtc(qemu_find_opts_singleton("rtc"));
3460 qemu_create_machine(select_machine());
3464 qemu_disable_default_devices();
3465 qemu_create_default_devices();
3466 qemu_create_early_backends();
3468 qemu_apply_machine_options();
3469 phase_advance(PHASE_MACHINE_CREATED);
3472 * Note: uses machine properties such as kernel-irqchip, must run
3473 * after machine_set_property().
3475 configure_accelerators(argv[0]);
3476 phase_advance(PHASE_ACCEL_CREATED);
3479 * Beware, QOM objects created before this point miss global and
3480 * compat properties.
3482 * Global properties get set up by qdev_prop_register_global(),
3483 * called from user_register_global_props(), and certain option
3484 * desugaring. Also in CPU feature desugaring (buried in
3485 * parse_cpu_option()), which happens below this point, but may
3486 * only target the CPU type, which can only be created after
3487 * parse_cpu_option() returned the type.
3489 * Machine compat properties: object_set_machine_compat_props().
3490 * Accelerator compat props: object_set_accelerator_compat_props(),
3491 * called from configure_accelerator().
3494 machine_class = MACHINE_GET_CLASS(current_machine);
3495 if (!qtest_enabled() && machine_class->deprecation_reason) {
3496 error_report("Machine type '%s' is deprecated: %s",
3497 machine_class->name, machine_class->deprecation_reason);
3501 * Note: creates a QOM object, must run only after global and
3502 * compat properties have been set up.
3504 migration_object_init();
3506 qemu_create_late_backends();
3508 /* parse features once if machine provides default cpu_type */
3509 current_machine->cpu_type = machine_class->default_cpu_type;
3511 current_machine->cpu_type = parse_cpu_option(cpu_option);
3514 qemu_resolve_machine_memdev();
3515 parse_numa_opts(current_machine);
3517 if (vmstate_dump_file) {
3519 dump_vmstate_json_to_file(vmstate_dump_file);
3523 qemu_init_displays();
3524 if (!preconfig_requested) {
3525 qmp_x_exit_preconfig(&error_fatal);
3527 accel_setup_post(current_machine);