Merge tag 'qemu-sparc-20211121' of git://github.com/mcayland/qemu into staging
[qemu.git] / softmmu / vl.c
blob1159a64bce4ec0cddf95dde8aa073d5e3b45ea48
1 /*
2 * QEMU System Emulator
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
22 * THE SOFTWARE.
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"
50 #include "hw/usb.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"
60 #include "net/net.h"
61 #include "net/slirp.h"
62 #include "monitor/monitor.h"
63 #include "ui/console.h"
64 #include "ui/input.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"
72 #include "qemu/log.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"
94 #ifdef CONFIG_VIRTFS
95 #include "fsdev/qemu-fsdev.h"
96 #endif
97 #include "sysemu/qtest.h"
99 #include "disas/disas.h"
101 #include "trace.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;
136 Location loc;
137 QSIMPLEQ_ENTRY(BlockdevOptionsQueueEntry) entry;
138 } BlockdevOptionsQueueEntry;
140 typedef QSIMPLEQ_HEAD(, BlockdevOptionsQueueEntry) BlockdevOptionsQueue;
142 typedef struct ObjectOption {
143 ObjectOptions *opts;
144 QTAILQ_ENTRY(ObjectOption) next;
145 } ObjectOption;
147 typedef struct DeviceOption {
148 QDict *opts;
149 Location loc;
150 QTAILQ_ENTRY(DeviceOption) next;
151 } DeviceOption;
153 static const char *cpu_option;
154 static const char *mem_path;
155 static const char *incoming;
156 static const char *loadvm;
157 static const char *accelerators;
158 static QDict *machine_opts_dict;
159 static QTAILQ_HEAD(, ObjectOption) object_opts = QTAILQ_HEAD_INITIALIZER(object_opts);
160 static QTAILQ_HEAD(, DeviceOption) device_opts = QTAILQ_HEAD_INITIALIZER(device_opts);
161 static ram_addr_t maxram_size;
162 static uint64_t ram_slots;
163 static int display_remote;
164 static int snapshot;
165 static bool preconfig_requested;
166 static QemuPluginList plugin_list = QTAILQ_HEAD_INITIALIZER(plugin_list);
167 static BlockdevOptionsQueue bdo_queue = QSIMPLEQ_HEAD_INITIALIZER(bdo_queue);
168 static bool nographic = false;
169 static int mem_prealloc; /* force preallocation of physical target memory */
170 static ram_addr_t ram_size;
171 static const char *vga_model = NULL;
172 static DisplayOptions dpy;
173 static int num_serial_hds;
174 static Chardev **serial_hds;
175 static const char *log_mask;
176 static const char *log_file;
177 static bool list_data_dirs;
178 static const char *watchdog;
179 static const char *qtest_chrdev;
180 static const char *qtest_log;
182 static int has_defaults = 1;
183 static int default_serial = 1;
184 static int default_parallel = 1;
185 static int default_monitor = 1;
186 static int default_floppy = 1;
187 static int default_cdrom = 1;
188 static int default_sdcard = 1;
189 static int default_vga = 1;
190 static int default_net = 1;
192 static struct {
193 const char *driver;
194 int *flag;
195 } default_list[] = {
196 { .driver = "isa-serial", .flag = &default_serial },
197 { .driver = "isa-parallel", .flag = &default_parallel },
198 { .driver = "isa-fdc", .flag = &default_floppy },
199 { .driver = "floppy", .flag = &default_floppy },
200 { .driver = "ide-cd", .flag = &default_cdrom },
201 { .driver = "ide-hd", .flag = &default_cdrom },
202 { .driver = "scsi-cd", .flag = &default_cdrom },
203 { .driver = "scsi-hd", .flag = &default_cdrom },
204 { .driver = "VGA", .flag = &default_vga },
205 { .driver = "isa-vga", .flag = &default_vga },
206 { .driver = "cirrus-vga", .flag = &default_vga },
207 { .driver = "isa-cirrus-vga", .flag = &default_vga },
208 { .driver = "vmware-svga", .flag = &default_vga },
209 { .driver = "qxl-vga", .flag = &default_vga },
210 { .driver = "virtio-vga", .flag = &default_vga },
211 { .driver = "ati-vga", .flag = &default_vga },
212 { .driver = "vhost-user-vga", .flag = &default_vga },
213 { .driver = "virtio-vga-gl", .flag = &default_vga },
216 static QemuOptsList qemu_rtc_opts = {
217 .name = "rtc",
218 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
219 .merge_lists = true,
220 .desc = {
222 .name = "base",
223 .type = QEMU_OPT_STRING,
225 .name = "clock",
226 .type = QEMU_OPT_STRING,
228 .name = "driftfix",
229 .type = QEMU_OPT_STRING,
231 { /* end of list */ }
235 static QemuOptsList qemu_option_rom_opts = {
236 .name = "option-rom",
237 .implied_opt_name = "romfile",
238 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
239 .desc = {
241 .name = "bootindex",
242 .type = QEMU_OPT_NUMBER,
243 }, {
244 .name = "romfile",
245 .type = QEMU_OPT_STRING,
247 { /* end of list */ }
251 static QemuOptsList qemu_accel_opts = {
252 .name = "accel",
253 .implied_opt_name = "accel",
254 .head = QTAILQ_HEAD_INITIALIZER(qemu_accel_opts.head),
255 .desc = {
257 * no elements => accept any
258 * sanity checking will happen later
259 * when setting accelerator properties
265 static QemuOptsList qemu_boot_opts = {
266 .name = "boot-opts",
267 .implied_opt_name = "order",
268 .merge_lists = true,
269 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
270 .desc = {
272 .name = "order",
273 .type = QEMU_OPT_STRING,
274 }, {
275 .name = "once",
276 .type = QEMU_OPT_STRING,
277 }, {
278 .name = "menu",
279 .type = QEMU_OPT_BOOL,
280 }, {
281 .name = "splash",
282 .type = QEMU_OPT_STRING,
283 }, {
284 .name = "splash-time",
285 .type = QEMU_OPT_NUMBER,
286 }, {
287 .name = "reboot-timeout",
288 .type = QEMU_OPT_NUMBER,
289 }, {
290 .name = "strict",
291 .type = QEMU_OPT_BOOL,
293 { /*End of list */ }
297 static QemuOptsList qemu_add_fd_opts = {
298 .name = "add-fd",
299 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
300 .desc = {
302 .name = "fd",
303 .type = QEMU_OPT_NUMBER,
304 .help = "file descriptor of which a duplicate is added to fd set",
306 .name = "set",
307 .type = QEMU_OPT_NUMBER,
308 .help = "ID of the fd set to add fd to",
310 .name = "opaque",
311 .type = QEMU_OPT_STRING,
312 .help = "free-form string used to describe fd",
314 { /* end of list */ }
318 static QemuOptsList qemu_object_opts = {
319 .name = "object",
320 .implied_opt_name = "qom-type",
321 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
322 .desc = {
327 static QemuOptsList qemu_tpmdev_opts = {
328 .name = "tpmdev",
329 .implied_opt_name = "type",
330 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
331 .desc = {
332 /* options are defined in the TPM backends */
333 { /* end of list */ }
337 static QemuOptsList qemu_overcommit_opts = {
338 .name = "overcommit",
339 .head = QTAILQ_HEAD_INITIALIZER(qemu_overcommit_opts.head),
340 .desc = {
342 .name = "mem-lock",
343 .type = QEMU_OPT_BOOL,
346 .name = "cpu-pm",
347 .type = QEMU_OPT_BOOL,
349 { /* end of list */ }
353 static QemuOptsList qemu_msg_opts = {
354 .name = "msg",
355 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
356 .desc = {
358 .name = "timestamp",
359 .type = QEMU_OPT_BOOL,
362 .name = "guest-name",
363 .type = QEMU_OPT_BOOL,
364 .help = "Prepends guest name for error messages but only if "
365 "-name guest is set otherwise option is ignored\n",
367 { /* end of list */ }
371 static QemuOptsList qemu_name_opts = {
372 .name = "name",
373 .implied_opt_name = "guest",
374 .merge_lists = true,
375 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
376 .desc = {
378 .name = "guest",
379 .type = QEMU_OPT_STRING,
380 .help = "Sets the name of the guest.\n"
381 "This name will be displayed in the SDL window caption.\n"
382 "The name will also be used for the VNC server",
383 }, {
384 .name = "process",
385 .type = QEMU_OPT_STRING,
386 .help = "Sets the name of the QEMU process, as shown in top etc",
387 }, {
388 .name = "debug-threads",
389 .type = QEMU_OPT_BOOL,
390 .help = "When enabled, name the individual threads; defaults off.\n"
391 "NOTE: The thread names are for debugging and not a\n"
392 "stable API.",
394 { /* End of list */ }
398 static QemuOptsList qemu_mem_opts = {
399 .name = "memory",
400 .implied_opt_name = "size",
401 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
402 .merge_lists = true,
403 .desc = {
405 .name = "size",
406 .type = QEMU_OPT_SIZE,
409 .name = "slots",
410 .type = QEMU_OPT_NUMBER,
413 .name = "maxmem",
414 .type = QEMU_OPT_SIZE,
416 { /* end of list */ }
420 static QemuOptsList qemu_icount_opts = {
421 .name = "icount",
422 .implied_opt_name = "shift",
423 .merge_lists = true,
424 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
425 .desc = {
427 .name = "shift",
428 .type = QEMU_OPT_STRING,
429 }, {
430 .name = "align",
431 .type = QEMU_OPT_BOOL,
432 }, {
433 .name = "sleep",
434 .type = QEMU_OPT_BOOL,
435 }, {
436 .name = "rr",
437 .type = QEMU_OPT_STRING,
438 }, {
439 .name = "rrfile",
440 .type = QEMU_OPT_STRING,
441 }, {
442 .name = "rrsnapshot",
443 .type = QEMU_OPT_STRING,
445 { /* end of list */ }
449 static QemuOptsList qemu_fw_cfg_opts = {
450 .name = "fw_cfg",
451 .implied_opt_name = "name",
452 .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
453 .desc = {
455 .name = "name",
456 .type = QEMU_OPT_STRING,
457 .help = "Sets the fw_cfg name of the blob to be inserted",
458 }, {
459 .name = "file",
460 .type = QEMU_OPT_STRING,
461 .help = "Sets the name of the file from which "
462 "the fw_cfg blob will be loaded",
463 }, {
464 .name = "string",
465 .type = QEMU_OPT_STRING,
466 .help = "Sets content of the blob to be inserted from a string",
467 }, {
468 .name = "gen_id",
469 .type = QEMU_OPT_STRING,
470 .help = "Sets id of the object generating the fw_cfg blob "
471 "to be inserted",
473 { /* end of list */ }
477 static QemuOptsList qemu_action_opts = {
478 .name = "action",
479 .merge_lists = true,
480 .head = QTAILQ_HEAD_INITIALIZER(qemu_action_opts.head),
481 .desc = {
483 .name = "shutdown",
484 .type = QEMU_OPT_STRING,
486 .name = "reboot",
487 .type = QEMU_OPT_STRING,
489 .name = "panic",
490 .type = QEMU_OPT_STRING,
492 .name = "watchdog",
493 .type = QEMU_OPT_STRING,
495 { /* end of list */ }
499 const char *qemu_get_vm_name(void)
501 return qemu_name;
504 static void default_driver_disable(const char *driver)
506 int i;
508 if (!driver) {
509 return;
512 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
513 if (strcmp(default_list[i].driver, driver) != 0)
514 continue;
515 *(default_list[i].flag) = 0;
519 static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
521 const char *driver = qemu_opt_get(opts, "driver");
523 default_driver_disable(driver);
524 return 0;
527 static void default_driver_check_json(void)
529 DeviceOption *opt;
531 QTAILQ_FOREACH(opt, &device_opts, next) {
532 const char *driver = qdict_get_try_str(opt->opts, "driver");
533 default_driver_disable(driver);
537 static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
539 const char *proc_name;
541 if (qemu_opt_get(opts, "debug-threads")) {
542 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
544 qemu_name = qemu_opt_get(opts, "guest");
546 proc_name = qemu_opt_get(opts, "process");
547 if (proc_name) {
548 os_set_proc_name(proc_name);
551 return 0;
554 bool defaults_enabled(void)
556 return has_defaults;
559 #ifndef _WIN32
560 static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
562 int fd, dupfd, flags;
563 int64_t fdset_id;
564 const char *fd_opaque = NULL;
565 AddfdInfo *fdinfo;
567 fd = qemu_opt_get_number(opts, "fd", -1);
568 fdset_id = qemu_opt_get_number(opts, "set", -1);
569 fd_opaque = qemu_opt_get(opts, "opaque");
571 if (fd < 0) {
572 error_setg(errp, "fd option is required and must be non-negative");
573 return -1;
576 if (fd <= STDERR_FILENO) {
577 error_setg(errp, "fd cannot be a standard I/O stream");
578 return -1;
582 * All fds inherited across exec() necessarily have FD_CLOEXEC
583 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
585 flags = fcntl(fd, F_GETFD);
586 if (flags == -1 || (flags & FD_CLOEXEC)) {
587 error_setg(errp, "fd is not valid or already in use");
588 return -1;
591 if (fdset_id < 0) {
592 error_setg(errp, "set option is required and must be non-negative");
593 return -1;
596 #ifdef F_DUPFD_CLOEXEC
597 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
598 #else
599 dupfd = dup(fd);
600 if (dupfd != -1) {
601 qemu_set_cloexec(dupfd);
603 #endif
604 if (dupfd == -1) {
605 error_setg(errp, "error duplicating fd: %s", strerror(errno));
606 return -1;
609 /* add the duplicate fd, and optionally the opaque string, to the fd set */
610 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
611 &error_abort);
612 g_free(fdinfo);
614 return 0;
617 static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
619 int fd;
621 fd = qemu_opt_get_number(opts, "fd", -1);
622 close(fd);
624 return 0;
626 #endif
628 /***********************************************************/
629 /* QEMU Block devices */
631 #define HD_OPTS "media=disk"
632 #define CDROM_OPTS "media=cdrom"
633 #define FD_OPTS ""
634 #define PFLASH_OPTS ""
635 #define MTD_OPTS ""
636 #define SD_OPTS ""
638 static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
640 BlockInterfaceType *block_default_type = opaque;
642 return drive_new(opts, *block_default_type, errp) == NULL;
645 static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
647 if (qemu_opt_get(opts, "snapshot") == NULL) {
648 qemu_opt_set(opts, "snapshot", "on", &error_abort);
650 return 0;
653 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
654 int index, const char *optstr)
656 QemuOpts *opts;
657 DriveInfo *dinfo;
659 if (!enable || drive_get_by_index(type, index)) {
660 return;
663 opts = drive_add(type, index, NULL, optstr);
664 if (snapshot) {
665 drive_enable_snapshot(NULL, opts, NULL);
668 dinfo = drive_new(opts, type, &error_abort);
669 dinfo->is_default = true;
673 static void configure_blockdev(BlockdevOptionsQueue *bdo_queue,
674 MachineClass *machine_class, int snapshot)
677 * If the currently selected machine wishes to override the
678 * units-per-bus property of its default HBA interface type, do so
679 * now.
681 if (machine_class->units_per_default_bus) {
682 override_max_devs(machine_class->block_default_type,
683 machine_class->units_per_default_bus);
686 /* open the virtual block devices */
687 while (!QSIMPLEQ_EMPTY(bdo_queue)) {
688 BlockdevOptionsQueueEntry *bdo = QSIMPLEQ_FIRST(bdo_queue);
690 QSIMPLEQ_REMOVE_HEAD(bdo_queue, entry);
691 loc_push_restore(&bdo->loc);
692 qmp_blockdev_add(bdo->bdo, &error_fatal);
693 loc_pop(&bdo->loc);
694 qapi_free_BlockdevOptions(bdo->bdo);
695 g_free(bdo);
697 if (snapshot) {
698 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
699 NULL, NULL);
701 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
702 &machine_class->block_default_type, &error_fatal)) {
703 /* We printed help */
704 exit(0);
707 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
708 CDROM_OPTS);
709 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
710 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
714 static QemuOptsList qemu_smp_opts = {
715 .name = "smp-opts",
716 .implied_opt_name = "cpus",
717 .merge_lists = true,
718 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
719 .desc = {
721 .name = "cpus",
722 .type = QEMU_OPT_NUMBER,
723 }, {
724 .name = "sockets",
725 .type = QEMU_OPT_NUMBER,
726 }, {
727 .name = "dies",
728 .type = QEMU_OPT_NUMBER,
729 }, {
730 .name = "cores",
731 .type = QEMU_OPT_NUMBER,
732 }, {
733 .name = "threads",
734 .type = QEMU_OPT_NUMBER,
735 }, {
736 .name = "maxcpus",
737 .type = QEMU_OPT_NUMBER,
739 { /*End of list */ }
743 static void realtime_init(void)
745 if (enable_mlock) {
746 if (os_mlock() < 0) {
747 error_report("locking memory failed");
748 exit(1);
754 static void configure_msg(QemuOpts *opts)
756 message_with_timestamp = qemu_opt_get_bool(opts, "timestamp", false);
757 error_with_guestname = qemu_opt_get_bool(opts, "guest-name", false);
761 /***********************************************************/
762 /* USB devices */
764 static int usb_device_add(const char *devname)
766 USBDevice *dev = NULL;
768 if (!machine_usb(current_machine)) {
769 return -1;
772 dev = usbdevice_create(devname);
773 if (!dev)
774 return -1;
776 return 0;
779 static int usb_parse(const char *cmdline)
781 int r;
782 r = usb_device_add(cmdline);
783 if (r < 0) {
784 error_report("could not add USB device '%s'", cmdline);
786 return r;
789 /***********************************************************/
790 /* machine registration */
792 static MachineClass *find_machine(const char *name, GSList *machines)
794 GSList *el;
796 for (el = machines; el; el = el->next) {
797 MachineClass *mc = el->data;
799 if (!strcmp(mc->name, name) || !g_strcmp0(mc->alias, name)) {
800 return mc;
804 return NULL;
807 static MachineClass *find_default_machine(GSList *machines)
809 GSList *el;
810 MachineClass *default_machineclass = NULL;
812 for (el = machines; el; el = el->next) {
813 MachineClass *mc = el->data;
815 if (mc->is_default) {
816 assert(default_machineclass == NULL && "Multiple default machines");
817 default_machineclass = mc;
821 return default_machineclass;
824 static void version(void)
826 printf("QEMU emulator version " QEMU_FULL_VERSION "\n"
827 QEMU_COPYRIGHT "\n");
830 static void help(int exitcode)
832 version();
833 printf("usage: %s [options] [disk_image]\n\n"
834 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
835 error_get_progname());
837 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
838 if ((arch_mask) & arch_type) \
839 fputs(opt_help, stdout);
841 #define ARCHHEADING(text, arch_mask) \
842 if ((arch_mask) & arch_type) \
843 puts(stringify(text));
845 #define DEFHEADING(text) ARCHHEADING(text, QEMU_ARCH_ALL)
847 #include "qemu-options.def"
849 printf("\nDuring emulation, the following keys are useful:\n"
850 "ctrl-alt-f toggle full screen\n"
851 "ctrl-alt-n switch to virtual console 'n'\n"
852 "ctrl-alt toggle mouse and keyboard grab\n"
853 "\n"
854 "When using -nographic, press 'ctrl-a h' to get some help.\n"
855 "\n"
856 QEMU_HELP_BOTTOM "\n");
858 exit(exitcode);
861 #define HAS_ARG 0x0001
863 typedef struct QEMUOption {
864 const char *name;
865 int flags;
866 int index;
867 uint32_t arch_mask;
868 } QEMUOption;
870 static const QEMUOption qemu_options[] = {
871 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
873 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
874 { option, opt_arg, opt_enum, arch_mask },
875 #define DEFHEADING(text)
876 #define ARCHHEADING(text, arch_mask)
878 #include "qemu-options.def"
879 { NULL },
882 typedef struct VGAInterfaceInfo {
883 const char *opt_name; /* option name */
884 const char *name; /* human-readable name */
885 /* Class names indicating that support is available.
886 * If no class is specified, the interface is always available */
887 const char *class_names[2];
888 } VGAInterfaceInfo;
890 static const VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
891 [VGA_NONE] = {
892 .opt_name = "none",
893 .name = "no graphic card",
895 [VGA_STD] = {
896 .opt_name = "std",
897 .name = "standard VGA",
898 .class_names = { "VGA", "isa-vga" },
900 [VGA_CIRRUS] = {
901 .opt_name = "cirrus",
902 .name = "Cirrus VGA",
903 .class_names = { "cirrus-vga", "isa-cirrus-vga" },
905 [VGA_VMWARE] = {
906 .opt_name = "vmware",
907 .name = "VMWare SVGA",
908 .class_names = { "vmware-svga" },
910 [VGA_VIRTIO] = {
911 .opt_name = "virtio",
912 .name = "Virtio VGA",
913 .class_names = { "virtio-vga" },
915 [VGA_QXL] = {
916 .opt_name = "qxl",
917 .name = "QXL VGA",
918 .class_names = { "qxl-vga" },
920 [VGA_TCX] = {
921 .opt_name = "tcx",
922 .name = "TCX framebuffer",
923 .class_names = { "sun-tcx" },
925 [VGA_CG3] = {
926 .opt_name = "cg3",
927 .name = "CG3 framebuffer",
928 .class_names = { "cgthree" },
930 [VGA_XENFB] = {
931 .opt_name = "xenfb",
932 .name = "Xen paravirtualized framebuffer",
936 static bool vga_interface_available(VGAInterfaceType t)
938 const VGAInterfaceInfo *ti = &vga_interfaces[t];
940 assert(t < VGA_TYPE_MAX);
941 return !ti->class_names[0] ||
942 module_object_class_by_name(ti->class_names[0]) ||
943 module_object_class_by_name(ti->class_names[1]);
946 static const char *
947 get_default_vga_model(const MachineClass *machine_class)
949 if (machine_class->default_display) {
950 return machine_class->default_display;
951 } else if (vga_interface_available(VGA_CIRRUS)) {
952 return "cirrus";
953 } else if (vga_interface_available(VGA_STD)) {
954 return "std";
957 return NULL;
960 static void select_vgahw(const MachineClass *machine_class, const char *p)
962 const char *opts;
963 int t;
965 if (g_str_equal(p, "help")) {
966 const char *def = get_default_vga_model(machine_class);
968 for (t = 0; t < VGA_TYPE_MAX; t++) {
969 const VGAInterfaceInfo *ti = &vga_interfaces[t];
971 if (vga_interface_available(t) && ti->opt_name) {
972 printf("%-20s %s%s\n", ti->opt_name, ti->name ?: "",
973 g_str_equal(ti->opt_name, def) ? " (default)" : "");
976 exit(0);
979 assert(vga_interface_type == VGA_NONE);
980 for (t = 0; t < VGA_TYPE_MAX; t++) {
981 const VGAInterfaceInfo *ti = &vga_interfaces[t];
982 if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
983 if (!vga_interface_available(t)) {
984 error_report("%s not available", ti->name);
985 exit(1);
987 vga_interface_type = t;
988 break;
991 if (t == VGA_TYPE_MAX) {
992 invalid_vga:
993 error_report("unknown vga type: %s", p);
994 exit(1);
996 while (*opts) {
997 const char *nextopt;
999 if (strstart(opts, ",retrace=", &nextopt)) {
1000 opts = nextopt;
1001 if (strstart(opts, "dumb", &nextopt))
1002 vga_retrace_method = VGA_RETRACE_DUMB;
1003 else if (strstart(opts, "precise", &nextopt))
1004 vga_retrace_method = VGA_RETRACE_PRECISE;
1005 else goto invalid_vga;
1006 } else goto invalid_vga;
1007 opts = nextopt;
1011 static void parse_display_qapi(const char *optarg)
1013 DisplayOptions *opts;
1014 Visitor *v;
1016 v = qobject_input_visitor_new_str(optarg, "type", &error_fatal);
1018 visit_type_DisplayOptions(v, NULL, &opts, &error_fatal);
1019 QAPI_CLONE_MEMBERS(DisplayOptions, &dpy, opts);
1021 qapi_free_DisplayOptions(opts);
1022 visit_free(v);
1025 DisplayOptions *qmp_query_display_options(Error **errp)
1027 return QAPI_CLONE(DisplayOptions, &dpy);
1030 static void parse_display(const char *p)
1032 const char *opts;
1034 if (is_help_option(p)) {
1035 qemu_display_help();
1036 exit(0);
1039 if (strstart(p, "sdl", &opts)) {
1041 * sdl DisplayType needs hand-crafted parser instead of
1042 * parse_display_qapi() due to some options not in
1043 * DisplayOptions, specifically:
1044 * - ctrl_grab + alt_grab
1045 * They can't be moved into the QAPI since they use underscores,
1046 * thus they will get replaced by "grab-mod" in the long term
1048 #if defined(CONFIG_SDL)
1049 dpy.type = DISPLAY_TYPE_SDL;
1050 while (*opts) {
1051 const char *nextopt;
1053 if (strstart(opts, ",grab-mod=", &nextopt)) {
1054 opts = nextopt;
1055 if (strstart(opts, "lshift-lctrl-lalt", &nextopt)) {
1056 alt_grab = 1;
1057 } else if (strstart(opts, "rctrl", &nextopt)) {
1058 ctrl_grab = 1;
1059 } else {
1060 goto invalid_sdl_args;
1062 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
1063 opts = nextopt;
1064 if (strstart(opts, "on", &nextopt)) {
1065 alt_grab = 1;
1066 } else if (strstart(opts, "off", &nextopt)) {
1067 alt_grab = 0;
1068 } else {
1069 goto invalid_sdl_args;
1071 warn_report("alt_grab is deprecated, use grab-mod instead.");
1072 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1073 opts = nextopt;
1074 if (strstart(opts, "on", &nextopt)) {
1075 ctrl_grab = 1;
1076 } else if (strstart(opts, "off", &nextopt)) {
1077 ctrl_grab = 0;
1078 } else {
1079 goto invalid_sdl_args;
1081 warn_report("ctrl_grab is deprecated, use grab-mod instead.");
1082 } else if (strstart(opts, ",window_close=", &nextopt) ||
1083 strstart(opts, ",window-close=", &nextopt)) {
1084 if (strstart(opts, ",window_close=", NULL)) {
1085 warn_report("window_close with an underscore is deprecated,"
1086 " please use window-close instead.");
1088 opts = nextopt;
1089 dpy.has_window_close = true;
1090 if (strstart(opts, "on", &nextopt)) {
1091 dpy.window_close = true;
1092 } else if (strstart(opts, "off", &nextopt)) {
1093 dpy.window_close = false;
1094 } else {
1095 goto invalid_sdl_args;
1097 } else if (strstart(opts, ",show-cursor=", &nextopt)) {
1098 opts = nextopt;
1099 dpy.has_show_cursor = true;
1100 if (strstart(opts, "on", &nextopt)) {
1101 dpy.show_cursor = true;
1102 } else if (strstart(opts, "off", &nextopt)) {
1103 dpy.show_cursor = false;
1104 } else {
1105 goto invalid_sdl_args;
1107 } else if (strstart(opts, ",gl=", &nextopt)) {
1108 opts = nextopt;
1109 dpy.has_gl = true;
1110 if (strstart(opts, "on", &nextopt)) {
1111 dpy.gl = DISPLAYGL_MODE_ON;
1112 } else if (strstart(opts, "core", &nextopt)) {
1113 dpy.gl = DISPLAYGL_MODE_CORE;
1114 } else if (strstart(opts, "es", &nextopt)) {
1115 dpy.gl = DISPLAYGL_MODE_ES;
1116 } else if (strstart(opts, "off", &nextopt)) {
1117 dpy.gl = DISPLAYGL_MODE_OFF;
1118 } else {
1119 goto invalid_sdl_args;
1121 } else {
1122 invalid_sdl_args:
1123 error_report("invalid SDL option string");
1124 exit(1);
1126 opts = nextopt;
1128 #else
1129 error_report("SDL display supported is not available in this binary");
1130 exit(1);
1131 #endif
1132 } else if (strstart(p, "vnc", &opts)) {
1134 * vnc isn't a (local) DisplayType but a protocol for remote
1135 * display access.
1137 if (*opts == '=') {
1138 vnc_parse(opts + 1);
1139 } else {
1140 error_report("VNC requires a display argument vnc=<display>");
1141 exit(1);
1143 } else {
1144 parse_display_qapi(p);
1148 static inline bool nonempty_str(const char *str)
1150 return str && *str;
1153 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
1155 gchar *buf;
1156 size_t size;
1157 const char *name, *file, *str, *gen_id;
1158 FWCfgState *fw_cfg = (FWCfgState *) opaque;
1160 if (fw_cfg == NULL) {
1161 error_setg(errp, "fw_cfg device not available");
1162 return -1;
1164 name = qemu_opt_get(opts, "name");
1165 file = qemu_opt_get(opts, "file");
1166 str = qemu_opt_get(opts, "string");
1167 gen_id = qemu_opt_get(opts, "gen_id");
1169 /* we need the name, and exactly one of: file, content string, gen_id */
1170 if (!nonempty_str(name) ||
1171 nonempty_str(file) + nonempty_str(str) + nonempty_str(gen_id) != 1) {
1172 error_setg(errp, "name, plus exactly one of file,"
1173 " string and gen_id, are needed");
1174 return -1;
1176 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
1177 error_setg(errp, "name too long (max. %d char)",
1178 FW_CFG_MAX_FILE_PATH - 1);
1179 return -1;
1181 if (nonempty_str(gen_id)) {
1183 * In this particular case where the content is populated
1184 * internally, the "etc/" namespace protection is relaxed,
1185 * so do not emit a warning.
1187 } else if (strncmp(name, "opt/", 4) != 0) {
1188 warn_report("externally provided fw_cfg item names "
1189 "should be prefixed with \"opt/\"");
1191 if (nonempty_str(str)) {
1192 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
1193 buf = g_memdup(str, size);
1194 } else if (nonempty_str(gen_id)) {
1195 if (!fw_cfg_add_from_generator(fw_cfg, name, gen_id, errp)) {
1196 return -1;
1198 return 0;
1199 } else {
1200 GError *err = NULL;
1201 if (!g_file_get_contents(file, &buf, &size, &err)) {
1202 error_setg(errp, "can't load %s: %s", file, err->message);
1203 g_error_free(err);
1204 return -1;
1207 /* For legacy, keep user files in a specific global order. */
1208 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
1209 fw_cfg_add_file(fw_cfg, name, buf, size);
1210 fw_cfg_reset_order_override(fw_cfg);
1211 return 0;
1214 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
1216 return qdev_device_help(opts);
1219 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
1221 DeviceState *dev;
1223 dev = qdev_device_add(opts, errp);
1224 if (!dev && *errp) {
1225 error_report_err(*errp);
1226 return -1;
1227 } else if (dev) {
1228 object_unref(OBJECT(dev));
1230 return 0;
1233 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
1235 Error *local_err = NULL;
1237 if (!qemu_chr_new_from_opts(opts, NULL, &local_err)) {
1238 if (local_err) {
1239 error_propagate(errp, local_err);
1240 return -1;
1242 exit(0);
1244 return 0;
1247 #ifdef CONFIG_VIRTFS
1248 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
1250 return qemu_fsdev_add(opts, errp);
1252 #endif
1254 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
1256 return monitor_init_opts(opts, errp);
1259 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
1261 static int monitor_device_index = 0;
1262 QemuOpts *opts;
1263 const char *p;
1264 char label[32];
1266 if (strstart(optarg, "chardev:", &p)) {
1267 snprintf(label, sizeof(label), "%s", p);
1268 } else {
1269 snprintf(label, sizeof(label), "compat_monitor%d",
1270 monitor_device_index);
1271 opts = qemu_chr_parse_compat(label, optarg, true);
1272 if (!opts) {
1273 error_report("parse error: %s", optarg);
1274 exit(1);
1278 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
1279 qemu_opt_set(opts, "mode", mode, &error_abort);
1280 qemu_opt_set(opts, "chardev", label, &error_abort);
1281 if (!strcmp(mode, "control")) {
1282 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
1283 } else {
1284 assert(pretty == false);
1286 monitor_device_index++;
1289 struct device_config {
1290 enum {
1291 DEV_USB, /* -usbdevice */
1292 DEV_SERIAL, /* -serial */
1293 DEV_PARALLEL, /* -parallel */
1294 DEV_DEBUGCON, /* -debugcon */
1295 DEV_GDB, /* -gdb, -s */
1296 DEV_SCLP, /* s390 sclp */
1297 } type;
1298 const char *cmdline;
1299 Location loc;
1300 QTAILQ_ENTRY(device_config) next;
1303 static QTAILQ_HEAD(, device_config) device_configs =
1304 QTAILQ_HEAD_INITIALIZER(device_configs);
1306 static void add_device_config(int type, const char *cmdline)
1308 struct device_config *conf;
1310 conf = g_malloc0(sizeof(*conf));
1311 conf->type = type;
1312 conf->cmdline = cmdline;
1313 loc_save(&conf->loc);
1314 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1317 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1319 struct device_config *conf;
1320 int rc;
1322 QTAILQ_FOREACH(conf, &device_configs, next) {
1323 if (conf->type != type)
1324 continue;
1325 loc_push_restore(&conf->loc);
1326 rc = func(conf->cmdline);
1327 loc_pop(&conf->loc);
1328 if (rc) {
1329 return rc;
1332 return 0;
1335 static void qemu_disable_default_devices(void)
1337 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1339 default_driver_check_json();
1340 qemu_opts_foreach(qemu_find_opts("device"),
1341 default_driver_check, NULL, NULL);
1342 qemu_opts_foreach(qemu_find_opts("global"),
1343 default_driver_check, NULL, NULL);
1345 if (!vga_model && !default_vga) {
1346 vga_interface_type = VGA_DEVICE;
1348 if (!has_defaults || machine_class->no_serial) {
1349 default_serial = 0;
1351 if (!has_defaults || machine_class->no_parallel) {
1352 default_parallel = 0;
1354 if (!has_defaults || machine_class->no_floppy) {
1355 default_floppy = 0;
1357 if (!has_defaults || machine_class->no_cdrom) {
1358 default_cdrom = 0;
1360 if (!has_defaults || machine_class->no_sdcard) {
1361 default_sdcard = 0;
1363 if (!has_defaults) {
1364 default_monitor = 0;
1365 default_net = 0;
1366 default_vga = 0;
1370 static void qemu_create_default_devices(void)
1372 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1374 if (is_daemonized()) {
1375 /* According to documentation and historically, -nographic redirects
1376 * serial port, parallel port and monitor to stdio, which does not work
1377 * with -daemonize. We can redirect these to null instead, but since
1378 * -nographic is legacy, let's just error out.
1379 * We disallow -nographic only if all other ports are not redirected
1380 * explicitly, to not break existing legacy setups which uses
1381 * -nographic _and_ redirects all ports explicitly - this is valid
1382 * usage, -nographic is just a no-op in this case.
1384 if (nographic
1385 && (default_parallel || default_serial || default_monitor)) {
1386 error_report("-nographic cannot be used with -daemonize");
1387 exit(1);
1391 if (nographic) {
1392 if (default_parallel)
1393 add_device_config(DEV_PARALLEL, "null");
1394 if (default_serial && default_monitor) {
1395 add_device_config(DEV_SERIAL, "mon:stdio");
1396 } else {
1397 if (default_serial)
1398 add_device_config(DEV_SERIAL, "stdio");
1399 if (default_monitor)
1400 monitor_parse("stdio", "readline", false);
1402 } else {
1403 if (default_serial)
1404 add_device_config(DEV_SERIAL, "vc:80Cx24C");
1405 if (default_parallel)
1406 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
1407 if (default_monitor)
1408 monitor_parse("vc:80Cx24C", "readline", false);
1411 if (default_net) {
1412 QemuOptsList *net = qemu_find_opts("net");
1413 qemu_opts_parse(net, "nic", true, &error_abort);
1414 #ifdef CONFIG_SLIRP
1415 qemu_opts_parse(net, "user", true, &error_abort);
1416 #endif
1419 #if defined(CONFIG_VNC)
1420 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
1421 display_remote++;
1423 #endif
1424 if (dpy.type == DISPLAY_TYPE_DEFAULT && !display_remote) {
1425 if (!qemu_display_find_default(&dpy)) {
1426 dpy.type = DISPLAY_TYPE_NONE;
1427 #if defined(CONFIG_VNC)
1428 vnc_parse("localhost:0,to=99,id=default");
1429 #endif
1432 if (dpy.type == DISPLAY_TYPE_DEFAULT) {
1433 dpy.type = DISPLAY_TYPE_NONE;
1436 /* If no default VGA is requested, the default is "none". */
1437 if (default_vga) {
1438 vga_model = get_default_vga_model(machine_class);
1440 if (vga_model) {
1441 select_vgahw(machine_class, vga_model);
1445 static int serial_parse(const char *devname)
1447 int index = num_serial_hds;
1448 char label[32];
1450 if (strcmp(devname, "none") == 0)
1451 return 0;
1452 snprintf(label, sizeof(label), "serial%d", index);
1453 serial_hds = g_renew(Chardev *, serial_hds, index + 1);
1455 serial_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
1456 if (!serial_hds[index]) {
1457 error_report("could not connect serial device"
1458 " to character backend '%s'", devname);
1459 return -1;
1461 num_serial_hds++;
1462 return 0;
1465 Chardev *serial_hd(int i)
1467 assert(i >= 0);
1468 if (i < num_serial_hds) {
1469 return serial_hds[i];
1471 return NULL;
1474 static int parallel_parse(const char *devname)
1476 static int index = 0;
1477 char label[32];
1479 if (strcmp(devname, "none") == 0)
1480 return 0;
1481 if (index == MAX_PARALLEL_PORTS) {
1482 error_report("too many parallel ports");
1483 exit(1);
1485 snprintf(label, sizeof(label), "parallel%d", index);
1486 parallel_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
1487 if (!parallel_hds[index]) {
1488 error_report("could not connect parallel device"
1489 " to character backend '%s'", devname);
1490 return -1;
1492 index++;
1493 return 0;
1496 static int debugcon_parse(const char *devname)
1498 QemuOpts *opts;
1500 if (!qemu_chr_new_mux_mon("debugcon", devname, NULL)) {
1501 error_report("invalid character backend '%s'", devname);
1502 exit(1);
1504 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
1505 if (!opts) {
1506 error_report("already have a debugcon device");
1507 exit(1);
1509 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
1510 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
1511 return 0;
1514 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
1516 const MachineClass *mc1 = a, *mc2 = b;
1517 int res;
1519 if (mc1->family == NULL) {
1520 if (mc2->family == NULL) {
1521 /* Compare standalone machine types against each other; they sort
1522 * in increasing order.
1524 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
1525 object_class_get_name(OBJECT_CLASS(mc2)));
1528 /* Standalone machine types sort after families. */
1529 return 1;
1532 if (mc2->family == NULL) {
1533 /* Families sort before standalone machine types. */
1534 return -1;
1537 /* Families sort between each other alphabetically increasingly. */
1538 res = strcmp(mc1->family, mc2->family);
1539 if (res != 0) {
1540 return res;
1543 /* Within the same family, machine types sort in decreasing order. */
1544 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
1545 object_class_get_name(OBJECT_CLASS(mc1)));
1548 static void machine_help_func(const QDict *qdict)
1550 GSList *machines, *el;
1551 const char *type = qdict_get_try_str(qdict, "type");
1553 machines = object_class_get_list(TYPE_MACHINE, false);
1554 if (type) {
1555 ObjectClass *machine_class = OBJECT_CLASS(find_machine(type, machines));
1556 if (machine_class) {
1557 type_print_class_properties(object_class_get_name(machine_class));
1558 return;
1562 printf("Supported machines are:\n");
1563 machines = g_slist_sort(machines, machine_class_cmp);
1564 for (el = machines; el; el = el->next) {
1565 MachineClass *mc = el->data;
1566 if (mc->alias) {
1567 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
1569 printf("%-20s %s%s%s\n", mc->name, mc->desc,
1570 mc->is_default ? " (default)" : "",
1571 mc->deprecation_reason ? " (deprecated)" : "");
1575 static void
1576 machine_merge_property(const char *propname, QDict *prop, Error **errp)
1578 QDict *opts;
1580 opts = qdict_new();
1581 /* Preserve the caller's reference to prop. */
1582 qobject_ref(prop);
1583 qdict_put(opts, propname, prop);
1584 keyval_merge(machine_opts_dict, opts, errp);
1585 qobject_unref(opts);
1588 static void
1589 machine_parse_property_opt(QemuOptsList *opts_list, const char *propname,
1590 const char *arg)
1592 QDict *prop = NULL;
1593 bool help = false;
1595 prop = keyval_parse(arg, opts_list->implied_opt_name, &help, &error_fatal);
1596 if (help) {
1597 qemu_opts_print_help(opts_list, true);
1598 exit(0);
1600 machine_merge_property(propname, prop, &error_fatal);
1601 qobject_unref(prop);
1604 static const char *pid_file;
1605 static Notifier qemu_unlink_pidfile_notifier;
1607 static void qemu_unlink_pidfile(Notifier *n, void *data)
1609 if (pid_file) {
1610 unlink(pid_file);
1614 static const QEMUOption *lookup_opt(int argc, char **argv,
1615 const char **poptarg, int *poptind)
1617 const QEMUOption *popt;
1618 int optind = *poptind;
1619 char *r = argv[optind];
1620 const char *optarg;
1622 loc_set_cmdline(argv, optind, 1);
1623 optind++;
1624 /* Treat --foo the same as -foo. */
1625 if (r[1] == '-')
1626 r++;
1627 popt = qemu_options;
1628 for(;;) {
1629 if (!popt->name) {
1630 error_report("invalid option");
1631 exit(1);
1633 if (!strcmp(popt->name, r + 1))
1634 break;
1635 popt++;
1637 if (popt->flags & HAS_ARG) {
1638 if (optind >= argc) {
1639 error_report("requires an argument");
1640 exit(1);
1642 optarg = argv[optind++];
1643 loc_set_cmdline(argv, optind - 2, 2);
1644 } else {
1645 optarg = NULL;
1648 *poptarg = optarg;
1649 *poptind = optind;
1651 return popt;
1654 static MachineClass *select_machine(QDict *qdict, Error **errp)
1656 const char *optarg = qdict_get_try_str(qdict, "type");
1657 GSList *machines = object_class_get_list(TYPE_MACHINE, false);
1658 MachineClass *machine_class;
1659 Error *local_err = NULL;
1661 if (optarg) {
1662 machine_class = find_machine(optarg, machines);
1663 qdict_del(qdict, "type");
1664 if (!machine_class) {
1665 error_setg(&local_err, "unsupported machine type");
1667 } else {
1668 machine_class = find_default_machine(machines);
1669 if (!machine_class) {
1670 error_setg(&local_err, "No machine specified, and there is no default");
1674 g_slist_free(machines);
1675 if (local_err) {
1676 error_append_hint(&local_err, "Use -machine help to list supported machines\n");
1677 error_propagate(errp, local_err);
1679 return machine_class;
1682 static int object_parse_property_opt(Object *obj,
1683 const char *name, const char *value,
1684 const char *skip, Error **errp)
1686 if (g_str_equal(name, skip)) {
1687 return 0;
1690 if (!object_property_parse(obj, name, value, errp)) {
1691 return -1;
1694 return 0;
1697 /* *Non*recursively replace underscores with dashes in QDict keys. */
1698 static void keyval_dashify(QDict *qdict, Error **errp)
1700 const QDictEntry *ent, *next;
1701 char *p;
1703 for (ent = qdict_first(qdict); ent; ent = next) {
1704 g_autofree char *new_key = NULL;
1706 next = qdict_next(qdict, ent);
1707 if (!strchr(ent->key, '_')) {
1708 continue;
1710 new_key = g_strdup(ent->key);
1711 for (p = new_key; *p; p++) {
1712 if (*p == '_') {
1713 *p = '-';
1716 if (qdict_haskey(qdict, new_key)) {
1717 error_setg(errp, "Conflict between '%s' and '%s'", ent->key, new_key);
1718 return;
1720 qobject_ref(ent->value);
1721 qdict_put_obj(qdict, new_key, ent->value);
1722 qdict_del(qdict, ent->key);
1726 static void qemu_apply_legacy_machine_options(QDict *qdict)
1728 const char *value;
1730 keyval_dashify(qdict, &error_fatal);
1732 /* Legacy options do not correspond to MachineState properties. */
1733 value = qdict_get_try_str(qdict, "accel");
1734 if (value) {
1735 accelerators = g_strdup(value);
1736 qdict_del(qdict, "accel");
1739 value = qdict_get_try_str(qdict, "igd-passthru");
1740 if (value) {
1741 object_register_sugar_prop(ACCEL_CLASS_NAME("xen"), "igd-passthru", value,
1742 false);
1743 qdict_del(qdict, "igd-passthru");
1746 value = qdict_get_try_str(qdict, "kvm-shadow-mem");
1747 if (value) {
1748 object_register_sugar_prop(ACCEL_CLASS_NAME("kvm"), "kvm-shadow-mem", value,
1749 false);
1750 qdict_del(qdict, "kvm-shadow-mem");
1753 value = qdict_get_try_str(qdict, "kernel-irqchip");
1754 if (value) {
1755 object_register_sugar_prop(ACCEL_CLASS_NAME("kvm"), "kernel-irqchip", value,
1756 false);
1757 object_register_sugar_prop(ACCEL_CLASS_NAME("whpx"), "kernel-irqchip", value,
1758 false);
1759 qdict_del(qdict, "kernel-irqchip");
1763 static void object_option_foreach_add(bool (*type_opt_predicate)(const char *))
1765 ObjectOption *opt, *next;
1767 QTAILQ_FOREACH_SAFE(opt, &object_opts, next, next) {
1768 const char *type = ObjectType_str(opt->opts->qom_type);
1769 if (type_opt_predicate(type)) {
1770 user_creatable_add_qapi(opt->opts, &error_fatal);
1771 qapi_free_ObjectOptions(opt->opts);
1772 QTAILQ_REMOVE(&object_opts, opt, next);
1773 g_free(opt);
1778 static void object_option_add_visitor(Visitor *v)
1780 ObjectOption *opt = g_new0(ObjectOption, 1);
1781 visit_type_ObjectOptions(v, NULL, &opt->opts, &error_fatal);
1782 QTAILQ_INSERT_TAIL(&object_opts, opt, next);
1785 static void object_option_parse(const char *optarg)
1787 QemuOpts *opts;
1788 const char *type;
1789 Visitor *v;
1791 if (optarg[0] == '{') {
1792 QObject *obj = qobject_from_json(optarg, &error_fatal);
1794 v = qobject_input_visitor_new(obj);
1795 qobject_unref(obj);
1796 } else {
1797 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
1798 optarg, true);
1799 if (!opts) {
1800 exit(1);
1803 type = qemu_opt_get(opts, "qom-type");
1804 if (!type) {
1805 error_setg(&error_fatal, QERR_MISSING_PARAMETER, "qom-type");
1807 if (user_creatable_print_help(type, opts)) {
1808 exit(0);
1811 v = opts_visitor_new(opts);
1814 object_option_add_visitor(v);
1815 visit_free(v);
1819 * Initial object creation happens before all other
1820 * QEMU data types are created. The majority of objects
1821 * can be created at this point. The rng-egd object
1822 * cannot be created here, as it depends on the chardev
1823 * already existing.
1825 static bool object_create_early(const char *type)
1828 * Objects should not be made "delayed" without a reason. If you
1829 * add one, state the reason in a comment!
1832 /* Reason: property "chardev" */
1833 if (g_str_equal(type, "rng-egd") ||
1834 g_str_equal(type, "qtest")) {
1835 return false;
1838 #if defined(CONFIG_VHOST_USER) && defined(CONFIG_LINUX)
1839 /* Reason: cryptodev-vhost-user property "chardev" */
1840 if (g_str_equal(type, "cryptodev-vhost-user")) {
1841 return false;
1843 #endif
1845 /* Reason: vhost-user-blk-server property "node-name" */
1846 if (g_str_equal(type, "vhost-user-blk-server")) {
1847 return false;
1850 * Reason: filter-* property "netdev" etc.
1852 if (g_str_equal(type, "filter-buffer") ||
1853 g_str_equal(type, "filter-dump") ||
1854 g_str_equal(type, "filter-mirror") ||
1855 g_str_equal(type, "filter-redirector") ||
1856 g_str_equal(type, "colo-compare") ||
1857 g_str_equal(type, "filter-rewriter") ||
1858 g_str_equal(type, "filter-replay")) {
1859 return false;
1863 * Allocation of large amounts of memory may delay
1864 * chardev initialization for too long, and trigger timeouts
1865 * on software that waits for a monitor socket to be created
1866 * (e.g. libvirt).
1868 if (g_str_has_prefix(type, "memory-backend-")) {
1869 return false;
1872 return true;
1875 static void qemu_apply_machine_options(QDict *qdict)
1877 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1878 const char *boot_order = NULL;
1879 const char *boot_once = NULL;
1880 QemuOpts *opts;
1882 object_set_properties_from_keyval(OBJECT(current_machine), qdict, false, &error_fatal);
1883 current_machine->ram_size = ram_size;
1884 current_machine->maxram_size = maxram_size;
1885 current_machine->ram_slots = ram_slots;
1887 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
1888 if (opts) {
1889 boot_order = qemu_opt_get(opts, "order");
1890 if (boot_order) {
1891 validate_bootdevices(boot_order, &error_fatal);
1894 boot_once = qemu_opt_get(opts, "once");
1895 if (boot_once) {
1896 validate_bootdevices(boot_once, &error_fatal);
1899 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
1900 boot_strict = qemu_opt_get_bool(opts, "strict", false);
1903 if (!boot_order) {
1904 boot_order = machine_class->default_boot_order;
1907 current_machine->boot_order = boot_order;
1908 current_machine->boot_once = boot_once;
1910 if (semihosting_enabled() && !semihosting_get_argc()) {
1911 /* fall back to the -kernel/-append */
1912 semihosting_arg_fallback(current_machine->kernel_filename, current_machine->kernel_cmdline);
1915 if (current_machine->smp.cpus > 1) {
1916 Error *blocker = NULL;
1917 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
1918 replay_add_blocker(blocker);
1922 static void qemu_create_early_backends(void)
1924 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1925 #if defined(CONFIG_SDL)
1926 const bool use_sdl = (dpy.type == DISPLAY_TYPE_SDL);
1927 #else
1928 const bool use_sdl = false;
1929 #endif
1930 #if defined(CONFIG_GTK)
1931 const bool use_gtk = (dpy.type == DISPLAY_TYPE_GTK);
1932 #else
1933 const bool use_gtk = false;
1934 #endif
1936 if ((alt_grab || ctrl_grab) && !use_sdl) {
1937 error_report("-alt-grab and -ctrl-grab are only valid "
1938 "for SDL, ignoring option");
1940 if (dpy.has_window_close && !use_gtk && !use_sdl) {
1941 error_report("-no-quit is only valid for GTK and SDL, "
1942 "ignoring option");
1945 qemu_display_early_init(&dpy);
1946 qemu_console_early_init();
1948 if (dpy.has_gl && dpy.gl != DISPLAYGL_MODE_OFF && display_opengl == 0) {
1949 #if defined(CONFIG_OPENGL)
1950 error_report("OpenGL is not supported by the display");
1951 #else
1952 error_report("OpenGL support is disabled");
1953 #endif
1954 exit(1);
1957 object_option_foreach_add(object_create_early);
1959 /* spice needs the timers to be initialized by this point */
1960 /* spice must initialize before audio as it changes the default auiodev */
1961 /* spice must initialize before chardevs (for spicevmc and spiceport) */
1962 qemu_spice.init();
1964 qemu_opts_foreach(qemu_find_opts("chardev"),
1965 chardev_init_func, NULL, &error_fatal);
1967 #ifdef CONFIG_VIRTFS
1968 qemu_opts_foreach(qemu_find_opts("fsdev"),
1969 fsdev_init_func, NULL, &error_fatal);
1970 #endif
1973 * Note: we need to create audio and block backends before
1974 * setting machine properties, so they can be referred to.
1976 configure_blockdev(&bdo_queue, machine_class, snapshot);
1977 audio_init_audiodevs();
1982 * The remainder of object creation happens after the
1983 * creation of chardev, fsdev, net clients and device data types.
1985 static bool object_create_late(const char *type)
1987 return !object_create_early(type);
1990 static void qemu_create_late_backends(void)
1992 if (qtest_chrdev) {
1993 qtest_server_init(qtest_chrdev, qtest_log, &error_fatal);
1996 net_init_clients(&error_fatal);
1998 object_option_foreach_add(object_create_late);
2000 if (tpm_init() < 0) {
2001 exit(1);
2004 qemu_opts_foreach(qemu_find_opts("mon"),
2005 mon_init_func, NULL, &error_fatal);
2007 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
2008 exit(1);
2009 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
2010 exit(1);
2011 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
2012 exit(1);
2014 /* now chardevs have been created we may have semihosting to connect */
2015 qemu_semihosting_connect_chardevs();
2016 qemu_semihosting_console_init();
2019 static bool have_custom_ram_size(void)
2021 QemuOpts *opts = qemu_find_opts_singleton("memory");
2022 return !!qemu_opt_get_size(opts, "size", 0);
2025 static void qemu_resolve_machine_memdev(void)
2027 if (current_machine->ram_memdev_id) {
2028 Object *backend;
2029 ram_addr_t backend_size;
2031 backend = object_resolve_path_type(current_machine->ram_memdev_id,
2032 TYPE_MEMORY_BACKEND, NULL);
2033 if (!backend) {
2034 error_report("Memory backend '%s' not found",
2035 current_machine->ram_memdev_id);
2036 exit(EXIT_FAILURE);
2038 backend_size = object_property_get_uint(backend, "size", &error_abort);
2039 if (have_custom_ram_size() && backend_size != ram_size) {
2040 error_report("Size specified by -m option must match size of "
2041 "explicitly specified 'memory-backend' property");
2042 exit(EXIT_FAILURE);
2044 if (mem_path) {
2045 error_report("'-mem-path' can't be used together with"
2046 "'-machine memory-backend'");
2047 exit(EXIT_FAILURE);
2049 ram_size = backend_size;
2052 if (!xen_enabled()) {
2053 /* On 32-bit hosts, QEMU is limited by virtual address space */
2054 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
2055 error_report("at most 2047 MB RAM can be simulated");
2056 exit(1);
2061 static void set_memory_options(MachineClass *mc)
2063 uint64_t sz;
2064 const char *mem_str;
2065 const ram_addr_t default_ram_size = mc->default_ram_size;
2066 QemuOpts *opts = qemu_find_opts_singleton("memory");
2067 Location loc;
2069 loc_push_none(&loc);
2070 qemu_opts_loc_restore(opts);
2072 sz = 0;
2073 mem_str = qemu_opt_get(opts, "size");
2074 if (mem_str) {
2075 if (!*mem_str) {
2076 error_report("missing 'size' option value");
2077 exit(EXIT_FAILURE);
2080 sz = qemu_opt_get_size(opts, "size", ram_size);
2082 /* Fix up legacy suffix-less format */
2083 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2084 uint64_t overflow_check = sz;
2086 sz *= MiB;
2087 if (sz / MiB != overflow_check) {
2088 error_report("too large 'size' option value");
2089 exit(EXIT_FAILURE);
2094 /* backward compatibility behaviour for case "-m 0" */
2095 if (sz == 0) {
2096 sz = default_ram_size;
2099 sz = QEMU_ALIGN_UP(sz, 8192);
2100 if (mc->fixup_ram_size) {
2101 sz = mc->fixup_ram_size(sz);
2103 ram_size = sz;
2104 if (ram_size != sz) {
2105 error_report("ram size too large");
2106 exit(EXIT_FAILURE);
2109 maxram_size = ram_size;
2111 if (qemu_opt_get(opts, "maxmem")) {
2112 uint64_t slots;
2114 sz = qemu_opt_get_size(opts, "maxmem", 0);
2115 slots = qemu_opt_get_number(opts, "slots", 0);
2116 if (sz < ram_size) {
2117 error_report("invalid value of -m option maxmem: "
2118 "maximum memory size (0x%" PRIx64 ") must be at least "
2119 "the initial memory size (0x" RAM_ADDR_FMT ")",
2120 sz, ram_size);
2121 exit(EXIT_FAILURE);
2122 } else if (slots && sz == ram_size) {
2123 error_report("invalid value of -m option maxmem: "
2124 "memory slots were specified but maximum memory size "
2125 "(0x%" PRIx64 ") is equal to the initial memory size "
2126 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
2127 exit(EXIT_FAILURE);
2130 maxram_size = sz;
2131 ram_slots = slots;
2132 } else if (qemu_opt_get(opts, "slots")) {
2133 error_report("invalid -m option value: missing 'maxmem' option");
2134 exit(EXIT_FAILURE);
2137 loc_pop(&loc);
2140 static void qemu_create_machine(QDict *qdict)
2142 MachineClass *machine_class = select_machine(qdict, &error_fatal);
2143 object_set_machine_compat_props(machine_class->compat_props);
2145 set_memory_options(machine_class);
2147 current_machine = MACHINE(object_new_with_class(OBJECT_CLASS(machine_class)));
2148 object_property_add_child(object_get_root(), "machine",
2149 OBJECT(current_machine));
2150 object_property_add_child(container_get(OBJECT(current_machine),
2151 "/unattached"),
2152 "sysbus", OBJECT(sysbus_get_default()));
2154 if (machine_class->minimum_page_bits) {
2155 if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
2156 /* This would be a board error: specifying a minimum smaller than
2157 * a target's compile-time fixed setting.
2159 g_assert_not_reached();
2163 cpu_exec_init_all();
2164 page_size_init();
2166 if (machine_class->hw_version) {
2167 qemu_set_hw_version(machine_class->hw_version);
2171 * Get the default machine options from the machine if it is not already
2172 * specified either by the configuration file or by the command line.
2174 if (machine_class->default_machine_opts) {
2175 QDict *default_opts =
2176 keyval_parse(machine_class->default_machine_opts, NULL, NULL,
2177 &error_abort);
2178 qemu_apply_legacy_machine_options(default_opts);
2179 object_set_properties_from_keyval(OBJECT(current_machine), default_opts,
2180 false, &error_abort);
2181 qobject_unref(default_opts);
2185 static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
2187 GlobalProperty *g;
2189 g = g_malloc0(sizeof(*g));
2190 g->driver = qemu_opt_get(opts, "driver");
2191 g->property = qemu_opt_get(opts, "property");
2192 g->value = qemu_opt_get(opts, "value");
2193 qdev_prop_register_global(g);
2194 return 0;
2198 * Return whether configuration group @group is stored in QemuOpts, or
2199 * recorded as one or more QDicts by qemu_record_config_group.
2201 static bool is_qemuopts_group(const char *group)
2203 if (g_str_equal(group, "object") ||
2204 g_str_equal(group, "machine") ||
2205 g_str_equal(group, "smp-opts")) {
2206 return false;
2208 return true;
2211 static void qemu_record_config_group(const char *group, QDict *dict,
2212 bool from_json, Error **errp)
2214 if (g_str_equal(group, "object")) {
2215 Visitor *v = qobject_input_visitor_new_keyval(QOBJECT(dict));
2216 object_option_add_visitor(v);
2217 visit_free(v);
2218 } else if (g_str_equal(group, "machine")) {
2220 * Cannot merge string-valued and type-safe dictionaries, so JSON
2221 * is not accepted yet for -M.
2223 assert(!from_json);
2224 keyval_merge(machine_opts_dict, dict, errp);
2225 } else if (g_str_equal(group, "smp-opts")) {
2226 machine_merge_property("smp", dict, &error_fatal);
2227 } else {
2228 abort();
2233 * Parse non-QemuOpts config file groups, pass the rest to
2234 * qemu_config_do_parse.
2236 static void qemu_parse_config_group(const char *group, QDict *qdict,
2237 void *opaque, Error **errp)
2239 QObject *crumpled;
2240 if (is_qemuopts_group(group)) {
2241 qemu_config_do_parse(group, qdict, opaque, errp);
2242 return;
2245 crumpled = qdict_crumple(qdict, errp);
2246 if (!crumpled) {
2247 return;
2249 switch (qobject_type(crumpled)) {
2250 case QTYPE_QDICT:
2251 qemu_record_config_group(group, qobject_to(QDict, crumpled), false, errp);
2252 break;
2253 case QTYPE_QLIST:
2254 error_setg(errp, "Lists cannot be at top level of a configuration section");
2255 break;
2256 default:
2257 g_assert_not_reached();
2259 qobject_unref(crumpled);
2262 static void qemu_read_default_config_file(Error **errp)
2264 ERRP_GUARD();
2265 int ret;
2266 g_autofree char *file = get_relocated_path(CONFIG_QEMU_CONFDIR "/qemu.conf");
2268 ret = qemu_read_config_file(file, qemu_parse_config_group, errp);
2269 if (ret < 0) {
2270 if (ret == -ENOENT) {
2271 error_free(*errp);
2272 *errp = NULL;
2277 static void qemu_set_option(const char *str, Error **errp)
2279 char group[64], id[64], arg[64];
2280 QemuOptsList *list;
2281 QemuOpts *opts;
2282 int rc, offset;
2284 rc = sscanf(str, "%63[^.].%63[^.].%63[^=]%n", group, id, arg, &offset);
2285 if (rc < 3 || str[offset] != '=') {
2286 error_setg(errp, "can't parse: \"%s\"", str);
2287 return;
2290 if (!is_qemuopts_group(group)) {
2291 error_setg(errp, "-set is not supported with %s", group);
2292 } else {
2293 list = qemu_find_opts_err(group, errp);
2294 if (list) {
2295 opts = qemu_opts_find(list, id);
2296 if (!opts) {
2297 error_setg(errp, "there is no %s \"%s\" defined", group, id);
2298 return;
2300 qemu_opt_set(opts, arg, str + offset + 1, errp);
2305 static void user_register_global_props(void)
2307 qemu_opts_foreach(qemu_find_opts("global"),
2308 global_init_func, NULL, NULL);
2311 static int do_configure_icount(void *opaque, QemuOpts *opts, Error **errp)
2313 icount_configure(opts, errp);
2314 return 0;
2317 static int accelerator_set_property(void *opaque,
2318 const char *name, const char *value,
2319 Error **errp)
2321 return object_parse_property_opt(opaque, name, value, "accel", errp);
2324 static int do_configure_accelerator(void *opaque, QemuOpts *opts, Error **errp)
2326 bool *p_init_failed = opaque;
2327 const char *acc = qemu_opt_get(opts, "accel");
2328 AccelClass *ac = accel_find(acc);
2329 AccelState *accel;
2330 int ret;
2331 bool qtest_with_kvm;
2333 qtest_with_kvm = g_str_equal(acc, "kvm") && qtest_chrdev != NULL;
2335 if (!ac) {
2336 *p_init_failed = true;
2337 if (!qtest_with_kvm) {
2338 error_report("invalid accelerator %s", acc);
2340 return 0;
2342 accel = ACCEL(object_new_with_class(OBJECT_CLASS(ac)));
2343 object_apply_compat_props(OBJECT(accel));
2344 qemu_opt_foreach(opts, accelerator_set_property,
2345 accel,
2346 &error_fatal);
2348 ret = accel_init_machine(accel, current_machine);
2349 if (ret < 0) {
2350 *p_init_failed = true;
2351 if (!qtest_with_kvm || ret != -ENOENT) {
2352 error_report("failed to initialize %s: %s", acc, strerror(-ret));
2354 return 0;
2357 return 1;
2360 static void configure_accelerators(const char *progname)
2362 bool init_failed = false;
2364 qemu_opts_foreach(qemu_find_opts("icount"),
2365 do_configure_icount, NULL, &error_fatal);
2367 if (QTAILQ_EMPTY(&qemu_accel_opts.head)) {
2368 char **accel_list, **tmp;
2370 if (accelerators == NULL) {
2371 /* Select the default accelerator */
2372 bool have_tcg = accel_find("tcg");
2373 bool have_kvm = accel_find("kvm");
2375 if (have_tcg && have_kvm) {
2376 if (g_str_has_suffix(progname, "kvm")) {
2377 /* If the program name ends with "kvm", we prefer KVM */
2378 accelerators = "kvm:tcg";
2379 } else {
2380 accelerators = "tcg:kvm";
2382 } else if (have_kvm) {
2383 accelerators = "kvm";
2384 } else if (have_tcg) {
2385 accelerators = "tcg";
2386 } else {
2387 error_report("No accelerator selected and"
2388 " no default accelerator available");
2389 exit(1);
2392 accel_list = g_strsplit(accelerators, ":", 0);
2394 for (tmp = accel_list; *tmp; tmp++) {
2396 * Filter invalid accelerators here, to prevent obscenities
2397 * such as "-machine accel=tcg,,thread=single".
2399 if (accel_find(*tmp)) {
2400 qemu_opts_parse_noisily(qemu_find_opts("accel"), *tmp, true);
2401 } else {
2402 init_failed = true;
2403 error_report("invalid accelerator %s", *tmp);
2406 g_strfreev(accel_list);
2407 } else {
2408 if (accelerators != NULL) {
2409 error_report("The -accel and \"-machine accel=\" options are incompatible");
2410 exit(1);
2414 if (!qemu_opts_foreach(qemu_find_opts("accel"),
2415 do_configure_accelerator, &init_failed, &error_fatal)) {
2416 if (!init_failed) {
2417 error_report("no accelerator found");
2419 exit(1);
2422 if (init_failed && !qtest_chrdev) {
2423 AccelClass *ac = ACCEL_GET_CLASS(current_accel());
2424 error_report("falling back to %s", ac->name);
2427 if (icount_enabled() && !tcg_enabled()) {
2428 error_report("-icount is not allowed with hardware virtualization");
2429 exit(1);
2433 static void create_default_memdev(MachineState *ms, const char *path)
2435 Object *obj;
2436 MachineClass *mc = MACHINE_GET_CLASS(ms);
2438 obj = object_new(path ? TYPE_MEMORY_BACKEND_FILE : TYPE_MEMORY_BACKEND_RAM);
2439 if (path) {
2440 object_property_set_str(obj, "mem-path", path, &error_fatal);
2442 object_property_set_int(obj, "size", ms->ram_size, &error_fatal);
2443 object_property_add_child(object_get_objects_root(), mc->default_ram_id,
2444 obj);
2445 /* Ensure backend's memory region name is equal to mc->default_ram_id */
2446 object_property_set_bool(obj, "x-use-canonical-path-for-ramblock-id",
2447 false, &error_fatal);
2448 user_creatable_complete(USER_CREATABLE(obj), &error_fatal);
2449 object_unref(obj);
2450 object_property_set_str(OBJECT(ms), "memory-backend", mc->default_ram_id,
2451 &error_fatal);
2454 static void qemu_validate_options(const QDict *machine_opts)
2456 const char *kernel_filename = qdict_get_try_str(machine_opts, "kernel");
2457 const char *initrd_filename = qdict_get_try_str(machine_opts, "initrd");
2458 const char *kernel_cmdline = qdict_get_try_str(machine_opts, "append");
2460 if (kernel_filename == NULL) {
2461 if (kernel_cmdline != NULL) {
2462 error_report("-append only allowed with -kernel option");
2463 exit(1);
2466 if (initrd_filename != NULL) {
2467 error_report("-initrd only allowed with -kernel option");
2468 exit(1);
2472 if (loadvm && preconfig_requested) {
2473 error_report("'preconfig' and 'loadvm' options are "
2474 "mutually exclusive");
2475 exit(EXIT_FAILURE);
2477 if (incoming && preconfig_requested && strcmp(incoming, "defer") != 0) {
2478 error_report("'preconfig' supports '-incoming defer' only");
2479 exit(EXIT_FAILURE);
2482 #ifdef CONFIG_CURSES
2483 if (is_daemonized() && dpy.type == DISPLAY_TYPE_CURSES) {
2484 error_report("curses display cannot be used with -daemonize");
2485 exit(1);
2487 #endif
2490 static void qemu_process_sugar_options(void)
2492 if (mem_prealloc) {
2493 QObject *smp = qdict_get(machine_opts_dict, "smp");
2494 if (smp && qobject_type(smp) == QTYPE_QDICT) {
2495 QObject *cpus = qdict_get(qobject_to(QDict, smp), "cpus");
2496 if (cpus && qobject_type(cpus) == QTYPE_QSTRING) {
2497 const char *val = qstring_get_str(qobject_to(QString, cpus));
2498 object_register_sugar_prop("memory-backend", "prealloc-threads",
2499 val, false);
2502 object_register_sugar_prop("memory-backend", "prealloc", "on", false);
2505 if (watchdog) {
2506 int i = select_watchdog(watchdog);
2507 if (i > 0)
2508 exit (i == 1 ? 1 : 0);
2512 /* -action processing */
2515 * Process all the -action parameters parsed from cmdline.
2517 static int process_runstate_actions(void *opaque, QemuOpts *opts, Error **errp)
2519 Error *local_err = NULL;
2520 QDict *qdict = qemu_opts_to_qdict(opts, NULL);
2521 QObject *ret = NULL;
2522 qmp_marshal_set_action(qdict, &ret, &local_err);
2523 qobject_unref(ret);
2524 qobject_unref(qdict);
2525 if (local_err) {
2526 error_propagate(errp, local_err);
2527 return 1;
2529 return 0;
2532 static void qemu_process_early_options(void)
2534 #ifdef CONFIG_SECCOMP
2535 QemuOptsList *olist = qemu_find_opts_err("sandbox", NULL);
2536 if (olist) {
2537 qemu_opts_foreach(olist, parse_sandbox, NULL, &error_fatal);
2539 #endif
2541 qemu_opts_foreach(qemu_find_opts("name"),
2542 parse_name, NULL, &error_fatal);
2544 if (qemu_opts_foreach(qemu_find_opts("action"),
2545 process_runstate_actions, NULL, &error_fatal)) {
2546 exit(1);
2549 #ifndef _WIN32
2550 qemu_opts_foreach(qemu_find_opts("add-fd"),
2551 parse_add_fd, NULL, &error_fatal);
2553 qemu_opts_foreach(qemu_find_opts("add-fd"),
2554 cleanup_add_fd, NULL, &error_fatal);
2555 #endif
2557 /* Open the logfile at this point and set the log mask if necessary. */
2558 if (log_file) {
2559 qemu_set_log_filename(log_file, &error_fatal);
2561 if (log_mask) {
2562 int mask;
2563 mask = qemu_str_to_log_mask(log_mask);
2564 if (!mask) {
2565 qemu_print_log_usage(stdout);
2566 exit(1);
2568 qemu_set_log(mask);
2569 } else {
2570 qemu_set_log(0);
2573 qemu_add_default_firmwarepath();
2576 static void qemu_process_help_options(void)
2579 * Check for -cpu help and -device help before we call select_machine(),
2580 * which will return an error if the architecture has no default machine
2581 * type and the user did not specify one, so that the user doesn't need
2582 * to say '-cpu help -machine something'.
2584 if (cpu_option && is_help_option(cpu_option)) {
2585 list_cpus(cpu_option);
2586 exit(0);
2589 if (qemu_opts_foreach(qemu_find_opts("device"),
2590 device_help_func, NULL, NULL)) {
2591 exit(0);
2594 /* -L help lists the data directories and exits. */
2595 if (list_data_dirs) {
2596 qemu_list_data_dirs();
2597 exit(0);
2601 static void qemu_maybe_daemonize(const char *pid_file)
2603 Error *err = NULL;
2605 os_daemonize();
2606 rcu_disable_atfork();
2608 if (pid_file && !qemu_write_pidfile(pid_file, &err)) {
2609 error_reportf_err(err, "cannot create PID file: ");
2610 exit(1);
2613 qemu_unlink_pidfile_notifier.notify = qemu_unlink_pidfile;
2614 qemu_add_exit_notifier(&qemu_unlink_pidfile_notifier);
2617 static void qemu_init_displays(void)
2619 DisplayState *ds;
2621 /* init local displays */
2622 ds = init_displaystate();
2623 qemu_display_init(ds, &dpy);
2625 /* must be after terminal init, SDL library changes signal handlers */
2626 os_setup_signal_handling();
2628 /* init remote displays */
2629 #ifdef CONFIG_VNC
2630 qemu_opts_foreach(qemu_find_opts("vnc"),
2631 vnc_init_func, NULL, &error_fatal);
2632 #endif
2634 if (using_spice) {
2635 qemu_spice.display_init();
2639 static void qemu_init_board(void)
2641 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
2643 if (machine_class->default_ram_id && current_machine->ram_size &&
2644 numa_uses_legacy_mem() && !current_machine->ram_memdev_id) {
2645 create_default_memdev(current_machine, mem_path);
2648 /* process plugin before CPUs are created, but once -smp has been parsed */
2649 qemu_plugin_load_list(&plugin_list, &error_fatal);
2651 /* From here on we enter MACHINE_PHASE_INITIALIZED. */
2652 machine_run_board_init(current_machine);
2654 drive_check_orphaned();
2656 realtime_init();
2658 if (hax_enabled()) {
2659 /* FIXME: why isn't cpu_synchronize_all_post_init enough? */
2660 hax_sync_vcpus();
2664 static void qemu_create_cli_devices(void)
2666 DeviceOption *opt;
2668 soundhw_init();
2670 qemu_opts_foreach(qemu_find_opts("fw_cfg"),
2671 parse_fw_cfg, fw_cfg_find(), &error_fatal);
2673 /* init USB devices */
2674 if (machine_usb(current_machine)) {
2675 if (foreach_device_config(DEV_USB, usb_parse) < 0)
2676 exit(1);
2679 /* init generic devices */
2680 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
2681 qemu_opts_foreach(qemu_find_opts("device"),
2682 device_init_func, NULL, &error_fatal);
2683 QTAILQ_FOREACH(opt, &device_opts, next) {
2684 loc_push_restore(&opt->loc);
2686 * TODO Eventually we should call qmp_device_add() here to make sure it
2687 * behaves the same, but QMP still has to accept incorrectly typed
2688 * options until libvirt is fixed and we want to be strict on the CLI
2689 * from the start, so call qdev_device_add_from_qdict() directly for
2690 * now.
2692 qdev_device_add_from_qdict(opt->opts, true, &error_fatal);
2693 loc_pop(&opt->loc);
2695 rom_reset_order_override();
2698 static void qemu_machine_creation_done(void)
2700 MachineState *machine = MACHINE(qdev_get_machine());
2702 /* Did we create any drives that we failed to create a device for? */
2703 drive_check_orphaned();
2705 /* Don't warn about the default network setup that you get if
2706 * no command line -net or -netdev options are specified. There
2707 * are two cases that we would otherwise complain about:
2708 * (1) board doesn't support a NIC but the implicit "-net nic"
2709 * requested one
2710 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
2711 * sets up a nic that isn't connected to anything.
2713 if (!default_net && (!qtest_enabled() || has_defaults)) {
2714 net_check_clients();
2717 qdev_prop_check_globals();
2719 qdev_machine_creation_done();
2721 if (machine->cgs) {
2723 * Verify that Confidential Guest Support has actually been initialized
2725 assert(machine->cgs->ready);
2728 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
2729 exit(1);
2733 void qmp_x_exit_preconfig(Error **errp)
2735 if (phase_check(PHASE_MACHINE_INITIALIZED)) {
2736 error_setg(errp, "The command is permitted only before machine initialization");
2737 return;
2740 qemu_init_board();
2741 qemu_create_cli_devices();
2742 qemu_machine_creation_done();
2744 if (loadvm) {
2745 load_snapshot(loadvm, NULL, false, NULL, &error_fatal);
2747 if (replay_mode != REPLAY_MODE_NONE) {
2748 replay_vmstate_init();
2751 if (incoming) {
2752 Error *local_err = NULL;
2753 if (strcmp(incoming, "defer") != 0) {
2754 qmp_migrate_incoming(incoming, &local_err);
2755 if (local_err) {
2756 error_reportf_err(local_err, "-incoming %s: ", incoming);
2757 exit(1);
2760 } else if (autostart) {
2761 qmp_cont(NULL);
2765 void qemu_init(int argc, char **argv, char **envp)
2767 QemuOpts *opts;
2768 QemuOpts *icount_opts = NULL, *accel_opts = NULL;
2769 QemuOptsList *olist;
2770 int optind;
2771 const char *optarg;
2772 MachineClass *machine_class;
2773 bool userconfig = true;
2774 FILE *vmstate_dump_file = NULL;
2776 qemu_add_opts(&qemu_drive_opts);
2777 qemu_add_drive_opts(&qemu_legacy_drive_opts);
2778 qemu_add_drive_opts(&qemu_common_drive_opts);
2779 qemu_add_drive_opts(&qemu_drive_opts);
2780 qemu_add_drive_opts(&bdrv_runtime_opts);
2781 qemu_add_opts(&qemu_chardev_opts);
2782 qemu_add_opts(&qemu_device_opts);
2783 qemu_add_opts(&qemu_netdev_opts);
2784 qemu_add_opts(&qemu_nic_opts);
2785 qemu_add_opts(&qemu_net_opts);
2786 qemu_add_opts(&qemu_rtc_opts);
2787 qemu_add_opts(&qemu_global_opts);
2788 qemu_add_opts(&qemu_mon_opts);
2789 qemu_add_opts(&qemu_trace_opts);
2790 qemu_plugin_add_opts();
2791 qemu_add_opts(&qemu_option_rom_opts);
2792 qemu_add_opts(&qemu_accel_opts);
2793 qemu_add_opts(&qemu_mem_opts);
2794 qemu_add_opts(&qemu_smp_opts);
2795 qemu_add_opts(&qemu_boot_opts);
2796 qemu_add_opts(&qemu_add_fd_opts);
2797 qemu_add_opts(&qemu_object_opts);
2798 qemu_add_opts(&qemu_tpmdev_opts);
2799 qemu_add_opts(&qemu_overcommit_opts);
2800 qemu_add_opts(&qemu_msg_opts);
2801 qemu_add_opts(&qemu_name_opts);
2802 qemu_add_opts(&qemu_numa_opts);
2803 qemu_add_opts(&qemu_icount_opts);
2804 qemu_add_opts(&qemu_semihosting_config_opts);
2805 qemu_add_opts(&qemu_fw_cfg_opts);
2806 qemu_add_opts(&qemu_action_opts);
2807 module_call_init(MODULE_INIT_OPTS);
2809 error_init(argv[0]);
2810 qemu_init_exec_dir(argv[0]);
2812 #ifdef CONFIG_MODULES
2813 module_init_info(qemu_modinfo);
2814 module_allow_arch(TARGET_NAME);
2815 #endif
2817 qemu_init_subsystems();
2819 /* first pass of option parsing */
2820 optind = 1;
2821 while (optind < argc) {
2822 if (argv[optind][0] != '-') {
2823 /* disk image */
2824 optind++;
2825 } else {
2826 const QEMUOption *popt;
2828 popt = lookup_opt(argc, argv, &optarg, &optind);
2829 switch (popt->index) {
2830 case QEMU_OPTION_nouserconfig:
2831 userconfig = false;
2832 break;
2837 machine_opts_dict = qdict_new();
2838 if (userconfig) {
2839 qemu_read_default_config_file(&error_fatal);
2842 /* second pass of option parsing */
2843 optind = 1;
2844 for(;;) {
2845 if (optind >= argc)
2846 break;
2847 if (argv[optind][0] != '-') {
2848 loc_set_cmdline(argv, optind, 1);
2849 drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2850 } else {
2851 const QEMUOption *popt;
2853 popt = lookup_opt(argc, argv, &optarg, &optind);
2854 if (!(popt->arch_mask & arch_type)) {
2855 error_report("Option not supported for this target");
2856 exit(1);
2858 switch(popt->index) {
2859 case QEMU_OPTION_cpu:
2860 /* hw initialization will check this */
2861 cpu_option = optarg;
2862 break;
2863 case QEMU_OPTION_hda:
2864 case QEMU_OPTION_hdb:
2865 case QEMU_OPTION_hdc:
2866 case QEMU_OPTION_hdd:
2867 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2868 HD_OPTS);
2869 break;
2870 case QEMU_OPTION_blockdev:
2872 Visitor *v;
2873 BlockdevOptionsQueueEntry *bdo;
2875 v = qobject_input_visitor_new_str(optarg, "driver",
2876 &error_fatal);
2878 bdo = g_new(BlockdevOptionsQueueEntry, 1);
2879 visit_type_BlockdevOptions(v, NULL, &bdo->bdo,
2880 &error_fatal);
2881 visit_free(v);
2882 loc_save(&bdo->loc);
2883 QSIMPLEQ_INSERT_TAIL(&bdo_queue, bdo, entry);
2884 break;
2886 case QEMU_OPTION_drive:
2887 if (drive_def(optarg) == NULL) {
2888 exit(1);
2890 break;
2891 case QEMU_OPTION_set:
2892 qemu_set_option(optarg, &error_fatal);
2893 break;
2894 case QEMU_OPTION_global:
2895 if (qemu_global_option(optarg) != 0)
2896 exit(1);
2897 break;
2898 case QEMU_OPTION_mtdblock:
2899 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2900 break;
2901 case QEMU_OPTION_sd:
2902 drive_add(IF_SD, -1, optarg, SD_OPTS);
2903 break;
2904 case QEMU_OPTION_pflash:
2905 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2906 break;
2907 case QEMU_OPTION_snapshot:
2909 Error *blocker = NULL;
2910 snapshot = 1;
2911 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
2912 "-snapshot");
2913 replay_add_blocker(blocker);
2915 break;
2916 case QEMU_OPTION_numa:
2917 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
2918 optarg, true);
2919 if (!opts) {
2920 exit(1);
2922 break;
2923 case QEMU_OPTION_display:
2924 parse_display(optarg);
2925 break;
2926 case QEMU_OPTION_nographic:
2927 qdict_put_str(machine_opts_dict, "graphics", "off");
2928 nographic = true;
2929 dpy.type = DISPLAY_TYPE_NONE;
2930 break;
2931 case QEMU_OPTION_curses:
2932 warn_report("-curses is deprecated, "
2933 "use -display curses instead.");
2934 #ifdef CONFIG_CURSES
2935 dpy.type = DISPLAY_TYPE_CURSES;
2936 #else
2937 error_report("curses or iconv support is disabled");
2938 exit(1);
2939 #endif
2940 break;
2941 case QEMU_OPTION_portrait:
2942 graphic_rotate = 90;
2943 break;
2944 case QEMU_OPTION_rotate:
2945 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2946 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2947 graphic_rotate != 180 && graphic_rotate != 270) {
2948 error_report("only 90, 180, 270 deg rotation is available");
2949 exit(1);
2951 break;
2952 case QEMU_OPTION_kernel:
2953 qdict_put_str(machine_opts_dict, "kernel", optarg);
2954 break;
2955 case QEMU_OPTION_initrd:
2956 qdict_put_str(machine_opts_dict, "initrd", optarg);
2957 break;
2958 case QEMU_OPTION_append:
2959 qdict_put_str(machine_opts_dict, "append", optarg);
2960 break;
2961 case QEMU_OPTION_dtb:
2962 qdict_put_str(machine_opts_dict, "dtb", optarg);
2963 break;
2964 case QEMU_OPTION_cdrom:
2965 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
2966 break;
2967 case QEMU_OPTION_boot:
2968 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
2969 optarg, true);
2970 if (!opts) {
2971 exit(1);
2973 break;
2974 case QEMU_OPTION_fda:
2975 case QEMU_OPTION_fdb:
2976 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2977 optarg, FD_OPTS);
2978 break;
2979 case QEMU_OPTION_no_fd_bootchk:
2980 fd_bootchk = 0;
2981 break;
2982 case QEMU_OPTION_netdev:
2983 default_net = 0;
2984 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2985 exit(1);
2987 break;
2988 case QEMU_OPTION_nic:
2989 default_net = 0;
2990 if (net_client_parse(qemu_find_opts("nic"), optarg) == -1) {
2991 exit(1);
2993 break;
2994 case QEMU_OPTION_net:
2995 default_net = 0;
2996 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2997 exit(1);
2999 break;
3000 #ifdef CONFIG_LIBISCSI
3001 case QEMU_OPTION_iscsi:
3002 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
3003 optarg, false);
3004 if (!opts) {
3005 exit(1);
3007 break;
3008 #endif
3009 case QEMU_OPTION_audio_help:
3010 audio_legacy_help();
3011 exit (0);
3012 break;
3013 case QEMU_OPTION_audiodev:
3014 audio_parse_option(optarg);
3015 break;
3016 case QEMU_OPTION_soundhw:
3017 select_soundhw (optarg);
3018 break;
3019 case QEMU_OPTION_h:
3020 help(0);
3021 break;
3022 case QEMU_OPTION_version:
3023 version();
3024 exit(0);
3025 break;
3026 case QEMU_OPTION_m:
3027 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
3028 optarg, true);
3029 if (!opts) {
3030 exit(EXIT_FAILURE);
3032 break;
3033 #ifdef CONFIG_TPM
3034 case QEMU_OPTION_tpmdev:
3035 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3036 exit(1);
3038 break;
3039 #endif
3040 case QEMU_OPTION_mempath:
3041 mem_path = optarg;
3042 break;
3043 case QEMU_OPTION_mem_prealloc:
3044 mem_prealloc = 1;
3045 break;
3046 case QEMU_OPTION_d:
3047 log_mask = optarg;
3048 break;
3049 case QEMU_OPTION_D:
3050 log_file = optarg;
3051 break;
3052 case QEMU_OPTION_DFILTER:
3053 qemu_set_dfilter_ranges(optarg, &error_fatal);
3054 break;
3055 case QEMU_OPTION_seed:
3056 qemu_guest_random_seed_main(optarg, &error_fatal);
3057 break;
3058 case QEMU_OPTION_s:
3059 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3060 break;
3061 case QEMU_OPTION_gdb:
3062 add_device_config(DEV_GDB, optarg);
3063 break;
3064 case QEMU_OPTION_L:
3065 if (is_help_option(optarg)) {
3066 list_data_dirs = true;
3067 } else {
3068 qemu_add_data_dir(g_strdup(optarg));
3070 break;
3071 case QEMU_OPTION_bios:
3072 qdict_put_str(machine_opts_dict, "firmware", optarg);
3073 break;
3074 case QEMU_OPTION_singlestep:
3075 singlestep = 1;
3076 break;
3077 case QEMU_OPTION_S:
3078 autostart = 0;
3079 break;
3080 case QEMU_OPTION_k:
3081 keyboard_layout = optarg;
3082 break;
3083 case QEMU_OPTION_vga:
3084 vga_model = optarg;
3085 default_vga = 0;
3086 break;
3087 case QEMU_OPTION_g:
3089 const char *p;
3090 int w, h, depth;
3091 p = optarg;
3092 w = strtol(p, (char **)&p, 10);
3093 if (w <= 0) {
3094 graphic_error:
3095 error_report("invalid resolution or depth");
3096 exit(1);
3098 if (*p != 'x')
3099 goto graphic_error;
3100 p++;
3101 h = strtol(p, (char **)&p, 10);
3102 if (h <= 0)
3103 goto graphic_error;
3104 if (*p == 'x') {
3105 p++;
3106 depth = strtol(p, (char **)&p, 10);
3107 if (depth != 1 && depth != 2 && depth != 4 &&
3108 depth != 8 && depth != 15 && depth != 16 &&
3109 depth != 24 && depth != 32)
3110 goto graphic_error;
3111 } else if (*p == '\0') {
3112 depth = graphic_depth;
3113 } else {
3114 goto graphic_error;
3117 graphic_width = w;
3118 graphic_height = h;
3119 graphic_depth = depth;
3121 break;
3122 case QEMU_OPTION_echr:
3124 char *r;
3125 term_escape_char = strtol(optarg, &r, 0);
3126 if (r == optarg)
3127 printf("Bad argument to echr\n");
3128 break;
3130 case QEMU_OPTION_monitor:
3131 default_monitor = 0;
3132 if (strncmp(optarg, "none", 4)) {
3133 monitor_parse(optarg, "readline", false);
3135 break;
3136 case QEMU_OPTION_qmp:
3137 monitor_parse(optarg, "control", false);
3138 default_monitor = 0;
3139 break;
3140 case QEMU_OPTION_qmp_pretty:
3141 monitor_parse(optarg, "control", true);
3142 default_monitor = 0;
3143 break;
3144 case QEMU_OPTION_mon:
3145 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3146 true);
3147 if (!opts) {
3148 exit(1);
3150 default_monitor = 0;
3151 break;
3152 case QEMU_OPTION_chardev:
3153 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3154 optarg, true);
3155 if (!opts) {
3156 exit(1);
3158 break;
3159 case QEMU_OPTION_fsdev:
3160 olist = qemu_find_opts("fsdev");
3161 if (!olist) {
3162 error_report("fsdev support is disabled");
3163 exit(1);
3165 opts = qemu_opts_parse_noisily(olist, optarg, true);
3166 if (!opts) {
3167 exit(1);
3169 break;
3170 case QEMU_OPTION_virtfs: {
3171 QemuOpts *fsdev;
3172 QemuOpts *device;
3173 const char *writeout, *sock_fd, *socket, *path, *security_model,
3174 *multidevs;
3176 olist = qemu_find_opts("virtfs");
3177 if (!olist) {
3178 error_report("virtfs support is disabled");
3179 exit(1);
3181 opts = qemu_opts_parse_noisily(olist, optarg, true);
3182 if (!opts) {
3183 exit(1);
3186 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3187 qemu_opt_get(opts, "mount_tag") == NULL) {
3188 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3189 exit(1);
3191 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3192 qemu_opts_id(opts) ?:
3193 qemu_opt_get(opts, "mount_tag"),
3194 1, NULL);
3195 if (!fsdev) {
3196 error_report("duplicate or invalid fsdev id: %s",
3197 qemu_opt_get(opts, "mount_tag"));
3198 exit(1);
3201 writeout = qemu_opt_get(opts, "writeout");
3202 if (writeout) {
3203 #ifdef CONFIG_SYNC_FILE_RANGE
3204 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3205 #else
3206 error_report("writeout=immediate not supported "
3207 "on this platform");
3208 exit(1);
3209 #endif
3211 qemu_opt_set(fsdev, "fsdriver",
3212 qemu_opt_get(opts, "fsdriver"), &error_abort);
3213 path = qemu_opt_get(opts, "path");
3214 if (path) {
3215 qemu_opt_set(fsdev, "path", path, &error_abort);
3217 security_model = qemu_opt_get(opts, "security_model");
3218 if (security_model) {
3219 qemu_opt_set(fsdev, "security_model", security_model,
3220 &error_abort);
3222 socket = qemu_opt_get(opts, "socket");
3223 if (socket) {
3224 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3226 sock_fd = qemu_opt_get(opts, "sock_fd");
3227 if (sock_fd) {
3228 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3231 qemu_opt_set_bool(fsdev, "readonly",
3232 qemu_opt_get_bool(opts, "readonly", 0),
3233 &error_abort);
3234 multidevs = qemu_opt_get(opts, "multidevs");
3235 if (multidevs) {
3236 qemu_opt_set(fsdev, "multidevs", multidevs, &error_abort);
3238 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3239 &error_abort);
3240 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3241 qemu_opt_set(device, "fsdev",
3242 qemu_opts_id(fsdev), &error_abort);
3243 qemu_opt_set(device, "mount_tag",
3244 qemu_opt_get(opts, "mount_tag"), &error_abort);
3245 break;
3247 case QEMU_OPTION_serial:
3248 add_device_config(DEV_SERIAL, optarg);
3249 default_serial = 0;
3250 if (strncmp(optarg, "mon:", 4) == 0) {
3251 default_monitor = 0;
3253 break;
3254 case QEMU_OPTION_watchdog:
3255 if (watchdog) {
3256 error_report("only one watchdog option may be given");
3257 exit(1);
3259 warn_report("-watchdog is deprecated; use -device instead.");
3260 watchdog = optarg;
3261 break;
3262 case QEMU_OPTION_action:
3263 olist = qemu_find_opts("action");
3264 if (!qemu_opts_parse_noisily(olist, optarg, false)) {
3265 exit(1);
3267 break;
3268 case QEMU_OPTION_watchdog_action: {
3269 QemuOpts *opts;
3270 opts = qemu_opts_create(qemu_find_opts("action"), NULL, 0, &error_abort);
3271 qemu_opt_set(opts, "watchdog", optarg, &error_abort);
3272 break;
3274 case QEMU_OPTION_parallel:
3275 add_device_config(DEV_PARALLEL, optarg);
3276 default_parallel = 0;
3277 if (strncmp(optarg, "mon:", 4) == 0) {
3278 default_monitor = 0;
3280 break;
3281 case QEMU_OPTION_debugcon:
3282 add_device_config(DEV_DEBUGCON, optarg);
3283 break;
3284 case QEMU_OPTION_loadvm:
3285 loadvm = optarg;
3286 break;
3287 case QEMU_OPTION_full_screen:
3288 dpy.has_full_screen = true;
3289 dpy.full_screen = true;
3290 break;
3291 case QEMU_OPTION_alt_grab:
3292 alt_grab = 1;
3293 warn_report("-alt-grab is deprecated, please use "
3294 "-display sdl,grab-mod=lshift-lctrl-lalt instead.");
3295 break;
3296 case QEMU_OPTION_ctrl_grab:
3297 ctrl_grab = 1;
3298 warn_report("-ctrl-grab is deprecated, please use "
3299 "-display sdl,grab-mod=rctrl instead.");
3300 break;
3301 case QEMU_OPTION_no_quit:
3302 dpy.has_window_close = true;
3303 dpy.window_close = false;
3304 warn_report("-no-quit is deprecated, please use "
3305 "-display ...,window-close=off instead.");
3306 break;
3307 case QEMU_OPTION_sdl:
3308 warn_report("-sdl is deprecated, use -display sdl instead.");
3309 #ifdef CONFIG_SDL
3310 dpy.type = DISPLAY_TYPE_SDL;
3311 break;
3312 #else
3313 error_report("SDL support is disabled");
3314 exit(1);
3315 #endif
3316 case QEMU_OPTION_pidfile:
3317 pid_file = optarg;
3318 break;
3319 case QEMU_OPTION_win2k_hack:
3320 win2k_install_hack = 1;
3321 break;
3322 case QEMU_OPTION_acpitable:
3323 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3324 optarg, true);
3325 if (!opts) {
3326 exit(1);
3328 acpi_table_add(opts, &error_fatal);
3329 break;
3330 case QEMU_OPTION_smbios:
3331 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3332 optarg, false);
3333 if (!opts) {
3334 exit(1);
3336 smbios_entry_add(opts, &error_fatal);
3337 break;
3338 case QEMU_OPTION_fwcfg:
3339 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3340 optarg, true);
3341 if (opts == NULL) {
3342 exit(1);
3344 break;
3345 case QEMU_OPTION_preconfig:
3346 preconfig_requested = true;
3347 break;
3348 case QEMU_OPTION_enable_kvm:
3349 qdict_put_str(machine_opts_dict, "accel", "kvm");
3350 break;
3351 case QEMU_OPTION_M:
3352 case QEMU_OPTION_machine:
3354 bool help;
3356 keyval_parse_into(machine_opts_dict, optarg, "type", &help, &error_fatal);
3357 if (help) {
3358 machine_help_func(machine_opts_dict);
3359 exit(EXIT_SUCCESS);
3361 break;
3363 case QEMU_OPTION_accel:
3364 accel_opts = qemu_opts_parse_noisily(qemu_find_opts("accel"),
3365 optarg, true);
3366 optarg = qemu_opt_get(accel_opts, "accel");
3367 if (!optarg || is_help_option(optarg)) {
3368 printf("Accelerators supported in QEMU binary:\n");
3369 GSList *el, *accel_list = object_class_get_list(TYPE_ACCEL,
3370 false);
3371 for (el = accel_list; el; el = el->next) {
3372 gchar *typename = g_strdup(object_class_get_name(
3373 OBJECT_CLASS(el->data)));
3374 /* omit qtest which is used for tests only */
3375 if (g_strcmp0(typename, ACCEL_CLASS_NAME("qtest")) &&
3376 g_str_has_suffix(typename, ACCEL_CLASS_SUFFIX)) {
3377 gchar **optname = g_strsplit(typename,
3378 ACCEL_CLASS_SUFFIX, 0);
3379 printf("%s\n", optname[0]);
3380 g_strfreev(optname);
3382 g_free(typename);
3384 g_slist_free(accel_list);
3385 exit(0);
3387 break;
3388 case QEMU_OPTION_usb:
3389 qdict_put_str(machine_opts_dict, "usb", "on");
3390 break;
3391 case QEMU_OPTION_usbdevice:
3392 qdict_put_str(machine_opts_dict, "usb", "on");
3393 add_device_config(DEV_USB, optarg);
3394 break;
3395 case QEMU_OPTION_device:
3396 if (optarg[0] == '{') {
3397 QObject *obj = qobject_from_json(optarg, &error_fatal);
3398 DeviceOption *opt = g_new0(DeviceOption, 1);
3399 opt->opts = qobject_to(QDict, obj);
3400 loc_save(&opt->loc);
3401 assert(opt->opts != NULL);
3402 QTAILQ_INSERT_TAIL(&device_opts, opt, next);
3403 } else {
3404 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3405 optarg, true)) {
3406 exit(1);
3409 break;
3410 case QEMU_OPTION_smp:
3411 machine_parse_property_opt(qemu_find_opts("smp-opts"),
3412 "smp", optarg);
3413 break;
3414 case QEMU_OPTION_vnc:
3415 vnc_parse(optarg);
3416 break;
3417 case QEMU_OPTION_no_acpi:
3418 qdict_put_str(machine_opts_dict, "acpi", "off");
3419 break;
3420 case QEMU_OPTION_no_hpet:
3421 qdict_put_str(machine_opts_dict, "hpet", "off");
3422 break;
3423 case QEMU_OPTION_no_reboot:
3424 olist = qemu_find_opts("action");
3425 qemu_opts_parse_noisily(olist, "reboot=shutdown", false);
3426 break;
3427 case QEMU_OPTION_no_shutdown:
3428 olist = qemu_find_opts("action");
3429 qemu_opts_parse_noisily(olist, "shutdown=pause", false);
3430 break;
3431 case QEMU_OPTION_uuid:
3432 if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
3433 error_report("failed to parse UUID string: wrong format");
3434 exit(1);
3436 qemu_uuid_set = true;
3437 break;
3438 case QEMU_OPTION_option_rom:
3439 if (nb_option_roms >= MAX_OPTION_ROMS) {
3440 error_report("too many option ROMs");
3441 exit(1);
3443 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3444 optarg, true);
3445 if (!opts) {
3446 exit(1);
3448 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3449 option_rom[nb_option_roms].bootindex =
3450 qemu_opt_get_number(opts, "bootindex", -1);
3451 if (!option_rom[nb_option_roms].name) {
3452 error_report("Option ROM file is not specified");
3453 exit(1);
3455 nb_option_roms++;
3456 break;
3457 case QEMU_OPTION_semihosting:
3458 qemu_semihosting_enable();
3459 break;
3460 case QEMU_OPTION_semihosting_config:
3461 if (qemu_semihosting_config_options(optarg) != 0) {
3462 exit(1);
3464 break;
3465 case QEMU_OPTION_name:
3466 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3467 optarg, true);
3468 if (!opts) {
3469 exit(1);
3471 /* Capture guest name if -msg guest-name is used later */
3472 error_guest_name = qemu_opt_get(opts, "guest");
3473 break;
3474 case QEMU_OPTION_prom_env:
3475 if (nb_prom_envs >= MAX_PROM_ENVS) {
3476 error_report("too many prom variables");
3477 exit(1);
3479 prom_envs[nb_prom_envs] = optarg;
3480 nb_prom_envs++;
3481 break;
3482 case QEMU_OPTION_old_param:
3483 old_param = 1;
3484 break;
3485 case QEMU_OPTION_rtc:
3486 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3487 false);
3488 if (!opts) {
3489 exit(1);
3491 break;
3492 case QEMU_OPTION_icount:
3493 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3494 optarg, true);
3495 if (!icount_opts) {
3496 exit(1);
3498 break;
3499 case QEMU_OPTION_incoming:
3500 if (!incoming) {
3501 runstate_set(RUN_STATE_INMIGRATE);
3503 incoming = optarg;
3504 break;
3505 case QEMU_OPTION_only_migratable:
3506 only_migratable = 1;
3507 break;
3508 case QEMU_OPTION_nodefaults:
3509 has_defaults = 0;
3510 break;
3511 case QEMU_OPTION_xen_domid:
3512 if (!(accel_find("xen"))) {
3513 error_report("Option not supported for this target");
3514 exit(1);
3516 xen_domid = atoi(optarg);
3517 break;
3518 case QEMU_OPTION_xen_attach:
3519 if (!(accel_find("xen"))) {
3520 error_report("Option not supported for this target");
3521 exit(1);
3523 xen_mode = XEN_ATTACH;
3524 break;
3525 case QEMU_OPTION_xen_domid_restrict:
3526 if (!(accel_find("xen"))) {
3527 error_report("Option not supported for this target");
3528 exit(1);
3530 xen_domid_restrict = true;
3531 break;
3532 case QEMU_OPTION_trace:
3533 trace_opt_parse(optarg);
3534 break;
3535 case QEMU_OPTION_plugin:
3536 qemu_plugin_opt_parse(optarg, &plugin_list);
3537 break;
3538 case QEMU_OPTION_readconfig:
3539 qemu_read_config_file(optarg, qemu_parse_config_group, &error_fatal);
3540 break;
3541 case QEMU_OPTION_spice:
3542 olist = qemu_find_opts_err("spice", NULL);
3543 if (!olist) {
3544 error_report("spice support is disabled");
3545 exit(1);
3547 opts = qemu_opts_parse_noisily(olist, optarg, false);
3548 if (!opts) {
3549 exit(1);
3551 display_remote++;
3552 break;
3553 case QEMU_OPTION_writeconfig:
3555 FILE *fp;
3556 warn_report("-writeconfig is deprecated and will go away without a replacement");
3557 if (strcmp(optarg, "-") == 0) {
3558 fp = stdout;
3559 } else {
3560 fp = fopen(optarg, "w");
3561 if (fp == NULL) {
3562 error_report("open %s: %s", optarg,
3563 strerror(errno));
3564 exit(1);
3567 qemu_config_write(fp);
3568 if (fp != stdout) {
3569 fclose(fp);
3571 break;
3573 case QEMU_OPTION_qtest:
3574 qtest_chrdev = optarg;
3575 break;
3576 case QEMU_OPTION_qtest_log:
3577 qtest_log = optarg;
3578 break;
3579 case QEMU_OPTION_sandbox:
3580 olist = qemu_find_opts("sandbox");
3581 if (!olist) {
3582 #ifndef CONFIG_SECCOMP
3583 error_report("-sandbox support is not enabled "
3584 "in this QEMU binary");
3585 #endif
3586 exit(1);
3589 opts = qemu_opts_parse_noisily(olist, optarg, true);
3590 if (!opts) {
3591 exit(1);
3593 break;
3594 case QEMU_OPTION_add_fd:
3595 #ifndef _WIN32
3596 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
3597 optarg, false);
3598 if (!opts) {
3599 exit(1);
3601 #else
3602 error_report("File descriptor passing is disabled on this "
3603 "platform");
3604 exit(1);
3605 #endif
3606 break;
3607 case QEMU_OPTION_object:
3608 object_option_parse(optarg);
3609 break;
3610 case QEMU_OPTION_overcommit:
3611 opts = qemu_opts_parse_noisily(qemu_find_opts("overcommit"),
3612 optarg, false);
3613 if (!opts) {
3614 exit(1);
3616 enable_mlock = qemu_opt_get_bool(opts, "mem-lock", false);
3617 enable_cpu_pm = qemu_opt_get_bool(opts, "cpu-pm", false);
3618 break;
3619 case QEMU_OPTION_compat:
3621 CompatPolicy *opts;
3622 Visitor *v;
3624 v = qobject_input_visitor_new_str(optarg, NULL,
3625 &error_fatal);
3627 visit_type_CompatPolicy(v, NULL, &opts, &error_fatal);
3628 QAPI_CLONE_MEMBERS(CompatPolicy, &compat_policy, opts);
3630 qapi_free_CompatPolicy(opts);
3631 visit_free(v);
3632 break;
3634 case QEMU_OPTION_msg:
3635 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
3636 false);
3637 if (!opts) {
3638 exit(1);
3640 configure_msg(opts);
3641 break;
3642 case QEMU_OPTION_dump_vmstate:
3643 if (vmstate_dump_file) {
3644 error_report("only one '-dump-vmstate' "
3645 "option may be given");
3646 exit(1);
3648 vmstate_dump_file = fopen(optarg, "w");
3649 if (vmstate_dump_file == NULL) {
3650 error_report("open %s: %s", optarg, strerror(errno));
3651 exit(1);
3653 break;
3654 case QEMU_OPTION_enable_sync_profile:
3655 qsp_enable();
3656 break;
3657 case QEMU_OPTION_nouserconfig:
3658 /* Nothing to be parsed here. Especially, do not error out below. */
3659 break;
3660 default:
3661 if (os_parse_cmd_args(popt->index, optarg)) {
3662 error_report("Option not supported in this build");
3663 exit(1);
3669 * Clear error location left behind by the loop.
3670 * Best done right after the loop. Do not insert code here!
3672 loc_set_none();
3674 qemu_validate_options(machine_opts_dict);
3675 qemu_process_sugar_options();
3678 * These options affect everything else and should be processed
3679 * before daemonizing.
3681 qemu_process_early_options();
3683 qemu_process_help_options();
3684 qemu_maybe_daemonize(pid_file);
3687 * The trace backend must be initialized after daemonizing.
3688 * trace_init_backends() will call st_init(), which will create the
3689 * trace thread in the parent, and also register st_flush_trace_buffer()
3690 * in atexit(). This function will force the parent to wait for the
3691 * writeout thread to finish, which will not occur, and the parent
3692 * process will be left in the host.
3694 if (!trace_init_backends()) {
3695 exit(1);
3697 trace_init_file();
3699 qemu_init_main_loop(&error_fatal);
3700 cpu_timers_init();
3702 user_register_global_props();
3703 replay_configure(icount_opts);
3705 configure_rtc(qemu_find_opts_singleton("rtc"));
3707 qemu_create_machine(machine_opts_dict);
3709 suspend_mux_open();
3711 qemu_disable_default_devices();
3712 qemu_create_default_devices();
3713 qemu_create_early_backends();
3715 qemu_apply_legacy_machine_options(machine_opts_dict);
3716 qemu_apply_machine_options(machine_opts_dict);
3717 qobject_unref(machine_opts_dict);
3718 phase_advance(PHASE_MACHINE_CREATED);
3721 * Note: uses machine properties such as kernel-irqchip, must run
3722 * after qemu_apply_machine_options.
3724 configure_accelerators(argv[0]);
3725 phase_advance(PHASE_ACCEL_CREATED);
3728 * Beware, QOM objects created before this point miss global and
3729 * compat properties.
3731 * Global properties get set up by qdev_prop_register_global(),
3732 * called from user_register_global_props(), and certain option
3733 * desugaring. Also in CPU feature desugaring (buried in
3734 * parse_cpu_option()), which happens below this point, but may
3735 * only target the CPU type, which can only be created after
3736 * parse_cpu_option() returned the type.
3738 * Machine compat properties: object_set_machine_compat_props().
3739 * Accelerator compat props: object_set_accelerator_compat_props(),
3740 * called from do_configure_accelerator().
3743 machine_class = MACHINE_GET_CLASS(current_machine);
3744 if (!qtest_enabled() && machine_class->deprecation_reason) {
3745 error_report("Machine type '%s' is deprecated: %s",
3746 machine_class->name, machine_class->deprecation_reason);
3750 * Note: creates a QOM object, must run only after global and
3751 * compat properties have been set up.
3753 migration_object_init();
3755 qemu_create_late_backends();
3757 /* parse features once if machine provides default cpu_type */
3758 current_machine->cpu_type = machine_class->default_cpu_type;
3759 if (cpu_option) {
3760 current_machine->cpu_type = parse_cpu_option(cpu_option);
3762 /* NB: for machine none cpu_type could STILL be NULL here! */
3764 qemu_resolve_machine_memdev();
3765 parse_numa_opts(current_machine);
3767 if (vmstate_dump_file) {
3768 /* dump and exit */
3769 dump_vmstate_json_to_file(vmstate_dump_file);
3770 exit(0);
3773 if (!preconfig_requested) {
3774 qmp_x_exit_preconfig(&error_fatal);
3776 qemu_init_displays();
3777 accel_setup_post(current_machine);
3778 os_setup_post();
3779 resume_mux_open();