Merge tag 'pull-loongarch-20240719' of https://gitlab.com/gaosong/qemu into staging
[qemu/ar7.git] / system / vl.c
blob9e8f16f1551674fc31808a69722b0d755ee0dd34
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/help-texts.h"
27 #include "qemu/datadir.h"
28 #include "qemu/units.h"
29 #include "exec/cpu-common.h"
30 #include "exec/page-vary.h"
31 #include "hw/qdev-properties.h"
32 #include "qapi/compat-policy.h"
33 #include "qapi/error.h"
34 #include "qapi/qmp/qdict.h"
35 #include "qapi/qmp/qstring.h"
36 #include "qapi/qmp/qjson.h"
37 #include "qemu-version.h"
38 #include "qemu/cutils.h"
39 #include "qemu/help_option.h"
40 #include "qemu/hw-version.h"
41 #include "qemu/uuid.h"
42 #include "sysemu/reset.h"
43 #include "sysemu/runstate.h"
44 #include "sysemu/runstate-action.h"
45 #include "sysemu/seccomp.h"
46 #include "sysemu/tcg.h"
47 #include "sysemu/xen.h"
49 #include "qemu/error-report.h"
50 #include "qemu/sockets.h"
51 #include "qemu/accel.h"
52 #include "qemu/async-teardown.h"
53 #include "hw/usb.h"
54 #include "hw/isa/isa.h"
55 #include "hw/scsi/scsi.h"
56 #include "hw/display/vga.h"
57 #include "hw/firmware/smbios.h"
58 #include "hw/acpi/acpi.h"
59 #include "hw/xen/xen.h"
60 #include "hw/loader.h"
61 #include "monitor/qdev.h"
62 #include "net/net.h"
63 #include "net/slirp.h"
64 #include "monitor/monitor.h"
65 #include "ui/console.h"
66 #include "ui/input.h"
67 #include "sysemu/sysemu.h"
68 #include "sysemu/numa.h"
69 #include "sysemu/hostmem.h"
70 #include "exec/gdbstub.h"
71 #include "gdbstub/enums.h"
72 #include "qemu/timer.h"
73 #include "chardev/char.h"
74 #include "qemu/bitmap.h"
75 #include "qemu/log.h"
76 #include "sysemu/blockdev.h"
77 #include "hw/block/block.h"
78 #include "hw/i386/x86.h"
79 #include "hw/i386/pc.h"
80 #include "migration/misc.h"
81 #include "migration/snapshot.h"
82 #include "sysemu/tpm.h"
83 #include "sysemu/dma.h"
84 #include "hw/audio/soundhw.h"
85 #include "audio/audio.h"
86 #include "sysemu/cpus.h"
87 #include "sysemu/cpu-timers.h"
88 #include "migration/colo.h"
89 #include "migration/postcopy-ram.h"
90 #include "sysemu/kvm.h"
91 #include "qapi/qobject-input-visitor.h"
92 #include "qemu/option.h"
93 #include "qemu/config-file.h"
94 #include "qemu/main-loop.h"
95 #ifdef CONFIG_VIRTFS
96 #include "fsdev/qemu-fsdev.h"
97 #endif
98 #include "sysemu/qtest.h"
99 #ifdef CONFIG_TCG
100 #include "tcg/perf.h"
101 #endif
103 #include "disas/disas.h"
105 #include "trace.h"
106 #include "trace/control.h"
107 #include "qemu/plugin.h"
108 #include "qemu/queue.h"
109 #include "sysemu/arch_init.h"
110 #include "exec/confidential-guest-support.h"
112 #include "ui/qemu-spice.h"
113 #include "qapi/string-input-visitor.h"
114 #include "qapi/opts-visitor.h"
115 #include "qapi/clone-visitor.h"
116 #include "qom/object_interfaces.h"
117 #include "semihosting/semihost.h"
118 #include "crypto/init.h"
119 #include "sysemu/replay.h"
120 #include "qapi/qapi-events-run-state.h"
121 #include "qapi/qapi-types-audio.h"
122 #include "qapi/qapi-visit-audio.h"
123 #include "qapi/qapi-visit-block-core.h"
124 #include "qapi/qapi-visit-compat.h"
125 #include "qapi/qapi-visit-machine.h"
126 #include "qapi/qapi-visit-ui.h"
127 #include "qapi/qapi-commands-block-core.h"
128 #include "qapi/qapi-commands-migration.h"
129 #include "qapi/qapi-commands-misc.h"
130 #include "qapi/qapi-visit-qom.h"
131 #include "qapi/qapi-commands-ui.h"
132 #include "block/qdict.h"
133 #include "qapi/qmp/qerror.h"
134 #include "sysemu/iothread.h"
135 #include "qemu/guest-random.h"
136 #include "qemu/keyval.h"
138 #define MAX_VIRTIO_CONSOLES 1
140 typedef struct BlockdevOptionsQueueEntry {
141 BlockdevOptions *bdo;
142 Location loc;
143 QSIMPLEQ_ENTRY(BlockdevOptionsQueueEntry) entry;
144 } BlockdevOptionsQueueEntry;
146 typedef QSIMPLEQ_HEAD(, BlockdevOptionsQueueEntry) BlockdevOptionsQueue;
148 typedef struct ObjectOption {
149 ObjectOptions *opts;
150 QTAILQ_ENTRY(ObjectOption) next;
151 } ObjectOption;
153 typedef struct DeviceOption {
154 QDict *opts;
155 Location loc;
156 QTAILQ_ENTRY(DeviceOption) next;
157 } DeviceOption;
159 static const char *cpu_option;
160 static const char *mem_path;
161 static const char *incoming;
162 static const char *loadvm;
163 static const char *accelerators;
164 static bool have_custom_ram_size;
165 static const char *ram_memdev_id;
166 static QDict *machine_opts_dict;
167 static QTAILQ_HEAD(, ObjectOption) object_opts = QTAILQ_HEAD_INITIALIZER(object_opts);
168 static QTAILQ_HEAD(, DeviceOption) device_opts = QTAILQ_HEAD_INITIALIZER(device_opts);
169 static int display_remote;
170 static int snapshot;
171 static bool preconfig_requested;
172 static QemuPluginList plugin_list = QTAILQ_HEAD_INITIALIZER(plugin_list);
173 static BlockdevOptionsQueue bdo_queue = QSIMPLEQ_HEAD_INITIALIZER(bdo_queue);
174 static bool nographic = false;
175 static int mem_prealloc; /* force preallocation of physical target memory */
176 static const char *vga_model = NULL;
177 static DisplayOptions dpy;
178 static int num_serial_hds;
179 static Chardev **serial_hds;
180 static const char *log_mask;
181 static const char *log_file;
182 static bool list_data_dirs;
183 static const char *qtest_chrdev;
184 static const char *qtest_log;
186 static int has_defaults = 1;
187 static int default_audio = 1;
188 static int default_serial = 1;
189 static int default_parallel = 1;
190 static int default_monitor = 1;
191 static int default_floppy = 1;
192 static int default_cdrom = 1;
193 static int default_sdcard = 1;
194 static int default_vga = 1;
195 static int default_net = 1;
197 static const struct {
198 const char *driver;
199 int *flag;
200 } default_list[] = {
201 { .driver = "xen-console", .flag = &default_serial },
202 { .driver = "isa-serial", .flag = &default_serial },
203 { .driver = "isa-parallel", .flag = &default_parallel },
204 { .driver = "isa-fdc", .flag = &default_floppy },
205 { .driver = "floppy", .flag = &default_floppy },
206 { .driver = "ide-cd", .flag = &default_cdrom },
207 { .driver = "ide-hd", .flag = &default_cdrom },
208 { .driver = "scsi-cd", .flag = &default_cdrom },
209 { .driver = "scsi-hd", .flag = &default_cdrom },
210 { .driver = "VGA", .flag = &default_vga },
211 { .driver = "isa-vga", .flag = &default_vga },
212 { .driver = "cirrus-vga", .flag = &default_vga },
213 { .driver = "isa-cirrus-vga", .flag = &default_vga },
214 { .driver = "vmware-svga", .flag = &default_vga },
215 { .driver = "qxl-vga", .flag = &default_vga },
216 { .driver = "virtio-vga", .flag = &default_vga },
217 { .driver = "ati-vga", .flag = &default_vga },
218 { .driver = "vhost-user-vga", .flag = &default_vga },
219 { .driver = "virtio-vga-gl", .flag = &default_vga },
220 { .driver = "virtio-vga-rutabaga", .flag = &default_vga },
223 static QemuOptsList qemu_rtc_opts = {
224 .name = "rtc",
225 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
226 .merge_lists = true,
227 .desc = {
229 .name = "base",
230 .type = QEMU_OPT_STRING,
232 .name = "clock",
233 .type = QEMU_OPT_STRING,
235 .name = "driftfix",
236 .type = QEMU_OPT_STRING,
238 { /* end of list */ }
242 static QemuOptsList qemu_option_rom_opts = {
243 .name = "option-rom",
244 .implied_opt_name = "romfile",
245 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
246 .desc = {
248 .name = "bootindex",
249 .type = QEMU_OPT_NUMBER,
250 }, {
251 .name = "romfile",
252 .type = QEMU_OPT_STRING,
254 { /* end of list */ }
258 static QemuOptsList qemu_accel_opts = {
259 .name = "accel",
260 .implied_opt_name = "accel",
261 .head = QTAILQ_HEAD_INITIALIZER(qemu_accel_opts.head),
262 .desc = {
264 * no elements => accept any
265 * sanity checking will happen later
266 * when setting accelerator properties
272 static QemuOptsList qemu_boot_opts = {
273 .name = "boot-opts",
274 .implied_opt_name = "order",
275 .merge_lists = true,
276 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
277 .desc = {
279 .name = "order",
280 .type = QEMU_OPT_STRING,
281 }, {
282 .name = "once",
283 .type = QEMU_OPT_STRING,
284 }, {
285 .name = "menu",
286 .type = QEMU_OPT_BOOL,
287 }, {
288 .name = "splash",
289 .type = QEMU_OPT_STRING,
290 }, {
291 .name = "splash-time",
292 .type = QEMU_OPT_NUMBER,
293 }, {
294 .name = "reboot-timeout",
295 .type = QEMU_OPT_NUMBER,
296 }, {
297 .name = "strict",
298 .type = QEMU_OPT_BOOL,
300 { /*End of list */ }
304 static QemuOptsList qemu_add_fd_opts = {
305 .name = "add-fd",
306 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
307 .desc = {
309 .name = "fd",
310 .type = QEMU_OPT_NUMBER,
311 .help = "file descriptor of which a duplicate is added to fd set",
313 .name = "set",
314 .type = QEMU_OPT_NUMBER,
315 .help = "ID of the fd set to add fd to",
317 .name = "opaque",
318 .type = QEMU_OPT_STRING,
319 .help = "free-form string used to describe fd",
321 { /* end of list */ }
325 static QemuOptsList qemu_object_opts = {
326 .name = "object",
327 .implied_opt_name = "qom-type",
328 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
329 .desc = {
334 static QemuOptsList qemu_tpmdev_opts = {
335 .name = "tpmdev",
336 .implied_opt_name = "type",
337 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
338 .desc = {
339 /* options are defined in the TPM backends */
340 { /* end of list */ }
344 static QemuOptsList qemu_overcommit_opts = {
345 .name = "overcommit",
346 .head = QTAILQ_HEAD_INITIALIZER(qemu_overcommit_opts.head),
347 .desc = {
349 .name = "mem-lock",
350 .type = QEMU_OPT_BOOL,
353 .name = "cpu-pm",
354 .type = QEMU_OPT_BOOL,
356 { /* end of list */ }
360 static QemuOptsList qemu_msg_opts = {
361 .name = "msg",
362 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
363 .desc = {
365 .name = "timestamp",
366 .type = QEMU_OPT_BOOL,
369 .name = "guest-name",
370 .type = QEMU_OPT_BOOL,
371 .help = "Prepends guest name for error messages but only if "
372 "-name guest is set otherwise option is ignored\n",
374 { /* end of list */ }
378 static QemuOptsList qemu_name_opts = {
379 .name = "name",
380 .implied_opt_name = "guest",
381 .merge_lists = true,
382 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
383 .desc = {
385 .name = "guest",
386 .type = QEMU_OPT_STRING,
387 .help = "Sets the name of the guest.\n"
388 "This name will be displayed in the SDL window caption.\n"
389 "The name will also be used for the VNC server",
390 }, {
391 .name = "process",
392 .type = QEMU_OPT_STRING,
393 .help = "Sets the name of the QEMU process, as shown in top etc",
394 }, {
395 .name = "debug-threads",
396 .type = QEMU_OPT_BOOL,
397 .help = "When enabled, name the individual threads; defaults off.\n"
398 "NOTE: The thread names are for debugging and not a\n"
399 "stable API.",
401 { /* End of list */ }
405 static QemuOptsList qemu_mem_opts = {
406 .name = "memory",
407 .implied_opt_name = "size",
408 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
409 .merge_lists = true,
410 .desc = {
412 .name = "size",
413 .type = QEMU_OPT_SIZE,
416 .name = "slots",
417 .type = QEMU_OPT_NUMBER,
420 .name = "maxmem",
421 .type = QEMU_OPT_SIZE,
423 { /* end of list */ }
427 static QemuOptsList qemu_icount_opts = {
428 .name = "icount",
429 .implied_opt_name = "shift",
430 .merge_lists = true,
431 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
432 .desc = {
434 .name = "shift",
435 .type = QEMU_OPT_STRING,
436 }, {
437 .name = "align",
438 .type = QEMU_OPT_BOOL,
439 }, {
440 .name = "sleep",
441 .type = QEMU_OPT_BOOL,
442 }, {
443 .name = "rr",
444 .type = QEMU_OPT_STRING,
445 }, {
446 .name = "rrfile",
447 .type = QEMU_OPT_STRING,
448 }, {
449 .name = "rrsnapshot",
450 .type = QEMU_OPT_STRING,
452 { /* end of list */ }
456 static QemuOptsList qemu_fw_cfg_opts = {
457 .name = "fw_cfg",
458 .implied_opt_name = "name",
459 .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
460 .desc = {
462 .name = "name",
463 .type = QEMU_OPT_STRING,
464 .help = "Sets the fw_cfg name of the blob to be inserted",
465 }, {
466 .name = "file",
467 .type = QEMU_OPT_STRING,
468 .help = "Sets the name of the file from which "
469 "the fw_cfg blob will be loaded",
470 }, {
471 .name = "string",
472 .type = QEMU_OPT_STRING,
473 .help = "Sets content of the blob to be inserted from a string",
474 }, {
475 .name = "gen_id",
476 .type = QEMU_OPT_STRING,
477 .help = "Sets id of the object generating the fw_cfg blob "
478 "to be inserted",
480 { /* end of list */ }
484 static QemuOptsList qemu_action_opts = {
485 .name = "action",
486 .merge_lists = true,
487 .head = QTAILQ_HEAD_INITIALIZER(qemu_action_opts.head),
488 .desc = {
490 .name = "shutdown",
491 .type = QEMU_OPT_STRING,
493 .name = "reboot",
494 .type = QEMU_OPT_STRING,
496 .name = "panic",
497 .type = QEMU_OPT_STRING,
499 .name = "watchdog",
500 .type = QEMU_OPT_STRING,
502 { /* end of list */ }
506 const char *qemu_get_vm_name(void)
508 return qemu_name;
511 static void default_driver_disable(const char *driver)
513 int i;
515 if (!driver) {
516 return;
519 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
520 if (strcmp(default_list[i].driver, driver) != 0)
521 continue;
522 *(default_list[i].flag) = 0;
526 static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
528 const char *driver = qemu_opt_get(opts, "driver");
530 default_driver_disable(driver);
531 return 0;
534 static void default_driver_check_json(void)
536 DeviceOption *opt;
538 QTAILQ_FOREACH(opt, &device_opts, next) {
539 const char *driver = qdict_get_try_str(opt->opts, "driver");
540 default_driver_disable(driver);
544 static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
546 const char *proc_name;
548 if (qemu_opt_get(opts, "debug-threads")) {
549 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
551 qemu_name = qemu_opt_get(opts, "guest");
553 proc_name = qemu_opt_get(opts, "process");
554 if (proc_name) {
555 os_set_proc_name(proc_name);
558 return 0;
561 bool defaults_enabled(void)
563 return has_defaults;
566 #ifndef _WIN32
567 static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
569 int fd, dupfd, flags;
570 int64_t fdset_id;
571 const char *fd_opaque = NULL;
572 AddfdInfo *fdinfo;
574 fd = qemu_opt_get_number(opts, "fd", -1);
575 fdset_id = qemu_opt_get_number(opts, "set", -1);
576 fd_opaque = qemu_opt_get(opts, "opaque");
578 if (fd < 0) {
579 error_setg(errp, "fd option is required and must be non-negative");
580 return -1;
583 if (fd <= STDERR_FILENO) {
584 error_setg(errp, "fd cannot be a standard I/O stream");
585 return -1;
589 * All fds inherited across exec() necessarily have FD_CLOEXEC
590 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
592 flags = fcntl(fd, F_GETFD);
593 if (flags == -1 || (flags & FD_CLOEXEC)) {
594 error_setg(errp, "fd is not valid or already in use");
595 return -1;
598 if (fdset_id < 0) {
599 error_setg(errp, "set option is required and must be non-negative");
600 return -1;
603 #ifdef F_DUPFD_CLOEXEC
604 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
605 #else
606 dupfd = dup(fd);
607 if (dupfd != -1) {
608 qemu_set_cloexec(dupfd);
610 #endif
611 if (dupfd == -1) {
612 error_setg(errp, "error duplicating fd: %s", strerror(errno));
613 return -1;
616 /* add the duplicate fd, and optionally the opaque string, to the fd set */
617 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, fd_opaque,
618 &error_abort);
619 g_free(fdinfo);
621 return 0;
624 static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
626 int fd;
628 fd = qemu_opt_get_number(opts, "fd", -1);
629 close(fd);
631 return 0;
633 #endif
635 /***********************************************************/
636 /* QEMU Block devices */
638 #define HD_OPTS "media=disk"
639 #define CDROM_OPTS "media=cdrom"
640 #define FD_OPTS ""
641 #define PFLASH_OPTS ""
642 #define MTD_OPTS ""
643 #define SD_OPTS ""
645 static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
647 BlockInterfaceType *block_default_type = opaque;
649 return drive_new(opts, *block_default_type, errp) == NULL;
652 static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
654 if (qemu_opt_get(opts, "snapshot") == NULL) {
655 qemu_opt_set(opts, "snapshot", "on", &error_abort);
657 return 0;
660 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
661 int index, const char *optstr)
663 QemuOpts *opts;
664 DriveInfo *dinfo;
666 if (!enable || drive_get_by_index(type, index)) {
667 return;
670 opts = drive_add(type, index, NULL, optstr);
671 if (snapshot) {
672 drive_enable_snapshot(NULL, opts, NULL);
675 dinfo = drive_new(opts, type, &error_abort);
676 dinfo->is_default = true;
680 static void configure_blockdev(BlockdevOptionsQueue *bdo_queue,
681 MachineClass *machine_class, int snapshot)
684 * If the currently selected machine wishes to override the
685 * units-per-bus property of its default HBA interface type, do so
686 * now.
688 if (machine_class->units_per_default_bus) {
689 override_max_devs(machine_class->block_default_type,
690 machine_class->units_per_default_bus);
693 /* open the virtual block devices */
694 while (!QSIMPLEQ_EMPTY(bdo_queue)) {
695 BlockdevOptionsQueueEntry *bdo = QSIMPLEQ_FIRST(bdo_queue);
697 QSIMPLEQ_REMOVE_HEAD(bdo_queue, entry);
698 loc_push_restore(&bdo->loc);
699 qmp_blockdev_add(bdo->bdo, &error_fatal);
700 loc_pop(&bdo->loc);
701 qapi_free_BlockdevOptions(bdo->bdo);
702 g_free(bdo);
704 if (snapshot) {
705 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
706 NULL, NULL);
708 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
709 &machine_class->block_default_type, &error_fatal)) {
710 /* We printed help */
711 exit(0);
714 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
715 CDROM_OPTS);
716 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
717 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
721 static QemuOptsList qemu_smp_opts = {
722 .name = "smp-opts",
723 .implied_opt_name = "cpus",
724 .merge_lists = true,
725 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
726 .desc = {
728 .name = "cpus",
729 .type = QEMU_OPT_NUMBER,
730 }, {
731 .name = "drawers",
732 .type = QEMU_OPT_NUMBER,
733 }, {
734 .name = "books",
735 .type = QEMU_OPT_NUMBER,
736 }, {
737 .name = "sockets",
738 .type = QEMU_OPT_NUMBER,
739 }, {
740 .name = "dies",
741 .type = QEMU_OPT_NUMBER,
742 }, {
743 .name = "clusters",
744 .type = QEMU_OPT_NUMBER,
745 }, {
746 .name = "modules",
747 .type = QEMU_OPT_NUMBER,
748 }, {
749 .name = "cores",
750 .type = QEMU_OPT_NUMBER,
751 }, {
752 .name = "threads",
753 .type = QEMU_OPT_NUMBER,
754 }, {
755 .name = "maxcpus",
756 .type = QEMU_OPT_NUMBER,
758 { /*End of list */ }
762 #if defined(CONFIG_POSIX)
763 static QemuOptsList qemu_run_with_opts = {
764 .name = "run-with",
765 .head = QTAILQ_HEAD_INITIALIZER(qemu_run_with_opts.head),
766 .desc = {
767 #if defined(CONFIG_LINUX)
769 .name = "async-teardown",
770 .type = QEMU_OPT_BOOL,
772 #endif
774 .name = "chroot",
775 .type = QEMU_OPT_STRING,
778 .name = "user",
779 .type = QEMU_OPT_STRING,
781 { /* end of list */ }
785 #define qemu_add_run_with_opts() qemu_add_opts(&qemu_run_with_opts)
787 #else
789 #define qemu_add_run_with_opts()
791 #endif /* CONFIG_POSIX */
793 static void realtime_init(void)
795 if (enable_mlock) {
796 if (os_mlock() < 0) {
797 error_report("locking memory failed");
798 exit(1);
804 static void configure_msg(QemuOpts *opts)
806 message_with_timestamp = qemu_opt_get_bool(opts, "timestamp", false);
807 error_with_guestname = qemu_opt_get_bool(opts, "guest-name", false);
811 /***********************************************************/
812 /* USB devices */
814 static int usb_device_add(const char *devname)
816 USBDevice *dev = NULL;
818 if (!machine_usb(current_machine)) {
819 return -1;
822 dev = usbdevice_create(devname);
823 if (!dev)
824 return -1;
826 return 0;
829 static int usb_parse(const char *cmdline)
831 int r;
832 r = usb_device_add(cmdline);
833 if (r < 0) {
834 error_report("could not add USB device '%s'", cmdline);
836 return r;
839 /***********************************************************/
840 /* machine registration */
842 static MachineClass *find_machine(const char *name, GSList *machines)
844 GSList *el;
846 for (el = machines; el; el = el->next) {
847 MachineClass *mc = el->data;
849 if (!strcmp(mc->name, name) || !g_strcmp0(mc->alias, name)) {
850 return mc;
854 return NULL;
857 static MachineClass *find_default_machine(GSList *machines)
859 GSList *el;
860 MachineClass *default_machineclass = NULL;
862 for (el = machines; el; el = el->next) {
863 MachineClass *mc = el->data;
865 if (mc->is_default) {
866 assert(default_machineclass == NULL && "Multiple default machines");
867 default_machineclass = mc;
871 return default_machineclass;
874 static void version(void)
876 printf("QEMU emulator version " QEMU_FULL_VERSION "\n"
877 QEMU_COPYRIGHT "\n");
880 static void help(int exitcode)
882 version();
883 printf("usage: %s [options] [disk_image]\n\n"
884 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
885 g_get_prgname());
887 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
888 if ((arch_mask) & arch_type) \
889 fputs(opt_help, stdout);
891 #define ARCHHEADING(text, arch_mask) \
892 if ((arch_mask) & arch_type) \
893 puts(stringify(text));
895 #define DEFHEADING(text) ARCHHEADING(text, QEMU_ARCH_ALL)
897 #include "qemu-options.def"
899 printf("\nDuring emulation, the following keys are useful:\n"
900 "ctrl-alt-f toggle full screen\n"
901 "ctrl-alt-n switch to virtual console 'n'\n"
902 "ctrl-alt-g toggle mouse and keyboard grab\n"
903 "\n"
904 "When using -nographic, press 'ctrl-a h' to get some help.\n"
905 "\n"
906 QEMU_HELP_BOTTOM "\n");
908 exit(exitcode);
911 enum {
913 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
914 opt_enum,
915 #define DEFHEADING(text)
916 #define ARCHHEADING(text, arch_mask)
918 #include "qemu-options.def"
921 #define HAS_ARG 0x0001
923 typedef struct QEMUOption {
924 const char *name;
925 int flags;
926 int index;
927 uint32_t arch_mask;
928 } QEMUOption;
930 static const QEMUOption qemu_options[] = {
931 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
933 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
934 { option, opt_arg, opt_enum, arch_mask },
935 #define DEFHEADING(text)
936 #define ARCHHEADING(text, arch_mask)
938 #include "qemu-options.def"
939 { /* end of list */ }
942 typedef struct VGAInterfaceInfo {
943 const char *opt_name; /* option name */
944 const char *name; /* human-readable name */
945 /* Class names indicating that support is available.
946 * If no class is specified, the interface is always available */
947 const char *class_names[2];
948 } VGAInterfaceInfo;
950 static const VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
951 [VGA_NONE] = {
952 .opt_name = "none",
953 .name = "no graphic card",
955 [VGA_STD] = {
956 .opt_name = "std",
957 .name = "standard VGA",
958 .class_names = { "VGA", "isa-vga" },
960 [VGA_CIRRUS] = {
961 .opt_name = "cirrus",
962 .name = "Cirrus VGA",
963 .class_names = { "cirrus-vga", "isa-cirrus-vga" },
965 [VGA_VMWARE] = {
966 .opt_name = "vmware",
967 .name = "VMWare SVGA",
968 .class_names = { "vmware-svga" },
970 [VGA_VIRTIO] = {
971 .opt_name = "virtio",
972 .name = "Virtio VGA",
973 .class_names = { "virtio-vga" },
975 [VGA_QXL] = {
976 .opt_name = "qxl",
977 .name = "QXL VGA",
978 .class_names = { "qxl-vga" },
980 [VGA_TCX] = {
981 .opt_name = "tcx",
982 .name = "TCX framebuffer",
983 .class_names = { "sun-tcx" },
985 [VGA_CG3] = {
986 .opt_name = "cg3",
987 .name = "CG3 framebuffer",
988 .class_names = { "cgthree" },
990 #ifdef CONFIG_XEN_BACKEND
991 [VGA_XENFB] = {
992 .opt_name = "xenfb",
993 .name = "Xen paravirtualized framebuffer",
995 #endif
998 static bool vga_interface_available(VGAInterfaceType t)
1000 const VGAInterfaceInfo *ti = &vga_interfaces[t];
1002 assert(t < VGA_TYPE_MAX);
1004 if (!ti->class_names[0] || module_object_class_by_name(ti->class_names[0])) {
1005 return true;
1008 if (ti->class_names[1] && module_object_class_by_name(ti->class_names[1])) {
1009 return true;
1012 return false;
1015 static const char *
1016 get_default_vga_model(const MachineClass *machine_class)
1018 if (machine_class->default_display) {
1019 for (int t = 0; t < VGA_TYPE_MAX; t++) {
1020 const VGAInterfaceInfo *ti = &vga_interfaces[t];
1022 if (ti->opt_name && vga_interface_available(t) &&
1023 g_str_equal(ti->opt_name, machine_class->default_display)) {
1024 return machine_class->default_display;
1028 warn_report_once("Default display '%s' is not available in this binary",
1029 machine_class->default_display);
1030 return NULL;
1031 } else if (vga_interface_available(VGA_CIRRUS)) {
1032 return "cirrus";
1033 } else if (vga_interface_available(VGA_STD)) {
1034 return "std";
1037 return NULL;
1040 static void select_vgahw(const MachineClass *machine_class, const char *p)
1042 const char *opts;
1043 int t;
1045 if (g_str_equal(p, "help")) {
1046 const char *def = get_default_vga_model(machine_class);
1048 for (t = 0; t < VGA_TYPE_MAX; t++) {
1049 const VGAInterfaceInfo *ti = &vga_interfaces[t];
1051 if (vga_interface_available(t) && ti->opt_name) {
1052 printf("%-20s %s%s\n", ti->opt_name, ti->name ?: "",
1053 (def && g_str_equal(ti->opt_name, def)) ?
1054 " (default)" : "");
1057 exit(0);
1060 assert(vga_interface_type == VGA_NONE);
1061 for (t = 0; t < VGA_TYPE_MAX; t++) {
1062 const VGAInterfaceInfo *ti = &vga_interfaces[t];
1063 if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
1064 if (!vga_interface_available(t)) {
1065 error_report("%s not available", ti->name);
1066 exit(1);
1068 vga_interface_type = t;
1069 break;
1072 if (t == VGA_TYPE_MAX) {
1073 invalid_vga:
1074 error_report("unknown vga type: %s", p);
1075 exit(1);
1077 while (*opts) {
1078 const char *nextopt;
1080 if (strstart(opts, ",retrace=", &nextopt)) {
1081 opts = nextopt;
1082 if (strstart(opts, "dumb", &nextopt))
1083 vga_retrace_method = VGA_RETRACE_DUMB;
1084 else if (strstart(opts, "precise", &nextopt))
1085 vga_retrace_method = VGA_RETRACE_PRECISE;
1086 else goto invalid_vga;
1087 } else goto invalid_vga;
1088 opts = nextopt;
1092 static void parse_display_qapi(const char *str)
1094 DisplayOptions *opts;
1095 Visitor *v;
1097 v = qobject_input_visitor_new_str(str, "type", &error_fatal);
1099 visit_type_DisplayOptions(v, NULL, &opts, &error_fatal);
1100 QAPI_CLONE_MEMBERS(DisplayOptions, &dpy, opts);
1102 qapi_free_DisplayOptions(opts);
1103 visit_free(v);
1106 DisplayOptions *qmp_query_display_options(Error **errp)
1108 return QAPI_CLONE(DisplayOptions, &dpy);
1111 static void parse_display(const char *p)
1113 if (is_help_option(p)) {
1114 qemu_display_help();
1115 exit(0);
1118 #ifdef CONFIG_VNC
1119 const char *opts;
1121 if (strstart(p, "vnc", &opts)) {
1123 * vnc isn't a (local) DisplayType but a protocol for remote
1124 * display access.
1126 if (*opts == '=') {
1127 vnc_parse(opts + 1);
1128 display_remote++;
1129 } else {
1130 error_report("VNC requires a display argument vnc=<display>");
1131 exit(1);
1133 return;
1135 #endif
1137 parse_display_qapi(p);
1140 static inline bool nonempty_str(const char *str)
1142 return str && *str;
1145 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
1147 gchar *buf;
1148 size_t size;
1149 const char *name, *file, *str, *gen_id;
1150 FWCfgState *fw_cfg = (FWCfgState *) opaque;
1152 if (fw_cfg == NULL) {
1153 error_setg(errp, "fw_cfg device not available");
1154 return -1;
1156 name = qemu_opt_get(opts, "name");
1157 file = qemu_opt_get(opts, "file");
1158 str = qemu_opt_get(opts, "string");
1159 gen_id = qemu_opt_get(opts, "gen_id");
1161 /* we need the name, and exactly one of: file, content string, gen_id */
1162 if (!nonempty_str(name) ||
1163 nonempty_str(file) + nonempty_str(str) + nonempty_str(gen_id) != 1) {
1164 error_setg(errp, "name, plus exactly one of file,"
1165 " string and gen_id, are needed");
1166 return -1;
1168 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
1169 error_setg(errp, "name too long (max. %d char)",
1170 FW_CFG_MAX_FILE_PATH - 1);
1171 return -1;
1173 if (nonempty_str(gen_id)) {
1175 * In this particular case where the content is populated
1176 * internally, the "etc/" namespace protection is relaxed,
1177 * so do not emit a warning.
1179 } else if (strncmp(name, "opt/", 4) != 0) {
1180 warn_report("externally provided fw_cfg item names "
1181 "should be prefixed with \"opt/\"");
1183 if (nonempty_str(str)) {
1184 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
1185 buf = g_memdup(str, size);
1186 } else if (nonempty_str(gen_id)) {
1187 if (!fw_cfg_add_from_generator(fw_cfg, name, gen_id, errp)) {
1188 return -1;
1190 return 0;
1191 } else {
1192 GError *err = NULL;
1193 if (!g_file_get_contents(file, &buf, &size, &err)) {
1194 error_setg(errp, "can't load %s: %s", file, err->message);
1195 g_error_free(err);
1196 return -1;
1199 /* For legacy, keep user files in a specific global order. */
1200 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
1201 fw_cfg_add_file(fw_cfg, name, buf, size);
1202 fw_cfg_reset_order_override(fw_cfg);
1203 return 0;
1206 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
1208 return qdev_device_help(opts);
1211 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
1213 DeviceState *dev;
1215 dev = qdev_device_add(opts, errp);
1216 if (!dev && *errp) {
1217 error_report_err(*errp);
1218 return -1;
1219 } else if (dev) {
1220 object_unref(OBJECT(dev));
1222 return 0;
1225 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
1227 Error *local_err = NULL;
1229 if (!qemu_chr_new_from_opts(opts, NULL, &local_err)) {
1230 if (local_err) {
1231 error_propagate(errp, local_err);
1232 return -1;
1234 exit(0);
1236 return 0;
1239 #ifdef CONFIG_VIRTFS
1240 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
1242 return qemu_fsdev_add(opts, errp);
1244 #endif
1246 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
1248 return monitor_init_opts(opts, errp);
1251 static void monitor_parse(const char *str, const char *mode, bool pretty)
1253 static int monitor_device_index = 0;
1254 QemuOpts *opts;
1255 const char *p;
1256 char label[32];
1258 if (strstart(str, "chardev:", &p)) {
1259 snprintf(label, sizeof(label), "%s", p);
1260 } else {
1261 snprintf(label, sizeof(label), "compat_monitor%d",
1262 monitor_device_index);
1263 opts = qemu_chr_parse_compat(label, str, true);
1264 if (!opts) {
1265 error_report("parse error: %s", str);
1266 exit(1);
1270 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
1271 qemu_opt_set(opts, "mode", mode, &error_abort);
1272 qemu_opt_set(opts, "chardev", label, &error_abort);
1273 if (!strcmp(mode, "control")) {
1274 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
1275 } else {
1276 assert(pretty == false);
1278 monitor_device_index++;
1281 struct device_config {
1282 enum {
1283 DEV_USB, /* -usbdevice */
1284 DEV_SERIAL, /* -serial */
1285 DEV_PARALLEL, /* -parallel */
1286 DEV_DEBUGCON, /* -debugcon */
1287 DEV_GDB, /* -gdb, -s */
1288 DEV_SCLP, /* s390 sclp */
1289 } type;
1290 const char *cmdline;
1291 Location loc;
1292 QTAILQ_ENTRY(device_config) next;
1295 static QTAILQ_HEAD(, device_config) device_configs =
1296 QTAILQ_HEAD_INITIALIZER(device_configs);
1298 static void add_device_config(int type, const char *cmdline)
1300 struct device_config *conf;
1302 conf = g_malloc0(sizeof(*conf));
1303 conf->type = type;
1304 conf->cmdline = cmdline;
1305 loc_save(&conf->loc);
1306 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1309 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1311 struct device_config *conf;
1312 int rc;
1314 QTAILQ_FOREACH(conf, &device_configs, next) {
1315 if (conf->type != type)
1316 continue;
1317 loc_push_restore(&conf->loc);
1318 rc = func(conf->cmdline);
1319 loc_pop(&conf->loc);
1320 if (rc) {
1321 return rc;
1324 return 0;
1327 static void qemu_disable_default_devices(void)
1329 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1331 default_driver_check_json();
1332 qemu_opts_foreach(qemu_find_opts("device"),
1333 default_driver_check, NULL, NULL);
1334 qemu_opts_foreach(qemu_find_opts("global"),
1335 default_driver_check, NULL, NULL);
1337 if (!vga_model && !default_vga) {
1338 vga_interface_type = VGA_DEVICE;
1339 vga_interface_created = true;
1341 if (!has_defaults || machine_class->no_serial) {
1342 default_serial = 0;
1344 if (!has_defaults || machine_class->no_parallel) {
1345 default_parallel = 0;
1347 if (!has_defaults || machine_class->no_floppy) {
1348 default_floppy = 0;
1350 if (!has_defaults || machine_class->no_cdrom) {
1351 default_cdrom = 0;
1353 if (!has_defaults || machine_class->no_sdcard) {
1354 default_sdcard = 0;
1356 if (!has_defaults) {
1357 default_audio = 0;
1358 default_monitor = 0;
1359 default_net = 0;
1360 default_vga = 0;
1361 } else {
1362 if (default_net && machine_class->default_nic &&
1363 !module_object_class_by_name(machine_class->default_nic)) {
1364 warn_report("Default NIC '%s' is not available in this binary",
1365 machine_class->default_nic);
1366 default_net = 0;
1371 static void qemu_setup_display(void)
1373 if (dpy.type == DISPLAY_TYPE_DEFAULT && !display_remote) {
1374 if (!qemu_display_find_default(&dpy)) {
1375 dpy.type = DISPLAY_TYPE_NONE;
1376 #if defined(CONFIG_VNC)
1377 vnc_parse("localhost:0,to=99,id=default");
1378 display_remote++;
1379 #endif
1382 if (dpy.type == DISPLAY_TYPE_DEFAULT) {
1383 dpy.type = DISPLAY_TYPE_NONE;
1386 qemu_display_early_init(&dpy);
1389 static void qemu_create_default_devices(void)
1391 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1392 const char *vc = qemu_display_get_vc(&dpy);
1394 if (is_daemonized()) {
1395 /* According to documentation and historically, -nographic redirects
1396 * serial port, parallel port and monitor to stdio, which does not work
1397 * with -daemonize. We can redirect these to null instead, but since
1398 * -nographic is legacy, let's just error out.
1399 * We disallow -nographic only if all other ports are not redirected
1400 * explicitly, to not break existing legacy setups which uses
1401 * -nographic _and_ redirects all ports explicitly - this is valid
1402 * usage, -nographic is just a no-op in this case.
1404 if (nographic
1405 && (default_parallel || default_serial || default_monitor)) {
1406 error_report("-nographic cannot be used with -daemonize");
1407 exit(1);
1411 if (nographic) {
1412 if (default_parallel) {
1413 add_device_config(DEV_PARALLEL, "null");
1415 if (default_serial && default_monitor) {
1416 add_device_config(DEV_SERIAL, "mon:stdio");
1417 } else {
1418 if (default_serial) {
1419 add_device_config(DEV_SERIAL, "stdio");
1421 if (default_monitor) {
1422 monitor_parse("stdio", "readline", false);
1425 } else {
1426 if (default_serial) {
1427 add_device_config(DEV_SERIAL, vc ?: "null");
1429 if (default_parallel) {
1430 add_device_config(DEV_PARALLEL, vc ?: "null");
1432 if (default_monitor && vc) {
1433 monitor_parse(vc, "readline", false);
1437 if (default_net) {
1438 QemuOptsList *net = qemu_find_opts("net");
1439 qemu_opts_parse(net, "nic", true, &error_abort);
1440 #ifdef CONFIG_SLIRP
1441 qemu_opts_parse(net, "user", true, &error_abort);
1442 #endif
1445 /* If no default VGA is requested, the default is "none". */
1446 if (default_vga) {
1447 vga_model = get_default_vga_model(machine_class);
1449 if (vga_model) {
1450 select_vgahw(machine_class, vga_model);
1454 static int serial_parse(const char *devname)
1456 int index = num_serial_hds;
1458 serial_hds = g_renew(Chardev *, serial_hds, index + 1);
1460 if (strcmp(devname, "none") == 0) {
1461 /* Don't allocate a serial device for this index */
1462 serial_hds[index] = NULL;
1463 } else {
1464 char label[32];
1465 snprintf(label, sizeof(label), "serial%d", index);
1467 serial_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
1468 if (!serial_hds[index]) {
1469 error_report("could not connect serial device"
1470 " to character backend '%s'", devname);
1471 return -1;
1474 num_serial_hds++;
1475 return 0;
1478 Chardev *serial_hd(int i)
1480 assert(i >= 0);
1481 if (i < num_serial_hds) {
1482 return serial_hds[i];
1484 return NULL;
1487 static int parallel_parse(const char *devname)
1489 static int index = 0;
1490 char label[32];
1492 if (strcmp(devname, "none") == 0)
1493 return 0;
1494 if (index == MAX_PARALLEL_PORTS) {
1495 error_report("too many parallel ports");
1496 exit(1);
1498 snprintf(label, sizeof(label), "parallel%d", index);
1499 parallel_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
1500 if (!parallel_hds[index]) {
1501 error_report("could not connect parallel device"
1502 " to character backend '%s'", devname);
1503 return -1;
1505 index++;
1506 return 0;
1509 static int debugcon_parse(const char *devname)
1511 QemuOpts *opts;
1513 if (!qemu_chr_new_mux_mon("debugcon", devname, NULL)) {
1514 error_report("invalid character backend '%s'", devname);
1515 exit(1);
1517 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
1518 if (!opts) {
1519 error_report("already have a debugcon device");
1520 exit(1);
1522 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
1523 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
1524 return 0;
1527 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
1529 const MachineClass *mc1 = a, *mc2 = b;
1530 int res;
1532 if (mc1->family == NULL) {
1533 if (mc2->family == NULL) {
1534 /* Compare standalone machine types against each other; they sort
1535 * in increasing order.
1537 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
1538 object_class_get_name(OBJECT_CLASS(mc2)));
1541 /* Standalone machine types sort after families. */
1542 return 1;
1545 if (mc2->family == NULL) {
1546 /* Families sort before standalone machine types. */
1547 return -1;
1550 /* Families sort between each other alphabetically increasingly. */
1551 res = strcmp(mc1->family, mc2->family);
1552 if (res != 0) {
1553 return res;
1556 /* Within the same family, machine types sort in decreasing order. */
1557 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
1558 object_class_get_name(OBJECT_CLASS(mc1)));
1561 static void machine_help_func(const QDict *qdict)
1563 g_autoptr(GSList) machines = NULL;
1564 GSList *el;
1565 const char *type = qdict_get_try_str(qdict, "type");
1567 machines = object_class_get_list(TYPE_MACHINE, false);
1568 if (type) {
1569 ObjectClass *machine_class = OBJECT_CLASS(find_machine(type, machines));
1570 if (machine_class) {
1571 type_print_class_properties(object_class_get_name(machine_class));
1572 return;
1576 printf("Supported machines are:\n");
1577 machines = g_slist_sort(machines, machine_class_cmp);
1578 for (el = machines; el; el = el->next) {
1579 MachineClass *mc = el->data;
1580 if (mc->alias) {
1581 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
1583 printf("%-20s %s%s%s\n", mc->name, mc->desc,
1584 mc->is_default ? " (default)" : "",
1585 mc->deprecation_reason ? " (deprecated)" : "");
1589 static void
1590 machine_merge_property(const char *propname, QDict *prop, Error **errp)
1592 QDict *opts;
1594 opts = qdict_new();
1595 /* Preserve the caller's reference to prop. */
1596 qobject_ref(prop);
1597 qdict_put(opts, propname, prop);
1598 keyval_merge(machine_opts_dict, opts, errp);
1599 qobject_unref(opts);
1602 static void
1603 machine_parse_property_opt(QemuOptsList *opts_list, const char *propname,
1604 const char *arg)
1606 QDict *prop = NULL;
1607 bool help = false;
1609 prop = keyval_parse(arg, opts_list->implied_opt_name, &help, &error_fatal);
1610 if (help) {
1611 qemu_opts_print_help(opts_list, true);
1612 exit(0);
1614 machine_merge_property(propname, prop, &error_fatal);
1615 qobject_unref(prop);
1618 static const char *pid_file;
1619 struct UnlinkPidfileNotifier {
1620 Notifier notifier;
1621 char *pid_file_realpath;
1623 static struct UnlinkPidfileNotifier qemu_unlink_pidfile_notifier;
1625 static void qemu_unlink_pidfile(Notifier *n, void *data)
1627 struct UnlinkPidfileNotifier *upn;
1629 upn = DO_UPCAST(struct UnlinkPidfileNotifier, notifier, n);
1630 unlink(upn->pid_file_realpath);
1633 static const QEMUOption *lookup_opt(int argc, char **argv,
1634 const char **poptarg, int *poptind)
1636 const QEMUOption *popt;
1637 int optind = *poptind;
1638 char *r = argv[optind];
1639 const char *optarg;
1641 loc_set_cmdline(argv, optind, 1);
1642 optind++;
1643 /* Treat --foo the same as -foo. */
1644 if (r[1] == '-')
1645 r++;
1646 popt = qemu_options;
1647 for(;;) {
1648 if (!popt->name) {
1649 error_report("invalid option");
1650 exit(1);
1652 if (!strcmp(popt->name, r + 1))
1653 break;
1654 popt++;
1656 if (popt->flags & HAS_ARG) {
1657 if (optind >= argc) {
1658 error_report("requires an argument");
1659 exit(1);
1661 optarg = argv[optind++];
1662 loc_set_cmdline(argv, optind - 2, 2);
1663 } else {
1664 optarg = NULL;
1667 *poptarg = optarg;
1668 *poptind = optind;
1670 return popt;
1673 static MachineClass *select_machine(QDict *qdict, Error **errp)
1675 ERRP_GUARD();
1676 const char *machine_type = qdict_get_try_str(qdict, "type");
1677 g_autoptr(GSList) machines = object_class_get_list(TYPE_MACHINE, false);
1678 MachineClass *machine_class = NULL;
1680 if (machine_type) {
1681 machine_class = find_machine(machine_type, machines);
1682 qdict_del(qdict, "type");
1683 if (!machine_class) {
1684 error_setg(errp, "unsupported machine type: \"%s\"", optarg);
1686 } else {
1687 machine_class = find_default_machine(machines);
1688 if (!machine_class) {
1689 error_setg(errp, "No machine specified, and there is no default");
1693 if (!machine_class) {
1694 error_append_hint(errp,
1695 "Use -machine help to list supported machines\n");
1697 return machine_class;
1700 static int object_parse_property_opt(Object *obj,
1701 const char *name, const char *value,
1702 const char *skip, Error **errp)
1704 if (g_str_equal(name, skip)) {
1705 return 0;
1708 if (!object_property_parse(obj, name, value, errp)) {
1709 return -1;
1712 return 0;
1715 /* *Non*recursively replace underscores with dashes in QDict keys. */
1716 static void keyval_dashify(QDict *qdict, Error **errp)
1718 const QDictEntry *ent, *next;
1719 char *p;
1721 for (ent = qdict_first(qdict); ent; ent = next) {
1722 g_autofree char *new_key = NULL;
1724 next = qdict_next(qdict, ent);
1725 if (!strchr(ent->key, '_')) {
1726 continue;
1728 new_key = g_strdup(ent->key);
1729 for (p = new_key; *p; p++) {
1730 if (*p == '_') {
1731 *p = '-';
1734 if (qdict_haskey(qdict, new_key)) {
1735 error_setg(errp, "Conflict between '%s' and '%s'", ent->key, new_key);
1736 return;
1738 qobject_ref(ent->value);
1739 qdict_put_obj(qdict, new_key, ent->value);
1740 qdict_del(qdict, ent->key);
1744 static void qemu_apply_legacy_machine_options(QDict *qdict)
1746 const char *value;
1747 QObject *prop;
1749 keyval_dashify(qdict, &error_fatal);
1751 /* Legacy options do not correspond to MachineState properties. */
1752 value = qdict_get_try_str(qdict, "accel");
1753 if (value) {
1754 accelerators = g_strdup(value);
1755 qdict_del(qdict, "accel");
1758 value = qdict_get_try_str(qdict, "igd-passthru");
1759 if (value) {
1760 object_register_sugar_prop(ACCEL_CLASS_NAME("xen"), "igd-passthru", value,
1761 false);
1762 qdict_del(qdict, "igd-passthru");
1765 value = qdict_get_try_str(qdict, "kvm-shadow-mem");
1766 if (value) {
1767 object_register_sugar_prop(ACCEL_CLASS_NAME("kvm"), "kvm-shadow-mem", value,
1768 false);
1769 qdict_del(qdict, "kvm-shadow-mem");
1772 value = qdict_get_try_str(qdict, "kernel-irqchip");
1773 if (value) {
1774 object_register_sugar_prop(ACCEL_CLASS_NAME("kvm"), "kernel-irqchip", value,
1775 false);
1776 object_register_sugar_prop(ACCEL_CLASS_NAME("whpx"), "kernel-irqchip", value,
1777 false);
1778 qdict_del(qdict, "kernel-irqchip");
1781 value = qdict_get_try_str(qdict, "memory-backend");
1782 if (value) {
1783 if (mem_path) {
1784 error_report("'-mem-path' can't be used together with"
1785 "'-machine memory-backend'");
1786 exit(EXIT_FAILURE);
1789 /* Resolved later. */
1790 ram_memdev_id = g_strdup(value);
1791 qdict_del(qdict, "memory-backend");
1794 prop = qdict_get(qdict, "memory");
1795 if (prop) {
1796 have_custom_ram_size =
1797 qobject_type(prop) == QTYPE_QDICT &&
1798 qdict_haskey(qobject_to(QDict, prop), "size");
1802 static void object_option_foreach_add(bool (*type_opt_predicate)(const char *))
1804 ObjectOption *opt, *next;
1806 QTAILQ_FOREACH_SAFE(opt, &object_opts, next, next) {
1807 const char *type = ObjectType_str(opt->opts->qom_type);
1808 if (type_opt_predicate(type)) {
1809 user_creatable_add_qapi(opt->opts, &error_fatal);
1810 qapi_free_ObjectOptions(opt->opts);
1811 QTAILQ_REMOVE(&object_opts, opt, next);
1812 g_free(opt);
1817 static void object_option_add_visitor(Visitor *v)
1819 ObjectOption *opt = g_new0(ObjectOption, 1);
1820 visit_type_ObjectOptions(v, NULL, &opt->opts, &error_fatal);
1821 QTAILQ_INSERT_TAIL(&object_opts, opt, next);
1824 static void object_option_parse(const char *str)
1826 QemuOpts *opts;
1827 const char *type;
1828 Visitor *v;
1830 if (str[0] == '{') {
1831 QObject *obj = qobject_from_json(str, &error_fatal);
1833 v = qobject_input_visitor_new(obj);
1834 qobject_unref(obj);
1835 } else {
1836 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
1837 str, true);
1838 if (!opts) {
1839 exit(1);
1842 type = qemu_opt_get(opts, "qom-type");
1843 if (!type) {
1844 error_setg(&error_fatal, QERR_MISSING_PARAMETER, "qom-type");
1846 if (user_creatable_print_help(type, opts)) {
1847 exit(0);
1850 v = opts_visitor_new(opts);
1853 object_option_add_visitor(v);
1854 visit_free(v);
1858 * Very early object creation, before the sandbox options have been activated.
1860 static bool object_create_pre_sandbox(const char *type)
1863 * Objects should in general not get initialized "too early" without
1864 * a reason. If you add one, state the reason in a comment!
1868 * Reason: -sandbox on,resourcecontrol=deny disallows setting CPU
1869 * affinity of threads.
1871 if (g_str_equal(type, "thread-context")) {
1872 return true;
1875 return false;
1879 * Initial object creation happens before all other
1880 * QEMU data types are created. The majority of objects
1881 * can be created at this point. The rng-egd object
1882 * cannot be created here, as it depends on the chardev
1883 * already existing.
1885 static bool object_create_early(const char *type)
1888 * Objects should not be made "delayed" without a reason. If you
1889 * add one, state the reason in a comment!
1892 /* Reason: already created. */
1893 if (object_create_pre_sandbox(type)) {
1894 return false;
1897 /* Reason: property "chardev" */
1898 if (g_str_equal(type, "rng-egd") ||
1899 g_str_equal(type, "qtest")) {
1900 return false;
1903 #if defined(CONFIG_VHOST_USER) && defined(CONFIG_LINUX)
1904 /* Reason: cryptodev-vhost-user property "chardev" */
1905 if (g_str_equal(type, "cryptodev-vhost-user")) {
1906 return false;
1908 #endif
1910 /* Reason: vhost-user-blk-server property "node-name" */
1911 if (g_str_equal(type, "vhost-user-blk-server")) {
1912 return false;
1915 * Reason: filter-* property "netdev" etc.
1917 if (g_str_equal(type, "filter-buffer") ||
1918 g_str_equal(type, "filter-dump") ||
1919 g_str_equal(type, "filter-mirror") ||
1920 g_str_equal(type, "filter-redirector") ||
1921 g_str_equal(type, "colo-compare") ||
1922 g_str_equal(type, "filter-rewriter") ||
1923 g_str_equal(type, "filter-replay")) {
1924 return false;
1928 * Allocation of large amounts of memory may delay
1929 * chardev initialization for too long, and trigger timeouts
1930 * on software that waits for a monitor socket to be created
1932 if (g_str_has_prefix(type, "memory-backend-")) {
1933 return false;
1936 return true;
1939 static void qemu_apply_machine_options(QDict *qdict)
1941 object_set_properties_from_keyval(OBJECT(current_machine), qdict, false, &error_fatal);
1943 if (semihosting_enabled(false) && !semihosting_get_argc()) {
1944 /* fall back to the -kernel/-append */
1945 semihosting_arg_fallback(current_machine->kernel_filename, current_machine->kernel_cmdline);
1948 if (current_machine->smp.cpus > 1) {
1949 replay_add_blocker("multiple CPUs");
1953 static void qemu_create_early_backends(void)
1955 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1956 #if defined(CONFIG_SDL)
1957 const bool use_sdl = (dpy.type == DISPLAY_TYPE_SDL);
1958 #else
1959 const bool use_sdl = false;
1960 #endif
1961 #if defined(CONFIG_GTK)
1962 const bool use_gtk = (dpy.type == DISPLAY_TYPE_GTK);
1963 #else
1964 const bool use_gtk = false;
1965 #endif
1967 if (dpy.has_window_close && !use_gtk && !use_sdl) {
1968 error_report("window-close is only valid for GTK and SDL, "
1969 "ignoring option");
1972 qemu_console_early_init();
1974 if (dpy.has_gl && dpy.gl != DISPLAYGL_MODE_OFF && display_opengl == 0) {
1975 #if defined(CONFIG_OPENGL)
1976 error_report("OpenGL is not supported by the display");
1977 #else
1978 error_report("OpenGL support is disabled");
1979 #endif
1980 exit(1);
1983 object_option_foreach_add(object_create_early);
1985 /* spice needs the timers to be initialized by this point */
1986 /* spice must initialize before audio as it changes the default audiodev */
1987 /* spice must initialize before chardevs (for spicevmc and spiceport) */
1988 qemu_spice.init();
1990 qemu_opts_foreach(qemu_find_opts("chardev"),
1991 chardev_init_func, NULL, &error_fatal);
1993 #ifdef CONFIG_VIRTFS
1994 qemu_opts_foreach(qemu_find_opts("fsdev"),
1995 fsdev_init_func, NULL, &error_fatal);
1996 #endif
1999 * Note: we need to create audio and block backends before
2000 * setting machine properties, so they can be referred to.
2002 configure_blockdev(&bdo_queue, machine_class, snapshot);
2003 audio_init_audiodevs();
2004 if (default_audio) {
2005 audio_create_default_audiodevs();
2011 * The remainder of object creation happens after the
2012 * creation of chardev, fsdev, net clients and device data types.
2014 static bool object_create_late(const char *type)
2016 return !object_create_early(type) && !object_create_pre_sandbox(type);
2019 static void qemu_create_late_backends(void)
2021 if (qtest_chrdev) {
2022 qtest_server_init(qtest_chrdev, qtest_log, &error_fatal);
2025 net_init_clients();
2027 object_option_foreach_add(object_create_late);
2030 * Wait for any outstanding memory prealloc from created memory
2031 * backends to complete.
2033 if (!qemu_finish_async_prealloc_mem(&error_fatal)) {
2034 exit(1);
2037 if (tpm_init() < 0) {
2038 exit(1);
2041 qemu_opts_foreach(qemu_find_opts("mon"),
2042 mon_init_func, NULL, &error_fatal);
2044 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
2045 exit(1);
2046 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
2047 exit(1);
2048 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
2049 exit(1);
2051 /* now chardevs have been created we may have semihosting to connect */
2052 qemu_semihosting_chardev_init();
2055 static void qemu_resolve_machine_memdev(void)
2057 if (ram_memdev_id) {
2058 Object *backend;
2059 ram_addr_t backend_size;
2061 backend = object_resolve_path_type(ram_memdev_id,
2062 TYPE_MEMORY_BACKEND, NULL);
2063 if (!backend) {
2064 error_report("Memory backend '%s' not found", ram_memdev_id);
2065 exit(EXIT_FAILURE);
2067 if (!have_custom_ram_size) {
2068 backend_size = object_property_get_uint(backend, "size", &error_abort);
2069 current_machine->ram_size = backend_size;
2071 object_property_set_link(OBJECT(current_machine),
2072 "memory-backend", backend, &error_fatal);
2076 static void parse_memory_options(void)
2078 QemuOpts *opts = qemu_find_opts_singleton("memory");
2079 QDict *dict, *prop;
2080 const char *mem_str;
2081 Location loc;
2083 loc_push_none(&loc);
2084 qemu_opts_loc_restore(opts);
2086 prop = qdict_new();
2088 if (qemu_opt_get_size(opts, "size", 0) != 0) {
2089 /* Fix up legacy suffix-less format */
2090 mem_str = qemu_opt_get(opts, "size");
2091 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2092 g_autofree char *mib_str = g_strdup_printf("%sM", mem_str);
2093 qdict_put_str(prop, "size", mib_str);
2094 } else {
2095 qdict_put_str(prop, "size", mem_str);
2099 if (qemu_opt_get(opts, "maxmem")) {
2100 qdict_put_str(prop, "max-size", qemu_opt_get(opts, "maxmem"));
2102 if (qemu_opt_get(opts, "slots")) {
2103 qdict_put_str(prop, "slots", qemu_opt_get(opts, "slots"));
2106 dict = qdict_new();
2107 qdict_put(dict, "memory", prop);
2108 keyval_merge(machine_opts_dict, dict, &error_fatal);
2109 qobject_unref(dict);
2110 loc_pop(&loc);
2113 static void qemu_create_machine(QDict *qdict)
2115 MachineClass *machine_class = select_machine(qdict, &error_fatal);
2116 object_set_machine_compat_props(machine_class->compat_props);
2118 current_machine = MACHINE(object_new_with_class(OBJECT_CLASS(machine_class)));
2119 object_property_add_child(object_get_root(), "machine",
2120 OBJECT(current_machine));
2121 object_property_add_child(container_get(OBJECT(current_machine),
2122 "/unattached"),
2123 "sysbus", OBJECT(sysbus_get_default()));
2125 if (machine_class->minimum_page_bits) {
2126 if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
2127 /* This would be a board error: specifying a minimum smaller than
2128 * a target's compile-time fixed setting.
2130 g_assert_not_reached();
2134 cpu_exec_init_all();
2136 if (machine_class->hw_version) {
2137 qemu_set_hw_version(machine_class->hw_version);
2141 * Get the default machine options from the machine if it is not already
2142 * specified either by the configuration file or by the command line.
2144 if (machine_class->default_machine_opts) {
2145 QDict *default_opts =
2146 keyval_parse(machine_class->default_machine_opts, NULL, NULL,
2147 &error_abort);
2148 qemu_apply_legacy_machine_options(default_opts);
2149 object_set_properties_from_keyval(OBJECT(current_machine), default_opts,
2150 false, &error_abort);
2151 qobject_unref(default_opts);
2155 static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
2157 GlobalProperty *g;
2159 g = g_malloc0(sizeof(*g));
2160 g->driver = qemu_opt_get(opts, "driver");
2161 g->property = qemu_opt_get(opts, "property");
2162 g->value = qemu_opt_get(opts, "value");
2163 qdev_prop_register_global(g);
2164 return 0;
2168 * Return whether configuration group @group is stored in QemuOpts, or
2169 * recorded as one or more QDicts by qemu_record_config_group.
2171 static bool is_qemuopts_group(const char *group)
2173 if (g_str_equal(group, "object") ||
2174 g_str_equal(group, "audiodev") ||
2175 g_str_equal(group, "machine") ||
2176 g_str_equal(group, "smp-opts") ||
2177 g_str_equal(group, "boot-opts")) {
2178 return false;
2180 return true;
2183 static void qemu_record_config_group(const char *group, QDict *dict,
2184 bool from_json, Error **errp)
2186 if (g_str_equal(group, "object")) {
2187 Visitor *v = qobject_input_visitor_new_keyval(QOBJECT(dict));
2188 object_option_add_visitor(v);
2189 visit_free(v);
2191 } else if (g_str_equal(group, "audiodev")) {
2192 Audiodev *dev = NULL;
2193 Visitor *v = qobject_input_visitor_new_keyval(QOBJECT(dict));
2194 if (visit_type_Audiodev(v, NULL, &dev, errp)) {
2195 audio_define(dev);
2197 visit_free(v);
2199 } else if (g_str_equal(group, "machine")) {
2201 * Cannot merge string-valued and type-safe dictionaries, so JSON
2202 * is not accepted yet for -M.
2204 assert(!from_json);
2205 keyval_merge(machine_opts_dict, dict, errp);
2206 } else if (g_str_equal(group, "smp-opts")) {
2207 machine_merge_property("smp", dict, &error_fatal);
2208 } else if (g_str_equal(group, "boot-opts")) {
2209 machine_merge_property("boot", dict, &error_fatal);
2210 } else {
2211 abort();
2216 * Parse non-QemuOpts config file groups, pass the rest to
2217 * qemu_config_do_parse.
2219 static void qemu_parse_config_group(const char *group, QDict *qdict,
2220 void *opaque, Error **errp)
2222 QObject *crumpled;
2223 if (is_qemuopts_group(group)) {
2224 qemu_config_do_parse(group, qdict, opaque, errp);
2225 return;
2228 crumpled = qdict_crumple(qdict, errp);
2229 if (!crumpled) {
2230 return;
2232 switch (qobject_type(crumpled)) {
2233 case QTYPE_QDICT:
2234 qemu_record_config_group(group, qobject_to(QDict, crumpled), false, errp);
2235 break;
2236 case QTYPE_QLIST:
2237 error_setg(errp, "Lists cannot be at top level of a configuration section");
2238 break;
2239 default:
2240 g_assert_not_reached();
2242 qobject_unref(crumpled);
2245 static void qemu_read_default_config_file(Error **errp)
2247 ERRP_GUARD();
2248 int ret;
2249 g_autofree char *file = get_relocated_path(CONFIG_QEMU_CONFDIR "/qemu.conf");
2251 ret = qemu_read_config_file(file, qemu_parse_config_group, errp);
2252 if (ret < 0) {
2253 if (ret == -ENOENT) {
2254 error_free(*errp);
2255 *errp = NULL;
2260 static void qemu_set_option(const char *str, Error **errp)
2262 char group[64], id[64], arg[64];
2263 QemuOptsList *list;
2264 QemuOpts *opts;
2265 int rc, offset;
2267 rc = sscanf(str, "%63[^.].%63[^.].%63[^=]%n", group, id, arg, &offset);
2268 if (rc < 3 || str[offset] != '=') {
2269 error_setg(errp, "can't parse: \"%s\"", str);
2270 return;
2273 if (!is_qemuopts_group(group)) {
2274 error_setg(errp, "-set is not supported with %s", group);
2275 } else {
2276 list = qemu_find_opts_err(group, errp);
2277 if (list) {
2278 opts = qemu_opts_find(list, id);
2279 if (!opts) {
2280 error_setg(errp, "there is no %s \"%s\" defined", group, id);
2281 return;
2283 qemu_opt_set(opts, arg, str + offset + 1, errp);
2288 static void user_register_global_props(void)
2290 qemu_opts_foreach(qemu_find_opts("global"),
2291 global_init_func, NULL, NULL);
2294 static int do_configure_icount(void *opaque, QemuOpts *opts, Error **errp)
2296 return !icount_configure(opts, errp);
2299 static int accelerator_set_property(void *opaque,
2300 const char *name, const char *value,
2301 Error **errp)
2303 return object_parse_property_opt(opaque, name, value, "accel", errp);
2306 static int do_configure_accelerator(void *opaque, QemuOpts *opts, Error **errp)
2308 bool *p_init_failed = opaque;
2309 const char *acc = qemu_opt_get(opts, "accel");
2310 AccelClass *ac = accel_find(acc);
2311 AccelState *accel;
2312 int ret;
2313 bool qtest_with_kvm;
2315 if (!acc) {
2316 error_setg(errp, QERR_MISSING_PARAMETER, "accel");
2317 goto bad;
2320 qtest_with_kvm = g_str_equal(acc, "kvm") && qtest_chrdev != NULL;
2322 if (!ac) {
2323 if (!qtest_with_kvm) {
2324 error_report("invalid accelerator %s", acc);
2326 goto bad;
2328 accel = ACCEL(object_new_with_class(OBJECT_CLASS(ac)));
2329 object_apply_compat_props(OBJECT(accel));
2330 qemu_opt_foreach(opts, accelerator_set_property,
2331 accel,
2332 &error_fatal);
2334 ret = accel_init_machine(accel, current_machine);
2335 if (ret < 0) {
2336 if (!qtest_with_kvm || ret != -ENOENT) {
2337 error_report("failed to initialize %s: %s", acc, strerror(-ret));
2339 goto bad;
2342 return 1;
2344 bad:
2345 *p_init_failed = true;
2346 return 0;
2349 static void configure_accelerators(const char *progname)
2351 bool init_failed = false;
2353 qemu_opts_foreach(qemu_find_opts("icount"),
2354 do_configure_icount, NULL, &error_fatal);
2356 if (QTAILQ_EMPTY(&qemu_accel_opts.head)) {
2357 char **accel_list, **tmp;
2359 if (accelerators == NULL) {
2360 /* Select the default accelerator */
2361 bool have_tcg = accel_find("tcg");
2362 bool have_kvm = accel_find("kvm");
2364 if (have_tcg && have_kvm) {
2365 if (g_str_has_suffix(progname, "kvm")) {
2366 /* If the program name ends with "kvm", we prefer KVM */
2367 accelerators = "kvm:tcg";
2368 } else {
2369 accelerators = "tcg:kvm";
2371 } else if (have_kvm) {
2372 accelerators = "kvm";
2373 } else if (have_tcg) {
2374 accelerators = "tcg";
2375 } else {
2376 error_report("No accelerator selected and"
2377 " no default accelerator available");
2378 exit(1);
2381 accel_list = g_strsplit(accelerators, ":", 0);
2383 for (tmp = accel_list; *tmp; tmp++) {
2385 * Filter invalid accelerators here, to prevent obscenities
2386 * such as "-machine accel=tcg,,thread=single".
2388 if (accel_find(*tmp)) {
2389 qemu_opts_parse_noisily(qemu_find_opts("accel"), *tmp, true);
2390 } else {
2391 init_failed = true;
2392 error_report("invalid accelerator %s", *tmp);
2395 g_strfreev(accel_list);
2396 } else {
2397 if (accelerators != NULL) {
2398 error_report("The -accel and \"-machine accel=\" options are incompatible");
2399 exit(1);
2403 if (!qemu_opts_foreach(qemu_find_opts("accel"),
2404 do_configure_accelerator, &init_failed, &error_fatal)) {
2405 if (!init_failed) {
2406 error_report("no accelerator found");
2408 exit(1);
2411 if (init_failed && !qtest_chrdev) {
2412 error_report("falling back to %s", current_accel_name());
2415 if (icount_enabled() && !tcg_enabled()) {
2416 error_report("-icount is not allowed with hardware virtualization");
2417 exit(1);
2421 static void qemu_validate_options(const QDict *machine_opts)
2423 const char *kernel_filename = qdict_get_try_str(machine_opts, "kernel");
2424 const char *initrd_filename = qdict_get_try_str(machine_opts, "initrd");
2425 const char *kernel_cmdline = qdict_get_try_str(machine_opts, "append");
2427 if (kernel_filename == NULL) {
2428 if (kernel_cmdline != NULL) {
2429 error_report("-append only allowed with -kernel option");
2430 exit(1);
2433 if (initrd_filename != NULL) {
2434 error_report("-initrd only allowed with -kernel option");
2435 exit(1);
2439 if (loadvm && incoming) {
2440 error_report("'incoming' and 'loadvm' options are mutually exclusive");
2441 exit(EXIT_FAILURE);
2443 if (loadvm && preconfig_requested) {
2444 error_report("'preconfig' and 'loadvm' options are "
2445 "mutually exclusive");
2446 exit(EXIT_FAILURE);
2448 if (incoming && preconfig_requested && strcmp(incoming, "defer") != 0) {
2449 error_report("'preconfig' supports '-incoming defer' only");
2450 exit(EXIT_FAILURE);
2453 #ifdef CONFIG_CURSES
2454 if (is_daemonized() && dpy.type == DISPLAY_TYPE_CURSES) {
2455 error_report("curses display cannot be used with -daemonize");
2456 exit(1);
2458 #endif
2461 static void qemu_process_sugar_options(void)
2463 if (mem_prealloc) {
2464 QObject *smp = qdict_get(machine_opts_dict, "smp");
2465 if (smp && qobject_type(smp) == QTYPE_QDICT) {
2466 QObject *cpus = qdict_get(qobject_to(QDict, smp), "cpus");
2467 if (cpus && qobject_type(cpus) == QTYPE_QSTRING) {
2468 const char *val = qstring_get_str(qobject_to(QString, cpus));
2469 object_register_sugar_prop("memory-backend", "prealloc-threads",
2470 val, false);
2473 object_register_sugar_prop("memory-backend", "prealloc", "on", false);
2477 /* -action processing */
2480 * Process all the -action parameters parsed from cmdline.
2482 static int process_runstate_actions(void *opaque, QemuOpts *opts, Error **errp)
2484 Error *local_err = NULL;
2485 QDict *qdict = qemu_opts_to_qdict(opts, NULL);
2486 QObject *ret = NULL;
2487 qmp_marshal_set_action(qdict, &ret, &local_err);
2488 qobject_unref(ret);
2489 qobject_unref(qdict);
2490 if (local_err) {
2491 error_propagate(errp, local_err);
2492 return 1;
2494 return 0;
2497 static void qemu_process_early_options(void)
2499 qemu_opts_foreach(qemu_find_opts("name"),
2500 parse_name, NULL, &error_fatal);
2502 object_option_foreach_add(object_create_pre_sandbox);
2504 #ifdef CONFIG_SECCOMP
2505 QemuOptsList *olist = qemu_find_opts_err("sandbox", NULL);
2506 if (olist) {
2507 qemu_opts_foreach(olist, parse_sandbox, NULL, &error_fatal);
2509 #endif
2511 if (qemu_opts_foreach(qemu_find_opts("action"),
2512 process_runstate_actions, NULL, &error_fatal)) {
2513 exit(1);
2516 #ifndef _WIN32
2517 qemu_opts_foreach(qemu_find_opts("add-fd"),
2518 parse_add_fd, NULL, &error_fatal);
2520 qemu_opts_foreach(qemu_find_opts("add-fd"),
2521 cleanup_add_fd, NULL, &error_fatal);
2522 #endif
2524 /* Open the logfile at this point and set the log mask if necessary. */
2526 int mask = 0;
2527 if (log_mask) {
2528 mask = qemu_str_to_log_mask(log_mask);
2529 if (!mask) {
2530 qemu_print_log_usage(stdout);
2531 exit(1);
2534 qemu_set_log_filename_flags(log_file, mask, &error_fatal);
2537 qemu_add_default_firmwarepath();
2540 static void qemu_process_help_options(void)
2543 * Check for -cpu help and -device help before we call select_machine(),
2544 * which will return an error if the architecture has no default machine
2545 * type and the user did not specify one, so that the user doesn't need
2546 * to say '-cpu help -machine something'.
2548 if (cpu_option && is_help_option(cpu_option)) {
2549 list_cpus();
2550 exit(0);
2553 if (qemu_opts_foreach(qemu_find_opts("device"),
2554 device_help_func, NULL, NULL)) {
2555 exit(0);
2558 /* -L help lists the data directories and exits. */
2559 if (list_data_dirs) {
2560 qemu_list_data_dirs();
2561 exit(0);
2565 static void qemu_maybe_daemonize(const char *pid_file)
2567 Error *err = NULL;
2569 os_daemonize();
2570 rcu_disable_atfork();
2572 if (pid_file) {
2573 char *pid_file_realpath = NULL;
2575 if (!qemu_write_pidfile(pid_file, &err)) {
2576 error_reportf_err(err, "cannot create PID file: ");
2577 exit(1);
2580 pid_file_realpath = g_malloc0(PATH_MAX);
2581 if (!realpath(pid_file, pid_file_realpath)) {
2582 if (errno != ENOENT) {
2583 warn_report("not removing PID file on exit: cannot resolve PID "
2584 "file path: %s: %s", pid_file, strerror(errno));
2586 return;
2589 qemu_unlink_pidfile_notifier = (struct UnlinkPidfileNotifier) {
2590 .notifier = {
2591 .notify = qemu_unlink_pidfile,
2593 .pid_file_realpath = pid_file_realpath,
2595 qemu_add_exit_notifier(&qemu_unlink_pidfile_notifier.notifier);
2599 static void qemu_init_displays(void)
2601 DisplayState *ds;
2603 /* init local displays */
2604 ds = init_displaystate();
2605 qemu_display_init(ds, &dpy);
2607 /* must be after terminal init, SDL library changes signal handlers */
2608 os_setup_signal_handling();
2610 /* init remote displays */
2611 #ifdef CONFIG_VNC
2612 qemu_opts_foreach(qemu_find_opts("vnc"),
2613 vnc_init_func, NULL, &error_fatal);
2614 #endif
2616 if (using_spice) {
2617 qemu_spice.display_init();
2621 static void qemu_init_board(void)
2623 /* process plugin before CPUs are created, but once -smp has been parsed */
2624 qemu_plugin_load_list(&plugin_list, &error_fatal);
2626 /* From here on we enter MACHINE_PHASE_INITIALIZED. */
2627 machine_run_board_init(current_machine, mem_path, &error_fatal);
2629 drive_check_orphaned();
2631 realtime_init();
2634 static void qemu_create_cli_devices(void)
2636 DeviceOption *opt;
2638 soundhw_init();
2640 qemu_opts_foreach(qemu_find_opts("fw_cfg"),
2641 parse_fw_cfg, fw_cfg_find(), &error_fatal);
2643 /* init USB devices */
2644 if (machine_usb(current_machine)) {
2645 if (foreach_device_config(DEV_USB, usb_parse) < 0)
2646 exit(1);
2649 /* init generic devices */
2650 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
2651 qemu_opts_foreach(qemu_find_opts("device"),
2652 device_init_func, NULL, &error_fatal);
2653 QTAILQ_FOREACH(opt, &device_opts, next) {
2654 DeviceState *dev;
2655 loc_push_restore(&opt->loc);
2657 * TODO Eventually we should call qmp_device_add() here to make sure it
2658 * behaves the same, but QMP still has to accept incorrectly typed
2659 * options until libvirt is fixed and we want to be strict on the CLI
2660 * from the start, so call qdev_device_add_from_qdict() directly for
2661 * now.
2663 dev = qdev_device_add_from_qdict(opt->opts, true, &error_fatal);
2664 object_unref(OBJECT(dev));
2665 loc_pop(&opt->loc);
2667 rom_reset_order_override();
2670 static bool qemu_machine_creation_done(Error **errp)
2672 MachineState *machine = MACHINE(qdev_get_machine());
2674 /* Did we create any drives that we failed to create a device for? */
2675 drive_check_orphaned();
2677 /* Don't warn about the default network setup that you get if
2678 * no command line -net or -netdev options are specified. There
2679 * are two cases that we would otherwise complain about:
2680 * (1) board doesn't support a NIC but the implicit "-net nic"
2681 * requested one
2682 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
2683 * sets up a nic that isn't connected to anything.
2685 if (!default_net && (!qtest_enabled() || has_defaults)) {
2686 net_check_clients();
2689 qdev_prop_check_globals();
2691 qdev_machine_creation_done();
2693 if (machine->cgs && !machine->cgs->ready) {
2694 error_setg(errp, "accelerator does not support confidential guest %s",
2695 object_get_typename(OBJECT(machine->cgs)));
2696 exit(1);
2699 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
2700 error_setg(errp, "could not start gdbserver");
2701 return false;
2703 if (!vga_interface_created && !default_vga &&
2704 vga_interface_type != VGA_NONE) {
2705 warn_report("A -vga option was passed but this machine "
2706 "type does not use that option; "
2707 "No VGA device has been created");
2709 return true;
2712 void qmp_x_exit_preconfig(Error **errp)
2714 if (phase_check(PHASE_MACHINE_INITIALIZED)) {
2715 error_setg(errp, "The command is permitted only before machine initialization");
2716 return;
2719 qemu_init_board();
2720 qemu_create_cli_devices();
2721 if (!qemu_machine_creation_done(errp)) {
2722 return;
2725 if (loadvm) {
2726 RunState state = autostart ? RUN_STATE_RUNNING : runstate_get();
2727 load_snapshot(loadvm, NULL, false, NULL, &error_fatal);
2728 load_snapshot_resume(state);
2730 if (replay_mode != REPLAY_MODE_NONE) {
2731 replay_vmstate_init();
2734 if (incoming) {
2735 Error *local_err = NULL;
2736 if (strcmp(incoming, "defer") != 0) {
2737 qmp_migrate_incoming(incoming, false, NULL, true, true,
2738 &local_err);
2739 if (local_err) {
2740 error_reportf_err(local_err, "-incoming %s: ", incoming);
2741 exit(1);
2744 } else if (autostart) {
2745 qmp_cont(NULL);
2749 void qemu_init(int argc, char **argv)
2751 QemuOpts *opts;
2752 QemuOpts *icount_opts = NULL, *accel_opts = NULL;
2753 QemuOptsList *olist;
2754 int optind;
2755 const char *optarg;
2756 MachineClass *machine_class;
2757 bool userconfig = true;
2758 FILE *vmstate_dump_file = NULL;
2760 qemu_add_opts(&qemu_drive_opts);
2761 qemu_add_drive_opts(&qemu_legacy_drive_opts);
2762 qemu_add_drive_opts(&qemu_common_drive_opts);
2763 qemu_add_drive_opts(&qemu_drive_opts);
2764 qemu_add_drive_opts(&bdrv_runtime_opts);
2765 qemu_add_opts(&qemu_chardev_opts);
2766 qemu_add_opts(&qemu_device_opts);
2767 qemu_add_opts(&qemu_netdev_opts);
2768 qemu_add_opts(&qemu_nic_opts);
2769 qemu_add_opts(&qemu_net_opts);
2770 qemu_add_opts(&qemu_rtc_opts);
2771 qemu_add_opts(&qemu_global_opts);
2772 qemu_add_opts(&qemu_mon_opts);
2773 qemu_add_opts(&qemu_trace_opts);
2774 qemu_plugin_add_opts();
2775 qemu_add_opts(&qemu_option_rom_opts);
2776 qemu_add_opts(&qemu_accel_opts);
2777 qemu_add_opts(&qemu_mem_opts);
2778 qemu_add_opts(&qemu_smp_opts);
2779 qemu_add_opts(&qemu_boot_opts);
2780 qemu_add_opts(&qemu_add_fd_opts);
2781 qemu_add_opts(&qemu_object_opts);
2782 qemu_add_opts(&qemu_tpmdev_opts);
2783 qemu_add_opts(&qemu_overcommit_opts);
2784 qemu_add_opts(&qemu_msg_opts);
2785 qemu_add_opts(&qemu_name_opts);
2786 qemu_add_opts(&qemu_numa_opts);
2787 qemu_add_opts(&qemu_icount_opts);
2788 qemu_add_opts(&qemu_semihosting_config_opts);
2789 qemu_add_opts(&qemu_fw_cfg_opts);
2790 qemu_add_opts(&qemu_action_opts);
2791 qemu_add_run_with_opts();
2792 module_call_init(MODULE_INIT_OPTS);
2794 error_init(argv[0]);
2795 qemu_init_exec_dir(argv[0]);
2797 os_setup_limits();
2799 qemu_init_arch_modules();
2801 qemu_init_subsystems();
2803 /* first pass of option parsing */
2804 optind = 1;
2805 while (optind < argc) {
2806 if (argv[optind][0] != '-') {
2807 /* disk image */
2808 optind++;
2809 } else {
2810 const QEMUOption *popt;
2812 popt = lookup_opt(argc, argv, &optarg, &optind);
2813 switch (popt->index) {
2814 case QEMU_OPTION_nouserconfig:
2815 userconfig = false;
2816 break;
2821 machine_opts_dict = qdict_new();
2822 if (userconfig) {
2823 qemu_read_default_config_file(&error_fatal);
2826 /* second pass of option parsing */
2827 optind = 1;
2828 for(;;) {
2829 if (optind >= argc)
2830 break;
2831 if (argv[optind][0] != '-') {
2832 loc_set_cmdline(argv, optind, 1);
2833 drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2834 } else {
2835 const QEMUOption *popt;
2837 popt = lookup_opt(argc, argv, &optarg, &optind);
2838 if (!(popt->arch_mask & arch_type)) {
2839 error_report("Option not supported for this target");
2840 exit(1);
2842 switch(popt->index) {
2843 case QEMU_OPTION_cpu:
2844 /* hw initialization will check this */
2845 cpu_option = optarg;
2846 break;
2847 case QEMU_OPTION_hda:
2848 case QEMU_OPTION_hdb:
2849 case QEMU_OPTION_hdc:
2850 case QEMU_OPTION_hdd:
2851 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2852 HD_OPTS);
2853 break;
2854 case QEMU_OPTION_blockdev:
2856 Visitor *v;
2857 BlockdevOptionsQueueEntry *bdo;
2859 v = qobject_input_visitor_new_str(optarg, "driver",
2860 &error_fatal);
2862 bdo = g_new(BlockdevOptionsQueueEntry, 1);
2863 visit_type_BlockdevOptions(v, NULL, &bdo->bdo,
2864 &error_fatal);
2865 visit_free(v);
2866 loc_save(&bdo->loc);
2867 QSIMPLEQ_INSERT_TAIL(&bdo_queue, bdo, entry);
2868 break;
2870 case QEMU_OPTION_drive:
2871 opts = qemu_opts_parse_noisily(qemu_find_opts("drive"),
2872 optarg, false);
2873 if (opts == NULL) {
2874 exit(1);
2876 break;
2877 case QEMU_OPTION_set:
2878 qemu_set_option(optarg, &error_fatal);
2879 break;
2880 case QEMU_OPTION_global:
2881 if (qemu_global_option(optarg) != 0)
2882 exit(1);
2883 break;
2884 case QEMU_OPTION_mtdblock:
2885 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2886 break;
2887 case QEMU_OPTION_sd:
2888 drive_add(IF_SD, -1, optarg, SD_OPTS);
2889 break;
2890 case QEMU_OPTION_pflash:
2891 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2892 break;
2893 case QEMU_OPTION_snapshot:
2894 snapshot = 1;
2895 replay_add_blocker("-snapshot");
2896 break;
2897 case QEMU_OPTION_numa:
2898 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
2899 optarg, true);
2900 if (!opts) {
2901 exit(1);
2903 break;
2904 case QEMU_OPTION_display:
2905 parse_display(optarg);
2906 break;
2907 case QEMU_OPTION_nographic:
2908 qdict_put_str(machine_opts_dict, "graphics", "off");
2909 nographic = true;
2910 dpy.type = DISPLAY_TYPE_NONE;
2911 break;
2912 case QEMU_OPTION_portrait:
2913 graphic_rotate = 90;
2914 break;
2915 case QEMU_OPTION_rotate:
2916 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2917 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2918 graphic_rotate != 180 && graphic_rotate != 270) {
2919 error_report("only 90, 180, 270 deg rotation is available");
2920 exit(1);
2922 break;
2923 case QEMU_OPTION_kernel:
2924 qdict_put_str(machine_opts_dict, "kernel", optarg);
2925 break;
2926 case QEMU_OPTION_initrd:
2927 qdict_put_str(machine_opts_dict, "initrd", optarg);
2928 break;
2929 case QEMU_OPTION_append:
2930 qdict_put_str(machine_opts_dict, "append", optarg);
2931 break;
2932 case QEMU_OPTION_dtb:
2933 qdict_put_str(machine_opts_dict, "dtb", optarg);
2934 break;
2935 case QEMU_OPTION_cdrom:
2936 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
2937 break;
2938 case QEMU_OPTION_boot:
2939 machine_parse_property_opt(qemu_find_opts("boot-opts"), "boot", optarg);
2940 break;
2941 case QEMU_OPTION_fda:
2942 case QEMU_OPTION_fdb:
2943 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2944 optarg, FD_OPTS);
2945 break;
2946 case QEMU_OPTION_no_fd_bootchk:
2947 qdict_put_str(machine_opts_dict, "fd-bootchk", "off");
2948 break;
2949 case QEMU_OPTION_netdev:
2950 default_net = 0;
2951 if (netdev_is_modern(optarg)) {
2952 netdev_parse_modern(optarg);
2953 } else {
2954 net_client_parse(qemu_find_opts("netdev"), optarg);
2956 break;
2957 case QEMU_OPTION_nic:
2958 default_net = 0;
2959 net_client_parse(qemu_find_opts("nic"), optarg);
2960 break;
2961 case QEMU_OPTION_net:
2962 default_net = 0;
2963 net_client_parse(qemu_find_opts("net"), optarg);
2964 break;
2965 #ifdef CONFIG_LIBISCSI
2966 case QEMU_OPTION_iscsi:
2967 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
2968 optarg, false);
2969 if (!opts) {
2970 exit(1);
2972 break;
2973 #endif
2974 case QEMU_OPTION_audiodev:
2975 default_audio = 0;
2976 audio_parse_option(optarg);
2977 break;
2978 case QEMU_OPTION_audio: {
2979 bool help;
2980 char *model = NULL;
2981 Audiodev *dev = NULL;
2982 Visitor *v;
2983 QDict *dict = keyval_parse(optarg, "driver", &help, &error_fatal);
2984 default_audio = 0;
2985 if (help || (qdict_haskey(dict, "driver") &&
2986 is_help_option(qdict_get_str(dict, "driver")))) {
2987 audio_help();
2988 exit(EXIT_SUCCESS);
2990 if (!qdict_haskey(dict, "id")) {
2991 qdict_put_str(dict, "id", "audiodev0");
2993 if (qdict_haskey(dict, "model")) {
2994 model = g_strdup(qdict_get_str(dict, "model"));
2995 qdict_del(dict, "model");
2996 if (is_help_option(model)) {
2997 show_valid_soundhw();
2998 exit(0);
3001 v = qobject_input_visitor_new_keyval(QOBJECT(dict));
3002 qobject_unref(dict);
3003 visit_type_Audiodev(v, NULL, &dev, &error_fatal);
3004 visit_free(v);
3005 if (model) {
3006 audio_define(dev);
3007 select_soundhw(model, dev->id);
3008 g_free(model);
3009 } else {
3010 audio_define_default(dev, &error_fatal);
3012 break;
3014 case QEMU_OPTION_h:
3015 help(0);
3016 break;
3017 case QEMU_OPTION_version:
3018 version();
3019 exit(0);
3020 break;
3021 case QEMU_OPTION_m:
3022 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"), optarg, true);
3023 if (opts == NULL) {
3024 exit(1);
3026 break;
3027 #ifdef CONFIG_TPM
3028 case QEMU_OPTION_tpmdev:
3029 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3030 exit(1);
3032 break;
3033 #endif
3034 case QEMU_OPTION_mempath:
3035 mem_path = optarg;
3036 break;
3037 case QEMU_OPTION_mem_prealloc:
3038 mem_prealloc = 1;
3039 break;
3040 case QEMU_OPTION_d:
3041 log_mask = optarg;
3042 break;
3043 case QEMU_OPTION_D:
3044 log_file = optarg;
3045 break;
3046 case QEMU_OPTION_DFILTER:
3047 qemu_set_dfilter_ranges(optarg, &error_fatal);
3048 break;
3049 #if defined(CONFIG_TCG) && defined(CONFIG_LINUX)
3050 case QEMU_OPTION_perfmap:
3051 perf_enable_perfmap();
3052 break;
3053 case QEMU_OPTION_jitdump:
3054 perf_enable_jitdump();
3055 break;
3056 #endif
3057 case QEMU_OPTION_seed:
3058 qemu_guest_random_seed_main(optarg, &error_fatal);
3059 break;
3060 case QEMU_OPTION_s:
3061 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3062 break;
3063 case QEMU_OPTION_gdb:
3064 add_device_config(DEV_GDB, optarg);
3065 break;
3066 case QEMU_OPTION_L:
3067 if (is_help_option(optarg)) {
3068 list_data_dirs = true;
3069 } else {
3070 qemu_add_data_dir(g_strdup(optarg));
3072 break;
3073 case QEMU_OPTION_bios:
3074 qdict_put_str(machine_opts_dict, "firmware", optarg);
3075 break;
3076 case QEMU_OPTION_S:
3077 autostart = 0;
3078 break;
3079 case QEMU_OPTION_k:
3080 keyboard_layout = optarg;
3081 break;
3082 case QEMU_OPTION_vga:
3083 vga_model = optarg;
3084 default_vga = 0;
3085 break;
3086 case QEMU_OPTION_g:
3088 const char *p;
3089 int w, h, depth;
3090 p = optarg;
3091 w = strtol(p, (char **)&p, 10);
3092 if (w <= 0) {
3093 graphic_error:
3094 error_report("invalid resolution or depth");
3095 exit(1);
3097 if (*p != 'x')
3098 goto graphic_error;
3099 p++;
3100 h = strtol(p, (char **)&p, 10);
3101 if (h <= 0)
3102 goto graphic_error;
3103 if (*p == 'x') {
3104 p++;
3105 depth = strtol(p, (char **)&p, 10);
3106 if (depth != 1 && depth != 2 && depth != 4 &&
3107 depth != 8 && depth != 15 && depth != 16 &&
3108 depth != 24 && depth != 32)
3109 goto graphic_error;
3110 } else if (*p == '\0') {
3111 depth = graphic_depth;
3112 } else {
3113 goto graphic_error;
3116 graphic_width = w;
3117 graphic_height = h;
3118 graphic_depth = depth;
3120 break;
3121 case QEMU_OPTION_echr:
3123 char *r;
3124 term_escape_char = strtol(optarg, &r, 0);
3125 if (r == optarg)
3126 printf("Bad argument to echr\n");
3127 break;
3129 case QEMU_OPTION_monitor:
3130 default_monitor = 0;
3131 if (strncmp(optarg, "none", 4)) {
3132 monitor_parse(optarg, "readline", false);
3134 break;
3135 case QEMU_OPTION_qmp:
3136 monitor_parse(optarg, "control", false);
3137 default_monitor = 0;
3138 break;
3139 case QEMU_OPTION_qmp_pretty:
3140 monitor_parse(optarg, "control", true);
3141 default_monitor = 0;
3142 break;
3143 case QEMU_OPTION_mon:
3144 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3145 true);
3146 if (!opts) {
3147 exit(1);
3149 default_monitor = 0;
3150 break;
3151 case QEMU_OPTION_chardev:
3152 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3153 optarg, true);
3154 if (!opts) {
3155 exit(1);
3157 break;
3158 case QEMU_OPTION_fsdev:
3159 olist = qemu_find_opts("fsdev");
3160 if (!olist) {
3161 error_report("fsdev support is disabled");
3162 exit(1);
3164 opts = qemu_opts_parse_noisily(olist, optarg, true);
3165 if (!opts) {
3166 exit(1);
3168 break;
3169 case QEMU_OPTION_virtfs: {
3170 QemuOpts *fsdev;
3171 QemuOpts *device;
3172 const char *writeout, *sock_fd, *socket, *path, *security_model,
3173 *multidevs;
3175 olist = qemu_find_opts("virtfs");
3176 if (!olist) {
3177 error_report("virtfs support is disabled");
3178 exit(1);
3180 opts = qemu_opts_parse_noisily(olist, optarg, true);
3181 if (!opts) {
3182 exit(1);
3185 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3186 qemu_opt_get(opts, "mount_tag") == NULL) {
3187 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3188 exit(1);
3190 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3191 qemu_opts_id(opts) ?:
3192 qemu_opt_get(opts, "mount_tag"),
3193 1, NULL);
3194 if (!fsdev) {
3195 error_report("duplicate or invalid fsdev id: %s",
3196 qemu_opt_get(opts, "mount_tag"));
3197 exit(1);
3200 writeout = qemu_opt_get(opts, "writeout");
3201 if (writeout) {
3202 #ifdef CONFIG_SYNC_FILE_RANGE
3203 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3204 #else
3205 error_report("writeout=immediate not supported "
3206 "on this platform");
3207 exit(1);
3208 #endif
3210 qemu_opt_set(fsdev, "fsdriver",
3211 qemu_opt_get(opts, "fsdriver"), &error_abort);
3212 path = qemu_opt_get(opts, "path");
3213 if (path) {
3214 qemu_opt_set(fsdev, "path", path, &error_abort);
3216 security_model = qemu_opt_get(opts, "security_model");
3217 if (security_model) {
3218 qemu_opt_set(fsdev, "security_model", security_model,
3219 &error_abort);
3221 socket = qemu_opt_get(opts, "socket");
3222 if (socket) {
3223 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3225 sock_fd = qemu_opt_get(opts, "sock_fd");
3226 if (sock_fd) {
3227 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3230 qemu_opt_set_bool(fsdev, "readonly",
3231 qemu_opt_get_bool(opts, "readonly", 0),
3232 &error_abort);
3233 multidevs = qemu_opt_get(opts, "multidevs");
3234 if (multidevs) {
3235 qemu_opt_set(fsdev, "multidevs", multidevs, &error_abort);
3237 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3238 &error_abort);
3239 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3240 qemu_opt_set(device, "fsdev",
3241 qemu_opts_id(fsdev), &error_abort);
3242 qemu_opt_set(device, "mount_tag",
3243 qemu_opt_get(opts, "mount_tag"), &error_abort);
3244 break;
3246 case QEMU_OPTION_serial:
3247 add_device_config(DEV_SERIAL, optarg);
3248 default_serial = 0;
3249 if (strncmp(optarg, "mon:", 4) == 0) {
3250 default_monitor = 0;
3252 break;
3253 case QEMU_OPTION_action:
3254 olist = qemu_find_opts("action");
3255 if (!qemu_opts_parse_noisily(olist, optarg, false)) {
3256 exit(1);
3258 break;
3259 case QEMU_OPTION_watchdog_action: {
3260 opts = qemu_opts_create(qemu_find_opts("action"), NULL, 0, &error_abort);
3261 qemu_opt_set(opts, "watchdog", optarg, &error_abort);
3262 break;
3264 case QEMU_OPTION_parallel:
3265 add_device_config(DEV_PARALLEL, optarg);
3266 default_parallel = 0;
3267 if (strncmp(optarg, "mon:", 4) == 0) {
3268 default_monitor = 0;
3270 break;
3271 case QEMU_OPTION_debugcon:
3272 add_device_config(DEV_DEBUGCON, optarg);
3273 break;
3274 case QEMU_OPTION_loadvm:
3275 loadvm = optarg;
3276 break;
3277 case QEMU_OPTION_full_screen:
3278 dpy.has_full_screen = true;
3279 dpy.full_screen = true;
3280 break;
3281 case QEMU_OPTION_pidfile:
3282 pid_file = optarg;
3283 break;
3284 case QEMU_OPTION_win2k_hack:
3285 object_register_sugar_prop("ide-device", "win2k-install-hack", "true", true);
3286 break;
3287 case QEMU_OPTION_acpitable:
3288 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3289 optarg, true);
3290 if (!opts) {
3291 exit(1);
3293 acpi_table_add(opts, &error_fatal);
3294 break;
3295 case QEMU_OPTION_smbios:
3296 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3297 optarg, false);
3298 if (!opts) {
3299 exit(1);
3301 smbios_entry_add(opts, &error_fatal);
3302 break;
3303 case QEMU_OPTION_fwcfg:
3304 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3305 optarg, true);
3306 if (opts == NULL) {
3307 exit(1);
3309 break;
3310 case QEMU_OPTION_preconfig:
3311 preconfig_requested = true;
3312 break;
3313 case QEMU_OPTION_enable_kvm:
3314 qdict_put_str(machine_opts_dict, "accel", "kvm");
3315 break;
3316 case QEMU_OPTION_M:
3317 case QEMU_OPTION_machine:
3319 bool help;
3321 keyval_parse_into(machine_opts_dict, optarg, "type", &help, &error_fatal);
3322 if (help) {
3323 machine_help_func(machine_opts_dict);
3324 exit(EXIT_SUCCESS);
3326 break;
3328 case QEMU_OPTION_accel:
3329 accel_opts = qemu_opts_parse_noisily(qemu_find_opts("accel"),
3330 optarg, true);
3331 optarg = qemu_opt_get(accel_opts, "accel");
3332 if (!optarg || is_help_option(optarg)) {
3333 printf("Accelerators supported in QEMU binary:\n");
3334 GSList *el, *accel_list = object_class_get_list(TYPE_ACCEL,
3335 false);
3336 for (el = accel_list; el; el = el->next) {
3337 gchar *typename = g_strdup(object_class_get_name(
3338 OBJECT_CLASS(el->data)));
3339 /* omit qtest which is used for tests only */
3340 if (g_strcmp0(typename, ACCEL_CLASS_NAME("qtest")) &&
3341 g_str_has_suffix(typename, ACCEL_CLASS_SUFFIX)) {
3342 gchar **optname = g_strsplit(typename,
3343 ACCEL_CLASS_SUFFIX, 0);
3344 printf("%s\n", optname[0]);
3345 g_strfreev(optname);
3347 g_free(typename);
3349 g_slist_free(accel_list);
3350 exit(0);
3352 break;
3353 case QEMU_OPTION_usb:
3354 qdict_put_str(machine_opts_dict, "usb", "on");
3355 break;
3356 case QEMU_OPTION_usbdevice:
3357 qdict_put_str(machine_opts_dict, "usb", "on");
3358 add_device_config(DEV_USB, optarg);
3359 break;
3360 case QEMU_OPTION_device:
3361 if (optarg[0] == '{') {
3362 QObject *obj = qobject_from_json(optarg, &error_fatal);
3363 DeviceOption *opt = g_new0(DeviceOption, 1);
3364 opt->opts = qobject_to(QDict, obj);
3365 loc_save(&opt->loc);
3366 assert(opt->opts != NULL);
3367 QTAILQ_INSERT_TAIL(&device_opts, opt, next);
3368 } else {
3369 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3370 optarg, true)) {
3371 exit(1);
3374 break;
3375 case QEMU_OPTION_smp:
3376 machine_parse_property_opt(qemu_find_opts("smp-opts"),
3377 "smp", optarg);
3378 break;
3379 #ifdef CONFIG_VNC
3380 case QEMU_OPTION_vnc:
3381 vnc_parse(optarg);
3382 display_remote++;
3383 break;
3384 #endif
3385 case QEMU_OPTION_no_reboot:
3386 olist = qemu_find_opts("action");
3387 qemu_opts_parse_noisily(olist, "reboot=shutdown", false);
3388 break;
3389 case QEMU_OPTION_no_shutdown:
3390 olist = qemu_find_opts("action");
3391 qemu_opts_parse_noisily(olist, "shutdown=pause", false);
3392 break;
3393 case QEMU_OPTION_uuid:
3394 if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
3395 error_report("failed to parse UUID string: wrong format");
3396 exit(1);
3398 qemu_uuid_set = true;
3399 break;
3400 case QEMU_OPTION_option_rom:
3401 if (nb_option_roms >= MAX_OPTION_ROMS) {
3402 error_report("too many option ROMs");
3403 exit(1);
3405 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3406 optarg, true);
3407 if (!opts) {
3408 exit(1);
3410 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3411 option_rom[nb_option_roms].bootindex =
3412 qemu_opt_get_number(opts, "bootindex", -1);
3413 if (!option_rom[nb_option_roms].name) {
3414 error_report("Option ROM file is not specified");
3415 exit(1);
3417 nb_option_roms++;
3418 break;
3419 case QEMU_OPTION_semihosting:
3420 qemu_semihosting_enable();
3421 break;
3422 case QEMU_OPTION_semihosting_config:
3423 if (qemu_semihosting_config_options(optarg) != 0) {
3424 exit(1);
3426 break;
3427 case QEMU_OPTION_name:
3428 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3429 optarg, true);
3430 if (!opts) {
3431 exit(1);
3433 /* Capture guest name if -msg guest-name is used later */
3434 error_guest_name = qemu_opt_get(opts, "guest");
3435 break;
3436 case QEMU_OPTION_prom_env:
3437 if (nb_prom_envs >= MAX_PROM_ENVS) {
3438 error_report("too many prom variables");
3439 exit(1);
3441 prom_envs[nb_prom_envs] = optarg;
3442 nb_prom_envs++;
3443 break;
3444 case QEMU_OPTION_old_param:
3445 old_param = 1;
3446 break;
3447 case QEMU_OPTION_rtc:
3448 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3449 false);
3450 if (!opts) {
3451 exit(1);
3453 break;
3454 case QEMU_OPTION_icount:
3455 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3456 optarg, true);
3457 if (!icount_opts) {
3458 exit(1);
3460 break;
3461 case QEMU_OPTION_incoming:
3462 if (!incoming) {
3463 runstate_set(RUN_STATE_INMIGRATE);
3465 incoming = optarg;
3466 break;
3467 case QEMU_OPTION_only_migratable:
3468 only_migratable = 1;
3469 break;
3470 case QEMU_OPTION_nodefaults:
3471 has_defaults = 0;
3472 break;
3473 case QEMU_OPTION_xen_domid:
3474 if (!(accel_find("xen")) && !(accel_find("kvm"))) {
3475 error_report("Option not supported for this target");
3476 exit(1);
3478 xen_domid = atoi(optarg);
3479 break;
3480 case QEMU_OPTION_xen_attach:
3481 if (!(accel_find("xen"))) {
3482 error_report("Option not supported for this target");
3483 exit(1);
3485 xen_mode = XEN_ATTACH;
3486 break;
3487 case QEMU_OPTION_xen_domid_restrict:
3488 if (!(accel_find("xen"))) {
3489 error_report("Option not supported for this target");
3490 exit(1);
3492 xen_domid_restrict = true;
3493 break;
3494 case QEMU_OPTION_trace:
3495 trace_opt_parse(optarg);
3496 break;
3497 case QEMU_OPTION_plugin:
3498 qemu_plugin_opt_parse(optarg, &plugin_list);
3499 break;
3500 case QEMU_OPTION_readconfig:
3501 qemu_read_config_file(optarg, qemu_parse_config_group, &error_fatal);
3502 break;
3503 #ifdef CONFIG_SPICE
3504 case QEMU_OPTION_spice:
3505 opts = qemu_opts_parse_noisily(qemu_find_opts("spice"), optarg, false);
3506 if (!opts) {
3507 exit(1);
3509 display_remote++;
3510 break;
3511 #endif
3512 case QEMU_OPTION_qtest:
3513 qtest_chrdev = optarg;
3514 break;
3515 case QEMU_OPTION_qtest_log:
3516 qtest_log = optarg;
3517 break;
3518 case QEMU_OPTION_sandbox:
3519 olist = qemu_find_opts("sandbox");
3520 if (!olist) {
3521 #ifndef CONFIG_SECCOMP
3522 error_report("-sandbox support is not enabled "
3523 "in this QEMU binary");
3524 #endif
3525 exit(1);
3528 opts = qemu_opts_parse_noisily(olist, optarg, true);
3529 if (!opts) {
3530 exit(1);
3532 break;
3533 case QEMU_OPTION_add_fd:
3534 #ifndef _WIN32
3535 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
3536 optarg, false);
3537 if (!opts) {
3538 exit(1);
3540 #else
3541 error_report("File descriptor passing is disabled on this "
3542 "platform");
3543 exit(1);
3544 #endif
3545 break;
3546 case QEMU_OPTION_object:
3547 object_option_parse(optarg);
3548 break;
3549 case QEMU_OPTION_overcommit:
3550 opts = qemu_opts_parse_noisily(qemu_find_opts("overcommit"),
3551 optarg, false);
3552 if (!opts) {
3553 exit(1);
3555 enable_mlock = qemu_opt_get_bool(opts, "mem-lock", enable_mlock);
3556 enable_cpu_pm = qemu_opt_get_bool(opts, "cpu-pm", enable_cpu_pm);
3557 break;
3558 case QEMU_OPTION_compat:
3560 CompatPolicy *opts_policy;
3561 Visitor *v;
3563 v = qobject_input_visitor_new_str(optarg, NULL,
3564 &error_fatal);
3566 visit_type_CompatPolicy(v, NULL, &opts_policy, &error_fatal);
3567 QAPI_CLONE_MEMBERS(CompatPolicy, &compat_policy, opts_policy);
3569 qapi_free_CompatPolicy(opts_policy);
3570 visit_free(v);
3571 break;
3573 case QEMU_OPTION_msg:
3574 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
3575 false);
3576 if (!opts) {
3577 exit(1);
3579 configure_msg(opts);
3580 break;
3581 case QEMU_OPTION_dump_vmstate:
3582 if (vmstate_dump_file) {
3583 error_report("only one '-dump-vmstate' "
3584 "option may be given");
3585 exit(1);
3587 vmstate_dump_file = fopen(optarg, "w");
3588 if (vmstate_dump_file == NULL) {
3589 error_report("open %s: %s", optarg, strerror(errno));
3590 exit(1);
3592 break;
3593 case QEMU_OPTION_enable_sync_profile:
3594 qsp_enable();
3595 break;
3596 case QEMU_OPTION_nouserconfig:
3597 /* Nothing to be parsed here. Especially, do not error out below. */
3598 break;
3599 #if defined(CONFIG_POSIX)
3600 case QEMU_OPTION_runas:
3601 warn_report("-runas is deprecated, use '-run-with user=...' instead");
3602 if (!os_set_runas(optarg)) {
3603 error_report("User \"%s\" doesn't exist"
3604 " (and is not <uid>:<gid>)",
3605 optarg);
3606 exit(1);
3608 break;
3609 case QEMU_OPTION_daemonize:
3610 os_set_daemonize(true);
3611 break;
3612 case QEMU_OPTION_run_with: {
3613 const char *str;
3614 opts = qemu_opts_parse_noisily(qemu_find_opts("run-with"),
3615 optarg, false);
3616 if (!opts) {
3617 exit(1);
3619 #if defined(CONFIG_LINUX)
3620 if (qemu_opt_get_bool(opts, "async-teardown", false)) {
3621 init_async_teardown();
3623 #endif
3624 str = qemu_opt_get(opts, "chroot");
3625 if (str) {
3626 os_set_chroot(str);
3628 str = qemu_opt_get(opts, "user");
3629 if (str) {
3630 if (!os_set_runas(str)) {
3631 error_report("User \"%s\" doesn't exist"
3632 " (and is not <uid>:<gid>)",
3633 optarg);
3634 exit(1);
3638 break;
3640 #endif /* CONFIG_POSIX */
3642 default:
3643 error_report("Option not supported in this build");
3644 exit(1);
3649 * Clear error location left behind by the loop.
3650 * Best done right after the loop. Do not insert code here!
3652 loc_set_none();
3654 qemu_validate_options(machine_opts_dict);
3655 qemu_process_sugar_options();
3658 * These options affect everything else and should be processed
3659 * before daemonizing.
3661 qemu_process_early_options();
3663 qemu_process_help_options();
3664 qemu_maybe_daemonize(pid_file);
3667 * The trace backend must be initialized after daemonizing.
3668 * trace_init_backends() will call st_init(), which will create the
3669 * trace thread in the parent, and also register st_flush_trace_buffer()
3670 * in atexit(). This function will force the parent to wait for the
3671 * writeout thread to finish, which will not occur, and the parent
3672 * process will be left in the host.
3674 if (!trace_init_backends()) {
3675 exit(1);
3677 trace_init_file();
3679 qemu_init_main_loop(&error_fatal);
3680 cpu_timers_init();
3682 user_register_global_props();
3683 replay_configure(icount_opts);
3685 configure_rtc(qemu_find_opts_singleton("rtc"));
3687 /* Transfer QemuOpts options into machine options */
3688 parse_memory_options();
3690 qemu_create_machine(machine_opts_dict);
3692 suspend_mux_open();
3694 qemu_disable_default_devices();
3695 qemu_setup_display();
3696 qemu_create_default_devices();
3697 qemu_create_early_backends();
3699 qemu_apply_legacy_machine_options(machine_opts_dict);
3700 qemu_apply_machine_options(machine_opts_dict);
3701 qobject_unref(machine_opts_dict);
3702 phase_advance(PHASE_MACHINE_CREATED);
3705 * Note: uses machine properties such as kernel-irqchip, must run
3706 * after qemu_apply_machine_options.
3708 configure_accelerators(argv[0]);
3709 phase_advance(PHASE_ACCEL_CREATED);
3712 * Beware, QOM objects created before this point miss global and
3713 * compat properties.
3715 * Global properties get set up by qdev_prop_register_global(),
3716 * called from user_register_global_props(), and certain option
3717 * desugaring. Also in CPU feature desugaring (buried in
3718 * parse_cpu_option()), which happens below this point, but may
3719 * only target the CPU type, which can only be created after
3720 * parse_cpu_option() returned the type.
3722 * Machine compat properties: object_set_machine_compat_props().
3723 * Accelerator compat props: object_set_accelerator_compat_props(),
3724 * called from do_configure_accelerator().
3727 machine_class = MACHINE_GET_CLASS(current_machine);
3728 if (!qtest_enabled() && machine_class->deprecation_reason) {
3729 warn_report("Machine type '%s' is deprecated: %s",
3730 machine_class->name, machine_class->deprecation_reason);
3734 * Create backends before creating migration objects, so that it can
3735 * check against compatibilities on the backend memories (e.g. postcopy
3736 * over memory-backend-file objects).
3738 qemu_create_late_backends();
3739 phase_advance(PHASE_LATE_BACKENDS_CREATED);
3742 * Note: creates a QOM object, must run only after global and
3743 * compat properties have been set up.
3745 migration_object_init();
3747 /* parse features once if machine provides default cpu_type */
3748 current_machine->cpu_type = machine_class_default_cpu_type(machine_class);
3749 if (cpu_option) {
3750 current_machine->cpu_type = parse_cpu_option(cpu_option);
3752 /* NB: for machine none cpu_type could STILL be NULL here! */
3754 qemu_resolve_machine_memdev();
3755 parse_numa_opts(current_machine);
3757 if (vmstate_dump_file) {
3758 /* dump and exit */
3759 module_load_qom_all();
3760 dump_vmstate_json_to_file(vmstate_dump_file);
3761 exit(0);
3764 if (!preconfig_requested) {
3765 qmp_x_exit_preconfig(&error_fatal);
3767 qemu_init_displays();
3768 accel_setup_post(current_machine);
3769 os_setup_post();
3770 resume_mux_open();