hw/block/nvme: remove irrelevant zone resource checks
[qemu/rayw.git] / softmmu / vl.c
blob93e78469bcee8eb830db3cabea8aaff4b73cc2ba
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
25 #include "qemu/osdep.h"
26 #include "qemu-common.h"
27 #include "qemu/datadir.h"
28 #include "qemu/units.h"
29 #include "exec/cpu-common.h"
30 #include "hw/qdev-properties.h"
31 #include "qapi/compat-policy.h"
32 #include "qapi/error.h"
33 #include "qapi/qmp/qdict.h"
34 #include "qapi/qmp/qjson.h"
35 #include "qemu-version.h"
36 #include "qemu/cutils.h"
37 #include "qemu/help_option.h"
38 #include "qemu/uuid.h"
39 #include "sysemu/reset.h"
40 #include "sysemu/runstate.h"
41 #include "sysemu/runstate-action.h"
42 #include "sysemu/seccomp.h"
43 #include "sysemu/tcg.h"
44 #include "sysemu/xen.h"
46 #include "qemu/error-report.h"
47 #include "qemu/sockets.h"
48 #include "qemu/accel.h"
49 #include "hw/usb.h"
50 #include "hw/isa/isa.h"
51 #include "hw/scsi/scsi.h"
52 #include "hw/display/vga.h"
53 #include "sysemu/watchdog.h"
54 #include "hw/firmware/smbios.h"
55 #include "hw/acpi/acpi.h"
56 #include "hw/xen/xen.h"
57 #include "hw/loader.h"
58 #include "monitor/qdev.h"
59 #include "net/net.h"
60 #include "net/slirp.h"
61 #include "monitor/monitor.h"
62 #include "ui/console.h"
63 #include "ui/input.h"
64 #include "sysemu/sysemu.h"
65 #include "sysemu/numa.h"
66 #include "sysemu/hostmem.h"
67 #include "exec/gdbstub.h"
68 #include "qemu/timer.h"
69 #include "chardev/char.h"
70 #include "qemu/bitmap.h"
71 #include "qemu/log.h"
72 #include "sysemu/blockdev.h"
73 #include "hw/block/block.h"
74 #include "hw/i386/x86.h"
75 #include "hw/i386/pc.h"
76 #include "migration/misc.h"
77 #include "migration/snapshot.h"
78 #include "sysemu/tpm.h"
79 #include "sysemu/dma.h"
80 #include "hw/audio/soundhw.h"
81 #include "audio/audio.h"
82 #include "sysemu/cpus.h"
83 #include "sysemu/cpu-timers.h"
84 #include "migration/colo.h"
85 #include "migration/postcopy-ram.h"
86 #include "sysemu/kvm.h"
87 #include "sysemu/hax.h"
88 #include "qapi/qobject-input-visitor.h"
89 #include "qemu/option.h"
90 #include "qemu/config-file.h"
91 #include "qemu-options.h"
92 #include "qemu/main-loop.h"
93 #ifdef CONFIG_VIRTFS
94 #include "fsdev/qemu-fsdev.h"
95 #endif
96 #include "sysemu/qtest.h"
98 #include "disas/disas.h"
100 #include "trace.h"
101 #include "trace/control.h"
102 #include "qemu/plugin.h"
103 #include "qemu/queue.h"
104 #include "sysemu/arch_init.h"
105 #include "exec/confidential-guest-support.h"
107 #include "ui/qemu-spice.h"
108 #include "qapi/string-input-visitor.h"
109 #include "qapi/opts-visitor.h"
110 #include "qapi/clone-visitor.h"
111 #include "qom/object_interfaces.h"
112 #include "semihosting/semihost.h"
113 #include "crypto/init.h"
114 #include "sysemu/replay.h"
115 #include "qapi/qapi-events-run-state.h"
116 #include "qapi/qapi-visit-block-core.h"
117 #include "qapi/qapi-visit-compat.h"
118 #include "qapi/qapi-visit-ui.h"
119 #include "qapi/qapi-commands-block-core.h"
120 #include "qapi/qapi-commands-migration.h"
121 #include "qapi/qapi-commands-misc.h"
122 #include "qapi/qapi-visit-qom.h"
123 #include "qapi/qapi-commands-ui.h"
124 #include "qapi/qmp/qerror.h"
125 #include "sysemu/iothread.h"
126 #include "qemu/guest-random.h"
128 #define MAX_VIRTIO_CONSOLES 1
130 typedef struct BlockdevOptionsQueueEntry {
131 BlockdevOptions *bdo;
132 Location loc;
133 QSIMPLEQ_ENTRY(BlockdevOptionsQueueEntry) entry;
134 } BlockdevOptionsQueueEntry;
136 typedef QSIMPLEQ_HEAD(, BlockdevOptionsQueueEntry) BlockdevOptionsQueue;
138 typedef struct ObjectOption {
139 ObjectOptions *opts;
140 QTAILQ_ENTRY(ObjectOption) next;
141 } ObjectOption;
143 static const char *cpu_option;
144 static const char *mem_path;
145 static const char *incoming;
146 static const char *loadvm;
147 static QTAILQ_HEAD(, ObjectOption) object_opts = QTAILQ_HEAD_INITIALIZER(object_opts);
148 static ram_addr_t maxram_size;
149 static uint64_t ram_slots;
150 static int display_remote;
151 static int snapshot;
152 static bool preconfig_requested;
153 static QemuPluginList plugin_list = QTAILQ_HEAD_INITIALIZER(plugin_list);
154 static BlockdevOptionsQueue bdo_queue = QSIMPLEQ_HEAD_INITIALIZER(bdo_queue);
155 static bool nographic = false;
156 static int mem_prealloc; /* force preallocation of physical target memory */
157 static ram_addr_t ram_size;
158 static const char *vga_model = NULL;
159 static DisplayOptions dpy;
160 static int num_serial_hds;
161 static Chardev **serial_hds;
162 static const char *log_mask;
163 static const char *log_file;
164 static bool list_data_dirs;
165 static const char *watchdog;
166 static const char *qtest_chrdev;
167 static const char *qtest_log;
169 static int has_defaults = 1;
170 static int default_serial = 1;
171 static int default_parallel = 1;
172 static int default_monitor = 1;
173 static int default_floppy = 1;
174 static int default_cdrom = 1;
175 static int default_sdcard = 1;
176 static int default_vga = 1;
177 static int default_net = 1;
179 static struct {
180 const char *driver;
181 int *flag;
182 } default_list[] = {
183 { .driver = "isa-serial", .flag = &default_serial },
184 { .driver = "isa-parallel", .flag = &default_parallel },
185 { .driver = "isa-fdc", .flag = &default_floppy },
186 { .driver = "floppy", .flag = &default_floppy },
187 { .driver = "ide-cd", .flag = &default_cdrom },
188 { .driver = "ide-hd", .flag = &default_cdrom },
189 { .driver = "scsi-cd", .flag = &default_cdrom },
190 { .driver = "scsi-hd", .flag = &default_cdrom },
191 { .driver = "VGA", .flag = &default_vga },
192 { .driver = "isa-vga", .flag = &default_vga },
193 { .driver = "cirrus-vga", .flag = &default_vga },
194 { .driver = "isa-cirrus-vga", .flag = &default_vga },
195 { .driver = "vmware-svga", .flag = &default_vga },
196 { .driver = "qxl-vga", .flag = &default_vga },
197 { .driver = "virtio-vga", .flag = &default_vga },
198 { .driver = "ati-vga", .flag = &default_vga },
199 { .driver = "vhost-user-vga", .flag = &default_vga },
202 static QemuOptsList qemu_rtc_opts = {
203 .name = "rtc",
204 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
205 .merge_lists = true,
206 .desc = {
208 .name = "base",
209 .type = QEMU_OPT_STRING,
211 .name = "clock",
212 .type = QEMU_OPT_STRING,
214 .name = "driftfix",
215 .type = QEMU_OPT_STRING,
217 { /* end of list */ }
221 static QemuOptsList qemu_option_rom_opts = {
222 .name = "option-rom",
223 .implied_opt_name = "romfile",
224 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
225 .desc = {
227 .name = "bootindex",
228 .type = QEMU_OPT_NUMBER,
229 }, {
230 .name = "romfile",
231 .type = QEMU_OPT_STRING,
233 { /* end of list */ }
237 static QemuOptsList qemu_machine_opts = {
238 .name = "machine",
239 .implied_opt_name = "type",
240 .merge_lists = true,
241 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
242 .desc = {
244 * no elements => accept any
245 * sanity checking will happen later
246 * when setting machine properties
252 static QemuOptsList qemu_accel_opts = {
253 .name = "accel",
254 .implied_opt_name = "accel",
255 .head = QTAILQ_HEAD_INITIALIZER(qemu_accel_opts.head),
256 .desc = {
258 * no elements => accept any
259 * sanity checking will happen later
260 * when setting accelerator properties
266 static QemuOptsList qemu_boot_opts = {
267 .name = "boot-opts",
268 .implied_opt_name = "order",
269 .merge_lists = true,
270 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
271 .desc = {
273 .name = "order",
274 .type = QEMU_OPT_STRING,
275 }, {
276 .name = "once",
277 .type = QEMU_OPT_STRING,
278 }, {
279 .name = "menu",
280 .type = QEMU_OPT_BOOL,
281 }, {
282 .name = "splash",
283 .type = QEMU_OPT_STRING,
284 }, {
285 .name = "splash-time",
286 .type = QEMU_OPT_NUMBER,
287 }, {
288 .name = "reboot-timeout",
289 .type = QEMU_OPT_NUMBER,
290 }, {
291 .name = "strict",
292 .type = QEMU_OPT_BOOL,
294 { /*End of list */ }
298 static QemuOptsList qemu_add_fd_opts = {
299 .name = "add-fd",
300 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
301 .desc = {
303 .name = "fd",
304 .type = QEMU_OPT_NUMBER,
305 .help = "file descriptor of which a duplicate is added to fd set",
307 .name = "set",
308 .type = QEMU_OPT_NUMBER,
309 .help = "ID of the fd set to add fd to",
311 .name = "opaque",
312 .type = QEMU_OPT_STRING,
313 .help = "free-form string used to describe fd",
315 { /* end of list */ }
319 static QemuOptsList qemu_object_opts = {
320 .name = "object",
321 .implied_opt_name = "qom-type",
322 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
323 .desc = {
328 static QemuOptsList qemu_tpmdev_opts = {
329 .name = "tpmdev",
330 .implied_opt_name = "type",
331 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
332 .desc = {
333 /* options are defined in the TPM backends */
334 { /* end of list */ }
338 static QemuOptsList qemu_overcommit_opts = {
339 .name = "overcommit",
340 .head = QTAILQ_HEAD_INITIALIZER(qemu_overcommit_opts.head),
341 .desc = {
343 .name = "mem-lock",
344 .type = QEMU_OPT_BOOL,
347 .name = "cpu-pm",
348 .type = QEMU_OPT_BOOL,
350 { /* end of list */ }
354 static QemuOptsList qemu_msg_opts = {
355 .name = "msg",
356 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
357 .desc = {
359 .name = "timestamp",
360 .type = QEMU_OPT_BOOL,
363 .name = "guest-name",
364 .type = QEMU_OPT_BOOL,
365 .help = "Prepends guest name for error messages but only if "
366 "-name guest is set otherwise option is ignored\n",
368 { /* end of list */ }
372 static QemuOptsList qemu_name_opts = {
373 .name = "name",
374 .implied_opt_name = "guest",
375 .merge_lists = true,
376 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
377 .desc = {
379 .name = "guest",
380 .type = QEMU_OPT_STRING,
381 .help = "Sets the name of the guest.\n"
382 "This name will be displayed in the SDL window caption.\n"
383 "The name will also be used for the VNC server",
384 }, {
385 .name = "process",
386 .type = QEMU_OPT_STRING,
387 .help = "Sets the name of the QEMU process, as shown in top etc",
388 }, {
389 .name = "debug-threads",
390 .type = QEMU_OPT_BOOL,
391 .help = "When enabled, name the individual threads; defaults off.\n"
392 "NOTE: The thread names are for debugging and not a\n"
393 "stable API.",
395 { /* End of list */ }
399 static QemuOptsList qemu_mem_opts = {
400 .name = "memory",
401 .implied_opt_name = "size",
402 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
403 .merge_lists = true,
404 .desc = {
406 .name = "size",
407 .type = QEMU_OPT_SIZE,
410 .name = "slots",
411 .type = QEMU_OPT_NUMBER,
414 .name = "maxmem",
415 .type = QEMU_OPT_SIZE,
417 { /* end of list */ }
421 static QemuOptsList qemu_icount_opts = {
422 .name = "icount",
423 .implied_opt_name = "shift",
424 .merge_lists = true,
425 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
426 .desc = {
428 .name = "shift",
429 .type = QEMU_OPT_STRING,
430 }, {
431 .name = "align",
432 .type = QEMU_OPT_BOOL,
433 }, {
434 .name = "sleep",
435 .type = QEMU_OPT_BOOL,
436 }, {
437 .name = "rr",
438 .type = QEMU_OPT_STRING,
439 }, {
440 .name = "rrfile",
441 .type = QEMU_OPT_STRING,
442 }, {
443 .name = "rrsnapshot",
444 .type = QEMU_OPT_STRING,
446 { /* end of list */ }
450 static QemuOptsList qemu_fw_cfg_opts = {
451 .name = "fw_cfg",
452 .implied_opt_name = "name",
453 .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
454 .desc = {
456 .name = "name",
457 .type = QEMU_OPT_STRING,
458 .help = "Sets the fw_cfg name of the blob to be inserted",
459 }, {
460 .name = "file",
461 .type = QEMU_OPT_STRING,
462 .help = "Sets the name of the file from which "
463 "the fw_cfg blob will be loaded",
464 }, {
465 .name = "string",
466 .type = QEMU_OPT_STRING,
467 .help = "Sets content of the blob to be inserted from a string",
468 }, {
469 .name = "gen_id",
470 .type = QEMU_OPT_STRING,
471 .help = "Sets id of the object generating the fw_cfg blob "
472 "to be inserted",
474 { /* end of list */ }
478 static QemuOptsList qemu_action_opts = {
479 .name = "action",
480 .merge_lists = true,
481 .head = QTAILQ_HEAD_INITIALIZER(qemu_action_opts.head),
482 .desc = {
484 .name = "shutdown",
485 .type = QEMU_OPT_STRING,
487 .name = "reboot",
488 .type = QEMU_OPT_STRING,
490 .name = "panic",
491 .type = QEMU_OPT_STRING,
493 .name = "watchdog",
494 .type = QEMU_OPT_STRING,
496 { /* end of list */ }
501 * Get machine options
503 * Returns: machine options (never null).
505 static QemuOpts *qemu_get_machine_opts(void)
507 return qemu_find_opts_singleton("machine");
510 const char *qemu_get_vm_name(void)
512 return qemu_name;
515 static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
517 const char *driver = qemu_opt_get(opts, "driver");
518 int i;
520 if (!driver)
521 return 0;
522 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
523 if (strcmp(default_list[i].driver, driver) != 0)
524 continue;
525 *(default_list[i].flag) = 0;
527 return 0;
530 static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
532 const char *proc_name;
534 if (qemu_opt_get(opts, "debug-threads")) {
535 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
537 qemu_name = qemu_opt_get(opts, "guest");
539 proc_name = qemu_opt_get(opts, "process");
540 if (proc_name) {
541 os_set_proc_name(proc_name);
544 return 0;
547 bool defaults_enabled(void)
549 return has_defaults;
552 #ifndef _WIN32
553 static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
555 int fd, dupfd, flags;
556 int64_t fdset_id;
557 const char *fd_opaque = NULL;
558 AddfdInfo *fdinfo;
560 fd = qemu_opt_get_number(opts, "fd", -1);
561 fdset_id = qemu_opt_get_number(opts, "set", -1);
562 fd_opaque = qemu_opt_get(opts, "opaque");
564 if (fd < 0) {
565 error_setg(errp, "fd option is required and must be non-negative");
566 return -1;
569 if (fd <= STDERR_FILENO) {
570 error_setg(errp, "fd cannot be a standard I/O stream");
571 return -1;
575 * All fds inherited across exec() necessarily have FD_CLOEXEC
576 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
578 flags = fcntl(fd, F_GETFD);
579 if (flags == -1 || (flags & FD_CLOEXEC)) {
580 error_setg(errp, "fd is not valid or already in use");
581 return -1;
584 if (fdset_id < 0) {
585 error_setg(errp, "set option is required and must be non-negative");
586 return -1;
589 #ifdef F_DUPFD_CLOEXEC
590 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
591 #else
592 dupfd = dup(fd);
593 if (dupfd != -1) {
594 qemu_set_cloexec(dupfd);
596 #endif
597 if (dupfd == -1) {
598 error_setg(errp, "error duplicating fd: %s", strerror(errno));
599 return -1;
602 /* add the duplicate fd, and optionally the opaque string, to the fd set */
603 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
604 &error_abort);
605 g_free(fdinfo);
607 return 0;
610 static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
612 int fd;
614 fd = qemu_opt_get_number(opts, "fd", -1);
615 close(fd);
617 return 0;
619 #endif
621 /***********************************************************/
622 /* QEMU Block devices */
624 #define HD_OPTS "media=disk"
625 #define CDROM_OPTS "media=cdrom"
626 #define FD_OPTS ""
627 #define PFLASH_OPTS ""
628 #define MTD_OPTS ""
629 #define SD_OPTS ""
631 static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
633 BlockInterfaceType *block_default_type = opaque;
635 return drive_new(opts, *block_default_type, errp) == NULL;
638 static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
640 if (qemu_opt_get(opts, "snapshot") == NULL) {
641 qemu_opt_set(opts, "snapshot", "on", &error_abort);
643 return 0;
646 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
647 int index, const char *optstr)
649 QemuOpts *opts;
650 DriveInfo *dinfo;
652 if (!enable || drive_get_by_index(type, index)) {
653 return;
656 opts = drive_add(type, index, NULL, optstr);
657 if (snapshot) {
658 drive_enable_snapshot(NULL, opts, NULL);
661 dinfo = drive_new(opts, type, &error_abort);
662 dinfo->is_default = true;
666 static void configure_blockdev(BlockdevOptionsQueue *bdo_queue,
667 MachineClass *machine_class, int snapshot)
670 * If the currently selected machine wishes to override the
671 * units-per-bus property of its default HBA interface type, do so
672 * now.
674 if (machine_class->units_per_default_bus) {
675 override_max_devs(machine_class->block_default_type,
676 machine_class->units_per_default_bus);
679 /* open the virtual block devices */
680 while (!QSIMPLEQ_EMPTY(bdo_queue)) {
681 BlockdevOptionsQueueEntry *bdo = QSIMPLEQ_FIRST(bdo_queue);
683 QSIMPLEQ_REMOVE_HEAD(bdo_queue, entry);
684 loc_push_restore(&bdo->loc);
685 qmp_blockdev_add(bdo->bdo, &error_fatal);
686 loc_pop(&bdo->loc);
687 qapi_free_BlockdevOptions(bdo->bdo);
688 g_free(bdo);
690 if (snapshot) {
691 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
692 NULL, NULL);
694 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
695 &machine_class->block_default_type, &error_fatal)) {
696 /* We printed help */
697 exit(0);
700 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
701 CDROM_OPTS);
702 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
703 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
707 static QemuOptsList qemu_smp_opts = {
708 .name = "smp-opts",
709 .implied_opt_name = "cpus",
710 .merge_lists = true,
711 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
712 .desc = {
714 .name = "cpus",
715 .type = QEMU_OPT_NUMBER,
716 }, {
717 .name = "sockets",
718 .type = QEMU_OPT_NUMBER,
719 }, {
720 .name = "dies",
721 .type = QEMU_OPT_NUMBER,
722 }, {
723 .name = "cores",
724 .type = QEMU_OPT_NUMBER,
725 }, {
726 .name = "threads",
727 .type = QEMU_OPT_NUMBER,
728 }, {
729 .name = "maxcpus",
730 .type = QEMU_OPT_NUMBER,
732 { /*End of list */ }
736 static void realtime_init(void)
738 if (enable_mlock) {
739 if (os_mlock() < 0) {
740 error_report("locking memory failed");
741 exit(1);
747 static void configure_msg(QemuOpts *opts)
749 message_with_timestamp = qemu_opt_get_bool(opts, "timestamp", false);
750 error_with_guestname = qemu_opt_get_bool(opts, "guest-name", false);
754 /***********************************************************/
755 /* USB devices */
757 static int usb_device_add(const char *devname)
759 USBDevice *dev = NULL;
761 if (!machine_usb(current_machine)) {
762 return -1;
765 dev = usbdevice_create(devname);
766 if (!dev)
767 return -1;
769 return 0;
772 static int usb_parse(const char *cmdline)
774 int r;
775 r = usb_device_add(cmdline);
776 if (r < 0) {
777 error_report("could not add USB device '%s'", cmdline);
779 return r;
782 /***********************************************************/
783 /* machine registration */
785 static MachineClass *find_machine(const char *name, GSList *machines)
787 GSList *el;
789 for (el = machines; el; el = el->next) {
790 MachineClass *mc = el->data;
792 if (!strcmp(mc->name, name) || !g_strcmp0(mc->alias, name)) {
793 return mc;
797 return NULL;
800 static MachineClass *find_default_machine(GSList *machines)
802 GSList *el;
803 MachineClass *default_machineclass = NULL;
805 for (el = machines; el; el = el->next) {
806 MachineClass *mc = el->data;
808 if (mc->is_default) {
809 assert(default_machineclass == NULL && "Multiple default machines");
810 default_machineclass = mc;
814 return default_machineclass;
817 static int machine_help_func(QemuOpts *opts, MachineState *machine)
819 ObjectProperty *prop;
820 ObjectPropertyIterator iter;
822 if (!qemu_opt_has_help_opt(opts)) {
823 return 0;
826 object_property_iter_init(&iter, OBJECT(machine));
827 while ((prop = object_property_iter_next(&iter))) {
828 if (!prop->set) {
829 continue;
832 printf("%s.%s=%s", MACHINE_GET_CLASS(machine)->name,
833 prop->name, prop->type);
834 if (prop->description) {
835 printf(" (%s)\n", prop->description);
836 } else {
837 printf("\n");
841 return 1;
844 static void version(void)
846 printf("QEMU emulator version " QEMU_FULL_VERSION "\n"
847 QEMU_COPYRIGHT "\n");
850 static void help(int exitcode)
852 version();
853 printf("usage: %s [options] [disk_image]\n\n"
854 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
855 error_get_progname());
857 #define QEMU_OPTIONS_GENERATE_HELP
858 #include "qemu-options-wrapper.h"
860 printf("\nDuring emulation, the following keys are useful:\n"
861 "ctrl-alt-f toggle full screen\n"
862 "ctrl-alt-n switch to virtual console 'n'\n"
863 "ctrl-alt toggle mouse and keyboard grab\n"
864 "\n"
865 "When using -nographic, press 'ctrl-a h' to get some help.\n"
866 "\n"
867 QEMU_HELP_BOTTOM "\n");
869 exit(exitcode);
872 #define HAS_ARG 0x0001
874 typedef struct QEMUOption {
875 const char *name;
876 int flags;
877 int index;
878 uint32_t arch_mask;
879 } QEMUOption;
881 static const QEMUOption qemu_options[] = {
882 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
883 #define QEMU_OPTIONS_GENERATE_OPTIONS
884 #include "qemu-options-wrapper.h"
885 { NULL },
888 typedef struct VGAInterfaceInfo {
889 const char *opt_name; /* option name */
890 const char *name; /* human-readable name */
891 /* Class names indicating that support is available.
892 * If no class is specified, the interface is always available */
893 const char *class_names[2];
894 } VGAInterfaceInfo;
896 static const VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
897 [VGA_NONE] = {
898 .opt_name = "none",
899 .name = "no graphic card",
901 [VGA_STD] = {
902 .opt_name = "std",
903 .name = "standard VGA",
904 .class_names = { "VGA", "isa-vga" },
906 [VGA_CIRRUS] = {
907 .opt_name = "cirrus",
908 .name = "Cirrus VGA",
909 .class_names = { "cirrus-vga", "isa-cirrus-vga" },
911 [VGA_VMWARE] = {
912 .opt_name = "vmware",
913 .name = "VMWare SVGA",
914 .class_names = { "vmware-svga" },
916 [VGA_VIRTIO] = {
917 .opt_name = "virtio",
918 .name = "Virtio VGA",
919 .class_names = { "virtio-vga" },
921 [VGA_QXL] = {
922 .opt_name = "qxl",
923 .name = "QXL VGA",
924 .class_names = { "qxl-vga" },
926 [VGA_TCX] = {
927 .opt_name = "tcx",
928 .name = "TCX framebuffer",
929 .class_names = { "sun-tcx" },
931 [VGA_CG3] = {
932 .opt_name = "cg3",
933 .name = "CG3 framebuffer",
934 .class_names = { "cgthree" },
936 [VGA_XENFB] = {
937 .opt_name = "xenfb",
938 .name = "Xen paravirtualized framebuffer",
942 static bool vga_interface_available(VGAInterfaceType t)
944 const VGAInterfaceInfo *ti = &vga_interfaces[t];
946 assert(t < VGA_TYPE_MAX);
947 return !ti->class_names[0] ||
948 module_object_class_by_name(ti->class_names[0]) ||
949 module_object_class_by_name(ti->class_names[1]);
952 static const char *
953 get_default_vga_model(const MachineClass *machine_class)
955 if (machine_class->default_display) {
956 return machine_class->default_display;
957 } else if (vga_interface_available(VGA_CIRRUS)) {
958 return "cirrus";
959 } else if (vga_interface_available(VGA_STD)) {
960 return "std";
963 return NULL;
966 static void select_vgahw(const MachineClass *machine_class, const char *p)
968 const char *opts;
969 int t;
971 if (g_str_equal(p, "help")) {
972 const char *def = get_default_vga_model(machine_class);
974 for (t = 0; t < VGA_TYPE_MAX; t++) {
975 const VGAInterfaceInfo *ti = &vga_interfaces[t];
977 if (vga_interface_available(t) && ti->opt_name) {
978 printf("%-20s %s%s\n", ti->opt_name, ti->name ?: "",
979 g_str_equal(ti->opt_name, def) ? " (default)" : "");
982 exit(0);
985 assert(vga_interface_type == VGA_NONE);
986 for (t = 0; t < VGA_TYPE_MAX; t++) {
987 const VGAInterfaceInfo *ti = &vga_interfaces[t];
988 if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
989 if (!vga_interface_available(t)) {
990 error_report("%s not available", ti->name);
991 exit(1);
993 vga_interface_type = t;
994 break;
997 if (t == VGA_TYPE_MAX) {
998 invalid_vga:
999 error_report("unknown vga type: %s", p);
1000 exit(1);
1002 while (*opts) {
1003 const char *nextopt;
1005 if (strstart(opts, ",retrace=", &nextopt)) {
1006 opts = nextopt;
1007 if (strstart(opts, "dumb", &nextopt))
1008 vga_retrace_method = VGA_RETRACE_DUMB;
1009 else if (strstart(opts, "precise", &nextopt))
1010 vga_retrace_method = VGA_RETRACE_PRECISE;
1011 else goto invalid_vga;
1012 } else goto invalid_vga;
1013 opts = nextopt;
1017 static void parse_display_qapi(const char *optarg)
1019 DisplayOptions *opts;
1020 Visitor *v;
1022 v = qobject_input_visitor_new_str(optarg, "type", &error_fatal);
1024 visit_type_DisplayOptions(v, NULL, &opts, &error_fatal);
1025 QAPI_CLONE_MEMBERS(DisplayOptions, &dpy, opts);
1027 qapi_free_DisplayOptions(opts);
1028 visit_free(v);
1031 DisplayOptions *qmp_query_display_options(Error **errp)
1033 return QAPI_CLONE(DisplayOptions, &dpy);
1036 static void parse_display(const char *p)
1038 const char *opts;
1040 if (is_help_option(p)) {
1041 qemu_display_help();
1042 exit(0);
1045 if (strstart(p, "sdl", &opts)) {
1047 * sdl DisplayType needs hand-crafted parser instead of
1048 * parse_display_qapi() due to some options not in
1049 * DisplayOptions, specifically:
1050 * - frame
1051 * Already deprecated.
1052 * - ctrl_grab + alt_grab
1053 * Not clear yet what happens to them long-term. Should
1054 * replaced by something better or deprecated and dropped.
1056 dpy.type = DISPLAY_TYPE_SDL;
1057 while (*opts) {
1058 const char *nextopt;
1060 if (strstart(opts, ",alt_grab=", &nextopt)) {
1061 opts = nextopt;
1062 if (strstart(opts, "on", &nextopt)) {
1063 alt_grab = 1;
1064 } else if (strstart(opts, "off", &nextopt)) {
1065 alt_grab = 0;
1066 } else {
1067 goto invalid_sdl_args;
1069 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1070 opts = nextopt;
1071 if (strstart(opts, "on", &nextopt)) {
1072 ctrl_grab = 1;
1073 } else if (strstart(opts, "off", &nextopt)) {
1074 ctrl_grab = 0;
1075 } else {
1076 goto invalid_sdl_args;
1078 } else if (strstart(opts, ",window_close=", &nextopt)) {
1079 opts = nextopt;
1080 dpy.has_window_close = true;
1081 if (strstart(opts, "on", &nextopt)) {
1082 dpy.window_close = true;
1083 } else if (strstart(opts, "off", &nextopt)) {
1084 dpy.window_close = false;
1085 } else {
1086 goto invalid_sdl_args;
1088 } else if (strstart(opts, ",show-cursor=", &nextopt)) {
1089 opts = nextopt;
1090 dpy.has_show_cursor = true;
1091 if (strstart(opts, "on", &nextopt)) {
1092 dpy.show_cursor = true;
1093 } else if (strstart(opts, "off", &nextopt)) {
1094 dpy.show_cursor = false;
1095 } else {
1096 goto invalid_sdl_args;
1098 } else if (strstart(opts, ",gl=", &nextopt)) {
1099 opts = nextopt;
1100 dpy.has_gl = true;
1101 if (strstart(opts, "on", &nextopt)) {
1102 dpy.gl = DISPLAYGL_MODE_ON;
1103 } else if (strstart(opts, "core", &nextopt)) {
1104 dpy.gl = DISPLAYGL_MODE_CORE;
1105 } else if (strstart(opts, "es", &nextopt)) {
1106 dpy.gl = DISPLAYGL_MODE_ES;
1107 } else if (strstart(opts, "off", &nextopt)) {
1108 dpy.gl = DISPLAYGL_MODE_OFF;
1109 } else {
1110 goto invalid_sdl_args;
1112 } else {
1113 invalid_sdl_args:
1114 error_report("invalid SDL option string");
1115 exit(1);
1117 opts = nextopt;
1119 } else if (strstart(p, "vnc", &opts)) {
1121 * vnc isn't a (local) DisplayType but a protocol for remote
1122 * display access.
1124 if (*opts == '=') {
1125 vnc_parse(opts + 1);
1126 } else {
1127 error_report("VNC requires a display argument vnc=<display>");
1128 exit(1);
1130 } else {
1131 parse_display_qapi(p);
1135 static inline bool nonempty_str(const char *str)
1137 return str && *str;
1140 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
1142 gchar *buf;
1143 size_t size;
1144 const char *name, *file, *str, *gen_id;
1145 FWCfgState *fw_cfg = (FWCfgState *) opaque;
1147 if (fw_cfg == NULL) {
1148 error_setg(errp, "fw_cfg device not available");
1149 return -1;
1151 name = qemu_opt_get(opts, "name");
1152 file = qemu_opt_get(opts, "file");
1153 str = qemu_opt_get(opts, "string");
1154 gen_id = qemu_opt_get(opts, "gen_id");
1156 /* we need the name, and exactly one of: file, content string, gen_id */
1157 if (!nonempty_str(name) ||
1158 nonempty_str(file) + nonempty_str(str) + nonempty_str(gen_id) != 1) {
1159 error_setg(errp, "name, plus exactly one of file,"
1160 " string and gen_id, are needed");
1161 return -1;
1163 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
1164 error_setg(errp, "name too long (max. %d char)",
1165 FW_CFG_MAX_FILE_PATH - 1);
1166 return -1;
1168 if (nonempty_str(gen_id)) {
1170 * In this particular case where the content is populated
1171 * internally, the "etc/" namespace protection is relaxed,
1172 * so do not emit a warning.
1174 } else if (strncmp(name, "opt/", 4) != 0) {
1175 warn_report("externally provided fw_cfg item names "
1176 "should be prefixed with \"opt/\"");
1178 if (nonempty_str(str)) {
1179 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
1180 buf = g_memdup(str, size);
1181 } else if (nonempty_str(gen_id)) {
1182 if (!fw_cfg_add_from_generator(fw_cfg, name, gen_id, errp)) {
1183 return -1;
1185 return 0;
1186 } else {
1187 GError *err = NULL;
1188 if (!g_file_get_contents(file, &buf, &size, &err)) {
1189 error_setg(errp, "can't load %s: %s", file, err->message);
1190 g_error_free(err);
1191 return -1;
1194 /* For legacy, keep user files in a specific global order. */
1195 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
1196 fw_cfg_add_file(fw_cfg, name, buf, size);
1197 fw_cfg_reset_order_override(fw_cfg);
1198 return 0;
1201 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
1203 return qdev_device_help(opts);
1206 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
1208 DeviceState *dev;
1210 dev = qdev_device_add(opts, errp);
1211 if (!dev && *errp) {
1212 error_report_err(*errp);
1213 return -1;
1214 } else if (dev) {
1215 object_unref(OBJECT(dev));
1217 return 0;
1220 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
1222 Error *local_err = NULL;
1224 if (!qemu_chr_new_from_opts(opts, NULL, &local_err)) {
1225 if (local_err) {
1226 error_propagate(errp, local_err);
1227 return -1;
1229 exit(0);
1231 return 0;
1234 #ifdef CONFIG_VIRTFS
1235 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
1237 return qemu_fsdev_add(opts, errp);
1239 #endif
1241 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
1243 return monitor_init_opts(opts, errp);
1246 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
1248 static int monitor_device_index = 0;
1249 QemuOpts *opts;
1250 const char *p;
1251 char label[32];
1253 if (strstart(optarg, "chardev:", &p)) {
1254 snprintf(label, sizeof(label), "%s", p);
1255 } else {
1256 snprintf(label, sizeof(label), "compat_monitor%d",
1257 monitor_device_index);
1258 opts = qemu_chr_parse_compat(label, optarg, true);
1259 if (!opts) {
1260 error_report("parse error: %s", optarg);
1261 exit(1);
1265 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
1266 qemu_opt_set(opts, "mode", mode, &error_abort);
1267 qemu_opt_set(opts, "chardev", label, &error_abort);
1268 if (!strcmp(mode, "control")) {
1269 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
1270 } else {
1271 assert(pretty == false);
1273 monitor_device_index++;
1276 struct device_config {
1277 enum {
1278 DEV_USB, /* -usbdevice */
1279 DEV_SERIAL, /* -serial */
1280 DEV_PARALLEL, /* -parallel */
1281 DEV_DEBUGCON, /* -debugcon */
1282 DEV_GDB, /* -gdb, -s */
1283 DEV_SCLP, /* s390 sclp */
1284 } type;
1285 const char *cmdline;
1286 Location loc;
1287 QTAILQ_ENTRY(device_config) next;
1290 static QTAILQ_HEAD(, device_config) device_configs =
1291 QTAILQ_HEAD_INITIALIZER(device_configs);
1293 static void add_device_config(int type, const char *cmdline)
1295 struct device_config *conf;
1297 conf = g_malloc0(sizeof(*conf));
1298 conf->type = type;
1299 conf->cmdline = cmdline;
1300 loc_save(&conf->loc);
1301 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1304 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1306 struct device_config *conf;
1307 int rc;
1309 QTAILQ_FOREACH(conf, &device_configs, next) {
1310 if (conf->type != type)
1311 continue;
1312 loc_push_restore(&conf->loc);
1313 rc = func(conf->cmdline);
1314 loc_pop(&conf->loc);
1315 if (rc) {
1316 return rc;
1319 return 0;
1322 static void qemu_disable_default_devices(void)
1324 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1326 qemu_opts_foreach(qemu_find_opts("device"),
1327 default_driver_check, NULL, NULL);
1328 qemu_opts_foreach(qemu_find_opts("global"),
1329 default_driver_check, NULL, NULL);
1331 if (!vga_model && !default_vga) {
1332 vga_interface_type = VGA_DEVICE;
1334 if (!has_defaults || machine_class->no_serial) {
1335 default_serial = 0;
1337 if (!has_defaults || machine_class->no_parallel) {
1338 default_parallel = 0;
1340 if (!has_defaults || machine_class->no_floppy) {
1341 default_floppy = 0;
1343 if (!has_defaults || machine_class->no_cdrom) {
1344 default_cdrom = 0;
1346 if (!has_defaults || machine_class->no_sdcard) {
1347 default_sdcard = 0;
1349 if (!has_defaults) {
1350 default_monitor = 0;
1351 default_net = 0;
1352 default_vga = 0;
1356 static void qemu_create_default_devices(void)
1358 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1360 if (is_daemonized()) {
1361 /* According to documentation and historically, -nographic redirects
1362 * serial port, parallel port and monitor to stdio, which does not work
1363 * with -daemonize. We can redirect these to null instead, but since
1364 * -nographic is legacy, let's just error out.
1365 * We disallow -nographic only if all other ports are not redirected
1366 * explicitly, to not break existing legacy setups which uses
1367 * -nographic _and_ redirects all ports explicitly - this is valid
1368 * usage, -nographic is just a no-op in this case.
1370 if (nographic
1371 && (default_parallel || default_serial || default_monitor)) {
1372 error_report("-nographic cannot be used with -daemonize");
1373 exit(1);
1377 if (nographic) {
1378 if (default_parallel)
1379 add_device_config(DEV_PARALLEL, "null");
1380 if (default_serial && default_monitor) {
1381 add_device_config(DEV_SERIAL, "mon:stdio");
1382 } else {
1383 if (default_serial)
1384 add_device_config(DEV_SERIAL, "stdio");
1385 if (default_monitor)
1386 monitor_parse("stdio", "readline", false);
1388 } else {
1389 if (default_serial)
1390 add_device_config(DEV_SERIAL, "vc:80Cx24C");
1391 if (default_parallel)
1392 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
1393 if (default_monitor)
1394 monitor_parse("vc:80Cx24C", "readline", false);
1397 if (default_net) {
1398 QemuOptsList *net = qemu_find_opts("net");
1399 qemu_opts_parse(net, "nic", true, &error_abort);
1400 #ifdef CONFIG_SLIRP
1401 qemu_opts_parse(net, "user", true, &error_abort);
1402 #endif
1405 #if defined(CONFIG_VNC)
1406 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
1407 display_remote++;
1409 #endif
1410 if (dpy.type == DISPLAY_TYPE_DEFAULT && !display_remote) {
1411 if (!qemu_display_find_default(&dpy)) {
1412 dpy.type = DISPLAY_TYPE_NONE;
1413 #if defined(CONFIG_VNC)
1414 vnc_parse("localhost:0,to=99,id=default");
1415 #endif
1418 if (dpy.type == DISPLAY_TYPE_DEFAULT) {
1419 dpy.type = DISPLAY_TYPE_NONE;
1422 /* If no default VGA is requested, the default is "none". */
1423 if (default_vga) {
1424 vga_model = get_default_vga_model(machine_class);
1426 if (vga_model) {
1427 select_vgahw(machine_class, vga_model);
1431 static int serial_parse(const char *devname)
1433 int index = num_serial_hds;
1434 char label[32];
1436 if (strcmp(devname, "none") == 0)
1437 return 0;
1438 snprintf(label, sizeof(label), "serial%d", index);
1439 serial_hds = g_renew(Chardev *, serial_hds, index + 1);
1441 serial_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
1442 if (!serial_hds[index]) {
1443 error_report("could not connect serial device"
1444 " to character backend '%s'", devname);
1445 return -1;
1447 num_serial_hds++;
1448 return 0;
1451 Chardev *serial_hd(int i)
1453 assert(i >= 0);
1454 if (i < num_serial_hds) {
1455 return serial_hds[i];
1457 return NULL;
1460 static int parallel_parse(const char *devname)
1462 static int index = 0;
1463 char label[32];
1465 if (strcmp(devname, "none") == 0)
1466 return 0;
1467 if (index == MAX_PARALLEL_PORTS) {
1468 error_report("too many parallel ports");
1469 exit(1);
1471 snprintf(label, sizeof(label), "parallel%d", index);
1472 parallel_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
1473 if (!parallel_hds[index]) {
1474 error_report("could not connect parallel device"
1475 " to character backend '%s'", devname);
1476 return -1;
1478 index++;
1479 return 0;
1482 static int debugcon_parse(const char *devname)
1484 QemuOpts *opts;
1486 if (!qemu_chr_new_mux_mon("debugcon", devname, NULL)) {
1487 error_report("invalid character backend '%s'", devname);
1488 exit(1);
1490 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
1491 if (!opts) {
1492 error_report("already have a debugcon device");
1493 exit(1);
1495 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
1496 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
1497 return 0;
1500 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
1502 const MachineClass *mc1 = a, *mc2 = b;
1503 int res;
1505 if (mc1->family == NULL) {
1506 if (mc2->family == NULL) {
1507 /* Compare standalone machine types against each other; they sort
1508 * in increasing order.
1510 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
1511 object_class_get_name(OBJECT_CLASS(mc2)));
1514 /* Standalone machine types sort after families. */
1515 return 1;
1518 if (mc2->family == NULL) {
1519 /* Families sort before standalone machine types. */
1520 return -1;
1523 /* Families sort between each other alphabetically increasingly. */
1524 res = strcmp(mc1->family, mc2->family);
1525 if (res != 0) {
1526 return res;
1529 /* Within the same family, machine types sort in decreasing order. */
1530 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
1531 object_class_get_name(OBJECT_CLASS(mc1)));
1534 static MachineClass *machine_parse(const char *name, GSList *machines)
1536 MachineClass *mc;
1537 GSList *el;
1539 if (is_help_option(name)) {
1540 printf("Supported machines are:\n");
1541 machines = g_slist_sort(machines, machine_class_cmp);
1542 for (el = machines; el; el = el->next) {
1543 MachineClass *mc = el->data;
1544 if (mc->alias) {
1545 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
1547 printf("%-20s %s%s%s\n", mc->name, mc->desc,
1548 mc->is_default ? " (default)" : "",
1549 mc->deprecation_reason ? " (deprecated)" : "");
1551 exit(0);
1554 mc = find_machine(name, machines);
1555 if (!mc) {
1556 error_report("unsupported machine type");
1557 error_printf("Use -machine help to list supported machines\n");
1558 exit(1);
1560 return mc;
1563 static const char *pid_file;
1564 static Notifier qemu_unlink_pidfile_notifier;
1566 static void qemu_unlink_pidfile(Notifier *n, void *data)
1568 if (pid_file) {
1569 unlink(pid_file);
1573 static const QEMUOption *lookup_opt(int argc, char **argv,
1574 const char **poptarg, int *poptind)
1576 const QEMUOption *popt;
1577 int optind = *poptind;
1578 char *r = argv[optind];
1579 const char *optarg;
1581 loc_set_cmdline(argv, optind, 1);
1582 optind++;
1583 /* Treat --foo the same as -foo. */
1584 if (r[1] == '-')
1585 r++;
1586 popt = qemu_options;
1587 for(;;) {
1588 if (!popt->name) {
1589 error_report("invalid option");
1590 exit(1);
1592 if (!strcmp(popt->name, r + 1))
1593 break;
1594 popt++;
1596 if (popt->flags & HAS_ARG) {
1597 if (optind >= argc) {
1598 error_report("requires an argument");
1599 exit(1);
1601 optarg = argv[optind++];
1602 loc_set_cmdline(argv, optind - 2, 2);
1603 } else {
1604 optarg = NULL;
1607 *poptarg = optarg;
1608 *poptind = optind;
1610 return popt;
1613 static MachineClass *select_machine(void)
1615 GSList *machines = object_class_get_list(TYPE_MACHINE, false);
1616 MachineClass *machine_class = find_default_machine(machines);
1617 const char *optarg;
1618 QemuOpts *opts;
1619 Location loc;
1621 loc_push_none(&loc);
1623 opts = qemu_get_machine_opts();
1624 qemu_opts_loc_restore(opts);
1626 optarg = qemu_opt_get(opts, "type");
1627 if (optarg) {
1628 machine_class = machine_parse(optarg, machines);
1631 if (!machine_class) {
1632 error_report("No machine specified, and there is no default");
1633 error_printf("Use -machine help to list supported machines\n");
1634 exit(1);
1637 loc_pop(&loc);
1638 g_slist_free(machines);
1639 return machine_class;
1642 static int object_parse_property_opt(Object *obj,
1643 const char *name, const char *value,
1644 const char *skip, Error **errp)
1646 if (g_str_equal(name, skip)) {
1647 return 0;
1650 if (!object_property_parse(obj, name, value, errp)) {
1651 return -1;
1654 return 0;
1657 static int machine_set_property(void *opaque,
1658 const char *name, const char *value,
1659 Error **errp)
1661 g_autofree char *qom_name = g_strdup(name);
1662 char *p;
1664 for (p = qom_name; *p; p++) {
1665 if (*p == '_') {
1666 *p = '-';
1670 /* Legacy options do not correspond to MachineState properties. */
1671 if (g_str_equal(qom_name, "accel")) {
1672 return 0;
1674 if (g_str_equal(qom_name, "igd-passthru")) {
1675 object_register_sugar_prop(ACCEL_CLASS_NAME("xen"), qom_name, value,
1676 false);
1677 return 0;
1679 if (g_str_equal(qom_name, "kvm-shadow-mem")) {
1680 object_register_sugar_prop(ACCEL_CLASS_NAME("kvm"), qom_name, value,
1681 false);
1682 return 0;
1684 if (g_str_equal(qom_name, "kernel-irqchip")) {
1685 object_register_sugar_prop(ACCEL_CLASS_NAME("kvm"), qom_name, value,
1686 false);
1687 object_register_sugar_prop(ACCEL_CLASS_NAME("whpx"), qom_name, value,
1688 false);
1689 return 0;
1692 return object_parse_property_opt(opaque, name, value, "type", errp);
1695 static void object_option_foreach_add(bool (*type_opt_predicate)(const char *))
1697 ObjectOption *opt, *next;
1699 QTAILQ_FOREACH_SAFE(opt, &object_opts, next, next) {
1700 const char *type = ObjectType_str(opt->opts->qom_type);
1701 if (type_opt_predicate(type)) {
1702 user_creatable_add_qapi(opt->opts, &error_fatal);
1703 qapi_free_ObjectOptions(opt->opts);
1704 QTAILQ_REMOVE(&object_opts, opt, next);
1705 g_free(opt);
1710 static void object_option_parse(const char *optarg)
1712 ObjectOption *opt;
1713 QemuOpts *opts;
1714 const char *type;
1715 Visitor *v;
1717 if (optarg[0] == '{') {
1718 QObject *obj = qobject_from_json(optarg, &error_fatal);
1720 v = qobject_input_visitor_new(obj);
1721 qobject_unref(obj);
1722 } else {
1723 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
1724 optarg, true);
1725 if (!opts) {
1726 exit(1);
1729 type = qemu_opt_get(opts, "qom-type");
1730 if (!type) {
1731 error_setg(&error_fatal, QERR_MISSING_PARAMETER, "qom-type");
1733 if (user_creatable_print_help(type, opts)) {
1734 exit(0);
1737 v = opts_visitor_new(opts);
1740 opt = g_new0(ObjectOption, 1);
1741 visit_type_ObjectOptions(v, NULL, &opt->opts, &error_fatal);
1742 visit_free(v);
1744 QTAILQ_INSERT_TAIL(&object_opts, opt, next);
1748 * Initial object creation happens before all other
1749 * QEMU data types are created. The majority of objects
1750 * can be created at this point. The rng-egd object
1751 * cannot be created here, as it depends on the chardev
1752 * already existing.
1754 static bool object_create_early(const char *type)
1757 * Objects should not be made "delayed" without a reason. If you
1758 * add one, state the reason in a comment!
1761 /* Reason: rng-egd property "chardev" */
1762 if (g_str_equal(type, "rng-egd")) {
1763 return false;
1766 #if defined(CONFIG_VHOST_USER) && defined(CONFIG_LINUX)
1767 /* Reason: cryptodev-vhost-user property "chardev" */
1768 if (g_str_equal(type, "cryptodev-vhost-user")) {
1769 return false;
1771 #endif
1773 /* Reason: vhost-user-blk-server property "node-name" */
1774 if (g_str_equal(type, "vhost-user-blk-server")) {
1775 return false;
1778 * Reason: filter-* property "netdev" etc.
1780 if (g_str_equal(type, "filter-buffer") ||
1781 g_str_equal(type, "filter-dump") ||
1782 g_str_equal(type, "filter-mirror") ||
1783 g_str_equal(type, "filter-redirector") ||
1784 g_str_equal(type, "colo-compare") ||
1785 g_str_equal(type, "filter-rewriter") ||
1786 g_str_equal(type, "filter-replay")) {
1787 return false;
1791 * Allocation of large amounts of memory may delay
1792 * chardev initialization for too long, and trigger timeouts
1793 * on software that waits for a monitor socket to be created
1794 * (e.g. libvirt).
1796 if (g_str_has_prefix(type, "memory-backend-")) {
1797 return false;
1800 return true;
1803 static void qemu_apply_machine_options(void)
1805 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1806 QemuOpts *machine_opts = qemu_get_machine_opts();
1807 const char *boot_order = NULL;
1808 const char *boot_once = NULL;
1809 QemuOpts *opts;
1811 qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
1812 &error_fatal);
1813 current_machine->ram_size = ram_size;
1814 current_machine->maxram_size = maxram_size;
1815 current_machine->ram_slots = ram_slots;
1817 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
1818 if (opts) {
1819 boot_order = qemu_opt_get(opts, "order");
1820 if (boot_order) {
1821 validate_bootdevices(boot_order, &error_fatal);
1824 boot_once = qemu_opt_get(opts, "once");
1825 if (boot_once) {
1826 validate_bootdevices(boot_once, &error_fatal);
1829 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
1830 boot_strict = qemu_opt_get_bool(opts, "strict", false);
1833 if (!boot_order) {
1834 boot_order = machine_class->default_boot_order;
1837 current_machine->boot_order = boot_order;
1838 current_machine->boot_once = boot_once;
1840 if (semihosting_enabled() && !semihosting_get_argc()) {
1841 const char *kernel_filename = qemu_opt_get(machine_opts, "kernel");
1842 const char *kernel_cmdline = qemu_opt_get(machine_opts, "append") ?: "";
1843 /* fall back to the -kernel/-append */
1844 semihosting_arg_fallback(kernel_filename, kernel_cmdline);
1848 static void qemu_create_early_backends(void)
1850 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1852 if ((alt_grab || ctrl_grab) && dpy.type != DISPLAY_TYPE_SDL) {
1853 error_report("-alt-grab and -ctrl-grab are only valid "
1854 "for SDL, ignoring option");
1856 if (dpy.has_window_close &&
1857 (dpy.type != DISPLAY_TYPE_GTK && dpy.type != DISPLAY_TYPE_SDL)) {
1858 error_report("-no-quit is only valid for GTK and SDL, "
1859 "ignoring option");
1862 qemu_display_early_init(&dpy);
1863 qemu_console_early_init();
1865 if (dpy.has_gl && dpy.gl != DISPLAYGL_MODE_OFF && display_opengl == 0) {
1866 #if defined(CONFIG_OPENGL)
1867 error_report("OpenGL is not supported by the display");
1868 #else
1869 error_report("OpenGL support is disabled");
1870 #endif
1871 exit(1);
1874 object_option_foreach_add(object_create_early);
1876 /* spice needs the timers to be initialized by this point */
1877 /* spice must initialize before audio as it changes the default auiodev */
1878 /* spice must initialize before chardevs (for spicevmc and spiceport) */
1879 qemu_spice.init();
1881 qemu_opts_foreach(qemu_find_opts("chardev"),
1882 chardev_init_func, NULL, &error_fatal);
1884 #ifdef CONFIG_VIRTFS
1885 qemu_opts_foreach(qemu_find_opts("fsdev"),
1886 fsdev_init_func, NULL, &error_fatal);
1887 #endif
1890 * Note: we need to create audio and block backends before
1891 * machine_set_property(), so machine properties can refer to
1892 * them.
1894 configure_blockdev(&bdo_queue, machine_class, snapshot);
1895 audio_init_audiodevs();
1900 * The remainder of object creation happens after the
1901 * creation of chardev, fsdev, net clients and device data types.
1903 static bool object_create_late(const char *type)
1905 return !object_create_early(type);
1908 static void qemu_create_late_backends(void)
1910 if (qtest_chrdev) {
1911 qtest_server_init(qtest_chrdev, qtest_log, &error_fatal);
1914 net_init_clients(&error_fatal);
1916 object_option_foreach_add(object_create_late);
1918 if (tpm_init() < 0) {
1919 exit(1);
1922 qemu_opts_foreach(qemu_find_opts("mon"),
1923 mon_init_func, NULL, &error_fatal);
1925 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
1926 exit(1);
1927 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
1928 exit(1);
1929 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
1930 exit(1);
1932 /* now chardevs have been created we may have semihosting to connect */
1933 qemu_semihosting_connect_chardevs();
1934 qemu_semihosting_console_init();
1937 static bool have_custom_ram_size(void)
1939 QemuOpts *opts = qemu_find_opts_singleton("memory");
1940 return !!qemu_opt_get_size(opts, "size", 0);
1943 static void qemu_resolve_machine_memdev(void)
1945 if (current_machine->ram_memdev_id) {
1946 Object *backend;
1947 ram_addr_t backend_size;
1949 backend = object_resolve_path_type(current_machine->ram_memdev_id,
1950 TYPE_MEMORY_BACKEND, NULL);
1951 if (!backend) {
1952 error_report("Memory backend '%s' not found",
1953 current_machine->ram_memdev_id);
1954 exit(EXIT_FAILURE);
1956 backend_size = object_property_get_uint(backend, "size", &error_abort);
1957 if (have_custom_ram_size() && backend_size != ram_size) {
1958 error_report("Size specified by -m option must match size of "
1959 "explicitly specified 'memory-backend' property");
1960 exit(EXIT_FAILURE);
1962 if (mem_path) {
1963 error_report("'-mem-path' can't be used together with"
1964 "'-machine memory-backend'");
1965 exit(EXIT_FAILURE);
1967 ram_size = backend_size;
1970 if (!xen_enabled()) {
1971 /* On 32-bit hosts, QEMU is limited by virtual address space */
1972 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
1973 error_report("at most 2047 MB RAM can be simulated");
1974 exit(1);
1979 static void set_memory_options(MachineClass *mc)
1981 uint64_t sz;
1982 const char *mem_str;
1983 const ram_addr_t default_ram_size = mc->default_ram_size;
1984 QemuOpts *opts = qemu_find_opts_singleton("memory");
1985 Location loc;
1987 loc_push_none(&loc);
1988 qemu_opts_loc_restore(opts);
1990 sz = 0;
1991 mem_str = qemu_opt_get(opts, "size");
1992 if (mem_str) {
1993 if (!*mem_str) {
1994 error_report("missing 'size' option value");
1995 exit(EXIT_FAILURE);
1998 sz = qemu_opt_get_size(opts, "size", ram_size);
2000 /* Fix up legacy suffix-less format */
2001 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2002 uint64_t overflow_check = sz;
2004 sz *= MiB;
2005 if (sz / MiB != overflow_check) {
2006 error_report("too large 'size' option value");
2007 exit(EXIT_FAILURE);
2012 /* backward compatibility behaviour for case "-m 0" */
2013 if (sz == 0) {
2014 sz = default_ram_size;
2017 sz = QEMU_ALIGN_UP(sz, 8192);
2018 if (mc->fixup_ram_size) {
2019 sz = mc->fixup_ram_size(sz);
2021 ram_size = sz;
2022 if (ram_size != sz) {
2023 error_report("ram size too large");
2024 exit(EXIT_FAILURE);
2027 /* store value for the future use */
2028 qemu_opt_set_number(opts, "size", ram_size, &error_abort);
2029 maxram_size = ram_size;
2031 if (qemu_opt_get(opts, "maxmem")) {
2032 uint64_t slots;
2034 sz = qemu_opt_get_size(opts, "maxmem", 0);
2035 slots = qemu_opt_get_number(opts, "slots", 0);
2036 if (sz < ram_size) {
2037 error_report("invalid value of -m option maxmem: "
2038 "maximum memory size (0x%" PRIx64 ") must be at least "
2039 "the initial memory size (0x" RAM_ADDR_FMT ")",
2040 sz, ram_size);
2041 exit(EXIT_FAILURE);
2042 } else if (slots && sz == ram_size) {
2043 error_report("invalid value of -m option maxmem: "
2044 "memory slots were specified but maximum memory size "
2045 "(0x%" PRIx64 ") is equal to the initial memory size "
2046 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
2047 exit(EXIT_FAILURE);
2050 maxram_size = sz;
2051 ram_slots = slots;
2052 } else if (qemu_opt_get(opts, "slots")) {
2053 error_report("invalid -m option value: missing 'maxmem' option");
2054 exit(EXIT_FAILURE);
2057 loc_pop(&loc);
2060 static void qemu_create_machine(MachineClass *machine_class)
2062 object_set_machine_compat_props(machine_class->compat_props);
2064 set_memory_options(machine_class);
2066 current_machine = MACHINE(object_new_with_class(OBJECT_CLASS(machine_class)));
2067 if (machine_help_func(qemu_get_machine_opts(), current_machine)) {
2068 exit(0);
2070 object_property_add_child(object_get_root(), "machine",
2071 OBJECT(current_machine));
2072 object_property_add_child(container_get(OBJECT(current_machine),
2073 "/unattached"),
2074 "sysbus", OBJECT(sysbus_get_default()));
2076 if (machine_class->minimum_page_bits) {
2077 if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
2078 /* This would be a board error: specifying a minimum smaller than
2079 * a target's compile-time fixed setting.
2081 g_assert_not_reached();
2085 cpu_exec_init_all();
2086 page_size_init();
2088 if (machine_class->hw_version) {
2089 qemu_set_hw_version(machine_class->hw_version);
2092 machine_smp_parse(current_machine,
2093 qemu_opts_find(qemu_find_opts("smp-opts"), NULL), &error_fatal);
2096 * Get the default machine options from the machine if it is not already
2097 * specified either by the configuration file or by the command line.
2099 if (machine_class->default_machine_opts) {
2100 qemu_opts_set_defaults(qemu_find_opts("machine"),
2101 machine_class->default_machine_opts, 0);
2105 static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
2107 GlobalProperty *g;
2109 g = g_malloc0(sizeof(*g));
2110 g->driver = qemu_opt_get(opts, "driver");
2111 g->property = qemu_opt_get(opts, "property");
2112 g->value = qemu_opt_get(opts, "value");
2113 qdev_prop_register_global(g);
2114 return 0;
2117 static void qemu_read_default_config_file(Error **errp)
2119 ERRP_GUARD();
2120 int ret;
2121 g_autofree char *file = get_relocated_path(CONFIG_QEMU_CONFDIR "/qemu.conf");
2123 ret = qemu_read_config_file(file, errp);
2124 if (ret < 0) {
2125 if (ret == -ENOENT) {
2126 error_free(*errp);
2127 *errp = NULL;
2132 static int qemu_set_option(const char *str)
2134 Error *local_err = NULL;
2135 char group[64], id[64], arg[64];
2136 QemuOptsList *list;
2137 QemuOpts *opts;
2138 int rc, offset;
2140 rc = sscanf(str, "%63[^.].%63[^.].%63[^=]%n", group, id, arg, &offset);
2141 if (rc < 3 || str[offset] != '=') {
2142 error_report("can't parse: \"%s\"", str);
2143 return -1;
2146 list = qemu_find_opts(group);
2147 if (list == NULL) {
2148 return -1;
2151 opts = qemu_opts_find(list, id);
2152 if (!opts) {
2153 error_report("there is no %s \"%s\" defined",
2154 list->name, id);
2155 return -1;
2158 if (!qemu_opt_set(opts, arg, str + offset + 1, &local_err)) {
2159 error_report_err(local_err);
2160 return -1;
2162 return 0;
2165 static void user_register_global_props(void)
2167 qemu_opts_foreach(qemu_find_opts("global"),
2168 global_init_func, NULL, NULL);
2171 static int do_configure_icount(void *opaque, QemuOpts *opts, Error **errp)
2173 icount_configure(opts, errp);
2174 return 0;
2177 static int accelerator_set_property(void *opaque,
2178 const char *name, const char *value,
2179 Error **errp)
2181 return object_parse_property_opt(opaque, name, value, "accel", errp);
2184 static int do_configure_accelerator(void *opaque, QemuOpts *opts, Error **errp)
2186 bool *p_init_failed = opaque;
2187 const char *acc = qemu_opt_get(opts, "accel");
2188 AccelClass *ac = accel_find(acc);
2189 AccelState *accel;
2190 int ret;
2191 bool qtest_with_kvm;
2193 qtest_with_kvm = g_str_equal(acc, "kvm") && qtest_chrdev != NULL;
2195 if (!ac) {
2196 *p_init_failed = true;
2197 if (!qtest_with_kvm) {
2198 error_report("invalid accelerator %s", acc);
2200 return 0;
2202 accel = ACCEL(object_new_with_class(OBJECT_CLASS(ac)));
2203 object_apply_compat_props(OBJECT(accel));
2204 qemu_opt_foreach(opts, accelerator_set_property,
2205 accel,
2206 &error_fatal);
2208 ret = accel_init_machine(accel, current_machine);
2209 if (ret < 0) {
2210 *p_init_failed = true;
2211 if (!qtest_with_kvm || ret != -ENOENT) {
2212 error_report("failed to initialize %s: %s", acc, strerror(-ret));
2214 return 0;
2217 return 1;
2220 static void configure_accelerators(const char *progname)
2222 const char *accelerators;
2223 bool init_failed = false;
2225 qemu_opts_foreach(qemu_find_opts("icount"),
2226 do_configure_icount, NULL, &error_fatal);
2228 accelerators = qemu_opt_get(qemu_get_machine_opts(), "accel");
2229 if (QTAILQ_EMPTY(&qemu_accel_opts.head)) {
2230 char **accel_list, **tmp;
2232 if (accelerators == NULL) {
2233 /* Select the default accelerator */
2234 bool have_tcg = accel_find("tcg");
2235 bool have_kvm = accel_find("kvm");
2237 if (have_tcg && have_kvm) {
2238 if (g_str_has_suffix(progname, "kvm")) {
2239 /* If the program name ends with "kvm", we prefer KVM */
2240 accelerators = "kvm:tcg";
2241 } else {
2242 accelerators = "tcg:kvm";
2244 } else if (have_kvm) {
2245 accelerators = "kvm";
2246 } else if (have_tcg) {
2247 accelerators = "tcg";
2248 } else {
2249 error_report("No accelerator selected and"
2250 " no default accelerator available");
2251 exit(1);
2254 accel_list = g_strsplit(accelerators, ":", 0);
2256 for (tmp = accel_list; *tmp; tmp++) {
2258 * Filter invalid accelerators here, to prevent obscenities
2259 * such as "-machine accel=tcg,,thread=single".
2261 if (accel_find(*tmp)) {
2262 qemu_opts_parse_noisily(qemu_find_opts("accel"), *tmp, true);
2263 } else {
2264 init_failed = true;
2265 error_report("invalid accelerator %s", *tmp);
2268 g_strfreev(accel_list);
2269 } else {
2270 if (accelerators != NULL) {
2271 error_report("The -accel and \"-machine accel=\" options are incompatible");
2272 exit(1);
2276 if (!qemu_opts_foreach(qemu_find_opts("accel"),
2277 do_configure_accelerator, &init_failed, &error_fatal)) {
2278 if (!init_failed) {
2279 error_report("no accelerator found");
2281 exit(1);
2284 if (init_failed && !qtest_chrdev) {
2285 AccelClass *ac = ACCEL_GET_CLASS(current_accel());
2286 error_report("falling back to %s", ac->name);
2289 if (icount_enabled() && !tcg_enabled()) {
2290 error_report("-icount is not allowed with hardware virtualization");
2291 exit(1);
2295 static void create_default_memdev(MachineState *ms, const char *path)
2297 Object *obj;
2298 MachineClass *mc = MACHINE_GET_CLASS(ms);
2300 obj = object_new(path ? TYPE_MEMORY_BACKEND_FILE : TYPE_MEMORY_BACKEND_RAM);
2301 if (path) {
2302 object_property_set_str(obj, "mem-path", path, &error_fatal);
2304 object_property_set_int(obj, "size", ms->ram_size, &error_fatal);
2305 object_property_add_child(object_get_objects_root(), mc->default_ram_id,
2306 obj);
2307 /* Ensure backend's memory region name is equal to mc->default_ram_id */
2308 object_property_set_bool(obj, "x-use-canonical-path-for-ramblock-id",
2309 false, &error_fatal);
2310 user_creatable_complete(USER_CREATABLE(obj), &error_fatal);
2311 object_unref(obj);
2312 object_property_set_str(OBJECT(ms), "memory-backend", mc->default_ram_id,
2313 &error_fatal);
2316 static void qemu_validate_options(void)
2318 QemuOpts *machine_opts = qemu_get_machine_opts();
2319 const char *kernel_filename = qemu_opt_get(machine_opts, "kernel");
2320 const char *initrd_filename = qemu_opt_get(machine_opts, "initrd");
2321 const char *kernel_cmdline = qemu_opt_get(machine_opts, "append");
2323 if (kernel_filename == NULL) {
2324 if (kernel_cmdline != NULL) {
2325 error_report("-append only allowed with -kernel option");
2326 exit(1);
2329 if (initrd_filename != NULL) {
2330 error_report("-initrd only allowed with -kernel option");
2331 exit(1);
2335 if (loadvm && preconfig_requested) {
2336 error_report("'preconfig' and 'loadvm' options are "
2337 "mutually exclusive");
2338 exit(EXIT_FAILURE);
2340 if (incoming && preconfig_requested && strcmp(incoming, "defer") != 0) {
2341 error_report("'preconfig' supports '-incoming defer' only");
2342 exit(EXIT_FAILURE);
2345 #ifdef CONFIG_CURSES
2346 if (is_daemonized() && dpy.type == DISPLAY_TYPE_CURSES) {
2347 error_report("curses display cannot be used with -daemonize");
2348 exit(1);
2350 #endif
2353 static void qemu_process_sugar_options(void)
2355 if (mem_prealloc) {
2356 char *val;
2358 val = g_strdup_printf("%d",
2359 (uint32_t) qemu_opt_get_number(qemu_find_opts_singleton("smp-opts"), "cpus", 1));
2360 object_register_sugar_prop("memory-backend", "prealloc-threads", val,
2361 false);
2362 g_free(val);
2363 object_register_sugar_prop("memory-backend", "prealloc", "on", false);
2366 if (watchdog) {
2367 int i = select_watchdog(watchdog);
2368 if (i > 0)
2369 exit (i == 1 ? 1 : 0);
2373 /* -action processing */
2376 * Process all the -action parameters parsed from cmdline.
2378 static int process_runstate_actions(void *opaque, QemuOpts *opts, Error **errp)
2380 Error *local_err = NULL;
2381 QDict *qdict = qemu_opts_to_qdict(opts, NULL);
2382 QObject *ret = NULL;
2383 qmp_marshal_set_action(qdict, &ret, &local_err);
2384 qobject_unref(ret);
2385 qobject_unref(qdict);
2386 if (local_err) {
2387 error_propagate(errp, local_err);
2388 return 1;
2390 return 0;
2393 static void qemu_process_early_options(void)
2395 #ifdef CONFIG_SECCOMP
2396 QemuOptsList *olist = qemu_find_opts_err("sandbox", NULL);
2397 if (olist) {
2398 qemu_opts_foreach(olist, parse_sandbox, NULL, &error_fatal);
2400 #endif
2402 qemu_opts_foreach(qemu_find_opts("name"),
2403 parse_name, NULL, &error_fatal);
2405 if (qemu_opts_foreach(qemu_find_opts("action"),
2406 process_runstate_actions, NULL, &error_fatal)) {
2407 exit(1);
2410 #ifndef _WIN32
2411 qemu_opts_foreach(qemu_find_opts("add-fd"),
2412 parse_add_fd, NULL, &error_fatal);
2414 qemu_opts_foreach(qemu_find_opts("add-fd"),
2415 cleanup_add_fd, NULL, &error_fatal);
2416 #endif
2418 /* Open the logfile at this point and set the log mask if necessary. */
2419 if (log_file) {
2420 qemu_set_log_filename(log_file, &error_fatal);
2422 if (log_mask) {
2423 int mask;
2424 mask = qemu_str_to_log_mask(log_mask);
2425 if (!mask) {
2426 qemu_print_log_usage(stdout);
2427 exit(1);
2429 qemu_set_log(mask);
2430 } else {
2431 qemu_set_log(0);
2434 qemu_add_default_firmwarepath();
2437 static void qemu_process_help_options(void)
2440 * Check for -cpu help and -device help before we call select_machine(),
2441 * which will return an error if the architecture has no default machine
2442 * type and the user did not specify one, so that the user doesn't need
2443 * to say '-cpu help -machine something'.
2445 if (cpu_option && is_help_option(cpu_option)) {
2446 list_cpus(cpu_option);
2447 exit(0);
2450 if (qemu_opts_foreach(qemu_find_opts("device"),
2451 device_help_func, NULL, NULL)) {
2452 exit(0);
2455 /* -L help lists the data directories and exits. */
2456 if (list_data_dirs) {
2457 qemu_list_data_dirs();
2458 exit(0);
2462 static void qemu_maybe_daemonize(const char *pid_file)
2464 Error *err;
2466 os_daemonize();
2467 rcu_disable_atfork();
2469 if (pid_file && !qemu_write_pidfile(pid_file, &err)) {
2470 error_reportf_err(err, "cannot create PID file: ");
2471 exit(1);
2474 qemu_unlink_pidfile_notifier.notify = qemu_unlink_pidfile;
2475 qemu_add_exit_notifier(&qemu_unlink_pidfile_notifier);
2478 static void qemu_init_displays(void)
2480 DisplayState *ds;
2482 /* init local displays */
2483 ds = init_displaystate();
2484 qemu_display_init(ds, &dpy);
2486 /* must be after terminal init, SDL library changes signal handlers */
2487 os_setup_signal_handling();
2489 /* init remote displays */
2490 #ifdef CONFIG_VNC
2491 qemu_opts_foreach(qemu_find_opts("vnc"),
2492 vnc_init_func, NULL, &error_fatal);
2493 #endif
2495 if (using_spice) {
2496 qemu_spice.display_init();
2500 static void qemu_init_board(void)
2502 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
2504 if (machine_class->default_ram_id && current_machine->ram_size &&
2505 numa_uses_legacy_mem() && !current_machine->ram_memdev_id) {
2506 create_default_memdev(current_machine, mem_path);
2509 /* process plugin before CPUs are created, but once -smp has been parsed */
2510 qemu_plugin_load_list(&plugin_list, &error_fatal);
2512 /* From here on we enter MACHINE_PHASE_INITIALIZED. */
2513 machine_run_board_init(current_machine);
2515 drive_check_orphaned();
2517 realtime_init();
2519 if (hax_enabled()) {
2520 /* FIXME: why isn't cpu_synchronize_all_post_init enough? */
2521 hax_sync_vcpus();
2525 static void qemu_create_cli_devices(void)
2527 soundhw_init();
2529 qemu_opts_foreach(qemu_find_opts("fw_cfg"),
2530 parse_fw_cfg, fw_cfg_find(), &error_fatal);
2532 /* init USB devices */
2533 if (machine_usb(current_machine)) {
2534 if (foreach_device_config(DEV_USB, usb_parse) < 0)
2535 exit(1);
2538 /* init generic devices */
2539 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
2540 qemu_opts_foreach(qemu_find_opts("device"),
2541 device_init_func, NULL, &error_fatal);
2542 rom_reset_order_override();
2545 static void qemu_machine_creation_done(void)
2547 MachineState *machine = MACHINE(qdev_get_machine());
2549 /* Did we create any drives that we failed to create a device for? */
2550 drive_check_orphaned();
2552 /* Don't warn about the default network setup that you get if
2553 * no command line -net or -netdev options are specified. There
2554 * are two cases that we would otherwise complain about:
2555 * (1) board doesn't support a NIC but the implicit "-net nic"
2556 * requested one
2557 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
2558 * sets up a nic that isn't connected to anything.
2560 if (!default_net && (!qtest_enabled() || has_defaults)) {
2561 net_check_clients();
2564 qdev_prop_check_globals();
2566 qdev_machine_creation_done();
2568 if (machine->cgs) {
2570 * Verify that Confidential Guest Support has actually been initialized
2572 assert(machine->cgs->ready);
2575 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
2576 exit(1);
2580 void qmp_x_exit_preconfig(Error **errp)
2582 if (phase_check(PHASE_MACHINE_INITIALIZED)) {
2583 error_setg(errp, "The command is permitted only before machine initialization");
2584 return;
2587 qemu_init_board();
2588 qemu_create_cli_devices();
2589 qemu_machine_creation_done();
2591 if (loadvm) {
2592 Error *local_err = NULL;
2593 if (!load_snapshot(loadvm, NULL, false, NULL, &local_err)) {
2594 error_report_err(local_err);
2595 autostart = 0;
2596 exit(1);
2599 if (replay_mode != REPLAY_MODE_NONE) {
2600 replay_vmstate_init();
2603 if (incoming) {
2604 Error *local_err = NULL;
2605 if (strcmp(incoming, "defer") != 0) {
2606 qmp_migrate_incoming(incoming, &local_err);
2607 if (local_err) {
2608 error_reportf_err(local_err, "-incoming %s: ", incoming);
2609 exit(1);
2612 } else if (autostart) {
2613 qmp_cont(NULL);
2617 void qemu_init(int argc, char **argv, char **envp)
2619 QemuOpts *opts;
2620 QemuOpts *icount_opts = NULL, *accel_opts = NULL;
2621 QemuOptsList *olist;
2622 int optind;
2623 const char *optarg;
2624 MachineClass *machine_class;
2625 bool userconfig = true;
2626 FILE *vmstate_dump_file = NULL;
2628 qemu_add_opts(&qemu_drive_opts);
2629 qemu_add_drive_opts(&qemu_legacy_drive_opts);
2630 qemu_add_drive_opts(&qemu_common_drive_opts);
2631 qemu_add_drive_opts(&qemu_drive_opts);
2632 qemu_add_drive_opts(&bdrv_runtime_opts);
2633 qemu_add_opts(&qemu_chardev_opts);
2634 qemu_add_opts(&qemu_device_opts);
2635 qemu_add_opts(&qemu_netdev_opts);
2636 qemu_add_opts(&qemu_nic_opts);
2637 qemu_add_opts(&qemu_net_opts);
2638 qemu_add_opts(&qemu_rtc_opts);
2639 qemu_add_opts(&qemu_global_opts);
2640 qemu_add_opts(&qemu_mon_opts);
2641 qemu_add_opts(&qemu_trace_opts);
2642 qemu_plugin_add_opts();
2643 qemu_add_opts(&qemu_option_rom_opts);
2644 qemu_add_opts(&qemu_machine_opts);
2645 qemu_add_opts(&qemu_accel_opts);
2646 qemu_add_opts(&qemu_mem_opts);
2647 qemu_add_opts(&qemu_smp_opts);
2648 qemu_add_opts(&qemu_boot_opts);
2649 qemu_add_opts(&qemu_add_fd_opts);
2650 qemu_add_opts(&qemu_object_opts);
2651 qemu_add_opts(&qemu_tpmdev_opts);
2652 qemu_add_opts(&qemu_overcommit_opts);
2653 qemu_add_opts(&qemu_msg_opts);
2654 qemu_add_opts(&qemu_name_opts);
2655 qemu_add_opts(&qemu_numa_opts);
2656 qemu_add_opts(&qemu_icount_opts);
2657 qemu_add_opts(&qemu_semihosting_config_opts);
2658 qemu_add_opts(&qemu_fw_cfg_opts);
2659 qemu_add_opts(&qemu_action_opts);
2660 module_call_init(MODULE_INIT_OPTS);
2662 error_init(argv[0]);
2663 qemu_init_exec_dir(argv[0]);
2665 qemu_init_subsystems();
2667 /* first pass of option parsing */
2668 optind = 1;
2669 while (optind < argc) {
2670 if (argv[optind][0] != '-') {
2671 /* disk image */
2672 optind++;
2673 } else {
2674 const QEMUOption *popt;
2676 popt = lookup_opt(argc, argv, &optarg, &optind);
2677 switch (popt->index) {
2678 case QEMU_OPTION_nouserconfig:
2679 userconfig = false;
2680 break;
2685 if (userconfig) {
2686 qemu_read_default_config_file(&error_fatal);
2689 /* second pass of option parsing */
2690 optind = 1;
2691 for(;;) {
2692 if (optind >= argc)
2693 break;
2694 if (argv[optind][0] != '-') {
2695 loc_set_cmdline(argv, optind, 1);
2696 drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2697 } else {
2698 const QEMUOption *popt;
2700 popt = lookup_opt(argc, argv, &optarg, &optind);
2701 if (!(popt->arch_mask & arch_type)) {
2702 error_report("Option not supported for this target");
2703 exit(1);
2705 switch(popt->index) {
2706 case QEMU_OPTION_cpu:
2707 /* hw initialization will check this */
2708 cpu_option = optarg;
2709 break;
2710 case QEMU_OPTION_hda:
2711 case QEMU_OPTION_hdb:
2712 case QEMU_OPTION_hdc:
2713 case QEMU_OPTION_hdd:
2714 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2715 HD_OPTS);
2716 break;
2717 case QEMU_OPTION_blockdev:
2719 Visitor *v;
2720 BlockdevOptionsQueueEntry *bdo;
2722 v = qobject_input_visitor_new_str(optarg, "driver",
2723 &error_fatal);
2725 bdo = g_new(BlockdevOptionsQueueEntry, 1);
2726 visit_type_BlockdevOptions(v, NULL, &bdo->bdo,
2727 &error_fatal);
2728 visit_free(v);
2729 loc_save(&bdo->loc);
2730 QSIMPLEQ_INSERT_TAIL(&bdo_queue, bdo, entry);
2731 break;
2733 case QEMU_OPTION_drive:
2734 if (drive_def(optarg) == NULL) {
2735 exit(1);
2737 break;
2738 case QEMU_OPTION_set:
2739 if (qemu_set_option(optarg) != 0)
2740 exit(1);
2741 break;
2742 case QEMU_OPTION_global:
2743 if (qemu_global_option(optarg) != 0)
2744 exit(1);
2745 break;
2746 case QEMU_OPTION_mtdblock:
2747 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2748 break;
2749 case QEMU_OPTION_sd:
2750 drive_add(IF_SD, -1, optarg, SD_OPTS);
2751 break;
2752 case QEMU_OPTION_pflash:
2753 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2754 break;
2755 case QEMU_OPTION_snapshot:
2757 Error *blocker = NULL;
2758 snapshot = 1;
2759 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
2760 "-snapshot");
2761 replay_add_blocker(blocker);
2763 break;
2764 case QEMU_OPTION_numa:
2765 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
2766 optarg, true);
2767 if (!opts) {
2768 exit(1);
2770 break;
2771 case QEMU_OPTION_display:
2772 parse_display(optarg);
2773 break;
2774 case QEMU_OPTION_nographic:
2775 olist = qemu_find_opts("machine");
2776 qemu_opts_parse_noisily(olist, "graphics=off", false);
2777 nographic = true;
2778 dpy.type = DISPLAY_TYPE_NONE;
2779 break;
2780 case QEMU_OPTION_curses:
2781 #ifdef CONFIG_CURSES
2782 dpy.type = DISPLAY_TYPE_CURSES;
2783 #else
2784 error_report("curses or iconv support is disabled");
2785 exit(1);
2786 #endif
2787 break;
2788 case QEMU_OPTION_portrait:
2789 graphic_rotate = 90;
2790 break;
2791 case QEMU_OPTION_rotate:
2792 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2793 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2794 graphic_rotate != 180 && graphic_rotate != 270) {
2795 error_report("only 90, 180, 270 deg rotation is available");
2796 exit(1);
2798 break;
2799 case QEMU_OPTION_kernel:
2800 qemu_opts_set(qemu_find_opts("machine"), "kernel", optarg, &error_abort);
2801 break;
2802 case QEMU_OPTION_initrd:
2803 qemu_opts_set(qemu_find_opts("machine"), "initrd", optarg, &error_abort);
2804 break;
2805 case QEMU_OPTION_append:
2806 qemu_opts_set(qemu_find_opts("machine"), "append", optarg, &error_abort);
2807 break;
2808 case QEMU_OPTION_dtb:
2809 qemu_opts_set(qemu_find_opts("machine"), "dtb", optarg, &error_abort);
2810 break;
2811 case QEMU_OPTION_cdrom:
2812 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
2813 break;
2814 case QEMU_OPTION_boot:
2815 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
2816 optarg, true);
2817 if (!opts) {
2818 exit(1);
2820 break;
2821 case QEMU_OPTION_fda:
2822 case QEMU_OPTION_fdb:
2823 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2824 optarg, FD_OPTS);
2825 break;
2826 case QEMU_OPTION_no_fd_bootchk:
2827 fd_bootchk = 0;
2828 break;
2829 case QEMU_OPTION_netdev:
2830 default_net = 0;
2831 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2832 exit(1);
2834 break;
2835 case QEMU_OPTION_nic:
2836 default_net = 0;
2837 if (net_client_parse(qemu_find_opts("nic"), optarg) == -1) {
2838 exit(1);
2840 break;
2841 case QEMU_OPTION_net:
2842 default_net = 0;
2843 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2844 exit(1);
2846 break;
2847 #ifdef CONFIG_LIBISCSI
2848 case QEMU_OPTION_iscsi:
2849 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
2850 optarg, false);
2851 if (!opts) {
2852 exit(1);
2854 break;
2855 #endif
2856 case QEMU_OPTION_audio_help:
2857 audio_legacy_help();
2858 exit (0);
2859 break;
2860 case QEMU_OPTION_audiodev:
2861 audio_parse_option(optarg);
2862 break;
2863 case QEMU_OPTION_soundhw:
2864 select_soundhw (optarg);
2865 break;
2866 case QEMU_OPTION_h:
2867 help(0);
2868 break;
2869 case QEMU_OPTION_version:
2870 version();
2871 exit(0);
2872 break;
2873 case QEMU_OPTION_m:
2874 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
2875 optarg, true);
2876 if (!opts) {
2877 exit(EXIT_FAILURE);
2879 break;
2880 #ifdef CONFIG_TPM
2881 case QEMU_OPTION_tpmdev:
2882 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
2883 exit(1);
2885 break;
2886 #endif
2887 case QEMU_OPTION_mempath:
2888 mem_path = optarg;
2889 break;
2890 case QEMU_OPTION_mem_prealloc:
2891 mem_prealloc = 1;
2892 break;
2893 case QEMU_OPTION_d:
2894 log_mask = optarg;
2895 break;
2896 case QEMU_OPTION_D:
2897 log_file = optarg;
2898 break;
2899 case QEMU_OPTION_DFILTER:
2900 qemu_set_dfilter_ranges(optarg, &error_fatal);
2901 break;
2902 case QEMU_OPTION_seed:
2903 qemu_guest_random_seed_main(optarg, &error_fatal);
2904 break;
2905 case QEMU_OPTION_s:
2906 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
2907 break;
2908 case QEMU_OPTION_gdb:
2909 add_device_config(DEV_GDB, optarg);
2910 break;
2911 case QEMU_OPTION_L:
2912 if (is_help_option(optarg)) {
2913 list_data_dirs = true;
2914 } else {
2915 qemu_add_data_dir(g_strdup(optarg));
2917 break;
2918 case QEMU_OPTION_bios:
2919 qemu_opts_set(qemu_find_opts("machine"), "firmware", optarg, &error_abort);
2920 break;
2921 case QEMU_OPTION_singlestep:
2922 singlestep = 1;
2923 break;
2924 case QEMU_OPTION_S:
2925 autostart = 0;
2926 break;
2927 case QEMU_OPTION_k:
2928 keyboard_layout = optarg;
2929 break;
2930 case QEMU_OPTION_vga:
2931 vga_model = optarg;
2932 default_vga = 0;
2933 break;
2934 case QEMU_OPTION_g:
2936 const char *p;
2937 int w, h, depth;
2938 p = optarg;
2939 w = strtol(p, (char **)&p, 10);
2940 if (w <= 0) {
2941 graphic_error:
2942 error_report("invalid resolution or depth");
2943 exit(1);
2945 if (*p != 'x')
2946 goto graphic_error;
2947 p++;
2948 h = strtol(p, (char **)&p, 10);
2949 if (h <= 0)
2950 goto graphic_error;
2951 if (*p == 'x') {
2952 p++;
2953 depth = strtol(p, (char **)&p, 10);
2954 if (depth != 1 && depth != 2 && depth != 4 &&
2955 depth != 8 && depth != 15 && depth != 16 &&
2956 depth != 24 && depth != 32)
2957 goto graphic_error;
2958 } else if (*p == '\0') {
2959 depth = graphic_depth;
2960 } else {
2961 goto graphic_error;
2964 graphic_width = w;
2965 graphic_height = h;
2966 graphic_depth = depth;
2968 break;
2969 case QEMU_OPTION_echr:
2971 char *r;
2972 term_escape_char = strtol(optarg, &r, 0);
2973 if (r == optarg)
2974 printf("Bad argument to echr\n");
2975 break;
2977 case QEMU_OPTION_monitor:
2978 default_monitor = 0;
2979 if (strncmp(optarg, "none", 4)) {
2980 monitor_parse(optarg, "readline", false);
2982 break;
2983 case QEMU_OPTION_qmp:
2984 monitor_parse(optarg, "control", false);
2985 default_monitor = 0;
2986 break;
2987 case QEMU_OPTION_qmp_pretty:
2988 monitor_parse(optarg, "control", true);
2989 default_monitor = 0;
2990 break;
2991 case QEMU_OPTION_mon:
2992 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
2993 true);
2994 if (!opts) {
2995 exit(1);
2997 default_monitor = 0;
2998 break;
2999 case QEMU_OPTION_chardev:
3000 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3001 optarg, true);
3002 if (!opts) {
3003 exit(1);
3005 break;
3006 case QEMU_OPTION_fsdev:
3007 olist = qemu_find_opts("fsdev");
3008 if (!olist) {
3009 error_report("fsdev support is disabled");
3010 exit(1);
3012 opts = qemu_opts_parse_noisily(olist, optarg, true);
3013 if (!opts) {
3014 exit(1);
3016 break;
3017 case QEMU_OPTION_virtfs: {
3018 QemuOpts *fsdev;
3019 QemuOpts *device;
3020 const char *writeout, *sock_fd, *socket, *path, *security_model,
3021 *multidevs;
3023 olist = qemu_find_opts("virtfs");
3024 if (!olist) {
3025 error_report("virtfs support is disabled");
3026 exit(1);
3028 opts = qemu_opts_parse_noisily(olist, optarg, true);
3029 if (!opts) {
3030 exit(1);
3033 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3034 qemu_opt_get(opts, "mount_tag") == NULL) {
3035 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3036 exit(1);
3038 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3039 qemu_opts_id(opts) ?:
3040 qemu_opt_get(opts, "mount_tag"),
3041 1, NULL);
3042 if (!fsdev) {
3043 error_report("duplicate or invalid fsdev id: %s",
3044 qemu_opt_get(opts, "mount_tag"));
3045 exit(1);
3048 writeout = qemu_opt_get(opts, "writeout");
3049 if (writeout) {
3050 #ifdef CONFIG_SYNC_FILE_RANGE
3051 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3052 #else
3053 error_report("writeout=immediate not supported "
3054 "on this platform");
3055 exit(1);
3056 #endif
3058 qemu_opt_set(fsdev, "fsdriver",
3059 qemu_opt_get(opts, "fsdriver"), &error_abort);
3060 path = qemu_opt_get(opts, "path");
3061 if (path) {
3062 qemu_opt_set(fsdev, "path", path, &error_abort);
3064 security_model = qemu_opt_get(opts, "security_model");
3065 if (security_model) {
3066 qemu_opt_set(fsdev, "security_model", security_model,
3067 &error_abort);
3069 socket = qemu_opt_get(opts, "socket");
3070 if (socket) {
3071 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3073 sock_fd = qemu_opt_get(opts, "sock_fd");
3074 if (sock_fd) {
3075 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3078 qemu_opt_set_bool(fsdev, "readonly",
3079 qemu_opt_get_bool(opts, "readonly", 0),
3080 &error_abort);
3081 multidevs = qemu_opt_get(opts, "multidevs");
3082 if (multidevs) {
3083 qemu_opt_set(fsdev, "multidevs", multidevs, &error_abort);
3085 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3086 &error_abort);
3087 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3088 qemu_opt_set(device, "fsdev",
3089 qemu_opts_id(fsdev), &error_abort);
3090 qemu_opt_set(device, "mount_tag",
3091 qemu_opt_get(opts, "mount_tag"), &error_abort);
3092 break;
3094 case QEMU_OPTION_serial:
3095 add_device_config(DEV_SERIAL, optarg);
3096 default_serial = 0;
3097 if (strncmp(optarg, "mon:", 4) == 0) {
3098 default_monitor = 0;
3100 break;
3101 case QEMU_OPTION_watchdog:
3102 if (watchdog) {
3103 error_report("only one watchdog option may be given");
3104 exit(1);
3106 watchdog = optarg;
3107 break;
3108 case QEMU_OPTION_action:
3109 olist = qemu_find_opts("action");
3110 if (!qemu_opts_parse_noisily(olist, optarg, false)) {
3111 exit(1);
3113 break;
3114 case QEMU_OPTION_watchdog_action:
3115 if (select_watchdog_action(optarg) == -1) {
3116 error_report("unknown -watchdog-action parameter");
3117 exit(1);
3119 break;
3120 case QEMU_OPTION_parallel:
3121 add_device_config(DEV_PARALLEL, optarg);
3122 default_parallel = 0;
3123 if (strncmp(optarg, "mon:", 4) == 0) {
3124 default_monitor = 0;
3126 break;
3127 case QEMU_OPTION_debugcon:
3128 add_device_config(DEV_DEBUGCON, optarg);
3129 break;
3130 case QEMU_OPTION_loadvm:
3131 loadvm = optarg;
3132 break;
3133 case QEMU_OPTION_full_screen:
3134 dpy.has_full_screen = true;
3135 dpy.full_screen = true;
3136 break;
3137 case QEMU_OPTION_alt_grab:
3138 alt_grab = 1;
3139 break;
3140 case QEMU_OPTION_ctrl_grab:
3141 ctrl_grab = 1;
3142 break;
3143 case QEMU_OPTION_no_quit:
3144 dpy.has_window_close = true;
3145 dpy.window_close = false;
3146 break;
3147 case QEMU_OPTION_sdl:
3148 #ifdef CONFIG_SDL
3149 dpy.type = DISPLAY_TYPE_SDL;
3150 break;
3151 #else
3152 error_report("SDL support is disabled");
3153 exit(1);
3154 #endif
3155 case QEMU_OPTION_pidfile:
3156 pid_file = optarg;
3157 break;
3158 case QEMU_OPTION_win2k_hack:
3159 win2k_install_hack = 1;
3160 break;
3161 case QEMU_OPTION_acpitable:
3162 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3163 optarg, true);
3164 if (!opts) {
3165 exit(1);
3167 acpi_table_add(opts, &error_fatal);
3168 break;
3169 case QEMU_OPTION_smbios:
3170 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3171 optarg, false);
3172 if (!opts) {
3173 exit(1);
3175 smbios_entry_add(opts, &error_fatal);
3176 break;
3177 case QEMU_OPTION_fwcfg:
3178 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3179 optarg, true);
3180 if (opts == NULL) {
3181 exit(1);
3183 break;
3184 case QEMU_OPTION_preconfig:
3185 preconfig_requested = true;
3186 break;
3187 case QEMU_OPTION_enable_kvm:
3188 olist = qemu_find_opts("machine");
3189 qemu_opts_parse_noisily(olist, "accel=kvm", false);
3190 break;
3191 case QEMU_OPTION_M:
3192 case QEMU_OPTION_machine:
3193 olist = qemu_find_opts("machine");
3194 opts = qemu_opts_parse_noisily(olist, optarg, true);
3195 if (!opts) {
3196 exit(1);
3198 break;
3199 case QEMU_OPTION_accel:
3200 accel_opts = qemu_opts_parse_noisily(qemu_find_opts("accel"),
3201 optarg, true);
3202 optarg = qemu_opt_get(accel_opts, "accel");
3203 if (!optarg || is_help_option(optarg)) {
3204 printf("Accelerators supported in QEMU binary:\n");
3205 GSList *el, *accel_list = object_class_get_list(TYPE_ACCEL,
3206 false);
3207 for (el = accel_list; el; el = el->next) {
3208 gchar *typename = g_strdup(object_class_get_name(
3209 OBJECT_CLASS(el->data)));
3210 /* omit qtest which is used for tests only */
3211 if (g_strcmp0(typename, ACCEL_CLASS_NAME("qtest")) &&
3212 g_str_has_suffix(typename, ACCEL_CLASS_SUFFIX)) {
3213 gchar **optname = g_strsplit(typename,
3214 ACCEL_CLASS_SUFFIX, 0);
3215 printf("%s\n", optname[0]);
3216 g_strfreev(optname);
3218 g_free(typename);
3220 g_slist_free(accel_list);
3221 exit(0);
3223 break;
3224 case QEMU_OPTION_usb:
3225 olist = qemu_find_opts("machine");
3226 qemu_opts_parse_noisily(olist, "usb=on", false);
3227 break;
3228 case QEMU_OPTION_usbdevice:
3229 olist = qemu_find_opts("machine");
3230 qemu_opts_parse_noisily(olist, "usb=on", false);
3231 add_device_config(DEV_USB, optarg);
3232 break;
3233 case QEMU_OPTION_device:
3234 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3235 optarg, true)) {
3236 exit(1);
3238 break;
3239 case QEMU_OPTION_smp:
3240 if (!qemu_opts_parse_noisily(qemu_find_opts("smp-opts"),
3241 optarg, true)) {
3242 exit(1);
3244 break;
3245 case QEMU_OPTION_vnc:
3246 vnc_parse(optarg);
3247 break;
3248 case QEMU_OPTION_no_acpi:
3249 olist = qemu_find_opts("machine");
3250 qemu_opts_parse_noisily(olist, "acpi=off", false);
3251 break;
3252 case QEMU_OPTION_no_hpet:
3253 olist = qemu_find_opts("machine");
3254 qemu_opts_parse_noisily(olist, "hpet=off", false);
3255 break;
3256 case QEMU_OPTION_no_reboot:
3257 olist = qemu_find_opts("action");
3258 qemu_opts_parse_noisily(olist, "reboot=shutdown", false);
3259 break;
3260 case QEMU_OPTION_no_shutdown:
3261 olist = qemu_find_opts("action");
3262 qemu_opts_parse_noisily(olist, "shutdown=pause", false);
3263 break;
3264 case QEMU_OPTION_uuid:
3265 if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
3266 error_report("failed to parse UUID string: wrong format");
3267 exit(1);
3269 qemu_uuid_set = true;
3270 break;
3271 case QEMU_OPTION_option_rom:
3272 if (nb_option_roms >= MAX_OPTION_ROMS) {
3273 error_report("too many option ROMs");
3274 exit(1);
3276 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3277 optarg, true);
3278 if (!opts) {
3279 exit(1);
3281 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3282 option_rom[nb_option_roms].bootindex =
3283 qemu_opt_get_number(opts, "bootindex", -1);
3284 if (!option_rom[nb_option_roms].name) {
3285 error_report("Option ROM file is not specified");
3286 exit(1);
3288 nb_option_roms++;
3289 break;
3290 case QEMU_OPTION_semihosting:
3291 qemu_semihosting_enable();
3292 break;
3293 case QEMU_OPTION_semihosting_config:
3294 if (qemu_semihosting_config_options(optarg) != 0) {
3295 exit(1);
3297 break;
3298 case QEMU_OPTION_name:
3299 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3300 optarg, true);
3301 if (!opts) {
3302 exit(1);
3304 /* Capture guest name if -msg guest-name is used later */
3305 error_guest_name = qemu_opt_get(opts, "guest");
3306 break;
3307 case QEMU_OPTION_prom_env:
3308 if (nb_prom_envs >= MAX_PROM_ENVS) {
3309 error_report("too many prom variables");
3310 exit(1);
3312 prom_envs[nb_prom_envs] = optarg;
3313 nb_prom_envs++;
3314 break;
3315 case QEMU_OPTION_old_param:
3316 old_param = 1;
3317 break;
3318 case QEMU_OPTION_rtc:
3319 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3320 false);
3321 if (!opts) {
3322 exit(1);
3324 break;
3325 case QEMU_OPTION_icount:
3326 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3327 optarg, true);
3328 if (!icount_opts) {
3329 exit(1);
3331 break;
3332 case QEMU_OPTION_incoming:
3333 if (!incoming) {
3334 runstate_set(RUN_STATE_INMIGRATE);
3336 incoming = optarg;
3337 break;
3338 case QEMU_OPTION_only_migratable:
3339 only_migratable = 1;
3340 break;
3341 case QEMU_OPTION_nodefaults:
3342 has_defaults = 0;
3343 break;
3344 case QEMU_OPTION_xen_domid:
3345 if (!(xen_available())) {
3346 error_report("Option not supported for this target");
3347 exit(1);
3349 xen_domid = atoi(optarg);
3350 break;
3351 case QEMU_OPTION_xen_attach:
3352 if (!(xen_available())) {
3353 error_report("Option not supported for this target");
3354 exit(1);
3356 xen_mode = XEN_ATTACH;
3357 break;
3358 case QEMU_OPTION_xen_domid_restrict:
3359 if (!(xen_available())) {
3360 error_report("Option not supported for this target");
3361 exit(1);
3363 xen_domid_restrict = true;
3364 break;
3365 case QEMU_OPTION_trace:
3366 trace_opt_parse(optarg);
3367 break;
3368 case QEMU_OPTION_plugin:
3369 qemu_plugin_opt_parse(optarg, &plugin_list);
3370 break;
3371 case QEMU_OPTION_readconfig:
3372 qemu_read_config_file(optarg, &error_fatal);
3373 break;
3374 case QEMU_OPTION_spice:
3375 olist = qemu_find_opts_err("spice", NULL);
3376 if (!olist) {
3377 ui_module_load_one("spice-core");
3378 olist = qemu_find_opts("spice");
3380 if (!olist) {
3381 error_report("spice support is disabled");
3382 exit(1);
3384 opts = qemu_opts_parse_noisily(olist, optarg, false);
3385 if (!opts) {
3386 exit(1);
3388 display_remote++;
3389 break;
3390 case QEMU_OPTION_writeconfig:
3392 FILE *fp;
3393 warn_report("-writeconfig is deprecated and will go away without a replacement");
3394 if (strcmp(optarg, "-") == 0) {
3395 fp = stdout;
3396 } else {
3397 fp = fopen(optarg, "w");
3398 if (fp == NULL) {
3399 error_report("open %s: %s", optarg,
3400 strerror(errno));
3401 exit(1);
3404 qemu_config_write(fp);
3405 if (fp != stdout) {
3406 fclose(fp);
3408 break;
3410 case QEMU_OPTION_qtest:
3411 qtest_chrdev = optarg;
3412 break;
3413 case QEMU_OPTION_qtest_log:
3414 qtest_log = optarg;
3415 break;
3416 case QEMU_OPTION_sandbox:
3417 olist = qemu_find_opts("sandbox");
3418 if (!olist) {
3419 #ifndef CONFIG_SECCOMP
3420 error_report("-sandbox support is not enabled "
3421 "in this QEMU binary");
3422 #endif
3423 exit(1);
3426 opts = qemu_opts_parse_noisily(olist, optarg, true);
3427 if (!opts) {
3428 exit(1);
3430 break;
3431 case QEMU_OPTION_add_fd:
3432 #ifndef _WIN32
3433 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
3434 optarg, false);
3435 if (!opts) {
3436 exit(1);
3438 #else
3439 error_report("File descriptor passing is disabled on this "
3440 "platform");
3441 exit(1);
3442 #endif
3443 break;
3444 case QEMU_OPTION_object:
3445 object_option_parse(optarg);
3446 break;
3447 case QEMU_OPTION_overcommit:
3448 opts = qemu_opts_parse_noisily(qemu_find_opts("overcommit"),
3449 optarg, false);
3450 if (!opts) {
3451 exit(1);
3453 enable_mlock = qemu_opt_get_bool(opts, "mem-lock", false);
3454 enable_cpu_pm = qemu_opt_get_bool(opts, "cpu-pm", false);
3455 break;
3456 case QEMU_OPTION_compat:
3458 CompatPolicy *opts;
3459 Visitor *v;
3461 v = qobject_input_visitor_new_str(optarg, NULL,
3462 &error_fatal);
3464 visit_type_CompatPolicy(v, NULL, &opts, &error_fatal);
3465 QAPI_CLONE_MEMBERS(CompatPolicy, &compat_policy, opts);
3467 qapi_free_CompatPolicy(opts);
3468 visit_free(v);
3469 break;
3471 case QEMU_OPTION_msg:
3472 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
3473 false);
3474 if (!opts) {
3475 exit(1);
3477 configure_msg(opts);
3478 break;
3479 case QEMU_OPTION_dump_vmstate:
3480 if (vmstate_dump_file) {
3481 error_report("only one '-dump-vmstate' "
3482 "option may be given");
3483 exit(1);
3485 vmstate_dump_file = fopen(optarg, "w");
3486 if (vmstate_dump_file == NULL) {
3487 error_report("open %s: %s", optarg, strerror(errno));
3488 exit(1);
3490 break;
3491 case QEMU_OPTION_enable_sync_profile:
3492 qsp_enable();
3493 break;
3494 case QEMU_OPTION_nouserconfig:
3495 /* Nothing to be parsed here. Especially, do not error out below. */
3496 break;
3497 default:
3498 if (os_parse_cmd_args(popt->index, optarg)) {
3499 error_report("Option not supported in this build");
3500 exit(1);
3506 * Clear error location left behind by the loop.
3507 * Best done right after the loop. Do not insert code here!
3509 loc_set_none();
3511 qemu_validate_options();
3512 qemu_process_sugar_options();
3515 * These options affect everything else and should be processed
3516 * before daemonizing.
3518 qemu_process_early_options();
3520 qemu_process_help_options();
3521 qemu_maybe_daemonize(pid_file);
3524 * The trace backend must be initialized after daemonizing.
3525 * trace_init_backends() will call st_init(), which will create the
3526 * trace thread in the parent, and also register st_flush_trace_buffer()
3527 * in atexit(). This function will force the parent to wait for the
3528 * writeout thread to finish, which will not occur, and the parent
3529 * process will be left in the host.
3531 if (!trace_init_backends()) {
3532 exit(1);
3534 trace_init_file();
3536 qemu_init_main_loop(&error_fatal);
3537 cpu_timers_init();
3539 user_register_global_props();
3540 replay_configure(icount_opts);
3542 configure_rtc(qemu_find_opts_singleton("rtc"));
3544 qemu_create_machine(select_machine());
3546 suspend_mux_open();
3548 qemu_disable_default_devices();
3549 qemu_create_default_devices();
3550 qemu_create_early_backends();
3552 qemu_apply_machine_options();
3553 phase_advance(PHASE_MACHINE_CREATED);
3556 * Note: uses machine properties such as kernel-irqchip, must run
3557 * after machine_set_property().
3559 configure_accelerators(argv[0]);
3560 phase_advance(PHASE_ACCEL_CREATED);
3563 * Beware, QOM objects created before this point miss global and
3564 * compat properties.
3566 * Global properties get set up by qdev_prop_register_global(),
3567 * called from user_register_global_props(), and certain option
3568 * desugaring. Also in CPU feature desugaring (buried in
3569 * parse_cpu_option()), which happens below this point, but may
3570 * only target the CPU type, which can only be created after
3571 * parse_cpu_option() returned the type.
3573 * Machine compat properties: object_set_machine_compat_props().
3574 * Accelerator compat props: object_set_accelerator_compat_props(),
3575 * called from do_configure_accelerator().
3578 machine_class = MACHINE_GET_CLASS(current_machine);
3579 if (!qtest_enabled() && machine_class->deprecation_reason) {
3580 error_report("Machine type '%s' is deprecated: %s",
3581 machine_class->name, machine_class->deprecation_reason);
3585 * Note: creates a QOM object, must run only after global and
3586 * compat properties have been set up.
3588 migration_object_init();
3590 qemu_create_late_backends();
3592 /* parse features once if machine provides default cpu_type */
3593 current_machine->cpu_type = machine_class->default_cpu_type;
3594 if (cpu_option) {
3595 current_machine->cpu_type = parse_cpu_option(cpu_option);
3597 /* NB: for machine none cpu_type could STILL be NULL here! */
3599 qemu_resolve_machine_memdev();
3600 parse_numa_opts(current_machine);
3602 if (vmstate_dump_file) {
3603 /* dump and exit */
3604 dump_vmstate_json_to_file(vmstate_dump_file);
3605 exit(0);
3608 if (!preconfig_requested) {
3609 qmp_x_exit_preconfig(&error_fatal);
3611 qemu_init_displays();
3612 accel_setup_post(current_machine);
3613 os_setup_post();
3614 resume_mux_open();