Merge remote-tracking branch 'dgibson/tags/ppc-for-2.10-20170525' into staging
[qemu/ar7.git] / vl.c
blob137f389699ea2377bdb7b172bc4339c564e60724
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.
24 #include "qemu/osdep.h"
25 #include "qemu-version.h"
26 #include "qemu/cutils.h"
27 #include "qemu/help_option.h"
28 #include "qemu/uuid.h"
30 #ifdef CONFIG_SECCOMP
31 #include "sysemu/seccomp.h"
32 #endif
34 #if defined(CONFIG_VDE)
35 #include <libvdeplug.h>
36 #endif
38 #ifdef CONFIG_SDL
39 #if defined(__APPLE__) || defined(main)
40 #include <SDL.h>
41 int qemu_main(int argc, char **argv, char **envp);
42 int main(int argc, char **argv)
44 return qemu_main(argc, argv, NULL);
46 #undef main
47 #define main qemu_main
48 #endif
49 #endif /* CONFIG_SDL */
51 #ifdef CONFIG_COCOA
52 #undef main
53 #define main qemu_main
54 #endif /* CONFIG_COCOA */
57 #include "qemu/error-report.h"
58 #include "qemu/sockets.h"
59 #include "hw/hw.h"
60 #include "hw/boards.h"
61 #include "sysemu/accel.h"
62 #include "hw/usb.h"
63 #include "hw/i386/pc.h"
64 #include "hw/isa/isa.h"
65 #include "hw/scsi/scsi.h"
66 #include "hw/bt.h"
67 #include "sysemu/watchdog.h"
68 #include "hw/smbios/smbios.h"
69 #include "hw/acpi/acpi.h"
70 #include "hw/xen/xen.h"
71 #include "hw/qdev.h"
72 #include "hw/loader.h"
73 #include "monitor/qdev.h"
74 #include "sysemu/bt.h"
75 #include "net/net.h"
76 #include "net/slirp.h"
77 #include "monitor/monitor.h"
78 #include "ui/console.h"
79 #include "ui/input.h"
80 #include "sysemu/sysemu.h"
81 #include "sysemu/numa.h"
82 #include "exec/gdbstub.h"
83 #include "qemu/timer.h"
84 #include "sysemu/char.h"
85 #include "qemu/bitmap.h"
86 #include "qemu/log.h"
87 #include "sysemu/blockdev.h"
88 #include "hw/block/block.h"
89 #include "migration/block.h"
90 #include "sysemu/tpm.h"
91 #include "sysemu/dma.h"
92 #include "hw/audio/soundhw.h"
93 #include "audio/audio.h"
94 #include "migration/migration.h"
95 #include "sysemu/cpus.h"
96 #include "migration/colo.h"
97 #include "sysemu/kvm.h"
98 #include "sysemu/hax.h"
99 #include "qapi/qobject-input-visitor.h"
100 #include "qapi/qobject-input-visitor.h"
101 #include "qapi-visit.h"
102 #include "qapi/qmp/qjson.h"
103 #include "qemu/option.h"
104 #include "qemu/config-file.h"
105 #include "qemu-options.h"
106 #include "qmp-commands.h"
107 #include "qemu/main-loop.h"
108 #ifdef CONFIG_VIRTFS
109 #include "fsdev/qemu-fsdev.h"
110 #endif
111 #include "sysemu/qtest.h"
113 #include "disas/disas.h"
116 #include "slirp/libslirp.h"
118 #include "trace-root.h"
119 #include "trace/control.h"
120 #include "qemu/queue.h"
121 #include "sysemu/arch_init.h"
123 #include "ui/qemu-spice.h"
124 #include "qapi/string-input-visitor.h"
125 #include "qapi/opts-visitor.h"
126 #include "qom/object_interfaces.h"
127 #include "qapi-event.h"
128 #include "exec/semihost.h"
129 #include "crypto/init.h"
130 #include "sysemu/replay.h"
131 #include "qapi/qmp/qerror.h"
132 #include "sysemu/iothread.h"
134 #define MAX_VIRTIO_CONSOLES 1
135 #define MAX_SCLP_CONSOLES 1
137 static const char *data_dir[16];
138 static int data_dir_idx;
139 const char *bios_name = NULL;
140 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
141 int request_opengl = -1;
142 int display_opengl;
143 const char* keyboard_layout = NULL;
144 ram_addr_t ram_size;
145 const char *mem_path = NULL;
146 int mem_prealloc = 0; /* force preallocation of physical target memory */
147 bool enable_mlock = false;
148 int nb_nics;
149 NICInfo nd_table[MAX_NICS];
150 int autostart;
151 static int rtc_utc = 1;
152 static int rtc_date_offset = -1; /* -1 means no change */
153 QEMUClockType rtc_clock;
154 int vga_interface_type = VGA_NONE;
155 static int full_screen = 0;
156 static int no_frame = 0;
157 int no_quit = 0;
158 static bool grab_on_hover;
159 Chardev *serial_hds[MAX_SERIAL_PORTS];
160 Chardev *parallel_hds[MAX_PARALLEL_PORTS];
161 Chardev *virtcon_hds[MAX_VIRTIO_CONSOLES];
162 Chardev *sclp_hds[MAX_SCLP_CONSOLES];
163 int win2k_install_hack = 0;
164 int singlestep = 0;
165 int smp_cpus = 1;
166 int max_cpus = 1;
167 int smp_cores = 1;
168 int smp_threads = 1;
169 int acpi_enabled = 1;
170 int no_hpet = 0;
171 int fd_bootchk = 1;
172 static int no_reboot;
173 int no_shutdown = 0;
174 int cursor_hide = 1;
175 int graphic_rotate = 0;
176 const char *watchdog;
177 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
178 int nb_option_roms;
179 int old_param = 0;
180 const char *qemu_name;
181 int alt_grab = 0;
182 int ctrl_grab = 0;
183 unsigned int nb_prom_envs = 0;
184 const char *prom_envs[MAX_PROM_ENVS];
185 int boot_menu;
186 bool boot_strict;
187 uint8_t *boot_splash_filedata;
188 size_t boot_splash_filedata_size;
189 uint8_t qemu_extra_params_fw[2];
190 int only_migratable; /* turn it off unless user states otherwise */
192 int icount_align_option;
194 /* The bytes in qemu_uuid are in the order specified by RFC4122, _not_ in the
195 * little-endian "wire format" described in the SMBIOS 2.6 specification.
197 QemuUUID qemu_uuid;
198 bool qemu_uuid_set;
200 static NotifierList exit_notifiers =
201 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
203 static NotifierList machine_init_done_notifiers =
204 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
206 bool xen_allowed;
207 uint32_t xen_domid;
208 enum xen_mode xen_mode = XEN_EMULATE;
209 bool xen_domid_restrict;
211 static int has_defaults = 1;
212 static int default_serial = 1;
213 static int default_parallel = 1;
214 static int default_virtcon = 1;
215 static int default_sclp = 1;
216 static int default_monitor = 1;
217 static int default_floppy = 1;
218 static int default_cdrom = 1;
219 static int default_sdcard = 1;
220 static int default_vga = 1;
221 static int default_net = 1;
223 static struct {
224 const char *driver;
225 int *flag;
226 } default_list[] = {
227 { .driver = "isa-serial", .flag = &default_serial },
228 { .driver = "isa-parallel", .flag = &default_parallel },
229 { .driver = "isa-fdc", .flag = &default_floppy },
230 { .driver = "floppy", .flag = &default_floppy },
231 { .driver = "ide-cd", .flag = &default_cdrom },
232 { .driver = "ide-hd", .flag = &default_cdrom },
233 { .driver = "ide-drive", .flag = &default_cdrom },
234 { .driver = "scsi-cd", .flag = &default_cdrom },
235 { .driver = "scsi-hd", .flag = &default_cdrom },
236 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
237 { .driver = "virtio-serial", .flag = &default_virtcon },
238 { .driver = "VGA", .flag = &default_vga },
239 { .driver = "isa-vga", .flag = &default_vga },
240 { .driver = "cirrus-vga", .flag = &default_vga },
241 { .driver = "isa-cirrus-vga", .flag = &default_vga },
242 { .driver = "vmware-svga", .flag = &default_vga },
243 { .driver = "qxl-vga", .flag = &default_vga },
244 { .driver = "virtio-vga", .flag = &default_vga },
247 static QemuOptsList qemu_rtc_opts = {
248 .name = "rtc",
249 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
250 .desc = {
252 .name = "base",
253 .type = QEMU_OPT_STRING,
255 .name = "clock",
256 .type = QEMU_OPT_STRING,
258 .name = "driftfix",
259 .type = QEMU_OPT_STRING,
261 { /* end of list */ }
265 static QemuOptsList qemu_sandbox_opts = {
266 .name = "sandbox",
267 .implied_opt_name = "enable",
268 .head = QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts.head),
269 .desc = {
271 .name = "enable",
272 .type = QEMU_OPT_BOOL,
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_msg_opts = {
414 .name = "msg",
415 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
416 .desc = {
418 .name = "timestamp",
419 .type = QEMU_OPT_BOOL,
421 { /* end of list */ }
425 static QemuOptsList qemu_name_opts = {
426 .name = "name",
427 .implied_opt_name = "guest",
428 .merge_lists = true,
429 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
430 .desc = {
432 .name = "guest",
433 .type = QEMU_OPT_STRING,
434 .help = "Sets the name of the guest.\n"
435 "This name will be displayed in the SDL window caption.\n"
436 "The name will also be used for the VNC server",
437 }, {
438 .name = "process",
439 .type = QEMU_OPT_STRING,
440 .help = "Sets the name of the QEMU process, as shown in top etc",
441 }, {
442 .name = "debug-threads",
443 .type = QEMU_OPT_BOOL,
444 .help = "When enabled, name the individual threads; defaults off.\n"
445 "NOTE: The thread names are for debugging and not a\n"
446 "stable API.",
448 { /* End of list */ }
452 static QemuOptsList qemu_mem_opts = {
453 .name = "memory",
454 .implied_opt_name = "size",
455 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
456 .merge_lists = true,
457 .desc = {
459 .name = "size",
460 .type = QEMU_OPT_SIZE,
463 .name = "slots",
464 .type = QEMU_OPT_NUMBER,
467 .name = "maxmem",
468 .type = QEMU_OPT_SIZE,
470 { /* end of list */ }
474 static QemuOptsList qemu_icount_opts = {
475 .name = "icount",
476 .implied_opt_name = "shift",
477 .merge_lists = true,
478 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
479 .desc = {
481 .name = "shift",
482 .type = QEMU_OPT_STRING,
483 }, {
484 .name = "align",
485 .type = QEMU_OPT_BOOL,
486 }, {
487 .name = "sleep",
488 .type = QEMU_OPT_BOOL,
489 }, {
490 .name = "rr",
491 .type = QEMU_OPT_STRING,
492 }, {
493 .name = "rrfile",
494 .type = QEMU_OPT_STRING,
495 }, {
496 .name = "rrsnapshot",
497 .type = QEMU_OPT_STRING,
499 { /* end of list */ }
503 static QemuOptsList qemu_semihosting_config_opts = {
504 .name = "semihosting-config",
505 .implied_opt_name = "enable",
506 .head = QTAILQ_HEAD_INITIALIZER(qemu_semihosting_config_opts.head),
507 .desc = {
509 .name = "enable",
510 .type = QEMU_OPT_BOOL,
511 }, {
512 .name = "target",
513 .type = QEMU_OPT_STRING,
514 }, {
515 .name = "arg",
516 .type = QEMU_OPT_STRING,
518 { /* end of list */ }
522 static QemuOptsList qemu_fw_cfg_opts = {
523 .name = "fw_cfg",
524 .implied_opt_name = "name",
525 .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
526 .desc = {
528 .name = "name",
529 .type = QEMU_OPT_STRING,
530 .help = "Sets the fw_cfg name of the blob to be inserted",
531 }, {
532 .name = "file",
533 .type = QEMU_OPT_STRING,
534 .help = "Sets the name of the file from which\n"
535 "the fw_cfg blob will be loaded",
536 }, {
537 .name = "string",
538 .type = QEMU_OPT_STRING,
539 .help = "Sets content of the blob to be inserted from a string",
541 { /* end of list */ }
546 * Get machine options
548 * Returns: machine options (never null).
550 QemuOpts *qemu_get_machine_opts(void)
552 return qemu_find_opts_singleton("machine");
555 const char *qemu_get_vm_name(void)
557 return qemu_name;
560 static void res_free(void)
562 g_free(boot_splash_filedata);
563 boot_splash_filedata = NULL;
566 static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
568 const char *driver = qemu_opt_get(opts, "driver");
569 int i;
571 if (!driver)
572 return 0;
573 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
574 if (strcmp(default_list[i].driver, driver) != 0)
575 continue;
576 *(default_list[i].flag) = 0;
578 return 0;
581 /***********************************************************/
582 /* QEMU state */
584 static RunState current_run_state = RUN_STATE_PRELAUNCH;
586 /* We use RUN_STATE__MAX but any invalid value will do */
587 static RunState vmstop_requested = RUN_STATE__MAX;
588 static QemuMutex vmstop_lock;
590 typedef struct {
591 RunState from;
592 RunState to;
593 } RunStateTransition;
595 static const RunStateTransition runstate_transitions_def[] = {
596 /* from -> to */
597 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
598 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
599 { RUN_STATE_DEBUG, RUN_STATE_PRELAUNCH },
601 { RUN_STATE_INMIGRATE, RUN_STATE_INTERNAL_ERROR },
602 { RUN_STATE_INMIGRATE, RUN_STATE_IO_ERROR },
603 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
604 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
605 { RUN_STATE_INMIGRATE, RUN_STATE_SHUTDOWN },
606 { RUN_STATE_INMIGRATE, RUN_STATE_SUSPENDED },
607 { RUN_STATE_INMIGRATE, RUN_STATE_WATCHDOG },
608 { RUN_STATE_INMIGRATE, RUN_STATE_GUEST_PANICKED },
609 { RUN_STATE_INMIGRATE, RUN_STATE_FINISH_MIGRATE },
610 { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH },
611 { RUN_STATE_INMIGRATE, RUN_STATE_POSTMIGRATE },
612 { RUN_STATE_INMIGRATE, RUN_STATE_COLO },
614 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
615 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
616 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PRELAUNCH },
618 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
619 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
620 { RUN_STATE_IO_ERROR, RUN_STATE_PRELAUNCH },
622 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
623 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
624 { RUN_STATE_PAUSED, RUN_STATE_PRELAUNCH },
625 { RUN_STATE_PAUSED, RUN_STATE_COLO},
627 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
628 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
629 { RUN_STATE_POSTMIGRATE, RUN_STATE_PRELAUNCH },
631 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
632 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
633 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
635 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
636 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
637 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PRELAUNCH },
638 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_COLO},
640 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
641 { RUN_STATE_RESTORE_VM, RUN_STATE_PRELAUNCH },
643 { RUN_STATE_COLO, RUN_STATE_RUNNING },
645 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
646 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
647 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
648 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
649 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
650 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
651 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
652 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
653 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
654 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
655 { RUN_STATE_RUNNING, RUN_STATE_COLO},
657 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
659 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
660 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
661 { RUN_STATE_SHUTDOWN, RUN_STATE_PRELAUNCH },
663 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
664 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
665 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
666 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
667 { RUN_STATE_SUSPENDED, RUN_STATE_PRELAUNCH },
668 { RUN_STATE_SUSPENDED, RUN_STATE_COLO},
670 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
671 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
672 { RUN_STATE_WATCHDOG, RUN_STATE_PRELAUNCH },
673 { RUN_STATE_WATCHDOG, RUN_STATE_COLO},
675 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
676 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
677 { RUN_STATE_GUEST_PANICKED, RUN_STATE_PRELAUNCH },
679 { RUN_STATE__MAX, RUN_STATE__MAX },
682 static bool runstate_valid_transitions[RUN_STATE__MAX][RUN_STATE__MAX];
684 bool runstate_check(RunState state)
686 return current_run_state == state;
689 bool runstate_store(char *str, size_t size)
691 const char *state = RunState_lookup[current_run_state];
692 size_t len = strlen(state) + 1;
694 if (len > size) {
695 return false;
697 memcpy(str, state, len);
698 return true;
701 static void runstate_init(void)
703 const RunStateTransition *p;
705 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
706 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE__MAX; p++) {
707 runstate_valid_transitions[p->from][p->to] = true;
710 qemu_mutex_init(&vmstop_lock);
713 /* This function will abort() on invalid state transitions */
714 void runstate_set(RunState new_state)
716 assert(new_state < RUN_STATE__MAX);
718 if (current_run_state == new_state) {
719 return;
722 if (!runstate_valid_transitions[current_run_state][new_state]) {
723 error_report("invalid runstate transition: '%s' -> '%s'",
724 RunState_lookup[current_run_state],
725 RunState_lookup[new_state]);
726 abort();
728 trace_runstate_set(new_state);
729 current_run_state = new_state;
732 int runstate_is_running(void)
734 return runstate_check(RUN_STATE_RUNNING);
737 bool runstate_needs_reset(void)
739 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
740 runstate_check(RUN_STATE_SHUTDOWN);
743 StatusInfo *qmp_query_status(Error **errp)
745 StatusInfo *info = g_malloc0(sizeof(*info));
747 info->running = runstate_is_running();
748 info->singlestep = singlestep;
749 info->status = current_run_state;
751 return info;
754 bool qemu_vmstop_requested(RunState *r)
756 qemu_mutex_lock(&vmstop_lock);
757 *r = vmstop_requested;
758 vmstop_requested = RUN_STATE__MAX;
759 qemu_mutex_unlock(&vmstop_lock);
760 return *r < RUN_STATE__MAX;
763 void qemu_system_vmstop_request_prepare(void)
765 qemu_mutex_lock(&vmstop_lock);
768 void qemu_system_vmstop_request(RunState state)
770 vmstop_requested = state;
771 qemu_mutex_unlock(&vmstop_lock);
772 qemu_notify_event();
775 /***********************************************************/
776 /* real time host monotonic timer */
778 static time_t qemu_time(void)
780 return qemu_clock_get_ms(QEMU_CLOCK_HOST) / 1000;
783 /***********************************************************/
784 /* host time/date access */
785 void qemu_get_timedate(struct tm *tm, int offset)
787 time_t ti = qemu_time();
789 ti += offset;
790 if (rtc_date_offset == -1) {
791 if (rtc_utc)
792 gmtime_r(&ti, tm);
793 else
794 localtime_r(&ti, tm);
795 } else {
796 ti -= rtc_date_offset;
797 gmtime_r(&ti, tm);
801 int qemu_timedate_diff(struct tm *tm)
803 time_t seconds;
805 if (rtc_date_offset == -1)
806 if (rtc_utc)
807 seconds = mktimegm(tm);
808 else {
809 struct tm tmp = *tm;
810 tmp.tm_isdst = -1; /* use timezone to figure it out */
811 seconds = mktime(&tmp);
813 else
814 seconds = mktimegm(tm) + rtc_date_offset;
816 return seconds - qemu_time();
819 static void configure_rtc_date_offset(const char *startdate, int legacy)
821 time_t rtc_start_date;
822 struct tm tm;
824 if (!strcmp(startdate, "now") && legacy) {
825 rtc_date_offset = -1;
826 } else {
827 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
828 &tm.tm_year,
829 &tm.tm_mon,
830 &tm.tm_mday,
831 &tm.tm_hour,
832 &tm.tm_min,
833 &tm.tm_sec) == 6) {
834 /* OK */
835 } else if (sscanf(startdate, "%d-%d-%d",
836 &tm.tm_year,
837 &tm.tm_mon,
838 &tm.tm_mday) == 3) {
839 tm.tm_hour = 0;
840 tm.tm_min = 0;
841 tm.tm_sec = 0;
842 } else {
843 goto date_fail;
845 tm.tm_year -= 1900;
846 tm.tm_mon--;
847 rtc_start_date = mktimegm(&tm);
848 if (rtc_start_date == -1) {
849 date_fail:
850 error_report("invalid date format");
851 error_printf("valid formats: "
852 "'2006-06-17T16:01:21' or '2006-06-17'\n");
853 exit(1);
855 rtc_date_offset = qemu_time() - rtc_start_date;
859 static void configure_rtc(QemuOpts *opts)
861 const char *value;
863 value = qemu_opt_get(opts, "base");
864 if (value) {
865 if (!strcmp(value, "utc")) {
866 rtc_utc = 1;
867 } else if (!strcmp(value, "localtime")) {
868 Error *blocker = NULL;
869 rtc_utc = 0;
870 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
871 "-rtc base=localtime");
872 replay_add_blocker(blocker);
873 } else {
874 configure_rtc_date_offset(value, 0);
877 value = qemu_opt_get(opts, "clock");
878 if (value) {
879 if (!strcmp(value, "host")) {
880 rtc_clock = QEMU_CLOCK_HOST;
881 } else if (!strcmp(value, "rt")) {
882 rtc_clock = QEMU_CLOCK_REALTIME;
883 } else if (!strcmp(value, "vm")) {
884 rtc_clock = QEMU_CLOCK_VIRTUAL;
885 } else {
886 error_report("invalid option value '%s'", value);
887 exit(1);
890 value = qemu_opt_get(opts, "driftfix");
891 if (value) {
892 if (!strcmp(value, "slew")) {
893 static GlobalProperty slew_lost_ticks = {
894 .driver = "mc146818rtc",
895 .property = "lost_tick_policy",
896 .value = "slew",
899 qdev_prop_register_global(&slew_lost_ticks);
900 } else if (!strcmp(value, "none")) {
901 /* discard is default */
902 } else {
903 error_report("invalid option value '%s'", value);
904 exit(1);
909 /***********************************************************/
910 /* Bluetooth support */
911 static int nb_hcis;
912 static int cur_hci;
913 static struct HCIInfo *hci_table[MAX_NICS];
915 struct HCIInfo *qemu_next_hci(void)
917 if (cur_hci == nb_hcis)
918 return &null_hci;
920 return hci_table[cur_hci++];
923 static int bt_hci_parse(const char *str)
925 struct HCIInfo *hci;
926 bdaddr_t bdaddr;
928 if (nb_hcis >= MAX_NICS) {
929 error_report("too many bluetooth HCIs (max %i)", MAX_NICS);
930 return -1;
933 hci = hci_init(str);
934 if (!hci)
935 return -1;
937 bdaddr.b[0] = 0x52;
938 bdaddr.b[1] = 0x54;
939 bdaddr.b[2] = 0x00;
940 bdaddr.b[3] = 0x12;
941 bdaddr.b[4] = 0x34;
942 bdaddr.b[5] = 0x56 + nb_hcis;
943 hci->bdaddr_set(hci, bdaddr.b);
945 hci_table[nb_hcis++] = hci;
947 return 0;
950 static void bt_vhci_add(int vlan_id)
952 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
954 if (!vlan->slave)
955 error_report("warning: adding a VHCI to an empty scatternet %i",
956 vlan_id);
958 bt_vhci_init(bt_new_hci(vlan));
961 static struct bt_device_s *bt_device_add(const char *opt)
963 struct bt_scatternet_s *vlan;
964 int vlan_id = 0;
965 char *endp = strstr(opt, ",vlan=");
966 int len = (endp ? endp - opt : strlen(opt)) + 1;
967 char devname[10];
969 pstrcpy(devname, MIN(sizeof(devname), len), opt);
971 if (endp) {
972 vlan_id = strtol(endp + 6, &endp, 0);
973 if (*endp) {
974 error_report("unrecognised bluetooth vlan Id");
975 return 0;
979 vlan = qemu_find_bt_vlan(vlan_id);
981 if (!vlan->slave)
982 error_report("warning: adding a slave device to an empty scatternet %i",
983 vlan_id);
985 if (!strcmp(devname, "keyboard"))
986 return bt_keyboard_init(vlan);
988 error_report("unsupported bluetooth device '%s'", devname);
989 return 0;
992 static int bt_parse(const char *opt)
994 const char *endp, *p;
995 int vlan;
997 if (strstart(opt, "hci", &endp)) {
998 if (!*endp || *endp == ',') {
999 if (*endp)
1000 if (!strstart(endp, ",vlan=", 0))
1001 opt = endp + 1;
1003 return bt_hci_parse(opt);
1005 } else if (strstart(opt, "vhci", &endp)) {
1006 if (!*endp || *endp == ',') {
1007 if (*endp) {
1008 if (strstart(endp, ",vlan=", &p)) {
1009 vlan = strtol(p, (char **) &endp, 0);
1010 if (*endp) {
1011 error_report("bad scatternet '%s'", p);
1012 return 1;
1014 } else {
1015 error_report("bad parameter '%s'", endp + 1);
1016 return 1;
1018 } else
1019 vlan = 0;
1021 bt_vhci_add(vlan);
1022 return 0;
1024 } else if (strstart(opt, "device:", &endp))
1025 return !bt_device_add(endp);
1027 error_report("bad bluetooth parameter '%s'", opt);
1028 return 1;
1031 static int parse_sandbox(void *opaque, QemuOpts *opts, Error **errp)
1033 /* FIXME: change this to true for 1.3 */
1034 if (qemu_opt_get_bool(opts, "enable", false)) {
1035 #ifdef CONFIG_SECCOMP
1036 if (seccomp_start() < 0) {
1037 error_report("failed to install seccomp syscall filter "
1038 "in the kernel");
1039 return -1;
1041 #else
1042 error_report("seccomp support is disabled");
1043 return -1;
1044 #endif
1047 return 0;
1050 static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
1052 const char *proc_name;
1054 if (qemu_opt_get(opts, "debug-threads")) {
1055 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
1057 qemu_name = qemu_opt_get(opts, "guest");
1059 proc_name = qemu_opt_get(opts, "process");
1060 if (proc_name) {
1061 os_set_proc_name(proc_name);
1064 return 0;
1067 bool defaults_enabled(void)
1069 return has_defaults;
1072 #ifndef _WIN32
1073 static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1075 int fd, dupfd, flags;
1076 int64_t fdset_id;
1077 const char *fd_opaque = NULL;
1078 AddfdInfo *fdinfo;
1080 fd = qemu_opt_get_number(opts, "fd", -1);
1081 fdset_id = qemu_opt_get_number(opts, "set", -1);
1082 fd_opaque = qemu_opt_get(opts, "opaque");
1084 if (fd < 0) {
1085 error_report("fd option is required and must be non-negative");
1086 return -1;
1089 if (fd <= STDERR_FILENO) {
1090 error_report("fd cannot be a standard I/O stream");
1091 return -1;
1095 * All fds inherited across exec() necessarily have FD_CLOEXEC
1096 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1098 flags = fcntl(fd, F_GETFD);
1099 if (flags == -1 || (flags & FD_CLOEXEC)) {
1100 error_report("fd is not valid or already in use");
1101 return -1;
1104 if (fdset_id < 0) {
1105 error_report("set option is required and must be non-negative");
1106 return -1;
1109 #ifdef F_DUPFD_CLOEXEC
1110 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1111 #else
1112 dupfd = dup(fd);
1113 if (dupfd != -1) {
1114 qemu_set_cloexec(dupfd);
1116 #endif
1117 if (dupfd == -1) {
1118 error_report("error duplicating fd: %s", strerror(errno));
1119 return -1;
1122 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1123 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
1124 &error_abort);
1125 g_free(fdinfo);
1127 return 0;
1130 static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1132 int fd;
1134 fd = qemu_opt_get_number(opts, "fd", -1);
1135 close(fd);
1137 return 0;
1139 #endif
1141 /***********************************************************/
1142 /* QEMU Block devices */
1144 #define HD_OPTS "media=disk"
1145 #define CDROM_OPTS "media=cdrom"
1146 #define FD_OPTS ""
1147 #define PFLASH_OPTS ""
1148 #define MTD_OPTS ""
1149 #define SD_OPTS ""
1151 static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
1153 BlockInterfaceType *block_default_type = opaque;
1155 return drive_new(opts, *block_default_type) == NULL;
1158 static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
1160 if (qemu_opt_get(opts, "snapshot") == NULL) {
1161 qemu_opt_set(opts, "snapshot", "on", &error_abort);
1163 return 0;
1166 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1167 int index, const char *optstr)
1169 QemuOpts *opts;
1170 DriveInfo *dinfo;
1172 if (!enable || drive_get_by_index(type, index)) {
1173 return;
1176 opts = drive_add(type, index, NULL, optstr);
1177 if (snapshot) {
1178 drive_enable_snapshot(NULL, opts, NULL);
1181 dinfo = drive_new(opts, type);
1182 if (!dinfo) {
1183 exit(1);
1185 dinfo->is_default = true;
1189 static QemuOptsList qemu_smp_opts = {
1190 .name = "smp-opts",
1191 .implied_opt_name = "cpus",
1192 .merge_lists = true,
1193 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
1194 .desc = {
1196 .name = "cpus",
1197 .type = QEMU_OPT_NUMBER,
1198 }, {
1199 .name = "sockets",
1200 .type = QEMU_OPT_NUMBER,
1201 }, {
1202 .name = "cores",
1203 .type = QEMU_OPT_NUMBER,
1204 }, {
1205 .name = "threads",
1206 .type = QEMU_OPT_NUMBER,
1207 }, {
1208 .name = "maxcpus",
1209 .type = QEMU_OPT_NUMBER,
1211 { /*End of list */ }
1215 static void smp_parse(QemuOpts *opts)
1217 if (opts) {
1218 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
1219 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
1220 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
1221 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
1223 /* compute missing values, prefer sockets over cores over threads */
1224 if (cpus == 0 || sockets == 0) {
1225 sockets = sockets > 0 ? sockets : 1;
1226 cores = cores > 0 ? cores : 1;
1227 threads = threads > 0 ? threads : 1;
1228 if (cpus == 0) {
1229 cpus = cores * threads * sockets;
1231 } else if (cores == 0) {
1232 threads = threads > 0 ? threads : 1;
1233 cores = cpus / (sockets * threads);
1234 cores = cores > 0 ? cores : 1;
1235 } else if (threads == 0) {
1236 threads = cpus / (cores * sockets);
1237 threads = threads > 0 ? threads : 1;
1238 } else if (sockets * cores * threads < cpus) {
1239 error_report("cpu topology: "
1240 "sockets (%u) * cores (%u) * threads (%u) < "
1241 "smp_cpus (%u)",
1242 sockets, cores, threads, cpus);
1243 exit(1);
1246 max_cpus = qemu_opt_get_number(opts, "maxcpus", cpus);
1248 if (max_cpus < cpus) {
1249 error_report("maxcpus must be equal to or greater than smp");
1250 exit(1);
1253 if (sockets * cores * threads > max_cpus) {
1254 error_report("cpu topology: "
1255 "sockets (%u) * cores (%u) * threads (%u) > "
1256 "maxcpus (%u)",
1257 sockets, cores, threads, max_cpus);
1258 exit(1);
1261 smp_cpus = cpus;
1262 smp_cores = cores;
1263 smp_threads = threads;
1266 if (smp_cpus > 1) {
1267 Error *blocker = NULL;
1268 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
1269 replay_add_blocker(blocker);
1273 static void realtime_init(void)
1275 if (enable_mlock) {
1276 if (os_mlock() < 0) {
1277 error_report("locking memory failed");
1278 exit(1);
1284 static void configure_msg(QemuOpts *opts)
1286 enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
1289 /***********************************************************/
1290 /* Semihosting */
1292 typedef struct SemihostingConfig {
1293 bool enabled;
1294 SemihostingTarget target;
1295 const char **argv;
1296 int argc;
1297 const char *cmdline; /* concatenated argv */
1298 } SemihostingConfig;
1300 static SemihostingConfig semihosting;
1302 bool semihosting_enabled(void)
1304 return semihosting.enabled;
1307 SemihostingTarget semihosting_get_target(void)
1309 return semihosting.target;
1312 const char *semihosting_get_arg(int i)
1314 if (i >= semihosting.argc) {
1315 return NULL;
1317 return semihosting.argv[i];
1320 int semihosting_get_argc(void)
1322 return semihosting.argc;
1325 const char *semihosting_get_cmdline(void)
1327 if (semihosting.cmdline == NULL && semihosting.argc > 0) {
1328 semihosting.cmdline = g_strjoinv(" ", (gchar **)semihosting.argv);
1330 return semihosting.cmdline;
1333 static int add_semihosting_arg(void *opaque,
1334 const char *name, const char *val,
1335 Error **errp)
1337 SemihostingConfig *s = opaque;
1338 if (strcmp(name, "arg") == 0) {
1339 s->argc++;
1340 /* one extra element as g_strjoinv() expects NULL-terminated array */
1341 s->argv = g_realloc(s->argv, (s->argc + 1) * sizeof(void *));
1342 s->argv[s->argc - 1] = val;
1343 s->argv[s->argc] = NULL;
1345 return 0;
1348 /* Use strings passed via -kernel/-append to initialize semihosting.argv[] */
1349 static inline void semihosting_arg_fallback(const char *file, const char *cmd)
1351 char *cmd_token;
1353 /* argv[0] */
1354 add_semihosting_arg(&semihosting, "arg", file, NULL);
1356 /* split -append and initialize argv[1..n] */
1357 cmd_token = strtok(g_strdup(cmd), " ");
1358 while (cmd_token) {
1359 add_semihosting_arg(&semihosting, "arg", cmd_token, NULL);
1360 cmd_token = strtok(NULL, " ");
1364 /* Now we still need this for compatibility with XEN. */
1365 bool has_igd_gfx_passthru;
1366 static void igd_gfx_passthru(void)
1368 has_igd_gfx_passthru = current_machine->igd_gfx_passthru;
1371 /***********************************************************/
1372 /* USB devices */
1374 static int usb_device_add(const char *devname)
1376 USBDevice *dev = NULL;
1377 #ifndef CONFIG_LINUX
1378 const char *p;
1379 #endif
1381 if (!machine_usb(current_machine)) {
1382 return -1;
1385 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1386 dev = usbdevice_create(devname);
1387 if (dev)
1388 goto done;
1390 /* the other ones */
1391 #ifndef CONFIG_LINUX
1392 /* only the linux version is qdev-ified, usb-bsd still needs this */
1393 if (strstart(devname, "host:", &p)) {
1394 dev = usb_host_device_open(usb_bus_find(-1), p);
1396 #endif
1397 if (!dev)
1398 return -1;
1400 done:
1401 return 0;
1404 static int usb_device_del(const char *devname)
1406 int bus_num, addr;
1407 const char *p;
1409 if (strstart(devname, "host:", &p)) {
1410 return -1;
1413 if (!machine_usb(current_machine)) {
1414 return -1;
1417 p = strchr(devname, '.');
1418 if (!p)
1419 return -1;
1420 bus_num = strtoul(devname, NULL, 0);
1421 addr = strtoul(p + 1, NULL, 0);
1423 return usb_device_delete_addr(bus_num, addr);
1426 static int usb_parse(const char *cmdline)
1428 int r;
1429 r = usb_device_add(cmdline);
1430 if (r < 0) {
1431 error_report("could not add USB device '%s'", cmdline);
1433 return r;
1436 void hmp_usb_add(Monitor *mon, const QDict *qdict)
1438 const char *devname = qdict_get_str(qdict, "devname");
1439 if (usb_device_add(devname) < 0) {
1440 error_report("could not add USB device '%s'", devname);
1444 void hmp_usb_del(Monitor *mon, const QDict *qdict)
1446 const char *devname = qdict_get_str(qdict, "devname");
1447 if (usb_device_del(devname) < 0) {
1448 error_report("could not delete USB device '%s'", devname);
1452 /***********************************************************/
1453 /* machine registration */
1455 MachineState *current_machine;
1457 static MachineClass *find_machine(const char *name)
1459 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1460 MachineClass *mc = NULL;
1462 for (el = machines; el; el = el->next) {
1463 MachineClass *temp = el->data;
1465 if (!strcmp(temp->name, name)) {
1466 mc = temp;
1467 break;
1469 if (temp->alias &&
1470 !strcmp(temp->alias, name)) {
1471 mc = temp;
1472 break;
1476 g_slist_free(machines);
1477 return mc;
1480 MachineClass *find_default_machine(void)
1482 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1483 MachineClass *mc = NULL;
1485 for (el = machines; el; el = el->next) {
1486 MachineClass *temp = el->data;
1488 if (temp->is_default) {
1489 mc = temp;
1490 break;
1494 g_slist_free(machines);
1495 return mc;
1498 MachineInfoList *qmp_query_machines(Error **errp)
1500 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1501 MachineInfoList *mach_list = NULL;
1503 for (el = machines; el; el = el->next) {
1504 MachineClass *mc = el->data;
1505 MachineInfoList *entry;
1506 MachineInfo *info;
1508 info = g_malloc0(sizeof(*info));
1509 if (mc->is_default) {
1510 info->has_is_default = true;
1511 info->is_default = true;
1514 if (mc->alias) {
1515 info->has_alias = true;
1516 info->alias = g_strdup(mc->alias);
1519 info->name = g_strdup(mc->name);
1520 info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
1521 info->hotpluggable_cpus = mc->has_hotpluggable_cpus;
1523 entry = g_malloc0(sizeof(*entry));
1524 entry->value = info;
1525 entry->next = mach_list;
1526 mach_list = entry;
1529 g_slist_free(machines);
1530 return mach_list;
1533 static int machine_help_func(QemuOpts *opts, MachineState *machine)
1535 ObjectProperty *prop;
1536 ObjectPropertyIterator iter;
1538 if (!qemu_opt_has_help_opt(opts)) {
1539 return 0;
1542 object_property_iter_init(&iter, OBJECT(machine));
1543 while ((prop = object_property_iter_next(&iter))) {
1544 if (!prop->set) {
1545 continue;
1548 error_printf("%s.%s=%s", MACHINE_GET_CLASS(machine)->name,
1549 prop->name, prop->type);
1550 if (prop->description) {
1551 error_printf(" (%s)\n", prop->description);
1552 } else {
1553 error_printf("\n");
1557 return 1;
1560 /***********************************************************/
1561 /* main execution loop */
1563 struct vm_change_state_entry {
1564 VMChangeStateHandler *cb;
1565 void *opaque;
1566 QLIST_ENTRY (vm_change_state_entry) entries;
1569 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1571 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1572 void *opaque)
1574 VMChangeStateEntry *e;
1576 e = g_malloc0(sizeof (*e));
1578 e->cb = cb;
1579 e->opaque = opaque;
1580 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1581 return e;
1584 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1586 QLIST_REMOVE (e, entries);
1587 g_free (e);
1590 void vm_state_notify(int running, RunState state)
1592 VMChangeStateEntry *e, *next;
1594 trace_vm_state_notify(running, state);
1596 QLIST_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
1597 e->cb(e->opaque, running, state);
1601 static ShutdownCause reset_requested;
1602 static ShutdownCause shutdown_requested;
1603 static int shutdown_signal;
1604 static pid_t shutdown_pid;
1605 static int powerdown_requested;
1606 static int debug_requested;
1607 static int suspend_requested;
1608 static WakeupReason wakeup_reason;
1609 static NotifierList powerdown_notifiers =
1610 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
1611 static NotifierList suspend_notifiers =
1612 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1613 static NotifierList wakeup_notifiers =
1614 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1615 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
1617 ShutdownCause qemu_shutdown_requested_get(void)
1619 return shutdown_requested;
1622 ShutdownCause qemu_reset_requested_get(void)
1624 return reset_requested;
1627 static int qemu_shutdown_requested(void)
1629 return atomic_xchg(&shutdown_requested, SHUTDOWN_CAUSE_NONE);
1632 static void qemu_kill_report(void)
1634 if (!qtest_driver() && shutdown_signal) {
1635 if (shutdown_pid == 0) {
1636 /* This happens for eg ^C at the terminal, so it's worth
1637 * avoiding printing an odd message in that case.
1639 error_report("terminating on signal %d", shutdown_signal);
1640 } else {
1641 char *shutdown_cmd = qemu_get_pid_name(shutdown_pid);
1643 error_report("terminating on signal %d from pid " FMT_pid " (%s)",
1644 shutdown_signal, shutdown_pid,
1645 shutdown_cmd ? shutdown_cmd : "<unknown process>");
1646 g_free(shutdown_cmd);
1648 shutdown_signal = 0;
1652 static ShutdownCause qemu_reset_requested(void)
1654 ShutdownCause r = reset_requested;
1656 if (r && replay_checkpoint(CHECKPOINT_RESET_REQUESTED)) {
1657 reset_requested = SHUTDOWN_CAUSE_NONE;
1658 return r;
1660 return SHUTDOWN_CAUSE_NONE;
1663 static int qemu_suspend_requested(void)
1665 int r = suspend_requested;
1666 if (r && replay_checkpoint(CHECKPOINT_SUSPEND_REQUESTED)) {
1667 suspend_requested = 0;
1668 return r;
1670 return false;
1673 static WakeupReason qemu_wakeup_requested(void)
1675 return wakeup_reason;
1678 static int qemu_powerdown_requested(void)
1680 int r = powerdown_requested;
1681 powerdown_requested = 0;
1682 return r;
1685 static int qemu_debug_requested(void)
1687 int r = debug_requested;
1688 debug_requested = 0;
1689 return r;
1693 * Reset the VM. Issue an event unless @reason is SHUTDOWN_CAUSE_NONE.
1695 void qemu_system_reset(ShutdownCause reason)
1697 MachineClass *mc;
1699 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1701 cpu_synchronize_all_states();
1703 if (mc && mc->reset) {
1704 mc->reset();
1705 } else {
1706 qemu_devices_reset();
1708 if (reason) {
1709 qapi_event_send_reset(shutdown_caused_by_guest(reason),
1710 &error_abort);
1712 cpu_synchronize_all_post_reset();
1715 void qemu_system_guest_panicked(GuestPanicInformation *info)
1717 qemu_log_mask(LOG_GUEST_ERROR, "Guest crashed\n");
1719 if (current_cpu) {
1720 current_cpu->crash_occurred = true;
1722 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE,
1723 !!info, info, &error_abort);
1724 vm_stop(RUN_STATE_GUEST_PANICKED);
1725 if (!no_shutdown) {
1726 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_POWEROFF,
1727 !!info, info, &error_abort);
1728 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_PANIC);
1731 if (info) {
1732 if (info->type == GUEST_PANIC_INFORMATION_TYPE_HYPER_V) {
1733 qemu_log_mask(LOG_GUEST_ERROR, "HV crash parameters: (%#"PRIx64
1734 " %#"PRIx64" %#"PRIx64" %#"PRIx64" %#"PRIx64")\n",
1735 info->u.hyper_v.arg1,
1736 info->u.hyper_v.arg2,
1737 info->u.hyper_v.arg3,
1738 info->u.hyper_v.arg4,
1739 info->u.hyper_v.arg5);
1741 qapi_free_GuestPanicInformation(info);
1745 void qemu_system_reset_request(ShutdownCause reason)
1747 if (no_reboot) {
1748 shutdown_requested = reason;
1749 } else {
1750 reset_requested = reason;
1752 cpu_stop_current();
1753 qemu_notify_event();
1756 static void qemu_system_suspend(void)
1758 pause_all_vcpus();
1759 notifier_list_notify(&suspend_notifiers, NULL);
1760 runstate_set(RUN_STATE_SUSPENDED);
1761 qapi_event_send_suspend(&error_abort);
1764 void qemu_system_suspend_request(void)
1766 if (runstate_check(RUN_STATE_SUSPENDED)) {
1767 return;
1769 suspend_requested = 1;
1770 cpu_stop_current();
1771 qemu_notify_event();
1774 void qemu_register_suspend_notifier(Notifier *notifier)
1776 notifier_list_add(&suspend_notifiers, notifier);
1779 void qemu_system_wakeup_request(WakeupReason reason)
1781 trace_system_wakeup_request(reason);
1783 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1784 return;
1786 if (!(wakeup_reason_mask & (1 << reason))) {
1787 return;
1789 runstate_set(RUN_STATE_RUNNING);
1790 wakeup_reason = reason;
1791 qemu_notify_event();
1794 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1796 if (enabled) {
1797 wakeup_reason_mask |= (1 << reason);
1798 } else {
1799 wakeup_reason_mask &= ~(1 << reason);
1803 void qemu_register_wakeup_notifier(Notifier *notifier)
1805 notifier_list_add(&wakeup_notifiers, notifier);
1808 void qemu_system_killed(int signal, pid_t pid)
1810 shutdown_signal = signal;
1811 shutdown_pid = pid;
1812 no_shutdown = 0;
1814 /* Cannot call qemu_system_shutdown_request directly because
1815 * we are in a signal handler.
1817 shutdown_requested = SHUTDOWN_CAUSE_HOST_SIGNAL;
1818 qemu_notify_event();
1821 void qemu_system_shutdown_request(ShutdownCause reason)
1823 trace_qemu_system_shutdown_request(reason);
1824 replay_shutdown_request(reason);
1825 shutdown_requested = reason;
1826 qemu_notify_event();
1829 static void qemu_system_powerdown(void)
1831 qapi_event_send_powerdown(&error_abort);
1832 notifier_list_notify(&powerdown_notifiers, NULL);
1835 void qemu_system_powerdown_request(void)
1837 trace_qemu_system_powerdown_request();
1838 powerdown_requested = 1;
1839 qemu_notify_event();
1842 void qemu_register_powerdown_notifier(Notifier *notifier)
1844 notifier_list_add(&powerdown_notifiers, notifier);
1847 void qemu_system_debug_request(void)
1849 debug_requested = 1;
1850 qemu_notify_event();
1853 static bool main_loop_should_exit(void)
1855 RunState r;
1856 ShutdownCause request;
1858 if (qemu_debug_requested()) {
1859 vm_stop(RUN_STATE_DEBUG);
1861 if (qemu_suspend_requested()) {
1862 qemu_system_suspend();
1864 request = qemu_shutdown_requested();
1865 if (request) {
1866 qemu_kill_report();
1867 qapi_event_send_shutdown(shutdown_caused_by_guest(request),
1868 &error_abort);
1869 if (no_shutdown) {
1870 vm_stop(RUN_STATE_SHUTDOWN);
1871 } else {
1872 return true;
1875 request = qemu_reset_requested();
1876 if (request) {
1877 pause_all_vcpus();
1878 qemu_system_reset(request);
1879 resume_all_vcpus();
1880 if (!runstate_check(RUN_STATE_RUNNING) &&
1881 !runstate_check(RUN_STATE_INMIGRATE)) {
1882 runstate_set(RUN_STATE_PRELAUNCH);
1885 if (qemu_wakeup_requested()) {
1886 pause_all_vcpus();
1887 qemu_system_reset(SHUTDOWN_CAUSE_NONE);
1888 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
1889 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
1890 resume_all_vcpus();
1891 qapi_event_send_wakeup(&error_abort);
1893 if (qemu_powerdown_requested()) {
1894 qemu_system_powerdown();
1896 if (qemu_vmstop_requested(&r)) {
1897 vm_stop(r);
1899 return false;
1902 static void main_loop(void)
1904 #ifdef CONFIG_PROFILER
1905 int64_t ti;
1906 #endif
1907 do {
1908 #ifdef CONFIG_PROFILER
1909 ti = profile_getclock();
1910 #endif
1911 main_loop_wait(false);
1912 #ifdef CONFIG_PROFILER
1913 dev_time += profile_getclock() - ti;
1914 #endif
1915 } while (!main_loop_should_exit());
1918 static void version(void)
1920 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION "\n"
1921 QEMU_COPYRIGHT "\n");
1924 static void help(int exitcode)
1926 version();
1927 printf("usage: %s [options] [disk_image]\n\n"
1928 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1929 error_get_progname());
1931 #define QEMU_OPTIONS_GENERATE_HELP
1932 #include "qemu-options-wrapper.h"
1934 printf("\nDuring emulation, the following keys are useful:\n"
1935 "ctrl-alt-f toggle full screen\n"
1936 "ctrl-alt-n switch to virtual console 'n'\n"
1937 "ctrl-alt toggle mouse and keyboard grab\n"
1938 "\n"
1939 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1941 exit(exitcode);
1944 #define HAS_ARG 0x0001
1946 typedef struct QEMUOption {
1947 const char *name;
1948 int flags;
1949 int index;
1950 uint32_t arch_mask;
1951 } QEMUOption;
1953 static const QEMUOption qemu_options[] = {
1954 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1955 #define QEMU_OPTIONS_GENERATE_OPTIONS
1956 #include "qemu-options-wrapper.h"
1957 { NULL },
1960 typedef struct VGAInterfaceInfo {
1961 const char *opt_name; /* option name */
1962 const char *name; /* human-readable name */
1963 /* Class names indicating that support is available.
1964 * If no class is specified, the interface is always available */
1965 const char *class_names[2];
1966 } VGAInterfaceInfo;
1968 static VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
1969 [VGA_NONE] = {
1970 .opt_name = "none",
1972 [VGA_STD] = {
1973 .opt_name = "std",
1974 .name = "standard VGA",
1975 .class_names = { "VGA", "isa-vga" },
1977 [VGA_CIRRUS] = {
1978 .opt_name = "cirrus",
1979 .name = "Cirrus VGA",
1980 .class_names = { "cirrus-vga", "isa-cirrus-vga" },
1982 [VGA_VMWARE] = {
1983 .opt_name = "vmware",
1984 .name = "VMWare SVGA",
1985 .class_names = { "vmware-svga" },
1987 [VGA_VIRTIO] = {
1988 .opt_name = "virtio",
1989 .name = "Virtio VGA",
1990 .class_names = { "virtio-vga" },
1992 [VGA_QXL] = {
1993 .opt_name = "qxl",
1994 .name = "QXL VGA",
1995 .class_names = { "qxl-vga" },
1997 [VGA_TCX] = {
1998 .opt_name = "tcx",
1999 .name = "TCX framebuffer",
2000 .class_names = { "SUNW,tcx" },
2002 [VGA_CG3] = {
2003 .opt_name = "cg3",
2004 .name = "CG3 framebuffer",
2005 .class_names = { "cgthree" },
2007 [VGA_XENFB] = {
2008 .opt_name = "xenfb",
2012 static bool vga_interface_available(VGAInterfaceType t)
2014 VGAInterfaceInfo *ti = &vga_interfaces[t];
2016 assert(t < VGA_TYPE_MAX);
2017 return !ti->class_names[0] ||
2018 object_class_by_name(ti->class_names[0]) ||
2019 object_class_by_name(ti->class_names[1]);
2022 static void select_vgahw(const char *p)
2024 const char *opts;
2025 int t;
2027 assert(vga_interface_type == VGA_NONE);
2028 for (t = 0; t < VGA_TYPE_MAX; t++) {
2029 VGAInterfaceInfo *ti = &vga_interfaces[t];
2030 if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
2031 if (!vga_interface_available(t)) {
2032 error_report("%s not available", ti->name);
2033 exit(1);
2035 vga_interface_type = t;
2036 break;
2039 if (t == VGA_TYPE_MAX) {
2040 invalid_vga:
2041 error_report("unknown vga type: %s", p);
2042 exit(1);
2044 while (*opts) {
2045 const char *nextopt;
2047 if (strstart(opts, ",retrace=", &nextopt)) {
2048 opts = nextopt;
2049 if (strstart(opts, "dumb", &nextopt))
2050 vga_retrace_method = VGA_RETRACE_DUMB;
2051 else if (strstart(opts, "precise", &nextopt))
2052 vga_retrace_method = VGA_RETRACE_PRECISE;
2053 else goto invalid_vga;
2054 } else goto invalid_vga;
2055 opts = nextopt;
2059 typedef enum DisplayType {
2060 DT_DEFAULT,
2061 DT_CURSES,
2062 DT_SDL,
2063 DT_COCOA,
2064 DT_GTK,
2065 DT_EGL,
2066 DT_NONE,
2067 } DisplayType;
2069 static DisplayType select_display(const char *p)
2071 const char *opts;
2072 DisplayType display = DT_DEFAULT;
2074 if (strstart(p, "sdl", &opts)) {
2075 #ifdef CONFIG_SDL
2076 display = DT_SDL;
2077 while (*opts) {
2078 const char *nextopt;
2080 if (strstart(opts, ",frame=", &nextopt)) {
2081 opts = nextopt;
2082 if (strstart(opts, "on", &nextopt)) {
2083 no_frame = 0;
2084 } else if (strstart(opts, "off", &nextopt)) {
2085 no_frame = 1;
2086 } else {
2087 goto invalid_sdl_args;
2089 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
2090 opts = nextopt;
2091 if (strstart(opts, "on", &nextopt)) {
2092 alt_grab = 1;
2093 } else if (strstart(opts, "off", &nextopt)) {
2094 alt_grab = 0;
2095 } else {
2096 goto invalid_sdl_args;
2098 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2099 opts = nextopt;
2100 if (strstart(opts, "on", &nextopt)) {
2101 ctrl_grab = 1;
2102 } else if (strstart(opts, "off", &nextopt)) {
2103 ctrl_grab = 0;
2104 } else {
2105 goto invalid_sdl_args;
2107 } else if (strstart(opts, ",window_close=", &nextopt)) {
2108 opts = nextopt;
2109 if (strstart(opts, "on", &nextopt)) {
2110 no_quit = 0;
2111 } else if (strstart(opts, "off", &nextopt)) {
2112 no_quit = 1;
2113 } else {
2114 goto invalid_sdl_args;
2116 } else if (strstart(opts, ",gl=", &nextopt)) {
2117 opts = nextopt;
2118 if (strstart(opts, "on", &nextopt)) {
2119 request_opengl = 1;
2120 } else if (strstart(opts, "off", &nextopt)) {
2121 request_opengl = 0;
2122 } else {
2123 goto invalid_sdl_args;
2125 } else {
2126 invalid_sdl_args:
2127 error_report("invalid SDL option string");
2128 exit(1);
2130 opts = nextopt;
2132 #else
2133 error_report("SDL support is disabled");
2134 exit(1);
2135 #endif
2136 } else if (strstart(p, "vnc", &opts)) {
2137 if (*opts == '=') {
2138 vnc_parse(opts + 1, &error_fatal);
2139 } else {
2140 error_report("VNC requires a display argument vnc=<display>");
2141 exit(1);
2143 } else if (strstart(p, "egl-headless", &opts)) {
2144 #ifdef CONFIG_OPENGL_DMABUF
2145 request_opengl = 1;
2146 display_opengl = 1;
2147 display = DT_EGL;
2148 #else
2149 fprintf(stderr, "egl support is disabled\n");
2150 exit(1);
2151 #endif
2152 } else if (strstart(p, "curses", &opts)) {
2153 #ifdef CONFIG_CURSES
2154 display = DT_CURSES;
2155 #else
2156 error_report("curses support is disabled");
2157 exit(1);
2158 #endif
2159 } else if (strstart(p, "gtk", &opts)) {
2160 #ifdef CONFIG_GTK
2161 display = DT_GTK;
2162 while (*opts) {
2163 const char *nextopt;
2165 if (strstart(opts, ",grab_on_hover=", &nextopt)) {
2166 opts = nextopt;
2167 if (strstart(opts, "on", &nextopt)) {
2168 grab_on_hover = true;
2169 } else if (strstart(opts, "off", &nextopt)) {
2170 grab_on_hover = false;
2171 } else {
2172 goto invalid_gtk_args;
2174 } else if (strstart(opts, ",gl=", &nextopt)) {
2175 opts = nextopt;
2176 if (strstart(opts, "on", &nextopt)) {
2177 request_opengl = 1;
2178 } else if (strstart(opts, "off", &nextopt)) {
2179 request_opengl = 0;
2180 } else {
2181 goto invalid_gtk_args;
2183 } else {
2184 invalid_gtk_args:
2185 error_report("invalid GTK option string");
2186 exit(1);
2188 opts = nextopt;
2190 #else
2191 error_report("GTK support is disabled");
2192 exit(1);
2193 #endif
2194 } else if (strstart(p, "none", &opts)) {
2195 display = DT_NONE;
2196 } else {
2197 error_report("unknown display type");
2198 exit(1);
2201 return display;
2204 static int balloon_parse(const char *arg)
2206 QemuOpts *opts;
2208 if (strcmp(arg, "none") == 0) {
2209 return 0;
2212 if (!strncmp(arg, "virtio", 6)) {
2213 if (arg[6] == ',') {
2214 /* have params -> parse them */
2215 opts = qemu_opts_parse_noisily(qemu_find_opts("device"), arg + 7,
2216 false);
2217 if (!opts)
2218 return -1;
2219 } else {
2220 /* create empty opts */
2221 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2222 &error_abort);
2224 qemu_opt_set(opts, "driver", "virtio-balloon", &error_abort);
2225 return 0;
2228 return -1;
2231 char *qemu_find_file(int type, const char *name)
2233 int i;
2234 const char *subdir;
2235 char *buf;
2237 /* Try the name as a straight path first */
2238 if (access(name, R_OK) == 0) {
2239 trace_load_file(name, name);
2240 return g_strdup(name);
2243 switch (type) {
2244 case QEMU_FILE_TYPE_BIOS:
2245 subdir = "";
2246 break;
2247 case QEMU_FILE_TYPE_KEYMAP:
2248 subdir = "keymaps/";
2249 break;
2250 default:
2251 abort();
2254 for (i = 0; i < data_dir_idx; i++) {
2255 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2256 if (access(buf, R_OK) == 0) {
2257 trace_load_file(name, buf);
2258 return buf;
2260 g_free(buf);
2262 return NULL;
2265 static inline bool nonempty_str(const char *str)
2267 return str && *str;
2270 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
2272 gchar *buf;
2273 size_t size;
2274 const char *name, *file, *str;
2275 FWCfgState *fw_cfg = (FWCfgState *) opaque;
2277 if (fw_cfg == NULL) {
2278 error_report("fw_cfg device not available");
2279 return -1;
2281 name = qemu_opt_get(opts, "name");
2282 file = qemu_opt_get(opts, "file");
2283 str = qemu_opt_get(opts, "string");
2285 /* we need name and either a file or the content string */
2286 if (!(nonempty_str(name) && (nonempty_str(file) || nonempty_str(str)))) {
2287 error_report("invalid argument(s)");
2288 return -1;
2290 if (nonempty_str(file) && nonempty_str(str)) {
2291 error_report("file and string are mutually exclusive");
2292 return -1;
2294 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
2295 error_report("name too long (max. %d char)", FW_CFG_MAX_FILE_PATH - 1);
2296 return -1;
2298 if (strncmp(name, "opt/", 4) != 0) {
2299 error_report("warning: externally provided fw_cfg item names "
2300 "should be prefixed with \"opt/\"");
2302 if (nonempty_str(str)) {
2303 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
2304 buf = g_memdup(str, size);
2305 } else {
2306 if (!g_file_get_contents(file, &buf, &size, NULL)) {
2307 error_report("can't load %s", file);
2308 return -1;
2311 /* For legacy, keep user files in a specific global order. */
2312 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
2313 fw_cfg_add_file(fw_cfg, name, buf, size);
2314 fw_cfg_reset_order_override(fw_cfg);
2315 return 0;
2318 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
2320 return qdev_device_help(opts);
2323 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
2325 Error *err = NULL;
2326 DeviceState *dev;
2328 dev = qdev_device_add(opts, &err);
2329 if (!dev) {
2330 error_report_err(err);
2331 return -1;
2333 object_unref(OBJECT(dev));
2334 return 0;
2337 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2339 Error *local_err = NULL;
2341 qemu_chr_new_from_opts(opts, &local_err);
2342 if (local_err) {
2343 error_report_err(local_err);
2344 return -1;
2346 return 0;
2349 #ifdef CONFIG_VIRTFS
2350 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2352 return qemu_fsdev_add(opts);
2354 #endif
2356 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
2358 Chardev *chr;
2359 const char *chardev;
2360 const char *mode;
2361 int flags;
2363 mode = qemu_opt_get(opts, "mode");
2364 if (mode == NULL) {
2365 mode = "readline";
2367 if (strcmp(mode, "readline") == 0) {
2368 flags = MONITOR_USE_READLINE;
2369 } else if (strcmp(mode, "control") == 0) {
2370 flags = MONITOR_USE_CONTROL;
2371 } else {
2372 error_report("unknown monitor mode \"%s\"", mode);
2373 exit(1);
2376 if (qemu_opt_get_bool(opts, "pretty", 0))
2377 flags |= MONITOR_USE_PRETTY;
2379 if (qemu_opt_get_bool(opts, "default", 0)) {
2380 error_report("option 'default' does nothing and is deprecated");
2383 chardev = qemu_opt_get(opts, "chardev");
2384 chr = qemu_chr_find(chardev);
2385 if (chr == NULL) {
2386 error_report("chardev \"%s\" not found", chardev);
2387 exit(1);
2390 monitor_init(chr, flags);
2391 return 0;
2394 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
2396 static int monitor_device_index = 0;
2397 QemuOpts *opts;
2398 const char *p;
2399 char label[32];
2401 if (strstart(optarg, "chardev:", &p)) {
2402 snprintf(label, sizeof(label), "%s", p);
2403 } else {
2404 snprintf(label, sizeof(label), "compat_monitor%d",
2405 monitor_device_index);
2406 opts = qemu_chr_parse_compat(label, optarg);
2407 if (!opts) {
2408 error_report("parse error: %s", optarg);
2409 exit(1);
2413 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
2414 qemu_opt_set(opts, "mode", mode, &error_abort);
2415 qemu_opt_set(opts, "chardev", label, &error_abort);
2416 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
2417 monitor_device_index++;
2420 struct device_config {
2421 enum {
2422 DEV_USB, /* -usbdevice */
2423 DEV_BT, /* -bt */
2424 DEV_SERIAL, /* -serial */
2425 DEV_PARALLEL, /* -parallel */
2426 DEV_VIRTCON, /* -virtioconsole */
2427 DEV_DEBUGCON, /* -debugcon */
2428 DEV_GDB, /* -gdb, -s */
2429 DEV_SCLP, /* s390 sclp */
2430 } type;
2431 const char *cmdline;
2432 Location loc;
2433 QTAILQ_ENTRY(device_config) next;
2436 static QTAILQ_HEAD(, device_config) device_configs =
2437 QTAILQ_HEAD_INITIALIZER(device_configs);
2439 static void add_device_config(int type, const char *cmdline)
2441 struct device_config *conf;
2443 conf = g_malloc0(sizeof(*conf));
2444 conf->type = type;
2445 conf->cmdline = cmdline;
2446 loc_save(&conf->loc);
2447 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2450 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2452 struct device_config *conf;
2453 int rc;
2455 QTAILQ_FOREACH(conf, &device_configs, next) {
2456 if (conf->type != type)
2457 continue;
2458 loc_push_restore(&conf->loc);
2459 rc = func(conf->cmdline);
2460 loc_pop(&conf->loc);
2461 if (rc) {
2462 return rc;
2465 return 0;
2468 static int serial_parse(const char *devname)
2470 static int index = 0;
2471 char label[32];
2473 if (strcmp(devname, "none") == 0)
2474 return 0;
2475 if (index == MAX_SERIAL_PORTS) {
2476 error_report("too many serial ports");
2477 exit(1);
2479 snprintf(label, sizeof(label), "serial%d", index);
2480 serial_hds[index] = qemu_chr_new(label, devname);
2481 if (!serial_hds[index]) {
2482 error_report("could not connect serial device"
2483 " to character backend '%s'", devname);
2484 return -1;
2486 index++;
2487 return 0;
2490 static int parallel_parse(const char *devname)
2492 static int index = 0;
2493 char label[32];
2495 if (strcmp(devname, "none") == 0)
2496 return 0;
2497 if (index == MAX_PARALLEL_PORTS) {
2498 error_report("too many parallel ports");
2499 exit(1);
2501 snprintf(label, sizeof(label), "parallel%d", index);
2502 parallel_hds[index] = qemu_chr_new(label, devname);
2503 if (!parallel_hds[index]) {
2504 error_report("could not connect parallel device"
2505 " to character backend '%s'", devname);
2506 return -1;
2508 index++;
2509 return 0;
2512 static int virtcon_parse(const char *devname)
2514 QemuOptsList *device = qemu_find_opts("device");
2515 static int index = 0;
2516 char label[32];
2517 QemuOpts *bus_opts, *dev_opts;
2519 if (strcmp(devname, "none") == 0)
2520 return 0;
2521 if (index == MAX_VIRTIO_CONSOLES) {
2522 error_report("too many virtio consoles");
2523 exit(1);
2526 bus_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2527 qemu_opt_set(bus_opts, "driver", "virtio-serial", &error_abort);
2529 dev_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2530 qemu_opt_set(dev_opts, "driver", "virtconsole", &error_abort);
2532 snprintf(label, sizeof(label), "virtcon%d", index);
2533 virtcon_hds[index] = qemu_chr_new(label, devname);
2534 if (!virtcon_hds[index]) {
2535 error_report("could not connect virtio console"
2536 " to character backend '%s'", devname);
2537 return -1;
2539 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2541 index++;
2542 return 0;
2545 static int sclp_parse(const char *devname)
2547 QemuOptsList *device = qemu_find_opts("device");
2548 static int index = 0;
2549 char label[32];
2550 QemuOpts *dev_opts;
2552 if (strcmp(devname, "none") == 0) {
2553 return 0;
2555 if (index == MAX_SCLP_CONSOLES) {
2556 error_report("too many sclp consoles");
2557 exit(1);
2560 assert(arch_type == QEMU_ARCH_S390X);
2562 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2563 qemu_opt_set(dev_opts, "driver", "sclpconsole", &error_abort);
2565 snprintf(label, sizeof(label), "sclpcon%d", index);
2566 sclp_hds[index] = qemu_chr_new(label, devname);
2567 if (!sclp_hds[index]) {
2568 error_report("could not connect sclp console"
2569 " to character backend '%s'", devname);
2570 return -1;
2572 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2574 index++;
2575 return 0;
2578 static int debugcon_parse(const char *devname)
2580 QemuOpts *opts;
2582 if (!qemu_chr_new("debugcon", devname)) {
2583 exit(1);
2585 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2586 if (!opts) {
2587 error_report("already have a debugcon device");
2588 exit(1);
2590 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
2591 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
2592 return 0;
2595 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
2597 const MachineClass *mc1 = a, *mc2 = b;
2598 int res;
2600 if (mc1->family == NULL) {
2601 if (mc2->family == NULL) {
2602 /* Compare standalone machine types against each other; they sort
2603 * in increasing order.
2605 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
2606 object_class_get_name(OBJECT_CLASS(mc2)));
2609 /* Standalone machine types sort after families. */
2610 return 1;
2613 if (mc2->family == NULL) {
2614 /* Families sort before standalone machine types. */
2615 return -1;
2618 /* Families sort between each other alphabetically increasingly. */
2619 res = strcmp(mc1->family, mc2->family);
2620 if (res != 0) {
2621 return res;
2624 /* Within the same family, machine types sort in decreasing order. */
2625 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
2626 object_class_get_name(OBJECT_CLASS(mc1)));
2629 static MachineClass *machine_parse(const char *name)
2631 MachineClass *mc = NULL;
2632 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
2634 if (name) {
2635 mc = find_machine(name);
2637 if (mc) {
2638 g_slist_free(machines);
2639 return mc;
2641 if (name && !is_help_option(name)) {
2642 error_report("unsupported machine type");
2643 error_printf("Use -machine help to list supported machines\n");
2644 } else {
2645 printf("Supported machines are:\n");
2646 machines = g_slist_sort(machines, machine_class_cmp);
2647 for (el = machines; el; el = el->next) {
2648 MachineClass *mc = el->data;
2649 if (mc->alias) {
2650 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
2652 printf("%-20s %s%s\n", mc->name, mc->desc,
2653 mc->is_default ? " (default)" : "");
2657 g_slist_free(machines);
2658 exit(!name || !is_help_option(name));
2661 void qemu_add_exit_notifier(Notifier *notify)
2663 notifier_list_add(&exit_notifiers, notify);
2666 void qemu_remove_exit_notifier(Notifier *notify)
2668 notifier_remove(notify);
2671 static void qemu_run_exit_notifiers(void)
2673 notifier_list_notify(&exit_notifiers, NULL);
2676 static bool machine_init_done;
2678 void qemu_add_machine_init_done_notifier(Notifier *notify)
2680 notifier_list_add(&machine_init_done_notifiers, notify);
2681 if (machine_init_done) {
2682 notify->notify(notify, NULL);
2686 void qemu_remove_machine_init_done_notifier(Notifier *notify)
2688 notifier_remove(notify);
2691 static void qemu_run_machine_init_done_notifiers(void)
2693 notifier_list_notify(&machine_init_done_notifiers, NULL);
2694 machine_init_done = true;
2697 static const QEMUOption *lookup_opt(int argc, char **argv,
2698 const char **poptarg, int *poptind)
2700 const QEMUOption *popt;
2701 int optind = *poptind;
2702 char *r = argv[optind];
2703 const char *optarg;
2705 loc_set_cmdline(argv, optind, 1);
2706 optind++;
2707 /* Treat --foo the same as -foo. */
2708 if (r[1] == '-')
2709 r++;
2710 popt = qemu_options;
2711 for(;;) {
2712 if (!popt->name) {
2713 error_report("invalid option");
2714 exit(1);
2716 if (!strcmp(popt->name, r + 1))
2717 break;
2718 popt++;
2720 if (popt->flags & HAS_ARG) {
2721 if (optind >= argc) {
2722 error_report("requires an argument");
2723 exit(1);
2725 optarg = argv[optind++];
2726 loc_set_cmdline(argv, optind - 2, 2);
2727 } else {
2728 optarg = NULL;
2731 *poptarg = optarg;
2732 *poptind = optind;
2734 return popt;
2737 static MachineClass *select_machine(void)
2739 MachineClass *machine_class = find_default_machine();
2740 const char *optarg;
2741 QemuOpts *opts;
2742 Location loc;
2744 loc_push_none(&loc);
2746 opts = qemu_get_machine_opts();
2747 qemu_opts_loc_restore(opts);
2749 optarg = qemu_opt_get(opts, "type");
2750 if (optarg) {
2751 machine_class = machine_parse(optarg);
2754 if (!machine_class) {
2755 error_report("No machine specified, and there is no default");
2756 error_printf("Use -machine help to list supported machines\n");
2757 exit(1);
2760 loc_pop(&loc);
2761 return machine_class;
2764 static int machine_set_property(void *opaque,
2765 const char *name, const char *value,
2766 Error **errp)
2768 Object *obj = OBJECT(opaque);
2769 Error *local_err = NULL;
2770 char *p, *qom_name;
2772 if (strcmp(name, "type") == 0) {
2773 return 0;
2776 qom_name = g_strdup(name);
2777 for (p = qom_name; *p; p++) {
2778 if (*p == '_') {
2779 *p = '-';
2783 object_property_parse(obj, value, qom_name, &local_err);
2784 g_free(qom_name);
2786 if (local_err) {
2787 error_report_err(local_err);
2788 return -1;
2791 return 0;
2796 * Initial object creation happens before all other
2797 * QEMU data types are created. The majority of objects
2798 * can be created at this point. The rng-egd object
2799 * cannot be created here, as it depends on the chardev
2800 * already existing.
2802 static bool object_create_initial(const char *type)
2804 if (g_str_equal(type, "rng-egd")) {
2805 return false;
2809 * return false for concrete netfilters since
2810 * they depend on netdevs already existing
2812 if (g_str_equal(type, "filter-buffer") ||
2813 g_str_equal(type, "filter-dump") ||
2814 g_str_equal(type, "filter-mirror") ||
2815 g_str_equal(type, "filter-redirector") ||
2816 g_str_equal(type, "colo-compare") ||
2817 g_str_equal(type, "filter-rewriter") ||
2818 g_str_equal(type, "filter-replay")) {
2819 return false;
2822 /* Memory allocation by backends needs to be done
2823 * after configure_accelerator() (due to the tcg_enabled()
2824 * checks at memory_region_init_*()).
2826 * Also, allocation of large amounts of memory may delay
2827 * chardev initialization for too long, and trigger timeouts
2828 * on software that waits for a monitor socket to be created
2829 * (e.g. libvirt).
2831 if (g_str_has_prefix(type, "memory-backend-")) {
2832 return false;
2835 return true;
2840 * The remainder of object creation happens after the
2841 * creation of chardev, fsdev, net clients and device data types.
2843 static bool object_create_delayed(const char *type)
2845 return !object_create_initial(type);
2849 static void set_memory_options(uint64_t *ram_slots, ram_addr_t *maxram_size,
2850 MachineClass *mc)
2852 uint64_t sz;
2853 const char *mem_str;
2854 const char *maxmem_str, *slots_str;
2855 const ram_addr_t default_ram_size = mc->default_ram_size;
2856 QemuOpts *opts = qemu_find_opts_singleton("memory");
2857 Location loc;
2859 loc_push_none(&loc);
2860 qemu_opts_loc_restore(opts);
2862 sz = 0;
2863 mem_str = qemu_opt_get(opts, "size");
2864 if (mem_str) {
2865 if (!*mem_str) {
2866 error_report("missing 'size' option value");
2867 exit(EXIT_FAILURE);
2870 sz = qemu_opt_get_size(opts, "size", ram_size);
2872 /* Fix up legacy suffix-less format */
2873 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2874 uint64_t overflow_check = sz;
2876 sz <<= 20;
2877 if ((sz >> 20) != overflow_check) {
2878 error_report("too large 'size' option value");
2879 exit(EXIT_FAILURE);
2884 /* backward compatibility behaviour for case "-m 0" */
2885 if (sz == 0) {
2886 sz = default_ram_size;
2889 sz = QEMU_ALIGN_UP(sz, 8192);
2890 ram_size = sz;
2891 if (ram_size != sz) {
2892 error_report("ram size too large");
2893 exit(EXIT_FAILURE);
2896 /* store value for the future use */
2897 qemu_opt_set_number(opts, "size", ram_size, &error_abort);
2898 *maxram_size = ram_size;
2900 maxmem_str = qemu_opt_get(opts, "maxmem");
2901 slots_str = qemu_opt_get(opts, "slots");
2902 if (maxmem_str && slots_str) {
2903 uint64_t slots;
2905 sz = qemu_opt_get_size(opts, "maxmem", 0);
2906 slots = qemu_opt_get_number(opts, "slots", 0);
2907 if (sz < ram_size) {
2908 error_report("invalid value of -m option maxmem: "
2909 "maximum memory size (0x%" PRIx64 ") must be at least "
2910 "the initial memory size (0x" RAM_ADDR_FMT ")",
2911 sz, ram_size);
2912 exit(EXIT_FAILURE);
2913 } else if (sz > ram_size) {
2914 if (!slots) {
2915 error_report("invalid value of -m option: maxmem was "
2916 "specified, but no hotplug slots were specified");
2917 exit(EXIT_FAILURE);
2919 } else if (slots) {
2920 error_report("invalid value of -m option maxmem: "
2921 "memory slots were specified but maximum memory size "
2922 "(0x%" PRIx64 ") is equal to the initial memory size "
2923 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
2924 exit(EXIT_FAILURE);
2927 *maxram_size = sz;
2928 *ram_slots = slots;
2929 } else if ((!maxmem_str && slots_str) ||
2930 (maxmem_str && !slots_str)) {
2931 error_report("invalid -m option value: missing "
2932 "'%s' option", slots_str ? "maxmem" : "slots");
2933 exit(EXIT_FAILURE);
2936 loc_pop(&loc);
2939 static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
2941 GlobalProperty *g;
2943 g = g_malloc0(sizeof(*g));
2944 g->driver = qemu_opt_get(opts, "driver");
2945 g->property = qemu_opt_get(opts, "property");
2946 g->value = qemu_opt_get(opts, "value");
2947 g->user_provided = true;
2948 g->errp = &error_fatal;
2949 qdev_prop_register_global(g);
2950 return 0;
2953 static int qemu_read_default_config_file(void)
2955 int ret;
2957 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
2958 if (ret < 0 && ret != -ENOENT) {
2959 return ret;
2962 return 0;
2965 int main(int argc, char **argv, char **envp)
2967 int i;
2968 int snapshot, linux_boot;
2969 const char *initrd_filename;
2970 const char *kernel_filename, *kernel_cmdline;
2971 const char *boot_order = NULL;
2972 const char *boot_once = NULL;
2973 DisplayState *ds;
2974 int cyls, heads, secs, translation;
2975 QemuOpts *opts, *machine_opts;
2976 QemuOpts *hda_opts = NULL, *icount_opts = NULL, *accel_opts = NULL;
2977 QemuOptsList *olist;
2978 int optind;
2979 const char *optarg;
2980 const char *loadvm = NULL;
2981 MachineClass *machine_class;
2982 const char *cpu_model;
2983 const char *vga_model = NULL;
2984 const char *qtest_chrdev = NULL;
2985 const char *qtest_log = NULL;
2986 const char *pid_file = NULL;
2987 const char *incoming = NULL;
2988 bool defconfig = true;
2989 bool userconfig = true;
2990 bool nographic = false;
2991 DisplayType display_type = DT_DEFAULT;
2992 int display_remote = 0;
2993 const char *log_mask = NULL;
2994 const char *log_file = NULL;
2995 char *trace_file = NULL;
2996 ram_addr_t maxram_size;
2997 uint64_t ram_slots = 0;
2998 FILE *vmstate_dump_file = NULL;
2999 Error *main_loop_err = NULL;
3000 Error *err = NULL;
3001 bool list_data_dirs = false;
3002 typedef struct BlockdevOptions_queue {
3003 BlockdevOptions *bdo;
3004 Location loc;
3005 QSIMPLEQ_ENTRY(BlockdevOptions_queue) entry;
3006 } BlockdevOptions_queue;
3007 QSIMPLEQ_HEAD(, BlockdevOptions_queue) bdo_queue
3008 = QSIMPLEQ_HEAD_INITIALIZER(bdo_queue);
3010 module_call_init(MODULE_INIT_TRACE);
3012 qemu_init_cpu_list();
3013 qemu_init_cpu_loop();
3014 qemu_mutex_lock_iothread();
3016 atexit(qemu_run_exit_notifiers);
3017 error_set_progname(argv[0]);
3018 qemu_init_exec_dir(argv[0]);
3020 module_call_init(MODULE_INIT_QOM);
3021 monitor_init_qmp_commands();
3023 qemu_add_opts(&qemu_drive_opts);
3024 qemu_add_drive_opts(&qemu_legacy_drive_opts);
3025 qemu_add_drive_opts(&qemu_common_drive_opts);
3026 qemu_add_drive_opts(&qemu_drive_opts);
3027 qemu_add_drive_opts(&bdrv_runtime_opts);
3028 qemu_add_opts(&qemu_chardev_opts);
3029 qemu_add_opts(&qemu_device_opts);
3030 qemu_add_opts(&qemu_netdev_opts);
3031 qemu_add_opts(&qemu_net_opts);
3032 qemu_add_opts(&qemu_rtc_opts);
3033 qemu_add_opts(&qemu_global_opts);
3034 qemu_add_opts(&qemu_mon_opts);
3035 qemu_add_opts(&qemu_trace_opts);
3036 qemu_add_opts(&qemu_option_rom_opts);
3037 qemu_add_opts(&qemu_machine_opts);
3038 qemu_add_opts(&qemu_accel_opts);
3039 qemu_add_opts(&qemu_mem_opts);
3040 qemu_add_opts(&qemu_smp_opts);
3041 qemu_add_opts(&qemu_boot_opts);
3042 qemu_add_opts(&qemu_sandbox_opts);
3043 qemu_add_opts(&qemu_add_fd_opts);
3044 qemu_add_opts(&qemu_object_opts);
3045 qemu_add_opts(&qemu_tpmdev_opts);
3046 qemu_add_opts(&qemu_realtime_opts);
3047 qemu_add_opts(&qemu_msg_opts);
3048 qemu_add_opts(&qemu_name_opts);
3049 qemu_add_opts(&qemu_numa_opts);
3050 qemu_add_opts(&qemu_icount_opts);
3051 qemu_add_opts(&qemu_semihosting_config_opts);
3052 qemu_add_opts(&qemu_fw_cfg_opts);
3053 module_call_init(MODULE_INIT_OPTS);
3055 runstate_init();
3057 if (qcrypto_init(&err) < 0) {
3058 error_reportf_err(err, "cannot initialize crypto: ");
3059 exit(1);
3061 rtc_clock = QEMU_CLOCK_HOST;
3063 QLIST_INIT (&vm_change_state_head);
3064 os_setup_early_signal_handling();
3066 cpu_model = NULL;
3067 snapshot = 0;
3068 cyls = heads = secs = 0;
3069 translation = BIOS_ATA_TRANSLATION_AUTO;
3071 nb_nics = 0;
3073 bdrv_init_with_whitelist();
3075 autostart = 1;
3077 /* first pass of option parsing */
3078 optind = 1;
3079 while (optind < argc) {
3080 if (argv[optind][0] != '-') {
3081 /* disk image */
3082 optind++;
3083 } else {
3084 const QEMUOption *popt;
3086 popt = lookup_opt(argc, argv, &optarg, &optind);
3087 switch (popt->index) {
3088 case QEMU_OPTION_nodefconfig:
3089 defconfig = false;
3090 break;
3091 case QEMU_OPTION_nouserconfig:
3092 userconfig = false;
3093 break;
3098 if (defconfig && userconfig) {
3099 if (qemu_read_default_config_file() < 0) {
3100 exit(1);
3104 /* second pass of option parsing */
3105 optind = 1;
3106 for(;;) {
3107 if (optind >= argc)
3108 break;
3109 if (argv[optind][0] != '-') {
3110 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
3111 } else {
3112 const QEMUOption *popt;
3114 popt = lookup_opt(argc, argv, &optarg, &optind);
3115 if (!(popt->arch_mask & arch_type)) {
3116 error_report("Option not supported for this target");
3117 exit(1);
3119 switch(popt->index) {
3120 case QEMU_OPTION_no_kvm_irqchip: {
3121 olist = qemu_find_opts("machine");
3122 qemu_opts_parse_noisily(olist, "kernel_irqchip=off", false);
3123 break;
3125 case QEMU_OPTION_cpu:
3126 /* hw initialization will check this */
3127 cpu_model = optarg;
3128 break;
3129 case QEMU_OPTION_hda:
3131 char buf[256];
3132 if (cyls == 0)
3133 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
3134 else
3135 snprintf(buf, sizeof(buf),
3136 "%s,cyls=%d,heads=%d,secs=%d%s",
3137 HD_OPTS , cyls, heads, secs,
3138 translation == BIOS_ATA_TRANSLATION_LBA ?
3139 ",trans=lba" :
3140 translation == BIOS_ATA_TRANSLATION_NONE ?
3141 ",trans=none" : "");
3142 drive_add(IF_DEFAULT, 0, optarg, buf);
3143 break;
3145 case QEMU_OPTION_hdb:
3146 case QEMU_OPTION_hdc:
3147 case QEMU_OPTION_hdd:
3148 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
3149 HD_OPTS);
3150 break;
3151 case QEMU_OPTION_blockdev:
3153 Visitor *v;
3154 BlockdevOptions_queue *bdo;
3156 v = qobject_input_visitor_new_str(optarg, "driver", &err);
3157 if (!v) {
3158 error_report_err(err);
3159 exit(1);
3162 bdo = g_new(BlockdevOptions_queue, 1);
3163 visit_type_BlockdevOptions(v, NULL, &bdo->bdo,
3164 &error_fatal);
3165 visit_free(v);
3166 loc_save(&bdo->loc);
3167 QSIMPLEQ_INSERT_TAIL(&bdo_queue, bdo, entry);
3168 break;
3170 case QEMU_OPTION_drive:
3171 if (drive_def(optarg) == NULL) {
3172 exit(1);
3174 break;
3175 case QEMU_OPTION_set:
3176 if (qemu_set_option(optarg) != 0)
3177 exit(1);
3178 break;
3179 case QEMU_OPTION_global:
3180 if (qemu_global_option(optarg) != 0)
3181 exit(1);
3182 break;
3183 case QEMU_OPTION_mtdblock:
3184 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3185 break;
3186 case QEMU_OPTION_sd:
3187 drive_add(IF_SD, -1, optarg, SD_OPTS);
3188 break;
3189 case QEMU_OPTION_pflash:
3190 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
3191 break;
3192 case QEMU_OPTION_snapshot:
3193 snapshot = 1;
3194 break;
3195 case QEMU_OPTION_hdachs:
3197 const char *p;
3198 p = optarg;
3199 cyls = strtol(p, (char **)&p, 0);
3200 if (cyls < 1 || cyls > 16383)
3201 goto chs_fail;
3202 if (*p != ',')
3203 goto chs_fail;
3204 p++;
3205 heads = strtol(p, (char **)&p, 0);
3206 if (heads < 1 || heads > 16)
3207 goto chs_fail;
3208 if (*p != ',')
3209 goto chs_fail;
3210 p++;
3211 secs = strtol(p, (char **)&p, 0);
3212 if (secs < 1 || secs > 63)
3213 goto chs_fail;
3214 if (*p == ',') {
3215 p++;
3216 if (!strcmp(p, "large")) {
3217 translation = BIOS_ATA_TRANSLATION_LARGE;
3218 } else if (!strcmp(p, "rechs")) {
3219 translation = BIOS_ATA_TRANSLATION_RECHS;
3220 } else if (!strcmp(p, "none")) {
3221 translation = BIOS_ATA_TRANSLATION_NONE;
3222 } else if (!strcmp(p, "lba")) {
3223 translation = BIOS_ATA_TRANSLATION_LBA;
3224 } else if (!strcmp(p, "auto")) {
3225 translation = BIOS_ATA_TRANSLATION_AUTO;
3226 } else {
3227 goto chs_fail;
3229 } else if (*p != '\0') {
3230 chs_fail:
3231 error_report("invalid physical CHS format");
3232 exit(1);
3234 if (hda_opts != NULL) {
3235 qemu_opt_set_number(hda_opts, "cyls", cyls,
3236 &error_abort);
3237 qemu_opt_set_number(hda_opts, "heads", heads,
3238 &error_abort);
3239 qemu_opt_set_number(hda_opts, "secs", secs,
3240 &error_abort);
3241 if (translation == BIOS_ATA_TRANSLATION_LARGE) {
3242 qemu_opt_set(hda_opts, "trans", "large",
3243 &error_abort);
3244 } else if (translation == BIOS_ATA_TRANSLATION_RECHS) {
3245 qemu_opt_set(hda_opts, "trans", "rechs",
3246 &error_abort);
3247 } else if (translation == BIOS_ATA_TRANSLATION_LBA) {
3248 qemu_opt_set(hda_opts, "trans", "lba",
3249 &error_abort);
3250 } else if (translation == BIOS_ATA_TRANSLATION_NONE) {
3251 qemu_opt_set(hda_opts, "trans", "none",
3252 &error_abort);
3256 error_report("'-hdachs' is deprecated, please use '-device"
3257 " ide-hd,cyls=c,heads=h,secs=s,...' instead");
3258 break;
3259 case QEMU_OPTION_numa:
3260 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
3261 optarg, true);
3262 if (!opts) {
3263 exit(1);
3265 break;
3266 case QEMU_OPTION_display:
3267 display_type = select_display(optarg);
3268 break;
3269 case QEMU_OPTION_nographic:
3270 olist = qemu_find_opts("machine");
3271 qemu_opts_parse_noisily(olist, "graphics=off", false);
3272 nographic = true;
3273 display_type = DT_NONE;
3274 break;
3275 case QEMU_OPTION_curses:
3276 #ifdef CONFIG_CURSES
3277 display_type = DT_CURSES;
3278 #else
3279 error_report("curses support is disabled");
3280 exit(1);
3281 #endif
3282 break;
3283 case QEMU_OPTION_portrait:
3284 graphic_rotate = 90;
3285 break;
3286 case QEMU_OPTION_rotate:
3287 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3288 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3289 graphic_rotate != 180 && graphic_rotate != 270) {
3290 error_report("only 90, 180, 270 deg rotation is available");
3291 exit(1);
3293 break;
3294 case QEMU_OPTION_kernel:
3295 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg,
3296 &error_abort);
3297 break;
3298 case QEMU_OPTION_initrd:
3299 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg,
3300 &error_abort);
3301 break;
3302 case QEMU_OPTION_append:
3303 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg,
3304 &error_abort);
3305 break;
3306 case QEMU_OPTION_dtb:
3307 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg,
3308 &error_abort);
3309 break;
3310 case QEMU_OPTION_cdrom:
3311 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3312 break;
3313 case QEMU_OPTION_boot:
3314 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
3315 optarg, true);
3316 if (!opts) {
3317 exit(1);
3319 break;
3320 case QEMU_OPTION_fda:
3321 case QEMU_OPTION_fdb:
3322 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3323 optarg, FD_OPTS);
3324 break;
3325 case QEMU_OPTION_no_fd_bootchk:
3326 fd_bootchk = 0;
3327 break;
3328 case QEMU_OPTION_netdev:
3329 default_net = 0;
3330 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3331 exit(1);
3333 break;
3334 case QEMU_OPTION_net:
3335 default_net = 0;
3336 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3337 exit(1);
3339 break;
3340 #ifdef CONFIG_LIBISCSI
3341 case QEMU_OPTION_iscsi:
3342 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
3343 optarg, false);
3344 if (!opts) {
3345 exit(1);
3347 break;
3348 #endif
3349 #ifdef CONFIG_SLIRP
3350 case QEMU_OPTION_tftp:
3351 error_report("The -tftp option is deprecated. "
3352 "Please use '-netdev user,tftp=...' instead.");
3353 legacy_tftp_prefix = optarg;
3354 break;
3355 case QEMU_OPTION_bootp:
3356 error_report("The -bootp option is deprecated. "
3357 "Please use '-netdev user,bootfile=...' instead.");
3358 legacy_bootp_filename = optarg;
3359 break;
3360 case QEMU_OPTION_redir:
3361 error_report("The -redir option is deprecated. "
3362 "Please use '-netdev user,hostfwd=...' instead.");
3363 if (net_slirp_redir(optarg) < 0)
3364 exit(1);
3365 break;
3366 #endif
3367 case QEMU_OPTION_bt:
3368 add_device_config(DEV_BT, optarg);
3369 break;
3370 case QEMU_OPTION_audio_help:
3371 AUD_help ();
3372 exit (0);
3373 break;
3374 case QEMU_OPTION_soundhw:
3375 select_soundhw (optarg);
3376 break;
3377 case QEMU_OPTION_h:
3378 help(0);
3379 break;
3380 case QEMU_OPTION_version:
3381 version();
3382 exit(0);
3383 break;
3384 case QEMU_OPTION_m:
3385 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
3386 optarg, true);
3387 if (!opts) {
3388 exit(EXIT_FAILURE);
3390 break;
3391 #ifdef CONFIG_TPM
3392 case QEMU_OPTION_tpmdev:
3393 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3394 exit(1);
3396 break;
3397 #endif
3398 case QEMU_OPTION_mempath:
3399 mem_path = optarg;
3400 break;
3401 case QEMU_OPTION_mem_prealloc:
3402 mem_prealloc = 1;
3403 break;
3404 case QEMU_OPTION_d:
3405 log_mask = optarg;
3406 break;
3407 case QEMU_OPTION_D:
3408 log_file = optarg;
3409 break;
3410 case QEMU_OPTION_DFILTER:
3411 qemu_set_dfilter_ranges(optarg, &error_fatal);
3412 break;
3413 case QEMU_OPTION_s:
3414 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3415 break;
3416 case QEMU_OPTION_gdb:
3417 add_device_config(DEV_GDB, optarg);
3418 break;
3419 case QEMU_OPTION_L:
3420 if (is_help_option(optarg)) {
3421 list_data_dirs = true;
3422 } else if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3423 data_dir[data_dir_idx++] = optarg;
3425 break;
3426 case QEMU_OPTION_bios:
3427 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg,
3428 &error_abort);
3429 break;
3430 case QEMU_OPTION_singlestep:
3431 singlestep = 1;
3432 break;
3433 case QEMU_OPTION_S:
3434 autostart = 0;
3435 break;
3436 case QEMU_OPTION_k:
3437 keyboard_layout = optarg;
3438 break;
3439 case QEMU_OPTION_localtime:
3440 rtc_utc = 0;
3441 break;
3442 case QEMU_OPTION_vga:
3443 vga_model = optarg;
3444 default_vga = 0;
3445 break;
3446 case QEMU_OPTION_g:
3448 const char *p;
3449 int w, h, depth;
3450 p = optarg;
3451 w = strtol(p, (char **)&p, 10);
3452 if (w <= 0) {
3453 graphic_error:
3454 error_report("invalid resolution or depth");
3455 exit(1);
3457 if (*p != 'x')
3458 goto graphic_error;
3459 p++;
3460 h = strtol(p, (char **)&p, 10);
3461 if (h <= 0)
3462 goto graphic_error;
3463 if (*p == 'x') {
3464 p++;
3465 depth = strtol(p, (char **)&p, 10);
3466 if (depth != 8 && depth != 15 && depth != 16 &&
3467 depth != 24 && depth != 32)
3468 goto graphic_error;
3469 } else if (*p == '\0') {
3470 depth = graphic_depth;
3471 } else {
3472 goto graphic_error;
3475 graphic_width = w;
3476 graphic_height = h;
3477 graphic_depth = depth;
3479 break;
3480 case QEMU_OPTION_echr:
3482 char *r;
3483 term_escape_char = strtol(optarg, &r, 0);
3484 if (r == optarg)
3485 printf("Bad argument to echr\n");
3486 break;
3488 case QEMU_OPTION_monitor:
3489 default_monitor = 0;
3490 if (strncmp(optarg, "none", 4)) {
3491 monitor_parse(optarg, "readline", false);
3493 break;
3494 case QEMU_OPTION_qmp:
3495 monitor_parse(optarg, "control", false);
3496 default_monitor = 0;
3497 break;
3498 case QEMU_OPTION_qmp_pretty:
3499 monitor_parse(optarg, "control", true);
3500 default_monitor = 0;
3501 break;
3502 case QEMU_OPTION_mon:
3503 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3504 true);
3505 if (!opts) {
3506 exit(1);
3508 default_monitor = 0;
3509 break;
3510 case QEMU_OPTION_chardev:
3511 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3512 optarg, true);
3513 if (!opts) {
3514 exit(1);
3516 break;
3517 case QEMU_OPTION_fsdev:
3518 olist = qemu_find_opts("fsdev");
3519 if (!olist) {
3520 error_report("fsdev support is disabled");
3521 exit(1);
3523 opts = qemu_opts_parse_noisily(olist, optarg, true);
3524 if (!opts) {
3525 exit(1);
3527 break;
3528 case QEMU_OPTION_virtfs: {
3529 QemuOpts *fsdev;
3530 QemuOpts *device;
3531 const char *writeout, *sock_fd, *socket;
3533 olist = qemu_find_opts("virtfs");
3534 if (!olist) {
3535 error_report("virtfs support is disabled");
3536 exit(1);
3538 opts = qemu_opts_parse_noisily(olist, optarg, true);
3539 if (!opts) {
3540 exit(1);
3543 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3544 qemu_opt_get(opts, "mount_tag") == NULL) {
3545 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3546 exit(1);
3548 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3549 qemu_opts_id(opts) ?:
3550 qemu_opt_get(opts, "mount_tag"),
3551 1, NULL);
3552 if (!fsdev) {
3553 error_report("duplicate or invalid fsdev id: %s",
3554 qemu_opt_get(opts, "mount_tag"));
3555 exit(1);
3558 writeout = qemu_opt_get(opts, "writeout");
3559 if (writeout) {
3560 #ifdef CONFIG_SYNC_FILE_RANGE
3561 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3562 #else
3563 error_report("writeout=immediate not supported "
3564 "on this platform");
3565 exit(1);
3566 #endif
3568 qemu_opt_set(fsdev, "fsdriver",
3569 qemu_opt_get(opts, "fsdriver"), &error_abort);
3570 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"),
3571 &error_abort);
3572 qemu_opt_set(fsdev, "security_model",
3573 qemu_opt_get(opts, "security_model"),
3574 &error_abort);
3575 socket = qemu_opt_get(opts, "socket");
3576 if (socket) {
3577 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3579 sock_fd = qemu_opt_get(opts, "sock_fd");
3580 if (sock_fd) {
3581 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3584 qemu_opt_set_bool(fsdev, "readonly",
3585 qemu_opt_get_bool(opts, "readonly", 0),
3586 &error_abort);
3587 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3588 &error_abort);
3589 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3590 qemu_opt_set(device, "fsdev",
3591 qemu_opts_id(fsdev), &error_abort);
3592 qemu_opt_set(device, "mount_tag",
3593 qemu_opt_get(opts, "mount_tag"), &error_abort);
3594 break;
3596 case QEMU_OPTION_virtfs_synth: {
3597 QemuOpts *fsdev;
3598 QemuOpts *device;
3600 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3601 1, NULL);
3602 if (!fsdev) {
3603 error_report("duplicate option: %s", "virtfs_synth");
3604 exit(1);
3606 qemu_opt_set(fsdev, "fsdriver", "synth", &error_abort);
3608 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3609 &error_abort);
3610 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3611 qemu_opt_set(device, "fsdev", "v_synth", &error_abort);
3612 qemu_opt_set(device, "mount_tag", "v_synth", &error_abort);
3613 break;
3615 case QEMU_OPTION_serial:
3616 add_device_config(DEV_SERIAL, optarg);
3617 default_serial = 0;
3618 if (strncmp(optarg, "mon:", 4) == 0) {
3619 default_monitor = 0;
3621 break;
3622 case QEMU_OPTION_watchdog:
3623 if (watchdog) {
3624 error_report("only one watchdog option may be given");
3625 return 1;
3627 watchdog = optarg;
3628 break;
3629 case QEMU_OPTION_watchdog_action:
3630 if (select_watchdog_action(optarg) == -1) {
3631 error_report("unknown -watchdog-action parameter");
3632 exit(1);
3634 break;
3635 case QEMU_OPTION_virtiocon:
3636 add_device_config(DEV_VIRTCON, optarg);
3637 default_virtcon = 0;
3638 if (strncmp(optarg, "mon:", 4) == 0) {
3639 default_monitor = 0;
3641 break;
3642 case QEMU_OPTION_parallel:
3643 add_device_config(DEV_PARALLEL, optarg);
3644 default_parallel = 0;
3645 if (strncmp(optarg, "mon:", 4) == 0) {
3646 default_monitor = 0;
3648 break;
3649 case QEMU_OPTION_debugcon:
3650 add_device_config(DEV_DEBUGCON, optarg);
3651 break;
3652 case QEMU_OPTION_loadvm:
3653 loadvm = optarg;
3654 break;
3655 case QEMU_OPTION_full_screen:
3656 full_screen = 1;
3657 break;
3658 case QEMU_OPTION_no_frame:
3659 no_frame = 1;
3660 break;
3661 case QEMU_OPTION_alt_grab:
3662 alt_grab = 1;
3663 break;
3664 case QEMU_OPTION_ctrl_grab:
3665 ctrl_grab = 1;
3666 break;
3667 case QEMU_OPTION_no_quit:
3668 no_quit = 1;
3669 break;
3670 case QEMU_OPTION_sdl:
3671 #ifdef CONFIG_SDL
3672 display_type = DT_SDL;
3673 break;
3674 #else
3675 error_report("SDL support is disabled");
3676 exit(1);
3677 #endif
3678 case QEMU_OPTION_pidfile:
3679 pid_file = optarg;
3680 break;
3681 case QEMU_OPTION_win2k_hack:
3682 win2k_install_hack = 1;
3683 break;
3684 case QEMU_OPTION_rtc_td_hack: {
3685 static GlobalProperty slew_lost_ticks = {
3686 .driver = "mc146818rtc",
3687 .property = "lost_tick_policy",
3688 .value = "slew",
3691 qdev_prop_register_global(&slew_lost_ticks);
3692 break;
3694 case QEMU_OPTION_acpitable:
3695 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3696 optarg, true);
3697 if (!opts) {
3698 exit(1);
3700 acpi_table_add(opts, &error_fatal);
3701 break;
3702 case QEMU_OPTION_smbios:
3703 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3704 optarg, false);
3705 if (!opts) {
3706 exit(1);
3708 smbios_entry_add(opts, &error_fatal);
3709 break;
3710 case QEMU_OPTION_fwcfg:
3711 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3712 optarg, true);
3713 if (opts == NULL) {
3714 exit(1);
3716 break;
3717 case QEMU_OPTION_enable_kvm:
3718 olist = qemu_find_opts("machine");
3719 qemu_opts_parse_noisily(olist, "accel=kvm", false);
3720 break;
3721 case QEMU_OPTION_enable_hax:
3722 olist = qemu_find_opts("machine");
3723 qemu_opts_parse_noisily(olist, "accel=hax", false);
3724 break;
3725 case QEMU_OPTION_M:
3726 case QEMU_OPTION_machine:
3727 olist = qemu_find_opts("machine");
3728 opts = qemu_opts_parse_noisily(olist, optarg, true);
3729 if (!opts) {
3730 exit(1);
3732 break;
3733 case QEMU_OPTION_no_kvm:
3734 olist = qemu_find_opts("machine");
3735 qemu_opts_parse_noisily(olist, "accel=tcg", false);
3736 break;
3737 case QEMU_OPTION_no_kvm_pit: {
3738 error_report("warning: ignoring deprecated option");
3739 break;
3741 case QEMU_OPTION_no_kvm_pit_reinjection: {
3742 static GlobalProperty kvm_pit_lost_tick_policy = {
3743 .driver = "kvm-pit",
3744 .property = "lost_tick_policy",
3745 .value = "discard",
3748 error_report("warning: deprecated, replaced by "
3749 "-global kvm-pit.lost_tick_policy=discard");
3750 qdev_prop_register_global(&kvm_pit_lost_tick_policy);
3751 break;
3753 case QEMU_OPTION_accel: {
3754 QemuOpts *accel_opts;
3756 accel_opts = qemu_opts_parse_noisily(qemu_find_opts("accel"),
3757 optarg, true);
3758 optarg = qemu_opt_get(accel_opts, "accel");
3759 if (!optarg || is_help_option(optarg)) {
3760 error_printf("Possible accelerators: kvm, xen, hax, tcg\n");
3761 exit(1);
3763 accel_opts = qemu_opts_create(qemu_find_opts("machine"), NULL,
3764 false, &error_abort);
3765 qemu_opt_set(accel_opts, "accel", optarg, &error_abort);
3766 break;
3768 case QEMU_OPTION_usb:
3769 olist = qemu_find_opts("machine");
3770 qemu_opts_parse_noisily(olist, "usb=on", false);
3771 break;
3772 case QEMU_OPTION_usbdevice:
3773 olist = qemu_find_opts("machine");
3774 qemu_opts_parse_noisily(olist, "usb=on", false);
3775 add_device_config(DEV_USB, optarg);
3776 break;
3777 case QEMU_OPTION_device:
3778 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3779 optarg, true)) {
3780 exit(1);
3782 break;
3783 case QEMU_OPTION_smp:
3784 if (!qemu_opts_parse_noisily(qemu_find_opts("smp-opts"),
3785 optarg, true)) {
3786 exit(1);
3788 break;
3789 case QEMU_OPTION_vnc:
3790 vnc_parse(optarg, &error_fatal);
3791 break;
3792 case QEMU_OPTION_no_acpi:
3793 acpi_enabled = 0;
3794 break;
3795 case QEMU_OPTION_no_hpet:
3796 no_hpet = 1;
3797 break;
3798 case QEMU_OPTION_balloon:
3799 if (balloon_parse(optarg) < 0) {
3800 error_report("unknown -balloon argument %s", optarg);
3801 exit(1);
3803 break;
3804 case QEMU_OPTION_no_reboot:
3805 no_reboot = 1;
3806 break;
3807 case QEMU_OPTION_no_shutdown:
3808 no_shutdown = 1;
3809 break;
3810 case QEMU_OPTION_show_cursor:
3811 cursor_hide = 0;
3812 break;
3813 case QEMU_OPTION_uuid:
3814 if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
3815 error_report("failed to parse UUID string: wrong format");
3816 exit(1);
3818 qemu_uuid_set = true;
3819 break;
3820 case QEMU_OPTION_option_rom:
3821 if (nb_option_roms >= MAX_OPTION_ROMS) {
3822 error_report("too many option ROMs");
3823 exit(1);
3825 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3826 optarg, true);
3827 if (!opts) {
3828 exit(1);
3830 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3831 option_rom[nb_option_roms].bootindex =
3832 qemu_opt_get_number(opts, "bootindex", -1);
3833 if (!option_rom[nb_option_roms].name) {
3834 error_report("Option ROM file is not specified");
3835 exit(1);
3837 nb_option_roms++;
3838 break;
3839 case QEMU_OPTION_semihosting:
3840 semihosting.enabled = true;
3841 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3842 break;
3843 case QEMU_OPTION_semihosting_config:
3844 semihosting.enabled = true;
3845 opts = qemu_opts_parse_noisily(qemu_find_opts("semihosting-config"),
3846 optarg, false);
3847 if (opts != NULL) {
3848 semihosting.enabled = qemu_opt_get_bool(opts, "enable",
3849 true);
3850 const char *target = qemu_opt_get(opts, "target");
3851 if (target != NULL) {
3852 if (strcmp("native", target) == 0) {
3853 semihosting.target = SEMIHOSTING_TARGET_NATIVE;
3854 } else if (strcmp("gdb", target) == 0) {
3855 semihosting.target = SEMIHOSTING_TARGET_GDB;
3856 } else if (strcmp("auto", target) == 0) {
3857 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3858 } else {
3859 error_report("unsupported semihosting-config %s",
3860 optarg);
3861 exit(1);
3863 } else {
3864 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3866 /* Set semihosting argument count and vector */
3867 qemu_opt_foreach(opts, add_semihosting_arg,
3868 &semihosting, NULL);
3869 } else {
3870 error_report("unsupported semihosting-config %s", optarg);
3871 exit(1);
3873 break;
3874 case QEMU_OPTION_tdf:
3875 error_report("warning: ignoring deprecated option");
3876 break;
3877 case QEMU_OPTION_name:
3878 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3879 optarg, true);
3880 if (!opts) {
3881 exit(1);
3883 break;
3884 case QEMU_OPTION_prom_env:
3885 if (nb_prom_envs >= MAX_PROM_ENVS) {
3886 error_report("too many prom variables");
3887 exit(1);
3889 prom_envs[nb_prom_envs] = optarg;
3890 nb_prom_envs++;
3891 break;
3892 case QEMU_OPTION_old_param:
3893 old_param = 1;
3894 break;
3895 case QEMU_OPTION_clock:
3896 /* Clock options no longer exist. Keep this option for
3897 * backward compatibility.
3899 break;
3900 case QEMU_OPTION_startdate:
3901 configure_rtc_date_offset(optarg, 1);
3902 break;
3903 case QEMU_OPTION_rtc:
3904 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3905 false);
3906 if (!opts) {
3907 exit(1);
3909 configure_rtc(opts);
3910 break;
3911 case QEMU_OPTION_tb_size:
3912 tcg_tb_size = strtol(optarg, NULL, 0);
3913 if (tcg_tb_size < 0) {
3914 tcg_tb_size = 0;
3916 break;
3917 case QEMU_OPTION_icount:
3918 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3919 optarg, true);
3920 if (!icount_opts) {
3921 exit(1);
3923 break;
3924 case QEMU_OPTION_incoming:
3925 if (!incoming) {
3926 runstate_set(RUN_STATE_INMIGRATE);
3928 incoming = optarg;
3929 break;
3930 case QEMU_OPTION_only_migratable:
3931 only_migratable = 1;
3932 break;
3933 case QEMU_OPTION_nodefaults:
3934 has_defaults = 0;
3935 break;
3936 case QEMU_OPTION_xen_domid:
3937 if (!(xen_available())) {
3938 error_report("Option not supported for this target");
3939 exit(1);
3941 xen_domid = atoi(optarg);
3942 break;
3943 case QEMU_OPTION_xen_create:
3944 if (!(xen_available())) {
3945 error_report("Option not supported for this target");
3946 exit(1);
3948 xen_mode = XEN_CREATE;
3949 break;
3950 case QEMU_OPTION_xen_attach:
3951 if (!(xen_available())) {
3952 error_report("Option not supported for this target");
3953 exit(1);
3955 xen_mode = XEN_ATTACH;
3956 break;
3957 case QEMU_OPTION_xen_domid_restrict:
3958 if (!(xen_available())) {
3959 error_report("Option not supported for this target");
3960 exit(1);
3962 xen_domid_restrict = true;
3963 break;
3964 case QEMU_OPTION_trace:
3965 g_free(trace_file);
3966 trace_file = trace_opt_parse(optarg);
3967 break;
3968 case QEMU_OPTION_readconfig:
3970 int ret = qemu_read_config_file(optarg);
3971 if (ret < 0) {
3972 error_report("read config %s: %s", optarg,
3973 strerror(-ret));
3974 exit(1);
3976 break;
3978 case QEMU_OPTION_spice:
3979 olist = qemu_find_opts("spice");
3980 if (!olist) {
3981 error_report("spice support is disabled");
3982 exit(1);
3984 opts = qemu_opts_parse_noisily(olist, optarg, false);
3985 if (!opts) {
3986 exit(1);
3988 display_remote++;
3989 break;
3990 case QEMU_OPTION_writeconfig:
3992 FILE *fp;
3993 if (strcmp(optarg, "-") == 0) {
3994 fp = stdout;
3995 } else {
3996 fp = fopen(optarg, "w");
3997 if (fp == NULL) {
3998 error_report("open %s: %s", optarg,
3999 strerror(errno));
4000 exit(1);
4003 qemu_config_write(fp);
4004 if (fp != stdout) {
4005 fclose(fp);
4007 break;
4009 case QEMU_OPTION_qtest:
4010 qtest_chrdev = optarg;
4011 break;
4012 case QEMU_OPTION_qtest_log:
4013 qtest_log = optarg;
4014 break;
4015 case QEMU_OPTION_sandbox:
4016 opts = qemu_opts_parse_noisily(qemu_find_opts("sandbox"),
4017 optarg, true);
4018 if (!opts) {
4019 exit(1);
4021 break;
4022 case QEMU_OPTION_add_fd:
4023 #ifndef _WIN32
4024 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
4025 optarg, false);
4026 if (!opts) {
4027 exit(1);
4029 #else
4030 error_report("File descriptor passing is disabled on this "
4031 "platform");
4032 exit(1);
4033 #endif
4034 break;
4035 case QEMU_OPTION_object:
4036 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
4037 optarg, true);
4038 if (!opts) {
4039 exit(1);
4041 break;
4042 case QEMU_OPTION_realtime:
4043 opts = qemu_opts_parse_noisily(qemu_find_opts("realtime"),
4044 optarg, false);
4045 if (!opts) {
4046 exit(1);
4048 enable_mlock = qemu_opt_get_bool(opts, "mlock", true);
4049 break;
4050 case QEMU_OPTION_msg:
4051 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
4052 false);
4053 if (!opts) {
4054 exit(1);
4056 configure_msg(opts);
4057 break;
4058 case QEMU_OPTION_dump_vmstate:
4059 if (vmstate_dump_file) {
4060 error_report("only one '-dump-vmstate' "
4061 "option may be given");
4062 exit(1);
4064 vmstate_dump_file = fopen(optarg, "w");
4065 if (vmstate_dump_file == NULL) {
4066 error_report("open %s: %s", optarg, strerror(errno));
4067 exit(1);
4069 break;
4070 default:
4071 os_parse_cmd_args(popt->index, optarg);
4076 * Clear error location left behind by the loop.
4077 * Best done right after the loop. Do not insert code here!
4079 loc_set_none();
4081 replay_configure(icount_opts);
4083 machine_class = select_machine();
4085 set_memory_options(&ram_slots, &maxram_size, machine_class);
4087 os_daemonize();
4089 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
4090 error_report("could not acquire pid file: %s", strerror(errno));
4091 exit(1);
4094 if (qemu_init_main_loop(&main_loop_err)) {
4095 error_report_err(main_loop_err);
4096 exit(1);
4099 if (qemu_opts_foreach(qemu_find_opts("sandbox"),
4100 parse_sandbox, NULL, NULL)) {
4101 exit(1);
4104 if (qemu_opts_foreach(qemu_find_opts("name"),
4105 parse_name, NULL, NULL)) {
4106 exit(1);
4109 #ifndef _WIN32
4110 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4111 parse_add_fd, NULL, NULL)) {
4112 exit(1);
4115 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4116 cleanup_add_fd, NULL, NULL)) {
4117 exit(1);
4119 #endif
4121 current_machine = MACHINE(object_new(object_class_get_name(
4122 OBJECT_CLASS(machine_class))));
4123 if (machine_help_func(qemu_get_machine_opts(), current_machine)) {
4124 exit(0);
4126 object_property_add_child(object_get_root(), "machine",
4127 OBJECT(current_machine), &error_abort);
4129 if (machine_class->minimum_page_bits) {
4130 if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
4131 /* This would be a board error: specifying a minimum smaller than
4132 * a target's compile-time fixed setting.
4134 g_assert_not_reached();
4138 cpu_exec_init_all();
4140 if (machine_class->hw_version) {
4141 qemu_set_hw_version(machine_class->hw_version);
4144 if (cpu_model && is_help_option(cpu_model)) {
4145 list_cpus(stdout, &fprintf, cpu_model);
4146 exit(0);
4149 if (!trace_init_backends()) {
4150 exit(1);
4152 trace_init_file(trace_file);
4154 /* Open the logfile at this point and set the log mask if necessary.
4156 if (log_file) {
4157 qemu_set_log_filename(log_file, &error_fatal);
4160 if (log_mask) {
4161 int mask;
4162 mask = qemu_str_to_log_mask(log_mask);
4163 if (!mask) {
4164 qemu_print_log_usage(stdout);
4165 exit(1);
4167 qemu_set_log(mask);
4168 } else {
4169 qemu_set_log(0);
4172 /* If no data_dir is specified then try to find it relative to the
4173 executable path. */
4174 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4175 data_dir[data_dir_idx] = os_find_datadir();
4176 if (data_dir[data_dir_idx] != NULL) {
4177 data_dir_idx++;
4180 /* If all else fails use the install path specified when building. */
4181 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4182 data_dir[data_dir_idx++] = CONFIG_QEMU_DATADIR;
4185 /* -L help lists the data directories and exits. */
4186 if (list_data_dirs) {
4187 for (i = 0; i < data_dir_idx; i++) {
4188 printf("%s\n", data_dir[i]);
4190 exit(0);
4193 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
4195 machine_class->max_cpus = machine_class->max_cpus ?: 1; /* Default to UP */
4196 if (max_cpus > machine_class->max_cpus) {
4197 error_report("Number of SMP CPUs requested (%d) exceeds max CPUs "
4198 "supported by machine '%s' (%d)", max_cpus,
4199 machine_class->name, machine_class->max_cpus);
4200 exit(1);
4204 * Get the default machine options from the machine if it is not already
4205 * specified either by the configuration file or by the command line.
4207 if (machine_class->default_machine_opts) {
4208 qemu_opts_set_defaults(qemu_find_opts("machine"),
4209 machine_class->default_machine_opts, 0);
4212 qemu_opts_foreach(qemu_find_opts("device"),
4213 default_driver_check, NULL, NULL);
4214 qemu_opts_foreach(qemu_find_opts("global"),
4215 default_driver_check, NULL, NULL);
4217 if (!vga_model && !default_vga) {
4218 vga_interface_type = VGA_DEVICE;
4220 if (!has_defaults || machine_class->no_serial) {
4221 default_serial = 0;
4223 if (!has_defaults || machine_class->no_parallel) {
4224 default_parallel = 0;
4226 if (!has_defaults || !machine_class->use_virtcon) {
4227 default_virtcon = 0;
4229 if (!has_defaults || !machine_class->use_sclp) {
4230 default_sclp = 0;
4232 if (!has_defaults || machine_class->no_floppy) {
4233 default_floppy = 0;
4235 if (!has_defaults || machine_class->no_cdrom) {
4236 default_cdrom = 0;
4238 if (!has_defaults || machine_class->no_sdcard) {
4239 default_sdcard = 0;
4241 if (!has_defaults) {
4242 default_monitor = 0;
4243 default_net = 0;
4244 default_vga = 0;
4247 if (is_daemonized()) {
4248 /* According to documentation and historically, -nographic redirects
4249 * serial port, parallel port and monitor to stdio, which does not work
4250 * with -daemonize. We can redirect these to null instead, but since
4251 * -nographic is legacy, let's just error out.
4252 * We disallow -nographic only if all other ports are not redirected
4253 * explicitly, to not break existing legacy setups which uses
4254 * -nographic _and_ redirects all ports explicitly - this is valid
4255 * usage, -nographic is just a no-op in this case.
4257 if (nographic
4258 && (default_parallel || default_serial
4259 || default_monitor || default_virtcon)) {
4260 error_report("-nographic cannot be used with -daemonize");
4261 exit(1);
4263 #ifdef CONFIG_CURSES
4264 if (display_type == DT_CURSES) {
4265 error_report("curses display cannot be used with -daemonize");
4266 exit(1);
4268 #endif
4271 if (nographic) {
4272 if (default_parallel)
4273 add_device_config(DEV_PARALLEL, "null");
4274 if (default_serial && default_monitor) {
4275 add_device_config(DEV_SERIAL, "mon:stdio");
4276 } else if (default_virtcon && default_monitor) {
4277 add_device_config(DEV_VIRTCON, "mon:stdio");
4278 } else if (default_sclp && default_monitor) {
4279 add_device_config(DEV_SCLP, "mon:stdio");
4280 } else {
4281 if (default_serial)
4282 add_device_config(DEV_SERIAL, "stdio");
4283 if (default_virtcon)
4284 add_device_config(DEV_VIRTCON, "stdio");
4285 if (default_sclp) {
4286 add_device_config(DEV_SCLP, "stdio");
4288 if (default_monitor)
4289 monitor_parse("stdio", "readline", false);
4291 } else {
4292 if (default_serial)
4293 add_device_config(DEV_SERIAL, "vc:80Cx24C");
4294 if (default_parallel)
4295 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
4296 if (default_monitor)
4297 monitor_parse("vc:80Cx24C", "readline", false);
4298 if (default_virtcon)
4299 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
4300 if (default_sclp) {
4301 add_device_config(DEV_SCLP, "vc:80Cx24C");
4305 #if defined(CONFIG_VNC)
4306 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
4307 display_remote++;
4309 #endif
4310 if (display_type == DT_DEFAULT && !display_remote) {
4311 #if defined(CONFIG_GTK)
4312 display_type = DT_GTK;
4313 #elif defined(CONFIG_SDL)
4314 display_type = DT_SDL;
4315 #elif defined(CONFIG_COCOA)
4316 display_type = DT_COCOA;
4317 #elif defined(CONFIG_VNC)
4318 vnc_parse("localhost:0,to=99,id=default", &error_abort);
4319 #else
4320 display_type = DT_NONE;
4321 #endif
4324 if ((no_frame || alt_grab || ctrl_grab) && display_type != DT_SDL) {
4325 error_report("-no-frame, -alt-grab and -ctrl-grab are only valid "
4326 "for SDL, ignoring option");
4328 if (no_quit && (display_type != DT_GTK && display_type != DT_SDL)) {
4329 error_report("-no-quit is only valid for GTK and SDL, "
4330 "ignoring option");
4333 if (display_type == DT_GTK) {
4334 early_gtk_display_init(request_opengl);
4337 if (display_type == DT_SDL) {
4338 sdl_display_early_init(request_opengl);
4341 qemu_console_early_init();
4343 if (request_opengl == 1 && display_opengl == 0) {
4344 #if defined(CONFIG_OPENGL)
4345 error_report("OpenGL is not supported by the display");
4346 #else
4347 error_report("OpenGL support is disabled");
4348 #endif
4349 exit(1);
4352 page_size_init();
4353 socket_init();
4355 if (qemu_opts_foreach(qemu_find_opts("object"),
4356 user_creatable_add_opts_foreach,
4357 object_create_initial, NULL)) {
4358 exit(1);
4361 if (qemu_opts_foreach(qemu_find_opts("chardev"),
4362 chardev_init_func, NULL, NULL)) {
4363 exit(1);
4366 #ifdef CONFIG_VIRTFS
4367 if (qemu_opts_foreach(qemu_find_opts("fsdev"),
4368 fsdev_init_func, NULL, NULL)) {
4369 exit(1);
4371 #endif
4373 if (qemu_opts_foreach(qemu_find_opts("device"),
4374 device_help_func, NULL, NULL)) {
4375 exit(0);
4378 machine_opts = qemu_get_machine_opts();
4379 if (qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
4380 NULL)) {
4381 object_unref(OBJECT(current_machine));
4382 exit(1);
4385 configure_accelerator(current_machine);
4387 if (qtest_chrdev) {
4388 qtest_init(qtest_chrdev, qtest_log, &error_fatal);
4391 machine_opts = qemu_get_machine_opts();
4392 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4393 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4394 kernel_cmdline = qemu_opt_get(machine_opts, "append");
4395 bios_name = qemu_opt_get(machine_opts, "firmware");
4397 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4398 if (opts) {
4399 boot_order = qemu_opt_get(opts, "order");
4400 if (boot_order) {
4401 validate_bootdevices(boot_order, &error_fatal);
4404 boot_once = qemu_opt_get(opts, "once");
4405 if (boot_once) {
4406 validate_bootdevices(boot_once, &error_fatal);
4409 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
4410 boot_strict = qemu_opt_get_bool(opts, "strict", false);
4413 if (!boot_order) {
4414 boot_order = machine_class->default_boot_order;
4417 if (!kernel_cmdline) {
4418 kernel_cmdline = "";
4419 current_machine->kernel_cmdline = (char *)kernel_cmdline;
4422 linux_boot = (kernel_filename != NULL);
4424 if (!linux_boot && *kernel_cmdline != '\0') {
4425 error_report("-append only allowed with -kernel option");
4426 exit(1);
4429 if (!linux_boot && initrd_filename != NULL) {
4430 error_report("-initrd only allowed with -kernel option");
4431 exit(1);
4434 if (semihosting_enabled() && !semihosting_get_argc() && kernel_filename) {
4435 /* fall back to the -kernel/-append */
4436 semihosting_arg_fallback(kernel_filename, kernel_cmdline);
4439 os_set_line_buffering();
4441 /* spice needs the timers to be initialized by this point */
4442 qemu_spice_init();
4444 cpu_ticks_init();
4445 if (icount_opts) {
4446 if (!tcg_enabled()) {
4447 error_report("-icount is not allowed with hardware virtualization");
4448 exit(1);
4450 configure_icount(icount_opts, &error_abort);
4451 qemu_opts_del(icount_opts);
4454 qemu_tcg_configure(accel_opts, &error_fatal);
4456 if (default_net) {
4457 QemuOptsList *net = qemu_find_opts("net");
4458 qemu_opts_set(net, NULL, "type", "nic", &error_abort);
4459 #ifdef CONFIG_SLIRP
4460 qemu_opts_set(net, NULL, "type", "user", &error_abort);
4461 #endif
4464 colo_info_init();
4466 if (net_init_clients() < 0) {
4467 exit(1);
4470 if (qemu_opts_foreach(qemu_find_opts("object"),
4471 user_creatable_add_opts_foreach,
4472 object_create_delayed, NULL)) {
4473 exit(1);
4476 #ifdef CONFIG_TPM
4477 if (tpm_init() < 0) {
4478 exit(1);
4480 #endif
4482 /* init the bluetooth world */
4483 if (foreach_device_config(DEV_BT, bt_parse))
4484 exit(1);
4486 if (!xen_enabled()) {
4487 /* On 32-bit hosts, QEMU is limited by virtual address space */
4488 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4489 error_report("at most 2047 MB RAM can be simulated");
4490 exit(1);
4494 blk_mig_init();
4495 ram_mig_init();
4497 /* If the currently selected machine wishes to override the units-per-bus
4498 * property of its default HBA interface type, do so now. */
4499 if (machine_class->units_per_default_bus) {
4500 override_max_devs(machine_class->block_default_type,
4501 machine_class->units_per_default_bus);
4504 /* open the virtual block devices */
4505 while (!QSIMPLEQ_EMPTY(&bdo_queue)) {
4506 BlockdevOptions_queue *bdo = QSIMPLEQ_FIRST(&bdo_queue);
4508 QSIMPLEQ_REMOVE_HEAD(&bdo_queue, entry);
4509 loc_push_restore(&bdo->loc);
4510 qmp_blockdev_add(bdo->bdo, &error_fatal);
4511 loc_pop(&bdo->loc);
4512 qapi_free_BlockdevOptions(bdo->bdo);
4513 g_free(bdo);
4515 if (snapshot || replay_mode != REPLAY_MODE_NONE) {
4516 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
4517 NULL, NULL);
4519 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
4520 &machine_class->block_default_type, NULL)) {
4521 exit(1);
4524 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
4525 CDROM_OPTS);
4526 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4527 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
4529 parse_numa_opts(current_machine);
4531 if (qemu_opts_foreach(qemu_find_opts("mon"),
4532 mon_init_func, NULL, NULL)) {
4533 exit(1);
4536 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4537 exit(1);
4538 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4539 exit(1);
4540 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4541 exit(1);
4542 if (foreach_device_config(DEV_SCLP, sclp_parse) < 0) {
4543 exit(1);
4545 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4546 exit(1);
4548 /* If no default VGA is requested, the default is "none". */
4549 if (default_vga) {
4550 if (machine_class->default_display) {
4551 vga_model = machine_class->default_display;
4552 } else if (vga_interface_available(VGA_CIRRUS)) {
4553 vga_model = "cirrus";
4554 } else if (vga_interface_available(VGA_STD)) {
4555 vga_model = "std";
4558 if (vga_model) {
4559 select_vgahw(vga_model);
4562 if (watchdog) {
4563 i = select_watchdog(watchdog);
4564 if (i > 0)
4565 exit (i == 1 ? 1 : 0);
4568 machine_register_compat_props(current_machine);
4570 qemu_opts_foreach(qemu_find_opts("global"),
4571 global_init_func, NULL, NULL);
4573 /* This checkpoint is required by replay to separate prior clock
4574 reading from the other reads, because timer polling functions query
4575 clock values from the log. */
4576 replay_checkpoint(CHECKPOINT_INIT);
4577 qdev_machine_init();
4579 current_machine->ram_size = ram_size;
4580 current_machine->maxram_size = maxram_size;
4581 current_machine->ram_slots = ram_slots;
4582 current_machine->boot_order = boot_order;
4583 current_machine->cpu_model = cpu_model;
4585 machine_run_board_init(current_machine);
4587 realtime_init();
4589 soundhw_init();
4591 if (hax_enabled()) {
4592 hax_sync_vcpus();
4595 if (qemu_opts_foreach(qemu_find_opts("fw_cfg"),
4596 parse_fw_cfg, fw_cfg_find(), NULL) != 0) {
4597 exit(1);
4600 /* init USB devices */
4601 if (machine_usb(current_machine)) {
4602 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4603 exit(1);
4606 /* Check if IGD GFX passthrough. */
4607 igd_gfx_passthru();
4609 /* init generic devices */
4610 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
4611 if (qemu_opts_foreach(qemu_find_opts("device"),
4612 device_init_func, NULL, NULL)) {
4613 exit(1);
4616 cpu_synchronize_all_post_init();
4618 rom_reset_order_override();
4621 * Create frontends for -drive if=scsi leftovers.
4622 * Normally, frontends for -drive get created by machine
4623 * initialization for onboard SCSI HBAs. However, we create a few
4624 * more ever since SCSI qdevification, but this is pretty much an
4625 * implementation accident, and deprecated.
4627 scsi_legacy_handle_cmdline();
4629 /* Did we create any drives that we failed to create a device for? */
4630 drive_check_orphaned();
4632 /* Don't warn about the default network setup that you get if
4633 * no command line -net or -netdev options are specified. There
4634 * are two cases that we would otherwise complain about:
4635 * (1) board doesn't support a NIC but the implicit "-net nic"
4636 * requested one
4637 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
4638 * sets up a nic that isn't connected to anything.
4640 if (!default_net) {
4641 net_check_clients();
4645 if (boot_once) {
4646 qemu_boot_set(boot_once, &error_fatal);
4647 qemu_register_reset(restore_boot_order, g_strdup(boot_order));
4650 ds = init_displaystate();
4652 /* init local displays */
4653 switch (display_type) {
4654 case DT_CURSES:
4655 curses_display_init(ds, full_screen);
4656 break;
4657 case DT_SDL:
4658 sdl_display_init(ds, full_screen, no_frame);
4659 break;
4660 case DT_COCOA:
4661 cocoa_display_init(ds, full_screen);
4662 break;
4663 case DT_GTK:
4664 gtk_display_init(ds, full_screen, grab_on_hover);
4665 break;
4666 default:
4667 break;
4670 /* must be after terminal init, SDL library changes signal handlers */
4671 os_setup_signal_handling();
4673 /* init remote displays */
4674 #ifdef CONFIG_VNC
4675 qemu_opts_foreach(qemu_find_opts("vnc"),
4676 vnc_init_func, NULL, NULL);
4677 #endif
4679 if (using_spice) {
4680 qemu_spice_display_init();
4683 #ifdef CONFIG_OPENGL_DMABUF
4684 if (display_type == DT_EGL) {
4685 egl_headless_init();
4687 #endif
4689 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
4690 exit(1);
4693 qdev_machine_creation_done();
4695 /* TODO: once all bus devices are qdevified, this should be done
4696 * when bus is created by qdev.c */
4697 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4698 qemu_run_machine_init_done_notifiers();
4700 if (rom_check_and_register_reset() != 0) {
4701 error_report("rom check and register reset failed");
4702 exit(1);
4705 replay_start();
4707 /* This checkpoint is required by replay to separate prior clock
4708 reading from the other reads, because timer polling functions query
4709 clock values from the log. */
4710 replay_checkpoint(CHECKPOINT_RESET);
4711 qemu_system_reset(SHUTDOWN_CAUSE_NONE);
4712 register_global_state();
4713 if (replay_mode != REPLAY_MODE_NONE) {
4714 replay_vmstate_init();
4715 } else if (loadvm) {
4716 Error *local_err = NULL;
4717 if (load_vmstate(loadvm, &local_err) < 0) {
4718 error_report_err(local_err);
4719 autostart = 0;
4723 qdev_prop_check_globals();
4724 if (vmstate_dump_file) {
4725 /* dump and exit */
4726 dump_vmstate_json_to_file(vmstate_dump_file);
4727 return 0;
4730 if (incoming) {
4731 Error *local_err = NULL;
4732 qemu_start_incoming_migration(incoming, &local_err);
4733 if (local_err) {
4734 error_reportf_err(local_err, "-incoming %s: ", incoming);
4735 exit(1);
4737 } else if (autostart) {
4738 vm_start();
4741 os_setup_post();
4743 main_loop();
4744 replay_disable_events();
4745 iothread_stop_all();
4747 bdrv_close_all();
4748 pause_all_vcpus();
4749 res_free();
4751 /* vhost-user must be cleaned up before chardevs. */
4752 net_cleanup();
4753 audio_cleanup();
4754 monitor_cleanup();
4755 qemu_chr_cleanup();
4756 /* TODO: unref root container, check all devices are ok */
4758 return 0;