qemuutil: remove qemu_set_fd_handler duplicate symbol
[qemu/ar7.git] / softmmu / vl.c
blobcba7ab34417cd2d9aa3856a4fba78261377eef30
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/boards.h"
31 #include "hw/qdev-properties.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-ui.h"
118 #include "qapi/qapi-commands-block-core.h"
119 #include "qapi/qapi-commands-migration.h"
120 #include "qapi/qapi-commands-misc.h"
121 #include "qapi/qapi-visit-qom.h"
122 #include "qapi/qapi-commands-ui.h"
123 #include "qapi/qmp/qerror.h"
124 #include "sysemu/iothread.h"
125 #include "qemu/guest-random.h"
127 #define MAX_VIRTIO_CONSOLES 1
129 typedef struct BlockdevOptionsQueueEntry {
130 BlockdevOptions *bdo;
131 Location loc;
132 QSIMPLEQ_ENTRY(BlockdevOptionsQueueEntry) entry;
133 } BlockdevOptionsQueueEntry;
135 typedef QSIMPLEQ_HEAD(, BlockdevOptionsQueueEntry) BlockdevOptionsQueue;
137 typedef struct ObjectOption {
138 ObjectOptions *opts;
139 QTAILQ_ENTRY(ObjectOption) next;
140 } ObjectOption;
142 static const char *cpu_option;
143 static const char *mem_path;
144 static const char *incoming;
145 static const char *loadvm;
146 static QTAILQ_HEAD(, ObjectOption) object_opts = QTAILQ_HEAD_INITIALIZER(object_opts);
147 static ram_addr_t maxram_size;
148 static uint64_t ram_slots;
149 static int display_remote;
150 static int snapshot;
151 static bool preconfig_requested;
152 static QemuPluginList plugin_list = QTAILQ_HEAD_INITIALIZER(plugin_list);
153 static BlockdevOptionsQueue bdo_queue = QSIMPLEQ_HEAD_INITIALIZER(bdo_queue);
154 static bool nographic = false;
155 static int mem_prealloc; /* force preallocation of physical target memory */
156 static ram_addr_t ram_size;
157 static const char *vga_model = NULL;
158 static DisplayOptions dpy;
159 static int num_serial_hds;
160 static Chardev **serial_hds;
161 static const char *log_mask;
162 static const char *log_file;
163 static bool list_data_dirs;
164 static const char *watchdog;
165 static const char *qtest_chrdev;
166 static const char *qtest_log;
168 static int has_defaults = 1;
169 static int default_serial = 1;
170 static int default_parallel = 1;
171 static int default_monitor = 1;
172 static int default_floppy = 1;
173 static int default_cdrom = 1;
174 static int default_sdcard = 1;
175 static int default_vga = 1;
176 static int default_net = 1;
178 static struct {
179 const char *driver;
180 int *flag;
181 } default_list[] = {
182 { .driver = "isa-serial", .flag = &default_serial },
183 { .driver = "isa-parallel", .flag = &default_parallel },
184 { .driver = "isa-fdc", .flag = &default_floppy },
185 { .driver = "floppy", .flag = &default_floppy },
186 { .driver = "ide-cd", .flag = &default_cdrom },
187 { .driver = "ide-hd", .flag = &default_cdrom },
188 { .driver = "scsi-cd", .flag = &default_cdrom },
189 { .driver = "scsi-hd", .flag = &default_cdrom },
190 { .driver = "VGA", .flag = &default_vga },
191 { .driver = "isa-vga", .flag = &default_vga },
192 { .driver = "cirrus-vga", .flag = &default_vga },
193 { .driver = "isa-cirrus-vga", .flag = &default_vga },
194 { .driver = "vmware-svga", .flag = &default_vga },
195 { .driver = "qxl-vga", .flag = &default_vga },
196 { .driver = "virtio-vga", .flag = &default_vga },
197 { .driver = "ati-vga", .flag = &default_vga },
198 { .driver = "vhost-user-vga", .flag = &default_vga },
201 static QemuOptsList qemu_rtc_opts = {
202 .name = "rtc",
203 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
204 .merge_lists = true,
205 .desc = {
207 .name = "base",
208 .type = QEMU_OPT_STRING,
210 .name = "clock",
211 .type = QEMU_OPT_STRING,
213 .name = "driftfix",
214 .type = QEMU_OPT_STRING,
216 { /* end of list */ }
220 static QemuOptsList qemu_option_rom_opts = {
221 .name = "option-rom",
222 .implied_opt_name = "romfile",
223 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
224 .desc = {
226 .name = "bootindex",
227 .type = QEMU_OPT_NUMBER,
228 }, {
229 .name = "romfile",
230 .type = QEMU_OPT_STRING,
232 { /* end of list */ }
236 static QemuOptsList qemu_machine_opts = {
237 .name = "machine",
238 .implied_opt_name = "type",
239 .merge_lists = true,
240 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
241 .desc = {
243 * no elements => accept any
244 * sanity checking will happen later
245 * when setting machine properties
251 static QemuOptsList qemu_accel_opts = {
252 .name = "accel",
253 .implied_opt_name = "accel",
254 .head = QTAILQ_HEAD_INITIALIZER(qemu_accel_opts.head),
255 .desc = {
257 * no elements => accept any
258 * sanity checking will happen later
259 * when setting accelerator properties
265 static QemuOptsList qemu_boot_opts = {
266 .name = "boot-opts",
267 .implied_opt_name = "order",
268 .merge_lists = true,
269 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
270 .desc = {
272 .name = "order",
273 .type = QEMU_OPT_STRING,
274 }, {
275 .name = "once",
276 .type = QEMU_OPT_STRING,
277 }, {
278 .name = "menu",
279 .type = QEMU_OPT_BOOL,
280 }, {
281 .name = "splash",
282 .type = QEMU_OPT_STRING,
283 }, {
284 .name = "splash-time",
285 .type = QEMU_OPT_NUMBER,
286 }, {
287 .name = "reboot-timeout",
288 .type = QEMU_OPT_NUMBER,
289 }, {
290 .name = "strict",
291 .type = QEMU_OPT_BOOL,
293 { /*End of list */ }
297 static QemuOptsList qemu_add_fd_opts = {
298 .name = "add-fd",
299 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
300 .desc = {
302 .name = "fd",
303 .type = QEMU_OPT_NUMBER,
304 .help = "file descriptor of which a duplicate is added to fd set",
306 .name = "set",
307 .type = QEMU_OPT_NUMBER,
308 .help = "ID of the fd set to add fd to",
310 .name = "opaque",
311 .type = QEMU_OPT_STRING,
312 .help = "free-form string used to describe fd",
314 { /* end of list */ }
318 static QemuOptsList qemu_object_opts = {
319 .name = "object",
320 .implied_opt_name = "qom-type",
321 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
322 .desc = {
327 static QemuOptsList qemu_tpmdev_opts = {
328 .name = "tpmdev",
329 .implied_opt_name = "type",
330 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
331 .desc = {
332 /* options are defined in the TPM backends */
333 { /* end of list */ }
337 static QemuOptsList qemu_overcommit_opts = {
338 .name = "overcommit",
339 .head = QTAILQ_HEAD_INITIALIZER(qemu_overcommit_opts.head),
340 .desc = {
342 .name = "mem-lock",
343 .type = QEMU_OPT_BOOL,
346 .name = "cpu-pm",
347 .type = QEMU_OPT_BOOL,
349 { /* end of list */ }
353 static QemuOptsList qemu_msg_opts = {
354 .name = "msg",
355 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
356 .desc = {
358 .name = "timestamp",
359 .type = QEMU_OPT_BOOL,
362 .name = "guest-name",
363 .type = QEMU_OPT_BOOL,
364 .help = "Prepends guest name for error messages but only if "
365 "-name guest is set otherwise option is ignored\n",
367 { /* end of list */ }
371 static QemuOptsList qemu_name_opts = {
372 .name = "name",
373 .implied_opt_name = "guest",
374 .merge_lists = true,
375 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
376 .desc = {
378 .name = "guest",
379 .type = QEMU_OPT_STRING,
380 .help = "Sets the name of the guest.\n"
381 "This name will be displayed in the SDL window caption.\n"
382 "The name will also be used for the VNC server",
383 }, {
384 .name = "process",
385 .type = QEMU_OPT_STRING,
386 .help = "Sets the name of the QEMU process, as shown in top etc",
387 }, {
388 .name = "debug-threads",
389 .type = QEMU_OPT_BOOL,
390 .help = "When enabled, name the individual threads; defaults off.\n"
391 "NOTE: The thread names are for debugging and not a\n"
392 "stable API.",
394 { /* End of list */ }
398 static QemuOptsList qemu_mem_opts = {
399 .name = "memory",
400 .implied_opt_name = "size",
401 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
402 .merge_lists = true,
403 .desc = {
405 .name = "size",
406 .type = QEMU_OPT_SIZE,
409 .name = "slots",
410 .type = QEMU_OPT_NUMBER,
413 .name = "maxmem",
414 .type = QEMU_OPT_SIZE,
416 { /* end of list */ }
420 static QemuOptsList qemu_icount_opts = {
421 .name = "icount",
422 .implied_opt_name = "shift",
423 .merge_lists = true,
424 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
425 .desc = {
427 .name = "shift",
428 .type = QEMU_OPT_STRING,
429 }, {
430 .name = "align",
431 .type = QEMU_OPT_BOOL,
432 }, {
433 .name = "sleep",
434 .type = QEMU_OPT_BOOL,
435 }, {
436 .name = "rr",
437 .type = QEMU_OPT_STRING,
438 }, {
439 .name = "rrfile",
440 .type = QEMU_OPT_STRING,
441 }, {
442 .name = "rrsnapshot",
443 .type = QEMU_OPT_STRING,
445 { /* end of list */ }
449 static QemuOptsList qemu_fw_cfg_opts = {
450 .name = "fw_cfg",
451 .implied_opt_name = "name",
452 .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
453 .desc = {
455 .name = "name",
456 .type = QEMU_OPT_STRING,
457 .help = "Sets the fw_cfg name of the blob to be inserted",
458 }, {
459 .name = "file",
460 .type = QEMU_OPT_STRING,
461 .help = "Sets the name of the file from which "
462 "the fw_cfg blob will be loaded",
463 }, {
464 .name = "string",
465 .type = QEMU_OPT_STRING,
466 .help = "Sets content of the blob to be inserted from a string",
467 }, {
468 .name = "gen_id",
469 .type = QEMU_OPT_STRING,
470 .help = "Sets id of the object generating the fw_cfg blob "
471 "to be inserted",
473 { /* end of list */ }
477 static QemuOptsList qemu_action_opts = {
478 .name = "action",
479 .merge_lists = true,
480 .head = QTAILQ_HEAD_INITIALIZER(qemu_action_opts.head),
481 .desc = {
483 .name = "shutdown",
484 .type = QEMU_OPT_STRING,
486 .name = "reboot",
487 .type = QEMU_OPT_STRING,
489 .name = "panic",
490 .type = QEMU_OPT_STRING,
492 .name = "watchdog",
493 .type = QEMU_OPT_STRING,
495 { /* end of list */ }
500 * Get machine options
502 * Returns: machine options (never null).
504 static QemuOpts *qemu_get_machine_opts(void)
506 return qemu_find_opts_singleton("machine");
509 const char *qemu_get_vm_name(void)
511 return qemu_name;
514 static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
516 const char *driver = qemu_opt_get(opts, "driver");
517 int i;
519 if (!driver)
520 return 0;
521 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
522 if (strcmp(default_list[i].driver, driver) != 0)
523 continue;
524 *(default_list[i].flag) = 0;
526 return 0;
529 static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
531 const char *proc_name;
533 if (qemu_opt_get(opts, "debug-threads")) {
534 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
536 qemu_name = qemu_opt_get(opts, "guest");
538 proc_name = qemu_opt_get(opts, "process");
539 if (proc_name) {
540 os_set_proc_name(proc_name);
543 return 0;
546 bool defaults_enabled(void)
548 return has_defaults;
551 #ifndef _WIN32
552 static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
554 int fd, dupfd, flags;
555 int64_t fdset_id;
556 const char *fd_opaque = NULL;
557 AddfdInfo *fdinfo;
559 fd = qemu_opt_get_number(opts, "fd", -1);
560 fdset_id = qemu_opt_get_number(opts, "set", -1);
561 fd_opaque = qemu_opt_get(opts, "opaque");
563 if (fd < 0) {
564 error_setg(errp, "fd option is required and must be non-negative");
565 return -1;
568 if (fd <= STDERR_FILENO) {
569 error_setg(errp, "fd cannot be a standard I/O stream");
570 return -1;
574 * All fds inherited across exec() necessarily have FD_CLOEXEC
575 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
577 flags = fcntl(fd, F_GETFD);
578 if (flags == -1 || (flags & FD_CLOEXEC)) {
579 error_setg(errp, "fd is not valid or already in use");
580 return -1;
583 if (fdset_id < 0) {
584 error_setg(errp, "set option is required and must be non-negative");
585 return -1;
588 #ifdef F_DUPFD_CLOEXEC
589 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
590 #else
591 dupfd = dup(fd);
592 if (dupfd != -1) {
593 qemu_set_cloexec(dupfd);
595 #endif
596 if (dupfd == -1) {
597 error_setg(errp, "error duplicating fd: %s", strerror(errno));
598 return -1;
601 /* add the duplicate fd, and optionally the opaque string, to the fd set */
602 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
603 &error_abort);
604 g_free(fdinfo);
606 return 0;
609 static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
611 int fd;
613 fd = qemu_opt_get_number(opts, "fd", -1);
614 close(fd);
616 return 0;
618 #endif
620 /***********************************************************/
621 /* QEMU Block devices */
623 #define HD_OPTS "media=disk"
624 #define CDROM_OPTS "media=cdrom"
625 #define FD_OPTS ""
626 #define PFLASH_OPTS ""
627 #define MTD_OPTS ""
628 #define SD_OPTS ""
630 static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
632 BlockInterfaceType *block_default_type = opaque;
634 return drive_new(opts, *block_default_type, errp) == NULL;
637 static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
639 if (qemu_opt_get(opts, "snapshot") == NULL) {
640 qemu_opt_set(opts, "snapshot", "on", &error_abort);
642 return 0;
645 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
646 int index, const char *optstr)
648 QemuOpts *opts;
649 DriveInfo *dinfo;
651 if (!enable || drive_get_by_index(type, index)) {
652 return;
655 opts = drive_add(type, index, NULL, optstr);
656 if (snapshot) {
657 drive_enable_snapshot(NULL, opts, NULL);
660 dinfo = drive_new(opts, type, &error_abort);
661 dinfo->is_default = true;
665 static void configure_blockdev(BlockdevOptionsQueue *bdo_queue,
666 MachineClass *machine_class, int snapshot)
669 * If the currently selected machine wishes to override the
670 * units-per-bus property of its default HBA interface type, do so
671 * now.
673 if (machine_class->units_per_default_bus) {
674 override_max_devs(machine_class->block_default_type,
675 machine_class->units_per_default_bus);
678 /* open the virtual block devices */
679 while (!QSIMPLEQ_EMPTY(bdo_queue)) {
680 BlockdevOptionsQueueEntry *bdo = QSIMPLEQ_FIRST(bdo_queue);
682 QSIMPLEQ_REMOVE_HEAD(bdo_queue, entry);
683 loc_push_restore(&bdo->loc);
684 qmp_blockdev_add(bdo->bdo, &error_fatal);
685 loc_pop(&bdo->loc);
686 qapi_free_BlockdevOptions(bdo->bdo);
687 g_free(bdo);
689 if (snapshot) {
690 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
691 NULL, NULL);
693 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
694 &machine_class->block_default_type, &error_fatal)) {
695 /* We printed help */
696 exit(0);
699 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
700 CDROM_OPTS);
701 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
702 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
706 static QemuOptsList qemu_smp_opts = {
707 .name = "smp-opts",
708 .implied_opt_name = "cpus",
709 .merge_lists = true,
710 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
711 .desc = {
713 .name = "cpus",
714 .type = QEMU_OPT_NUMBER,
715 }, {
716 .name = "sockets",
717 .type = QEMU_OPT_NUMBER,
718 }, {
719 .name = "dies",
720 .type = QEMU_OPT_NUMBER,
721 }, {
722 .name = "cores",
723 .type = QEMU_OPT_NUMBER,
724 }, {
725 .name = "threads",
726 .type = QEMU_OPT_NUMBER,
727 }, {
728 .name = "maxcpus",
729 .type = QEMU_OPT_NUMBER,
731 { /*End of list */ }
735 static void realtime_init(void)
737 if (enable_mlock) {
738 if (os_mlock() < 0) {
739 error_report("locking memory failed");
740 exit(1);
746 static void configure_msg(QemuOpts *opts)
748 message_with_timestamp = qemu_opt_get_bool(opts, "timestamp", false);
749 error_with_guestname = qemu_opt_get_bool(opts, "guest-name", false);
753 /***********************************************************/
754 /* USB devices */
756 static int usb_device_add(const char *devname)
758 USBDevice *dev = NULL;
760 if (!machine_usb(current_machine)) {
761 return -1;
764 dev = usbdevice_create(devname);
765 if (!dev)
766 return -1;
768 return 0;
771 static int usb_parse(const char *cmdline)
773 int r;
774 r = usb_device_add(cmdline);
775 if (r < 0) {
776 error_report("could not add USB device '%s'", cmdline);
778 return r;
781 /***********************************************************/
782 /* machine registration */
784 static MachineClass *find_machine(const char *name, GSList *machines)
786 GSList *el;
788 for (el = machines; el; el = el->next) {
789 MachineClass *mc = el->data;
791 if (!strcmp(mc->name, name) || !g_strcmp0(mc->alias, name)) {
792 return mc;
796 return NULL;
799 static MachineClass *find_default_machine(GSList *machines)
801 GSList *el;
802 MachineClass *default_machineclass = NULL;
804 for (el = machines; el; el = el->next) {
805 MachineClass *mc = el->data;
807 if (mc->is_default) {
808 assert(default_machineclass == NULL && "Multiple default machines");
809 default_machineclass = mc;
813 return default_machineclass;
816 static int machine_help_func(QemuOpts *opts, MachineState *machine)
818 ObjectProperty *prop;
819 ObjectPropertyIterator iter;
821 if (!qemu_opt_has_help_opt(opts)) {
822 return 0;
825 object_property_iter_init(&iter, OBJECT(machine));
826 while ((prop = object_property_iter_next(&iter))) {
827 if (!prop->set) {
828 continue;
831 printf("%s.%s=%s", MACHINE_GET_CLASS(machine)->name,
832 prop->name, prop->type);
833 if (prop->description) {
834 printf(" (%s)\n", prop->description);
835 } else {
836 printf("\n");
840 return 1;
843 static void version(void)
845 printf("QEMU emulator version " QEMU_FULL_VERSION "\n"
846 QEMU_COPYRIGHT "\n");
849 static void help(int exitcode)
851 version();
852 printf("usage: %s [options] [disk_image]\n\n"
853 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
854 error_get_progname());
856 #define QEMU_OPTIONS_GENERATE_HELP
857 #include "qemu-options-wrapper.h"
859 printf("\nDuring emulation, the following keys are useful:\n"
860 "ctrl-alt-f toggle full screen\n"
861 "ctrl-alt-n switch to virtual console 'n'\n"
862 "ctrl-alt toggle mouse and keyboard grab\n"
863 "\n"
864 "When using -nographic, press 'ctrl-a h' to get some help.\n"
865 "\n"
866 QEMU_HELP_BOTTOM "\n");
868 exit(exitcode);
871 #define HAS_ARG 0x0001
873 typedef struct QEMUOption {
874 const char *name;
875 int flags;
876 int index;
877 uint32_t arch_mask;
878 } QEMUOption;
880 static const QEMUOption qemu_options[] = {
881 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
882 #define QEMU_OPTIONS_GENERATE_OPTIONS
883 #include "qemu-options-wrapper.h"
884 { NULL },
887 typedef struct VGAInterfaceInfo {
888 const char *opt_name; /* option name */
889 const char *name; /* human-readable name */
890 /* Class names indicating that support is available.
891 * If no class is specified, the interface is always available */
892 const char *class_names[2];
893 } VGAInterfaceInfo;
895 static const VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
896 [VGA_NONE] = {
897 .opt_name = "none",
898 .name = "no graphic card",
900 [VGA_STD] = {
901 .opt_name = "std",
902 .name = "standard VGA",
903 .class_names = { "VGA", "isa-vga" },
905 [VGA_CIRRUS] = {
906 .opt_name = "cirrus",
907 .name = "Cirrus VGA",
908 .class_names = { "cirrus-vga", "isa-cirrus-vga" },
910 [VGA_VMWARE] = {
911 .opt_name = "vmware",
912 .name = "VMWare SVGA",
913 .class_names = { "vmware-svga" },
915 [VGA_VIRTIO] = {
916 .opt_name = "virtio",
917 .name = "Virtio VGA",
918 .class_names = { "virtio-vga" },
920 [VGA_QXL] = {
921 .opt_name = "qxl",
922 .name = "QXL VGA",
923 .class_names = { "qxl-vga" },
925 [VGA_TCX] = {
926 .opt_name = "tcx",
927 .name = "TCX framebuffer",
928 .class_names = { "SUNW,tcx" },
930 [VGA_CG3] = {
931 .opt_name = "cg3",
932 .name = "CG3 framebuffer",
933 .class_names = { "cgthree" },
935 [VGA_XENFB] = {
936 .opt_name = "xenfb",
937 .name = "Xen paravirtualized framebuffer",
941 static bool vga_interface_available(VGAInterfaceType t)
943 const VGAInterfaceInfo *ti = &vga_interfaces[t];
945 assert(t < VGA_TYPE_MAX);
946 return !ti->class_names[0] ||
947 module_object_class_by_name(ti->class_names[0]) ||
948 module_object_class_by_name(ti->class_names[1]);
951 static const char *
952 get_default_vga_model(const MachineClass *machine_class)
954 if (machine_class->default_display) {
955 return machine_class->default_display;
956 } else if (vga_interface_available(VGA_CIRRUS)) {
957 return "cirrus";
958 } else if (vga_interface_available(VGA_STD)) {
959 return "std";
962 return NULL;
965 static void select_vgahw(const MachineClass *machine_class, const char *p)
967 const char *opts;
968 int t;
970 if (g_str_equal(p, "help")) {
971 const char *def = get_default_vga_model(machine_class);
973 for (t = 0; t < VGA_TYPE_MAX; t++) {
974 const VGAInterfaceInfo *ti = &vga_interfaces[t];
976 if (vga_interface_available(t) && ti->opt_name) {
977 printf("%-20s %s%s\n", ti->opt_name, ti->name ?: "",
978 g_str_equal(ti->opt_name, def) ? " (default)" : "");
981 exit(0);
984 assert(vga_interface_type == VGA_NONE);
985 for (t = 0; t < VGA_TYPE_MAX; t++) {
986 const VGAInterfaceInfo *ti = &vga_interfaces[t];
987 if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
988 if (!vga_interface_available(t)) {
989 error_report("%s not available", ti->name);
990 exit(1);
992 vga_interface_type = t;
993 break;
996 if (t == VGA_TYPE_MAX) {
997 invalid_vga:
998 error_report("unknown vga type: %s", p);
999 exit(1);
1001 while (*opts) {
1002 const char *nextopt;
1004 if (strstart(opts, ",retrace=", &nextopt)) {
1005 opts = nextopt;
1006 if (strstart(opts, "dumb", &nextopt))
1007 vga_retrace_method = VGA_RETRACE_DUMB;
1008 else if (strstart(opts, "precise", &nextopt))
1009 vga_retrace_method = VGA_RETRACE_PRECISE;
1010 else goto invalid_vga;
1011 } else goto invalid_vga;
1012 opts = nextopt;
1016 static void parse_display_qapi(const char *optarg)
1018 DisplayOptions *opts;
1019 Visitor *v;
1021 v = qobject_input_visitor_new_str(optarg, "type", &error_fatal);
1023 visit_type_DisplayOptions(v, NULL, &opts, &error_fatal);
1024 QAPI_CLONE_MEMBERS(DisplayOptions, &dpy, opts);
1026 qapi_free_DisplayOptions(opts);
1027 visit_free(v);
1030 DisplayOptions *qmp_query_display_options(Error **errp)
1032 return QAPI_CLONE(DisplayOptions, &dpy);
1035 static void parse_display(const char *p)
1037 const char *opts;
1039 if (is_help_option(p)) {
1040 qemu_display_help();
1041 exit(0);
1044 if (strstart(p, "sdl", &opts)) {
1046 * sdl DisplayType needs hand-crafted parser instead of
1047 * parse_display_qapi() due to some options not in
1048 * DisplayOptions, specifically:
1049 * - frame
1050 * Already deprecated.
1051 * - ctrl_grab + alt_grab
1052 * Not clear yet what happens to them long-term. Should
1053 * replaced by something better or deprecated and dropped.
1055 dpy.type = DISPLAY_TYPE_SDL;
1056 while (*opts) {
1057 const char *nextopt;
1059 if (strstart(opts, ",alt_grab=", &nextopt)) {
1060 opts = nextopt;
1061 if (strstart(opts, "on", &nextopt)) {
1062 alt_grab = 1;
1063 } else if (strstart(opts, "off", &nextopt)) {
1064 alt_grab = 0;
1065 } else {
1066 goto invalid_sdl_args;
1068 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1069 opts = nextopt;
1070 if (strstart(opts, "on", &nextopt)) {
1071 ctrl_grab = 1;
1072 } else if (strstart(opts, "off", &nextopt)) {
1073 ctrl_grab = 0;
1074 } else {
1075 goto invalid_sdl_args;
1077 } else if (strstart(opts, ",window_close=", &nextopt)) {
1078 opts = nextopt;
1079 dpy.has_window_close = true;
1080 if (strstart(opts, "on", &nextopt)) {
1081 dpy.window_close = true;
1082 } else if (strstart(opts, "off", &nextopt)) {
1083 dpy.window_close = false;
1084 } else {
1085 goto invalid_sdl_args;
1087 } else if (strstart(opts, ",show-cursor=", &nextopt)) {
1088 opts = nextopt;
1089 dpy.has_show_cursor = true;
1090 if (strstart(opts, "on", &nextopt)) {
1091 dpy.show_cursor = true;
1092 } else if (strstart(opts, "off", &nextopt)) {
1093 dpy.show_cursor = false;
1094 } else {
1095 goto invalid_sdl_args;
1097 } else if (strstart(opts, ",gl=", &nextopt)) {
1098 opts = nextopt;
1099 dpy.has_gl = true;
1100 if (strstart(opts, "on", &nextopt)) {
1101 dpy.gl = DISPLAYGL_MODE_ON;
1102 } else if (strstart(opts, "core", &nextopt)) {
1103 dpy.gl = DISPLAYGL_MODE_CORE;
1104 } else if (strstart(opts, "es", &nextopt)) {
1105 dpy.gl = DISPLAYGL_MODE_ES;
1106 } else if (strstart(opts, "off", &nextopt)) {
1107 dpy.gl = DISPLAYGL_MODE_OFF;
1108 } else {
1109 goto invalid_sdl_args;
1111 } else {
1112 invalid_sdl_args:
1113 error_report("invalid SDL option string");
1114 exit(1);
1116 opts = nextopt;
1118 } else if (strstart(p, "vnc", &opts)) {
1120 * vnc isn't a (local) DisplayType but a protocol for remote
1121 * display access.
1123 if (*opts == '=') {
1124 vnc_parse(opts + 1);
1125 } else {
1126 error_report("VNC requires a display argument vnc=<display>");
1127 exit(1);
1129 } else {
1130 parse_display_qapi(p);
1134 static inline bool nonempty_str(const char *str)
1136 return str && *str;
1139 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
1141 gchar *buf;
1142 size_t size;
1143 const char *name, *file, *str, *gen_id;
1144 FWCfgState *fw_cfg = (FWCfgState *) opaque;
1146 if (fw_cfg == NULL) {
1147 error_setg(errp, "fw_cfg device not available");
1148 return -1;
1150 name = qemu_opt_get(opts, "name");
1151 file = qemu_opt_get(opts, "file");
1152 str = qemu_opt_get(opts, "string");
1153 gen_id = qemu_opt_get(opts, "gen_id");
1155 /* we need the name, and exactly one of: file, content string, gen_id */
1156 if (!nonempty_str(name) ||
1157 nonempty_str(file) + nonempty_str(str) + nonempty_str(gen_id) != 1) {
1158 error_setg(errp, "name, plus exactly one of file,"
1159 " string and gen_id, are needed");
1160 return -1;
1162 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
1163 error_setg(errp, "name too long (max. %d char)",
1164 FW_CFG_MAX_FILE_PATH - 1);
1165 return -1;
1167 if (nonempty_str(gen_id)) {
1169 * In this particular case where the content is populated
1170 * internally, the "etc/" namespace protection is relaxed,
1171 * so do not emit a warning.
1173 } else if (strncmp(name, "opt/", 4) != 0) {
1174 warn_report("externally provided fw_cfg item names "
1175 "should be prefixed with \"opt/\"");
1177 if (nonempty_str(str)) {
1178 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
1179 buf = g_memdup(str, size);
1180 } else if (nonempty_str(gen_id)) {
1181 if (!fw_cfg_add_from_generator(fw_cfg, name, gen_id, errp)) {
1182 return -1;
1184 return 0;
1185 } else {
1186 GError *err = NULL;
1187 if (!g_file_get_contents(file, &buf, &size, &err)) {
1188 error_setg(errp, "can't load %s: %s", file, err->message);
1189 g_error_free(err);
1190 return -1;
1193 /* For legacy, keep user files in a specific global order. */
1194 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
1195 fw_cfg_add_file(fw_cfg, name, buf, size);
1196 fw_cfg_reset_order_override(fw_cfg);
1197 return 0;
1200 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
1202 return qdev_device_help(opts);
1205 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
1207 DeviceState *dev;
1209 dev = qdev_device_add(opts, errp);
1210 if (!dev && *errp) {
1211 error_report_err(*errp);
1212 return -1;
1213 } else if (dev) {
1214 object_unref(OBJECT(dev));
1216 return 0;
1219 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
1221 Error *local_err = NULL;
1223 if (!qemu_chr_new_from_opts(opts, NULL, &local_err)) {
1224 if (local_err) {
1225 error_propagate(errp, local_err);
1226 return -1;
1228 exit(0);
1230 return 0;
1233 #ifdef CONFIG_VIRTFS
1234 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
1236 return qemu_fsdev_add(opts, errp);
1238 #endif
1240 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
1242 return monitor_init_opts(opts, errp);
1245 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
1247 static int monitor_device_index = 0;
1248 QemuOpts *opts;
1249 const char *p;
1250 char label[32];
1252 if (strstart(optarg, "chardev:", &p)) {
1253 snprintf(label, sizeof(label), "%s", p);
1254 } else {
1255 snprintf(label, sizeof(label), "compat_monitor%d",
1256 monitor_device_index);
1257 opts = qemu_chr_parse_compat(label, optarg, true);
1258 if (!opts) {
1259 error_report("parse error: %s", optarg);
1260 exit(1);
1264 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
1265 qemu_opt_set(opts, "mode", mode, &error_abort);
1266 qemu_opt_set(opts, "chardev", label, &error_abort);
1267 if (!strcmp(mode, "control")) {
1268 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
1269 } else {
1270 assert(pretty == false);
1272 monitor_device_index++;
1275 struct device_config {
1276 enum {
1277 DEV_USB, /* -usbdevice */
1278 DEV_SERIAL, /* -serial */
1279 DEV_PARALLEL, /* -parallel */
1280 DEV_DEBUGCON, /* -debugcon */
1281 DEV_GDB, /* -gdb, -s */
1282 DEV_SCLP, /* s390 sclp */
1283 } type;
1284 const char *cmdline;
1285 Location loc;
1286 QTAILQ_ENTRY(device_config) next;
1289 static QTAILQ_HEAD(, device_config) device_configs =
1290 QTAILQ_HEAD_INITIALIZER(device_configs);
1292 static void add_device_config(int type, const char *cmdline)
1294 struct device_config *conf;
1296 conf = g_malloc0(sizeof(*conf));
1297 conf->type = type;
1298 conf->cmdline = cmdline;
1299 loc_save(&conf->loc);
1300 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1303 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1305 struct device_config *conf;
1306 int rc;
1308 QTAILQ_FOREACH(conf, &device_configs, next) {
1309 if (conf->type != type)
1310 continue;
1311 loc_push_restore(&conf->loc);
1312 rc = func(conf->cmdline);
1313 loc_pop(&conf->loc);
1314 if (rc) {
1315 return rc;
1318 return 0;
1321 static void qemu_disable_default_devices(void)
1323 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1325 qemu_opts_foreach(qemu_find_opts("device"),
1326 default_driver_check, NULL, NULL);
1327 qemu_opts_foreach(qemu_find_opts("global"),
1328 default_driver_check, NULL, NULL);
1330 if (!vga_model && !default_vga) {
1331 vga_interface_type = VGA_DEVICE;
1333 if (!has_defaults || machine_class->no_serial) {
1334 default_serial = 0;
1336 if (!has_defaults || machine_class->no_parallel) {
1337 default_parallel = 0;
1339 if (!has_defaults || machine_class->no_floppy) {
1340 default_floppy = 0;
1342 if (!has_defaults || machine_class->no_cdrom) {
1343 default_cdrom = 0;
1345 if (!has_defaults || machine_class->no_sdcard) {
1346 default_sdcard = 0;
1348 if (!has_defaults) {
1349 default_monitor = 0;
1350 default_net = 0;
1351 default_vga = 0;
1355 static void qemu_create_default_devices(void)
1357 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1359 if (is_daemonized()) {
1360 /* According to documentation and historically, -nographic redirects
1361 * serial port, parallel port and monitor to stdio, which does not work
1362 * with -daemonize. We can redirect these to null instead, but since
1363 * -nographic is legacy, let's just error out.
1364 * We disallow -nographic only if all other ports are not redirected
1365 * explicitly, to not break existing legacy setups which uses
1366 * -nographic _and_ redirects all ports explicitly - this is valid
1367 * usage, -nographic is just a no-op in this case.
1369 if (nographic
1370 && (default_parallel || default_serial || default_monitor)) {
1371 error_report("-nographic cannot be used with -daemonize");
1372 exit(1);
1376 if (nographic) {
1377 if (default_parallel)
1378 add_device_config(DEV_PARALLEL, "null");
1379 if (default_serial && default_monitor) {
1380 add_device_config(DEV_SERIAL, "mon:stdio");
1381 } else {
1382 if (default_serial)
1383 add_device_config(DEV_SERIAL, "stdio");
1384 if (default_monitor)
1385 monitor_parse("stdio", "readline", false);
1387 } else {
1388 if (default_serial)
1389 add_device_config(DEV_SERIAL, "vc:80Cx24C");
1390 if (default_parallel)
1391 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
1392 if (default_monitor)
1393 monitor_parse("vc:80Cx24C", "readline", false);
1396 if (default_net) {
1397 QemuOptsList *net = qemu_find_opts("net");
1398 qemu_opts_parse(net, "nic", true, &error_abort);
1399 #ifdef CONFIG_SLIRP
1400 qemu_opts_parse(net, "user", true, &error_abort);
1401 #endif
1404 #if defined(CONFIG_VNC)
1405 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
1406 display_remote++;
1408 #endif
1409 if (dpy.type == DISPLAY_TYPE_DEFAULT && !display_remote) {
1410 if (!qemu_display_find_default(&dpy)) {
1411 dpy.type = DISPLAY_TYPE_NONE;
1412 #if defined(CONFIG_VNC)
1413 vnc_parse("localhost:0,to=99,id=default");
1414 #endif
1417 if (dpy.type == DISPLAY_TYPE_DEFAULT) {
1418 dpy.type = DISPLAY_TYPE_NONE;
1421 /* If no default VGA is requested, the default is "none". */
1422 if (default_vga) {
1423 vga_model = get_default_vga_model(machine_class);
1425 if (vga_model) {
1426 select_vgahw(machine_class, vga_model);
1430 static int serial_parse(const char *devname)
1432 int index = num_serial_hds;
1433 char label[32];
1435 if (strcmp(devname, "none") == 0)
1436 return 0;
1437 snprintf(label, sizeof(label), "serial%d", index);
1438 serial_hds = g_renew(Chardev *, serial_hds, index + 1);
1440 serial_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
1441 if (!serial_hds[index]) {
1442 error_report("could not connect serial device"
1443 " to character backend '%s'", devname);
1444 return -1;
1446 num_serial_hds++;
1447 return 0;
1450 Chardev *serial_hd(int i)
1452 assert(i >= 0);
1453 if (i < num_serial_hds) {
1454 return serial_hds[i];
1456 return NULL;
1459 static int parallel_parse(const char *devname)
1461 static int index = 0;
1462 char label[32];
1464 if (strcmp(devname, "none") == 0)
1465 return 0;
1466 if (index == MAX_PARALLEL_PORTS) {
1467 error_report("too many parallel ports");
1468 exit(1);
1470 snprintf(label, sizeof(label), "parallel%d", index);
1471 parallel_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
1472 if (!parallel_hds[index]) {
1473 error_report("could not connect parallel device"
1474 " to character backend '%s'", devname);
1475 return -1;
1477 index++;
1478 return 0;
1481 static int debugcon_parse(const char *devname)
1483 QemuOpts *opts;
1485 if (!qemu_chr_new_mux_mon("debugcon", devname, NULL)) {
1486 error_report("invalid character backend '%s'", devname);
1487 exit(1);
1489 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
1490 if (!opts) {
1491 error_report("already have a debugcon device");
1492 exit(1);
1494 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
1495 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
1496 return 0;
1499 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
1501 const MachineClass *mc1 = a, *mc2 = b;
1502 int res;
1504 if (mc1->family == NULL) {
1505 if (mc2->family == NULL) {
1506 /* Compare standalone machine types against each other; they sort
1507 * in increasing order.
1509 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
1510 object_class_get_name(OBJECT_CLASS(mc2)));
1513 /* Standalone machine types sort after families. */
1514 return 1;
1517 if (mc2->family == NULL) {
1518 /* Families sort before standalone machine types. */
1519 return -1;
1522 /* Families sort between each other alphabetically increasingly. */
1523 res = strcmp(mc1->family, mc2->family);
1524 if (res != 0) {
1525 return res;
1528 /* Within the same family, machine types sort in decreasing order. */
1529 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
1530 object_class_get_name(OBJECT_CLASS(mc1)));
1533 static MachineClass *machine_parse(const char *name, GSList *machines)
1535 MachineClass *mc;
1536 GSList *el;
1538 if (is_help_option(name)) {
1539 printf("Supported machines are:\n");
1540 machines = g_slist_sort(machines, machine_class_cmp);
1541 for (el = machines; el; el = el->next) {
1542 MachineClass *mc = el->data;
1543 if (mc->alias) {
1544 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
1546 printf("%-20s %s%s%s\n", mc->name, mc->desc,
1547 mc->is_default ? " (default)" : "",
1548 mc->deprecation_reason ? " (deprecated)" : "");
1550 exit(0);
1553 mc = find_machine(name, machines);
1554 if (!mc) {
1555 error_report("unsupported machine type");
1556 error_printf("Use -machine help to list supported machines\n");
1557 exit(1);
1559 return mc;
1562 static const char *pid_file;
1563 static Notifier qemu_unlink_pidfile_notifier;
1565 static void qemu_unlink_pidfile(Notifier *n, void *data)
1567 if (pid_file) {
1568 unlink(pid_file);
1572 static const QEMUOption *lookup_opt(int argc, char **argv,
1573 const char **poptarg, int *poptind)
1575 const QEMUOption *popt;
1576 int optind = *poptind;
1577 char *r = argv[optind];
1578 const char *optarg;
1580 loc_set_cmdline(argv, optind, 1);
1581 optind++;
1582 /* Treat --foo the same as -foo. */
1583 if (r[1] == '-')
1584 r++;
1585 popt = qemu_options;
1586 for(;;) {
1587 if (!popt->name) {
1588 error_report("invalid option");
1589 exit(1);
1591 if (!strcmp(popt->name, r + 1))
1592 break;
1593 popt++;
1595 if (popt->flags & HAS_ARG) {
1596 if (optind >= argc) {
1597 error_report("requires an argument");
1598 exit(1);
1600 optarg = argv[optind++];
1601 loc_set_cmdline(argv, optind - 2, 2);
1602 } else {
1603 optarg = NULL;
1606 *poptarg = optarg;
1607 *poptind = optind;
1609 return popt;
1612 static MachineClass *select_machine(void)
1614 GSList *machines = object_class_get_list(TYPE_MACHINE, false);
1615 MachineClass *machine_class = find_default_machine(machines);
1616 const char *optarg;
1617 QemuOpts *opts;
1618 Location loc;
1620 loc_push_none(&loc);
1622 opts = qemu_get_machine_opts();
1623 qemu_opts_loc_restore(opts);
1625 optarg = qemu_opt_get(opts, "type");
1626 if (optarg) {
1627 machine_class = machine_parse(optarg, machines);
1630 if (!machine_class) {
1631 error_report("No machine specified, and there is no default");
1632 error_printf("Use -machine help to list supported machines\n");
1633 exit(1);
1636 loc_pop(&loc);
1637 g_slist_free(machines);
1638 return machine_class;
1641 static int object_parse_property_opt(Object *obj,
1642 const char *name, const char *value,
1643 const char *skip, Error **errp)
1645 if (g_str_equal(name, skip)) {
1646 return 0;
1649 if (!object_property_parse(obj, name, value, errp)) {
1650 return -1;
1653 return 0;
1656 static int machine_set_property(void *opaque,
1657 const char *name, const char *value,
1658 Error **errp)
1660 g_autofree char *qom_name = g_strdup(name);
1661 char *p;
1663 for (p = qom_name; *p; p++) {
1664 if (*p == '_') {
1665 *p = '-';
1669 /* Legacy options do not correspond to MachineState properties. */
1670 if (g_str_equal(qom_name, "accel")) {
1671 return 0;
1673 if (g_str_equal(qom_name, "igd-passthru")) {
1674 object_register_sugar_prop(ACCEL_CLASS_NAME("xen"), qom_name, value,
1675 false);
1676 return 0;
1678 if (g_str_equal(qom_name, "kvm-shadow-mem")) {
1679 object_register_sugar_prop(ACCEL_CLASS_NAME("kvm"), qom_name, value,
1680 false);
1681 return 0;
1683 if (g_str_equal(qom_name, "kernel-irqchip")) {
1684 object_register_sugar_prop(ACCEL_CLASS_NAME("kvm"), qom_name, value,
1685 false);
1686 object_register_sugar_prop(ACCEL_CLASS_NAME("whpx"), qom_name, value,
1687 false);
1688 return 0;
1691 return object_parse_property_opt(opaque, name, value, "type", errp);
1694 static void object_option_foreach_add(bool (*type_opt_predicate)(const char *))
1696 ObjectOption *opt, *next;
1698 QTAILQ_FOREACH_SAFE(opt, &object_opts, next, next) {
1699 const char *type = ObjectType_str(opt->opts->qom_type);
1700 if (type_opt_predicate(type)) {
1701 user_creatable_add_qapi(opt->opts, &error_fatal);
1702 qapi_free_ObjectOptions(opt->opts);
1703 QTAILQ_REMOVE(&object_opts, opt, next);
1704 g_free(opt);
1709 static void object_option_parse(const char *optarg)
1711 ObjectOption *opt;
1712 QemuOpts *opts;
1713 const char *type;
1714 Visitor *v;
1716 if (optarg[0] == '{') {
1717 QObject *obj = qobject_from_json(optarg, &error_fatal);
1719 v = qobject_input_visitor_new(obj);
1720 qobject_unref(obj);
1721 } else {
1722 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
1723 optarg, true);
1724 if (!opts) {
1725 exit(1);
1728 type = qemu_opt_get(opts, "qom-type");
1729 if (!type) {
1730 error_setg(&error_fatal, QERR_MISSING_PARAMETER, "qom-type");
1732 if (user_creatable_print_help(type, opts)) {
1733 exit(0);
1736 v = opts_visitor_new(opts);
1739 opt = g_new0(ObjectOption, 1);
1740 visit_type_ObjectOptions(v, NULL, &opt->opts, &error_fatal);
1741 visit_free(v);
1743 QTAILQ_INSERT_TAIL(&object_opts, opt, next);
1747 * Initial object creation happens before all other
1748 * QEMU data types are created. The majority of objects
1749 * can be created at this point. The rng-egd object
1750 * cannot be created here, as it depends on the chardev
1751 * already existing.
1753 static bool object_create_early(const char *type)
1756 * Objects should not be made "delayed" without a reason. If you
1757 * add one, state the reason in a comment!
1760 /* Reason: rng-egd property "chardev" */
1761 if (g_str_equal(type, "rng-egd")) {
1762 return false;
1765 #if defined(CONFIG_VHOST_USER) && defined(CONFIG_LINUX)
1766 /* Reason: cryptodev-vhost-user property "chardev" */
1767 if (g_str_equal(type, "cryptodev-vhost-user")) {
1768 return false;
1770 #endif
1772 /* Reason: vhost-user-blk-server property "node-name" */
1773 if (g_str_equal(type, "vhost-user-blk-server")) {
1774 return false;
1777 * Reason: filter-* property "netdev" etc.
1779 if (g_str_equal(type, "filter-buffer") ||
1780 g_str_equal(type, "filter-dump") ||
1781 g_str_equal(type, "filter-mirror") ||
1782 g_str_equal(type, "filter-redirector") ||
1783 g_str_equal(type, "colo-compare") ||
1784 g_str_equal(type, "filter-rewriter") ||
1785 g_str_equal(type, "filter-replay")) {
1786 return false;
1790 * Allocation of large amounts of memory may delay
1791 * chardev initialization for too long, and trigger timeouts
1792 * on software that waits for a monitor socket to be created
1793 * (e.g. libvirt).
1795 if (g_str_has_prefix(type, "memory-backend-")) {
1796 return false;
1799 return true;
1802 static void qemu_apply_machine_options(void)
1804 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1805 QemuOpts *machine_opts = qemu_get_machine_opts();
1806 const char *boot_order = NULL;
1807 const char *boot_once = NULL;
1808 QemuOpts *opts;
1810 qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
1811 &error_fatal);
1812 current_machine->ram_size = ram_size;
1813 current_machine->maxram_size = maxram_size;
1814 current_machine->ram_slots = ram_slots;
1816 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
1817 if (opts) {
1818 boot_order = qemu_opt_get(opts, "order");
1819 if (boot_order) {
1820 validate_bootdevices(boot_order, &error_fatal);
1823 boot_once = qemu_opt_get(opts, "once");
1824 if (boot_once) {
1825 validate_bootdevices(boot_once, &error_fatal);
1828 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
1829 boot_strict = qemu_opt_get_bool(opts, "strict", false);
1832 if (!boot_order) {
1833 boot_order = machine_class->default_boot_order;
1836 current_machine->boot_order = boot_order;
1837 current_machine->boot_once = boot_once;
1839 if (semihosting_enabled() && !semihosting_get_argc()) {
1840 const char *kernel_filename = qemu_opt_get(machine_opts, "kernel");
1841 const char *kernel_cmdline = qemu_opt_get(machine_opts, "append") ?: "";
1842 /* fall back to the -kernel/-append */
1843 semihosting_arg_fallback(kernel_filename, kernel_cmdline);
1847 static void qemu_create_early_backends(void)
1849 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1851 if ((alt_grab || ctrl_grab) && dpy.type != DISPLAY_TYPE_SDL) {
1852 error_report("-alt-grab and -ctrl-grab are only valid "
1853 "for SDL, ignoring option");
1855 if (dpy.has_window_close &&
1856 (dpy.type != DISPLAY_TYPE_GTK && dpy.type != DISPLAY_TYPE_SDL)) {
1857 error_report("-no-quit is only valid for GTK and SDL, "
1858 "ignoring option");
1861 qemu_display_early_init(&dpy);
1862 qemu_console_early_init();
1864 if (dpy.has_gl && dpy.gl != DISPLAYGL_MODE_OFF && display_opengl == 0) {
1865 #if defined(CONFIG_OPENGL)
1866 error_report("OpenGL is not supported by the display");
1867 #else
1868 error_report("OpenGL support is disabled");
1869 #endif
1870 exit(1);
1873 object_option_foreach_add(object_create_early);
1875 /* spice needs the timers to be initialized by this point */
1876 /* spice must initialize before audio as it changes the default auiodev */
1877 /* spice must initialize before chardevs (for spicevmc and spiceport) */
1878 qemu_spice.init();
1880 qemu_opts_foreach(qemu_find_opts("chardev"),
1881 chardev_init_func, NULL, &error_fatal);
1883 #ifdef CONFIG_VIRTFS
1884 qemu_opts_foreach(qemu_find_opts("fsdev"),
1885 fsdev_init_func, NULL, &error_fatal);
1886 #endif
1889 * Note: we need to create audio and block backends before
1890 * machine_set_property(), so machine properties can refer to
1891 * them.
1893 configure_blockdev(&bdo_queue, machine_class, snapshot);
1894 audio_init_audiodevs();
1899 * The remainder of object creation happens after the
1900 * creation of chardev, fsdev, net clients and device data types.
1902 static bool object_create_late(const char *type)
1904 return !object_create_early(type);
1907 static void qemu_create_late_backends(void)
1909 if (qtest_chrdev) {
1910 qtest_server_init(qtest_chrdev, qtest_log, &error_fatal);
1913 net_init_clients(&error_fatal);
1915 object_option_foreach_add(object_create_late);
1917 if (tpm_init() < 0) {
1918 exit(1);
1921 qemu_opts_foreach(qemu_find_opts("mon"),
1922 mon_init_func, NULL, &error_fatal);
1924 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
1925 exit(1);
1926 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
1927 exit(1);
1928 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
1929 exit(1);
1931 /* now chardevs have been created we may have semihosting to connect */
1932 qemu_semihosting_connect_chardevs();
1933 qemu_semihosting_console_init();
1936 static bool have_custom_ram_size(void)
1938 QemuOpts *opts = qemu_find_opts_singleton("memory");
1939 return !!qemu_opt_get_size(opts, "size", 0);
1942 static void qemu_resolve_machine_memdev(void)
1944 if (current_machine->ram_memdev_id) {
1945 Object *backend;
1946 ram_addr_t backend_size;
1948 backend = object_resolve_path_type(current_machine->ram_memdev_id,
1949 TYPE_MEMORY_BACKEND, NULL);
1950 if (!backend) {
1951 error_report("Memory backend '%s' not found",
1952 current_machine->ram_memdev_id);
1953 exit(EXIT_FAILURE);
1955 backend_size = object_property_get_uint(backend, "size", &error_abort);
1956 if (have_custom_ram_size() && backend_size != ram_size) {
1957 error_report("Size specified by -m option must match size of "
1958 "explicitly specified 'memory-backend' property");
1959 exit(EXIT_FAILURE);
1961 if (mem_path) {
1962 error_report("'-mem-path' can't be used together with"
1963 "'-machine memory-backend'");
1964 exit(EXIT_FAILURE);
1966 ram_size = backend_size;
1969 if (!xen_enabled()) {
1970 /* On 32-bit hosts, QEMU is limited by virtual address space */
1971 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
1972 error_report("at most 2047 MB RAM can be simulated");
1973 exit(1);
1978 static void set_memory_options(MachineClass *mc)
1980 uint64_t sz;
1981 const char *mem_str;
1982 const ram_addr_t default_ram_size = mc->default_ram_size;
1983 QemuOpts *opts = qemu_find_opts_singleton("memory");
1984 Location loc;
1986 loc_push_none(&loc);
1987 qemu_opts_loc_restore(opts);
1989 sz = 0;
1990 mem_str = qemu_opt_get(opts, "size");
1991 if (mem_str) {
1992 if (!*mem_str) {
1993 error_report("missing 'size' option value");
1994 exit(EXIT_FAILURE);
1997 sz = qemu_opt_get_size(opts, "size", ram_size);
1999 /* Fix up legacy suffix-less format */
2000 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2001 uint64_t overflow_check = sz;
2003 sz *= MiB;
2004 if (sz / MiB != overflow_check) {
2005 error_report("too large 'size' option value");
2006 exit(EXIT_FAILURE);
2011 /* backward compatibility behaviour for case "-m 0" */
2012 if (sz == 0) {
2013 sz = default_ram_size;
2016 sz = QEMU_ALIGN_UP(sz, 8192);
2017 if (mc->fixup_ram_size) {
2018 sz = mc->fixup_ram_size(sz);
2020 ram_size = sz;
2021 if (ram_size != sz) {
2022 error_report("ram size too large");
2023 exit(EXIT_FAILURE);
2026 /* store value for the future use */
2027 qemu_opt_set_number(opts, "size", ram_size, &error_abort);
2028 maxram_size = ram_size;
2030 if (qemu_opt_get(opts, "maxmem")) {
2031 uint64_t slots;
2033 sz = qemu_opt_get_size(opts, "maxmem", 0);
2034 slots = qemu_opt_get_number(opts, "slots", 0);
2035 if (sz < ram_size) {
2036 error_report("invalid value of -m option maxmem: "
2037 "maximum memory size (0x%" PRIx64 ") must be at least "
2038 "the initial memory size (0x" RAM_ADDR_FMT ")",
2039 sz, ram_size);
2040 exit(EXIT_FAILURE);
2041 } else if (slots && sz == ram_size) {
2042 error_report("invalid value of -m option maxmem: "
2043 "memory slots were specified but maximum memory size "
2044 "(0x%" PRIx64 ") is equal to the initial memory size "
2045 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
2046 exit(EXIT_FAILURE);
2049 maxram_size = sz;
2050 ram_slots = slots;
2051 } else if (qemu_opt_get(opts, "slots")) {
2052 error_report("invalid -m option value: missing 'maxmem' option");
2053 exit(EXIT_FAILURE);
2056 loc_pop(&loc);
2059 static void qemu_create_machine(MachineClass *machine_class)
2061 object_set_machine_compat_props(machine_class->compat_props);
2063 set_memory_options(machine_class);
2065 current_machine = MACHINE(object_new_with_class(OBJECT_CLASS(machine_class)));
2066 if (machine_help_func(qemu_get_machine_opts(), current_machine)) {
2067 exit(0);
2069 object_property_add_child(object_get_root(), "machine",
2070 OBJECT(current_machine));
2071 object_property_add_child(container_get(OBJECT(current_machine),
2072 "/unattached"),
2073 "sysbus", OBJECT(sysbus_get_default()));
2075 if (machine_class->minimum_page_bits) {
2076 if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
2077 /* This would be a board error: specifying a minimum smaller than
2078 * a target's compile-time fixed setting.
2080 g_assert_not_reached();
2084 cpu_exec_init_all();
2085 page_size_init();
2087 if (machine_class->hw_version) {
2088 qemu_set_hw_version(machine_class->hw_version);
2091 machine_smp_parse(current_machine,
2092 qemu_opts_find(qemu_find_opts("smp-opts"), NULL), &error_fatal);
2095 * Get the default machine options from the machine if it is not already
2096 * specified either by the configuration file or by the command line.
2098 if (machine_class->default_machine_opts) {
2099 qemu_opts_set_defaults(qemu_find_opts("machine"),
2100 machine_class->default_machine_opts, 0);
2104 static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
2106 GlobalProperty *g;
2108 g = g_malloc0(sizeof(*g));
2109 g->driver = qemu_opt_get(opts, "driver");
2110 g->property = qemu_opt_get(opts, "property");
2111 g->value = qemu_opt_get(opts, "value");
2112 qdev_prop_register_global(g);
2113 return 0;
2116 static void qemu_read_default_config_file(Error **errp)
2118 ERRP_GUARD();
2119 int ret;
2120 g_autofree char *file = get_relocated_path(CONFIG_QEMU_CONFDIR "/qemu.conf");
2122 ret = qemu_read_config_file(file, errp);
2123 if (ret < 0) {
2124 if (ret == -ENOENT) {
2125 error_free(*errp);
2126 *errp = NULL;
2131 static int qemu_set_option(const char *str)
2133 Error *local_err = NULL;
2134 char group[64], id[64], arg[64];
2135 QemuOptsList *list;
2136 QemuOpts *opts;
2137 int rc, offset;
2139 rc = sscanf(str, "%63[^.].%63[^.].%63[^=]%n", group, id, arg, &offset);
2140 if (rc < 3 || str[offset] != '=') {
2141 error_report("can't parse: \"%s\"", str);
2142 return -1;
2145 list = qemu_find_opts(group);
2146 if (list == NULL) {
2147 return -1;
2150 opts = qemu_opts_find(list, id);
2151 if (!opts) {
2152 error_report("there is no %s \"%s\" defined",
2153 list->name, id);
2154 return -1;
2157 if (!qemu_opt_set(opts, arg, str + offset + 1, &local_err)) {
2158 error_report_err(local_err);
2159 return -1;
2161 return 0;
2164 static void user_register_global_props(void)
2166 qemu_opts_foreach(qemu_find_opts("global"),
2167 global_init_func, NULL, NULL);
2170 static int do_configure_icount(void *opaque, QemuOpts *opts, Error **errp)
2172 icount_configure(opts, errp);
2173 return 0;
2176 static int accelerator_set_property(void *opaque,
2177 const char *name, const char *value,
2178 Error **errp)
2180 return object_parse_property_opt(opaque, name, value, "accel", errp);
2183 static int do_configure_accelerator(void *opaque, QemuOpts *opts, Error **errp)
2185 bool *p_init_failed = opaque;
2186 const char *acc = qemu_opt_get(opts, "accel");
2187 AccelClass *ac = accel_find(acc);
2188 AccelState *accel;
2189 int ret;
2190 bool qtest_with_kvm;
2192 qtest_with_kvm = g_str_equal(acc, "kvm") && qtest_chrdev != NULL;
2194 if (!ac) {
2195 *p_init_failed = true;
2196 if (!qtest_with_kvm) {
2197 error_report("invalid accelerator %s", acc);
2199 return 0;
2201 accel = ACCEL(object_new_with_class(OBJECT_CLASS(ac)));
2202 object_apply_compat_props(OBJECT(accel));
2203 qemu_opt_foreach(opts, accelerator_set_property,
2204 accel,
2205 &error_fatal);
2207 ret = accel_init_machine(accel, current_machine);
2208 if (ret < 0) {
2209 *p_init_failed = true;
2210 if (!qtest_with_kvm || ret != -ENOENT) {
2211 error_report("failed to initialize %s: %s", acc, strerror(-ret));
2213 return 0;
2216 return 1;
2219 static void configure_accelerators(const char *progname)
2221 const char *accelerators;
2222 bool init_failed = false;
2224 qemu_opts_foreach(qemu_find_opts("icount"),
2225 do_configure_icount, NULL, &error_fatal);
2227 accelerators = qemu_opt_get(qemu_get_machine_opts(), "accel");
2228 if (QTAILQ_EMPTY(&qemu_accel_opts.head)) {
2229 char **accel_list, **tmp;
2231 if (accelerators == NULL) {
2232 /* Select the default accelerator */
2233 bool have_tcg = accel_find("tcg");
2234 bool have_kvm = accel_find("kvm");
2236 if (have_tcg && have_kvm) {
2237 if (g_str_has_suffix(progname, "kvm")) {
2238 /* If the program name ends with "kvm", we prefer KVM */
2239 accelerators = "kvm:tcg";
2240 } else {
2241 accelerators = "tcg:kvm";
2243 } else if (have_kvm) {
2244 accelerators = "kvm";
2245 } else if (have_tcg) {
2246 accelerators = "tcg";
2247 } else {
2248 error_report("No accelerator selected and"
2249 " no default accelerator available");
2250 exit(1);
2253 accel_list = g_strsplit(accelerators, ":", 0);
2255 for (tmp = accel_list; *tmp; tmp++) {
2257 * Filter invalid accelerators here, to prevent obscenities
2258 * such as "-machine accel=tcg,,thread=single".
2260 if (accel_find(*tmp)) {
2261 qemu_opts_parse_noisily(qemu_find_opts("accel"), *tmp, true);
2262 } else {
2263 init_failed = true;
2264 error_report("invalid accelerator %s", *tmp);
2267 g_strfreev(accel_list);
2268 } else {
2269 if (accelerators != NULL) {
2270 error_report("The -accel and \"-machine accel=\" options are incompatible");
2271 exit(1);
2275 if (!qemu_opts_foreach(qemu_find_opts("accel"),
2276 do_configure_accelerator, &init_failed, &error_fatal)) {
2277 if (!init_failed) {
2278 error_report("no accelerator found");
2280 exit(1);
2283 if (init_failed && !qtest_chrdev) {
2284 AccelClass *ac = ACCEL_GET_CLASS(current_accel());
2285 error_report("falling back to %s", ac->name);
2288 if (icount_enabled() && !tcg_enabled()) {
2289 error_report("-icount is not allowed with hardware virtualization");
2290 exit(1);
2294 static void create_default_memdev(MachineState *ms, const char *path)
2296 Object *obj;
2297 MachineClass *mc = MACHINE_GET_CLASS(ms);
2299 obj = object_new(path ? TYPE_MEMORY_BACKEND_FILE : TYPE_MEMORY_BACKEND_RAM);
2300 if (path) {
2301 object_property_set_str(obj, "mem-path", path, &error_fatal);
2303 object_property_set_int(obj, "size", ms->ram_size, &error_fatal);
2304 object_property_add_child(object_get_objects_root(), mc->default_ram_id,
2305 obj);
2306 /* Ensure backend's memory region name is equal to mc->default_ram_id */
2307 object_property_set_bool(obj, "x-use-canonical-path-for-ramblock-id",
2308 false, &error_fatal);
2309 user_creatable_complete(USER_CREATABLE(obj), &error_fatal);
2310 object_unref(obj);
2311 object_property_set_str(OBJECT(ms), "memory-backend", mc->default_ram_id,
2312 &error_fatal);
2315 static void qemu_validate_options(void)
2317 QemuOpts *machine_opts = qemu_get_machine_opts();
2318 const char *kernel_filename = qemu_opt_get(machine_opts, "kernel");
2319 const char *initrd_filename = qemu_opt_get(machine_opts, "initrd");
2320 const char *kernel_cmdline = qemu_opt_get(machine_opts, "append");
2322 if (kernel_filename == NULL) {
2323 if (kernel_cmdline != NULL) {
2324 error_report("-append only allowed with -kernel option");
2325 exit(1);
2328 if (initrd_filename != NULL) {
2329 error_report("-initrd only allowed with -kernel option");
2330 exit(1);
2334 if (loadvm && preconfig_requested) {
2335 error_report("'preconfig' and 'loadvm' options are "
2336 "mutually exclusive");
2337 exit(EXIT_FAILURE);
2339 if (incoming && preconfig_requested && strcmp(incoming, "defer") != 0) {
2340 error_report("'preconfig' supports '-incoming defer' only");
2341 exit(EXIT_FAILURE);
2344 #ifdef CONFIG_CURSES
2345 if (is_daemonized() && dpy.type == DISPLAY_TYPE_CURSES) {
2346 error_report("curses display cannot be used with -daemonize");
2347 exit(1);
2349 #endif
2352 static void qemu_process_sugar_options(void)
2354 if (mem_prealloc) {
2355 char *val;
2357 val = g_strdup_printf("%d",
2358 (uint32_t) qemu_opt_get_number(qemu_find_opts_singleton("smp-opts"), "cpus", 1));
2359 object_register_sugar_prop("memory-backend", "prealloc-threads", val,
2360 false);
2361 g_free(val);
2362 object_register_sugar_prop("memory-backend", "prealloc", "on", false);
2365 if (watchdog) {
2366 int i = select_watchdog(watchdog);
2367 if (i > 0)
2368 exit (i == 1 ? 1 : 0);
2372 /* -action processing */
2375 * Process all the -action parameters parsed from cmdline.
2377 static int process_runstate_actions(void *opaque, QemuOpts *opts, Error **errp)
2379 Error *local_err = NULL;
2380 QDict *qdict = qemu_opts_to_qdict(opts, NULL);
2381 QObject *ret = NULL;
2382 qmp_marshal_set_action(qdict, &ret, &local_err);
2383 qobject_unref(ret);
2384 qobject_unref(qdict);
2385 if (local_err) {
2386 error_propagate(errp, local_err);
2387 return 1;
2389 return 0;
2392 static void qemu_process_early_options(void)
2394 #ifdef CONFIG_SECCOMP
2395 QemuOptsList *olist = qemu_find_opts_err("sandbox", NULL);
2396 if (olist) {
2397 qemu_opts_foreach(olist, parse_sandbox, NULL, &error_fatal);
2399 #endif
2401 qemu_opts_foreach(qemu_find_opts("name"),
2402 parse_name, NULL, &error_fatal);
2404 if (qemu_opts_foreach(qemu_find_opts("action"),
2405 process_runstate_actions, NULL, &error_fatal)) {
2406 exit(1);
2409 #ifndef _WIN32
2410 qemu_opts_foreach(qemu_find_opts("add-fd"),
2411 parse_add_fd, NULL, &error_fatal);
2413 qemu_opts_foreach(qemu_find_opts("add-fd"),
2414 cleanup_add_fd, NULL, &error_fatal);
2415 #endif
2417 /* Open the logfile at this point and set the log mask if necessary. */
2418 if (log_file) {
2419 qemu_set_log_filename(log_file, &error_fatal);
2421 if (log_mask) {
2422 int mask;
2423 mask = qemu_str_to_log_mask(log_mask);
2424 if (!mask) {
2425 qemu_print_log_usage(stdout);
2426 exit(1);
2428 qemu_set_log(mask);
2429 } else {
2430 qemu_set_log(0);
2433 qemu_add_default_firmwarepath();
2436 static void qemu_process_help_options(void)
2439 * Check for -cpu help and -device help before we call select_machine(),
2440 * which will return an error if the architecture has no default machine
2441 * type and the user did not specify one, so that the user doesn't need
2442 * to say '-cpu help -machine something'.
2444 if (cpu_option && is_help_option(cpu_option)) {
2445 list_cpus(cpu_option);
2446 exit(0);
2449 if (qemu_opts_foreach(qemu_find_opts("device"),
2450 device_help_func, NULL, NULL)) {
2451 exit(0);
2454 /* -L help lists the data directories and exits. */
2455 if (list_data_dirs) {
2456 qemu_list_data_dirs();
2457 exit(0);
2461 static void qemu_maybe_daemonize(const char *pid_file)
2463 Error *err;
2465 os_daemonize();
2466 rcu_disable_atfork();
2468 if (pid_file && !qemu_write_pidfile(pid_file, &err)) {
2469 error_reportf_err(err, "cannot create PID file: ");
2470 exit(1);
2473 qemu_unlink_pidfile_notifier.notify = qemu_unlink_pidfile;
2474 qemu_add_exit_notifier(&qemu_unlink_pidfile_notifier);
2477 static void qemu_init_displays(void)
2479 DisplayState *ds;
2481 /* init local displays */
2482 ds = init_displaystate();
2483 qemu_display_init(ds, &dpy);
2485 /* must be after terminal init, SDL library changes signal handlers */
2486 os_setup_signal_handling();
2488 /* init remote displays */
2489 #ifdef CONFIG_VNC
2490 qemu_opts_foreach(qemu_find_opts("vnc"),
2491 vnc_init_func, NULL, &error_fatal);
2492 #endif
2494 if (using_spice) {
2495 qemu_spice.display_init();
2499 static void qemu_init_board(void)
2501 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
2503 if (machine_class->default_ram_id && current_machine->ram_size &&
2504 numa_uses_legacy_mem() && !current_machine->ram_memdev_id) {
2505 create_default_memdev(current_machine, mem_path);
2508 /* process plugin before CPUs are created, but once -smp has been parsed */
2509 qemu_plugin_load_list(&plugin_list, &error_fatal);
2511 /* From here on we enter MACHINE_PHASE_INITIALIZED. */
2512 machine_run_board_init(current_machine);
2515 * TODO To drop support for deprecated bogus if=..., move
2516 * drive_check_orphaned() here, replacing this call. Also drop
2517 * its deprecation warning, along with DriveInfo member
2518 * @claimed_by_board.
2520 drive_mark_claimed_by_board();
2522 realtime_init();
2524 if (hax_enabled()) {
2525 /* FIXME: why isn't cpu_synchronize_all_post_init enough? */
2526 hax_sync_vcpus();
2530 static void qemu_create_cli_devices(void)
2532 soundhw_init();
2534 qemu_opts_foreach(qemu_find_opts("fw_cfg"),
2535 parse_fw_cfg, fw_cfg_find(), &error_fatal);
2537 /* init USB devices */
2538 if (machine_usb(current_machine)) {
2539 if (foreach_device_config(DEV_USB, usb_parse) < 0)
2540 exit(1);
2543 /* init generic devices */
2544 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
2545 qemu_opts_foreach(qemu_find_opts("device"),
2546 device_init_func, NULL, &error_fatal);
2547 rom_reset_order_override();
2550 static void qemu_machine_creation_done(void)
2552 MachineState *machine = MACHINE(qdev_get_machine());
2554 /* Did we create any drives that we failed to create a device for? */
2555 drive_check_orphaned();
2557 /* Don't warn about the default network setup that you get if
2558 * no command line -net or -netdev options are specified. There
2559 * are two cases that we would otherwise complain about:
2560 * (1) board doesn't support a NIC but the implicit "-net nic"
2561 * requested one
2562 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
2563 * sets up a nic that isn't connected to anything.
2565 if (!default_net && (!qtest_enabled() || has_defaults)) {
2566 net_check_clients();
2569 qdev_prop_check_globals();
2571 qdev_machine_creation_done();
2573 if (machine->cgs) {
2575 * Verify that Confidential Guest Support has actually been initialized
2577 assert(machine->cgs->ready);
2580 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
2581 exit(1);
2585 void qmp_x_exit_preconfig(Error **errp)
2587 if (phase_check(PHASE_MACHINE_INITIALIZED)) {
2588 error_setg(errp, "The command is permitted only before machine initialization");
2589 return;
2592 qemu_init_board();
2593 qemu_create_cli_devices();
2594 qemu_machine_creation_done();
2596 if (loadvm) {
2597 Error *local_err = NULL;
2598 if (!load_snapshot(loadvm, NULL, false, NULL, &local_err)) {
2599 error_report_err(local_err);
2600 autostart = 0;
2601 exit(1);
2604 if (replay_mode != REPLAY_MODE_NONE) {
2605 replay_vmstate_init();
2608 if (incoming) {
2609 Error *local_err = NULL;
2610 if (strcmp(incoming, "defer") != 0) {
2611 qmp_migrate_incoming(incoming, &local_err);
2612 if (local_err) {
2613 error_reportf_err(local_err, "-incoming %s: ", incoming);
2614 exit(1);
2617 } else if (autostart) {
2618 qmp_cont(NULL);
2622 void qemu_init(int argc, char **argv, char **envp)
2624 QemuOpts *opts;
2625 QemuOpts *icount_opts = NULL, *accel_opts = NULL;
2626 QemuOptsList *olist;
2627 int optind;
2628 const char *optarg;
2629 MachineClass *machine_class;
2630 bool userconfig = true;
2631 FILE *vmstate_dump_file = NULL;
2633 qemu_add_opts(&qemu_drive_opts);
2634 qemu_add_drive_opts(&qemu_legacy_drive_opts);
2635 qemu_add_drive_opts(&qemu_common_drive_opts);
2636 qemu_add_drive_opts(&qemu_drive_opts);
2637 qemu_add_drive_opts(&bdrv_runtime_opts);
2638 qemu_add_opts(&qemu_chardev_opts);
2639 qemu_add_opts(&qemu_device_opts);
2640 qemu_add_opts(&qemu_netdev_opts);
2641 qemu_add_opts(&qemu_nic_opts);
2642 qemu_add_opts(&qemu_net_opts);
2643 qemu_add_opts(&qemu_rtc_opts);
2644 qemu_add_opts(&qemu_global_opts);
2645 qemu_add_opts(&qemu_mon_opts);
2646 qemu_add_opts(&qemu_trace_opts);
2647 qemu_plugin_add_opts();
2648 qemu_add_opts(&qemu_option_rom_opts);
2649 qemu_add_opts(&qemu_machine_opts);
2650 qemu_add_opts(&qemu_accel_opts);
2651 qemu_add_opts(&qemu_mem_opts);
2652 qemu_add_opts(&qemu_smp_opts);
2653 qemu_add_opts(&qemu_boot_opts);
2654 qemu_add_opts(&qemu_add_fd_opts);
2655 qemu_add_opts(&qemu_object_opts);
2656 qemu_add_opts(&qemu_tpmdev_opts);
2657 qemu_add_opts(&qemu_overcommit_opts);
2658 qemu_add_opts(&qemu_msg_opts);
2659 qemu_add_opts(&qemu_name_opts);
2660 qemu_add_opts(&qemu_numa_opts);
2661 qemu_add_opts(&qemu_icount_opts);
2662 qemu_add_opts(&qemu_semihosting_config_opts);
2663 qemu_add_opts(&qemu_fw_cfg_opts);
2664 qemu_add_opts(&qemu_action_opts);
2665 module_call_init(MODULE_INIT_OPTS);
2667 error_init(argv[0]);
2668 qemu_init_exec_dir(argv[0]);
2670 qemu_init_subsystems();
2672 /* first pass of option parsing */
2673 optind = 1;
2674 while (optind < argc) {
2675 if (argv[optind][0] != '-') {
2676 /* disk image */
2677 optind++;
2678 } else {
2679 const QEMUOption *popt;
2681 popt = lookup_opt(argc, argv, &optarg, &optind);
2682 switch (popt->index) {
2683 case QEMU_OPTION_nouserconfig:
2684 userconfig = false;
2685 break;
2690 if (userconfig) {
2691 qemu_read_default_config_file(&error_fatal);
2694 /* second pass of option parsing */
2695 optind = 1;
2696 for(;;) {
2697 if (optind >= argc)
2698 break;
2699 if (argv[optind][0] != '-') {
2700 loc_set_cmdline(argv, optind, 1);
2701 drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2702 } else {
2703 const QEMUOption *popt;
2705 popt = lookup_opt(argc, argv, &optarg, &optind);
2706 if (!(popt->arch_mask & arch_type)) {
2707 error_report("Option not supported for this target");
2708 exit(1);
2710 switch(popt->index) {
2711 case QEMU_OPTION_cpu:
2712 /* hw initialization will check this */
2713 cpu_option = optarg;
2714 break;
2715 case QEMU_OPTION_hda:
2716 case QEMU_OPTION_hdb:
2717 case QEMU_OPTION_hdc:
2718 case QEMU_OPTION_hdd:
2719 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2720 HD_OPTS);
2721 break;
2722 case QEMU_OPTION_blockdev:
2724 Visitor *v;
2725 BlockdevOptionsQueueEntry *bdo;
2727 v = qobject_input_visitor_new_str(optarg, "driver",
2728 &error_fatal);
2730 bdo = g_new(BlockdevOptionsQueueEntry, 1);
2731 visit_type_BlockdevOptions(v, NULL, &bdo->bdo,
2732 &error_fatal);
2733 visit_free(v);
2734 loc_save(&bdo->loc);
2735 QSIMPLEQ_INSERT_TAIL(&bdo_queue, bdo, entry);
2736 break;
2738 case QEMU_OPTION_drive:
2739 if (drive_def(optarg) == NULL) {
2740 exit(1);
2742 break;
2743 case QEMU_OPTION_set:
2744 if (qemu_set_option(optarg) != 0)
2745 exit(1);
2746 break;
2747 case QEMU_OPTION_global:
2748 if (qemu_global_option(optarg) != 0)
2749 exit(1);
2750 break;
2751 case QEMU_OPTION_mtdblock:
2752 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2753 break;
2754 case QEMU_OPTION_sd:
2755 drive_add(IF_SD, -1, optarg, SD_OPTS);
2756 break;
2757 case QEMU_OPTION_pflash:
2758 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2759 break;
2760 case QEMU_OPTION_snapshot:
2762 Error *blocker = NULL;
2763 snapshot = 1;
2764 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
2765 "-snapshot");
2766 replay_add_blocker(blocker);
2768 break;
2769 case QEMU_OPTION_numa:
2770 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
2771 optarg, true);
2772 if (!opts) {
2773 exit(1);
2775 break;
2776 case QEMU_OPTION_display:
2777 parse_display(optarg);
2778 break;
2779 case QEMU_OPTION_nographic:
2780 olist = qemu_find_opts("machine");
2781 qemu_opts_parse_noisily(olist, "graphics=off", false);
2782 nographic = true;
2783 dpy.type = DISPLAY_TYPE_NONE;
2784 break;
2785 case QEMU_OPTION_curses:
2786 #ifdef CONFIG_CURSES
2787 dpy.type = DISPLAY_TYPE_CURSES;
2788 #else
2789 error_report("curses or iconv support is disabled");
2790 exit(1);
2791 #endif
2792 break;
2793 case QEMU_OPTION_portrait:
2794 graphic_rotate = 90;
2795 break;
2796 case QEMU_OPTION_rotate:
2797 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2798 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2799 graphic_rotate != 180 && graphic_rotate != 270) {
2800 error_report("only 90, 180, 270 deg rotation is available");
2801 exit(1);
2803 break;
2804 case QEMU_OPTION_kernel:
2805 qemu_opts_set(qemu_find_opts("machine"), "kernel", optarg, &error_abort);
2806 break;
2807 case QEMU_OPTION_initrd:
2808 qemu_opts_set(qemu_find_opts("machine"), "initrd", optarg, &error_abort);
2809 break;
2810 case QEMU_OPTION_append:
2811 qemu_opts_set(qemu_find_opts("machine"), "append", optarg, &error_abort);
2812 break;
2813 case QEMU_OPTION_dtb:
2814 qemu_opts_set(qemu_find_opts("machine"), "dtb", optarg, &error_abort);
2815 break;
2816 case QEMU_OPTION_cdrom:
2817 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
2818 break;
2819 case QEMU_OPTION_boot:
2820 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
2821 optarg, true);
2822 if (!opts) {
2823 exit(1);
2825 break;
2826 case QEMU_OPTION_fda:
2827 case QEMU_OPTION_fdb:
2828 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2829 optarg, FD_OPTS);
2830 break;
2831 case QEMU_OPTION_no_fd_bootchk:
2832 fd_bootchk = 0;
2833 break;
2834 case QEMU_OPTION_netdev:
2835 default_net = 0;
2836 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2837 exit(1);
2839 break;
2840 case QEMU_OPTION_nic:
2841 default_net = 0;
2842 if (net_client_parse(qemu_find_opts("nic"), optarg) == -1) {
2843 exit(1);
2845 break;
2846 case QEMU_OPTION_net:
2847 default_net = 0;
2848 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2849 exit(1);
2851 break;
2852 #ifdef CONFIG_LIBISCSI
2853 case QEMU_OPTION_iscsi:
2854 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
2855 optarg, false);
2856 if (!opts) {
2857 exit(1);
2859 break;
2860 #endif
2861 case QEMU_OPTION_audio_help:
2862 audio_legacy_help();
2863 exit (0);
2864 break;
2865 case QEMU_OPTION_audiodev:
2866 audio_parse_option(optarg);
2867 break;
2868 case QEMU_OPTION_soundhw:
2869 select_soundhw (optarg);
2870 break;
2871 case QEMU_OPTION_h:
2872 help(0);
2873 break;
2874 case QEMU_OPTION_version:
2875 version();
2876 exit(0);
2877 break;
2878 case QEMU_OPTION_m:
2879 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
2880 optarg, true);
2881 if (!opts) {
2882 exit(EXIT_FAILURE);
2884 break;
2885 #ifdef CONFIG_TPM
2886 case QEMU_OPTION_tpmdev:
2887 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
2888 exit(1);
2890 break;
2891 #endif
2892 case QEMU_OPTION_mempath:
2893 mem_path = optarg;
2894 break;
2895 case QEMU_OPTION_mem_prealloc:
2896 mem_prealloc = 1;
2897 break;
2898 case QEMU_OPTION_d:
2899 log_mask = optarg;
2900 break;
2901 case QEMU_OPTION_D:
2902 log_file = optarg;
2903 break;
2904 case QEMU_OPTION_DFILTER:
2905 qemu_set_dfilter_ranges(optarg, &error_fatal);
2906 break;
2907 case QEMU_OPTION_seed:
2908 qemu_guest_random_seed_main(optarg, &error_fatal);
2909 break;
2910 case QEMU_OPTION_s:
2911 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
2912 break;
2913 case QEMU_OPTION_gdb:
2914 add_device_config(DEV_GDB, optarg);
2915 break;
2916 case QEMU_OPTION_L:
2917 if (is_help_option(optarg)) {
2918 list_data_dirs = true;
2919 } else {
2920 qemu_add_data_dir(g_strdup(optarg));
2922 break;
2923 case QEMU_OPTION_bios:
2924 qemu_opts_set(qemu_find_opts("machine"), "firmware", optarg, &error_abort);
2925 break;
2926 case QEMU_OPTION_singlestep:
2927 singlestep = 1;
2928 break;
2929 case QEMU_OPTION_S:
2930 autostart = 0;
2931 break;
2932 case QEMU_OPTION_k:
2933 keyboard_layout = optarg;
2934 break;
2935 case QEMU_OPTION_vga:
2936 vga_model = optarg;
2937 default_vga = 0;
2938 break;
2939 case QEMU_OPTION_g:
2941 const char *p;
2942 int w, h, depth;
2943 p = optarg;
2944 w = strtol(p, (char **)&p, 10);
2945 if (w <= 0) {
2946 graphic_error:
2947 error_report("invalid resolution or depth");
2948 exit(1);
2950 if (*p != 'x')
2951 goto graphic_error;
2952 p++;
2953 h = strtol(p, (char **)&p, 10);
2954 if (h <= 0)
2955 goto graphic_error;
2956 if (*p == 'x') {
2957 p++;
2958 depth = strtol(p, (char **)&p, 10);
2959 if (depth != 1 && depth != 2 && depth != 4 &&
2960 depth != 8 && depth != 15 && depth != 16 &&
2961 depth != 24 && depth != 32)
2962 goto graphic_error;
2963 } else if (*p == '\0') {
2964 depth = graphic_depth;
2965 } else {
2966 goto graphic_error;
2969 graphic_width = w;
2970 graphic_height = h;
2971 graphic_depth = depth;
2973 break;
2974 case QEMU_OPTION_echr:
2976 char *r;
2977 term_escape_char = strtol(optarg, &r, 0);
2978 if (r == optarg)
2979 printf("Bad argument to echr\n");
2980 break;
2982 case QEMU_OPTION_monitor:
2983 default_monitor = 0;
2984 if (strncmp(optarg, "none", 4)) {
2985 monitor_parse(optarg, "readline", false);
2987 break;
2988 case QEMU_OPTION_qmp:
2989 monitor_parse(optarg, "control", false);
2990 default_monitor = 0;
2991 break;
2992 case QEMU_OPTION_qmp_pretty:
2993 monitor_parse(optarg, "control", true);
2994 default_monitor = 0;
2995 break;
2996 case QEMU_OPTION_mon:
2997 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
2998 true);
2999 if (!opts) {
3000 exit(1);
3002 default_monitor = 0;
3003 break;
3004 case QEMU_OPTION_chardev:
3005 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3006 optarg, true);
3007 if (!opts) {
3008 exit(1);
3010 break;
3011 case QEMU_OPTION_fsdev:
3012 olist = qemu_find_opts("fsdev");
3013 if (!olist) {
3014 error_report("fsdev support is disabled");
3015 exit(1);
3017 opts = qemu_opts_parse_noisily(olist, optarg, true);
3018 if (!opts) {
3019 exit(1);
3021 break;
3022 case QEMU_OPTION_virtfs: {
3023 QemuOpts *fsdev;
3024 QemuOpts *device;
3025 const char *writeout, *sock_fd, *socket, *path, *security_model,
3026 *multidevs;
3028 olist = qemu_find_opts("virtfs");
3029 if (!olist) {
3030 error_report("virtfs support is disabled");
3031 exit(1);
3033 opts = qemu_opts_parse_noisily(olist, optarg, true);
3034 if (!opts) {
3035 exit(1);
3038 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3039 qemu_opt_get(opts, "mount_tag") == NULL) {
3040 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3041 exit(1);
3043 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3044 qemu_opts_id(opts) ?:
3045 qemu_opt_get(opts, "mount_tag"),
3046 1, NULL);
3047 if (!fsdev) {
3048 error_report("duplicate or invalid fsdev id: %s",
3049 qemu_opt_get(opts, "mount_tag"));
3050 exit(1);
3053 writeout = qemu_opt_get(opts, "writeout");
3054 if (writeout) {
3055 #ifdef CONFIG_SYNC_FILE_RANGE
3056 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3057 #else
3058 error_report("writeout=immediate not supported "
3059 "on this platform");
3060 exit(1);
3061 #endif
3063 qemu_opt_set(fsdev, "fsdriver",
3064 qemu_opt_get(opts, "fsdriver"), &error_abort);
3065 path = qemu_opt_get(opts, "path");
3066 if (path) {
3067 qemu_opt_set(fsdev, "path", path, &error_abort);
3069 security_model = qemu_opt_get(opts, "security_model");
3070 if (security_model) {
3071 qemu_opt_set(fsdev, "security_model", security_model,
3072 &error_abort);
3074 socket = qemu_opt_get(opts, "socket");
3075 if (socket) {
3076 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3078 sock_fd = qemu_opt_get(opts, "sock_fd");
3079 if (sock_fd) {
3080 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3083 qemu_opt_set_bool(fsdev, "readonly",
3084 qemu_opt_get_bool(opts, "readonly", 0),
3085 &error_abort);
3086 multidevs = qemu_opt_get(opts, "multidevs");
3087 if (multidevs) {
3088 qemu_opt_set(fsdev, "multidevs", multidevs, &error_abort);
3090 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3091 &error_abort);
3092 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3093 qemu_opt_set(device, "fsdev",
3094 qemu_opts_id(fsdev), &error_abort);
3095 qemu_opt_set(device, "mount_tag",
3096 qemu_opt_get(opts, "mount_tag"), &error_abort);
3097 break;
3099 case QEMU_OPTION_serial:
3100 add_device_config(DEV_SERIAL, optarg);
3101 default_serial = 0;
3102 if (strncmp(optarg, "mon:", 4) == 0) {
3103 default_monitor = 0;
3105 break;
3106 case QEMU_OPTION_watchdog:
3107 if (watchdog) {
3108 error_report("only one watchdog option may be given");
3109 exit(1);
3111 watchdog = optarg;
3112 break;
3113 case QEMU_OPTION_action:
3114 olist = qemu_find_opts("action");
3115 if (!qemu_opts_parse_noisily(olist, optarg, false)) {
3116 exit(1);
3118 break;
3119 case QEMU_OPTION_watchdog_action:
3120 if (select_watchdog_action(optarg) == -1) {
3121 error_report("unknown -watchdog-action parameter");
3122 exit(1);
3124 break;
3125 case QEMU_OPTION_parallel:
3126 add_device_config(DEV_PARALLEL, optarg);
3127 default_parallel = 0;
3128 if (strncmp(optarg, "mon:", 4) == 0) {
3129 default_monitor = 0;
3131 break;
3132 case QEMU_OPTION_debugcon:
3133 add_device_config(DEV_DEBUGCON, optarg);
3134 break;
3135 case QEMU_OPTION_loadvm:
3136 loadvm = optarg;
3137 break;
3138 case QEMU_OPTION_full_screen:
3139 dpy.has_full_screen = true;
3140 dpy.full_screen = true;
3141 break;
3142 case QEMU_OPTION_alt_grab:
3143 alt_grab = 1;
3144 break;
3145 case QEMU_OPTION_ctrl_grab:
3146 ctrl_grab = 1;
3147 break;
3148 case QEMU_OPTION_no_quit:
3149 dpy.has_window_close = true;
3150 dpy.window_close = false;
3151 break;
3152 case QEMU_OPTION_sdl:
3153 #ifdef CONFIG_SDL
3154 dpy.type = DISPLAY_TYPE_SDL;
3155 break;
3156 #else
3157 error_report("SDL support is disabled");
3158 exit(1);
3159 #endif
3160 case QEMU_OPTION_pidfile:
3161 pid_file = optarg;
3162 break;
3163 case QEMU_OPTION_win2k_hack:
3164 win2k_install_hack = 1;
3165 break;
3166 case QEMU_OPTION_acpitable:
3167 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3168 optarg, true);
3169 if (!opts) {
3170 exit(1);
3172 acpi_table_add(opts, &error_fatal);
3173 break;
3174 case QEMU_OPTION_smbios:
3175 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3176 optarg, false);
3177 if (!opts) {
3178 exit(1);
3180 smbios_entry_add(opts, &error_fatal);
3181 break;
3182 case QEMU_OPTION_fwcfg:
3183 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3184 optarg, true);
3185 if (opts == NULL) {
3186 exit(1);
3188 break;
3189 case QEMU_OPTION_preconfig:
3190 preconfig_requested = true;
3191 break;
3192 case QEMU_OPTION_enable_kvm:
3193 olist = qemu_find_opts("machine");
3194 qemu_opts_parse_noisily(olist, "accel=kvm", false);
3195 break;
3196 case QEMU_OPTION_M:
3197 case QEMU_OPTION_machine:
3198 olist = qemu_find_opts("machine");
3199 opts = qemu_opts_parse_noisily(olist, optarg, true);
3200 if (!opts) {
3201 exit(1);
3203 break;
3204 case QEMU_OPTION_accel:
3205 accel_opts = qemu_opts_parse_noisily(qemu_find_opts("accel"),
3206 optarg, true);
3207 optarg = qemu_opt_get(accel_opts, "accel");
3208 if (!optarg || is_help_option(optarg)) {
3209 printf("Accelerators supported in QEMU binary:\n");
3210 GSList *el, *accel_list = object_class_get_list(TYPE_ACCEL,
3211 false);
3212 for (el = accel_list; el; el = el->next) {
3213 gchar *typename = g_strdup(object_class_get_name(
3214 OBJECT_CLASS(el->data)));
3215 /* omit qtest which is used for tests only */
3216 if (g_strcmp0(typename, ACCEL_CLASS_NAME("qtest")) &&
3217 g_str_has_suffix(typename, ACCEL_CLASS_SUFFIX)) {
3218 gchar **optname = g_strsplit(typename,
3219 ACCEL_CLASS_SUFFIX, 0);
3220 printf("%s\n", optname[0]);
3221 g_strfreev(optname);
3223 g_free(typename);
3225 g_slist_free(accel_list);
3226 exit(0);
3228 break;
3229 case QEMU_OPTION_usb:
3230 olist = qemu_find_opts("machine");
3231 qemu_opts_parse_noisily(olist, "usb=on", false);
3232 break;
3233 case QEMU_OPTION_usbdevice:
3234 olist = qemu_find_opts("machine");
3235 qemu_opts_parse_noisily(olist, "usb=on", false);
3236 add_device_config(DEV_USB, optarg);
3237 break;
3238 case QEMU_OPTION_device:
3239 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3240 optarg, true)) {
3241 exit(1);
3243 break;
3244 case QEMU_OPTION_smp:
3245 if (!qemu_opts_parse_noisily(qemu_find_opts("smp-opts"),
3246 optarg, true)) {
3247 exit(1);
3249 break;
3250 case QEMU_OPTION_vnc:
3251 vnc_parse(optarg);
3252 break;
3253 case QEMU_OPTION_no_acpi:
3254 olist = qemu_find_opts("machine");
3255 qemu_opts_parse_noisily(olist, "acpi=off", false);
3256 break;
3257 case QEMU_OPTION_no_hpet:
3258 olist = qemu_find_opts("machine");
3259 qemu_opts_parse_noisily(olist, "hpet=off", false);
3260 break;
3261 case QEMU_OPTION_no_reboot:
3262 olist = qemu_find_opts("action");
3263 qemu_opts_parse_noisily(olist, "reboot=shutdown", false);
3264 break;
3265 case QEMU_OPTION_no_shutdown:
3266 olist = qemu_find_opts("action");
3267 qemu_opts_parse_noisily(olist, "shutdown=pause", false);
3268 break;
3269 case QEMU_OPTION_uuid:
3270 if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
3271 error_report("failed to parse UUID string: wrong format");
3272 exit(1);
3274 qemu_uuid_set = true;
3275 break;
3276 case QEMU_OPTION_option_rom:
3277 if (nb_option_roms >= MAX_OPTION_ROMS) {
3278 error_report("too many option ROMs");
3279 exit(1);
3281 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3282 optarg, true);
3283 if (!opts) {
3284 exit(1);
3286 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3287 option_rom[nb_option_roms].bootindex =
3288 qemu_opt_get_number(opts, "bootindex", -1);
3289 if (!option_rom[nb_option_roms].name) {
3290 error_report("Option ROM file is not specified");
3291 exit(1);
3293 nb_option_roms++;
3294 break;
3295 case QEMU_OPTION_semihosting:
3296 qemu_semihosting_enable();
3297 break;
3298 case QEMU_OPTION_semihosting_config:
3299 if (qemu_semihosting_config_options(optarg) != 0) {
3300 exit(1);
3302 break;
3303 case QEMU_OPTION_name:
3304 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3305 optarg, true);
3306 if (!opts) {
3307 exit(1);
3309 /* Capture guest name if -msg guest-name is used later */
3310 error_guest_name = qemu_opt_get(opts, "guest");
3311 break;
3312 case QEMU_OPTION_prom_env:
3313 if (nb_prom_envs >= MAX_PROM_ENVS) {
3314 error_report("too many prom variables");
3315 exit(1);
3317 prom_envs[nb_prom_envs] = optarg;
3318 nb_prom_envs++;
3319 break;
3320 case QEMU_OPTION_old_param:
3321 old_param = 1;
3322 break;
3323 case QEMU_OPTION_rtc:
3324 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3325 false);
3326 if (!opts) {
3327 exit(1);
3329 break;
3330 case QEMU_OPTION_icount:
3331 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3332 optarg, true);
3333 if (!icount_opts) {
3334 exit(1);
3336 break;
3337 case QEMU_OPTION_incoming:
3338 if (!incoming) {
3339 runstate_set(RUN_STATE_INMIGRATE);
3341 incoming = optarg;
3342 break;
3343 case QEMU_OPTION_only_migratable:
3344 only_migratable = 1;
3345 break;
3346 case QEMU_OPTION_nodefaults:
3347 has_defaults = 0;
3348 break;
3349 case QEMU_OPTION_xen_domid:
3350 if (!(xen_available())) {
3351 error_report("Option not supported for this target");
3352 exit(1);
3354 xen_domid = atoi(optarg);
3355 break;
3356 case QEMU_OPTION_xen_attach:
3357 if (!(xen_available())) {
3358 error_report("Option not supported for this target");
3359 exit(1);
3361 xen_mode = XEN_ATTACH;
3362 break;
3363 case QEMU_OPTION_xen_domid_restrict:
3364 if (!(xen_available())) {
3365 error_report("Option not supported for this target");
3366 exit(1);
3368 xen_domid_restrict = true;
3369 break;
3370 case QEMU_OPTION_trace:
3371 trace_opt_parse(optarg);
3372 break;
3373 case QEMU_OPTION_plugin:
3374 qemu_plugin_opt_parse(optarg, &plugin_list);
3375 break;
3376 case QEMU_OPTION_readconfig:
3377 qemu_read_config_file(optarg, &error_fatal);
3378 break;
3379 case QEMU_OPTION_spice:
3380 olist = qemu_find_opts_err("spice", NULL);
3381 if (!olist) {
3382 ui_module_load_one("spice-core");
3383 olist = qemu_find_opts("spice");
3385 if (!olist) {
3386 error_report("spice support is disabled");
3387 exit(1);
3389 opts = qemu_opts_parse_noisily(olist, optarg, false);
3390 if (!opts) {
3391 exit(1);
3393 display_remote++;
3394 break;
3395 case QEMU_OPTION_writeconfig:
3397 FILE *fp;
3398 warn_report("-writeconfig is deprecated and will go away without a replacement");
3399 if (strcmp(optarg, "-") == 0) {
3400 fp = stdout;
3401 } else {
3402 fp = fopen(optarg, "w");
3403 if (fp == NULL) {
3404 error_report("open %s: %s", optarg,
3405 strerror(errno));
3406 exit(1);
3409 qemu_config_write(fp);
3410 if (fp != stdout) {
3411 fclose(fp);
3413 break;
3415 case QEMU_OPTION_qtest:
3416 qtest_chrdev = optarg;
3417 break;
3418 case QEMU_OPTION_qtest_log:
3419 qtest_log = optarg;
3420 break;
3421 case QEMU_OPTION_sandbox:
3422 olist = qemu_find_opts("sandbox");
3423 if (!olist) {
3424 #ifndef CONFIG_SECCOMP
3425 error_report("-sandbox support is not enabled "
3426 "in this QEMU binary");
3427 #endif
3428 exit(1);
3431 opts = qemu_opts_parse_noisily(olist, optarg, true);
3432 if (!opts) {
3433 exit(1);
3435 break;
3436 case QEMU_OPTION_add_fd:
3437 #ifndef _WIN32
3438 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
3439 optarg, false);
3440 if (!opts) {
3441 exit(1);
3443 #else
3444 error_report("File descriptor passing is disabled on this "
3445 "platform");
3446 exit(1);
3447 #endif
3448 break;
3449 case QEMU_OPTION_object:
3450 object_option_parse(optarg);
3451 break;
3452 case QEMU_OPTION_overcommit:
3453 opts = qemu_opts_parse_noisily(qemu_find_opts("overcommit"),
3454 optarg, false);
3455 if (!opts) {
3456 exit(1);
3458 enable_mlock = qemu_opt_get_bool(opts, "mem-lock", false);
3459 enable_cpu_pm = qemu_opt_get_bool(opts, "cpu-pm", false);
3460 break;
3461 case QEMU_OPTION_msg:
3462 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
3463 false);
3464 if (!opts) {
3465 exit(1);
3467 configure_msg(opts);
3468 break;
3469 case QEMU_OPTION_dump_vmstate:
3470 if (vmstate_dump_file) {
3471 error_report("only one '-dump-vmstate' "
3472 "option may be given");
3473 exit(1);
3475 vmstate_dump_file = fopen(optarg, "w");
3476 if (vmstate_dump_file == NULL) {
3477 error_report("open %s: %s", optarg, strerror(errno));
3478 exit(1);
3480 break;
3481 case QEMU_OPTION_enable_sync_profile:
3482 qsp_enable();
3483 break;
3484 case QEMU_OPTION_nouserconfig:
3485 /* Nothing to be parsed here. Especially, do not error out below. */
3486 break;
3487 default:
3488 if (os_parse_cmd_args(popt->index, optarg)) {
3489 error_report("Option not supported in this build");
3490 exit(1);
3496 * Clear error location left behind by the loop.
3497 * Best done right after the loop. Do not insert code here!
3499 loc_set_none();
3501 qemu_validate_options();
3502 qemu_process_sugar_options();
3505 * These options affect everything else and should be processed
3506 * before daemonizing.
3508 qemu_process_early_options();
3510 qemu_process_help_options();
3511 qemu_maybe_daemonize(pid_file);
3514 * The trace backend must be initialized after daemonizing.
3515 * trace_init_backends() will call st_init(), which will create the
3516 * trace thread in the parent, and also register st_flush_trace_buffer()
3517 * in atexit(). This function will force the parent to wait for the
3518 * writeout thread to finish, which will not occur, and the parent
3519 * process will be left in the host.
3521 if (!trace_init_backends()) {
3522 exit(1);
3524 trace_init_file();
3526 qemu_init_main_loop(&error_fatal);
3527 cpu_timers_init();
3529 user_register_global_props();
3530 replay_configure(icount_opts);
3532 configure_rtc(qemu_find_opts_singleton("rtc"));
3534 qemu_create_machine(select_machine());
3536 suspend_mux_open();
3538 qemu_disable_default_devices();
3539 qemu_create_default_devices();
3540 qemu_create_early_backends();
3542 qemu_apply_machine_options();
3543 phase_advance(PHASE_MACHINE_CREATED);
3546 * Note: uses machine properties such as kernel-irqchip, must run
3547 * after machine_set_property().
3549 configure_accelerators(argv[0]);
3550 phase_advance(PHASE_ACCEL_CREATED);
3553 * Beware, QOM objects created before this point miss global and
3554 * compat properties.
3556 * Global properties get set up by qdev_prop_register_global(),
3557 * called from user_register_global_props(), and certain option
3558 * desugaring. Also in CPU feature desugaring (buried in
3559 * parse_cpu_option()), which happens below this point, but may
3560 * only target the CPU type, which can only be created after
3561 * parse_cpu_option() returned the type.
3563 * Machine compat properties: object_set_machine_compat_props().
3564 * Accelerator compat props: object_set_accelerator_compat_props(),
3565 * called from do_configure_accelerator().
3568 machine_class = MACHINE_GET_CLASS(current_machine);
3569 if (!qtest_enabled() && machine_class->deprecation_reason) {
3570 error_report("Machine type '%s' is deprecated: %s",
3571 machine_class->name, machine_class->deprecation_reason);
3575 * Note: creates a QOM object, must run only after global and
3576 * compat properties have been set up.
3578 migration_object_init();
3580 qemu_create_late_backends();
3582 /* parse features once if machine provides default cpu_type */
3583 current_machine->cpu_type = machine_class->default_cpu_type;
3584 if (cpu_option) {
3585 current_machine->cpu_type = parse_cpu_option(cpu_option);
3587 /* NB: for machine none cpu_type could STILL be NULL here! */
3588 accel_init_interfaces(ACCEL_GET_CLASS(current_machine->accelerator));
3590 qemu_resolve_machine_memdev();
3591 parse_numa_opts(current_machine);
3593 if (vmstate_dump_file) {
3594 /* dump and exit */
3595 dump_vmstate_json_to_file(vmstate_dump_file);
3596 exit(0);
3599 if (!preconfig_requested) {
3600 qmp_x_exit_preconfig(&error_fatal);
3602 qemu_init_displays();
3603 accel_setup_post(current_machine);
3604 os_setup_post();
3605 resume_mux_open();