Merge tag 'v3.1.0-rc0'
[qemu/ar7.git] / vl.c
blob29e88fced45ff09d62b991abc1220bd9c530b2f9
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 add_device_config(DEV_BT, optarg);
3283 break;
3284 case QEMU_OPTION_audio_help:
3285 AUD_help ();
3286 exit (0);
3287 break;
3288 case QEMU_OPTION_soundhw:
3289 select_soundhw (optarg);
3290 break;
3291 case QEMU_OPTION_h:
3292 help(0);
3293 break;
3294 case QEMU_OPTION_version:
3295 version();
3296 exit(0);
3297 break;
3298 case QEMU_OPTION_m:
3299 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
3300 optarg, true);
3301 if (!opts) {
3302 exit(EXIT_FAILURE);
3304 break;
3305 #ifdef CONFIG_TPM
3306 case QEMU_OPTION_tpmdev:
3307 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3308 exit(1);
3310 break;
3311 #endif
3312 case QEMU_OPTION_mempath:
3313 mem_path = optarg;
3314 break;
3315 case QEMU_OPTION_mem_prealloc:
3316 mem_prealloc = 1;
3317 break;
3318 case QEMU_OPTION_d:
3319 log_mask = optarg;
3320 break;
3321 case QEMU_OPTION_D:
3322 log_file = optarg;
3323 break;
3324 case QEMU_OPTION_DFILTER:
3325 qemu_set_dfilter_ranges(optarg, &error_fatal);
3326 break;
3327 case QEMU_OPTION_s:
3328 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3329 break;
3330 case QEMU_OPTION_gdb:
3331 add_device_config(DEV_GDB, optarg);
3332 break;
3333 case QEMU_OPTION_L:
3334 if (is_help_option(optarg)) {
3335 list_data_dirs = true;
3336 } else {
3337 qemu_add_data_dir(optarg);
3339 break;
3340 case QEMU_OPTION_bios:
3341 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg,
3342 &error_abort);
3343 break;
3344 case QEMU_OPTION_singlestep:
3345 singlestep = 1;
3346 break;
3347 case QEMU_OPTION_S:
3348 autostart = 0;
3349 break;
3350 case QEMU_OPTION_k:
3351 keyboard_layout = optarg;
3352 break;
3353 case QEMU_OPTION_vga:
3354 vga_model = optarg;
3355 default_vga = 0;
3356 break;
3357 case QEMU_OPTION_g:
3359 const char *p;
3360 int w, h, depth;
3361 p = optarg;
3362 w = strtol(p, (char **)&p, 10);
3363 if (w <= 0) {
3364 graphic_error:
3365 error_report("invalid resolution or depth");
3366 exit(1);
3368 if (*p != 'x')
3369 goto graphic_error;
3370 p++;
3371 h = strtol(p, (char **)&p, 10);
3372 if (h <= 0)
3373 goto graphic_error;
3374 if (*p == 'x') {
3375 p++;
3376 depth = strtol(p, (char **)&p, 10);
3377 if (depth != 8 && depth != 15 && depth != 16 &&
3378 depth != 24 && depth != 32)
3379 goto graphic_error;
3380 } else if (*p == '\0') {
3381 depth = graphic_depth;
3382 } else {
3383 goto graphic_error;
3386 graphic_width = w;
3387 graphic_height = h;
3388 graphic_depth = depth;
3390 break;
3391 case QEMU_OPTION_echr:
3393 char *r;
3394 term_escape_char = strtol(optarg, &r, 0);
3395 if (r == optarg)
3396 printf("Bad argument to echr\n");
3397 break;
3399 case QEMU_OPTION_monitor:
3400 default_monitor = 0;
3401 if (strncmp(optarg, "none", 4)) {
3402 monitor_parse(optarg, "readline", false);
3404 break;
3405 case QEMU_OPTION_qmp:
3406 monitor_parse(optarg, "control", false);
3407 default_monitor = 0;
3408 break;
3409 case QEMU_OPTION_qmp_pretty:
3410 monitor_parse(optarg, "control", true);
3411 default_monitor = 0;
3412 break;
3413 case QEMU_OPTION_mon:
3414 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3415 true);
3416 if (!opts) {
3417 exit(1);
3419 default_monitor = 0;
3420 break;
3421 case QEMU_OPTION_chardev:
3422 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3423 optarg, true);
3424 if (!opts) {
3425 exit(1);
3427 break;
3428 case QEMU_OPTION_fsdev:
3429 olist = qemu_find_opts("fsdev");
3430 if (!olist) {
3431 error_report("fsdev support is disabled");
3432 exit(1);
3434 opts = qemu_opts_parse_noisily(olist, optarg, true);
3435 if (!opts) {
3436 exit(1);
3438 break;
3439 case QEMU_OPTION_virtfs: {
3440 QemuOpts *fsdev;
3441 QemuOpts *device;
3442 const char *writeout, *sock_fd, *socket, *path, *security_model;
3444 olist = qemu_find_opts("virtfs");
3445 if (!olist) {
3446 error_report("virtfs support is disabled");
3447 exit(1);
3449 opts = qemu_opts_parse_noisily(olist, optarg, true);
3450 if (!opts) {
3451 exit(1);
3454 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3455 qemu_opt_get(opts, "mount_tag") == NULL) {
3456 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3457 exit(1);
3459 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3460 qemu_opts_id(opts) ?:
3461 qemu_opt_get(opts, "mount_tag"),
3462 1, NULL);
3463 if (!fsdev) {
3464 error_report("duplicate or invalid fsdev id: %s",
3465 qemu_opt_get(opts, "mount_tag"));
3466 exit(1);
3469 writeout = qemu_opt_get(opts, "writeout");
3470 if (writeout) {
3471 #ifdef CONFIG_SYNC_FILE_RANGE
3472 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3473 #else
3474 error_report("writeout=immediate not supported "
3475 "on this platform");
3476 exit(1);
3477 #endif
3479 qemu_opt_set(fsdev, "fsdriver",
3480 qemu_opt_get(opts, "fsdriver"), &error_abort);
3481 path = qemu_opt_get(opts, "path");
3482 if (path) {
3483 qemu_opt_set(fsdev, "path", path, &error_abort);
3485 security_model = qemu_opt_get(opts, "security_model");
3486 if (security_model) {
3487 qemu_opt_set(fsdev, "security_model", security_model,
3488 &error_abort);
3490 socket = qemu_opt_get(opts, "socket");
3491 if (socket) {
3492 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3494 sock_fd = qemu_opt_get(opts, "sock_fd");
3495 if (sock_fd) {
3496 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3499 qemu_opt_set_bool(fsdev, "readonly",
3500 qemu_opt_get_bool(opts, "readonly", 0),
3501 &error_abort);
3502 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3503 &error_abort);
3504 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3505 qemu_opt_set(device, "fsdev",
3506 qemu_opts_id(fsdev), &error_abort);
3507 qemu_opt_set(device, "mount_tag",
3508 qemu_opt_get(opts, "mount_tag"), &error_abort);
3509 break;
3511 case QEMU_OPTION_virtfs_synth: {
3512 QemuOpts *fsdev;
3513 QemuOpts *device;
3515 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3516 1, NULL);
3517 if (!fsdev) {
3518 error_report("duplicate option: %s", "virtfs_synth");
3519 exit(1);
3521 qemu_opt_set(fsdev, "fsdriver", "synth", &error_abort);
3523 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3524 &error_abort);
3525 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3526 qemu_opt_set(device, "fsdev", "v_synth", &error_abort);
3527 qemu_opt_set(device, "mount_tag", "v_synth", &error_abort);
3528 break;
3530 case QEMU_OPTION_serial:
3531 add_device_config(DEV_SERIAL, optarg);
3532 default_serial = 0;
3533 if (strncmp(optarg, "mon:", 4) == 0) {
3534 default_monitor = 0;
3536 break;
3537 case QEMU_OPTION_watchdog:
3538 if (watchdog) {
3539 error_report("only one watchdog option may be given");
3540 return 1;
3542 watchdog = optarg;
3543 break;
3544 case QEMU_OPTION_watchdog_action:
3545 if (select_watchdog_action(optarg) == -1) {
3546 error_report("unknown -watchdog-action parameter");
3547 exit(1);
3549 break;
3550 case QEMU_OPTION_virtiocon:
3551 warn_report("This option is deprecated, "
3552 "use '-device virtconsole' instead");
3553 add_device_config(DEV_VIRTCON, optarg);
3554 default_virtcon = 0;
3555 if (strncmp(optarg, "mon:", 4) == 0) {
3556 default_monitor = 0;
3558 break;
3559 case QEMU_OPTION_parallel:
3560 add_device_config(DEV_PARALLEL, optarg);
3561 default_parallel = 0;
3562 if (strncmp(optarg, "mon:", 4) == 0) {
3563 default_monitor = 0;
3565 break;
3566 case QEMU_OPTION_debugcon:
3567 add_device_config(DEV_DEBUGCON, optarg);
3568 break;
3569 case QEMU_OPTION_loadvm:
3570 loadvm = optarg;
3571 break;
3572 case QEMU_OPTION_full_screen:
3573 dpy.has_full_screen = true;
3574 dpy.full_screen = true;
3575 break;
3576 case QEMU_OPTION_no_frame:
3577 g_printerr("The -no-frame switch is deprecated, and will be\n"
3578 "removed in a future release.\n");
3579 no_frame = 1;
3580 break;
3581 case QEMU_OPTION_alt_grab:
3582 alt_grab = 1;
3583 break;
3584 case QEMU_OPTION_ctrl_grab:
3585 ctrl_grab = 1;
3586 break;
3587 case QEMU_OPTION_no_quit:
3588 dpy.has_window_close = true;
3589 dpy.window_close = false;
3590 break;
3591 case QEMU_OPTION_sdl:
3592 #ifdef CONFIG_SDL
3593 dpy.type = DISPLAY_TYPE_SDL;
3594 break;
3595 #else
3596 error_report("SDL support is disabled");
3597 exit(1);
3598 #endif
3599 case QEMU_OPTION_pidfile:
3600 pid_file = optarg;
3601 break;
3602 case QEMU_OPTION_win2k_hack:
3603 win2k_install_hack = 1;
3604 break;
3605 case QEMU_OPTION_acpitable:
3606 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3607 optarg, true);
3608 if (!opts) {
3609 exit(1);
3611 acpi_table_add(opts, &error_fatal);
3612 break;
3613 case QEMU_OPTION_smbios:
3614 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3615 optarg, false);
3616 if (!opts) {
3617 exit(1);
3619 smbios_entry_add(opts, &error_fatal);
3620 break;
3621 case QEMU_OPTION_fwcfg:
3622 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3623 optarg, true);
3624 if (opts == NULL) {
3625 exit(1);
3627 break;
3628 case QEMU_OPTION_preconfig:
3629 preconfig_exit_requested = false;
3630 break;
3631 case QEMU_OPTION_enable_kvm:
3632 olist = qemu_find_opts("machine");
3633 qemu_opts_parse_noisily(olist, "accel=kvm", false);
3634 break;
3635 case QEMU_OPTION_enable_hax:
3636 warn_report("Option is deprecated, use '-accel hax' instead");
3637 olist = qemu_find_opts("machine");
3638 qemu_opts_parse_noisily(olist, "accel=hax", false);
3639 break;
3640 case QEMU_OPTION_M:
3641 case QEMU_OPTION_machine:
3642 olist = qemu_find_opts("machine");
3643 opts = qemu_opts_parse_noisily(olist, optarg, true);
3644 if (!opts) {
3645 exit(1);
3647 break;
3648 case QEMU_OPTION_no_kvm:
3649 olist = qemu_find_opts("machine");
3650 qemu_opts_parse_noisily(olist, "accel=tcg", false);
3651 break;
3652 case QEMU_OPTION_accel:
3653 accel_opts = qemu_opts_parse_noisily(qemu_find_opts("accel"),
3654 optarg, true);
3655 optarg = qemu_opt_get(accel_opts, "accel");
3656 if (!optarg || is_help_option(optarg)) {
3657 error_printf("Possible accelerators: kvm, xen, hax, tcg\n");
3658 exit(0);
3660 opts = qemu_opts_create(qemu_find_opts("machine"), NULL,
3661 false, &error_abort);
3662 qemu_opt_set(opts, "accel", optarg, &error_abort);
3663 break;
3664 case QEMU_OPTION_usb:
3665 olist = qemu_find_opts("machine");
3666 qemu_opts_parse_noisily(olist, "usb=on", false);
3667 break;
3668 case QEMU_OPTION_usbdevice:
3669 error_report("'-usbdevice' is deprecated, please use "
3670 "'-device usb-...' instead");
3671 olist = qemu_find_opts("machine");
3672 qemu_opts_parse_noisily(olist, "usb=on", false);
3673 add_device_config(DEV_USB, optarg);
3674 break;
3675 case QEMU_OPTION_device:
3676 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3677 optarg, true)) {
3678 exit(1);
3680 break;
3681 case QEMU_OPTION_smp:
3682 if (!qemu_opts_parse_noisily(qemu_find_opts("smp-opts"),
3683 optarg, true)) {
3684 exit(1);
3686 break;
3687 case QEMU_OPTION_vnc:
3688 vnc_parse(optarg, &error_fatal);
3689 break;
3690 case QEMU_OPTION_no_acpi:
3691 acpi_enabled = 0;
3692 break;
3693 case QEMU_OPTION_no_hpet:
3694 no_hpet = 1;
3695 break;
3696 case QEMU_OPTION_no_reboot:
3697 no_reboot = 1;
3698 break;
3699 case QEMU_OPTION_no_shutdown:
3700 no_shutdown = 1;
3701 break;
3702 case QEMU_OPTION_show_cursor:
3703 cursor_hide = 0;
3704 break;
3705 case QEMU_OPTION_uuid:
3706 if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
3707 error_report("failed to parse UUID string: wrong format");
3708 exit(1);
3710 qemu_uuid_set = true;
3711 break;
3712 case QEMU_OPTION_option_rom:
3713 if (nb_option_roms >= MAX_OPTION_ROMS) {
3714 error_report("too many option ROMs");
3715 exit(1);
3717 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3718 optarg, true);
3719 if (!opts) {
3720 exit(1);
3722 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3723 option_rom[nb_option_roms].bootindex =
3724 qemu_opt_get_number(opts, "bootindex", -1);
3725 if (!option_rom[nb_option_roms].name) {
3726 error_report("Option ROM file is not specified");
3727 exit(1);
3729 nb_option_roms++;
3730 break;
3731 case QEMU_OPTION_semihosting:
3732 semihosting.enabled = true;
3733 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3734 break;
3735 case QEMU_OPTION_semihosting_config:
3736 semihosting.enabled = true;
3737 opts = qemu_opts_parse_noisily(qemu_find_opts("semihosting-config"),
3738 optarg, false);
3739 if (opts != NULL) {
3740 semihosting.enabled = qemu_opt_get_bool(opts, "enable",
3741 true);
3742 const char *target = qemu_opt_get(opts, "target");
3743 if (target != NULL) {
3744 if (strcmp("native", target) == 0) {
3745 semihosting.target = SEMIHOSTING_TARGET_NATIVE;
3746 } else if (strcmp("gdb", target) == 0) {
3747 semihosting.target = SEMIHOSTING_TARGET_GDB;
3748 } else if (strcmp("auto", target) == 0) {
3749 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3750 } else {
3751 error_report("unsupported semihosting-config %s",
3752 optarg);
3753 exit(1);
3755 } else {
3756 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3758 /* Set semihosting argument count and vector */
3759 qemu_opt_foreach(opts, add_semihosting_arg,
3760 &semihosting, NULL);
3761 } else {
3762 error_report("unsupported semihosting-config %s", optarg);
3763 exit(1);
3765 break;
3766 case QEMU_OPTION_name:
3767 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3768 optarg, true);
3769 if (!opts) {
3770 exit(1);
3772 break;
3773 case QEMU_OPTION_prom_env:
3774 if (nb_prom_envs >= MAX_PROM_ENVS) {
3775 error_report("too many prom variables");
3776 exit(1);
3778 prom_envs[nb_prom_envs] = optarg;
3779 nb_prom_envs++;
3780 break;
3781 case QEMU_OPTION_old_param:
3782 old_param = 1;
3783 break;
3784 case QEMU_OPTION_clock:
3785 /* Clock options no longer exist. Keep this option for
3786 * backward compatibility.
3788 warn_report("This option is ignored and will be removed soon");
3789 break;
3790 case QEMU_OPTION_rtc:
3791 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3792 false);
3793 if (!opts) {
3794 exit(1);
3796 break;
3797 case QEMU_OPTION_tb_size:
3798 #ifndef CONFIG_TCG
3799 error_report("TCG is disabled");
3800 exit(1);
3801 #endif
3802 if (qemu_strtoul(optarg, NULL, 0, &tcg_tb_size) < 0) {
3803 error_report("Invalid argument to -tb-size");
3804 exit(1);
3806 break;
3807 case QEMU_OPTION_icount:
3808 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3809 optarg, true);
3810 if (!icount_opts) {
3811 exit(1);
3813 break;
3814 case QEMU_OPTION_incoming:
3815 if (!incoming) {
3816 runstate_set(RUN_STATE_INMIGRATE);
3818 incoming = optarg;
3819 break;
3820 case QEMU_OPTION_only_migratable:
3822 * TODO: we can remove this option one day, and we
3823 * should all use:
3825 * "-global migration.only-migratable=true"
3827 qemu_global_option("migration.only-migratable=true");
3828 break;
3829 case QEMU_OPTION_nodefaults:
3830 has_defaults = 0;
3831 break;
3832 case QEMU_OPTION_xen_domid:
3833 if (!(xen_available())) {
3834 error_report("Option not supported for this target");
3835 exit(1);
3837 xen_domid = atoi(optarg);
3838 break;
3839 case QEMU_OPTION_xen_create:
3840 if (!(xen_available())) {
3841 error_report("Option not supported for this target");
3842 exit(1);
3844 xen_mode = XEN_CREATE;
3845 break;
3846 case QEMU_OPTION_xen_attach:
3847 if (!(xen_available())) {
3848 error_report("Option not supported for this target");
3849 exit(1);
3851 xen_mode = XEN_ATTACH;
3852 break;
3853 case QEMU_OPTION_xen_domid_restrict:
3854 if (!(xen_available())) {
3855 error_report("Option not supported for this target");
3856 exit(1);
3858 xen_domid_restrict = true;
3859 break;
3860 case QEMU_OPTION_trace:
3861 g_free(trace_file);
3862 trace_file = trace_opt_parse(optarg);
3863 break;
3864 case QEMU_OPTION_trace_unassigned:
3865 trace_unassigned = true;
3866 break;
3867 case QEMU_OPTION_readconfig:
3869 int ret = qemu_read_config_file(optarg);
3870 if (ret < 0) {
3871 error_report("read config %s: %s", optarg,
3872 strerror(-ret));
3873 exit(1);
3875 break;
3877 case QEMU_OPTION_spice:
3878 olist = qemu_find_opts("spice");
3879 if (!olist) {
3880 error_report("spice support is disabled");
3881 exit(1);
3883 opts = qemu_opts_parse_noisily(olist, optarg, false);
3884 if (!opts) {
3885 exit(1);
3887 display_remote++;
3888 break;
3889 case QEMU_OPTION_writeconfig:
3891 FILE *fp;
3892 if (strcmp(optarg, "-") == 0) {
3893 fp = stdout;
3894 } else {
3895 fp = fopen(optarg, "w");
3896 if (fp == NULL) {
3897 error_report("open %s: %s", optarg,
3898 strerror(errno));
3899 exit(1);
3902 qemu_config_write(fp);
3903 if (fp != stdout) {
3904 fclose(fp);
3906 break;
3908 case QEMU_OPTION_qtest:
3909 qtest_chrdev = optarg;
3910 break;
3911 case QEMU_OPTION_qtest_log:
3912 qtest_log = optarg;
3913 break;
3914 case QEMU_OPTION_sandbox:
3915 #ifdef CONFIG_SECCOMP
3916 opts = qemu_opts_parse_noisily(qemu_find_opts("sandbox"),
3917 optarg, true);
3918 if (!opts) {
3919 exit(1);
3921 #else
3922 error_report("-sandbox support is not enabled "
3923 "in this QEMU binary");
3924 exit(1);
3925 #endif
3926 break;
3927 case QEMU_OPTION_add_fd:
3928 #ifndef _WIN32
3929 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
3930 optarg, false);
3931 if (!opts) {
3932 exit(1);
3934 #else
3935 error_report("File descriptor passing is disabled on this "
3936 "platform");
3937 exit(1);
3938 #endif
3939 break;
3940 case QEMU_OPTION_object:
3941 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
3942 optarg, true);
3943 if (!opts) {
3944 exit(1);
3946 break;
3947 case QEMU_OPTION_realtime:
3948 opts = qemu_opts_parse_noisily(qemu_find_opts("realtime"),
3949 optarg, false);
3950 if (!opts) {
3951 exit(1);
3953 /* Don't override the -overcommit option if set */
3954 enable_mlock = enable_mlock ||
3955 qemu_opt_get_bool(opts, "mlock", true);
3956 break;
3957 case QEMU_OPTION_overcommit:
3958 opts = qemu_opts_parse_noisily(qemu_find_opts("overcommit"),
3959 optarg, false);
3960 if (!opts) {
3961 exit(1);
3963 /* Don't override the -realtime option if set */
3964 enable_mlock = enable_mlock ||
3965 qemu_opt_get_bool(opts, "mem-lock", false);
3966 enable_cpu_pm = qemu_opt_get_bool(opts, "cpu-pm", false);
3967 break;
3968 case QEMU_OPTION_msg:
3969 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
3970 false);
3971 if (!opts) {
3972 exit(1);
3974 configure_msg(opts);
3975 break;
3976 case QEMU_OPTION_dump_vmstate:
3977 if (vmstate_dump_file) {
3978 error_report("only one '-dump-vmstate' "
3979 "option may be given");
3980 exit(1);
3982 vmstate_dump_file = fopen(optarg, "w");
3983 if (vmstate_dump_file == NULL) {
3984 error_report("open %s: %s", optarg, strerror(errno));
3985 exit(1);
3987 break;
3988 case QEMU_OPTION_enable_sync_profile:
3989 qsp_enable();
3990 break;
3991 case QEMU_OPTION_nouserconfig:
3992 /* Nothing to be parsed here. Especially, do not error out below. */
3993 break;
3994 default:
3995 if (os_parse_cmd_args(popt->index, optarg)) {
3996 error_report("Option not supported in this build");
3997 exit(1);
4003 * Clear error location left behind by the loop.
4004 * Best done right after the loop. Do not insert code here!
4006 loc_set_none();
4008 replay_configure(icount_opts);
4010 if (incoming && !preconfig_exit_requested) {
4011 error_report("'preconfig' and 'incoming' options are "
4012 "mutually exclusive");
4013 exit(EXIT_FAILURE);
4016 configure_rtc(qemu_find_opts_singleton("rtc"));
4018 machine_class = select_machine();
4020 set_memory_options(&ram_slots, &maxram_size, machine_class);
4022 os_daemonize();
4023 rcu_disable_atfork();
4025 if (pid_file && !qemu_write_pidfile(pid_file, &err)) {
4026 error_reportf_err(err, "cannot create PID file: ");
4027 exit(1);
4030 qemu_unlink_pidfile_notifier.notify = qemu_unlink_pidfile;
4031 qemu_add_exit_notifier(&qemu_unlink_pidfile_notifier);
4033 if (qemu_init_main_loop(&main_loop_err)) {
4034 error_report_err(main_loop_err);
4035 exit(1);
4038 #ifdef CONFIG_SECCOMP
4039 olist = qemu_find_opts_err("sandbox", NULL);
4040 if (olist) {
4041 qemu_opts_foreach(olist, parse_sandbox, NULL, &error_fatal);
4043 #endif
4045 qemu_opts_foreach(qemu_find_opts("name"),
4046 parse_name, NULL, &error_fatal);
4048 #ifndef _WIN32
4049 qemu_opts_foreach(qemu_find_opts("add-fd"),
4050 parse_add_fd, NULL, &error_fatal);
4052 qemu_opts_foreach(qemu_find_opts("add-fd"),
4053 cleanup_add_fd, NULL, &error_fatal);
4054 #endif
4056 current_machine = MACHINE(object_new(object_class_get_name(
4057 OBJECT_CLASS(machine_class))));
4058 if (machine_help_func(qemu_get_machine_opts(), current_machine)) {
4059 exit(0);
4061 object_property_add_child(object_get_root(), "machine",
4062 OBJECT(current_machine), &error_abort);
4064 if (machine_class->minimum_page_bits) {
4065 if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
4066 /* This would be a board error: specifying a minimum smaller than
4067 * a target's compile-time fixed setting.
4069 g_assert_not_reached();
4073 cpu_exec_init_all();
4075 if (machine_class->hw_version) {
4076 qemu_set_hw_version(machine_class->hw_version);
4079 if (cpu_model && is_help_option(cpu_model)) {
4080 list_cpus(stdout, &fprintf, cpu_model);
4081 exit(0);
4084 if (!trace_init_backends()) {
4085 exit(1);
4087 trace_init_file(trace_file);
4089 /* Open the logfile at this point and set the log mask if necessary.
4091 if (log_file) {
4092 qemu_set_log_filename(log_file, &error_fatal);
4095 if (log_mask) {
4096 int mask;
4097 mask = qemu_str_to_log_mask(log_mask);
4098 if (!mask) {
4099 qemu_print_log_usage(stdout);
4100 exit(1);
4102 qemu_set_log(mask);
4103 } else {
4104 qemu_set_log(0);
4107 /* add configured firmware directories */
4108 dirs = g_strsplit(CONFIG_QEMU_FIRMWAREPATH, G_SEARCHPATH_SEPARATOR_S, 0);
4109 for (i = 0; dirs[i] != NULL; i++) {
4110 qemu_add_data_dir(dirs[i]);
4112 g_strfreev(dirs);
4114 /* try to find datadir relative to the executable path */
4115 dir = os_find_datadir();
4116 qemu_add_data_dir(dir);
4117 g_free(dir);
4119 /* add the datadir specified when building */
4120 qemu_add_data_dir(CONFIG_QEMU_DATADIR);
4122 /* -L help lists the data directories and exits. */
4123 if (list_data_dirs) {
4124 for (i = 0; i < data_dir_idx; i++) {
4125 printf("%s\n", data_dir[i]);
4127 exit(0);
4130 /* machine_class: default to UP */
4131 machine_class->max_cpus = machine_class->max_cpus ?: 1;
4132 machine_class->min_cpus = machine_class->min_cpus ?: 1;
4133 machine_class->default_cpus = machine_class->default_cpus ?: 1;
4135 /* default to machine_class->default_cpus */
4136 smp_cpus = machine_class->default_cpus;
4137 max_cpus = machine_class->default_cpus;
4139 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
4141 /* sanity-check smp_cpus and max_cpus against machine_class */
4142 if (smp_cpus < machine_class->min_cpus) {
4143 error_report("Invalid SMP CPUs %d. The min CPUs "
4144 "supported by machine '%s' is %d", smp_cpus,
4145 machine_class->name, machine_class->min_cpus);
4146 exit(1);
4148 if (max_cpus > machine_class->max_cpus) {
4149 error_report("Invalid SMP CPUs %d. The max CPUs "
4150 "supported by machine '%s' is %d", max_cpus,
4151 machine_class->name, machine_class->max_cpus);
4152 exit(1);
4156 * Get the default machine options from the machine if it is not already
4157 * specified either by the configuration file or by the command line.
4159 if (machine_class->default_machine_opts) {
4160 qemu_opts_set_defaults(qemu_find_opts("machine"),
4161 machine_class->default_machine_opts, 0);
4164 qemu_opts_foreach(qemu_find_opts("device"),
4165 default_driver_check, NULL, NULL);
4166 qemu_opts_foreach(qemu_find_opts("global"),
4167 default_driver_check, NULL, NULL);
4169 if (!vga_model && !default_vga) {
4170 vga_interface_type = VGA_DEVICE;
4172 if (!has_defaults || machine_class->no_serial) {
4173 default_serial = 0;
4175 if (!has_defaults || machine_class->no_parallel) {
4176 default_parallel = 0;
4178 if (!has_defaults || !machine_class->use_virtcon) {
4179 default_virtcon = 0;
4181 if (!has_defaults || machine_class->no_floppy) {
4182 default_floppy = 0;
4184 if (!has_defaults || machine_class->no_cdrom) {
4185 default_cdrom = 0;
4187 if (!has_defaults || machine_class->no_sdcard) {
4188 default_sdcard = 0;
4190 if (!has_defaults) {
4191 default_monitor = 0;
4192 default_net = 0;
4193 default_vga = 0;
4196 if (is_daemonized()) {
4197 if (!preconfig_exit_requested) {
4198 error_report("'preconfig' and 'daemonize' options are "
4199 "mutually exclusive");
4200 exit(EXIT_FAILURE);
4203 /* According to documentation and historically, -nographic redirects
4204 * serial port, parallel port and monitor to stdio, which does not work
4205 * with -daemonize. We can redirect these to null instead, but since
4206 * -nographic is legacy, let's just error out.
4207 * We disallow -nographic only if all other ports are not redirected
4208 * explicitly, to not break existing legacy setups which uses
4209 * -nographic _and_ redirects all ports explicitly - this is valid
4210 * usage, -nographic is just a no-op in this case.
4212 if (nographic
4213 && (default_parallel || default_serial
4214 || default_monitor || default_virtcon)) {
4215 error_report("-nographic cannot be used with -daemonize");
4216 exit(1);
4218 #ifdef CONFIG_CURSES
4219 if (dpy.type == DISPLAY_TYPE_CURSES) {
4220 error_report("curses display cannot be used with -daemonize");
4221 exit(1);
4223 #endif
4226 if (nographic) {
4227 if (default_parallel)
4228 add_device_config(DEV_PARALLEL, "null");
4229 if (default_serial && default_monitor) {
4230 add_device_config(DEV_SERIAL, "mon:stdio");
4231 } else if (default_virtcon && default_monitor) {
4232 add_device_config(DEV_VIRTCON, "mon:stdio");
4233 } else {
4234 if (default_serial)
4235 add_device_config(DEV_SERIAL, "stdio");
4236 if (default_virtcon)
4237 add_device_config(DEV_VIRTCON, "stdio");
4238 if (default_monitor)
4239 monitor_parse("stdio", "readline", false);
4241 } else {
4242 if (default_serial)
4243 add_device_config(DEV_SERIAL, "vc:80Cx24C");
4244 if (default_parallel)
4245 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
4246 if (default_monitor)
4247 monitor_parse("vc:80Cx24C", "readline", false);
4248 if (default_virtcon)
4249 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
4252 #if defined(CONFIG_VNC)
4253 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
4254 display_remote++;
4256 #endif
4257 if (dpy.type == DISPLAY_TYPE_DEFAULT && !display_remote) {
4258 if (!qemu_display_find_default(&dpy)) {
4259 dpy.type = DISPLAY_TYPE_NONE;
4260 #if defined(CONFIG_VNC)
4261 vnc_parse("localhost:0,to=99,id=default", &error_abort);
4262 #endif
4265 if (dpy.type == DISPLAY_TYPE_DEFAULT) {
4266 dpy.type = DISPLAY_TYPE_NONE;
4269 if ((no_frame || alt_grab || ctrl_grab) && dpy.type != DISPLAY_TYPE_SDL) {
4270 error_report("-no-frame, -alt-grab and -ctrl-grab are only valid "
4271 "for SDL, ignoring option");
4273 if (dpy.has_window_close &&
4274 (dpy.type != DISPLAY_TYPE_GTK && dpy.type != DISPLAY_TYPE_SDL)) {
4275 error_report("-no-quit is only valid for GTK and SDL, "
4276 "ignoring option");
4279 qemu_display_early_init(&dpy);
4280 qemu_console_early_init();
4282 if (dpy.has_gl && dpy.gl != DISPLAYGL_MODE_OFF && display_opengl == 0) {
4283 #if defined(CONFIG_OPENGL)
4284 error_report("OpenGL is not supported by the display");
4285 #else
4286 error_report("OpenGL support is disabled");
4287 #endif
4288 exit(1);
4291 page_size_init();
4292 socket_init();
4294 qemu_opts_foreach(qemu_find_opts("object"),
4295 user_creatable_add_opts_foreach,
4296 object_create_initial, &error_fatal);
4298 qemu_opts_foreach(qemu_find_opts("chardev"),
4299 chardev_init_func, NULL, &error_fatal);
4301 #ifdef CONFIG_VIRTFS
4302 qemu_opts_foreach(qemu_find_opts("fsdev"),
4303 fsdev_init_func, NULL, &error_fatal);
4304 #endif
4306 if (qemu_opts_foreach(qemu_find_opts("device"),
4307 device_help_func, NULL, NULL)) {
4308 exit(0);
4311 machine_opts = qemu_get_machine_opts();
4312 qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
4313 &error_fatal);
4315 configure_accelerator(current_machine);
4317 if (!qtest_enabled() && machine_class->deprecation_reason) {
4318 error_report("Machine type '%s' is deprecated: %s",
4319 machine_class->name, machine_class->deprecation_reason);
4323 * Register all the global properties, including accel properties,
4324 * machine properties, and user-specified ones.
4326 register_global_properties(current_machine);
4329 * Migration object can only be created after global properties
4330 * are applied correctly.
4332 migration_object_init();
4334 if (qtest_chrdev) {
4335 qtest_init(qtest_chrdev, qtest_log, &error_fatal);
4338 machine_opts = qemu_get_machine_opts();
4339 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4340 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4341 kernel_cmdline = qemu_opt_get(machine_opts, "append");
4342 bios_name = qemu_opt_get(machine_opts, "firmware");
4344 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4345 if (opts) {
4346 boot_order = qemu_opt_get(opts, "order");
4347 if (boot_order) {
4348 validate_bootdevices(boot_order, &error_fatal);
4351 boot_once = qemu_opt_get(opts, "once");
4352 if (boot_once) {
4353 validate_bootdevices(boot_once, &error_fatal);
4356 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
4357 boot_strict = qemu_opt_get_bool(opts, "strict", false);
4360 if (!boot_order) {
4361 boot_order = machine_class->default_boot_order;
4364 if (!kernel_cmdline) {
4365 kernel_cmdline = "";
4366 current_machine->kernel_cmdline = (char *)kernel_cmdline;
4369 linux_boot = (kernel_filename != NULL);
4371 if (!linux_boot && *kernel_cmdline != '\0') {
4372 error_report("-append only allowed with -kernel option");
4373 exit(1);
4376 if (!linux_boot && initrd_filename != NULL) {
4377 error_report("-initrd only allowed with -kernel option");
4378 exit(1);
4381 if (semihosting_enabled() && !semihosting_get_argc() && kernel_filename) {
4382 /* fall back to the -kernel/-append */
4383 semihosting_arg_fallback(kernel_filename, kernel_cmdline);
4386 os_set_line_buffering();
4388 /* spice needs the timers to be initialized by this point */
4389 qemu_spice_init();
4391 cpu_ticks_init();
4392 if (icount_opts) {
4393 if (!tcg_enabled()) {
4394 error_report("-icount is not allowed with hardware virtualization");
4395 exit(1);
4397 configure_icount(icount_opts, &error_abort);
4398 qemu_opts_del(icount_opts);
4401 if (tcg_enabled()) {
4402 qemu_tcg_configure(accel_opts, &error_fatal);
4405 if (default_net) {
4406 QemuOptsList *net = qemu_find_opts("net");
4407 qemu_opts_set(net, NULL, "type", "nic", &error_abort);
4408 #ifdef CONFIG_SLIRP
4409 qemu_opts_set(net, NULL, "type", "user", &error_abort);
4410 #endif
4413 if (net_init_clients(&err) < 0) {
4414 error_report_err(err);
4415 exit(1);
4418 qemu_opts_foreach(qemu_find_opts("object"),
4419 user_creatable_add_opts_foreach,
4420 object_create_delayed, &error_fatal);
4422 tpm_init();
4424 /* init the bluetooth world */
4425 if (foreach_device_config(DEV_BT, bt_parse))
4426 exit(1);
4428 if (!xen_enabled()) {
4429 /* On 32-bit hosts, QEMU is limited by virtual address space */
4430 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4431 error_report("at most 2047 MB RAM can be simulated");
4432 exit(1);
4436 blk_mig_init();
4437 ram_mig_init();
4438 dirty_bitmap_mig_init();
4440 /* If the currently selected machine wishes to override the units-per-bus
4441 * property of its default HBA interface type, do so now. */
4442 if (machine_class->units_per_default_bus) {
4443 override_max_devs(machine_class->block_default_type,
4444 machine_class->units_per_default_bus);
4447 /* open the virtual block devices */
4448 while (!QSIMPLEQ_EMPTY(&bdo_queue)) {
4449 BlockdevOptions_queue *bdo = QSIMPLEQ_FIRST(&bdo_queue);
4451 QSIMPLEQ_REMOVE_HEAD(&bdo_queue, entry);
4452 loc_push_restore(&bdo->loc);
4453 qmp_blockdev_add(bdo->bdo, &error_fatal);
4454 loc_pop(&bdo->loc);
4455 qapi_free_BlockdevOptions(bdo->bdo);
4456 g_free(bdo);
4458 if (snapshot || replay_mode != REPLAY_MODE_NONE) {
4459 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
4460 NULL, NULL);
4462 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
4463 &machine_class->block_default_type, &error_fatal)) {
4464 /* We printed help */
4465 exit(0);
4468 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
4469 CDROM_OPTS);
4470 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4471 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
4473 qemu_opts_foreach(qemu_find_opts("mon"),
4474 mon_init_func, NULL, &error_fatal);
4476 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4477 exit(1);
4478 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4479 exit(1);
4480 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4481 exit(1);
4482 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4483 exit(1);
4485 /* If no default VGA is requested, the default is "none". */
4486 if (default_vga) {
4487 if (machine_class->default_display) {
4488 vga_model = machine_class->default_display;
4489 } else if (vga_interface_available(VGA_CIRRUS)) {
4490 vga_model = "cirrus";
4491 } else if (vga_interface_available(VGA_STD)) {
4492 vga_model = "std";
4495 if (vga_model) {
4496 select_vgahw(vga_model);
4499 if (watchdog) {
4500 i = select_watchdog(watchdog);
4501 if (i > 0)
4502 exit (i == 1 ? 1 : 0);
4505 /* This checkpoint is required by replay to separate prior clock
4506 reading from the other reads, because timer polling functions query
4507 clock values from the log. */
4508 replay_checkpoint(CHECKPOINT_INIT);
4509 qdev_machine_init();
4511 current_machine->ram_size = ram_size;
4512 current_machine->maxram_size = maxram_size;
4513 current_machine->ram_slots = ram_slots;
4514 current_machine->boot_order = boot_order;
4516 /* parse features once if machine provides default cpu_type */
4517 current_machine->cpu_type = machine_class->default_cpu_type;
4518 if (cpu_model) {
4519 current_machine->cpu_type = parse_cpu_model(cpu_model);
4521 parse_numa_opts(current_machine);
4523 /* do monitor/qmp handling at preconfig state if requested */
4524 main_loop();
4526 /* from here on runstate is RUN_STATE_PRELAUNCH */
4527 machine_run_board_init(current_machine);
4529 realtime_init();
4531 soundhw_init();
4533 if (hax_enabled()) {
4534 hax_sync_vcpus();
4537 qemu_opts_foreach(qemu_find_opts("fw_cfg"),
4538 parse_fw_cfg, fw_cfg_find(), &error_fatal);
4540 /* init USB devices */
4541 if (machine_usb(current_machine)) {
4542 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4543 exit(1);
4546 /* Check if IGD GFX passthrough. */
4547 igd_gfx_passthru();
4549 /* init generic devices */
4550 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
4551 qemu_opts_foreach(qemu_find_opts("device"),
4552 device_init_func, NULL, &error_fatal);
4554 cpu_synchronize_all_post_init();
4556 rom_reset_order_override();
4558 /* Did we create any drives that we failed to create a device for? */
4559 drive_check_orphaned();
4561 /* Don't warn about the default network setup that you get if
4562 * no command line -net or -netdev options are specified. There
4563 * are two cases that we would otherwise complain about:
4564 * (1) board doesn't support a NIC but the implicit "-net nic"
4565 * requested one
4566 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
4567 * sets up a nic that isn't connected to anything.
4569 if (!default_net && (!qtest_enabled() || has_defaults)) {
4570 net_check_clients();
4573 if (boot_once) {
4574 qemu_boot_set(boot_once, &error_fatal);
4575 qemu_register_reset(restore_boot_order, g_strdup(boot_order));
4578 /* init local displays */
4579 ds = init_displaystate();
4580 qemu_display_init(ds, &dpy);
4582 /* must be after terminal init, SDL library changes signal handlers */
4583 os_setup_signal_handling();
4585 /* init remote displays */
4586 #ifdef CONFIG_VNC
4587 qemu_opts_foreach(qemu_find_opts("vnc"),
4588 vnc_init_func, NULL, &error_fatal);
4589 #endif
4591 if (using_spice) {
4592 qemu_spice_display_init();
4595 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
4596 exit(1);
4599 qdev_machine_creation_done();
4601 /* TODO: once all bus devices are qdevified, this should be done
4602 * when bus is created by qdev.c */
4603 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4604 qemu_run_machine_init_done_notifiers();
4606 if (rom_check_and_register_reset() != 0) {
4607 error_report("rom check and register reset failed");
4608 exit(1);
4611 replay_start();
4613 /* This checkpoint is required by replay to separate prior clock
4614 reading from the other reads, because timer polling functions query
4615 clock values from the log. */
4616 replay_checkpoint(CHECKPOINT_RESET);
4617 qemu_system_reset(SHUTDOWN_CAUSE_NONE);
4618 register_global_state();
4619 if (loadvm) {
4620 Error *local_err = NULL;
4621 if (load_snapshot(loadvm, &local_err) < 0) {
4622 error_report_err(local_err);
4623 autostart = 0;
4624 exit(1);
4627 if (replay_mode != REPLAY_MODE_NONE) {
4628 replay_vmstate_init();
4631 qdev_prop_check_globals();
4632 if (vmstate_dump_file) {
4633 /* dump and exit */
4634 dump_vmstate_json_to_file(vmstate_dump_file);
4635 return 0;
4638 if (incoming) {
4639 Error *local_err = NULL;
4640 qemu_start_incoming_migration(incoming, &local_err);
4641 if (local_err) {
4642 error_reportf_err(local_err, "-incoming %s: ", incoming);
4643 exit(1);
4645 } else if (autostart) {
4646 vm_start();
4649 accel_setup_post(current_machine);
4650 os_setup_post();
4652 main_loop();
4654 gdbserver_cleanup();
4656 /* No more vcpu or device emulation activity beyond this point */
4657 vm_shutdown();
4659 job_cancel_sync_all();
4660 bdrv_close_all();
4662 res_free();
4664 /* vhost-user must be cleaned up before chardevs. */
4665 tpm_cleanup();
4666 net_cleanup();
4667 audio_cleanup();
4668 monitor_cleanup();
4669 qemu_chr_cleanup();
4670 user_creatable_cleanup();
4671 migration_object_finalize();
4672 /* TODO: unref root container, check all devices are ok */
4674 return 0;