iotests/264: add mirror-cancel test-case
[qemu/ar7.git] / softmmu / vl.c
blobb219ce1f357ed2fb20da29898644cdc01de48829
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 "qemu-version.h"
35 #include "qemu/cutils.h"
36 #include "qemu/help_option.h"
37 #include "qemu/uuid.h"
38 #include "sysemu/reset.h"
39 #include "sysemu/runstate.h"
40 #include "sysemu/runstate-action.h"
41 #include "sysemu/seccomp.h"
42 #include "sysemu/tcg.h"
43 #include "sysemu/xen.h"
45 #include "qemu/error-report.h"
46 #include "qemu/sockets.h"
47 #include "qemu/accel.h"
48 #include "hw/usb.h"
49 #include "hw/isa/isa.h"
50 #include "hw/scsi/scsi.h"
51 #include "hw/display/vga.h"
52 #include "sysemu/watchdog.h"
53 #include "hw/firmware/smbios.h"
54 #include "hw/acpi/acpi.h"
55 #include "hw/xen/xen.h"
56 #include "hw/loader.h"
57 #include "monitor/qdev.h"
58 #include "net/net.h"
59 #include "net/slirp.h"
60 #include "monitor/monitor.h"
61 #include "ui/console.h"
62 #include "ui/input.h"
63 #include "sysemu/sysemu.h"
64 #include "sysemu/numa.h"
65 #include "sysemu/hostmem.h"
66 #include "exec/gdbstub.h"
67 #include "qemu/timer.h"
68 #include "chardev/char.h"
69 #include "qemu/bitmap.h"
70 #include "qemu/log.h"
71 #include "sysemu/blockdev.h"
72 #include "hw/block/block.h"
73 #include "hw/i386/x86.h"
74 #include "hw/i386/pc.h"
75 #include "migration/misc.h"
76 #include "migration/snapshot.h"
77 #include "sysemu/tpm.h"
78 #include "sysemu/dma.h"
79 #include "hw/audio/soundhw.h"
80 #include "audio/audio.h"
81 #include "sysemu/cpus.h"
82 #include "sysemu/cpu-timers.h"
83 #include "migration/colo.h"
84 #include "migration/postcopy-ram.h"
85 #include "sysemu/kvm.h"
86 #include "sysemu/hax.h"
87 #include "qapi/qobject-input-visitor.h"
88 #include "qemu/option.h"
89 #include "qemu/config-file.h"
90 #include "qemu-options.h"
91 #include "qemu/main-loop.h"
92 #ifdef CONFIG_VIRTFS
93 #include "fsdev/qemu-fsdev.h"
94 #endif
95 #include "sysemu/qtest.h"
97 #include "disas/disas.h"
99 #include "trace.h"
100 #include "trace/control.h"
101 #include "qemu/plugin.h"
102 #include "qemu/queue.h"
103 #include "sysemu/arch_init.h"
104 #include "exec/confidential-guest-support.h"
106 #include "ui/qemu-spice.h"
107 #include "qapi/string-input-visitor.h"
108 #include "qapi/opts-visitor.h"
109 #include "qapi/clone-visitor.h"
110 #include "qom/object_interfaces.h"
111 #include "hw/semihosting/semihost.h"
112 #include "crypto/init.h"
113 #include "sysemu/replay.h"
114 #include "qapi/qapi-events-run-state.h"
115 #include "qapi/qapi-visit-block-core.h"
116 #include "qapi/qapi-visit-ui.h"
117 #include "qapi/qapi-commands-block-core.h"
118 #include "qapi/qapi-commands-migration.h"
119 #include "qapi/qapi-commands-misc.h"
120 #include "qapi/qapi-commands-ui.h"
121 #include "qapi/qmp/qerror.h"
122 #include "sysemu/iothread.h"
123 #include "qemu/guest-random.h"
125 #define MAX_VIRTIO_CONSOLES 1
127 typedef struct BlockdevOptionsQueueEntry {
128 BlockdevOptions *bdo;
129 Location loc;
130 QSIMPLEQ_ENTRY(BlockdevOptionsQueueEntry) entry;
131 } BlockdevOptionsQueueEntry;
133 typedef QSIMPLEQ_HEAD(, BlockdevOptionsQueueEntry) BlockdevOptionsQueue;
135 static const char *cpu_option;
136 static const char *mem_path;
137 static const char *incoming;
138 static const char *loadvm;
139 static ram_addr_t maxram_size;
140 static uint64_t ram_slots;
141 static int display_remote;
142 static int snapshot;
143 static bool preconfig_requested;
144 static QemuPluginList plugin_list = QTAILQ_HEAD_INITIALIZER(plugin_list);
145 static BlockdevOptionsQueue bdo_queue = QSIMPLEQ_HEAD_INITIALIZER(bdo_queue);
146 static bool nographic = false;
147 static int mem_prealloc; /* force preallocation of physical target memory */
148 static ram_addr_t ram_size;
149 static const char *vga_model = NULL;
150 static DisplayOptions dpy;
151 static int num_serial_hds;
152 static Chardev **serial_hds;
153 static const char *log_mask;
154 static const char *log_file;
155 static bool list_data_dirs;
156 static const char *watchdog;
157 static const char *qtest_chrdev;
158 static const char *qtest_log;
160 static int has_defaults = 1;
161 static int default_serial = 1;
162 static int default_parallel = 1;
163 static int default_monitor = 1;
164 static int default_floppy = 1;
165 static int default_cdrom = 1;
166 static int default_sdcard = 1;
167 static int default_vga = 1;
168 static int default_net = 1;
170 static struct {
171 const char *driver;
172 int *flag;
173 } default_list[] = {
174 { .driver = "isa-serial", .flag = &default_serial },
175 { .driver = "isa-parallel", .flag = &default_parallel },
176 { .driver = "isa-fdc", .flag = &default_floppy },
177 { .driver = "floppy", .flag = &default_floppy },
178 { .driver = "ide-cd", .flag = &default_cdrom },
179 { .driver = "ide-hd", .flag = &default_cdrom },
180 { .driver = "ide-drive", .flag = &default_cdrom },
181 { .driver = "scsi-cd", .flag = &default_cdrom },
182 { .driver = "scsi-hd", .flag = &default_cdrom },
183 { .driver = "VGA", .flag = &default_vga },
184 { .driver = "isa-vga", .flag = &default_vga },
185 { .driver = "cirrus-vga", .flag = &default_vga },
186 { .driver = "isa-cirrus-vga", .flag = &default_vga },
187 { .driver = "vmware-svga", .flag = &default_vga },
188 { .driver = "qxl-vga", .flag = &default_vga },
189 { .driver = "virtio-vga", .flag = &default_vga },
190 { .driver = "ati-vga", .flag = &default_vga },
191 { .driver = "vhost-user-vga", .flag = &default_vga },
194 static QemuOptsList qemu_rtc_opts = {
195 .name = "rtc",
196 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
197 .merge_lists = true,
198 .desc = {
200 .name = "base",
201 .type = QEMU_OPT_STRING,
203 .name = "clock",
204 .type = QEMU_OPT_STRING,
206 .name = "driftfix",
207 .type = QEMU_OPT_STRING,
209 { /* end of list */ }
213 static QemuOptsList qemu_option_rom_opts = {
214 .name = "option-rom",
215 .implied_opt_name = "romfile",
216 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
217 .desc = {
219 .name = "bootindex",
220 .type = QEMU_OPT_NUMBER,
221 }, {
222 .name = "romfile",
223 .type = QEMU_OPT_STRING,
225 { /* end of list */ }
229 static QemuOptsList qemu_machine_opts = {
230 .name = "machine",
231 .implied_opt_name = "type",
232 .merge_lists = true,
233 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
234 .desc = {
236 * no elements => accept any
237 * sanity checking will happen later
238 * when setting machine properties
244 static QemuOptsList qemu_accel_opts = {
245 .name = "accel",
246 .implied_opt_name = "accel",
247 .head = QTAILQ_HEAD_INITIALIZER(qemu_accel_opts.head),
248 .desc = {
250 * no elements => accept any
251 * sanity checking will happen later
252 * when setting accelerator properties
258 static QemuOptsList qemu_boot_opts = {
259 .name = "boot-opts",
260 .implied_opt_name = "order",
261 .merge_lists = true,
262 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
263 .desc = {
265 .name = "order",
266 .type = QEMU_OPT_STRING,
267 }, {
268 .name = "once",
269 .type = QEMU_OPT_STRING,
270 }, {
271 .name = "menu",
272 .type = QEMU_OPT_BOOL,
273 }, {
274 .name = "splash",
275 .type = QEMU_OPT_STRING,
276 }, {
277 .name = "splash-time",
278 .type = QEMU_OPT_NUMBER,
279 }, {
280 .name = "reboot-timeout",
281 .type = QEMU_OPT_NUMBER,
282 }, {
283 .name = "strict",
284 .type = QEMU_OPT_BOOL,
286 { /*End of list */ }
290 static QemuOptsList qemu_add_fd_opts = {
291 .name = "add-fd",
292 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
293 .desc = {
295 .name = "fd",
296 .type = QEMU_OPT_NUMBER,
297 .help = "file descriptor of which a duplicate is added to fd set",
299 .name = "set",
300 .type = QEMU_OPT_NUMBER,
301 .help = "ID of the fd set to add fd to",
303 .name = "opaque",
304 .type = QEMU_OPT_STRING,
305 .help = "free-form string used to describe fd",
307 { /* end of list */ }
311 static QemuOptsList qemu_object_opts = {
312 .name = "object",
313 .implied_opt_name = "qom-type",
314 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
315 .desc = {
320 static QemuOptsList qemu_tpmdev_opts = {
321 .name = "tpmdev",
322 .implied_opt_name = "type",
323 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
324 .desc = {
325 /* options are defined in the TPM backends */
326 { /* end of list */ }
330 static QemuOptsList qemu_overcommit_opts = {
331 .name = "overcommit",
332 .head = QTAILQ_HEAD_INITIALIZER(qemu_overcommit_opts.head),
333 .desc = {
335 .name = "mem-lock",
336 .type = QEMU_OPT_BOOL,
339 .name = "cpu-pm",
340 .type = QEMU_OPT_BOOL,
342 { /* end of list */ }
346 static QemuOptsList qemu_msg_opts = {
347 .name = "msg",
348 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
349 .desc = {
351 .name = "timestamp",
352 .type = QEMU_OPT_BOOL,
355 .name = "guest-name",
356 .type = QEMU_OPT_BOOL,
357 .help = "Prepends guest name for error messages but only if "
358 "-name guest is set otherwise option is ignored\n",
360 { /* end of list */ }
364 static QemuOptsList qemu_name_opts = {
365 .name = "name",
366 .implied_opt_name = "guest",
367 .merge_lists = true,
368 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
369 .desc = {
371 .name = "guest",
372 .type = QEMU_OPT_STRING,
373 .help = "Sets the name of the guest.\n"
374 "This name will be displayed in the SDL window caption.\n"
375 "The name will also be used for the VNC server",
376 }, {
377 .name = "process",
378 .type = QEMU_OPT_STRING,
379 .help = "Sets the name of the QEMU process, as shown in top etc",
380 }, {
381 .name = "debug-threads",
382 .type = QEMU_OPT_BOOL,
383 .help = "When enabled, name the individual threads; defaults off.\n"
384 "NOTE: The thread names are for debugging and not a\n"
385 "stable API.",
387 { /* End of list */ }
391 static QemuOptsList qemu_mem_opts = {
392 .name = "memory",
393 .implied_opt_name = "size",
394 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
395 .merge_lists = true,
396 .desc = {
398 .name = "size",
399 .type = QEMU_OPT_SIZE,
402 .name = "slots",
403 .type = QEMU_OPT_NUMBER,
406 .name = "maxmem",
407 .type = QEMU_OPT_SIZE,
409 { /* end of list */ }
413 static QemuOptsList qemu_icount_opts = {
414 .name = "icount",
415 .implied_opt_name = "shift",
416 .merge_lists = true,
417 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
418 .desc = {
420 .name = "shift",
421 .type = QEMU_OPT_STRING,
422 }, {
423 .name = "align",
424 .type = QEMU_OPT_BOOL,
425 }, {
426 .name = "sleep",
427 .type = QEMU_OPT_BOOL,
428 }, {
429 .name = "rr",
430 .type = QEMU_OPT_STRING,
431 }, {
432 .name = "rrfile",
433 .type = QEMU_OPT_STRING,
434 }, {
435 .name = "rrsnapshot",
436 .type = QEMU_OPT_STRING,
438 { /* end of list */ }
442 static QemuOptsList qemu_fw_cfg_opts = {
443 .name = "fw_cfg",
444 .implied_opt_name = "name",
445 .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
446 .desc = {
448 .name = "name",
449 .type = QEMU_OPT_STRING,
450 .help = "Sets the fw_cfg name of the blob to be inserted",
451 }, {
452 .name = "file",
453 .type = QEMU_OPT_STRING,
454 .help = "Sets the name of the file from which "
455 "the fw_cfg blob will be loaded",
456 }, {
457 .name = "string",
458 .type = QEMU_OPT_STRING,
459 .help = "Sets content of the blob to be inserted from a string",
460 }, {
461 .name = "gen_id",
462 .type = QEMU_OPT_STRING,
463 .help = "Sets id of the object generating the fw_cfg blob "
464 "to be inserted",
466 { /* end of list */ }
470 static QemuOptsList qemu_action_opts = {
471 .name = "action",
472 .merge_lists = true,
473 .head = QTAILQ_HEAD_INITIALIZER(qemu_action_opts.head),
474 .desc = {
476 .name = "shutdown",
477 .type = QEMU_OPT_STRING,
479 .name = "reboot",
480 .type = QEMU_OPT_STRING,
482 .name = "panic",
483 .type = QEMU_OPT_STRING,
485 .name = "watchdog",
486 .type = QEMU_OPT_STRING,
488 { /* end of list */ }
493 * Get machine options
495 * Returns: machine options (never null).
497 static QemuOpts *qemu_get_machine_opts(void)
499 return qemu_find_opts_singleton("machine");
502 const char *qemu_get_vm_name(void)
504 return qemu_name;
507 static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
509 const char *driver = qemu_opt_get(opts, "driver");
510 int i;
512 if (!driver)
513 return 0;
514 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
515 if (strcmp(default_list[i].driver, driver) != 0)
516 continue;
517 *(default_list[i].flag) = 0;
519 return 0;
522 static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
524 const char *proc_name;
526 if (qemu_opt_get(opts, "debug-threads")) {
527 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
529 qemu_name = qemu_opt_get(opts, "guest");
531 proc_name = qemu_opt_get(opts, "process");
532 if (proc_name) {
533 os_set_proc_name(proc_name);
536 return 0;
539 bool defaults_enabled(void)
541 return has_defaults;
544 #ifndef _WIN32
545 static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
547 int fd, dupfd, flags;
548 int64_t fdset_id;
549 const char *fd_opaque = NULL;
550 AddfdInfo *fdinfo;
552 fd = qemu_opt_get_number(opts, "fd", -1);
553 fdset_id = qemu_opt_get_number(opts, "set", -1);
554 fd_opaque = qemu_opt_get(opts, "opaque");
556 if (fd < 0) {
557 error_setg(errp, "fd option is required and must be non-negative");
558 return -1;
561 if (fd <= STDERR_FILENO) {
562 error_setg(errp, "fd cannot be a standard I/O stream");
563 return -1;
567 * All fds inherited across exec() necessarily have FD_CLOEXEC
568 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
570 flags = fcntl(fd, F_GETFD);
571 if (flags == -1 || (flags & FD_CLOEXEC)) {
572 error_setg(errp, "fd is not valid or already in use");
573 return -1;
576 if (fdset_id < 0) {
577 error_setg(errp, "set option is required and must be non-negative");
578 return -1;
581 #ifdef F_DUPFD_CLOEXEC
582 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
583 #else
584 dupfd = dup(fd);
585 if (dupfd != -1) {
586 qemu_set_cloexec(dupfd);
588 #endif
589 if (dupfd == -1) {
590 error_setg(errp, "error duplicating fd: %s", strerror(errno));
591 return -1;
594 /* add the duplicate fd, and optionally the opaque string, to the fd set */
595 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
596 &error_abort);
597 g_free(fdinfo);
599 return 0;
602 static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
604 int fd;
606 fd = qemu_opt_get_number(opts, "fd", -1);
607 close(fd);
609 return 0;
611 #endif
613 /***********************************************************/
614 /* QEMU Block devices */
616 #define HD_OPTS "media=disk"
617 #define CDROM_OPTS "media=cdrom"
618 #define FD_OPTS ""
619 #define PFLASH_OPTS ""
620 #define MTD_OPTS ""
621 #define SD_OPTS ""
623 static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
625 BlockInterfaceType *block_default_type = opaque;
627 return drive_new(opts, *block_default_type, errp) == NULL;
630 static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
632 if (qemu_opt_get(opts, "snapshot") == NULL) {
633 qemu_opt_set(opts, "snapshot", "on", &error_abort);
635 return 0;
638 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
639 int index, const char *optstr)
641 QemuOpts *opts;
642 DriveInfo *dinfo;
644 if (!enable || drive_get_by_index(type, index)) {
645 return;
648 opts = drive_add(type, index, NULL, optstr);
649 if (snapshot) {
650 drive_enable_snapshot(NULL, opts, NULL);
653 dinfo = drive_new(opts, type, &error_abort);
654 dinfo->is_default = true;
658 static void configure_blockdev(BlockdevOptionsQueue *bdo_queue,
659 MachineClass *machine_class, int snapshot)
662 * If the currently selected machine wishes to override the
663 * units-per-bus property of its default HBA interface type, do so
664 * now.
666 if (machine_class->units_per_default_bus) {
667 override_max_devs(machine_class->block_default_type,
668 machine_class->units_per_default_bus);
671 /* open the virtual block devices */
672 while (!QSIMPLEQ_EMPTY(bdo_queue)) {
673 BlockdevOptionsQueueEntry *bdo = QSIMPLEQ_FIRST(bdo_queue);
675 QSIMPLEQ_REMOVE_HEAD(bdo_queue, entry);
676 loc_push_restore(&bdo->loc);
677 qmp_blockdev_add(bdo->bdo, &error_fatal);
678 loc_pop(&bdo->loc);
679 qapi_free_BlockdevOptions(bdo->bdo);
680 g_free(bdo);
682 if (snapshot) {
683 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
684 NULL, NULL);
686 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
687 &machine_class->block_default_type, &error_fatal)) {
688 /* We printed help */
689 exit(0);
692 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
693 CDROM_OPTS);
694 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
695 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
699 static QemuOptsList qemu_smp_opts = {
700 .name = "smp-opts",
701 .implied_opt_name = "cpus",
702 .merge_lists = true,
703 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
704 .desc = {
706 .name = "cpus",
707 .type = QEMU_OPT_NUMBER,
708 }, {
709 .name = "sockets",
710 .type = QEMU_OPT_NUMBER,
711 }, {
712 .name = "dies",
713 .type = QEMU_OPT_NUMBER,
714 }, {
715 .name = "cores",
716 .type = QEMU_OPT_NUMBER,
717 }, {
718 .name = "threads",
719 .type = QEMU_OPT_NUMBER,
720 }, {
721 .name = "maxcpus",
722 .type = QEMU_OPT_NUMBER,
724 { /*End of list */ }
728 static void realtime_init(void)
730 if (enable_mlock) {
731 if (os_mlock() < 0) {
732 error_report("locking memory failed");
733 exit(1);
739 static void configure_msg(QemuOpts *opts)
741 message_with_timestamp = qemu_opt_get_bool(opts, "timestamp", false);
742 error_with_guestname = qemu_opt_get_bool(opts, "guest-name", false);
746 /***********************************************************/
747 /* USB devices */
749 static int usb_device_add(const char *devname)
751 USBDevice *dev = NULL;
753 if (!machine_usb(current_machine)) {
754 return -1;
757 dev = usbdevice_create(devname);
758 if (!dev)
759 return -1;
761 return 0;
764 static int usb_parse(const char *cmdline)
766 int r;
767 r = usb_device_add(cmdline);
768 if (r < 0) {
769 error_report("could not add USB device '%s'", cmdline);
771 return r;
774 /***********************************************************/
775 /* machine registration */
777 static MachineClass *find_machine(const char *name, GSList *machines)
779 GSList *el;
781 for (el = machines; el; el = el->next) {
782 MachineClass *mc = el->data;
784 if (!strcmp(mc->name, name) || !g_strcmp0(mc->alias, name)) {
785 return mc;
789 return NULL;
792 static MachineClass *find_default_machine(GSList *machines)
794 GSList *el;
795 MachineClass *default_machineclass = NULL;
797 for (el = machines; el; el = el->next) {
798 MachineClass *mc = el->data;
800 if (mc->is_default) {
801 assert(default_machineclass == NULL && "Multiple default machines");
802 default_machineclass = mc;
806 return default_machineclass;
809 static int machine_help_func(QemuOpts *opts, MachineState *machine)
811 ObjectProperty *prop;
812 ObjectPropertyIterator iter;
814 if (!qemu_opt_has_help_opt(opts)) {
815 return 0;
818 object_property_iter_init(&iter, OBJECT(machine));
819 while ((prop = object_property_iter_next(&iter))) {
820 if (!prop->set) {
821 continue;
824 printf("%s.%s=%s", MACHINE_GET_CLASS(machine)->name,
825 prop->name, prop->type);
826 if (prop->description) {
827 printf(" (%s)\n", prop->description);
828 } else {
829 printf("\n");
833 return 1;
836 static void version(void)
838 printf("QEMU emulator version " QEMU_FULL_VERSION "\n"
839 QEMU_COPYRIGHT "\n");
842 static void help(int exitcode)
844 version();
845 printf("usage: %s [options] [disk_image]\n\n"
846 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
847 error_get_progname());
849 #define QEMU_OPTIONS_GENERATE_HELP
850 #include "qemu-options-wrapper.h"
852 printf("\nDuring emulation, the following keys are useful:\n"
853 "ctrl-alt-f toggle full screen\n"
854 "ctrl-alt-n switch to virtual console 'n'\n"
855 "ctrl-alt toggle mouse and keyboard grab\n"
856 "\n"
857 "When using -nographic, press 'ctrl-a h' to get some help.\n"
858 "\n"
859 QEMU_HELP_BOTTOM "\n");
861 exit(exitcode);
864 #define HAS_ARG 0x0001
866 typedef struct QEMUOption {
867 const char *name;
868 int flags;
869 int index;
870 uint32_t arch_mask;
871 } QEMUOption;
873 static const QEMUOption qemu_options[] = {
874 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
875 #define QEMU_OPTIONS_GENERATE_OPTIONS
876 #include "qemu-options-wrapper.h"
877 { NULL },
880 typedef struct VGAInterfaceInfo {
881 const char *opt_name; /* option name */
882 const char *name; /* human-readable name */
883 /* Class names indicating that support is available.
884 * If no class is specified, the interface is always available */
885 const char *class_names[2];
886 } VGAInterfaceInfo;
888 static const VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
889 [VGA_NONE] = {
890 .opt_name = "none",
891 .name = "no graphic card",
893 [VGA_STD] = {
894 .opt_name = "std",
895 .name = "standard VGA",
896 .class_names = { "VGA", "isa-vga" },
898 [VGA_CIRRUS] = {
899 .opt_name = "cirrus",
900 .name = "Cirrus VGA",
901 .class_names = { "cirrus-vga", "isa-cirrus-vga" },
903 [VGA_VMWARE] = {
904 .opt_name = "vmware",
905 .name = "VMWare SVGA",
906 .class_names = { "vmware-svga" },
908 [VGA_VIRTIO] = {
909 .opt_name = "virtio",
910 .name = "Virtio VGA",
911 .class_names = { "virtio-vga" },
913 [VGA_QXL] = {
914 .opt_name = "qxl",
915 .name = "QXL VGA",
916 .class_names = { "qxl-vga" },
918 [VGA_TCX] = {
919 .opt_name = "tcx",
920 .name = "TCX framebuffer",
921 .class_names = { "SUNW,tcx" },
923 [VGA_CG3] = {
924 .opt_name = "cg3",
925 .name = "CG3 framebuffer",
926 .class_names = { "cgthree" },
928 [VGA_XENFB] = {
929 .opt_name = "xenfb",
930 .name = "Xen paravirtualized framebuffer",
934 static bool vga_interface_available(VGAInterfaceType t)
936 const VGAInterfaceInfo *ti = &vga_interfaces[t];
938 assert(t < VGA_TYPE_MAX);
939 return !ti->class_names[0] ||
940 module_object_class_by_name(ti->class_names[0]) ||
941 module_object_class_by_name(ti->class_names[1]);
944 static const char *
945 get_default_vga_model(const MachineClass *machine_class)
947 if (machine_class->default_display) {
948 return machine_class->default_display;
949 } else if (vga_interface_available(VGA_CIRRUS)) {
950 return "cirrus";
951 } else if (vga_interface_available(VGA_STD)) {
952 return "std";
955 return NULL;
958 static void select_vgahw(const MachineClass *machine_class, const char *p)
960 const char *opts;
961 int t;
963 if (g_str_equal(p, "help")) {
964 const char *def = get_default_vga_model(machine_class);
966 for (t = 0; t < VGA_TYPE_MAX; t++) {
967 const VGAInterfaceInfo *ti = &vga_interfaces[t];
969 if (vga_interface_available(t) && ti->opt_name) {
970 printf("%-20s %s%s\n", ti->opt_name, ti->name ?: "",
971 g_str_equal(ti->opt_name, def) ? " (default)" : "");
974 exit(0);
977 assert(vga_interface_type == VGA_NONE);
978 for (t = 0; t < VGA_TYPE_MAX; t++) {
979 const VGAInterfaceInfo *ti = &vga_interfaces[t];
980 if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
981 if (!vga_interface_available(t)) {
982 error_report("%s not available", ti->name);
983 exit(1);
985 vga_interface_type = t;
986 break;
989 if (t == VGA_TYPE_MAX) {
990 invalid_vga:
991 error_report("unknown vga type: %s", p);
992 exit(1);
994 while (*opts) {
995 const char *nextopt;
997 if (strstart(opts, ",retrace=", &nextopt)) {
998 opts = nextopt;
999 if (strstart(opts, "dumb", &nextopt))
1000 vga_retrace_method = VGA_RETRACE_DUMB;
1001 else if (strstart(opts, "precise", &nextopt))
1002 vga_retrace_method = VGA_RETRACE_PRECISE;
1003 else goto invalid_vga;
1004 } else goto invalid_vga;
1005 opts = nextopt;
1009 static void parse_display_qapi(const char *optarg)
1011 DisplayOptions *opts;
1012 Visitor *v;
1014 v = qobject_input_visitor_new_str(optarg, "type", &error_fatal);
1016 visit_type_DisplayOptions(v, NULL, &opts, &error_fatal);
1017 QAPI_CLONE_MEMBERS(DisplayOptions, &dpy, opts);
1019 qapi_free_DisplayOptions(opts);
1020 visit_free(v);
1023 DisplayOptions *qmp_query_display_options(Error **errp)
1025 return QAPI_CLONE(DisplayOptions, &dpy);
1028 static void parse_display(const char *p)
1030 const char *opts;
1032 if (is_help_option(p)) {
1033 qemu_display_help();
1034 exit(0);
1037 if (strstart(p, "sdl", &opts)) {
1039 * sdl DisplayType needs hand-crafted parser instead of
1040 * parse_display_qapi() due to some options not in
1041 * DisplayOptions, specifically:
1042 * - frame
1043 * Already deprecated.
1044 * - ctrl_grab + alt_grab
1045 * Not clear yet what happens to them long-term. Should
1046 * replaced by something better or deprecated and dropped.
1048 dpy.type = DISPLAY_TYPE_SDL;
1049 while (*opts) {
1050 const char *nextopt;
1052 if (strstart(opts, ",alt_grab=", &nextopt)) {
1053 opts = nextopt;
1054 if (strstart(opts, "on", &nextopt)) {
1055 alt_grab = 1;
1056 } else if (strstart(opts, "off", &nextopt)) {
1057 alt_grab = 0;
1058 } else {
1059 goto invalid_sdl_args;
1061 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1062 opts = nextopt;
1063 if (strstart(opts, "on", &nextopt)) {
1064 ctrl_grab = 1;
1065 } else if (strstart(opts, "off", &nextopt)) {
1066 ctrl_grab = 0;
1067 } else {
1068 goto invalid_sdl_args;
1070 } else if (strstart(opts, ",window_close=", &nextopt)) {
1071 opts = nextopt;
1072 dpy.has_window_close = true;
1073 if (strstart(opts, "on", &nextopt)) {
1074 dpy.window_close = true;
1075 } else if (strstart(opts, "off", &nextopt)) {
1076 dpy.window_close = false;
1077 } else {
1078 goto invalid_sdl_args;
1080 } else if (strstart(opts, ",show-cursor=", &nextopt)) {
1081 opts = nextopt;
1082 dpy.has_show_cursor = true;
1083 if (strstart(opts, "on", &nextopt)) {
1084 dpy.show_cursor = true;
1085 } else if (strstart(opts, "off", &nextopt)) {
1086 dpy.show_cursor = false;
1087 } else {
1088 goto invalid_sdl_args;
1090 } else if (strstart(opts, ",gl=", &nextopt)) {
1091 opts = nextopt;
1092 dpy.has_gl = true;
1093 if (strstart(opts, "on", &nextopt)) {
1094 dpy.gl = DISPLAYGL_MODE_ON;
1095 } else if (strstart(opts, "core", &nextopt)) {
1096 dpy.gl = DISPLAYGL_MODE_CORE;
1097 } else if (strstart(opts, "es", &nextopt)) {
1098 dpy.gl = DISPLAYGL_MODE_ES;
1099 } else if (strstart(opts, "off", &nextopt)) {
1100 dpy.gl = DISPLAYGL_MODE_OFF;
1101 } else {
1102 goto invalid_sdl_args;
1104 } else {
1105 invalid_sdl_args:
1106 error_report("invalid SDL option string");
1107 exit(1);
1109 opts = nextopt;
1111 } else if (strstart(p, "vnc", &opts)) {
1113 * vnc isn't a (local) DisplayType but a protocol for remote
1114 * display access.
1116 if (*opts == '=') {
1117 vnc_parse(opts + 1);
1118 } else {
1119 error_report("VNC requires a display argument vnc=<display>");
1120 exit(1);
1122 } else {
1123 parse_display_qapi(p);
1127 static inline bool nonempty_str(const char *str)
1129 return str && *str;
1132 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
1134 gchar *buf;
1135 size_t size;
1136 const char *name, *file, *str, *gen_id;
1137 FWCfgState *fw_cfg = (FWCfgState *) opaque;
1139 if (fw_cfg == NULL) {
1140 error_setg(errp, "fw_cfg device not available");
1141 return -1;
1143 name = qemu_opt_get(opts, "name");
1144 file = qemu_opt_get(opts, "file");
1145 str = qemu_opt_get(opts, "string");
1146 gen_id = qemu_opt_get(opts, "gen_id");
1148 /* we need the name, and exactly one of: file, content string, gen_id */
1149 if (!nonempty_str(name) ||
1150 nonempty_str(file) + nonempty_str(str) + nonempty_str(gen_id) != 1) {
1151 error_setg(errp, "name, plus exactly one of file,"
1152 " string and gen_id, are needed");
1153 return -1;
1155 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
1156 error_setg(errp, "name too long (max. %d char)",
1157 FW_CFG_MAX_FILE_PATH - 1);
1158 return -1;
1160 if (nonempty_str(gen_id)) {
1162 * In this particular case where the content is populated
1163 * internally, the "etc/" namespace protection is relaxed,
1164 * so do not emit a warning.
1166 } else if (strncmp(name, "opt/", 4) != 0) {
1167 warn_report("externally provided fw_cfg item names "
1168 "should be prefixed with \"opt/\"");
1170 if (nonempty_str(str)) {
1171 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
1172 buf = g_memdup(str, size);
1173 } else if (nonempty_str(gen_id)) {
1174 if (!fw_cfg_add_from_generator(fw_cfg, name, gen_id, errp)) {
1175 return -1;
1177 return 0;
1178 } else {
1179 GError *err = NULL;
1180 if (!g_file_get_contents(file, &buf, &size, &err)) {
1181 error_setg(errp, "can't load %s: %s", file, err->message);
1182 g_error_free(err);
1183 return -1;
1186 /* For legacy, keep user files in a specific global order. */
1187 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
1188 fw_cfg_add_file(fw_cfg, name, buf, size);
1189 fw_cfg_reset_order_override(fw_cfg);
1190 return 0;
1193 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
1195 return qdev_device_help(opts);
1198 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
1200 DeviceState *dev;
1202 dev = qdev_device_add(opts, errp);
1203 if (!dev && *errp) {
1204 error_report_err(*errp);
1205 return -1;
1206 } else if (dev) {
1207 object_unref(OBJECT(dev));
1209 return 0;
1212 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
1214 Error *local_err = NULL;
1216 if (!qemu_chr_new_from_opts(opts, NULL, &local_err)) {
1217 if (local_err) {
1218 error_propagate(errp, local_err);
1219 return -1;
1221 exit(0);
1223 return 0;
1226 #ifdef CONFIG_VIRTFS
1227 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
1229 return qemu_fsdev_add(opts, errp);
1231 #endif
1233 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
1235 return monitor_init_opts(opts, errp);
1238 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
1240 static int monitor_device_index = 0;
1241 QemuOpts *opts;
1242 const char *p;
1243 char label[32];
1245 if (strstart(optarg, "chardev:", &p)) {
1246 snprintf(label, sizeof(label), "%s", p);
1247 } else {
1248 snprintf(label, sizeof(label), "compat_monitor%d",
1249 monitor_device_index);
1250 opts = qemu_chr_parse_compat(label, optarg, true);
1251 if (!opts) {
1252 error_report("parse error: %s", optarg);
1253 exit(1);
1257 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
1258 qemu_opt_set(opts, "mode", mode, &error_abort);
1259 qemu_opt_set(opts, "chardev", label, &error_abort);
1260 if (!strcmp(mode, "control")) {
1261 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
1262 } else {
1263 assert(pretty == false);
1265 monitor_device_index++;
1268 struct device_config {
1269 enum {
1270 DEV_USB, /* -usbdevice */
1271 DEV_SERIAL, /* -serial */
1272 DEV_PARALLEL, /* -parallel */
1273 DEV_DEBUGCON, /* -debugcon */
1274 DEV_GDB, /* -gdb, -s */
1275 DEV_SCLP, /* s390 sclp */
1276 } type;
1277 const char *cmdline;
1278 Location loc;
1279 QTAILQ_ENTRY(device_config) next;
1282 static QTAILQ_HEAD(, device_config) device_configs =
1283 QTAILQ_HEAD_INITIALIZER(device_configs);
1285 static void add_device_config(int type, const char *cmdline)
1287 struct device_config *conf;
1289 conf = g_malloc0(sizeof(*conf));
1290 conf->type = type;
1291 conf->cmdline = cmdline;
1292 loc_save(&conf->loc);
1293 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1296 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1298 struct device_config *conf;
1299 int rc;
1301 QTAILQ_FOREACH(conf, &device_configs, next) {
1302 if (conf->type != type)
1303 continue;
1304 loc_push_restore(&conf->loc);
1305 rc = func(conf->cmdline);
1306 loc_pop(&conf->loc);
1307 if (rc) {
1308 return rc;
1311 return 0;
1314 static void qemu_disable_default_devices(void)
1316 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1318 qemu_opts_foreach(qemu_find_opts("device"),
1319 default_driver_check, NULL, NULL);
1320 qemu_opts_foreach(qemu_find_opts("global"),
1321 default_driver_check, NULL, NULL);
1323 if (!vga_model && !default_vga) {
1324 vga_interface_type = VGA_DEVICE;
1326 if (!has_defaults || machine_class->no_serial) {
1327 default_serial = 0;
1329 if (!has_defaults || machine_class->no_parallel) {
1330 default_parallel = 0;
1332 if (!has_defaults || machine_class->no_floppy) {
1333 default_floppy = 0;
1335 if (!has_defaults || machine_class->no_cdrom) {
1336 default_cdrom = 0;
1338 if (!has_defaults || machine_class->no_sdcard) {
1339 default_sdcard = 0;
1341 if (!has_defaults) {
1342 default_monitor = 0;
1343 default_net = 0;
1344 default_vga = 0;
1348 static void qemu_create_default_devices(void)
1350 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1352 if (is_daemonized()) {
1353 /* According to documentation and historically, -nographic redirects
1354 * serial port, parallel port and monitor to stdio, which does not work
1355 * with -daemonize. We can redirect these to null instead, but since
1356 * -nographic is legacy, let's just error out.
1357 * We disallow -nographic only if all other ports are not redirected
1358 * explicitly, to not break existing legacy setups which uses
1359 * -nographic _and_ redirects all ports explicitly - this is valid
1360 * usage, -nographic is just a no-op in this case.
1362 if (nographic
1363 && (default_parallel || default_serial || default_monitor)) {
1364 error_report("-nographic cannot be used with -daemonize");
1365 exit(1);
1369 if (nographic) {
1370 if (default_parallel)
1371 add_device_config(DEV_PARALLEL, "null");
1372 if (default_serial && default_monitor) {
1373 add_device_config(DEV_SERIAL, "mon:stdio");
1374 } else {
1375 if (default_serial)
1376 add_device_config(DEV_SERIAL, "stdio");
1377 if (default_monitor)
1378 monitor_parse("stdio", "readline", false);
1380 } else {
1381 if (default_serial)
1382 add_device_config(DEV_SERIAL, "vc:80Cx24C");
1383 if (default_parallel)
1384 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
1385 if (default_monitor)
1386 monitor_parse("vc:80Cx24C", "readline", false);
1389 if (default_net) {
1390 QemuOptsList *net = qemu_find_opts("net");
1391 qemu_opts_parse(net, "nic", true, &error_abort);
1392 #ifdef CONFIG_SLIRP
1393 qemu_opts_parse(net, "user", true, &error_abort);
1394 #endif
1397 #if defined(CONFIG_VNC)
1398 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
1399 display_remote++;
1401 #endif
1402 if (dpy.type == DISPLAY_TYPE_DEFAULT && !display_remote) {
1403 if (!qemu_display_find_default(&dpy)) {
1404 dpy.type = DISPLAY_TYPE_NONE;
1405 #if defined(CONFIG_VNC)
1406 vnc_parse("localhost:0,to=99,id=default");
1407 #endif
1410 if (dpy.type == DISPLAY_TYPE_DEFAULT) {
1411 dpy.type = DISPLAY_TYPE_NONE;
1414 /* If no default VGA is requested, the default is "none". */
1415 if (default_vga) {
1416 vga_model = get_default_vga_model(machine_class);
1418 if (vga_model) {
1419 select_vgahw(machine_class, vga_model);
1423 static int serial_parse(const char *devname)
1425 int index = num_serial_hds;
1426 char label[32];
1428 if (strcmp(devname, "none") == 0)
1429 return 0;
1430 snprintf(label, sizeof(label), "serial%d", index);
1431 serial_hds = g_renew(Chardev *, serial_hds, index + 1);
1433 serial_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
1434 if (!serial_hds[index]) {
1435 error_report("could not connect serial device"
1436 " to character backend '%s'", devname);
1437 return -1;
1439 num_serial_hds++;
1440 return 0;
1443 Chardev *serial_hd(int i)
1445 assert(i >= 0);
1446 if (i < num_serial_hds) {
1447 return serial_hds[i];
1449 return NULL;
1452 static int parallel_parse(const char *devname)
1454 static int index = 0;
1455 char label[32];
1457 if (strcmp(devname, "none") == 0)
1458 return 0;
1459 if (index == MAX_PARALLEL_PORTS) {
1460 error_report("too many parallel ports");
1461 exit(1);
1463 snprintf(label, sizeof(label), "parallel%d", index);
1464 parallel_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
1465 if (!parallel_hds[index]) {
1466 error_report("could not connect parallel device"
1467 " to character backend '%s'", devname);
1468 return -1;
1470 index++;
1471 return 0;
1474 static int debugcon_parse(const char *devname)
1476 QemuOpts *opts;
1478 if (!qemu_chr_new_mux_mon("debugcon", devname, NULL)) {
1479 error_report("invalid character backend '%s'", devname);
1480 exit(1);
1482 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
1483 if (!opts) {
1484 error_report("already have a debugcon device");
1485 exit(1);
1487 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
1488 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
1489 return 0;
1492 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
1494 const MachineClass *mc1 = a, *mc2 = b;
1495 int res;
1497 if (mc1->family == NULL) {
1498 if (mc2->family == NULL) {
1499 /* Compare standalone machine types against each other; they sort
1500 * in increasing order.
1502 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
1503 object_class_get_name(OBJECT_CLASS(mc2)));
1506 /* Standalone machine types sort after families. */
1507 return 1;
1510 if (mc2->family == NULL) {
1511 /* Families sort before standalone machine types. */
1512 return -1;
1515 /* Families sort between each other alphabetically increasingly. */
1516 res = strcmp(mc1->family, mc2->family);
1517 if (res != 0) {
1518 return res;
1521 /* Within the same family, machine types sort in decreasing order. */
1522 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
1523 object_class_get_name(OBJECT_CLASS(mc1)));
1526 static MachineClass *machine_parse(const char *name, GSList *machines)
1528 MachineClass *mc;
1529 GSList *el;
1531 if (is_help_option(name)) {
1532 printf("Supported machines are:\n");
1533 machines = g_slist_sort(machines, machine_class_cmp);
1534 for (el = machines; el; el = el->next) {
1535 MachineClass *mc = el->data;
1536 if (mc->alias) {
1537 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
1539 printf("%-20s %s%s%s\n", mc->name, mc->desc,
1540 mc->is_default ? " (default)" : "",
1541 mc->deprecation_reason ? " (deprecated)" : "");
1543 exit(0);
1546 mc = find_machine(name, machines);
1547 if (!mc) {
1548 error_report("unsupported machine type");
1549 error_printf("Use -machine help to list supported machines\n");
1550 exit(1);
1552 return mc;
1555 static const char *pid_file;
1556 static Notifier qemu_unlink_pidfile_notifier;
1558 static void qemu_unlink_pidfile(Notifier *n, void *data)
1560 if (pid_file) {
1561 unlink(pid_file);
1565 static const QEMUOption *lookup_opt(int argc, char **argv,
1566 const char **poptarg, int *poptind)
1568 const QEMUOption *popt;
1569 int optind = *poptind;
1570 char *r = argv[optind];
1571 const char *optarg;
1573 loc_set_cmdline(argv, optind, 1);
1574 optind++;
1575 /* Treat --foo the same as -foo. */
1576 if (r[1] == '-')
1577 r++;
1578 popt = qemu_options;
1579 for(;;) {
1580 if (!popt->name) {
1581 error_report("invalid option");
1582 exit(1);
1584 if (!strcmp(popt->name, r + 1))
1585 break;
1586 popt++;
1588 if (popt->flags & HAS_ARG) {
1589 if (optind >= argc) {
1590 error_report("requires an argument");
1591 exit(1);
1593 optarg = argv[optind++];
1594 loc_set_cmdline(argv, optind - 2, 2);
1595 } else {
1596 optarg = NULL;
1599 *poptarg = optarg;
1600 *poptind = optind;
1602 return popt;
1605 static MachineClass *select_machine(void)
1607 GSList *machines = object_class_get_list(TYPE_MACHINE, false);
1608 MachineClass *machine_class = find_default_machine(machines);
1609 const char *optarg;
1610 QemuOpts *opts;
1611 Location loc;
1613 loc_push_none(&loc);
1615 opts = qemu_get_machine_opts();
1616 qemu_opts_loc_restore(opts);
1618 optarg = qemu_opt_get(opts, "type");
1619 if (optarg) {
1620 machine_class = machine_parse(optarg, machines);
1623 if (!machine_class) {
1624 error_report("No machine specified, and there is no default");
1625 error_printf("Use -machine help to list supported machines\n");
1626 exit(1);
1629 loc_pop(&loc);
1630 g_slist_free(machines);
1631 return machine_class;
1634 static int object_parse_property_opt(Object *obj,
1635 const char *name, const char *value,
1636 const char *skip, Error **errp)
1638 if (g_str_equal(name, skip)) {
1639 return 0;
1642 if (!object_property_parse(obj, name, value, errp)) {
1643 return -1;
1646 return 0;
1649 static int machine_set_property(void *opaque,
1650 const char *name, const char *value,
1651 Error **errp)
1653 g_autofree char *qom_name = g_strdup(name);
1654 char *p;
1656 for (p = qom_name; *p; p++) {
1657 if (*p == '_') {
1658 *p = '-';
1662 /* Legacy options do not correspond to MachineState properties. */
1663 if (g_str_equal(qom_name, "accel")) {
1664 return 0;
1666 if (g_str_equal(qom_name, "igd-passthru")) {
1667 object_register_sugar_prop(ACCEL_CLASS_NAME("xen"), qom_name, value,
1668 false);
1669 return 0;
1671 if (g_str_equal(qom_name, "kvm-shadow-mem")) {
1672 object_register_sugar_prop(ACCEL_CLASS_NAME("kvm"), qom_name, value,
1673 false);
1674 return 0;
1676 if (g_str_equal(qom_name, "kernel-irqchip")) {
1677 object_register_sugar_prop(ACCEL_CLASS_NAME("kvm"), qom_name, value,
1678 false);
1679 object_register_sugar_prop(ACCEL_CLASS_NAME("whpx"), qom_name, value,
1680 false);
1681 return 0;
1684 return object_parse_property_opt(opaque, name, value, "type", errp);
1688 * Initial object creation happens before all other
1689 * QEMU data types are created. The majority of objects
1690 * can be created at this point. The rng-egd object
1691 * cannot be created here, as it depends on the chardev
1692 * already existing.
1694 static bool object_create_early(const char *type, QemuOpts *opts)
1696 if (user_creatable_print_help(type, opts)) {
1697 exit(0);
1701 * Objects should not be made "delayed" without a reason. If you
1702 * add one, state the reason in a comment!
1705 /* Reason: rng-egd property "chardev" */
1706 if (g_str_equal(type, "rng-egd")) {
1707 return false;
1710 #if defined(CONFIG_VHOST_USER) && defined(CONFIG_LINUX)
1711 /* Reason: cryptodev-vhost-user property "chardev" */
1712 if (g_str_equal(type, "cryptodev-vhost-user")) {
1713 return false;
1715 #endif
1717 /* Reason: vhost-user-blk-server property "node-name" */
1718 if (g_str_equal(type, "vhost-user-blk-server")) {
1719 return false;
1722 * Reason: filter-* property "netdev" etc.
1724 if (g_str_equal(type, "filter-buffer") ||
1725 g_str_equal(type, "filter-dump") ||
1726 g_str_equal(type, "filter-mirror") ||
1727 g_str_equal(type, "filter-redirector") ||
1728 g_str_equal(type, "colo-compare") ||
1729 g_str_equal(type, "filter-rewriter") ||
1730 g_str_equal(type, "filter-replay")) {
1731 return false;
1735 * Allocation of large amounts of memory may delay
1736 * chardev initialization for too long, and trigger timeouts
1737 * on software that waits for a monitor socket to be created
1738 * (e.g. libvirt).
1740 if (g_str_has_prefix(type, "memory-backend-")) {
1741 return false;
1744 return true;
1747 static void qemu_apply_machine_options(void)
1749 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1750 QemuOpts *machine_opts = qemu_get_machine_opts();
1751 const char *boot_order = NULL;
1752 const char *boot_once = NULL;
1753 QemuOpts *opts;
1755 qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
1756 &error_fatal);
1757 current_machine->ram_size = ram_size;
1758 current_machine->maxram_size = maxram_size;
1759 current_machine->ram_slots = ram_slots;
1761 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
1762 if (opts) {
1763 boot_order = qemu_opt_get(opts, "order");
1764 if (boot_order) {
1765 validate_bootdevices(boot_order, &error_fatal);
1768 boot_once = qemu_opt_get(opts, "once");
1769 if (boot_once) {
1770 validate_bootdevices(boot_once, &error_fatal);
1773 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
1774 boot_strict = qemu_opt_get_bool(opts, "strict", false);
1777 if (!boot_order) {
1778 boot_order = machine_class->default_boot_order;
1781 current_machine->boot_order = boot_order;
1782 current_machine->boot_once = boot_once;
1784 if (semihosting_enabled() && !semihosting_get_argc()) {
1785 const char *kernel_filename = qemu_opt_get(machine_opts, "kernel");
1786 const char *kernel_cmdline = qemu_opt_get(machine_opts, "append") ?: "";
1787 /* fall back to the -kernel/-append */
1788 semihosting_arg_fallback(kernel_filename, kernel_cmdline);
1792 static void qemu_create_early_backends(void)
1794 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1796 if ((alt_grab || ctrl_grab) && dpy.type != DISPLAY_TYPE_SDL) {
1797 error_report("-alt-grab and -ctrl-grab are only valid "
1798 "for SDL, ignoring option");
1800 if (dpy.has_window_close &&
1801 (dpy.type != DISPLAY_TYPE_GTK && dpy.type != DISPLAY_TYPE_SDL)) {
1802 error_report("-no-quit is only valid for GTK and SDL, "
1803 "ignoring option");
1806 qemu_display_early_init(&dpy);
1807 qemu_console_early_init();
1809 if (dpy.has_gl && dpy.gl != DISPLAYGL_MODE_OFF && display_opengl == 0) {
1810 #if defined(CONFIG_OPENGL)
1811 error_report("OpenGL is not supported by the display");
1812 #else
1813 error_report("OpenGL support is disabled");
1814 #endif
1815 exit(1);
1818 qemu_opts_foreach(qemu_find_opts("object"),
1819 user_creatable_add_opts_foreach,
1820 object_create_early, &error_fatal);
1822 /* spice needs the timers to be initialized by this point */
1823 /* spice must initialize before audio as it changes the default auiodev */
1824 /* spice must initialize before chardevs (for spicevmc and spiceport) */
1825 qemu_spice.init();
1827 qemu_opts_foreach(qemu_find_opts("chardev"),
1828 chardev_init_func, NULL, &error_fatal);
1830 #ifdef CONFIG_VIRTFS
1831 qemu_opts_foreach(qemu_find_opts("fsdev"),
1832 fsdev_init_func, NULL, &error_fatal);
1833 #endif
1836 * Note: we need to create audio and block backends before
1837 * machine_set_property(), so machine properties can refer to
1838 * them.
1840 configure_blockdev(&bdo_queue, machine_class, snapshot);
1841 audio_init_audiodevs();
1846 * The remainder of object creation happens after the
1847 * creation of chardev, fsdev, net clients and device data types.
1849 static bool object_create_late(const char *type, QemuOpts *opts)
1851 return !object_create_early(type, opts);
1854 static void qemu_create_late_backends(void)
1856 if (qtest_chrdev) {
1857 qtest_server_init(qtest_chrdev, qtest_log, &error_fatal);
1860 net_init_clients(&error_fatal);
1862 qemu_opts_foreach(qemu_find_opts("object"),
1863 user_creatable_add_opts_foreach,
1864 object_create_late, &error_fatal);
1866 if (tpm_init() < 0) {
1867 exit(1);
1870 qemu_opts_foreach(qemu_find_opts("mon"),
1871 mon_init_func, NULL, &error_fatal);
1873 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
1874 exit(1);
1875 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
1876 exit(1);
1877 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
1878 exit(1);
1880 /* now chardevs have been created we may have semihosting to connect */
1881 qemu_semihosting_connect_chardevs();
1882 qemu_semihosting_console_init();
1885 static bool have_custom_ram_size(void)
1887 QemuOpts *opts = qemu_find_opts_singleton("memory");
1888 return !!qemu_opt_get_size(opts, "size", 0);
1891 static void qemu_resolve_machine_memdev(void)
1893 if (current_machine->ram_memdev_id) {
1894 Object *backend;
1895 ram_addr_t backend_size;
1897 backend = object_resolve_path_type(current_machine->ram_memdev_id,
1898 TYPE_MEMORY_BACKEND, NULL);
1899 if (!backend) {
1900 error_report("Memory backend '%s' not found",
1901 current_machine->ram_memdev_id);
1902 exit(EXIT_FAILURE);
1904 backend_size = object_property_get_uint(backend, "size", &error_abort);
1905 if (have_custom_ram_size() && backend_size != ram_size) {
1906 error_report("Size specified by -m option must match size of "
1907 "explicitly specified 'memory-backend' property");
1908 exit(EXIT_FAILURE);
1910 if (mem_path) {
1911 error_report("'-mem-path' can't be used together with"
1912 "'-machine memory-backend'");
1913 exit(EXIT_FAILURE);
1915 ram_size = backend_size;
1918 if (!xen_enabled()) {
1919 /* On 32-bit hosts, QEMU is limited by virtual address space */
1920 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
1921 error_report("at most 2047 MB RAM can be simulated");
1922 exit(1);
1927 static void set_memory_options(MachineClass *mc)
1929 uint64_t sz;
1930 const char *mem_str;
1931 const ram_addr_t default_ram_size = mc->default_ram_size;
1932 QemuOpts *opts = qemu_find_opts_singleton("memory");
1933 Location loc;
1935 loc_push_none(&loc);
1936 qemu_opts_loc_restore(opts);
1938 sz = 0;
1939 mem_str = qemu_opt_get(opts, "size");
1940 if (mem_str) {
1941 if (!*mem_str) {
1942 error_report("missing 'size' option value");
1943 exit(EXIT_FAILURE);
1946 sz = qemu_opt_get_size(opts, "size", ram_size);
1948 /* Fix up legacy suffix-less format */
1949 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
1950 uint64_t overflow_check = sz;
1952 sz *= MiB;
1953 if (sz / MiB != overflow_check) {
1954 error_report("too large 'size' option value");
1955 exit(EXIT_FAILURE);
1960 /* backward compatibility behaviour for case "-m 0" */
1961 if (sz == 0) {
1962 sz = default_ram_size;
1965 sz = QEMU_ALIGN_UP(sz, 8192);
1966 if (mc->fixup_ram_size) {
1967 sz = mc->fixup_ram_size(sz);
1969 ram_size = sz;
1970 if (ram_size != sz) {
1971 error_report("ram size too large");
1972 exit(EXIT_FAILURE);
1975 /* store value for the future use */
1976 qemu_opt_set_number(opts, "size", ram_size, &error_abort);
1977 maxram_size = ram_size;
1979 if (qemu_opt_get(opts, "maxmem")) {
1980 uint64_t slots;
1982 sz = qemu_opt_get_size(opts, "maxmem", 0);
1983 slots = qemu_opt_get_number(opts, "slots", 0);
1984 if (sz < ram_size) {
1985 error_report("invalid value of -m option maxmem: "
1986 "maximum memory size (0x%" PRIx64 ") must be at least "
1987 "the initial memory size (0x" RAM_ADDR_FMT ")",
1988 sz, ram_size);
1989 exit(EXIT_FAILURE);
1990 } else if (slots && sz == ram_size) {
1991 error_report("invalid value of -m option maxmem: "
1992 "memory slots were specified but maximum memory size "
1993 "(0x%" PRIx64 ") is equal to the initial memory size "
1994 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
1995 exit(EXIT_FAILURE);
1998 maxram_size = sz;
1999 ram_slots = slots;
2000 } else if (qemu_opt_get(opts, "slots")) {
2001 error_report("invalid -m option value: missing 'maxmem' option");
2002 exit(EXIT_FAILURE);
2005 loc_pop(&loc);
2008 static void qemu_create_machine(MachineClass *machine_class)
2010 object_set_machine_compat_props(machine_class->compat_props);
2012 set_memory_options(machine_class);
2014 current_machine = MACHINE(object_new_with_class(OBJECT_CLASS(machine_class)));
2015 if (machine_help_func(qemu_get_machine_opts(), current_machine)) {
2016 exit(0);
2018 object_property_add_child(object_get_root(), "machine",
2019 OBJECT(current_machine));
2020 object_property_add_child(container_get(OBJECT(current_machine),
2021 "/unattached"),
2022 "sysbus", OBJECT(sysbus_get_default()));
2024 if (machine_class->minimum_page_bits) {
2025 if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
2026 /* This would be a board error: specifying a minimum smaller than
2027 * a target's compile-time fixed setting.
2029 g_assert_not_reached();
2033 cpu_exec_init_all();
2034 page_size_init();
2036 if (machine_class->hw_version) {
2037 qemu_set_hw_version(machine_class->hw_version);
2040 machine_smp_parse(current_machine,
2041 qemu_opts_find(qemu_find_opts("smp-opts"), NULL), &error_fatal);
2044 * Get the default machine options from the machine if it is not already
2045 * specified either by the configuration file or by the command line.
2047 if (machine_class->default_machine_opts) {
2048 qemu_opts_set_defaults(qemu_find_opts("machine"),
2049 machine_class->default_machine_opts, 0);
2053 static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
2055 GlobalProperty *g;
2057 g = g_malloc0(sizeof(*g));
2058 g->driver = qemu_opt_get(opts, "driver");
2059 g->property = qemu_opt_get(opts, "property");
2060 g->value = qemu_opt_get(opts, "value");
2061 qdev_prop_register_global(g);
2062 return 0;
2065 static int qemu_read_default_config_file(void)
2067 int ret;
2068 g_autofree char *file = get_relocated_path(CONFIG_QEMU_CONFDIR "/qemu.conf");
2070 ret = qemu_read_config_file(file);
2071 if (ret < 0 && ret != -ENOENT) {
2072 return ret;
2075 return 0;
2078 static int qemu_set_option(const char *str)
2080 Error *local_err = NULL;
2081 char group[64], id[64], arg[64];
2082 QemuOptsList *list;
2083 QemuOpts *opts;
2084 int rc, offset;
2086 rc = sscanf(str, "%63[^.].%63[^.].%63[^=]%n", group, id, arg, &offset);
2087 if (rc < 3 || str[offset] != '=') {
2088 error_report("can't parse: \"%s\"", str);
2089 return -1;
2092 list = qemu_find_opts(group);
2093 if (list == NULL) {
2094 return -1;
2097 opts = qemu_opts_find(list, id);
2098 if (!opts) {
2099 error_report("there is no %s \"%s\" defined",
2100 list->name, id);
2101 return -1;
2104 if (!qemu_opt_set(opts, arg, str + offset + 1, &local_err)) {
2105 error_report_err(local_err);
2106 return -1;
2108 return 0;
2111 static void user_register_global_props(void)
2113 qemu_opts_foreach(qemu_find_opts("global"),
2114 global_init_func, NULL, NULL);
2117 static int do_configure_icount(void *opaque, QemuOpts *opts, Error **errp)
2119 icount_configure(opts, errp);
2120 return 0;
2123 static int accelerator_set_property(void *opaque,
2124 const char *name, const char *value,
2125 Error **errp)
2127 return object_parse_property_opt(opaque, name, value, "accel", errp);
2130 static int do_configure_accelerator(void *opaque, QemuOpts *opts, Error **errp)
2132 bool *p_init_failed = opaque;
2133 const char *acc = qemu_opt_get(opts, "accel");
2134 AccelClass *ac = accel_find(acc);
2135 AccelState *accel;
2136 int ret;
2137 bool qtest_with_kvm;
2139 qtest_with_kvm = g_str_equal(acc, "kvm") && qtest_chrdev != NULL;
2141 if (!ac) {
2142 *p_init_failed = true;
2143 if (!qtest_with_kvm) {
2144 error_report("invalid accelerator %s", acc);
2146 return 0;
2148 accel = ACCEL(object_new_with_class(OBJECT_CLASS(ac)));
2149 object_apply_compat_props(OBJECT(accel));
2150 qemu_opt_foreach(opts, accelerator_set_property,
2151 accel,
2152 &error_fatal);
2154 ret = accel_init_machine(accel, current_machine);
2155 if (ret < 0) {
2156 *p_init_failed = true;
2157 if (!qtest_with_kvm || ret != -ENOENT) {
2158 error_report("failed to initialize %s: %s", acc, strerror(-ret));
2160 return 0;
2163 return 1;
2166 static void configure_accelerators(const char *progname)
2168 const char *accelerators;
2169 bool init_failed = false;
2171 qemu_opts_foreach(qemu_find_opts("icount"),
2172 do_configure_icount, NULL, &error_fatal);
2174 accelerators = qemu_opt_get(qemu_get_machine_opts(), "accel");
2175 if (QTAILQ_EMPTY(&qemu_accel_opts.head)) {
2176 char **accel_list, **tmp;
2178 if (accelerators == NULL) {
2179 /* Select the default accelerator */
2180 bool have_tcg = accel_find("tcg");
2181 bool have_kvm = accel_find("kvm");
2183 if (have_tcg && have_kvm) {
2184 if (g_str_has_suffix(progname, "kvm")) {
2185 /* If the program name ends with "kvm", we prefer KVM */
2186 accelerators = "kvm:tcg";
2187 } else {
2188 accelerators = "tcg:kvm";
2190 } else if (have_kvm) {
2191 accelerators = "kvm";
2192 } else if (have_tcg) {
2193 accelerators = "tcg";
2194 } else {
2195 error_report("No accelerator selected and"
2196 " no default accelerator available");
2197 exit(1);
2200 accel_list = g_strsplit(accelerators, ":", 0);
2202 for (tmp = accel_list; *tmp; tmp++) {
2204 * Filter invalid accelerators here, to prevent obscenities
2205 * such as "-machine accel=tcg,,thread=single".
2207 if (accel_find(*tmp)) {
2208 qemu_opts_parse_noisily(qemu_find_opts("accel"), *tmp, true);
2209 } else {
2210 init_failed = true;
2211 error_report("invalid accelerator %s", *tmp);
2214 g_strfreev(accel_list);
2215 } else {
2216 if (accelerators != NULL) {
2217 error_report("The -accel and \"-machine accel=\" options are incompatible");
2218 exit(1);
2222 if (!qemu_opts_foreach(qemu_find_opts("accel"),
2223 do_configure_accelerator, &init_failed, &error_fatal)) {
2224 if (!init_failed) {
2225 error_report("no accelerator found");
2227 exit(1);
2230 if (init_failed && !qtest_chrdev) {
2231 AccelClass *ac = ACCEL_GET_CLASS(current_accel());
2232 error_report("falling back to %s", ac->name);
2235 if (icount_enabled() && !tcg_enabled()) {
2236 error_report("-icount is not allowed with hardware virtualization");
2237 exit(1);
2241 static void create_default_memdev(MachineState *ms, const char *path)
2243 Object *obj;
2244 MachineClass *mc = MACHINE_GET_CLASS(ms);
2246 obj = object_new(path ? TYPE_MEMORY_BACKEND_FILE : TYPE_MEMORY_BACKEND_RAM);
2247 if (path) {
2248 object_property_set_str(obj, "mem-path", path, &error_fatal);
2250 object_property_set_int(obj, "size", ms->ram_size, &error_fatal);
2251 object_property_add_child(object_get_objects_root(), mc->default_ram_id,
2252 obj);
2253 /* Ensure backend's memory region name is equal to mc->default_ram_id */
2254 object_property_set_bool(obj, "x-use-canonical-path-for-ramblock-id",
2255 false, &error_fatal);
2256 user_creatable_complete(USER_CREATABLE(obj), &error_fatal);
2257 object_unref(obj);
2258 object_property_set_str(OBJECT(ms), "memory-backend", mc->default_ram_id,
2259 &error_fatal);
2262 static void qemu_validate_options(void)
2264 QemuOpts *machine_opts = qemu_get_machine_opts();
2265 const char *kernel_filename = qemu_opt_get(machine_opts, "kernel");
2266 const char *initrd_filename = qemu_opt_get(machine_opts, "initrd");
2267 const char *kernel_cmdline = qemu_opt_get(machine_opts, "append");
2269 if (kernel_filename == NULL) {
2270 if (kernel_cmdline != NULL) {
2271 error_report("-append only allowed with -kernel option");
2272 exit(1);
2275 if (initrd_filename != NULL) {
2276 error_report("-initrd only allowed with -kernel option");
2277 exit(1);
2281 if (loadvm && preconfig_requested) {
2282 error_report("'preconfig' and 'loadvm' options are "
2283 "mutually exclusive");
2284 exit(EXIT_FAILURE);
2286 if (incoming && preconfig_requested && strcmp(incoming, "defer") != 0) {
2287 error_report("'preconfig' supports '-incoming defer' only");
2288 exit(EXIT_FAILURE);
2291 #ifdef CONFIG_CURSES
2292 if (is_daemonized() && dpy.type == DISPLAY_TYPE_CURSES) {
2293 error_report("curses display cannot be used with -daemonize");
2294 exit(1);
2296 #endif
2299 static void qemu_process_sugar_options(void)
2301 if (mem_prealloc) {
2302 char *val;
2304 val = g_strdup_printf("%d",
2305 (uint32_t) qemu_opt_get_number(qemu_find_opts_singleton("smp-opts"), "cpus", 1));
2306 object_register_sugar_prop("memory-backend", "prealloc-threads", val,
2307 false);
2308 g_free(val);
2309 object_register_sugar_prop("memory-backend", "prealloc", "on", false);
2312 if (watchdog) {
2313 int i = select_watchdog(watchdog);
2314 if (i > 0)
2315 exit (i == 1 ? 1 : 0);
2319 /* -action processing */
2322 * Process all the -action parameters parsed from cmdline.
2324 static int process_runstate_actions(void *opaque, QemuOpts *opts, Error **errp)
2326 Error *local_err = NULL;
2327 QDict *qdict = qemu_opts_to_qdict(opts, NULL);
2328 QObject *ret = NULL;
2329 qmp_marshal_set_action(qdict, &ret, &local_err);
2330 qobject_unref(ret);
2331 qobject_unref(qdict);
2332 if (local_err) {
2333 error_propagate(errp, local_err);
2334 return 1;
2336 return 0;
2339 static void qemu_process_early_options(void)
2341 #ifdef CONFIG_SECCOMP
2342 QemuOptsList *olist = qemu_find_opts_err("sandbox", NULL);
2343 if (olist) {
2344 qemu_opts_foreach(olist, parse_sandbox, NULL, &error_fatal);
2346 #endif
2348 qemu_opts_foreach(qemu_find_opts("name"),
2349 parse_name, NULL, &error_fatal);
2351 if (qemu_opts_foreach(qemu_find_opts("action"),
2352 process_runstate_actions, NULL, &error_fatal)) {
2353 exit(1);
2356 #ifndef _WIN32
2357 qemu_opts_foreach(qemu_find_opts("add-fd"),
2358 parse_add_fd, NULL, &error_fatal);
2360 qemu_opts_foreach(qemu_find_opts("add-fd"),
2361 cleanup_add_fd, NULL, &error_fatal);
2362 #endif
2364 if (!trace_init_backends()) {
2365 exit(1);
2367 trace_init_file();
2369 /* Open the logfile at this point and set the log mask if necessary. */
2370 qemu_set_log_filename(log_file, &error_fatal);
2371 if (log_mask) {
2372 int mask;
2373 mask = qemu_str_to_log_mask(log_mask);
2374 if (!mask) {
2375 qemu_print_log_usage(stdout);
2376 exit(1);
2378 qemu_set_log(mask);
2379 } else {
2380 qemu_set_log(0);
2383 qemu_add_default_firmwarepath();
2386 static void qemu_process_help_options(void)
2389 * Check for -cpu help and -device help before we call select_machine(),
2390 * which will return an error if the architecture has no default machine
2391 * type and the user did not specify one, so that the user doesn't need
2392 * to say '-cpu help -machine something'.
2394 if (cpu_option && is_help_option(cpu_option)) {
2395 list_cpus(cpu_option);
2396 exit(0);
2399 if (qemu_opts_foreach(qemu_find_opts("device"),
2400 device_help_func, NULL, NULL)) {
2401 exit(0);
2404 /* -L help lists the data directories and exits. */
2405 if (list_data_dirs) {
2406 qemu_list_data_dirs();
2407 exit(0);
2411 static void qemu_maybe_daemonize(const char *pid_file)
2413 Error *err;
2415 os_daemonize();
2416 rcu_disable_atfork();
2418 if (pid_file && !qemu_write_pidfile(pid_file, &err)) {
2419 error_reportf_err(err, "cannot create PID file: ");
2420 exit(1);
2423 qemu_unlink_pidfile_notifier.notify = qemu_unlink_pidfile;
2424 qemu_add_exit_notifier(&qemu_unlink_pidfile_notifier);
2427 static void qemu_init_displays(void)
2429 DisplayState *ds;
2431 /* init local displays */
2432 ds = init_displaystate();
2433 qemu_display_init(ds, &dpy);
2435 /* must be after terminal init, SDL library changes signal handlers */
2436 os_setup_signal_handling();
2438 /* init remote displays */
2439 #ifdef CONFIG_VNC
2440 qemu_opts_foreach(qemu_find_opts("vnc"),
2441 vnc_init_func, NULL, &error_fatal);
2442 #endif
2444 if (using_spice) {
2445 qemu_spice.display_init();
2449 static void qemu_init_board(void)
2451 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
2453 if (machine_class->default_ram_id && current_machine->ram_size &&
2454 numa_uses_legacy_mem() && !current_machine->ram_memdev_id) {
2455 create_default_memdev(current_machine, mem_path);
2458 /* process plugin before CPUs are created, but once -smp has been parsed */
2459 qemu_plugin_load_list(&plugin_list, &error_fatal);
2461 /* From here on we enter MACHINE_PHASE_INITIALIZED. */
2462 machine_run_board_init(current_machine);
2465 * TODO To drop support for deprecated bogus if=..., move
2466 * drive_check_orphaned() here, replacing this call. Also drop
2467 * its deprecation warning, along with DriveInfo member
2468 * @claimed_by_board.
2470 drive_mark_claimed_by_board();
2472 realtime_init();
2474 if (hax_enabled()) {
2475 /* FIXME: why isn't cpu_synchronize_all_post_init enough? */
2476 hax_sync_vcpus();
2480 static void qemu_create_cli_devices(void)
2482 soundhw_init();
2484 qemu_opts_foreach(qemu_find_opts("fw_cfg"),
2485 parse_fw_cfg, fw_cfg_find(), &error_fatal);
2487 /* init USB devices */
2488 if (machine_usb(current_machine)) {
2489 if (foreach_device_config(DEV_USB, usb_parse) < 0)
2490 exit(1);
2493 /* init generic devices */
2494 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
2495 qemu_opts_foreach(qemu_find_opts("device"),
2496 device_init_func, NULL, &error_fatal);
2497 rom_reset_order_override();
2500 static void qemu_machine_creation_done(void)
2502 MachineState *machine = MACHINE(qdev_get_machine());
2504 /* Did we create any drives that we failed to create a device for? */
2505 drive_check_orphaned();
2507 /* Don't warn about the default network setup that you get if
2508 * no command line -net or -netdev options are specified. There
2509 * are two cases that we would otherwise complain about:
2510 * (1) board doesn't support a NIC but the implicit "-net nic"
2511 * requested one
2512 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
2513 * sets up a nic that isn't connected to anything.
2515 if (!default_net && (!qtest_enabled() || has_defaults)) {
2516 net_check_clients();
2519 qdev_prop_check_globals();
2521 qdev_machine_creation_done();
2523 if (machine->cgs) {
2525 * Verify that Confidential Guest Support has actually been initialized
2527 assert(machine->cgs->ready);
2530 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
2531 exit(1);
2535 void qmp_x_exit_preconfig(Error **errp)
2537 if (phase_check(PHASE_MACHINE_INITIALIZED)) {
2538 error_setg(errp, "The command is permitted only before machine initialization");
2539 return;
2542 qemu_init_board();
2543 qemu_create_cli_devices();
2544 qemu_machine_creation_done();
2546 if (loadvm) {
2547 Error *local_err = NULL;
2548 if (!load_snapshot(loadvm, NULL, false, NULL, &local_err)) {
2549 error_report_err(local_err);
2550 autostart = 0;
2551 exit(1);
2554 if (replay_mode != REPLAY_MODE_NONE) {
2555 replay_vmstate_init();
2558 if (incoming) {
2559 Error *local_err = NULL;
2560 if (strcmp(incoming, "defer") != 0) {
2561 qmp_migrate_incoming(incoming, &local_err);
2562 if (local_err) {
2563 error_reportf_err(local_err, "-incoming %s: ", incoming);
2564 exit(1);
2567 } else if (autostart) {
2568 qmp_cont(NULL);
2572 void qemu_init(int argc, char **argv, char **envp)
2574 QemuOpts *opts;
2575 QemuOpts *icount_opts = NULL, *accel_opts = NULL;
2576 QemuOptsList *olist;
2577 int optind;
2578 const char *optarg;
2579 MachineClass *machine_class;
2580 bool userconfig = true;
2581 FILE *vmstate_dump_file = NULL;
2583 qemu_add_opts(&qemu_drive_opts);
2584 qemu_add_drive_opts(&qemu_legacy_drive_opts);
2585 qemu_add_drive_opts(&qemu_common_drive_opts);
2586 qemu_add_drive_opts(&qemu_drive_opts);
2587 qemu_add_drive_opts(&bdrv_runtime_opts);
2588 qemu_add_opts(&qemu_chardev_opts);
2589 qemu_add_opts(&qemu_device_opts);
2590 qemu_add_opts(&qemu_netdev_opts);
2591 qemu_add_opts(&qemu_nic_opts);
2592 qemu_add_opts(&qemu_net_opts);
2593 qemu_add_opts(&qemu_rtc_opts);
2594 qemu_add_opts(&qemu_global_opts);
2595 qemu_add_opts(&qemu_mon_opts);
2596 qemu_add_opts(&qemu_trace_opts);
2597 qemu_plugin_add_opts();
2598 qemu_add_opts(&qemu_option_rom_opts);
2599 qemu_add_opts(&qemu_machine_opts);
2600 qemu_add_opts(&qemu_accel_opts);
2601 qemu_add_opts(&qemu_mem_opts);
2602 qemu_add_opts(&qemu_smp_opts);
2603 qemu_add_opts(&qemu_boot_opts);
2604 qemu_add_opts(&qemu_add_fd_opts);
2605 qemu_add_opts(&qemu_object_opts);
2606 qemu_add_opts(&qemu_tpmdev_opts);
2607 qemu_add_opts(&qemu_overcommit_opts);
2608 qemu_add_opts(&qemu_msg_opts);
2609 qemu_add_opts(&qemu_name_opts);
2610 qemu_add_opts(&qemu_numa_opts);
2611 qemu_add_opts(&qemu_icount_opts);
2612 qemu_add_opts(&qemu_semihosting_config_opts);
2613 qemu_add_opts(&qemu_fw_cfg_opts);
2614 qemu_add_opts(&qemu_action_opts);
2615 module_call_init(MODULE_INIT_OPTS);
2617 error_init(argv[0]);
2618 qemu_init_exec_dir(argv[0]);
2620 qemu_init_subsystems();
2622 /* first pass of option parsing */
2623 optind = 1;
2624 while (optind < argc) {
2625 if (argv[optind][0] != '-') {
2626 /* disk image */
2627 optind++;
2628 } else {
2629 const QEMUOption *popt;
2631 popt = lookup_opt(argc, argv, &optarg, &optind);
2632 switch (popt->index) {
2633 case QEMU_OPTION_nouserconfig:
2634 userconfig = false;
2635 break;
2640 if (userconfig) {
2641 if (qemu_read_default_config_file() < 0) {
2642 exit(1);
2646 /* second pass of option parsing */
2647 optind = 1;
2648 for(;;) {
2649 if (optind >= argc)
2650 break;
2651 if (argv[optind][0] != '-') {
2652 loc_set_cmdline(argv, optind, 1);
2653 drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2654 } else {
2655 const QEMUOption *popt;
2657 popt = lookup_opt(argc, argv, &optarg, &optind);
2658 if (!(popt->arch_mask & arch_type)) {
2659 error_report("Option not supported for this target");
2660 exit(1);
2662 switch(popt->index) {
2663 case QEMU_OPTION_cpu:
2664 /* hw initialization will check this */
2665 cpu_option = optarg;
2666 break;
2667 case QEMU_OPTION_hda:
2668 case QEMU_OPTION_hdb:
2669 case QEMU_OPTION_hdc:
2670 case QEMU_OPTION_hdd:
2671 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2672 HD_OPTS);
2673 break;
2674 case QEMU_OPTION_blockdev:
2676 Visitor *v;
2677 BlockdevOptionsQueueEntry *bdo;
2679 v = qobject_input_visitor_new_str(optarg, "driver",
2680 &error_fatal);
2682 bdo = g_new(BlockdevOptionsQueueEntry, 1);
2683 visit_type_BlockdevOptions(v, NULL, &bdo->bdo,
2684 &error_fatal);
2685 visit_free(v);
2686 loc_save(&bdo->loc);
2687 QSIMPLEQ_INSERT_TAIL(&bdo_queue, bdo, entry);
2688 break;
2690 case QEMU_OPTION_drive:
2691 if (drive_def(optarg) == NULL) {
2692 exit(1);
2694 break;
2695 case QEMU_OPTION_set:
2696 if (qemu_set_option(optarg) != 0)
2697 exit(1);
2698 break;
2699 case QEMU_OPTION_global:
2700 if (qemu_global_option(optarg) != 0)
2701 exit(1);
2702 break;
2703 case QEMU_OPTION_mtdblock:
2704 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2705 break;
2706 case QEMU_OPTION_sd:
2707 drive_add(IF_SD, -1, optarg, SD_OPTS);
2708 break;
2709 case QEMU_OPTION_pflash:
2710 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2711 break;
2712 case QEMU_OPTION_snapshot:
2714 Error *blocker = NULL;
2715 snapshot = 1;
2716 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
2717 "-snapshot");
2718 replay_add_blocker(blocker);
2720 break;
2721 case QEMU_OPTION_numa:
2722 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
2723 optarg, true);
2724 if (!opts) {
2725 exit(1);
2727 break;
2728 case QEMU_OPTION_display:
2729 parse_display(optarg);
2730 break;
2731 case QEMU_OPTION_nographic:
2732 olist = qemu_find_opts("machine");
2733 qemu_opts_parse_noisily(olist, "graphics=off", false);
2734 nographic = true;
2735 dpy.type = DISPLAY_TYPE_NONE;
2736 break;
2737 case QEMU_OPTION_curses:
2738 #ifdef CONFIG_CURSES
2739 dpy.type = DISPLAY_TYPE_CURSES;
2740 #else
2741 error_report("curses or iconv support is disabled");
2742 exit(1);
2743 #endif
2744 break;
2745 case QEMU_OPTION_portrait:
2746 graphic_rotate = 90;
2747 break;
2748 case QEMU_OPTION_rotate:
2749 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2750 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2751 graphic_rotate != 180 && graphic_rotate != 270) {
2752 error_report("only 90, 180, 270 deg rotation is available");
2753 exit(1);
2755 break;
2756 case QEMU_OPTION_kernel:
2757 qemu_opts_set(qemu_find_opts("machine"), "kernel", optarg, &error_abort);
2758 break;
2759 case QEMU_OPTION_initrd:
2760 qemu_opts_set(qemu_find_opts("machine"), "initrd", optarg, &error_abort);
2761 break;
2762 case QEMU_OPTION_append:
2763 qemu_opts_set(qemu_find_opts("machine"), "append", optarg, &error_abort);
2764 break;
2765 case QEMU_OPTION_dtb:
2766 qemu_opts_set(qemu_find_opts("machine"), "dtb", optarg, &error_abort);
2767 break;
2768 case QEMU_OPTION_cdrom:
2769 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
2770 break;
2771 case QEMU_OPTION_boot:
2772 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
2773 optarg, true);
2774 if (!opts) {
2775 exit(1);
2777 break;
2778 case QEMU_OPTION_fda:
2779 case QEMU_OPTION_fdb:
2780 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2781 optarg, FD_OPTS);
2782 break;
2783 case QEMU_OPTION_no_fd_bootchk:
2784 fd_bootchk = 0;
2785 break;
2786 case QEMU_OPTION_netdev:
2787 default_net = 0;
2788 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2789 exit(1);
2791 break;
2792 case QEMU_OPTION_nic:
2793 default_net = 0;
2794 if (net_client_parse(qemu_find_opts("nic"), optarg) == -1) {
2795 exit(1);
2797 break;
2798 case QEMU_OPTION_net:
2799 default_net = 0;
2800 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2801 exit(1);
2803 break;
2804 #ifdef CONFIG_LIBISCSI
2805 case QEMU_OPTION_iscsi:
2806 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
2807 optarg, false);
2808 if (!opts) {
2809 exit(1);
2811 break;
2812 #endif
2813 case QEMU_OPTION_audio_help:
2814 audio_legacy_help();
2815 exit (0);
2816 break;
2817 case QEMU_OPTION_audiodev:
2818 audio_parse_option(optarg);
2819 break;
2820 case QEMU_OPTION_soundhw:
2821 select_soundhw (optarg);
2822 break;
2823 case QEMU_OPTION_h:
2824 help(0);
2825 break;
2826 case QEMU_OPTION_version:
2827 version();
2828 exit(0);
2829 break;
2830 case QEMU_OPTION_m:
2831 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
2832 optarg, true);
2833 if (!opts) {
2834 exit(EXIT_FAILURE);
2836 break;
2837 #ifdef CONFIG_TPM
2838 case QEMU_OPTION_tpmdev:
2839 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
2840 exit(1);
2842 break;
2843 #endif
2844 case QEMU_OPTION_mempath:
2845 mem_path = optarg;
2846 break;
2847 case QEMU_OPTION_mem_prealloc:
2848 mem_prealloc = 1;
2849 break;
2850 case QEMU_OPTION_d:
2851 log_mask = optarg;
2852 break;
2853 case QEMU_OPTION_D:
2854 log_file = optarg;
2855 break;
2856 case QEMU_OPTION_DFILTER:
2857 qemu_set_dfilter_ranges(optarg, &error_fatal);
2858 break;
2859 case QEMU_OPTION_seed:
2860 qemu_guest_random_seed_main(optarg, &error_fatal);
2861 break;
2862 case QEMU_OPTION_s:
2863 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
2864 break;
2865 case QEMU_OPTION_gdb:
2866 add_device_config(DEV_GDB, optarg);
2867 break;
2868 case QEMU_OPTION_L:
2869 if (is_help_option(optarg)) {
2870 list_data_dirs = true;
2871 } else {
2872 qemu_add_data_dir(g_strdup(optarg));
2874 break;
2875 case QEMU_OPTION_bios:
2876 qemu_opts_set(qemu_find_opts("machine"), "firmware", optarg, &error_abort);
2877 break;
2878 case QEMU_OPTION_singlestep:
2879 singlestep = 1;
2880 break;
2881 case QEMU_OPTION_S:
2882 autostart = 0;
2883 break;
2884 case QEMU_OPTION_k:
2885 keyboard_layout = optarg;
2886 break;
2887 case QEMU_OPTION_vga:
2888 vga_model = optarg;
2889 default_vga = 0;
2890 break;
2891 case QEMU_OPTION_g:
2893 const char *p;
2894 int w, h, depth;
2895 p = optarg;
2896 w = strtol(p, (char **)&p, 10);
2897 if (w <= 0) {
2898 graphic_error:
2899 error_report("invalid resolution or depth");
2900 exit(1);
2902 if (*p != 'x')
2903 goto graphic_error;
2904 p++;
2905 h = strtol(p, (char **)&p, 10);
2906 if (h <= 0)
2907 goto graphic_error;
2908 if (*p == 'x') {
2909 p++;
2910 depth = strtol(p, (char **)&p, 10);
2911 if (depth != 1 && depth != 2 && depth != 4 &&
2912 depth != 8 && depth != 15 && depth != 16 &&
2913 depth != 24 && depth != 32)
2914 goto graphic_error;
2915 } else if (*p == '\0') {
2916 depth = graphic_depth;
2917 } else {
2918 goto graphic_error;
2921 graphic_width = w;
2922 graphic_height = h;
2923 graphic_depth = depth;
2925 break;
2926 case QEMU_OPTION_echr:
2928 char *r;
2929 term_escape_char = strtol(optarg, &r, 0);
2930 if (r == optarg)
2931 printf("Bad argument to echr\n");
2932 break;
2934 case QEMU_OPTION_monitor:
2935 default_monitor = 0;
2936 if (strncmp(optarg, "none", 4)) {
2937 monitor_parse(optarg, "readline", false);
2939 break;
2940 case QEMU_OPTION_qmp:
2941 monitor_parse(optarg, "control", false);
2942 default_monitor = 0;
2943 break;
2944 case QEMU_OPTION_qmp_pretty:
2945 monitor_parse(optarg, "control", true);
2946 default_monitor = 0;
2947 break;
2948 case QEMU_OPTION_mon:
2949 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
2950 true);
2951 if (!opts) {
2952 exit(1);
2954 default_monitor = 0;
2955 break;
2956 case QEMU_OPTION_chardev:
2957 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
2958 optarg, true);
2959 if (!opts) {
2960 exit(1);
2962 break;
2963 case QEMU_OPTION_fsdev:
2964 olist = qemu_find_opts("fsdev");
2965 if (!olist) {
2966 error_report("fsdev support is disabled");
2967 exit(1);
2969 opts = qemu_opts_parse_noisily(olist, optarg, true);
2970 if (!opts) {
2971 exit(1);
2973 break;
2974 case QEMU_OPTION_virtfs: {
2975 QemuOpts *fsdev;
2976 QemuOpts *device;
2977 const char *writeout, *sock_fd, *socket, *path, *security_model,
2978 *multidevs;
2980 olist = qemu_find_opts("virtfs");
2981 if (!olist) {
2982 error_report("virtfs support is disabled");
2983 exit(1);
2985 opts = qemu_opts_parse_noisily(olist, optarg, true);
2986 if (!opts) {
2987 exit(1);
2990 if (qemu_opt_get(opts, "fsdriver") == NULL ||
2991 qemu_opt_get(opts, "mount_tag") == NULL) {
2992 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
2993 exit(1);
2995 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
2996 qemu_opts_id(opts) ?:
2997 qemu_opt_get(opts, "mount_tag"),
2998 1, NULL);
2999 if (!fsdev) {
3000 error_report("duplicate or invalid fsdev id: %s",
3001 qemu_opt_get(opts, "mount_tag"));
3002 exit(1);
3005 writeout = qemu_opt_get(opts, "writeout");
3006 if (writeout) {
3007 #ifdef CONFIG_SYNC_FILE_RANGE
3008 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3009 #else
3010 error_report("writeout=immediate not supported "
3011 "on this platform");
3012 exit(1);
3013 #endif
3015 qemu_opt_set(fsdev, "fsdriver",
3016 qemu_opt_get(opts, "fsdriver"), &error_abort);
3017 path = qemu_opt_get(opts, "path");
3018 if (path) {
3019 qemu_opt_set(fsdev, "path", path, &error_abort);
3021 security_model = qemu_opt_get(opts, "security_model");
3022 if (security_model) {
3023 qemu_opt_set(fsdev, "security_model", security_model,
3024 &error_abort);
3026 socket = qemu_opt_get(opts, "socket");
3027 if (socket) {
3028 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3030 sock_fd = qemu_opt_get(opts, "sock_fd");
3031 if (sock_fd) {
3032 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3035 qemu_opt_set_bool(fsdev, "readonly",
3036 qemu_opt_get_bool(opts, "readonly", 0),
3037 &error_abort);
3038 multidevs = qemu_opt_get(opts, "multidevs");
3039 if (multidevs) {
3040 qemu_opt_set(fsdev, "multidevs", multidevs, &error_abort);
3042 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3043 &error_abort);
3044 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3045 qemu_opt_set(device, "fsdev",
3046 qemu_opts_id(fsdev), &error_abort);
3047 qemu_opt_set(device, "mount_tag",
3048 qemu_opt_get(opts, "mount_tag"), &error_abort);
3049 break;
3051 case QEMU_OPTION_serial:
3052 add_device_config(DEV_SERIAL, optarg);
3053 default_serial = 0;
3054 if (strncmp(optarg, "mon:", 4) == 0) {
3055 default_monitor = 0;
3057 break;
3058 case QEMU_OPTION_watchdog:
3059 if (watchdog) {
3060 error_report("only one watchdog option may be given");
3061 exit(1);
3063 watchdog = optarg;
3064 break;
3065 case QEMU_OPTION_action:
3066 olist = qemu_find_opts("action");
3067 if (!qemu_opts_parse_noisily(olist, optarg, false)) {
3068 exit(1);
3070 break;
3071 case QEMU_OPTION_watchdog_action:
3072 if (select_watchdog_action(optarg) == -1) {
3073 error_report("unknown -watchdog-action parameter");
3074 exit(1);
3076 break;
3077 case QEMU_OPTION_parallel:
3078 add_device_config(DEV_PARALLEL, optarg);
3079 default_parallel = 0;
3080 if (strncmp(optarg, "mon:", 4) == 0) {
3081 default_monitor = 0;
3083 break;
3084 case QEMU_OPTION_debugcon:
3085 add_device_config(DEV_DEBUGCON, optarg);
3086 break;
3087 case QEMU_OPTION_loadvm:
3088 loadvm = optarg;
3089 break;
3090 case QEMU_OPTION_full_screen:
3091 dpy.has_full_screen = true;
3092 dpy.full_screen = true;
3093 break;
3094 case QEMU_OPTION_alt_grab:
3095 alt_grab = 1;
3096 break;
3097 case QEMU_OPTION_ctrl_grab:
3098 ctrl_grab = 1;
3099 break;
3100 case QEMU_OPTION_no_quit:
3101 dpy.has_window_close = true;
3102 dpy.window_close = false;
3103 break;
3104 case QEMU_OPTION_sdl:
3105 #ifdef CONFIG_SDL
3106 dpy.type = DISPLAY_TYPE_SDL;
3107 break;
3108 #else
3109 error_report("SDL support is disabled");
3110 exit(1);
3111 #endif
3112 case QEMU_OPTION_pidfile:
3113 pid_file = optarg;
3114 break;
3115 case QEMU_OPTION_win2k_hack:
3116 win2k_install_hack = 1;
3117 break;
3118 case QEMU_OPTION_acpitable:
3119 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3120 optarg, true);
3121 if (!opts) {
3122 exit(1);
3124 acpi_table_add(opts, &error_fatal);
3125 break;
3126 case QEMU_OPTION_smbios:
3127 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3128 optarg, false);
3129 if (!opts) {
3130 exit(1);
3132 smbios_entry_add(opts, &error_fatal);
3133 break;
3134 case QEMU_OPTION_fwcfg:
3135 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3136 optarg, true);
3137 if (opts == NULL) {
3138 exit(1);
3140 break;
3141 case QEMU_OPTION_preconfig:
3142 preconfig_requested = true;
3143 break;
3144 case QEMU_OPTION_enable_kvm:
3145 olist = qemu_find_opts("machine");
3146 qemu_opts_parse_noisily(olist, "accel=kvm", false);
3147 break;
3148 case QEMU_OPTION_M:
3149 case QEMU_OPTION_machine:
3150 olist = qemu_find_opts("machine");
3151 opts = qemu_opts_parse_noisily(olist, optarg, true);
3152 if (!opts) {
3153 exit(1);
3155 break;
3156 case QEMU_OPTION_accel:
3157 accel_opts = qemu_opts_parse_noisily(qemu_find_opts("accel"),
3158 optarg, true);
3159 optarg = qemu_opt_get(accel_opts, "accel");
3160 if (!optarg || is_help_option(optarg)) {
3161 printf("Accelerators supported in QEMU binary:\n");
3162 GSList *el, *accel_list = object_class_get_list(TYPE_ACCEL,
3163 false);
3164 for (el = accel_list; el; el = el->next) {
3165 gchar *typename = g_strdup(object_class_get_name(
3166 OBJECT_CLASS(el->data)));
3167 /* omit qtest which is used for tests only */
3168 if (g_strcmp0(typename, ACCEL_CLASS_NAME("qtest")) &&
3169 g_str_has_suffix(typename, ACCEL_CLASS_SUFFIX)) {
3170 gchar **optname = g_strsplit(typename,
3171 ACCEL_CLASS_SUFFIX, 0);
3172 printf("%s\n", optname[0]);
3173 g_strfreev(optname);
3175 g_free(typename);
3177 g_slist_free(accel_list);
3178 exit(0);
3180 break;
3181 case QEMU_OPTION_usb:
3182 olist = qemu_find_opts("machine");
3183 qemu_opts_parse_noisily(olist, "usb=on", false);
3184 break;
3185 case QEMU_OPTION_usbdevice:
3186 error_report("'-usbdevice' is deprecated, please use "
3187 "'-device usb-...' instead");
3188 olist = qemu_find_opts("machine");
3189 qemu_opts_parse_noisily(olist, "usb=on", false);
3190 add_device_config(DEV_USB, optarg);
3191 break;
3192 case QEMU_OPTION_device:
3193 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3194 optarg, true)) {
3195 exit(1);
3197 break;
3198 case QEMU_OPTION_smp:
3199 if (!qemu_opts_parse_noisily(qemu_find_opts("smp-opts"),
3200 optarg, true)) {
3201 exit(1);
3203 break;
3204 case QEMU_OPTION_vnc:
3205 vnc_parse(optarg);
3206 break;
3207 case QEMU_OPTION_no_acpi:
3208 olist = qemu_find_opts("machine");
3209 qemu_opts_parse_noisily(olist, "acpi=off", false);
3210 break;
3211 case QEMU_OPTION_no_hpet:
3212 olist = qemu_find_opts("machine");
3213 qemu_opts_parse_noisily(olist, "hpet=off", false);
3214 break;
3215 case QEMU_OPTION_no_reboot:
3216 olist = qemu_find_opts("action");
3217 qemu_opts_parse_noisily(olist, "reboot=shutdown", false);
3218 break;
3219 case QEMU_OPTION_no_shutdown:
3220 olist = qemu_find_opts("action");
3221 qemu_opts_parse_noisily(olist, "shutdown=pause", false);
3222 break;
3223 case QEMU_OPTION_uuid:
3224 if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
3225 error_report("failed to parse UUID string: wrong format");
3226 exit(1);
3228 qemu_uuid_set = true;
3229 break;
3230 case QEMU_OPTION_option_rom:
3231 if (nb_option_roms >= MAX_OPTION_ROMS) {
3232 error_report("too many option ROMs");
3233 exit(1);
3235 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3236 optarg, true);
3237 if (!opts) {
3238 exit(1);
3240 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3241 option_rom[nb_option_roms].bootindex =
3242 qemu_opt_get_number(opts, "bootindex", -1);
3243 if (!option_rom[nb_option_roms].name) {
3244 error_report("Option ROM file is not specified");
3245 exit(1);
3247 nb_option_roms++;
3248 break;
3249 case QEMU_OPTION_semihosting:
3250 qemu_semihosting_enable();
3251 break;
3252 case QEMU_OPTION_semihosting_config:
3253 if (qemu_semihosting_config_options(optarg) != 0) {
3254 exit(1);
3256 break;
3257 case QEMU_OPTION_name:
3258 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3259 optarg, true);
3260 if (!opts) {
3261 exit(1);
3263 /* Capture guest name if -msg guest-name is used later */
3264 error_guest_name = qemu_opt_get(opts, "guest");
3265 break;
3266 case QEMU_OPTION_prom_env:
3267 if (nb_prom_envs >= MAX_PROM_ENVS) {
3268 error_report("too many prom variables");
3269 exit(1);
3271 prom_envs[nb_prom_envs] = optarg;
3272 nb_prom_envs++;
3273 break;
3274 case QEMU_OPTION_old_param:
3275 old_param = 1;
3276 break;
3277 case QEMU_OPTION_rtc:
3278 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3279 false);
3280 if (!opts) {
3281 exit(1);
3283 break;
3284 case QEMU_OPTION_icount:
3285 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3286 optarg, true);
3287 if (!icount_opts) {
3288 exit(1);
3290 break;
3291 case QEMU_OPTION_incoming:
3292 if (!incoming) {
3293 runstate_set(RUN_STATE_INMIGRATE);
3295 incoming = optarg;
3296 break;
3297 case QEMU_OPTION_only_migratable:
3298 only_migratable = 1;
3299 break;
3300 case QEMU_OPTION_nodefaults:
3301 has_defaults = 0;
3302 break;
3303 case QEMU_OPTION_xen_domid:
3304 if (!(xen_available())) {
3305 error_report("Option not supported for this target");
3306 exit(1);
3308 xen_domid = atoi(optarg);
3309 break;
3310 case QEMU_OPTION_xen_attach:
3311 if (!(xen_available())) {
3312 error_report("Option not supported for this target");
3313 exit(1);
3315 xen_mode = XEN_ATTACH;
3316 break;
3317 case QEMU_OPTION_xen_domid_restrict:
3318 if (!(xen_available())) {
3319 error_report("Option not supported for this target");
3320 exit(1);
3322 xen_domid_restrict = true;
3323 break;
3324 case QEMU_OPTION_trace:
3325 trace_opt_parse(optarg);
3326 break;
3327 case QEMU_OPTION_plugin:
3328 qemu_plugin_opt_parse(optarg, &plugin_list);
3329 break;
3330 case QEMU_OPTION_readconfig:
3332 int ret = qemu_read_config_file(optarg);
3333 if (ret < 0) {
3334 error_report("read config %s: %s", optarg,
3335 strerror(-ret));
3336 exit(1);
3338 break;
3340 case QEMU_OPTION_spice:
3341 olist = qemu_find_opts_err("spice", NULL);
3342 if (!olist) {
3343 ui_module_load_one("spice-core");
3344 olist = qemu_find_opts("spice");
3346 if (!olist) {
3347 error_report("spice support is disabled");
3348 exit(1);
3350 opts = qemu_opts_parse_noisily(olist, optarg, false);
3351 if (!opts) {
3352 exit(1);
3354 display_remote++;
3355 break;
3356 case QEMU_OPTION_writeconfig:
3358 FILE *fp;
3359 if (strcmp(optarg, "-") == 0) {
3360 fp = stdout;
3361 } else {
3362 fp = fopen(optarg, "w");
3363 if (fp == NULL) {
3364 error_report("open %s: %s", optarg,
3365 strerror(errno));
3366 exit(1);
3369 qemu_config_write(fp);
3370 if (fp != stdout) {
3371 fclose(fp);
3373 break;
3375 case QEMU_OPTION_qtest:
3376 qtest_chrdev = optarg;
3377 break;
3378 case QEMU_OPTION_qtest_log:
3379 qtest_log = optarg;
3380 break;
3381 case QEMU_OPTION_sandbox:
3382 olist = qemu_find_opts("sandbox");
3383 if (!olist) {
3384 #ifndef CONFIG_SECCOMP
3385 error_report("-sandbox support is not enabled "
3386 "in this QEMU binary");
3387 #endif
3388 exit(1);
3391 opts = qemu_opts_parse_noisily(olist, optarg, true);
3392 if (!opts) {
3393 exit(1);
3395 break;
3396 case QEMU_OPTION_add_fd:
3397 #ifndef _WIN32
3398 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
3399 optarg, false);
3400 if (!opts) {
3401 exit(1);
3403 #else
3404 error_report("File descriptor passing is disabled on this "
3405 "platform");
3406 exit(1);
3407 #endif
3408 break;
3409 case QEMU_OPTION_object:
3410 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
3411 optarg, true);
3412 if (!opts) {
3413 exit(1);
3415 break;
3416 case QEMU_OPTION_overcommit:
3417 opts = qemu_opts_parse_noisily(qemu_find_opts("overcommit"),
3418 optarg, false);
3419 if (!opts) {
3420 exit(1);
3422 enable_mlock = qemu_opt_get_bool(opts, "mem-lock", false);
3423 enable_cpu_pm = qemu_opt_get_bool(opts, "cpu-pm", false);
3424 break;
3425 case QEMU_OPTION_msg:
3426 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
3427 false);
3428 if (!opts) {
3429 exit(1);
3431 configure_msg(opts);
3432 break;
3433 case QEMU_OPTION_dump_vmstate:
3434 if (vmstate_dump_file) {
3435 error_report("only one '-dump-vmstate' "
3436 "option may be given");
3437 exit(1);
3439 vmstate_dump_file = fopen(optarg, "w");
3440 if (vmstate_dump_file == NULL) {
3441 error_report("open %s: %s", optarg, strerror(errno));
3442 exit(1);
3444 break;
3445 case QEMU_OPTION_enable_sync_profile:
3446 qsp_enable();
3447 break;
3448 case QEMU_OPTION_nouserconfig:
3449 /* Nothing to be parsed here. Especially, do not error out below. */
3450 break;
3451 default:
3452 if (os_parse_cmd_args(popt->index, optarg)) {
3453 error_report("Option not supported in this build");
3454 exit(1);
3460 * Clear error location left behind by the loop.
3461 * Best done right after the loop. Do not insert code here!
3463 loc_set_none();
3465 qemu_validate_options();
3466 qemu_process_sugar_options();
3469 * These options affect everything else and should be processed
3470 * before daemonizing.
3472 qemu_process_early_options();
3474 qemu_process_help_options();
3475 qemu_maybe_daemonize(pid_file);
3477 qemu_init_main_loop(&error_fatal);
3478 cpu_timers_init();
3480 user_register_global_props();
3481 replay_configure(icount_opts);
3483 configure_rtc(qemu_find_opts_singleton("rtc"));
3485 qemu_create_machine(select_machine());
3487 suspend_mux_open();
3489 qemu_disable_default_devices();
3490 qemu_create_default_devices();
3491 qemu_create_early_backends();
3493 qemu_apply_machine_options();
3494 phase_advance(PHASE_MACHINE_CREATED);
3497 * Note: uses machine properties such as kernel-irqchip, must run
3498 * after machine_set_property().
3500 configure_accelerators(argv[0]);
3501 phase_advance(PHASE_ACCEL_CREATED);
3504 * Beware, QOM objects created before this point miss global and
3505 * compat properties.
3507 * Global properties get set up by qdev_prop_register_global(),
3508 * called from user_register_global_props(), and certain option
3509 * desugaring. Also in CPU feature desugaring (buried in
3510 * parse_cpu_option()), which happens below this point, but may
3511 * only target the CPU type, which can only be created after
3512 * parse_cpu_option() returned the type.
3514 * Machine compat properties: object_set_machine_compat_props().
3515 * Accelerator compat props: object_set_accelerator_compat_props(),
3516 * called from do_configure_accelerator().
3519 machine_class = MACHINE_GET_CLASS(current_machine);
3520 if (!qtest_enabled() && machine_class->deprecation_reason) {
3521 error_report("Machine type '%s' is deprecated: %s",
3522 machine_class->name, machine_class->deprecation_reason);
3526 * Note: creates a QOM object, must run only after global and
3527 * compat properties have been set up.
3529 migration_object_init();
3531 qemu_create_late_backends();
3533 /* parse features once if machine provides default cpu_type */
3534 current_machine->cpu_type = machine_class->default_cpu_type;
3535 if (cpu_option) {
3536 current_machine->cpu_type = parse_cpu_option(cpu_option);
3538 /* NB: for machine none cpu_type could STILL be NULL here! */
3539 accel_init_interfaces(ACCEL_GET_CLASS(current_machine->accelerator));
3541 qemu_resolve_machine_memdev();
3542 parse_numa_opts(current_machine);
3544 if (vmstate_dump_file) {
3545 /* dump and exit */
3546 dump_vmstate_json_to_file(vmstate_dump_file);
3547 exit(0);
3550 if (!preconfig_requested) {
3551 qmp_x_exit_preconfig(&error_fatal);
3553 qemu_init_displays();
3554 accel_setup_post(current_machine);
3555 os_setup_post();
3556 resume_mux_open();