nsis: Add qemu-edid and qemu-ga
[qemu/ar7.git] / vl.c
blobc6d159525b43df7d4f75cd6bb5eb1939b72400c6
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/units.h"
27 #include "qapi/error.h"
28 #include "qemu-version.h"
29 #include "qemu/cutils.h"
30 #include "qemu/help_option.h"
31 #include "qemu/uuid.h"
32 #include "sysemu/seccomp.h"
34 #ifdef CONFIG_SDL
35 #if defined(__APPLE__) || defined(main)
36 #define SDL_MAIN_HANDLED
37 #include <SDL.h>
38 #if SDL_MAJOR_VERSION == 1
39 int qemu_main(int argc, char **argv);
40 int main(int argc, char **argv)
42 return qemu_main(argc, argv);
44 #undef main
45 #define main qemu_main
46 #elif SDL_MAJOR_VERSION == 2
47 #undef main
48 #else
49 #error undefined SDL version
50 #endif
51 #endif
52 #endif /* CONFIG_SDL */
54 #ifdef CONFIG_COCOA
55 #undef main
56 #define main qemu_main
57 #endif /* CONFIG_COCOA */
60 #include "qemu/error-report.h"
61 #include "qemu/sockets.h"
62 #include "hw/hw.h"
63 #include "hw/boards.h"
64 #include "sysemu/accel.h"
65 #include "hw/usb.h"
66 #include "hw/isa/isa.h"
67 #include "hw/scsi/scsi.h"
68 #include "hw/display/vga.h"
69 #include "hw/bt.h"
70 #include "sysemu/watchdog.h"
71 #include "hw/smbios/smbios.h"
72 #include "hw/acpi/acpi.h"
73 #include "hw/xen/xen.h"
74 #include "hw/qdev.h"
75 #include "hw/loader.h"
76 #include "monitor/qdev.h"
77 #include "sysemu/bt.h"
78 #include "net/net.h"
79 #include "net/slirp.h"
80 #include "monitor/monitor.h"
81 #include "ui/console.h"
82 #include "ui/input.h"
83 #include "sysemu/sysemu.h"
84 #include "sysemu/numa.h"
85 #include "exec/gdbstub.h"
86 #include "qemu/timer.h"
87 #include "chardev/char.h"
88 #include "qemu/bitmap.h"
89 #include "qemu/log.h"
90 #include "sysemu/blockdev.h"
91 #include "hw/block/block.h"
92 #include "migration/misc.h"
93 #include "migration/snapshot.h"
94 #include "migration/global_state.h"
95 #include "sysemu/tpm.h"
96 #include "sysemu/dma.h"
97 #include "hw/audio/soundhw.h"
98 #include "audio/audio.h"
99 #include "sysemu/cpus.h"
100 #include "migration/colo.h"
101 #include "migration/postcopy-ram.h"
102 #include "sysemu/kvm.h"
103 #include "sysemu/hax.h"
104 #include "qapi/qobject-input-visitor.h"
105 #include "qemu/option.h"
106 #include "qemu/config-file.h"
107 #include "qemu-options.h"
108 #include "qemu/main-loop.h"
109 #ifdef CONFIG_VIRTFS
110 #include "fsdev/qemu-fsdev.h"
111 #endif
112 #include "sysemu/qtest.h"
114 #include "disas/disas.h"
117 #include "slirp/libslirp.h"
119 #include "trace-root.h"
120 #include "trace/control.h"
121 #include "qemu/queue.h"
122 #include "sysemu/arch_init.h"
124 #include "ui/qemu-spice.h"
125 #include "qapi/string-input-visitor.h"
126 #include "qapi/opts-visitor.h"
127 #include "qapi/clone-visitor.h"
128 #include "qom/object_interfaces.h"
129 #include "exec/semihost.h"
130 #include "crypto/init.h"
131 #include "sysemu/replay.h"
132 #include "qapi/qapi-events-run-state.h"
133 #include "qapi/qapi-visit-block-core.h"
134 #include "qapi/qapi-visit-ui.h"
135 #include "qapi/qapi-commands-block-core.h"
136 #include "qapi/qapi-commands-misc.h"
137 #include "qapi/qapi-commands-run-state.h"
138 #include "qapi/qmp/qerror.h"
139 #include "sysemu/iothread.h"
141 #define MAX_VIRTIO_CONSOLES 1
143 static const char *data_dir[16];
144 static int data_dir_idx;
145 const char *bios_name = NULL;
146 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
147 int display_opengl;
148 const char* keyboard_layout = NULL;
149 ram_addr_t ram_size;
150 const char *mem_path = NULL;
151 int mem_prealloc = 0; /* force preallocation of physical target memory */
152 bool enable_mlock = false;
153 bool enable_cpu_pm = false;
154 int nb_nics;
155 NICInfo nd_table[MAX_NICS];
156 int autostart;
157 static enum {
158 RTC_BASE_UTC,
159 RTC_BASE_LOCALTIME,
160 RTC_BASE_DATETIME,
161 } rtc_base_type = RTC_BASE_UTC;
162 static time_t rtc_ref_start_datetime;
163 static int rtc_realtime_clock_offset; /* used only with QEMU_CLOCK_REALTIME */
164 static int rtc_host_datetime_offset = -1; /* valid & used only with
165 RTC_BASE_DATETIME */
166 QEMUClockType rtc_clock;
167 int vga_interface_type = VGA_NONE;
168 static DisplayOptions dpy;
169 int no_frame;
170 static int num_serial_hds;
171 static Chardev **serial_hds;
172 Chardev *parallel_hds[MAX_PARALLEL_PORTS];
173 Chardev *virtcon_hds[MAX_VIRTIO_CONSOLES];
174 int win2k_install_hack = 0;
175 int singlestep = 0;
176 int smp_cpus;
177 unsigned int max_cpus;
178 int smp_cores = 1;
179 int smp_threads = 1;
180 int acpi_enabled = 1;
181 int no_hpet = 0;
182 int fd_bootchk = 1;
183 static int no_reboot;
184 int no_shutdown = 0;
185 int cursor_hide = 1;
186 int graphic_rotate = 0;
187 const char *watchdog;
188 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
189 int nb_option_roms;
190 int old_param = 0;
191 const char *qemu_name;
192 int alt_grab = 0;
193 int ctrl_grab = 0;
194 unsigned int nb_prom_envs = 0;
195 const char *prom_envs[MAX_PROM_ENVS];
196 int boot_menu;
197 bool boot_strict;
198 uint8_t *boot_splash_filedata;
199 size_t boot_splash_filedata_size;
200 uint8_t qemu_extra_params_fw[2];
202 int icount_align_option;
204 /* The bytes in qemu_uuid are in the order specified by RFC4122, _not_ in the
205 * little-endian "wire format" described in the SMBIOS 2.6 specification.
207 QemuUUID qemu_uuid;
208 bool qemu_uuid_set;
210 /* Trace unassigned memory or i/o accesses. */
211 bool trace_unassigned;
213 static NotifierList exit_notifiers =
214 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
216 static NotifierList machine_init_done_notifiers =
217 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
219 bool xen_allowed;
220 uint32_t xen_domid;
221 enum xen_mode xen_mode = XEN_EMULATE;
222 bool xen_domid_restrict;
224 static int has_defaults = 1;
225 static int default_serial = 1;
226 static int default_parallel = 1;
227 static int default_virtcon = 1;
228 static int default_monitor = 1;
229 static int default_floppy = 1;
230 static int default_cdrom = 1;
231 static int default_sdcard = 1;
232 static int default_vga = 1;
233 static int default_net = 1;
235 static struct {
236 const char *driver;
237 int *flag;
238 } default_list[] = {
239 { .driver = "isa-serial", .flag = &default_serial },
240 { .driver = "isa-parallel", .flag = &default_parallel },
241 { .driver = "isa-fdc", .flag = &default_floppy },
242 { .driver = "floppy", .flag = &default_floppy },
243 { .driver = "ide-cd", .flag = &default_cdrom },
244 { .driver = "ide-hd", .flag = &default_cdrom },
245 { .driver = "ide-drive", .flag = &default_cdrom },
246 { .driver = "scsi-cd", .flag = &default_cdrom },
247 { .driver = "scsi-hd", .flag = &default_cdrom },
248 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
249 { .driver = "virtio-serial", .flag = &default_virtcon },
250 { .driver = "VGA", .flag = &default_vga },
251 { .driver = "isa-vga", .flag = &default_vga },
252 { .driver = "cirrus-vga", .flag = &default_vga },
253 { .driver = "isa-cirrus-vga", .flag = &default_vga },
254 { .driver = "vmware-svga", .flag = &default_vga },
255 { .driver = "qxl-vga", .flag = &default_vga },
256 { .driver = "virtio-vga", .flag = &default_vga },
259 static QemuOptsList qemu_rtc_opts = {
260 .name = "rtc",
261 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
262 .merge_lists = true,
263 .desc = {
265 .name = "base",
266 .type = QEMU_OPT_STRING,
268 .name = "clock",
269 .type = QEMU_OPT_STRING,
271 .name = "driftfix",
272 .type = QEMU_OPT_STRING,
274 { /* end of list */ }
278 static QemuOptsList qemu_option_rom_opts = {
279 .name = "option-rom",
280 .implied_opt_name = "romfile",
281 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
282 .desc = {
284 .name = "bootindex",
285 .type = QEMU_OPT_NUMBER,
286 }, {
287 .name = "romfile",
288 .type = QEMU_OPT_STRING,
290 { /* end of list */ }
294 static QemuOptsList qemu_machine_opts = {
295 .name = "machine",
296 .implied_opt_name = "type",
297 .merge_lists = true,
298 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
299 .desc = {
301 * no elements => accept any
302 * sanity checking will happen later
303 * when setting machine properties
309 static QemuOptsList qemu_accel_opts = {
310 .name = "accel",
311 .implied_opt_name = "accel",
312 .head = QTAILQ_HEAD_INITIALIZER(qemu_accel_opts.head),
313 .merge_lists = true,
314 .desc = {
316 .name = "accel",
317 .type = QEMU_OPT_STRING,
318 .help = "Select the type of accelerator",
321 .name = "thread",
322 .type = QEMU_OPT_STRING,
323 .help = "Enable/disable multi-threaded TCG",
325 { /* end of list */ }
329 static QemuOptsList qemu_boot_opts = {
330 .name = "boot-opts",
331 .implied_opt_name = "order",
332 .merge_lists = true,
333 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
334 .desc = {
336 .name = "order",
337 .type = QEMU_OPT_STRING,
338 }, {
339 .name = "once",
340 .type = QEMU_OPT_STRING,
341 }, {
342 .name = "menu",
343 .type = QEMU_OPT_BOOL,
344 }, {
345 .name = "splash",
346 .type = QEMU_OPT_STRING,
347 }, {
348 .name = "splash-time",
349 .type = QEMU_OPT_STRING,
350 }, {
351 .name = "reboot-timeout",
352 .type = QEMU_OPT_STRING,
353 }, {
354 .name = "strict",
355 .type = QEMU_OPT_BOOL,
357 { /*End of list */ }
361 static QemuOptsList qemu_add_fd_opts = {
362 .name = "add-fd",
363 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
364 .desc = {
366 .name = "fd",
367 .type = QEMU_OPT_NUMBER,
368 .help = "file descriptor of which a duplicate is added to fd set",
370 .name = "set",
371 .type = QEMU_OPT_NUMBER,
372 .help = "ID of the fd set to add fd to",
374 .name = "opaque",
375 .type = QEMU_OPT_STRING,
376 .help = "free-form string used to describe fd",
378 { /* end of list */ }
382 static QemuOptsList qemu_object_opts = {
383 .name = "object",
384 .implied_opt_name = "qom-type",
385 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
386 .desc = {
391 static QemuOptsList qemu_tpmdev_opts = {
392 .name = "tpmdev",
393 .implied_opt_name = "type",
394 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
395 .desc = {
396 /* options are defined in the TPM backends */
397 { /* end of list */ }
401 static QemuOptsList qemu_realtime_opts = {
402 .name = "realtime",
403 .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
404 .desc = {
406 .name = "mlock",
407 .type = QEMU_OPT_BOOL,
409 { /* end of list */ }
413 static QemuOptsList qemu_overcommit_opts = {
414 .name = "overcommit",
415 .head = QTAILQ_HEAD_INITIALIZER(qemu_overcommit_opts.head),
416 .desc = {
418 .name = "mem-lock",
419 .type = QEMU_OPT_BOOL,
422 .name = "cpu-pm",
423 .type = QEMU_OPT_BOOL,
425 { /* end of list */ }
429 static QemuOptsList qemu_msg_opts = {
430 .name = "msg",
431 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
432 .desc = {
434 .name = "timestamp",
435 .type = QEMU_OPT_BOOL,
437 { /* end of list */ }
441 static QemuOptsList qemu_name_opts = {
442 .name = "name",
443 .implied_opt_name = "guest",
444 .merge_lists = true,
445 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
446 .desc = {
448 .name = "guest",
449 .type = QEMU_OPT_STRING,
450 .help = "Sets the name of the guest.\n"
451 "This name will be displayed in the SDL window caption.\n"
452 "The name will also be used for the VNC server",
453 }, {
454 .name = "process",
455 .type = QEMU_OPT_STRING,
456 .help = "Sets the name of the QEMU process, as shown in top etc",
457 }, {
458 .name = "debug-threads",
459 .type = QEMU_OPT_BOOL,
460 .help = "When enabled, name the individual threads; defaults off.\n"
461 "NOTE: The thread names are for debugging and not a\n"
462 "stable API.",
464 { /* End of list */ }
468 static QemuOptsList qemu_mem_opts = {
469 .name = "memory",
470 .implied_opt_name = "size",
471 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
472 .merge_lists = true,
473 .desc = {
475 .name = "size",
476 .type = QEMU_OPT_SIZE,
479 .name = "slots",
480 .type = QEMU_OPT_NUMBER,
483 .name = "maxmem",
484 .type = QEMU_OPT_SIZE,
486 { /* end of list */ }
490 static QemuOptsList qemu_icount_opts = {
491 .name = "icount",
492 .implied_opt_name = "shift",
493 .merge_lists = true,
494 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
495 .desc = {
497 .name = "shift",
498 .type = QEMU_OPT_STRING,
499 }, {
500 .name = "align",
501 .type = QEMU_OPT_BOOL,
502 }, {
503 .name = "sleep",
504 .type = QEMU_OPT_BOOL,
505 }, {
506 .name = "rr",
507 .type = QEMU_OPT_STRING,
508 }, {
509 .name = "rrfile",
510 .type = QEMU_OPT_STRING,
511 }, {
512 .name = "rrsnapshot",
513 .type = QEMU_OPT_STRING,
515 { /* end of list */ }
519 static QemuOptsList qemu_semihosting_config_opts = {
520 .name = "semihosting-config",
521 .implied_opt_name = "enable",
522 .head = QTAILQ_HEAD_INITIALIZER(qemu_semihosting_config_opts.head),
523 .desc = {
525 .name = "enable",
526 .type = QEMU_OPT_BOOL,
527 }, {
528 .name = "target",
529 .type = QEMU_OPT_STRING,
530 }, {
531 .name = "arg",
532 .type = QEMU_OPT_STRING,
534 { /* end of list */ }
538 static QemuOptsList qemu_fw_cfg_opts = {
539 .name = "fw_cfg",
540 .implied_opt_name = "name",
541 .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
542 .desc = {
544 .name = "name",
545 .type = QEMU_OPT_STRING,
546 .help = "Sets the fw_cfg name of the blob to be inserted",
547 }, {
548 .name = "file",
549 .type = QEMU_OPT_STRING,
550 .help = "Sets the name of the file from which "
551 "the fw_cfg blob will be loaded",
552 }, {
553 .name = "string",
554 .type = QEMU_OPT_STRING,
555 .help = "Sets content of the blob to be inserted from a string",
557 { /* end of list */ }
562 * Get machine options
564 * Returns: machine options (never null).
566 QemuOpts *qemu_get_machine_opts(void)
568 return qemu_find_opts_singleton("machine");
571 const char *qemu_get_vm_name(void)
573 return qemu_name;
576 static void res_free(void)
578 g_free(boot_splash_filedata);
579 boot_splash_filedata = NULL;
582 static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
584 const char *driver = qemu_opt_get(opts, "driver");
585 int i;
587 if (!driver)
588 return 0;
589 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
590 if (strcmp(default_list[i].driver, driver) != 0)
591 continue;
592 *(default_list[i].flag) = 0;
594 return 0;
597 /***********************************************************/
598 /* QEMU state */
600 static RunState current_run_state = RUN_STATE_PRECONFIG;
602 /* We use RUN_STATE__MAX but any invalid value will do */
603 static RunState vmstop_requested = RUN_STATE__MAX;
604 static QemuMutex vmstop_lock;
606 typedef struct {
607 RunState from;
608 RunState to;
609 } RunStateTransition;
611 static const RunStateTransition runstate_transitions_def[] = {
612 /* from -> to */
613 { RUN_STATE_PRECONFIG, RUN_STATE_PRELAUNCH },
614 /* Early switch to inmigrate state to allow -incoming CLI option work
615 * as it used to. TODO: delay actual switching to inmigrate state to
616 * the point after machine is built and remove this hack.
618 { RUN_STATE_PRECONFIG, RUN_STATE_INMIGRATE },
620 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
621 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
622 { RUN_STATE_DEBUG, RUN_STATE_PRELAUNCH },
624 { RUN_STATE_INMIGRATE, RUN_STATE_INTERNAL_ERROR },
625 { RUN_STATE_INMIGRATE, RUN_STATE_IO_ERROR },
626 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
627 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
628 { RUN_STATE_INMIGRATE, RUN_STATE_SHUTDOWN },
629 { RUN_STATE_INMIGRATE, RUN_STATE_SUSPENDED },
630 { RUN_STATE_INMIGRATE, RUN_STATE_WATCHDOG },
631 { RUN_STATE_INMIGRATE, RUN_STATE_GUEST_PANICKED },
632 { RUN_STATE_INMIGRATE, RUN_STATE_FINISH_MIGRATE },
633 { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH },
634 { RUN_STATE_INMIGRATE, RUN_STATE_POSTMIGRATE },
635 { RUN_STATE_INMIGRATE, RUN_STATE_COLO },
637 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
638 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
639 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PRELAUNCH },
641 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
642 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
643 { RUN_STATE_IO_ERROR, RUN_STATE_PRELAUNCH },
645 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
646 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
647 { RUN_STATE_PAUSED, RUN_STATE_POSTMIGRATE },
648 { RUN_STATE_PAUSED, RUN_STATE_PRELAUNCH },
649 { RUN_STATE_PAUSED, RUN_STATE_COLO},
651 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
652 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
653 { RUN_STATE_POSTMIGRATE, RUN_STATE_PRELAUNCH },
655 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
656 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
657 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
659 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
660 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PAUSED },
661 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
662 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PRELAUNCH },
663 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_COLO},
665 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
666 { RUN_STATE_RESTORE_VM, RUN_STATE_PRELAUNCH },
668 { RUN_STATE_COLO, RUN_STATE_RUNNING },
670 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
671 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
672 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
673 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
674 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
675 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
676 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
677 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
678 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
679 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
680 { RUN_STATE_RUNNING, RUN_STATE_COLO},
682 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
684 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
685 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
686 { RUN_STATE_SHUTDOWN, RUN_STATE_PRELAUNCH },
688 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
689 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
690 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
691 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
692 { RUN_STATE_SUSPENDED, RUN_STATE_PRELAUNCH },
693 { RUN_STATE_SUSPENDED, RUN_STATE_COLO},
695 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
696 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
697 { RUN_STATE_WATCHDOG, RUN_STATE_PRELAUNCH },
698 { RUN_STATE_WATCHDOG, RUN_STATE_COLO},
700 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
701 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
702 { RUN_STATE_GUEST_PANICKED, RUN_STATE_PRELAUNCH },
704 { RUN_STATE__MAX, RUN_STATE__MAX },
707 static bool runstate_valid_transitions[RUN_STATE__MAX][RUN_STATE__MAX];
709 bool runstate_check(RunState state)
711 return current_run_state == state;
714 bool runstate_store(char *str, size_t size)
716 const char *state = RunState_str(current_run_state);
717 size_t len = strlen(state) + 1;
719 if (len > size) {
720 return false;
722 memcpy(str, state, len);
723 return true;
726 static void runstate_init(void)
728 const RunStateTransition *p;
730 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
731 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE__MAX; p++) {
732 runstate_valid_transitions[p->from][p->to] = true;
735 qemu_mutex_init(&vmstop_lock);
738 /* This function will abort() on invalid state transitions */
739 void runstate_set(RunState new_state)
741 assert(new_state < RUN_STATE__MAX);
743 if (current_run_state == new_state) {
744 return;
747 if (!runstate_valid_transitions[current_run_state][new_state]) {
748 error_report("invalid runstate transition: '%s' -> '%s'",
749 RunState_str(current_run_state),
750 RunState_str(new_state));
751 abort();
753 trace_runstate_set(new_state);
754 current_run_state = new_state;
757 int runstate_is_running(void)
759 return runstate_check(RUN_STATE_RUNNING);
762 bool runstate_needs_reset(void)
764 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
765 runstate_check(RUN_STATE_SHUTDOWN);
768 StatusInfo *qmp_query_status(Error **errp)
770 StatusInfo *info = g_malloc0(sizeof(*info));
772 info->running = runstate_is_running();
773 info->singlestep = singlestep;
774 info->status = current_run_state;
776 return info;
779 bool qemu_vmstop_requested(RunState *r)
781 qemu_mutex_lock(&vmstop_lock);
782 *r = vmstop_requested;
783 vmstop_requested = RUN_STATE__MAX;
784 qemu_mutex_unlock(&vmstop_lock);
785 return *r < RUN_STATE__MAX;
788 void qemu_system_vmstop_request_prepare(void)
790 qemu_mutex_lock(&vmstop_lock);
793 void qemu_system_vmstop_request(RunState state)
795 vmstop_requested = state;
796 qemu_mutex_unlock(&vmstop_lock);
797 qemu_notify_event();
800 /***********************************************************/
801 /* RTC reference time/date access */
802 static time_t qemu_ref_timedate(QEMUClockType clock)
804 time_t value = qemu_clock_get_ms(clock) / 1000;
805 switch (clock) {
806 case QEMU_CLOCK_REALTIME:
807 value -= rtc_realtime_clock_offset;
808 /* no break */
809 case QEMU_CLOCK_VIRTUAL:
810 value += rtc_ref_start_datetime;
811 break;
812 case QEMU_CLOCK_HOST:
813 if (rtc_base_type == RTC_BASE_DATETIME) {
814 value -= rtc_host_datetime_offset;
816 break;
817 default:
818 assert(0);
820 return value;
823 void qemu_get_timedate(struct tm *tm, int offset)
825 time_t ti = qemu_ref_timedate(rtc_clock);
827 ti += offset;
829 switch (rtc_base_type) {
830 case RTC_BASE_DATETIME:
831 case RTC_BASE_UTC:
832 gmtime_r(&ti, tm);
833 break;
834 case RTC_BASE_LOCALTIME:
835 localtime_r(&ti, tm);
836 break;
840 int qemu_timedate_diff(struct tm *tm)
842 time_t seconds;
844 switch (rtc_base_type) {
845 case RTC_BASE_DATETIME:
846 case RTC_BASE_UTC:
847 seconds = mktimegm(tm);
848 break;
849 case RTC_BASE_LOCALTIME:
851 struct tm tmp = *tm;
852 tmp.tm_isdst = -1; /* use timezone to figure it out */
853 seconds = mktime(&tmp);
854 break;
856 default:
857 abort();
860 return seconds - qemu_ref_timedate(QEMU_CLOCK_HOST);
863 static void configure_rtc_base_datetime(const char *startdate)
865 time_t rtc_start_datetime;
866 struct tm tm;
868 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d", &tm.tm_year, &tm.tm_mon,
869 &tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec) == 6) {
870 /* OK */
871 } else if (sscanf(startdate, "%d-%d-%d",
872 &tm.tm_year, &tm.tm_mon, &tm.tm_mday) == 3) {
873 tm.tm_hour = 0;
874 tm.tm_min = 0;
875 tm.tm_sec = 0;
876 } else {
877 goto date_fail;
879 tm.tm_year -= 1900;
880 tm.tm_mon--;
881 rtc_start_datetime = mktimegm(&tm);
882 if (rtc_start_datetime == -1) {
883 date_fail:
884 error_report("invalid datetime format");
885 error_printf("valid formats: "
886 "'2006-06-17T16:01:21' or '2006-06-17'\n");
887 exit(1);
889 rtc_host_datetime_offset = rtc_ref_start_datetime - rtc_start_datetime;
890 rtc_ref_start_datetime = rtc_start_datetime;
893 static void configure_rtc(QemuOpts *opts)
895 const char *value;
897 /* Set defaults */
898 rtc_clock = QEMU_CLOCK_HOST;
899 rtc_ref_start_datetime = qemu_clock_get_ms(QEMU_CLOCK_HOST) / 1000;
900 rtc_realtime_clock_offset = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) / 1000;
902 value = qemu_opt_get(opts, "base");
903 if (value) {
904 if (!strcmp(value, "utc")) {
905 rtc_base_type = RTC_BASE_UTC;
906 } else if (!strcmp(value, "localtime")) {
907 Error *blocker = NULL;
908 rtc_base_type = RTC_BASE_LOCALTIME;
909 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
910 "-rtc base=localtime");
911 replay_add_blocker(blocker);
912 } else {
913 rtc_base_type = RTC_BASE_DATETIME;
914 configure_rtc_base_datetime(value);
917 value = qemu_opt_get(opts, "clock");
918 if (value) {
919 if (!strcmp(value, "host")) {
920 rtc_clock = QEMU_CLOCK_HOST;
921 } else if (!strcmp(value, "rt")) {
922 rtc_clock = QEMU_CLOCK_REALTIME;
923 } else if (!strcmp(value, "vm")) {
924 rtc_clock = QEMU_CLOCK_VIRTUAL;
925 } else {
926 error_report("invalid option value '%s'", value);
927 exit(1);
930 value = qemu_opt_get(opts, "driftfix");
931 if (value) {
932 if (!strcmp(value, "slew")) {
933 static GlobalProperty slew_lost_ticks = {
934 .driver = "mc146818rtc",
935 .property = "lost_tick_policy",
936 .value = "slew",
939 qdev_prop_register_global(&slew_lost_ticks);
940 } else if (!strcmp(value, "none")) {
941 /* discard is default */
942 } else {
943 error_report("invalid option value '%s'", value);
944 exit(1);
949 /***********************************************************/
950 /* Bluetooth support */
951 static int nb_hcis;
952 static int cur_hci;
953 static struct HCIInfo *hci_table[MAX_NICS];
955 struct HCIInfo *qemu_next_hci(void)
957 if (cur_hci == nb_hcis)
958 return &null_hci;
960 return hci_table[cur_hci++];
963 static int bt_hci_parse(const char *str)
965 struct HCIInfo *hci;
966 bdaddr_t bdaddr;
968 if (nb_hcis >= MAX_NICS) {
969 error_report("too many bluetooth HCIs (max %i)", MAX_NICS);
970 return -1;
973 hci = hci_init(str);
974 if (!hci)
975 return -1;
977 bdaddr.b[0] = 0x52;
978 bdaddr.b[1] = 0x54;
979 bdaddr.b[2] = 0x00;
980 bdaddr.b[3] = 0x12;
981 bdaddr.b[4] = 0x34;
982 bdaddr.b[5] = 0x56 + nb_hcis;
983 hci->bdaddr_set(hci, bdaddr.b);
985 hci_table[nb_hcis++] = hci;
987 return 0;
990 static void bt_vhci_add(int vlan_id)
992 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
994 if (!vlan->slave)
995 warn_report("adding a VHCI to an empty scatternet %i",
996 vlan_id);
998 bt_vhci_init(bt_new_hci(vlan));
1001 static struct bt_device_s *bt_device_add(const char *opt)
1003 struct bt_scatternet_s *vlan;
1004 int vlan_id = 0;
1005 char *endp = strstr(opt, ",vlan=");
1006 int len = (endp ? endp - opt : strlen(opt)) + 1;
1007 char devname[10];
1009 pstrcpy(devname, MIN(sizeof(devname), len), opt);
1011 if (endp) {
1012 vlan_id = strtol(endp + 6, &endp, 0);
1013 if (*endp) {
1014 error_report("unrecognised bluetooth vlan Id");
1015 return 0;
1019 vlan = qemu_find_bt_vlan(vlan_id);
1021 if (!vlan->slave)
1022 warn_report("adding a slave device to an empty scatternet %i",
1023 vlan_id);
1025 if (!strcmp(devname, "keyboard"))
1026 return bt_keyboard_init(vlan);
1028 error_report("unsupported bluetooth device '%s'", devname);
1029 return 0;
1032 static int bt_parse(const char *opt)
1034 const char *endp, *p;
1035 int vlan;
1037 if (strstart(opt, "hci", &endp)) {
1038 if (!*endp || *endp == ',') {
1039 if (*endp)
1040 if (!strstart(endp, ",vlan=", 0))
1041 opt = endp + 1;
1043 return bt_hci_parse(opt);
1045 } else if (strstart(opt, "vhci", &endp)) {
1046 if (!*endp || *endp == ',') {
1047 if (*endp) {
1048 if (strstart(endp, ",vlan=", &p)) {
1049 vlan = strtol(p, (char **) &endp, 0);
1050 if (*endp) {
1051 error_report("bad scatternet '%s'", p);
1052 return 1;
1054 } else {
1055 error_report("bad parameter '%s'", endp + 1);
1056 return 1;
1058 } else
1059 vlan = 0;
1061 bt_vhci_add(vlan);
1062 return 0;
1064 } else if (strstart(opt, "device:", &endp))
1065 return !bt_device_add(endp);
1067 error_report("bad bluetooth parameter '%s'", opt);
1068 return 1;
1071 static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
1073 const char *proc_name;
1075 if (qemu_opt_get(opts, "debug-threads")) {
1076 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
1078 qemu_name = qemu_opt_get(opts, "guest");
1080 proc_name = qemu_opt_get(opts, "process");
1081 if (proc_name) {
1082 os_set_proc_name(proc_name);
1085 return 0;
1088 bool defaults_enabled(void)
1090 return has_defaults;
1093 #ifndef _WIN32
1094 static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1096 int fd, dupfd, flags;
1097 int64_t fdset_id;
1098 const char *fd_opaque = NULL;
1099 AddfdInfo *fdinfo;
1101 fd = qemu_opt_get_number(opts, "fd", -1);
1102 fdset_id = qemu_opt_get_number(opts, "set", -1);
1103 fd_opaque = qemu_opt_get(opts, "opaque");
1105 if (fd < 0) {
1106 error_setg(errp, "fd option is required and must be non-negative");
1107 return -1;
1110 if (fd <= STDERR_FILENO) {
1111 error_setg(errp, "fd cannot be a standard I/O stream");
1112 return -1;
1116 * All fds inherited across exec() necessarily have FD_CLOEXEC
1117 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1119 flags = fcntl(fd, F_GETFD);
1120 if (flags == -1 || (flags & FD_CLOEXEC)) {
1121 error_setg(errp, "fd is not valid or already in use");
1122 return -1;
1125 if (fdset_id < 0) {
1126 error_setg(errp, "set option is required and must be non-negative");
1127 return -1;
1130 #ifdef F_DUPFD_CLOEXEC
1131 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1132 #else
1133 dupfd = dup(fd);
1134 if (dupfd != -1) {
1135 qemu_set_cloexec(dupfd);
1137 #endif
1138 if (dupfd == -1) {
1139 error_setg(errp, "error duplicating fd: %s", strerror(errno));
1140 return -1;
1143 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1144 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
1145 &error_abort);
1146 g_free(fdinfo);
1148 return 0;
1151 static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1153 int fd;
1155 fd = qemu_opt_get_number(opts, "fd", -1);
1156 close(fd);
1158 return 0;
1160 #endif
1162 /***********************************************************/
1163 /* QEMU Block devices */
1165 #define HD_OPTS "media=disk"
1166 #define CDROM_OPTS "media=cdrom"
1167 #define FD_OPTS ""
1168 #define PFLASH_OPTS ""
1169 #define MTD_OPTS ""
1170 #define SD_OPTS ""
1172 static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
1174 BlockInterfaceType *block_default_type = opaque;
1176 return drive_new(opts, *block_default_type, errp) == NULL;
1179 static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
1181 if (qemu_opt_get(opts, "snapshot") == NULL) {
1182 qemu_opt_set(opts, "snapshot", "on", &error_abort);
1184 return 0;
1187 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1188 int index, const char *optstr)
1190 QemuOpts *opts;
1191 DriveInfo *dinfo;
1193 if (!enable || drive_get_by_index(type, index)) {
1194 return;
1197 opts = drive_add(type, index, NULL, optstr);
1198 if (snapshot) {
1199 drive_enable_snapshot(NULL, opts, NULL);
1202 dinfo = drive_new(opts, type, &error_abort);
1203 dinfo->is_default = true;
1207 static QemuOptsList qemu_smp_opts = {
1208 .name = "smp-opts",
1209 .implied_opt_name = "cpus",
1210 .merge_lists = true,
1211 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
1212 .desc = {
1214 .name = "cpus",
1215 .type = QEMU_OPT_NUMBER,
1216 }, {
1217 .name = "sockets",
1218 .type = QEMU_OPT_NUMBER,
1219 }, {
1220 .name = "cores",
1221 .type = QEMU_OPT_NUMBER,
1222 }, {
1223 .name = "threads",
1224 .type = QEMU_OPT_NUMBER,
1225 }, {
1226 .name = "maxcpus",
1227 .type = QEMU_OPT_NUMBER,
1229 { /*End of list */ }
1233 static void smp_parse(QemuOpts *opts)
1235 if (opts) {
1236 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
1237 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
1238 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
1239 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
1241 /* compute missing values, prefer sockets over cores over threads */
1242 if (cpus == 0 || sockets == 0) {
1243 cores = cores > 0 ? cores : 1;
1244 threads = threads > 0 ? threads : 1;
1245 if (cpus == 0) {
1246 sockets = sockets > 0 ? sockets : 1;
1247 cpus = cores * threads * sockets;
1248 } else {
1249 max_cpus = qemu_opt_get_number(opts, "maxcpus", cpus);
1250 sockets = max_cpus / (cores * threads);
1252 } else if (cores == 0) {
1253 threads = threads > 0 ? threads : 1;
1254 cores = cpus / (sockets * threads);
1255 cores = cores > 0 ? cores : 1;
1256 } else if (threads == 0) {
1257 threads = cpus / (cores * sockets);
1258 threads = threads > 0 ? threads : 1;
1259 } else if (sockets * cores * threads < cpus) {
1260 error_report("cpu topology: "
1261 "sockets (%u) * cores (%u) * threads (%u) < "
1262 "smp_cpus (%u)",
1263 sockets, cores, threads, cpus);
1264 exit(1);
1267 max_cpus = qemu_opt_get_number(opts, "maxcpus", cpus);
1269 if (max_cpus < cpus) {
1270 error_report("maxcpus must be equal to or greater than smp");
1271 exit(1);
1274 if (sockets * cores * threads > max_cpus) {
1275 error_report("cpu topology: "
1276 "sockets (%u) * cores (%u) * threads (%u) > "
1277 "maxcpus (%u)",
1278 sockets, cores, threads, max_cpus);
1279 exit(1);
1282 if (sockets * cores * threads != max_cpus) {
1283 warn_report("Invalid CPU topology deprecated: "
1284 "sockets (%u) * cores (%u) * threads (%u) "
1285 "!= maxcpus (%u)",
1286 sockets, cores, threads, max_cpus);
1289 smp_cpus = cpus;
1290 smp_cores = cores;
1291 smp_threads = threads;
1294 if (smp_cpus > 1) {
1295 Error *blocker = NULL;
1296 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
1297 replay_add_blocker(blocker);
1301 static void realtime_init(void)
1303 if (enable_mlock) {
1304 if (os_mlock() < 0) {
1305 error_report("locking memory failed");
1306 exit(1);
1312 static void configure_msg(QemuOpts *opts)
1314 enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
1317 /***********************************************************/
1318 /* Semihosting */
1320 typedef struct SemihostingConfig {
1321 bool enabled;
1322 SemihostingTarget target;
1323 const char **argv;
1324 int argc;
1325 const char *cmdline; /* concatenated argv */
1326 } SemihostingConfig;
1328 static SemihostingConfig semihosting;
1330 bool semihosting_enabled(void)
1332 return semihosting.enabled;
1335 SemihostingTarget semihosting_get_target(void)
1337 return semihosting.target;
1340 const char *semihosting_get_arg(int i)
1342 if (i >= semihosting.argc) {
1343 return NULL;
1345 return semihosting.argv[i];
1348 int semihosting_get_argc(void)
1350 return semihosting.argc;
1353 const char *semihosting_get_cmdline(void)
1355 if (semihosting.cmdline == NULL && semihosting.argc > 0) {
1356 semihosting.cmdline = g_strjoinv(" ", (gchar **)semihosting.argv);
1358 return semihosting.cmdline;
1361 static int add_semihosting_arg(void *opaque,
1362 const char *name, const char *val,
1363 Error **errp)
1365 SemihostingConfig *s = opaque;
1366 if (strcmp(name, "arg") == 0) {
1367 s->argc++;
1368 /* one extra element as g_strjoinv() expects NULL-terminated array */
1369 s->argv = g_realloc(s->argv, (s->argc + 1) * sizeof(void *));
1370 s->argv[s->argc - 1] = val;
1371 s->argv[s->argc] = NULL;
1373 return 0;
1376 /* Use strings passed via -kernel/-append to initialize semihosting.argv[] */
1377 static inline void semihosting_arg_fallback(const char *file, const char *cmd)
1379 char *cmd_token;
1381 /* argv[0] */
1382 add_semihosting_arg(&semihosting, "arg", file, NULL);
1384 /* split -append and initialize argv[1..n] */
1385 cmd_token = strtok(g_strdup(cmd), " ");
1386 while (cmd_token) {
1387 add_semihosting_arg(&semihosting, "arg", cmd_token, NULL);
1388 cmd_token = strtok(NULL, " ");
1392 /* Now we still need this for compatibility with XEN. */
1393 bool has_igd_gfx_passthru;
1394 static void igd_gfx_passthru(void)
1396 has_igd_gfx_passthru = current_machine->igd_gfx_passthru;
1399 /***********************************************************/
1400 /* USB devices */
1402 static int usb_device_add(const char *devname)
1404 USBDevice *dev = NULL;
1406 if (!machine_usb(current_machine)) {
1407 return -1;
1410 dev = usbdevice_create(devname);
1411 if (!dev)
1412 return -1;
1414 return 0;
1417 static int usb_parse(const char *cmdline)
1419 int r;
1420 r = usb_device_add(cmdline);
1421 if (r < 0) {
1422 error_report("could not add USB device '%s'", cmdline);
1424 return r;
1427 /***********************************************************/
1428 /* machine registration */
1430 MachineState *current_machine;
1432 static MachineClass *find_machine(const char *name)
1434 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1435 MachineClass *mc = NULL;
1437 for (el = machines; el; el = el->next) {
1438 MachineClass *temp = el->data;
1440 if (!strcmp(temp->name, name)) {
1441 mc = temp;
1442 break;
1444 if (temp->alias &&
1445 !strcmp(temp->alias, name)) {
1446 mc = temp;
1447 break;
1451 g_slist_free(machines);
1452 return mc;
1455 MachineClass *find_default_machine(void)
1457 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1458 MachineClass *mc = NULL;
1460 for (el = machines; el; el = el->next) {
1461 MachineClass *temp = el->data;
1463 if (temp->is_default) {
1464 mc = temp;
1465 break;
1469 g_slist_free(machines);
1470 return mc;
1473 MachineInfoList *qmp_query_machines(Error **errp)
1475 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1476 MachineInfoList *mach_list = NULL;
1478 for (el = machines; el; el = el->next) {
1479 MachineClass *mc = el->data;
1480 MachineInfoList *entry;
1481 MachineInfo *info;
1483 info = g_malloc0(sizeof(*info));
1484 if (mc->is_default) {
1485 info->has_is_default = true;
1486 info->is_default = true;
1489 if (mc->alias) {
1490 info->has_alias = true;
1491 info->alias = g_strdup(mc->alias);
1494 info->name = g_strdup(mc->name);
1495 info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
1496 info->hotpluggable_cpus = mc->has_hotpluggable_cpus;
1498 entry = g_malloc0(sizeof(*entry));
1499 entry->value = info;
1500 entry->next = mach_list;
1501 mach_list = entry;
1504 g_slist_free(machines);
1505 return mach_list;
1508 static int machine_help_func(QemuOpts *opts, MachineState *machine)
1510 ObjectProperty *prop;
1511 ObjectPropertyIterator iter;
1513 if (!qemu_opt_has_help_opt(opts)) {
1514 return 0;
1517 object_property_iter_init(&iter, OBJECT(machine));
1518 while ((prop = object_property_iter_next(&iter))) {
1519 if (!prop->set) {
1520 continue;
1523 error_printf("%s.%s=%s", MACHINE_GET_CLASS(machine)->name,
1524 prop->name, prop->type);
1525 if (prop->description) {
1526 error_printf(" (%s)\n", prop->description);
1527 } else {
1528 error_printf("\n");
1532 return 1;
1535 /***********************************************************/
1536 /* main execution loop */
1538 struct vm_change_state_entry {
1539 VMChangeStateHandler *cb;
1540 void *opaque;
1541 QLIST_ENTRY (vm_change_state_entry) entries;
1544 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1546 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1547 void *opaque)
1549 VMChangeStateEntry *e;
1551 e = g_malloc0(sizeof (*e));
1553 e->cb = cb;
1554 e->opaque = opaque;
1555 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1556 return e;
1559 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1561 QLIST_REMOVE (e, entries);
1562 g_free (e);
1565 void vm_state_notify(int running, RunState state)
1567 VMChangeStateEntry *e, *next;
1569 trace_vm_state_notify(running, state);
1571 QLIST_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
1572 e->cb(e->opaque, running, state);
1576 static ShutdownCause reset_requested;
1577 static ShutdownCause shutdown_requested;
1578 static int shutdown_signal;
1579 static pid_t shutdown_pid;
1580 static int powerdown_requested;
1581 static int debug_requested;
1582 static int suspend_requested;
1583 static bool preconfig_exit_requested = true;
1584 static WakeupReason wakeup_reason;
1585 static NotifierList powerdown_notifiers =
1586 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
1587 static NotifierList suspend_notifiers =
1588 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1589 static NotifierList wakeup_notifiers =
1590 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1591 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
1593 ShutdownCause qemu_shutdown_requested_get(void)
1595 return shutdown_requested;
1598 ShutdownCause qemu_reset_requested_get(void)
1600 return reset_requested;
1603 static int qemu_shutdown_requested(void)
1605 return atomic_xchg(&shutdown_requested, SHUTDOWN_CAUSE_NONE);
1608 static void qemu_kill_report(void)
1610 if (!qtest_driver() && shutdown_signal) {
1611 if (shutdown_pid == 0) {
1612 /* This happens for eg ^C at the terminal, so it's worth
1613 * avoiding printing an odd message in that case.
1615 error_report("terminating on signal %d", shutdown_signal);
1616 } else {
1617 char *shutdown_cmd = qemu_get_pid_name(shutdown_pid);
1619 error_report("terminating on signal %d from pid " FMT_pid " (%s)",
1620 shutdown_signal, shutdown_pid,
1621 shutdown_cmd ? shutdown_cmd : "<unknown process>");
1622 g_free(shutdown_cmd);
1624 shutdown_signal = 0;
1628 static ShutdownCause qemu_reset_requested(void)
1630 ShutdownCause r = reset_requested;
1632 if (r && replay_checkpoint(CHECKPOINT_RESET_REQUESTED)) {
1633 reset_requested = SHUTDOWN_CAUSE_NONE;
1634 return r;
1636 return SHUTDOWN_CAUSE_NONE;
1639 static int qemu_suspend_requested(void)
1641 int r = suspend_requested;
1642 if (r && replay_checkpoint(CHECKPOINT_SUSPEND_REQUESTED)) {
1643 suspend_requested = 0;
1644 return r;
1646 return false;
1649 static WakeupReason qemu_wakeup_requested(void)
1651 return wakeup_reason;
1654 static int qemu_powerdown_requested(void)
1656 int r = powerdown_requested;
1657 powerdown_requested = 0;
1658 return r;
1661 static int qemu_debug_requested(void)
1663 int r = debug_requested;
1664 debug_requested = 0;
1665 return r;
1668 void qemu_exit_preconfig_request(void)
1670 preconfig_exit_requested = true;
1674 * Reset the VM. Issue an event unless @reason is SHUTDOWN_CAUSE_NONE.
1676 void qemu_system_reset(ShutdownCause reason)
1678 MachineClass *mc;
1680 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1682 cpu_synchronize_all_states();
1684 if (mc && mc->reset) {
1685 mc->reset();
1686 } else {
1687 qemu_devices_reset();
1689 if (reason != SHUTDOWN_CAUSE_SUBSYSTEM_RESET) {
1690 qapi_event_send_reset(shutdown_caused_by_guest(reason));
1692 cpu_synchronize_all_post_reset();
1695 void qemu_system_guest_panicked(GuestPanicInformation *info)
1697 qemu_log_mask(LOG_GUEST_ERROR, "Guest crashed");
1699 if (current_cpu) {
1700 current_cpu->crash_occurred = true;
1702 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE,
1703 !!info, info);
1704 vm_stop(RUN_STATE_GUEST_PANICKED);
1705 if (!no_shutdown) {
1706 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_POWEROFF,
1707 !!info, info);
1708 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_PANIC);
1711 if (info) {
1712 if (info->type == GUEST_PANIC_INFORMATION_TYPE_HYPER_V) {
1713 qemu_log_mask(LOG_GUEST_ERROR, "\nHV crash parameters: (%#"PRIx64
1714 " %#"PRIx64" %#"PRIx64" %#"PRIx64" %#"PRIx64")\n",
1715 info->u.hyper_v.arg1,
1716 info->u.hyper_v.arg2,
1717 info->u.hyper_v.arg3,
1718 info->u.hyper_v.arg4,
1719 info->u.hyper_v.arg5);
1720 } else if (info->type == GUEST_PANIC_INFORMATION_TYPE_S390) {
1721 qemu_log_mask(LOG_GUEST_ERROR, " on cpu %d: %s\n"
1722 "PSW: 0x%016" PRIx64 " 0x%016" PRIx64"\n",
1723 info->u.s390.core,
1724 S390CrashReason_str(info->u.s390.reason),
1725 info->u.s390.psw_mask,
1726 info->u.s390.psw_addr);
1728 qapi_free_GuestPanicInformation(info);
1732 void qemu_system_reset_request(ShutdownCause reason)
1734 if (no_reboot && reason != SHUTDOWN_CAUSE_SUBSYSTEM_RESET) {
1735 shutdown_requested = reason;
1736 } else {
1737 reset_requested = reason;
1739 cpu_stop_current();
1740 qemu_notify_event();
1743 static void qemu_system_suspend(void)
1745 pause_all_vcpus();
1746 notifier_list_notify(&suspend_notifiers, NULL);
1747 runstate_set(RUN_STATE_SUSPENDED);
1748 qapi_event_send_suspend();
1751 void qemu_system_suspend_request(void)
1753 if (runstate_check(RUN_STATE_SUSPENDED)) {
1754 return;
1756 suspend_requested = 1;
1757 cpu_stop_current();
1758 qemu_notify_event();
1761 void qemu_register_suspend_notifier(Notifier *notifier)
1763 notifier_list_add(&suspend_notifiers, notifier);
1766 void qemu_system_wakeup_request(WakeupReason reason)
1768 trace_system_wakeup_request(reason);
1770 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1771 return;
1773 if (!(wakeup_reason_mask & (1 << reason))) {
1774 return;
1776 runstate_set(RUN_STATE_RUNNING);
1777 wakeup_reason = reason;
1778 qemu_notify_event();
1781 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1783 if (enabled) {
1784 wakeup_reason_mask |= (1 << reason);
1785 } else {
1786 wakeup_reason_mask &= ~(1 << reason);
1790 void qemu_register_wakeup_notifier(Notifier *notifier)
1792 notifier_list_add(&wakeup_notifiers, notifier);
1795 void qemu_system_killed(int signal, pid_t pid)
1797 shutdown_signal = signal;
1798 shutdown_pid = pid;
1799 no_shutdown = 0;
1801 /* Cannot call qemu_system_shutdown_request directly because
1802 * we are in a signal handler.
1804 shutdown_requested = SHUTDOWN_CAUSE_HOST_SIGNAL;
1805 qemu_notify_event();
1808 void qemu_system_shutdown_request(ShutdownCause reason)
1810 trace_qemu_system_shutdown_request(reason);
1811 replay_shutdown_request(reason);
1812 shutdown_requested = reason;
1813 qemu_notify_event();
1816 static void qemu_system_powerdown(void)
1818 qapi_event_send_powerdown();
1819 notifier_list_notify(&powerdown_notifiers, NULL);
1822 void qemu_system_powerdown_request(void)
1824 trace_qemu_system_powerdown_request();
1825 powerdown_requested = 1;
1826 qemu_notify_event();
1829 void qemu_register_powerdown_notifier(Notifier *notifier)
1831 notifier_list_add(&powerdown_notifiers, notifier);
1834 void qemu_system_debug_request(void)
1836 debug_requested = 1;
1837 qemu_notify_event();
1840 static bool main_loop_should_exit(void)
1842 RunState r;
1843 ShutdownCause request;
1845 if (preconfig_exit_requested) {
1846 if (runstate_check(RUN_STATE_PRECONFIG)) {
1847 runstate_set(RUN_STATE_PRELAUNCH);
1849 preconfig_exit_requested = false;
1850 return true;
1852 if (qemu_debug_requested()) {
1853 vm_stop(RUN_STATE_DEBUG);
1855 if (qemu_suspend_requested()) {
1856 qemu_system_suspend();
1858 request = qemu_shutdown_requested();
1859 if (request) {
1860 qemu_kill_report();
1861 qapi_event_send_shutdown(shutdown_caused_by_guest(request));
1862 if (no_shutdown) {
1863 vm_stop(RUN_STATE_SHUTDOWN);
1864 } else {
1865 return true;
1868 request = qemu_reset_requested();
1869 if (request) {
1870 pause_all_vcpus();
1871 qemu_system_reset(request);
1872 resume_all_vcpus();
1873 if (!runstate_check(RUN_STATE_RUNNING) &&
1874 !runstate_check(RUN_STATE_INMIGRATE)) {
1875 runstate_set(RUN_STATE_PRELAUNCH);
1878 if (qemu_wakeup_requested()) {
1879 pause_all_vcpus();
1880 qemu_system_reset(SHUTDOWN_CAUSE_NONE);
1881 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
1882 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
1883 resume_all_vcpus();
1884 qapi_event_send_wakeup();
1886 if (qemu_powerdown_requested()) {
1887 qemu_system_powerdown();
1889 if (qemu_vmstop_requested(&r)) {
1890 vm_stop(r);
1892 return false;
1895 static void main_loop(void)
1897 #ifdef CONFIG_PROFILER
1898 int64_t ti;
1899 #endif
1900 while (!main_loop_should_exit()) {
1901 #ifdef CONFIG_PROFILER
1902 ti = profile_getclock();
1903 #endif
1904 main_loop_wait(false);
1905 #ifdef CONFIG_PROFILER
1906 dev_time += profile_getclock() - ti;
1907 #endif
1911 static void version(void)
1913 printf("QEMU emulator version " QEMU_FULL_VERSION "\n"
1914 QEMU_COPYRIGHT "\n");
1917 static void QEMU_NORETURN help(int exitcode)
1919 version();
1920 printf("usage: %s [options] [disk_image]\n\n"
1921 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1922 error_get_progname());
1924 #define QEMU_OPTIONS_GENERATE_HELP
1925 #include "qemu-options-wrapper.h"
1927 printf("\nDuring emulation, the following keys are useful:\n"
1928 "ctrl-alt-f toggle full screen\n"
1929 "ctrl-alt-n switch to virtual console 'n'\n"
1930 "ctrl-alt toggle mouse and keyboard grab\n"
1931 "\n"
1932 "When using -nographic, press 'ctrl-a h' to get some help.\n"
1933 "\n"
1934 QEMU_HELP_BOTTOM "\n");
1936 exit(exitcode);
1939 #define HAS_ARG 0x0001
1941 typedef struct QEMUOption {
1942 const char *name;
1943 int flags;
1944 int index;
1945 uint32_t arch_mask;
1946 } QEMUOption;
1948 static const QEMUOption qemu_options[] = {
1949 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1950 #define QEMU_OPTIONS_GENERATE_OPTIONS
1951 #include "qemu-options-wrapper.h"
1952 { NULL },
1955 typedef struct VGAInterfaceInfo {
1956 const char *opt_name; /* option name */
1957 const char *name; /* human-readable name */
1958 /* Class names indicating that support is available.
1959 * If no class is specified, the interface is always available */
1960 const char *class_names[2];
1961 } VGAInterfaceInfo;
1963 static VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
1964 [VGA_NONE] = {
1965 .opt_name = "none",
1967 [VGA_STD] = {
1968 .opt_name = "std",
1969 .name = "standard VGA",
1970 .class_names = { "VGA", "isa-vga" },
1972 [VGA_CIRRUS] = {
1973 .opt_name = "cirrus",
1974 .name = "Cirrus VGA",
1975 .class_names = { "cirrus-vga", "isa-cirrus-vga" },
1977 [VGA_VMWARE] = {
1978 .opt_name = "vmware",
1979 .name = "VMWare SVGA",
1980 .class_names = { "vmware-svga" },
1982 [VGA_VIRTIO] = {
1983 .opt_name = "virtio",
1984 .name = "Virtio VGA",
1985 .class_names = { "virtio-vga" },
1987 [VGA_QXL] = {
1988 .opt_name = "qxl",
1989 .name = "QXL VGA",
1990 .class_names = { "qxl-vga" },
1992 [VGA_TCX] = {
1993 .opt_name = "tcx",
1994 .name = "TCX framebuffer",
1995 .class_names = { "SUNW,tcx" },
1997 [VGA_CG3] = {
1998 .opt_name = "cg3",
1999 .name = "CG3 framebuffer",
2000 .class_names = { "cgthree" },
2002 [VGA_XENFB] = {
2003 .opt_name = "xenfb",
2007 static bool vga_interface_available(VGAInterfaceType t)
2009 VGAInterfaceInfo *ti = &vga_interfaces[t];
2011 assert(t < VGA_TYPE_MAX);
2012 return !ti->class_names[0] ||
2013 object_class_by_name(ti->class_names[0]) ||
2014 object_class_by_name(ti->class_names[1]);
2017 static void select_vgahw(const char *p)
2019 const char *opts;
2020 int t;
2022 assert(vga_interface_type == VGA_NONE);
2023 for (t = 0; t < VGA_TYPE_MAX; t++) {
2024 VGAInterfaceInfo *ti = &vga_interfaces[t];
2025 if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
2026 if (!vga_interface_available(t)) {
2027 error_report("%s not available", ti->name);
2028 exit(1);
2030 vga_interface_type = t;
2031 break;
2034 if (t == VGA_TYPE_MAX) {
2035 invalid_vga:
2036 error_report("unknown vga type: %s", p);
2037 exit(1);
2039 while (*opts) {
2040 const char *nextopt;
2042 if (strstart(opts, ",retrace=", &nextopt)) {
2043 opts = nextopt;
2044 if (strstart(opts, "dumb", &nextopt))
2045 vga_retrace_method = VGA_RETRACE_DUMB;
2046 else if (strstart(opts, "precise", &nextopt))
2047 vga_retrace_method = VGA_RETRACE_PRECISE;
2048 else goto invalid_vga;
2049 } else goto invalid_vga;
2050 opts = nextopt;
2054 static void parse_display_qapi(const char *optarg)
2056 DisplayOptions *opts;
2057 Visitor *v;
2059 v = qobject_input_visitor_new_str(optarg, "type", &error_fatal);
2061 visit_type_DisplayOptions(v, NULL, &opts, &error_fatal);
2062 QAPI_CLONE_MEMBERS(DisplayOptions, &dpy, opts);
2064 qapi_free_DisplayOptions(opts);
2065 visit_free(v);
2068 static void parse_display(const char *p)
2070 const char *opts;
2072 if (strstart(p, "sdl", &opts)) {
2074 * sdl DisplayType needs hand-crafted parser instead of
2075 * parse_display_qapi() due to some options not in
2076 * DisplayOptions, specifically:
2077 * - frame
2078 * Already deprecated.
2079 * - ctrl_grab + alt_grab
2080 * Not clear yet what happens to them long-term. Should
2081 * replaced by something better or deprecated and dropped.
2083 dpy.type = DISPLAY_TYPE_SDL;
2084 while (*opts) {
2085 const char *nextopt;
2087 if (strstart(opts, ",frame=", &nextopt)) {
2088 g_printerr("The frame= sdl option is deprecated, and will be\n"
2089 "removed in a future release.\n");
2090 opts = nextopt;
2091 if (strstart(opts, "on", &nextopt)) {
2092 no_frame = 0;
2093 } else if (strstart(opts, "off", &nextopt)) {
2094 no_frame = 1;
2095 } else {
2096 goto invalid_sdl_args;
2098 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
2099 opts = nextopt;
2100 if (strstart(opts, "on", &nextopt)) {
2101 alt_grab = 1;
2102 } else if (strstart(opts, "off", &nextopt)) {
2103 alt_grab = 0;
2104 } else {
2105 goto invalid_sdl_args;
2107 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2108 opts = nextopt;
2109 if (strstart(opts, "on", &nextopt)) {
2110 ctrl_grab = 1;
2111 } else if (strstart(opts, "off", &nextopt)) {
2112 ctrl_grab = 0;
2113 } else {
2114 goto invalid_sdl_args;
2116 } else if (strstart(opts, ",window_close=", &nextopt)) {
2117 opts = nextopt;
2118 dpy.has_window_close = true;
2119 if (strstart(opts, "on", &nextopt)) {
2120 dpy.window_close = true;
2121 } else if (strstart(opts, "off", &nextopt)) {
2122 dpy.window_close = false;
2123 } else {
2124 goto invalid_sdl_args;
2126 } else if (strstart(opts, ",gl=", &nextopt)) {
2127 opts = nextopt;
2128 dpy.has_gl = true;
2129 if (strstart(opts, "on", &nextopt)) {
2130 dpy.gl = DISPLAYGL_MODE_ON;
2131 } else if (strstart(opts, "core", &nextopt)) {
2132 dpy.gl = DISPLAYGL_MODE_CORE;
2133 } else if (strstart(opts, "es", &nextopt)) {
2134 dpy.gl = DISPLAYGL_MODE_ES;
2135 } else if (strstart(opts, "off", &nextopt)) {
2136 dpy.gl = DISPLAYGL_MODE_OFF;
2137 } else {
2138 goto invalid_sdl_args;
2140 } else {
2141 invalid_sdl_args:
2142 error_report("invalid SDL option string");
2143 exit(1);
2145 opts = nextopt;
2147 } else if (strstart(p, "vnc", &opts)) {
2149 * vnc isn't a (local) DisplayType but a protocol for remote
2150 * display access.
2152 if (*opts == '=') {
2153 vnc_parse(opts + 1, &error_fatal);
2154 } else {
2155 error_report("VNC requires a display argument vnc=<display>");
2156 exit(1);
2158 } else {
2159 parse_display_qapi(p);
2163 char *qemu_find_file(int type, const char *name)
2165 int i;
2166 const char *subdir;
2167 char *buf;
2169 /* Try the name as a straight path first */
2170 if (access(name, R_OK) == 0) {
2171 trace_load_file(name, name);
2172 return g_strdup(name);
2175 switch (type) {
2176 case QEMU_FILE_TYPE_BIOS:
2177 subdir = "";
2178 break;
2179 case QEMU_FILE_TYPE_KEYMAP:
2180 subdir = "keymaps/";
2181 break;
2182 default:
2183 abort();
2186 for (i = 0; i < data_dir_idx; i++) {
2187 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2188 if (access(buf, R_OK) == 0) {
2189 trace_load_file(name, buf);
2190 return buf;
2192 g_free(buf);
2194 return NULL;
2197 static void qemu_add_data_dir(const char *path)
2199 int i;
2201 if (path == NULL) {
2202 return;
2204 if (data_dir_idx == ARRAY_SIZE(data_dir)) {
2205 return;
2207 for (i = 0; i < data_dir_idx; i++) {
2208 if (strcmp(data_dir[i], path) == 0) {
2209 return; /* duplicate */
2212 data_dir[data_dir_idx++] = g_strdup(path);
2215 static inline bool nonempty_str(const char *str)
2217 return str && *str;
2220 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
2222 gchar *buf;
2223 size_t size;
2224 const char *name, *file, *str;
2225 FWCfgState *fw_cfg = (FWCfgState *) opaque;
2227 if (fw_cfg == NULL) {
2228 error_setg(errp, "fw_cfg device not available");
2229 return -1;
2231 name = qemu_opt_get(opts, "name");
2232 file = qemu_opt_get(opts, "file");
2233 str = qemu_opt_get(opts, "string");
2235 /* we need name and either a file or the content string */
2236 if (!(nonempty_str(name) && (nonempty_str(file) || nonempty_str(str)))) {
2237 error_setg(errp, "invalid argument(s)");
2238 return -1;
2240 if (nonempty_str(file) && nonempty_str(str)) {
2241 error_setg(errp, "file and string are mutually exclusive");
2242 return -1;
2244 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
2245 error_setg(errp, "name too long (max. %d char)",
2246 FW_CFG_MAX_FILE_PATH - 1);
2247 return -1;
2249 if (strncmp(name, "opt/", 4) != 0) {
2250 warn_report("externally provided fw_cfg item names "
2251 "should be prefixed with \"opt/\"");
2253 if (nonempty_str(str)) {
2254 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
2255 buf = g_memdup(str, size);
2256 } else {
2257 if (!g_file_get_contents(file, &buf, &size, NULL)) {
2258 error_setg(errp, "can't load %s", file);
2259 return -1;
2262 /* For legacy, keep user files in a specific global order. */
2263 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
2264 fw_cfg_add_file(fw_cfg, name, buf, size);
2265 fw_cfg_reset_order_override(fw_cfg);
2266 return 0;
2269 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
2271 return qdev_device_help(opts);
2274 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
2276 DeviceState *dev;
2278 dev = qdev_device_add(opts, errp);
2279 if (!dev) {
2280 return -1;
2282 object_unref(OBJECT(dev));
2283 return 0;
2286 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2288 Error *local_err = NULL;
2290 if (!qemu_chr_new_from_opts(opts, &local_err)) {
2291 if (local_err) {
2292 error_propagate(errp, local_err);
2293 return -1;
2295 exit(0);
2297 return 0;
2300 #ifdef CONFIG_VIRTFS
2301 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2303 return qemu_fsdev_add(opts, errp);
2305 #endif
2307 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
2309 Chardev *chr;
2310 const char *chardev;
2311 const char *mode;
2312 int flags;
2314 mode = qemu_opt_get(opts, "mode");
2315 if (mode == NULL) {
2316 mode = "readline";
2318 if (strcmp(mode, "readline") == 0) {
2319 flags = MONITOR_USE_READLINE;
2320 } else if (strcmp(mode, "control") == 0) {
2321 flags = MONITOR_USE_CONTROL;
2322 } else {
2323 error_setg(errp, "unknown monitor mode \"%s\"", mode);
2324 return -1;
2327 if (qemu_opt_get_bool(opts, "pretty", 0))
2328 flags |= MONITOR_USE_PRETTY;
2330 /* OOB is off by default */
2331 if (qemu_opt_get_bool(opts, "x-oob", 0)) {
2332 flags |= MONITOR_USE_OOB;
2335 chardev = qemu_opt_get(opts, "chardev");
2336 if (!chardev) {
2337 error_report("chardev is required");
2338 exit(1);
2340 chr = qemu_chr_find(chardev);
2341 if (chr == NULL) {
2342 error_setg(errp, "chardev \"%s\" not found", chardev);
2343 return -1;
2346 monitor_init(chr, flags);
2347 return 0;
2350 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
2352 static int monitor_device_index = 0;
2353 QemuOpts *opts;
2354 const char *p;
2355 char label[32];
2357 if (strstart(optarg, "chardev:", &p)) {
2358 snprintf(label, sizeof(label), "%s", p);
2359 } else {
2360 snprintf(label, sizeof(label), "compat_monitor%d",
2361 monitor_device_index);
2362 opts = qemu_chr_parse_compat(label, optarg, true);
2363 if (!opts) {
2364 error_report("parse error: %s", optarg);
2365 exit(1);
2369 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
2370 qemu_opt_set(opts, "mode", mode, &error_abort);
2371 qemu_opt_set(opts, "chardev", label, &error_abort);
2372 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
2373 monitor_device_index++;
2376 struct device_config {
2377 enum {
2378 DEV_USB, /* -usbdevice */
2379 DEV_BT, /* -bt */
2380 DEV_SERIAL, /* -serial */
2381 DEV_PARALLEL, /* -parallel */
2382 DEV_VIRTCON, /* -virtioconsole */
2383 DEV_DEBUGCON, /* -debugcon */
2384 DEV_GDB, /* -gdb, -s */
2385 DEV_SCLP, /* s390 sclp */
2386 } type;
2387 const char *cmdline;
2388 Location loc;
2389 QTAILQ_ENTRY(device_config) next;
2392 static QTAILQ_HEAD(, device_config) device_configs =
2393 QTAILQ_HEAD_INITIALIZER(device_configs);
2395 static void add_device_config(int type, const char *cmdline)
2397 struct device_config *conf;
2399 conf = g_malloc0(sizeof(*conf));
2400 conf->type = type;
2401 conf->cmdline = cmdline;
2402 loc_save(&conf->loc);
2403 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2406 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2408 struct device_config *conf;
2409 int rc;
2411 QTAILQ_FOREACH(conf, &device_configs, next) {
2412 if (conf->type != type)
2413 continue;
2414 loc_push_restore(&conf->loc);
2415 rc = func(conf->cmdline);
2416 loc_pop(&conf->loc);
2417 if (rc) {
2418 return rc;
2421 return 0;
2424 static int serial_parse(const char *devname)
2426 int index = num_serial_hds;
2427 char label[32];
2429 if (strcmp(devname, "none") == 0)
2430 return 0;
2431 snprintf(label, sizeof(label), "serial%d", index);
2432 serial_hds = g_renew(Chardev *, serial_hds, index + 1);
2434 serial_hds[index] = qemu_chr_new_mux_mon(label, devname);
2435 if (!serial_hds[index]) {
2436 error_report("could not connect serial device"
2437 " to character backend '%s'", devname);
2438 return -1;
2440 num_serial_hds++;
2441 return 0;
2444 Chardev *serial_hd(int i)
2446 assert(i >= 0);
2447 if (i < num_serial_hds) {
2448 return serial_hds[i];
2450 return NULL;
2453 int serial_max_hds(void)
2455 return num_serial_hds;
2458 static int parallel_parse(const char *devname)
2460 static int index = 0;
2461 char label[32];
2463 if (strcmp(devname, "none") == 0)
2464 return 0;
2465 if (index == MAX_PARALLEL_PORTS) {
2466 error_report("too many parallel ports");
2467 exit(1);
2469 snprintf(label, sizeof(label), "parallel%d", index);
2470 parallel_hds[index] = qemu_chr_new_mux_mon(label, devname);
2471 if (!parallel_hds[index]) {
2472 error_report("could not connect parallel device"
2473 " to character backend '%s'", devname);
2474 return -1;
2476 index++;
2477 return 0;
2480 static int virtcon_parse(const char *devname)
2482 QemuOptsList *device = qemu_find_opts("device");
2483 static int index = 0;
2484 char label[32];
2485 QemuOpts *bus_opts, *dev_opts;
2487 if (strcmp(devname, "none") == 0)
2488 return 0;
2489 if (index == MAX_VIRTIO_CONSOLES) {
2490 error_report("too many virtio consoles");
2491 exit(1);
2494 bus_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2495 qemu_opt_set(bus_opts, "driver", "virtio-serial", &error_abort);
2497 dev_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2498 qemu_opt_set(dev_opts, "driver", "virtconsole", &error_abort);
2500 snprintf(label, sizeof(label), "virtcon%d", index);
2501 virtcon_hds[index] = qemu_chr_new_mux_mon(label, devname);
2502 if (!virtcon_hds[index]) {
2503 error_report("could not connect virtio console"
2504 " to character backend '%s'", devname);
2505 return -1;
2507 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2509 index++;
2510 return 0;
2513 static int debugcon_parse(const char *devname)
2515 QemuOpts *opts;
2517 if (!qemu_chr_new_mux_mon("debugcon", devname)) {
2518 error_report("invalid character backend '%s'", devname);
2519 exit(1);
2521 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2522 if (!opts) {
2523 error_report("already have a debugcon device");
2524 exit(1);
2526 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
2527 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
2528 return 0;
2531 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
2533 const MachineClass *mc1 = a, *mc2 = b;
2534 int res;
2536 if (mc1->family == NULL) {
2537 if (mc2->family == NULL) {
2538 /* Compare standalone machine types against each other; they sort
2539 * in increasing order.
2541 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
2542 object_class_get_name(OBJECT_CLASS(mc2)));
2545 /* Standalone machine types sort after families. */
2546 return 1;
2549 if (mc2->family == NULL) {
2550 /* Families sort before standalone machine types. */
2551 return -1;
2554 /* Families sort between each other alphabetically increasingly. */
2555 res = strcmp(mc1->family, mc2->family);
2556 if (res != 0) {
2557 return res;
2560 /* Within the same family, machine types sort in decreasing order. */
2561 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
2562 object_class_get_name(OBJECT_CLASS(mc1)));
2565 static MachineClass *machine_parse(const char *name)
2567 MachineClass *mc = NULL;
2568 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
2570 if (name) {
2571 mc = find_machine(name);
2573 if (mc) {
2574 g_slist_free(machines);
2575 return mc;
2577 if (name && !is_help_option(name)) {
2578 error_report("unsupported machine type");
2579 error_printf("Use -machine help to list supported machines\n");
2580 } else {
2581 printf("Supported machines are:\n");
2582 machines = g_slist_sort(machines, machine_class_cmp);
2583 for (el = machines; el; el = el->next) {
2584 MachineClass *mc = el->data;
2585 if (mc->alias) {
2586 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
2588 printf("%-20s %s%s%s\n", mc->name, mc->desc,
2589 mc->is_default ? " (default)" : "",
2590 mc->deprecation_reason ? " (deprecated)" : "");
2594 g_slist_free(machines);
2595 exit(!name || !is_help_option(name));
2598 void qemu_add_exit_notifier(Notifier *notify)
2600 notifier_list_add(&exit_notifiers, notify);
2603 void qemu_remove_exit_notifier(Notifier *notify)
2605 notifier_remove(notify);
2608 static void qemu_run_exit_notifiers(void)
2610 notifier_list_notify(&exit_notifiers, NULL);
2613 static const char *pid_file;
2614 static Notifier qemu_unlink_pidfile_notifier;
2616 static void qemu_unlink_pidfile(Notifier *n, void *data)
2618 if (pid_file) {
2619 unlink(pid_file);
2623 bool machine_init_done;
2625 void qemu_add_machine_init_done_notifier(Notifier *notify)
2627 notifier_list_add(&machine_init_done_notifiers, notify);
2628 if (machine_init_done) {
2629 notify->notify(notify, NULL);
2633 void qemu_remove_machine_init_done_notifier(Notifier *notify)
2635 notifier_remove(notify);
2638 static void qemu_run_machine_init_done_notifiers(void)
2640 machine_init_done = true;
2641 notifier_list_notify(&machine_init_done_notifiers, NULL);
2644 static const QEMUOption *lookup_opt(int argc, char **argv,
2645 const char **poptarg, int *poptind)
2647 const QEMUOption *popt;
2648 int optind = *poptind;
2649 char *r = argv[optind];
2650 const char *optarg;
2652 loc_set_cmdline(argv, optind, 1);
2653 optind++;
2654 /* Treat --foo the same as -foo. */
2655 if (r[1] == '-')
2656 r++;
2657 popt = qemu_options;
2658 for(;;) {
2659 if (!popt->name) {
2660 error_report("invalid option");
2661 exit(1);
2663 if (!strcmp(popt->name, r + 1))
2664 break;
2665 popt++;
2667 if (popt->flags & HAS_ARG) {
2668 if (optind >= argc) {
2669 error_report("requires an argument");
2670 exit(1);
2672 optarg = argv[optind++];
2673 loc_set_cmdline(argv, optind - 2, 2);
2674 } else {
2675 optarg = NULL;
2678 *poptarg = optarg;
2679 *poptind = optind;
2681 return popt;
2684 static MachineClass *select_machine(void)
2686 MachineClass *machine_class = find_default_machine();
2687 const char *optarg;
2688 QemuOpts *opts;
2689 Location loc;
2691 loc_push_none(&loc);
2693 opts = qemu_get_machine_opts();
2694 qemu_opts_loc_restore(opts);
2696 optarg = qemu_opt_get(opts, "type");
2697 if (optarg) {
2698 machine_class = machine_parse(optarg);
2701 if (!machine_class) {
2702 error_report("No machine specified, and there is no default");
2703 error_printf("Use -machine help to list supported machines\n");
2704 exit(1);
2707 loc_pop(&loc);
2708 return machine_class;
2711 static int machine_set_property(void *opaque,
2712 const char *name, const char *value,
2713 Error **errp)
2715 Object *obj = OBJECT(opaque);
2716 Error *local_err = NULL;
2717 char *p, *qom_name;
2719 if (strcmp(name, "type") == 0) {
2720 return 0;
2723 qom_name = g_strdup(name);
2724 for (p = qom_name; *p; p++) {
2725 if (*p == '_') {
2726 *p = '-';
2730 object_property_parse(obj, value, qom_name, &local_err);
2731 g_free(qom_name);
2733 if (local_err) {
2734 error_propagate(errp, local_err);
2735 return -1;
2738 return 0;
2743 * Initial object creation happens before all other
2744 * QEMU data types are created. The majority of objects
2745 * can be created at this point. The rng-egd object
2746 * cannot be created here, as it depends on the chardev
2747 * already existing.
2749 static bool object_create_initial(const char *type, QemuOpts *opts)
2751 ObjectClass *klass;
2753 if (is_help_option(type)) {
2754 GSList *l, *list;
2756 printf("List of user creatable objects:\n");
2757 list = object_class_get_list_sorted(TYPE_USER_CREATABLE, false);
2758 for (l = list; l != NULL; l = l->next) {
2759 ObjectClass *oc = OBJECT_CLASS(l->data);
2760 printf(" %s\n", object_class_get_name(oc));
2762 g_slist_free(list);
2763 exit(0);
2766 klass = object_class_by_name(type);
2767 if (klass && qemu_opt_has_help_opt(opts)) {
2768 ObjectPropertyIterator iter;
2769 ObjectProperty *prop;
2770 GPtrArray *array = g_ptr_array_new();
2771 int i;
2773 object_class_property_iter_init(&iter, klass);
2774 while ((prop = object_property_iter_next(&iter))) {
2775 GString *str;
2777 if (!prop->set) {
2778 continue;
2781 str = g_string_new(NULL);
2782 g_string_append_printf(str, " %s=<%s>", prop->name, prop->type);
2783 if (prop->description) {
2784 if (str->len < 24) {
2785 g_string_append_printf(str, "%*s", 24 - (int)str->len, "");
2787 g_string_append_printf(str, " - %s", prop->description);
2789 g_ptr_array_add(array, g_string_free(str, false));
2791 g_ptr_array_sort(array, (GCompareFunc)qemu_pstrcmp0);
2792 if (array->len > 0) {
2793 printf("%s options:\n", type);
2794 } else {
2795 printf("There are no options for %s.\n", type);
2797 for (i = 0; i < array->len; i++) {
2798 printf("%s\n", (char *)array->pdata[i]);
2800 g_ptr_array_set_free_func(array, g_free);
2801 g_ptr_array_free(array, true);
2802 exit(0);
2805 if (g_str_equal(type, "rng-egd") ||
2806 g_str_has_prefix(type, "pr-manager-")) {
2807 return false;
2810 #if defined(CONFIG_VHOST_USER) && defined(CONFIG_LINUX)
2811 if (g_str_equal(type, "cryptodev-vhost-user")) {
2812 return false;
2814 #endif
2817 * return false for concrete netfilters since
2818 * they depend on netdevs already existing
2820 if (g_str_equal(type, "filter-buffer") ||
2821 g_str_equal(type, "filter-dump") ||
2822 g_str_equal(type, "filter-mirror") ||
2823 g_str_equal(type, "filter-redirector") ||
2824 g_str_equal(type, "colo-compare") ||
2825 g_str_equal(type, "filter-rewriter") ||
2826 g_str_equal(type, "filter-replay")) {
2827 return false;
2830 /* Memory allocation by backends needs to be done
2831 * after configure_accelerator() (due to the tcg_enabled()
2832 * checks at memory_region_init_*()).
2834 * Also, allocation of large amounts of memory may delay
2835 * chardev initialization for too long, and trigger timeouts
2836 * on software that waits for a monitor socket to be created
2837 * (e.g. libvirt).
2839 if (g_str_has_prefix(type, "memory-backend-")) {
2840 return false;
2843 return true;
2848 * The remainder of object creation happens after the
2849 * creation of chardev, fsdev, net clients and device data types.
2851 static bool object_create_delayed(const char *type, QemuOpts *opts)
2853 return !object_create_initial(type, opts);
2857 static void set_memory_options(uint64_t *ram_slots, ram_addr_t *maxram_size,
2858 MachineClass *mc)
2860 uint64_t sz;
2861 const char *mem_str;
2862 const ram_addr_t default_ram_size = mc->default_ram_size;
2863 QemuOpts *opts = qemu_find_opts_singleton("memory");
2864 Location loc;
2866 loc_push_none(&loc);
2867 qemu_opts_loc_restore(opts);
2869 sz = 0;
2870 mem_str = qemu_opt_get(opts, "size");
2871 if (mem_str) {
2872 if (!*mem_str) {
2873 error_report("missing 'size' option value");
2874 exit(EXIT_FAILURE);
2877 sz = qemu_opt_get_size(opts, "size", ram_size);
2879 /* Fix up legacy suffix-less format */
2880 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2881 uint64_t overflow_check = sz;
2883 sz *= MiB;
2884 if (sz / MiB != overflow_check) {
2885 error_report("too large 'size' option value");
2886 exit(EXIT_FAILURE);
2891 /* backward compatibility behaviour for case "-m 0" */
2892 if (sz == 0) {
2893 sz = default_ram_size;
2896 sz = QEMU_ALIGN_UP(sz, 8192);
2897 ram_size = sz;
2898 if (ram_size != sz) {
2899 error_report("ram size too large");
2900 exit(EXIT_FAILURE);
2903 /* store value for the future use */
2904 qemu_opt_set_number(opts, "size", ram_size, &error_abort);
2905 *maxram_size = ram_size;
2907 if (qemu_opt_get(opts, "maxmem")) {
2908 uint64_t slots;
2910 sz = qemu_opt_get_size(opts, "maxmem", 0);
2911 slots = qemu_opt_get_number(opts, "slots", 0);
2912 if (sz < ram_size) {
2913 error_report("invalid value of -m option maxmem: "
2914 "maximum memory size (0x%" PRIx64 ") must be at least "
2915 "the initial memory size (0x" RAM_ADDR_FMT ")",
2916 sz, ram_size);
2917 exit(EXIT_FAILURE);
2918 } else if (slots && sz == ram_size) {
2919 error_report("invalid value of -m option maxmem: "
2920 "memory slots were specified but maximum memory size "
2921 "(0x%" PRIx64 ") is equal to the initial memory size "
2922 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
2923 exit(EXIT_FAILURE);
2926 *maxram_size = sz;
2927 *ram_slots = slots;
2928 } else if (qemu_opt_get(opts, "slots")) {
2929 error_report("invalid -m option value: missing 'maxmem' option");
2930 exit(EXIT_FAILURE);
2933 loc_pop(&loc);
2936 static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
2938 GlobalProperty *g;
2940 g = g_malloc0(sizeof(*g));
2941 g->driver = qemu_opt_get(opts, "driver");
2942 g->property = qemu_opt_get(opts, "property");
2943 g->value = qemu_opt_get(opts, "value");
2944 g->user_provided = true;
2945 g->errp = &error_fatal;
2946 qdev_prop_register_global(g);
2947 return 0;
2950 static int qemu_read_default_config_file(void)
2952 int ret;
2954 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
2955 if (ret < 0 && ret != -ENOENT) {
2956 return ret;
2959 return 0;
2962 static void user_register_global_props(void)
2964 qemu_opts_foreach(qemu_find_opts("global"),
2965 global_init_func, NULL, NULL);
2969 * Note: we should see that these properties are actually having a
2970 * priority: accel < machine < user. This means e.g. when user
2971 * specifies something in "-global", it'll always be used with highest
2972 * priority than either machine/accelerator compat properties.
2974 static void register_global_properties(MachineState *ms)
2976 accel_register_compat_props(ms->accelerator);
2977 machine_register_compat_props(ms);
2978 user_register_global_props();
2981 int main(int argc, char **argv)
2983 int i;
2984 int snapshot, linux_boot;
2985 const char *initrd_filename;
2986 const char *kernel_filename, *kernel_cmdline;
2987 const char *boot_order = NULL;
2988 const char *boot_once = NULL;
2989 DisplayState *ds;
2990 QemuOpts *opts, *machine_opts;
2991 QemuOpts *icount_opts = NULL, *accel_opts = NULL;
2992 QemuOptsList *olist;
2993 int optind;
2994 const char *optarg;
2995 const char *loadvm = NULL;
2996 MachineClass *machine_class;
2997 const char *cpu_model;
2998 const char *vga_model = NULL;
2999 const char *qtest_chrdev = NULL;
3000 const char *qtest_log = NULL;
3001 const char *incoming = NULL;
3002 bool userconfig = true;
3003 bool nographic = false;
3004 int display_remote = 0;
3005 const char *log_mask = NULL;
3006 const char *log_file = NULL;
3007 char *trace_file = NULL;
3008 ram_addr_t maxram_size;
3009 uint64_t ram_slots = 0;
3010 FILE *vmstate_dump_file = NULL;
3011 Error *main_loop_err = NULL;
3012 Error *err = NULL;
3013 bool list_data_dirs = false;
3014 char *dir, **dirs;
3015 typedef struct BlockdevOptions_queue {
3016 BlockdevOptions *bdo;
3017 Location loc;
3018 QSIMPLEQ_ENTRY(BlockdevOptions_queue) entry;
3019 } BlockdevOptions_queue;
3020 QSIMPLEQ_HEAD(, BlockdevOptions_queue) bdo_queue
3021 = QSIMPLEQ_HEAD_INITIALIZER(bdo_queue);
3023 module_call_init(MODULE_INIT_TRACE);
3025 qemu_init_cpu_list();
3026 qemu_init_cpu_loop();
3028 qemu_mutex_lock_iothread();
3030 atexit(qemu_run_exit_notifiers);
3031 error_set_progname(argv[0]);
3032 qemu_init_exec_dir(argv[0]);
3034 module_call_init(MODULE_INIT_QOM);
3036 qemu_add_opts(&qemu_drive_opts);
3037 qemu_add_drive_opts(&qemu_legacy_drive_opts);
3038 qemu_add_drive_opts(&qemu_common_drive_opts);
3039 qemu_add_drive_opts(&qemu_drive_opts);
3040 qemu_add_drive_opts(&bdrv_runtime_opts);
3041 qemu_add_opts(&qemu_chardev_opts);
3042 qemu_add_opts(&qemu_device_opts);
3043 qemu_add_opts(&qemu_netdev_opts);
3044 qemu_add_opts(&qemu_nic_opts);
3045 qemu_add_opts(&qemu_net_opts);
3046 qemu_add_opts(&qemu_rtc_opts);
3047 qemu_add_opts(&qemu_global_opts);
3048 qemu_add_opts(&qemu_mon_opts);
3049 qemu_add_opts(&qemu_trace_opts);
3050 qemu_add_opts(&qemu_option_rom_opts);
3051 qemu_add_opts(&qemu_machine_opts);
3052 qemu_add_opts(&qemu_accel_opts);
3053 qemu_add_opts(&qemu_mem_opts);
3054 qemu_add_opts(&qemu_smp_opts);
3055 qemu_add_opts(&qemu_boot_opts);
3056 qemu_add_opts(&qemu_add_fd_opts);
3057 qemu_add_opts(&qemu_object_opts);
3058 qemu_add_opts(&qemu_tpmdev_opts);
3059 qemu_add_opts(&qemu_realtime_opts);
3060 qemu_add_opts(&qemu_overcommit_opts);
3061 qemu_add_opts(&qemu_msg_opts);
3062 qemu_add_opts(&qemu_name_opts);
3063 qemu_add_opts(&qemu_numa_opts);
3064 qemu_add_opts(&qemu_icount_opts);
3065 qemu_add_opts(&qemu_semihosting_config_opts);
3066 qemu_add_opts(&qemu_fw_cfg_opts);
3067 module_call_init(MODULE_INIT_OPTS);
3069 runstate_init();
3070 postcopy_infrastructure_init();
3071 monitor_init_globals();
3073 if (qcrypto_init(&err) < 0) {
3074 error_reportf_err(err, "cannot initialize crypto: ");
3075 exit(1);
3078 QLIST_INIT (&vm_change_state_head);
3079 os_setup_early_signal_handling();
3081 cpu_model = NULL;
3082 snapshot = 0;
3084 nb_nics = 0;
3086 bdrv_init_with_whitelist();
3088 autostart = 1;
3090 /* first pass of option parsing */
3091 optind = 1;
3092 while (optind < argc) {
3093 if (argv[optind][0] != '-') {
3094 /* disk image */
3095 optind++;
3096 } else {
3097 const QEMUOption *popt;
3099 popt = lookup_opt(argc, argv, &optarg, &optind);
3100 switch (popt->index) {
3101 case QEMU_OPTION_nouserconfig:
3102 userconfig = false;
3103 break;
3108 if (userconfig) {
3109 if (qemu_read_default_config_file() < 0) {
3110 exit(1);
3114 /* second pass of option parsing */
3115 optind = 1;
3116 for(;;) {
3117 if (optind >= argc)
3118 break;
3119 if (argv[optind][0] != '-') {
3120 drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
3121 } else {
3122 const QEMUOption *popt;
3124 popt = lookup_opt(argc, argv, &optarg, &optind);
3125 if (!(popt->arch_mask & arch_type)) {
3126 error_report("Option not supported for this target");
3127 exit(1);
3129 switch(popt->index) {
3130 case QEMU_OPTION_cpu:
3131 /* hw initialization will check this */
3132 cpu_model = optarg;
3133 break;
3134 case QEMU_OPTION_hda:
3135 case QEMU_OPTION_hdb:
3136 case QEMU_OPTION_hdc:
3137 case QEMU_OPTION_hdd:
3138 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
3139 HD_OPTS);
3140 break;
3141 case QEMU_OPTION_blockdev:
3143 Visitor *v;
3144 BlockdevOptions_queue *bdo;
3146 v = qobject_input_visitor_new_str(optarg, "driver", &err);
3147 if (!v) {
3148 error_report_err(err);
3149 exit(1);
3152 bdo = g_new(BlockdevOptions_queue, 1);
3153 visit_type_BlockdevOptions(v, NULL, &bdo->bdo,
3154 &error_fatal);
3155 visit_free(v);
3156 loc_save(&bdo->loc);
3157 QSIMPLEQ_INSERT_TAIL(&bdo_queue, bdo, entry);
3158 break;
3160 case QEMU_OPTION_drive:
3161 if (drive_def(optarg) == NULL) {
3162 exit(1);
3164 break;
3165 case QEMU_OPTION_set:
3166 if (qemu_set_option(optarg) != 0)
3167 exit(1);
3168 break;
3169 case QEMU_OPTION_global:
3170 if (qemu_global_option(optarg) != 0)
3171 exit(1);
3172 break;
3173 case QEMU_OPTION_mtdblock:
3174 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3175 break;
3176 case QEMU_OPTION_sd:
3177 drive_add(IF_SD, -1, optarg, SD_OPTS);
3178 break;
3179 case QEMU_OPTION_pflash:
3180 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
3181 break;
3182 case QEMU_OPTION_snapshot:
3183 snapshot = 1;
3184 break;
3185 case QEMU_OPTION_numa:
3186 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
3187 optarg, true);
3188 if (!opts) {
3189 exit(1);
3191 break;
3192 case QEMU_OPTION_display:
3193 parse_display(optarg);
3194 break;
3195 case QEMU_OPTION_nographic:
3196 olist = qemu_find_opts("machine");
3197 qemu_opts_parse_noisily(olist, "graphics=off", false);
3198 nographic = true;
3199 dpy.type = DISPLAY_TYPE_NONE;
3200 break;
3201 case QEMU_OPTION_curses:
3202 #ifdef CONFIG_CURSES
3203 dpy.type = DISPLAY_TYPE_CURSES;
3204 #else
3205 error_report("curses support is disabled");
3206 exit(1);
3207 #endif
3208 break;
3209 case QEMU_OPTION_portrait:
3210 graphic_rotate = 90;
3211 break;
3212 case QEMU_OPTION_rotate:
3213 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3214 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3215 graphic_rotate != 180 && graphic_rotate != 270) {
3216 error_report("only 90, 180, 270 deg rotation is available");
3217 exit(1);
3219 break;
3220 case QEMU_OPTION_kernel:
3221 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg,
3222 &error_abort);
3223 break;
3224 case QEMU_OPTION_initrd:
3225 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg,
3226 &error_abort);
3227 break;
3228 case QEMU_OPTION_append:
3229 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg,
3230 &error_abort);
3231 break;
3232 case QEMU_OPTION_dtb:
3233 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg,
3234 &error_abort);
3235 break;
3236 case QEMU_OPTION_cdrom:
3237 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3238 break;
3239 case QEMU_OPTION_boot:
3240 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
3241 optarg, true);
3242 if (!opts) {
3243 exit(1);
3245 break;
3246 case QEMU_OPTION_fda:
3247 case QEMU_OPTION_fdb:
3248 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3249 optarg, FD_OPTS);
3250 break;
3251 case QEMU_OPTION_no_fd_bootchk:
3252 fd_bootchk = 0;
3253 break;
3254 case QEMU_OPTION_netdev:
3255 default_net = 0;
3256 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3257 exit(1);
3259 break;
3260 case QEMU_OPTION_nic:
3261 default_net = 0;
3262 if (net_client_parse(qemu_find_opts("nic"), optarg) == -1) {
3263 exit(1);
3265 break;
3266 case QEMU_OPTION_net:
3267 default_net = 0;
3268 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3269 exit(1);
3271 break;
3272 #ifdef CONFIG_LIBISCSI
3273 case QEMU_OPTION_iscsi:
3274 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
3275 optarg, false);
3276 if (!opts) {
3277 exit(1);
3279 break;
3280 #endif
3281 case QEMU_OPTION_bt:
3282 warn_report("The bluetooth subsystem is deprecated and will "
3283 "be removed soon. If the bluetooth subsystem is "
3284 "still useful for you, please send a mail to "
3285 "qemu-devel@nongnu.org with your usecase.");
3286 add_device_config(DEV_BT, optarg);
3287 break;
3288 case QEMU_OPTION_audio_help:
3289 AUD_help ();
3290 exit (0);
3291 break;
3292 case QEMU_OPTION_soundhw:
3293 select_soundhw (optarg);
3294 break;
3295 case QEMU_OPTION_h:
3296 help(0);
3297 break;
3298 case QEMU_OPTION_version:
3299 version();
3300 exit(0);
3301 break;
3302 case QEMU_OPTION_m:
3303 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
3304 optarg, true);
3305 if (!opts) {
3306 exit(EXIT_FAILURE);
3308 break;
3309 #ifdef CONFIG_TPM
3310 case QEMU_OPTION_tpmdev:
3311 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3312 exit(1);
3314 break;
3315 #endif
3316 case QEMU_OPTION_mempath:
3317 mem_path = optarg;
3318 break;
3319 case QEMU_OPTION_mem_prealloc:
3320 mem_prealloc = 1;
3321 break;
3322 case QEMU_OPTION_d:
3323 log_mask = optarg;
3324 break;
3325 case QEMU_OPTION_D:
3326 log_file = optarg;
3327 break;
3328 case QEMU_OPTION_DFILTER:
3329 qemu_set_dfilter_ranges(optarg, &error_fatal);
3330 break;
3331 case QEMU_OPTION_s:
3332 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3333 break;
3334 case QEMU_OPTION_gdb:
3335 add_device_config(DEV_GDB, optarg);
3336 break;
3337 case QEMU_OPTION_L:
3338 if (is_help_option(optarg)) {
3339 list_data_dirs = true;
3340 } else {
3341 qemu_add_data_dir(optarg);
3343 break;
3344 case QEMU_OPTION_bios:
3345 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg,
3346 &error_abort);
3347 break;
3348 case QEMU_OPTION_singlestep:
3349 singlestep = 1;
3350 break;
3351 case QEMU_OPTION_S:
3352 autostart = 0;
3353 break;
3354 case QEMU_OPTION_k:
3355 keyboard_layout = optarg;
3356 break;
3357 case QEMU_OPTION_vga:
3358 vga_model = optarg;
3359 default_vga = 0;
3360 break;
3361 case QEMU_OPTION_g:
3363 const char *p;
3364 int w, h, depth;
3365 p = optarg;
3366 w = strtol(p, (char **)&p, 10);
3367 if (w <= 0) {
3368 graphic_error:
3369 error_report("invalid resolution or depth");
3370 exit(1);
3372 if (*p != 'x')
3373 goto graphic_error;
3374 p++;
3375 h = strtol(p, (char **)&p, 10);
3376 if (h <= 0)
3377 goto graphic_error;
3378 if (*p == 'x') {
3379 p++;
3380 depth = strtol(p, (char **)&p, 10);
3381 if (depth != 8 && depth != 15 && depth != 16 &&
3382 depth != 24 && depth != 32)
3383 goto graphic_error;
3384 } else if (*p == '\0') {
3385 depth = graphic_depth;
3386 } else {
3387 goto graphic_error;
3390 graphic_width = w;
3391 graphic_height = h;
3392 graphic_depth = depth;
3394 break;
3395 case QEMU_OPTION_echr:
3397 char *r;
3398 term_escape_char = strtol(optarg, &r, 0);
3399 if (r == optarg)
3400 printf("Bad argument to echr\n");
3401 break;
3403 case QEMU_OPTION_monitor:
3404 default_monitor = 0;
3405 if (strncmp(optarg, "none", 4)) {
3406 monitor_parse(optarg, "readline", false);
3408 break;
3409 case QEMU_OPTION_qmp:
3410 monitor_parse(optarg, "control", false);
3411 default_monitor = 0;
3412 break;
3413 case QEMU_OPTION_qmp_pretty:
3414 monitor_parse(optarg, "control", true);
3415 default_monitor = 0;
3416 break;
3417 case QEMU_OPTION_mon:
3418 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3419 true);
3420 if (!opts) {
3421 exit(1);
3423 default_monitor = 0;
3424 break;
3425 case QEMU_OPTION_chardev:
3426 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3427 optarg, true);
3428 if (!opts) {
3429 exit(1);
3431 break;
3432 case QEMU_OPTION_fsdev:
3433 olist = qemu_find_opts("fsdev");
3434 if (!olist) {
3435 error_report("fsdev support is disabled");
3436 exit(1);
3438 opts = qemu_opts_parse_noisily(olist, optarg, true);
3439 if (!opts) {
3440 exit(1);
3442 break;
3443 case QEMU_OPTION_virtfs: {
3444 QemuOpts *fsdev;
3445 QemuOpts *device;
3446 const char *writeout, *sock_fd, *socket, *path, *security_model;
3448 olist = qemu_find_opts("virtfs");
3449 if (!olist) {
3450 error_report("virtfs support is disabled");
3451 exit(1);
3453 opts = qemu_opts_parse_noisily(olist, optarg, true);
3454 if (!opts) {
3455 exit(1);
3458 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3459 qemu_opt_get(opts, "mount_tag") == NULL) {
3460 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3461 exit(1);
3463 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3464 qemu_opts_id(opts) ?:
3465 qemu_opt_get(opts, "mount_tag"),
3466 1, NULL);
3467 if (!fsdev) {
3468 error_report("duplicate or invalid fsdev id: %s",
3469 qemu_opt_get(opts, "mount_tag"));
3470 exit(1);
3473 writeout = qemu_opt_get(opts, "writeout");
3474 if (writeout) {
3475 #ifdef CONFIG_SYNC_FILE_RANGE
3476 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3477 #else
3478 error_report("writeout=immediate not supported "
3479 "on this platform");
3480 exit(1);
3481 #endif
3483 qemu_opt_set(fsdev, "fsdriver",
3484 qemu_opt_get(opts, "fsdriver"), &error_abort);
3485 path = qemu_opt_get(opts, "path");
3486 if (path) {
3487 qemu_opt_set(fsdev, "path", path, &error_abort);
3489 security_model = qemu_opt_get(opts, "security_model");
3490 if (security_model) {
3491 qemu_opt_set(fsdev, "security_model", security_model,
3492 &error_abort);
3494 socket = qemu_opt_get(opts, "socket");
3495 if (socket) {
3496 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3498 sock_fd = qemu_opt_get(opts, "sock_fd");
3499 if (sock_fd) {
3500 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3503 qemu_opt_set_bool(fsdev, "readonly",
3504 qemu_opt_get_bool(opts, "readonly", 0),
3505 &error_abort);
3506 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3507 &error_abort);
3508 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3509 qemu_opt_set(device, "fsdev",
3510 qemu_opts_id(fsdev), &error_abort);
3511 qemu_opt_set(device, "mount_tag",
3512 qemu_opt_get(opts, "mount_tag"), &error_abort);
3513 break;
3515 case QEMU_OPTION_virtfs_synth: {
3516 QemuOpts *fsdev;
3517 QemuOpts *device;
3519 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3520 1, NULL);
3521 if (!fsdev) {
3522 error_report("duplicate option: %s", "virtfs_synth");
3523 exit(1);
3525 qemu_opt_set(fsdev, "fsdriver", "synth", &error_abort);
3527 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3528 &error_abort);
3529 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3530 qemu_opt_set(device, "fsdev", "v_synth", &error_abort);
3531 qemu_opt_set(device, "mount_tag", "v_synth", &error_abort);
3532 break;
3534 case QEMU_OPTION_serial:
3535 add_device_config(DEV_SERIAL, optarg);
3536 default_serial = 0;
3537 if (strncmp(optarg, "mon:", 4) == 0) {
3538 default_monitor = 0;
3540 break;
3541 case QEMU_OPTION_watchdog:
3542 if (watchdog) {
3543 error_report("only one watchdog option may be given");
3544 return 1;
3546 watchdog = optarg;
3547 break;
3548 case QEMU_OPTION_watchdog_action:
3549 if (select_watchdog_action(optarg) == -1) {
3550 error_report("unknown -watchdog-action parameter");
3551 exit(1);
3553 break;
3554 case QEMU_OPTION_virtiocon:
3555 warn_report("This option is deprecated, "
3556 "use '-device virtconsole' instead");
3557 add_device_config(DEV_VIRTCON, optarg);
3558 default_virtcon = 0;
3559 if (strncmp(optarg, "mon:", 4) == 0) {
3560 default_monitor = 0;
3562 break;
3563 case QEMU_OPTION_parallel:
3564 add_device_config(DEV_PARALLEL, optarg);
3565 default_parallel = 0;
3566 if (strncmp(optarg, "mon:", 4) == 0) {
3567 default_monitor = 0;
3569 break;
3570 case QEMU_OPTION_debugcon:
3571 add_device_config(DEV_DEBUGCON, optarg);
3572 break;
3573 case QEMU_OPTION_loadvm:
3574 loadvm = optarg;
3575 break;
3576 case QEMU_OPTION_full_screen:
3577 dpy.has_full_screen = true;
3578 dpy.full_screen = true;
3579 break;
3580 case QEMU_OPTION_no_frame:
3581 g_printerr("The -no-frame switch is deprecated, and will be\n"
3582 "removed in a future release.\n");
3583 no_frame = 1;
3584 break;
3585 case QEMU_OPTION_alt_grab:
3586 alt_grab = 1;
3587 break;
3588 case QEMU_OPTION_ctrl_grab:
3589 ctrl_grab = 1;
3590 break;
3591 case QEMU_OPTION_no_quit:
3592 dpy.has_window_close = true;
3593 dpy.window_close = false;
3594 break;
3595 case QEMU_OPTION_sdl:
3596 #ifdef CONFIG_SDL
3597 dpy.type = DISPLAY_TYPE_SDL;
3598 break;
3599 #else
3600 error_report("SDL support is disabled");
3601 exit(1);
3602 #endif
3603 case QEMU_OPTION_pidfile:
3604 pid_file = optarg;
3605 break;
3606 case QEMU_OPTION_win2k_hack:
3607 win2k_install_hack = 1;
3608 break;
3609 case QEMU_OPTION_acpitable:
3610 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3611 optarg, true);
3612 if (!opts) {
3613 exit(1);
3615 acpi_table_add(opts, &error_fatal);
3616 break;
3617 case QEMU_OPTION_smbios:
3618 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3619 optarg, false);
3620 if (!opts) {
3621 exit(1);
3623 smbios_entry_add(opts, &error_fatal);
3624 break;
3625 case QEMU_OPTION_fwcfg:
3626 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3627 optarg, true);
3628 if (opts == NULL) {
3629 exit(1);
3631 break;
3632 case QEMU_OPTION_preconfig:
3633 preconfig_exit_requested = false;
3634 break;
3635 case QEMU_OPTION_enable_kvm:
3636 olist = qemu_find_opts("machine");
3637 qemu_opts_parse_noisily(olist, "accel=kvm", false);
3638 break;
3639 case QEMU_OPTION_enable_hax:
3640 warn_report("Option is deprecated, use '-accel hax' instead");
3641 olist = qemu_find_opts("machine");
3642 qemu_opts_parse_noisily(olist, "accel=hax", false);
3643 break;
3644 case QEMU_OPTION_M:
3645 case QEMU_OPTION_machine:
3646 olist = qemu_find_opts("machine");
3647 opts = qemu_opts_parse_noisily(olist, optarg, true);
3648 if (!opts) {
3649 exit(1);
3651 break;
3652 case QEMU_OPTION_no_kvm:
3653 olist = qemu_find_opts("machine");
3654 qemu_opts_parse_noisily(olist, "accel=tcg", false);
3655 break;
3656 case QEMU_OPTION_accel:
3657 accel_opts = qemu_opts_parse_noisily(qemu_find_opts("accel"),
3658 optarg, true);
3659 optarg = qemu_opt_get(accel_opts, "accel");
3660 if (!optarg || is_help_option(optarg)) {
3661 error_printf("Possible accelerators: kvm, xen, hax, tcg\n");
3662 exit(0);
3664 opts = qemu_opts_create(qemu_find_opts("machine"), NULL,
3665 false, &error_abort);
3666 qemu_opt_set(opts, "accel", optarg, &error_abort);
3667 break;
3668 case QEMU_OPTION_usb:
3669 olist = qemu_find_opts("machine");
3670 qemu_opts_parse_noisily(olist, "usb=on", false);
3671 break;
3672 case QEMU_OPTION_usbdevice:
3673 error_report("'-usbdevice' is deprecated, please use "
3674 "'-device usb-...' instead");
3675 olist = qemu_find_opts("machine");
3676 qemu_opts_parse_noisily(olist, "usb=on", false);
3677 add_device_config(DEV_USB, optarg);
3678 break;
3679 case QEMU_OPTION_device:
3680 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3681 optarg, true)) {
3682 exit(1);
3684 break;
3685 case QEMU_OPTION_smp:
3686 if (!qemu_opts_parse_noisily(qemu_find_opts("smp-opts"),
3687 optarg, true)) {
3688 exit(1);
3690 break;
3691 case QEMU_OPTION_vnc:
3692 vnc_parse(optarg, &error_fatal);
3693 break;
3694 case QEMU_OPTION_no_acpi:
3695 acpi_enabled = 0;
3696 break;
3697 case QEMU_OPTION_no_hpet:
3698 no_hpet = 1;
3699 break;
3700 case QEMU_OPTION_no_reboot:
3701 no_reboot = 1;
3702 break;
3703 case QEMU_OPTION_no_shutdown:
3704 no_shutdown = 1;
3705 break;
3706 case QEMU_OPTION_show_cursor:
3707 cursor_hide = 0;
3708 break;
3709 case QEMU_OPTION_uuid:
3710 if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
3711 error_report("failed to parse UUID string: wrong format");
3712 exit(1);
3714 qemu_uuid_set = true;
3715 break;
3716 case QEMU_OPTION_option_rom:
3717 if (nb_option_roms >= MAX_OPTION_ROMS) {
3718 error_report("too many option ROMs");
3719 exit(1);
3721 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3722 optarg, true);
3723 if (!opts) {
3724 exit(1);
3726 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3727 option_rom[nb_option_roms].bootindex =
3728 qemu_opt_get_number(opts, "bootindex", -1);
3729 if (!option_rom[nb_option_roms].name) {
3730 error_report("Option ROM file is not specified");
3731 exit(1);
3733 nb_option_roms++;
3734 break;
3735 case QEMU_OPTION_semihosting:
3736 semihosting.enabled = true;
3737 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3738 break;
3739 case QEMU_OPTION_semihosting_config:
3740 semihosting.enabled = true;
3741 opts = qemu_opts_parse_noisily(qemu_find_opts("semihosting-config"),
3742 optarg, false);
3743 if (opts != NULL) {
3744 semihosting.enabled = qemu_opt_get_bool(opts, "enable",
3745 true);
3746 const char *target = qemu_opt_get(opts, "target");
3747 if (target != NULL) {
3748 if (strcmp("native", target) == 0) {
3749 semihosting.target = SEMIHOSTING_TARGET_NATIVE;
3750 } else if (strcmp("gdb", target) == 0) {
3751 semihosting.target = SEMIHOSTING_TARGET_GDB;
3752 } else if (strcmp("auto", target) == 0) {
3753 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3754 } else {
3755 error_report("unsupported semihosting-config %s",
3756 optarg);
3757 exit(1);
3759 } else {
3760 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3762 /* Set semihosting argument count and vector */
3763 qemu_opt_foreach(opts, add_semihosting_arg,
3764 &semihosting, NULL);
3765 } else {
3766 error_report("unsupported semihosting-config %s", optarg);
3767 exit(1);
3769 break;
3770 case QEMU_OPTION_name:
3771 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3772 optarg, true);
3773 if (!opts) {
3774 exit(1);
3776 break;
3777 case QEMU_OPTION_prom_env:
3778 if (nb_prom_envs >= MAX_PROM_ENVS) {
3779 error_report("too many prom variables");
3780 exit(1);
3782 prom_envs[nb_prom_envs] = optarg;
3783 nb_prom_envs++;
3784 break;
3785 case QEMU_OPTION_old_param:
3786 old_param = 1;
3787 break;
3788 case QEMU_OPTION_clock:
3789 /* Clock options no longer exist. Keep this option for
3790 * backward compatibility.
3792 warn_report("This option is ignored and will be removed soon");
3793 break;
3794 case QEMU_OPTION_rtc:
3795 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3796 false);
3797 if (!opts) {
3798 exit(1);
3800 break;
3801 case QEMU_OPTION_tb_size:
3802 #ifndef CONFIG_TCG
3803 error_report("TCG is disabled");
3804 exit(1);
3805 #endif
3806 if (qemu_strtoul(optarg, NULL, 0, &tcg_tb_size) < 0) {
3807 error_report("Invalid argument to -tb-size");
3808 exit(1);
3810 break;
3811 case QEMU_OPTION_icount:
3812 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3813 optarg, true);
3814 if (!icount_opts) {
3815 exit(1);
3817 break;
3818 case QEMU_OPTION_incoming:
3819 if (!incoming) {
3820 runstate_set(RUN_STATE_INMIGRATE);
3822 incoming = optarg;
3823 break;
3824 case QEMU_OPTION_only_migratable:
3826 * TODO: we can remove this option one day, and we
3827 * should all use:
3829 * "-global migration.only-migratable=true"
3831 qemu_global_option("migration.only-migratable=true");
3832 break;
3833 case QEMU_OPTION_nodefaults:
3834 has_defaults = 0;
3835 break;
3836 case QEMU_OPTION_xen_domid:
3837 if (!(xen_available())) {
3838 error_report("Option not supported for this target");
3839 exit(1);
3841 xen_domid = atoi(optarg);
3842 break;
3843 case QEMU_OPTION_xen_create:
3844 if (!(xen_available())) {
3845 error_report("Option not supported for this target");
3846 exit(1);
3848 xen_mode = XEN_CREATE;
3849 break;
3850 case QEMU_OPTION_xen_attach:
3851 if (!(xen_available())) {
3852 error_report("Option not supported for this target");
3853 exit(1);
3855 xen_mode = XEN_ATTACH;
3856 break;
3857 case QEMU_OPTION_xen_domid_restrict:
3858 if (!(xen_available())) {
3859 error_report("Option not supported for this target");
3860 exit(1);
3862 xen_domid_restrict = true;
3863 break;
3864 case QEMU_OPTION_trace:
3865 g_free(trace_file);
3866 trace_file = trace_opt_parse(optarg);
3867 break;
3868 case QEMU_OPTION_trace_unassigned:
3869 trace_unassigned = true;
3870 break;
3871 case QEMU_OPTION_readconfig:
3873 int ret = qemu_read_config_file(optarg);
3874 if (ret < 0) {
3875 error_report("read config %s: %s", optarg,
3876 strerror(-ret));
3877 exit(1);
3879 break;
3881 case QEMU_OPTION_spice:
3882 olist = qemu_find_opts("spice");
3883 if (!olist) {
3884 error_report("spice support is disabled");
3885 exit(1);
3887 opts = qemu_opts_parse_noisily(olist, optarg, false);
3888 if (!opts) {
3889 exit(1);
3891 display_remote++;
3892 break;
3893 case QEMU_OPTION_writeconfig:
3895 FILE *fp;
3896 if (strcmp(optarg, "-") == 0) {
3897 fp = stdout;
3898 } else {
3899 fp = fopen(optarg, "w");
3900 if (fp == NULL) {
3901 error_report("open %s: %s", optarg,
3902 strerror(errno));
3903 exit(1);
3906 qemu_config_write(fp);
3907 if (fp != stdout) {
3908 fclose(fp);
3910 break;
3912 case QEMU_OPTION_qtest:
3913 qtest_chrdev = optarg;
3914 break;
3915 case QEMU_OPTION_qtest_log:
3916 qtest_log = optarg;
3917 break;
3918 case QEMU_OPTION_sandbox:
3919 #ifdef CONFIG_SECCOMP
3920 opts = qemu_opts_parse_noisily(qemu_find_opts("sandbox"),
3921 optarg, true);
3922 if (!opts) {
3923 exit(1);
3925 #else
3926 error_report("-sandbox support is not enabled "
3927 "in this QEMU binary");
3928 exit(1);
3929 #endif
3930 break;
3931 case QEMU_OPTION_add_fd:
3932 #ifndef _WIN32
3933 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
3934 optarg, false);
3935 if (!opts) {
3936 exit(1);
3938 #else
3939 error_report("File descriptor passing is disabled on this "
3940 "platform");
3941 exit(1);
3942 #endif
3943 break;
3944 case QEMU_OPTION_object:
3945 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
3946 optarg, true);
3947 if (!opts) {
3948 exit(1);
3950 break;
3951 case QEMU_OPTION_realtime:
3952 opts = qemu_opts_parse_noisily(qemu_find_opts("realtime"),
3953 optarg, false);
3954 if (!opts) {
3955 exit(1);
3957 /* Don't override the -overcommit option if set */
3958 enable_mlock = enable_mlock ||
3959 qemu_opt_get_bool(opts, "mlock", true);
3960 break;
3961 case QEMU_OPTION_overcommit:
3962 opts = qemu_opts_parse_noisily(qemu_find_opts("overcommit"),
3963 optarg, false);
3964 if (!opts) {
3965 exit(1);
3967 /* Don't override the -realtime option if set */
3968 enable_mlock = enable_mlock ||
3969 qemu_opt_get_bool(opts, "mem-lock", false);
3970 enable_cpu_pm = qemu_opt_get_bool(opts, "cpu-pm", false);
3971 break;
3972 case QEMU_OPTION_msg:
3973 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
3974 false);
3975 if (!opts) {
3976 exit(1);
3978 configure_msg(opts);
3979 break;
3980 case QEMU_OPTION_dump_vmstate:
3981 if (vmstate_dump_file) {
3982 error_report("only one '-dump-vmstate' "
3983 "option may be given");
3984 exit(1);
3986 vmstate_dump_file = fopen(optarg, "w");
3987 if (vmstate_dump_file == NULL) {
3988 error_report("open %s: %s", optarg, strerror(errno));
3989 exit(1);
3991 break;
3992 case QEMU_OPTION_enable_sync_profile:
3993 qsp_enable();
3994 break;
3995 case QEMU_OPTION_nouserconfig:
3996 /* Nothing to be parsed here. Especially, do not error out below. */
3997 break;
3998 default:
3999 if (os_parse_cmd_args(popt->index, optarg)) {
4000 error_report("Option not supported in this build");
4001 exit(1);
4007 * Clear error location left behind by the loop.
4008 * Best done right after the loop. Do not insert code here!
4010 loc_set_none();
4012 replay_configure(icount_opts);
4014 if (incoming && !preconfig_exit_requested) {
4015 error_report("'preconfig' and 'incoming' options are "
4016 "mutually exclusive");
4017 exit(EXIT_FAILURE);
4020 configure_rtc(qemu_find_opts_singleton("rtc"));
4022 machine_class = select_machine();
4024 set_memory_options(&ram_slots, &maxram_size, machine_class);
4026 os_daemonize();
4027 rcu_disable_atfork();
4029 if (pid_file && !qemu_write_pidfile(pid_file, &err)) {
4030 error_reportf_err(err, "cannot create PID file: ");
4031 exit(1);
4034 qemu_unlink_pidfile_notifier.notify = qemu_unlink_pidfile;
4035 qemu_add_exit_notifier(&qemu_unlink_pidfile_notifier);
4037 if (qemu_init_main_loop(&main_loop_err)) {
4038 error_report_err(main_loop_err);
4039 exit(1);
4042 #ifdef CONFIG_SECCOMP
4043 olist = qemu_find_opts_err("sandbox", NULL);
4044 if (olist) {
4045 qemu_opts_foreach(olist, parse_sandbox, NULL, &error_fatal);
4047 #endif
4049 qemu_opts_foreach(qemu_find_opts("name"),
4050 parse_name, NULL, &error_fatal);
4052 #ifndef _WIN32
4053 qemu_opts_foreach(qemu_find_opts("add-fd"),
4054 parse_add_fd, NULL, &error_fatal);
4056 qemu_opts_foreach(qemu_find_opts("add-fd"),
4057 cleanup_add_fd, NULL, &error_fatal);
4058 #endif
4060 current_machine = MACHINE(object_new(object_class_get_name(
4061 OBJECT_CLASS(machine_class))));
4062 if (machine_help_func(qemu_get_machine_opts(), current_machine)) {
4063 exit(0);
4065 object_property_add_child(object_get_root(), "machine",
4066 OBJECT(current_machine), &error_abort);
4068 if (machine_class->minimum_page_bits) {
4069 if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
4070 /* This would be a board error: specifying a minimum smaller than
4071 * a target's compile-time fixed setting.
4073 g_assert_not_reached();
4077 cpu_exec_init_all();
4079 if (machine_class->hw_version) {
4080 qemu_set_hw_version(machine_class->hw_version);
4083 if (cpu_model && is_help_option(cpu_model)) {
4084 list_cpus(stdout, &fprintf, cpu_model);
4085 exit(0);
4088 if (!trace_init_backends()) {
4089 exit(1);
4091 trace_init_file(trace_file);
4093 /* Open the logfile at this point and set the log mask if necessary.
4095 if (log_file) {
4096 qemu_set_log_filename(log_file, &error_fatal);
4099 if (log_mask) {
4100 int mask;
4101 mask = qemu_str_to_log_mask(log_mask);
4102 if (!mask) {
4103 qemu_print_log_usage(stdout);
4104 exit(1);
4106 qemu_set_log(mask);
4107 } else {
4108 qemu_set_log(0);
4111 /* add configured firmware directories */
4112 dirs = g_strsplit(CONFIG_QEMU_FIRMWAREPATH, G_SEARCHPATH_SEPARATOR_S, 0);
4113 for (i = 0; dirs[i] != NULL; i++) {
4114 qemu_add_data_dir(dirs[i]);
4116 g_strfreev(dirs);
4118 /* try to find datadir relative to the executable path */
4119 dir = os_find_datadir();
4120 qemu_add_data_dir(dir);
4121 g_free(dir);
4123 /* add the datadir specified when building */
4124 qemu_add_data_dir(CONFIG_QEMU_DATADIR);
4126 /* -L help lists the data directories and exits. */
4127 if (list_data_dirs) {
4128 for (i = 0; i < data_dir_idx; i++) {
4129 printf("%s\n", data_dir[i]);
4131 exit(0);
4134 /* machine_class: default to UP */
4135 machine_class->max_cpus = machine_class->max_cpus ?: 1;
4136 machine_class->min_cpus = machine_class->min_cpus ?: 1;
4137 machine_class->default_cpus = machine_class->default_cpus ?: 1;
4139 /* default to machine_class->default_cpus */
4140 smp_cpus = machine_class->default_cpus;
4141 max_cpus = machine_class->default_cpus;
4143 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
4145 /* sanity-check smp_cpus and max_cpus against machine_class */
4146 if (smp_cpus < machine_class->min_cpus) {
4147 error_report("Invalid SMP CPUs %d. The min CPUs "
4148 "supported by machine '%s' is %d", smp_cpus,
4149 machine_class->name, machine_class->min_cpus);
4150 exit(1);
4152 if (max_cpus > machine_class->max_cpus) {
4153 error_report("Invalid SMP CPUs %d. The max CPUs "
4154 "supported by machine '%s' is %d", max_cpus,
4155 machine_class->name, machine_class->max_cpus);
4156 exit(1);
4160 * Get the default machine options from the machine if it is not already
4161 * specified either by the configuration file or by the command line.
4163 if (machine_class->default_machine_opts) {
4164 qemu_opts_set_defaults(qemu_find_opts("machine"),
4165 machine_class->default_machine_opts, 0);
4168 qemu_opts_foreach(qemu_find_opts("device"),
4169 default_driver_check, NULL, NULL);
4170 qemu_opts_foreach(qemu_find_opts("global"),
4171 default_driver_check, NULL, NULL);
4173 if (!vga_model && !default_vga) {
4174 vga_interface_type = VGA_DEVICE;
4176 if (!has_defaults || machine_class->no_serial) {
4177 default_serial = 0;
4179 if (!has_defaults || machine_class->no_parallel) {
4180 default_parallel = 0;
4182 if (!has_defaults || !machine_class->use_virtcon) {
4183 default_virtcon = 0;
4185 if (!has_defaults || machine_class->no_floppy) {
4186 default_floppy = 0;
4188 if (!has_defaults || machine_class->no_cdrom) {
4189 default_cdrom = 0;
4191 if (!has_defaults || machine_class->no_sdcard) {
4192 default_sdcard = 0;
4194 if (!has_defaults) {
4195 default_monitor = 0;
4196 default_net = 0;
4197 default_vga = 0;
4200 if (is_daemonized()) {
4201 if (!preconfig_exit_requested) {
4202 error_report("'preconfig' and 'daemonize' options are "
4203 "mutually exclusive");
4204 exit(EXIT_FAILURE);
4207 /* According to documentation and historically, -nographic redirects
4208 * serial port, parallel port and monitor to stdio, which does not work
4209 * with -daemonize. We can redirect these to null instead, but since
4210 * -nographic is legacy, let's just error out.
4211 * We disallow -nographic only if all other ports are not redirected
4212 * explicitly, to not break existing legacy setups which uses
4213 * -nographic _and_ redirects all ports explicitly - this is valid
4214 * usage, -nographic is just a no-op in this case.
4216 if (nographic
4217 && (default_parallel || default_serial
4218 || default_monitor || default_virtcon)) {
4219 error_report("-nographic cannot be used with -daemonize");
4220 exit(1);
4222 #ifdef CONFIG_CURSES
4223 if (dpy.type == DISPLAY_TYPE_CURSES) {
4224 error_report("curses display cannot be used with -daemonize");
4225 exit(1);
4227 #endif
4230 if (nographic) {
4231 if (default_parallel)
4232 add_device_config(DEV_PARALLEL, "null");
4233 if (default_serial && default_monitor) {
4234 add_device_config(DEV_SERIAL, "mon:stdio");
4235 } else if (default_virtcon && default_monitor) {
4236 add_device_config(DEV_VIRTCON, "mon:stdio");
4237 } else {
4238 if (default_serial)
4239 add_device_config(DEV_SERIAL, "stdio");
4240 if (default_virtcon)
4241 add_device_config(DEV_VIRTCON, "stdio");
4242 if (default_monitor)
4243 monitor_parse("stdio", "readline", false);
4245 } else {
4246 if (default_serial)
4247 add_device_config(DEV_SERIAL, "vc:80Cx24C");
4248 if (default_parallel)
4249 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
4250 if (default_monitor)
4251 monitor_parse("vc:80Cx24C", "readline", false);
4252 if (default_virtcon)
4253 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
4256 #if defined(CONFIG_VNC)
4257 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
4258 display_remote++;
4260 #endif
4261 if (dpy.type == DISPLAY_TYPE_DEFAULT && !display_remote) {
4262 if (!qemu_display_find_default(&dpy)) {
4263 dpy.type = DISPLAY_TYPE_NONE;
4264 #if defined(CONFIG_VNC)
4265 vnc_parse("localhost:0,to=99,id=default", &error_abort);
4266 #endif
4269 if (dpy.type == DISPLAY_TYPE_DEFAULT) {
4270 dpy.type = DISPLAY_TYPE_NONE;
4273 if ((no_frame || alt_grab || ctrl_grab) && dpy.type != DISPLAY_TYPE_SDL) {
4274 error_report("-no-frame, -alt-grab and -ctrl-grab are only valid "
4275 "for SDL, ignoring option");
4277 if (dpy.has_window_close &&
4278 (dpy.type != DISPLAY_TYPE_GTK && dpy.type != DISPLAY_TYPE_SDL)) {
4279 error_report("-no-quit is only valid for GTK and SDL, "
4280 "ignoring option");
4283 qemu_display_early_init(&dpy);
4284 qemu_console_early_init();
4286 if (dpy.has_gl && dpy.gl != DISPLAYGL_MODE_OFF && display_opengl == 0) {
4287 #if defined(CONFIG_OPENGL)
4288 error_report("OpenGL is not supported by the display");
4289 #else
4290 error_report("OpenGL support is disabled");
4291 #endif
4292 exit(1);
4295 page_size_init();
4296 socket_init();
4298 qemu_opts_foreach(qemu_find_opts("object"),
4299 user_creatable_add_opts_foreach,
4300 object_create_initial, &error_fatal);
4302 qemu_opts_foreach(qemu_find_opts("chardev"),
4303 chardev_init_func, NULL, &error_fatal);
4305 #ifdef CONFIG_VIRTFS
4306 qemu_opts_foreach(qemu_find_opts("fsdev"),
4307 fsdev_init_func, NULL, &error_fatal);
4308 #endif
4310 if (qemu_opts_foreach(qemu_find_opts("device"),
4311 device_help_func, NULL, NULL)) {
4312 exit(0);
4315 machine_opts = qemu_get_machine_opts();
4316 qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
4317 &error_fatal);
4319 configure_accelerator(current_machine);
4321 if (!qtest_enabled() && machine_class->deprecation_reason) {
4322 error_report("Machine type '%s' is deprecated: %s",
4323 machine_class->name, machine_class->deprecation_reason);
4327 * Register all the global properties, including accel properties,
4328 * machine properties, and user-specified ones.
4330 register_global_properties(current_machine);
4333 * Migration object can only be created after global properties
4334 * are applied correctly.
4336 migration_object_init();
4338 if (qtest_chrdev) {
4339 qtest_init(qtest_chrdev, qtest_log, &error_fatal);
4342 machine_opts = qemu_get_machine_opts();
4343 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4344 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4345 kernel_cmdline = qemu_opt_get(machine_opts, "append");
4346 bios_name = qemu_opt_get(machine_opts, "firmware");
4348 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4349 if (opts) {
4350 boot_order = qemu_opt_get(opts, "order");
4351 if (boot_order) {
4352 validate_bootdevices(boot_order, &error_fatal);
4355 boot_once = qemu_opt_get(opts, "once");
4356 if (boot_once) {
4357 validate_bootdevices(boot_once, &error_fatal);
4360 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
4361 boot_strict = qemu_opt_get_bool(opts, "strict", false);
4364 if (!boot_order) {
4365 boot_order = machine_class->default_boot_order;
4368 if (!kernel_cmdline) {
4369 kernel_cmdline = "";
4370 current_machine->kernel_cmdline = (char *)kernel_cmdline;
4373 linux_boot = (kernel_filename != NULL);
4375 if (!linux_boot && *kernel_cmdline != '\0') {
4376 error_report("-append only allowed with -kernel option");
4377 exit(1);
4380 if (!linux_boot && initrd_filename != NULL) {
4381 error_report("-initrd only allowed with -kernel option");
4382 exit(1);
4385 if (semihosting_enabled() && !semihosting_get_argc() && kernel_filename) {
4386 /* fall back to the -kernel/-append */
4387 semihosting_arg_fallback(kernel_filename, kernel_cmdline);
4390 os_set_line_buffering();
4392 /* spice needs the timers to be initialized by this point */
4393 qemu_spice_init();
4395 cpu_ticks_init();
4396 if (icount_opts) {
4397 if (!tcg_enabled()) {
4398 error_report("-icount is not allowed with hardware virtualization");
4399 exit(1);
4401 configure_icount(icount_opts, &error_abort);
4402 qemu_opts_del(icount_opts);
4405 if (tcg_enabled()) {
4406 qemu_tcg_configure(accel_opts, &error_fatal);
4409 if (default_net) {
4410 QemuOptsList *net = qemu_find_opts("net");
4411 qemu_opts_set(net, NULL, "type", "nic", &error_abort);
4412 #ifdef CONFIG_SLIRP
4413 qemu_opts_set(net, NULL, "type", "user", &error_abort);
4414 #endif
4417 if (net_init_clients(&err) < 0) {
4418 error_report_err(err);
4419 exit(1);
4422 qemu_opts_foreach(qemu_find_opts("object"),
4423 user_creatable_add_opts_foreach,
4424 object_create_delayed, &error_fatal);
4426 tpm_init();
4428 /* init the bluetooth world */
4429 if (foreach_device_config(DEV_BT, bt_parse))
4430 exit(1);
4432 if (!xen_enabled()) {
4433 /* On 32-bit hosts, QEMU is limited by virtual address space */
4434 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4435 error_report("at most 2047 MB RAM can be simulated");
4436 exit(1);
4440 blk_mig_init();
4441 ram_mig_init();
4442 dirty_bitmap_mig_init();
4444 /* If the currently selected machine wishes to override the units-per-bus
4445 * property of its default HBA interface type, do so now. */
4446 if (machine_class->units_per_default_bus) {
4447 override_max_devs(machine_class->block_default_type,
4448 machine_class->units_per_default_bus);
4451 /* open the virtual block devices */
4452 while (!QSIMPLEQ_EMPTY(&bdo_queue)) {
4453 BlockdevOptions_queue *bdo = QSIMPLEQ_FIRST(&bdo_queue);
4455 QSIMPLEQ_REMOVE_HEAD(&bdo_queue, entry);
4456 loc_push_restore(&bdo->loc);
4457 qmp_blockdev_add(bdo->bdo, &error_fatal);
4458 loc_pop(&bdo->loc);
4459 qapi_free_BlockdevOptions(bdo->bdo);
4460 g_free(bdo);
4462 if (snapshot || replay_mode != REPLAY_MODE_NONE) {
4463 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
4464 NULL, NULL);
4466 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
4467 &machine_class->block_default_type, &error_fatal)) {
4468 /* We printed help */
4469 exit(0);
4472 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
4473 CDROM_OPTS);
4474 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4475 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
4477 qemu_opts_foreach(qemu_find_opts("mon"),
4478 mon_init_func, NULL, &error_fatal);
4480 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4481 exit(1);
4482 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4483 exit(1);
4484 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4485 exit(1);
4486 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4487 exit(1);
4489 /* If no default VGA is requested, the default is "none". */
4490 if (default_vga) {
4491 if (machine_class->default_display) {
4492 vga_model = machine_class->default_display;
4493 } else if (vga_interface_available(VGA_CIRRUS)) {
4494 vga_model = "cirrus";
4495 } else if (vga_interface_available(VGA_STD)) {
4496 vga_model = "std";
4499 if (vga_model) {
4500 select_vgahw(vga_model);
4503 if (watchdog) {
4504 i = select_watchdog(watchdog);
4505 if (i > 0)
4506 exit (i == 1 ? 1 : 0);
4509 /* This checkpoint is required by replay to separate prior clock
4510 reading from the other reads, because timer polling functions query
4511 clock values from the log. */
4512 replay_checkpoint(CHECKPOINT_INIT);
4513 qdev_machine_init();
4515 current_machine->ram_size = ram_size;
4516 current_machine->maxram_size = maxram_size;
4517 current_machine->ram_slots = ram_slots;
4518 current_machine->boot_order = boot_order;
4520 /* parse features once if machine provides default cpu_type */
4521 current_machine->cpu_type = machine_class->default_cpu_type;
4522 if (cpu_model) {
4523 current_machine->cpu_type = parse_cpu_model(cpu_model);
4525 parse_numa_opts(current_machine);
4527 /* do monitor/qmp handling at preconfig state if requested */
4528 main_loop();
4530 /* from here on runstate is RUN_STATE_PRELAUNCH */
4531 machine_run_board_init(current_machine);
4533 realtime_init();
4535 soundhw_init();
4537 if (hax_enabled()) {
4538 hax_sync_vcpus();
4541 qemu_opts_foreach(qemu_find_opts("fw_cfg"),
4542 parse_fw_cfg, fw_cfg_find(), &error_fatal);
4544 /* init USB devices */
4545 if (machine_usb(current_machine)) {
4546 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4547 exit(1);
4550 /* Check if IGD GFX passthrough. */
4551 igd_gfx_passthru();
4553 /* init generic devices */
4554 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
4555 qemu_opts_foreach(qemu_find_opts("device"),
4556 device_init_func, NULL, &error_fatal);
4558 cpu_synchronize_all_post_init();
4560 rom_reset_order_override();
4562 /* Did we create any drives that we failed to create a device for? */
4563 drive_check_orphaned();
4565 /* Don't warn about the default network setup that you get if
4566 * no command line -net or -netdev options are specified. There
4567 * are two cases that we would otherwise complain about:
4568 * (1) board doesn't support a NIC but the implicit "-net nic"
4569 * requested one
4570 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
4571 * sets up a nic that isn't connected to anything.
4573 if (!default_net && (!qtest_enabled() || has_defaults)) {
4574 net_check_clients();
4577 if (boot_once) {
4578 qemu_boot_set(boot_once, &error_fatal);
4579 qemu_register_reset(restore_boot_order, g_strdup(boot_order));
4582 /* init local displays */
4583 ds = init_displaystate();
4584 qemu_display_init(ds, &dpy);
4586 /* must be after terminal init, SDL library changes signal handlers */
4587 os_setup_signal_handling();
4589 /* init remote displays */
4590 #ifdef CONFIG_VNC
4591 qemu_opts_foreach(qemu_find_opts("vnc"),
4592 vnc_init_func, NULL, &error_fatal);
4593 #endif
4595 if (using_spice) {
4596 qemu_spice_display_init();
4599 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
4600 exit(1);
4603 qdev_machine_creation_done();
4605 /* TODO: once all bus devices are qdevified, this should be done
4606 * when bus is created by qdev.c */
4607 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4608 qemu_run_machine_init_done_notifiers();
4610 if (rom_check_and_register_reset() != 0) {
4611 error_report("rom check and register reset failed");
4612 exit(1);
4615 replay_start();
4617 /* This checkpoint is required by replay to separate prior clock
4618 reading from the other reads, because timer polling functions query
4619 clock values from the log. */
4620 replay_checkpoint(CHECKPOINT_RESET);
4621 qemu_system_reset(SHUTDOWN_CAUSE_NONE);
4622 register_global_state();
4623 if (loadvm) {
4624 Error *local_err = NULL;
4625 if (load_snapshot(loadvm, &local_err) < 0) {
4626 error_report_err(local_err);
4627 autostart = 0;
4628 exit(1);
4631 if (replay_mode != REPLAY_MODE_NONE) {
4632 replay_vmstate_init();
4635 qdev_prop_check_globals();
4636 if (vmstate_dump_file) {
4637 /* dump and exit */
4638 dump_vmstate_json_to_file(vmstate_dump_file);
4639 return 0;
4642 if (incoming) {
4643 Error *local_err = NULL;
4644 qemu_start_incoming_migration(incoming, &local_err);
4645 if (local_err) {
4646 error_reportf_err(local_err, "-incoming %s: ", incoming);
4647 exit(1);
4649 } else if (autostart) {
4650 vm_start();
4653 accel_setup_post(current_machine);
4654 os_setup_post();
4656 main_loop();
4658 gdbserver_cleanup();
4660 /* No more vcpu or device emulation activity beyond this point */
4661 vm_shutdown();
4663 job_cancel_sync_all();
4664 bdrv_close_all();
4666 res_free();
4668 /* vhost-user must be cleaned up before chardevs. */
4669 tpm_cleanup();
4670 net_cleanup();
4671 audio_cleanup();
4672 monitor_cleanup();
4673 qemu_chr_cleanup();
4674 user_creatable_cleanup();
4675 migration_object_finalize();
4676 /* TODO: unref root container, check all devices are ok */
4678 return 0;