Merge remote-tracking branch 'remotes/pmaydell/tags/pull-target-arm-20210323' into...
[qemu/ar7.git] / softmmu / vl.c
blobaadb52613888ef6ac1fe7ec3a0382e8b4cb50ef6
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/compat-policy.h"
33 #include "qapi/error.h"
34 #include "qapi/qmp/qdict.h"
35 #include "qapi/qmp/qjson.h"
36 #include "qemu-version.h"
37 #include "qemu/cutils.h"
38 #include "qemu/help_option.h"
39 #include "qemu/uuid.h"
40 #include "sysemu/reset.h"
41 #include "sysemu/runstate.h"
42 #include "sysemu/runstate-action.h"
43 #include "sysemu/seccomp.h"
44 #include "sysemu/tcg.h"
45 #include "sysemu/xen.h"
47 #include "qemu/error-report.h"
48 #include "qemu/sockets.h"
49 #include "qemu/accel.h"
50 #include "hw/usb.h"
51 #include "hw/isa/isa.h"
52 #include "hw/scsi/scsi.h"
53 #include "hw/display/vga.h"
54 #include "sysemu/watchdog.h"
55 #include "hw/firmware/smbios.h"
56 #include "hw/acpi/acpi.h"
57 #include "hw/xen/xen.h"
58 #include "hw/loader.h"
59 #include "monitor/qdev.h"
60 #include "net/net.h"
61 #include "net/slirp.h"
62 #include "monitor/monitor.h"
63 #include "ui/console.h"
64 #include "ui/input.h"
65 #include "sysemu/sysemu.h"
66 #include "sysemu/numa.h"
67 #include "sysemu/hostmem.h"
68 #include "exec/gdbstub.h"
69 #include "qemu/timer.h"
70 #include "chardev/char.h"
71 #include "qemu/bitmap.h"
72 #include "qemu/log.h"
73 #include "sysemu/blockdev.h"
74 #include "hw/block/block.h"
75 #include "hw/i386/x86.h"
76 #include "hw/i386/pc.h"
77 #include "migration/misc.h"
78 #include "migration/snapshot.h"
79 #include "sysemu/tpm.h"
80 #include "sysemu/dma.h"
81 #include "hw/audio/soundhw.h"
82 #include "audio/audio.h"
83 #include "sysemu/cpus.h"
84 #include "sysemu/cpu-timers.h"
85 #include "migration/colo.h"
86 #include "migration/postcopy-ram.h"
87 #include "sysemu/kvm.h"
88 #include "sysemu/hax.h"
89 #include "qapi/qobject-input-visitor.h"
90 #include "qemu/option.h"
91 #include "qemu/config-file.h"
92 #include "qemu-options.h"
93 #include "qemu/main-loop.h"
94 #ifdef CONFIG_VIRTFS
95 #include "fsdev/qemu-fsdev.h"
96 #endif
97 #include "sysemu/qtest.h"
99 #include "disas/disas.h"
101 #include "trace.h"
102 #include "trace/control.h"
103 #include "qemu/plugin.h"
104 #include "qemu/queue.h"
105 #include "sysemu/arch_init.h"
106 #include "exec/confidential-guest-support.h"
108 #include "ui/qemu-spice.h"
109 #include "qapi/string-input-visitor.h"
110 #include "qapi/opts-visitor.h"
111 #include "qapi/clone-visitor.h"
112 #include "qom/object_interfaces.h"
113 #include "semihosting/semihost.h"
114 #include "crypto/init.h"
115 #include "sysemu/replay.h"
116 #include "qapi/qapi-events-run-state.h"
117 #include "qapi/qapi-visit-block-core.h"
118 #include "qapi/qapi-visit-compat.h"
119 #include "qapi/qapi-visit-ui.h"
120 #include "qapi/qapi-commands-block-core.h"
121 #include "qapi/qapi-commands-migration.h"
122 #include "qapi/qapi-commands-misc.h"
123 #include "qapi/qapi-visit-qom.h"
124 #include "qapi/qapi-commands-ui.h"
125 #include "qapi/qmp/qerror.h"
126 #include "sysemu/iothread.h"
127 #include "qemu/guest-random.h"
129 #define MAX_VIRTIO_CONSOLES 1
131 typedef struct BlockdevOptionsQueueEntry {
132 BlockdevOptions *bdo;
133 Location loc;
134 QSIMPLEQ_ENTRY(BlockdevOptionsQueueEntry) entry;
135 } BlockdevOptionsQueueEntry;
137 typedef QSIMPLEQ_HEAD(, BlockdevOptionsQueueEntry) BlockdevOptionsQueue;
139 typedef struct ObjectOption {
140 ObjectOptions *opts;
141 QTAILQ_ENTRY(ObjectOption) next;
142 } ObjectOption;
144 static const char *cpu_option;
145 static const char *mem_path;
146 static const char *incoming;
147 static const char *loadvm;
148 static QTAILQ_HEAD(, ObjectOption) object_opts = QTAILQ_HEAD_INITIALIZER(object_opts);
149 static ram_addr_t maxram_size;
150 static uint64_t ram_slots;
151 static int display_remote;
152 static int snapshot;
153 static bool preconfig_requested;
154 static QemuPluginList plugin_list = QTAILQ_HEAD_INITIALIZER(plugin_list);
155 static BlockdevOptionsQueue bdo_queue = QSIMPLEQ_HEAD_INITIALIZER(bdo_queue);
156 static bool nographic = false;
157 static int mem_prealloc; /* force preallocation of physical target memory */
158 static ram_addr_t ram_size;
159 static const char *vga_model = NULL;
160 static DisplayOptions dpy;
161 static int num_serial_hds;
162 static Chardev **serial_hds;
163 static const char *log_mask;
164 static const char *log_file;
165 static bool list_data_dirs;
166 static const char *watchdog;
167 static const char *qtest_chrdev;
168 static const char *qtest_log;
170 static int has_defaults = 1;
171 static int default_serial = 1;
172 static int default_parallel = 1;
173 static int default_monitor = 1;
174 static int default_floppy = 1;
175 static int default_cdrom = 1;
176 static int default_sdcard = 1;
177 static int default_vga = 1;
178 static int default_net = 1;
180 static struct {
181 const char *driver;
182 int *flag;
183 } default_list[] = {
184 { .driver = "isa-serial", .flag = &default_serial },
185 { .driver = "isa-parallel", .flag = &default_parallel },
186 { .driver = "isa-fdc", .flag = &default_floppy },
187 { .driver = "floppy", .flag = &default_floppy },
188 { .driver = "ide-cd", .flag = &default_cdrom },
189 { .driver = "ide-hd", .flag = &default_cdrom },
190 { .driver = "scsi-cd", .flag = &default_cdrom },
191 { .driver = "scsi-hd", .flag = &default_cdrom },
192 { .driver = "VGA", .flag = &default_vga },
193 { .driver = "isa-vga", .flag = &default_vga },
194 { .driver = "cirrus-vga", .flag = &default_vga },
195 { .driver = "isa-cirrus-vga", .flag = &default_vga },
196 { .driver = "vmware-svga", .flag = &default_vga },
197 { .driver = "qxl-vga", .flag = &default_vga },
198 { .driver = "virtio-vga", .flag = &default_vga },
199 { .driver = "ati-vga", .flag = &default_vga },
200 { .driver = "vhost-user-vga", .flag = &default_vga },
203 static QemuOptsList qemu_rtc_opts = {
204 .name = "rtc",
205 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
206 .merge_lists = true,
207 .desc = {
209 .name = "base",
210 .type = QEMU_OPT_STRING,
212 .name = "clock",
213 .type = QEMU_OPT_STRING,
215 .name = "driftfix",
216 .type = QEMU_OPT_STRING,
218 { /* end of list */ }
222 static QemuOptsList qemu_option_rom_opts = {
223 .name = "option-rom",
224 .implied_opt_name = "romfile",
225 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
226 .desc = {
228 .name = "bootindex",
229 .type = QEMU_OPT_NUMBER,
230 }, {
231 .name = "romfile",
232 .type = QEMU_OPT_STRING,
234 { /* end of list */ }
238 static QemuOptsList qemu_machine_opts = {
239 .name = "machine",
240 .implied_opt_name = "type",
241 .merge_lists = true,
242 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
243 .desc = {
245 * no elements => accept any
246 * sanity checking will happen later
247 * when setting machine properties
253 static QemuOptsList qemu_accel_opts = {
254 .name = "accel",
255 .implied_opt_name = "accel",
256 .head = QTAILQ_HEAD_INITIALIZER(qemu_accel_opts.head),
257 .desc = {
259 * no elements => accept any
260 * sanity checking will happen later
261 * when setting accelerator properties
267 static QemuOptsList qemu_boot_opts = {
268 .name = "boot-opts",
269 .implied_opt_name = "order",
270 .merge_lists = true,
271 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
272 .desc = {
274 .name = "order",
275 .type = QEMU_OPT_STRING,
276 }, {
277 .name = "once",
278 .type = QEMU_OPT_STRING,
279 }, {
280 .name = "menu",
281 .type = QEMU_OPT_BOOL,
282 }, {
283 .name = "splash",
284 .type = QEMU_OPT_STRING,
285 }, {
286 .name = "splash-time",
287 .type = QEMU_OPT_NUMBER,
288 }, {
289 .name = "reboot-timeout",
290 .type = QEMU_OPT_NUMBER,
291 }, {
292 .name = "strict",
293 .type = QEMU_OPT_BOOL,
295 { /*End of list */ }
299 static QemuOptsList qemu_add_fd_opts = {
300 .name = "add-fd",
301 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
302 .desc = {
304 .name = "fd",
305 .type = QEMU_OPT_NUMBER,
306 .help = "file descriptor of which a duplicate is added to fd set",
308 .name = "set",
309 .type = QEMU_OPT_NUMBER,
310 .help = "ID of the fd set to add fd to",
312 .name = "opaque",
313 .type = QEMU_OPT_STRING,
314 .help = "free-form string used to describe fd",
316 { /* end of list */ }
320 static QemuOptsList qemu_object_opts = {
321 .name = "object",
322 .implied_opt_name = "qom-type",
323 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
324 .desc = {
329 static QemuOptsList qemu_tpmdev_opts = {
330 .name = "tpmdev",
331 .implied_opt_name = "type",
332 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
333 .desc = {
334 /* options are defined in the TPM backends */
335 { /* end of list */ }
339 static QemuOptsList qemu_overcommit_opts = {
340 .name = "overcommit",
341 .head = QTAILQ_HEAD_INITIALIZER(qemu_overcommit_opts.head),
342 .desc = {
344 .name = "mem-lock",
345 .type = QEMU_OPT_BOOL,
348 .name = "cpu-pm",
349 .type = QEMU_OPT_BOOL,
351 { /* end of list */ }
355 static QemuOptsList qemu_msg_opts = {
356 .name = "msg",
357 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
358 .desc = {
360 .name = "timestamp",
361 .type = QEMU_OPT_BOOL,
364 .name = "guest-name",
365 .type = QEMU_OPT_BOOL,
366 .help = "Prepends guest name for error messages but only if "
367 "-name guest is set otherwise option is ignored\n",
369 { /* end of list */ }
373 static QemuOptsList qemu_name_opts = {
374 .name = "name",
375 .implied_opt_name = "guest",
376 .merge_lists = true,
377 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
378 .desc = {
380 .name = "guest",
381 .type = QEMU_OPT_STRING,
382 .help = "Sets the name of the guest.\n"
383 "This name will be displayed in the SDL window caption.\n"
384 "The name will also be used for the VNC server",
385 }, {
386 .name = "process",
387 .type = QEMU_OPT_STRING,
388 .help = "Sets the name of the QEMU process, as shown in top etc",
389 }, {
390 .name = "debug-threads",
391 .type = QEMU_OPT_BOOL,
392 .help = "When enabled, name the individual threads; defaults off.\n"
393 "NOTE: The thread names are for debugging and not a\n"
394 "stable API.",
396 { /* End of list */ }
400 static QemuOptsList qemu_mem_opts = {
401 .name = "memory",
402 .implied_opt_name = "size",
403 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
404 .merge_lists = true,
405 .desc = {
407 .name = "size",
408 .type = QEMU_OPT_SIZE,
411 .name = "slots",
412 .type = QEMU_OPT_NUMBER,
415 .name = "maxmem",
416 .type = QEMU_OPT_SIZE,
418 { /* end of list */ }
422 static QemuOptsList qemu_icount_opts = {
423 .name = "icount",
424 .implied_opt_name = "shift",
425 .merge_lists = true,
426 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
427 .desc = {
429 .name = "shift",
430 .type = QEMU_OPT_STRING,
431 }, {
432 .name = "align",
433 .type = QEMU_OPT_BOOL,
434 }, {
435 .name = "sleep",
436 .type = QEMU_OPT_BOOL,
437 }, {
438 .name = "rr",
439 .type = QEMU_OPT_STRING,
440 }, {
441 .name = "rrfile",
442 .type = QEMU_OPT_STRING,
443 }, {
444 .name = "rrsnapshot",
445 .type = QEMU_OPT_STRING,
447 { /* end of list */ }
451 static QemuOptsList qemu_fw_cfg_opts = {
452 .name = "fw_cfg",
453 .implied_opt_name = "name",
454 .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
455 .desc = {
457 .name = "name",
458 .type = QEMU_OPT_STRING,
459 .help = "Sets the fw_cfg name of the blob to be inserted",
460 }, {
461 .name = "file",
462 .type = QEMU_OPT_STRING,
463 .help = "Sets the name of the file from which "
464 "the fw_cfg blob will be loaded",
465 }, {
466 .name = "string",
467 .type = QEMU_OPT_STRING,
468 .help = "Sets content of the blob to be inserted from a string",
469 }, {
470 .name = "gen_id",
471 .type = QEMU_OPT_STRING,
472 .help = "Sets id of the object generating the fw_cfg blob "
473 "to be inserted",
475 { /* end of list */ }
479 static QemuOptsList qemu_action_opts = {
480 .name = "action",
481 .merge_lists = true,
482 .head = QTAILQ_HEAD_INITIALIZER(qemu_action_opts.head),
483 .desc = {
485 .name = "shutdown",
486 .type = QEMU_OPT_STRING,
488 .name = "reboot",
489 .type = QEMU_OPT_STRING,
491 .name = "panic",
492 .type = QEMU_OPT_STRING,
494 .name = "watchdog",
495 .type = QEMU_OPT_STRING,
497 { /* end of list */ }
502 * Get machine options
504 * Returns: machine options (never null).
506 static QemuOpts *qemu_get_machine_opts(void)
508 return qemu_find_opts_singleton("machine");
511 const char *qemu_get_vm_name(void)
513 return qemu_name;
516 static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
518 const char *driver = qemu_opt_get(opts, "driver");
519 int i;
521 if (!driver)
522 return 0;
523 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
524 if (strcmp(default_list[i].driver, driver) != 0)
525 continue;
526 *(default_list[i].flag) = 0;
528 return 0;
531 static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
533 const char *proc_name;
535 if (qemu_opt_get(opts, "debug-threads")) {
536 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
538 qemu_name = qemu_opt_get(opts, "guest");
540 proc_name = qemu_opt_get(opts, "process");
541 if (proc_name) {
542 os_set_proc_name(proc_name);
545 return 0;
548 bool defaults_enabled(void)
550 return has_defaults;
553 #ifndef _WIN32
554 static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
556 int fd, dupfd, flags;
557 int64_t fdset_id;
558 const char *fd_opaque = NULL;
559 AddfdInfo *fdinfo;
561 fd = qemu_opt_get_number(opts, "fd", -1);
562 fdset_id = qemu_opt_get_number(opts, "set", -1);
563 fd_opaque = qemu_opt_get(opts, "opaque");
565 if (fd < 0) {
566 error_setg(errp, "fd option is required and must be non-negative");
567 return -1;
570 if (fd <= STDERR_FILENO) {
571 error_setg(errp, "fd cannot be a standard I/O stream");
572 return -1;
576 * All fds inherited across exec() necessarily have FD_CLOEXEC
577 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
579 flags = fcntl(fd, F_GETFD);
580 if (flags == -1 || (flags & FD_CLOEXEC)) {
581 error_setg(errp, "fd is not valid or already in use");
582 return -1;
585 if (fdset_id < 0) {
586 error_setg(errp, "set option is required and must be non-negative");
587 return -1;
590 #ifdef F_DUPFD_CLOEXEC
591 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
592 #else
593 dupfd = dup(fd);
594 if (dupfd != -1) {
595 qemu_set_cloexec(dupfd);
597 #endif
598 if (dupfd == -1) {
599 error_setg(errp, "error duplicating fd: %s", strerror(errno));
600 return -1;
603 /* add the duplicate fd, and optionally the opaque string, to the fd set */
604 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
605 &error_abort);
606 g_free(fdinfo);
608 return 0;
611 static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
613 int fd;
615 fd = qemu_opt_get_number(opts, "fd", -1);
616 close(fd);
618 return 0;
620 #endif
622 /***********************************************************/
623 /* QEMU Block devices */
625 #define HD_OPTS "media=disk"
626 #define CDROM_OPTS "media=cdrom"
627 #define FD_OPTS ""
628 #define PFLASH_OPTS ""
629 #define MTD_OPTS ""
630 #define SD_OPTS ""
632 static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
634 BlockInterfaceType *block_default_type = opaque;
636 return drive_new(opts, *block_default_type, errp) == NULL;
639 static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
641 if (qemu_opt_get(opts, "snapshot") == NULL) {
642 qemu_opt_set(opts, "snapshot", "on", &error_abort);
644 return 0;
647 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
648 int index, const char *optstr)
650 QemuOpts *opts;
651 DriveInfo *dinfo;
653 if (!enable || drive_get_by_index(type, index)) {
654 return;
657 opts = drive_add(type, index, NULL, optstr);
658 if (snapshot) {
659 drive_enable_snapshot(NULL, opts, NULL);
662 dinfo = drive_new(opts, type, &error_abort);
663 dinfo->is_default = true;
667 static void configure_blockdev(BlockdevOptionsQueue *bdo_queue,
668 MachineClass *machine_class, int snapshot)
671 * If the currently selected machine wishes to override the
672 * units-per-bus property of its default HBA interface type, do so
673 * now.
675 if (machine_class->units_per_default_bus) {
676 override_max_devs(machine_class->block_default_type,
677 machine_class->units_per_default_bus);
680 /* open the virtual block devices */
681 while (!QSIMPLEQ_EMPTY(bdo_queue)) {
682 BlockdevOptionsQueueEntry *bdo = QSIMPLEQ_FIRST(bdo_queue);
684 QSIMPLEQ_REMOVE_HEAD(bdo_queue, entry);
685 loc_push_restore(&bdo->loc);
686 qmp_blockdev_add(bdo->bdo, &error_fatal);
687 loc_pop(&bdo->loc);
688 qapi_free_BlockdevOptions(bdo->bdo);
689 g_free(bdo);
691 if (snapshot) {
692 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
693 NULL, NULL);
695 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
696 &machine_class->block_default_type, &error_fatal)) {
697 /* We printed help */
698 exit(0);
701 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
702 CDROM_OPTS);
703 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
704 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
708 static QemuOptsList qemu_smp_opts = {
709 .name = "smp-opts",
710 .implied_opt_name = "cpus",
711 .merge_lists = true,
712 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
713 .desc = {
715 .name = "cpus",
716 .type = QEMU_OPT_NUMBER,
717 }, {
718 .name = "sockets",
719 .type = QEMU_OPT_NUMBER,
720 }, {
721 .name = "dies",
722 .type = QEMU_OPT_NUMBER,
723 }, {
724 .name = "cores",
725 .type = QEMU_OPT_NUMBER,
726 }, {
727 .name = "threads",
728 .type = QEMU_OPT_NUMBER,
729 }, {
730 .name = "maxcpus",
731 .type = QEMU_OPT_NUMBER,
733 { /*End of list */ }
737 static void realtime_init(void)
739 if (enable_mlock) {
740 if (os_mlock() < 0) {
741 error_report("locking memory failed");
742 exit(1);
748 static void configure_msg(QemuOpts *opts)
750 message_with_timestamp = qemu_opt_get_bool(opts, "timestamp", false);
751 error_with_guestname = qemu_opt_get_bool(opts, "guest-name", false);
755 /***********************************************************/
756 /* USB devices */
758 static int usb_device_add(const char *devname)
760 USBDevice *dev = NULL;
762 if (!machine_usb(current_machine)) {
763 return -1;
766 dev = usbdevice_create(devname);
767 if (!dev)
768 return -1;
770 return 0;
773 static int usb_parse(const char *cmdline)
775 int r;
776 r = usb_device_add(cmdline);
777 if (r < 0) {
778 error_report("could not add USB device '%s'", cmdline);
780 return r;
783 /***********************************************************/
784 /* machine registration */
786 static MachineClass *find_machine(const char *name, GSList *machines)
788 GSList *el;
790 for (el = machines; el; el = el->next) {
791 MachineClass *mc = el->data;
793 if (!strcmp(mc->name, name) || !g_strcmp0(mc->alias, name)) {
794 return mc;
798 return NULL;
801 static MachineClass *find_default_machine(GSList *machines)
803 GSList *el;
804 MachineClass *default_machineclass = NULL;
806 for (el = machines; el; el = el->next) {
807 MachineClass *mc = el->data;
809 if (mc->is_default) {
810 assert(default_machineclass == NULL && "Multiple default machines");
811 default_machineclass = mc;
815 return default_machineclass;
818 static int machine_help_func(QemuOpts *opts, MachineState *machine)
820 ObjectProperty *prop;
821 ObjectPropertyIterator iter;
823 if (!qemu_opt_has_help_opt(opts)) {
824 return 0;
827 object_property_iter_init(&iter, OBJECT(machine));
828 while ((prop = object_property_iter_next(&iter))) {
829 if (!prop->set) {
830 continue;
833 printf("%s.%s=%s", MACHINE_GET_CLASS(machine)->name,
834 prop->name, prop->type);
835 if (prop->description) {
836 printf(" (%s)\n", prop->description);
837 } else {
838 printf("\n");
842 return 1;
845 static void version(void)
847 printf("QEMU emulator version " QEMU_FULL_VERSION "\n"
848 QEMU_COPYRIGHT "\n");
851 static void help(int exitcode)
853 version();
854 printf("usage: %s [options] [disk_image]\n\n"
855 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
856 error_get_progname());
858 #define QEMU_OPTIONS_GENERATE_HELP
859 #include "qemu-options-wrapper.h"
861 printf("\nDuring emulation, the following keys are useful:\n"
862 "ctrl-alt-f toggle full screen\n"
863 "ctrl-alt-n switch to virtual console 'n'\n"
864 "ctrl-alt toggle mouse and keyboard grab\n"
865 "\n"
866 "When using -nographic, press 'ctrl-a h' to get some help.\n"
867 "\n"
868 QEMU_HELP_BOTTOM "\n");
870 exit(exitcode);
873 #define HAS_ARG 0x0001
875 typedef struct QEMUOption {
876 const char *name;
877 int flags;
878 int index;
879 uint32_t arch_mask;
880 } QEMUOption;
882 static const QEMUOption qemu_options[] = {
883 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
884 #define QEMU_OPTIONS_GENERATE_OPTIONS
885 #include "qemu-options-wrapper.h"
886 { NULL },
889 typedef struct VGAInterfaceInfo {
890 const char *opt_name; /* option name */
891 const char *name; /* human-readable name */
892 /* Class names indicating that support is available.
893 * If no class is specified, the interface is always available */
894 const char *class_names[2];
895 } VGAInterfaceInfo;
897 static const VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
898 [VGA_NONE] = {
899 .opt_name = "none",
900 .name = "no graphic card",
902 [VGA_STD] = {
903 .opt_name = "std",
904 .name = "standard VGA",
905 .class_names = { "VGA", "isa-vga" },
907 [VGA_CIRRUS] = {
908 .opt_name = "cirrus",
909 .name = "Cirrus VGA",
910 .class_names = { "cirrus-vga", "isa-cirrus-vga" },
912 [VGA_VMWARE] = {
913 .opt_name = "vmware",
914 .name = "VMWare SVGA",
915 .class_names = { "vmware-svga" },
917 [VGA_VIRTIO] = {
918 .opt_name = "virtio",
919 .name = "Virtio VGA",
920 .class_names = { "virtio-vga" },
922 [VGA_QXL] = {
923 .opt_name = "qxl",
924 .name = "QXL VGA",
925 .class_names = { "qxl-vga" },
927 [VGA_TCX] = {
928 .opt_name = "tcx",
929 .name = "TCX framebuffer",
930 .class_names = { "sun-tcx" },
932 [VGA_CG3] = {
933 .opt_name = "cg3",
934 .name = "CG3 framebuffer",
935 .class_names = { "cgthree" },
937 [VGA_XENFB] = {
938 .opt_name = "xenfb",
939 .name = "Xen paravirtualized framebuffer",
943 static bool vga_interface_available(VGAInterfaceType t)
945 const VGAInterfaceInfo *ti = &vga_interfaces[t];
947 assert(t < VGA_TYPE_MAX);
948 return !ti->class_names[0] ||
949 module_object_class_by_name(ti->class_names[0]) ||
950 module_object_class_by_name(ti->class_names[1]);
953 static const char *
954 get_default_vga_model(const MachineClass *machine_class)
956 if (machine_class->default_display) {
957 return machine_class->default_display;
958 } else if (vga_interface_available(VGA_CIRRUS)) {
959 return "cirrus";
960 } else if (vga_interface_available(VGA_STD)) {
961 return "std";
964 return NULL;
967 static void select_vgahw(const MachineClass *machine_class, const char *p)
969 const char *opts;
970 int t;
972 if (g_str_equal(p, "help")) {
973 const char *def = get_default_vga_model(machine_class);
975 for (t = 0; t < VGA_TYPE_MAX; t++) {
976 const VGAInterfaceInfo *ti = &vga_interfaces[t];
978 if (vga_interface_available(t) && ti->opt_name) {
979 printf("%-20s %s%s\n", ti->opt_name, ti->name ?: "",
980 g_str_equal(ti->opt_name, def) ? " (default)" : "");
983 exit(0);
986 assert(vga_interface_type == VGA_NONE);
987 for (t = 0; t < VGA_TYPE_MAX; t++) {
988 const VGAInterfaceInfo *ti = &vga_interfaces[t];
989 if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
990 if (!vga_interface_available(t)) {
991 error_report("%s not available", ti->name);
992 exit(1);
994 vga_interface_type = t;
995 break;
998 if (t == VGA_TYPE_MAX) {
999 invalid_vga:
1000 error_report("unknown vga type: %s", p);
1001 exit(1);
1003 while (*opts) {
1004 const char *nextopt;
1006 if (strstart(opts, ",retrace=", &nextopt)) {
1007 opts = nextopt;
1008 if (strstart(opts, "dumb", &nextopt))
1009 vga_retrace_method = VGA_RETRACE_DUMB;
1010 else if (strstart(opts, "precise", &nextopt))
1011 vga_retrace_method = VGA_RETRACE_PRECISE;
1012 else goto invalid_vga;
1013 } else goto invalid_vga;
1014 opts = nextopt;
1018 static void parse_display_qapi(const char *optarg)
1020 DisplayOptions *opts;
1021 Visitor *v;
1023 v = qobject_input_visitor_new_str(optarg, "type", &error_fatal);
1025 visit_type_DisplayOptions(v, NULL, &opts, &error_fatal);
1026 QAPI_CLONE_MEMBERS(DisplayOptions, &dpy, opts);
1028 qapi_free_DisplayOptions(opts);
1029 visit_free(v);
1032 DisplayOptions *qmp_query_display_options(Error **errp)
1034 return QAPI_CLONE(DisplayOptions, &dpy);
1037 static void parse_display(const char *p)
1039 const char *opts;
1041 if (is_help_option(p)) {
1042 qemu_display_help();
1043 exit(0);
1046 if (strstart(p, "sdl", &opts)) {
1048 * sdl DisplayType needs hand-crafted parser instead of
1049 * parse_display_qapi() due to some options not in
1050 * DisplayOptions, specifically:
1051 * - frame
1052 * Already deprecated.
1053 * - ctrl_grab + alt_grab
1054 * Not clear yet what happens to them long-term. Should
1055 * replaced by something better or deprecated and dropped.
1057 dpy.type = DISPLAY_TYPE_SDL;
1058 while (*opts) {
1059 const char *nextopt;
1061 if (strstart(opts, ",alt_grab=", &nextopt)) {
1062 opts = nextopt;
1063 if (strstart(opts, "on", &nextopt)) {
1064 alt_grab = 1;
1065 } else if (strstart(opts, "off", &nextopt)) {
1066 alt_grab = 0;
1067 } else {
1068 goto invalid_sdl_args;
1070 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1071 opts = nextopt;
1072 if (strstart(opts, "on", &nextopt)) {
1073 ctrl_grab = 1;
1074 } else if (strstart(opts, "off", &nextopt)) {
1075 ctrl_grab = 0;
1076 } else {
1077 goto invalid_sdl_args;
1079 } else if (strstart(opts, ",window_close=", &nextopt)) {
1080 opts = nextopt;
1081 dpy.has_window_close = true;
1082 if (strstart(opts, "on", &nextopt)) {
1083 dpy.window_close = true;
1084 } else if (strstart(opts, "off", &nextopt)) {
1085 dpy.window_close = false;
1086 } else {
1087 goto invalid_sdl_args;
1089 } else if (strstart(opts, ",show-cursor=", &nextopt)) {
1090 opts = nextopt;
1091 dpy.has_show_cursor = true;
1092 if (strstart(opts, "on", &nextopt)) {
1093 dpy.show_cursor = true;
1094 } else if (strstart(opts, "off", &nextopt)) {
1095 dpy.show_cursor = false;
1096 } else {
1097 goto invalid_sdl_args;
1099 } else if (strstart(opts, ",gl=", &nextopt)) {
1100 opts = nextopt;
1101 dpy.has_gl = true;
1102 if (strstart(opts, "on", &nextopt)) {
1103 dpy.gl = DISPLAYGL_MODE_ON;
1104 } else if (strstart(opts, "core", &nextopt)) {
1105 dpy.gl = DISPLAYGL_MODE_CORE;
1106 } else if (strstart(opts, "es", &nextopt)) {
1107 dpy.gl = DISPLAYGL_MODE_ES;
1108 } else if (strstart(opts, "off", &nextopt)) {
1109 dpy.gl = DISPLAYGL_MODE_OFF;
1110 } else {
1111 goto invalid_sdl_args;
1113 } else {
1114 invalid_sdl_args:
1115 error_report("invalid SDL option string");
1116 exit(1);
1118 opts = nextopt;
1120 } else if (strstart(p, "vnc", &opts)) {
1122 * vnc isn't a (local) DisplayType but a protocol for remote
1123 * display access.
1125 if (*opts == '=') {
1126 vnc_parse(opts + 1);
1127 } else {
1128 error_report("VNC requires a display argument vnc=<display>");
1129 exit(1);
1131 } else {
1132 parse_display_qapi(p);
1136 static inline bool nonempty_str(const char *str)
1138 return str && *str;
1141 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
1143 gchar *buf;
1144 size_t size;
1145 const char *name, *file, *str, *gen_id;
1146 FWCfgState *fw_cfg = (FWCfgState *) opaque;
1148 if (fw_cfg == NULL) {
1149 error_setg(errp, "fw_cfg device not available");
1150 return -1;
1152 name = qemu_opt_get(opts, "name");
1153 file = qemu_opt_get(opts, "file");
1154 str = qemu_opt_get(opts, "string");
1155 gen_id = qemu_opt_get(opts, "gen_id");
1157 /* we need the name, and exactly one of: file, content string, gen_id */
1158 if (!nonempty_str(name) ||
1159 nonempty_str(file) + nonempty_str(str) + nonempty_str(gen_id) != 1) {
1160 error_setg(errp, "name, plus exactly one of file,"
1161 " string and gen_id, are needed");
1162 return -1;
1164 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
1165 error_setg(errp, "name too long (max. %d char)",
1166 FW_CFG_MAX_FILE_PATH - 1);
1167 return -1;
1169 if (nonempty_str(gen_id)) {
1171 * In this particular case where the content is populated
1172 * internally, the "etc/" namespace protection is relaxed,
1173 * so do not emit a warning.
1175 } else if (strncmp(name, "opt/", 4) != 0) {
1176 warn_report("externally provided fw_cfg item names "
1177 "should be prefixed with \"opt/\"");
1179 if (nonempty_str(str)) {
1180 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
1181 buf = g_memdup(str, size);
1182 } else if (nonempty_str(gen_id)) {
1183 if (!fw_cfg_add_from_generator(fw_cfg, name, gen_id, errp)) {
1184 return -1;
1186 return 0;
1187 } else {
1188 GError *err = NULL;
1189 if (!g_file_get_contents(file, &buf, &size, &err)) {
1190 error_setg(errp, "can't load %s: %s", file, err->message);
1191 g_error_free(err);
1192 return -1;
1195 /* For legacy, keep user files in a specific global order. */
1196 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
1197 fw_cfg_add_file(fw_cfg, name, buf, size);
1198 fw_cfg_reset_order_override(fw_cfg);
1199 return 0;
1202 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
1204 return qdev_device_help(opts);
1207 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
1209 DeviceState *dev;
1211 dev = qdev_device_add(opts, errp);
1212 if (!dev && *errp) {
1213 error_report_err(*errp);
1214 return -1;
1215 } else if (dev) {
1216 object_unref(OBJECT(dev));
1218 return 0;
1221 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
1223 Error *local_err = NULL;
1225 if (!qemu_chr_new_from_opts(opts, NULL, &local_err)) {
1226 if (local_err) {
1227 error_propagate(errp, local_err);
1228 return -1;
1230 exit(0);
1232 return 0;
1235 #ifdef CONFIG_VIRTFS
1236 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
1238 return qemu_fsdev_add(opts, errp);
1240 #endif
1242 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
1244 return monitor_init_opts(opts, errp);
1247 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
1249 static int monitor_device_index = 0;
1250 QemuOpts *opts;
1251 const char *p;
1252 char label[32];
1254 if (strstart(optarg, "chardev:", &p)) {
1255 snprintf(label, sizeof(label), "%s", p);
1256 } else {
1257 snprintf(label, sizeof(label), "compat_monitor%d",
1258 monitor_device_index);
1259 opts = qemu_chr_parse_compat(label, optarg, true);
1260 if (!opts) {
1261 error_report("parse error: %s", optarg);
1262 exit(1);
1266 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
1267 qemu_opt_set(opts, "mode", mode, &error_abort);
1268 qemu_opt_set(opts, "chardev", label, &error_abort);
1269 if (!strcmp(mode, "control")) {
1270 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
1271 } else {
1272 assert(pretty == false);
1274 monitor_device_index++;
1277 struct device_config {
1278 enum {
1279 DEV_USB, /* -usbdevice */
1280 DEV_SERIAL, /* -serial */
1281 DEV_PARALLEL, /* -parallel */
1282 DEV_DEBUGCON, /* -debugcon */
1283 DEV_GDB, /* -gdb, -s */
1284 DEV_SCLP, /* s390 sclp */
1285 } type;
1286 const char *cmdline;
1287 Location loc;
1288 QTAILQ_ENTRY(device_config) next;
1291 static QTAILQ_HEAD(, device_config) device_configs =
1292 QTAILQ_HEAD_INITIALIZER(device_configs);
1294 static void add_device_config(int type, const char *cmdline)
1296 struct device_config *conf;
1298 conf = g_malloc0(sizeof(*conf));
1299 conf->type = type;
1300 conf->cmdline = cmdline;
1301 loc_save(&conf->loc);
1302 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1305 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1307 struct device_config *conf;
1308 int rc;
1310 QTAILQ_FOREACH(conf, &device_configs, next) {
1311 if (conf->type != type)
1312 continue;
1313 loc_push_restore(&conf->loc);
1314 rc = func(conf->cmdline);
1315 loc_pop(&conf->loc);
1316 if (rc) {
1317 return rc;
1320 return 0;
1323 static void qemu_disable_default_devices(void)
1325 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1327 qemu_opts_foreach(qemu_find_opts("device"),
1328 default_driver_check, NULL, NULL);
1329 qemu_opts_foreach(qemu_find_opts("global"),
1330 default_driver_check, NULL, NULL);
1332 if (!vga_model && !default_vga) {
1333 vga_interface_type = VGA_DEVICE;
1335 if (!has_defaults || machine_class->no_serial) {
1336 default_serial = 0;
1338 if (!has_defaults || machine_class->no_parallel) {
1339 default_parallel = 0;
1341 if (!has_defaults || machine_class->no_floppy) {
1342 default_floppy = 0;
1344 if (!has_defaults || machine_class->no_cdrom) {
1345 default_cdrom = 0;
1347 if (!has_defaults || machine_class->no_sdcard) {
1348 default_sdcard = 0;
1350 if (!has_defaults) {
1351 default_monitor = 0;
1352 default_net = 0;
1353 default_vga = 0;
1357 static void qemu_create_default_devices(void)
1359 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1361 if (is_daemonized()) {
1362 /* According to documentation and historically, -nographic redirects
1363 * serial port, parallel port and monitor to stdio, which does not work
1364 * with -daemonize. We can redirect these to null instead, but since
1365 * -nographic is legacy, let's just error out.
1366 * We disallow -nographic only if all other ports are not redirected
1367 * explicitly, to not break existing legacy setups which uses
1368 * -nographic _and_ redirects all ports explicitly - this is valid
1369 * usage, -nographic is just a no-op in this case.
1371 if (nographic
1372 && (default_parallel || default_serial || default_monitor)) {
1373 error_report("-nographic cannot be used with -daemonize");
1374 exit(1);
1378 if (nographic) {
1379 if (default_parallel)
1380 add_device_config(DEV_PARALLEL, "null");
1381 if (default_serial && default_monitor) {
1382 add_device_config(DEV_SERIAL, "mon:stdio");
1383 } else {
1384 if (default_serial)
1385 add_device_config(DEV_SERIAL, "stdio");
1386 if (default_monitor)
1387 monitor_parse("stdio", "readline", false);
1389 } else {
1390 if (default_serial)
1391 add_device_config(DEV_SERIAL, "vc:80Cx24C");
1392 if (default_parallel)
1393 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
1394 if (default_monitor)
1395 monitor_parse("vc:80Cx24C", "readline", false);
1398 if (default_net) {
1399 QemuOptsList *net = qemu_find_opts("net");
1400 qemu_opts_parse(net, "nic", true, &error_abort);
1401 #ifdef CONFIG_SLIRP
1402 qemu_opts_parse(net, "user", true, &error_abort);
1403 #endif
1406 #if defined(CONFIG_VNC)
1407 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
1408 display_remote++;
1410 #endif
1411 if (dpy.type == DISPLAY_TYPE_DEFAULT && !display_remote) {
1412 if (!qemu_display_find_default(&dpy)) {
1413 dpy.type = DISPLAY_TYPE_NONE;
1414 #if defined(CONFIG_VNC)
1415 vnc_parse("localhost:0,to=99,id=default");
1416 #endif
1419 if (dpy.type == DISPLAY_TYPE_DEFAULT) {
1420 dpy.type = DISPLAY_TYPE_NONE;
1423 /* If no default VGA is requested, the default is "none". */
1424 if (default_vga) {
1425 vga_model = get_default_vga_model(machine_class);
1427 if (vga_model) {
1428 select_vgahw(machine_class, vga_model);
1432 static int serial_parse(const char *devname)
1434 int index = num_serial_hds;
1435 char label[32];
1437 if (strcmp(devname, "none") == 0)
1438 return 0;
1439 snprintf(label, sizeof(label), "serial%d", index);
1440 serial_hds = g_renew(Chardev *, serial_hds, index + 1);
1442 serial_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
1443 if (!serial_hds[index]) {
1444 error_report("could not connect serial device"
1445 " to character backend '%s'", devname);
1446 return -1;
1448 num_serial_hds++;
1449 return 0;
1452 Chardev *serial_hd(int i)
1454 assert(i >= 0);
1455 if (i < num_serial_hds) {
1456 return serial_hds[i];
1458 return NULL;
1461 static int parallel_parse(const char *devname)
1463 static int index = 0;
1464 char label[32];
1466 if (strcmp(devname, "none") == 0)
1467 return 0;
1468 if (index == MAX_PARALLEL_PORTS) {
1469 error_report("too many parallel ports");
1470 exit(1);
1472 snprintf(label, sizeof(label), "parallel%d", index);
1473 parallel_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
1474 if (!parallel_hds[index]) {
1475 error_report("could not connect parallel device"
1476 " to character backend '%s'", devname);
1477 return -1;
1479 index++;
1480 return 0;
1483 static int debugcon_parse(const char *devname)
1485 QemuOpts *opts;
1487 if (!qemu_chr_new_mux_mon("debugcon", devname, NULL)) {
1488 error_report("invalid character backend '%s'", devname);
1489 exit(1);
1491 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
1492 if (!opts) {
1493 error_report("already have a debugcon device");
1494 exit(1);
1496 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
1497 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
1498 return 0;
1501 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
1503 const MachineClass *mc1 = a, *mc2 = b;
1504 int res;
1506 if (mc1->family == NULL) {
1507 if (mc2->family == NULL) {
1508 /* Compare standalone machine types against each other; they sort
1509 * in increasing order.
1511 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
1512 object_class_get_name(OBJECT_CLASS(mc2)));
1515 /* Standalone machine types sort after families. */
1516 return 1;
1519 if (mc2->family == NULL) {
1520 /* Families sort before standalone machine types. */
1521 return -1;
1524 /* Families sort between each other alphabetically increasingly. */
1525 res = strcmp(mc1->family, mc2->family);
1526 if (res != 0) {
1527 return res;
1530 /* Within the same family, machine types sort in decreasing order. */
1531 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
1532 object_class_get_name(OBJECT_CLASS(mc1)));
1535 static MachineClass *machine_parse(const char *name, GSList *machines)
1537 MachineClass *mc;
1538 GSList *el;
1540 if (is_help_option(name)) {
1541 printf("Supported machines are:\n");
1542 machines = g_slist_sort(machines, machine_class_cmp);
1543 for (el = machines; el; el = el->next) {
1544 MachineClass *mc = el->data;
1545 if (mc->alias) {
1546 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
1548 printf("%-20s %s%s%s\n", mc->name, mc->desc,
1549 mc->is_default ? " (default)" : "",
1550 mc->deprecation_reason ? " (deprecated)" : "");
1552 exit(0);
1555 mc = find_machine(name, machines);
1556 if (!mc) {
1557 error_report("unsupported machine type");
1558 error_printf("Use -machine help to list supported machines\n");
1559 exit(1);
1561 return mc;
1564 static const char *pid_file;
1565 static Notifier qemu_unlink_pidfile_notifier;
1567 static void qemu_unlink_pidfile(Notifier *n, void *data)
1569 if (pid_file) {
1570 unlink(pid_file);
1574 static const QEMUOption *lookup_opt(int argc, char **argv,
1575 const char **poptarg, int *poptind)
1577 const QEMUOption *popt;
1578 int optind = *poptind;
1579 char *r = argv[optind];
1580 const char *optarg;
1582 loc_set_cmdline(argv, optind, 1);
1583 optind++;
1584 /* Treat --foo the same as -foo. */
1585 if (r[1] == '-')
1586 r++;
1587 popt = qemu_options;
1588 for(;;) {
1589 if (!popt->name) {
1590 error_report("invalid option");
1591 exit(1);
1593 if (!strcmp(popt->name, r + 1))
1594 break;
1595 popt++;
1597 if (popt->flags & HAS_ARG) {
1598 if (optind >= argc) {
1599 error_report("requires an argument");
1600 exit(1);
1602 optarg = argv[optind++];
1603 loc_set_cmdline(argv, optind - 2, 2);
1604 } else {
1605 optarg = NULL;
1608 *poptarg = optarg;
1609 *poptind = optind;
1611 return popt;
1614 static MachineClass *select_machine(void)
1616 GSList *machines = object_class_get_list(TYPE_MACHINE, false);
1617 MachineClass *machine_class = find_default_machine(machines);
1618 const char *optarg;
1619 QemuOpts *opts;
1620 Location loc;
1622 loc_push_none(&loc);
1624 opts = qemu_get_machine_opts();
1625 qemu_opts_loc_restore(opts);
1627 optarg = qemu_opt_get(opts, "type");
1628 if (optarg) {
1629 machine_class = machine_parse(optarg, machines);
1632 if (!machine_class) {
1633 error_report("No machine specified, and there is no default");
1634 error_printf("Use -machine help to list supported machines\n");
1635 exit(1);
1638 loc_pop(&loc);
1639 g_slist_free(machines);
1640 return machine_class;
1643 static int object_parse_property_opt(Object *obj,
1644 const char *name, const char *value,
1645 const char *skip, Error **errp)
1647 if (g_str_equal(name, skip)) {
1648 return 0;
1651 if (!object_property_parse(obj, name, value, errp)) {
1652 return -1;
1655 return 0;
1658 static int machine_set_property(void *opaque,
1659 const char *name, const char *value,
1660 Error **errp)
1662 g_autofree char *qom_name = g_strdup(name);
1663 char *p;
1665 for (p = qom_name; *p; p++) {
1666 if (*p == '_') {
1667 *p = '-';
1671 /* Legacy options do not correspond to MachineState properties. */
1672 if (g_str_equal(qom_name, "accel")) {
1673 return 0;
1675 if (g_str_equal(qom_name, "igd-passthru")) {
1676 object_register_sugar_prop(ACCEL_CLASS_NAME("xen"), qom_name, value,
1677 false);
1678 return 0;
1680 if (g_str_equal(qom_name, "kvm-shadow-mem")) {
1681 object_register_sugar_prop(ACCEL_CLASS_NAME("kvm"), qom_name, value,
1682 false);
1683 return 0;
1685 if (g_str_equal(qom_name, "kernel-irqchip")) {
1686 object_register_sugar_prop(ACCEL_CLASS_NAME("kvm"), qom_name, value,
1687 false);
1688 object_register_sugar_prop(ACCEL_CLASS_NAME("whpx"), qom_name, value,
1689 false);
1690 return 0;
1693 return object_parse_property_opt(opaque, name, value, "type", errp);
1696 static void object_option_foreach_add(bool (*type_opt_predicate)(const char *))
1698 ObjectOption *opt, *next;
1700 QTAILQ_FOREACH_SAFE(opt, &object_opts, next, next) {
1701 const char *type = ObjectType_str(opt->opts->qom_type);
1702 if (type_opt_predicate(type)) {
1703 user_creatable_add_qapi(opt->opts, &error_fatal);
1704 qapi_free_ObjectOptions(opt->opts);
1705 QTAILQ_REMOVE(&object_opts, opt, next);
1706 g_free(opt);
1711 static void object_option_parse(const char *optarg)
1713 ObjectOption *opt;
1714 QemuOpts *opts;
1715 const char *type;
1716 Visitor *v;
1718 if (optarg[0] == '{') {
1719 QObject *obj = qobject_from_json(optarg, &error_fatal);
1721 v = qobject_input_visitor_new(obj);
1722 qobject_unref(obj);
1723 } else {
1724 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
1725 optarg, true);
1726 if (!opts) {
1727 exit(1);
1730 type = qemu_opt_get(opts, "qom-type");
1731 if (!type) {
1732 error_setg(&error_fatal, QERR_MISSING_PARAMETER, "qom-type");
1734 if (user_creatable_print_help(type, opts)) {
1735 exit(0);
1738 v = opts_visitor_new(opts);
1741 opt = g_new0(ObjectOption, 1);
1742 visit_type_ObjectOptions(v, NULL, &opt->opts, &error_fatal);
1743 visit_free(v);
1745 QTAILQ_INSERT_TAIL(&object_opts, opt, next);
1749 * Initial object creation happens before all other
1750 * QEMU data types are created. The majority of objects
1751 * can be created at this point. The rng-egd object
1752 * cannot be created here, as it depends on the chardev
1753 * already existing.
1755 static bool object_create_early(const char *type)
1758 * Objects should not be made "delayed" without a reason. If you
1759 * add one, state the reason in a comment!
1762 /* Reason: rng-egd property "chardev" */
1763 if (g_str_equal(type, "rng-egd")) {
1764 return false;
1767 #if defined(CONFIG_VHOST_USER) && defined(CONFIG_LINUX)
1768 /* Reason: cryptodev-vhost-user property "chardev" */
1769 if (g_str_equal(type, "cryptodev-vhost-user")) {
1770 return false;
1772 #endif
1774 /* Reason: vhost-user-blk-server property "node-name" */
1775 if (g_str_equal(type, "vhost-user-blk-server")) {
1776 return false;
1779 * Reason: filter-* property "netdev" etc.
1781 if (g_str_equal(type, "filter-buffer") ||
1782 g_str_equal(type, "filter-dump") ||
1783 g_str_equal(type, "filter-mirror") ||
1784 g_str_equal(type, "filter-redirector") ||
1785 g_str_equal(type, "colo-compare") ||
1786 g_str_equal(type, "filter-rewriter") ||
1787 g_str_equal(type, "filter-replay")) {
1788 return false;
1792 * Allocation of large amounts of memory may delay
1793 * chardev initialization for too long, and trigger timeouts
1794 * on software that waits for a monitor socket to be created
1795 * (e.g. libvirt).
1797 if (g_str_has_prefix(type, "memory-backend-")) {
1798 return false;
1801 return true;
1804 static void qemu_apply_machine_options(void)
1806 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1807 QemuOpts *machine_opts = qemu_get_machine_opts();
1808 const char *boot_order = NULL;
1809 const char *boot_once = NULL;
1810 QemuOpts *opts;
1812 qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
1813 &error_fatal);
1814 current_machine->ram_size = ram_size;
1815 current_machine->maxram_size = maxram_size;
1816 current_machine->ram_slots = ram_slots;
1818 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
1819 if (opts) {
1820 boot_order = qemu_opt_get(opts, "order");
1821 if (boot_order) {
1822 validate_bootdevices(boot_order, &error_fatal);
1825 boot_once = qemu_opt_get(opts, "once");
1826 if (boot_once) {
1827 validate_bootdevices(boot_once, &error_fatal);
1830 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
1831 boot_strict = qemu_opt_get_bool(opts, "strict", false);
1834 if (!boot_order) {
1835 boot_order = machine_class->default_boot_order;
1838 current_machine->boot_order = boot_order;
1839 current_machine->boot_once = boot_once;
1841 if (semihosting_enabled() && !semihosting_get_argc()) {
1842 const char *kernel_filename = qemu_opt_get(machine_opts, "kernel");
1843 const char *kernel_cmdline = qemu_opt_get(machine_opts, "append") ?: "";
1844 /* fall back to the -kernel/-append */
1845 semihosting_arg_fallback(kernel_filename, kernel_cmdline);
1849 static void qemu_create_early_backends(void)
1851 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1853 if ((alt_grab || ctrl_grab) && dpy.type != DISPLAY_TYPE_SDL) {
1854 error_report("-alt-grab and -ctrl-grab are only valid "
1855 "for SDL, ignoring option");
1857 if (dpy.has_window_close &&
1858 (dpy.type != DISPLAY_TYPE_GTK && dpy.type != DISPLAY_TYPE_SDL)) {
1859 error_report("-no-quit is only valid for GTK and SDL, "
1860 "ignoring option");
1863 qemu_display_early_init(&dpy);
1864 qemu_console_early_init();
1866 if (dpy.has_gl && dpy.gl != DISPLAYGL_MODE_OFF && display_opengl == 0) {
1867 #if defined(CONFIG_OPENGL)
1868 error_report("OpenGL is not supported by the display");
1869 #else
1870 error_report("OpenGL support is disabled");
1871 #endif
1872 exit(1);
1875 object_option_foreach_add(object_create_early);
1877 /* spice needs the timers to be initialized by this point */
1878 /* spice must initialize before audio as it changes the default auiodev */
1879 /* spice must initialize before chardevs (for spicevmc and spiceport) */
1880 qemu_spice.init();
1882 qemu_opts_foreach(qemu_find_opts("chardev"),
1883 chardev_init_func, NULL, &error_fatal);
1885 #ifdef CONFIG_VIRTFS
1886 qemu_opts_foreach(qemu_find_opts("fsdev"),
1887 fsdev_init_func, NULL, &error_fatal);
1888 #endif
1891 * Note: we need to create audio and block backends before
1892 * machine_set_property(), so machine properties can refer to
1893 * them.
1895 configure_blockdev(&bdo_queue, machine_class, snapshot);
1896 audio_init_audiodevs();
1901 * The remainder of object creation happens after the
1902 * creation of chardev, fsdev, net clients and device data types.
1904 static bool object_create_late(const char *type)
1906 return !object_create_early(type);
1909 static void qemu_create_late_backends(void)
1911 if (qtest_chrdev) {
1912 qtest_server_init(qtest_chrdev, qtest_log, &error_fatal);
1915 net_init_clients(&error_fatal);
1917 object_option_foreach_add(object_create_late);
1919 if (tpm_init() < 0) {
1920 exit(1);
1923 qemu_opts_foreach(qemu_find_opts("mon"),
1924 mon_init_func, NULL, &error_fatal);
1926 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
1927 exit(1);
1928 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
1929 exit(1);
1930 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
1931 exit(1);
1933 /* now chardevs have been created we may have semihosting to connect */
1934 qemu_semihosting_connect_chardevs();
1935 qemu_semihosting_console_init();
1938 static bool have_custom_ram_size(void)
1940 QemuOpts *opts = qemu_find_opts_singleton("memory");
1941 return !!qemu_opt_get_size(opts, "size", 0);
1944 static void qemu_resolve_machine_memdev(void)
1946 if (current_machine->ram_memdev_id) {
1947 Object *backend;
1948 ram_addr_t backend_size;
1950 backend = object_resolve_path_type(current_machine->ram_memdev_id,
1951 TYPE_MEMORY_BACKEND, NULL);
1952 if (!backend) {
1953 error_report("Memory backend '%s' not found",
1954 current_machine->ram_memdev_id);
1955 exit(EXIT_FAILURE);
1957 backend_size = object_property_get_uint(backend, "size", &error_abort);
1958 if (have_custom_ram_size() && backend_size != ram_size) {
1959 error_report("Size specified by -m option must match size of "
1960 "explicitly specified 'memory-backend' property");
1961 exit(EXIT_FAILURE);
1963 if (mem_path) {
1964 error_report("'-mem-path' can't be used together with"
1965 "'-machine memory-backend'");
1966 exit(EXIT_FAILURE);
1968 ram_size = backend_size;
1971 if (!xen_enabled()) {
1972 /* On 32-bit hosts, QEMU is limited by virtual address space */
1973 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
1974 error_report("at most 2047 MB RAM can be simulated");
1975 exit(1);
1980 static void set_memory_options(MachineClass *mc)
1982 uint64_t sz;
1983 const char *mem_str;
1984 const ram_addr_t default_ram_size = mc->default_ram_size;
1985 QemuOpts *opts = qemu_find_opts_singleton("memory");
1986 Location loc;
1988 loc_push_none(&loc);
1989 qemu_opts_loc_restore(opts);
1991 sz = 0;
1992 mem_str = qemu_opt_get(opts, "size");
1993 if (mem_str) {
1994 if (!*mem_str) {
1995 error_report("missing 'size' option value");
1996 exit(EXIT_FAILURE);
1999 sz = qemu_opt_get_size(opts, "size", ram_size);
2001 /* Fix up legacy suffix-less format */
2002 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2003 uint64_t overflow_check = sz;
2005 sz *= MiB;
2006 if (sz / MiB != overflow_check) {
2007 error_report("too large 'size' option value");
2008 exit(EXIT_FAILURE);
2013 /* backward compatibility behaviour for case "-m 0" */
2014 if (sz == 0) {
2015 sz = default_ram_size;
2018 sz = QEMU_ALIGN_UP(sz, 8192);
2019 if (mc->fixup_ram_size) {
2020 sz = mc->fixup_ram_size(sz);
2022 ram_size = sz;
2023 if (ram_size != sz) {
2024 error_report("ram size too large");
2025 exit(EXIT_FAILURE);
2028 /* store value for the future use */
2029 qemu_opt_set_number(opts, "size", ram_size, &error_abort);
2030 maxram_size = ram_size;
2032 if (qemu_opt_get(opts, "maxmem")) {
2033 uint64_t slots;
2035 sz = qemu_opt_get_size(opts, "maxmem", 0);
2036 slots = qemu_opt_get_number(opts, "slots", 0);
2037 if (sz < ram_size) {
2038 error_report("invalid value of -m option maxmem: "
2039 "maximum memory size (0x%" PRIx64 ") must be at least "
2040 "the initial memory size (0x" RAM_ADDR_FMT ")",
2041 sz, ram_size);
2042 exit(EXIT_FAILURE);
2043 } else if (slots && sz == ram_size) {
2044 error_report("invalid value of -m option maxmem: "
2045 "memory slots were specified but maximum memory size "
2046 "(0x%" PRIx64 ") is equal to the initial memory size "
2047 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
2048 exit(EXIT_FAILURE);
2051 maxram_size = sz;
2052 ram_slots = slots;
2053 } else if (qemu_opt_get(opts, "slots")) {
2054 error_report("invalid -m option value: missing 'maxmem' option");
2055 exit(EXIT_FAILURE);
2058 loc_pop(&loc);
2061 static void qemu_create_machine(MachineClass *machine_class)
2063 object_set_machine_compat_props(machine_class->compat_props);
2065 set_memory_options(machine_class);
2067 current_machine = MACHINE(object_new_with_class(OBJECT_CLASS(machine_class)));
2068 if (machine_help_func(qemu_get_machine_opts(), current_machine)) {
2069 exit(0);
2071 object_property_add_child(object_get_root(), "machine",
2072 OBJECT(current_machine));
2073 object_property_add_child(container_get(OBJECT(current_machine),
2074 "/unattached"),
2075 "sysbus", OBJECT(sysbus_get_default()));
2077 if (machine_class->minimum_page_bits) {
2078 if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
2079 /* This would be a board error: specifying a minimum smaller than
2080 * a target's compile-time fixed setting.
2082 g_assert_not_reached();
2086 cpu_exec_init_all();
2087 page_size_init();
2089 if (machine_class->hw_version) {
2090 qemu_set_hw_version(machine_class->hw_version);
2093 machine_smp_parse(current_machine,
2094 qemu_opts_find(qemu_find_opts("smp-opts"), NULL), &error_fatal);
2097 * Get the default machine options from the machine if it is not already
2098 * specified either by the configuration file or by the command line.
2100 if (machine_class->default_machine_opts) {
2101 qemu_opts_set_defaults(qemu_find_opts("machine"),
2102 machine_class->default_machine_opts, 0);
2106 static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
2108 GlobalProperty *g;
2110 g = g_malloc0(sizeof(*g));
2111 g->driver = qemu_opt_get(opts, "driver");
2112 g->property = qemu_opt_get(opts, "property");
2113 g->value = qemu_opt_get(opts, "value");
2114 qdev_prop_register_global(g);
2115 return 0;
2118 static void qemu_read_default_config_file(Error **errp)
2120 ERRP_GUARD();
2121 int ret;
2122 g_autofree char *file = get_relocated_path(CONFIG_QEMU_CONFDIR "/qemu.conf");
2124 ret = qemu_read_config_file(file, errp);
2125 if (ret < 0) {
2126 if (ret == -ENOENT) {
2127 error_free(*errp);
2128 *errp = NULL;
2133 static int qemu_set_option(const char *str)
2135 Error *local_err = NULL;
2136 char group[64], id[64], arg[64];
2137 QemuOptsList *list;
2138 QemuOpts *opts;
2139 int rc, offset;
2141 rc = sscanf(str, "%63[^.].%63[^.].%63[^=]%n", group, id, arg, &offset);
2142 if (rc < 3 || str[offset] != '=') {
2143 error_report("can't parse: \"%s\"", str);
2144 return -1;
2147 list = qemu_find_opts(group);
2148 if (list == NULL) {
2149 return -1;
2152 opts = qemu_opts_find(list, id);
2153 if (!opts) {
2154 error_report("there is no %s \"%s\" defined",
2155 list->name, id);
2156 return -1;
2159 if (!qemu_opt_set(opts, arg, str + offset + 1, &local_err)) {
2160 error_report_err(local_err);
2161 return -1;
2163 return 0;
2166 static void user_register_global_props(void)
2168 qemu_opts_foreach(qemu_find_opts("global"),
2169 global_init_func, NULL, NULL);
2172 static int do_configure_icount(void *opaque, QemuOpts *opts, Error **errp)
2174 icount_configure(opts, errp);
2175 return 0;
2178 static int accelerator_set_property(void *opaque,
2179 const char *name, const char *value,
2180 Error **errp)
2182 return object_parse_property_opt(opaque, name, value, "accel", errp);
2185 static int do_configure_accelerator(void *opaque, QemuOpts *opts, Error **errp)
2187 bool *p_init_failed = opaque;
2188 const char *acc = qemu_opt_get(opts, "accel");
2189 AccelClass *ac = accel_find(acc);
2190 AccelState *accel;
2191 int ret;
2192 bool qtest_with_kvm;
2194 qtest_with_kvm = g_str_equal(acc, "kvm") && qtest_chrdev != NULL;
2196 if (!ac) {
2197 *p_init_failed = true;
2198 if (!qtest_with_kvm) {
2199 error_report("invalid accelerator %s", acc);
2201 return 0;
2203 accel = ACCEL(object_new_with_class(OBJECT_CLASS(ac)));
2204 object_apply_compat_props(OBJECT(accel));
2205 qemu_opt_foreach(opts, accelerator_set_property,
2206 accel,
2207 &error_fatal);
2209 ret = accel_init_machine(accel, current_machine);
2210 if (ret < 0) {
2211 *p_init_failed = true;
2212 if (!qtest_with_kvm || ret != -ENOENT) {
2213 error_report("failed to initialize %s: %s", acc, strerror(-ret));
2215 return 0;
2218 return 1;
2221 static void configure_accelerators(const char *progname)
2223 const char *accelerators;
2224 bool init_failed = false;
2226 qemu_opts_foreach(qemu_find_opts("icount"),
2227 do_configure_icount, NULL, &error_fatal);
2229 accelerators = qemu_opt_get(qemu_get_machine_opts(), "accel");
2230 if (QTAILQ_EMPTY(&qemu_accel_opts.head)) {
2231 char **accel_list, **tmp;
2233 if (accelerators == NULL) {
2234 /* Select the default accelerator */
2235 bool have_tcg = accel_find("tcg");
2236 bool have_kvm = accel_find("kvm");
2238 if (have_tcg && have_kvm) {
2239 if (g_str_has_suffix(progname, "kvm")) {
2240 /* If the program name ends with "kvm", we prefer KVM */
2241 accelerators = "kvm:tcg";
2242 } else {
2243 accelerators = "tcg:kvm";
2245 } else if (have_kvm) {
2246 accelerators = "kvm";
2247 } else if (have_tcg) {
2248 accelerators = "tcg";
2249 } else {
2250 error_report("No accelerator selected and"
2251 " no default accelerator available");
2252 exit(1);
2255 accel_list = g_strsplit(accelerators, ":", 0);
2257 for (tmp = accel_list; *tmp; tmp++) {
2259 * Filter invalid accelerators here, to prevent obscenities
2260 * such as "-machine accel=tcg,,thread=single".
2262 if (accel_find(*tmp)) {
2263 qemu_opts_parse_noisily(qemu_find_opts("accel"), *tmp, true);
2264 } else {
2265 init_failed = true;
2266 error_report("invalid accelerator %s", *tmp);
2269 g_strfreev(accel_list);
2270 } else {
2271 if (accelerators != NULL) {
2272 error_report("The -accel and \"-machine accel=\" options are incompatible");
2273 exit(1);
2277 if (!qemu_opts_foreach(qemu_find_opts("accel"),
2278 do_configure_accelerator, &init_failed, &error_fatal)) {
2279 if (!init_failed) {
2280 error_report("no accelerator found");
2282 exit(1);
2285 if (init_failed && !qtest_chrdev) {
2286 AccelClass *ac = ACCEL_GET_CLASS(current_accel());
2287 error_report("falling back to %s", ac->name);
2290 if (icount_enabled() && !tcg_enabled()) {
2291 error_report("-icount is not allowed with hardware virtualization");
2292 exit(1);
2296 static void create_default_memdev(MachineState *ms, const char *path)
2298 Object *obj;
2299 MachineClass *mc = MACHINE_GET_CLASS(ms);
2301 obj = object_new(path ? TYPE_MEMORY_BACKEND_FILE : TYPE_MEMORY_BACKEND_RAM);
2302 if (path) {
2303 object_property_set_str(obj, "mem-path", path, &error_fatal);
2305 object_property_set_int(obj, "size", ms->ram_size, &error_fatal);
2306 object_property_add_child(object_get_objects_root(), mc->default_ram_id,
2307 obj);
2308 /* Ensure backend's memory region name is equal to mc->default_ram_id */
2309 object_property_set_bool(obj, "x-use-canonical-path-for-ramblock-id",
2310 false, &error_fatal);
2311 user_creatable_complete(USER_CREATABLE(obj), &error_fatal);
2312 object_unref(obj);
2313 object_property_set_str(OBJECT(ms), "memory-backend", mc->default_ram_id,
2314 &error_fatal);
2317 static void qemu_validate_options(void)
2319 QemuOpts *machine_opts = qemu_get_machine_opts();
2320 const char *kernel_filename = qemu_opt_get(machine_opts, "kernel");
2321 const char *initrd_filename = qemu_opt_get(machine_opts, "initrd");
2322 const char *kernel_cmdline = qemu_opt_get(machine_opts, "append");
2324 if (kernel_filename == NULL) {
2325 if (kernel_cmdline != NULL) {
2326 error_report("-append only allowed with -kernel option");
2327 exit(1);
2330 if (initrd_filename != NULL) {
2331 error_report("-initrd only allowed with -kernel option");
2332 exit(1);
2336 if (loadvm && preconfig_requested) {
2337 error_report("'preconfig' and 'loadvm' options are "
2338 "mutually exclusive");
2339 exit(EXIT_FAILURE);
2341 if (incoming && preconfig_requested && strcmp(incoming, "defer") != 0) {
2342 error_report("'preconfig' supports '-incoming defer' only");
2343 exit(EXIT_FAILURE);
2346 #ifdef CONFIG_CURSES
2347 if (is_daemonized() && dpy.type == DISPLAY_TYPE_CURSES) {
2348 error_report("curses display cannot be used with -daemonize");
2349 exit(1);
2351 #endif
2354 static void qemu_process_sugar_options(void)
2356 if (mem_prealloc) {
2357 char *val;
2359 val = g_strdup_printf("%d",
2360 (uint32_t) qemu_opt_get_number(qemu_find_opts_singleton("smp-opts"), "cpus", 1));
2361 object_register_sugar_prop("memory-backend", "prealloc-threads", val,
2362 false);
2363 g_free(val);
2364 object_register_sugar_prop("memory-backend", "prealloc", "on", false);
2367 if (watchdog) {
2368 int i = select_watchdog(watchdog);
2369 if (i > 0)
2370 exit (i == 1 ? 1 : 0);
2374 /* -action processing */
2377 * Process all the -action parameters parsed from cmdline.
2379 static int process_runstate_actions(void *opaque, QemuOpts *opts, Error **errp)
2381 Error *local_err = NULL;
2382 QDict *qdict = qemu_opts_to_qdict(opts, NULL);
2383 QObject *ret = NULL;
2384 qmp_marshal_set_action(qdict, &ret, &local_err);
2385 qobject_unref(ret);
2386 qobject_unref(qdict);
2387 if (local_err) {
2388 error_propagate(errp, local_err);
2389 return 1;
2391 return 0;
2394 static void qemu_process_early_options(void)
2396 #ifdef CONFIG_SECCOMP
2397 QemuOptsList *olist = qemu_find_opts_err("sandbox", NULL);
2398 if (olist) {
2399 qemu_opts_foreach(olist, parse_sandbox, NULL, &error_fatal);
2401 #endif
2403 qemu_opts_foreach(qemu_find_opts("name"),
2404 parse_name, NULL, &error_fatal);
2406 if (qemu_opts_foreach(qemu_find_opts("action"),
2407 process_runstate_actions, NULL, &error_fatal)) {
2408 exit(1);
2411 #ifndef _WIN32
2412 qemu_opts_foreach(qemu_find_opts("add-fd"),
2413 parse_add_fd, NULL, &error_fatal);
2415 qemu_opts_foreach(qemu_find_opts("add-fd"),
2416 cleanup_add_fd, NULL, &error_fatal);
2417 #endif
2419 /* Open the logfile at this point and set the log mask if necessary. */
2420 if (log_file) {
2421 qemu_set_log_filename(log_file, &error_fatal);
2423 if (log_mask) {
2424 int mask;
2425 mask = qemu_str_to_log_mask(log_mask);
2426 if (!mask) {
2427 qemu_print_log_usage(stdout);
2428 exit(1);
2430 qemu_set_log(mask);
2431 } else {
2432 qemu_set_log(0);
2435 qemu_add_default_firmwarepath();
2438 static void qemu_process_help_options(void)
2441 * Check for -cpu help and -device help before we call select_machine(),
2442 * which will return an error if the architecture has no default machine
2443 * type and the user did not specify one, so that the user doesn't need
2444 * to say '-cpu help -machine something'.
2446 if (cpu_option && is_help_option(cpu_option)) {
2447 list_cpus(cpu_option);
2448 exit(0);
2451 if (qemu_opts_foreach(qemu_find_opts("device"),
2452 device_help_func, NULL, NULL)) {
2453 exit(0);
2456 /* -L help lists the data directories and exits. */
2457 if (list_data_dirs) {
2458 qemu_list_data_dirs();
2459 exit(0);
2463 static void qemu_maybe_daemonize(const char *pid_file)
2465 Error *err;
2467 os_daemonize();
2468 rcu_disable_atfork();
2470 if (pid_file && !qemu_write_pidfile(pid_file, &err)) {
2471 error_reportf_err(err, "cannot create PID file: ");
2472 exit(1);
2475 qemu_unlink_pidfile_notifier.notify = qemu_unlink_pidfile;
2476 qemu_add_exit_notifier(&qemu_unlink_pidfile_notifier);
2479 static void qemu_init_displays(void)
2481 DisplayState *ds;
2483 /* init local displays */
2484 ds = init_displaystate();
2485 qemu_display_init(ds, &dpy);
2487 /* must be after terminal init, SDL library changes signal handlers */
2488 os_setup_signal_handling();
2490 /* init remote displays */
2491 #ifdef CONFIG_VNC
2492 qemu_opts_foreach(qemu_find_opts("vnc"),
2493 vnc_init_func, NULL, &error_fatal);
2494 #endif
2496 if (using_spice) {
2497 qemu_spice.display_init();
2501 static void qemu_init_board(void)
2503 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
2505 if (machine_class->default_ram_id && current_machine->ram_size &&
2506 numa_uses_legacy_mem() && !current_machine->ram_memdev_id) {
2507 create_default_memdev(current_machine, mem_path);
2510 /* process plugin before CPUs are created, but once -smp has been parsed */
2511 qemu_plugin_load_list(&plugin_list, &error_fatal);
2513 /* From here on we enter MACHINE_PHASE_INITIALIZED. */
2514 machine_run_board_init(current_machine);
2516 drive_check_orphaned();
2518 realtime_init();
2520 if (hax_enabled()) {
2521 /* FIXME: why isn't cpu_synchronize_all_post_init enough? */
2522 hax_sync_vcpus();
2526 static void qemu_create_cli_devices(void)
2528 soundhw_init();
2530 qemu_opts_foreach(qemu_find_opts("fw_cfg"),
2531 parse_fw_cfg, fw_cfg_find(), &error_fatal);
2533 /* init USB devices */
2534 if (machine_usb(current_machine)) {
2535 if (foreach_device_config(DEV_USB, usb_parse) < 0)
2536 exit(1);
2539 /* init generic devices */
2540 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
2541 qemu_opts_foreach(qemu_find_opts("device"),
2542 device_init_func, NULL, &error_fatal);
2543 rom_reset_order_override();
2546 static void qemu_machine_creation_done(void)
2548 MachineState *machine = MACHINE(qdev_get_machine());
2550 /* Did we create any drives that we failed to create a device for? */
2551 drive_check_orphaned();
2553 /* Don't warn about the default network setup that you get if
2554 * no command line -net or -netdev options are specified. There
2555 * are two cases that we would otherwise complain about:
2556 * (1) board doesn't support a NIC but the implicit "-net nic"
2557 * requested one
2558 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
2559 * sets up a nic that isn't connected to anything.
2561 if (!default_net && (!qtest_enabled() || has_defaults)) {
2562 net_check_clients();
2565 qdev_prop_check_globals();
2567 qdev_machine_creation_done();
2569 if (machine->cgs) {
2571 * Verify that Confidential Guest Support has actually been initialized
2573 assert(machine->cgs->ready);
2576 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
2577 exit(1);
2581 void qmp_x_exit_preconfig(Error **errp)
2583 if (phase_check(PHASE_MACHINE_INITIALIZED)) {
2584 error_setg(errp, "The command is permitted only before machine initialization");
2585 return;
2588 qemu_init_board();
2589 qemu_create_cli_devices();
2590 qemu_machine_creation_done();
2592 if (loadvm) {
2593 Error *local_err = NULL;
2594 if (!load_snapshot(loadvm, NULL, false, NULL, &local_err)) {
2595 error_report_err(local_err);
2596 autostart = 0;
2597 exit(1);
2600 if (replay_mode != REPLAY_MODE_NONE) {
2601 replay_vmstate_init();
2604 if (incoming) {
2605 Error *local_err = NULL;
2606 if (strcmp(incoming, "defer") != 0) {
2607 qmp_migrate_incoming(incoming, &local_err);
2608 if (local_err) {
2609 error_reportf_err(local_err, "-incoming %s: ", incoming);
2610 exit(1);
2613 } else if (autostart) {
2614 qmp_cont(NULL);
2618 void qemu_init(int argc, char **argv, char **envp)
2620 QemuOpts *opts;
2621 QemuOpts *icount_opts = NULL, *accel_opts = NULL;
2622 QemuOptsList *olist;
2623 int optind;
2624 const char *optarg;
2625 MachineClass *machine_class;
2626 bool userconfig = true;
2627 FILE *vmstate_dump_file = NULL;
2629 qemu_add_opts(&qemu_drive_opts);
2630 qemu_add_drive_opts(&qemu_legacy_drive_opts);
2631 qemu_add_drive_opts(&qemu_common_drive_opts);
2632 qemu_add_drive_opts(&qemu_drive_opts);
2633 qemu_add_drive_opts(&bdrv_runtime_opts);
2634 qemu_add_opts(&qemu_chardev_opts);
2635 qemu_add_opts(&qemu_device_opts);
2636 qemu_add_opts(&qemu_netdev_opts);
2637 qemu_add_opts(&qemu_nic_opts);
2638 qemu_add_opts(&qemu_net_opts);
2639 qemu_add_opts(&qemu_rtc_opts);
2640 qemu_add_opts(&qemu_global_opts);
2641 qemu_add_opts(&qemu_mon_opts);
2642 qemu_add_opts(&qemu_trace_opts);
2643 qemu_plugin_add_opts();
2644 qemu_add_opts(&qemu_option_rom_opts);
2645 qemu_add_opts(&qemu_machine_opts);
2646 qemu_add_opts(&qemu_accel_opts);
2647 qemu_add_opts(&qemu_mem_opts);
2648 qemu_add_opts(&qemu_smp_opts);
2649 qemu_add_opts(&qemu_boot_opts);
2650 qemu_add_opts(&qemu_add_fd_opts);
2651 qemu_add_opts(&qemu_object_opts);
2652 qemu_add_opts(&qemu_tpmdev_opts);
2653 qemu_add_opts(&qemu_overcommit_opts);
2654 qemu_add_opts(&qemu_msg_opts);
2655 qemu_add_opts(&qemu_name_opts);
2656 qemu_add_opts(&qemu_numa_opts);
2657 qemu_add_opts(&qemu_icount_opts);
2658 qemu_add_opts(&qemu_semihosting_config_opts);
2659 qemu_add_opts(&qemu_fw_cfg_opts);
2660 qemu_add_opts(&qemu_action_opts);
2661 module_call_init(MODULE_INIT_OPTS);
2663 error_init(argv[0]);
2664 qemu_init_exec_dir(argv[0]);
2666 qemu_init_subsystems();
2668 /* first pass of option parsing */
2669 optind = 1;
2670 while (optind < argc) {
2671 if (argv[optind][0] != '-') {
2672 /* disk image */
2673 optind++;
2674 } else {
2675 const QEMUOption *popt;
2677 popt = lookup_opt(argc, argv, &optarg, &optind);
2678 switch (popt->index) {
2679 case QEMU_OPTION_nouserconfig:
2680 userconfig = false;
2681 break;
2686 if (userconfig) {
2687 qemu_read_default_config_file(&error_fatal);
2690 /* second pass of option parsing */
2691 optind = 1;
2692 for(;;) {
2693 if (optind >= argc)
2694 break;
2695 if (argv[optind][0] != '-') {
2696 loc_set_cmdline(argv, optind, 1);
2697 drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2698 } else {
2699 const QEMUOption *popt;
2701 popt = lookup_opt(argc, argv, &optarg, &optind);
2702 if (!(popt->arch_mask & arch_type)) {
2703 error_report("Option not supported for this target");
2704 exit(1);
2706 switch(popt->index) {
2707 case QEMU_OPTION_cpu:
2708 /* hw initialization will check this */
2709 cpu_option = optarg;
2710 break;
2711 case QEMU_OPTION_hda:
2712 case QEMU_OPTION_hdb:
2713 case QEMU_OPTION_hdc:
2714 case QEMU_OPTION_hdd:
2715 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2716 HD_OPTS);
2717 break;
2718 case QEMU_OPTION_blockdev:
2720 Visitor *v;
2721 BlockdevOptionsQueueEntry *bdo;
2723 v = qobject_input_visitor_new_str(optarg, "driver",
2724 &error_fatal);
2726 bdo = g_new(BlockdevOptionsQueueEntry, 1);
2727 visit_type_BlockdevOptions(v, NULL, &bdo->bdo,
2728 &error_fatal);
2729 visit_free(v);
2730 loc_save(&bdo->loc);
2731 QSIMPLEQ_INSERT_TAIL(&bdo_queue, bdo, entry);
2732 break;
2734 case QEMU_OPTION_drive:
2735 if (drive_def(optarg) == NULL) {
2736 exit(1);
2738 break;
2739 case QEMU_OPTION_set:
2740 if (qemu_set_option(optarg) != 0)
2741 exit(1);
2742 break;
2743 case QEMU_OPTION_global:
2744 if (qemu_global_option(optarg) != 0)
2745 exit(1);
2746 break;
2747 case QEMU_OPTION_mtdblock:
2748 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2749 break;
2750 case QEMU_OPTION_sd:
2751 drive_add(IF_SD, -1, optarg, SD_OPTS);
2752 break;
2753 case QEMU_OPTION_pflash:
2754 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2755 break;
2756 case QEMU_OPTION_snapshot:
2758 Error *blocker = NULL;
2759 snapshot = 1;
2760 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
2761 "-snapshot");
2762 replay_add_blocker(blocker);
2764 break;
2765 case QEMU_OPTION_numa:
2766 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
2767 optarg, true);
2768 if (!opts) {
2769 exit(1);
2771 break;
2772 case QEMU_OPTION_display:
2773 parse_display(optarg);
2774 break;
2775 case QEMU_OPTION_nographic:
2776 olist = qemu_find_opts("machine");
2777 qemu_opts_parse_noisily(olist, "graphics=off", false);
2778 nographic = true;
2779 dpy.type = DISPLAY_TYPE_NONE;
2780 break;
2781 case QEMU_OPTION_curses:
2782 #ifdef CONFIG_CURSES
2783 dpy.type = DISPLAY_TYPE_CURSES;
2784 #else
2785 error_report("curses or iconv support is disabled");
2786 exit(1);
2787 #endif
2788 break;
2789 case QEMU_OPTION_portrait:
2790 graphic_rotate = 90;
2791 break;
2792 case QEMU_OPTION_rotate:
2793 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2794 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2795 graphic_rotate != 180 && graphic_rotate != 270) {
2796 error_report("only 90, 180, 270 deg rotation is available");
2797 exit(1);
2799 break;
2800 case QEMU_OPTION_kernel:
2801 qemu_opts_set(qemu_find_opts("machine"), "kernel", optarg, &error_abort);
2802 break;
2803 case QEMU_OPTION_initrd:
2804 qemu_opts_set(qemu_find_opts("machine"), "initrd", optarg, &error_abort);
2805 break;
2806 case QEMU_OPTION_append:
2807 qemu_opts_set(qemu_find_opts("machine"), "append", optarg, &error_abort);
2808 break;
2809 case QEMU_OPTION_dtb:
2810 qemu_opts_set(qemu_find_opts("machine"), "dtb", optarg, &error_abort);
2811 break;
2812 case QEMU_OPTION_cdrom:
2813 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
2814 break;
2815 case QEMU_OPTION_boot:
2816 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
2817 optarg, true);
2818 if (!opts) {
2819 exit(1);
2821 break;
2822 case QEMU_OPTION_fda:
2823 case QEMU_OPTION_fdb:
2824 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2825 optarg, FD_OPTS);
2826 break;
2827 case QEMU_OPTION_no_fd_bootchk:
2828 fd_bootchk = 0;
2829 break;
2830 case QEMU_OPTION_netdev:
2831 default_net = 0;
2832 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2833 exit(1);
2835 break;
2836 case QEMU_OPTION_nic:
2837 default_net = 0;
2838 if (net_client_parse(qemu_find_opts("nic"), optarg) == -1) {
2839 exit(1);
2841 break;
2842 case QEMU_OPTION_net:
2843 default_net = 0;
2844 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2845 exit(1);
2847 break;
2848 #ifdef CONFIG_LIBISCSI
2849 case QEMU_OPTION_iscsi:
2850 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
2851 optarg, false);
2852 if (!opts) {
2853 exit(1);
2855 break;
2856 #endif
2857 case QEMU_OPTION_audio_help:
2858 audio_legacy_help();
2859 exit (0);
2860 break;
2861 case QEMU_OPTION_audiodev:
2862 audio_parse_option(optarg);
2863 break;
2864 case QEMU_OPTION_soundhw:
2865 select_soundhw (optarg);
2866 break;
2867 case QEMU_OPTION_h:
2868 help(0);
2869 break;
2870 case QEMU_OPTION_version:
2871 version();
2872 exit(0);
2873 break;
2874 case QEMU_OPTION_m:
2875 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
2876 optarg, true);
2877 if (!opts) {
2878 exit(EXIT_FAILURE);
2880 break;
2881 #ifdef CONFIG_TPM
2882 case QEMU_OPTION_tpmdev:
2883 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
2884 exit(1);
2886 break;
2887 #endif
2888 case QEMU_OPTION_mempath:
2889 mem_path = optarg;
2890 break;
2891 case QEMU_OPTION_mem_prealloc:
2892 mem_prealloc = 1;
2893 break;
2894 case QEMU_OPTION_d:
2895 log_mask = optarg;
2896 break;
2897 case QEMU_OPTION_D:
2898 log_file = optarg;
2899 break;
2900 case QEMU_OPTION_DFILTER:
2901 qemu_set_dfilter_ranges(optarg, &error_fatal);
2902 break;
2903 case QEMU_OPTION_seed:
2904 qemu_guest_random_seed_main(optarg, &error_fatal);
2905 break;
2906 case QEMU_OPTION_s:
2907 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
2908 break;
2909 case QEMU_OPTION_gdb:
2910 add_device_config(DEV_GDB, optarg);
2911 break;
2912 case QEMU_OPTION_L:
2913 if (is_help_option(optarg)) {
2914 list_data_dirs = true;
2915 } else {
2916 qemu_add_data_dir(g_strdup(optarg));
2918 break;
2919 case QEMU_OPTION_bios:
2920 qemu_opts_set(qemu_find_opts("machine"), "firmware", optarg, &error_abort);
2921 break;
2922 case QEMU_OPTION_singlestep:
2923 singlestep = 1;
2924 break;
2925 case QEMU_OPTION_S:
2926 autostart = 0;
2927 break;
2928 case QEMU_OPTION_k:
2929 keyboard_layout = optarg;
2930 break;
2931 case QEMU_OPTION_vga:
2932 vga_model = optarg;
2933 default_vga = 0;
2934 break;
2935 case QEMU_OPTION_g:
2937 const char *p;
2938 int w, h, depth;
2939 p = optarg;
2940 w = strtol(p, (char **)&p, 10);
2941 if (w <= 0) {
2942 graphic_error:
2943 error_report("invalid resolution or depth");
2944 exit(1);
2946 if (*p != 'x')
2947 goto graphic_error;
2948 p++;
2949 h = strtol(p, (char **)&p, 10);
2950 if (h <= 0)
2951 goto graphic_error;
2952 if (*p == 'x') {
2953 p++;
2954 depth = strtol(p, (char **)&p, 10);
2955 if (depth != 1 && depth != 2 && depth != 4 &&
2956 depth != 8 && depth != 15 && depth != 16 &&
2957 depth != 24 && depth != 32)
2958 goto graphic_error;
2959 } else if (*p == '\0') {
2960 depth = graphic_depth;
2961 } else {
2962 goto graphic_error;
2965 graphic_width = w;
2966 graphic_height = h;
2967 graphic_depth = depth;
2969 break;
2970 case QEMU_OPTION_echr:
2972 char *r;
2973 term_escape_char = strtol(optarg, &r, 0);
2974 if (r == optarg)
2975 printf("Bad argument to echr\n");
2976 break;
2978 case QEMU_OPTION_monitor:
2979 default_monitor = 0;
2980 if (strncmp(optarg, "none", 4)) {
2981 monitor_parse(optarg, "readline", false);
2983 break;
2984 case QEMU_OPTION_qmp:
2985 monitor_parse(optarg, "control", false);
2986 default_monitor = 0;
2987 break;
2988 case QEMU_OPTION_qmp_pretty:
2989 monitor_parse(optarg, "control", true);
2990 default_monitor = 0;
2991 break;
2992 case QEMU_OPTION_mon:
2993 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
2994 true);
2995 if (!opts) {
2996 exit(1);
2998 default_monitor = 0;
2999 break;
3000 case QEMU_OPTION_chardev:
3001 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3002 optarg, true);
3003 if (!opts) {
3004 exit(1);
3006 break;
3007 case QEMU_OPTION_fsdev:
3008 olist = qemu_find_opts("fsdev");
3009 if (!olist) {
3010 error_report("fsdev support is disabled");
3011 exit(1);
3013 opts = qemu_opts_parse_noisily(olist, optarg, true);
3014 if (!opts) {
3015 exit(1);
3017 break;
3018 case QEMU_OPTION_virtfs: {
3019 QemuOpts *fsdev;
3020 QemuOpts *device;
3021 const char *writeout, *sock_fd, *socket, *path, *security_model,
3022 *multidevs;
3024 olist = qemu_find_opts("virtfs");
3025 if (!olist) {
3026 error_report("virtfs support is disabled");
3027 exit(1);
3029 opts = qemu_opts_parse_noisily(olist, optarg, true);
3030 if (!opts) {
3031 exit(1);
3034 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3035 qemu_opt_get(opts, "mount_tag") == NULL) {
3036 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3037 exit(1);
3039 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3040 qemu_opts_id(opts) ?:
3041 qemu_opt_get(opts, "mount_tag"),
3042 1, NULL);
3043 if (!fsdev) {
3044 error_report("duplicate or invalid fsdev id: %s",
3045 qemu_opt_get(opts, "mount_tag"));
3046 exit(1);
3049 writeout = qemu_opt_get(opts, "writeout");
3050 if (writeout) {
3051 #ifdef CONFIG_SYNC_FILE_RANGE
3052 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3053 #else
3054 error_report("writeout=immediate not supported "
3055 "on this platform");
3056 exit(1);
3057 #endif
3059 qemu_opt_set(fsdev, "fsdriver",
3060 qemu_opt_get(opts, "fsdriver"), &error_abort);
3061 path = qemu_opt_get(opts, "path");
3062 if (path) {
3063 qemu_opt_set(fsdev, "path", path, &error_abort);
3065 security_model = qemu_opt_get(opts, "security_model");
3066 if (security_model) {
3067 qemu_opt_set(fsdev, "security_model", security_model,
3068 &error_abort);
3070 socket = qemu_opt_get(opts, "socket");
3071 if (socket) {
3072 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3074 sock_fd = qemu_opt_get(opts, "sock_fd");
3075 if (sock_fd) {
3076 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3079 qemu_opt_set_bool(fsdev, "readonly",
3080 qemu_opt_get_bool(opts, "readonly", 0),
3081 &error_abort);
3082 multidevs = qemu_opt_get(opts, "multidevs");
3083 if (multidevs) {
3084 qemu_opt_set(fsdev, "multidevs", multidevs, &error_abort);
3086 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3087 &error_abort);
3088 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3089 qemu_opt_set(device, "fsdev",
3090 qemu_opts_id(fsdev), &error_abort);
3091 qemu_opt_set(device, "mount_tag",
3092 qemu_opt_get(opts, "mount_tag"), &error_abort);
3093 break;
3095 case QEMU_OPTION_serial:
3096 add_device_config(DEV_SERIAL, optarg);
3097 default_serial = 0;
3098 if (strncmp(optarg, "mon:", 4) == 0) {
3099 default_monitor = 0;
3101 break;
3102 case QEMU_OPTION_watchdog:
3103 if (watchdog) {
3104 error_report("only one watchdog option may be given");
3105 exit(1);
3107 watchdog = optarg;
3108 break;
3109 case QEMU_OPTION_action:
3110 olist = qemu_find_opts("action");
3111 if (!qemu_opts_parse_noisily(olist, optarg, false)) {
3112 exit(1);
3114 break;
3115 case QEMU_OPTION_watchdog_action:
3116 if (select_watchdog_action(optarg) == -1) {
3117 error_report("unknown -watchdog-action parameter");
3118 exit(1);
3120 break;
3121 case QEMU_OPTION_parallel:
3122 add_device_config(DEV_PARALLEL, optarg);
3123 default_parallel = 0;
3124 if (strncmp(optarg, "mon:", 4) == 0) {
3125 default_monitor = 0;
3127 break;
3128 case QEMU_OPTION_debugcon:
3129 add_device_config(DEV_DEBUGCON, optarg);
3130 break;
3131 case QEMU_OPTION_loadvm:
3132 loadvm = optarg;
3133 break;
3134 case QEMU_OPTION_full_screen:
3135 dpy.has_full_screen = true;
3136 dpy.full_screen = true;
3137 break;
3138 case QEMU_OPTION_alt_grab:
3139 alt_grab = 1;
3140 break;
3141 case QEMU_OPTION_ctrl_grab:
3142 ctrl_grab = 1;
3143 break;
3144 case QEMU_OPTION_no_quit:
3145 dpy.has_window_close = true;
3146 dpy.window_close = false;
3147 break;
3148 case QEMU_OPTION_sdl:
3149 #ifdef CONFIG_SDL
3150 dpy.type = DISPLAY_TYPE_SDL;
3151 break;
3152 #else
3153 error_report("SDL support is disabled");
3154 exit(1);
3155 #endif
3156 case QEMU_OPTION_pidfile:
3157 pid_file = optarg;
3158 break;
3159 case QEMU_OPTION_win2k_hack:
3160 win2k_install_hack = 1;
3161 break;
3162 case QEMU_OPTION_acpitable:
3163 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3164 optarg, true);
3165 if (!opts) {
3166 exit(1);
3168 acpi_table_add(opts, &error_fatal);
3169 break;
3170 case QEMU_OPTION_smbios:
3171 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3172 optarg, false);
3173 if (!opts) {
3174 exit(1);
3176 smbios_entry_add(opts, &error_fatal);
3177 break;
3178 case QEMU_OPTION_fwcfg:
3179 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3180 optarg, true);
3181 if (opts == NULL) {
3182 exit(1);
3184 break;
3185 case QEMU_OPTION_preconfig:
3186 preconfig_requested = true;
3187 break;
3188 case QEMU_OPTION_enable_kvm:
3189 olist = qemu_find_opts("machine");
3190 qemu_opts_parse_noisily(olist, "accel=kvm", false);
3191 break;
3192 case QEMU_OPTION_M:
3193 case QEMU_OPTION_machine:
3194 olist = qemu_find_opts("machine");
3195 opts = qemu_opts_parse_noisily(olist, optarg, true);
3196 if (!opts) {
3197 exit(1);
3199 break;
3200 case QEMU_OPTION_accel:
3201 accel_opts = qemu_opts_parse_noisily(qemu_find_opts("accel"),
3202 optarg, true);
3203 optarg = qemu_opt_get(accel_opts, "accel");
3204 if (!optarg || is_help_option(optarg)) {
3205 printf("Accelerators supported in QEMU binary:\n");
3206 GSList *el, *accel_list = object_class_get_list(TYPE_ACCEL,
3207 false);
3208 for (el = accel_list; el; el = el->next) {
3209 gchar *typename = g_strdup(object_class_get_name(
3210 OBJECT_CLASS(el->data)));
3211 /* omit qtest which is used for tests only */
3212 if (g_strcmp0(typename, ACCEL_CLASS_NAME("qtest")) &&
3213 g_str_has_suffix(typename, ACCEL_CLASS_SUFFIX)) {
3214 gchar **optname = g_strsplit(typename,
3215 ACCEL_CLASS_SUFFIX, 0);
3216 printf("%s\n", optname[0]);
3217 g_strfreev(optname);
3219 g_free(typename);
3221 g_slist_free(accel_list);
3222 exit(0);
3224 break;
3225 case QEMU_OPTION_usb:
3226 olist = qemu_find_opts("machine");
3227 qemu_opts_parse_noisily(olist, "usb=on", false);
3228 break;
3229 case QEMU_OPTION_usbdevice:
3230 olist = qemu_find_opts("machine");
3231 qemu_opts_parse_noisily(olist, "usb=on", false);
3232 add_device_config(DEV_USB, optarg);
3233 break;
3234 case QEMU_OPTION_device:
3235 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3236 optarg, true)) {
3237 exit(1);
3239 break;
3240 case QEMU_OPTION_smp:
3241 if (!qemu_opts_parse_noisily(qemu_find_opts("smp-opts"),
3242 optarg, true)) {
3243 exit(1);
3245 break;
3246 case QEMU_OPTION_vnc:
3247 vnc_parse(optarg);
3248 break;
3249 case QEMU_OPTION_no_acpi:
3250 olist = qemu_find_opts("machine");
3251 qemu_opts_parse_noisily(olist, "acpi=off", false);
3252 break;
3253 case QEMU_OPTION_no_hpet:
3254 olist = qemu_find_opts("machine");
3255 qemu_opts_parse_noisily(olist, "hpet=off", false);
3256 break;
3257 case QEMU_OPTION_no_reboot:
3258 olist = qemu_find_opts("action");
3259 qemu_opts_parse_noisily(olist, "reboot=shutdown", false);
3260 break;
3261 case QEMU_OPTION_no_shutdown:
3262 olist = qemu_find_opts("action");
3263 qemu_opts_parse_noisily(olist, "shutdown=pause", false);
3264 break;
3265 case QEMU_OPTION_uuid:
3266 if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
3267 error_report("failed to parse UUID string: wrong format");
3268 exit(1);
3270 qemu_uuid_set = true;
3271 break;
3272 case QEMU_OPTION_option_rom:
3273 if (nb_option_roms >= MAX_OPTION_ROMS) {
3274 error_report("too many option ROMs");
3275 exit(1);
3277 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3278 optarg, true);
3279 if (!opts) {
3280 exit(1);
3282 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3283 option_rom[nb_option_roms].bootindex =
3284 qemu_opt_get_number(opts, "bootindex", -1);
3285 if (!option_rom[nb_option_roms].name) {
3286 error_report("Option ROM file is not specified");
3287 exit(1);
3289 nb_option_roms++;
3290 break;
3291 case QEMU_OPTION_semihosting:
3292 qemu_semihosting_enable();
3293 break;
3294 case QEMU_OPTION_semihosting_config:
3295 if (qemu_semihosting_config_options(optarg) != 0) {
3296 exit(1);
3298 break;
3299 case QEMU_OPTION_name:
3300 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3301 optarg, true);
3302 if (!opts) {
3303 exit(1);
3305 /* Capture guest name if -msg guest-name is used later */
3306 error_guest_name = qemu_opt_get(opts, "guest");
3307 break;
3308 case QEMU_OPTION_prom_env:
3309 if (nb_prom_envs >= MAX_PROM_ENVS) {
3310 error_report("too many prom variables");
3311 exit(1);
3313 prom_envs[nb_prom_envs] = optarg;
3314 nb_prom_envs++;
3315 break;
3316 case QEMU_OPTION_old_param:
3317 old_param = 1;
3318 break;
3319 case QEMU_OPTION_rtc:
3320 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3321 false);
3322 if (!opts) {
3323 exit(1);
3325 break;
3326 case QEMU_OPTION_icount:
3327 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3328 optarg, true);
3329 if (!icount_opts) {
3330 exit(1);
3332 break;
3333 case QEMU_OPTION_incoming:
3334 if (!incoming) {
3335 runstate_set(RUN_STATE_INMIGRATE);
3337 incoming = optarg;
3338 break;
3339 case QEMU_OPTION_only_migratable:
3340 only_migratable = 1;
3341 break;
3342 case QEMU_OPTION_nodefaults:
3343 has_defaults = 0;
3344 break;
3345 case QEMU_OPTION_xen_domid:
3346 if (!(xen_available())) {
3347 error_report("Option not supported for this target");
3348 exit(1);
3350 xen_domid = atoi(optarg);
3351 break;
3352 case QEMU_OPTION_xen_attach:
3353 if (!(xen_available())) {
3354 error_report("Option not supported for this target");
3355 exit(1);
3357 xen_mode = XEN_ATTACH;
3358 break;
3359 case QEMU_OPTION_xen_domid_restrict:
3360 if (!(xen_available())) {
3361 error_report("Option not supported for this target");
3362 exit(1);
3364 xen_domid_restrict = true;
3365 break;
3366 case QEMU_OPTION_trace:
3367 trace_opt_parse(optarg);
3368 break;
3369 case QEMU_OPTION_plugin:
3370 qemu_plugin_opt_parse(optarg, &plugin_list);
3371 break;
3372 case QEMU_OPTION_readconfig:
3373 qemu_read_config_file(optarg, &error_fatal);
3374 break;
3375 case QEMU_OPTION_spice:
3376 olist = qemu_find_opts_err("spice", NULL);
3377 if (!olist) {
3378 ui_module_load_one("spice-core");
3379 olist = qemu_find_opts("spice");
3381 if (!olist) {
3382 error_report("spice support is disabled");
3383 exit(1);
3385 opts = qemu_opts_parse_noisily(olist, optarg, false);
3386 if (!opts) {
3387 exit(1);
3389 display_remote++;
3390 break;
3391 case QEMU_OPTION_writeconfig:
3393 FILE *fp;
3394 warn_report("-writeconfig is deprecated and will go away without a replacement");
3395 if (strcmp(optarg, "-") == 0) {
3396 fp = stdout;
3397 } else {
3398 fp = fopen(optarg, "w");
3399 if (fp == NULL) {
3400 error_report("open %s: %s", optarg,
3401 strerror(errno));
3402 exit(1);
3405 qemu_config_write(fp);
3406 if (fp != stdout) {
3407 fclose(fp);
3409 break;
3411 case QEMU_OPTION_qtest:
3412 qtest_chrdev = optarg;
3413 break;
3414 case QEMU_OPTION_qtest_log:
3415 qtest_log = optarg;
3416 break;
3417 case QEMU_OPTION_sandbox:
3418 olist = qemu_find_opts("sandbox");
3419 if (!olist) {
3420 #ifndef CONFIG_SECCOMP
3421 error_report("-sandbox support is not enabled "
3422 "in this QEMU binary");
3423 #endif
3424 exit(1);
3427 opts = qemu_opts_parse_noisily(olist, optarg, true);
3428 if (!opts) {
3429 exit(1);
3431 break;
3432 case QEMU_OPTION_add_fd:
3433 #ifndef _WIN32
3434 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
3435 optarg, false);
3436 if (!opts) {
3437 exit(1);
3439 #else
3440 error_report("File descriptor passing is disabled on this "
3441 "platform");
3442 exit(1);
3443 #endif
3444 break;
3445 case QEMU_OPTION_object:
3446 object_option_parse(optarg);
3447 break;
3448 case QEMU_OPTION_overcommit:
3449 opts = qemu_opts_parse_noisily(qemu_find_opts("overcommit"),
3450 optarg, false);
3451 if (!opts) {
3452 exit(1);
3454 enable_mlock = qemu_opt_get_bool(opts, "mem-lock", false);
3455 enable_cpu_pm = qemu_opt_get_bool(opts, "cpu-pm", false);
3456 break;
3457 case QEMU_OPTION_compat:
3459 CompatPolicy *opts;
3460 Visitor *v;
3462 v = qobject_input_visitor_new_str(optarg, NULL,
3463 &error_fatal);
3465 visit_type_CompatPolicy(v, NULL, &opts, &error_fatal);
3466 QAPI_CLONE_MEMBERS(CompatPolicy, &compat_policy, opts);
3468 qapi_free_CompatPolicy(opts);
3469 visit_free(v);
3470 break;
3472 case QEMU_OPTION_msg:
3473 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
3474 false);
3475 if (!opts) {
3476 exit(1);
3478 configure_msg(opts);
3479 break;
3480 case QEMU_OPTION_dump_vmstate:
3481 if (vmstate_dump_file) {
3482 error_report("only one '-dump-vmstate' "
3483 "option may be given");
3484 exit(1);
3486 vmstate_dump_file = fopen(optarg, "w");
3487 if (vmstate_dump_file == NULL) {
3488 error_report("open %s: %s", optarg, strerror(errno));
3489 exit(1);
3491 break;
3492 case QEMU_OPTION_enable_sync_profile:
3493 qsp_enable();
3494 break;
3495 case QEMU_OPTION_nouserconfig:
3496 /* Nothing to be parsed here. Especially, do not error out below. */
3497 break;
3498 default:
3499 if (os_parse_cmd_args(popt->index, optarg)) {
3500 error_report("Option not supported in this build");
3501 exit(1);
3507 * Clear error location left behind by the loop.
3508 * Best done right after the loop. Do not insert code here!
3510 loc_set_none();
3512 qemu_validate_options();
3513 qemu_process_sugar_options();
3516 * These options affect everything else and should be processed
3517 * before daemonizing.
3519 qemu_process_early_options();
3521 qemu_process_help_options();
3522 qemu_maybe_daemonize(pid_file);
3525 * The trace backend must be initialized after daemonizing.
3526 * trace_init_backends() will call st_init(), which will create the
3527 * trace thread in the parent, and also register st_flush_trace_buffer()
3528 * in atexit(). This function will force the parent to wait for the
3529 * writeout thread to finish, which will not occur, and the parent
3530 * process will be left in the host.
3532 if (!trace_init_backends()) {
3533 exit(1);
3535 trace_init_file();
3537 qemu_init_main_loop(&error_fatal);
3538 cpu_timers_init();
3540 user_register_global_props();
3541 replay_configure(icount_opts);
3543 configure_rtc(qemu_find_opts_singleton("rtc"));
3545 qemu_create_machine(select_machine());
3547 suspend_mux_open();
3549 qemu_disable_default_devices();
3550 qemu_create_default_devices();
3551 qemu_create_early_backends();
3553 qemu_apply_machine_options();
3554 phase_advance(PHASE_MACHINE_CREATED);
3557 * Note: uses machine properties such as kernel-irqchip, must run
3558 * after machine_set_property().
3560 configure_accelerators(argv[0]);
3561 phase_advance(PHASE_ACCEL_CREATED);
3564 * Beware, QOM objects created before this point miss global and
3565 * compat properties.
3567 * Global properties get set up by qdev_prop_register_global(),
3568 * called from user_register_global_props(), and certain option
3569 * desugaring. Also in CPU feature desugaring (buried in
3570 * parse_cpu_option()), which happens below this point, but may
3571 * only target the CPU type, which can only be created after
3572 * parse_cpu_option() returned the type.
3574 * Machine compat properties: object_set_machine_compat_props().
3575 * Accelerator compat props: object_set_accelerator_compat_props(),
3576 * called from do_configure_accelerator().
3579 machine_class = MACHINE_GET_CLASS(current_machine);
3580 if (!qtest_enabled() && machine_class->deprecation_reason) {
3581 error_report("Machine type '%s' is deprecated: %s",
3582 machine_class->name, machine_class->deprecation_reason);
3586 * Note: creates a QOM object, must run only after global and
3587 * compat properties have been set up.
3589 migration_object_init();
3591 qemu_create_late_backends();
3593 /* parse features once if machine provides default cpu_type */
3594 current_machine->cpu_type = machine_class->default_cpu_type;
3595 if (cpu_option) {
3596 current_machine->cpu_type = parse_cpu_option(cpu_option);
3598 /* NB: for machine none cpu_type could STILL be NULL here! */
3599 accel_init_interfaces(ACCEL_GET_CLASS(current_machine->accelerator));
3601 qemu_resolve_machine_memdev();
3602 parse_numa_opts(current_machine);
3604 if (vmstate_dump_file) {
3605 /* dump and exit */
3606 dump_vmstate_json_to_file(vmstate_dump_file);
3607 exit(0);
3610 if (!preconfig_requested) {
3611 qmp_x_exit_preconfig(&error_fatal);
3613 qemu_init_displays();
3614 accel_setup_post(current_machine);
3615 os_setup_post();
3616 resume_mux_open();