softmmu: Add qemu_init_arch_modules()
[qemu/rayw.git] / softmmu / vl.c
blob0b81f61535489f4f49b4872c5e3b5a2f2d5671fd
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/hw-version.h"
40 #include "qemu/uuid.h"
41 #include "sysemu/reset.h"
42 #include "sysemu/runstate.h"
43 #include "sysemu/runstate-action.h"
44 #include "sysemu/seccomp.h"
45 #include "sysemu/tcg.h"
46 #include "sysemu/xen.h"
48 #include "qemu/error-report.h"
49 #include "qemu/sockets.h"
50 #include "qemu/accel.h"
51 #include "hw/usb.h"
52 #include "hw/isa/isa.h"
53 #include "hw/scsi/scsi.h"
54 #include "hw/display/vga.h"
55 #include "sysemu/watchdog.h"
56 #include "hw/firmware/smbios.h"
57 #include "hw/acpi/acpi.h"
58 #include "hw/xen/xen.h"
59 #include "hw/loader.h"
60 #include "monitor/qdev.h"
61 #include "net/net.h"
62 #include "net/slirp.h"
63 #include "monitor/monitor.h"
64 #include "ui/console.h"
65 #include "ui/input.h"
66 #include "sysemu/sysemu.h"
67 #include "sysemu/numa.h"
68 #include "sysemu/hostmem.h"
69 #include "exec/gdbstub.h"
70 #include "qemu/timer.h"
71 #include "chardev/char.h"
72 #include "qemu/bitmap.h"
73 #include "qemu/log.h"
74 #include "sysemu/blockdev.h"
75 #include "hw/block/block.h"
76 #include "hw/i386/x86.h"
77 #include "hw/i386/pc.h"
78 #include "migration/misc.h"
79 #include "migration/snapshot.h"
80 #include "sysemu/tpm.h"
81 #include "sysemu/dma.h"
82 #include "hw/audio/soundhw.h"
83 #include "audio/audio.h"
84 #include "sysemu/cpus.h"
85 #include "sysemu/cpu-timers.h"
86 #include "migration/colo.h"
87 #include "migration/postcopy-ram.h"
88 #include "sysemu/kvm.h"
89 #include "sysemu/hax.h"
90 #include "qapi/qobject-input-visitor.h"
91 #include "qemu/option.h"
92 #include "qemu/config-file.h"
93 #include "qemu/qemu-options.h"
94 #include "qemu/main-loop.h"
95 #ifdef CONFIG_VIRTFS
96 #include "fsdev/qemu-fsdev.h"
97 #endif
98 #include "sysemu/qtest.h"
100 #include "disas/disas.h"
102 #include "trace.h"
103 #include "trace/control.h"
104 #include "qemu/plugin.h"
105 #include "qemu/queue.h"
106 #include "sysemu/arch_init.h"
107 #include "exec/confidential-guest-support.h"
109 #include "ui/qemu-spice.h"
110 #include "qapi/string-input-visitor.h"
111 #include "qapi/opts-visitor.h"
112 #include "qapi/clone-visitor.h"
113 #include "qom/object_interfaces.h"
114 #include "semihosting/semihost.h"
115 #include "crypto/init.h"
116 #include "sysemu/replay.h"
117 #include "qapi/qapi-events-run-state.h"
118 #include "qapi/qapi-visit-block-core.h"
119 #include "qapi/qapi-visit-compat.h"
120 #include "qapi/qapi-visit-ui.h"
121 #include "qapi/qapi-commands-block-core.h"
122 #include "qapi/qapi-commands-migration.h"
123 #include "qapi/qapi-commands-misc.h"
124 #include "qapi/qapi-visit-qom.h"
125 #include "qapi/qapi-commands-ui.h"
126 #include "qapi/qmp/qdict.h"
127 #include "qapi/qmp/qerror.h"
128 #include "sysemu/iothread.h"
129 #include "qemu/guest-random.h"
131 #include "config-host.h"
133 #define MAX_VIRTIO_CONSOLES 1
135 typedef struct BlockdevOptionsQueueEntry {
136 BlockdevOptions *bdo;
137 Location loc;
138 QSIMPLEQ_ENTRY(BlockdevOptionsQueueEntry) entry;
139 } BlockdevOptionsQueueEntry;
141 typedef QSIMPLEQ_HEAD(, BlockdevOptionsQueueEntry) BlockdevOptionsQueue;
143 typedef struct ObjectOption {
144 ObjectOptions *opts;
145 QTAILQ_ENTRY(ObjectOption) next;
146 } ObjectOption;
148 typedef struct DeviceOption {
149 QDict *opts;
150 Location loc;
151 QTAILQ_ENTRY(DeviceOption) next;
152 } DeviceOption;
154 static const char *cpu_option;
155 static const char *mem_path;
156 static const char *incoming;
157 static const char *loadvm;
158 static const char *accelerators;
159 static QDict *machine_opts_dict;
160 static QTAILQ_HEAD(, ObjectOption) object_opts = QTAILQ_HEAD_INITIALIZER(object_opts);
161 static QTAILQ_HEAD(, DeviceOption) device_opts = QTAILQ_HEAD_INITIALIZER(device_opts);
162 static ram_addr_t maxram_size;
163 static uint64_t ram_slots;
164 static int display_remote;
165 static int snapshot;
166 static bool preconfig_requested;
167 static QemuPluginList plugin_list = QTAILQ_HEAD_INITIALIZER(plugin_list);
168 static BlockdevOptionsQueue bdo_queue = QSIMPLEQ_HEAD_INITIALIZER(bdo_queue);
169 static bool nographic = false;
170 static int mem_prealloc; /* force preallocation of physical target memory */
171 static ram_addr_t ram_size;
172 static const char *vga_model = NULL;
173 static DisplayOptions dpy;
174 static int num_serial_hds;
175 static Chardev **serial_hds;
176 static const char *log_mask;
177 static const char *log_file;
178 static bool list_data_dirs;
179 static const char *watchdog;
180 static const char *qtest_chrdev;
181 static const char *qtest_log;
183 static int has_defaults = 1;
184 static int default_serial = 1;
185 static int default_parallel = 1;
186 static int default_monitor = 1;
187 static int default_floppy = 1;
188 static int default_cdrom = 1;
189 static int default_sdcard = 1;
190 static int default_vga = 1;
191 static int default_net = 1;
193 static struct {
194 const char *driver;
195 int *flag;
196 } default_list[] = {
197 { .driver = "isa-serial", .flag = &default_serial },
198 { .driver = "isa-parallel", .flag = &default_parallel },
199 { .driver = "isa-fdc", .flag = &default_floppy },
200 { .driver = "floppy", .flag = &default_floppy },
201 { .driver = "ide-cd", .flag = &default_cdrom },
202 { .driver = "ide-hd", .flag = &default_cdrom },
203 { .driver = "scsi-cd", .flag = &default_cdrom },
204 { .driver = "scsi-hd", .flag = &default_cdrom },
205 { .driver = "VGA", .flag = &default_vga },
206 { .driver = "isa-vga", .flag = &default_vga },
207 { .driver = "cirrus-vga", .flag = &default_vga },
208 { .driver = "isa-cirrus-vga", .flag = &default_vga },
209 { .driver = "vmware-svga", .flag = &default_vga },
210 { .driver = "qxl-vga", .flag = &default_vga },
211 { .driver = "virtio-vga", .flag = &default_vga },
212 { .driver = "ati-vga", .flag = &default_vga },
213 { .driver = "vhost-user-vga", .flag = &default_vga },
214 { .driver = "virtio-vga-gl", .flag = &default_vga },
217 static QemuOptsList qemu_rtc_opts = {
218 .name = "rtc",
219 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
220 .merge_lists = true,
221 .desc = {
223 .name = "base",
224 .type = QEMU_OPT_STRING,
226 .name = "clock",
227 .type = QEMU_OPT_STRING,
229 .name = "driftfix",
230 .type = QEMU_OPT_STRING,
232 { /* end of list */ }
236 static QemuOptsList qemu_option_rom_opts = {
237 .name = "option-rom",
238 .implied_opt_name = "romfile",
239 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
240 .desc = {
242 .name = "bootindex",
243 .type = QEMU_OPT_NUMBER,
244 }, {
245 .name = "romfile",
246 .type = QEMU_OPT_STRING,
248 { /* end of list */ }
252 static QemuOptsList qemu_accel_opts = {
253 .name = "accel",
254 .implied_opt_name = "accel",
255 .head = QTAILQ_HEAD_INITIALIZER(qemu_accel_opts.head),
256 .desc = {
258 * no elements => accept any
259 * sanity checking will happen later
260 * when setting accelerator properties
266 static QemuOptsList qemu_boot_opts = {
267 .name = "boot-opts",
268 .implied_opt_name = "order",
269 .merge_lists = true,
270 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
271 .desc = {
273 .name = "order",
274 .type = QEMU_OPT_STRING,
275 }, {
276 .name = "once",
277 .type = QEMU_OPT_STRING,
278 }, {
279 .name = "menu",
280 .type = QEMU_OPT_BOOL,
281 }, {
282 .name = "splash",
283 .type = QEMU_OPT_STRING,
284 }, {
285 .name = "splash-time",
286 .type = QEMU_OPT_NUMBER,
287 }, {
288 .name = "reboot-timeout",
289 .type = QEMU_OPT_NUMBER,
290 }, {
291 .name = "strict",
292 .type = QEMU_OPT_BOOL,
294 { /*End of list */ }
298 static QemuOptsList qemu_add_fd_opts = {
299 .name = "add-fd",
300 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
301 .desc = {
303 .name = "fd",
304 .type = QEMU_OPT_NUMBER,
305 .help = "file descriptor of which a duplicate is added to fd set",
307 .name = "set",
308 .type = QEMU_OPT_NUMBER,
309 .help = "ID of the fd set to add fd to",
311 .name = "opaque",
312 .type = QEMU_OPT_STRING,
313 .help = "free-form string used to describe fd",
315 { /* end of list */ }
319 static QemuOptsList qemu_object_opts = {
320 .name = "object",
321 .implied_opt_name = "qom-type",
322 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
323 .desc = {
328 static QemuOptsList qemu_tpmdev_opts = {
329 .name = "tpmdev",
330 .implied_opt_name = "type",
331 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
332 .desc = {
333 /* options are defined in the TPM backends */
334 { /* end of list */ }
338 static QemuOptsList qemu_overcommit_opts = {
339 .name = "overcommit",
340 .head = QTAILQ_HEAD_INITIALIZER(qemu_overcommit_opts.head),
341 .desc = {
343 .name = "mem-lock",
344 .type = QEMU_OPT_BOOL,
347 .name = "cpu-pm",
348 .type = QEMU_OPT_BOOL,
350 { /* end of list */ }
354 static QemuOptsList qemu_msg_opts = {
355 .name = "msg",
356 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
357 .desc = {
359 .name = "timestamp",
360 .type = QEMU_OPT_BOOL,
363 .name = "guest-name",
364 .type = QEMU_OPT_BOOL,
365 .help = "Prepends guest name for error messages but only if "
366 "-name guest is set otherwise option is ignored\n",
368 { /* end of list */ }
372 static QemuOptsList qemu_name_opts = {
373 .name = "name",
374 .implied_opt_name = "guest",
375 .merge_lists = true,
376 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
377 .desc = {
379 .name = "guest",
380 .type = QEMU_OPT_STRING,
381 .help = "Sets the name of the guest.\n"
382 "This name will be displayed in the SDL window caption.\n"
383 "The name will also be used for the VNC server",
384 }, {
385 .name = "process",
386 .type = QEMU_OPT_STRING,
387 .help = "Sets the name of the QEMU process, as shown in top etc",
388 }, {
389 .name = "debug-threads",
390 .type = QEMU_OPT_BOOL,
391 .help = "When enabled, name the individual threads; defaults off.\n"
392 "NOTE: The thread names are for debugging and not a\n"
393 "stable API.",
395 { /* End of list */ }
399 static QemuOptsList qemu_mem_opts = {
400 .name = "memory",
401 .implied_opt_name = "size",
402 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
403 .merge_lists = true,
404 .desc = {
406 .name = "size",
407 .type = QEMU_OPT_SIZE,
410 .name = "slots",
411 .type = QEMU_OPT_NUMBER,
414 .name = "maxmem",
415 .type = QEMU_OPT_SIZE,
417 { /* end of list */ }
421 static QemuOptsList qemu_icount_opts = {
422 .name = "icount",
423 .implied_opt_name = "shift",
424 .merge_lists = true,
425 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
426 .desc = {
428 .name = "shift",
429 .type = QEMU_OPT_STRING,
430 }, {
431 .name = "align",
432 .type = QEMU_OPT_BOOL,
433 }, {
434 .name = "sleep",
435 .type = QEMU_OPT_BOOL,
436 }, {
437 .name = "rr",
438 .type = QEMU_OPT_STRING,
439 }, {
440 .name = "rrfile",
441 .type = QEMU_OPT_STRING,
442 }, {
443 .name = "rrsnapshot",
444 .type = QEMU_OPT_STRING,
446 { /* end of list */ }
450 static QemuOptsList qemu_fw_cfg_opts = {
451 .name = "fw_cfg",
452 .implied_opt_name = "name",
453 .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
454 .desc = {
456 .name = "name",
457 .type = QEMU_OPT_STRING,
458 .help = "Sets the fw_cfg name of the blob to be inserted",
459 }, {
460 .name = "file",
461 .type = QEMU_OPT_STRING,
462 .help = "Sets the name of the file from which "
463 "the fw_cfg blob will be loaded",
464 }, {
465 .name = "string",
466 .type = QEMU_OPT_STRING,
467 .help = "Sets content of the blob to be inserted from a string",
468 }, {
469 .name = "gen_id",
470 .type = QEMU_OPT_STRING,
471 .help = "Sets id of the object generating the fw_cfg blob "
472 "to be inserted",
474 { /* end of list */ }
478 static QemuOptsList qemu_action_opts = {
479 .name = "action",
480 .merge_lists = true,
481 .head = QTAILQ_HEAD_INITIALIZER(qemu_action_opts.head),
482 .desc = {
484 .name = "shutdown",
485 .type = QEMU_OPT_STRING,
487 .name = "reboot",
488 .type = QEMU_OPT_STRING,
490 .name = "panic",
491 .type = QEMU_OPT_STRING,
493 .name = "watchdog",
494 .type = QEMU_OPT_STRING,
496 { /* end of list */ }
500 const char *qemu_get_vm_name(void)
502 return qemu_name;
505 static void default_driver_disable(const char *driver)
507 int i;
509 if (!driver) {
510 return;
513 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
514 if (strcmp(default_list[i].driver, driver) != 0)
515 continue;
516 *(default_list[i].flag) = 0;
520 static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
522 const char *driver = qemu_opt_get(opts, "driver");
524 default_driver_disable(driver);
525 return 0;
528 static void default_driver_check_json(void)
530 DeviceOption *opt;
532 QTAILQ_FOREACH(opt, &device_opts, next) {
533 const char *driver = qdict_get_try_str(opt->opts, "driver");
534 default_driver_disable(driver);
538 static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
540 const char *proc_name;
542 if (qemu_opt_get(opts, "debug-threads")) {
543 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
545 qemu_name = qemu_opt_get(opts, "guest");
547 proc_name = qemu_opt_get(opts, "process");
548 if (proc_name) {
549 os_set_proc_name(proc_name);
552 return 0;
555 bool defaults_enabled(void)
557 return has_defaults;
560 #ifndef _WIN32
561 static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
563 int fd, dupfd, flags;
564 int64_t fdset_id;
565 const char *fd_opaque = NULL;
566 AddfdInfo *fdinfo;
568 fd = qemu_opt_get_number(opts, "fd", -1);
569 fdset_id = qemu_opt_get_number(opts, "set", -1);
570 fd_opaque = qemu_opt_get(opts, "opaque");
572 if (fd < 0) {
573 error_setg(errp, "fd option is required and must be non-negative");
574 return -1;
577 if (fd <= STDERR_FILENO) {
578 error_setg(errp, "fd cannot be a standard I/O stream");
579 return -1;
583 * All fds inherited across exec() necessarily have FD_CLOEXEC
584 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
586 flags = fcntl(fd, F_GETFD);
587 if (flags == -1 || (flags & FD_CLOEXEC)) {
588 error_setg(errp, "fd is not valid or already in use");
589 return -1;
592 if (fdset_id < 0) {
593 error_setg(errp, "set option is required and must be non-negative");
594 return -1;
597 #ifdef F_DUPFD_CLOEXEC
598 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
599 #else
600 dupfd = dup(fd);
601 if (dupfd != -1) {
602 qemu_set_cloexec(dupfd);
604 #endif
605 if (dupfd == -1) {
606 error_setg(errp, "error duplicating fd: %s", strerror(errno));
607 return -1;
610 /* add the duplicate fd, and optionally the opaque string, to the fd set */
611 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
612 &error_abort);
613 g_free(fdinfo);
615 return 0;
618 static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
620 int fd;
622 fd = qemu_opt_get_number(opts, "fd", -1);
623 close(fd);
625 return 0;
627 #endif
629 /***********************************************************/
630 /* QEMU Block devices */
632 #define HD_OPTS "media=disk"
633 #define CDROM_OPTS "media=cdrom"
634 #define FD_OPTS ""
635 #define PFLASH_OPTS ""
636 #define MTD_OPTS ""
637 #define SD_OPTS ""
639 static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
641 BlockInterfaceType *block_default_type = opaque;
643 return drive_new(opts, *block_default_type, errp) == NULL;
646 static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
648 if (qemu_opt_get(opts, "snapshot") == NULL) {
649 qemu_opt_set(opts, "snapshot", "on", &error_abort);
651 return 0;
654 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
655 int index, const char *optstr)
657 QemuOpts *opts;
658 DriveInfo *dinfo;
660 if (!enable || drive_get_by_index(type, index)) {
661 return;
664 opts = drive_add(type, index, NULL, optstr);
665 if (snapshot) {
666 drive_enable_snapshot(NULL, opts, NULL);
669 dinfo = drive_new(opts, type, &error_abort);
670 dinfo->is_default = true;
674 static void configure_blockdev(BlockdevOptionsQueue *bdo_queue,
675 MachineClass *machine_class, int snapshot)
678 * If the currently selected machine wishes to override the
679 * units-per-bus property of its default HBA interface type, do so
680 * now.
682 if (machine_class->units_per_default_bus) {
683 override_max_devs(machine_class->block_default_type,
684 machine_class->units_per_default_bus);
687 /* open the virtual block devices */
688 while (!QSIMPLEQ_EMPTY(bdo_queue)) {
689 BlockdevOptionsQueueEntry *bdo = QSIMPLEQ_FIRST(bdo_queue);
691 QSIMPLEQ_REMOVE_HEAD(bdo_queue, entry);
692 loc_push_restore(&bdo->loc);
693 qmp_blockdev_add(bdo->bdo, &error_fatal);
694 loc_pop(&bdo->loc);
695 qapi_free_BlockdevOptions(bdo->bdo);
696 g_free(bdo);
698 if (snapshot) {
699 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
700 NULL, NULL);
702 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
703 &machine_class->block_default_type, &error_fatal)) {
704 /* We printed help */
705 exit(0);
708 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
709 CDROM_OPTS);
710 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
711 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
715 static QemuOptsList qemu_smp_opts = {
716 .name = "smp-opts",
717 .implied_opt_name = "cpus",
718 .merge_lists = true,
719 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
720 .desc = {
722 .name = "cpus",
723 .type = QEMU_OPT_NUMBER,
724 }, {
725 .name = "sockets",
726 .type = QEMU_OPT_NUMBER,
727 }, {
728 .name = "dies",
729 .type = QEMU_OPT_NUMBER,
730 }, {
731 .name = "clusters",
732 .type = QEMU_OPT_NUMBER,
733 }, {
734 .name = "cores",
735 .type = QEMU_OPT_NUMBER,
736 }, {
737 .name = "threads",
738 .type = QEMU_OPT_NUMBER,
739 }, {
740 .name = "maxcpus",
741 .type = QEMU_OPT_NUMBER,
743 { /*End of list */ }
747 static void realtime_init(void)
749 if (enable_mlock) {
750 if (os_mlock() < 0) {
751 error_report("locking memory failed");
752 exit(1);
758 static void configure_msg(QemuOpts *opts)
760 message_with_timestamp = qemu_opt_get_bool(opts, "timestamp", false);
761 error_with_guestname = qemu_opt_get_bool(opts, "guest-name", false);
765 /***********************************************************/
766 /* USB devices */
768 static int usb_device_add(const char *devname)
770 USBDevice *dev = NULL;
772 if (!machine_usb(current_machine)) {
773 return -1;
776 dev = usbdevice_create(devname);
777 if (!dev)
778 return -1;
780 return 0;
783 static int usb_parse(const char *cmdline)
785 int r;
786 r = usb_device_add(cmdline);
787 if (r < 0) {
788 error_report("could not add USB device '%s'", cmdline);
790 return r;
793 /***********************************************************/
794 /* machine registration */
796 static MachineClass *find_machine(const char *name, GSList *machines)
798 GSList *el;
800 for (el = machines; el; el = el->next) {
801 MachineClass *mc = el->data;
803 if (!strcmp(mc->name, name) || !g_strcmp0(mc->alias, name)) {
804 return mc;
808 return NULL;
811 static MachineClass *find_default_machine(GSList *machines)
813 GSList *el;
814 MachineClass *default_machineclass = NULL;
816 for (el = machines; el; el = el->next) {
817 MachineClass *mc = el->data;
819 if (mc->is_default) {
820 assert(default_machineclass == NULL && "Multiple default machines");
821 default_machineclass = mc;
825 return default_machineclass;
828 static void version(void)
830 printf("QEMU emulator version " QEMU_FULL_VERSION "\n"
831 QEMU_COPYRIGHT "\n");
834 static void help(int exitcode)
836 version();
837 printf("usage: %s [options] [disk_image]\n\n"
838 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
839 error_get_progname());
841 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
842 if ((arch_mask) & arch_type) \
843 fputs(opt_help, stdout);
845 #define ARCHHEADING(text, arch_mask) \
846 if ((arch_mask) & arch_type) \
847 puts(stringify(text));
849 #define DEFHEADING(text) ARCHHEADING(text, QEMU_ARCH_ALL)
851 #include "qemu-options.def"
853 printf("\nDuring emulation, the following keys are useful:\n"
854 "ctrl-alt-f toggle full screen\n"
855 "ctrl-alt-n switch to virtual console 'n'\n"
856 "ctrl-alt toggle mouse and keyboard grab\n"
857 "\n"
858 "When using -nographic, press 'ctrl-a h' to get some help.\n"
859 "\n"
860 QEMU_HELP_BOTTOM "\n");
862 exit(exitcode);
865 #define HAS_ARG 0x0001
867 typedef struct QEMUOption {
868 const char *name;
869 int flags;
870 int index;
871 uint32_t arch_mask;
872 } QEMUOption;
874 static const QEMUOption qemu_options[] = {
875 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
877 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
878 { option, opt_arg, opt_enum, arch_mask },
879 #define DEFHEADING(text)
880 #define ARCHHEADING(text, arch_mask)
882 #include "qemu-options.def"
883 { NULL },
886 typedef struct VGAInterfaceInfo {
887 const char *opt_name; /* option name */
888 const char *name; /* human-readable name */
889 /* Class names indicating that support is available.
890 * If no class is specified, the interface is always available */
891 const char *class_names[2];
892 } VGAInterfaceInfo;
894 static const VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
895 [VGA_NONE] = {
896 .opt_name = "none",
897 .name = "no graphic card",
899 [VGA_STD] = {
900 .opt_name = "std",
901 .name = "standard VGA",
902 .class_names = { "VGA", "isa-vga" },
904 [VGA_CIRRUS] = {
905 .opt_name = "cirrus",
906 .name = "Cirrus VGA",
907 .class_names = { "cirrus-vga", "isa-cirrus-vga" },
909 [VGA_VMWARE] = {
910 .opt_name = "vmware",
911 .name = "VMWare SVGA",
912 .class_names = { "vmware-svga" },
914 [VGA_VIRTIO] = {
915 .opt_name = "virtio",
916 .name = "Virtio VGA",
917 .class_names = { "virtio-vga" },
919 [VGA_QXL] = {
920 .opt_name = "qxl",
921 .name = "QXL VGA",
922 .class_names = { "qxl-vga" },
924 [VGA_TCX] = {
925 .opt_name = "tcx",
926 .name = "TCX framebuffer",
927 .class_names = { "sun-tcx" },
929 [VGA_CG3] = {
930 .opt_name = "cg3",
931 .name = "CG3 framebuffer",
932 .class_names = { "cgthree" },
934 [VGA_XENFB] = {
935 .opt_name = "xenfb",
936 .name = "Xen paravirtualized framebuffer",
940 static bool vga_interface_available(VGAInterfaceType t)
942 const VGAInterfaceInfo *ti = &vga_interfaces[t];
944 assert(t < VGA_TYPE_MAX);
945 return !ti->class_names[0] ||
946 module_object_class_by_name(ti->class_names[0]) ||
947 module_object_class_by_name(ti->class_names[1]);
950 static const char *
951 get_default_vga_model(const MachineClass *machine_class)
953 if (machine_class->default_display) {
954 return machine_class->default_display;
955 } else if (vga_interface_available(VGA_CIRRUS)) {
956 return "cirrus";
957 } else if (vga_interface_available(VGA_STD)) {
958 return "std";
961 return NULL;
964 static void select_vgahw(const MachineClass *machine_class, const char *p)
966 const char *opts;
967 int t;
969 if (g_str_equal(p, "help")) {
970 const char *def = get_default_vga_model(machine_class);
972 for (t = 0; t < VGA_TYPE_MAX; t++) {
973 const VGAInterfaceInfo *ti = &vga_interfaces[t];
975 if (vga_interface_available(t) && ti->opt_name) {
976 printf("%-20s %s%s\n", ti->opt_name, ti->name ?: "",
977 g_str_equal(ti->opt_name, def) ? " (default)" : "");
980 exit(0);
983 assert(vga_interface_type == VGA_NONE);
984 for (t = 0; t < VGA_TYPE_MAX; t++) {
985 const VGAInterfaceInfo *ti = &vga_interfaces[t];
986 if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
987 if (!vga_interface_available(t)) {
988 error_report("%s not available", ti->name);
989 exit(1);
991 vga_interface_type = t;
992 break;
995 if (t == VGA_TYPE_MAX) {
996 invalid_vga:
997 error_report("unknown vga type: %s", p);
998 exit(1);
1000 while (*opts) {
1001 const char *nextopt;
1003 if (strstart(opts, ",retrace=", &nextopt)) {
1004 opts = nextopt;
1005 if (strstart(opts, "dumb", &nextopt))
1006 vga_retrace_method = VGA_RETRACE_DUMB;
1007 else if (strstart(opts, "precise", &nextopt))
1008 vga_retrace_method = VGA_RETRACE_PRECISE;
1009 else goto invalid_vga;
1010 } else goto invalid_vga;
1011 opts = nextopt;
1015 static void parse_display_qapi(const char *optarg)
1017 DisplayOptions *opts;
1018 Visitor *v;
1020 v = qobject_input_visitor_new_str(optarg, "type", &error_fatal);
1022 visit_type_DisplayOptions(v, NULL, &opts, &error_fatal);
1023 QAPI_CLONE_MEMBERS(DisplayOptions, &dpy, opts);
1025 qapi_free_DisplayOptions(opts);
1026 visit_free(v);
1029 DisplayOptions *qmp_query_display_options(Error **errp)
1031 return QAPI_CLONE(DisplayOptions, &dpy);
1034 static void parse_display(const char *p)
1036 const char *opts;
1038 if (is_help_option(p)) {
1039 qemu_display_help();
1040 exit(0);
1043 if (strstart(p, "sdl", &opts)) {
1045 * sdl DisplayType needs hand-crafted parser instead of
1046 * parse_display_qapi() due to some options not in
1047 * DisplayOptions, specifically:
1048 * - ctrl_grab + alt_grab
1049 * They can't be moved into the QAPI since they use underscores,
1050 * thus they will get replaced by "grab-mod" in the long term
1052 #if defined(CONFIG_SDL)
1053 dpy.type = DISPLAY_TYPE_SDL;
1054 while (*opts) {
1055 const char *nextopt;
1057 if (strstart(opts, ",grab-mod=", &nextopt)) {
1058 opts = nextopt;
1059 if (strstart(opts, "lshift-lctrl-lalt", &nextopt)) {
1060 alt_grab = 1;
1061 } else if (strstart(opts, "rctrl", &nextopt)) {
1062 ctrl_grab = 1;
1063 } else {
1064 goto invalid_sdl_args;
1066 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
1067 opts = nextopt;
1068 if (strstart(opts, "on", &nextopt)) {
1069 alt_grab = 1;
1070 } else if (strstart(opts, "off", &nextopt)) {
1071 alt_grab = 0;
1072 } else {
1073 goto invalid_sdl_args;
1075 warn_report("alt_grab is deprecated, use grab-mod instead.");
1076 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1077 opts = nextopt;
1078 if (strstart(opts, "on", &nextopt)) {
1079 ctrl_grab = 1;
1080 } else if (strstart(opts, "off", &nextopt)) {
1081 ctrl_grab = 0;
1082 } else {
1083 goto invalid_sdl_args;
1085 warn_report("ctrl_grab is deprecated, use grab-mod instead.");
1086 } else if (strstart(opts, ",window_close=", &nextopt) ||
1087 strstart(opts, ",window-close=", &nextopt)) {
1088 if (strstart(opts, ",window_close=", NULL)) {
1089 warn_report("window_close with an underscore is deprecated,"
1090 " please use window-close instead.");
1092 opts = nextopt;
1093 dpy.has_window_close = true;
1094 if (strstart(opts, "on", &nextopt)) {
1095 dpy.window_close = true;
1096 } else if (strstart(opts, "off", &nextopt)) {
1097 dpy.window_close = false;
1098 } else {
1099 goto invalid_sdl_args;
1101 } else if (strstart(opts, ",show-cursor=", &nextopt)) {
1102 opts = nextopt;
1103 dpy.has_show_cursor = true;
1104 if (strstart(opts, "on", &nextopt)) {
1105 dpy.show_cursor = true;
1106 } else if (strstart(opts, "off", &nextopt)) {
1107 dpy.show_cursor = false;
1108 } else {
1109 goto invalid_sdl_args;
1111 } else if (strstart(opts, ",gl=", &nextopt)) {
1112 opts = nextopt;
1113 dpy.has_gl = true;
1114 if (strstart(opts, "on", &nextopt)) {
1115 dpy.gl = DISPLAYGL_MODE_ON;
1116 } else if (strstart(opts, "core", &nextopt)) {
1117 dpy.gl = DISPLAYGL_MODE_CORE;
1118 } else if (strstart(opts, "es", &nextopt)) {
1119 dpy.gl = DISPLAYGL_MODE_ES;
1120 } else if (strstart(opts, "off", &nextopt)) {
1121 dpy.gl = DISPLAYGL_MODE_OFF;
1122 } else {
1123 goto invalid_sdl_args;
1125 } else {
1126 invalid_sdl_args:
1127 error_report("invalid SDL option string");
1128 exit(1);
1130 opts = nextopt;
1132 #else
1133 error_report("SDL display supported is not available in this binary");
1134 exit(1);
1135 #endif
1136 } else if (strstart(p, "vnc", &opts)) {
1138 * vnc isn't a (local) DisplayType but a protocol for remote
1139 * display access.
1141 if (*opts == '=') {
1142 vnc_parse(opts + 1);
1143 } else {
1144 error_report("VNC requires a display argument vnc=<display>");
1145 exit(1);
1147 } else {
1148 parse_display_qapi(p);
1152 static inline bool nonempty_str(const char *str)
1154 return str && *str;
1157 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
1159 gchar *buf;
1160 size_t size;
1161 const char *name, *file, *str, *gen_id;
1162 FWCfgState *fw_cfg = (FWCfgState *) opaque;
1164 if (fw_cfg == NULL) {
1165 error_setg(errp, "fw_cfg device not available");
1166 return -1;
1168 name = qemu_opt_get(opts, "name");
1169 file = qemu_opt_get(opts, "file");
1170 str = qemu_opt_get(opts, "string");
1171 gen_id = qemu_opt_get(opts, "gen_id");
1173 /* we need the name, and exactly one of: file, content string, gen_id */
1174 if (!nonempty_str(name) ||
1175 nonempty_str(file) + nonempty_str(str) + nonempty_str(gen_id) != 1) {
1176 error_setg(errp, "name, plus exactly one of file,"
1177 " string and gen_id, are needed");
1178 return -1;
1180 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
1181 error_setg(errp, "name too long (max. %d char)",
1182 FW_CFG_MAX_FILE_PATH - 1);
1183 return -1;
1185 if (nonempty_str(gen_id)) {
1187 * In this particular case where the content is populated
1188 * internally, the "etc/" namespace protection is relaxed,
1189 * so do not emit a warning.
1191 } else if (strncmp(name, "opt/", 4) != 0) {
1192 warn_report("externally provided fw_cfg item names "
1193 "should be prefixed with \"opt/\"");
1195 if (nonempty_str(str)) {
1196 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
1197 buf = g_memdup(str, size);
1198 } else if (nonempty_str(gen_id)) {
1199 if (!fw_cfg_add_from_generator(fw_cfg, name, gen_id, errp)) {
1200 return -1;
1202 return 0;
1203 } else {
1204 GError *err = NULL;
1205 if (!g_file_get_contents(file, &buf, &size, &err)) {
1206 error_setg(errp, "can't load %s: %s", file, err->message);
1207 g_error_free(err);
1208 return -1;
1211 /* For legacy, keep user files in a specific global order. */
1212 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
1213 fw_cfg_add_file(fw_cfg, name, buf, size);
1214 fw_cfg_reset_order_override(fw_cfg);
1215 return 0;
1218 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
1220 return qdev_device_help(opts);
1223 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
1225 DeviceState *dev;
1227 dev = qdev_device_add(opts, errp);
1228 if (!dev && *errp) {
1229 error_report_err(*errp);
1230 return -1;
1231 } else if (dev) {
1232 object_unref(OBJECT(dev));
1234 return 0;
1237 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
1239 Error *local_err = NULL;
1241 if (!qemu_chr_new_from_opts(opts, NULL, &local_err)) {
1242 if (local_err) {
1243 error_propagate(errp, local_err);
1244 return -1;
1246 exit(0);
1248 return 0;
1251 #ifdef CONFIG_VIRTFS
1252 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
1254 return qemu_fsdev_add(opts, errp);
1256 #endif
1258 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
1260 return monitor_init_opts(opts, errp);
1263 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
1265 static int monitor_device_index = 0;
1266 QemuOpts *opts;
1267 const char *p;
1268 char label[32];
1270 if (strstart(optarg, "chardev:", &p)) {
1271 snprintf(label, sizeof(label), "%s", p);
1272 } else {
1273 snprintf(label, sizeof(label), "compat_monitor%d",
1274 monitor_device_index);
1275 opts = qemu_chr_parse_compat(label, optarg, true);
1276 if (!opts) {
1277 error_report("parse error: %s", optarg);
1278 exit(1);
1282 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
1283 qemu_opt_set(opts, "mode", mode, &error_abort);
1284 qemu_opt_set(opts, "chardev", label, &error_abort);
1285 if (!strcmp(mode, "control")) {
1286 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
1287 } else {
1288 assert(pretty == false);
1290 monitor_device_index++;
1293 struct device_config {
1294 enum {
1295 DEV_USB, /* -usbdevice */
1296 DEV_SERIAL, /* -serial */
1297 DEV_PARALLEL, /* -parallel */
1298 DEV_DEBUGCON, /* -debugcon */
1299 DEV_GDB, /* -gdb, -s */
1300 DEV_SCLP, /* s390 sclp */
1301 } type;
1302 const char *cmdline;
1303 Location loc;
1304 QTAILQ_ENTRY(device_config) next;
1307 static QTAILQ_HEAD(, device_config) device_configs =
1308 QTAILQ_HEAD_INITIALIZER(device_configs);
1310 static void add_device_config(int type, const char *cmdline)
1312 struct device_config *conf;
1314 conf = g_malloc0(sizeof(*conf));
1315 conf->type = type;
1316 conf->cmdline = cmdline;
1317 loc_save(&conf->loc);
1318 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1321 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1323 struct device_config *conf;
1324 int rc;
1326 QTAILQ_FOREACH(conf, &device_configs, next) {
1327 if (conf->type != type)
1328 continue;
1329 loc_push_restore(&conf->loc);
1330 rc = func(conf->cmdline);
1331 loc_pop(&conf->loc);
1332 if (rc) {
1333 return rc;
1336 return 0;
1339 static void qemu_disable_default_devices(void)
1341 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1343 default_driver_check_json();
1344 qemu_opts_foreach(qemu_find_opts("device"),
1345 default_driver_check, NULL, NULL);
1346 qemu_opts_foreach(qemu_find_opts("global"),
1347 default_driver_check, NULL, NULL);
1349 if (!vga_model && !default_vga) {
1350 vga_interface_type = VGA_DEVICE;
1352 if (!has_defaults || machine_class->no_serial) {
1353 default_serial = 0;
1355 if (!has_defaults || machine_class->no_parallel) {
1356 default_parallel = 0;
1358 if (!has_defaults || machine_class->no_floppy) {
1359 default_floppy = 0;
1361 if (!has_defaults || machine_class->no_cdrom) {
1362 default_cdrom = 0;
1364 if (!has_defaults || machine_class->no_sdcard) {
1365 default_sdcard = 0;
1367 if (!has_defaults) {
1368 default_monitor = 0;
1369 default_net = 0;
1370 default_vga = 0;
1374 static void qemu_create_default_devices(void)
1376 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1378 if (is_daemonized()) {
1379 /* According to documentation and historically, -nographic redirects
1380 * serial port, parallel port and monitor to stdio, which does not work
1381 * with -daemonize. We can redirect these to null instead, but since
1382 * -nographic is legacy, let's just error out.
1383 * We disallow -nographic only if all other ports are not redirected
1384 * explicitly, to not break existing legacy setups which uses
1385 * -nographic _and_ redirects all ports explicitly - this is valid
1386 * usage, -nographic is just a no-op in this case.
1388 if (nographic
1389 && (default_parallel || default_serial || default_monitor)) {
1390 error_report("-nographic cannot be used with -daemonize");
1391 exit(1);
1395 if (nographic) {
1396 if (default_parallel)
1397 add_device_config(DEV_PARALLEL, "null");
1398 if (default_serial && default_monitor) {
1399 add_device_config(DEV_SERIAL, "mon:stdio");
1400 } else {
1401 if (default_serial)
1402 add_device_config(DEV_SERIAL, "stdio");
1403 if (default_monitor)
1404 monitor_parse("stdio", "readline", false);
1406 } else {
1407 if (default_serial)
1408 add_device_config(DEV_SERIAL, "vc:80Cx24C");
1409 if (default_parallel)
1410 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
1411 if (default_monitor)
1412 monitor_parse("vc:80Cx24C", "readline", false);
1415 if (default_net) {
1416 QemuOptsList *net = qemu_find_opts("net");
1417 qemu_opts_parse(net, "nic", true, &error_abort);
1418 #ifdef CONFIG_SLIRP
1419 qemu_opts_parse(net, "user", true, &error_abort);
1420 #endif
1423 #if defined(CONFIG_VNC)
1424 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
1425 display_remote++;
1427 #endif
1428 if (dpy.type == DISPLAY_TYPE_DEFAULT && !display_remote) {
1429 if (!qemu_display_find_default(&dpy)) {
1430 dpy.type = DISPLAY_TYPE_NONE;
1431 #if defined(CONFIG_VNC)
1432 vnc_parse("localhost:0,to=99,id=default");
1433 #endif
1436 if (dpy.type == DISPLAY_TYPE_DEFAULT) {
1437 dpy.type = DISPLAY_TYPE_NONE;
1440 /* If no default VGA is requested, the default is "none". */
1441 if (default_vga) {
1442 vga_model = get_default_vga_model(machine_class);
1444 if (vga_model) {
1445 select_vgahw(machine_class, vga_model);
1449 static int serial_parse(const char *devname)
1451 int index = num_serial_hds;
1452 char label[32];
1454 if (strcmp(devname, "none") == 0)
1455 return 0;
1456 snprintf(label, sizeof(label), "serial%d", index);
1457 serial_hds = g_renew(Chardev *, serial_hds, index + 1);
1459 serial_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
1460 if (!serial_hds[index]) {
1461 error_report("could not connect serial device"
1462 " to character backend '%s'", devname);
1463 return -1;
1465 num_serial_hds++;
1466 return 0;
1469 Chardev *serial_hd(int i)
1471 assert(i >= 0);
1472 if (i < num_serial_hds) {
1473 return serial_hds[i];
1475 return NULL;
1478 static int parallel_parse(const char *devname)
1480 static int index = 0;
1481 char label[32];
1483 if (strcmp(devname, "none") == 0)
1484 return 0;
1485 if (index == MAX_PARALLEL_PORTS) {
1486 error_report("too many parallel ports");
1487 exit(1);
1489 snprintf(label, sizeof(label), "parallel%d", index);
1490 parallel_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
1491 if (!parallel_hds[index]) {
1492 error_report("could not connect parallel device"
1493 " to character backend '%s'", devname);
1494 return -1;
1496 index++;
1497 return 0;
1500 static int debugcon_parse(const char *devname)
1502 QemuOpts *opts;
1504 if (!qemu_chr_new_mux_mon("debugcon", devname, NULL)) {
1505 error_report("invalid character backend '%s'", devname);
1506 exit(1);
1508 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
1509 if (!opts) {
1510 error_report("already have a debugcon device");
1511 exit(1);
1513 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
1514 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
1515 return 0;
1518 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
1520 const MachineClass *mc1 = a, *mc2 = b;
1521 int res;
1523 if (mc1->family == NULL) {
1524 if (mc2->family == NULL) {
1525 /* Compare standalone machine types against each other; they sort
1526 * in increasing order.
1528 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
1529 object_class_get_name(OBJECT_CLASS(mc2)));
1532 /* Standalone machine types sort after families. */
1533 return 1;
1536 if (mc2->family == NULL) {
1537 /* Families sort before standalone machine types. */
1538 return -1;
1541 /* Families sort between each other alphabetically increasingly. */
1542 res = strcmp(mc1->family, mc2->family);
1543 if (res != 0) {
1544 return res;
1547 /* Within the same family, machine types sort in decreasing order. */
1548 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
1549 object_class_get_name(OBJECT_CLASS(mc1)));
1552 static void machine_help_func(const QDict *qdict)
1554 GSList *machines, *el;
1555 const char *type = qdict_get_try_str(qdict, "type");
1557 machines = object_class_get_list(TYPE_MACHINE, false);
1558 if (type) {
1559 ObjectClass *machine_class = OBJECT_CLASS(find_machine(type, machines));
1560 if (machine_class) {
1561 type_print_class_properties(object_class_get_name(machine_class));
1562 return;
1566 printf("Supported machines are:\n");
1567 machines = g_slist_sort(machines, machine_class_cmp);
1568 for (el = machines; el; el = el->next) {
1569 MachineClass *mc = el->data;
1570 if (mc->alias) {
1571 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
1573 printf("%-20s %s%s%s\n", mc->name, mc->desc,
1574 mc->is_default ? " (default)" : "",
1575 mc->deprecation_reason ? " (deprecated)" : "");
1579 static void
1580 machine_merge_property(const char *propname, QDict *prop, Error **errp)
1582 QDict *opts;
1584 opts = qdict_new();
1585 /* Preserve the caller's reference to prop. */
1586 qobject_ref(prop);
1587 qdict_put(opts, propname, prop);
1588 keyval_merge(machine_opts_dict, opts, errp);
1589 qobject_unref(opts);
1592 static void
1593 machine_parse_property_opt(QemuOptsList *opts_list, const char *propname,
1594 const char *arg)
1596 QDict *prop = NULL;
1597 bool help = false;
1599 prop = keyval_parse(arg, opts_list->implied_opt_name, &help, &error_fatal);
1600 if (help) {
1601 qemu_opts_print_help(opts_list, true);
1602 exit(0);
1604 machine_merge_property(propname, prop, &error_fatal);
1605 qobject_unref(prop);
1608 static const char *pid_file;
1609 static Notifier qemu_unlink_pidfile_notifier;
1611 static void qemu_unlink_pidfile(Notifier *n, void *data)
1613 if (pid_file) {
1614 unlink(pid_file);
1618 static const QEMUOption *lookup_opt(int argc, char **argv,
1619 const char **poptarg, int *poptind)
1621 const QEMUOption *popt;
1622 int optind = *poptind;
1623 char *r = argv[optind];
1624 const char *optarg;
1626 loc_set_cmdline(argv, optind, 1);
1627 optind++;
1628 /* Treat --foo the same as -foo. */
1629 if (r[1] == '-')
1630 r++;
1631 popt = qemu_options;
1632 for(;;) {
1633 if (!popt->name) {
1634 error_report("invalid option");
1635 exit(1);
1637 if (!strcmp(popt->name, r + 1))
1638 break;
1639 popt++;
1641 if (popt->flags & HAS_ARG) {
1642 if (optind >= argc) {
1643 error_report("requires an argument");
1644 exit(1);
1646 optarg = argv[optind++];
1647 loc_set_cmdline(argv, optind - 2, 2);
1648 } else {
1649 optarg = NULL;
1652 *poptarg = optarg;
1653 *poptind = optind;
1655 return popt;
1658 static MachineClass *select_machine(QDict *qdict, Error **errp)
1660 const char *optarg = qdict_get_try_str(qdict, "type");
1661 GSList *machines = object_class_get_list(TYPE_MACHINE, false);
1662 MachineClass *machine_class;
1663 Error *local_err = NULL;
1665 if (optarg) {
1666 machine_class = find_machine(optarg, machines);
1667 qdict_del(qdict, "type");
1668 if (!machine_class) {
1669 error_setg(&local_err, "unsupported machine type");
1671 } else {
1672 machine_class = find_default_machine(machines);
1673 if (!machine_class) {
1674 error_setg(&local_err, "No machine specified, and there is no default");
1678 g_slist_free(machines);
1679 if (local_err) {
1680 error_append_hint(&local_err, "Use -machine help to list supported machines\n");
1681 error_propagate(errp, local_err);
1683 return machine_class;
1686 static int object_parse_property_opt(Object *obj,
1687 const char *name, const char *value,
1688 const char *skip, Error **errp)
1690 if (g_str_equal(name, skip)) {
1691 return 0;
1694 if (!object_property_parse(obj, name, value, errp)) {
1695 return -1;
1698 return 0;
1701 /* *Non*recursively replace underscores with dashes in QDict keys. */
1702 static void keyval_dashify(QDict *qdict, Error **errp)
1704 const QDictEntry *ent, *next;
1705 char *p;
1707 for (ent = qdict_first(qdict); ent; ent = next) {
1708 g_autofree char *new_key = NULL;
1710 next = qdict_next(qdict, ent);
1711 if (!strchr(ent->key, '_')) {
1712 continue;
1714 new_key = g_strdup(ent->key);
1715 for (p = new_key; *p; p++) {
1716 if (*p == '_') {
1717 *p = '-';
1720 if (qdict_haskey(qdict, new_key)) {
1721 error_setg(errp, "Conflict between '%s' and '%s'", ent->key, new_key);
1722 return;
1724 qobject_ref(ent->value);
1725 qdict_put_obj(qdict, new_key, ent->value);
1726 qdict_del(qdict, ent->key);
1730 static void qemu_apply_legacy_machine_options(QDict *qdict)
1732 const char *value;
1734 keyval_dashify(qdict, &error_fatal);
1736 /* Legacy options do not correspond to MachineState properties. */
1737 value = qdict_get_try_str(qdict, "accel");
1738 if (value) {
1739 accelerators = g_strdup(value);
1740 qdict_del(qdict, "accel");
1743 value = qdict_get_try_str(qdict, "igd-passthru");
1744 if (value) {
1745 object_register_sugar_prop(ACCEL_CLASS_NAME("xen"), "igd-passthru", value,
1746 false);
1747 qdict_del(qdict, "igd-passthru");
1750 value = qdict_get_try_str(qdict, "kvm-shadow-mem");
1751 if (value) {
1752 object_register_sugar_prop(ACCEL_CLASS_NAME("kvm"), "kvm-shadow-mem", value,
1753 false);
1754 qdict_del(qdict, "kvm-shadow-mem");
1757 value = qdict_get_try_str(qdict, "kernel-irqchip");
1758 if (value) {
1759 object_register_sugar_prop(ACCEL_CLASS_NAME("kvm"), "kernel-irqchip", value,
1760 false);
1761 object_register_sugar_prop(ACCEL_CLASS_NAME("whpx"), "kernel-irqchip", value,
1762 false);
1763 qdict_del(qdict, "kernel-irqchip");
1767 static void object_option_foreach_add(bool (*type_opt_predicate)(const char *))
1769 ObjectOption *opt, *next;
1771 QTAILQ_FOREACH_SAFE(opt, &object_opts, next, next) {
1772 const char *type = ObjectType_str(opt->opts->qom_type);
1773 if (type_opt_predicate(type)) {
1774 user_creatable_add_qapi(opt->opts, &error_fatal);
1775 qapi_free_ObjectOptions(opt->opts);
1776 QTAILQ_REMOVE(&object_opts, opt, next);
1777 g_free(opt);
1782 static void object_option_add_visitor(Visitor *v)
1784 ObjectOption *opt = g_new0(ObjectOption, 1);
1785 visit_type_ObjectOptions(v, NULL, &opt->opts, &error_fatal);
1786 QTAILQ_INSERT_TAIL(&object_opts, opt, next);
1789 static void object_option_parse(const char *optarg)
1791 QemuOpts *opts;
1792 const char *type;
1793 Visitor *v;
1795 if (optarg[0] == '{') {
1796 QObject *obj = qobject_from_json(optarg, &error_fatal);
1798 v = qobject_input_visitor_new(obj);
1799 qobject_unref(obj);
1800 } else {
1801 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
1802 optarg, true);
1803 if (!opts) {
1804 exit(1);
1807 type = qemu_opt_get(opts, "qom-type");
1808 if (!type) {
1809 error_setg(&error_fatal, QERR_MISSING_PARAMETER, "qom-type");
1811 if (user_creatable_print_help(type, opts)) {
1812 exit(0);
1815 v = opts_visitor_new(opts);
1818 object_option_add_visitor(v);
1819 visit_free(v);
1823 * Initial object creation happens before all other
1824 * QEMU data types are created. The majority of objects
1825 * can be created at this point. The rng-egd object
1826 * cannot be created here, as it depends on the chardev
1827 * already existing.
1829 static bool object_create_early(const char *type)
1832 * Objects should not be made "delayed" without a reason. If you
1833 * add one, state the reason in a comment!
1836 /* Reason: property "chardev" */
1837 if (g_str_equal(type, "rng-egd") ||
1838 g_str_equal(type, "qtest")) {
1839 return false;
1842 #if defined(CONFIG_VHOST_USER) && defined(CONFIG_LINUX)
1843 /* Reason: cryptodev-vhost-user property "chardev" */
1844 if (g_str_equal(type, "cryptodev-vhost-user")) {
1845 return false;
1847 #endif
1849 /* Reason: vhost-user-blk-server property "node-name" */
1850 if (g_str_equal(type, "vhost-user-blk-server")) {
1851 return false;
1854 * Reason: filter-* property "netdev" etc.
1856 if (g_str_equal(type, "filter-buffer") ||
1857 g_str_equal(type, "filter-dump") ||
1858 g_str_equal(type, "filter-mirror") ||
1859 g_str_equal(type, "filter-redirector") ||
1860 g_str_equal(type, "colo-compare") ||
1861 g_str_equal(type, "filter-rewriter") ||
1862 g_str_equal(type, "filter-replay")) {
1863 return false;
1867 * Allocation of large amounts of memory may delay
1868 * chardev initialization for too long, and trigger timeouts
1869 * on software that waits for a monitor socket to be created
1870 * (e.g. libvirt).
1872 if (g_str_has_prefix(type, "memory-backend-")) {
1873 return false;
1876 return true;
1879 static void qemu_apply_machine_options(QDict *qdict)
1881 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1882 const char *boot_order = NULL;
1883 const char *boot_once = NULL;
1884 QemuOpts *opts;
1886 object_set_properties_from_keyval(OBJECT(current_machine), qdict, false, &error_fatal);
1887 current_machine->ram_size = ram_size;
1888 current_machine->maxram_size = maxram_size;
1889 current_machine->ram_slots = ram_slots;
1891 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
1892 if (opts) {
1893 boot_order = qemu_opt_get(opts, "order");
1894 if (boot_order) {
1895 validate_bootdevices(boot_order, &error_fatal);
1898 boot_once = qemu_opt_get(opts, "once");
1899 if (boot_once) {
1900 validate_bootdevices(boot_once, &error_fatal);
1903 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
1904 boot_strict = qemu_opt_get_bool(opts, "strict", false);
1907 if (!boot_order) {
1908 boot_order = machine_class->default_boot_order;
1911 current_machine->boot_order = boot_order;
1912 current_machine->boot_once = boot_once;
1914 if (semihosting_enabled() && !semihosting_get_argc()) {
1915 /* fall back to the -kernel/-append */
1916 semihosting_arg_fallback(current_machine->kernel_filename, current_machine->kernel_cmdline);
1919 if (current_machine->smp.cpus > 1) {
1920 Error *blocker = NULL;
1921 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
1922 replay_add_blocker(blocker);
1926 static void qemu_create_early_backends(void)
1928 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1929 #if defined(CONFIG_SDL)
1930 const bool use_sdl = (dpy.type == DISPLAY_TYPE_SDL);
1931 #else
1932 const bool use_sdl = false;
1933 #endif
1934 #if defined(CONFIG_GTK)
1935 const bool use_gtk = (dpy.type == DISPLAY_TYPE_GTK);
1936 #else
1937 const bool use_gtk = false;
1938 #endif
1940 if ((alt_grab || ctrl_grab) && !use_sdl) {
1941 error_report("-alt-grab and -ctrl-grab are only valid "
1942 "for SDL, ignoring option");
1944 if (dpy.has_window_close && !use_gtk && !use_sdl) {
1945 error_report("window-close is only valid for GTK and SDL, "
1946 "ignoring option");
1949 qemu_display_early_init(&dpy);
1950 qemu_console_early_init();
1952 if (dpy.has_gl && dpy.gl != DISPLAYGL_MODE_OFF && display_opengl == 0) {
1953 #if defined(CONFIG_OPENGL)
1954 error_report("OpenGL is not supported by the display");
1955 #else
1956 error_report("OpenGL support is disabled");
1957 #endif
1958 exit(1);
1961 object_option_foreach_add(object_create_early);
1963 /* spice needs the timers to be initialized by this point */
1964 /* spice must initialize before audio as it changes the default auiodev */
1965 /* spice must initialize before chardevs (for spicevmc and spiceport) */
1966 qemu_spice.init();
1968 qemu_opts_foreach(qemu_find_opts("chardev"),
1969 chardev_init_func, NULL, &error_fatal);
1971 #ifdef CONFIG_VIRTFS
1972 qemu_opts_foreach(qemu_find_opts("fsdev"),
1973 fsdev_init_func, NULL, &error_fatal);
1974 #endif
1977 * Note: we need to create audio and block backends before
1978 * setting machine properties, so they can be referred to.
1980 configure_blockdev(&bdo_queue, machine_class, snapshot);
1981 audio_init_audiodevs();
1986 * The remainder of object creation happens after the
1987 * creation of chardev, fsdev, net clients and device data types.
1989 static bool object_create_late(const char *type)
1991 return !object_create_early(type);
1994 static void qemu_create_late_backends(void)
1996 if (qtest_chrdev) {
1997 qtest_server_init(qtest_chrdev, qtest_log, &error_fatal);
2000 net_init_clients(&error_fatal);
2002 object_option_foreach_add(object_create_late);
2004 if (tpm_init() < 0) {
2005 exit(1);
2008 qemu_opts_foreach(qemu_find_opts("mon"),
2009 mon_init_func, NULL, &error_fatal);
2011 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
2012 exit(1);
2013 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
2014 exit(1);
2015 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
2016 exit(1);
2018 /* now chardevs have been created we may have semihosting to connect */
2019 qemu_semihosting_connect_chardevs();
2020 qemu_semihosting_console_init();
2023 static bool have_custom_ram_size(void)
2025 QemuOpts *opts = qemu_find_opts_singleton("memory");
2026 return !!qemu_opt_get_size(opts, "size", 0);
2029 static void qemu_resolve_machine_memdev(void)
2031 if (current_machine->ram_memdev_id) {
2032 Object *backend;
2033 ram_addr_t backend_size;
2035 backend = object_resolve_path_type(current_machine->ram_memdev_id,
2036 TYPE_MEMORY_BACKEND, NULL);
2037 if (!backend) {
2038 error_report("Memory backend '%s' not found",
2039 current_machine->ram_memdev_id);
2040 exit(EXIT_FAILURE);
2042 backend_size = object_property_get_uint(backend, "size", &error_abort);
2043 if (have_custom_ram_size() && backend_size != ram_size) {
2044 error_report("Size specified by -m option must match size of "
2045 "explicitly specified 'memory-backend' property");
2046 exit(EXIT_FAILURE);
2048 if (mem_path) {
2049 error_report("'-mem-path' can't be used together with"
2050 "'-machine memory-backend'");
2051 exit(EXIT_FAILURE);
2053 ram_size = backend_size;
2056 if (!xen_enabled()) {
2057 /* On 32-bit hosts, QEMU is limited by virtual address space */
2058 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
2059 error_report("at most 2047 MB RAM can be simulated");
2060 exit(1);
2065 static void set_memory_options(MachineClass *mc)
2067 uint64_t sz;
2068 const char *mem_str;
2069 const ram_addr_t default_ram_size = mc->default_ram_size;
2070 QemuOpts *opts = qemu_find_opts_singleton("memory");
2071 Location loc;
2073 loc_push_none(&loc);
2074 qemu_opts_loc_restore(opts);
2076 sz = 0;
2077 mem_str = qemu_opt_get(opts, "size");
2078 if (mem_str) {
2079 if (!*mem_str) {
2080 error_report("missing 'size' option value");
2081 exit(EXIT_FAILURE);
2084 sz = qemu_opt_get_size(opts, "size", ram_size);
2086 /* Fix up legacy suffix-less format */
2087 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2088 uint64_t overflow_check = sz;
2090 sz *= MiB;
2091 if (sz / MiB != overflow_check) {
2092 error_report("too large 'size' option value");
2093 exit(EXIT_FAILURE);
2098 /* backward compatibility behaviour for case "-m 0" */
2099 if (sz == 0) {
2100 sz = default_ram_size;
2103 sz = QEMU_ALIGN_UP(sz, 8192);
2104 if (mc->fixup_ram_size) {
2105 sz = mc->fixup_ram_size(sz);
2107 ram_size = sz;
2108 if (ram_size != sz) {
2109 error_report("ram size too large");
2110 exit(EXIT_FAILURE);
2113 maxram_size = ram_size;
2115 if (qemu_opt_get(opts, "maxmem")) {
2116 uint64_t slots;
2118 sz = qemu_opt_get_size(opts, "maxmem", 0);
2119 slots = qemu_opt_get_number(opts, "slots", 0);
2120 if (sz < ram_size) {
2121 error_report("invalid value of -m option maxmem: "
2122 "maximum memory size (0x%" PRIx64 ") must be at least "
2123 "the initial memory size (0x" RAM_ADDR_FMT ")",
2124 sz, ram_size);
2125 exit(EXIT_FAILURE);
2126 } else if (slots && sz == ram_size) {
2127 error_report("invalid value of -m option maxmem: "
2128 "memory slots were specified but maximum memory size "
2129 "(0x%" PRIx64 ") is equal to the initial memory size "
2130 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
2131 exit(EXIT_FAILURE);
2134 maxram_size = sz;
2135 ram_slots = slots;
2136 } else if (qemu_opt_get(opts, "slots")) {
2137 error_report("invalid -m option value: missing 'maxmem' option");
2138 exit(EXIT_FAILURE);
2141 loc_pop(&loc);
2144 static void qemu_create_machine(QDict *qdict)
2146 MachineClass *machine_class = select_machine(qdict, &error_fatal);
2147 object_set_machine_compat_props(machine_class->compat_props);
2149 set_memory_options(machine_class);
2151 current_machine = MACHINE(object_new_with_class(OBJECT_CLASS(machine_class)));
2152 object_property_add_child(object_get_root(), "machine",
2153 OBJECT(current_machine));
2154 object_property_add_child(container_get(OBJECT(current_machine),
2155 "/unattached"),
2156 "sysbus", OBJECT(sysbus_get_default()));
2158 if (machine_class->minimum_page_bits) {
2159 if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
2160 /* This would be a board error: specifying a minimum smaller than
2161 * a target's compile-time fixed setting.
2163 g_assert_not_reached();
2167 cpu_exec_init_all();
2168 page_size_init();
2170 if (machine_class->hw_version) {
2171 qemu_set_hw_version(machine_class->hw_version);
2175 * Get the default machine options from the machine if it is not already
2176 * specified either by the configuration file or by the command line.
2178 if (machine_class->default_machine_opts) {
2179 QDict *default_opts =
2180 keyval_parse(machine_class->default_machine_opts, NULL, NULL,
2181 &error_abort);
2182 qemu_apply_legacy_machine_options(default_opts);
2183 object_set_properties_from_keyval(OBJECT(current_machine), default_opts,
2184 false, &error_abort);
2185 qobject_unref(default_opts);
2189 static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
2191 GlobalProperty *g;
2193 g = g_malloc0(sizeof(*g));
2194 g->driver = qemu_opt_get(opts, "driver");
2195 g->property = qemu_opt_get(opts, "property");
2196 g->value = qemu_opt_get(opts, "value");
2197 qdev_prop_register_global(g);
2198 return 0;
2202 * Return whether configuration group @group is stored in QemuOpts, or
2203 * recorded as one or more QDicts by qemu_record_config_group.
2205 static bool is_qemuopts_group(const char *group)
2207 if (g_str_equal(group, "object") ||
2208 g_str_equal(group, "machine") ||
2209 g_str_equal(group, "smp-opts")) {
2210 return false;
2212 return true;
2215 static void qemu_record_config_group(const char *group, QDict *dict,
2216 bool from_json, Error **errp)
2218 if (g_str_equal(group, "object")) {
2219 Visitor *v = qobject_input_visitor_new_keyval(QOBJECT(dict));
2220 object_option_add_visitor(v);
2221 visit_free(v);
2222 } else if (g_str_equal(group, "machine")) {
2224 * Cannot merge string-valued and type-safe dictionaries, so JSON
2225 * is not accepted yet for -M.
2227 assert(!from_json);
2228 keyval_merge(machine_opts_dict, dict, errp);
2229 } else if (g_str_equal(group, "smp-opts")) {
2230 machine_merge_property("smp", dict, &error_fatal);
2231 } else {
2232 abort();
2237 * Parse non-QemuOpts config file groups, pass the rest to
2238 * qemu_config_do_parse.
2240 static void qemu_parse_config_group(const char *group, QDict *qdict,
2241 void *opaque, Error **errp)
2243 QObject *crumpled;
2244 if (is_qemuopts_group(group)) {
2245 qemu_config_do_parse(group, qdict, opaque, errp);
2246 return;
2249 crumpled = qdict_crumple(qdict, errp);
2250 if (!crumpled) {
2251 return;
2253 switch (qobject_type(crumpled)) {
2254 case QTYPE_QDICT:
2255 qemu_record_config_group(group, qobject_to(QDict, crumpled), false, errp);
2256 break;
2257 case QTYPE_QLIST:
2258 error_setg(errp, "Lists cannot be at top level of a configuration section");
2259 break;
2260 default:
2261 g_assert_not_reached();
2263 qobject_unref(crumpled);
2266 static void qemu_read_default_config_file(Error **errp)
2268 ERRP_GUARD();
2269 int ret;
2270 g_autofree char *file = get_relocated_path(CONFIG_QEMU_CONFDIR "/qemu.conf");
2272 ret = qemu_read_config_file(file, qemu_parse_config_group, errp);
2273 if (ret < 0) {
2274 if (ret == -ENOENT) {
2275 error_free(*errp);
2276 *errp = NULL;
2281 static void qemu_set_option(const char *str, Error **errp)
2283 char group[64], id[64], arg[64];
2284 QemuOptsList *list;
2285 QemuOpts *opts;
2286 int rc, offset;
2288 rc = sscanf(str, "%63[^.].%63[^.].%63[^=]%n", group, id, arg, &offset);
2289 if (rc < 3 || str[offset] != '=') {
2290 error_setg(errp, "can't parse: \"%s\"", str);
2291 return;
2294 if (!is_qemuopts_group(group)) {
2295 error_setg(errp, "-set is not supported with %s", group);
2296 } else {
2297 list = qemu_find_opts_err(group, errp);
2298 if (list) {
2299 opts = qemu_opts_find(list, id);
2300 if (!opts) {
2301 error_setg(errp, "there is no %s \"%s\" defined", group, id);
2302 return;
2304 qemu_opt_set(opts, arg, str + offset + 1, errp);
2309 static void user_register_global_props(void)
2311 qemu_opts_foreach(qemu_find_opts("global"),
2312 global_init_func, NULL, NULL);
2315 static int do_configure_icount(void *opaque, QemuOpts *opts, Error **errp)
2317 icount_configure(opts, errp);
2318 return 0;
2321 static int accelerator_set_property(void *opaque,
2322 const char *name, const char *value,
2323 Error **errp)
2325 return object_parse_property_opt(opaque, name, value, "accel", errp);
2328 static int do_configure_accelerator(void *opaque, QemuOpts *opts, Error **errp)
2330 bool *p_init_failed = opaque;
2331 const char *acc = qemu_opt_get(opts, "accel");
2332 AccelClass *ac = accel_find(acc);
2333 AccelState *accel;
2334 int ret;
2335 bool qtest_with_kvm;
2337 qtest_with_kvm = g_str_equal(acc, "kvm") && qtest_chrdev != NULL;
2339 if (!ac) {
2340 *p_init_failed = true;
2341 if (!qtest_with_kvm) {
2342 error_report("invalid accelerator %s", acc);
2344 return 0;
2346 accel = ACCEL(object_new_with_class(OBJECT_CLASS(ac)));
2347 object_apply_compat_props(OBJECT(accel));
2348 qemu_opt_foreach(opts, accelerator_set_property,
2349 accel,
2350 &error_fatal);
2352 ret = accel_init_machine(accel, current_machine);
2353 if (ret < 0) {
2354 *p_init_failed = true;
2355 if (!qtest_with_kvm || ret != -ENOENT) {
2356 error_report("failed to initialize %s: %s", acc, strerror(-ret));
2358 return 0;
2361 return 1;
2364 static void configure_accelerators(const char *progname)
2366 bool init_failed = false;
2368 qemu_opts_foreach(qemu_find_opts("icount"),
2369 do_configure_icount, NULL, &error_fatal);
2371 if (QTAILQ_EMPTY(&qemu_accel_opts.head)) {
2372 char **accel_list, **tmp;
2374 if (accelerators == NULL) {
2375 /* Select the default accelerator */
2376 bool have_tcg = accel_find("tcg");
2377 bool have_kvm = accel_find("kvm");
2379 if (have_tcg && have_kvm) {
2380 if (g_str_has_suffix(progname, "kvm")) {
2381 /* If the program name ends with "kvm", we prefer KVM */
2382 accelerators = "kvm:tcg";
2383 } else {
2384 accelerators = "tcg:kvm";
2386 } else if (have_kvm) {
2387 accelerators = "kvm";
2388 } else if (have_tcg) {
2389 accelerators = "tcg";
2390 } else {
2391 error_report("No accelerator selected and"
2392 " no default accelerator available");
2393 exit(1);
2396 accel_list = g_strsplit(accelerators, ":", 0);
2398 for (tmp = accel_list; *tmp; tmp++) {
2400 * Filter invalid accelerators here, to prevent obscenities
2401 * such as "-machine accel=tcg,,thread=single".
2403 if (accel_find(*tmp)) {
2404 qemu_opts_parse_noisily(qemu_find_opts("accel"), *tmp, true);
2405 } else {
2406 init_failed = true;
2407 error_report("invalid accelerator %s", *tmp);
2410 g_strfreev(accel_list);
2411 } else {
2412 if (accelerators != NULL) {
2413 error_report("The -accel and \"-machine accel=\" options are incompatible");
2414 exit(1);
2418 if (!qemu_opts_foreach(qemu_find_opts("accel"),
2419 do_configure_accelerator, &init_failed, &error_fatal)) {
2420 if (!init_failed) {
2421 error_report("no accelerator found");
2423 exit(1);
2426 if (init_failed && !qtest_chrdev) {
2427 AccelClass *ac = ACCEL_GET_CLASS(current_accel());
2428 error_report("falling back to %s", ac->name);
2431 if (icount_enabled() && !tcg_enabled()) {
2432 error_report("-icount is not allowed with hardware virtualization");
2433 exit(1);
2437 static void create_default_memdev(MachineState *ms, const char *path)
2439 Object *obj;
2440 MachineClass *mc = MACHINE_GET_CLASS(ms);
2442 obj = object_new(path ? TYPE_MEMORY_BACKEND_FILE : TYPE_MEMORY_BACKEND_RAM);
2443 if (path) {
2444 object_property_set_str(obj, "mem-path", path, &error_fatal);
2446 object_property_set_int(obj, "size", ms->ram_size, &error_fatal);
2447 object_property_add_child(object_get_objects_root(), mc->default_ram_id,
2448 obj);
2449 /* Ensure backend's memory region name is equal to mc->default_ram_id */
2450 object_property_set_bool(obj, "x-use-canonical-path-for-ramblock-id",
2451 false, &error_fatal);
2452 user_creatable_complete(USER_CREATABLE(obj), &error_fatal);
2453 object_unref(obj);
2454 object_property_set_str(OBJECT(ms), "memory-backend", mc->default_ram_id,
2455 &error_fatal);
2458 static void qemu_validate_options(const QDict *machine_opts)
2460 const char *kernel_filename = qdict_get_try_str(machine_opts, "kernel");
2461 const char *initrd_filename = qdict_get_try_str(machine_opts, "initrd");
2462 const char *kernel_cmdline = qdict_get_try_str(machine_opts, "append");
2464 if (kernel_filename == NULL) {
2465 if (kernel_cmdline != NULL) {
2466 error_report("-append only allowed with -kernel option");
2467 exit(1);
2470 if (initrd_filename != NULL) {
2471 error_report("-initrd only allowed with -kernel option");
2472 exit(1);
2476 if (loadvm && preconfig_requested) {
2477 error_report("'preconfig' and 'loadvm' options are "
2478 "mutually exclusive");
2479 exit(EXIT_FAILURE);
2481 if (incoming && preconfig_requested && strcmp(incoming, "defer") != 0) {
2482 error_report("'preconfig' supports '-incoming defer' only");
2483 exit(EXIT_FAILURE);
2486 #ifdef CONFIG_CURSES
2487 if (is_daemonized() && dpy.type == DISPLAY_TYPE_CURSES) {
2488 error_report("curses display cannot be used with -daemonize");
2489 exit(1);
2491 #endif
2494 static void qemu_process_sugar_options(void)
2496 if (mem_prealloc) {
2497 QObject *smp = qdict_get(machine_opts_dict, "smp");
2498 if (smp && qobject_type(smp) == QTYPE_QDICT) {
2499 QObject *cpus = qdict_get(qobject_to(QDict, smp), "cpus");
2500 if (cpus && qobject_type(cpus) == QTYPE_QSTRING) {
2501 const char *val = qstring_get_str(qobject_to(QString, cpus));
2502 object_register_sugar_prop("memory-backend", "prealloc-threads",
2503 val, false);
2506 object_register_sugar_prop("memory-backend", "prealloc", "on", false);
2509 if (watchdog) {
2510 int i = select_watchdog(watchdog);
2511 if (i > 0)
2512 exit (i == 1 ? 1 : 0);
2516 /* -action processing */
2519 * Process all the -action parameters parsed from cmdline.
2521 static int process_runstate_actions(void *opaque, QemuOpts *opts, Error **errp)
2523 Error *local_err = NULL;
2524 QDict *qdict = qemu_opts_to_qdict(opts, NULL);
2525 QObject *ret = NULL;
2526 qmp_marshal_set_action(qdict, &ret, &local_err);
2527 qobject_unref(ret);
2528 qobject_unref(qdict);
2529 if (local_err) {
2530 error_propagate(errp, local_err);
2531 return 1;
2533 return 0;
2536 static void qemu_process_early_options(void)
2538 #ifdef CONFIG_SECCOMP
2539 QemuOptsList *olist = qemu_find_opts_err("sandbox", NULL);
2540 if (olist) {
2541 qemu_opts_foreach(olist, parse_sandbox, NULL, &error_fatal);
2543 #endif
2545 qemu_opts_foreach(qemu_find_opts("name"),
2546 parse_name, NULL, &error_fatal);
2548 if (qemu_opts_foreach(qemu_find_opts("action"),
2549 process_runstate_actions, NULL, &error_fatal)) {
2550 exit(1);
2553 #ifndef _WIN32
2554 qemu_opts_foreach(qemu_find_opts("add-fd"),
2555 parse_add_fd, NULL, &error_fatal);
2557 qemu_opts_foreach(qemu_find_opts("add-fd"),
2558 cleanup_add_fd, NULL, &error_fatal);
2559 #endif
2561 /* Open the logfile at this point and set the log mask if necessary. */
2562 if (log_file) {
2563 qemu_set_log_filename(log_file, &error_fatal);
2565 if (log_mask) {
2566 int mask;
2567 mask = qemu_str_to_log_mask(log_mask);
2568 if (!mask) {
2569 qemu_print_log_usage(stdout);
2570 exit(1);
2572 qemu_set_log(mask);
2573 } else {
2574 qemu_set_log(0);
2577 qemu_add_default_firmwarepath();
2580 static void qemu_process_help_options(void)
2583 * Check for -cpu help and -device help before we call select_machine(),
2584 * which will return an error if the architecture has no default machine
2585 * type and the user did not specify one, so that the user doesn't need
2586 * to say '-cpu help -machine something'.
2588 if (cpu_option && is_help_option(cpu_option)) {
2589 list_cpus(cpu_option);
2590 exit(0);
2593 if (qemu_opts_foreach(qemu_find_opts("device"),
2594 device_help_func, NULL, NULL)) {
2595 exit(0);
2598 /* -L help lists the data directories and exits. */
2599 if (list_data_dirs) {
2600 qemu_list_data_dirs();
2601 exit(0);
2605 static void qemu_maybe_daemonize(const char *pid_file)
2607 Error *err = NULL;
2609 os_daemonize();
2610 rcu_disable_atfork();
2612 if (pid_file && !qemu_write_pidfile(pid_file, &err)) {
2613 error_reportf_err(err, "cannot create PID file: ");
2614 exit(1);
2617 qemu_unlink_pidfile_notifier.notify = qemu_unlink_pidfile;
2618 qemu_add_exit_notifier(&qemu_unlink_pidfile_notifier);
2621 static void qemu_init_displays(void)
2623 DisplayState *ds;
2625 /* init local displays */
2626 ds = init_displaystate();
2627 qemu_display_init(ds, &dpy);
2629 /* must be after terminal init, SDL library changes signal handlers */
2630 os_setup_signal_handling();
2632 /* init remote displays */
2633 #ifdef CONFIG_VNC
2634 qemu_opts_foreach(qemu_find_opts("vnc"),
2635 vnc_init_func, NULL, &error_fatal);
2636 #endif
2638 if (using_spice) {
2639 qemu_spice.display_init();
2643 static void qemu_init_board(void)
2645 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
2647 if (machine_class->default_ram_id && current_machine->ram_size &&
2648 numa_uses_legacy_mem() && !current_machine->ram_memdev_id) {
2649 create_default_memdev(current_machine, mem_path);
2652 /* process plugin before CPUs are created, but once -smp has been parsed */
2653 qemu_plugin_load_list(&plugin_list, &error_fatal);
2655 /* From here on we enter MACHINE_PHASE_INITIALIZED. */
2656 machine_run_board_init(current_machine);
2658 drive_check_orphaned();
2660 realtime_init();
2662 if (hax_enabled()) {
2663 /* FIXME: why isn't cpu_synchronize_all_post_init enough? */
2664 hax_sync_vcpus();
2668 static void qemu_create_cli_devices(void)
2670 DeviceOption *opt;
2672 soundhw_init();
2674 qemu_opts_foreach(qemu_find_opts("fw_cfg"),
2675 parse_fw_cfg, fw_cfg_find(), &error_fatal);
2677 /* init USB devices */
2678 if (machine_usb(current_machine)) {
2679 if (foreach_device_config(DEV_USB, usb_parse) < 0)
2680 exit(1);
2683 /* init generic devices */
2684 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
2685 qemu_opts_foreach(qemu_find_opts("device"),
2686 device_init_func, NULL, &error_fatal);
2687 QTAILQ_FOREACH(opt, &device_opts, next) {
2688 DeviceState *dev;
2689 loc_push_restore(&opt->loc);
2691 * TODO Eventually we should call qmp_device_add() here to make sure it
2692 * behaves the same, but QMP still has to accept incorrectly typed
2693 * options until libvirt is fixed and we want to be strict on the CLI
2694 * from the start, so call qdev_device_add_from_qdict() directly for
2695 * now.
2697 dev = qdev_device_add_from_qdict(opt->opts, true, &error_fatal);
2698 object_unref(OBJECT(dev));
2699 loc_pop(&opt->loc);
2701 rom_reset_order_override();
2704 static void qemu_machine_creation_done(void)
2706 MachineState *machine = MACHINE(qdev_get_machine());
2708 /* Did we create any drives that we failed to create a device for? */
2709 drive_check_orphaned();
2711 /* Don't warn about the default network setup that you get if
2712 * no command line -net or -netdev options are specified. There
2713 * are two cases that we would otherwise complain about:
2714 * (1) board doesn't support a NIC but the implicit "-net nic"
2715 * requested one
2716 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
2717 * sets up a nic that isn't connected to anything.
2719 if (!default_net && (!qtest_enabled() || has_defaults)) {
2720 net_check_clients();
2723 qdev_prop_check_globals();
2725 qdev_machine_creation_done();
2727 if (machine->cgs) {
2729 * Verify that Confidential Guest Support has actually been initialized
2731 assert(machine->cgs->ready);
2734 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
2735 exit(1);
2739 void qmp_x_exit_preconfig(Error **errp)
2741 if (phase_check(PHASE_MACHINE_INITIALIZED)) {
2742 error_setg(errp, "The command is permitted only before machine initialization");
2743 return;
2746 qemu_init_board();
2747 qemu_create_cli_devices();
2748 qemu_machine_creation_done();
2750 if (loadvm) {
2751 load_snapshot(loadvm, NULL, false, NULL, &error_fatal);
2753 if (replay_mode != REPLAY_MODE_NONE) {
2754 replay_vmstate_init();
2757 if (incoming) {
2758 Error *local_err = NULL;
2759 if (strcmp(incoming, "defer") != 0) {
2760 qmp_migrate_incoming(incoming, &local_err);
2761 if (local_err) {
2762 error_reportf_err(local_err, "-incoming %s: ", incoming);
2763 exit(1);
2766 } else if (autostart) {
2767 qmp_cont(NULL);
2771 void qemu_init(int argc, char **argv, char **envp)
2773 QemuOpts *opts;
2774 QemuOpts *icount_opts = NULL, *accel_opts = NULL;
2775 QemuOptsList *olist;
2776 int optind;
2777 const char *optarg;
2778 MachineClass *machine_class;
2779 bool userconfig = true;
2780 FILE *vmstate_dump_file = NULL;
2782 qemu_add_opts(&qemu_drive_opts);
2783 qemu_add_drive_opts(&qemu_legacy_drive_opts);
2784 qemu_add_drive_opts(&qemu_common_drive_opts);
2785 qemu_add_drive_opts(&qemu_drive_opts);
2786 qemu_add_drive_opts(&bdrv_runtime_opts);
2787 qemu_add_opts(&qemu_chardev_opts);
2788 qemu_add_opts(&qemu_device_opts);
2789 qemu_add_opts(&qemu_netdev_opts);
2790 qemu_add_opts(&qemu_nic_opts);
2791 qemu_add_opts(&qemu_net_opts);
2792 qemu_add_opts(&qemu_rtc_opts);
2793 qemu_add_opts(&qemu_global_opts);
2794 qemu_add_opts(&qemu_mon_opts);
2795 qemu_add_opts(&qemu_trace_opts);
2796 qemu_plugin_add_opts();
2797 qemu_add_opts(&qemu_option_rom_opts);
2798 qemu_add_opts(&qemu_accel_opts);
2799 qemu_add_opts(&qemu_mem_opts);
2800 qemu_add_opts(&qemu_smp_opts);
2801 qemu_add_opts(&qemu_boot_opts);
2802 qemu_add_opts(&qemu_add_fd_opts);
2803 qemu_add_opts(&qemu_object_opts);
2804 qemu_add_opts(&qemu_tpmdev_opts);
2805 qemu_add_opts(&qemu_overcommit_opts);
2806 qemu_add_opts(&qemu_msg_opts);
2807 qemu_add_opts(&qemu_name_opts);
2808 qemu_add_opts(&qemu_numa_opts);
2809 qemu_add_opts(&qemu_icount_opts);
2810 qemu_add_opts(&qemu_semihosting_config_opts);
2811 qemu_add_opts(&qemu_fw_cfg_opts);
2812 qemu_add_opts(&qemu_action_opts);
2813 module_call_init(MODULE_INIT_OPTS);
2815 error_init(argv[0]);
2816 qemu_init_exec_dir(argv[0]);
2818 qemu_init_arch_modules();
2820 qemu_init_subsystems();
2822 /* first pass of option parsing */
2823 optind = 1;
2824 while (optind < argc) {
2825 if (argv[optind][0] != '-') {
2826 /* disk image */
2827 optind++;
2828 } else {
2829 const QEMUOption *popt;
2831 popt = lookup_opt(argc, argv, &optarg, &optind);
2832 switch (popt->index) {
2833 case QEMU_OPTION_nouserconfig:
2834 userconfig = false;
2835 break;
2840 machine_opts_dict = qdict_new();
2841 if (userconfig) {
2842 qemu_read_default_config_file(&error_fatal);
2845 /* second pass of option parsing */
2846 optind = 1;
2847 for(;;) {
2848 if (optind >= argc)
2849 break;
2850 if (argv[optind][0] != '-') {
2851 loc_set_cmdline(argv, optind, 1);
2852 drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2853 } else {
2854 const QEMUOption *popt;
2856 popt = lookup_opt(argc, argv, &optarg, &optind);
2857 if (!(popt->arch_mask & arch_type)) {
2858 error_report("Option not supported for this target");
2859 exit(1);
2861 switch(popt->index) {
2862 case QEMU_OPTION_cpu:
2863 /* hw initialization will check this */
2864 cpu_option = optarg;
2865 break;
2866 case QEMU_OPTION_hda:
2867 case QEMU_OPTION_hdb:
2868 case QEMU_OPTION_hdc:
2869 case QEMU_OPTION_hdd:
2870 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2871 HD_OPTS);
2872 break;
2873 case QEMU_OPTION_blockdev:
2875 Visitor *v;
2876 BlockdevOptionsQueueEntry *bdo;
2878 v = qobject_input_visitor_new_str(optarg, "driver",
2879 &error_fatal);
2881 bdo = g_new(BlockdevOptionsQueueEntry, 1);
2882 visit_type_BlockdevOptions(v, NULL, &bdo->bdo,
2883 &error_fatal);
2884 visit_free(v);
2885 loc_save(&bdo->loc);
2886 QSIMPLEQ_INSERT_TAIL(&bdo_queue, bdo, entry);
2887 break;
2889 case QEMU_OPTION_drive:
2890 opts = qemu_opts_parse_noisily(qemu_find_opts("drive"),
2891 optarg, false);
2892 if (opts == NULL) {
2893 exit(1);
2895 break;
2896 case QEMU_OPTION_set:
2897 qemu_set_option(optarg, &error_fatal);
2898 break;
2899 case QEMU_OPTION_global:
2900 if (qemu_global_option(optarg) != 0)
2901 exit(1);
2902 break;
2903 case QEMU_OPTION_mtdblock:
2904 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2905 break;
2906 case QEMU_OPTION_sd:
2907 drive_add(IF_SD, -1, optarg, SD_OPTS);
2908 break;
2909 case QEMU_OPTION_pflash:
2910 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2911 break;
2912 case QEMU_OPTION_snapshot:
2914 Error *blocker = NULL;
2915 snapshot = 1;
2916 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
2917 "-snapshot");
2918 replay_add_blocker(blocker);
2920 break;
2921 case QEMU_OPTION_numa:
2922 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
2923 optarg, true);
2924 if (!opts) {
2925 exit(1);
2927 break;
2928 case QEMU_OPTION_display:
2929 parse_display(optarg);
2930 break;
2931 case QEMU_OPTION_nographic:
2932 qdict_put_str(machine_opts_dict, "graphics", "off");
2933 nographic = true;
2934 dpy.type = DISPLAY_TYPE_NONE;
2935 break;
2936 case QEMU_OPTION_curses:
2937 warn_report("-curses is deprecated, "
2938 "use -display curses instead.");
2939 #ifdef CONFIG_CURSES
2940 dpy.type = DISPLAY_TYPE_CURSES;
2941 #else
2942 error_report("curses or iconv support is disabled");
2943 exit(1);
2944 #endif
2945 break;
2946 case QEMU_OPTION_portrait:
2947 graphic_rotate = 90;
2948 break;
2949 case QEMU_OPTION_rotate:
2950 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2951 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2952 graphic_rotate != 180 && graphic_rotate != 270) {
2953 error_report("only 90, 180, 270 deg rotation is available");
2954 exit(1);
2956 break;
2957 case QEMU_OPTION_kernel:
2958 qdict_put_str(machine_opts_dict, "kernel", optarg);
2959 break;
2960 case QEMU_OPTION_initrd:
2961 qdict_put_str(machine_opts_dict, "initrd", optarg);
2962 break;
2963 case QEMU_OPTION_append:
2964 qdict_put_str(machine_opts_dict, "append", optarg);
2965 break;
2966 case QEMU_OPTION_dtb:
2967 qdict_put_str(machine_opts_dict, "dtb", optarg);
2968 break;
2969 case QEMU_OPTION_cdrom:
2970 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
2971 break;
2972 case QEMU_OPTION_boot:
2973 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
2974 optarg, true);
2975 if (!opts) {
2976 exit(1);
2978 break;
2979 case QEMU_OPTION_fda:
2980 case QEMU_OPTION_fdb:
2981 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2982 optarg, FD_OPTS);
2983 break;
2984 case QEMU_OPTION_no_fd_bootchk:
2985 fd_bootchk = 0;
2986 break;
2987 case QEMU_OPTION_netdev:
2988 default_net = 0;
2989 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2990 exit(1);
2992 break;
2993 case QEMU_OPTION_nic:
2994 default_net = 0;
2995 if (net_client_parse(qemu_find_opts("nic"), optarg) == -1) {
2996 exit(1);
2998 break;
2999 case QEMU_OPTION_net:
3000 default_net = 0;
3001 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3002 exit(1);
3004 break;
3005 #ifdef CONFIG_LIBISCSI
3006 case QEMU_OPTION_iscsi:
3007 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
3008 optarg, false);
3009 if (!opts) {
3010 exit(1);
3012 break;
3013 #endif
3014 case QEMU_OPTION_audio_help:
3015 audio_legacy_help();
3016 exit (0);
3017 break;
3018 case QEMU_OPTION_audiodev:
3019 audio_parse_option(optarg);
3020 break;
3021 case QEMU_OPTION_soundhw:
3022 select_soundhw (optarg);
3023 break;
3024 case QEMU_OPTION_h:
3025 help(0);
3026 break;
3027 case QEMU_OPTION_version:
3028 version();
3029 exit(0);
3030 break;
3031 case QEMU_OPTION_m:
3032 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
3033 optarg, true);
3034 if (!opts) {
3035 exit(EXIT_FAILURE);
3037 break;
3038 #ifdef CONFIG_TPM
3039 case QEMU_OPTION_tpmdev:
3040 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3041 exit(1);
3043 break;
3044 #endif
3045 case QEMU_OPTION_mempath:
3046 mem_path = optarg;
3047 break;
3048 case QEMU_OPTION_mem_prealloc:
3049 mem_prealloc = 1;
3050 break;
3051 case QEMU_OPTION_d:
3052 log_mask = optarg;
3053 break;
3054 case QEMU_OPTION_D:
3055 log_file = optarg;
3056 break;
3057 case QEMU_OPTION_DFILTER:
3058 qemu_set_dfilter_ranges(optarg, &error_fatal);
3059 break;
3060 case QEMU_OPTION_seed:
3061 qemu_guest_random_seed_main(optarg, &error_fatal);
3062 break;
3063 case QEMU_OPTION_s:
3064 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3065 break;
3066 case QEMU_OPTION_gdb:
3067 add_device_config(DEV_GDB, optarg);
3068 break;
3069 case QEMU_OPTION_L:
3070 if (is_help_option(optarg)) {
3071 list_data_dirs = true;
3072 } else {
3073 qemu_add_data_dir(g_strdup(optarg));
3075 break;
3076 case QEMU_OPTION_bios:
3077 qdict_put_str(machine_opts_dict, "firmware", optarg);
3078 break;
3079 case QEMU_OPTION_singlestep:
3080 singlestep = 1;
3081 break;
3082 case QEMU_OPTION_S:
3083 autostart = 0;
3084 break;
3085 case QEMU_OPTION_k:
3086 keyboard_layout = optarg;
3087 break;
3088 case QEMU_OPTION_vga:
3089 vga_model = optarg;
3090 default_vga = 0;
3091 break;
3092 case QEMU_OPTION_g:
3094 const char *p;
3095 int w, h, depth;
3096 p = optarg;
3097 w = strtol(p, (char **)&p, 10);
3098 if (w <= 0) {
3099 graphic_error:
3100 error_report("invalid resolution or depth");
3101 exit(1);
3103 if (*p != 'x')
3104 goto graphic_error;
3105 p++;
3106 h = strtol(p, (char **)&p, 10);
3107 if (h <= 0)
3108 goto graphic_error;
3109 if (*p == 'x') {
3110 p++;
3111 depth = strtol(p, (char **)&p, 10);
3112 if (depth != 1 && depth != 2 && depth != 4 &&
3113 depth != 8 && depth != 15 && depth != 16 &&
3114 depth != 24 && depth != 32)
3115 goto graphic_error;
3116 } else if (*p == '\0') {
3117 depth = graphic_depth;
3118 } else {
3119 goto graphic_error;
3122 graphic_width = w;
3123 graphic_height = h;
3124 graphic_depth = depth;
3126 break;
3127 case QEMU_OPTION_echr:
3129 char *r;
3130 term_escape_char = strtol(optarg, &r, 0);
3131 if (r == optarg)
3132 printf("Bad argument to echr\n");
3133 break;
3135 case QEMU_OPTION_monitor:
3136 default_monitor = 0;
3137 if (strncmp(optarg, "none", 4)) {
3138 monitor_parse(optarg, "readline", false);
3140 break;
3141 case QEMU_OPTION_qmp:
3142 monitor_parse(optarg, "control", false);
3143 default_monitor = 0;
3144 break;
3145 case QEMU_OPTION_qmp_pretty:
3146 monitor_parse(optarg, "control", true);
3147 default_monitor = 0;
3148 break;
3149 case QEMU_OPTION_mon:
3150 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3151 true);
3152 if (!opts) {
3153 exit(1);
3155 default_monitor = 0;
3156 break;
3157 case QEMU_OPTION_chardev:
3158 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3159 optarg, true);
3160 if (!opts) {
3161 exit(1);
3163 break;
3164 case QEMU_OPTION_fsdev:
3165 olist = qemu_find_opts("fsdev");
3166 if (!olist) {
3167 error_report("fsdev support is disabled");
3168 exit(1);
3170 opts = qemu_opts_parse_noisily(olist, optarg, true);
3171 if (!opts) {
3172 exit(1);
3174 break;
3175 case QEMU_OPTION_virtfs: {
3176 QemuOpts *fsdev;
3177 QemuOpts *device;
3178 const char *writeout, *sock_fd, *socket, *path, *security_model,
3179 *multidevs;
3181 olist = qemu_find_opts("virtfs");
3182 if (!olist) {
3183 error_report("virtfs support is disabled");
3184 exit(1);
3186 opts = qemu_opts_parse_noisily(olist, optarg, true);
3187 if (!opts) {
3188 exit(1);
3191 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3192 qemu_opt_get(opts, "mount_tag") == NULL) {
3193 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3194 exit(1);
3196 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3197 qemu_opts_id(opts) ?:
3198 qemu_opt_get(opts, "mount_tag"),
3199 1, NULL);
3200 if (!fsdev) {
3201 error_report("duplicate or invalid fsdev id: %s",
3202 qemu_opt_get(opts, "mount_tag"));
3203 exit(1);
3206 writeout = qemu_opt_get(opts, "writeout");
3207 if (writeout) {
3208 #ifdef CONFIG_SYNC_FILE_RANGE
3209 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3210 #else
3211 error_report("writeout=immediate not supported "
3212 "on this platform");
3213 exit(1);
3214 #endif
3216 qemu_opt_set(fsdev, "fsdriver",
3217 qemu_opt_get(opts, "fsdriver"), &error_abort);
3218 path = qemu_opt_get(opts, "path");
3219 if (path) {
3220 qemu_opt_set(fsdev, "path", path, &error_abort);
3222 security_model = qemu_opt_get(opts, "security_model");
3223 if (security_model) {
3224 qemu_opt_set(fsdev, "security_model", security_model,
3225 &error_abort);
3227 socket = qemu_opt_get(opts, "socket");
3228 if (socket) {
3229 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3231 sock_fd = qemu_opt_get(opts, "sock_fd");
3232 if (sock_fd) {
3233 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3236 qemu_opt_set_bool(fsdev, "readonly",
3237 qemu_opt_get_bool(opts, "readonly", 0),
3238 &error_abort);
3239 multidevs = qemu_opt_get(opts, "multidevs");
3240 if (multidevs) {
3241 qemu_opt_set(fsdev, "multidevs", multidevs, &error_abort);
3243 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3244 &error_abort);
3245 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3246 qemu_opt_set(device, "fsdev",
3247 qemu_opts_id(fsdev), &error_abort);
3248 qemu_opt_set(device, "mount_tag",
3249 qemu_opt_get(opts, "mount_tag"), &error_abort);
3250 break;
3252 case QEMU_OPTION_serial:
3253 add_device_config(DEV_SERIAL, optarg);
3254 default_serial = 0;
3255 if (strncmp(optarg, "mon:", 4) == 0) {
3256 default_monitor = 0;
3258 break;
3259 case QEMU_OPTION_watchdog:
3260 if (watchdog) {
3261 error_report("only one watchdog option may be given");
3262 exit(1);
3264 warn_report("-watchdog is deprecated; use -device instead.");
3265 watchdog = optarg;
3266 break;
3267 case QEMU_OPTION_action:
3268 olist = qemu_find_opts("action");
3269 if (!qemu_opts_parse_noisily(olist, optarg, false)) {
3270 exit(1);
3272 break;
3273 case QEMU_OPTION_watchdog_action: {
3274 QemuOpts *opts;
3275 opts = qemu_opts_create(qemu_find_opts("action"), NULL, 0, &error_abort);
3276 qemu_opt_set(opts, "watchdog", optarg, &error_abort);
3277 break;
3279 case QEMU_OPTION_parallel:
3280 add_device_config(DEV_PARALLEL, optarg);
3281 default_parallel = 0;
3282 if (strncmp(optarg, "mon:", 4) == 0) {
3283 default_monitor = 0;
3285 break;
3286 case QEMU_OPTION_debugcon:
3287 add_device_config(DEV_DEBUGCON, optarg);
3288 break;
3289 case QEMU_OPTION_loadvm:
3290 loadvm = optarg;
3291 break;
3292 case QEMU_OPTION_full_screen:
3293 dpy.has_full_screen = true;
3294 dpy.full_screen = true;
3295 break;
3296 case QEMU_OPTION_alt_grab:
3297 alt_grab = 1;
3298 warn_report("-alt-grab is deprecated, please use "
3299 "-display sdl,grab-mod=lshift-lctrl-lalt instead.");
3300 break;
3301 case QEMU_OPTION_ctrl_grab:
3302 ctrl_grab = 1;
3303 warn_report("-ctrl-grab is deprecated, please use "
3304 "-display sdl,grab-mod=rctrl instead.");
3305 break;
3306 case QEMU_OPTION_sdl:
3307 warn_report("-sdl is deprecated, use -display sdl instead.");
3308 #ifdef CONFIG_SDL
3309 dpy.type = DISPLAY_TYPE_SDL;
3310 break;
3311 #else
3312 error_report("SDL support is disabled");
3313 exit(1);
3314 #endif
3315 case QEMU_OPTION_pidfile:
3316 pid_file = optarg;
3317 break;
3318 case QEMU_OPTION_win2k_hack:
3319 win2k_install_hack = 1;
3320 break;
3321 case QEMU_OPTION_acpitable:
3322 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3323 optarg, true);
3324 if (!opts) {
3325 exit(1);
3327 acpi_table_add(opts, &error_fatal);
3328 break;
3329 case QEMU_OPTION_smbios:
3330 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3331 optarg, false);
3332 if (!opts) {
3333 exit(1);
3335 smbios_entry_add(opts, &error_fatal);
3336 break;
3337 case QEMU_OPTION_fwcfg:
3338 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3339 optarg, true);
3340 if (opts == NULL) {
3341 exit(1);
3343 break;
3344 case QEMU_OPTION_preconfig:
3345 preconfig_requested = true;
3346 break;
3347 case QEMU_OPTION_enable_kvm:
3348 qdict_put_str(machine_opts_dict, "accel", "kvm");
3349 break;
3350 case QEMU_OPTION_M:
3351 case QEMU_OPTION_machine:
3353 bool help;
3355 keyval_parse_into(machine_opts_dict, optarg, "type", &help, &error_fatal);
3356 if (help) {
3357 machine_help_func(machine_opts_dict);
3358 exit(EXIT_SUCCESS);
3360 break;
3362 case QEMU_OPTION_accel:
3363 accel_opts = qemu_opts_parse_noisily(qemu_find_opts("accel"),
3364 optarg, true);
3365 optarg = qemu_opt_get(accel_opts, "accel");
3366 if (!optarg || is_help_option(optarg)) {
3367 printf("Accelerators supported in QEMU binary:\n");
3368 GSList *el, *accel_list = object_class_get_list(TYPE_ACCEL,
3369 false);
3370 for (el = accel_list; el; el = el->next) {
3371 gchar *typename = g_strdup(object_class_get_name(
3372 OBJECT_CLASS(el->data)));
3373 /* omit qtest which is used for tests only */
3374 if (g_strcmp0(typename, ACCEL_CLASS_NAME("qtest")) &&
3375 g_str_has_suffix(typename, ACCEL_CLASS_SUFFIX)) {
3376 gchar **optname = g_strsplit(typename,
3377 ACCEL_CLASS_SUFFIX, 0);
3378 printf("%s\n", optname[0]);
3379 g_strfreev(optname);
3381 g_free(typename);
3383 g_slist_free(accel_list);
3384 exit(0);
3386 break;
3387 case QEMU_OPTION_usb:
3388 qdict_put_str(machine_opts_dict, "usb", "on");
3389 break;
3390 case QEMU_OPTION_usbdevice:
3391 qdict_put_str(machine_opts_dict, "usb", "on");
3392 add_device_config(DEV_USB, optarg);
3393 break;
3394 case QEMU_OPTION_device:
3395 if (optarg[0] == '{') {
3396 QObject *obj = qobject_from_json(optarg, &error_fatal);
3397 DeviceOption *opt = g_new0(DeviceOption, 1);
3398 opt->opts = qobject_to(QDict, obj);
3399 loc_save(&opt->loc);
3400 assert(opt->opts != NULL);
3401 QTAILQ_INSERT_TAIL(&device_opts, opt, next);
3402 } else {
3403 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3404 optarg, true)) {
3405 exit(1);
3408 break;
3409 case QEMU_OPTION_smp:
3410 machine_parse_property_opt(qemu_find_opts("smp-opts"),
3411 "smp", optarg);
3412 break;
3413 case QEMU_OPTION_vnc:
3414 vnc_parse(optarg);
3415 break;
3416 case QEMU_OPTION_no_acpi:
3417 qdict_put_str(machine_opts_dict, "acpi", "off");
3418 break;
3419 case QEMU_OPTION_no_hpet:
3420 qdict_put_str(machine_opts_dict, "hpet", "off");
3421 break;
3422 case QEMU_OPTION_no_reboot:
3423 olist = qemu_find_opts("action");
3424 qemu_opts_parse_noisily(olist, "reboot=shutdown", false);
3425 break;
3426 case QEMU_OPTION_no_shutdown:
3427 olist = qemu_find_opts("action");
3428 qemu_opts_parse_noisily(olist, "shutdown=pause", false);
3429 break;
3430 case QEMU_OPTION_uuid:
3431 if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
3432 error_report("failed to parse UUID string: wrong format");
3433 exit(1);
3435 qemu_uuid_set = true;
3436 break;
3437 case QEMU_OPTION_option_rom:
3438 if (nb_option_roms >= MAX_OPTION_ROMS) {
3439 error_report("too many option ROMs");
3440 exit(1);
3442 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3443 optarg, true);
3444 if (!opts) {
3445 exit(1);
3447 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3448 option_rom[nb_option_roms].bootindex =
3449 qemu_opt_get_number(opts, "bootindex", -1);
3450 if (!option_rom[nb_option_roms].name) {
3451 error_report("Option ROM file is not specified");
3452 exit(1);
3454 nb_option_roms++;
3455 break;
3456 case QEMU_OPTION_semihosting:
3457 qemu_semihosting_enable();
3458 break;
3459 case QEMU_OPTION_semihosting_config:
3460 if (qemu_semihosting_config_options(optarg) != 0) {
3461 exit(1);
3463 break;
3464 case QEMU_OPTION_name:
3465 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3466 optarg, true);
3467 if (!opts) {
3468 exit(1);
3470 /* Capture guest name if -msg guest-name is used later */
3471 error_guest_name = qemu_opt_get(opts, "guest");
3472 break;
3473 case QEMU_OPTION_prom_env:
3474 if (nb_prom_envs >= MAX_PROM_ENVS) {
3475 error_report("too many prom variables");
3476 exit(1);
3478 prom_envs[nb_prom_envs] = optarg;
3479 nb_prom_envs++;
3480 break;
3481 case QEMU_OPTION_old_param:
3482 old_param = 1;
3483 break;
3484 case QEMU_OPTION_rtc:
3485 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3486 false);
3487 if (!opts) {
3488 exit(1);
3490 break;
3491 case QEMU_OPTION_icount:
3492 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3493 optarg, true);
3494 if (!icount_opts) {
3495 exit(1);
3497 break;
3498 case QEMU_OPTION_incoming:
3499 if (!incoming) {
3500 runstate_set(RUN_STATE_INMIGRATE);
3502 incoming = optarg;
3503 break;
3504 case QEMU_OPTION_only_migratable:
3505 only_migratable = 1;
3506 break;
3507 case QEMU_OPTION_nodefaults:
3508 has_defaults = 0;
3509 break;
3510 case QEMU_OPTION_xen_domid:
3511 if (!(accel_find("xen"))) {
3512 error_report("Option not supported for this target");
3513 exit(1);
3515 xen_domid = atoi(optarg);
3516 break;
3517 case QEMU_OPTION_xen_attach:
3518 if (!(accel_find("xen"))) {
3519 error_report("Option not supported for this target");
3520 exit(1);
3522 xen_mode = XEN_ATTACH;
3523 break;
3524 case QEMU_OPTION_xen_domid_restrict:
3525 if (!(accel_find("xen"))) {
3526 error_report("Option not supported for this target");
3527 exit(1);
3529 xen_domid_restrict = true;
3530 break;
3531 case QEMU_OPTION_trace:
3532 trace_opt_parse(optarg);
3533 break;
3534 case QEMU_OPTION_plugin:
3535 qemu_plugin_opt_parse(optarg, &plugin_list);
3536 break;
3537 case QEMU_OPTION_readconfig:
3538 qemu_read_config_file(optarg, qemu_parse_config_group, &error_fatal);
3539 break;
3540 case QEMU_OPTION_spice:
3541 olist = qemu_find_opts_err("spice", NULL);
3542 if (!olist) {
3543 error_report("spice support is disabled");
3544 exit(1);
3546 opts = qemu_opts_parse_noisily(olist, optarg, false);
3547 if (!opts) {
3548 exit(1);
3550 display_remote++;
3551 break;
3552 case QEMU_OPTION_writeconfig:
3554 FILE *fp;
3555 warn_report("-writeconfig is deprecated and will go away without a replacement");
3556 if (strcmp(optarg, "-") == 0) {
3557 fp = stdout;
3558 } else {
3559 fp = fopen(optarg, "w");
3560 if (fp == NULL) {
3561 error_report("open %s: %s", optarg,
3562 strerror(errno));
3563 exit(1);
3566 qemu_config_write(fp);
3567 if (fp != stdout) {
3568 fclose(fp);
3570 break;
3572 case QEMU_OPTION_qtest:
3573 qtest_chrdev = optarg;
3574 break;
3575 case QEMU_OPTION_qtest_log:
3576 qtest_log = optarg;
3577 break;
3578 case QEMU_OPTION_sandbox:
3579 olist = qemu_find_opts("sandbox");
3580 if (!olist) {
3581 #ifndef CONFIG_SECCOMP
3582 error_report("-sandbox support is not enabled "
3583 "in this QEMU binary");
3584 #endif
3585 exit(1);
3588 opts = qemu_opts_parse_noisily(olist, optarg, true);
3589 if (!opts) {
3590 exit(1);
3592 break;
3593 case QEMU_OPTION_add_fd:
3594 #ifndef _WIN32
3595 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
3596 optarg, false);
3597 if (!opts) {
3598 exit(1);
3600 #else
3601 error_report("File descriptor passing is disabled on this "
3602 "platform");
3603 exit(1);
3604 #endif
3605 break;
3606 case QEMU_OPTION_object:
3607 object_option_parse(optarg);
3608 break;
3609 case QEMU_OPTION_overcommit:
3610 opts = qemu_opts_parse_noisily(qemu_find_opts("overcommit"),
3611 optarg, false);
3612 if (!opts) {
3613 exit(1);
3615 enable_mlock = qemu_opt_get_bool(opts, "mem-lock", false);
3616 enable_cpu_pm = qemu_opt_get_bool(opts, "cpu-pm", false);
3617 break;
3618 case QEMU_OPTION_compat:
3620 CompatPolicy *opts;
3621 Visitor *v;
3623 v = qobject_input_visitor_new_str(optarg, NULL,
3624 &error_fatal);
3626 visit_type_CompatPolicy(v, NULL, &opts, &error_fatal);
3627 QAPI_CLONE_MEMBERS(CompatPolicy, &compat_policy, opts);
3629 qapi_free_CompatPolicy(opts);
3630 visit_free(v);
3631 break;
3633 case QEMU_OPTION_msg:
3634 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
3635 false);
3636 if (!opts) {
3637 exit(1);
3639 configure_msg(opts);
3640 break;
3641 case QEMU_OPTION_dump_vmstate:
3642 if (vmstate_dump_file) {
3643 error_report("only one '-dump-vmstate' "
3644 "option may be given");
3645 exit(1);
3647 vmstate_dump_file = fopen(optarg, "w");
3648 if (vmstate_dump_file == NULL) {
3649 error_report("open %s: %s", optarg, strerror(errno));
3650 exit(1);
3652 break;
3653 case QEMU_OPTION_enable_sync_profile:
3654 qsp_enable();
3655 break;
3656 case QEMU_OPTION_nouserconfig:
3657 /* Nothing to be parsed here. Especially, do not error out below. */
3658 break;
3659 default:
3660 if (os_parse_cmd_args(popt->index, optarg)) {
3661 error_report("Option not supported in this build");
3662 exit(1);
3668 * Clear error location left behind by the loop.
3669 * Best done right after the loop. Do not insert code here!
3671 loc_set_none();
3673 qemu_validate_options(machine_opts_dict);
3674 qemu_process_sugar_options();
3677 * These options affect everything else and should be processed
3678 * before daemonizing.
3680 qemu_process_early_options();
3682 qemu_process_help_options();
3683 qemu_maybe_daemonize(pid_file);
3686 * The trace backend must be initialized after daemonizing.
3687 * trace_init_backends() will call st_init(), which will create the
3688 * trace thread in the parent, and also register st_flush_trace_buffer()
3689 * in atexit(). This function will force the parent to wait for the
3690 * writeout thread to finish, which will not occur, and the parent
3691 * process will be left in the host.
3693 if (!trace_init_backends()) {
3694 exit(1);
3696 trace_init_file();
3698 qemu_init_main_loop(&error_fatal);
3699 cpu_timers_init();
3701 user_register_global_props();
3702 replay_configure(icount_opts);
3704 configure_rtc(qemu_find_opts_singleton("rtc"));
3706 qemu_create_machine(machine_opts_dict);
3708 suspend_mux_open();
3710 qemu_disable_default_devices();
3711 qemu_create_default_devices();
3712 qemu_create_early_backends();
3714 qemu_apply_legacy_machine_options(machine_opts_dict);
3715 qemu_apply_machine_options(machine_opts_dict);
3716 qobject_unref(machine_opts_dict);
3717 phase_advance(PHASE_MACHINE_CREATED);
3720 * Note: uses machine properties such as kernel-irqchip, must run
3721 * after qemu_apply_machine_options.
3723 configure_accelerators(argv[0]);
3724 phase_advance(PHASE_ACCEL_CREATED);
3727 * Beware, QOM objects created before this point miss global and
3728 * compat properties.
3730 * Global properties get set up by qdev_prop_register_global(),
3731 * called from user_register_global_props(), and certain option
3732 * desugaring. Also in CPU feature desugaring (buried in
3733 * parse_cpu_option()), which happens below this point, but may
3734 * only target the CPU type, which can only be created after
3735 * parse_cpu_option() returned the type.
3737 * Machine compat properties: object_set_machine_compat_props().
3738 * Accelerator compat props: object_set_accelerator_compat_props(),
3739 * called from do_configure_accelerator().
3742 machine_class = MACHINE_GET_CLASS(current_machine);
3743 if (!qtest_enabled() && machine_class->deprecation_reason) {
3744 error_report("Machine type '%s' is deprecated: %s",
3745 machine_class->name, machine_class->deprecation_reason);
3749 * Note: creates a QOM object, must run only after global and
3750 * compat properties have been set up.
3752 migration_object_init();
3754 qemu_create_late_backends();
3756 /* parse features once if machine provides default cpu_type */
3757 current_machine->cpu_type = machine_class->default_cpu_type;
3758 if (cpu_option) {
3759 current_machine->cpu_type = parse_cpu_option(cpu_option);
3761 /* NB: for machine none cpu_type could STILL be NULL here! */
3763 qemu_resolve_machine_memdev();
3764 parse_numa_opts(current_machine);
3766 if (vmstate_dump_file) {
3767 /* dump and exit */
3768 module_load_qom_all();
3769 dump_vmstate_json_to_file(vmstate_dump_file);
3770 exit(0);
3773 if (!preconfig_requested) {
3774 qmp_x_exit_preconfig(&error_fatal);
3776 qemu_init_displays();
3777 accel_setup_post(current_machine);
3778 os_setup_post();
3779 resume_mux_open();