fuzz: unblock SIGALRM so the timeout works
[qemu/kevin.git] / softmmu / vl.c
blobea05bb39c501b452edfc53e0cb2b8dbc95a15942
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
25 #include "qemu/osdep.h"
26 #include "qemu-common.h"
27 #include "qemu/datadir.h"
28 #include "qemu/units.h"
29 #include "exec/cpu-common.h"
30 #include "hw/qdev-properties.h"
31 #include "qapi/compat-policy.h"
32 #include "qapi/error.h"
33 #include "qapi/qmp/qdict.h"
34 #include "qapi/qmp/qstring.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/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/qdict.h"
126 #include "qapi/qmp/qerror.h"
127 #include "sysemu/iothread.h"
128 #include "qemu/guest-random.h"
130 #include "config-host.h"
132 #define MAX_VIRTIO_CONSOLES 1
134 typedef struct BlockdevOptionsQueueEntry {
135 BlockdevOptions *bdo;
136 Location loc;
137 QSIMPLEQ_ENTRY(BlockdevOptionsQueueEntry) entry;
138 } BlockdevOptionsQueueEntry;
140 typedef QSIMPLEQ_HEAD(, BlockdevOptionsQueueEntry) BlockdevOptionsQueue;
142 typedef struct ObjectOption {
143 ObjectOptions *opts;
144 QTAILQ_ENTRY(ObjectOption) next;
145 } ObjectOption;
147 static const char *cpu_option;
148 static const char *mem_path;
149 static const char *incoming;
150 static const char *loadvm;
151 static const char *accelerators;
152 static QDict *machine_opts_dict;
153 static QTAILQ_HEAD(, ObjectOption) object_opts = QTAILQ_HEAD_INITIALIZER(object_opts);
154 static ram_addr_t maxram_size;
155 static uint64_t ram_slots;
156 static int display_remote;
157 static int snapshot;
158 static bool preconfig_requested;
159 static QemuPluginList plugin_list = QTAILQ_HEAD_INITIALIZER(plugin_list);
160 static BlockdevOptionsQueue bdo_queue = QSIMPLEQ_HEAD_INITIALIZER(bdo_queue);
161 static bool nographic = false;
162 static int mem_prealloc; /* force preallocation of physical target memory */
163 static ram_addr_t ram_size;
164 static const char *vga_model = NULL;
165 static DisplayOptions dpy;
166 static int num_serial_hds;
167 static Chardev **serial_hds;
168 static const char *log_mask;
169 static const char *log_file;
170 static bool list_data_dirs;
171 static const char *watchdog;
172 static const char *qtest_chrdev;
173 static const char *qtest_log;
175 static int has_defaults = 1;
176 static int default_serial = 1;
177 static int default_parallel = 1;
178 static int default_monitor = 1;
179 static int default_floppy = 1;
180 static int default_cdrom = 1;
181 static int default_sdcard = 1;
182 static int default_vga = 1;
183 static int default_net = 1;
185 static struct {
186 const char *driver;
187 int *flag;
188 } default_list[] = {
189 { .driver = "isa-serial", .flag = &default_serial },
190 { .driver = "isa-parallel", .flag = &default_parallel },
191 { .driver = "isa-fdc", .flag = &default_floppy },
192 { .driver = "floppy", .flag = &default_floppy },
193 { .driver = "ide-cd", .flag = &default_cdrom },
194 { .driver = "ide-hd", .flag = &default_cdrom },
195 { .driver = "scsi-cd", .flag = &default_cdrom },
196 { .driver = "scsi-hd", .flag = &default_cdrom },
197 { .driver = "VGA", .flag = &default_vga },
198 { .driver = "isa-vga", .flag = &default_vga },
199 { .driver = "cirrus-vga", .flag = &default_vga },
200 { .driver = "isa-cirrus-vga", .flag = &default_vga },
201 { .driver = "vmware-svga", .flag = &default_vga },
202 { .driver = "qxl-vga", .flag = &default_vga },
203 { .driver = "virtio-vga", .flag = &default_vga },
204 { .driver = "ati-vga", .flag = &default_vga },
205 { .driver = "vhost-user-vga", .flag = &default_vga },
206 { .driver = "virtio-vga-gl", .flag = &default_vga },
209 static QemuOptsList qemu_rtc_opts = {
210 .name = "rtc",
211 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
212 .merge_lists = true,
213 .desc = {
215 .name = "base",
216 .type = QEMU_OPT_STRING,
218 .name = "clock",
219 .type = QEMU_OPT_STRING,
221 .name = "driftfix",
222 .type = QEMU_OPT_STRING,
224 { /* end of list */ }
228 static QemuOptsList qemu_option_rom_opts = {
229 .name = "option-rom",
230 .implied_opt_name = "romfile",
231 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
232 .desc = {
234 .name = "bootindex",
235 .type = QEMU_OPT_NUMBER,
236 }, {
237 .name = "romfile",
238 .type = QEMU_OPT_STRING,
240 { /* end of list */ }
244 static QemuOptsList qemu_accel_opts = {
245 .name = "accel",
246 .implied_opt_name = "accel",
247 .head = QTAILQ_HEAD_INITIALIZER(qemu_accel_opts.head),
248 .desc = {
250 * no elements => accept any
251 * sanity checking will happen later
252 * when setting accelerator properties
258 static QemuOptsList qemu_boot_opts = {
259 .name = "boot-opts",
260 .implied_opt_name = "order",
261 .merge_lists = true,
262 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
263 .desc = {
265 .name = "order",
266 .type = QEMU_OPT_STRING,
267 }, {
268 .name = "once",
269 .type = QEMU_OPT_STRING,
270 }, {
271 .name = "menu",
272 .type = QEMU_OPT_BOOL,
273 }, {
274 .name = "splash",
275 .type = QEMU_OPT_STRING,
276 }, {
277 .name = "splash-time",
278 .type = QEMU_OPT_NUMBER,
279 }, {
280 .name = "reboot-timeout",
281 .type = QEMU_OPT_NUMBER,
282 }, {
283 .name = "strict",
284 .type = QEMU_OPT_BOOL,
286 { /*End of list */ }
290 static QemuOptsList qemu_add_fd_opts = {
291 .name = "add-fd",
292 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
293 .desc = {
295 .name = "fd",
296 .type = QEMU_OPT_NUMBER,
297 .help = "file descriptor of which a duplicate is added to fd set",
299 .name = "set",
300 .type = QEMU_OPT_NUMBER,
301 .help = "ID of the fd set to add fd to",
303 .name = "opaque",
304 .type = QEMU_OPT_STRING,
305 .help = "free-form string used to describe fd",
307 { /* end of list */ }
311 static QemuOptsList qemu_object_opts = {
312 .name = "object",
313 .implied_opt_name = "qom-type",
314 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
315 .desc = {
320 static QemuOptsList qemu_tpmdev_opts = {
321 .name = "tpmdev",
322 .implied_opt_name = "type",
323 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
324 .desc = {
325 /* options are defined in the TPM backends */
326 { /* end of list */ }
330 static QemuOptsList qemu_overcommit_opts = {
331 .name = "overcommit",
332 .head = QTAILQ_HEAD_INITIALIZER(qemu_overcommit_opts.head),
333 .desc = {
335 .name = "mem-lock",
336 .type = QEMU_OPT_BOOL,
339 .name = "cpu-pm",
340 .type = QEMU_OPT_BOOL,
342 { /* end of list */ }
346 static QemuOptsList qemu_msg_opts = {
347 .name = "msg",
348 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
349 .desc = {
351 .name = "timestamp",
352 .type = QEMU_OPT_BOOL,
355 .name = "guest-name",
356 .type = QEMU_OPT_BOOL,
357 .help = "Prepends guest name for error messages but only if "
358 "-name guest is set otherwise option is ignored\n",
360 { /* end of list */ }
364 static QemuOptsList qemu_name_opts = {
365 .name = "name",
366 .implied_opt_name = "guest",
367 .merge_lists = true,
368 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
369 .desc = {
371 .name = "guest",
372 .type = QEMU_OPT_STRING,
373 .help = "Sets the name of the guest.\n"
374 "This name will be displayed in the SDL window caption.\n"
375 "The name will also be used for the VNC server",
376 }, {
377 .name = "process",
378 .type = QEMU_OPT_STRING,
379 .help = "Sets the name of the QEMU process, as shown in top etc",
380 }, {
381 .name = "debug-threads",
382 .type = QEMU_OPT_BOOL,
383 .help = "When enabled, name the individual threads; defaults off.\n"
384 "NOTE: The thread names are for debugging and not a\n"
385 "stable API.",
387 { /* End of list */ }
391 static QemuOptsList qemu_mem_opts = {
392 .name = "memory",
393 .implied_opt_name = "size",
394 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
395 .merge_lists = true,
396 .desc = {
398 .name = "size",
399 .type = QEMU_OPT_SIZE,
402 .name = "slots",
403 .type = QEMU_OPT_NUMBER,
406 .name = "maxmem",
407 .type = QEMU_OPT_SIZE,
409 { /* end of list */ }
413 static QemuOptsList qemu_icount_opts = {
414 .name = "icount",
415 .implied_opt_name = "shift",
416 .merge_lists = true,
417 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
418 .desc = {
420 .name = "shift",
421 .type = QEMU_OPT_STRING,
422 }, {
423 .name = "align",
424 .type = QEMU_OPT_BOOL,
425 }, {
426 .name = "sleep",
427 .type = QEMU_OPT_BOOL,
428 }, {
429 .name = "rr",
430 .type = QEMU_OPT_STRING,
431 }, {
432 .name = "rrfile",
433 .type = QEMU_OPT_STRING,
434 }, {
435 .name = "rrsnapshot",
436 .type = QEMU_OPT_STRING,
438 { /* end of list */ }
442 static QemuOptsList qemu_fw_cfg_opts = {
443 .name = "fw_cfg",
444 .implied_opt_name = "name",
445 .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
446 .desc = {
448 .name = "name",
449 .type = QEMU_OPT_STRING,
450 .help = "Sets the fw_cfg name of the blob to be inserted",
451 }, {
452 .name = "file",
453 .type = QEMU_OPT_STRING,
454 .help = "Sets the name of the file from which "
455 "the fw_cfg blob will be loaded",
456 }, {
457 .name = "string",
458 .type = QEMU_OPT_STRING,
459 .help = "Sets content of the blob to be inserted from a string",
460 }, {
461 .name = "gen_id",
462 .type = QEMU_OPT_STRING,
463 .help = "Sets id of the object generating the fw_cfg blob "
464 "to be inserted",
466 { /* end of list */ }
470 static QemuOptsList qemu_action_opts = {
471 .name = "action",
472 .merge_lists = true,
473 .head = QTAILQ_HEAD_INITIALIZER(qemu_action_opts.head),
474 .desc = {
476 .name = "shutdown",
477 .type = QEMU_OPT_STRING,
479 .name = "reboot",
480 .type = QEMU_OPT_STRING,
482 .name = "panic",
483 .type = QEMU_OPT_STRING,
485 .name = "watchdog",
486 .type = QEMU_OPT_STRING,
488 { /* end of list */ }
492 const char *qemu_get_vm_name(void)
494 return qemu_name;
497 static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
499 const char *driver = qemu_opt_get(opts, "driver");
500 int i;
502 if (!driver)
503 return 0;
504 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
505 if (strcmp(default_list[i].driver, driver) != 0)
506 continue;
507 *(default_list[i].flag) = 0;
509 return 0;
512 static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
514 const char *proc_name;
516 if (qemu_opt_get(opts, "debug-threads")) {
517 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
519 qemu_name = qemu_opt_get(opts, "guest");
521 proc_name = qemu_opt_get(opts, "process");
522 if (proc_name) {
523 os_set_proc_name(proc_name);
526 return 0;
529 bool defaults_enabled(void)
531 return has_defaults;
534 #ifndef _WIN32
535 static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
537 int fd, dupfd, flags;
538 int64_t fdset_id;
539 const char *fd_opaque = NULL;
540 AddfdInfo *fdinfo;
542 fd = qemu_opt_get_number(opts, "fd", -1);
543 fdset_id = qemu_opt_get_number(opts, "set", -1);
544 fd_opaque = qemu_opt_get(opts, "opaque");
546 if (fd < 0) {
547 error_setg(errp, "fd option is required and must be non-negative");
548 return -1;
551 if (fd <= STDERR_FILENO) {
552 error_setg(errp, "fd cannot be a standard I/O stream");
553 return -1;
557 * All fds inherited across exec() necessarily have FD_CLOEXEC
558 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
560 flags = fcntl(fd, F_GETFD);
561 if (flags == -1 || (flags & FD_CLOEXEC)) {
562 error_setg(errp, "fd is not valid or already in use");
563 return -1;
566 if (fdset_id < 0) {
567 error_setg(errp, "set option is required and must be non-negative");
568 return -1;
571 #ifdef F_DUPFD_CLOEXEC
572 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
573 #else
574 dupfd = dup(fd);
575 if (dupfd != -1) {
576 qemu_set_cloexec(dupfd);
578 #endif
579 if (dupfd == -1) {
580 error_setg(errp, "error duplicating fd: %s", strerror(errno));
581 return -1;
584 /* add the duplicate fd, and optionally the opaque string, to the fd set */
585 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
586 &error_abort);
587 g_free(fdinfo);
589 return 0;
592 static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
594 int fd;
596 fd = qemu_opt_get_number(opts, "fd", -1);
597 close(fd);
599 return 0;
601 #endif
603 /***********************************************************/
604 /* QEMU Block devices */
606 #define HD_OPTS "media=disk"
607 #define CDROM_OPTS "media=cdrom"
608 #define FD_OPTS ""
609 #define PFLASH_OPTS ""
610 #define MTD_OPTS ""
611 #define SD_OPTS ""
613 static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
615 BlockInterfaceType *block_default_type = opaque;
617 return drive_new(opts, *block_default_type, errp) == NULL;
620 static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
622 if (qemu_opt_get(opts, "snapshot") == NULL) {
623 qemu_opt_set(opts, "snapshot", "on", &error_abort);
625 return 0;
628 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
629 int index, const char *optstr)
631 QemuOpts *opts;
632 DriveInfo *dinfo;
634 if (!enable || drive_get_by_index(type, index)) {
635 return;
638 opts = drive_add(type, index, NULL, optstr);
639 if (snapshot) {
640 drive_enable_snapshot(NULL, opts, NULL);
643 dinfo = drive_new(opts, type, &error_abort);
644 dinfo->is_default = true;
648 static void configure_blockdev(BlockdevOptionsQueue *bdo_queue,
649 MachineClass *machine_class, int snapshot)
652 * If the currently selected machine wishes to override the
653 * units-per-bus property of its default HBA interface type, do so
654 * now.
656 if (machine_class->units_per_default_bus) {
657 override_max_devs(machine_class->block_default_type,
658 machine_class->units_per_default_bus);
661 /* open the virtual block devices */
662 while (!QSIMPLEQ_EMPTY(bdo_queue)) {
663 BlockdevOptionsQueueEntry *bdo = QSIMPLEQ_FIRST(bdo_queue);
665 QSIMPLEQ_REMOVE_HEAD(bdo_queue, entry);
666 loc_push_restore(&bdo->loc);
667 qmp_blockdev_add(bdo->bdo, &error_fatal);
668 loc_pop(&bdo->loc);
669 qapi_free_BlockdevOptions(bdo->bdo);
670 g_free(bdo);
672 if (snapshot) {
673 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
674 NULL, NULL);
676 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
677 &machine_class->block_default_type, &error_fatal)) {
678 /* We printed help */
679 exit(0);
682 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
683 CDROM_OPTS);
684 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
685 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
689 static QemuOptsList qemu_smp_opts = {
690 .name = "smp-opts",
691 .implied_opt_name = "cpus",
692 .merge_lists = true,
693 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
694 .desc = {
696 .name = "cpus",
697 .type = QEMU_OPT_NUMBER,
698 }, {
699 .name = "sockets",
700 .type = QEMU_OPT_NUMBER,
701 }, {
702 .name = "dies",
703 .type = QEMU_OPT_NUMBER,
704 }, {
705 .name = "cores",
706 .type = QEMU_OPT_NUMBER,
707 }, {
708 .name = "threads",
709 .type = QEMU_OPT_NUMBER,
710 }, {
711 .name = "maxcpus",
712 .type = QEMU_OPT_NUMBER,
714 { /*End of list */ }
718 static void realtime_init(void)
720 if (enable_mlock) {
721 if (os_mlock() < 0) {
722 error_report("locking memory failed");
723 exit(1);
729 static void configure_msg(QemuOpts *opts)
731 message_with_timestamp = qemu_opt_get_bool(opts, "timestamp", false);
732 error_with_guestname = qemu_opt_get_bool(opts, "guest-name", false);
736 /***********************************************************/
737 /* USB devices */
739 static int usb_device_add(const char *devname)
741 USBDevice *dev = NULL;
743 if (!machine_usb(current_machine)) {
744 return -1;
747 dev = usbdevice_create(devname);
748 if (!dev)
749 return -1;
751 return 0;
754 static int usb_parse(const char *cmdline)
756 int r;
757 r = usb_device_add(cmdline);
758 if (r < 0) {
759 error_report("could not add USB device '%s'", cmdline);
761 return r;
764 /***********************************************************/
765 /* machine registration */
767 static MachineClass *find_machine(const char *name, GSList *machines)
769 GSList *el;
771 for (el = machines; el; el = el->next) {
772 MachineClass *mc = el->data;
774 if (!strcmp(mc->name, name) || !g_strcmp0(mc->alias, name)) {
775 return mc;
779 return NULL;
782 static MachineClass *find_default_machine(GSList *machines)
784 GSList *el;
785 MachineClass *default_machineclass = NULL;
787 for (el = machines; el; el = el->next) {
788 MachineClass *mc = el->data;
790 if (mc->is_default) {
791 assert(default_machineclass == NULL && "Multiple default machines");
792 default_machineclass = mc;
796 return default_machineclass;
799 static void version(void)
801 printf("QEMU emulator version " QEMU_FULL_VERSION "\n"
802 QEMU_COPYRIGHT "\n");
805 static void help(int exitcode)
807 version();
808 printf("usage: %s [options] [disk_image]\n\n"
809 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
810 error_get_progname());
812 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
813 if ((arch_mask) & arch_type) \
814 fputs(opt_help, stdout);
816 #define ARCHHEADING(text, arch_mask) \
817 if ((arch_mask) & arch_type) \
818 puts(stringify(text));
820 #define DEFHEADING(text) ARCHHEADING(text, QEMU_ARCH_ALL)
822 #include "qemu-options.def"
824 printf("\nDuring emulation, the following keys are useful:\n"
825 "ctrl-alt-f toggle full screen\n"
826 "ctrl-alt-n switch to virtual console 'n'\n"
827 "ctrl-alt toggle mouse and keyboard grab\n"
828 "\n"
829 "When using -nographic, press 'ctrl-a h' to get some help.\n"
830 "\n"
831 QEMU_HELP_BOTTOM "\n");
833 exit(exitcode);
836 #define HAS_ARG 0x0001
838 typedef struct QEMUOption {
839 const char *name;
840 int flags;
841 int index;
842 uint32_t arch_mask;
843 } QEMUOption;
845 static const QEMUOption qemu_options[] = {
846 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
848 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
849 { option, opt_arg, opt_enum, arch_mask },
850 #define DEFHEADING(text)
851 #define ARCHHEADING(text, arch_mask)
853 #include "qemu-options.def"
854 { NULL },
857 typedef struct VGAInterfaceInfo {
858 const char *opt_name; /* option name */
859 const char *name; /* human-readable name */
860 /* Class names indicating that support is available.
861 * If no class is specified, the interface is always available */
862 const char *class_names[2];
863 } VGAInterfaceInfo;
865 static const VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
866 [VGA_NONE] = {
867 .opt_name = "none",
868 .name = "no graphic card",
870 [VGA_STD] = {
871 .opt_name = "std",
872 .name = "standard VGA",
873 .class_names = { "VGA", "isa-vga" },
875 [VGA_CIRRUS] = {
876 .opt_name = "cirrus",
877 .name = "Cirrus VGA",
878 .class_names = { "cirrus-vga", "isa-cirrus-vga" },
880 [VGA_VMWARE] = {
881 .opt_name = "vmware",
882 .name = "VMWare SVGA",
883 .class_names = { "vmware-svga" },
885 [VGA_VIRTIO] = {
886 .opt_name = "virtio",
887 .name = "Virtio VGA",
888 .class_names = { "virtio-vga" },
890 [VGA_QXL] = {
891 .opt_name = "qxl",
892 .name = "QXL VGA",
893 .class_names = { "qxl-vga" },
895 [VGA_TCX] = {
896 .opt_name = "tcx",
897 .name = "TCX framebuffer",
898 .class_names = { "sun-tcx" },
900 [VGA_CG3] = {
901 .opt_name = "cg3",
902 .name = "CG3 framebuffer",
903 .class_names = { "cgthree" },
905 [VGA_XENFB] = {
906 .opt_name = "xenfb",
907 .name = "Xen paravirtualized framebuffer",
911 static bool vga_interface_available(VGAInterfaceType t)
913 const VGAInterfaceInfo *ti = &vga_interfaces[t];
915 assert(t < VGA_TYPE_MAX);
916 return !ti->class_names[0] ||
917 module_object_class_by_name(ti->class_names[0]) ||
918 module_object_class_by_name(ti->class_names[1]);
921 static const char *
922 get_default_vga_model(const MachineClass *machine_class)
924 if (machine_class->default_display) {
925 return machine_class->default_display;
926 } else if (vga_interface_available(VGA_CIRRUS)) {
927 return "cirrus";
928 } else if (vga_interface_available(VGA_STD)) {
929 return "std";
932 return NULL;
935 static void select_vgahw(const MachineClass *machine_class, const char *p)
937 const char *opts;
938 int t;
940 if (g_str_equal(p, "help")) {
941 const char *def = get_default_vga_model(machine_class);
943 for (t = 0; t < VGA_TYPE_MAX; t++) {
944 const VGAInterfaceInfo *ti = &vga_interfaces[t];
946 if (vga_interface_available(t) && ti->opt_name) {
947 printf("%-20s %s%s\n", ti->opt_name, ti->name ?: "",
948 g_str_equal(ti->opt_name, def) ? " (default)" : "");
951 exit(0);
954 assert(vga_interface_type == VGA_NONE);
955 for (t = 0; t < VGA_TYPE_MAX; t++) {
956 const VGAInterfaceInfo *ti = &vga_interfaces[t];
957 if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
958 if (!vga_interface_available(t)) {
959 error_report("%s not available", ti->name);
960 exit(1);
962 vga_interface_type = t;
963 break;
966 if (t == VGA_TYPE_MAX) {
967 invalid_vga:
968 error_report("unknown vga type: %s", p);
969 exit(1);
971 while (*opts) {
972 const char *nextopt;
974 if (strstart(opts, ",retrace=", &nextopt)) {
975 opts = nextopt;
976 if (strstart(opts, "dumb", &nextopt))
977 vga_retrace_method = VGA_RETRACE_DUMB;
978 else if (strstart(opts, "precise", &nextopt))
979 vga_retrace_method = VGA_RETRACE_PRECISE;
980 else goto invalid_vga;
981 } else goto invalid_vga;
982 opts = nextopt;
986 static void parse_display_qapi(const char *optarg)
988 DisplayOptions *opts;
989 Visitor *v;
991 v = qobject_input_visitor_new_str(optarg, "type", &error_fatal);
993 visit_type_DisplayOptions(v, NULL, &opts, &error_fatal);
994 QAPI_CLONE_MEMBERS(DisplayOptions, &dpy, opts);
996 qapi_free_DisplayOptions(opts);
997 visit_free(v);
1000 DisplayOptions *qmp_query_display_options(Error **errp)
1002 return QAPI_CLONE(DisplayOptions, &dpy);
1005 static void parse_display(const char *p)
1007 const char *opts;
1009 if (is_help_option(p)) {
1010 qemu_display_help();
1011 exit(0);
1014 if (strstart(p, "sdl", &opts)) {
1016 * sdl DisplayType needs hand-crafted parser instead of
1017 * parse_display_qapi() due to some options not in
1018 * DisplayOptions, specifically:
1019 * - ctrl_grab + alt_grab
1020 * Not clear yet what happens to them long-term. Should
1021 * replaced by something better or deprecated and dropped.
1023 #if defined(CONFIG_SDL)
1024 dpy.type = DISPLAY_TYPE_SDL;
1025 while (*opts) {
1026 const char *nextopt;
1028 if (strstart(opts, ",alt_grab=", &nextopt)) {
1029 opts = nextopt;
1030 if (strstart(opts, "on", &nextopt)) {
1031 alt_grab = 1;
1032 } else if (strstart(opts, "off", &nextopt)) {
1033 alt_grab = 0;
1034 } else {
1035 goto invalid_sdl_args;
1037 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1038 opts = nextopt;
1039 if (strstart(opts, "on", &nextopt)) {
1040 ctrl_grab = 1;
1041 } else if (strstart(opts, "off", &nextopt)) {
1042 ctrl_grab = 0;
1043 } else {
1044 goto invalid_sdl_args;
1046 } else if (strstart(opts, ",window_close=", &nextopt) ||
1047 strstart(opts, ",window-close=", &nextopt)) {
1048 if (strstart(opts, ",window_close=", NULL)) {
1049 warn_report("window_close with an underscore is deprecated,"
1050 " please use window-close instead.");
1052 opts = nextopt;
1053 dpy.has_window_close = true;
1054 if (strstart(opts, "on", &nextopt)) {
1055 dpy.window_close = true;
1056 } else if (strstart(opts, "off", &nextopt)) {
1057 dpy.window_close = false;
1058 } else {
1059 goto invalid_sdl_args;
1061 } else if (strstart(opts, ",show-cursor=", &nextopt)) {
1062 opts = nextopt;
1063 dpy.has_show_cursor = true;
1064 if (strstart(opts, "on", &nextopt)) {
1065 dpy.show_cursor = true;
1066 } else if (strstart(opts, "off", &nextopt)) {
1067 dpy.show_cursor = false;
1068 } else {
1069 goto invalid_sdl_args;
1071 } else if (strstart(opts, ",gl=", &nextopt)) {
1072 opts = nextopt;
1073 dpy.has_gl = true;
1074 if (strstart(opts, "on", &nextopt)) {
1075 dpy.gl = DISPLAYGL_MODE_ON;
1076 } else if (strstart(opts, "core", &nextopt)) {
1077 dpy.gl = DISPLAYGL_MODE_CORE;
1078 } else if (strstart(opts, "es", &nextopt)) {
1079 dpy.gl = DISPLAYGL_MODE_ES;
1080 } else if (strstart(opts, "off", &nextopt)) {
1081 dpy.gl = DISPLAYGL_MODE_OFF;
1082 } else {
1083 goto invalid_sdl_args;
1085 } else {
1086 invalid_sdl_args:
1087 error_report("invalid SDL option string");
1088 exit(1);
1090 opts = nextopt;
1092 #else
1093 error_report("SDL display supported is not available in this binary");
1094 exit(1);
1095 #endif
1096 } else if (strstart(p, "vnc", &opts)) {
1098 * vnc isn't a (local) DisplayType but a protocol for remote
1099 * display access.
1101 if (*opts == '=') {
1102 vnc_parse(opts + 1);
1103 } else {
1104 error_report("VNC requires a display argument vnc=<display>");
1105 exit(1);
1107 } else {
1108 parse_display_qapi(p);
1112 static inline bool nonempty_str(const char *str)
1114 return str && *str;
1117 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
1119 gchar *buf;
1120 size_t size;
1121 const char *name, *file, *str, *gen_id;
1122 FWCfgState *fw_cfg = (FWCfgState *) opaque;
1124 if (fw_cfg == NULL) {
1125 error_setg(errp, "fw_cfg device not available");
1126 return -1;
1128 name = qemu_opt_get(opts, "name");
1129 file = qemu_opt_get(opts, "file");
1130 str = qemu_opt_get(opts, "string");
1131 gen_id = qemu_opt_get(opts, "gen_id");
1133 /* we need the name, and exactly one of: file, content string, gen_id */
1134 if (!nonempty_str(name) ||
1135 nonempty_str(file) + nonempty_str(str) + nonempty_str(gen_id) != 1) {
1136 error_setg(errp, "name, plus exactly one of file,"
1137 " string and gen_id, are needed");
1138 return -1;
1140 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
1141 error_setg(errp, "name too long (max. %d char)",
1142 FW_CFG_MAX_FILE_PATH - 1);
1143 return -1;
1145 if (nonempty_str(gen_id)) {
1147 * In this particular case where the content is populated
1148 * internally, the "etc/" namespace protection is relaxed,
1149 * so do not emit a warning.
1151 } else if (strncmp(name, "opt/", 4) != 0) {
1152 warn_report("externally provided fw_cfg item names "
1153 "should be prefixed with \"opt/\"");
1155 if (nonempty_str(str)) {
1156 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
1157 buf = g_memdup(str, size);
1158 } else if (nonempty_str(gen_id)) {
1159 if (!fw_cfg_add_from_generator(fw_cfg, name, gen_id, errp)) {
1160 return -1;
1162 return 0;
1163 } else {
1164 GError *err = NULL;
1165 if (!g_file_get_contents(file, &buf, &size, &err)) {
1166 error_setg(errp, "can't load %s: %s", file, err->message);
1167 g_error_free(err);
1168 return -1;
1171 /* For legacy, keep user files in a specific global order. */
1172 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
1173 fw_cfg_add_file(fw_cfg, name, buf, size);
1174 fw_cfg_reset_order_override(fw_cfg);
1175 return 0;
1178 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
1180 return qdev_device_help(opts);
1183 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
1185 DeviceState *dev;
1187 dev = qdev_device_add(opts, errp);
1188 if (!dev && *errp) {
1189 error_report_err(*errp);
1190 return -1;
1191 } else if (dev) {
1192 object_unref(OBJECT(dev));
1194 return 0;
1197 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
1199 Error *local_err = NULL;
1201 if (!qemu_chr_new_from_opts(opts, NULL, &local_err)) {
1202 if (local_err) {
1203 error_propagate(errp, local_err);
1204 return -1;
1206 exit(0);
1208 return 0;
1211 #ifdef CONFIG_VIRTFS
1212 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
1214 return qemu_fsdev_add(opts, errp);
1216 #endif
1218 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
1220 return monitor_init_opts(opts, errp);
1223 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
1225 static int monitor_device_index = 0;
1226 QemuOpts *opts;
1227 const char *p;
1228 char label[32];
1230 if (strstart(optarg, "chardev:", &p)) {
1231 snprintf(label, sizeof(label), "%s", p);
1232 } else {
1233 snprintf(label, sizeof(label), "compat_monitor%d",
1234 monitor_device_index);
1235 opts = qemu_chr_parse_compat(label, optarg, true);
1236 if (!opts) {
1237 error_report("parse error: %s", optarg);
1238 exit(1);
1242 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
1243 qemu_opt_set(opts, "mode", mode, &error_abort);
1244 qemu_opt_set(opts, "chardev", label, &error_abort);
1245 if (!strcmp(mode, "control")) {
1246 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
1247 } else {
1248 assert(pretty == false);
1250 monitor_device_index++;
1253 struct device_config {
1254 enum {
1255 DEV_USB, /* -usbdevice */
1256 DEV_SERIAL, /* -serial */
1257 DEV_PARALLEL, /* -parallel */
1258 DEV_DEBUGCON, /* -debugcon */
1259 DEV_GDB, /* -gdb, -s */
1260 DEV_SCLP, /* s390 sclp */
1261 } type;
1262 const char *cmdline;
1263 Location loc;
1264 QTAILQ_ENTRY(device_config) next;
1267 static QTAILQ_HEAD(, device_config) device_configs =
1268 QTAILQ_HEAD_INITIALIZER(device_configs);
1270 static void add_device_config(int type, const char *cmdline)
1272 struct device_config *conf;
1274 conf = g_malloc0(sizeof(*conf));
1275 conf->type = type;
1276 conf->cmdline = cmdline;
1277 loc_save(&conf->loc);
1278 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1281 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1283 struct device_config *conf;
1284 int rc;
1286 QTAILQ_FOREACH(conf, &device_configs, next) {
1287 if (conf->type != type)
1288 continue;
1289 loc_push_restore(&conf->loc);
1290 rc = func(conf->cmdline);
1291 loc_pop(&conf->loc);
1292 if (rc) {
1293 return rc;
1296 return 0;
1299 static void qemu_disable_default_devices(void)
1301 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1303 qemu_opts_foreach(qemu_find_opts("device"),
1304 default_driver_check, NULL, NULL);
1305 qemu_opts_foreach(qemu_find_opts("global"),
1306 default_driver_check, NULL, NULL);
1308 if (!vga_model && !default_vga) {
1309 vga_interface_type = VGA_DEVICE;
1311 if (!has_defaults || machine_class->no_serial) {
1312 default_serial = 0;
1314 if (!has_defaults || machine_class->no_parallel) {
1315 default_parallel = 0;
1317 if (!has_defaults || machine_class->no_floppy) {
1318 default_floppy = 0;
1320 if (!has_defaults || machine_class->no_cdrom) {
1321 default_cdrom = 0;
1323 if (!has_defaults || machine_class->no_sdcard) {
1324 default_sdcard = 0;
1326 if (!has_defaults) {
1327 default_monitor = 0;
1328 default_net = 0;
1329 default_vga = 0;
1333 static void qemu_create_default_devices(void)
1335 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1337 if (is_daemonized()) {
1338 /* According to documentation and historically, -nographic redirects
1339 * serial port, parallel port and monitor to stdio, which does not work
1340 * with -daemonize. We can redirect these to null instead, but since
1341 * -nographic is legacy, let's just error out.
1342 * We disallow -nographic only if all other ports are not redirected
1343 * explicitly, to not break existing legacy setups which uses
1344 * -nographic _and_ redirects all ports explicitly - this is valid
1345 * usage, -nographic is just a no-op in this case.
1347 if (nographic
1348 && (default_parallel || default_serial || default_monitor)) {
1349 error_report("-nographic cannot be used with -daemonize");
1350 exit(1);
1354 if (nographic) {
1355 if (default_parallel)
1356 add_device_config(DEV_PARALLEL, "null");
1357 if (default_serial && default_monitor) {
1358 add_device_config(DEV_SERIAL, "mon:stdio");
1359 } else {
1360 if (default_serial)
1361 add_device_config(DEV_SERIAL, "stdio");
1362 if (default_monitor)
1363 monitor_parse("stdio", "readline", false);
1365 } else {
1366 if (default_serial)
1367 add_device_config(DEV_SERIAL, "vc:80Cx24C");
1368 if (default_parallel)
1369 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
1370 if (default_monitor)
1371 monitor_parse("vc:80Cx24C", "readline", false);
1374 if (default_net) {
1375 QemuOptsList *net = qemu_find_opts("net");
1376 qemu_opts_parse(net, "nic", true, &error_abort);
1377 #ifdef CONFIG_SLIRP
1378 qemu_opts_parse(net, "user", true, &error_abort);
1379 #endif
1382 #if defined(CONFIG_VNC)
1383 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
1384 display_remote++;
1386 #endif
1387 if (dpy.type == DISPLAY_TYPE_DEFAULT && !display_remote) {
1388 if (!qemu_display_find_default(&dpy)) {
1389 dpy.type = DISPLAY_TYPE_NONE;
1390 #if defined(CONFIG_VNC)
1391 vnc_parse("localhost:0,to=99,id=default");
1392 #endif
1395 if (dpy.type == DISPLAY_TYPE_DEFAULT) {
1396 dpy.type = DISPLAY_TYPE_NONE;
1399 /* If no default VGA is requested, the default is "none". */
1400 if (default_vga) {
1401 vga_model = get_default_vga_model(machine_class);
1403 if (vga_model) {
1404 select_vgahw(machine_class, vga_model);
1408 static int serial_parse(const char *devname)
1410 int index = num_serial_hds;
1411 char label[32];
1413 if (strcmp(devname, "none") == 0)
1414 return 0;
1415 snprintf(label, sizeof(label), "serial%d", index);
1416 serial_hds = g_renew(Chardev *, serial_hds, index + 1);
1418 serial_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
1419 if (!serial_hds[index]) {
1420 error_report("could not connect serial device"
1421 " to character backend '%s'", devname);
1422 return -1;
1424 num_serial_hds++;
1425 return 0;
1428 Chardev *serial_hd(int i)
1430 assert(i >= 0);
1431 if (i < num_serial_hds) {
1432 return serial_hds[i];
1434 return NULL;
1437 static int parallel_parse(const char *devname)
1439 static int index = 0;
1440 char label[32];
1442 if (strcmp(devname, "none") == 0)
1443 return 0;
1444 if (index == MAX_PARALLEL_PORTS) {
1445 error_report("too many parallel ports");
1446 exit(1);
1448 snprintf(label, sizeof(label), "parallel%d", index);
1449 parallel_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
1450 if (!parallel_hds[index]) {
1451 error_report("could not connect parallel device"
1452 " to character backend '%s'", devname);
1453 return -1;
1455 index++;
1456 return 0;
1459 static int debugcon_parse(const char *devname)
1461 QemuOpts *opts;
1463 if (!qemu_chr_new_mux_mon("debugcon", devname, NULL)) {
1464 error_report("invalid character backend '%s'", devname);
1465 exit(1);
1467 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
1468 if (!opts) {
1469 error_report("already have a debugcon device");
1470 exit(1);
1472 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
1473 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
1474 return 0;
1477 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
1479 const MachineClass *mc1 = a, *mc2 = b;
1480 int res;
1482 if (mc1->family == NULL) {
1483 if (mc2->family == NULL) {
1484 /* Compare standalone machine types against each other; they sort
1485 * in increasing order.
1487 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
1488 object_class_get_name(OBJECT_CLASS(mc2)));
1491 /* Standalone machine types sort after families. */
1492 return 1;
1495 if (mc2->family == NULL) {
1496 /* Families sort before standalone machine types. */
1497 return -1;
1500 /* Families sort between each other alphabetically increasingly. */
1501 res = strcmp(mc1->family, mc2->family);
1502 if (res != 0) {
1503 return res;
1506 /* Within the same family, machine types sort in decreasing order. */
1507 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
1508 object_class_get_name(OBJECT_CLASS(mc1)));
1511 static void machine_help_func(const QDict *qdict)
1513 GSList *machines, *el;
1514 const char *type = qdict_get_try_str(qdict, "type");
1516 machines = object_class_get_list(TYPE_MACHINE, false);
1517 if (type) {
1518 ObjectClass *machine_class = OBJECT_CLASS(find_machine(type, machines));
1519 if (machine_class) {
1520 type_print_class_properties(object_class_get_name(machine_class));
1521 return;
1525 printf("Supported machines are:\n");
1526 machines = g_slist_sort(machines, machine_class_cmp);
1527 for (el = machines; el; el = el->next) {
1528 MachineClass *mc = el->data;
1529 if (mc->alias) {
1530 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
1532 printf("%-20s %s%s%s\n", mc->name, mc->desc,
1533 mc->is_default ? " (default)" : "",
1534 mc->deprecation_reason ? " (deprecated)" : "");
1538 static void
1539 machine_merge_property(const char *propname, QDict *prop, Error **errp)
1541 QDict *opts;
1543 opts = qdict_new();
1544 /* Preserve the caller's reference to prop. */
1545 qobject_ref(prop);
1546 qdict_put(opts, propname, prop);
1547 keyval_merge(machine_opts_dict, opts, errp);
1548 qobject_unref(opts);
1551 static void
1552 machine_parse_property_opt(QemuOptsList *opts_list, const char *propname,
1553 const char *arg)
1555 QDict *prop = NULL;
1556 bool help = false;
1558 prop = keyval_parse(arg, opts_list->implied_opt_name, &help, &error_fatal);
1559 if (help) {
1560 qemu_opts_print_help(opts_list, true);
1561 exit(0);
1563 machine_merge_property(propname, prop, &error_fatal);
1564 qobject_unref(prop);
1567 static const char *pid_file;
1568 static Notifier qemu_unlink_pidfile_notifier;
1570 static void qemu_unlink_pidfile(Notifier *n, void *data)
1572 if (pid_file) {
1573 unlink(pid_file);
1577 static const QEMUOption *lookup_opt(int argc, char **argv,
1578 const char **poptarg, int *poptind)
1580 const QEMUOption *popt;
1581 int optind = *poptind;
1582 char *r = argv[optind];
1583 const char *optarg;
1585 loc_set_cmdline(argv, optind, 1);
1586 optind++;
1587 /* Treat --foo the same as -foo. */
1588 if (r[1] == '-')
1589 r++;
1590 popt = qemu_options;
1591 for(;;) {
1592 if (!popt->name) {
1593 error_report("invalid option");
1594 exit(1);
1596 if (!strcmp(popt->name, r + 1))
1597 break;
1598 popt++;
1600 if (popt->flags & HAS_ARG) {
1601 if (optind >= argc) {
1602 error_report("requires an argument");
1603 exit(1);
1605 optarg = argv[optind++];
1606 loc_set_cmdline(argv, optind - 2, 2);
1607 } else {
1608 optarg = NULL;
1611 *poptarg = optarg;
1612 *poptind = optind;
1614 return popt;
1617 static MachineClass *select_machine(QDict *qdict, Error **errp)
1619 const char *optarg = qdict_get_try_str(qdict, "type");
1620 GSList *machines = object_class_get_list(TYPE_MACHINE, false);
1621 MachineClass *machine_class;
1622 Error *local_err = NULL;
1624 if (optarg) {
1625 machine_class = find_machine(optarg, machines);
1626 qdict_del(qdict, "type");
1627 if (!machine_class) {
1628 error_setg(&local_err, "unsupported machine type");
1630 } else {
1631 machine_class = find_default_machine(machines);
1632 if (!machine_class) {
1633 error_setg(&local_err, "No machine specified, and there is no default");
1637 g_slist_free(machines);
1638 if (local_err) {
1639 error_append_hint(&local_err, "Use -machine help to list supported machines\n");
1640 error_propagate(errp, local_err);
1642 return machine_class;
1645 static int object_parse_property_opt(Object *obj,
1646 const char *name, const char *value,
1647 const char *skip, Error **errp)
1649 if (g_str_equal(name, skip)) {
1650 return 0;
1653 if (!object_property_parse(obj, name, value, errp)) {
1654 return -1;
1657 return 0;
1660 /* *Non*recursively replace underscores with dashes in QDict keys. */
1661 static void keyval_dashify(QDict *qdict, Error **errp)
1663 const QDictEntry *ent, *next;
1664 char *p;
1666 for (ent = qdict_first(qdict); ent; ent = next) {
1667 g_autofree char *new_key = NULL;
1669 next = qdict_next(qdict, ent);
1670 if (!strchr(ent->key, '_')) {
1671 continue;
1673 new_key = g_strdup(ent->key);
1674 for (p = new_key; *p; p++) {
1675 if (*p == '_') {
1676 *p = '-';
1679 if (qdict_haskey(qdict, new_key)) {
1680 error_setg(errp, "Conflict between '%s' and '%s'", ent->key, new_key);
1681 return;
1683 qobject_ref(ent->value);
1684 qdict_put_obj(qdict, new_key, ent->value);
1685 qdict_del(qdict, ent->key);
1689 static void qemu_apply_legacy_machine_options(QDict *qdict)
1691 const char *value;
1693 keyval_dashify(qdict, &error_fatal);
1695 /* Legacy options do not correspond to MachineState properties. */
1696 value = qdict_get_try_str(qdict, "accel");
1697 if (value) {
1698 accelerators = g_strdup(value);
1699 qdict_del(qdict, "accel");
1702 value = qdict_get_try_str(qdict, "igd-passthru");
1703 if (value) {
1704 object_register_sugar_prop(ACCEL_CLASS_NAME("xen"), "igd-passthru", value,
1705 false);
1706 qdict_del(qdict, "igd-passthru");
1709 value = qdict_get_try_str(qdict, "kvm-shadow-mem");
1710 if (value) {
1711 object_register_sugar_prop(ACCEL_CLASS_NAME("kvm"), "kvm-shadow-mem", value,
1712 false);
1713 qdict_del(qdict, "kvm-shadow-mem");
1716 value = qdict_get_try_str(qdict, "kernel-irqchip");
1717 if (value) {
1718 object_register_sugar_prop(ACCEL_CLASS_NAME("kvm"), "kernel-irqchip", value,
1719 false);
1720 object_register_sugar_prop(ACCEL_CLASS_NAME("whpx"), "kernel-irqchip", value,
1721 false);
1722 qdict_del(qdict, "kernel-irqchip");
1726 static void object_option_foreach_add(bool (*type_opt_predicate)(const char *))
1728 ObjectOption *opt, *next;
1730 QTAILQ_FOREACH_SAFE(opt, &object_opts, next, next) {
1731 const char *type = ObjectType_str(opt->opts->qom_type);
1732 if (type_opt_predicate(type)) {
1733 user_creatable_add_qapi(opt->opts, &error_fatal);
1734 qapi_free_ObjectOptions(opt->opts);
1735 QTAILQ_REMOVE(&object_opts, opt, next);
1736 g_free(opt);
1741 static void object_option_add_visitor(Visitor *v)
1743 ObjectOption *opt = g_new0(ObjectOption, 1);
1744 visit_type_ObjectOptions(v, NULL, &opt->opts, &error_fatal);
1745 QTAILQ_INSERT_TAIL(&object_opts, opt, next);
1748 static void object_option_parse(const char *optarg)
1750 QemuOpts *opts;
1751 const char *type;
1752 Visitor *v;
1754 if (optarg[0] == '{') {
1755 QObject *obj = qobject_from_json(optarg, &error_fatal);
1757 v = qobject_input_visitor_new(obj);
1758 qobject_unref(obj);
1759 } else {
1760 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
1761 optarg, true);
1762 if (!opts) {
1763 exit(1);
1766 type = qemu_opt_get(opts, "qom-type");
1767 if (!type) {
1768 error_setg(&error_fatal, QERR_MISSING_PARAMETER, "qom-type");
1770 if (user_creatable_print_help(type, opts)) {
1771 exit(0);
1774 v = opts_visitor_new(opts);
1777 object_option_add_visitor(v);
1778 visit_free(v);
1782 * Initial object creation happens before all other
1783 * QEMU data types are created. The majority of objects
1784 * can be created at this point. The rng-egd object
1785 * cannot be created here, as it depends on the chardev
1786 * already existing.
1788 static bool object_create_early(const char *type)
1791 * Objects should not be made "delayed" without a reason. If you
1792 * add one, state the reason in a comment!
1795 /* Reason: property "chardev" */
1796 if (g_str_equal(type, "rng-egd") ||
1797 g_str_equal(type, "qtest")) {
1798 return false;
1801 #if defined(CONFIG_VHOST_USER) && defined(CONFIG_LINUX)
1802 /* Reason: cryptodev-vhost-user property "chardev" */
1803 if (g_str_equal(type, "cryptodev-vhost-user")) {
1804 return false;
1806 #endif
1808 /* Reason: vhost-user-blk-server property "node-name" */
1809 if (g_str_equal(type, "vhost-user-blk-server")) {
1810 return false;
1813 * Reason: filter-* property "netdev" etc.
1815 if (g_str_equal(type, "filter-buffer") ||
1816 g_str_equal(type, "filter-dump") ||
1817 g_str_equal(type, "filter-mirror") ||
1818 g_str_equal(type, "filter-redirector") ||
1819 g_str_equal(type, "colo-compare") ||
1820 g_str_equal(type, "filter-rewriter") ||
1821 g_str_equal(type, "filter-replay")) {
1822 return false;
1826 * Allocation of large amounts of memory may delay
1827 * chardev initialization for too long, and trigger timeouts
1828 * on software that waits for a monitor socket to be created
1829 * (e.g. libvirt).
1831 if (g_str_has_prefix(type, "memory-backend-")) {
1832 return false;
1835 return true;
1838 static void qemu_apply_machine_options(QDict *qdict)
1840 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1841 const char *boot_order = NULL;
1842 const char *boot_once = NULL;
1843 QemuOpts *opts;
1845 object_set_properties_from_keyval(OBJECT(current_machine), qdict, false, &error_fatal);
1846 current_machine->ram_size = ram_size;
1847 current_machine->maxram_size = maxram_size;
1848 current_machine->ram_slots = ram_slots;
1850 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
1851 if (opts) {
1852 boot_order = qemu_opt_get(opts, "order");
1853 if (boot_order) {
1854 validate_bootdevices(boot_order, &error_fatal);
1857 boot_once = qemu_opt_get(opts, "once");
1858 if (boot_once) {
1859 validate_bootdevices(boot_once, &error_fatal);
1862 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
1863 boot_strict = qemu_opt_get_bool(opts, "strict", false);
1866 if (!boot_order) {
1867 boot_order = machine_class->default_boot_order;
1870 current_machine->boot_order = boot_order;
1871 current_machine->boot_once = boot_once;
1873 if (semihosting_enabled() && !semihosting_get_argc()) {
1874 /* fall back to the -kernel/-append */
1875 semihosting_arg_fallback(current_machine->kernel_filename, current_machine->kernel_cmdline);
1878 if (current_machine->smp.cpus > 1) {
1879 Error *blocker = NULL;
1880 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
1881 replay_add_blocker(blocker);
1885 static void qemu_create_early_backends(void)
1887 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1888 #if defined(CONFIG_SDL)
1889 const bool use_sdl = (dpy.type == DISPLAY_TYPE_SDL);
1890 #else
1891 const bool use_sdl = false;
1892 #endif
1893 #if defined(CONFIG_GTK)
1894 const bool use_gtk = (dpy.type == DISPLAY_TYPE_GTK);
1895 #else
1896 const bool use_gtk = false;
1897 #endif
1899 if ((alt_grab || ctrl_grab) && !use_sdl) {
1900 error_report("-alt-grab and -ctrl-grab are only valid "
1901 "for SDL, ignoring option");
1903 if (dpy.has_window_close && !use_gtk && !use_sdl) {
1904 error_report("-no-quit is only valid for GTK and SDL, "
1905 "ignoring option");
1908 qemu_display_early_init(&dpy);
1909 qemu_console_early_init();
1911 if (dpy.has_gl && dpy.gl != DISPLAYGL_MODE_OFF && display_opengl == 0) {
1912 #if defined(CONFIG_OPENGL)
1913 error_report("OpenGL is not supported by the display");
1914 #else
1915 error_report("OpenGL support is disabled");
1916 #endif
1917 exit(1);
1920 object_option_foreach_add(object_create_early);
1922 /* spice needs the timers to be initialized by this point */
1923 /* spice must initialize before audio as it changes the default auiodev */
1924 /* spice must initialize before chardevs (for spicevmc and spiceport) */
1925 qemu_spice.init();
1927 qemu_opts_foreach(qemu_find_opts("chardev"),
1928 chardev_init_func, NULL, &error_fatal);
1930 #ifdef CONFIG_VIRTFS
1931 qemu_opts_foreach(qemu_find_opts("fsdev"),
1932 fsdev_init_func, NULL, &error_fatal);
1933 #endif
1936 * Note: we need to create audio and block backends before
1937 * setting machine properties, so they can be referred to.
1939 configure_blockdev(&bdo_queue, machine_class, snapshot);
1940 audio_init_audiodevs();
1945 * The remainder of object creation happens after the
1946 * creation of chardev, fsdev, net clients and device data types.
1948 static bool object_create_late(const char *type)
1950 return !object_create_early(type);
1953 static void qemu_create_late_backends(void)
1955 if (qtest_chrdev) {
1956 qtest_server_init(qtest_chrdev, qtest_log, &error_fatal);
1959 net_init_clients(&error_fatal);
1961 object_option_foreach_add(object_create_late);
1963 if (tpm_init() < 0) {
1964 exit(1);
1967 qemu_opts_foreach(qemu_find_opts("mon"),
1968 mon_init_func, NULL, &error_fatal);
1970 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
1971 exit(1);
1972 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
1973 exit(1);
1974 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
1975 exit(1);
1977 /* now chardevs have been created we may have semihosting to connect */
1978 qemu_semihosting_connect_chardevs();
1979 qemu_semihosting_console_init();
1982 static bool have_custom_ram_size(void)
1984 QemuOpts *opts = qemu_find_opts_singleton("memory");
1985 return !!qemu_opt_get_size(opts, "size", 0);
1988 static void qemu_resolve_machine_memdev(void)
1990 if (current_machine->ram_memdev_id) {
1991 Object *backend;
1992 ram_addr_t backend_size;
1994 backend = object_resolve_path_type(current_machine->ram_memdev_id,
1995 TYPE_MEMORY_BACKEND, NULL);
1996 if (!backend) {
1997 error_report("Memory backend '%s' not found",
1998 current_machine->ram_memdev_id);
1999 exit(EXIT_FAILURE);
2001 backend_size = object_property_get_uint(backend, "size", &error_abort);
2002 if (have_custom_ram_size() && backend_size != ram_size) {
2003 error_report("Size specified by -m option must match size of "
2004 "explicitly specified 'memory-backend' property");
2005 exit(EXIT_FAILURE);
2007 if (mem_path) {
2008 error_report("'-mem-path' can't be used together with"
2009 "'-machine memory-backend'");
2010 exit(EXIT_FAILURE);
2012 ram_size = backend_size;
2015 if (!xen_enabled()) {
2016 /* On 32-bit hosts, QEMU is limited by virtual address space */
2017 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
2018 error_report("at most 2047 MB RAM can be simulated");
2019 exit(1);
2024 static void set_memory_options(MachineClass *mc)
2026 uint64_t sz;
2027 const char *mem_str;
2028 const ram_addr_t default_ram_size = mc->default_ram_size;
2029 QemuOpts *opts = qemu_find_opts_singleton("memory");
2030 Location loc;
2032 loc_push_none(&loc);
2033 qemu_opts_loc_restore(opts);
2035 sz = 0;
2036 mem_str = qemu_opt_get(opts, "size");
2037 if (mem_str) {
2038 if (!*mem_str) {
2039 error_report("missing 'size' option value");
2040 exit(EXIT_FAILURE);
2043 sz = qemu_opt_get_size(opts, "size", ram_size);
2045 /* Fix up legacy suffix-less format */
2046 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2047 uint64_t overflow_check = sz;
2049 sz *= MiB;
2050 if (sz / MiB != overflow_check) {
2051 error_report("too large 'size' option value");
2052 exit(EXIT_FAILURE);
2057 /* backward compatibility behaviour for case "-m 0" */
2058 if (sz == 0) {
2059 sz = default_ram_size;
2062 sz = QEMU_ALIGN_UP(sz, 8192);
2063 if (mc->fixup_ram_size) {
2064 sz = mc->fixup_ram_size(sz);
2066 ram_size = sz;
2067 if (ram_size != sz) {
2068 error_report("ram size too large");
2069 exit(EXIT_FAILURE);
2072 maxram_size = ram_size;
2074 if (qemu_opt_get(opts, "maxmem")) {
2075 uint64_t slots;
2077 sz = qemu_opt_get_size(opts, "maxmem", 0);
2078 slots = qemu_opt_get_number(opts, "slots", 0);
2079 if (sz < ram_size) {
2080 error_report("invalid value of -m option maxmem: "
2081 "maximum memory size (0x%" PRIx64 ") must be at least "
2082 "the initial memory size (0x" RAM_ADDR_FMT ")",
2083 sz, ram_size);
2084 exit(EXIT_FAILURE);
2085 } else if (slots && sz == ram_size) {
2086 error_report("invalid value of -m option maxmem: "
2087 "memory slots were specified but maximum memory size "
2088 "(0x%" PRIx64 ") is equal to the initial memory size "
2089 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
2090 exit(EXIT_FAILURE);
2093 maxram_size = sz;
2094 ram_slots = slots;
2095 } else if (qemu_opt_get(opts, "slots")) {
2096 error_report("invalid -m option value: missing 'maxmem' option");
2097 exit(EXIT_FAILURE);
2100 loc_pop(&loc);
2103 static void qemu_create_machine(QDict *qdict)
2105 MachineClass *machine_class = select_machine(qdict, &error_fatal);
2106 object_set_machine_compat_props(machine_class->compat_props);
2108 set_memory_options(machine_class);
2110 current_machine = MACHINE(object_new_with_class(OBJECT_CLASS(machine_class)));
2111 object_property_add_child(object_get_root(), "machine",
2112 OBJECT(current_machine));
2113 object_property_add_child(container_get(OBJECT(current_machine),
2114 "/unattached"),
2115 "sysbus", OBJECT(sysbus_get_default()));
2117 if (machine_class->minimum_page_bits) {
2118 if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
2119 /* This would be a board error: specifying a minimum smaller than
2120 * a target's compile-time fixed setting.
2122 g_assert_not_reached();
2126 cpu_exec_init_all();
2127 page_size_init();
2129 if (machine_class->hw_version) {
2130 qemu_set_hw_version(machine_class->hw_version);
2134 * Get the default machine options from the machine if it is not already
2135 * specified either by the configuration file or by the command line.
2137 if (machine_class->default_machine_opts) {
2138 QDict *default_opts =
2139 keyval_parse(machine_class->default_machine_opts, NULL, NULL,
2140 &error_abort);
2141 qemu_apply_legacy_machine_options(default_opts);
2142 object_set_properties_from_keyval(OBJECT(current_machine), default_opts,
2143 false, &error_abort);
2144 qobject_unref(default_opts);
2148 static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
2150 GlobalProperty *g;
2152 g = g_malloc0(sizeof(*g));
2153 g->driver = qemu_opt_get(opts, "driver");
2154 g->property = qemu_opt_get(opts, "property");
2155 g->value = qemu_opt_get(opts, "value");
2156 qdev_prop_register_global(g);
2157 return 0;
2161 * Return whether configuration group @group is stored in QemuOpts, or
2162 * recorded as one or more QDicts by qemu_record_config_group.
2164 static bool is_qemuopts_group(const char *group)
2166 if (g_str_equal(group, "object") ||
2167 g_str_equal(group, "machine") ||
2168 g_str_equal(group, "smp-opts")) {
2169 return false;
2171 return true;
2174 static void qemu_record_config_group(const char *group, QDict *dict,
2175 bool from_json, Error **errp)
2177 if (g_str_equal(group, "object")) {
2178 Visitor *v = qobject_input_visitor_new_keyval(QOBJECT(dict));
2179 object_option_add_visitor(v);
2180 visit_free(v);
2181 } else if (g_str_equal(group, "machine")) {
2183 * Cannot merge string-valued and type-safe dictionaries, so JSON
2184 * is not accepted yet for -M.
2186 assert(!from_json);
2187 keyval_merge(machine_opts_dict, dict, errp);
2188 } else if (g_str_equal(group, "smp-opts")) {
2189 machine_merge_property("smp", dict, &error_fatal);
2190 } else {
2191 abort();
2196 * Parse non-QemuOpts config file groups, pass the rest to
2197 * qemu_config_do_parse.
2199 static void qemu_parse_config_group(const char *group, QDict *qdict,
2200 void *opaque, Error **errp)
2202 QObject *crumpled;
2203 if (is_qemuopts_group(group)) {
2204 qemu_config_do_parse(group, qdict, opaque, errp);
2205 return;
2208 crumpled = qdict_crumple(qdict, errp);
2209 if (!crumpled) {
2210 return;
2212 switch (qobject_type(crumpled)) {
2213 case QTYPE_QDICT:
2214 qemu_record_config_group(group, qobject_to(QDict, crumpled), false, errp);
2215 break;
2216 case QTYPE_QLIST:
2217 error_setg(errp, "Lists cannot be at top level of a configuration section");
2218 break;
2219 default:
2220 g_assert_not_reached();
2222 qobject_unref(crumpled);
2225 static void qemu_read_default_config_file(Error **errp)
2227 ERRP_GUARD();
2228 int ret;
2229 g_autofree char *file = get_relocated_path(CONFIG_QEMU_CONFDIR "/qemu.conf");
2231 ret = qemu_read_config_file(file, qemu_parse_config_group, errp);
2232 if (ret < 0) {
2233 if (ret == -ENOENT) {
2234 error_free(*errp);
2235 *errp = NULL;
2240 static void qemu_set_option(const char *str, Error **errp)
2242 char group[64], id[64], arg[64];
2243 QemuOptsList *list;
2244 QemuOpts *opts;
2245 int rc, offset;
2247 rc = sscanf(str, "%63[^.].%63[^.].%63[^=]%n", group, id, arg, &offset);
2248 if (rc < 3 || str[offset] != '=') {
2249 error_setg(errp, "can't parse: \"%s\"", str);
2250 return;
2253 if (!is_qemuopts_group(group)) {
2254 error_setg(errp, "-set is not supported with %s", group);
2255 } else {
2256 list = qemu_find_opts_err(group, errp);
2257 if (list) {
2258 opts = qemu_opts_find(list, id);
2259 if (!opts) {
2260 error_setg(errp, "there is no %s \"%s\" defined", group, id);
2261 return;
2263 qemu_opt_set(opts, arg, str + offset + 1, errp);
2268 static void user_register_global_props(void)
2270 qemu_opts_foreach(qemu_find_opts("global"),
2271 global_init_func, NULL, NULL);
2274 static int do_configure_icount(void *opaque, QemuOpts *opts, Error **errp)
2276 icount_configure(opts, errp);
2277 return 0;
2280 static int accelerator_set_property(void *opaque,
2281 const char *name, const char *value,
2282 Error **errp)
2284 return object_parse_property_opt(opaque, name, value, "accel", errp);
2287 static int do_configure_accelerator(void *opaque, QemuOpts *opts, Error **errp)
2289 bool *p_init_failed = opaque;
2290 const char *acc = qemu_opt_get(opts, "accel");
2291 AccelClass *ac = accel_find(acc);
2292 AccelState *accel;
2293 int ret;
2294 bool qtest_with_kvm;
2296 qtest_with_kvm = g_str_equal(acc, "kvm") && qtest_chrdev != NULL;
2298 if (!ac) {
2299 *p_init_failed = true;
2300 if (!qtest_with_kvm) {
2301 error_report("invalid accelerator %s", acc);
2303 return 0;
2305 accel = ACCEL(object_new_with_class(OBJECT_CLASS(ac)));
2306 object_apply_compat_props(OBJECT(accel));
2307 qemu_opt_foreach(opts, accelerator_set_property,
2308 accel,
2309 &error_fatal);
2311 ret = accel_init_machine(accel, current_machine);
2312 if (ret < 0) {
2313 *p_init_failed = true;
2314 if (!qtest_with_kvm || ret != -ENOENT) {
2315 error_report("failed to initialize %s: %s", acc, strerror(-ret));
2317 return 0;
2320 return 1;
2323 static void configure_accelerators(const char *progname)
2325 bool init_failed = false;
2327 qemu_opts_foreach(qemu_find_opts("icount"),
2328 do_configure_icount, NULL, &error_fatal);
2330 if (QTAILQ_EMPTY(&qemu_accel_opts.head)) {
2331 char **accel_list, **tmp;
2333 if (accelerators == NULL) {
2334 /* Select the default accelerator */
2335 bool have_tcg = accel_find("tcg");
2336 bool have_kvm = accel_find("kvm");
2338 if (have_tcg && have_kvm) {
2339 if (g_str_has_suffix(progname, "kvm")) {
2340 /* If the program name ends with "kvm", we prefer KVM */
2341 accelerators = "kvm:tcg";
2342 } else {
2343 accelerators = "tcg:kvm";
2345 } else if (have_kvm) {
2346 accelerators = "kvm";
2347 } else if (have_tcg) {
2348 accelerators = "tcg";
2349 } else {
2350 error_report("No accelerator selected and"
2351 " no default accelerator available");
2352 exit(1);
2355 accel_list = g_strsplit(accelerators, ":", 0);
2357 for (tmp = accel_list; *tmp; tmp++) {
2359 * Filter invalid accelerators here, to prevent obscenities
2360 * such as "-machine accel=tcg,,thread=single".
2362 if (accel_find(*tmp)) {
2363 qemu_opts_parse_noisily(qemu_find_opts("accel"), *tmp, true);
2364 } else {
2365 init_failed = true;
2366 error_report("invalid accelerator %s", *tmp);
2369 g_strfreev(accel_list);
2370 } else {
2371 if (accelerators != NULL) {
2372 error_report("The -accel and \"-machine accel=\" options are incompatible");
2373 exit(1);
2377 if (!qemu_opts_foreach(qemu_find_opts("accel"),
2378 do_configure_accelerator, &init_failed, &error_fatal)) {
2379 if (!init_failed) {
2380 error_report("no accelerator found");
2382 exit(1);
2385 if (init_failed && !qtest_chrdev) {
2386 AccelClass *ac = ACCEL_GET_CLASS(current_accel());
2387 error_report("falling back to %s", ac->name);
2390 if (icount_enabled() && !tcg_enabled()) {
2391 error_report("-icount is not allowed with hardware virtualization");
2392 exit(1);
2396 static void create_default_memdev(MachineState *ms, const char *path)
2398 Object *obj;
2399 MachineClass *mc = MACHINE_GET_CLASS(ms);
2401 obj = object_new(path ? TYPE_MEMORY_BACKEND_FILE : TYPE_MEMORY_BACKEND_RAM);
2402 if (path) {
2403 object_property_set_str(obj, "mem-path", path, &error_fatal);
2405 object_property_set_int(obj, "size", ms->ram_size, &error_fatal);
2406 object_property_add_child(object_get_objects_root(), mc->default_ram_id,
2407 obj);
2408 /* Ensure backend's memory region name is equal to mc->default_ram_id */
2409 object_property_set_bool(obj, "x-use-canonical-path-for-ramblock-id",
2410 false, &error_fatal);
2411 user_creatable_complete(USER_CREATABLE(obj), &error_fatal);
2412 object_unref(obj);
2413 object_property_set_str(OBJECT(ms), "memory-backend", mc->default_ram_id,
2414 &error_fatal);
2417 static void qemu_validate_options(const QDict *machine_opts)
2419 const char *kernel_filename = qdict_get_try_str(machine_opts, "kernel");
2420 const char *initrd_filename = qdict_get_try_str(machine_opts, "initrd");
2421 const char *kernel_cmdline = qdict_get_try_str(machine_opts, "append");
2423 if (kernel_filename == NULL) {
2424 if (kernel_cmdline != NULL) {
2425 error_report("-append only allowed with -kernel option");
2426 exit(1);
2429 if (initrd_filename != NULL) {
2430 error_report("-initrd only allowed with -kernel option");
2431 exit(1);
2435 if (loadvm && preconfig_requested) {
2436 error_report("'preconfig' and 'loadvm' options are "
2437 "mutually exclusive");
2438 exit(EXIT_FAILURE);
2440 if (incoming && preconfig_requested && strcmp(incoming, "defer") != 0) {
2441 error_report("'preconfig' supports '-incoming defer' only");
2442 exit(EXIT_FAILURE);
2445 #ifdef CONFIG_CURSES
2446 if (is_daemonized() && dpy.type == DISPLAY_TYPE_CURSES) {
2447 error_report("curses display cannot be used with -daemonize");
2448 exit(1);
2450 #endif
2453 static void qemu_process_sugar_options(void)
2455 if (mem_prealloc) {
2456 QObject *smp = qdict_get(machine_opts_dict, "smp");
2457 if (smp && qobject_type(smp) == QTYPE_QDICT) {
2458 QObject *cpus = qdict_get(qobject_to(QDict, smp), "cpus");
2459 if (cpus && qobject_type(cpus) == QTYPE_QSTRING) {
2460 const char *val = qstring_get_str(qobject_to(QString, cpus));
2461 object_register_sugar_prop("memory-backend", "prealloc-threads",
2462 val, false);
2465 object_register_sugar_prop("memory-backend", "prealloc", "on", false);
2468 if (watchdog) {
2469 int i = select_watchdog(watchdog);
2470 if (i > 0)
2471 exit (i == 1 ? 1 : 0);
2475 /* -action processing */
2478 * Process all the -action parameters parsed from cmdline.
2480 static int process_runstate_actions(void *opaque, QemuOpts *opts, Error **errp)
2482 Error *local_err = NULL;
2483 QDict *qdict = qemu_opts_to_qdict(opts, NULL);
2484 QObject *ret = NULL;
2485 qmp_marshal_set_action(qdict, &ret, &local_err);
2486 qobject_unref(ret);
2487 qobject_unref(qdict);
2488 if (local_err) {
2489 error_propagate(errp, local_err);
2490 return 1;
2492 return 0;
2495 static void qemu_process_early_options(void)
2497 #ifdef CONFIG_SECCOMP
2498 QemuOptsList *olist = qemu_find_opts_err("sandbox", NULL);
2499 if (olist) {
2500 qemu_opts_foreach(olist, parse_sandbox, NULL, &error_fatal);
2502 #endif
2504 qemu_opts_foreach(qemu_find_opts("name"),
2505 parse_name, NULL, &error_fatal);
2507 if (qemu_opts_foreach(qemu_find_opts("action"),
2508 process_runstate_actions, NULL, &error_fatal)) {
2509 exit(1);
2512 #ifndef _WIN32
2513 qemu_opts_foreach(qemu_find_opts("add-fd"),
2514 parse_add_fd, NULL, &error_fatal);
2516 qemu_opts_foreach(qemu_find_opts("add-fd"),
2517 cleanup_add_fd, NULL, &error_fatal);
2518 #endif
2520 /* Open the logfile at this point and set the log mask if necessary. */
2521 if (log_file) {
2522 qemu_set_log_filename(log_file, &error_fatal);
2524 if (log_mask) {
2525 int mask;
2526 mask = qemu_str_to_log_mask(log_mask);
2527 if (!mask) {
2528 qemu_print_log_usage(stdout);
2529 exit(1);
2531 qemu_set_log(mask);
2532 } else {
2533 qemu_set_log(0);
2536 qemu_add_default_firmwarepath();
2539 static void qemu_process_help_options(void)
2542 * Check for -cpu help and -device help before we call select_machine(),
2543 * which will return an error if the architecture has no default machine
2544 * type and the user did not specify one, so that the user doesn't need
2545 * to say '-cpu help -machine something'.
2547 if (cpu_option && is_help_option(cpu_option)) {
2548 list_cpus(cpu_option);
2549 exit(0);
2552 if (qemu_opts_foreach(qemu_find_opts("device"),
2553 device_help_func, NULL, NULL)) {
2554 exit(0);
2557 /* -L help lists the data directories and exits. */
2558 if (list_data_dirs) {
2559 qemu_list_data_dirs();
2560 exit(0);
2564 static void qemu_maybe_daemonize(const char *pid_file)
2566 Error *err = NULL;
2568 os_daemonize();
2569 rcu_disable_atfork();
2571 if (pid_file && !qemu_write_pidfile(pid_file, &err)) {
2572 error_reportf_err(err, "cannot create PID file: ");
2573 exit(1);
2576 qemu_unlink_pidfile_notifier.notify = qemu_unlink_pidfile;
2577 qemu_add_exit_notifier(&qemu_unlink_pidfile_notifier);
2580 static void qemu_init_displays(void)
2582 DisplayState *ds;
2584 /* init local displays */
2585 ds = init_displaystate();
2586 qemu_display_init(ds, &dpy);
2588 /* must be after terminal init, SDL library changes signal handlers */
2589 os_setup_signal_handling();
2591 /* init remote displays */
2592 #ifdef CONFIG_VNC
2593 qemu_opts_foreach(qemu_find_opts("vnc"),
2594 vnc_init_func, NULL, &error_fatal);
2595 #endif
2597 if (using_spice) {
2598 qemu_spice.display_init();
2602 static void qemu_init_board(void)
2604 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
2606 if (machine_class->default_ram_id && current_machine->ram_size &&
2607 numa_uses_legacy_mem() && !current_machine->ram_memdev_id) {
2608 create_default_memdev(current_machine, mem_path);
2611 /* process plugin before CPUs are created, but once -smp has been parsed */
2612 qemu_plugin_load_list(&plugin_list, &error_fatal);
2614 /* From here on we enter MACHINE_PHASE_INITIALIZED. */
2615 machine_run_board_init(current_machine);
2617 drive_check_orphaned();
2619 realtime_init();
2621 if (hax_enabled()) {
2622 /* FIXME: why isn't cpu_synchronize_all_post_init enough? */
2623 hax_sync_vcpus();
2627 static void qemu_create_cli_devices(void)
2629 soundhw_init();
2631 qemu_opts_foreach(qemu_find_opts("fw_cfg"),
2632 parse_fw_cfg, fw_cfg_find(), &error_fatal);
2634 /* init USB devices */
2635 if (machine_usb(current_machine)) {
2636 if (foreach_device_config(DEV_USB, usb_parse) < 0)
2637 exit(1);
2640 /* init generic devices */
2641 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
2642 qemu_opts_foreach(qemu_find_opts("device"),
2643 device_init_func, NULL, &error_fatal);
2644 rom_reset_order_override();
2647 static void qemu_machine_creation_done(void)
2649 MachineState *machine = MACHINE(qdev_get_machine());
2651 /* Did we create any drives that we failed to create a device for? */
2652 drive_check_orphaned();
2654 /* Don't warn about the default network setup that you get if
2655 * no command line -net or -netdev options are specified. There
2656 * are two cases that we would otherwise complain about:
2657 * (1) board doesn't support a NIC but the implicit "-net nic"
2658 * requested one
2659 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
2660 * sets up a nic that isn't connected to anything.
2662 if (!default_net && (!qtest_enabled() || has_defaults)) {
2663 net_check_clients();
2666 qdev_prop_check_globals();
2668 qdev_machine_creation_done();
2670 if (machine->cgs) {
2672 * Verify that Confidential Guest Support has actually been initialized
2674 assert(machine->cgs->ready);
2677 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
2678 exit(1);
2682 void qmp_x_exit_preconfig(Error **errp)
2684 if (phase_check(PHASE_MACHINE_INITIALIZED)) {
2685 error_setg(errp, "The command is permitted only before machine initialization");
2686 return;
2689 qemu_init_board();
2690 qemu_create_cli_devices();
2691 qemu_machine_creation_done();
2693 if (loadvm) {
2694 load_snapshot(loadvm, NULL, false, NULL, &error_fatal);
2696 if (replay_mode != REPLAY_MODE_NONE) {
2697 replay_vmstate_init();
2700 if (incoming) {
2701 Error *local_err = NULL;
2702 if (strcmp(incoming, "defer") != 0) {
2703 qmp_migrate_incoming(incoming, &local_err);
2704 if (local_err) {
2705 error_reportf_err(local_err, "-incoming %s: ", incoming);
2706 exit(1);
2709 } else if (autostart) {
2710 qmp_cont(NULL);
2714 void qemu_init(int argc, char **argv, char **envp)
2716 QemuOpts *opts;
2717 QemuOpts *icount_opts = NULL, *accel_opts = NULL;
2718 QemuOptsList *olist;
2719 int optind;
2720 const char *optarg;
2721 MachineClass *machine_class;
2722 bool userconfig = true;
2723 FILE *vmstate_dump_file = NULL;
2725 qemu_add_opts(&qemu_drive_opts);
2726 qemu_add_drive_opts(&qemu_legacy_drive_opts);
2727 qemu_add_drive_opts(&qemu_common_drive_opts);
2728 qemu_add_drive_opts(&qemu_drive_opts);
2729 qemu_add_drive_opts(&bdrv_runtime_opts);
2730 qemu_add_opts(&qemu_chardev_opts);
2731 qemu_add_opts(&qemu_device_opts);
2732 qemu_add_opts(&qemu_netdev_opts);
2733 qemu_add_opts(&qemu_nic_opts);
2734 qemu_add_opts(&qemu_net_opts);
2735 qemu_add_opts(&qemu_rtc_opts);
2736 qemu_add_opts(&qemu_global_opts);
2737 qemu_add_opts(&qemu_mon_opts);
2738 qemu_add_opts(&qemu_trace_opts);
2739 qemu_plugin_add_opts();
2740 qemu_add_opts(&qemu_option_rom_opts);
2741 qemu_add_opts(&qemu_accel_opts);
2742 qemu_add_opts(&qemu_mem_opts);
2743 qemu_add_opts(&qemu_smp_opts);
2744 qemu_add_opts(&qemu_boot_opts);
2745 qemu_add_opts(&qemu_add_fd_opts);
2746 qemu_add_opts(&qemu_object_opts);
2747 qemu_add_opts(&qemu_tpmdev_opts);
2748 qemu_add_opts(&qemu_overcommit_opts);
2749 qemu_add_opts(&qemu_msg_opts);
2750 qemu_add_opts(&qemu_name_opts);
2751 qemu_add_opts(&qemu_numa_opts);
2752 qemu_add_opts(&qemu_icount_opts);
2753 qemu_add_opts(&qemu_semihosting_config_opts);
2754 qemu_add_opts(&qemu_fw_cfg_opts);
2755 qemu_add_opts(&qemu_action_opts);
2756 module_call_init(MODULE_INIT_OPTS);
2758 error_init(argv[0]);
2759 qemu_init_exec_dir(argv[0]);
2761 #ifdef CONFIG_MODULES
2762 module_init_info(qemu_modinfo);
2763 module_allow_arch(TARGET_NAME);
2764 #endif
2766 qemu_init_subsystems();
2768 /* first pass of option parsing */
2769 optind = 1;
2770 while (optind < argc) {
2771 if (argv[optind][0] != '-') {
2772 /* disk image */
2773 optind++;
2774 } else {
2775 const QEMUOption *popt;
2777 popt = lookup_opt(argc, argv, &optarg, &optind);
2778 switch (popt->index) {
2779 case QEMU_OPTION_nouserconfig:
2780 userconfig = false;
2781 break;
2786 machine_opts_dict = qdict_new();
2787 if (userconfig) {
2788 qemu_read_default_config_file(&error_fatal);
2791 /* second pass of option parsing */
2792 optind = 1;
2793 for(;;) {
2794 if (optind >= argc)
2795 break;
2796 if (argv[optind][0] != '-') {
2797 loc_set_cmdline(argv, optind, 1);
2798 drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2799 } else {
2800 const QEMUOption *popt;
2802 popt = lookup_opt(argc, argv, &optarg, &optind);
2803 if (!(popt->arch_mask & arch_type)) {
2804 error_report("Option not supported for this target");
2805 exit(1);
2807 switch(popt->index) {
2808 case QEMU_OPTION_cpu:
2809 /* hw initialization will check this */
2810 cpu_option = optarg;
2811 break;
2812 case QEMU_OPTION_hda:
2813 case QEMU_OPTION_hdb:
2814 case QEMU_OPTION_hdc:
2815 case QEMU_OPTION_hdd:
2816 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2817 HD_OPTS);
2818 break;
2819 case QEMU_OPTION_blockdev:
2821 Visitor *v;
2822 BlockdevOptionsQueueEntry *bdo;
2824 v = qobject_input_visitor_new_str(optarg, "driver",
2825 &error_fatal);
2827 bdo = g_new(BlockdevOptionsQueueEntry, 1);
2828 visit_type_BlockdevOptions(v, NULL, &bdo->bdo,
2829 &error_fatal);
2830 visit_free(v);
2831 loc_save(&bdo->loc);
2832 QSIMPLEQ_INSERT_TAIL(&bdo_queue, bdo, entry);
2833 break;
2835 case QEMU_OPTION_drive:
2836 if (drive_def(optarg) == NULL) {
2837 exit(1);
2839 break;
2840 case QEMU_OPTION_set:
2841 qemu_set_option(optarg, &error_fatal);
2842 break;
2843 case QEMU_OPTION_global:
2844 if (qemu_global_option(optarg) != 0)
2845 exit(1);
2846 break;
2847 case QEMU_OPTION_mtdblock:
2848 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2849 break;
2850 case QEMU_OPTION_sd:
2851 drive_add(IF_SD, -1, optarg, SD_OPTS);
2852 break;
2853 case QEMU_OPTION_pflash:
2854 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2855 break;
2856 case QEMU_OPTION_snapshot:
2858 Error *blocker = NULL;
2859 snapshot = 1;
2860 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
2861 "-snapshot");
2862 replay_add_blocker(blocker);
2864 break;
2865 case QEMU_OPTION_numa:
2866 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
2867 optarg, true);
2868 if (!opts) {
2869 exit(1);
2871 break;
2872 case QEMU_OPTION_display:
2873 parse_display(optarg);
2874 break;
2875 case QEMU_OPTION_nographic:
2876 qdict_put_str(machine_opts_dict, "graphics", "off");
2877 nographic = true;
2878 dpy.type = DISPLAY_TYPE_NONE;
2879 break;
2880 case QEMU_OPTION_curses:
2881 #ifdef CONFIG_CURSES
2882 dpy.type = DISPLAY_TYPE_CURSES;
2883 #else
2884 error_report("curses or iconv support is disabled");
2885 exit(1);
2886 #endif
2887 break;
2888 case QEMU_OPTION_portrait:
2889 graphic_rotate = 90;
2890 break;
2891 case QEMU_OPTION_rotate:
2892 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2893 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2894 graphic_rotate != 180 && graphic_rotate != 270) {
2895 error_report("only 90, 180, 270 deg rotation is available");
2896 exit(1);
2898 break;
2899 case QEMU_OPTION_kernel:
2900 qdict_put_str(machine_opts_dict, "kernel", optarg);
2901 break;
2902 case QEMU_OPTION_initrd:
2903 qdict_put_str(machine_opts_dict, "initrd", optarg);
2904 break;
2905 case QEMU_OPTION_append:
2906 qdict_put_str(machine_opts_dict, "append", optarg);
2907 break;
2908 case QEMU_OPTION_dtb:
2909 qdict_put_str(machine_opts_dict, "dtb", optarg);
2910 break;
2911 case QEMU_OPTION_cdrom:
2912 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
2913 break;
2914 case QEMU_OPTION_boot:
2915 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
2916 optarg, true);
2917 if (!opts) {
2918 exit(1);
2920 break;
2921 case QEMU_OPTION_fda:
2922 case QEMU_OPTION_fdb:
2923 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2924 optarg, FD_OPTS);
2925 break;
2926 case QEMU_OPTION_no_fd_bootchk:
2927 fd_bootchk = 0;
2928 break;
2929 case QEMU_OPTION_netdev:
2930 default_net = 0;
2931 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2932 exit(1);
2934 break;
2935 case QEMU_OPTION_nic:
2936 default_net = 0;
2937 if (net_client_parse(qemu_find_opts("nic"), optarg) == -1) {
2938 exit(1);
2940 break;
2941 case QEMU_OPTION_net:
2942 default_net = 0;
2943 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2944 exit(1);
2946 break;
2947 #ifdef CONFIG_LIBISCSI
2948 case QEMU_OPTION_iscsi:
2949 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
2950 optarg, false);
2951 if (!opts) {
2952 exit(1);
2954 break;
2955 #endif
2956 case QEMU_OPTION_audio_help:
2957 audio_legacy_help();
2958 exit (0);
2959 break;
2960 case QEMU_OPTION_audiodev:
2961 audio_parse_option(optarg);
2962 break;
2963 case QEMU_OPTION_soundhw:
2964 select_soundhw (optarg);
2965 break;
2966 case QEMU_OPTION_h:
2967 help(0);
2968 break;
2969 case QEMU_OPTION_version:
2970 version();
2971 exit(0);
2972 break;
2973 case QEMU_OPTION_m:
2974 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
2975 optarg, true);
2976 if (!opts) {
2977 exit(EXIT_FAILURE);
2979 break;
2980 #ifdef CONFIG_TPM
2981 case QEMU_OPTION_tpmdev:
2982 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
2983 exit(1);
2985 break;
2986 #endif
2987 case QEMU_OPTION_mempath:
2988 mem_path = optarg;
2989 break;
2990 case QEMU_OPTION_mem_prealloc:
2991 mem_prealloc = 1;
2992 break;
2993 case QEMU_OPTION_d:
2994 log_mask = optarg;
2995 break;
2996 case QEMU_OPTION_D:
2997 log_file = optarg;
2998 break;
2999 case QEMU_OPTION_DFILTER:
3000 qemu_set_dfilter_ranges(optarg, &error_fatal);
3001 break;
3002 case QEMU_OPTION_seed:
3003 qemu_guest_random_seed_main(optarg, &error_fatal);
3004 break;
3005 case QEMU_OPTION_s:
3006 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3007 break;
3008 case QEMU_OPTION_gdb:
3009 add_device_config(DEV_GDB, optarg);
3010 break;
3011 case QEMU_OPTION_L:
3012 if (is_help_option(optarg)) {
3013 list_data_dirs = true;
3014 } else {
3015 qemu_add_data_dir(g_strdup(optarg));
3017 break;
3018 case QEMU_OPTION_bios:
3019 qdict_put_str(machine_opts_dict, "firmware", optarg);
3020 break;
3021 case QEMU_OPTION_singlestep:
3022 singlestep = 1;
3023 break;
3024 case QEMU_OPTION_S:
3025 autostart = 0;
3026 break;
3027 case QEMU_OPTION_k:
3028 keyboard_layout = optarg;
3029 break;
3030 case QEMU_OPTION_vga:
3031 vga_model = optarg;
3032 default_vga = 0;
3033 break;
3034 case QEMU_OPTION_g:
3036 const char *p;
3037 int w, h, depth;
3038 p = optarg;
3039 w = strtol(p, (char **)&p, 10);
3040 if (w <= 0) {
3041 graphic_error:
3042 error_report("invalid resolution or depth");
3043 exit(1);
3045 if (*p != 'x')
3046 goto graphic_error;
3047 p++;
3048 h = strtol(p, (char **)&p, 10);
3049 if (h <= 0)
3050 goto graphic_error;
3051 if (*p == 'x') {
3052 p++;
3053 depth = strtol(p, (char **)&p, 10);
3054 if (depth != 1 && depth != 2 && depth != 4 &&
3055 depth != 8 && depth != 15 && depth != 16 &&
3056 depth != 24 && depth != 32)
3057 goto graphic_error;
3058 } else if (*p == '\0') {
3059 depth = graphic_depth;
3060 } else {
3061 goto graphic_error;
3064 graphic_width = w;
3065 graphic_height = h;
3066 graphic_depth = depth;
3068 break;
3069 case QEMU_OPTION_echr:
3071 char *r;
3072 term_escape_char = strtol(optarg, &r, 0);
3073 if (r == optarg)
3074 printf("Bad argument to echr\n");
3075 break;
3077 case QEMU_OPTION_monitor:
3078 default_monitor = 0;
3079 if (strncmp(optarg, "none", 4)) {
3080 monitor_parse(optarg, "readline", false);
3082 break;
3083 case QEMU_OPTION_qmp:
3084 monitor_parse(optarg, "control", false);
3085 default_monitor = 0;
3086 break;
3087 case QEMU_OPTION_qmp_pretty:
3088 monitor_parse(optarg, "control", true);
3089 default_monitor = 0;
3090 break;
3091 case QEMU_OPTION_mon:
3092 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3093 true);
3094 if (!opts) {
3095 exit(1);
3097 default_monitor = 0;
3098 break;
3099 case QEMU_OPTION_chardev:
3100 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3101 optarg, true);
3102 if (!opts) {
3103 exit(1);
3105 break;
3106 case QEMU_OPTION_fsdev:
3107 olist = qemu_find_opts("fsdev");
3108 if (!olist) {
3109 error_report("fsdev support is disabled");
3110 exit(1);
3112 opts = qemu_opts_parse_noisily(olist, optarg, true);
3113 if (!opts) {
3114 exit(1);
3116 break;
3117 case QEMU_OPTION_virtfs: {
3118 QemuOpts *fsdev;
3119 QemuOpts *device;
3120 const char *writeout, *sock_fd, *socket, *path, *security_model,
3121 *multidevs;
3123 olist = qemu_find_opts("virtfs");
3124 if (!olist) {
3125 error_report("virtfs support is disabled");
3126 exit(1);
3128 opts = qemu_opts_parse_noisily(olist, optarg, true);
3129 if (!opts) {
3130 exit(1);
3133 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3134 qemu_opt_get(opts, "mount_tag") == NULL) {
3135 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3136 exit(1);
3138 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3139 qemu_opts_id(opts) ?:
3140 qemu_opt_get(opts, "mount_tag"),
3141 1, NULL);
3142 if (!fsdev) {
3143 error_report("duplicate or invalid fsdev id: %s",
3144 qemu_opt_get(opts, "mount_tag"));
3145 exit(1);
3148 writeout = qemu_opt_get(opts, "writeout");
3149 if (writeout) {
3150 #ifdef CONFIG_SYNC_FILE_RANGE
3151 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3152 #else
3153 error_report("writeout=immediate not supported "
3154 "on this platform");
3155 exit(1);
3156 #endif
3158 qemu_opt_set(fsdev, "fsdriver",
3159 qemu_opt_get(opts, "fsdriver"), &error_abort);
3160 path = qemu_opt_get(opts, "path");
3161 if (path) {
3162 qemu_opt_set(fsdev, "path", path, &error_abort);
3164 security_model = qemu_opt_get(opts, "security_model");
3165 if (security_model) {
3166 qemu_opt_set(fsdev, "security_model", security_model,
3167 &error_abort);
3169 socket = qemu_opt_get(opts, "socket");
3170 if (socket) {
3171 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3173 sock_fd = qemu_opt_get(opts, "sock_fd");
3174 if (sock_fd) {
3175 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3178 qemu_opt_set_bool(fsdev, "readonly",
3179 qemu_opt_get_bool(opts, "readonly", 0),
3180 &error_abort);
3181 multidevs = qemu_opt_get(opts, "multidevs");
3182 if (multidevs) {
3183 qemu_opt_set(fsdev, "multidevs", multidevs, &error_abort);
3185 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3186 &error_abort);
3187 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3188 qemu_opt_set(device, "fsdev",
3189 qemu_opts_id(fsdev), &error_abort);
3190 qemu_opt_set(device, "mount_tag",
3191 qemu_opt_get(opts, "mount_tag"), &error_abort);
3192 break;
3194 case QEMU_OPTION_serial:
3195 add_device_config(DEV_SERIAL, optarg);
3196 default_serial = 0;
3197 if (strncmp(optarg, "mon:", 4) == 0) {
3198 default_monitor = 0;
3200 break;
3201 case QEMU_OPTION_watchdog:
3202 if (watchdog) {
3203 error_report("only one watchdog option may be given");
3204 exit(1);
3206 watchdog = optarg;
3207 break;
3208 case QEMU_OPTION_action:
3209 olist = qemu_find_opts("action");
3210 if (!qemu_opts_parse_noisily(olist, optarg, false)) {
3211 exit(1);
3213 break;
3214 case QEMU_OPTION_watchdog_action:
3215 if (select_watchdog_action(optarg) == -1) {
3216 error_report("unknown -watchdog-action parameter");
3217 exit(1);
3219 break;
3220 case QEMU_OPTION_parallel:
3221 add_device_config(DEV_PARALLEL, optarg);
3222 default_parallel = 0;
3223 if (strncmp(optarg, "mon:", 4) == 0) {
3224 default_monitor = 0;
3226 break;
3227 case QEMU_OPTION_debugcon:
3228 add_device_config(DEV_DEBUGCON, optarg);
3229 break;
3230 case QEMU_OPTION_loadvm:
3231 loadvm = optarg;
3232 break;
3233 case QEMU_OPTION_full_screen:
3234 dpy.has_full_screen = true;
3235 dpy.full_screen = true;
3236 break;
3237 case QEMU_OPTION_alt_grab:
3238 alt_grab = 1;
3239 break;
3240 case QEMU_OPTION_ctrl_grab:
3241 ctrl_grab = 1;
3242 break;
3243 case QEMU_OPTION_no_quit:
3244 dpy.has_window_close = true;
3245 dpy.window_close = false;
3246 warn_report("-no-quit is deprecated, please use "
3247 "-display ...,window-close=off instead.");
3248 break;
3249 case QEMU_OPTION_sdl:
3250 #ifdef CONFIG_SDL
3251 dpy.type = DISPLAY_TYPE_SDL;
3252 break;
3253 #else
3254 error_report("SDL support is disabled");
3255 exit(1);
3256 #endif
3257 case QEMU_OPTION_pidfile:
3258 pid_file = optarg;
3259 break;
3260 case QEMU_OPTION_win2k_hack:
3261 win2k_install_hack = 1;
3262 break;
3263 case QEMU_OPTION_acpitable:
3264 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3265 optarg, true);
3266 if (!opts) {
3267 exit(1);
3269 acpi_table_add(opts, &error_fatal);
3270 break;
3271 case QEMU_OPTION_smbios:
3272 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3273 optarg, false);
3274 if (!opts) {
3275 exit(1);
3277 smbios_entry_add(opts, &error_fatal);
3278 break;
3279 case QEMU_OPTION_fwcfg:
3280 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3281 optarg, true);
3282 if (opts == NULL) {
3283 exit(1);
3285 break;
3286 case QEMU_OPTION_preconfig:
3287 preconfig_requested = true;
3288 break;
3289 case QEMU_OPTION_enable_kvm:
3290 qdict_put_str(machine_opts_dict, "accel", "kvm");
3291 break;
3292 case QEMU_OPTION_M:
3293 case QEMU_OPTION_machine:
3295 bool help;
3297 keyval_parse_into(machine_opts_dict, optarg, "type", &help, &error_fatal);
3298 if (help) {
3299 machine_help_func(machine_opts_dict);
3300 exit(EXIT_SUCCESS);
3302 break;
3304 case QEMU_OPTION_accel:
3305 accel_opts = qemu_opts_parse_noisily(qemu_find_opts("accel"),
3306 optarg, true);
3307 optarg = qemu_opt_get(accel_opts, "accel");
3308 if (!optarg || is_help_option(optarg)) {
3309 printf("Accelerators supported in QEMU binary:\n");
3310 GSList *el, *accel_list = object_class_get_list(TYPE_ACCEL,
3311 false);
3312 for (el = accel_list; el; el = el->next) {
3313 gchar *typename = g_strdup(object_class_get_name(
3314 OBJECT_CLASS(el->data)));
3315 /* omit qtest which is used for tests only */
3316 if (g_strcmp0(typename, ACCEL_CLASS_NAME("qtest")) &&
3317 g_str_has_suffix(typename, ACCEL_CLASS_SUFFIX)) {
3318 gchar **optname = g_strsplit(typename,
3319 ACCEL_CLASS_SUFFIX, 0);
3320 printf("%s\n", optname[0]);
3321 g_strfreev(optname);
3323 g_free(typename);
3325 g_slist_free(accel_list);
3326 exit(0);
3328 break;
3329 case QEMU_OPTION_usb:
3330 qdict_put_str(machine_opts_dict, "usb", "on");
3331 break;
3332 case QEMU_OPTION_usbdevice:
3333 qdict_put_str(machine_opts_dict, "usb", "on");
3334 add_device_config(DEV_USB, optarg);
3335 break;
3336 case QEMU_OPTION_device:
3337 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3338 optarg, true)) {
3339 exit(1);
3341 break;
3342 case QEMU_OPTION_smp:
3343 machine_parse_property_opt(qemu_find_opts("smp-opts"),
3344 "smp", optarg);
3345 break;
3346 case QEMU_OPTION_vnc:
3347 vnc_parse(optarg);
3348 break;
3349 case QEMU_OPTION_no_acpi:
3350 qdict_put_str(machine_opts_dict, "acpi", "off");
3351 break;
3352 case QEMU_OPTION_no_hpet:
3353 qdict_put_str(machine_opts_dict, "hpet", "off");
3354 break;
3355 case QEMU_OPTION_no_reboot:
3356 olist = qemu_find_opts("action");
3357 qemu_opts_parse_noisily(olist, "reboot=shutdown", false);
3358 break;
3359 case QEMU_OPTION_no_shutdown:
3360 olist = qemu_find_opts("action");
3361 qemu_opts_parse_noisily(olist, "shutdown=pause", false);
3362 break;
3363 case QEMU_OPTION_uuid:
3364 if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
3365 error_report("failed to parse UUID string: wrong format");
3366 exit(1);
3368 qemu_uuid_set = true;
3369 break;
3370 case QEMU_OPTION_option_rom:
3371 if (nb_option_roms >= MAX_OPTION_ROMS) {
3372 error_report("too many option ROMs");
3373 exit(1);
3375 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3376 optarg, true);
3377 if (!opts) {
3378 exit(1);
3380 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3381 option_rom[nb_option_roms].bootindex =
3382 qemu_opt_get_number(opts, "bootindex", -1);
3383 if (!option_rom[nb_option_roms].name) {
3384 error_report("Option ROM file is not specified");
3385 exit(1);
3387 nb_option_roms++;
3388 break;
3389 case QEMU_OPTION_semihosting:
3390 qemu_semihosting_enable();
3391 break;
3392 case QEMU_OPTION_semihosting_config:
3393 if (qemu_semihosting_config_options(optarg) != 0) {
3394 exit(1);
3396 break;
3397 case QEMU_OPTION_name:
3398 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3399 optarg, true);
3400 if (!opts) {
3401 exit(1);
3403 /* Capture guest name if -msg guest-name is used later */
3404 error_guest_name = qemu_opt_get(opts, "guest");
3405 break;
3406 case QEMU_OPTION_prom_env:
3407 if (nb_prom_envs >= MAX_PROM_ENVS) {
3408 error_report("too many prom variables");
3409 exit(1);
3411 prom_envs[nb_prom_envs] = optarg;
3412 nb_prom_envs++;
3413 break;
3414 case QEMU_OPTION_old_param:
3415 old_param = 1;
3416 break;
3417 case QEMU_OPTION_rtc:
3418 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3419 false);
3420 if (!opts) {
3421 exit(1);
3423 break;
3424 case QEMU_OPTION_icount:
3425 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3426 optarg, true);
3427 if (!icount_opts) {
3428 exit(1);
3430 break;
3431 case QEMU_OPTION_incoming:
3432 if (!incoming) {
3433 runstate_set(RUN_STATE_INMIGRATE);
3435 incoming = optarg;
3436 break;
3437 case QEMU_OPTION_only_migratable:
3438 only_migratable = 1;
3439 break;
3440 case QEMU_OPTION_nodefaults:
3441 has_defaults = 0;
3442 break;
3443 case QEMU_OPTION_xen_domid:
3444 if (!(accel_find("xen"))) {
3445 error_report("Option not supported for this target");
3446 exit(1);
3448 xen_domid = atoi(optarg);
3449 break;
3450 case QEMU_OPTION_xen_attach:
3451 if (!(accel_find("xen"))) {
3452 error_report("Option not supported for this target");
3453 exit(1);
3455 xen_mode = XEN_ATTACH;
3456 break;
3457 case QEMU_OPTION_xen_domid_restrict:
3458 if (!(accel_find("xen"))) {
3459 error_report("Option not supported for this target");
3460 exit(1);
3462 xen_domid_restrict = true;
3463 break;
3464 case QEMU_OPTION_trace:
3465 trace_opt_parse(optarg);
3466 break;
3467 case QEMU_OPTION_plugin:
3468 qemu_plugin_opt_parse(optarg, &plugin_list);
3469 break;
3470 case QEMU_OPTION_readconfig:
3471 qemu_read_config_file(optarg, qemu_parse_config_group, &error_fatal);
3472 break;
3473 case QEMU_OPTION_spice:
3474 olist = qemu_find_opts_err("spice", NULL);
3475 if (!olist) {
3476 error_report("spice support is disabled");
3477 exit(1);
3479 opts = qemu_opts_parse_noisily(olist, optarg, false);
3480 if (!opts) {
3481 exit(1);
3483 display_remote++;
3484 break;
3485 case QEMU_OPTION_writeconfig:
3487 FILE *fp;
3488 warn_report("-writeconfig is deprecated and will go away without a replacement");
3489 if (strcmp(optarg, "-") == 0) {
3490 fp = stdout;
3491 } else {
3492 fp = fopen(optarg, "w");
3493 if (fp == NULL) {
3494 error_report("open %s: %s", optarg,
3495 strerror(errno));
3496 exit(1);
3499 qemu_config_write(fp);
3500 if (fp != stdout) {
3501 fclose(fp);
3503 break;
3505 case QEMU_OPTION_qtest:
3506 qtest_chrdev = optarg;
3507 break;
3508 case QEMU_OPTION_qtest_log:
3509 qtest_log = optarg;
3510 break;
3511 case QEMU_OPTION_sandbox:
3512 olist = qemu_find_opts("sandbox");
3513 if (!olist) {
3514 #ifndef CONFIG_SECCOMP
3515 error_report("-sandbox support is not enabled "
3516 "in this QEMU binary");
3517 #endif
3518 exit(1);
3521 opts = qemu_opts_parse_noisily(olist, optarg, true);
3522 if (!opts) {
3523 exit(1);
3525 break;
3526 case QEMU_OPTION_add_fd:
3527 #ifndef _WIN32
3528 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
3529 optarg, false);
3530 if (!opts) {
3531 exit(1);
3533 #else
3534 error_report("File descriptor passing is disabled on this "
3535 "platform");
3536 exit(1);
3537 #endif
3538 break;
3539 case QEMU_OPTION_object:
3540 object_option_parse(optarg);
3541 break;
3542 case QEMU_OPTION_overcommit:
3543 opts = qemu_opts_parse_noisily(qemu_find_opts("overcommit"),
3544 optarg, false);
3545 if (!opts) {
3546 exit(1);
3548 enable_mlock = qemu_opt_get_bool(opts, "mem-lock", false);
3549 enable_cpu_pm = qemu_opt_get_bool(opts, "cpu-pm", false);
3550 break;
3551 case QEMU_OPTION_compat:
3553 CompatPolicy *opts;
3554 Visitor *v;
3556 v = qobject_input_visitor_new_str(optarg, NULL,
3557 &error_fatal);
3559 visit_type_CompatPolicy(v, NULL, &opts, &error_fatal);
3560 QAPI_CLONE_MEMBERS(CompatPolicy, &compat_policy, opts);
3562 qapi_free_CompatPolicy(opts);
3563 visit_free(v);
3564 break;
3566 case QEMU_OPTION_msg:
3567 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
3568 false);
3569 if (!opts) {
3570 exit(1);
3572 configure_msg(opts);
3573 break;
3574 case QEMU_OPTION_dump_vmstate:
3575 if (vmstate_dump_file) {
3576 error_report("only one '-dump-vmstate' "
3577 "option may be given");
3578 exit(1);
3580 vmstate_dump_file = fopen(optarg, "w");
3581 if (vmstate_dump_file == NULL) {
3582 error_report("open %s: %s", optarg, strerror(errno));
3583 exit(1);
3585 break;
3586 case QEMU_OPTION_enable_sync_profile:
3587 qsp_enable();
3588 break;
3589 case QEMU_OPTION_nouserconfig:
3590 /* Nothing to be parsed here. Especially, do not error out below. */
3591 break;
3592 default:
3593 if (os_parse_cmd_args(popt->index, optarg)) {
3594 error_report("Option not supported in this build");
3595 exit(1);
3601 * Clear error location left behind by the loop.
3602 * Best done right after the loop. Do not insert code here!
3604 loc_set_none();
3606 qemu_validate_options(machine_opts_dict);
3607 qemu_process_sugar_options();
3610 * These options affect everything else and should be processed
3611 * before daemonizing.
3613 qemu_process_early_options();
3615 qemu_process_help_options();
3616 qemu_maybe_daemonize(pid_file);
3619 * The trace backend must be initialized after daemonizing.
3620 * trace_init_backends() will call st_init(), which will create the
3621 * trace thread in the parent, and also register st_flush_trace_buffer()
3622 * in atexit(). This function will force the parent to wait for the
3623 * writeout thread to finish, which will not occur, and the parent
3624 * process will be left in the host.
3626 if (!trace_init_backends()) {
3627 exit(1);
3629 trace_init_file();
3631 qemu_init_main_loop(&error_fatal);
3632 cpu_timers_init();
3634 user_register_global_props();
3635 replay_configure(icount_opts);
3637 configure_rtc(qemu_find_opts_singleton("rtc"));
3639 qemu_create_machine(machine_opts_dict);
3641 suspend_mux_open();
3643 qemu_disable_default_devices();
3644 qemu_create_default_devices();
3645 qemu_create_early_backends();
3647 qemu_apply_legacy_machine_options(machine_opts_dict);
3648 qemu_apply_machine_options(machine_opts_dict);
3649 qobject_unref(machine_opts_dict);
3650 phase_advance(PHASE_MACHINE_CREATED);
3653 * Note: uses machine properties such as kernel-irqchip, must run
3654 * after qemu_apply_machine_options.
3656 configure_accelerators(argv[0]);
3657 phase_advance(PHASE_ACCEL_CREATED);
3660 * Beware, QOM objects created before this point miss global and
3661 * compat properties.
3663 * Global properties get set up by qdev_prop_register_global(),
3664 * called from user_register_global_props(), and certain option
3665 * desugaring. Also in CPU feature desugaring (buried in
3666 * parse_cpu_option()), which happens below this point, but may
3667 * only target the CPU type, which can only be created after
3668 * parse_cpu_option() returned the type.
3670 * Machine compat properties: object_set_machine_compat_props().
3671 * Accelerator compat props: object_set_accelerator_compat_props(),
3672 * called from do_configure_accelerator().
3675 machine_class = MACHINE_GET_CLASS(current_machine);
3676 if (!qtest_enabled() && machine_class->deprecation_reason) {
3677 error_report("Machine type '%s' is deprecated: %s",
3678 machine_class->name, machine_class->deprecation_reason);
3682 * Note: creates a QOM object, must run only after global and
3683 * compat properties have been set up.
3685 migration_object_init();
3687 qemu_create_late_backends();
3689 /* parse features once if machine provides default cpu_type */
3690 current_machine->cpu_type = machine_class->default_cpu_type;
3691 if (cpu_option) {
3692 current_machine->cpu_type = parse_cpu_option(cpu_option);
3694 /* NB: for machine none cpu_type could STILL be NULL here! */
3696 qemu_resolve_machine_memdev();
3697 parse_numa_opts(current_machine);
3699 if (vmstate_dump_file) {
3700 /* dump and exit */
3701 dump_vmstate_json_to_file(vmstate_dump_file);
3702 exit(0);
3705 if (!preconfig_requested) {
3706 qmp_x_exit_preconfig(&error_fatal);
3708 qemu_init_displays();
3709 accel_setup_post(current_machine);
3710 os_setup_post();
3711 resume_mux_open();